mirror of
https://github.com/netbymatt/nexrad-level-2-plot.git
synced 2026-11-28 18:28:05 -08:00
Compare commits
1
Commits
non-hi-res
...
typescript
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b9a72ed99 |
No files matched your search
+62
-37
@@ -1,19 +1,33 @@
|
||||
module.exports = {
|
||||
root: true,
|
||||
env: {
|
||||
browser: true,
|
||||
commonjs: true,
|
||||
node: true,
|
||||
jquery: true,
|
||||
},
|
||||
extends: 'airbnb-base',
|
||||
extends: [
|
||||
'airbnb-base',
|
||||
'plugin:@typescript-eslint/eslint-recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
],
|
||||
globals: {
|
||||
Atomics: 'readonly',
|
||||
SharedArrayBuffer: 'readonly',
|
||||
},
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
ecmaVersion: 2020,
|
||||
},
|
||||
plugins: ['import'],
|
||||
settings: {
|
||||
'import/parsers': {
|
||||
'@typescript-eslint/parser': ['.ts', '.tsx'],
|
||||
},
|
||||
'import/resolver': {
|
||||
typescript: {
|
||||
alwaysTryTypes: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
indent: [
|
||||
'error',
|
||||
@@ -25,12 +39,12 @@ module.exports = {
|
||||
variables: false,
|
||||
},
|
||||
],
|
||||
'no-param-reassign': [
|
||||
'error',
|
||||
{
|
||||
props: false,
|
||||
},
|
||||
],
|
||||
// 'no-param-reassign': [
|
||||
// 'error',
|
||||
// {
|
||||
// props: false,
|
||||
// },
|
||||
// ],
|
||||
'no-tabs': 0,
|
||||
'no-console': 0,
|
||||
'max-len': 0,
|
||||
@@ -39,40 +53,51 @@ module.exports = {
|
||||
'error',
|
||||
'single',
|
||||
],
|
||||
semi: [
|
||||
'error',
|
||||
'always',
|
||||
],
|
||||
'no-prototype-builtins': 0,
|
||||
'comma-dangle': ['error', 'always-multiline'],
|
||||
'block-scoped-var': ['error'],
|
||||
'default-case': ['error'],
|
||||
'default-param-last': ['error'],
|
||||
'dot-location': ['error', 'property'],
|
||||
eqeqeq: ['error'],
|
||||
'no-eval': ['error'],
|
||||
'no-eq-null': ['error'],
|
||||
'no-floating-decimal': ['error'],
|
||||
'no-trailing-spaces': ['error'],
|
||||
'brace-style': [2, '1tbs', { allowSingleLine: true }],
|
||||
'no-mixed-operators': [
|
||||
// semi: [
|
||||
// 'error',
|
||||
// 'always',
|
||||
// ],
|
||||
// 'no-prototype-builtins': 0,
|
||||
// 'comma-dangle': ['error', 'always-multiline'],
|
||||
// 'block-scoped-var': ['error'],
|
||||
// 'default-case': ['error'],
|
||||
// 'default-param-last': ['error'],
|
||||
// 'dot-location': ['error', 'property'],
|
||||
// eqeqeq: ['error'],
|
||||
// 'no-eval': ['error'],
|
||||
// 'no-eq-null': ['error'],
|
||||
// 'no-floating-decimal': ['error'],
|
||||
// 'no-trailing-spaces': ['error'],
|
||||
// 'brace-style': [2, '1tbs', { allowSingleLine: true }],
|
||||
// 'no-mixed-operators': [
|
||||
// 'error',
|
||||
// {
|
||||
// groups: [
|
||||
// ['&', '|', '^', '~', '<<', '>>', '>>>'],
|
||||
// ['==', '!=', '===', '!==', '>', '>=', '<', '<='],
|
||||
// ['&&', '||'],
|
||||
// ['in', 'instanceof'],
|
||||
// ],
|
||||
// allowSamePrecedence: true,
|
||||
// },
|
||||
// ],
|
||||
// 'no-underscore-dangle': ['error', {
|
||||
// allowAfterThis: true,
|
||||
// }],
|
||||
// 'no-bitwise': 0,
|
||||
'import/extensions': [
|
||||
'error',
|
||||
'ignorePackages',
|
||||
{
|
||||
groups: [
|
||||
['&', '|', '^', '~', '<<', '>>', '>>>'],
|
||||
['==', '!=', '===', '!==', '>', '>=', '<', '<='],
|
||||
['&&', '||'],
|
||||
['in', 'instanceof'],
|
||||
],
|
||||
allowSamePrecedence: true,
|
||||
ts: 'never',
|
||||
js: 'never',
|
||||
},
|
||||
],
|
||||
'no-underscore-dangle': ['error', {
|
||||
allowAfterThis: true,
|
||||
}],
|
||||
'no-bitwise': 0,
|
||||
// the following are handled by the typescript parser
|
||||
'lines-between-class-members': 'off',
|
||||
},
|
||||
ignorePatterns: [
|
||||
'*.min.js',
|
||||
'dist/**',
|
||||
],
|
||||
};
|
||||
Vendored
+23
-1
@@ -11,7 +11,29 @@
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "${workspaceFolder}/demos/test.js"
|
||||
"program": "${workspaceFolder}/dist/demos/test.js",
|
||||
"preLaunchTask": "tsc: build - tsconfig.json",
|
||||
"outFiles": [
|
||||
"${workspaceFolder}/dist/**/*.js",
|
||||
"!**/node_modules/**"
|
||||
],
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
},
|
||||
{
|
||||
"type": "pwa-node",
|
||||
"request": "launch",
|
||||
"name": "Test Single",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "${workspaceFolder}/dist/demos/test.js",
|
||||
"preLaunchTask": "tsc: build - tsconfig.json",
|
||||
"args": ["single"],
|
||||
"outFiles": [
|
||||
"${workspaceFolder}/dist/**/*.js",
|
||||
"!**/node_modules/**"
|
||||
],
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
}
|
||||
]
|
||||
}
|
||||
Vendored
+3
-1
@@ -1,7 +1,8 @@
|
||||
{
|
||||
"files.exclude": {
|
||||
"node_modules/nexrad-level-2-data/**/node_modules": true,
|
||||
"**/node_modules": true
|
||||
"**/node_modules": true,
|
||||
"dist/**": true,
|
||||
},
|
||||
"cSpell.words": [
|
||||
"KLOT",
|
||||
@@ -9,6 +10,7 @@
|
||||
"NOAA",
|
||||
"Palettized",
|
||||
"RGBA",
|
||||
"Rbga",
|
||||
"palettization",
|
||||
"palettize",
|
||||
"palletizing",
|
||||
|
||||
Vendored
+56
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
const { Level2Radar } = require('nexrad-level-2-data');
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||
const glob = require('glob');
|
||||
const fs = require('fs');
|
||||
const { plot, writePngToFile } = require('../index');
|
||||
// list files
|
||||
// const files = glob.sync('./data/KLOT/34/*');
|
||||
// const files = glob.sync('./data/KLOT/381/*');
|
||||
// const files = glob.sync('./data/KLOT/548/*');
|
||||
// const files = glob.sync('./data/KLOT/940/*');
|
||||
// const files = glob.sync('./data/KSRX/804/*');
|
||||
// const files = glob.sync('./data/TORD/767/*');
|
||||
const files = glob.sync('./data/KLOT/KLOT20210812_171451_V*'); // ref palette tuning
|
||||
// const files = glob.sync('./data/KLOT/KLOT20210621_041151_V06'); // vel palette tuning
|
||||
// const store each file's data
|
||||
const chunks = [];
|
||||
// parse each file
|
||||
files.forEach((file) => {
|
||||
const fileBuffer = fs.readFileSync(file);
|
||||
chunks.push(new Level2Radar(fileBuffer));
|
||||
});
|
||||
// combine data
|
||||
const radarData = Level2Radar.combineData(chunks);
|
||||
// result array by elevations
|
||||
const plots = [];
|
||||
// default to all plots unless single is specificed
|
||||
const single = process.argv.includes('single');
|
||||
let sizes = [3600, 1800, 900, 800, 600, 450, 100];
|
||||
let elevations = radarData.listElevations();
|
||||
if (single) {
|
||||
sizes = sizes.slice(0, 1);
|
||||
// elevations where the lowest elevation is present, this is to ensure velocity is available
|
||||
const firstElevationIndex = elevations.slice(0, 1)[0];
|
||||
const lowestAngle = radarData.vcp.record.elevations[firstElevationIndex].elevation_angle;
|
||||
elevations = elevations.filter((elevation) => radarData.vcp.record.elevations[elevation].elevation_angle === lowestAngle);
|
||||
}
|
||||
// plot for each elevation and size
|
||||
(async () => {
|
||||
await Promise.allSettled(sizes.map(async (size) => {
|
||||
await Promise.allSettled(elevations.map(async (elevation) => {
|
||||
plots[elevation] = plot(radarData, ['REF', 'VEL'], {
|
||||
elevation,
|
||||
size,
|
||||
palettize: true,
|
||||
cropTo: size / 2,
|
||||
});
|
||||
// write files to disk
|
||||
await Promise.allSettled([
|
||||
writePngToFile(`./output/REF-${elevation}-${size}.png`, plots[elevation].REF),
|
||||
writePngToFile(`./output/VEL-${elevation}-${size}.png`, plots[elevation].VEL),
|
||||
]);
|
||||
}));
|
||||
}));
|
||||
})();
|
||||
//# sourceMappingURL=test.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"test.js","sourceRoot":"","sources":["../../src/demos/test.js"],"names":[],"mappings":";AAAA,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;AACvD,6DAA6D;AAC7D,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC7B,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEzB,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAErD,aAAa;AACb,+CAA+C;AAC/C,gDAAgD;AAChD,gDAAgD;AAChD,gDAAgD;AAChD,gDAAgD;AAChD,gDAAgD;AAChD,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,oCAAoC,CAAC,CAAC,CAAC,qBAAqB;AACpF,wFAAwF;AAExF,+BAA+B;AAC/B,MAAM,MAAM,GAAG,EAAE,CAAC;AAElB,kBAAkB;AAClB,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;IACtB,MAAM,UAAU,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IACzC,MAAM,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,CAAC,CAAC;AAC1C,CAAC,CAAC,CAAC;AAEH,eAAe;AACf,MAAM,SAAS,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AAElD,6BAA6B;AAC7B,MAAM,KAAK,GAAG,EAAE,CAAC;AAEjB,mDAAmD;AACnD,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAE/C,IAAI,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI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AI,IAAI,MAAM,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC;aAC7E,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,EAAE,CAAC"}
|
||||
Vendored
+170
@@ -0,0 +1,170 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const canvas_1 = __importDefault(require("canvas"));
|
||||
const palettes_1 = __importDefault(require("./palettes"));
|
||||
const ref_1 = __importDefault(require("./palettes/ref"));
|
||||
const vel_1 = __importDefault(require("./palettes/vel"));
|
||||
const palettize_1 = __importDefault(require("./palettize"));
|
||||
const { createCanvas } = canvas_1.default;
|
||||
// names of data structures keyed to product name
|
||||
const dataNames = {
|
||||
REF: 'reflect',
|
||||
VEL: 'velocity',
|
||||
'SW ': 'spectrum',
|
||||
ZDR: 'zdr',
|
||||
PHI: 'phi',
|
||||
RHO: 'rho',
|
||||
};
|
||||
// names of data retrieval routines keyed to product name
|
||||
const dataFunctions = {
|
||||
REF: 'getHighresReflectivity',
|
||||
VEL: 'getHighresVelocity',
|
||||
};
|
||||
// generate all palettes
|
||||
const palettes = {
|
||||
REF: new palettes_1.default(ref_1.default),
|
||||
VEL: new palettes_1.default(vel_1.default),
|
||||
};
|
||||
// default options
|
||||
const DEFAULT_OPTIONS = {
|
||||
// must be a square image
|
||||
size: 3600,
|
||||
cropTo: 3600,
|
||||
background: 'black',
|
||||
lineWidth: 2,
|
||||
};
|
||||
// calculation constants
|
||||
const RAD45 = ((45 * Math.PI) / 180);
|
||||
const RAD90 = RAD45 * 2;
|
||||
const draw = (data, _options) => {
|
||||
// combine options with defaults
|
||||
const options = {
|
||||
...DEFAULT_OPTIONS,
|
||||
..._options,
|
||||
};
|
||||
// calculate scale
|
||||
if (options.size > DEFAULT_OPTIONS.size)
|
||||
throw new Error(`Upsampling is not supported. Provide a size <= ${DEFAULT_OPTIONS.size}`);
|
||||
if (options.size < 1)
|
||||
throw new Error('Provide options.size > 0');
|
||||
const scale = DEFAULT_OPTIONS.size / options.size;
|
||||
// calculate crop, adjust if necessary
|
||||
const cropTo = Math.min(options.size, options.cropTo);
|
||||
if (options.cropTo < 1)
|
||||
throw new Error('Provide options.cropTo > 0');
|
||||
// create the canvas and context
|
||||
const canvas = createCanvas(cropTo, cropTo);
|
||||
const ctx = canvas.getContext('2d');
|
||||
// fill background with black
|
||||
ctx.fillStyle = options.background;
|
||||
ctx.fillRect(0, 0, cropTo, cropTo);
|
||||
// canvas settings
|
||||
ctx.imageSmoothingEnabled = true;
|
||||
ctx.lineWidth = options.lineWidth;
|
||||
ctx.translate(cropTo / 2, cropTo / 2);
|
||||
ctx.rotate(-Math.PI / 2);
|
||||
// get the palette
|
||||
const palette = palettes[options.product];
|
||||
// test for valid palette
|
||||
if (!palette)
|
||||
throw new Error(`No product found for product type: ${options.product}`);
|
||||
// set the elevation
|
||||
data.setElevation(options.elevation);
|
||||
// get the header data
|
||||
const headers = data.getHeader();
|
||||
// calculate resolution in radians, default to 1°
|
||||
let resolution = Math.PI / 180;
|
||||
if (data?.vcp?.record?.elevation?.s[options.elevation]?.super_res_control?.super_res?.halfDegreeAzimuth)
|
||||
resolution /= 2;
|
||||
// calculate half resolution step for additional calculations below
|
||||
const halfResolution = resolution / 2;
|
||||
// match product name to data
|
||||
const dataName = dataNames[options.product];
|
||||
const dataFunction = dataFunctions[options.product];
|
||||
// check for valid product
|
||||
if (dataName === undefined)
|
||||
throw new Error(`No data object name found for product: ${options.product}`);
|
||||
if (dataFunction === undefined)
|
||||
throw new Error(`No data function found for product: ${options.product}`);
|
||||
// check for data for this product
|
||||
if (headers[0][dataName] === undefined)
|
||||
return false;
|
||||
// loop through data
|
||||
headers.forEach((header) => {
|
||||
// get correct data
|
||||
const thisRadial = header[dataName];
|
||||
// skip if this radial isn't found
|
||||
if (thisRadial === undefined)
|
||||
return;
|
||||
// calculate plotting parameters
|
||||
const deadZone = thisRadial.first_gate / thisRadial.gate_size;
|
||||
const startAngle = header.azimuth * (Math.PI / 180) - halfResolution;
|
||||
const endAngle = startAngle + resolution;
|
||||
// track max value for downsampling(d)
|
||||
let downsampled = 0;
|
||||
let lastRemainder = 0;
|
||||
// calculate maximum bin to plot based on azimuth
|
||||
// wrap azimuth to 90° offset by -45° (% in js is remainder, the formula below make it in to modulus)
|
||||
const azWrap = (((startAngle - RAD45) % RAD90) + RAD90) % RAD90;
|
||||
// calculate a magnitude multiplier as 1/sin with 45° shift removed
|
||||
const azMagnitudeMult = 1 / Math.abs(Math.sin(azWrap + RAD45));
|
||||
const cropMaxBin = Math.ceil(Math.abs((options.cropTo / 2) * scale * azMagnitudeMult));
|
||||
// compare max calculated value with length of radial
|
||||
const maxBin = Math.min(cropMaxBin, thisRadial.moment_data.length);
|
||||
// plot each bin
|
||||
for (let idx = 0; idx < maxBin; idx += 1) {
|
||||
// get the value
|
||||
const bin = thisRadial.moment_data[idx];
|
||||
// skip null values
|
||||
if (bin !== null) {
|
||||
let thisSample;
|
||||
// test for downsampling
|
||||
if (scale !== 1) {
|
||||
const remainder = idx % scale;
|
||||
// test for rollover in scaling
|
||||
if (remainder < lastRemainder) {
|
||||
// plot this point and reset values
|
||||
thisSample = downsampled;
|
||||
downsampled = 0;
|
||||
}
|
||||
// store this sample if it meets downsample requirements
|
||||
downsampled = palette.downSample(bin, downsampled);
|
||||
// store for rollover tracking
|
||||
lastRemainder = remainder;
|
||||
}
|
||||
else {
|
||||
thisSample = bin;
|
||||
}
|
||||
// see if there's a sample to plot
|
||||
if (thisSample && !palette.inDeadband(thisSample)) {
|
||||
ctx.beginPath();
|
||||
ctx.strokeStyle = palette.findColorRgba(thisSample);
|
||||
ctx.arc(0, 0, (idx + deadZone) / scale, startAngle, endAngle);
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!options.palettize) {
|
||||
// return the palette and canvas
|
||||
return {
|
||||
canvas,
|
||||
};
|
||||
}
|
||||
// palettize image
|
||||
const palettized = palettize_1.default(canvas.getContext('2d'), palette);
|
||||
// return palettized image
|
||||
return {
|
||||
canvas: palettized,
|
||||
palette: palette.getPalette(),
|
||||
};
|
||||
};
|
||||
module.exports = {
|
||||
draw,
|
||||
DEFAULT_OPTIONS,
|
||||
canvas: canvas_1.default,
|
||||
};
|
||||
//# sourceMappingURL=index.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/draw/index.ts"],"names":[],"mappings":";;;;;AAAA,oDAA+B;AAC/B,0DAAiC;AACjC,yDAAiC;AACjC,yDAAiC;AACjC,4DAAyC;AAGzC,MAAM,EAAE,YAAY,EAAE,GAAG,gBAAS,CAAC;AAMnC,iDAAiD;AACjD,MAAM,SAAS,GAAiB;IAC/B,GAAG,EAAE,SAAS;IACd,GAAG,EAAE,UAAU;IACf,KAAK,EAAE,UAAU;IACjB,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,KAAK;CACV,CAAC;AAEF,yDAAyD;AACzD,MAAM,aAAa,GAAiB;IACnC,GAAG,EAAE,wBAAwB;IAC7B,GAAG,EAAE,oBAAoB;CACzB,CAAC;AAMF,wBAAwB;AACxB,MAAM,QAAQ,GAAY;IACzB,GAAG,EAAE,IAAI,kBAAO,CAAC,aAAG,CAAC;IACrB,GAAG,EAAE,IAAI,kBAAO,CAAC,aAAG,CAAC;CACrB,CAAC;AAEF,kBAAkB;AAClB,MAAM,eAAe,GAAG;IACvB,yBAAyB;IACzB,IAAI,EAAE,IAAI;IACV,MAAM,EAAE,IAAI;IACZ,UAAU,EAAE,OAAO;IACnB,SAAS,EAAE,CAAC;CACZ,CAAC;AAEF,wBAAwB;AACxB,MAAM,KAAK,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC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,OAAO,EAAE,OAAO,CAAC,UAAU,EAAE;KLine truncated
|
||||
Vendored
+102
@@ -0,0 +1,102 @@
|
||||
"use strict";
|
||||
// ingest a palette and provide lookup and formatting functionality
|
||||
// {palette: [r1,g1,b1,a1, r2,g2,b2,a2, ...], limits: [1,2, ...]}
|
||||
// rgba values are returned with the the color index in the g position with 100% opacity
|
||||
// this ensures that the colors are not blended because the index should be returned in the a position
|
||||
// after the entire image is drawn the index information must be shifted from g to a
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const rgbaLookupSize = 200;
|
||||
class Palette {
|
||||
palette;
|
||||
limits;
|
||||
transparentIndex;
|
||||
lookupRgba;
|
||||
closest;
|
||||
downSampleReset;
|
||||
downSample;
|
||||
inDeadband;
|
||||
constructor(palette) {
|
||||
this.palette = palette.palette;
|
||||
this.limits = palette.limits;
|
||||
this.transparentIndex = palette.transparentIndex ?? 0;
|
||||
this.lookupRgba = findDbzRbgaGenerator(this.limits, this.palette, palette.maxDbzIndex);
|
||||
this.closest = {};
|
||||
this.downSampleReset = palette.downSampleReset ?? -Infinity;
|
||||
if (typeof palette.downSample === 'function') {
|
||||
this.downSample = palette.downSample;
|
||||
}
|
||||
else {
|
||||
this.downSample = downSample;
|
||||
}
|
||||
if (typeof palette.inDeadband === 'function') {
|
||||
this.inDeadband = palette.inDeadband;
|
||||
}
|
||||
else {
|
||||
this.inDeadband = inDeadband(this.downSampleReset);
|
||||
}
|
||||
}
|
||||
findColorRgba(dbz) {
|
||||
// find the rgba value of the color from the provided dbz value
|
||||
return this.lookupRgba[Math.trunc(dbz + rgbaLookupSize)];
|
||||
}
|
||||
getPalette() {
|
||||
// return the palette properly formatted for the PNG function
|
||||
const dest = new Uint8ClampedArray(256 * 4);
|
||||
this.palette.forEach((val, idx) => {
|
||||
dest[idx] = val;
|
||||
});
|
||||
return dest;
|
||||
}
|
||||
transparentColorRgba() {
|
||||
// return the rgba value of the transparent color
|
||||
const index = this.transparentIndex;
|
||||
return `rgba(${this.palette[index * 4]},${this.palette[index * 4 + 1]},${this.palette[index * 4 + 2]},${this.palette[(index * 4 + 3)] / 255})`;
|
||||
}
|
||||
// match = [r,g,b[,a]]
|
||||
closestIndex(match) {
|
||||
// short circuit for transparent (black)
|
||||
if (match[0] <= 2 && match[1] <= 2 && match[2] <= 2)
|
||||
return this.transparentIndex;
|
||||
// short circuit previously calculated matches
|
||||
const asHex = match[0].toString(16).padStart(2, '0') + match[1].toString(16).padStart(2, '0') + match[2].toString(16).padStart(2, '0');
|
||||
if (this.closest[asHex])
|
||||
return this.closest[asHex];
|
||||
// initial conditions
|
||||
let closestIndex = 0;
|
||||
let closest = Infinity;
|
||||
// loop through array
|
||||
for (let i = 0; i < this.palette.length; i += 4) {
|
||||
const dist = Palette.geometricDistance(match, this.palette.slice(i, i + match.length));
|
||||
// test for closer
|
||||
if (dist < closest) {
|
||||
closest = dist;
|
||||
closestIndex = i / 4;
|
||||
if (dist === 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
// store closest match to speed up next iteration
|
||||
this.closest[asHex] = closestIndex;
|
||||
return closestIndex;
|
||||
}
|
||||
// geometric distance
|
||||
static geometricDistance(a, b) {
|
||||
return Array.from(a).reduce((acc, val, idx) => acc + (val - b[idx]) ** 2, 0);
|
||||
}
|
||||
}
|
||||
exports.default = Palette;
|
||||
// create a lookup table of colors for faster processing
|
||||
const findDbzRbgaGenerator = (limits, palette, maxDbzIndex = 14) => {
|
||||
const lookupRgba = [];
|
||||
for (let i = -rgbaLookupSize; i <= rgbaLookupSize; i += 1) {
|
||||
let index = limits.findIndex((limit) => i < limit);
|
||||
// a value of -1 means it is off the end of the scale, set to highest value
|
||||
if (index === -1)
|
||||
index = maxDbzIndex;
|
||||
lookupRgba.push(`rgba(${palette[index * 4]},${palette[index * 4 + 1]},${palette[index * 4 + 2]},${palette[(index * 4 + 3)] / 255})`);
|
||||
}
|
||||
return lookupRgba;
|
||||
};
|
||||
const downSample = (cur, prev) => (cur === null ? prev : Math.max(cur, prev));
|
||||
const inDeadband = (reset) => (a) => (a === null || a === reset || a === undefined);
|
||||
//# sourceMappingURL=index.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/draw/palettes/index.ts"],"names":[],"mappings":";AAAA,mEAAmE;AACnE,iEAAiE;AACjE,wFAAwF;AACxF,sGAAsG;AACtG,oFAAoF;;AAIpF,MAAM,cAAc,GAAG,GAAG,CAAC;AAM3B,MAAqB,OAAO;IAC3B,OAAO,CAAW;IAClB,MAAM,CAAW;IACjB,gBAAgB,CAAS;IACzB,UAAU,CAAW;IACrB,OAAO,CAAU;IACjB,eAAe,CAAS;IACxB,UAAU,CAAoC;IAC9C,UAAU,CAA0B;IAEpC,YAAY,OAAmB;QAC9B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,CAAC,CAAC;QACtD,IAAI,CAAC,UAAU,GAAG,oBAAoB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;QACvF,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAElB,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,CAAC,QAAQ,CAAC;QAC5D,IAAI,OAAO,OAAO,CAAC,UAAU,KAAK,UAAU,EAAE;YAC7C,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;SACrC;aAAM;YACN,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;SAC7B;QAED,IAAI,OAAO,OAAO,CAAC,UAAU,KAAK,UAAU,EAAE;YAC7C,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;SACrC;aAAM;YACN,IAAI,CAAC,UAAU,GAAG,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
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const palette = [
|
||||
255, 255, 255, 0,
|
||||
0, 128, 128, 192,
|
||||
1, 80, 128, 192,
|
||||
0, 0, 128, 192,
|
||||
0, 255, 0, 255,
|
||||
0, 200, 0, 255,
|
||||
0, 144, 0, 255,
|
||||
255, 255, 0, 255,
|
||||
231, 192, 0, 255,
|
||||
255, 144, 0, 255,
|
||||
255, 0, 0, 255,
|
||||
214, 0, 0, 255,
|
||||
192, 0, 0, 255,
|
||||
255, 0, 255, 255,
|
||||
153, 85, 201, 255,
|
||||
// additional halfway to black values for cleaner picture
|
||||
0, 64, 64, 96,
|
||||
1, 40, 64, 96,
|
||||
0, 0, 64, 96,
|
||||
0, 128, 0, 128,
|
||||
0, 100, 0, 128,
|
||||
0, 72, 0, 128,
|
||||
128, 128, 0, 128,
|
||||
116, 96, 0, 128,
|
||||
128, 72, 0, 128,
|
||||
128, 0, 0, 128,
|
||||
107, 0, 0, 128,
|
||||
96, 0, 0, 128,
|
||||
128, 0, 128, 128,
|
||||
77, 43, 101, 128,
|
||||
];
|
||||
const limits = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75];
|
||||
const paletteData = {
|
||||
palette,
|
||||
limits,
|
||||
maxDbzIndex: 14,
|
||||
};
|
||||
exports.default = paletteData;
|
||||
//# sourceMappingURL=ref.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../../src/draw/palettes/ref.ts"],"names":[],"mappings":";;AAEA,MAAM,OAAO,GAAY;IACxB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;IAChB,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;IAChB,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG;IACf,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG;IACd,CAAC,EAAE,GAAG,EAAE,CAAC,E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E,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;AAErF,MAAM,WAAW,GAAgB;IAChC,OAAO;IACP,MAAM;IACN,WAAW,EAAE,EAAE;CACf,CAAC;AAEF,kBAAe,WAAW,CAAC"}
|
||||
Vendored
+56
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const palette = [
|
||||
// 0: green/outbound
|
||||
0, 255, 0, 255,
|
||||
0, 240, 0, 255,
|
||||
0, 223, 0, 255,
|
||||
0, 206, 0, 255,
|
||||
0, 189, 0, 255,
|
||||
0, 172, 0, 255,
|
||||
0, 155, 0, 255,
|
||||
0, 138, 0, 255,
|
||||
16, 121, 16, 255,
|
||||
32, 113, 32, 255,
|
||||
48, 104, 48, 255,
|
||||
// 11: nearly zero
|
||||
64, 95, 64, 255,
|
||||
95, 64, 64, 255,
|
||||
// 13: red/inbound
|
||||
104, 32, 32, 255,
|
||||
113, 16, 16, 255,
|
||||
121, 0, 0, 255,
|
||||
138, 0, 0, 255,
|
||||
155, 0, 0, 255,
|
||||
172, 0, 0, 255,
|
||||
189, 0, 0, 255,
|
||||
206, 0, 0, 255,
|
||||
223, 0, 0, 255,
|
||||
240, 0, 0, 255,
|
||||
255, 0, 0, 255,
|
||||
// 24: transparent
|
||||
255, 255, 255, 0,
|
||||
];
|
||||
const downSample = (cur, prev) => {
|
||||
// no data, use previous value
|
||||
if (cur === null)
|
||||
return prev;
|
||||
// test for magnitude change, prev may be null but Math.abs(null) === 0 so this is valid
|
||||
if (Math.abs(cur) >= Math.abs(prev))
|
||||
return cur;
|
||||
return prev;
|
||||
};
|
||||
const limits = [-99, -90, -80, -70, -60, -50, -40, -30, -20, -15, -10, -5, 0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 99,
|
||||
];
|
||||
const downSampleReset = null;
|
||||
const transparentIndex = 24;
|
||||
const paletteData = {
|
||||
palette,
|
||||
downSample,
|
||||
limits,
|
||||
downSampleReset,
|
||||
transparentIndex,
|
||||
maxDbzIndex: 23,
|
||||
};
|
||||
exports.default = paletteData;
|
||||
//# sourceMappingURL=vel.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"vel.js","sourceRoot":"","sources":["../../../src/draw/palettes/vel.ts"],"names":[],"mappings":";;AAEA,MAAM,OAAO,GAAY;IACxB,oBAAoB;IACpB,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG;IACd,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG;IACd,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG;IACd,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG;IACd,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG;IACd,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG;IACd,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG;IACd,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG;IACd,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG;IAChB,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG;IAChB,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG;IAEhB,kBAAkB;IAClB,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG;IACf,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG;IAEf,kBAAkB;IAClB,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG;IAChB,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG;IAChB,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG;IACd,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG;IACd,GAAG,EAAE,CAAC,EAAE,CAAC,E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AgB;IAChB,WAAW,EAAE,EAAE;CACf,CAAC;AAEF,kBAAe,WAAW,CAAC"}
|
||||
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const canvas_1 = require("canvas");
|
||||
const palettizeImage = (sourceCtx, palette) => {
|
||||
// get the dimensions of the image from the ctx
|
||||
const dim = {
|
||||
x: sourceCtx.canvas.width,
|
||||
y: sourceCtx.canvas.height,
|
||||
};
|
||||
console.time('Palettize image');
|
||||
// transform into indexed palette by finding the closest indexed color for each pixel
|
||||
// create a destination image that is transparent and indexed
|
||||
const indexedCanvas = canvas_1.createCanvas(dim.x, dim.y);
|
||||
const indexedCtx = indexedCanvas.getContext('2d', { pixelFormat: 'A8' });
|
||||
// get the source image data
|
||||
const sourceImageData = sourceCtx.getImageData(0, 0, dim.x, dim.y);
|
||||
// get the indexed image data (destination)
|
||||
const indexedImageData = indexedCtx.getImageData(0, 0, dim.x, dim.y);
|
||||
// loop through each pixel
|
||||
console.log(`Palettized cache size: ${Object.keys(palette.closest).length}`);
|
||||
indexedImageData.data.forEach((val, idx) => {
|
||||
indexedImageData.data[idx] = palette.closestIndex(sourceImageData.data.slice(idx * 4, idx * 4 + 3));
|
||||
});
|
||||
console.timeEnd('Palettize image');
|
||||
// write the new image data
|
||||
indexedCtx.putImageData(indexedImageData, 0, 0);
|
||||
return indexedCanvas;
|
||||
};
|
||||
exports.default = palettizeImage;
|
||||
//# sourceMappingURL=palettize.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"palettize.js","sourceRoot":"","sources":["../../src/draw/palettize.ts"],"names":[],"mappings":";;AAAA,mCAAwE;AAGxE,MAAM,cAAc,GAAG,CAAC,SAAkC,EAAE,OAAgB,EAAS,EAAE;IACtF,+CAA+C;IAC/C,MAAM,GAAG,GAAG;QACX,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,KAAK;QACzB,CAAC,EAAE,SAAS,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|
||||
Vendored
+49
@@ -0,0 +1,49 @@
|
||||
"use strict";
|
||||
const { draw, canvas } = require('./draw');
|
||||
const { writePngToFile } = require('./utils/file');
|
||||
/**
|
||||
* Plot level 2 data
|
||||
* @param {Level2Data} data output from the nexrad-level-2-data library
|
||||
* @param {(string|string[])} _products Options are REF, VEL, SW , ZDR, PHI and RHO
|
||||
* @param {object} options Plotting options
|
||||
* @param {number} [options.size=3600] 1 to 3600. Size of the x and y axis in pixels. The image must be square so only a single integer is needed.
|
||||
* @param {number} [options.cropTo=3600] 1 to 3600. After scaling and downsampling as described above crop the resulting plot to the size specified. Internally, the image is actually drawn at the cropped size to save on processing time.
|
||||
* @param {string} [options.background=#000000] Background color of the image. This can be transparent by using #RGBA notation.
|
||||
* @param {number} [options.lineWidth=2] The raster image is created by drawing several arcs at the locations and colors specified in the data file. When scaling down you may get a better looking image by adjusting this value to something large than the default.
|
||||
* @param {(boolean|object)} [options.palettize] After drawing the image convert the image from RGBA to a palettized image. When true the same palette as the product is used.
|
||||
* @returns Canvas
|
||||
*/
|
||||
const plot = (data, _products, options) => {
|
||||
// store result
|
||||
const result = {};
|
||||
let products;
|
||||
// make product list into an array
|
||||
if (_products && Array.isArray(_products)) {
|
||||
products = _products;
|
||||
}
|
||||
else if (typeof _products === 'string') {
|
||||
products = [_products];
|
||||
}
|
||||
else {
|
||||
// default to all products
|
||||
products = ['REF', 'VEL', 'SW ', 'ZDR', 'PHI', 'RHO'];
|
||||
}
|
||||
// get the available elevations
|
||||
const elevations = data.listElevations();
|
||||
if (elevations.length === 0)
|
||||
throw new Error('No elevations availabe');
|
||||
products.forEach((product) => {
|
||||
// parse and store result
|
||||
result[product] = draw(data, {
|
||||
...options,
|
||||
product,
|
||||
});
|
||||
});
|
||||
return result;
|
||||
};
|
||||
module.exports = {
|
||||
plot,
|
||||
writePngToFile,
|
||||
canvas,
|
||||
};
|
||||
//# sourceMappingURL=index.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"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use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
//# sourceMappingURL=types.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs = require('fs');
|
||||
// write a canvas to a Png file
|
||||
/**
|
||||
* Write a Canvas to the specified file
|
||||
* @param {string} fileName Path to write output to, passed to fs.createWriteStream
|
||||
* @param {Canvas} data Canvas provided by the plot() function
|
||||
* @returns Promise
|
||||
*/
|
||||
const writePngToFile = (fileName, data) => new Promise((resolve, reject) => {
|
||||
// the draw function will return false if no data was found, short circuit the output here
|
||||
if (!data.canvas) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
// get a write stream
|
||||
const writeStream = fs.createWriteStream(fileName);
|
||||
// options
|
||||
const options = {};
|
||||
if (data.palette)
|
||||
options.palette = data.palette;
|
||||
// write to the stream
|
||||
data.canvas.createPNGStream(options).pipe(writeStream);
|
||||
writeStream.on('finish', () => resolve());
|
||||
writeStream.on('error', (e) => reject(e));
|
||||
});
|
||||
module.exports = {
|
||||
writePngToFile,
|
||||
};
|
||||
//# sourceMappingURL=file.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"file.js","sourceRoot":"","sources":["../../src/utils/file.ts"],"names":[],"mappings":";;AAGA,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AACzB,+BAA+B;AAC/B;;;;;GAKG;AACH,MAAM,cAAc,GAAG,CAAC,QAAgB,EAAE,IAAU,EAAE,EAAE,CAAC,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;IAC9F,0FAA0F;IAC1F,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;QACjB,OAAO,EAAE,CAAC;QACV,OAAO;KACP;IAED,qBAAqB;IACrB,MAAM,WAAW,GAAG,EAAE,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IAEnD,UAAU;IACV,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,IAAI,IAAI,CAAC,OAAO;QAAE,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;IAEjD,sBAAsB;IACtB,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACvD,WAAW,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;IAC1C,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,CAAQ,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAClD,CAAC,CAAC,CAAC;AAEH,MAAM,CAAC,OAAO,GAAG;IAChB,cAAc;CACd,CAAC"}
|
||||
Generated
+823
-4
File diff suppressed because it is too large.
Load diff
+7
-2
@@ -25,13 +25,18 @@
|
||||
"homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
|
||||
"dependencies": {
|
||||
"canvas": "^2.8.0",
|
||||
"nexrad-level-2-data": "file:../nexrad-level-2-data"
|
||||
"nexrad-level-2-data": "file:../nexrad-level-2-data"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.7.2",
|
||||
"@typescript-eslint/eslint-plugin": "^4.29.3",
|
||||
"@typescript-eslint/parser": "^4.29.3",
|
||||
"eslint": "^7.32.0",
|
||||
"eslint-config-airbnb-base": "^14.2.1",
|
||||
"eslint-import-resolver-typescript": "^2.4.0",
|
||||
"eslint-plugin-import": "^2.23.4",
|
||||
"glob": "^7.1.7"
|
||||
"glob": "^7.1.7",
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=13.0.0"
|
||||
|
||||
@@ -3,7 +3,7 @@ const { Level2Radar } = require('nexrad-level-2-data');
|
||||
const glob = require('glob');
|
||||
const fs = require('fs');
|
||||
|
||||
const { plot, writePngToFile } = require('../src');
|
||||
const { plot, writePngToFile } = require('../index');
|
||||
|
||||
// list files
|
||||
// const files = glob.sync('./data/KLOT/34/*');
|
||||
@@ -1,12 +1,18 @@
|
||||
const canvasObj = require('canvas');
|
||||
import canvasObj from 'canvas';
|
||||
import Palette from './palettes';
|
||||
import REF from './palettes/ref';
|
||||
import VEL from './palettes/vel';
|
||||
import palettizeImage from './palettize';
|
||||
import { DrawOptions } from '../types';
|
||||
|
||||
const { createCanvas } = canvasObj;
|
||||
|
||||
const Palette = require('./palettes');
|
||||
const palettizeImage = require('./palettize');
|
||||
interface ObjectStrings {
|
||||
[key: string]: string,
|
||||
}
|
||||
|
||||
// names of data structures keyed to product name
|
||||
const dataNames = {
|
||||
const dataNames:ObjectStrings = {
|
||||
REF: 'reflect',
|
||||
VEL: 'velocity',
|
||||
'SW ': 'spectrum', // intentional space to fill 3-character requirement
|
||||
@@ -16,21 +22,23 @@ const dataNames = {
|
||||
};
|
||||
|
||||
// names of data retrieval routines keyed to product name
|
||||
const dataFunctions = {
|
||||
const dataFunctions:ObjectStrings = {
|
||||
REF: 'getHighresReflectivity',
|
||||
VEL: 'getHighresVelocity',
|
||||
};
|
||||
|
||||
interface Palettes {
|
||||
[key: string]: Palette,
|
||||
}
|
||||
|
||||
// generate all palettes
|
||||
/* eslint-disable global-require */
|
||||
const palettes = {
|
||||
REF: new Palette(require('./palettes/ref')),
|
||||
VEL: new Palette(require('./palettes/vel')),
|
||||
const palettes:Palettes = {
|
||||
REF: new Palette(REF),
|
||||
VEL: new Palette(VEL),
|
||||
};
|
||||
/* eslint-enable global-require */
|
||||
|
||||
// default options
|
||||
const DEFAULT_OPTIONS = {
|
||||
export const DEFAULT_OPTIONS = {
|
||||
// must be a square image
|
||||
size: 3600,
|
||||
cropTo: 3600,
|
||||
@@ -39,10 +47,15 @@ const DEFAULT_OPTIONS = {
|
||||
};
|
||||
|
||||
// calculation constants
|
||||
const RAD45 = (45 * Math.PI / 180);
|
||||
const RAD45 = ((45 * Math.PI) / 180);
|
||||
const RAD90 = RAD45 * 2;
|
||||
|
||||
const draw = (data, _options) => {
|
||||
export interface draw {
|
||||
canvas: canvasObj.Canvas,
|
||||
palette?: Uint8ClampedArray,
|
||||
}
|
||||
|
||||
export const draw = (data, _options:DrawOptions):draw => {
|
||||
// combine options with defaults
|
||||
const options = {
|
||||
...DEFAULT_OPTIONS,
|
||||
@@ -97,7 +110,7 @@ const draw = (data, _options) => {
|
||||
if (dataFunction === undefined) throw new Error(`No data function found for product: ${options.product}`);
|
||||
|
||||
// check for data for this product
|
||||
if (headers[0][dataName] === undefined) return false;
|
||||
if (headers[0][dataName] === undefined) throw new Error('No data for this product');
|
||||
|
||||
// loop through data
|
||||
headers.forEach((header) => {
|
||||
@@ -121,7 +134,7 @@ const draw = (data, _options) => {
|
||||
const azWrap = (((startAngle - RAD45) % RAD90) + RAD90) % RAD90;
|
||||
// calculate a magnitude multiplier as 1/sin with 45° shift removed
|
||||
const azMagnitudeMult = 1 / Math.abs(Math.sin(azWrap + RAD45));
|
||||
const cropMaxBin = Math.ceil(Math.abs(options.cropTo / 2 * scale * azMagnitudeMult));
|
||||
const cropMaxBin = Math.ceil(Math.abs((options.cropTo / 2) * scale * azMagnitudeMult));
|
||||
|
||||
// compare max calculated value with length of radial
|
||||
const maxBin = Math.min(cropMaxBin, thisRadial.moment_data.length);
|
||||
@@ -179,8 +192,4 @@ const draw = (data, _options) => {
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
draw,
|
||||
DEFAULT_OPTIONS,
|
||||
canvas: canvasObj,
|
||||
};
|
||||
export { Canvas } from 'canvas';
|
||||
@@ -4,22 +4,32 @@
|
||||
// this ensures that the colors are not blended because the index should be returned in the a position
|
||||
// after the entire image is drawn the index information must be shifted from g to a
|
||||
|
||||
import { PaletteData } from '../../types';
|
||||
|
||||
const rgbaLookupSize = 200;
|
||||
|
||||
class Palette {
|
||||
constructor(palette) {
|
||||
interface Closest {
|
||||
[key: string]: number,
|
||||
}
|
||||
|
||||
export default class Palette {
|
||||
palette: number[];
|
||||
limits: number[];
|
||||
transparentIndex: number;
|
||||
lookupRgba: string[];
|
||||
closest: Closest;
|
||||
downSampleReset: number;
|
||||
downSample: (cur:number, prev:number)=>number;
|
||||
inDeadband: (reset:number)=>boolean;
|
||||
|
||||
constructor(palette:PaletteData) {
|
||||
this.palette = palette.palette;
|
||||
this.limits = palette.limits;
|
||||
this.transparentIndex = palette.transparentIndex;
|
||||
this.transparentIndex = palette.transparentIndex ?? 0;
|
||||
this.lookupRgba = findDbzRbgaGenerator(this.limits, this.palette, palette.maxDbzIndex);
|
||||
this.closest = {};
|
||||
|
||||
// these may not exist and are overridden with default functions
|
||||
if (palette.hasOwnProperty('downSampleReset')) {
|
||||
this.downSampleReset = palette.downSampleReset;
|
||||
} else {
|
||||
this.downSampleReset = -Infinity;
|
||||
}
|
||||
this.downSampleReset = palette.downSampleReset ?? -Infinity;
|
||||
if (typeof palette.downSample === 'function') {
|
||||
this.downSample = palette.downSample;
|
||||
} else {
|
||||
@@ -33,12 +43,12 @@ class Palette {
|
||||
}
|
||||
}
|
||||
|
||||
findColorRgba(dbz) {
|
||||
findColorRgba(dbz:number):string {
|
||||
// find the rgba value of the color from the provided dbz value
|
||||
return this.lookupRgba[Math.trunc(dbz + rgbaLookupSize)];
|
||||
}
|
||||
|
||||
getPalette() {
|
||||
getPalette():Uint8ClampedArray {
|
||||
// return the palette properly formatted for the PNG function
|
||||
const dest = new Uint8ClampedArray(256 * 4);
|
||||
this.palette.forEach((val, idx) => {
|
||||
@@ -47,14 +57,14 @@ class Palette {
|
||||
return dest;
|
||||
}
|
||||
|
||||
transparentColorRgba() {
|
||||
transparentColorRgba():string {
|
||||
// return the rgba value of the transparent color
|
||||
const index = this.transparentIndex;
|
||||
const index:number = this.transparentIndex;
|
||||
return `rgba(${this.palette[index * 4]},${this.palette[index * 4 + 1]},${this.palette[index * 4 + 2]},${this.palette[(index * 4 + 3)] / 255})`;
|
||||
}
|
||||
|
||||
// match = [r,g,b[,a]]
|
||||
closestIndex(match) {
|
||||
closestIndex(match:ArrayLike<number>):number {
|
||||
// short circuit for transparent (black)
|
||||
if (match[0] <= 2 && match[1] <= 2 && match[2] <= 2) return this.transparentIndex;
|
||||
// short circuit previously calculated matches
|
||||
@@ -79,13 +89,13 @@ class Palette {
|
||||
}
|
||||
|
||||
// geometric distance
|
||||
static geometricDistance(a, b) {
|
||||
return a.reduce((acc, val, idx) => acc + (val - b[idx]) ** 2, 0);
|
||||
static geometricDistance(a:ArrayLike<number>, b:ArrayLike<number>):number {
|
||||
return Array.from(a).reduce((acc:number, val:number, idx:number) => acc + (val - b[idx]) ** 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// create a lookup table of colors for faster processing
|
||||
const findDbzRbgaGenerator = (limits, palette, maxDbzIndex = 14) => {
|
||||
const findDbzRbgaGenerator = (limits:number[], palette:number[], maxDbzIndex = 14):string[] => {
|
||||
const lookupRgba = [];
|
||||
for (let i = -rgbaLookupSize; i <= rgbaLookupSize; i += 1) {
|
||||
let index = limits.findIndex((limit) => i < limit);
|
||||
@@ -96,8 +106,6 @@ const findDbzRbgaGenerator = (limits, palette, maxDbzIndex = 14) => {
|
||||
return lookupRgba;
|
||||
};
|
||||
|
||||
const downSample = (cur, prev) => (cur === null ? prev : Math.max(cur, prev));
|
||||
const downSample = (cur:number, prev:number):number => (cur === null ? prev : Math.max(cur, prev));
|
||||
|
||||
const inDeadband = (reset) => (a) => (a === null || a === reset || a === undefined);
|
||||
|
||||
module.exports = Palette;
|
||||
const inDeadband = (reset:number) => (a:number) => (a === null || a === reset || a === undefined);
|
||||
@@ -1,4 +1,6 @@
|
||||
const palette = [
|
||||
import { PaletteData } from '../../types';
|
||||
|
||||
const palette:number[] = [
|
||||
255, 255, 255, 0, // transparent
|
||||
0, 128, 128, 192,
|
||||
1, 80, 128, 192,
|
||||
@@ -32,15 +34,12 @@ const palette = [
|
||||
77, 43, 101, 128,
|
||||
];
|
||||
|
||||
const maxDbzIndex = 14; // index of maximum dbz
|
||||
const limits:number[] = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75];
|
||||
|
||||
const limits = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75];
|
||||
|
||||
const transparentIndex = 0;
|
||||
|
||||
module.exports = {
|
||||
const paletteData: PaletteData = {
|
||||
palette,
|
||||
maxDbzIndex,
|
||||
limits,
|
||||
transparentIndex,
|
||||
maxDbzIndex: 14,
|
||||
};
|
||||
|
||||
export default paletteData;
|
||||
@@ -1,4 +1,6 @@
|
||||
const palette = [
|
||||
import { PaletteData } from '../../types';
|
||||
|
||||
const palette:number[] = [
|
||||
// 0: green/outbound
|
||||
0, 255, 0, 255,
|
||||
0, 240, 0, 255,
|
||||
@@ -33,7 +35,7 @@ const palette = [
|
||||
255, 255, 255, 0,
|
||||
];
|
||||
|
||||
const downSample = (cur, prev) => {
|
||||
const downSample = (cur:number, prev:number): number => {
|
||||
// no data, use previous value
|
||||
if (cur === null) return prev;
|
||||
// test for magnitude change, prev may be null but Math.abs(null) === 0 so this is valid
|
||||
@@ -41,20 +43,20 @@ const downSample = (cur, prev) => {
|
||||
return prev;
|
||||
};
|
||||
|
||||
const limits = [-99, -90, -80, -70, -60, -50, -40, -30, -20, -15, -10, -5, 0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 99,
|
||||
const limits:number[] = [-99, -90, -80, -70, -60, -50, -40, -30, -20, -15, -10, -5, 0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 99,
|
||||
];
|
||||
|
||||
const downSampleReset = null;
|
||||
|
||||
const maxDbzIndex = 12; // index of maximum dbz
|
||||
|
||||
const transparentIndex = 24;
|
||||
|
||||
module.exports = {
|
||||
const paletteData: PaletteData = {
|
||||
palette,
|
||||
downSample,
|
||||
limits,
|
||||
downSampleReset,
|
||||
maxDbzIndex,
|
||||
transparentIndex,
|
||||
maxDbzIndex: 23,
|
||||
};
|
||||
|
||||
export default paletteData;
|
||||
@@ -1,6 +1,7 @@
|
||||
const { createCanvas } = require('canvas');
|
||||
import { createCanvas, CanvasRenderingContext2D, Canvas } from 'canvas';
|
||||
import Palette from './palettes';
|
||||
|
||||
const palettizeImage = (sourceCtx, palette) => {
|
||||
const palettizeImage = (sourceCtx:CanvasRenderingContext2D, palette: Palette):Canvas => {
|
||||
// get the dimensions of the image from the ctx
|
||||
const dim = {
|
||||
x: sourceCtx.canvas.width,
|
||||
@@ -30,4 +31,4 @@ const palettizeImage = (sourceCtx, palette) => {
|
||||
return indexedCanvas;
|
||||
};
|
||||
|
||||
module.exports = palettizeImage;
|
||||
export default palettizeImage;
|
||||
@@ -1,5 +1,11 @@
|
||||
const { draw, canvas } = require('./draw');
|
||||
const { writePngToFile } = require('./utils/file');
|
||||
import { draw, Canvas } from './draw';
|
||||
import { writePngToFile } from './utils/file';
|
||||
import { DrawOptions } from './types';
|
||||
|
||||
interface Results {
|
||||
[key: string]: draw,
|
||||
}
|
||||
|
||||
/**
|
||||
* Plot level 2 data
|
||||
* @param {Level2Data} data output from the nexrad-level-2-data library
|
||||
@@ -12,9 +18,9 @@ const { writePngToFile } = require('./utils/file');
|
||||
* @param {(boolean|object)} [options.palettize] After drawing the image convert the image from RGBA to a palettized image. When true the same palette as the product is used.
|
||||
* @returns Canvas
|
||||
*/
|
||||
const plot = (data, _products, options) => {
|
||||
const plot = (data, _products:string|string[], options:DrawOptions) => {
|
||||
// store result
|
||||
const result = {};
|
||||
const result:Results = {};
|
||||
|
||||
let products;
|
||||
// make product list into an array
|
||||
@@ -44,5 +50,5 @@ const plot = (data, _products, options) => {
|
||||
module.exports = {
|
||||
plot,
|
||||
writePngToFile,
|
||||
canvas,
|
||||
Canvas,
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Canvas } from 'canvas';
|
||||
|
||||
export interface Plot {
|
||||
canvas: Canvas,
|
||||
palette?: Uint8ClampedArray,
|
||||
}
|
||||
|
||||
export interface PaletteData {
|
||||
palette: number[],
|
||||
limits: number[],
|
||||
transparentIndex?: number,
|
||||
downSampleReset?: number|null,
|
||||
downSample?: (cur:number, prev:number) => number,
|
||||
inDeadband?: () => boolean,
|
||||
maxDbzIndex: number;
|
||||
}
|
||||
|
||||
export interface DrawOptions {
|
||||
size: number,
|
||||
cropTo: number,
|
||||
background: string,
|
||||
lineWidth: number,
|
||||
product: string,
|
||||
elevation: number,
|
||||
palettize?: boolean,
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
const fs = require('fs');
|
||||
import { PngConfig } from 'canvas';
|
||||
import fs from 'fs';
|
||||
import { Plot } from '../types';
|
||||
|
||||
// write a canvas to a Png file
|
||||
/**
|
||||
* Write a Canvas to the specified file
|
||||
@@ -6,7 +9,8 @@ const fs = require('fs');
|
||||
* @param {Canvas} data Canvas provided by the plot() function
|
||||
* @returns Promise
|
||||
*/
|
||||
const writePngToFile = (fileName, data) => new Promise((resolve, reject) => {
|
||||
// eslint-disable-next-line import/prefer-default-export
|
||||
export const writePngToFile = (fileName: string, data: Plot):Promise<void> => new Promise<void>((resolve, reject) => {
|
||||
// the draw function will return false if no data was found, short circuit the output here
|
||||
if (!data.canvas) {
|
||||
resolve();
|
||||
@@ -17,15 +21,11 @@ const writePngToFile = (fileName, data) => new Promise((resolve, reject) => {
|
||||
const writeStream = fs.createWriteStream(fileName);
|
||||
|
||||
// options
|
||||
const options = {};
|
||||
const options:PngConfig = {};
|
||||
if (data.palette) options.palette = data.palette;
|
||||
|
||||
// write to the stream
|
||||
data.canvas.createPNGStream(options).pipe(writeStream);
|
||||
writeStream.on('finish', () => resolve(fileName));
|
||||
writeStream.on('error', (e) => reject(e));
|
||||
writeStream.on('finish', () => resolve());
|
||||
writeStream.on('error', (e: Error) => reject(e));
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
writePngToFile,
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"module": "commonjs",
|
||||
"allowJs": true,
|
||||
"checkJs": false,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"sourceMap": true,
|
||||
"noImplicitAny": true
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user