Skip to content

Commit

Permalink
Merge branch 'iodoc'
Browse files Browse the repository at this point in the history
  • Loading branch information
cvalenzu committed Dec 16, 2016
2 parents dcefe95 + 4391753 commit acde689
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
15 changes: 8 additions & 7 deletions acalib/io/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from acalib.io.fits import save_fits_from_cont,load_fits_to_cont

class Container:
""" Data structure that contains a list of NDData and astropy tables.
"""
Data structure that contains a list of NDData and astropy tables.
It can load from and save to fits file format. To write a fits, please put in "primary"
It can load from and save to FITS file format. To write a FITS, please put in "primary"
the main object, and in nndata and table all the extensions.
"""
def __init__(self):
Expand All @@ -24,30 +25,30 @@ def save_fits(self,path):

def load_fits(path):
"""
Load a fits into a container.
Load a FITS into a container.
Parameters
----------
path : str
Path to fits file in local disk.
Path to FITS file in local disk.
Returns
-------
result: :class:`~acalib.Container` with the fits loaded.
result: :class:`~acalib.Container` with the FITS loaded.
"""
cont=Container()
cont.load_fits(path)
return cont

def save_fits(cont,path):
"""
Save a fits from a container.
Create a new FITS file from a container.
Parameters
----------
cont : :class:`~acalib.Container`
path : str
Path to new fits file to be created from the container.
Path to new FITS file to be created from the container.
"""
cont.save_fits(path)
50 changes: 50 additions & 0 deletions acalib/io/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
import os

def HDU_to_NDData(hdu):
"""
Create an N-dimensional dataset from an HDU component.
Parameters
----------
hdu : HDU object
HDU to transform into an N-dimensional dataset.
Returns
-------
result: numpy.ndarray or astropy.nddata.NDData with data from the HDU object.
"""
data=hdu.data
meta=hdu.header
mask=np.isnan(data)
Expand Down Expand Up @@ -55,10 +67,34 @@ def HDU_to_NDData(hdu):
return ndd.NDData(data, uncertainty=None, mask=mask,wcs=mywcs, meta=meta, unit=bunit)

def HDU_to_Table(hdu):
"""
Create a data table from a HDU component.
Parameters
----------
hdu : HDU object
HDU to transform into a data table.
Returns
-------
result: astropy.table.Table with data from the HDU.
"""
log.warning("FITS Table ---> AstroPy Table not implemented Yet")
#return atable.ATable(data=hdu.data,meta=hdu.header)

def Table_to_HDU(tab):
"""
Create a HDU object from a data table.
Parameters
----------
tab : astropy.table.Table
Table to transform into a HDU object.
Returns
-------
result: HDU object with data from the data table.
"""
#if tab.data.masked:???
# dtmp = [col.filled(None) for col in six.itervalues(self.columns)]
hdu=fits.BinTableHDU.from_columns(np.array(tab))
Expand All @@ -68,6 +104,20 @@ def Table_to_HDU(tab):
return hdu

def NDData_to_HDU(cube,primary=False):
"""
Create a HDU object from an N-dimensional dataset.
Parameters
----------
cube : numpy.ndarray or astropy.nddata.NDData
Astronomical data cube.
primary : bool
Whether to pick the primary or an image HDU.
Returns
-------
result: HDU object with data from the data cube.
"""
header = cube.wcs.to_header()
if primary==True:
hdu = fits.PrimaryHDU(cube.data,header=header)
Expand Down

0 comments on commit acde689

Please sign in to comment.