An error occurred while loading the file. Please try again.
-
Pithon Kabiro authored233f7b2c
"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
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
*/
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,
};