Esempio n. 1
0
def read(file_name, map_inds=None, feedback=2):
    """Read a map from an image fits file.
    """

    fname_abbr = ku.abbreviate_file_path(file_name)
    if feedback > 0:
        print 'Opening file: ' + fname_abbr
    # Open the fits file.
    hdulist = pyfits.open(file_name)
    history = bf.get_history_header(hdulist[0].header)
    history.add('Read from file.', 'File name: ' + fname_abbr)
    map_list = []

    if map_inds is None:
        map_inds = range(1, len(hdulist))
    elif not hasattr(map_inds, '__iter__'):
        map_inds = (map_inds, )
    elif len(scans) == 0:
        map_inds = range(1, len(hdulist))

    for ii in map_inds:
        data = hdulist[ii].data

        # Set the data attriute
        Map = data_map.DataMap()
        Map.set_data(sp.swapaxes(data, 0, 2))
        # Masked data is stored in FITS files as float('nan')
        Map.data[sp.logical_not(sp.isfinite(Map.data))] = ma.masked
        Map.history = history

        # Set the other fields.
        for field_name in fields:
            if not field_name in hdulist[1].header.keys():
                continue
            value = hdulist[1].header[field_name]
            Map.set_field(field_name, value)
        map_list.append(Map)
    if len(map_list) == 1:
        map_list = map_list[0]

    return map_list
Esempio n. 2
0
def read(file_name, map_inds=None, feedback=2):
    """Read a map from an image fits file.
    """

    fname_abbr = ku.abbreviate_file_path(file_name)
    if feedback > 0:
        print "Opening file: " + fname_abbr
    # Open the fits file.
    hdulist = pyfits.open(file_name)
    history = bf.get_history_header(hdulist[0].header)
    history.add("Read from file.", "File name: " + fname_abbr)
    map_list = []

    if map_inds is None:
        map_inds = range(1, len(hdulist))
    elif not hasattr(map_inds, "__iter__"):
        map_inds = (map_inds,)
    elif len(scans) == 0:
        map_inds = range(1, len(hdulist))

    for ii in map_inds:
        data = hdulist[ii].data

        # Set the data attriute
        Map = data_map.DataMap()
        Map.set_data(sp.swapaxes(data, 0, 2))
        # Masked data is stored in FITS files as float('nan')
        Map.data[sp.logical_not(sp.isfinite(Map.data))] = ma.masked
        Map.history = history

        # Set the other fields.
        for field_name in fields:
            if not field_name in hdulist[1].header.keys():
                continue
            value = hdulist[1].header[field_name]
            Map.set_field(field_name, value)
        map_list.append(Map)
    if len(map_list) == 1:
        map_list = map_list[0]

    return map_list
Esempio n. 3
0
    def read(self, scans=None, IFs=None, force_tuple=False) :
        """Read in data from the fits file.

        This method reads data from the fits file including the files history
        and basically every peice of data that could be needed.  It is done,
        one scan and one IF at a time.  Each scan and IF is returned in an
        instance of the DataBlock class (defined in another module of this
        package).

        Arguments:
            scans: Which scans in the file to be processed.  A list of 
                integers, with 0 corresponding to the lowest numbered scan.
                Default is all of them.
            IFs: Which intermediate frequencies (also called frequency windows)
                to process.  A list of integers with 0 coorsponding to the 
                lowest frequency present. Default is all of them.
                TODO : Overlapping frequency windows stiched together somehow.
            force_tuple: By default, if there is only a single output Data
                Block, it is returned not wraped in a tuple, but if we want to
                loop over the output we can force the output to be a tuple,
                even if it only has one element.

        Returns: Instance of the DataBlock class, or a tuple of these instances
        (if asked for multiple scans and IFs).
        """
        
        # We want scans and IFs to be a sequence of indicies.
        if scans is None :
            scans = range(len(self.scan_set))
        elif not hasattr(scans, '__iter__') :
            scans = (scans, )
        elif len(scans) == 0 :
            scans = range(len(self.scan_set))
        if IFs is None :
            IFs = range(len(self.IF_set))
        elif not hasattr(IFs, '__iter__') :
            IFs = (IFs, )
        elif len(IFs) == 0 :
            IFs = range(len(self.IF_set))
        
        # Sequence of output DataBlock objects.
        output = ()
        for scan_ind in scans :
            for IF_ind in IFs :
                # Choose the appropriate records from the file, get that data.
                inds_sif = self.get_scan_IF_inds(scan_ind, IF_ind)
                Data_sif = db.DataBlock(self.fitsdata.field('DATA')[inds_sif])
                # Masked data is stored in FITS files as float('nan')
                Data_sif.data[sp.logical_not(sp.isfinite(
                                   Data_sif.data))] = ma.masked
                # Now iterate over the fields and add them
                # to the data block.
                for field, axis in fields_and_axes.iteritems() :
                    # See if this fits file has the key we are looking for.
                    if not field in self.fitsdata._names :
                        continue
                    # First get the 'FITS' format string.
                    field_format = self.hdulist[1].columns.formats[
                                self.hdulist[1].columns.names.index(field)]
                    if axis :
                        # From the indices in inds_sif, we only need a
                        # subset: which_data will subscript inds_sif.
                        temp_data = self.fitsdata.field(field)[inds_sif]
                        # For reshaping at the end.
                        field_shape = []
                        for ii, single_axis in enumerate(Data_sif.axes[0:-1]) :
                            # For each axis, slice out all the data except the
                            # stuff we need.
                            which_data = [slice(None)] * 3
                            if single_axis in axis :
                                field_shape.append(Data_sif.dims[ii])
                            else :
                                which_data[ii] = [0]
                            temp_data = temp_data[tuple(which_data)]
                        temp_data.shape = tuple(field_shape)
                        Data_sif.set_field(field, temp_data, axis, field_format)
                    else :
                        Data_sif.set_field(field, self.fitsdata.field(field)
                            [inds_sif[0,0,0]], axis, field_format)
                if hasattr(self, 'history') :
                    Data_sif.history = db.History(self.history)
                else :
                    self.history =bf.get_history_header(self.hdulist[0].header)
                    #self.set_history(Data_sif)
                    fname_abbr = ku.abbreviate_file_path(self.fname)
                    self.history.add('Read from file.', ('File name: ' + 
                                         fname_abbr, ))
                    Data_sif.history = db.History(self.history)
                Data_sif.verify()
                output = output + (Data_sif, )
        if self.feedback > 2 :
            print 'Read finished.'
        if len(output) == 1 and not force_tuple :
            return output[0]
        else :
            return output
