You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
290 lines
8.7 KiB
290 lines
8.7 KiB
|
|
var IO = require('./io');
|
|
var meteo = require('./meteo');
|
|
var stations = require('./stations');
|
|
var chalk = require('chalk');
|
|
|
|
// {
|
|
// station-46011: {
|
|
// id: str
|
|
// name: str
|
|
// lat: str
|
|
// lon: str
|
|
|
|
// avg1982: {
|
|
// d: int[365] || null,
|
|
// w: int[52] || null,
|
|
// m: int[12] || null,
|
|
// y: int || null
|
|
// },
|
|
|
|
// avg1983: ...
|
|
// },
|
|
//
|
|
// {
|
|
// id: str
|
|
// ...
|
|
// }
|
|
// }
|
|
|
|
|
|
module.exports = {
|
|
/**
|
|
*
|
|
*/
|
|
parse: function(str) {
|
|
var json = {};
|
|
|
|
try {
|
|
json = JSON.parse(str);
|
|
} catch(e) {
|
|
IO.error(e);
|
|
}
|
|
|
|
return json;
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getYearlyAverages: function(arr, col) {
|
|
console.log('Yearly averages for column ' + col + '.');
|
|
|
|
if (arr.length === undefined) {
|
|
return 0;
|
|
}
|
|
|
|
var sum = 0;
|
|
var count = 0;
|
|
var val = 0;
|
|
|
|
arr.forEach(function(row) {
|
|
val = parseFloat(row[col]);
|
|
|
|
if (val === 999 || val === 0 || val === 99) {
|
|
return;
|
|
}
|
|
|
|
sum += val;
|
|
count++;
|
|
});
|
|
|
|
|
|
var avg = Math.round((sum / count) * 10) / 10 || 0;
|
|
return avg;
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getMonthlyAverages: function(arr, col) {
|
|
console.log('Monthly averages for column ' + col + '.');
|
|
|
|
if (arr.length === undefined) {
|
|
return Array.apply(null, Array(12)).map(Number.prototype.valueOf,0);
|
|
}
|
|
|
|
var sum, count;
|
|
var months = [];
|
|
var averages = [];
|
|
|
|
for (var i = 0; i < 12; i++) {
|
|
months[i] = [];
|
|
}
|
|
|
|
// Assemble all the values for each month.
|
|
arr.forEach(function(row, index) {
|
|
months[row[1] - 1].push(parseFloat(row[col]));
|
|
});
|
|
|
|
// Get the average for each collection of values in each day of the year.
|
|
months.forEach(function(values, index) {
|
|
sum = 0;
|
|
count = 0;
|
|
|
|
values.map(function(value) {
|
|
if (value === 999 || value === 0 || value === 99) {
|
|
return;
|
|
}
|
|
|
|
sum += parseFloat(value);
|
|
count++;
|
|
});
|
|
|
|
averages[index] = Math.round((sum / count) * 10) / 10 || 0;
|
|
});
|
|
|
|
return averages;
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getAllMonthlyAverages: function(station, col) {
|
|
var result = [];
|
|
|
|
for (var year = meteo.years.start; year <= meteo.years.end; year++) {
|
|
result.push(
|
|
IO.read(meteo.dirs.json + station + '-' + year + '.json')
|
|
.then(module.exports.parse)
|
|
.then(function(arr) {
|
|
return module.exports.getMonthlyAverages(arr, col);
|
|
})
|
|
);
|
|
}
|
|
|
|
return Promise.all(result);
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getAllYearlyAverages: function(station, col) {
|
|
var result = [];
|
|
|
|
for (var year = meteo.years.start; year <= meteo.years.end; year++) {
|
|
result.push(
|
|
IO.read(meteo.dirs.json + station + '-' + year + '.json')
|
|
.then(module.exports.parse)
|
|
.then(function(arr) {
|
|
return module.exports.getYearlyAverages(arr, col);
|
|
})
|
|
);
|
|
}
|
|
|
|
return Promise.all(result);
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getAllStations: function(arr) {
|
|
console.log('asdf')
|
|
|
|
return null;
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
buildFinalJson: function() {
|
|
var finalResult = {};
|
|
|
|
function next(index) {
|
|
if (index === stations.ids.length) {
|
|
// if (index === 1) {
|
|
// IO.write('_stations.json', JSON.stringify(finalResult, null, 4));
|
|
IO.write('_stations.json', JSON.stringify(finalResult));
|
|
return;
|
|
}
|
|
|
|
var station = stations.ids[index];
|
|
|
|
// Init objects.
|
|
finalResult['s' + station] = {};
|
|
for (var year = meteo.years.start; year <= meteo.years.end; year++) {
|
|
finalResult['s' + station]['WSPD' + year] = {}; // 6
|
|
finalResult['s' + station]['WVHT' + year] = {}; // 8
|
|
finalResult['s' + station]['WPER' + year] = {}; // 9
|
|
finalResult['s' + station]['ATMP' + year] = {}; // 13
|
|
finalResult['s' + station]['WTMP' + year] = {}; // 14
|
|
}
|
|
|
|
Promise.resolve()
|
|
// Stations
|
|
.then(IO.read.bind(null, stations.dirs.json + station + '.json'))
|
|
.then(module.exports.parse)
|
|
.then(function(json) {
|
|
finalResult['s' + station].id = json.id;
|
|
finalResult['s' + station].name = json.name;
|
|
finalResult['s' + station].lat = json.lat;
|
|
finalResult['s' + station].lon = json.lon;
|
|
})
|
|
|
|
// Monthly wind speed
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 6))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['WSPD' + (meteo.years.start + index)].m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly wind speed
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 6))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['WSPD' + (meteo.years.start + index)].y = averages;
|
|
});
|
|
})
|
|
|
|
// Monthly wave height
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 8))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['WVHT' + (meteo.years.start + index)].m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly wave height
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 8))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['WVHT' + (meteo.years.start + index)].y = averages;
|
|
});
|
|
})
|
|
|
|
// Monthly wave period
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 9))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['WPER' + (meteo.years.start + index)].m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly wave period
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 9))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['WPER' + (meteo.years.start + index)].y = averages;
|
|
});
|
|
})
|
|
|
|
// Monthly air temp
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 13))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['ATMP' + (meteo.years.start + index)].m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly air temp
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 13))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['ATMP' + (meteo.years.start + index)].y = averages;
|
|
});
|
|
})
|
|
|
|
// Monthly water temp
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 14))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['WTMP' + (meteo.years.start + index)].m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly water temp
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 14))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['WTMP' + (meteo.years.start + index)].y = averages;
|
|
});
|
|
})
|
|
|
|
.then(next.bind(null, index + 1))
|
|
.catch(IO.error);
|
|
};
|
|
|
|
next(0);
|
|
}
|
|
};
|
|
|