From d677cbd0ca84e3b99b799abb4148794ebfc9ce33 Mon Sep 17 00:00:00 2001 From: Matt Walsh Date: Mon, 1 Mar 2021 08:11:45 -0600 Subject: [PATCH] basic plotting code --- .vscode/launch.json | 17 ++++++++ demos/78-n1p-one-hour-precipitation.js | 11 +++++ .../data}/LOT_HHC_2021_01_31_11_06_30 | Bin .../data}/LOT_N0H_2021_01_31_11_06_30 | Bin .../data}/LOT_N0S_2021_01_31_11_06_30 | Bin .../data}/LOT_N1P_2021_01_31_11_06_30 | Bin .../data}/LOT_NTP_2021_01_31_11_06_30 | Bin demos/test.js | 21 ++++++++++ src/draw/index.js | 39 ++++++++++++++++++ src/index.js | 22 ++++++++++ src/products/78/index.js | 30 ++++++++++++++ src/products/index.js | 22 ++++++++++ 12 files changed, 162 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 demos/78-n1p-one-hour-precipitation.js rename {data => demos/data}/LOT_HHC_2021_01_31_11_06_30 (100%) rename {data => demos/data}/LOT_N0H_2021_01_31_11_06_30 (100%) rename {data => demos/data}/LOT_N0S_2021_01_31_11_06_30 (100%) rename {data => demos/data}/LOT_N1P_2021_01_31_11_06_30 (100%) rename {data => demos/data}/LOT_NTP_2021_01_31_11_06_30 (100%) create mode 100644 demos/test.js create mode 100644 src/draw/index.js create mode 100644 src/products/78/index.js create mode 100644 src/products/index.js diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5bc8f26 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/demos/78-n1p-one-hour-precipitation.js" + } + ] +} \ No newline at end of file diff --git a/demos/78-n1p-one-hour-precipitation.js b/demos/78-n1p-one-hour-precipitation.js new file mode 100644 index 0000000..ad89c99 --- /dev/null +++ b/demos/78-n1p-one-hour-precipitation.js @@ -0,0 +1,11 @@ +const fs = require('fs'); +const { plot } = require('../src'); + +// read file +const file = fs.readFileSync('./demos/data/LOT_N1P_2021_01_31_11_06_30'); + +// pass to plotting engine as a string or buffer +const level3Plot = plot(file); + +console.log(level3Plot); +console.log(); diff --git a/data/LOT_HHC_2021_01_31_11_06_30 b/demos/data/LOT_HHC_2021_01_31_11_06_30 similarity index 100% rename from data/LOT_HHC_2021_01_31_11_06_30 rename to demos/data/LOT_HHC_2021_01_31_11_06_30 diff --git a/data/LOT_N0H_2021_01_31_11_06_30 b/demos/data/LOT_N0H_2021_01_31_11_06_30 similarity index 100% rename from data/LOT_N0H_2021_01_31_11_06_30 rename to demos/data/LOT_N0H_2021_01_31_11_06_30 diff --git a/data/LOT_N0S_2021_01_31_11_06_30 b/demos/data/LOT_N0S_2021_01_31_11_06_30 similarity index 100% rename from data/LOT_N0S_2021_01_31_11_06_30 rename to demos/data/LOT_N0S_2021_01_31_11_06_30 diff --git a/data/LOT_N1P_2021_01_31_11_06_30 b/demos/data/LOT_N1P_2021_01_31_11_06_30 similarity index 100% rename from data/LOT_N1P_2021_01_31_11_06_30 rename to demos/data/LOT_N1P_2021_01_31_11_06_30 diff --git a/data/LOT_NTP_2021_01_31_11_06_30 b/demos/data/LOT_NTP_2021_01_31_11_06_30 similarity index 100% rename from data/LOT_NTP_2021_01_31_11_06_30 rename to demos/data/LOT_NTP_2021_01_31_11_06_30 diff --git a/demos/test.js b/demos/test.js new file mode 100644 index 0000000..dac3856 --- /dev/null +++ b/demos/test.js @@ -0,0 +1,21 @@ +const fs = require('fs'); +const { plot } = require('../src'); + +// read file + +// 56 N0S Storm relative velocity +// const file = fs.readFileSync('./data/LOT_N0S_2021_01_31_11_06_30'); +// 78 One-hour precipitation +const file = fs.readFileSync('./data/LOT_N1P_2021_01_31_11_06_30'); +// 80 NTP Storm total accumulation +// const file = fs.readFileSync('./data/LOT_NTP_2021_01_31_11_06_30'); +// 165 N0H Hydrometeor classification +// const file = fs.readFileSync('./data/LOT_N0H_2021_01_31_11_06_30'); +// 177 HHC Hybrid Hydrometeor classification +// const file = fs.readFileSync('./data/LOT_HHC_2021_01_31_11_06_30'); + +// pass to plotting engine as a string or buffer +const level3Plot = plot(file); + +console.log(level3Plot); +console.log(); diff --git a/src/draw/index.js b/src/draw/index.js new file mode 100644 index 0000000..f69c509 --- /dev/null +++ b/src/draw/index.js @@ -0,0 +1,39 @@ +const { createCanvas } = require('canvas'); + +const draw = (data, product) => { + // canvas size + const dim = { + x: 1800, + y: 1800, + }; + + // create the canvas and context + const canvas = createCanvas(dim.x, dim.y); + const ctx = canvas.getContext('2d'); + + // fill background with black + ctx.fillStyle = 'black'; + ctx.fillRect(0, 0, dim.x, dim.y); + + // canvas settings + ctx.imageSmoothingEnabled = true; + ctx.lineWidth = 1; + ctx.translate(dim.x / 2, dim.y / 2); + ctx.rotate(-Math.PI / 2); + + data.radialPackets[0].radials.forEach((radial) => { + const startAngle = radial.startAngle * Math.PI / 180; + const endAngle = startAngle + radial.angleDelta * Math.PI / 180; + // for each bin + radial.bins.forEach((bin, idx) => { + ctx.beginPath(); + ctx.strokeStyle = product.palette[bin]; + ctx.arc(0, 0, idx + data.radialPackets[0].firstBin, startAngle, endAngle); + ctx.stroke(); + }); + }); + + console.log(data); +}; + +module.exports = draw; diff --git a/src/index.js b/src/index.js index e69de29..266af0e 100644 --- a/src/index.js +++ b/src/index.js @@ -0,0 +1,22 @@ +const NexradLevel3Data = require('nexrad-level-3-data'); +const { products, productAbbreviations } = require('./products'); +const draw = require('./draw'); + +const plot = (file) => { + // parse the file + const data = NexradLevel3Data(file); + // test the product code and product type + if (!productAbbreviations.includes(data.productDescription.abbreviation)) throw new Error(`Unsupported product ${data.productDescription.abbreviation}`); + // get the product + const product = products[data.productDescription.code]; + if (!product) throw new Error(`Unsupported product code ${data.productDescription.code}`); + + // call the draw function + const image = draw(data, product); + + return image; +}; + +module.exports = { + plot, +}; diff --git a/src/products/78/index.js b/src/products/78/index.js new file mode 100644 index 0000000..0c12141 --- /dev/null +++ b/src/products/78/index.js @@ -0,0 +1,30 @@ +const code = 78; +const abbreviation = 'N1P'; +const description = 'One-hour precipitation'; + +// 4-bit palette +const palette = [ + 0, 0, 0, + 16, 0, 0, + 32, 0, 0, + 48, 0, 0, + 64, 0, 0, + 80, 0, 0, + 96, 0, 0, + 112, 0, 0, + 128, 0, 0, + 144, 0, 0, + 160, 0, 0, + 176, 0, 0, + 192, 0, 0, + 208, 0, 0, + 224, 0, 0, + 240, 0, 0, +]; + +module.exports = { + code, + abbreviation, + description, + palette, +}; diff --git a/src/products/index.js b/src/products/index.js new file mode 100644 index 0000000..a8643a8 --- /dev/null +++ b/src/products/index.js @@ -0,0 +1,22 @@ +const fs = require('fs'); +const path = require('path'); + +// load all products in folder automatically +const folders = fs.readdirSync(__dirname); +// eslint-disable-next-line import/no-dynamic-require, global-require +const productsRaw = folders.map((folder) => require(path.join(__dirname, folder))); + +// make up a list of products by integer type +const products = {}; +productsRaw.forEach((product) => { + if (products[product.code]) throw new Error(`Duplicate product code ${product.code}`); + products[product.code] = product; +}); + +// list of available product code abbreviations for type-checking +const productAbbreviations = productsRaw.map((product) => product.abbreviation).flat(); + +module.exports = { + products, + productAbbreviations, +};