Skip to content

Commit ab4e1ea

Browse files
committed
Update code with new keyword
Date is optional and add tracer module handling
1 parent f24d60c commit ab4e1ea

1 file changed

Lines changed: 60 additions & 32 deletions

File tree

schimpy/bctide.py

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numbers
1212
import numpy as np
1313
import click
14+
import datetime
1415

1516

1617
__all__ = ["load_boundary"]
@@ -98,6 +99,28 @@ def __init__(self, hgrid,bc_yaml=None,boundary_segments=None):
9899
""" initialize the boundary condition from yaml file"""
99100

100101
#main_id = "bctides"
102+
# "date" is optional, default to Jan 1, 2020
103+
# ensure bc_yaml is a dict and normalize/insert a datetime object so the
104+
# later `self.date = bc_yaml["date"]` works reliably
105+
if bc_yaml is None:
106+
bc_yaml = {}
107+
if "date" not in bc_yaml or bc_yaml["date"] is None:
108+
bc_yaml["date"] = datetime.datetime(2020, 1, 1)
109+
else:
110+
d = bc_yaml["date"]
111+
if isinstance(d, str):
112+
# try ISO format first, then common YYYY-MM-DD
113+
try:
114+
bc_yaml["date"] = datetime.datetime.fromisoformat(d)
115+
except Exception:
116+
try:
117+
bc_yaml["date"] = datetime.datetime.strptime(d, "%Y-%m-%d")
118+
except Exception:
119+
raise ValueError(f"Unrecognized date format for bctides date: {d}")
120+
elif isinstance(d, datetime.date) and not isinstance(d, datetime.datetime):
121+
bc_yaml["date"] = datetime.datetime(d.year, d.month, d.day)
122+
else:
123+
raise ValueError(f"Unrecognized date format for bctides date: {d}")
101124
self.date = bc_yaml["date"]
102125
if "earth_tides" in bc_yaml.keys():
103126
self.earth_tides = bc_yaml["earth_tides"]
@@ -187,6 +210,11 @@ def __init__(self, hgrid,bc_yaml=None,boundary_segments=None):
187210
"none": 0,
188211
}
189212

213+
## also reading tracer modules defined in the bctides yaml here
214+
self.tracer_modules = bc_yaml.get("modules", [])
215+
216+
217+
190218
def _norm_earth(self,consts):
191219
out = []
192220
for item in consts:
@@ -201,7 +229,7 @@ def _norm_earth(self,consts):
201229
node = item.get("node_factor", item.get("node", 1.0))
202230
freq = item.get("angular_frequency", item.get("frequency", 0.0))
203231
eqa = item.get(
204-
"earth_equilibrium_argument",
232+
"equilibrium_argument",
205233
item.get("eqa", item.get("phase", 0.0)),
206234
)
207235
out.append(
@@ -210,7 +238,7 @@ def _norm_earth(self,consts):
210238
"amplitude": amp,
211239
"node_factor": node,
212240
"angular_frequency": freq,
213-
"earth_equilibrium_argument": eqa,
241+
"equilibrium_argument": eqa,
214242
}
215243
)
216244
continue
@@ -225,7 +253,7 @@ def _norm_earth(self,consts):
225253
node = vals.get("node_factor", vals.get("node", 1.0))
226254
freq = vals.get("angular_frequency", vals.get("frequency", 0.0))
227255
eqa = vals.get(
228-
"earth_equilibrium_argument",
256+
"equilibrium_argument",
229257
vals.get("eqa", vals.get("phase", 0.0)),
230258
)
231259
out.append(
@@ -234,7 +262,7 @@ def _norm_earth(self,consts):
234262
"amplitude": amp,
235263
"node_factor": node,
236264
"angular_frequency": freq,
237-
"earth_equilibrium_argument": eqa,
265+
"equilibrium_argument": eqa,
238266
}
239267
)
240268
continue
@@ -264,7 +292,7 @@ def _norm_earth(self,consts):
264292
"amplitude": amp,
265293
"node_factor": node,
266294
"angular_frequency": freq,
267-
"earth_equilibrium_argument": eqa,
295+
"equilibrium_argument": eqa,
268296
}
269297
)
270298
continue
@@ -295,14 +323,14 @@ def _norm_boundary(self,consts):
295323
freq = item.get("angular_frequency", item.get("frequency", 0.0))
296324
node = item.get("node_factor", item.get("node", 1.0))
297325
eqa = item.get(
298-
"earth_equilibrium_argument", item.get("eqa", item.get("phase", 0.0))
326+
"equilibrium_argument", item.get("eqa", item.get("phase", 0.0))
299327
)
300328
out.append(
301329
{
302330
"name": name,
303331
"angular_frequency": freq,
304332
"node_factor": node,
305-
"earth_equilibrium_argument": eqa,
333+
"equilibrium_argument": eqa,
306334
}
307335
)
308336
continue
@@ -317,14 +345,14 @@ def _norm_boundary(self,consts):
317345
freq = vals.get("angular_frequency", vals.get("frequency", 0.0))
318346
node = vals.get("node_factor", vals.get("node", 1.0))
319347
eqa = vals.get(
320-
"earth_equilibrium_argument", vals.get("eqa", vals.get("phase", 0.0))
348+
"equilibrium_argument", vals.get("eqa", vals.get("phase", 0.0))
321349
)
322350
out.append(
323351
{
324352
"name": name,
325353
"angular_frequency": freq,
326354
"node_factor": node,
327-
"earth_equilibrium_argument": eqa,
355+
"equilibrium_argument": eqa,
328356
}
329357
)
330358
continue
@@ -342,7 +370,7 @@ def _norm_boundary(self,consts):
342370
"name": name,
343371
"angular_frequency": freq,
344372
"node_factor": node,
345-
"earth_equilibrium_argument": eqa,
373+
"equilibrium_argument": eqa,
346374
}
347375
)
348376
continue
@@ -354,7 +382,7 @@ def _norm_boundary(self,consts):
354382
"name": name,
355383
"angular_frequency": vals,
356384
"node_factor": 1.0,
357-
"earth_equilibrium_argument": 0.0,
385+
"equilibrium_argument": 0.0,
358386
}
359387
)
360388
continue
@@ -369,7 +397,7 @@ def _norm_boundary(self,consts):
369397
"name": item,
370398
"angular_frequency": 0.0,
371399
"node_factor": 1.0,
372-
"earth_equilibrium_argument": 0.0,
400+
"equilibrium_argument": 0.0,
373401
}
374402
)
375403
continue
@@ -464,33 +492,33 @@ def write_bctides(self, bctides_file):
464492
# normalize tidal-constituent input formats so downstream code can
465493
# expect a list of dicts with explicit keys.
466494

