forked from ESMG/gridtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkGridsExample03FRE.py
226 lines (200 loc) · 7.8 KB
/
mkGridsExample03FRE.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import sys, os, logging, cartopy
import numpy as np
from gridtools.gridutils import GridUtils
from gridtools.datasource import DataSource
from gridtools.sysinfo import SysInfo
# Initialize a grid object
grd = GridUtils()
sysObj = SysInfo()
# Define a place to write example files
# FRE-NCtools will write files in the current directory
# so we need to switch to that directory first.
wrkDir = '/import/AKWATERS/jrcermakiii/configs/FRE'
os.chdir(wrkDir)
# This makes the horizontal_grid.nc file
pgm = 'make_hgrid'
if grd.isAvailable(pgm):
cmd = "%s --grid_type regular_lonlat_grid --nxbnds 2 --nybnds 2 --xbnds -140,-120 --ybnds 25,55 --nlon 40 --nlat 60" % (pgm)
(stdout, stderr, rc) = sysObj.runCommand(cmd)
if rc == 0:
msg = "SUCCESS: %s" % (pgm)
grd.printMsg(msg, level=logging.INFO)
else:
msg = "ERROR: Failed to run: %s" % (cmd)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
else:
msg = "Executable not found: %s" % (pgm)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
# Ocean and Atmosphere are on the same grid
# Copy horizontal_grid.nc to ocean_hgrid.nc and atmos_hgrid.nc
cmd = "cp horizontal_grid.nc ocean_hgrid.nc"
(stdout, stderr, rc) = sysObj.runCommand(cmd)
cmd = "cp horizontal_grid.nc atmos_hgrid.nc"
(stdout, stderr, rc) = sysObj.runCommand(cmd)
# Create solo mosaic files
pgm = 'make_solo_mosaic'
if grd.isAvailable(pgm):
cmd = "%s --num_tiles 1 --dir ./ --mosaic_name ocean_mosaic --tile_file ocean_hgrid.nc" % (pgm)
(stdout, stderr, rc) = sysObj.runCommand(cmd)
if rc == 0:
msg = "SUCCESS: %s" % (pgm)
grd.printMsg(msg, level=logging.INFO)
else:
msg = "ERROR: Failed to run: %s" % (cmd)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
cmd = "%s --num_tiles 1 --dir ./ --mosaic_name atmos_mosaic --tile_file atmos_hgrid.nc" % (pgm)
(stdout, stderr, rc) = sysObj.runCommand(cmd)
if rc == 0:
msg = "SUCCESS: %s" % (pgm)
grd.printMsg(msg, level=logging.INFO)
else:
msg = "ERROR: Failed to run: %s" % (cmd)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
else:
msg = "Executable not found: %s" % (pgm)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
# Create a vertical grid
# This is an arbitrary vertical grid taken from another example. The
# selection of bounds here do not necessarily make sense for this domain!
pgm = 'make_vgrid'
if grd.isAvailable(pgm):
cmd = "%s --nbnds 3 --bnds 0.,220.,5500. --dbnds 10.,10.,367.14286 --center c_cell --grid_name ocean_vgrid" % (pgm)
(stdout, stderr, rc) = sysObj.runCommand(cmd)
if rc == 0:
msg = "SUCCESS: %s" % (pgm)
grd.printMsg(msg, level=logging.INFO)
else:
msg = "ERROR: Failed to run: %s" % (cmd)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
else:
msg = "Executable not found: %s" % (pgm)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
# Create a useable GEBCO topographic dataset
# The program make_topog requires:
# - missing_value must exist and be of type NC_DOUBLE, NC_FLOAT, NC_INT, NC_SHORT or NC_CHAR
# - variable with topo/bathy info must be NC_DOUBLE, NC_FLOAT or NC_INT
# - a vertical grid: ocean_vgrid.nc
if not(os.path.isfile('GEBCO_subset.nc')):
msg = "Creating GEBCO subset for MOM6 model grid."
grd.printMsg(msg, level=logging.INFO)
# External data sources are required
# This creates an empty data source catalog
ds = DataSource()
# Connect the catalog to the grid object
grd.useDataSource(ds)
# For variableMap, matching variable values will be renamed to the
# variable key. For evalMap, variables in the expression need
# to be in brackets. If the key is new, a new field will be
# created with the given expression.
ds.addDataSource({
'GEBCO_2020': {
'url' : 'file:///import/AKWATERS/jrcermakiii/bathy/gebco/GEBCO_2020.nc',
'variableMap' : {
'lat': 'lat',
'lon': 'lon',
'depth' : 'elevation'
},
'evalMap': {
'depth' : '-[depth]'
}
}
})
# Load the dataset
gebco = grd.openDataset("ds:GEBCO_2020")
la = gebco.coords['lat']
lo = gebco.coords['lon']
# Subset GEBCO to the extent of the MOM6 model grid
gebcoSubset =\
gebco.loc[dict(lon=lo[(lo >= -150) & (lo <= -110)], lat=la[(la >= 20) & (la <= 60)])]
# Need to use Numpy to assign a specific dtype to missing_value
# otherwise (-9999) will yield a long integer.
gebcoSubset['depth'].attrs['missing_value'] = np.array([-9999], dtype='int32')[0]
# Update metadata
gebcoSubset['depth'].attrs['standard_name'] = "height_below_reference_ellipsoid"
gebcoSubset['depth'].attrs['long_name'] = "Depth relative to sea level"
del gebcoSubset['depth'].attrs['sdn_parameter_urn']
del gebcoSubset['depth'].attrs['sdn_parameter_name']
del gebcoSubset['depth'].attrs['sdn_uom_urn']
del gebcoSubset['depth'].attrs['sdn_uom_name']
# Convert elevation=>depth to NC_INT(int32)
gebcoSubset.to_netcdf('GEBCO_subset.nc', encoding =
{'depth': {'dtype': 'int32'}})
else:
msg = "The GEBCO subset for MOM6 model grid already exists."
grd.printMsg(msg, level=logging.INFO)
# Create the topography for the MOM6 model grid
pgm = "make_topog"
if grd.isAvailable(pgm):
cmd = "%s --verbose --mosaic ocean_mosaic.nc --topog_type realistic --scale_factor -1 --vgrid ocean_vgrid.nc --output ocean_topog.nc --topog_file GEBCO_subset.nc --topog_field depth" % (pgm)
(stdout, stderr, rc) = sysObj.runCommand(cmd)
if rc == 0:
msg = "SUCCESS: %s" % (pgm)
grd.printMsg(msg, level=logging.INFO)
else:
msg = "ERROR: Failed to run: %s" % (cmd)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
else:
msg = "Executable not found: %s" % (pgm)
grd.printMsg(msg, level=logging.ERROR)
sys.exit()
# Read the MOM6 model grid
grd.openGrid('ocean_hgrid.nc')
grd.readGrid()
# Define plot parameters for this model grid
grd.setPlotParameters(
{
'figsize': (8,8),
'projection': {
'name': 'Mercator',
'lat_0': 40.0,
'lon_0': 230.0
},
'extent': [-160.0 ,-100.0, 20.0, 60.0],
'iLinewidth': 1.0,
'jLinewidth': 1.0,
'showGridCells': True,
'title': "Mercator: 1.0 deg x 1.0 deg",
'satelliteHeight': 35785831.0,
'transform': cartopy.crs.PlateCarree(),
'iColor': 'k',
'jColor': 'k'
}
)
(figure, axes) = grd.plotGrid()
# Show the FRE model grids
msg = "Creating MOM6 model grid graphics."
grd.printMsg(msg, level=logging.INFO)
figure.savefig(os.path.join(wrkDir, 'MERC_20x30_Example3FRE.jpg'),
dpi=None, facecolor='w', edgecolor='w',
orientation='portrait', transparent=False, bbox_inches=None,
pad_inches=0.1)
figure.savefig(os.path.join(wrkDir, 'MERC_20x30_Example3FRE.pdf'),
dpi=None, facecolor='w', edgecolor='w',
orientation='portrait', transparent=False, bbox_inches=None,
pad_inches=0.1)
# Plot the diagnosed ocean topography
ocean_topog = grd.openDataset('ocean_topog.nc')
(figure, axes) = grd.plotGrid(
showModelGrid=False,
plotVariables={
'depth': {
'values': ocean_topog['depth'],
'title': 'Diagnosed ocean topographic field from make_topog',
'cbar_kwargs': {
'orientation': 'horizontal',
}
}
},
)
msg = "Creating MOM6 model grid diagnosed ocean topography graphic."
grd.printMsg(msg, level=logging.INFO)
figure.savefig(os.path.join(wrkDir, 'MERC_20x30_FREBathy.png'), dpi=None, facecolor='w', edgecolor='w',
orientation='landscape', transparent=False, bbox_inches=None, pad_inches=0.1)