-
Notifications
You must be signed in to change notification settings - Fork 0
/
BASE_CSV_IO.f90
5259 lines (4374 loc) · 201 KB
/
BASE_CSV_IO.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!*******************************************************************************
! SVN $Id: BASE_CSV_IO.f90 14220 2023-06-18 08:28:31Z sbu062 $
!*******************************************************************************
! CSV_IO
! PURPOSE: This module provides high level functions for input and output in
! CSV (Comma Seperated Values) file format along with a few other
! basic utilities. CSV is accessible by LibreOffice and MS Excel.
! NOTES:
! (1) The typical workflow for output in CSV file format is like this:
! * CSV_FILE_OPEN_WRITE - physically open CSV file for writing;
! * CSV_FILE_HEADER_WRITE - physically write optional descriptive header
! (header is just the first line of the CSV file);
! * do -- loop (1) over records (rows of data file)
! do -- loop (2) over values within the same record
! CSV_RECORD_APPEND - produce record of data values
! of different types, append single values, usually in a loop
! end do -- end loop (2)
! CSV_FILE_RECORD_WRITE - physically write record of data
! end do -- end loop(1) -- go to producing next record;
! * CSV_FILE_CLOSE - physically closes the output CSV file.
! Thus, subs ending with _WRITE do physical write, as well as _CLOSE.
! (2) This module is most suited at this moment (version Nov-2015) for CSV
! file output rather than input. Input CSV is to be done.
! (3) This module widely uses optional arguments. They could be called
! using named parameters, e.g.
! this way (the first optional parameter absent):
! intNextunit = GET_FREE_FUNIT(file_status=logicalFlag)
! or (both parameters present but swapped):
! intNextunit = GET_FREE_FUNIT(file_status=logicalFlag, max_funit=200)
! or (optional parameters absent altogether):
! intNextunit = GET_FREE_FUNIT()
! or (standard way)
! intNextunit = GET_FREE_FUNIT(200, logicalFlag)
! (4) Files can be referred either by unit or by name, but unit has
! precedence (if both a provided, unit is used).
!
! Author: Sergey Budaev, based on csv_io functions by John Burkardt
!*******************************************************************************
module CSV_IO
use BASE_STRINGS, only : VALUE, PARSE, SPLIT, COMPACT, IS_NUMERIC, DELALL
implicit none
! Precision constants
integer, parameter, private :: R4 = selected_real_kind(6, 37)
integer, parameter, private :: R8 = selected_real_kind(15, 307)
integer, parameter, private :: QP = selected_real_kind(33, 4931)
! Integer kinds (from STRINGS)
integer, parameter, private :: ki4=selected_int_kind(9) ! single integer
integer, parameter, private :: ki8=selected_int_kind(18) ! double integer
!Complex kinds (from STRINGS)
integer, parameter, private :: kc4 = R4 ! single complex
integer, parameter, private :: kc8 = R8 ! double complex
! Public constants
! Maximum unit number (in old Fortran units range 1..99)
integer, public, parameter :: MAX_UNIT=255
! These constants are defined in the intrinsic module ISO_FORTRAN_ENV,
! but may be redefined here if compiler doesn't use this. It is not really
! necessary in most cases and might be dangerous if this particular
! platform/compiler actually implements a different assignment.
! Use this intrinsic module as follows:
! use, intrinsic :: ISO_FORTRAN_ENV
! So, here it is commented it out for portability and standard compliance:
!integer, public, parameter :: INPUT_UNIT=5 ! standard ISO values for input,
!integer, public, parameter :: OUTPUT_UNIT=6 ! output, and standard
!integer, public, parameter :: ERROR_UNIT=9 ! error units
! Module name for the DEBUG LOGGER: every function/sub must also have
! the PROCNAME parameter referring to its name. This is done for the Debug
! Logger module. Each module must also have a DEBUG Logger subroutine, that
! is a wrapper to module LOGGER (or perhaps any other that is being used)
! procedure name PROCNAME
character (len=*), private, parameter :: MODNAME = "CSV_IO"
! Set the debug mode to ON or OFF, in the debug mode, events are written to
! the log, determined by the module local LOG_DBG subroutine, normally, a
! wrapper to the module LOGGER. May also define integer DEBUG_LEVEL parameter...
logical, private, parameter :: IS_DEBUG = .FALSE.
!*******************************************************************************
! DERIVED TYPE for CSV FILE HANDLE
! Define derived type csv_file structure for keeping csv file handle:
!*******************************************************************************
!
! PORTABILITY NOTE for derived type file handle:
! character (len=:), allocatable :: name -- works on Oracle F95 and probably
! some other, but gfortran prior to v. 5 issues this error:
!
! Deferred-length character component 'name' at (1) is not yet supported
!
! Therefore, use fixed length string for portability here; don't forget that
! concatenation of fixed strings requires trim() to avoid empty holes in
! the result or no result, e.g.:
! type (csv_file) :: zoutput
! zoutput%name= trim(directory) // "file_" // TOSTR(number) // ".csv"
! We also define the maximum length of file name string as a parameter,
! is it (255) enough length for full file path?
integer, public, parameter :: MAX_FILENAME=255
type, public :: csv_file
!character (len=:), allocatable :: name ! name of file :: disabled, unportable
character (len=MAX_FILENAME) :: name ! the name of the file
integer :: unit = -1 ! file-handle unit, default auto
logical :: status = .TRUE. ! flag for success of latest operation
end type csv_file
!*******************************************************************************
! GENERIC INTERFACES
! Generic interfaces to the modules. These allow calling CSV_RECORD_APPEND
! generically, for different data types, which are selected by the module
! automatically. e.g. just call CSV_RECORD_APPEND irrespective of the data type
!*******************************************************************************
! Generic interfaces for record building, one value or array or list
interface CSV_RECORD_APPEND
module procedure CSV_RECORD_APPEND_I4
module procedure CSV_RECORD_APPEND_I8
module procedure CSV_RECORD_APPEND_R4
module procedure CSV_RECORD_APPEND_R8
module procedure CSV_RECORD_APPEND_S
module procedure CSV_RECORD_APPEND_ARRAY_I4
module procedure CSV_RECORD_APPEND_ARRAY_I8
module procedure CSV_RECORD_APPEND_ARRAY_R4
module procedure CSV_RECORD_APPEND_ARRAY_R8
module procedure CSV_RECORD_APPEND_ARRAY_S
module procedure CSV_RECORD_APPEND_LST_I4
module procedure CSV_RECORD_APPEND_LST_I8
module procedure CSV_RECORD_APPEND_LST_R4
module procedure CSV_RECORD_APPEND_LST_R8
module procedure CSV_RECORD_APPEND_LST_S
end interface CSV_RECORD_APPEND
! Generic interfaces for whole array/matrix operations with arbitrary data types
interface CSV_MATRIX_WRITE
module procedure CSV_MATRIX_WRITE_I4
module procedure CSV_MATRIX_WRITE_R4
module procedure CSV_MATRIX_WRITE_R8
module procedure CSV_MATRIX_WRITE_S
module procedure CSV_ARRAY_WRITE_I4
module procedure CSV_ARRAY_WRITE_R4
module procedure CSV_ARRAY_WRITE_R8
module procedure CSV_ARRAY_WRITE_S
end interface CSV_MATRIX_WRITE
interface CSV_ARRAY_WRITE
module procedure CSV_ARRAY_WRITE_I4
module procedure CSV_ARRAY_WRITE_R4
module procedure CSV_ARRAY_WRITE_R8
module procedure CSV_ARRAY_WRITE_S
end interface CSV_ARRAY_WRITE
! Generic interfaces for physical read/write using the derived type file handle
interface CSV_OPEN_READ
module procedure CSV_FILE_OPEN_READ
module procedure CSV_FILE_OPEN_READ_T
end interface CSV_OPEN_READ
interface CSV_OPEN_WRITE
module procedure CSV_FILE_OPEN_WRITE
module procedure CSV_FILE_OPEN_WRITE_T
end interface CSV_OPEN_WRITE
interface CSV_CLOSE
module procedure CSV_FILE_CLOSE
module procedure CSV_FILE_CLOSE_T
end interface CSV_CLOSE
interface CSV_HEADER_WRITE
module procedure CSV_FILE_HEADER_WRITE
module procedure CSV_FILE_HEADER_WRITE_T
end interface CSV_HEADER_WRITE
interface CSV_RECORD_WRITE
module procedure CSV_FILE_RECORD_WRITE
module procedure CSV_FILE_RECORD_WRITE_T
end interface CSV_RECORD_WRITE
interface CHECK_FILE_OPEN
module procedure CHECK_FILE_OPEN_S
module procedure CHECK_FILE_OPEN_T
end interface CHECK_FILE_OPEN
interface CSV_GUESS_RECORD_LENGTH
module procedure CSV_GUESS_RECORD_LEN_I4
module procedure CSV_GUESS_RECORD_LEN_R4
module procedure CSV_GUESS_RECORD_LEN_R8
end interface CSV_GUESS_RECORD_LENGTH
interface CSV_MATRIX_READ
module procedure CSV_MATRIX_READ_R4
module procedure CSV_MATRIX_READ_R8
end interface CSV_MATRIX_READ
! They are identical in CSV_IO and BASE_UTILS. Private here to avoid possible
! name conflicts, do we need them outside?
private :: I4_WIDTH, I8_WIDTH, I4_LOG_10, I8_LOG_10, STR_ITOA_LZ, STR_ITOA, CLEANUP
! This wrapper DEBUG LOG is used only for debugging this module. It may
! not or may use the module LOGGER, if not, (normally) it can be used as a
! stand-alone module in other projects...
private :: LOG_DBG
!-------------------------------------------------------------------------------
contains !-----[ SUBROUTINES AND FUNCTIONS FOLLOW ]----------------------------
subroutine LOG_DBG(message_string, procname, modname)
!*******************************************************************************
! LOG_DBG
! PURPOSE: This subroutine is a wrapper for writing debug messages. It can
! either just print to STDERR or use the module LOGGER. Here the selection
! of the module behaviour is made by commenting out unused code, possibly
! change to conditional compilation with preprocessor... but portability will
! be problem in such a case...
! NOTE: To use the LOGGER module, it must be compiled before CSV_IO, so
! (re)place LOGGER first in the Makefile.
!*******************************************************************************
!#ifdef USE_LOGGER_MODULE
!use LOGGER ! we might need logger later
!#endif
use, intrinsic :: ISO_FORTRAN_ENV ! need it for write(ERROR_UNIT, *)
implicit none
! Calling parameters
character(len=*), intent(in) :: message_string
character (len=*), optional, intent(in) :: procname
character (len=*), optional, intent(in) :: modname
! Local variables
character (len=:), allocatable :: prefix_msg
!-----------------------------------------------------------------------------
if (IS_DEBUG) then ! Only if IS_DEBUG is set to TRUE, this sub is not
! used in normal operation when not debugging
! We first generate the message prefix containing module and procedure name
if (present(procname)) then
if (present(modname)) then
prefix_msg="MODULE:" // modname // "PROCEDURE: " // procname // ":: "
else
prefix_msg="PROCEDURE: " // procname // ":: "
end if
else
if (present(modname)) then
prefix_msg="MODULE:" // modname // ":: "
else
prefix_msg=""
end if
end if
! Second, we print the message prefix + message
!#ifdef USE_LOGGER_MODULE
!call LOG_MSG( prefix_msg // message_string ) ! use module LOGGER
!# else
write(ERROR_UNIT, *) prefix_msg, message_string
!#endif
end if
end subroutine LOG_DBG
!-------------------------------------------------------------------------------
function CHECK_UNIT_VALID (file_unit) result (file_status)
!*******************************************************************************
! CHECK_UNIT_VALID
! PURPOSE: Checks if file unit is valid, that is within the allowed range and
! doesn't include standard input/output/stderr units. The unit should
! not necessarily be linked to any file or be an open file unit.
! CALL PARAMETERS:
! integer file unit
! RETURNS:
! logical status (true if valid)
!*******************************************************************************
use, intrinsic :: ISO_FORTRAN_ENV ! Provides system-wide scalar constants
! INPUT_UNIT OUTPUT_UNIT ERROR_UNIT
implicit none
! Calling parameters
integer, intent(in) :: file_unit
logical :: file_status
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "CHECK_UNIT_VALID"
!-----------------------------------------------------------------------------
if (file_unit > 0 .and. file_unit < MAX_UNIT .and. &
file_unit /= INPUT_UNIT .and. &
file_unit /= OUTPUT_UNIT .and. &
file_unit /= ERROR_UNIT ) then
file_status = .TRUE.
else
file_status = .FALSE.
end if
end function CHECK_UNIT_VALID
!-------------------------------------------------------------------------------
function GET_FREE_FUNIT (file_status, max_funit) result (file_unit)
!*******************************************************************************
! GET_FREE_FUNIT
! PURPOSE: returns the first free Fortran unit number (search in 1 to MAX_UNIT).
! RETURNS:
! Integer unit number
! CALL PARAMETERS:
! optional logical execution error status (.TRUE.)
! optional integer max_funit to search (default MAX_UNIT defined in mudule)
!
! Author: John Burkardt : This code is distributed under the GNU LGPL license.
! Modified by Sergey Budaev
!*******************************************************************************
use, intrinsic :: ISO_FORTRAN_ENV ! Provides system-wide scalar constants
! INPUT_UNIT OUTPUT_UNIT ERROR_UNIT
implicit none
! Function value
integer :: file_unit
! Calling parameters
logical, optional, intent(out) :: file_status
integer, optional, intent(in) :: max_funit
! Local variables: copies of optional parameters we need co copy optional
logical :: file_status_here ! variables in case they are absent, so always
integer :: max_funit_here ! work with copies of optionals inside
! Other local variables
integer :: i
integer :: ios
logical :: lopen
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "GET_FREE_FUNIT"
!-----------------------------------------------------------------------------
file_unit = 0
file_status_here = .FALSE.
if (present(max_funit)) then
max_funit_here=max_funit
else
max_funit_here=MAX_UNIT ! max from globals
end if
do i=1, max_funit_here
if (i /= INPUT_UNIT .and. i /= OUTPUT_UNIT .and. &
i /= ERROR_UNIT) then ! exclude standard console units
inquire (unit=i, opened=lopen, iostat=ios)
if (ios == 0) then
if (.not. lopen) then
file_unit = i ! First free unit found
file_status_here=.TRUE.
if (present(file_status)) file_status=file_status_here
return
end if
end if
end if
end do
if (.not. file_status_here) file_unit=-1 ! if no free unit found return -1
if (present(file_status)) file_status=file_status_here ! and error flag
end function GET_FREE_FUNIT
!-------------------------------------------------------------------------------
function GET_FILE_UNIT (csv_file_name, csv_file_status) &
result (csv_file_unit)
!*******************************************************************************
! GET_FILE_UNIT
! PURPOSE: returns file unit associated with an existing open file name,
! if no file unit is associated with this name (file is not opened),
! return unit=-1 and error status
! CALL PARAMETERS:
! Character file name
! Optional logical execution error status (.TRUE.)
! RETURNS: Integer file unit
!
! Author: Sergey Budaev
!*******************************************************************************
implicit none
! Function value
integer :: csv_file_unit
! Calling parameters
character (len=*), intent(in) :: csv_file_name
logical, optional, intent(out) :: csv_file_status
! Local variables, copies of optionals and intent-in
character (len=:), allocatable :: csv_file_name_here
logical :: csv_file_status_here
! Local variables
integer :: file_error_status
logical :: openedq
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "GET_FILE_UNIT"
!-----------------------------------------------------------------------------
csv_file_name_here=csv_file_name ! copy name as it is intent-in
!csv_file_unit=-1 ! this is default if no unit linked
inquire(file=csv_file_name_here, number=csv_file_unit, opened=openedq, &
iostat=file_error_status)
! Check if there were errors inquiring
if (file_error_status==0) then
csv_file_status_here=.TRUE.
else
csv_file_status_here=.FALSE. ! there was an error inquiring, go back
if (present(csv_file_status)) & ! with error flag
csv_file_status=csv_file_status_here
csv_file_unit=-1
return
end if
if (openedq) then ! Check if this file is opened
csv_file_status_here=.TRUE. ! and set error flag
else
csv_file_status_here=.FALSE.
end if
if (present(csv_file_status)) csv_file_status=csv_file_status_here
end function GET_FILE_UNIT
!-------------------------------------------------------------------------------
function CHECK_FILE_OPEN_S (csv_file_name, csv_file_unit, csv_file_status, &
get_csv_file_unit) result (file_opened)
!*******************************************************************************
! CHECK_FILE_OPEN
! PURPOSE: Check if a file identified by name or unit is opened for
! reading/writing, returns TRUE if opened, FALSE otherwise
! If there was an error reading the file, returns
! csv_file_status FALSE. Optionally also return the file unit
! associated with the file name if it is open (-1 otherwise).
! CALL PARAMETERS:
! Character file name or integer file unit,
! Optional logical execution error status (.TRUE.) and
! optional output file unit
! RETURNS: Logical flag
!
! Author: Sergey Budaev
!*******************************************************************************
implicit none
! Function value
logical :: file_opened ! file_exist
! Calling parameters
character (len=*), optional, intent(in) :: csv_file_name
integer, optional, intent(in) :: csv_file_unit
logical, optional, intent(out) :: csv_file_status
integer, optional, intent(out) :: get_csv_file_unit
! Local variables, copies of optionals and intent-in
character (len=:), allocatable :: csv_file_name_here
integer :: csv_file_unit_here
logical :: csv_file_status_here
integer :: get_csv_file_unit_here
! Local variables
integer :: file_error_status
logical :: openedq
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "CHECK_FILE_OPEN"
!-----------------------------------------------------------------------------
! exit if neither name nor unit given
if(.not. present(csv_file_name) .and. .not. present(csv_file_unit)) then
if (present(csv_file_status)) csv_file_status=.FALSE.
if (present(get_csv_file_unit)) get_csv_file_unit=-1
file_opened = .FALSE.
return
end if
if (present(csv_file_unit)) then ! if we got unit, just ignore the name
csv_file_unit_here=csv_file_unit
get_csv_file_unit_here=csv_file_unit_here
inquire(unit=csv_file_unit_here, opened=openedq, iostat=file_error_status)
else
csv_file_name_here=csv_file_name
inquire(file=csv_file_name_here, number=get_csv_file_unit_here, &
opened=openedq, iostat=file_error_status)
end if
! Check if there were errors inquiring
if (file_error_status==0) then
csv_file_status_here=.TRUE.
else ! if there was an error inquiring, go back
csv_file_status_here=.FALSE. ! with error flag
if (present(csv_file_status)) csv_file_status=csv_file_status_here
if (present(get_csv_file_unit)) get_csv_file_unit=-1
file_opened = .FALSE.
return
end if
file_opened = openedq ! get the logical flag
if (.not. file_opened) get_csv_file_unit_here=-1
if (present(get_csv_file_unit)) get_csv_file_unit=get_csv_file_unit_here
if (present(csv_file_status)) csv_file_status=csv_file_status_here
end function CHECK_FILE_OPEN_S
!-------------------------------------------------------------------------------
function CHECK_FILE_OPEN_T (csv_file_handle) result (file_opened)
!*******************************************************************************
! CHECK_FILE_OPEN_T
! PURPOSE: Check if a file identified by name or unit is opened for
! reading/writing, returns TRUE if opened, FALSE otherwise
! CALL PARAMETERS:
! File handle object of the type csv_file
! RETURNS: Logical flag
!
! Author: Sergey Budaev
!*******************************************************************************
implicit none
! Function value
logical :: file_opened
! Calling parameters
type(csv_file) :: csv_file_handle
file_opened = CHECK_FILE_OPEN_S(csv_file_name=csv_file_handle%name, &
csv_file_unit=csv_file_handle%unit, &
csv_file_status=csv_file_handle%status)
end function CHECK_FILE_OPEN_T
!-------------------------------------------------------------------------------
subroutine CSV_FILE_OPEN_READ (csv_file_name, csv_file_unit, csv_file_status)
!*******************************************************************************
! CSV_FILE_OPEN_READ
! PURPOSE: opens a CSV file for reading. Optionally also sets a free file unit
! for all subsequent input to this file.
! CALL PARAMETERS:
! Character CSV_FILE_NAME, the name of the file.
! Integer CSV_FILE_UNIT, file unit number (<1 then returns first free unit)
! Logical CSV_FILE_STATUS, .TRUE. if successfull, no errors
! Author: John Burkardt : This code is distributed under the GNU LGPL license.
! Modified by Sergey Budaev
! USES: GET_FREE_FUNIT from the same module
! NOTES: Units 5 and 6 are usually for standard input and output (terminal)
! F2003 uses INPUT_UNIT and OUTPUT_UNIT for that (portability).
! The subroutine has no error handling itself, use CSV_FILE_STATUS for
! checking the file write status.
!*******************************************************************************
implicit none
! Calling parameters
character (len=*), intent(in) :: csv_file_name
integer :: csv_file_unit ! intent(inout), doesn't work with literals
logical, optional, intent(out) :: csv_file_status
! Local variables, copies of optionals
logical :: csv_file_status_here
! Local variables
integer :: file_error_status
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "CSV_FILE_OPEN_READ"
!-----------------------------------------------------------------------------
! First we get a free input unit if one is not provided (invalid, e.g. -1)
if (.not. CHECK_UNIT_VALID(csv_file_unit)) then
csv_file_unit=GET_FREE_FUNIT(csv_file_status_here, MAX_UNIT)
if (.not. csv_file_status_here) then
if(present(csv_file_status)) csv_file_status=csv_file_status_here
return ! Return back (with error flag) if no units available
end if
end if
open (unit=csv_file_unit, file=csv_file_name, status='old', &
iostat=file_error_status)
if (file_error_status==0) then
csv_file_status_here=.TRUE. ! No error
else
csv_file_status_here=.FALSE. ! File error occurred
end if
if(present(csv_file_status)) csv_file_status=csv_file_status_here
end subroutine CSV_FILE_OPEN_READ
!-------------------------------------------------------------------------------
subroutine CSV_FILE_OPEN_READ_T (csv_file_handle)
!*******************************************************************************
! CSV_FILE_OPEN_READ_T
! PURPOSE: opens a CSV file for reading. Wrapper using file handle derived type
! CALL PARAMETERS:
! csv file handle of the type csv_file defined in this module
! Author: Sergey Budaev
!*******************************************************************************
implicit none
! Calling parameters
type(csv_file), intent(inout) :: csv_file_handle
call CSV_FILE_OPEN_READ( csv_file_handle%name, &
csv_file_handle%unit, &
csv_file_handle%status )
end subroutine CSV_FILE_OPEN_READ_T
!-------------------------------------------------------------------------------
subroutine CSV_FILE_OPEN_WRITE (csv_file_name, csv_file_unit, csv_file_status)
!*******************************************************************************
! CSV_FILE_OPEN_WRITE
! PURPOSE: opens a CSV file for writing. Optionally also sets a free file unit
! for all subsequent output to this file.
! CALL PARAMETERS:
! Character CSV_FILE_NAME, the name of the file.
! Integer CSV_FILE_UNIT, file unit number (<1 then returns first free unit)
! Logical CSV_FILE_STATUS, .TRUE. if successfull, no errors
! Author: John Burkardt : This code is distributed under the GNU LGPL license.
! Modified by Sergey Budaev
! USES: GET_FREE_FUNIT from the same module
! NOTES: Units 5 and 6 are usually for standard input and output (terminal)
! F2003 uses INPUT_UNIT and OUTPUT_UNIT for that (portability).
! The subroutine has no error handling itself, use CSV_FILE_STATUS for
! checking the file write status.
!*******************************************************************************
implicit none
! Calling parameters
character (len=*), intent(in) :: csv_file_name
integer :: csv_file_unit ! intent(inout), doesn't work with literals
logical, optional, intent(out) :: csv_file_status
! Local variables, copies of optionals
logical :: csv_file_status_here
! Local variables
integer :: file_error_status
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "CSV_FILE_OPEN_WRITE"
!-----------------------------------------------------------------------------
! First we get a free input unit if one is not provided (invalid, e.g. -1)
if (.not. CHECK_UNIT_VALID(csv_file_unit)) then
csv_file_unit=GET_FREE_FUNIT(csv_file_status_here, MAX_UNIT)
if (.not. csv_file_status_here) then
if(present(csv_file_status)) csv_file_status=csv_file_status_here
return ! Return back (with error flag) if no units available
end if
end if
open (unit=csv_file_unit, file=csv_file_name, status='replace', &
iostat=file_error_status)
if (file_error_status==0) then
csv_file_status_here=.TRUE. ! No error
else
csv_file_status_here=.FALSE. ! File error occurred
end if
if(present(csv_file_status)) csv_file_status=csv_file_status_here
end subroutine CSV_FILE_OPEN_WRITE
!-------------------------------------------------------------------------------
subroutine CSV_FILE_OPEN_WRITE_T (csv_file_handle)
!*******************************************************************************
! CSV_FILE_OPEN_WRITE_T
! PURPOSE: opens a CSV file for writing. Wrapper using file handle derived type
! CALL PARAMETERS:
! csv file handle of the type csv_file defined in this module
! Author: Sergey Budaev
!*******************************************************************************
implicit none
! Calling parameters
type(csv_file), intent(inout) :: csv_file_handle
call CSV_FILE_OPEN_WRITE( csv_file_handle%name, &
csv_file_handle%unit, &
csv_file_handle%status )
end subroutine CSV_FILE_OPEN_WRITE_T
!-------------------------------------------------------------------------------
subroutine CSV_FILE_CLOSE (csv_file_name, csv_file_unit, csv_file_status)
!*******************************************************************************
! CSV_FILE_CLOSE
! PURPOSE: closes a CSV file for reading or writing.
! CALL PARAMETERS:
! Character CSV_FILE_NAME, the name of the file.
! Integer CSV_FILE_UNIT, file unit number (<1 then returns first free unit)
! Logical CSV_FILE_STATUS, .TRUE. if successfull, no errors
! USES: GET_FILE_UNIT from the same module
! Author: John Burkardt : This code is distributed under the GNU LGPL license.
! Modified by Sergey Budaev
!*******************************************************************************
implicit none
! Calling parameters
character (len=*), optional, intent(in) :: csv_file_name
integer, optional :: csv_file_unit ! intent(inout), doesn't work with literals
logical, optional, intent(out) :: csv_file_status
! Local variables, copies of optionals
integer :: csv_file_unit_here
logical:: csv_file_status_here
! Local variables
integer :: file_error_status
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "CSV_FILE_CLOSE"
!-----------------------------------------------------------------------------
csv_file_status_here=.FALSE.
if (.not. present(csv_file_unit)) then
if (present(csv_file_name)) then
! determine file unit from existing file name
csv_file_unit_here=GET_FILE_UNIT(csv_file_name, &
csv_file_status_here)
if (.not. csv_file_status_here) then ! if there was error
if (present(csv_file_status)) & ! inquiring file status,
csv_file_status=.FALSE. ! return with error flag
return
end if
else !if (.not. present(csv_file_name))
! neither file name nor unit specified, set error status
if (present(csv_file_status)) csv_file_status=.FALSE.
return
end if
else
! file unit present in the list of arguments
csv_file_unit_here=csv_file_unit
end if
close (unit = csv_file_unit_here, iostat=file_error_status)
! Check IO errors and report back if optional args are present
if (file_error_status==0) then
if (present(csv_file_status)) csv_file_status=.TRUE. ! No error
else
if (present(csv_file_status)) csv_file_status=.FALSE. ! File error
end if
end subroutine CSV_FILE_CLOSE
!-------------------------------------------------------------------------------
subroutine CSV_FILE_CLOSE_T (csv_file_handle)
!*******************************************************************************
! CSV_FILE_CLOSE_T
! PURPOSE: closes a CSV file for reading or writing. Wrapper using file handle
! derived type
! CALL PARAMETERS:
! csv file handle of the type csv_file defined in this module
! Author: Sergey Budaev
!*******************************************************************************
implicit none
! Calling parameters
type(csv_file), intent(inout) :: csv_file_handle
call CSV_FILE_CLOSE ( csv_file_handle%name, &
csv_file_handle%unit, &
csv_file_handle%status )
end subroutine CSV_FILE_CLOSE_T
!-------------------------------------------------------------------------------
subroutine CSV_FILE_HEADER_WRITE (csv_file_name, csv_file_unit, header, &
csv_file_status)
!*******************************************************************************
! CSV_FILE_HEADER_WRITE
! PURPOSE: writes a header to a CSV file. Should normally be the first line.
! CALL PARAMETERS:
! Optional character CSV_FILE_NAME, the name of the file.
! Optional integer CSV_FILE_UNIT, file unit number
! Cherecter HEADER
! Optional logical CSV_FILE_STATUS, .TRUE. if successfull, no errors
! USES: GET_FILE_UNIT from the same module
! Author: John Burkardt : This code is distributed under the GNU LGPL license.
! Modified by Sergey Budaev
!*******************************************************************************
implicit none
! Calling parameters
character (len=*), optional, intent(in) :: csv_file_name
integer, optional :: csv_file_unit ! intent(inout), doesn't work with literals
character (len=*), intent(in) :: header
logical, optional, intent(out) :: csv_file_status
! Local variables, copies of optionals
integer :: csv_file_unit_here
logical :: csv_file_status_here
! Local variables
integer :: file_error_status
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "CSV_FILE_HEADER_WRITE"
!-----------------------------------------------------------------------------
if (.not. present(csv_file_unit)) then
if (present(csv_file_name)) then
! determine file unit from existing file name
csv_file_unit_here=GET_FILE_UNIT(csv_file_name, &
csv_file_status_here)
if (.not. csv_file_status_here) then ! if there was error
if (present(csv_file_status)) & ! inquiring file status,
csv_file_status=.FALSE. ! return with error flag
return
end if
else !if (.not. present(csv_file_name))
! neither file name nor unit specified, set error status
if (present(csv_file_status)) csv_file_status=.FALSE.
return
end if
else
! file unit present in the list of arguments
csv_file_unit_here=csv_file_unit
end if
write (unit=csv_file_unit_here, fmt='(a)', &
iostat=file_error_status) trim(header)
! Check IO errors
if (file_error_status==0) then
if (present(csv_file_status)) csv_file_status=.TRUE. ! No error
else
if (present(csv_file_status)) csv_file_status=.FALSE. ! File error
end if
end subroutine CSV_FILE_HEADER_WRITE
!-------------------------------------------------------------------------------
subroutine CSV_FILE_HEADER_WRITE_T(header, csv_file_handle)
!*******************************************************************************
! CSV_FILE_HEADER_WRITE_T
! PURPOSE: writes a header to a CSV file. Should normally be the first line.
! Wrapper using file handle derived type
! CALL PARAMETERS:
! arbitrary string header
! csv file handle of the type csv_file defined in this module
! Author: Sergey Budaev
!*******************************************************************************
implicit none
! Calling parameters
character (len=*), intent(in) :: header
type(csv_file), intent(inout) :: csv_file_handle
call CSV_FILE_HEADER_WRITE ( csv_file_handle%name, &
csv_file_handle%unit, &
header, &
csv_file_handle%status )
end subroutine CSV_FILE_HEADER_WRITE_T
!-------------------------------------------------------------------------------
function CSV_FILE_LINES_COUNT (csv_file_name, numeric_only, csv_file_status) &
result (line_num)
!*******************************************************************************
! CSV_FILE_LINE_COUNT
! PURPOSE: counts the number of lines in a CSV file.
! CALL PARAMETERS:
! Optional character CSV_FILE_NAME, the name of the file.
! Optional flag indicating whether to count only numeric lines (if .TRUE.)
! (numeric are only lines including digits, spaces, comma and colon);
! Optional Optional logical CSV_FILE_STATUS, .TRUE. if no errors
! USES: GET_FREE_FUNIT from the same module
! NOTE: This routine does not try to distinguish the possible header line,
! blank lines, or cases where a single CSV record extends over multiple
! lines. It simply counts the number of lines.
! Author: John Burkardt : This code is distributed under the GNU LGPL license.
! Modified by Sergey Budaev
!*******************************************************************************
implicit none
! Function value
integer :: line_num
! Calling parameters
character (len=*), intent(in) :: csv_file_name
logical, optional, intent(in) :: numeric_only
logical, optional, intent(out) :: csv_file_status
! Local variables, copies of optionals
logical :: numeric_only_here
logical :: csv_file_status_here
! Local variables
integer :: input_status
integer :: input_unit
!character (len=2) :: line ! We only need to count rows here
character (len=:), allocatable :: line
! Subroutine name for DEBUG LOGGER
character (len=*), parameter :: PROCNAME = "CSV_FILE_LINES_COUNT"
!-----------------------------------------------------------------------------
if(present(numeric_only)) then
numeric_only_here = numeric_only
else
numeric_only_here = .FALSE.
end if
! May need initialise logical on some platforms/compilers
if (present(csv_file_status)) csv_file_status = .TRUE.
line_num = -1
input_unit=GET_FREE_FUNIT(csv_file_status_here, MAX_UNIT)
if (.not. csv_file_status_here) then
if (present(csv_file_status)) csv_file_status=.FALSE.
return ! Return back (with error flag) if no units available
end if
open (unit=input_unit, file=csv_file_name, status='old', &
iostat=input_status )
! check if we can open the file, if not, issue error flag and go back
if ( input_status /= 0 ) then
if (present(csv_file_status)) csv_file_status=.FALSE.
return
end if