Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
 - Issue #21: set `tile` variable to `NC_CHAR` dtype
 - Issue #21: initial fix for mosaic and exchange file handling for land vs
   no land points in ocean grid.
  • Loading branch information
jr3cermak committed May 11, 2022
1 parent e933cc5 commit a10db1b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
8 changes: 8 additions & 0 deletions docs/development/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 2022-05-11

Updates

- Issue #22: fixing up creation of mosaics and exchange grids depending on
what is seen in the land mask. Fix for ocean grid `tile` variable to
make sure it is of type `NC_CHAR`.

## 2022-05-10

Updates
Expand Down
6 changes: 6 additions & 0 deletions docs/development/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
## Milestones

- [ ] Release 0.3.3
- [ ] Resolve Issue #21
- [X] Save `tile` variable as type NC_CHAR in ocean grid
- [ ] Add test for regular grid with land
- [ ] Add test for regular grid without land
- [ ] Add test for regular grid specified to be without land
- [ ] Add test for regular grid with land but error out with noLandMosaic=True specified
- [ ] Resolve Issue #19
- [X] add a keyword option to trigger alternate calcuation of angle_dx
in gridutils.grids.mom6 and gridutils.
Expand Down
23 changes: 18 additions & 5 deletions gridtools/grids/mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ def write_MOM6_exchange_grid_files(self, grd, **kwargs):
# Loop for the three types
for name1, name2 in exchangeGrids:

# If no land mosaic is requested, skip writing these files
if kwargs['noLandMosaic']:
if name1 == 'land' or name2 == 'land':
continue

ds = xr.Dataset()

# calculate the exchange grid for name1 X name2
Expand Down Expand Up @@ -719,9 +724,14 @@ def add_string_var_2d(ds, var_name, dim_name, standard_name, value, strVarMap):
add_string_var_1d(ds, 'atm_mosaic_file', 'atmosphere_mosaic_file_name', self.mom6_grid['filenames']['mosaic'] , strVarMap)
add_string_var_1d(ds, 'atm_mosaic', 'atmosphere_mosaic_name', 'atmos_mosaic' , strVarMap)

add_string_var_1d(ds, 'lnd_mosaic_dir', 'directory_storing_land_mosaic', self.mom6_grid['filenames']['directory'], strVarMap)
add_string_var_1d(ds, 'lnd_mosaic_file', 'land_mosaic_file_name', self.mom6_grid['filenames']['mosaic'] , strVarMap)
add_string_var_1d(ds, 'lnd_mosaic', 'land_mosaic_name', 'land_mosaic' , strVarMap)
if kwargs['noLandMosaic']:
add_string_var_1d(ds, 'lnd_mosaic_dir', 'directory_storing_land_mosaic', self.mom6_grid['filenames']['directory'], strVarMap)
add_string_var_1d(ds, 'lnd_mosaic_file', 'land_mosaic_file_name', self.mom6_grid['filenames']['mosaic'] , strVarMap)
add_string_var_1d(ds, 'lnd_mosaic', 'land_mosaic_name', 'land_mosaic' , strVarMap)
else:
add_string_var_1d(ds, 'lnd_mosaic_dir', 'directory_storing_land_mosaic', self.mom6_grid['filenames']['directory'], strVarMap)
add_string_var_1d(ds, 'lnd_mosaic_file', 'land_mosaic_file_name', self.mom6_grid['filenames']['mosaic'] , strVarMap)
add_string_var_1d(ds, 'lnd_mosaic', 'land_mosaic_name', 'atmos_mosaic' , strVarMap)

add_string_var_1d(ds, 'ocn_mosaic_dir', 'directory_storing_ocean_mosaic', self.mom6_grid['filenames']['directory'], strVarMap)
add_string_var_1d(ds, 'ocn_mosaic_file', 'ocean_mosaic_file_name', self.mom6_grid['filenames']['mosaic'] , strVarMap)
Expand All @@ -731,8 +741,11 @@ def add_string_var_2d(ds, var_name, dim_name, standard_name, value, strVarMap):
add_string_var_1d(ds, 'ocn_topog_file', 'ocean_topog_file_name', self.mom6_grid['filenames']['topography'], strVarMap)

