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 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 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_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 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 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_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_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)
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_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_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)