iridium-units 0.1.0

A high-performance runtime unit-of-measure library for Rust
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Brightness temperature equivalencies for radio astronomy.
//!
//! Brightness temperature is the temperature a blackbody would need to have
//! to produce the observed flux density at a given frequency. It's commonly
//! used in radio astronomy to characterize emission.
//!
//! # Formulas
//!
//! ## Rayleigh-Jeans Approximation (hν << kT)
//!
//! For radio frequencies where the Rayleigh-Jeans approximation is valid:
//!
//! ```text
//! T_b = S_ν × c² / (2k × ν² × Ω)
//! ```
//!
//! Where:
//! - T_b = brightness temperature (K)
//! - S_ν = flux density (W/m²/Hz)
//! - c = speed of light
//! - k = Boltzmann constant
//! - ν = frequency (Hz)
//! - Ω = solid angle (sr)
//!
//! ## Full Planck Function
//!
//! For the general case:
//!
//! ```text
//! B_ν(T) = (2hν³/c²) / (exp(hν/kT) - 1)
//! ```
//!
//! Solving for T requires numerical methods (Newton-Raphson iteration).
//!
//! # Example
//!
//! ```
//! use iridium_units::prelude::*;
//! use iridium_units::equivalencies::brightness_temperature;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Observe 1 Jy at 1.4 GHz with a 1 arcmin² beam
//!     let freq = 1.4e9 * HZ;
//!     let beam = (1.0 / 3600.0_f64.powi(2)) * (std::f64::consts::PI / 180.0).powi(2) * SR;
//!
//!     let flux = 1.0 * JANSKY;
//!     let temp = flux.to_equiv(&K, brightness_temperature(freq, beam))?;
//!     Ok(())
//! }
//! ```

use super::{Converter, Equivalency};
use crate::constants::{BOLTZMANN_CONSTANT, PLANCK_CONSTANT, SPEED_OF_LIGHT};
use crate::dimension::{Dimension, Rational16};
use crate::quantity::Quantity;
use crate::unit::Unit;

// =============================================================================
// Helper Functions
// =============================================================================

/// Check if a unit has flux density dimension (M T⁻²)
/// This is the dimension of W/(m² Hz) = Jansky × 10⁻²⁶
fn is_flux_density(unit: &Unit) -> bool {
    let flux_density_dim = Dimension::MASS.mul(&Dimension::TIME.pow(Rational16::new(-2, 1)));
    unit.dimension() == flux_density_dim
}

/// Check if a unit has temperature dimension
fn is_temperature(unit: &Unit) -> bool {
    unit.dimension() == Dimension::TEMPERATURE
}

/// Check if a unit has spectral radiance dimension (M T⁻² sr⁻¹)
/// This is the dimension of W/(m² Hz sr) = Jansky/sr × 10⁻²⁶
fn is_spectral_radiance(unit: &Unit) -> bool {
    let radiance_dim = Dimension::MASS
        .mul(&Dimension::TIME.pow(Rational16::new(-2, 1)))
        .mul(&Dimension::SOLID_ANGLE.pow(Rational16::new(-1, 1)));
    unit.dimension() == radiance_dim
}

// =============================================================================
// Rayleigh-Jeans Brightness Temperature
// =============================================================================

