-
Notifications
You must be signed in to change notification settings - Fork 2
/
SSCWeb.js
117 lines (103 loc) · 5.79 KB
/
SSCWeb.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Call the SSCWeb API and respond with HAPI CSV.
// Old version. Do not uses. Does not handle last record properly if last
// record does not have same timestamp of stop time in request.
process.exit(0);
var fs = require('fs');
var request = require('request');
var moment = require('moment');
var argv = require('yargs')
.default
({
'id': '',
'parameters': '',
'start': '',
'stop': ''
})
.argv;
var allparameters = false;
if (argv.parameters === true || argv.parameters === '') {
allparameters = true;
} else {
var PARAMETERS = argv.parameters.split(",");
var timeonly = false;
if (PARAMETERS.length == 1 && PARAMETERS[0] === 'Time') {
var timeonly = true;
}
var json = JSON.parse(fs.readFileSync('metadata/SSCWeb/SSCWeb-catalog.json', 'utf8'));
var parameters = json[0].info.parameters;
if (PARAMETERS[0] !== "Time") {
PARAMETERS.push("Time");
}
var pn = [];
for (var i=0;i < parameters.length;i++) {
if (PARAMETERS.includes(parameters[i].name)) {
pn.push(i);
}
}
}
var SPCR = 'ace';
if (argv.id !== '') {
SPCR = argv.id;
}
//console.log(SPCR);
var START_TIME = '2000+001+00:00:00';
if (argv.start !== '') {
START_TIME = moment(argv.start).utc().format('YYYY+DDDD+HH:mm:ss');
}
var STOP_TIME = '2000+001+23:59:59';
if (argv.stop !== '') {
var STOP_TIME = moment(argv.stop).utc().format('YYYY+DDDD+HH:mm:ss');
}
var url = 'https://sscweb.gsfc.nasa.gov/cgi-bin/Locator.cgi?&SPCR='+SPCR+'&'+SPCR+'_res=&START_TIME='+START_TIME+'&STOP_T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and+wait+for+output&.cgifields=TRC_GEON&.cgifields=REG_OPT&.cgifields=TOD&.cgifields=GEO&.cgifields=TRC_GMS&.cgifields=OPT&.cgifields=GM&.cgifields=J2000&.cgifields=GSE&.cgifields=TRC_GMN&.cgifields=GSM&.cgifields=SM&.cgifields=TRC_GEOS';
request({url: url, strictSSL: false}, function (error, response, body) {
if (error) {
console.log(error);
process.exit(1);
}
if (response && response.statusCode != 200) {
process.exit(1);
}
console.log(extractData(body));
});
function extractData(data) {
// Input `data` string is output of web call (HTML page with data embedded)
// Replace space or tab with comma
// Create array (using split)
// Iterate through lines
// Keep lines that start with four digits
// Make DOY zero padded and convert YYYY,DOY,HH:MM:SS to YYYY-DOYTHH:MM:SS
// Convert array back to string and remove extra newlines that appear when line was not kept
// Note: The N/A, NA, -nan, nan replacements could be moved into map callback function.
return data.toString()
.replace(/N\/A/g,"1e31")
.replace(/NA/g,"1e31")
.replace(/-nan/g,"1e31")
.replace(/nan/g,"1e31")
.replace(/[ \t]+/g,',')
.replace(/\0/g,'') // ASCII NULL is inserted before data lines starting with the second
.split("\n")
.map(function(line){
if (line.search(/^[0-9]{4},[0-9]+,/) != -1) {
line = line
.replace(/^([0-9]{4}),([0-9]),/,"$1-00$2T")
.replace(/^([0-9]{4}),([0-9][0-9]),/,"$1-0$2T")
.replace(/^([0-9]{4}),([0-9][0-9][0-9]),/,"$1-$2T")
.replace(/,\s*$/,"");
linearr = line.split(",");
var linenew = linearr[0] + "Z"; // Always return time
if (allparameters) {
linearr[0] = linearr[0] + "Z";
return linearr.join(',');
}
if (timeonly) {return linenew;}
for (var i = 1;i < pn.length;i++) {
linenew = linenew + "," + linearr[pn[i]];
}
return linenew;
}
return "";
})
.filter(function(s){ return s !== '' })
.slice(0,-1)
.join("\n")
};