#NEWLINE# @ data_factory ( label = 'FITS file' , identifier = is_fits , priority = 100 ) #NEWLINE# def fits_reader( source , auto_merge = False , exclude_exts = None , label = None ) : #NEWLINE# #INDENT# "\n    Read in all extensions from a FITS file.\n\n    Parameters\n    ----------\n    source: str or HDUList\n        The pathname to the FITS file.\n        If an HDUList is passed in, simply use that.\n\n    auto_merge: bool\n        Merge extensions that have the same shape\n        and only one has a defined WCS.\n\n    exclude_exts: [hdu, ] or [index, ]\n        List of HDU's to exclude from reading.\n        This can be a list of HDU's or a list\n        of HDU indexes.\n    " #NEWLINE# from astropy.io import fits #NEWLINE# from astropy.table import Table #NEWLINE# exclude_exts = ( exclude_exts or [ ] ) #NEWLINE# if ( not isinstance ( source , fits . hdu . hdulist . HDUList ) ) : #NEWLINE# #INDENT# hdulist = fits . open ( source , ignore_missing_end = True ) #NEWLINE# hdulist . verify ( 'fix' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# hdulist = source #NEWLINE# #UNINDENT# groups = OrderedDict ( ) #NEWLINE# extension_by_shape = OrderedDict ( ) #NEWLINE# if ( label is not None ) : #NEWLINE# #INDENT# label_base = label #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# hdulist_name = hdulist . filename ( ) #NEWLINE# if ( hdulist_name is None ) : #NEWLINE# #INDENT# hdulist_name = 'HDUList' #NEWLINE# #UNINDENT# label_base = basename ( hdulist_name ) . rpartition ( '.' ) [ 0 ] #NEWLINE# if ( not label_base ) : #NEWLINE# #INDENT# label_base = basename ( hdulist_name ) #NEWLINE# #UNINDENT# #UNINDENT# def new_data( ) : #NEWLINE# #INDENT# label = '{0}[{1}]' . format ( label_base , hdu_name ) #NEWLINE# data = Data ( label = label ) #NEWLINE# data . coords = coords #NEWLINE# groups [ hdu_name ] = data #NEWLINE# extension_by_shape [ shape ] = hdu_name #NEWLINE# return data #NEWLINE# #UNINDENT# for ( extnum , hdu ) in enumerate ( hdulist ) : #NEWLINE# #INDENT# hdu_name = ( hdu . name if hdu . name else 'HDU{0}' . format ( extnum ) ) #NEWLINE# if ( ( hdu . data is not None ) and ( hdu . data . size > 0 ) and ( hdu_name not in exclude_exts ) and ( extnum not in exclude_exts ) ) : #NEWLINE# #INDENT# if is_image_hdu ( hdu ) : #NEWLINE# #INDENT# shape = hdu . data . shape #NEWLINE# coords = coordinates_from_header ( hdu . header ) #NEWLINE# if ( ( not auto_merge ) or has_wcs ( coords ) ) : #NEWLINE# #INDENT# data = new_data ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# data = groups [ extension_by_shape [ shape ] ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# data = new_data ( ) #NEWLINE# #UNINDENT# #UNINDENT# data . add_component ( component = hdu . data , label = hdu_name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if is_table_hdu ( hdu ) : #NEWLINE# #INDENT# table = Table ( hdu . data ) #NEWLINE# label = '{0}[{1}]' . format ( label_base , hdu_name ) #NEWLINE# data = Data ( label = label ) #NEWLINE# groups [ hdu_name ] = data #NEWLINE# for column_name in table . columns : #NEWLINE# #INDENT# column = table [ column_name ] #NEWLINE# if ( column . ndim != 1 ) : #NEWLINE# #INDENT# warnings . warn ( "Dropping column '{0}' since it is not 1-dimensional" . format ( column_name ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# component = Component . autotyped ( column , units = column . unit ) #NEWLINE# data . add_component ( component = component , label = column_name ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return [ groups [ idx ] for idx in groups ]