Skip to content

Commit c927a75

Browse files
committed
improve colorscales - disable console.logs by default - etc
1 parent ce82601 commit c927a75

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

basic_plot.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,38 @@ module.exports = function basicPlot (grid, canvas, call) {
4141

4242
var p = 0;
4343
for (i = 0; i < nPoints; i++) {
44-
var r = ratios[i];
44+
var z = ratios[i];
4545
var q = 4 * (p++);
46-
if (r === undefined) {
46+
if (z === undefined) {
4747
img.data[q + 0] = 127;
4848
img.data[q + 1] = 127;
4949
img.data[q + 2] = 127;
5050
img.data[q + 3] = 127;
5151
} else {
52-
img.data[q + 0] = 255 * r;
53-
img.data[q + 1] = 0;
54-
img.data[q + 2] = 255 * (1 - r);
55-
img.data[q + 3] = 255;
52+
var c = colorscale(z);
53+
img.data[q + 0] = 255 * c.r;
54+
img.data[q + 1] = 255 * c.g;
55+
img.data[q + 2] = 255 * c.b;
56+
img.data[q + 3] = 255 * c.a;
5657
}
5758
}
5859

5960
ctx.putImageData(img, 0, 0);
6061

6162
call.after();
6263
};
64+
65+
function fn (v) {
66+
return Math.sin(v * Math.PI);
67+
}
68+
69+
function colorscale (ratio) { // 0 to 1
70+
var v = 2 * ratio - 1; // -1 to +1
71+
72+
return {
73+
r: v > 0 ? fn(v) : 0,
74+
g: 0,
75+
b: v > 0 ? 0 : fn(-v),
76+
a: Math.abs(v)
77+
};
78+
}

interactive_plot.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ module.exports = function interactivePlot (grid, div, call) {
3131
type: "heatmap",
3232
z: z,
3333
hovertemplate: "%{z:.1f}K<extra>(%{x}, %{y})</extra>",
34-
colorscale: "Portland",
34+
colorscale: [
35+
[0.0, "rgb(0, 0, 127)"],
36+
[0.2, "rgb(0, 0, 255)"],
37+
[0.5, "rgb(255, 255, 255)"],
38+
[0.8, "rgb(255, 0, 0)"],
39+
[1.0, "rgb(127, 0, 0)"]
40+
],
3541
colorbar: {
3642
len: 0.5
3743
}

opengrib2.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

3-
var isInteractive = true; // could be set to false for non-interactive graphs - useful e.g. for working with the ensembles
3+
var isInteractive = true; // true: using plotly.js | false: without plotly.js - useful e.g. for working with the ensembles
4+
var showConsoleLogs = false;
45

56
var basicPlot = isInteractive ? null : require("./basic_plot.js");
67
var interactivePlot = isInteractive ? require("./interactive_plot.js") : null;
@@ -49,16 +50,20 @@ function getLocalMocks () {
4950
];
5051
}
5152

52-
console.log("process.env.NODE_ENV='" + process.env.NODE_ENV + "'");
53+
function echo (txt) {
54+
if (showConsoleLogs) console.log(txt);
55+
}
56+
57+
echo("process.env.NODE_ENV='" + process.env.NODE_ENV + "'");
5358
var mocks;
5459
switch (process.env.NODE_ENV) {
5560
case "proxy-data":
5661
mocks = getLiveMocks();
57-
console.log("Using grib2 data fetched from Datamart using proxy server!");
62+
echo("Using grib2 data fetched from Datamart using proxy server!");
5863
break;
5964
case "local-data":
6065
mocks = getLocalMocks();
61-
console.log("Using local (already downloaded) grib2 data");
66+
echo("Using local (already downloaded) grib2 data");
6267
break;
6368
default:
6469
console.error("BAD BUNDLE");
@@ -99,7 +104,7 @@ var beforeAfter = {
99104
};
100105

101106
function go (link) {
102-
console.log("Loading:'" + link + "'");
107+
echo("Loading:'" + link + "'");
103108
enableLoading();
104109

105110
link = link.replace("https://", "http://");
@@ -125,7 +130,7 @@ function go (link) {
125130
});
126131
res.on("end", function () {
127132
myGrid.parse(Buffer.concat(allChunks));
128-
console.log(myGrid);
133+
echo(myGrid);
129134

130135
if (isInteractive) {
131136
interactivePlot(myGrid, document.getElementById("interactivePlot"), beforeAfter);

0 commit comments

Comments
 (0)