MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
ArrayReaderBase.f90
Go to the documentation of this file.
2 
3  use kindmodule, only: dp, i4b, lgp
4  use constantsmodule, only: maxcharlen
6  use simvariablesmodule, only: errmsg
7  use simmodule, only: store_error
8  use inputoutputmodule, only: openfile
9 
10  implicit none
11  private
12  public :: arrayreaderbasetype
13 
15 
16  type(blockparsertype), pointer :: parser => null()
17  integer(I4B) :: iout = 0
18  integer(I4B) :: input_unit = 0
19  character(len=:), allocatable :: array_name
20  character(len=:), allocatable :: filename
21  integer(I4B) :: iprn = 0
22  logical(LGP) :: isconstant = .false.
23  logical(LGP) :: isinternal = .false.
24  logical(LGP) :: isopenclose = .false.
25  logical(LGP) :: isbinary = .false.
26 
27  contains
28 
29  procedure :: read_array
30  procedure :: reset_reader
31  procedure :: read_control_record
32  procedure :: set_constant ! must be overridden
33  procedure :: fill_constant ! must be overridden
34  procedure :: fill_internal
35  procedure :: fill_open_close
36  procedure :: read_ascii ! must be overridden
37  procedure :: read_binary ! must be overridden
38  procedure :: set_factor ! must be overridden
39  procedure :: apply_factor ! must be overridden
40  procedure :: open_file
41 
42  end type arrayreaderbasetype
43 
44 contains
45 
46  subroutine read_array(this)
47  class(arrayreaderbasetype) :: this
48 
49  ! read control record
50  call this%read_control_record()
51 
52  ! fill array
53  if (this%isConstant) then
54  call this%fill_constant()
55  else if (this%isInternal) then
56  call this%fill_internal()
57  else if (this%isOpenClose) then
58  call this%fill_open_close()
59  end if
60 
61  end subroutine read_array
62 
63  subroutine reset_reader(this)
64  class(arrayreaderbasetype) :: this
65  this%iprn = 0
66  this%isConstant = .false.
67  this%isInternal = .false.
68  this%isOpenClose = .false.
69  this%isBinary = .false.
70  end subroutine reset_reader
71 
72  subroutine read_control_record(this)
73  class(arrayreaderbasetype) :: this
74  logical(LGP) :: endOfBlock
75  character(len=100) :: keyword
76  character(len=MAXCHARLEN) :: string
77 
78  ! read the array input style
79  call this%parser%GetNextLine(endofblock)
80  call this%parser%GetStringCaps(keyword)
81 
82  ! load array based on the different styles
83  select case (keyword)
84  case ('CONSTANT')
85  this%isConstant = .true.
86  call this%set_constant()
87  case ('INTERNAL')
88  this%isInternal = .true.
89  case ('OPEN/CLOSE')
90  this%isOpenClose = .true.
91  call this%parser%GetString(string)
92  this%filename = trim(string)
93  case default
94  write (errmsg, *) 'Error reading control record for '// &
95  trim(adjustl(this%array_name))//'. &
96  & Use CONSTANT, INTERNAL, or OPEN/CLOSE.'
97  call store_error(errmsg)
98  call this%parser%StoreErrorUnit()
99  end select
100 
101  ! if INTERNAL or OPEN/CLOSE then look for FACTOR and IPRN
102  if (this%isInternal .or. this%isOpenClose) then
103  do
104  call this%parser%GetStringCaps(keyword)
105  if (keyword == '') exit
106  select case (keyword)
107  case ('FACTOR')
108  call this%set_factor()
109  case ('IPRN')
110  this%iprn = this%parser%GetInteger()
111  case ('(BINARY)')
112  this%isBinary = .true.
113  end select
114  end do
115  end if
116 
117  end subroutine read_control_record
118 
119  subroutine set_constant(this)
120  class(arrayreaderbasetype) :: this
121  errmsg = 'Programming error in ArrayReader'
122  call store_error(errmsg, terminate=.true.)
123  end subroutine set_constant
124 
125  subroutine fill_constant(this)
126  class(arrayreaderbasetype) :: this
127  errmsg = 'Programming error in ArrayReader'
128  call store_error(errmsg, terminate=.true.)
129  end subroutine fill_constant
130 
131  subroutine fill_internal(this)
132  class(arrayreaderbasetype) :: this
133  this%input_unit = this%parser%iuactive
134  call this%read_ascii()
135  call this%apply_factor()
136  end subroutine fill_internal
137 
138  subroutine fill_open_close(this)
139  class(arrayreaderbasetype) :: this
140  this%input_unit = 0
141  call this%open_file()
142  if (this%isBinary) then
143  call this%read_binary()
144  else
145  call this%read_ascii()
146  end if
147  close (this%input_unit)
148  call this%apply_factor()
149  end subroutine fill_open_close
150 
151  subroutine read_ascii(this)
152  class(arrayreaderbasetype) :: this
153  errmsg = 'Programming error in ArrayReader'
154  call store_error(errmsg, terminate=.true.)
155  end subroutine read_ascii
156 
157  subroutine read_binary(this)
158  class(arrayreaderbasetype) :: this
159  errmsg = 'Programming error in ArrayReader'
160  call store_error(errmsg, terminate=.true.)
161  end subroutine read_binary
162 
163  subroutine set_factor(this)
164  class(arrayreaderbasetype) :: this
165  errmsg = 'Programming error in ArrayReader'
166  call store_error(errmsg, terminate=.true.)
167  end subroutine set_factor
168 
169  subroutine apply_factor(this)
170  class(arrayreaderbasetype) :: this
171  errmsg = 'Programming error in ArrayReader'
172  call store_error(errmsg, terminate=.true.)
173  end subroutine apply_factor
174 
175  subroutine open_file(this)
176  use openspecmodule, only: form, access
177  class(arrayreaderbasetype) :: this
178  if (this%isBinary) then
179  call openfile(this%input_unit, this%iout, this%filename, &
180  'OPEN/CLOSE', fmtarg_opt=form, accarg_opt=access)
181  else
182  call openfile(this%input_unit, this%iout, this%filename, 'OPEN/CLOSE')
183  end if
184  end subroutine open_file
185 
186 end module arrayreaderbasemodule
subroutine set_factor(this)
subroutine fill_constant(this)
subroutine read_array(this)
subroutine reset_reader(this)
subroutine read_control_record(this)
subroutine set_constant(this)
subroutine fill_open_close(this)
subroutine apply_factor(this)
subroutine open_file(this)
subroutine fill_internal(this)
subroutine read_binary(this)
subroutine read_ascii(this)
This module contains block parser methods.
Definition: BlockParser.f90:7
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter maxcharlen
maximum length of char string
Definition: Constants.f90:47
subroutine, public openfile(iu, iout, fname, ftype, fmtarg_opt, accarg_opt, filstat_opt, mode_opt)
Open a file.
Definition: InputOutput.f90:30
This module defines variable data types.
Definition: kind.f90:8
character(len=20) access
Definition: OpenSpec.f90:7
character(len=20) form
Definition: OpenSpec.f90:7
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition: Sim.f90:92
This module contains simulation variables.
Definition: SimVariables.f90:9
character(len=maxcharlen) errmsg
error message string