-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspar-cli.js
94 lines (80 loc) · 3.59 KB
/
raspar-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
let raspar = require('.');
let fs = require('fs');
let moment = require('moment');
//configure the command line
let _argv = require('yargs').scriptName("raspar")
.usage('$0 <cmd> [args]')
.command('noaa-buoy [station_id]', 'get noaa buoy station data formatted as csv', function(yargs){
yargs.positional('station_id', {
type: 'string',
describe: 'The ID of the buoy station you want data for, multiple ID values separated by comma, or a ' +
'file name containing a station id per line.'
})
.demandOption('station_id')
.option('d', {
alias: 'date-filter',
requiresArg: true,
describe: 'a year, range of years, or \'realtime\'',
type: 'string'
})
.option('o', {
alias:'output-file',
requiresArg: true,
describe: 'a file to save output to. will be created if it does not exist.',
type: 'string'
})
.option('v',{
alias:'verbose',
"boolean":true,
describe: 'print out status messages'
})
.option('without-headers', {
"boolean": true,
describe: 'save data without column headers'
})
.example('$0 noaa-buoy PORO3', 'get data from station id PORO3')
.example('$0 noaa-buoy PORO3,UNLA2', 'get data from station id\'s PORO3 and UNLA2')
.example('$0 noaa-buoy stations.txt', 'get data from station id\'s listed in stations.txt file (one id per line)')
.example('$0 noaa-buoy stations.txt -o=my_output.csv', 'send data output to file my_output.csv')
.example('$0 noaa-buoy PORO3 -d=2015', 'limit to data from 2015')
.example('$0 noaa-buoy PORO3 -d=2018-2015', 'limit to data from 2018-2015')
.example('$0 noaa-buoy PORO3 -d=2015-realtime', 'limit to data from 2015-realtime')
}, commandNoaaBuoy)
.example('$0 noaa-buoy --help', 'buoy data - help using the `noaa-buoy` command')
.help()
.argv
;
function commandNoaaBuoy(argv) {
let stations = argv['station_id'];
let dateFilters = argv['date-filter'] ? argv['date-filter'] : null;
let defaultOutputFile = createNoaaBuoyOutputName(stations, dateFilters);
let outputFile = argv['output-file'] ? argv['output-file'] : defaultOutputFile;
let verbose = argv['verbose'];
//if the without-headers flag is set (true), then the `addHeaders` variable is false
let addHeaders = ! argv['without-headers'];
//test if the station_id passed is actually a file
if( fs.existsSync(stations) ){
//if so, get the stations as a csv from the file
stations = getStationsFromFile(stations);
}
let options = {
dateFilters:dateFilters,
verbose:verbose,
addHeaders:addHeaders
};
raspar.scrapeBuoyData(stations, options).then(function(buoyDataCsv){
fs.writeFileSync(outputFile, buoyDataCsv);
console.log('raspar success!');
console.log('Output file created at: ' + outputFile);
});
}
function getStationsFromFile(fileName){
let stationsFileData = fs.readFileSync(fileName, 'utf8');
return stationsFileData.replace(/\n/g, ',');
}
function createNoaaBuoyOutputName(stations, dateFilters){
let stationsPart = stations.replace(',', '-');
let dateFiltersPart = dateFilters ? dateFilters : 'realtime';
return 'noaa-buoy_' + stationsPart + '_' + dateFiltersPart + '_' + moment().format('YYYYMMDDHHmmssZZ') + '.csv';
}