lox-space 0.1.0-alpha.48

The Lox toolbox for space mission analysis and design
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// SPDX-FileCopyrightText: 2026 Helge Eichhorn <git@helgeeichhorn.de>
//
// SPDX-License-Identifier: MPL-2.0

//! Python bindings for ITU-R atmospheric propagation models.

use std::path::PathBuf;
use std::sync::Arc;

use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;

use crate::comms::python::PyDecibel;
use crate::units::python::{PyAngle, PyDistance, PyFrequency, PyPressure, PyTemperature};

use lox_core::units::{Angle, Decibel};
use lox_itur::{EnvironmentalLosses, ItuProvider};

/// An open ITU-R data bundle (`lox-itur-data.npz`).
///
/// Grid-based recommendations (rain, cloud, scintillation, topography, …) are
/// served as methods on this object. Build a bundle once with
/// `cargo run -p lox-itur --bin pack -- <itur-wheel.whl> lox-itur-data.npz`.
#[pyclass(name = "ItuProvider", module = "lox_space", frozen)]
pub struct PyItuProvider(pub Arc<ItuProvider>);

#[pymethods]
impl PyItuProvider {
    /// Opens an ITU-R data bundle.
    ///
    /// Args:
    ///     path: filesystem path to lox-itur-data.npz.
    ///
    /// Raises:
    ///     RuntimeError: if the file is missing, malformed, or the manifest
    ///         version is unsupported.
    #[new]
    fn new(path: PathBuf) -> PyResult<Self> {
        let p = ItuProvider::open(&path).map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(Self(Arc::new(p)))
    }

    /// The upstream `itur` package version this bundle was built from.
    fn upstream_version(&self) -> &str {
        self.0.upstream_version()
    }

    /// Returns the topographic altitude above mean sea level (P.1511-2).
    fn topographic_altitude(&self, lat: PyAngle, lon: PyAngle) -> PyResult<PyDistance> {
        let d = self
            .0
            .topographic_altitude(lat.0, lon.0)
            .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(PyDistance(d))
    }

    /// Returns the annual mean surface temperature (P.1510-1).
    fn surface_mean_temperature(&self, lat: PyAngle, lon: PyAngle) -> PyResult<PyTemperature> {
        let t = self
            .0
            .surface_mean_temperature(lat.0, lon.0)
            .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(PyTemperature(t))
    }

    /// Returns the mean annual rain height (P.839-4).
    fn rain_height(&self, lat: PyAngle, lon: PyAngle) -> PyResult<PyDistance> {
        let d = self
            .0
            .rain_height(lat.0, lon.0)
            .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(PyDistance(d))
    }

    /// Returns the rainfall rate exceeded for a given probability (P.837-7).
    ///
    /// Args:
    ///     lat: Latitude.
    ///     lon: Longitude.
    ///     probability: Exceedance probability (% of average year).
    ///
    /// Returns:
    ///     Rainfall rate in mm/h.
    fn rainfall_rate(&self, lat: PyAngle, lon: PyAngle, probability: f64) -> PyResult<f64> {
        self.0
            .rainfall_rate(lat.0, lon.0, probability)
            .map_err(|e| PyRuntimeError::new_err(e.to_string()))
    }

    /// Computes rain attenuation exceeded for a given probability (P.618-13).
    ///
    /// Args:
    ///     lat: Latitude.
    ///     lon: Longitude.
    ///     frequency: Frequency.
    ///     elevation: Elevation angle.
    ///     probability: Exceedance probability (% of average year).
    ///     polarisation_tilt: Polarisation tilt angle (default 45 deg for circular).
    ///     station_altitude: Station altitude (default: looked up from P.1511).
    ///
    /// Returns:
    ///     Rain attenuation.
    #[allow(clippy::too_many_arguments)]
    #[pyo3(signature = (lat, lon, frequency, elevation, probability, polarisation_tilt=None, station_altitude=None))]
    fn rain_attenuation(
        &self,
        lat: PyAngle,
        lon: PyAngle,
        frequency: PyFrequency,
        elevation: PyAngle,
        probability: f64,
        polarisation_tilt: Option<PyAngle>,
        station_altitude: Option<PyDistance>,
    ) -> PyResult<PyDecibel> {
        let tau = polarisation_tilt
            .map(|a| a.0)
            .unwrap_or(Angle::degrees(45.0));
        let d = self
            .0
            .rain_attenuation(
                lat.0,
                lon.0,
                frequency.0,
                elevation.0,
                probability,
                tau,
                station_altitude.map(|s| s.0),
            )
            .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(PyDecibel(d))
    }

