# [AstroPy] Moving Ahead with Raw Image Conversion

Peter Erwin erwin@mpe.mpg...
Mon Apr 13 15:17:36 CDT 2009

```Hi Wayne,

Pyfits will want a numpy array of integers (or floats).  You can get a
numpy array of
either integer or floats with the numpy.array() function.

Here's a short example (sort-of based on your "make_image" function),
using integers:

import numpy, pyfits

# create a 2D integer array with 2 rows (a normal Python list-of-lists)
int_array = []
for j in range(2):
new_row = []
for k in range(5):
new_row.append(k + j*5)
int_array.append(new_row)

>>> print int_array
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]

# make a numpy array version of that Python list-of-lists
int_array_numpy = numpy.array(int_array)

>>> print int_array_numpy
[[0 1 2 3 4]
[5 6 7 8 9]]

# save this numpy array as a FITS image (based on p.10 of the PyFits
1.0 Manual):
hdu = pyfits.PrimaryHDU(int_array_numpy)
# [ at this point you might optionally modify the header (e.g., add
date, lat and long, etc.) ... ]
# ... and save the data to file:
hdu.writeto("simple_array.fits")

Now, the problem is that what I've done is to create a Python array of
*integers*,
then converted it to numpy form.  In your example code, you've created
a Python
array of hexadecimal-valued bytes -- which I guess is what you get
"raw" image, so that's OK.  But you still have to convert those
'\x01', etc., values
to Python integers.  Since I basically never work with raw bytes or
coding, I don't actually know how to do this... but I imagine other
people on the
list do.

cheers,

Peter

On Apr 13, 2009, at 6:38 PM, Wayne Watson wrote:

> Hi, not familiar with much of anything in numpy, then again, I'm not
> anxious to read through the 300+ page manual. :-)
> Well, let's see if I can explain this in py-talk. Here's a small
> program I wrote to get the raw image
>
> #import Image
> import string
>
> def make_array():
>   im_array=[]
>   im_data=range(10)
>   print im_data
>   for j in range(2):
>       row=[]
>       for k in range(5):
>           item=hex(int(im_data[k+j*5]))
>           row.append(item)
>       im_array.append(row)
>   print im_array
>
> raw_file=open('sent_internal.raw','rb')
> raw_file.close()
> print 'info:', len(raw_image), type(raw_image)
> make_array()
>
> make_array, instead of working on the 307200 byte(640x480) string
> representing the image, is used to see if I can generate a 2x5 array
> like that apparently needed to represent the image to pyfits. The
> above produces this:
> info: 307200 <type 'str'>
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  <--- my fictional 2x5 image as a py
> list.
> [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]   <--- the type of structure I
> think numarray(numpy equivalent) wants.
>
> I'm looking at page 7 of the FITS manual, and I see in the middle,
> hdulist[1].data. This appears to be where the data(image) goes. At
> the top of page 10, the steps to construct a simple fits file are
> shown. numarray is used. Simply put, how do I proceed from here?
>
>
> I've attached the 'sent_internal.raw' file. It may cause this msg to
> be rejected by the mail list.
>
> Jim Vickroy wrote:
>> Wayne Watson wrote:
>>
>>> I've pretty much gotten my 640x480 1 byte, "raw" image under
>>> control within Python Image and Python file use. I wrote a file of
>>> 307200 bytes in bytes, byte representation of characters. I
>>> couldn't see anything within pyfits or numpy that would help me do
>>> that. Now I believe I'm in a position to use pyfits to create a
>>> fits format file. The next step is to arrange the image (data)
>>> into a 640x480 array. I can do that in python, but would like to
>>> know how it can be done in numpy, for the experience. The IDA
>>> tutorial talks to making arrays, but I do not see where they work
>>> with creating an array with fresh data. Once that's done, then I
>>> should be able to work out from a reading of page 10 in the pyfits
>>> manual.
>>>
>>>
>> Hello Wayne,
>>
>> Could you post a sample script illustrating exactly what you want
>> to do along with some sample output showing how it is failing to do
>> so?
>>
>> Are you familiar with the numpy.ndarray.reshape() procedure?
>>
>> -- jv
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy@scipy.org
>> http://mail.scipy.org/mailman/listinfo/astropy
>>
>>
>
> --
>
>            (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
>             Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
>          All the neutrons, and protons in the human body
> occupy           a cube whose side is 5.52*10**-6 meters (tiny!).
> That           adds up to a 150 pound person. It's not a surprise that
>          we are mostly space. (Calculation by WTW)
>
> <sent_internal.raw>_______________________________________________
> AstroPy mailing list
> AstroPy@scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy

=============================================================
Peter Erwin                   Max-Planck-Insitute for Extraterrestrial
erwin@mpe.mpg.de              Physics, Giessenbachstrasse
tel. +49 (0)89 30000 3695     85748 Garching, Germany
fax  +49 (0)89 30000 3495     http://www.mpe.mpg.de/~erwin

```