Read and process a Fortran90 binary file

91 Ansichten (letzte 30 Tage)
Carola Forlini
Carola Forlini am 21 Nov. 2024 um 18:30
Bearbeitet: James Tursa am 22 Nov. 2024 um 22:39
Hi,
I have a Fortran90 code where a variable called 'stream' is defined such that it stores certain variable every nstep:
open(19,file='stream',form='UNFORMATTED',position='append')
if(mod(NSTEP,100).eq.0)then
write(19)NSTEP,U,W,zeta
endif
How can I properly read the file in Matlab for postprocessing?
FYI, the fortran code is built with double precision.
Thank you
  3 Kommentare
Walter Roberson
Walter Roberson am 21 Nov. 2024 um 20:21
The FORTRAN runtime system embeds the record boundaries in the data by inserting an INTEGER*4 byte count at the beginning and end of each unformatted sequential record during an unformatted sequential WRITE. The trailing byte count enables BACKSPACE to operate on records.
Carola Forlini
Carola Forlini am 21 Nov. 2024 um 20:30
I am attaching an extract of the file.
NSTEP is an integer = 2000
U, W, zeta are matrices = 32x512
Thank you

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Les Beckham
Les Beckham am 21 Nov. 2024 um 19:14
Bearbeitet: Les Beckham am 21 Nov. 2024 um 19:15
It would help to have a sample file to experiment with.
Nevertheless, assuming that the write call that you have shown is the only thing that writes to this file in your Fortran code, I would suggest something like this using fread and reshape:
fid = fopen('your_file_name.ext', 'rb');
A = fread(fid);
data = reshape(A, 4, numel(A)/4)';
nstep = A(:,1);
U = A(:,2);
W = A(:,3);
zeta = A(:,4);
Note that you might run into endian-ness issues if your Fortran code runs on a different architecture than your Matlab is running on. See the documentation for the machinefmt option to fread if necessary.
  5 Kommentare
Les Beckham
Les Beckham am 22 Nov. 2024 um 14:04
Thanks for adding the example.
Can you clarify what you mean by this?
NSTEP is an integer = 2000
Especially the "= 2000" part. Is that just an example of a possible value? What is the size of this integer (number of bits)?
Also, are U, W, and zeta double precision (64 bit) floating point 2-d arrays with the given size?
Carola Forlini
Carola Forlini am 22 Nov. 2024 um 14:54
I apologize for the misunderstanding.The size of NSTEP is 32 bit.
U, W, zeta are single precision (32 bit) of size N1×N3, with N1=32 and N3=512.

Melden Sie sich an, um zu kommentieren.


Les Beckham
Les Beckham am 22 Nov. 2024 um 19:29
Bearbeitet: Les Beckham am 22 Nov. 2024 um 19:30
In light of the additional variable size information and sample file provided by the OP, and the information from Walter about Fortran inserting record boundary byte counts at the beginning and end of each record written with an unformatted sequential WRITE, here is a fresh answer.
Note that this makes an assumption about the order the data is written (column major order). If this assumption is not correct, the inner for loop will need to be modified accordingly.
Hopefully this is close enough to get you where you need to go.
filenames = unzip('stream.zip')
filenames = 1x1 cell array
{'stream'}
fid = fopen(filenames{1}, 'rb');
% start/stop byte counts (4 bytes each) + NSTEP (4 bytes) + U, W, zeta (3*32*512*4 bytes)
recordSize = 2*4 + 4 + 3*32*512*4;
fseek(fid, 0, 'eof'); % seek to the end of the file
nBytes = ftell(fid); % find number of bytes in the file
frewind(fid); % go back to the beginning of the file
nRecords = nBytes / recordSize;
% preallocate
nStep = zeros(nRecords, 1); % column vector
U = zeros(3, 512, nRecords); % 3D arrays
W = zeros(3, 512, nRecords);
zeta = zeros(3, 512, nRecords);
for iRecord = 1:nRecords
fseek(fid, 4, 'cof'); % skip begin record byte count field
nStep(iRecord) = fread(fid, 1, 'integer*4');
for iRow = 1:3
U(iRow, :, iRecord) = fread(fid, 512, 'real*4');
W(iRow, :, iRecord) = fread(fid, 512, 'real*4');
zeta(iRow, :, iRecord) = fread(fid, 512, 'real*4');
end
fseek(fid, 4, 'cof'); % skip end record byte count field
end
fclose(fid); % close the file
whos nStep U W zeta % check variable sizes
Name Size Bytes Class Attributes U 3x512x21 258048 double W 3x512x21 258048 double nStep 21x1 168 double zeta 3x512x21 258048 double
plot(nStep); % make a plot see if nStep makes sense
grid on
Well, I don't know what your NSTEP should look like, but I would have expected a monotonically increasing count. So, either I made a mistake, or there is some additional information needed. It does appear that the size information you gave is either correct, or just coincidentally came out to be an integer divisor of the file size.
  4 Kommentare
Les Beckham
Les Beckham am 22 Nov. 2024 um 21:59
Good catch, Voss. Thanks. I don't know why I switched from using 32 in the number of records calculation, to using 3 in the section where I did the preallocation and the reads. I should have spent more time reviewing my answer before submitting it, as it was obvious that something was wrong.
James Tursa
James Tursa am 22 Nov. 2024 um 22:38
Bearbeitet: James Tursa am 22 Nov. 2024 um 22:39
"... this makes an assumption about the order the data is written (column major order) ..."
Fortran is column-major storage order, same as MATLAB. When you specify just the variable name in a read/write statement, that is the element order for the read/write.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by