Skip to content

Commit 4526c69

Browse files
committed
Updated BBP tools to handle both srf and mrf files.
1 parent 95c0984 commit 4526c69

File tree

5 files changed

+77
-74
lines changed

5 files changed

+77
-74
lines changed

bbp/comps/fault_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
"""
33
BSD 3-Clause License
44
5-
Copyright (c) 2021, University of Southern California
5+
Copyright (c) 2024, University of Southern California
66
All rights reserved.
77
88
Redistribution and use in source and binary forms, with or without
@@ -69,17 +69,17 @@ def get_magnitude(velfile, srffile, suffix="tmp"):
6969
bband_utils.runprog(cmd, False)
7070
return magnitude
7171

72-
def get_hypocenter(srffile, suffix="tmp"):
72+
def get_hypocenter(input_file, suffix="tmp"):
7373
"""
74-
Looks up the hypocenter of an event in a srffile
74+
Looks up the hypocenter of an event in a SRF/MRF input_file
7575
"""
7676
hypfile = os.path.join(tempfile.gettempdir(),
7777
"%s_%s" %
7878
(str(uuid.uuid4()), suffix))
7979
install = InstallCfg.getInstance()
8080
cmd = ("%s < %s > %s" %
8181
(os.path.join(install.A_GP_BIN_DIR, "srf_gethypo"),
82-
srffile, hypfile))
82+
input_file, hypfile))
8383
bband_utils.runprog(cmd)
8484
srf_hypo_fp = open(hypfile, 'r')
8585
srf_hypo_data = srf_hypo_fp.readline()
@@ -126,12 +126,12 @@ def calculate_hypo_depth(srcfile):
126126
def calculate_epicenter(input_file):
127127
"""
128128
This function returns the epicenter of an event using either a SRC
129-
file or a SRF file to look for the hypocenter location. It uses
129+
file or a SRF/MRF file to look for the hypocenter location. It uses
130130
Rob Graves' xy2ll utility to convert the coordinates to lat/lon.
131131
"""
132132
# If we have a SRF file, we already have a function that does this
133-
if input_file.endswith(".srf"):
134-
# Get information from srf file
133+
if input_file.endswith(".srf") or input_file.endswith(".mrf"):
134+
# Get information from srf/mrf file
135135
hypo_lon, hypo_lat, _ = get_hypocenter(input_file)
136136
return hypo_lon, hypo_lat
137137

bbp/comps/plot_map.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
"""
33
BSD 3-Clause License
44
5-
Copyright (c) 2021, University of Southern California
5+
Copyright (c) 2024, University of Southern California
66
All rights reserved.
77
88
Redistribution and use in source and binary forms, with or without
@@ -71,8 +71,9 @@ def run(self):
7171

7272
if (self.input_file is None or self.input_file == "" or
7373
(not self.input_file.endswith(".srf") and
74-
not self.input_file.endswith(".src"))):
75-
# We need a SRC or SRF file to get the fault geometry
74+
(not self.input_file.endswith(".mrf") and
75+
not self.input_file.endswith(".src")))):
76+
# We need a SRC/SRF/MRF file to get the fault geometry
7677
return
7778

7879
install = InstallCfg.getInstance()
@@ -99,14 +100,14 @@ def run(self):
99100
"%d.plot_map.log" % (self.sim_id))
100101
trace_file = "%s.trace" % (a_input_file)
101102
simple_station_file = "%s.simple" % (a_station_file)
102-
if self.input_file.endswith(".srf"):
103+
if self.input_file.endswith(".srf") or self.input_file.endswith(".mrf"):
103104
self.trace = plot_utils.write_fault_trace(a_input_file, trace_file)
104105
else:
105106
self.trace = plot_utils.write_simple_trace(a_input_file, trace_file)
106107
plot_utils.write_simple_stations(a_station_file, simple_station_file)
107108
map_prefix = os.path.join(a_outdir, "station_map")
108109

109-
# Get hypo_lon, hypo_lat from src/srf file
110+
# Get hypo_lon, hypo_lat from src/srf/mrf file
110111
hypo_coord = {}
111112
hypo_lon, hypo_lat = fault_utils.calculate_epicenter(a_input_file)
112113
hypo_coord['lat'] = hypo_lat

