コード例 #1
0
def cl2skycat(componentlist='', skycat=''):
    """
    Converts a componentlist dictionary or componnent list file on disk
    to  a skycatalog to overlay on image in the viewer
    
    """
    qa = casac.quanta()
    cl = casac.componentlist()
    tb = casac.table()
    if (type(componentlist) == str):
        cl.open(componentlist)
    elif (type(componentlist) == dict):
        cl.purge()
        cl.fromrecord(componentlist)
    if (cl.length() == 0):
        print "no components found"
        return

    des = {}
    des['Type'] = {'valueType': 'string'}
    des['Long'] = {'valueType': 'double'}
    des['Lat'] = {'valueType': 'double'}
    des['COMP_ID'] = {'valueType': 'string'}
    des['RA'] = {'valueType': 'string'}
    des['DEC'] = {'valueType': 'string'}
    des['FluxValue'] = {'valueType': 'double'}
    tb.create(tablename=skycat, tabledesc=des, nrow=cl.length())
    eltype = []
    nam = []
    RA = []
    DEC = []
    lati = np.zeros((cl.length(), ))
    longi = np.zeros((cl.length(), ))
    fluxval = np.zeros((cl.length(), ))
    for k in range(cl.length()):
        longi[k] = qa.convert(cl.getrefdir(k)['m0'], 'deg')['value']
        lati[k] = qa.convert(cl.getrefdir(k)['m1'], 'deg')['value']
        fluxval[k] = cl.getfluxvalue(k)[0]
        RA.append(qa.time(cl.getrefdir(k)['m0'], prec=10))
        DEC.append(qa.angle(cl.getrefdir(k)['m1'], prec=10))
        eltype.append(cl.getrefdir(k)['refer'])
        nam.append(str(k))
    tb.putcol('Type', eltype)
    tb.putcol('RA', RA)
    tb.putcol('DEC', DEC)
    tb.putcol('COMP_ID', nam)
    tb.putcol('Long', longi)
    tb.putcol('Lat', lati)
    tb.putcol('FluxValue', fluxval)
    tb.putcolkeyword(columnname='Long', keyword='UNIT', value='deg')
    tb.putcolkeyword(columnname='Lat', keyword='UNIT', value='deg')
    tb.putinfo({'type': 'Skycatalog'})
    tb.done()
コード例 #2
0
    def _muellerModelDataReader(self):
        """
        read flux density from the asteroid models by
        Mueller
        """
        from casac import casac 
        qa = casac.quanta()

        date=qa.time(str(self.mjd)+'d',form='ymd')[0]
        yr=date.split('/')[0] 
        #if int(yr) < 2015: yr = '2015'
        if self.src in self.knownsrcdict.keys():
            filename = self.datapath+self.src+"_ALMA_TPMprediction_"+yr+"_"+self.knownsrcdict[self.src]['interval']+".txt"
            #print "filename=",filename
        import glob
        flist = glob.glob(filename)
        if len(flist)==1:
            #print "Reading model data: ",flist[0]
            with open(flist[0],"r") as f:
                data = f.readlines()
            freqs = self._checkheader(data)
            if len(freqs)==0:
                raise IOError("Problem in reading model data file!")
            i = np.abs(freqs-self.freq).argmin()
            reffreq = freqs[i]
            
            maindata = []
            mjds = []
            fluxes = []
            for line in data[8:]:
                if line.find('----')<0:
                    maindata.append(line)
                    mjds.append(float(line.split()[5]))
                    fluxes.append(float(line.split()[i+5]))

            mjdarr = np.array(mjds)
            flxarr = np.array(fluxes)
            # find flux density of nearest in time
            t = np.abs(mjdarr-self.mjd).argmin()
            refflux = flxarr[t] 
            #print "flxarr=",flxarr
            #print "freq, mjd=",self.freq,self.mjd
            print "refflux@reffreq=%s@%s at %s" % (refflux,reffreq,mjdarr[t])

        elif len(flist)==0:
            raise IOError("Cannot find the model file for "+self.src+" for date="+date+".")

       
        return (reffreq, refflux)
