Exemple #1
0
    def __init__(self, parent, filename):
        super(LoadDialog, self).__init__(parent)
        self.setupUi(self)
        # init datatype combobox
        self.DataTypeComboBox.addItems(DTYPES_LABELS)
        self.DataTypeComboBox.setCurrentIndex(DTYPES.index(rawfile.datatype_float64))

        self.filename = filename

        self.FileFormatComboBox.currentIndexChanged.connect(self.on_format_change)
        self.FileFormatComboBox.currentIndexChanged.connect(self.load_preview)
        self.DataTypeComboBox.currentIndexChanged.connect(self.load_preview)
        self.ByteOrderComboBox.currentIndexChanged.connect(self.load_preview)

        # Detect settings
        if futils.istextfile(self.filename):
            self.FileFormatComboBox.setCurrentIndex(1)
            # Detect sample rate
            fs = futils.get_sample_rate(filename)
            if fs:
                self.SampleFrequencySpinBox.setValue(fs)
        else:
            self.FileFormatComboBox.setCurrentIndex(0)
        if futils.is_little_endian():
            self.ByteOrderComboBox.setCurrentIndex(0)
        else:
            self.ByteOrderComboBox.setCurrentIndex(1)
        self.load_preview()
Exemple #2
0
def get_file_handler(filename, fmt='', dtype='float64', byteorder='native', **kwargs):
    """Gets a handler for a binary or text file.

    Args:
        filename: name of the file.
        fmt: The format of the data file to read.
            Possible values are 'binary', 'text' or ''.
            If '' is selected, the function will detect whether the file
            is a binary or a text file.
        dtype: Data-type of the numeric data stored in the file.
            Possible values are 'int16', 'int32', 'int64', 'float16', 'float32'
            and 'float64'. Default value is 'float64'.
        byteorder: Byte-order of the numeric data stored in the file.
            Possible values are 'little-endian', 'big-endian' and 'native'.
            Default value is 'native'.

    Returns:
        A BinFile or TextFile object, depending of 'fmt'.
    """
    if isinstance(filename, file):
        filename = filename.name
    formats = [format_binary, format_text]
    if fmt not in formats:
        fmt = format_text if futils.istextfile(filename) else format_binary
    if fmt == format_text:
        return TextFile(filename, dtype=dtype, byteorder=byteorder)
    else:
        return BinFile(filename, dtype=dtype, byteorder=byteorder)
Exemple #3
0
def get_file_handler(filename,
                     fmt='',
                     dtype='float64',
                     byteorder='native',
                     **kwargs):
    """Gets a handler for a binary or text file.

    Args:
        filename: name of the file.
        fmt: The format of the data file to read.
            Possible values are 'binary', 'text' or ''.
            If '' is selected, the function will detect whether the file
            is a binary or a text file.
        dtype: Data-type of the numeric data stored in the file.
            Possible values are 'int16', 'int32', 'int64', 'float16', 'float32'
            and 'float64'. Default value is 'float64'.
        byteorder: Byte-order of the numeric data stored in the file.
            Possible values are 'little-endian', 'big-endian' and 'native'.
            Default value is 'native'.

    Returns:
        A BinFile or TextFile object, depending of 'fmt'.
    """
    if isinstance(filename, file):
        filename = filename.name
    formats = [format_binary, format_text]
    if fmt not in formats:
        fmt = format_text if futils.istextfile(filename) else format_binary
    if fmt == format_text:
        return TextFile(filename, dtype=dtype, byteorder=byteorder)
    else:
        return BinFile(filename, dtype=dtype, byteorder=byteorder)
Exemple #4
0
 def _fopen(self, fname):
     if futils.istextfile(fname):
         ft = argparse.FileType('r')
     else:
         ft = argparse.FileType('rb')
     return ft(fname)
Exemple #5
0
 def _fopen(self, fname):
     if futils.istextfile(fname):
         ft = argparse.FileType('r')
     else:
         ft = argparse.FileType('rb')
     return ft(fname)
