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
use crate::QualityLevel;
use crate::data_array::DataArray;
use crate::fits::read_healpix_column_cached;
use crate::generate_index_map;
use crate::healpix::{
HPX_UNSEEN, HealpixMeta, HealpixOrdering, downgrade_healpix_map,
downgrade_healpix_map_balanced, downgrade_healpix_map_balanced_generic,
downgrade_healpix_map_checkerboard, downgrade_healpix_map_checkerboard_generic,
downgrade_healpix_map_generic, is_seen, read_healpix_meta, target_nside_for_resolution,
};
use crate::rotation::CoordSystem;
use std::str::FromStr;
/// Processed HEALPix data ready for plotting
///
/// Preserves native FITS precision (f32 or f64) throughout the pipeline
/// to avoid unnecessary type conversions. No f32→f64 or f64→f32 conversions occur.
pub struct ProcessedData {
pub map: DataArray,
pub meta: HealpixMeta,
}
/// Load and process HEALPix data from FITS file
pub fn load_and_process_data(
fits_path: &Option<String>,
col: usize,
scale_factor: f64,
width: u32,
verbose: bool,
no_downgrade: bool,
quality: &str,
) -> Result<ProcessedData, String> {
use std::time::Instant;
let Some(new_fits_path) = fits_path else {
let map_vec = generate_index_map(1);
let meta = HealpixMeta {
ordering: HealpixOrdering::Ring,
nside: 1,
coord: CoordSystem::G,
};
return Ok(ProcessedData {
map: DataArray::from_f64(map_vec),
meta,
});
};
// Load metadata
let meta = read_healpix_meta(new_fits_path).ok_or_else(|| {
format!(
"Could not determine HEALPix ordering / NSIDE for file: {}",
new_fits_path
)
})?;
// Load and scale data (Tier 5.2.1: Column caching)
// Note: Zero-valued pixels are treated as masked/unseen pixels.
// This is important for files with explicit masking where 0.0 represents bad/masked data.
// We check for zero values BEFORE scaling, and also preserve existing HPX_UNSEEN values.
let fits_read_start = Instant::now();
let map = read_healpix_column_cached(new_fits_path, col);
let fits_read_time = fits_read_start.elapsed();
let mut map = map;
// Convert zero-valued pixels to HPX_UNSEEN (explicit masking)
// This handles files where 0.0 represents masked/bad data
match &mut map {
DataArray::Float32(v) => {
for val in v.iter_mut() {
if *val == 0.0 {
*val = HPX_UNSEEN as f32;
}
}
}
DataArray::Float64(v) => {
for val in v.iter_mut() {
if *val == 0.0 {
*val = HPX_UNSEEN;
}
}
}
}
// Scale data - works with both f32 and f64 without conversion
match &mut map {
DataArray::Float32(v) => {
let factor = scale_factor as f32;
for val in v.iter_mut() {
if is_seen(*val as f64) {
*val *= factor;
}
}
}
DataArray::Float64(v) => {
for val in v.iter_mut() {
if is_seen(*val) {
*val *= scale_factor;
}
}
}
}
// Apply downgrade for high-resolution maps (unless disabled)
let (final_map, final_meta) = if !no_downgrade && meta.nside > crate::HIGH_RES_NSIDE_THRESHOLD {
let target_nside = target_nside_for_resolution(width as usize, (width / 2) as usize);
if meta.nside > target_nside {
if verbose {
println!(
"Downgrading from nside={} to nside={} for {}x{} output",
meta.nside,
target_nside,
width,
width / 2
);
}
let downgrade_start = Instant::now();
// Parse quality level and select appropriate downsampling algorithm
let quality_level = QualityLevel::from_str(quality).unwrap_or(QualityLevel::Best);
// Downgrade using generic functions that preserve f32/f64 types
let downgraded_map = match &map {
DataArray::Float32(v) => {
// Use generic downsampling for f32 data (no conversion)
let downsampled_f32 = match quality_level {
QualityLevel::Best => downgrade_healpix_map_generic(
v,
meta.nside,
target_nside,
meta.ordering,
),
QualityLevel::Balanced => downgrade_healpix_map_balanced_generic(
v,
meta.nside,
target_nside,
meta.ordering,
),
QualityLevel::Fast => downgrade_healpix_map_checkerboard_generic(
v,
meta.nside,
target_nside,
meta.ordering,
),
};
DataArray::Float32(downsampled_f32)
}
DataArray::Float64(v) => {
// Use old f64 functions for f64 data (backward compatible)
let downsampled_f64 = match quality_level {
QualityLevel::Best => {
downgrade_healpix_map(v, meta.nside, target_nside, meta.ordering)
}
QualityLevel::Balanced => downgrade_healpix_map_balanced(
v,
meta.nside,
target_nside,
meta.ordering,
),
QualityLevel::Fast => downgrade_healpix_map_checkerboard(
v,
meta.nside,
target_nside,
meta.ordering,
),
};
DataArray::Float64(downsampled_f64)
}
};
let downgrade_time = downgrade_start.elapsed();
if verbose {
eprintln!(" FITS read: {:.3}s", fits_read_time.as_secs_f64());
eprintln!(" Downgrade: {:.3}s", downgrade_time.as_secs_f64());
}
(
downgraded_map,
HealpixMeta {
nside: target_nside,
ordering: meta.ordering,
coord: meta.coord,
},
)
} else {
(map, meta)
}
} else {
(map, meta)
};
Ok(ProcessedData {
map: final_map,
meta: final_meta,
})
}
/// Subtract monopole (and optionally dipole) from a HEALPix map.
///
/// Uses least-squares fitting to compute the monopole and dipole components,
/// then subtracts them from the map. This follows the algorithm used in map_editor.
///
/// # Arguments
/// * `map`: Mutable reference to the map data
/// * `meta`: Metadata about the map
/// * `remove_monopole`: If true, remove the monopole
/// * `remove_dipole`: If true, remove the dipole (monopole is always included)
/// * `mask`: Optional mask (1.0 = good pixel, 0.0 = bad). If None, all UNSEEN pixels are masked.
/// * `verbose`: If true, print dipole parameters to stdout
///
/// The fit is performed only on pixels where mask == 1.0 (or not UNSEEN if no mask provided).
#[allow(clippy::collapsible_if, clippy::needless_range_loop)]
pub fn subtract_mono_dipole(
map: &mut [f64],
meta: HealpixMeta,
remove_monopole: bool,
remove_dipole: bool,
mask: Option<&[f64]>,
verbose: bool,
) {
if !remove_monopole && !remove_dipole {
return;
}
let npix = map.len();
let nside = meta.nside;
// Build harmonics: [1.0, x, y, z] for each pixel
// where (x, y, z) is the unit vector in Cartesian coordinates
let mut harmonics = vec![[0.0; 4]; npix];
let mut valid_count = 0;
for i in 0..npix {
// Check if pixel is valid (not UNSEEN and not masked out)
let is_valid = if let Some(m) = mask {
is_seen(map[i]) && m[i] > 0.5
} else {
is_seen(map[i])
};
if is_valid {
// Get pixel spherical coordinates
let (theta, phi) = crate::healpix::pix2ang_ring(nside, i as i64);
// Convert to Cartesian unit vector
let vec = crate::rotation::sph_to_vec(theta, phi);
harmonics[i][0] = 1.0;
harmonics[i][1] = vec[0];
harmonics[i][2] = vec[1];
harmonics[i][3] = vec[2];
valid_count += 1;
}
}
if valid_count == 0 {
if verbose {
eprintln!("Warning: No valid pixels found for dipole/monopole fit");
}
return;
}
// Build the normal equations: A * x = b
// A[j][k] = sum of harmonics[i][j] * harmonics[i][k] for all valid pixels
// b[j] = sum of map[i] * harmonics[i][j] for all valid pixels
#[allow(non_snake_case)]
let mut A = [[0.0; 4]; 4];
let mut b = [0.0; 4];
for i in 0..npix {
if !is_seen(map[i]) {
continue;
}
if let Some(m) = mask {
if m[i] <= 0.5 {
continue;
}
}
for j in 0..4 {
b[j] += map[i] * harmonics[i][j];
for k in j..4 {
A[j][k] += harmonics[i][j] * harmonics[i][k];
}
}
}
// Symmetrize the matrix
for j in 0..4 {
for k in (j + 1)..4 {
A[k][j] = A[j][k];
}
}
// Solve the 4x4 linear system using Gaussian elimination with partial pivoting
let multipoles = solve_4x4(A, b);
if verbose {
println!(
"Monopole/Dipole fit: m={:.6e}, dx={:.6e}, dy={:.6e}, dz={:.6e}",
multipoles[0], multipoles[1], multipoles[2], multipoles[3]
);
// Compute dipole amplitude and direction
let dipole_amp = (multipoles[1] * multipoles[1]
+ multipoles[2] * multipoles[2]
+ multipoles[3] * multipoles[3])
.sqrt();
if dipole_amp > 0.0 {
let lat = (multipoles[3] / dipole_amp).asin().to_degrees();
let lon = multipoles[2].atan2(multipoles[1]).to_degrees();
let lon = if lon < 0.0 { lon + 360.0 } else { lon };
println!(
"Dipole: Amplitude={:.6e}, Longitude={:.2}°, Latitude={:.2}°",
dipole_amp, lon, lat
);
}
}
// Subtract the fit from the map
let num_terms = if remove_dipole { 4 } else { 1 };
for i in 0..npix {
if is_seen(map[i]) {
for j in 0..num_terms {
map[i] -= multipoles[j] * harmonics[i][j];
}
}
}
}
/// Solve a 4x4 linear system Ax = b using Gaussian elimination with partial pivoting.
#[allow(non_snake_case, clippy::needless_range_loop)]
fn solve_4x4(mut A: [[f64; 4]; 4], mut b: [f64; 4]) -> [f64; 4] {
// Forward elimination with partial pivoting
for col in 0..4 {
// Find pivot
let mut pivot_row = col;
for row in (col + 1)..4 {
if A[row][col].abs() > A[pivot_row][col].abs() {
pivot_row = row;
}
}
// Swap rows
if pivot_row != col {
for j in col..4 {
(A[col][j], A[pivot_row][j]) = (A[pivot_row][j], A[col][j]);
}
b.swap(col, pivot_row);
}
// Check for singularity
if A[col][col].abs() < 1e-15 {
return [0.0; 4];
}
// Eliminate column
for row in (col + 1)..4 {
let factor = A[row][col] / A[col][col];
for j in (col + 1)..4 {
A[row][j] -= factor * A[col][j];
}
b[row] -= factor * b[col];
}
}
// Back substitution
let mut x = [0.0; 4];
for i in (0..4).rev() {
x[i] = b[i];
for j in (i + 1)..4 {
x[i] -= A[i][j] * x[j];
}
x[i] /= A[i][i];
}
x
}