I want to get data from a file, which can have a variable size in its data content. However, the structure is quite simple. 3 columns and undefined number of rows. I figured that using a distributed multidimensional array and an explicit DO loop make me solve my problem. Here is my code so far
program arraycall
implicit none
integer, dimension(:,:), allocatable :: array
integer :: max_rows, max_cols, row, col
allocate(array(row,col))
open(10, file='boundary.txt', access='sequential', status='old', FORM='FORMATTED')
DO row = 1, max_rows
DO col = 1, max_cols
READ (10,*) array (row, col)
END DO
END DO
print *, array (row,col)
deallocate(array)
end program arraycall
Now the problem that I am facing is that I do not know how to determine these max_rows and max_cols, which resonate with the fact that it is of unknown size.
An example file will look like
11 12 13
21 22 23
31 32 33
41 42 43
So, I figured out a way to estimate the length of a file record on the fly (dynamically). Update for future reference to others.
!
! Estimate the number of records in the inputfile
!
open(lin,file=inputfile,status='old',action='read',position='rewind')
loop1: do
read(lin,*,iostat=eastat) inputline
if (eastat < 0) then
write(*,*) trim(inputfile),": number of records = ", numvalues
exit loop1
else if (eastat > 0 ) then
stop "IO-Error!"
end if
numvalues=numvalues+1
end do loop1
!
! Read the records from the inputfile
!
rewind(lin)
allocate (lon(numvalues),lat(numvalues),value(numvalues))
do i=1,numvalues
read(lin,*) lon(i),lat(i),value(i)
end do
close(lin)