MODFLOW 6  version 6.9.0.dev0
USGS Modular Hydrologic Model
gridfilereadermodule Module Reference

Data Types

type  gridfilereadertype
 

Functions/Subroutines

subroutine initialize (this, iu)
 @Brief Initialize the grid file reader. More...
 
subroutine finalize (this)
 Finalize the grid file reader. More...
 
subroutine read_header (this)
 Read the file's self-describing header. Internal use only. More...
 
subroutine read_header_meta (this)
 Read self-describing metadata (first four lines). Internal use only. More...
 
subroutine read_header_body (this)
 Read the header body section (text following first. More...
 
integer(i4b) function lookup (this, name, ndim, typ, desc)
 Look up a variable and check its rank and type. More...
 
integer(i4b) function read_int (this, name)
 Read an integer scalar from a grid file. More...
 
real(dp) function read_dbl (this, name)
 Read a double precision scalar from a grid file. More...
 
integer(i4b) function, dimension(:), allocatable read_int_1d (this, name)
 Read a 1D integer array from a grid file. More...
 
subroutine read_int_1d_into (this, name, v)
 Read a 1D integer array into a preallocated array. More...
 
real(dp) function, dimension(:), allocatable read_dbl_1d (this, name)
 Read a 1D double array from a grid file. More...
 
subroutine read_dbl_1d_into (this, name, v)
 Read a 1D double array into a preallocated array. More...
 
character(len=:) function, allocatable read_charstr (this, name)
 Read a character string from a grid file. More...
 
subroutine read_charstr_into (this, name, charstr)
 Read a character string into a preallocated string. More...
 
integer(i4b) function, dimension(:), allocatable read_grid_shape (this)
 Read the grid shape from a grid file. More...
 
logical(lgp) function has_variable (this, name)
 Check whether the grid file contains a variable. More...
 

Variables

integer(i4b), parameter typ_int = 1
 integer variable type More...
 
integer(i4b), parameter typ_dbl = 2
 double precision variable type More...
 
integer(i4b), parameter typ_chr = 3
 character variable type More...
 

Function/Subroutine Documentation

◆ finalize()

subroutine gridfilereadermodule::finalize ( class(gridfilereadertype)  this)

Definition at line 73 of file GridFileReader.f90.

74  class(GridFileReaderType) :: this
75 
76  close (this%inunit)
77  call hash_table_da(this%idx)
78  if (allocated(this%names)) deallocate (this%names)
79  if (allocated(this%ndims)) deallocate (this%ndims)
80  if (allocated(this%typs)) deallocate (this%typs)
81  if (allocated(this%shp_start)) deallocate (this%shp_start)
82  if (allocated(this%pos)) deallocate (this%pos)
83  if (allocated(this%shp)) deallocate (this%shp)
84 
Here is the call graph for this function:

◆ has_variable()

logical(lgp) function gridfilereadermodule::has_variable ( class(gridfilereadertype)  this,
character(len=*), intent(in)  name 
)

Definition at line 435 of file GridFileReader.f90.

436  class(GridFileReaderType) :: this
437  character(len=*), intent(in) :: name
438  logical(LGP) :: has
439 
440  has = this%idx%get(name) /= 0

◆ initialize()

subroutine gridfilereadermodule::initialize ( class(gridfilereadertype)  this,
integer(i4b), intent(in)  iu 
)

Definition at line 61 of file GridFileReader.f90.

62  class(GridFileReaderType) :: this
63  integer(I4B), intent(in) :: iu
64 
65  this%inunit = iu
66  call hash_table_cr(this%idx)
67  allocate (this%shp(0))
68  call this%read_header()
69 
Here is the call graph for this function:

◆ lookup()

integer(i4b) function gridfilereadermodule::lookup ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name,
integer(i4b), intent(in)  ndim,
integer(i4b), intent(in)  typ,
character(len=*), intent(in)  desc 
)

Returns the variable's index. Terminates with an error if the variable does not exist or does not have the expected rank and type. Internal use only.

Parameters
[in]ndimexpected number of dims
[in]typexpected type
[in]descexpected kind, for error messages

Definition at line 230 of file GridFileReader.f90.

231  class(GridFileReaderType), intent(inout) :: this
232  character(len=*), intent(in) :: name
233  integer(I4B), intent(in) :: ndim !< expected number of dims
234  integer(I4B), intent(in) :: typ !< expected type
235  character(len=*), intent(in) :: desc !< expected kind, for error messages
236  integer(I4B) :: ivar
237 
238  ivar = this%idx%get(name)
239  if (ivar == 0) then
240  write (errmsg, '(a)') 'Variable '//trim(name)//' not found'
241  call store_error(errmsg, terminate=.true.)
242  end if
243  if (this%ndims(ivar) /= ndim .or. this%typs(ivar) /= typ) then
244  write (errmsg, '(a)') 'Variable '//trim(name)//' is not '//desc
245  call store_error(errmsg, terminate=.true.)
246  end if
Here is the call graph for this function:

