Skip to content

Commit

Permalink
Store true (#45)
Browse files Browse the repository at this point in the history
* use store_true in argument parser

* update tests for use of action=store_true in argparse

* change release date to tomorrow

* don't add package data
  • Loading branch information
Ronald van Haren authored and JaroCamphuijsen committed Jul 16, 2019
1 parent 321945d commit 4bb7775
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 170 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Release Notes
*************

1.0.0rc3 (2019-07-15)
1.0.0rc3 (2019-07-16)
~~~~~~~~~~~~~~~~~~~~~
* Third Release Candidate for the stable 1.0.0 era5cli release.
* Improve documentation `#21 <https://github.com/eWaterCycle/era5cli/issues/21>`_ `#29 <https://github.com/eWaterCycle/era5cli/issues/29>`_.
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ authors:
given-names: Rolf

cff-version: "1.0.3"
date-released: 2019-07-15
date-released: 2019-07-16
doi: 10.5281/zenodo.3252665
keywords:
- "ERA5"
Expand Down
32 changes: 0 additions & 32 deletions eWaterCycle/README.rst

This file was deleted.

23 changes: 0 additions & 23 deletions eWaterCycle/cartesius/era5_job_array.sh

This file was deleted.

5 changes: 0 additions & 5 deletions eWaterCycle/cartesius/readme_Cartesius_scripts.md

This file was deleted.

18 changes: 0 additions & 18 deletions eWaterCycle/variables.txt

This file was deleted.

42 changes: 15 additions & 27 deletions era5cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@
import era5cli.fetch as efetch


def _str2bool(string):
"""Return boolean based on input string."""
if isinstance(string, bool):
return string
if string.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif string.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')


def _build_parser():
"""Build the argument parser."""
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -98,10 +86,11 @@ def _build_parser():
)

common.add_argument(
"--split", type=_str2bool, default=True,
"--merge", action="store_true", default=False,
help=textwrap.dedent('''\
Split output by years, producing a separate file
for every year. Default is True.
Merge yearly output files.
Default is split output files into separate files
for every year.
''')
)
Expand All @@ -117,23 +106,23 @@ def _build_parser():
)

common.add_argument(
"--ensemble", type=_str2bool, default=False,
"--ensemble", action="store_true", default=False,
help=textwrap.dedent('''\
Whether to download high resolution realisation
(HRES) or a reduced resolution ten member ensemble
(EDA). "--ensemble True" downloads the reduced
resolution ensemble.
(EDA). Providing the "--ensemble" argument
downloads the reduced resolution ensemble.
''')
)

common.add_argument(
"--dryrun", type=_str2bool, default=False,
"--dryrun", action="store_true", default=False,
help=textwrap.dedent('''\
Whether to start downloading the request or just
print information on the chosen parameters and
output file names. "--dryrun True" will print
the information.
output file names. Providing the "--dryrun"
argument will print the information to stdout.
''')
)
Expand Down Expand Up @@ -195,12 +184,11 @@ def _build_parser():
formatter_class=argparse.RawTextHelpFormatter)

hourly.add_argument(
"--statistics", type=_str2bool, default=False,
"--statistics", action="store_true",
help=textwrap.dedent('''\
When downloading hourly ensemble data, set
"--statistics True" to download statistics
(ensemble mean and ensemble spread). Default is
False.
When downloading hourly ensemble data, provide
the "--statistics" argument to download statistics
(ensemble mean and ensemble spread).
''')
)
Expand Down Expand Up @@ -340,7 +328,7 @@ def _execute(args):
statistics=statistics,
pressurelevels=args.levels,
threads=args.threads,
split=args.split)
merge=args.merge)
era5.fetch(dryrun=args.dryrun)
return True

