Reading a multidimensional array of unknown shape

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)
+4
2

, 3 , :

  • . , . , , - .

  • @AlexanderVogt, . . - ( @AlexanderVogt):

    allocate(array2(3,tot_rows))
    array2 = array(:, :tot_rows)
    deallocate(array)
    

    , , 2 , . , , array arrray2 .

  • . . . , . :

    integer, dimension(3) :: data
    

    , 3 .

:

program LinkedList
implicit none
  integer :: i, eastat, value, numvalues
  type node
      integer :: data
      type( node ), pointer :: next
  end type node
  integer, dimension(:), allocatable :: lon
  type( node ), pointer :: head, current, previous


  nullify( head )   ! Initialize list to point to no target.

  open(10,file='data.dat',status='old',action='read', position='rewind')
  numvalues = 0 
  do 
      read(10,*,iostat=eastat) value
      if (eastat < 0) then
          write(*,*) "number of records = ", numvalues
          exit
      else if (eastat > 0 ) then
          stop "IO-Error!"
      end if
      allocate( current )
      current%data = value
      current%next => head
      head => current
      numvalues=numvalues+1
  end do 
  close(10)
! The list is read. You can now convert it into an array, if needed for
! numerical efficiency

  allocate(lon(numvalues))  
  current => head
  ! You could transverse the list this way if you hadn't kept numvalues
  !do  while ( associated( current ) )
  do i= numvalues, 1, -1
      lon(i) = current%data
      previous => current
      current => current%next
!       head => current
      deallocate(previous)
   end do


! Output the list, deallocating them after use.
print *,"lon = ", lon

end program LinkedList
+2

iostat ( ):

program arraycall 

  implicit none
  integer, dimension(:,:), allocatable :: array
  integer           :: row
  integer           :: stat ! Check return values
  ! Define max. values
  integer,parameter :: max_rows=1000
  integer,parameter :: max_cols=3    ! As stated in the question
  ! Total number of rows in the file
  integer           :: tot_rows

  allocate( array(max_cols,max_rows), stat=stat)
  ! Always a good idea to check the return value
  if ( stat /= 0 ) stop 'Cannot allocate memory!'

  open(10, file='boundary.txt', access='sequential', &
       status='old', FORM='FORMATTED')

  DO row = 1, max_rows
    ! You can directly read in arrays! 
    READ (10,*,iostat=stat) array(:,row)
    if ( stat > 0 ) then
      stop 'An error occured while reading the file'
    elseif ( stat < 0 ) then
      tot_rows = row-1
      print *, 'EOF reached. Found a total of ', tot_rows, 'rows.'
      exit
    endif
  END DO

  close(10)

  ! Do stuff, e.g. re-allocate the array
  print *,array(:,:tot_rows)

  deallocate(array)
end program arraycall 

iostat > 0 - , iostat < 0 - ( ).

+1

Source: https://habr.com/ru/post/1544291/


All Articles