Ejemplo n.º 1
0
def convert_flags(infile, idifiles, outfp=sys.stdout, outfile=None):
    # Make sure we're dealing with a list
    if not isinstance(idifiles, list):
        idifiles = [idifiles]
        pass

    # Create new output file if requested to do so
    if outfile:
        outfp = open(outfile, 'w')
        pass

    first_date = find_first_dobs(idifiles)

    infp = open(infile, 'r')
    flags = key.read_keyfile(infp)
    infp.close()

    flags = [dict(x) for x in flags]

    for flag in flags:
        antenna = flag['ANT_NAME'].upper()
        timerange = flag['TIMERANG']
        if timerange == [0.0, 0.0, 0.0, 0.0, 400.0, 0.0, 0.0, 0.0]:
            timerange = ""
        else:
            year = datetime.datetime(first_date.year, 1, 1)
            date1 = year + datetime.timedelta(timerange[0] - 1)
            date1 = date1.strftime("%Y/%m/%d")
            date2 = year + datetime.timedelta(timerange[4] - 1)
            date2 = date2.strftime("%Y/%m/%d")
            timerange = "%s/%02d:%02d:%02d~%s/%02d:%02d:%02d" % \
                (date1, timerange[1], timerange[2], timerange[3],
                 date2, timerange[5], timerange[6], timerange[7])
            pass
        if 'BIF' in flag:
            spw = "%d~%d" % (flag['BIF'] - 1, flag['EIF'] - 1)
        else:
            spw = ""
            pass
        if 'BCHAN' in flag:
            spw = spw + ":%d~%d" % (flag['BCHAN'] - 1, flag['ECHAN'] - 1)
            pass
        cmd = "antenna='%s'" % antenna
        if timerange:
            cmd = cmd + " timerange='%s'" % timerange
            pass
        if spw:
            cmd = cmd + " spw='%s'" % spw
            pass
        outfp.write(cmd + "\n")
    return