Esempio n. 4
0
    def read(self, scans=None, bands=None, force_tuple=False, IFs=None) :
        """Read in data from the fits file.

        This method reads data from the fits file including the files history
        and basically every peice of data that could be needed.  It is done,
        one scan and one IF at a time.  Each scan and IF is returned in an
        instance of the DataBlock class (defined in another module of this
        package).

        Parameters
        ----------
        scans : tuple of integers
            Which scans in the file to be processed.  A list of 
            integers, with 0 corresponding to the lowest numbered scan.
            Default is all of them.
        bands : tuple of integers
            Which intermediate frequencies (also called frequency windows)
            to process.  A list of integers with 0 coorsponding to the 
            lowest frequency present. Default is all of them.
            TODO : Overlapping frequency windows stiched together somehow.
        force_tuple: By default, if there is only a single output Data
            Block, it is returned not wraped in a tuple, but if we want to
            loop over the output we can force the output to be a tuple,
            even if it only has one element.
        IFs : tuple of integers
            Depricated, use `bands`.

        Returns
        -------
        Instance of the DataBlock class, or a tuple of these instances
        (if asked for multiple scans and IFs).
        """
        
        # `bands` and `IFs` is are two names for the same parameter.
        if not bands is None and IFs is None:
            IFs = bands
        # We want scans and IFs to be a sequence of indicies.
        if scans is None :
            scans = range(len(self.scan_set))
        elif not hasattr(scans, '__iter__') :
            scans = (scans, )
        elif len(scans) == 0 :
            scans = range(len(self.scan_set))
        if IFs is None :
            IFs = range(len(self.IF_set))
        elif not hasattr(IFs, '__iter__') :
            IFs = (IFs, )
        elif len(IFs) == 0 :
            IFs = range(len(self.IF_set))
        
        # Sequence of output DataBlock objects.
        output = ()
        for scan_ind in scans :
            for IF_ind in IFs :
                # Choose the appropriate records from the file, get that data.
                inds_sif = self.get_scan_IF_inds(scan_ind, IF_ind)
                Data_sif = db.DataBlock(self.fitsdata.field('DATA')[inds_sif])
                # Masked data is stored in FITS files as float('nan')
                Data_sif.data[sp.logical_not(sp.isfinite(
                                   Data_sif.data))] = ma.masked
                # Now iterate over the fields and add them
                # to the data block.
                for field, axis in fields_and_axes.iteritems() :
                    # See if this fits file has the key we are looking for.
                    try:
                        names = self.fitsdata.names
                    except AttributeError:
                        names = self.fitsdata._names
                    if not field in names :
                        continue
                    # First get the 'FITS' format string.
                    field_format = self.hdulist[1].columns.formats[
                                self.hdulist[1].columns.names.index(field)]
                    if axis :
                        # From the indices in inds_sif, we only need a
                        # subset: which_data will subscript inds_sif.
                        temp_data = self.fitsdata.field(field)[inds_sif]
                        # For reshaping at the end.
                        field_shape = []
                        for ii, single_axis in enumerate(Data_sif.axes[0:-1]) :
                            # For each axis, slice out all the data except the
                            # stuff we need.
                            which_data = [slice(None)] * 3
                            if single_axis in axis :
                                field_shape.append(Data_sif.dims[ii])
                            else :
                                which_data[ii] = [0]
                            temp_data = temp_data[tuple(which_data)]
                        temp_data.shape = tuple(field_shape)
                        Data_sif.set_field(field, temp_data, axis, field_format)
                    else :
                        Data_sif.set_field(field, self.fitsdata.field(field)
                            [inds_sif[0,0,0]], axis, field_format)
                if hasattr(self, 'history') :
                    Data_sif.history = db.History(self.history)
                else :
                    self.history =bf.get_history_header(self.hdulist[0].header)
                    #self.set_history(Data_sif)
                    fname_abbr = ku.abbreviate_file_path(self.fname)
                    self.history.add('Read from file.', ('File name: ' + 
                                         fname_abbr, ))
                    Data_sif.history = db.History(self.history)
                Data_sif.verify()
                output = output + (Data_sif, )
        if self.feedback > 2 :
            print 'Read finished.'
        if len(output) == 1 and not force_tuple :
            return output[0]
        else :
            return output