Skip to content

Instantly share code, notes, and snippets.

@rruntsch
Last active February 23, 2022 12:00
Show Gist options
  • Save rruntsch/265e026faf99cbc93cb8088b86c5dc13 to your computer and use it in GitHub Desktop.
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')
@rruntsch
Copy link
Author

rruntsch commented Dec 16, 2021 via email

@sdxingaijing
Copy link

Thanks so much.
非常感谢!

@rruntsch
Copy link
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