Example #1
0
 def execute(self):
     self.manager.set_data(self.scan)
     if self.dosky:
         self.set_caloption()
         self.manager.set_calmode(self.skymode)
         self.manager.calibrate()
     if self.dotsys:
         self.__detect_tsysspw()
         self.manager.set_calmode('tsys')
         #self.manager.set_tsys_spw(self.tsysspw)
         if len(self.tsysspw) > 0:
             casalog.post('Testing new tsysspw specification: %s'%(self.tsysspw))
             tsysspw_dict = {}
             for (k,v) in self.scan.parse_spw_selection(self.tsysspw).items():
                 tsysspw_dict[str(k)] = numpy.array(v).flatten().tolist()
             casalog.post('tsysspw_dict=%s'%(tsysspw_dict))
             self.manager.set_tsys_spw_withrange(tsysspw_dict, self.tsysavg)
         self.manager.calibrate()
     if self.doapply:
         if isinstance(self.applytable,str):
             if len(self.applytable) > 0:
                 self.manager.add_applytable(self.applytable)
         else:
             for tab in self.applytable:
                 self.manager.add_applytable(tab)
         if len(self.interp_time) > 0:
             self.manager.set_time_interpolation(self.interp_time)
         if len(self.interp_freq) > 0:
             self.manager.set_freq_interpolation(self.interp_freq)
         for (k,v) in self.spwmap.items():
             self.manager.set_tsys_transfer(int(k),list(v))
         self.manager.apply(self.insitu, True)
Example #2
0
    def execute(self):
        # set various attributes to self.scan
        self.set_to_scan()

        #WORKAROUND for new tasks (in future this should be done in sdutil)
        if not self.timeaverage: self.scanaverage = False
        # Average data if necessary
        self.scan = sdutil.doaverage(self.scan, self.scanaverage, self.timeaverage, self.tweight, self.polaverage, self.pweight)

        # Reload plotter if necessary
        sd.plotter._assert_plotter(action="reload")

        # Set subplot layout
        if self.subplot > 10:
            row = int(self.subplot/10)
            col = (self.subplot % 10)
            sd.plotter.set_layout(rows=row,cols=col,refresh=False)
        else:
            if self.subplot > -1:
                casalog.post(("Invalid subplot value, %d, is ignored. It should be in between 11 and 99." % self.subplot),priority="WARN")
            sd.plotter.set_layout(refresh=False)

        # Set subplot margins
        if self.margin != sd.plotter._margins:
            sd.plotter.set_margin(margin=self.margin,refresh=False)

        # Actual plotting
        getattr(self,'plot_%s'%(self.plottype))()
Example #3
0
 def __get_mapsize(self):
     if self.subplot < 11:
         casalog.post("Setting default subplot layout (1x1).",priority="WARN")
         self.subplot = 11
     nx = (self.subplot % 10)
     ny = int(self.subplot/10)
     return (nx,ny)
Example #4
0
 def __sampling_summary(self):
     """Print summary of sampling setup"""
     if not self.is_sampling_set:
         self.__notset_message()
         return
     casalog.post("sampling interval: %s arcsec" % str(self.sampling_arcsec))
     casalog.post("position angle: %s" % (self.pa))
Example #5
0
    def parameter_check(self):
        # use FLOAT_DATA if exists
        self.open_table(self.infile)
        colnames = self.table.colnames()
        if any(col=='FLOAT_DATA' for col in colnames):
            self.datacol = 'FLOAT_DATA'
            casalog.post( "FLOAT_DATA exist" )
        else:
            self.datacol = 'DATA'
            casalog.post( "No FLOAT_DATA, DATA is used" )
        self.close_table()

        # masklist must be specified for baseline subtraction
        self.dobaseline = self.calmode.lower() == 'baseline'
        if self.dobaseline and (self.masklist is None \
                                or len(self.masklist) == 0):
            msg='Please specify a region to be fitted in masklist'
            raise Exception, msg

        # outfile must be specified for imaging
        if self.createimage and self.outfile == '':
            msg='Please specify out image name'
            raise Exception, msg

        # flag operation
        self.doflag = len(self.flaglist) > 0

        # plot
        self.doplot = abs(self.plotlevel) > 0
Example #6
0
    def execute(self):
        # CAS-5410 Use private tools inside task scripts
        qa = qatool()

        scan = self.worker.scan
        if self.kernel == 'regrid':
            if not qa.isquantity(self.chanwidth):
                errstr = "Invalid quantity chanwidth "+self.chanwidth
                raise Exception, errstr
            qchw = qa.quantity(self.chanwidth)
            oldUnit = scan.get_unit()
            if qchw['unit'] in ("", "channel", "pixel"):
                scan.set_unit("channel")
            elif qa.compare(self.chanwidth,"1Hz") or \
                     qa.compare(self.chanwidth,"1m/s"):
                scan.set_unit(qchw['unit'])
            else:
                errstr = "Invalid dimension of quantity chanwidth "+self.chanwidth
                raise Exception, errstr
            casalog.post( "Regridding spectra in width "+self.chanwidth )
            scan.regrid_channel(width=qchw['value'],plot=self.verify,insitu=True)
            scan.set_unit(oldUnit)
        else:
            casalog.post( "Smoothing spectra with kernel "+self.kernel )
            scan.smooth(kernel=self.kernel,width=self.kwidth,plot=self.verify,insitu=True)
Example #7
0
 def check_job(self, jobId, verbose=True):
     """Check the status of a non-blocking job"""
     
     jobId_list = list(jobId)
     command_response_list = self.__command_client.get_command_response(jobId_list,block=False,verbose=verbose)
     
     # Aggregate exceptions and completed jobIds
     error_msg = ''
     completed_jobs = []
     for command_response in command_response_list:
         if not command_response['successful']:
             if len(error_msg) > 0: error_msg += "\n"
             
             error_msg += "Exception executing command in server %s: %s" % (command_response['server'],
                                                                           command_response['traceback'])
         else:
             completed_jobs.append(command_response['id'])
             
     # Re-throw aggregated exception
     if len(error_msg) > 0:
         casalog.post(error_msg,"SEVERE","MPIInterfaceCore::check_job")    
         raise Exception(error_msg) 
             
     # Check that all jobs have been completed
     completed = True
     for jobId in jobId_list:
         if jobId not in completed_jobs:
             completed = False
             break
     
     # Return completion status
     return completed
Example #8
0
    def __set_mask(self):
        self.msk = None
        if self.interactive:
            # Interactive masking
            self.msk = sdutil.interactive_mask(self.scan,
                                               self.masklist,
                                               False,
                                               purpose='to calculate statistics')
            msks = self.scan.get_masklist(self.msk)
            if len(msks) < 1:
                raise Exception, 'No channel is selected. Exit without calculation.'
            lbl=self.scan.get_unit()
            casalog.post( 'final mask list ('+lbl+') = '+str(msks) )

            del msks

        # set the mask region
        elif ( len(self.masklist) > 0 and self.masklist!=[[]]):
            self.msk=self.scan.create_mask(self.masklist,invert=False)
            msks=self.scan.get_masklist(self.msk)
            if len(msks) < 1:
                del self.msk, msks
                raise Exception, 'Selected mask lists are out of range. Exit without calculation.'
            del msks
        else:
            # Full region
            casalog.post( 'Using full region' )
Example #9
0
    def __calc_stats(self):
        usr = get_user()
        tmpfile = '/tmp/tmp_'+usr+'_casapy_asap_scantable_stats'
        self.result = {}

        # calculate regular statistics
        statsname = ['max', 'min', 'max_abc', 'min_abc',
                     'sum', 'mean', 'median', 'rms',
                     'stddev']
        for name in statsname:
            v = self.scan.stats(name,self.msk,self.format_string, None, True)
            self.result[name] = list(v) # if len(v) > 1 else v[0]
            if sd.rcParams['verbose']:
                self.savestats += get_text_from_file(tmpfile)

        # calculate additional statistics (eqw and integrated intensity)
        self.__calc_eqw_and_integf()
        
        if sd.rcParams['verbose']:
            # Print equivalent width
            out = self.__get_statstext('eqw', self.abclbl, 'eqw')
            self.savestats += out

            # Print integrated flux
            outp = self.__get_statstext('Integrated intensity', self.intlbl, 'totint')
            self.savestats += outp

            # to logger
            casalog.post(out[:-2]+outp)
