mirror of
https://github.com/netbymatt/nexrad-level-2-plot.git
synced 2026-11-28 10:28:04 -08:00
correct max size and add crop to
This commit is contained in:
1 parent
02beb2720c
commit
8f25b5beee
3 files changed
+53
-37
No files matched your search
@@ -68,17 +68,20 @@ The first level key in the returned object represents each requested product.
|
||||
|---|---|---|---|
|
||||
data|Level2Radar||Output from [nexrad-level-2-data](https://github.com/netbymatt/nexrad-level-2-data/).
|
||||
products|Array of strings|All|Individual strings as shown in supported products to specify the products to be plotted.
|
||||
options.size|integer|1800|1 to 1800. Size of the x and y axis in pixels. The image must be square so only a single integer is needed. See [downsampling](#downsampling)
|
||||
options.size|integer|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. See [downsampling](#downsampling)
|
||||
options.cropTo|integer|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.
|
||||
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.
|
||||
|
||||
### Downsampling
|
||||
A full size plot is 1800 x 1800 pixels. This corresponds to the maximum range of the radar ~250 mi * maximum resolution 0.25 mi/bin * 2 (east and west side of radar).
|
||||
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).
|
||||
|
||||
options.size < 1800 will internally scale the image to the selected size. The scaling algorithm is specific to radar data and returns the maximum value over the range of bins that are combined due to the scaling factor. This ensures that maximum reflectivity or maximum velocity are preserved during the scaling process. Using an image scaling package is not preferred in this case as the scaling algorithm used my mask important data.
|
||||
options.size < 3600 will internally scale the image to the selected size. The scaling algorithm is specific to radar data and returns the maximum value over the range of bins that are combined due to the scaling factor. This ensures that maximum reflectivity or maximum velocity are preserved during the scaling process. Using an image scaling package is not preferred in this case as the scaling algorithm used my mask important data.
|
||||
|
||||
options.size > 1800 is invalid as this would cause data to be interpolated and would not be a true representation of the data returned by the radar. If you need this functionality it's recommended to use an image scaling package such as [jimp](https://www.npmjs.com/package/jimp) or [gm](https://www.npmjs.com/package/gm) on the Canvas returned by plot().
|
||||
options.size > 3600 is invalid as this would cause data to be interpolated and would not be a true representation of the data returned by the radar. If you need this functionality it's recommended to use an image scaling package such as [jimp](https://www.npmjs.com/package/jimp) or [gm](https://www.npmjs.com/package/gm) on the Canvas returned by plot().
|
||||
|
||||
options.crop
|
||||
|
||||
### Palettizing
|
||||
>If used with [plotAndData()](#plotanddatafile-options) both the original image and the palettized image will be returned if used with [plot()](#plotfile-options) only the palettized image will be returned.
|
||||
@@ -91,7 +94,7 @@ Palettizing introduces a compromise between image size and compresability. After
|
||||
|
||||
A look-up table is created and cached as part of the palettization process speeding up additional calls the the palettizing function. The cache is specific to the product and options provided.
|
||||
|
||||
#### Palettization options (options.palettize.<parameter>)
|
||||
#### Palettization options (options.palettize.\<parameter>)
|
||||
Parameter|Type|Default|Description
|
||||
|--|--|--|--|
|
||||
generate|integer|0|Generate a palette by creating a number of steps between the background color and each color provided the the product data. There is a hard limit of 256 colors in the palette due to the PNG specification. For example a 16-color original palette with generate = 4 would produce a palette of 64 colors (4 versions of each of the original 16 colors).
|
||||
|
||||
+2
-1
@@ -29,12 +29,13 @@ const plots = [];
|
||||
|
||||
// plot for each elevation and size
|
||||
(async () => {
|
||||
await Promise.allSettled([1800, 900, 450, 100].map(async (size) => {
|
||||
await Promise.allSettled([3600, 1800, 900, 450, 100].map(async (size) => {
|
||||
await Promise.allSettled(radarData.listElevations().map(async (elevation) => {
|
||||
plots[elevation] = plot(radarData, ['REF', 'VEL'], {
|
||||
elevation,
|
||||
size,
|
||||
palettize: true,
|
||||
cropTo: size / 2,
|
||||
});
|
||||
|
||||
// write files to disk
|
||||
|
||||
+43
-31
@@ -32,7 +32,8 @@ const palettes = {
|
||||
// default options
|
||||
const DEFAULT_OPTIONS = {
|
||||
// must be a square image
|
||||
size: 1800,
|
||||
size: 3600,
|
||||
cropTo: 3600,
|
||||
background: 'black',
|
||||
lineWidth: 2,
|
||||
};
|
||||
@@ -46,21 +47,28 @@ const draw = (data, _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 a size > 0');
|
||||
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');
|
||||
// calculate highest bin number to plot, diagonal distance from center of plot area to corner
|
||||
// TODO improve plotting time by calculating the max bin for each azimuth
|
||||
const cropMaxBin = Math.ceil((Math.sqrt(2) * (cropTo / 2)) * scale);
|
||||
|
||||
// create the canvas and context
|
||||
const canvas = createCanvas(options.size, options.size);
|
||||
const canvas = createCanvas(cropTo, cropTo);
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
// fill background with black
|
||||
ctx.fillStyle = options.background;
|
||||
ctx.fillRect(0, 0, options.size, options.size);
|
||||
ctx.fillRect(0, 0, cropTo, cropTo);
|
||||
|
||||
// canvas settings
|
||||
ctx.imageSmoothingEnabled = true;
|
||||
ctx.lineWidth = options.lineWidth;
|
||||
ctx.translate(options.size / 2, options.size / 2);
|
||||
ctx.translate(cropTo / 2, cropTo / 2);
|
||||
ctx.rotate(-Math.PI / 2);
|
||||
|
||||
// get the palette
|
||||
@@ -107,38 +115,42 @@ const draw = (data, _options) => {
|
||||
let downsampled = 0;
|
||||
let lastRemainder = 0;
|
||||
|
||||
// calculate maximum bin to plot
|
||||
const maxBin = Math.min(cropMaxBin, thisRadial.moment_data.length);
|
||||
// plot each bin
|
||||
thisRadial.moment_data.forEach((bin, idx) => {
|
||||
for (let idx = 0; idx < maxBin; idx += 1) {
|
||||
// get the value
|
||||
const bin = thisRadial.moment_data[idx];
|
||||
// skip null values
|
||||
if (bin === null) return;
|
||||
|
||||
let thisSample;
|
||||
// test for downsampling
|
||||
if (scale !== 1) {
|
||||
const remainder = idx % scale;
|
||||
// test for rollover in scaling
|
||||
if (remainder < lastRemainder) {
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
// see if there's a sample to plot
|
||||
if (!thisSample || palette.inDeadband(thisSample)) return;
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.strokeStyle = palette.findColorRgba(thisSample);
|
||||
ctx.arc(0, 0, (idx + deadZone) / scale, startAngle, endAngle);
|
||||
ctx.stroke();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (!options.palettize) {
|
||||
|
||||
Reference in new issue
Block a user