Skip to content

Commit

Permalink
Correct for Logic Error. Collect Assumed Accumulation, not Average Rate.
Browse files Browse the repository at this point in the history
  • Loading branch information
leoherzog committed Sep 16, 2024
1 parent 7cb2b81 commit eda4d06
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ function refreshFromIBM_() {
"in": Number(ibmConditions.imperial.precipTotal).toFixedNumber(3),
"mm": Number(ibmConditions.imperial.precipTotal).inTomm().toFixedNumber(2)
};

let calculatedHourlyPrecipRate = getCalculatedHourlyPrecipRate_(conditions.precipRate.in);
if (calculatedHourlyPrecipRate != null) conditions.precipLastHour = {
"in": Number(calculatedHourlyPrecipRate).toFixedNumber(3),
"mm": Number(calculatedHourlyPrecipRate).inTomm().toFixedNumber(2)
};

console.log(JSON.stringify(conditions));

Expand Down Expand Up @@ -341,6 +347,12 @@ function refreshFromAcurite_() {
"in": rain.chart_unit === 'in' ? Number(rain.last_reading_value).toFixedNumber(3) : Number(rain.last_reading_value).mmToIn().toFixedNumber(3),
"mm": rain.chart_unit === 'mm' ? Number(rain.last_reading_value).toFixedNumber(2) : Number(rain.last_reading_value).inTomm().toFixedNumber(2)
};

let calculatedHourlyPrecipRate = getCalculatedHourlyPrecipRate_(conditions.precipRate.in);
if (calculatedHourlyPrecipRate != null) conditions.precipLastHour = {
"in": Number(calculatedHourlyPrecipRate).toFixedNumber(3),
"mm": Number(calculatedHourlyPrecipRate).inTomm().toFixedNumber(2)
};

console.log(JSON.stringify(conditions));

Expand Down Expand Up @@ -598,6 +610,12 @@ function refreshFromAmbientWeather_() {
"mm": Number(station.lastData['24hourrainin']).inTomm().toFixedNumber(2)
};

let calculatedHourlyPrecipRate = getCalculatedHourlyPrecipRate_(conditions.precipRate.in);
if (calculatedHourlyPrecipRate != null) conditions.precipLastHour = {
"in": Number(calculatedHourlyPrecipRate).toFixedNumber(3),
"mm": Number(calculatedHourlyPrecipRate).inTomm().toFixedNumber(2)
};

console.log(JSON.stringify(conditions));

CacheService.getScriptCache().put('conditions', JSON.stringify(conditions), 21600);
Expand Down Expand Up @@ -671,6 +689,12 @@ function refreshFromAPRSFI() {
"mm": Number(aprsConditions.rain_24h).toFixedNumber(2)
};

let calculatedHourlyPrecipRate = getCalculatedHourlyPrecipRate_(conditions.precipRate.in);
if (calculatedHourlyPrecipRate != null) conditions.precipLastHour = {
"in": Number(calculatedHourlyPrecipRate).toFixedNumber(3),
"mm": Number(calculatedHourlyPrecipRate).inTomm().toFixedNumber(2)
};

console.log(JSON.stringify(conditions));

CacheService.getScriptCache().put('conditions', JSON.stringify(conditions), 21600);
Expand Down Expand Up @@ -1047,35 +1071,33 @@ function updateCWOP_() {
}

function getCalculatedHourlyPrecipRate_(currentPrecipRate) {

let precipHistory = JSON.parse(CacheService.getScriptCache().get('calculatedHourlyPrecipRate') || '[]');

let now = new Date();

// add the new reading with a timestamp
precipHistory.push({
"rate": currentPrecipRate,
"timestamp": now.getTime()
});

// remove entries older than 1 hour
const oneHourAgo = now.getTime() - 3600000;
precipHistory = precipHistory.filter(entry => entry.timestamp >= oneHourAgo);

console.log(JSON.stringify(precipHistory));

CacheService.getScriptCache().put('calculatedHourlyPrecipRate', JSON.stringify(precipHistory), 21600); // 6 hours
const ONE_HOUR_MS = 3600000; // 1 hour in milliseconds

// calculate the average if we have data spanning close to an hour
if (precipHistory.length > 1 &&
(precipHistory[precipHistory.length - 1].timestamp - precipHistory[0].timestamp) >= 3600000 * 0.9) { // are there two or more readings spanning almost an hour?
const sum = precipHistory.reduce((acc, entry) => acc + entry.rate, 0);
const average = sum / precipHistory.length;
return average;
} else {
return null; // not enough data spanning an hour yet
let precipHistory = JSON.parse(CacheService.getScriptCache().get('calculatedHourlyPrecipRate') || '[]');
const currentTime = new Date().getTime();

// add the new reading and remove old entries
precipHistory.push({ rate: currentPrecipRate, timestamp: currentTime });
precipHistory = precipHistory.filter(entry => entry.timestamp >= currentTime - ONE_HOUR_MS);

// save the updated history back to cache
CacheService.getScriptCache().put('calculatedHourlyPrecipRate', JSON.stringify(precipHistory), 21600); // 6 hours cache

// calculate the accumulation if we have sufficient data
if (precipHistory.length > 1 && currentTime - precipHistory[0].timestamp >= ONE_HOUR_MS * 0.95) { // are there two or more readings spanning almost an hour?
const totalPrecip = precipHistory.reduce((acc, entry, index, array) => {
if (index === 0) return acc; // skip the first entry
const prevEntry = array[index - 1];
const timeFraction = (entry.timestamp - prevEntry.timestamp) / ONE_HOUR_MS; // how much of one hour does the last entry to this entry represent
return acc + prevEntry.rate * timeFraction; // add that much of the rain rate during that fraction of the hour to the total
}, 0);

return totalPrecip;
}

return null; // not enough data spanning an hour yet

}

// https://gist.github.com/rcknr/ad7d4623b0a2d90415323f96e634cdee
Expand Down

0 comments on commit eda4d06

Please sign in to comment.