将泊松噪声添加到拟合文件中

时间:2013-10-17 23:32:46

标签: python image poisson fits

我正在尝试将增量的泊松噪声添加到.fits文件中。我知道如何为常规文件类型执行此操作,但我似乎无法读取匹配,然后添加泊松噪声。有人知道怎么做吗?

这是代码。其中大部分并不是特别相关。

s=str(raw_input("filter name: "))
t=str(raw_input("sci or wht: "))
poisson = str(raw_input("Poisson noise amount: "))
for i in range(0,len(ra_new)):
    ra_new2=cat['ra'][z2&lmass2&ra2&dec2][i]
    dec_new2=cat['dec'][z2&lmass2&ra2&dec2][i]
    id_new=cat['id'][z2&lmass2&ra2&dec2][i]
    target_pixel_x = ((ra_new2-ra_ref)/(pixel_size_x))+reference_pixel_x       
    target_pixel_y = ((dec_new2-dec_ref)/(pixel_size_y))+reference_pixel_y   
    fig = plt.figure(figsize=(5.,5.))
    timage=img[target_pixel_y-65:target_pixel_y+65,target_pixel_x-65:target_pixel_x+65]
    plt.imshow(img[target_pixel_y-65:target_pixel_y+65,target_pixel_x-65:target_pixel_x+65], vmin=-0.01, vmax=0.1, cmap='Greys')

galimage =  pf.writeto(t+'PHOTO'+s+str(i)+'.fits',timage,clobber=True,header=hdr)
        imagea = (scipy.misc.imread(galimage)).astype(float)
        poissonNoise = numpy.random.poisson(poisson,imagea.shape).astype(float)
        noisyImage = imagea + poissonNoise
        pf.writeto(t+'POISSONPHOTO'+s+str(i)+poisson+'.fits',timage,clobber=True,header=hdr)
        lmass3=cat['lmass'][z2&lmass2&ra2&dec2][i]
        print id_new, ra_new2,dec_new2

1 个答案:

答案 0 :(得分:1)

我是否正确地解释了您的问题?使用pyfits

import numpy
import scipy
import pyfits

# Use Pyfits to read in a file.
im = pyfits.open("example.fits")

# pyfits opens a "list" of all header extensions in the file.
isinstance(im,list)

# my example is a simple 2d image. so I want the first header unit
im0 = im[0]

# you access the data shape this way
print im0.data.shape

# simple check of the image variance
print numpy.var(im0.data)

# now I'm just repeating the same as your example
poisson = str(raw_input("Poisson noise amount: "))
poissonNoise = numpy.random.poisson(poisson, im0.data.shape).astype(float)
test = im0.data + poissonNoise

# check things out and write to new file
print numpy.var(test)

# you could do this.
im0.data = test

# write that new image back out with the old header. 
pyfits.writeto("/tmp/test.fits", data=test, header=im0.header)

# prove it worked. 
check = pyfits.open("/tmp/test.fits")

numpy.var(check[0].data)