forked from nrcharles/solpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epw.py
215 lines (197 loc) · 6.43 KB
/
epw.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# coding=utf-8
# Copyright (C) 2013 Nathan Charles
#
# This program is free software. See terms in LICENSE file.
import os
import re
import csv
#path to epw data
#default = ~/epw/
path = os.environ['HOME'] + "/epw/"
SPATH = os.path.dirname(os.path.abspath(__file__))
try:
os.listdir(os.environ['HOME'] + '/epw')
except OSError:
try:
os.mkdir(os.environ['HOME'] + '/epw')
except IOError:
pass
def basename(USAF):
files = os.listdir(path)
for f in files:
if f.find(USAF) is not -1:
return f[0:f.rfind('.')]
def epwbasename(USAF):
f = open(SPATH + '/epwurls.csv')
for line in f.readlines():
if line.find(USAF) is not -1:
return line.rstrip()
def downloadEPW(USAF):
import os
url = "http://apps1.eere.energy.gov/buildings/energyplus/weatherdata/4_north_and_central_america_wmo_region_4/1_usa/"
import urllib2
import zipfile
epwfile = epwbasename(USAF)
u = urllib2.urlopen(url + epwfile)
localFile = open(path + epwfile, 'w')
localFile.write(u.read())
localFile.close()
epw = zipfile.ZipFile(path + epwfile, 'r')
epw.extractall(path)
os.remove(path + epwfile)
def twopercent(USAF):
#(DB=>MWB) 2%, MaxDB=
temp = None
try:
fin = open('%s/%s.ddy' % (path,basename(USAF)))
for line in fin:
m = re.search('2%, MaxDB=(\d+\.\d*)',line)
if m:
temp = float(m.groups()[0])
except:
pass
if not temp:
#(DB=>MWB) 2%, MaxDB=
try:
fin = open('%s/%s.stat' % (path,basename(USAF)))
flag = 0
data = []
for line in fin:
if line.find('2%') is not -1:
flag = 3
if flag > 0:
data.append(line.split('\t'))
flag -= 1
temp = float(data[2][5].strip())
except:
pass
if temp:
return temp
else:
print "Warning: 2% High Temperature not found, using worst case"
return 38.0
def minimum(USAF):
#(DB=>MWB) 2%, MaxDB=
temp = None
fin = None
try:
fin = open('%s/%s.ddy' % (path,basename(USAF)))
except:
print "File not found"
print "Downloading ..."
downloadEPW(USAF)
fin = open('%s/%s.ddy' % (path,basename(USAF)))
for line in fin:
m = re.search('Max Drybulb=(-?\d+\.\d*)',line)
if m:
temp = float(m.groups()[0])
if not temp:
try:
fin = open('%s/%s.stat' % (path,basename(USAF)))
for line in fin:
if line.find('Minimum Dry Bulb') is not -1:
return float(line[37:-1].split('\xb0')[0])
except:
pass
if temp:
return temp
else:
print "Warning: Minimum Temperature not found, using worst case"
return -23.0
class data():
def __init__(self, USAF):
#filename = path + USAF + 'TY.csv'
filename = '%s/%s.epw' % (path,basename(USAF))
self.csvfile = None
try:
self.csvfile = open(filename)
except:
print "File not found"
print "Downloading ..."
downloadEPW(USAF)
self.csvfile = open(filename)
header = ""
fieldnames = ["Year","Month","Day","Hour","Minute","DS","Drybulb (C)",
"Dewpoint (C)", "Relative Humidity", "Pressure (Pa)",
"ETR (W/m^2)","ETRN (W/m^2)","HIR (W/m^2)","GHI (W/m^2)",
"DNI (W/m^2)","DHI (W/m^2)","GHIL (lux)","DNIL (lux)",
"DFIL (lux)","Zlum (Cd/m2)","Wdir (degrees)","Wspd (m/s)",
"Ts cover", "O sky cover","CeilHgt (m)","Present Weather",
"Pw codes","Pwat (cm)","AOD (unitless)","Snow Depth (cm)",
"Days since snowfall"]
for i in range(8):
header += self.csvfile.readline()
self.epw_data = csv.DictReader(self.csvfile,fieldnames=fieldnames)
#print self.latitude, self.longitude
def __iter__(self):
return self
def next(self):
t = self.epw_data.next()
#sd = t['Date (MM/DD/YYYY)'] +' '+ t['Time (HH:MM)']
#tz = -5
#t['utc_datetime'] = strptime(sd,tz)
#t['datetime'] = strptime(sd)
return t
def __del__(self):
self.csvfile.close()
def hdd(USAF,base=18):
"""Heating degree days in C"""
total = 0.0
for record in data(USAF):
delta = base - float(record['Drybulb (C)'])
if delta > 0:
total += delta
return round(total/24.0,1)
def cdd(USAF,base=18):
"""cooling degree days in C"""
total = 0.0
for record in data(USAF):
delta = float(record['Drybulb (C)']) - base
if delta > 0:
total += delta
return round(total/24.0,1)
if __name__ == "__main__":
import argparse
import sys
import geo
parser = argparse.ArgumentParser(description='Model a PV system. Currently displays annual output and graph')
#import sys
#opts, args = getopt.getopt(sys.argv[1:], 'f:h')
parser.add_argument('-z', '--zipcode',required=True)
parser.add_argument('-m', '--mname')
parser.add_argument('-v', '--voltage',type=int,default=600)
args = vars(parser.parse_args())
#print args
try:
#start program
zip = args['zipcode']
maxVoltage = args['voltage']
stationClass = 1
name, usaf = geo.closestUSAF( geo.zipToCoordinates(zip), stationClass)
print "%s USAF: %s" % (name, usaf)
print "Minimum Temperature: %s C" % minimum(usaf)
print "2%% Max: %s C" % twopercent(usaf)
print "Heating Degree days: %s" % hdd(usaf)
print "Cooling Degree days: %s" % cdd(usaf)
if args['mname']:
print ""
import modules
models = modules.model_search(args['mname'].split(' '))
m = None
if len(models) > 1:
for i in models:
print i
sys.exit(1)
elif len(models) == 1:
print models[0]
m = modules.module(models[0])
else:
print "Model not found"
sys.exit(1)
print "Maximum: %sV" % m.Vmax(minimum(usaf))
print "Max in series", int(maxVoltage/m.Vmax(minimum(usaf)))
print "Minimum: %sV" % m.Vmin(twopercent(usaf))
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
raise