9
9
from dweather_client .struct_utils import tupleify , convert_nans_to_none
10
10
import datetime
11
11
import pytz
12
- import csv , json
12
+ import csv
13
+ import json
13
14
import inspect
14
15
import numpy as np
15
16
import pandas as pd
16
17
from astropy import units as u
17
18
from timezonefinder import TimezoneFinder
18
19
from dweather_client import gridded_datasets
19
20
from dweather_client .storms_datasets import IbtracsDataset , AtcfDataset , SimulatedStormsDataset
20
- from dweather_client .ipfs_queries import AustraliaBomStations , CedaBiomass , CmeStationsDataset , DutchStationsDataset , DwdStationsDataset , DwdHourlyStationsDataset , GlobalHourlyStationsDataset , JapanStations , StationDataset ,\
21
+ from dweather_client .ipfs_queries import AustraliaBomStations , CedaBiomass , CmeStationsDataset , DutchStationsDataset , DwdStationsDataset , DwdHourlyStationsDataset , GlobalHourlyStationsDataset , JapanStations , StationDataset , EauFranceDataset , \
21
22
YieldDatasets , FsaIrrigationDataset , AemoPowerDataset , AemoGasDataset , AesoPowerDataset , ForecastDataset , AfrDataset , DroughtMonitor , CwvStations , SpeedwellStations , TeleconnectionsDataset , CsvStationDataset , StationForecastDataset
22
23
from dweather_client .slice_utils import DateRangeRetriever , has_changed
23
24
from dweather_client .ipfs_errors import *
@@ -246,6 +247,7 @@ def get_tropical_storms(
246
247
min_lon = None ,
247
248
max_lat = None ,
248
249
max_lon = None ,
250
+ as_of = None ,
249
251
ipfs_timeout = None ):
250
252
"""
251
253
return:
@@ -288,11 +290,11 @@ def get_tropical_storms(
288
290
289
291
with cm as storm_getter :
290
292
if radius :
291
- return storm_getter .get_data (basin , radius = radius , lat = lat , lon = lon )
293
+ return storm_getter .get_data (basin , radius = radius , lat = lat , lon = lon , as_of = as_of )
292
294
elif min_lat :
293
- return storm_getter .get_data (basin , min_lat = min_lat , min_lon = min_lon , max_lat = max_lat , max_lon = max_lon )
295
+ return storm_getter .get_data (basin , min_lat = min_lat , min_lon = min_lon , max_lat = max_lat , max_lon = max_lon , as_of = as_of )
294
296
else :
295
- return storm_getter .get_data (basin )
297
+ return storm_getter .get_data (basin , as_of = as_of )
296
298
297
299
298
300
def get_station_history (
@@ -485,6 +487,7 @@ def get_hourly_station_history(dataset, station_id, weather_variable, use_imperi
485
487
v ) for k , v in final_resp_series .to_dict ().items ()}
486
488
return result
487
489
490
+
488
491
def get_csv_station_history (dataset , station_id , weather_variable , use_imperial_units = True , desired_units = None , ipfs_timeout = None ):
489
492
"""
490
493
This is almost an exact copy of get_hourly_station_history
@@ -543,14 +546,26 @@ def get_csv_station_history(dataset, station_id, weather_variable, use_imperial_
543
546
"Invalid weather variable for this station" )
544
547
545
548
try :
546
- if dataset == "inmet_brazil-hourly" :
549
+ # RawSet style where we only want the most recent file
550
+ if dataset in ["inmet_brazil-hourly" ]:
547
551
with CsvStationDataset (dataset = dataset , ipfs_timeout = ipfs_timeout ) as dataset_obj :
548
- csv_text = dataset_obj .get_data (station_id , weather_variable )
552
+ csv_text_list = [dataset_obj .get_data (
553
+ station_id , weather_variable )]
554
+ # ClimateSet style where we need the entire linked list history
555
+ elif dataset in ["ne_iso-hourly" ]:
556
+ with CsvStationDataset (dataset = dataset , ipfs_timeout = ipfs_timeout ) as dataset_obj :
557
+ csv_text_list = dataset_obj .get_data_recursive (
558
+ station_id , weather_variable )
549
559
else :
550
560
raise DatasetError ("No such dataset in dClimate" )
551
561
except ipfshttpclient .exceptions .ErrorResponse :
552
562
raise StationNotFoundError ("Invalid station ID for dataset" )
553
- df = pd .read_csv (StringIO (csv_text ))
563
+
564
+ # concat together all retrieved station csv texts
565
+ dfs = []
566
+ for csv_text in csv_text_list :
567
+ dfs .append (pd .read_csv (StringIO (csv_text )))
568
+ df = pd .concat (dfs , ignore_index = True )
554
569
str_resp_series = df [column_name ].astype (str )
555
570
df = df .set_index ("dt" )
556
571
if desired_units :
@@ -579,18 +594,21 @@ def get_csv_station_history(dataset, station_id, weather_variable, use_imperial_
579
594
v ) for k , v in final_resp_series .to_dict ().items ()}
580
595
return result
581
596
597
+
582
598
def get_station_forecast_history (dataset , station_id , forecast_date , desired_units = None , ipfs_timeout = None ):
583
- try :
599
+ try :
584
600
with StationForecastDataset (dataset , ipfs_timeout = ipfs_timeout ) as dataset_obj :
585
601
csv_text = dataset_obj .get_data (station_id , forecast_date )
586
602
history = {}
587
603
reader = csv .reader (csv_text .split ('\n ' ))
588
604
headers = next (reader )
589
605
date_col = headers .index ('DATE' )
590
606
try : # Make sure weather variable is correct.
591
- data_col = headers .index ("SETT" ) #at the moment the only variable is "SETT"
607
+ # at the moment the only variable is "SETT"
608
+ data_col = headers .index ("SETT" )
592
609
except ValueError :
593
- raise WeatherVariableNotFoundError ("Invalid weather variable for this station" )
610
+ raise WeatherVariableNotFoundError (
611
+ "Invalid weather variable for this station" )
594
612
for row in reader :
595
613
try :
596
614
if not row :
@@ -599,11 +617,12 @@ def get_station_forecast_history(dataset, station_id, forecast_date, desired_uni
599
617
row [date_col ], "%Y-%m-%d" ).date ()] = float (row [data_col ])
600
618
except ValueError :
601
619
history [datetime .datetime .strptime (
602
- row [date_col ], "%Y-%m-%d" ).date ()] = row [data_col ]
620
+ row [date_col ], "%Y-%m-%d" ).date ()] = row [data_col ]
603
621
return history
604
622
except ipfshttpclient .exceptions .ErrorResponse :
605
623
raise StationNotFoundError ("Invalid station ID for dataset" )
606
624
625
+
607
626
def get_station_forecast_stations (dataset , forecast_date , desired_units = None , ipfs_timeout = None ):
608
627
with StationForecastDataset (dataset , ipfs_timeout = ipfs_timeout ) as dataset_obj :
609
628
csv_text = dataset_obj .get_stations (forecast_date )
@@ -850,13 +869,13 @@ def has_dataset_updated(dataset, slices, as_of, ipfs_timeout=None):
850
869
851
870
def get_teleconnections_history (weather_variable , ipfs_timeout = None ):
852
871
with TeleconnectionsDataset (ipfs_timeout = ipfs_timeout ) as dataset_obj :
853
- csv_text = dataset_obj .get_data ()
872
+ csv_text = dataset_obj .get_data (weather_variable )
854
873
history = {}
855
874
reader = csv .reader (csv_text .split ('\n ' ))
856
875
headers = next (reader )
857
876
date_col = headers .index ('DATE' )
858
- try : # Make sure weather variable is correct.
859
- data_col = headers .index (weather_variable )
877
+ try :
878
+ data_col = headers .index ("value" )
860
879
except ValueError :
861
880
raise WeatherVariableNotFoundError (
862
881
"Invalid weather variable for this station" )
@@ -874,3 +893,40 @@ def get_teleconnections_history(weather_variable, ipfs_timeout=None):
874
893
history [datetime .datetime .strptime (
875
894
row [date_col ], "%Y-%m-%d" ).date ()] = row [data_col ]
876
895
return history
896
+
897
+
898
+ def get_eaufrance_history (station , weather_variable , use_imperial_units = False , desired_units = None , ipfs_timeout = None ):
899
+ try :
900
+ with EauFranceDataset (ipfs_timeout = ipfs_timeout ) as dataset_obj :
901
+ csv_text = dataset_obj .get_data (station )
902
+ df = pd .read_csv (StringIO (csv_text ))
903
+ df = df .set_index ("DATE" )
904
+ original_units = "m^3/s"
905
+ if desired_units :
906
+ converter , dweather_unit = get_unit_converter_no_aliases (
907
+ original_units , desired_units )
908
+ else :
909
+ converter , dweather_unit = get_unit_converter (
910
+ original_units , use_imperial_units )
911
+ if converter :
912
+ try :
913
+ converted_resp_series = pd .Series (
914
+ converter (df [weather_variable ].values * dweather_unit ), index = df .index )
915
+ except ValueError :
916
+ raise UnitError (
917
+ f"Specified unit is incompatible with original, original units are { original_units } and requested units are { desired_units } " )
918
+ if desired_units is not None :
919
+ rounded_resp_array = np .vectorize (rounding_formula_temperature )(
920
+ str_resp_series , converted_resp_series )
921
+ final_resp_series = pd .Series (
922
+ rounded_resp_array * converted_resp_series .values .unit , index = df .index )
923
+ else :
924
+ final_resp_series = converted_resp_series
925
+ else :
926
+ final_resp_series = pd .Series (
927
+ df [weather_variable ].values * dweather_unit , index = df .index )
928
+ result = {datetime .date .fromisoformat (k ): convert_nans_to_none (
929
+ v ) for k , v in final_resp_series .to_dict ().items ()}
930
+ return result
931
+ except ipfshttpclient .exceptions .ErrorResponse :
932
+ raise StationNotFoundError ("Invalid station ID for dataset" )
0 commit comments