Ejemplo n.º 1
0
def find_beam(image):
    f = fits.open(image)
    if 'BEAM' in f[0].header.keys():
        if os.path.exists(f[0].header['BEAM']):
            logger.info('Found existing primary beam in header: %s' %
                        f[0].header['BEAM'])
            return f[0].header['BEAM']
    if not 'DELAYS' in f[0].header.keys():
        return None
    delays = [int(x) for x in f[0].header['DELAYS'].split(',')]
    logger.info('Creating primary beam for %s' % image)
    out = make_beam.make_beam(image,
                              ext=0,
                              delays=delays,
                              model='analytic',
                              jones=False,
                              precess=False)
    if f[0].header['CRVAL4'] == -5:
        # XX
        return out[0]
    elif f[0].header['CRVAL4'] == -6:
        # YY
        return out[1]
    elif f[0].header['CRVAL4'] == 1:
        # I
        fXX = fits.open(out[0])
        fYY = fits.open(out[1])
        fXX[0].data = 0.5 * (fXX[0].data + fYY[0].data)
        fXX[0].header['CRVAL4'] = 1
        out = out[0].replace('_beamXX', '_beamI')
        if os.path.exists(out):
            os.remove(out)
        fXX.writeto(out)
        return out
Ejemplo n.º 2
0
def find_beam(image):
    f=fits.open(image)
    if 'BEAM' in f[0].header.keys():
        if os.path.exists(f[0].header['BEAM']):
            logger.info('Found existing primary beam in header: %s' % f[0].header['BEAM'])
            return f[0].header['BEAM']
    if not 'DELAYS' in f[0].header.keys():
        return None
    delays=[int(x) for x in f[0].header['DELAYS'].split(',')]
    logger.info('Creating primary beam for %s' % image)
    out=make_beam.make_beam(image, ext=0,
                            delays=delays,
                            analytic_model=True,
                            jones=False,
                            precess=False)
    if f[0].header['CRVAL4']==-5:
        # XX
        return out[0]
    elif f[0].header['CRVAL4']==-6:
        # YY
        return out[1]
    elif f[0].header['CRVAL4']==1:
        # I
        fXX=fits.open(out[0])
        fYY=fits.open(out[1])
        fXX[0].data=0.5*(fXX[0].data+fYY[0].data)
        fXX[0].header['CRVAL4']=1
        out=out[0].replace('_beamXX','_beamI')
        if os.path.exists(out):
            os.remove(out)
        fXX.writeto(out)
        return out            
Ejemplo n.º 3
0
def ft_beam(vis=None,
            refant='Tile012',
            clobber=True,
            spectral_beam=False,
            subcalibrator=False,
            uvrange='>0.03klambda'):
    """
	def ft_beam(vis=None,refant='Tile012',clobber=True,
	spectral_beam=False,
	subcalibrator=False,uvrange='>0.03klambda'):

	# Reference antenna
	refant='Tile012'
	# Overwrite files
	clobber=True
	# Option to include the spectral index of the primary beam
	spectral_beam=False
	# Option to add more sources to the field
	"""
    # output calibration solution
    caltable = re.sub('ms', 'cal', vis)
    if vis is None or len(vis) == 0 or not os.path.exists(vis):
        print 'Input visibility must be defined'
        return None

    # Get the frequency information of the measurement set
    ms.open(vis)
    rec = ms.getdata(['axis_info'])
    df, f0 = (rec['axis_info']['freq_axis']['resolution'][len(
        rec['axis_info']['freq_axis']['resolution']) / 2],
              rec['axis_info']['freq_axis']['chan_freq'][len(
                  rec['axis_info']['freq_axis']['resolution']) / 2])
    F = rec['axis_info']['freq_axis']['chan_freq'].squeeze() / 1e6
    df = df[0] * len(rec['axis_info']['freq_axis']['resolution'])
    f0 = f0[0]
    rec_time = ms.getdata(['time'])
    sectime = qa.quantity(rec_time['time'][0], unitname='s')
    midfreq = f0
    bandwidth = df
    if isinstance(qa.time(sectime, form='fits'), list):
        dateobs = qa.time(sectime, form='fits')[0]
    else:
        dateobs = qa.time(sectime, form='fits')

    if spectral_beam:
        # Start and end of the channels so we can make the spectral beam image
        startfreq = f0 - df / 2
        endfreq = f0 + df / 2
        freq_array = [midfreq, startfreq, endfreq]
    else:
        freq_array = [midfreq]

    # Get observation number directly from the measurement set
    tb.open(vis + '/OBSERVATION')
    obsnum = int(tb.getcol('MWA_GPS_TIME'))
    tb.close

    info = mwapy.get_observation_info.MWA_Observation(obsnum, db=db)
    print 'Retrieved observation info for %d...\n%s\n' % (obsnum, info)

    # Calibrator information
    if info.calibration:
        calibrator = info.calibrators
    else:
        # Observation wasn't scheduled properly so calibrator field is missing: try parsing the fieldname
        # assuming it's something like 3C444_81
        calibrator = info.filename.rsplit('_', 1)[0]

    print 'Calibrator is %s...' % calibrator

    # subcalibrators not yet improving the calibration, probably due to poor beam model
    if subcalibrator and calibrator == 'PKS0408-65':
        subcalibrator = 'PKS0410-75'
    elif subcalibrator and calibrator == 'HerA':
        subcalibrator = '3C353'
    else:
        subcalibrator = False

    # Start models are 150MHz Jy/pixel fits files in a known directory
    model = modeldir + calibrator + '.fits'
    # With a corresponding spectral index map
    spec_index = modeldir + calibrator + '_spec_index.fits'

    if not os.path.exists(model):
        print 'Could not find calibrator model %s' % model
        return None

    # Generate the primary beam
    delays = info.delays
    str_delays = ','.join(map(str, delays))
    print 'Delays are: %s' % str_delays

    # load in the model FITS file as a template for later
    ftemplate = pyfits.open(model)

    # do this for the start, middle, and end frequencies
    for freq in freq_array:
        freqstring = str(freq / 1.0e6) + 'MHz'
        # We'll generate images in the local directory at the right frequency for this ms
        outname = calibrator + '_' + freqstring
        outnt2 = calibrator + '_' + freqstring + '_nt2'
        # import model, edit header so make_beam generates the right beam in the right place
        if os.path.exists(outname + '.fits') and clobber:
            os.remove(outname + '.fits')
        shutil.copy(model, outname + '.fits')
        fp = pyfits.open(outname + '.fits', 'update')
        fp[0].header['CRVAL3'] = freq
        fp[0].header['CDELT3'] = bandwidth
        fp[0].header['DATE-OBS'] = dateobs
        fp.flush()

        print 'Creating primary beam models...'
        beamarray = make_beam.make_beam(outname + '.fits', delays=delays)
        # delete the temporary model
        os.remove(outname + '.fits')

        beamimage = {}

        for stokes in ['XX', 'YY']:
            beamimage[
                stokes] = calibrator + '_' + freqstring + '_beam' + stokes + '.fits'

    # scale by the primary beam
    # Correct way of doing this is to generate separate models for XX and YY
    # Unfortunately, ft doesn't really understand cubes
    # So instead we just use the XX model, and then scale the YY solution later

    freq = midfreq
    freqstring = str(freq / 1.0e6) + 'MHz'
    outname = calibrator + '_' + freqstring
    outnt2 = calibrator + '_' + freqstring + '_nt2'
    # divide to make a ratio beam, so we know how to scale the YY solution later
    fbeamX = pyfits.open(beamimage['XX'])
    fbeamY = pyfits.open(beamimage['YY'])
    ratiovalue = (fbeamX[0].data / fbeamY[0].data).mean()
    print 'Found <XX/YY>=%.2f' % ratiovalue

    # Models are at 150MHz
    # Generate scaled image at correct frequency
    if os.path.exists(outname + '.fits') and clobber:
        os.remove(outname + '.fits')
    # Hardcoded to use the XX beam in the model
    fbeam = fbeamX
    fmodel = pyfits.open(model)
    fspec_index = pyfits.open(spec_index)

    ftemplate[0].data = fbeam[0].data * fmodel[0].data / (
        (150000000 / f0)**(fspec_index[0].data))
    ftemplate[0].header['CRVAL3'] = freq
    ftemplate[0].header['CDELT3'] = bandwidth
    ftemplate[0].header['DATE-OBS'] = dateobs
    ftemplate[0].header['CRVAL4'] = 1
    ftemplate.writeto(outname + '.fits')
    print 'Wrote scaled model to %s' % (outname + '.fits')
    foutname = pyfits.open(outname + '.fits')

    # Generate 2nd Taylor term
    if os.path.exists(outnt2 + '.fits') and clobber:
        os.remove(outnt2 + '.fits')

    if spectral_beam:
        # Generate spectral image of the beam
        fcalstart = pyfits.open(calibrator + '_' + str(startfreq / 1.0e6) +
                                'MHz_beamXX.fits')
        fcalend = pyfits.open(calibrator + '_' + str(endfreq / 1.0e6) +
                              'MHz_beamXX.fits')
        ftemplate[0].data = (n.log(fcalstart[0].data / fcalend[0].data) /
                             n.log((f0 - df / 2) / (f0 + df / 2)))
        beam_spec = '%s_%sMHz--%sMHz_beamXX.fits' % (
            calibrator, str(startfreq / 1.0e6), str(endfreq / 1.0e6))
        if os.path.exists(beam_spec):
            os.remove(beam_spec)
        ftemplate.writeto(beam_spec)
        fbeam_spec = pyfits.open(beam_spec)

        ftemplate[0].data = foutname[0].data * fbeam[0].data * (
            fspec_index[0].data + fbeam_spec[0].data)
    else:
        ftemplate[
            0].data = foutname[0].data * fbeam[0].data * fspec_index[0].data
    ftemplate[0].header['DATE-OBS'] = dateobs
    ftemplate.writeto(outnt2 + '.fits')
    print 'Wrote scaled Taylor term to %s' % (outnt2 + '.fits')

    # import as CASA images
    if os.path.exists(outname + '.im') and clobber:
        tasks.rmtables(outname + '.im')
    if os.path.exists(outnt2 + '.im') and clobber:
        tasks.rmtables(outnt2 + '.im')
    tasks.importfits(outname + '.fits', outname + '.im')
    tasks.importfits(outnt2 + '.fits', outnt2 + '.im')
    print 'Fourier transforming model...'
    tasks.ft(vis=vis,
             model=[outname + '.im', outnt2 + '.im'],
             nterms=2,
             usescratch=True)

    print 'Calibrating...'
    tasks.bandpass(vis=vis, caltable=caltable, refant=refant, uvrange=uvrange)

    print 'Scaling YY solutions by beam ratio...'
    # Scale YY solution by the ratio
    tb.open(caltable)
    G = tb.getcol('CPARAM')
    tb.close()

    new_gains = n.empty(shape=G.shape, dtype=n.complex128)

    # XX gains stay the same
    new_gains[0, :, :] = G[0, :, :]
    # YY gains are scaled
    new_gains[1, :, :] = ratiovalue * G[1, :, :]

    tb.open(caltable, nomodify=False)
    tb.putcol('CPARAM', new_gains)
    tb.putkeyword('MODEL', model)
    tb.putkeyword('SPECINDX', spec_index)
    tb.putkeyword('BMRATIO', ratiovalue)
    try:
        tb.putkeyword('MWAVER', mwapy.__version__)
    except:
        pass
    tb.close()
    print 'Created %s!' % caltable
    return caltable
Ejemplo n.º 4
0
def main():
    usage="Usage: %prog [options] <images>\n"
    usage+='\tRegrids all images onto a common coordinate frame\n'
    usage+='\tFrame is that of template image (if specified), otherwise first image\n'
    usage+='\tImage size is expanded by a given factor if desired\n'
    parser = OptionParser(usage=usage)
    parser.add_option('-i','--image',dest='image',default=None,
                      help='Template image base')
    parser.add_option('-x','--expandx',dest='factorx',default=2,
                      type='int',
                      help='X Expansion factor [default=%default]')
    parser.add_option('-y','--expandy',dest='factory',default=2,
                      type='int',
                      help='Y Expansion factor [default=%default]')
    parser.add_option('--projection',dest='projection',type='choice',
                      default='ZEA',
                      choices=['ZEA','SIN','AIT','HPX','MOL'],
                      help='Image projection [default=%default]')
    parser.add_option('--deczero',dest='deczero',default=False,
                      action='store_true',
                      help='Set reference Dec to 0 for AIT and MOL?')
    parser.add_option('--nobeam',dest='beam',default=False,
                      action='store_true',
                      help='Do not compute primary beams')
    parser.add_option('-v','--verbose',action="store_true",
                      dest="verbose",default=False,
                      help="Increase verbosity of output")

    db=None
    execname=inspect.stack()[0][1]
    imin=1
    while sys.argv[imin] != execname:
        imin+=1
    (options, args) = parser.parse_args(args=sys.argv[imin+1:])
    images=args
    dobeam=not options.beam
    if options.image is None:
        image0=images[0]
    else:
        image0=options.image
    template='_template.fits'
    factorx=options.factorx
    factory=options.factory

    f=pyfits.open(image0)
    shape0=f[0].data.shape
    f[0].data=numpy.zeros((shape0[0],shape0[1],
                           factory*shape0[2],factorx*shape0[3]))
    f[0].header['CRPIX1']=shape0[3]/2*factorx+1
    f[0].header['CRPIX2']=shape0[2]/2*factory+1

    if os.path.exists(template):
        os.remove(template)
    f.writeto(template)
    print 'Created template (%dx%d pixels) from %s' % (factorx*shape0[3],
                                                       factory*shape0[2],
                                                       image0)

    templateroot,ext=os.path.splitext(template)
    if os.path.exists('%s.image' % templateroot):
        os.system('rm -r %s.image' % templateroot)
    ia.fromfits(templateroot + '.image',template)
    csys=ia.coordsys()
    if not options.projection==csys.projection()['type']:
        csys.setprojection(options.projection)
        if options.projection in ['MOL','AIT'] and options.deczero:
            # set the Dec reference to 0
            ddec=csys.increment()['numeric'][1]*180/math.pi
            ref=csys.referencevalue(format='n')
            dec0=ref['numeric'][1]*180/math.pi
            ref['numeric'][1]=0            
            csys.setreferencevalue(ref)
            refpix=csys.referencepixel()
            refpix['numeric'][1]+=int(math.fabs(dec0/ddec))
            csys.setreferencepixel(value=refpix['numeric'])

        if os.path.exists('%s_%s.image' % (templateroot,options.projection)):
            os.system('rm -r %s_%s.image' % (templateroot,options.projection))
        im2=ia.regrid(templateroot + '_' + options.projection + '.image',
                      csys=csys.torecord(),
                      overwrite=True)    
        ia.close()
        ia.open(templateroot + '_' + options.projection + '.image')
        if os.path.exists(templateroot + '_' + options.projection + '.fits'):
            os.remove(templateroot + '_' + options.projection + '.fits')

            print 'Created %s template %s' % (options.projection,
                                              templateroot + '_' + options.projection + '.fits')
            ia.tofits(templateroot + '_' + options.projection + '.fits')
    shape=ia.shape()
    ia.close()

    new=[]
    newbeam=[]
    for im in images:
        if options.verbose:
            print 'Working on %s...' % im
        fi=pyfits.open(im)
        try:
            obsid=fi[0].header['GPSTIME']
            delays=fi[0].header['DELAYS']
            delays=map(int,delays.split(','))
        except:
            if db is None:
                from mwapy.obssched.base import schedule
                db = schedule.getdb()
            d,f=os.path.split(im)
            obsid=int(f.split('-')[0])            
            info=get_observation_info.MWA_Observation(obsid,db=db)
            delays=info.delays

        if dobeam:
            f,e=os.path.splitext(im)
            if Vstring in f and not (os.path.exists(f+'_beamXX'+e) or os.path.exists(f+'_beamI'+e)):
                f=f.replace(Vstring,Istring)
            if not os.path.exists(f+'_beamI'+e):
                if not os.path.exists(f+'_beamXX'+e):
                    # need to create the beam
                    from mwapy.pb import make_beam
                    beamfiles=make_beam.make_beam(im, delays=delays)
                else:
                    print 'XX beam %s already exists' % (f+'_beamXX'+e)
                    beamfiles=[f+'_beamXX'+e,
                               f+'_beamYY'+e]
                if not os.path.exists(f+'_beamI'+e):
                    fx=pyfits.open(beamfiles[0])
                    fy=pyfits.open(beamfiles[1])
                    fx[0].data=0.5*(fx[0].data+fy[0].data)
                    beamfileI=beamfiles[0].replace('beamXX','beamI')
                    if os.path.exists(beamfileI):
                        os.remove(beamfileI)
                    fx.writeto(beamfileI)
                    print 'Made beam for %s: %s' % (im,beamfileI)
            else:
                print 'I beam %s already exists' % (f+'_beamI'+e)
                beamfileI=f+'_beamI'+e
        try:
            outfits=regrid(im, csys, shape, options.projection)
            print 'Regridded %s -> %s' % (im,outfits)
            new.append(outfits)
            if dobeam:
                newbeam.append(regrid(beamfileI, csys, shape, 
                                      options.projection))
                print 'Regridded %s -> %s' % (beamfileI,newbeam[-1])
        except IndexError:
            print 'Failed to regrid %s' % im
Ejemplo n.º 5
0
            logger.error('Cannot find DELAYS in %s' % options.metafits)
            sys.exit(1)            
        options.delays=f[0].header['DELAYS']
        try:
            options.delays=[int(x) for x in options.delays.split(',')]
        except Exception,e:
            logger.error('Unable to parse beamformer delays %s: %s' % (options.delays,e))
            sys.exit(1)
            
    if options.filename is None:
        logger.error('Must supply a filename')
        sys.exit(1)

    out=make_beam.make_beam(options.filename, ext=ext, delays=options.delays,
                            model=options.model,
                            jones=options.jones,
                            precess=options.precess,
                            interp=options.interp,
                            dipheight=options.height, dip_sep=options.separation)
    if out  is None:
        logger.error('Problem creating primary beams')
        sys.exit(1)
    

    sys.exit(0)
            
    

######################################################################

if __name__=="__main__":
    main()
Ejemplo n.º 6
0
            logger.error('Cannot find DELAYS in %s' % options.metafits)
            sys.exit(1)            
        options.delays=f[0].header['DELAYS']
        try:
            options.delays=[int(x) for x in options.delays.split(',')]
        except Exception,e:
            logger.error('Unable to parse beamformer delays %s: %s' % (options.delays,e))
            sys.exit(1)
            
    if options.filename is None:
        logger.error('Must supply a filename')
        sys.exit(1)

    out=make_beam.make_beam(options.filename, ext=ext, delays=options.delays,
                            analytic_model=options.analytic_model,
                            jones=options.jones,
                            precess=options.precess,
                            dipheight=options.height, dip_sep=options.separation)
    if out  is None:
        logger.error('Problem creating primary beams')
        sys.exit(1)
    

    sys.exit(0)
            
    

######################################################################

if __name__=="__main__":
    main()
