-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdeSelections.js
More file actions
1155 lines (1041 loc) · 51.2 KB
/
sdeSelections.js
File metadata and controls
1155 lines (1041 loc) · 51.2 KB
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
function openDatasetLabels() {
const datasetLabels = document.getElementById('datasetLabels');
datasetLabels.style.display = 'block';
let form = document.getElementById('datasetLabelsForm');
form.innerHTML = '';
let index = 0;
for (dataset in selectedSampleInfo) {
// Create a label
let label = document.createElement('label');
label.setAttribute('for', `dataset-${index}`);
label.textContent = `Enter alternate name for ${dataset}: `;
// Create an input field
let input = document.createElement('input');
input.type = 'text';
input.id = `dataset-${index}`;
input.name = `dataset-${index}`;
input.placeholder = `Alternate name for ${dataset}`;
// Append the label and input to the form
form.appendChild(label);
form.appendChild(input);
// Add a line break for spacing
form.appendChild(document.createElement('br'));
index += 1;
}
}
function updateDatasetLabels() {
let form = document.getElementById('datasetLabelsForm');
let formData = new FormData(form); // Collect form data
// let result = {};
let index = 0;
// Iterate through form entries and collect values
for (dataset in selectedSampleInfo) {
let altName = formData.get(`dataset-${index}`);
//console.log(`dataset-${index}`);
//console.log('altName ',altName);
if (!(altName === '')) {
// result[dataset] = altName;
selectedSampleInfo[dataset].label = altName;
}
index += 1;
}
closeDatasetLabels();
// console.log(result); // Log the result to the console
}
function closeDatasetLabels() {
const datasetLabels = document.getElementById('datasetLabels');
datasetLabels.style.display = 'none';
}
function openSampleLabels() {
const sampleLabels = document.getElementById('sampleLabels');
sampleLabels.style.display = 'block';
let form = document.getElementById('sampleLabelsForm');
form.innerHTML = '';
let index = 0;
for (dataset in selectedSampleInfo) {
for (sample in selectedSampleInfo[dataset].position) {
// Create a label
let label = document.createElement('label');
label.setAttribute('for', `sample-${index}`);
label.textContent = `Enter alternate name for ${sample}: `;
// Create an input field
let input = document.createElement('input');
input.type = 'text';
input.id = `sample-${index}`;
input.name = `sample-${index}`;
input.placeholder = `Alternate name for ${sample}`;
// Append the label and input to the form
form.appendChild(label);
form.appendChild(input);
// Add a line break for spacing
form.appendChild(document.createElement('br'));
index += 1;
}
}
}
function updateSampleLabels() {
let form = document.getElementById('sampleLabelsForm');
let formData = new FormData(form); // Collect form data
// let result = {};
let index = 0;
// Iterate through form entries and collect values
for (dataset in selectedSampleInfo) {
for (sample in selectedSampleInfo[dataset].position) {
let altName = formData.get(`sample-${index}`);
//console.log(`sample-${index}`);
//console.log('altName ',altName);
if (!(altName === '')) {
// result[sample] = altName;
selectedSampleInfo[dataset].position[sample].label = altName;
}
index += 1;
}
}
closeSampleLabels();
// console.log(result); // Log the result to the console
}
function closeSampleLabels() {
const sampleLabels = document.getElementById('sampleLabels');
sampleLabels.style.display = 'none';
}
function openChemicalSelection(sampleMeasurements) {
const chemicalModal = document.getElementById('chemicalModal');
chemicalModal.style.display = 'block';
const chemicalCheckboxes = document.getElementById('chemicalCheckboxes');
chemicalCheckboxes.innerHTML = '';
const datesSampled = Object.keys(selectedSampleMeasurements);
for (chemicalType in selectedSampleMeasurements[datesSampled[0]]) {
if (chemicalType !== 'Physical Data') {
const chemicals = Object.keys(selectedSampleMeasurements[datesSampled[0]][chemicalType].chemicals);
const checkboxContainer = document.createElement('div');
checkboxContainer.className = 'checkbox-container';
chemicals.forEach(chemical => {
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `chemical`;
checkbox.name = 'chemical';
checkbox.value = chemical;
checkbox.checked = true; // Initially all chemicals are checked
const label = document.createElement('label');
label.htmlFor = `chemical_${chemical}`;
label.appendChild(document.createTextNode(chemical));
checkboxContainer.appendChild(checkbox);
checkboxContainer.appendChild(label);
checkboxContainer.appendChild(document.createElement('br'));
});
chemicalCheckboxes.appendChild(checkboxContainer);
}
}
}
function flipChemicalSelections(selection) {
const checkboxes = document.querySelectorAll('#chemicalCheckboxes input[type="checkbox"]');
checkboxes.forEach(checkbox => {
if (selection) {
checkbox.checked = true;
} else {
checkbox.checked = false;
};
});
}
function applyChemicalFilter() {
const containsText = document.getElementById('containsTextChemical').value.toLowerCase();
const checkboxes = document.querySelectorAll('#chemicalCheckboxes input[type="checkbox"]');
checkboxes.forEach(checkbox => {
if (containsText.length > 0) {
if (checkbox.nextSibling.textContent.toLowerCase().includes(containsText)) {
checkbox.checked = true;
} else {
checkbox.checked = false;
}
}
});
}
function closeChemicalSelection() {
selectChemicals();
const chemicalModal = document.getElementById('chemicalModal');
chemicalModal.style.display = 'none';
}
function selectChemicals() {
const checkboxes = document.querySelectorAll('input[name="chemical"]:checked');
const selectedChemicals = Array.from(checkboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
selectedSampleMeasurements = getSelectedChemicalSampleMeasurements(selectedChemicals);
selectedSampleInfo = getSelectedChemicalSampleInfo(selectedChemicals);
updateChart();
}
function getSelectedChemicalSampleMeasurements(selectedChemicals) {
//console.log(selectedChemicals);
//console.log(selectedSampleInfo);
//console.log(selectedSampleMeasurements);
selectedMeas = {};
for (dateSampled in selectedSampleMeasurements) {
for (const chemicalType in selectedSampleMeasurements[dateSampled]) {
if ('Physical Data' != chemicalType) {
for (const chemical in selectedSampleMeasurements[dateSampled][chemicalType].chemicals) {
if (selectedChemicals.includes(chemical)) {
// Put here as if no chemicals selected then don't need chemical type
if (!selectedMeas[dateSampled]) {
selectedMeas[dateSampled] = {};
}
if (!(chemicalType in selectedMeas[dateSampled])) {
selectedMeas[dateSampled][chemicalType] = {};
selectedMeas[dateSampled][chemicalType].chemicals = {};
selectedMeas[dateSampled][chemicalType]['Unit of measurement'] = sampleMeasurements[dateSampled][chemicalType]['Unit of measurement'];
if (chemicalType == 'PAH data') {
// Just copy all common data even if only 1 PAH is selected
if (sampleMeasurements[dateSampled][chemicalType].gorhamTest !== undefined) selectedMeas[dateSampled][chemicalType].gorhamTest = sampleMeasurements[dateSampled][chemicalType].gorhamTest;
if (sampleMeasurements[dateSampled][chemicalType].total !== undefined) selectedMeas[dateSampled][chemicalType].total = sampleMeasurements[dateSampled][chemicalType].total;
if (sampleMeasurements[dateSampled][chemicalType].totalHC !== undefined) selectedMeas[dateSampled][chemicalType].totalHC = sampleMeasurements[dateSampled][chemicalType].totalHC;
if (sampleMeasurements[dateSampled][chemicalType].totalHCUnit !== undefined) selectedMeas[dateSampled][chemicalType].totalHCUnit = sampleMeasurements[dateSampled][chemicalType].totalHCUnit;
}
if (chemicalType == 'PCB data') {
//console.log('Create ', dateSampled, chemicalType,'Gorham Test');
/* selectedMeas[dateSampled][chemicalType].congenerTest = {};
selectedMeas[dateSampled][chemicalType].congenerTest[sample] = sampleMeasurements[dateSampled][chemicalType].congenerTest[sample];*/
// selectedMeas[dateSampled][chemicalType].congenerTest = {};
if (sampleMeasurements[dateSampled][chemicalType].congenerTest !== undefined) selectedMeas[dateSampled][chemicalType].congenerTest = sampleMeasurements[dateSampled][chemicalType].congenerTest;
if (sampleMeasurements[dateSampled][chemicalType].total !== undefined) selectedMeas[dateSampled][chemicalType].total = sampleMeasurements[dateSampled][chemicalType].total;
}
}
for (sample in selectedSampleInfo[dateSampled].position) {
//console.log(selectedSampleMeasurements,dateSampled,chemicalType,chemical);
//console.log(selectedMeas[dateSampled][chemicalType].chemicals[chemical]);
if (!selectedMeas[dateSampled][chemicalType]?.chemicals[chemical]) {
selectedMeas[dateSampled][chemicalType].chemicals[chemical] = {};
selectedMeas[dateSampled][chemicalType].chemicals[chemical].samples = {};
}
/* if (!selectedMeas[dateSampled][chemicalType].chemicals[chemical]?.samples) {
selectedMeas[dateSampled][chemicalType].chemicals[chemical].samples = {};
}*/
//console.log(selectedMeas[dateSampled][chemicalType].chemicals[chemical]);
selectedMeas[dateSampled][chemicalType].chemicals[chemical].samples[sample] = selectedSampleMeasurements[dateSampled][chemicalType].chemicals[chemical].samples[sample];
/* if (!selectedMeas[dateSampled]?.["Physical Data"]) {
selectedMeas[dateSampled]["Physical Data"] = {};
selectedMeas[dateSampled]["Physical Data"]["Date analysed"] = selectedSampleMeasurements[dateSampled]["Physical Data"]["Date analysed"]
selectedMeas[dateSampled]["Physical Data"]["Laboratory/contractor"] = selectedSampleMeasurements[dateSampled]["Physical Data"]["Laboratory/contractor"];
selectedMeas[dateSampled]["Physical Data"]["Unit of measurement"] = selectedSampleMeasurements[dateSampled]["Physical Data"]["Unit of measurement"];
selectedMeas[dateSampled]["Physical Data"].sizes = selectedSampleMeasurements[dateSampled]["Physical Data"].sizes;
selectedMeas[dateSampled]["Physical Data"].samples = {};
}*/
if (selectedSampleMeasurements[dateSampled]["Physical Data"]?.samples[sample] !== undefined) {
selectedMeas[dateSampled]["Physical Data"].samples[sample] = selectedSampleMeasurements[dateSampled]["Physical Data"].samples[sample];
}
}
}
}
}
}
}
for (dateSampled in selectedMeas) {
if ('Physical Data' in selectedSampleMeasurements[dateSampled]){
for (sample in selectedSampleInfo[dateSampled].position) {
selectedMeas[dateSampled]['Physical Data'] = selectedSampleMeasurements[dateSampled]['Physical Data'];
}
}
}
return selectedMeas;
}
function getSelectedChemicalSampleInfo(selectedChemicals) {
let selectedSamps = {};
for (const dateSampled in selectedSampleMeasurements) {
selectedSamps[dateSampled] = {};
selectedSamps[dateSampled]['Date sampled'] = selectedSampleInfo[dateSampled]['Date sampled'];
selectedSamps[dateSampled].fileURL = selectedSampleInfo[dateSampled].fileURL;
selectedSamps[dateSampled].Applicant = selectedSampleInfo[dateSampled].Applicant;
selectedSamps[dateSampled]['Application number'] = selectedSampleInfo[dateSampled]['Application number'];
selectedSamps[dateSampled]['Application title'] = selectedSampleInfo[dateSampled]['Application title'];
selectedSamps[dateSampled]['label'] = selectedSampleInfo[dateSampled]['label'];
for (const chemicalType in selectedSampleMeasurements[dateSampled]) {
if (!('position' in selectedSamps[dateSampled])) {
//console.log('setting up position');
selectedSamps[dateSampled].position = {};
}
if ('Physical Data' != chemicalType) {
//console.log('getting positions',chemicalType,chemical);
for (const chemical in selectedSampleMeasurements[dateSampled][chemicalType].chemicals) {
if (selectedChemicals.includes(chemical)) {
for (const sample in selectedSampleMeasurements[dateSampled][chemicalType].chemicals[chemical].samples) {
//if (sample.includes('Scar')) {
// console.log('Sample',sample);
//}
selectedSamps[dateSampled].position[sample] = selectedSampleInfo[dateSampled].position[sample];
}
}
}
}
}
}
return selectedSamps;
}
function openDatasetSelection(selectedSampleMeasurements) {
const sampleModal = document.getElementById('datasetModal');
sampleModal.style.display = 'block';
}
function yourapplyDatasetFilter() {
const sampleModal = document.getElementById('datasetModal');
sampleModal.style.display = 'none';
let sheetsToSelect = {}; // Use an object
for (let i = 0; i < dataSheetNames.length; i++) {
let sheetName = dataSheetNames[i]; // Declare sheetName with let
sheetsToSelect[sheetName] = document.getElementById(dataSheetNamesCheckboxes[i] + 'set').checked;
}
console.log(sheetsToSelect);
console.log(Object.keys(sheetsToSelect));
let datasetsToKeep = {}; // Use an object
for (let dateSelected in selectedSampleInfo) {
datasetsToKeep[dateSelected] = true;
for (let sheetName in sheetsToSelect) {
if (sheetsToSelect[sheetName]) {
console.log(sheetName);
if (!(sheetName in selectedSampleMeasurements[dateSelected])) {
datasetsToKeep[dateSelected] = false;
console.log(dateSelected, ' has no ', sheetName);
break;
}
}
}
console.log(dateSelected, ' has ', sheetName);
}
console.log(datasetsToKeep);
selectedSampleInfo = {};
selectedSampleMeasurements = {};
for (let dateSelected in datasetsToKeep) {
if (datasetsToKeep[dateSelected]) {
selectedSampleInfo[dateSelected] = sampleInfo[dateSelected];
selectedSampleMeasurements[dateSelected] = sampleMeasurements[dateSelected];
}
}
updateChart();
}
function applyDatasetFilter() {
const sampleModal = document.getElementById('datasetModal');
sampleModal.style.display = 'none';
let sheetsToSelect = {};
for (let i = 0; i < dataSheetNames.length; i++) {
//sheetName = dataSheetNames[i];
sheetsToSelect[dataSheetNames[i]] = document.getElementById(dataSheetNamesCheckboxes[i]+'set').checked ? true : false; // Check the checkbox state
}
//console.log(sheetsToSelect);
//console.log(Object.keys(sheetsToSelect));
let datasetsToKeep = {};
for (let dateSelected in selectedSampleInfo) {
datasetsToKeep[dateSelected] = true;
for (let sheetName in sheetsToSelect) {
//console.log(sheetName);
if (sheetsToSelect[sheetName]) {
//console.log(sheetName);
if (!(sheetName in selectedSampleMeasurements[dateSelected])) {
datasetsToKeep[dateSelected] = false;
//console.log(dateSelected,' has no ',sheetName);
break;
}
// keepInfo = sampleInfo[dateSelected];
// keepSample = sampleMeasurements[dateSelected]
}
}
//console.log(dateSelected,' has ',sheetName);
}
//console.log(datasetsToKeep);
selectedSampleInfo = {};
selectedSampleMeasurements = {};
for (let dateSelected in datasetsToKeep) {
if (datasetsToKeep[dateSelected]) {
selectedSampleInfo[dateSelected] = sampleInfo[dateSelected];
selectedSampleMeasurements[dateSelected] = sampleMeasurements[dateSelected];
}
}
updateChart();
}
function openSampleSelection(sampleMeasurements) {
const sampleModal = document.getElementById('sampleModal');
sampleModal.style.display = 'block';
const sampleCheckboxes = document.getElementById('sampleCheckboxes');
sampleCheckboxes.innerHTML = '';
// const datesSampled = Object.keys(sampleInfo);
const datesSampled = Object.keys(selectedSampleInfo);
datesSampled.sort();
datesSampled.forEach(dateSampled => {
// for (dateSampled in sampleInfo) {
const samples = Object.keys(selectedSampleInfo[dateSampled].position);
samples.sort();
const checkboxContainer = document.createElement('div');
checkboxContainer.className = 'checkbox-container';
samples.forEach(sample => {
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `sample_${dateSampled + ': ' + sample}`;
checkbox.name = 'sample';
checkbox.value = dateSampled + ': ' + sample;
checkbox.checked = true; // Initially all samples are checked
const label = document.createElement('label');
label.htmlFor = `sample_${sample}`;
label.appendChild(document.createTextNode(dateSampled + ': ' + sample));
checkboxContainer.appendChild(checkbox);
checkboxContainer.appendChild(label);
checkboxContainer.appendChild(document.createElement('br'));
});
sampleCheckboxes.appendChild(checkboxContainer);
});
createStandardFilterUI();
populateAreaFilter();
}
function flipSampleSelections(selection) {
const checkboxes = document.querySelectorAll('#sampleCheckboxes input[type="checkbox"]');
checkboxes.forEach(checkbox => {
if (selection) {
checkbox.checked = true;
} else {
checkbox.checked = false;
};
});
}
/*function selectHighlighted(selection) {
const checkboxes = document.querySelectorAll('#sampleCheckboxes input[type="checkbox"]');
index = 0;
// for (ds in selectedSampleInfo) {
// for (s in selectedSampleInfo[ds]) {
checkboxes.forEach(checkbox => {
checkbox.checked = highlighted[index];
index += 1;
});
// }
// }
}*/
function selectHighlighted(selection) {
// 1. Create the master list of all samples and sort it EXACTLY as in createHighlights.
// This list correctly corresponds to the indices in the 'highlighted' array.
const masterSampleList = [];
const datesSampled = Object.keys(selectedSampleInfo);
datesSampled.forEach(date => {
const samplesForDate = Object.keys(selectedSampleInfo[date].position);
samplesForDate.forEach(sampleName => {
masterSampleList.push(date + ': ' + sampleName);
});
});
masterSampleList.sortComplexSamples(); // Use the same custom sort function
// 2. Get all checkboxes from the modal.
const checkboxes = document.querySelectorAll('#sampleCheckboxes input[type="checkbox"]');
// 3. Create a map of checkboxes by their value (which is the unique sample ID) for fast lookups.
const checkboxMap = new Map();
checkboxes.forEach(cb => checkboxMap.set(cb.value, cb));
// 4. Iterate through the master sorted list. For each sample, check if it should be highlighted.
masterSampleList.forEach((sampleId, index) => {
const checkbox = checkboxMap.get(sampleId);
if (checkbox) {
// Use the index from the sorted list to check the 'highlighted' array.
// This ensures the correct checkbox is updated.
checkbox.checked = highlighted[index] || false; // Default to false if undefined
}
});
}
function applySampleFilter() {
let sheetsToSelect = [];
selectBySheet = false;
for (i = 0; i < dataSheetNames.length; i++) {
sheetName = dataSheetNames[i];
console.log('sheetName',sheetName,dataSheetNamesCheckboxes[i]);
sheetsToSelect[dataSheetNames[i]] = document.getElementById(dataSheetNamesCheckboxes[i]+'sample').checked ? true : false; // Check the checkbox state
if (sheetsToSelect[dataSheetNames[i]]) {
selectBySheet = true;
}
}
const checkboxes = document.querySelectorAll('#sampleCheckboxes input[type="checkbox"]');
const checkboxesIn = document.querySelectorAll('input[name="sample"]:checked');
//console.log(checkboxes,checkboxesIn);
//console.log(Object.keys(checkboxes));
let checkboxesNames = [];
for (let i = 0; i < checkboxes.length; i++) {
checkboxesNames[i] = checkboxes[i].value;
}
if (selectBySheet) {
index = 0;
samplesToKeep = {};
// for (const sheetName in sheetsToSelect) {
// for (const dateSelected in selectedSampleMeasurements) {
for (const dateSelected in selectedSampleMeasurements) {
for (const sample in selectedSampleInfo[dateSelected].position) {
//a for (const sample in sampleInfo[dateSelected].position) {
samplesToKeep[dateSelected + ': ' + sample] = true;
}
for (const sheetName in sheetsToSelect) {
//console.log('Should I check for',sheetName);
if (sheetsToSelect[sheetName]) {
//console.log('I should check for',sheetName);
if (sheetName in selectedSampleMeasurements[dateSelected]) {
//console.log(dateSelected,'has',sheetName,'so setting all false');
for (const sample in selectedSampleInfo[dateSelected].position) {
//a for (const sample in sampleInfo[dateSelected].position) {
samplesToKeep[dateSelected + ': ' + sample] = false;
}
for (const chemical in selectedSampleMeasurements[dateSelected][sheetName].chemicals) {
for (const sample in selectedSampleMeasurements[dateSelected][sheetName].chemicals[chemical].samples) {
//console.log(dateSelected,sheetName,chemical);
/* if (sample === 'CSA 2.00') {
index += 1;
console.log(index,sample,chemical);
}*/
if (!samplesToKeep[dateSelected + ': ' + sample]) {
if (selectedSampleMeasurements[dateSelected][sheetName].chemicals[chemical].samples[sample] > 0) {
//console.log('keeping',dateSelected,sample);
samplesToKeep[dateSelected + ': ' + sample] = true;
}
}
}
}
} else {
if (!(sheetName === 'Physical Data')) {
for (const sample in selectedSampleInfo[dateSelected].position) {
//a for (const sample in sampleInfo[dateSelected].position) {
//console.log(dateSelected,'has no',sheetName,'so setting all false');
samplesToKeep[dateSelected + ': ' + sample] = false;
}
}
}
}
}
}
//console.log(samplesToKeep);
for (const sampleName in samplesToKeep) {
if(samplesToKeep[sampleName]) {
//console.log('keeping ',sampleName);
const checkName = `sample_${sampleName}`;
const checkbox = document.getElementById(checkName);
checkbox.checked = true;
}
}
}
//console.log(checkboxes);
const containsText = document.getElementById('containsText').value.toLowerCase();
const minDepth = parseFloat(document.getElementById('minDepth').value);
const maxDepth = parseFloat(document.getElementById('maxDepth').value);
centreLat = parseFloat(document.getElementById('centreLat').value);
centreLon = parseFloat(document.getElementById('centreLon').value);
const centreDist = parseFloat(document.getElementById('centreDist').value);
// const checkboxes = document.querySelectorAll('#sampleCheckboxes input[type="checkbox"]');
// const checkboxesIn = document.querySelectorAll('input[name="sample"]:checked');
checkboxes.forEach(checkbox => {
if (containsText.length > 0) {
if (checkbox.nextSibling.textContent.toLowerCase().includes(containsText)) {
checkbox.checked = true;
} else {
checkbox.checked = false;
}
}
});
if (!(isNaN(minDepth) || isNaN(maxDepth)) && (minDepth <= maxDepth)) {
checkboxes.forEach(checkbox => {
checkbox.checked = false;
});
for (const dateSelected in selectedSampleInfo) {
for (const sample in selectedSampleInfo[dateSelected].position) {
const minSample = selectedSampleInfo[dateSelected].position[sample]['Sampling depth (m)'].minDepth;
if (minDepth <= minSample) {
const maxSample = selectedSampleInfo[dateSelected].position[sample]['Sampling depth (m)'].maxDepth;
/*a for (const dateSelected in sampleInfo) {
for (const sample in sampleInfo[dateSelected].position) {
const minSample = sampleInfo[dateSelected].position[sample]['Sampling depth (m)'].minDepth;
if (minDepth <= minSample) {
const maxSample = sampleInfo[dateSelected].position[sample]['Sampling depth (m)'].maxDepth;*/
if (maxDepth >= maxSample) {
const checkName = `sample_${dateSelected + ': ' + sample}`;
const checkbox = document.getElementById(checkName);
checkbox.checked = true;
}
}
}
}
}
// going to find samples close to a set of coordinates
if (!isNaN(centreDist)) {
// latitude and longitude not supplied
if ((isNaN(centreLat) || isNaN(centreLon))) {
// so assuming only one checkbox is ticked then find all samples near to that position
const selectedSamples = Array.from(checkboxesIn)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
if (selectedSamples.length === 1) {
for (const dateSampled in selectedSampleInfo) {
for (const sample in selectedSampleInfo[dateSampled].position) {
if (selectedSamples.includes(dateSampled + ': ' + sample)) {
centreLat = selectedSampleInfo[dateSampled].position[sample]['Position latitude'];
centreLon = selectedSampleInfo[dateSampled].position[sample]['Position longitude'];
/*a for (const dateSampled in sampleInfo) {
for (const sample in sampleInfo[dateSampled].position) {
if (selectedSamples.includes(dateSampled + ': ' + sample)) {
centreLat = sampleInfo[dateSampled].position[sample]['Position latitude'];
centreLon = sampleInfo[dateSampled].position[sample]['Position longitude'];*/
}
}
}
} else {
return
}
}
checkboxes.forEach(checkbox => {
checkbox.checked = false;
});
for (const dateSelected in selectedSampleInfo) {
for (const sample in selectedSampleInfo[dateSelected].position) {
const sampleLat = selectedSampleInfo[dateSelected].position[sample]['Position latitude'];
const sampleLon = selectedSampleInfo[dateSelected].position[sample]['Position longitude'];
/*a for (const dateSelected in sampleInfo) {
for (const sample in sampleInfo[dateSelected].position) {
const sampleLat = sampleInfo[dateSelected].position[sample]['Position latitude'];
const sampleLon = sampleInfo[dateSelected].position[sample]['Position longitude'];*/
distance = 1000 * haversineDistance(sampleLat, sampleLon, centreLat, centreLon);
if (distance <= centreDist) {
const checkName = `sample_${dateSelected + ': ' + sample}`;
const checkbox = document.getElementById(checkName);
checkbox.checked = true;
}
}
}
}
// going to find sample with specific measurements
/* for (const dateSelected in selectedSampleMeasurements) {
for (const sheetName in selectedSampleMeasurements[dateSelected]) {
const mustChemicalType = document.getElementById((sheetName + 'sample').replace(/\s/g, '').toLowerCase()).checked;
if (mustChemicalType) {
for (const chemical in selectedSampleMeasurements[dateSelected][sheetName].chemicals) {
for (const sample in selectedSampleInfo[dateSelected].position) {
const conc = selectedSampleMeasurements[dateSelected][sheetName].chemicals[chemical].samples[sample];
console.log(dateSelected,sheetName,chemical,sample,conc);
if (conc === null || conc == undefined) {
const checkName = `sample_${dateSelected + ': ' + sample}`;
const checkbox = document.getElementById(checkName);
checkbox.checked = false;
}
}
}
}
}
}*/
for (let i = 0; i < dataSheetNames.length; i++) {
sheetName = dataSheetNames[i];
const mustChemicalType = document.getElementById((sheetName + 'sample').replace(/\s/g, '').toLowerCase()).checked;
if(mustChemicalType) {
for (let j = 0; j < checkboxesNames.length; j++) {
let parts = checkboxesNames[j].split(": ");
if (parts.length > 2) parts[1] = parts[1] + ': ' + parts[2];
const dateSampled = parts[0];
const sample = parts[1];
const checkName = `sample_${dateSampled + ': ' + sample}`;
const checkbox = document.getElementById(checkName);
if (!(sheetName in selectedSampleMeasurements[dateSampled])) {
checkbox.checked = false;
} else {
// Assume no chemicals are present for this sample
checkbox.checked = false;
for (const chemical in selectedSampleMeasurements[dateSampled][sheetName].chemicals) {
const conc = selectedSampleMeasurements[dateSampled][sheetName].chemicals[chemical].samples[sample];
// console.log(dateSampled,sheetName,chemical,sample,conc);
/* if (conc === null || conc == undefined) {
const checkName = `sample_${dateSampled + ': ' + sample}`;
const checkbox = document.getElementById(checkName);
checkbox.checked = false;
}*/
// If any chemical has a concentration > 0 then keep the sample
if (conc > 0) {
checkbox.checked = true;
break;
}
}
}
}
}
}
}
function haversineDistance(lat1, lon1, lat2, lon2) {
const R = 6371; // Radius of the Earth in kilometers
const dLat = toRadians(lat2 - lat1);
const dLon = toRadians(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = R * c; // Distance in kilometers
return distance;
}
function toRadians(degrees) {
return degrees * (Math.PI / 180);
}
function closeSampleSelection() {
selectSamples();
const sampleModal = document.getElementById('sampleModal');
sampleModal.style.display = 'none';
}
function selectSamples() {
const checkboxes = document.querySelectorAll('input[name="sample"]:checked');
//console.log(checkboxes);
const selectedSamples = Array.from(checkboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
//console.log(selectedSamples);
selectedSampleMeasurements = getSelectedSampleMeasurements(selectedSamples);
selectedSampleInfo = getSelectedSamples(selectedSamples);
console.log(selectedSampleInfo);
updateChart();
}
function getSelectedSampleMeasurements(selectedSamples) {
//console.log('getSelectedSampleMeasurements',selectedSamples,selectedSamples.length);
selectedMeas = {};
for (let i=0; i < selectedSamples.length; i++) {
let parts = selectedSamples[i].split(": ");
if (parts.length>2) {
parts[1] = parts[1] + ': ' + parts[2];
}
dateSampled = parts[0];
sample = parts[1];
let dataset = sampleMeasurements[dateSampled];
if (!selectedMeas[dateSampled]) {
selectedMeas[dateSampled] = {};
}
const chemicalTypes = Object.keys(sampleMeasurements[dateSampled]);
for (const chemicalType in sampleMeasurements[dateSampled]) {
if (!selectedMeas[dateSampled][chemicalType]) {
//console.log('Create ', dateSampled);
selectedMeas[dateSampled][chemicalType] = {};
}
//console.log(dateSampled,sample,chemicalType);
if (chemicalType === 'Physical Data') {
if (!selectedMeas[dateSampled][chemicalType].samples) {
selectedMeas[dateSampled][chemicalType].samples = {};
selectedMeas[dateSampled][chemicalType]['Unit of measurement'] = sampleMeasurements[dateSampled][chemicalType]['Unit of measurement'];
selectedMeas[dateSampled][chemicalType].sizes = sampleMeasurements[dateSampled][chemicalType].sizes;
//console.log('1 psd selectedMeas ',dateSampled,chemicalType,selectedMeas);
//console.log('2 psd selectedMeas ',dateSampled,chemicalType,selectedMeas);
}
if (sampleMeasurements[dateSampled][chemicalType].samples[sample] !== undefined) {
selectedMeas[dateSampled][chemicalType].samples[sample] = sampleMeasurements[dateSampled][chemicalType].samples[sample];
}
} else {
// for (const chemicalType in sampleMeasurements[dateSampled]) {
let newData = {};
if (selectedMeas[dateSampled]?.[chemicalType]) {
newData = selectedMeas[dateSampled][chemicalType];
} else {
newData = {};
}
console.log(chemicalType, dataset, newData);
for (const key in dataset[chemicalType]) {
let currentData = dataset[chemicalType][key];
console.log(chemicalType,key,currentData);
if (key === 'chemicals') {
if (!newData.chemicals) {
newData.chemicals = {};
for (const chemical in dataset[chemicalType].chemicals) {
//console.log('Create ', dateSampled, chemicalType,chemical);
newData.chemicals[chemical] = {};
newData.chemicals[chemical].samples = {};
if (currentData[chemical].samples[sample] !== undefined) {
newData.chemicals[chemical].samples[sample] = currentData[chemical].samples[sample];
}
}
} else {
newData.chemicals = selectedMeas[dateSampled][chemicalType].chemicals;
for (const chemical in dataset[chemicalType].chemicals) {
//console.log('Create ', dateSampled, chemicalType, chemical,newData, currentData);
if (currentData[chemical].samples[sample] !== undefined) {
newData.chemicals[chemical].samples[sample] = currentData[chemical].samples[sample];
}
}
}
} else {
if (isSingleValue(currentData)) {
if (!newData[key]) {
newData[key] = currentData;
}
} else {
//if (key === 'gorhamTest') {
console.log(dateSampled,sample,key,currentData,newData);
//}
if (currentData) {
if (!newData[key]) {
newData[key] = {};
}
if (currentData[sample] !== undefined) {
newData[key][sample] = currentData[sample];
}
}
}
}
}
//console.log(newData);
selectedMeas[dateSampled][chemicalType] = newData;
}
}
}
//console.log(selectedMeas); // Output each cell value to console
return selectedMeas;
}
function getSelectedSamples(selectedSamples) {
let selectedSamps = {};
//let numberNow = 0;
for (let i=0; i < selectedSamples.length; i++) {
let parts = selectedSamples[i].split(": ");
if (parts.length>2) {
parts[1] = parts[1] + ': ' + parts[2];
}
dateSampled = parts[0];
sample = parts[1];
// for (const dateSampled in selectedSampleInfo) {
// for (const sample in selectedSampleInfo[dateSampled].position) {
//numberNow += 1;
// if (selectedSamples.includes(dateSampled + ': ' + sample)) {
// console.log('a point ' + dateSampled + ': ' + sample);
if (!selectedSamps[dateSampled]) {
selectedSamps[dateSampled] = {};
}
for (const key in selectedSampleInfo[dateSampled]) {
currentData = selectedSampleInfo[dateSampled][key];
if (isSingleValue(currentData)) {
selectedSamps[dateSampled][key] = currentData;
} else {
if (!selectedSamps[dateSampled][key]) {
selectedSamps[dateSampled][key] = {};
}
//console.log(dateSampled, sample, currentData, selectedSamps);
selectedSamps[dateSampled][key][sample] = currentData[sample];
}
}
}
// selectedSampleInfo = selectedSamps;
//console.log(selectedSamps); // Output each cell value to console
//console.log('numberNow',numberNow);
fred=selectedSamps;
return selectedSamps;
}
function clearSelections() {
selectedSampleMeasurements = sampleMeasurements;
selectedSampleInfo = sampleInfo;
updateChart();
}
function populateAreaFilter() {
const areaSelect = document.getElementById('areaSelect');
if (!areaSelect) return;
// Clear existing options except the first "Select an Area"
areaSelect.innerHTML = '<option value="">-- Select an Area --</option>';
// Check if we have shapes loaded (allShapeLayers is global from sdeMaps.js)
if (typeof allShapeLayers === 'undefined' || allShapeLayers.length === 0) {
const option = document.createElement('option');
option.text = "No shapes loaded";
option.disabled = true;
areaSelect.appendChild(option);
return;
}
allShapeLayers.forEach((layer, index) => {
// 1. Extract Name
// We try standard properties, then the tooltip if properties are missing
let name = layer.options?.name || layer.name || layer.feature?.properties?.name || "Unnamed Area";
// If name is still generic, try to parse it from the binded Tooltip content
if ((name === "Unnamed Area" || name === "") && layer.getTooltip()) {
const tooltipContent = layer.getTooltip().getContent();
// Regex to strip HTML tags like <b>Name</b>
const div = document.createElement("div");
div.innerHTML = tooltipContent;
name = div.innerText.split('\n')[0] || "Unnamed Area";
}
// 2. Extract Area
// We recalculate it here to be safe, ensuring consistency with sdeMaps.js
let areaHa = 0;
const latLngs = layer.getLatLngs();
if (Array.isArray(latLngs) && latLngs.length > 0) {
// Handle simple polygons vs polygons with holes/multipolygons
if (Array.isArray(latLngs[0]) && !Array.isArray(latLngs[0][0]) && typeof latLngs[0][0] !== 'number') {
// Polygon with holes or simple nested array
let areaM2 = L.GeometryUtil.geodesicArea(latLngs[0]);
for (let i = 1; i < latLngs.length; i++) {
areaM2 -= L.GeometryUtil.geodesicArea(latLngs[i]);
}
areaHa = areaM2 / 10000;
} else {
// Simple Polygon
areaHa = L.GeometryUtil.geodesicArea(latLngs) / 10000;
}
}
// 3. Create Option
const option = document.createElement('option');
option.value = index; // We use the index in allShapeLayers array as the value
option.text = `${name} (${areaHa.toFixed(2)} ha)`;
areaSelect.appendChild(option);
});
}
/**
* Triggered when the user selects an area from the dropdown.
* Unchecks all samples, then checks only those falling inside the selected polygon.
*/
function applyAreaFilter() {
const areaSelect = document.getElementById('areaSelect');
const selectedIndex = areaSelect.value;
// If no area selected, do nothing (or reset? usually better to do nothing so other filters work)
if (selectedIndex === "") return;
const selectedLayer = allShapeLayers[parseInt(selectedIndex)];
if (!selectedLayer) return;
// 1. Deselect all checkboxes first
const checkboxes = document.querySelectorAll('#sampleCheckboxes input[type="checkbox"]');
checkboxes.forEach(cb => cb.checked = false);
// 2. Iterate all samples and check if inside polygon
// Note: selectedSampleInfo is global from the main app
for (const dateSampled in selectedSampleInfo) {
for (const sample in selectedSampleInfo[dateSampled].position) {
// Get Sample Coordinates
const lat = parseFloat(selectedSampleInfo[dateSampled].position[sample]['Position latitude']);
const lon = parseFloat(selectedSampleInfo[dateSampled].position[sample]['Position longitude']);
if (isNaN(lat) || isNaN(lon)) continue;
// Check if point is inside the selected layer
if (isPointInLayer(lat, lon, selectedLayer)) {
// Check the specific checkbox
// ID format from openSampleSelection: `sample_${dateSampled + ': ' + sample}`
const checkBoxId = `sample_${dateSampled}: ${sample}`;
const checkbox = document.getElementById(checkBoxId);
if (checkbox) checkbox.checked = true;
}
}
}
}
/**
* Helper: Checks if a Lat/Lon is inside a Leaflet Polygon Layer.
* Handles Polygons, Polygons with holes, and MultiPolygons.
*/
function isPointInLayer(lat, lon, layer) {
const point = [lat, lon];
const latlngs = layer.getLatLngs();
// Leaflet stores LatLngs differently depending on shape complexity:
// 1. Simple Polygon: [ [Lat,Lon], [Lat,Lon] ... ] (Wait, Leaflet uses objects usually, but we need points)
// Actually Leaflet .getLatLngs() returns [ L.LatLng, L.LatLng... ] for simple,
// or [ [L.LatLng...], [L.LatLng...] ] for holes/multipolygons.
// Helper to run Ray Casting on a ring of L.LatLng objects
function isInsideRing(pt, ring) {
let x = pt[0], y = pt[1];
let inside = false;