    /// Computes cloud attenuation on a slant path (P.840-9).
    ///
    /// Args:
    ///     lat: Latitude.
    ///     lon: Longitude.
    ///     elevation: Elevation angle.
    ///     frequency: Frequency.
    ///     probability: Exceedance probability (% of average year).
    ///
    /// Returns:
    ///     Cloud attenuation.
    fn cloud_attenuation(
        &self,
        lat: PyAngle,
        lon: PyAngle,
        elevation: PyAngle,
        frequency: PyFrequency,
        probability: f64,
    ) -> PyResult<PyDecibel> {
        let v = self
            .0
            .cloud_attenuation(lat.0, lon.0, elevation.0, frequency.0, probability)
            .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(PyDecibel(Decibel::new(v)))
    }

    /// Computes tropospheric scintillation fade depth exceeded for a given
    /// probability (P.618-13).
    ///
    /// Args:
    ///     frequency: Frequency.
    ///     elevation: Elevation angle.
    ///     probability: Exceedance probability (% of average year).
    ///     diameter: Physical antenna diameter.
    ///     eta: Antenna efficiency (default 0.5).
    ///     lat: Latitude (for N_wet lookup).
    ///     lon: Longitude (for N_wet lookup).
    ///
    /// Returns:
    ///     Scintillation attenuation.
    #[allow(clippy::too_many_arguments)]
    #[pyo3(signature = (frequency, elevation, probability, diameter, eta=0.5, lat=None, lon=None))]
    fn scintillation_attenuation(
        &self,
        frequency: PyFrequency,
        elevation: PyAngle,
        probability: f64,
        diameter: PyDistance,
        eta: f64,
        lat: Option<PyAngle>,
        lon: Option<PyAngle>,
    ) -> PyResult<PyDecibel> {
        let lat_angle = lat.map(|a| a.0).unwrap_or(Angle::degrees(0.0));
        let lon_angle = lon.map(|a| a.0).unwrap_or(Angle::degrees(0.0));
        let d = self
            .0
            .scintillation_attenuation(
                frequency.0,
                elevation.0,
                probability,
                diameter.0,
                eta,
                None,
                lat_angle,
                lon_angle,
            )
            .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(PyDecibel(d))
    }

    /// Computes atmospheric attenuation on a slant path, returning individual
    /// contributions as an `EnvironmentalLosses` object.
    ///
    /// Args:
    ///     lat: Latitude.
    ///     lon: Longitude.
    ///     frequency: Frequency.
    ///     elevation: Elevation angle.
    ///     probability: Exceedance probability (% of average year).
    ///     diameter: Physical antenna diameter.
    ///     polarisation_tilt: Polarisation tilt angle (default 45 deg for circular).
    ///
    /// Returns:
    ///     EnvironmentalLosses with rain, gaseous, cloud, scintillation, and
    ///     depolarization fields populated.
    #[allow(clippy::too_many_arguments)]
    #[pyo3(signature = (lat, lon, frequency, elevation, probability, diameter, polarisation_tilt=None))]
    fn atmospheric_attenuation_slant_path(
        &self,
        lat: PyAngle,
        lon: PyAngle,
        frequency: PyFrequency,
        elevation: PyAngle,
        probability: f64,
        diameter: PyDistance,
        polarisation_tilt: Option<PyAngle>,
    ) -> PyResult<PyEnvironmentalLosses> {
        let tau = polarisation_tilt
            .map(|a| a.0)
            .unwrap_or(Angle::degrees(45.0));
        let losses = EnvironmentalLosses::new(
            &self.0,
            lat.0,
            lon.0,
            frequency.0,
            elevation.0,
            probability,
            diameter.0,
            tau,
        )
        .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(PyEnvironmentalLosses(losses))
    }

