Skip to content

Commit

Permalink
failing to make sure elevation is string, but opening type to allow b…
Browse files Browse the repository at this point in the history
…uild
  • Loading branch information
bacalj committed Jul 22, 2024
1 parent cc85401 commit 292cad2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/assets/data/weather-stations.json
Original file line number Diff line number Diff line change
Expand Up @@ -126540,6 +126540,7 @@
"latitude": 32.65437,
"longitude": -79.948532,
"name": "FOLLY BEACH, SC US",
"elevation": 0,
"ICAO": "",
"mindate": "2008-01-31",
"maxdate": "2024-06-01",
Expand Down
6 changes: 4 additions & 2 deletions src/bin/noaa-create-station-data
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ function main() {
const ISDStations = getProcessedISDStations(configuration);
const GHCNDStations = getFlatGHCNDStations(configuration);
const mergedStations = mergeInGHCNDStations(ISDStations, GHCNDStations);
const dualIDStations = mergedStations.filter(stn => stn.ghcndID && stn.isdID);
const finalStations = mergedStations.filter(stn => stn.ghcndID && stn.isdID);
writeDebugToFile(ISDStations, "ISDStations");
writeDebugToFile(GHCNDStations, "GHCNDStations");
writeDebugToFile(mergedStations, "mergedStations");
writeDebugToFile(dualIDStations, "dualIDStations");
writeDebugToFile(finalStations, "dualIDStations");

process.stdout.write(JSON.stringify(finalStations));
}

catch (ex) {
Expand Down
16 changes: 10 additions & 6 deletions src/bin/noaa-station-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const process = require("process");
const path = require("path");
/* eslint-enable @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports */

const kDeltaSq = 0.0001; // distance in degrees within which two stations are considered the same.
//const kDeltaSq = 0.0001; // distance in degrees within which two stations are considered the same.


function configure() {
let configuration = {};

configuration.progName = process.argv[1].replace(/.*\//, '');
configuration.progName = process.argv[1].replace(/.*\//, "");

let isdStationsFile = process.argv[2];
if (!(isdStationsFile && fs.existsSync(isdStationsFile) ) ) {
Expand Down Expand Up @@ -47,8 +47,12 @@ function usage(configuration) {
// This is a way to get a distance between two points on a sphere (Earth) given their latitudes and longitudes.
const R = 6378137; // equatorial mean radius of Earth (in meters)

function squared (x) { return x * x }
function toRad (x) { return x * Math.PI / 180.0 }
function squared (x) {
return x * x;
}
function toRad (x) {
return x * Math.PI / 180.0;
}
function hav (x) {
return squared(Math.sin(x / 2));
}
Expand Down Expand Up @@ -166,7 +170,7 @@ function normalizeISDStation(st) {
latitude: st.latitude,
longitude: st.longitude,
name: st.name,
elevation: st.elevation,
elevation: getElevationsFromMetersString(st.elevation),
ICAO: st.ICAO,
mindate: st.mindate,
maxdate: st.maxdate,
Expand Down Expand Up @@ -196,7 +200,7 @@ function normalizeGHCNDStation(st) {
latitude: st.latitude,
longitude: st.longitude,
name: st.name,
elevation: st.elevation,
elevation: getElevationsFromMetersString(st.elevation),
mindate: st.mindate,
maxdate: st.maxdate,
ghcndID: id,
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface IWeatherStation {
latitude: number;
longitude: number;
name: string;
elevation?: number;
elevation?: number | string;
ICAO: string; // "KMWN"
mindate: string; // "1973-01-01"
maxdate: string; // "1973-01-01" || "present",
Expand Down
37 changes: 36 additions & 1 deletion src/utils/getWeatherStations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,49 @@ import dayjs from "dayjs";
import { IStation, IWeatherStation } from "../types";
import weatherStations from "../assets/data/weather-stations.json";

function getElevationsFromMetersString(elevation: number | string): number {
if (typeof elevation === "number") {
return elevation;
}

if (typeof elevation === "string") {
elevation = elevation.trim();
if (elevation === "") return 0;

const isNegative = elevation.startsWith("-");
if (elevation.startsWith("+") || isNegative) {
elevation = elevation.substring(1);
}

const elevationInMeters = parseFloat(elevation);
if (isNaN(elevationInMeters)) return 0;

const rawMeters = isNegative ? -elevationInMeters : elevationInMeters;
const meters = Math.round(rawMeters);
return meters;
}

return 0;
}

/**
* We assume that the station dataset was prepared in the past but
* stations that were active at the time of preparation are still active.
* If they reported within a week of the dataset's max date, we mark the
* max date as "present".
*/
export const adjustStationDataset = () => {
const datasetArr = Array.from(weatherStations);
const datasetArrRaw = Array.from(weatherStations);
// make sure any elevation is a number

const datasetArr = datasetArrRaw.map(station => {
const newStation = {...station};
if (newStation.elevation) {
newStation.elevation = getElevationsFromMetersString(newStation.elevation);
}
return newStation;
});

let maxDate: dayjs.Dayjs | null = null;

if (datasetArr) {
Expand Down

0 comments on commit 292cad2

Please sign in to comment.