|
94 | 94 | } |
95 | 95 | write_final_padding(writer, n_elems * naxis1) |
96 | 96 | } |
| 97 | +// Made for python wrapper |
| 98 | +pub fn write_explicit_skymap_fits_from_parts<R, K, V>( |
| 99 | + mut writer: R, |
| 100 | + depth: u8, |
| 101 | + n_elems: usize, |
| 102 | + it_keys: K, |
| 103 | + it_vals: V, |
| 104 | +) -> Result<(), FitsError> |
| 105 | +where |
| 106 | + R: Write, |
| 107 | + K: Iterator<Item = u64>, |
| 108 | + V: Iterator, |
| 109 | + V::Item: SkyMapValue, |
| 110 | +{ |
| 111 | + let naxis1 = (<u64 as HHash>::FITS_NAXIS1 + V::Item::FITS_NAXIS1) as usize; |
| 112 | + write_explicit_skymap_fits_header_from_parts::<_, u64, V::Item>(&mut writer, depth, n_elems)?; |
| 113 | + let mut n = 0; |
| 114 | + for (h, v) in it_keys.zip(it_vals) { |
| 115 | + writer |
| 116 | + .write_all(h.to_be_bytes().as_ref()) |
| 117 | + .and_then(|()| writer.write_all(v.to_be_bytes().as_ref()))?; |
| 118 | + n += 1; |
| 119 | + } |
| 120 | + if n != n_elems { |
| 121 | + return Err(FitsError::new_custom(format!( |
| 122 | + "Wrong number of HEALPix cells writen at depth {}. Expected: {}. Actual: {}.", |
| 123 | + depth, n_elems, n |
| 124 | + ))); |
| 125 | + } |
| 126 | + write_final_padding(writer, n_elems * naxis1) |
| 127 | +} |
97 | 128 |
|
98 | 129 | /// Possible add blanks at the end of the FITS file to complete the last |
99 | 130 | /// 2880 bytes block. |
@@ -216,15 +247,12 @@ fn write_implicit_skymap_fits_header<R: Write, T: SkyMapValue>( |
216 | 247 | writer.write_all(&header_block[..]).map_err(FitsError::Io) |
217 | 248 | } |
218 | 249 |
|
219 | | -fn write_explicit_skymap_fits_header<'a, R, S>( |
220 | | - mut writer: R, |
221 | | - skymap: &'a S, |
222 | | -) -> Result<(), FitsError> |
| 250 | +fn write_explicit_skymap_fits_header<'a, R, S>(writer: R, skymap: &'a S) -> Result<(), FitsError> |
223 | 251 | where |
224 | 252 | R: Write, |
225 | 253 | S: SkyMap<'a>, |
226 | 254 | { |
227 | | - let nside = nside(skymap.depth()); |
| 255 | + /*let nside = nside(skymap.depth()); |
228 | 256 | let n_cells = skymap.len() as u64; |
229 | 257 |
|
230 | 258 | write_primary_hdu(&mut writer)?; |
@@ -267,5 +295,65 @@ where |
267 | 295 | ); |
268 | 296 | it.next().unwrap()[0..3].copy_from_slice(b"END"); |
269 | 297 | // Do write the header |
| 298 | + writer.write_all(&header_block[..]).map_err(FitsError::Io)*/ |
| 299 | + write_explicit_skymap_fits_header_from_parts::<_, S::HashType, S::ValueType>( |
| 300 | + writer, |
| 301 | + skymap.depth(), |
| 302 | + skymap.len(), |
| 303 | + ) |
| 304 | +} |
| 305 | +fn write_explicit_skymap_fits_header_from_parts<R, K, V>( |
| 306 | + mut writer: R, |
| 307 | + depth: u8, |
| 308 | + n_elems: usize, |
| 309 | +) -> Result<(), FitsError> |
| 310 | +where |
| 311 | + R: Write, |
| 312 | + K: HHash, |
| 313 | + V: SkyMapValue, |
| 314 | +{ |
| 315 | + let nside = nside(depth); |
| 316 | + let n_cells = n_elems as u64; |
| 317 | + |
| 318 | + write_primary_hdu(&mut writer)?; |
| 319 | + let mut header_block = [b' '; 2880]; |
| 320 | + let mut it = header_block.chunks_mut(80); |
| 321 | + // Write BINTABLE specific keywords in the buffer |
| 322 | + it.next().unwrap()[0..20].copy_from_slice(b"XTENSION= 'BINTABLE'"); |
| 323 | + it.next().unwrap()[0..30].copy_from_slice(b"BITPIX = 8"); |
| 324 | + it.next().unwrap()[0..30].copy_from_slice(b"NAXIS = 2"); |
| 325 | + write_uint_mandatory_keyword_record( |
| 326 | + it.next().unwrap(), |
| 327 | + b"NAXIS1 ", |
| 328 | + (K::FITS_NAXIS1 + V::FITS_NAXIS1) as u64, |
| 329 | + ); |
| 330 | + write_uint_mandatory_keyword_record(it.next().unwrap(), b"NAXIS2 ", n_cells); |
| 331 | + it.next().unwrap()[0..30].copy_from_slice(b"PCOUNT = 0"); |
| 332 | + it.next().unwrap()[0..30].copy_from_slice(b"GCOUNT = 1"); |
| 333 | + it.next().unwrap()[0..30].copy_from_slice(b"TFIELDS = 2"); |
| 334 | + it.next().unwrap()[0..20].copy_from_slice(b"TTYPE1 = 'PIXEL '"); |
| 335 | + it.next().unwrap()[0..20].copy_from_slice(b"TTYPE2 = 'VALUE '"); |
| 336 | + write_str_mandatory_keyword_record(it.next().unwrap(), b"TFORM1 ", K::FITS_TFORM); |
| 337 | + write_str_mandatory_keyword_record(it.next().unwrap(), b"TFORM2 ", V::FITS_TFORM); |
| 338 | + it.next().unwrap()[0..20].copy_from_slice(b"PIXTYPE = 'HEALPIX '"); |
| 339 | + it.next().unwrap()[0..20].copy_from_slice(b"INDXSCHM= 'EXPLICIT'"); |
| 340 | + it.next().unwrap()[0..20].copy_from_slice(b"ORDERING= 'NESTED '"); |
| 341 | + it.next().unwrap()[0..20].copy_from_slice(b"COORDSYS= 'C '"); |
| 342 | + it.next().unwrap()[0..20].copy_from_slice(b"EXTNAME = 'xtension'"); |
| 343 | + write_uint_mandatory_keyword_record(it.next().unwrap(), b"NSIDE ", nside as u64); |
| 344 | + write_uint_mandatory_keyword_record(it.next().unwrap(), b"OBS_NPIX", n_cells); |
| 345 | + // it.next().unwrap()[0..28].copy_from_slice(b"CREATOR = 'CDS HEALPix Rust'"); |
| 346 | + write_keyword_record( |
| 347 | + it.next().unwrap(), |
| 348 | + b"CREATOR ", |
| 349 | + format!( |
| 350 | + "'Rust crate {} {}'", |
| 351 | + env!("CARGO_PKG_NAME"), |
| 352 | + env!("CARGO_PKG_VERSION") |
| 353 | + ) |
| 354 | + .as_str(), |
| 355 | + ); |
| 356 | + it.next().unwrap()[0..3].copy_from_slice(b"END"); |
| 357 | + // Do write the header |
270 | 358 | writer.write_all(&header_block[..]).map_err(FitsError::Io) |
271 | 359 | } |
0 commit comments