casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_viewer.py
Go to the documentation of this file.
00001 import sys
00002 import os
00003 import inspect
00004 import string
00005 import time
00006 from taskinit import *
00007 import viewertool
00008 
00009 
00010 class __viewer_class(object):
00011         "viewer() task with local state for created viewer tool"
00012 
00013         def __init__( self ):
00014                 self.local_vi = None
00015                 self.local_ving = None
00016 
00017         def __call__(self, infile=None,displaytype=None,channel=None,zoom=None,outfile=None,outscale=None,outdpi=None,outformat=None,outlandscape=None,gui=None):
00018                 """ The viewer will display images in raster, contour, vector or
00019                 marker form.  Images can be blinked, and movies are available
00020                 for spectral-line image cubes.  For measurement sets, many
00021                 display and editing options are available.
00022 
00023                 examples of usage:
00024 
00025                 viewer
00026                 viewer "myimage.im"
00027                 viewer "mymeasurementset.ms"
00028                 viewer "myrestorefile.rstr"
00029                 
00030                 viewer "myimage.im", "contour"
00031 
00032                 viewer "'myimage1.im' - 2 * 'myimage2.im'", "lel"
00033         
00034                 The viewer can be run outside of casapy by typing <casaviewer>.
00035 
00036                 Executing viewer <viewer> will bring up a display panel
00037                 window, which can be resized.  If no data file was specified,
00038                 a Load Data window will also appear.  Click on the desired data
00039                 file and choose the display type; the rendered data should appear
00040                 on the display panel.
00041 
00042                 A Data Display Options window will also appear.  It has drop-down
00043                 subsections for related options, most of which are self-explanatory.
00044           
00045                 The state of the viewer -- loaded data and related display
00046                 options -- can be saved in a 'restore' file for later use.
00047                 You can provide the restore filename on the command line or
00048                 select it from the Load Data window.
00049 
00050                 See the cookbook for more details on using the viewer.
00051         
00052                 Keyword arguments:
00053                 infile -- Name of file to visualize
00054                         default: ''
00055                         example: infile='ngc5921.image'
00056                         If no infile is specified the Load Data window
00057                         will appear for selecting data.
00058                 displaytype -- (optional): method of rendering data
00059                         visually (raster, contour, vector or marker).  
00060                         You can also set this parameter to 'lel' and
00061                         provide an lel expression for infile (advanced).
00062                         default: 'raster'
00063                         example: displaytype='contour'
00064 
00065                 Note: there is no longer a filetype parameter; typing of
00066                 data files is now done automatically.
00067                         example:  viewer infile='my.ms'
00068                         obsolete: viewer infile='my.ms', filetype='ms'
00069 
00070 
00071                 """
00072                 a=inspect.stack()
00073                 stacklevel=0
00074                 for k in range(len(a)):
00075                         if a[k][1] == "<string>" or (string.find(a[k][1], 'ipython console') > 0 or string.find(a[k][1],"casapy.py") > 0):
00076                                 stacklevel=k
00077 
00078                 myf=sys._getframe(stacklevel).f_globals
00079 
00080                 #Python script
00081                 try:
00082                         ## vi might not be defined in taskinit if loading
00083                         ## directly from python via casa.py...
00084                         vwr = vi
00085                         if type(gui) == bool and gui == False:
00086                                 vwr = ving
00087 
00088                         if type(vwr.cwd( )) != str:
00089                                 vwr = None
00090                 except:
00091                         vwr = None
00092 
00093                 if type(vwr) == type(None):
00094                         need_gui = True
00095                         if type(gui) == bool and gui == False:
00096                                 need_gui = False
00097 
00098                         if need_gui :
00099                                 if self.local_vi is not None:
00100                                         vwr = self.local_vi
00101                                 else:
00102                                         vwr = viewertool.viewertool( True, True, (type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == type(os)) )
00103                                         self.local_vi = vwr
00104                         else:
00105                                 if self.local_ving is not None:
00106                                         vwr = self.local_ving
00107                                 else:
00108                                         vwr = viewertool.viewertool( False, True, (type(myf) == dict and myf.has_key('casa') and type(myf['casa']) == type(os)) )
00109                                         self.local_ving = vwr
00110 
00111                 if type(vwr) != type(None) :
00112                         ##
00113                         ## (1) save current *viewer*server* path
00114                         ## (2) have viewer() task follow casapy/python's cwd
00115                         try:
00116                                 old_path = vwr.cwd( )
00117                         except:
00118                                 raise Exception, "viewer() failed to get the current working directory [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]"
00119 
00120                         try:
00121                                 vwr.cwd(os.path.abspath(os.curdir))
00122                         except:
00123                                 raise Exception, "viewer() failed to change to the new working directory (" + os.path.abspath(os.curdir) + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]"
00124 
00125                         panel = vwr.panel("viewer")
00126                         data = None
00127                         if type(infile) == str and len(infile) > 0 :
00128                                 if type(displaytype) == str:
00129                                         data = vwr.load( infile, displaytype, panel=panel )
00130                                 else:
00131                                         data = vwr.load( infile, panel=panel )
00132 
00133                                 if type(channel) == int and channel > 0 :
00134                                         vwr.channel(channel,panel=panel)
00135                                 if type(zoom) == int and zoom != 1 :
00136                                         vwr.zoom(zoom,panel=panel)
00137                                 if type(outfile) == str and len(outfile) > 0 :
00138                                         scale=1.0
00139                                         if type(outscale) == float :
00140                                                 scale=outscale
00141                                         dpi=300
00142                                         if type(outdpi) == int :
00143                                                 dpi=outdpi
00144                                         format="jpg"
00145                                         if type(outformat) == str :
00146                                                 format=outformat
00147                                         orientation="portrait"
00148                                         if type(outlandscape) == bool and outlandscape :
00149                                                 orientation="landscape"
00150                                         vwr.output(outfile,scale=scale,dpi=dpi,format=format,orientation=orientation,panel=panel)
00151                         else:
00152                                 vwr.popup( 'open', panel=panel )
00153 
00154 
00155                         # it makes no sense to leave a panel open with no way of interacting with it
00156                         if type(gui) == bool and not gui:
00157                                 vwr.close(panel)
00158 
00159                         ## (3) restore original path
00160                         try:
00161                                 vwr.cwd(old_path)
00162                         except:
00163                                 raise Exception, "viewer() failed to restore the old working directory (" + old_path + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]"
00164 
00165                 else:
00166                         viewer_path = myf['casa']['helpers']['viewer']   #### set in casapy.py
00167                         args = [ viewer_path ]
00168 
00169                         if type(infile) == str:
00170                                 if type(displaytype) == str:
00171                                         args += [ infile, displaytype ]
00172                                 else:
00173                                         args += [ infile ]
00174 
00175                         if (os.uname()[0]=='Darwin'):
00176                                 vwrpid=os.spawnvp( os.P_NOWAIT, viewer_path, args )
00177                         elif (os.uname()[0]=='Linux'):
00178                                 vwrpid=os.spawnlp( os.P_NOWAIT, viewer_path, *args )
00179                         else:
00180                                 print 'Unrecognized OS: No viewer available'
00181 
00182                 return None
00183 
00184 viewer = __viewer_class( )