-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathepw.py
More file actions
144 lines (112 loc) · 3.99 KB
/
epw.py
File metadata and controls
144 lines (112 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import requests
import re
import pandas as pd
import time
import os
import csv
import numpy as np
class epw():
"""A class which represents an EnergyPlus weather (epw) file
https://github.com/building-energy/epw/blob/master/epw/epw.py
"""
def __init__(self):
"""
"""
self.headers={}
self.dataframe=pd.DataFrame()
def read(self,fp):
"""Reads an epw file
Arguments:
- fp (str): the file path of the epw file
"""
self.headers=self._read_headers(fp)
self.dataframe=self._read_data(fp)
def _read_headers(self,fp):
"""Reads the headers of an epw file
Arguments:
- fp (str): the file path of the epw file
Return value:
- d (dict): a dictionary containing the header rows
"""
d={}
with open(fp, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvreader:
if row[0].isdigit():
break
else:
d[row[0]]=row[1:]
return d
def _read_data(self,fp):
"""Reads the climate data of an epw file
Arguments:
- fp (str): the file path of the epw file
Return value:
- df (pd.DataFrame): a DataFrame comtaining the climate data
"""
names=['Year',
'Month',
'Day',
'Hour',
'Minute',
'Data Source and Uncertainty Flags',
'Dry Bulb Temperature',
'Dew Point Temperature',
'Relative Humidity',
'Atmospheric Station Pressure',
'Extraterrestrial Horizontal Radiation',
'Extraterrestrial Direct Normal Radiation',
'Horizontal Infrared Radiation Intensity',
'Global Horizontal Radiation',
'Direct Normal Radiation',
'Diffuse Horizontal Radiation',
'Global Horizontal Illuminance',
'Direct Normal Illuminance',
'Diffuse Horizontal Illuminance',
'Zenith Luminance',
'Wind Direction',
'Wind Speed',
'Total Sky Cover',
'Opaque Sky Cover (used if Horizontal IR Intensity missing)',
'Visibility',
'Ceiling Height',
'Present Weather Observation',
'Present Weather Codes',
'Precipitable Water',
'Aerosol Optical Depth',
'Snow Depth',
'Days Since Last Snowfall',
'Albedo',
'Liquid Precipitation Depth',
'Liquid Precipitation Quantity']
first_row=self._first_row_with_climate_data(fp)
df=pd.read_csv(fp,
skiprows=first_row,
header=None,
names=names)
return df
def _first_row_with_climate_data(self,fp):
"""Finds the first row with the climate data of an epw file
Arguments:
- fp (str): the file path of the epw file
Return value:
- i (int): the row number
"""
with open(fp, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for i,row in enumerate(csvreader):
if row[0].isdigit():
break
return i
def write(self,fp):
"""Writes an epw file
Arguments:
- fp (str): the file path of the new epw file
"""
with open(fp, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
for k,v in self.headers.items():
csvwriter.writerow([k]+v)
for row in self.dataframe.itertuples(index= False):
csvwriter.writerow(i for i in row)