Skip to content

Commit 4e59fb6

Browse files
committed
Fixes
1 parent 083e126 commit 4e59fb6

File tree

4 files changed

+94
-91
lines changed

4 files changed

+94
-91
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,5 @@ __pycache__/
132132
*.bak
133133
.vscode/settings.json
134134
run_act_test.bat
135+
.DS_Store
136+
*.py~

nrel_psm3_2_epw/assets.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import json
1+
import calendar
22
import sys
3+
from datetime import datetime
34

45
import numpy as np
56
import pandas as pd
67
import requests
8+
79
from . import epw
8-
from datetime import datetime
9-
import calendar
1010

1111

1212
def download_epw(lon, lat, year, location, attributes, interval, utc, your_name, api_key, reason_for_use,
1313
your_affiliation, your_email, mailing_list, leap_year):
1414
currentYear = datetime.now().year
15-
if int(year) == currentYear or int(year) == currentYear-1:
15+
if int(year) == currentYear or int(year) == currentYear - 1:
1616
raise Exception("NREL does not provide data for the current year " + str(
1717
year) + ". It is also unlikely that there is data availability for " + str(int(year) - 1) + ".")
1818

@@ -42,8 +42,8 @@ def download_epw(lon, lat, year, location, attributes, interval, utc, your_name,
4242

4343
try:
4444
r = requests.request("GET", url, params=
45-
payload, headers=headers, timeout=20)
46-
45+
payload, headers=headers, timeout=20)
46+
4747
print(r.text)
4848
r.raise_for_status()
4949

