basic plotting code

This commit is contained in:
Matt Walsh committed 2021-03-01 08:11:45 -06:00
1 parent e861edbb50
commit d677cbd0ca
12 files changed
+162

No files matched your search

+17
View File
@@ -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": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/demos/78-n1p-one-hour-precipitation.js"
}
]
}
+11
View File
@@ -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();
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+21
View File
@@ -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();
+39
View File
@@ -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;
+22
View File
@@ -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,
};
+30
View File
@@ -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,
};
+22
View File
@@ -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,
};