Skip to content

Commit

Permalink
Merge pull request #515 from jeanlucmargot/main
Browse files Browse the repository at this point in the history
Solve inability to read GUPPI/VEGAS keywords written as strings
  • Loading branch information
mhvk committed May 16, 2023
1 parent 8c0ae38 commit 00b80c6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
9 changes: 9 additions & 0 deletions baseband/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ def _full_path(name, dirname=_path.dirname(_path.abspath(__file__))):
equivalent to 1024 complete samples (with 64 overlap).
"""

SAMPLE_VEGAS = _full_path('sample_vegas.raw')
r"""VEGAS sample, npol=2, obsnchan=32.
Created from a Green Bank Telescope observation of TOI 1898
dd if=vegas_59332_80137_Jade_1898_1_0001.0000.raw \
of=sample_vegas.raw bs=80 count=178
Initial frame is >50 MB and truncated. For tests of header functionality only.
"""

SAMPLE_GSB_RAWDUMP_HEADER = _full_path('gsb/sample_gsb_rawdump.timestamp')
"""GSB rawdump header sample.
Expand Down
Binary file added baseband/data/sample_vegas.raw
Binary file not shown.
25 changes: 13 additions & 12 deletions baseband/guppi/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def nbytes(self):
@property
def payload_nbytes(self):
"""Size of the payload in bytes."""
return self['BLOCSIZE']
return int(self['BLOCSIZE'])

@payload_nbytes.setter
def payload_nbytes(self, payloadsize):
Expand All @@ -233,7 +233,7 @@ def frame_nbytes(self, frame_nbytes):
@property
def bps(self):
"""Bits per elementary sample."""
return self['NBITS']
return int(self['NBITS'])

@bps.setter
def bps(self, bps):
Expand All @@ -242,12 +242,12 @@ def bps(self, bps):
@property
def complex_data(self):
"""Whether the data are complex."""
return self['OBSNCHAN'] != 1
return int(self['OBSNCHAN']) != 1

@property
def npol(self):
"""Number of polarisations."""
return self['NPOL'] // (2 if self.complex_data else 1)
return int(self['NPOL']) // (2 if self.complex_data else 1)

@npol.setter
def npol(self, npol):
Expand All @@ -256,7 +256,7 @@ def npol(self, npol):
@property
def nchan(self):
"""Number of channels."""
return self['OBSNCHAN']
return int(self['OBSNCHAN'])

@nchan.setter
def nchan(self, nchan):
Expand All @@ -278,7 +278,7 @@ def sample_shape(self, sample_shape):
def _bpcs(self):
"""Bits per complete sample."""
# NPOL includes factor of 2 for real/complex components.
return self['OBSNCHAN'] * self['NPOL'] * self.bps
return int(self['OBSNCHAN']) * int(self['NPOL']) * self.bps

@property
def sample_rate(self):
Expand All @@ -287,19 +287,19 @@ def sample_rate(self):
Can be set with a negative quantity to set `sideband`. Overlap samples
are not included in the rate.
"""
return (1. / self['TBIN']) * u.Hz
return (1. / float(self['TBIN'])) * u.Hz

@sample_rate.setter
def sample_rate(self, sample_rate):
self['TBIN'] = 1. / abs(sample_rate.to_value(u.Hz))
bw = (sample_rate.to_value(u.MHz) * self['OBSNCHAN']
bw = (sample_rate.to_value(u.MHz) * int(self['OBSNCHAN'])
/ (1 if self.complex_data else 2))
self['OBSBW'] = bw

@property
def sideband(self):
"""True if upper sideband."""
return self['OBSBW'] > 0
return float(self['OBSBW']) > 0

@sideband.setter
def sideband(self, sideband):
Expand Down Expand Up @@ -334,7 +334,7 @@ def samples_per_frame(self, samples_per_frame):
@property
def overlap(self):
"""Number of complete samples that overlap with the next frame."""
return self['OVERLAP']
return int(self['OVERLAP'])

@overlap.setter
def overlap(self, overlap):
Expand All @@ -345,11 +345,12 @@ def offset(self):
"""Offset from start of observation in units of time."""
# PKTIDX only counts valid packets, not overlap ones.
return ((self['PKTIDX'] * self['PKTSIZE'] * 8 // self._bpcs)
* self['TBIN']) * u.s
* float(self['TBIN'])) * u.s

@offset.setter
def offset(self, offset):
self['PKTIDX'] = int((offset / (self['TBIN'] * u.s) / self['PKTSIZE']
self['PKTIDX'] = int((offset / (float(self['TBIN']) * u.s)
/ self['PKTSIZE']
* ((self._bpcs + 7) // 8)).to(u.one).round())

@property
Expand Down
15 changes: 14 additions & 1 deletion baseband/guppi/tests/test_guppi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ... import guppi
from ...helpers import sequentialfile as sf
from ..base import GUPPIFileNameSequencer
from ...data import SAMPLE_PUPPI as SAMPLE_FILE
from ...data import SAMPLE_PUPPI as SAMPLE_FILE, SAMPLE_VEGAS


class TestGUPPI:
Expand Down Expand Up @@ -827,3 +827,16 @@ def test_header_extraction(self):
fns = GUPPIFileNameSequencer(template, self.header)
assert fns[0] == 'puppi_58132_J1810+1744_2176.0000.raw'
assert fns[29] == 'puppi_58132_J1810+1744_2176.0029.raw'


def test_vegas_header_keywords():
with guppi.open(SAMPLE_VEGAS, 'rs') as fh:
assert fh.header0.payload_nbytes == 132186112
assert fh.header0.bps == 8
assert fh.header0.complex_data
assert fh.header0.npol == 2
assert fh.header0.nchan == 32
assert fh.header0.sample_rate == 3125000.0 * u.Hz
assert not fh.header0.sideband
assert fh.header0.overlap == 512
assert fh.header0.offset == 0.

0 comments on commit 00b80c6

Please sign in to comment.