# Displaying the dictionary for key, value in extent.items(): print(str(key)+':'+str(value)) #extent = 41.53,41.21,-84.90,-85.33 # input of start data, end date, type of dataset, and name of gauge start_date = input('Enter begin date (format:yyyy-mm-dd) \t') # 2019-01-01 startdate = pd.to_datetime(start_date) end_date = input('Enter end date (format:yyyy-mm-dd) \t') #2019-12-31 enddate = pd.to_datetime(end_date) print(startdate, enddate) datasetid = 'GHCND' #The find_station function returns the dataframe containing stations' info within the input extent. stations = my_client.find_stations( datasetid = datasetid, extent = extent, startdate = startdate, enddate = enddate, return_dataframe = True) print(stations) # download data from all stations specified by their id for i in stations.id: Rainfall_data = my_client.get_data_by_station(datasetid = datasetid, stationid = i, startdate = startdate, enddate = enddate, return_dataframe = True, include_station_meta = True) filename = datasetid + '_' + i + '.csv' Rainfall_data.to_csv(filename) # Get the daily average values of all stations P=[] for i in stations.id:
def filter(self): # semantic checks on params # Check (1) exactly four values need to be provided in extent extent_vals = list(map((lambda val: float(val)),self.extent.split(','))) if len(extent_vals) != 4: raise GeoEDFError('NOAAStationFilter requires a N,S,E,W string of floating point numbers as the extent') # Check (2) that lat and lon pairs are in the right order north = extent_vals[0] south = extent_vals[1] east = extent_vals[2] west = extent_vals[3] if south > north: raise GeoEDFError('please check the ordering of the south and north extents') if west > east: raise GeoEDFError('please check the ordering of the east and west extents') # passed semantic checks, prepare dict of extents for API extent_dict = {"north": north, "south": south, "east": east, "west": west} # process dates try: startdate = pd.to_datetime(self.start_date,format='%m/%d/%Y') enddate = pd.to_datetime(self.end_date,format='%m/%d/%Y') except: raise GeoEDFError("Error parsing dates provided to NOAAStationFiler, please ensure format is mm/dd/YYYY") # param checks complete try: # get a client for NCDC API usage cdo_client = Client(self.token, default_units="None", default_limit=1000) # we are looking for stations with GHCND data #The find_stations function returns the dataframe containing stations' info within the input extent. stations = cdo_client.find_stations( datasetid="GHCND", extent=extent_dict, startdate=startdate, enddate=enddate, return_dataframe=True) # filter to only retain stations which have sufficient data for the date range stations_to_drop = [] # Drop stations without enough observations for the given date range for i in range(len(stations.maxdate)): # get max and min date of each station station_maxdate = pd.to_datetime(stations.maxdate[i],format='%Y-%m-%d') station_mindate = pd.to_datetime(stations.mindate[i],format='%Y-%m-%d') # check if station's maxdate is earlier than enddate if station_maxdate < enddate: stations_to_drop.append(i) # check if station's mindate is later than startdate if station_mindate > startdate: stations_to_drop.append(i) # delete stations without enough time length valid_stations = stations.drop(stations.index[stations_to_drop]) # add station IDs to values array self.values += list(valid_stations.id) except: raise GeoEDFError('Error occurred when querying NCDC API for stations in NOAAStationFiler')