Skip to content

Commit

Permalink
Add return value to update_nc
Browse files Browse the repository at this point in the history
  • Loading branch information
tukiains committed Sep 10, 2021
1 parent 225a6ae commit b58559b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 9 additions & 3 deletions cloudnetpy/concat_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/test_concat_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b58559b

Please sign in to comment.