Skip to content

Commit 1c6655d

Browse files
committed
handle missing values in get_rainfall_dict and build_historical_rainfall_dict by returning None when data is missing
1 parent 900bebb commit 1c6655d

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

dweather_client/arithmetic.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def build_historical_rainfall_dict(lat, lon, dataset, start_date, end_date, dail
6363
dict of int: (float, int): keys are the start date year for each term, values are
6464
a tuple of total rainfall and the number of days in the term for the term period starting that year
6565
bool is_final: true if all data included is from the final dataset
66+
If there is missing rainfall data for a period, the value for that year will be None, None
6667
Raises:
6768
DatasetError: If no matching dataset found on server
6869
InputOutOfRangeError: If the lat/lon is outside the dataset range in metadata
@@ -87,9 +88,11 @@ def build_historical_rainfall_dict(lat, lon, dataset, start_date, end_date, dail
8788
# end date is tricky because it could be in the next calendar year
8889
historical_end = end_date.replace(year=year + (end_date.year - start_date.year))
8990
year_term = listify_period(historical_start, historical_end)
90-
if daily_cap:
91-
yearly_rain = [min(rainfall_dict[date], daily_cap) for date in year_term]
91+
yearly_rain = [rainfall_dict[date] for date in year_term]
92+
if None in yearly_rain:
93+
yearly_rainfall[year]= None, None
9294
else:
93-
yearly_rain = [rainfall_dict[date] for date in year_term]
94-
yearly_rainfall[year] = (sum(yearly_rain), len(yearly_rain))
95+
if daily_cap:
96+
yearly_rain = list(map(lambda x: min(x, daily_cap), yearly_rain))
97+
yearly_rainfall[year] = (sum(yearly_rain), len(yearly_rain))
9598
return yearly_rainfall, is_final

dweather_client/http_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ def get_rainfall_dict(lat, lon, dataset_revision, return_metadata=False):
168168
raise DataMalformedError ("Number of days in data file does not match the provided metadata")
169169
rainfall_dict = {}
170170
for i in range(days_in_record):
171-
rainfall_dict[dataset_start_date + datetime.timedelta(days=i)] = float(day_strs[i])
171+
if day_strs[i] == metadata["missing value"]:
172+
rainfall_dict[dataset_start_date + datetime.timedelta(days=i)] = None
173+
else:
174+
rainfall_dict[dataset_start_date + datetime.timedelta(days=i)] = float(day_strs[i])
172175
if return_metadata:
173176
return metadata, rainfall_dict
174177
else:

0 commit comments

Comments
 (0)