Skip to content

Commit b174ab3

Browse files
Merge pull request #23 from jacobwilliams/develop
Develop
2 parents 02d02b1 + b3f04e8 commit b174ab3

File tree

5 files changed

+111
-20
lines changed

5 files changed

+111
-20
lines changed

.github/workflows/CI.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: CI
2-
on: [push]
2+
on: [push, pull_request]
33
jobs:
44

55
Build:

README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,9 @@ cd fortran-csv-module
2020
```
2121
#### Dependencies
2222
1. Git
23-
2. [FoBis](https://github.com/szaghi/FoBiS), [fpm](https://github.com/fortran-lang/fpm), or [CMake](https://cmake.org)
23+
2. [fpm](https://github.com/fortran-lang/fpm), or [CMake](https://cmake.org)
2424
3. [FORD](https://github.com/Fortran-FOSS-Programmers/ford) (optional)
2525

26-
#### Build with FoBis
27-
You can build using provided `build.sh`:
28-
```bash
29-
./build.sh
30-
```
31-
3226
#### Build with [fortran-lang/fpm](https://github.com/fortran-lang/fpm)
3327
Fortran Package Manager (fpm) is a great package manager and build system for Fortran.
3428
You can build using provided `fpm.toml`:

src/csv_kinds.f90

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
module csv_kinds
66

7-
use iso_fortran_env, only: real64,int32
7+
use iso_fortran_env, only: real64,real32,int32
88

99
private
1010

1111
integer,parameter,public :: wp = real64 !! default real kind
12+
integer,parameter,public :: sp = real32 !! additional real kind, single precision
1213
integer,parameter,public :: ip = int32 !! default integer kind
1314

1415
end module csv_kinds

src/csv_module.F90

+74-9
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ module csv_module
8989
! after the file has been read.
9090
generic,public :: get => get_csv_data_as_str,&
9191
csv_get_value,&
92-
get_real_column,&
92+
get_real_sp_column,&
93+
get_real_wp_column,&
9394
get_integer_column,&
9495
get_logical_column,&
9596
get_character_column,&
9697
get_csv_string_column
9798
procedure :: get_csv_data_as_str
9899
procedure :: csv_get_value
99-
procedure :: get_real_column
100+
procedure :: get_real_sp_column
101+
procedure :: get_real_wp_column
100102
procedure :: get_integer_column
101103
procedure :: get_logical_column
102104
procedure :: get_character_column
@@ -428,6 +430,14 @@ subroutine add_cell(me,val,int_fmt,real_fmt,trim_str)
428430
end if
429431
write(int_val,fmt=ifmt,iostat=istat) val
430432
write(me%iunit,fmt='(A)',advance='NO',iostat=istat) trim(adjustl(int_val))
433+
type is (real(sp))
434+
if (present(real_fmt)) then
435+
rfmt = trim(adjustl(real_fmt))
436+
else
437+
rfmt = default_real_fmt
438+
end if
439+
write(real_val,fmt=rfmt,iostat=istat) val
440+
write(me%iunit,fmt='(A)',advance='NO',iostat=istat) trim(adjustl(real_val))
431441
type is (real(wp))
432442
if (present(real_fmt)) then
433443
rfmt = trim(adjustl(real_fmt))
@@ -689,11 +699,36 @@ subroutine get_csv_data_as_str(me,csv_data,status_ok)
689699
end subroutine get_csv_data_as_str
690700
!*****************************************************************************************
691701

702+
!*****************************************************************************************
703+
!>
704+
! Convert a string to a `real(sp)`
705+
706+
pure elemental subroutine to_real_sp(str,val,status_ok)
707+
708+
implicit none
709+
710+
character(len=*),intent(in) :: str
711+
real(sp),intent(out) :: val
712+
logical,intent(out) :: status_ok
713+
714+
integer :: istat !! read `iostat` error code
715+
716+
read(str,fmt=*,iostat=istat) val
717+
if (istat==0) then
718+
status_ok = .true.
719+
else
720+
status_ok = .false.
721+
val = zero
722+
end if
723+
724+
end subroutine to_real_sp
725+
!*****************************************************************************************
726+
692727
!*****************************************************************************************
693728
!>
694729
! Convert a string to a `real(wp)`
695730

696-
pure elemental subroutine to_real(str,val,status_ok)
731+
pure elemental subroutine to_real_wp(str,val,status_ok)
697732

698733
implicit none
699734

@@ -711,7 +746,7 @@ pure elemental subroutine to_real(str,val,status_ok)
711746
val = zero
712747
end if
713748

714-
end subroutine to_real
749+
end subroutine to_real_wp
715750
!*****************************************************************************************
716751

717752
!*****************************************************************************************
@@ -840,7 +875,7 @@ subroutine infer_variable_type(str,itype)
840875
return
841876
end if
842877

843-
call to_real(str,rval,status_ok)
878+
call to_real_wp(str,rval,status_ok)
844879
if (status_ok) then
845880
itype = csv_type_double
846881
return
@@ -878,8 +913,10 @@ subroutine csv_get_value(me,row,col,val,status_ok)
878913
select type (val)
879914
type is (integer(ip))
880915
call to_integer(me%csv_data(row,col)%str,val,status_ok)
916+
type is (real(sp))
917+
call to_real_sp(me%csv_data(row,col)%str,val,status_ok)
881918
type is (real(wp))
882-
call to_real(me%csv_data(row,col)%str,val,status_ok)
919+
call to_real_wp(me%csv_data(row,col)%str,val,status_ok)
883920
type is (logical)
884921
call to_logical(me%csv_data(row,col)%str,val,status_ok)
885922
type is (character(len=*))
@@ -951,9 +988,13 @@ subroutine get_column(me,icol,r,status_ok)
951988
if (me%verbose) write(error_unit,'(A)') &
952989
'Error converting string to integer: '//trim(me%csv_data(i,icol)%str)
953990
r(i) = 0
991+
type is (real(sp))
992+
if (me%verbose) write(error_unit,'(A)') &
993+
'Error converting string to real(real32): '//trim(me%csv_data(i,icol)%str)
994+
r(i) = zero
954995
type is (real(wp))
955996
if (me%verbose) write(error_unit,'(A)') &
956-
'Error converting string to real: '//trim(me%csv_data(i,icol)%str)
997+
'Error converting string to real(real64): '//trim(me%csv_data(i,icol)%str)
957998
r(i) = zero
958999
type is (logical)
9591000
if (me%verbose) write(error_unit,'(A)') &
@@ -972,11 +1013,35 @@ subroutine get_column(me,icol,r,status_ok)
9721013
end subroutine get_column
9731014
!*****************************************************************************************
9741015

1016+
!*****************************************************************************************
1017+
!>
1018+
! Return a column from a CSV file as a `real(sp)` vector.
1019+
1020+
subroutine get_real_sp_column(me,icol,r,status_ok)
1021+
1022+
implicit none
1023+
1024+
class(csv_file),intent(inout) :: me
1025+
integer,intent(in) :: icol !! column number
1026+
real(sp),dimension(:),allocatable,intent(out) :: r
1027+
logical,intent(out) :: status_ok
1028+
1029+
if (allocated(me%csv_data)) then
1030+
allocate(r(me%n_rows)) ! size the output vector
1031+
call me%get_column(icol,r,status_ok)
1032+
else
1033+
if (me%verbose) write(error_unit,'(A,1X,I5)') 'Error: class has not been initialized'
1034+
status_ok = .false.
1035+
end if
1036+
1037+
end subroutine get_real_sp_column
1038+
!*****************************************************************************************
1039+
9751040
!*****************************************************************************************
9761041
!>
9771042
! Return a column from a CSV file as a `real(wp)` vector.
9781043

979-
subroutine get_real_column(me,icol,r,status_ok)
1044+
subroutine get_real_wp_column(me,icol,r,status_ok)
9801045

9811046
implicit none
9821047

@@ -993,7 +1058,7 @@ subroutine get_real_column(me,icol,r,status_ok)
9931058
status_ok = .false.
9941059
end if
9951060

996-
end subroutine get_real_column
1061+
end subroutine get_real_wp_column
9971062
!*****************************************************************************************
9981063

9991064
!*****************************************************************************************

src/tests/csv_test.f90

+33-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
program csv_test
88

99
use csv_module
10-
use iso_fortran_env, only: wp => real64
10+
use iso_fortran_env, only: wp => real64, sp => real32
1111

1212
implicit none
1313

@@ -27,7 +27,8 @@ subroutine csv_test_1()
2727
integer :: k !! counter
2828
character(len=30),dimension(:),allocatable :: header !! the header
2929
character(len=30),dimension(:,:),allocatable :: csv_data !! the data from the file as strings
30-
real(wp),dimension(:),allocatable :: x !! for getting a real vector from a csv file
30+
real(wp),dimension(:),allocatable :: x !! for getting a real(wp) vector from a csv file
31+
real(sp),dimension(:),allocatable :: y !! for getting a real(sp) vector from a csv file
3132
logical :: status_ok !! error flag
3233
integer,dimension(:),allocatable :: itypes !! array of variable types in the file
3334
integer :: ifile !! file counter
@@ -92,10 +93,16 @@ subroutine csv_test_1()
9293
write(*,*) 'get some vectors:'
9394
if (ifile==1) then
9495
write(*,*) ''
96+
write(*,*) 'get real(wp) vector:'
9597
write(*,*) 'age:'
9698
call f%get(3,x,status_ok)
9799
write(*,'(F6.3,1x)',advance='NO') x
98100
write(*,*) ''
101+
write(*,*) 'get real(sp) vector:'
102+
write(*,*) 'age:'
103+
call f%get(3,y,status_ok)
104+
write(*,'(F6.3,1x)',advance='NO') y
105+
write(*,*) ''
99106
else
100107
write(*,*) ''
101108
write(*,*) 'name:'
@@ -120,6 +127,13 @@ subroutine csv_test_1()
120127
call f2%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)') ! add as vectors
121128
call f2%add(.false.)
122129
call f2%next_row()
130+
call f2%add(1.5_sp) ! add as scalars
131+
call f2%add(2.5_sp)
132+
call f2%add(3.5_sp)
133+
call f2%add(.true.)
134+
call f2%next_row()
135+
call f2%add([4.5_sp,5.5_sp,6.5_sp],real_fmt='(F5.3)') ! add as vectors
136+
call f2%add(.false.)
123137
end if
124138
call f2%close(status_ok)
125139

@@ -153,6 +167,12 @@ subroutine csv_write_test()
153167
call f%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)')
154168
call f%add(.false.)
155169
call f%next_row()
170+
call f%add([1.5_sp,2.5_sp,3.5_sp],real_fmt='(F5.3)')
171+
call f%add(.true.)
172+
call f%next_row()
173+
call f%add([4.5_sp,5.5_sp,6.5_sp],real_fmt='(F5.3)')
174+
call f%add(.false.)
175+
call f%next_row()
156176

157177
! finished
158178
call f%close(status_ok)
@@ -170,6 +190,7 @@ subroutine csv_read_test()
170190
type(csv_file) :: f
171191
character(len=30),dimension(:),allocatable :: header
172192
real(wp),dimension(:),allocatable :: x,y,z
193+
real(sp),dimension(:),allocatable :: u,v,w
173194
logical,dimension(:),allocatable :: t
174195
logical :: status_ok
175196
integer,dimension(:),allocatable :: itypes
@@ -199,6 +220,16 @@ subroutine csv_read_test()
199220
write(*,*) 'y=',y
200221
write(*,*) 'z=',z
201222
write(*,*) 't=',t
223+
224+
call f%get(1,u,status_ok)
225+
call f%get(2,v,status_ok)
226+
call f%get(3,w,status_ok)
227+
call f%get(4,t,status_ok)
228+
229+
write(*,*) 'x=',u
230+
write(*,*) 'y=',v
231+
write(*,*) 'z=',w
232+
write(*,*) 't=',t
202233

203234
! destroy the file
204235
call f%destroy()

0 commit comments

Comments
 (0)