From d2fcff9b5f4eec71b6ed39c441fbe2e17e587ec1 Mon Sep 17 00:00:00 2001
From: "Woude, Auke van der" <auke.vanderwoude@wur.nl>
Date: Mon, 7 Nov 2022 16:53:51 +0100
Subject: [PATCH] Added functionality to write dictionaries to file; write GPP
 and TER separately

---
 da/preprocessing/classes.py     | 33 +++++++++++++++++++++++++--------
 da/preprocessing/upscale_bio.py | 11 +++++++++--
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/da/preprocessing/classes.py b/da/preprocessing/classes.py
index d255fc28..c789b11b 100644
--- a/da/preprocessing/classes.py
+++ b/da/preprocessing/classes.py
@@ -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
diff --git a/da/preprocessing/upscale_bio.py b/da/preprocessing/upscale_bio.py
index 7a3d7190..a7726575 100644
--- a/da/preprocessing/upscale_bio.py
+++ b/da/preprocessing/upscale_bio.py
@@ -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)
-- 
GitLab