Open, read in, and process the GROMACS-style index file, storing information in memory.
68 class(indexfile),
intent(inout) :: this
69 character (len=*),
intent(in) :: filename
70 character (len=2048) :: line, ncols_string, fmt_string
71 integer :: index_file_unit, io_status, ngrps, i, j, ncols
72 integer,
allocatable :: indices_tmp(:), title_loc(:), num_array(:)
74 integer,
intent(in),
optional :: n
76 write(error_unit,
'(a)') prompt//
"Reading in "//trim(filename)//
"." 79 inquire(file=trim(filename), exist=ex)
80 if (ex .eqv. .false.)
call error_stop_program(trim(filename)//
" does not exist.")
83 open(newunit=index_file_unit, file=trim(filename), status=
"old")
84 read(index_file_unit,
'(a)', iostat=io_status) line
85 if (index(line,
"[") .eq. 0)
call error_stop_program(trim(filename)//
" is not a valid index file.")
88 rewind index_file_unit
91 do while (io_status .eq. 0)
92 read(index_file_unit,
'(a)', iostat=io_status) line
93 if (io_status .ne. 0)
goto 100
94 if (index(line,
"[") .ne. 0) ngrps = ngrps + 1
99 if (
allocated(this%group))
deallocate(this%group)
100 allocate(this%group(ngrps), title_loc(ngrps+1))
103 rewind index_file_unit
107 do while (io_status .eq. 0)
109 read(index_file_unit,
'(a)', iostat=io_status) line
110 if (io_status .ne. 0)
goto 200
111 if (index(line,
"[") .ne. 0)
then 112 this%group(i)%title = trim(line(index(line,
"[")+2:index(line,
"]")-2))
122 if (this%group(1)%title .ne.
"System")
call error_stop_program(
"Index file does not have 'System' group as first group.")
128 allocate(num_array(ncols))
130 do while (io_status .ne. 0)
132 write(ncols_string,
'(i0)') ncols
133 write(fmt_string,
'(a)')
'('//trim(ncols_string)//
'i0)' 134 rewind index_file_unit
135 read(index_file_unit,
'(a)', iostat=io_status) line
136 read(index_file_unit,
'(a)', iostat=io_status) line
137 read(line, *, iostat=io_status) num_array(1:ncols)
144 allocate(indices_tmp(n))
149 this%group(i)%NUMATOMS = (title_loc(i+1)-title_loc(i)-1)*ncols + 1
151 if (n < this%group(i)%NUMATOMS) this%group(i)%NUMATOMS = n + 1
154 do while (io_status .ne. 0)
157 rewind index_file_unit
158 this%group(i)%NUMATOMS = this%group(i)%NUMATOMS - 1
159 if (this%group(i)%NUMATOMS .le. 0)
then 160 this%group(i)%NUMATOMS = 0
165 do j = 1, title_loc(i);
read(index_file_unit,
'(a)', iostat=io_status) line;
end do 168 read(index_file_unit, *, iostat=io_status) indices_tmp(1:this%group(i)%NUMATOMS)
173 allocate(this%group(i)%LOC(1:this%group(i)%NUMATOMS), source=indices_tmp(1:this%group(i)%NUMATOMS))
178 deallocate(indices_tmp)
183 if (this%group(i)%title .eq. this%group(j)%title)
then 185 write(error_unit,
'(a, a, a)') prompt//
"WARNING: Index group ", this%group(i)%title, &
186 " was specified more than once in index file." 196 if (this%group(1)%numatoms .ne. n .or. this%group(1)%loc(this%group(1)%numatoms) .ne. n)
then 197 call error_stop_program(
"Index file does not match dcd file.")
204 if (this%group(i)%numatoms .gt. n)
call error_stop_program(
"Index file does not match dcd file.")
207 do j = 1, this%group(i)%numatoms
208 if (this%group(i)%loc(j) .gt. n)
call error_stop_program(
"Index file does not match dcd file.")
214 close(index_file_unit)
216 write(error_unit,
'(a,i0,a)') prompt//
"Read in ", ngrps,
" index groups."