◆ read_charstr()

character(len=:) function, allocatable gridfilereadermodule::read_charstr ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name 
)

Allocates and returns a new character string containing the data.

Definition at line 365 of file GridFileReader.f90.

366  class(GridFileReaderType), intent(inout) :: this
367  character(len=*), intent(in) :: name
368  character(len=:), allocatable :: charstr
369  ! local
370  integer(I4B) :: ivar, nvals
371 
372  ivar = this%lookup(name, 1, typ_chr, 'a character array')
373  nvals = this%shp(this%shp_start(ivar))
374  allocate (character(nvals) :: charstr)
375  read (this%inunit, pos=this%pos(ivar)) charstr
376  rewind(this%inunit)

◆ read_charstr_into()

subroutine gridfilereadermodule::read_charstr_into ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name,
character(len=:), intent(inout), allocatable  charstr 
)

Populates a preallocated character string. If the string is not allocated or is the wrong length, it will be (re)allocated to the correct length.

Definition at line 384 of file GridFileReader.f90.

385  class(GridFileReaderType), intent(inout) :: this
386  character(len=*), intent(in) :: name
387  character(len=:), allocatable, intent(inout) :: charstr
388  ! local
389  integer(I4B) :: ivar, nvals
390 
391  ivar = this%lookup(name, 1, typ_chr, 'a character array')
392  nvals = this%shp(this%shp_start(ivar))
393  if (allocated(charstr)) then
394  if (len(charstr) /= nvals) deallocate (charstr)
395  end if
396  if (.not. allocated(charstr)) allocate (character(nvals) :: charstr)
397  read (this%inunit, pos=this%pos(ivar)) charstr
398  rewind(this%inunit)

◆ read_dbl()

real(dp) function gridfilereadermodule::read_dbl ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name 
)

Definition at line 263 of file GridFileReader.f90.

264  class(GridFileReaderType), intent(inout) :: this
265  character(len=*), intent(in) :: name
266  real(DP) :: v
267  ! local
268  integer(I4B) :: ivar
269 
270  ivar = this%lookup(name, 0, typ_dbl, 'a double precision scalar')
271  read (this%inunit, pos=this%pos(ivar)) v
272  rewind(this%inunit)

◆ read_dbl_1d()

real(dp) function, dimension(:), allocatable gridfilereadermodule::read_dbl_1d ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name 
)

Allocates and returns a new array containing the data.

Definition at line 322 of file GridFileReader.f90.

323  class(GridFileReaderType), intent(inout) :: this
324  character(len=*), intent(in) :: name
325  real(DP), allocatable :: v(:)
326  ! local
327  integer(I4B) :: ivar, nvals
328 
329  ivar = this%lookup(name, 1, typ_dbl, 'a 1D double array')
330  nvals = this%shp(this%shp_start(ivar))
331  allocate (v(nvals))
332  read (this%inunit, pos=this%pos(ivar)) v
333  rewind(this%inunit)

◆ read_dbl_1d_into()

subroutine gridfilereadermodule::read_dbl_1d_into ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name,
real(dp), dimension(:), intent(inout)  v 
)

Populates a preallocated array. Array must already be allocated to the correct size. This version is compatible with both allocatable arrays and memory-manager-allocated pointer targets.

Definition at line 342 of file GridFileReader.f90.

343  class(GridFileReaderType), intent(inout) :: this
344  character(len=*), intent(in) :: name
345  real(DP), dimension(:), intent(inout) :: v
346  ! local
347  integer(I4B) :: ivar, nvals
348 
349  ivar = this%lookup(name, 1, typ_dbl, 'a 1D double array')
350  nvals = this%shp(this%shp_start(ivar))
351  if (size(v) /= nvals) then
352  write (errmsg, '(a,i0,a,i0)') &
353  'Array size mismatch for '//trim(name)//': expected ', &
354  nvals, ', got ', size(v)
355  call store_error(errmsg, terminate=.true.)
356  end if
357  read (this%inunit, pos=this%pos(ivar)) v
358  rewind(this%inunit)
Here is the call graph for this function:

◆ read_grid_shape()

integer(i4b) function, dimension(:), allocatable gridfilereadermodule::read_grid_shape ( class(gridfilereadertype)  this)

Definition at line 402 of file GridFileReader.f90.

