def write(self, data): format_str = "%%0%ds" % self.length try: temp = float(data) except: msg = "No float value found for %s." % self.attribute_name raise SEEDTypeException(msg) # special format for exponential output result = format_str % (self.mask % temp) if 'E' in self.mask or 'e' in self.mask: result = util.formatScientific(result.upper()) if len(result) != self.length: msg = "Invalid field length %d of %d in %s." % \ (len(result), self.length, self.attribute_name) raise SEEDTypeException(msg) return result
def writeHeader(f, head): """ Rewriting the write_header Function of gse_functions.c Different operating systems are delivering different output for the scientific format of floats (fprinf libc6). Here we ensure to deliver in a for GSE2 valid format independent of the OS. For speed issues we simple cut any number ending with E+0XX or E-0XX down to E+XX or E-XX. This fails for numbers XX>99, but should not occur. :type f: File pointer :param f: File pointer to to GSE2 file to write :type head: Ctypes struct :param head: Ctypes structure to write """ calib = formatScientific("%10.2e" % head.calib) header = "WID2 %4d/%02d/%02d %02d:%02d:%06.3f %-5s %-3s %-4s %-3s %8d " + \ "%11.6f %s %7.3f %-6s %5.1f %4.1f\n" f.write(header % ( head.d_year, head.d_mon, head.d_day, head.t_hour, head.t_min, head.t_sec, head.station, head.channel, head.auxid, head.datatype, head.n_samps, head.samp_rate, calib, head.calper, head.instype, head.hang, head.vang))
def writeASC(stream, filename, included_headers=None, npl=4, custom_format=None, append=False, **kwargs): # @UnusedVariable """ Writes a Seismic Handler ASCII file from given ObsPy Stream object. .. warning:: This function should NOT be called directly, it registers via the the :meth:`~obspy.core.stream.Stream.write` method of an ObsPy :class:`~obspy.core.stream.Stream` object, call this instead. :type stream: :class:`~obspy.core.stream.Stream` :param stream: The ObsPy Stream object to write. :type filename: str :param filename: Name of the ASCII file to write. :type npl: int, optional :param npl: Number of data columns in file, default to four. :type included_headers: list or None, optional :param included_headers: If set to a list, only these header entries will be written to file. DELTA and LENGTH are written in any case. If it's set to None, a basic set will be included. :type custom_format: string, optional :param custom_format: Parameter for number formatting of samples, defaults to None. This will use ObsPy's formatScientific method. :type append: bool, optional :param append: If filename exists append all data to file, default False. """ if included_headers is None: included_headers = STANDARD_ASC_HEADERS if append: fh = open(filename, 'ab') else: fh = open(filename, 'wb') for trace in stream: # write headers fh.write("DELTA: %s\n" % formatScientific("%-.6e" % trace.stats.delta)) fh.write("LENGTH: %d\n" % trace.stats.npts) # additional headers for key, value in trace.stats.get('sh', {}).iteritems(): if included_headers and key not in included_headers: continue fh.write("%s: %s\n" % (key, value)) # special format for start time if "START" in included_headers: dt = trace.stats.starttime fh.write("START: %s\n" % fromUTCDateTime(dt)) # component must be split if len(trace.stats.channel) > 2 and "COMP" in included_headers: fh.write("COMP: %c\n" % trace.stats.channel[2]) if len(trace.stats.channel) > 0 and "CHAN1" in included_headers: fh.write("CHAN1: %c\n" % trace.stats.channel[0]) if len(trace.stats.channel) > 1 and "CHAN2" in included_headers: fh.write("CHAN2: %c\n" % trace.stats.channel[1]) if "STATION" in included_headers: fh.write("STATION: %s\n" % trace.stats.station) if "CALIB" in included_headers: fh.write("CALIB: %s\n" % formatScientific("%-.6e" % \ trace.stats.calib)) # write data in npl columns mask = ([''] * (npl - 1)) + ['\n'] delimiter = mask * ((trace.stats.npts / npl) + 1) delimiter = delimiter[:trace.stats.npts - 1] delimiter.append('\n') for (sample, delim) in zip(trace.data, delimiter): if custom_format is None: value = formatScientific("%-.6e" % sample) else: value = custom_format % sample fh.write("%s %s" % (value, delim)) fh.write("\n") fh.close()
def formatRESP(number, digits=4): """ Formats a number according to the RESP format. """ format_string = "%%-10.%dE" % digits return formatScientific(format_string % number)