Skip to content

Commit d2fd55c

Browse files
authored
Merge pull request dClimate#82 from dClimate/national-grid
national-grid retrieval code
2 parents fbe2148 + 27d9624 commit d2fd55c

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

dweather_client/client.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from dweather_client import gridded_datasets
2020
from dweather_client.storms_datasets import IbtracsDataset, AtcfDataset, SimulatedStormsDataset
2121
from dweather_client.ipfs_queries import AustraliaBomStations, CedaBiomass, CmeStationsDataset, DutchStationsDataset, DwdStationsDataset, DwdHourlyStationsDataset, GlobalHourlyStationsDataset, JapanStations, StationDataset, EauFranceDataset,\
22-
YieldDatasets, FsaIrrigationDataset, AemoPowerDataset, AemoGasDataset, AesoPowerDataset, ForecastDataset, AfrDataset, DroughtMonitor, CwvStations, SpeedwellStations, TeleconnectionsDataset, CsvStationDataset, StationForecastDataset
22+
YieldDatasets, FsaIrrigationDataset, AemoPowerDataset, AemoGasDataset, AesoPowerDataset, ForecastDataset, AfrDataset, DroughtMonitor, CwvStations, SpeedwellStations, TeleconnectionsDataset, CsvStationDataset, StationForecastDataset, SapStations
2323
from dweather_client.slice_utils import DateRangeRetriever, has_changed
2424
from dweather_client.ipfs_errors import *
2525
from io import StringIO
@@ -771,6 +771,19 @@ def get_cwv_station_history(station_name, as_of=None, ipfs_timeout=None):
771771
return (resp_series * u.dimensionless_unscaled).to_dict()
772772

773773

774+
def get_sap_station_history(as_of=None, ipfs_timeout=None):
775+
"""
776+
return:
777+
dict with datetime keys and sap Quantities as values
778+
"""
779+
metadata = get_metadata(get_heads()["sap-daily"])
780+
with SapStations(ipfs_timeout=ipfs_timeout, as_of=as_of) as dataset_obj:
781+
str_resp_series = dataset_obj.get_data()
782+
resp_series = str_resp_series.astype(float)
783+
# SAP uses financial units, best to return unscaled
784+
return (resp_series * u.dimensionless_unscaled).to_dict()
785+
786+
774787
def get_australia_station_history(station_name, weather_variable, desired_units=None, as_of=None, ipfs_timeout=None):
775788
"""
776789
return:
@@ -875,7 +888,7 @@ def get_teleconnections_history(weather_variable, ipfs_timeout=None):
875888
headers = next(reader)
876889
date_col = headers.index('DATE')
877890
try:
878-
data_col=headers.index("value")
891+
data_col = headers.index("value")
879892
except ValueError:
880893
raise WeatherVariableNotFoundError(
881894
"Invalid weather variable for this station")

dweather_client/ipfs_queries.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,43 @@ def extract_data_from_text(self, date_range, ipfs_hash, station_name):
926926
return data_dict
927927

928928

929+
class SapStations(GriddedDataset):
930+
"""
931+
Instantiable class for Composite Weather Variable Station Data
932+
"""
933+
@property
934+
def dataset(self):
935+
return "sap-daily"
936+
937+
@property
938+
def data_file_format(self):
939+
"""
940+
format string requires station name eg 'EM'
941+
"""
942+
return "sap_update_UK.txt"
943+
944+
def get_data(self):
945+
super().get_data()
946+
hashes = self.get_hashes()
947+
ret_dict = {}
948+
for h in hashes:
949+
date_range = self.get_date_range_from_metadata(h)
950+
new_dict = self.extract_data_from_text(date_range, h)
951+
ret_dict = {**ret_dict, **new_dict}
952+
return pd.Series(ret_dict)
953+
954+
def extract_data_from_text(self, date_range, ipfs_hash):
955+
byte_obj = self.get_file_object(
956+
f"{ipfs_hash}/{self.data_file_format}")
957+
data = byte_obj.read().decode("utf-8").split(",")
958+
day_itr = date_range[0].date()
959+
data_dict = {}
960+
for point in data:
961+
data_dict[day_itr] = point
962+
day_itr += datetime.timedelta(days=1)
963+
return data_dict
964+
965+
929966
class AustraliaBomStations(GriddedDataset):
930967
"""
931968
Instantiable class for Australia BOM Data
@@ -1250,7 +1287,7 @@ def __init__(self, ipfs_timeout=None):
12501287
def get_data(self, station):
12511288
super().get_data()
12521289
metadata = self.get_metadata(self.head)
1253-
1290+
12541291
file_name = f"{self.head}/{station}.csv"
12551292
return self.get_file_object(file_name).read().decode("utf-8")
12561293

