Headers

In this section we will see how to manipulate HDU headers

Keys: astropy.io.fits, dictionaries, iterable objects, loop for and None

Select the HDU

In [1]:
#Import the module
from astropy.io import fits

#Set the file name to open
filename = 'data/header.fits'

# Open the MEF, without load it
hdulist = fits.open(filename)

#Get the first extention
image = hdulist[0]
In [ ]:
 

display the header

In [2]:
image.header
Out[2]:
SIMPLE  =                    T / conforms to FITS standard                      
BITPIX  =                   16 / array data type                                
NAXIS   =                    2 / number of array dimensions                     
NAXIS1  =                   70                                                  
NAXIS2  =                   40                                                  
EXTEND  =                    T                                                  
BSCALE  =                    1                                                  
BZERO   =                32768                                                  
KEY_B   = T                                                                     
KEY_S   = 'TEST STR'                                                            
KEY_I   =                    1 / This is Integer keyword                        
COMMENT  The comment to this file starts here                                   
COMMENT  Continues here....                                                     
COMMENT  ...and here stops. Bye                                                 
KEY_F   =                 3.14 / This is Integer too                            
In [ ]:
 

The header keys

In [3]:
header = image.header 

#Header keys is a generator object
type(header.keys())

#Display all the avaiable keywords
list(header.keys())
Out[3]:
['SIMPLE',
 'BITPIX',
 'NAXIS',
 'NAXIS1',
 'NAXIS2',
 'EXTEND',
 'BSCALE',
 'BZERO',
 'KEY_B',
 'KEY_S',
 'KEY_I',
 'COMMENT',
 'COMMENT',
 'COMMENT',
 'KEY_F']
In [4]:
header = image.header 
type(header.keys())
list(header.keys())
Out[4]:
['SIMPLE',
 'BITPIX',
 'NAXIS',
 'NAXIS1',
 'NAXIS2',
 'EXTEND',
 'BSCALE',
 'BZERO',
 'KEY_B',
 'KEY_S',
 'KEY_I',
 'COMMENT',
 'COMMENT',
 'COMMENT',
 'KEY_F']

Dictionaries

  • a dictionary consists of a collection of key-value pairs.
  • each key-value pair maps the key to its associated value.
  • we can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces {}. A colon : separates each key from its associated value
In [5]:
#Define the dictionary
a = {"first": 1, "second": [1,2,3], (4,5): None}
  • add key/value
In [6]:
a["new"] = "hello"
  • a given key can appear in a dictionary only once, duplicate keys are not allowed.
In [7]:
a["new"]=print 
  • remove key/value
In [8]:
del a["first"]
  • a dictionary key must be of a type that is immutable (more precisely must be hashable)
In [ ]:
b={[1,2]: 4} #Error: list is unhashable, can not be a key!
  • loop over keys and values
In [9]:
a.keys()
Out[9]:
dict_keys(['second', (4, 5), 'new'])
In [10]:
a.values()
Out[10]:
dict_values([[1, 2, 3], None, <built-in function print>])
In [11]:
for key in a:
    print(key, a[key])
second [1, 2, 3]
(4, 5) None
new <built-in function print>

For loop

typical C/Fortran for loop

for (k=0; k<30; ++k){
    <loop body>
}
  • an initialization
  • an expression specifying an ending condition
  • an action to be performed at the end of each iteration.

typical Python for loop

for i in <collection>
    <loop body>
  • python: collection-Based or Iterator-Based Loop
  • this type of loop iterates over a collection of objects, rather than specifying numeric values or conditions

Examples

In [ ]:
for key in a:
    print(key, a[key])
    
for item in (1, 2, "alpha", [10, 20, 30]):
    print(item)

for item in range(4):
    print(item)
    
for k in range(2, 10, 3):
    print(k)
In [ ]:
 

None object

This type has a single value. There is a single object with this value. This object is accessed through the built-in name None. It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything

PEP8: Comparisons to singletons like None should always be done with is or is not, never the equality operators

In [ ]:
a=None
a is None
a is not None

Keyword manipulation (dictionary like)

  • retrieve single keyword value
In [12]:
header["KEY_F"]
Out[12]:
3.14
  • modify keyword value
In [13]:
header["KEY_F"]=7
header["KEY_F"]*=2
  • rename keyword
In [14]:
header.rename_keyword("KEY_B", "KEY_BOOL")
  • delete keyword
In [15]:
del header["KEY_F"]
"KEY_F" in header
Out[15]:
False
  • add keyword
In [16]:
header["KEY_REAL"] = 2.7

Comments

  • get the single keyword comment
In [17]:
header.comments["KEY_I"]
Out[17]:
'This is Integer keyword'
  • modify keyword comment
In [18]:
header.comments["KEY_I"] = "new comment"
header.comments["KEY_I"]
Out[18]:
'new comment'
  • comment or history records
In [19]:
header["COMMENT"][0]
Out[19]:
' The comment to this file starts here'

save

In [20]:
hdulist.writeto("tmp/t2.fits", overwrite=True)
header.totextfile("tmp/header.txt", overwrite=True)
In [ ]:
 
In [21]:
#Close the file
hdulist.close()
In [ ]: