-
Notifications
You must be signed in to change notification settings - Fork 1
/
opengrib2.js
149 lines (122 loc) · 4.38 KB
/
opengrib2.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
"use strict";
var isInteractive = true; // true: using plotly.js | false: without plotly.js - useful e.g. for working with the ensembles
var showConsoleLogs = false;
var basicPlot = isInteractive ? null : require("./basic_plot.js");
var interactivePlot = isInteractive ? require("./interactive_plot.js") : null;
var http = require("http");
var grib2class = require("grib2class");
var grib2links = require("./grib2links");
var JpxImage = require("./jpeg2000/jpx.min.js"); // https://github.com/OHIF/image-JPEG2000/blob/master/dist/jpx.min.js"
var jpeg2000decoder = function (imageBytes) {
var jpeg2000 = new JpxImage();
jpeg2000.parse(imageBytes);
return jpeg2000.tiles[0].items;
};
function getNumMembers (link) {
return link.indexOf("ensemble") !== -1 ?
21 : // i.e. ensembles
1; //i.e. deterministic
}
function getLiveMocks () {
var deltaTime = 6; // some delay needed for each model run to be available on the web
var modelTime = new Date();
modelTime.setHours(modelTime.getHours() - deltaTime);
return grib2links({
year: modelTime.getUTCFullYear(),
month: modelTime.getUTCMonth() + 1, // getUTCMonth returns 0 - 11
day: modelTime.getUTCDate(),
hour: Math.floor(modelTime.getUTCHours() / 12) * 12, // most of CMC models are produced at 00Z and 12Z
forecastHour: 24 // for now let's pick the forecast at hour 24
});
}
function getLocalMocks () {
return [
"./grib2/CMC_glb_TMP_TGL_2_latlon.24x.24_2019081112_P024.grib2",
"./grib2/CMC_gdwps_global_HTSGW_SFC_0_latlon0.25x0.25_2019081112_P024.grib2",
"./grib2/CMC_hrdps_west_TMP_TGL_2_ps2.5km_2019081112_P024-00.grib2",
"./grib2/CMC_hrdps_east_TMP_TGL_2_ps2.5km_2019081112_P024-00.grib2",
"./grib2/CMC_hrdps_continental_TMP_TGL_80_ps2.5km_2019081112_P024-00.grib2",
"./grib2/CMC_reg_TMP_TGL_2_ps10km_2019081112_P024.grib2",
"./grib2/CMC-reps-srpe-raw_TMP_TGL_2m_ps15km_2019081112_P024_allmbrs.grib2",
"./grib2/CMC_geps-raw_TMP_TGL_2m_latlon0p5x0p5_2019081112_P024_allmbrs.grib2"
];
}
function echo (txt) {
if (showConsoleLogs) console.log(txt);
}
echo("process.env.NODE_ENV='" + process.env.NODE_ENV + "'");
var mocks;
switch (process.env.NODE_ENV) {
case "proxy-data":
mocks = getLiveMocks();
echo("Using grib2 data fetched from Datamart using proxy server!");
break;
case "local-data":
mocks = getLocalMocks();
echo("Using local (already downloaded) grib2 data");
break;
default:
console.error("BAD BUNDLE");
break;
}
var dropDown = document.getElementById("file-selector");
var createDropDown = function () {
for (var i in mocks) {
var opt = document.createElement("option");
opt.value = mocks[i];
opt.text = mocks[i];
dropDown.append(opt);
}
dropDown.addEventListener("change", function (e) {
go(e.target.value);
});
};
createDropDown();
var loading = document.getElementById("loading");
var enableLoading = function () {
loading.style.display = "block";
};
var disableLoading = function () {
loading.style.display = "none";
};
var beforeAfter = {
before: enableLoading,
after: disableLoading
};
function go (link) {
echo("Loading:'" + link + "'");
enableLoading();
link = link.replace("https://", "http://");
if (process.env.NODE_ENV === "proxy-data") {
link = link.replace("://dd.meteo.gc.ca/", "://localhost:3000/");
link = link.replace("://dd.weather.gc.ca/", "://localhost:3000/");
}
var myGrid = new grib2class({
numMembers: getNumMembers(link),
log: false,
jpeg2000decoder: jpeg2000decoder
});
http.get(link, function (res, err) {
if (err) {
disableLoading();
}
var allChunks = [];
res.on("data", function (chunk) {
allChunks.push(chunk);
});
res.on("end", function () {
myGrid.parse(Buffer.concat(allChunks));
echo(myGrid);
if (isInteractive) {
interactivePlot(myGrid, document.getElementById("interactivePlot"), beforeAfter);
} else {
basicPlot(myGrid, document.getElementById("basicPlot"), beforeAfter);
}
});
}).on("error", function (err) {
disableLoading();
window.alert(err);
});
}
window.go = go;
go(mocks[0]);