'numpy.ndarray'对象没有属性'write'

时间:2019-10-28 16:04:09

标签: python-3.x numpy

我正在编写一个python代码来计算球状星团M15(缩小了M15)的天文图像的背景。我的代码可以计算背景,并使用plt.imshow()对其进行绘制。为了保存减去背景的图像,我必须将其从numpy.nparray转换为str。我已经尝试了很多东西,包括这里使用的np.array2string。该文件只是保留为数组,无法保存,因为我需要将其另存为.fits文件。有什么想法如何使它发挥作用吗? 代码:

#sigma clip is the number of standard deviations from centre value that value can be before being rejected
sigma_clip = SigmaClip(sigma=2.)
#used to estimate the background in each of the meshes
bkg_estimator = MedianBackground()
#define path for reading in images
M15red_path = Path('.', 'ObservingData/M15normalised/')
M15red_images = ccdp.ImageFileCollection(M15red_path)
M15reduced = M15red_images.files_filtered(imagetyp='Light Frame', include_path=True)
M15backsub_path = Path('.', 'ObservingData/M15backsub/')
for n in range (0,59):
    bkg = Background2D(CCDData.read(M15reduced[n]).data, box_size=(20,20), 
                   filter_size=(3, 3), 
                   edge_method='pad', 
                   sigma_clip=sigma_clip, 
                   bkg_estimator=bkg_estimator)
    M15subback = CCDData.read(M15reduced[n]).data - bkg.background
    np.array2string(M15subback)
    #M15subback.write(M15backsub_path / 'M15backsub{}.fits'.format(n))

    print(type(M15subback[1]))

1 个答案:

答案 0 :(得分:0)

您可以尝试使用[numpy.save] [1](但它会保存一个'.npy'文件)。就您而言,

import numpy as np
...
for n in range (0,59):
    ...
    np.save('M15backsub{}.npy'.format(n), M15backsub)

由于您需要存储一个numpy数组,因此应该可以。