def generate(FILEIN, length, t_event, output, gen_event_power=5.0, n_events=1,
             gen_noise_coefficients=False, output_format='binary',
             datatype='float64', byteorder='native', **kwargs):
    """Generates synthetic earthquake signals with background noise and saves
    them to file.

    The function accepts a list of command-line arguments and renders synthetic
    seismic data in two ways: If a list of input files containing seismic data
    is provided, the function generates a new output signal for each one of
    the files by adding background noise. If no input file is provided,
    the function generates a list of synthetic seismic signals.

    Args:
        FILEIN: A list of binary or text file objects storing seismic data.
        length: Length of rendered seismic signals, in seconds.
            If FILEIN is None, this parameter has no effect.
        t_event: Start time of rendered earthquake, given in seconds from the
            beginning of the signal.
            If FILEIN is None, this parameter has no effect.
        output: Output file name (absolute path).
            If no input file is provided and n_events is greater than 1, the
            name of each generated file will be followed by its ordinal number.

            E.g. given FILEIN = None, output = 'example.out' and n_events = 5,
            the function will generate 5 synthetic files named:
            'example00.out', 'example01.out', 'example02.out', 'example03.out'
            and 'example04.out'.

        gen_event_power: Earthquake power in dB.
            If FILEIN is None, this parameter has no effect.
            Default: 5.0.
        n_events: No. of signals to generate.
            If FILEIN is None, this parameter has no effect.
            Default: 1.
        gen_noise_coefficients: A binary or text file object containing a list
            of numeric coefficients of a FIR filter that models the background
            noise.
            Default value is False, meaning unfiltered white noise is used
            to model the background noise.
        output_format: Output file format. Possible values are 'binary' or
            'text'. Default: 'binary'.
        datatype: Data-type of generated data. Default value is 'float64'.
            If FILEIN is not None, this parameter is also the datatype of
            input data.
        byteorder: Byte-order of generated data. Possible values are
            'little-endian', 'big-endian' and 'native'.
            If FILEIN is not None, this parameter is also the format of
            input data.
            Default value is 'native'.
    """
    fs = kwargs.get('fs', 50.0)
    # Configure generator
    clt.print_msg("Configuring generator... ")
    generator = eqgenerator.EarthquakeGenerator(**kwargs)
    clt.print_msg("Done\n")
    # Load noise coefficients
    if gen_noise_coefficients:
        if futils.istextfile(gen_noise_coefficients):
            f = open(gen_noise_coefficients, 'r')
        else:
            f = open(gen_noise_coefficients, 'rb')
        clt.print_msg("Loading noise coefficients from %s... " %
                         f.name)
        generator.load_noise_coefficients(f, dtype=datatype,
                                          byteorder=byteorder)
        clt.print_msg("Done\n")
    # Process input files
    basename, ext = os.path.splitext(output)
    filename_out = output
    # If a list of input files containing seismic data
    # is provided, generate a new output signal for each one of
    # the files by adding background noise.
    if FILEIN:
        fileno = 0
        for f in FILEIN:
            # Read input signal
            fin_handler = rawfile.get_file_handler(f, dtype=datatype,
                                                   byteorder=byteorder)
            clt.print_msg("Loading seismic signal from %s... " %
                          fin_handler.filename)
            signal = fin_handler.read()
            clt.print_msg("Done\n")
            # Generate output filename
            if len(FILEIN) > 1:
                filename_out = "%s%02.0i%s" % (basename, fileno, ext)
                fileno += 1
            clt.print_msg("Generating artificial signal in %s... " %
                             filename_out)
            # Add background noise to signal
            eq = generator.generate_noise(signal)
            # Save outputs to file
            if output_format == 'text':
                fout_handler = rawfile.TextFile(filename_out, dtype=datatype,
                                            byteorder=byteorder)
            else:
                fout_handler = rawfile.BinFile(filename_out, dtype=datatype,
                                              byteorder=byteorder)
            fout_handler.write(eq, header="Sample rate: %g Hz." % fs)
            clt.print_msg("Done\n")
    # If no input file is provided,
    # generate a list of synthetic seismic signals.
    else:
        for i in xrange(n_events):
            # Generate output filename
            if n_events > 1:
                filename_out = "%s%02.0i%s" % (basename, i, ext)
            clt.print_msg("Generating artificial signal in %s... " %
                             filename_out)
            # Generate a synthetic signal
            eq = generator.generate_earthquake(length, t_event,
                                               gen_event_power)
            # Save outputs to file
            if output_format == 'text':
                fout_handler = rawfile.TextFile(filename_out, dtype=datatype,
                                                byteorder=byteorder)
            else:
                fout_handler = rawfile.BinFile(filename_out, dtype=datatype,
                                byteorder=byteorder)
            fout_handler.write(eq, header="Sample rate: %g Hz." % fs)
            clt.print_msg("Done\n")