1 | module prep_file_m |
---|
2 | |
---|
3 | implicit none |
---|
4 | |
---|
5 | contains |
---|
6 | |
---|
7 | subroutine prep_file(unit, first_r, first_c, last_r, last_c, f_r_not_opt, & |
---|
8 | f_c_not_opt, l_r_not_opt, l_c_not_opt) |
---|
9 | |
---|
10 | ! This subroutine is used by "csvread". It fills non-optional |
---|
11 | ! arguments: first and last row, first and last column which will |
---|
12 | ! actually be read, taking information from the file itself if |
---|
13 | ! necessary. It also positions the input file on the first row to |
---|
14 | ! read. |
---|
15 | |
---|
16 | use opt_merge_m, only: opt_merge |
---|
17 | |
---|
18 | integer, intent(in):: unit ! logical unit for input file |
---|
19 | integer, intent(in), optional:: first_r ! (first row to read) |
---|
20 | integer, intent(in), optional:: first_c ! (first column to read) |
---|
21 | integer, intent(in), optional:: last_r ! (last row to read) |
---|
22 | integer, intent(in), optional:: last_c ! (last column to read) |
---|
23 | integer, intent(out):: f_r_not_opt ! (first row to read, not optional) |
---|
24 | integer, intent(out):: f_c_not_opt ! (first column to read, not optional) |
---|
25 | integer, intent(out):: l_r_not_opt ! (last row to read, not optional) |
---|
26 | integer, intent(out):: l_c_not_opt ! (last column to read, not optional) |
---|
27 | |
---|
28 | ! Variables local to the subprogram: |
---|
29 | integer iostat, i |
---|
30 | character c |
---|
31 | logical prev_value ! previous character was part of a value |
---|
32 | logical curr_value ! current character is part of a value |
---|
33 | |
---|
34 | !------------------------------------------------------ |
---|
35 | |
---|
36 | f_r_not_opt = opt_merge(first_r, 1) |
---|
37 | f_c_not_opt = opt_merge(first_c, 1) |
---|
38 | l_r_not_opt = opt_merge(last_r, 0) |
---|
39 | l_c_not_opt = opt_merge(last_c, 0) |
---|
40 | |
---|
41 | if (l_r_not_opt == 0) then |
---|
42 | ! Count the number of lines in the file: |
---|
43 | l_r_not_opt = 0 |
---|
44 | do |
---|
45 | read(unit, fmt=*, iostat=iostat) |
---|
46 | if (iostat /= 0) exit |
---|
47 | l_r_not_opt = l_r_not_opt + 1 |
---|
48 | end do |
---|
49 | if (l_r_not_opt == 0) stop 'Empty file.' |
---|
50 | |
---|
51 | rewind(unit) |
---|
52 | end if |
---|
53 | |
---|
54 | ! Go to first row to read: |
---|
55 | do i = 1, f_r_not_opt - 1 |
---|
56 | read(unit, fmt=*) |
---|
57 | end do |
---|
58 | |
---|
59 | if (l_c_not_opt == 0) then |
---|
60 | ! Count the number of values per line: |
---|
61 | l_c_not_opt = 0 |
---|
62 | curr_value = .false. |
---|
63 | do |
---|
64 | read(unit, fmt='(a)', advance='no', iostat=iostat) c |
---|
65 | if (iostat /= 0) exit |
---|
66 | prev_value = curr_value |
---|
67 | curr_value = c /= " " .and. c /= "," |
---|
68 | if (curr_value .and. .not. prev_value) l_c_not_opt = l_c_not_opt + 1 |
---|
69 | end do |
---|
70 | |
---|
71 | backspace(unit) |
---|
72 | end if |
---|
73 | |
---|
74 | print *, 'Reading column(s) ', f_c_not_opt, ':', l_c_not_opt, & |
---|
75 | ', row(s) ', f_r_not_opt, ':', l_r_not_opt |
---|
76 | |
---|
77 | end subroutine prep_file |
---|
78 | |
---|
79 | end module prep_file_m |
---|