From 7b9839b50ea92b9214a20b3e14eb7b453f0502cc Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sat, 12 Sep 2026 11:01:38 -0700 Subject: [PATCH] Add paramater so that provider calculates the elevation. This is the default in the API, but not with this program. --- prototype.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/prototype.js b/prototype.js index be66070..ab9fcd3 100755 --- a/prototype.js +++ b/prototype.js @@ -32,19 +32,26 @@ const days_fore_max = 16; const days_fore_default = 12; const days_past_default = 2; +const ft_to_m = 0.3048; + const argv = yargs(hideBin(process.argv)) .option('days_fore', { alias: 'f', - describe: 'Set forecast days', + describe: 'Set forecast days, such as "-f 10". Maximum of 16.', type: 'number', requiresArg: true, }) .option('days_past', { alias: 'p', - describe: 'Set past days', + describe: 'Set past days, such as "-p 3"', type: 'number', requiresArg: true, }) + .option('calc_elevation', { + alias: 'e', + describe: 'Set to calculate elevation at lat/long.', + type: 'boolean', + }) .check((argv) => { if (argv.f !== undefined && (isNaN(argv.f) || argv.f < 0)) { throw new Error('Forecast days must be a non-negative number'); @@ -66,10 +73,13 @@ function max (value, upper) { } const days_fore = max(argv.hasOwnProperty("days_fore") ? argv.days_fore : days_fore_default, days_fore_max); const days_past = argv.hasOwnProperty("days_past") ? argv.days_past : days_past_default; +const calc_elevation = argv.calc_elevation ? true : false; const days = days_fore + days_past; console.log(`Averaging for ${days} days: ${days_fore} forecasted, ${days_past} past.`); +if (calc_elevation) console.log("Calculating elevation rather than using provided values."); +else console.log("Using user-provided elevations when they exist."); const locations = [ //{ name: "Flagstaff, AZ", lat: 35.1983, lon: -111.6513, elevation: 6903 }, @@ -93,8 +103,8 @@ const locations = [ { name: "Cherry: ++", lat: 34.5781, lon: -112.0677, elevation: 5300 }, //{ name: "Sedona: NG", lat: 34.8697, lon: -111.7610, elevation: 4420 }, //{ name: "Ehrenberg, AZ", lat: 33.9897, lon: -114.3550, elevation: 333 }, - { name: "Ehrenberg: ++", lat: 33.9897, lon: -114.3550, elevation: 333 }, - { name: "Quartzsite: ++", lat: 33.6763, lon: -114.2104, elevation: 888 }, + { name: "Ehrenberg: ++", lat: 33.9897, lon: -114.3550, elevation: 300 }, + { name: "Quartzsite: ++", lat: 33.6763, lon: -114.2104, elevation: 900 }, ]; locations.sort((a, b) => a.elevation - b.elevation); @@ -155,7 +165,7 @@ async function getForecast(loc) { const url = new URL("https://api.open-meteo.com/v1/forecast"); url.searchParams.set("latitude", loc.lat); url.searchParams.set("longitude", loc.lon); - url.searchParams.set("elevation", loc.elevation * 0.3048); + if (!calc_elevation && loc.elevation) url.searchParams.set("elevation", loc.elevation * ft_to_m); url.searchParams.set("daily", "temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min"); url.searchParams.set("temperature_unit", "fahrenheit"); url.searchParams.set("precipitation_unit", "inch"); @@ -179,7 +189,9 @@ async function getForecast(loc) { const avgHigh2 = (highs2.reduce((a, b) => a + b, 0) / highs2.length).toFixed(1); const avgLow2 = (lows2.reduce((a, b) => a + b, 0) / lows2.length).toFixed(1); - return { name: loc.name, elevation: loc.elevation, avgHigh, avgLow, avgHigh2, avgLow2 }; + const elev = (!calc_elevation && loc.elevation) ? loc.elevation : (data.elevation / ft_to_m).toFixed(0); + + return { name: loc.name, elevation: elev, avgHigh, avgLow, avgHigh2, avgLow2 }; } (async () => {