Exemplo n.º 1
0
def from_fil(filename):
    """ Create a DataArray from a sigproc filterbank file
    
    Args:
        filename (str): Path to filterbank file.
    
    Returns a DataArray object with mem-mapped filterbank data.
    """
    # Filterbank reading
    from blimpy.io import sigproc

    hdr    = sigproc.read_header(filename)
    hdrlen = sigproc.len_header(filename)
    n_int  = sigproc.calc_n_ints_in_file(filename)
    shape  = (n_int,  hdr['nbeams'], hdr['nchans'])
    data   = np.memmap(filename=filename, dtype='float32', offset=hdrlen, shape=shape)
    
    attrs = {'name': os.path.basename(filename),
             'source': hdr['source_name'],
             'sky_coord':  SkyCoord(hdr['src_raj'], hdr['src_dej'])}
    
    dims  = ('time', 'feed_id', 'frequency')
    scales = {
        'time':      TimeScale('time', hdr['tstart'], hdr['tsamp'], data.shape[0], time_format='mjd', time_delta_format='sec'),
        'feed_id':   DimensionScale('feed_id', 0, 0, data.shape[1], units=''),
        'frequency': DimensionScale('frequency', hdr['fch1'], hdr['foff'], data.shape[2], units='MHz')
    }
    
    d = DataArray(data, dims, scales, attrs)
    return d
def main(hotpotato):
    print("Getting Information from the Fits Header.")

    params_list = ['directory', 'basename', 'filetype']
    fil_params_list = ['filname_withhdr']
    print_params(params_list)

    directory = get_value(hotpotato, 'directory')
    filetype = get_value(hotpotato, 'filetype')

    if filetype == 'psrfits':
        fitsfile = directory + '/' + get_value(hotpotato, 'basename') + '.fits'
        hdulist = fits.open(fitsfile, ignore_missing_end=True)
        # Get Header Info and put it into a dictionary
        primaryDictionary = {}
        subintDictionary = {}
        primaryHeader = hdulist['PRIMARY'].header
        subintHeader = hdulist['SUBINT'].header
        for i in primaryHeader:
            primaryDictionary[i] = primaryHeader[i]
        for j in subintHeader:
            subintDictionary[j] = subintHeader[j]
        # Add headers to input dictionary
        hotpotato.update(primaryDictionary)
        hotpotato.update(subintDictionary)

    elif filetype == 'filterbank':
        dictionary = {}
        filname = get_value(hotpotato, 'filname_withhdr')
        filfile = directory + '/' + filname
        # Get Header Info from filterbank file
        wat = Waterfall(filfile, load_data=False)
        header = wat.header
        for j in header:
            dictionary[str(j)[2:-1]] = header[j]
        dictionary['hdr_size'] = len_header(filfile)
        print('Filterbank Header Info:\n\n')
        print(dictionary)
        hotpotato.update(dictionary)

    return hotpotato
Exemplo n.º 3
0
    def __init__(self,
                 filename,
                 f_start=None,
                 f_stop=None,
                 t_start=None,
                 t_stop=None,
                 load_data=True,
                 max_load=1.):
        """ Constructor.

        Args:
            filename (str): filename of blimpy file.
            f_start (float): start frequency, in MHz
            f_stop (float): stop frequency, in MHz
            t_start (int): start time bin
            t_stop (int): stop time bin
        """
        super(FilReader, self).__init__()

        self.header_keywords_types = sigproc.header_keyword_types

        if filename and os.path.isfile(filename):
            self.filename = filename
            self.load_data = load_data
            self.header = self.read_header()
            self.file_size_bytes = os.path.getsize(self.filename)
            self.idx_data = sigproc.len_header(self.filename)
            self.n_channels_in_file = self.header['nchans']
            self.n_beams_in_file = self.header[
                'nifs']  #Placeholder for future development.
            self.n_pols_in_file = 1  #Placeholder for future development.
            self._n_bytes = int(self.header['nbits'] /
                                8)  #number of bytes per digit.
            self._d_type = self._setup_dtype()
            self._setup_n_ints_in_file()
            self.file_shape = (self.n_ints_in_file, self.n_beams_in_file,
                               self.n_channels_in_file)

            if self.header['foff'] < 0:
                self.f_end = self.header['fch1']
                self.f_begin = self.f_end + self.n_channels_in_file * self.header[
                    'foff']
            else:
                self.f_begin = self.header['fch1']
                self.f_end = self.f_begin + self.n_channels_in_file * self.header[
                    'foff']

            self.t_begin = 0
            self.t_end = self.n_ints_in_file

            #Taking care all the frequencies are assigned correctly.
            self._setup_selection_range(f_start=f_start,
                                        f_stop=f_stop,
                                        t_start=t_start,
                                        t_stop=t_stop,
                                        init=True)
            #Convert input frequencies into what their corresponding channel number would be.
            self._setup_chans()
            #Update frequencies ranges from channel number.
            self._setup_freqs()

            self.freq_axis = 2
            self.time_axis = 0
            self.beam_axis = 1  # Place holder

            #EE ie.
            #           spec = np.squeeze(fil_file.data)
            # set start of data, at real length of header  (future development.)
            #            self.datastart=self.hdrraw.find('HEADER_END')+len('HEADER_END')+self.startsample*self.channels

            #Applying data size limit to load.
            if max_load is not None:
                if max_load > 1.0:
                    logger.warning(
                        'Setting data limit != 1GB, please handle with care!')
                self.MAX_DATA_ARRAY_SIZE = max_load * MAX_DATA_ARRAY_SIZE_UNIT
            else:
                self.MAX_DATA_ARRAY_SIZE = MAX_DATA_ARRAY_SIZE_UNIT

            if self.file_size_bytes > self.MAX_DATA_ARRAY_SIZE:
                self.large_file = True
            else:
                self.large_file = False

            if self.load_data:
                if self.large_file:
                    if self.f_start or self.f_stop or self.t_start or self.t_stop:
                        if self.isheavy():
                            logger.warning(
                                "Selection size of %.2f GB, exceeding our size limit %.2f GB. Instance created, header loaded, but data not loaded, please try another (t,v) selection."
                                % (self._calc_selection_size() /
                                   (1024.**3), self.MAX_DATA_ARRAY_SIZE /
                                   (1024.**3)))
                            self._init_empty_selection()
                        else:
                            self.read_data()
                    else:
                        logger.warning(
                            "The file is of size %.2f GB, exceeding our size limit %.2f GB. Instance created, header loaded, but data not loaded. You could try another (t,v) selection."
                            % (self.file_size_bytes /
                               (1024.**3), self.MAX_DATA_ARRAY_SIZE /
                               (1024.**3)))
                        self._init_empty_selection()
                else:
                    self.read_data()
            else:
                logger.debug("Skipping loading data ...")
                self._init_empty_selection()
        else:
            raise IOError("Need a file to open, please give me one!")
