mirror of
https://github.com/netbymatt/nexrad-level-2-plot.git
synced 2026-11-28 18:28:05 -08:00
Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a6f27875e9 | ||
|
|
1ef5053ca0 | ||
|
|
ea4cb9de74 | ||
|
|
e3c882a57c | ||
|
|
aa4b02236a | ||
|
|
847d4cbecc | ||
|
|
7731d64af6 | ||
|
|
96d3f36545 | ||
|
|
6a7f0868a5 | ||
|
|
bed3043ccd | ||
|
|
676c63dc1c |
No files matched your search
@@ -16,10 +16,10 @@ const { plot, writeToPngFile } = require('nexrad-level-3-plot');
|
||||
// read a file
|
||||
const file = fs.readFileSync('<path to data>');
|
||||
// parse and plot
|
||||
const level2Plot = plot(file, {elevation: 1, product: 'REF'});
|
||||
const level2Plot = plot(file, 'REF', {elevations: 1});
|
||||
// use bundled utility to write to disk
|
||||
(async () => {
|
||||
await writePngToFile('<path to output>.png', level2Plot.REF.canvas);
|
||||
await writePngToFile('<path to output>.png', level2Plot[0].REF);
|
||||
})();
|
||||
```
|
||||
# Data
|
||||
@@ -56,7 +56,7 @@ Returns an array of objects. The order of elevations in the returned array match
|
||||
canvas: <Canvas>,
|
||||
palette: <Uint8ClampedArray>
|
||||
},
|
||||
//... additional products
|
||||
// ... additional products
|
||||
},
|
||||
// ... additional elevations
|
||||
]
|
||||
@@ -79,6 +79,9 @@ options.lineWidth|integer|2|The raster image is created by drawing several arcs
|
||||
options.palettize|boolean\|object|false|After drawing the image convert the image from RGBA to a palettized image. When true the same palette as the product is used. Additional options are described in [palettizing](#palettizing). This can significantly reduce the size of the resulting image with minimal loss of clarity.
|
||||
options.elevations|undefined\|integer\|Array of integers|undefined|The specific elevations to plot. If undefined (default) all available elevations will be plotted. If an integer is provided a single elevation will be plotted with all the selected ```products```. If an array of integers are provided each ```products``` in each elevation will be plotted. Note that when a product and elevation combination do not exist the returned object will have false in the missing location. This is common where elevation 1 will have reflectivity data and elevation 2 (altough at the same angle as elevation 1) will contain velocity data.
|
||||
options.usePreferredWaveforms|boolean|true|Waveform types of 1, 2, 3, 4 and 5 are produced by the radar. Type 1 is typically exclusive to reflectivity, Type 2 is typically exclusive to velocity however it also produces reflectivity data. Types 3, 4 and 5 all produce both velocity and reflectivity. When set to true type 1 waveforms will not plot velocity data, and type 2 waveforms will not plot reflectivity data. If you must see all information set this to false.
|
||||
options.alpha|boolean|true|Draw on a 32-bit canvas. Passed directly to getContext('2d', {alpha}).
|
||||
options.imageSmoothingEnabled|boolean|true|Enable image smoothing. Passed directly to ctx.imageSmoothingEnabled.
|
||||
options.antialias|string|'default'|Enable antialias. Passed directly to ctx.antialias (part of node-canvas)
|
||||
|
||||
### Downsampling
|
||||
A full size plot is 3600 x 3600 pixels. This corresponds to the maximum range of the radar 460km (~250 mi) * maximum resolution 0.25 mi/bin * 2 (east and west side of radar).
|
||||
@@ -114,6 +117,12 @@ data|{canvas[palette]}|Typically the output of [plot()](#plotfile-options).\<pro
|
||||
|
||||
# Notable and breaking changes
|
||||
|
||||
## v2.6.0 Notable, Additional canvas configuration options
|
||||
The options antialias, imageSmoothingEnabled, and alpha were added and passed directly to the canvas context when creating it before plotting. Disabling these options (which were previously defaulted to on) can speed up plotting by about 5% and reduce output image size by up to 10% at a very slight cost of visual appearance.
|
||||
|
||||
## v2.5.0 Notable, 300km range now interperted correctly
|
||||
Prior to this version, if the data had the ```super_res_control.300km``` flag set the resulting image would effectively be "zoomed in by 2x". Upon examing the results of this flag on the plot, reference plots and the ```gate_count``` values it was determined no scaling needed to be performed if this flag was present. Instead, the ```gate_count``` already reflects the additional data the flag indicates. This mainly affects VEL plots and some REF plots at higher elevations, depending on the VCP the radar is using.
|
||||
|
||||
## v2.0.0 Breaking, returns an array of objects
|
||||
Previously, a single elevation would be plotted by the routine. The elevation was selected by ```options.elevation```. ```options.elevation``` has been renamed to ```options.elevations``` and will still take a single integer as an input, but in this configuration an array is still returned. The simplest way to return to the functionality of v1.1.0 is ```plot(data,products,{elevation: <integer>})[0]``` which will return only the first object in the array.
|
||||
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ if (single) {
|
||||
const plots = plot(radarData, ['REF', 'VEL'], {
|
||||
elevations,
|
||||
size,
|
||||
palettize: false,
|
||||
palettize: true,
|
||||
cropTo: size / 2,
|
||||
});
|
||||
|
||||
|
||||
Generated
+798
-271
File diff suppressed because it is too large.
Load diff
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "nexrad-level-2-plot",
|
||||
"version": "2.2.1",
|
||||
"version": "2.6.0",
|
||||
"description": "Plotting library for nexrad-level-2-data",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
@@ -24,7 +24,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
|
||||
"dependencies": {
|
||||
"@napi-rs/canvas": "^0.1.21",
|
||||
"canvas": "^2.9.1",
|
||||
"nexrad-level-2-data": "^2.3.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
+16
-5
@@ -1,4 +1,4 @@
|
||||
const canvasObj = require('@napi-rs/canvas');
|
||||
const canvasObj = require('canvas');
|
||||
|
||||
const { createCanvas } = canvasObj;
|
||||
|
||||
@@ -51,6 +51,9 @@ const DEFAULT_OPTIONS = {
|
||||
background: 'black',
|
||||
lineWidth: 2,
|
||||
usePreferredWaveforms: true,
|
||||
alpha: true,
|
||||
imageSmoothingEnabled: true,
|
||||
antialias: 'default',
|
||||
};
|
||||
|
||||
const draw = (data, _options) => {
|
||||
@@ -73,13 +76,21 @@ const draw = (data, _options) => {
|
||||
if (options.size < 1) throw new Error('Provide options.size > 0');
|
||||
const scale = DEFAULT_OPTIONS.size / options.size;
|
||||
|
||||
// wsr88d uses a gate size of 0.25km, tdwr uses a gate size of 0.15km, although it is reported as 0.3km for processing reasons
|
||||
// this calculation scales the plot accordingly to the nominal 0.25km so all generated plots are at the same scale
|
||||
const rawGateSize = data?.data?.[options.elevation]?.[0]?.record?.reflect?.gate_size ?? 0.25;
|
||||
const realGateSize = rawGateSize !== 0.3 ? rawGateSize : rawGateSize / 2;
|
||||
const gateSizeScaling = 0.25 / realGateSize;
|
||||
|
||||
// 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');
|
||||
const ctx = canvas.getContext('2d', { alpha: options.alpha });
|
||||
ctx.antialias = options.antialias;
|
||||
ctx.imageSmoothingEnabled = options.imageSmoothingEnabled;
|
||||
|
||||
// fill background with black
|
||||
ctx.fillStyle = options.background;
|
||||
@@ -87,7 +98,7 @@ const draw = (data, _options) => {
|
||||
|
||||
// canvas settings
|
||||
ctx.imageSmoothingEnabled = true;
|
||||
ctx.lineWidth = options.lineWidth;
|
||||
ctx.lineWidth = options.lineWidth / gateSizeScaling;
|
||||
ctx.translate(cropTo / 2, cropTo / 2);
|
||||
ctx.rotate(-Math.PI / 2);
|
||||
|
||||
@@ -141,11 +152,11 @@ const draw = (data, _options) => {
|
||||
if (bin.count) {
|
||||
// rrle encoded
|
||||
ctx.strokeStyle = palette.lookupRgba[bin.value];
|
||||
ctx.arc(0, 0, (idx + deadZone), startAngle, endAngle + resolution * (bin.count - 1));
|
||||
ctx.arc(0, 0, (idx + deadZone) * gateSizeScaling, startAngle, endAngle + resolution * (bin.count - 1));
|
||||
} else {
|
||||
// plain data
|
||||
ctx.strokeStyle = palette.lookupRgba[bin];
|
||||
ctx.arc(0, 0, (idx + deadZone), startAngle, endAngle);
|
||||
ctx.arc(0, 0, (idx + deadZone) * gateSizeScaling, startAngle, endAngle);
|
||||
}
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
@@ -11,11 +11,9 @@ class Palette {
|
||||
this.palette = palette.palette;
|
||||
this.limits = palette.limits;
|
||||
this.transparentIndex = palette.transparentIndex;
|
||||
this.transparentValue = this.indexToValue(this.transparentIndex);
|
||||
this.lookupRgba = findDbzRbgaGenerator(this.limits, this.palette, palette.maxDbzIndex);
|
||||
this.lookupIndex = findDbzIndexGenerator(this.limits, this.palette, palette.maxDbzIndex);
|
||||
this.closestIndexCache = {};
|
||||
this.closestValueCache = {};
|
||||
this.closest = {};
|
||||
|
||||
// these may not exist and are overridden with default functions
|
||||
if (palette.hasOwnProperty('downSampleReset')) {
|
||||
@@ -62,7 +60,7 @@ class Palette {
|
||||
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.closestIndexCache[asHex]) return this.closestIndexCache[asHex];
|
||||
if (this.closest[asHex]) return this.closest[asHex];
|
||||
// initial conditions
|
||||
let closestIndex = 0;
|
||||
let closest = Infinity;
|
||||
@@ -77,29 +75,10 @@ class Palette {
|
||||
}
|
||||
}
|
||||
// store closest match to speed up next iteration
|
||||
this.closestIndexCache[asHex] = closestIndex;
|
||||
this.closest[asHex] = closestIndex;
|
||||
return closestIndex;
|
||||
}
|
||||
|
||||
// match = [r,g,b[,a]]
|
||||
closestValue(match) {
|
||||
// short circuit for transparent (black)
|
||||
if (match[0] <= 2 && match[1] <= 2 && match[2] <= 2) return this.transparentValue;
|
||||
// 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.closestValueCache[asHex]) return this.closestValueCache[asHex];
|
||||
const closest = this.closestIndex(match);
|
||||
const closestAsValue = this.indexToValue(closest);
|
||||
this.closestValueCache[asHex] = closestAsValue;
|
||||
|
||||
return closestAsValue;
|
||||
}
|
||||
|
||||
// index to value
|
||||
indexToValue(index) {
|
||||
return this.palette.slice(index * 4, index * 4 + 4);
|
||||
}
|
||||
|
||||
// geometric distance
|
||||
static geometricDistance(a, b) {
|
||||
return a.reduce((acc, val, idx) => acc + (val - b[idx]) ** 2, 0);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const { createCanvas } = require('@napi-rs/canvas');
|
||||
const { createCanvas } = require('canvas');
|
||||
|
||||
const palettizeImage = (sourceCtx, palette) => {
|
||||
// get the dimensions of the image from the ctx
|
||||
@@ -10,7 +10,7 @@ const palettizeImage = (sourceCtx, palette) => {
|
||||
// transform into indexed palette by finding the closest indexed color for each pixel
|
||||
// create a destination image that is transparent and indexed
|
||||
const indexedCanvas = createCanvas(dim.x, dim.y);
|
||||
const indexedCtx = indexedCanvas.getContext('2d');
|
||||
const indexedCtx = indexedCanvas.getContext('2d', { pixelFormat: 'A8' });
|
||||
|
||||
// get the source image data
|
||||
const sourceImageData = sourceCtx.getImageData(0, 0, dim.x, dim.y);
|
||||
@@ -18,13 +18,9 @@ const palettizeImage = (sourceCtx, palette) => {
|
||||
const indexedImageData = indexedCtx.getImageData(0, 0, dim.x, dim.y);
|
||||
|
||||
// loop through each pixel
|
||||
for (let i = 0; i < indexedImageData.data.length; i += 4) {
|
||||
const [r, g, b, a] = palette.closestValue(sourceImageData.data.slice(i, i + 3));
|
||||
indexedImageData.data[i] = r;
|
||||
indexedImageData.data[i + 1] = g;
|
||||
indexedImageData.data[i + 2] = b;
|
||||
indexedImageData.data[i + 3] = a;
|
||||
}
|
||||
indexedImageData.data.forEach((val, idx) => {
|
||||
indexedImageData.data[idx] = palette.closestIndex(sourceImageData.data.slice(idx * 4, idx * 4 + 3));
|
||||
});
|
||||
|
||||
// write the new image data
|
||||
indexedCtx.putImageData(indexedImageData, 0, 0);
|
||||
|
||||
@@ -12,6 +12,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.
|
||||
* @param {(undefined|number|number[])} [options.elevations=undefined] List of elevations to plot.
|
||||
* @param {boolean} [options.usePreferredWaveforms = true] Limit the plotted data to the preferred use of waveforms 1 and 2.
|
||||
* @param {boolean} [options.alpha = true] Draw on a 32-bit canvas. Passed directly to getContext('2d', {alpha}).
|
||||
* @param {boolean} [options.imageSmoothingEnabled = true] Control image smoothing. Passed directly to ctx.imageSmoothingEnabled.
|
||||
* @param {string} [options.antialias = 'default'] Control antialias. Passed directly to ctx.antialias (part of node-canvas)
|
||||
* @returns Canvas
|
||||
*/
|
||||
const plot = (data, _products, options) => {
|
||||
|
||||
+10
-4
@@ -1,4 +1,4 @@
|
||||
const fs = require('fs/promises');
|
||||
const fs = require('fs');
|
||||
// write a canvas to a Png file
|
||||
/**
|
||||
* Write a Canvas to the specified file
|
||||
@@ -6,19 +6,25 @@ const fs = require('fs/promises');
|
||||
* @param {Canvas} data Canvas provided by the plot() function
|
||||
* @returns Promise
|
||||
*/
|
||||
const writePngToFile = async (fileName, data) => {
|
||||
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
|
||||
await fs.writeFile(fileName, await data.canvas.encode('webp', 100));
|
||||
};
|
||||
data.canvas.createPNGStream(options).pipe(writeStream);
|
||||
writeStream.on('finish', () => resolve(fileName));
|
||||
writeStream.on('error', (e) => reject(e));
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
writePngToFile,
|
||||
|
||||
Reference in new issue
Block a user