-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_cr10x_by_month.py
executable file
·204 lines (165 loc) · 5.71 KB
/
split_cr10x_by_month.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
#!/usr/bin/env python
# Last modified: Time-stamp: <2009-10-08 16:49:23 haines>
"""
parse yr, yrday, time from csi loggernet and create monthly files
input file
/seacoos/data/nccoos/level0/crow/crow_csi_loggernet_yyyymmdd-yyyymmdd.dat
Output form
/seacoos/data/nccoos/level0/crow/yyyy_mm/wq/csi_wq_yyyy_mm.dat
/seacoos/data/nccoos/level0/crow/yyyy_mm/flow/csi_flow_yyyy_mm.dat
load data file
parse lines for time YYYY, jjj, HHMM
what year and month?
create YYYY_MM directory and output file if does not exist.
write line to YYYY_MM/csi_loggernet_yyyy_mm.dat output file
"""
REAL_RE_STR = '\\s*(-?\\d(\\.\\d+|)[Ee][+\\-]\\d\\d?|-?(\\d+\\.\\d*|\\d*\\.\\d+)|-?\\d+)\\s*'
import sys
import os
import re
def parse_csi_loggernet(fn, lines):
"""
From FSL (CSI datalogger program files):
15 Output_Table 15.00 Min
1 15 L
2 Year_RTM L
3 Day_RTM L
4 Hour_Minute_RTM L
5 Rain15sec_TOT L
6 SonLevlft L
7 SonFlow L
8 PrDepthft L
9 PrFlowcfs L
1 Output_Table 60.00 Min
1 1 L
2 Year_RTM L
3 Day_RTM L
4 Hour_Minute_RTM L
5 H2OTempC L
6 SpCond L
7 DOSat L
8 DOmg L
9 PH L
10 Turb L
11 BattVolts L
Example data:
1,2005,83,1600,16.47,0,.4,.04,8.14,115.5,14.25
15,2005,83,1615,0,4.551,-.547,.897,.885
15,2005,83,1630,0,4.541,.727,.908,1.005
15,2005,83,1645,0,4.537,6.731,.878,.676
15,2005,83,1700,0,4.537,6.731,.83,.167
1,2005,83,1700,16.57,0,.4,.03,8.03,145.7,13.08
15,2005,83,1715,0,4.547,5.29,.847,.347
15,2005,83,1730,0,4.541,.908,.842,.287
15,2005,83,1745,0,4.547,7.3,.853,.407
15,2005,83,1800,0,4.551,6.939,.855,.437
1,2005,83,1800,15.65,0,.2,.02,7.91,111.3,12.98
"""
import numpy
from datetime import datetime
from time import strptime
import math
p = os.path.split(fn)
id = p[1].split('_')[0]
for line in lines:
csi = []
# split line and parse float and integers
sw = re.split(',', line)
for s in sw:
m = re.search(REAL_RE_STR, s)
if m:
csi.append(float(m.groups()[0]))
if len(csi)>=4 and (re.search('^1,',line) or re.search('^15,',line)):
# print line
# correct 2400 hour
# get sample datetime from data
yyyy = csi[1]
yday = csi[2]
(MM, HH) = math.modf(csi[3]/100.)
MM = math.ceil(MM*100.)
if (HH == 24):
yday=yday+1
HH = 0.
sample_str = '%04d-%03d %02d:%02d' % (yyyy, yday, HH, MM)
sample_dt = scanf_datetime(sample_str, fmt='%Y-%j %H:%M')
month_str = '%4d_%02d' % sample_dt.timetuple()[0:2]
else:
# not a well-formed line, so skip to next line
print 'ill-formed time, line not to be copied: ' + line
continue
if re.search('^1,',line) and len(csi)>=4:
# does month dir exist
data_dir = os.path.join(p[0],'wq',month_str)
if not os.path.isdir(data_dir):
print 'Creating directory: '+data_dir
os.mkdir(data_dir)
ofn_prefix = '%s_%s' % (id, 'wq')
ofn = os.path.join(data_dir, ofn_prefix)
ofn = '_'.join([ofn, month_str])
ofn = '.'.join([ofn, 'dat'])
if os.path.exists(ofn):
f = open(ofn, 'a')
f.write(line)
f.close
else:
print 'Creating file: '+ofn
f = open(ofn, 'w')
f.write(line)
f.close()
if re.search('^15,',line) and len(csi)>=4:
data_dir = os.path.join(p[0],'flow',month_str)
if not os.path.isdir(data_dir):
print 'Creating directory: '+data_dir
os.mkdir(data_dir)
ofn_prefix = '%s_%s' % (id, 'flow')
ofn = os.path.join(data_dir, ofn_prefix)
ofn = '_'.join([ofn, month_str])
ofn = '.'.join([ofn, 'dat'])
if os.path.exists(ofn):
f = open(ofn, 'a')
f.write(line)
f.close
else:
print 'Creating file: '+ofn
f = open(ofn, 'w')
f.write(line)
f.close()
# for line
return
def load_data(inFile):
lines=None
if os.path.exists(inFile):
f = open(inFile, 'r')
lines = f.readlines()
f.close()
if len(lines)<=0:
print 'Empty file: '+ inFile
else:
print 'File does not exist: '+ inFile
return lines
from raw2proc import *
def test1(fn):
lines = load_data(fn)
return parse_csi_loggernet(fn, lines)
def spin():
# data prior to 2009-01 (CR10X v1 and v2)
fns = [
'/seacoos/data/nccoos/level0/crow/cbc_loggernet_20060316-20080829.dat',
#
'/seacoos/data/nccoos/level0/meet/mow_loggernet_20010510-20030925.dat',
'/seacoos/data/nccoos/level0/meet/mow_loggernet_20030925-20041209.dat',
'/seacoos/data/nccoos/level0/meet/mow_loggernet_20050325-20070726.dat',
'/seacoos/data/nccoos/level0/meet/mow_loggernet_20080404-20080826.dat',
]
for fn in fns:
test1(fn)
if __name__ == '__main__':
pass
# fn = '/seacoos/data/nccoos/level0/crow/cbc_loggernet_20050325-20070726.dat'
#
# #
# # fn = sys.argv[1]
# try:
# test1(fn)
# except:
# pass