astro-metadata 0.3.0

Metadata handling for astronomical images
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
//! Parser for FITS file headers
//!
//! This module provides functions to extract metadata from FITS file headers
//! and convert it into the AstroMetadata structure.

use anyhow::{Context, Result};
use chrono::{DateTime, NaiveDateTime, Utc};
use fitsio::FitsFile;
use log::warn;
use std::collections::HashMap;
use std::path::Path;

use super::types::{
    AstroMetadata, Detector, Environment, Equipment, Exposure, Filter, Mount, WcsData,
};

/// Extract metadata from a FITS file path
pub fn extract_metadata_from_path(path: &Path) -> Result<AstroMetadata> {
    let mut fits_file = FitsFile::open(path).context("Failed to open FITS file")?;
    extract_metadata(&mut fits_file)
}

/// Extract metadata from a FITS file
pub fn extract_metadata(fits_file: &mut FitsFile) -> Result<AstroMetadata> {
    let hdu = fits_file.primary_hdu()?;
    let mut metadata = AstroMetadata::default();
    let mut raw_headers = HashMap::new();

    // Extract common FITS header keywords that we're interested in
    let keywords = [
        "TELESCOP", "FOCALLEN", "APERTURE", "INSTRUME", "CAMERA", "PIXSIZE", "XPIXSZ", "NAXIS1",
        "NAXIS2", "XBINNING", "YBINNING", "GAIN", "EGAIN", "RDNOISE", "CCD-TEMP", "CCDTEMP",
        "SET-TEMP", "FILTER", "OBJECT", "RA", "OBJCTRA", "DEC", "OBJCTDEC", "DATE-OBS", "EXPTIME",
        "EXPOSURE", "IMAGETYP", "FRAME",
    ];

    // Read each keyword
    for keyword in &keywords {
        if let Ok(value) = hdu.read_key::<String>(fits_file, keyword) {
            raw_headers.insert(keyword.to_string(), value);
        }
    }

    // Parse equipment information
    parse_equipment(&mut metadata.equipment, &raw_headers);

    // Parse detector information
    parse_detector(&mut metadata.detector, &raw_headers, &hdu.info);

    // Parse filter information
    parse_filter(&mut metadata.filter, &raw_headers);

    // Parse exposure information
    parse_exposure(&mut metadata.exposure, &raw_headers);

    // Parse mount information
    metadata.mount = parse_mount(&raw_headers);

    // Parse environment information
    metadata.environment = parse_environment(&raw_headers);

    // Parse WCS information
    metadata.wcs = parse_wcs(&raw_headers);

    // Store raw headers for any fields we didn't explicitly parse
    metadata.raw_headers = raw_headers;

    // Calculate session date
    metadata.calculate_session_date();

    Ok(metadata)
}

/// Parse equipment information from FITS headers
fn parse_equipment(equipment: &mut Equipment, headers: &HashMap<String, String>) {
    equipment.telescope_name = get_string_header(headers, &["TELESCOP"]);
    equipment.focal_length = get_float_header(headers, &["FOCALLEN"]);
    equipment.aperture = get_float_header(headers, &["APERTURE"]);

    // Calculate focal ratio if not directly available
    if equipment.focal_ratio.is_none() {
        if let (Some(focal_length), Some(aperture)) = (equipment.focal_length, equipment.aperture) {
            if aperture > 0.0 {
                equipment.focal_ratio = Some(focal_length / aperture);
            }
        }
    }

    // Try to extract reducer/flattener info from INSTRUME
    if let Some(instrume) = get_string_header(headers, &["INSTRUME"]) {
        if instrume.contains("reducer") || instrume.contains("flattener") {
            equipment.reducer_flattener = Some(instrume);
        }
    }

    equipment.mount_model = get_string_header(headers, &["MOUNT"]);

    // Focuser information
    equipment.focuser_position = get_int_header(headers, &["FOCPOS", "FOCUSPOS"]);
    equipment.focuser_temperature = get_float_header(headers, &["FOCTEMP", "FOCUSTEMP"]);
}