add_string_var_2d(ds, 'aXo_file', 'nfile_aXo', 'atmXocn_exchange_grid_file', self.mom6_grid['filenames']['atmos_ocean_exchange'], strVarMap)
add_string_var_2d(ds, 'aXl_file', 'nfile_aXl', 'atmXlnd_exchange_grid_file', self.mom6_grid['filenames']['atmos_land_exchange'] , strVarMap)
add_string_var_2d(ds, 'lXo_file', 'nfile_lXo', 'lndXocn_exchange_grid_file', self.mom6_grid['filenames']['land_ocean_exchange'] , strVarMap)
if not(kwargs['noLandMosaic']):
add_string_var_2d(ds, 'aXl_file', 'nfile_aXl', 'atmXlnd_exchange_grid_file', self.mom6_grid['filenames']['atmos_land_exchange'] , strVarMap)
add_string_var_2d(ds, 'lXo_file', 'nfile_lXo', 'lndXocn_exchange_grid_file', self.mom6_grid['filenames']['land_ocean_exchange'] , strVarMap)
else:
add_string_var_2d(ds, 'lXo_file', 'nfile_lXo', 'lndXocn_exchange_grid_file', self.mom6_grid['filenames']['atmos_ocean_exchange'], strVarMap)

# Global attributes
self._add_global_attributes(ds)
Expand Down
26 changes: 23 additions & 3 deletions gridtools/gridutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1859,20 +1859,23 @@ def saveGrid(self, filename=None, directory=None, enc=None, noTile=False):

# User a temporary copy of self.grid so the original contents are not
# modified
stringVars = {}
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')
else:
stringVars = {'tile': len(str(self.grid['tile'].data))}

# Save the grid here
try:
gridEncoding = self.removeFillValueAttributes(toWrite)
gridEncoding = self.removeFillValueAttributes(toWrite, stringVars=stringVars)
if enc:
for vrb in ['x', 'y', 'dx', 'dy', 'angle_dx', 'area']:
gridEncoding[vrb] = {'dtype': enc}
toWrite.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:
Expand Down Expand Up @@ -2056,6 +2059,7 @@ def makeSoloMosaic(self, **kwargs):
* *overwrite* (``boolean``) -- set True to overwrite existing files. Default: False
* *inputDirectory* (``string``) -- absolute or relative path to write model input files. Default: "INPUT"
* *relativeToINPUTDir* (``string``) -- absolute or relative path for mosaic files to the INPUT directory. Default: "./"
* *noLandMosaic* (``boolean``) -- set True if the grid does not contain land points. Default: False
.. note::
Integers stored in the mosaic tiles must be 32 bit integers. If 64 bit integers are used,
Expand All @@ -2075,6 +2079,10 @@ def makeSoloMosaic(self, **kwargs):
For *oceanGridFile*, if the filename is not provided, the routine will attempt to use
the name provided when the grid was read. The filename may need to be set if the grid
was just constructed using the library.
If no land points are detected, the ``atmos_mosaic_tile1Xland_mosaic_tile1.nc`` file is omitted
from the mosaic tiles. If it is known that the grid does not have land points, the
``noLandMosaic=True`` can be used to further simply the generated mosaic tiles.
'''

# Check and set any defaults to kwargs
Expand All @@ -2097,15 +2105,27 @@ def makeSoloMosaic(self, **kwargs):
utils.checkArgument(kwargs, 'overwrite', False)
utils.checkArgument(kwargs, 'inputDirectory', "INPUT")
utils.checkArgument(kwargs, 'relativeToINPUTDir', "./")
utils.checkArgument(kwargs, 'noLandMosaic', False)

if len(self.grid.variables) == 0:
# No grid found
msg = ("ERROR: A grid first must be defined by reading and existing grid or creating a new grid.")
self.printMsg(msg, level=logging.ERROR)
return

# Load the MOM6() class
mdl = importlib.import_module('gridtools.grids.mom6')
mom6 = mdl.MOM6()

# Do some solo_mosaic and coupler_mosaic adjustments here
landMask = mom6._generate_mask('land', self, **kwargs)
utils.checkArgument(kwargs, 'haveLandPoints', bool((landMask == 1).any()))

if kwargs['noLandMosaic'] and kwargs['haveLandPoints']:
msg = ("ERROR: The noLandMosaic option cannot be used if land points exist in the ocean grid.")
self.printMsg(msg, level=logging.ERROR)
return

# Supergrid is stored via GridUtils.saveGrid()
mom6.write_MOM6_topography_file(self, **kwargs)
mom6.write_MOM6_solo_mosaic_file(self, **kwargs)
Expand Down Expand Up @@ -2726,7 +2746,7 @@ def extendGridDetectMethod(self):
to see how the grid should be extended.
**Sources probed for information**:
* Global attribute: projection
* Variable 'tile' attribute: geometry
'''
Expand Down

0 comments on commit a10db1b

Please sign in to comment.