@@ -2998,7 +2998,7 @@ def hypoDist(self, coords):
2998
2998
def calculateStationMagnitudes (self ):
2999
2999
event = self .catalog [0 ]
3000
3000
origin = event .origins [0 ]
3001
- event . station_magnitudes = []
3001
+ station_magnitudes = []
3002
3002
3003
3003
netstaloc = set ([(amp .waveform_id .network_code ,
3004
3004
amp .waveform_id .station_code ,
@@ -3011,7 +3011,22 @@ def calculateStationMagnitudes(self):
3011
3011
pazs = []
3012
3012
channels = []
3013
3013
p2ps = []
3014
- for amplitude in self .getAmplitudes (net , sta , loc ):
3014
+ amplitudes_ = self .getAmplitudes (net , sta , loc )
3015
+
3016
+ # check if we can reuse an existing StationMagnitude
3017
+ amp_ids = set (str (amp .resource_id ) for amp in amplitudes_ )
3018
+ for sm in event .station_magnitudes :
3019
+ if sm .get ('_amplitude_ids' ) == amp_ids :
3020
+ break
3021
+ else :
3022
+ sm = None
3023
+ if sm is not None :
3024
+ station_magnitudes .append (sm )
3025
+ self .critical ('reusing existing magnitude for %s: %0.2f' % (
3026
+ sta , sm .mag ))
3027
+ continue
3028
+
3029
+ for amplitude in amplitudes_ :
3015
3030
self .debug (str (amplitude ))
3016
3031
timedelta = amplitude .get_timedelta ()
3017
3032
self .debug ("Timedelta: " + str (timedelta ))
@@ -3046,7 +3061,10 @@ def calculateStationMagnitudes(self):
3046
3061
dist = self .hypoDist (tr .stats .coordinates )
3047
3062
mag = estimate_magnitude (pazs , p2ps , timedeltas , dist )
3048
3063
sm = StationMagnitude ()
3049
- event .station_magnitudes .append (sm )
3064
+ # store ids of used amplitudes, so we're able to tell when a
3065
+ # station magnitude needs to be updated
3066
+ sm ._amplitude_ids = amp_ids
3067
+ station_magnitudes .append (sm )
3050
3068
sm .origin_id = origin .resource_id
3051
3069
sm .method_id = "/" .join (
3052
3070
[ID_ROOT , "station_magnitude_method" , "obspyck" , "2" ])
@@ -3062,8 +3080,16 @@ def calculateStationMagnitudes(self):
3062
3080
extra .amplitudeIDs = {'value' : "," .join ([str (a .resource_id )
3063
3081
for a in amplitudes ]),
3064
3082
'namespace' : NAMESPACE }
3083
+ # need to do this at the very end when StationMagnitude is not
3084
+ # changed anymore, and we need to avoid setting a new random ID
3085
+ # automatically (in event_helper.py)
3086
+ for amplitude in amplitudes :
3087
+ super (Amplitude , amplitude ).__setattr__ (
3088
+ '_station_magnitude_id' , str (sm .resource_id ))
3065
3089
self .critical ('calculated new magnitude for %s: %0.2f (channels: %s)' % (
3066
3090
sta , mag , "," .join (channels )))
3091
+ # finally switch out station magnitudes
3092
+ event .station_magnitudes = station_magnitudes
3067
3093
3068
3094
#see http://www.scipy.org/Cookbook/LinearRegression for alternative routine
3069
3095
#XXX replace with drawWadati()
0 commit comments