Skip to content

Commit

Permalink
expose ImgXY and LonLat types from wcs/mapproj
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatthieu3 committed Feb 21, 2025
1 parent 6a1b51e commit fa5843f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Example
use std::fs::File;
use std::io::Cursor;
use fitsrs::{Fits, ImageData, HDU, hdu::header::Xtension};
use fitsrs::wcs::{ImgXY, LonLat};

use std::io::{BufReader, Read};

Expand All @@ -93,6 +94,23 @@ while let Some(Ok(hdu)) = hdu_list.next() {

let num_pixels = (naxis2 * naxis1) as usize;

// Try to access the WCS for an HDU image
if let Ok(wcs) = hdu.wcs() {
// Get the lonlat position on the sky of the pixel located at (0, 0) on the image
let xy = ImgXY::new(0.0, 0.0);
let lonlat = wcs
.unproj_lonlat(&xy)
.unwrap();

// Get the pixel position in the image of a sky location
let xy_2 = wcs
.proj_lonlat(&lonlat)
.unwrap();

assert!((xy.x() - xy_2.x()).abs() <= 1e-9);
assert!((xy.y() - xy_2.y()).abs() <= 1e-9);
}

match hdu_list.get_data(&hdu) {
ImageData::U8(it) => {
let data = it.collect::<Vec<_>>();
Expand Down Expand Up @@ -121,13 +139,24 @@ while let Some(Ok(hdu)) = hdu_list.next() {
}
},
HDU::XBinaryTable(hdu) => {
/*let data: Vec<_> = hdu_list
.get_data(&hdu)
.table_data()
// Select specific fields with the select_fields method
.select_fields(&[
ColumnId::Name("mag"),
ColumnId::Name("phot_bp_mean_mag"),
ColumnId::Name("phot_rp_mean_mag"),
])
.collect();
*/
let num_rows = hdu.get_header()
.get_xtension()
.get_num_rows();

let rows = hdu_list.get_data(&hdu)
let rows: Vec<_> = hdu_list
.get_data(&hdu)
.row_iter()
.collect::<Vec<_>>();
.collect();

assert_eq!(num_rows, rows.len());
},
Expand Down
20 changes: 14 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ pub mod card;
pub mod error;
pub mod file;
pub mod fits;
mod wcs;
pub mod wcs;

mod gz;
pub mod hdu;

Expand All @@ -58,6 +59,7 @@ mod tests {
use crate::hdu::data::DataStream;
use crate::hdu::AsyncHDU;
use crate::{FITSFile, ImageData};
use crate::wcs::ImgXY;

use crate::hdu::data::bintable::ColumnId;
use crate::hdu::header::extension::Xtension;
Expand Down Expand Up @@ -236,7 +238,6 @@ mod tests {
fn test_fits_not_fitting_in_memory() {
use std::fs::File;
use std::io::BufReader;

let f = File::open("samples/fits.gsfc.nasa.gov/EUVE.fits").unwrap();
let reader = BufReader::new(f);
let mut hdu_list = Fits::from_reader(reader);
Expand All @@ -248,6 +249,15 @@ mod tests {

let num_pixels = (naxis1 * naxis2) as usize;

// Try to access the WCS on a specific HDU image
if let Ok(wcs) = hdu.wcs() {
// and perform projection/unprojection using that image WCS
let xy = ImgXY::new(0.0, 0.0);
let _lonlat = wcs
.unproj_lonlat(&xy)
.unwrap();
}

match hdu_list.get_data(&hdu) {
ImageData::I16(it) => {
let data = it.collect::<Vec<_>>();
Expand Down Expand Up @@ -310,8 +320,8 @@ mod tests {
let reader = BufReader::new(f);
let mut hdu_list = Fits::from_reader(reader);
let mut data = vec![];
while let Some(Ok(hdu)) = dbg!(hdu_list.next()) {
match dbg!(hdu) {
while let Some(Ok(hdu)) = hdu_list.next() {
match hdu {
HDU::XBinaryTable(hdu) => {
let _ = hdu.get_header().get_xtension();
data = hdu_list.get_data(&hdu).collect::<Vec<_>>();
Expand Down Expand Up @@ -344,8 +354,6 @@ mod tests {
])
.collect();

let data = dbg!(data);

assert_eq!(data.len(), 3 * 52);
}
_ => (),
Expand Down
3 changes: 3 additions & 0 deletions src/wcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::error::Error;
use crate::card::CardValue;
use std::str::FromStr;

pub type ImgXY = wcs::ImgXY;
pub type LonLat = wcs::LonLat;

use wcs::WCS;
use crate::fits::HDU;
use std::convert::TryInto;
Expand Down

0 comments on commit fa5843f

Please sign in to comment.