Ejemplo n.º 2
0
def append_gc(antabfile, idifile):
    # Check if we already have a GAIN_CURVE table
    try:
        hdulist = pyfits.open(idifile)
        hdu = hdulist['GAIN_CURVE']
        print('GAIN_CURVE table already present in FITS-IDI file')
        sys.exit(1)
    except KeyError:
        pass

    idi = IdiData([idifile])
    data = GainCurveTable()

    pols = []
    keys = StringIO()
    fp = open(antabfile, 'r')
    for line in fp:
        if line.startswith('!'):
            continue
        keys.write(line)
        if line.strip().endswith('/'):
            keys.seek(0)
            gain = key.read_keyfile(keys)
            # Standard ANTAB
            if gain and gain[0] and gain[0][0][0] == 'GAIN':
                process_gc_values(fp, gain, pols, idi, data)
            elif gain and gain[0] and gain[0][0][0] == 'TSYS':
                skip_values(fp)
                pass
            keys = StringIO()
            continue
        continue

    n_tab = max(data.nterm)
    nrows = len(data.nterm)

    antenna_no = data.antenna_no
    array = data.array
    freqid = data.freqid
    _type = nrows * [idi.n_band * [2]]
    nterm = np.repeat(data.nterm, idi.n_band).reshape(nrows, idi.n_band)
    x_typ = nrows * [idi.n_band * [0]]
    y_typ = np.repeat(data.y_typ, idi.n_band).reshape(nrows, idi.n_band)
    x_val = nrows * [idi.n_band * [float('nan')]]
    y_val = nrows * [(n_tab * idi.n_band) * [float('nan')]]
    sens_1 = np.repeat(data.sens_1, idi.n_band).reshape(nrows, idi.n_band)
    if len(pols) > 1:
        sens_2 = np.repeat(data.sens_2, idi.n_band).reshape(nrows, idi.n_band)
        pass

    gain = np.zeros((nrows, n_tab))
    for row in range(nrows):
        for elem in range(len(data.gain[row])):
            gain[(row, elem)] = data.gain[row][elem]
            continue
        continue
    gain = np.repeat(gain, idi.n_band, axis=0)
    gain = gain.reshape(nrows, n_tab * idi.n_band)

    cols = []
    col = pyfits.Column(name='ANTENNA_NO', format='1J', array=antenna_no)
    cols.append(col)
    col = pyfits.Column(name='ARRAY', format='1J', array=array)
    cols.append(col)
    col = pyfits.Column(name='FREQID', format='1J', array=freqid)
    cols.append(col)
    format = '%dJ' % idi.n_band
    col = pyfits.Column(name='TYPE_1', format=format, array=_type)
    cols.append(col)
    col = pyfits.Column(name='NTERM_1', format=format, array=nterm)
    cols.append(col)
    col = pyfits.Column(name='X_TYP_1', format=format, array=x_typ)
    cols.append(col)
    col = pyfits.Column(name='Y_TYP_1', format=format, array=y_typ)
    cols.append(col)
    format = '%dE' % idi.n_band
    col = pyfits.Column(name='X_VAL_1', format=format, array=x_val)
    cols.append(col)
    format = '%dE' % (n_tab * idi.n_band)
    col = pyfits.Column(name='Y_VAL_1', format=format, array=y_val)
    cols.append(col)
    col = pyfits.Column(name='GAIN_1', format=format, array=gain)
    cols.append(col)
    format = '%dE' % idi.n_band
    col = pyfits.Column(name='SENS_1',
                        format=format,
                        unit='K/Jy',
                        array=sens_1)
    cols.append(col)
    if len(pols) > 1:
        format = '%dJ' % idi.n_band
        col = pyfits.Column(name='TYPE_2', format=format, array=_type)
        cols.append(col)
        col = pyfits.Column(name='NTERM_2', format=format, array=nterm)
        cols.append(col)
        col = pyfits.Column(name='X_TYP_2', format=format, array=x_typ)
        cols.append(col)
        col = pyfits.Column(name='Y_TYP_2', format=format, array=y_typ)
        cols.append(col)
        format = '%dE' % idi.n_band
        col = pyfits.Column(name='X_VAL_2', format=format, array=x_val)
        cols.append(col)
        format = '%dE' % (n_tab * idi.n_band)
        col = pyfits.Column(name='Y_VAL_2', format=format, array=y_val)
        cols.append(col)
        col = pyfits.Column(name='GAIN_2', format=format, array=gain)
        cols.append(col)
        format = '%dE' % idi.n_band
        col = pyfits.Column(name='SENS_2',
                            format=format,
                            unit='K/Jy',
                            array=sens_2)
        cols.append(col)
        pass
    coldefs = pyfits.ColDefs(cols)

    header = pyfits.Header()
    try:
        # Attempt to use the new interfaces available in PyFITS 3.1.x
        # and later.  The old interfaces are still available, but
        # generate warnings that say they're deprecated.
        header['EXTNAME'] = 'GAIN_CURVE'
        header['EXTVER'] = 1
        header['TABREV'] = 1
        header['OBSCODE'] = idi.obscode
        header['NO_STKD'] = idi.n_stokes
        header['STK_1'] = idi.stk_1
        header['NO_BAND'] = idi.n_band
        header['NO_CHAN'] = idi.n_chan
        header['REF_FREQ'] = idi.ref_freq
        header['CHAN_BW'] = idi.chan_bw
        header['REF_PIXL'] = idi.ref_pixl
        # Repeat the reference data even though the FITS-IDI standard
        # doesn't seem to require it.
        header['RDATE'] = idi.rdate
        header['NO_POL'] = len(pols)
        header['NO_TABS'] = n_tab
        tbhdu = pyfits.BinTableHDU.from_columns(coldefs, header)
    except:
        # Fall back on the old interfaces available in PyFits 3.0.x
        # and earlier.
        header.update('EXTNAME', 'GAIN_CURVE')
        header.update('EXTVER', 1)
        header.update('TABREV', 1)
        header.update('OBSCODE', idi.obscode)
        header.update('NO_STKD', idi.n_stokes)
        header.update('STK_1', idi.stk_1)
        header.update('NO_BAND', idi.n_band)
        header.update('NO_CHAN', idi.n_chan)
        header.update('REF_FREQ', idi.ref_freq)
        header.update('CHAN_BW', idi.chan_bw)
        header.update('REF_PIXL', idi.ref_pixl)
        # Repeat the reference data even though the FITS-IDI standard doesn't
        # seem to require it.
        header.update('RDATE', idi.rdate)
        header.update('NO_POL', len(pols))
        header.update('NO_TABS', n_tab)
        rows = len(nterm)
        tbhdu = pyfits.core.new_table(coldefs, header, rows, False,
                                      'BinTableHDU')
        pass

    hdulist = pyfits.open(idifile, mode='append')
    hdulist.append(tbhdu)
    hdulist.close()
    return