Exemplo n.º 4
0
    def __init__(self, filename,f_start=None, f_stop=None,t_start=None, t_stop=None, load_data=True, max_load=None):
        """ Constructor.

        Args:
            filename (str): filename of blimpy file.
            f_start (float): start frequency, in MHz
            f_stop (float): stop frequency, in MHz
            t_start (int): start time bin
            t_stop (int): stop time bin
            max_load (float): memory limit in gigabytes
        """
        super(FilReader, self).__init__()

        self.header_keywords_types = sigproc.header_keyword_types

        if filename and os.path.isfile(filename):
            self.filename = filename
            self.load_data = load_data
            self.header = self.read_header()
            self.file_size_bytes = os.path.getsize(self.filename)
            self.idx_data = sigproc.len_header(self.filename)
            self.n_channels_in_file  = self.header['nchans']
            self.n_beams_in_file = self.header['nifs'] #Placeholder for future development.
            self.n_pols_in_file = 1 #Placeholder for future development.
            self._n_bytes = int(self.header['nbits'] / 8)  #number of bytes per digit.
            self._d_type = self._setup_dtype()
            self._setup_n_ints_in_file()
            self.file_shape = (self.n_ints_in_file,self.n_beams_in_file,self.n_channels_in_file)

            if self.header['foff'] < 0:
                self.f_end  = self.header['fch1']
                self.f_begin  = self.f_end + self.n_channels_in_file*self.header['foff']
            else:
                self.f_begin  = self.header['fch1']
                self.f_end  = self.f_begin + self.n_channels_in_file*self.header['foff']

            self.t_begin = 0
            self.t_end = self.n_ints_in_file

            #Taking care all the frequencies are assigned correctly.
            self._setup_selection_range(f_start=f_start, f_stop=f_stop, t_start=t_start, t_stop=t_stop, init=True)
            #Convert input frequencies into what their corresponding channel number would be.
            self._setup_chans()
            #Update frequencies ranges from channel number.
            self._setup_freqs()

            self.freq_axis = 2
            self.time_axis = 0
            self.beam_axis = 1  # Place holder

#EE ie.
#           spec = np.squeeze(fil_file.data)
            # set start of data, at real length of header  (future development.)
#            self.datastart=self.hdrraw.find('HEADER_END')+len('HEADER_END')+self.startsample*self.channels

            #Applying data size limit to load.
            if max_load is not None and max_load > 0:
                self.max_data_array_size = max_load * GIGA

            if self.file_size_bytes > self.max_data_array_size:
                self.large_file = True
            else:
                self.large_file = False

            if self.load_data:
                if self.large_file:
                    if self.f_start or self.f_stop or self.t_start or self.t_stop:
                        if self.isheavy():
                            self.warn_memory("Selection", self._calc_selection_size())
                            self._init_empty_selection()
                        else:
                            self.read_data()
                    else:
                        self.warn_memory("File", self.file_size_bytes)
                        self._init_empty_selection()
                else:
                    self.read_data()
            else:
                logger.debug("Skipping loading data ...")
                self._init_empty_selection()
        else:
            raise IOError("Need a file to open, please give me one!")