From 49a9d24c9ac0d4c4ae9b595c847d8240494afe76 Mon Sep 17 00:00:00 2001 From: Sarthak Jariwala Date: Sun, 7 Mar 2021 18:04:19 -0800 Subject: [PATCH] fixes #1 --- 04_solar.nsrdb.ipynb | 397 +++++++++++++++++++++--------------- docs/solar.nsrdb.html | 330 +++++++++++++++++++----------- nrel_dev_api/_nbdev.py | 3 +- nrel_dev_api/solar/nsrdb.py | 15 +- 4 files changed, 452 insertions(+), 293 deletions(-) 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", - " 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", " \n", " \n", " \n", " \n", - " 2017-01-01 00:00:00\n", - " 2017\n", - " 1\n", - " 1\n", + " 2016-01-01 00:00:00\n", + " -1\n", + " 36\n", + " 1010\n", + " 0.9\n", + " 0.430\n", + " \n", + " \n", + " 2016-01-01 00:30:00\n", + " -1\n", + " 0\n", + " 1010\n", + " 0.8\n", + " 0.423\n", + " \n", + " \n", + " 2016-01-01 01:00:00\n", + " -1\n", " 0\n", + " 1010\n", + " 0.8\n", + " 0.416\n", + " \n", + " \n", + " 2016-01-01 01:30:00\n", + " -1\n", " 0\n", + " 1010\n", + " 0.7\n", + " 0.411\n", + " \n", + " \n", + " 2016-01-01 02:00:00\n", + " -2\n", " 0\n", - " 20\n", - " 233\n", - " 32\n", - " 4\n", + " 1010\n", + " 0.7\n", + " 0.406\n", + " \n", + " \n", + " ...\n", + " ...\n", + " ...\n", + " ...\n", + " ...\n", " ...\n", - " 32\n", - " 100.0\n", - " 87.08\n", - " 0.866\n", + " \n", + " \n", + " 2016-12-31 21:30:00\n", + " 1\n", + " 272\n", " 990\n", - " 1.020\n", - " 188.2\n", " 1.2\n", - " 0.0\n", - " 0.0\n", + " 1.062\n", " \n", " \n", - " 2017-01-01 00:30:00\n", - " 2017\n", + " 2016-12-31 22:00:00\n", " 1\n", - " 1\n", - " 0\n", - " 30\n", - " 0\n", - " 0\n", - " 0\n", - " 0\n", - " 8\n", - " ...\n", - " 0\n", - " 100.0\n", - " 91.13\n", - " 0.866\n", + " 237\n", " 990\n", - " 0.984\n", - " 188.2\n", - " 1.1\n", - " 0.0\n", - " 0.0\n", + " 1.3\n", + " 1.090\n", " \n", " \n", - " 2017-01-01 01:00:00\n", - " 2017\n", - " 1\n", + " 2016-12-31 22:30:00\n", " 1\n", - " 1\n", - " 0\n", - " 0\n", - " 0\n", - " 0\n", - " 0\n", - " 8\n", - " ...\n", - " 0\n", - " 100.0\n", - " 95.45\n", - " 0.866\n", + " 192\n", " 990\n", - " 0.949\n", - " 208.3\n", - " 1.0\n", - " 0.0\n", - " 0.0\n", + " 1.3\n", + " 1.082\n", " \n", " \n", - " 2017-01-01 01:30:00\n", - " 2017\n", - " 1\n", - " 1\n", + " 2016-12-31 23:00:00\n", " 1\n", - " 30\n", - " 0\n", - " 0\n", - " 0\n", - " 0\n", - " 4\n", - " ...\n", - " 0\n", - " 100.0\n", - " 100.00\n", - " 0.866\n", + " 140\n", " 990\n", - " 0.911\n", - " 208.3\n", - " 0.9\n", - " 0.0\n", - " 0.0\n", + " 1.3\n", + " 1.075\n", " \n", " \n", - " 2017-01-01 02:00:00\n", - " 2017\n", - " 1\n", + " 2016-12-31 23:30:00\n", " 1\n", - " 2\n", - " 0\n", - " 0\n", - " 0\n", - " 0\n", - " 0\n", - " 4\n", - " ...\n", - " 0\n", - " 99.9\n", - " 104.72\n", - " 0.866\n", + " 84\n", " 990\n", - " 0.874\n", - " 228.4\n", - " 0.9\n", - " 0.0\n", - " 0.0\n", + " 1.3\n", + " 1.075\n", " \n", " \n", "\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", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
YearMonthDayMinute
2016-02-29 21:30:00201622930
2016-02-29 22:00:0020162290
2016-02-29 22:30:00201622930
2016-02-29 23:00:0020162290
2016-02-29 23:30:00201622930
\n", + "
" + ], + "text/plain": [ + " Year Month Day Minute\n", + "2016-02-29 21:30:00 2016 2 29 30\n", + "2016-02-29 22:00:00 2016 2 29 0\n", + "2016-02-29 22:30:00 2016 2 29 30\n", + "2016-02-29 23:00:00 2016 2 29 0\n", + "2016-02-29 23:30:00 2016 2 29 30" ] }, "execution_count": null, @@ -915,7 +973,8 @@ } ], "source": [ - "df.head()" + "feb = df.loc[\"2016-02\"][[\"Year\", \"Month\", \"Day\", \"Minute\"]]\n", + "feb.tail()" ] }, { diff --git a/docs/solar.nsrdb.html b/docs/solar.nsrdb.html index fdf7c59..db526e9 100644 --- a/docs/solar.nsrdb.html +++ b/docs/solar.nsrdb.html @@ -335,7 +335,7 @@