Skip to content

Commit

Permalink
improved nc export
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrdn committed May 9, 2024
1 parent 60bebdc commit 94f75ff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
8 changes: 5 additions & 3 deletions bgcArgoDMQC/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def load_argo(local_path, wmo, grid=False, verbose=True):

return floatData, Sprof, BRtraj, meta, fillvalue

def load_profile(local_path, wmo, cyc, kind='C'):
def load_profile(local_path, wmo, cyc, kind='C', direction='A'):
'''
Function to load in all data from a single profile file,
core or BGC.
Expand All @@ -207,6 +207,7 @@ def load_profile(local_path, wmo, cyc, kind='C'):
wmo: float ID number
cyc: cycle number
kind: core ("C") or B ("B") file
direction: ascending ("A") or descending ("D")
Returns:
floatData: python dict() object with Argo variables
Expand All @@ -229,10 +230,11 @@ def load_profile(local_path, wmo, cyc, kind='C'):
cyc = str(cyc) if type(wmo) is not str else cyc

kind = '' if kind == 'C' else kind
direction = '' if direction == 'A' else direction

# check that the file exists - check for D-mode file first
profFile = local_path / dac / wmo / 'profiles' / f'{kind}D{wmo}_{cyc:03d}.nc'
profFile = profFile.parent / f'{kind}R{wmo}_{cyc:03d}.nc' if not profFile.exists() else profFile
profFile = local_path / dac / wmo / 'profiles' / f'{kind}D{wmo}_{cyc:03d}{direction}.nc'
profFile = profFile.parent / f'{kind}R{wmo}_{cyc:03d}{direction}.nc' if not profFile.exists() else profFile

if not profFile.exists():
raise FileNotFoundError(f'No R- or D-mode file: {profFile.absolute()}')
Expand Down
18 changes: 16 additions & 2 deletions bgcArgoDMQC/core/prof.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,22 @@ class prof:

set_dirs = set_dirs

def __init__(self, wmo, cycle, kind='C', keep_fillvalue=False, rcheck=True, verbose=False):
def __init__(self, wmo=None, cycle=None, file=None, kind='C', direction='A', keep_fillvalue=False, rcheck=True, verbose=False):

self.__floatdict__, self.__prof__, self.__fillvalue__ = load_profile(io.Path.ARGO_PATH, wmo, cycle, kind=kind)
if (wmo is not None and cycle is not None) and file is not None:
raise ValueError("wmo/cycle and file cannot be defined at the same time")

file = file.as_posix() if type(file) is not str else file
wmo = file.split('/')[-3] if file is not None else wmo
cycle = file.split('_')[-1].split('.')[0] if file is not None else cycle
direction = cycle[-1] if cycle[-1] == 'D' else direction
cycle = cycle[:-1] if cycle[-1] == 'D' else cycle
kind = file.split('/')[-1][0] if file.split('/')[-1][0] == 'B' else kind

wmo = int(wmo)
cycle = int(cycle)

self.__floatdict__, self.__prof__, self.__fillvalue__ = load_profile(io.Path.ARGO_PATH, wmo, cycle, kind=kind, direction=direction)
self.__rawfloatdict__ = copy.deepcopy(self.__floatdict__)
self._dict = 'raw'
self._changelog = []
Expand All @@ -41,6 +54,7 @@ def __init__(self, wmo, cycle, kind='C', keep_fillvalue=False, rcheck=True, verb
self.WMO = wmo
self.cycle = cycle
self.kind = kind
self.direction = direction

self.to_dataframe()

Expand Down
2 changes: 1 addition & 1 deletion bgcArgoDMQC/io/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def update_nc(fdict, fn, changelog, history_dict={}):
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()}...')
sys.stdout.write(f'Working on file {output_file.name}...')

O_nc = copy_netcdf(fn, output_file)
if not O_nc.dimensions['N_HISTORY'].isunlimited():
Expand Down

0 comments on commit 94f75ff

Please sign in to comment.