Пример #1
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
Пример #2
0
    def __setattr__(self, name, value):
        self.__dict__[name] = value

        if (name == 'filenames' and value is not None and len(value) > 0):
            self.ntimefiles = len(value)
            self.ndas = len(value[0])
            for filenames in value:
                for filename in filenames:
                    if not os.path.exists(filename):
                        logger.error('File %s does not exist' % filename)
                        self.filenames = None
                        return None
            self.coarse_channels = 12 * self.ndas
            logger.info('Have %d x %d input files' %
                        (self.ndas, self.ntimefiles))

        elif (name == 'gpstime' and value is not None and value > 0):
            # if the gpstime is set, compute everything else
            self.observation = get_observation_info.MWA_Observation(
                observation_number=self.gpstime, db=self.db)
            if (self.observation.duration <= 0):
                logger.warning('Did not identify observation for gpstime %d' %
                               self.gpstime)
                self.find_closest_observation()
                if (self.observation_number is None):
                    self.observation = None
                    return None
                self.observation = get_observation_info.MWA_Observation(
                    observation_number=self.observation_number, db=self.db)
            else:
                self.observation_number = self.observation.observation_number
            self.center_channel = self.observation.center_channel
            self.duration = self.observation.duration

        elif (name == 'observation_number' and value is not None
              and value > 0):
            self.get_metadata()
Пример #3
0
def find_matches(observation, search_subset=None, matchfreq=True, matchdelays=True, dtmax=None, db=None):
    """
    matches=find_matches(observation, matchfreq=True, matchdelays=True,dtmax=None, db=None)

    returns matches from the observation database that match the supplied observation
    observation can be specified via observation_number or get_observation_info.MWA_Observation object

    matches are via frequency and/or delays
    """

    if db is None:
        logger.error('Must supply a database object')
        return None
    
    if not matchfreq and not matchdelays:
        logger.error('Must match frequencies and/or delays')
        return None
    if (not isinstance(observation, get_observation_info.MWA_Observation)):
        try:
            o=get_observation_info.MWA_Observation(observation,db=db)
        except:
            logger.error('Unable to retrieve observation information for observation %d' % observation)
            return None
        observation=o
    
    delays=observation.delays
    channels=observation.channels
    #receiver=observation.receivers[0]

    
    s='select observation_number from Obsc_Recv_Cmds where '
    if not search_subset is None:
        s+=' observation_number IN ('+','.join(map(str,search_subset))+') and '
    if matchfreq:
        s+='frequency_values = array[%s]' % (
            ','.join([str(x) for x in channels]))
    if matchfreq and matchdelays:
        s+=' and '
    if matchdelays:
        s+=' xdelaysetting[1:1]=array[array[%s]]' % (
        ','.join([str(x) for x in delays]))
    if dtmax is not None:
        if matchfreq or matchdelays:
            s+=' and '
        s+=' abs(observation_number - %d) < %d' % (observation.observation_number, dtmax)
    logger.debug("SQL = %s"%s)
    res=dbobj.execute(s,db=db)

    return set([xx[0] for xx in res])
Пример #4
0
from mwapy.pb import primary_beam
from mwapy.obssched.base import schedule
import astropy.io.fits as pyfits

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

RA, Dec = 69.3158945833333, -47.2523944444444
gpstime = 1063400456
filename = '1063400456_corr_phased.fits'

o = get_observation_info.MWA_Observation(gpstime, db=db)

mwatime = ephem_utils.MWATime(gpstime=gpstime + o.duration / 2)
frequency = o.center_channel * 1.28e6
delays = o.delays

RAnow, Decnow = ephem_utils.precess(RA, Dec, 2000, mwatime.epoch)
HA = float(mwatime.LST) - RAnow
mwa = ephem_utils.Obs[ephem_utils.obscode['MWA']]
Az, Alt = ephem_utils.eq2horz(HA, Decnow, mwa.lat)
theta = (90 - Alt) * math.pi / 180
phi = Az * math.pi / 180


def MWA_Jones_Analytic(theta,
                       phi,
Пример #5
0
def populate_qc(obsid, verbose=False):
    """
    populate_qc(obsid, verbose=False)
    actually does the population
    """
    try:
        observation_info = get_observation_info.MWA_Observation(obsid, db=db)
    except:
        logger.error('Cannot retrieve observation info for %d' % obsid)
        return None

    if verbose:
        print '\nFound observation:'
        print observation_info

    oi = qcdb.Observation_Info(db=qcdbcon)
    oi.obsid = obsid
    oi.lst_deg = observation_info.LST
    if isinstance(observation_info.HA, numpy.ndarray):
        oi.hourangle_deg = observation_info.HA[0] * 15
    else:
        oi.hourangle_deg = observation_info.HA * 15
    oi.obsname = observation_info.filename
    oi.gridname = observation_info._Schedule_Metadata.gridpoint_name
    oi.gridnum = observation_info._Schedule_Metadata.gridpoint_number
    oi.creator = observation_info._MWA_Setting.creator
    oi.mode = observation_info._MWA_Setting.mode
    oi.project = observation_info._MWA_Setting.projectid
    oi.calibrator = observation_info.calibration
    oi.center_coarse_channel = observation_info.center_channel
    oi.azimuth_deg = observation_info.azimuth
    oi.altitude_deg = observation_info.elevation
    oi.ra_pointing_deg = observation_info.RA
    oi.dec_pointing_deg = observation_info.Dec
    oi.coarse_channels = list(observation_info.channels)
    oi.beamformer_delays = list(observation_info.delays)
    oi.active_rx = list(observation_info.receivers)
    oi.exposure_time_sec = observation_info.duration
    oi.corr_int_time = observation_info.inttime
    oi.corr_int_freq = observation_info.fine_channel
    # DIGITAL_GAINS: are per tile
    corr2uvfitsheader = make_metafiles.Corr2UVFITSHeader(obsid, db=db)
    corr2uvfitsheader.make_header()
    nav_freq = int(corr2uvfitsheader.fine_channel / 10)
    oi.center_freq = make_metafiles.channel2frequency(
        corr2uvfitsheader.channel) + (nav_freq - 1) * 0.005,
    oi.bandwidth = corr2uvfitsheader.bandwidth
    oi.corr_ninputs = corr2uvfitsheader.n_inputs
    oi.corr_n_time_outs = corr2uvfitsheader.n_scans
    oi.n_fine_channels = corr2uvfitsheader.n_chans
    # need to calculate these
    oi.sun_dist, oi.sun_alt = qcdb.get_elevation_separation_azel(
        oi.azimuth_deg, oi.altitude_deg, obsid, 'Sun')
    oi.moon_dist, oi.moon_alt = qcdb.get_elevation_separation_azel(
        oi.azimuth_deg, oi.altitude_deg, obsid, 'Moon')
    oi.jupiter_dist, oi.jupiter_alt = qcdb.get_elevation_separation_azel(
        oi.azimuth_deg, oi.altitude_deg, obsid, 'Jupiter')

    # Get sky temp from Schedule_Metadata
    schedule_metadata = observation_info._Schedule_Metadata
    oi.tsys = schedule_metadata.sky_temp

    # Get schedule command from Mwa_Log
    mwa_setting = observation_info._MWA_Setting
    for i in xrange(len(mwa_setting.logs)):
        if mwa_setting.logs[i].referencetime == obsid:
            oi.schedule_command = mwa_setting.logs[i].comment

    # do some time/date conversions
    t = astropy.time.Time(obsid, format='gps', scale='utc')
    oi.mjd = t.mjd
    # 0 is Monday
    oi.day_of_week = t.datetime.weekday()
    oi.day_of_year = t.datetime.timetuple().tm_yday
    oi.date = list(t.datetime.timetuple()[:6])

    if _USE_IONEX:
        #i=ionex.ionexmaps(t)
        oi.site_tec = observation_info.TEC
        #_ionex_files.append(i.filename)

    # don't know how to do:
    # 'site_rms_tec'
    # 'rx_outside_temp'
    # 'humidity'
    # 'wind_speed'
    # 'wind_dir'
    # 'solar_problem'
    # 'holiday'
    # 'FIBRFACT'

    return oi
Пример #6
0
def main():

    observation_num=None

    usage="Usage: %prog [options]\n"
    usage+='\tMakes metadata files necessary for corr2uvfits\n'
    usage+='\t128T sampling windows:\n'
    for i in xrange(len(make_metafiles._START)):
        t1=ephem_utils.MWATime(gpstime=make_metafiles._START[i])
        if i<len(make_metafiles._START)-1:
            t2=ephem_utils.MWATime(gpstime=make_metafiles._START[i+1]-1)
        else:
            t2=ephem_utils.MWATime(gpstime=9046304456)
        usage+='\t%s (%10d) - %s (%10d): %.1f s, %d kHz\n' % (t1.datetime.strftime('%y-%m-%dT%H:%M:%S'),
                                                          t1.gpstime,
                                                          t2.datetime.strftime('%y-%m-%dT%H:%M:%S'),
                                                          t2.gpstime,
                                                          make_metafiles._DT[i],
                                                          make_metafiles._DF[i])
    parser = OptionParser(usage=usage)
    parser.add_option('-f','--filename',dest="filename",
                      help="Create metafiles for <FILE>",metavar="FILE")
    parser.add_option('-d','--datetime',dest="datetimestring",
                      help="Create metafiles for <DATETIME> (YYYYMMDDhhmmss)",
                      metavar="DATETIME")
    parser.add_option('-g','--gps',dest="gpstime",
                      help="Create metafiles for <GPS>",type='int',
                      metavar="GPS")
    parser.add_option('--contiguous',dest="contiguous",
                      action="store_true",default=False,
                      help="Output separate files for contiguous channels only")    
    parser.add_option('--minbaddipoles',dest='min_bad_dipoles',default=2,type='int',
                      help='Minimum number of bad dipoles in a single pol required to flag the entire tile [default=%default]')
    parser.add_option('-m','--maxdiff',dest="maxtimediff",type='int',
                      help="Maximum time difference for search (in sec)", default=10)
    parser.add_option('--channels',dest="channels",default=24,type=int,
                      help="Number of coarse channels [default=%default]")
    parser.add_option('--channel',dest="channel",default=None,type=int,
                      help="Coarse channel (implies channels=1) [default=%default]")
    parser.add_option('--dt',dest="dt",default=0,type=float,
                      help="[sec] Integration time [default=%default, select by gpstime]")
    parser.add_option('--df',dest="df",default=0,type=int,
                      help="[kHz] Fine channel width  [default=%default, select by gpstime]")
    parser.add_option('--timeoffset',dest="timeoffset",default=0,type=int,
                      help="[sec] Time offset between filename and start of data [default=%default]")
    parser.add_option('--header',dest='header',default='header_%gpstime%_%channel%.txt',
                      help="Name of header output file [default=%default]")
    parser.add_option('--antenna',dest='antenna',default='antenna_locations_%gpstime%_%channel%.txt',
                      help="Name of antenna_locations output file [default=%default]")
    parser.add_option('--instr',dest='instr',default='instr_config_%gpstime%_%channel%.txt',
                      help="Name of instr_config output file [default=%default]")
    parser.add_option('--rts',dest='rts',default=False,action="store_true",
                      help="Write RTS files (array file and rts_in)?")
    parser.add_option('--array',dest='array',default='array_file.txt',
                      help="Name of RTS array file  output file [default=%default]")
    parser.add_option('-v','--verbose',action="store_true",dest="verbose",default=False,
                      help="Increase verbosity of output")
    parser.add_option('--debug',action="store_true",dest="debug",default=False,
                      help="Display all database commands")
    parser.add_option('-l','--lock',dest='lock',action='store_true',default=False,
                          help='Use \"-l\" option to create header file for locked phase center (RTS input)')
    

    (options, args) = parser.parse_args()

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

    logger.info('Connecting to database %s@%s' % (mwaconfig.mandc.dbuser,mwaconfig.mandc.dbhost))

    if options.filename is not None:
        observation_num=get_observation_info.find_observation_num(options.filename,
                                                                  maxdiff=options.maxtimediff, db=db)
        if observation_num is None:
            logger.error('No matching observation found for filename=%s\n' % (options.filename))
            sys.exit(1)
    elif options.datetimestring is not None:
        observation_num=get_observation_info.find_observation_num(options.datetimestring,
                                                                  maxdiff=options.maxtimediff, db=db)
        if observation_num is None:
            logger.error('No matching observation found for datetimestring=%s\n' % (options.datetimestring))
            sys.exit(1)

    elif options.gpstime is not None:
        observation_num=get_observation_info.find_closest_observation((options.gpstime),
                                                                      maxdiff=options.maxtimediff,db=db)
        if observation_num is None:
            logger.error('No matching observation found for gpstime=%d\n' % (options.gpstime))
            sys.exit(1)

    else:
        logger.error('Must specify one of filename, datetime, or gpstime')
        sys.exit(1)


    if options.channel is not None:
        options.channels=1
    if observation_num is not None:
        obs=get_observation_info.MWA_Observation(observation_number=observation_num, db=db)        
        # do this first so that the instrument_configuration can know the duration
        h=make_metafiles.Corr2UVFITSHeader(observation_num, coarse_channels=options.channels,
                                           coarse_channel=options.channel,
                                           timeoffset=options.timeoffset,
                                           inttime=options.dt,
                                           fine_channel=options.df,
                                           lock=options.lock,
                                           db=db)
        vars={'gpstime': observation_num,
              'channel': make_metafiles.ternary(options.channel is None,'all',options.channel)}

        if '%' in options.header:
            options.header=make_metafiles.update_filename(options.header,vars)
        if '%' in options.instr:
            options.instr=make_metafiles.update_filename(options.instr,vars)
        if '%' in options.antenna:
            options.antenna=make_metafiles.update_filename(options.antenna,vars)
            
        T=make_metafiles.instrument_configuration(gpstime=observation_num, duration=h.obs.duration, db=db,
                                                  min_bad_dipoles=options.min_bad_dipoles,
                                                  debug=options.debug)
        result=T.make_instr_config()
        if result is None:
            logger.error('Error making instr_config file')
            sys.exit(1)
        try:
            f=open(options.instr,'w')
        except:
            logger.error('Could not open instr_config file %s for writing' % options.instr)
            sys.exit(1)
            
        f.write(str(T))
        f.close()
        logger.info('Wrote instr_config file %s!' % (options.instr))

        try:
            f=open(options.antenna,'w')
        except:
            logger.error('Could not open antenna_locations file %s for writing' % options.antenna)
            sys.exit(1)
        f.write(T.antenna_locations())
        f.close()
        logger.info('Wrote antenna_locations file %s!' % (options.antenna))

        if options.rts:
            try:
                f=open(options.array,'w')
            except:
                logger.error('Could not open RTS array_file  file %s for writing' % options.array)
                sys.exit(1)
            f.write(T.array_file())
            f.close()
            logger.info('Wrote array file %s!' % (options.array))

            try:
                f=open("rts.in",'w')
            except:
                logger.error('Could not open RTS configuration file rts.in for writing' )
                sys.exit(1)
            try:
                f.write(T.rts_in(rtstime=datetime.strptime(options.datetimestring,"%Y%m%d%H%M%S")))
                f.close()
                logger.info('Wrote rts_in file !')
            except:
                logger.error('Unable to write rts_in file')


        if numpy.diff(obs.channels).max()>1 and options.contiguous:
            # the frequencies are not contiguous
            # determine the contiguous ranges
            df=numpy.diff(obs.channels)
            frequency_indices=numpy.where(df>1)[0]
            channel_selections=[]
            istart=0
            for istop in frequency_indices:
                channel_selections.append(numpy.arange(istart,istop+1))
                istart=istop+1
            channel_selections.append(numpy.arange(istart,len(obs.channels)))            
        else:
            channel_selections=[numpy.arange(len(obs.channels))]


        for i,channel_selection in zip(range(len(channel_selections)),channel_selections):
            header_name=options.header
            logger.info('Creating output for channels %s' % channel_selection)
            h.channel_selection=channel_selection

            h.make_header()
            if len(channel_selections)>1:
                header_name=header_name.replace('.txt','.%02d.txt' % i)
            try:
                f=open(header_name,'w')
            except:
                logger.error('Could not open header file %s for writing' % header_name)
                sys.exit(1)
            f.write(str(h))
            f.close()
            logger.info('Wrote header file %s!' % (header_name))
Пример #7
0
        coord = ICRS.from_name(options.source)
    except:
        logger.error("Unable to find coordinates for '%s'" % options.source)
        sys.exit(1)

if coord is None:
    logger.error('Must supply one of (RA,Dec), (l,b), source')
    sys.exit(1)

observation_num = get_observation_info.find_closest_observation(observationid,
                                                                maxdiff=0,
                                                                db=db)
if observation_num is None:
    logger.error('Observation not found for gpstime=%d' % observationid)
    sys.exit(1)
obs = get_observation_info.MWA_Observation(observationid, db=db)
Az, El = ephem_utils.radec2azel(coord.ra.degree, coord.dec.degree,
                                obs.observation_number)
# first go from altitude to zenith angle
theta = numpy.radians((90 - El))
phi = numpy.radians(Az)
# this is the response for XX and YY
try:
    respX, respY = primary_beam.MWA_Tile_analytic(theta,
                                                  phi,
                                                  freq=obs.center_channel *
                                                  1.28e6,
                                                  delays=obs.delays)
except:
    logger.error('Error creating primary beams\n')
    sys.exit(1)
Пример #8
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
Пример #9
0
def main():
    observation_number=None

    usage="Usage: %prog [options]\n"
    usage+='\tMatches specified observation based on frequency and/or delays; can also restrict to calibration observations\n'
    usage+="\tInitial observation  based on a filename, a UT datetime string, or a GPS time\n"
    usage+="\tRequires connection to MandC database through local configuration file\n"
    parser = OptionParser(usage=usage)
    parser.add_option('-f','--filename',dest="filename",
                      help="Search for information on <FILE>",metavar="FILE")
    parser.add_option('-d','--datetime',dest="datetimestring",
                      help="Search for information on <DATETIME> (YYYYMMDDhhmmss)",
                      metavar="DATETIME")
    parser.add_option('-g','--gps',dest="gpstime",
                      help="Search for information on <GPS>",type='int',
                      metavar="GPS")
    parser.add_option('-m','--maxdiff',dest="maxtimediff",type='int',
                      help="Maximum time difference for search (in sec)", default=10)
    parser.add_option('-v','--verbose',action="store_true",dest="verbose",default=False,
                      help="Increase verbosity of output")
    parser.add_option('--delay',action='store_true',dest='delay',default=False,
                      help='Match delays?')
    parser.add_option('--freq',action='store_true',dest='freq',default=False,
                      help='Match frequencies?')
    parser.add_option('--cal',action='store_true',dest='cal',default=False,
                      help='Require calibration observations?')
    parser.add_option('-n','--nmax',dest='nmax',default=None,type='int',
                      help='Maximum number of records to display [default=%default]')
    parser.add_option('--dtmax',dest='dtmax',default=None,type='int',
                      help='Maximum time offset to search in seconds [default=%default]')
    parser.add_option('--min',action='store_true',
                    help='Only return the obsid of the best cal match (useful for scripting).')
    
    

    (options, args) = parser.parse_args()
    if len(args)<1:
        search_subset = None
    else:
        search_subset = args

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

    logger.info('Connecting to database %s@%s' % (mwaconfig.mandc.dbuser,mwaconfig.mandc.dbhost))

    if not (options.delay or options.freq or options.min):
        logger.error('Must match either delays and/or frequencies')
        sys.exit(1)

    if options.filename is not None:
        observation_number=get_observation_info.find_observation_num(options.filename, maxdiff=options.maxtimediff, db=db)
        if observation_number is None:
            logger.error('No matching observation found for filename=%s\n' % (options.filename))
            sys.exit(1)
    elif options.datetimestring is not None:
        observation_number=get_observation_info.find_observation_num(options.datetimestring, maxdiff=options.maxtimediff, db=db)
        if observation_number is None:
            logger.error('No matching observation found for datetimestring=%s\n' % (options.datetimestring))
            sys.exit(1)

    elif options.gpstime is not None:
        observation_number=get_observation_info.find_closest_observation((options.gpstime),maxdiff=options.maxtimediff,db=db)
        if observation_number is None:
            logger.error('No matching observation found for gpstime=%d\n' % (options.gpstime))
            sys.exit(1)

    else:
        logger.error('Must specify one of filename, datetime, or gpstime')
        sys.exit(1)
    if options.min:
        """
        Only print the obsid of the _best_ calibrator. Assumes you want matching frequencies.
        Why would you want anything else?
        """
        print observation_number,find_best_cal(observation_number,cal_dtmax=options.dtmax,
                    available_cals=search_subset,relax=True)
        sys.exit(0)
        
    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,search_subset=search_subset, matchdelays=options.delay, matchfreq=options.freq, dtmax=options.dtmax, db=db)
    if (matches is None or len(matches)==0):
        logger.warning('No matches identified for observation %d' % observation_number)
        sys.exit(0)
    s='# Starttime  Timediff Filename                  Cal? MJD   Date       Time     Channels                   '
    s+='                                                                     Delays'
    print s
    if observation.calibration:
        ss='T'
    else:
        ss='F'
    s='%10d %10d %-20s\t%s    %d %04d/%02d/%02d %02d:%02d:%02d' % (observation.observation_number,0,
                                                                   observation.filename,ss,
                                                                   observation.MJD,
                                                                   observation.year,observation.month,observation.day,
                                                                   observation.hour,observation.minute,observation.second)
    s+=' %s' % ((','.join([str(x) for x in observation.channels])))
    s+=' %s' % (','.join([str(x) for x in observation.delays]))
    print s
    nprinted=0
    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 options.cal and not observation.calibration:
            continue
        if observation.calibration:
            ss='T'
        else:
            ss='F'
        s='%10d %10d %-20s\t%s    %d %04d/%02d/%02d %02d:%02d:%02d' % (observation.observation_number,
                                                                       (observation.observation_number-observation_number),
                                                                       observation.filename,ss,
                                                                       observation.MJD,
                                                                       observation.year,observation.month,observation.day,
                                                                       observation.hour,observation.minute,observation.second)
        s+=' %s' % ((','.join([str(x) for x in observation.channels])))
        s+=' %s' % (','.join([str(x) for x in observation.delays]))
        print s
        nprinted+=1
        if options.nmax is not None and nprinted >= options.nmax:
            sys.exit(0)
        if options.dtmax is not None and math.fabs(match-observation_number)>options.dtmax:
            sys.exit(0)
