Compare commits

...
14 Commits
Author SHA1 Message Date
Matt Walsh 0982b90faa palletize 2022-05-13 12:09:02 -05:00
Matt Walsh 4a2cf1d993 change to @napi-rs/canvas 2022-05-13 11:55:11 -05:00
Matt Walsh e559ba1d9c better benchmark statistics 2022-05-13 10:28:26 -05:00
Matt Walsh febb902f15 2.2.1 2022-05-12 22:02:18 -05:00
Matt Walsh aa307c44c0 update nexrad-level-2-data version 2022-05-12 22:01:57 -05:00
Matt Walsh a9fd62a513 update dependencies 2022-05-12 16:02:22 -05:00
Matt Walsh c12f6bbb95 update dependencies 2022-03-30 16:16:09 -05:00
Matt Walsh 28cfd34bcd 2.2.0 2022-03-18 15:57:32 -05:00
Matt Walsh a3987914bc update nexrad-level-2-data dependency 2022-03-18 15:56:38 -05:00
Matt Walsh 2b8fbbc3b5 2.1.1 2022-03-11 13:31:01 -06:00
Matt Walsh cab1556cc4 disable preferred products for chunk mode 2022-03-11 13:30:51 -06:00
Matt Walsh 2017a6f4ff fix benchmark code 2022-03-11 09:53:14 -06:00
Matt Walsh b8389de11f 2.1.0 2022-03-11 09:13:12 -06:00
Matt Walsh dc02fe5f50 add preferred waveforms 2022-03-11 09:13:05 -06:00
11 changed files with 3488 additions and 3869 deletions

No files matched your search