403  ! dummy
404  class(GridFileReaderType) :: this
405  integer(I4B), allocatable :: v(:)
406 
407  select case (this%grid_type)
408  case ("DIS")
409  allocate (v(3))
410  v(1) = this%read_int("NLAY")
411  v(2) = this%read_int("NROW")
412  v(3) = this%read_int("NCOL")
413  case ("DISV")
414  allocate (v(2))
415  v(1) = this%read_int("NLAY")
416  v(2) = this%read_int("NCPL")
417  case ("DISU")
418  allocate (v(1))
419  v(1) = this%read_int("NODES")
420  case ("DIS2D")
421  allocate (v(2))
422  v(1) = this%read_int("NROW")
423  v(2) = this%read_int("NCOL")
424  case ("DISV2D")
425  allocate (v(1))
426  v(1) = this%read_int("NODES")
427  case ("DISV1D")
428  allocate (v(1))
429  v(1) = this%read_int("NCELLS")
430  end select
431 

◆ read_header()

subroutine gridfilereadermodule::read_header ( class(gridfilereadertype)  this)

Definition at line 88 of file GridFileReader.f90.

89  class(GridFileReaderType) :: this
90  call this%read_header_meta()
91  call this%read_header_body()

◆ read_header_body()

subroutine gridfilereadermodule::read_header_body ( class(gridfilereadertype)  this)

Definition at line 141 of file GridFileReader.f90.

142  ! dummy
143  class(GridFileReaderType) :: this
144  ! local
145  character(len=:), allocatable :: body
146  character(len=:), allocatable :: line
147  character(len=10) :: name, dtype
148  real(DP) :: rval
149  integer(I4B) :: i, lloc, istart, istop, ival
150  integer(I4B) :: ivar, ndim, dim, ishp, nbytes
151  integer(I8B) :: pos
152 
153  allocate (this%names(this%ntxt))
154  allocate (this%ndims(this%ntxt))
155  allocate (this%typs(this%ntxt))
156  allocate (this%shp_start(this%ntxt))
157  allocate (this%pos(this%ntxt))
158  allocate (character(len=this%lentxt*this%ntxt) :: body)
159  allocate (character(len=this%lentxt) :: line)
160 
161  read (this%inunit) body
162  inquire (this%inunit, pos=pos)
163  do ivar = 1, this%ntxt
164  i = (ivar - 1) * this%lentxt + 1
165  line = body(i:i + this%lentxt - 1)
166 
167  ! name
168  lloc = 1
169  call urword(line, lloc, istart, istop, 1, ival, rval, 0, 0)
170  name = line(istart:istop)
171  this%names(ivar) = name
172  call this%idx%add(name, ivar)
173 
174  ! type
175  call urword(line, lloc, istart, istop, 1, ival, rval, 0, 0)
176  dtype = line(istart:istop)
177  select case (dtype)
178  case ("INTEGER")
179  this%typs(ivar) = typ_int
180  nbytes = 4
181  case ("DOUBLE")
182  this%typs(ivar) = typ_dbl
183  nbytes = 8
184  case ("CHARACTER")
185  this%typs(ivar) = typ_chr
186  nbytes = 1
187  case default
188  this%typs(ivar) = 0
189  nbytes = 0
190  end select
191 
192  ! dims
193  call urword(line, lloc, istart, istop, 0, ival, rval, 0, 0)
194  call urword(line, lloc, istart, istop, 2, ival, rval, 0, 0)
195  ndim = ival
196  this%ndims(ivar) = ndim
197 
198  ! shape
199  this%shp_start(ivar) = 0
200  if (ndim > 0) then
201  ishp = size(this%shp)
202  call expandarray(this%shp, increment=ndim)
203  do dim = 1, ndim
204  call urword(line, lloc, istart, istop, 2, ival, rval, 0, 0)
205  this%shp(ishp + dim) = ival
206  end do
207  this%shp_start(ivar) = ishp + 1
208  end if
209 
210  ! position
211  this%pos(ivar) = pos
212  if (ndim == 0) then
213  pos = pos + nbytes
214  else
215  ishp = this%shp_start(ivar)
216  pos = pos + product(int(this%shp(ishp:ishp + ndim - 1), i8b)) * nbytes
217  end if
218  end do
219 
220  rewind(this%inunit)
221 
Here is the call graph for this function:

◆ read_header_meta()

subroutine gridfilereadermodule::read_header_meta ( class(gridfilereadertype)  this)

Definition at line 95 of file GridFileReader.f90.

