def prepOutfile(self,filename,updates=None,nbits=None,back_compatible=True): """Prepare a file to have sigproc format data written to it. :param filename: filename of new file :type filename: string :param updates: values to overide existing header values :type updates: dict :param nbits: the bitsize of data points that will written to this file (1,2,4,8,32) :type nbits: int :param back_compatible: flag for making file Sigproc compatible :type back_compatible: bool :returns: a prepared file :rtype: :class:`~sigpyproc.Utils.File` """ self.updateHeader() if nbits is None: nbits = self.nbits out_file = File(filename,"w+",nbits) new = self.newHeader(updates) new["nbits"] = nbits out_file.write(new.SPPHeader(back_compatible=back_compatible)) return out_file
def prepOutfile(self, filename, updates=None, nbits=None, back_compatible=True): """Prepare a file to have sigproc format data written to it. Parameters ---------- filename : str name of new file updates : dict, optional values to overide existing header values, by default None nbits : int, optional the bitsize of data points that will written to this file (1,2,4,8,32), by default None back_compatible : bool, optional flag for making file Sigproc compatible, by default True Returns ------- :class:`~sigpyproc.Utils.File` a prepared file """ self.updateHeader() if nbits is None: nbits = self.nbits out_file = File(filename, "w+", nbits) new = self.newHeader(updates) new["nbits"] = nbits out_file.write(new.SPPHeader(back_compatible=back_compatible)) return out_file
def readFFT(filename, inf=None): """Read a presto .fft format file. :param filename: the name of the file to read :type filename: :func:`str` :params inf: the name of the corresponding .inf file (def=None) :type inf: :func:`str` :return: an array containing the whole file contents :rtype: :class:`~sigpyproc.FourierSeries.FourierSeries` .. note:: If inf=None, the function will look for a corresponding file with the same basename which has the .inf file extension. """ basename = os.path.splitext(filename)[0] if inf is None: inf = "%s.inf" % (basename) if not os.path.isfile(inf): raise IOError, "No corresponding inf file found" header = parseInfHeader(inf) f = File(filename, "r", nbits=32) data = np.fromfile(f, dtype="float32") header["basename"] = basename header["inf"] = inf header["filename"] = filename return FourierSeries(data, header)
def readSpec(cls, filename): """Read a sigpyproc format ``.spec`` file. Parameters ---------- filename : str the name of the ``.spec`` file to read Returns ------- :class:`~sigpyproc.FourierSeries.FourierSeries` an array containing the whole file contents Notes ----- This is not setup to handle ``.spec`` files such as are created by Sigprocs seek module. To do this would require a new header parser for that file format. """ header = Header.parseSigprocHeader(filename) hdrlen = header["hdrlen"] f = File(filename, "r", nbits=32) f.seek(hdrlen) data = np.fromfile(f, dtype="complex32") return cls(data, header)
def __init__(self, filename): self.filename = filename self.header = parseSigprocHeader(self.filename) self._file = File(filename, "r", self.header.nbits) self.itemsize = np.dtype(self.header.dtype).itemsize if self.header.nbits in [1, 2, 4]: self.bitfact = 8 / self.header.nbits else: self.bitfact = 1 self.sampsize = self.header.nchans * self.itemsize / self.bitfact super(FilReader, self).__init__()
def toFFTFile(self, basename=None): """Write spectrum to file in sigpyproc format. :param basename: basename of .fft and .inf file to be written :type filename: str :return: name of files written to :rtype: :func:`tuple` of :func:`str` """ if basename is None: basename = self.header.basename self.header.makeInf(outfile="%s.inf" % (basename)) fftfile = File("%s.fft" % (basename), "w+") self.tofile(fftfile) return "%s.fft" % (basename), "%s.inf" % (basename)
def readTim(filename): """Read a sigproc format time series from file. :param filename: the name of the file to read :type filename: :func:`str` :return: an array containing the whole file contents :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries` """ header = parseSigprocHeader(filename) nbits = header["nbits"] hdrlen = header["hdrlen"] f = File(filename, "r", nbits=nbits) f.seek(hdrlen) data = np.fromfile(f, dtype=header["dtype"]).astype("float32") return TimeSeries(data, header)
def toFFTFile(self, basename=None): """Write spectrum to file in presto ``.fft`` format. Parameters ---------- basename : str, optional basename of ``.fft`` and ``.inf`` file to be written, by default None Returns ------- tuple of str name of files written to """ if basename is None: basename = self.header.basename self.header.makeInf(outfile=f"{basename}.inf") fftfile = File(f"{basename}.fft", "w+") self.tofile(fftfile) return f"{basename}.fft", f"{basename}.inf"
def readTim(cls, filename): """Read a sigproc format ``.tim`` file. Parameters ---------- filename : str the name of the ``.tim`` file to read Returns ------- :class:`~sigpyproc.TimeSeries.TimeSeries` a new TimeSeries object """ header = Header.parseSigprocHeader(filename) nbits = header["nbits"] hdrlen = header["hdrlen"] f = File(filename, "r", nbits=nbits) f.seek(hdrlen) data = np.fromfile(f, dtype=header["dtype"]).astype(np.float32, copy=False) return cls(data, header)
def readSpec(filename): """Read a sigpyproc format spec file. :param filename: the name of the file to read :type filename: :func:`str` :return: an array containing the whole file contents :rtype: :class:`~sigpyproc.FourierSeries.FourierSeries` .. note:: This is not setup to handle ``.spec`` files such as are created by Sigprocs seek module. To do this would require a new header parser for that file format. """ header = parseSigprocHeader(filename) hdrlen = header["hdrlen"] f = File(filename, "r", nbits=32) f.seek(hdrlen) data = np.fromfile(f, dtype="complex32") return FourierSeries(data, header)
def readDat(cls, filename, inf=None): """Read a presto format ``.dat`` file. Parameters ---------- filename : str the name of the ``.dat`` file to read inf : str, optional the name of the corresponding ``.inf`` file, by default None Returns ------- :class:`~sigpyproc.TimeSeries.TimeSeries` a new TimeSeries object Raises ------ IOError If no ``.inf`` file found in the same directory of ``.dat`` file. Notes ----- If inf=None, then the associated .inf file must be in the same directory. """ datfile = os.path.realpath(filename) basename, ext = os.path.splitext(datfile) if inf is None: inf = f"{basename}.inf" if not os.path.isfile(inf): raise IOError("No corresponding .inf file found") header = Header.parseInfHeader(inf) f = File(filename, "r", nbits=32) data = np.fromfile(f, dtype=np.float32) header["basename"] = basename header["inf"] = inf header["filename"] = filename header["nsamples"] = data.size return cls(data, header)
def readFFT(cls, filename, inf=None): """Read a presto format ``.fft`` file. Parameters ---------- filename : str the name of the ``.fft`` file to read inf : str, optional the name of the corresponding ``.inf`` file, by default None Returns ------- :class:`~sigpyproc.FourierSeries.FourierSeries` an array containing the whole file contents Raises ------ IOError If no ``.inf`` file found in the same directory of ``.fft`` file. Notes ----- If inf=None, then the associated .inf file must be in the same directory. """ fftfile = os.path.realpath(filename) basename, ext = os.path.splitext(fftfile) if inf is None: inf = f"{basename}.inf" if not os.path.isfile(inf): raise IOError("No corresponding inf file found") header = Header.parseInfHeader(inf) f = File(filename, "r", nbits=32) data = np.fromfile(f, dtype="float32") header["basename"] = basename header["inf"] = inf header["filename"] = filename return cls(data, header)