@@ -89,14 +89,16 @@ module csv_module
89
89
! after the file has been read.
90
90
generic,public :: get = > get_csv_data_as_str,&
91
91
csv_get_value,&
92
- get_real_column,&
92
+ get_real_sp_column,&
93
+ get_real_wp_column,&
93
94
get_integer_column,&
94
95
get_logical_column,&
95
96
get_character_column,&
96
97
get_csv_string_column
97
98
procedure :: get_csv_data_as_str
98
99
procedure :: csv_get_value
99
- procedure :: get_real_column
100
+ procedure :: get_real_sp_column
101
+ procedure :: get_real_wp_column
100
102
procedure :: get_integer_column
101
103
procedure :: get_logical_column
102
104
procedure :: get_character_column
@@ -428,6 +430,14 @@ subroutine add_cell(me,val,int_fmt,real_fmt,trim_str)
428
430
end if
429
431
write (int_val,fmt= ifmt,iostat= istat) val
430
432
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))
431
441
type is (real (wp))
432
442
if (present (real_fmt)) then
433
443
rfmt = trim (adjustl (real_fmt))
@@ -689,11 +699,36 @@ subroutine get_csv_data_as_str(me,csv_data,status_ok)
689
699
end subroutine get_csv_data_as_str
690
700
! *****************************************************************************************
691
701
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
+
692
727
! *****************************************************************************************
693
728
! >
694
729
! Convert a string to a `real(wp)`
695
730
696
- pure elemental subroutine to_real (str,val,status_ok)
731
+ pure elemental subroutine to_real_wp (str,val,status_ok)
697
732
698
733
implicit none
699
734
@@ -711,7 +746,7 @@ pure elemental subroutine to_real(str,val,status_ok)
711
746
val = zero
712
747
end if
713
748
714
- end subroutine to_real
749
+ end subroutine to_real_wp
715
750
! *****************************************************************************************
716
751
717
752
! *****************************************************************************************
@@ -840,7 +875,7 @@ subroutine infer_variable_type(str,itype)
840
875
return
841
876
end if
842
877
843
- call to_real (str,rval,status_ok)
878
+ call to_real_wp (str,rval,status_ok)
844
879
if (status_ok) then
845
880
itype = csv_type_double
846
881
return
@@ -878,8 +913,10 @@ subroutine csv_get_value(me,row,col,val,status_ok)
878
913
select type (val)
879
914
type is (integer (ip))
880
915
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)
881
918
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)
883
920
type is (logical )
884
921
call to_logical(me% csv_data(row,col)% str,val,status_ok)
885
922
type is (character (len=* ))
@@ -951,9 +988,13 @@ subroutine get_column(me,icol,r,status_ok)
951
988
if (me% verbose) write (error_unit,' (A)' ) &
952
989
' Error converting string to integer: ' // trim (me% csv_data(i,icol)% str)
953
990
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
954
995
type is (real (wp))
955
996
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)
957
998
r(i) = zero
958
999
type is (logical )
959
1000
if (me% verbose) write (error_unit,' (A)' ) &
@@ -972,11 +1013,35 @@ subroutine get_column(me,icol,r,status_ok)
972
1013
end subroutine get_column
973
1014
! *****************************************************************************************
974
1015
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
+
975
1040
! *****************************************************************************************
976
1041
! >
977
1042
! Return a column from a CSV file as a `real(wp)` vector.
978
1043
979
- subroutine get_real_column (me ,icol ,r ,status_ok )
1044
+ subroutine get_real_wp_column (me ,icol ,r ,status_ok )
980
1045
981
1046
implicit none
982
1047
@@ -993,7 +1058,7 @@ subroutine get_real_column(me,icol,r,status_ok)
993
1058
status_ok = .false.
994
1059
end if
995
1060
996
- end subroutine get_real_column
1061
+ end subroutine get_real_wp_column
997
1062
! *****************************************************************************************
998
1063
999
1064
! *****************************************************************************************
0 commit comments