/// Create a Rayleigh-Jeans brightness temperature equivalency.
///
/// This converts between flux density and brightness temperature using the
/// Rayleigh-Jeans approximation, which is valid when hν << kT (radio frequencies).
///
/// # Arguments
///
/// * `frequency` - The observation frequency
/// * `beam_solid_angle` - The beam solid angle (e.g., telescope beam or source size)
///
/// # Formulas
///
/// ```text
/// T_b = S_ν × c² / (2k × ν² × Ω)
/// S_ν = T_b × 2k × ν² × Ω / c²
/// ```
///
/// # Example
///
/// ```
/// use iridium_units::prelude::*;
/// use iridium_units::equivalencies::brightness_temperature;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // 21 cm hydrogen line at 1.420 GHz
///     let freq = 1.420405751768e9 * HZ;
///     // Small beam solid angle
///     let beam = 1e-6 * SR;
///
///     let flux = 1.0 * JANSKY;
///     let temp = flux.to_equiv(&K, brightness_temperature(freq, beam))?;
///     Ok(())
/// }
/// ```
///
/// # Validity
///
/// The Rayleigh-Jeans approximation is valid when hν << kT, which gives:
/// - T >> hν/k ≈ 0.048 × ν\[GHz\] Kelvin
/// - For 1 GHz: valid for T >> 0.048 K
/// - For 100 GHz: valid for T >> 4.8 K
///
/// For higher frequencies or lower temperatures, use [`brightness_temperature_planck`].
pub fn brightness_temperature(frequency: Quantity, beam_solid_angle: Quantity) -> Equivalency {
    // Extract frequency in Hz (SI)
    let nu_hz = frequency.value() * frequency.unit().scale();

    // Extract beam solid angle in sr (SI)
    let omega_sr = beam_solid_angle.value() * beam_solid_angle.unit().scale();

    // Pre-compute constants
    let c_squared = SPEED_OF_LIGHT * SPEED_OF_LIGHT;
    let two_k = 2.0 * BOLTZMANN_CONSTANT;
    let nu_squared = nu_hz * nu_hz;

    // Conversion factor: c² / (2k × ν² × Ω)
    let flux_to_temp_factor = c_squared / (two_k * nu_squared * omega_sr);

    // Inverse: 2k × ν² × Ω / c²
    let temp_to_flux_factor = two_k * nu_squared * omega_sr / c_squared;

    Equivalency::new("brightness_temperature", move |from, to| {
        // Flux density → Temperature
        if is_flux_density(from) && is_temperature(to) {
            return Some(Converter::new(
                move |s_nu_si| {
                    if s_nu_si < 0.0 {
                        return Err("flux density must be non-negative".to_string());
                    }
                    Ok(s_nu_si * flux_to_temp_factor)
                },
                move |t_b_si| {
                    if t_b_si < 0.0 {
                        return Err("brightness temperature must be non-negative".to_string());
                    }
                    Ok(t_b_si * temp_to_flux_factor)
                },
            ));
        }

        // Temperature → Flux density
        if is_temperature(from) && is_flux_density(to) {
            return Some(Converter::new(
                move |t_b_si| {
                    if t_b_si < 0.0 {
                        return Err("brightness temperature must be non-negative".to_string());
                    }
                    Ok(t_b_si * temp_to_flux_factor)
                },
                move |s_nu_si| {
                    if s_nu_si < 0.0 {
                        return Err("flux density must be non-negative".to_string());
                    }
                    Ok(s_nu_si * flux_to_temp_factor)
                },
            ));
        }

        None
    })
}

/// Create a Rayleigh-Jeans brightness temperature equivalency for spectral radiance.
///
/// This variant converts between spectral radiance (intensity per steradian)
/// and brightness temperature, without requiring a beam solid angle.
///
/// # Arguments
///
/// * `frequency` - The observation frequency
///
/// # Formula
///
/// ```text
/// I_ν = 2kTν²/c²    (Rayleigh-Jeans)
/// T_b = I_ν × c² / (2k × ν²)
/// ```
///
/// # Example
///
/// ```
/// use iridium_units::prelude::*;
/// use iridium_units::equivalencies::brightness_temperature_intensity;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let freq = 1.0e9 * HZ;
///     // Spectral radiance in W/(m² Hz sr)
///     let intensity = 1e-20 * (W / (M.pow(2) * HZ * SR));
///     let temp = intensity.to_equiv(&K, brightness_temperature_intensity(freq))?;
///     Ok(())
/// }
/// ```
pub fn brightness_temperature_intensity(frequency: Quantity) -> Equivalency {
    // Extract frequency in Hz (SI)
    let nu_hz = frequency.value() * frequency.unit().scale();

    // Pre-compute constants
    let c_squared = SPEED_OF_LIGHT * SPEED_OF_LIGHT;
    let two_k = 2.0 * BOLTZMANN_CONSTANT;
    let nu_squared = nu_hz * nu_hz;

    // Conversion factor: c² / (2k × ν²)
    let intensity_to_temp = c_squared / (two_k * nu_squared);

    // Inverse: 2k × ν² / c²
    let temp_to_intensity = two_k * nu_squared / c_squared;

    Equivalency::new("brightness_temperature_intensity", move |from, to| {
        // Spectral radiance → Temperature
        if is_spectral_radiance(from) && is_temperature(to) {
            return Some(Converter::new(
                move |i_nu_si| {
                    if i_nu_si < 0.0 {
                        return Err("spectral radiance must be non-negative".to_string());
                    }
                    Ok(i_nu_si * intensity_to_temp)
                },
                move |t_b_si| {
                    if t_b_si < 0.0 {
                        return Err("brightness temperature must be non-negative".to_string());
                    }
                    Ok(t_b_si * temp_to_intensity)
                },
            ));
        }

        // Temperature → Spectral radiance
        if is_temperature(from) && is_spectral_radiance(to) {
            return Some(Converter::new(
                move |t_b_si| {
                    if t_b_si < 0.0 {
                        return Err("brightness temperature must be non-negative".to_string());
                    }
                    Ok(t_b_si * temp_to_intensity)
                },
                move |i_nu_si| {
                    if i_nu_si < 0.0 {
                        return Err("spectral radiance must be non-negative".to_string());
                    }
                    Ok(i_nu_si * intensity_to_temp)
                },
            ));
        }

        None
    })
}

