diff --git a/04_solar.nsrdb.ipynb b/04_solar.nsrdb.ipynb
index 8a701db..e2923c8 100644
--- a/04_solar.nsrdb.ipynb
+++ b/04_solar.nsrdb.ipynb
@@ -597,11 +597,8 @@
{
"data": {
"text/plain": [
- "['https://developer.nrel.gov/api/nsrdb/v2/solar/psm3-download.csv?names=2017&wkt=POINT%28-122.3+47.6%29&interval=30&api_key=yourapikey&email=youremail',\n",
- " 'https://developer.nrel.gov/api/nsrdb/v2/solar/psm3-download.csv?names=2017&wkt=POINT%28-122.3+47.6%29&interval=60&api_key=yourapikey&email=youremail',\n",
- " 'https://developer.nrel.gov/api/nsrdb/v2/solar/psm3-tmy-download.csv?names=tmy-2017&wkt=POINT%28-122.3+47.6%29&interval=60&api_key=yourapikey&email=youremail',\n",
- " 'https://developer.nrel.gov/api/nsrdb/v2/solar/psm3-tmy-download.csv?names=tdy-2017&wkt=POINT%28-122.3+47.6%29&interval=60&api_key=yourapikey&email=youremail',\n",
- " 'https://developer.nrel.gov/api/nsrdb/v2/solar/psm3-tmy-download.csv?names=tgy-2017&wkt=POINT%28-122.3+47.6%29&interval=60&api_key=yourapikey&email=youremail']"
+ "['https://developer.nrel.gov/api/nsrdb/v2/solar/psm3-download.csv?names=2016&wkt=POINT%28-122.3+47.6%29&interval=30&api_key=yourapikey&email=youremail',\n",
+ " 'https://developer.nrel.gov/api/nsrdb/v2/solar/psm3-download.csv?names=2016&wkt=POINT%28-122.3+47.6%29&interval=60&api_key=yourapikey&email=youremail']"
]
},
"execution_count": null,
@@ -610,7 +607,7 @@
}
],
"source": [
- "links = get_nsrdb_download_links(year=2017, lat=47.6, lon=-122.3)\n",
+ "links = get_nsrdb_download_links(year=2016, lat=47.6, lon=-122.3)\n",
"links"
]
},
@@ -624,6 +621,9 @@
"import pandas as pd\n",
"import warnings\n",
"\n",
+ "# these are strings because of easy comparisons made later\n",
+ "LEAP_YEARS = [\"1992\", \"1996\", \"2000\", \"2004\", \"2008\", \"2012\", \"2016\", \"2020\", \"2024\", \"2028\", \"2032\"]\n",
+ "\n",
"def download_nsrdb_data(\n",
" link,\n",
" email,\n",
@@ -631,6 +631,8 @@
"):\n",
" \"\"\"Download NSRDB data from the provided link and returns a pandas DataFrame.\"\"\"\n",
" \n",
+ " total_mins_in_year = 365 * 24 * 60\n",
+ " \n",
" if not isinstance(link, str):\n",
" raise ValueError(f\"Requires a str type. You provided {type(link)} type\")\n",
" \n",
@@ -648,14 +650,18 @@
" if i[0] == \"interval\":\n",
" interval = i[1]\n",
" if i[0] == \"names\":\n",
- " year = i[1] # this will also catch \"tmy-*\" names in years & and lead to errors in date_range\n",
+ " year = i[1][-4:] # this will catch only the year even from \"tmy-*\" names in years\n",
+ " \n",
+ " if year in LEAP_YEARS:\n",
+ " link = link + \"&leap_day=true\"\n",
+ " total_mins_in_year += (24 * 60) # add extra mins for the leap year\n",
" \n",
" df = pd.read_csv(link, skiprows=2)\n",
" \n",
" try:\n",
" # if this doesn't raise valuerror, then we can go ahead and set the new index\n",
" int(year)\n",
- " df = df.set_index(pd.date_range(f\"1/1/{year}\", freq=interval+'Min', periods=525600/int(interval)))\n",
+ " df = df.set_index(pd.date_range(f\"1/1/{year}\", freq=interval+'Min', periods=total_mins_in_year/int(interval)))\n",
" \n",
" except ValueError:\n",
" warnings.warn(\"Could not set the index to datetime; please do it manually\", UserWarning)\n",
@@ -681,6 +687,32 @@
"df = download_nsrdb_data(links[0], email=\"sarthakjariwala1@gmail.com\")"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "Index(['Year', 'Month', 'Day', 'Hour', 'Minute', 'Temperature', 'Clearsky DHI',\n",
+ " 'Clearsky DNI', 'Clearsky GHI', 'Cloud Type', 'Dew Point', 'DHI', 'DNI',\n",
+ " 'Fill Flag', 'GHI', 'Relative Humidity', 'Solar Zenith Angle',\n",
+ " 'Surface Albedo', 'Pressure', 'Precipitable Water', 'Wind Direction',\n",
+ " 'Wind Speed', 'Global Horizontal UV Irradiance (280-400nm)',\n",
+ " 'Global Horizontal UV Irradiance (295-385nm)'],\n",
+ " dtype='object')"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.columns"
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -707,206 +739,232 @@
" \n",
" \n",
" \n",
" \n",
" \n",
- " Year \n",
- " Month \n",
- " Day \n",
- " Hour \n",
- " Minute \n",
" Temperature \n",
- " Clearsky DHI \n",
- " Clearsky DNI \n",
- " Clearsky GHI \n",
- " Cloud Type \n",
- " ... \n",
" GHI \n",
- " Relative Humidity \n",
- " Solar Zenith Angle \n",
- " Surface Albedo \n",
" Pressure \n",
- " Precipitable Water \n",
- " Wind Direction \n",
" Wind Speed \n",
- " Global Horizontal UV Irradiance (280-400nm) \n",
- " Global Horizontal UV Irradiance (295-385nm) \n",
+ " Precipitable Water \n",
"
5 rows × 24 columns
\n", + "17568 rows × 5 columns
\n", "" ], "text/plain": [ - " Year Month Day Hour Minute Temperature \\\n", - "2017-01-01 00:00:00 2017 1 1 0 0 0 \n", - "2017-01-01 00:30:00 2017 1 1 0 30 0 \n", - "2017-01-01 01:00:00 2017 1 1 1 0 0 \n", - "2017-01-01 01:30:00 2017 1 1 1 30 0 \n", - "2017-01-01 02:00:00 2017 1 1 2 0 0 \n", + " Temperature GHI Pressure Wind Speed \\\n", + "2016-01-01 00:00:00 -1 36 1010 0.9 \n", + "2016-01-01 00:30:00 -1 0 1010 0.8 \n", + "2016-01-01 01:00:00 -1 0 1010 0.8 \n", + "2016-01-01 01:30:00 -1 0 1010 0.7 \n", + "2016-01-01 02:00:00 -2 0 1010 0.7 \n", + "... ... ... ... ... \n", + "2016-12-31 21:30:00 1 272 990 1.2 \n", + "2016-12-31 22:00:00 1 237 990 1.3 \n", + "2016-12-31 22:30:00 1 192 990 1.3 \n", + "2016-12-31 23:00:00 1 140 990 1.3 \n", + "2016-12-31 23:30:00 1 84 990 1.3 \n", "\n", - " Clearsky DHI Clearsky DNI Clearsky GHI Cloud Type \\\n", - "2017-01-01 00:00:00 20 233 32 4 \n", - "2017-01-01 00:30:00 0 0 0 8 \n", - "2017-01-01 01:00:00 0 0 0 8 \n", - "2017-01-01 01:30:00 0 0 0 4 \n", - "2017-01-01 02:00:00 0 0 0 4 \n", + " Precipitable Water \n", + "2016-01-01 00:00:00 0.430 \n", + "2016-01-01 00:30:00 0.423 \n", + "2016-01-01 01:00:00 0.416 \n", + "2016-01-01 01:30:00 0.411 \n", + "2016-01-01 02:00:00 0.406 \n", + "... ... \n", + "2016-12-31 21:30:00 1.062 \n", + "2016-12-31 22:00:00 1.090 \n", + "2016-12-31 22:30:00 1.082 \n", + "2016-12-31 23:00:00 1.075 \n", + "2016-12-31 23:30:00 1.075 \n", "\n", - " ... GHI Relative Humidity Solar Zenith Angle \\\n", - "2017-01-01 00:00:00 ... 32 100.0 87.08 \n", - "2017-01-01 00:30:00 ... 0 100.0 91.13 \n", - "2017-01-01 01:00:00 ... 0 100.0 95.45 \n", - "2017-01-01 01:30:00 ... 0 100.0 100.00 \n", - "2017-01-01 02:00:00 ... 0 99.9 104.72 \n", - "\n", - " Surface Albedo Pressure Precipitable Water \\\n", - "2017-01-01 00:00:00 0.866 990 1.020 \n", - "2017-01-01 00:30:00 0.866 990 0.984 \n", - "2017-01-01 01:00:00 0.866 990 0.949 \n", - "2017-01-01 01:30:00 0.866 990 0.911 \n", - "2017-01-01 02:00:00 0.866 990 0.874 \n", - "\n", - " Wind Direction Wind Speed \\\n", - "2017-01-01 00:00:00 188.2 1.2 \n", - "2017-01-01 00:30:00 188.2 1.1 \n", - "2017-01-01 01:00:00 208.3 1.0 \n", - "2017-01-01 01:30:00 208.3 0.9 \n", - "2017-01-01 02:00:00 228.4 0.9 \n", - "\n", - " Global Horizontal UV Irradiance (280-400nm) \\\n", - "2017-01-01 00:00:00 0.0 \n", - "2017-01-01 00:30:00 0.0 \n", - "2017-01-01 01:00:00 0.0 \n", - "2017-01-01 01:30:00 0.0 \n", - "2017-01-01 02:00:00 0.0 \n", + "[17568 rows x 5 columns]" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[[\"Temperature\", \"GHI\", \"Pressure\", \"Wind Speed\", \"Precipitable Water\"]]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The index in the `DataFrame` returned automatically accounts for the leap years." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " | Year | \n", + "Month | \n", + "Day | \n", + "Minute | \n", + "
---|---|---|---|---|
2016-02-29 21:30:00 | \n", + "2016 | \n", + "2 | \n", + "29 | \n", + "30 | \n", + "
2016-02-29 22:00:00 | \n", + "2016 | \n", + "2 | \n", + "29 | \n", + "0 | \n", + "
2016-02-29 22:30:00 | \n", + "2016 | \n", + "2 | \n", + "29 | \n", + "30 | \n", + "
2016-02-29 23:00:00 | \n", + "2016 | \n", + "2 | \n", + "29 | \n", + "0 | \n", + "
2016-02-29 23:30:00 | \n", + "2016 | \n", + "2 | \n", + "29 | \n", + "30 | \n", + "
get_nsrdb_download_li
-links = get_nsrdb_download_links(year=2017, lat=47.6, lon=-122.3)
+links = get_nsrdb_download_links(year=2016, lat=47.6, lon=-122.3)
links
@@ -351,11 +351,8 @@ get_nsrdb_download_li
@@ -377,7 +374,7 @@ get_nsrdb_download_li
+
+
+
+
+df[["Temperature", "GHI", "Pressure", "Wind Speed", "Precipitable Water"]]
@@ -462,153 +498,207 @@ download_nsrdb_data
- Year
- Month
- Day
- Hour
- Minute
Temperature
- Clearsky DHI
- Clearsky DNI
- Clearsky GHI
- Cloud Type
- ...
GHI
- Relative Humidity
- Solar Zenith Angle
- Surface Albedo
Pressure
- Precipitable Water
- Wind Direction
Wind Speed
- Global Horizontal UV Irradiance (280-400nm)
- Global Horizontal UV Irradiance (295-385nm)
+ Precipitable Water
- 2017-01-01 00:00:00
- 2017
- 1
- 1
+ 2016-01-01 00:00:00
+ -1
+ 36
+ 1010
+ 0.9
+ 0.430
+
+
+ 2016-01-01 00:30:00
+ -1
0
+ 1010
+ 0.8
+ 0.423
+
+
+ 2016-01-01 01:00:00
+ -1
0
+ 1010
+ 0.8
+ 0.416
+
+
+ 2016-01-01 01:30:00
+ -1
+ 0
+ 1010
+ 0.7
+ 0.411
+
+
+ 2016-01-01 02:00:00
+ -2
0
- 20
- 233
- 32
- 4
+ 1010
+ 0.7
+ 0.406
+
+
+ ...
+ ...
+ ...
+ ...
...
- 32
- 100.0
- 87.08
- 0.866
+ ...
+
+
+ 2016-12-31 21:30:00
+ 1
+ 272
990
- 1.020
- 188.2
1.2
- 0.0
- 0.0
+ 1.062
- 2017-01-01 00:30:00
- 2017
- 1
+ 2016-12-31 22:00:00
1
- 0
- 30
- 0
- 0
- 0
- 0
- 8
- ...
- 0
- 100.0
- 91.13
- 0.866
+ 237
990
- 0.984
- 188.2
- 1.1
- 0.0
- 0.0
+ 1.3
+ 1.090
- 2017-01-01 01:00:00
- 2017
- 1
+ 2016-12-31 22:30:00
1
- 1
- 0
- 0
- 0
- 0
- 0
- 8
- ...
- 0
- 100.0
- 95.45
- 0.866
+ 192
990
- 0.949
- 208.3
- 1.0
- 0.0
- 0.0
+ 1.3
+ 1.082
- 2017-01-01 01:30:00
- 2017
+ 2016-12-31 23:00:00
1
- 1
- 1
- 30
- 0
- 0
- 0
- 0
- 4
- ...
- 0
- 100.0
- 100.00
- 0.866
+ 140
990
- 0.911
- 208.3
- 0.9
- 0.0
- 0.0
+ 1.3
+ 1.075
- 2017-01-01 02:00:00
- 2017
- 1
+ 2016-12-31 23:30:00
1
+ 84
+ 990
+ 1.3
+ 1.075
+
+
+
+
17568 rows × 5 columns
+
+
+
+
+
+
+ {% endraw %}
+
+ {% raw %}
+
+
+
+
+
+ {% endraw %}
+
+
+
+The index in the DataFrame
returned automatically accounts for the leap years.
+
+
+
+
+ {% raw %}
+
+
+
+
+
+
+feb = df.loc["2016-02"][["Year", "Month", "Day", "Minute"]]
+feb.tail()
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/nrel_dev_api/_nbdev.py b/nrel_dev_api/_nbdev.py
index 1ff95cc..e389029 100644
--- a/nrel_dev_api/_nbdev.py
+++ b/nrel_dev_api/_nbdev.py
@@ -11,7 +11,8 @@
"PVWattsV6": "03_solar.pvwatts_v6.ipynb",
"NSRDB_DataQuery": "04_solar.nsrdb.ipynb",
"get_nsrdb_download_links": "04_solar.nsrdb.ipynb",
- "download_nsrdb_data": "04_solar.nsrdb.ipynb"}
+ "download_nsrdb_data": "04_solar.nsrdb.ipynb",
+ "LEAP_YEARS": "04_solar.nsrdb.ipynb"}
modules = ["_core.py",
"solar/resource_data.py",
diff --git a/nrel_dev_api/solar/nsrdb.py b/nrel_dev_api/solar/nsrdb.py
index 7f92764..2c0f90f 100644
--- a/nrel_dev_api/solar/nsrdb.py
+++ b/nrel_dev_api/solar/nsrdb.py
@@ -1,6 +1,6 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: 04_solar.nsrdb.ipynb (unless otherwise specified).
-__all__ = ['NSRDB_DataQuery', 'get_nsrdb_download_links', 'download_nsrdb_data']
+__all__ = ['NSRDB_DataQuery', 'get_nsrdb_download_links', 'download_nsrdb_data', 'LEAP_YEARS']
# Cell
from .._core import get_request, check_api_key, _API_KEY
@@ -129,6 +129,9 @@ def get_nsrdb_download_links(
import pandas as pd
import warnings
+# these are strings because of easy comparisons made later
+LEAP_YEARS = ["1992", "1996", "2000", "2004", "2008", "2012", "2016", "2020", "2024", "2028", "2032"]
+
def download_nsrdb_data(
link,
email,
@@ -136,6 +139,8 @@ def download_nsrdb_data(
):
"""Download NSRDB data from the provided link and returns a pandas DataFrame."""
+ total_mins_in_year = 365 * 24 * 60
+
if not isinstance(link, str):
raise ValueError(f"Requires a str type. You provided {type(link)} type")
@@ -153,14 +158,18 @@ def download_nsrdb_data(
if i[0] == "interval":
interval = i[1]
if i[0] == "names":
- year = i[1] # this will also catch "tmy-*" names in years & and lead to errors in date_range
+ year = i[1][-4:] # this will catch only the year even from "tmy-*" names in years
+
+ if year in LEAP_YEARS:
+ link = link + "&leap_day=true"
+ total_mins_in_year += (24 * 60) # add extra mins for the leap year
df = pd.read_csv(link, skiprows=2)
try:
# if this doesn't raise valuerror, then we can go ahead and set the new index
int(year)
- df = df.set_index(pd.date_range(f"1/1/{year}", freq=interval+'Min', periods=525600/int(interval)))
+ df = df.set_index(pd.date_range(f"1/1/{year}", freq=interval+'Min', periods=total_mins_in_year/int(interval)))
except ValueError:
warnings.warn("Could not set the index to datetime; please do it manually", UserWarning)