#! /usr/bin/env python

# Update BE_DELAY keyword for guppi/puppi files

import sys, fitsio

for fname in sys.argv[1:]:
    f = fitsio.FITS(fname,'rw')
    h = f[0].read_header()
    if not 'UPPI' in h['BACKEND']:
        print "%s: backend='%s', skipping" % h['BACKEND']
        f.close()
        continue
    orig_ch_bw = abs(h['OBSBW']) / h['OBSNCHAN']
    if 'GUPPI' in h['BACKEND'] and h['OBSNCHAN']==128 and \
            (h['STT_IMJD']<55692 and h['STT_IMJD']>55587):
                be_delay = 8.0e-6 / orig_ch_bw / 2.0
    else:
        be_delay = 12.0e-6 / orig_ch_bw / 2.0
    print "%s: %d %.5e" % (fname, h['OBSNCHAN'], be_delay)
    f[0].write_key('BE_DELAY', be_delay, '[s] Updated')
    f.close()




