diff --git a/bgcArgoDMQC/core/prof.py b/bgcArgoDMQC/core/prof.py index aa29128..a983967 100644 --- a/bgcArgoDMQC/core/prof.py +++ b/bgcArgoDMQC/core/prof.py @@ -29,8 +29,9 @@ class prof: def __init__(self, wmo, cycle, kind='C', keep_fillvalue=False, rcheck=True, verbose=False): self.__floatdict__, self.__prof__, self.__fillvalue__ = load_profile(io.Path.ARGO_PATH, wmo, cycle, kind=kind) - self.__rawfloatdict__ = self.__floatdict__ + self.__rawfloatdict__ = copy.deepcopy(self.__floatdict__) self._dict = 'raw' + self._changelog = [] # local path info self.argo_path = io.Path.ARGO_PATH @@ -83,7 +84,7 @@ def reset(self): Reset all variables back to original loaded variables. Undoes the effect of clean(), rm_fillvalue(), check_range(). ''' - self.__floatdict__ = copy.deepcopy(self.__rawfloatdict__) + self.__floatdict__ = self.__rawfloatdict__ self._dict = 'raw' self.to_dataframe() @@ -166,12 +167,14 @@ def update_field(self, field, value, where=None): self.__floatdict__[field][where] = value self.set_dict(current_float_dict) + self._changelog.append(field) self.to_dataframe() def set_fillvalue(self, field, where=None): self.update_field(field, self.__fillvalue__[field], where) - def export_files(self): + def update_file(self, history): - io.update_nc(self.__floatdict__, self.__prof__) \ No newline at end of file + export_file = io.update_nc(self.__floatdict__, self.__prof__, self._changelog, history_dict=history) + return export_file \ No newline at end of file diff --git a/bgcArgoDMQC/io/netcdf.py b/bgcArgoDMQC/io/netcdf.py index 26ca8b6..bbfe632 100644 --- a/bgcArgoDMQC/io/netcdf.py +++ b/bgcArgoDMQC/io/netcdf.py @@ -355,6 +355,45 @@ def export_delayed_files(fdict, files, gain, data_mode='D', comment=None, equati sys.stdout.write('done\n') D_nc.close() -def update_nc(fdict, file, changelog, history_dict={}): +def update_nc(fdict, fn, changelog, history_dict={}): + + # get DATE_UPDATE + date_update = pd.Timestamp.now(tz='utc').strftime('%Y%m%d%H%M%S') + + dac = fn.as_posix().split('/')[-4] + output_file = Path(fn.as_posix().replace(f'dac/{dac}/', f'dac/{dac}/E/')) + if not output_file.parent.exists(): + output_file.parent.mkdir(parents=True) + sys.stdout.write(f'Working on file {output_file.as_posix()}...') + + O_nc = copy_netcdf(fn, output_file) + if not O_nc.dimensions['N_HISTORY'].isunlimited(): + O_nc.close() + O_nc = unlimit_dimension(fn, output_file, 'N_HISTORY') + + for f in changelog: + + O_nc[f][:] = fdict[f] + + # if a changed value is QC flags, recalculate PROFILE__QC + if f.split('_')[1] == 'QC': + for i in range(O_nc.dimensions['N_PROF'].size): + flags = read_qc(O_nc[f][:].data[i,:]) + grade = profile_qc(pd.Series(flags)).encode('utf-8') + O_nc[f'PROFILE_{f}'][i] = grade + + history = dict( + HISTORY_INSTITUTION=history_dict['HISTORY_INSTITUTION'], + HISTORY_STEP=history_dict['HISTORY_STEP'], + HISTORY_SOFTWARE='BGQC', + HISTORY_SOFTWARE_RELEASE='v0.2', + HISTORY_DATE=date_update, + HISTORY_ACTION=history_dict['HISTORY_ACTION'] + ) - return \ No newline at end of file + O_nc['DATE_UPDATE'][:] = string_to_array(date_update, O_nc.dimensions['DATE_TIME']) + update_history(O_nc, history) + sys.stdout.write('done\n') + O_nc.close() + + return output_file \ No newline at end of file