-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
218 lines (169 loc) · 5.01 KB
/
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const HELP_MSG = `===================================================================
node cli.js download [... options]
Downloads the given set of grids
| -d [arg] : Date of model run
| * YYYYMMDD[hh] format (hh is optional) [default: most recent run]
| -f [arg] : forecast range
| * x,y -> x AND y hours ahead
| * x-y -> x TO y hours ahead
| * x -> x hours ahead (single grid) [default: 0]
eg: [...] -f 6-18 will download every possible forecast for the next 6 to 18 hours
| -g [arg] : grids to download
| * wind : U, V-component of wind [DEFAULT]
| * temp : avg temp (GFS only)
| * pres : Pressure & geopot. height (GFS only)
| * snow : Snow&Ice grids (GFS only)
| * wave : Swell waves (WAVE only)
| -j : dumps to JSON (using eccodes)
| -x : deletes the .grb file (useful if you only need the JSON file)
| -q : Quiet mode (no console output)
| -r [arg] : resolution (degrees per point)
| * 1 [DEFAULT]
| * .5
| * .25
| -s [arg] : source (case insensitive)
| * gfs (NOAA GFS) [DEFAULT]
| * wave (NOAA WAVE)
node cli.js get-inventory [-f -i -r -s]
Dumps the most recent inventory for the given source
| -i [arg] : matches species slug with the given user defined table (case insensitive)
| * ncep
| * ecmwf128
| * ecmwf160
node cli.js get-udt [ud_table]
Dumps every user defined tables (uses console.table), if a third argument is given only displays this
| * ncep
| * ecmwf128
| * ecmwf160
===================================================================`
const {
getUDTables,
getModels,
buildURL,
getInventory,
getClosestModelRun,
} = require('./lib/helpers.js')
const e2e = require('./lib/e2e.js')
const _D_ = require('./lib/d.js')
const filteredUDT = (f=null) => {
const UDT = getUDTables()
if(f != undefined)
return Object.entries(UDT)
.filter(x => x[0] === String(f).toUpperCase())
return Object.entries(UDT)
}
const getArg = (x, args) => {
const pos = args.indexOf(x)
if(pos !== -1)
return _args[pos + 1]
return null
}
const DEFAULT_GRIDS = {
'wind': [
// GFS
'UGRD:10 m',
'VGRD:10 m',
// WAVE
'UGRD:surface',
'VGRD:surface',
],
// GFS ONLY
'temp': [
'TMP:surface',
],
// GFS ONLY
'snow': [
'WEASD:surface',
'SNOD:surface',
'CPOFP:surface'
],
// GFS ONLY
'pres': [
'PRES:surface',
'MSLET:mean sea',
'HGT:surface'
],
'wave': [
// GFS
'LAND:surface',
// WAVE
'SWELL:1',
'SWELL:2',
'SWDIR:1',
'SWDIR:2',
]
}
const DEFAULT_RESOLUTIONS = ['1p00', '0p50', '0p25']
const _args = process.argv
if(_args.length < 3)
console.log(HELP_MSG)
else{
const models = getModels()
const _s = getArg('-s', _args) || 'GFS'
const model = models.hasOwnProperty(_s.toUpperCase()) ? models[_s.toUpperCase()] : models.GFS
const _d = getArg('-d', _args)
const date = _d !== null ? new _D_(_d) : getClosestModelRun(model, new _D_()._(model.meta.UTC_offset))
const fc_range = getArg('-f', _args) || '0'
const _r = getArg('-r', _args)
const resolution = DEFAULT_RESOLUTIONS.includes(_r) ? _r : DEFAULT_RESOLUTIONS[0]
switch(_args[2]){
case 'download':
const _g = getArg('-g', _args) || 'wind'
const grid = DEFAULT_GRIDS[getArg('-g', _args)] || DEFAULT_GRIDS.wind
const _j = _args.includes('-j')
const _x = _args.includes('-x')
const _q = _args.includes('-q')
e2e(model, grid, date, fc_range, {
prefixFilename: _g,
resolution,
dumpToJSON: _j,
deleteTmpGRBFile: _x,
logFunction: _q ? () => null : console.log
})
break
case 'get-inventory':
const _i = getArg('-i', _args)
const UDT = _i !== null ? filteredUDT(_i) : []
console.log('Inventory for ' + date.ymdh())
getInventory({
host: model.host,
path: buildURL(model.path, {
DATE: date.ymd(),
FC: '000',
HOUR: date.h(),
RESOLUTION: resolution
})
})
.then(r => {
r.forEach((x, i) => {
const splitedLine = x.split(':')
const lvl = splitedLine[4]
let pName = splitedLine[3]
if(UDT.length == 1){
const replacement = UDT[0][1].find(x => x.slug === pName)
const fullName = replacement !== undefined && replacement.hasOwnProperty('name') ? replacement.name : '?'
console.log(i + ') [' + pName + '] '+ fullName +' @' + lvl)
}
else{
console.log(i + ') ' + pName + ' @' + lvl)
}
})
})
.catch(err => {
console.log("Inventory not found. Try changing some parameters (eg: manually setting the date)")
console.log("------------ HTTP ERROR ------------")
console.log(err)
})
break
case 'get-udt':
filteredUDT(_args[3]).forEach((x, i, ar) => {
console.log('------------ ' + x[0] + ' ------------')
console.table(x[1])
if(i !== ar.length - 1)
console.log('\n')
})
break
default:
console.log(HELP_MSG)
}
}