forked from Flowm/wind-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
288 lines (247 loc) · 7.88 KB
/
index.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const express = require("express");
const compression = require("compression");
const moment = require("moment");
const fetch = require("node-fetch");
const fs = require("fs");
const cors = require("cors");
const { exec } = require("child_process");
const app = express();
const grib2json = process.env.GRIB2JSON || "./converter/bin/grib2json";
const port = process.env.PORT || 7000;
const resolution = process.env.RESOLUTION || "0.5";
const baseDir = `https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_${resolution === "1" ? "1p00" : "0p50"}.pl`;
const wind = process.env.WIND || true;
const temp = process.env.TEMP || false;
// GFS model update cycle
const GFS_CYCLE_H = 6;
// GFS model forecast timestep
const GFS_FORECAST_H = 3;
// Number of days to download historic GFS data (last 14 days available in theory)
const GFS_CYCLE_MAX_D = process.env.MAX_HISTORY_DAYS || 2;
// Number of forecast hours to download for each model cycle
const GFS_FORECAST_MAX_H = process.env.MAX_FORECAST_HOURS || 18;
// cors config
const whitelist = [
"https://tracktherace.com",
/^https:\/\/.*\.tracktherace\.com$/
];
const corsOptions = {
origin: whitelist
};
app.use(compression());
app.listen(port, () => {
console.log(`Running wind server for data resolution of ${resolution === "1" ? "1" : "0.5"} degree on port ${port}`);
});
app.get("/", cors(corsOptions), (req, res) => {
res.send("Wind server : go to /latest for last wind data.");
});
app.get("/alive", cors(corsOptions), (req, res) => {
res.send("Wind server is alive");
});
/**
* Find and return the nearest available GFS forecast for the current timestamp.
* Considers the 6 hour model update cycle and the 3 hour forecast steps.
*
* @param targetMoment {Object} UTC moment
*/
function findNearest(targetMoment, limitHours = GFS_FORECAST_MAX_H, searchBackwards = true) {
console.log(`FindNearest: Target ${targetMoment.format("YYYYMMDD-HH")}`);
const nearestGFSCycle = moment(targetMoment).hour(roundHours(moment(targetMoment).hour(), GFS_CYCLE_H));
let forecastOffset = 0;
if (nearestGFSCycle.diff(moment().utc(), "hours") > limitHours) {
console.log("FindNearest: Requested timestamp too far in the future");
return false;
}
do {
forecastOffset = targetMoment.diff(nearestGFSCycle, "hours");
const forecastOffsetRounded = roundHours(forecastOffset, GFS_FORECAST_H);
const stamp = getStampFromMoment(nearestGFSCycle, forecastOffsetRounded);
console.log(`FindNearest: Checking for ${stamp.filename}`);
const file = `${__dirname}/json-data/${stamp.filename}.json`;
if (checkPath(file, false)) {
return file;
}
if (searchBackwards) {
nearestGFSCycle.subtract(GFS_CYCLE_H, "hours");
} else {
nearestGFSCycle.add(GFS_CYCLE_H, "hours");
}
} while (forecastOffset < limitHours);
return false;
}
app.get("/latest", cors(corsOptions), (req, res, next) => {
const targetMoment = moment().utc();
const filename = findNearest(targetMoment);
if (!filename) {
next(new Error("No current data available"));
return;
}
res.setHeader("Content-Type", "application/json");
res.sendFile(filename, {}, (err) => {
if (err) {
console.log(`Error sending ${filename}: ${err}`);
}
});
});
app.get("/nearest", cors(corsOptions), (req, res, next) => {
const { time } = req.query;
const limit = req.query.limit || GFS_FORECAST_MAX_H;
if (!time || !moment(time).isValid()) {
next(new Error("Invalid time, expecting ISO 8601 date"));
return;
}
const targetMoment = moment.utc(time);
const filename = findNearest(targetMoment, limit);
if (!filename) {
next(new Error("No current data available"));
return;
}
res.setHeader("Content-Type", "application/json");
res.sendFile(filename, {}, (err) => {
if (err) {
console.log(`Error sending ${filename}: ${err}`);
}
});
});
function nextFile(targetMoment, offset, success) {
const previousTargetMoment = moment(targetMoment).subtract(GFS_CYCLE_H, "hours");
if (moment.utc().diff(previousTargetMoment, "days") > GFS_CYCLE_MAX_D) {
console.log("Harvest complete or there is a big gap in data");
return;
}
if (!success || offset > GFS_FORECAST_MAX_H) {
// Download previous targetMoment
getGribData(previousTargetMoment, 0);
} else {
// Download forecast of current targetMoment
getGribData(targetMoment, offset + GFS_FORECAST_H);
}
}
function getStampFromMoment(targetMoment, offset) {
const stamp = {};
stamp.date = moment(targetMoment).format("YYYYMMDD");
stamp.hour = roundHours(moment(targetMoment).hour(), GFS_CYCLE_H).toString().padStart(2, "0");
stamp.forecast = offset.toString().padStart(GFS_FORECAST_H, "0");
stamp.filename = `${moment(targetMoment).format("YYYY-MM-DD")}T${stamp.hour}.f${stamp.forecast}`;
return stamp;
}
/**
*
* Finds and downloads the latest 6 hourly GRIB2 data from NOAA
*
*/
function getGribData(targetMoment, offset) {
const stamp = getStampFromMoment(targetMoment, offset);
if (checkPath(`json-data/${stamp.filename}.json`, false)) {
console.log(`Already got ${stamp.filename}, stopping harvest`);
return;
}
const url = new URL(`${baseDir}`);
const filesuffix = resolution === "1" ? `z.pgrb2.1p00.f${stamp.forecast}` : `z.pgrb2full.0p50.f${stamp.forecast}`;
const file = `gfs.t${stamp.hour}${filesuffix}`;
const params = {
file,
...temp && {
lev_surface: "on",
var_TMP: "on",
},
...wind && {
lev_10_m_above_ground: "on",
var_UGRD: "on",
var_VGRD: "on",
},
leftlon: 0,
rightlon: 360,
toplat: 90,
bottomlat: -90,
dir: `/gfs.${stamp.date}/${stamp.hour}/atmos`,
};
Object.entries(params).forEach(([key, val]) => url.searchParams.append(key, val));
fetch(url)
.then((response) => {
console.log(`RESP ${response.status} ${stamp.filename}`);
if (response.status !== 200) {
nextFile(targetMoment, offset, false);
return;
}
if (!checkPath(`json-data/${stamp.filename}.json`, false)) {
console.log("Write output");
// Make sure output directory exists
checkPath("grib-data", true);
const f = fs.createWriteStream(`grib-data/${stamp.filename}`);
response.body.pipe(f);
f.on("finish", () => {
f.close();
convertGribToJson(stamp.filename, targetMoment, offset);
});
} else {
console.log(`Already have ${stamp.filename}, not looking further`);
}
})
.catch((err) => {
console.log("ERR", stamp.filename, err);
nextFile(targetMoment, offset, false);
});
}
function convertGribToJson(filename, targetMoment, offset) {
// Make sure output directory exists
checkPath("json-data", true);
exec(`${grib2json} --data --output json-data/${filename}.json --names --compact grib-data/${filename}`,
{ maxBuffer: 500 * 1024 },
(error) => {
if (error) {
console.log(`Exec error: ${error}`);
return;
}
console.log("Converted");
// Delete raw grib data
exec("rm grib-data/*");
nextFile(targetMoment, offset, true);
});
}
/**
*
* Round hours to expected interval
*
* @param hours
* @param interval
* @param floor
* @returns {number}
*/
function roundHours(hours, interval, floor = true) {
if (floor) {
return Math.floor(hours / interval) * interval;
}
return Math.round(hours / interval) * interval;
}
/**
* Sync check if path or file exists
*
* @param path {string}
* @param mkdir {boolean} create dir if doesn't exist
* @returns {boolean}
*/
function checkPath(path, mkdir) {
try {
fs.statSync(path);
return true;
} catch (e) {
if (mkdir) {
fs.mkdirSync(path);
}
return false;
}
}
/**
*
* @param targetMoment {Object} moment to check for new data
*/
function run(targetMoment) {
getGribData(targetMoment, 0);
}
// Check for new data every 15 mins
setInterval(() => {
run(moment.utc());
}, 900000);
// Init harvest
run(moment.utc());