Skip to content

Instantly share code, notes, and snippets.

@rruntsch
Last active February 23, 2022 12:00
Show Gist options
  • Select an option

  • Save rruntsch/265e026faf99cbc93cb8088b86c5dc13 to your computer and use it in GitHub Desktop.

Select an option

Save rruntsch/265e026faf99cbc93cb8088b86c5dc13 to your computer and use it in GitHub Desktop.
Python Class c_ncei_data_service_api
import requests
class c_ncei_data_service_api:
"""
Name: c_ncei_data_service_api.py
Author: Randy Runtsch
Date: April 11, 2021
Description: Python wrapper class for the NOA NCEI
Data Service API used to obtain weather and climate data.
References: NCIE Data Service APU User Documentation - https://www.ncei.noaa.gov/support/access-data-service-api-user-documentation
"""
def __init__(self, dataset, data_types, stations, start_date_time, end_date_time, bounding_box):
# Set the base API URL.
self._base_api_url = 'https://www.ncei.noaa.gov/access/services/data/v1/?'
# Retrieve data.
self._dataset = self.call_api(dataset, data_types, stations, start_date_time, end_date_time, bounding_box)
def get_data(self):
# Return the data retrieved with the API call.
return self._dataset
def write_data_file(self, file_nm):
# Write the weather dataset to the specified file.
file = open(file_nm, 'w')
file.write(self._dataset)
file.close()
def call_api(self, dataset, data_types, stations, start_date_time, end_date_time, bounding_box):
# Create the full API request URL and submit it to the server. Add station names.
full_url = self._base_api_url + 'dataset=' + dataset + '&dataTypes=' + data_types + \
'&stations=' + stations + '&startDate=' + start_date_time + '&endDate=' + end_date_time + \
'&boundingBox=' + bounding_box + \
'&units=standard'
response = requests.get(full_url)
return response.text
from c_ncei_data_service_api import c_ncei_data_service_api
api_result = c_ncei_data_service_api('daily-summaries', 'AWND,WSF2,WSF5', 'USW00094846,USW00014925,USW00023293', '2000-01-01', '2020-12-31', '90,-180,-90,180')
print(api_result.get_data())
api_result.write_data_file('c:/project_data/weather/weather_data.csv')
@christophergraber
Copy link
Copy Markdown

Super helpful! I was able to look at json weather data from a station just near my house in Tucson, AZ, thanks.

@rruntsch
Copy link
Copy Markdown
Author

rruntsch commented Oct 17, 2021 via email

@Jackil1993
Copy link
Copy Markdown

Hello Sir,
That is super useful. I understand very little in meteorology. Can you give me a hint at what stations should I access to get precipitation data?
Eventually, I need state average or even US average in order to understand its effect on the fuel economy. Surely, I can aggregate by myself.

@rruntsch
Copy link
Copy Markdown
Author

rruntsch commented Dec 16, 2021 via email

@sdxingaijing
Copy link
Copy Markdown

Thanks so much.
非常感谢!

@rruntsch
Copy link
Copy Markdown
Author

rruntsch commented Feb 23, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment