def precip_dataframe(m: Meso, start: str, end: str, **kwargs) -> pandas.DataFrame: ''' Returns a pandas dataframe with STID as the index and the precip observation as the first column. :param m: Meso instance for retrieving data. :param start: start of observation window. :param end: end of observation window. :param kwargs: other key word arguments for the API. :return: the stations with a observation window within 0.05% of the expected window. The observation window is defined as (end - start). When creating the dataframe we filter out any observations that do not have that large of a window as they are un representative of the situation. The filter uses last_report and first_report from the API dictionary to determine if it is valid. (Returning this invalid data may be implemented) ''' kwargs['timeformat'] = '%s' # force timeformat df = json_normalize(m.precip(start=start, end=end, **kwargs)['STATION']) precip_col = np.full(df.shape[0], np.nan, dtype='float64') epoch_delta_col = np.full(df.shape[0], 0, dtype='int') start = datetime.strptime(start, '%Y%m%d%H%M') end = datetime.strptime(end, '%Y%m%d%H%M') delta = end.timestamp() - start.timestamp() if 'units' in kwargs: _accum_label = 'ACCUM_' + str(int( delta / 86400)).strip() + '_DAYS[' + kwargs['units'] + ']' else: _accum_label = 'ACCUM_' + str(int(delta / 86400)).strip() + '_DAYS[mm]' for i, row in df.iterrows( ): # extract precip observation into it's own column if len(row['OBSERVATIONS.precipitation']) > 0: _dict = row['OBSERVATIONS.precipitation'][0] epoch_delta_col[i] = (int(_dict['last_report']) - int(_dict['first_report'])) precip_col[i] = _dict['total'] df[_accum_label] = precip_col # precip data column df['EPOCH_TIMEDELTA'] = epoch_delta_col # used to filter out erroneous time periods df = df[abs(df['EPOCH_TIMEDELTA'] - delta) < .0005 * delta] df = df[['STID', _accum_label]].set_index('STID', drop=True) return df
startclim='04260000', endclim='04270000', units='precip|in') # Fetches the latest obs for Fort Collins airport within 30 min of Apr 26 18z attime = m.attime(stid='kfnl', attime='201504261800', within='30') # Or just get the latest observation within the last 15 minutes latest = m.latest(stid='kfnl', within='15') # Returns a time series from Fort Collins airport from Apr 26 18z to Apr 26 23z time = m.timeseries(stid='kfnl', start='201504261800', end='201504262300') # Returns the precip obs from Fort Collins airport from Apr 26 18z to Apr 27 12z precip = m.precip(stid='kfnl', start='201504261800', end='201504271200', units='precip|in') # Learn more about all of the networks in MesoWest with the networks() func networks = m.networks() # Or explore the categories MesoWest networks belong to nettypes = m.networktypes() # You can obtain time series statistics for any station stats = m.time_stats(stid='mtmet', start='201403240000', end='201403280000', type='all') # Or get climatology stats for a station (remember to change the date!)
variables = m.variables() # This returns a climatology for Denver from Apr 26 OOz to Apr 27 OOz climate = m.climatology(stid='kden', startclim='04260000', endclim='04270000', units='precip|in') # Fetches the latest obs for Fort Collins airport within 30 min of Apr 26 18z attime = m.attime(stid='kfnl', attime='201504261800', within='30') # Or just get the latest observation within the last 15 minutes latest = m.latest(stid='kfnl', within='15') # Returns a time series from Fort Collins airport from Apr 26 18z to Apr 26 23z time = m.timeseries(stid='kfnl', start='201504261800', end='201504262300') # Returns the precip obs from Fort Collins airport from Apr 26 18z to Apr 27 12z precip = m.precip(stid='kfnl', start='201504261800', end='201504271200', units='precip|in') # Learn more about all of the networks in MesoWest with the networks() func networks = m.networks() # Or explore the categories MesoWest networks belong to nettypes = m.networktypes() # You can obtain time series statistics for any station stats = m.time_stats(stid='mtmet', start='201403240000', end='201403280000', type='all') # Or get climatology stats for a station (remember to change the date!) clim_stats = m.climate_stats(stid='mtmet', startclim='03240000', endclim='03280000', type='all') # Lastly, see the latency of the API by using the latency() function latency = m.latency(stid='mtmet', start='201403240000', end='201403280000')
#print len(kbdistations) # Set default values for dictionary entries. These will later be overwritten if for station in stationsDict: stationsDict[station]['RECENT_OBS'] = -99 stationsDict[station]['MX_TEMP'] = -99 stationsDict[station]['MW_PCP'] = 0 stationsDict[station]['LONGITUDE'] = 0 stationsDict[station]['LATITUDE'] = 0 #stationsDict[station]['NoRainDays'] = 0 # precipitation query will return total precipitation over the last 24 hours #start, end = '201712111800','201712121200' print('start:', start, 'end:', end) mwprecipdata = m.precip(stid=kbdistations, start=start, end=end, units='precip|in') # Add vars for air_temp_high_6_hour if (mwprecipdata): for ob in mwprecipdata['STATION']: #print(ob) if 'total_precip_value_1' in ob['OBSERVATIONS']: obid = ob['STID'] if "E" in str(ob['OBSERVATIONS']['total_precip_value_1']): stationsDict[obid]['MW_PCP'] = ob['OBSERVATIONS'][ 'total_precip_value_1'][2:] elif ob['OBSERVATIONS']['total_precip_value_1'] == "T 0": stationsDict[obid]['MW_PCP'] = 0.0 # Try to catch any unexpected tags in data and add to log file elif re.search(