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
//! FITS (Flexible Image Transport System) format reader.
use super::misc::mktag;
use crate::error::{Error, Result};
use crate::tag::Tag;
use crate::value::Value;
pub fn read_fits(data: &[u8]) -> Result<Vec<Tag>> {
if data.len() < 80 || !data.starts_with(b"SIMPLE =") {
return Err(Error::InvalidData("not a FITS file".into()));
}
let mut tags = Vec::new();
// FITS header: 80-byte fixed-width keyword records
let mut pos = 0;
// For CONTINUE: track current tag to append value
let mut continue_tag: Option<String> = None;
let mut continue_val: String = String::new();
while pos + 80 <= data.len() {
let record = &data[pos..pos + 80];
let keyword = crate::encoding::decode_utf8_or_latin1(&record[..8])
.trim_end()
.to_string();
pos += 80;
if keyword == "END" {
break;
}
// Handle CONTINUE keyword
if keyword == "CONTINUE" {
if continue_tag.is_some() {
// Continue value from previous quoted string
let val_raw = crate::encoding::decode_utf8_or_latin1(&record[8..]).to_string();
let (more, cont) = fits_parse_continued_value(&val_raw);
continue_val.push_str(&more);
if !cont {
let tag_name = continue_tag.take().unwrap();
let tag_desc = fits_tag_description(&tag_name);
tags.push(mktag(
"FITS",
&tag_name,
&tag_desc,
Value::String(continue_val.clone()),
));
continue_val.clear();
}
}
continue;
}
// Flush any pending continue
if let Some(tag_name) = continue_tag.take() {
let tag_desc = fits_tag_description(&tag_name);
tags.push(mktag(
"FITS",
&tag_name,
&tag_desc,
Value::String(continue_val.clone()),
));
continue_val.clear();
}
// COMMENT and HISTORY: special handling (no '= ' at position 8)
if keyword == "COMMENT" || keyword == "HISTORY" {
// PrintConv strips leading spaces ($val =~ s/^ +//), value strips trailing.
let val = crate::encoding::decode_utf8_or_latin1(&record[8..])
.trim_end()
.trim_start_matches(' ')
.to_string();
let name = if keyword == "COMMENT" {
"Comment"
} else {
"History"
};
tags.push(mktag("FITS", name, name, Value::String(val)));
continue;
}
// Standard keyword = value
if keyword.is_empty() {
continue;
}
if record.len() <= 10 || record[8] != b'=' {
continue;
}
let val_raw = crate::encoding::decode_utf8_or_latin1(&record[10..]).to_string();
// Parse value: may be quoted string, boolean T/F, or number
let (value, is_continued) = fits_parse_value(&val_raw);
if value.is_empty() {
continue;
}
// Map known keywords, generate names for others
let tag_name = fits_keyword_to_name(&keyword);
// SIMPLE is the FITS magic, not a tag — ExifTool doesn't emit it (it maps
// to an empty name here). Skip any keyword with no tag name.
if tag_name.is_empty() {
continue;
}
let tag_desc = fits_tag_description(&tag_name);
if is_continued {
continue_tag = Some(tag_name);
continue_val = value;
} else {
tags.push(mktag("FITS", &tag_name, &tag_desc, Value::String(value)));
}
}
// Flush pending continue
if let Some(tag_name) = continue_tag.take() {
let tag_desc = fits_tag_description(&tag_name);
tags.push(mktag(
"FITS",
&tag_name,
&tag_desc,
Value::String(continue_val.clone()),
));
}
Ok(tags)
}
/// Parse a FITS value field (columns 11-80 of an 80-char record).
/// Returns (value_string, is_continued) where is_continued means value ends with '&'.
fn fits_parse_value(s: &str) -> (String, bool) {
let s = s.trim_start();
if let Some(inner) = s.strip_prefix('\'') {
// Quoted string: parse until closing quote (doubled quotes are escaped)
let mut result = String::new();
let mut chars = inner.chars().peekable();
loop {
match chars.next() {
None => break,
Some('\'') => {
if chars.peek() == Some(&'\'') {
// Escaped quote
chars.next();
result.push('\'');
} else {
break; // End of string
}
}
Some(c) => result.push(c),
}
}
// Trim trailing spaces from quoted string
let trimmed = result.trim_end().to_string();
let is_cont = trimmed.ends_with('&');
let val = if is_cont {
trimmed[..trimmed.len() - 1].to_string()
} else {
trimmed
};
(val, is_cont)
} else {
// Non-quoted: take everything up to comment marker /
// Remove trailing spaces and comment
let val = s.split('/').next().unwrap_or("").trim().to_string();
// Re-format float exponents: D/E -> e
let val = val.replace(['D', 'E'], "e");
if val.is_empty() {
return (String::new(), false);
}
(val, false)
}
}
/// Parse a FITS CONTINUE value (same format as normal value but starting at column 9).
fn fits_parse_continued_value(s: &str) -> (String, bool) {
fits_parse_value(s)
}
/// Convert a FITS keyword to a tag name (ExifTool naming convention).
/// Known keywords get special names; others get generated from keyword.
fn fits_keyword_to_name(keyword: &str) -> String {
match keyword {
"SIMPLE" => String::new(), // Perl internal only
"BITPIX" => "Bitpix".into(),
"NAXIS" => "Naxis".into(),
"NAXIS1" => "Naxis1".into(),
"NAXIS2" => "Naxis2".into(),
"EXTEND" => "Extend".into(),
"ORIGIN" => "Origin".into(),
"TELESCOP" => "Telescope".into(),
"BACKGRND" => "Background".into(),
"INSTRUME" => "Instrument".into(),
"OBJECT" => "Object".into(),
"OBSERVER" => "Observer".into(),
"DATE" => "CreateDate".into(),
"AUTHOR" => "Creator".into(),
"REFERENC" => "Reference".into(),
"DATE-OBS" => "ObservationDate".into(),
"TIME-OBS" => "ObservationTime".into(),
"DATE-END" => "ObservationDateEnd".into(),
"TIME-END" => "ObservationTimeEnd".into(),
"COMMENT" => "Comment".into(),
"HISTORY" => "History".into(),
_ => {
// Generate name: ucfirst lc tag, remove underscores and capitalize next
let lower = keyword.to_lowercase();
let mut result = String::new();
let mut capitalize_next = true;
for ch in lower.chars() {
if ch == '_' || ch == '-' {
capitalize_next = true;
} else if capitalize_next {
for c in ch.to_uppercase() {
result.push(c);
}
capitalize_next = false;
} else {
result.push(ch);
}
}
result
}
}
}
fn fits_tag_description(name: &str) -> String {
// Generate description by inserting spaces before capitals
let mut desc = String::new();
for ch in name.chars() {
if ch.is_uppercase() && !desc.is_empty() {
desc.push(' ');
}
desc.push(ch);
}
desc
}