MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
NCFileVars.f90
Go to the documentation of this file.
1 !> @brief This module contains the NCFileVarsModule
2 !!
3 !! These data structures organize package input information
4 !! associated with a single model netcdf input file.
5 !!
6 !<
8 
9  use kindmodule, only: dp, i4b, lgp
11  use simvariablesmodule, only: errmsg
13  use listmodule, only: listtype
14 
15  implicit none
16  private
17  public :: ncfilevarstype
18  public :: ncpackagevarstype
19 
20  !> @brief Type describing input variables for a package in NetCDF file
21  !<
23  character(len=LENMODELNAME) :: modelname !< name of model
24  type(listtype) :: nc_vars
25  character(len=LINELENGTH), pointer :: grid => null() !< grid type
26  character(len=LINELENGTH), pointer :: nc_fname => null() !< netcdf filename
27  integer(I4B), pointer :: ncid => null() !< netcdf file handle
28  contains
29  procedure :: init => ncvars_init
30  procedure :: varid => ncvars_varid
31  procedure :: destroy => ncvars_destroy
32  end type ncpackagevarstype
33 
34  !> @brief Type which describes a modflow input variable in a netcdf file
35  !<
37  character(LINELENGTH) :: pkgname !< package name
38  character(LINELENGTH) :: tagname !< tag name
39  integer(I4B) :: layer !< variable layer
40  integer(I4B) :: period !< variable period
41  integer(I4B) :: iaux !< variable aux index
42  integer(I4B) :: varid !< NC file variable id
43  contains
44  end type ncfilemf6vartype
45 
46  !> @brief Type describing modflow6 input variables in model NetCDF file
47  !<
49  type(listtype) :: mf6invar !< list of modflow 6 input variables in netcdf file
50  character(len=LINELENGTH), pointer :: grid => null() !< grid type
51  character(len=LINELENGTH), pointer :: nc_fname => null() !< netcdf filename
52  integer(I4B), pointer :: ncid => null() !< netcdf file handle
53  contains
54  procedure :: init => fv_init
55  procedure :: add => fv_add
56  procedure :: destroy => fv_destroy
57  procedure :: create_varlists
58  end type ncfilevarstype
59 
60 contains
61 
62  !> @brief create netcdf package variable lists
63  !<
64  subroutine ncvars_init(this, modelname)
65  ! -- modules
66  ! -- dummy
67  class(ncpackagevarstype) :: this
68  character(len=*), intent(in) :: modelname
69  ! -- local
70  !
71  ! -- set modelname
72  this%modelname = modelname
73  !
74  ! -- return
75  return
76  end subroutine ncvars_init
77 
78  !> @brief return a netcdf variable id for a package tagname
79  !<
80  function ncvars_varid(this, tagname, layer, period, iaux) result(varid)
81  ! -- modules
82  ! -- dummy
83  class(ncpackagevarstype) :: this
84  character(len=*), intent(in) :: tagname
85  integer(I4B), optional :: layer
86  integer(I4B), optional :: period
87  integer(I4B), optional :: iaux
88  ! -- return
89  integer(I4B) :: varid
90  ! -- local
91  integer(I4B) :: n, l, p, a
92  class(ncfilemf6vartype), pointer :: nc_var
93  !
94  ! -- initialize
95  varid = -1
96  l = -1
97  p = -1
98  a = -1
99  !
100  ! -- set search layer if provided
101  if (present(layer)) then
102  l = layer
103  end if
104  !
105  ! -- set search period if provided
106  if (present(period)) then
107  p = period
108  end if
109  ! -- set search iaux if provided
110  if (present(iaux)) then
111  a = iaux
112  end if
113  !
114  do n = 1, this%nc_vars%Count()
115  nc_var => ncvar_get(this%nc_vars, n)
116  if (nc_var%tagname == tagname .and. &
117  nc_var%layer == l .and. &
118  nc_var%period == p .and. &
119  nc_var%iaux == a) then
120  varid = nc_var%varid
121  end if
122  end do
123  !
124  ! -- set error and exit if variable not in NetCDF input
125  if (varid == -1) then
126  if (this%nc_fname /= '') then
127  write (errmsg, '(a)') &
128  'NetCDF variable not found, tagname="'//trim(tagname)//'"'
129  if (present(layer)) then
130  write (errmsg, '(a,i0)') trim(errmsg)//', ilayer=', layer
131  end if
132  if (present(period)) then
133  write (errmsg, '(a,i0)') trim(errmsg)//', period=', period
134  end if
135  if (present(iaux)) then
136  write (errmsg, '(a,i0)') trim(errmsg)//', iaux=', iaux
137  end if
138  write (errmsg, '(a)') trim(errmsg)//'.'
139  call store_error(errmsg)
140  call store_error_filename(this%nc_fname)
141  else
142  write (errmsg, '(a)') &
143  'NetCDF variable not found, tagname="'//trim(tagname)// &
144  '". NetCDF input not provided for model "'//trim(this%modelname)//'".'
145  call store_error(errmsg, .true.)
146  end if
147  end if
148  !
149  ! -- return
150  return
151  end function ncvars_varid
152 
153  !> @brief destroy netcdf package variable lists
154  !<
155  subroutine ncvars_destroy(this)
156  ! -- modules
157  ! -- dummy
158  class(ncpackagevarstype) :: this
159  ! -- local
160  class(ncfilemf6vartype), pointer :: nc_var
161  integer(I4B) :: n
162  !
163  ! -- deallocate allocated memory
164  do n = 1, this%nc_vars%Count()
165  nc_var => ncvar_get(this%nc_vars, n)
166  deallocate (nc_var)
167  nullify (nc_var)
168  end do
169  !
170  call this%nc_vars%Clear()
171  !
172  ! -- return
173  return
174  end subroutine ncvars_destroy
175 
176  !> @brief initialize netcdf model variable description type
177  !<
178  subroutine fv_init(this, modelname, nc_fname, ncid, grid)
179  ! -- modules
180  use constantsmodule, only: lenmempath
184  ! -- dummy variables
185  class(ncfilevarstype) :: this
186  character(len=*), intent(in) :: modelname
187  character(len=*), intent(in) :: nc_fname
188  integer(I4B), intent(in) :: ncid
189  character(len=*), intent(in) :: grid
190  ! -- local variables
191  character(len=LENMEMPATH) :: mempath
192  integer(I4B) :: ilen
193  !
194  ! -- set mempath
195  mempath = create_mem_path(component=modelname, &
196  context=idm_context)
197  ! -- initialize strlen
198  ilen = linelength
199  !
200  ! -- allocate managed memory
201  call mem_allocate(this%grid, ilen, 'NETCDF_GRID', mempath)
202  call mem_allocate(this%nc_fname, ilen, 'NETCDF_FNAME', mempath)
203  call mem_allocate(this%ncid, 'NCID', mempath)
204  !
205  ! -- set
206  this%grid = grid
207  this%nc_fname = nc_fname
208  this%ncid = ncid
209  !
210  ! -- return
211  return
212  end subroutine fv_init
213 
214  !> @brief add netcdf modflow6 input variable to list
215  !<
216  subroutine fv_add(this, pkgname, tagname, layer, period, iaux, varid)
217  ! -- modules
219  ! -- dummy variables
220  class(ncfilevarstype) :: this
221  character(len=*), intent(in) :: pkgname
222  character(len=*), intent(in) :: tagname
223  integer(I4B), intent(in) :: layer
224  integer(I4B), intent(in) :: period
225  integer(I4B), intent(in) :: iaux
226  integer(I4B), intent(in) :: varid
227  ! -- local variables
228  class(ncfilemf6vartype), pointer :: invar
229  class(*), pointer :: obj
230  !
231  ! -- add mf6 variable to file list
232  allocate (invar)
233  invar%pkgname = pkgname
234  invar%tagname = tagname
235  invar%layer = layer
236  invar%period = period
237  invar%iaux = iaux
238  invar%varid = varid
239  !
240  obj => invar
241  call this%mf6invar%Add(obj)
242  !
243  ! -- return
244  return
245  end subroutine fv_add
246 
247  !> @brief destroy netcdf model variable description type
248  !<
249  subroutine fv_destroy(this)
250  ! -- modules
251  ! -- dummy
252  class(ncfilevarstype) :: this
253  class(ncfilemf6vartype), pointer :: invar
254  integer(I4B) :: n
255  !
256  do n = 1, this%mf6invar%Count()
257  invar => ncvar_get(this%mf6invar, n)
258  deallocate (invar)
259  nullify (invar)
260  end do
261  !
262  call this%mf6invar%Clear()
263  !
264  return
265  end subroutine fv_destroy
266 
267  !> @brief create list of variables that correspond to a package
268  !<
269  subroutine create_varlists(this, modelname, pkgname, nc_vars)
270  ! -- modules
271  ! -- dummy
272  class(ncfilevarstype) :: this
273  character(len=*), intent(in) :: modelname
274  character(len=*), intent(in) :: pkgname
275  type(ncpackagevarstype), pointer, intent(inout) :: nc_vars
276  integer(I4B) :: n
277  ! -- local
278  class(ncfilemf6vartype), pointer :: invar, nc_var
279  class(*), pointer :: obj
280  !
281  do n = 1, this%mf6invar%count()
282  invar => ncvar_get(this%mf6invar, n)
283  if (invar%pkgname == pkgname) then
284  ! -- create package variable description
285  allocate (nc_var)
286  nc_var%pkgname = invar%pkgname
287  nc_var%tagname = invar%tagname
288  nc_var%layer = invar%layer
289  nc_var%period = invar%period
290  nc_var%iaux = invar%iaux
291  nc_var%varid = invar%varid
292  obj => nc_var
293  call nc_vars%nc_vars%Add(obj)
294  end if
295  end do
296  !
297  ! -- set modelname
298  nc_vars%modelname = modelname
299  !
300  ! -- set file attribute pointers
301  nc_vars%ncid => this%ncid
302  nc_vars%nc_fname => this%nc_fname
303  nc_vars%grid => this%grid
304  !
305  ! -- return
306  return
307  end subroutine create_varlists
308 
309  !> @brief get modflow6 input variable description at position idx
310  !<
311  function ncvar_get(nc_vars, idx) result(res)
312  ! -- dummy variables
313  type(listtype) :: nc_vars
314  integer(I4B), intent(in) :: idx !< package number
315  class(ncfilemf6vartype), pointer :: res
316  ! -- local variables
317  class(*), pointer :: obj
318  !
319  ! -- initialize res
320  res => null()
321  !
322  ! -- get the package from the list
323  obj => nc_vars%GetItem(idx)
324  if (associated(obj)) then
325  select type (obj)
326  class is (ncfilemf6vartype)
327  res => obj
328  end select
329  end if
330  !
331  ! -- return
332  return
333  end function ncvar_get
334 
335 end module ncfilevarsmodule
subroutine init()
Definition: GridSorting.f90:24
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition: Constants.f90:45
integer(i4b), parameter lenmodelname
maximum length of the model name
Definition: Constants.f90:22
integer(i4b), parameter lenmempath
maximum length of the memory path
Definition: Constants.f90:27
This module defines variable data types.
Definition: kind.f90:8
character(len=lenmempath) function create_mem_path(component, subcomponent, context)
returns the path to the memory object
This module contains the NCFileVarsModule.
Definition: NCFileVars.f90:7
subroutine create_varlists(this, modelname, pkgname, nc_vars)
create list of variables that correspond to a package
Definition: NCFileVars.f90:270
subroutine fv_add(this, pkgname, tagname, layer, period, iaux, varid)
add netcdf modflow6 input variable to list
Definition: NCFileVars.f90:217
subroutine fv_init(this, modelname, nc_fname, ncid, grid)
initialize netcdf model variable description type
Definition: NCFileVars.f90:179
class(ncfilemf6vartype) function, pointer ncvar_get(nc_vars, idx)
get modflow6 input variable description at position idx
Definition: NCFileVars.f90:312
subroutine fv_destroy(this)
destroy netcdf model variable description type
Definition: NCFileVars.f90:250
integer(i4b) function ncvars_varid(this, tagname, layer, period, iaux)
return a netcdf variable id for a package tagname
Definition: NCFileVars.f90:81
subroutine ncvars_destroy(this)
destroy netcdf package variable lists
Definition: NCFileVars.f90:156
subroutine ncvars_init(this, modelname)
create netcdf package variable lists
Definition: NCFileVars.f90:65
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition: Sim.f90:92
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition: Sim.f90:203
This module contains simulation variables.
Definition: SimVariables.f90:9
character(len=maxcharlen) errmsg
error message string
character(len=linelength) idm_context
A generic heterogeneous doubly-linked list.
Definition: List.f90:14
Type which describes a modflow input variable in a netcdf file.
Definition: NCFileVars.f90:36
Type describing modflow6 input variables in model NetCDF file.
Definition: NCFileVars.f90:48
Type describing input variables for a package in NetCDF file.
Definition: NCFileVars.f90:22