Skip to content

Commit 57b9673

Browse files
committed
Fix >3 axes and reference pixel
1 parent fdc7a1d commit 57b9673

1 file changed

Lines changed: 51 additions & 22 deletions

File tree

src/main.rs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use fitsio::FitsFile;
66
use fitsrs::hdu::header::extension::image::Image;
77
use fitsrs::hdu::header::Header;
88
use rayon::prelude::*;
9-
use wcs::{ImgXY, LonLat, WCS};
9+
use wcs::{LonLat, WCS};
1010

1111
use std::fs::File;
1212
use std::io::BufReader;
@@ -85,9 +85,6 @@ fn make_cutout(
8585
let coord = LonLat::new(ra.to_radians(), dec.to_radians());
8686
let coord_pix = wcs.proj_lonlat(&coord).unwrap();
8787

88-
let coord_ref_pix = ImgXY::new(coord_pix.x(), coord_pix.y());
89-
let coord_ref = wcs.unproj_lonlat(&coord_ref_pix).unwrap();
90-
9188
let x_pix = coord_pix.x().round() as i64;
9289
let y_pix = coord_pix.y().round() as i64;
9390

@@ -121,23 +118,35 @@ fn make_cutout(
121118
let rrange = lim_low_row as usize..lim_up_row as usize;
122119
let crange = lim_low_col as usize..lim_up_col as usize;
123120

124-
let img_desc = ImageDescription {
125-
data_type: ImageType::Float,
126-
dimensions: &[imsize.try_into().unwrap(), imsize.try_into().unwrap()],
121+
let ctype3: std::string::String = hdu.read_key(&mut fptr, "CTYPE3").unwrap_or("".to_string());
122+
let ctype4: std::string::String = hdu.read_key(&mut fptr, "CTYPE4").unwrap_or("".to_string());
123+
124+
let img_desc = if (ctype3.len() > 0) && (ctype4.len() == 0) {
125+
ImageDescription {
126+
data_type: ImageType::Float,
127+
dimensions: &[1, imsize.try_into().unwrap(), imsize.try_into().unwrap()],
128+
}
129+
} else if (ctype3.len() > 0) && (ctype4.len() > 0) {
130+
ImageDescription {
131+
data_type: ImageType::Float,
132+
dimensions: &[1, 1, imsize.try_into().unwrap(), imsize.try_into().unwrap()],
133+
}
134+
} else {
135+
ImageDescription {
136+
data_type: ImageType::Float,
137+
dimensions: &[imsize.try_into().unwrap(), imsize.try_into().unwrap()],
138+
}
127139
};
128140
let mut fptr_new = FitsFile::create(&outfile)
129141
.with_custom_primary(&img_desc)
130142
.open()?;
131143

132-
hdu.write_key(
133-
&mut fptr_new,
134-
"CRVAL1",
135-
coord_ref.lon().to_degrees() + cdelt1.abs() / 2.0,
136-
)?;
137-
hdu.write_key(&mut fptr_new, "CRVAL2", coord_ref.lat().to_degrees())?;
144+
copy_key_if_exists::<i64>("WCSAXES", &hdu, &mut fptr, &mut fptr_new)?;
145+
let crpix1: i64 = hdu.read_key(&mut fptr, "CRPIX1").unwrap_or_else(|_| 0);
146+
let crpix2: i64 = hdu.read_key(&mut fptr, "CRPIX2").unwrap_or_else(|_| 0);
138147

139-
hdu.write_key(&mut fptr_new, "CRPIX1", (imsize as f64 / 2.0).ceil() as u64)?;
140-
hdu.write_key(&mut fptr_new, "CRPIX2", (imsize as f64 / 2.0).ceil() as u64)?;
148+
hdu.write_key(&mut fptr_new, "CRPIX1", crpix1 - lim_low_row)?;
149+
hdu.write_key(&mut fptr_new, "CRPIX2", crpix2 - lim_low_col)?;
141150

142151
hdu.write_key(&mut fptr_new, "CDELT1", cdelt1)?;
143152
hdu.write_key(&mut fptr_new, "CDELT2", cdelt2)?;
@@ -151,18 +160,17 @@ fn make_cutout(
151160
hdu.write_key(&mut fptr_new, "CTYPE1", ctype1)?;
152161
hdu.write_key(&mut fptr_new, "CTYPE2", ctype2)?;
153162

154-
let ctype3: std::string::String = hdu.read_key(&mut fptr, "CTYPE3").unwrap_or("".to_string());
155163
if ctype3.len() > 0 {
156164
hdu.write_key(&mut fptr_new, "CTYPE3", ctype3.clone())?;
157165
}
158166

159-
let ctype4: std::string::String = hdu.read_key(&mut fptr, "CTYPE4").unwrap_or("".to_string());
160167
if ctype4.len() > 0 {
161168
hdu.write_key(&mut fptr_new, "CTYPE4", ctype4.clone())?;
162169
}
163170

164171
copy_key_if_exists::<String>("RADESYS", &hdu, &mut fptr, &mut fptr_new)?;
165172
copy_key_if_exists::<String>("BUNIT", &hdu, &mut fptr, &mut fptr_new)?;
173+
copy_key_if_exists::<f64>("BZERO", &hdu, &mut fptr, &mut fptr_new)?;
166174
copy_key_if_exists::<f64>("LONPOLE", &hdu, &mut fptr, &mut fptr_new)?;
167175
copy_key_if_exists::<f64>("LATPOLE", &hdu, &mut fptr, &mut fptr_new)?;
168176

@@ -179,11 +187,32 @@ fn make_cutout(
179187
cutout_flat = hdu.read_region(&mut fptr, &[&rrange, &crange])?;
180188
}
181189
assert!(cutout_flat.len() == (imsize as usize).pow(2));
182-
hdu.write_region(
183-
&mut fptr_new,
184-
&[&(0..imsize as usize), &(0..imsize as usize)],
185-
&cutout_flat,
186-
)?;
190+
if ctype3.len() > 0 {
191+
if ctype4.len() > 0 {
192+
hdu.write_region(
193+
&mut fptr_new,
194+
&[
195+
&(0..imsize as usize),
196+
&(0..imsize as usize),
197+
&(0..1),
198+
&(0..1),
199+
],
200+
&cutout_flat,
201+
)?;
202+
} else {
203+
hdu.write_region(
204+
&mut fptr_new,
205+
&[&(0..imsize as usize), &(0..imsize as usize), &(0..1)],
206+
&cutout_flat,
207+
)?;
208+
}
209+
} else {
210+
hdu.write_region(
211+
&mut fptr_new,
212+
&[&(0..imsize as usize), &(0..imsize as usize)],
213+
&cutout_flat,
214+
)?;
215+
}
187216
drop(fptr_new);
188217

189218
let mut fptr_new = FitsFile::edit(&outfile).unwrap();

0 commit comments

Comments
 (0)