+1
View File
@@ -78,6 +78,7 @@ options.background|string|#000000|Background color of the image. This can be tra
options.lineWidth|integer|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.
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.
### 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).
+14 -13
View File
@@ -1,3 +1,4 @@
/* eslint-disable no-await-in-loop */
const { Level2Radar } = require('nexrad-level-2-data');
// eslint-disable-next-line import/no-extraneous-dependencies
const glob = require('glob');
@@ -20,29 +21,29 @@ files.forEach((file) => {
// combine data
const radarData = Level2Radar.combineData(chunks);
// result array by elevations
const plots = [];
const size = 1800;
const elevation = 1;
const iterations = 10;
// plot for each elevation and size
(async () => {
const start = new Date();
for (let i = 0; i < 100; i += 1) {
plots[elevation] = plot(radarData, ['REF', 'VEL'], {
elevation,
for (let i = 0; i < iterations; i += 1) {
const plots = plot(radarData, ['REF', 'VEL'], {
size,
palettize: true,
cropTo: size / 2,
});
// write files to disk
await Promise.allSettled([
writePngToFile(`./output/REF-${elevation}-${size}--${i}.png`, plots[elevation].REF),
writePngToFile(`./output/VEL-${elevation}-${size}--${i}.png`, plots[elevation].VEL),
]);
const writePromises = [];
plots.forEach((p) => {
const { elevation } = p;
writePromises.push(writePngToFile(`./output/REF-${elevation}-${size}.png`, p.REF));
writePromises.push(writePngToFile(`./output/VEL-${elevation}-${size}.png`, p.VEL));
});
await Promise.allSettled(writePromises);
const end = new Date();
console.log(`Iteration: ${i + 1}/${iterations} Average time: ${(end - start) / 1000 / (i + 1)} s`);
}
const end = new Date();
console.log(`Total Time: ${(end - start) / 1000}`);
})();
+1 -1
View File
@@ -46,7 +46,7 @@ if (single) {
const plots = plot(radarData, ['REF', 'VEL'], {
elevations,
size,
palettize: true,
palettize: false,
cropTo: size / 2,
});
+8
View File
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}
+3370 -3799
View File
File diff suppressed because it is too large. Load diff
+37 -37
View File
@@ -1,39 +1,39 @@
{
"name": "nexrad-level-2-plot",
"version": "2.0.0",
"description": "Plotting library for nexrad-level-2-data",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/netbymatt/nexrad-level-2-plot.git"
},
"keywords": [
"NEXRAD",
"level",
"2",
"radar",
"plot"
],
"author": "Matt Walsh",
"license": "MIT",
"bugs": {
"url": "https://github.com/netbymatt/nexrad-level-2-plot/issues"
},
"homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
"dependencies": {
"canvas": "^2.8.0",
"nexrad-level-2-data": "^2.1.0"
},
"devDependencies": {
"eslint": "^8.0.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.23.4",
"glob": "^7.1.7"
},
"engines": {
"node": ">=13.0.0"
}
"name": "nexrad-level-2-plot",
"version": "2.2.1",
"description": "Plotting library for nexrad-level-2-data",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/netbymatt/nexrad-level-2-plot.git"
},
"keywords": [
"NEXRAD",
"level",
"2",
"radar",
"plot"
],
"author": "Matt Walsh",
"license": "MIT",
"bugs": {
"url": "https://github.com/netbymatt/nexrad-level-2-plot/issues"
},
"homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
"dependencies": {
"@napi-rs/canvas": "^0.1.21",
"nexrad-level-2-data": "^2.3.5"
},
"devDependencies": {
"eslint": "^8.0.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.23.4",
"glob": "^7.1.7"
},
"engines": {
"node": ">=13.0.0"
}
}
+18 -1
View File
@@ -1,4 +1,4 @@
const canvasObj = require('canvas');
const canvasObj = require('@napi-rs/canvas');
const { createCanvas } = canvasObj;
@@ -35,6 +35,14 @@ const palettes = {
};
/* eslint-enable global-require */
const preferredWaveformUsage = {
1: ['REF', 'SW ', 'ZDR', 'PHI', 'RHO'],
2: ['VEL'],
3: ['REF', 'VEL', 'SW ', 'ZDR', 'PHI', 'RHO'],
4: ['REF', 'VEL', 'SW ', 'ZDR', 'PHI', 'RHO'],
5: ['REF', 'VEL', 'SW ', 'ZDR', 'PHI', 'RHO'],
};
// default options
const DEFAULT_OPTIONS = {
// must be a square image
@@ -42,6 +50,7 @@ const DEFAULT_OPTIONS = {
cropTo: 3600,
background: 'black',
lineWidth: 2,
usePreferredWaveforms: true,
};
const draw = (data, _options) => {
@@ -51,6 +60,14 @@ const draw = (data, _options) => {
..._options,
};
// check preferred waveforms
const elevationInfo = data?.vcp?.record?.elevations?.[options?.elevation];
// elevation info is not available in chunks mode, so preferred waveforms cannot be processed
if (elevationInfo) {
const preferredProducts = preferredWaveformUsage[elevationInfo.waveform_type];
if (options.usePreferredWaveforms && !preferredProducts.includes(options.product)) return false;
}
// 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');
+24 -3
View File
@@ -11,9 +11,11 @@ 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.closest = {};
this.closestIndexCache = {};
this.closestValueCache = {};
// these may not exist and are overridden with default functions
if (palette.hasOwnProperty('downSampleReset')) {
@@ -60,7 +62,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.closest[asHex]) return this.closest[asHex];
if (this.closestIndexCache[asHex]) return this.closestIndexCache[asHex];
// initial conditions
let closestIndex = 0;
let closest = Infinity;
@@ -75,10 +77,29 @@ class Palette {
}
}
// store closest match to speed up next iteration
this.closest[asHex] = closestIndex;
this.closestIndexCache[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);
+9 -5
View File
@@ -1,4 +1,4 @@
const { createCanvas } = require('canvas');
const { createCanvas } = require('@napi-rs/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', { pixelFormat: 'A8' });
const indexedCtx = indexedCanvas.getContext('2d');
// get the source image data
const sourceImageData = sourceCtx.getImageData(0, 0, dim.x, dim.y);
@@ -18,9 +18,13 @@ const palettizeImage = (sourceCtx, palette) => {
const indexedImageData = indexedCtx.getImageData(0, 0, dim.x, dim.y);
// loop through each pixel
indexedImageData.data.forEach((val, idx) => {
indexedImageData.data[idx] = palette.closestIndex(sourceImageData.data.slice(idx * 4, idx * 4 + 3));
});
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;
}
// write the new image data
indexedCtx.putImageData(indexedImageData, 0, 0);
+2
View File
@@ -10,6 +10,8 @@ const { writePngToFile } = require('./utils/file');
* @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.
* @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.
* @returns Canvas
*/
const plot = (data, _products, options) => {
+4 -10
View File
@@ -1,4 +1,4 @@
const fs = require('fs');
const fs = require('fs/promises');
// write a canvas to a Png file
/**
* Write a Canvas to the specified file
@@ -6,25 +6,19 @@ const fs = require('fs');
* @param {Canvas} data Canvas provided by the plot() function
* @returns Promise
*/
const writePngToFile = (fileName, data) => new Promise((resolve, reject) => {
const writePngToFile = async (fileName, data) => {
// 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(fileName));
writeStream.on('error', (e) => reject(e));
});
await fs.writeFile(fileName, await data.canvas.encode('webp', 100));
};
module.exports = {
writePngToFile,