Пример #1
0
def test_invalid_files():
    from blimpy.io.file_wrapper import open_file
    # script should error-out if file does not exist
    with pytest.raises(IOError):
        open_file(here + 'file_does_not_exist.h5')

    # script should error-out if file is not a
    # valid blimpy data file
    with pytest.raises(NotImplementedError):
        open_file(here + '/run_tests.sh')
Пример #2
0
def test_file_wrapper_open_h5_file():
    from blimpy.io.file_wrapper import open_file
    h5r = open_file(voyager_h5)

    # check all if branches
    h5r.populate_timestamps(update_header=False)
    h5r.populate_timestamps(update_header=True)

    with pytest.raises(ValueError):
        h5r.read_blob((300, 300, 300, 300), -1)

    # testing to make sure all internal functions are already used
    h5r._setup_selection_range(f_start=3, f_stop=4, t_stop=3, t_start=4)
    h5r._setup_selection_range()

    h5r._init_empty_selection()

    # perform reading again and we make sure that
    # the user-facing functions survive

    h5r.populate_freqs()

    # just arbitrary values
    h5r.calc_n_blobs(300)
    h5r.read_blob((300, 300, 300, 300))

    h5r.calc_n_coarse_chan()
    h5r.calc_n_coarse_chan(1)
Пример #3
0
def test_file_wrapper_open_fil_file():
    from blimpy.io.file_wrapper import open_file
    filr = open_file(voyager_fil)

    #print('voyager_fil header:')
    #filr.read_header()
    filr.calc_n_blobs(300)
    filr.read_blob((300, 300, 300, 300))
Пример #4
0
    def h5_to_xarray(self,
                     path,
                     out='arr',
                     f_start=None,
                     f_stop=None,
                     max_load=1.,
                     out_dir=None,
                     in_blimpy=False,
                     waterfall_obj=None):
        """Converts h5 file to an xarray DataArray"""
        if in_blimpy and waterfall_obj is not None:
            container = waterfall_obj.container
            #container = path.container
        else:
            container = fw.open_file(path,
                                     f_start=f_start,
                                     f_stop=f_stop,
                                     max_load=max_load)
        snr_data = np.asarray(
            [array for array in [data[0] for data in container.data]])
        #snr_data = np.asarray([data[0] for data in container.data])
        timestamps = container.populate_timestamps()
        frequencies = container.populate_freqs()
        header = container.header
        if not in_blimpy:
            del header['DIMENSION_LABELS']
        header['n_ints_in_file'] = container.selection_shape[0]
        dataset = xr.Dataset(data_vars=dict(snr=(['time',
                                                  'freq'], snr_data), ),
                             coords=dict(
                                 frequency=(['freq'], frequencies),
                                 time=timestamps,
                             ),
                             attrs=header)
        if out_dir is not None:
            dataset.to_netcdf(out_dir, format='NETCDF4')

        data_array = xr.DataArray(
            #data=dict(
            #    snr=(['time','freq'], snr_data),
            #    ),
            snr_data,
            coords=[timestamps, frequencies],
            dims=['time', 'freq'],
            attrs=header)
        #coords=dict(
        #    frequency=(['freq'], frequencies),
        #    time=timestamps,
        #    ),
        #attrs=header
        #)
        if out == 'dataset':
            return dataset
        else:
            return data_array
Пример #5
0
def test_file_wrapper_open_file():
    from blimpy.io.file_wrapper import open_file
    open_file(voyager_h5)
    open_file(voyager_fil)

    with pytest.raises(NotImplementedError):
        open_file(here + '/run_tests.sh')
