-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssfUtil.py
192 lines (167 loc) · 6.15 KB
/
ssfUtil.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
import datetime,re
import numpy as np
import pylab
from matplotlib.dates import *
import matplotlib.ticker as tk
def listdate2ordarray(dlist,tlist,dlim='/'):
assert len(dlist)==len(tlist)
ordArray = np.zeros((len(dlist)))
for l in range(0,len(dlist)):
draw = dlist[l].split(dlim)
thisDay = int(draw[0])
thisMonth = int(draw[1])
thisYear = int(draw[2])
traw = tlist[l].split(':')
thisHour = int(traw[0])
thisMin = int(traw[1])
thisSec = float(traw[2])
thisDate = datetime.datetime(thisYear,thisMonth,thisDay)
ordArray[l] = thisDate.toordinal()
return ordArray
def load_ssf(file):
data = []
names = []
f = open(file,'r')
raw = f.readline().strip().split()
d,t = [raw[1]],[raw[2]]
dataList = [raw[3]]
thisSite = raw[0]
for line in f:
raw = line.strip().split()
if raw[0] != thisSite:
print thisSite,raw[0]
dataArray = np.array(dataList)
ordArray = listdate2ordarray(d,t)
names.append(thisSite)
ordArray = np.vstack((ordArray,dataArray))
data.append(ordArray.transpose().astype(float))
d,t = [raw[1]],[raw[2]]
dataList = [raw[3]]
thisSite = raw[0]
else:
d.append(raw[1])
t.append(raw[2])
dataList.append(raw[3])
dataArray = np.array(dataList)
ordArray = listdate2ordarray(d,t)
#print dataArray.shape,ordArray.shape
names.append(thisSite)
ordArray = np.vstack((ordArray,dataArray))
#print ordArray.shape
data.append(ordArray.transpose().astype(float))
f.close()
return names,data
def write_ssf(site,array,file):
f = open(file,'w')
for rec in array:
this_dt = datetime.datetime.fromordinal(rec[0])
date_string = str(this_dt.day).zfill(2)+'/'+str(this_dt.month).zfill(2)+'/'+str(this_dt.year)
time_string = str(this_dt.hour).zfill(2)+':'+str(this_dt.minute).zfill(2)+':'+str(this_dt.second).zfill(2)
#print site,date_string,time_string,rec[1]
f.write(site.ljust(15)+' '+date_string+' '+time_string+' '+str(rec[1])+'\n')
f.close()
def load_tsproc_list(file):
reg = re.compile('TIME_SERIES',re.IGNORECASE)
data = []
names = []
f = open(file,'r')
blank = f.readline()
tsname = f.readline()
raw = f.readline().strip().split()
d,t = [raw[1]],[raw[2]]
dataList = [raw[3]]
thisSite = raw[0]
#for line in f:
while True:
line = f.readline()
if line == '': break
raw = line.strip().split()
#if raw[0] != thisSite:
if len(raw) == 0:
#print thisSite,raw[0]
dataArray = np.array(dataList)
ordArray = listdate2ordarray(d,t)
names.append(thisSite)
ordArray = np.vstack((ordArray,dataArray))
data.append(ordArray.transpose().astype(float))
tsname = f.readline()
#print tsname
if reg.search(tsname) == None: break
line = f.readline()
raw = line.strip().split()
d,t = [raw[1]],[raw[2]]
dataList = [raw[3]]
thisSite = raw[0]
else:
d.append(raw[1])
t.append(raw[2])
dataList.append(raw[3])
dataArray = np.array(dataList)
ordArray = listdate2ordarray(d,t)
names.append(thisSite)
ordArray = np.vstack((ordArray,dataArray))
#print ordArray.shape
data.append(ordArray.transpose().astype(float))
f.close()
return names,data
def plot_ts(t,data,names=None,output='show',ax=None,color=None,lw=1.0):
#print 'total date range: ',t[-1]-t[0]
#--set up date formatters
if t[-1]-t[0] < 90:
majortick = MonthLocator()
minortick = DayLocator(15)
minFmt = DateFormatter('%d')
majFmt = DateFormatter('%b')
elif t[-1]-t[0] < 365:
majortick = MonthLocator()
minortick = DayLocator()
minFmt = DateFormatter('')
majFmt = DateFormatter('%b')
elif t[-1]-t[0] < 5280:
majortick = YearLocator()
minortick = MonthLocator((5,9))
minFmt = DateFormatter('%b')
majFmt = DateFormatter('%Y')
elif t[-1]-t[0] < 3650:
majortick = YearLocator(2)
minortick = MonthLocator()
minFmt = DateFormatter('')
majFmt = DateFormatter('%Y')
else:
majortick = YearLocator(5)
minortick = YearLocator()
minFmt = DateFormatter('')
majFmt = DateFormatter('%Y')
if ax == None:
fig = pylab.figure()
ax = pylab.subplot(111)
#print data.shape
try:
for s in range(0,np.shape(data)[1]):
if color != None:
ax.plot(t,data[:,s],label=names[s],color=color[s],lw=lw)
else:
ax.plot(t,data[:,s],label=names[s],lw=lw)
except:
if color != None:
ax.plot(t,data,label=names,color=color,lw=lw)
else:
ax.plot(t,data,label=names,lw=lw)
if names != None:
ax.legend()
ax.xaxis.set_major_locator(majortick)
ax.xaxis.set_minor_locator(minortick)
ax.xaxis.set_major_formatter(majFmt)
ax.xaxis.set_minor_formatter(minFmt)
if output =='show' :
pylab.show()
return
elif output == None: return ax
else:
fmt = output.split('.')[-1]
pylab.savefig(output,orientation='portrait',format=fmt,dpi=150)
return
#names,data = load_ssf('flows.out')
#print names,len(data),data[0].shape
#print data[0][0,0],data[0][0,1]
#plot_ts(data[0][:,0],data[0][:,1],names[0])