Last modified: November 2023

URL: https://cxc.cfa.harvard.edu/ciao/ahelp/make_image_crate.html
AHELP for CIAO 4.16

make_image_crate

Context: contrib

Synopsis

Create an IMAGECrate from an array.

Syntax

make_image_crate(pixvals)

The return value is an IMAGECrate.

Description

This routine provides a quick means of creating an IMAGE Crate from an array of values.

Loading the routine

The routine can be loaded into Python by saying:

from crates_contrib.utils import *

Writing the data out to file

The write_file() command can be used to write the data out to file in FITS binary or ASCII formats.


Examples

Example 1

>>> cr = make_image_crate([1, 2, 3, 4, 5])
>>> write_file(cr, "img.fits")

The one-dimensional array of integers 1 to 5 is written out to the file img.fits:

>>> !dmlist img.fits blocks
 
--------------------------------------------------------------------------------
Dataset: img.fits
--------------------------------------------------------------------------------
 
     Block Name                          Type         Dimensions
--------------------------------------------------------------------------------
Block    1: IMAGE                          Image      Int4(5)

Example 2

>>> (y, x) = np.mgrid[1:251, 1:201]
>>> dx = x - 100
>>> dy = y - 120
>>> a = np.pi * 3 / 4
>>> xn = dx * np.cos(a) + dy * np.sin(a)
>>> yn = dy * np.cos(a) - dx * np.sin(a)
>>> r2 = (xn**2 * 0.8**2 + yn**2) / 0.8**2
>>> z = np.exp(-r2 / (20*20))
>>> cr = make_image_crate(z)
>>> cr.name = 'ELGAUSS'
>>> write_file(cr, 'z.img')

Here we create a 2D elliptical gaussian, centered at (100,120) with an ellipticity of 0.2 and rotated by 135 degrees, within an array of size (200,250). The mgrid call creates two 2-D arrays set to the x and y coordinates of the array; we start at 1 rather than 0 since the FITS coordinate system has 1,1 as the center of the lower-left pixel. The formula for the gaussian is taken from "ahelp gauss2d".

>>> !dmstat z.img 
ELGAUSS(X, Y)
    min:	4.3649283641e-46 	      @:	( 200 250 )
    max:	1 	      @:	( 100 120 )
cntrd[log] :	( 100 120 )
cntrd[phys]:	( 100 120 )
sigma_cntrd:	( 14.292603045 17.109341909 )
   good:	50000 
   null:	0 

Example 3

>>> ds = CrateDataset()
>>> ivals = np.arange(12).reshape(3,4)
>>> col1 = np.arange(1, 5)
>>> rng = np.random.default_rng()
>>> col2 = integers(10, 20, size=col1.size)
>>> cr1 = make_image_crate(ivals)
>>> cr2 = make_table_crate(col1, col2, colnames=['INDEX', 'RANDVAL'])
>>> add_crate(ds, cr1)
>>> add_crate(ds, cr2)
>>> write_file(ds, 'blocks.fits')

Here we use a CrateDataset object to store two blocks - an image and then a table - which is written out to blocks.fits:

>>> !dmlist blocks.fits blocks
 
--------------------------------------------------------------------------------
Dataset: blocks.fits
--------------------------------------------------------------------------------
 
     Block Name                          Type         Dimensions
--------------------------------------------------------------------------------
Block    1: IMAGE                          Image      Int4(4x3)
Block    2: TABLE                          Table         2 cols x 4        rows

The NumPy integers method is used to create random integers betwen 10 and 19 (inclusive).


Adding or changing metadata

The crate has a name of 'IMAGE' and contains two keywords, CREATOR and DATE. These can be changed, or other values added, using Crates routines.

>>> cr.name = 'HISTVALS'

will change the block name.

Keywords

The set_key() routine is an easy way to add or edit a keyword, while get_key() and add_key() provide a more powerful way of handling keywords. The delete_key() routine removes keywords.


Bugs

See the bug pages on the CIAO website for an up-to-date listing of known bugs.

Refer to the CIAO bug pages for an up-to-date listing of known issues.

See Also

contrib
make_table_crate, scale_image_crate, smooth_image_crate, write_arrays, write_columns
crates
add_col, add_key, add_piximg, delete_col, delete_key, delete_piximg, read_file, read_pha, read_rmf, write_file, write_pha, write_rmf