-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathher2bbp.py
More file actions
executable file
·151 lines (140 loc) · 6.05 KB
/
her2bbp.py
File metadata and controls
executable file
·151 lines (140 loc) · 6.05 KB
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
#!/usr/bin/env python
"""
Utility to convert Hercules .her time history files to BBP format
"""
from __future__ import division, print_function
# Import python modules
import os
import sys
import argparse
def parse_her_header(filename):
"""
This function parses the her file header
to try to figure out what units to use
"""
# Default unit is meters
unit = "m"
try:
input_file = open(filename, 'r')
for line in input_file:
line = line.strip()
if line.startswith("#"):
# Header line, look into it
pieces = line.split()
if len(pieces) != 11:
# Not the line we are looking for
continue
if pieces[2].find("(m)") > 0:
# It's meters!
unit = "m"
break
if pieces[2].find("(cm)") > 0:
# It's cm!
unit = "cm"
break
continue
except IOError:
print("[ERROR]: Unable to read file: %s" % (filename))
sys.exit(1)
input_file.close()
# Return units
return unit
def write_bbp_header(out_fp, file_type, file_unit, args):
"""
This function writes the bbp header
"""
# Write header
out_fp.write("# Station: %s\n" % (args.station_name))
out_fp.write("# time= %s\n" % (args.time))
out_fp.write("# lon= %s\n" % (args.longitude))
out_fp.write("# lat= %s\n" % (args.latitude))
out_fp.write("# units= %s\n" % (file_unit))
out_fp.write("#\n")
out_fp.write("# Data fields are TAB-separated\n")
out_fp.write("# Column 1: Time (s)\n")
out_fp.write("# Column 2: N/S component ground "
"%s (+ is 000)\n" % (file_type))
out_fp.write("# Column 3: E/W component ground "
"%s (+ is 090)\n" % (file_type))
out_fp.write("# Column 4: U/D component ground "
"%s (+ is upward)\n" % (file_type))
out_fp.write("#\n")
def her2bbp_main():
"""
Main function for her to bbp converter
"""
parser = argparse.ArgumentParser(description="Converts a Hercules .her"
"file to BBP format, generating "
"displacement, velocity and acceleration "
"BBP files.")
parser.add_argument("-s", "--station-name", dest="station_name",
default="NoName",
help="provides the name for this station")
parser.add_argument("--lat", dest="latitude", type=float, default=0.0,
help="provides the latitude for the station")
parser.add_argument("--lon", dest="longitude", type=float, default=0.0,
help="provides the longitude for the station")
parser.add_argument("-t", "--time", default="00/00/00,0:0:0.0 UTC",
help="provides timing information for this timeseries")
parser.add_argument("input_file", help="Hercules input timeseries")
parser.add_argument("output_stem",
help="output BBP filename stem without the "
" .{dis,vel,acc}.bbp extensions")
parser.add_argument("-d", dest="output_dir", default="",
help="output directory for the BBP file")
args = parser.parse_args()
input_file = args.input_file
output_file_dis = "%s.dis.bbp" % (os.path.join(args.output_dir,
args.output_stem))
output_file_vel = "%s.vel.bbp" % (os.path.join(args.output_dir,
args.output_stem))
output_file_acc = "%s.acc.bbp" % (os.path.join(args.output_dir,
args.output_stem))
# Try to get the units used in the her file
units = {"m": ["m", "m/s", "m/s^2"],
"cm": ["cm", "cm/s", "cm/s^2"]}
unit = parse_her_header(input_file)
# Covert from her to BBP format
ifile = open(input_file)
o_dis_file = open(output_file_dis, 'w')
o_vel_file = open(output_file_vel, 'w')
o_acc_file = open(output_file_acc, 'w')
write_bbp_header(o_dis_file, "displacement", units[unit][0], args)
write_bbp_header(o_vel_file, "velocity", units[unit][1], args)
write_bbp_header(o_acc_file, "acceleration", units[unit][2], args)
for line in ifile:
line = line.strip()
# Skip comments
if line.startswith("#") or line.startswith("%"):
pieces = line.split()[1:]
# Write header
if len(pieces) >= 10:
o_dis_file.write("# her header: # %s %s %s %s\n" %
(pieces[0], pieces[1], pieces[2], pieces[3]))
o_vel_file.write("# her header: # %s %s %s %s\n" %
(pieces[0], pieces[4], pieces[5], pieces[6]))
o_acc_file.write("# her header: # %s %s %s %s\n" %
(pieces[0], pieces[7], pieces[8], pieces[9]))
else:
o_dis_file.write("# her header: %s\n" % (line))
continue
pieces = line.split()
pieces = [float(piece) for piece in pieces]
# Write timeseries to files. Please not that Hercules files have
# the vertical component positive pointing down so we have to flip it
# here to match the BBP format in which vertical component points up
o_dis_file.write("%1.9E %1.9E %1.9E %1.9E\n" %
(pieces[0], pieces[1], pieces[2], -1 * pieces[3]))
o_vel_file.write("%1.9E %1.9E %1.9E %1.9E\n" %
(pieces[0], pieces[4], pieces[5], -1 * pieces[6]))
o_acc_file.write("%1.9E %1.9E %1.9E %1.9E\n" %
(pieces[0], pieces[7], pieces[8], -1 * pieces[9]))
# All done, close everything
ifile.close()
o_dis_file.close()
o_vel_file.close()
o_acc_file.close()
# ============================ MAIN ==============================
if __name__ == "__main__":
her2bbp_main()
# end of main program