Skip to content

Instantly share code, notes, and snippets.

@garethpaul
Created January 22, 2020 05:23
Show Gist options
  • Save garethpaul/13ae8af894c85d7eae25c03972656577 to your computer and use it in GitHub Desktop.
Save garethpaul/13ae8af894c85d7eae25c03972656577 to your computer and use it in GitHub Desktop.
#for each year from 2018-2019 ...
for year in range(2018, 2019):
year = str(year)
print('working on year '+year)
#make the api call
r = requests.get('https://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCND&datatypeid=TAVG&datatypeid=TMAX&limit=1000&stationid='+station_id+'&startdate='+year+'-01-01&enddate='+year+'-12-31', headers={'token':token})
r2 = requests.get('https://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCND&datatypeid=TMIN&datatypeid=PRCP&limit=1000&stationid='+station_id+'&startdate='+year+'-01-01&enddate='+year+'-12-31', headers={'token':token})
#load the api response as a json
d = json.loads(r.text)
d2 = json.loads(r2.text)
#get all items in the response which are average temperature readings
avg_temps = [item for item in d['results'] if item['datatype']=='TAVG']
min_temps = [item for item in d2['results'] if item['datatype']=='TMIN']
max_temps = [item for item in d['results'] if item['datatype']=='TMAX']
precp = [item for item in d2['results'] if item['datatype']=='PRCP']
dates_temp += [item['date'] for item in avg_temps]
#get the actual average temperature from all average temperature readings
temps += [item['value'] for item in avg_temps]
minT += [item['value'] for item in min_temps]
maxT += [item['value'] for item in max_temps]
prcp += [item['value'] for item in precp]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment