12 integer,
parameter ::
dp = kind(0.d0)
13 integer,
parameter :: max_input_files = 10000
14 integer,
parameter :: out_unit = 40
15 integer,
parameter :: in_unit_start = 41
17 character(len=1000) :: filename
19 character(len=1000) :: word1, word2
20 logical :: eol1, eol2, eof1, eof2
21 real(kind=
dp) :: total
22 integer :: row, col, i_file, n_file
23 integer :: in_units(max_input_files)
26 if (command_argument_count() < 2)
then
27 write(6,*)
'Usage: numeric_average <out_filename>' &
28 //
' <in_filename_1> ... <in_filename_N>'
31 n_file = command_argument_count() - 1
32 if (n_file > max_input_files)
then
33 write(0,*)
'ERROR: Too many input files'
36 call get_command_argument(1, filename)
37 write(*,*)
"averaging output: ", trim(filename)
38 open(unit=out_unit, file=filename, iostat=ios)
40 write(0,
'(a,a,a,i4)')
'ERROR: unable to open file ', &
41 trim(filename),
' for writing: ', ios
45 call get_command_argument(i_file + 1, filename)
46 in_units(i_file) = in_unit_start + i_file - 1
47 write(*,*)
"averaging input: ", trim(filename)
48 open(unit=in_units(i_file), status=
'old', file=filename, iostat=ios)
50 write(0,
'(a,a,a,i4)')
'ERROR: unable to open file ', &
51 trim(filename),
' for reading: ', ios
63 if (len(word1) > 0)
then
68 if (((len(word1) > 0) .and. (len(word2) == 0)) &
69 .or. ((len(word1) > 0) .and. (len(word2) == 0)) &
70 .or. (eol1 .and. (.not. eol2)) &
71 .or. ((.not. eol1) .and. eol2) &
72 .or. (eof1 .and. (.not. eof2)) &
73 .or. ((.not. eof1) .and. eof2))
then
74 write(*,
'(a,i8,i8,i8)')
'different shape at row/col/file:', &
78 if (len(word1) > 0)
then
82 if (len(word1) > 0)
then
90 write(out_unit,
'(e30.15e3)', advance=
'no') &
91 (total / real(n_file, kind=
dp))
92 if (eol1)
write(out_unit,
'(a)')
''
99 close(in_units(i_file))
110 character(len=*),
intent(in) :: string
115 read(string,
'(e40.0)', iostat=ios) val
117 write(0,
'(a,a,a,i3)')
'Error converting ', trim(string), &
118 ' to real: IOSTAT = ', ios
132 character(len=*),
intent(inout) :: line
137 if (ichar(line(i:i)) == 9)
then
155 integer,
intent(in) :: unit
157 character,
intent(out) :: char
159 logical,
intent(out) :: eol
161 logical,
intent(out) :: eof
163 integer :: ios, n_read
164 character(len=1) :: read_char
170 read(unit=unit, fmt=
'(a)', advance=
'no',
end=100, eor=110, &
171 iostat=ios) read_char
173 write(0,*)
'ERROR: reading file: IOSTAT = ', ios
199 integer,
intent(in) :: unit
201 character(len=*),
intent(out) :: word
203 logical,
intent(out) :: eol
205 logical,
intent(out) :: eof
214 do while (((ichar(char) == 9) .or. (ichar(char) == 32)) &
215 .and. (.not. eol) .and. (.not. eof))
218 if (eol .or. eof)
return
224 do while ((ichar(char) /= 9) .and. (ichar(char) /= 32) &
225 .and. (.not. eol) .and. (.not. eof))