-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprocess_smc.py
More file actions
executable file
·196 lines (173 loc) · 6.61 KB
/
process_smc.py
File metadata and controls
executable file
·196 lines (173 loc) · 6.61 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
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
#!/usr/bin/env python
"""
# ==========================================================================
# The program is to get filename/directory in various ways, and then processes
# found V1/V2 files --> generate column acceleration .txt files and .her files.
# ==========================================================================
"""
from __future__ import print_function
import sys
import os
from smc import set_destination, load_smc_v1, load_smc_v2, \
print_smc, print_her, print_bbp
from seism import seism_station
destination = ''
def get_parameters():
"""
This function gets the list of files to process and creates a list.
Additionally, it asks the user to enter a destination directory where
output files will be written.
"""
file_list = []
output_format = ''
global destination
# Check if user provided any parameters
if len(sys.argv) >= 2:
output_format = sys.argv[1]
if len(sys.argv) >= 3:
destination = sys.argv[2]
if len(sys.argv) >= 4:
file_list = sys.argv[3:]
# Get the output format the user wants
while output_format != 'bbp' and output_format != 'her':
output_format = raw_input('== Enter output format (bbp/her): ')
output_format = output_format.lower()
while not file_list:
# ask user if filename is not provided in command-line
file_list = raw_input('== Enter the file / directory name: ')
file_list = file_list.split()
while not destination:
destination = raw_input('== Enter name of the directory to store outputs: ')
# check existence of target directory
if not os.path.exists(destination):
os.makedirs(destination)
else:
# clear content of unprocessed and warning files if they exist
if os.path.exists(os.path.join(destination, 'unprocessed.txt')):
clear(os.path.join(destination, 'unprocessed.txt'))
if os.path.exists(os.path.join(destination, 'warning.txt')):
clear(os.path.join(destination, 'warning.txt'))
# Set destination in smc module
set_destination(destination)
# All done!
return file_list, output_format
# end of get_filename
def read_list(file_list, output_format):
"""
The function is to read a list of files/directory and check their
types to call corresponding functions.
"""
for cur_file in file_list:
# if is a directory; read all files\directories in the directory
# and append them to file_list
if os.path.isdir(cur_file):
for item in os.listdir(cur_file):
filename = os.path.join(cur_file, item)
if not filename in file_list:
file_list.append(filename)
# if is an non-empty file
elif os.path.isfile(cur_file) and os.stat(cur_file).st_size != 0:
# if the file is V1/raw data file: generate text file
# for acceleration, and .her file
if cur_file.upper().endswith(".V1") \
or cur_file.upper().endswith(".RAW"):
processed = False
station = load_smc_v1(cur_file)
# if encounters errors with records in station
if (not station) or (not station.list):
station = False
else:
processed = station.process_v1()
if not processed:
print_message(cur_file, 'unprocessed')
else:
print_smc(station)
print_her(station)
check_station(station)
# if the file is V2/processed data file; generate text file
# for acceleration, and .her file
elif cur_file.upper().endswith(".V2"):
processed = False
station = load_smc_v2(cur_file)
if station:
processed = station.process_v2() #rotate
if not processed:
print_message(cur_file, 'unprocessed')
else:
print_smc(station)
if output_format == 'her':
print_her(station)
elif output_format == 'bbp':
print_bbp(station)
else:
print("Error: Unknown output format %s!" %
(output_format))
check_station(station)
else:
try:
input_file = open(cur_file)
except IOError as err:
print(err)
continue
lines = input_file.read().split()
input_file.close()
if not '#filelist' in lines[0]:
# unrecognized file type, skip it!
continue
else:
for item in lines[1:]:
if not item in file_list:
file_list.append(item)
else:
print("[ERROR]: no such file or directory: %s" % (cur_file))
# end of read_list
def print_message(message, ftype):
"""
The function is to generate a files containing warning/unprocessed
messages for input files.
"""
out_file = open(os.path.join(destination, '%s.txt' % (ftype)), 'a')
out_file.write(message + "\n")
out_file.close()
# end of print_message
def check_station(station):
"""
The function is to check the station name of each record,
if it's in the location should be discarded, print warning.
"""
# check instance
if not isinstance(station, seism_station):
return
if not station.list:
return
discard = {'dam': 'Dam', 'Fire Sta': 'Fire Station',
'Acosta Res': 'Acosta Res', 'Bldg': 'Building',
'Br': 'Interchange Bridge'}
name = station.name
for key in discard:
if key in name:
filename = station.network + station.id + '.' + station.type
msg = filename + " was processed, but it's from " + discard[key]
print_message(msg, 'warning')
break
# end of check_station
def clear(filename):
"""
This function clears the content of a file if it exists.
"""
try:
open(filename, 'w').close()
except IOError as err:
print(err)
# end of clear
def process_main():
"""
Main function for the process_smc program
"""
# Main function
file_list, output_format = get_parameters()
read_list(file_list, output_format)
# ============================ MAIN ==============================
if __name__ == "__main__":
process_main()
# end of main program