This repository has been archived by the owner on Apr 1, 2019. It is now read-only.
/
asos2hcd.py
executable file
·58 lines (52 loc) · 1.61 KB
/
asos2hcd.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
#!/mesonet/python/bin/python
# Generate .hcd file for Pavement Design Guide
#YYYYMMDDHH,Temperature (F),Wind speed (mph),% Sun shine, Precipitation, Relative humidity.
pcsun = {
-9999: 0,
0: 100,
1: 88,
2: 75,
3: 62,
4: 50,
5: 38,
6: 25,
7: 13,
8: 0,
9: 0,
}
import mx.DateTime, sys
from pyIEM import iemdb, mesonet
i = iemdb.iemdb()
asos = i['asos']
stid = sys.argv[1]
out = open("%s.dat" % (stid,), 'w')
fmt = "%s,%s,%.1f,%s,%s,%.1f\n"
missing = 0
now = mx.DateTime.DateTime(1972,12,31,23)
for yr in range(1973,2007):
sql = "SELECT *, \
case when sknt > 0 THEN sknt ELSE 0 END as sknt0,\
case when p01m > 0 THEN p01m ELSE 0 END as p,\
case when skyc > -1 THEN skyc ELSE 0 END as sk from t%s WHERE station = '%s' and tmpf > -50 and extract(year from valid) = %s ORDER by valid ASC" % (yr,stid,yr)
rs = asos.query(sql).dictresult()
for i in range(len(rs)):
ts = mx.DateTime.strptime(rs[i]['valid'][:13], "%Y-%m-%d %H")
if (now == ts):
#print 'DUP', now
continue
tmpf = rs[i]['tmpf']
mph = rs[i]['sknt0'] / 1.18
psun = pcsun[ int(rs[i]['sk']) ]
phour = rs[i]['p'] * 25.4
relh = mesonet.relh( rs[i]['tmpf'], rs[i]['dwpf'])
if (relh == "M"):
relh = 65
while ((now + mx.DateTime.RelativeDateTime(hours=1)) < ts):
now += mx.DateTime.RelativeDateTime(hours=1)
#print 'MISSING', now
missing += 1
out.write( fmt % (now.strftime("%Y%m%d%H"),tmpf, mph, psun, phour, relh))
out.write(fmt % (ts.strftime("%Y%m%d%H"), tmpf, mph, psun, phour, relh))
now = ts
out.close()
print 'Total missing:', missing