Read FITS file in rust

Hi, new Rustacean here. How can I read Flexible Image Transport System (FITS) files in rust? I want to read the header, know keywords and extract lists of numerical data. I can open the .fits file like a text file in rust. But I couldn't do the above things. The following code reads fits file like text and gives a long vector.

let mut fits_data:Vec<u32>=Vec::new();
let mut file = std::fs::File::open("filename.fits").unwrap();
file.read_to_end(&mut fits_data);
println!("{:?}", fits_data);

I tried fitsio and fits-rs crate but that did not go well. The documentation link of fits-rs is not working and I did not get the documentation of fitsio.
What is the rust's get help syntax? Is there an equivalent of python's help(module_name) How can I know about the functions, arguments, methods, of the imported crates and know how to use them from the terminal? How can we use any external crate or std library to read fits files and work on its data? simple examples of syntax are most welcome.

You can find the documentation for any crate at the url docs.rs/crate_name. The two crates you mention can be found here:

Looking at the documentation, it seems that fitsio is the most well documented choice.

If you wish to view the documentation locally, another option is to call the command

cargo doc --open

which will build the documentation and open it in your browser. The sidebar has links to all of the crates in your dependency tree. You can also view the documentation for the standard library locally using the command rustup doc --std

1 Like

I mentioned the fitsio version in the cargo.toml and use fitsio; in main.rs. Cargo run gives the following error, even though I have already installed cfitsio using homebrew install cfitsio. -

Compiling fitsio-sys v0.4.0
error: failed to run custom build command for `fitsio-sys v0.4.0`

Caused by:
  process didn't exit successfully: `/home/mangesh/rust_practice/indices/target/debug/build/fitsio-sys-8b5ff3af4e1c4d4b/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=CFITSIO_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=CFITSIO_STATIC
  cargo:rerun-if-env-changed=CFITSIO_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR

  --- stderr

  Cannot find cfitsio on the pkg-config search path.  Consider installing the library for your
  system (e.g. through homebrew, apt-get etc.).  Alternatively if it is installed, then add
  the directory that contains `cfitsio.pc` on your PKG_CONFIG_PATH, e.g.:

  PKG_CONFIG_PATH=<blah> cargo build

Do you have any suggestion?

I was able to build the fits_rs crate and import its modules in the main.rs. There were no simple examples in the doc you provided, that I could understand, to extract information from the .fits file. Can you please tell me the syntax of how can I print keywords and corresponding values in the header, and a list of f64 under column 'wavelength'?

My suggestion is to follow this part of the error message:

Cannot find cfitsio on the pkg-config search path. Consider installing the library for your system (e.g. through homebrew, apt-get etc.). Alternatively if it is installed, then add the directory that contains cfitsio.pc on your PKG_CONFIG_PATH, e.g.:

PKG_CONFIG_PATH=<blah> cargo build

I have no idea what FITS even is, and all I have access to is the same documentation as what you see.

Restarted the laptop and now fitsio is imported. The error went away.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.