Skip to content

Instantly share code, notes, and snippets.

@astropenguin
Last active March 4, 2018 10:14
Show Gist options
  • Save astropenguin/cc68aeb7c786b1ef4228a259edf32b03 to your computer and use it in GitHub Desktop.
Save astropenguin/cc68aeb7c786b1ef4228a259edf32b03 to your computer and use it in GitHub Desktop.
"""Mini tool to modify nqm to one compatible with CASA's scantable.
Usage:
$ python nqm_for_scantable.py <nqmname>
Notes:
User can use wildcard '*' in <nqmname> for batch process.
"""
# standard library
import sys
# module constants
FITS_HEADER_SIZE = 23040 # 8*2880 bytes
# functions
def modify(nqmname):
# read nqm
with open(nqmname, 'rb') as f_nqm:
# read header part
h_nqm = b''
while True:
h_nqm += f_nqm.read(1)
if h_nqm.endswith(b' LS '):
break
h_nqm = h_nqm.rstrip(b'LS ')
d_nqm = b'LS ' + f_nqm.read()
# adjust header size to 23040 bytes
if len(h_nqm) == FITS_HEADER_SIZE:
pass
elif len(h_nqm) < FITS_HEADER_SIZE:
h_nqm += b' ' * (FITS_HEADER_SIZE-len(h_nqm))
else:
h_nqm = h_nqm.split(b'HISTORY', 1)[0]
h_nqm += b'END'
h_nqm += b' ' * (FITS_HEADER_SIZE-len(h_nqm))
# create modified nqm
nqmname = nqmname.rstrip('.nqm') + '.mod.nqm'
with open(nqmname, 'wb') as f_nqm:
f_nqm.write(h_nqm + d_nqm)
# main program
if __name__ == '__main__':
args = sys.argv[1:]
if len(args) == 0:
print(__doc__)
sys.exit()
for nqmname in args:
print('modifying: ' + nqmname)
modify(nqmname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment