-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdeDredgeData.js
404 lines (385 loc) · 19.6 KB
/
sdeDredgeData.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
CEFASdata = {};
CEFASfile = {};
CEFASfilename = '';
CEFASUniqueRows = {};
ddLookup = {};
ddLookup.chemical = {'pcb101' : "2,2',4,5,5'-Pentachlorobiphenyl" , 'pcb105' : "2,3,3',4,4'-Pentachlorobiphenyl" , 'pcb110' : "2,3,3',4',6-Pentachlorobiphenyl" ,
'pcb118' : "2,3',4,4',5-Pentachlorobiphenyl" , 'pcb128' : "2,2',3,3',4,4'-Hexachlorobiphenyl" , 'pcb138' : "2,2',3,4,4',5'-Hexachlorobiphenyl" ,
'pcb141' : "2,2',3,4,5,5'-Hexachlorobiphenyl" , 'pcb149' : "2,2',3,4',5',6-Hexachlorobiphenyl" , 'pcb151' : "2,2',3,5,5',6-Hexachlorobiphenyl" ,
'pcb153' : "2,2',4,4',5,5'-Hexachlorobiphenyl" , 'pcb156' : "2,3,3',4,4',5-Hexachlorobiphenyl" , 'pcb158' : "2,3,3',4,4',6-Hexachlorobiphenyl" ,
'pcb170' : "2,2',3,3',4,4',5-Heptachlorobiphenyl" , 'pcb18' : "2,2',5- Trichlorobiphenyl" , 'pcb180' : "2,2',3,4,4',5,5'-Heptachlorobiphenyl" ,
'pcb183' : "2,2',3,4,4',5',6-Heptachlorobiphenyl" , 'pcb187' : "2,2',3,4',5,5',6-Heptachlorobiphenyl" , 'pcb194' : "2,2',3,3',4,4',5,5'-Octachlorobiphenyl" ,
'pcb28' : "2,4,4'-Trichlorobiphenyl" , 'pcb31' : "2,4',5-Trichlorobiphenyl" , 'pcb44' : "2,2',3,5'-Tetrachlorobiphenyl" ,
'pcb47' : "2,2',4,4'-Tetrachlorobiphenyl" , 'pcb49' : "2,2',4,5'-Tetrachlorobiphenyl" , 'pcb52' : "2,2',5,5'-Tetrachlorobiphenyl" ,
'pcb66' : "2,3',4,4'-Tetrachlorobiphenyl" , 'acenapth' : "Acenapthene" , 'acenapthylene' : "Acenapthylene" , 'anthracn' : "Anthracene" ,
'baa' : "Benz[a]anthracene" , 'bap' : "Benzo[a]pyrene" , 'bbf' : "Benzo[b]fluoranthene" , 'benzghip' : "Benzo[g,h,i]perylene" , 'bep' : "Benzo[e]pyrene" ,
'bkf' : "Benzo[k]fluoranthene" , 'c1n' : "C1-Napthalenes" , 'c1phen' : "C1-Phenanthrenes" , 'c2n' : "C2-Napthalenes" , 'c3n' : "C3-Napthalenes" ,
'chrysene' : "Chrysene" , 'dibenzah' : "Dibenz[a,h]anthracene" , 'flurant' : "Fluoranthene" , 'fluorene' : "Fluorene" , 'indypr' : "Indeno[123-c,d]pyrene" ,
'napth' : "Napthalene" , 'perylene' : "Perylene" , 'phenant' : "Phenanthrene" , 'pyrene' : "Pyrene" , 'thc' : "totalHC" ,
'ahch' : "alpha-hexachlorocyclohexane (AHCH)" , 'bhch' : "beta-hexachlorocyclohexane (BHCH)" , 'ghch' : "gamma-hexachlorocyclohexane (GHCH)" ,
'dieldrin' : "Dieldrin" , 'hcb' : "Hexachlorobenzene (HCB)" , 'dde' : "1,1-Dichloro-2,2-bis(p-chlorophenyl) ethylene (PPDDE)" ,
'ddt' : "Dichlorodiphenyltrichloroethane (PPDDT)" , 'tde' : "1,1-dichloro-2,2-bis(p-chlorophenyl)ethane (PPTDE)" ,
'bde100' : "2,2′,4,4′,6-penta-bromodiphenyl ether (BDE100)" , 'bde138' : "Hexabromodiphenyl ether (BDE138)" ,
'bde153' : "2,2′,4,4′,5,5′-hexa-bromodiphenyl ether (BDE153)" , 'bde154' : "2,2′,4,4′,5,6′-hexa-bromodiphenyl ether (BDE154)" ,
'bde17' : "2,2´,4-tri-bromodiphenylether (BDE17)" , 'bde183' : "2,2′,3,4,4′,5′,6-heptabromodiphenyl ether (BDE183)" ,
'bde28' : "2,4,4'-tribromodiphenyl ether (BDE28)" , 'bde47' : "2,2′,4,4′-Tetrabromodiphenyl ether (BDE47)" ,
'bde66' : "2,3',4,4'-Tetrabromodiphenyl ether (BDE66)" , 'bde85' : "2,2',3,4,4'-Pentabromodiphenyl ether (BDE85)" ,
'bde99' : "2,2',4,4',5-pentabromodiphenyl ether (BDE99)", 'bde209' : "2,2',3,3',4,4',5,5',6,6'-decabrominated diphenyl ether (BDE 209)" ,
'dbt' : "Dibutyltine (DBT)" , 'tbt' : "Tributyltin (TBT)" , 'as' : "Arsenic (As)" , 'cd' : "Cadmium (Cd)" , 'cr' : "Chromium (Cr)" , 'cu' : "Copper (Cu)" ,
'hg' : "Mercury (Hg)" , 'ni' : "Nickel (Ni)" , 'pb' : "Lead (Pb)" , 'zn' : "Zinc (Zn)" };
ddLookup.sheet = {'pcb101' : "PCB data" , 'pcb105' : "PCB data" , 'pcb110' : "PCB data" , 'pcb118' : "PCB data" , 'pcb128' : "PCB data" , 'pcb138' : "PCB data" ,
'pcb141' : "PCB data" , 'pcb149' : "PCB data" , 'pcb151' : "PCB data" , 'pcb153' : "PCB data" , 'pcb156' : "PCB data" , 'pcb158' : "PCB data" ,
'pcb170' : "PCB data" , 'pcb18' : "PCB data" , 'pcb180' : "PCB data" , 'pcb183' : "PCB data" , 'pcb187' : "PCB data" , 'pcb194' : "PCB data" ,
'pcb28' : "PCB data" , 'pcb31' : "PCB data" , 'pcb44' : "PCB data" , 'pcb47' : "PCB data" , 'pcb49' : "PCB data" , 'pcb52' : "PCB data" ,
'pcb66' : "PCB data" , 'acenapth' : "PAH data" , 'acenapthylene' : "PAH data" , 'anthracn' : "PAH data" , 'baa' : "PAH data" , 'bap' : "PAH data" ,
'bbf' : "PAH data" , 'benzghip' : "PAH data" , 'bep' : "PAH data" , 'bkf' : "PAH data" , 'c1n' : "PAH data" , 'c1phen' : "PAH data" , 'c2n' : "PAH data" ,
'c3n' : "PAH data" , 'chrysene' : "PAH data" , 'dibenzah' : "PAH data" , 'flurant' : "PAH data" , 'fluorene' : "PAH data" , 'indypr' : "PAH data" ,
'napth' : "PAH data" , 'perylene' : "PAH data" , 'phenant' : "PAH data" , 'pyrene' : "PAH data" , 'thc' : "PAH data" , 'ahch' : "Organochlorine data" ,
'bhch' : "Organochlorine data" , 'ghch' : "Organochlorine data" , 'dieldrin' : "Organochlorine data" , 'hcb' : "Organochlorine data" ,
'dde' : "Organochlorine data" , 'ddt' : "Organochlorine data" , 'tde' : "Organochlorine data" , 'bde100' : "BDE data" , 'bde138' : "BDE data" ,
'bde153' : "BDE data" , 'bde154' : "BDE data" , 'bde17' : "BDE data" , 'bde183' : "BDE data" , 'bde28' : "BDE data" , 'bde47' : "BDE data" ,
'bde66' : "BDE data" , 'bde85' : "BDE data" , 'bde99' : "BDE data" , 'bde209' : "BDE data", 'dbt' : "Organotins data" , 'tbt' : "Organotins data" ,
'as' : "Trace metal data" , 'cd' : "Trace metal data" , 'cr' : "Trace metal data" , 'cu' : "Trace metal data" , 'hg' : "Trace metal data" ,
'ni' : "Trace metal data" , 'pb' : "Trace metal data" , 'zn' : "Trace metal data"};
//BDE209 missing from original dataset
//ddLookup.chemical['BDE#209']="2,2',3,3',4,4',5,5',6,6'-decabrominateddiphenylether (BDE209)";
//[A-Z]{3}\_[0-9]{4}\_[0-9]{5}
//ddLookup.sheet['BDE#209']="BDE data";
ddCorrection = {'Trace metal data' : 1,'PAH data' : 1,'PCB data' : 0.001,'BDE data' : 0.001,'Organotins data' : 0.001,'Organochlorine data' : 0.001};
CEFASconcentration='concentration';
CEFASlatitude = 'lat';
CEFASlongitude = 'lon';
CEFASmla = 'ml_application';
CEFASchemical = 'parameter_measured';
CEFASsampledate = 'sample_date_collection';
CEFASdepth = 'sample_depth';
CEFASsamplename = 'sample_reference';
everything = {};
function importDredgeData(url,centreLat,centreLon,radius,startDate,finishDate,licences) {
// var urls = {};
if (url === undefined) {
const fileInputDD = document.getElementById('fileInputDD');
const urlInputDD = document.getElementById('urlInputDD');
files = fileInputDD.files; // Files is now a FileList object containing multiple files
//console.log(files);
// urls = urlInputDD.value.trim().split(',').map(url => url.trim()); // Split comma-separated URLs
url = urlInputDD.value;
}
if (files.length === 0 && url === undefined) {
alert('Please select files or enter URLs.');
return;
}
// Process files
if (files.length > 0) {
let filesProcessed = 0; // counter to track the number of files processed
let fL = files.length;
console.log(files.length);
for (let i = 0; i < files.length; i++) {
CEFASfilename = files[i].name;
const reader = new FileReader();
reader.onload = function (e) {
data = new Uint8Array(e.target.result);
filesProcessed++; // Increment the counter after processing each file
ret = loadDredgeData(data);
CEFASdata = ret['df'];
CEFASUniqueRows = ret['uniqueRows'];
};
reader.readAsArrayBuffer(files[i]);
}
}
// if (urls.length > 0) {
if (!(url === undefined)) {
// Array to store all fetch promises
const fetchPromises = [];
// urls.forEach(url => {
console.log(url);
// Check if the URL is a valid URL before fetching
if (!/^https?:\/\//i.test(url)) {
console.error('Invalid URL:', url);
return;
}
CEFASfilename = url;
function disableUserInteraction(fileName) {
const overlay = document.createElement('div');
overlay.id = 'loadingOverlay';
overlay.style.position = 'fixed';
overlay.style.top = 0;
overlay.style.left = 0;
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
overlay.style.zIndex = 1000;
overlay.style.display = 'flex';
overlay.style.justifyContent = 'center';
overlay.style.alignItems = 'center';
overlay.innerHTML = '<p style="color: white; font-size: 20px; text-align: center">Loading <br>' + fileName + '<br>...</p>';
document.body.appendChild(overlay);
}
// Function to enable user interaction
function enableUserInteraction() {
const overlay = document.getElementById('loadingOverlay');
if (overlay) {
overlay.remove();
}
}
// Disable user interaction
disableUserInteraction(url);
// Push each fetch promise into the array
fetchPromises.push(
fetch(url)
.then(response => response.arrayBuffer())
.then(data => {
// var data = new Uint8Array(e.target.result);
// ret = loadDredgeData(data);
ret = loadDredgeData(new Uint8Array(data));
CEFASdata = ret['df'];
CEFASUniqueRows = ret['uniqueRows'];
})
.catch(error => {
console.error('Error fetching the DD file:', error);
})
);
Promise.all(fetchPromises)
.then(() => {
//console.log(startDate,finishDate);
if (centreLat && centreLon && radius) {
if (!(startDate || finishDate)) {
//console.log('settting invalid date');
startDate = 'Invalid Date';
finishDate = 'Invalid Date';
}
closeCEFASSearch(centreLat,centreLon,radius,startDate,finishDate);
}
if(licences) {
closeCEFASSelection(dlicences);
}
enableUserInteraction();
//console.log('there again');
})
.catch(error => {
console.error('Error in Promise.all:', error);
// Ensure user interaction is enabled in case of error
enableUserInteraction();
});
}
fileInputDD.value = '';
urlInputDD.value = '';
}
function loadDredgeData(data) {
const workbook = XLSX.read(data, { type: 'array' });
//everything = workbook;
sheetData = workbook.Sheets['Dredge Contaminant Seabed Data '];
let uniqueRows = [];
let uniqueDatasets = [];
let uniqueMLAs = [];
let noMLAs = 0;
const df = XLSX.utils.sheet_to_json(sheetData, { cellText: true });
console.log(df);
df.forEach(row => {
let mlaLicense = row[CEFASmla];
let mlaDate = row[CEFASsampledate];
let mlaName = mlaLicense + mlaDate;
if (mlaName && !uniqueDatasets[mlaName]) {
uniqueDatasets[mlaName] = true;
if (mlaLicense && !uniqueMLAs[mlaLicense]) {
uniqueMLAs[mlaLicense] = true;
noMLAs += 1;
}
uniqueRows.push(row);
}
});
sedDredgeDataDisplay(noMLAs,uniqueRows.length);
return {df,uniqueRows};
}
function processDDExcelData(data, url, mlaInput) {
console.log('processexceldata', url);
sheetData = data;
//console.log(mlaInput);
if (mlaInput === undefined || mlaInput === null) {
mlaReturn = document.getElementById('mlApplications');
mlaInput = mlaReturn.value;
}
//console.log(mlaInput);
mlas = mlaInput.trim().split(',').map(mla => mla.trim()); // Split comma-separated URLs
if (mlas.length > 0) {
mlas.forEach(mlApplication => {
extractDataFromSheet(sheetData, mlApplication);
});
}
// dateAnalysed = extractDataFromSheet();
selectedSampleInfo = sampleInfo;
selectedSampleMeasurements = sampleMeasurements;
updateChart();
}
function extractDataFromSheet(sheetData, mlApplication) {
df = sheetData;
testAll = df.filter(row => row[CEFASmla] && row[CEFASmla].includes(mlApplication));
console.log(testAll);
console.log('length testAll', testAll.length);
let uniqueSamples = {};
let uniqueRows = [];
let testBits = {};
testAll.forEach(row => {
let sampleDate = parseDates(row[CEFASsampledate]);
let applicationNo = row[CEFASmla];
let dateSampled = sampleDate + ' d ' + applicationNo;
if (!(dateSampled in testBits)) {
testBits[dateSampled] = [];
}
testBits[dateSampled].push(row);
});
console.log(testBits);
for (dateSampled in testBits) {
testDD = testBits[dateSampled];
testDD.forEach(row => {
let sampleName = row[CEFASsamplename];
if (sampleName && !uniqueSamples[sampleName]) {
uniqueSamples[sampleName] = true;
uniqueRows.push(row);
}
});
sampleDate = parseDates(uniqueRows[0][CEFASsampledate]);
applicationNo = uniqueRows[0][CEFASmla];
console.log(dateSampled);
sampleInfo[dateSampled] = {};
sampleInfo[dateSampled]['Date Sampled'] = sampleDate;
sampleInfo[dateSampled]['Application number'] = applicationNo;
sampleInfo[dateSampled]['fileURL'] = CEFASfilename;
sampleInfo[dateSampled].position = {};
sampleInfo[dateSampled].label = dateSampled;
uniqueRows.forEach(row => {
sampleInfo[dateSampled].position[row[CEFASsamplename]] = {
'Position latitude': row[CEFASlatitude], 'Position longitude': row[CEFASlongitude],
'Sampling depth (m)': row[CEFASdepth]
};
});
let meas = {};
if (!(dateSampled in sampleMeasurements)) {
meas = {};
} else {
meas = sampleMeasurements[dateSampled];
};
everything = testDD;
testDD.forEach(row => {
let chemicalAbr = row[CEFASchemical].trim();
let sheetName = '';
if (chemicalAbr in ddLookup.sheet) {
let chemicalName = ddLookup.chemical[chemicalAbr];
let sample = row[CEFASsamplename];
if (chemicalName === 'totalHC') {
sheetName = 'PAH data';
} else {
sheetName = ddLookup.sheet[chemicalAbr];
}
let concentration = parseFloat(row[CEFASconcentration]) * ddCorrection[sheetName];
if (concentration === undefined) {
concentration = 0.0;
}
if (!(sheetName in meas)) {
meas[sheetName] = {};
meas[sheetName].chemicals = {};
meas[sheetName].total = {};
//Populate chemicals as data could be missing some chemicals
expectedChemicals = Object.keys(ddLookup.sheet).filter(key => ddLookup.sheet[key] === sheetName);
for (let i = 0; i < expectedChemicals.length; i++) {
meas[sheetName].chemicals[ddLookup.chemical[expectedChemicals[i]]] = {};
meas[sheetName].chemicals[ddLookup.chemical[expectedChemicals[i]]].samples = {};
}
}
if (chemicalName === 'totalHC') {
if (!('totalHC' in meas[sheetName])) {
meas[sheetName].totalHC = {};
}
meas[sheetName].totalHC[sample] = concentration;
} else {
if (!(chemicalName in meas[sheetName].chemicals)) {
meas[sheetName].chemicals[chemicalName] = {};
meas[sheetName].chemicals[chemicalName].samples = {};
}
if (!(sample in meas[sheetName].total)) {
meas[sheetName].total[sample] = 0;
}
meas[sheetName].chemicals[chemicalName].samples[sample] = concentration;
meas[sheetName].total[sample] += concentration;
}
}
});
sampleMeasurements[dateSampled] = meas;
for (const sheetName in meas) {
if (sheetName === 'PAH data') {
//console.log(meas[sheetName]);
pahPostProcess(meas[sheetName], dateSampled);
//console.log(sampleMeasurements[dateSampled][sheetName]);
}
if (sheetName === 'PCB data') {
pcbPostProcess(meas[sheetName], dateSampled);
}
}
}
}
function openCEFASSelection() {
const sedDataModal = document.getElementById('sedDataModal');
sedDataModal.style.display = 'block';
}
function closeCEFASSelection(licences) {
const sedDataModal = document.getElementById('sedDataModal');
sedDataModal.style.display = 'none';
processDDExcelData(CEFASdata,CEFASfile,licences);
}
function closeCEFASSearch(centreLat,centreLon,radius,startDate,finishDate) {
const isBetweenDates = (dateStart, dateEnd, date) =>
date > dateStart && date < dateEnd;
const sedDataModal = document.getElementById('sedDataModal');
sedDataModal.style.display = 'none';
if (!(centreLat && centreLon && radius)) {
centreLat = parseFloat(document.getElementById('centreLatitude').value);
centreLon = parseFloat(document.getElementById('centreLongitude').value);
radius = parseFloat(document.getElementById('radius').value);
startDate = new Date(document.getElementById('startDate').value);
finishDate = new Date(document.getElementById('finishDate').value);
}
mlas = [];
uniqueRows = CEFASUniqueRows;
//console.log(uniqueRows);
uniqueRows.forEach(row => {
samplingDate = new Date(parseDates(row[CEFASsampledate])[0]);
sampleLat = row[CEFASlatitude];
sampleLon = row[CEFASlongitude];
distance = 1000 * haversineDistance(sampleLat, sampleLon, centreLat, centreLon);
if (distance <= radius) {
//console.log(startDate);
if(startDate.toString() === 'Invalid Date' || finishDate.toString() === 'Invalid Date'){
//console.log('Pushing Invalid Date');
mlas.push(row[CEFASmla]);
} else {
if (isBetweenDates(startDate,finishDate,samplingDate)){
mlas.push(row[CEFASmla]);
}
}
}
});
console.log(mlas);
if (mlas.length > 0) {
mlas.forEach(mlApplication => {
//console.log(mlApplication);
extractDataFromSheet(CEFASdata, mlApplication);
});
selectedSampleInfo = sampleInfo;
selectedSampleMeasurements = sampleMeasurements;
updateChart();
}
}
function sedDredgeDataDisplay(noMLAs,noDatasets) {
const sedDDDisplayDiv = document.getElementById("sedDredgeData");
// blank it each time
sedDDDisplayDiv.innerHTML = "";
var countNode = document.createTextNode(`${noMLAs} marine applications available`);
sedDDDisplayDiv.appendChild(countNode);
// Add a line break for better readability
sedDDDisplayDiv.appendChild(document.createElement("br"));
var countNode = document.createTextNode(`${noDatasets} marine datasets available`);
sedDDDisplayDiv.appendChild(countNode);
// Add a line break for better readability
sedDDDisplayDiv.appendChild(document.createElement("br"));
sedDDDisplayDiv.style.display = 'block';
}