Expand Down
18 changes: 10 additions & 8 deletions era5cli/fetch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Fetch ERA5 variables."""

import os

import cdsapi
from pathos.threading import ThreadPool as Pool
import os

import era5cli.inputref as ref
import era5cli.utils
Expand Down Expand Up @@ -45,9 +46,10 @@ class Fetch:
(synoptic=False).
pressurelevels: None, list(int)
List of pressure levels to download 3D variables for.
split: bool
Split output files by year if `True`. If `False` all output
years will be in a single file.
merge: bool
Merge yearly output files (merge=True), or split
output files into separate files for every year
(merge=False).
threads: None, int
Number of parallel calls to cdsapi. If `None` no
parallel calls are done.
Expand All @@ -61,7 +63,7 @@ def __init__(self, years: list, months: list, days: list,
hours: list, variables: list, outputformat: str,
outputprefix: str, period: str, ensemble: bool,
statistics=None, synoptic=None, pressurelevels=None,
split=True, threads=None):
merge=False, threads=None):
"""Initialization of Fetch class."""
self.months = era5cli.utils._zpad_months(months)
"""list(str): List of zero-padded strings of months
Expand All @@ -88,8 +90,8 @@ def __init__(self, years: list, months: list, days: list,
"""str: Prefix of output filename."""
self.threads = threads
"""int: number of parallel threads to use for downloading."""
self.split = split
"""bool: Split output files by year if True."""
self.merge = merge
"""bool: Merge yearly output files if True."""
self.period = period
"""str: Frequency of output data (monthly or daily)."""
self.ensemble = ensemble
Expand Down Expand Up @@ -120,7 +122,7 @@ def fetch(self, dryrun=False):
# define extension output filename
self._extension()
# define fetch call depending on split argument
if self.split:
if not self.merge:
# split by variable and year
self._split_variable_yr()
else:
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
url="https://github.com/ewatercycle/era5cli",
packages=find_packages(),
include_package_data=True, # include everything in source control
package_data={'era5cli': ['cartesius/*']},
long_description=README,
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
43 changes: 18 additions & 25 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
"""Tests for era5cli utility functios."""

import era5cli.cli as cli
import era5cli.inputref as ref
import unittest.mock as mock
import pytest


def test_str2bool():
"""Test str2bool cli utility function."""
for f in ['No', 'False', 'F', 'N', '0']:
assert not cli._str2bool(f)
for t in ['Yes', 'True', 'T', 'Y', '1']:
assert cli._str2bool(t)
import era5cli.cli as cli
import era5cli.inputref as ref


def test_parse_args():
"""Test argument parser of cli."""
argv = ['hourly', '--startyear', '2008', '--ensemble', 'false',
'--variables', 'total_precipitation', '--statistics', 'true',
'--split', 'true', '--endyear', '2008', '--ensemble', 'true']
argv = ['hourly', '--startyear', '2008',
'--variables', 'total_precipitation', '--statistics',
'--endyear', '2008', '--ensemble']
args = cli._parse_args(argv)
assert args.command == 'hourly'
assert args.days == list(range(1, 32))
Expand All @@ -29,7 +22,7 @@ def test_parse_args():
assert args.levels == ref.PLEVELS
assert args.months == list(range(1, 13))
assert args.outputprefix == 'era5'
assert args.split
assert not args.merge
assert args.startyear == 2008
assert args.statistics
assert not args.threads
Expand All @@ -38,17 +31,17 @@ def test_parse_args():

def test_period_args():
"""Test the period specific argument setter with synoptic options."""
argv = ['monthly', '--startyear', '2008', '--ensemble', 'false',
argv = ['monthly', '--startyear', '2008',
'--variables', 'total_precipitation',
'--endyear', '2008', '--ensemble', 'true']
'--endyear', '2008', '--ensemble']
args = cli._parse_args(argv)
period_args = cli._set_period_args(args)
# Period_args consists of (synoptic, statistics, days, hours)
assert period_args == (None, None, None, [0])

argv = ['monthly', '--startyear', '2008', '--ensemble', 'false',
argv = ['monthly', '--startyear', '2008',
'--variables', 'total_precipitation',
'--synoptic', '4', '7', '--ensemble', 'true']
'--synoptic', '4', '7', '--ensemble']
args = cli._parse_args(argv)
period_args = cli._set_period_args(args)
# Period_args consists of (synoptic, statistics, days, hours)
Expand All @@ -64,24 +57,24 @@ def test_period_args():
@mock.patch("era5cli.fetch.Fetch", autospec=True)
def test_main_fetch(fetch):
"""Test if Fetch part of main completes without error."""
argv = ['hourly', '--startyear', '2008', '--ensemble', 'false',
'--variables', 'total_precipitation', '--statistics', 'true',
'--split', 'true', '--endyear', '2008', '--ensemble', 'true']
argv = ['hourly', '--startyear', '2008',
'--variables', 'total_precipitation', '--statistics',
'--endyear', '2008', '--ensemble']
args = cli._parse_args(argv)
assert cli._execute(args)

# should give an AssertionError if endyear is before startyear
argv = ['hourly', '--startyear', '2008', '--ensemble', 'false',
'--variables', 'total_precipitation', '--statistics', 'true',
'--split', 'true', '--endyear', '2007', '--ensemble', 'true']
argv = ['hourly', '--startyear', '2008',
'--variables', 'total_precipitation', '--statistics',
'--endyear', '2007', '--ensemble']
args = cli._parse_args(argv)
with pytest.raises(AssertionError):
assert cli._execute(args)

# monthly call without endyear
argv = ['monthly', '--startyear', '2008', '--ensemble', 'false',
argv = ['monthly', '--startyear', '2008',
'--variables', 'total_precipitation', '--synoptic',
'--split', 'true', '--ensemble', 'true']
'--ensemble']
args = cli._parse_args(argv)
cli._execute(args)

Expand Down
Loading

0 comments on commit 4bb7775

Please sign in to comment.