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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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') |
I’m glad that it helped!
Randy
…
On Oct 16, 2021 at 11:03 PM, <Chris Graber ***@***.***)> wrote:
@christophergraber commented on this gist.
Super helpful! I was able to look at json weather data from a station just near my house in Tucson, AZ, thanks.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub (https://gist.github.com/265e026faf99cbc93cb8088b86c5dc13#gistcomment-3929785), or unsubscribe (https://github.com/notifications/unsubscribe-auth/ARY7RMK45TWLDF25BXSZAZDUHJKKDANCNFSM5GEMUA3A).
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).
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.
Hi Ilya,
I'm happy to hear that you find the code useful. The code is a companion to
the article linked below. I published it to the online publication called
Towards Data Science. It should give you enough information about finding
stations (out of thousands of possibilities) that will meet your needs.
https://towardsdatascience.com/acquire-and-analyze-weather-and-climate-data-with-python-and-tableau-4878e8759152?sk=a6e6e90d75ac66b484142a0310f99493
Kind regards,
Randy Runtsch
…On Thu, Dec 16, 2021 at 4:29 PM Ilya Jackson ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/265e026faf99cbc93cb8088b86c5dc13#gistcomment-3998662>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ARY7RMLCWRXWWNX6VJSUWQTURJR37ANCNFSM5GEMUA3A>
.
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
Thanks so much.
非常感谢!
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
Super helpful! I was able to look at json weather data from a station just near my house in Tucson, AZ, thanks.