casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_imhead.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 #######################################################################3
00003 #  task_imhead.py
00004 #
00005 #
00006 # Copyright (C) 2008
00007 # Associated Universities, Inc. Washington DC, USA.
00008 #
00009 # This library is free software; you can redistribute it and/or modify it
00010 # under the terms of the GNU Library General Public License as published by
00011 # the Free Software Foundation; either version 2 of the License, or (at your
00012 # option) any later version.
00013 #
00014 # This library is distributed in the hope that it will be useful, but WITHOUT
00015 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00017 # License for more details.
00018 #
00019 # You should have received a copy of the GNU Library General Public License
00020 # along with this library; if not, write to the Free Software Foundation,
00021 # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00022 #
00023 # Correspondence concerning AIPS++ should be adressed as follows:
00024 #        Internet email: aips2-request@nrao.edu.
00025 #        Postal address: AIPS++ Project Office
00026 #                        National Radio Astronomy Observatory
00027 #                        520 Edgemont Road
00028 #                        Charlottesville, VA 22903-2475 USA
00029 #
00030 # <author>
00031 # Shannon Jaeger (University of Calgary)
00032 # </author>
00033 #
00034 # <summary>
00035 # CASA task for reading/writing/listing the CASA Image header
00036 # contents
00037 # </summary>
00038 #
00039 # <reviewed reviwer="" date="" tests="" demos="">
00040 # </reviewed
00041 #
00042 # <etymology>
00043 # imhead stands for image header
00044 # </etymology>
00045 #
00046 # <synopsis>
00047 # task_imhead.py is a Python script providing an easy to use task
00048 # for adding, removing, listing and updating the contents of a CASA
00049 # image.  This task is very useful for fixing mistakes made in the
00050 # importing of FITS files into CASA images, as well as seeing what
00051 # checking the header to see what type of data is in the image file.
00052 #
00053 # NOTE: This task does not edit FITS files, but FITS files may
00054 #       be created with exportuvfits task
00055 #
00056 # </synopsis>
00057 #
00058 # <example>
00059 # <srcblock>
00060 ## The following code lists the keyword/value pairs found in
00061 ## the header of the CASA image file ngc133.clean.image.  The information
00062 ## is stored in Python variable hdr_info in a Python dictionary.
00063 ## The information is also listed in the CASA logger.  The observation
00064 ## date is #printed out from the hdr_info variable.
00065 # hdr_info = imhead( 'ngc4826.clean.image', 'list' )
00066 ##print "Observation Date: ", hdr_info['date-obs']
00067 #
00068 ## The following exmple displays the CASA images history in the CASA logger.
00069 # imhead( 'ngc4826.clean.image', 'history' )
00070 #
00071 ## The following example adds a new, user-defined keyword to the
00072 ## CASA image ngc4826.clean.image
00073 # imhead( 'ngc4826.clean.image', 'add', 'observer 1', 'Joe Smith'  )
00074 # imhead( 'ngc4826.clean.image', 'add', 'observer 2', 'Daniel Boulanger'  )
00075 #
00076 ## The following example changes the name of the observer keyword,
00077 ## OBSERVE, to ALMA
00078 # imhead( 'ngc4826.clean.image', 'put', 'telescope', 'ALMA' )
00079 # </srblock>
00080 # </example>
00081 #
00082 # <motivation>
00083 # To provide headering modification and reading tools to the CASA users.
00084 # </motivation>
00085 #
00086 # <todo>
00087 # </todo>
00088 
00089 import numpy
00090 import os
00091 
00092 from taskinit import *
00093 
00094 not_known = ' Not Known '
00095 
00096 
00097 # TODO Holy moly is this code screaming to be refactored.
00098 
00099 
00100 def imhead(
00101     imagename, mode, hdkey, hdvalue,
00102     hdtype, hdcomment, verbose
00103 ):
00104     # Some debugging info.
00105     casalog.origin('imhead')
00106     casalog.post('parameter imagename: ' + imagename, 'DEBUG1')
00107     casalog.post('parameter mode:      ' + mode, 'DEBUG1')
00108     casalog.post('parameter hdkey:     ' + hdkey, 'DEBUG1')
00109     casalog.post('parameter hdvalue:   ' + str(hdvalue), 'DEBUG1')
00110     casalog.post('parameter hdtype:    ' + hdtype, 'DEBUG1')
00111     casalog.post('parameter hdcomment: ' + hdcomment, 'DEBUG1')
00112 
00113     # Initialization stuff, If we are here the user has
00114     # specified an imagename and mode and we don't need to do
00115     # checks on them.  The CASA task infrustructure will
00116     # have done them.  But to make the script insensitive we'll
00117     # make everything lower case.
00118     mode = mode.lower()
00119     hdkey = hdkey.lower()
00120     # if ( isinstance( hdvalue, str ) ):
00121     # hdvalue   = hdvalue.lower()
00122     hdtype = hdtype.lower()
00123     hdcomment = hdcomment.lower()
00124     # ############################################################
00125     #                 HISTORY Mode
00126     # ############################################################
00127     # History mode, List the history information for the
00128     # CASA image file.
00129     myia = iatool()
00130     try:
00131         if mode.startswith('his'):
00132             myia.open(imagename)
00133             myia.history()
00134             myia.done()
00135             return True
00136     except Exception, instance:
00137         casalog.post(str('*** Error ***') + str(instance), 'SEVERE')
00138         return False
00139 
00140     # ############################################################
00141     #                 Summary Mode
00142     # ############################################################
00143     # Summary mode, likely to become obsolete.  When
00144     # Image Analysis summary output looks like the task
00145     # output
00146     try:
00147         if mode.startswith('sum'):
00148             myia.open(imagename)
00149             myia.summary(verbose=verbose)
00150             myia.done()
00151             return True
00152     except Exception, instance:
00153         casalog.post(str('*** Error ***') + str(instance), 'SEVERE')
00154         return False
00155     # ############################################################
00156     # Some variables used to keep track of header iformation
00157     # as follows:
00158     #    hd_keys:     Full list of header keys
00159     #    hd_values:   The values associated with the keywords
00160     #    hd_type:
00161     #    hd_comment:
00162     #
00163     #    axes:        List of lists with axis information.
00164     #                 Order is dir1, dir2, spec, stokes
00165     #
00166     # NOTE: There is extra work done here for put, add, get,
00167     #       and del modes.  As we read the full header into
00168     #       local variables.  However, it makes the code
00169     #       cleaner to read.
00170     #
00171     # ############################################################
00172     axes = []
00173     try:
00174         axes = getimaxes(imagename)
00175     except Exception, instance:
00176         casalog.post(str('*** Error ***') + str(instance), 'SEVERE')
00177         return False
00178 
00179     tbkeys = ['imtype', 'object', 'equinox']
00180     csyskeys = [
00181         'date-obs',
00182         'equinox',
00183         'observer',
00184         'projection',
00185         'restfreq',
00186         'reffreqtype',
00187         'telescope'
00188         ]
00189     imkeys = [
00190         'beammajor',
00191         'beamminor',
00192         'beampa',
00193         'bunit',
00194         'masks',
00195         'shape'
00196         ]
00197     crdkeys = ['ctype', 'crpix', 'crval', 'cdelt', 'cunit']
00198     statkeys = [
00199         'datamin',
00200         'datamax',
00201         'minpos',
00202         'minpixpos',
00203         'maxpos',
00204         'maxpixpos'
00205         ]
00206 
00207     hd_keys = tbkeys + csyskeys + imkeys + statkeys
00208     for key in crdkeys:
00209         for axis in range(len(axes)):
00210             hd_keys.append(key + str(axis + 1))
00211 
00212     hd_values = {}
00213     hd_types = {}
00214     hd_units = {}
00215     hd_comments = {}
00216 
00217     # For each header keyword initialize the dictionaries
00218     # as follows:
00219     #   value     "Not Known"
00220     #   type:     "Not Known"
00221     #   unit:     ""
00222     #   comment:  ""
00223     for hd_field in hd_keys:
00224         hd_values[hd_field] = not_known
00225         hd_types[hd_field] = not_known
00226         hd_units[hd_field] = ''
00227         hd_comments[hd_field] = ''
00228 
00229     # ############################################################
00230     #            Read the header contents.
00231     #
00232     # Needed for get and list mode only.
00233     #
00234     # TODO or consider.  Some of the coordsys() functions take
00235     # a format type.  It might save us a lot of time and trouble
00236     # if we retrieve the information as a string.  For example
00237     #   csys.refernecevalue( format='s')
00238     # ############################################################
00239 
00240     # # Read the information from the TABLE keywords
00241     tbColumns = {}
00242     try:
00243         tb.open(imagename)
00244         tbColumns = tb.getcolkeywords()
00245         tb.close()
00246     except:
00247         casalog.post(str('*** Error *** Unable to open image file ')
00248                      + imagename, 'SEVERE')
00249         return False
00250 
00251     # Now update our header dictionary.
00252     if tbColumns.has_key('imageinfo') and tbColumns['imageinfo'
00253             ].has_key('objectname'):
00254         hd_values['object'] = tbColumns['imageinfo']['objectname']
00255         hd_types['object'] = 'string'
00256         hd_units['object'] = ''
00257         hd_comments['object'] = ''
00258 
00259     if tbColumns.has_key('imageinfo') and tbColumns['imageinfo'
00260             ].has_key('imagetype'):
00261         hd_values['imtype'] = tbColumns['imageinfo']['imagetype']
00262         hd_types['imtype'] = 'string'
00263         hd_units['imtype'] = ''
00264         hd_comments['imtype'] = ''
00265 
00266     # Use ia.summary to gather some of the header information
00267     #     ia.stats for the min and max
00268     #     ia.coordsys() to get a coordsys object to retrieve
00269     #     the coordinate information.
00270     #     Also find the units the data is stored in, for storing
00271     hd_dict = {}
00272     stats = {}
00273     csys = None
00274     misc_info = {}
00275     data_unit = ''
00276     try:
00277         myia.open(imagename)
00278         csys = myia.coordsys()
00279         myia.done()
00280     except Exception, instance:
00281         casalog.post(str('*** Error *** Unable to get coordinate syatem info ')
00282                      + imagename, 'SEVERE')
00283         raise instance
00284     # Find some of the general information from COORD SYS object:
00285     #     OBSERVER, TELESCOPE, RESTFREQUENCY, PROJECTION, EPOCH, and
00286     #     EQUINOX
00287     try:
00288         hd_values['observer'] = csys.observer()
00289         hd_types['observer'] = 'string'
00290         hd_units['observer'] = ''
00291         hd_comments['observer'] = ''
00292     except:
00293         no_op = 'noop'
00294     try:
00295         hd_values['telescope'] = csys.telescope()
00296         hd_types['telescope'] = 'string'
00297         hd_units['telesceope'] = ''
00298         hd_comments['telescope'] = ''
00299     except:
00300         no_op = 'noop'
00301 
00302     try:
00303         tmp = csys.restfrequency()
00304         if tmp.has_key('value'):
00305             hd_values['restfreq'] = list(tmp['value'])
00306         hd_types['restfreq'] = 'list'
00307         if tmp.has_key('unit'):
00308             hd_units['restfreq'] = tmp['unit']
00309         hd_comments['restfreq'] = ''
00310     except:
00311         no_op = 'noop'
00312 
00313     try:
00314         # Expected value is a dictionary with two keys:
00315         #    parameters: the parameters to the projection
00316         #    type:       Typeof projection (value we want)
00317         tmp = csys.projection()
00318         if tmp.has_key('type'):
00319             hd_values['projection'] = tmp['type']
00320         hd_types['projection'] = 'string'
00321         hd_units['projection'] = ''
00322         if tmp.has_key('parameters'):
00323             hd_comments['projection'] = str(list(tmp['parameters']))
00324     except:
00325         no_op = 'noop'
00326 
00327     try:
00328         hd_values['equinox'] = csys.referencecode(type='direction')[0]
00329         hd_types['equinox'] = 'string'
00330         hd_units['equinox'] = ''
00331         hd_comments['equinox'] = ''
00332     except:
00333         no_op = 'noop'
00334 
00335     try:
00336         hd_values['reffreqtype'] = csys.referencecode(type='spectral')
00337         hd_types['reffreqtype'] = 'string'
00338         hd_units['reffreqtype'] = ''
00339         hd_comments['reffreqtype'] = ''
00340     except:
00341         no_op = 'noop'
00342 
00343     try:
00344         tmp = csys.epoch()
00345         if tmp.has_key('m0') and tmp['m0'].has_key('value') and tmp['m0'
00346                 ]['value'] > 0:
00347             hd_values['date-obs'] = qa.time(tmp['m0'], form='ymd')[0]
00348             hd_units['data-obs'] = 'ymd'
00349         hd_types['date-obs'] = 'string'
00350         hd_comments['date-obs'] = ''
00351     except:
00352         no_op = 'noop'
00353         
00354     # yeah the code sucks this way, but when one is given a pile of
00355     # crap to maintain, this is what happens.
00356     if mode == "get" and hd_values.has_key(hdkey) and hd_values[hdkey] != not_known:
00357         return _doget(hd_values.keys(), hdkey, hd_values, hd_types, hd_units)
00358     
00359     try:
00360         myia.open(imagename)
00361         hd_dict = myia.summary(list=False)
00362         stats = myia.statistics(verbose=False, list=False)
00363         misc_info = myia.miscinfo()
00364         data_unit = myia.brightnessunit()
00365         myia.done()
00366     except Exception, instance:
00367         casalog.post(str('*** Error *** Unable to open image file ')
00368                      + imagename, 'SEVERE')
00369         casalog.post(str('              Python error: ')
00370                      + str(instance), 'SEVERE')
00371         return False
00372     # Add the Statistical information as follows:
00373     #    DATAMIN, DATAMAX, MINPIX, MINPIXPOS, MAXPOS, MAXPIXPOS
00374 
00375     # Store the MIN and MAX values.
00376     if stats.has_key('min'):
00377         hd_values['datamin'] = stats['min'][0]
00378         hd_types['datamin'] = 'double'
00379         hd_units['datamin'] = data_unit
00380         hd_comments['datamin'] = ''
00381     if stats.has_key('minposf'):
00382         hd_values['minpos'] = stats['minposf']
00383         hd_types['minpos'] = 'list'
00384         hd_units['minpos'] = ''
00385         hd_comments['minpos'] = ''
00386     if stats.has_key('minpos'):
00387         hd_values['minpixpos'] = stats['minpos']
00388         hd_types['minpixpos'] = 'list'
00389         hd_units['minpixpos'] = 'pixels'
00390         hd_comments['minpixpos'] = ''
00391 
00392     if stats.has_key('max'):
00393         hd_values['datamax'] = stats['max'][0]
00394         hd_types['datamax'] = 'double'
00395         hd_units['datamax'] = data_unit
00396         hd_comments['datamax'] = ''
00397     if stats.has_key('maxposf'):
00398         hd_values['maxpos'] = stats['maxposf']
00399         hd_types['maxpos'] = 'list'
00400         hd_units['maxpos'] = ''
00401         hd_comments['maxpos'] = ''
00402     if stats.has_key('maxpos'):
00403         hd_values['maxpixpos'] = stats['maxpos']
00404         hd_types['maxpixpos'] = 'list'
00405         hd_units['maxpixpos'] = 'pixels'
00406         hd_comments['maxpixpos'] = ''
00407 
00408     
00409     # Set some more of the standard header keys from the
00410     # header dictionary abtained from ia.summary().  The
00411     # fiels that will be set are:
00412     #     BEAMMAJOR, BEAMMINOR, BEAMPA, BUNIT, MASKS, CRVALx, CRPIXx,
00413     #     CDETLx,  CTYPEx, CUNITx
00414     if hd_dict.has_key('unit'):
00415         hd_values['bunit'] = hd_dict['unit']
00416         hd_types['bunit'] = 'string'
00417         hd_units['bunit'] = ''
00418         hd_comments['bunit'] = ''
00419 
00420     if hd_dict.has_key('restoringbeam'):
00421         tmp = hd_dict['restoringbeam']
00422         if tmp.has_key('major'):
00423             tmp2 = tmp['major']
00424             if tmp2.has_key('value'):
00425                 hd_values['beammajor'] = tmp2['value']
00426             if tmp2.has_key('unit'):
00427                 hd_units['beammajor'] = str(tmp2['unit'])
00428             hd_types['beammajor'] = 'double'
00429             hd_comments['beammajor'] = ''
00430 
00431         if tmp.has_key('minor'):
00432             tmp2 = tmp['minor']
00433             if tmp2.has_key('value'):
00434                 hd_values['beamminor'] = tmp2['value']
00435             if tmp2.has_key('unit'):
00436                 hd_units['beamminor'] = str(tmp2['unit'])
00437             hd_types['beamminor'] = 'double'
00438             hd_comments['beamminor'] = ''
00439 
00440         if tmp.has_key('positionangle'):
00441             tmp2 = tmp['positionangle']
00442             if tmp2.has_key('value'):
00443                 hd_values['beampa'] = tmp2['value']
00444             if tmp2.has_key('unit'):
00445                 hd_units['beampa'] = str(tmp2['unit'])
00446             hd_types['beampa'] = 'double'
00447             hd_comments['beampa'] = ''
00448 
00449     if hd_dict.has_key('masks') and len(hd_dict['masks']) > 0:
00450         hd_values['masks'] = hd_dict['masks'][0]
00451         hd_types['masks'] = 'string'
00452         hd_units['masks'] = ''
00453         hd_comments['masks'] = ''
00454 
00455     if hd_dict.has_key('shape'):
00456         hd_values['shape'] = hd_dict['shape']
00457         hd_types['shape'] = 'list'
00458 
00459     # The COORDINATE keywords
00460     stokes = 'Not Known'
00461     if isinstance(axes[3][0], int):
00462         stokes = csys.stokes()
00463     hd_coordtypes = []
00464     for i in range(hd_dict['ndim']):
00465         hd_values['ctype' + str(i + 1)] = hd_dict['axisnames'][i]
00466         hd_values['crpix' + str(i + 1)] = hd_dict['refpix'][i]
00467         if hd_dict['axisnames'][i].lower() == 'stokes':
00468             hd_values['crval' + str(i + 1)] = stokes
00469             hd_units['crval' + str(i + 1)] = hd_dict['axisunits'][i]
00470         else:
00471             hd_values['crval' + str(i + 1)] = hd_dict['refval'][i]
00472             hd_units['crval' + str(i + 1)] = hd_dict['axisunits'][i]
00473             hd_types['crval' + str(i + 1)] = 'float'
00474 
00475         hd_values['cdelt' + str(i + 1)] = hd_dict['incr'][i]
00476         hd_units['cdelt' + str(i + 1)] = hd_dict['axisunits'][i]
00477         hd_types['cdelt' + str(i + 1)] = 'float'
00478         hd_values['cunit' + str(i + 1)] = hd_dict['axisunits'][i]
00479 
00480     # Add the miscellaneous info/keywords
00481     # TODO add some some smarts to figure out the type
00482     #      of the keyword, and maybe unit.
00483     for new_key in misc_info.keys():
00484         hd_values[new_key] = misc_info[new_key]
00485         hd_types[new_key] = ''
00486         hd_units[new_key] = ''
00487         hd_comments[new_key] = ''
00488 
00489     # Find all of the *user defined* keywords, Python sets
00490     # support differences but lists don't this is why we use
00491     # sets here.
00492     user_keys = list(set(hd_values.keys()) - set(hd_keys))
00493     casalog.post('List of user defined keywords found are: '
00494                  + str(user_keys), 'DEBUG2')
00495     casalog.post(str(hd_values), 'DEBUG2')
00496     casalog.post(str(hd_types), 'DEBUG2')
00497     casalog.post(str(hd_units), 'DEBUG2')
00498     casalog.post(str(hd_comments), 'DEBUG2')
00499 
00500     # ############################################################
00501     #                     FITS MODE
00502     #
00503     # Just #print out all the information we just gathered.
00504     # ############################################################
00505     if mode == 'fits':
00506         casalog.post('Sorry this mode is not available yet.', 'WARN')
00507         return False
00508 
00509     # ############################################################
00510     #                     List MODE
00511     #
00512     # Just #print out all the information we just gathered.
00513     # ############################################################
00514     if mode == 'list':
00515         try:
00516             casalog.post('Available header items to modify:')
00517             casalog.post('General --')
00518             # casalog.post( '' )
00519 
00520             user_key_count = 0
00521             for field in hd_keys + user_keys:
00522                 if field.startswith('ctype'):
00523                     if field == 'ctype1':
00524                         casalog.post('axes --')
00525                     casalog.post(str('        -- ') + field + str(': ')
00526                                  + str(hd_values[field]))
00527                 elif field.startswith('crpix'):
00528 
00529                     if field == 'crpix1':
00530                         casalog.post('crpix --')
00531                     casalog.post(str('        -- ') + field + str(': ')
00532                                  + str(hd_values[field]))
00533                 elif field.startswith('crval'):
00534 
00535                     if field == 'crval1':
00536                         casalog.post('crval --')
00537                     index = int(field[5:])
00538                     printVal = str(hd_values[field]) \
00539                         + str(hd_values['cunit' + str(index)])
00540                     if hd_values['cunit' + str(index)] == 'rad':
00541                         printVal = qa.formxxx(printVal, 'dms') \
00542                             + str('deg.min.sec')
00543                     casalog.post(str('        -- ') + field + str(': ')
00544                                  + printVal)
00545                 elif field.startswith('cdelt'):
00546 
00547                     if field == 'cdelt1':
00548                         casalog.post('cdelt --')
00549                     index = int(field[5:])
00550                     printVal = str(hd_values[field]) \
00551                         + str(hd_values['cunit' + str(index)])
00552                     if hd_values['cunit' + str(index)] == 'rad':
00553                         printVal = qa.formxxx(printVal, 'dms') \
00554                             + str('deg.min.sec')
00555                     casalog.post(str('        -- ') + field + str(': ')
00556                                  + printVal)
00557                 elif field.startswith('cunit'):
00558 
00559                     if field == 'cunit1':
00560                         casalog.post('units --')
00561                     outUnit = hd_values[field]
00562                     if outUnit == 'rad':
00563                         outUnit = 'deg.min.sec'
00564                     casalog.post(str('        -- ') + field + str(': ')
00565                                  + outUnit)
00566                 elif hd_keys.count(field) < 1:
00567 
00568                     # This is a user defined keyword
00569                     if user_key_count < 1:
00570                         casalog.post('User Defined --')
00571                     user_key_count = user_key_count + 1
00572                     casalog.post(str('        -- ') + field + str(': ')
00573                                  + str(hd_values[field]))
00574                 else:
00575                     value = str(hd_values[field])
00576                     if (
00577                         field.startswith("beam")
00578                         and str(hd_values[field]) == not_known
00579                     ):
00580                         myia.open(imagename)
00581                         res = myia.restoringbeam()
00582                         myia.done()
00583                         if (len(res.keys()) > 0):
00584                             value = "This image has multiple beams. Use mode='summary' to get a listing'"
00585                     casalog.post(str('        -- ') + field + str(': ')
00586                                  + value)
00587             if csys != None:
00588                 csys.done()
00589                 del csys
00590             return hd_values
00591         except Exception, instance:
00592 
00593             casalog.post(str('*** Error ***') + str(instance), 'SEVERE')
00594             casalog.post(str('              Python error: ')
00595                          + str(instance), 'SEVERE')
00596             return False
00597 
00598     # ############################################################
00599     #                     add MODE
00600     #
00601     # Add a new keyword to the image table.
00602     #
00603     # ############################################################
00604     key_list = hd_keys + user_keys
00605     casalog.post('All of the header keys: ' + str(key_list), 'DEBUG2')
00606 
00607     if mode == 'add' and key_list.count(hdkey) > 0 \
00608         and str(hd_values[hdkey]) != not_known:
00609         # The Keyword is already in the header, we are
00610         # switching to put mode.
00611         casalog.post(hdkey
00612                      + str(' is already in the header, switching to "put" mode.'
00613                      ), 'WARN')
00614         mode = 'put'
00615 
00616     if mode == 'add':
00617         if hdkey in tbkeys:
00618             try:
00619                 # We are dealing with having to add to the table columns.
00620                 tb.open(imagename, nomodify=False)
00621                 if hdkey == 'imtype':
00622                     tbColumns['imageinfo']['imagetype'] = hdvalue
00623                 else:
00624                     tbColumns['imageinfo']['objectname'] = hdvalue
00625                 tb.putkeywords(tbColumns)
00626                 tb.close()
00627                 casalog.post(hdkey + ' keyword has been ADDED to '
00628                              + imagename + "'s header", 'NORMAL')
00629                 return True
00630             except Exception, instance:
00631                 casalog.post('*** Error *** Unable to add keyword '
00632                              + hdkey + ' to image file ' + imagename,
00633                              'SEVERE')
00634                 casalog.post(str('              Python error: ')
00635                              + str(instance), 'SEVERE')
00636                 return False
00637         elif hdkey in statkeys:
00638 
00639             # This is a statistical value, these are generated by
00640             # ia.statistics and not actually in the header.
00641             casalog.post(hdkey
00642                          + str(' is not part of the header information, but generated by the image\nstatistics function. Therefore there are no values to place in the header.'
00643                          ), 'WARN')
00644             return False
00645         elif hdkey in crdkeys:
00646 
00647             # We need to use a coordinate system function to add
00648             # this keyword, in fact we need to add a coordinate
00649             # axis.
00650             #
00651             # TODO
00652             return False
00653         elif hdkey in csyskeys or hdkey in imkeys:
00654 
00655             # We need to use a coordinate system function to add
00656             # this keyword.  Since these keywords are known by
00657             # by the coordsys tool, it is sufficent to change
00658             # to "put" mode and update the field.
00659             if hdkey == 'masks':
00660                 casalog.post('imhead does not add masks to images, this is to complex a task\n use the "makemask" task to add masks'
00661                              , 'WARN')
00662                 return False
00663 
00664             # CAS-3301. I guess shape is always present in an image,
00665             # therefore it is enough to change its value using put.
00666             casalog.post(hdkey + ' switching to "put" mode to add the '
00667                          + hdkey + ' to the header.', 'WARN')
00668             mode = 'put'
00669         else:
00670 
00671             # Handle a User defined keyword
00672             try:
00673                 # First step is to make sure we have the given
00674                 # value in the expected data type, if there is
00675                 # one.  The default is the string stored in hd_types
00676                 # if there isn't a value then str is used.
00677                 value = hdvalue
00678                 keytype = 'str'
00679                 if type(hdtype) != None and len(hdtype) > 0:
00680                     keytype = hdtype
00681                 elif hd_types.has_key(hdkey) and len(hd_types[hdkey]) \
00682                     > 0:
00683                     keytype = hd_types[hdkey]
00684                 elif isinstance(hdvalue, str):
00685                     keytype = 'str'
00686                 elif isinstance(hdvalue, int):
00687                     keytype = 'int'
00688                 elif isinstance(hdvalue, float):
00689                     keytype = 'float'
00690                 elif isinstance(hdvalue, complex):
00691                     keytype = 'complex'
00692                 elif isinstance(hdvalue, list):
00693                     keytype = 'list'
00694                 elif isinstance(hdvalue, dict):
00695                     keytype = 'dict'
00696 
00697                 if keytype == 'double':
00698                     keytype = 'float'
00699                 if keytype == 'string':
00700                     keytype = 'str'
00701 
00702                 # TODO -- Add a check to see if we really need
00703                 #        to do the type conversion.
00704                 if isinstance(value, str):
00705                     value = eval(keytype + '("' + value + '")')
00706                 else:
00707                     value = eval(keytype + '("' + str(value) + '")')
00708 
00709                 misc_info[hdkey] = value
00710                 myia.open(imagename)
00711                 myia.setmiscinfo(misc_info)
00712                 myia.done()
00713                 casalog.post(hdkey + ' keyword has been ADDED to '
00714                              + imagename + "'s header with value "
00715                              + str(value), 'NORMAL')
00716                 return True
00717             except Exception, instance:
00718                 casalog.post('*** Error *** Unable to add keyword '
00719                              + hdkey + ' to image file ' + imagename,
00720                              'SEVERE')
00721                 casalog.post(str('              Python error: ')
00722                              + str(instance), 'SEVERE')
00723                 return False
00724 
00725     # ############################################################
00726     #                     Del MODE
00727     #
00728     # Remove a keyword from the image table.
00729     # ############################################################
00730     if mode == 'del' and (key_list.count(hdkey) < 0 or hd_values[hdkey]
00731                           == not_known):
00732         # The Keyword is already absent there is nothing to do.
00733         casalog.post(hdkey
00734                      + str(' is already absent in the header, therfore nothing to delete.'
00735                      , 'WARN'))
00736         return False
00737     if mode == 'del':
00738         if hdkey in tbkeys:
00739             try:
00740                 # We are dealing with having to delete from the table columns.
00741                 tb.open(imagename, nomodify=False)
00742                 key = ''
00743                 if hdkey == 'object':
00744                     if (tbColumns['imageinfo'].has_key('objectname')):
00745                         tbColumns['imageinfo'].pop('objectname')
00746                 else:
00747                     tbColumns['imageinfo'].has_key('imagetype')
00748                     tbColumns['imageinfo'].pop('imagetype')
00749                 tb.putcolkeywords(columnname="", value=tbColumns)
00750                 tb.flush()
00751                 tb.done()
00752                 casalog.post(hdkey + ' keyword has been DELETED from '
00753                              + imagename + "'s header", 'NORMAL')
00754                 return True
00755             except Exception, instance:
00756                 casalog.post('*** Error *** Unable delete keyword '
00757                              + hdkey + ' from image file ' + imagename,
00758                              'SEVERE')
00759                 casalog.post(str('              Python error: ')
00760                              + str(instance), 'SEVERE')
00761                 return False
00762         elif hdkey in statkeys:
00763 
00764             # This is a statistical value, these are generated by
00765             # ia.statistics and not actually in the header.
00766             casalog.post(hdkey
00767                          + str(' is not part of the header information, but generated by the image\nstatistics function. Therefore there are no values to be removed from the header.'
00768                          ), 'WARN')
00769             return False
00770         elif hdkey in imkeys or hdkey in csyskeys:
00771 
00772             # We need to use an image analysis function to delete this
00773             # keyword.
00774             try:
00775                 if hdkey.startswith('beam'):
00776                     # We don't actually remove the beam information, we
00777                     # instead set it to the default information.
00778                     myia.open(imagename)
00779                     myia.setrestoringbeam(remove=True)
00780                     myia.done()
00781                     casalog.post('The restoring beam has been removed from '
00782                                   + imagename, 'NORMAL')
00783                     return True
00784                 elif hdkey.startswith('mask'):
00785                     myia.open(imagename)
00786                     myia.maskhandler(op='delete', name=hdvalue)
00787                     myia.done()
00788                     casalog.post('Mask ' + hdvalue
00789                                  + ' has been removed from '
00790                                  + imagename, 'NORMAL')
00791                 else:
00792                     casalog.post(hdkey
00793                                  + str(' can not be removed, setting value to '
00794                                  ) + not_known + str(' instead.'),
00795                                  'WARN')
00796                     mode = 'put'
00797                     # TODO set hdvalue to a sensible default?
00798                     # hdvalue = not_known
00799                     hdvalue = ''
00800             except Exception, instance:
00801                 casalog.post('*** Error *** Unable delete keyword '
00802                              + hdkey + ' from image file ' + imagename,
00803                              'SEVERE')
00804                 casalog.post(str('              Python error: ')
00805                              + str(instance), 'SEVERE')
00806                 return True
00807         elif hdkey in crdkeys:
00808 
00809             # We need to use a coordinate system function to add
00810             # this keyword, in fact we need to add a coordinat
00811             # axis.
00812             #
00813             # TODO
00814             no_op = 'noop'
00815             return False
00816         else:
00817 
00818             try:
00819                 # Handle a User defined keyword
00820                 value = hdvalue
00821                 if misc_info.has_key(hdkey):
00822                     junk = misc_info.pop(hdkey)
00823                 myia.open(imagename)
00824                 myia.setmiscinfo(misc_info)
00825                 myia.done()
00826                 casalog.post(hdkey + ' keyword has been ADDED to '
00827                              + imagename + "'s header with value "
00828                              + str(value), 'NORMAL')
00829                 return True
00830             except Exception, instance:
00831                 casalog.post('*** Error *** Unable delete keyword '
00832                              + hdkey + ' from image file ' + imagename,
00833                              'SEVERE')
00834                 casalog.post(str('              Python error: ')
00835                              + str(instance), 'SEVERE')
00836                 return False
00837 
00838     if mode == "get":
00839         return _doget(key_list, hdkey, hd_values, hd_types, hd_units)
00840     # ############################################################
00841     #                     put MODE
00842     #
00843     # If we made it here the user is putting something into
00844     # the header.
00845     # ############################################################
00846     if mode != 'put':
00847         return False
00848     casalog.post('Putting (changing): ' + hdkey + ' to  '
00849                  + str(hdvalue), 'DEBUG2')
00850     if hdkey in tbkeys:
00851         try:
00852             tb.open(imagename, nomodify=False)
00853             if hdkey == 'equinox':
00854                 imagecoords = tb.getkeyword('coords')
00855                 imagecoords['direction0']['system'] = hdvalue
00856                 tb.putkeyword(keyword='coords', value=imagecoords)
00857                 casalog.post('Changing only the value of ' + hdkey
00858                              + '. It is your responsibility to ensure the coordinate '
00859 
00860                              + 'values are corret; this task does not transform the '
00861 
00862                              + 'coordinates. If you want something that does, see eg imregrid'
00863                              , 'WARN')
00864             else:
00865                 if hdkey == 'object':
00866                     tbColumns['imageinfo']['objectname'] = hdvalue
00867                 elif hdkey == 'imtype':
00868                     tbColumns['imageinfo']['imagetype'] = hdvalue
00869                 tb.putcolkeywords(columnname="", value=tbColumns)
00870             tb.flush()
00871             tb.done()
00872             casalog.post(hdkey + ' keyword has been UPDATED in '
00873                          + imagename + "'s header", 'NORMAL')
00874             return True
00875         except Exception, instance:
00876             casalog.post('*** Error *** Unable to update keyword ' + hdkey
00877                          + ' from image file ' + imagename + "\n" + str(instance), 'SEVERE')
00878             return False
00879     elif hdkey in statkeys:
00880         # This is a statistical value, these are generated by
00881         # ia.statistics and not actually in the header.
00882         casalog.post(hdkey
00883                      + str(' is not part of the header information, but generated by the image\nstatistics function. Therefore, '
00884                       + hdkey + ' can not be changed.'), 'WARN')
00885         return hdvalue
00886     elif hdkey in imkeys:
00887 
00888         # Header values that can be changed through the image analysis tool.
00889         try:
00890             # These are field that can be set with the image
00891             # analysis tool
00892             myia.open(imagename)
00893             if hdkey == 'bunit':
00894                 myia.setbrightnessunit(hdvalue)
00895             elif hdkey == 'masks':
00896 
00897                 # We only set the default mask to the first mask in
00898                 # the list.
00899                 # TODO delete any masks that aren't in the list.
00900                 if type(hdvalue, list):
00901                     myia.maskhanderler(op='set', name=hdvalue[0])
00902                 else:
00903                     myia.maskhanderler(op='set', name=hdvalue)
00904             elif hdkey.startswith('beam'):
00905 
00906                 # Get orignal values.  Note that since there are
00907                 # expected header fields we assume that they exist
00908                 # in our dictionary of header values.
00909                 #
00910                 # TODO IF NOT A LIST BUT A STRING CHECK FOR
00911                 # UNITS
00912                
00913 
00914                 major = {'unit': 'arcsec', 'value': 1}
00915                 if str(hd_values['beammajor']) != not_known:
00916                     major = {'value': hd_values['beammajor'],
00917                              'unit': hd_units['beammajor']}
00918 
00919                 minor = {'unit': 'arcsec', 'value': 1}
00920                 if str(hd_values['beamminor']) != not_known:
00921                     minor = {'value': hd_values['beamminor'],
00922                              'unit': hd_units['beamminor']}
00923 
00924                 pa = {'unit': 'deg', 'value': 0}
00925                 if str(hd_values['beampa']) != not_known:
00926                     pa = {'value': hd_values['beampa'],
00927                           'unit': hd_units['beampa']}
00928                 if hdkey == 'beammajor':
00929                     major = _imhead_strip_units(hdvalue,
00930                             hd_values['beammajor'], hd_units['beammajor'
00931                             ])
00932                     major['value'] = float(major['value'])
00933                 elif hdkey == 'beamminor':
00934                     minor = _imhead_strip_units(hdvalue,
00935                             hd_values['beamminor'], hd_units['beamminor'
00936                             ])
00937                     minor['value'] = float(minor['value'])
00938                 elif hdkey == 'beampa':
00939                     pa = _imhead_strip_units(hdvalue, hd_values['beampa'
00940                             ], hd_units['beampa'])
00941                     pa['value'] = float(pa['value'])
00942                 else:
00943                     casalog.post('*** Error *** Unrecognized beam keyword '
00944                                   + hdkey, 'SEVERE')
00945                     myia.done()
00946                     return False
00947                 myia.setrestoringbeam(beam={'major': major, 'minor'
00948                                     : minor, 'positionangle': pa},
00949                                     log=True)
00950             elif hdkey == 'shape':
00951 
00952             # CAS-3301
00953                 casalog.post('*** Error *** imhead does not support changing the shape of the image body. Use the ia tool instead'
00954                              , 'SEVERE')
00955                 myia.done()
00956                 return False
00957 
00958             myia.done()
00959             casalog.post(hdkey + ' keyword has been UPDATED to '
00960                          + imagename + "'s header", 'NORMAL')
00961             return hdvalue
00962         except Exception, instance:
00963             casalog.post('*** Error *** Unable to update keyword '
00964                          + hdkey + ' from image file ' + imagename
00965                          + '\n' + str(instance), 'SEVERE')
00966             return False
00967     elif hdkey in csyskeys:
00968 
00969         # TODO I'm not sure why those who came before decided to use the coordsys tool for this
00970         # because its very dangerous as other values can also be changed without the user
00971         # being aware. For example, I'm moving 'equinox' out of this block because changing
00972         # the epoch using the coordsys tool also changes the positional (crval) values, which
00973         # is definitely not desired. - dmehring 2009sep01.
00974         #
00975         # Header values that can be changed through the coordsys tool
00976         try:
00977             myia.open(imagename)
00978             csys = myia.coordsys()
00979             if hdkey == 'date-obs':
00980                 if hdvalue == 'Not Known' or hdvalue == 'UNKNOWN':
00981                     hdvalue = 0
00982                 tmp = me.epoch(v0=str(hdvalue))
00983                 csys.setepoch(tmp)
00984             elif hdkey == 'observer':
00985                 csys.setobserver(str(hdvalue))
00986             elif hdkey == 'projection':
00987                 csys.setprojection(hdvalue)
00988             elif hdkey == 'telescope':
00989                 csys.settelescope(str(hdvalue))
00990             elif hdkey == 'reffreqtype':
00991                 csys.setconversiontype(spectral=str(hdvalue))
00992             elif hdkey == 'restfreq':
00993                 # TODO handle a list of rest frequencies
00994 
00995                 if isinstance(hdvalue, list):
00996                     no_op = 'noop'  # Nothing to change here
00997                 elif not isinstance(hdvalue, str):
00998                     hdvalue = str(hdvalue)
00999                 else:
01000                     hdvalue = hdvalue.split(',')
01001                 # Loop through the list of values, adding each
01002                 # one separately.
01003 
01004                 if isinstance(hdvalue, str):
01005                     num_freq = 1
01006                 else:
01007                     num_freq = len(hdvalue)
01008 
01009                 for i in range(num_freq):
01010                     if not isinstance(hdvalue[i], str):
01011                         current = str(hdvalue[i])
01012                     else:
01013                         current = hdvalue[i]
01014                     # Remove any units from the string, if
01015                     # there are any.
01016                     parsed_input = _imhead_strip_units(current, 0.0,
01017                             hd_units[hdkey])
01018                     parsed_input['value'] = float(parsed_input['value'])
01019 
01020                     if i < 1:
01021                         csys.setrestfrequency(parsed_input)
01022                     else:
01023                         csys.setrestfrequency(parsed_input, append=True)
01024             else:
01025                 casalog.post(str('*** Error *** Unrecognized hdkey ')
01026                              + str(hdkey), 'SEVERE')
01027                 return
01028 
01029             # Now store the values!
01030             myia.setcoordsys(csys=csys.torecord())
01031             myia.done()
01032             csys.done()
01033             del csys
01034             casalog.post(hdkey + ' keyword has been UPDATED to '
01035                          + imagename + "'s header", 'NORMAL')
01036             return hdvalue
01037         except Exception, instance:
01038             if myia.isopen():
01039                 myia.done()
01040             if csys != None:
01041                 csys.done()
01042                 del csys
01043             casalog.post('*** Error *** Unable to UPDATE keyword '
01044                          + hdkey + ' in image file ' + imagename + "\n" + str(instance),
01045                          'SEVERE')
01046             casalog.post(str('              Python error: ')
01047                          + str(instance), 'SEVERE')
01048             return False
01049     elif hdkey[0:5] in crdkeys:
01050 
01051         # Coordinate axes information, changed through the coordsys tool
01052         try:
01053             # Open the file and obtain a coordsys tool
01054             myia.open(imagename)
01055             csys = myia.coordsys()
01056 
01057             # Find which axis is being modified from the field name
01058             # (hdkey). Note, that internally we use 0-based
01059             # indexes but the user input will be 1-based, we
01060             # convert by subtracting 1. Also note that all of our
01061             # fields are 5 character long.
01062             fieldRoot = hdkey[0:5]
01063             index = -1
01064             if len(hdkey) > 5:
01065                 index = int(hdkey[5:]) - 1
01066 
01067             # cdelt, crpix, and cval all return a record, the values
01068             # are store in and *array* in the 'numeric' field.  But
01069             # we need to input a list, this makes things a bit messy!
01070             if hdkey.startswith('cdelt'):
01071                 # We need
01072                 if index < 0:
01073                     csys.setincrement(hdvalue)
01074                 else:
01075                     # We grab the reference values as quantities,
01076                     # which allows users to specify a wider variety
01077                     # of units.  We are going to make an assumption
01078                     # that if there are no units at the end that the
01079                     # user gave the value in radians, but checking if
01080                     # the last character of the string is a number.
01081                     values = csys.increment(format='q')
01082                     units = csys.units()
01083 
01084                     if isinstance(hdvalue, str):
01085                         hdvalue = hdvalue.strip()
01086 
01087                         # Remove any '.' and ':' from the string
01088                         # if all that remains is a number then the
01089                         # user didn't give any units.
01090                         tmpVal = hdvalue.replace('.', '')
01091                         tmpVal = tmpVal.replace(':', '')
01092                         if tmpVal.isdigit():
01093                             if len(units[index]) > 0:
01094                                 hdvalue = hdvalue + units[index]
01095                     elif isinstance(hdvalue, int) \
01096                         or isinstance(hdvalue, float):
01097 
01098                         if len(units[index]) > 0:
01099                             hdvalue = qa.convert(str(hdvalue)
01100                                     + units[index], units[index])
01101                         else:
01102                             hdvalue = str(hdvalue)
01103 
01104                     # reference axies are 1-based, but python
01105                     # lists are 0-based -- yikes!
01106                     values['quantity']['*' + str(index + 1)] = \
01107                         qa.convert(hdvalue, units[index])
01108                     csys.setincrement(values)
01109             elif hdkey.startswith('crpix'):
01110 
01111                 if index < 0:
01112                     csys.setreferencepixel(hdvalue)
01113                 else:
01114                     values = csys.referencepixel()['numeric']
01115                     values = [values[0], values[1], values[2],
01116                               values[3]]
01117                     values[index] = float(hdvalue)
01118                     csys.setreferencepixel(values)
01119             elif hdkey.startswith('crval'):
01120                 # Because setreferencevalue has issues
01121                 # with setting the stokes value, if
01122                 # the stokes axis has changed we use the
01123                 # setstokes function instead.
01124                 if isinstance(axes[3][0], int):
01125                     stokesIndex = axes[3][0]
01126                     origvalue = csys.referencevalue('s')['string'
01127                             ][stokesIndex]
01128                 else:
01129                     origvalue = 'Not Known'
01130 
01131                 if index < 0:
01132                     newvalue = hdvalue[stokesIndex]
01133                     if origvalue != newvalue:
01134                         hdvalue[stokesIndex] = origvalue
01135                         newvalue = hdvalue[stokesIndex]
01136                         csys.setreferencevalue(hdvalue)
01137                         csys.setstokes(newvalue)
01138                 else:
01139                     # We grab the reference values as quantities,
01140                     # which allows users to specify a wider variety
01141                     # of units.  We are going to make an assumption
01142                     # that if there are no units at the end that the
01143                     # user gave the value in radians, but checking if
01144                     # the last character of the string is a number.
01145                     values = csys.referencevalue(format='q')
01146                     units = csys.units()
01147                     if isinstance(hdvalue, str):
01148                         hdvalue = hdvalue.strip()
01149 
01150                         # Remove any '.' and ':' from the string
01151                         # if all that remains is a number then the
01152                         # user didn't give any units.
01153                         tmpVal = hdvalue.replace('.', '')
01154                         tmpVal = tmpVal.replace(':', '')
01155                         if tmpVal.isdigit():
01156                             if index < len(units) and len(units[index]) \
01157                                 > 0:
01158                                 if (hdvalue.count(":") > 1 or hdvalue.count(".") > 2):
01159                                     # hh:mm:ss or dd.mm.ss input format here
01160                                     hdvalue = qa.tos(qa.toangle(hdvalue))
01161                                 else:
01162                                     # input is a floating point number
01163                                     hdvalue = hdvalue + units[index]
01164                             else:
01165                                 hdvalue = hdvalue
01166                     elif isinstance(hdvalue, int) \
01167                         or isinstance(hdvalue, float):
01168                         if index < len(units) and len(units[index]) > 0:
01169                             hdvalue = qa.convert(str(hdvalue)
01170                                     + units[index], units[index])
01171                         else:
01172                             hdvalue = str(hdvalue)
01173 
01174                     # reference axies are 1-based, but python
01175                     # lists are 0-based -- yikes!
01176                     #
01177                     # We also need to deal with stokes changes
01178                     # very carefully, and use csys.setstokes()
01179                     #
01180                     # TODO We need to deal with adding stokes
01181                     # directional, and spectral values with
01182                     # the appropriate csys.set?? methods.
01183                     if index != axes[3][0]:
01184                         if index < len(units) and len(units[index]) > 0:
01185                             values['quantity']['*' + str(index + 1)] = \
01186                                 qa.convert(hdvalue, units[index])
01187                         else:
01188                             values['quantity']['*' + str(index + 1)] = \
01189                                 hdvalue
01190                         csys.setreferencevalue(values)
01191                     else:
01192                         origStokes = csys.referencevalue('s')
01193                         origStokes = origStokes['string'][index]
01194                         if origStokes != hdvalue:
01195                             csys.setstokes(hdvalue)
01196             elif hdkey.startswith('ctype'):
01197 
01198                 if index < 0:
01199                     csys.setnames(hdvalue)
01200                 else:
01201                     values = csys.names()
01202                     values[index] = str(hdvalue)
01203                     csys.setnames(values)
01204             elif hdkey.startswith('cunit'):
01205 
01206                 if index < 0:
01207                     csys.setunits(hdvalue)
01208                 else:
01209                     values = csys.units()
01210                     values[index] = str(hdvalue)
01211                     csys.setunits(values)
01212 
01213             # Store the changed values.
01214             if csys != None:
01215                 myia.setcoordsys(csys=csys.torecord())
01216                 csys.done()
01217                 del csys
01218 
01219             myia.done()
01220             casalog.post(hdkey + ' keyword has been UPDATED in '
01221                          + imagename + "'s header", 'NORMAL')
01222             return hdvalue
01223         except Exception, instance:
01224             casalog.post(str('*** Error ***') + str(instance), 'SEVERE')
01225             return False
01226     else:
01227 
01228         # User defined keywords, changed with the image analsys too's
01229         # "miscinfo" function.
01230         # TODO Add units and comments
01231         try:
01232             # First step is to make sure we have the given
01233             # value in the expected data type, if there is
01234             # one.  The default is the string stored in hd_types
01235             # if there isn't a value then str is used.
01236             value = hdvalue
01237             keytype = 'str'
01238             if type(hdtype) != None and len(hdtype) > 0:
01239                 keytype = hdtype
01240             elif hd_types.has_key(hdkey) and len(hd_types[hdkey]) > 0:
01241                 keytype = hd_types[hdkey]
01242             elif isinstance(hdvalue, str):
01243                 keytype = 'str'
01244             elif isinstance(hdvalue, int):
01245                 keytype = 'int'
01246             elif isinstance(hdvalue, float):
01247                 keytype = 'float'
01248             elif isinstance(hdvalue, complex):
01249                 keytype = 'complex'
01250             elif isinstance(hdvalue, list):
01251                 keytype = 'list'
01252             elif isinstance(hdvalue, dict):
01253                 keytype = 'dict'
01254 
01255             if keytype == 'double':
01256                 keytype = 'float'
01257             if keytype == 'string':
01258                 keytype = 'str'
01259 
01260             # TODO -- Add a check to see if we really need
01261             #        to do the type conversion.
01262             if isinstance(value, str):
01263                 value = eval(keytype + '("' + value + '")')
01264             else:
01265                 value = eval(keytype + '("' + str(value) + '")')
01266 
01267             misc_info[hdkey] = value
01268             myia.open(imagename)
01269             myia.setmiscinfo(misc_info)
01270             myia.done()
01271             casalog.post(hdkey + ' keyword has been ADDED to '
01272                          + imagename + "'s header with value "
01273                          + str(value), 'NORMAL')
01274             return True
01275         except Exception, instance:
01276             casalog.post('*** Error *** Unable to change keyword '
01277                          + hdkey + ' in ' + imagename + ' to '
01278                          + str(hdvalue), 'SEVERE')
01279             casalog.post(str('              Python error: ')
01280                          + str(instance), 'SEVERE')
01281             return False
01282 
01283 
01284 #
01285 # NAME:        _imhead_strip_units
01286 #
01287 # AUTHOR:      S. Jaeger
01288 #
01289 # PURPOSE:     To take as input a string which contains a numeric value
01290 #              followed by a unit and separate them.
01291 #
01292 # DESCRIPTION: Take the input string, find the the index of the
01293 #              last numerical character in the string.  We assume
01294 #              this is the point where the number ends and the unit
01295 #              begins and split the input string at this place.
01296 #
01297 #              If there is no value found, then the default value
01298 #              is used.  Similarly for the units.
01299 #
01300 # RETURN:      dictionary of the form:
01301 #                  { 'value': numberFound, 'unit', 'unitfound' }
01302 
01303 
01304 def _imhead_strip_units(input_number, default_value=0.0, default_unit=''
01305                         ):
01306     # Find the place where the units start and the number
01307     # ends.
01308 
01309     if isinstance(input_number, dict):
01310         if input_number.has_key('value'):
01311             value = input_number['value']
01312         else:
01313             value = default_value
01314 
01315         if input_number.has_key('unit'):
01316             unit = input_number['unit']
01317         else:
01318             unit = default_unit
01319     elif isinstance(input_number, str):
01320         lastNumber = -1
01321         for j in range(len(input_number)):
01322             if input_number[j].isdigit():
01323                 lastNumber = j
01324 
01325         if lastNumber >= len(input_number) - 1:
01326             unit = default_unit
01327         else:
01328             unit = input_number[lastNumber + 1:]
01329 
01330         if lastNumber < 0:
01331             value = default_value
01332         else:
01333             value = input_number[0:lastNumber + 1]
01334     else:
01335         raise Exception, \
01336             'Unable to parse units from numerical value in input ' \
01337             + str(input_number)
01338 
01339     return {'value': value, 'unit': unit}
01340 
01341 
01342 def _doget(key_list, hdkey, hd_values, hd_types, hd_units):
01343  # ############################################################
01344     #                     get MODE
01345     #
01346     # Just #print out the requested information. Note that we
01347     # acquired all of the header details, which is not very
01348     # efficient for get, but it does make the code cleaner!
01349     # Getting the min/max values can take some time to
01350     # calculate, and it might be worthwhile doing this step
01351     # only if we are getting them.
01352     # ############################################################
01353     if key_list.count(hdkey) < 1:
01354         # The Keyword is not in the header, nothing to GET!!!
01355         casalog.post(hdkey
01356                      + str(' is NOT in the header unable to "get" its value.'
01357                      ), 'SEVERE')
01358         return False
01359     if str(hd_values[hdkey]) == not_known:
01360         # This is a standard header keyword but we don't have a value
01361         # for it so we return "not_known"
01362         return {'unit': not_known, 'value': not_known}
01363 
01364     retValue = ''
01365 
01366     msg = ''
01367     if len(str(hd_types[hdkey])) < 1 or str(hd_types[hdkey]) \
01368         == not_known:
01369         retValue = str(hd_values[hdkey])
01370     elif str(hd_types[hdkey]) == 'list':
01371         retValue = hd_values[hdkey]
01372     else:
01373 
01374         keytype = str(hd_types[hdkey])
01375         if keytype == 'double':
01376             keytype = 'float'
01377         if keytype == 'string':
01378             keytype = 'str'
01379         retValue = eval(keytype + '("' + str(hd_values[hdkey])
01380                         + '")')
01381     retValue = {'unit': hd_units[hdkey], 'value': retValue}
01382     msg = 'Value of Header Key ' + hdkey + ' is:' + str(retValue)
01383     if len(hd_units) > 0:
01384         msg = msg + ' ' + str(hd_units[hdkey])
01385 
01386     casalog.post(msg, 'NORMAL')
01387     return retValue
01388 
01389