// =============================================================================
// Full Planck Brightness Temperature
// =============================================================================

/// Create a full Planck brightness temperature equivalency.
///
/// This converts between flux density and brightness temperature using the
/// complete Planck function, valid at all frequencies and temperatures.
///
/// # Arguments
///
/// * `frequency` - The observation frequency
/// * `beam_solid_angle` - The beam solid angle
///
/// # Formula
///
/// The Planck function for spectral radiance is:
///
/// ```text
/// B_ν(T) = (2hν³/c²) / (exp(hν/kT) - 1)
/// ```
///
/// For flux density S_ν = B_ν × Ω, solving for T:
///
/// ```text
/// T = (hν/k) / ln(1 + 2hν³Ω/(c²S_ν))
/// ```
///
/// # Example
///
/// ```
/// use iridium_units::prelude::*;
/// use iridium_units::equivalencies::brightness_temperature_planck;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // Submillimeter observation at 345 GHz
///     let freq = 345.0e9 * HZ;
///     let beam = 1e-8 * SR;  // Small beam
///
///     let flux = 100.0 * JANSKY;
///     let temp = flux.to_equiv(&K, brightness_temperature_planck(freq, beam))?;
///     Ok(())
/// }
/// ```
///
/// # When to Use
///
/// Use the full Planck function when:
/// - Working at submillimeter or infrared frequencies
/// - Brightness temperatures are low (< 10 K for mm wavelengths)
/// - High accuracy is required
///
/// The Rayleigh-Jeans approximation [`brightness_temperature`] is faster and
/// sufficient for most radio astronomy applications.
pub fn brightness_temperature_planck(
    frequency: Quantity,
    beam_solid_angle: Quantity,
) -> Equivalency {
    // Extract frequency in Hz (SI)
    let nu_hz = frequency.value() * frequency.unit().scale();

    // Extract beam solid angle in sr (SI)
    let omega_sr = beam_solid_angle.value() * beam_solid_angle.unit().scale();

    // Pre-compute constants
    let h = PLANCK_CONSTANT;
    let k = BOLTZMANN_CONSTANT;
    let c = SPEED_OF_LIGHT;
    let c_squared = c * c;

    // hν/k - temperature scale for this frequency
    let h_nu_over_k = h * nu_hz / k;

    // 2hν³/c² - Planck function prefactor
    let two_h_nu3_over_c2 = 2.0 * h * nu_hz.powi(3) / c_squared;

    // For flux density: multiply by Ω
    let planck_prefactor_flux = two_h_nu3_over_c2 * omega_sr;

    Equivalency::new("brightness_temperature_planck", move |from, to| {
        // Flux density → Temperature (using full Planck)
        if is_flux_density(from) && is_temperature(to) {
            return Some(Converter::new(
                move |s_nu_si| {
                    if s_nu_si <= 0.0 {
                        return Err(
                            "flux density must be positive for Planck conversion".to_string()
                        );
                    }
                    // T = (hν/k) / ln(1 + 2hν³Ω/(c²S_ν))
                    let x = planck_prefactor_flux / s_nu_si;
                    if x <= 0.0 {
                        return Err("invalid flux density for Planck inversion".to_string());
                    }
                    let ln_arg = 1.0 + x;
                    if ln_arg <= 1.0 {
                        // This shouldn't happen with positive s_nu and positive x
                        return Err("numerical error in Planck inversion".to_string());
                    }
                    Ok(h_nu_over_k / ln_arg.ln())
                },
                move |t_b_si| {
                    if t_b_si <= 0.0 {
                        return Err("brightness temperature must be positive".to_string());
                    }
                    // S_ν = (2hν³Ω/c²) / (exp(hν/kT) - 1)
                    let exp_arg = h_nu_over_k / t_b_si;
                    if exp_arg > 700.0 {
                        // Would overflow; result is essentially zero
                        return Ok(0.0);
                    }
                    let denominator = exp_arg.exp() - 1.0;
                    if denominator <= 0.0 {
                        return Err("numerical error in Planck function".to_string());
                    }
                    Ok(planck_prefactor_flux / denominator)
                },
            ));
        }

        // Temperature → Flux density (using full Planck)
        if is_temperature(from) && is_flux_density(to) {
            return Some(Converter::new(
                move |t_b_si| {
                    if t_b_si <= 0.0 {
                        return Err("brightness temperature must be positive".to_string());
                    }
                    // S_ν = (2hν³Ω/c²) / (exp(hν/kT) - 1)
                    let exp_arg = h_nu_over_k / t_b_si;
                    if exp_arg > 700.0 {
                        return Ok(0.0);
                    }
                    let denominator = exp_arg.exp() - 1.0;
                    if denominator <= 0.0 {
                        return Err("numerical error in Planck function".to_string());
                    }
                    Ok(planck_prefactor_flux / denominator)
                },
                move |s_nu_si| {
                    if s_nu_si <= 0.0 {
                        return Err(
                            "flux density must be positive for Planck conversion".to_string()
                        );
                    }
                    let x = planck_prefactor_flux / s_nu_si;
                    let ln_arg = 1.0 + x;
                    if ln_arg <= 1.0 {
                        return Err("numerical error in Planck inversion".to_string());
                    }
                    Ok(h_nu_over_k / ln_arg.ln())
                },
            ));
        }

        None
    })
}

