Skip to content

Commit

Permalink
update / ignore type hints for numpy 2.2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
zebengberg committed Dec 9, 2024
1 parent 0d83a5b commit 9f662ac
Show file tree
Hide file tree
Showing 37 changed files with 1,633 additions and 1,597 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Fix the `ERA5` interface when making a pressure-level request with a single pressure level. This change accommodates CDS-Beta server behavior. Previously, a ValueError was raised in this case.

### Internals

- Update type hints for `numpy` 2.2 compatibility. Additional changes may be required after the next iteration of the `numpy` 2.2 series.

## v0.54.3

### Breaking changes
Expand Down
116 changes: 58 additions & 58 deletions pycontrails/core/aircraft_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ def simulate_fuel_and_performance(
self,
*,
aircraft_type: str,
altitude_ft: npt.NDArray[np.float64],
altitude_ft: npt.NDArray[np.floating],
time: npt.NDArray[np.datetime64],
true_airspeed: npt.NDArray[np.float64],
air_temperature: npt.NDArray[np.float64],
aircraft_mass: npt.NDArray[np.float64] | float | None,
thrust: npt.NDArray[np.float64] | float | None,
engine_efficiency: npt.NDArray[np.float64] | float | None,
fuel_flow: npt.NDArray[np.float64] | float | None,
true_airspeed: npt.NDArray[np.floating],
air_temperature: npt.NDArray[np.floating],
aircraft_mass: npt.NDArray[np.floating] | float | None,
thrust: npt.NDArray[np.floating] | float | None,
engine_efficiency: npt.NDArray[np.floating] | float | None,
fuel_flow: npt.NDArray[np.floating] | float | None,
q_fuel: float,
n_iter: int,
amass_oew: float,
Expand All @@ -192,21 +192,21 @@ def simulate_fuel_and_performance(
----------
aircraft_type: str
Aircraft type designator used to query the underlying model database.
altitude_ft: npt.NDArray[np.float64]
altitude_ft: npt.NDArray[np.floating]
Altitude at each waypoint, [:math:`ft`]
time: npt.NDArray[np.datetime64]
Waypoint time in ``np.datetime64`` format.
true_airspeed: npt.NDArray[np.float64]
true_airspeed: npt.NDArray[np.floating]
True airspeed for each waypoint, [:math:`m s^{-1}`]
air_temperature : npt.NDArray[np.float64]
air_temperature : npt.NDArray[np.floating]
Ambient temperature for each waypoint, [:math:`K`]
aircraft_mass : npt.NDArray[np.float64] | float | None
aircraft_mass : npt.NDArray[np.floating] | float | None
Override the aircraft_mass at each waypoint, [:math:`kg`].
thrust : npt.NDArray[np.float64] | float | None
thrust : npt.NDArray[np.floating] | float | None
Override the thrust setting at each waypoint, [:math: `N`].
engine_efficiency : npt.NDArray[np.float64] | float | None
engine_efficiency : npt.NDArray[np.floating] | float | None
Override the engine efficiency at each waypoint.
fuel_flow : npt.NDArray[np.float64] | float | None
fuel_flow : npt.NDArray[np.floating] | float | None
Override the fuel flow at each waypoint, [:math:`kg s^{-1}`].
q_fuel : float
Lower calorific value (LCV) of fuel, [:math:`J \ kg_{fuel}^{-1}`].
Expand Down Expand Up @@ -280,14 +280,14 @@ def _simulate_fuel_and_performance_known_aircraft_mass(
self,
*,
aircraft_type: str,
altitude_ft: npt.NDArray[np.float64],
altitude_ft: npt.NDArray[np.floating],
time: npt.NDArray[np.datetime64],
true_airspeed: npt.NDArray[np.float64],
air_temperature: npt.NDArray[np.float64],
aircraft_mass: npt.NDArray[np.float64] | float,
thrust: npt.NDArray[np.float64] | float | None,
engine_efficiency: npt.NDArray[np.float64] | float | None,
fuel_flow: npt.NDArray[np.float64] | float | None,
true_airspeed: npt.NDArray[np.floating],
air_temperature: npt.NDArray[np.floating],
aircraft_mass: npt.NDArray[np.floating] | float,
thrust: npt.NDArray[np.floating] | float | None,
engine_efficiency: npt.NDArray[np.floating] | float | None,
fuel_flow: npt.NDArray[np.floating] | float | None,
q_fuel: float,
**kwargs: Any,
) -> AircraftPerformanceData:
Expand Down Expand Up @@ -332,13 +332,13 @@ def _simulate_fuel_and_performance_unknown_aircraft_mass(
self,
*,
aircraft_type: str,
altitude_ft: npt.NDArray[np.float64],
altitude_ft: npt.NDArray[np.floating],
time: npt.NDArray[np.datetime64],
true_airspeed: npt.NDArray[np.float64],
air_temperature: npt.NDArray[np.float64],
thrust: npt.NDArray[np.float64] | float | None,
engine_efficiency: npt.NDArray[np.float64] | float | None,
fuel_flow: npt.NDArray[np.float64] | float | None,
true_airspeed: npt.NDArray[np.floating],
air_temperature: npt.NDArray[np.floating],
thrust: npt.NDArray[np.floating] | float | None,
engine_efficiency: npt.NDArray[np.floating] | float | None,
fuel_flow: npt.NDArray[np.floating] | float | None,
q_fuel: float,
n_iter: int,
amass_oew: float,
Expand All @@ -351,7 +351,7 @@ def _simulate_fuel_and_performance_unknown_aircraft_mass(
# Variable aircraft_mass will change dynamically after each iteration
# Set the initial aircraft mass depending on a possible load factor

aircraft_mass: npt.NDArray[np.float64] | float
aircraft_mass: npt.NDArray[np.floating] | float
if takeoff_mass is not None:
aircraft_mass = takeoff_mass
else:
Expand Down Expand Up @@ -411,14 +411,14 @@ def calculate_aircraft_performance(
self,
*,
aircraft_type: str,
altitude_ft: npt.NDArray[np.float64],
air_temperature: npt.NDArray[np.float64],
altitude_ft: npt.NDArray[np.floating],
air_temperature: npt.NDArray[np.floating],
time: npt.NDArray[np.datetime64] | None,
true_airspeed: npt.NDArray[np.float64] | float | None,
aircraft_mass: npt.NDArray[np.float64] | float,
engine_efficiency: npt.NDArray[np.float64] | float | None,
fuel_flow: npt.NDArray[np.float64] | float | None,
thrust: npt.NDArray[np.float64] | float | None,
true_airspeed: npt.NDArray[np.floating] | float | None,
aircraft_mass: npt.NDArray[np.floating] | float,
engine_efficiency: npt.NDArray[np.floating] | float | None,
fuel_flow: npt.NDArray[np.floating] | float | None,
thrust: npt.NDArray[np.floating] | float | None,
q_fuel: float,
**kwargs: Any,
) -> AircraftPerformanceData:
Expand All @@ -441,24 +441,24 @@ def calculate_aircraft_performance(
----------
aircraft_type : str
Used to query the underlying model database for aircraft engine parameters.
altitude_ft : npt.NDArray[np.float64]
altitude_ft : npt.NDArray[np.floating]
Altitude at each waypoint, [:math:`ft`]
air_temperature : npt.NDArray[np.float64]
air_temperature : npt.NDArray[np.floating]
Ambient temperature for each waypoint, [:math:`K`]
time: npt.NDArray[np.datetime64] | None
Waypoint time in ``np.datetime64`` format. If None, only drag force
will is used in thrust calculations (ie, no vertical change and constant
horizontal change). In addition, aircraft is assumed to be in cruise.
true_airspeed : npt.NDArray[np.float64] | float | None
true_airspeed : npt.NDArray[np.floating] | float | None
True airspeed for each waypoint, [:math:`m s^{-1}`].
If None, a nominal value is used.
aircraft_mass : npt.NDArray[np.float64] | float
aircraft_mass : npt.NDArray[np.floating] | float
Aircraft mass for each waypoint, [:math:`kg`].
engine_efficiency : npt.NDArray[np.float64] | float | None
engine_efficiency : npt.NDArray[np.floating] | float | None
Override the engine efficiency at each waypoint.
fuel_flow : npt.NDArray[np.float64] | float | None
fuel_flow : npt.NDArray[np.floating] | float | None
Override the fuel flow at each waypoint, [:math:`kg s^{-1}`].
thrust : npt.NDArray[np.float64] | float | None
thrust : npt.NDArray[np.floating] | float | None
Override the thrust setting at each waypoint, [:math: `N`].
q_fuel : float
Lower calorific value (LCV) of fuel, [:math:`J \ kg_{fuel}^{-1}`].
Expand All @@ -471,12 +471,12 @@ def calculate_aircraft_performance(
Derived performance metrics at each waypoint.
"""

def ensure_true_airspeed_on_source(self) -> npt.NDArray[np.float64]:
def ensure_true_airspeed_on_source(self) -> npt.NDArray[np.floating]:
"""Add ``true_airspeed`` field to :attr:`source` data if not already present.
Returns
-------
npt.NDArray[np.float64]
npt.NDArray[np.floating]
True airspeed, [:math:`m s^{-1}`]. If ``true_airspeed`` is already present
on :attr:`source`, this is returned directly. Otherwise, it is calculated
using :meth:`Flight.segment_true_airspeed`.
Expand Down Expand Up @@ -529,30 +529,30 @@ class AircraftPerformanceData:
Parameters
----------
fuel_flow : npt.NDArray[np.float64]
fuel_flow : npt.NDArray[np.floating]
Fuel mass flow rate for each waypoint, [:math:`kg s^{-1}`]
aircraft_mass : npt.NDArray[np.float64]
aircraft_mass : npt.NDArray[np.floating]
Aircraft mass for each waypoint, [:math:`kg`]
true_airspeed : npt.NDArray[np.float64]
true_airspeed : npt.NDArray[np.floating]
True airspeed at each waypoint, [:math: `m s^{-1}`]
fuel_burn: npt.NDArray[np.float64]
fuel_burn: npt.NDArray[np.floating]
Fuel consumption for each waypoint, [:math:`kg`]. Set to an array of
all nan values if it cannot be computed (ie, working with gridpoints).
thrust: npt.NDArray[np.float64]
thrust: npt.NDArray[np.floating]
Thrust force, [:math:`N`]
engine_efficiency: npt.NDArray[np.float64]
engine_efficiency: npt.NDArray[np.floating]
Overall propulsion efficiency for each waypoint
rocd : npt.NDArray[np.float64]
rocd : npt.NDArray[np.floating]
Rate of climb and descent, [:math:`ft min^{-1}`]
"""

fuel_flow: npt.NDArray[np.float64]
aircraft_mass: npt.NDArray[np.float64]
true_airspeed: npt.NDArray[np.float64]
fuel_burn: npt.NDArray[np.float64]
thrust: npt.NDArray[np.float64]
engine_efficiency: npt.NDArray[np.float64]
rocd: npt.NDArray[np.float64]
fuel_flow: npt.NDArray[np.floating]
aircraft_mass: npt.NDArray[np.floating]
true_airspeed: npt.NDArray[np.floating]
fuel_burn: npt.NDArray[np.floating]
thrust: npt.NDArray[np.floating]
engine_efficiency: npt.NDArray[np.floating]
rocd: npt.NDArray[np.floating]


# --------------------------------
Expand Down
16 changes: 8 additions & 8 deletions pycontrails/core/fleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ def to_flight_list(self, copy: bool = True) -> list[Flight]:

def segment_true_airspeed(
self,
u_wind: npt.NDArray[np.float64] | float = 0.0,
v_wind: npt.NDArray[np.float64] | float = 0.0,
u_wind: npt.NDArray[np.floating] | float = 0.0,
v_wind: npt.NDArray[np.floating] | float = 0.0,
smooth: bool = True,
window_length: int = 7,
polyorder: int = 1,
) -> npt.NDArray[np.float64]:
) -> npt.NDArray[np.floating]:
"""Calculate the true airspeed [:math:`m / s`] from the ground speed and horizontal winds.
Because Flight.segment_true_airspeed uses a smoothing pattern, waypoints in :attr:`data`
Expand Down Expand Up @@ -302,7 +302,7 @@ def segment_true_airspeed(
self[key] = v_wind

# Calculate TAS on each flight individually
def calc_tas(fl: Flight) -> npt.NDArray[np.float64]:
def calc_tas(fl: Flight) -> npt.NDArray[np.floating]:
u = fl.get("__u_wind", u_wind)
v = fl.get("__v_wind", v_wind)

Expand All @@ -325,7 +325,7 @@ def calc_tas(fl: Flight) -> npt.NDArray[np.float64]:
return np.concatenate(tas)

@override
def segment_groundspeed(self, *args: Any, **kwargs: Any) -> npt.NDArray[np.float64]:
def segment_groundspeed(self, *args: Any, **kwargs: Any) -> npt.NDArray[np.floating]:
fls = self.to_flight_list(copy=False)
gs = [fl.segment_groundspeed(*args, **kwargs) for fl in fls]
return np.concatenate(gs)
Expand All @@ -337,7 +337,7 @@ def resample_and_fill(self, *args: Any, **kwargs: Any) -> Fleet:
return type(self).from_seq(flights, copy=False, broadcast_numeric=False, attrs=self.attrs)

@override
def segment_length(self) -> npt.NDArray[np.float64]:
def segment_length(self) -> npt.NDArray[np.floating]:
return np.where(self.final_waypoints, np.nan, super().segment_length())

@property
Expand All @@ -346,11 +346,11 @@ def max_distance_gap(self) -> float:
return np.nanmax(self.segment_length()).item()

@override
def segment_azimuth(self) -> npt.NDArray[np.float64]:
def segment_azimuth(self) -> npt.NDArray[np.floating]:
return np.where(self.final_waypoints, np.nan, super().segment_azimuth())

@override
def segment_angle(self) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]:
def segment_angle(self) -> tuple[npt.NDArray[np.floating], npt.NDArray[np.floating]]:
sin_a, cos_a = super().segment_angle()
sin_a[self.final_waypoints] = np.nan
cos_a[self.final_waypoints] = np.nan
Expand Down
Loading

0 comments on commit 9f662ac

Please sign in to comment.