casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_ft.py
Go to the documentation of this file.
00001 import os
00002 from taskinit import *
00003 
00004 def ft(vis=None,field=None,spw=None,model=None,nterms=None,reffreq=None,complist=None,incremental=None, usescratch=None):
00005        """ Insert a source model into the MODEL_DATA column of a visibility set:
00006 
00007        A source model (souce.model image) or components list is converted into a
00008        model visibility that is inserted into the MODEL_DATA column.  This is
00009        needed to use resolved source in gaincal and in fluxscale.  (Setjy will
00010        automatically make this ft step.)
00011 
00012        The sources currently available are 3C48, 3C138, 3C147, 3C286
00013        at 1.4, 5.0, 8.4, 15, 22, 43 GHz.  Their location is site
00014        dependent.  In Charlottesville and at the AOC, the models are
00015        in /usr/lib/casapy/data/nrao/VLA/CalModels.
00016 
00017        Keyword arguments:
00018        vis -- Name of input visibility file
00019                default: none; example: vis='ngc5921.ms'
00020        field -- Field name list
00021                default: '' ==> all
00022                NOTE: each source must be specified in a multi-source vis.
00023                field = '1328+307'  specifies source '1328+307'
00024                field = '4' specified field with index 4
00025        spw -- Spw selection
00026                default: spw = '' (all spw)
00027        model -- Name of input model image
00028                default: None;
00029                example: model='/usr/lib/casapy/data/nrao/VLA/CalModels/3C286_X.im'
00030                Note: The model visibilities are scaled from the model frequency
00031                      to the observed frequency of the data.
00032        nterms -- Number of terms used to model the sky frequency dependence
00033                  default: 1
00034                  example : nterms=3  represents a 2nd order Taylor-polynomial in frequency
00035                            and is to be used along with 3 model-image names. 
00036                            model=['xxx.image.tt0','xxx.image.tt1', 'xxx.image.tt2']
00037           reffreq -- Reference-frequency about which this Taylor-expansion is defined.
00038        complist -- Name of component list
00039                default: None; ; example: complist='test.cl'
00040                components tool not yet available
00041        incremental -- Add model visibility to the existing MODEL_DATA visibilties
00042                default: False; example: incremental=True
00043 
00044        """
00045        casalog.origin('ft')
00046 
00047        #Python script
00048        try:
00049                # Check if datafile exists and open it
00050                if ((type(vis)==str) & (os.path.exists(vis))):
00051                        im.open(vis, usescratch=usescratch)
00052                else:
00053                        raise Exception, 'Visibility data set not found - please verify the name'
00054         
00055                # Select data
00056                im.selectvis(field=field,spw=spw)
00057                
00058                # Define image co-ordinates (all defaults)
00059                im.defineimage()
00060 
00061                # Check 'model'. The 'xml' allows a variant => do the checking here.
00062                if( (not type(model)==str) and (not (type(model)==list) ) ) :
00063                        raise Exception, 'The model image must be a string or a list of strings (or \'\' or [])';
00064 
00065                # If model is a single string, make it a list
00066                if( type(model)==str ):
00067                        model = [model];
00068 
00069                # Check that either a model or a complist has been given.
00070                if( (model==[] or model==['']) and complist=='' ):
00071                        raise Exception, 'Please specify a model image or component list to ft';
00072 
00073                #model is a list now. Check that all elements are strings. If so, check file existence too.
00074                if( type(model)==list ):
00075                        for onemodel in model:
00076                               if(not type(onemodel)==str):
00077                                     raise Exception, 'Model image names must be strings';
00078                               if( (not onemodel=='') and (not os.path.exists(onemodel)) ):
00079                                     raise Exception, 'Model image '+onemodel+' cannot be found';
00080 
00081                # Check complist : one string : name of complist file. Check existance on disk.
00082                if( (not complist=='') and (not os.path.exists(complist)) ):
00083                        raise Exception, 'Componentlist '+complist+' cannot be found';
00084 
00085 
00086                # If nterms>1, then check that len(model)=nterms [ no multifield for now ]
00087                # Call im.settaylorterms()
00088                #               
00089                if (nterms > 1) :
00090                        if(type(model)==str or (not (type(model)==list and len(model)==nterms)) ):
00091                                raise Exception, 'For nterms>1, please provide a list of nterms model-image names';
00092                        # parse the reference-frequency field.
00093                        qat=qatool();
00094                        try:
00095                           rff=qat.canonical(reffreq);
00096                        except Exception, instance:
00097                           print '*** Error *** In conversion of reffreq=\'',reffreq,'\' to a numerical value';
00098                           raise Exception, instance
00099                        reffreqVal=rff['value'];  # This is the frequency in Hz
00100                        if(reffreqVal==0.0):   # if unspecified, set the default from the model image
00101                                ia.open(model[0]);
00102                                icsys = ia.coordsys();
00103                                ia.close();
00104                                reffreqVal=icsys.referencevalue(type='spectral')['numeric'][0];
00105                                casalog.post('Using reference frequency from model image : '+str(reffreqVal)+' Hz');
00106                        else:
00107                                casalog.post('Using reference frequency : '+str(reffreqVal)+' Hz');
00108                        # set nterms and ref-freq
00109                        im.settaylorterms(ntaylorterms=nterms,reffreq=reffreqVal)
00110 
00111                # Just checking...
00112                if (nterms < 1) :
00113                        raise Exception, 'nterms must be greater than or equal to 1';
00114 
00115 
00116                # Do the forward transform and close.
00117                im.ft(model=model,complist=complist,incremental=incremental)
00118                im.close()
00119 
00120 
00121                #write history
00122                ms.open(vis,nomodify=False)
00123                ms.writehistory(message='taskname = ft',origin='ft')
00124                ms.writehistory(message='vis         = "'+str(vis)+'"',origin='ft')
00125                ms.writehistory(message='field       = "'+str(field)+'"',origin='ft')
00126                ms.writehistory(message='spw         = "'+str(spw)+'"',origin='ft')
00127                ms.writehistory(message='model       = "'+str(model)+'"',origin='ft')
00128                ms.writehistory(message='complist    = "'+str(complist)+'"',origin='ft')
00129                ms.writehistory(message='incremental = "'+str(incremental)+'"',origin='ft')
00130                ms.close()
00131 
00132        except Exception, instance:
00133                print '*** Error ***',instance
00134                raise Exception, instance