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.
302 lines
8.7 KiB
302 lines
8.7 KiB
var IO = require('./io');
|
|
var meteo = require('./meteo');
|
|
var stations = require('./stations');
|
|
var chalk = require('chalk');
|
|
|
|
// a1982 ws m y wh m y wp m y at m y wt m y
|
|
//
|
|
// {
|
|
// station-46011: {
|
|
// id: str
|
|
// name: str
|
|
// lat: str
|
|
// lon: str
|
|
//
|
|
// a1982: {
|
|
// ws: {
|
|
// m int[12]
|
|
// y int
|
|
// },
|
|
// wh: {
|
|
// m
|
|
// y
|
|
// },
|
|
// wp: {
|
|
// m
|
|
// y
|
|
// },
|
|
// at: {
|
|
// m
|
|
// y
|
|
// },
|
|
// wt: {
|
|
// m
|
|
// y
|
|
// }
|
|
// },
|
|
|
|
// a1983: ...
|
|
// }
|
|
// }
|
|
|
|
|
|
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) {
|
|
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]['a' + year] = {
|
|
ws: {}, // 6
|
|
wh: {}, // 8
|
|
wp: {}, // 9
|
|
at: {}, // 13
|
|
wt: {} // 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]['a' + (meteo.years.start + index)].ws.m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly wind speed
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 6))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].ws.y = averages;
|
|
});
|
|
})
|
|
|
|
// Monthly wave height
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 8))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].wh.m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly wave height
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 8))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].wh.y = averages;
|
|
});
|
|
})
|
|
|
|
// Monthly wave period
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 9))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].wp.m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly wave period
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 9))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].wp.y = averages;
|
|
});
|
|
})
|
|
|
|
// Monthly air temp
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 13))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].at.m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly air temp
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 13))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].at.y = averages;
|
|
});
|
|
})
|
|
|
|
// Monthly water temp
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, 14))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].wt.m = averages;
|
|
});
|
|
})
|
|
|
|
// Yearly water temp
|
|
.then(module.exports.getAllYearlyAverages.bind(null, station, 14))
|
|
.then(function(arr) {
|
|
arr.forEach(function(averages, index) {
|
|
finalResult['s' + station]['a' + (meteo.years.start + index)].wt.y = averages;
|
|
});
|
|
})
|
|
|
|
.then(next.bind(null, index + 1))
|
|
.catch(IO.error);
|
|
};
|
|
|
|
next(0);
|
|
}
|
|
};
|
|
|