Skip to content

Commit

Permalink
Implement dashed varname option (#148)
Browse files Browse the repository at this point in the history
* Implement dashed varname option

* Rename "--dashed_varname" to "--dashed-varname"
  • Loading branch information
BSchilperoort authored Apr 17, 2023
1 parent e6c85c4 commit 6c85d09
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions era5cli/args/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ def add_common_args(argument_parser: ArgumentParser) -> None:
),
)

argument_parser.add_argument(
"--dashed-varname",
action="store_true",
default=False,
help=textwrap.dedent(
"""
Whether to use dashed variable names in the output
files, or the (default )normal names.
Dashed names can allow for easier extraction
of the different facets from the filename.
For example:
'era5_temperature-of-snow-layer_1999_hourly.nc'
instead:
'era5_temperature_of_snow_layer_1999_hourly.nc'
"""
),
)


def construct_year_list(args):
"""Make a continous list of years from the startyear and endyear arguments."""
Expand Down
1 change: 1 addition & 0 deletions era5cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def _execute(input_args: argparse.Namespace) -> True:
prelimbe=input_args.prelimbe,
land=input_args.land,
overwrite=input_args.overwrite,
dashed_vars=input_args.dashed_varname,
)
era5.fetch(dryrun=input_args.dryrun)
return True
Expand Down
13 changes: 12 additions & 1 deletion era5cli/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class Fetch:
era5cli overwrite existing files. By default,
you will be prompted if a file already exists, with
the question if you want to overwrite it or not.
dashed_vars: bool
Whether to use dashed variable names in the output
files, or the normal names ('temperature-of-snow-layer'
instead of 'temperature_of_snow_layer').
"""

def __init__(
Expand All @@ -115,6 +119,7 @@ def __init__(
prelimbe=False,
land=False,
overwrite=False,
dashed_vars=False,
):
"""Initialization of Fetch class."""
self._get_login() # Get login info from config file.
Expand Down Expand Up @@ -175,6 +180,9 @@ def __init__(
dataset."""
self.overwrite = overwrite
"""bool: Whether to overwrite existing files."""
self.dashed_vars = dashed_vars
"""bool: Whether to use dashed variable names in the output
files, or the normal names."""

if self.merge and self.splitmonths:
raise ValueError(
Expand Down Expand Up @@ -262,7 +270,10 @@ def _define_outputfilename(self, var, years, month=None):

yearblock = f"{start}-{end}" if start != end else f"{start}"

fname = f"{prefix}_{var}_{yearblock}"
varname = var.replace("_", "-") if self.dashed_vars else var

fname = f"{prefix}_{varname}_{yearblock}"

if month is not None:
fname += f"-{month}"
fname += f"_{self.period}"
Expand Down

0 comments on commit 6c85d09

Please sign in to comment.