Skip to content

Commit

Permalink
Merge pull request #154 from IvanKolesarEquinor/windIrisOptionalDates
Browse files Browse the repository at this point in the history
Make windiris source dates optional
  • Loading branch information
IvanKolesarEquinor authored Feb 7, 2019
2 parents 94041f5 + a4a74e2 commit 9b35ac1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
46 changes: 27 additions & 19 deletions camille/source/windiris.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import pytz


def _to_string(x):
x = re.sub('[^A-Za-z0-9,.]', '', str(x))
return '(' + x + ')'
Expand All @@ -18,25 +19,31 @@ def _sqlite(start_date,
los_id=None,
distance=None,
status=None):

query_params = []
if start_date is not None:
query_params.append(' Timestamp >= "{}" '
.format(str(start_date.astimezone(pytz.utc)
.replace(tzinfo=None))))
if end_date is not None:
query_params.append(' Timestamp < "{}" '
.format(str(end_date.astimezone(pytz.utc)
.replace(tzinfo=None))))
if los_id is not None:
query_params.append(' "LOS Index" IN {} '.format(_to_string(los_id)))
if distance is not None:
query_params.append(' Distance IN {} '.format(_to_string(distance)))
if status is not None:
query_params.append(' "RWS Status" IN {} '.format(_to_string(status)))

query = (
'SELECT * FROM ' + installation #nosec
+ ' WHERE Timestamp >= :start AND Timestamp < :end'
+ ( '' if los_id is None
else ' AND "LOS Index" IN {}'.format(_to_string(los_id)) )
+ ( '' if distance is None
else ' AND Distance IN {}'.format(_to_string(distance)) )
+ ( '' if status is None
else ' AND "RWS Status" IN {}'.format(_to_string(status)) )
'SELECT * FROM ' + _to_string(installation) #nosec
+ ('' if len(query_params) == 0
else ' WHERE ' + 'AND'.join(query_params))
+ ';'
)

df = pd.read_sql_query(query, connection,
params={
'start': str(start_date.astimezone(pytz.utc)
.replace(tzinfo=None)),
'end': str(end_date.astimezone(pytz.utc)
.replace(tzinfo=None))
},
index_col='Timestamp',
parse_dates={
'Timestamp': {'utc': True}
Expand Down Expand Up @@ -107,22 +114,23 @@ def windiris(root, tzinfo=pytz.utc):
raise ValueError('{} is not a directory'.format(root))

def windiris_internal(installation,
start_date,
end_date,
start_date=None,
end_date=None,
los_id=None,
distance=None,
status=None):

if start_date.tzinfo is None or end_date.tzinfo is None:
raise ValueError('dates must be timezone aware')

f = os.path.join(root, installation, installation + '_rtd.db' )

if not os.path.isfile(f):
raise ValueError('Installation {} not found'.format(installation))

conn = sqlite3.connect(f)

if (start_date is not None and start_date.tzinfo is None) or \
(end_date is not None and end_date.tzinfo is None):
raise ValueError('dates must be timezone aware')

return _sqlite(start_date,
end_date,
conn,
Expand Down
32 changes: 23 additions & 9 deletions tests/source/test_windiris.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@

wi = windiris('tests/test_data/windiris')

def test_load_all_data():
s = datetime(2017, 12, 17, tzinfo=utc)
e = datetime(2018, 10, 23, tzinfo=utc)
all_radial_windspeed = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3]
db_start_datetime = datetime(2017, 12, 17, tzinfo=utc)
db_end_datetime = datetime(2018, 10, 23, tzinfo=utc)

df = wi('inst2', s, e)
def test_load_all_data():
df = wi('inst2', db_start_datetime, db_end_datetime)

assert df.shape[0] == 27
assert (
df.radial_windspeed == [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3 ]
).all()
assert (df.radial_windspeed == all_radial_windspeed).all()


def test_load_one_day():
Expand Down Expand Up @@ -138,3 +137,18 @@ def test_no_time_zone():
with pytest.raises(ValueError) as exc:
wi('inst2', ntz_date, tz_date)
assert 'timezone aware' in str(exc)

def test_load_all_data_by_no_date():
df_no_dates = wi('inst2')

assert (df_no_dates.radial_windspeed == all_radial_windspeed).all()

def test_load_data_without_start_date():
df_no_start_date = wi('inst2', end_date=db_end_datetime)

assert (df_no_start_date.radial_windspeed == all_radial_windspeed).all()

def test_load_data_without_end_date():
df_no_end_date = wi('inst2', start_date=db_start_datetime)

assert (df_no_end_date.radial_windspeed == all_radial_windspeed).all()

0 comments on commit 9b35ac1

Please sign in to comment.