Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatasks/private/task_imhead.py: 18%
44 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-31 19:10 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-31 19:10 +0000
1# -*- coding: utf-8 -*-
2#######################################################################3
3# task_imhead.py
4#
5#
6# Copyright (C) 2008
7# Associated Universities, Inc. Washington DC, USA.
8#
9# This library is free software; you can redistribute it and/or modify it
10# under the terms of the GNU Library General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or (at your
12# option) any later version.
13#
14# This library is distributed in the hope that it will be useful, but WITHOUT
15# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
17# License for more details.
18#
19# You should have received a copy of the GNU Library General Public License
20# along with this library; if not, write to the Free Software Foundation,
21# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
22#
23# Correspondence concerning AIPS++ should be adressed as follows:
24# Internet email: casa-feedback@nrao.edu.
25# Postal address: AIPS++ Project Office
26# National Radio Astronomy Observatory
27# 520 Edgemont Road
28# Charlottesville, VA 22903-2475 USA
29#
30# <author>
31# Shannon Jaeger (University of Calgary)
32# </author>
33#
34# <summary>
35# CASA task for reading/writing/listing the CASA Image header
36# contents
37# </summary>
38#
39# <reviewed reviwer="" date="" tests="" demos="">
40# </reviewed
41#
42# <etymology>
43# imhead stands for image header
44# </etymology>
45#
46# <synopsis>
47# task_imhead.py is a Python script providing an easy to use task
48# for adding, removing, listing and updating the contents of a CASA
49# image. This task is very useful for fixing mistakes made in the
50# importing of FITS files into CASA images, as well as seeing what
51# checking the header to see what type of data is in the image file.
52#
53# NOTE: This task does not edit FITS files, but FITS files may
54# be created with exportuvfits task
55#
56# </synopsis>
57#
58# <example>
59# <srcblock>
60## The following code lists the keyword/value pairs found in
61## the header of the CASA image file ngc133.clean.image. The information
62## is stored in Python variable hdr_info in a Python dictionary.
63## The information is also listed in the CASA logger. The observation
64## date is #printed out from the hdr_info variable.
65# hdr_info = imhead( 'ngc4826.clean.image', 'list' )
66##print "Observation Date: ", hdr_info['date-obs']
67#
68## The following exmple displays the CASA images history in the CASA logger.
69# imhead( 'ngc4826.clean.image', 'history' )
70#
71## The following example adds a new, user-defined keyword to the
72## CASA image ngc4826.clean.image
73# imhead( 'ngc4826.clean.image', 'add', 'observer 1', 'Joe Smith' )
74# imhead( 'ngc4826.clean.image', 'add', 'observer 2', 'Daniel Boulanger' )
75#
76## The following example changes the name of the observer keyword,
77## OBSERVE, to ALMA
78# imhead( 'ngc4826.clean.image', 'put', 'telescope', 'ALMA' )
79# </srblock>
80# </example>
81#
82# <motivation>
83# To provide headering modification and reading tools to the CASA users.
84# </motivation>
85#
86# <todo>
87# </todo>
89import numpy
90import sys
91import os
93from casatools import image
94from casatools import imagemetadata
95from .. import casalog
96from .ialib import write_image_history
98def imhead(
99 imagename, mode, hdkey, hdvalue, verbose
100):
101 if mode.startswith('h') or mode.startswith('s'):
102 myia = image()
103 try:
104 myia.open(imagename)
105 if mode.startswith('h'):
106 myia.history()
107 return
108 elif mode.startswith('s'):
109 return myia.summary(verbose=verbose)
110 finally:
111 myia.done()
112 if (
113 mode.startswith('a') or mode.startswith('d')
114 or mode.startswith('g') or mode.startswith('l')
115 or mode.startswith('p')
116 ):
117 myimd = imagemetadata()
118 try:
119 myimd.open(imagename)
120 res = False
121 if mode.startswith('a'):
122 res = myimd.add(hdkey, hdvalue)
123 elif mode.startswith('d'):
124 res = myimd.remove(hdkey, hdvalue)
125 elif mode.startswith('g'):
126 return myimd.get(hdkey)
127 elif mode.startswith('l'):
128 return myimd.list(True)
129 elif mode.startswith('p'):
130 res = myimd.set(hdkey, hdvalue)
131 else:
132 raise RuntimeError('Unknown imhead mode ' + str(mode))
133 if res:
134 try:
135 param_names = imhead.__code__.co_varnames[:imhead.__code__.co_argcount]
136 vars = locals( )
137 param_vals = [vars[p] for p in param_names]
138 write_image_history(
139 imagename, sys._getframe().f_code.co_name,
140 param_names, param_vals, casalog
141 )
142 except Exception as instance:
143 casalog.post("*** Error \'%s\' updating HISTORY" % (instance), 'WARN')
144 return
145 finally:
146 myimd.done()