Migrate pressure to a typed unit.

This commit is contained in:
Dan Albert
2021-07-16 22:38:41 -07:00
parent d11174da21
commit 28f98aed88
5 changed files with 44 additions and 33 deletions

View File

@@ -16,6 +16,9 @@ KPH_TO_KNOTS = 1 / KNOTS_TO_KPH
MS_TO_KPH = 3.6
KPH_TO_MS = 1 / MS_TO_KPH
INHG_TO_HPA = 33.86389
INHG_TR_MMHG = 25.400002776728
def heading_sum(h: int, a: int) -> int:
h += a
@@ -181,6 +184,27 @@ def mach(value: float, altitude: Distance) -> Speed:
SPEED_OF_SOUND_AT_SEA_LEVEL = knots(661.5)
@dataclass(frozen=True, order=True)
class Pressure:
pressure_in_inches_hg: float
@property
def inches_hg(self) -> float:
return self.pressure_in_inches_hg
@property
def mm_hg(self) -> float:
return self.pressure_in_inches_hg * INHG_TR_MMHG
@property
def hecto_pascals(self) -> float:
return self.pressure_in_inches_hg * INHG_TO_HPA
def inches_hg(value: float) -> Pressure:
return Pressure(value)
def pairwise(iterable: Iterable[Any]) -> Iterable[tuple[Any, Any]]:
"""
itertools recipe

View File

@@ -5,13 +5,14 @@ import logging
import random
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING, Any
from dcs.cloud_presets import Clouds as PydcsClouds
from dcs.weather import CloudPreset, Weather as PydcsWeather, Wind
from game.savecompat import has_save_compat_for
from game.settings import Settings
from game.utils import Distance, meters, interpolate
from game.utils import Distance, meters, interpolate, Pressure, inches_hg
if TYPE_CHECKING:
from game.theater import ConflictTheater
@@ -27,11 +28,19 @@ class TimeOfDay(Enum):
@dataclass(frozen=True)
class AtmosphericConditions:
#: Pressure at sea level in inches of mercury.
qnh_inches_mercury: float
#: Pressure at sea level.
qnh: Pressure
#: Temperature at sea level in Celcius.
temperature_celsius: float
@has_save_compat_for(5)
def __setstate__(self, state: dict[str, Any]) -> None:
if "qnh" not in state:
state["qnh"] = inches_hg(state["qnh_inches_mercury"])
del state["qnh_inches_mercury"]
self.__dict__.update(state)
@dataclass(frozen=True)
class WindConditions:
@@ -111,7 +120,7 @@ class Weather:
pressure += self.pressure_adjustment
temperature += self.temperature_adjustment
conditions = AtmosphericConditions(
qnh_inches_mercury=self.random_pressure(pressure),
qnh=self.random_pressure(pressure),
temperature_celsius=self.random_temperature(temperature),
)
return conditions
@@ -162,14 +171,14 @@ class Weather:
return random.randint(100, 400)
@staticmethod
def random_pressure(average_pressure: float) -> float:
def random_pressure(average_pressure: float) -> Pressure:
# "Safe" constants based roughly on ME and viper altimeter.
# Units are inches of mercury.
SAFE_MIN = 28.4
SAFE_MAX = 30.9
# Use normalvariate to get normal distribution, more realistic than uniform
pressure = random.normalvariate(average_pressure, 0.1)
return max(SAFE_MIN, min(SAFE_MAX, pressure))
return inches_hg(max(SAFE_MIN, min(SAFE_MAX, pressure)))
@staticmethod
def random_temperature(average_temperature: float) -> float: