casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_imregrid.py
Go to the documentation of this file.
00001 import os
00002 import shutil
00003 from taskinit import *
00004 
00005 def imregrid(imagename, template, output, asvelocity, axes, shape):
00006     try:
00007         casalog.origin('imregrid')
00008         if hasattr(template, 'lower') and not template.lower() == "get":
00009             # First check to see if the output file exists.  If it
00010             # does then we abort.  CASA doesn't allow files to be
00011             # over-written, just a policy.
00012             if len(output) < 1:
00013                 output = imagename + '.regridded'
00014                 casalog.post("output was not specified - defaulting to\n\t"
00015                      + output, 'INFO')
00016         if os.path.exists(output):
00017             raise Exception, 'Output destination ' + output + \
00018               " exists.\nPlease remove it or change the output file name."
00019         _myia = iatool()
00020 
00021         # Figure out what the user wants.
00022         if not isinstance(template, dict):
00023             if template.lower() == 'get':
00024                 _myia.open(imagename)
00025                 csys = _myia.coordsys().torecord()
00026                 shape = _myia.shape()
00027                 _myia.done()
00028                 return {'csys': csys, 'shap': shape}
00029             elif template.upper() in ('J2000', 'B1950', 'B1950_VLA',
00030                                       'GALACTIC', 'HADEC', 'AZEL',
00031                                       'AZELSW', 'AZELNE', 'ECLIPTIC',
00032                                       'MECLIPTIC', 'TECLIPTIC',
00033                                       'SUPERGAL'):       
00034                 _myia.open(imagename)
00035                 csys = _myia.coordsys().torecord()
00036                 shape = _myia.shape()
00037                 _myia.done()
00038 
00039                 newrefcode = template.upper()
00040                 oldrefcode = csys['direction0']['system']
00041                 if oldrefcode == newrefcode:
00042                     casalog.post(imagename + ' is already in ' + oldrefcode,
00043                                  'INFO')
00044                     casalog.post("...making a straight copy...", 'INFO')
00045                     shutil.copytree(imagename, output)
00046                     return True
00047                     
00048                 casalog.post("Changing coordinate system from " + oldrefcode
00049                              + " to " + newrefcode, 'INFO')
00050                 csys['direction0']['conversionSystem'] = newrefcode
00051                 csys['direction0']['system'] = newrefcode
00052                 refdir = me.direction(oldrefcode,
00053                                       {'unit': 'rad',
00054                                        'value': csys['direction0']['crval'][0]},
00055                                       {'unit': 'rad',
00056                                        'value': csys['direction0']['crval'][1]})
00057                 refdir = me.measure(refdir, newrefcode)
00058                 csys['direction0']['crval'][0] = refdir['m0']['value']
00059                 csys['direction0']['crval'][1] = refdir['m1']['value']
00060             else:                   # Don't use a template named 'get', people.
00061                 if not os.path.isdir(template) or not os.access(template,
00062                                                                 os.R_OK):
00063                     raise TypeError, 'Cannot read template image ' + template
00064 
00065                 _myia.open(template)
00066                 csys = _myia.coordsys().torecord()
00067                 if (len(shape) == 1 and shape[0] == -1):
00068                     _myia.open(imagename)
00069                     shape = _myia.shape()
00070                 _myia.done()
00071         else:
00072             csys = template['csys']
00073             shape = template['shap']
00074 
00075         # The actual regridding.
00076         _myia.open(imagename)
00077         _tmp = _myia.regrid(outfile=output, shape=shape, csys=csys, axes=axes, overwrite=True, asvelocity=asvelocity)
00078         _myia.done()
00079         _tmp.done()
00080         return True
00081         
00082     except Exception, instance:
00083         _myia.close()
00084         raise instance
00085         
00086