-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgfun_user.py
327 lines (271 loc) · 11.8 KB
/
gfun_user.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
User-specific code for pgrid.
You would edit the information to reflect whatever grid you are working on.
"""
import numpy as np
import pandas as pd
from lo_tools import zfun, Lfun
import sys
from pathlib import Path
pth = Path(__file__).absolute().parent.parent.parent / 'LO' / 'pgrid'
if str(pth) not in sys.path:
sys.path.append(str(pth))
import gfun_utility as gfu
import gfun
# This is the name of the grid that you are working on.
gridname = 'oly1'
# default s-coordinate info (could override below)
s_dict = {'THETA_S': 4, 'THETA_B': 2, 'TCLINE': 10, 'N': 30,
'VTRANSFORM': 2, 'VSTRETCHING': 4}
def make_initial_info(gridname=gridname):
# Add an elif section for your grid.
if gridname == 'test0':
# A large grid, used as a test.
dch = gfun.default_choices()
aa = [-130, -122, 42, 52]
res = 1000 # target resolution (m)
Lon_vec, Lat_vec = gfu.simple_grid(aa, res)
dch['nudging_edges'] = ['north','south','west']
# Make the rho grid.
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
# Initialize bathymetry
dch['t_list'] = ['srtm15plus','cascadia','nw_pacific','psdem',
'ttp_patch','grays_harbor','willapa_bay']
z = gfu.combine_bathy_from_sources(lon, lat, dch)
if dch['use_z_offset']:
z = z + dch['z_offset']
elif gridname == 'hc0':
dch = gfun.default_choices()
aa = [-123.2, -122.537, 47.3, 47.9]
res = 100 # target resolution (m)
Lon_vec, Lat_vec = gfu.simple_grid(aa, res)
dch['nudging_edges'] = ['north']
dch['nudging_days'] = (0.1, 1.0)
# Make the rho grid.
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
# Initialize bathymetry
dch['t_list'] = ['psdem']
z = gfu.combine_bathy_from_sources(lon, lat, dch)
if dch['use_z_offset']:
z = z + dch['z_offset']
elif gridname == 'ai0':
dch = gfun.default_choices()
aa = [-122.82, -122.36, 47.758, 48.18]
res = 100 # target resolution (m)
Lon_vec, Lat_vec = gfu.simple_grid(aa, res)
dch['nudging_edges'] = ['north', 'south', 'east', 'west']
dch['nudging_days'] = (0.1, 1.0)
# Make the rho grid.
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
# Initialize bathymetry
dch['t_list'] = ['psdem']
z = gfu.combine_bathy_from_sources(lon, lat, dch)
if dch['use_z_offset']:
z = z + dch['z_offset']
elif gridname == 'oly1':
# South Sound, new version, 2024.11.25
# Includes Vashon Island and Colvos Passage
dch = gfun.default_choices()
dch['z_offset'] = -2 # same logic as wgh2
dch['excluded_rivers'] = ['skokomish']
dch['do_traps'] = True
dch['excluded_triv'] = ['Lynch Cove','Tahuya']
dch['excluded_wwtp'] = ['Alderbrook']
aa = [-123.12, -122.3, 47.02, 47.53]
res = 100 # target resolution (m)
Lon_vec, Lat_vec = gfu.simple_grid(aa, res)
dch['nudging_edges'] = ['north']
dch['nudging_days'] = (0.1, 1.0)
# by setting a small min_depth were are planning to use
# wetting and drying in ROMS, but maintaining positive depth
# for all water cells
dch['min_depth'] = 0.2 # meters (positive down)
# Make the rho grid.
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
# Initialize bathymetry
dch['t_list'] = ['psdem']
z = gfu.combine_bathy_from_sources(lon, lat, dch)
if dch['use_z_offset']:
z = z + dch['z_offset']
elif gridname == 'wgh2':
# Willapa Bay and Grays Harbor nest
dch = gfun.default_choices()
aa = [-124.4,-123.7,46.35,47.1]
res = 200 # target resolution (m)
Lon_vec, Lat_vec = gfu.simple_grid(aa, res)
dch['z_offset'] = -2
# The docs for nw_pacific say the vertical datum is "sea level" and for Willapa
# Bay and Grays Harbor it is MLLW so to match
# this we would use z_offset = 0 or -1, but the intention here is to make the z=0
# level be higher up, so that we catch more of the intertidal when using
# WET_DRY. This should be matched by a similar intervention to zeta in ocnN.
dch['nudging_edges'] = ['north', 'south', 'west']
dch['nudging_days'] = (0.1, 1.0)
# by setting a small min_depth were are planning to use
# WET_DRY in ROMS, but maintaining positive depth
# for all water cells
dch['min_depth'] = 0.2 # meters (positive down)
# Make the rho grid.
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
# Initialize bathymetry
dch['t_list'] = ['nw_pacific','grays_harbor','willapa_bay']
z = gfu.combine_bathy_from_sources(lon, lat, dch)
if dch['use_z_offset']:
z = z + dch['z_offset']
elif gridname == 'sq0':
# Sequim Bay nest
dch = gfun.default_choices()
lon_list = [-123.095, -123.059, -123.033, -122.96]
x_res_list = [50, 30, 30, 50]
lat_list = [48.016, 48.075, 48.097, 48.2]
y_res_list = [50, 30, 30, 100]
Lon_vec, Lat_vec = gfu.stretched_grid(lon_list, x_res_list, lat_list, y_res_list)
dch['z_offset'] = -2
# Assume the vertical datum is "sea level" [Check later, the server is down.].
# The intention here is to make the z=0
# level be higher up, so that we catch more of the intertidal when using
# WET_DRY. This should be matched by a similar intervention to zeta in ocnN.
dch['nudging_edges'] = ['north', 'east', 'west']
dch['nudging_days'] = (0.1, 1.0)
# by setting a small min_depth were are planning to use
# WET_DRY in ROMS, but maintaining positive depth
# for all water cells
dch['min_depth'] = 0.2 # meters (positive down)
# Make the rho grid.
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
# Initialize bathymetry
dch['t_list'] = ['nw_pacific','psdem']
z = gfu.combine_bathy_from_sources(lon, lat, dch)
if dch['use_z_offset']:
z = z + dch['z_offset']
elif gridname == 'sq1':
# Sequim Bay nest, lower resolution than sq0
dch = gfun.default_choices()
lon_list = [-123.095, -123.059, -123.033, -122.96]
x_res_list = [100, 60, 60, 100]
lat_list = [48.016, 48.075, 48.097, 48.2]
y_res_list = [100, 60, 60, 100]
Lon_vec, Lat_vec = gfu.stretched_grid(lon_list, x_res_list, lat_list, y_res_list)
dch['z_offset'] = -2
# Assume the vertical datum is "sea level" [Check later, the server is down.].
# The intention here is to make the z=0
# level be higher up, so that we catch more of the intertidal when using
# WET_DRY. This should be matched by a similar intervention to zeta in ocnN.
dch['nudging_edges'] = ['north', 'east', 'west']
dch['nudging_days'] = (0.1, 1.0)
# by setting a small min_depth were are planning to use
# WET_DRY in ROMS, but maintaining positive depth
# for all water cells
dch['min_depth'] = 0.2 # meters (positive down)
# Make the rho grid.
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
# Initialize bathymetry
dch['t_list'] = ['nw_pacific','psdem']
z = gfu.combine_bathy_from_sources(lon, lat, dch)
if dch['use_z_offset']:
z = z + dch['z_offset']
elif gridname == 'cas2k':
# cas6 domain but with 2 km resolution
dch = gfun.default_choices()
aa = [-130, -122, 42, 52]
res = 2000 # target resolution (m)
Lon_vec, Lat_vec = gfu.simple_grid(aa, res)
dch['nudging_edges'] = ['north', 'south', 'west']
dch['nudging_days'] = (3.0, 60.0)
# Make the rho grid.
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
# Initialize bathymetry
dch['t_list'] = ['srtm15plus','cascadia','nw_pacific','psdem']
z = gfu.combine_bathy_from_sources(lon, lat, dch)
if dch['use_z_offset']:
z = z + dch['z_offset']
elif gridname == 'cas7':
# based completely on cas6 except we carve out Agate Pass and
# Swinomish Channel by hand. This is an example of working from an
# existing grid.
dch = gfun.default_choices()
dch['nudging_edges'] = ['north', 'south', 'west']
dch['nudging_days'] = (3.0, 60.0)
Ldir = Lfun.Lstart()
fn = Ldir['parent'] / 'LO_output' / 'pgrid' / 'cas6' / 'grid.nc'
dch['maskfile_to_copy'] = fn
dch['remove_islands'] = False
dch['trim_grid'] = False
import xarray as xr
ds = xr.open_dataset(fn)
z = -ds.h.values
lon = ds.lon_rho.values
lat = ds.lat_rho.values
# The plan is to only run:
# start_grid
# make_mask
# edit_mask
# (don't run carve_rivers - just copy the file from cas6)
# smooth_grid
# make_extras
# grid_to_LO
elif gridname == 'ae0':
# analytical model estuary
dch = gfun.default_choices()
lon_list = [-2, 0, 1, 2]
x_res_list = [2500, 500, 500, 2500]
lat_list = [43, 44.9, 45.1, 47]
y_res_list = [2500, 500, 500, 2500]
Lon_vec, Lat_vec = gfu.stretched_grid(lon_list, x_res_list,
lat_list, y_res_list)
lon, lat = np.meshgrid(Lon_vec, Lat_vec)
dch['analytical'] = True
dch['nudging_edges'] = ['north', 'south', 'west']
dch['use_z_offset'] = False
# tidy up dch
dch['z_offset'] = 0.0
dch['t_dir'] = 'BLANK'
dch['t_list'] = ['BLANK']
# make bathymetry by hand
z = np.zeros(lon.shape)
x, y = zfun.ll2xy(lon, lat, 0, 45)
zshelf = x * 1e-3
zestuary = -20 + 20*x/1e5 + 20/(1e4)*np.abs(y)
z = zshelf
mask = zestuary < z
z[mask] = zestuary[mask]
# create a river file by hand
Ldir = Lfun.Lstart()
dch['ctag'] = 'ae0_v0'
ri_dir = Ldir['LOo'] / 'pre' / 'river1' / dch['ctag']
Lfun.make_dir(ri_dir)
gri_fn = ri_dir / 'river_info.csv'
with open(gri_fn, 'w') as rf:
rf.write('rname,usgs,ec,nws,ratio,depth,flow_units,temp_units\n')
rf.write('creek0,,,,1.0,5.0,m3/s,degC\n')
# and make a track for the river
track_dir = ri_dir / 'tracks'
Lfun.make_dir(track_dir)
track_fn = track_dir / 'creek0.p'
track_df = pd.DataFrame()
NTR = 100
if True:
track_df['lon'] = np.linspace(0,4,NTR) # OK to go past edge of domain
track_df['lat'] = 45*np.ones(NTR)
else: # Debugging with N/S river channel
track_df['lon'] = 0.25*np.ones(NTR)
track_df['lat'] = np.linspace(45,44,NTR) # South to North river
track_df.to_pickle(track_fn)
# *** NOTE: TRACKS MUST GO FROM OCEAN TO LAND ***
else:
print('Error from make_initial_info: unsupported gridname')
return
if dch['trim_grid']:
# check for odd size of grid and trim if needed
NR, NC = lon.shape
if np.mod(NR,2) != 0:
print('- trimming row from grid')
lon = lon[:-1,:]
lat = lat[:-1,:]
z = z[:-1,:]
if np.mod(NC,2) != 0:
print('- trimming column from grid')
lon = lon[:,:-1]
lat = lat[:,:-1]
z = z[:,:-1]
return lon, lat, z, dch