From 7652e9fb8147fa80ff298d96828f3e2e02fd38dd Mon Sep 17 00:00:00 2001 From: Matt Walsh Date: Wed, 11 Aug 2021 11:05:46 -0500 Subject: [PATCH] don't plot outside crop area --- .vscode/settings.json | 1 + demos/test.js | 33 +++++++++++++++++---------------- src/draw/index.js | 14 ++++++++++++-- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3a1f85a..256f7d0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ }, "cSpell.words": [ "KLOT", + "Mult", "NOAA", "Palettized", "RGBA", diff --git a/demos/test.js b/demos/test.js index d94ba35..b0e2868 100644 --- a/demos/test.js +++ b/demos/test.js @@ -6,8 +6,8 @@ const { plot, writePngToFile } = require('../src'); // list files // const files = glob.sync('./data/KLOT/34/*'); -const files = glob.sync('./data/KLOT/381/*'); -// const files = glob.sync('./data/KLOT/548/*'); +// const files = glob.sync('./data/KLOT/381/*'); +const files = glob.sync('./data/KLOT/548/*'); // const files = glob.sync('./data/KLOT/940/*'); // const files = glob.sync('./data/KSRX/804/*'); // const files = glob.sync('./data/TORD/767/*'); @@ -29,20 +29,21 @@ const plots = []; // plot for each elevation and size (async () => { - 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, - }); + await Promise.allSettled([3600, 1800, 900, 800, 600, 450, 100].map(async (size) => { + // await Promise.allSettled(radarData.listElevations().map(async (elevation) => {\ + const elevation = radarData.listElevations()[0]; + plots[elevation] = plot(radarData, ['REF', 'VEL'], { + elevation, + size, + palettize: true, + cropTo: size / 2, + }); - // 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), - ]); - })); + // 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), + ]); + // })); })); })(); diff --git a/src/draw/index.js b/src/draw/index.js index 518223d..2735655 100644 --- a/src/draw/index.js +++ b/src/draw/index.js @@ -38,6 +38,10 @@ const DEFAULT_OPTIONS = { lineWidth: 2, }; +// calculation constants +const RAD45 = (45 * Math.PI / 180); +const RAD90 = RAD45 * 2; + const draw = (data, _options) => { // combine options with defaults const options = { @@ -113,9 +117,15 @@ const draw = (data, _options) => { let lastRemainder = 0; // calculate maximum bin to plot based on azimuth - const azWrap = startAngle % (Math.PI / 2); - const cropMaxBin = Math.ceil(Math.abs(cropTo / Math.cos(azWrap)) - 50); + // wrap azimuth to 90° offset by -45° (% in js is remainder, the formula below make it in to modulus) + const azWrap = (((startAngle - RAD45) % RAD90) + RAD90) % RAD90; + // calculate a magnitude multiplier as 1/sin with 45° shift removed + const azMagnitudeMult = 1 / Math.abs(Math.sin(azWrap + RAD45)); + const cropMaxBin = Math.ceil(Math.abs(options.cropTo / 2 * scale * azMagnitudeMult)); + + // compare max calculated value with length of radial const maxBin = Math.min(cropMaxBin, thisRadial.moment_data.length); + // plot each bin for (let idx = 0; idx < maxBin; idx += 1) { // get the value