    fn __repr__(&self) -> String {
        format!("ItuProvider(upstream={:?})", self.0.upstream_version())
    }
}

/// Atmospheric environmental losses computed from ITU-R models.
#[pyclass(
    name = "EnvironmentalLosses",
    module = "lox_space",
    frozen,
    from_py_object
)]
#[derive(Debug, Clone)]
pub struct PyEnvironmentalLosses(pub EnvironmentalLosses);

#[pymethods]
impl PyEnvironmentalLosses {
    /// Computes atmospheric attenuation on a slant path from ITU-R models.
    ///
    /// Args:
    ///     provider: Open ItuProvider supplying the gridded reference data.
    ///     lat: Latitude.
    ///     lon: Longitude.
    ///     frequency: Frequency.
    ///     elevation: Elevation angle (clamped to >= 5 deg).
    ///     probability: Exceedance probability (% of average year).
    ///     diameter: Physical antenna diameter.
    ///     polarisation_tilt: Polarisation tilt angle (default 45 deg for circular).
    #[new]
    #[allow(clippy::too_many_arguments)]
    #[pyo3(signature = (provider, lat, lon, frequency, elevation, probability, diameter, polarisation_tilt=None))]
    fn new(
        provider: &PyItuProvider,
        lat: PyAngle,
        lon: PyAngle,
        frequency: PyFrequency,
        elevation: PyAngle,
        probability: f64,
        diameter: PyDistance,
        polarisation_tilt: Option<PyAngle>,
    ) -> PyResult<Self> {
        let tau = polarisation_tilt
            .map(|a| a.0)
            .unwrap_or(Angle::degrees(45.0));
        let losses = EnvironmentalLosses::new(
            &provider.0,
            lat.0,
            lon.0,
            frequency.0,
            elevation.0,
            probability,
            diameter.0,
            tau,
        )
        .map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
        Ok(Self(losses))
    }

    /// Returns zero environmental losses.
    #[staticmethod]
    fn none() -> Self {
        Self(EnvironmentalLosses::none())
    }

    /// Creates environmental losses from individual values.
    #[staticmethod]
    #[pyo3(signature = (rain=None, gaseous=None, scintillation=None, atmospheric=None, cloud=None, depolarization=None))]
    fn from_values(
        rain: Option<PyDecibel>,
        gaseous: Option<PyDecibel>,
        scintillation: Option<PyDecibel>,
        atmospheric: Option<PyDecibel>,
        cloud: Option<PyDecibel>,
        depolarization: Option<PyDecibel>,
    ) -> Self {
        Self(EnvironmentalLosses {
            rain: rain.map_or(Decibel::new(0.0), |d| d.0),
            gaseous: gaseous.map_or(Decibel::new(0.0), |d| d.0),
            scintillation: scintillation.map_or(Decibel::new(0.0), |d| d.0),
            atmospheric: atmospheric.map_or(Decibel::new(0.0), |d| d.0),
            cloud: cloud.map_or(Decibel::new(0.0), |d| d.0),
            depolarization: depolarization.map_or(Decibel::new(0.0), |d| d.0),
        })
    }

    /// Returns the total environmental loss.
    fn total(&self) -> PyDecibel {
        PyDecibel(self.0.total())
    }

