allocate_and_assignRvec Subroutine

private subroutine allocate_and_assignRvec(buffer, variable, keyword, fs)

Allocate and assign an arbitrary length vector of reals to variable

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(inout) :: buffer

Input buffer

real(kind=real32), intent(out), dimension(:), allocatable :: variable

Variable to assign data to

character(len=*), intent(in), optional :: keyword

Keyword to start from

character(len=1), intent(in), optional :: fs

Field separator


Source Code

  subroutine allocate_and_assignRvec(buffer, variable, keyword, fs)
    !! Allocate and assign an arbitrary length vector of reals to variable
    implicit none

    ! Arguments
    character(*), intent(inout) :: buffer
    !! Input buffer
    real(real32), dimension(:), allocatable, intent(out) :: variable
    !! Variable to assign data to
    character(*), optional, intent(in) :: keyword
    !! Keyword to start from
    character(1), optional, intent(in) :: fs
    !! Field separator

    ! Local variables
    integer :: i
    !! Number of values and loop index
    character(1024) :: buffer2
    !! Temporary buffer
    character(1) :: fs_
    !! Field separator
    character(1), parameter :: open_brackets(3) = ['[', '(', '{']
    character(1), parameter :: close_brackets(3) = [']', ')', '}']

    fs_ = '='
    if(present(fs)) fs_ = fs

    if(present(keyword)) buffer = buffer(index(buffer, keyword):)
    if(scan(buffer, fs_) .ne. 0)then
       buffer2 = get_val(buffer, fs_)
    else
       buffer2 = buffer
    end if
    buffer2 = adjustl(buffer2)
    if(any(index(buffer2,open_brackets).eq.1))then
       do i = 1, size(open_brackets)
          if(index(buffer2, open_brackets(i)) .eq. 1)then
             buffer2 = buffer2(2:)
          end if
       end do
    end if
    if(any(index(trim(buffer2),close_brackets).eq.len(trim(buffer2))))then
       do i = 1, size(close_brackets)
          if(index(trim(buffer2), close_brackets(i)) .eq. len(trim(buffer2)))then
             buffer2 = buffer2(:len(trim(buffer2))-1)
          end if
       end do
    end if
    ! count number of values
    i = icount(buffer2)
    allocate(variable(i))
    read(buffer2, *) (variable(i), i = 1, size(variable))
  end subroutine allocate_and_assignRvec