@@ -11,85 +11,212 @@ program csv_test
1111
1212 implicit none
1313
14- type (csv_file) :: f
15- type (csv_file) :: f2
16- integer :: i ! ! counter
17- character (len= 30 ),dimension (:),allocatable :: header ! ! the header
18- character (len= 30 ),dimension (:,:),allocatable :: csv_data ! ! the data from the file as strings
19- real (wp),dimension (:),allocatable :: x ! ! for getting a real vector from a csv file
20- logical :: status_ok ! ! error flag
21- integer ,dimension (:),allocatable :: itypes ! ! array of variable types in the file
22- integer :: ifile ! ! file counter
23- character (len= 30 ),dimension (:),allocatable :: names
24-
25- character (len=* ),dimension (2 ),parameter :: files_to_test = [' ../files/test.csv ' ,&
26- ' ../files/test_2_columns.csv' ]
27-
28- do ifile = 1 , size (files_to_test)
29-
30- ! read the file:
31- if (ifile== 1 ) then
32- call f% read (trim (files_to_test(ifile)),&
33- header_row= 1 ,status_ok= status_ok)
34- else
35- ! also skip a row
36- call f% read (trim (files_to_test(ifile)),&
37- header_row= 1 ,skip_rows= [2 ],status_ok= status_ok)
38- end if
14+ call csv_test_1()
15+ call csv_write_test()
16+ call csv_read_test()
17+
18+ contains
19+
20+ subroutine csv_test_1 ()
21+
22+ implicit none
23+
24+ type (csv_file) :: f
25+ type (csv_file) :: f2
26+ integer :: i ! ! counter
27+ character (len= 30 ),dimension (:),allocatable :: header ! ! the header
28+ character (len= 30 ),dimension (:,:),allocatable :: csv_data ! ! the data from the file as strings
29+ real (wp),dimension (:),allocatable :: x ! ! for getting a real vector from a csv file
30+ logical :: status_ok ! ! error flag
31+ integer ,dimension (:),allocatable :: itypes ! ! array of variable types in the file
32+ integer :: ifile ! ! file counter
33+ character (len= 30 ),dimension (:),allocatable :: names
34+ character (len= :),allocatable :: file
35+
36+ character (len=* ),dimension (2 ),parameter :: files_to_test = [' ../files/test.csv ' ,&
37+ ' ../files/test_2_columns.csv' ]
3938
4039 write (* ,* ) ' '
41- write (* ,* ) ' File: ' // trim (files_to_test(ifile))
42- ! print the header and type info:
43- call f% get_header(header,status_ok)
44- call f% variable_types(itypes,status_ok)
40+ write (* ,* ) ' ============================'
41+ write (* ,* ) ' csv_test_1 '
42+ write (* ,* ) ' ============================'
4543 write (* ,* ) ' '
46- write (* ,' (*(A30,1X,A4))' ) ' Header' , ' Type'
47- do i= 1 ,size (header)
48- write (* ,' (*(A30,1X,I4))' ) header(i), itypes(i)
49- end do
5044
51- write (* ,* ) ' '
52- write (* ,* ) ' print all the rows:'
45+ do ifile = 1 , size (files_to_test)
5346
54- call f% get(csv_data,status_ok)
55- do i= 1 ,size (csv_data,1 )
56- write (* ,' (*(A30,1X))' ) csv_data(i,:)
57- end do
47+ file = trim (files_to_test(ifile))
48+ if (.not. file_exists(file)) then
49+ file (1 :1 ) = ' ' ! try from current working directory
50+ file = trim (adjustl (file))
51+ end if
52+
53+ ! read the file:
54+ if (ifile== 1 ) then
55+ call f% read (file,header_row= 1 ,status_ok= status_ok)
56+ else
57+ ! also skip a row
58+ call f% read (file,header_row= 1 ,skip_rows= [2 ],status_ok= status_ok)
59+ end if
60+
61+ if (.not. status_ok) then
62+ error stop ' could not open file'
63+ end if
5864
59- write (* ,* ) ' '
60- write (* ,* ) ' get some vectors:'
61- if (ifile== 1 ) then
6265 write (* ,* ) ' '
63- write (* ,* ) ' age:'
64- call f% get(3 ,x,status_ok)
65- write (* ,' (F6.3,1x)' ,advance= ' NO' ) x
66+ write (* ,* ) ' File: ' // trim (files_to_test(ifile))
67+ ! print the header and type info:
68+ call f% get_header(header,status_ok)
69+ call f% variable_types(itypes,status_ok)
6670 write (* ,* ) ' '
67- else
71+ write (* ,' (*(A30,1X,A4))' ) ' Header' , ' Type'
72+ do i= 1 ,size (header)
73+ write (* ,' (*(A30,1X,I4))' ) header(i), itypes(i)
74+ end do
75+
6876 write (* ,* ) ' '
69- write (* ,* ) ' name:'
70- call f% get(2 ,names,status_ok)
71- write (* ,' (A10,1x)' ,advance= ' NO' ) names
77+ write (* ,* ) ' print all the rows:'
78+
79+ call f% get(csv_data,status_ok)
80+ do i= 1 ,size (csv_data,1 )
81+ write (* ,' (*(A30,1X))' ) csv_data(i,:)
82+ end do
83+
7284 write (* ,* ) ' '
85+ write (* ,* ) ' get some vectors:'
86+ if (ifile== 1 ) then
87+ write (* ,* ) ' '
88+ write (* ,* ) ' age:'
89+ call f% get(3 ,x,status_ok)
90+ write (* ,' (F6.3,1x)' ,advance= ' NO' ) x
91+ write (* ,* ) ' '
92+ else
93+ write (* ,* ) ' '
94+ write (* ,* ) ' name:'
95+ call f% get(2 ,names,status_ok)
96+ write (* ,' (A10,1x)' ,advance= ' NO' ) names
97+ write (* ,* ) ' '
98+ end if
99+
100+ end do
101+
102+ ! now test creating a CSV:
103+ call f2% initialize(enclose_strings_in_quotes= .false. ,verbose= .true. )
104+ call f2% open (' test2.csv' ,n_cols= 4 ,status_ok= status_ok)
105+ if (status_ok) then
106+ call f2% add([' x' ,' y' ,' z' ,' t' ]) ! add header as vector
107+ call f2% next_row()
108+ call f2% add(1.0_wp ) ! add as scalars
109+ call f2% add(2.0_wp )
110+ call f2% add(3.0_wp )
111+ call f2% add(.true. )
112+ call f2% next_row()
113+ call f2% add([4.0_wp ,5.0_wp ,6.0_wp ],real_fmt= ' (F5.3)' ) ! add as vectors
114+ call f2% add(.false. )
115+ call f2% next_row()
73116 end if
117+ call f2% close (status_ok)
118+
119+ end subroutine csv_test_1
120+
121+ subroutine csv_write_test ()
122+
123+ implicit none
124+
125+ type (csv_file) :: f
126+ logical :: status_ok
127+
128+ write (* ,* ) ' '
129+ write (* ,* ) ' ============================'
130+ write (* ,* ) ' csv_write_test '
131+ write (* ,* ) ' ============================'
132+ write (* ,* ) ' '
133+
134+ ! open the file
135+ call f% open (' test.csv' ,n_cols= 4 ,status_ok= status_ok)
136+ if (status_ok) then
137+
138+ ! add header
139+ call f% add([' x' ,' y' ,' z' ,' t' ])
140+ call f% next_row()
141+
142+ ! add some data:
143+ call f% add([1.0_wp ,2.0_wp ,3.0_wp ],real_fmt= ' (F5.3)' )
144+ call f% add(.true. )
145+ call f% next_row()
146+ call f% add([4.0_wp ,5.0_wp ,6.0_wp ],real_fmt= ' (F5.3)' )
147+ call f% add(.false. )
148+ call f% next_row()
149+
150+ ! finished
151+ call f% close (status_ok)
152+
153+ else
154+ error stop ' could not open file'
155+ end if
156+
157+ end subroutine csv_write_test
158+
159+ subroutine csv_read_test ()
160+
161+ implicit none
162+
163+ type (csv_file) :: f
164+ character (len= 30 ),dimension (:),allocatable :: header
165+ real (wp),dimension (:),allocatable :: x,y,z
166+ logical ,dimension (:),allocatable :: t
167+ logical :: status_ok
168+ integer ,dimension (:),allocatable :: itypes
169+
170+ write (* ,* ) ' '
171+ write (* ,* ) ' ============================'
172+ write (* ,* ) ' csv_read_test '
173+ write (* ,* ) ' ============================'
174+ write (* ,* ) ' '
175+
176+ ! read the file
177+ call f% read (' test.csv' ,header_row= 1 ,status_ok= status_ok)
178+
179+ if (status_ok) then
180+
181+ ! get the header and type info
182+ call f% get_header(header,status_ok)
183+ call f% variable_types(itypes,status_ok)
184+
185+ ! get some data
186+ call f% get(1 ,x,status_ok)
187+ call f% get(2 ,y,status_ok)
188+ call f% get(3 ,z,status_ok)
189+ call f% get(4 ,t,status_ok)
190+
191+ write (* ,* ) ' x=' ,x
192+ write (* ,* ) ' y=' ,y
193+ write (* ,* ) ' z=' ,z
194+ write (* ,* ) ' t=' ,t
195+
196+ ! destroy the file
197+ call f% destroy()
198+
199+ else
200+ error stop ' could not open file'
201+ end if
202+
203+ end subroutine csv_read_test
204+
205+ function file_exists (file ) result(exists)
206+
207+ ! ! returns True if the file exists
208+
209+ implicit none
210+ character (len=* ),intent (in ) :: file
211+ logical :: exists
212+
213+ integer :: istat
214+
215+ inquire (file= file,exist= exists,iostat= istat)
216+
217+ exists = exists .and. istat== 0 ! just in case?
74218
75- end do
76-
77- ! now test creating a CSV:
78- call f2% initialize(enclose_strings_in_quotes= .false. ,verbose= .true. )
79- call f2% open (' test2.csv' ,n_cols= 4 ,status_ok= status_ok)
80- if (status_ok) then
81- call f2% add([' x' ,' y' ,' z' ,' t' ]) ! add header as vector
82- call f2% next_row()
83- call f2% add(1.0_wp ) ! add as scalars
84- call f2% add(2.0_wp )
85- call f2% add(3.0_wp )
86- call f2% add(.true. )
87- call f2% next_row()
88- call f2% add([4.0_wp ,5.0_wp ,6.0_wp ],real_fmt= ' (F5.3)' ) ! add as vectors
89- call f2% add(.false. )
90- call f2% next_row()
91- end if
92- call f2% close (status_ok)
219+ end function file_exists
93220
94221 end program csv_test
95222! *****************************************************************************************
0 commit comments