66
77 module csv_module
88
9- use utilities_module
10- use kinds_module
11- use iso_fortran_env
9+ use csv_utilities
10+ use csv_kinds
11+ use csv_parameters
12+ use iso_fortran_env, only: error_unit
1213
1314 implicit none
1415
@@ -24,6 +25,9 @@ module csv_module
2425
2526 type,public :: csv_string
2627 ! ! a cell from a CSV file.
28+ ! !
29+ ! ! This use used to store the data internally
30+ ! ! in the [[csv_file]] class.
2731 private
2832 character (len= :),allocatable :: str
2933 end type csv_string
@@ -38,7 +42,7 @@ module csv_module
3842
3943 private
4044
41- logical :: verbose = .true . ! ! to print error messages
45+ logical :: verbose = .false . ! ! to print error messages
4246
4347 character (len= 1 ) :: quote = ' "' ! ! quotation character
4448 character (len= 1 ) :: delimiter = ' ,' ! ! delimiter character
@@ -128,17 +132,22 @@ subroutine initialize_csv_file(me,quote,delimiter,&
128132 enclose_all_in_quotes ,&
129133 logical_true_string ,&
130134 logical_false_string ,&
135+ chunk_size ,&
131136 verbose )
132137
133138 implicit none
134139
135140 class(csv_file),intent (out ) :: me
136141 character (len= 1 ),intent (in ),optional :: quote ! ! note: can only be one character
142+ ! ! (Default is `"`)
137143 character (len= 1 ),intent (in ),optional :: delimiter ! ! note: can only be one character
144+ ! ! (Default is `,`)
138145 logical ,intent (in ),optional :: enclose_strings_in_quotes ! ! if true, all string cells
139146 ! ! will be enclosed in quotes.
147+ ! ! (Default is True)
140148 logical ,intent (in ),optional :: enclose_all_in_quotes ! ! if true, *all* cells will
141149 ! ! be enclosed in quotes.
150+ ! ! (Default is False)
142151 character (len= 1 ),intent (in ),optional :: logical_true_string ! ! when writing a logical `true`
143152 ! ! value to a CSV file, this
144153 ! ! is the string to use
@@ -147,7 +156,10 @@ subroutine initialize_csv_file(me,quote,delimiter,&
147156 ! ! value to a CSV file, this
148157 ! ! is the string to use
149158 ! ! (default is `F`)
150- logical ,intent (in ),optional :: verbose
159+ integer ,intent (in ),optional :: chunk_size ! ! factor for expanding vectors
160+ ! ! (default is 100)
161+ logical ,intent (in ),optional :: verbose ! ! print error messages to the
162+ ! ! console (default is False)
151163
152164 if (present (quote)) me% quote = quote
153165 if (present (delimiter)) me% delimiter = delimiter
@@ -160,6 +172,10 @@ subroutine initialize_csv_file(me,quote,delimiter,&
160172 if (present (logical_false_string)) &
161173 me% logical_false_string = logical_false_string
162174 if (present (verbose)) me% verbose = verbose
175+ if (present (chunk_size)) me% chunk_size = chunk_size
176+
177+ ! override:
178+ if (me% enclose_all_in_quotes) me% enclose_strings_in_quotes = .true.
163179
164180 end subroutine initialize_csv_file
165181! *****************************************************************************************
@@ -299,21 +315,17 @@ end subroutine read_csv_file
299315
300316! *****************************************************************************************
301317! >
302- ! Open a CSV file for writing
318+ ! Open a CSV file for writing.
319+ !
320+ ! Use `initialize` to set options for the CSV file.
303321
304- subroutine open_csv_file (me ,filename ,&
305- n_cols ,&
306- enclose_strings_in_quotes ,&
307- enclose_all_in_quotes ,&
308- status_ok )
322+ subroutine open_csv_file (me ,filename ,n_cols ,status_ok )
309323
310324 implicit none
311325
312326 class(csv_file),intent (inout ) :: me
313327 character (len=* ),intent (in ) :: filename ! ! the CSV file to open
314328 integer ,intent (in ) :: n_cols ! ! number of columns in the file
315- logical ,intent (in ),optional :: enclose_strings_in_quotes ! ! default is true
316- logical ,intent (in ),optional :: enclose_all_in_quotes ! ! default is false
317329 logical ,intent (out ) :: status_ok ! ! status flag
318330
319331 integer :: istat ! ! open `iostat` flag
@@ -322,18 +334,6 @@ subroutine open_csv_file(me,filename,&
322334
323335 me% n_cols = n_cols
324336
325- if (present (enclose_strings_in_quotes)) then
326- me% enclose_strings_in_quotes = enclose_strings_in_quotes
327- else
328- me% enclose_strings_in_quotes = .true.
329- end if
330- if (present (enclose_all_in_quotes)) then
331- me% enclose_all_in_quotes = enclose_all_in_quotes
332- else
333- me% enclose_all_in_quotes = .false.
334- end if
335- if (me% enclose_all_in_quotes) me% enclose_strings_in_quotes = .true. ! override
336-
337337 open (newunit= me% iunit,file= filename,status= ' REPLACE' ,iostat= istat)
338338 if (istat== 0 ) then
339339 status_ok = .true.
@@ -367,25 +367,27 @@ end subroutine close_csv_file
367367! *****************************************************************************************
368368! >
369369! Add a cell to a CSV file.
370+ !
371+ ! @todo Need to check the `istat` values for errors.
370372
371373 subroutine add_cell (me ,val ,int_fmt ,real_fmt ,trim_str )
372374
373375 implicit none
374376
375377 class(csv_file),intent (inout ) :: me
376378 class(* ),intent (in ) :: val ! ! the value to add
377- character (len=* ),intent (in ),optional :: int_fmt ! ! format string for integers
378- character (len=* ),intent (in ),optional :: real_fmt ! ! format string for reals
379- logical ,intent (in ),optional :: trim_str ! ! to trim the string
379+ character (len=* ),intent (in ),optional :: int_fmt ! ! if `val` is an integer, use
380+ ! ! this format string.
381+ character (len=* ),intent (in ),optional :: real_fmt ! ! if `val` is a real, use
382+ ! ! this format string.
383+ logical ,intent (in ),optional :: trim_str ! ! if `val` is a string, then trim it.
380384
381385 integer :: istat ! ! write `iostat` flag
382386 character (len= :),allocatable :: ifmt ! ! actual format string to use for integers
383387 character (len= :),allocatable :: rfmt ! ! actual format string to use for reals
384388 logical :: trimstr ! ! if the strings are to be trimmed
385- character (len= max_real_str_len) :: real_val
386- character (len= max_integer_str_len) :: int_val
387-
388- ! TODO need to check the istat values for errors
389+ character (len= max_real_str_len) :: real_val ! ! for writing a real value
390+ character (len= max_integer_str_len) :: int_val ! ! for writing an integer value
389391
390392 ! make sure the row isn't already finished
391393 if (me% icol< me% n_cols) then
@@ -479,9 +481,11 @@ subroutine add_vector(me,val,int_fmt,real_fmt,trim_str)
479481
480482 class(csv_file),intent (inout ) :: me
481483 class(* ),dimension (:),intent (in ) :: val ! ! the values to add
482- character (len=* ),intent (in ),optional :: int_fmt ! ! format string for integers
483- character (len=* ),intent (in ),optional :: real_fmt ! ! format string for reals
484- logical ,intent (in ),optional :: trim_str ! ! to trim the string
484+ character (len=* ),intent (in ),optional :: int_fmt ! ! if `val` is an integer, use
485+ ! ! this format string.
486+ character (len=* ),intent (in ),optional :: real_fmt ! ! if `val` is a real, use
487+ ! ! this format string.
488+ logical ,intent (in ),optional :: trim_str ! ! if `val` is a string, then trim it.
485489
486490 integer :: i ! ! counter
487491
@@ -504,9 +508,11 @@ subroutine add_matrix(me,val,int_fmt,real_fmt,trim_str)
504508
505509 class(csv_file),intent (inout ) :: me
506510 class(* ),dimension (:,:),intent (in ) :: val ! ! the values to add
507- character (len=* ),intent (in ),optional :: int_fmt ! ! format string for integers
508- character (len=* ),intent (in ),optional :: real_fmt ! ! format string for reals
509- logical ,intent (in ),optional :: trim_str ! ! to trim the string
511+ character (len=* ),intent (in ),optional :: int_fmt ! ! if `val` is an integer, use
512+ ! ! this format string.
513+ character (len=* ),intent (in ),optional :: real_fmt ! ! if `val` is a real, use
514+ ! ! this format string.
515+ logical ,intent (in ),optional :: trim_str ! ! if `val` is a string, then trim it.
510516
511517 integer :: i ! ! counter
512518
0 commit comments