This notebook demonstrates some basic functions of the MesoPy package. Here we'll provide a brief intro to each function and demonstrate how to obtain data from the dictionaries that are returned.
External libraries used:
First we'll import the Meso object from MesoPy and pass in an API token to create an instance to talk to the MesoWest servers.
In [1]:
from MesoPy import Meso
# Instance a Meso object by passing in YOUR api_token
m = Meso(token='demotoken')
Now we should get the latest observation data for Boulder within 30 minutes of now
In [2]:
# Fetches the latest obs for Boulder airport within 30 min of now
latest = m.latest(stid='kbdu', within='30', units='ENGLISH')
latest
Out[2]:
This returns a wealth of information about the station including the observations we requested. Since the returned data is a dictionary containing several lists, we can 'drill down' into the data and get the info we need.
In [3]:
ob = latest['STATION'][0]
st_name = ob['NAME']
temp = str(ob['OBSERVATIONS']['air_temp_value_1']['value']) + u'\N{DEGREE SIGN}' + 'F'
wind = str(ob['OBSERVATIONS']['wind_speed_value_1']['value']) + ' mph'
result = 'The current weather at ' + st_name + ' is ' + temp + ' with a sustained wind of ' + wind
result
Out[3]:
You can retrieve information about certain stations or search for stations based on state, county, etc.
In [4]:
# Here we retrieve only the stations in Larimer County, Colorado
stations = m.metadata(state='CO', county='Larimer')
#stations # uncomment to print the resulting stations
You can retrieve the possible sensor variables available for the stations
In [5]:
# Calling variable_list() returns all possible sensor variables at stations
variables = m.variables()
#variables # uncomment to print the resulting variables
Climate observations are easy to obtain using the climatology_obs() function.
In [6]:
# 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')
#climate # uncomment to print climate observations
The latest observations for a station or list of stations can be retrieved by either specifying an exact time or within a certain time range of the current time.
In [7]:
# Fetches the latest obs for Fort Collins airport within 30 min of Apr 26 18z
attime = m.attime(stid='kfnl', attime='201504261800', within='30')
#attime # uncomment to print the latest obs
Time series observations must include the start and end parameter.
In [8]:
# 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')
#time # uncomment to obtain a time series of observations
Precipitation measurement observations can be retrieved specifically.
In [9]:
# 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')
#precip # uncomment to get the precipitation obs
Learn more about all of the networks in MesoWest with the networks() func.
In [10]:
# Returns a dictionary of all networks in the MesoWest repository
networks = m.networks()
#networks # uncomment to get networks
Or explore the categories MesoWest networks belong to.
In [11]:
# Returns a dictionary of categories that networks may be assigned to
nettypes = m.networktypes()
#nettypes # uncomment to print network types
You can obtain a time series of statistics for any station.
In [12]:
# Returns stats for the mountain meteorology station at the University of Utah for the specified dates
stats = m.time_stats(stid='mtmet', start='201403240000', end='201403280000', type='all')
#stats # uncomment to see stats
Or a climatology of statistics for any station.
In [13]:
# Returns climatology stats for a station (remember to change the date format for climatology!)
clim_stats = m.climate_stats(stid='mtmet', startclim='03240000', endclim='03280000', type='all')
#clim_stats # uncomment to see climate stats
Lastly, use the latency() function to see the latency of any station
In [14]:
# Returns a dictionary of latency values
latency = m.latency(stid='mtmet', start='201403240000', end='201403280000')
#latency # uncomment to see latency values