Crate fitsio [−] [src]
fitsio
- a thin wrapper around the cfitsio
C library.
This library wraps the low level cfitsio
bindings: fitsio-sys
and provides a more
native experience for rust users.
The main interface to a fits file is FitsFile
. All file manipulation
and reading starts with this class.
Opening a file:
use fitsio::FitsFile; // let filename = ...; let fptr = FitsFile::open(filename).unwrap();
Alternatively a new file can be created on disk with the companion method
create
:
use fitsio::FitsFile; // let filename = ...; let fptr = FitsFile::create(filename).unwrap();
From this point, the current HDU can be queried and changed, or fits header cards can be read or file contents can be read.
To open a fits file in read/write mode (to allow changes to the file), the
edit
must be used. This opens a file which already exists
on disk for editing.
HDU access
HDU information belongs to the FitsHdu
object. HDUs can be fetched by
String
/str
or integer (0-indexed).
The HduInfo
object contains information about the current HDU:
let hdu = fptr.hdu(0).unwrap(); // image HDU if let HduInfo::ImageInfo { shape, .. } = hdu.info { println!("Image is {}-dimensional", shape.len()); println!("Found image with shape {:?}", shape); } // tables if let HduInfo::TableInfo { column_descriptions, num_rows, .. } = hdu.info { println!("Table contains {} rows", num_rows); println!("Table has {} columns", column_descriptions.len()); }
Creating new HDUs
Images
New fits images are created with the create_image
method. This method requires the extension name, and an
ImageDescription
object, which defines the shape and type of
the desired image:
let image_description = ImageDescription { data_type: ImageType::FLOAT_IMG, dimensions: &[100, 100], }; let hdu = fptr.create_image("EXTNAME".to_string(), &image_description).unwrap();
Tables
Similar to creating new images, new tables are created with the
create_table
method. This requires an extension
name, and a slice of ColumnDescription
s:
let first_description = ColumnDescription::new("A") .with_type(ColumnDataType::Int) .create().unwrap(); let second_description = ColumnDescription::new("B") .with_type(ColumnDataType::Long) .create().unwrap(); let descriptions = [first_description, second_description]; let hdu = fptr.create_table("EXTNAME".to_string(), &descriptions).unwrap();
Column descriptions
Columns are described with the
ColumnDescription
struct. This
encapsulates: the name of the column, and the data format.
The fits specification allows scalar or vector columns, and the data format is described the
ColumnDataDescription
struct, which in
turn encapsulates the number of elements per row element (typically 1), the width of the
column (for strings), and the data type, which is one of the
ColumnDataType
members
For the common case of a scalar column, a ColumnDataDescription
object can be constructed
with the scalar
method:
let desc = ColumnDataDescription::scalar(ColumnDataType::Int); assert_eq!(desc.repeat, 1); assert_eq!(desc.width, 1);
Vector columns can be constructed with the vector
method:
let desc = ColumnDataDescription::vector(ColumnDataType::Int, 100); assert_eq!(desc.repeat, 100); assert_eq!(desc.width, 1);
These impl From<...> for String
such that the traditional fits column description string can
be obtained:
let desc = ColumnDataDescription::scalar(ColumnDataType::Int); assert_eq!(String::from(desc), "1J".to_string());
Header keys
Header keys are read through the read_key
function,
and is generic over types that implement the ReadsKey
trait:
let int_value: i64 = fptr.hdu(0).unwrap().read_key(&mut fptr, "INTTEST").unwrap(); // Alternatively let int_value = fptr.hdu(0).unwrap().read_key::<i64>(&mut fptr, "INTTEST").unwrap(); // Or let the compiler infer the types (if possible)
Header cards can be written through the method
write_key
. It takes a key name and value. See the
WritesKey
trait for supported data types.
fptr.hdu(0).unwrap().write_key(&mut fptr, "foo", 1i64).unwrap(); assert_eq!(fptr.hdu(0).unwrap().read_key::<i64>(&mut fptr, "foo").unwrap(), 1i64);
Reading file data
Images
Image data can be read through either
read_section
which reads contiguous pixels
between a start index and end index, or
read_region
which reads rectangular chunks from
the image.
// Read the first 100 pixels let first_row: Vec<i32> = hdu.read_section(&mut fptr, 0, 100).unwrap(); // Read a square section of the image let xcoord = 0..10; let ycoord = 0..10; let chunk: Vec<i32> = hdu.read_region(&mut fptr, &[&ycoord, &xcoord]).unwrap();
Some convenience methods are available for reading rows of the image. This is typically useful as it's an efficient access method:
let start_row = 0; let num_rows = 10; let first_few_rows: Vec<f32> = hdu.read_rows(&mut fptr, start_row, num_rows).unwrap(); // 10 rows of 100 columns assert_eq!(first_few_rows.len(), 1000);
The whole image can also be read into memory:
let image_data: Vec<f32> = hdu.read_image(&mut fptr, ).unwrap(); // 100 rows of 100 columns assert_eq!(image_data.len(), 10_000);
Tables
Columns can be read using the read_col
function,
which can convert data types on the fly. See the ReadsCol
trait for
supported data types.
let integer_data: Vec<i32> = hdu.and_then(|hdu| hdu.read_col(&mut fptr, "intcol")).unwrap();
The columns
method returns an iterator over all of the
columns in a table.
Writing file data
When writing to the file, all methods are attached to the FitsHdu
object to which data is to
be written.
let hdu = fptr.hdu(1);
Images
Image data is written through two methods on the HDU object:
write_section
and
write_region
.
write_section
requires a start index and
end index and data to write. The data parameter needs to be a slice, meaning any contiguous
memory storage method (e.g. Vec
) can be passed.
let data_to_write: Vec<f64> = vec![1.0, 2.0, 3.0]; hdu.write_section(&mut fptr, 0, data_to_write.len(), &data_to_write).unwrap();
write_region
takes a slice of ranges with which
the data is to be written, and the data to write.
let data_to_write: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0]; let ranges = [&(0..1), &(0..1)]; hdu.write_region(&mut fptr, &ranges, &data_to_write).unwrap();
Tables
Inserting columns
Two methods on the HDU object allow for adding new columns: append_column
and insert_column
.
append_column
adds a new column as the last column member, and is generally
preferred as it does not require shifting of data within the file.
let column_description = ColumnDescription::new("abcdefg") .with_type(ColumnDataType::Int) .create().unwrap(); hdu.append_column(&mut fptr, &column_description).unwrap();
Deleting columns
The HDU object has the method delete_column
which removes a column. The
column can either be accessed by integer or name
let newhdu = hdu.delete_column(&mut fptr, "bar").unwrap(); // or let newhdu = hdu.delete_column(&mut fptr, 0).unwrap();
Raw fits file access
If this library does not support the particular use case that is needed, the raw fitsfile
pointer can be accessed:
extern crate fitsio_sys; let fptr = FitsFile::open(filename).unwrap(); /* Find out the number of HDUs in the file */ let mut num_hdus = 0; let mut status = 0; unsafe { let fitsfile = fptr.as_raw(); /* Use the unsafe fitsio-sys low level library to call a function that is possibly not implemented in this crate */ fitsio_sys::ffthdu(fitsfile, &mut num_hdus, &mut status); } assert_eq!(num_hdus, 2);
This (unsafe) pointer can then be used with the underlying fitsio-sys
library directly.
Reexports
pub use self::fitsfile::FitsFile; |
pub use self::fitsfile::FitsHdu; |
pub use self::types::HduInfo; |
pub use self::errors::Error; |
pub use self::errors::Result; |
Modules
columndescription | |
errors | |
fitsfile | |
types |