Convert Fortran code to Matlab

조회 수: 5 (최근 30일)
Kalore Shubham Arun
Kalore Shubham Arun 2020년 7월 3일
댓글: Kalore Shubham Arun 2020년 7월 4일
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
Walter Roberson
Walter Roberson 2020년 7월 3일
I will need the sample input file to test with.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 3일
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
Walter Roberson
Walter Roberson 2020년 7월 3일
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 2020년 7월 4일
Thanks! it worked.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by