@@ -66,7 +66,7 @@ def download_epw(lon, lat, year, location, attributes, interval, utc, your_name,
6666

6767
hours_per_year = 8760
6868

69-
if calendar.isleap(int(year)) and bool(leap_year) is True and all_data.shape[0] == 8784+2:
69+
if calendar.isleap(int(year)) and bool(leap_year) is True and all_data.shape[0] == 8784 + 2:
7070
hours_per_year = 8784
7171

7272
datetimes = pd.date_range('01/01/' + str(year),
@@ -310,7 +310,7 @@ def download_epw(lon, lat, year, location, attributes, interval, utc, your_name,
310310

311311
d = "_"
312312
file_name = str(location) + d + str(lat) + d + \
313-
str(lon) + d + str(year) + '.epw'
313+
str(lon) + d + str(year) + '.epw'
314314

315315
out.write(file_name)
316316
print("Success: File", file_name, "written")

nrel_psm3_2_epw/epw.py

+72-75
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
# -*- coding: utf-8 -*-
2-
import pandas as pd
32
import csv
43

4+
import pandas as pd
5+
6+
57
class EPW():
68
"""A class which represents an EnergyPlus weather (epw) file
79
"""
8-
10+
911
def __init__(self):
1012
"""
1113
"""
12-
self.headers={}
13-
self.dataframe=pd.DataFrame()
14-
15-
16-
def read(self,fp):
14+
self.headers = {}
15+
self.dataframe = pd.DataFrame()
16+
17+
def read(self, fp):
1718
"""Reads an epw file
1819
1920
Arguments:
2021
- fp (str): the file path of the epw file
2122
2223
"""
23-
24-
self.headers=self._read_headers(fp)
25-
self.dataframe=self._read_data(fp)
26-
27-
28-
def _read_headers(self,fp):
24+
25+
self.headers = self._read_headers(fp)
26+
self.dataframe = self._read_data(fp)
27+
28+
def _read_headers(self, fp):
2929
"""Reads the headers of an epw file
3030
3131
Arguments:
@@ -35,19 +35,18 @@ def _read_headers(self,fp):
3535
- d (dict): a dictionary containing the header rows
3636
3737
"""
38-
39-
d={}
38+
39+
d = {}
4040
with open(fp, newline='') as csvfile:
4141
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
4242
for row in csvreader:
4343
if row[0].isdigit():
4444
break
4545
else:
46-
d[row[0]]=row[1:]
46+
d[row[0]] = row[1:]
4747
return d
48-
49-
50-
def _read_data(self,fp):
48+
49+
def _read_data(self, fp):
5150
"""Reads the climate data of an epw file
5251
5352
Arguments:
@@ -57,52 +56,51 @@ def _read_data(self,fp):
5756
- df (pd.DataFrame): a DataFrame comtaining the climate data
5857
5958
"""
60-
61-
names=['Year',
62-
'Month',
63-
'Day',
64-
'Hour',
65-
'Minute',
66-
'Data Source and Uncertainty Flags',
67-
'Dry Bulb Temperature',
68-
'Dew Point Temperature',
69-
'Relative Humidity',
70-
'Atmospheric Station Pressure',
71-
'Extraterrestrial Horizontal Radiation',
72-
'Extraterrestrial Direct Normal Radiation',
73-
'Horizontal Infrared Radiation Intensity',
74-
'Global Horizontal Radiation',
75-
'Direct Normal Radiation',
76-
'Diffuse Horizontal Radiation',
77-
'Global Horizontal Illuminance',
78-
'Direct Normal Illuminance',
79-
'Diffuse Horizontal Illuminance',
80-
'Zenith Luminance',
81-
'Wind Direction',
82-
'Wind Speed',
83-
'Total Sky Cover',
84-
'Opaque Sky Cover (used if Horizontal IR Intensity missing)',
85-
'Visibility',
86-
'Ceiling Height',
87-
'Present Weather Observation',
88-
'Present Weather Codes',
89-
'Precipitable Water',
90-
'Aerosol Optical Depth',
91-
'Snow Depth',
92-
'Days Since Last Snowfall',
93-
'Albedo',
94-
'Liquid Precipitation Depth',
95-
'Liquid Precipitation Quantity']
96-
97-
first_row=self._first_row_with_climate_data(fp)
98-
df=pd.read_csv(fp,
99-
skiprows=first_row,
100-
header=None,
101-
names=names)
59+
60+
names = ['Year',
61+
'Month',
62+
'Day',
63+
'Hour',
64+
'Minute',
65+
'Data Source and Uncertainty Flags',
66+
'Dry Bulb Temperature',
67+
'Dew Point Temperature',
68+
'Relative Humidity',
69+
'Atmospheric Station Pressure',
70+
'Extraterrestrial Horizontal Radiation',
71+
'Extraterrestrial Direct Normal Radiation',
72+
'Horizontal Infrared Radiation Intensity',
73+
'Global Horizontal Radiation',
74+
'Direct Normal Radiation',
75+
'Diffuse Horizontal Radiation',
76+
'Global Horizontal Illuminance',
77+
'Direct Normal Illuminance',
78+
'Diffuse Horizontal Illuminance',
79+
'Zenith Luminance',
80+
'Wind Direction',
81+
'Wind Speed',
82+
'Total Sky Cover',
83+
'Opaque Sky Cover (used if Horizontal IR Intensity missing)',
84+
'Visibility',
85+
'Ceiling Height',
86+
'Present Weather Observation',
87+
'Present Weather Codes',
88+
'Precipitable Water',
89+
'Aerosol Optical Depth',
90+
'Snow Depth',
91+
'Days Since Last Snowfall',
92+
'Albedo',
93+
'Liquid Precipitation Depth',
94+
'Liquid Precipitation Quantity']
95+
96+
first_row = self._first_row_with_climate_data(fp)
97+
df = pd.read_csv(fp,
98+
skiprows=first_row,
99+
header=None,
100+
names=names)
102101
return df
103-
104-
105-
def _first_row_with_climate_data(self,fp):
102+
103+
def _first_row_with_climate_data(self, fp):
106104
"""Finds the first row with the climate data of an epw file
107105
108106
Arguments:
@@ -112,27 +110,26 @@ def _first_row_with_climate_data(self,fp):
112110
- i (int): the row number
113111
114112
"""
115-
113+
116114
with open(fp, newline='') as csvfile:
117115
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
118-
for i,row in enumerate(csvreader):
116+
for i, row in enumerate(csvreader):
119117
if row[0].isdigit():
120118
break
121119
return i
122-
123-
124-
def write(self,fp):
120+
121+
def write(self, fp):
125122
"""Writes an epw file
126123
127124
Arguments:
128125
- fp (str): the file path of the new epw file
129126
130127
"""
131-
128+
132129
with open(fp, 'w', newline='') as csvfile:
133130
csvwriter = csv.writer(csvfile, delimiter=',',
134-
quotechar='"', quoting=csv.QUOTE_MINIMAL)
135-
for k,v in self.headers.items():
136-
csvwriter.writerow([k]+v)
137-
for row in self.dataframe.itertuples(index= False):
138-
csvwriter.writerow(i for i in row)
131+
quotechar='"', quoting=csv.QUOTE_MINIMAL)
132+
for k, v in self.headers.items():
133+
csvwriter.writerow([k] + v)
134+
for row in self.dataframe.itertuples(index=False):
135+
csvwriter.writerow(i for i in row)

tests/test_download.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from nrel_psm3_2_epw.assets import *
2-
from pathlib import Path
31
import os
2+
from pathlib import Path
3+
4+
from nrel_psm3_2_epw.assets import *
45

56

67
def test_download_epw():
7-
88
lat, lon = 40.755840, -73.982684
99
location = "NYC"
1010
year = '2012'
1111
attributes = 'air_temperature,clearsky_dhi,clearsky_dni,clearsky_ghi,cloud_type,dew_point,dhi,dni,fill_flag,ghi,' \
12-
'relative_humidity,solar_zenith_angle,surface_albedo,surface_pressure,total_precipitable_water,' \
13-
'wind_direction,wind_speed,ghuv-280-400,ghuv-295-385'
12+
'relative_humidity,solar_zenith_angle,surface_albedo,surface_pressure,total_precipitable_water,' \
13+
'wind_direction,wind_speed,ghuv-280-400,ghuv-295-385'
1414

1515
interval = '60'
1616
utc = 'false'
@@ -19,22 +19,26 @@ def test_download_epw():
1919
cwd = Path.cwd()
2020
api_key_file_path = cwd / Path("api_key")
2121

22+
api_key = None
23+
2224
if api_key_file_path.is_file():
2325
api_key_file = open(api_key_file_path, 'r')
2426
api_key = api_key_file.readline().rstrip()
2527
else:
2628
api_key = os.environ['APIKEY']
2729

30+
print(api_key)
31+
2832
reason_for_use = 'beta+testing'
2933
your_affiliation = 'aaa'
3034
your_email = "[email protected]"
3135
mailing_list = 'false'
3236
leap_year = 'true'
3337

34-
download_epw(lat, lon, year, location, attributes, interval, utc, your_name, api_key, reason_for_use, your_affiliation,
35-
your_email, mailing_list, leap_year)
38+
download_epw(lon, lat, int(year), location, attributes, interval, utc, your_name,
39+
api_key, reason_for_use, your_affiliation, your_email, mailing_list, leap_year)
3640

3741
data = epw.EPW()
3842
data.read("NYC_40.75584_-73.982684_2012.epw")
3943

40-
assert(data.dataframe['Year'][0] == 2012)
44+
assert (data.dataframe['Year'][0] == 2012)

0 commit comments

Comments
 (0)