An error occurred while loading the file. Please try again.
aggregate.mjs 6.42 KiB
"use strict";
import {
  extractObservationValuesWithinDatesInterval,
  extractObservationValuesWithinMonthInterval,
} from "./aggregateHelpers.mjs";
/**
 * Calculate the sum of observation values that fall within a time interval delimited by a start date and end date
 * @param {Array} obsValuesForDaysIntervalArr An array of observation values that fall within our time interval
 * @returns {Number} A floating-point number representing the sum of observation values
const calculateSumOfObservationValuesWithinDatesInterval = function (
  obsValuesForDaysIntervalArr
) {
  return obsValuesForDaysIntervalArr.reduce(
    (accumulator, currentValue) => accumulator + currentValue
/**
 * Calculate the average (arithmetic mean) of observation values that fall within a time interval delimited by a start date and end date
 * @param {Array} obsValuesForDaysIntervalArr An array of observation values that fall within our time interval
 * @returns {Number} A floating-point number representing the average (arithmetic mean) of observation values
const calculateAverageOfObservationValuesWithinDatesInterval = function (
  obsValuesForDaysIntervalArr
) {
  return (
    calculateSumOfObservationValuesWithinDatesInterval(
      obsValuesForDaysIntervalArr
    ) / obsValuesForDaysIntervalArr.length
/**
 * Calculate the sum of observation values that fall within a time interval delimited by the first day and last day of a calendar month
 * @param {Array} obsValuesForMonthIntervalArr An array of observation values that fall within one calendar month
 * @returns {Number} A floating-point number representing the sum of observation values within one calendar month
const calculateSumOfObservationValuesWithinMonthInterval = function (
  obsValuesForMonthIntervalArr
) {
  return obsValuesForMonthIntervalArr.reduce(
    (accumulator, currentValue) => accumulator + currentValue
/**
 * Calculate the average (arithmetic mean) of observation values that fall within a time interval delimited by the first day and last day of a calendar month
 * @param {Array} obsValuesForMonthIntervalArr An array of observation values that fall within one calendar month
 * @returns {Number} A floating-point number representing the average (arithmetic mean) of observation values within one calendar month
const calculateAverageOfObservationValuesWithinMonthInterval = function (
  obsValuesForMonthIntervalArr
) {
  return (
    calculateSumOfObservationValuesWithinMonthInterval(
      obsValuesForMonthIntervalArr
    ) / obsValuesForMonthIntervalArr.length
/**
 * 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)
 * @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 sum values
*/ const calculateSumOfObservationValuesWithinInterval = function ( obsNestedArr, samplingRate, uniqueCalendarDatesOrMonthsArr, aggregationInterval ) { // Calculate sum of values of observations - daily // Note the use of the two nested `map` methods if (aggregationInterval === "daily") { return uniqueCalendarDatesOrMonthsArr.map((uniqueCalendarDatesArr, i) => uniqueCalendarDatesArr.map((uniqueCalendarDate) => calculateSumOfObservationValuesWithinDatesInterval( extractObservationValuesWithinDatesInterval( obsNestedArr[i], samplingRate, uniqueCalendarDate, uniqueCalendarDate ) ) ) ); } // Calculate sum of values of observations - monthly // Note the use of the two nested `map` methods if (aggregationInterval === "monthly") { return uniqueCalendarDatesOrMonthsArr.map((uniqueCalendarMonthsArr, i) => uniqueCalendarMonthsArr.map((uniqueCalendarMonth) => calculateSumOfObservationValuesWithinMonthInterval( extractObservationValuesWithinMonthInterval( obsNestedArr[i], samplingRate, uniqueCalendarMonth ) ) ) ); } }; /** * Calculate the average (arithmetic mean) 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 average (arithmetic mean) values */ const calculateAverageOfObservationValuesWithinInterval = function ( obsNestedArr, samplingRate, uniqueCalendarDatesOrMonthsArr, aggregationInterval ) { // Calculate average of values of observations - daily // Note the use of the two nested `map` methods if (aggregationInterval === "daily") { return uniqueCalendarDatesOrMonthsArr.map((uniqueCalendarDatesArr, i) => uniqueCalendarDatesArr.map((uniqueCalendarDate) => calculateAverageOfObservationValuesWithinDatesInterval( extractObservationValuesWithinDatesInterval( obsNestedArr[i], samplingRate, uniqueCalendarDate, uniqueCalendarDate ) ) ) );
} // Calculate average of values of observations - monthly // Note the use of the two nested `map` methods if (aggregationInterval === "monthly") { return uniqueCalendarDatesOrMonthsArr.map((uniqueCalendarMonthsArr, i) => uniqueCalendarMonthsArr.map((uniqueCalendarMonth) => calculateAverageOfObservationValuesWithinMonthInterval( extractObservationValuesWithinMonthInterval( obsNestedArr[i], samplingRate, uniqueCalendarMonth ) ) ) ); } }; export { calculateSumOfObservationValuesWithinInterval, calculateAverageOfObservationValuesWithinInterval, };