def parseInfHeader(filename): """Parse the metadata from a presto ``.inf`` file. :param filename: file containing the header :type filename: :func:`str` :return: observational metadata :rtype: :class:`~sigpyproc.Header.Header` """ f = open(filename, "r") header = {} lines = f.readlines() f.close() for line in lines: key = line.split("=")[0].strip() val = line.split("=")[-1].strip() if not key in conf.inf_to_header.keys(): continue else: key, keytype = conf.inf_to_header[key] header[key] = keytype(val) header["src_raj"] = float("".join(header["src_raj"].split(":"))) header["src_dej"] = float("".join(header["src_dej"].split(":"))) header["telescope_id"] = conf.telescope_ids.get(header["telescope_id"], 10) header["machine_id"] = conf.machine_ids.get(header["machine_id"], 9) header["data_type"] = 2 header["nchans"] = 1 header["nbits"] = 32 header["hdrlen"] = 0 header["nsamples"] = 0 return Header(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 test_toFFTFile(self, fourier_data, tim_header): myFS = FourierSeries(fourier_data, Header(tim_header)) fftfile, inffile = myFS.toFFTFile(basename="temp_test") assert os.path.isfile(fftfile) assert os.path.isfile(inffile) os.remove(fftfile) os.remove(inffile)
def test_toDat(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) datfile, inffile = myTim.toDat(basename="temp_test") assert os.path.isfile(datfile) assert os.path.isfile(inffile) os.remove(datfile) os.remove(inffile)
def parseSigprocHeader(filename): """Parse the metadata from a Sigproc-style file header. :param filename: file containing the header :type filename: :func:`str` :return: observational metadata :rtype: :class:`~sigpyproc.Header.Header` """ f = open(filename, "rb") header = {} try: keylen = unpack("I", f.read(4))[0] except struct.error: raise IOError("File Header is not in sigproc format... Is file empty?") key = f.read(keylen) if key != b"HEADER_START": raise IOError("File Header is not in sigproc format") while True: keylen = unpack("I", f.read(4))[0] key = f.read(keylen) # convert bytestring to unicode try: key = key.decode("UTF-8") except UnicodeDecodeError as e: print("Could not convert to unicode: {0}".format(str(e))) # this is basically a sanity check that we haven't passed header_end if not key in conf.header_keys: print("'%s' not recognised header key" % (key)) return None if conf.header_keys[key] == "str": header[key] = _read_string(f) elif conf.header_keys[key] == "I": header[key] = _read_int(f) elif conf.header_keys[key] == "b": header[key] = _read_char(f) elif conf.header_keys[key] == "d": header[key] = _read_double(f) if key == "HEADER_END": break header["hdrlen"] = f.tell() f.seek(0, 2) header["filelen"] = f.tell() header["nbytes"] = header["filelen"] - header["hdrlen"] header[ "nsamples"] = 8 * header["nbytes"] / header["nbits"] / header["nchans"] f.seek(0) header["filename"] = filename header["basename"] = os.path.splitext(filename)[0] f.close() return Header(header)
def test_readTim(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) outfile = myTim.toFile() mynewTim = TimeSeries.readTim(filename=outfile) assert mynewTim.header.nbits == 32 assert mynewTim.header.source_name == "test" np.testing.assert_allclose(np.mean(mynewTim), 128, atol=0.1) os.remove(outfile)
def test_makeInf(self, inffile, tmpfile): myheader = Header.parseInfHeader(filename=inffile) myheader.makeInf(outfile=tmpfile) with open(inffile, 'r') as file_inf: infdata = file_inf.read() with open(tmpfile, 'r') as file_tmp: tmpdata = file_tmp.read() np.testing.assert_string_equal(tmpdata, infdata)
def test_readFFT(self, fourier_data, tim_header): myFS = FourierSeries(fourier_data, Header(tim_header)) fftfile, inffile = myFS.toFFTFile(basename="temp_test") mynewFS = FourierSeries.readFFT(filename=fftfile) assert mynewFS.header.nbits == 32 assert mynewFS.header.source_name == "test" np.testing.assert_allclose(np.mean(mynewFS), 135.61371, atol=0.01) os.remove(fftfile) os.remove(inffile)
def test_readDat(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) datfile, inffile = myTim.toDat(basename="temp_test") mynewTim = TimeSeries.readDat(filename=datfile) assert mynewTim.header.nbits == 32 assert mynewTim.header.source_name == "test" np.testing.assert_allclose(np.mean(mynewTim), 128, atol=0.1) os.remove(datfile) os.remove(inffile)
def test_parseSigprocHeader(self, timfile): header = Header.parseSigprocHeader(filename=timfile) assert header.nbits == 32 assert header.source_name == "Mystery_PSR" assert header.telescope_id == 6 assert header.machine_id == 4 assert header.ra == "16:43:38.1000" assert header.dec == "-12:24:58.7000" assert header.nsamples == 66250 assert header.nchans == 1
def test_parseInfHeader(self, inffile): header = Header.parseInfHeader(filename=inffile) assert header.nbits == 32 assert header.source_name == "Mystery_PSR" assert header.telescope_id == 6 assert header.machine_id == 9 assert header.src_raj == 164338.1 assert header.src_dej == -122458.7 assert header.nsamples == 66250 assert header.nchans == 1
def __init__(self, filename): self.filename = filename self.header = 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().__init__()
def parseSigprocHeader(filename): """Parse the metadata from a Sigproc-style file header. :param filename: file containing the header :type filename: :func:`str` :return: observational metadata :rtype: :class:`~sigpyproc.Header.Header` """ f = open(filename,"r") header = {} try: keylen = unpack("I",f.read(4))[0] except struct.error: raise IOError,"File Header is not in sigproc format... Is file empty?" key = f.read(keylen) if key != "HEADER_START": raise IOError,"File Header is not in sigproc format" while True: keylen = unpack("I",f.read(4))[0] key = f.read(keylen) if not key in conf.header_keys: print "'%s' not recognised header key"%(key) return None if conf.header_keys[key] == "str": header[key] = _read_string(f) elif conf.header_keys[key] == "I": header[key] = _read_int(f) elif conf.header_keys[key] == "b": header[key] = _read_char(f) elif conf.header_keys[key] == "d": header[key] = _read_double(f) if key == "HEADER_END": break header["hdrlen"] = f.tell() f.seek(0,2) header["filelen"] = f.tell() header["nbytes"] = header["filelen"]-header["hdrlen"] header["nsamples"] = 8*header["nbytes"]/header["nbits"]/header["nchans"] f.seek(0) header["filename"] = filename header["basename"] = os.path.splitext(filename)[0] f.close() return Header(header)
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 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)
def test_prepOutfile(self, filfile, tmpfile): myheader = Header.parseSigprocHeader(filename=filfile) outfile = myheader.prepOutfile(tmpfile) out_header = Header.parseSigprocHeader(filename=outfile.name) np.testing.assert_equal(out_header.SPPHeader(), myheader.SPPHeader())
def test_fourierSeries(self, fourier_data, tim_header): myFS = FourierSeries(fourier_data, Header(tim_header)) assert myFS.header.nbits == 32 assert myFS.header.source_name == "test" assert myFS.shape == (10002, ) np.testing.assert_allclose(np.mean(myFS), 135.61371, atol=0.01)
def test_getDMdelays(self, filfile): header = Header.parseSigprocHeader(filename=filfile) delays_time = header.getDMdelays(dm=100, in_samples=False) delays_samp = header.getDMdelays(dm=100, in_samples=True) np.testing.assert_equal(delays_samp.mean(), 193.3125) np.testing.assert_allclose(delays_time.mean(), 0.013913)
def test_toFile(self, fourier_data, tim_header): myFS = FourierSeries(fourier_data, Header(tim_header)) outfile = myFS.toFile() assert os.path.isfile(outfile) os.remove(outfile)
def test_iFFT(self, fourier_data, tim_header, tim_data): myFS = FourierSeries(fourier_data, Header(tim_header)) myTim = myFS.iFFT() np.testing.assert_allclose(myTim, tim_data, atol=0.01)
def test_runningMedian(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) median_filter = myTim.runningMedian(window=101) np.testing.assert_allclose(np.mean(median_filter), 128, atol=0.1)
def test_downsample(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) downsampled = myTim.downsample(factor=16) np.testing.assert_allclose(np.mean(downsampled), 128, atol=0.1)
def test_toFile(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) outfile = myTim.toFile() assert os.path.isfile(outfile) os.remove(outfile)
def test_timeseries(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) assert myTim.header.nbits == 32 assert myTim.header.source_name == "test" np.testing.assert_allclose(np.mean(myTim), 128, atol=0.1)