Compare commits

..
1 Commits
Author SHA1 Message Date
Matt Walsh dcc07e5bf7 add non-hi-res plot 2022-01-30 16:53:19 -06:00
10 changed files with 3897 additions and 4039 deletions

No files matched your search

+7 -18
View File
@@ -47,19 +47,15 @@ Test code and data is provided in the `./demo` folder. `test.js` can be used to
# API
## plot(data, products, options)
Returns an array of objects. The order of elevations in the returned array matches the order of elevations in options.elevations
Returns an object
``` javascript
[
{
elevation: <integer>,
REF: {
canvas: <Canvas>,
palette: <Uint8ClampedArray>
},
//... additional products
{
REF: {
canvas: <Canvas>,
palette: <Uint8ClampedArray>
},
// ... additional elevations
]
//... additional products
}
```
The first level key in the returned object represents each requested product.
> Note: Each product may return false if no data was found for the specified elevation and product.
@@ -77,8 +73,6 @@ options.cropTo|integer|3600|1 to 3600. After scaling and downsampling as describ
options.background|string|#000000|Background color of the image. This can be transparent by using #RGBA notation. See [ctx.fillStyle](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle) for more information.
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).
@@ -114,11 +108,6 @@ data|{canvas[palette]}|Typically the output of [plot()](#plotfile-options).\<pro
# Notable and breaking changes
## 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.
```options.elevations``` defaults to all the elevations in the provided file. This may not be the desired behavior, and will take significantly longer to plot if you do not need all of the data provided. You can query ```data.listElevations()``` to return an array of available elevations. The result can be passed directly to options.elevations or further processed to only select interesting elevations before calling ```plot()```. Additional details of ```options.elevations``` are provided above.
## v1.1.0 Notable, RRLE pre-processing
Testing and monitoring of this package in production showed that drawing each 0.5&deg; or 1.0&deg; arc as part of the plotting process was very time-expensive. Drawing a wider arc when adjacent radials had the same color at this position was significantly faster than drawing the two separate arcs.
Binary file not shown.
+10 -9
View File
@@ -1,4 +1,3 @@
/* 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');
@@ -21,26 +20,28 @@ files.forEach((file) => {
// combine data
const radarData = Level2Radar.combineData(chunks);
// result array by elevations
const plots = [];
const size = 1800;
const elevation = 1;
// plot for each elevation and size
(async () => {
const start = new Date();
for (let i = 0; i < 100; i += 1) {
const plots = plot(radarData, ['REF', 'VEL'], {
plots[elevation] = plot(radarData, ['REF', 'VEL'], {
elevation,
size,
palettize: true,
cropTo: size / 2,
});
// write files to disk
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);
await Promise.allSettled([
writePngToFile(`./output/REF-${elevation}-${size}--${i}.png`, plots[elevation].REF),
writePngToFile(`./output/VEL-${elevation}-${size}--${i}.png`, plots[elevation].VEL),
]);
}
const end = new Date();
console.log(`Total Time: ${(end - start) / 1000}`);
+17 -14
View File
@@ -14,6 +14,7 @@ const { plot, writePngToFile } = require('../src');
// 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 files = glob.sync('./data/non-hi-res/*');
// const store each file's data
const chunks = [];
@@ -27,6 +28,9 @@ files.forEach((file) => {
// 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');
@@ -43,20 +47,19 @@ if (single) {
// plot for each elevation and size
(async () => {
await Promise.allSettled(sizes.map(async (size) => {
const plots = plot(radarData, ['REF', 'VEL'], {
elevations,
size,
palettize: true,
cropTo: size / 2,
});
await Promise.allSettled(elevations.map(async (elevation) => {
plots[elevation] = plot(radarData, ['REF', 'VEL'], {
elevation,
size,
palettize: true,
cropTo: size / 2,
});
// write files to disk
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);
// 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),
]);
}));
}));
})();
-8
View File
@@ -1,8 +0,0 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}
+3777 -3897
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.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": {
"canvas": "^2.9.1",
"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"
}
"name": "nexrad-level-2-plot",
"version": "1.1.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": {
"canvas": "^2.8.0",
"nexrad-level-2-data": "file:../nexrad-level-2-data"
},
"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"
}
}
+20 -34
View File
@@ -35,14 +35,6 @@ 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
@@ -50,7 +42,6 @@ const DEFAULT_OPTIONS = {
cropTo: 3600,
background: 'black',
lineWidth: 2,
usePreferredWaveforms: true,
};
const draw = (data, _options) => {
@@ -60,14 +51,6 @@ 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');
@@ -77,20 +60,6 @@ const draw = (data, _options) => {
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
@@ -124,10 +93,27 @@ const draw = (data, _options) => {
const indexedProduct = indexProduct(downSampledProduct, palette);
const rrlEncoded = rrle(indexedProduct, resolution);
// 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);
// calculate a gate size against the nominal size
const gateScale = rrlEncoded[0].gate_size / 0.25;
// canvas settings
ctx.imageSmoothingEnabled = true;
ctx.lineWidth = options.lineWidth * gateScale;
ctx.translate(cropTo / 2, cropTo / 2);
ctx.rotate(-Math.PI / 2);
// loop through data
rrlEncoded.forEach((radial) => {
// calculate plotting parameters
const deadZone = radial.first_gate / radial.gate_size / scale;
const deadZone = radial.first_gate / radial.gate_size / scale * gateScale;
// 10% is added to the arc to ensure that each arc bleeds into the next just slightly to avoid radial empty spaces at further distances
const startAngle = radial.azimuth * (Math.PI / 180) - halfResolution * 1.1;
@@ -141,11 +127,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) * gateScale, 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) * gateScale, startAngle, endAngle);
}
ctx.stroke();
}
+21 -1
View File
@@ -5,7 +5,27 @@
const filterProduct = (data, product) => data.map((header) => {
// get correct data
const thisRadial = header[product];
let thisRadial = header[product];
// special case for non-hi-res data
// re-formats data to expected format
if (!thisRadial.moment_data && thisRadial) {
thisRadial = {
...header,
moment_data: header[product],
};
if (product === 'refelect') {
thisRadial.gate_size = header.surveillance_range_sample_interval;
thisRadial.gate_count = header.number_of_surveillance_bins;
} else {
thisRadial.gate_size = header.doppler_range_sample_interval;
thisRadial.gate_count = header.number_of_doppler_bins;
}
thisRadial.first_gate = 2;
}
// skip if this radial isn't found
if (thisRadial === undefined) return false;
thisRadial.azimuth = header.azimuth;
+8 -21
View File
@@ -10,13 +10,11 @@ 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) => {
// store result
const result = [];
const result = {};
let products;
// make product list into an array
@@ -30,27 +28,16 @@ const plot = (data, _products, options) => {
}
// get the available elevations
const availableElevations = data.listElevations();
if (availableElevations.length === 0) throw new Error('No elevations availabe');
const elevations = data.listElevations();
if (elevations.length === 0) throw new Error('No elevations availabe');
// handle options.elevations defaults
let elevations = options?.elevations;
if (!elevations) elevations = availableElevations;
if (typeof elevations === 'number') elevations = [elevations];
elevations.forEach((elevation) => {
const elevationResult = { elevation };
products.forEach((product) => {
// parse and store result
elevationResult[product] = draw(data, {
...options,
elevation,
product,
});
products.forEach((product) => {
// parse and store result
result[product] = draw(data, {
...options,
product,
});
result.push(elevationResult);
});
return result;
};