[AstroPy] Creating a new Image file in astropy with specified dimensions of another image file and then applying corrections
Roberts, Michael
michael.roberts.15 at ucl.ac.uk
Sun Feb 14 11:57:47 EST 2016
I'm having a problem wit fits file manipulation in the astropy package, and I'm in need of some help. I'm trying to convert an old IDL script into python.
I essentially want to take an image I have in fits file format, and create a new file I need to start inputing correction factors to and a new image which can then be used with the correction factors and the original image to produce a correction image. Each of these will have the same dimensions.
This is where I am at thus far:
import numpy as numpy
from astropy.io import fits
#URL: to M33_sum_epoch1_um2_norm.img : https://www.dropbox.com/s/q7e14uosaj5u9en/M33_sum_epoch1_um2_norm.img?dl=0
ima_sk_um2 = fits.open('/Users/UCL_Astronomy/Documents/UCL/PHASG199/M33_UVOT_sum/UVOTIMSUM/M33_sum_epoch1_um2_norm.img')
# Compute the size of the images (you can also do this manually rather than calling these keywords from the header):
nxpix_um2_ext1 = ima_sk_um2[1].data['NAXIS1'] # IDL: nxpix_uw1_ext1 = sxpar(hima_sk_uw1_ext1,'NAXIS1')
nypix_um2_ext1 = ima_sk_um2[1].data['NAXIS2'] # IDL: nypix_uw1_ext1 = sxpar(hima_sk_uw1_ext1,'NAXIS2')
# Make a new image file to save the correction factors:
corrfact_um2_ext1 = numpy.array(nxpix_um2_ext1,nypix_um2_ext1)
hdu_corrfact = ImageHDU(corrfact_um2_ext1)
HDUList([hdu_corrfact]).writeto('/Users/UCL_Astronomy/Documents/UCL/PHASG199/M33_UVOT_sum/UVOTIMSUM/M33_sum_epoch1_um2_corrfact.img')
# Make a new image file to save the corrected image:
ima_sk_coincorr_um2_ext1 = numpy.array(nxpix_um2_ext1,nypix_um2_ext1)
hdu_coincorr = ImageHDU(ima_sk_coincorr_um2_ext1)
HDUList([hdu_coincorr]).writeto('/Users/UCL_Astronomy/Documents/UCL/PHASG199/M33_UVOT_sum/UVOTIMSUM/M33_sum_epoch1_um2_coincorr.img')
# Define the variables from Poole et al. (2008) "Photometric calibration of the Swift ultraviolet/optical telescope":
alpha = 0.9842000
ft = 0.0110329
a1 = 0.0658568
a2 = -0.0907142
a3 = 0.0285951
a4 = 0.0308063
for i in range(nxpix_um2_ext1-1): #do begin
for j in range(nypix_um2_ext1-1): #do begin
if (numpy.less_equal(i, 4) | numpy.greater_equal(i, nxpix_um2_ext1-4) | numpy.less_equal(j, 4) | numpy.greater_equal(j, nxpix_um2_ext1-4)): #then begin
#UVM2
corrfact_um2_ext1[i,j] == 0
ima_sk_coincorr_um2_ext1[i,j] == 0
else:
xpixmin = i-4
xpixmax = i+4
ypixmin = j-4
ypixmax = j+4
#UVM2
ima_UVM2sum = total(ima_sk_um2[xpixmin:xpixmax,ypixmin:ypixmax])
xvec_UVM2 = ft*ima_UVM2sum
fxvec_UVM2 = 1 + (a1*xvec_UVM2) + (a2*xvec_UVM2*xvec_UVM2) + (a3*xvec_UVM2*xvec_UVM2*xvec_UVM2) + (a4*xvec_UVM2*xvec_UVM2*xvec_UVM2*xvec_UVM2)
Ctheory_UVM2 = - alog(1-(alpha*ima_UVM2sum*ft))/(alpha*ft)
corrfact_um2_ext1[i,j] = Ctheory_UVM2*(fxvec_UVM2/ima_UVM2sum)
ima_sk_coincorr_um2_ext1[i,j] = corrfact_um2_ext1[i,j]*ima_sk_um2[i,j]
This is essentially a lot of muddling between IDL, python and astropy.
In essence I want to have a new .img file that has the same dimensions as the original file but with the correction factors calculated in the loop as the new image array, and then the coincidence correction as the two multiplied together, so the correction factor times the image input.
A dropbox link to the image file is in the script, so hopefully that should be self-containing.
Hopefully there is someone here that knows how to guide me on this.
Many thanks,
Michael Roberts
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20160214/672b2fa3/attachment.html>
More information about the AstroPy
mailing list