Skip to content

Instantly share code, notes, and snippets.

@py-ranoid
Last active January 18, 2022 09:26
Show Gist options
  • Save py-ranoid/f31fed3a9c21758f2e714194b5e7e07b to your computer and use it in GitHub Desktop.
Save py-ranoid/f31fed3a9c21758f2e714194b5e7e07b to your computer and use it in GitHub Desktop.
import requests
import pandas as pd
def get_weather_df(lat='40.7128',lon='-74.006',start_date='1979-01-01', end_date='2022-01-15'):
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',
'Origin': 'https://climate.northwestknowledge.net',
'Sec-Fetch-Site': 'cross-site',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://climate.northwestknowledge.net/',
'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
}
params = (
('decimal-precision', '5'),
('positive-east-longitude', 'False'),
('lat', lat), ('lon', lon),
('start-date', start_date), ('end-date', end_date),
('data-path', ['http://thredds.northwestknowledge.net/thredds/dodsC/agg_met_tmmn_1979_CurrentYear_CONUS.nc', 'http://thredds.northwestknowledge.net/thredds/dodsC/agg_met_tmmx_1979_CurrentYear_CONUS.nc', 'http://thredds.northwestknowledge.net/thredds/dodsC/agg_met_pr_1979_CurrentYear_CONUS.nc']),
('variable', ['daily_minimum_temperature', 'daily_maximum_temperature', 'precipitation_amount']),
('variable-name', ['tmmn', 'tmmx', 'pr']),
('request-JSON', 'True'),
)
response = requests.get('https://climate-dev.nkn.uidaho.edu/Services/get-netcdf-data/', headers=headers, params=params)
data = response.json()['data'][0]
weather_df = pd.DataFrame({i:data[i] for i in data.keys() if i not in ['lat_lon', 'metadata']})
return weather_df
get_weather_df('40.7128', '-74.006').to_csv('~/Downloads/hist_weather.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment