get_column Subroutine

private subroutine get_column(me, icol, r, status_ok)

Return a column from a CSV file vector.

Note

This routine requires that the r array already be allocated. This is because Fortran doesn't want to allow to you pass a non-polymorphic variable into a routine with a dummy variable with class(*),dimension(:),allocatable,intent(out) attributes.

Type Bound

csv_file

Arguments

Type IntentOptional Attributes Name
class(csv_file), intent(inout) :: me
integer, intent(in) :: icol

column number

class(*), intent(out), dimension(:) :: r

assumed to have been allocated to the correct size by the caller. (n_rows)

logical, intent(out) :: status_ok

status flag


Calls

proc~~get_column~~CallsGraph proc~get_column csv_file%get_column proc~csv_get_value csv_file%csv_get_value proc~get_column->proc~csv_get_value proc~to_integer to_integer proc~csv_get_value->proc~to_integer proc~to_logical to_logical proc~csv_get_value->proc~to_logical proc~to_real_sp to_real_sp proc~csv_get_value->proc~to_real_sp proc~to_real_wp to_real_wp proc~csv_get_value->proc~to_real_wp proc~lowercase_string lowercase_string proc~to_logical->proc~lowercase_string

Called by

proc~~get_column~~CalledByGraph proc~get_column csv_file%get_column proc~get_character_column csv_file%get_character_column proc~get_character_column->proc~get_column proc~get_csv_string_column csv_file%get_csv_string_column proc~get_csv_string_column->proc~get_column proc~get_integer_column csv_file%get_integer_column proc~get_integer_column->proc~get_column proc~get_logical_column csv_file%get_logical_column proc~get_logical_column->proc~get_column proc~get_real_sp_column csv_file%get_real_sp_column proc~get_real_sp_column->proc~get_column proc~get_real_wp_column csv_file%get_real_wp_column proc~get_real_wp_column->proc~get_column

Source Code

    subroutine get_column(me,icol,r,status_ok)

    implicit none

    class(csv_file),intent(inout) :: me
    integer,intent(in) :: icol  !! column number
    class(*),dimension(:),intent(out) :: r  !! assumed to have been allocated to
                                            !! the correct size by the caller.
                                            !! (`n_rows`)
    logical,intent(out) :: status_ok  !! status flag

    integer :: i !! counter
    character(len=:),allocatable :: tmp !! for gfortran workaround

    ! we know the data is allocated, since that
    ! was checked by the calling routines.

    if (me%n_cols>=icol .and. icol>0) then

        do i=1,me%n_rows  ! row loop

#if ( defined __GFORTRAN__ ) && ( __GNUC__ <= 10 )
            ! the following is a workaround for gfortran bugs:
            select type (r)
            type is (character(len=*))
                tmp = repeat(' ',len(r)) ! size the string
                call me%csv_get_value(i,icol,tmp,status_ok)
                r(i) = tmp
            class default
                call me%csv_get_value(i,icol,r(i),status_ok)
            end select
#else
            call me%csv_get_value(i,icol,r(i),status_ok)
#endif
            if (.not. status_ok) then
                select type (r)
                ! note: character conversion can never fail, so not
                ! checking for that here. also we know it is real,
                ! integer, or logical at this point.
                type is (integer(ip))
                    if (me%verbose) write(error_unit,'(A)') &
                        'Error converting string to integer: '//trim(me%csv_data(i,icol)%str)
                    r(i) = 0
                type is (real(sp))
                    if (me%verbose) write(error_unit,'(A)') &
                        'Error converting string to real(real32): '//trim(me%csv_data(i,icol)%str)
                    r(i) = zero
                type is (real(wp))
                    if (me%verbose) write(error_unit,'(A)') &
                        'Error converting string to real(real64): '//trim(me%csv_data(i,icol)%str)
                    r(i) = zero
                type is (logical)
                    if (me%verbose) write(error_unit,'(A)') &
                        'Error converting string to logical: '//trim(me%csv_data(i,icol)%str)
                    r(i) = .false.
                end select
            end if

        end do

    else
        if (me%verbose) write(error_unit,'(A,1X,I5)') 'Error: invalid column number: ',icol
        status_ok = .false.
    end if

    end subroutine get_column