How can I receive CSV files from API Code?
Question:
I have a df dataset with latitudes and longitudes columns like this.
station
long
lat
Station 1
50.80
60.80
Station 2
45
47
API: https://power.larc.nasa.gov/docs/tutorials/service-data-request/api/
This API provides some parameters (PS,WS10M_RANGE) for coordinate (latitute and longitute) points. I want to run this api code for all the latitute and longitute in my dataset. I already have a code for the json file, but I need to get the data in CSV format.
I then tried the following:
import os, json, requests
lat=df['lat'].values.tolist()
long=df['long'].values.tolist()
output = r"C:Users....."
base_url = r"https://power.larc.nasa.gov/api/temporal/monthly/point?parameters=PS,WS10M_RANGE&community=SB&longitude={longitude}&latitude={latitude}&format=CSV&start=2018&end=2021"
contents=[]
for latitude, longitude in zip(lat,long):
api_request_url = base_url.format(longitude=longitude, latitude=latitude)
response = requests.get(url=api_request_url, verify=True, timeout=30.00)
content=pd.read_csv(io.BytesIO(content), encoding='utf8', sep=",")
filename = response.headers['content-disposition'].split('filename=')[1]
filepath = os.path.join(output, filename)
with open(filepath, 'w') as file_object:
Answers:
The retrieved data from that API looks like this:
-BEGIN HEADER-
NASA/POWER CERES/MERRA2 Native Resolution Monthly and Annual
Dates (month/day/year): 01/01/2018 through 12/31/2021
Location: Latitude 60.8 Longitude 50.8
Elevation from MERRA-2: Average for 0.5 x 0.625 degree lat/lon region = 157.3 meters
The value for missing source data that cannot be computed or is outside of the sources availability range: -999
Parameter(s):
PS MERRA-2 Surface Pressure (kPa)
WS10M_RANGE MERRA-2 Wind Speed at 10 Meters Range (m/s)
-END HEADER-
PARAMETER,YEAR,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC,ANN
PS,2018,100.28,100.66,98.98,99.33,99.83,98.71,99.77,99.44,99.53,99.13,100.02,100.41,99.67
PS,2019,98.94,99.11,98.64,100.16,99.45,99.36,98.59,99.15,99.22,98.98,100.26,98.86,99.22
PS,2020,98.39,98.3,99.29,98.57,99.29,99.77,99.25,99.21,99.56,99.91,100.13,100.85,99.38
PS,2021,99.99,99.56,98.87,99.74,99.54,99.71,99.19,99.76,99.43,99.72,98.84,99.21,99.47
WS10M_RANGE,2018,6.22,3.59,5.1,5.45,5.06,3.72,3.09,4.02,5.02,5.29,4.57,3.3,6.7
WS10M_RANGE,2019,4.38,6.25,5.09,4.68,4.98,4.42,3.95,3.91,3.57,4.95,4.05,5.38,6.41
WS10M_RANGE,2020,5.05,6.76,5.43,5.12,4.27,3.91,3.96,3.7,4.3,3.8,4.85,3.87,6.91
WS10M_RANGE,2021,5.02,6.41,4.7,4.6,4.13,3.99,3.47,3.72,4.34,4.95,4.43,5.15,6.73
The csv format starts after -END HEADER-
, so you should get the data under the end of header like this:
response.content.decode("utf-8").split('-END HEADER-')[1].strip()
Full code:
import io, os, json, requests, pandas as pd
# df = pd.DataFrame({
# "lat":[32.929, 5],
# "long": [-95.770, 10]
# })
lat=df['lat'].values.tolist()
long=df['long'].values.tolist()
output = r"C:Users....."
base_url = r"https://power.larc.nasa.gov/api/temporal/monthly/point?parameters=PS,WS10M_RANGE&community=SB&longitude={longitude}&latitude={latitude}&format=CSV&start=2018&end=2021"
contents=[]
for latitude, longitude in zip(lat,long):
api_request_url = base_url.format(longitude=longitude, latitude=latitude)
response = requests.get(url=api_request_url, verify=True, timeout=30.00)
content = response.content.decode("utf-8").split('-END HEADER-')[1].strip()
content = pd.read_csv(io.StringIO(content), encoding='utf8', sep=",")
filename = response.headers['content-disposition'].split('filename=')[1]
filepath = os.path.join(output, filename)
with open(filepath, 'w') as file_object:
I have a df dataset with latitudes and longitudes columns like this.
station | long | lat |
---|---|---|
Station 1 | 50.80 | 60.80 |
Station 2 | 45 | 47 |
API: https://power.larc.nasa.gov/docs/tutorials/service-data-request/api/
This API provides some parameters (PS,WS10M_RANGE) for coordinate (latitute and longitute) points. I want to run this api code for all the latitute and longitute in my dataset. I already have a code for the json file, but I need to get the data in CSV format.
I then tried the following:
import os, json, requests
lat=df['lat'].values.tolist()
long=df['long'].values.tolist()
output = r"C:Users....."
base_url = r"https://power.larc.nasa.gov/api/temporal/monthly/point?parameters=PS,WS10M_RANGE&community=SB&longitude={longitude}&latitude={latitude}&format=CSV&start=2018&end=2021"
contents=[]
for latitude, longitude in zip(lat,long):
api_request_url = base_url.format(longitude=longitude, latitude=latitude)
response = requests.get(url=api_request_url, verify=True, timeout=30.00)
content=pd.read_csv(io.BytesIO(content), encoding='utf8', sep=",")
filename = response.headers['content-disposition'].split('filename=')[1]
filepath = os.path.join(output, filename)
with open(filepath, 'w') as file_object:
The retrieved data from that API looks like this:
-BEGIN HEADER-
NASA/POWER CERES/MERRA2 Native Resolution Monthly and Annual
Dates (month/day/year): 01/01/2018 through 12/31/2021
Location: Latitude 60.8 Longitude 50.8
Elevation from MERRA-2: Average for 0.5 x 0.625 degree lat/lon region = 157.3 meters
The value for missing source data that cannot be computed or is outside of the sources availability range: -999
Parameter(s):
PS MERRA-2 Surface Pressure (kPa)
WS10M_RANGE MERRA-2 Wind Speed at 10 Meters Range (m/s)
-END HEADER-
PARAMETER,YEAR,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC,ANN
PS,2018,100.28,100.66,98.98,99.33,99.83,98.71,99.77,99.44,99.53,99.13,100.02,100.41,99.67
PS,2019,98.94,99.11,98.64,100.16,99.45,99.36,98.59,99.15,99.22,98.98,100.26,98.86,99.22
PS,2020,98.39,98.3,99.29,98.57,99.29,99.77,99.25,99.21,99.56,99.91,100.13,100.85,99.38
PS,2021,99.99,99.56,98.87,99.74,99.54,99.71,99.19,99.76,99.43,99.72,98.84,99.21,99.47
WS10M_RANGE,2018,6.22,3.59,5.1,5.45,5.06,3.72,3.09,4.02,5.02,5.29,4.57,3.3,6.7
WS10M_RANGE,2019,4.38,6.25,5.09,4.68,4.98,4.42,3.95,3.91,3.57,4.95,4.05,5.38,6.41
WS10M_RANGE,2020,5.05,6.76,5.43,5.12,4.27,3.91,3.96,3.7,4.3,3.8,4.85,3.87,6.91
WS10M_RANGE,2021,5.02,6.41,4.7,4.6,4.13,3.99,3.47,3.72,4.34,4.95,4.43,5.15,6.73
The csv format starts after -END HEADER-
, so you should get the data under the end of header like this:
response.content.decode("utf-8").split('-END HEADER-')[1].strip()
Full code:
import io, os, json, requests, pandas as pd
# df = pd.DataFrame({
# "lat":[32.929, 5],
# "long": [-95.770, 10]
# })
lat=df['lat'].values.tolist()
long=df['long'].values.tolist()
output = r"C:Users....."
base_url = r"https://power.larc.nasa.gov/api/temporal/monthly/point?parameters=PS,WS10M_RANGE&community=SB&longitude={longitude}&latitude={latitude}&format=CSV&start=2018&end=2021"
contents=[]
for latitude, longitude in zip(lat,long):
api_request_url = base_url.format(longitude=longitude, latitude=latitude)
response = requests.get(url=api_request_url, verify=True, timeout=30.00)
content = response.content.decode("utf-8").split('-END HEADER-')[1].strip()
content = pd.read_csv(io.StringIO(content), encoding='utf8', sep=",")
filename = response.headers['content-disposition'].split('filename=')[1]
filepath = os.path.join(output, filename)
with open(filepath, 'w') as file_object: