@@ -114,15 +114,26 @@ def read_log(log):
114
114
The log file follows the ECD format
115
115
'''
116
116
117
- lines = open (log , 'r' ).readlines ()
117
+ # Fix log file for GSIMs tailored for specific regions
118
+ fix_gsims (log )
118
119
119
- # Get calculation details
120
+ # Read lines of log file
121
+ with open (log , 'r' , encoding = "utf-8" ) as f :
122
+ lines = f .readlines ()
123
+
124
+ # Get calculation details from the description line
125
+ # (recording stations, gmlt, rupture, and optionally the max_distance)
120
126
calc_id = int (lines [0 ][8 :- 1 ])
121
- description = lines [1 ][17 :]
122
- rs , gmlt , rup = description .split (',' )[1 :]
123
- rs = rs .replace ('Stations:' , '' )
124
- gmlt = gmlt .replace ('gmlt:' , '' )
125
- rup = rup .replace ('Rupture:' , '' ).replace ('\n ' , '' )
127
+ description = lines [1 ][17 :].replace ('\n ' , '' ).strip ()
128
+ _ , rs , gmlt , rup , * max_dist = map (str .strip , description .split (',' )[0 :5 ])
129
+
130
+ rs = rs .replace ('Stations:' , '' ).strip ()
131
+ gmlt = gmlt .replace ('gmlt:' , '' ).strip ()
132
+ rup = rup .replace ('Rupture:' , '' ).strip ()
133
+ if max_dist :
134
+ max_dist = max_dist [0 ].replace ('Max_dist:' , '' ).strip ()
135
+ else :
136
+ max_dist = ''
126
137
127
138
# Get time
128
139
try :
@@ -132,44 +143,60 @@ def read_log(log):
132
143
# If no time is found, then the calculation has an error
133
144
raise Exception (f"Calculation with error. Check log file: { log } " )
134
145
135
- # Create DataFrame
146
+ # Create DataFrame columns
136
147
cols = ['calc_id' , 'description' , 'cal_time' ,
137
- 'recording_stations' , 'gmlt' , 'rupture' , 'gmpe' , 'imt' ,
138
- 'nominal_bias_mean' , 'nominal_bias_stdev' ]
139
- calc = []
148
+ 'recording_stations' , 'gmlt' , 'rupture' , 'gmpe' , 'max_distance' ,
149
+ 'imt' , 'nominal_bias_mean' , 'nominal_bias_stdev' ]
140
150
141
- # Get bias
142
- bias = [line for line in lines if 'Nominal bias' in line ]
143
- if bias :
151
+ # Initialize data list for DataFrame
152
+ calc = []
153
+
154
+ # Get bias information
155
+ bias_lines = [line for line in lines if 'Nominal bias' in line ]
156
+ if bias_lines :
144
157
# Calculation with station data
145
- for line in bias :
158
+ for line in bias_lines :
146
159
ini = line .find ('INFO]' )
147
160
if ini != - 1 :
148
161
line = line [ini + 6 :] # Remove info from initial paragraph
149
- gmpe = line .split (':' )[1 ].split (',' )[0 ].strip (' [] ' )
162
+ gmpe = line .split (':' )[1 ].split (',' )[0 ].strip (' [] ' )
150
163
imt = line .split (':' )[2 ].split (',' )[0 ].strip ()
151
164
bias_mean = float (line .split (':' )[3 ].split ("," )[0 ].replace ('\n ' , '' ).strip ())
152
165
bias_stdv = float (line .split (':' )[4 ].replace ('\n ' , '' ).strip ())
153
- vals = [calc_id , description , time , rs , gmlt , rup , gmpe , imt , bias_mean , bias_stdv ]
166
+ vals = [calc_id , description , time , rs , gmlt , rup , gmpe , max_dist , imt , bias_mean , bias_stdv ]
154
167
data = pd .DataFrame (dict (zip (cols , vals )), index = [0 ])
155
168
calc .append (data )
156
169
else :
157
- # Calculation without station data
170
+ # Calculation without station data (i.e., no bias information found)
158
171
gmpe = ''
159
172
imt = ''
173
+ max_dist = ''
160
174
bias_mean = np .nan
161
175
bias_stdv = np .nan
162
- vals = [calc_id , description , time , rs , gmlt , rup , gmpe , imt , bias_mean , bias_stdv ]
176
+ vals = [calc_id , description , time , rs , gmlt , rup , gmpe , max_dist , imt , bias_mean , bias_stdv ]
163
177
data = pd .DataFrame (dict (zip (cols , vals )), index = [0 ])
164
178
calc .append (data )
165
-
179
+
180
+ # Concatenate DataFrames
166
181
df = pd .concat (calc , ignore_index = True )
167
182
168
183
assert df .shape [0 ]> 0 , f'No data extracted from log file { log } '
169
184
170
185
return df , calc_id
171
186
172
187
188
+ def fix_gsims (log ):
189
+ """Fix log files that have GSIMS tailored for specific regions
190
+ """
191
+ with open (log , 'r' , encoding = "utf-8" ) as f :
192
+ content = f .read ()
193
+ # Replace the desired string
194
+ content = content .replace (']\n region = ' , '] region = ' )
195
+ with open (log , 'w' , encoding = "utf-8" ) as f :
196
+ f .write (content )
197
+ return
198
+
199
+
173
200
def plot_df (df , pad = None , ** kwargs ):
174
201
'''
175
202
Plot ground motion fields starting from a given DataFrame.
@@ -310,4 +337,4 @@ def get_imfd(df_log, gmfs):
310
337
for col in cols :
311
338
df .insert (0 , col , df_log .loc [0 , col ])
312
339
313
- return df
340
+ return df
0 commit comments