20
20
plt .switch_backend ('agg' ) # NOQA
21
21
22
22
from pyasdf import ASDFDataSet
23
- from spaceweight import SpherePoint
24
- from spaceweight import SphereDistRel
25
23
from pytomo3d .adjoint .sum_adjoint import check_events_consistent
24
+ from pytomo3d .window .window_weights import determine_receiver_weighting , \
25
+ determine_category_weighting
26
26
from pypaw .bins .utils import load_json , dump_json , load_yaml
27
27
28
28
@@ -147,125 +147,6 @@ def extract_source_location(input_info):
147
147
return src_info
148
148
149
149
150
- def _receiver_validator (weights , rec_wcounts , cat_wcounts ):
151
-
152
- for comp in weights :
153
- wsum = 0
154
- for trace_id , trace_weight in weights [comp ].iteritems ():
155
- nwin = rec_wcounts [trace_id ]
156
- wsum += trace_weight * nwin
157
-
158
- if not np .isclose (wsum , cat_wcounts [comp ]):
159
- raise ValueError ("receiver validator fails: %f, %f" %
160
- (wsum , cat_wcounts [comp ]))
161
-
162
-
163
- def determine_receiver_weighting (src , stations , windows , max_ratio = 0.35 ,
164
- flag = True , plot = False ,
165
- figname_prefix = None ):
166
- """
167
- Given one station and window information, determine the receiver
168
- weighting
169
- In one asdf file, there are still 3 components, for example,
170
- ["BHR", "BHT", "BHZ"]. These three components should be treated
171
- indepandently and weights will be calculated independantly.
172
-
173
- :return: dict of weights which contains 3 components. Each components
174
- contains weights values
175
- """
176
- center = SpherePoint (src ["latitude" ], src ["longitude" ],
177
- tag = "source" )
178
-
179
- # extract window information
180
- weights = {}
181
- rec_wcounts = {}
182
- cat_wcounts = defaultdict (lambda : 0 )
183
- for sta , sta_window in windows .iteritems ():
184
- for chan , chan_win in sta_window .iteritems ():
185
- comp = chan .split ("." )[- 1 ]
186
- _nwin = len (chan_win )
187
- if _nwin == 0 :
188
- continue
189
- weights .setdefault (comp , {}).update ({chan : 0.0 })
190
- rec_wcounts [chan ] = _nwin
191
- cat_wcounts [comp ] += _nwin
192
-
193
- # in each components, calculate weight
194
- ref_dists = {}
195
- cond_nums = {}
196
- for comp , comp_info in weights .iteritems ():
197
- points = []
198
- logger .info ("Components:%s" % comp )
199
- for chan in comp_info :
200
- _comp = chan [- 1 ]
201
- if _comp == "Z" :
202
- point = SpherePoint (stations [chan ]["latitude" ],
203
- stations [chan ]["longitude" ],
204
- tag = chan , weight = 1.0 )
205
- else :
206
- # for R and T component. In station file, there
207
- # are only E and N component. So we need transfer
208
- echan = chan [:- 1 ] + "E"
209
- chan1 = chan [:- 1 ] + "1"
210
- zchan = chan [:- 1 ] + "Z"
211
- if echan in stations :
212
- point = SpherePoint (stations [echan ]["latitude" ],
213
- stations [echan ]["longitude" ],
214
- tag = chan , weight = 1.0 )
215
- elif chan1 in stations :
216
- point = SpherePoint (stations [chan1 ]["latitude" ],
217
- stations [chan1 ]["longitude" ],
218
- tag = chan , weight = 1.0 )
219
- elif zchan in stations :
220
- point = SpherePoint (stations [zchan ]["latitude" ],
221
- stations [zchan ]["longitude" ],
222
- tag = chan , weight = 1.0 )
223
- points .append (point )
224
-
225
- if flag :
226
- # calculate weight; otherwise, leave it as default value(1)
227
- weightobj = SphereDistRel (points , center = center )
228
- scan_figname = figname_prefix + ".%s.smart_scan.png" % comp
229
- ref_dists [comp ], cond_nums [comp ] = weightobj .smart_scan (
230
- max_ratio = max_ratio , start = 0.5 , gap = 0.5 ,
231
- drop_ratio = 0.95 , plot = plot ,
232
- figname = scan_figname )
233
- if plot :
234
- figname = figname_prefix + ".%s.weight.png" % comp
235
- weightobj .plot_global_map (figname = figname , lon0 = 180.0 )
236
- else :
237
- ref_dists [comp ] = None
238
- cond_nums [comp ] = None
239
-
240
- wsum = 0
241
- for point in points :
242
- nwin = rec_wcounts [point .tag ]
243
- wsum += point .weight * nwin
244
- norm_factor = cat_wcounts [comp ] / wsum
245
-
246
- for point in points :
247
- weights [comp ][point .tag ] = point .weight * norm_factor
248
-
249
- _receiver_validator (weights , rec_wcounts , cat_wcounts )
250
-
251
- return {"rec_weights" : weights , "rec_wcounts" : rec_wcounts ,
252
- "cat_wcounts" : cat_wcounts , "rec_ref_dists" : ref_dists ,
253
- "rec_cond_nums" : cond_nums }
254
-
255
-
256
- def _category_validator (weights , counts ):
257
- wsum = 0.0
258
- nwins = 0
259
- for p , pinfo in weights .iteritems ():
260
- for c in pinfo :
261
- wsum += weights [p ][c ] * counts [p ][c ]
262
- nwins += counts [p ][c ]
263
-
264
- if not np .isclose (wsum , nwins ):
265
- raise ValueError ("Category validator fails: %f, %f" %
266
- (wsum , nwins ))
267
-
268
-
269
150
def check_cat_consistency (cat_ratio , cat_wcounts ):
270
151
err = 0
271
152
# check consistency
@@ -281,38 +162,6 @@ def check_cat_consistency(cat_ratio, cat_wcounts):
281
162
"consistent with window information" )
282
163
283
164
284
- def determine_category_weighting (weight_param , cat_wcounts ):
285
- """
286
- determine the category weighting based on window counts in each category
287
- """
288
- logger_block ("Category Weighting" )
289
- weights = {}
290
-
291
- cat_ratio = weight_param ["ratio" ]
292
-
293
- print ("cat_ratio: %s" % cat_ratio )
294
- print ("cat_wcounts: %s" % cat_wcounts )
295
- sumv = 0
296
- nwins = 0
297
- for p , pinfo in cat_wcounts .iteritems ():
298
- for c in pinfo :
299
- sumv += cat_wcounts [p ][c ] / cat_ratio [p ][c ]
300
- nwins += cat_wcounts [p ][c ]
301
-
302
- normc = nwins / sumv
303
- logger .info ("Total number of windows: %d" % nwins )
304
-
305
- weights = {}
306
- for p , pinfo in cat_wcounts .iteritems ():
307
- weights [p ] = {}
308
- for c in pinfo :
309
- weights [p ][c ] = normc / cat_ratio [p ][c ]
310
-
311
- logger .info ("Category weights: %s" % weights )
312
- _category_validator (weights , cat_wcounts )
313
- return weights
314
-
315
-
316
165
def plot_histogram (figname , array , nbins = 50 ):
317
166
# plot histogram of weights
318
167
plt .hist (array , nbins )
@@ -529,9 +378,9 @@ def calculate_receiver_weights_asdf(self, period_info, weighting_param):
529
378
530
379
_results = determine_receiver_weighting (
531
380
self .src_info , station_info , window_info ,
532
- max_ratio = search_ratio ,
533
- flag = weight_flag ,
534
- plot = plot_flag , figname_prefix = figname_prefix )
381
+ search_ratio = search_ratio ,
382
+ weight_flag = weight_flag ,
383
+ plot_flag = plot_flag , figname_prefix = figname_prefix )
535
384
536
385
return _results
537
386
0 commit comments