From e933cc53fe23a7ab47176f1d13fc37ceb7895f53 Mon Sep 17 00:00:00 2001 From: Rob Cermak Date: Tue, 10 May 2022 16:00:41 -0800 Subject: [PATCH] Updates - FMS version 1 notes: variables in the grid file may be double; the `tile` variable should not be written. - saveGrid(): Add option to prevent `tile` variable from being written. Use `noTile=True`. --- docs/development/CHANGELOG.md | 4 ++++ examples/mkGridsExample07.py | 4 +++- gridtools/gridutils.py | 43 ++++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/docs/development/CHANGELOG.md b/docs/development/CHANGELOG.md index 1939518..2f3b480 100644 --- a/docs/development/CHANGELOG.md +++ b/docs/development/CHANGELOG.md @@ -4,6 +4,10 @@ Updates + - FMS version 1 notes: variables in the grid file may be double; the + `tile` variable should not be written. + - saveGrid(): Add option to prevent `tile` variable from being written. + Use `noTile=True`. - Track down more encoding issues in code to remove FMS coupler complaints about data types. - Fixed some whitespace issues. diff --git a/examples/mkGridsExample07.py b/examples/mkGridsExample07.py index 33f8c5d..0c287d6 100644 --- a/examples/mkGridsExample07.py +++ b/examples/mkGridsExample07.py @@ -149,7 +149,9 @@ inputDirectory=inputDir, overwrite=True, ) -grd.saveGrid(filename=os.path.join(inputDir, "ocean_hgrid_7.nc")) + +# The FMS coupler (v1) does not like the 'tile' variable +grd.saveGrid(filename=os.path.join(inputDir, "ocean_hgrid_7.nc"), noTile=True) # Do some plotting! diff --git a/gridtools/gridutils.py b/gridtools/gridutils.py index 682528e..36c9575 100644 --- a/gridtools/gridutils.py +++ b/gridtools/gridutils.py @@ -187,7 +187,7 @@ def debugMsg(self, msg, level = -1): .. note:: Currently defined debug levels: - 0=off + 0=off 1=log debugging messages 2=raise an exception 3=stop with a pdb breakpoint after logging message @@ -1817,11 +1817,32 @@ def removeFillValueAttributes(self, data=None, stringVars=None): ncEncoding[ncVar] = {'dtype': 'S%d' % (stringVars[ncVar]), 'char_dim_name': 'string'} return ncEncoding - - def saveGrid(self, filename=None, directory=None, enc=None): + + def saveGrid(self, filename=None, directory=None, enc=None, noTile=False): ''' - This operation is destructive using the last known filename which can be overridden. + This function writes the current grid from memory to a netCDF file. + + If the grid was opened from an existing grid, the last known filename + will be used and overwrite the original grid file. The filename + can be overridden to keep the original grid file intact. + + If grid is a new grid, no filename exists and will need to be passed + as an argument to this function. + + **Keyword arguments** + + * *filename* (``str``) -- the grid filename to use for saving the grid. + * *directory* (``str``) -- directory path to use for saving the grid. + * *enc* (``str``) -- this passes a dtype to all the grid variables. Default: double or float64 + * *noTile* (``boolean``) -- do not write the ``tile`` variable. Default: False + + .. note:: + + FMS version 1 does not like the ``tile`` variable within the grid file. To + exclude this variable, use ``noTile=True``. FMS version 1 is ok with variables + being double precision. If single precision is required, set ``enc='float32'``. ''' + if filename: if directory: filename = os.path.join(directory, filename) @@ -1836,16 +1857,22 @@ def saveGrid(self, filename=None, directory=None, enc=None): if self.grid['x'].attrs['units'] == 'degrees_east': self.grid['x'].values = np.where(self.grid['x'].values>180, self.grid['x'].values-360, self.grid['x'].values) - #Duplicate - #self.grid.to_netcdf(self.xrFilename, encoding=self.removeFillValueAttributes()) + # User a temporary copy of self.grid so the original contents are not + # modified + toWrite = self.grid.copy() + + # For FMS, do not write the tile variable in the grid file + if noTile: + if 'tile' in toWrite: + toWrite = toWrite.drop('tile') # Save the grid here try: - gridEncoding = self.removeFillValueAttributes() + gridEncoding = self.removeFillValueAttributes(toWrite) if enc: for vrb in ['x', 'y', 'dx', 'dy', 'angle_dx', 'area']: gridEncoding[vrb] = {'dtype': enc} - self.grid.to_netcdf(self.xrFilename, encoding = gridEncoding) + toWrite.to_netcdf(self.xrFilename, encoding = gridEncoding) msg = "Successfully wrote netCDF file to %s" % (self.xrFilename) self.printMsg(msg, level=logging.INFO) except: