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  class(ncpackagevarstype) :: this
66  character(len=*), intent(in) :: modelname
67  ! set modelname
68  this%modelname = modelname
69  end subroutine ncvars_init
70 
71  !> @brief return a netcdf variable id for a package tagname
72  !<
73  function ncvars_varid(this, tagname, layer, period, iaux) result(varid)
74  class(ncpackagevarstype) :: this
75  character(len=*), intent(in) :: tagname
76  integer(I4B), optional :: layer
77  integer(I4B), optional :: period
78  integer(I4B), optional :: iaux
79  integer(I4B) :: varid
80  integer(I4B) :: n, l, p, a
81  class(ncfilemf6vartype), pointer :: nc_var
82 
83  ! initialize
84  varid = -1
85  l = -1
86  p = -1
87  a = -1
88 
89  ! set search layer if provided
90  if (present(layer)) then
91  l = layer
92  end if
93 
94  ! set search period if provided
95  if (present(period)) then
96  p = period
97  end if
98  ! set search iaux if provided
99  if (present(iaux)) then
100  a = iaux
101  end if
102 
103  do n = 1, this%nc_vars%Count()
104  nc_var => ncvar_get(this%nc_vars, n)
105  if (nc_var%tagname == tagname .and. &
106  nc_var%layer == l .and. &
107  nc_var%period == p .and. &
108  nc_var%iaux == a) then
109  varid = nc_var%varid
110  end if
111  end do
112 
113  ! set error and exit if variable not in NetCDF input
114  if (varid == -1) then
115  if (this%nc_fname /= '') then
116  write (errmsg, '(a)') &
117  'NetCDF variable not found, tagname="'//trim(tagname)//'"'
118  if (present(layer)) then
119  write (errmsg, '(a,i0)') trim(errmsg)//', ilayer=', layer
120  end if
121  if (present(period)) then
122  write (errmsg, '(a,i0)') trim(errmsg)//', period=', period
123  end if
124  if (present(iaux)) then
125  write (errmsg, '(a,i0)') trim(errmsg)//', iaux=', iaux
126  end if
127  write (errmsg, '(a)') trim(errmsg)//'.'
128  call store_error(errmsg)
129  call store_error_filename(this%nc_fname)
130  else
131  write (errmsg, '(a)') &
132  'NetCDF variable not found, tagname="'//trim(tagname)// &
133  '". NetCDF input not provided for model "'//trim(this%modelname)//'".'
134  call store_error(errmsg, .true.)
135  end if
136  end if
137  end function ncvars_varid
138 
139  !> @brief destroy netcdf package variable lists
140  !<
141  subroutine ncvars_destroy(this)
142  class(ncpackagevarstype) :: this
143  class(ncfilemf6vartype), pointer :: nc_var
144  integer(I4B) :: n
145  ! deallocate allocated memory
146  do n = 1, this%nc_vars%Count()
147  nc_var => ncvar_get(this%nc_vars, n)
148  deallocate (nc_var)
149  nullify (nc_var)
150  end do
151  call this%nc_vars%Clear()
152  end subroutine ncvars_destroy
153 
154  !> @brief initialize netcdf model variable description type
155  !<
156  subroutine fv_init(this, modelname, nc_fname, ncid, grid)
157  use constantsmodule, only: lenmempath
161  class(ncfilevarstype) :: this
162  character(len=*), intent(in) :: modelname
163  character(len=*), intent(in) :: nc_fname
164  integer(I4B), intent(in) :: ncid
165  character(len=*), intent(in) :: grid
166  character(len=LENMEMPATH) :: mempath
167  integer(I4B) :: ilen
168 
169  ! set mempath
170  mempath = create_mem_path(component=modelname, &
171  context=idm_context)
172  ! initialize strlen
173  ilen = linelength
174 
175  ! allocate managed memory
176  call mem_allocate(this%grid, ilen, 'NETCDF_GRID', mempath)
177  call mem_allocate(this%nc_fname, ilen, 'NETCDF_FNAME', mempath)
178  call mem_allocate(this%ncid, 'NCID', mempath)
179 
180  ! set
181  this%grid = grid
182  this%nc_fname = nc_fname
183  this%ncid = ncid
184  end subroutine fv_init
185 
186  !> @brief add netcdf modflow6 input variable to list
187  !<
188  subroutine fv_add(this, pkgname, tagname, layer, period, iaux, varid)
190  class(ncfilevarstype) :: this
191  character(len=*), intent(in) :: pkgname
192  character(len=*), intent(in) :: tagname
193  integer(I4B), intent(in) :: layer
194  integer(I4B), intent(in) :: period
195  integer(I4B), intent(in) :: iaux
196  integer(I4B), intent(in) :: varid
197  class(ncfilemf6vartype), pointer :: invar
198  class(*), pointer :: obj
199  ! add mf6 variable to file list
200  allocate (invar)
201  invar%pkgname = pkgname
202  invar%tagname = tagname
203  invar%layer = layer
204  invar%period = period
205  invar%iaux = iaux
206  invar%varid = varid
207  obj => invar
208  call this%mf6invar%Add(obj)
209  end subroutine fv_add
210 
211  !> @brief destroy netcdf model variable description type
212  !<
213  subroutine fv_destroy(this)
214  class(ncfilevarstype) :: this
215  class(ncfilemf6vartype), pointer :: invar
216  integer(I4B) :: n
217  do n = 1, this%mf6invar%Count()
218  invar => ncvar_get(this%mf6invar, n)
219  deallocate (invar)
220  nullify (invar)
221  end do
222  call this%mf6invar%Clear()
223  end subroutine fv_destroy
224 
225  !> @brief create list of variables that correspond to a package
226  !<
227  subroutine create_varlists(this, modelname, pkgname, nc_vars)
228  class(ncfilevarstype) :: this
229  character(len=*), intent(in) :: modelname
230  character(len=*), intent(in) :: pkgname
231  type(ncpackagevarstype), pointer, intent(inout) :: nc_vars
232  integer(I4B) :: n
233  class(ncfilemf6vartype), pointer :: invar, nc_var
234  class(*), pointer :: obj
235 
236  do n = 1, this%mf6invar%count()
237  invar => ncvar_get(this%mf6invar, n)
238  if (invar%pkgname == pkgname) then
239  ! create package variable description
240  allocate (nc_var)
241  nc_var%pkgname = invar%pkgname
242  nc_var%tagname = invar%tagname
243  nc_var%layer = invar%layer
244  nc_var%period = invar%period
245  nc_var%iaux = invar%iaux
246  nc_var%varid = invar%varid
247  obj => nc_var
248  call nc_vars%nc_vars%Add(obj)
249  end if
250  end do
251 
252  ! set modelname
253  nc_vars%modelname = modelname
254 
255  ! set file attribute pointers
256  nc_vars%ncid => this%ncid
257  nc_vars%nc_fname => this%nc_fname
258  nc_vars%grid => this%grid
259  end subroutine create_varlists
260 
261  !> @brief get modflow6 input variable description at position idx
262  !<
263  function ncvar_get(nc_vars, idx) result(res)
264  type(listtype) :: nc_vars
265  integer(I4B), intent(in) :: idx !< package number
266  class(ncfilemf6vartype), pointer :: res
267  class(*), pointer :: obj
268 
269  ! initialize res
270  res => null()
271 
272  ! get the package from the list
273  obj => nc_vars%GetItem(idx)
274  if (associated(obj)) then
275  select type (obj)
276  class is (ncfilemf6vartype)
277  res => obj
278  end select
279  end if
280  end function ncvar_get
281 
282 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:228
subroutine fv_add(this, pkgname, tagname, layer, period, iaux, varid)
add netcdf modflow6 input variable to list
Definition: NCFileVars.f90:189
subroutine fv_init(this, modelname, nc_fname, ncid, grid)
initialize netcdf model variable description type
Definition: NCFileVars.f90:157
class(ncfilemf6vartype) function, pointer ncvar_get(nc_vars, idx)
get modflow6 input variable description at position idx
Definition: NCFileVars.f90:264
subroutine fv_destroy(this)
destroy netcdf model variable description type
Definition: NCFileVars.f90:214
integer(i4b) function ncvars_varid(this, tagname, layer, period, iaux)
return a netcdf variable id for a package tagname
Definition: NCFileVars.f90:74
subroutine ncvars_destroy(this)
destroy netcdf package variable lists
Definition: NCFileVars.f90:142
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