What I am basically trying to do is convert from some MATLAB code to Python:
The MATLAB code:
for time = 100:model_times
for i = 1:5
indat = fread(fid,[48,40],'real*4');
vort(:,:,i,time) = indat';
end
end
fid holds the file path (a DAT file) is being used. vort is a preallocated as: vort = zeros(40,48,5,model_times). model_times is a fixed integer (e.g. 100).
What seems to be happening is that the .dat file data is being read in as a 48x40 matrix, then inserted into the preallocated array vort, at a fixed i and time (the loop counters).
I have attempted this in Python:
for time in range(model_times):
for i in range(5):
vort[:,:,i,time] = np.fromfile(fid,np.float64)
I receive an error that says, "ValueError: operands could not be broadcast together with shapes (40,48) (6048000)". The error occurs on the last line of Python code above. I have also tried adding .reshape((40,48,5,model_times)) to the line with the error, but receive another error that says "ValueError: total size of new array must be unchanged."
So my question is, what is the Python equivalent to MATLAB's "fread", that can handle multidimensional arrays?
On a scale from 1 to 10, with 1 being a total beginner and 10 being a seasoned veteran, I'm about a 4.