Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snotel #2

Merged
merged 22 commits into from
Jul 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ See https://github.com/heigeo/climata to report any issues.
Available Services
------------------

=================== ======================================================= ==========
Module Classes Data Source
=================== ======================================================= ==========
climata.acis_ ``StationMetaIO``, ``StationDataIO`` ACIS_
climata.cocorahs_ ``CocorahsIO`` CoCoRaHS_
climata.hydromet_ ``DailyDataIO``, ``InstantDataIO``, ``AgrimetRecentIO`` Hydromet_
climata.usgs_ ``SiteIO``, ``DailyValueIO``, ``InstantValueIO`` `USGS (NWIS)`_
=================== ======================================================= ==========
=================== ========================================================== ==========
Module Classes Data Source
=================== ========================================================== ==========
climata.acis_ ``StationMetaIO``, ``StationDataIO`` ACIS_
climata.cocorahs_ ``CocorahsIO`` CoCoRaHS_
climata.hydromet_ ``DailyDataIO``, ``InstantDataIO``, ``AgrimetRecentIO`` Hydromet_
climata.snotel_ ``StationIO``, ``StationDailyDataIO``, ``RegionDailyData`` `NRCS (SNOTEL AWDB)`_
climata.usgs_ ``SiteIO``, ``DailyValueIO``, ``InstantValueIO`` `USGS (NWIS)`_
=================== ========================================================== ==========

Usage
-----
Expand Down Expand Up @@ -65,8 +66,10 @@ Python API:
.. _ACIS: http://data.rcc-acis.org/
.. _CoCoRaHS: http://data.cocorahs.org/cocorahs/export/exportmanager.aspx
.. _Hydromet: http://www.usbr.gov/pn/hydromet/arcread.html
.. _NRCS (SNOTEL AWDB): http://www.wcc.nrcs.usda.gov/web_service/awdb_web_service_landing.htm
.. _USGS (NWIS): http://waterdata.usgs.gov/nwis
.. _climata.acis: https://github.com/heigeo/climata/blob/master/climata/acis/__init__.py
.. _climata.cocorahs: https://github.com/heigeo/climata/blob/master/climata/cocorahs/__init__.py
.. _climata.hydromet: https://github.com/heigeo/climata/blob/master/climata/hydromet/__init__.py
.. _climata.snotel: https://github.com/heigeo/climata/blob/master/climata/snotel/__init__.py
.. _climata.usgs: https://github.com/heigeo/climata/blob/master/climata/usgs/__init__.py
10 changes: 4 additions & 6 deletions climata/acis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
from datetime import datetime, date, timedelta
from wq.io import JsonParser, BaseIO, TupleMapper, TimeSeriesMapper
from climata.base import (
parse_date, WebserviceLoader, FilterOpt, DateOpt, ChoiceOpt
WebserviceLoader, FilterOpt, DateOpt, ChoiceOpt,
parse_date, fill_date_range,
)
from .constants import *

Expand Down Expand Up @@ -212,9 +212,8 @@ def load_data(self, data):
Infer time series based on start date.
"""

date = self.start_date
day = timedelta(days=1)
for row in data:
dates = fill_date_range(self.start_date, self.end_date)
for row, date in zip(data, dates):
data = {'date': date}
if self.add:
# If self.add is set, results will contain additional
Expand All @@ -235,7 +234,6 @@ def load_data(self, data):
elem = "e%s" % elem
data[elem] = val
yield data
date += day

def __init__(self, *args, **kwargs):
data = kwargs.pop('data')
Expand Down
31 changes: 28 additions & 3 deletions climata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from .version import VERSION
from wq.io.loaders import HTTPLIB_VERSION, VERSION as WQIO_VERSION

from datetime import datetime, timedelta


class FilterOpt(object):
"""
Expand Down Expand Up @@ -178,10 +180,8 @@ def str_value(val):

if value is None:
pass
elif isinstance(value, (list, tuple)):
value = [str_value(val) for val in value]
else:
value = [str_value(value)]
value = [str_value(val) for val in as_list(value)]

return value, complex

Expand All @@ -202,6 +202,8 @@ def get_params(self):
complex = False

for name, opt in self.filter_options.items():
if opt.ignored:
continue
if self.set_param(params, name):
complex = True
return params, complex
Expand Down Expand Up @@ -237,3 +239,26 @@ def user_agent(self):
"https://github.com/heigeo/climata"
)
return agent


def fill_date_range(start_date, end_date, date_format=None):
"""
Function accepts start date, end date, and format (if dates are strings)
and returns a list of Python dates.
"""

if date_format:
start_date = datetime.strptime(start_date, date_format).date()
end_date = datetime.strptime(end_date, date_format).date()
date_list = []
while start_date <= end_date:
date_list.append(start_date)
start_date = start_date + timedelta(days=1)
return date_list


def as_list(value):
if isinstance(value, (list, tuple)):
return value
else:
return [value]
Loading