Silverfrost Forums

Welcome to our forums

Reading from large file,saving to array

7 May 2006 12:30 #653

Hi I am a recent Fortran user who has still a lot to learn so i decided to ask for your help as experts for a problem that i face. I need to read from a file.txt some values and store them to an allocatable array.Arrays dimensions are given at the first line af the file. For example a small part of the file is like that:

|----12911=Number of rows ←---3 No of columns 12911 3 _ _ ______________|the values that need to NO2 SO2 O3 / / / be stored 06-12-2000 02:00 17 42 60 06-12-2000 03:00 12 20 65 06-12-2000 04:00 7 10 1.2 06-12-2000 05:00 6 7 74 06-12-2000 06:00 5 5 74 06-12-2000 07:00 13 7 63 06-12-2000 08:00 28 -999 48 etc etc etc Ofcourse 12911 and 3 may change from file to file I would be grateful if anyone could tell me the do loop i have to use in order to store the values as well as the format because the values differ in type(e.g.-999,1.2 ,70 etc) thank for your time

Dimitris

7 May 2006 11:37 #655

Dimitris

You have picked a difficult exercise for a Fortran beginner. The following may help you to get started...

INTEGER rows,columns,i CHARACTER date11,time6,values16 REAL,ALLOCATABLE::c1(:),c2(:),c3(:) OPEN(UNIT=7,FILE='test.dat') READ(7,) rows,columns ALLOCATE(c1(rows),c2(rows),c3(rows)) DO i=1,rows READ(7,'(10a,5a,16a)') date,time,values READ(values,*) c1(i),c2(i),c3(i) ENDDO CLOSE(7) END

8 May 2006 1:19 #656

Perhaps I should have included the next stage of development....

INTEGER rows,columns,i,j CHARACTER date11,time6,values16 REAL,ALLOCATABLE::d(:,:) OPEN(UNIT=7,FILE='test.dat') READ(7,) rows,columns ALLOCATE(d(rows,columns)) DO i=1,rows READ(7,'(10a,5a,16a)') date,time,values READ(values,*) (d(i,j),j=1,columns) ENDDO CLOSE(7) END

Please login to reply.