[AstroPy] Moving Ahead with Raw Image Conversion

Wayne Watson sierra_mtnview at sbcglobal.net
Tue Apr 14 00:28:29 EDT 2009

Hi, Peter. It looks like I confused a few people with the hex. I had 
removed it, but only after I placed the code in the msg. I was playing 
with hex in the event I might need it somehow. int was fine for my 
experimental purposes.

Here's your code with a minor correction. I changed this line:
   rawim_numpy_2d = numpy.reshape(rawim_numpy,(480, 640))
The difference is rawmin_numpy.
I added a print at the end, and threw in two lines that attempt to put 
header info into the final file.  Continuing below the code.

import struct, numpy, pyfits   # struct is part of the main Python library

# get raw data from .raw file:

# convert raw data to Python list of integers (1-D):
rawim_intarray = []
for x in raw_image:
    # convert byte to integer ('B' = treat data as single unsigned byte)
    newint = struct.unpack('B', x)[0]

# convert Python list of integers into numpy array of integers (note: 
still 1-D at this point)
rawim_numpy = numpy.array(rawim_intarray)

# reshape numpy array into 2D form, using our knowledge of the original 
# x and y sizes (kudos to Megan Sosey for pointing out how to do this)
rawim_numpy_2d = numpy.reshape(rawim_numpy,(480, 640))

# create Pyfits header-data unit:
hdu = pyfits.PrimaryHDU(rawim_numpy_2d)

# (make any modifications to the header you might want to... e.g., see 
Megan Sosey's
# email of April 8 for examples)

# ... and save the data to disk as a FITS file:
print "Finished. See test.fits"
===============End code============

I'm not sure what the 'byte' stuff is about. It may be that it's not 
needed. I think the original 'raw' file is byte oriented--rather than 
int or whatever. Anyway,  I don't seem to have succeeded in getting 
header info into the file. Sort of. If I use DS9, I can see the image 
and the fits header with what I coded into it. Here's what I see with ds9.

*However, with my ccdsoft program, te image is black and the header is not the same. Whoops. Interesting. I changed the file to test.fit instead of test.fits, and the header info is there, but the image is still black. The dimension are the same, 640x480. I'm attaching test-ww.fits. I just renamed my test.fits. I think I'll check with The Bisque (ccdsoft) to see what format they produce and use for the raw image. 

Question. When I execute the program a second time, the *hdu.writeto("test.fits") *will not write over the old file. Is there an option to force the write?

Well, this is all good progress. 
**  *


           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (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)

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test-ww.fits
URL: <http://mail.python.org/pipermail/astropy/attachments/20090413/ca215bd8/attachment.ksh>

More information about the AstroPy mailing list