casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_imcontsub.py
Go to the documentation of this file.
00001 import os
00002 import re
00003 from taskinit import *
00004 
00005 from odict import odict
00006 
00007 def imcontsub(imagename=None,linefile=None,contfile=None,fitorder=None,region=None,box=None,chans=None,stokes=None,xstat=None):
00008 
00009     casalog.origin('imcontsub')
00010     filesExist=False
00011     if ( len( linefile ) > 0 ):
00012         if ( os.path.exists( linefile ) ):
00013             casalog.post('Error: file ' + linefile
00014                           +' already exists, please delete before continuing.',\
00015                           'SEVERE' )
00016             filesExist=True
00017     else:
00018         casalog.post("The linefile parameter is empty, consequently the"
00019                       +" spectral line image will NOT be\nsaved on disk.", \
00020                       'WARN')
00021             
00022     if ( len( contfile ) > 0 ):
00023             if ( os.path.exists( contfile ) ):
00024                 casalog.post( 'Error: Unable to continue file '+contfile\
00025                               +' already exists, please delete before continuing.',\
00026                           'SEVERE' )
00027                 filesExist=True
00028     else:
00029         casalog.post("The contfile parameter is empty, consequently the"
00030                       +" continuum image will NOT be\nsaved on disk.", \
00031                       'WARN')
00032     if ( filesExist ):
00033         return False
00034     
00035     _myia = iatool()
00036     _myia.open(imagename)
00037     mycsys = _myia.coordsys()
00038 
00039     if isinstance(box, list):
00040         box = ', '.join([str(b) for b in box])
00041 
00042     # Don't mix chans up with reg!  reg selects a subset for output, and chans
00043     # selects a subset to define the line-free channels.
00044     reg = rg.frombcs(csys=mycsys.torecord(), shape=_myia.shape(),
00045                      box=box, stokes=stokes, stokescontrol="f",
00046                      region=region)
00047 
00048     channels=[]
00049 
00050     try:
00051         # Find the max and min channel.
00052         axes=getimaxes(imagename)
00053         fullSize = _myia.boundingbox(_myia.setboxregion())
00054         minChan=int(fullSize['blc'][axes[2][0]])
00055         maxChan=int(fullSize['trc'][axes[2][0]])
00056 
00057 
00058         # Parse the channel information.
00059         tmpChan=[]
00060         if ( len(chans) > 0 ):
00061             tmpChans = re.split(r'[,;]', chans)
00062         else:
00063             tmpChans=[str(minChan)+"~"+str(maxChan)]
00064 
00065                         
00066         # Now make our list of strings into a list of integers.
00067         # In case someone has used ',' as the main separator we
00068         # split each element up on ','
00069         for tcs in tmpChans:
00070             channels.extend(_parse_chans(tcs, minChan, maxChan))
00071 
00072     except Exception, err:
00073         casalog.post( 'Error: Unable to parse the channel information. '+str(err),'SEVERE' )
00074         # Cleanup
00075         if ( reg != None ):
00076             del reg
00077         _myia.done()
00078         return  False
00079 
00080 
00081     try:
00082         # Now do the continuum subtraction.
00083         #ia.continuumsub( outline=linefile, outcont=contfile, region=reg, fitorder=fitorder, overwrite=False )
00084         _myia.continuumsub(outline=linefile, outcont=contfile, region=reg,
00085                            channels=channels, fitorder=fitorder, overwrite=False)
00086     
00087         # Cleanup
00088         if ( reg != None ):
00089             del reg
00090         _myia.done()
00091         return True
00092         #return retValue
00093                 
00094     except Exception, err:
00095         casalog.post( 'Error: Unable to perform continuum subtraction'+str(err), 'SEVERE' )
00096         # Cleanup
00097         if ( reg != None ):
00098             del reg
00099         _myia.done()
00100         return  False
00101         
00102     return True
00103 
00104 #
00105 #TODO add a try/catch block or type checking to make
00106 #sure all channel values are ints.
00107 def _parse_chans( chanString='', min=0, max=0 ):
00108     retValue=[]
00109     startChan=min;
00110     endChan=max;
00111 
00112     values=chanString.split('~')
00113     if ( len(values)==2 ):
00114         # We have a min and max value
00115         startChan=int(values[0])
00116         endChan=int(values[1])
00117     elif ( len(values)==1 ):
00118         # We probably have a <, <=, > or >= sign
00119         if ( values[0].startswith( '<=') ):
00120             endChan=int(values[0][2:])
00121         elif ( values[0].startswith( '<') ):
00122             endChan=int(values[0][1:])-1
00123         elif( values[0].startswith( '>=') ):
00124             startChan=int(values[0][2:])
00125         elif( values[0].startswith( '>') ):
00126             startChan=int(values[0][1:])+1
00127         elif( values[0]== '-1' ):
00128             startChan = min
00129             endChan   = max
00130         elif ( values.count( ',' ) > 0 ):
00131             # We have a list of specific channel numbers
00132             startChan=endChan=-1
00133             tmpList = chanString.split( ',' )
00134             for j in range( len( tmpList ) ):
00135                 retValue.append( int( tmpList[j] ) )
00136         else:
00137             # We have a single value
00138             startChan=int(values[0])
00139             endChan=int(values[0])
00140 
00141     if ( startChan >= 0 and endChan >= 0 and \
00142          startChan <= int(max) and endChan <= int(max) ):
00143         for i in range( startChan, endChan+1 ):
00144             retValue.append( i )
00145     else:
00146         raise Exception, "Invalid channel specification: "+str(values)
00147     
00148     return retValue