2

I am trying to use the NOAA API to get data on monthly mean temperature and precipitation for the past 10 years. I wrote the following python code (the variable "token" in the code is the API token that I got:

import requests
url = "http://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCNDMS&locationid=ZIP:28801&startdate=2000-01-01&enddate=2010-01-01"
response = requests.get(url, headers = token) 
data = response.json()

I am not able to get data for for the specified time period. I am not sure where I am going wrong.

  • Wild speculation: too much data requested? What happens if you try 1 year or even 1 month? – mkennedy Jul 31 '15 at 22:27
  • what response do you get? – toms Jul 31 '15 at 22:44
  • I am getting the average monthly temperature data but its for only one month. I don't know why I am unable to get the rest of the data. Even if I try to get data for a few months, I still end up getting data for the first month. – user3119875 Aug 3 '15 at 13:55
3

From the looks of it, you are getting a python error because the headers argument is expecting a dictionary, not a string

The following works for me on the entire date range:

import requests
url = "http://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCNDMS&locationid=ZIP:28801&startdate=2000-01-01&enddate=2010-01-01"
# replace 'myToken' with the actual token, below
headers = {'token': 'myToken'}
response = requests.get(url, headers = headers)
response = response.json()

# the beginning of the response
{u'results': [{u'date': u'2000-01-01T00:00:00', u'datatype': u'CLDD', u'station': u'GHCND:USW00013872', ...

Note: curl is a useful tool here, too:

curl -H "token:myToken" "http://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCNDMS&locationid=ZIP:28801&startdate=2000-01-01&enddate=2010-01-01"
  • Yes, I my header is a dictionary with my token information. The data is similar to your data. I don't know how to use curl in python hence I ended up writing the above code. I am having issue with the result that I am getting. I am getting the average monthly temperature data but its for only one month. I don't know why I am unable to get the rest of the data. Even if I try to get data for a few months, I still end up getting data for the first month. I don't know what I need to do to get the entire data. – user3119875 Aug 3 '15 at 13:58
2

I figured out where I went wrong. I forgot to set my limit for the data, so by default the limit was set up to be 25. This was the reason why I was getting data just for the first month. Thanks for your help.

  • I was also getting a response limit of 25. I added &limit=50 to the end of the url and all is good now. – GISdirtsniffer Nov 2 '16 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.