    /// Rain attenuation.
    #[getter]
    fn rain(&self) -> PyDecibel {
        PyDecibel(self.0.rain)
    }
    /// Gaseous absorption.
    #[getter]
    fn gaseous(&self) -> PyDecibel {
        PyDecibel(self.0.gaseous)
    }
    /// Scintillation loss.
    #[getter]
    fn scintillation(&self) -> PyDecibel {
        PyDecibel(self.0.scintillation)
    }
    /// General atmospheric loss (combined total).
    #[getter]
    fn atmospheric(&self) -> PyDecibel {
        PyDecibel(self.0.atmospheric)
    }
    /// Cloud attenuation.
    #[getter]
    fn cloud(&self) -> PyDecibel {
        PyDecibel(self.0.cloud)
    }
    /// Depolarization loss.
    #[getter]
    fn depolarization(&self) -> PyDecibel {
        PyDecibel(self.0.depolarization)
    }

    fn __eq__(&self, other: &PyEnvironmentalLosses) -> bool {
        self.0.rain.as_f64() == other.0.rain.as_f64()
            && self.0.gaseous.as_f64() == other.0.gaseous.as_f64()
            && self.0.scintillation.as_f64() == other.0.scintillation.as_f64()
            && self.0.atmospheric.as_f64() == other.0.atmospheric.as_f64()
            && self.0.cloud.as_f64() == other.0.cloud.as_f64()
            && self.0.depolarization.as_f64() == other.0.depolarization.as_f64()
    }

    fn __repr__(&self) -> String {
        format!(
            "EnvironmentalLosses(rain={}, gaseous={}, scintillation={}, atmospheric={}, cloud={}, depolarization={})",
            PyDecibel(self.0.rain).__repr__(),
            PyDecibel(self.0.gaseous).__repr__(),
            PyDecibel(self.0.scintillation).__repr__(),
            PyDecibel(self.0.atmospheric).__repr__(),
            PyDecibel(self.0.cloud).__repr__(),
            PyDecibel(self.0.depolarization).__repr__(),
        )
    }
}

/// Computes gaseous attenuation on a slant path (P.676-12 approximate method).
///
/// Args:
///     frequency: Frequency.
///     elevation: Elevation angle.
///     pressure: Surface pressure.
///     rho: Surface water vapour density in g/m³.
///     temperature: Surface temperature.
///
/// Returns:
///     Tuple of (oxygen attenuation, water vapour attenuation).
#[pyfunction]
pub fn gaseous_attenuation_slant_path(
    frequency: PyFrequency,
    elevation: PyAngle,
    pressure: PyPressure,
    rho: f64,
    temperature: PyTemperature,
) -> (PyDecibel, PyDecibel) {
    let (a_o, a_w) = lox_itur::p676::gaseous_attenuation_slant_path(
        frequency.0,
        elevation.0,
        pressure.0,
        rho,
        temperature.0,
    );
    (PyDecibel(a_o), PyDecibel(a_w))
}

/// Computes the specific attenuation from rain (P.838-3).
///
/// Args:
///     rain_rate: Rainfall rate in mm/h.
///     frequency: Frequency.
///     elevation: Elevation angle.
///     polarisation_tilt: Polarisation tilt angle (default 45° for circular).
///
/// Returns:
///     Specific rain attenuation in dB/km.
#[pyfunction]
#[pyo3(signature = (rain_rate, frequency, elevation, polarisation_tilt=None))]
pub fn rain_specific_attenuation(
    rain_rate: f64,
    frequency: PyFrequency,
    elevation: PyAngle,
    polarisation_tilt: Option<PyAngle>,
) -> f64 {
    let tau = polarisation_tilt
        .map(|a| a.0)
        .unwrap_or(Angle::degrees(45.0));
    lox_itur::p838::rain_specific_attenuation(rain_rate, frequency.0, elevation.0, tau)
}

/// Registers the pure-formula ITU-R functions with the Python module.
///
/// Grid-based recommendations are exposed as methods on `ItuProvider`.
pub fn register_itur_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(gaseous_attenuation_slant_path, m)?)?;
    m.add_function(wrap_pyfunction!(rain_specific_attenuation, m)?)?;
    Ok(())
}