Skip to content

Commit b95594e

Browse files
eli@water.ca.goveli@water.ca.gov
authored andcommitted
Fixed bug in iteration labeling
1 parent c4782d1 commit b95594e

2 files changed

Lines changed: 50 additions & 96 deletions

File tree

schimpy/hotstart_inventory.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def hotstart_inventory(
103103

104104
hots = glob.glob(os.path.join(workdir, "hotstart_000000_*.nc"))
105105
is_existing = len(hots) > 0
106-
print(workdir)
106+
107107
param_needed = (
108108
(dt is None)
109109
or (run_start is None)
@@ -118,7 +118,6 @@ def hotstart_inventory(
118118
if param_needed:
119119
if paramfile is None or paramfile == "":
120120
paramfile = "param.nml"
121-
print("got here", paramfile, workdir)
122121
if os.path.exists(os.path.join(workdir, "..", paramfile)):
123122
paramfile = os.path.join(workdir, "..", paramfile)
124123
params = read_params(paramfile)
@@ -185,8 +184,9 @@ def hotstart_inventory_exist(start, dt=90, workdir=".", do_print=True):
185184
if len(hots) == 0:
186185
hots = glob.glob(os.path.join("hotstart_0000_*.nc"))
187186
hots.sort()
188-
iters = [int(x.split("_")[2].replace(".nc", "")) for x in hots]
189-
187+
iters = [
188+
int(os.path.splitext(os.path.basename(x))[0].split("_")[2]) for x in hots
189+
]
190190
iters.sort()
191191
times = [start + seconds(x * dt) for x in iters]
192192
df = pd.DataFrame(index=times, data=iters)
@@ -196,7 +196,6 @@ def hotstart_inventory_exist(start, dt=90, workdir=".", do_print=True):
196196
if do_print:
197197
for it, t in zip(iters, times):
198198
print("{}: {}".format(it, t))
199-
200199
return df
201200

202201

schimpy/param.py

Lines changed: 46 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ParamAlias:
2121
Name of the underlying SCHISM parameter or Params property.
2222
kind : {"raw", "property"}
2323
"raw" -> use Params.__getitem__/__setitem__ with SCHISM name.
24-
"property" -> use getattr/setattr on Params (e.g. run_nday, nc_out_interval).
24+
"property" -> use getattr/setattr on Params (e.g. run_nday, nc_out_freq).
2525
description : str
2626
Human-readable explanation for help/--help text.
2727
value_hint : str
@@ -55,15 +55,15 @@ class ParamAlias:
5555
value_hint="0, 1, or 2",
5656
),
5757

58-
# --- Global NetCDF output interval ---------------------------------------
58+
# --- Global NetCDF output freq ---------------------------------------
5959

6060
# --- Time span per NetCDF file -------------------------------------------
6161
"nc_out_file_span": ParamAlias(
6262
target="nc_stack",
6363
kind="property",
6464
description=(
6565
"Wall-clock time covered by each history file. Must be "
66-
"a multiple of nc_out_interval. Used to derive ihfskip, "
66+
"a multiple of nc_out_freq. Used to derive ihfskip, "
6767
"the number of (dt) time steps in the file, which is the parameter "
6868
" actually used by SCHISM. nc_out_steps_per_file is an alias "
6969
" that more directly expresses the ihfskip parameter."
@@ -82,8 +82,8 @@ class ParamAlias:
8282
),
8383

8484
# --- Hotstart writing frequency ------------------------------------------
85-
"hotstart_interval": ParamAlias(
86-
target="hotstart_interval",
85+
"hotstart_freq": ParamAlias(
86+
target="hotstart_freq",
8787
kind="property",
8888
description=(
8989
"Wall-clock interval between writing hotstart.nc files. Implemented "
@@ -94,8 +94,8 @@ class ParamAlias:
9494
),
9595

9696
# --- Station outputs: wall-clock interval --------------------------------
97-
"station_interval": ParamAlias(
98-
target="station_interval",
97+
"station_freq": ParamAlias(
98+
target="station_freq",
9999
kind="property",
100100
description=(
101101
"Wall-clock interval between samples written to station output files "
@@ -105,7 +105,7 @@ class ParamAlias:
105105
),
106106

107107
# --- Station outputs: step interval (low-level) --------------------------
108-
"station_intervel_in_model_steps": ParamAlias(
108+
"station_freq_in_model_steps": ParamAlias(
109109
target="nspool_sta",
110110
kind="raw",
111111
description=(
@@ -148,13 +148,13 @@ def _parse_alias_value(alias: ParamAlias, raw: str) -> Any:
148148
s = str(raw).strip()
149149

150150
# Allow disabling for some interval-like aliases
151-
if alias.target in ("hotstart_freq", "station_out_interval"):
151+
if alias.target in ("hotstart_freq", "station_out_freq"):
152152
if s.lower() in ("none", "off", "0", "disable", "disabled"):
153153
return None
154154
return _normalize_freq_string(s)
155155

156156
# Core global output controls (intervals and spans)
157-
if alias.target in ("nc_out_interval", "nc_stack"):
157+
if alias.target in ("nc_out_freq", "nc_stack"):
158158
return _normalize_freq_string(s)
159159

160160
if alias.target in ("run_nday",):
@@ -234,13 +234,13 @@ def set_by_name_or_alias(self, name: str, value: Any) -> None:
234234
Set a parameter value using either:
235235
236236
- The original SCHISM name (e.g. 'ihot', 'rnday', 'ihfskip'), or
237-
- A friendly alias from PARAM_ALIASES (e.g. 'run_nday', 'nc_out_interval').
237+
- A friendly alias from PARAM_ALIASES (e.g. 'run_nday', 'nc_out_freq').
238238
239239
Examples
240240
--------
241241
>>> p.set_by_name_or_alias("ihot", 0)
242242
>>> p.set_by_name_or_alias("run_nday", 365)
243-
>>> p.set_by_name_or_alias("nc_out_interval", "1h")
243+
>>> p.set_by_name_or_alias("nc_out_freq", "1h")
244244
"""
245245
alias = PARAM_ALIASES.get(name)
246246

@@ -260,7 +260,7 @@ def set_by_name_or_alias(self, name: str, value: Any) -> None:
260260
return
261261

262262
if alias.kind == "property":
263-
# property on Params, e.g. run_nday, nc_out_interval, station_out_interval
263+
# property on Params, e.g. run_nday, nc_out_freq, station_out_freq
264264
parsed = _parse_alias_value(alias, value)
265265
setattr(self, alias.target, parsed)
266266
return
@@ -315,58 +315,58 @@ def get_run_start(self):
315315
return pd.Timestamp(y, m, d, h)
316316

317317

318-
def get_interval(self, name):
318+
def get_freq(self, name):
319319
dt = self["dt"]
320320
sec = self[name] * dt
321321
freq = pd.Timedelta(sec, unit="s")
322322

323323
return pd.tseries.frequencies.to_offset(freq)
324324

325325
# 1) Hotstart
326-
def get_hotstart_interval(self):
327-
return self.get_interval("nhot_write")
326+
def get_hotstart_freq(self):
327+
return self.get_freq("nhot_write")
328328

329-
def set_hotstart_interval(self, freq):
329+
def set_hotstart_freq(self, freq):
330330
if freq is None:
331331
self["nhot"] = 0
332332
else:
333333
self["nhot"] = 1
334-
self.set_interval("nhot_write", freq)
334+
self.set_freq("nhot_write", freq)
335335

336-
hotstart_interval = property(get_hotstart_interval, set_hotstart_interval)
336+
hotstart_freq = property(get_hotstart_freq, set_hotstart_freq)
337337

338-
# 2) NetCDF history output *interval* (between writes)
339-
def get_nc_out_interval(self):
338+
# 2) NetCDF history output *freq* (between writes)
339+
def get_nc_out_freq(self):
340340
# should use nspool * dt
341-
return self.get_interval("nspool")
341+
return self.get_freq("nspool")
342342

343-
def set_nc_out_interval(self, freq):
344-
self.set_interval("nspool", freq)
343+
def set_nc_out_freq(self, freq):
344+
self.set_freq("nspool", freq)
345345

346-
nc_out_interval = property(get_nc_out_interval, set_nc_out_interval)
346+
nc_out_freq = property(get_nc_out_freq, set_nc_out_freq)
347347

348348
# 3) NetCDF history file *span* (time per file)
349349
def get_nc_out_file_span(self):
350350
# ihfskip * dt
351-
return self.get_interval("ihfskip")
351+
return self.get_freq("ihfskip")
352352

353353
def set_nc_out_file_span(self, freq):
354-
self.set_interval("ihfskip", freq)
354+
self.set_freq("ihfskip", freq)
355355

356356
nc_out_file_span = property(get_nc_out_file_span, set_nc_out_file_span)
357357

358-
# 4) Station output interval
359-
def get_station_interval(self):
360-
return self.get_interval("nspool_sta")
358+
# 4) Station output freq
359+
def get_station_freq(self):
360+
return self.get_freq("nspool_sta")
361361

362-
def set_station_interval(self, freq):
362+
def set_station_freq(self, freq):
363363
if freq is None:
364364
self["iout_sta"] = 0
365365
else:
366-
self.set_interval("nspool_sta", freq)
366+
self.set_freq("nspool_sta", freq)
367367
self["iout_sta"] = 1
368368

369-
station_interval = property(get_station_interval, set_station_interval)
369+
station_freq = property(get_station_freq, set_station_freq)
370370

371371

372372
def get_mode(self):
@@ -390,7 +390,7 @@ def set_run_start(self, run_start):
390390
run_start = property(get_run_start, set_run_start)
391391

392392

393-
# NEW: getter/setter for total run length (days), stored in CORE::rnday
393+
# getter/setter for total run length (days), stored in CORE::rnday
394394
def get_run_nday(self):
395395
"""
396396
Return total run length (in days) as an int, based on CORE::rnday.
@@ -408,7 +408,7 @@ def set_run_nday(self, nday):
408408
run_nday = property(get_run_nday, set_run_nday)
409409

410410

411-
def set_interval(self, name, freq):
411+
def set_freq(self, name, freq):
412412
"""Set binary output frequency using Pandas offset or string that evaluates as offset"""
413413
dt = int(self["dt"])
414414
if type(freq) in (str, pd.Timedelta):
@@ -419,7 +419,7 @@ def set_interval(self, name, freq):
419419
dt = pd.tseries.frequencies.to_offset(f"{dt}s")
420420
nspool = freq / dt
421421
if abs(nspool - round(nspool)) > 0.01:
422-
raise ValueError("Output interval not divisible by dt")
422+
raise ValueError("Output freq not divisible by dt")
423423
else:
424424
nspool = round(nspool)
425425
else:
@@ -429,27 +429,27 @@ def set_interval(self, name, freq):
429429
)
430430
self[name] = int(nspool)
431431

432-
def get_station_out_interval(self):
433-
return self.get_interval("nspool_sta")
432+
def get_station_out_freq(self):
433+
return self.get_freq("nspool_sta")
434434

435-
def set_station_out_interval(self, freq):
435+
def set_station_out_freq(self, freq):
436436
"""Set station output frequency
437437
438438
Parameters
439439
----------
440440
freq : offset or string
441441
442-
Sets output interval for staout files and ensures that output is enabled.
442+
Sets output freq for staout files and ensures that output is enabled.
443443
If None, frequency will be set using default (or 1 Hour) and station output disabled
444444
445445
"""
446446
if freq is None:
447447
self["iout_sta"] = 0
448448
else:
449-
self.set_interval("nspool_sta", freq)
449+
self.set_freq("nspool_sta", freq)
450450
self["iout_sta"] = 1
451451

452-
station_out_interval = property(get_station_out_interval, set_station_out_interval)
452+
station_out_freq = property(get_station_out_freq, set_station_out_freq)
453453

454454
def sections(self, defaults=False):
455455
sections = self._namelist.keys()
@@ -648,51 +648,6 @@ def read_params(fname, default=None):
648648
return p
649649

650650

651-
def test_param():
652-
test_param_file = "C:/Delta/BayDeltaSCHISM/templates/bay_delta/param.nml.clinic"
653-
parms = read_params(test_param_file)
654-
print(parms)
655-
print(parms["rnday"])
656-
parms["rnday"] = 3000
657-
print(parms["rnday"])
658-
print(parms.run_start)
659-
parms.run_start = "2010-02-04"
660-
print(parms.run_start)
661-
print("Stack")
662-
print(parms.nc_stack)
663-
parms.nc_stack = "8h"
664-
print(parms.nc_stack)
665-
print("IHFSKIP")
666-
print(parms["ihfskip"])
667-
print(parms.nc_out_interval)
668-
print(parms.station_out_interval)
669-
parms.station_out = None
670-
print(parms.station_out_interval)
671-
print(parms["iout_sta"])
672-
parms.station_out_interval = "15min"
673-
print(parms.station_out_interval)
674-
print(parms["iout_sta"])
675-
676-
print(parms.hotstart_freq)
677-
print(parms["nhot"])
678-
parms.hotstart_freq = "10D"
679-
print(parms["nhot"])
680-
print(parms.hotstart_freq)
681-
parms.hotstart_freq = None
682-
print(parms["nhot"])
683-
parms.hotstart_freq = "5D"
684-
print(parms["nhot"])
685-
686-
print(parms.sections())
687-
688-
other_param_file = "C:/Delta/BayDeltaSCHISM/templates/bay_delta/param.nml.tropic"
689-
otherparms = read_params(other_param_file)
690-
df = parms.diff(otherparms)
691-
692-
parms.nc_stack = pd.tseries.frequencies.to_offset("1D")
693-
parms.validate()
694-
parms.write("./junk.nml")
695-
696651
@click.command(
697652
name="set_param",
698653
context_settings={"help_option_names": ["-h", "--help"]},
@@ -732,13 +687,13 @@ def main(param_file: Path, pairs: tuple[str, ...], output: Path | None, dry_run:
732687
733688
* the original SCHISM name (ihot, rnday, ihfskip, nspool_sta, ...)
734689
* an alias defined in PARAM_ALIASES
735-
(run_nday, nc_out_interval, station_out_interval, hotstart_freq, ...)
690+
(run_nday, nc_out_freq, station_out_freq, hotstart_freq, ...)
736691
737-
VALUE is a scalar or interval:
692+
VALUE is a scalar or freq:
738693
739694
* integers / floats: 0, 252, 90.0
740-
* time intervals: 15min, 1H, 3D (pandas-style offsets)
741-
* 'none' for aliases like hotstart_freq / station_out_interval to disable them
695+
* time freqs: 15min, 1H, 3D (pandas-style offsets)
696+
* 'none' for aliases like hotstart_freq / station_out_freq to disable them
742697
"""
743698
if not pairs:
744699
raise click.ClickException(

0 commit comments

Comments
 (0)