6
6
7
7
module csv_module
8
8
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
12
13
13
14
implicit none
14
15
@@ -24,6 +25,9 @@ module csv_module
24
25
25
26
type,public :: csv_string
26
27
! ! a cell from a CSV file.
28
+ ! !
29
+ ! ! This use used to store the data internally
30
+ ! ! in the [[csv_file]] class.
27
31
private
28
32
character (len= :),allocatable :: str
29
33
end type csv_string
@@ -38,7 +42,7 @@ module csv_module
38
42
39
43
private
40
44
41
- logical :: verbose = .true . ! ! to print error messages
45
+ logical :: verbose = .false . ! ! to print error messages
42
46
43
47
character (len= 1 ) :: quote = ' "' ! ! quotation character
44
48
character (len= 1 ) :: delimiter = ' ,' ! ! delimiter character
@@ -128,17 +132,22 @@ subroutine initialize_csv_file(me,quote,delimiter,&
128
132
enclose_all_in_quotes ,&
129
133
logical_true_string ,&
130
134
logical_false_string ,&
135
+ chunk_size ,&
131
136
verbose )
132
137
133
138
implicit none
134
139
135
140
class(csv_file),intent (out ) :: me
136
141
character (len= 1 ),intent (in ),optional :: quote ! ! note: can only be one character
142
+ ! ! (Default is `"`)
137
143
character (len= 1 ),intent (in ),optional :: delimiter ! ! note: can only be one character
144
+ ! ! (Default is `,`)
138
145
logical ,intent (in ),optional :: enclose_strings_in_quotes ! ! if true, all string cells
139
146
! ! will be enclosed in quotes.
147
+ ! ! (Default is True)
140
148
logical ,intent (in ),optional :: enclose_all_in_quotes ! ! if true, *all* cells will
141
149
! ! be enclosed in quotes.
150
+ ! ! (Default is False)
142
151
character (len= 1 ),intent (in ),optional :: logical_true_string ! ! when writing a logical `true`
143
152
! ! value to a CSV file, this
144
153
! ! is the string to use
@@ -147,7 +156,10 @@ subroutine initialize_csv_file(me,quote,delimiter,&
147
156
! ! value to a CSV file, this
148
157
! ! is the string to use
149
158
! ! (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)
151
163
152
164
if (present (quote)) me% quote = quote
153
165
if (present (delimiter)) me% delimiter = delimiter
@@ -160,6 +172,10 @@ subroutine initialize_csv_file(me,quote,delimiter,&
160
172
if (present (logical_false_string)) &
161
173
me% logical_false_string = logical_false_string
162
174
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.
163
179
164
180
end subroutine initialize_csv_file
165
181
! *****************************************************************************************
@@ -299,21 +315,17 @@ end subroutine read_csv_file
299
315
300
316
! *****************************************************************************************
301
317
! >
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.
303
321
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 )
309
323
310
324
implicit none
311
325
312
326
class(csv_file),intent (inout ) :: me
313
327
character (len=* ),intent (in ) :: filename ! ! the CSV file to open
314
328
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
317
329
logical ,intent (out ) :: status_ok ! ! status flag
318
330
319
331
integer :: istat ! ! open `iostat` flag
@@ -322,18 +334,6 @@ subroutine open_csv_file(me,filename,&
322
334
323
335
me% n_cols = n_cols
324
336
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
-
337
337
open (newunit= me% iunit,file= filename,status= ' REPLACE' ,iostat= istat)
338
338
if (istat== 0 ) then
339
339
status_ok = .true.
@@ -367,25 +367,27 @@ end subroutine close_csv_file
367
367
! *****************************************************************************************
368
368
! >
369
369
! Add a cell to a CSV file.
370
+ !
371
+ ! @todo Need to check the `istat` values for errors.
370
372
371
373
subroutine add_cell (me ,val ,int_fmt ,real_fmt ,trim_str )
372
374
373
375
implicit none
374
376
375
377
class(csv_file),intent (inout ) :: me
376
378
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.
380
384
381
385
integer :: istat ! ! write `iostat` flag
382
386
character (len= :),allocatable :: ifmt ! ! actual format string to use for integers
383
387
character (len= :),allocatable :: rfmt ! ! actual format string to use for reals
384
388
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
389
391
390
392
! make sure the row isn't already finished
391
393
if (me% icol< me% n_cols) then
@@ -479,9 +481,11 @@ subroutine add_vector(me,val,int_fmt,real_fmt,trim_str)
479
481
480
482
class(csv_file),intent (inout ) :: me
481
483
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.
485
489
486
490
integer :: i ! ! counter
487
491
@@ -504,9 +508,11 @@ subroutine add_matrix(me,val,int_fmt,real_fmt,trim_str)
504
508
505
509
class(csv_file),intent (inout ) :: me
506
510
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.
510
516
511
517
integer :: i ! ! counter
512
518
0 commit comments