MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
DisNCStructured.f90
Go to the documentation of this file.
1!> @brief This module contains the DisNCStructuredModule
2!!
3!! This module defines a STRUCTURED (non-ugrid) netcdf
4!! export type for DIS models. It is dependent on netcdf
5!! libraries.
6!!
7!<
9
10 use kindmodule, only: dp, i4b, lgp
24 use dismodule, only: distype
26 use netcdf
27
28 implicit none
29 private
30 public :: disncstructuredtype
31
33 integer(I4B) :: x !< number of columns
34 integer(I4B) :: y !< number of rows
35 integer(I4B) :: layer !< number of layers
36 integer(I4B) :: time !< number of steps
37 integer(I4B) :: bnd !< number in boundary
38 contains
40
42 integer(I4B) :: x !< x coordinate variable
43 integer(I4B) :: y !< y coordinate variable
44 integer(I4B) :: layer !< layer coordinate variable (discrete layer index, not a position)
45 integer(I4B) :: elevation !< z auxiliary coordinate variable (cell center elevation, a real vertical position)
46 integer(I4B) :: time !< time coordinate variable
47 integer(I4B) :: time_bnds !< time boundaries 2D array
48 integer(I4B) :: dependent !< dependent variable
49 integer(I4B) :: x_bnds !< x boundaries 2D array
50 integer(I4B) :: y_bnds !< y boundaries 2D array
51 integer(I4B) :: latitude !< latitude 2D array
52 integer(I4B) :: longitude !< longitude 2D array
53 integer(I4B) :: export !< in scope export
54 contains
56
58 type(structuredncdimidtype) :: dim_ids !< structured dimension ids type
59 type(structuredncvaridtype) :: var_ids !< structured variable ids type
60 type(distype), pointer :: dis => null() !< pointer to model dis package
61 integer(I4B) :: nlay !< number of layers
62 real(dp), dimension(:), pointer, contiguous :: latitude => null() !< lat input array pointer
63 real(dp), dimension(:), pointer, contiguous :: longitude => null() !< lon input array pointer
64 integer(I4B), pointer :: chunk_z !< chunking parameter for z dimension
65 integer(I4B), pointer :: chunk_y !< chunking parameter for y dimension
66 integer(I4B), pointer :: chunk_x !< chunking parameter for x dimension
67 integer(I4B), dimension(:), allocatable :: layers !< layers array
68 logical(LGP) :: latlon !< are lat and lon arrays to be written to netcdf file
69 contains
70 procedure :: init => dis_export_init
72 procedure :: df
73 procedure :: df_export
74 procedure :: step
75 procedure :: export_input_array
76 procedure :: export_df
77 procedure :: create_timeseries
79 procedure :: package_step
80 procedure :: add_pkg_data
81 procedure :: add_global_att
82 procedure :: define_dim
83 procedure :: define_dependent
84 procedure :: define_gridmap
85 procedure :: define_geocoords
86 procedure :: add_proj_data
87 procedure :: add_grid_data
88 end type disncstructuredtype
89
91 module procedure nc_export_int1d, nc_export_int2d, &
94 end interface nc_export_array
95
96contains
97
98 !> @brief netcdf export dis init
99 !<
100 subroutine dis_export_init(this, modelname, modeltype, modelfname, nc_fname, &
101 disenum, nctype, iout)
104 class(disncstructuredtype), intent(inout) :: this
105 character(len=*), intent(in) :: modelname
106 character(len=*), intent(in) :: modeltype
107 character(len=*), intent(in) :: modelfname
108 character(len=*), intent(in) :: nc_fname
109 integer(I4B), intent(in) :: disenum
110 integer(I4B), intent(in) :: nctype
111 integer(I4B), intent(in) :: iout
112 integer(I4B) :: k, latsz, lonsz
113 logical(LGP) :: found
114
115 ! set nlay
116 this%nlay = this%dis%nlay
117
118 ! allocate
119 allocate (this%chunk_z)
120 allocate (this%chunk_y)
121 allocate (this%chunk_x)
122 allocate (this%layers(this%nlay))
123
124 ! initialize
125 this%chunk_z = -1
126 this%chunk_y = -1
127 this%chunk_x = -1
128 do k = 1, this%nlay
129 this%layers(k) = k
130 end do
131
132 this%latlon = .false.
133
134 ! initialize base class
135 call this%NCModelExportType%init(modelname, modeltype, modelfname, nc_fname, &
136 disenum, nctype, iout)
137
138 ! update values from input context
139 if (this%ncf_mempath /= '') then
140 call mem_set_value(this%chunk_z, 'CHUNK_Z', this%ncf_mempath, found)
141 call mem_set_value(this%chunk_y, 'CHUNK_Y', this%ncf_mempath, found)
142 call mem_set_value(this%chunk_x, 'CHUNK_X', this%ncf_mempath, found)
143
144 if (this%chunk_time > 0 .and. this%chunk_z > 0 .and. &
145 this%chunk_y > 0 .and. this%chunk_x > 0) then
146 this%chunking_active = .true.
147 else if (this%chunk_time > 0 .or. this%chunk_z > 0 .or. &
148 this%chunk_y > 0 .or. this%chunk_x > 0) then
149 this%chunk_time = -1
150 this%chunk_z = -1
151 this%chunk_y = -1
152 this%chunk_x = -1
153 write (warnmsg, '(a)') 'Ignoring user provided NetCDF chunking &
154 &parameters. Define chunk_time, chunk_x, chunk_y and chunk_z input &
155 &parameters to see an effect in file "'//trim(nc_fname)//'".'
157 end if
158
159 call get_isize('LATITUDE', this%ncf_mempath, latsz)
160 call get_isize('LONGITUDE', this%ncf_mempath, lonsz)
161
162 if (latsz > 0 .and. lonsz > 0) then
163 this%latlon = .true.
164 if (this%wkt /= '' .or. this%crs_wkt /= '') then
165 write (warnmsg, '(a)') 'Ignoring user provided NetCDF wkt/crs_wkt &
166 &parameter(s) as longitude and latitude arrays have been provided. &
167 &Applies to file "'//trim(nc_fname)//'".'
169 this%wkt = ''
170 this%crs_wkt = ''
171 this%gridmap_name = ''
172 end if
173 call mem_setptr(this%latitude, 'LATITUDE', this%ncf_mempath)
174 call mem_setptr(this%longitude, 'LONGITUDE', this%ncf_mempath)
175 if (this%dis%angrot /= dzero) then
176 write (warnmsg, '(a)') 'Structured rotated grid with &
177 &user-provided latitude/longitude: MF6 cannot verify these &
178 &values correctly account for grid rotation (angrot). They &
179 &must be computed from each cell''s true rotated position, &
180 &not from grid-local x/y. Applies to file "'// &
181 trim(nc_fname)//'".'
183 end if
184 end if
185
186 if (this%gridmap_name /= '') then
187 if (this%dis%angrot /= dzero) then
188 write (warnmsg, '(a)') 'CRS parameter set with structured rotated &
189 &grid. The x/y coordinate variables have grid-local, not &
190 &real-world, values. A CRS-aware consumer must apply the full &
191 &crs_wkt (which encodes rotation when a valid WKT2 PROJCRS was &
192 &supplied) via proper reprojection to resolve the true &
193 &position; deriving real longitude/latitude directly from x/y &
194 &and the flat/numeric CF grid_mapping parameters alone will be &
195 &incorrect, since those parameters never encode grid rotation. &
196 &Applies to file "'//trim(nc_fname)//'".'
198 end if
199 end if
200 end if
201
202 if (this%dis%lenuni == 1) then
203 this%lenunits = 'ft'
204 else if (this%dis%lenuni == 3) then
205 this%lenunits = 'cm'
206 else
207 this%lenunits = 'm'
208 end if
209
210 ! create the netcdf file
211 call nf_verify(nf90_create(this%nc_fname, &
212 ior(nf90_clobber, nf90_netcdf4), this%ncid), &
213 this%nc_fname)
214 end subroutine dis_export_init
215
216 !> @brief netcdf export dis destroy
217 !<
218 subroutine dis_export_destroy(this)
219 class(disncstructuredtype), intent(inout) :: this
220 call nf_verify(nf90_close(this%ncid), this%nc_fname)
221 deallocate (this%chunk_z)
222 deallocate (this%chunk_y)
223 deallocate (this%chunk_x)
224 deallocate (this%layers)
225 nullify (this%chunk_z)
226 nullify (this%chunk_y)
227 nullify (this%chunk_x)
228 ! destroy base class
229 call this%NCModelExportType%destroy()
230 end subroutine dis_export_destroy
231
232 !> @brief netcdf export define
233 !<
234 subroutine df(this)
235 use constantsmodule, only: mvalidate
237 class(disncstructuredtype), intent(inout) :: this
238 ! put root group file scope attributes
239 call this%add_global_att()
240 ! define root group dimensions and coordinate variables
241 call this%define_dim()
242 ! define grid projection variables
243 call this%define_geocoords()
244 if (isim_mode == mvalidate) then
245 ! define period input arrays
246 call this%df_export()
247 else
248 ! define the dependent variable
249 call this%define_dependent()
250 end if
251 ! exit define mode
252 call nf_verify(nf90_enddef(this%ncid), this%nc_fname)
253 ! add data locations
254 call this%add_grid_data()
255 ! add projection data
256 call this%add_proj_data()
257 if (isim_mode == mvalidate) then
258 ! define and set package input griddata
259 call this%add_pkg_data()
260 end if
261 ! define and set gridmap variable
262 call this%define_gridmap()
263 ! synchronize file
264 call nf_verify(nf90_sync(this%ncid), this%nc_fname)
265 end subroutine df
266
267 !> @brief define timeseries input variables
268 !<
269 subroutine df_export(this)
271 class(disncstructuredtype), intent(inout) :: this
272 class(exportpackagetype), pointer :: export_pkg
273 integer(I4B) :: idx
274 do idx = 1, this%pkglist%Count()
275 export_pkg => this%get(idx)
276 call this%export_df(export_pkg)
277 end do
278 end subroutine df_export
279
280 !> @brief netcdf export step
281 !<
282 subroutine step(this)
283 use constantsmodule, only: dhnoflo
284 use tdismodule, only: totim
285 class(disncstructuredtype), intent(inout) :: this
286 real(DP), dimension(:), pointer, contiguous :: dbl1d
287 integer(I4B) :: n, istp
288
289 ! set global step index
290 istp = this%istp()
291
292 if (size(this%dis%nodeuser) < &
293 size(this%dis%nodereduced)) then
294 allocate (dbl1d(size(this%dis%nodereduced)))
295 dbl1d = dhnoflo
296 do n = 1, size(this%dis%nodereduced)
297 if (this%dis%nodereduced(n) > 0) then
298 dbl1d(n) = this%x(this%dis%nodereduced(n))
299 end if
300 end do
301 ! write step data to dependent variable
302 call nf_verify(nf90_put_var(this%ncid, &
303 this%var_ids%dependent, dbl1d, &
304 start=(/1, 1, 1, istp/), &
305 count=(/this%dis%ncol, &
306 this%dis%nrow, &
307 this%dis%nlay, 1/)), &
308 this%nc_fname)
309 deallocate (dbl1d)
310 else
311 ! write step data to dependent variable
312 call nf_verify(nf90_put_var(this%ncid, &
313 this%var_ids%dependent, this%x, &
314 start=(/1, 1, 1, istp/), &
315 count=(/this%dis%ncol, &
316 this%dis%nrow, &
317 this%dis%nlay, 1/)), &
318 this%nc_fname)
319 end if
320
321 ! write to time coordinate variable
322 call nf_verify(nf90_put_var(this%ncid, this%var_ids%time, &
323 totim, start=(/istp/)), &
324 this%nc_fname)
325
326 ! synchronize file
327 call nf_verify(nf90_sync(this%ncid), this%nc_fname)
328 end subroutine step
329
330 !> @brief netcdf export an input array
331 !<
332 subroutine export_input_array(this, pkgtype, pkgname, mempath, idt)
333 class(disncstructuredtype), intent(inout) :: this
334 character(len=*), intent(in) :: pkgtype
335 character(len=*), intent(in) :: pkgname
336 character(len=*), intent(in) :: mempath
337 type(inputparamdefinitiontype), pointer, intent(in) :: idt
338 integer(I4B), dimension(:), pointer, contiguous :: int1d
339 integer(I4B), dimension(:, :), pointer, contiguous :: int2d
340 integer(I4B), dimension(:, :, :), pointer, contiguous :: int3d
341 real(DP), dimension(:), pointer, contiguous :: dbl1d
342 real(DP), dimension(:, :), pointer, contiguous :: dbl2d
343 real(DP), dimension(:, :, :), pointer, contiguous :: dbl3d
344 character(len=LINELENGTH) :: nc_tag
345 integer(I4B) :: iper, iaux
346
347 ! initialize
348 iper = 0
349 iaux = 0
350
351 ! set variable name and input attribute string
352 nc_tag = this%input_attribute(pkgname, idt)
353
354 select case (idt%datatype)
355 case ('INTEGER1D')
356 call mem_setptr(int1d, idt%mf6varname, mempath)
357 call nc_export_array(int1d, this%ncid, this%dim_ids, this%var_ids, &
358 this%dis, idt, mempath, nc_tag, pkgname, &
359 this%gridmap_name, this%latlon, this%deflate, &
360 this%shuffle, this%chunk_z, this%chunk_y, &
361 this%chunk_x, iper, this%nc_fname)
362 case ('INTEGER2D')
363 call mem_setptr(int2d, idt%mf6varname, mempath)
364 call nc_export_array(int2d, this%ncid, this%dim_ids, this%var_ids, &
365 this%dis, idt, mempath, nc_tag, pkgname, &
366 this%gridmap_name, this%latlon, this%deflate, &
367 this%shuffle, this%chunk_z, this%chunk_y, &
368 this%chunk_x, this%nc_fname)
369 case ('INTEGER3D')
370 call mem_setptr(int3d, idt%mf6varname, mempath)
371 call nc_export_array(int3d, this%ncid, this%dim_ids, this%var_ids, &
372 this%dis, idt, mempath, nc_tag, pkgname, &
373 this%gridmap_name, this%latlon, this%deflate, &
374 this%shuffle, this%chunk_z, this%chunk_y, &
375 this%chunk_x, this%nc_fname)
376 case ('DOUBLE1D')
377 call mem_setptr(dbl1d, idt%mf6varname, mempath)
378 call nc_export_array(dbl1d, this%ncid, this%dim_ids, this%var_ids, &
379 this%dis, idt, mempath, nc_tag, pkgname, &
380 this%gridmap_name, this%latlon, this%deflate, &
381 this%shuffle, this%chunk_z, this%chunk_y, &
382 this%chunk_x, iper, iaux, this%nc_fname)
383 case ('DOUBLE2D')
384 call mem_setptr(dbl2d, idt%mf6varname, mempath)
385 call nc_export_array(dbl2d, this%ncid, this%dim_ids, this%var_ids, &
386 this%dis, idt, mempath, nc_tag, pkgname, &
387 this%gridmap_name, this%latlon, this%deflate, &
388 this%shuffle, this%chunk_z, this%chunk_y, &
389 this%chunk_x, this%nc_fname)
390 case ('DOUBLE3D')
391 call mem_setptr(dbl3d, idt%mf6varname, mempath)
392 call nc_export_array(dbl3d, this%ncid, this%dim_ids, this%var_ids, &
393 this%dis, idt, mempath, nc_tag, pkgname, &
394 this%gridmap_name, this%latlon, this%deflate, &
395 this%shuffle, this%chunk_z, this%chunk_y, &
396 this%chunk_x, this%nc_fname)
397 case default
398 ! no-op, no other datatypes exported
399 end select
400 end subroutine export_input_array
401
402 !> @brief define export package
403 !<
404 subroutine export_df(this, export_pkg)
407 class(disncstructuredtype), intent(inout) :: this
408 class(exportpackagetype), pointer, intent(in) :: export_pkg
409 type(inputparamdefinitiontype), pointer :: idt
410 integer(I4B) :: iparam, iaux
411
412 ! export defined period input
413 do iparam = 1, export_pkg%nparam
414 ! initialize
415 iaux = 0
416 ! set input definition
417 idt => &
418 get_param_definition_type(export_pkg%mf6_input%param_dfns, &
419 export_pkg%mf6_input%component_type, &
420 export_pkg%mf6_input%subcomponent_type, &
421 'PERIOD', export_pkg%param_names(iparam), '')
422 select case (idt%shape)
423 case ('NCPL', 'NODES')
424 call this%create_timeseries(idt, iparam, iaux, export_pkg)
425 case ('NAUX NCPL', 'NAUX NODES')
426 do iaux = 1, export_pkg%naux
427 call this%create_timeseries(idt, iparam, iaux, export_pkg)
428 end do
429 case default
430 end select
431 end do
432 end subroutine export_df
433
434 !> @brief create timeseries export variable
435 !<
436 subroutine create_timeseries(this, idt, iparam, iaux, export_pkg)
438 class(disncstructuredtype), intent(inout) :: this
439 type(inputparamdefinitiontype), pointer, intent(in) :: idt
440 integer(I4B), intent(in) :: iparam
441 integer(I4B), intent(in) :: iaux
442 class(exportpackagetype), pointer, intent(in) :: export_pkg
443 character(len=LINELENGTH) :: varname, longname, nc_tag
444 integer(I4B) :: varid
445 logical(LGP) :: has_layer
446
447 has_layer = .not. (idt%shape == 'NCPL' .or. idt%shape == 'NAUX NCPL')
448
449 ! set variable input tag
450 nc_tag = this%input_attribute(export_pkg%mf6_input%subcomponent_name, &
451 idt)
452
453 ! set names
454 varname = export_varname(export_pkg%mf6_input%subcomponent_name, &
455 idt%tagname, export_pkg%mf6_input%mempath, &
456 iaux=iaux)
457 longname = export_longname(idt%longname, &
458 export_pkg%mf6_input%subcomponent_name, &
459 idt%tagname, export_pkg%mf6_input%mempath, &
460 iaux=iaux, &
461 component_type=idt%component_type, &
462 subcomponent_type=idt%subcomponent_type)
463
464 ! create the netcdf timeseries variable
465 select case (idt%datatype)
466 case ('DOUBLE1D', 'DOUBLE2D')
467 if (idt%shape == 'NCPL' .or. &
468 idt%shape == 'NAUX NCPL') then
469 call nf_verify(nf90_def_var(this%ncid, varname, nf90_double, &
470 (/this%dim_ids%x, &
471 this%dim_ids%y, &
472 this%dim_ids%time/), varid), &
473 this%nc_fname)
474 else
475 call nf_verify(nf90_def_var(this%ncid, varname, nf90_double, &
476 (/this%dim_ids%x, &
477 this%dim_ids%y, &
478 this%dim_ids%layer, &
479 this%dim_ids%time/), varid), &
480 this%nc_fname)
481 end if
482 call nf_verify(nf90_put_att(this%ncid, varid, &
483 '_FillValue', (/dnodata/)), &
484 this%nc_fname)
485 ! input in effect over the stress period (CF-1.13 7.3)
486 call nf_verify(nf90_put_att(this%ncid, varid, 'cell_methods', &
487 'time: mean'), this%nc_fname)
488 case ('INTEGER1D')
489 if (idt%shape == 'NCPL' .or. &
490 idt%shape == 'NAUX NCPL') then
491 call nf_verify(nf90_def_var(this%ncid, varname, nf90_int, &
492 (/this%dim_ids%x, &
493 this%dim_ids%y, &
494 this%dim_ids%time/), varid), &
495 this%nc_fname)
496 else
497 call nf_verify(nf90_def_var(this%ncid, varname, nf90_int, &
498 (/this%dim_ids%x, &
499 this%dim_ids%y, &
500 this%dim_ids%layer, &
501 this%dim_ids%time/), varid), &
502 this%nc_fname)
503 end if
504 call nf_verify(nf90_put_att(this%ncid, varid, &
505 '_FillValue', (/nf90_fill_int/)), &
506 this%nc_fname)
507 end select
508
509 ! apply chunking parameters
510 if (this%chunking_active) then
511 call nf_verify(nf90_def_var_chunking(this%ncid, &
512 varid, &
513 nf90_chunked, &
514 (/this%chunk_x, this%chunk_y, &
515 this%chunk_z, this%chunk_time/)), &
516 this%nc_fname)
517 end if
518
519 ! deflate and shuffle
520 call ncvar_deflate(this%ncid, varid, this%deflate, &
521 this%shuffle, this%nc_fname)
522
523 ! variable attributes
524 call nf_verify(nf90_put_att(this%ncid, varid, &
525 'long_name', longname), this%nc_fname)
526
527 ! add grid mapping and mf6 attr
528 call ncvar_gridmap(this%ncid, varid, this%gridmap_name, this%latlon, &
529 has_layer, this%nc_fname)
530 call ncvar_mf6attr(this%ncid, varid, iaux, nc_tag, this%nc_fname)
531
532 ! store variable id
533 if (idt%tagname == 'AUX') then
534 export_pkg%varids_aux(iaux, 1) = varid
535 else
536 export_pkg%varids_param(iparam, 1) = varid
537 end if
538 end subroutine create_timeseries
539
540 !> @brief write package gridded input data
541 !<
542 subroutine export_input_arrays(this, pkgtype, pkgname, mempath, param_dfns)
544 class(disncstructuredtype), intent(inout) :: this
545 character(len=*), intent(in) :: pkgtype
546 character(len=*), intent(in) :: pkgname
547 character(len=*), intent(in) :: mempath
548 type(inputparamdefinitiontype), dimension(:), pointer, &
549 intent(in) :: param_dfns
550 type(inputparamdefinitiontype), pointer :: idt
551 integer(I4B) :: iparam, isize
552 do iparam = 1, size(param_dfns)
553 ! assign param definition pointer
554 idt => param_dfns(iparam)
555 ! for now only griddata is exported
556 if (idt%blockname == 'GRIDDATA') then
557 ! check if variable is already allocated
558 call get_isize(idt%mf6varname, mempath, isize)
559 if (isize > 0) then
560 call this%export_input_array(pkgtype, pkgname, mempath, idt)
561 end if
562 end if
563 end do
564 end subroutine export_input_arrays
565
566 !> @brief netcdf export package dynamic input
567 !<
568 subroutine package_step(this, export_pkg)
569 use tdismodule, only: totim, kper, perlen
572 class(disncstructuredtype), intent(inout) :: this
573 class(exportpackagetype), pointer, intent(in) :: export_pkg
574 type(inputparamdefinitiontype), pointer :: idt
575 integer(I4B), dimension(:), pointer, contiguous :: int1d
576 real(DP), dimension(:), pointer, contiguous :: dbl1d, nodes
577 real(DP), dimension(:, :), pointer, contiguous :: dbl2d
578 character(len=LINELENGTH) :: nc_tag
579 integer(I4B) :: iaux, iparam, nvals, n
580
581 ! initialize
582 iaux = 0
583
584 ! update input in effect for this period
585 call export_pkg%update_state()
586
587 ! export period input in effect
588 do iparam = 1, export_pkg%nparam
589 ! check if variable has input in effect
590 if (.not. export_pkg%param_state(iparam)) cycle
591
592 ! set input definition
593 idt => &
594 get_param_definition_type(export_pkg%mf6_input%param_dfns, &
595 export_pkg%mf6_input%component_type, &
596 export_pkg%mf6_input%subcomponent_type, &
597 'PERIOD', export_pkg%param_names(iparam), '')
598
599 ! set variable input tag
600 nc_tag = this%input_attribute(export_pkg%mf6_input%subcomponent_name, &
601 idt)
602
603 ! export arrays
604 select case (idt%datatype)
605 case ('INTEGER1D')
606 call mem_setptr(int1d, idt%mf6varname, export_pkg%mf6_input%mempath)
607 this%var_ids%export = export_pkg%varids_param(iparam, 1)
608 call nc_export_array(int1d, this%ncid, this%dim_ids, this%var_ids, &
609 this%dis, idt, export_pkg%mf6_input%mempath, &
610 nc_tag, export_pkg%mf6_input%subcomponent_name, &
611 this%gridmap_name, this%latlon, this%deflate, &
612 this%shuffle, this%chunk_z, this%chunk_y, &
613 this%chunk_x, kper, this%nc_fname)
614 case ('DOUBLE1D')
615 call mem_setptr(dbl1d, idt%mf6varname, export_pkg%mf6_input%mempath)
616 this%var_ids%export = export_pkg%varids_param(iparam, 1)
617 select case (idt%shape)
618 case ('NCPL')
619 call nc_export_array(dbl1d, this%ncid, this%dim_ids, this%var_ids, &
620 this%dis, idt, export_pkg%mf6_input%mempath, &
621 nc_tag, export_pkg%mf6_input%subcomponent_name, &
622 this%gridmap_name, this%latlon, this%deflate, &
623 this%shuffle, this%chunk_z, this%chunk_y, &
624 this%chunk_x, kper, iaux, this%nc_fname)
625 case ('NODES')
626 nvals = this%dis%nodesuser
627 allocate (nodes(nvals))
628 nodes = dnodata
629 call mem_setptr(dbl1d, idt%mf6varname, export_pkg%mf6_input%mempath)
630 call export_pkg%user_nodes(dbl1d, nodes)
631 call nc_export_array(nodes, this%ncid, this%dim_ids, this%var_ids, &
632 this%dis, idt, export_pkg%mf6_input%mempath, &
633 nc_tag, export_pkg%mf6_input%subcomponent_name, &
634 this%gridmap_name, this%latlon, this%deflate, &
635 this%shuffle, this%chunk_z, this%chunk_y, &
636 this%chunk_x, kper, iaux, this%nc_fname)
637 deallocate (nodes)
638 case default
639 end select
640 case ('DOUBLE2D')
641 call mem_setptr(dbl2d, idt%mf6varname, export_pkg%mf6_input%mempath)
642 select case (idt%shape)
643 case ('NAUX NCPL')
644 nvals = this%dis%nrow * this%dis%ncol
645 allocate (nodes(nvals))
646 do iaux = 1, size(dbl2d, dim=1) !naux
647 if (.not. export_pkg%aux_state(iaux)) cycle
648 this%var_ids%export = export_pkg%varids_aux(iaux, 1)
649 do n = 1, nvals
650 nodes(n) = dbl2d(iaux, n)
651 end do
652 call nc_export_array(nodes, this%ncid, this%dim_ids, this%var_ids, &
653 this%dis, idt, export_pkg%mf6_input%mempath, &
654 nc_tag, export_pkg%mf6_input%subcomponent_name, &
655 this%gridmap_name, this%latlon, this%deflate, &
656 this%shuffle, this%chunk_z, this%chunk_y, &
657 this%chunk_x, kper, iaux, this%nc_fname)
658 end do
659 deallocate (nodes)
660 case ('NAUX NODES')
661 nvals = this%dis%nodesuser
662 allocate (nodes(nvals))
663 do iaux = 1, size(dbl2d, dim=1) ! naux
664 if (.not. export_pkg%aux_state(iaux)) cycle
665 nodes = dnodata
666 this%var_ids%export = export_pkg%varids_aux(iaux, 1)
667 call export_pkg%user_nodes(dbl2d(iaux, :), nodes)
668 call nc_export_array(nodes, this%ncid, this%dim_ids, this%var_ids, &
669 this%dis, idt, export_pkg%mf6_input%mempath, &
670 nc_tag, export_pkg%mf6_input%subcomponent_name, &
671 this%gridmap_name, this%latlon, this%deflate, &
672 this%shuffle, this%chunk_z, this%chunk_y, &
673 this%chunk_x, kper, iaux, this%nc_fname)
674
675 end do
676 deallocate (nodes)
677 case default
678 end select
679 case default
680 ! no-op, no other datatypes exported
681 end select
682 end do
683
684 ! write to time coordinate variable
685 call nf_verify(nf90_put_var(this%ncid, this%var_ids%time, &
686 totim, start=(/kper/)), &
687 this%nc_fname)
688
689 ! write stress period time bounds; the period start is also the end
690 ! of the previous period
691 call nf_verify(nf90_put_var(this%ncid, this%var_ids%time_bnds, &
692 (/totim, totim + perlen(kper)/), &
693 start=(/1, kper/), count=(/2, 1/)), &
694 this%nc_fname)
695 if (kper > 1) then
696 call nf_verify(nf90_put_var(this%ncid, this%var_ids%time_bnds, &
697 totim, start=(/2, kper - 1/)), &
698 this%nc_fname)
699 end if
700
701 ! synchronize file
702 call nf_verify(nf90_sync(this%ncid), this%nc_fname)
703 end subroutine package_step
704
705 !> @brief determine packages to write gridded input
706 !<
707 subroutine add_pkg_data(this)
714 class(disncstructuredtype), intent(inout) :: this
715 character(LENCOMPONENTNAME) :: ptype, pname, pkgtype
716 type(characterstringtype), dimension(:), contiguous, &
717 pointer :: pkgtypes => null()
718 type(characterstringtype), dimension(:), contiguous, &
719 pointer :: pkgnames => null()
720 type(characterstringtype), dimension(:), contiguous, &
721 pointer :: mempaths => null()
722 type(inputparamdefinitiontype), dimension(:), pointer :: param_dfns
723 character(len=LENMEMPATH) :: input_mempath, mempath
724 integer(I4B) :: n
725 integer(I4B), pointer :: export_arrays
726 logical(LGP) :: found
727
728 input_mempath = create_mem_path(component=this%modelname, context=idm_context)
729
730 ! set pointers to model path package info
731 call mem_setptr(pkgtypes, 'PKGTYPES', input_mempath)
732 call mem_setptr(pkgnames, 'PKGNAMES', input_mempath)
733 call mem_setptr(mempaths, 'MEMPATHS', input_mempath)
734
735 do n = 1, size(mempaths)
736 ! allocate export_arrays
737 allocate (export_arrays)
738 export_arrays = 0
739
740 ! set package attributes
741 mempath = mempaths(n)
742 pname = pkgnames(n)
743 ptype = pkgtypes(n)
744
745 ! export input arrays
746 if (mempath /= '') then
747 ! update export
748 call mem_set_value(export_arrays, 'EXPORT_NC', mempath, found, &
749 release=.false.)
750
751 if (export_arrays > 0) then
752 pkgtype = idm_subcomponent_type(this%modeltype, ptype)
753 ! base packages are named by type, as in the input context
754 pname = idm_subcomponent_name(this%modeltype, pkgtype, pname)
755 param_dfns => param_definitions(this%modeltype, pkgtype)
756 call this%export_input_arrays(ptype, pname, mempath, param_dfns)
757 end if
758 end if
759
760 ! cleanup
761 deallocate (export_arrays)
762 end do
763 end subroutine add_pkg_data
764
765 !> @brief create file (group) attributes
766 !<
767 subroutine add_global_att(this)
768 class(disncstructuredtype), intent(inout) :: this
769 ! file scoped title
770 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'title', &
771 this%annotation%title), this%nc_fname)
772 ! source (MODFLOW 6)
773 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'source', &
774 this%annotation%source), this%nc_fname)
775 ! grid type (MODFLOW 6)
776 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'modflow_grid', &
777 this%annotation%grid), this%nc_fname)
778 ! MODFLOW 6 model type
779 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'modflow_model', &
780 this%annotation%model), this%nc_fname)
781 ! generation datetime
782 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'history', &
783 this%annotation%history), this%nc_fname)
784 ! supported conventions
785 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'Conventions', &
786 this%annotation%conventions), &
787 this%nc_fname)
788 end subroutine add_global_att
789
790 !> @brief netcdf export define dimensions
791 !<
792 subroutine define_dim(this)
793 use constantsmodule, only: mvalidate
795 class(disncstructuredtype), intent(inout) :: this
796
797 ! bound dim
798 call nf_verify(nf90_def_dim(this%ncid, 'bnd', 2, this%dim_ids%bnd), &
799 this%nc_fname)
800
801 if (isim_mode /= mvalidate .or. this%pkglist%Count() > 0) then
802 ! Time
803 call nf_verify(nf90_def_dim(this%ncid, 'time', this%totnstp, &
804 this%dim_ids%time), this%nc_fname)
805 call nf_verify(nf90_def_var(this%ncid, 'time', nf90_double, &
806 this%dim_ids%time, this%var_ids%time), &
807 this%nc_fname)
808 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'calendar', &
809 'standard'), this%nc_fname)
810 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'units', &
811 this%datetime), this%nc_fname)
812 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'axis', 'T'), &
813 this%nc_fname)
814 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'standard_name', &
815 'time'), this%nc_fname)
816 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'long_name', &
817 'time'), this%nc_fname)
818 ! stress period bounds of the time coordinate (CF-1.13 7.1)
819 if (isim_mode == mvalidate) then
820 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'bounds', &
821 'time_bnds'), this%nc_fname)
822 call nf_verify(nf90_def_var(this%ncid, 'time_bnds', nf90_double, &
823 (/this%dim_ids%bnd, this%dim_ids%time/), &
824 this%var_ids%time_bnds), this%nc_fname)
825 end if
826 end if
827
828 ! Z dimension
829 call nf_verify(nf90_def_dim(this%ncid, 'layer', this%dis%nlay, &
830 this%dim_ids%layer), this%nc_fname)
831 call nf_verify(nf90_def_var(this%ncid, 'layer', nf90_int, &
832 this%dim_ids%layer, this%var_ids%layer), &
833 this%nc_fname)
834 call nf_verify(nf90_put_att(this%ncid, this%var_ids%layer, 'units', '1'), &
835 this%nc_fname)
836 call nf_verify(nf90_put_att(this%ncid, this%var_ids%layer, 'axis', 'Z'), &
837 this%nc_fname)
838 call nf_verify(nf90_put_att(this%ncid, this%var_ids%layer, 'positive', &
839 'down'), this%nc_fname)
840 call nf_verify(nf90_put_att(this%ncid, this%var_ids%layer, 'long_name', &
841 'model layer'), this%nc_fname)
842
843 ! Y dimension
844 call nf_verify(nf90_def_dim(this%ncid, 'y', this%dis%nrow, this%dim_ids%y), &
845 this%nc_fname)
846 call nf_verify(nf90_def_var(this%ncid, 'y', nf90_double, this%dim_ids%y, &
847 this%var_ids%y), this%nc_fname)
848 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'units', &
849 this%lenunits), this%nc_fname)
850 if (this%dis%angrot == dzero .or. &
851 wrap_rotated_crs(this%crs_wkt, this%dis%xorigin, this%dis%yorigin, &
852 this%dis%angrot) /= '') then
853 ! axis/standard_name assert that y holds the true projected
854 ! position. For a rotated grid this only holds once crs_wkt
855 ! encodes the rotation (see wrap_rotated_crs in define_gridmap);
856 ! otherwise y is grid-local (see add_grid_data) and asserting it
857 ! would cause GDAL's netCDF driver to derive an incorrect,
858 ! unrotated position directly from y's grid-local values.
859 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'axis', 'Y'), &
860 this%nc_fname)
861 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'standard_name', &
862 'projection_y_coordinate'), this%nc_fname)
863 end if
864 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'long_name', &
865 'Northing'), this%nc_fname)
866 if (this%gridmap_name /= '') then
867 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'grid_mapping', &
868 this%gridmap_name), this%nc_fname)
869 end if
870 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'bounds', 'y_bnds'), &
871 this%nc_fname)
872 call nf_verify(nf90_def_var(this%ncid, 'y_bnds', nf90_double, &
873 (/this%dim_ids%bnd, this%dim_ids%y/), &
874 this%var_ids%y_bnds), this%nc_fname)
875
876 ! X dimension
877 call nf_verify(nf90_def_dim(this%ncid, 'x', this%dis%ncol, this%dim_ids%x), &
878 this%nc_fname)
879 call nf_verify(nf90_def_var(this%ncid, 'x', nf90_double, this%dim_ids%x, &
880 this%var_ids%x), this%nc_fname)
881 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'units', &
882 this%lenunits), this%nc_fname)
883 if (this%dis%angrot == dzero .or. &
884 wrap_rotated_crs(this%crs_wkt, this%dis%xorigin, this%dis%yorigin, &
885 this%dis%angrot) /= '') then
886 ! see matching comment on the Y dimension above
887 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'axis', 'X'), &
888 this%nc_fname)
889 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'standard_name', &
890 'projection_x_coordinate'), this%nc_fname)
891 end if
892 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'long_name', &
893 'Easting'), this%nc_fname)
894 if (this%gridmap_name /= '') then
895 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'grid_mapping', &
896 this%gridmap_name), this%nc_fname)
897 end if
898 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'bounds', 'x_bnds'), &
899 this%nc_fname)
900 call nf_verify(nf90_def_var(this%ncid, 'x_bnds', nf90_double, &
901 (/this%dim_ids%bnd, this%dim_ids%x/), &
902 this%var_ids%x_bnds), this%nc_fname)
903
904 ! z: cell center elevation auxiliary coordinate -- a real vertical
905 ! position, unlike the discrete layer index. Always written,
906 ! independent of CRS/NCF configuration.
907 call nf_verify(nf90_def_var(this%ncid, 'z', nf90_double, &
908 (/this%dim_ids%x, this%dim_ids%y, &
909 this%dim_ids%layer/), &
910 this%var_ids%elevation), this%nc_fname)
911 call ncvar_chunk3d(this%ncid, this%var_ids%elevation, this%chunk_x, &
912 this%chunk_y, this%chunk_z, this%nc_fname)
913 call ncvar_deflate(this%ncid, this%var_ids%elevation, this%deflate, &
914 this%shuffle, this%nc_fname)
915 call nf_verify(nf90_put_att(this%ncid, this%var_ids%elevation, 'units', &
916 this%lenunits), this%nc_fname)
917 call nf_verify(nf90_put_att(this%ncid, this%var_ids%elevation, &
918 'standard_name', 'altitude'), this%nc_fname)
919 call nf_verify(nf90_put_att(this%ncid, this%var_ids%elevation, &
920 'positive', 'up'), this%nc_fname)
921 call nf_verify(nf90_put_att(this%ncid, this%var_ids%elevation, &
922 'long_name', 'cell center elevation'), &
923 this%nc_fname)
924 end subroutine define_dim
925
926 !> @brief create the model layer dependent variables
927 !<
928 subroutine define_dependent(this)
929 use constantsmodule, only: dhnoflo
930 class(disncstructuredtype), intent(inout) :: this
931
932 call nf_verify(nf90_def_var(this%ncid, this%xname, nf90_double, &
933 (/this%dim_ids%x, this%dim_ids%y, &
934 this%dim_ids%layer, this%dim_ids%time/), &
935 this%var_ids%dependent), &
936 this%nc_fname)
937
938 ! apply chunking parameters
939 if (this%chunking_active) then
940 call nf_verify(nf90_def_var_chunking(this%ncid, &
941 this%var_ids%dependent, &
942 nf90_chunked, &
943 (/this%chunk_x, this%chunk_y, &
944 this%chunk_z, this%chunk_time/)), &
945 this%nc_fname)
946 end if
947
948 ! deflate and shuffle
949 call ncvar_deflate(this%ncid, this%var_ids%dependent, this%deflate, &
950 this%shuffle, this%nc_fname)
951
952 ! put attr; head is a length, other dependent variables have no
953 ! units known from the input
954 if (this%modeltype == 'GWF') then
955 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent, &
956 'units', this%lenunits), this%nc_fname)
957 end if
958 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent, 'long_name', &
959 this%annotation%longname), this%nc_fname)
960 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent, '_FillValue', &
961 (/dhnoflo/)), this%nc_fname)
962 ! cell_methods (CF-1.13 7.3): dependent variable values are the
963 ! instantaneous simulated state at each output time, not a time
964 ! mean/accumulation over the interval -- an intensive quantity whose
965 ! default method is already "point" per CF's own Appendix E, stated
966 ! explicitly here per CF's recommendation to do so for every
967 ! spatio-temporal dimension.
968 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent, &
969 'cell_methods', 'time: point'), this%nc_fname)
970
971 ! add grid mapping
972 call ncvar_gridmap(this%ncid, this%var_ids%dependent, this%gridmap_name, &
973 this%latlon, .true., this%nc_fname)
974 end subroutine define_dependent
975
976 !> @brief create the file grid mapping container variable
977 !<
978 subroutine define_gridmap(this)
979 class(disncstructuredtype), intent(inout) :: this
980 integer(I4B) :: var_id
981 character(len=LINELENGTH) :: gmname
982 character(len=LENBIGLINE) :: effective_crs_wkt, derived_wkt
983 character(len=100) :: rotinfo
984 real(DP) :: longitude_of_central_meridian, latitude_of_projection_origin
985 real(DP) :: scale_factor_at_central_meridian, false_easting, false_northing
986 real(DP) :: semi_major_axis, inverse_flattening
987 real(DP) :: standard_parallel_1, standard_parallel_2
988 logical(LGP) :: params_found, is_2sp
989
990 if (this%wkt /= '' .or. this%crs_wkt /= '') then
991 call nf_verify(nf90_redef(this%ncid), this%nc_fname)
992 call nf_verify(nf90_def_var(this%ncid, this%gridmap_name, nf90_int, &
993 var_id), this%nc_fname)
994 ! wkt (WKT1, OGC 01-009) -- for legacy/QGIS compatibility. WKT1
995 ! has no derived-CRS syntax, so it cannot encode grid rotation and
996 ! is always written unmodified.
997 if (this%wkt /= '') then
998 call nf_verify(nf90_put_att(this%ncid, var_id, 'wkt', this%wkt), &
999 this%nc_fname)
1000 end if
1001 ! crs_wkt (WKT2, ISO 19162:2019) -- required by CF-1.13
1002 if (this%crs_wkt /= '') then
1003 effective_crs_wkt = this%crs_wkt
1004 else
1005 effective_crs_wkt = this%wkt
1006 end if
1007 ! For a rotated structured grid, wrap crs_wkt in a derived CRS
1008 ! encoding the rotation (see wrap_rotated_crs) so a CRS-aware
1009 ! consumer resolves the correct real-world position and shape
1010 ! via ordinary reprojection. Falls back to an unmodified
1011 ! crs_wkt, with a warning, if CRS_WKT was not provided or is not
1012 ! a recognized WKT2 PROJCRS.
1013 if (this%dis%angrot /= dzero) then
1014 if (this%crs_wkt == '') then
1015 write (warnmsg, '(a)') 'Structured rotated grid with no CRS_WKT &
1016 &input: no rotation-positioning information will be written &
1017 &to the output CRS; x/y coordinate values will not reflect &
1018 &the true rotated position. Applies to file "'// &
1019 trim(this%nc_fname)//'".'
1020 call store_warning(warnmsg)
1021 else
1022 derived_wkt = wrap_rotated_crs(this%crs_wkt, this%dis%xorigin, &
1023 this%dis%yorigin, this%dis%angrot)
1024 if (derived_wkt /= '') then
1025 write (rotinfo, '(a,g0,a,g0,a,g0,a)') 'xorigin=', &
1026 this%dis%xorigin, ', yorigin=', this%dis%yorigin, &
1027 ', angrot=', this%dis%angrot, ' deg'
1028 write (warnmsg, '(a)') 'Structured rotated grid: wrapped &
1029 &CRS_WKT in a derived CRS to encode grid rotation ('// &
1030 trim(rotinfo)//'). Applies to file "'// &
1031 trim(this%nc_fname)//'".'
1032 call store_warning(warnmsg)
1033 effective_crs_wkt = derived_wkt
1034 else
1035 write (warnmsg, '(a)') 'Structured rotated grid: CRS_WKT is &
1036 &not a recognized WKT2 PROJCRS; no rotation-positioning &
1037 &information will be written to the output CRS; x/y &
1038 &coordinate values will not reflect the true rotated &
1039 &position. Applies to file "'//trim(this%nc_fname)//'".'
1040 call store_warning(warnmsg)
1041 end if
1042 end if
1043 end if
1044 call nf_verify(nf90_put_att(this%ncid, var_id, 'crs_wkt', &
1045 trim(effective_crs_wkt)), this%nc_fname)
1046 ! grid_mapping_name derived from WKT projection keyword
1047 gmname = wkt_to_cf_gridmapping(trim(effective_crs_wkt))
1048 if (gmname /= '') then
1049 call nf_verify(nf90_put_att(this%ncid, var_id, 'grid_mapping_name', &
1050 trim(gmname)), this%nc_fname)
1051 end if
1052 ! CF-standard numeric grid_mapping parameters (in addition to
1053 ! wkt/crs_wkt): some netCDF readers -- notably ArcGIS's classic
1054 ! netCDF connector -- position a projected grid using these
1055 ! individual attributes rather than parsing wkt/crs_wkt, and
1056 ! default every parameter to 0 if they are absent. Covers
1057 ! transverse_mercator, lambert_conformal_conic (2SP only -- CF has
1058 ! no scale_factor attribute for this method, so a 1SP/scale-factor
1059 ! WKT cannot be exactly represented), and albers_conical_equal_area;
1060 ! other grid_mapping_name values are unaffected. Best-effort
1061 ! extraction from effective_crs_wkt -- MF6 has no CRS parsing
1062 ! library, so this is skipped if any parameter cannot be located.
1063 if (trim(gmname) == 'transverse_mercator') then
1064 call wkt_transverse_mercator_params(trim(effective_crs_wkt), &
1065 longitude_of_central_meridian, &
1066 latitude_of_projection_origin, &
1067 scale_factor_at_central_meridian, &
1068 false_easting, false_northing, &
1069 semi_major_axis, inverse_flattening, &
1070 params_found)
1071 if (params_found) then
1072 call nf_verify(nf90_put_att(this%ncid, var_id, &
1073 'longitude_of_central_meridian', &
1074 longitude_of_central_meridian), &
1075 this%nc_fname)
1076 call nf_verify(nf90_put_att(this%ncid, var_id, &
1077 'latitude_of_projection_origin', &
1078 latitude_of_projection_origin), &
1079 this%nc_fname)
1080 call nf_verify(nf90_put_att(this%ncid, var_id, &
1081 'scale_factor_at_central_meridian', &
1082 scale_factor_at_central_meridian), &
1083 this%nc_fname)
1084 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_easting', &
1085 false_easting), this%nc_fname)
1086 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_northing', &
1087 false_northing), this%nc_fname)
1088 call nf_verify(nf90_put_att(this%ncid, var_id, 'semi_major_axis', &
1089 semi_major_axis), this%nc_fname)
1090 call nf_verify(nf90_put_att(this%ncid, var_id, 'inverse_flattening', &
1091 inverse_flattening), this%nc_fname)
1092 else
1093 write (warnmsg, '(a)') 'Structured grid: unable to extract CF &
1094 &transverse_mercator grid_mapping parameters from CRS_WKT; &
1095 &some netCDF readers may not correctly position this grid. &
1096 &Applies to file "'//trim(this%nc_fname)//'".'
1097 call store_warning(warnmsg)
1098 end if
1099 else if (trim(gmname) == 'lambert_conformal_conic') then
1100 call wkt_lambert_conformal_conic_params(trim(effective_crs_wkt), &
1101 standard_parallel_1, &
1102 standard_parallel_2, &
1103 longitude_of_central_meridian, &
1104 latitude_of_projection_origin, &
1105 false_easting, false_northing, &
1106 semi_major_axis, &
1107 inverse_flattening, is_2sp, &
1108 params_found)
1109 if (params_found) then
1110 call nf_verify(nf90_put_att(this%ncid, var_id, 'standard_parallel', &
1111 (/standard_parallel_1, &
1112 standard_parallel_2/)), this%nc_fname)
1113 call nf_verify(nf90_put_att(this%ncid, var_id, &
1114 'longitude_of_central_meridian', &
1115 longitude_of_central_meridian), &
1116 this%nc_fname)
1117 call nf_verify(nf90_put_att(this%ncid, var_id, &
1118 'latitude_of_projection_origin', &
1119 latitude_of_projection_origin), &
1120 this%nc_fname)
1121 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_easting', &
1122 false_easting), this%nc_fname)
1123 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_northing', &
1124 false_northing), this%nc_fname)
1125 call nf_verify(nf90_put_att(this%ncid, var_id, 'semi_major_axis', &
1126 semi_major_axis), this%nc_fname)
1127 call nf_verify(nf90_put_att(this%ncid, var_id, 'inverse_flattening', &
1128 inverse_flattening), this%nc_fname)
1129 else if (.not. is_2sp) then
1130 write (warnmsg, '(a)') 'Structured grid: 1SP lambert_conformal_conic &
1131 &variants are not supported for CF grid_mapping parameter &
1132 &extraction (CF has no scale_factor attribute for this method); &
1133 &some netCDF readers may not correctly position this grid. &
1134 &Applies to file "'//trim(this%nc_fname)//'".'
1135 call store_warning(warnmsg)
1136 else
1137 write (warnmsg, '(a)') 'Structured grid: unable to extract CF &
1138 &lambert_conformal_conic grid_mapping parameters from CRS_WKT; &
1139 &some netCDF readers may not correctly position this grid. &
1140 &Applies to file "'//trim(this%nc_fname)//'".'
1141 call store_warning(warnmsg)
1142 end if
1143 else if (trim(gmname) == 'albers_conical_equal_area') then
1144 call wkt_albers_conical_equal_area_params(trim(effective_crs_wkt), &
1145 standard_parallel_1, &
1146 standard_parallel_2, &
1147 longitude_of_central_meridian, &
1148 latitude_of_projection_origin, &
1149 false_easting, false_northing, &
1150 semi_major_axis, &
1151 inverse_flattening, &
1152 params_found)
1153 if (params_found) then
1154 call nf_verify(nf90_put_att(this%ncid, var_id, 'standard_parallel', &
1155 (/standard_parallel_1, &
1156 standard_parallel_2/)), this%nc_fname)
1157 call nf_verify(nf90_put_att(this%ncid, var_id, &
1158 'longitude_of_central_meridian', &
1159 longitude_of_central_meridian), &
1160 this%nc_fname)
1161 call nf_verify(nf90_put_att(this%ncid, var_id, &
1162 'latitude_of_projection_origin', &
1163 latitude_of_projection_origin), &
1164 this%nc_fname)
1165 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_easting', &
1166 false_easting), this%nc_fname)
1167 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_northing', &
1168 false_northing), this%nc_fname)
1169 call nf_verify(nf90_put_att(this%ncid, var_id, 'semi_major_axis', &
1170 semi_major_axis), this%nc_fname)
1171 call nf_verify(nf90_put_att(this%ncid, var_id, 'inverse_flattening', &
1172 inverse_flattening), this%nc_fname)
1173 else
1174 write (warnmsg, '(a)') 'Structured grid: unable to extract CF &
1175 &albers_conical_equal_area grid_mapping parameters from &
1176 &CRS_WKT; some netCDF readers may not correctly position this &
1177 &grid. Applies to file "'//trim(this%nc_fname)//'".'
1178 call store_warning(warnmsg)
1179 end if
1180 end if
1181 ! GeoTransform/spatial_ref intentionally not written: a GDAL-only,
1182 ! non-CF extension ArcGIS does not read, superseded by crs_wkt's
1183 ! derived-CRS encoding above, which any CRS-aware consumer resolves
1184 ! via ordinary reprojection.
1185 call nf_verify(nf90_enddef(this%ncid), this%nc_fname)
1186 call nf_verify(nf90_put_var(this%ncid, var_id, 1), &
1187 this%nc_fname)
1188 end if
1189 end subroutine define_gridmap
1190
1191 !> @brief define grid projection variables
1192 !<
1193 subroutine define_geocoords(this)
1194 class(disncstructuredtype), intent(inout) :: this
1195 if (this%latlon) then
1196 ! lat
1197 call nf_verify(nf90_def_var(this%ncid, 'lat', nf90_double, &
1198 (/this%dim_ids%x, this%dim_ids%y/), &
1199 this%var_ids%latitude), this%nc_fname)
1200 call nf_verify(nf90_put_att(this%ncid, this%var_ids%latitude, &
1201 'units', 'degrees_north'), this%nc_fname)
1202 call nf_verify(nf90_put_att(this%ncid, this%var_ids%latitude, &
1203 'standard_name', 'latitude'), this%nc_fname)
1204 call nf_verify(nf90_put_att(this%ncid, this%var_ids%latitude, &
1205 'long_name', 'latitude'), this%nc_fname)
1206
1207 ! lon
1208 call nf_verify(nf90_def_var(this%ncid, 'lon', nf90_double, &
1209 (/this%dim_ids%x, this%dim_ids%y/), &
1210 this%var_ids%longitude), this%nc_fname)
1211 call nf_verify(nf90_put_att(this%ncid, this%var_ids%longitude, &
1212 'units', 'degrees_east'), this%nc_fname)
1213 call nf_verify(nf90_put_att(this%ncid, this%var_ids%longitude, &
1214 'standard_name', 'longitude'), this%nc_fname)
1215 call nf_verify(nf90_put_att(this%ncid, this%var_ids%longitude, &
1216 'long_name', 'longitude'), this%nc_fname)
1217 end if
1218 end subroutine define_geocoords
1219
1220 !> @brief add grid projection data
1221 !<
1222 subroutine add_proj_data(this)
1223 class(disncstructuredtype), intent(inout) :: this
1224 if (this%latlon) then
1225 ! lat
1226 call nf_verify(nf90_put_var(this%ncid, this%var_ids%latitude, &
1227 this%latitude, start=(/1, 1/), &
1228 count=(/this%dis%ncol, this%dis%nrow/)), &
1229 this%nc_fname)
1230
1231 ! lon
1232 call nf_verify(nf90_put_var(this%ncid, this%var_ids%longitude, &
1233 this%longitude, start=(/1, 1/), &
1234 count=(/this%dis%ncol, this%dis%nrow/)), &
1235 this%nc_fname)
1236 end if
1237 end subroutine add_proj_data
1238
1239 !> @brief add grid coordinates
1240 !<
1241 subroutine add_grid_data(this)
1242 class(disncstructuredtype), intent(inout) :: this
1243 integer(I4B) :: ibnd, n, k
1244 real(DP), dimension(:, :), pointer, contiguous :: dbl2d
1245 real(DP), dimension(:), allocatable :: x, y
1246 real(DP), dimension(:, :, :), allocatable :: elev3d
1247 real(DP) :: xoff, yoff
1248
1249 if (this%dis%angrot /= dzero) then
1250 xoff = dzero
1251 yoff = dzero
1252 else
1253 xoff = this%dis%xorigin
1254 yoff = this%dis%yorigin
1255 end if
1256
1257 allocate (x(size(this%dis%cellx)))
1258 allocate (y(size(this%dis%celly)))
1259
1260 do n = 1, size(this%dis%cellx)
1261 x(n) = this%dis%cellx(n) + xoff
1262 end do
1263
1264 do n = 1, size(this%dis%celly)
1265 y(n) = this%dis%celly(n) + yoff
1266 end do
1267
1268 call nf_verify(nf90_put_var(this%ncid, this%var_ids%x, x), &
1269 this%nc_fname)
1270 call nf_verify(nf90_put_var(this%ncid, this%var_ids%y, y), &
1271 this%nc_fname)
1272 ! TODO see cf-conventions 4.3.3. Parametric Vertical Coordinate
1273 call nf_verify(nf90_put_var(this%ncid, this%var_ids%layer, this%layers), &
1274 this%nc_fname)
1275
1276 deallocate (x)
1277 deallocate (y)
1278
1279 ! z: cell center elevation, layer 1 = midpoint of top/bot3d(1),
1280 ! layer k>1 = midpoint of bot3d(k-1)/bot3d(k)
1281 allocate (elev3d(this%dis%ncol, this%dis%nrow, this%nlay))
1282 do k = 1, this%nlay
1283 if (k == 1) then
1284 elev3d(:, :, k) = (this%dis%top2d(:, :) + this%dis%bot3d(:, :, k)) * &
1285 dhalf
1286 else
1287 elev3d(:, :, k) = (this%dis%bot3d(:, :, k - 1) + &
1288 this%dis%bot3d(:, :, k)) * dhalf
1289 end if
1290 end do
1291 call nf_verify(nf90_put_var(this%ncid, this%var_ids%elevation, elev3d), &
1292 this%nc_fname)
1293 deallocate (elev3d)
1294
1295 ! bounds x
1296 allocate (dbl2d(2, size(this%dis%cellx)))
1297 ibnd = 1
1298 do n = 1, size(this%dis%cellx)
1299 if (ibnd == 1) then
1300 dbl2d(1, ibnd) = xoff
1301 dbl2d(2, ibnd) = xoff + this%dis%delr(ibnd)
1302 else
1303 dbl2d(1, ibnd) = dbl2d(1, ibnd - 1) + this%dis%delr(ibnd)
1304 dbl2d(2, ibnd) = dbl2d(2, ibnd - 1) + this%dis%delr(ibnd)
1305 end if
1306 ibnd = ibnd + 1
1307 end do
1308 call nf_verify(nf90_put_var(this%ncid, this%var_ids%x_bnds, dbl2d), &
1309 this%nc_fname)
1310 deallocate (dbl2d)
1311
1312 ! bounds y
1313 allocate (dbl2d(2, size(this%dis%celly)))
1314 ibnd = 1
1315 do n = size(this%dis%celly), 1, -1
1316 if (ibnd == 1) then
1317 dbl2d(1, ibnd) = yoff + sum(this%dis%delc) - this%dis%delc(n)
1318 dbl2d(2, ibnd) = yoff + sum(this%dis%delc)
1319 else
1320 dbl2d(1, ibnd) = dbl2d(1, ibnd - 1) - this%dis%delc(n)
1321 dbl2d(2, ibnd) = dbl2d(2, ibnd - 1) - this%dis%delc(n)
1322 end if
1323 ibnd = ibnd + 1
1324 end do
1325 call nf_verify(nf90_put_var(this%ncid, this%var_ids%y_bnds, dbl2d), &
1326 this%nc_fname)
1327 deallocate (dbl2d)
1328 end subroutine add_grid_data
1329
1330 !> @brief define 2d variable chunking
1331 !<
1332 subroutine ncvar_chunk2d(ncid, varid, chunk_x, chunk_y, nc_fname)
1333 integer(I4B), intent(in) :: ncid
1334 integer(I4B), intent(in) :: varid
1335 integer(I4B), intent(in) :: chunk_x
1336 integer(I4B), intent(in) :: chunk_y
1337 character(len=*), intent(in) :: nc_fname
1338 if (chunk_y > 0 .and. chunk_x > 0) then
1339 call nf_verify(nf90_def_var_chunking(ncid, varid, nf90_chunked, &
1340 (/chunk_x, chunk_y/)), nc_fname)
1341 end if
1342 end subroutine ncvar_chunk2d
1343
1344 !> @brief define 3d variable chunking
1345 !<
1346 subroutine ncvar_chunk3d(ncid, varid, chunk_x, chunk_y, chunk_z, nc_fname)
1347 integer(I4B), intent(in) :: ncid
1348 integer(I4B), intent(in) :: varid
1349 integer(I4B), intent(in) :: chunk_x
1350 integer(I4B), intent(in) :: chunk_y
1351 integer(I4B), intent(in) :: chunk_z
1352 character(len=*), intent(in) :: nc_fname
1353 if (chunk_z > 0 .and. chunk_y > 0 .and. chunk_x > 0) then
1354 call nf_verify(nf90_def_var_chunking(ncid, varid, nf90_chunked, &
1355 (/chunk_x, chunk_y, chunk_z/)), &
1356 nc_fname)
1357 end if
1358 end subroutine ncvar_chunk3d
1359
1360 !> @brief define variable compression
1361 !<
1362 subroutine ncvar_deflate(ncid, varid, deflate, shuffle, nc_fname)
1363 integer(I4B), intent(in) :: ncid
1364 integer(I4B), intent(in) :: varid
1365 integer(I4B), intent(in) :: deflate
1366 integer(I4B), intent(in) :: shuffle
1367 character(len=*), intent(in) :: nc_fname
1368 ! deflate and shuffle
1369 if (deflate >= 0) then
1370 call nf_verify(nf90_def_var_deflate(ncid, varid, shuffle=shuffle, &
1371 deflate=1, deflate_level=deflate), &
1372 nc_fname)
1373 end if
1374 end subroutine ncvar_deflate
1375
1376 !> @brief put variable gridmap attributes
1377 !!
1378 !! has_layer must be true only for variables carrying the layer, y, and x
1379 !! dimensions -- z(layer, y, x) can only be legally referenced via
1380 !! `coordinates` from a variable whose dimensions are a superset of its
1381 !! own (CF-1.13 5.2). 2D (y, x)-only variables such as dis_top must pass
1382 !! has_layer=.false. z is written unconditionally, independent of
1383 !! gridmap_name/latlon.
1384 !<
1385 subroutine ncvar_gridmap(ncid, varid, gridmap_name, latlon, has_layer, &
1386 nc_fname)
1387 integer(I4B), intent(in) :: ncid
1388 integer(I4B), intent(in) :: varid
1389 character(len=*), intent(in) :: gridmap_name
1390 logical(LGP), intent(in) :: latlon
1391 logical(LGP), intent(in) :: has_layer
1392 character(len=*), intent(in) :: nc_fname
1393 character(len=LINELENGTH) :: coords
1394 coords = ''
1395 ! NOTE: x/y are not listed here even when gridmap_name is set -- unlike
1396 ! lon/lat (real 2D auxiliary coordinates), x/y are true CF dimension
1397 ! coordinates already discoverable by dimension-name matching alone, so
1398 ! listing them in 'coordinates' is redundant (CF-1.13 Ch.5 preamble).
1399 if (latlon) then
1400 coords = 'lon lat'
1401 end if
1402 if (has_layer) then
1403 if (len_trim(coords) > 0) then
1404 coords = trim(coords)//' z'
1405 else
1406 coords = 'z'
1407 end if
1408 end if
1409 if (len_trim(coords) > 0) then
1410 call nf_verify(nf90_put_att(ncid, varid, 'coordinates', trim(coords)), &
1411 nc_fname)
1412 end if
1413 if (gridmap_name /= '') then
1414 call nf_verify(nf90_put_att(ncid, varid, 'grid_mapping', gridmap_name), &
1415 nc_fname)
1416 end if
1417 end subroutine ncvar_gridmap
1418
1419 !> @brief put variable internal modflow6 attributes
1420 !<
1421 subroutine ncvar_mf6attr(ncid, varid, iaux, nc_tag, nc_fname)
1422 integer(I4B), intent(in) :: ncid
1423 integer(I4B), intent(in) :: varid
1424 integer(I4B), intent(in) :: iaux
1425 character(len=*), intent(in) :: nc_tag
1426 character(len=*), intent(in) :: nc_fname
1427 if (nc_tag /= '') then
1428 call nf_verify(nf90_put_att(ncid, varid, 'modflow_input', &
1429 nc_tag), nc_fname)
1430 if (iaux > 0) then
1431 call nf_verify(nf90_put_att(ncid, varid, 'modflow_iaux', &
1432 iaux), nc_fname)
1433 end if
1434 end if
1435 end subroutine ncvar_mf6attr
1436
1437 !> @brief netcdf export 1D integer
1438 !<
1439 subroutine nc_export_int1d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1440 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1441 shuffle, chunk_z, chunk_y, chunk_x, iper, nc_fname)
1442 use tdismodule, only: kper
1443 integer(I4B), dimension(:), pointer, contiguous, intent(in) :: p_mem
1444 integer(I4B), intent(in) :: ncid
1445 type(structuredncdimidtype), intent(inout) :: dim_ids
1446 type(structuredncvaridtype), intent(inout) :: var_ids
1447 type(distype), pointer, intent(in) :: dis
1448 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1449 character(len=*), intent(in) :: mempath
1450 character(len=*), intent(in) :: nc_tag
1451 character(len=*), intent(in) :: pkgname
1452 character(len=*), intent(in) :: gridmap_name
1453 logical(LGP), intent(in) :: latlon
1454 integer(I4B), intent(in) :: deflate
1455 integer(I4B), intent(in) :: shuffle
1456 integer(I4B), intent(in) :: chunk_z
1457 integer(I4B), intent(in) :: chunk_y
1458 integer(I4B), intent(in) :: chunk_x
1459 integer(I4B), intent(in) :: iper
1460 character(len=*), intent(in) :: nc_fname
1461 integer(I4B) :: var_id, axis_sz
1462 character(len=LINELENGTH) :: varname, longname
1463
1464 varname = export_varname(pkgname, idt%tagname, mempath)
1465
1466 if (idt%shape == 'NROW' .or. &
1467 idt%shape == 'NCOL' .or. &
1468 idt%shape == 'NCPL' .or. &
1469 idt%shape == 'NAUX NCPL') then
1470
1471 if (iper == 0) then
1472 select case (idt%shape)
1473 case ('NROW')
1474 axis_sz = dim_ids%y
1475 case ('NCOL')
1476 axis_sz = dim_ids%x
1477 end select
1478
1479 longname = export_longname(idt%longname, pkgname, idt%tagname, mempath, &
1480 component_type=idt%component_type, &
1481 subcomponent_type=idt%subcomponent_type)
1482
1483 ! reenter define mode and create variable
1484 call nf_verify(nf90_redef(ncid), nc_fname)
1485 call nf_verify(nf90_def_var(ncid, varname, nf90_int, &
1486 (/axis_sz/), var_id), &
1487 nc_fname)
1488
1489 ! NROW/NCOL shapes use default chunking
1490 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1491
1492 ! put attr
1493 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1494 (/nf90_fill_int/)), nc_fname)
1495 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1496 longname), nc_fname)
1497
1498 ! add mf6 attr
1499 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1500
1501 ! exit define mode and write data
1502 call nf_verify(nf90_enddef(ncid), nc_fname)
1503 call nf_verify(nf90_put_var(ncid, var_id, p_mem), &
1504 nc_fname)
1505 else
1506 ! timeseries
1507 call nf_verify(nf90_put_var(ncid, &
1508 var_ids%export, p_mem, &
1509 start=(/1, 1, kper/), &
1510 count=(/dis%ncol, dis%nrow, 1/)), nc_fname)
1511 end if
1512
1513 else
1514
1515 if (iper == 0) then
1516 ! reenter define mode and create variable
1517 call nf_verify(nf90_redef(ncid), nc_fname)
1518 call nf_verify(nf90_def_var(ncid, varname, nf90_int, &
1519 (/dim_ids%x, dim_ids%y, dim_ids%layer/), &
1520 var_id), nc_fname)
1521
1522 ! apply chunking parameters
1523 call ncvar_chunk3d(ncid, var_id, chunk_x, chunk_y, chunk_z, nc_fname)
1524 ! deflate and shuffle
1525 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1526
1527 ! put attr
1528 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1529 (/nf90_fill_int/)), nc_fname)
1530 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1531 idt%longname), nc_fname)
1532
1533 ! add grid mapping and mf6 attr
1534 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .true., &
1535 nc_fname)
1536 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1537
1538 ! exit define mode and write data
1539 call nf_verify(nf90_enddef(ncid), nc_fname)
1540 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1, 1/), &
1541 count=(/dis%ncol, dis%nrow, dis%nlay/)), &
1542 nc_fname)
1543 else
1544 ! timeseries
1545 call nf_verify(nf90_put_var(ncid, &
1546 var_ids%export, p_mem, &
1547 start=(/1, 1, 1, kper/), &
1548 count=(/dis%ncol, dis%nrow, dis%nlay, 1/)), &
1549 nc_fname)
1550 end if
1551 end if
1552 end subroutine nc_export_int1d
1553
1554 !> @brief netcdf export 2D integer
1555 !<
1556 subroutine nc_export_int2d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1557 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1558 shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
1559 integer(I4B), dimension(:, :), pointer, contiguous, intent(in) :: p_mem
1560 integer(I4B), intent(in) :: ncid
1561 type(structuredncdimidtype), intent(inout) :: dim_ids
1562 type(structuredncvaridtype), intent(inout) :: var_ids
1563 type(distype), pointer, intent(in) :: dis
1564 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1565 character(len=*), intent(in) :: mempath
1566 character(len=*), intent(in) :: nc_tag
1567 character(len=*), intent(in) :: pkgname
1568 character(len=*), intent(in) :: gridmap_name
1569 logical(LGP), intent(in) :: latlon
1570 integer(I4B), intent(in) :: deflate
1571 integer(I4B), intent(in) :: shuffle
1572 integer(I4B), intent(in) :: chunk_z
1573 integer(I4B), intent(in) :: chunk_y
1574 integer(I4B), intent(in) :: chunk_x
1575 character(len=*), intent(in) :: nc_fname
1576 character(len=LINELENGTH) :: varname
1577 integer(I4B) :: var_id
1578
1579 varname = export_varname(pkgname, idt%tagname, mempath)
1580
1581 ! reenter define mode and create variable
1582 call nf_verify(nf90_redef(ncid), nc_fname)
1583 call nf_verify(nf90_def_var(ncid, varname, nf90_int, &
1584 (/dim_ids%x, dim_ids%y/), var_id), &
1585 nc_fname)
1586
1587 ! apply chunking parameters
1588 call ncvar_chunk2d(ncid, var_id, chunk_x, chunk_y, nc_fname)
1589 ! deflate and shuffle
1590 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1591
1592 ! put attr
1593 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1594 (/nf90_fill_int/)), nc_fname)
1595 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1596 idt%longname), nc_fname)
1597
1598 ! add grid mapping and mf6 attr
1599 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .false., &
1600 nc_fname)
1601 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1602
1603 ! exit define mode and write data
1604 call nf_verify(nf90_enddef(ncid), nc_fname)
1605 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1/), &
1606 count=(/dis%ncol, dis%nrow/)), &
1607 nc_fname)
1608 end subroutine nc_export_int2d
1609
1610 !> @brief netcdf export 3D integer
1611 !<
1612 subroutine nc_export_int3d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1613 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1614 shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
1615 integer(I4B), dimension(:, :, :), pointer, contiguous, intent(in) :: p_mem
1616 integer(I4B), intent(in) :: ncid
1617 type(structuredncdimidtype), intent(inout) :: dim_ids
1618 type(structuredncvaridtype), intent(inout) :: var_ids
1619 type(distype), pointer, intent(in) :: dis
1620 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1621 character(len=*), intent(in) :: mempath
1622 character(len=*), intent(in) :: nc_tag
1623 character(len=*), intent(in) :: pkgname
1624 character(len=*), intent(in) :: gridmap_name
1625 logical(LGP), intent(in) :: latlon
1626 integer(I4B), intent(in) :: deflate
1627 integer(I4B), intent(in) :: shuffle
1628 integer(I4B), intent(in) :: chunk_z
1629 integer(I4B), intent(in) :: chunk_y
1630 integer(I4B), intent(in) :: chunk_x
1631 character(len=*), intent(in) :: nc_fname
1632 character(len=LINELENGTH) :: varname
1633 integer(I4B) :: var_id
1634
1635 varname = export_varname(pkgname, idt%tagname, mempath)
1636
1637 ! reenter define mode and create variable
1638 call nf_verify(nf90_redef(ncid), nc_fname)
1639 call nf_verify(nf90_def_var(ncid, varname, nf90_int, &
1640 (/dim_ids%x, dim_ids%y, dim_ids%layer/), &
1641 var_id), nc_fname)
1642
1643 ! apply chunking parameters
1644 call ncvar_chunk3d(ncid, var_id, chunk_x, chunk_y, chunk_z, nc_fname)
1645 ! deflate and shuffle
1646 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1647
1648 ! put attr
1649 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1650 (/nf90_fill_int/)), nc_fname)
1651 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1652 idt%longname), nc_fname)
1653
1654 ! add grid mapping and mf6 attr
1655 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .true., &
1656 nc_fname)
1657 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1658
1659 ! exit define mode and write data
1660 call nf_verify(nf90_enddef(ncid), nc_fname)
1661 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1, 1/), &
1662 count=(/dis%ncol, dis%nrow, dis%nlay/)), &
1663 nc_fname)
1664 end subroutine nc_export_int3d
1665
1666 !> @brief netcdf export 1D double
1667 !<
1668 subroutine nc_export_dbl1d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1669 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1670 shuffle, chunk_z, chunk_y, chunk_x, iper, iaux, &
1671 nc_fname)
1672 use tdismodule, only: kper
1673 real(DP), dimension(:), pointer, contiguous, intent(in) :: p_mem
1674 integer(I4B), intent(in) :: ncid
1675 type(structuredncdimidtype), intent(inout) :: dim_ids
1676 type(structuredncvaridtype), intent(inout) :: var_ids
1677 type(distype), pointer, intent(in) :: dis
1678 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1679 character(len=*), intent(in) :: mempath
1680 character(len=*), intent(in) :: nc_tag
1681 character(len=*), intent(in) :: pkgname
1682 character(len=*), intent(in) :: gridmap_name
1683 logical(LGP), intent(in) :: latlon
1684 integer(I4B), intent(in) :: deflate
1685 integer(I4B), intent(in) :: shuffle
1686 integer(I4B), intent(in) :: chunk_z
1687 integer(I4B), intent(in) :: chunk_y
1688 integer(I4B), intent(in) :: chunk_x
1689 integer(I4B), intent(in) :: iper
1690 integer(I4B), intent(in) :: iaux
1691 character(len=*), intent(in) :: nc_fname
1692 integer(I4B) :: var_id, axis_sz
1693 character(len=LINELENGTH) :: varname, longname
1694
1695 if (idt%shape == 'NROW' .or. &
1696 idt%shape == 'NCOL' .or. &
1697 idt%shape == 'NCPL' .or. &
1698 idt%shape == 'NAUX NCPL') then
1699
1700 if (iper == 0) then
1701 select case (idt%shape)
1702 case ('NROW')
1703 axis_sz = dim_ids%y
1704 case ('NCOL')
1705 axis_sz = dim_ids%x
1706 end select
1707
1708 varname = export_varname(pkgname, idt%tagname, mempath)
1709 longname = export_longname(idt%longname, pkgname, idt%tagname, mempath, &
1710 iaux=iaux, &
1711 component_type=idt%component_type, &
1712 subcomponent_type=idt%subcomponent_type)
1713
1714 ! reenter define mode and create variable
1715 call nf_verify(nf90_redef(ncid), nc_fname)
1716 call nf_verify(nf90_def_var(ncid, varname, nf90_double, &
1717 (/axis_sz/), var_id), &
1718 nc_fname)
1719
1720 ! NROW/NCOL shapes use default chunking
1721 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1722
1723 ! put attr
1724 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1725 (/nf90_fill_double/)), nc_fname)
1726 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1727 longname), nc_fname)
1728
1729 ! add mf6 attr
1730 call ncvar_mf6attr(ncid, var_id, iaux, nc_tag, nc_fname)
1731
1732 ! exit define mode and write data
1733 call nf_verify(nf90_enddef(ncid), nc_fname)
1734 call nf_verify(nf90_put_var(ncid, var_id, p_mem), &
1735 nc_fname)
1736 else
1737 ! timeseries
1738 call nf_verify(nf90_put_var(ncid, &
1739 var_ids%export, p_mem, &
1740 start=(/1, 1, kper/), &
1741 count=(/dis%ncol, dis%nrow, 1/)), nc_fname)
1742 end if
1743
1744 else
1745
1746 if (iper == 0) then
1747 varname = export_varname(pkgname, idt%tagname, mempath, iaux=iaux)
1748 longname = export_longname(idt%longname, pkgname, idt%tagname, mempath, &
1749 iaux=iaux, &
1750 component_type=idt%component_type, &
1751 subcomponent_type=idt%subcomponent_type)
1752
1753 ! reenter define mode and create variable
1754 call nf_verify(nf90_redef(ncid), nc_fname)
1755 call nf_verify(nf90_def_var(ncid, varname, nf90_double, &
1756 (/dim_ids%x, dim_ids%y, dim_ids%layer/), &
1757 var_id), nc_fname)
1758
1759 ! apply chunking parameters
1760 call ncvar_chunk3d(ncid, var_id, chunk_x, chunk_y, chunk_z, nc_fname)
1761 ! deflate and shuffle
1762 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1763
1764 ! put attr
1765 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1766 (/nf90_fill_double/)), nc_fname)
1767 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1768 longname), nc_fname)
1769
1770 ! add grid mapping and mf6 attr
1771 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .true., &
1772 nc_fname)
1773 call ncvar_mf6attr(ncid, var_id, iaux, nc_tag, nc_fname)
1774
1775 ! exit define mode and write data
1776 call nf_verify(nf90_enddef(ncid), nc_fname)
1777 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1, 1/), &
1778 count=(/dis%ncol, dis%nrow, dis%nlay/)), &
1779 nc_fname)
1780 else
1781 ! timeseries
1782 call nf_verify(nf90_put_var(ncid, &
1783 var_ids%export, p_mem, &
1784 start=(/1, 1, 1, kper/), &
1785 count=(/dis%ncol, dis%nrow, dis%nlay, 1/)), &
1786 nc_fname)
1787 end if
1788 end if
1789 end subroutine nc_export_dbl1d
1790
1791 !> @brief netcdf export 2D double
1792 !<
1793 subroutine nc_export_dbl2d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1794 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1795 shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
1796 real(DP), dimension(:, :), pointer, contiguous, intent(in) :: p_mem
1797 integer(I4B), intent(in) :: ncid
1798 type(structuredncdimidtype), intent(inout) :: dim_ids
1799 type(structuredncvaridtype), intent(inout) :: var_ids
1800 type(distype), pointer, intent(in) :: dis
1801 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1802 character(len=*), intent(in) :: mempath
1803 character(len=*), intent(in) :: nc_tag
1804 character(len=*), intent(in) :: pkgname
1805 character(len=*), intent(in) :: gridmap_name
1806 logical(LGP), intent(in) :: latlon
1807 integer(I4B), intent(in) :: deflate
1808 integer(I4B), intent(in) :: shuffle
1809 integer(I4B), intent(in) :: chunk_z
1810 integer(I4B), intent(in) :: chunk_y
1811 integer(I4B), intent(in) :: chunk_x
1812 character(len=*), intent(in) :: nc_fname
1813 character(len=LINELENGTH) :: varname
1814 integer(I4B) :: var_id
1815
1816 varname = export_varname(pkgname, idt%tagname, mempath)
1817
1818 ! reenter define mode and create variable
1819 call nf_verify(nf90_redef(ncid), nc_fname)
1820 call nf_verify(nf90_def_var(ncid, varname, nf90_double, &
1821 (/dim_ids%x, dim_ids%y/), var_id), &
1822 nc_fname)
1823
1824 ! apply chunking parameters
1825 call ncvar_chunk2d(ncid, var_id, chunk_x, chunk_y, nc_fname)
1826 ! deflate and shuffle
1827 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1828
1829 ! put attr
1830 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1831 (/nf90_fill_double/)), nc_fname)
1832 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1833 idt%longname), nc_fname)
1834
1835 ! add grid mapping and mf6 attr
1836 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .false., &
1837 nc_fname)
1838 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1839
1840 ! exit define mode and write data
1841 call nf_verify(nf90_enddef(ncid), nc_fname)
1842 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1/), &
1843 count=(/dis%ncol, dis%nrow/)), &
1844 nc_fname)
1845 end subroutine nc_export_dbl2d
1846
1847 !> @brief netcdf export 3D double
1848 !<
1849 subroutine nc_export_dbl3d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1850 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1851 shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
1852 real(DP), dimension(:, :, :), pointer, contiguous, intent(in) :: p_mem
1853 integer(I4B), intent(in) :: ncid
1854 type(structuredncdimidtype), intent(inout) :: dim_ids
1855 type(structuredncvaridtype), intent(inout) :: var_ids
1856 type(distype), pointer, intent(in) :: dis
1857 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1858 character(len=*), intent(in) :: mempath
1859 character(len=*), intent(in) :: nc_tag
1860 character(len=*), intent(in) :: pkgname
1861 character(len=*), intent(in) :: gridmap_name
1862 logical(LGP), intent(in) :: latlon
1863 integer(I4B), intent(in) :: deflate
1864 integer(I4B), intent(in) :: shuffle
1865 integer(I4B), intent(in) :: chunk_z
1866 integer(I4B), intent(in) :: chunk_y
1867 integer(I4B), intent(in) :: chunk_x
1868 character(len=*), intent(in) :: nc_fname
1869 integer(I4B) :: var_id
1870 character(len=LINELENGTH) :: varname, longname
1871
1872 varname = export_varname(pkgname, idt%tagname, mempath)
1873 longname = export_longname(idt%longname, pkgname, idt%tagname, mempath, &
1874 component_type=idt%component_type, &
1875 subcomponent_type=idt%subcomponent_type)
1876
1877 ! reenter define mode and create variable
1878 call nf_verify(nf90_redef(ncid), nc_fname)
1879 call nf_verify(nf90_def_var(ncid, varname, nf90_double, &
1880 (/dim_ids%x, dim_ids%y, dim_ids%layer/), &
1881 var_id), nc_fname)
1882
1883 ! apply chunking parameters
1884 call ncvar_chunk3d(ncid, var_id, chunk_x, chunk_y, chunk_z, nc_fname)
1885 ! deflate and shuffle
1886 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1887
1888 ! put attr
1889 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1890 (/nf90_fill_double/)), nc_fname)
1891 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1892 longname), nc_fname)
1893
1894 ! add grid mapping and mf6 attr
1895 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .true., &
1896 nc_fname)
1897 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1898
1899 ! exit define mode and write data
1900 call nf_verify(nf90_enddef(ncid), nc_fname)
1901 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1, 1/), &
1902 count=(/dis%ncol, dis%nrow, dis%nlay/)), &
1903 nc_fname)
1904 end subroutine nc_export_dbl3d
1905
1906end module disncstructuredmodule
subroutine init()
subroutine destroy(this)
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
@ mvalidate
validation mode - do not run time steps
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
@ dis
DIS6 discretization.
real(dp), parameter dhalf
real constant 1/2
Definition Constants.f90:68
real(dp), parameter dzero
real constant zero
Definition Constants.f90:65
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.
This module contains the DisNCStructuredModule.
subroutine add_pkg_data(this)
determine packages to write gridded input
subroutine ncvar_mf6attr(ncid, varid, iaux, nc_tag, nc_fname)
put variable internal modflow6 attributes
subroutine dis_export_destroy(this)
netcdf export dis destroy
subroutine nc_export_int3d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, nc_tag, pkgname, gridmap_name, latlon, deflate, shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
netcdf export 3D integer
subroutine ncvar_gridmap(ncid, varid, gridmap_name, latlon, has_layer, nc_fname)
put variable gridmap attributes
subroutine add_global_att(this)
create file (group) attributes
subroutine dis_export_init(this, modelname, modeltype, modelfname, nc_fname, disenum, nctype, iout)
netcdf export dis init
subroutine export_input_arrays(this, pkgtype, pkgname, mempath, param_dfns)
write package gridded input data
subroutine add_grid_data(this)
add grid coordinates
subroutine define_geocoords(this)
define grid projection variables
subroutine df(this)
netcdf export define
subroutine nc_export_dbl3d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, nc_tag, pkgname, gridmap_name, latlon, deflate, shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
netcdf export 3D double
subroutine nc_export_dbl2d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, nc_tag, pkgname, gridmap_name, latlon, deflate, shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
netcdf export 2D double
subroutine ncvar_chunk2d(ncid, varid, chunk_x, chunk_y, nc_fname)
define 2d variable chunking
subroutine nc_export_int1d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, nc_tag, pkgname, gridmap_name, latlon, deflate, shuffle, chunk_z, chunk_y, chunk_x, iper, nc_fname)
netcdf export 1D integer
subroutine add_proj_data(this)
add grid projection data
subroutine df_export(this)
define timeseries input variables
subroutine step(this)
netcdf export step
subroutine ncvar_deflate(ncid, varid, deflate, shuffle, nc_fname)
define variable compression
subroutine export_df(this, export_pkg)
define export package
subroutine define_dependent(this)
create the model layer dependent variables
subroutine create_timeseries(this, idt, iparam, iaux, export_pkg)
create timeseries export variable
subroutine export_input_array(this, pkgtype, pkgname, mempath, idt)
netcdf export an input array
subroutine ncvar_chunk3d(ncid, varid, chunk_x, chunk_y, chunk_z, nc_fname)
define 3d variable chunking
subroutine nc_export_int2d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, nc_tag, pkgname, gridmap_name, latlon, deflate, shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
netcdf export 2D integer
subroutine define_dim(this)
netcdf export define dimensions
subroutine package_step(this, export_pkg)
netcdf export package dynamic input
subroutine nc_export_dbl1d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, nc_tag, pkgname, gridmap_name, latlon, deflate, shuffle, chunk_z, chunk_y, chunk_x, iper, iaux, nc_fname)
netcdf export 1D double
subroutine define_gridmap(this)
create the file grid mapping container variable
type(inputparamdefinitiontype) function, dimension(:), pointer, public param_definitions(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 NCModelExportModule.
Definition NCModel.f90:8
character(len=lenbigline) function, public wrap_rotated_crs(wkt, xorigin, yorigin, angrot)
wrap a WKT2 PROJCRS in a derived CRS encoding grid rotation
Definition NCModel.f90:1036
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
subroutine, public wkt_lambert_conformal_conic_params(wkt, standard_parallel_1, standard_parallel_2, longitude_of_central_meridian, latitude_of_projection_origin, false_easting, false_northing, semi_major_axis, inverse_flattening, is_2sp, found)
extract CF lambert_conformal_conic grid_mapping parameters
Definition NCModel.f90:921
subroutine, public wkt_transverse_mercator_params(wkt, longitude_of_central_meridian, latitude_of_projection_origin, scale_factor_at_central_meridian, false_easting, false_northing, semi_major_axis, inverse_flattening, found)
extract CF transverse_mercator grid_mapping parameters
Definition NCModel.f90:870
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
subroutine, public wkt_albers_conical_equal_area_params(wkt, standard_parallel_1, standard_parallel_2, longitude_of_central_meridian, latitude_of_projection_origin, false_easting, false_northing, semi_major_axis, inverse_flattening, found)
extract CF albers_conical_equal_area grid_mapping parameters
Definition NCModel.f90:982
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
integer(i4b) isim_mode
simulation mode
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
real(dp), dimension(:), pointer, public, contiguous perlen
length of each stress period
Definition tdis.f90:41
real(dp), pointer, public totim
time relative to start of simulation
Definition tdis.f90:35
integer(i4b), pointer, public kper
current stress period number
Definition tdis.f90:26
This class is used to store a single deferred-length character string. It was designed to work in an ...
Structured grid discretization.
Definition Dis.f90:23
Input parameter definition. Describes an input parameter.
abstract type for model netcdf export type
Definition NCModel.f90:120