/// Parse detector information from FITS headers
fn parse_detector(
    detector: &mut Detector,
    headers: &HashMap<String, String>,
    hdu_info: &fitsio::hdu::HduInfo,
) {
    detector.camera_name = get_string_header(headers, &["INSTRUME", "CAMERA"]);
    detector.pixel_size = get_float_header(headers, &["PIXSIZE", "XPIXSZ"]);

    // Get dimensions from NAXIS1/NAXIS2 headers
    if let Some(naxis1) = get_int_header(headers, &["NAXIS1"]) {
        detector.width = naxis1 as usize;
    }

    if let Some(naxis2) = get_int_header(headers, &["NAXIS2"]) {
        detector.height = naxis2 as usize;
    }

    // If dimensions are not in headers, try to get them from HDU info
    if detector.width == 0 || detector.height == 0 {
        if let fitsio::hdu::HduInfo::ImageInfo { shape, .. } = hdu_info {
            if shape.len() >= 2 {
                // FITS standard: first dimension is y (height), second is x (width)
                detector.height = shape[0];
                detector.width = shape[1];
            }
        }
    }

    // Binning
    detector.binning_x = get_int_header(headers, &["XBINNING"]).unwrap_or(1) as usize;
    detector.binning_y = get_int_header(headers, &["YBINNING"]).unwrap_or(1) as usize;

    // Camera settings
    detector.gain = get_float_header(headers, &["GAIN", "EGAIN"]);
    detector.offset = get_int_header(headers, &["OFFSET", "CCDOFFST"]);
    detector.readout_mode = get_string_header(headers, &["READOUT", "READOUTM"]);
    detector.usb_limit = get_string_header(headers, &["USBLIMIT", "USBTRFC"]);
    detector.read_noise = get_float_header(headers, &["RDNOISE"]);
    detector.temperature = get_float_header(headers, &["CCD-TEMP", "CCDTEMP"]);
    detector.temp_setpoint = get_float_header(headers, &["CCD-TEMP-SETPOINT", "SET-TEMP"]);
    detector.cooler_power = get_float_header(headers, &["COOL-PWR", "COOLPWR"]);
    detector.cooler_status = get_string_header(headers, &["COOL-STAT", "COOLSTAT"]);
    detector.rotator_angle = get_float_header(headers, &["ROTANG", "ROTPA", "ROTATANG"]);
}

/// Parse filter information from FITS headers
fn parse_filter(filter: &mut Filter, headers: &HashMap<String, String>) {
    filter.name = get_string_header(headers, &["FILTER"]);

    // Try to get filter position
    if let Some(pos_str) = get_string_header(headers, &["FILTERID", "FLTPOS"]) {
        if let Ok(pos) = pos_str.parse::<usize>() {
            filter.position = Some(pos);
        }
    }

    // Filter wavelength is rarely in FITS headers, but we'll check anyway
    filter.wavelength = get_float_header(headers, &["WAVELENG", "WAVELEN"]);
}

/// Parse exposure information from FITS headers
fn parse_exposure(exposure: &mut Exposure, headers: &HashMap<String, String>) {
    exposure.object_name = get_string_header(headers, &["OBJECT"]);

    // Parse coordinates
    exposure.ra = get_float_header(headers, &["RA", "OBJCTRA"]).map(|ra| ra as f64 * 15.0); // Convert hours to degrees
    exposure.dec = get_float_header(headers, &["DEC", "OBJCTDEC"]).map(|dec| dec as f64);

    // Parse date/time
    if let Some(date_str) = get_string_header(headers, &["DATE-OBS"]) {
        exposure.date_obs = parse_date_time(&date_str);
    }

    // Exposure time
    exposure.exposure_time = get_float_header(headers, &["EXPTIME", "EXPOSURE"]);

    // Frame type
    exposure.frame_type = get_string_header(headers, &["IMAGETYP", "FRAME"]);

    // Sequence information
    exposure.sequence_id = get_string_header(headers, &["SEQID", "SEQFILE"]);

    if let Some(frame_num_str) = get_string_header(headers, &["FRAMENUM", "SEQNUM"]) {
        if let Ok(frame_num) = frame_num_str.parse::<usize>() {
            exposure.frame_number = Some(frame_num);
        }
    }

    // Dither offsets
    exposure.dither_offset_x = get_float_header(headers, &["DX", "DITHX"]);
    exposure.dither_offset_y = get_float_header(headers, &["DY", "DITHY"]);

    // Scheduler information
    exposure.project_name = get_string_header(headers, &["PROJECT", "PROJNAME"]);
    exposure.session_id = get_string_header(headers, &["SESSIONID", "SESSID"]);
}

/// Parse mount information from FITS headers
fn parse_mount(headers: &HashMap<String, String>) -> Option<Mount> {
    // Check if we have any mount information
    if !headers.contains_key("PIERSIDE")
        && !headers.contains_key("MFLIP")
        && !headers.contains_key("GUIDERMS")
        && !headers.contains_key("SITELAT")
        && !headers.contains_key("OBSLAT")
    {
        return None;
    }

    let mut mount = Mount {
        pier_side: get_string_header(headers, &["PIERSIDE"]),
        latitude: get_float_header(headers, &["SITELAT", "OBSLAT"]).map(|v| v as f64),
        longitude: get_float_header(headers, &["SITELONG", "OBSLONG"]).map(|v| v as f64),
        height: get_float_header(headers, &["SITEELEV", "OBSELEV"]).map(|v| v as f64),
        guide_camera: get_string_header(headers, &["GUIDECAM"]),
        guide_rms: get_float_header(headers, &["GUIDERMS"]),
        guide_scale: get_float_header(headers, &["GUIDESCALE"]),
        peak_ra_error: get_float_header(headers, &["PEAKRA", "PEAKRAER"]),
        peak_dec_error: get_float_header(headers, &["PEAKDEC", "PEAKDCER"]),
        ..Default::default()
    };

    // Parse meridian flip
    if let Some(mflip_str) = get_string_header(headers, &["MFLIP", "MFOC"]) {
        mount.meridian_flip = Some(mflip_str.to_lowercase() == "true" || mflip_str == "1");
    }

    // Parse dither enabled
    if let Some(dither_str) = get_string_header(headers, &["DITHER"]) {
        mount.dither_enabled = Some(dither_str.to_lowercase() == "true" || dither_str == "1");
    }

    Some(mount)
}

/// Parse environment information from FITS headers
fn parse_environment(headers: &HashMap<String, String>) -> Option<Environment> {
    // Check if we have any environment information
    if !headers.contains_key("AMB_TEMP")
        && !headers.contains_key("HUMIDITY")
        && !headers.contains_key("NINA-VERSION")
        && !headers.contains_key("EKOS-VERSION")
        && !headers.contains_key("SQM")
    {
        return None;
    }

    let mut env = Environment {
        ambient_temp: get_float_header(headers, &["AMB_TEMP", "AMBTEMP"]),
        humidity: get_float_header(headers, &["HUMIDITY"]),
        dew_heater_power: get_float_header(headers, &["DEWPOWER", "DEWPWR"]),
        voltage: get_float_header(headers, &["VOLTAGE", "SYSVOLT"]),
        current: get_float_header(headers, &["CURRENT", "SYSCURR"]),
        sqm: get_float_header(headers, &["SQM", "SQMMAG", "SKYQUAL"]),
        ..Default::default()
    };

    // Software version
    if let Some(nina_ver) = get_string_header(headers, &["NINA-VERSION"]) {
        env.software_version = Some(format!("NINA {}", nina_ver));
    } else if let Some(ekos_ver) = get_string_header(headers, &["EKOS-VERSION"]) {
        env.software_version = Some(format!("EKOS {}", ekos_ver));
    } else if let Some(software) = get_string_header(headers, &["SWCREATE", "SOFTWARE"]) {
        env.software_version = Some(software);
    }

    Some(env)
}

/// Parse WCS information from FITS headers
fn parse_wcs(headers: &HashMap<String, String>) -> Option<WcsData> {
    // Check if we have any WCS information
    if !headers.contains_key("CRPIX1")
        && !headers.contains_key("CRPIX2")
        && !headers.contains_key("CRVAL1")
        && !headers.contains_key("CRVAL2")
    {
        return None;
    }

    let wcs = WcsData {
        // Reference pixel coordinates
        crpix1: get_float_header(headers, &["CRPIX1"]).map(|v| v as f64),
        crpix2: get_float_header(headers, &["CRPIX2"]).map(|v| v as f64),
        // Reference pixel values (usually RA/DEC in degrees)
        crval1: get_float_header(headers, &["CRVAL1"]).map(|v| v as f64),
        crval2: get_float_header(headers, &["CRVAL2"]).map(|v| v as f64),
        // CD matrix elements (transformation matrix)
        cd1_1: get_float_header(headers, &["CD1_1"]).map(|v| v as f64),
        cd1_2: get_float_header(headers, &["CD1_2"]).map(|v| v as f64),
        cd2_1: get_float_header(headers, &["CD2_1"]).map(|v| v as f64),
        cd2_2: get_float_header(headers, &["CD2_2"]).map(|v| v as f64),
        // Coordinate system
        ctype1: get_string_header(headers, &["CTYPE1"]),
        ctype2: get_string_header(headers, &["CTYPE2"]),
        ..Default::default()
    };

    // Note: If we need to store equinox information, we would need to add
    // an equinox field to the WcsData struct

    Some(wcs)
}

