-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathseismogram.ts
1466 lines (1307 loc) · 38.1 KB
/
seismogram.ts
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Philip Crotwell
* University of South Carolina, 2019
* https://www.seis.sc.edu
*/
import { DateTime, Duration, Interval } from "luxon";
import { FDSNSourceId, NslcId } from "./fdsnsourceid";
import {
meanOfSlice, isDef, stringify,
checkLuxonValid, validStartTime, validEndTime, startDuration
} from "./util";
import * as seedcodec from "./seedcodec";
import { distaz, DistAzOutput } from "./distaz";
import { Network, Station, Channel, InstrumentSensitivity, findChannels } from "./stationxml";
import { Quake } from "./quakeml";
import { AMPLITUDE_MODE, MinMaxable } from "./scale";
import { SeismogramSegment } from "./seismogramsegment";
//export {SeismogramSegment} from "./seismogramsegment";
import type { TraveltimeJsonType, TraveltimeArrivalType } from "./traveltime";
export const COUNT_UNIT = "count";
export type HighLowType = {
xScaleDomain: Array<Date>;
xScaleRange: Array<number>;
secondsPerPixel: number;
samplesPerPixel: number;
highlowArray: Array<number>;
};
export type MarkerType = {
name: string;
time: DateTime;
markertype: string;
description: string;
link?: string;
};
export function isValidMarker(v: unknown): v is MarkerType {
if (!v || typeof v !== 'object') {
return false;
}
const m = v as Record<string, unknown>;
return typeof m.time === 'string' &&
typeof m.name === 'string' &&
typeof m.markertype === 'string' &&
typeof m.description === 'string' &&
(!("link" in m) || typeof m.link === 'string');
}
/**
* Represents time window for a single channel that may
* contain gaps or overlaps, but is otherwise more or less
* continuous, or at least adjacent data from the channel.
* Each segment within
* the Seismogram will have the same units, channel identifiers
* and sample rate, but cover different times.
*/
export class Seismogram {
_segmentArray: Array<SeismogramSegment>;
_interval: Interval;
_y: null | Int32Array | Float32Array | Float64Array;
constructor(segmentArray: SeismogramSegment | Array<SeismogramSegment>) {
this._y = null;
if (
Array.isArray(segmentArray) &&
segmentArray[0] instanceof SeismogramSegment
) {
this._segmentArray = segmentArray;
} else if (segmentArray instanceof SeismogramSegment) {
this._segmentArray = [segmentArray];
} else {
throw new Error(
`segmentArray is not Array<SeismogramSegment> or SeismogramSegment: ${stringify(
segmentArray,
)}`,
);
}
this.checkAllSimilar();
this._interval = this.findStartEnd();
checkLuxonValid(this._interval, "seis const");
}
checkAllSimilar() {
if (this._segmentArray.length === 0) {
throw new Error("Seismogram is empty");
}
const f = this._segmentArray[0];
this._segmentArray.forEach((s, i) => {
if (!s) {
throw new Error(`index ${i} is null in trace`);
}
this.checkSimilar(f, s);
});
}
checkSimilar(f: SeismogramSegment, s: SeismogramSegment) {
if (!s.sourceId.equals(f.sourceId)) {
throw new Error(`SourceId not same: ${s.sourceId.toString()} !== ${f.sourceId.toString()}`);
}
if (s.yUnit !== f.yUnit) {
throw new Error("yUnit not same: " + s.yUnit + " !== " + f.yUnit);
}
}
findStartEnd(): Interval {
if (this._segmentArray.length === 0) {
throw new Error("Seismogram is empty");
}
return this._segmentArray.reduce((acc, cur) => acc.union(cur.timeRange),
this._segmentArray[0].timeRange);
}
findMinMax(minMaxAccumulator?: MinMaxable): MinMaxable {
if (this._segmentArray.length === 0) {
throw new Error("No data");
}
for (const s of this._segmentArray) {
minMaxAccumulator = s.findMinMax(minMaxAccumulator);
}
if (minMaxAccumulator) {
return minMaxAccumulator;
} else {
throw new Error("No data to calc minmax");
}
}
/**
* calculates the mean of a seismogrma.
*
* @returns mean value
*/
mean(): number {
let meanVal = 0;
const npts = this.numPoints;
for (const s of this.segments) {
meanVal += meanOfSlice(s.y, s.y.length) * s.numPoints;
}
meanVal = meanVal / npts;
return meanVal;
}
get start(): DateTime {
return this.startTime;
}
get startTime(): DateTime {
return validStartTime(this._interval);
}
get end(): DateTime {
return this.endTime;
}
get endTime(): DateTime {
return validEndTime(this._interval);
}
get timeRange(): Interval {
return this._interval;
}
get networkCode(): string | null {
return this.sourceId.networkCode;
}
get stationCode(): string | null {
return this.sourceId.stationCode;
}
get locationCode(): string | null {
return this.sourceId.locationCode;
}
get channelCode(): string | null {
return this.sourceId.formChannelCode();
}
/**
* return FDSN source id as a string.
*
* @returns FDSN source id
*/
get sourceId(): FDSNSourceId {
return this._segmentArray[0].sourceId;
}
set sourceId(sid: FDSNSourceId) {
this._segmentArray.forEach(s => (s.sourceId = sid));
}
get sampleRate(): number {
return this._segmentArray[0].sampleRate;
}
get samplePeriod(): number {
return 1.0 / this.sampleRate;
}
get yUnit(): string | null {
return this._segmentArray[0].yUnit;
}
isYUnitCount(): boolean {
return this.yUnit?.toLowerCase() === COUNT_UNIT;
}
get numPoints(): number {
return this._segmentArray.reduce(
(accumulator, seis) => accumulator + seis.numPoints,
0,
);
}
hasCodes(): boolean {
return this._segmentArray[0].hasCodes();
}
/**
* return network, station, location and channels codes as one string.
* Uses this.channel if it exists, this.seismogram if not.
*
* @returns net.sta.loc.chan
*/
get nslc(): string {
return this.codes();
}
get nslcId(): NslcId {
return this._segmentArray[0].nslcId;
}
codes(): string {
return this._segmentArray[0].codes();
}
get segments(): Array<SeismogramSegment> {
return this._segmentArray;
}
append(seismogram: SeismogramSegment | Seismogram) {
if (seismogram instanceof Seismogram) {
seismogram._segmentArray.forEach(s => this.append(s));
} else {
this.checkSimilar(this._segmentArray[0], seismogram);
this._interval = this._interval.union(seismogram.timeRange);
this._segmentArray.push(seismogram);
}
}
/**
* Cut the seismogram. Creates a new seismogram with all datapoints
* contained in the time window.
*
* @param timeRange start and end of cut
* @returns new seismogram
*/
cut(timeRange: Interval): null | Seismogram {
// coarse trim first
let out = this.trim(timeRange);
if (out && out._segmentArray) {
const cutSeisArray = this._segmentArray
.map(seg => seg.cut(timeRange))
.filter(isDef);
if (cutSeisArray.length > 0) {
out = new Seismogram(cutSeisArray);
} else {
out = null;
}
} else {
out = null;
}
return out;
}
/**
* Creates a new Seismogram composed of all seismogram segments that overlap the
* given time window. If none do, this returns null. This is a faster but coarser
* version of cut as it only removes whole segments that do not overlap the
* time window. For most seismograms that consist of a single contiguous
* data segment, this will do nothing.
*
* @param timeRange time range to trim to
* @returns new seismogram if data in the window, null otherwise
* @see cut
*/
trim(timeRange: Interval): null | Seismogram {
let out = null;
checkLuxonValid(timeRange);
const timeRange_start = validStartTime(timeRange);
const timeRange_end = validEndTime(timeRange);
if (this._segmentArray) {
const trimSeisArray = this._segmentArray
.filter(function(d) {
return d.endTime >= timeRange_start;
})
.filter(function(d) {
return d.startTime <= timeRange_end;
});
if (trimSeisArray.length > 0) {
out = new Seismogram(trimSeisArray);
}
}
return out;
}
break(duration: Duration): void {
if (duration.valueOf() < 0) {
throw new Error(`Negative duration not allowed: ${duration.toString()}`);
}
if (this._segmentArray) {
let breakStart = this.startTime;
let out: Array<SeismogramSegment> = [];
while (breakStart < this.endTime) {
const breakWindow = Interval.after(breakStart, duration);
const cutSeisArray: Array<SeismogramSegment> =
this._segmentArray.map(seg => seg.cut(breakWindow)).filter(isDef);
out = out.concat(cutSeisArray);
breakStart = breakStart.plus(duration);
}
// check for null, filter true if seg not null
out = out.filter(isDef);
this._segmentArray = out;
}
}
isContiguous(): boolean {
if (this._segmentArray.length === 1) {
return true;
}
let prev = null;
for (const s of this._segmentArray) {
if (
prev &&
!(
prev.endTime < s.startTime &&
prev.endTime
.plus(Duration.fromMillis((1000 * 1.5) / prev.sampleRate))
> s.startTime
)
) {
return false;
}
prev = s;
}
return true;
}
/**
* Merges all segments into a single array of the same type as the first
* segment. No checking is done for gaps or overlaps, this is a simple
* congatination. Be careful!
*
* @returns contatenated data
*/
merge(): Int32Array | Float32Array | Float64Array {
let outArray: Int32Array | Float32Array | Float64Array;
let idx = 0;
if (this._segmentArray.every(seg => seg.y instanceof Int32Array)) {
outArray = new Int32Array(this.numPoints);
this._segmentArray.forEach(seg => {
outArray.set(seg.y, idx);
idx += seg.y.length;
});
} else if (this._segmentArray.every(seg => seg.y instanceof Float32Array)) {
outArray = new Float32Array(this.numPoints);
this._segmentArray.forEach(seg => {
outArray.set(seg.y, idx);
idx += seg.y.length;
});
} else if (this._segmentArray.every(seg => seg.y instanceof Float64Array)) {
outArray = new Float64Array(this.numPoints);
this._segmentArray.forEach(seg => {
outArray.set(seg.y, idx);
idx += seg.y.length;
});
} else {
throw new Error(
`data not all same one of Int32Array, Float32Array or Float64Array`,
);
}
return outArray;
}
/**
* Gets the timeseries as an typed array if it is contiguous.
*
* @throws {NonContiguousData} if data is not contiguous.
* @returns timeseries as array of number
*/
get y(): Int32Array | Float32Array | Float64Array {
if (!this._y) {
if (this.isContiguous()) {
this._y = this.merge();
}
}
if (this._y) {
return this._y;
} else {
throw new Error(
"Seismogram is not contiguous, access each SeismogramSegment idividually.",
);
}
}
set y(val: Int32Array | Float32Array | Float64Array) {
// ToDo
throw new Error("seismogram y setter not impl, see cloneWithNewData()");
}
clone(): Seismogram {
const cloned = this._segmentArray.map(s => s.clone());
return new Seismogram(cloned);
}
cloneWithNewData(newY: Int32Array | Float32Array | Float64Array): Seismogram {
if (newY && newY.length > 0) {
const seg = this._segmentArray[0].cloneWithNewData(newY);
return new Seismogram([seg]);
} else {
throw new Error("Y value is empty");
}
}
/**
* factory method to create a single segment Seismogram from either encoded data
* or a TypedArray, along with sample rate and start time.
*
* @param yArray array of encoded data or typed array
* @param sampleRate sample rate, samples per second of the data
* @param startTime time of first sample
* @param sourceId optional source id
* @returns seismogram initialized with the data
*/
static fromContiguousData(
yArray:
| Array<seedcodec.EncodedDataSegment>
| Int32Array
| Float32Array
| Float64Array,
sampleRate: number,
startTime: DateTime,
sourceId?: FDSNSourceId,
): Seismogram {
const seg = new SeismogramSegment(yArray, sampleRate, startTime, sourceId);
return new Seismogram([seg]);
}
}
export class NonContiguousData extends Error {
constructor(message?: string) {
super(message);
this.name = this.constructor.name;
}
}
export function ensureIsSeismogram(
seisSeismogram: Seismogram | SeismogramSegment,
): Seismogram {
if (typeof seisSeismogram === "object") {
if (seisSeismogram instanceof Seismogram) {
return seisSeismogram;
} else if (seisSeismogram instanceof SeismogramSegment) {
return new Seismogram([seisSeismogram]);
} else {
throw new Error("must be Seismogram or SeismogramSegment but " + stringify(seisSeismogram));
}
} else {
throw new Error(
"must be Seismogram or SeismogramSegment but not an object: " + stringify(seisSeismogram),
);
}
}
export class SeismogramDisplayData {
/** @private */
_seismogram: Seismogram | null;
_id: string | null;
_sourceId: FDSNSourceId | null;
label: string | null;
markerList: Array<MarkerType>;
traveltimeList: Array<TraveltimeArrivalType>;
channel: Channel | null;
_instrumentSensitivity: InstrumentSensitivity | null;
quakeList: Array<Quake>;
quakeReferenceList: Array<string> = [];
timeRange: Interval;
alignmentTime: DateTime;
doShow: boolean;
_statsCache: SeismogramDisplayStats | null;
constructor(timeRange: Interval) {
if (!timeRange) {
throw new Error("timeRange must not be missing.");
}
checkLuxonValid(timeRange);
this._id = null;
this._sourceId = null;
this._seismogram = null;
this.label = null;
this.markerList = [];
this.traveltimeList = [];
this.channel = null;
this._instrumentSensitivity = null;
this.quakeList = [];
this.timeRange = timeRange;
this.alignmentTime = validStartTime(timeRange);
this.doShow = true;
this._statsCache = null;
}
static fromSeismogram(seismogram: Seismogram): SeismogramDisplayData {
const out = new SeismogramDisplayData(
Interval.fromDateTimes(
seismogram.startTime,
seismogram.endTime,
),
);
out.seismogram = seismogram;
return out;
}
/**
* Create a Seismogram from the segment, then call fromSeismogram to create
* the SeismogramDisplayData;
* @param seisSegment segment of contiguous data
* @returns new SeismogramDisplayData
*/
static fromSeismogramSegment(seisSegment: SeismogramSegment): SeismogramDisplayData {
return SeismogramDisplayData.fromSeismogram(new Seismogram([seisSegment]));
}
/**
* Useful for creating fake data from an array, sample rate and start time
*
* @param yArray fake data
* @param sampleRate samples per second
* @param startTime start of data, time of first point
* @param sourceId optional source id
* @returns seismogramdisplaydata
*/
static fromContiguousData(
yArray:
| Array<seedcodec.EncodedDataSegment>
| Int32Array
| Float32Array
| Float64Array,
sampleRate: number,
startTime: DateTime,
sourceId?: FDSNSourceId,
): SeismogramDisplayData {
return SeismogramDisplayData.fromSeismogram(
Seismogram.fromContiguousData(yArray, sampleRate, startTime, sourceId));
}
static fromChannelAndTimeWindow(
channel: Channel,
timeRange: Interval,
): SeismogramDisplayData {
if (!channel) {
throw new Error("fromChannelAndTimeWindow, channel is undef");
}
const out = new SeismogramDisplayData(timeRange);
out.channel = channel;
return out;
}
static fromChannelAndTimes(
channel: Channel,
startTime: DateTime,
endTime: DateTime,
): SeismogramDisplayData {
const out = new SeismogramDisplayData(
Interval.fromDateTimes(startTime, endTime),
);
out.channel = channel;
return out;
}
static fromSourceIdAndTimes(
sourceId: FDSNSourceId,
startTime: DateTime,
endTime: DateTime,
): SeismogramDisplayData {
const out = new SeismogramDisplayData(
Interval.fromDateTimes(startTime, endTime),
);
out._sourceId = sourceId;
return out;
}
static fromCodesAndTimes(
networkCode: string,
stationCode: string,
locationCode: string,
channelCode: string,
startTime: DateTime,
endTime: DateTime,
): SeismogramDisplayData {
const out = new SeismogramDisplayData(
Interval.fromDateTimes(startTime, endTime),
);
out._sourceId = FDSNSourceId.fromNslc(
networkCode,
stationCode,
locationCode,
channelCode
);
return out;
}
addQuake(quake: Quake | Array<Quake>) {
if (Array.isArray(quake)) {
quake.forEach(q => this.quakeList.push(q));
} else {
this.quakeList.push(quake);
}
}
/**
* Adds a public id for a Quake to the seismogram. For use in case where
* the quake is not yet available, but wish to retain the connection.
* @param publicId id of the earthquake assocated with this seismogram
*/
addQuakeId(publicId: string) {
this.quakeReferenceList.push(publicId);
}
addMarker(marker: MarkerType) {
this.addMarkers([marker]);
}
addMarkers(markers: MarkerType | Array<MarkerType>) {
if (Array.isArray(markers)) {
markers.forEach(m => this.markerList.push(m));
} else {
this.markerList.push(markers);
}
}
addTravelTimes(
ttimes:
| TraveltimeJsonType
| TraveltimeArrivalType
| Array<TraveltimeArrivalType>,
) {
if (Array.isArray(ttimes)) {
ttimes.forEach(m => this.traveltimeList.push(m));
} else if ("arrivals" in ttimes) { // TraveltimeJsonType
ttimes.arrivals.forEach(m => this.traveltimeList.push(m));
} else {
this.traveltimeList.push(ttimes);
}
}
hasQuake(): boolean {
return this.quakeList.length > 0;
}
get quake(): Quake | null {
if (this.hasQuake()) {
return this.quakeList[0];
}
return null;
}
hasSeismogram(): this is { _seismogram: Seismogram } {
return isDef(this._seismogram);
}
append(seismogram: SeismogramSegment | Seismogram) {
if (isDef(this._seismogram)) {
this._seismogram.append(seismogram);
if (this.startTime > seismogram.startTime || this.endTime < seismogram.endTime) {
const startTime =
this.startTime < seismogram.startTime ? this.startTime : seismogram.startTime;
const endTime =
this.endTime > seismogram.endTime ? this.endTime : seismogram.endTime;
this.timeRange = Interval.fromDateTimes(startTime, endTime);
}
} else {
if (seismogram instanceof SeismogramSegment) {
this.seismogram = new Seismogram(seismogram);
} else {
this.seismogram = seismogram;
}
}
this._statsCache = null;
}
hasChannel(): this is { channel: Channel } {
return isDef(this.channel);
}
hasSensitivity(): this is { _instrumentSensitivity: InstrumentSensitivity } {
return (
this._instrumentSensitivity !== null ||
(isDef(this.channel) && this.channel.hasInstrumentSensitivity())
);
}
/**
* Allows id-ing a seismogram. Optional.
*
* @returns string id
*/
get id(): string | null {
return this._id;
}
/**
* Allows iding a seismogram. Optional.
*
* @param value string id
*/
set id(value: string | null) {
this._id = value;
}
/**
* return network code as a string.
* Uses this.channel if it exists, this.seismogram if not.
*
* @returns network code
*/
get networkCode(): string {
let out = this.sourceId.networkCode;
if (!isDef(out)) {
out = "unknown";
}
return out;
}
/**
* return station code as a string.
* Uses this.channel if it exists, this.seismogram if not.
*
* @returns station code
*/
get stationCode(): string {
let out = this.sourceId.stationCode;
if (!isDef(out)) {
out = "unknown";
}
return out;
}
/**
* return location code a a string.
* Uses this.channel if it exists, this.seismogram if not.
*
* @returns location code
*/
get locationCode(): string {
let out = this.sourceId.locationCode;
if (!isDef(out)) {
out = "unknown";
}
return out;
}
/**
* return channels code as a string.
* Uses this.channel if it exists, this.seismogram if not.
*
* @returns channel code
*/
get channelCode(): string {
let out = this.sourceId.formChannelCode();
if (!isDef(out)) {
out = "unknown";
}
return out;
}
/**
* return FDSN source id as a string.
* Uses this.channel if it exists, this.seismogram if not.
*
* @returns FDSN source id
*/
get sourceId(): FDSNSourceId {
if (isDef(this.channel)) {
return this.channel.sourceId;
} else if (isDef(this._seismogram)) {
return this._seismogram.sourceId;
} else if (isDef(this._sourceId)) {
return this._sourceId;
} else {
// should not happen
return FDSNSourceId.createUnknown();
//throw new Error("unable to create Id, neither channel, _sourceId nor seismogram");
}
}
/**
* return network, station, location and channels codes as one string.
* Uses this.channel if it exists, this.seismogram if not
*
* @returns net.sta.loc.chan
*/
get nslc(): string {
return this.codes();
}
get nslcId(): NslcId {
if (this.channel !== null) {
return this.channel.nslcId;
} else {
return new NslcId(
this.networkCode ? this.networkCode : "",
this.stationCode ? this.stationCode : "",
(this.locationCode && this.locationCode !== "--") ? this.locationCode : "",
this.channelCode ? this.channelCode : ""
);
}
}
/**
* return network, station, location and channels codes as one string.
* Uses this.channel if it exists, this.seismogram if not.
*
* @param sep separator, defaults to '.'
* @returns nslc codes separated by sep
*/
codes(sep = "."): string {
if (this.channel !== null) {
return this.channel.codes();
} else {
return (
(this.networkCode ? this.networkCode : "") +
sep +
(this.stationCode ? this.stationCode : "") +
sep +
(this.locationCode ? this.locationCode : "") +
sep +
(this.channelCode ? this.channelCode : "")
);
}
}
get startTime(): DateTime {
return validStartTime(this.timeRange);
}
get start(): DateTime {
return this.startTime;
}
get endTime(): DateTime {
return validEndTime(this.timeRange);
}
get end(): DateTime {
return this.endTime;
}
get numPoints(): number {
if (this._seismogram) {
return this._seismogram.numPoints;
}
return 0;
}
associateChannel(nets: Array<Network>) {
const matchChans = findChannels(nets,
this.networkCode,
this.stationCode,
this.locationCode,
this.channelCode);
for (const c of matchChans) {
if (c.timeRange.overlaps(this.timeRange)) {
this.channel = c;
return;
}
}
}
alignStartTime() {
this.alignmentTime = this.start;
}
alignOriginTime() {
if (this.quake) {
this.alignmentTime = this.quake.time;
} else {
this.alignmentTime = this.start;
}
}
alignPhaseTime(phaseRegExp: RegExp | string) {
let intPhaseRegExp: RegExp;
if (typeof phaseRegExp === "string") {
intPhaseRegExp = new RegExp(phaseRegExp);
} else {
intPhaseRegExp = phaseRegExp;
}
if (this.quake && this.traveltimeList) {
const q = this.quake;
const matchArrival = this.traveltimeList.find(ttArrival => {
const match = intPhaseRegExp.exec(ttArrival.phase);
// make sure regexp matches whole string, not just partial
return match !== null && match[0] === ttArrival.phase;
});
if (matchArrival) {
this.alignmentTime = q.time
.plus(Duration.fromMillis(matchArrival.time * 1000)); //seconds
} else {
this.alignmentTime = this.start;
}
}
}
/**
* Create a time window relative to the alignmentTime if set, or the start time if not.
* Negative durations are allowed.
* @param alignmentOffset offset duration from the alignment time
* @param duration duration from the offset for the window
* @returns time window as an Interval
*/
relativeTimeWindow(
alignmentOffset: Duration,
duration: Duration,
): Interval {
const atime = this.alignmentTime ? this.alignmentTime.plus(alignmentOffset) : this.startTime.plus(alignmentOffset);
return startDuration(atime, duration);
}
get sensitivity(): InstrumentSensitivity | null {
const channel = this.channel;
if (this._instrumentSensitivity) {
return this._instrumentSensitivity;
} else if (isDef(channel) && channel.hasInstrumentSensitivity()) {
return channel.instrumentSensitivity;
} else {
return null;
}
}
set sensitivity(value: InstrumentSensitivity | null) {
this._instrumentSensitivity = value;
}
get min(): number {
if (!this._statsCache) {
this._statsCache = this.calcStats();
}
return this._statsCache.min;
}
get max(): number {
if (!this._statsCache) {
this._statsCache = this.calcStats();
}
return this._statsCache.max;
}
get mean(): number {
if (!this._statsCache) {
this._statsCache = this.calcStats();
}
return this._statsCache.mean;
}
get middle(): number {
if (!this._statsCache) {