96  ! dummy
97  class(GridFileReaderType) :: this
98  ! local
99  character(len=50) :: line
100  integer(I4B) :: lloc, istart, istop
101  integer(I4B) :: ival
102  real(DP) :: rval
103 
104  ! grid type
105  read (this%inunit) line
106  lloc = 1
107  call urword(line, lloc, istart, istop, 1, ival, rval, 0, 0)
108  if (line(istart:istop) /= 'GRID') then
109  call store_error('Binary grid file must begin with "GRID". '//&
110  &'Found: '//line(istart:istop))
111  call store_error_unit(this%inunit)
112  end if
113  call urword(line, lloc, istart, istop, 1, ival, rval, 0, 0)
114  this%grid_type = line(istart:istop)
115 
116  ! version
117  read (this%inunit) line
118  lloc = 1
119  call urword(line, lloc, istart, istop, 0, ival, rval, 0, 0)
120  call urword(line, lloc, istart, istop, 2, ival, rval, 0, 0)
121  this%version = ival
122 
123  ! ntxt
124  read (this%inunit) line
125  lloc = 1
126  call urword(line, lloc, istart, istop, 0, ival, rval, 0, 0)
127  call urword(line, lloc, istart, istop, 2, ival, rval, 0, 0)
128  this%ntxt = ival
129 
130  ! lentxt
131  read (this%inunit) line
132  lloc = 1
133  call urword(line, lloc, istart, istop, 0, ival, rval, 0, 0)
134  call urword(line, lloc, istart, istop, 2, ival, rval, 0, 0)
135  this%lentxt = ival
136 
Here is the call graph for this function:

◆ read_int()

integer(i4b) function gridfilereadermodule::read_int ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name 
)

Definition at line 250 of file GridFileReader.f90.

251  class(GridFileReaderType), intent(inout) :: this
252  character(len=*), intent(in) :: name
253  integer(I4B) :: v
254  ! local
255  integer(I4B) :: ivar
256 
257  ivar = this%lookup(name, 0, typ_int, 'an integer scalar')
258  read (this%inunit, pos=this%pos(ivar)) v
259  rewind(this%inunit)

◆ read_int_1d()

integer(i4b) function, dimension(:), allocatable gridfilereadermodule::read_int_1d ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name 
)

Allocates and returns a new array containing the data.

Definition at line 279 of file GridFileReader.f90.

280  class(GridFileReaderType), intent(inout) :: this
281  character(len=*), intent(in) :: name
282  integer(I4B), allocatable :: v(:)
283  ! local
284  integer(I4B) :: ivar, nvals
285 
286  ivar = this%lookup(name, 1, typ_int, 'a 1D integer array')
287  nvals = this%shp(this%shp_start(ivar))
288  allocate (v(nvals))
289  read (this%inunit, pos=this%pos(ivar)) v
290  rewind(this%inunit)

◆ read_int_1d_into()

subroutine gridfilereadermodule::read_int_1d_into ( class(gridfilereadertype), intent(inout)  this,
character(len=*), intent(in)  name,
integer(i4b), dimension(:), intent(inout)  v 
)

Populates a preallocated array. Array must already be allocated to the correct size. This version is compatible with both allocatable arrays and memory-manager-allocated pointer targets.

Definition at line 299 of file GridFileReader.f90.

300  class(GridFileReaderType), intent(inout) :: this
301  character(len=*), intent(in) :: name
302  integer(I4B), dimension(:), intent(inout) :: v
303  ! local
304  integer(I4B) :: ivar, nvals
305 
306  ivar = this%lookup(name, 1, typ_int, 'a 1D integer array')
307  nvals = this%shp(this%shp_start(ivar))
308  if (size(v) /= nvals) then
309  write (errmsg, '(a,i0,a,i0)') &
310  'Array size mismatch for '//trim(name)//': expected ', &
311  nvals, ', got ', size(v)
312  call store_error(errmsg, terminate=.true.)
313  end if
314  read (this%inunit, pos=this%pos(ivar)) v
315  rewind(this%inunit)
Here is the call graph for this function:

Variable Documentation

◆ typ_chr

integer(i4b), parameter gridfilereadermodule::typ_chr = 3

Definition at line 17 of file GridFileReader.f90.

17  integer(I4B), parameter :: TYP_CHR = 3 !< character variable type

◆ typ_dbl

integer(i4b), parameter gridfilereadermodule::typ_dbl = 2

Definition at line 16 of file GridFileReader.f90.

16  integer(I4B), parameter :: TYP_DBL = 2 !< double precision variable type

◆ typ_int

integer(i4b), parameter gridfilereadermodule::typ_int = 1

Definition at line 15 of file GridFileReader.f90.

15  integer(I4B), parameter :: TYP_INT = 1 !< integer variable type