/// Helper function to get a string value from headers
fn get_string_header(headers: &HashMap<String, String>, keys: &[&str]) -> Option<String> {
    for key in keys {
        if let Some(value) = headers.get(*key) {
            if !value.is_empty() {
                return Some(value.clone());
            }
        }
    }
    None
}

/// Helper function to get a float value from headers
fn get_float_header(headers: &HashMap<String, String>, keys: &[&str]) -> Option<f32> {
    for key in keys {
        if let Some(value) = headers.get(*key) {
            if let Ok(float_val) = value.parse::<f32>() {
                return Some(float_val);
            }
        }
    }
    None
}

/// Helper function to get an integer value from headers
fn get_int_header(headers: &HashMap<String, String>, keys: &[&str]) -> Option<i32> {
    for key in keys {
        if let Some(value) = headers.get(*key) {
            if let Ok(int_val) = value.parse::<i32>() {
                return Some(int_val);
            }
        }
    }
    None
}

/// Parse sexagesimal format (HH MM SS or DD MM SS) to decimal degrees
///
/// This function converts a string in sexagesimal format (hours/degrees, minutes, seconds)
/// to decimal degrees. It handles both positive and negative values.
///
/// # Examples
///
/// ```
/// use astro_metadata::fits_parser::parse_sexagesimal;
///
/// // Parse right ascension: "12 34 56" (12h 34m 56s)
/// let ra_deg = parse_sexagesimal("12 34 56").map(|ra| ra * 15.0); // Convert hours to degrees
///
/// // Parse declination: "-45 12 34" (-45° 12' 34")
/// let dec_deg = parse_sexagesimal("-45 12 34");
/// ```
pub fn parse_sexagesimal(value: &str) -> Option<f64> {
    let parts: Vec<&str> = value.split_whitespace().collect();
    if parts.len() >= 3 {
        if let (Ok(h), Ok(m), Ok(s)) = (
            parts[0].parse::<f64>(),
            parts[1].parse::<f64>(),
            parts[2].parse::<f64>(),
        ) {
            let sign = if h < 0.0 || value.starts_with('-') {
                -1.0
            } else {
                1.0
            };
            return Some(sign * (h.abs() + m / 60.0 + s / 3600.0));
        }
    }
    None
}

/// Helper function to parse date/time strings
fn parse_date_time(date_str: &str) -> Option<DateTime<Utc>> {
    // Try different date formats
    let formats = [
        "%Y-%m-%dT%H:%M:%S%.fZ", // ISO 8601 with Z suffix
        "%Y-%m-%dT%H:%M:%SZ",    // ISO 8601 with Z suffix, no fractional seconds
        "%Y-%m-%dT%H:%M:%S%.f",  // ISO 8601 with fractional seconds
        "%Y-%m-%dT%H:%M:%S",     // ISO 8601 without fractional seconds
        "%Y-%m-%d %H:%M:%S%.f",  // Space-separated with fractional seconds
        "%Y-%m-%d %H:%M:%S",     // Space-separated without fractional seconds
    ];

    for format in &formats {
        if let Ok(dt) = NaiveDateTime::parse_from_str(date_str, format) {
            return Some(DateTime::from_naive_utc_and_offset(dt, Utc));
        }
    }

    warn!("Failed to parse date string: {}", date_str);
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_sexagesimal() {
        // Test RA format (HH MM SS)
        assert_eq!(parse_sexagesimal("12 30 45"), Some(12.5125));

        // Test DEC format (DD MM SS)
        assert_eq!(parse_sexagesimal("-45 30 15"), Some(-45.50416666666667));

        // Test with zero values
        assert_eq!(parse_sexagesimal("0 0 0"), Some(0.0));

        // Test with invalid input
        assert_eq!(parse_sexagesimal("not a coordinate"), None);
        assert_eq!(parse_sexagesimal("12 30"), None); // Not enough parts
    }
}