read Module Subroutine

module subroutine read(this, file)

Uses

    • coreutils

Read the network from a file check if a tag line check for card

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

character(len=*), intent(in) :: file

File to read the network from


Source Code

  module subroutine read(this, file)
    !! Read the network from a file
    use coreutils, only: icount
    implicit none

    ! Arguments
    class(network_type), intent(inout) :: this
    !! Instance of network
    character(*), intent(in) :: file
    !! File to read the network from

    ! Local variables
    integer :: i, unit, stat, itmp1
    !! Loop index
    integer, dimension(:), allocatable :: input_list, output_list
    !!! List of input and output layers
    character(256) :: buffer, err_msg, input_str, output_str
    !! Buffer for reading lines from file
    character(20) :: name
    !! Name of the layer
    character(2) :: operator_in, operator_out
    !! Operator for the layer
    integer :: layer_index
    !! Index of the layer in the list of layer types


    if(.not.allocated(list_of_layer_types))then
       call allocate_list_of_layer_types()
    end if

    open(newunit=unit,file=file,action='read')
    i = 0
    card_loop: do
       i = i + 1
       read(unit,'(A)',iostat=stat) buffer
       if(stat.lt.0)then
          exit card_loop
       elseif(stat.gt.0)then
          call stop_program("error encountered in network read")
          return
       end if
       if(trim(adjustl(buffer)).eq."") cycle card_loop

       !! check if a tag line
       if(scan(buffer,'=').ne.0)then
          write(0,*) "WARNING: unexpected line in read file"
          write(0,*) trim(buffer)
          write(0,*) " skipping..."
          cycle card_loop
       end if

       !! check for card
       name = trim(adjustl(buffer(1:scan(buffer,' ')-1)))
       if(name.eq."NETWORK_SETTINGS")then
          call this%read_network_settings(unit)
          cycle card_loop
       end if
       buffer = trim(adjustl(buffer(scan(buffer,' ')+1:)))
       operator_in = trim(adjustl(buffer(1:scan(buffer,' ')-1)))
       buffer = trim(adjustl(buffer(scan(buffer,' ')+1:)))
       input_str = trim(adjustl(buffer(1:scan(buffer,']'))))
       if(scan(input_str,'[').ne.0)then
          input_str = &
               trim(adjustl(input_str(scan(input_str,'[')+1:scan(input_str,']')-1)))
          itmp1 = icount(input_str)
          allocate(input_list(itmp1))
          read(input_str,*) input_list
       else
          allocate(input_list, source = [-1])
       end if
       buffer = trim(adjustl(buffer(scan(buffer,']')+1:)))
       operator_out = trim(adjustl(buffer(1:scan(buffer,' ')-1)))
       buffer = trim(adjustl(buffer(scan(buffer,' ')+1:)))
       output_str = trim(adjustl(buffer(1:scan(buffer,']'))))
       if(scan(output_str,'[').ne.0)then
          output_str = &
               trim(adjustl(output_str(scan(output_str,'[')+1:scan(output_str,']')-1)))
          itmp1 = icount(output_str)
          allocate(output_list(itmp1))
          read(output_str,*) output_list
       else
          allocate(output_list(0))
       end if
       name = trim(adjustl(to_lower(name)))
       layer_index = &
            findloc( &
                 [ list_of_layer_types(:)%name ], &
                 name, &
                 dim = 1 &
            )
       if(layer_index.eq.0)then
          write(err_msg,'("unrecognised card ''",A)') trim(adjustl(buffer))
          call stop_program(err_msg)
          return
       end if
       call this%add( &
            list_of_layer_types(layer_index)%read_ptr(unit), &
            input_list = input_list, &
            operator = operator_in &
       )
       if(allocated(input_list)) deallocate(input_list)
       if(allocated(output_list)) deallocate(output_list)
    end do card_loop
    close(unit)

  end subroutine read