Convert Fortran code to Matlab
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kalore Shubham Arun
le 3 Juil 2020
Commenté : Kalore Shubham Arun
le 4 Juil 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 commentaires
Walter Roberson
le 3 Juil 2020
I suggest starting with https://www.mathworks.com/matlabcentral/fileexchange/5260-benbarrowes-f2matlab
Réponse acceptée
Walter Roberson
le 3 Juil 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 commentaires
Walter Roberson
le 3 Juil 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Fortran with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!