Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pressure bug, add pressure altitudes, default to normalized, remove interpolated #162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/features/rap/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default function Table({
);

switch (altitudeLevels) {
case AltitudeLevels.Default:
case AltitudeLevels.Raw:
return filteredPressures;
case AltitudeLevels.Normalized:
return NORMALIZED_PRESSURE_MB.map((pressure) =>
Expand All @@ -115,7 +115,7 @@ export default function Table({
}

switch (altitudeLevels) {
case AltitudeLevels.Default:
case AltitudeLevels.Raw:
return windsAloftHour.altitudes
.slice(0, rows)
.filter((datum) => !hiddenAltitude(datum));
Expand Down
2 changes: 1 addition & 1 deletion src/features/rap/extra/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function Settings() {
<Radio
label={t("Altitude levels")}
tip={t("Altitude levels tip")}
options={[AltitudeLevels.Default, AltitudeLevels.Normalized]}
options={[AltitudeLevels.Normalized, AltitudeLevels.Raw]}
value={altitudeLevels}
onChange={(value) => dispatch(setAltitudeLevels(value))}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/features/rap/extra/settings/settingEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export enum AltitudeType {
}

export enum AltitudeLevels {
Default = "Default",
Normalized = "Normalized",
Raw = "Raw",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to translate (and remove Default key)

}

// CSS scroll-snap-stop
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/locationHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function getAltitudeLevels() {
const value = locationHashMap.get(
ALTITUDE_LEVELS_STORAGE_KEY,
) as AltitudeLevels;
if (value !== AltitudeLevels.Default && value !== AltitudeLevels.Normalized)
if (value !== AltitudeLevels.Raw && value !== AltitudeLevels.Normalized)
return undefined;
return value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/features/user/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ export function getAltitudeLevels(): AltitudeLevels {

if (
typeof savedValue !== "string" ||
(savedValue !== AltitudeLevels.Default &&
(savedValue !== AltitudeLevels.Raw &&
savedValue !== AltitudeLevels.Normalized)
)
return AltitudeLevels.Default;
return AltitudeLevels.Normalized;

return savedValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"Hush G-Airmets tip": "If turned on, new G-AIRMETs will not trigger the unread alert notifications banner, and they will be pushed to the bottom of the alerts list. This can be useful if you find G-AIRMETs too noisy. [US-only]",
"Localization in progress": "Localization is in progress. To help, please email hello@ppg.report",
"Default": "Default",
"Normalized": "Normalized",
"Raw": "Raw",
"Advanced features": "Advanced features",
"Advanced features tip": "EXPERIMENTAL. Current features include: <ul><li>Icon to represent lapse rate (thresholds for DALR, MALR, and temperature inversion).</li><li>Relative humidity saturation indicator.</li><li>Tap temperature cell to open temperature details tooltip.</li></ul> Feedback welcome — hello@ppg.report",
"Nocturnal temperature inversion information": "<strong>Nocturnal temperature inversions</strong> indicate calm conditions, but quickly erode after sunrise.",
Expand Down
85 changes: 16 additions & 69 deletions src/services/openMeteo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import axios from "axios";
import { WindsAloftAltitude, WindsAloftReport } from "../models/WindsAloft";
import {
convertToInterpolatorWithHeight,
interpolateWindVectors,
} from "../helpers/interpolate";
import { WindsAloftReport } from "../models/WindsAloft";
import { notEmpty } from "../helpers/array";
import zipObject from "lodash/zipObject";
import * as velitherm from "velitherm";
Expand All @@ -15,7 +11,8 @@ const FORECAST_DAYS_WINDS_ALOFT = 2;
* in hPa
*/
const PRESSURE_ALTITUDES = [
1000, 975, 950, 925, 900, 850, 800, 700, 600, 500, 400, 300, 250,
1000, 975, 950, 925, 900, 875, 850, 825, 800, 775, 750, 725, 700, 650, 600,
550, 500, 450, 400, 300, 250,
] as const;

const PRESSURE_ALTITUDE_METRICS = [
Expand Down Expand Up @@ -118,11 +115,12 @@ export async function getWindsAloft(
windsAloft: WindsAloftReport;
elevationInM: number;
}> {
const aloft = await getOpenMeteoWindsAloft(latitude, longitude);
const response = await getOpenMeteoWindsAloft(latitude, longitude);
const windsAloft = convertOpenMeteoToWindsAloft(response);

return {
windsAloft: interpolate(convertOpenMeteoToWindsAloft(aloft)),
elevationInM: aloft.elevation,
windsAloft,
elevationInM: response.elevation,
};
}

Expand All @@ -146,62 +144,6 @@ function convertOpenMeteoToWeather(
};
}

function interpolate(report: WindsAloftReport): WindsAloftReport {
const lowestAltitudeMsl =
(report.elevationInM ?? 0) + Math.max(...AGL_ALTITUDES);

return {
...report,
hours: report.hours.map((hour) => ({
...hour,
altitudes: (() => {
const ditheredAltitudes: WindsAloftAltitude[] = [];

for (let i = 0; i < hour.altitudes.length; i++) {
ditheredAltitudes.push(hour.altitudes[i]);

if (!hour.altitudes[i + 1]) continue;
if (hour.altitudes[i].altitudeInM <= lowestAltitudeMsl) continue;

const middleAltitude =
(hour.altitudes[i].altitudeInM +
hour.altitudes[i + 1].altitudeInM) /
2;

const { height, speed, direction } = interpolateWindVectors(
convertToInterpolatorWithHeight(hour.altitudes[i]),
convertToInterpolatorWithHeight(hour.altitudes[i + 1]),
middleAltitude,
);

const temperatureInC = Math.round(
(hour.altitudes[i].temperatureInC +
hour.altitudes[i + 1].temperatureInC) /
2,
);

ditheredAltitudes.push({
windSpeedInKph: Math.round(speed * 10) / 10,
windDirectionInDeg: Math.round(direction),
altitudeInM: Math.round(height),
temperatureInC,
dewpointInC: Math.round(
(hour.altitudes[i].dewpointInC +
hour.altitudes[i + 1].dewpointInC) /
2,
),
pressure: Math.round(
(hour.altitudes[i].pressure + hour.altitudes[i + 1].pressure) / 2,
),
});
}

return ditheredAltitudes;
})(),
})),
};
}

async function getOpenMeteoWindsAloft(
latitude: number,
longitude: number,
Expand Down Expand Up @@ -300,6 +242,7 @@ function convertOpenMeteoToWindsAloft(
openMeteoResponse.hourly[`temperature_${agl}m`][index],
dewpointInC: velitherm.dewPoint(
calculateRelativeHumidity(
openMeteoResponse.elevation,
agl,
openMeteoResponse.hourly[`surface_pressure`][index],
openMeteoResponse.hourly[`temperature_${agl}m`][index],
Expand All @@ -309,8 +252,11 @@ function convertOpenMeteoToWindsAloft(
),
pressure: Math.round(
velitherm.pressureFromAltitude(
agl,
openMeteoResponse.elevation + agl,
openMeteoResponse.hourly["pressure_msl"][index],
(openMeteoResponse.hourly[`temperature_${agl}m`][index] +
openMeteoResponse.hourly[`temperature_2m`][index]) /
2,
),
),
})),
Expand Down Expand Up @@ -354,20 +300,21 @@ function generateWeatherParams(): HourlyWeatherParams[] {
}

function calculateRelativeHumidity(
altitude: number,
elevation: number,
agl: number,
basePressure: number,
baseTemperature: number,
baseRH: number,
): number {
// Calculate the pressure at the given altitude
const pressure = velitherm.pressureFromAltitude(
altitude,
elevation + agl,
basePressure,
baseTemperature,
);

// Calculate the temperature at the given altitude
const temperature = baseTemperature - altitude * velitherm.gamma;
const temperature = baseTemperature - agl * velitherm.gamma;

// Calculate the specific humidity at the given temperature and pressure
const specificHumidity = velitherm.specificHumidity(
Expand Down
Loading