Пример #6
0
def test_file_wrapper_open_h5_file():
    from blimpy.io.file_wrapper import open_file
    h5r = open_file(voyager_h5)

    'check all if branches'
    h5r.populate_timestamps(update_header=False)
    h5r.populate_timestamps(update_header=True)
    # we are missing at least one if-body

    with pytest.raises(ValueError):
        h5r.read_blob((300, 300, 300, 300), -1)

    'testing to make sure all internal functions'
    'are already used'
    # h5r._setup_selection_range(init=False)
    # h5r._setup_selection_range(init=True)
    'h5r.t_begin = 0'
    'h5r.t_end = 4'
    h5r._setup_selection_range(f_start=3, f_stop=4, t_stop=3, t_start=4)
    h5r._setup_selection_range()
    #h5r._setup_selection_range()

    h5r._init_empty_selection()
    # h5r._setup_dtype()
    # we will definitely want data files with different dtypes
    # h5r._calc_selection_size()
    # h5r._calc_selection_shape()
    # h5r._setup_chans()
    # many unused if branches
    #h5r._setup_freqs()

    'Now that I have fiddled with all of these internal'
    'functions, the true test will be to perform reading again'
    'and we make sure that the user-facing functions survive'

    h5r.populate_freqs()
    # we want to test both if cases,
    # but I am working with an outdated version
    # that mandates bytestrings
    #h5r.isheavy()

    'These values do not express any understanding'
    'of real-world use cases for the code.'
    h5r.calc_n_blobs(300)
    h5r.read_blob((300, 300, 300, 300))
    #h5r.read_data()
    h5r.calc_n_coarse_chan()
    h5r.calc_n_coarse_chan(1)
Пример #7
0
    def __init__(self,
                 filename=None,
                 f_start=None,
                 f_stop=None,
                 t_start=None,
                 t_stop=None,
                 load_data=True,
                 max_load=1.,
                 header_dict=None,
                 data_array=None):
        """ Class for loading and plotting blimpy data.

        This class parses the blimpy file and stores the header and data
        as objects:
            fb = Waterfall('filename_here.fil')
            fb.header        # blimpy header, as a dictionary
            fb.data          # blimpy data, as a numpy array

        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 integration ID
            t_stop (int): stop integration ID
            load_data (bool): load data. If set to False, only header will be read.
            max_load (float): maximum data to load in GB. Default: 1GB.
                              e.g. 0.1 is 100 MB
            header_dict (dict): Create blimpy from header dictionary + data array
            data_array (np.array): Create blimpy from header dict + data array
        """

        ##EE        super(Waterfall, self).__init__()

        if filename:
            self.filename = filename
            self.ext = os.path.splitext(filename)[-1].lower()
            self.container = fw.open_file(filename,
                                          f_start=f_start,
                                          f_stop=f_stop,
                                          t_start=t_start,
                                          t_stop=t_stop,
                                          load_data=load_data,
                                          max_load=max_load)
            self.file_header = self.container.header
            self.header = self.file_header
            self.n_ints_in_file = self.container.n_ints_in_file
            self.file_shape = self.container.file_shape
            self.file_size_bytes = self.container.file_size_bytes
            self.selection_shape = self.container.selection_shape
            self.n_channels_in_file = self.container.n_channels_in_file

            # These values will be modified once code for multi_beam and multi_stokes observations are possible.
            self.freq_axis = 2
            self.time_axis = 0
            self.beam_axis = 1  # Place holder
            self.stokes_axis = 4  # Place holder

            self.logger = logger

            self.__load_data()

        elif header_dict is not None and data_array is not None:
            self.filename = ''
            self.header = header_dict
            self.data = data_array
            self.n_ints_in_file = 0
            self._setup_freqs()

        else:
            self.filename = ''

        # Attach methods
        self.plot_spectrum = six.create_bound_method(plot_spectrum, self)
        self.plot_waterfall = six.create_bound_method(plot_waterfall, self)
        self.plot_kurtosis = six.create_bound_method(plot_kurtosis, self)
        self.plot_time_series = six.create_bound_method(plot_time_series, self)
        self.plot_all = six.create_bound_method(plot_all, self)
        self.plot_spectrum_min_max = six.create_bound_method(
            plot_spectrum_min_max, self)
Пример #8
0
def test_file_wrapper_open_fil_file():
    from blimpy.io.file_wrapper import open_file
    filr = open_file(voyager_fil)

    filr.calc_n_blobs(300)
    filr.read_blob((300, 300, 300, 300))