Example #10
0
 def check_update(self):
     if len(self.outfile) == 0:
         if not self.overwrite:
             raise Exception('You should set overwrite to True if you want to update infile.')
         else:
             casalog.post('%s will be overwritten by the calibrated spectra.'%(self.infile), 'INFO')
             self.insitu = True
Example #11
0
    def __load_contour( self, vwr, panel, contour ):
        ## here we can assume we have a dictionary
        ## that specifies what needs to be done...
        data = None
        if not contour.has_key('file'):
            return panel

        if type(contour['file']) != str or not os.path.exists(contour['file']) or \
               vwr.fileinfo(contour['file'])['type'] != 'image':
            casalog.post( str(contour['file']) + " does not exist or is not an image", 'SEVERE')
            raise Exception, contour['file'] + " does not exist or is not an image"

        if panel is None:
            panel = self.__panel(vwr)

        data = vwr.load( contour['file'], 'contour', panel=panel )

        if contour.has_key('levels'):
            vwr.contourlevels( self.__checknumeric(contour['levels'], float, "contour levels", array_size=0), data=data )
        if contour.has_key('unit'):
            vwr.contourlevels( unitlevel=self.__checknumeric(contour['unit'], float, "contour unitlevel"), data=data )
        if contour.has_key('base'):
            vwr.contourlevels( baselevel=self.__checknumeric(contour['base'], float, "contour baselevel"), data=data )

        try:
            if contour.has_key('thickness'):
                vwr.contourthickness( thickness=self.__checknumeric(contour['thickness'], float, "contour thickness"), data=data )
            if contour.has_key('color'):
                vwr.contourcolor( contour['color'], data=data )
        except:
            print "viewertool error: %s" % sys.exc_info()[1]

        return panel
Example #12
0
    def __initial_guess(self, scantab, dbw, numfit, comps, irow):
        llist = self.linelist[irow] if self.fitmode == 'auto' \
                else self.linelist
        if len(llist) > 0:
            # guesses: [maxlist, cenlist, fwhmlist]
            guesses = [[],[],[]]
            for x in llist:
                x.sort()
                casalog.post( "detected line: "+str(x) ) 
                msk = scantab.create_mask(x, row=irow)
                guess = self.__get_initial_guess(scantab,msk,x,dbw,irow)
                for i in xrange(3):
                    guesses[i] = guesses[i] + [guess[i]]
        else:
            guess = self.__get_initial_guess(scantab,self.defaultmask,[],dbw,irow)
            guesses = [[guess[i]] for i in xrange(3)]

        # Guesses using max, cen, and fwhm=0.7*eqw
        # NOTE: should there be user options here?
        n = 0
        for i in range(numfit):
            # cannot guess for multiple comps yet
            if ( comps[i] == 1 ):
                # use guess
                #getattr(self.fitter,'set_%s_parameters'%(self.fitfunc))(maxl[i], cenl[i], fwhm[i], component=n)
                guess = (guesses[k][i] for k in xrange(3))
                getattr(self.fitter,'set_%s_parameters'%(self.fitfunc))(*guess, component=n)
            n += comps[i]
Example #13
0
    def select(self, rowid=None, rasterid=None):
        if not ((rowid is None) ^ (rasterid is None)):
            raise RuntimeError('one of rowid or rasterid must be specified')

        if (rowid is not None) and (rowid >= self.nrow):
            raise IndexError('row index %s is out of range (number of rows detected: %s)'%(rowid,self.nrow))
        if (rasterid is not None) and (rasterid >= self.nraster):
            raise IndexError('row index %s is out of range (number of rasters detected: %s)'%(rasterid,self.nraster))

        with selection_manager(self.scantab, self.original_selection, types=0, ifs=self.spw, pols=self.pol) as s:
            alltimes = numpy.array(map(lambda x: qa.quantity(x)['value'], s.get_time(prec=16)))
            mean_interval = numpy.array(s.get_inttime()).mean()
            self.margin = 0.1 * mean_interval
            mjd_margin = self.margin / 86400.0

        if rowid is not None:
            times = alltimes[self.gaplist[rowid]:self.gaplist[rowid+1]]
        else:
            times = alltimes[self.gaplist_raster[rasterid]:self.gaplist_raster[rasterid+1]]

        tmp_mjd_range = (times.min() - mjd_margin, times.max() + mjd_margin,)
        tmp_mjd_range_nomargin = (times.min(), times.max(),)
        casalog.post('time range: %s ~ %s'%(tmp_mjd_range_nomargin), priority='DEBUG')

        if rowid is not None:
            self.mjd_range = tmp_mjd_range
            self.mjd_range_nomargin = tmp_mjd_range_nomargin
        else:
            self.mjd_range_raster = tmp_mjd_range
            self.mjd_range_nomargin_raster = tmp_mjd_range_nomargin
Example #14
0
 def _set_linelist_interact(self, scantab=None):
     if scantab is None:
         scantab = self.scan
     # Interactive masking
     new_mask = sdutil.init_interactive_mask(scantab,
                                             self.maskline,
                                             False)
     self.defaultmask = sdutil.get_interactive_mask(new_mask,
                                                    purpose='to fit lines')
     self.linelist=scantab.get_masklist(self.defaultmask)
     self.nlines=len(self.linelist)
     if self.nlines < 1:
         msg='No channel is selected. Exit without fittinging.'
         raise Exception(msg)
     print '%d region(s) is selected as a linemask' % self.nlines
     print 'The final mask list ('+scantab._getabcissalabel()+') ='+str(self.linelist)
     print 'Number of line(s) to fit: nfit =',self.nfit
     ans=raw_input('Do you want to reassign nfit? [N/y]: ')
     if (ans.upper() == 'Y'):
         ans=input('Input nfit = ')
         if type(ans) == list: self.nfit = ans
         elif type(ans) == int: self.nfit = [ans]
         else:
             msg='Invalid definition of nfit. Setting nfit=[1] and proceed.'
             casalog.post(msg, priority='WARN')
             self.nfit = [1]
         casalog.post('List of line number reassigned.\n   nfit = '+str(self.nfit))
     sdutil.finalize_interactive_mask(new_mask)
Example #15
0
 def __init_plotter(self):
     colormap = ["green","red","#dddddd","#777777"]
     self.myp = sdutil.get_plotter(self.plotlevel)
     casalog.post('Create new plotter')
     self.myp.palette(0,colormap)
     self.myp.hold()
     self.myp.clear()
Example #16
0
 def __execute_imaging(self):
     #if pointingcolumn.upper()=="OFFSET":
     #   pointingcolumn="POINTING_OFFSET"
     #print "pointingcolumn=",pointingcolumn
     #print "Imaging...."
     casalog.post( "pointingcolumn used: %s" % self.pointingcolumn )
     casalog.post( "Imaging...." )
     self.open_imager(self.infile)
     self.imager.selectvis(spw=self.spw, baseline=self.antsel)
     #self.imager.selectvis(field=0, spw=self.spw, baseline=self.antsel)
     #self.imager.selectvis(vis=infile, field=0, spw=spw, baseline=antsel)
     #self.imager.defineimage(nx=nx, ny=ny, cellx=cellx, celly=celly,  phasecenter=phasecenter, spw=0, stokes=stokes, movingsource=ephemsrcname)
     self.imager.defineimage(nx=self.nx, ny=self.ny, cellx=self.cellx, celly=self.celly, phasecenter=self.phasecenter, spw=self.spw, stokes=self.stokes, movingsource=self.ephemsrcname)
     #self.imager.defineimage(nx=nx, ny=ny, cellx=cellx, celly=celly,  phasecenter=phasecenter, spw=0, stokes=stokes, movingsource=ephemsrcname,restfreq=1.14e11)
     self.imager.setoptions(ftmachine='sd', gridfunction=self.gridfunction)
     #self.imager.setsdoptions(convsupport=5)
     self.imager.setsdoptions(pointingcolumntouse=self.pointingcolumn, convsupport=-1, truncate=-1, gwidth=-1, jwidth=-1)
     self.imager.makeimage(type='singledish-observed', image=self.outfile)
     #self.imager.makeimage(type='singledish', image=self.outfile)
     # create temporal weight image for masking
     weightfile_temp = self.outfile+".weight."+str(time.time()).replace('.', '_')
     self.imager.makeimage(type='coverage', image=weightfile_temp)
     self.close_imager()
     # Handle mask
     my_ia = gentools(['ia'])[0]
     my_ia.open(self.outfile)
     my_ia.calcmask("'%s'>%f" % (weightfile_temp,0.0), asdefault=True)
     my_ia.close()
     shutil.rmtree(weightfile_temp)
Example #17
0
 def get_kernel(self, axis):
     """Returns imaging kernel array"""
     self.__assert_kernel()
     if self.kernel_type == "SF":
         ### TODO: what to do for cell[0]!=cell[1]???
         return self.get_sf_kernel(axis,self.kernel_param['convsupport'],
                                   self.cell_arcsec[0])
     elif self.kernel_type == "GJINC":
         return self.get_gjinc_kernel(axis,self.kernel_param['truncate'],
                                      self.kernel_param['gwidth'],
                                      self.kernel_param['jwidth'],
                                      self.cell_arcsec[0])
     elif self.kernel_type == "GAUSS":
         return self.get_gauss_kernel(axis,self.kernel_param['truncate'],
                                      self.kernel_param['gwidth'],
                                      self.cell_arcsec[0])
     elif self.kernel_type == "BOX":
         return self.get_box_kernel(axis,self.kernel_param['width'])
     elif self.kernel_type == "PB":
         diam = self.antenna_diam_m
         if self.kernel_param['alma']:
             diam = 10.7
             casalog.post("Using effective antenna diameter %fm for %s kernel of ALMA antennas" % (diam,self.kernel_type))
         epsilon = self.antenna_block_m/diam
         return self.get_pb_kernel(axis,diam,self.ref_freq, epsilon=epsilon)
         #return (self.rootAiryIntensity(axis, epsilon))**2
     else:
         raise RuntimeError, "Invalid kernel: %s" % self.kernel_type
Example #18
0
def plotprofilemap(imagename=None, figfile=None, overwrite=None, transparent=None,
                   pol=None, spectralaxis=None, restfreq=None, plotrange=None, title=None, 
                   linecolor=None, linestyle=None, linewidth=None,
                   separatepanel=None, plotmasked=None, maskedcolor=None, 
                   showaxislabel=None, showtick=None, showticklabel=None,
                   figsize=None, numpanels=None):
    casalog.origin('plotprofilemap')
    
    try:
        if len(figfile) > 0 and os.path.exists(figfile) and overwrite == False:
            raise RuntimeError('overwrite is False and output file exists: \'%s\''%(figfile))
    
        image = SpectralImage(imagename)
        npol = len(image.stokes)
        if pol < 0 or pol > npol - 1:
            raise RuntimeError('pol {pol} is out of range (Stokes axis {stokes})'.format(pol=pol,stokes=image.stokes))        

        parsed_size = parse_figsize(figsize)
        nx, ny = parse_numpanels(numpanels)
        if (not isinstance(restfreq, str)) or len(restfreq) == 0:
            restfreq = None 
        plot_profile_map(image, figfile, pol, spectralaxis, restfreq, title, linecolor, linestyle, linewidth,
                         separatepanel, plotmasked, maskedcolor,
                         showaxislabel, showtick, showticklabel, parsed_size,
                         nx, ny, transparent, plotrange)
    except Exception, e:
        casalog.post('Error: %s'%(str(e)), priority='SEVERE')
        import traceback
        casalog.post(traceback.format_exc(), priority='DEBUG')
        raise e
Example #19
0
 def __antenna_summary(self):
     """Print summary of antenna setup"""
     if not self.is_antenna_set:
         self.__notset_message()
         return
     casalog.post("diameter: %f m" % (self.antenna_diam_m))
     casalog.post("blockage: %f m" % (self.antenna_block_m))
Example #20
0
    def __init__(self, nh, nv, brightnessunit, direction_label, direction_reference, 
                 spectral_label, spectral_unit, ticksize, title='', 
                 separatepanel=True, 
                 showaxislabel=False, showtick=False, showticklabel=False,
                 figsize=None,
                 clearpanel=True):
        self.nh = nh
        self.nv = nv
        self.ticksize = ticksize
        self.brightnessunit = brightnessunit
        self.numeric_formatter = pl.FormatStrFormatter('%.2f')
        self.direction_label = direction_label
        self.direction_reference = direction_reference
        self.separatepanel = separatepanel
        self.spectral_label = spectral_label
        self.spectral_unit = spectral_unit
        self.showaxislabel = showaxislabel
        self.showtick = showtick
        self.showticklabel = showticklabel
        self.title = title
        self.figsize = figsize
        casalog.post('figsize={figsize}'.format(figsize=self.figsize), priority='DEBUG')
        
        self._axes_spmap = None

        # to resize matplotlib window to specified size        
        pl.figure(self.MATPLOTLIB_FIGURE_ID)
        pl.close()
        
        if self.figsize is None:
            pl.figure(self.MATPLOTLIB_FIGURE_ID)
        else:
            pl.figure(self.MATPLOTLIB_FIGURE_ID, figsize=self.figsize)
        if clearpanel:
            pl.clf()
Example #21
0
        def set_status(self,keyword,value):
            
            casalog_call_origin = "MPIMonitorServer::set_status"

            if self.__status.has_key(keyword):
                self.__status[keyword] = value
            else:
                casalog.post("Status keyword %s not defined" % str(keyword),"WARN",casalog_call_origin)          
Example #22
0
 def __summarize_raw_inputs(self):
     params = ['infiles', 'antenna', 'scanno', 'spw',
               'polno', 'gridfunction', 'convsupport',
               'truncate', 'gwidth', 'jwidth', 'weight',
               'clipminmax', 'outfile', 'overwrite',
               'npix', 'cell', 'center', 'plot']
     summary = self.__generate_summary(header='Input Parameter Summary',
                                       params=params)
     casalog.post(summary, priority='DEBUG')
Example #23
0
def clearcal(
    vis=None,
    field=None,
    spw=None,
    intent=None,
    addmodel=None,
    ):

    casalog.origin('clearcal')

    # Do the trivial parallelization
    if ParallelTaskHelper.isParallelMS(vis):
        helper = ParallelTaskHelper('clearcal', locals())
        helper.go()
        return

    # Local versions of the tools
    tblocal = tbtool()
    cblocal = cbtool()
    mslocal = mstool()

    try:

        # we will initialize scr cols only if we don't create them
        doinit = False

        if (type(vis) == str) & os.path.exists(vis):
            tblocal.open(vis)
            doinit = tblocal.colnames().count('CORRECTED_DATA') > 0
            tblocal.close()

            # We ignore selection if creating the scratch columns
            if not doinit:
                casalog.post('Need to create scratch columns; ignoring selection.'
                             )

            cblocal.open(vis, addmodel=addmodel)
        else:
            raise Exception, \
                'Visibility data set not found - please verify the name'

        # If necessary (scr col not just created), initialize scr cols
        if doinit:
            cblocal.selectvis(field=field, spw=spw, intent=intent)
            cblocal.initcalset(1)
        cblocal.close()

        # Write history to the MS
        param_names = clearcal.func_code.co_varnames[:clearcal.func_code.co_argcount]
        param_vals = [eval(p) for p in param_names]
        casalog.post('Updating the history in the output', 'DEBUG1')
        write_history(mslocal, vis, 'clearcal', param_names,
                      param_vals, casalog)
        
    except Exception, instance:

        print '*** Error ***', instance
Example #24
0
    def parameter_check(self):
        self.assert_no_channel_selection_in_spw('warn')

        if self.gridfunction.upper() == 'PB':
            msg='Sorry. PB gridding is not implemented yet.'
            raise Exception, msg
        elif self.gridfunction.upper() == 'BOX':
            casalog.post('convsupport is automatically set to -1 for BOX gridding.', priority='WARN')
            self.convsupport = -1
Example #25
0
 def save(self):
     if ( len(self.outfile) > 0 ):
         if ( not os.path.exists(sdutil.get_abspath(self.outfile)) \
              or self.overwrite ):
             f=open(self.outfile,'w')
             f.write(self.savestats)
             f.close()
         else:
             casalog.post( 'File '+self.outfile+' already exists.\nStatistics results are not written into the file.', priority = 'WARN' )
Example #26
0
def check_unit(unit_in,valid_unit=None,default_unit=None):
    # CAS-5410 Use private tools inside task scripts
    qa = qatool()

    if qa.check(unit_in):
        return valid_unit
    else:
        casalog.post('Undefined unit: \'%s\'...ignored'%(unit_in), priority='WARN')
        return default_unit
Example #27
0
    def finalize(self):
        if ( abs(self.plotlevel) > 1 ):
            casalog.post( "Final Calibrated Scantable:" )
            self.worker.scan._summary()

        # Plot final spectrum
        if ( abs(self.plotlevel) > 0 ):
            pltfile = self.project + '_calspec.eps'
            sdutil.plot_scantable(self.worker.scan, pltfile, self.plotlevel)
Example #28
0
def _resetweight(vis):
    # work with private tb tool
    casalog.post('fill WEIGHT and SIGMA failed. reset all WEIGHT and SIGMA to 1.0...', priority='WARN')
    with sdutil.tbmanager(vis, nomodify=False) as tb:
        for column in ['WEIGHT', 'SIGMA']:
            values = tb.getvarcol(column)
            for v in values.values():
                v[:] = 1.0
            tb.putvarcol(column, values)
Example #29
0
 def check_outfile(self):
     if len(self.outfile) > 0:
         # outfile is specified
         if os.path.exists(self.outfile):
             if not self.overwrite:
                 raise Exception('Output file \'%s\' exists.'%(self.outfile))
             else:
                 casalog.post('Overwrite %s ...'%(self.outfile), 'INFO')
                 os.system('rm -rf %s'%(self.outfile))
Example #30
0
 def __summarize_compiled_inputs(self):
     params = ['infiles', 'ifno', 'scans', 'pols',
               'gridfunction', 'convsupport',
               'truncate', 'gwidth', 'jwidth', 'weight',
               'clipminmax', 'outname', 'overwrite',
               'nx', 'ny', 'cellx', 'celly',
               'mapcenter', 'plot']
     summary = self.__generate_summary(header='Grid Parameter Summary',
                                       params=params)
     casalog.post(summary, priority='DEBUG')