Пример #10
0
def main():

    observation_num = None

    usage = "Usage: %prog [options]\n"
    usage += '\tMakes metadata FITS file\n'
    parser = OptionParser(usage=usage,
                          version=mwapy.__version__ + ' ' + mwapy.__date__)
    parser.add_option('-f',
                      '--filename',
                      dest="filename",
                      help="Create metafiles for <FILE>",
                      metavar="FILE")
    parser.add_option('-d',
                      '--datetime',
                      dest="datetimestring",
                      help="Create metafiles for <DATETIME> (YYYYMMDDhhmmss)",
                      metavar="DATETIME")
    parser.add_option('-g',
                      '--gps',
                      dest="gpstime",
                      help="Create metafiles for <GPS>",
                      type='int',
                      metavar="GPS")
    parser.add_option(
        '--contiguous',
        dest="contiguous",
        action="store_true",
        default=False,
        help="Output separate files for contiguous channels only")
    parser.add_option(
        '--quick',
        dest='quick',
        default=False,
        action='store_true',
        help='Quick output (ignores all Rx connections)? [default=%default]')
    parser.add_option(
        '--minbaddipoles',
        dest='min_bad_dipoles',
        default=2,
        type='int',
        help=
        'Minimum number of bad dipoles in a single pol required to flag the entire tile [default=%default]'
    )
    parser.add_option('-m',
                      '--maxdiff',
                      dest="maxtimediff",
                      type='int',
                      help="Maximum time difference for search (in sec)",
                      default=10)
    parser.add_option(
        '--channels',
        dest="channels",
        default=24,
        type=int,
        help=
        "Number of coarse channels [default=%default, select automatically]")
    parser.add_option(
        '--dt',
        dest="dt",
        default=0,
        type=float,
        help="[sec] Integration time [default=%default, select by gpstime]")
    parser.add_option(
        '--df',
        dest="df",
        default=0,
        type=int,
        help="[kHz] Fine channel width  [default=%default, select by gpstime]")
    parser.add_option(
        '--timeoffset',
        dest="timeoffset",
        default=0,
        type=int,
        help=
        "[sec] Time offset between filename and start of data [default=%default]"
    )
    parser.add_option(
        '-o',
        '--output',
        type=str,
        default=None,
        help='Name of output FITS file [default=<gpstime>.metafits]')
    parser.add_option('-v',
                      '--verbose',
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Increase verbosity of output")
    #parser.add_option('-l','--lock',dest='lock',action='store_true',default=False,
    #                      help='Use \"-l\" option to create header file for locked phase center (RTS input)')

    (options, args) = parser.parse_args()

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

    logger.info('Connecting to database %s@%s' %
                (mwaconfig.mandc.dbuser, mwaconfig.mandc.dbhost))

    if options.filename is not None:
        observation_num = get_observation_info.find_observation_num(
            options.filename, maxdiff=options.maxtimediff, db=db)
        if observation_num is None:
            logger.error('No matching observation found for filename=%s\n' %
                         (options.filename))
            sys.exit(1)
    elif options.datetimestring is not None:
        observation_num = get_observation_info.find_observation_num(
            options.datetimestring, maxdiff=options.maxtimediff, db=db)
        if observation_num is None:
            logger.error(
                'No matching observation found for datetimestring=%s\n' %
                (options.datetimestring))
            sys.exit(1)

    elif options.gpstime is not None:
        observation_num = get_observation_info.find_closest_observation(
            (options.gpstime), maxdiff=options.maxtimediff, db=db)
        if observation_num is None:
            logger.error('No matching observation found for gpstime=%d\n' %
                         (options.gpstime))
            sys.exit(1)

    else:
        logger.error('Must specify one of filename, datetime, or gpstime')
        sys.exit(1)

    if options.output is None:
        options.output = '%d.metafits' % observation_num

    if observation_num is not None:
        obs = get_observation_info.MWA_Observation(
            observation_number=observation_num, db=db)
        if numpy.diff(obs.channels).max() > 1 and options.contiguous:
            # the frequencies are not contiguous
            # determine the contiguous ranges
            df = numpy.diff(obs.channels)
            frequency_indices = numpy.where(df > 1)[0]
            channel_selections = []
            istart = 0
            for istop in frequency_indices:
                channel_selections.append(numpy.arange(istart, istop + 1))
                istart = istop + 1
            channel_selections.append(numpy.arange(istart, len(obs.channels)))
        else:
            channel_selections = [numpy.arange(len(obs.channels))]
        # do this first so that the instrument_configuration can know the duration
        hh = make_metafiles.Corr2UVFITSHeader(
            observation_num,
            coarse_channels=options.channels,
            timeoffset=options.timeoffset,
            inttime=options.dt,
            fine_channel=options.df,
            #lock=options.lock,
            db=db)

        T = make_metafiles.instrument_configuration(
            gpstime=observation_num,
            duration=hh.obs.duration,
            min_bad_dipoles=options.min_bad_dipoles,
            db=db)
        result = T.make_instr_config(quick=options.quick)
        if result is None:
            logger.error('Error making instr_config file')
            sys.exit(1)

        for i, channel_selection in zip(range(len(channel_selections)),
                                        channel_selections):
            logger.info('Creating output for channels %s' % channel_selection)
            T.channel_selection = channel_selection
            hh.channel_selection = channel_selection

            hh.make_header()
            T.corr2uvfitsheader = hh

            h = T.make_metafits(quick=options.quick)
            output = options.output
            if len(channel_selections) > 1:
                output = output.replace('.metafits', '.%02d.metafits' % i)
            if os.path.exists(output):
                os.remove(output)
            try:
                h.writeto(output)
                logger.info('Metafits written to %s' % (output))
            except Exception, e:
                logger.error('Unable to write metafits file %s:\n%s' %
                             (output, e))
                sys.exit(1)