Skip to content

Commit

Permalink
Fixed custom output command not rendering correctly. Convert config a…
Browse files Browse the repository at this point in the history
…nd output paths to absolute paths. (#360)
  • Loading branch information
huard authored May 7, 2024
1 parent e60d704 commit 829a5ec
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ History
* Upgraded `owslib` to `>=0.29.1`. (PR #358)
* All operations that open NetCDF files or DAP links accept an `engine` argument. The default for all of these is `h5netcdf`. (PR #358)
* Added `pydap` as an alternate backend for opening DAP links. (PR #358)
* Fixed buggy CustomOutput command
* Make sure config and output paths are absolute

Internal changes
^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks/time_series_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The base flow index needs as input arguments the link to a NetCDF file storing the stream flow time series, the name of the stream flow variable, and the frequency at which the index is computed (`YS`: yearly, `QS-DEC`: seasonally)."
"The base flow index needs as input arguments a DataArray storing the stream flow time series, and the frequency at which the index is computed (`YS`: yearly, `QS-DEC`: seasonally)."
]
},
{
Expand Down
7 changes: 5 additions & 2 deletions ravenpy/config/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class EvaluationPeriod(LineCommand):
end: dt.date


class CustomOutput(FlatCommand):
class CustomOutput(LineCommand):
"""
Create custom output file to track a single variable, parameter or forcing function over time at a number of
basins, HRUs, or across the watershed.
Expand Down Expand Up @@ -796,7 +796,10 @@ def from_nc(
forcings.difference_update(data)

if len(data) == 0:
raise ValueError("No data found in netCDF files.")
raise ValueError(
"No data found in netCDF files. Check that variable names follow CF conventions, "
"or if not, provide `alt_names` mapping Raven data types to variable names."
)

# Default Gauge name
attrs["name"] = attrs.get("name", f"Gauge_{idx}")
Expand Down
4 changes: 3 additions & 1 deletion ravenpy/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def nc_specs(
else:
raise ValueError("NetCDF file not found.")

if isinstance(alt_names, str):
if alt_names is None:
alt_names = ()
elif isinstance(alt_names, str):
alt_names = (alt_names,)

attrs = {
Expand Down
4 changes: 2 additions & 2 deletions ravenpy/ravenpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ def run(
)

# Confirm configdir exists
configdir = Path(configdir)
configdir = Path(configdir).absolute()
if not configdir.exists():
raise OSError("Workdir should include configuration files.")

# Create outputdir
outputdir = Path(outputdir or "output")
if not outputdir.is_absolute():
outputdir = configdir / outputdir
outputdir = (configdir / outputdir).absolute()

if overwrite and outputdir.exists():
shutil.rmtree(str(outputdir))
Expand Down
2 changes: 1 addition & 1 deletion ravenpy/utilities/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def units_transform(source, target, context="hydro"):
context : str, optional
Context of unit conversion. Default: "hydro".
"""
from xclim.core.units import convert_units_to, units
from xclim.core.units import convert_units_to

b = convert_units_to(0 * source, target, context)
a = convert_units_to(1 * source, target, context) - b
Expand Down
18 changes: 17 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@

from ravenpy.config import commands as rc
from ravenpy.config import options as o
from ravenpy.config.base import RV
from ravenpy.config.base import RV, optfield

salmon_land_hru_1 = dict(
area=4250.6, elevation=843.0, latitude=54.4848, longitude=-123.3659, hru_type="land"
)


def test_custom_ouputs():
class Test(RV):
co: Sequence[rc.CustomOutput] = optfield(alias="CustomOutput")

co = rc.CustomOutput(
time_per="YEARLY",
stat="AVERAGE",
variable="PRECIP",
space_agg="ENTIRE_WATERSHED",
)
assert (
Test(co=[co]).to_rv().strip()
== ":CustomOutput YEARLY AVERAGE PRECIP ENTIRE_WATERSHED"
)


def test_evaluation_metrics():
class Test(RV):
em: Sequence[o.EvaluationMetrics] = Field(
Expand Down

0 comments on commit 829a5ec

Please sign in to comment.