Convert Fortran code to Matlab
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Kalore Shubham Arun
il 3 Lug 2020
Commentato: Kalore Shubham Arun
il 4 Lug 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 Commenti
Walter Roberson
il 3 Lug 2020
I suggest starting with https://www.mathworks.com/matlabcentral/fileexchange/5260-benbarrowes-f2matlab
Risposta accettata
Walter Roberson
il 3 Lug 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 Commenti
Walter Roberson
il 3 Lug 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Fortran with MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!