def testWriteRead(self): now = time.time() n = 10 deltat = 0.1 networks = [ rn(2) for i in range(5) ] traces1 = [ trace.Trace(rc(networks), rn(4), rn(2), rn(3), tmin=now+i*deltat*n*2, deltat=deltat, ydata=num.arange(n, dtype=num.int32), mtime=now) for i in range(100) ] tempdir = tempfile.mkdtemp() fns = mseed.save(traces1, pjoin(tempdir, '%(network)s')) traces2 = [] for fn in fns: traces2.extend(mseed.load(fn)) for tr in traces1: assert tr in traces2 for fn in fns: os.remove(fn) shutil.rmtree(tempdir)
def save(traces, filename_template, format='mseed', additional={}, stations=None, overwrite=True): '''Save traces to file(s). :param traces: a trace or an iterable of traces to store :param filename_template: filename template with placeholders for trace metadata. Uses normal python '%%(placeholder)s' string templates. The following placeholders are considered: ``network``, ``station``, ``location``, ``channel``, ``tmin`` (time of first sample), ``tmax`` (time of last sample), ``tmin_ms``, ``tmax_ms``, ``tmin_us``, ``tmax_us``. The versions with '_ms' include milliseconds, the versions with '_us' include microseconds. :param format: %s :param additional: dict with custom template placeholder fillins. :param overwrite': if ``False``, raise an exception if file exists :returns: list of generated filenames .. note:: Network, station, location, and channel codes may be silently truncated to file format specific maximum lengthes. ''' if isinstance(traces, trace.Trace): traces = [traces] if format == 'from_extension': format = os.path.splitext(filename_template)[1][1:] if format == 'mseed': return mseed.save(traces, filename_template, additional, overwrite=overwrite) elif format == 'gse2': return gse2_io_wrap.save(traces, filename_template, additional, overwrite=overwrite) elif format == 'sac': fns = [] for tr in traces: fn = tr.fill_template(filename_template, **additional) if not overwrite and os.path.exists(fn): raise FileSaveError('file exists: %s' % fn) util.ensuredirs(fn) f = sac.SacFile(from_trace=tr) if stations: s = stations[tr.network, tr.station, tr.location] f.stla = s.lat f.stlo = s.lon f.stel = s.elevation f.stdp = s.depth f.cmpinc = s.get_channel(tr.channel).dip + 90. f.cmpaz = s.get_channel(tr.channel).azimuth f.write(fn) fns.append(fn) return fns elif format == 'text': fns = [] for tr in traces: fn = tr.fill_template(filename_template, **additional) if not overwrite and os.path.exists(fn): raise FileSaveError('file exists: %s' % fn) util.ensuredirs(fn) x, y = tr.get_xdata(), tr.get_ydata() num.savetxt(fn, num.transpose((x, y))) fns.append(fn) return fns elif format == 'yaff': return yaff.save(traces, filename_template, additional, overwrite=overwrite) else: raise UnsupportedFormat(format)
def save(traces, filename_template, format='mseed', additional={}, stations=None, overwrite=True): '''Save traces to file(s). :param traces: a trace or an iterable of traces to store :param filename_template: filename template with placeholders for trace metadata. Uses normal python '%%(placeholder)s' string templates. The following placeholders are considered: ``network``, ``station``, ``location``, ``channel``, ``tmin`` (time of first sample), ``tmax`` (time of last sample), ``tmin_ms``, ``tmax_ms``, ``tmin_us``, ``tmax_us``. The versions with '_ms' include milliseconds, the versions with '_us' include microseconds. :param format: %s :param additional: dict with custom template placeholder fillins. :param overwrite': if ``False``, raise an exception if file exists :returns: list of generated filenames .. note:: Network, station, location, and channel codes may be silently truncated to file format specific maximum lengthes. ''' if isinstance(traces, trace.Trace): traces = [ traces ] if format == 'from_extension': format = os.path.splitext(filename_template)[1][1:] if format == 'mseed': return mseed.save(traces, filename_template, additional, overwrite=overwrite) elif format == 'gse2': return gse2_io_wrap.save(traces, filename_template, additional, overwrite=overwrite) elif format == 'sac': fns = [] for tr in traces: fn = tr.fill_template(filename_template, **additional) if not overwrite and os.path.exists(fn): raise FileSaveError('file exists: %s' % fn) util.ensuredirs(fn) f = sac.SacFile(from_trace=tr) if stations: s = stations[tr.network, tr.station, tr.location] f.stla = s.lat f.stlo = s.lon f.stel = s.elevation f.stdp = s.depth f.cmpinc = s.get_channel(tr.channel).dip + 90. f.cmpaz = s.get_channel(tr.channel).azimuth f.write(fn) fns.append(fn) return fns elif format == 'text': fns = [] for tr in traces: fn = tr.fill_template(filename_template, **additional) if not overwrite and os.path.exists(fn): raise FileSaveError('file exists: %s' % fn) util.ensuredirs(fn) x,y = tr.get_xdata(), tr.get_ydata() num.savetxt(fn, num.transpose((x,y))) fns.append(fn) return fns elif format == 'yaff': return yaff.save(traces, filename_template, additional, overwrite=overwrite) else: raise UnsupportedFormat(format)