Newer
Older
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
#!/usr/bin/python
#
# sta-fr22.py
# $Id$
"""
Python script for writing up an station file for SADCP processing CASCADE
usage:
$ python sta-fr22.py [options] <files>
options are:
-h, --help : display this help and quit
-v, --version : display program version and quit
-e, --echo : display processed file name
-q, --quiet : don't display output
-o, --output=<file> : write output to file, default is sta-fr22.list
exemple:
$ python sta-fr22.py ../../CTD/data/FR22*.btl -e
$ python sta-fr22.py ../../CTD/data/FR22*.btl --output=FR22_sta.list
"""
import sys, re, getopt
from datetime import datetime
# initialize variables and options
# --------------------------------
VERSION = "V1.0 J Grelet - IRD - US191 IMAGO, Plouzane - may 2011"
echo = 0
quiet = 1
outputfile = 'FR22_sta.list'
# initialize datetime object
# --------------------------
dt = datetime
# ---------------------------------------------------------------
# display help using text inside """ """" at the beginning of the
# script with special attribute __doc__ and quit
# ---------------------------------------------------------------
def usage():
print __doc__
sys.exit()
# ------------------------------------------------
# display version and quit
# ------------------------------------------------
def version():
print "%s: %s" % (sys.argv[0], VERSION)
sys.exit()
# display help with no arg on command-line
# ----------------------------------------
if len(sys.argv) == 1:
usage()
# setting and get otpions
# -----------------------
try:
# The return value consists of two elements: the first is a list of
# (option, value) pairs; the second is the list of program arguments
# left after the option list was stripped
# see optparse, a powerful library for parsing command-line options
# The gnu version of getopt means that option and non-option
# arguments may be intermixed
# ------------------------------------------------------------------
options, args = getopt.gnu_getopt(sys.argv[1:], 'eqo:hv',
['echo', 'quiet', 'output=', 'help', 'version'])
# if bad option, display an error message and usage
# -------------------------------------------------
except getopt.GetoptError, err:
print str(err)
usage()
# iterate over options list
# -------------------------
for option, value in options:
if option in ('-e', '--echo'):
echo = 1
if option in ('-q', '--quiet'):
quiet = 0
elif option in ('-o', '--output'):
outputfile = value
elif option in ('-h', '--help'):
usage()
elif option in ('-v', '--version'):
version()
# prepare compiled patterns for regular expressions
# extract station number
# -------------------------------------------------
s = re.compile(r"FR22(\d{3})")
# extract CTD station start date
# ------------------------------
p = re.compile(r"System UpLoad Time\s+=\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+:\d+:\d+)")
# extract sample date
# -------------------
d = re.compile(r"^\s+\d+\s+(\w+)\s+(\d+)\s+(\d+)\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\(avg\)")
# extract sample time
# -------------------
h = re.compile(r"^\s+(\d+:\d+:\d+)\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\d+.\d+\s+\(sdev\)")
# open output file, need to add some test if open fail
# ----------------------------------------------------
output = open( outputfile, "wr")
# iterate over file list getting with getopt.gnu_getopt
# ----------------------------------------------------
for arg in args:
# display processed file name
# ---------------------------
if echo: print arg
# extract station number from file name using compiled regexp s
# -------------------------------------------------------------
if s.search(arg):
st = int(s.search(arg).group(1))
# open current file, add some test
# --------------------------------
file = open( arg, "r")
# read each line of current file
# ------------------------------
for ligne in file.readlines():
# extract CTD station start date
# ------------------------------
if p.search(ligne):
(month, day, year, hour) = p.search(ligne).groups()
# extract every sample bottle date, and get the last
# -------------------------------------------------
if d.search(ligne):
(e_month, e_day, e_year) = d.search(ligne).groups()
# extract every sample bottle time
# --------------------------------
if h.search(ligne):
e_hour = h.search(ligne).group(1)
# format date and time to "May 09 2011 16:33:53"
# -----------------------------------------------
str1 = "%s/%s/%s %s" % (day, month, year, hour)
str2 = "%s/%s/%s %s" % (e_day, e_month, e_year, e_hour)
# conversion with datetime to "09/05/2011 16:33:53"
# -------------------------------------------------
strout = "%2d %s %s" % \
(st, dt.strptime(str1, "%d/%b/%Y %H:%M:%S").strftime("%d/%m/%Y %H:%M:%S"),
dt.strptime(str2, "%d/%b/%Y %H:%M:%S").strftime("%d/%m/%Y %H:%M:%S"))
# display the converted date and time
# -----------------------------------
if quiet: print strout
# print result to file
# --------------------
output.write(strout + "\n")
# display output file name if echo is define
# ------------------------------------------
if echo: print "writing data in %s \n" % (outputfile)