1
1
# -*- coding: utf-8 -*-
2
- import pandas as pd
3
2
import csv
4
3
4
+ import pandas as pd
5
+
6
+
5
7
class EPW ():
6
8
"""A class which represents an EnergyPlus weather (epw) file
7
9
"""
8
-
10
+
9
11
def __init__ (self ):
10
12
"""
11
13
"""
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 ):
17
18
"""Reads an epw file
18
19
19
20
Arguments:
20
21
- fp (str): the file path of the epw file
21
22
22
23
"""
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 ):
29
29
"""Reads the headers of an epw file
30
30
31
31
Arguments:
@@ -35,19 +35,18 @@ def _read_headers(self,fp):
35
35
- d (dict): a dictionary containing the header rows
36
36
37
37
"""
38
-
39
- d = {}
38
+
39
+ d = {}
40
40
with open (fp , newline = '' ) as csvfile :
41
41
csvreader = csv .reader (csvfile , delimiter = ',' , quotechar = '"' )
42
42
for row in csvreader :
43
43
if row [0 ].isdigit ():
44
44
break
45
45
else :
46
- d [row [0 ]]= row [1 :]
46
+ d [row [0 ]] = row [1 :]
47
47
return d
48
-
49
-
50
- def _read_data (self ,fp ):
48
+
49
+ def _read_data (self , fp ):
51
50
"""Reads the climate data of an epw file
52
51
53
52
Arguments:
@@ -57,52 +56,51 @@ def _read_data(self,fp):
57
56
- df (pd.DataFrame): a DataFrame comtaining the climate data
58
57
59
58
"""
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 )
102
101
return df
103
-
104
-
105
- def _first_row_with_climate_data (self ,fp ):
102
+
103
+ def _first_row_with_climate_data (self , fp ):
106
104
"""Finds the first row with the climate data of an epw file
107
105
108
106
Arguments:
@@ -112,27 +110,26 @@ def _first_row_with_climate_data(self,fp):
112
110
- i (int): the row number
113
111
114
112
"""
115
-
113
+
116
114
with open (fp , newline = '' ) as csvfile :
117
115
csvreader = csv .reader (csvfile , delimiter = ',' , quotechar = '"' )
118
- for i ,row in enumerate (csvreader ):
116
+ for i , row in enumerate (csvreader ):
119
117
if row [0 ].isdigit ():
120
118
break
121
119
return i
122
-
123
-
124
- def write (self ,fp ):
120
+
121
+ def write (self , fp ):
125
122
"""Writes an epw file
126
123
127
124
Arguments:
128
125
- fp (str): the file path of the new epw file
129
126
130
127
"""
131
-
128
+
132
129
with open (fp , 'w' , newline = '' ) as csvfile :
133
130
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 )
0 commit comments