UNPKG

solar-scores

Version:

Compute scores for solar decathlon competition - Cali 2015

91 lines (69 loc) 3.04 kB
'use strict'; const utils = require('./../utils'); const points = require('./../points'); const baseScore = require('./../baseScore'); const moment = require('moment'); const periodsUtil = require('../periods'); class Temperature extends baseScore { constructor(options) { super(options); this.internals = []; this.internalParams = []; //this.scoringFunction = utils.getSingleScoringFunction(27, 29); } addInternal(values, param) { this.internals.push(utils.filterValues(values, param)); this.internalParams.push(param); } setExternal(values, param) { this.external = utils.filterValues(values, param); this.externalParam = param; } getScore(asOfDate) { var that = this; // Compute tmin // periods for last 6 days var maxSerie = utils.resampleWithMax(this.internals, this.internalParams, 'Temperature'); var byDay = utils.filterByEpochPerDay(maxSerie, 'Temperature'); //console.log(JSON.stringify(byDay)); for(let i=0; i<byDay.length; i++) { var date = new Date(byDay[i].day + ' GMT-0500'); var tMax = utils.getRefTemperature(this.external, this.externalParam, date); console.log('tMax day ' + byDay[i].day + ' is ' + tMax); let scoringFunction = utils.getSingleScoringFunction(tMax, tMax + 2); let data = byDay[i].data; let l = byDay[i].periods.length; byDay[i].points = new Array(l); byDay[i].pointsFraction = new Array(l); byDay[i].maxUntilNow = new Array(l); byDay[i].fraction = new Array(l); for(var j=0; j < l; j++) { var temperatures = data[j].map(function(val) { return val.value; }); utils.interpolate(temperatures); temperatures = temperatures.filter(function(val) { return val !== null; }); var elapsedFraction = utils.getElapsedFraction([byDay[i].periods[j]], asOfDate); if (temperatures.length === 0 || Number.isNaN(tMax)) { byDay[i].points[j] = 0; byDay[i].maxUntilNow[j] = byDay[i].periods[j].points * elapsedFraction; byDay[i].fraction[j] = 0; continue; } var avg = 0; for (var k = 0; k < temperatures.length; k++) { avg += scoringFunction(temperatures[k]); } avg /= temperatures.length; byDay[i].fraction[j] = avg; byDay[i].pointsFraction[j] = avg; byDay[i].points[j] = byDay[i].pointsFraction[j] * byDay[i].periods[j].points * elapsedFraction; byDay[i].maxUntilNow[j] = byDay[i].periods[j].points * elapsedFraction; } } return utils.finalizeScores(byDay); } } module.exports = Temperature;