Using MesoPy to retrieve observations

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:

  • MesoPy

Example

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]:
{'STATION': [{'ELEVATION': '5288',
   'ID': '31841',
   'LATITUDE': '40.0394297',
   'LONGITUDE': '-105.2258217',
   'MNET_ID': '1',
   'NAME': 'Boulder Municipal Airport',
   'OBSERVATIONS': {'air_temp_value_1': {'date_time': '2016-01-08T06:16:00Z',
     'value': 32.0},
    'altimeter_value_1': {'date_time': '2016-01-08T06:16:00Z', 'value': 35.89},
    'ceiling_value_1': {'date_time': '2016-01-08T06:16:00Z', 'value': 400.0},
    'cloud_layer_1_code_value_1': {'date_time': '2016-01-08T06:16:00Z',
     'value': 44.0},
    'dew_point_temperature_value_1': {'date_time': '2016-01-08T06:16:00Z',
     'value': 32.0},
    'relative_humidity_value_1': {'date_time': '2016-01-08T06:16:00Z',
     'value': 100.0},
    'visibility_value_1': {'date_time': '2016-01-08T06:16:00Z', 'value': 1.75},
    'weather_cond_code_value_1': {'date_time': '2016-01-08T06:16:00Z',
     'value': 20.0},
    'wind_direction_value_1': {'date_time': '2016-01-08T06:16:00Z',
     'value': 0.0},
    'wind_speed_value_1': {'date_time': '2016-01-08T06:16:00Z', 'value': 0.0}},
   'PERIOD_OF_RECORD': {'end': '2016-01-08T06:16:00Z',
    'start': '2011-10-27T00:00:00Z'},
   'SENSOR_VARIABLES': {'air_temp': {'air_temp_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '2'}},
    'altimeter': {'altimeter_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '2'}},
    'ceiling': {'ceiling_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '1'}},
    'cloud_layer_1_code': {'cloud_layer_1_code_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '1'}},
    'dew_point_temperature': {'dew_point_temperature_value_1': {}},
    'relative_humidity': {'relative_humidity_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '2'}},
    'visibility': {'visibility_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '3'}},
    'weather_cond_code': {'weather_cond_code_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '3'}},
    'wind_direction': {'wind_direction_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '10'}},
    'wind_speed': {'wind_speed_value_1': {'period_of_record': {'end': '2016-01-08T06:16:00Z',
       'start': '2011-10-27T00:00:00Z'},
      'position': '10'}}},
   'STATE': 'CO',
   'STATUS': 'ACTIVE',
   'STID': 'KBDU',
   'TIMEZONE': 'America/Denver'}],
 'SUMMARY': {'DATA_PARSING_TIME': '1.18088722229 ms',
  'DATA_QUERY_TIME': '34.3651771545 ms',
  'METADATA_RESPONSE_TIME': '2.04610824585 ms',
  'NUMBER_OF_OBJECTS': 1,
  'RESPONSE_CODE': 1,
  'RESPONSE_MESSAGE': 'OK',
  'TOTAL_DATA_TIME': '35.551071167 ms',
  'TOTAL_TIME': '140.078783035 ms'},
 'UNITS': {'air_temp': 'Fahrenheit',
  'altimeter': 'Pascals',
  'ceiling': 'Feet',
  'cloud_layer_1_code': 'code',
  'dew_point_temperature': 'Fahrenheit',
  'relative_humidity': '%',
  'visibility': 'Statute miles',
  'weather_cond_code': 'code',
  'wind_direction': 'Degrees',
  'wind_speed': 'knots'}}

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]:
'The current weather at Boulder Municipal Airport is 32.0°F with a sustained wind of 0.0 mph'

Other Functions

For a complete list of parameters for each function, consult the latest documentation.

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