Ejemplo n.º 3
0
def append_tsys(antabfile, idifiles):
    # Make sure we're dealing with a list
    if not isinstance(idifiles, list):
        idifiles = [idifiles]
        pass

    # Check if we already have a SYSTEM_TEMPERATURES table
    try:
        hdulist = pyfits.open(idifiles[0])
        hdu = hdulist['SYSTEM_TEMPERATURE']
        print('SYSTEM_TEMPERATURE table already present in FITS-IDI file')
        sys.exit(1)
    except KeyError:
        pass

    idi = IdiData(idifiles)
    data = TSysTable()

    pols = []
    keys = StringIO()
    fp = open(antabfile, 'r')
    for line in fp:
        if line.startswith('!'):
            continue
        keys.write(line)
        if line.strip().endswith('/'):
            keys.seek(0)
            try:
                tsys = key.read_keyfile(keys)
            except RuntimeError:
                print("\n", keys.getvalue(), file=sys.stderr)
                sys.exit(1)
                pass
            if tsys and tsys[0] and tsys[0][0][0] == 'TSYS':
                process_values(fp, tsys, pols, idi, data)
                pass
            keys = StringIO()
            continue
        continue

    time = data.time
    time_interval = data.time_interval
    source_id = data.source_id
    antenna_no = data.antenna_no
    array = data.array
    freqid = data.freqid
    tsys_1 = data.tsys_1
    tsys_2 = data.tsys_2
    tant = data.tant

    if len(pols) == 1 and pols[0] == 'L':
        tsys_1 = tsys_2
        pass

    cols = []
    col = pyfits.Column(name='TIME', format='1D', unit='DAYS', array=time)
    cols.append(col)
    col = pyfits.Column(name='TIME_INTERVAL',
                        format='1E',
                        unit='DAYS',
                        array=time_interval)
    cols.append(col)
    col = pyfits.Column(name='SOURCE_ID', format='1J', array=source_id)
    cols.append(col)
    col = pyfits.Column(name='ANTENNA_NO', format='1J', array=antenna_no)
    cols.append(col)
    col = pyfits.Column(name='ARRAY', format='1J', array=array)
    cols.append(col)
    col = pyfits.Column(name='FREQID', format='1J', array=freqid)
    cols.append(col)
    format = '%dE' % idi.n_band
    col = pyfits.Column(name='TSYS_1', format=format, unit='K', array=tsys_1)
    cols.append(col)
    col = pyfits.Column(name='TANT_1', format=format, unit='K', array=tant)
    cols.append(col)
    if len(pols) > 1:
        col = pyfits.Column(name='TSYS_2',
                            format=format,
                            unit='K',
                            array=tsys_2)
        cols.append(col)
        col = pyfits.Column(name='TANT_2', format=format, unit='K', array=tant)
        cols.append(col)
        pass
    coldefs = pyfits.ColDefs(cols)

    header = pyfits.Header()
    try:
        # Attempt to use the new interfaces available in PyFITS 3.1.x
        # and later.  The old interfaces are still available, but
        # generate warnings that say they're deprecated.
        header['EXTNAME'] = 'SYSTEM_TEMPERATURE'
        header['EXTVER'] = 1
        header['TABREV'] = 1
        header['OBSCODE'] = idi.obscode
        header['NO_STKD'] = idi.n_stokes
        header['STK_1'] = idi.stk_1
        header['NO_BAND'] = idi.n_band
        header['NO_CHAN'] = idi.n_chan
        header['REF_FREQ'] = idi.ref_freq
        header['CHAN_BW'] = idi.chan_bw
        header['REF_PIXL'] = idi.ref_pixl
        # Repeat the reference data even though the FITS-IDI standard
        # doesn't seem to require it.
        header['RDATE'] = idi.rdate
        header['NO_POL'] = len(pols)
        tbhdu = pyfits.BinTableHDU.from_columns(coldefs, header)
    except:
        # Fall back on the old interfaces available in PyFits 3.0.x
        # and earlier.
        header.update('EXTNAME', 'SYSTEM_TEMPERATURE')
        header.update('EXTVER', 1)
        header.update('TABREV', 1)
        header.update('OBSCODE', idi.obscode)
        header.update('NO_STKD', idi.n_stokes)
        header.update('STK_1', idi.stk_1)
        header.update('NO_BAND', idi.n_band)
        header.update('NO_CHAN', idi.n_chan)
        header.update('REF_FREQ', idi.ref_freq)
        header.update('CHAN_BW', idi.chan_bw)
        header.update('REF_PIXL', idi.ref_pixl)
        # Repeat the reference data even though the FITS-IDI standard doesn't
        # seem to require it.
        header.update('RDATE', idi.rdate)
        header.update('NO_POL', len(pols))
        rows = len(tsys_1)
        tbhdu = pyfits.core.new_table(coldefs, header, rows, False,
                                      'BinTableHDU')
        pass

    hdulist = pyfits.open(idifiles[0], mode='append')
    hdulist.append(tbhdu)
    hdulist.close()
    return
Ejemplo n.º 4
0
def convert_gaincurve(antab, gc, min_elevation=0.0, max_elevation=90.0):
    tb = casatools.table()

    outfp = tempfile.NamedTemporaryFile('w')

    t = time.strptime("2000y01d00h00m00s", "%Yy%jd%Hh%Mm%Ss")
    btime = time.mktime(t) + 40587.0 * 86400
    t = time.strptime("2100y01d00h00m00s", "%Yy%jd%Hh%Mm%Ss")
    etime = time.mktime(t) + 40587.0 * 86400

    keys = StringIO()
    fp = open(antab, 'r')
    for line in fp:
        if line.startswith('!'):
            continue
        keys.write(line)
        if line.strip().endswith('/'):
            keys.seek(0)
            gain = key.read_keyfile(keys)
            # Standard ANTAB
            if gain and gain[0] and gain[0][0][0] == 'GAIN':
                antenna = find_antenna(gain[0], keyin_keys)
                gain = dict(gain[0])
                try:
                    bfreq = gain['FREQ'][0] * 1e6
                except:
                    bfreq = 0
                    pass
                try:
                    efreq = gain['FREQ'][1] * 1e6
                except:
                    efreq = 1e12
                    pass
                gain_common(gain, antenna, "C", bfreq, efreq, btime, etime,
                            min_elevation, max_elevation, outfp)
            # VLBA gains file
            elif gain and gain[0] and gain[0][0][0] == 'BAND':
                antenna = gain[0][8][0]
                gain = dict(gain[0])
                timerange = gain['TIMERANG']
                btime = parse_timerange(timerange[0:])
                etime = parse_timerange(timerange[4:])
                band = gain['BAND']
                try:
                    freq = vlba_freqs[band]
                    bfreq = freq[0] * 1e9
                    efreq = freq[1] * 1e9
                except:
                    freq = gain['FREQ']
                    bfreq = freq - freq / 4
                    efreq = freq + freq / 4
                    pass
                gain_common(gain, antenna, band, bfreq, efreq, btime, etime,
                            min_elevation, max_elevation, outfp)
            elif gain and gain[0] and gain[0][0][0] == 'TSYS':
                skip_values(fp)
                pass
            keys = StringIO()
            continue
        continue

    outfp.flush()

    tb.fromascii(gc,
                 asciifile=outfp.name,
                 sep=' ',
                 columnnames=columnnames,
                 datatypes=datatypes)

    outfp.close()
    fp.close()
    return
Ejemplo n.º 5
0
def append_wx(wxfile, idifiles):
    # Check if we already have a WEATHER table
    try:
        hdulist = pyfits.open(idifiles[0])
        hdu = hdulist['WEATHER']
        print('WEATHER table already present in FITS-IDI file')
        sys.exit(1)
    except KeyError:
        pass

    idi = idiData(idifiles)
    data = WXTable()

    keys = StringIO()
    fp = open(args[0], 'r')
    for line in fp:
        if line.startswith('!'):
            continue
        keys.write(line)
        if line.strip().endswith('/'):
            keys.seek(0)
            try:
                wx = key.read_keyfile(keys)
            except RuntimeError:
                print("\n", keys.getvalue(), file=sys.stderr)
                sys.exit(1)
                pass
            if wx and wx[0] and wx[0][0][0] == 'WEATHER':
                process_values(fp, wx, idi, data)
                pass
            keys = StringIO()
            continue
        continue

    time = data.time
    time_interval = data.time_interval
    antenna_no = data.antenna_no
    temperature = data.temperature
    pressure = data.pressure
    dewpoint = data.dewpoint
    wind_velocity = data.wind_velocity
    wind_direction = data.wind_direction
    wind_gust = data.wind_gust
    data.precipitation = data.precipitation
    wvr_h20 = data.wvr_h2o
    ionos_electron = data.ionos_electron

    cols = []
    col = pyfits.Column(name='TIME', format='1D', unit='DAYS', array=time)
    cols.append(col)
    col = pyfits.Column(name='TIME_INTERVAL', format='1E', unit='DAYS',
                        array=time_interval)
    cols.append(col)
    col = pyfits.Column(name='ANTENNA_NO', format='1J', array=antenna_no)
    cols.append(col)
    col = pyfits.Column(name='TEMPERATURE', format='1E', unit='CENTIGRADE',
                        array=temperature)
    cols.append(col)
    col = pyfits.Column(name='PRESSURE', format='1E', unit='MILLIBARS',
                        array=pressure)
    cols.append(col)
    col = pyfits.Column(name='DEWPOINT', format='1E', unit='CENTIGRADE',
                        array=dewpoint)
    cols.append(col)
    col = pyfits.Column(name='WIND_VELOCITY', format='1E', unit='M/SEC',
                        array=wind_velocity)
    cols.append(col)
    col = pyfits.Column(name='WIND_DIRECTION', format='1E', unit='DEGREES',
                        array=wind_direction)
    cols.append(col)
    col = pyfits.Column(name='WIND_GUST', format='1E', unit='M/SEC',
                        array=wind_gust)
    cols.append(col)
    col = pyfits.Column(name='PRECIPITATION', format='1E', unit='CM',
                        array=precipitation)
    cols.append(col)
    col = pyfits.Column(name='WVR_H2O', format='1E', array=wvr_h2o)
    cols.append(col)
    col = pyfits.Column(name='IONOS_ELECTRON', format='1E', array=ionos_electron)
    cols.append(col)
    coldefs = pyfits.ColDefs(cols)

    header = pyfits.Header()
    try:
        # Attempt to use the new interfaces available in PyFITS 3.1.x and
        # later.  The old interfaces are still available, but generate
        # warnings that say they're deprecated.
        header['EXTNAME'] = 'WEATHER'
        header['EXTVER'] = 1
        header['TABREV'] = 1
        header['OBSCODE'] = idi.obscode
        header['NO_STKD'] = idi.n_stokes
        header['STK_1'] = idi.stk_1
        header['NO_BAND'] = idi.n_band
        header['NO_CHAN'] = idi.n_chan
        header['REF_FREQ'] = idi.ref_freq
        header['CHAN_BW'] = idi.chan_bw
        header['REF_PIXL'] = idi.ref_pixl
        # Repeat the reference data even though the FITS-IDI standard doesn't
        # seem to require it.
        header['RDATE'] = idi.rdate
        tbhdu = pyfits.BinTableHDU.from_columns(coldefs, header)
    except:
        # Fall back on the old interfaces available in PyFits 3.0.x and
        # earlier.
        header.update('EXTNAME', 'WEATHER')
        header.update('EXTVER', 1)
        header.update('TABREV', 1)
        header.update('OBSCODE', idi.obscode)
        header.update('NO_STKD', idi.n_stokes)
        header.update('STK_1', idi.stk_1)
        header.update('NO_BAND', idi.n_band)
        header.update('NO_CHAN', idi.n_chan)
        header.update('REF_FREQ', idi.ref_freq)
        header.update('CHAN_BW', idi.chan_bw)
        header.update('REF_PIXL', idi.ref_pixl)
        # Repeat the reference data even though the FITS-IDI standard doesn't
        # seem to require it.
        header.update('RDATE', idi.rdate)
        rows = len(temperature)
        tbhdu = pyfits.core.new_table(coldefs, header, rows, False, 'BinTableHDU')
        pass

    hdulist = pyfits.open(idifiles[0], mode='append')
    hdulist.append(tbhdu)
    hdulist.close()
    return
Ejemplo n.º 6
0
    scan_times.append([start, end])
scan_times = sorted(scan_times)

outfp = tempfile.NamedTemporaryFile('w')

pols = []
keys = StringIO.StringIO()
fp = open(antab, 'r')
for line in fp:
    if line.startswith('!'):
        continue
    keys.write(line)
    if line.strip().endswith('/'):
        keys.seek(0)
        try:
            tsys = key.read_keyfile(keys)
        except RuntimeError:
            print >> sys.stderr, "\n", keys.getvalue()
            sys.exit(1)
        if tsys[0][0][0] == 'TSYS':
            process_values(fp, tsys, pols, vis)
            pass
        keys = StringIO.StringIO()
        continue
    continue

write_values(outfp)

outfp.flush()

tb.open(vis)