bbp/comps/plot_map_gof.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
"""
33
BSD 3-Clause License
44
5-
Copyright (c) 2021, University of Southern California
5+
Copyright (c) 2024, University of Southern California
66
All rights reserved.
77
88
Redistribution and use in source and binary forms, with or without
@@ -130,24 +130,25 @@ def read_resid(resid_file, period, summary_output):
130130
# Return the data we found
131131
return sta_x_data, sta_y_data, sta_resid_data
132132

133-
def plot_map_gof(r_srcfile, r_stations, resid_file, comp_label, sim_id):
133+
def plot_map_gof(source_file, r_stations, resid_file, comp_label, sim_id):
134134
"""
135135
Reads data from resid_file and plots a map gof plot with a number
136136
of periods
137137
"""
138138
# Make sure we have a src or srf file
139-
if (r_srcfile is None or r_srcfile == "" or
140-
(not r_srcfile.endswith(".srf") and
141-
not r_srcfile.endswith(".src"))):
142-
# We need a SRC or SRF file to get the fault geometry
139+
if (source_file is None or source_file == "" or
140+
(not source_file.endswith(".srf") and
141+
(not source_file.endswith(".mrf")) and
142+
not source_file.endswith(".src"))):
143+
# We need a SRC/SRF/MRF file to get the fault geometry
143144
return
144145

145146
# Get directory names
146147
install = InstallCfg.getInstance()
147148
a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id))
148149
a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
149150

150-
a_input_file = os.path.join(a_indir, r_srcfile)
151+
a_input_file = os.path.join(a_indir, source_file)
151152
a_station_file = os.path.join(a_indir, r_stations)
152153

153154
# Define boundaries to plot using the stations in the station file
@@ -157,7 +158,7 @@ def plot_map_gof(r_srcfile, r_stations, resid_file, comp_label, sim_id):
157158
trace_file = "%s.trace" % (a_input_file)
158159
simple_station_file = "%s.simple" % (a_station_file)
159160

160-
if r_srcfile.endswith(".srf"):
161+
if source_file.endswith(".srf") or source_file.endswith(".mrf"):
161162
plot_utils.write_fault_trace(a_input_file, trace_file)
162163
else:
163164
plot_utils.write_simple_trace(a_input_file, trace_file)

bbp/comps/plot_srf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
"""
33
BSD 3-Clause License
44
5-
Copyright (c) 2021, University of Southern California
5+
Copyright (c) 2024, University of Southern California
66
All rights reserved.
77
88
Redistribution and use in source and binary forms, with or without
@@ -537,7 +537,7 @@ def run(r_srffile, sim_id=0):
537537

538538
# Get number of segments
539539
num_segments = get_srf_num_segments(r_srffile)
540-
srfbase = r_srffile[0:r_srffile.find(".srf")]
540+
srfbase = os.path.splitext(r_srffile)[0]
541541

542542
# Write slip and tinit files for each segment
543543
for seg in range(num_segments):

bbp/comps/plot_utils.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
BSD 3-Clause License
44
5-
Copyright (c) 2023, University of Southern California
5+
Copyright (c) 2024, University of Southern California
66
All rights reserved.
77
88
Redistribution and use in source and binary forms, with or without
@@ -50,77 +50,77 @@
5050
BUFFER_LATITUDE = 0.25
5151
BUFFER_LONGITUDE = 0.25
5252

53-
def get_srf_num_segments(srf_file):
53+
def get_srf_num_segments(input_file):
5454
"""
55-
Returns number of segments in a SRF file
55+
Returns number of segments in a SRF/MRF file
5656
"""
57-
srf_segments = None
57+
num_segments = None
5858

59-
srf = open(srf_file, 'r')
59+
srf = open(input_file, 'r')
6060
for line in srf:
6161
if line.startswith("PLANE"):
6262
# Found the plane line, read number of segments
63-
srf_segments = int(line.split()[1])
63+
num_segments = int(line.split()[1])
6464
break
6565
srf.close()
6666

67-
if srf_segments is None:
67+
if num_segments is None:
6868
print("ERROR: Could not read number of segments from "
69-
"SRF file: %s" % (src_file))
69+
"input file: %s" % (input_file))
7070
sys.exit(1)
7171

7272
# Return number of segments
73-
return srf_segments
73+
return num_segments
7474

75-
def get_srf_params(srf_file, segment=0):
75+
def get_srf_params(input_file, segment=0):
7676
"""
77-
Reads fault_len, width, dlen, dwid, and azimuth from the srf_file
77+
Reads fault_len, width, dlen, dwid, and azimuth from the input_file
7878
Segment allows users to specify segment of interest (0-based)
7979
"""
80-
srf_params1 = None
81-
srf_params2 = None
82-
srf = open(srf_file, 'r')
80+
param_line1 = None
81+
param_line2 = None
82+
srf = open(input_file, 'r')
8383
for line in srf:
8484
if line.startswith("PLANE"):
8585
# Found the plane line, read number of segments
86-
srf_segments = int(line.split()[1])
87-
if srf_segments < segment + 1:
86+
num_segments = int(line.split()[1])
87+
if num_segments < segment + 1:
8888
print("ERROR: Requested parameters from segment %d, "
89-
" SRF file only has %d segment(s)!" %
90-
(segment + 1, srf_segments))
89+
" SRF/MRF file only has %d segment(s)!" %
90+
(segment + 1, num_segments))
9191
sys.exit(1)
9292
for _ in range(segment):
9393
# Skip lines to get to the segment we want
9494
_ = next(srf)
9595
_ = next(srf)
9696
# The next line should have what we need
97-
srf_params1 = next(srf)
98-
srf_params2 = next(srf)
97+
param_line1 = next(srf)
98+
param_line2 = next(srf)
9999
break
100100
srf.close()
101-
if srf_params1 is None or srf_params2 is None:
102-
print("ERROR: Cannot determine parameters from SRF file %s" %
103-
(srf_file))
101+
if param_line1 is None or param_line2 is None:
102+
print("ERROR: Cannot determine parameters from SRF/MRF file %s" %
103+
(input_file))
104104
sys.exit(1)
105-
srf_params1 = srf_params1.strip()
106-
srf_params1 = srf_params1.split()
107-
srf_params2 = srf_params2.strip()
108-
srf_params2 = srf_params2.split()
105+
param_line1 = param_line1.strip()
106+
param_line1 = param_line1.split()
107+
param_line2 = param_line2.strip()
108+
param_line2 = param_line2.split()
109109
# Make sure we have the correct number of pieces
110-
if len(srf_params1) != 6 or len(srf_params2) != 5:
111-
print("ERROR: Cannot parse params from SRF file %s" %
112-
(srf_file))
110+
if len(param_line1) != 6 or len(param_line2) != 5:
111+
print("ERROR: Cannot parse params from SRF/MRF file %s" %
112+
(input_file))
113113
sys.exit(1)
114114