Example #31
0
	def getGains(self,solver,accumd,accummodel,accumwt,accumfl,goodbl):	
		nsamples=self.solintMap[self.ispw][self.isol]
		if(not self.combinepoln):	
		
			for j in range(0,self.nCorr):
				thisflags=accumfl[:nsamples,goodbl,:,j]
				if(np.sum(thisflags)==0):
					casalog.post("No unflagged samples")
					self.gains[j,0,:]=1+1j*0
					self.antFlags[j,0,:]=np.zeros(self.Nant,dtype=np.bool)
					continue
				self.gains[j,0,:],self.gains_er[j,0,:],self.antFlags[j,0,:]=solver.solve(accumd[:nsamples,goodbl,:,j],accummodel[:nsamples,goodbl,:,j],accumfl[:nsamples,goodbl,:,j],accumwt[:nsamples,goodbl,:,j])
				if(not self.antFlags[j,0,self.refant]):
					self.gains[j,0,:]=self.gains[j,0,:]*np.exp(1j*np.angle(self.gainsOld[j,0,solver.refant]))					
					solver.refant=self.refant
					
		else:
			thisflags=accumfl[:nsamples,goodbl,:,:]
			if(np.sum(thisflags)==0):
				casalog.post("No unflagged samples")
				self.gains[:,0,:]=1+1j*0
				self.antFlags[:,0,:]=np.zeros(self.Nant,dtype=np.bool)
				return

			nCorr=accumd.shape[3]
			nChan=accumd.shape[2]
			nBaseline=accumd.shape[1]
			newshape=(nsamples,nBaseline,nCorr*nChan)
			thisdata=np.swapaxes(accumd[:nsamples,:,:,:].reshape(newshape),0,1)
			thismodel=np.swapaxes(accummodel[:nsamples,:,:,:].reshape(newshape),0,1)
			thisflag=np.swapaxes(accumfl[:nsamples,:,:,:].reshape(newshape),0,1)
			accumwt_tile=np.tile(accumwt,(1,1,nChan//accumwt.shape[2],1))
			thiswt=np.swapaxes(accumwt_tile[:nsamples,:,:,:].reshape(newshape),0,1)
			self.gains[0,0,:],self.gains_er[0,0,:],self.antFlags[0,0,:]= solver.solve(thisdata[goodbl,:,:],thismodel[goodbl,:,:],thisflag[goodbl,:,:],thiswt[goodbl,:,:])
			if(not self.antFlags[0,0,self.refant]):
				self.gains[0,0,:]=self.gains[0,0,:]*np.exp(1j*np.angle(self.gainsOld[0,0,solver.refant]))					
				solver.refant=self.refant
			self.gains[1,:,:]=self.gains[0,:,:]
			self.gains_er[1,:,:]=self.gains_er[0,:,:]
			self.antFlags[1,:,:]=self.antFlags[0,:,:]
Example #32
0
 def __dochannelrange(self, scantab):
     if len(self.spw) > 0:
         sel_org = scantab.get_selection()
         channelrange_dic = scantab.parse_spw_selection(self.spw)
         valid_spw_list = []
         for (k,v) in channelrange_dic.items():
             casalog.post('k=%s, v=%s'%(k,v), priority='DEBUG')
             unique_list = []
             for item in map(list, v):
                 if not item in unique_list:
                     unique_list.append(item)
             casalog.post('unique_list=%s'%(unique_list), priority='DEBUG')
             if len(unique_list) > 1:
                 raise SyntaxError('sdsaveold doesn\'t support multiple channel range selection for spw.')
             elif len(unique_list) == 0:
                 raise SyntaxError('Invalid channel range specification')
             elif numpy.any(numpy.array(map(len, unique_list)) == 0):
                 # empty channel range
                 continue
             nchan = scantab.nchan(k)
             full_range = [0.0, float(nchan-1)]
             if unique_list[0] != full_range:
                 sel = sd.selector()
                 sel.set_ifs(k)
                 scantab.set_selection(sel)
                 sdutil.dochannelrange(scantab, unique_list[0])
                 scantab.set_selection()
             valid_spw_list.append(k)
         sel = sd.selector(sel_org)
         ifs_org = sel.get_ifs()
         ifs_new = list(set(ifs_org) & set(valid_spw_list))
         casalog.post('ifs_new = %s'%(ifs_new))
         sel.set_ifs(ifs_new)
         scantab.set_selection(sel)
Example #33
0
        def __start_ping_status_request_handler_service(self):

            casalog_call_origin = "MPIMonitorServer::start_ping_status_request_handler_service"

            if self.__ping_status_request_handler_service_running:
                casalog.post(
                    "MPI ping status request handler service is already running",
                    "WARN", casalog_call_origin)
                return True

            try:
                self.__ping_status_request_handler_service_on = True
                self.__ping_status_request_handler_service_thread = thread.start_new_thread(
                    self.__ping_status_request_handler_service, ())
            except Exception as instance:
                self.__ping_status_request_handler_service_on = False
                self.__ping_status_request_handler_service_running = False
                casalog.post(
                    "Exception starting MPI ping status request handler service: %s"
                    % str(instance), "SEVERE", casalog_call_origin)
                return False

            while (not self.__ping_status_request_handler_service_running):
                time.sleep(MPIEnvironment.mpi_check_start_service_sleep_time)

            casalog.post("MPI ping status request handler service started",
                         "INFO", casalog_call_origin)

            return True
Example #34
0
    def __load_files( self, filetype, vwr, panel, files ):

        if filetype != "raster" and filetype != "contour":
            casalog.post( "internal error __load_files( )...", 'SEVERE')
            raise Exception, "internal error __load_files( )..."

        if type(files) == str:
            panel = self.__load_raster( vwr, panel, { 'file': files } ) if filetype == 'raster' else \
                        self.__load_contour( vwr, panel, { 'file': files } )
        elif type(files) == dict:
            panel = self.__load_raster( vwr, panel, files ) if filetype == 'raster' else \
                        self.__load_contour( vwr, panel, files )
        elif type(files) == list:
            if all(map( lambda x: type(x) == dict, files )):
                for f in files:
                    panel = self.__load_raster( vwr, panel, f ) if filetype == 'raster' else \
                                self.__load_contour( vwr, panel, f )
            elif all(map( lambda x: type(x) == str, files )):
                for f in files:
                    panel = self.__load_raster( vwr, panel, { 'file': f } ) if filetype == 'raster' else \
                                self.__load_contour( vwr, panel, { 'file': f } )
            else:
                casalog.post( "multiple " + str(filetype) + " specifications must be either all dictionaries or all strings", 'SEVERE')
                raise Exception, "multiple " + filetype + " specifications must be either all dictionaries or all strings"
        else:
            casalog.post( filetype + "s can be a single file path (string), a single specification (dictionary), or a list containing all strings or all dictionaries", 'SEVERE')
            raise Exception, filetype + "s can be a single file path (string), a single specification (dictionary), or a list containing all strings or all dictionaries"
        return panel
Example #35
0
        def stop_virtual_frame_buffer(self):
            
            casalog_call_origin = "MPICommandServer::stop_virtual_frame_buffer"
            
            if self.__virtual_frame_buffer_process is not None:
                try:
                    self.__virtual_frame_buffer_process.terminate()
                    casalog.post("Virtual frame buffer deployed at %s with pid %s successfully shutdown" % 
                                 (self.__virtual_frame_buffer_port,
                                  str(self.__virtual_frame_buffer_process.pid)),
                                 "DEBUG",casalog_call_origin)
                    self.__virtual_frame_buffer_process = None
                except:
                    formatted_traceback = traceback.format_exc()
                    casalog.post("Exception shutting down virtual frame buffer deployed at %s with pid %s: %s" 
                                 % (self.__virtual_frame_buffer_port,
                                    str(self.__virtual_frame_buffer_process.pid),
                                    str(formatted_traceback)),
                                    "SEVERE",casalog_call_origin)
            else:
                casalog.post("Virtual frame buffer not deployed","WARN",casalog_call_origin)            

            try:
                subprocess.call(['xauth', '-f', self.__xauthfile.name, 'remove',
                                 self.__virtual_frame_buffer_port],
                                 stdout=self.__logfile_descriptor,
                                 stderr=self.__logfile_descriptor)
            except OSError:
                pass
            self.__xauthfile.close()
Example #36
0
    def __overlay_linecatalog(self):
        # Use jpl catalog for now (later allow custom catalogs)

        casapath = os.environ['CASAPATH'].split()
        catpath = casapath[0] + '/data/catalogs/lines'
        catname = catpath + '/jpl_asap.tbl'
        # TEMPORARY: hard-wire to my version
        # catname='/home/sandrock/smyers/NAUG/Tasks/jpl.tbl'
        # FOR LOCAL CATALOGS:
        # catname='jpl.tbl'
        try:
            linc = sd.linecatalog(catname)
            dolinc = True
        except:
            casalog.post("Could not find catalog at " + catname,
                         priority="WARN")
            dolinc = False
        if (dolinc):
            if (len(self.sprange) > 1):
                if (specunit == 'GHz' or specunit == 'MHz'):
                    linc.set_frequency_limits(sprange[0], sprange[1], specunit)
                else:
                    casalog.post(
                        "sd.linecatalog.set_frequency_limits accepts onlyGHz and MHz",
                        priority='WARN')
                    casalog.post(
                        "continuing without sprange selection on catalog",
                        priority='WARN')
            if (self.linecat != 'all' and self.linecat != 'ALL'):
                # do some molecule selection
                linc.set_name(self.linecat)
            # Plot up the selected part of the line catalog
            # use doppler offset
            sd.plotter.plot_lines(linc, doppler=self.linedop)
            del linc
Example #37
0
 def _get_imsize(self, width, height, dx, dy):
     casalog.post("Calculating pixel size.")
     # CAS-5410 Use private tools inside task scripts
     my_qa = qatool()
     ny = numpy.ceil( ( my_qa.convert(height, my_qa.getunit(dy))['value'] /  \
                        my_qa.getvalue(dy) ) )
     nx = numpy.ceil( ( my_qa.convert(width, my_qa.getunit(dx))['value'] /  \
                        my_qa.getvalue(dx) ) )
     casalog.post("- Map extent: [%s, %s]" %
                  (my_qa.tos(width), my_qa.tos(height)))
     casalog.post("- Cell size: [%s, %s]" % (my_qa.tos(dx), my_qa.tos(dy)))
     casalog.post("Image pixel numbers to cover the extent: [%d, %d] (projected)" % \
                  (nx+1, ny+1))
     return (int(nx + 1), int(ny + 1))
Example #38
0
 def test1_applycal_fluxscale_gcal_bcal(self):
     """Test 1: Apply calibration using fluxscal gcal and bcal tables"""
     
     # Run applycal in MS mode
     applycal(vis=self.ref,field='',spw='',selectdata=False,gaintable=self.aux,
              gainfield=['nearest','nearest','0'],
              interp=['linear', 'linear','nearest'],spwmap=[])
     
     # Run applycal in MMS mode
     applycal(vis=self.vis,field='',spw='',selectdata=False,gaintable=self.aux,
              gainfield=['nearest','nearest','0'],
              interp=['linear', 'linear','nearest'],spwmap=[])
     
     # Sort file to properly match rows for comparison
     casalog.post("Sorting vis file: %s" % str(self.vis),"INFO","test1_applycal_fluxscale_gcal_bcal")
     sortFile(self.vis,self.vis_sorted)  
     casalog.post("Sorting ref file: %s" % str(self.ref),"INFO","test1_applycal_fluxscale_gcal_bcal")    
     sortFile(self.ref,self.ref_sorted)        
     
     # Compare files
     compare = testhelper.compTables(self.ref_sorted,self.vis_sorted,['FLAG_CATEGORY'])
     self.assertTrue(compare)      
Example #39
0
    def __pushd(self, vwr, newdir):
        try:
            old_path = vwr.cwd()
        except:
            casalog.post(
                "imview() failed to get the current working directory [" +
                str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]",
                'SEVERE')
            raise Exception, "imview() failed to get the current working directory [" + str(
                sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]"

        self.__dirstack.append(old_path)
        try:
            vwr.cwd(newdir)
        except:
            casalog.post(
                "imview() failed to change to the new working directory (" +
                os.path.abspath(os.curdir) + ") [" + str(sys.exc_info()[0]) +
                ": " + str(sys.exc_info()[1]) + "]", 'SEVERE')
            raise Exception, "imview() failed to change to the new working directory (" + os.path.abspath(
                os.curdir) + ") [" + str(sys.exc_info()[0]) + ": " + str(
                    sys.exc_info()[1]) + "]"
Example #40
0
    def save(self):
        self.scan.set_selection(None)
        #sdutil.save(self.scan, self.project, self.outform, self.overwrite)
        sdutil.assert_outfile_canoverwrite_or_nonexistent(
            self.project, self.outform, self.overwrite)
        outform_local = self.outform.upper()
        if outform_local == 'MS': outform_local = 'MS2'
        if outform_local not in ['ASAP', 'ASCII', 'MS2', 'SDFITS']:
            outform_local = 'ASAP'

        # SHOULD NOT remove infile if disk storage and infile==outfile
        outfilename = sdutil.get_abspath(self.project)
        if self.overwrite and os.path.exists(outfilename):
            if not self.is_disk_storage or \
                    (outfilename != sdutil.get_abspath(self.infile)):
                os.system('rm -rf %s' % outfilename)

        self.scan.save(self.project, outform_local, self.overwrite)

        if outform_local != 'ASCII':
            casalog.post('Wrote output %s file %s' %
                         (outform_local, self.project))
Example #41
0
        def __ping_status_response_handler_service(self):

            casalog_call_origin = "MPIMonitorClient::ping_status_response_handler_service"

            # Mark service as running
            self.__ping_status_response_handler_service_running = True

            while (self.__ping_status_response_handler_service_on):

                # First check if there is a ping_status response msg available
                msg_available = False
                try:
                    msg_available = self.__communicator.ping_status_response_probe(
                    )
                except:
                    msg_available = False
                    formatted_traceback = traceback.format_exc()
                    casalog.post(
                        "Exception checking if ping status response msg is available: %s"
                        % str(formatted_traceback), "SEVERE",
                        casalog_call_origin)

                # Then receive, store and post ping_status response msg
                if (msg_available):
                    try:
                        ping_status_response = self.__communicator.ping_status_response_recv(
                        )
                        pong_time = time.time()
                        rank = ping_status_response['rank']
                        self.__server_status_list[rank][
                            'command'] = ping_status_response['command']
                        self.__server_status_list[rank][
                            'command_start_time'] = ping_status_response[
                                'command_start_time']
                        self.__server_status_list[rank][
                            'pong_time'] = pong_time
                        self.__server_status_list[rank]['pong_pending'] = False
                        elapsed_time = pong_time - self.__server_status_list[
                            rank]['ping_time']
                        # Notify if the response has been received after timeout
                        if self.__server_status_list[rank]['timeout']:
                            self.__server_status_list[rank]['timeout'] = False
                            casalog.post(
                                "Ping status response from server %s received after %ss"
                                % (str(rank), str(int(elapsed_time))), "WARN",
                                casalog_call_origin)
                    except:
                        formatted_traceback = traceback.format_exc()
                        casalog.post(
                            "Exception receiving ping status response msg: %s"
                            % str(formatted_traceback), "SEVERE",
                            casalog_call_origin)
                else:
                    time.sleep(
                        MPIEnvironment.
                        mpi_ping_status_response_handler_service_sleep_time)

            # Mark service as not running
            self.__ping_status_response_handler_service_running = False
Example #42
0
def get_value(cfg, sect, opt, default=None, n=None, sep=','):
    # Initial value
    value = cfg.get(sect, opt)

    # Split value
    value = value.split(sep)

    # Cases:
    if len(value) == 0:
        value = ''
    elif len(value) == 1 and n is None:
        value = value[0]
    elif len(value) > 1 and n is None:
        # Just in case someone used coma separated value for single eb
        casalog.post('More than one value for %s, using all' % opt, 'WARN')
        value = ' '.join(value)
    elif n >= 0:
        value = value[n]
    else:
        value = default

    return value
Example #43
0
    def __checknumeric( self, value, otype, error_string, array_size=None ):
        if array_size is not None:
            if type(array_size) != int:
                casalog.post( "internal error: array_size is expected to be of type int", 'SEVERE')
                raise Exception, "internal error: array_size is expected to be of type int"
            if type(value) != list and not isinstance(value,ndarray):
                casalog.post( error_string + " must be a list", 'SEVERE')
                raise Exception, error_string + " must be a list"
            if array_size > 0 and len(value) != array_size:
                numbers = { '1': 'one', '2': 'two', '3': 'three' }
                casalog.post( error_string + " can only be a " + numbers[str(array_size)] + " element numeric list", 'SEVERE')
                raise Exception, error_string + " can only be a " + numbers[str(array_size)] + " element numeric list"
            if not all(map( lambda x: type(x) == int or type(x) == float or isinstance(x,float64), value )):
                casalog.post( error_string + " must be a numeric list", 'SEVERE')
                raise Exception, error_string + " must be a numeric list"
            return map( lambda x: otype(x), value )
                    
        if type(value) != int and type(value) != float:
            casalog.post( error_string + " must be numeric", 'SEVERE')
            raise Exception, error_string + " must be numeric"

        return otype(value)
Example #44
0
def pmaxfit(imagefiles, ncpu, box, width):
    from functools import partial
    # check if imagefiles is a single file or a list of files

    if isinstance(imagefiles, str):
        imagefiles = [imagefiles]
    if (not isinstance(imagefiles, list)):
        print 'input "imagefiles" is not a list. Abort...'

    # check file existence
    imgfiles = []
    for i, img in enumerate(imagefiles):
        if os.path.exists(img):
            imgfiles.append(img)
        else:
            casalog.post(img + 'does not exist. Skipping this one...')

    iterable = range(len(imgfiles))
    res = []

    if not (type(ncpu) is int):
        casalog.post('ncpu should be an integer')
        ncpu = 8

    # partition
    maxfit_part = partial(maxfit_iter, imgfiles, box, width)

    # parallelization
    para = 1
    timelapse = 0
    t0 = time()
    if para:
        casalog.post('Perform maxfit in parallel ...')
        pool = mprocs.Pool(ncpu)
        # res = pool.map_async(maxfit_part, iterable)
        res = pool.map(maxfit_part, iterable)
        pool.close()
        pool.join()
    else:
        for i in iterable:
            res.append(maxfit_part(i))

    t1 = time()
    timelapse = t1 - t0
    print 'It took %f secs to complete' % timelapse
    # repackage this into a single dictionary
    results = {
        'succeeded': [],
        'timestamps': [],
        'imagenames': [],
        'outputs': []
    }
    for r in res:
        results['succeeded'].append(r[0])
        results['timestamps'].append(r[1])
        results['imagenames'].append(r[2])
        results['outputs'].append(r[3])

    return results
Example #45
0
    def __set_axes(self, vwr, panel, axes):
        x = ''
        y = ''
        z = ''
        invoke = False
        if type(axes) == list and len(axes) == 3 and \
           all( map( lambda x: type(x) == str, axes ) ) :
            x = axes[0]
            y = axes[1]
            z = axes[2]
            invoke = True
        elif type(axes) == dict:
            if axes.has_key('x'):
                if type(axes['x']) != str:
                    casalog.post(
                        "dimensions of axes must be strings (x is not)",
                        'SEVERE')
                    raise Exception, "dimensions of axes must be strings (x is not)"
                x = axes['x']
                invoke = True
            if axes.has_key('y'):
                if type(axes['y']) != str:
                    casalog.post(
                        "dimensions of axes must be strings (y is not)",
                        'SEVERE')
                    raise Exception, "dimensions of axes must be strings (y is not)"
                y = axes['y']
                invoke = True
            if axes.has_key('z'):
                if type(axes['z']) != str:
                    casalog.post(
                        "dimensions of axes must be strings (z is not)",
                        'SEVERE')
                    raise Exception, "dimensions of axes must be strings (z is not)"
                z = axes['z']
                invoke = True
        else:
            casalog.post(
                "'axes' must either be a string list of 3 dimensions or a dictionary",
                'SEVERE')
            raise Exception, "'axes' must either be a string list of 3 dimensions or a dictionary"

        result = False
        if invoke:
            vwr.axes(x, y, z, panel=panel)
            result = True

        return result
Example #46
0
    def plot_baseline_fringe_p(self, l, n_cols, i, j):
        nlumps = 5  # number of peaks in sincified data to show
        (n_times, ) = self.ffd.times.shape
        (n_freqs, ) = self.ffd.freqs.shape

        c = self.ffd.c_all[i, j]
        peak_ind = self.ffd.get_fringe_peak_ind(i, j)
        xpeak_ind, ypeak_ind = peak_ind
        xpeak = self.ffd.xaxis0[peak_ind]
        ypeak = self.ffd.yaxis0[peak_ind]
        casalog.post(
            "Max value {}; Fringe rate: {}; Delay: {}".format(
                c[peak_ind], ypeak, xpeak), "DEBUG")
        casalog.post("Index {}".format(peak_ind), "DEBUG")
        #
        pad = self.ffd.pad
        xslice = slice(n_freqs * pad / 2 - nlumps * pad,
                       n_freqs * pad / 2 + nlumps * pad)
        yslice = slice(n_times * pad / 2 - nlumps * pad,
                       n_times * pad / 2 + nlumps * pad)
        #
        plt.subplot(2, n_cols, l + 1)
        plt.plot(self.ffd.xaxis0[xslice, ypeak_ind],
                 np.abs(c[xslice, ypeak_ind]))
        plt.ylabel('Fringe height')
        plt.xlabel('Group delay (s) ({}-{})'.format(i, j))
        plt.axvline(x=xpeak, linestyle='--', color='r')
        #
        plt.subplot(2, n_cols, n_cols + l + 1)
        plt.plot(self.ffd.yaxis0[xpeak_ind, yslice],
                 np.abs(c[xpeak_ind, yslice]))
        plt.ylabel('Fringe height')
        plt.xlabel('Delay rate (s/s) ({}-{})'.format(i, j))
        plt.axvline(x=ypeak, linestyle='--', color='r')
        #
        title = "Baseline {}-{}".format(i, j)
        plt.title(title, y=2.75)
        plt.show()
Example #47
0
def _fillweight(vis):
    if not os.path.exists(vis):
        return

    casalog.post('fill WEIGHT and SIGMA columns for %s ...'%(vis))

    # work with private cb tool
    with sdutil.cbmanager(vis, compress=False, addcorr=False, addmodel=False) as cb:
        status = cb.initweights()

    if status:
        # cb.initweights() succeeded so try to apply Tsys to
        # weight column
        # procedure:
        # 1. generate temporary Tsys caltable
        # 2. apply temporary Tsys caltable to vis
        # 3. remove temporary Tsys caltable
        import time
        from gencal import gencal
        from applycal import applycal
        caltable = 'temporary_caltable%s.tsys'%(str(time.time()).replace('.',''))
        casalog.post('tempolary caltable name: %s'%(caltable))
        try:
            gencal(vis=vis, caltable=caltable, caltype='tsys')
            # add 0.5*INTERVAL to the TIME values in caltable to make them time of
            # integration midpoint, because gencal currently sets (TIME_VIS -
            # INTERVAL_VIS/2), namely start time, into TIME in its output based on
            # ALMA's conventions...(2014/6/17 WK)
            with sdutil.tbmanager(caltable, nomodify=False) as tbcal:
                with sdutil.tbmanager(vis) as tbvis:
                    interval = tbvis.getcol('INTERVAL')[0]
                tbcal.putcol('TIME', tbcal.getcol('TIME') + 0.5*interval)
            applycal(vis=vis, docallib=False, gaintable=[caltable], applymode='calonly')
        except Exception, e:
            # Tsys application failed so that reset WEIGHT and SIGMA to 1.0
            _resetweight(vis)
            raise e
        finally:
 def run(self):
     shape = (2, len(self.antennas2))
     flags = np.zeros(shape, np.bool)
     delays = np.zeros(shape, np.float)
     phases = np.zeros(shape, np.float)
     rates = np.zeros(shape, np.float)
     sigs = []
     ref_freqs = utils.get_reference_freqs(self.msname)
     ref_freq_diffs = ref_freqs - ref_freqs[0]
     for scan in self.scans:
         if self.solint is None:
             solint = utils.get_scan_length(self.msname, scan)
         else:
             solint = self.solint
         scanq = self.make_time_q_from_scan(scan)
         solintqs = ffd.divide_up_timerange(self.msname, scanq, solint,
                                            self.solmin, self.solsub, self.dofloat)
         for timeq in solintqs:
             timeq2 = ffd.actual_timerangeq(self.msname, timeq)
             for pi, polind in enumerate(self.polinds):
                 casalog.post("Getting data")
                 anffd = ffd.FFData.make_FFD_multiband(self.msname, self.antennas2, self.pol_id,
                                                       polind, timeq2, datacol="CORRECTED_DATA",
                                                       solint=self.solint)
                 self.anffd = anffd # so developers can poke it offline.
                 casalog.post("Fitting fringes")
                 self.t = fringer.fit_fringe_ffd(anffd, self.ref_antenna, self.antennas2, pad=self.pad)
                 fs, dels, phs, rs, sig = self.t
                 flags[pi, :] = fs
                 delays[pi, :] = dels
                 phases[pi, :] = phs
                 rates[pi, :] = rs
                 sigs.append(sig)
             for swid in self.spectral_windows:
                 diffs = 2*np.pi*((ref_freq_diffs[swid]*delays) % 1.0)
                 ph = phases + diffs
                 # casalog.post("phases {} diffs {}".format(phases, diffs))
                 self.write_table(anffd, timeq2, flags, swid, delays, ph, rates, sigs)
Example #49
0
def get_mask(regions, hdu):

    ww = wcs.WCS(hdu.header).celestial

    reg_pix = [reg.to_pixel(ww) for reg in regions]

    reg_masks = [reg.to_mask() for reg in reg_pix]

    mask = np.zeros(hdu.data.shape[-2:], dtype='int16')

    assert mask.size > 1

    for mm, reg in zip(reg_masks, regions):
        if mm.cutout(mask) is None:
            #casalog.post("Skipped region {0}".format(str(reg)), origin='get_mask')
            continue
        else:
            #nmask = mask.sum()
            if hasattr(mm, '_to_image_partial_overlap'):
                mm._to_image_partial_overlap(mask)
            elif hasattr(mm, 'to_image_partial_overlap'):
                mm.to_image_partial_overlap(mask)
            else:
                raise ValueError("Something is wrong with the regions build")
            #nmask_after = mask.sum()
            #casalog.post("For region {0}, mask went from {1} to {2}"
            #             .format(str(reg), nmask, nmask_after),
            #             origin='get_mask',
            #            )

    nmask_after = mask.sum()
    casalog.post(
        "Final mask in get_mask has {0} of {1} pixels included ({2}%)".format(
            nmask_after, mask.size, nmask_after / float(mask.size) * 100),
        origin='get_mask',
    )

    return mask
	def normalize(self):
		self.tb.open(self.caltable,nomodify=False)
		cparam=self.tb.getcol('CPARAM')
		paramerr=self.tb.getcol('PARAMERR')
		flags=self.tb.getcol('FLAG')
		if(self.usemedian):
			meanfunc=np.median
		else:
			meanfunc=np.mean
		if(self.combinecorrnorm):
			globalVal=meanfunc(np.abs(cparam[np.logical_not(flags)]))
			casalog.post("Central value of gains: %.3f"%globalVal)
			cparam/=globalVal
			paramerr/=globalVal
		else:
			for icorr in range(0,self.nCorr):
				globalVal=meanfunc(np.abs(cparam[icorr,:,:][np.logical_not(flags[icorr,:,:])]))
				cparam[icorr,:,:]/=globalVal
				paramerr[icorr,:,:]/=globalVal
		self.tb.putcol('CPARAM',cparam)
		self.tb.putcol('PARAMERR',paramerr)
		self.tb.putcol('FLAG',flags)
		self.tb.close()
Example #51
0
    def calc_e_snr(self, i, j):
        """Calculate baseline signal to noise ration using the formula from
AIPS FRING.FOR lines 4162 t/m 4163.

I have literally no idea where the tangents and powers come from, sorry."""
        d = self.data[i, j]
        w = self.snr_weights[i, j]
        d.count(axis=0)
        xcount = d.count()
        sumw = np.sum(w)
        sumww = np.sum(w * w)
        work1 = self.get_fringe_peak_size(i, j)
        # casalog.post("Baseline {}-{}".format(i,j), "DEBUG")
        # casalog.post("work1 {}\nxcount {}\nsumw {}\nsumww {}".format(
        #     work1, xcount, sumw, sumww), "DEBUG")
        if xcount == 0:
            casalog.post("Baseline (matrix indices) {}-{} has no data".format(
                i, j))
            cwt = 0
        else:
            cwt = ((math.tan(math.pi / 2 * work1 / sumw)**1.163) *
                   math.sqrt(sumw / math.sqrt(sumww / xcount)))
        return cwt
def mbsubtractbaseline(method='median', label=''):
    depth = [s[3] for s in inspect.stack()].index('<module>')
    mbglobals = sys._getframe(depth).f_globals
    taskname = sys._getframe().f_code.co_name
    logger.origin(taskname)

    mbsc = mbglobals['__mbscans__'][label]
    sc_old = mbsc['data'].data

    logger.post('method: {0}'.format(method))
    try:
        fr_baseline = getattr(np, method)(sc_old, axis=0)
        sc_new = sc_old - fr_baseline
        logger.post('baseline is successfully estimated')
    except:
        logger.post('an error occured', 'ERROR')

    mbsc['data'].data = sc_new
    mbsc.recordtask(taskname)
    mbglobals['__mbscans__'].append(mbsc)

    logger.post('baseline is successfully subtracted from MBScan')
    logger.post('new MBScan is now stored as __mbscans__[0]')
Example #53
0
    def getGains(self, solver, accumd, accummodel, accumwt, accumfl, goodbl):

        if (accumd.shape[2] != self.gains.shape[1]):
            print("Sub-selection of channels not supported in bandpassR")
            self.error = True
            return
        if (self.dividebychanzero):
            accumd, accumfl = self.dividechanzero(
                accumd, accumfl)  #for divide by chanzero
        nsamples = self.solintMap[self.ispw][self.isol]
        nchanwt = accumwt.shape[2]
        for ichan in range(0, self.nChan):
            if (self.debug):
                casalog.post('DEBUG: solving channel %d' % ichan)

            for icorr in range(0, self.nCorr):
                if (nchanwt == 1):
                    thiswt = accumwt[:nsamples, goodbl, :, icorr]
                else:
                    thiswt = accumwt[:nsamples, goodbl, ichan:ichan + 1, icorr]
                self.gains[icorr, ichan, :], self.gains_er[
                    icorr,
                    ichan, :], self.antFlags[icorr, ichan, :] = solver.solve(
                        accumd[:nsamples, goodbl, ichan:ichan + 1, icorr],
                        accummodel[:nsamples, goodbl, ichan:ichan + 1, icorr],
                        accumfl[:nsamples, goodbl, ichan:ichan + 1,
                                icorr], thiswt)
                if (not self.antFlags[icorr, ichan, self.refant]):
                    casalog.post("change in refant")
                    self.gains[icorr, ichan, :] = self.gains[
                        icorr, ichan, :] * np.exp(1j * np.angle(
                            self.gainsOld[icorr, ichan, solver.refant]))
                    solver.refant = self.refant
        if (self.solnorm):
            self.normalizeGains()
        if (self.debug):
            print ''
Example #54
0
    def dividechanzero(self, accumd, accumfl):
        samples = self.solintMap[self.ispw][self.isol]
        if (self.debug):
            casalog.post("DEBUG: chanzerorange: %d,%d" %
                         (self.chanzerorange[0], self.chanzerorange[1]))
        chanzero = accumd[:samples, :,
                          self.chanzerorange[0]:self.chanzerorange[1], :]
        chanzeroflags = accumfl[:samples, :,
                                self.chanzerorange[0]:self.chanzerorange[1], :]

        chanzero = self.centralValue(
            data=accumd[:samples, :,
                        self.chanzerorange[0]:self.chanzerorange[1], :],
            flags=chanzeroflags,
            axis=2,
            keepdims=True)
        chanzeroflags = np.mean(chanzeroflags, axis=2, keepdims=True)
        chanzeroflags = chanzeroflags > 0.001
        if (self.preaverage):
            chanzero = self.centralValue(data=chanzero,
                                         flags=chanzeroflags,
                                         axis=3,
                                         keepdims=True)

        if (self.normamp and self.zerophase):
            accumd[:samples] /= chanzero
        elif (self.normamp):
            accumd[:samples] /= np.abs(chanzero)
        elif (self.normphase):
            chanzero /= np.abs(chanzero)
            accumd[:samples] /= chanzero
        accumfl[np.isnan(accumd)] = False
        accumfl[np.isinf(accumd)] = False
        accumd[np.isnan(accumd)] = 0.0
        accumd[np.isinf(accumd)] = 0.0
        return accumd, accumfl
        '''
Example #55
0
    def __init__(self, nh, nv, brightnessunit, direction_label, direction_reference, 
                 spectral_label, spectral_unit, ticksize, title='', 
                 separatepanel=True, 
                 showaxislabel=False, showtick=False, showticklabel=False,
                 figsize=None,
                 clearpanel=True):
        self.nh = nh
        self.nv = nv
        self.ticksize = ticksize
        self.brightnessunit = brightnessunit
        self.numeric_formatter = pl.FormatStrFormatter('%.2f')
        self.direction_label = direction_label
        self.direction_reference = direction_reference
        self.separatepanel = separatepanel
        self.spectral_label = spectral_label
        self.spectral_unit = spectral_unit
        self.showaxislabel = showaxislabel
        self.showtick = showtick
        self.showticklabel = showticklabel
        self.title = title
        self.figsize = figsize
        casalog.post('figsize={figsize}'.format(figsize=self.figsize), priority='DEBUG')
        
        self.normalization_factor = 1
        
        self._axes_spmap = None

        # to resize matplotlib window to specified size        
        pl.figure(self.MATPLOTLIB_FIGURE_ID)
        pl.close()
        
        if self.figsize is None:
            pl.figure(self.MATPLOTLIB_FIGURE_ID)
        else:
            pl.figure(self.MATPLOTLIB_FIGURE_ID, figsize=self.figsize)
        if clearpanel:
            pl.clf()
Example #56
0
        def check_job(self, jobId, verbose=True):
            """Check the status of a non-blocking job"""

            jobId_list = list(jobId)
            command_response_list = self.__command_client.get_command_response(
                jobId_list, block=False, verbose=verbose)

            # Aggregate exceptions and completed jobIds
            error_msg = ''
            completed_jobs = []
            for command_response in command_response_list:
                if not command_response['successful']:
                    if len(error_msg) > 0:
                        error_msg += "\n"

                    error_msg += "Exception executing command in server %s: %s" % (
                        command_response['server'],
                        command_response['traceback'])
                else:
                    completed_jobs.append(command_response['id'])

            # Re-throw aggregated exception
            if len(error_msg) > 0:
                casalog.post(error_msg, "SEVERE",
                             "MPIInterfaceCore::check_job")
                raise Exception(error_msg)

            # Check that all jobs have been completed
            completed = True
            for jobId in jobId_list:
                if jobId not in completed_jobs:
                    completed = False
                    break

            # Return completion status
            return completed
Example #57
0
    def __init__(self,start_services=False):
        """ Create MPICommandServer singleton instance """
        
        casalog_call_origin = "MPICommandServer::__init__"
        
        # Check if MPI is effectively enabled
        if not MPIEnvironment.is_mpi_enabled:
            msg = "MPI is not enabled"
            casalog.post(msg,"SEVERE",casalog_call_origin)
            raise Exception,msg
        
        # Check if MPIMonitorServer can be instantiated
        if MPIEnvironment.is_mpi_client:
            msg = "MPICommandServer cannot be instantiated at master MPI process"
            casalog.post(msg,"SEVERE",casalog_call_origin)
            raise Exception,msg          
        
        # Check whether we already have a MPICommandServer singleton instance
        if MPICommandServer.__instance is None:
            # Create MPICommandServer singleton instance
            MPICommandServer.__instance = MPICommandServer.__MPICommandServerImpl(start_services=start_services)

        # Store MPICommandServer singleton instance reference as the only member in the handle
        self.__dict__['_MPICommandServer__instance'] = MPICommandServer.__instance
Example #58
0
def plotuv(vis=None, field=None, antenna=None, spw=None, observation=None, array=None,
           maxnpts=None, colors=None, symb=None, ncycles=None, figfile=None):
    """
    Plots the uv coverage of vis in klambda.  ncycles of colors will be
    allocated to representative wavelengths.

    colors: a list of matplotlib color codes.
    symb: One of matplotlib's codes for plot symbols: .:,o^v<>s+xDd234hH|_
          default: ',':  The smallest points I could find.
    maxnpts: Save memory and/or screen space by plotting a maximum of maxnpts
             (or all of them if maxnpts < 1).  There is a very sharp
             slowdown if the plotter starts swapping.
    spw: spw selection string (ignores channel specifications for now).
    field: field selection string (for now, only 1 field will be plotted).
    antenna: antenna selection string (currently ignored).
    """
    casalog.origin('plotuv')
    try:
        uvplotinfo = UVPlotInfo(vis, spw, field, antenna, observation, array,
                                ncycles, colors, symb, figfile, maxnpts)
    except Exception, e:
        casalog.post("Error plotting the UVWs of %s:" % vis, 'SEVERE')
        casalog.post("%s" % e, 'SEVERE')
        return False
Example #59
0
def delmod(vis=None, otf=None, field=None, scr=None):

    casalog.origin('delmod')

    # Do the trivial parallelization
    if ParallelTaskHelper.isParallelMS(vis):
        helper = ParallelTaskHelper('delmod', locals())
        helper.go()
        return

    #Python script
    try:

        # only if vis exists...
        if ((type(vis) == str) & (os.path.exists(vis))):
            # ... and we are asked to do something...
            # open without adding anything!
            _cb.open(vis, addcorr=False, addmodel=False)
            _cb.delmod(otf=otf, field=field, scr=scr)
            _cb.close()
        else:
            raise Exception, 'Visibility data set not found - please verify the name'

        # Write history to MS
        try:
            param_names = delmod.func_code.co_varnames[:delmod.func_code.
                                                       co_argcount]
            param_vals = [eval(p) for p in param_names]
            write_history(mstool(), vis, 'delmod', param_names, param_vals,
                          casalog)
        except Exception, instance:
            casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
                         'WARN')

    except Exception, instance:
        print '*** Error ***', instance
Example #60
0
    def __init__(self):
        """ Create MPIInterface singleton instance """

        casalog_call_origin = "MPIInterface::__init__"

        # Check if MPI is effectively enabled
        if not MPIEnvironment.is_mpi_enabled:
            msg = "MPI is not enabled"
            casalog.post(msg, "SEVERE", casalog_call_origin)
            raise Exception(msg)

        # Check if MPIInterface can be instantiated
        if not MPIEnvironment.is_mpi_client:
            msg = "MPIInterface can only be instantiated at master MPI process"
            casalog.post(msg, "SEVERE", casalog_call_origin)
            raise Exception(msg)

        # Check whether we already have a MPIInterface singleton instance
        if MPIInterface.__instance is None:
            # Create MPIInterface singleton instance
            MPIInterface.__instance = MPIInterface.__MPIInterfaceImpl()

        # Store MPIInterface singleton instance reference as the only member in the handle
        self.__dict__['_MPIInterface__instance'] = MPIInterface.__instance