1 |
|
---|
2 |
|
---|
3 |
""" |
---|
4 |
parse datestr from cr1000 files to create monthly files |
---|
5 |
intended to run on Loggernet Computer |
---|
6 |
|
---|
7 |
input file |
---|
8 |
C:/Campbellsci/Loggernet/CR1000_CBC_15MinData.dat |
---|
9 |
C:/Campbellsci/Loggernet/CR1000_CBC_HourlyData.dat |
---|
10 |
|
---|
11 |
Output form |
---|
12 |
C:/Campbellsci/Loggernet//cbc/wq/cbc_wq_yyyy_mm.dat |
---|
13 |
C:/Campbellsci/Loggernet//cbc/flow/cbc_flow_yyyy_mm.dat |
---|
14 |
|
---|
15 |
load data file |
---|
16 |
parse lines for time YYYY-MM-DD HH:MM:SS |
---|
17 |
what year and month? |
---|
18 |
|
---|
19 |
create monthly output file for the present month of data |
---|
20 |
this will delete any old and rewrite a new one from the original |
---|
21 |
Loggernet Data file |
---|
22 |
|
---|
23 |
There doesn't seem to be a clear cut way to do this in Loggernet |
---|
24 |
with LNBackup or LNSplit. |
---|
25 |
|
---|
26 |
""" |
---|
27 |
|
---|
28 |
REAL_RE_STR = '\\s*(-?\\d(\\.\\d+|)[Ee][+\\-]\\d\\d?|-?(\\d+\\.\\d*|\\d*\\.\\d+)|-?\\d+)\\s*' |
---|
29 |
|
---|
30 |
import sys |
---|
31 |
import os |
---|
32 |
import re |
---|
33 |
|
---|
34 |
import procutil |
---|
35 |
|
---|
36 |
def parse_csi_loggernet(fn, lines): |
---|
37 |
""" |
---|
38 |
|
---|
39 |
From FSL (CSI datalogger program files): |
---|
40 |
Example data: |
---|
41 |
|
---|
42 |
TOA5,CR1000_CBC,CR1000,5498,CR1000.Std.11,CPU:UNC_CrowBranch.CR1,1554,Data15Min |
---|
43 |
TIMESTAMP,RECORD,RainIn_Tot,WaterLevelFt,Flow |
---|
44 |
TS,RN,,, |
---|
45 |
,,Tot,Smp,Smp |
---|
46 |
2009-01-22 15:30:00,0,0,0,0 |
---|
47 |
2009-01-22 15:45:00,1,0,0,0 |
---|
48 |
2009-01-22 16:00:00,2,0.01,0,0 |
---|
49 |
2009-01-22 16:15:00,3,0,0,0 |
---|
50 |
|
---|
51 |
TOA5,CR1000_CBC,CR1000,5498,CR1000.Std.11,CPU:UNC_CrowBranch.CR1,1554,DataHourly |
---|
52 |
TIMESTAMP,RECORD,SondeTempC,SpCond,DOSat,DOmg,pH,Turb,BattVolt_Min |
---|
53 |
TS,RN,,,,,,, |
---|
54 |
,,Smp,Smp,Smp,Smp,Smp,Smp,Min |
---|
55 |
2009-01-22 16:00:00,0,2.68,0.533,7.63,-46.8,-1.4,0,11.99 |
---|
56 |
2009-01-22 17:00:00,1,3.07,0.553,7.62,-46.6,-1.4,0,11.96 |
---|
57 |
2009-01-22 18:00:00,2,3.45,0.548,7.62,-46.5,-1.4,0,11.91 |
---|
58 |
2009-01-22 19:00:00,3,3.53,0.546,7.62,-46.3,-1.4,0,11.89 |
---|
59 |
2009-01-22 20:00:00,4,3.59,0.547,7.62,-46.3,-1.4,0,11.86 |
---|
60 |
2009-01-22 21:00:00,5,3.55,0.545,7.61,-46.2,-0.7,0,11.84 |
---|
61 |
2009-01-22 22:00:00,6,3.47,0.545,7.62,-46.3,4.2,0,11.81 |
---|
62 |
2009-01-22 23:00:00,7,3.37,0.545,7.62,-46.4,-0.7,0,11.8 |
---|
63 |
2009-01-23 00:00:00,8,3.28,0.545,7.62,-46.5,4.2,0,11.78 |
---|
64 |
2009-01-23 01:00:00,9,3.17,0.546,7.62,-46.7,-0.9,0,11.76 |
---|
65 |
2009-01-23 02:00:00,10,3,0.549,7.63,-46.8,-1.3,0,11.74 |
---|
66 |
2009-01-23 03:00:00,11,2.95,0.55,7.64,-47.3,-1.4,0,11.73 |
---|
67 |
2009-01-23 04:00:00,12,2.89,0.552,7.63,-47.2,-1.4,0,11.71 |
---|
68 |
2009-01-23 05:00:00,13,2.8,0.554,7.64,-47.3,-1.4,0,11.69 |
---|
69 |
2009-01-23 06:00:00,14,2.72,0.554,7.64,-47.6,-1.3,0,11.68 |
---|
70 |
|
---|
71 |
""" |
---|
72 |
|
---|
73 |
p = os.path.split(fn) |
---|
74 |
(loggertype, id, datatype) = p[1].split('_') |
---|
75 |
|
---|
76 |
this_month_str = procutil.this_month() |
---|
77 |
|
---|
78 |
if datatype=='Data15Min.dat': |
---|
79 |
data_dir = os.path.join(p[0],id.lower(),'flow') |
---|
80 |
ofn_prefix = '%s_%s' % (id.lower(), 'flow') |
---|
81 |
samples_per_hour = 4 |
---|
82 |
elif datatype=='DataHourly.dat': |
---|
83 |
data_dir = os.path.join(p[0],id.lower(),'wq') |
---|
84 |
ofn_prefix = '%s_%s' % (id.lower(), 'wq') |
---|
85 |
samples_per_hour = 1 |
---|
86 |
|
---|
87 |
if not os.path.isdir(data_dir): |
---|
88 |
print ' ... Creating directory: '+data_dir |
---|
89 |
os.mkdir(data_dir) |
---|
90 |
|
---|
91 |
ofn = os.path.join(data_dir, ofn_prefix) |
---|
92 |
ofn = '_'.join([ofn, this_month_str]) |
---|
93 |
ofn = '.'.join([ofn, 'dat']) |
---|
94 |
|
---|
95 |
|
---|
96 |
if os.path.exists(ofn): |
---|
97 |
print ' ... ... Deleting file: '+ofn |
---|
98 |
os.remove(ofn) |
---|
99 |
|
---|
100 |
|
---|
101 |
starti = -32*samples_per_hour*24 |
---|
102 |
endi = -1 |
---|
103 |
|
---|
104 |
if len(lines)<32*samples_per_hour*24+4: |
---|
105 |
starti = 4 |
---|
106 |
|
---|
107 |
|
---|
108 |
for line in lines[starti:endi]: |
---|
109 |
|
---|
110 |
sw = re.split(',', line) |
---|
111 |
|
---|
112 |
if len(sw)>=1: |
---|
113 |
|
---|
114 |
|
---|
115 |
sample_dt = procutil.scanf_datetime(sw[0], fmt='"%Y-%m-%d %H:%M:%S"') |
---|
116 |
file_month_str = '%4d_%02d' % sample_dt.timetuple()[0:2] |
---|
117 |
else: |
---|
118 |
|
---|
119 |
print 'ill-formed time, line not to be copied: ' + line |
---|
120 |
continue |
---|
121 |
|
---|
122 |
if file_month_str == this_month_str: |
---|
123 |
if os.path.exists(ofn): |
---|
124 |
f = open(ofn, 'a') |
---|
125 |
f.write(line) |
---|
126 |
f.close |
---|
127 |
else: |
---|
128 |
print ' ... ... Creating file: '+ofn |
---|
129 |
print lines[0:4] |
---|
130 |
|
---|
131 |
|
---|
132 |
f = open(ofn, 'w') |
---|
133 |
for l in lines[0:4]: |
---|
134 |
f.write(l) |
---|
135 |
f.write(line) |
---|
136 |
f.close() |
---|
137 |
|
---|
138 |
|
---|
139 |
return |
---|
140 |
|
---|
141 |
def load_data(inFile): |
---|
142 |
lines=None |
---|
143 |
if os.path.exists(inFile): |
---|
144 |
f = open(inFile, 'r') |
---|
145 |
lines = f.readlines() |
---|
146 |
f.close() |
---|
147 |
if len(lines)<=0: |
---|
148 |
print 'Empty file: '+ inFile |
---|
149 |
else: |
---|
150 |
print 'File does not exist: '+ inFile |
---|
151 |
return lines |
---|
152 |
|
---|
153 |
def spin(): |
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 |
fns = [ |
---|
161 |
'C:/Campbellsci/Loggernet/CR1000_CBC_Data15Min.dat', |
---|
162 |
'C:/Campbellsci/Loggernet/CR1000_CBC_DataHourly.dat', |
---|
163 |
'C:/Campbellsci/Loggernet/CR1000_MOW_Data15Min.dat', |
---|
164 |
'C:/Campbellsci/Loggernet/CR1000_MOW_DataHourly.dat', |
---|
165 |
] |
---|
166 |
|
---|
167 |
for fn in fns: |
---|
168 |
lines = load_data(fn) |
---|
169 |
parse_csi_loggernet(fn, lines) |
---|
170 |
|
---|
171 |
return |
---|
172 |
|
---|
173 |
|
---|
174 |
if __name__ == '__main__': |
---|
175 |
try: |
---|
176 |
spin() |
---|
177 |
except: |
---|
178 |
pass |
---|
179 |
|
---|