Ejemplo n.º 1
0
def asdm2ms(asdmfile, msfile, scans, inttime='0s'):
    """ Converts asdm to ms format for a single scan.
    msfile defines the name template for the ms. Should end in .ms, but "s<scans>" will be put in.
    scans should be a comma-delimited list of scans to include (fed directly to importasdm).
    inttime is string to feed to split command. gives option of integrated data down in time.
    """
# can't get split to work with antenna names...?
#    dropants is a list of antenna names (['ea01', 'ea17']) that will not be kept in the ms.
    # anttree = et.parse(asdmfile + '/Antenna.xml')
    # antlist = [row.find('name').text for row in anttree.getiterator('row')]
    # for drop in dropants.split(','):
    #     if drop in antlist:
    #         print 'Dropping ant %s.' % drop
    #         antlist.pop(antlist.index(drop))
    #     else:
    #         print 'Ant %s not in data.' % drop

    # # make string of ants comma delimited
    # ants = ''
    # for ant in antlist:
    #     ants = ants+ant+','
    # ants = ants[:-1]

    # fill ms file
    msfile2 = msfile[:-3] + '_s' + scans + '.ms'
    if os.path.exists(msfile2):
        print '%s already set.' % msfile2
    else:
        # if filtering ants, use tmp file to split. **NOT TESTED YET**
#        if (antlist != [row.find('name').text for row in anttree.getiterator('row')]) or (inttime != '0s'):
        if inttime != '0s':
            print 'Filtering by int time.'
            importasdm(asdm=asdmfile, vis='tmp_'+msfile2, scans=scans, ocorr_mode='co', savecmds=False, applyflags=True, process_flags=False)
#            split(vis='tmp_'+msfile2, outputvis=msfile2, datacolumn='data', timebin=inttime, antenna=ants)
            split(vis='tmp_'+msfile2, outputvis=msfile2, datacolumn='data', timebin=inttime)
            shutil.rmtree('tmp_'+msfile2)
            shutil.move('tmp_'+msfile2+'.flagversions', msfile2+'.flagversions')
        else:
            importasdm(asdm=asdmfile, vis=msfile2, scans=scans, ocorr_mode='co', corr_mode='co', savecmds=False, applyflags=True, process_flags=False, lazy=True)

    # optionally clean up asdm after checking ms file?
    return msfile2
Ejemplo n.º 2
0
    def splitCalibrator(self, msName):
        "Split the MS calibrators"
                
        listCalibratorMS = []
        
        es = aU.stuffForScienceDataReduction()
        
        try :
            tb.open(msName)
            dataColNames = tb.colnames()
            tb.close()
            
        except:
            print("###")
            print("### Error to open MS : %s \n"%(msName))
            return(-1)

        ## Band
        
        msmd.open(msName)
        chan_freq = msmd.chanfreqs(1) 
            
        band =  aU.freqToBand(chan_freq[0])
                
        print("## band %s"%(band))
        
        



        if 'CORRECTED_DATA' in dataColNames:
            dataCol = 'corrected'
        else:
            dataCol = 'data'

        calNameList = []
        
        intentSources = es.getIntentsAndSourceNames(msName)
        
        for intentkey in self.listIntent:
                   
            calIds   = intentSources[intentkey]['sourceid']
            calName  = intentSources[intentkey]['name']
            # calIds   = sorted(dict.fromkeys(calIds).keys())
            # calName  = sorted(dict.fromkeys(calName).keys())
            
            # print calName
            # print calIds
            
            iterCalid = iter(calIds)

            for name  in calName:
                
                sourceid =  iterCalid.next()
                
                if name != '':
                    found = False
                else :
                    found = True                        ## avoid empty name
                for checkname in calNameList:
                    if checkname['name'] == name :
                        found = True
                
                if not found :
                    calNameList.append({'name' : name, 'intent': intentkey, 'sourceId': sourceid})
                    
        
        print calNameList
        
        for cal in calNameList:
                           
        ## split the calibrators
            nameMSCal = "%s-%s-%s.ms"%(msName, cal['intent'], cal['name'])
            
            print("## splitting %s \n"%(nameMSCal))
            if os.path.exists(nameMSCal):
                shutil.rmtree(nameMSCal)
            

            success = split(vis=msName, outputvis= nameMSCal,datacolumn= dataCol,field= cal['name'],spw="",width=1,antenna="",
                      timebin="0s",timerange="",scan="",intent="",array="",uvrange="",correlation="",observation="",combine="",
                      keepflags=True,keepmms=False)
            
            print success
            
            if success :
                   refdir = msmd.refdir(cal['sourceId'])
                   listCalibratorMS.append([nameMSCal,cal['name'], band, refdir])
                   print("## direction:")
                   print(refdir)
                   print("#")
                   
                
            else:
                print("### Splitting : Error with %s"%(nameMSCal))
                
        msmd.close()
                   
        return(listCalibratorMS)