dweather_client/tests/test_client.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from dweather_client.client import get_australia_station_history, get_station_history, get_gridcell_history, get_tropical_storms,\
44
get_yield_history, get_irrigation_data, get_power_history, get_gas_history, get_alberta_power_history, GRIDDED_DATASETS, has_dataset_updated,\
55
get_forecast_datasets, get_forecast, get_cme_station_history, get_european_station_history, get_hourly_station_history, get_drought_monitor_history, get_japan_station_history,\
6-
get_afr_history, get_cwv_station_history, get_teleconnections_history, get_station_forecast_history, get_station_forecast_stations, get_eaufrance_history
6+
get_afr_history, get_cwv_station_history, get_teleconnections_history, get_station_forecast_history, get_station_forecast_stations, get_eaufrance_history, get_sap_station_history
77
from dweather_client.aliases_and_units import snotel_to_ghcnd
88
import pandas as pd
99
from io import StringIO
@@ -362,7 +362,7 @@ def test_historical_storms():
362362

363363
def test_hist_storm_as_of():
364364
df_all_na = get_tropical_storms(
365-
'historical', 'NA', as_of=datetime.date(2023, 5, 20), ipfs_timeout=IPFS_TIMEOUT) # This will throw an exception if there's ever a break in the chain
365+
'historical', 'NA', as_of=datetime.date(2023, 5, 20), ipfs_timeout=IPFS_TIMEOUT) # This will throw an exception if there's ever a break in the chain
366366

367367

368368
def test_yields():
@@ -398,6 +398,12 @@ def test_cwv():
398398
assert data[sorted(data)[0]].unit == u.dimensionless_unscaled
399399

400400

401+
def test_sap():
402+
data = get_sap_station_history(ipfs_timeout=IPFS_TIMEOUT)
403+
assert len(data) == (sorted(data)[-1] - sorted(data)[0]).days + 1
404+
assert data[sorted(data)[0]].unit == u.dimensionless_unscaled
405+
406+
401407
def test_australia():
402408
data = get_australia_station_history(
403409
"Adelaide Airport", weather_variable="TMAX", ipfs_timeout=IPFS_TIMEOUT)
@@ -489,13 +495,17 @@ def test_has_dataset_updated_false():
489495

490496

491497
def test_forecast_station_history():
492-
history = get_station_forecast_history("cme_futures-daily", "D2", datetime.date(2023, 1, 31))
498+
history = get_station_forecast_history(
499+
"cme_futures-daily", "D2", datetime.date(2023, 1, 31))
493500
assert history[datetime.date(2023, 2, 28)] == 369.0
494501

502+
495503
def test_forecast_station_stations():
496-
stations = get_station_forecast_stations("cme_futures-daily", datetime.date(2023, 1, 31))
504+
stations = get_station_forecast_stations(
505+
"cme_futures-daily", datetime.date(2023, 1, 31))
497506
assert stations["features"][0]["properties"]["station name"] == "D2"
498507

508+
499509
def test_eaufrance_station():
500510
history = get_eaufrance_history("V720001002", "FLOWRATE")
501-
assert history[datetime.date(2022,4,2)].value == 749
511+
assert history[datetime.date(2022, 4, 2)].value == 749

0 commit comments

Comments
 (0)