def read(filename, format=None, dtype='float64', byteorder='native', description='', normalize=True, *args, **kwargs): """ Read signal files into an ApasvoStream object :param filename: :param format: :param file_dtype: :param file_byteorder: :param description: :param args: :param kwargs: :return: """ # Try to read using obspy core functionality try: traces = [ApasvoTrace(copy.deepcopy(trace.data), copy.deepcopy(trace.stats), filename=filename, normalize=normalize) \ for trace in op.read(filename, format=format, *args, **kwargs).traces] # Otherwise try to read as a binary or text file except Exception as e: fhandler = rawfile.get_file_handler(filename, format=format, dtype=dtype, byteorder=byteorder) trace = ApasvoTrace(fhandler.read().astype(DEFAULT_DTYPE, casting='safe'), filename=filename) sample_fs = kwargs.get('fs') trace.stats.delta = DEFAULT_DELTA if sample_fs is None else 1. / sample_fs traces = [trace] # Convert Obspy traces to apasvo traces return ApasvoStream(traces, description=description, filename=filename)
def read(filename, format=None, dtype='float64', byteorder='native', description='', *args, **kwargs): """ Read signal files into an ApasvoStream object :param filename: :param format: :param file_dtype: :param file_byteorder: :param description: :param args: :param kwargs: :return: """ # Try to read using obspy core functionality try: traces = [ApasvoTrace(copy.deepcopy(trace.data), copy.deepcopy(trace.stats)) \ for trace in op.read(filename, format=format, *args, **kwargs).traces] # Otherwise try to read as a binary or text file except Exception as e: fhandler = rawfile.get_file_handler( filename, format=format, dtype=dtype, byteorder=byteorder) trace = ApasvoTrace(fhandler.read().astype( DEFAULT_DTYPE, casting='safe')) sample_fs = kwargs.get('fs') trace.stats.delta = DEFAULT_DELTA if sample_fs is None else 1. / sample_fs traces = [trace] # Convert Obspy traces to apasvo traces return ApasvoStream(traces, description=description)
def __init__(self, fileobj, fs, label='', description='', fmt='', dtype=rawfile.datatype_float64, byteorder=rawfile.byteorder_native, **kwargs): """Initializes a Record instance. Args: fileobj: A file (binary or plain text) storing seismic data. fs: Sample rate in Hz. label: A string that identifies the seismic record. Default: ''. description: Additional comments. fmt: A string indicating fileobj's format. Possible values are 'binary', 'text' or ''. Default value is ''. dtype: Data-type of the data stored in fileobj. Default value is 'float64'. byteorder: Byte-order of the data stored in fileobj. Valid values are: 'little-endian', 'big-endian' and 'native'. Default: 'native'. """ super(Record, self).__init__() if isinstance(fileobj, file): self.filename = fileobj.name else: self.filename = fileobj fhandler = rawfile.get_file_handler(fileobj, fmt=fmt, dtype=dtype, byteorder=byteorder) self.signal = fhandler.read().astype(rawfile.datatype_float64, casting='safe') self.fs = fs self.cf = np.array([], dtype=rawfile.datatype_float64) self.events = [] if label == '': _, rname = os.path.split(self.filename) label, _ = os.path.splitext(rname) self.label = label self.description = description
def load_noise_coefficients(self, fileobj, dtype='float64', byteorder='native'): """Loads 'bfirls' attribute from a given file. File must be on binary or plain text format. Args: fileobj: A binary or text file object containing a list of numeric coefficients. dtype: Data-type of the numeric data stored into the file. byteorder: Byte-order of the numeric data stored into the file. """ fhandler = rawfile.get_file_handler(fileobj, dtype=dtype, byteorder=byteorder) self.bfirls = fhandler.read()
def load_preview(self): """Shows a preview of loaded data using the selected parameters.""" # Load parameters values = self.get_values() try: # Set up a file handler according to the type of raw data (binary or text) fhandler = rawfile.get_file_handler(self.filename, **values) # Print data preview array = fhandler.read_in_blocks().next() data = '' for x in array: data += ("%g\n" % x) except: data = '*** There was a problem reading the file content ***' self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False) else: self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True) self.PreviewTextEdit.clear() self.PreviewTextEdit.setText(data)
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")