/// Calculate the temperature at which the Rayleigh-Jeans approximation
/// has a given fractional error.
///
/// The Rayleigh-Jeans approximation has error ≈ hν/(2kT) at low hν/kT.
/// This returns T such that the error is approximately `fractional_error`.
///
/// # Example
///
/// ```
/// use iridium_units::equivalencies::brightness_temperature::rayleigh_jeans_validity_temperature;
///
/// // At what temperature is R-J accurate to 1% at 100 GHz?
/// let t_min = rayleigh_jeans_validity_temperature(100.0e9, 0.01);
/// // t_min ≈ 240 K
/// ```
pub fn rayleigh_jeans_validity_temperature(frequency_hz: f64, fractional_error: f64) -> f64 {
    // Error ≈ hν/(2kT), so T ≈ hν/(2k × error)
    let h_nu = PLANCK_CONSTANT * frequency_hz;
    h_nu / (2.0 * BOLTZMANN_CONSTANT * fractional_error)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::systems::astrophysical::JANSKY;
    use crate::systems::si::{GHZ, HZ, K, M, SR, W};

    fn spectral_radiance_unit() -> Unit {
        // W/(m² Hz sr)
        W / (&M.pow(2) * HZ * SR)
    }

    #[test]
    fn test_rayleigh_jeans_roundtrip() {
        let freq = 1.0e9 * HZ.clone(); // 1 GHz
        let beam = 1e-6 * SR.clone(); // 1 µsr

        let flux = 1.0 * JANSKY.clone();
        let temp = flux
            .to_equiv(&K, brightness_temperature(freq.clone(), beam.clone()))
            .unwrap();

        // Convert back
        let flux_back = temp
            .to_equiv(&JANSKY, brightness_temperature(freq, beam))
            .unwrap();

        assert!((flux_back.value() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_rayleigh_jeans_known_value() {
        // Test against known formula: T_b = S_ν × c² / (2k × ν² × Ω)
        let freq_hz: f64 = 1.0e9; // 1 GHz
        let omega_sr: f64 = 1e-6; // 1 µsr
        let s_nu_jy: f64 = 1.0;
        let s_nu_si = s_nu_jy * 1e-26; // Convert to W/(m² Hz)

        let expected_t = s_nu_si * SPEED_OF_LIGHT.powi(2)
            / (2.0 * BOLTZMANN_CONSTANT * freq_hz.powi(2) * omega_sr);

        let freq = freq_hz * HZ.clone();
        let beam = omega_sr * SR.clone();
        let flux = s_nu_jy * JANSKY.clone();

        let temp = flux
            .to_equiv(&K, brightness_temperature(freq, beam))
            .unwrap();

        assert!((temp.value() - expected_t).abs() / expected_t < 1e-10);
    }

    #[test]
    fn test_planck_vs_rayleigh_jeans_high_temp() {
        // At high temperature, Planck and R-J should agree
        let freq = 1.0e9 * HZ.clone(); // 1 GHz
        let beam = 1e-6 * SR.clone();

        // Start with a high temperature (R-J valid: T >> hν/k ≈ 0.048 K)
        let temp_high = 1000.0 * K.clone();

        let flux_rj = temp_high
            .to_equiv(&JANSKY, brightness_temperature(freq.clone(), beam.clone()))
            .unwrap();
        let flux_planck = temp_high
            .to_equiv(&JANSKY, brightness_temperature_planck(freq, beam))
            .unwrap();

        // Should agree to better than 0.1% at this temperature
        let rel_diff = (flux_rj.value() - flux_planck.value()).abs() / flux_rj.value();
        assert!(
            rel_diff < 0.001,
            "R-J and Planck differ by {} at 1000 K",
            rel_diff
        );
    }

    #[test]
    fn test_planck_low_temp_differs() {
        // At low temperature (relative to hν/k), Planck and R-J should differ
        let freq = 100.0e9 * HZ.clone(); // 100 GHz -> hν/k ≈ 4.8 K
        let beam = 1e-6 * SR.clone();

        // Temperature comparable to hν/k
        let temp_low = 10.0 * K.clone();

        let flux_rj = temp_low
            .to_equiv(&JANSKY, brightness_temperature(freq.clone(), beam.clone()))
            .unwrap();
        let flux_planck = temp_low
            .to_equiv(&JANSKY, brightness_temperature_planck(freq, beam))
            .unwrap();

        // Should differ noticeably (Planck gives less flux than R-J at low T)
        assert!(flux_planck.value() < flux_rj.value());
    }

    #[test]
    fn test_planck_roundtrip() {
        let freq = 345.0e9 * HZ.clone(); // 345 GHz (submm)
        let beam = 1e-8 * SR.clone();

        let flux = 100.0 * JANSKY.clone();
        let temp = flux
            .to_equiv(
                &K,
                brightness_temperature_planck(freq.clone(), beam.clone()),
            )
            .unwrap();

        // Convert back
        let flux_back = temp
            .to_equiv(&JANSKY, brightness_temperature_planck(freq, beam))
            .unwrap();

        assert!((flux_back.value() - 100.0).abs() / 100.0 < 1e-10);
    }

    #[test]
    fn test_intensity_equivalency() {
        let freq = 1.0e9 * HZ.clone();

        // Test with spectral radiance
        let intensity = 1e-20 * spectral_radiance_unit();
        let temp = intensity
            .to_equiv(&K, brightness_temperature_intensity(freq.clone()))
            .unwrap();

        // Convert back
        let intensity_back = temp
            .to_equiv(
                &spectral_radiance_unit(),
                brightness_temperature_intensity(freq),
            )
            .unwrap();

        assert!((intensity_back.value() - 1e-20).abs() / 1e-20 < 1e-10);
    }

    #[test]
    fn test_rj_validity_temperature() {
        // At 100 GHz, for 1% accuracy
        let t_min = rayleigh_jeans_validity_temperature(100.0e9, 0.01);

        // hν/k at 100 GHz ≈ 4.8 K
        // For 1% error: T ≈ hν/(2k × 0.01) ≈ 240 K
        assert!((t_min - 240.0).abs() / 240.0 < 0.1);
    }

    #[test]
    fn test_negative_flux_fails() {
        let freq = 1.0e9 * HZ.clone();
        let beam = 1e-6 * SR.clone();

        let flux = -1.0 * JANSKY.clone();
        let result = flux.to_equiv(&K, brightness_temperature(freq, beam));
        assert!(result.is_err());
    }

    #[test]
    fn test_negative_temp_fails() {
        let freq = 1.0e9 * HZ.clone();
        let beam = 1e-6 * SR.clone();

        let temp = -100.0 * K.clone();
        let result = temp.to_equiv(&JANSKY, brightness_temperature(freq, beam));
        assert!(result.is_err());
    }

    #[test]
    fn test_ghz_frequency() {
        // Test that GHz units work correctly
        let freq = 1.4 * GHZ.clone(); // 1.4 GHz (21 cm line)
        let beam = 1e-6 * SR.clone();

        let flux = 1.0 * JANSKY.clone();
        let temp = flux
            .to_equiv(&K, brightness_temperature(freq, beam))
            .unwrap();

        // Should get a reasonable temperature (not crazy high or low)
        assert!(temp.value() > 0.0);
        assert!(temp.value() < 1e10); // Sanity check
    }
}