-
Notifications
You must be signed in to change notification settings - Fork 7
/
seismographconfig.ts
726 lines (651 loc) · 19.7 KB
/
seismographconfig.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
/*
* Philip Crotwell
* University of South Carolina, 2019
* http://www.seis.sc.edu
*/
import { AUTO_COLOR_SELECTOR, G_DATA_SELECTOR} from "./cssutil";
import {IndividualAmplitudeScale, LinkedAmplitudeScale, LinkedTimeScale} from "./scale";
import {SeismogramDisplayData, Seismogram} from "./seismogram";
import {isDef} from "./util";
import { Duration, Interval} from "luxon";
import * as d3 from "d3";
import type {AxisDomain} from "d3-axis";
// browser field in package.json for handlebars 4.7.7 is bad,
// direct import from file works for now, but is fragile
import Handlebars from "handlebars/dist/cjs/handlebars.js";
//import Handlebars from "handlebars";
export type MarginType = {
top: number;
right: number;
bottom: number;
left: number;
toString?: () => string;
};
/** Constant for drawing seismogram using svg. */
export const DRAW_SVG = "svg";
/** Constant for drawing seismogram using canvas, axies are still svg. */
export const DRAW_CANVAS = "canvas";
/** Constant for drawing seismogram using both canvas and svg, for testing. */
export const DRAW_BOTH = "both"; // for testing
/** Constant for drawing seismogram using both canvas and svg plus alignment markers, for testing. */
export const DRAW_BOTH_ALIGN = "alignment"; // for testing
export const DEFAULT_TITLE =
"{{#each seisDataList}}<tspan>{{onlyChangesChannel ../seisDataList @index}}</tspan> {{else}}No Data{{/each}}";
/**
* Configuration object for Seismograph display.
*
*/
export class SeismographConfig {
static _lastID: number;
configId: number;
drawingType: string; // canvas or svg
timeFormat: (date: Date) => string;
relativeTimeFormat: (value: number) => string;
amplitudeFormat: (value: number) => string;
showTitle: boolean;
/** @private */
_title: Array<string>;
/** @private */
_titleHandlebarsCompiled: null | ((arg0: {}, arg1: {}) => string);
isXAxis: boolean;
isXAxisTop: boolean;
_xLabel: string;
/** @private */
_xLabelHandlebarsCompiled: null | ((arg0: {}, arg1: {}) => string);
xLabelOrientation: string;
_xSublabel: string;
xSublabelIsUnits: boolean;
/** @private */
_xSublabelHandlebarsCompiled: null | ((arg0: {}, arg1: {}) => string);
isYAxis: boolean;
isYAxisRight: boolean;
isYAxisNice: boolean;
_yLabel: string;
/** @private */
_yLabelHandlebarsCompiled: null | ((arg0: {}, arg1: {}) => string);
_yLabelRight: string;
/** @private */
_yLabelRightHandlebarsCompiled: null | ((arg0: {}, arg1: {}) => string);
yLabelOrientation: string;
_ySublabel: string;
/** @private */
_ySublabelHandlebarsCompiled: null | ((arg0: {}, arg1: {}) => string);
ySublabelTrans: number;
ySublabelIsUnits: boolean;
doMarkers: boolean;
markerTextOffset: number;
markerTextAngle: number;
markerFlagpoleBase: string;
minHeight: number;
maxHeight: null | number;
minWidth: number;
maxWidth: null | number;
margin: MarginType;
segmentDrawCompressedCutoff: number; //below this draw all points, above draw minmax
maxZoomPixelPerSample: number; // no zoom in past point of sample
// separated by pixels
connectSegments: boolean;
lineColors: Array<string>;
lineWidth: number;
wheelZoom: boolean;
centeredAmp: boolean;
doGain: boolean;
windowAmp: boolean;
/** @private */
_fixedAmplitudeScale: null | Array<number>;
/** @private */
_fixedTimeScale: null | Interval;
/** @private */
_linkedAmplitudeScale: null | LinkedAmplitudeScale;
/** @private */
_linkedTimeScale: null | LinkedTimeScale;
isRelativeTime: boolean;
constructor() {
this.configId = ++SeismographConfig._lastID;
this.drawingType = DRAW_CANVAS;
this.isXAxis = true;
this.isXAxisTop = false;
this.isYAxisNice = true;
this.isYAxis = true;
this.isYAxisRight = false;
this.timeFormat = multiFormatHour;
this.relativeTimeFormat = formatCountOrAmp;
this.amplitudeFormat = formatCountOrAmp;
this._title = [DEFAULT_TITLE];
this._titleHandlebarsCompiled = null;
this.showTitle = true;
this._xLabel = "Time";
this.xLabelOrientation = "horizontal";
this._xSublabel = "";
this.xSublabelIsUnits = false;
this._yLabel = "Amplitude";
this._yLabelRight = "";
this._xLabelHandlebarsCompiled = null;
this._xSublabelHandlebarsCompiled = null;
this.yLabelOrientation = "vertical";
this._yLabelHandlebarsCompiled = null;
this._yLabelRightHandlebarsCompiled = null;
this._ySublabel = "";
this._ySublabelHandlebarsCompiled = null;
this.ySublabelTrans = 15;
this.ySublabelIsUnits = true;
this.centeredAmp = true;
this.doGain = true;
this.windowAmp = true;
this._fixedAmplitudeScale = null;
this._fixedTimeScale = null;
this._linkedAmplitudeScale = new IndividualAmplitudeScale();
this._linkedTimeScale = new LinkedTimeScale([], Duration.fromMillis(0), Duration.fromMillis(0), this.configId);
this.isRelativeTime = false;
this.doMarkers = true;
this.markerTextOffset = 0.85;
this.markerTextAngle = 45;
this.markerFlagpoleBase = "bottom"; // bottom or center
this.minHeight = 0;
this.maxHeight = null;
this.minWidth = 0;
this.maxWidth = null;
this.margin = {
top: 25,
right: 20,
bottom: 42,
left: 85,
toString: function () {
return (
"t:" +
this.top +
" l:" +
this.left +
" b:" +
this.bottom +
" r:" +
this.right
);
},
};
this.segmentDrawCompressedCutoff = 10; //below this draw all points, above draw minmax
this.maxZoomPixelPerSample = 20; // no zoom in past point of sample
this.wheelZoom = false;
// separated by pixels
this.connectSegments = false;
this.lineColors = [
"skyblue",
"olivedrab",
"goldenrod",
"firebrick",
"darkcyan",
"orange",
"darkmagenta",
"mediumvioletred",
"sienna",
"black",
];
this.lineWidth = 1;
}
static fromJSON(json: any): SeismographConfig {
const seisConfig = new SeismographConfig();
Object.assign(seisConfig, json);
if (json.isLinkedTimeScale) {
seisConfig.linkedTimeScale = new LinkedTimeScale();
}
if (json.isLinkedAmplitudeScale) {
seisConfig.linkedAmplitudeScale = new LinkedAmplitudeScale();
} else if (! isDef(seisConfig.fixedAmplitudeScale)) {
// neither fixed nor linked, so individual
seisConfig.linkedAmplitudeScale = new IndividualAmplitudeScale();
}
return seisConfig;
}
asJSON(): any {
// kind of dumb...
const out = JSON.parse(JSON.stringify(this));
out.title = out._title;
delete out._title;
out.fixedAmplitudeScale = out._fixedAmplitudeScale;
delete out._fixedAmplitudeScale;
out.fixedTimeScale = out._fixedTimeScale;
delete out._fixedTimeScale;
delete out._linkedTimeScale;
out.isLinkedTimeScale = isDef(this._linkedTimeScale);
out.isLinkedAmplitudeScale = isDef(this._linkedAmplitudeScale) && ! (this._linkedAmplitudeScale instanceof IndividualAmplitudeScale);
delete out._titleHandlebarsCompiled;
return out;
}
get fixedAmplitudeScale(): null | Array<number> {
return this._fixedAmplitudeScale;
}
set fixedAmplitudeScale(ts: null | Array<number>) {
if (!isDef(ts)) {throw new Error("amp scale must be defined");}
this._fixedAmplitudeScale = ts;
this._linkedAmplitudeScale = null;
}
get linkedAmplitudeScale(): null | LinkedAmplitudeScale {
return this._linkedAmplitudeScale;
}
set linkedAmplitudeScale(ts: null | LinkedAmplitudeScale) {
if (!isDef(ts)) {throw new Error("amp scale must be defined");}
if (this._linkedAmplitudeScale) {
ts.linkAll(this._linkedAmplitudeScale.graphList);
}
this._linkedAmplitudeScale = ts;
this._fixedAmplitudeScale = null;
}
get fixedTimeScale(): null | Interval {
return this._fixedTimeScale;
}
set fixedTimeScale(ts: null | Interval) {
if (!isDef(ts)) {throw new Error("time scale must be defined");}
this._fixedTimeScale = ts;
this._linkedTimeScale = null;
}
get linkedTimeScale(): null | LinkedTimeScale {
return this._linkedTimeScale;
}
set linkedTimeScale(ts: null | LinkedTimeScale) {
if (!isDef(ts)) {throw new Error("time scale must be defined");}
if (this._linkedTimeScale) {
ts.linkAll(this._linkedTimeScale.graphList);
}
this._linkedTimeScale = ts;
this._fixedTimeScale = null;
}
/**
* gets the current title
*
* @returns title as an array of strings
*/
get title(): Array<string> {
return this._title;
}
/**
* Sets the title as simple string or array of strings or a
* handlebars template. If an array
* then each item will be in a separate tspan for easier formatting.
*
* @param value string or array of strings to be the title
*/
set title(value: null | string | Array<string>) {
if (!isDef(value)) {
this._title = [""];
} else if (Array.isArray(value)) {
this._title = value;
} else {
this._title = [value];
}
this._titleHandlebarsCompiled = null;
}
handlebarsTitle(context: {}, runtimeOptions: {}): string {
if (!isDef(this._titleHandlebarsCompiled)) {
if (
!isDef(this._title) ||
this._title.length === 0 ||
!isDef(this._title[0])
) {
// empty title
return "";
} else if (this._title.length === 1) {
this._titleHandlebarsCompiled = Handlebars.compile(this._title[0]);
} else {
this._titleHandlebarsCompiled = Handlebars.compile(
"" + this._title.join(" "),
);
}
}
// don't think this can happen, keep typescript happy
if (! this._titleHandlebarsCompiled) {
throw new Error(`Unable to compile handlebars title for ${this._title}`);
}
return this._titleHandlebarsCompiled(context, runtimeOptions);
}
/**
* gets the current x label
*
* @returns x label
*/
get xLabel(): string {
return this._xLabel;
}
/**
* Sets the xLabel as simple string or a
* handlebars template.
*
* @param value string to be the x label
*/
set xLabel(value: null | string) {
if (!isDef(value)) {
this._xLabel = "";
} else {
this._xLabel = value;
}
this._xLabelHandlebarsCompiled = null;
}
/**
* gets the current x sublabel
*
* @returns x sublabel
*/
get xSublabel(): string {
return this._xSublabel;
}
/**
* Sets the xSublabel as simple string or a
* handlebars template.
*
* @param value string to be the x sublabel
*/
set xSublabel(value: null | string) {
if (!isDef(value)) {
this._xSublabel = "";
} else {
this._xSublabel = value;
}
this._xSublabelHandlebarsCompiled = null;
}
handlebarsXLabel(context: {}, runtimeOptions: {}): string {
if (!isDef(this._xLabelHandlebarsCompiled)) {
if (!isDef(this._xLabel) || this._xLabel.length === 0) {
// empty label
return "";
} else {
this._xLabelHandlebarsCompiled = Handlebars.compile(this._xLabel);
}
}
// don't think this can happen, keep typescript happy
if (! this._xLabelHandlebarsCompiled) {
throw new Error(`Unable to compile handlebars xLabel for ${this._xLabel}`);
}
return this._xLabelHandlebarsCompiled(context, runtimeOptions);
}
handlebarsXSublabel(context: {}, runtimeOptions: {}): string {
if (!isDef(this._xSublabelHandlebarsCompiled)) {
if (!isDef(this._xSublabel) || this._xSublabel.length === 0) {
// empty label
return "";
} else {
this._xSublabelHandlebarsCompiled = Handlebars.compile(this._xSublabel);
}
}
// don't think this can happen, keep typescript happy
if (! this._xSublabelHandlebarsCompiled) {
throw new Error(`Unable to compile handlebars xLabel for ${this._xSublabel}`);
}
return this._xSublabelHandlebarsCompiled(context, runtimeOptions);
}
/**
* gets the current y label
*
* @returns yLabel
*/
get yLabel(): string {
return this._yLabel;
}
/**
* Sets the ylabel as simple string or a
* handlebars template.
*
* @param value string to be the y label
*/
set yLabel(value: null | string) {
if (!isDef(value)) {
this._yLabel = "";
} else {
this._yLabel = value;
}
this._yLabelHandlebarsCompiled = null;
}
/**
* gets the current y sublabel
*
* @returns ySublabel
*/
get ySublabel(): string {
return this._ySublabel;
}
/**
* Sets the y sublabel as simple string or a
* handlebars template.
*
* @param value string to be the y sublabel
*/
set ySublabel(value: null | string) {
if (!isDef(value)) {
this._ySublabel = "";
} else {
this._ySublabel = value;
}
this._ySublabelHandlebarsCompiled = null;
}
handlebarsYLabel(context: {}, runtimeOptions: {}): string {
if (!isDef(this._yLabelHandlebarsCompiled)) {
if (!isDef(this._yLabel) || this._yLabel.length === 0) {
// empty label
return "";
} else {
this._yLabelHandlebarsCompiled = Handlebars.compile(this._yLabel);
}
}
// don't think this can happen, keep typescript happy
if (! this._yLabelHandlebarsCompiled) {
throw new Error(`Unable to compile handlebars yLabel for ${this._yLabel}`);
}
return this._yLabelHandlebarsCompiled(context, runtimeOptions);
}
handlebarsYSublabel(context: {}, runtimeOptions: {}): string {
if (!isDef(this._ySublabelHandlebarsCompiled)) {
if (!isDef(this._ySublabel) || this._ySublabel.length === 0) {
// empty label
return "";
} else {
this._ySublabelHandlebarsCompiled = Handlebars.compile(this._ySublabel);
}
}
// don't think this can happen, keep typescript happy
if (! this._ySublabelHandlebarsCompiled) {
throw new Error(`Unable to compile handlebars yLabel for ${this._ySublabel}`);
}
return this._ySublabelHandlebarsCompiled(context, runtimeOptions);
}
/**
* gets the current title
*
* @returns yLabel
*/
get yLabelRight(): string {
return this._yLabelRight;
}
/**
* Sets the ylabel as simple string or a
* handlebars template.
*
* @param value string to be the y label
*/
set yLabelRight(value: null | string) {
if (!isDef(value)) {
this._yLabelRight = "";
} else {
this._yLabelRight = value;
}
this._yLabelRightHandlebarsCompiled = null;
}
handlebarsYLabelRight(context: {}, runtimeOptions: {}): string {
if (!isDef(this._yLabelRightHandlebarsCompiled)) {
if (!isDef(this._yLabelRight) || this._yLabelRight.length === 0) {
// empty label
return "";
} else {
this._yLabelRightHandlebarsCompiled = Handlebars.compile(
this._yLabelRight,
);
}
}
// don't think this can happen, keep typescript happy
if (! this._yLabelRightHandlebarsCompiled) {
throw new Error(`Unable to compile handlebars yLabelRight for ${this._yLabelRight}`);
}
return this._yLabelRightHandlebarsCompiled(context, runtimeOptions);
}
/**
* Fake data to use to test alignment of seismograph axis and between canvas
* and svg drawing.
*
* @param timeRange start and end of fake data
* @param min min amplitude for fake data, default is -100
* @param max max amplitude for fake data, default is 100
* @returns fake data
*/
createAlignmentData(
timeRange: Interval,
min = -100,
max = 100,
): SeismogramDisplayData {
const mid = (max + min) / 2;
const fakeData = Float32Array.from([
max,
min,
max,
min,
mid,
mid,
max,
mid,
mid,
min,
]);
const fakeSampleRate =
1 / (1000*timeRange.toDuration().toMillis() / (fakeData.length - 1));
const fakeSeis = Seismogram.createFromContiguousData(
fakeData,
fakeSampleRate,
timeRange.start,
);
const fakeSDD = SeismogramDisplayData.fromSeismogram(fakeSeis);
return fakeSDD;
}
getColorForIndex(i: number): string {
if (isDef(this.lineColors) && this.lineColors.length > 0) {
return this.lineColors[i % this.lineColors.length];
} else {
return "black";
}
}
createCSSForLineColors(svgClass?: string): string {
let cssText = "";
const numColors = this.lineColors.length;
let svgEl = "svg";
if (!isDef(svgClass)) {
svgEl = `svg.${AUTO_COLOR_SELECTOR}`;
} else if (svgClass.length === 0) {
svgEl = "svg";
} else {
svgEl = `svg.${svgClass}`;
}
// line width and fill for paths that are seisplotjsdata
cssText =
cssText +
`
${svgEl} g.${G_DATA_SELECTOR} g path {
fill: none;
stroke-width: 1px;
}
`;
this.lineColors.forEach((color, index) => {
cssText =
cssText +
`
${svgEl} g.title text tspan:nth-child(${numColors}n+${index + 1}) {
stroke: ${color};
fill: ${color};
color: ${color};
}
`;
// not used by actual waveform as default is canvas, not svg
// is used by fftplot
cssText += `
${svgEl} g.${G_DATA_SELECTOR} g:nth-child(${numColors}n+${
index + 1
}) path {
stroke: ${color};
}
`;
});
return cssText;
}
clone(): SeismographConfig {
const out = new SeismographConfig();
Object.getOwnPropertyNames(this).forEach(name => {
// @ts-ignore
if (Array.isArray(this[name])) {
// @ts-ignore
out[name] = this[name].slice();
} else {
// @ts-ignore
out[name] = this[name];
}
// handle margin separately
out.margin = {
top: this.margin.top,
right: this.margin.right,
bottom: this.margin.bottom,
left: this.margin.left,
toString: function () {
return (
"t:" +
this.top +
" l:" +
this.left +
" b:" +
this.bottom +
" r:" +
this.right
);
},
};
});
return out;
}
toString(): string {
let outS = "";
Object.getOwnPropertyNames(this).forEach(name => {
// @ts-ignore
outS += ` seisConfig.${name} = ${JSON.stringify(this[name])}\n`;
});
return outS;
}
}
export function numberFormatWrapper( formater: (arg0: number) => string): ((domainValue: AxisDomain) => string) {
return function(domainValue: AxisDomain) {
if (typeof domainValue === "number" ) {
return formater(domainValue);
} else {
throw new Error("Can only format number, "+domainValue);
}
};
}
export const formatCount: (arg0: number) => string = d3.format("~s");
export const formatExp: (arg0: number) => string = d3.format(".2e");
export const formatCountOrAmp = function (v: number): string {
return -1 < v && v < 1 && v !== 0 ? formatExp(v) : formatCount(v);
};
export const formatMillisecond: (arg0: Date) => string = d3.utcFormat(".%L");
export const formatSecond: (arg0: Date) => string = d3.utcFormat(":%S");
export const formatMinute: (arg0: Date) => string = d3.utcFormat("%H:%M");
export const formatHour: (arg0: Date) => string = d3.utcFormat("%H:%M");
export const formatDay: (arg0: Date) => string = d3.utcFormat("%m/%d");
export const formatMonth: (arg0: Date) => string = d3.utcFormat("%Y/%m");
export const formatYear: (arg0: Date) => string = d3.utcFormat("%Y");
export const multiFormatHour = function (date: Date): string {
return (d3.utcSecond(date) < date
? formatMillisecond
: d3.utcMinute(date) < date
? formatSecond
: d3.utcHour(date) < date
? formatMinute
: d3.utcDay(date) < date
? formatHour
: d3.utcMonth(date) < date
? formatDay
: d3.utcYear(date) < date
? formatMonth
: formatYear)(date);
};
SeismographConfig._lastID = 0;