467-
if self.earth_tides and "tidal_constituents" in self.earth_tides:
468-
self.earth_tides["tidal_constituents"] = self._norm_earth(
469-
self.earth_tides["tidal_constituents"]
495+
if self.earth_tides and "constituents" in self.earth_tides:
496+
self.earth_tides["constituents"] = self._norm_earth(
497+
self.earth_tides["constituents"]
470498
)
471-
if self.boundary_tides and "tidal_constituents" in self.boundary_tides:
472-
self.boundary_tides["tidal_constituents"] = self._norm_boundary(
473-
self.boundary_tides["tidal_constituents"]
499+
if self.boundary_tides and "constituents" in self.boundary_tides:
500+
self.boundary_tides["constituents"] = self._norm_boundary(
501+
self.boundary_tides["constituents"]
474502
)
475503
##out earth tidal potential if any
476504
if self.earth_tides:
477-
num_tidal = len(self.earth_tides["tidal_constituents"])
478-
cutoff_depth = self.earth_tides["tidal_cutoff_depth"]
505+
num_tidal = len(self.earth_tides["constituents"])
506+
cutoff_depth = self.earth_tides["cutoff_depth"]
479507
outf.write(str(num_tidal) + " " + str(cutoff_depth) + "\n")
480508
for i in range(num_tidal):
481509
outf.write(
482-
self.earth_tides["tidal_constituents"][i]["name"]
510+
self.earth_tides["constituents"][i]["name"]
483511
)
484512
outf.write("\n")
485-
amp = self.earth_tides["tidal_constituents"][i]["amplitude"]
486-
node_factor = self.earth_tides["tidal_constituents"][i][
513+
amp = self.earth_tides["constituents"][i]["amplitude"]
514+
node_factor = self.earth_tides["constituents"][i][
487515
"node_factor"
488516
]
489-
freq = self.earth_tides["tidal_constituents"][i][
517+
freq = self.earth_tides["constituents"][i][
490518
"angular_frequency"
491519
]
492-
eqa = self.earth_tides["tidal_constituents"][i][
493-
"earth_equilibrium_argument"
520+
eqa = self.earth_tides["constituents"][i][
521+
"equilibrium_argument"
494522
]
495523
outf.write(
496524
str(amp)
@@ -509,21 +537,21 @@ def write_bctides(self, bctides_file):
509537

510538
## out boundary forcing tidal
511539
if self.boundary_tides:
512-
num_tidal = len(self.boundary_tides["tidal_constituents"])
540+
num_tidal = len(self.boundary_tides["constituents"])
513541
outf.write(str(num_tidal) + "\n")
514542
for i in range(num_tidal):
515543
outf.write(
516-
self.boundary_tides["tidal_constituents"][i]["name"]
544+
self.boundary_tides["constituents"][i]["name"]
517545
)
518546
outf.write("\n")
519-
freq = self.boundary_tides["tidal_constituents"][i][
547+
freq = self.boundary_tides["constituents"][i][
520548
"angular_frequency"
521549
]
522-
node_factor = self.boundary_tides["tidal_constituents"][i][
550+
node_factor = self.boundary_tides["constituents"][i][
523551
"node_factor"
524552
]
525-
eqa = self.boundary_tides["tidal_constituents"][i][
526-
"earth_equilibrium_argument"
553+
eqa = self.boundary_tides["constituents"][i][
554+
"equilibrium_argument"
527555
]
528556
outf.write(str(freq) + " " + str(node_factor) + " " + str(eqa))
529557
outf.write("\n")

0 commit comments

Comments
 (0)