casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
exportuvfits_pg.py
Go to the documentation of this file.
00001 #
00002 # This file was generated using xslt from its XML file
00003 #
00004 # Copyright 2008, Associated Universities Inc., Washington DC
00005 #
00006 import sys
00007 import os
00008 from casac import *
00009 import string
00010 import time
00011 import inspect
00012 import gc
00013 import numpy
00014 from odict import odict
00015 from task_exportuvfits import exportuvfits
00016 from task_exportuvfits import casalog
00017 
00018 class exportuvfits_pg_:
00019     __name__ = "exportuvfits"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (exportuvfits_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, vis=None, fitsfile=None, datacolumn=None, field=None, spw=None, antenna=None, timerange=None, avgchan=None, writesyscal=None, multisource=None, combinespw=None, writestation=None, padwithflags=None, async=None):
00027 
00028         """Convert a CASA visibility data set to a UVFITS file:
00029 
00030         This task writes a UVFITS file, a general format data set used
00031         to transfer data between different software systems.  It is
00032         written in floating point format.  Different programs have different
00033         restrictions on what forms of UVFITS files they will use, especially
00034         whether they will accept multiple sources and/or spectral windows in
00035         the same file.  See the spw, multisource, and combinespw descriptions
00036         below.
00037 
00038         Keyword arguments:
00039         vis -- Name of input visibility file
00040                 default: none;  example: vis='ngc5921.ms'
00041         fitsfile -- Name of output UV FITS file
00042                 default: none;  example='3C273XC1.fits'
00043         datacolumn -- Visibility file data column
00044                 default: => 'corrected'; example: datacolumn='model'
00045                 Options: 'data' (raw),'corrected','model','weight'
00046         field -- Select field using field id(s) or field name(s).
00047                   [run listobs to obtain the list id's or names]
00048                default: ''=all fields
00049                If field string is a non-negative integer, it is assumed a field index
00050                otherwise, it is assumed a field name
00051                field='0~2'; field ids 0,1,2
00052                field='0,4,5~7'; field ids 0,4,5,6,7
00053                field='3C286,3C295'; field named 3C286 adn 3C295
00054                field = '3,4C*'; field id 3, all names starting with 4C
00055         spw -- Select spectral window/channels 
00056                  type 'help par.selection' for more examples.
00057                spw='0~2,4'; spectral windows 0,1,2,4 (all channels)
00058                spw='<2';  spectral windows less than 2 (i.e. 0,1)
00059                spw='0:5~61'; spw 0, channels 5 to 61, INCLUSIVE
00060                spw='*:5~61'; all spw with channels 5 to 62
00061                spw='0,10,3:3~45'; spw 0,10 all channels, spw 3, channels 3 to 45.
00062                spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each.
00063                spw='0:0~10;15~60'; spectral window 0 with channels 0-10,15-60
00064                          NOTE ';' to separate channel selections
00065                spw='0:0~10^2,1:20~30^5'; spw 0, channels 0,2,4,6,8,10,
00066                      spw 1, channels 20,25,30
00067         antenna -- Select data based on antenna/baseline
00068                default: '' (all)
00069                If antenna string is a non-negative integer, it is assumed
00070                   an antenna index
00071                  otherwise, it is assumed as an antenna name
00072                antenna='5&6'; baseline between antenna index 5 and index 6.
00073                antenna='VA05&VA06'; baseline between VLA antenna 5 and 6.
00074                antenna='5&6;7&8'; baseline 5-6 and 7-8
00075                antenna='5'; all baselines with antenna index 5
00076                antenna='05'; all baselines with antenna name '05', vla antenna
00077                antenna='5,6,10'; all baselines with antennas 5, 6 and 10
00078         timerange  -- Select data based on time range:
00079                default = '' (all); examples,
00080                timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
00081                Note: if YYYY/MM/DD is missing dat defaults to first day in data set
00082                timerange='09:14:0~09:54:0' picks 40 min on first day
00083                timerange='25:00:00~27:30:00' picks 1 hr to 3 hr 30min on next day
00084                timerange='09:44:00' data within one integration of time
00085                timerange='>10:24:00' data after this time
00086         avgchan -- Channel averaging width (value > 1 indicates averaging)
00087                 default =>1; example: avgchan=3
00088                 output data will average channels in groups of three.
00089         multisource -- Write in multi-source format
00090                 default: => True;
00091                    false if one source is selected
00092                 True works with AIPS, but not difmap.
00093         combinespw -- If True, export the spectral windows as IFs.
00094                       Otherwise multiple windows will use multiple FREQIDs.
00095                 default: =>  True;
00096                    all spectral windows must have same shape.
00097                 True is recommended for AIPS, and mandatory for difmap.
00098         padwithflags -- If True, and combinespw is True, fill in missing
00099                         data as needed to fit the IF structure.  This is
00100                         appropriate if the MS had a few frequency-dependent
00101                         flags applied, and was then time-averaged by split, or
00102                         when exporting for use by difmap.  If the spectral
00103                         windows were observed at different times,
00104                         padwithflags=True will add a large number of flags,
00105                         making the output file significantly longer.  It does
00106                         not yet support spectral windows with different widths.
00107         writestation -- Write station name instead of antenna name
00108                 default: True;
00109         writesyscal -- Write GC and TY tables
00110                 default: => False; Not yet available
00111         async --  Run asynchronously
00112                 default = False;
00113 
00114 
00115         """
00116         a=inspect.stack()
00117         stacklevel=0
00118         for k in range(len(a)):
00119           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00120                 stacklevel=k
00121                 break
00122         myf=sys._getframe(stacklevel).f_globals
00123         myf['__last_task'] = 'exportuvfits'
00124         myf['taskname'] = 'exportuvfits'
00125         ###
00126         myf['update_params'](func=myf['taskname'],printtext=False)
00127         ###
00128         ###
00129         #Handle globals or user over-ride of arguments
00130         #
00131         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00132         useLocalDefaults = False
00133 
00134         for item in function_signature_defaults.iteritems():
00135                 key,val = item
00136                 keyVal = eval(key)
00137                 if (keyVal == None):
00138                         #user hasn't set it - use global/default
00139                         pass
00140                 else:
00141                         #user has set it - use over-ride
00142                         if (key != 'self') :
00143                            useLocalDefaults = True
00144                         #myf[key]=keyVal
00145 
00146         myparams = {}
00147         if useLocalDefaults :
00148            for item in function_signature_defaults.iteritems():
00149                key,val = item
00150                keyVal = eval(key)
00151                exec('myparams[key] = keyVal')
00152                if (keyVal == None):
00153                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00154                    keyVal = eval(key)
00155                    if(type(keyVal) == dict) :
00156                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00157 
00158         else :
00159             uselessvariable = None 
00160             myparams['vis'] = vis = myf['vis']
00161             myparams['fitsfile'] = fitsfile = myf['fitsfile']
00162             myparams['datacolumn'] = datacolumn = myf['datacolumn']
00163             myparams['field'] = field = myf['field']
00164             myparams['spw'] = spw = myf['spw']
00165             myparams['antenna'] = antenna = myf['antenna']
00166             myparams['timerange'] = timerange = myf['timerange']
00167             myparams['avgchan'] = avgchan = myf['avgchan']
00168             myparams['writesyscal'] = writesyscal = myf['writesyscal']
00169             myparams['multisource'] = multisource = myf['multisource']
00170             myparams['combinespw'] = combinespw = myf['combinespw']
00171             myparams['writestation'] = writestation = myf['writestation']
00172             myparams['padwithflags'] = padwithflags = myf['padwithflags']
00173 
00174 
00175         result = None
00176 
00177 #
00178 #    The following is work around to avoid a bug with current python translation
00179 #
00180         mytmp = {}
00181 
00182         mytmp['vis'] = vis
00183         mytmp['fitsfile'] = fitsfile
00184         mytmp['datacolumn'] = datacolumn
00185         mytmp['field'] = field
00186         mytmp['spw'] = spw
00187         mytmp['antenna'] = antenna
00188         mytmp['timerange'] = timerange
00189         mytmp['avgchan'] = avgchan
00190         mytmp['writesyscal'] = writesyscal
00191         mytmp['multisource'] = multisource
00192         mytmp['combinespw'] = combinespw
00193         mytmp['writestation'] = writestation
00194         mytmp['padwithflags'] = padwithflags
00195         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00196         trec = casac.utils().torecord(pathname+'exportuvfits.xml')
00197 
00198         casalog.origin('exportuvfits')
00199         if not trec.has_key('exportuvfits') or not casac.utils().verify(mytmp, trec['exportuvfits']) :
00200             return False
00201 
00202 
00203         try :
00204           casalog.post('')
00205           casalog.post('##########################################')
00206           casalog.post('##### Begin Task: exportuvfits           #####')
00207           casalog.post('')
00208           result = exportuvfits(vis, fitsfile, datacolumn, field, spw, antenna, timerange, avgchan, writesyscal, multisource, combinespw, writestation, padwithflags)
00209           casalog.post('')
00210           casalog.post('##### End Task: exportuvfits           #####')
00211           casalog.post('##########################################')
00212 
00213 
00214 # saveinputs for individule engine has no use
00215 # saveinputs should alos be removed from casa_in_py.py
00216 #
00217 #
00218 #          saveinputs = myf['saveinputs']
00219 #          saveinputs('exportuvfits', 'exportuvfits.last', myparams)
00220 #
00221 #
00222         except Exception, instance:
00223           #print '**** Error **** ',instance
00224           pass
00225 
00226         gc.collect()
00227         return result
00228 #
00229 #
00230 ##
00231 #    def paramgui(self, useGlobals=True):
00232 #        """
00233 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00234 #        """
00235 #        import paramgui
00236 #
00237 #        a=inspect.stack()
00238 #        stacklevel=0
00239 #        for k in range(len(a)):
00240 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00241 #            stacklevel=k
00242 #            break
00243 #        myf = sys._getframe(stacklevel).f_globals
00244 #
00245 #        if useGlobals:
00246 #            paramgui.setGlobals(myf)
00247 #        else:
00248 #            paramgui.setGlobals({})
00249 #
00250 #        paramgui.runTask('exportuvfits', myf['_ip'])
00251 #        paramgui.setGlobals({})
00252 #
00253 #
00254 #
00255 #
00256     def defaults(self, param=None):
00257         a=inspect.stack()
00258         stacklevel=0
00259         for k in range(len(a)):
00260           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00261                 stacklevel=k
00262                 break
00263         myf=sys._getframe(stacklevel).f_globals
00264         a = odict()
00265         a['vis']  = ''
00266         a['fitsfile']  = ''
00267         a['datacolumn']  = 'corrected'
00268         a['field']  = ''
00269         a['spw']  = ''
00270         a['antenna']  = ''
00271         a['timerange']  = ''
00272         a['avgchan']  = 1
00273         a['writesyscal']  = False
00274         a['multisource']  = True
00275         a['combinespw']  = True
00276         a['writestation']  = True
00277 
00278         a['async']=False
00279         a['combinespw'] = {
00280                     0:odict([{'value':True}, {'padwithflags':True}]), 
00281                     1:{'value':False}}
00282 
00283 ### This function sets the default values but also will return the list of
00284 ### parameters or the default value of a given parameter
00285         if(param == None):
00286                 myf['__set_default_parameters'](a)
00287         elif(param == 'paramkeys'):
00288                 return a.keys()
00289         else:
00290                 if(a.has_key(param)):
00291                    #if(type(a[param]) == dict) :
00292                    #   return a[param][len(a[param])-1]['value']
00293                    #else :
00294                       return a[param]
00295 
00296 
00297 #
00298 #
00299     def check_params(self, param=None, value=None):
00300       a=inspect.stack() 
00301       stacklevel=0
00302       for k in range(len(a)):
00303         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00304             stacklevel=k
00305             break
00306       myf=sys._getframe(stacklevel).f_globals
00307 
00308 #      print 'param:', param, 'value:', value
00309       try :
00310          if str(type(value)) != "<type 'instance'>" :
00311             value0 = value
00312             value = myf['cu'].expandparam(param, value)
00313             matchtype = False
00314             if(type(value) == numpy.ndarray):
00315                if(type(value) == type(value0)):
00316                   myf[param] = value.tolist()
00317                else:
00318                   #print 'value:', value, 'value0:', value0
00319                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00320                   myf[param] = value0
00321                   if type(value0) != list :
00322                      matchtype = True
00323             else :
00324                myf[param] = value
00325             value = myf['cu'].verifyparam({param:value})
00326             if matchtype:
00327                value = False
00328       except Exception, instance:
00329          #ignore the exception and just return it unchecked
00330          myf[param] = value
00331       return value
00332 
00333 #
00334 #
00335     def description(self, key='exportuvfits', subkey=None):
00336         desc={'exportuvfits': 'Convert a CASA visibility data set to a UVFITS file:',
00337                'vis': 'Name of input visibility file',
00338                'fitsfile': 'Name of output UV FITS file',
00339                'datacolumn': 'Visibility file data column',
00340                'field': 'Select field using field id(s) or field name(s)',
00341                'spw': 'Select spectral window/channels',
00342                'antenna': 'Select data based on antenna/baseline',
00343                'timerange': 'Select data based on time range',
00344                'avgchan': 'Channel averaging width (value > 1 indicates averaging)',
00345                'writesyscal': 'Write GC and TY tables, (Not yet available)',
00346                'multisource': 'Write in multi-source format',
00347                'combinespw': 'Export the spectral windows as IFs',
00348                'writestation': 'Write station name instead of antenna name',
00349                'padwithflags': 'Fill in missing data with flags to fit IFs',
00350 
00351                'async': 'If true the taskname must be started using exportuvfits(...)'
00352               }
00353 
00354 #
00355 # Set subfields defaults if needed
00356 #
00357 
00358         if(desc.has_key(key)) :
00359            return desc[key]
00360 
00361     def itsdefault(self, paramname) :
00362         a = {}
00363         a['vis']  = ''
00364         a['fitsfile']  = ''
00365         a['datacolumn']  = 'corrected'
00366         a['field']  = ''
00367         a['spw']  = ''
00368         a['antenna']  = ''
00369         a['timerange']  = ''
00370         a['avgchan']  = 1
00371         a['writesyscal']  = False
00372         a['multisource']  = True
00373         a['combinespw']  = True
00374         a['writestation']  = True
00375         a['padwithflags']  = False
00376 
00377         if a.has_key(paramname) :
00378               return a[paramname]
00379 exportuvfits_pg = exportuvfits_pg_()