Export dataframe to csv

master
Dan Howe 7 years ago
parent 23789a8942
commit 4d19214d2e

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

Loading…
Cancel
Save