Convert Fortran code to Matlab

5 Ansichten (letzte 30 Tage)
Kalore Shubham Arun
Kalore Shubham Arun am 3 Jul. 2020
Hi, I need help in converting the following Fortran code to Matlab
* This program reads binary data for 365/366 days and writes in ascii file.
PROGRAM READ
PARAMETER(ISIZ=31,JSIZ=31)
DIMENSION T(366,ISIZ,JSIZ)
OPEN(1,FILE='D:\\DailyT\\MeanT\\MEANT1980.GRD',
1 FORM='UNFORMATTED',ACCESS='DIRECT',
2 RECL=ISIZ*JSIZ*4,STATUS='OLD')
OPEN(2,FILE='D:\\DAILYT\\MEANT15APR1980.TXT',STATUS='UNKNOWN')
* TAKE NDAY=366 FOR LEAP YEARS
NDAY=366
DO IDAY = 1,NDAY
READ(1,REC=IDAY)((T(IDAY,I,J),J=1,JSIZ),I=1,ISIZ)
ENDDO
WRITE(2,'('' Daily Tempereture for 15 APR 1980 '')')
DO I = 1,ISIZ
WRITE(2,'(31F6.2)')(T(106,I,J),J=1,JSIZ)
ENDDO
STOP
END
Thanks
Shubham
  2 Kommentare
Walter Roberson
Walter Roberson am 3 Jul. 2020
I will need the sample input file to test with.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 3 Jul. 2020
1 FORM='UNFORMATTED',ACCESS='DIRECT',
There is no standard for the representation of DIRECT UNFORMATTED files in fortran. The most common implementation is as a binary file in which each record is proceeded and followed by a 4 byte record length count.
You should probably be doing something like
NDAY = 366;
ISIZ = 31; JSIZ = 31;
T = zeros(NDAY, ISIZ, JSIZ, 'single');
fid = fopen('D:/DailyT/MeanT/MEANT1980.GRD');
for IDAY = 1 : NDAY
fseek(fid, 4, 'cur');
T(IDAY, :, :) = fread(fid, [JSIZ ISIZ], '*single') .';
fseek(fid, 4, 'cur');
end
fclose(fid);
After which you would write the data to a file, a row at a time
fmt = [repmat('%6.2f', 1, 31), '\n'];
fid = fopen( 'D:/DAILYT/MEANT15APR1980.TXT', 'wt');
fprintf(fid, '('' Daily Tempereture for 15 APR 1980 '')\n');
fprintf(fid, fmt, permute(T(106,:,:), [3 2 1]));
fclose(fid);
  2 Kommentare
Walter Roberson
Walter Roberson am 3 Jul. 2020
It appears that those particular files have no gaps between records, so remove the fseek() calls.
The values of Tr must be less than 20
The -999 "no data" entries are coming through too clearly for me to believe that there is a byte order problem or anything like that.
If you examine the data for days 129 and (especially) 130, you will see entries as high as 133.7053 . It looks like there was probably a bad storm those days, which would correspond to May 9 to May 10 of whatever year you are examining.
Kalore Shubham Arun
Kalore Shubham Arun am 4 Jul. 2020
Thanks! it worked.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Fortran with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by