Skip to content

Commit

Permalink
function for file export - success!
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrdn committed Mar 13, 2024
1 parent 60d8579 commit 7f6af3a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
11 changes: 7 additions & 4 deletions bgcArgoDMQC/core/prof.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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__)
export_file = io.update_nc(self.__floatdict__, self.__prof__, self._changelog, history_dict=history)
return export_file
43 changes: 41 additions & 2 deletions bgcArgoDMQC/io/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_<PARAM>_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
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

0 comments on commit 7f6af3a

Please sign in to comment.