Skip to content
Snippets Groups Projects
Commit d2fcff9b authored by Woude, Auke van der's avatar Woude, Auke van der
Browse files

Added functionality to write dictionaries to file; write GPP and TER separately

parent 56d15fda
Branches near-real-time
No related tags found
No related merge requests found
......@@ -35,9 +35,18 @@ class Regional(object):
self.name = 'Here goes the flux name' # Overwrite in the subclasses
def create_filename(self):
def create_filename(self, change_name=None, outdir=None):
"""Create the filename for a regional file"""
return '{0}/regional.{1}.nc'.format(self.dacycle['dir.output'], self.name)
# Add possibility to write to a different file than self.name
if change_name is None:
name = self.name
else:
name = change_name
if outdir is None:
outdir = self.dacycle['dir.output']
return '{0}/regional.{1}.nc'.format(outdir, name)
def get_filenames(self):
"""Get all filenames for the current month"""
......@@ -125,9 +134,12 @@ class Regional(object):
A[iy, :] = Alat
return A # m2
def write(self, values, varname=None):
def write(self, values, varname=None, filename=None, outdir=None):
# Write the biosphere fluxes to a file
filename = self.create_filename()
if outdir is None:
outdir = self.dacycle['dir.output']
filename = self.create_filename(change_name=filename, outdir=outdir)
logging.debug('Writing fluxes to file {}'.format(filename))
if varname is None:
......@@ -143,8 +155,8 @@ class Regional(object):
values = np.nan_to_num(values)
# Only make the file if this is the first variable to put there
if i == 0:
if not os.path.exists(self.dacycle['dir.output']):
os.mkdir(self.dacycle['dir.output'])
if not os.path.exists(outdir):
os.mkdir(outdir)
with io.CT_CDF(filename, method='create') as f:
dimtime, dimlat, dimlon = f.add_dimensions(self.times, self.lats, self.lons)
......@@ -196,9 +208,14 @@ class Global(Regional):
dtstart=self.startdate, until=self.enddate)) # rrule includes until
self.times = times
def create_filename(self):
def create_filename(self, change_name=None):
"""Create the filename for a regional file"""
return '{0}/global.{1}_{2:%Y%m}.nc'.format(self.dacycle['dir.output'], self.name, self.dacycle['time.start'])
# Add possibility to write to a different file than self.name
if change_name is None:
name = self.name
else:
name = change_name
return '{0}/global.{1}_{2:%Y%m}.nc'.format(self.dacycle['dir.output'], name, self.dacycle['time.start'])
def combine_with_regional(self):
"""Combine the high resolution with global fluxes
......
......@@ -33,12 +33,17 @@ class SiBUpscaler(Regional):
self.sibfile = self.sibdir + sibfile
self.sibfile_avg = self.sibfile.replace('lu.qp3', 'g.qp2')
logging.debug('Reading SiB data from {}'.format(self.sibfile))
# Write GPP and TER
datadict = {}
for v in ('nee', 'resp_tot', 'assim'):
for v in ('resp_tot', 'assim'):
logging.info(f'Getting high-res {v}')
data = self.load_biosphere_emissions(v)
datadict[v] = data
outfile = self.write(datadict)
outfile = self.write(datadict, filename='bio')
# Write NEP seperately
data = self.load_biosphere_emissions('nee')
outfile = self.write(data)
return outfile, self.name
def load_biosphere_emissions(self, fluxname):
......@@ -93,6 +98,8 @@ class SiBUpscaler(Regional):
fluxname = 'reco'
elif fluxname == 'assim':
fluxname = 'gpp'
elif fluxname == 'nee':
fluxname == 'nep'
nee_averaged = ds[fluxname]
nee_averaged = nee_averaged[startindex:stopindex, :].astype(np.float32)
nee_averaged = np.flip(st.convert_2d(nee_averaged, latsib, lonsib), axis=-2)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment