Ejemplo n.º 1
0
def add_delays(obsnum,fitsimage):
   if os.path.exists(fitsimage):
      metafits=obsnum+'.metafits'

      if os.path.exists(metafits):
         hdu_in=pyfits.open(metafits)
         str_delays=hdu_in[0].header['DELAYS']
         hdu_in.flush()
      else:
         try:
            import mwapy.get_observation_info
            from mwapy.obssched.base import schedule
            db=schedule.getdb()
         except:
            print 'Unable to open connection to database'
            raise KeyboardInterrupt
         info=mwapy.get_observation_info.MWA_Observation(obsnum,db=db)
         delays=info.delays 
         str_delays=','.join(map(str,delays)) 
         db.close()
         schedule.tempdb.close()

      hdu_in=pyfits.open(fitsimage,mode='update')
      hdr_in=hdu_in[0].header
      hdr_in.update('delays',str_delays)
      hdu_in.flush()

      return str_delays
   else:
      print fitsimage+' does not exist!'
Ejemplo n.º 2
0
def getDelays(gpstime):
    try:
        db = schedule.getdb()
    except NameError:
        logger.error(
            'Cannot use the schedule database; use static database instead (getDelays2)'
        )
        return None
    curs = db.cursor()
    delays = {}

    for rx in (1, 2, 3, 4):
        mask = 0
        delays[rx] = {}

        while mask & 255 != 255:
            cmd = "select rx_id,beamformer_mask,delays,update_time,status from bf_status where update_time <= %f and rx_id=%i and beamformer_mask & %i != beamformer_mask order by update_time desc limit 1" % (
                gpstime, rx, mask)
            curs.execute(cmd)
            row = curs.fetchone()
            colnames = [t[0] for t in curs.description]
            data = dict(zip(colnames, row))
            mask |= data['beamformer_mask']
            for slot in range(1, 9):
                if data['beamformer_mask'] & (1 << (slot - 1)) == (1 <<
                                                                   (slot - 1)):
                    if data['status'] == 1:
                        delays[rx][slot] = [
                            int(a) for a in data['delays'][1:-1].split(',')
                        ]
                    else:
                        delays[rx][slot] = None

    mydelay = None
    match = True

    for rx, slots in delays.iteritems():
        for slot, delayset in slots.iteritems():
            if delayset is not None:
                if mydelay is None:
                    mydelay = delayset
                else:
                    if mydelay != delayset:
                        match = False
            else:
                print >> sys.stderr, "Rx %i Slot %i is Off" % (rx, slot)

    if match:
        return mydelay
    else:
        print >> sys.stderr, "No Match!"
        print >> sys.stderr, delays
        return None
Ejemplo n.º 3
0
def find_best_cal(observation_number,cal_dtmax=6e5,available_cals=None,relax=False):
    # open up database connection
    try:
        db = schedule.getdb()
    except:
        logger.error("Unable to open connection to database")
        sys.exit(1)
    logger.debug("searching db for obsnum %d"%observation_number)
    try:
        observation=get_observation_info.MWA_Observation(observation_number,db=db)
    except:
        logger.error('Unable to retrieve observation information for observation %d' % observation_number)
        sys.exit(0)
    matches=find_matches(observation, matchdelays=True, matchfreq=True, dtmax=cal_dtmax,\
    db=db,search_subset=available_cals) 
    if (matches is None or len(matches)==0):
        if relax:
            #logger.warning("""No matches identified for observation %d, relaxing delay match
            #constraint."""%observation_number)
            #logger.warning('WARNING FLUX CAL WILL BE BOGUS')
            matches=find_matches(observation, matchfreq=True, matchdelays=False,dtmax=cal_dtmax,\
                db=db,search_subset=available_cals) 
    if matches is None:
        logger.warning('No matches identified for observation %d' % observation_number)
        sys.exit(1)
    logger.debug("sorting through %d matches for nearest calibrator"%len(matches))
    for match in sorted(matches, key=lambda x: math.fabs(x-observation_number)):
        if match == observation_number:
            continue
        try:
            observation=get_observation_info.MWA_Observation(match,db=db)
        except:
            logger.error('Unable to retrieve observation information for observation %d' % match)
            sys.exit(0)
        if observation.calibration:
            return observation.observation_number
    logger.error("No cal found for observation %d "%observation_number)
    return None
Ejemplo n.º 4
0
from astropy.time import Time

#STUFF RELATED TO PUTTING INFO ON THE PLOTS
import mwapy
from mwapy import ephem_utils
from mwapy.get_observation_info import *
from mwapy.obssched.base import schedule

# configure the logging
logging.basicConfig(format='# %(levelname)s:%(name)s: %(message)s')
logger = logging.getLogger('get_observation_info')
logger.setLevel(logging.WARNING)

# open up database connection
try:
    db = schedule.getdb()
except:
    logger.error("Unable to open connection to database")
    sys.exit(1)

rcParams['font.size'] = 22
rcParams['font.family'] = 'serif'
NBLS = 2000
tic = time.time()
F = n.load(sys.argv[1])
bl_lengths = F['bl_lengths']
Nbl = len(bl_lengths)
delays = F['delays']
freq = F['freq']
windowpad = F['windowpad']
plotres = True
Ejemplo n.º 5
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.º 6
0
def ft_beam(vis=None,
            refant='Tile012',
            clobber=True,
            correct_beam=True,
            spectral_beam=False,
            subcalibrator=False,
            uvrange='>0.03klambda'):
    """
   def ft_beam(vis=None,refant='Tile012',clobber=True,correct_beam=True
   spectral_beam=False,
   subcalibrator=False,uvrange='>0.03klambda'):

   # Reference antenna
   refant='Tile012'
   # Overwrite files
   clobber=True
   # Option to correct for the attenuation effects of the primary beam
   correct_beam=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

    # Try getting the calibrator information from the metafits file

    metafits = str(obsnum) + '.metafits'

    calibrator = ""

    if os.path.exists(metafits):
        hdu_in = pyfits.open(metafits)
        try:
            calibrator = hdu_in[0].header['CALIBSRC']
            str_delays = hdu_in[0].header['DELAYS']
            delays = [int(x) for x in str_delays.split(',')]
        except:
            print 'Unable to retrieve calibrator from metafits file.'

    if not calibrator:
        print 'Could not use the metafits file: trying the observation database.'
        import mwapy.get_observation_info
        from mwapy.obssched.base import schedule
        try:
            db = schedule.getdb()
        except:
            print 'Unable to open connection to database'
            raise KeyboardInterrupt
        info = mwapy.get_observation_info.MWA_Observation(obsnum, db=db)
        db.close()
        schedule.tempdb.close()
        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]
        # Delays
        delays = info.delays
        str_delays = ','.join(map(str, delays))

    print 'Calibrator is %s...' % calibrator
    print 'Delays are: %s' % str_delays

    # 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

    # 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')
        try:
            fp[0].header['CRVAL3'] = freq
            fp[0].header['CDELT3'] = bandwidth
            fp[0].header['DATE-OBS'] = dateobs
        except KeyError:
            fp[0].header.update('CRVAL3', freq)
            fp[0].header.update('CDELT3', bandwidth)
            fp[0].header.update('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'])
    if correct_beam:
        ratiovalue = (fbeamX[0].data / fbeamY[0].data).mean()
        print 'Found <XX/YY>=%.2f' % ratiovalue
    else:
        ratiovalue = 1.0
        print 'Not correcting for the beam: using <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)

    if correct_beam:
        ftemplate[0].data = fbeam[0].data * fmodel[0].data / (
            (150000000 / f0)**(fspec_index[0].data))
    else:
        ftemplate[0].data = fmodel[0].data / (
            (150000000 / f0)**(fspec_index[0].data))

    try:
        ftemplate[0].header['CRVAL3'] = freq
        ftemplate[0].header['CDELT3'] = bandwidth
        ftemplate[0].header['DATE-OBS'] = dateobs
        ftemplate[0].header['CRVAL4'] = 1
    except KeyError:
        ftemplate[0].header.update('CRVAL3', freq)
        ftemplate[0].header.update('CDELT3', bandwidth)
        ftemplate[0].header.update('DATE-OBS', dateobs)
        ftemplate[0].header.update('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 correct_beam:
        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
    else:
        ftemplate[0].data = foutname[0].data

    try:
        ftemplate[0].header['DATE-OBS'] = dateobs
    except KeyError:
        ftemplate[0].header.update('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')
    if not os.path.exists(outname + '.fits'):
        print 'Cannot find %s' % (outname + '.fits')
        return None
    if not os.path.exists(outnt2 + '.fits'):
        print 'Cannot find %s' % (outnt2 + '.fits')
        return None

    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.º 7
0

#STUFF RELATED TO PUTTING INFO ON THE PLOTS
import mwapy
from mwapy import ephem_utils
from mwapy.get_observation_info import *
from mwapy.obssched.base import schedule

# configure the logging
logging.basicConfig(format='# %(levelname)s:%(name)s: %(message)s')
logger=logging.getLogger('get_observation_info')
logger.setLevel(logging.WARNING)

# open up database connection
try:
    db = schedule.getdb()
except:
    logger.error("Unable to open connection to database")
    sys.exit(1)


rcParams['font.size'] = 22
rcParams['font.family']='serif'
NBLS  = 2000
tic = time.time()
F = n.load(sys.argv[1])
bl_lengths = F['bl_lengths']
Nbl  = len(bl_lengths)
delays = F['delays']
freq = F['freq']
windowpad = F['windowpad']
Ejemplo n.º 8
0
def main():
    usage = "Usage: %prog [options] <newhost>\n"
    usage += """
    A tool to change the local database connection
    By default it finds the last mwa.conf file in your search path and modifies the \'dbhost\' entry
    If --local (default) it then writes the output to ./mwa.conf or another file as specified
    If --global it will try to update the last file in the search path
    """
    usage += "\t<newhost> is one of:\n\t%s" % print_hosts().replace(
        '\n', '\n\t')
    usage += "Can accept either the name or the aliases"
    parser = OptionParser(usage=usage, version=mwapy.__version__)
    parser.add_option(
        '-g',
        '--global',
        dest="globalwrite",
        default=False,
        action='store_true',
        help="Try to update the global config file? [default=%default]")
    parser.add_option('-l',
                      '--local',
                      dest="globalwrite",
                      default=False,
                      action='store_false',
                      help="Do not try to update the global config file?")
    parser.add_option('-o',
                      '--output',
                      dest="output",
                      default=_output,
                      help="Output config file [default=%default]")
    parser.add_option(
        '--test',
        action="store_true",
        dest="test",
        default=True,
        help="Test database connection after change? [default=%default]")
    parser.add_option(
        '--notest',
        action="store_false",
        dest="test",
        default=True,
        help="Test database connection after change? [default=%default]")

    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)

    newhost = None

    for arg in args:
        if arg.lower() in _hosts.keys():
            newhost = arg.lower()
        else:
            for host in _aliases.keys():
                if arg.lower() in _aliases[host]:
                    newhost = host
        if newhost is None:
            logger.error('No matching database host identified for \"%s\"' %
                         arg)
            logger.error('Possible hosts:\n%s' % print_hosts())

    if newhost is None:
        logger.error('No database host given')
        logger.error('Possible hosts:\n%s' % print_hosts())
        sys.exit(1)

    logger.info('Will change host to %s (%s)' % (newhost, _hosts[newhost]))

    # get the last config file in the list, and change that one
    lastconfigfile = mwaconfig.CPfile[-1]
    CP = ConfigParser.SafeConfigParser(defaults={})
    CP.read(lastconfigfile)
    if options.globalwrite:
        options.output = lastconfigfile
    # write into default section
    CP.set('', 'dbhost', _hosts[newhost])
    try:
        f = open(options.output, 'w')
    except IOError:
        logger.error('Unable to open %s for writing' % (options.output))
        sys.exit(1)
    CP.write(f)
    f.close()
    logger.info('Wrote %s with dbhost=%s' % (options.output, _hosts[newhost]))

    # reproduce a bit of mwaconfig to reload things here
    CP = ConfigParser.SafeConfigParser(defaults={})
    CPfile = CP.read(mwaconfig.CPpath)
    if not CPfile:
        print "None of the specified configuration files found by mwaconfig.py: %s" % (
            CPpath, )
    for _s in CP.sections():
        for _name, _value in CP.items(_s):
            setattr(mwaconfig.__dict__[_s], _name, _value)

    dbuser = mwaconfig.mandc.dbuser
    dbpassword = mwaconfig.mandc.dbpass
    dbhost = mwaconfig.mandc.dbhost
    dbname = mwaconfig.mandc.dbname

    logger.info(
        'DB setup:\n\tdbuser = %s\n\tdbpassword = %s\n\tdbhost = %s\n\tdbname = %s\n'
        % (dbuser, dbpassword, dbhost, dbname))
    if options.test:
        logger.info('Testing connection to database...')
        # open up database connection
        from mwapy.obssched.base import schedule
        try:
            db = schedule.getdb()
        except:
            logger.error("Unable to open connection to database")
            sys.exit(1)
        logger.info('Connected!')

    sys.exit(0)
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