Convert package to module

master
Dan Howe 7 years ago
parent 73bbde9bc6
commit 6d99762e34

@ -0,0 +1,10 @@
from setuptools import setup
setup(
name='silo',
version='0.1.0',
packages=['silo'],
install_requires=['requests', 'pandas'],
author='Dan Howe',
author_email='d.howe@wrl.unsw.edu.au',
description='Request climate data from SILO')

@ -0,0 +1 @@
from .silo import pointdata

@ -1,132 +1,132 @@
import requests import requests
import pandas as pd import pandas as pd
from io import StringIO from io import StringIO
def pointdata(variables, def pointdata(variables,
api_key, api_key,
start, start,
finish, finish,
station=None, station=None,
lat=None, lat=None,
lon=None, lon=None,
units=True, units=True,
output=None): output=None):
"""Request point data from SILO. """Request point data from SILO.
Args: Args:
variables: list of variable code strings (see variable info below) variables: list of variable code strings (see variable info below)
api_key: SILO api key api_key: SILO api key
start: start date (yyyymmdd, earliest date is '18890101') start: start date (yyyymmdd, earliest date is '18890101')
finish: finish date (yyyymmdd, latest date is yesterday) finish: finish date (yyyymmdd, latest date is yesterday)
station: weather station ID (or use 'lat' and 'lon') station: weather station ID (or use 'lat' and 'lon')
lat: latitude (between -44° and -10°, in increments of 0.05°) lat: latitude (between -44° and -10°, in increments of 0.05°)
lon: longitude (between 112° and 154°, in increments of 0.05°) lon: longitude (between 112° and 154°, in increments of 0.05°)
units: include units in dataframe column names units: include units in dataframe column names
output: name of file to export data (csv format) output: name of file to export data (csv format)
Returns: Returns:
Dataframe containing climate data time series Dataframe containing climate data time series
API tutorial: API tutorial:
https://silo.longpaddock.qld.gov.au/api-documentation/tutorial https://silo.longpaddock.qld.gov.au/api-documentation/tutorial
API documentation: API documentation:
https://silo.longpaddock.qld.gov.au/api-documentation/reference https://silo.longpaddock.qld.gov.au/api-documentation/reference
API key registration: API key registration:
https://silo.longpaddock.qld.gov.au/register https://silo.longpaddock.qld.gov.au/register
Variable info: Variable info:
Code Units Name Code Units Name
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
daily_rain mm Daily rainfall daily_rain mm Daily rainfall
monthly_rain mm Monthly rainfall monthly_rain mm Monthly rainfall
max_temp Celsius Maximum temperature max_temp Celsius Maximum temperature
min_temp Celsius Minimum temperature min_temp Celsius Minimum temperature
vp hPa Vapour pressure vp hPa Vapour pressure
vp_deficit hPa Vapour pressure deficit vp_deficit hPa Vapour pressure deficit
evap_pan mm Evaporation - Class A pan evap_pan mm Evaporation - Class A pan
evap_syn mm Evaporation - synthetic estimate evap_syn mm Evaporation - synthetic estimate
evap_comb mm Evaporation - combination (synthetic estimate evap_comb mm Evaporation - combination (synthetic estimate
pre-1970, class A pan 1970 onwards) pre-1970, class A pan 1970 onwards)
evap_morton_lake mm Evaporation - Morton's shallow lake evap. evap_morton_lake mm Evaporation - Morton's shallow lake evap.
radiation MJm-2 Solar radiation - total incoming downward radiation MJm-2 Solar radiation - total incoming downward
shortwave radiation on a horizontal surface shortwave radiation on a horizontal surface
rh_tmax % Relative humidity at the time of max. temp. rh_tmax % Relative humidity at the time of max. temp.
rh_tmin % Relative humidity at the time of min. temp. rh_tmin % Relative humidity at the time of min. temp.
et_short_crop mm Evapotranspiration - FAO56 short crop et_short_crop mm Evapotranspiration - FAO56 short crop
et_tall_crop mm Evapotranspiration - ASCE tall crop et_tall_crop mm Evapotranspiration - ASCE tall crop
et_morton_actual mm Evapotranspiration - Morton's areal actual et_morton_actual mm Evapotranspiration - Morton's areal actual
evapotranspiration evapotranspiration
et_morton_potential mm Evapotranspiration - Morton's potential et_morton_potential mm Evapotranspiration - Morton's potential
evapotranspiration evapotranspiration
et_morton_wet mm Evapotranspiration - Morton's wet-environment et_morton_wet mm Evapotranspiration - Morton's wet-environment
areal evapotranspiration over land areal evapotranspiration over land
mslp hPa Mean sea level pressure mslp hPa Mean sea level pressure
""" """
unit_defs = { unit_defs = {
'daily_rain': 'mm', 'daily_rain': 'mm',
'monthly_rain': 'mm', 'monthly_rain': 'mm',
'max_temp': 'Celsius', 'max_temp': 'Celsius',
'min_temp': 'Celsius', 'min_temp': 'Celsius',
'vp': 'hPa', 'vp': 'hPa',
'vp_deficit': 'hPa', 'vp_deficit': 'hPa',
'evap_pan': 'mm', 'evap_pan': 'mm',
'evap_syn': 'mm', 'evap_syn': 'mm',
'evap_comb': 'mm', 'evap_comb': 'mm',
'evap_morton_lake': 'mm', 'evap_morton_lake': 'mm',
'radiation': 'MJm-2', 'radiation': 'MJm-2',
'rh_tmax': '%', 'rh_tmax': '%',
'rh_tmin': '%', 'rh_tmin': '%',
'et_short_crop': 'mm', 'et_short_crop': 'mm',
'et_tall_crop': 'mm', 'et_tall_crop': 'mm',
'et_morton_actual': 'mm', 'et_morton_actual': 'mm',
'et_morton_potential': 'mm', 'et_morton_potential': 'mm',
'et_morton_wet': 'mm', 'et_morton_wet': 'mm',
'mslp': 'hPa', 'mslp': 'hPa',
} }
# Validate inputs # Validate inputs
if (type(lat) and type(lon)) == type(station): if (type(lat) and type(lon)) == type(station):
raise ValueError( raise ValueError(
"'lat' and 'lon' must be provided if 'station' is not specified.") "'lat' and 'lon' must be provided if 'station' is not specified.")
params = { params = {
'apikey': api_key, 'apikey': api_key,
'format': 'csv', 'format': 'csv',
'start': start, 'start': start,
'finish': finish, 'finish': finish,
'variables': ','.join(variables) 'variables': ','.join(variables)
} }
if station is not None: if station is not None:
# Get observations from specified weather station # Get observations from specified weather station
params['station'] = station params['station'] = station
else: else:
# Get observations from specified coordinates # Get observations from specified coordinates
params['lat'] = lat params['lat'] = lat
params['lon'] = lon params['lon'] = lon
base_url = 'https://siloapi.longpaddock.qld.gov.au/pointdata' base_url = 'https://siloapi.longpaddock.qld.gov.au/pointdata'
r = requests.get(base_url, params=params) r = requests.get(base_url, params=params)
text = r.content.decode() text = r.content.decode()
df = pd.read_csv(StringIO(text), parse_dates=['date']) df = pd.read_csv(StringIO(text), parse_dates=['date'])
df = df.set_index('date') df = df.set_index('date')
# Add units to columns names # Add units to columns names
if units: if units:
labels = {} labels = {}
for key, val in unit_defs.items(): for key, val in unit_defs.items():
labels[key] = '{}_{}'.format(key, val) labels[key] = '{}_{}'.format(key, val)
df = df.rename(columns=labels) df = df.rename(columns=labels)
# Write to csv # Write to csv
if output: if output:
df.to_csv(output) df.to_csv(output)
return df return df
Loading…
Cancel
Save