forked from SeisComP/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLcan.cpp
415 lines (332 loc) · 12.8 KB
/
MLcan.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* ################################################################################
* # Copyright (C) 2024 by IGN Spain #
* # #
* # author: J. Barco, E. Suarez #
* # email: [email protected] , [email protected] #
* # last modified: 2024-03-20 #
* # #
* # This program is free software; you can redistribute it and/or modify #
* # it under the terms of the GNU General Public License as published by #
* # the Free Software Foundation; either version 2 of the License, or #
* # (at your option) any later version. #
* # #
* # This program is distributed in the hope that it will be useful, #
* # but WITHOUT ANY WARRANTY; without even the implied warranty of #
* # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
* # GNU General Public License for more details. #
* # #
* # You should have received a copy of the GNU General Public License #
* # along with this program; if not, write to the #
* # Free Software Foundation, Inc., #
* # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
* ################################################################################ */
#define SEISCOMP_COMPONENT MLcan
#include <seiscomp/math/geo.h>
#include <seiscomp/processing/magnitudes/MLcan.h>
#include <seiscomp/config/config.h>
#include <seiscomp/logging/log.h>
#include <seiscomp/client/inventory.h>
#include <seiscomp/datamodel/amplitude.h>
using namespace std;
namespace Seiscomp {
namespace Processing {
namespace {
std::string ExpectedAmplitudeUnit = "mm";
DEFINE_SMARTPOINTER(ExtraLocale);
class ExtraLocale : public Core::BaseObject {
public:
// general magnitude parameters
OPT(string) distanceMode;
OPT(string) calibrationType;
// parametric coefficients
OPT(double) c0_first;
OPT(double) c0_second;
OPT(double) c1;
OPT(double) c2;
OPT(double) c3;
OPT(double) c4;
OPT(double) c5;
// A0, non-parametric coefficients
OPT(LogA0) logA0;
};
}
REGISTER_MAGNITUDEPROCESSOR(MagnitudeProcessor_MLcan, "MLcan");
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
MagnitudeProcessor_MLcan::MagnitudeProcessor_MLcan()
: Processing::MagnitudeProcessor("MLcan") {}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
std::string MagnitudeProcessor_MLcan::amplitudeType() const {
return MagnitudeProcessor::amplitudeType();
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
bool MagnitudeProcessor_MLcan::setup(const Settings &settings) {
if ( !MagnitudeProcessor::setup(settings) ) {
return false;
}
try {_maxDepth = settings.getDouble("magnitudes." + _type + ".maxDepth"); }
catch ( ... ) {}
// distance constraints
try {_distanceMode = settings.getString("magnitudes." + _type + ".distMode"); }
catch ( ... ) {}
try {_minDistanceKm = Math::Geo::deg2km(settings.getDouble("magnitudes." + _type + ".minDist")); }
catch ( ... ) {}
try {_maxDistanceKm = Math::Geo::deg2km(settings.getDouble("magnitudes." + _type + ".maxDist")); }
catch ( ... ) {}
// calibration function
try {_calibrationType = settings.getString("magnitudes." + _type + ".calibrationType"); }
catch ( ... ) {}
if ( (_calibrationType != "A0") && (_calibrationType != "parametric") ) {
SEISCOMP_ERROR("%s: unrecognized calibration type: %s", _type.c_str(),
_calibrationType.c_str());
return false;
}
// parametric calibration function
try { _c0_first = settings.getDouble("magnitudes." + _type + ".parametric.c0_first"); }
catch ( ... ) {}
try { _c0_second = settings.getDouble("magnitudes." + _type + ".parametric.c0_second"); }
catch ( ... ) {}
try { _c1 = settings.getDouble("magnitudes." + _type + ".parametric.c1"); }
catch ( ... ) {}
try { _c2 = settings.getDouble("magnitudes." + _type + ".parametric.c2"); }
catch ( ... ) {}
try { _c3 = settings.getDouble("magnitudes." + _type + ".parametric.c3"); }
catch ( ... ) {}
try { _c4 = settings.getDouble("magnitudes." + _type + ".parametric.c4"); }
catch ( ... ) {}
try { _c5 = settings.getDouble("magnitudes." + _type + ".parametric.c5"); }
catch ( ... ) {}
// A0, non-parametric calibration function
std::string defLogA0 = "0:-1.3,60:-2.8,100:-3.0,400:-4.5,1000:-5.85";
bool logA0Default = true;
try {
defLogA0 = settings.getString("magnitudes." + _type + ".A0.logA0");
logA0Default = false;
}
catch ( ... ) {}
// set distance-dependent intervals
if ( !_logA0.set(defLogA0) ) {
SEISCOMP_ERROR("%s: incorrect correction term log(A0): %s", _type.c_str(),
defLogA0.c_str());
return false;
}
if ( !logA0Default ) {
SEISCOMP_DEBUG("%s.%s: %s: logA0 from bindings = %s",
settings.networkCode, settings.stationCode,
type(), Core::toString(_logA0));
}
return true;
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
bool MagnitudeProcessor_MLcan::initLocale(Locale *locale,
const Settings &settings,
const string &configPrefix) {
const Seiscomp::Config::Config *cfg = settings.localConfiguration;
ExtraLocalePtr extra = new ExtraLocale;
// general
try {
extra->distanceMode = cfg->getString(configPrefix + "distMode");
}
catch ( ... ) {}
try {
extra->calibrationType = cfg->getString(configPrefix + "calibrationType");
}
catch ( ... ) {}
// parametric
try {
extra->c0_first = cfg->getDouble(configPrefix + ".parametric.c0_first");
}
catch ( ... ){}
try {
extra->c0_second = cfg->getDouble(configPrefix + ".parametric.c0_second");
}
catch ( ... ) {}{}
try {
extra->c1 = cfg->getDouble(configPrefix + "parametric.c1");
}
catch ( ... ) {}
try {
extra->c2 = cfg->getDouble(configPrefix + "parametric.c2");
}
catch ( ... ) {}
try {
extra->c3 = cfg->getDouble(configPrefix + "parametric.c3");
}
catch ( ... ) {}
try {
extra->c4 = cfg->getDouble(configPrefix + "parametric.c4");
}
catch ( ... ) {}
try {
extra->c5 = cfg->getDouble(configPrefix + "parametric.c5");
}
catch ( ... ) {}
// A0, non-parametric
try {
auto logA0 = cfg->getStrings(configPrefix + "A0.logA0");
if ( !logA0.empty() ) {
// If the first item contains a comma, then maybe it has been configured
// with double quotes. Raise an error.
if ( logA0[0].find(',') != string::npos ) {
SEISCOMP_ERROR("%sA0.logA0[0] contains a comma. Are the coefficients "
"enclosed with double quotes in the configuration?",
configPrefix);
return false;
}
if ( logA0[0].find(';') != string::npos ) {
SEISCOMP_ERROR("%sA0.logA0 = %s contains semicolon. Supported format:"
" distance1:correction1,distance2:correction2, ...",
configPrefix, Core::toString(logA0).c_str());
return false;
}
extra->logA0 = LogA0();
if ( !extra->logA0->set(logA0) ) {
SEISCOMP_ERROR("%s@%s: incorrect correction term log(A0)",
_type.c_str(), locale->name.c_str());
return false;
}
}
}
catch ( ... ) {}
locale->extra = extra;
return true;
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
double MagnitudeProcessor_MLcan::getChanCorr(const double c0_first, const double c0_second, std::string ChanelCode){
double chanCorr = 0;
string component = "";
component = ChanelCode.back();
if ((component == "1") || (component == "N")){
chanCorr = c0_first;
}
if ((component == "2") || (component == "E")){
chanCorr = c0_second;
}
else{
chanCorr = (c0_first + c0_second)/2;
}
return chanCorr;
};
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
MagnitudeProcessor::Status MagnitudeProcessor_MLcan::computeMagnitude(
double amplitude, const std::string &unit,
double, double, double delta, double depth,
const DataModel::Origin *,
const DataModel::SensorLocation *receiver,
const DataModel::Amplitude *new_amplitude,
const Locale *locale,
double &value) {
if ( amplitude <= 0 ) {
return AmplitudeOutOfRange;
}
auto distanceMode = _distanceMode;
auto calibrationType = _calibrationType;
auto minimumDistanceKm = _minDistanceKm;
auto maximumDistanceKm = _maxDistanceKm;
auto maximumDepth = _maxDepth;
ExtraLocale *extra = nullptr;
if ( locale ) {
extra = static_cast<ExtraLocale*>(locale->extra.get());
if ( extra ) {
if ( extra->distanceMode ) {
distanceMode = *extra->distanceMode;
}
if ( extra->calibrationType ) {
calibrationType = *extra->calibrationType;
}
}
if ( locale->minimumDistance ) {
minimumDistanceKm = Math::Geo::deg2km(*locale->minimumDistance);
}
if ( locale->maximumDistance ) {
maximumDistanceKm = Math::Geo::deg2km(*locale->maximumDistance);
}
}
else {
SEISCOMP_DEBUG(" + maximum depth: %.3f km", maximumDepth);
if ( depth > maximumDepth ) {
return DepthOutOfRange;
}
SEISCOMP_DEBUG(" + minimum distance: %.3f km", minimumDistanceKm);
SEISCOMP_DEBUG(" + maximum distance: %.3f km", maximumDistanceKm);
}
SEISCOMP_DEBUG(" + distance type: %s", distanceMode.c_str());
double hDistanceKm = Math::Geo::deg2km(delta);
double vDistanceKm = 0;
if ( !receiver && distanceMode == "hypocentral" ) {
return MetaDataRequired;
}
if ( receiver ) {
vDistanceKm = receiver->elevation() / 1000. + depth;
}
double distanceKm;
if ( distanceMode == "hypocentral" ) {
distanceKm = sqrt(pow(hDistanceKm, 2) + pow(vDistanceKm, 2));
}
else {
distanceKm = hDistanceKm;
}
SEISCOMP_DEBUG(" + considered distance to station: %.3f km", distanceKm);
if ( minimumDistanceKm >= 0 && distanceKm < minimumDistanceKm ) {
return DistanceOutOfRange;
}
if ( maximumDistanceKm >= 0 && distanceKm > maximumDistanceKm ) {
return DistanceOutOfRange;
}
if ( !convertAmplitude(amplitude, unit, ExpectedAmplitudeUnit) ) {
return InvalidAmplitudeUnit;
}
SEISCOMP_DEBUG(" + %s distance: %.3f km (horiz. %.3f vert. %.3f)",
distanceMode.c_str(), distanceKm, hDistanceKm, vDistanceKm);
double correction;
SEISCOMP_DEBUG(" + calibration type: %s", calibrationType.c_str());
if ( calibrationType == "parametric" ) {
// parametric calibration function
double c0 = 0;
// parametric calibration function
auto c0_first = (extra and extra->c0_first) ? *extra->c0_first : _c0_first;
auto c0_second = (extra and extra->c0_second) ? *extra->c0_second : _c0_second;
auto c1 = (extra and extra->c1) ? *extra->c1 : _c1;
auto c2 = (extra and extra->c2) ? *extra->c2 : _c2;
auto c3 = (extra and extra->c3) ? *extra->c3 : _c3;
auto c4 = (extra and extra->c4) ? *extra->c4 : _c4;
auto c5 = (extra and extra->c5) ? *extra->c5 : _c5;
// extract the channel code and the location code from the amplitude
// as strings
std::string channelCode = new_amplitude->waveformID().channelCode();
std::string locationCode = new_amplitude->waveformID().locationCode();
c0 = getChanCorr(c0_first, c0_second, channelCode);
SEISCOMP_DEBUG(" + Location: %s", locationCode);
SEISCOMP_DEBUG(" + Channel: %s", channelCode);
SEISCOMP_DEBUG(" + c0 - c5: %.5f %.5f %.5f %.5f %.5f %.5f",
c0, c1, c2, c3, c4, c5);
correction = c3 * log10(distanceKm / c5)
+ c2 * (distanceKm + c4)
+ c1
+ c0;
value = log10(amplitude) + correction;
}
else if ( calibrationType == "A0" ) {
// A0, non-parametric calibration function
try {
correction = -1.0 * (extra and extra->logA0 ? extra->logA0->at(distanceKm) : _logA0.at(distanceKm));
SEISCOMP_DEBUG(" + -log10(A0) at %.3f km: %.5f", distanceKm, correction);
value = log10(amplitude) + correction;
}
catch ( std::out_of_range & ) {
return DistanceOutOfRange;
}
}
else {
return IncompleteConfiguration;
}
SEISCOMP_DEBUG(" + amplitude: %.5f, magnitude: %.3f", amplitude, value);
return OK;
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
}