コード例 #3
0
ファイル: setjy_helper.py プロジェクト: keflavich/casa
    def setSolarObjectJy(self,field,spw,scalebychan, timerange,observation, scan, intent, useephemdir, usescratch=False):
	"""
	Set flux density of a solar system object using Bryan Butler's new
	python model calculation code.
        A single time stamp (first time stamp of MS after selections are applied) is
        currently used per execution. For flux observation done in a long time span
        may need to run multiple setjy with selections by time range (or scans). 
	"""
	#retval = True 
        output = {}
	cleanupcomps = True # leave generated cl files 

        #from taskinit import * 
        from taskinit import gentools 
        qa = casac.quanta()
 

	(myms, mytb, mycl, myme, mycb, mymsmd) = gentools(['ms','tb','cl','me', 'cb','msmd'])
	# prepare parameters need to pass to the Bryan's code
	# make ms selections
	# get source name
	# get time ranges
	# get spwused and get frequency ranges

	sel={}
	sel['field']=field
	sel['spw']=spw
	sel['timerange']=timerange
	sel['observation']=str(observation)
	sel['scan']=scan
        sel['scanintent']=intent

	measframes=['REST','LSRK','LSRD','BARY','GEO','TOPO','GALACTO','LGROUP','CMB']
	myms.open(self.vis)
	myms.msselect(sel,False)
	scansummary=myms.getscansummary()
	nscan=len(scansummary.keys())
        selectedids=myms.msselectedindices()
	fieldids=selectedids['field']
        obsids=selectedids['observationid']
	myms.close()
	  
        mytb.open(self.vis+'/OBSERVATION')
        if len(obsids)==0:
          getrow=0
        else:
          getrow=obsids[0]
        observatory=mytb.getcell('TELESCOPE_NAME',getrow)
        mytb.close()
 
	mytb.open(self.vis+'/FIELD')
        colnames = mytb.colnames()
	if len(fieldids)==0:
	  fieldids = range(mytb.nrows())
        # frame reference for field position
        phdir_info=mytb.getcolkeyword("PHASE_DIR","MEASINFO")
        if phdir_info.has_key('Ref'):
          fieldref=phdir_info['Ref']
        elif phdir_info.has_key('TabRefTypes'):
          colnames=mytb.colnames()
          for col in colnames:
            if col=='PhaseDir_Ref':
                fieldrefind=mytb.getcell(col,fieldids[0])
                fieldref=phdir_info['TabRefTypes'][fieldrefind]
        else:
          fieldref=''
	srcnames={}
        fielddirs={}
        ftimes={}
        ephemid={}
	for fid in fieldids:
	  #srcnames.append(mytb.getcell('NAME',int(fid)))
	  srcnames[fid]=(mytb.getcell('NAME',int(fid)))
          #fielddirs[fid]=(mytb.getcell('PHASE_DIR',int(fid)))
          ftimes[fid]=(mytb.getcell('TIME',int(fid)))
          if colnames.count('EPHEMERIS_ID'):
              ephemid[fid]=(mytb.getcell('EPHEMERIS_ID',int(fid))) 
          else:
              ephemid[fid]=-1
	mytb.close() # close FIELD table

	# need to get a list of time
	# but for now for test just get a time centroid of the all scans
	# get time range
	# Also, this needs to be done per source

	# gather freq info from Spw table
	mytb.open(self.vis+'/SPECTRAL_WINDOW')
	nspw = mytb.nrows()
	freqcol=mytb.getvarcol('CHAN_FREQ')
	freqwcol=mytb.getvarcol('CHAN_WIDTH')
	fmeasrefcol=mytb.getcol('MEAS_FREQ_REF')
	reffreqs=mytb.getcol('REF_FREQUENCY')
	mytb.close()

	# store all parameters need to call solar_system_fd for all sources
	inparams={}
        validfids = [] # keep track of valid fid that has data (after selection) 
        # if same source name ....need handle this...
	for fid in fieldids:
	  sel['field']=str(fid)
	  myms.open(self.vis)
          #reset the selection
	  try:
            status=myms.msselect(sel)
          except:
            #skip this field
            continue 
          if not status:
            continue 

 
          validfids.append(fid)
	  trange=myms.range('time')
	  #if not inparams.has_key(srcnames[fid]):
          #  inparams[srcnames[fid]]={}
	  if not inparams.has_key(fid):
            inparams[fid]={}
            inparams[fid]['fieldname']=srcnames[fid]

	  #tc = (trange['time'][0]+trange['time'][1])/2. #in sec. 
          # use first timestamp to be consistent with the ALMA Control
          # old setjy (Butler-JPL-Horizons 2010) seems to be using
          # time in FIELD... but here is first selected time in main table
          if ephemid[fid]==-1: # keep old behavior
              tc = trange['time'][0] #in sec.
              tmsg = "Time used for the model calculation (=first time stamp of the selected data) for field "
          else: # must have ephem table attached...
              tc = ftimes[fid]#in sec.
              tmsg = "Time used for the model calculation for field "
       
	  #if inparams[srcnames[fid]].has_key('mjd'):
          #  inparams[srcnames[fid]]['mjds'][0].append([myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']])
          #else:
          #  inparams[srcnames[fid]]['mjds']=[myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']]
	  if inparams[fid].has_key('mjd'):
            inparams[fid]['mjds'][0].append([myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']])
          else:
            inparams[fid]['mjds']=[myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']]
          usedtime = qa.time(qa.quantity(tc,'s'),form='ymd')[0]
          self._casalog.post(tmsg+str(fid)+":"+usedtime)

          # get a correct direction measure
          mymsmd.open(self.vis)
          dirdic = mymsmd.phasecenter(int(fid))
          mymsmd.close()

          # check if frame is J2000 and if not do the transform
          if dirdic['refer'] != 'J2000':
              myme.doframe(myme.observatory(observatory))
              myme.doframe(myme.epoch('utc', qa.quantity(tc,'s')))
              azeldir = myme.measure(dirdic,'AZELGEO')
              dirdic=myme.measure(azeldir,'J2000')
       
          fielddirs[fid]=(numpy.array([[dirdic['m0']['value']],[dirdic['m1']['value']]]))

          # somehow it gives you duplicated ids .... so need to uniquify
	  selspws= list(set(myms.msselectedindices()['spw']))
          # make sure it is int rather than numpy.int32, etc.
          selspws = [int(ispw) for ispw in selspws]
	  #inparams[srcnames[fid]]['spwids']= selspws if len(selspws)!=0 else range(nspw) 
	  inparams[fid]['spwids']= selspws if len(selspws)!=0 else range(nspw) 

	  #create a list of freq ranges with selected spws
	  # should worry about freq order???
	  freqlist=[]
          freqlistraw=[]
	  framelist=[]
	  #for idx in inparams[srcnames[fid]]['spwids']:
	  for idx in inparams[fid]['spwids']:
	    freqs=freqcol['r'+str(idx+1)]
	    freqws=freqwcol['r'+str(idx+1)]
	    fmeasref=fmeasrefcol[idx]
	 
	    #if scalebychan=T, this has to be min, max determined from
	    # chan_freq(channel center)+/- chan width.
	    if scalebychan:
              # pack into list of list of list (freqlist[nf][nspw])
	      perspwfreqlist=[]
	      for nf in range(len(freqs)):
		fl = freqs[nf][0]-freqws[nf][0]/2.
		fh = freqs[nf][0]+freqws[nf][0]/2.
		perspwfreqlist.append([fl,fh])
	      freqlist.append(perspwfreqlist)
	    else:
	      if (len(freqs)==1): 
		fl = freqs[0][0]-freqws[0][0]/2.
		fh = freqs[0][0]+freqws[0][0]/2.
		freqlist.append([float(fl),float(fh)])
	      else:
		freqlist.append([float(min(freqs)),float(max(freqs))])   
	 
	    framelist.append(measframes[fmeasref])
            freqlistraw.append(freqs.transpose()[0].tolist()) # data chanfreq list
	  #inparams[srcnames[fid]]['freqlist']=freqlist
	  #inparams[srcnames[fid]]['freqlistraw']=freqlistraw
	  #inparams[srcnames[fid]]['framelist']=framelist
	  #inparams[srcnames[fid]]['reffreqs']=reffreqs

	  inparams[fid]['freqlist']=freqlist
	  inparams[fid]['freqlistraw']=freqlistraw
	  inparams[fid]['framelist']=framelist
	  inparams[fid]['reffreqs']=reffreqs
          myms.close()

             
	# call Bryan's code
	# errcode: list of list - inner list - each values for range of freqs
	# flluxes: list of list 
	# fluxerrs:
	# size: [majoraxis, minoraxis, pa]
	# direction: direction for each time stamp
        # 
	#import solar_system_setjy as ss_setjy
	import solar_system_setjy as SSsetjy 
	retdict={} # for returning flux densities?
        ss_setjy=SSsetjy.solar_system_setjy()
	#for src in srcnames:
        self.clnamelist=[]
	for vfid in validfids:
          src=srcnames[vfid]
	  #mjds=inparams[src]['mjds']
	  mjds=inparams[vfid]['mjds']
	  fluxes=[]
          # call solar_system_fd() per spw (for scalebychan freqlist has an extra dimention)
          #nspwused=len(inparams[src]['freqlist'])
          nspwused=len(inparams[vfid]['freqlist'])

          # warning for many channels but it is really depends on the source
          if scalebychan:
            maxnf=0
            for ispw in range(nspwused):
              #nf = len(inparams[src]['freqlist'][ispw])
              nf = len(inparams[vfid]['freqlist'][ispw])
              maxnf = max(nf,maxnf)
            if maxnf >= 3840 and src.upper()!="MARS": # mars shoulde be ok
              self._casalog.post("Processing %s spw(s) and at least some of them are a few 1000 channels or more. This may take \
                            many minutes (>3min per spw for 3840 channels) in some cases. Please be patient." % nspwused,"WARN")
              
	  for i in range(nspwused): # corresponds to n spw
	    if type(freqlist[0][0])==list:
	      #infreqs=inparams[src]['freqlist'][i]
	      infreqs=inparams[vfid]['freqlist'][i]
	    else:
	      #infreqs=[inparams[src]['freqlist'][i]]
	      infreqs=[inparams[vfid]['freqlist'][i]]
            self._casalog.post("Calling solar_system_fd: %s(%s) for spw%s freqs=%s" % (src, vfid,i,freqlist[i]),'DEBUG1')
	    (errcodes, subfluxes, fluxerrs, sizes, dirs)=\
               ss_setjy.solar_system_fd(source_name=src, MJDs=mjds, frequencies=infreqs, observatory=observatory, casalog=self._casalog)
            # for old code
	    #(errcodes, subfluxes, fluxerrs, sizes, dirs)=\
            #   ss_setjy.solar_system_fd(source_name=src, MJDs=mjds, frequencies=infreqs, casalog=self._casalog)
          # for nf freq ranges, nt mjds
          # errcodes[nf][nt], subfluxes[nf][nt], fluxerrs[nf][nt], sizes[nt],  dirs[nt]
            self._casalog.post("+++++ solar_system_fd() returned values +++++", 'DEBUG1')
            self._casalog.post(" fluxes(fds)=%s" % subfluxes, 'DEBUG1')
            self._casalog.post(" sizes=%s" % sizes, 'DEBUG1')
            self._casalog.post(" directions=%s\n" % dirs, 'DEBUG1')

          # packed fluxes for all spws 
	    fluxes.append(subfluxes)    
          # fluxes has fluxesi[nspw][nf][nt]

	  # ------------------------------------------------------------------------
	  # For testing with hardcoded values without calling solar_system_fd()...
	  #errcodes=[[0,0,0,0,0]]
	  #fluxes=[[26.40653147,65.23839313,65.23382757,65.80638802,69.33396562]]
	  #fluxerrs=[[0.0,0.0,0.0,0.0,0.0]]
	  #sizes=[[3.6228991032674371,3.6228991032674371,0.0]]
	  #dirs=[{'m0': {'unit': 'rad', 'value': 0.0}, 'm1': {'unit': 'rad', 'value': 0.0}, 'refer': 'J2000', 'type': 'direction'}]
	  # ------------------------------------------------------------------------
          # local params for selected src
	  #framelist=inparams[src]['framelist']
	  #freqlist=inparams[src]['freqlist']
	  #freqlistraw=inparams[src]['freqlistraw']
	  #reffreqs=inparams[src]['reffreqs']
	  #spwids=inparams[src]['spwids']

	  framelist=inparams[vfid]['framelist']
	  freqlist=inparams[vfid]['freqlist']
	  freqlistraw=inparams[vfid]['freqlistraw']
	  reffreqs=inparams[vfid]['reffreqs']
	  spwids=inparams[vfid]['spwids']

	  clrecs=odict.odict()
	  labels = []
	  # loop for over for multiple directions (=multiple  MJDs) for a given src
	  for i in range(len(dirs)):  # this is currently only length of 1 since no multiple timestamps were used
	    # check errcode - error code would be there per flux per time (dir)  
	    reterr=testerrs(errcodes[i],src) 
	    if reterr == 2: 
	      continue
	  
	    #dirstring = [dirs[i]['refer'],qa.tos(dirs[i]['m0']),qa.tos(dirs[i]['m1'])]
            if useephemdir:
	        dirstring = [dirs[i]['refer'],qa.tos(dirs[i]['m0']),qa.tos(dirs[i]['m1'])]
                dirmsg = "Using the source position from the ephemeris table in the casa data directory: "
            else:
                #dirstring = [fieldref, str(fielddirs[fieldids[0]][0][0])+'rad', str(fielddirs[fieldids[0]][1][0])+'rad']
                # extract field direction of first id of the selected field ids
                #dirstring = [fieldref, "%.18frad" % (fielddirs[fieldids[0]][0][0]), "%.18frad" % (fielddirs[fieldids[0]][1][0])]
                #dirstring = [fieldref, "%.18frad" % (fielddirs[vfid][0][0]), "%.18frad" % (fielddirs[vfid][1][0])]
                # by now the direction should be in J2000
                dirstring = ['J2000', "%.18frad" % (fielddirs[vfid][0][0]), "%.18frad" % (fielddirs[vfid][1][0])]
                dirmsg = "Using the source position in the MS (or in the attached ephemeris table, if any): "
            
            self._casalog.post(dirmsg+str(dirstring))

	    # setup componentlists
	    # need to set per dir
	    # if scalebychan=F, len(freqs) corresponds to nspw selected
            # Need to put in for-loop to create cl for each time stamp? or scan?

	    #clpath='/tmp/'
	    clpath='./'
            # construct cl name (multiple spws in one componentlist table)
            selreffreqs = [reffreqs[indx] for indx in spwids]
            minfreq = min(selreffreqs)
            maxfreq = max(selreffreqs)
	    freqlabel = '%.3f-%.3fGHz' % (minfreq/1.e9, maxfreq/1.e9)
	    #tmlabel = '%.1fd' % (tc/86400.)
	    mjd=inparams[vfid]['mjds'][0]
	    tmlabel = '%.1fd' % (mjd)
	    #clabel = src+'_spw'+str(spwids[j])+'_'+freqlabel+'_'+tmlabel
	    #clabel0 = src+'_'+freqlabel+'_'+tmlabel
            #pid=str(os.getpid())
	    #clname = clpath+clabel0+"_"+pid+'.cl'
	    clabel0 = src+'_'+freqlabel+'_'+tmlabel+"_"+os.path.basename(self.vis)
	    clname = clpath+clabel0+'.cl'
            #debug
            self._casalog.post("Create componentlist: %s for vfid=%s" % (clname,vfid),'DEBUG1')
	      
	    if (os.path.exists(clname)):
              shutil.rmtree(clname)

            iiflux=[]
            iinfreq=[]
            # for logging/output dict - pack each freq list for each spw
            freqsforlog=[]
	    for j in range(len(freqlist)): # loop over nspw
	      freqlabel = '%.3fGHz' % (reffreqs[int(spwids[j])]/1.e9)
              clabel=src+'_spw'+str(spwids[j])+'_'+freqlabel+'_'+tmlabel
	      #print "addcomponent...for spw=",spwids[j]," i=",i," flux=",fluxes[j][i]
	      if scalebychan:
		index= 2.0
		sptype = 'spectral index'
	      else:
		index= 0.0
		sptype = 'constant'
              # adjust to change in returned flux shape. An extra [] now seems to be gone. 2012-09-27
              iflux=fluxes[j][i][0]
              #print "addcomponent with flux=%s at frequency=%s" %\
              #                    (iflux,str(reffreqs[int(spwids[j])]/1.e9)+'GHz')

              # i - time stamps = 0 for now, j = a freq range
              infreq=freqlist[j][0][0] if type(freqlist[j][0])==list else freqlist[j][0]

              # for a single CL with multple spws
              if j ==0: infreq0=infreq 

              # --- version for 'multi-spws in a single comp single CL'
              # accumulate all frequencies from all spws to a list
              #if not scalebychan:
                #freqlist[j] for j spw should contain a list with two freqs (min and max of
                # of the spw and the same fluxes should be set for the freqs so that cl.setspectrum
                # with tabular would not interpolate inside each spw 
                ##iinfreq.extend(freqlist[j])
              #  nch = len(freqlistraw[j])
              #  iinfreq.extend(freqlistraw[j])
                # assigning the same flux to the two freqs
                ##iiflux.extend([iflux,iflux])
              #  for ich in range(nch):
              #    iiflux.extend([iflux])
              #else:
              #  iiflux.extend(fluxes[j][i])
	      #  if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
	      #	  for fr in freqlist[j]:
	      #	    iinfreq.append((fr[1]+fr[0])/2)
	      #  else:
              #    if type(freqlist[j])==list:
	      #	    iinfreq.append((freqlist[j][1]+freqlist[j][0])/2)
              #    else:
	      #      iinfreq.append(freqlist[j])
              #  freqsforlog.append(iinfreq)
              # -----------------------------------------------------------------------------------
              
              self._casalog.post("addcomponent with flux=%s at frequency=%s" %\
              #                    (fluxes[j][i][0],str(reffreqs[int(spwids[j])]/1.e9)+'GHz'), 'INFO1')
                                  (iflux,str(reffreqs[int(spwids[j])]/1.e9)+'GHz'), 'INFO1')
            
              #  
	      mycl.addcomponent(iflux,fluxunit='Jy', polarization="Stokes", dir=dirstring,
              		 shape='disk', majoraxis=str(sizes[i][0])+'arcsec', minoraxis=str(sizes[i][1])+'arcsec', 
                        positionangle=str(sizes[i][2])+'deg', freq=[framelist[0],str(infreq)+'Hz'], 
	      		 spectrumtype=sptype, index=index, label=clabel)

              if scalebychan:
                # use tabular form to set flux for each channel
                
                # This may be redundant 
                if type(fluxes[j][i]) ==list and len(fluxes[j][i])> 1:
                  if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
                    freqs=[]
                    for fr in freqlist[j]:
                      freqs.append((fr[1]+fr[0])/2)
                    clind = mycl.length() - 1
                    mycl.setspectrum(which=clind, type='tabular', tabularfreqs=freqs, tabularflux=fluxes[j][0],
	                             tabularframe=framelist[j])
            ### === per spw process end here

          # - version that put all spws into a component -
          # set a single component freq set at the the lower edge of first spw 
	  #mycl.addcomponent(iiflux[0],fluxunit='Jy', polarization="Stokes", dir=dirstring,
	  #		 shape='disk', majoraxis=str(sizes[i][0])+'arcsec', minoraxis=str(sizes[i][1])+'arcsec', 
	  #	         positionangle=str(sizes[i][2])+'deg', freq=[framelist[0],str(infreq0)+'Hz'], 
	  #		 spectrumtype=sptype, index=index, label=clabel)
          # if it's list of fluxes try to put in tabular form
	  #if type(fluxes[j][i]) ==list and len(fluxes[j][i])> 1:
	#  if len(iiflux)> 1:
	        #print "framelist[j]=",framelist[j]
	        #if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
		#  freqs=[]
		#  for fr in freqlist[j]:
		#    freqs.append((fr[1]+fr[0])/2)
		#else:
		#  freqs=freqlist[j]
                #clind = mycl.length() - 1
	#	mycl.setspectrum(which=clind, type='tabular', tabularfreqs=freqs, tabularflux=fluxes[j][0],
			   #tabularframe=framelist[j])

        #    mycl.setspectrum(which=clind, type='tabular', tabularfreqs=iinfreq, tabularflux=iiflux,
	#                     tabularframe=framelist[0])
	      #mycl.rename(clname)
	      #put in a record for log output
	      #clrecs[clabel] = mycl.torecord()
        
            clrecs[clabel0] = mycl.torecord()
            clrecs[clabel0]['allfluxinfo']=fluxes
            clrecs[clabel0]['allfreqinfo']=freqsforlog
            clrecs[clabel0]['spwids']=spwids
	    mycl.rename(clname) #  - A CL per field
            mycl.close(False) # False for not to send a warning message
	    mycl.done()

              # if scratch=F check if the virtual model already exist
              # for the field if it is clear it.
            #  if j==0 and (not usescratch): 
              #  mytb.open(self.vis, nomodify=False)
              #  kwds = mytb.getkeywords()
              #  modelkwd='definedmodel_field_'+str(vfid)
              #  if kwds.has_key(modelkwd):
              #    clmodname=kwds[modelkwd]
              #    mytb.removekeyword(clmodname)
              #    mytb.removekeyword(modelkwd)
              #  mytb.close()
             
            mycb.open(self.vis,addcorr=False,addmodel=False)
            mycb.delmod(otf=True,field=str(vfid),spw=spwids, scr=False)
            mycb.close()

	      # finally, put the componentlist as model
              #tqlstr=''
              #if intent!='':
              #   tqlstr='any(STATE_ID=='+str(stateids.tolist())+')'
            # Currently still need to do per spw (see the comment below...)
            # assuming CL contains nrow = nspw components
            mycl.open(clname)
            clinrec = mycl.torecord()
            mycl.close(False)
            mycl.done()
            ncomp = clinrec['nelements']
            if ncomp != len(spwids):
               raise Exception, "Inconsistency in generated componentlist...Please submit a bug report."
            for icomp in range(ncomp): 
	      #self.im.selectvis(spw=spwids[icomp],field=field,observation=observation,time=timerange,intent=intent)
	      ok = self.im.selectvis(spw=spwids[icomp],field=vfid,observation=observation,time=timerange,intent=intent)
              # for MMS, particular subms may not contain the particular spw, so skip for such a case
              if ok: 
                  newclinrec = {}
                  newclinrec['nelements']=1
                  newclinrec['component0']=clinrec['component'+str(icomp)]
                  mycl.fromrecord(newclinrec)
                  cdir = os.getcwd()
                  if os.access(cdir,os.W_OK):
                      tmpclpath = cdir+"/"
                  elif os.access("/tmp",os.W_OK):
                      tmpclpath = "/tmp/"
                  #tmpclpath=tmpclpath+"_tmp_setjyCLfile_"+pid
                  tmpclpath=tmpclpath+os.path.basename(self.vis)+"_tmp_setjyCLfile"
                  if os.path.exists(tmpclpath):
                      shutil.rmtree(tmpclpath)
                  mycl.rename(tmpclpath)
                  mycl.close(False)
	          self.im.ft(complist=tmpclpath)
             

            # --------------------------------------------------------------------------------------------
            # Currently this does not work properly for some case. Need ft can handle multi-component CL
            # with the way to map which component apply to which spw
            # Unable when such functionality is implemented in im.ft()
	    #self.im.selectvis(spw=spwids,field=field,observation=observation,time=timerange,intent=intent)
	    #self.im.ft(complist=clname) <= e.g. im.ft(comprec) where comrec = {'spw-mapping':[...],comprecs}..
            # --------------------------------------------------------------------------------------------

              #debug: set locally saved 2010-version component list instead
              #cl2010='mod_setjy_spw0_Titan_230.543GHz55674.1d.cl'
              #print "Using complist=",cl2010
	      #self.im.ft(complist=cl2010)

	      #if cleanupcomps:          
              # 		  shutil.rmtree(clname)
              #self.clnamelist.append(clname)
            self.clnamelist.append(clname)

	  msg="Using channel dependent " if scalebychan else "Using spw dependent "
       
          if clrecs=={}:
            raise Exception, "No componentlist is generated."
	  self._casalog.post(msg+" flux densities")
	  #self._reportoLog(clrecs,self._casalog)
	  self._reportoLog2(clrecs,self._casalog)
	  #self._updateHistory(clrecs,self.vis)
	  self._updateHistory2(clrecs,self.vis)
          # dictionary of dictionary for each field
          retdict[vfid]=clrecs 
        # ==== end of for loop over fields		 
        #output=self._makeRetFluxDict(retdict)
        output=self._makeRetFluxDict2(retdict)
	#return retval
	return output
コード例 #4
0
from casac import casac
import os
import numpy as np
import pdb
iaim = casac.image()
iamask = casac.image()
qa = casac.quanta()
rg = casac.regionmanager()


def automask(image='',
             maskimage='',
             fracofpeak=0,
             rmsthresh=3.0,
             resolution=None,
             twopass=False):
    """
    image : dirty image or residual to mask
    maskimage: name of mask to create. If already existant has to be same shape 
    and coordsys as image
    fracofpeak : Fraction of peak to use as masking threshold (a number between 0 and 1)
                       if 0  then rmsthresh or  a value based on the rms of the image is used
    rmsthresh : Threshold in sigma  to use; default is 3 sigma
    resolution: if image has no restoring beam ..this value is used for resolution of 
                     image (e.g '4arcsec')
    twopass: set to True if faint fluffy stuff is wanted else false if a less agressive masking 
    is needed especially a multi pass masking after some cleaning

    """
    #    print '2pass ', twopass
    iaim.open(image)
コード例 #5
0
ファイル: automask.py プロジェクト: schiebel/casa
from casac import casac
import os
import numpy as np
import pdb
iaim=casac.image()
iamask=casac.image()
qa=casac.quanta()
rg=casac.regionmanager()

def automask(image='', maskimage='', fracofpeak=0, rmsthresh=3.0, resolution=None, twopass=False):
    """
    image : dirty image or residual to mask
    maskimage: name of mask to create. If already existant has to be same shape 
    and coordsys as image
    fracofpeak : Fraction of peak to use as masking threshold (a number between 0 and 1)
                       if 0  then rmsthresh or  a value based on the rms of the image is used
    rmsthresh : Threshold in sigma  to use; default is 3 sigma
    resolution: if image has no restoring beam ..this value is used for resolution of 
                     image (e.g '4arcsec')
    twopass: set to True if faint fluffy stuff is wanted else false if a less agressive masking 
    is needed especially a multi pass masking after some cleaning

    """
#    print '2pass ', twopass
    iaim.open(image)
    stat=iaim.statistics(mask='abs("'+image+'") > 0.0')
    csys=iaim.coordsys()
    rb=iaim.restoringbeam()
    numpix=10
    shp=iaim.shape()
    resol=qa.quantity(resolution, 'arcsec')
コード例 #6
0
ファイル: ic2233.py プロジェクト: ajm-asiaa/New-casa
from casac import casac
from locatescript import copydata

quantity = casac.quanta()
im = casac.imager()
ia = casac.image()


def data():
    return ['ic2233_1.ms']


def run(fetch=False):

    #####fetch data
    if fetch:
        for f in data():
            copydata(f, os.getcwd())

    im.open('ic2233_1.ms')
    npix = 1024
    im.selectvis(spw='0', nchan=[6], start=[0], step=[1])
    im.defineimage(nx=npix,
                   ny=npix,
                   cellx='3.0arcsec',
                   celly='3.0arcsec',
                   stokes="IV",
                   spw=[0])
    im.weight(type='briggs', robust=0.7)
    im.setoptions(imagetilevol=-1000000)
    im.setvp(dovp=bool(1),
コード例 #7
0
ファイル: setjy_helper.py プロジェクト: schiebel/casa
    def setSolarObjectJy(self,
                         field,
                         spw,
                         scalebychan,
                         timerange,
                         observation,
                         scan,
                         intent,
                         useephemdir,
                         usescratch=False):
        """
	Set flux density of a solar system object using Bryan Butler's new
	python model calculation code.
        A single time stamp (first time stamp of MS after selections are applied) is
        currently used per execution. For flux observation done in a long time span
        may need to run multiple setjy with selections by time range (or scans). 
	"""
        #retval = True
        output = {}
        cleanupcomps = True  # leave generated cl files

        #from taskinit import *
        from taskinit import gentools
        qa = casac.quanta()

        (myms, mytb, mycl, myme, mycb,
         mymsmd) = gentools(['ms', 'tb', 'cl', 'me', 'cb', 'msmd'])
        # prepare parameters need to pass to the Bryan's code
        # make ms selections
        # get source name
        # get time ranges
        # get spwused and get frequency ranges

        sel = {}
        sel['field'] = field
        sel['spw'] = spw
        sel['timerange'] = timerange
        sel['observation'] = str(observation)
        sel['scan'] = scan
        sel['scanintent'] = intent

        measframes = [
            'REST', 'LSRK', 'LSRD', 'BARY', 'GEO', 'TOPO', 'GALACTO', 'LGROUP',
            'CMB'
        ]
        myms.open(self.vis)
        myms.msselect(sel, False)
        scansummary = myms.getscansummary()
        nscan = len(scansummary.keys())
        selectedids = myms.msselectedindices()
        fieldids = selectedids['field']
        obsids = selectedids['observationid']
        myms.close()

        mytb.open(self.vis + '/OBSERVATION')
        if len(obsids) == 0:
            getrow = 0
        else:
            getrow = obsids[0]
        observatory = mytb.getcell('TELESCOPE_NAME', getrow)
        mytb.close()

        mytb.open(self.vis + '/FIELD')
        colnames = mytb.colnames()
        if len(fieldids) == 0:
            fieldids = range(mytb.nrows())
    # frame reference for field position
        phdir_info = mytb.getcolkeyword("PHASE_DIR", "MEASINFO")
        if phdir_info.has_key('Ref'):
            fieldref = phdir_info['Ref']
        elif phdir_info.has_key('TabRefTypes'):
            colnames = mytb.colnames()
            for col in colnames:
                if col == 'PhaseDir_Ref':
                    fieldrefind = mytb.getcell(col, fieldids[0])
                    fieldref = phdir_info['TabRefTypes'][fieldrefind]
        else:
            fieldref = ''
        srcnames = {}
        fielddirs = {}
        ftimes = {}
        ephemid = {}
        for fid in fieldids:
            #srcnames.append(mytb.getcell('NAME',int(fid)))
            srcnames[fid] = (mytb.getcell('NAME', int(fid)))
            #fielddirs[fid]=(mytb.getcell('PHASE_DIR',int(fid)))
            ftimes[fid] = (mytb.getcell('TIME', int(fid)))
            if colnames.count('EHPHEMERIS_ID'):
                ephemid[fid] = (mytb.getcell('EPHEMERIS_ID', int(fid)))
            else:
                ephemid[fid] = -1
        mytb.close()  # close FIELD table

        # need to get a list of time
        # but for now for test just get a time centroid of the all scans
        # get time range
        # Also, this needs to be done per source

        # gather freq info from Spw table
        mytb.open(self.vis + '/SPECTRAL_WINDOW')
        nspw = mytb.nrows()
        freqcol = mytb.getvarcol('CHAN_FREQ')
        freqwcol = mytb.getvarcol('CHAN_WIDTH')
        fmeasrefcol = mytb.getcol('MEAS_FREQ_REF')
        reffreqs = mytb.getcol('REF_FREQUENCY')
        mytb.close()

        # store all parameters need to call solar_system_fd for all sources
        inparams = {}
        validfids = [
        ]  # keep track of valid fid that has data (after selection)
        # if same source name ....need handle this...
        for fid in fieldids:
            sel['field'] = str(fid)
            myms.open(self.vis)
            #reset the selection
            try:
                status = myms.msselect(sel)
            except:
                #skip this field
                continue
            if not status:
                continue

            validfids.append(fid)
            trange = myms.range('time')
            #if not inparams.has_key(srcnames[fid]):
            #  inparams[srcnames[fid]]={}
            if not inparams.has_key(fid):
                inparams[fid] = {}
                inparams[fid]['fieldname'] = srcnames[fid]

            #tc = (trange['time'][0]+trange['time'][1])/2. #in sec.
            # use first timestamp to be consistent with the ALMA Control
            # old setjy (Butler-JPL-Horizons 2010) seems to be using
            # time in FIELD... but here is first selected time in main table
            if ephemid[fid] == -1:  # keep old behavior
                tc = trange['time'][0]  #in sec.
                tmsg = "Time used for the model calculation (=first time stamp of the selected data) for field "
            else:  # must have ephem table attached...
                tc = ftimes[fid]  #in sec.
                tmsg = "Time used for the model calculation for field "

            #if inparams[srcnames[fid]].has_key('mjd'):
            #  inparams[srcnames[fid]]['mjds'][0].append([myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']])
            #else:
            #  inparams[srcnames[fid]]['mjds']=[myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']]
            if inparams[fid].has_key('mjd'):
                inparams[fid]['mjds'][0].append(
                    [myme.epoch('utc', qa.quantity(tc, 's'))['m0']['value']])
            else:
                inparams[fid]['mjds'] = [
                    myme.epoch('utc', qa.quantity(tc, 's'))['m0']['value']
                ]
            usedtime = qa.time(qa.quantity(tc, 's'), form='ymd')[0]
            self._casalog.post(tmsg + str(fid) + ":" + usedtime)

            # get a correct direction measure
            mymsmd.open(self.vis)
            dirdic = mymsmd.phasecenter(int(fid))
            mymsmd.close()

            # check if frame is J2000 and if not do the transform
            if dirdic['refer'] != 'J2000':
                myme.doframe(myme.observatory(observatory))
                myme.doframe(myme.epoch('utc', qa.quantity(tc, 's')))
                azeldir = myme.measure(dirdic, 'AZELGEO')
                dirdic = myme.measure(azeldir, 'J2000')

            fielddirs[fid] = (numpy.array([[dirdic['m0']['value']],
                                           [dirdic['m1']['value']]]))

            # somehow it gives you duplicated ids .... so need to uniquify
            selspws = list(set(myms.msselectedindices()['spw']))
            # make sure it is int rather than numpy.int32, etc.
            selspws = [int(ispw) for ispw in selspws]
            #inparams[srcnames[fid]]['spwids']= selspws if len(selspws)!=0 else range(nspw)
            inparams[fid]['spwids'] = selspws if len(selspws) != 0 else range(
                nspw)

            #create a list of freq ranges with selected spws
            # should worry about freq order???
            freqlist = []
            freqlistraw = []
            framelist = []
            #for idx in inparams[srcnames[fid]]['spwids']:
            for idx in inparams[fid]['spwids']:
                freqs = freqcol['r' + str(idx + 1)]
                freqws = freqwcol['r' + str(idx + 1)]
                fmeasref = fmeasrefcol[idx]

                #if scalebychan=T, this has to be min, max determined from
                # chan_freq(channel center)+/- chan width.
                if scalebychan:
                    # pack into list of list of list (freqlist[nf][nspw])
                    perspwfreqlist = []
                    for nf in range(len(freqs)):
                        fl = freqs[nf][0] - freqws[nf][0] / 2.
                        fh = freqs[nf][0] + freqws[nf][0] / 2.
                        perspwfreqlist.append([fl, fh])
                    freqlist.append(perspwfreqlist)
                else:
                    if (len(freqs) == 1):
                        fl = freqs[0][0] - freqws[0][0] / 2.
                        fh = freqs[0][0] + freqws[0][0] / 2.
                        freqlist.append([float(fl), float(fh)])
                    else:
                        freqlist.append([float(min(freqs)), float(max(freqs))])

                framelist.append(measframes[fmeasref])
                freqlistraw.append(
                    freqs.transpose()[0].tolist())  # data chanfreq list
            #inparams[srcnames[fid]]['freqlist']=freqlist
            #inparams[srcnames[fid]]['freqlistraw']=freqlistraw
            #inparams[srcnames[fid]]['framelist']=framelist
            #inparams[srcnames[fid]]['reffreqs']=reffreqs

            inparams[fid]['freqlist'] = freqlist
            inparams[fid]['freqlistraw'] = freqlistraw
            inparams[fid]['framelist'] = framelist
            inparams[fid]['reffreqs'] = reffreqs
            myms.close()

        # call Bryan's code
        # errcode: list of list - inner list - each values for range of freqs
        # flluxes: list of list
        # fluxerrs:
        # size: [majoraxis, minoraxis, pa]
        # direction: direction for each time stamp
        #
        #import solar_system_setjy as ss_setjy
        import solar_system_setjy as SSsetjy
        retdict = {}  # for returning flux densities?
        ss_setjy = SSsetjy.solar_system_setjy()
        #for src in srcnames:
        self.clnamelist = []
        for vfid in validfids:
            src = srcnames[vfid]
            #mjds=inparams[src]['mjds']
            mjds = inparams[vfid]['mjds']
            fluxes = []
            # call solar_system_fd() per spw (for scalebychan freqlist has an extra dimention)
            #nspwused=len(inparams[src]['freqlist'])
            nspwused = len(inparams[vfid]['freqlist'])

            # warning for many channels but it is really depends on the source
            if scalebychan:
                maxnf = 0
                for ispw in range(nspwused):
                    #nf = len(inparams[src]['freqlist'][ispw])
                    nf = len(inparams[vfid]['freqlist'][ispw])
                    maxnf = max(nf, maxnf)
                if maxnf >= 3840 and src.upper(
                ) != "MARS":  # mars shoulde be ok
                    self._casalog.post(
                        "Processing %s spw(s) and at least some of them are a few 1000 channels or more. This may take \
                            many minutes (>3min per spw for 3840 channels) in some cases. Please be patient."
                        % nspwused, "WARN")

            for i in range(nspwused):  # corresponds to n spw
                if type(freqlist[0][0]) == list:
                    #infreqs=inparams[src]['freqlist'][i]
                    infreqs = inparams[vfid]['freqlist'][i]
                else:
                    #infreqs=[inparams[src]['freqlist'][i]]
                    infreqs = [inparams[vfid]['freqlist'][i]]
                self._casalog.post(
                    "Calling solar_system_fd: %s(%s) for spw%s freqs=%s" %
                    (src, vfid, i, freqlist[i]), 'DEBUG1')
                (errcodes, subfluxes, fluxerrs, sizes, dirs)=\
                          ss_setjy.solar_system_fd(source_name=src, MJDs=mjds, frequencies=infreqs, observatory=observatory, casalog=self._casalog)
                # for old code
                #(errcodes, subfluxes, fluxerrs, sizes, dirs)=\
                #   ss_setjy.solar_system_fd(source_name=src, MJDs=mjds, frequencies=infreqs, casalog=self._casalog)
                # for nf freq ranges, nt mjds
                # errcodes[nf][nt], subfluxes[nf][nt], fluxerrs[nf][nt], sizes[nt],  dirs[nt]
                self._casalog.post(
                    "+++++ solar_system_fd() returned values +++++", 'DEBUG1')
                self._casalog.post(" fluxes(fds)=%s" % subfluxes, 'DEBUG1')
                self._casalog.post(" sizes=%s" % sizes, 'DEBUG1')
                self._casalog.post(" directions=%s\n" % dirs, 'DEBUG1')

                # packed fluxes for all spws
                fluxes.append(subfluxes)
            # fluxes has fluxesi[nspw][nf][nt]

        # ------------------------------------------------------------------------
        # For testing with hardcoded values without calling solar_system_fd()...
        #errcodes=[[0,0,0,0,0]]
        #fluxes=[[26.40653147,65.23839313,65.23382757,65.80638802,69.33396562]]
        #fluxerrs=[[0.0,0.0,0.0,0.0,0.0]]
        #sizes=[[3.6228991032674371,3.6228991032674371,0.0]]
        #dirs=[{'m0': {'unit': 'rad', 'value': 0.0}, 'm1': {'unit': 'rad', 'value': 0.0}, 'refer': 'J2000', 'type': 'direction'}]
        # ------------------------------------------------------------------------
        # local params for selected src
        #framelist=inparams[src]['framelist']
        #freqlist=inparams[src]['freqlist']
        #freqlistraw=inparams[src]['freqlistraw']
        #reffreqs=inparams[src]['reffreqs']
        #spwids=inparams[src]['spwids']

            framelist = inparams[vfid]['framelist']
            freqlist = inparams[vfid]['freqlist']
            freqlistraw = inparams[vfid]['freqlistraw']
            reffreqs = inparams[vfid]['reffreqs']
            spwids = inparams[vfid]['spwids']

            clrecs = odict.odict()
            labels = []
            # loop for over for multiple directions (=multiple  MJDs) for a given src
            for i in range(
                    len(dirs)
            ):  # this is currently only length of 1 since no multiple timestamps were used
                # check errcode - error code would be there per flux per time (dir)
                reterr = testerrs(errcodes[i], src)
                if reterr == 2:
                    continue

                #dirstring = [dirs[i]['refer'],qa.tos(dirs[i]['m0']),qa.tos(dirs[i]['m1'])]
                if useephemdir:
                    dirstring = [
                        dirs[i]['refer'],
                        qa.tos(dirs[i]['m0']),
                        qa.tos(dirs[i]['m1'])
                    ]
                else:
                    #dirstring = [fieldref, str(fielddirs[fieldids[0]][0][0])+'rad', str(fielddirs[fieldids[0]][1][0])+'rad']
                    # extract field direction of first id of the selected field ids
                    #dirstring = [fieldref, "%.18frad" % (fielddirs[fieldids[0]][0][0]), "%.18frad" % (fielddirs[fieldids[0]][1][0])]
                    dirstring = [
                        fieldref,
                        "%.18frad" % (fielddirs[vfid][0][0]),
                        "%.18frad" % (fielddirs[vfid][1][0])
                    ]
            #print "dirstring=",dirstring

                # setup componentlists
                # need to set per dir
                # if scalebychan=F, len(freqs) corresponds to nspw selected
                # Need to put in for-loop to create cl for each time stamp? or scan?

                #clpath='/tmp/'
                clpath = './'
                # construct cl name (multiple spws in one componentlist table)
                selreffreqs = [reffreqs[indx] for indx in spwids]
                minfreq = min(selreffreqs)
                maxfreq = max(selreffreqs)
                freqlabel = '%.3f-%.3fGHz' % (minfreq / 1.e9, maxfreq / 1.e9)
                #tmlabel = '%.1fd' % (tc/86400.)
                mjd = inparams[vfid]['mjds'][0]
                tmlabel = '%.1fd' % (mjd)
                #clabel = src+'_spw'+str(spwids[j])+'_'+freqlabel+'_'+tmlabel
                #clabel0 = src+'_'+freqlabel+'_'+tmlabel
                #pid=str(os.getpid())
                #clname = clpath+clabel0+"_"+pid+'.cl'
                clabel0 = src + '_' + freqlabel + '_' + tmlabel + "_" + os.path.basename(
                    self.vis)
                clname = clpath + clabel0 + '.cl'
                #debug
                self._casalog.post(
                    "Create componentlist: %s for vfid=%s" % (clname, vfid),
                    'DEBUG1')

                if (os.path.exists(clname)):
                    shutil.rmtree(clname)

                iiflux = []
                iinfreq = []
                # for logging/output dict - pack each freq list for each spw
                freqsforlog = []
                for j in range(len(freqlist)):  # loop over nspw
                    freqlabel = '%.3fGHz' % (reffreqs[int(spwids[j])] / 1.e9)
                    clabel = src + '_spw' + str(
                        spwids[j]) + '_' + freqlabel + '_' + tmlabel
                    #print "addcomponent...for spw=",spwids[j]," i=",i," flux=",fluxes[j][i]
                    if scalebychan:
                        index = 2.0
                        sptype = 'spectral index'
                    else:
                        index = 0.0
                        sptype = 'constant'
            # adjust to change in returned flux shape. An extra [] now seems to be gone. 2012-09-27
                    iflux = fluxes[j][i][0]
                    #print "addcomponent with flux=%s at frequency=%s" %\
                    #                    (iflux,str(reffreqs[int(spwids[j])]/1.e9)+'GHz')

                    # i - time stamps = 0 for now, j = a freq range
                    infreq = freqlist[j][0][0] if type(
                        freqlist[j][0]) == list else freqlist[j][0]

                    # for a single CL with multple spws
                    if j == 0: infreq0 = infreq

                    # --- version for 'multi-spws in a single comp single CL'
                    # accumulate all frequencies from all spws to a list
                    #if not scalebychan:
                    #freqlist[j] for j spw should contain a list with two freqs (min and max of
                    # of the spw and the same fluxes should be set for the freqs so that cl.setspectrum
                    # with tabular would not interpolate inside each spw
                    ##iinfreq.extend(freqlist[j])
                    #  nch = len(freqlistraw[j])
                    #  iinfreq.extend(freqlistraw[j])
                    # assigning the same flux to the two freqs
                    ##iiflux.extend([iflux,iflux])
                    #  for ich in range(nch):
                    #    iiflux.extend([iflux])
                    #else:
                    #  iiflux.extend(fluxes[j][i])
                    #  if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
                    #	  for fr in freqlist[j]:
                    #	    iinfreq.append((fr[1]+fr[0])/2)
                    #  else:
                    #    if type(freqlist[j])==list:
                    #	    iinfreq.append((freqlist[j][1]+freqlist[j][0])/2)
                    #    else:
                    #      iinfreq.append(freqlist[j])
                    #  freqsforlog.append(iinfreq)
                    # -----------------------------------------------------------------------------------

                    self._casalog.post("addcomponent with flux=%s at frequency=%s" %\
              #                    (fluxes[j][i][0],str(reffreqs[int(spwids[j])]/1.e9)+'GHz'), 'INFO1')

                                        (iflux,str(reffreqs[int(spwids[j])]/1.e9)+'GHz'), 'INFO1')

                    #
                    mycl.addcomponent(iflux,
                                      fluxunit='Jy',
                                      polarization="Stokes",
                                      dir=dirstring,
                                      shape='disk',
                                      majoraxis=str(sizes[i][0]) + 'arcsec',
                                      minoraxis=str(sizes[i][1]) + 'arcsec',
                                      positionangle=str(sizes[i][2]) + 'deg',
                                      freq=[framelist[0],
                                            str(infreq) + 'Hz'],
                                      spectrumtype=sptype,
                                      index=index,
                                      label=clabel)

                    if scalebychan:
                        # use tabular form to set flux for each channel

                        # This may be redundant
                        if type(fluxes[j][i]) == list and len(
                                fluxes[j][i]) > 1:
                            if type(freqlist[j][0]) == list and len(
                                    freqlist[j][0]) > 1:
                                freqs = []
                                for fr in freqlist[j]:
                                    freqs.append((fr[1] + fr[0]) / 2)
                                clind = mycl.length() - 1
                                mycl.setspectrum(which=clind,
                                                 type='tabular',
                                                 tabularfreqs=freqs,
                                                 tabularflux=fluxes[j][0],
                                                 tabularframe=framelist[j])
            ### === per spw process end here

            # - version that put all spws into a component -
            # set a single component freq set at the the lower edge of first spw
        #mycl.addcomponent(iiflux[0],fluxunit='Jy', polarization="Stokes", dir=dirstring,
        #		 shape='disk', majoraxis=str(sizes[i][0])+'arcsec', minoraxis=str(sizes[i][1])+'arcsec',
        #	         positionangle=str(sizes[i][2])+'deg', freq=[framelist[0],str(infreq0)+'Hz'],
        #		 spectrumtype=sptype, index=index, label=clabel)
        # if it's list of fluxes try to put in tabular form
        #if type(fluxes[j][i]) ==list and len(fluxes[j][i])> 1:
        #  if len(iiflux)> 1:
        #print "framelist[j]=",framelist[j]
        #if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
        #  freqs=[]
        #  for fr in freqlist[j]:
        #    freqs.append((fr[1]+fr[0])/2)
        #else:
        #  freqs=freqlist[j]
        #clind = mycl.length() - 1
        #	mycl.setspectrum(which=clind, type='tabular', tabularfreqs=freqs, tabularflux=fluxes[j][0],
        #tabularframe=framelist[j])

        #    mycl.setspectrum(which=clind, type='tabular', tabularfreqs=iinfreq, tabularflux=iiflux,
        #                     tabularframe=framelist[0])
        #mycl.rename(clname)
        #put in a record for log output
        #clrecs[clabel] = mycl.torecord()

                clrecs[clabel0] = mycl.torecord()
                clrecs[clabel0]['allfluxinfo'] = fluxes
                clrecs[clabel0]['allfreqinfo'] = freqsforlog
                clrecs[clabel0]['spwids'] = spwids
                mycl.rename(clname)  #  - A CL per field
                mycl.close(False)  # False for not to send a warning message
                mycl.done()

                # if scratch=F check if the virtual model already exist
                # for the field if it is clear it.
                #  if j==0 and (not usescratch):
                #  mytb.open(self.vis, nomodify=False)
                #  kwds = mytb.getkeywords()
                #  modelkwd='definedmodel_field_'+str(vfid)
                #  if kwds.has_key(modelkwd):
                #    clmodname=kwds[modelkwd]
                #    mytb.removekeyword(clmodname)
                #    mytb.removekeyword(modelkwd)
                #  mytb.close()

                mycb.open(self.vis, addcorr=False, addmodel=False)
                mycb.delmod(otf=True, field=str(vfid), spw=spwids, scr=False)
                mycb.close()

                # finally, put the componentlist as model
                #tqlstr=''
                #if intent!='':
                #   tqlstr='any(STATE_ID=='+str(stateids.tolist())+')'
                # Currently still need to do per spw (see the comment below...)
                # assuming CL contains nrow = nspw components
                mycl.open(clname)
                clinrec = mycl.torecord()
                mycl.close(False)
                mycl.done()
                ncomp = clinrec['nelements']
                if ncomp != len(spwids):
                    raise Exception, "Inconsistency in generated componentlist...Please submit a bug report."
                for icomp in range(ncomp):
                    #self.im.selectvis(spw=spwids[icomp],field=field,observation=observation,time=timerange,intent=intent)
                    ok = self.im.selectvis(spw=spwids[icomp],
                                           field=vfid,
                                           observation=observation,
                                           time=timerange,
                                           intent=intent)
                    # for MMS, particular subms may not contain the particular spw, so skip for such a case
                    if ok:
                        newclinrec = {}
                        newclinrec['nelements'] = 1
                        newclinrec['component0'] = clinrec['component' +
                                                           str(icomp)]
                        mycl.fromrecord(newclinrec)
                        cdir = os.getcwd()
                        if os.access(cdir, os.W_OK):
                            tmpclpath = cdir + "/"
                        elif os.access("/tmp", os.W_OK):
                            tmpclpath = "/tmp/"
                        #tmpclpath=tmpclpath+"_tmp_setjyCLfile_"+pid
                        tmpclpath = tmpclpath + os.path.basename(
                            self.vis) + "_tmp_setjyCLfile"
                        self._casalog.post("tmpclpath=" + tmpclpath)
                        if os.path.exists(tmpclpath):
                            shutil.rmtree(tmpclpath)
                        mycl.rename(tmpclpath)
                        mycl.close(False)
                        self._casalog.post("Now tmpclpath=" + tmpclpath)
                        self.im.ft(complist=tmpclpath)

            # --------------------------------------------------------------------------------------------
            # Currently this does not work properly for some case. Need ft can handle multi-component CL
            # with the way to map which component apply to which spw
            # Unable when such functionality is implemented in im.ft()
                #self.im.selectvis(spw=spwids,field=field,observation=observation,time=timerange,intent=intent)
                #self.im.ft(complist=clname) <= e.g. im.ft(comprec) where comrec = {'spw-mapping':[...],comprecs}..
                # --------------------------------------------------------------------------------------------

                #debug: set locally saved 2010-version component list instead
                #cl2010='mod_setjy_spw0_Titan_230.543GHz55674.1d.cl'
                #print "Using complist=",cl2010
                #self.im.ft(complist=cl2010)

                #if cleanupcomps:
                # 		  shutil.rmtree(clname)
                #self.clnamelist.append(clname)
                self.clnamelist.append(clname)

            msg = "Using channel dependent " if scalebychan else "Using spw dependent "

            if clrecs == {}:
                raise Exception, "No componentlist is generated."
            self._casalog.post(msg + " flux densities")
            #self._reportoLog(clrecs,self._casalog)
            self._reportoLog2(clrecs, self._casalog)
            #self._updateHistory(clrecs,self.vis)
            self._updateHistory2(clrecs, self.vis)
            # dictionary of dictionary for each field
            retdict[vfid] = clrecs

    # ==== end of for loop over fields
    #output=self._makeRetFluxDict(retdict)
        output = self._makeRetFluxDict2(retdict)
        #return retval
        return output
コード例 #8
0
ファイル: ic2233.py プロジェクト: ahalester/casa-testing
from casac import casac
from locatescript import copydata

quantity=casac.quanta()
im = casac.imager()
ia = casac.image()

def data():
    return ['ic2233_1.ms']

def run(fetch=False) :

    #####fetch data
    if fetch:
        for f in data( ):
            copydata( f, os.getcwd( ) )
    
    im.open('ic2233_1.ms')
    npix=1024
    im.selectvis(spw='0', nchan=[6], start=[0], step=[1])
    im.defineimage(nx=npix, ny=npix, cellx='3.0arcsec', celly='3.0arcsec', stokes="IV", spw=[0])
    im.weight(type='briggs', robust=0.7)
    im.setoptions(imagetilevol=-1000000);
    im.setvp(dovp=bool(1), usedefaultvp=bool(1), dosquint=bool(1),
             parangleinc='5.0deg')
    im.make('squint_corr')
    im.clean(algorithm='mfhogbom', niter=1000, model=['squint_corr'], residual=['squint_corr.residual'], image=['squint_corr.restored'], threshold='0Jy')
    im.done()
    return ['squint_corr.restored']
コード例 #9
0
    def __init__(self,
                 imageName,
                 write=True,
                 resultDir='WEBPAGES/imageTest/',
                 imDir='IMAGES/'):

        import shutil
        self.imTool = casac.image()
        self.imTool.open(imageName)  #open('tables/squint_corr.restored')

        # fix up images that don't have CASA-canonical stokes and spec:
        # assume for now that direction is in 01 at least,
        mycs = self.imTool.coordsys()
        findstok = mycs.findcoordinate("stokes")
        if not findstok['return']:
            myImagename = imageName + ".k"
            self.imTool.adddegaxes(stokes=True,
                                   outfile=myImagename,
                                   overwrite=True)
            mystokpix = self.imTool.summary()['ndim']  # ct from 0
            self.imTool.close()
            shutil.rmtree(imageName)
            shutil.move(myImagename, imageName)
            self.imTool.open(imageName)
            mycs.done()
            mycs = self.imTool.coordsys()
        else:
            mystokpix = findstok['pixel']

        findspec = mycs.findcoordinate("spectral")
        if not findspec['return']:
            myImagename = imageName + ".s"
            self.imTool.adddegaxes(spectral=True,
                                   outfile=myImagename,
                                   overwrite=True)
            myspecpix = self.imTool.summary()['ndim']  # ct from 0
            self.imTool.close()
            shutil.rmtree(imageName)
            shutil.move(myImagename, imageName)
            self.imTool.open(imageName)
            mycs.done()
            mycs = self.imTool.coordsys()
        else:
            myspecpix = findspec['pixel']

        curr_order = [mystokpix, myspecpix]
        if curr_order != [2, 3]:
            myImagename = imageName + ".t"
            self.imTool.transpose(order="01%1i%1i" % (mystokpix, myspecpix),
                                  outfile=myImagename,
                                  overwrite=True)
            shutil.rmtree(imageName)
            shutil.move(myImagename, imageName)
            self.imTool.open(imageName)

        self.rgTool = casac.regionmanager()
        self.qaTool = casac.quanta()
        self.getArr()  #instead make explicit call to getArr()
        self.write = write
        self.imageName = imageName
        self.iterate = 0  #for multiple cubeFit() tests

        if self.write:
            self.resultDir = resultDir + strftime('/%Y_%m_%d/')
            if os.access(self.resultDir, os.F_OK) is False:
                print self.resultDir + ' directory DNE, so am making one!'
                os.mkdir(self.resultDir)
            else:
                print self.resultDir + ' directory exists; will add to it!'
            self.imDir = imDir
            if os.access(imDir, os.F_OK) is False:
                print imDir + ' directory DNE, so am making one!'
                os.mkdir(imDir)
            else:
                print imDir + ' directory exists; will add to it!'

            t = localtime(time())
            self.fname = 'Regression-%s-%s-%s-%s-%s-%s.html' % (
                t[0], t[1], t[2], t[3], t[4], t[5])
            self.html = self.resultDir + self.fname
            self.body1 = []
            self.body2 = []
            self.htmlPub = htmlPub(self.html, 'Image tests')
        else:
            print 'stats-only mode; will not write to html file!'
コード例 #10
0
ファイル: imageTest.py プロジェクト: keflavich/casa
    def __init__(self, imageName, write=True,
                 resultDir='WEBPAGES/imageTest/',
                 imDir='IMAGES/'):

        import shutil
        self.imTool=casac.image()
        self.imTool.open(imageName) #open('tables/squint_corr.restored')

        # fix up images that don't have CASA-canonical stokes and spec:
        # assume for now that direction is in 01 at least, 
        mycs=self.imTool.coordsys()
        findstok=mycs.findcoordinate("stokes")
        if not findstok['return']:
            myImagename=imageName+".k"
            self.imTool.adddegaxes(stokes=True,outfile=myImagename,overwrite=True)
            mystokpix=self.imTool.summary()['ndim'] # ct from 0
            self.imTool.close()
            shutil.rmtree(imageName)
            shutil.move(myImagename,imageName)
            self.imTool.open(imageName)
            mycs.done()
            mycs=self.imTool.coordsys()
        else:
            mystokpix=findstok['pixel']

        findspec=mycs.findcoordinate("spectral")    
        if not findspec['return']:
            myImagename=imageName+".s"
            self.imTool.adddegaxes(spectral=True,outfile=myImagename,overwrite=True)
            myspecpix=self.imTool.summary()['ndim'] # ct from 0
            self.imTool.close()
            shutil.rmtree(imageName)
            shutil.move(myImagename,imageName)
            self.imTool.open(imageName)
            mycs.done()
            mycs=self.imTool.coordsys()
        else:
            myspecpix=findspec['pixel']                    

        curr_order=[mystokpix,myspecpix]
        if curr_order != [2,3]:
            myImagename=imageName+".t"
            self.imTool.transpose(order="01%1i%1i" % (mystokpix,myspecpix),outfile=myImagename,overwrite=True)
            shutil.rmtree(imageName)
            shutil.move(myImagename,imageName)
            self.imTool.open(imageName)                    
            
        self.rgTool=casac.regionmanager()
        self.qaTool=casac.quanta()
	self.getArr() #instead make explicit call to getArr()
	self.write=write
	self.imageName=imageName
        self.iterate=0 #for multiple cubeFit() tests

	if self.write:
         self.resultDir=resultDir+strftime('/%Y_%m_%d/')
         if os.access(self.resultDir,os.F_OK) is False:
          print self.resultDir+' directory DNE, so am making one!'
          os.mkdir(self.resultDir)
         else: 
          print self.resultDir+' directory exists; will add to it!'
	 self.imDir=imDir
	 if os.access(imDir,os.F_OK) is False:
	  print imDir+' directory DNE, so am making one!'
	  os.mkdir(imDir)
	 else: 
	  print imDir+' directory exists; will add to it!'

         t=localtime( time() )
         self.fname='Regression-%s-%s-%s-%s-%s-%s.html'%(t[0],t[1],t[2],t[3],t[4],t[5])
	 self.html=self.resultDir+self.fname
         self.body1=[]
         self.body2=[]
         self.htmlPub=htmlPub(self.html,'Image tests')
        else:
	 print 'stats-only mode; will not write to html file!'
コード例 #11
0
    def _muellerModelDataReader(self):
        """
        read flux density from the asteroid models by
        Mueller
        """
        from casac import casac
        qa = casac.quanta()

        reffreq = None
        refflux = None

        date = qa.time(str(self.mjd) + 'd', form='ymd')[0]
        yr = date.split('/')[0]

        # ------------------------------
        # Following section should only to read the data initially and keep the data and only scan the new model data file
        # when the mjd is outside of the current data...
        #
        #if int(yr) < 2015: yr = '2015'
        if self.src in self.knownsrcdict.keys():
            filename = self.datapath + self.src + "_ALMA_TPMprediction_" + yr + "_" + self.knownsrcdict[
                self.src]['interval'] + ".txt"
            if (self.openedFile != filename):
                # read a new file
                self.openedFile = filename
                import glob
                flist = glob.glob(filename)
                if len(flist) == 1:
                    self._casalog.post("Reading model data: " + flist[0])
                    with open(flist[0], "r") as f:
                        self.data = f.readlines()
                elif len(flist) == 0:
                    raise IOError("Cannot find the model file for " +
                                  self.src + " for date=" + date + ".")
        # else:
        #     self._casalog.post("use the file already read..self.openedFile="+self.openedFile)

            freqs = self._checkheader(self.data)
            if len(freqs) == 0:
                raise IOError("Problem in reading model data file!")

            i = np.abs(freqs - self.freq).argmin()
            reffreq = freqs[i]
            #self._casalog.post("self.freq= %s i=%s reffreq=%s" % (self.freq, i, reffreq))
            #print "self.freq= %s i=%s reffreq=%s" % (self.freq, i, reffreq)

            maindata = []
            mjds = []
            fluxes = []
            for line in self.data[8:]:
                if line.find('----') < 0:
                    maindata.append(line)
                    mjds.append(float(line.split()[5]))
                    # i th flux (0 based)
                    fluxes.append(float(line.split()[(i + 1) + 5]))

            mjdarr = np.array(mjds)
            flxarr = np.array(fluxes)
            # ---------------------

            # find flux density of nearest in time
            t = np.abs(mjdarr - self.mjd).argmin()
            refflux = flxarr[t]
            #print "flxarr=",flxarr
            #print "freq, mjd=",self.freq,self.mjd
            #print "refflux@reffreq=%s@%s at %s" % (refflux,reffreq,mjdarr[t])
            #self._casalog.post("refflux@reffreq=%s@%s at %s" % (refflux,reffreq,mjdarr[t]),"DEBUG1")

        return (reffreq, refflux)
コード例 #12
0
def field2skycat(msname='', skycat='', fieldpattern=''):
    """
    Converts field table in ms to skycatalog
    allow overlay on image in the viewer
    """
    enumTypes = [
        'J2000', 'JMEAN', 'JTRUE', 'APP', 'B1950', 'B1950_VLA', 'BMEAN',
        'BTRUE', 'GALACTIC', 'HADEC', 'AZEL', 'AZELSW', 'AZELGEO', 'AZELSWGEO',
        'JNAT', 'ECLIPTIC', 'MECLIPTIC', 'TECLIPTIC', 'SUPERGAL', 'ITRF',
        'TOPO', 'ICRS'
    ]

    qa = casac.quanta()
    tb = casac.table()
    tb.open(msname + '/FIELD')
    dir = tb.getcol('PHASE_DIR')
    nam = tb.getcol('NAME')
    nfield = tb.nrows()
    n = 0
    for k in range(nfield):
        if (re.match(fieldpattern, nam[k]) != None):
            n = n + 1

    eltype = []
    if (tb.getcolkeyword('PHASE_DIR', 'MEASINFO').has_key('VarRefCol')):
        typeid = tb.getcol(
            tb.getcolkeyword('PHASE_DIR', 'MEASINFO')['VarRefCol'])
        for k in range(nfield):
            if (re.match(fieldpattern, nam[k]) != None):
                eltype.append(enumTypes[typeid[k]])
    else:
        eltype = [tb.getcolkeyword('PHASE_DIR', 'MEASINFO')['Ref']] * nfield
    unitra = tb.getcolkeyword('PHASE_DIR', 'QuantumUnits')[0]
    unitdec = tb.getcolkeyword('PHASE_DIR', 'QuantumUnits')[1]
    tb.done()
    des = {}
    des['Type'] = {'valueType': 'string'}
    des['Long'] = {'valueType': 'double'}
    des['Lat'] = {'valueType': 'double'}
    des['FIELD_NAME'] = {'valueType': 'string'}
    des['FIELD_ID'] = {'valueType': 'string'}
    des['RA'] = {'valueType': 'string'}
    des['DEC'] = {'valueType': 'string'}

    tb.create(tablename=skycat, tabledesc=des, nrow=n)
    tb.putcol('Type', eltype)
    lati = np.zeros((n, ))
    longi = np.zeros((n, ))
    RA = []
    DEC = []
    fid = []
    fieldname = []
    n = 0
    for k in range(nfield):
        if (re.match(fieldpattern, nam[k]) != None):
            longi[n] = qa.convert(qa.quantity(dir[0, 0, k], unitra),
                                  'deg')['value']
            lati[n] = qa.convert(qa.quantity(dir[1, 0, k], unitdec),
                                 'deg')['value']
            RA.append(qa.time(qa.quantity(dir[0, 0, k], unitra), prec=10))
            DEC.append(qa.angle(qa.quantity(dir[1, 0, 0], unitdec), prec=10))
            fid.append(str(k))
            fieldname.append(nam[k])
            n = n + 1
    tb.putcol('RA', RA)
    tb.putcol('DEC', DEC)
    #    tb.putcol('FIELD_NAME', nam)
    tb.putcol('FIELD_NAME', fieldname)
    tb.putcol('FIELD_ID', fid)
    tb.putcol('Long', longi)
    tb.putcol('Lat', lati)
    tb.putcolkeyword(columnname='Long', keyword='UNIT', value='deg')
    tb.putcolkeyword(columnname='Lat', keyword='UNIT', value='deg')
    tb.putinfo({'type': 'Skycatalog'})
    tb.done()