mirror of
https://github.com/netbymatt/nexrad-level-2-plot.git
synced 2026-11-29 02:38:05 -08:00
Compare commits
6
Commits
docs
...
canvas-change
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0982b90faa | ||
|
|
4a2cf1d993 | ||
|
|
e559ba1d9c | ||
|
|
febb902f15 | ||
|
|
aa307c44c0 | ||
|
|
a9fd62a513 |
No files matched your search
+5
-3
@@ -22,11 +22,12 @@ files.forEach((file) => {
|
|||||||
const radarData = Level2Radar.combineData(chunks);
|
const radarData = Level2Radar.combineData(chunks);
|
||||||
|
|
||||||
const size = 1800;
|
const size = 1800;
|
||||||
|
const iterations = 10;
|
||||||
|
|
||||||
// plot for each elevation and size
|
// plot for each elevation and size
|
||||||
(async () => {
|
(async () => {
|
||||||
const start = new Date();
|
const start = new Date();
|
||||||
for (let i = 0; i < 100; i += 1) {
|
for (let i = 0; i < iterations; i += 1) {
|
||||||
const plots = plot(radarData, ['REF', 'VEL'], {
|
const plots = plot(radarData, ['REF', 'VEL'], {
|
||||||
size,
|
size,
|
||||||
palettize: true,
|
palettize: true,
|
||||||
@@ -41,7 +42,8 @@ const size = 1800;
|
|||||||
writePromises.push(writePngToFile(`./output/VEL-${elevation}-${size}.png`, p.VEL));
|
writePromises.push(writePngToFile(`./output/VEL-${elevation}-${size}.png`, p.VEL));
|
||||||
});
|
});
|
||||||
await Promise.allSettled(writePromises);
|
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
@@ -46,7 +46,7 @@ if (single) {
|
|||||||
const plots = plot(radarData, ['REF', 'VEL'], {
|
const plots = plot(radarData, ['REF', 'VEL'], {
|
||||||
elevations,
|
elevations,
|
||||||
size,
|
size,
|
||||||
palettize: true,
|
palettize: false,
|
||||||
cropTo: size / 2,
|
cropTo: size / 2,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"path": "."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"settings": {}
|
||||||
|
}
|
||||||
Generated
+3370
-3779
File diff suppressed because it is too large.
Load diff
+37
-37
@@ -1,39 +1,39 @@
|
|||||||
{
|
{
|
||||||
"name": "nexrad-level-2-plot",
|
"name": "nexrad-level-2-plot",
|
||||||
"version": "2.2.0",
|
"version": "2.2.1",
|
||||||
"description": "Plotting library for nexrad-level-2-data",
|
"description": "Plotting library for nexrad-level-2-data",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/netbymatt/nexrad-level-2-plot.git"
|
"url": "git+https://github.com/netbymatt/nexrad-level-2-plot.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"NEXRAD",
|
"NEXRAD",
|
||||||
"level",
|
"level",
|
||||||
"2",
|
"2",
|
||||||
"radar",
|
"radar",
|
||||||
"plot"
|
"plot"
|
||||||
],
|
],
|
||||||
"author": "Matt Walsh",
|
"author": "Matt Walsh",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/netbymatt/nexrad-level-2-plot/issues"
|
"url": "https://github.com/netbymatt/nexrad-level-2-plot/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
|
"homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"canvas": "^2.8.0",
|
"@napi-rs/canvas": "^0.1.21",
|
||||||
"nexrad-level-2-data": "^2.1.0"
|
"nexrad-level-2-data": "^2.3.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.0.0",
|
"eslint": "^8.0.0",
|
||||||
"eslint-config-airbnb-base": "^15.0.0",
|
"eslint-config-airbnb-base": "^15.0.0",
|
||||||
"eslint-plugin-import": "^2.23.4",
|
"eslint-plugin-import": "^2.23.4",
|
||||||
"glob": "^7.1.7"
|
"glob": "^7.1.7"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=13.0.0"
|
"node": ">=13.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
const canvasObj = require('canvas');
|
const canvasObj = require('@napi-rs/canvas');
|
||||||
|
|
||||||
const { createCanvas } = canvasObj;
|
const { createCanvas } = canvasObj;
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ class Palette {
|
|||||||
this.palette = palette.palette;
|
this.palette = palette.palette;
|
||||||
this.limits = palette.limits;
|
this.limits = palette.limits;
|
||||||
this.transparentIndex = palette.transparentIndex;
|
this.transparentIndex = palette.transparentIndex;
|
||||||
|
this.transparentValue = this.indexToValue(this.transparentIndex);
|
||||||
this.lookupRgba = findDbzRbgaGenerator(this.limits, this.palette, palette.maxDbzIndex);
|
this.lookupRgba = findDbzRbgaGenerator(this.limits, this.palette, palette.maxDbzIndex);
|
||||||
this.lookupIndex = findDbzIndexGenerator(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
|
// these may not exist and are overridden with default functions
|
||||||
if (palette.hasOwnProperty('downSampleReset')) {
|
if (palette.hasOwnProperty('downSampleReset')) {
|
||||||
@@ -60,7 +62,7 @@ class Palette {
|
|||||||
if (match[0] <= 2 && match[1] <= 2 && match[2] <= 2) return this.transparentIndex;
|
if (match[0] <= 2 && match[1] <= 2 && match[2] <= 2) return this.transparentIndex;
|
||||||
// short circuit previously calculated matches
|
// 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');
|
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
|
// initial conditions
|
||||||
let closestIndex = 0;
|
let closestIndex = 0;
|
||||||
let closest = Infinity;
|
let closest = Infinity;
|
||||||
@@ -75,10 +77,29 @@ class Palette {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// store closest match to speed up next iteration
|
// store closest match to speed up next iteration
|
||||||
this.closest[asHex] = closestIndex;
|
this.closestIndexCache[asHex] = closestIndex;
|
||||||
return 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
|
// geometric distance
|
||||||
static geometricDistance(a, b) {
|
static geometricDistance(a, b) {
|
||||||
return a.reduce((acc, val, idx) => acc + (val - b[idx]) ** 2, 0);
|
return a.reduce((acc, val, idx) => acc + (val - b[idx]) ** 2, 0);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const { createCanvas } = require('canvas');
|
const { createCanvas } = require('@napi-rs/canvas');
|
||||||
|
|
||||||
const palettizeImage = (sourceCtx, palette) => {
|
const palettizeImage = (sourceCtx, palette) => {
|
||||||
// get the dimensions of the image from the ctx
|
// 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
|
// transform into indexed palette by finding the closest indexed color for each pixel
|
||||||
// create a destination image that is transparent and indexed
|
// create a destination image that is transparent and indexed
|
||||||
const indexedCanvas = createCanvas(dim.x, dim.y);
|
const indexedCanvas = createCanvas(dim.x, dim.y);
|
||||||
const indexedCtx = indexedCanvas.getContext('2d', { pixelFormat: 'A8' });
|
const indexedCtx = indexedCanvas.getContext('2d');
|
||||||
|
|
||||||
// get the source image data
|
// get the source image data
|
||||||
const sourceImageData = sourceCtx.getImageData(0, 0, dim.x, dim.y);
|
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);
|
const indexedImageData = indexedCtx.getImageData(0, 0, dim.x, dim.y);
|
||||||
|
|
||||||
// loop through each pixel
|
// loop through each pixel
|
||||||
indexedImageData.data.forEach((val, idx) => {
|
for (let i = 0; i < indexedImageData.data.length; i += 4) {
|
||||||
indexedImageData.data[idx] = palette.closestIndex(sourceImageData.data.slice(idx * 4, idx * 4 + 3));
|
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
|
// write the new image data
|
||||||
indexedCtx.putImageData(indexedImageData, 0, 0);
|
indexedCtx.putImageData(indexedImageData, 0, 0);
|
||||||
|
|||||||
+4
-10
@@ -1,4 +1,4 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs/promises');
|
||||||
// write a canvas to a Png file
|
// write a canvas to a Png file
|
||||||
/**
|
/**
|
||||||
* Write a Canvas to the specified 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
|
* @param {Canvas} data Canvas provided by the plot() function
|
||||||
* @returns Promise
|
* @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
|
// the draw function will return false if no data was found, short circuit the output here
|
||||||
if (!data.canvas) {
|
if (!data.canvas) {
|
||||||
resolve();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get a write stream
|
|
||||||
const writeStream = fs.createWriteStream(fileName);
|
|
||||||
|
|
||||||
// options
|
// options
|
||||||
const options = {};
|
const options = {};
|
||||||
if (data.palette) options.palette = data.palette;
|
if (data.palette) options.palette = data.palette;
|
||||||
|
|
||||||
// write to the stream
|
// write to the stream
|
||||||
data.canvas.createPNGStream(options).pipe(writeStream);
|
await fs.writeFile(fileName, await data.canvas.encode('webp', 100));
|
||||||
writeStream.on('finish', () => resolve(fileName));
|
};
|
||||||
writeStream.on('error', (e) => reject(e));
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
writePngToFile,
|
writePngToFile,
|
||||||
|
|||||||
Reference in new issue
Block a user