2

I have a 1024^3 data set to read. But this array is too large, so I want to save the data every 2^n points. For example, if I set skip = 2, then the array will be 512^3.

import numpy as np
nx = 1024
filename = 'E:/AUTOIGNITION/Z1/Velocity1_inertHIT.bin'
U = np.fromfile(filename, dtype=np.float32, count=nx**3).reshape(nx, nx, nx)

How do I do this by using reshape?

Hyejin
  • 29
  • 2

1 Answers1

0

If I understand correctly, you want to sample every (2^n):th value of the array, in each of the 3 dimensions.

You can do this by indexing as follows.

>>> import numpy as np
>>> n = 1
>>> example_array = np.zeros((1024,1024,1024))
>>> N = 2^n
>>> example_array_sampled = example_array[::N, ::N, ::N]
>>> example_array_sampled.shape
(512, 512, 512)

Verifying that it actually samples evenly:

>>> np.arange(64).reshape((4,4,4))[::2,::2,::2]
array([[[ 0,  2],
        [ 8, 10]],

       [[32, 34],
        [40, 42]]])

This particular code still assumes that you will read in the whole array in memory before indexing into it. And the variable example_array_sampled will be a view into the original array.

More info on indexing numpy arrays: https://numpy.org/doc/stable/reference/arrays.indexing.html

Some info on numpy views: https://scipy-cookbook.readthedocs.io/items/ViewsVsCopies.html

Related question (in one dimension): subsampling every nth entry in a numpy array

wjakobw
  • 525
  • 1
  • 4
  • 11