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