formatters.pywatershed¶
pywatershed
¶
Format derived pywatershed parameters into model-ready output files.
Convert the xarray Dataset of derived PRMS parameters into the file formats expected by pywatershed v2.0. This is the output formatter plugin for the pywatershed model -- it implements the second half of the two-phase separation (pipeline produces raw SIR, plugin formats model-specific output).
Output components
- Parameter NetCDF -- static parameters loadable by
pws.Parameters.from_netcdf(). CF-1.8 compliant. - Forcing NetCDF files -- one file per climate variable
(
prcp.nc,tmax.nc,tmin.nc) with PRMS internal units (inches/day, degrees F). pywatershed'sPRMSAtmosphereacceptsUnion[str, Path, ndarray, Adapter]for these inputs. - Soltab NetCDF -- potential solar radiation tables with shape
(nhru, 366)in Langleys (cal/cm2/day). - Control YAML -- simulation time period configuration.
Unit conventions ~~~~~~~~~~~~~~~~ PRMS internal units: feet, inches, degrees F, acres, Langleys. Forcing conversions performed here: mm to inches (prcp), C to F (tmax, tmin).
See Also
docs/reference/pywatershed_parameterization_guide.md : Section 2C. hydro_param.derivations.pywatershed : Derivation plugin that produces the input Dataset for this formatter. hydro_param.units.convert : Unit conversion utility.
PywatershedFormatter
¶
Format derived parameters for pywatershed consumption.
Produce parameter NetCDF, forcing NetCDF files (one variable per
file), soltab arrays, and control configuration compatible with
pywatershed v2.0. This class implements the FormatterProtocol
defined in hydro_param.plugins.
The formatter validates parameters against bundled metadata before writing, logging warnings for missing required parameters or out-of-range values.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Formatter identifier (
TYPE:
|
Notes
Parameter metadata (valid ranges, required flags) is loaded from
the bundled data/pywatershed/parameter_metadata.yml file and
cached for the lifetime of the formatter instance.
Initialize the formatter with an empty metadata cache.
Source code in src/hydro_param/formatters/pywatershed.py
write
¶
Write all pywatershed output files to a directory.
Orchestrate the four output components: static parameters, climate forcing, soltab, and control. Validates the dataset before writing and logs any validation warnings.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Derived pywatershed parameter dataset containing static
parameters, optional forcing time series (
TYPE:
|
output_path
|
Root output directory. Created if it does not exist.
TYPE:
|
config
|
Formatter configuration dict with keys:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Path]
|
Absolute paths to all files written. |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If output directory creation or file writing fails. |
ValueError
|
If |
Source code in src/hydro_param/formatters/pywatershed.py
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 | |
write_parameters
¶
Write static parameter NetCDF for pws.Parameters.from_netcdf().
Extract static parameters (excluding forcing time series and soltab variables) and write them to a CF-1.8 compliant NetCDF file with pywatershed-compatible dimension names.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Derived parameter dataset. Forcing variables (
TYPE:
|
output_path
|
Path for the output NetCDF file (e.g.,
TYPE:
|
Notes
If no static parameters remain after exclusion, a warning is logged and no file is written.
Source code in src/hydro_param/formatters/pywatershed.py
write_forcing_netcdf
¶
Write forcing NetCDF files (one variable per file) in PRMS units.
Produce one NetCDF file per forcing variable with unit conversions applied:
prcp.nc-- precipitation (mm to inches/day)tmax.nc-- maximum temperature (C to F)tmin.nc-- minimum temperature (C to F)swrad.nc-- shortwave radiation (Langleys/day, no conversion)potet.nc-- potential ET (inches, no conversion)
Variables not present in the input dataset are silently skipped.
pywatershed's PRMSAtmosphere accepts file paths directly
for these inputs.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Dataset potentially containing forcing variables (
TYPE:
|
output_dir
|
Directory for forcing output files. Created if it does not exist.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Path]
|
Absolute paths to forcing files written. Empty list if no forcing variables are present. |
Notes
Unit conversions use hydro_param.units.convert() with
float64 precision to avoid truncation of precipitation values.
Source code in src/hydro_param/formatters/pywatershed.py
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 | |
write_soltab
¶
Write potential solar radiation tables to NetCDF.
Extract soltab variables from the derived dataset and write them to a standalone NetCDF file. These tables provide 365+1 days of potential clear-sky radiation for PRMS's solar geometry calculations.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Dataset containing
TYPE:
|
output_path
|
Path for the output NetCDF file (e.g.,
TYPE:
|
Notes
If no soltab variables are present, a warning is logged and no file is written.
Source code in src/hydro_param/formatters/pywatershed.py
write_control
¶
Write pywatershed control YAML with simulation time bounds.
Produce a minimal control file specifying the simulation start time, end time, and daily timestep. This file is consumed by pywatershed's control infrastructure.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Configuration dict with
TYPE:
|
output_path
|
Path for the control YAML file (e.g.,
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/hydro_param/formatters/pywatershed.py
validate
¶
Validate parameters against pywatershed metadata constraints.
Check that all parameters marked as required in the bundled
parameter_metadata.yml are present, and that values for
parameters with defined valid_range fall within bounds.
Non-finite values (NaN, inf) are excluded from range checks.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Derived parameter dataset to validate.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
Human-readable validation warning messages. Empty list if all checks pass. Returns a single-element list with a skip message if metadata cannot be loaded. |
Notes
This method does not raise exceptions for invalid data -- it collects all issues as warning strings so the caller can decide whether to proceed or abort.