MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
HeadFileReader.f90
Go to the documentation of this file.
2 
3  use kindmodule
4  use constantsmodule, only: linelength
5 
6  implicit none
7 
8  private
9  public :: headfilereadertype
10 
12 
13  integer(I4B) :: inunit
14  character(len=16) :: text
15  integer(I4B) :: nlay
16  integer(I4B) :: kstp
17  integer(I4B) :: kper
18  integer(I4B) :: kstpnext
19  integer(I4B) :: kpernext
20  logical :: endoffile
21  real(dp) :: delt
22  real(dp) :: pertim
23  real(dp) :: totim
24  real(dp), dimension(:), allocatable :: head
25 
26  contains
27 
28  procedure :: initialize
29  procedure :: read_record
30  procedure :: finalize
31 
32  end type headfilereadertype
33 
34 contains
35 
36  !< @brief initialize
37  !<
38  subroutine initialize(this, iu, iout)
39  ! -- dummy
40  class(headfilereadertype) :: this
41  integer(I4B), intent(in) :: iu
42  integer(I4B), intent(in) :: iout
43  ! -- local
44  integer(I4B) :: kstp_last, kper_last
45  logical :: success
46  this%inunit = iu
47  this%endoffile = .false.
48  this%nlay = 0
49  !
50  ! -- Read the first head data record to set kstp_last, kstp_last
51  call this%read_record(success)
52  kstp_last = this%kstp
53  kper_last = this%kper
54  rewind(this%inunit)
55  !
56  ! -- Determine number of records within a time step
57  if (iout > 0) &
58  write (iout, '(a)') &
59  'Reading binary file to determine number of records per time step.'
60  do
61  call this%read_record(success, iout)
62  if (.not. success) exit
63  if (kstp_last /= this%kstp .or. kper_last /= this%kper) exit
64  this%nlay = this%nlay + 1
65  end do
66  rewind(this%inunit)
67  if (iout > 0) &
68  write (iout, '(a, i0, a)') 'Detected ', this%nlay, &
69  ' unique records in binary file.'
70  end subroutine initialize
71 
72  !< @brief read record
73  !<
74  subroutine read_record(this, success, iout_opt)
75  ! -- modules
77  ! -- dummy
78  class(headfilereadertype) :: this
79  logical, intent(out) :: success
80  integer(I4B), intent(in), optional :: iout_opt
81  ! -- local
82  integer(I4B) :: iostat, iout
83  integer(I4B) :: ncol, nrow, ilay
84  !
85  if (present(iout_opt)) then
86  iout = iout_opt
87  else
88  iout = 0
89  end if
90  !
91  this%kstp = 0
92  this%kper = 0
93  success = .true.
94  this%kstpnext = 0
95  this%kpernext = 0
96  read (this%inunit, iostat=iostat) this%kstp, this%kper, this%pertim, &
97  this%totim, this%text, ncol, nrow, ilay
98  if (iostat /= 0) then
99  success = .false.
100  if (iostat < 0) this%endoffile = .true.
101  return
102  end if
103  !
104  ! -- allocate head to proper size
105  if (.not. allocated(this%head)) then
106  allocate (this%head(ncol * nrow))
107  else
108  if (size(this%head) /= ncol * nrow) then
109  deallocate (this%head)
110  allocate (this%head(ncol * nrow))
111  end if
112  end if
113  !
114  ! -- read the head array
115  read (this%inunit) this%head
116  !
117  ! -- look ahead to next kstp and kper, then backup if read successfully
118  if (.not. this%endoffile) then
119  read (this%inunit, iostat=iostat) this%kstpnext, this%kpernext
120  if (iostat == 0) then
121  call fseek_stream(this%inunit, -2 * i4b, 1, iostat)
122  else if (iostat < 0) then
123  this%endoffile = .true.
124  end if
125  end if
126  end subroutine read_record
127 
128  !< @brief finalize
129  !<
130  subroutine finalize(this)
131  class(headfilereadertype) :: this
132  close (this%inunit)
133  if (allocated(this%head)) deallocate (this%head)
134  end subroutine finalize
135 
136 end module headfilereadermodule
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition: Constants.f90:45
subroutine finalize(this)
subroutine read_record(this, success, iout_opt)
subroutine initialize(this, iu, iout)
subroutine, public fseek_stream(iu, offset, whence, status)
Move the file pointer.
This module defines variable data types.
Definition: kind.f90:8