diff --git a/cloudnetpy/concat_lib.py b/cloudnetpy/concat_lib.py index c471f344..5990a0cf 100644 --- a/cloudnetpy/concat_lib.py +++ b/cloudnetpy/concat_lib.py @@ -5,24 +5,30 @@ from cloudnetpy import utils -def update_nc(old_file: str, new_file: str): - """Appends data to daily netCDF file. +def update_nc(old_file: str, new_file: str) -> int: + """Appends data to existing netCDF file. Args: - old_file: Filename of a daily netCDF file. + old_file: Filename of a existing netCDF file. new_file: Filename of a new file whose data will be appended to the end. + Returns: + 1 = success, 0 = failed to add new data. + Notes: Requires 'time' variable with unlimited dimension. """ + success = 0 nc_old = netCDF4.Dataset(old_file, 'a') nc_new = netCDF4.Dataset(new_file) valid_ind = _find_valid_time_indices(nc_old, nc_new) if len(valid_ind) > 0: _update_fields(nc_old, nc_new, valid_ind) + success = 1 nc_new.close() nc_old.close() + return success def concatenate_files(filenames: list, diff --git a/tests/unit/test_concat_lib.py b/tests/unit/test_concat_lib.py index 6e5bd55f..23409b83 100644 --- a/tests/unit/test_concat_lib.py +++ b/tests/unit/test_concat_lib.py @@ -24,7 +24,8 @@ def run_before_and_after_tests(self): def test_does_append_to_end(self): concat_lib.concatenate_files(self.files[:2], self.filename, concat_dimension='profile') - concat_lib.update_nc(self.filename, self.files[2]) + succ = concat_lib.update_nc(self.filename, self.files[2]) + assert succ == 1 nc = netCDF4.Dataset(self.filename) time = nc.variables['time'][:] assert len(time) == 3 * 12 @@ -33,7 +34,8 @@ def test_does_append_to_end(self): def test_does_not_append_to_beginning(self): concat_lib.concatenate_files(self.files[1:3], self.filename, concat_dimension='profile') - concat_lib.update_nc(self.filename, self.files[0]) + succ = concat_lib.update_nc(self.filename, self.files[0]) + assert succ == 0 nc = netCDF4.Dataset(self.filename) time = nc.variables['time'][:] assert len(time) == 2 * 12 @@ -43,7 +45,8 @@ def test_does_not_append_to_beginning(self): def test_does_not_append_to_middle(self): files = [self.files[0], self.files[2]] concat_lib.concatenate_files(files, self.filename, concat_dimension='profile') - concat_lib.update_nc(self.filename, self.files[1]) + succ = concat_lib.update_nc(self.filename, self.files[1]) + assert succ == 0 nc = netCDF4.Dataset(self.filename) time = nc.variables['time'][:] assert len(time) == 2 * 12