|
1 | 1 | """Functions to read data from the NOAA SOLRAD network.""" |
2 | 2 |
|
3 | 3 | import pandas as pd |
| 4 | +import warnings |
4 | 5 | import requests |
5 | 6 | import io |
6 | 7 |
|
@@ -72,6 +73,10 @@ def read_solrad(filename): |
72 | 73 | metadata : dict |
73 | 74 | Metadata. |
74 | 75 |
|
| 76 | + See Also |
| 77 | + -------- |
| 78 | + get_solrad |
| 79 | +
|
75 | 80 | Notes |
76 | 81 | ----- |
77 | 82 | SOLRAD data resolution is described by the README_SOLRAD.txt: |
@@ -104,6 +109,7 @@ def read_solrad(filename): |
104 | 109 |
|
105 | 110 | if str(filename).startswith('ftp') or str(filename).startswith('http'): |
106 | 111 | response = requests.get(filename) |
| 112 | + response.raise_for_status() |
107 | 113 | file_buffer = io.StringIO(response.content.decode()) |
108 | 114 | else: |
109 | 115 | with open(str(filename), 'r') as file_buffer: |
@@ -135,3 +141,81 @@ def read_solrad(filename): |
135 | 141 | data = data.set_index(dtindex) |
136 | 142 |
|
137 | 143 | return data, meta |
| 144 | + |
| 145 | + |
| 146 | +def get_solrad(station, start, end, |
| 147 | + url="https://gml.noaa.gov/aftp/data/radiation/solrad/"): |
| 148 | + """Request data from NOAA SOLRAD and read it into a Dataframe. |
| 149 | +
|
| 150 | + A list of stations and their descriptions can be found in [1]_, |
| 151 | + The data files are described in [2]_. |
| 152 | +
|
| 153 | + Data is returned for complete days, including ``start`` and ``end``. |
| 154 | +
|
| 155 | + Parameters |
| 156 | + ---------- |
| 157 | + station : str |
| 158 | + Three letter station abbreviation. |
| 159 | + start : datetime-like |
| 160 | + First day of the requested period |
| 161 | + end : datetime-like |
| 162 | + Last day of the requested period |
| 163 | + url : str, default: 'https://gml.noaa.gov/aftp/data/radiation/solrad/' |
| 164 | + API endpoint URL |
| 165 | +
|
| 166 | + Returns |
| 167 | + ------- |
| 168 | + data : pd.DataFrame |
| 169 | + Dataframe with data from SOLRAD. |
| 170 | + meta : dict |
| 171 | + Metadata. |
| 172 | +
|
| 173 | + See Also |
| 174 | + -------- |
| 175 | + read_solrad |
| 176 | +
|
| 177 | + Notes |
| 178 | + ----- |
| 179 | + Recent SOLRAD data is 1-minute averages. Prior to 2015-01-01, it was |
| 180 | + 3-minute averages. |
| 181 | +
|
| 182 | + References |
| 183 | + ---------- |
| 184 | + .. [1] https://gml.noaa.gov/grad/solrad/index.html |
| 185 | + .. [2] https://gml.noaa.gov/aftp/data/radiation/solrad/README_SOLRAD.txt |
| 186 | +
|
| 187 | + Examples |
| 188 | + -------- |
| 189 | + >>> # Retrieve one month of irradiance data from the ABQ SOLRAD station |
| 190 | + >>> data, metadata = pvlib.iotools.get_solrad( |
| 191 | + >>> station='abq', start="2020-01-01", end="2020-01-31") |
| 192 | + """ |
| 193 | + # Use pd.to_datetime so that strings (e.g. '2021-01-01') are accepted |
| 194 | + start = pd.to_datetime(start) |
| 195 | + end = pd.to_datetime(end) |
| 196 | + |
| 197 | + # Generate list of filenames |
| 198 | + dates = pd.date_range(start.floor('d'), end, freq='d') |
| 199 | + station = station.lower() |
| 200 | + filenames = [ |
| 201 | + f"{station}/{d.year}/{station}{d.strftime('%y')}{d.dayofyear:03}.dat" |
| 202 | + for d in dates |
| 203 | + ] |
| 204 | + |
| 205 | + dfs = [] # Initialize list of monthly dataframes |
| 206 | + for f in filenames: |
| 207 | + try: |
| 208 | + dfi, file_metadata = read_solrad(url + f) |
| 209 | + dfs.append(dfi) |
| 210 | + except requests.exceptions.HTTPError: |
| 211 | + warnings.warn(f"The following file was not found: {f}") |
| 212 | + |
| 213 | + data = pd.concat(dfs, axis='rows') |
| 214 | + |
| 215 | + meta = {'station': station, |
| 216 | + 'filenames': filenames, |
| 217 | + # all file should have the same metadata, so just merge in the |
| 218 | + # metadata from the last file |
| 219 | + **file_metadata} |
| 220 | + |
| 221 | + return data, meta |
0 commit comments