Last active
February 9, 2021 05:27
-
-
Save zaneselvans/b2f5135e91b8dfcb14f198affbe265bd to your computer and use it in GitHub Desktop.
Pulling monthly total irradiance from the NREL NSRDB 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 os | |
import pandas as pd | |
import itertools | |
def get_nsrdb_data( | |
locations, | |
years, | |
attributes="ghi", | |
leap_day="true", | |
interval=60, | |
utc="true", | |
email="zane.selvans@catalyst.coop", | |
api_key=os.environ.get("API_KEY_NREL_NSRDB"), | |
): | |
out_df = pd.DataFrame() | |
for (location_id, lat, lon), year in itertools.product(locations, years): | |
print(f"Fetching {year} data for location {location_id}.") | |
url = ( | |
"https://developer.nrel.gov/api/solar/nsrdb_psm3_download.csv?" | |
f"wkt=POINT({lon}%20{lat})&" | |
f"names={year}&" | |
f"leap_day={leap_day}&" | |
f"interval={interval}&" | |
f"email={email}&" | |
f"api_key={api_key}&" | |
f"attributes={attributes}&" | |
) | |
# Skip the first two rows to avoid metadata | |
df = pd.read_csv(url, skiprows=2) | |
df["datetime_utc"] = pd.to_datetime( | |
dict( | |
year=df["Year"], | |
month=df["Month"], | |
day=df["Day"], | |
hour=df["Hour"], | |
minute=df["Minute"], | |
) | |
) | |
df = df.drop(["Year", "Month", "Day", "Hour", "Minute"], axis="columns") | |
df = df.set_index("datetime_utc") | |
df = df.resample("M").sum() | |
df = df.rename_axis(index="month_ending") | |
df["GHI"] = df["GHI"] / 1000 | |
df = df.rename({"GHI": "monthly_sum_ghi_kWh_per_m2"}, axis="columns") | |
df["location_id"] = location_id | |
df = df[["location_id", "monthly_sum_ghi_kWh_per_m2"]] | |
out_df = pd.concat([out_df, df]) | |
return out_df | |
test_locs = [ | |
("Boulder", 40.00, -105.30), | |
("Oaxaca de Juarez", 17.10, -96.70), | |
] | |
df_out = get_nsrdb_data( | |
locations=test_locs, | |
years=range(2018, 2020), | |
attrs="ghi", | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment