MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
MeshNCModel.f90
Go to the documentation of this file.
1!> @brief This module contains the MeshModelModule
2!!
3!! This module defines a base class for UGRID based
4!! (mesh) model netcdf exports. It is dependent on
5!! external netcdf libraries.
6!<
8
9 use kindmodule, only: dp, i4b, lgp
20 use netcdf
21
22 implicit none
23 private
25 public :: mesh2dmodeltype
26 public :: ncvar_chunk
27 public :: ncvar_deflate
28 public :: ncvar_gridmap
29 public :: ncvar_mf6attr
30 public :: ncvar_layer
31
32 !> @brief type for storing model export dimension ids
33 !<
35 integer(I4B) :: nmesh_node !< number of nodes in mesh
36 integer(I4B) :: nmesh_face !< number of faces in mesh
37 integer(I4B) :: max_nmesh_face_nodes !< max number of nodes in a single face
38 integer(I4B) :: time !< number of steps
39 integer(I4B) :: bnd !< number in boundary
40 integer(I4B) :: layer !< number of layers
41 contains
42 end type meshncdimidtype
43
44 !> @brief type for storing model export variable ids
45 !<
47 integer(I4B) :: mesh !< mesh container variable
48 integer(I4B) :: mesh_node_x !< mesh nodes x array
49 integer(I4B) :: mesh_node_y !< mesh nodes y array
50 integer(I4B) :: mesh_face_x !< mesh faces x location array
51 integer(I4B) :: mesh_face_y !< mesh faces y location array
52 integer(I4B) :: mesh_face_xbnds !< mesh faces 2D x bounds array
53 integer(I4B) :: mesh_face_ybnds !< mesh faces 2D y bounds array
54 integer(I4B) :: mesh_face_nodes !< mesh faces 2D nodes array
55 integer(I4B) :: time !< time coordinate variable
56 integer(I4B) :: time_bnds !< time boundaries 2D array
57 integer(I4B) :: layer !< layer coordinate variable
58 integer(I4B), dimension(:), allocatable :: export !< in scope layer export
59 integer(I4B), dimension(:), allocatable :: dependent !< layered dependent variables array
60 integer(I4B), dimension(:), allocatable :: elevation !< z_lN per-layer cell center elevation variables array
61 contains
62 end type meshncvaridtype
63
64 !> @brief base ugrid netcdf export type
65 !<
66 type, abstract, extends(ncbasemodelexporttype) :: meshmodeltype
67 type(meshncdimidtype) :: dim_ids !< dimension ids
68 type(meshncvaridtype) :: var_ids !< variable ids
69 integer(I4B) :: nlay !< number of layers
70 integer(I4B), pointer :: chunk_face !< chunking parameter for face dimension
71 contains
72 procedure :: mesh_init
73 procedure :: mesh_destroy
74 procedure :: df_export
75 procedure :: export_df
76 procedure :: create_timeseries
77 procedure :: add_global_att
78 procedure(nc_array_export_if), deferred :: export_input_array
80 procedure :: add_pkg_data
81 procedure :: define_dependent
82 procedure :: define_gridmap
83 end type meshmodeltype
84
85 !> @brief abstract interfaces for derived ugrid netcd export types
86 !<
87 abstract interface
88 subroutine nc_array_export_if(this, pkgtype, pkgname, mempath, idt)
90 class(meshmodeltype), intent(inout) :: this
91 character(len=*), intent(in) :: pkgtype
92 character(len=*), intent(in) :: pkgname
93 character(len=*), intent(in) :: mempath
94 type(inputparamdefinitiontype), pointer, intent(in) :: idt
95 end subroutine
96 end interface
97
98 type, abstract, extends(meshmodeltype) :: mesh2dmodeltype
99 contains
100 procedure :: create_mesh
101 end type mesh2dmodeltype
102
103contains
104
105 !> @brief initialize
106 !<
107 subroutine mesh_init(this, modelname, modeltype, modelfname, nc_fname, &
108 disenum, nctype, lenuni, iout)
110 class(meshmodeltype), intent(inout) :: this
111 character(len=*), intent(in) :: modelname
112 character(len=*), intent(in) :: modeltype
113 character(len=*), intent(in) :: modelfname
114 character(len=*), intent(in) :: nc_fname
115 integer(I4B), intent(in) :: disenum
116 integer(I4B), intent(in) :: nctype
117 integer(I4B), intent(in) :: lenuni
118 integer(I4B), intent(in) :: iout
119 logical(LGP) :: found
120 integer(I4B) :: latsz, lonsz
121
122 ! initialize base class
123 call this%NCModelExportType%init(modelname, modeltype, modelfname, nc_fname, &
124 disenum, nctype, iout)
125
126 if (this%ncf_mempath /= '') then
127 latsz = 0
128 lonsz = 0
129 call get_isize('LATITUDE', this%ncf_mempath, latsz)
130 call get_isize('LONGITUDE', this%ncf_mempath, lonsz)
131 if (latsz > 0 .or. lonsz > 0) then
132 write (warnmsg, '(a)') 'LATITUDE and LONGITUDE griddata are not &
133 &supported for a NETCDF_MESH2D export and will be ignored. &
134 &Applies to file "'//trim(nc_fname)//'".'
136 end if
137 end if
138
139 ! allocate and initialize
140 allocate (this%chunk_face)
141 this%chunk_face = -1
142
143 ! update values from input context
144 if (this%ncf_mempath /= '') then
145 call mem_set_value(this%chunk_face, 'CHUNK_FACE', this%ncf_mempath, found)
146 end if
147
148 if (this%chunk_time > 0 .and. this%chunk_face > 0) then
149 this%chunking_active = .true.
150 else if (this%chunk_time > 0 .or. this%chunk_face > 0) then
151 this%chunk_face = -1
152 this%chunk_time = -1
153 write (warnmsg, '(a)') 'Ignoring user provided NetCDF chunking parameter. &
154 &Define chunk_time and chunk_face input parameters to see an effect in &
155 &file "'//trim(nc_fname)//'".'
157 end if
158
159 if (lenuni == 1) then
160 this%lenunits = 'ft'
161 else if (lenuni == 3) then
162 this%lenunits = 'cm'
163 else
164 this%lenunits = 'm'
165 end if
166
167 ! create the netcdf file
168 call nf_verify(nf90_create(this%nc_fname, &
169 ior(nf90_clobber, nf90_netcdf4), this%ncid), &
170 this%nc_fname)
171 end subroutine mesh_init
172
173 !> @brief initialize
174 !<
175 subroutine mesh_destroy(this)
177 class(meshmodeltype), intent(inout) :: this
178 call nf_verify(nf90_close(this%ncid), this%nc_fname)
179 deallocate (this%chunk_face)
180 nullify (this%chunk_face)
181 end subroutine mesh_destroy
182
183 !> @brief define timeseries input variables
184 !<
185 subroutine df_export(this)
187 class(meshmodeltype), intent(inout) :: this
188 class(exportpackagetype), pointer :: export_pkg
189 integer(I4B) :: idx
190 do idx = 1, this%pkglist%Count()
191 export_pkg => this%get(idx)
192 call this%export_df(export_pkg)
193 end do
194 end subroutine df_export
195
196 !> @brief define export package
197 !<
198 subroutine export_df(this, export_pkg)
201 class(meshmodeltype), intent(inout) :: this
202 class(exportpackagetype), pointer, intent(in) :: export_pkg
203 type(inputparamdefinitiontype), pointer :: idt
204 integer(I4B) :: iparam, iaux, layer
205
206 ! export defined period input
207 do iparam = 1, export_pkg%nparam
208 ! initialize
209 iaux = 0
210 layer = 0
211 ! set input definition
212 idt => &
213 get_param_definition_type(export_pkg%mf6_input%param_dfns, &
214 export_pkg%mf6_input%component_type, &
215 export_pkg%mf6_input%subcomponent_type, &
216 'PERIOD', export_pkg%param_names(iparam), '')
217
218 select case (idt%shape)
219 case ('NCPL')
220 call this%create_timeseries(idt, iparam, iaux, layer, export_pkg)
221 case ('NODES')
222 do layer = 1, this%nlay
223 call this%create_timeseries(idt, iparam, iaux, layer, export_pkg)
224 end do
225 case ('NAUX NCPL')
226 do iaux = 1, export_pkg%naux
227 call this%create_timeseries(idt, iparam, iaux, layer, export_pkg)
228 end do
229 case ('NAUX NODES')
230 do iaux = 1, export_pkg%naux
231 do layer = 1, this%nlay
232 call this%create_timeseries(idt, iparam, iaux, layer, export_pkg)
233 end do
234 end do
235 case default
236 end select
237 end do
238 end subroutine export_df
239
240 !> @brief create timeseries export variable
241 !<
242 subroutine create_timeseries(this, idt, iparam, iaux, layer, export_pkg)
243 use constantsmodule, only: dnodata
245 class(meshmodeltype), intent(inout) :: this
246 type(inputparamdefinitiontype), pointer, intent(in) :: idt
247 integer(I4B), intent(in) :: iparam
248 integer(I4B), intent(in) :: iaux
249 integer(I4B), intent(in) :: layer
250 class(exportpackagetype), pointer, intent(in) :: export_pkg
251 character(len=LINELENGTH) :: varname, longname, nc_tag
252 integer(I4B) :: varid
253
254 ! set variable input tag
255 nc_tag = this%input_attribute(export_pkg%mf6_input%subcomponent_name, &
256 idt)
257
258 ! set names
259 varname = export_varname(export_pkg%mf6_input%subcomponent_name, &
260 idt%tagname, export_pkg%mf6_input%mempath, &
261 layer=layer, iaux=iaux)
262 longname = export_longname(idt%longname, &
263 export_pkg%mf6_input%subcomponent_name, &
264 idt%tagname, export_pkg%mf6_input%mempath, &
265 layer=layer, iaux=iaux, &
266 component_type=idt%component_type, &
267 subcomponent_type=idt%subcomponent_type)
268
269 ! create the netcdf dependent layer variable
270 select case (idt%datatype)
271 case ('DOUBLE1D', 'DOUBLE2D')
272 call nf_verify(nf90_def_var(this%ncid, varname, nf90_double, &
273 (/this%dim_ids%nmesh_face, &
274 this%dim_ids%time/), &
275 varid), &
276 this%nc_fname)
277 call nf_verify(nf90_put_att(this%ncid, varid, &
278 '_FillValue', (/dnodata/)), &
279 this%nc_fname)
280 ! input in effect over the stress period (CF-1.13 7.3)
281 call nf_verify(nf90_put_att(this%ncid, varid, 'cell_methods', &
282 'time: mean'), this%nc_fname)
283 case ('INTEGER1D')
284 call nf_verify(nf90_def_var(this%ncid, varname, nf90_int, &
285 (/this%dim_ids%nmesh_face, &
286 this%dim_ids%time/), &
287 varid), &
288 this%nc_fname)
289 call nf_verify(nf90_put_att(this%ncid, varid, &
290 '_FillValue', (/nf90_fill_int/)), &
291 this%nc_fname)
292 end select
293
294 ! apply chunking parameters
295 if (this%chunking_active) then
296 call nf_verify(nf90_def_var_chunking(this%ncid, &
297 varid, &
298 nf90_chunked, &
299 (/this%chunk_face, &
300 this%chunk_time/)), &
301 this%nc_fname)
302 end if
303
304 ! deflate and shuffle
305 call ncvar_deflate(this%ncid, varid, this%deflate, &
306 this%shuffle, this%nc_fname)
307
308 ! assign variable attributes
309 call nf_verify(nf90_put_att(this%ncid, varid, &
310 'long_name', longname), this%nc_fname)
311
312 ! add grid mapping and mf6 attr (mesh, location, coordinates, grid_mapping)
313 call ncvar_gridmap(this%ncid, varid, &
314 this%gridmap_name, layer, this%nc_fname)
315 call ncvar_mf6attr(this%ncid, varid, layer, iaux, nc_tag, this%nc_fname)
316
317 ! store variable id
318 if (idt%tagname == 'AUX') then
319 if (layer > 0) then
320 export_pkg%varids_aux(iaux, layer) = varid
321 else
322 export_pkg%varids_aux(iaux, 1) = varid
323 end if
324 else
325 if (layer > 0) then
326 export_pkg%varids_param(iparam, layer) = varid
327 else
328 export_pkg%varids_param(iparam, 1) = varid
329 end if
330 end if
331 end subroutine create_timeseries
332
333 !> @brief create file (group) attributes
334 !<
335 subroutine add_global_att(this)
336 class(meshmodeltype), intent(inout) :: this
337 ! file scoped title
338 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'title', &
339 this%annotation%title), this%nc_fname)
340 ! source (MODFLOW 6)
341 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'source', &
342 this%annotation%source), this%nc_fname)
343 ! grid type (MODFLOW 6)
344 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'modflow_grid', &
345 this%annotation%grid), this%nc_fname)
346 ! mesh type (MODFLOW 6)
347 if (this%annotation%mesh /= '') then
348 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'modflow_mesh', &
349 this%annotation%mesh), this%nc_fname)
350
351 end if
352 ! MODFLOW 6 model type
353 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'modflow_model', &
354 this%annotation%model), this%nc_fname)
355 ! generation datetime
356 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'history', &
357 this%annotation%history), this%nc_fname)
358 ! supported conventions
359 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'Conventions', &
360 this%annotation%conventions), &
361 this%nc_fname)
362 end subroutine add_global_att
363
364 !> @brief write package gridded input data
365 !<
366 subroutine export_input_arrays(this, pkgtype, pkgname, mempath, param_dfns)
368 class(meshmodeltype), intent(inout) :: this
369 character(len=*), intent(in) :: pkgtype
370 character(len=*), intent(in) :: pkgname
371 character(len=*), intent(in) :: mempath
372 type(inputparamdefinitiontype), dimension(:), pointer, &
373 intent(in) :: param_dfns
374 type(inputparamdefinitiontype), pointer :: idt
375 integer(I4B) :: iparam, isize
376 ! export griddata block parameters
377 do iparam = 1, size(param_dfns)
378 ! assign param definition pointer
379 idt => param_dfns(iparam)
380 ! for now only griddata is exported
381 if (idt%blockname == 'GRIDDATA') then
382 ! veriy variable is allocated
383 call get_isize(idt%mf6varname, mempath, isize)
384 if (isize > 0) then
385 call this%export_input_array(pkgtype, pkgname, mempath, idt)
386 end if
387 end if
388 end do
389 end subroutine export_input_arrays
390
391 !> @brief determine packages to write gridded input
392 !<
393 subroutine add_pkg_data(this)
401 class(meshmodeltype), intent(inout) :: this
402 character(LENCOMPONENTNAME) :: ptype, pname, pkgtype
403 character(len=LENMEMPATH) :: input_mempath
404 type(characterstringtype), dimension(:), contiguous, &
405 pointer :: pkgtypes => null()
406 type(characterstringtype), dimension(:), contiguous, &
407 pointer :: pkgnames => null()
408 type(characterstringtype), dimension(:), contiguous, &
409 pointer :: mempaths => null()
410 type(inputparamdefinitiontype), dimension(:), pointer :: param_dfns
411 character(len=LENMEMPATH) :: mempath
412 integer(I4B) :: n
413 integer(I4B), pointer :: export_arrays
414 logical(LGP) :: found
415
416 input_mempath = create_mem_path(component=this%modelname, context=idm_context)
417
418 ! set pointers to model path package info
419 call mem_setptr(pkgtypes, 'PKGTYPES', input_mempath)
420 call mem_setptr(pkgnames, 'PKGNAMES', input_mempath)
421 call mem_setptr(mempaths, 'MEMPATHS', input_mempath)
422
423 allocate (export_arrays)
424
425 do n = 1, size(mempaths)
426 ! initialize export_arrays
427 export_arrays = 0
428
429 ! set package attributes
430 mempath = mempaths(n)
431 pname = pkgnames(n)
432 ptype = pkgtypes(n)
433
434 ! export input arrays
435 if (mempath /= '') then
436 ! update export
437 call mem_set_value(export_arrays, 'EXPORT_NC', mempath, found, &
438 release=.false.)
439 if (export_arrays > 0) then
440 pkgtype = idm_subcomponent_type(this%modeltype, ptype)
441 ! base packages are named by type, as in the input context
442 pname = idm_subcomponent_name(this%modeltype, pkgtype, pname)
443 param_dfns => param_definitions(this%modeltype, pkgtype)
444 call this%export_input_arrays(ptype, pname, mempath, param_dfns)
445 end if
446 end if
447 end do
448
449 ! cleanup
450 deallocate (export_arrays)
451 end subroutine add_pkg_data
452
453 !> @brief create the model layer dependent variables
454 !<
455 subroutine define_dependent(this)
456 class(meshmodeltype), intent(inout) :: this
457 character(len=LINELENGTH) :: varname, longname
458 integer(I4B) :: k
459
460 ! create a dependent variable for each layer
461 do k = 1, this%nlay
462 ! initialize names
463 varname = ''
464 longname = ''
465
466 ! set layer variable and longnames
467 write (varname, '(a,i0)') trim(this%xname)//'_l', k
468 write (longname, '(a,i0,a)') trim(this%annotation%longname)// &
469 ' (layer ', k, ')'
470
471 ! create the netcdf dependent layer variable
472 call nf_verify(nf90_def_var(this%ncid, varname, nf90_double, &
473 (/this%dim_ids%nmesh_face, &
474 this%dim_ids%time/), &
475 this%var_ids%dependent(k)), &
476 this%nc_fname)
477
478 ! apply chunking parameters
479 if (this%chunking_active) then
480 call nf_verify(nf90_def_var_chunking(this%ncid, &
481 this%var_ids%dependent(k), &
482 nf90_chunked, &
483 (/this%chunk_face, &
484 this%chunk_time/)), &
485 this%nc_fname)
486 end if
487
488 ! deflate and shuffle
489 call ncvar_deflate(this%ncid, this%var_ids%dependent(k), this%deflate, &
490 this%shuffle, this%nc_fname)
491
492 ! assign variable attributes; head is a length, other dependent
493 ! variables have no units known from the input
494 if (this%modeltype == 'GWF') then
495 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent(k), &
496 'units', this%lenunits), this%nc_fname)
497 end if
498 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent(k), &
499 'long_name', longname), this%nc_fname)
500 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent(k), &
501 '_FillValue', (/dhnoflo/)), &
502 this%nc_fname)
503 ! cell_methods (CF-1.13 7.3): dependent variable values are the
504 ! instantaneous simulated state at each output time, not a time
505 ! mean/accumulation over the interval -- an intensive quantity whose
506 ! default method is already "point" per CF's own Appendix E, stated
507 ! explicitly here per CF's recommendation to do so for every
508 ! spatio-temporal dimension.
509 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent(k), &
510 'cell_methods', 'time: point'), &
511 this%nc_fname)
512
513 ! add grid mapping (mesh, location, coordinates, grid_mapping)
514 call ncvar_gridmap(this%ncid, this%var_ids%dependent(k), &
515 this%gridmap_name, k, this%nc_fname)
516
517 ! add layer attribute
518 call ncvar_layer(this%ncid, this%var_ids%dependent(k), k, &
519 this%nc_fname)
520 end do
521 end subroutine define_dependent
522
523 !> @brief create the file grid mapping container variable
524 !<
525 subroutine define_gridmap(this)
526 class(meshmodeltype), intent(inout) :: this
527 integer(I4B) :: var_id
528 character(len=LINELENGTH) :: gmname
529 character(len=LENBIGLINE) :: effective_crs_wkt
530
531 ! was projection info provided
532 if (this%wkt /= '' .or. this%crs_wkt /= '') then
533 ! create projection variable
534 call nf_verify(nf90_redef(this%ncid), this%nc_fname)
535 call nf_verify(nf90_def_var(this%ncid, this%gridmap_name, nf90_int, &
536 var_id), this%nc_fname)
537 ! wkt (WKT1, OGC 01-009) -- for legacy/QGIS compatibility
538 if (this%wkt /= '') then
539 call nf_verify(nf90_put_att(this%ncid, var_id, 'wkt', this%wkt), &
540 this%nc_fname)
541 end if
542 ! crs_wkt (WKT2, ISO 19162:2019) -- required by CF-1.13
543 if (this%crs_wkt /= '') then
544 effective_crs_wkt = this%crs_wkt
545 else
546 effective_crs_wkt = this%wkt
547 end if
548 call nf_verify(nf90_put_att(this%ncid, var_id, 'crs_wkt', &
549 trim(effective_crs_wkt)), this%nc_fname)
550 ! grid_mapping_name derived from WKT projection keyword
551 gmname = wkt_to_cf_gridmapping(trim(effective_crs_wkt))
552 if (gmname /= '') then
553 call nf_verify(nf90_put_att(this%ncid, var_id, 'grid_mapping_name', &
554 trim(gmname)), this%nc_fname)
555 end if
556 call nf_verify(nf90_enddef(this%ncid), this%nc_fname)
557 call nf_verify(nf90_put_var(this%ncid, var_id, 1), &
558 this%nc_fname)
559 end if
560 end subroutine define_gridmap
561
562 !> @brief create the file mesh container variable
563 !<
564 subroutine create_mesh(this)
565 class(mesh2dmodeltype), intent(inout) :: this
566
567 ! create mesh container variable
568 call nf_verify(nf90_def_var(this%ncid, this%mesh_name, nf90_int, &
569 this%var_ids%mesh), this%nc_fname)
570
571 ! assign container variable attributes
572 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh, 'cf_role', &
573 'mesh_topology'), this%nc_fname)
574 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh, 'long_name', &
575 '2D mesh topology'), this%nc_fname)
576 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh, &
577 'topology_dimension', 2), this%nc_fname)
578 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh, 'face_dimension', &
579 'nmesh_face'), this%nc_fname)
580 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh, &
581 'node_coordinates', 'mesh_node_x mesh_node_y'), &
582 this%nc_fname)
583 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh, &
584 'face_coordinates', 'mesh_face_x mesh_face_y'), &
585 this%nc_fname)
586 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh, &
587 'face_node_connectivity', 'mesh_face_nodes'), &
588 this%nc_fname)
589
590 ! create mesh x node (mesh vertex) variable
591 call nf_verify(nf90_def_var(this%ncid, 'mesh_node_x', nf90_double, &
592 (/this%dim_ids%nmesh_node/), &
593 this%var_ids%mesh_node_x), this%nc_fname)
594
595 ! assign mesh x node variable attributes
596 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_node_x, &
597 'units', this%lenunits), this%nc_fname)
598 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_node_x, &
599 'standard_name', 'projection_x_coordinate'), &
600 this%nc_fname)
601 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_node_x, &
602 'long_name', 'Easting'), this%nc_fname)
603
604 if (this%gridmap_name /= '') then
605 ! associate with projection
606 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_node_x, &
607 'grid_mapping', this%gridmap_name), &
608 this%nc_fname)
609 end if
610
611 ! create mesh y node (mesh vertex) variable
612 call nf_verify(nf90_def_var(this%ncid, 'mesh_node_y', nf90_double, &
613 (/this%dim_ids%nmesh_node/), &
614 this%var_ids%mesh_node_y), this%nc_fname)
615
616 ! assign mesh y variable attributes
617 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_node_y, &
618 'units', this%lenunits), this%nc_fname)
619 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_node_y, &
620 'standard_name', 'projection_y_coordinate'), &
621 this%nc_fname)
622 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_node_y, &
623 'long_name', 'Northing'), this%nc_fname)
624
625 if (this%gridmap_name /= '') then
626 ! associate with projection
627 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_node_y, &
628 'grid_mapping', this%gridmap_name), &
629 this%nc_fname)
630 end if
631
632 ! create mesh x face (cell vertex) variable
633 call nf_verify(nf90_def_var(this%ncid, 'mesh_face_x', nf90_double, &
634 (/this%dim_ids%nmesh_face/), &
635 this%var_ids%mesh_face_x), this%nc_fname)
636
637 ! assign mesh x face variable attributes
638 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_x, &
639 'units', this%lenunits), this%nc_fname)
640 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_x, &
641 'standard_name', 'projection_x_coordinate'), &
642 this%nc_fname)
643 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_x, &
644 'long_name', 'Easting'), this%nc_fname)
645 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_x, 'bounds', &
646 'mesh_face_xbnds'), this%nc_fname)
647 if (this%gridmap_name /= '') then
648 ! associate with projection
649 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_x, &
650 'grid_mapping', this%gridmap_name), &
651 this%nc_fname)
652 end if
653
654 ! create mesh x cell bounds variable
655 call nf_verify(nf90_def_var(this%ncid, 'mesh_face_xbnds', nf90_double, &
656 (/this%dim_ids%max_nmesh_face_nodes, &
657 this%dim_ids%nmesh_face/), &
658 this%var_ids%mesh_face_xbnds), &
659 this%nc_fname)
660
661 ! create mesh y face (cell vertex) variable
662 call nf_verify(nf90_def_var(this%ncid, 'mesh_face_y', nf90_double, &
663 (/this%dim_ids%nmesh_face/), &
664 this%var_ids%mesh_face_y), this%nc_fname)
665
666 ! assign mesh y face variable attributes
667 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_y, &
668 'units', this%lenunits), this%nc_fname)
669 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_y, &
670 'standard_name', 'projection_y_coordinate'), &
671 this%nc_fname)
672 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_y, &
673 'long_name', 'Northing'), this%nc_fname)
674 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_y, 'bounds', &
675 'mesh_face_ybnds'), this%nc_fname)
676
677 if (this%gridmap_name /= '') then
678 ! associate with projection
679 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_y, &
680 'grid_mapping', this%gridmap_name), &
681 this%nc_fname)
682 end if
683
684 ! create mesh y cell bounds variable
685 call nf_verify(nf90_def_var(this%ncid, 'mesh_face_ybnds', nf90_double, &
686 (/this%dim_ids%max_nmesh_face_nodes, &
687 this%dim_ids%nmesh_face/), &
688 this%var_ids%mesh_face_ybnds), &
689 this%nc_fname)
690
691 ! create mesh face nodes variable
692 call nf_verify(nf90_def_var(this%ncid, 'mesh_face_nodes', nf90_int, &
693 (/this%dim_ids%max_nmesh_face_nodes, &
694 this%dim_ids%nmesh_face/), &
695 this%var_ids%mesh_face_nodes), &
696 this%nc_fname)
697
698 ! assign variable attributes
699 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_nodes, &
700 'cf_role', 'face_node_connectivity'), &
701 this%nc_fname)
702 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_nodes, &
703 'long_name', &
704 'Vertices bounding cell (counterclockwise)'), &
705 this%nc_fname)
706 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_nodes, &
707 '_FillValue', (/nf90_fill_int/)), &
708 this%nc_fname)
709 call nf_verify(nf90_put_att(this%ncid, this%var_ids%mesh_face_nodes, &
710 'start_index', 1), this%nc_fname)
711 end subroutine create_mesh
712
713 !> @brief define variable chunking
714 !<
715 subroutine ncvar_chunk(ncid, varid, chunk_face, nc_fname)
716 integer(I4B), intent(in) :: ncid
717 integer(I4B), intent(in) :: varid
718 integer(I4B), intent(in) :: chunk_face
719 character(len=*), intent(in) :: nc_fname
720 if (chunk_face > 0) then
721 call nf_verify(nf90_def_var_chunking(ncid, varid, nf90_chunked, &
722 (/chunk_face/)), nc_fname)
723 end if
724 end subroutine ncvar_chunk
725
726 !> @brief define variable compression
727 !<
728 subroutine ncvar_deflate(ncid, varid, deflate, shuffle, nc_fname)
729 integer(I4B), intent(in) :: ncid
730 integer(I4B), intent(in) :: varid
731 integer(I4B), intent(in) :: deflate
732 integer(I4B), intent(in) :: shuffle
733 character(len=*), intent(in) :: nc_fname
734 if (deflate >= 0) then
735 call nf_verify(nf90_def_var_deflate(ncid, varid, shuffle=shuffle, &
736 deflate=1, deflate_level=deflate), &
737 nc_fname)
738 end if
739 end subroutine ncvar_deflate
740
741 !> @brief put variable gridmap attributes
742 !!
743 !! layer must be the 1-based layer number when varid is a per-layer
744 !! variable (e.g. npf_k_l3), or 0 when it is not layered (e.g. a flat
745 !! NCPL-shaped array). z_l{layer} is only referenced from coordinates
746 !! when layer > 0, since it is the only case where z_l{layer} shares
747 !! nmesh_face with varid (CF-1.13 5.2 subset rule).
748 !<
749 subroutine ncvar_gridmap(ncid, varid, gridmap_name, layer, nc_fname)
750 integer(I4B), intent(in) :: ncid
751 integer(I4B), intent(in) :: varid
752 character(len=*), intent(in) :: gridmap_name
753 integer(I4B), intent(in) :: layer
754 character(len=*), intent(in) :: nc_fname
755 character(len=LINELENGTH) :: coords
756 ! UGRID topology attrs are CRS-independent -- always written on face vars
757 call nf_verify(nf90_put_att(ncid, varid, 'mesh', 'mesh'), nc_fname)
758 call nf_verify(nf90_put_att(ncid, varid, 'location', 'face'), nc_fname)
759 if (layer > 0) then
760 write (coords, '(a,i0)') 'mesh_face_x mesh_face_y z_l', layer
761 else
762 coords = 'mesh_face_x mesh_face_y'
763 end if
764 call nf_verify(nf90_put_att(ncid, varid, 'coordinates', trim(coords)), &
765 nc_fname)
766 ! grid_mapping only written when a CRS is configured
767 if (gridmap_name /= '') then
768 call nf_verify(nf90_put_att(ncid, varid, 'grid_mapping', &
769 gridmap_name), nc_fname)
770 end if
771 end subroutine ncvar_gridmap
772
773 !> @brief put variable internal attributes
774 !<
775 subroutine ncvar_mf6attr(ncid, varid, layer, iaux, nc_tag, nc_fname)
776 integer(I4B), intent(in) :: ncid
777 integer(I4B), intent(in) :: varid
778 integer(I4B), intent(in) :: layer
779 integer(I4B), intent(in) :: iaux
780 character(len=*), intent(in) :: nc_tag
781 character(len=*), intent(in) :: nc_fname
782 if (nc_tag /= '') then
783 call nf_verify(nf90_put_att(ncid, varid, 'modflow_input', &
784 nc_tag), nc_fname)
785 call ncvar_layer(ncid, varid, layer, nc_fname)
786 if (iaux > 0) then
787 call nf_verify(nf90_put_att(ncid, varid, 'modflow_iaux', &
788 iaux), nc_fname)
789 end if
790 end if
791 end subroutine ncvar_mf6attr
792
793 !> @brief put the layer attribute (only when layer > 0)
794 !!
795 !! layer identifies which layer a per-layer-split mesh variable belongs
796 !! to (e.g. head_l3, npf_k_l3) -- distinct from the layer dimension
797 !! coordinate. Not a CF/UGRID attribute; MF6-internal. Applies to every
798 !! per-layer-split variable.
799 !<
800 subroutine ncvar_layer(ncid, varid, layer, nc_fname)
801 integer(I4B), intent(in) :: ncid
802 integer(I4B), intent(in) :: varid
803 integer(I4B), intent(in) :: layer
804 character(len=*), intent(in) :: nc_fname
805 if (layer > 0) then
806 call nf_verify(nf90_put_att(ncid, varid, 'layer', layer), nc_fname)
807 end if
808 end subroutine ncvar_layer
809
810end module meshmodelmodule
abstract interfaces for derived ugrid netcd export types
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 lencomponentname
maximum length of a component name
Definition Constants.f90:18
real(dp), parameter dnodata
real no data constant
Definition Constants.f90:95
integer(i4b), parameter lenbigline
maximum length of a big line
Definition Constants.f90:15
real(dp), parameter dhnoflo
real no flow constant
Definition Constants.f90:93
integer(i4b), parameter lenmempath
maximum length of the memory path
Definition Constants.f90:27
This module contains the DefinitionSelectModule.
type(inputparamdefinitiontype) function, pointer, public get_param_definition_type(input_definition_types, component_type, subcomponent_type, blockname, tagname, filename, found)
Return parameter definition.
type(inputparamdefinitiontype) function, dimension(:), pointer, public param_definitions(component, subcomponent)
logical function, public idm_multi_package(component, subcomponent)
Input definition module.
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
subroutine, public get_isize(name, mem_path, isize)
Get the number of elements for this variable.
This module contains the MeshModelModule.
subroutine, public ncvar_mf6attr(ncid, varid, layer, iaux, nc_tag, nc_fname)
put variable internal attributes
subroutine, public ncvar_gridmap(ncid, varid, gridmap_name, layer, nc_fname)
put variable gridmap attributes
subroutine define_gridmap(this)
create the file grid mapping container variable
subroutine create_timeseries(this, idt, iparam, iaux, layer, export_pkg)
create timeseries export variable
subroutine, public ncvar_chunk(ncid, varid, chunk_face, nc_fname)
define variable chunking
subroutine mesh_init(this, modelname, modeltype, modelfname, nc_fname, disenum, nctype, lenuni, iout)
initialize
subroutine mesh_destroy(this)
initialize
subroutine add_global_att(this)
create file (group) attributes
subroutine add_pkg_data(this)
determine packages to write gridded input
subroutine df_export(this)
define timeseries input variables
subroutine export_df(this, export_pkg)
define export package
subroutine, public ncvar_layer(ncid, varid, layer, nc_fname)
put the layer attribute (only when layer > 0)
subroutine export_input_arrays(this, pkgtype, pkgname, mempath, param_dfns)
write package gridded input data
subroutine define_dependent(this)
create the model layer dependent variables
subroutine, public ncvar_deflate(ncid, varid, deflate, shuffle, nc_fname)
define variable compression
subroutine create_mesh(this)
create the file mesh container variable
This module contains the NCModelExportModule.
Definition NCModel.f90:8
character(len=linelength) function, public wkt_to_cf_gridmapping(wkt)
map a WKT string to a CF-1.13 grid_mapping_name
Definition NCModel.f90:681
character(len=linelength) function, public export_varname(pkgname, tagname, mempath, layer, iaux)
build netcdf variable name
Definition NCModel.f90:562
character(len=linelength) function, public export_longname(longname, pkgname, tagname, mempath, layer, iaux, component_type, subcomponent_type)
build netcdf variable longname
Definition NCModel.f90:603
This module contains the NetCDFCommonModule.
subroutine, public nf_verify(res, nc_fname)
error check a netcdf-fortran interface call
This module contains simulation methods.
Definition Sim.f90:10
subroutine, public store_warning(msg, substring)
Store warning message.
Definition Sim.f90:241
subroutine, public store_error(msg, terminate)
Store an error message.
Definition Sim.f90:94
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition Sim.f90:208
This module contains simulation variables.
character(len=maxcharlen) errmsg
error message string
character(len=linelength) idm_context
character(len=maxcharlen) warnmsg
warning message string
This module contains the SourceCommonModule.
character(len=lencomponentname) function, public idm_subcomponent_type(component, subcomponent)
component from package or model type
character(len=lenpackagename) function, public idm_subcomponent_name(component_type, subcomponent_type, sc_name)
model package subcomponent name
This class is used to store a single deferred-length character string. It was designed to work in an ...
Input parameter definition. Describes an input parameter.
base ugrid netcdf export type
type for storing model export dimension ids
type for storing model export variable ids
abstract type for model netcdf export type
Definition NCModel.f90:120