Skip to content

Instantly share code, notes, and snippets.

@lukeify
Created February 4, 2020 10:03
Show Gist options
  • Save lukeify/59c1f9f8eb0c09dfc40dc09bda071b1e to your computer and use it in GitHub Desktop.
Save lukeify/59c1f9f8eb0c09dfc40dc09bda071b1e to your computer and use it in GitHub Desktop.
Sunshine hours calculator
/**
* Sunshine hours calculation. Data taken from https://cliflo.niwa.co.nz.
*
*/
const fs = require('fs');
const readline = require('readline');
const linestream = readline.createInterface({
input: fs.createReadStream(process.argv[2]),
output: process,
console: false
});
const reduced = {};
linestream.on('line', line => {
if (!line.startsWith("Wellington")) return;
const values = line.split(',');
const year = values[2].substr(0, 4);
const month = values[2].substr(4, 2);
initializeMonthAndYear(year, month);
addToMonthAndYear(year, month, values[3]);
});
linestream.on('close', () => {
Object.entries(reduced).forEach(([k,v]) => {
console.log(`Year ${k}: ${v['01'].toFixed(1)} sunshine hours.`)
});
});
/**
*
* @param {*} year
* @param {*} month
*/
const initializeMonthAndYear = (year, month) => {
if (!reduced.hasOwnProperty(year)) {
reduced[year] = {};
}
if (!reduced[year].hasOwnProperty(month)) {
reduced[year][month] = 0;
}
}
/**
*
* @param {*} year
* @param {*} month
* @param {*} sunshineTotal
*/
const addToMonthAndYear = (year, month, sunshineTotal) => {
reduced[year][month] += parseFloat(sunshineTotal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment