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
use std::{fmt::Display, ops::Deref};

use serde::Serialize;

/// Star photometry
///
/// Photometry is available for the following bands: V, R, I, J, H and K
/// ## Example
/// ```
/// use eyepiece::Photometry;
/// let photometry: Photometry = "V".into();
/// ```
#[derive(Debug, Clone, Copy, Serialize)]
pub enum Photometry {
    V(PhotometryData),
    R(PhotometryData),
    I(PhotometryData),
    J(PhotometryData),
    H(PhotometryData),
    K(PhotometryData),
}
impl Deref for Photometry {
    type Target = PhotometryData;

    fn deref(&self) -> &Self::Target {
        match self {
            Photometry::V(p) => p,
            Photometry::R(p) => p,
            Photometry::I(p) => p,
            Photometry::J(p) => p,
            Photometry::H(p) => p,
            Photometry::K(p) => p,
        }
    }
}
/// Photometric data
#[derive(Debug, Clone, Copy, Serialize)]
pub struct PhotometryData {
    pub wavelength: f64,
    zeropoint: f64,
    #[allow(dead_code)]
    spectral_bandwidth: f64,
}
impl Photometry {
    /// Returns the number of photon for the given magnitude
    pub fn n_photon(&self, magnitude: f64) -> f64 {
        self.zeropoint * 10f64.powf(-0.4 * magnitude)
    }
}

/// Astronomical photometric bands
pub struct PhotometricBands<'a>([&'a str; 6]);
impl<'a> Default for PhotometricBands<'a> {
    /// Returns the array `["V", "R", "I", "J", "H", "K"]`
    fn default() -> Self {
        Self(["V", "R", "I", "J", "H", "K"])
    }
}
impl<'a> IntoIterator for PhotometricBands<'a> {
    type Item = &'a str;

    type IntoIter = std::array::IntoIter<&'a str, 6>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl From<&str> for Photometry {
    /// Astronomical photometric bands
    ///
    /// Converts the bands V, R, I, J, H and K into star [Photometry]
    fn from(band: &str) -> Self {
        match band {
            "V" => Photometry::V(PhotometryData {
                wavelength: 0.55e-6,
                zeropoint: 8.97e9,
                spectral_bandwidth: 0.09e-6,
            }),
            "R" => Photometry::R(PhotometryData {
                wavelength: 0.64e-6,
                zeropoint: 10.87e9,
                spectral_bandwidth: 0.15e-6,
            }),
            "I" => Photometry::I(PhotometryData {
                wavelength: 0.79e-6,
                zeropoint: 7.34e9,
                spectral_bandwidth: 0.15e-6,
            }),
            "J" => Photometry::J(PhotometryData {
                wavelength: 1.215e-6,
                zeropoint: 5.16e9,
                spectral_bandwidth: 0.26e-6,
            }),
            "H" => Photometry::H(PhotometryData {
                wavelength: 1.654e-6,
                zeropoint: 2.99e9,
                spectral_bandwidth: 0.29e-6,
            }),
            "K" => Photometry::K(PhotometryData {
                wavelength: 2.179e-6,
                zeropoint: 1.90e9,
                spectral_bandwidth: 0.41e-6,
            }),
            _ => panic!("expected the photometric band: V, R, I, J, H or K, found {band}"),
        }
    }
}
impl From<&String> for Photometry {
    fn from(band: &String) -> Self {
        band.as_str().into()
    }
}
impl From<String> for Photometry {
    fn from(band: String) -> Self {
        band.as_str().into()
    }
}
impl Display for Photometry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Photometry::V(_) => "V",
                Photometry::R(_) => "R",
                Photometry::I(_) => "I",
                Photometry::J(_) => "J",
                Photometry::H(_) => "H",
                Photometry::K(_) => "K",
            }
        )
    }
}