dropDownListProcessing.mjs 16.1 KB
Newer Older
1
2
"use strict";

3
4
5
6
import {
  extractUniqueCalendarMonthsFromCalendarDates,
  formatAggregationResultForChart,
} from "./aggregateHelpers.mjs";
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

import {
  calculateMinimumObservationValuesWithinInterval,
  calculateMaximumObservationValuesWithinInterval,
  calculateSumOfObservationValuesWithinInterval,
  calculateAverageOfObservationValuesWithinInterval,
} from "./aggregate.mjs";

import { extractPropertiesFromFormattedDatastreamMetadata } from "./fetchedDataProcessing.mjs";

/**
 * Calculate the daily sum of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (daily sum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailySumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
32
  // Calculate SUM / DAILY of values of observations
33
34
35
36
37
38
39
40
  const observationsSumDailyNestedArr =
    calculateSumOfObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      "daily"
    );

41
  // Format the observations
42
43
  const formattedObservationsSumDailyNestedArr =
    observationsSumDailyNestedArr.map((obsSumDailyArr, i) =>
44
      formatAggregationResultForChart(
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
        uniqueCalendarDatesNestedArr[i],
        obsSumDailyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "daily",
      "sum"
    );

  return [
    formattedObservationsSumDailyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the monthly sum of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (monthly sum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlySumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

86
  // Calculate SUM / MONTHLY of values of observations
87
88
89
90
91
92
93
94
  const observationsSumMonthlyNestedArr =
    calculateSumOfObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarMonthsNestedArr,
      "monthly"
    );

95
  // Format the observations
96
97
  const formattedObservationsSumMonthlyNestedArr =
    observationsSumMonthlyNestedArr.map((obsSumMonthlyArr, i) =>
98
      formatAggregationResultForChart(
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
        uniqueCalendarMonthsNestedArr[i],
        obsSumMonthlyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "monthly",
      "sum"
    );

  return [
    formattedObservationsSumMonthlyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the daily maximum of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (daily maximum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailyMaximumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
134
  // Calculate MAXIMUM / DAILY of values of observations
135
136
137
138
139
140
141
142
  const observationsMaximumDailyNestedArr =
    calculateMaximumObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      "daily"
    );

143
  // Format the observations
144
145
  const formattedObservationsMaximumDailyNestedArr =
    observationsMaximumDailyNestedArr.map((obsMinDailyArr, i) =>
146
      formatAggregationResultForChart(
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
        uniqueCalendarDatesNestedArr[i],
        obsMinDailyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "daily",
      "maximum"
    );

  return [
    formattedObservationsMaximumDailyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the monthly maximum of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (monthly maximum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlyMaximumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

188
  // Calculate MAXIMUM / MONTHLY of values of observations
189
190
191
192
193
194
195
196
  const observationsMaximumMonthlyNestedArr =
    calculateMaximumObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarMonthsNestedArr,
      "monthly"
    );

197
  // Format the observations
198
199
  const formattedObservationsMaximumMonthlyNestedArr =
    observationsMaximumMonthlyNestedArr.map((obsMaxMonthlyArr, i) =>
200
      formatAggregationResultForChart(
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
        uniqueCalendarMonthsNestedArr[i],
        obsMaxMonthlyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "monthly",
      "maximum"
    );

  return [
    formattedObservationsMaximumMonthlyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the daily minimum of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (daily minimum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailyMinimumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
236
  // Calculate MINIMUM / DAILY of values of observations
237
238
239
240
241
242
243
244
  const observationsMinimumDailyNestedArr =
    calculateMinimumObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      "daily"
    );

245
  // Format the observations
246
247
  const formattedObservationsMinimumDailyNestedArr =
    observationsMinimumDailyNestedArr.map((obsMinDailyArr, i) =>
248
      formatAggregationResultForChart(
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
        uniqueCalendarDatesNestedArr[i],
        obsMinDailyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "daily",
      "minimum"
    );

  return [
    formattedObservationsMinimumDailyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the monthly minimum of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (monthly minimum) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlyMinimumObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

290
  // Calculate MINIMUM / MONTHLY of values of observations
291
292
293
294
295
296
297
298
  const observationsMinimumMonthlyNestedArr =
    calculateMinimumObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarMonthsNestedArr,
      "monthly"
    );

299
  // Format the observations
300
301
  const formattedObservationsMinimumMonthlyNestedArr =
    observationsMinimumMonthlyNestedArr.map((obsMinMonthlyArr, i) =>
302
      formatAggregationResultForChart(
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
        uniqueCalendarMonthsNestedArr[i],
        obsMinMonthlyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "monthly",
      "minimum"
    );

  return [
    formattedObservationsMinimumMonthlyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the daily average of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (daily average) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatDailyAverageObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
338
  // Calculate AVERAGE / DAILY of values of observations
339
340
341
342
343
344
345
346
  const observationsAverageDailyNestedArr =
    calculateAverageOfObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      "daily"
    );

347
  // Format the observations
348
349
  const formattedObservationsAverageDailyNestedArr =
    observationsAverageDailyNestedArr.map((obsAverageDailyArr, i) =>
350
      formatAggregationResultForChart(
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
        uniqueCalendarDatesNestedArr[i],
        obsAverageDailyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "daily",
      "average"
    );

  return [
    formattedObservationsAverageDailyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

/**
 * Calculate the monthly average of observations and format these aggregated observations
 *
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {Array} An array whose first element is the formatted aggregated (monthly average) observations. The second element is an object made up of extracted & formatted datastream properties
 */
const calculateAndFormatMonthlyAverageObservations = function (
  uniqueCalendarDatesNestedArr,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  formattedMetadataNestedArr
) {
  // Unique calendar months
  const uniqueCalendarMonthsNestedArr = uniqueCalendarDatesNestedArr.map(
    (uniqueCalendarDatesArr) =>
      extractUniqueCalendarMonthsFromCalendarDates(uniqueCalendarDatesArr)
  );

392
  // Calculate AVERAGE / MONTHLY of values of observations
393
394
395
396
397
398
399
400
  const observationsAverageMonthlyNestedArr =
    calculateAverageOfObservationValuesWithinInterval(
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarMonthsNestedArr,
      "monthly"
    );

401
  // Format the observations
402
403
  const formattedObservationsAverageMonthlyNestedArr =
    observationsAverageMonthlyNestedArr.map((obsAverageMonthlyArr, i) =>
404
      formatAggregationResultForChart(
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
        uniqueCalendarMonthsNestedArr[i],
        obsAverageMonthlyArr
      )
    );

  // Extract the formatted metadata properties
  const extractedFormattedDatastreamProperties =
    extractPropertiesFromFormattedDatastreamMetadata(
      formattedMetadataNestedArr,
      true,
      "monthly",
      "average"
    );

  return [
    formattedObservationsAverageMonthlyNestedArr,
    extractedFormattedDatastreamProperties,
  ];
};

export {
426
427
428
429
430
431
432
433
  calculateAndFormatDailySumObservations,
  calculateAndFormatMonthlySumObservations,
  calculateAndFormatDailyMaximumObservations,
  calculateAndFormatMonthlyMaximumObservations,
  calculateAndFormatDailyMinimumObservations,
  calculateAndFormatMonthlyMinimumObservations,
  calculateAndFormatDailyAverageObservations,
  calculateAndFormatMonthlyAverageObservations,
434
};