0

So I have multiple files that can be accessed and be treated as 2D arrays. What I would like to do is take all those 2D arrays and put them in a single 3D array. For example, if I have 10 files with the shapes (100,100), when I combine them, I should be left with a 3D array of shape (10,100,100). The following attempt I have is the following:

filenames = glob.glob('source')
preset = np.empty([100,100], dtype = 'int16')
for file in filenames:
    data = fits.open(file)[0].data
    np.vstack([preset,data]).reshape((10,100,100))

But what I'm getting is the following error:

ValueError: cannot reshape array of size 20000 into shape (10,100,100)

1 Answer 1

1

You are performing the operation pair by pair. Try to perform this on all the arrays together:

arrs = [fits.open(file)[0].data for file in filenames]
np.vstack(arrs).reshape((10,100,100))

Or even more direct:

np.stack(arrs)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.