Ejemplo n.º 7
0
def main():

    localswarpconfig = os.path.join(os.path.split(__file__)[0], swarpconfig)

    usage = "Usage: %prog [options] <files>\n"
    usage += "\tMosaic together MWA driftscan data\n"
    #usage+="\tImages should be either raw XX,YY or primary-beam corrected I (or Q or U or V)\n"
    usage += "\tImages should be either raw XX,YY or primary-beam corrected I\n"
    usage += "\tNormal operation:\n"
    usage += "\tXX,YY data will be converted into Stokes I\n"
    usage += "\tStokes I beams will be computed for optimal weighting\n"
    usage += "\tExcept if --separate and then only XX,YY will be processed and mosaiced separately\n"
    usage += "\tswarp is used for mosaicing\n"
    parser = OptionParser(usage=usage,
                          version=mwapy.__version__ + ' ' + mwapy.__date__)
    parser.add_option('--metafits',
                      action="store_true",
                      dest="metafits",
                      default=False,
                      help="Force creation of metafits files?")
    parser.add_option(
        '--copybeam',
        action="store_true",
        dest="copy_beam",
        default=False,
        help="Copy the beam for the driftscan instead of creating separately?")
    parser.add_option(
        '--analytic',
        action="store_true",
        dest="analytic_model",
        default=False,
        help=
        "Use the old analytic dipole model, instead of the default Sutinjo 2014 model."
    )
    parser.add_option(
        '--noprecess',
        action='store_false',
        dest='precess',
        default=True,
        help=
        'Do not precess coordinates to current epoch (faster but less accurate) [default=False]'
    )
    parser.add_option('-t',
                      '--threshold',
                      dest='threshold',
                      default=0.05,
                      type='float',
                      help='Beam threshold [default=%default]')
    parser.add_option('--reduce',
                      dest='reduce',
                      default=False,
                      action='store_true',
                      help='Reduce dimensionality of input images?')
    parser.add_option('--oversample',
                      dest='oversample',
                      default=2,
                      type='int',
                      help='Swarp oversampling [default=%default]')
    parser.add_option(
        '--ra',
        dest='ra',
        default=None,
        help='Center RA of mosaic in hours or "middle" [default=auto]')
    parser.add_option('--dec',
                      dest='dec',
                      default=None,
                      help='Center Dec of mosaic in degrees [default=auto]')
    parser.add_option('--proj',
                      dest='projection',
                      default='MOL',
                      type='choice',
                      choices=['MOL', 'ZEA', 'SIN', 'AIT'],
                      help='Image projection [default=%default]')
    parser.add_option(
        '--scale',
        '--scalar',
        dest='scalarweight',
        default=None,
        help=
        'Additional scalar weight header keyword for each image [default=None]'
    )
    parser.add_option(
        '--function',
        dest='weightfunction',
        default='inversesquare',
        type='choice',
        choices=['inversesquare', 'inverse', 'linear', 'square'],
        help=
        'Function applied to scalar weight (higher weight is better) [default=%default]'
    )
    parser.add_option(
        '--separate',
        dest='separate',
        default=False,
        action='store_true',
        help='Mosaic XX and YY separately as well [default=%default]')
    parser.add_option('--arguments',
                      dest='swarpcommands',
                      default='',
                      help='Additional arguments for Swarp')
    parser.add_option('--out',
                      dest='root',
                      default='mosaic',
                      help='SWARP output root [default=%default]')
    parser.add_option('--swarp',
                      dest='swarp',
                      default=None,
                      help='Path to Swarp executable [default=search path]')
    parser.add_option('--config',
                      dest='config',
                      default=None,
                      help='Path to separate Swarp config file [default=%s]' %
                      localswarpconfig)
    parser.add_option('--clean',
                      action="store_true",
                      dest="clean",
                      default=False,
                      help="Clean temporary files?")
    parser.add_option('-v',
                      '--verbose',
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Increase verbosity of output")
    (options, args) = parser.parse_args()

    if (options.verbose):
        logger.setLevel(logging.INFO)

    if options.config is not None:
        localswarpconfig = options.config
    if not os.path.exists(localswarpconfig):
        logger.error('Cannot find swarp config file %s' % localswarpconfig)
        sys.exit(1)
    logger.info('Will use config file %s...' % localswarpconfig)

    inputfiles = args
    if len(inputfiles) == 0:
        logger.error('Must supply >=1 input images')
        sys.exit(1)
    Imagelist = {}

    if options.swarp is None:
        # python 2.6 compatibility
        try:
            swarp = subprocess.check_output(['which', 'swarp']).strip()
        except AttributeError:
            swarp = subprocess.Popen(
                ['which', 'swarp'],
                stdout=subprocess.PIPE).communicate()[0].strip()
    else:
        swarp = options.swarp

    ##############################
    # go through the images
    # figure out which is for which polarization
    # and see if there is also a metafits file
    ##############################
    times = {}
    times['start'] = time.time()
    for file in inputfiles:
        polaxis = None
        try:
            obsid = int(file.split('_')[0])
        except:
            try:
                # allow for wsclean-style output
                obsid = int(file.split('-')[0])
            except:
                logger.error('Unable to interpret filename %s' % file)
                sys.exit(0)

        if not Imagelist.has_key(obsid):
            Imagelist[obsid] = MWAImage(obsid)
        mwaimage = Imagelist[obsid]
        currentstokes = None
        try:
            f = pyfits.open(file)
            if f[0].header['CTYPE3'] == 'STOKES':
                polaxis = 3
            if f[0].header['CTYPE4'] == 'STOKES':
                polaxis = 4
            if not polaxis is None:
                if f[0].header['CRVAL%d' % polaxis] == -5:
                    mwaimage.ximage = file
                    currentstokes = 'XX'
                elif f[0].header['CRVAL%d' % polaxis] == -6:
                    mwaimage.yimage = file
                    currentstokes = 'YY'
                elif f[0].header['CRVAL%d' % polaxis] >= 1 and f[0].header[
                        'CRVAL%d' % polaxis] <= 4:
                    mwaimage.iimage = file
                    currentstokes = 'I'
                else:
                    logger.warning(
                        'Do not know how to process Stokes=%d for %s; skipping...'
                        % (f[0].header['CRVAL%d' % polaxis], file))
                    continue
            else:
                if 'XX' in file:
                    mwaimage.ximage = file
                    currentstokes = 'XX'
                elif 'YY' in file:
                    mwaimage.yimage = file
                    currentstokes = 'YY'

            # see if beams already exist
            # would be identified through header keywords
            if 'BEAM' in f[0].header.keys():
                if currentstokes == 'I' and os.path.exists(
                        f[0].header['BEAM']):
                    mwaimage.ibeam = f[0].header['BEAM']
                elif currentstokes == 'XX' and os.path.exists(
                        f[0].header['BEAM']):
                    mwaimage.xbeam = f[0].header['BEAM']
                elif currentstokes == 'YY' and os.path.exists(
                        f[0].header['BEAM']):
                    mwaimage.ybeam = f[0].header['BEAM']

        except:
            if 'XX' in file:
                mwaimage.ximage = file
                currentstokes = 'XX'
            elif 'YY' in file:
                mwaimage.yimage = file
                currentstokes = 'YY'

        if os.path.exists('%d.metafits' % obsid):
            mwaimage.metafits = '%d.metafits' % obsid

    logger.info('Found %d input images for %d observations\n' %
                (len(inputfiles), len(Imagelist.keys())))

    times['beams'] = time.time()
    ##############################
    # now loop through and do what needs to be done
    ##############################
    beams = {}
    tempfiles = []
    for obsid in sorted(Imagelist.keys()):
        logger.info('Processing %d...' % obsid)
        if Imagelist[obsid].ximage is None and Imagelist[
                obsid].yimage is not None:
            logger.error('Obsid %d has YY image but no XX image' % obsid)
            sys.exit(1)
        if Imagelist[obsid].yimage is None and Imagelist[
                obsid].ximage is not None:
            logger.error('Obsid %d has XX image but no YY image' % obsid)
            sys.exit(1)
        s = '%d: ' % obsid
        if Imagelist[obsid].ximage is not None:
            s += '(XX,YY)=(%s, %s)' % (Imagelist[obsid].ximage,
                                       Imagelist[obsid].yimage)
        if Imagelist[obsid].iimage is not None:
            s += ' I=%s' % Imagelist[obsid].iimage
        if Imagelist[obsid].metafits is not None:
            s += ' meta=%s' % Imagelist[obsid].metafits
        logger.info(s)

        if Imagelist[obsid].xbeam is not None or Imagelist[
                obsid].ibeam is not None:
            s = '%d: ' % obsid
            if Imagelist[obsid].xbeam is not None:
                s += '(XXbeam,YYbeam)=(%s, %s)' % (Imagelist[obsid].xbeam,
                                                   Imagelist[obsid].ybeam)
            if Imagelist[obsid].ibeam is not None:
                s += ' Ibeam=%s' % Imagelist[obsid].ibeam
            logger.info(s)

        if Imagelist[obsid].iimage is None:
            # no I image exists: deal with XX, YY
            fx = pyfits.open(Imagelist[obsid].ximage)
            fy = pyfits.open(Imagelist[obsid].yimage)
            # Get scaling from imageheader
            if options.scalarweight is not None:
                try:
                    Imagelist[obsid].xscalarweight = weightfunction(
                        fx[0].header[options.scalarweight],
                        options.weightfunction)
                except:
                    logger.warning(
                        'Unable to read header keyword "%s" from image %s; setting I scalar weight to 1'
                        % (options.scalarweight, Imagelist[obsid].ximage))
                    Imagelist[obsid].xscalarweight = 1
                try:
                    Imagelist[obsid].yscalarweight = weightfunction(
                        fy[0].header[options.scalarweight],
                        options.weightfunction)
                except:
                    logger.warning(
                        'Unable to read header keyword "%s" from image %s; setting I scalar weight to 1'
                        % (options.scalarweight, Imagelist[obsid].yimage))
                    Imagelist[obsid].yscalarweight = 1

            try:
                result = fx[0].header['DELAYS'] is not None and fx[0].header[
                    'DATE-OBS'] is not None
                xheader = True
            except KeyError:
                xheader = False
            try:
                result = fy[0].header['DELAYS'] is not None and fy[0].header[
                    'DATE-OBS'] is not None
                yheader = True
            except KeyError:
                yheader = False
            if not (xheader or yheader) or options.metafits:
                # does not have the necessary keys
                # need to make metafits
                if Imagelist[obsid].metafits is None:
                    logger.warning(
                        'FITS header keywords not present for obsid %d; creating metafits...'
                        % obsid)
                    if not _newmetadata:
                        hh = make_metafiles.Corr2UVFITSHeader(obsid, db=db)
                        T = make_metafiles.instrument_configuration(
                            gpstime=obsid, duration=hh.obs.duration, db=db)
                        result = T.make_instr_config(quick=True)
                        if result is None:
                            logger.error(
                                'Error making instr_config file for %d' %
                                obsid)
                            sys.exit(1)
                        hh.make_header()
                        T.corr2uvfitsheader = hh
                        h = T.make_metafits(quick=True)
                    else:
                        # use newer interface
                        obs = metadata.MWA_Observation(obsid)
                        if obs.observation_number is None:
                            logger.error('No observation found for %s' % obsid)
                            sys.exit(1)
                        m = metadata.instrument_configuration(obsid)
                        h = m.make_metafits(quick=True)

                    Imagelist[obsid].metafits = '%d.metafits' % obsid
                    if os.path.exists(Imagelist[obsid].metafits):
                        os.remove(Imagelist[obsid].metafits)
                    try:
                        h.writeto(Imagelist[obsid].metafits)
                        logger.info('Metafits for %d written to %s' %
                                    (obsid, Imagelist[obsid].metafits))
                    except Exception, e:
                        logger.error(
                            'Unable to write metafits file for %d:\n%s' %
                            (obsid, e))
                        sys.exit(1)
            if not xheader:
                fx.close()
                if not Imagelist[obsid].metafits is None:
                    logger.warning(
                        'Copying metafits from %s to %s' %
                        (Imagelist[obsid].metafits, Imagelist[obsid].ximage))
                    result = copy_metafits(Imagelist[obsid].ximage,
                                           Imagelist[obsid].metafits)
                else:
                    # assume we can get it from the YY image?
                    logger.warning(
                        'Copying metafits from %s to %s' %
                        (Imagelist[obsid].yimage, Imagelist[obsid].ximage))
                    result = copy_metafits(Imagelist[obsid].ximage,
                                           Imagelist[obsid].yimage,
                                           keys=['DELAYS', 'DATE-OBS'])

                if not result:
                    logger.error('Error updating FITS header of %s' %
                                 Imagelist[obsid].ximage)
                    sys.exit(1)
            if not yheader:
                fy.close()
                if not Imagelist[obsid].metafits is None:
                    logger.warning(
                        'Copying metafits from %s to %s' %
                        (Imagelist[obsid].metafits, Imagelist[obsid].yimage))
                    result = copy_metafits(Imagelist[obsid].yimage,
                                           Imagelist[obsid].metafits)
                else:
                    # assume we can get it from the YY image?
                    logger.warning(
                        'Copying metafits from %s to %s' %
                        (Imagelist[obsid].ximage, Imagelist[obsid].yimage))
                    result = copy_metafits(Imagelist[obsid].yimage,
                                           Imagelist[obsid].ximage,
                                           keys=['DELAYS', 'DATE-OBS'])

                if not result:
                    logger.error('Error updating FITS header of %s' %
                                 Imagelist[obsid].yimage)
                    sys.exit(1)

            ##############################
            # now create the beams
            ##############################
            fx = pyfits.open(Imagelist[obsid].ximage, 'update')
            fy = pyfits.open(Imagelist[obsid].yimage, 'update')
            delays = [int(x) for x in fx[0].header['DELAYS'].split(',')]
            if Imagelist[obsid].xbeam is not None and Imagelist[
                    obsid].ybeam is not None:
                logger.info(
                    'XX,YY beams already exists for %d; using %s,%s...' %
                    (obsid, Imagelist[obsid].xbeam, Imagelist[obsid].ybeam))

            elif not options.copy_beam or (not beams.has_key(
                    fx[0].header['DELAYS'])):
                logger.info('Creating primary beams for %d' % obsid)
                out = make_beam.make_beam(
                    Imagelist[obsid].ximage,
                    ext=0,
                    delays=delays,
                    analytic_model=options.analytic_model,
                    jones=False,
                    precess=options.precess)
                if out is None:
                    logger.error('Problem creating primary beam for %s' %
                                 obsid)
                    sys.exit(1)
                Imagelist[obsid].xbeam, Imagelist[obsid].ybeam = out
                beams[fx[0].header['DELAYS']] = obsid
                logger.info('Wrote %s,%s' % (out[0], out[1]))
                fx[0].header['BEAM'] = Imagelist[obsid].xbeam
                fy[0].header['BEAM'] = Imagelist[obsid].ybeam
                fx.flush()
                fy.flush()
                tempfiles += out
            else:
                logger.info('Copying beam from %s to %s' %
                            (Imagelist[beams[fx[0].header['DELAYS']]].ximage,
                             Imagelist[obsid].ximage))
                result = copy_driftscanbeam(
                    Imagelist[beams[fx[0].header['DELAYS']]].ximage,
                    Imagelist[obsid].ximage)
                if result is None:
                    sys.exit(1)
                Imagelist[obsid].xbeam, Imagelist[obsid].ybeam = result
                fx[0].header['BEAM'] = Imagelist[obsid].xbeam
                fy[0].header['BEAM'] = Imagelist[obsid].ybeam
                fx.flush()
                fy.flush()
                tempfiles += result
            fx.close()
            fy.close()
            fx = pyfits.open(Imagelist[obsid].ximage)
            fy = pyfits.open(Imagelist[obsid].yimage)

            if not options.separate:
                # and primary-beam correct
                fxB = pyfits.open(Imagelist[obsid].xbeam)
                fyB = pyfits.open(Imagelist[obsid].ybeam)
                fx[0].data = (fx[0].data / fxB[0].data +
                              fy[0].data / fyB[0].data) / 2.0
                fx[0].header['CRVAL%d' % polaxis] = 1
                Imagelist[obsid].iimage = Imagelist[obsid].ximage.replace(
                    '_XX', '_I')
                if os.path.exists(Imagelist[obsid].iimage):
                    os.remove(Imagelist[obsid].iimage)
                fx.writeto(Imagelist[obsid].iimage)
                logger.info('Wrote I image %s' % Imagelist[obsid].iimage)
                tempfiles.append(Imagelist[obsid].iimage)
                fxB[0].data = (fxB[0].data + fyB[0].data) / 2.0
                fxB[0].header['CRVAL%d' % polaxis] = 1
                Imagelist[obsid].ibeam = Imagelist[obsid].xbeam.replace(
                    '_beamXX', '_beamI')
                Imagelist[obsid].ibeam = Imagelist[obsid].ibeam.replace(
                    '_XX', '_I')
                if os.path.exists(Imagelist[obsid].ibeam):
                    os.remove(Imagelist[obsid].ibeam)
                fxB.writeto(Imagelist[obsid].ibeam)
                logger.info('Wrote I beam %s' % Imagelist[obsid].ibeam)
                fi = pyfits.open(Imagelist[obsid].iimage, 'update')
                fi[0].header['BEAM'] = Imagelist[obsid].ibeam
                fi.flush()
                fi.close()
                tempfiles.append(Imagelist[obsid].ibeam)

            else:
                # want to make corrected XX and YY
                fxB = pyfits.open(Imagelist[obsid].xbeam)
                fyB = pyfits.open(Imagelist[obsid].ybeam)
                fx[0].data = (fx[0].data / fxB[0].data)
                Imagelist[obsid].ximage = Imagelist[obsid].ximage.replace(
                    '_XX', '_XXcorr')
                if os.path.exists(Imagelist[obsid].ximage):
                    os.remove(Imagelist[obsid].ximage)
                fx.writeto(Imagelist[obsid].ximage)
                logger.info('Wrote corrected XX image %s' %
                            Imagelist[obsid].ximage)
                tempfiles.append(Imagelist[obsid].ximage)
                # and copy the XX beam to correspond to the new name
                shutil.copy(
                    Imagelist[obsid].xbeam, Imagelist[obsid].xbeam.replace(
                        '_XX', '_XXcorr').replace('_YY', '_YYcorr'))
                logger.info(
                    'Copied XX beam from %s -> %s' %
                    (Imagelist[obsid].xbeam, Imagelist[obsid].xbeam.replace(
                        '_XX', '_XXcorr').replace('_YY', '_YYcorr')))
                Imagelist[obsid].xbeam = Imagelist[obsid].xbeam.replace(
                    '_XX', '_XXcorr').replace('_YY', '_YYcorr')
                tempfiles.append(Imagelist[obsid].xbeam)
                fy[0].data = (fy[0].data / fyB[0].data)
                Imagelist[obsid].yimage = Imagelist[obsid].yimage.replace(
                    '_YY', '_YYcorr')
                if os.path.exists(Imagelist[obsid].yimage):
                    os.remove(Imagelist[obsid].yimage)
                fy.writeto(Imagelist[obsid].yimage)
                logger.info('Wrote corrected YY image %s' %
                            Imagelist[obsid].yimage)
                tempfiles.append(Imagelist[obsid].yimage)
                # and copy the YY beam to correspond to the new name
                shutil.copy(
                    Imagelist[obsid].ybeam, Imagelist[obsid].ybeam.replace(
                        '_XX', '_XXcorr').replace('_YY', '_YYcorr'))
                logger.info(
                    'Copied YY beam from %s -> %s' %
                    (Imagelist[obsid].ybeam, Imagelist[obsid].ybeam.replace(
                        '_XX', '_XXcorr').replace('_YY', '_YYcorr')))
                Imagelist[obsid].ybeam = Imagelist[obsid].ybeam.replace(
                    '_XX', '_XXcorr').replace('_YY', '_YYcorr')
                tempfiles.append(Imagelist[obsid].ybeam)

        else:
            # Stokes I (corrected) exists
            # now just make I beam
            fi = pyfits.open(Imagelist[obsid].iimage)
            try:
                result = fi[0].header['DELAYS'] is not None and fi[0].header[
                    'DATE-OBS'] is not None
                iheader = True
            except KeyError:
                iheader = False
            if not (iheader) or options.metafits:
                # does not have the necessary keys
                # need to make metafits
                if Imagelist[obsid].metafits is None:
                    logger.warning(
                        'FITS header keywords not present for obsid %d; creating metafits...'
                        % obsid)

                    if not _newmetadata:
                        hh = make_metafiles.Corr2UVFITSHeader(obsid, db=db)
                        T = make_metafiles.instrument_configuration(
                            gpstime=obsid, duration=hh.obs.duration, db=db)
                        result = T.make_instr_config(quick=True)
                        if result is None:
                            logger.error(
                                'Error making instr_config file for %d' %
                                obsid)
                            sys.exit(1)
                        hh.make_header()
                        T.corr2uvfitsheader = hh
                        h = T.make_metafits(quick=True)
                    else:
                        # use newer interface
                        obs = metadata.MWA_Observation(obsid)
                        if obs.observation_number is None:
                            logger.error('No observation found for %s' % obsid)
                            sys.exit(1)
                        m = metadata.instrument_configuration(obsid)
                        h = m.make_metafits(quick=True)

                    Imagelist[obsid].metafits = '%d.metafits' % obsid
                    if os.path.exists(Imagelist[obsid].metafits):
                        os.remove(Imagelist[obsid].metafits)
                    try:
                        h.writeto(Imagelist[obsid].metafits)
                        logger.info('Metafits for %d written to %s' %
                                    (obsid, Imagelist[obsid].metafits))
                    except Exception, e:
                        logger.error(
                            'Unable to write metafits file for %d:\n%s' %
                            (obsid, e))
                        sys.exit(1)
            if not iheader:
                fi.close()
                if not Imagelist[obsid].metafits is None:
                    logger.warning(
                        'Copying metafits from %s to %s' %
                        (Imagelist[obsid].metafits, Imagelist[obsid].iimage))
                    result = copy_metafits(Imagelist[obsid].iimage,
                                           Imagelist[obsid].metafits)
            if not result:
                logger.error('Error updating FITS header of %s' %
                             Imagelist[obsid].iimage)
                sys.exit(1)
            ##############################
            # now create the beams
            ##############################

            fi = pyfits.open(Imagelist[obsid].iimage, 'update')
            delays = [int(x) for x in fi[0].header['DELAYS'].split(',')]

            if Imagelist[obsid].ibeam is not None:
                logger.info('I beam already exists for %d; using %s...' %
                            (obsid, Imagelist[obsid].ibeam))
            else:
                if not options.copy_beam or (not beams.has_key(
                        fi[0].header['DELAYS'])):
                    logger.info('Creating primary beams for %d' % obsid)
                    out = make_beam.make_beam(
                        Imagelist[obsid].iimage,
                        ext=0,
                        delays=delays,
                        analytic_model=options.analytic_model,
                        jones=False,
                        precess=options.precess)
                    if out is None:
                        logger.error('Problem creating primary beam for %s' %
                                     obsid)
                        sys.exit(1)
                    Imagelist[obsid].xbeam, Imagelist[obsid].ybeam = out
                    beams[fi[0].header['DELAYS']] = obsid
                    logger.info('Wrote %s,%s' % (out[0], out[1]))
                    tempfiles += out
                else:
                    logger.info(
                        'Copying beam from %s to %s' %
                        (Imagelist[beams[fi[0].header['DELAYS']]].iimage,
                         Imagelist[obsid].iimage))
                    result = copy_driftscanbeam(
                        Imagelist[beams[fi[0].header['DELAYS']]].iimage,
                        Imagelist[obsid].iimage)
                    if result is None:
                        sys.exit(1)
                    Imagelist[obsid].xbeam, Imagelist[obsid].ybeam = result
                    tempfiles += result
                # create the I beam
                fxB = pyfits.open(Imagelist[obsid].xbeam)
                fyB = pyfits.open(Imagelist[obsid].ybeam)
                fxB[0].data = (fxB[0].data + fyB[0].data) / 2.0
                fxB[0].header['CRVAL%d' % polaxis] = 1
                Imagelist[obsid].ibeam = Imagelist[obsid].xbeam.replace(
                    '_beamXX', '_beamI')
                Imagelist[obsid].ibeam = Imagelist[obsid].ibeam.replace(
                    '_XX', '_I')
                if os.path.exists(Imagelist[obsid].ibeam):
                    os.remove(Imagelist[obsid].ibeam)
                fxB.writeto(Imagelist[obsid].ibeam)
                logger.info('Wrote I beam %s' % Imagelist[obsid].ibeam)
                # update the header so that the beam can be re-used in the future
                fi[0].header['BEAM'] = Imagelist[obsid].ibeam
                fi.flush()
                fi.close()
                tempfiles.append(Imagelist[obsid].ibeam)
Ejemplo n.º 8
0
def main():


    localswarpconfig=os.path.join(os.path.split(__file__)[0], swarpconfig)
    
    usage="Usage: %prog [options] <files>\n"
    usage+="\tMosaic together MWA driftscan data\n"
    usage+="\tImages should be either raw XX,YY or primary-beam corrected I (or Q or U or V)\n"
    usage+="\tXX,YY data will be converted into Stokes I\n"
    usage+="\tStokes I beams will be computed for optimal weighting\n"
    usage+="\tswarp is used for mosaicing\n"
    parser = OptionParser(usage=usage,version=mwapy.__version__ + ' ' + mwapy.__date__)
    parser.add_option('--metafits',action="store_true",dest="metafits",default=False,
                      help="Force creation of metafits files?")
    parser.add_option('--copybeam',action="store_true",dest="copy_beam",default=False,
                      help="Copy the beam for the driftscan instead of creating separately?")
    parser.add_option('--analytic',action="store_true",dest="analytic_model",default=False,
                      help="Use the old analytic dipole model, instead of the default Sutinjo 2014 model.")
    parser.add_option('--noprecess',action='store_false',
                      dest='precess',default=True,
                      help='Do not precess coordinates to current epoch (faster but less accurate) [default=False]')
    parser.add_option('-t','--threshold',dest='threshold',default=0.05,
                      type='float',
                      help='Beam threshold [default=%default]')
    parser.add_option('--reduce',dest='reduce',default=False,
                      action='store_true',
                      help='Reduce dimensionality of input images?')
    parser.add_option('--oversample',dest='oversample', default=2,
                      type='int',
                      help='Swarp oversampling [default=%default]')
    parser.add_option('--ra',dest='ra', default=None,
                      help='Center RA of mosaic in hours or "middle" [default=auto]')
    parser.add_option('--dec',dest='dec', default=None,
                      help='Center Dec of mosaic in degrees [default=auto]')
    parser.add_option('--proj',dest='projection',default='MOL',
                      type='choice',
                      choices=['MOL','ZEA','SIN','AIT'],
                      help='Image projection [default=%default]')    
    parser.add_option('--arguments',dest='swarpcommands',default='',
                      help='Additional arguments for Swarp')
    parser.add_option('--out',dest='root', default='mosaic',
                      help='SWARP output root [default=%default]')
    parser.add_option('--swarp',dest='swarp',default=None,
                      help='Path to Swarp executable [default=search path]')
    parser.add_option('--config',dest='config',default=None,
                      help='Path to separate Swarp config file [default=%s]' % localswarpconfig)

    parser.add_option('--clean',action="store_true",dest="clean",default=False,
                      help="Clean temporary files?")
    parser.add_option('-v','--verbose',action="store_true",dest="verbose",default=False,
                      help="Increase verbosity of output")
    (options, args) = parser.parse_args()

        
    if (options.verbose):
        logger.setLevel(logging.INFO)

    if options.config is not None:
        localswarpconfig=options.config
    if not os.path.exists(localswarpconfig):
        logger.error('Cannot find swarp config file %s' % localswarpconfig)
        sys.exit(1)
    logger.info('Will use config file %s...' % localswarpconfig)

    inputfiles=args
    Imagelist={}

    if options.swarp is None:
        # python 2.6 compatibility
        try:
            swarp=subprocess.check_output(['which','swarp']).strip()
        except AttributeError:
            swarp=subprocess.Popen(['which', 'swarp'],
                                   stdout=subprocess.PIPE).communicate()[0].strip()                              
    else:
        swarp=options.swarp

    # go through the images
    # figure out which is for which polarization
    # and see if there is also a metafits file
    times={}
    times['start']=time.time()
    for file in inputfiles:
        polaxis=None
        results=file.split('_')
        obsid=int(results[0])

        if not Imagelist.has_key(obsid):
            Imagelist[obsid]=MWAImage(obsid)
        mwaimage=Imagelist[obsid]            
        currentstokes=None
        try:
            f=pyfits.open(file)
            if f[0].header['CTYPE3']=='STOKES':
                polaxis=3
            if f[0].header['CTYPE4']=='STOKES':
                polaxis=4
            if not polaxis is None:
                if f[0].header['CRVAL%d' % polaxis]==-5:
                    mwaimage.ximage=file
                    currentstokes='XX'
                elif f[0].header['CRVAL%d' % polaxis]==-6:
                    mwaimage.yimage=file
                    currentstokes='YY'
                elif f[0].header['CRVAL%d' % polaxis]>=1 and f[0].header['CRVAL%d' % polaxis]<=4:
                    mwaimage.iimage=file
                    currentstokes='I'
                else:
                    logger.warning('Do not know how to process Stokes=%d for %s; skipping...' % (
                        f[0].header['CRVAL%d' % polaxis], file))
                    continue
            else:
                if 'XX' in file:
                    mwaimage.ximage=file
                    currentstokes='XX'
                elif 'YY' in file:
                    mwaimage.yimage=file                
                    currentstokes='YY'
                
            # see if beams already exist
            # would be identified through header keywords
            if 'BEAM' in f[0].header.keys():
                if currentstokes=='I' and os.path.exists(f[0].header['BEAM']):
                    mwaimage.ibeam=f[0].header['BEAM']
                elif currentstokes=='XX' and os.path.exists(f[0].header['BEAM']):
                    mwaimage.xbeam=f[0].header['BEAM']
                elif currentstokes=='YY' and os.path.exists(f[0].header['BEAM']):
                    mwaimage.ybeam=f[0].header['BEAM']
                    
        except:
            if 'XX' in file:
                mwaimage.ximage=file
                currentstokes='XX'
            elif 'YY' in file:
                mwaimage.yimage=file
                currentstokes='YY'
                
        if os.path.exists('%d.metafits' % obsid):
            mwaimage.metafits='%d.metafits' % obsid

    logger.info('Found %d input images for %d observations\n' % (
        len(inputfiles), len(Imagelist.keys())))

    times['beams']=time.time()
    ##############################
    # now loop through and do what needs to be done
    ##############################
    beams={}
    tempfiles=[]
    for obsid in sorted(Imagelist.keys()):
        logger.info('Processing %d...' % obsid)
        if Imagelist[obsid].ximage is None and Imagelist[obsid].yimage is not None:
            logger.error('Obsid %d has YY image but no XX image' % obsid)
            sys.exit(1)
        if Imagelist[obsid].yimage is None  and Imagelist[obsid].ximage is not None:
            logger.error('Obsid %d has XX image but no YY image' % obsid)
            sys.exit(1)
        s='%d: ' % obsid
        if Imagelist[obsid].ximage is not None:
            s+='(XX,YY)=(%s, %s)' % (Imagelist[obsid].ximage,
                                     Imagelist[obsid].yimage)
        if Imagelist[obsid].iimage is not None:
            s+=' I=%s' % Imagelist[obsid].iimage
        if Imagelist[obsid].metafits is not None:
            s+=' meta=%s' % Imagelist[obsid].metafits
        logger.info(s)

        if Imagelist[obsid].xbeam is not None or Imagelist[obsid].ibeam is not None:
            s='%d: ' % obsid
            if Imagelist[obsid].xbeam is not None:
                s+='(XXbeam,YYbeam)=(%s, %s)' % (Imagelist[obsid].xbeam,
                                                 Imagelist[obsid].ybeam)
            if Imagelist[obsid].ibeam is not None:
                s+=' Ibeam=%s' % Imagelist[obsid].ibeam
            logger.info(s)

        if Imagelist[obsid].iimage is None:
            # no I image exists: deal with XX, YY
            fx=pyfits.open(Imagelist[obsid].ximage)
            fy=pyfits.open(Imagelist[obsid].yimage)
            try:
                result=fx[0].header['DELAYS'] is not None and fx[0].header['DATE-OBS'] is not None
                xheader=True
            except KeyError:
                xheader=False
            try:
                result=fy[0].header['DELAYS'] is not None and fy[0].header['DATE-OBS'] is not None
                yheader=True
            except KeyError:
                yheader=False
            if not (xheader or yheader) or options.metafits:
                # does not have the necessary keys
                # need to make metafits
                if Imagelist[obsid].metafits is None:                
                    logger.warning('FITS header keywords not present for obsid %d; creating metafits...' % obsid)
                    hh=make_metafiles.Corr2UVFITSHeader(obsid, db=db)
                    T=make_metafiles.instrument_configuration(gpstime=obsid, duration=hh.obs.duration,
                                                              db=db)
                    result=T.make_instr_config(quick=True)
                    if result is None:
                        logger.error('Error making instr_config file for %d' % obsid)
                        sys.exit(1)
                    Imagelist[obsid].metafits='%d.metafits' % obsid
                    hh.make_header()
                    T.corr2uvfitsheader=hh
                    h=T.make_metafits(quick=True)                                    
                    if os.path.exists(Imagelist[obsid].metafits):
                        os.remove(Imagelist[obsid].metafits)
                    try:
                        h.writeto(Imagelist[obsid].metafits)
                        logger.info('Metafits for %d written to %s' % (obsid, Imagelist[obsid].metafits))
                    except Exception, e:
                        logger.error('Unable to write metafits file for %d:\n%s' % (obsid,e))
                        sys.exit(1)
            if not xheader:
                fx.close()
                if not Imagelist[obsid].metafits is None:
                    logger.warning('Copying metafits from %s to %s' % (
                        Imagelist[obsid].metafits,
                        Imagelist[obsid].ximage))
                    result=copy_metafits(Imagelist[obsid].ximage,
                                         Imagelist[obsid].metafits)
                else:
                    # assume we can get it from the YY image?
                    logger.warning('Copying metafits from %s to %s' % (
                        Imagelist[obsid].yimage,
                        Imagelist[obsid].ximage))
                    result=copy_metafits(Imagelist[obsid].ximage,
                                         Imagelist[obsid].yimage,
                                         keys=['DELAYS', 'DATE-OBS'])
                
                if not result:
                    logger.error('Error updating FITS header of %s' % Imagelist[obsid].ximage)
                    sys.exit(1)
            if not yheader:
                fy.close()
                if not Imagelist[obsid].metafits is None:
                    logger.warning('Copying metafits from %s to %s' % (
                        Imagelist[obsid].metafits,
                        Imagelist[obsid].yimage))               
                    result=copy_metafits(Imagelist[obsid].yimage,
                                         Imagelist[obsid].metafits)
                else:
                    # assume we can get it from the YY image?
                    logger.warning('Copying metafits from %s to %s' % (
                        Imagelist[obsid].ximage,
                        Imagelist[obsid].yimage))               
                    result=copy_metafits(Imagelist[obsid].yimage,
                                         Imagelist[obsid].ximage,
                                         keys=['DELAYS', 'DATE-OBS'])
                
                if not result:
                    logger.error('Error updating FITS header of %s' % Imagelist[obsid].yimage)
                    sys.exit(1)

            ##############################
            # now create the beams
            ##############################
            fx=pyfits.open(Imagelist[obsid].ximage,'update')
            fy=pyfits.open(Imagelist[obsid].yimage,'update')
            delays=[int(x) for x in fx[0].header['DELAYS'].split(',')]
            if Imagelist[obsid].xbeam is not None and Imagelist[obsid].ybeam is not None:
                logger.info('XX,YY beams already exists for %d; using %s,%s...' % (
                    obsid, Imagelist[obsid].xbeam, Imagelist[obsid].ybeam))

            elif not options.copy_beam or (not beams.has_key(fx[0].header['DELAYS'])):            
                logger.info('Creating primary beams for %d' % obsid)
                out=make_beam.make_beam(Imagelist[obsid].ximage, ext=0, delays=delays,
                                        analytic_model=options.analytic_model,
                                        jones=False,
                                        precess=options.precess)
                if out is None:
                    logger.error('Problem creating primary beam for %s' % obsid)
                    sys.exit(1)
                Imagelist[obsid].xbeam, Imagelist[obsid].ybeam=out
                beams[fx[0].header['DELAYS']]=obsid
                logger.info('Wrote %s,%s' % (out[0], out[1]))
                fx[0].header['BEAM']=Imagelist[obsid].xbeam
                fy[0].header['BEAM']=Imagelist[obsid].ybeam
                fx.flush()
                fy.flush()
                tempfiles+=out
            else:
                logger.info('Copying beam from %s to %s' % (Imagelist[beams[fx[0].header['DELAYS']]].ximage,
                                                            Imagelist[obsid].ximage))
                result=copy_driftscanbeam(Imagelist[beams[fx[0].header['DELAYS']]].ximage,
                                          Imagelist[obsid].ximage)
                if result is None:
                    sys.exit(1)
                Imagelist[obsid].xbeam, Imagelist[obsid].ybeam=result
                fx[0].header['BEAM']=Imagelist[obsid].xbeam
                fy[0].header['BEAM']=Imagelist[obsid].ybeam
                fx.flush()
                fy.flush()
                tempfiles+=result
            fx.close()
            fy.close()
            fx=pyfits.open(Imagelist[obsid].ximage)
            fy=pyfits.open(Imagelist[obsid].yimage)

            # and primary-beam correct
            fxB=pyfits.open(Imagelist[obsid].xbeam)
            fyB=pyfits.open(Imagelist[obsid].ybeam)
            fx[0].data=(fx[0].data/fxB[0].data+fy[0].data/fyB[0].data)/2.0
            fx[0].header['CRVAL%d' % polaxis]=1
            Imagelist[obsid].iimage=Imagelist[obsid].ximage.replace('_XX','_I')
            if os.path.exists(Imagelist[obsid].iimage):
                os.remove(Imagelist[obsid].iimage)
            fx.writeto(Imagelist[obsid].iimage)
            logger.info('Wrote I image %s' % Imagelist[obsid].iimage)
            tempfiles.append(Imagelist[obsid].iimage)
            fxB[0].data=(fxB[0].data+fyB[0].data)/2.0
            fxB[0].header['CRVAL%d' % polaxis]=1            
            Imagelist[obsid].ibeam=Imagelist[obsid].xbeam.replace('_beamXX','_beamI')
            Imagelist[obsid].ibeam=Imagelist[obsid].ibeam.replace('_XX','_I')
            if os.path.exists(Imagelist[obsid].ibeam):
                os.remove(Imagelist[obsid].ibeam)
            fxB.writeto(Imagelist[obsid].ibeam)
            logger.info('Wrote I beam %s' % Imagelist[obsid].ibeam)
            fi=pyfits.open(Imagelist[obsid].iimage, 'update')
            fi[0].header['BEAM']=Imagelist[obsid].ibeam
            fi.flush()
            fi.close()
            tempfiles.append(Imagelist[obsid].ibeam)
        else:
            # Stokes I (corrected) exists
            # now just make I beam
            fi=pyfits.open(Imagelist[obsid].iimage)
            try:
                result=fi[0].header['DELAYS'] is not None and fi[0].header['DATE-OBS'] is not None
                iheader=True
            except KeyError:
                iheader=False
            if not (iheader) or options.metafits:
                # does not have the necessary keys
                # need to make metafits
                if Imagelist[obsid].metafits is None:                
                    logger.warning('FITS header keywords not present for obsid %d; creating metafits...' % obsid)
                    hh=make_metafiles.Corr2UVFITSHeader(obsid, db=db)
                    T=make_metafiles.instrument_configuration(gpstime=obsid, duration=hh.obs.duration,
                                                              db=db)
                    result=T.make_instr_config(quick=True)
                    if result is None:
                        logger.error('Error making instr_config file for %d' % obsid)
                        sys.exit(1)
                    Imagelist[obsid].metafits='%d.metafits' % obsid
                    hh.make_header()
                    T.corr2uvfitsheader=hh
                    h=T.make_metafits(quick=True)                                    
                    if os.path.exists(Imagelist[obsid].metafits):
                        os.remove(Imagelist[obsid].metafits)
                    try:
                        h.writeto(Imagelist[obsid].metafits)
                        logger.info('Metafits for %d written to %s' % (obsid, Imagelist[obsid].metafits))
                    except Exception, e:
                        logger.error('Unable to write metafits file for %d:\n%s' % (obsid,e))
                        sys.exit(1)
            if not iheader:
                fi.close()
                if not Imagelist[obsid].metafits is None:
                    logger.warning('Copying metafits from %s to %s' % (
                        Imagelist[obsid].metafits,
                        Imagelist[obsid].iimage))
                    result=copy_metafits(Imagelist[obsid].iimage,
                                         Imagelist[obsid].metafits)
            if not result:
                logger.error('Error updating FITS header of %s' % Imagelist[obsid].iimage)
                sys.exit(1)
            ##############################
            # now create the beams
            ##############################            
            
            fi=pyfits.open(Imagelist[obsid].iimage, 'update')
            delays=[int(x) for x in fi[0].header['DELAYS'].split(',')]

            if Imagelist[obsid].ibeam is not None:
                logger.info('I beam already exists for %d; using %s...' % (
                    obsid, Imagelist[obsid].ibeam))
            else:
                if not options.copy_beam or (not beams.has_key(fi[0].header['DELAYS'])):
                    logger.info('Creating primary beams for %d' % obsid)
                    out=make_beam.make_beam(Imagelist[obsid].iimage, ext=0, delays=delays,
                                            analytic_model=options.analytic_model,
                                            jones=False,
                                            precess=options.precess)
                    if out is None:
                        logger.error('Problem creating primary beam for %s' % obsid)
                        sys.exit(1)
                    Imagelist[obsid].xbeam, Imagelist[obsid].ybeam=out
                    beams[fi[0].header['DELAYS']]=obsid
                    logger.info('Wrote %s,%s' % (out[0], out[1]))
                    tempfiles+=out
                else:
                    logger.info('Copying beam from %s to %s' % (Imagelist[beams[fi[0].header['DELAYS']]].iimage,
                                                                Imagelist[obsid].iimage))
                    result=copy_driftscanbeam(Imagelist[beams[fi[0].header['DELAYS']]].iimage,
                                              Imagelist[obsid].iimage)
                    if result is None:
                        sys.exit(1)
                    Imagelist[obsid].xbeam, Imagelist[obsid].ybeam=result
                    tempfiles+=result
                # create the I beam
                fxB=pyfits.open(Imagelist[obsid].xbeam)
                fyB=pyfits.open(Imagelist[obsid].ybeam)
                fxB[0].data=(fxB[0].data+fyB[0].data)/2.0
                fxB[0].header['CRVAL%d' % polaxis]=1
                Imagelist[obsid].ibeam=Imagelist[obsid].xbeam.replace('_beamXX','_beamI')
                Imagelist[obsid].ibeam=Imagelist[obsid].ibeam.replace('_XX','_I')
                if os.path.exists(Imagelist[obsid].ibeam):
                    os.remove(Imagelist[obsid].ibeam)
                fxB.writeto(Imagelist[obsid].ibeam)
                logger.info('Wrote I beam %s' % Imagelist[obsid].ibeam)
                # update the header so that the beam can be re-used in the future
                fi[0].header['BEAM']=Imagelist[obsid].ibeam
                fi.flush()
                fi.close()
                tempfiles.append(Imagelist[obsid].ibeam)
Ejemplo n.º 9
0
def main():
    usage = "Usage: %prog [options] <images>\n"
    usage += '\tRegrids all images onto a common coordinate frame\n'
    usage += '\tFrame is that of template image (if specified), otherwise first image\n'
    usage += '\tImage size is expanded by a given factor if desired\n'
    parser = OptionParser(usage=usage)
    parser.add_option('-i',
                      '--image',
                      dest='image',
                      default=None,
                      help='Template image base')
    parser.add_option('-x',
                      '--expandx',
                      dest='factorx',
                      default=2,
                      type='int',
                      help='X Expansion factor [default=%default]')
    parser.add_option('-y',
                      '--expandy',
                      dest='factory',
                      default=2,
                      type='int',
                      help='Y Expansion factor [default=%default]')
    parser.add_option('--projection',
                      dest='projection',
                      type='choice',
                      default='ZEA',
                      choices=['ZEA', 'SIN', 'AIT', 'HPX', 'MOL'],
                      help='Image projection [default=%default]')
    parser.add_option('--deczero',
                      dest='deczero',
                      default=False,
                      action='store_true',
                      help='Set reference Dec to 0 for AIT and MOL?')
    parser.add_option('--nobeam',
                      dest='beam',
                      default=False,
                      action='store_true',
                      help='Do not compute primary beams')
    parser.add_option('-v',
                      '--verbose',
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Increase verbosity of output")

    db = None
    execname = inspect.stack()[0][1]
    imin = 1
    while sys.argv[imin] != execname:
        imin += 1
    (options, args) = parser.parse_args(args=sys.argv[imin + 1:])
    images = args
    dobeam = not options.beam
    if options.image is None:
        image0 = images[0]
    else:
        image0 = options.image
    template = '_template.fits'
    factorx = options.factorx
    factory = options.factory

    f = pyfits.open(image0)
    shape0 = f[0].data.shape
    f[0].data = numpy.zeros(
        (shape0[0], shape0[1], factory * shape0[2], factorx * shape0[3]))
    f[0].header['CRPIX1'] = shape0[3] / 2 * factorx + 1
    f[0].header['CRPIX2'] = shape0[2] / 2 * factory + 1

    if os.path.exists(template):
        os.remove(template)
    f.writeto(template)
    print 'Created template (%dx%d pixels) from %s' % (
        factorx * shape0[3], factory * shape0[2], image0)

    templateroot, ext = os.path.splitext(template)
    if os.path.exists('%s.image' % templateroot):
        os.system('rm -r %s.image' % templateroot)
    ia.fromfits(templateroot + '.image', template)
    csys = ia.coordsys()
    if not options.projection == csys.projection()['type']:
        csys.setprojection(options.projection)
        if options.projection in ['MOL', 'AIT'] and options.deczero:
            # set the Dec reference to 0
            ddec = csys.increment()['numeric'][1] * 180 / math.pi
            ref = csys.referencevalue(format='n')
            dec0 = ref['numeric'][1] * 180 / math.pi
            ref['numeric'][1] = 0
            csys.setreferencevalue(ref)
            refpix = csys.referencepixel()
            refpix['numeric'][1] += int(math.fabs(dec0 / ddec))
            csys.setreferencepixel(value=refpix['numeric'])

        if os.path.exists('%s_%s.image' % (templateroot, options.projection)):
            os.system('rm -r %s_%s.image' % (templateroot, options.projection))
        im2 = ia.regrid(templateroot + '_' + options.projection + '.image',
                        csys=csys.torecord(),
                        overwrite=True)
        ia.close()
        ia.open(templateroot + '_' + options.projection + '.image')
        if os.path.exists(templateroot + '_' + options.projection + '.fits'):
            os.remove(templateroot + '_' + options.projection + '.fits')

            print 'Created %s template %s' % (options.projection,
                                              templateroot + '_' +
                                              options.projection + '.fits')
            ia.tofits(templateroot + '_' + options.projection + '.fits')
    shape = ia.shape()
    ia.close()

    new = []
    newbeam = []
    for im in images:
        if options.verbose:
            print 'Working on %s...' % im
        fi = pyfits.open(im)
        try:
            obsid = fi[0].header['GPSTIME']
            delays = fi[0].header['DELAYS']
            delays = map(int, delays.split(','))
        except:
            if db is None:
                from mwapy.obssched.base import schedule
                db = schedule.getdb()
            d, f = os.path.split(im)
            obsid = int(f.split('-')[0])
            info = get_observation_info.MWA_Observation(obsid, db=db)
            delays = info.delays

        if dobeam:
            f, e = os.path.splitext(im)
            if Vstring in f and not (os.path.exists(f + '_beamXX' + e)
                                     or os.path.exists(f + '_beamI' + e)):
                f = f.replace(Vstring, Istring)
            if not os.path.exists(f + '_beamI' + e):
                if not os.path.exists(f + '_beamXX' + e):
                    # need to create the beam
                    from mwapy.pb import make_beam
                    beamfiles = make_beam.make_beam(im, delays=delays)
                else:
                    print 'XX beam %s already exists' % (f + '_beamXX' + e)
                    beamfiles = [f + '_beamXX' + e, f + '_beamYY' + e]
                if not os.path.exists(f + '_beamI' + e):
                    fx = pyfits.open(beamfiles[0])
                    fy = pyfits.open(beamfiles[1])
                    fx[0].data = 0.5 * (fx[0].data + fy[0].data)
                    beamfileI = beamfiles[0].replace('beamXX', 'beamI')
                    if os.path.exists(beamfileI):
                        os.remove(beamfileI)
                    fx.writeto(beamfileI)
                    print 'Made beam for %s: %s' % (im, beamfileI)
            else:
                print 'I beam %s already exists' % (f + '_beamI' + e)
                beamfileI = f + '_beamI' + e
        try:
            outfits = regrid(im, csys, shape, options.projection)
            print 'Regridded %s -> %s' % (im, outfits)
            new.append(outfits)
            if dobeam:
                newbeam.append(
                    regrid(beamfileI, csys, shape, options.projection))
                print 'Regridded %s -> %s' % (beamfileI, newbeam[-1])
        except IndexError:
            print 'Failed to regrid %s' % im