0

I am writing a python code to calculate the background of an astronomical image of globular cluster M15 (M15 reduced). My code can calculate the background and plot it using plt.imshow(). To save the background subtracted image I have to convert it to a str from a numpy.nparray. I have tried many things including the np.array2string used here. The file just stays as an array, which can't be saved as I need it to save as a .fits file. Any ideas how to get this to a str? The code:

#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
    Possible duplicate of [Write 3d Numpy array to FITS file with Astropy](https://stackoverflow.com/questions/31887478/write-3d-numpy-array-to-fits-file-with-astropy) – Lior Cohen Oct 28 '19 at 16:12
  • Looks like `M15subback` is an array, which doesn't have a `write` method. Usually it's a file like object that has a write method, not an array, list, or string. – hpaulj Oct 28 '19 at 16:46
  • And `array2string` does not operate in place; it returns a string. It is difficult, if not impossible, to recreate an array from the result of `array2string`. Don't use to save an array! – hpaulj Oct 28 '19 at 16:51
  • What's a `fits` file? I see you use `fits` in the name of the file, but that doesn't define the save method. If this is an `astropy` format, then tag the question appropriately, and use an `astropy` function. There's nothing in `numpy` that uses this format. – hpaulj Oct 28 '19 at 17:41

1 Answers1

1

You could try using [numpy.save][1] (but it saves a '.npy' file). In your case,

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

Since you need to store a numpy array, this should work.

Avinash
  • 133
  • 9