-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlme.js
1334 lines (1207 loc) · 51.5 KB
/
lme.js
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
// global constant for image tag
const lmeImg = document.querySelector('#lmePlot');
const lmeImgDownload = document.querySelector('#lmePlotDownload');
const lmeAni = document.querySelector('#lmeAnimeIFrame');
let errorLMEPlot = 0;
// add the plotMode radio button
let radioButtons = document.getElementsByName('plotMode');
showPickRadio();
let pickRadio = 'single';
// add the tsMode radio button
let radioButtonsTS = document.getElementsByName('tsMode');
showPickRadioTS();
let pickRadioTS = 'lmeTS';
// add event listener on radios to assign radio flag (pickRadio)
$('#singlePlot').on("click", function () {
$(this).prop('checked',true);
showPickRadio(); // just for console log
pickRadio = 'single';
$("#singlePlotInterface").removeClass("hidden");
$("#comparePlotInterface").addClass("hidden");
$("#compareDatasetInterface").addClass("hidden");
$(".timeseriesInterfaceGroup").addClass("hidden");
$("#animationInterface").addClass("hidden");
$("#lmeAnime").addClass("hidden");
$("#lmePlot").removeClass("hidden");
if (errorLMEPlot == 1) {
$("div.working").addClass("hidden");
$("div.error").removeClass("hidden");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmePlot").addClass("hidden");
};
});
$('#comparePlot').on("click", function () {
$(this).prop('checked',true);
showPickRadio();
pickRadio = 'compare';
$("#singlePlotInterface").addClass("hidden");
$("#comparePlotInterface").removeClass("hidden");
$("#compareDatasetInterface").addClass("hidden");
$(".timeseriesInterfaceGroup").addClass("hidden");
$("#animationInterface").addClass("hidden");
$("#lmeAnime").addClass("hidden");
$("#lmePlot").removeClass("hidden");
if (errorLMEPlot == 1) {
$("div.working").addClass("hidden");
$("div.error").removeClass("hidden");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmePlot").addClass("hidden");
};
});
$('#compareDataset').on("click", function () {
$(this).prop('checked',true);
showPickRadio();
pickRadio = 'compareData';
$("#singlePlotInterface").addClass("hidden");
$("#comparePlotInterface").addClass("hidden");
$("#compareDatasetInterface").removeClass("hidden");
$(".timeseriesInterfaceGroup").addClass("hidden");
$("#animationInterface").addClass("hidden");
$("#lmeAnime").addClass("hidden");
$("#lmePlot").removeClass("hidden");
if (errorLMEPlot == 1) {
$("div.working").addClass("hidden");
$("div.error").removeClass("hidden");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmePlot").addClass("hidden");
};
});
$('#timeSeries').on("click", function () {
$(this).prop('checked',true);
showPickRadio();
pickRadio = 'timeSeries';
$("#singlePlotInterface").addClass("hidden");
$("#comparePlotInterface").addClass("hidden");
$("#compareDatasetInterface").addClass("hidden");
$(".timeseriesInterfaceGroup").removeClass("hidden");
$("#animationInterface").addClass("hidden");
$("#lmeAnime").addClass("hidden");
$("#lmePlot").removeClass("hidden");
if (errorLMEPlot == 1) {
$("div.working").addClass("hidden");
$("div.error").removeClass("hidden");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmePlot").addClass("hidden");
};
});
$('#animation').on("click", function () {
$(this).prop('checked',true);
showPickRadio();
pickRadio = 'animation';
$("#singlePlotInterface").addClass("hidden");
$("#comparePlotInterface").addClass("hidden");
$("#compareDatasetInterface").addClass("hidden");
$(".timeseriesInterfaceGroup").addClass("hidden");
$("#animationInterface").removeClass("hidden");
$("#lmeAnime").removeClass("hidden");
$("#lmePlot").addClass("hidden");
iframeButtonClick();
if (errorLMEPlot == 1) {
$("div.error").addClass("hidden");
};
});
// for time series radios
$('#singlePTTS').on("click", function () {
$(this).prop('checked',true);
showPickRadioTS();
pickRadioTS = 'singlePTTS';
$("#lmeAWTS").addClass("hidden");
$(".PTTS").removeClass("hidden");
});
$('#lmeTS').on("click", function () {
$(this).prop('checked',true);
showPickRadioTS();
pickRadioTS = 'lmeTS';
$("#lmeAWTS").removeClass("hidden");
$(".PTTS").addClass("hidden");
});
// add the dataset options
createDatasetOpt('datasetType');
createDatasetOpt('datasetType1');
createDatasetOpt('datasetTypeD1');
createDatasetOpt('datasetTypeD2');
createDatasetTSOpt('datasetTypeTS1');
createDatasetOpt('datasetTypeAM');
// add the LME options
createLMEOpt('lmeNum');
createLMEOpt('lmeNum1');
createLMEOpt('lmeNumD');
createLMEOpt('lmeNumTS');
createLMEOpt('lmeNumAM');
// add the year options
createYearOpt('year',2002); // default syear value for noaa star
createYearOpt('year1',2002); // default syear value for noaa star
createYearOpt('year2',2002); // default syear value for noaa star
createYearOpt('yearD1',2002); // default syear value for noaa star
createYearOpt('yearD2',2002); // default syear value for noaa star
// const today = new Date();
// const latestYear = today.getFullYear();
createYearOpt('syear',2002); // default syear value for noaa star
createYearOpt('fyear',2002); // default fyear value for noaa star
createYearOpt('yearAM',2002); // default syear value for noaa star
// add the month options
createMonthOpt('month', 1); // default smonth value all month (for latestImage dropdown update to work)
createMonthOpt('month1', 9); // default smonth value for noaa star
createMonthOpt('month2', 9); // default smonth value for noaa star
createMonthOpt('monthD1', 9);// default smonth value for noaa star
createMonthOpt('monthD2', 9);// default smonth value for noaa star
createMonthOpt('monthAM',1); // default smonth value all month (for latestAnimation dropdown update to work)
// add the day options
createDayOpt('day',31);
createDayOpt('day1',31);
createDayOpt('day2',31);
createDayOpt('dayD1',31);
createDayOpt('dayD2',31);
createDayOpt('dayAM',31);
// default image and options
latestImage();
latestAnimation();
// hide the loading signal when image finish loading
$('#lmePlot').on("load", function () {
$("div.working").addClass("hidden");
$("div.error").addClass("hidden");
$("#lmePlot").removeClass("hidden");
$("#lmePlotDownload").removeClass("hidden");
// if (pickRadio == 'timeSeries') {
// $("#lmeDataDownload").removeClass("hidden");
// }
errorLMEPlot = 0;
});
// hide the loading signal and add the error signal when image not exist
$('#lmePlot').on("error", function () {
console.log('No available data.');
$("div.working").addClass("hidden");
$("div.error").removeClass("hidden");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$(this).removeAttr("src");
$(this).addClass("hidden");
errorLMEPlot = 1;
});
// hide the loading signal and play Ani when Anime finish loading (the first time)
$( window ).on( "load", function() {
if (pickRadio == 'animation') {
$("div.working").addClass("hidden");
$("div.error").addClass("hidden");
$("#lmeAnime").removeClass("hidden");
$("#lmePlotDownload").addClass("hidden");
$("#lmePlot").addClass("hidden");
iframeButtonClick();
}
});
// hide the loading signal and play Ani when Anime finish loading (after option changes )
$('#lmeAnimeIFrame').on("load", function () {
if (pickRadio == 'animation') {
$("div.working").addClass("hidden");
$("div.error").addClass("hidden");
$("#lmeAnime").removeClass("hidden");
$("#lmePlotDownload").addClass("hidden");
$("#lmePlot").addClass("hidden");
iframeButtonClick();
}
});
// event listener for the plot button that update image
$('#btn').on("click", updateImage);
$('#compareBtn').on("click", updateCompareImage);
$('#compareDBtn').on("click", updateCompareDataset);
$('#timeSeriesBtn').on("click", updateTimeseries);
$('#heatmapBtn').on("click", updateHeatmap);
$('#animeBtn').on("click", updateAnimation);
// event listener for year option due to dataset selection
$('#datasetType').on("change", {yearID : 'year'},updateYearOptAll);
$('#datasetType1').on("change", {yearID : 'year1'},updateYearOptAll);
$('#datasetType1').on("change", {yearID : 'year2'},updateYearOptAll);
$('#datasetTypeD1').on("change", {yearID : 'yearD1'},updateYearOptAll);
$('#datasetTypeD2').on("change", {yearID : 'yearD2'},updateYearOptAll);
$('#datasetTypeTS1').on("change", {yearID : 'syear'},updateTSYearOptAll);
$('#datasetTypeTS1').on("change", {yearID : 'fyear'},updateTSYearOptAll);
$('#datasetTypeAM').on("change", {yearID : 'yearAM'},updateYearOptAll);
// event listener for month option due to dataset selection
$('.dataInitMon').on("change",{dataID:'datasetType',yearID:'year',monthID:'month'}, updateMonthOptAll);
$('.dataInitMon1').on("change",{dataID:'datasetType1',yearID:'year1',monthID:'month1'}, updateMonthOptAll);
$('.dataInitMon2').on("change",{dataID:'datasetType1',yearID:'year2',monthID:'month2'}, updateMonthOptAll);
$('.dataInitMonD1').on("change",{dataID:'datasetTypeD1',yearID:'yearD1',monthID:'monthD1'}, updateMonthOptAll);
$('.dataInitMonD2').on("change",{dataID:'datasetTypeD2',yearID:'yearD2',monthID:'monthD2'}, updateMonthOptAll);
$('.dataInitMonAM').on("change",{dataID:'datasetTypeAM',yearID:'yearAM',monthID:'monthAM'}, updateMonthOptAll);
// event listener for day option due to month/year selection
$('.calenderChange').on("change",{yearID:'year',monthID:'month',dayID:'day'}, updateDayOptAll);
$('.calenderChange1').on("change",{yearID:'year1',monthID:'month1',dayID:'day1'}, updateDayOptAll);
$('.calenderChange2').on("change",{yearID:'year2',monthID:'month2',dayID:'day2'}, updateDayOptAll);
$('.calenderChangeD1').on("change",{yearID:'yearD1',monthID:'monthD1',dayID:'dayD1'}, updateDayOptAll);
$('.calenderChangeD2').on("change",{yearID:'yearD2',monthID:'monthD2',dayID:'dayD2'}, updateDayOptAll);
$('.calenderChangeAM').on("change",{yearID:'yearAM',monthID:'monthAM',dayID:'dayAM'}, updateDayOptAll);
// event listener for frequency option
$('#tFreq').on("change", function () {
let tSelect = document.getElementById("tFreq").value;
if (tSelect === 'monthly') {
$("#dayRow").addClass("hidden");
$("#90PercThres").removeClass("hidden");
$("#95PercThres").removeClass("hidden");
$("#99PercThres").removeClass("hidden");
$("#90MHW").removeClass("hidden");
$("#95MHW").removeClass("hidden");
$("#99MHW").removeClass("hidden");
} else if (tSelect === 'daily') {
$("#dayRow").removeClass("hidden");
$("#90PercThres").addClass("hidden");
$("#95PercThres").addClass("hidden");
$("#99PercThres").addClass("hidden");
$("#90MHW").addClass("hidden");
$("#95MHW").addClass("hidden");
$("#99MHW").addClass("hidden");
if ($("#stat").val() === '90perc' || $("#stat").val() === '95perc' || $("#stat").val() === '99perc' || $("#stat").val() === '90mhw' || $("#stat").val() === '95mhw' || $("#stat").val() === '99mhw') {
$("#stat").val('absolute');
}
// console.log($("#stat").val())
}
});
$('#tFreq1').on("change", function () {
let tSelect = document.getElementById("tFreq1").value;
if (tSelect === 'monthly') {
$("#day1Row").addClass("hidden");
} else if (tSelect === 'daily') {
$("#day1Row").removeClass("hidden");
}
});
$('#tFreq2').on("change", function () {
let tSelect = document.getElementById("tFreq2").value;
if (tSelect === 'monthly') {
$("#day2Row").addClass("hidden");
} else if (tSelect === 'daily') {
$("#day2Row").removeClass("hidden");
}
});
$('#tFreqD1').on("change", function () {
let tSelect = document.getElementById("tFreqD1").value;
if (tSelect === 'monthly') {
$("#dayD1Row").addClass("hidden");
} else if (tSelect === 'daily') {
$("#dayD1Row").removeClass("hidden");
}
});
$('#tFreqD2').on("change", function () {
let tSelect = document.getElementById("tFreqD2").value;
if (tSelect === 'monthly') {
$("#dayD2Row").addClass("hidden");
} else if (tSelect === 'daily') {
$("#dayD2Row").removeClass("hidden");
}
});
// event listener for statistic option
$('#stat').on("change", function () {
let statSelect = document.getElementById("stat").value;
if (statSelect === 'climatology') {
$("#yearRow").addClass("hidden");
} else {
$("#yearRow").removeClass("hidden");
}
});
$('#stat').on("change", function () {
let statSelect = document.getElementById("stat").value;
if (statSelect.includes('perc')) {
$("#yearRow").addClass("hidden");
} else {
$("#yearRow").removeClass("hidden");
}
});
$('#stat1').on("change", function () {
let statSelect = document.getElementById("stat1").value;
if (statSelect === 'climatology') {
$("#year1Row").addClass("hidden");
} else {
$("#year1Row").removeClass("hidden");
}
});
$('#stat2').on("change", function () {
let statSelect = document.getElementById("stat2").value;
if (statSelect === 'climatology') {
$("#year2Row").addClass("hidden");
} else {
$("#year2Row").removeClass("hidden");
}
});
//execute code after DOM is ready
function iframeButtonClick() {
//find iframe
let iframe = $('#lmeAnimeIFrame');
//find button inside iframe
let button = iframe.contents().find('.anim-buttons > button:nth-child(6)');
//trigger button click
button.trigger("click");
};
// function for different plotting mode
function showPickRadio() {
for(i = 0; i < radioButtons.length; i++) {
if(radioButtons[i].checked){
//console.log(radioButtons[i].value);
}
}
}
function showPickRadioTS() {
for(i = 0; i < radioButtonsTS.length; i++) {
if(radioButtonsTS[i].checked){
//console.log(radioButtonsTS[i].value);
}
}
}
// functions for change event on different option
function updateTSYearOptAll(event) {
// store original picked year
let yearID = event.data.yearID;
let selectYear = document.getElementById(yearID);
for (var i=0; i<selectYear.options.length; i++) {
let option = selectYear.options[i];
if (option.selected == true) {
yearOrig = option.value;
//console.log('Original picked year '+yearOrig);
}
}
// read the dataset and syear array
let datasetSelect = $(this).val();
//console.log('Dataset change to '+datasetSelect);
let datasets = datasetNames(); // store dataset names
let syears = datasetTSSyears(); // store dataset start years
//console.log('Dataset name '+datasets[datasetSelect-1]);
syear = syears[datasetSelect-1];
//console.log('Syear at '+syear);
$("#"+yearID).empty();
createYearOpt(yearID,syear);
// update to original picked year
let selectYearNew = document.getElementById(yearID);
for (var i=0; i<selectYearNew.options.length; i++) {
let option = selectYearNew.options[i];
if (option.value == yearOrig) {
option.selected = true;
}
}
};
function updateYearOptAll(event) {
// store original picked year
let yearID = event.data.yearID;
let selectYear = document.getElementById(yearID);
for (var i=0; i<selectYear.options.length; i++) {
let option = selectYear.options[i];
if (option.selected == true) {
yearOrig = option.value;
//console.log('Original picked year '+yearOrig);
}
}
// read the dataset and syear array
let datasetSelect = $(this).val();
//console.log('Dataset change to '+datasetSelect);
let datasets = datasetNames(); // store dataset names
let syears = datasetSyears(); // store dataset start years
//console.log('Dataset name '+datasets[datasetSelect-1]);
syear = syears[datasetSelect-1];
//console.log('Syear at '+syear);
$("#"+yearID).empty();
createYearOpt(yearID,syear);
// update to original picked year
let selectYearNew = document.getElementById(yearID);
for (var i=0; i<selectYearNew.options.length; i++) {
let option = selectYearNew.options[i];
if (option.value == yearOrig) {
option.selected = true;
}
}
};
function updateMonthOptAll(event) {
//console.log('updateMonthOptAll triggered');
let dataID = event.data.dataID;
let yearID = event.data.yearID;
let monthID = event.data.monthID;
// store original picked month
let selectMonth = document.getElementById(monthID);
for (var i=0; i<selectMonth.options.length; i++) {
let option = selectMonth.options[i];
if (option.selected == true) {
monthOrig = option.value;
//console.log('Original picked month '+monthOrig);
}
}
// read the dataset and smonth array
let datasetSelect = document.getElementById(dataID).value;
let yearSelect = document.getElementById(yearID).value;
let datasets = datasetNames(); // store dataset names
let syears = datasetSyears(); // store dataset start years
let smonths = datasetSmonths(); // store dataset start months
syear = syears[datasetSelect-1];
smonth = smonths[datasetSelect-1];
// if the year option is set to the initial year of the dataset month switch to smonth
// else the month is set to original month
if (yearSelect == syear) {
//console.log('Dataset '+datasets[datasetSelect-1]+' with year change to '+syear);
//console.log('Smonth at '+smonth);
$("#"+monthID).empty();
createMonthOpt(monthID,smonth);
} else {
$("#"+monthID).empty();
createMonthOpt(monthID,1);
// update to original picked year
let selectMonthNew = document.getElementById(monthID);
for (var i=0; i<selectMonthNew.options.length; i++) {
let option = selectMonthNew.options[i];
if (option.value == monthOrig) {
option.selected = true;
};
};
};
};
function updateDayOptAll(event) {
let yearID = event.data.yearID;
let monthID = event.data.monthID;
let dayID = event.data.dayID;
let monthSelect = document.getElementById(monthID).value;
let yearSelect = document.getElementById(yearID).value;
//console.log('year change to '+yearSelect);
//console.log('month change to '+monthSelect);
const month30Day = ['4','6','9','11'];
const month28Day = '2';
if (month30Day.includes(monthSelect)) {
$("#"+dayID).empty();
createDayOpt(dayID,30);
} else if (month28Day == monthSelect) {
if (yearSelect%4 == 0) {
$("#"+dayID).empty();
createDayOpt(dayID,29);
}
else {
$("#"+dayID).empty();
createDayOpt(dayID,28);
}
} else {
$("#"+dayID).empty();
createDayOpt(dayID,31);
}
};
// functions creating list in different options
function createDayOpt(dayID,totaldays) {
let elm = document.getElementById(dayID); // get the select
let df = document.createDocumentFragment(); // create a document fragment to hold the options while we create them
for (let i = 1; i <= totaldays; i++) { // loop
let option = document.createElement('option'); // create the option element
option.value = i; // set the value property
option.appendChild(document.createTextNode(i)); // set the textContent in a safe way.
df.appendChild(option); // append the option to the document fragment
}
elm.appendChild(df); // append the document fragment to the DOM. this is the better way rather than setting innerHTML a bunch of times (or even once with a long string)
};
function createMonthOpt(monthID,smonth) {
let elm = document.getElementById(monthID); // get the select
let df = document.createDocumentFragment(); // create a document fragment to hold the options while we create them
let monthString = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
for (let i = smonth; i <= 12; i++) { // loop
let option = document.createElement('option'); // create the option element
option.value = i; // set the value property
option.appendChild(document.createTextNode(monthString[i-1])); // set the textContent in a safe way.
df.appendChild(option); // append the option to the document fragment
}
elm.appendChild(df); // append the document fragment to the DOM. this is the better way rather than setting innerHTML a bunch of times (or even once with a long string)
};
function createYearOpt(yearID,syear) {
const today = new Date();
const latestYear = today.getFullYear();
let elm = document.getElementById(yearID); // get the select
let df = document.createDocumentFragment(); // create a document fragment to hold the options while we create them
for (let i = syear; i <= latestYear; i++) { // loop
let option = document.createElement('option'); // create the option element
option.value = i; // set the value property
option.appendChild(document.createTextNode(i)); // set the textContent in a safe way.
df.appendChild(option); // append the option to the document fragment
}
elm.appendChild(df); // append the document fragment to the DOM. this is the better way rather than setting innerHTML a bunch of times (or even once with a long string)
};
function createDatasetOpt(dataID) {
let elm = document.getElementById(dataID); // get the select
let df = document.createDocumentFragment(); // create a document fragment to hold the options while we create them
let datasets = datasetNames(); // store dataset names
for (let i = 0; i <= datasets.length-1; i++) { // loop
let option = document.createElement('option'); // create the option element
option.value = i+1; // set the value property
option.appendChild(document.createTextNode((i+1)+". "+datasets[i])); // set the textContent in a safe way.
df.appendChild(option); // append the option to the document fragment
}
elm.appendChild(df); // append the document fragment to the DOM. this is the better way rather than setting innerHTML a bunch of times (or even once with a long string)
};
function createDatasetTSOpt(dataID) {
let elm = document.getElementById(dataID); // get the select
let df = document.createDocumentFragment(); // create a document fragment to hold the options while we create them
let datasets = datasetTSNames(); // store dataset names
for (let i = 0; i <= datasets.length-1; i++) { // loop
let option = document.createElement('option'); // create the option element
option.value = i+1; // set the value property
option.appendChild(document.createTextNode((i+1)+". "+datasets[i])); // set the textContent in a safe way.
df.appendChild(option); // append the option to the document fragment
}
elm.appendChild(df); // append the document fragment to the DOM. this is the better way rather than setting innerHTML a bunch of times (or even once with a long string)
};
function createLMEOpt(lmeNumID) {
let elm = document.getElementById(lmeNumID); // get the select
let df = document.createDocumentFragment(); // create a document fragment to hold the options while we create them
let lmes = lmeNames(); // store LME names
for (let i = 0; i <= lmes.length-1; i++) { // loop
let option = document.createElement('option'); // create the option element
option.value = i+1; // set the value property
option.appendChild(document.createTextNode((i+1)+". "+lmes[i])); // set the textContent in a safe way.
df.appendChild(option); // append the option to the document fragment
}
elm.appendChild(df); // append the document fragment to the DOM. this is the better way rather than setting innerHTML a bunch of times (or even once with a long string)
};
// Functions for default action on the page
function latestImage() {
const today = new Date();
let yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
const latestYear = yesterday.getFullYear();
const latestMonth = yesterday.getMonth()+1;
const latestDay = yesterday.getDate();
const defaultTFreq = "daily";
const defaultLmeNum = 1;
const defaultStat = "absolute";
const defaultDataset = 1;
let selectDataset = document.getElementById('datasetType');
for (var i=0; i<selectDataset.options.length; i++) {
let option = selectDataset.options[i];
if (option.value == defaultDataset) {
option.selected = true;
}
}
let selectLME = document.getElementById('lmeNum');
for (var i=0; i<selectLME.options.length; i++) {
let option = selectLME.options[i];
if (option.value == defaultLmeNum) {
option.selected = true;
}
}
let selectYear = document.getElementById('year');
for (var i=0; i<selectYear.options.length; i++) {
let option = selectYear.options[i];
if (option.value == latestYear) {
option.selected = true;
}
}
let selectMonth = document.getElementById('month');
for (var i=0; i<selectMonth.options.length; i++) {
let option = selectMonth.options[i];
if (option.value == latestMonth) {
option.selected = true;
}
}
const month30Day = [4,6,9,11];
const month28Day = 2;
if (month30Day.includes(latestMonth)) {
$("#day").empty();
createDayOpt('day',30);
} else if (month28Day == latestMonth) {
$("#day").empty();
createDayOpt('day',28);
} else {
$("#day").empty();
createDayOpt('day',31);
};
let selectDay = document.getElementById('day');
for (var i=0; i<selectDay.options.length; i++) {
let option = selectDay.options[i];
if (option.value == latestDay) {
option.selected = true;
}
}
//console.log("the Default Dataset num is "+defaultDataset);
//console.log("the Default LME num is "+defaultLmeNum);
//console.log("the Default T frequency is "+defaultTFreq);
//console.log("the Default stat is "+defaultStat);
//console.log("the Default year is "+latestYear);
//console.log("the Default month is "+latestMonth);
//console.log("the Default day is "+latestDay);
lmeImg.src = "/cgi-bin/marinehw/HighRes_coastal_static_mod.py"
+"?lme_num="+defaultLmeNum
+"&dataset="+defaultDataset
+"&tFrequency="+defaultTFreq
+"&statistic="+defaultStat
+"&year="+latestYear
+"&month="+latestMonth
+"&day="+latestDay;
lmeImgDownload.href = "/cgi-bin/marinehw/HighRes_coastal_static_mod.py"
+"?lme_num="+defaultLmeNum
+"&dataset="+defaultDataset
+"&tFrequency="+defaultTFreq
+"&statistic="+defaultStat
+"&year="+latestYear
+"&month="+latestMonth
+"&day="+latestDay;
}
// Functions for button submissions
function updateImage() {
let datasetSelect = document.getElementById("datasetType");
let lmeNumSelect = document.getElementById("lmeNum");
let tFreqSelect = document.getElementById("tFreq");
let statSelect = document.getElementById("stat");
let yearSelect = document.getElementById("year");
let monthSelect = document.getElementById("month");
let daySelect = document.getElementById("day");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmeAnime").addClass("hidden");
// lmeAni.removeAttribute("src");
$("#lmePlot").addClass("hidden");
// lmeImg.removeAttribute("src");
$("div.working").removeClass("hidden");
$("div.error").addClass("hidden")
//console.log("the input Dataset num is "+datasetSelect.value);
//console.log("the input LME num is "+lmeNumSelect.value);
//console.log("the input T frequency is "+tFreqSelect.value);
//console.log("the input stat is "+statSelect.value);
//console.log("the input year is "+yearSelect.value);
//console.log("the input month is "+monthSelect.value);
//console.log("the input day is "+daySelect.value);
lmeImg.src = "/cgi-bin/marinehw/HighRes_coastal_static_mod.py"
+"?lme_num="+lmeNumSelect.value
+"&dataset="+datasetSelect.value
+"&tFrequency="+tFreqSelect.value
+"&statistic="+statSelect.value
+"&year="+yearSelect.value
+"&month="+monthSelect.value
+"&day="+daySelect.value;
lmeImgDownload.href = "/cgi-bin/marinehw/HighRes_coastal_static_mod.py"
+"?lme_num="+lmeNumSelect.value
+"&dataset="+datasetSelect.value
+"&tFrequency="+tFreqSelect.value
+"&statistic="+statSelect.value
+"&year="+yearSelect.value
+"&month="+monthSelect.value
+"&day="+daySelect.value;
}
function updateCompareImage() {
let datasetSelect = document.getElementById("datasetType1");
let lmeNumSelect = document.getElementById("lmeNum1");
let tFreqSelect1 = document.getElementById("tFreq1");
let statSelect1 = document.getElementById("stat1");
let yearSelect1 = document.getElementById("year1");
let monthSelect1 = document.getElementById("month1");
let daySelect1 = document.getElementById("day1");
let tFreqSelect2 = document.getElementById("tFreq2");
let statSelect2 = document.getElementById("stat2");
let yearSelect2 = document.getElementById("year2");
let monthSelect2 = document.getElementById("month2");
let daySelect2 = document.getElementById("day2");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmeAnime").addClass("hidden");
// lmeAni.removeAttribute("src");
$("#lmePlot").addClass("hidden");
// lmeImg.removeAttribute("src");
$("div.working").removeClass("hidden");
$("div.error").addClass("hidden")
//console.log("the input dataset num is "+datasetSelect.value);
//console.log("the input LME num is "+lmeNumSelect.value);
//console.log("the input T frequency1 is "+tFreqSelect1.value);
//console.log("the input stat1 is "+statSelect1.value);
//console.log("the input year1 is "+yearSelect1.value);
//console.log("the input month1 is "+monthSelect1.value);
//console.log("the input day1 is "+daySelect1.value);
//console.log("the input T frequency2 is "+tFreqSelect2.value);
//console.log("the input stat2 is "+statSelect2.value);
//console.log("the input year2 is "+yearSelect2.value);
//console.log("the input month2 is "+monthSelect2.value);
//console.log("the input day2 is "+daySelect2.value);
lmeImg.src = "/cgi-bin/marinehw/HighRes_coastal_static_compare.py"
+"?lme_num1="+lmeNumSelect.value
+"&dataset1="+datasetSelect.value
+"&tFrequency1="+tFreqSelect1.value
+"&statistic1="+statSelect1.value
+"&year1="+yearSelect1.value
+"&month1="+monthSelect1.value
+"&day1="+daySelect1.value
+"&tFrequency2="+tFreqSelect2.value
+"&statistic2="+statSelect2.value
+"&year2="+yearSelect2.value
+"&month2="+monthSelect2.value
+"&day2="+daySelect2.value;
lmeImgDownload.href = "/cgi-bin/marinehw/HighRes_coastal_static_compare.py"
+"?lme_num1="+lmeNumSelect.value
+"&dataset1="+datasetSelect.value
+"&tFrequency1="+tFreqSelect1.value
+"&statistic1="+statSelect1.value
+"&year1="+yearSelect1.value
+"&month1="+monthSelect1.value
+"&day1="+daySelect1.value
+"&tFrequency2="+tFreqSelect2.value
+"&statistic2="+statSelect2.value
+"&year2="+yearSelect2.value
+"&month2="+monthSelect2.value
+"&day2="+daySelect2.value;
}
function updateCompareDataset() {
let lmeNumSelect = document.getElementById("lmeNumD");
let datasetSelectD1 = document.getElementById("datasetTypeD1");
let tFreqSelectD1 = document.getElementById("tFreqD1");
let yearSelectD1 = document.getElementById("yearD1");
let monthSelectD1 = document.getElementById("monthD1");
let daySelectD1 = document.getElementById("dayD1");
let datasetSelectD2 = document.getElementById("datasetTypeD2");
let tFreqSelectD2 = document.getElementById("tFreqD2");
let yearSelectD2 = document.getElementById("yearD2");
let monthSelectD2 = document.getElementById("monthD2");
let daySelectD2 = document.getElementById("dayD2");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmeAnime").addClass("hidden");
// lmeAni.removeAttribute("src");
$("#lmePlot").addClass("hidden");
// lmeImg.removeAttribute("src");
$("div.working").removeClass("hidden");
$("div.error").addClass("hidden")
//console.log("entering dataset comparison cgi call");
//console.log("the input LME num is "+lmeNumSelect.value);
//console.log("the input dataset1 num is "+datasetSelectD1.value);
//console.log("the input T frequencyD1 is "+tFreqSelectD1.value);
//console.log("the input yearD1 is "+yearSelectD1.value);
//console.log("the input monthD1 is "+monthSelectD1.value);
//console.log("the input dayD1 is "+daySelectD1.value);
////console.log("the input dataset2 num is "+datasetSelectD2.value);
//console.log("the input T frequencyD2 is "+tFreqSelectD2.value);
//console.log("the input yearD2 is "+yearSelectD2.value);
//console.log("the input monthD2 is "+monthSelectD2.value);
//console.log("the input dayD2 is "+daySelectD2.value);
lmeImg.src = "/cgi-bin/marinehw/HighRes_coastal_static_compare_dataset.py"
+"?lme_numD="+lmeNumSelect.value
+"&datasetD1="+datasetSelectD1.value
+"&tFrequencyD1="+tFreqSelectD1.value
+"&yearD1="+yearSelectD1.value
+"&monthD1="+monthSelectD1.value
+"&dayD1="+daySelectD1.value
+"&datasetD2="+datasetSelectD2.value
+"&tFrequencyD2="+tFreqSelectD2.value
+"&yearD2="+yearSelectD2.value
+"&monthD2="+monthSelectD2.value
+"&dayD2="+daySelectD2.value;
lmeImgDownload.href = "/cgi-bin/marinehw/HighRes_coastal_static_compare_dataset.py"
+"?lme_numD="+lmeNumSelect.value
+"&datasetD1="+datasetSelectD1.value
+"&tFrequencyD1="+tFreqSelectD1.value
+"&yearD1="+yearSelectD1.value
+"&monthD1="+monthSelectD1.value
+"&dayD1="+daySelectD1.value
+"&datasetD2="+datasetSelectD2.value
+"&tFrequencyD2="+tFreqSelectD2.value
+"&yearD2="+yearSelectD2.value
+"&monthD2="+monthSelectD2.value
+"&dayD2="+daySelectD2.value;
}
function updateTimeseries() {
let lonVal = document.getElementById("lonTS");
let latVal = document.getElementById("latTS");
let lmeNumSelect = document.getElementById("lmeNumTS");
let datasetSelectTS1 = document.getElementById("datasetTypeTS1");
// let tFreqSelectD1 = document.getElementById("tFreqD1");
let statTS1 = document.getElementById("tStatTS1");
let syearSelectTS1 = document.getElementById("syear");
let fyearSelectTS1 = document.getElementById("fyear");
let mhw = document.getElementById("threshold");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmeAnime").addClass("hidden");
// lmeAni.removeAttribute("src");
$("#lmePlot").addClass("hidden");
// lmeImg.removeAttribute("src");
$("div.working").removeClass("hidden");
$("div.error").addClass("hidden")
//console.log("entering time series line cgi call");
//console.log("radio pick TSmode is "+pickRadioTS);
//console.log("the input lon is "+lonVal.value);
//console.log("the input lat is "+latVal.value);
//console.log("the input LME num is "+lmeNumSelect.value);
//console.log("the input dataset num is "+datasetSelectTS1.value);
//console.log("the input statistic is "+statTS1.value);
//console.log("the input syear is "+syearSelectTS1.value);
//console.log("the input fyear is "+fyearSelectTS1.value);
//console.log("the input mhw is "+mhw.value);
var ajaxGet = "/cgi-bin/marinehw/HighRes_coastal_static_timeseries.py"
+"?lme_num="+lmeNumSelect.value
+"&tsMode="+pickRadioTS
+"&lon_TS="+lonVal.value
+"&lat_TS="+latVal.value
+"&dataset="+datasetSelectTS1.value
+"&varmode="+statTS1.value
+"&syear="+syearSelectTS1.value
+"&fyear="+fyearSelectTS1.value
+"&mhw_threshold="+mhw.value;
fetch(ajaxGet) // Replace with the URL you want to request
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.blob();
})
.then(imageBlob => {
// Process the response image here
const imageUrl = URL.createObjectURL(imageBlob);
// attached to src of lmeImg
lmeImg.src = imageUrl;
// attached to href of lmeImgDownload
lmeImgDownload.href = imageUrl;
// setup data path to href of lmeDataDownload
const sessionId = getCookie('session_id');
lmeDataDownload.href = "/tmp/marinehw/HighRes_LME_ts_"+sessionId+".nc";
console.log(lmeDataDownload.href);
$("div.working").addClass("hidden");
$("div.error").addClass("hidden");
$("#lmePlot").removeClass("hidden");
$("#lmePlotDownload").removeClass("hidden");
$("#lmeDataDownload").removeClass("hidden");
errorLMEPlot = 0;
})
.catch(error => {
// Handle errors here
console.error('Fetch error:', error);
});
// lmeImg.src = "/cgi-bin/marinehw/HighRes_coastal_static_timeseries.py"
// +"?lme_num="+lmeNumSelect.value
// +"&tsMode="+pickRadioTS
// +"&lon_TS="+lonVal.value
// +"&lat_TS="+latVal.value
// +"&dataset="+datasetSelectTS1.value
// +"&varmode="+statTS1.value
// +"&syear="+syearSelectTS1.value
// +"&fyear="+fyearSelectTS1.value
// +"&mhw_threshold="+mhw.value;
// lmeImgDownload.href = "/cgi-bin/marinehw/HighRes_coastal_static_timeseries.py"
// +"?lme_num="+lmeNumSelect.value
// +"&tsMode="+pickRadioTS
// +"&lon_TS="+lonVal.value
// +"&lat_TS="+latVal.value
// +"&dataset="+datasetSelectTS1.value
// +"&varmode="+statTS1.value
// +"&syear="+syearSelectTS1.value
// +"&fyear="+fyearSelectTS1.value
// +"&mhw_threshold="+mhw.value;
}
function getCookie(name) {
const cookies = document.cookie.split('; ');
for (let cookie of cookies) {
const [cookieName, cookieValue] = cookie.split('=');
if (cookieName === name) {
return decodeURIComponent(cookieValue);
}
}
return null; // Cookie not found
};
function updateHeatmap() {
let lonVal = document.getElementById("lonTS");
let latVal = document.getElementById("latTS");
let lmeNumSelect = document.getElementById("lmeNumTS");
let datasetSelectTS1 = document.getElementById("datasetTypeTS1");
// let tFreqSelectD1 = document.getElementById("tFreqD1");
let syearSelectTS1 = document.getElementById("syear");
let fyearSelectTS1 = document.getElementById("fyear");
let mhw = document.getElementById("threshold");
$("#lmePlotDownload").addClass("hidden");
$("#lmeDataDownload").addClass("hidden");
$("#lmeAnime").addClass("hidden");
// lmeAni.removeAttribute("src");
$("#lmePlot").addClass("hidden");
// lmeImg.removeAttribute("src");
$("div.working").removeClass("hidden");
$("div.error").addClass("hidden")