Created
March 31, 2022 09:49
-
-
Save rruntsch/e21f0f29bd9e49c9c7f01f127d89c650 to your computer and use it in GitHub Desktop.
Demonstration Python program to request agricultural data with the USDA's Quick Stats 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
# Name: c_usda_quick_stats.py | |
# Author: Randy Runtsch | |
# Date: March 29, 2022 | |
# Project: Query USDA QuickStats API | |
# Author: Randall P. Runtsch | |
# | |
# Description: Query the USDA QuickStats api_GET API with a specified set of | |
# parameters. Write the retrieved data, in CSV format, to a file. | |
# | |
# See Quick Stats (NASS) API user guide: https://quickstats.nass.usda.gov/api | |
# Request a QuickStats API key here: https://quickstats.nass.usda.gov/api#param_define | |
# | |
# Attribution: This product uses the NASS API but is not endorsed or certified by NASS. | |
# | |
# Changes | |
# | |
import urllib.request | |
from requests.utils import requote_uri | |
class c_usda_quick_stats: | |
def __init__(self): | |
# Set the USDA QuickStats API key, API base URL, and output file path where CSV files will be written. | |
self.api_key = 'PASTE_YOUR_API_KEY_HERE' | |
self.base_url_api_get = 'http://quickstats.nass.usda.gov/api/api_GET/?key=' + self.api_key + '&' | |
self.output_file_path = r'c:\\usda_quickstats_files\\' | |
def get_data(self, parameters, file_name): | |
# Call the api_GET api with the specified parameters. | |
# Write the CSV data to the specified output file. | |
# Create the full URL and retrieve the data from the Quick Stats server. | |
full_url = self.base_url_api_get + parameters | |
s_result = urllib.request.urlopen(full_url) | |
s_text = s_result.read().decode('utf-8') | |
# Create the output file and write the CSV data records to the file. | |
s_file_name = self.output_file_path + file_name + ".csv" | |
o_file = open(s_file_name, "w", encoding="utf8") | |
o_file.write(s_text) | |
o_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
# Date: March 29, 2022 | |
# Project: Program controller to query USDA QuickStats API | |
# Author: Randall P. Runtsch | |
# | |
# Description: Create an instance of the c_usda_quick_stats class. Call it with | |
# the desired search parameter and output file name. | |
# | |
# Attribution: This product uses the NASS API but is not endorsed or certified by NASS. | |
# | |
# Changes | |
# | |
from c_usda_quick_stats import c_usda_quick_stats | |
import urllib.parse | |
# Create an instance of the c_usda_quick_stats class. Call it with search parameters | |
# and the output file to write the returned CSV data into. | |
parameters = 'source_desc=SURVEY' + \ | |
'&' + urllib.parse.quote('sector_desc=FARMS & LANDS & ASSETS') + \ | |
'&' + urllib.parse.quote('commodity_desc=FARM OPERATIONS') + \ | |
'&' + urllib.parse.quote('statisticcat_desc=AREA OPERATED') + \ | |
'&unit_desc=ACRES' + \ | |
'&freq_desc=ANNUAL' + \ | |
'&reference_period_desc=YEAR' + \ | |
'&year__GE=1997' + \ | |
'&agg_level_desc=NATIONAL' + \ | |
'&' + urllib.parse.quote('state_name=US TOTAL') + \ | |
'&format=CSV' | |
stats = c_usda_quick_stats() | |
s_json = stats.get_data(parameters, 'national_farm_survey_acres_ge_1997') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment