Last active
February 23, 2022 12:00
-
-
Save rruntsch/265e026faf99cbc93cb8088b86c5dc13 to your computer and use it in GitHub Desktop.
Python Class c_ncei_data_service_api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_data(self): | |
# Return the data retrieved with the API call. | |
return self._dataset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Author
rruntsch
commented
Feb 23, 2022
via email
You're welcome.
…On Mon, Feb 14, 2022 at 1:16 AM sdxingaijing ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Thanks so much.
非常感谢!
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/265e026faf99cbc93cb8088b86c5dc13#gistcomment-4064463>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ARY7RMKB2DQDLKLA4DEBADTU3CT5PANCNFSM5GEMUA3A>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
--
Randy Runtsch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment