Skip to content

Commit

Permalink
ENH: Adding date parser to get_armfiles. (#449)
Browse files Browse the repository at this point in the history
* ENH: Adding date parser to get_armfiles.

* FIX: Forgot datetime import.

* ENH: Adding output format and return datetime obj parameters.
Also moving function to datetimeutils.

* TST: Adding unittests for date_parser.

* FIX: Forgot full import.
  • Loading branch information
zssherman authored Apr 21, 2022
1 parent b8439fe commit 5b7ec2d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
10 changes: 8 additions & 2 deletions act/discovery/get_armfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
except ImportError:
from urllib import urlopen

from act.utils import date_parser


def download_data(username, token, datastream, startdate, enddate, time=None, output=None):
"""
Expand All @@ -28,9 +30,11 @@ def download_data(username, token, datastream, startdate, enddate, time=None, ou
datastream : str
The name of the datastream to acquire.
startdate : str
The start date of the data to acquire. Format is YYYY-MM-DD.
The start date of the data to acquire. Formats accepted are
YYYY-MM-DD, DD.MM.YYYY, DD/MM/YYYY, YYYYMMDD or YYYY/MM/DD.
enddate : str
The end date of the data to acquire. Format is YYYY-MM-DD.
The end date of the data to acquire. Formats accepted are
YYYY-MM-DD, DD.MM.YYYY, DD/MM/YYYY, YYYYMMDD or YYYY/MM/DD.
time: str or None
The specific time. Format is HHMMSS. Set to None to download all files
in the given date interval.
Expand Down Expand Up @@ -88,8 +92,10 @@ def download_data(username, token, datastream, startdate, enddate, time=None, ou
# start and end strings for query_url are constructed
# if the arguments were provided
if startdate:
start = date_parser(startdate, output_format='%Y-%m-%d')
start = f'&start={startdate}'
if enddate:
end = date_parser(enddate, output_format='%Y-%m-%d')
end = f'&end={enddate}'
# build the url to query the web service using the arguments provided
query_url = (
Expand Down
16 changes: 16 additions & 0 deletions act/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,19 @@ def test_height_adjusted_pressure():
pressure=obj[press_var_name].values,
press_var_units=None,
)


def test_date_parser():
datestring = '20111001'
output_format = '%Y/%m/%d'

test_string = act.utils.date_parser(
datestring, output_format, return_datetime=False)
assert test_string == '2011/10/01'

test_datetime = act.utils.date_parser(
datestring, output_format, return_datetime=True)
assert test_datetime == datetime(2011, 10, 1)

pytest.raises(
ValueError, act.utils.date_parser, '0511')
1 change: 1 addition & 0 deletions act/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
determine_time_delta,
numpy_to_arm_date,
reduce_time_ranges,
date_parser,
)
from .geo_utils import add_solar_variable, destination_azimuth_distance
from .inst_utils import decode_present_weather
Expand Down
55 changes: 49 additions & 6 deletions act/utils/datetime_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def reduce_time_ranges(time, time_delta=60, broken_barh=False):

def determine_time_delta(time, default=60):
"""
Returns the most likely time step in seconds by analyzing the difference in
time steps.
Returns the most likely time step in seconds by analyzing the difference
in time steps.
Parameters
----------
Expand Down Expand Up @@ -140,17 +140,19 @@ def determine_time_delta(time, default=60):

def datetime64_to_datetime(time):
"""
Given a numpy datetime64 array time series, return datetime (y, m, d, h, m, s)
Given a numpy datetime64 array time series, return datetime
(y, m, d, h, m, s)
Parameters
----------
time: numpy datetime64 array, list of numpy datetime64 values or scalar numpy datetime64
The numpy array of date time values.
time : numpy datetime64 array, list of numpy datetime64 values or
scalar numpy datetime64. The numpy array of date time values.
Returns
-------
datetime: list
datetime : list
Returns a list of datetimes (y, m, d, h, m, s) from a time series.
YYYY-MM-DD, DD.MM.YYYY, DD/MM/YYYY or YYYYMMDD.
"""
if isinstance(time, (tuple, list)):
Expand All @@ -164,3 +166,44 @@ def datetime64_to_datetime(time):
for tm in time
]
return datetime_array


def date_parser(date_string, output_format='%Y%m%d',
return_datetime=False):
""" Converts one datetime string to another or to
a datetime object.
Parameters
----------
date_string : str
datetime string to be parsed. Accepted formats are
YYYY-MM-DD, DD.MM.YYYY, DD/MM/YYYY or YYYYMMDD.
output_format : str
Format for datetime.strftime to output datetime string.
return_datetime : bool
If true, returns str as a datetime object.
Default is False.
returns
-------
datetime_str : str
A valid datetime string.
datetime_obj : datetime.datetime
A datetime object.
"""
date_fmts = ['%Y-%m-%d', '%d.%m.%Y',
'%d/%m/%Y', '%Y%m%d', '%Y/%m/%d']
for fmt in date_fmts:
try:
datetime_obj = dt.datetime.strptime(date_string, fmt)
if return_datetime:
return datetime_obj
else:
return datetime_obj.strftime(output_format)
except ValueError:
pass
fmt_strings = ', '.join(date_fmts)
raise ValueError(
'Invalid Date format, please use one of these formats '
+ fmt_strings)

0 comments on commit 5b7ec2d

Please sign in to comment.