|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import datetime
|
6 |
| -import io |
7 | 6 | import re
|
8 |
| -from urllib.request import urlopen, Request |
9 | 7 | import pandas as pd
|
10 | 8 |
|
11 | 9 |
|
12 |
| -def read_tmy3(filename=None, coerce_year=None, recolumn=True): |
| 10 | +def read_tmy3(filename, coerce_year=None, recolumn=True): |
13 | 11 | '''
|
14 | 12 | Read a TMY3 file in to a pandas dataframe.
|
15 | 13 |
|
16 | 14 | Note that values contained in the metadata dictionary are unchanged
|
17 | 15 | from the TMY3 file (i.e. units are retained). In the case of any
|
18 |
| - discrepencies between this documentation and the TMY3 User's Manual |
| 16 | + discrepancies between this documentation and the TMY3 User's Manual |
19 | 17 | [1]_, the TMY3 User's Manual takes precedence.
|
20 | 18 |
|
21 | 19 | The TMY3 files were updated in Jan. 2015. This function requires the
|
22 | 20 | use of the updated files.
|
23 | 21 |
|
24 | 22 | Parameters
|
25 | 23 | ----------
|
26 |
| - filename : None or string, default None |
27 |
| - If None, attempts to use a Tkinter file browser. A string can be |
28 |
| - a relative file path, absolute file path, or url. |
| 24 | + filename : str |
| 25 | + A relative file path or absolute file path. |
29 | 26 |
|
30 | 27 | coerce_year : None or int, default None
|
31 | 28 | If supplied, the year of the index will be set to `coerce_year`, except
|
@@ -161,46 +158,25 @@ def read_tmy3(filename=None, coerce_year=None, recolumn=True):
|
161 | 158 | Update: Users Manual. 472 pp.; NREL Report No. TP-581-41364.
|
162 | 159 | '''
|
163 | 160 |
|
164 |
| - if filename is None: |
165 |
| - try: |
166 |
| - filename = _interactive_load() |
167 |
| - except ImportError: |
168 |
| - raise ImportError('Interactive load failed. tkinter not supported ' |
169 |
| - 'on this system. Try installing X-Quartz and ' |
170 |
| - 'reloading') |
171 |
| - |
172 | 161 | head = ['USAF', 'Name', 'State', 'TZ', 'latitude', 'longitude', 'altitude']
|
173 | 162 |
|
174 |
| - if str(filename).startswith('http'): |
175 |
| - request = Request(filename, headers={'User-Agent': ( |
176 |
| - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) ' |
177 |
| - 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 ' |
178 |
| - 'Safari/537.36')}) |
179 |
| - response = urlopen(request) |
180 |
| - csvdata = io.StringIO(response.read().decode(errors='ignore')) |
181 |
| - else: |
182 |
| - # assume it's accessible via the file system |
183 |
| - csvdata = open(str(filename), 'r') |
184 |
| - |
185 |
| - # read in file metadata, advance buffer to second line |
186 |
| - firstline = csvdata.readline() |
187 |
| - if 'Request Rejected' in firstline: |
188 |
| - raise IOError('Remote server rejected TMY file request') |
| 163 | + with open(str(filename), 'r') as csvdata: |
| 164 | + # read in file metadata, advance buffer to second line |
| 165 | + firstline = csvdata.readline() |
| 166 | + # use pandas to read the csv file buffer |
| 167 | + # header is actually the second line, but tell pandas to look for |
| 168 | + # header information on the 1st line (0 indexing) because we've already |
| 169 | + # advanced past the true first line with the readline call above. |
| 170 | + data = pd.read_csv(csvdata, header=0) |
189 | 171 |
|
190 | 172 | meta = dict(zip(head, firstline.rstrip('\n').split(",")))
|
191 |
| - |
192 | 173 | # convert metadata strings to numeric types
|
193 | 174 | meta['altitude'] = float(meta['altitude'])
|
194 | 175 | meta['latitude'] = float(meta['latitude'])
|
195 | 176 | meta['longitude'] = float(meta['longitude'])
|
196 | 177 | meta['TZ'] = float(meta['TZ'])
|
197 | 178 | meta['USAF'] = int(meta['USAF'])
|
198 | 179 |
|
199 |
| - # use pandas to read the csv file/stringio buffer |
200 |
| - # header is actually the second line in file, but tell pandas to look for |
201 |
| - # header information on the 1st line (0 indexing) because we've already |
202 |
| - # advanced past the true first line with the readline call above. |
203 |
| - data = pd.read_csv(csvdata, header=0) |
204 | 180 | # get the date column as a pd.Series of numpy datetime64
|
205 | 181 | data_ymd = pd.to_datetime(data['Date (MM/DD/YYYY)'], format='%m/%d/%Y')
|
206 | 182 | # shift the time column so that midnite is 00:00 instead of 24:00
|
@@ -231,13 +207,6 @@ def read_tmy3(filename=None, coerce_year=None, recolumn=True):
|
231 | 207 | return data, meta
|
232 | 208 |
|
233 | 209 |
|
234 |
| -def _interactive_load(): |
235 |
| - import tkinter |
236 |
| - from tkinter.filedialog import askopenfilename |
237 |
| - tkinter.Tk().withdraw() # Start interactive file input |
238 |
| - return askopenfilename() |
239 |
| - |
240 |
| - |
241 | 210 | def _recolumn(tmy3_dataframe):
|
242 | 211 | """
|
243 | 212 | Rename the columns of the TMY3 DataFrame.
|
@@ -295,9 +264,8 @@ def read_tmy2(filename):
|
295 | 264 |
|
296 | 265 | Parameters
|
297 | 266 | ----------
|
298 |
| - filename : None or string |
299 |
| - If None, attempts to use a Tkinter file browser. A string can be |
300 |
| - a relative file path, absolute file path, or url. |
| 267 | + filename : str |
| 268 | + A relative or absolute file path. |
301 | 269 |
|
302 | 270 | Returns
|
303 | 271 | -------
|
@@ -412,14 +380,6 @@ def read_tmy2(filename):
|
412 | 380 | for TMY2s". NREL 1995.
|
413 | 381 | '''
|
414 | 382 |
|
415 |
| - if filename is None: |
416 |
| - try: |
417 |
| - filename = _interactive_load() |
418 |
| - except ImportError: |
419 |
| - raise ImportError('Interactive load failed. tkinter not supported ' |
420 |
| - 'on this system. Try installing X-Quartz and ' |
421 |
| - 'reloading') |
422 |
| - |
423 | 383 | # paste in the column info as one long line
|
424 | 384 | string = '%2d%2d%2d%2d%4d%4d%4d%1s%1d%4d%1s%1d%4d%1s%1d%4d%1s%1d%4d%1s%1d%4d%1s%1d%4d%1s%1d%2d%1s%1d%2d%1s%1d%4d%1s%1d%4d%1s%1d%3d%1s%1d%4d%1s%1d%3d%1s%1d%3d%1s%1d%4d%1s%1d%5d%1s%1d%10d%3d%1s%1d%3d%1s%1d%3d%1s%1d%2d%1s%1d' # noqa: E501
|
425 | 385 | columns = 'year,month,day,hour,ETR,ETRN,GHI,GHISource,GHIUncertainty,DNI,DNISource,DNIUncertainty,DHI,DHISource,DHIUncertainty,GHillum,GHillumSource,GHillumUncertainty,DNillum,DNillumSource,DNillumUncertainty,DHillum,DHillumSource,DHillumUncertainty,Zenithlum,ZenithlumSource,ZenithlumUncertainty,TotCld,TotCldSource,TotCldUncertainty,OpqCld,OpqCldSource,OpqCldUncertainty,DryBulb,DryBulbSource,DryBulbUncertainty,DewPoint,DewPointSource,DewPointUncertainty,RHum,RHumSource,RHumUncertainty,Pressure,PressureSource,PressureUncertainty,Wdir,WdirSource,WdirUncertainty,Wspd,WspdSource,WspdUncertainty,Hvis,HvisSource,HvisUncertainty,CeilHgt,CeilHgtSource,CeilHgtUncertainty,PresentWeather,Pwat,PwatSource,PwatUncertainty,AOD,AODSource,AODUncertainty,SnowDepth,SnowDepthSource,SnowDepthUncertainty,LastSnowfall,LastSnowfallSource,LastSnowfallUncertaint' # noqa: E501
|
|
0 commit comments