Skip to content

Commit cad986c

Browse files
authored
Convert data from view into copy (#153)
1 parent 11bbd72 commit cad986c

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

astrocut/ASDFCutout.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ def _get_cutout_data(self, data: np.ndarray, wcs: WCS, pixel_coords: Tuple[int,
226226

227227
log.debug('Image cutout shape: %s', img_cutout.shape)
228228

229+
# Data in the Cutout2D object is a view into the original data. We need to deep copy the
230+
# data in the cutout object to ensure that it is fully independent of the original data
231+
# and that it does not take up the same amount of storage.
232+
img_cutout.data = np.array(img_cutout.data, copy=True)
233+
229234
return img_cutout
230235

231236
def _slice_gwcs(self, cutout: Cutout2D, gwcs: gwcs.wcs.WCS) -> gwcs.wcs.WCS:

astrocut/tests/test_ASDFCutout.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def test_asdf_cutout_write_to_file(test_images, center_coord, cutout_size, tmpdi
159159
assert 'meta' in af.tree['roman']
160160
assert np.all(af.tree['roman']['data'] == cutout.cutouts[i].data)
161161
assert af.tree['roman']['meta']['wcs'].pixel_shape == (10, 10)
162+
assert Path(asdf_file).stat().st_size < Path(test_images[i]).stat().st_size
162163

163164
# Write cutouts to FITS files on disk
164165
cutout = ASDFCutout(test_images, center_coord, cutout_size)
@@ -169,6 +170,7 @@ def test_asdf_cutout_write_to_file(test_images, center_coord, cutout_size, tmpdi
169170
assert np.all(hdul[0].data == cutout.cutouts[i].data)
170171
assert hdul[0].header['NAXIS1'] == 10
171172
assert hdul[0].header['NAXIS2'] == 10
173+
assert Path(fits_file).stat().st_size < Path(test_images[i]).stat().st_size
172174

173175

174176
def test_asdf_cutout_partial(test_images, center_coord, cutout_size):

0 commit comments

Comments
 (0)