from MesoPy import Meso

# Instance a Meso object by passing in YOUR api_token
m = Meso(token='demotoken')

"""
Explanation: 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.
End of explanation
"""


# Fetches the latest obs for Boulder airport within 30 min of now
latest = m.latest(stid='kbdu', within='30', units='ENGLISH')
latest

"""
Explanation: Now we should get the latest observation data for Boulder within 30 minutes of now
End of explanation
"""


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

"""
Explanation: 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.
End of explanation
"""


# Here we retrieve only the stations in Larimer County, Colorado
stations = m.metadata(state='CO', county='Larimer')
#stations  # uncomment to print the resulting stations

"""
Explanation: 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.
End of explanation
"""


# Calling variable_list() returns all possible sensor variables at stations
variables = m.variables()
#variables  # uncomment to print the resulting variables

"""
Explanation: You can retrieve the possible sensor variables available for the stations
End of explanation
"""


# 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

"""
Explanation: Climate observations are easy to obtain using the climatology_obs() function.
End of explanation
"""


# 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

"""
Explanation: 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.
End of explanation
"""


# 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

"""
Explanation: Time series observations must include the start and end parameter.
End of explanation
"""


# 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

"""
Explanation: Precipitation measurement observations can be retrieved specifically.
End of explanation
"""


# Returns a dictionary of all networks in the MesoWest repository 
networks = m.networks()
#networks # uncomment to get networks

"""
Explanation: Learn more about all of the networks in MesoWest with the networks() func.
End of explanation
"""


# Returns a dictionary of categories that networks may be assigned to
nettypes = m.networktypes()
#nettypes # uncomment to print network types

"""
Explanation: Or explore the categories MesoWest networks belong to.
End of explanation
"""


# 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

"""
Explanation: You can obtain a time series of statistics for any station.
End of explanation
"""


# 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

"""
Explanation: Or a climatology of statistics for any station.
End of explanation
"""


# Returns a dictionary of latency values
latency = m.latency(stid='mtmet', start='201403240000', end='201403280000')
#latency # uncomment to see latency values

"""
Explanation: Lastly, use the latency() function to see the latency of any station
End of explanation
"""