115115
# Pick the parameters that we need
116116
params = {}
117-
params["lon"] = float(srf_params1[0])
118-
params["lat"] = float(srf_params1[1])
119-
params["dim_len"] = int(srf_params1[2])
120-
params["dim_wid"] = int(srf_params1[3])
121-
params["fault_len"] = float(srf_params1[4])
122-
params["fault_width"] = float(srf_params1[5])
123-
params["azimuth"] = int(float(srf_params2[0]))
117+
params["lon"] = float(param_line1[0])
118+
params["lat"] = float(param_line1[1])
119+
params["dim_len"] = int(param_line1[2])
120+
params["dim_wid"] = int(param_line1[3])
121+
params["fault_len"] = float(param_line1[4])
122+
params["fault_width"] = float(param_line1[5])
123+
params["azimuth"] = int(float(param_line2[0]))
124124

125125
return params
126126

@@ -138,22 +138,23 @@ def write_simple_stations(station_file, out_file):
138138

139139
def get_srf_info(srf_file):
140140
"""
141-
This function reads a SRF file and returns version,
141+
This function reads a SRF/MRF file and returns version,
142142
number of segments, and a list with the nstk values
143143
for each segment
144144
"""
145145
version = None
146146
num_segments = None
147147
nstk = []
148148

149-
# Read SRF file
149+
# Read SRF/MRF file
150150
input_file = open(srf_file, 'r')
151151
for line in input_file:
152152
line = line.strip()
153153
# Skip blank lines
154154
if not line:
155155
continue
156-
version = int(float(line))
156+
# First line should contain version number
157+
version = int(float(line.split()[0]))
157158
break
158159

159160
# Read number of segments
@@ -170,7 +171,7 @@ def get_srf_info(srf_file):
170171
break
171172

172173
if num_segments is None or version is None:
173-
bband_utils.ParameterError("Cannot parse SRF file!")
174+
bband_utils.ParameterError("Cannot parse SRF/MRF file!")
174175

175176
# Read nstk for each segment
176177
for line in input_file:
@@ -188,13 +189,13 @@ def get_srf_info(srf_file):
188189

189190
input_file.close()
190191
if len(nstk) != num_segments:
191-
bband_utils.ParameterError("Cannot read nstk from SRF file!")
192+
bband_utils.ParameterError("Cannot read nstk from SRF/MRF file!")
192193

193194
return version, num_segments, nstk
194195

195196
def read_srf_trace(srf_file, num_segment, nstk):
196197
"""
197-
This function reads an SRF file and returns the
198+
This function reads an SRF/MRF file and returns the
198199
top layer trace for the segment specified
199200
"""
200201
install = InstallCfg.getInstance()
@@ -230,12 +231,12 @@ def read_srf_trace(srf_file, num_segment, nstk):
230231

231232
def write_fault_trace(srf_file, out_file):
232233
"""
233-
This function reads the srf file and outputs a trace file
234+
This function reads the srf/mrf file and outputs a trace file
234235
"""
235236
all_points = []
236237

237-
# Figure out SRF file version
238-
version, num_segments , nstk = get_srf_info(srf_file)
238+
# Figure out SRF/MRF file version
239+
version, num_segments, nstk = get_srf_info(srf_file)
239240

240241
# Reads the points for each segment
241242
for segment in range(0, num_segments):
@@ -413,8 +414,8 @@ def set_boundaries_from_stations(station_file, a_input_file):
413414
if a_input_file.endswith(".src"):
414415
# Read fault information from SRC file
415416
lat1, lon1, _, _, lat2, lon2 = calculate_fault_edges_from_src(a_input_file)
416-
elif a_input_file.endswith(".srf"):
417-
# Read fault information from SRF file
417+
elif a_input_file.endswith(".srf") or a_input_file.endswith(".mrf"):
418+
# Read fault information from SRF/MRF file
418419
lat1, lon1, lat2, lon2 = calculate_fault_edges_from_srf(a_input_file)
419420
else:
420421
bband_utils.ParameterError("Cannot determine input_file format!")

0 commit comments

Comments
 (0)