This example demonstrates the process of saving multiple Trieste Collection
objects to a single file. More specifically, we will transform three multi-extension FITS files into Collection
objects, retaining the FITS header data. The three collections will then be stored into a single Trieste file. The file will then be read and examined. Finally, we show how to convert a Collection
back into a FITS HDUList
.
%pylab notebook
import trieste as tr
from trieste import utils
import astropy.io.fits as fits
Let's load three multi-extension FITS files and put them into a Trieste file...
one = utils.fits_to_collection("one.fits")
two = utils.fits_to_collection("two.fits")
three = utils.fits_to_collection("three.fits")
At this point, we have 3 Trieste Collection
objects (one
, two
, and three
). Verify the type:
type(one)
Alternatively,
two
Create a documentation string / README for the file that we are going to create:
readme = "An example of storing several multi-extension FITS files within a Trieste file."
Create the file:
tr.save("group", {'doc': readme}, one, two, three)
Now, let's see what is in the directory. Note the file sizes:
ls -sh *.fits *.npz
The group.npz
file is considerably smaller than the three FITS files that were used to create it, because it is compressed. Let's try reading the file to see what we put in there...
group = tr.load("group.npz")
group
Let's take a look at some of the Trieste file's properties...
group.readme
group.name
group.print_toc()
There are a few ways to access the contents:
using the name of an object within the file.
indexing into the file.
iterating through the contents.
group['one.fits']
group[0]
for ob in group:
print(ob.name)
The history that led to the creation of the file:
group.print_history()
Now let's look an one of the individual objects within the file:
a = group[0]
a
a.toc
a[0]
This array has a name:
a[0].name
Alternatively, the same array can be fetched from the Collection
by name, instead of by index:
a['SCI']
A = a['SCI']
The Array
object, A
contains a NumPy array and metadata
A.data
The data looks like this:
plt.imshow(A.data, origin='lower')
The FITS header keywords and comments are stored in the metadata of the Array
:
A.metadata
It is possible to unpack a Trieste Collection
and convert it back into a multi-extension FITS file:
hdulist_one = utils.collection_to_fits(one)
hdulist_one.info()