Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow deletion of hdus #45

Merged
merged 3 commits into from
Nov 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion fitsio/src/fitsfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ impl FitsFile {
FitsHdu::new(self, hdu_description)
}

pub fn num_hdus(&mut self) -> Result<usize> {
let mut status = 0;
let mut num_hdus = 0;
unsafe {
sys::ffthdu(self.fptr as *mut _, &mut num_hdus, &mut status);
}

check_status(status).map(|_| num_hdus as _)
}

/// Return the list of HDU names
pub fn hdu_names(&mut self) -> Result<Vec<String>> {
let num_hdus = self.num_hdus()?;
let mut result = Vec::with_capacity(num_hdus);
for i in 0..num_hdus {
let hdu = self.hdu(i)?;
let name = hdu.name(self)?;
result.push(name);
}
Ok(result)
}

/// Function to make the HDU the current hdu
fn make_current(&mut self, hdu: &FitsHdu) -> Result<()> {
self.change_hdu(hdu.hdu_num)
Expand Down Expand Up @@ -1307,6 +1329,14 @@ impl FitsHdu {
}
}

/// Read the HDU name
pub fn name(&self, fits_file: &mut FitsFile) -> Result<String> {
let extname = self.read_key(fits_file, "EXTNAME").unwrap_or(
"".to_string(),
);
Ok(extname)
}

/// Read header key
pub fn read_key<T: ReadsKey>(&self, fits_file: &mut FitsFile, name: &str) -> Result<T> {
fits_file.make_current(&self)?;
Expand Down Expand Up @@ -1439,7 +1469,7 @@ impl FitsHdu {
);
}

check_status(status).and_then(|_| Ok(()))
check_status(status).map(|_| ())
}

pub fn insert_column(
Expand Down Expand Up @@ -1584,6 +1614,17 @@ impl FitsHdu {
);
ColumnIterator::new(fits_file)
}

pub fn delete(self, fits_file: &mut FitsFile) -> Result<()> {
fits_file.make_current(&self)?;

let mut status = 0;
let mut curhdu = 0;
unsafe {
sys::ffdhdu(fits_file.fptr as *mut _, &mut curhdu, &mut status);
}
check_status(status).map(|_| ())
}
}


Expand Down Expand Up @@ -1902,6 +1943,24 @@ mod test {
);
}

#[test]
fn fetch_number_of_hdus() {
duplicate_test_file(|filename| {
let mut f = FitsFile::open(filename).unwrap();
let num_hdus = f.num_hdus().unwrap();
assert_eq!(num_hdus, 2);
});
}

#[test]
fn fetch_hdu_names() {
duplicate_test_file(|filename| {
let mut f = FitsFile::open(filename).unwrap();
let hdu_names = f.hdu_names().unwrap();
assert_eq!(hdu_names.as_slice(), &["", "TESTEXT"]);
});
}

#[test]
fn creating_new_image_returns_hdu_object() {
with_temp_file(|filename| {
Expand Down Expand Up @@ -2561,6 +2620,15 @@ mod test {
});
}

#[test]
fn fetch_hdu_name() {
duplicate_test_file(|filename| {
let mut f = FitsFile::open(filename).unwrap();
let hdu = f.hdu("TESTEXT").unwrap();
assert_eq!(hdu.name(&mut f).unwrap(), "TESTEXT".to_string());
});
}

#[test]
fn inserting_columns() {
duplicate_test_file(|filename| {
Expand Down Expand Up @@ -2630,6 +2698,21 @@ mod test {
});
}

#[test]
fn delete_hdu() {
duplicate_test_file(|filename| {
{
let mut f = FitsFile::edit(filename).unwrap();
let hdu = f.hdu("TESTEXT").unwrap();
hdu.delete(&mut f).unwrap();
}

let mut f = FitsFile::open(filename).unwrap();
let hdu_names = f.hdu_names().unwrap();
assert!(!hdu_names.contains(&"TESTEXT".to_string()));
});
}

#[test]
fn deleting_columns_by_number() {
duplicate_test_file(|filename| {
Expand Down