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.
 
 
 
 

132 lines
2.9 KiB

'use strict'
var fs = require('fs');
var downloader = require('./downloader');
var xml2js = require ('xml2js');
var dir = 'data/stations/';
module.exports = {
/**
* Add station IDs here, A-Z 0-9
*/
stations: [
'ANVC1',
'BDXC1',
'CECC1',
'CPXC1',
'HBYC1',
'ICAC1',
'NTBC1',
'PRYC1',
'PTGC1',
'46011',
'46012',
'46013',
'46014',
'46022',
'46025',
'46027',
'46028',
'46042',
'46053',
'46054',
'46092',
'46213',
'46214',
'46215',
'46216',
'46217',
'46221',
'46223',
'46225',
'46232',
'46234',
'46237',
'46239',
'46240',
'46242',
'46244',
'46253',
'46254',
'46256',
'46257'
],
/**
* Downloads each station's data XML.
*/
downloadAllMetadata: function() {
var len = this.stations.length;
var url;
for (var i = 0; i < len; i++) {
url = 'http://www.ndbc.noaa.gov/get_observation_as_xml.php?station=' + this.stations[i];
downloader.download(url, dir + this.stations[i] + '.txt');
}
},
/**
*
*/
parseAllMetadata: function() {
function done() {
if (data.length === len) {
fs.writeFile(outfile, data.join('\n'), function(err) {
if (err) {
throw new Error(err)
}
console.log('Station data written to ' + outfile);
});
}
};
function next() {
// Wait for other concurrent files to finish.
if (count !== data.length) {
return;
}
var concurrent = 3;
var tmp;
for (var i = 0; i < concurrent; i++) {
tmp = count + i;
if (tmp === len) {
break;
}
// console.log(tmp + "(" + count + ")" + " reading " + dir + module.exports.stations[tmp] + '.txt');
fs.readFile(dir + module.exports.stations[tmp] + '.txt', 'utf8', thenParse);
}
count += concurrent;
};
function thenParse(err, xml) {
if (err) {
throw new Error(err)
}
xml2js.parseString(xml, thenReport);
};
function thenReport(err, json) {
if (err) {
throw new Error(err)
}
data.push(JSON.stringify(json.observation.$));
next();
done();
};
var outfile = 'data-stations.json';
var len = module.exports.stations.length;
var count = 0;
var data = [];
next();
},
};