diff --git a/public/js/src_modules/aggregate.mjs b/public/js/src_modules/aggregate.mjs index b38603f1a234b3b419f66723dceef062020584bc..324716daf646328d6ac8a25ef40ec38a9a799b5a 100644 --- a/public/js/src_modules/aggregate.mjs +++ b/public/js/src_modules/aggregate.mjs @@ -105,6 +105,102 @@ const calculateAverageOfObservationValuesWithinMonthInterval = function ( ); }; +/** + * Calculate the minimum of observation values within a time interval delimited by a start date and end date. The time interval may be daily or monthly + * @param {Array} obsNestedArr A 1*N array that contains N nested arrays of observations (timestamp + value) + * @param {String} samplingRate The sampling rate of observations as a string, e.g. "15min", "60min" + * @param {Array} uniqueCalendarDatesOrMonthsArr A 1*N array of unique calendar dates or calendar months strings + * @param {String} aggregationInterval The aggregation interval as a string e.g. "daily", "monthly" + * @returns {Array} A 1*N array that contains N nested arrays of minimum values + */ +const calculateMinimumObservationValuesWithinInterval = function ( + obsNestedArr, + samplingRate, + uniqueCalendarDatesOrMonthsArr, + aggregationInterval +) { + // Calculate minimum values of observations - daily + // Note the use of the two nested `map` methods + if (aggregationInterval === "daily") { + return uniqueCalendarDatesOrMonthsArr.map((uniqueCalendarDatesArr, i) => + uniqueCalendarDatesArr.map((uniqueCalendarDate) => + calculateMinimumObservationValuesWithinDatesInterval( + extractObservationValuesWithinDatesInterval( + obsNestedArr[i], + samplingRate, + uniqueCalendarDate, + uniqueCalendarDate + ) + ) + ) + ); + } + + // Calculate minimum values of observations - monthly + // Note the use of the two nested `map` methods + if (aggregationInterval === "monthly") { + return uniqueCalendarDatesOrMonthsArr.map((uniqueCalendarMonthsArr, i) => + uniqueCalendarMonthsArr.map((uniqueCalendarMonth) => + calculateMinimumObservationValuesWithinMonthInterval( + extractObservationValuesWithinMonthInterval( + obsNestedArr[i], + samplingRate, + uniqueCalendarMonth + ) + ) + ) + ); + } +}; + +/** + * Calculate the maximum of observation values within a time interval delimited by a start date and end date. The time interval may be daily or monthly + * @param {Array} obsNestedArr A 1*N array that contains N nested arrays of observations (timestamp + value) + * @param {String} samplingRate The sampling rate of observations as a string, e.g. "15min", "60min" + * @param {Array} uniqueCalendarDatesOrMonthsArr A 1*N array of unique calendar dates or calendar months strings + * @param {String} aggregationInterval The aggregation interval as a string e.g. "daily", "monthly" + * @returns {Array} A 1*N array that contains N nested arrays of maximum values + */ +const calculateMaximumObservationValuesWithinInterval = function ( + obsNestedArr, + samplingRate, + uniqueCalendarDatesOrMonthsArr, + aggregationInterval +) { + // Calculate maximum values of observations - daily + // Note the use of the two nested `map` methods + if (aggregationInterval === "daily") { + return uniqueCalendarDatesOrMonthsArr.map((uniqueCalendarDatesArr, i) => + uniqueCalendarDatesArr.map((uniqueCalendarDate) => + calculateMaximumObservationValuesWithinDatesInterval( + extractObservationValuesWithinDatesInterval( + obsNestedArr[i], + samplingRate, + uniqueCalendarDate, + uniqueCalendarDate + ) + ) + ) + ); + } + + // Calculate maximum values of observations - monthly + // Note the use of the two nested `map` methods + if (aggregationInterval === "monthly") { + return uniqueCalendarDatesOrMonthsArr.map((uniqueCalendarMonthsArr, i) => + uniqueCalendarMonthsArr.map((uniqueCalendarMonth) => + calculateMaximumObservationValuesWithinMonthInterval( + extractObservationValuesWithinMonthInterval( + obsNestedArr[i], + samplingRate, + uniqueCalendarMonth + ) + ) + ) + ); + } +}; + /** * Calculate the sum of observation values within a time interval delimited by a start date and end date. The time interval may be daily or monthly * @param {Array} obsNestedArr A 1*N array that contains N nested arrays of observations (timestamp + value) @@ -202,6 +298,8 @@ const calculateAverageOfObservationValuesWithinInterval = function ( }; export { + calculateMinimumObservationValuesWithinInterval, + calculateMaximumObservationValuesWithinInterval, calculateSumOfObservationValuesWithinInterval, calculateAverageOfObservationValuesWithinInterval, };