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 | Intent | Optional | 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.
( |
|
logical, | intent(out) | :: | status_ok |
status flag |
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