From b131e47a120e61cfc58b3916ce857bf0f854052d Mon Sep 17 00:00:00 2001 From: Alexander Harding <2166114+aeharding@users.noreply.github.com> Date: Wed, 7 Jun 2023 19:34:06 -0500 Subject: [PATCH] Add optional dewpoints for invalid values (#5) --- lib/index.ts | 27 +++++++++++++++-- test/01/expected.json | 2 +- test/01/index.ts | 2 +- test/02/expected.json | 2 -- test/02/index.ts | 2 +- test/08/index.ts | 23 +++++++++++++++ test/08/rap.txt | 68 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 test/08/index.ts create mode 100644 test/08/rap.txt diff --git a/lib/index.ts b/lib/index.ts index c75fa3f..1871002 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -21,9 +21,20 @@ export interface RapDatum { pressure: number; height: number; temp: number; - dewpt: number; + + /** + * dewpt is very rarely `undefined` in data error issues. + * + * In my experience, this happens with very low dewpoint values. + * You could probably assume dewpt of -100C + * + * For more, see: https://github.com/NOAA-GSL/GSL-Regional-Model-Forum/discussions/36 + */ + dewpt?: number; + windDir: number; windSpd: number; + hhmm?: number; bearing?: number; range?: number; @@ -67,7 +78,7 @@ function parseReport(asciiReport: string): Rap | undefined { const data = parseLines(lines); - if (data.data.length === 1 && data.data[0].dewpt === 997259) + if (data.data.length === 1 && data.data[0].dewpt == null) throw new CoordinatesGslError(); return { headerLine, date, type, cape, cin, ...data }; @@ -204,7 +215,7 @@ function parseDataLine([ windDir, windSpd, ]: string[]): RapDatum { - return parseToNumber({ + let parsed = parseToNumber({ pressure, height, temp, @@ -212,6 +223,16 @@ function parseDataLine([ windDir, windSpd, }); + + if (isInvalidDewpt(parsed.dewpt)) return { ...parsed, dewpt: undefined }; + + return parsed; +} + +// https://github.com/NOAA-GSL/GSL-Regional-Model-Forum/discussions/36 +// Filter out dewpoints above 100°C +function isInvalidDewpt(dewpt: number) { + return dewpt > 1_000; } function parseToNumber( diff --git a/test/01/expected.json b/test/01/expected.json index 2c2045c..e03aaad 100644 --- a/test/01/expected.json +++ b/test/01/expected.json @@ -4,7 +4,7 @@ "date": "2021-09-02T23:00:00.000Z", "type": "Op40", "cape": 0, - "cin": -0, + "cin": 0, "wban": 23062, "wmo": 72641, "lat": 43.05, diff --git a/test/01/index.ts b/test/01/index.ts index c27c894..b30227b 100644 --- a/test/01/index.ts +++ b/test/01/index.ts @@ -9,7 +9,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url)); run(async () => { const rap = fse.readFileSync(`${__dirname}/rap.txt`).toString(); - const result = parse(rap); + const result = JSON.parse(JSON.stringify(parse(rap))); const expected = fse.readJSONSync(`${__dirname}/expected.json`); diff --git a/test/02/expected.json b/test/02/expected.json index 7685584..21b227f 100644 --- a/test/02/expected.json +++ b/test/02/expected.json @@ -717,7 +717,6 @@ "pressure": 3186, "height": 9236, "temp": -267, - "dewpt": 997259, "windDir": 241, "windSpd": 46 }, @@ -725,7 +724,6 @@ "pressure": 3000, "height": 9667, "temp": -299, - "dewpt": 61639, "windDir": 240, "windSpd": 49 }, diff --git a/test/02/index.ts b/test/02/index.ts index c27c894..b30227b 100644 --- a/test/02/index.ts +++ b/test/02/index.ts @@ -9,7 +9,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url)); run(async () => { const rap = fse.readFileSync(`${__dirname}/rap.txt`).toString(); - const result = parse(rap); + const result = JSON.parse(JSON.stringify(parse(rap))); const expected = fse.readJSONSync(`${__dirname}/expected.json`); diff --git a/test/08/index.ts b/test/08/index.ts new file mode 100644 index 0000000..b6a7ab4 --- /dev/null +++ b/test/08/index.ts @@ -0,0 +1,23 @@ +import assert from "assert/strict"; +import fse from "fs-extra"; +import { run } from "../misc.js"; +import parse from "../../lib/index.js"; +import { dirname } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +// RAP from Alaska + +run(async () => { + const rap = fse.readFileSync(`${__dirname}/rap.txt`).toString(); + + const parsed = parse(rap); + + for (const datum of parsed[0].data) { + if (datum.dewpt) { + assert(datum.dewpt >= -1000); + assert(datum.dewpt <= 1000); + } + } +}); diff --git a/test/08/rap.txt b/test/08/rap.txt new file mode 100644 index 0000000..a1a5d3f --- /dev/null +++ b/test/08/rap.txt @@ -0,0 +1,68 @@ +Op40 analysis valid for grid point 5.6 nm / 94 deg from 43.053,-89.438: +Op40 22 07 Jun 2023 + CAPE 0 CIN 0 Helic 99999 PW 99999 + 1 23062 99999 43.05 89.31 99999 99999 + 2 99999 99999 99999 66 99999 99999 + 3 43.053,-89.438 12 kt + 9 9783 289 232 36 25 9 + 4 10000 99999 99999 99999 99999 99999 + 5 9754 318 225 32 27 10 + 5 9701 369 219 26 27 11 + 5 9608 453 209 18 28 12 + 5 9472 574 197 9 28 12 + 5 9298 733 181 0 28 12 + 4 9250 775 176 -1 28 12 + 5 9090 922 161 -6 27 12 + 5 8863 1136 140 -11 26 12 + 5 8617 1374 118 -30 24 12 + 4 8500 1488 109 -46 24 12 + 5 8343 1644 96 -67 23 12 + 5 8041 1949 75 -116 20 13 + 5 7711 2293 56 -213 18 15 + 5 7352 2681 39 -245 11 21 + 4 7000 3079 24 -307 5 28 + 5 6966 3119 23 -313 4 29 + 5 6551 3614 5 997259 353 38 + 5 6107 4173 -19 -455 349 43 + 5 5639 4790 -58 -323 350 48 + 5 5184 5430 -102 -355 351 54 + 4 5000 5705 -124 -359 352 55 + 5 4774 6058 -153 -364 353 56 + 5 4406 6658 -199 -389 357 57 + 5 4074 7235 -236 -456 360 63 + 4 4000 7368 -246 -464 360 64 + 5 3773 7791 -276 -488 358 67 + 5 3501 8323 -320 -487 360 70 + 5 3256 8832 -358 -506 2 78 + 5 3034 9317 -395 -547 3 85 + 4 3000 9394 -401 -550 3 86 + 5 2835 9779 -430 -563 4 90 + 5 2655 10221 -459 -559 360 97 + 4 2500 10614 -490 -567 359 103 + 5 2487 10649 -493 -568 359 103 + 5 2335 11060 -522 -604 2 103 + 5 2196 11464 -542 -645 5 100 + 5 2057 11883 -559 -667 5 91 + 5 1917 12328 -572 -684 2 81 + 5 1778 12806 -575 -720 360 70 + 5 1638 13324 -569 -774 360 59 + 4 1500 13879 -573 -797 359 55 + 5 1498 13888 -573 -797 359 55 + 5 1359 14507 -573 -822 3 47 + 5 1219 15194 -567 -849 10 37 + 5 1080 15966 -569 -860 6 30 + 4 1000 16442 -573 -865 6 28 + 5 940 16826 -576 -869 6 26 + 5 810 17727 -585 -879 15 21 + 5 706 18591 -596 -888 30 17 + 4 700 18647 -596 -888 31 17 + 5 622 19416 -597 -895 41 17 + 5 543 20266 -582 -902 61 20 + 4 500 20785 -570 -906 75 17 + 5 469 21187 -561 -909 85 15 + 5 401 22192 -542 -915 114 8 + 5 338 23305 -524 -922 113 8 + 5 278 24563 -508 -932 127 10 + 5 223 26019 -495 -949 106 8 + 5 171 27770 -462 -947 69 10 + 5 123 30051 -408 -956 108 20