From bc8002d66acaaed8fe0c2a3bd14c3f2ef912b278 Mon Sep 17 00:00:00 2001 From: Jaime R Calzada Date: Tue, 30 Mar 2021 15:42:00 -0400 Subject: [PATCH] tested write param with template --- pyschism/cmd/forecast/forecast.py | 1 + pyschism/cmd/forecast/init.py | 3 ++- pyschism/driver/driver.py | 4 +++- pyschism/param/core.py | 14 ++++++++++++++ pyschism/param/opt.py | 17 +++++++++++++++++ pyschism/param/param.nml.template | 2 +- pyschism/param/param.py | 6 +++--- pyschism/param/schout.py | 26 ++++++++++++++++++++++++++ 8 files changed, 67 insertions(+), 6 deletions(-) diff --git a/pyschism/cmd/forecast/forecast.py b/pyschism/cmd/forecast/forecast.py index 5dac030c..3255ea95 100644 --- a/pyschism/cmd/forecast/forecast.py +++ b/pyschism/cmd/forecast/forecast.py @@ -56,6 +56,7 @@ def add_forecast_init(actions): "--skip-run", action="store_true", help="Skips running the model.") init.add_argument('--nproc', type=int, default=cpu_count(logical=False)) + init.add_argument('--use-param-template', action='store_true') _add_tidal_constituents(init) _add_atmospheric_forcing(init) _add_hydrologic_forcing(init) diff --git a/pyschism/cmd/forecast/init.py b/pyschism/cmd/forecast/init.py index 9b7f5338..869e1869 100644 --- a/pyschism/cmd/forecast/init.py +++ b/pyschism/cmd/forecast/init.py @@ -249,7 +249,8 @@ def __init__(self, args: Namespace): vgrid=False, fgrid=False, wind_rot=False, - overwrite=self.args.overwrite + overwrite=self.args.overwrite, + use_param_template=self.args.use_param_template ) if self.args.skip_run is False: diff --git a/pyschism/driver/driver.py b/pyschism/driver/driver.py index c301e49a..1a8f6370 100644 --- a/pyschism/driver/driver.py +++ b/pyschism/driver/driver.py @@ -103,6 +103,7 @@ def write( nws=True, wind_rot=True, stations=True, + use_param_template=False, ): """Writes to disk the full set of input files necessary to run SCHISM. """ @@ -132,7 +133,8 @@ def write( self.model_domain.fgrid.write(outdir / fgrid, overwrite) if param: param = 'param.nml' if param is True else param - self.param.write(outdir / param, overwrite) + self.param.write(outdir / param, overwrite, + use_template=use_param_template) if bctides: bctides = 'bctides.in' if bctides is True else bctides self.bctides.write(outdir / bctides, overwrite) diff --git a/pyschism/param/core.py b/pyschism/param/core.py index 47b1474d..f7d3e0bd 100644 --- a/pyschism/param/core.py +++ b/pyschism/param/core.py @@ -207,3 +207,17 @@ def __str__(self): data.append(f" {key}={str(current)}") data = '\n'.join(data) return f"&CORE\n{data}\n/" + + def to_dict(self): + output = {} + for key, default in PARAM_DEFAULTS.items(): + current = getattr(self, key) + if key == 'rnday': + current = current.total_seconds() / (60.*60.*24) + if key == 'dt': + current = current.total_seconds() + if key in self._mandatory: + output[key] = current + elif default != current: + output[key] = current + return output diff --git a/pyschism/param/opt.py b/pyschism/param/opt.py index 972d565a..7bc710df 100644 --- a/pyschism/param/opt.py +++ b/pyschism/param/opt.py @@ -221,3 +221,20 @@ def __str__(self): current.append(f' {current}({i+1}) = 1') data = '\n'.join(data) return f"&OPT\n{data}\n/" + + def to_dict(self): + data = {} + for key, default in PARAM_DEFAULTS.items(): + current = getattr(self, key) + if key in ['dramp', 'drampbc']: + if current is not None: + current = current.total_seconds() / (60*60*24) + if current is not None: + if not isinstance(current, list): + data[key] = current + else: + data[key] = len(current) * [0] + for i, state in enumerate(current): + if state: + data[key][i] = 1 + return data diff --git a/pyschism/param/param.nml.template b/pyschism/param/param.nml.template index a272c203..3b57d643 100644 --- a/pyschism/param/param.nml.template +++ b/pyschism/param/param.nml.template @@ -590,7 +590,7 @@ ! If USE_MARSH is on and isav=1, all .gr3 must have constant depths! !---------------------------------------------------------------------- isav = 0 !on/off flag - sav_cd = 1.13 !only needed if isav=1. Drag coefficient +! sav_cd = 1.13 !only needed if isav=1. Drag coefficient !---------------------------------------------------------------------- ! Coupling step with ICE module. diff --git a/pyschism/param/param.py b/pyschism/param/param.py index 5d174f44..e8db0523 100644 --- a/pyschism/param/param.py +++ b/pyschism/param/param.py @@ -57,9 +57,9 @@ def write(self, path, overwrite=False, use_template=False): def to_dict(self): return { - 'CORE': self.core.__dict__, - 'OPT': self.opt.__dict__, - 'SCHOUT': self.schout.__dict__ + 'CORE': self.core.to_dict(), + 'OPT': self.opt.to_dict(), + 'SCHOUT': self.schout.to_dict() } @property diff --git a/pyschism/param/schout.py b/pyschism/param/schout.py index a8b2d1f1..6b8bd3db 100644 --- a/pyschism/param/schout.py +++ b/pyschism/param/schout.py @@ -181,3 +181,29 @@ def __str__(self): schout.append(f' {var[1:]}({i+1})={state}') schout.append('/') return '\n'.join(schout) + + def to_dict(self): + data = {} + if self.nhot_write is not None: + data['nhot'] = self._nhot + data['nhot_write'] = self.nhot_write + if self.nspool_sta is not None: + nspool_sta = self.nspool_sta + if isinstance(nspool_sta, timedelta): + nspool_sta = int(round(nspool_sta.total_seconds() / self._dt)) + if isinstance(nspool_sta, float): + nspool_sta = int( + round(timedelta(hours=nspool_sta) / self._dt)) + if isinstance(nspool_sta, (int, float)): + if nspool_sta <= 0: + raise ValueError("nspool_sta must be positive.") + data['iout_sta'] = self._iout_sta + data['nspool_sta'] = nspool_sta + for var in dir(self): + if var.startswith('_iof'): + _var = var[1:] + data[_var] = len(getattr(self, var)) * [0] + for i, state in enumerate(getattr(self, var)): + if state == 1: + data[_var][i] = state + return data