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)
713 class(disncstructuredtype), intent(inout) :: this
714 character(LENCOMPONENTNAME) :: ptype, pname, pkgtype
715 type(characterstringtype), dimension(:), contiguous, &
716 pointer :: pkgtypes => null()
717 type(characterstringtype), dimension(:), contiguous, &
718 pointer :: pkgnames => null()
719 type(characterstringtype), dimension(:), contiguous, &
720 pointer :: mempaths => null()
721 type(inputparamdefinitiontype), dimension(:), pointer :: param_dfns
722 character(len=LENMEMPATH) :: input_mempath, mempath
723 integer(I4B) :: n
724 integer(I4B), pointer :: export_arrays
725 logical(LGP) :: found
726
727 input_mempath = create_mem_path(component=this%modelname, context=idm_context)
728
729 ! set pointers to model path package info
730 call mem_setptr(pkgtypes, 'PKGTYPES', input_mempath)
731 call mem_setptr(pkgnames, 'PKGNAMES', input_mempath)
732 call mem_setptr(mempaths, 'MEMPATHS', input_mempath)
733
734 do n = 1, size(mempaths)
735 ! allocate export_arrays
736 allocate (export_arrays)
737 export_arrays = 0
738
739 ! set package attributes
740 mempath = mempaths(n)
741 pname = pkgnames(n)
742 ptype = pkgtypes(n)
743
744 ! export input arrays
745 if (mempath /= '') then
746 ! update export
747 call mem_set_value(export_arrays, 'EXPORT_NC', mempath, found, &
748 release=.false.)
749
750 if (export_arrays > 0) then
751 pkgtype = idm_subcomponent_type(this%modeltype, ptype)
752 param_dfns => param_definitions(this%modeltype, pkgtype)
753 call this%export_input_arrays(ptype, pname, mempath, param_dfns)
754 end if
755 end if
756
757 ! cleanup
758 deallocate (export_arrays)
759 end do
760 end subroutine add_pkg_data
761
762 !> @brief create file (group) attributes
763 !<
764 subroutine add_global_att(this)
765 class(disncstructuredtype), intent(inout) :: this
766 ! file scoped title
767 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'title', &
768 this%annotation%title), this%nc_fname)
769 ! source (MODFLOW 6)
770 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'source', &
771 this%annotation%source), this%nc_fname)
772 ! grid type (MODFLOW 6)
773 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'modflow_grid', &
774 this%annotation%grid), this%nc_fname)
775 ! MODFLOW 6 model type
776 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'modflow_model', &
777 this%annotation%model), this%nc_fname)
778 ! generation datetime
779 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'history', &
780 this%annotation%history), this%nc_fname)
781 ! supported conventions
782 call nf_verify(nf90_put_att(this%ncid, nf90_global, 'Conventions', &
783 this%annotation%conventions), &
784 this%nc_fname)
785 end subroutine add_global_att
786
787 !> @brief netcdf export define dimensions
788 !<
789 subroutine define_dim(this)
790 use constantsmodule, only: mvalidate
792 class(disncstructuredtype), intent(inout) :: this
793
794 ! bound dim
795 call nf_verify(nf90_def_dim(this%ncid, 'bnd', 2, this%dim_ids%bnd), &
796 this%nc_fname)
797
798 if (isim_mode /= mvalidate .or. this%pkglist%Count() > 0) then
799 ! Time
800 call nf_verify(nf90_def_dim(this%ncid, 'time', this%totnstp, &
801 this%dim_ids%time), this%nc_fname)
802 call nf_verify(nf90_def_var(this%ncid, 'time', nf90_double, &
803 this%dim_ids%time, this%var_ids%time), &
804 this%nc_fname)
805 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'calendar', &
806 'standard'), this%nc_fname)
807 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'units', &
808 this%datetime), this%nc_fname)
809 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'axis', 'T'), &
810 this%nc_fname)
811 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'standard_name', &
812 'time'), this%nc_fname)
813 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'long_name', &
814 'time'), this%nc_fname)
815 ! stress period bounds of the time coordinate (CF-1.13 7.1)
816 if (isim_mode == mvalidate) then
817 call nf_verify(nf90_put_att(this%ncid, this%var_ids%time, 'bounds', &
818 'time_bnds'), this%nc_fname)
819 call nf_verify(nf90_def_var(this%ncid, 'time_bnds', nf90_double, &
820 (/this%dim_ids%bnd, this%dim_ids%time/), &
821 this%var_ids%time_bnds), this%nc_fname)
822 end if
823 end if
824
825 ! Z dimension
826 call nf_verify(nf90_def_dim(this%ncid, 'layer', this%dis%nlay, &
827 this%dim_ids%layer), this%nc_fname)
828 call nf_verify(nf90_def_var(this%ncid, 'layer', nf90_int, &
829 this%dim_ids%layer, this%var_ids%layer), &
830 this%nc_fname)
831 call nf_verify(nf90_put_att(this%ncid, this%var_ids%layer, 'units', '1'), &
832 this%nc_fname)
833 call nf_verify(nf90_put_att(this%ncid, this%var_ids%layer, 'axis', 'Z'), &
834 this%nc_fname)
835 call nf_verify(nf90_put_att(this%ncid, this%var_ids%layer, 'positive', &
836 'down'), this%nc_fname)
837 call nf_verify(nf90_put_att(this%ncid, this%var_ids%layer, 'long_name', &
838 'model layer'), this%nc_fname)
839
840 ! Y dimension
841 call nf_verify(nf90_def_dim(this%ncid, 'y', this%dis%nrow, this%dim_ids%y), &
842 this%nc_fname)
843 call nf_verify(nf90_def_var(this%ncid, 'y', nf90_double, this%dim_ids%y, &
844 this%var_ids%y), this%nc_fname)
845 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'units', &
846 this%lenunits), this%nc_fname)
847 if (this%dis%angrot == dzero .or. &
848 wrap_rotated_crs(this%crs_wkt, this%dis%xorigin, this%dis%yorigin, &
849 this%dis%angrot) /= '') then
850 ! axis/standard_name assert that y holds the true projected
851 ! position. For a rotated grid this only holds once crs_wkt
852 ! encodes the rotation (see wrap_rotated_crs in define_gridmap);
853 ! otherwise y is grid-local (see add_grid_data) and asserting it
854 ! would cause GDAL's netCDF driver to derive an incorrect,
855 ! unrotated position directly from y's grid-local values.
856 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'axis', 'Y'), &
857 this%nc_fname)
858 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'standard_name', &
859 'projection_y_coordinate'), this%nc_fname)
860 end if
861 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'long_name', &
862 'Northing'), this%nc_fname)
863 if (this%gridmap_name /= '') then
864 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'grid_mapping', &
865 this%gridmap_name), this%nc_fname)
866 end if
867 call nf_verify(nf90_put_att(this%ncid, this%var_ids%y, 'bounds', 'y_bnds'), &
868 this%nc_fname)
869 call nf_verify(nf90_def_var(this%ncid, 'y_bnds', nf90_double, &
870 (/this%dim_ids%bnd, this%dim_ids%y/), &
871 this%var_ids%y_bnds), this%nc_fname)
872
873 ! X dimension
874 call nf_verify(nf90_def_dim(this%ncid, 'x', this%dis%ncol, this%dim_ids%x), &
875 this%nc_fname)
876 call nf_verify(nf90_def_var(this%ncid, 'x', nf90_double, this%dim_ids%x, &
877 this%var_ids%x), this%nc_fname)
878 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'units', &
879 this%lenunits), this%nc_fname)
880 if (this%dis%angrot == dzero .or. &
881 wrap_rotated_crs(this%crs_wkt, this%dis%xorigin, this%dis%yorigin, &
882 this%dis%angrot) /= '') then
883 ! see matching comment on the Y dimension above
884 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'axis', 'X'), &
885 this%nc_fname)
886 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'standard_name', &
887 'projection_x_coordinate'), this%nc_fname)
888 end if
889 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'long_name', &
890 'Easting'), this%nc_fname)
891 if (this%gridmap_name /= '') then
892 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'grid_mapping', &
893 this%gridmap_name), this%nc_fname)
894 end if
895 call nf_verify(nf90_put_att(this%ncid, this%var_ids%x, 'bounds', 'x_bnds'), &
896 this%nc_fname)
897 call nf_verify(nf90_def_var(this%ncid, 'x_bnds', nf90_double, &
898 (/this%dim_ids%bnd, this%dim_ids%x/), &
899 this%var_ids%x_bnds), this%nc_fname)
900
901 ! z: cell center elevation auxiliary coordinate -- a real vertical
902 ! position, unlike the discrete layer index. Always written,
903 ! independent of CRS/NCF configuration.
904 call nf_verify(nf90_def_var(this%ncid, 'z', nf90_double, &
905 (/this%dim_ids%x, this%dim_ids%y, &
906 this%dim_ids%layer/), &
907 this%var_ids%elevation), this%nc_fname)
908 call ncvar_chunk3d(this%ncid, this%var_ids%elevation, this%chunk_x, &
909 this%chunk_y, this%chunk_z, this%nc_fname)
910 call ncvar_deflate(this%ncid, this%var_ids%elevation, this%deflate, &
911 this%shuffle, this%nc_fname)
912 call nf_verify(nf90_put_att(this%ncid, this%var_ids%elevation, 'units', &
913 this%lenunits), this%nc_fname)
914 call nf_verify(nf90_put_att(this%ncid, this%var_ids%elevation, &
915 'standard_name', 'altitude'), this%nc_fname)
916 call nf_verify(nf90_put_att(this%ncid, this%var_ids%elevation, &
917 'positive', 'up'), this%nc_fname)
918 call nf_verify(nf90_put_att(this%ncid, this%var_ids%elevation, &
919 'long_name', 'cell center elevation'), &
920 this%nc_fname)
921 end subroutine define_dim
922
923 !> @brief create the model layer dependent variables
924 !<
925 subroutine define_dependent(this)
926 use constantsmodule, only: dhnoflo
927 class(disncstructuredtype), intent(inout) :: this
928
929 call nf_verify(nf90_def_var(this%ncid, this%xname, nf90_double, &
930 (/this%dim_ids%x, this%dim_ids%y, &
931 this%dim_ids%layer, this%dim_ids%time/), &
932 this%var_ids%dependent), &
933 this%nc_fname)
934
935 ! apply chunking parameters
936 if (this%chunking_active) then
937 call nf_verify(nf90_def_var_chunking(this%ncid, &
938 this%var_ids%dependent, &
939 nf90_chunked, &
940 (/this%chunk_x, this%chunk_y, &
941 this%chunk_z, this%chunk_time/)), &
942 this%nc_fname)
943 end if
944
945 ! deflate and shuffle
946 call ncvar_deflate(this%ncid, this%var_ids%dependent, this%deflate, &
947 this%shuffle, this%nc_fname)
948
949 ! put attr; head is a length, other dependent variables have no
950 ! units known from the input
951 if (this%modeltype == 'GWF') then
952 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent, &
953 'units', this%lenunits), this%nc_fname)
954 end if
955 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent, 'long_name', &
956 this%annotation%longname), this%nc_fname)
957 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent, '_FillValue', &
958 (/dhnoflo/)), this%nc_fname)
959 ! cell_methods (CF-1.13 7.3): dependent variable values are the
960 ! instantaneous simulated state at each output time, not a time
961 ! mean/accumulation over the interval -- an intensive quantity whose
962 ! default method is already "point" per CF's own Appendix E, stated
963 ! explicitly here per CF's recommendation to do so for every
964 ! spatio-temporal dimension.
965 call nf_verify(nf90_put_att(this%ncid, this%var_ids%dependent, &
966 'cell_methods', 'time: point'), this%nc_fname)
967
968 ! add grid mapping
969 call ncvar_gridmap(this%ncid, this%var_ids%dependent, this%gridmap_name, &
970 this%latlon, .true., this%nc_fname)
971 end subroutine define_dependent
972
973 !> @brief create the file grid mapping container variable
974 !<
975 subroutine define_gridmap(this)
976 class(disncstructuredtype), intent(inout) :: this
977 integer(I4B) :: var_id
978 character(len=LINELENGTH) :: gmname
979 character(len=LENBIGLINE) :: effective_crs_wkt, derived_wkt
980 character(len=100) :: rotinfo
981 real(DP) :: longitude_of_central_meridian, latitude_of_projection_origin
982 real(DP) :: scale_factor_at_central_meridian, false_easting, false_northing
983 real(DP) :: semi_major_axis, inverse_flattening
984 real(DP) :: standard_parallel_1, standard_parallel_2
985 logical(LGP) :: params_found, is_2sp
986
987 if (this%wkt /= '' .or. this%crs_wkt /= '') then
988 call nf_verify(nf90_redef(this%ncid), this%nc_fname)
989 call nf_verify(nf90_def_var(this%ncid, this%gridmap_name, nf90_int, &
990 var_id), this%nc_fname)
991 ! wkt (WKT1, OGC 01-009) -- for legacy/QGIS compatibility. WKT1
992 ! has no derived-CRS syntax, so it cannot encode grid rotation and
993 ! is always written unmodified.
994 if (this%wkt /= '') then
995 call nf_verify(nf90_put_att(this%ncid, var_id, 'wkt', this%wkt), &
996 this%nc_fname)
997 end if
998 ! crs_wkt (WKT2, ISO 19162:2019) -- required by CF-1.13
999 if (this%crs_wkt /= '') then
1000 effective_crs_wkt = this%crs_wkt
1001 else
1002 effective_crs_wkt = this%wkt
1003 end if
1004 ! For a rotated structured grid, wrap crs_wkt in a derived CRS
1005 ! encoding the rotation (see wrap_rotated_crs) so a CRS-aware
1006 ! consumer resolves the correct real-world position and shape
1007 ! via ordinary reprojection. Falls back to an unmodified
1008 ! crs_wkt, with a warning, if CRS_WKT was not provided or is not
1009 ! a recognized WKT2 PROJCRS.
1010 if (this%dis%angrot /= dzero) then
1011 if (this%crs_wkt == '') then
1012 write (warnmsg, '(a)') 'Structured rotated grid with no CRS_WKT &
1013 &input: no rotation-positioning information will be written &
1014 &to the output CRS; x/y coordinate values will not reflect &
1015 &the true rotated position. Applies to file "'// &
1016 trim(this%nc_fname)//'".'
1017 call store_warning(warnmsg)
1018 else
1019 derived_wkt = wrap_rotated_crs(this%crs_wkt, this%dis%xorigin, &
1020 this%dis%yorigin, this%dis%angrot)
1021 if (derived_wkt /= '') then
1022 write (rotinfo, '(a,g0,a,g0,a,g0,a)') 'xorigin=', &
1023 this%dis%xorigin, ', yorigin=', this%dis%yorigin, &
1024 ', angrot=', this%dis%angrot, ' deg'
1025 write (warnmsg, '(a)') 'Structured rotated grid: wrapped &
1026 &CRS_WKT in a derived CRS to encode grid rotation ('// &
1027 trim(rotinfo)//'). Applies to file "'// &
1028 trim(this%nc_fname)//'".'
1029 call store_warning(warnmsg)
1030 effective_crs_wkt = derived_wkt
1031 else
1032 write (warnmsg, '(a)') 'Structured rotated grid: CRS_WKT is &
1033 &not a recognized WKT2 PROJCRS; no rotation-positioning &
1034 &information will be written to the output CRS; x/y &
1035 &coordinate values will not reflect the true rotated &
1036 &position. Applies to file "'//trim(this%nc_fname)//'".'
1037 call store_warning(warnmsg)
1038 end if
1039 end if
1040 end if
1041 call nf_verify(nf90_put_att(this%ncid, var_id, 'crs_wkt', &
1042 trim(effective_crs_wkt)), this%nc_fname)
1043 ! grid_mapping_name derived from WKT projection keyword
1044 gmname = wkt_to_cf_gridmapping(trim(effective_crs_wkt))
1045 if (gmname /= '') then
1046 call nf_verify(nf90_put_att(this%ncid, var_id, 'grid_mapping_name', &
1047 trim(gmname)), this%nc_fname)
1048 end if
1049 ! CF-standard numeric grid_mapping parameters (in addition to
1050 ! wkt/crs_wkt): some netCDF readers -- notably ArcGIS's classic
1051 ! netCDF connector -- position a projected grid using these
1052 ! individual attributes rather than parsing wkt/crs_wkt, and
1053 ! default every parameter to 0 if they are absent. Covers
1054 ! transverse_mercator, lambert_conformal_conic (2SP only -- CF has
1055 ! no scale_factor attribute for this method, so a 1SP/scale-factor
1056 ! WKT cannot be exactly represented), and albers_conical_equal_area;
1057 ! other grid_mapping_name values are unaffected. Best-effort
1058 ! extraction from effective_crs_wkt -- MF6 has no CRS parsing
1059 ! library, so this is skipped if any parameter cannot be located.
1060 if (trim(gmname) == 'transverse_mercator') then
1061 call wkt_transverse_mercator_params(trim(effective_crs_wkt), &
1062 longitude_of_central_meridian, &
1063 latitude_of_projection_origin, &
1064 scale_factor_at_central_meridian, &
1065 false_easting, false_northing, &
1066 semi_major_axis, inverse_flattening, &
1067 params_found)
1068 if (params_found) then
1069 call nf_verify(nf90_put_att(this%ncid, var_id, &
1070 'longitude_of_central_meridian', &
1071 longitude_of_central_meridian), &
1072 this%nc_fname)
1073 call nf_verify(nf90_put_att(this%ncid, var_id, &
1074 'latitude_of_projection_origin', &
1075 latitude_of_projection_origin), &
1076 this%nc_fname)
1077 call nf_verify(nf90_put_att(this%ncid, var_id, &
1078 'scale_factor_at_central_meridian', &
1079 scale_factor_at_central_meridian), &
1080 this%nc_fname)
1081 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_easting', &
1082 false_easting), this%nc_fname)
1083 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_northing', &
1084 false_northing), this%nc_fname)
1085 call nf_verify(nf90_put_att(this%ncid, var_id, 'semi_major_axis', &
1086 semi_major_axis), this%nc_fname)
1087 call nf_verify(nf90_put_att(this%ncid, var_id, 'inverse_flattening', &
1088 inverse_flattening), this%nc_fname)
1089 else
1090 write (warnmsg, '(a)') 'Structured grid: unable to extract CF &
1091 &transverse_mercator grid_mapping parameters from CRS_WKT; &
1092 &some netCDF readers may not correctly position this grid. &
1093 &Applies to file "'//trim(this%nc_fname)//'".'
1094 call store_warning(warnmsg)
1095 end if
1096 else if (trim(gmname) == 'lambert_conformal_conic') then
1097 call wkt_lambert_conformal_conic_params(trim(effective_crs_wkt), &
1098 standard_parallel_1, &
1099 standard_parallel_2, &
1100 longitude_of_central_meridian, &
1101 latitude_of_projection_origin, &
1102 false_easting, false_northing, &
1103 semi_major_axis, &
1104 inverse_flattening, is_2sp, &
1105 params_found)
1106 if (params_found) then
1107 call nf_verify(nf90_put_att(this%ncid, var_id, 'standard_parallel', &
1108 (/standard_parallel_1, &
1109 standard_parallel_2/)), this%nc_fname)
1110 call nf_verify(nf90_put_att(this%ncid, var_id, &
1111 'longitude_of_central_meridian', &
1112 longitude_of_central_meridian), &
1113 this%nc_fname)
1114 call nf_verify(nf90_put_att(this%ncid, var_id, &
1115 'latitude_of_projection_origin', &
1116 latitude_of_projection_origin), &
1117 this%nc_fname)
1118 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_easting', &
1119 false_easting), this%nc_fname)
1120 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_northing', &
1121 false_northing), this%nc_fname)
1122 call nf_verify(nf90_put_att(this%ncid, var_id, 'semi_major_axis', &
1123 semi_major_axis), this%nc_fname)
1124 call nf_verify(nf90_put_att(this%ncid, var_id, 'inverse_flattening', &
1125 inverse_flattening), this%nc_fname)
1126 else if (.not. is_2sp) then
1127 write (warnmsg, '(a)') 'Structured grid: 1SP lambert_conformal_conic &
1128 &variants are not supported for CF grid_mapping parameter &
1129 &extraction (CF has no scale_factor attribute for this method); &
1130 &some netCDF readers may not correctly position this grid. &
1131 &Applies to file "'//trim(this%nc_fname)//'".'
1132 call store_warning(warnmsg)
1133 else
1134 write (warnmsg, '(a)') 'Structured grid: unable to extract CF &
1135 &lambert_conformal_conic grid_mapping parameters from CRS_WKT; &
1136 &some netCDF readers may not correctly position this grid. &
1137 &Applies to file "'//trim(this%nc_fname)//'".'
1138 call store_warning(warnmsg)
1139 end if
1140 else if (trim(gmname) == 'albers_conical_equal_area') then
1141 call wkt_albers_conical_equal_area_params(trim(effective_crs_wkt), &
1142 standard_parallel_1, &
1143 standard_parallel_2, &
1144 longitude_of_central_meridian, &
1145 latitude_of_projection_origin, &
1146 false_easting, false_northing, &
1147 semi_major_axis, &
1148 inverse_flattening, &
1149 params_found)
1150 if (params_found) then
1151 call nf_verify(nf90_put_att(this%ncid, var_id, 'standard_parallel', &
1152 (/standard_parallel_1, &
1153 standard_parallel_2/)), this%nc_fname)
1154 call nf_verify(nf90_put_att(this%ncid, var_id, &
1155 'longitude_of_central_meridian', &
1156 longitude_of_central_meridian), &
1157 this%nc_fname)
1158 call nf_verify(nf90_put_att(this%ncid, var_id, &
1159 'latitude_of_projection_origin', &
1160 latitude_of_projection_origin), &
1161 this%nc_fname)
1162 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_easting', &
1163 false_easting), this%nc_fname)
1164 call nf_verify(nf90_put_att(this%ncid, var_id, 'false_northing', &
1165 false_northing), this%nc_fname)
1166 call nf_verify(nf90_put_att(this%ncid, var_id, 'semi_major_axis', &
1167 semi_major_axis), this%nc_fname)
1168 call nf_verify(nf90_put_att(this%ncid, var_id, 'inverse_flattening', &
1169 inverse_flattening), this%nc_fname)
1170 else
1171 write (warnmsg, '(a)') 'Structured grid: unable to extract CF &
1172 &albers_conical_equal_area grid_mapping parameters from &
1173 &CRS_WKT; some netCDF readers may not correctly position this &
1174 &grid. Applies to file "'//trim(this%nc_fname)//'".'
1175 call store_warning(warnmsg)
1176 end if
1177 end if
1178 ! GeoTransform/spatial_ref intentionally not written: a GDAL-only,
1179 ! non-CF extension ArcGIS does not read, superseded by crs_wkt's
1180 ! derived-CRS encoding above, which any CRS-aware consumer resolves
1181 ! via ordinary reprojection.
1182 call nf_verify(nf90_enddef(this%ncid), this%nc_fname)
1183 call nf_verify(nf90_put_var(this%ncid, var_id, 1), &
1184 this%nc_fname)
1185 end if
1186 end subroutine define_gridmap
1187
1188 !> @brief define grid projection variables
1189 !<
1190 subroutine define_geocoords(this)
1191 class(disncstructuredtype), intent(inout) :: this
1192 if (this%latlon) then
1193 ! lat
1194 call nf_verify(nf90_def_var(this%ncid, 'lat', nf90_double, &
1195 (/this%dim_ids%x, this%dim_ids%y/), &
1196 this%var_ids%latitude), this%nc_fname)
1197 call nf_verify(nf90_put_att(this%ncid, this%var_ids%latitude, &
1198 'units', 'degrees_north'), this%nc_fname)
1199 call nf_verify(nf90_put_att(this%ncid, this%var_ids%latitude, &
1200 'standard_name', 'latitude'), this%nc_fname)
1201 call nf_verify(nf90_put_att(this%ncid, this%var_ids%latitude, &
1202 'long_name', 'latitude'), this%nc_fname)
1203
1204 ! lon
1205 call nf_verify(nf90_def_var(this%ncid, 'lon', nf90_double, &
1206 (/this%dim_ids%x, this%dim_ids%y/), &
1207 this%var_ids%longitude), this%nc_fname)
1208 call nf_verify(nf90_put_att(this%ncid, this%var_ids%longitude, &
1209 'units', 'degrees_east'), this%nc_fname)
1210 call nf_verify(nf90_put_att(this%ncid, this%var_ids%longitude, &
1211 'standard_name', 'longitude'), this%nc_fname)
1212 call nf_verify(nf90_put_att(this%ncid, this%var_ids%longitude, &
1213 'long_name', 'longitude'), this%nc_fname)
1214 end if
1215 end subroutine define_geocoords
1216
1217 !> @brief add grid projection data
1218 !<
1219 subroutine add_proj_data(this)
1220 class(disncstructuredtype), intent(inout) :: this
1221 if (this%latlon) then
1222 ! lat
1223 call nf_verify(nf90_put_var(this%ncid, this%var_ids%latitude, &
1224 this%latitude, start=(/1, 1/), &
1225 count=(/this%dis%ncol, this%dis%nrow/)), &
1226 this%nc_fname)
1227
1228 ! lon
1229 call nf_verify(nf90_put_var(this%ncid, this%var_ids%longitude, &
1230 this%longitude, start=(/1, 1/), &
1231 count=(/this%dis%ncol, this%dis%nrow/)), &
1232 this%nc_fname)
1233 end if
1234 end subroutine add_proj_data
1235
1236 !> @brief add grid coordinates
1237 !<
1238 subroutine add_grid_data(this)
1239 class(disncstructuredtype), intent(inout) :: this
1240 integer(I4B) :: ibnd, n, k
1241 real(DP), dimension(:, :), pointer, contiguous :: dbl2d
1242 real(DP), dimension(:), allocatable :: x, y
1243 real(DP), dimension(:, :, :), allocatable :: elev3d
1244 real(DP) :: xoff, yoff
1245
1246 if (this%dis%angrot /= dzero) then
1247 xoff = dzero
1248 yoff = dzero
1249 else
1250 xoff = this%dis%xorigin
1251 yoff = this%dis%yorigin
1252 end if
1253
1254 allocate (x(size(this%dis%cellx)))
1255 allocate (y(size(this%dis%celly)))
1256
1257 do n = 1, size(this%dis%cellx)
1258 x(n) = this%dis%cellx(n) + xoff
1259 end do
1260
1261 do n = 1, size(this%dis%celly)
1262 y(n) = this%dis%celly(n) + yoff
1263 end do
1264
1265 call nf_verify(nf90_put_var(this%ncid, this%var_ids%x, x), &
1266 this%nc_fname)
1267 call nf_verify(nf90_put_var(this%ncid, this%var_ids%y, y), &
1268 this%nc_fname)
1269 ! TODO see cf-conventions 4.3.3. Parametric Vertical Coordinate
1270 call nf_verify(nf90_put_var(this%ncid, this%var_ids%layer, this%layers), &
1271 this%nc_fname)
1272
1273 deallocate (x)
1274 deallocate (y)
1275
1276 ! z: cell center elevation, layer 1 = midpoint of top/bot3d(1),
1277 ! layer k>1 = midpoint of bot3d(k-1)/bot3d(k)
1278 allocate (elev3d(this%dis%ncol, this%dis%nrow, this%nlay))
1279 do k = 1, this%nlay
1280 if (k == 1) then
1281 elev3d(:, :, k) = (this%dis%top2d(:, :) + this%dis%bot3d(:, :, k)) * &
1282 dhalf
1283 else
1284 elev3d(:, :, k) = (this%dis%bot3d(:, :, k - 1) + &
1285 this%dis%bot3d(:, :, k)) * dhalf
1286 end if
1287 end do
1288 call nf_verify(nf90_put_var(this%ncid, this%var_ids%elevation, elev3d), &
1289 this%nc_fname)
1290 deallocate (elev3d)
1291
1292 ! bounds x
1293 allocate (dbl2d(2, size(this%dis%cellx)))
1294 ibnd = 1
1295 do n = 1, size(this%dis%cellx)
1296 if (ibnd == 1) then
1297 dbl2d(1, ibnd) = xoff
1298 dbl2d(2, ibnd) = xoff + this%dis%delr(ibnd)
1299 else
1300 dbl2d(1, ibnd) = dbl2d(1, ibnd - 1) + this%dis%delr(ibnd)
1301 dbl2d(2, ibnd) = dbl2d(2, ibnd - 1) + this%dis%delr(ibnd)
1302 end if
1303 ibnd = ibnd + 1
1304 end do
1305 call nf_verify(nf90_put_var(this%ncid, this%var_ids%x_bnds, dbl2d), &
1306 this%nc_fname)
1307 deallocate (dbl2d)
1308
1309 ! bounds y
1310 allocate (dbl2d(2, size(this%dis%celly)))
1311 ibnd = 1
1312 do n = size(this%dis%celly), 1, -1
1313 if (ibnd == 1) then
1314 dbl2d(1, ibnd) = yoff + sum(this%dis%delc) - this%dis%delc(n)
1315 dbl2d(2, ibnd) = yoff + sum(this%dis%delc)
1316 else
1317 dbl2d(1, ibnd) = dbl2d(1, ibnd - 1) - this%dis%delc(n)
1318 dbl2d(2, ibnd) = dbl2d(2, ibnd - 1) - this%dis%delc(n)
1319 end if
1320 ibnd = ibnd + 1
1321 end do
1322 call nf_verify(nf90_put_var(this%ncid, this%var_ids%y_bnds, dbl2d), &
1323 this%nc_fname)
1324 deallocate (dbl2d)
1325 end subroutine add_grid_data
1326
1327 !> @brief define 2d variable chunking
1328 !<
1329 subroutine ncvar_chunk2d(ncid, varid, chunk_x, chunk_y, nc_fname)
1330 integer(I4B), intent(in) :: ncid
1331 integer(I4B), intent(in) :: varid
1332 integer(I4B), intent(in) :: chunk_x
1333 integer(I4B), intent(in) :: chunk_y
1334 character(len=*), intent(in) :: nc_fname
1335 if (chunk_y > 0 .and. chunk_x > 0) then
1336 call nf_verify(nf90_def_var_chunking(ncid, varid, nf90_chunked, &
1337 (/chunk_x, chunk_y/)), nc_fname)
1338 end if
1339 end subroutine ncvar_chunk2d
1340
1341 !> @brief define 3d variable chunking
1342 !<
1343 subroutine ncvar_chunk3d(ncid, varid, chunk_x, chunk_y, chunk_z, nc_fname)
1344 integer(I4B), intent(in) :: ncid
1345 integer(I4B), intent(in) :: varid
1346 integer(I4B), intent(in) :: chunk_x
1347 integer(I4B), intent(in) :: chunk_y
1348 integer(I4B), intent(in) :: chunk_z
1349 character(len=*), intent(in) :: nc_fname
1350 if (chunk_z > 0 .and. chunk_y > 0 .and. chunk_x > 0) then
1351 call nf_verify(nf90_def_var_chunking(ncid, varid, nf90_chunked, &
1352 (/chunk_x, chunk_y, chunk_z/)), &
1353 nc_fname)
1354 end if
1355 end subroutine ncvar_chunk3d
1356
1357 !> @brief define variable compression
1358 !<
1359 subroutine ncvar_deflate(ncid, varid, deflate, shuffle, nc_fname)
1360 integer(I4B), intent(in) :: ncid
1361 integer(I4B), intent(in) :: varid
1362 integer(I4B), intent(in) :: deflate
1363 integer(I4B), intent(in) :: shuffle
1364 character(len=*), intent(in) :: nc_fname
1365 ! deflate and shuffle
1366 if (deflate >= 0) then
1367 call nf_verify(nf90_def_var_deflate(ncid, varid, shuffle=shuffle, &
1368 deflate=1, deflate_level=deflate), &
1369 nc_fname)
1370 end if
1371 end subroutine ncvar_deflate
1372
1373 !> @brief put variable gridmap attributes
1374 !!
1375 !! has_layer must be true only for variables carrying the layer, y, and x
1376 !! dimensions -- z(layer, y, x) can only be legally referenced via
1377 !! `coordinates` from a variable whose dimensions are a superset of its
1378 !! own (CF-1.13 5.2). 2D (y, x)-only variables such as dis_top must pass
1379 !! has_layer=.false. z is written unconditionally, independent of
1380 !! gridmap_name/latlon.
1381 !<
1382 subroutine ncvar_gridmap(ncid, varid, gridmap_name, latlon, has_layer, &
1383 nc_fname)
1384 integer(I4B), intent(in) :: ncid
1385 integer(I4B), intent(in) :: varid
1386 character(len=*), intent(in) :: gridmap_name
1387 logical(LGP), intent(in) :: latlon
1388 logical(LGP), intent(in) :: has_layer
1389 character(len=*), intent(in) :: nc_fname
1390 character(len=LINELENGTH) :: coords
1391 coords = ''
1392 ! NOTE: x/y are not listed here even when gridmap_name is set -- unlike
1393 ! lon/lat (real 2D auxiliary coordinates), x/y are true CF dimension
1394 ! coordinates already discoverable by dimension-name matching alone, so
1395 ! listing them in 'coordinates' is redundant (CF-1.13 Ch.5 preamble).
1396 if (latlon) then
1397 coords = 'lon lat'
1398 end if
1399 if (has_layer) then
1400 if (len_trim(coords) > 0) then
1401 coords = trim(coords)//' z'
1402 else
1403 coords = 'z'
1404 end if
1405 end if
1406 if (len_trim(coords) > 0) then
1407 call nf_verify(nf90_put_att(ncid, varid, 'coordinates', trim(coords)), &
1408 nc_fname)
1409 end if
1410 if (gridmap_name /= '') then
1411 call nf_verify(nf90_put_att(ncid, varid, 'grid_mapping', gridmap_name), &
1412 nc_fname)
1413 end if
1414 end subroutine ncvar_gridmap
1415
1416 !> @brief put variable internal modflow6 attributes
1417 !<
1418 subroutine ncvar_mf6attr(ncid, varid, iaux, nc_tag, nc_fname)
1419 integer(I4B), intent(in) :: ncid
1420 integer(I4B), intent(in) :: varid
1421 integer(I4B), intent(in) :: iaux
1422 character(len=*), intent(in) :: nc_tag
1423 character(len=*), intent(in) :: nc_fname
1424 if (nc_tag /= '') then
1425 call nf_verify(nf90_put_att(ncid, varid, 'modflow_input', &
1426 nc_tag), nc_fname)
1427 if (iaux > 0) then
1428 call nf_verify(nf90_put_att(ncid, varid, 'modflow_iaux', &
1429 iaux), nc_fname)
1430 end if
1431 end if
1432 end subroutine ncvar_mf6attr
1433
1434 !> @brief netcdf export 1D integer
1435 !<
1436 subroutine nc_export_int1d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1437 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1438 shuffle, chunk_z, chunk_y, chunk_x, iper, nc_fname)
1439 use tdismodule, only: kper
1440 integer(I4B), dimension(:), pointer, contiguous, intent(in) :: p_mem
1441 integer(I4B), intent(in) :: ncid
1442 type(structuredncdimidtype), intent(inout) :: dim_ids
1443 type(structuredncvaridtype), intent(inout) :: var_ids
1444 type(distype), pointer, intent(in) :: dis
1445 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1446 character(len=*), intent(in) :: mempath
1447 character(len=*), intent(in) :: nc_tag
1448 character(len=*), intent(in) :: pkgname
1449 character(len=*), intent(in) :: gridmap_name
1450 logical(LGP), intent(in) :: latlon
1451 integer(I4B), intent(in) :: deflate
1452 integer(I4B), intent(in) :: shuffle
1453 integer(I4B), intent(in) :: chunk_z
1454 integer(I4B), intent(in) :: chunk_y
1455 integer(I4B), intent(in) :: chunk_x
1456 integer(I4B), intent(in) :: iper
1457 character(len=*), intent(in) :: nc_fname
1458 integer(I4B) :: var_id, axis_sz
1459 character(len=LINELENGTH) :: varname, longname
1460
1461 varname = export_varname(pkgname, idt%tagname, mempath)
1462
1463 if (idt%shape == 'NROW' .or. &
1464 idt%shape == 'NCOL' .or. &
1465 idt%shape == 'NCPL' .or. &
1466 idt%shape == 'NAUX NCPL') then
1467
1468 if (iper == 0) then
1469 select case (idt%shape)
1470 case ('NROW')
1471 axis_sz = dim_ids%y
1472 case ('NCOL')
1473 axis_sz = dim_ids%x
1474 end select
1475
1476 longname = export_longname(idt%longname, pkgname, idt%tagname, mempath, &
1477 component_type=idt%component_type, &
1478 subcomponent_type=idt%subcomponent_type)
1479
1480 ! reenter define mode and create variable
1481 call nf_verify(nf90_redef(ncid), nc_fname)
1482 call nf_verify(nf90_def_var(ncid, varname, nf90_int, &
1483 (/axis_sz/), var_id), &
1484 nc_fname)
1485
1486 ! NROW/NCOL shapes use default chunking
1487 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1488
1489 ! put attr
1490 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1491 (/nf90_fill_int/)), nc_fname)
1492 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1493 longname), nc_fname)
1494
1495 ! add mf6 attr
1496 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1497
1498 ! exit define mode and write data
1499 call nf_verify(nf90_enddef(ncid), nc_fname)
1500 call nf_verify(nf90_put_var(ncid, var_id, p_mem), &
1501 nc_fname)
1502 else
1503 ! timeseries
1504 call nf_verify(nf90_put_var(ncid, &
1505 var_ids%export, p_mem, &
1506 start=(/1, 1, kper/), &
1507 count=(/dis%ncol, dis%nrow, 1/)), nc_fname)
1508 end if
1509
1510 else
1511
1512 if (iper == 0) then
1513 ! reenter define mode and create variable
1514 call nf_verify(nf90_redef(ncid), nc_fname)
1515 call nf_verify(nf90_def_var(ncid, varname, nf90_int, &
1516 (/dim_ids%x, dim_ids%y, dim_ids%layer/), &
1517 var_id), nc_fname)
1518
1519 ! apply chunking parameters
1520 call ncvar_chunk3d(ncid, var_id, chunk_x, chunk_y, chunk_z, nc_fname)
1521 ! deflate and shuffle
1522 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1523
1524 ! put attr
1525 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1526 (/nf90_fill_int/)), nc_fname)
1527 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1528 idt%longname), nc_fname)
1529
1530 ! add grid mapping and mf6 attr
1531 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .true., &
1532 nc_fname)
1533 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1534
1535 ! exit define mode and write data
1536 call nf_verify(nf90_enddef(ncid), nc_fname)
1537 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1, 1/), &
1538 count=(/dis%ncol, dis%nrow, dis%nlay/)), &
1539 nc_fname)
1540 else
1541 ! timeseries
1542 call nf_verify(nf90_put_var(ncid, &
1543 var_ids%export, p_mem, &
1544 start=(/1, 1, 1, kper/), &
1545 count=(/dis%ncol, dis%nrow, dis%nlay, 1/)), &
1546 nc_fname)
1547 end if
1548 end if
1549 end subroutine nc_export_int1d
1550
1551 !> @brief netcdf export 2D integer
1552 !<
1553 subroutine nc_export_int2d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1554 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1555 shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
1556 integer(I4B), dimension(:, :), pointer, contiguous, intent(in) :: p_mem
1557 integer(I4B), intent(in) :: ncid
1558 type(structuredncdimidtype), intent(inout) :: dim_ids
1559 type(structuredncvaridtype), intent(inout) :: var_ids
1560 type(distype), pointer, intent(in) :: dis
1561 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1562 character(len=*), intent(in) :: mempath
1563 character(len=*), intent(in) :: nc_tag
1564 character(len=*), intent(in) :: pkgname
1565 character(len=*), intent(in) :: gridmap_name
1566 logical(LGP), intent(in) :: latlon
1567 integer(I4B), intent(in) :: deflate
1568 integer(I4B), intent(in) :: shuffle
1569 integer(I4B), intent(in) :: chunk_z
1570 integer(I4B), intent(in) :: chunk_y
1571 integer(I4B), intent(in) :: chunk_x
1572 character(len=*), intent(in) :: nc_fname
1573 character(len=LINELENGTH) :: varname
1574 integer(I4B) :: var_id
1575
1576 varname = export_varname(pkgname, idt%tagname, mempath)
1577
1578 ! reenter define mode and create variable
1579 call nf_verify(nf90_redef(ncid), nc_fname)
1580 call nf_verify(nf90_def_var(ncid, varname, nf90_int, &
1581 (/dim_ids%x, dim_ids%y/), var_id), &
1582 nc_fname)
1583
1584 ! apply chunking parameters
1585 call ncvar_chunk2d(ncid, var_id, chunk_x, chunk_y, nc_fname)
1586 ! deflate and shuffle
1587 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1588
1589 ! put attr
1590 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1591 (/nf90_fill_int/)), nc_fname)
1592 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1593 idt%longname), nc_fname)
1594
1595 ! add grid mapping and mf6 attr
1596 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .false., &
1597 nc_fname)
1598 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1599
1600 ! exit define mode and write data
1601 call nf_verify(nf90_enddef(ncid), nc_fname)
1602 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1/), &
1603 count=(/dis%ncol, dis%nrow/)), &
1604 nc_fname)
1605 end subroutine nc_export_int2d
1606
1607 !> @brief netcdf export 3D integer
1608 !<
1609 subroutine nc_export_int3d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1610 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1611 shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
1612 integer(I4B), dimension(:, :, :), pointer, contiguous, intent(in) :: p_mem
1613 integer(I4B), intent(in) :: ncid
1614 type(structuredncdimidtype), intent(inout) :: dim_ids
1615 type(structuredncvaridtype), intent(inout) :: var_ids
1616 type(distype), pointer, intent(in) :: dis
1617 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1618 character(len=*), intent(in) :: mempath
1619 character(len=*), intent(in) :: nc_tag
1620 character(len=*), intent(in) :: pkgname
1621 character(len=*), intent(in) :: gridmap_name
1622 logical(LGP), intent(in) :: latlon
1623 integer(I4B), intent(in) :: deflate
1624 integer(I4B), intent(in) :: shuffle
1625 integer(I4B), intent(in) :: chunk_z
1626 integer(I4B), intent(in) :: chunk_y
1627 integer(I4B), intent(in) :: chunk_x
1628 character(len=*), intent(in) :: nc_fname
1629 character(len=LINELENGTH) :: varname
1630 integer(I4B) :: var_id
1631
1632 varname = export_varname(pkgname, idt%tagname, mempath)
1633
1634 ! reenter define mode and create variable
1635 call nf_verify(nf90_redef(ncid), nc_fname)
1636 call nf_verify(nf90_def_var(ncid, varname, nf90_int, &
1637 (/dim_ids%x, dim_ids%y, dim_ids%layer/), &
1638 var_id), nc_fname)
1639
1640 ! apply chunking parameters
1641 call ncvar_chunk3d(ncid, var_id, chunk_x, chunk_y, chunk_z, nc_fname)
1642 ! deflate and shuffle
1643 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1644
1645 ! put attr
1646 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1647 (/nf90_fill_int/)), nc_fname)
1648 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1649 idt%longname), nc_fname)
1650
1651 ! add grid mapping and mf6 attr
1652 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .true., &
1653 nc_fname)
1654 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1655
1656 ! exit define mode and write data
1657 call nf_verify(nf90_enddef(ncid), nc_fname)
1658 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1, 1/), &
1659 count=(/dis%ncol, dis%nrow, dis%nlay/)), &
1660 nc_fname)
1661 end subroutine nc_export_int3d
1662
1663 !> @brief netcdf export 1D double
1664 !<
1665 subroutine nc_export_dbl1d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1666 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1667 shuffle, chunk_z, chunk_y, chunk_x, iper, iaux, &
1668 nc_fname)
1669 use tdismodule, only: kper
1670 real(DP), dimension(:), pointer, contiguous, intent(in) :: p_mem
1671 integer(I4B), intent(in) :: ncid
1672 type(structuredncdimidtype), intent(inout) :: dim_ids
1673 type(structuredncvaridtype), intent(inout) :: var_ids
1674 type(distype), pointer, intent(in) :: dis
1675 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1676 character(len=*), intent(in) :: mempath
1677 character(len=*), intent(in) :: nc_tag
1678 character(len=*), intent(in) :: pkgname
1679 character(len=*), intent(in) :: gridmap_name
1680 logical(LGP), intent(in) :: latlon
1681 integer(I4B), intent(in) :: deflate
1682 integer(I4B), intent(in) :: shuffle
1683 integer(I4B), intent(in) :: chunk_z
1684 integer(I4B), intent(in) :: chunk_y
1685 integer(I4B), intent(in) :: chunk_x
1686 integer(I4B), intent(in) :: iper
1687 integer(I4B), intent(in) :: iaux
1688 character(len=*), intent(in) :: nc_fname
1689 integer(I4B) :: var_id, axis_sz
1690 character(len=LINELENGTH) :: varname, longname
1691
1692 if (idt%shape == 'NROW' .or. &
1693 idt%shape == 'NCOL' .or. &
1694 idt%shape == 'NCPL' .or. &
1695 idt%shape == 'NAUX NCPL') then
1696
1697 if (iper == 0) then
1698 select case (idt%shape)
1699 case ('NROW')
1700 axis_sz = dim_ids%y
1701 case ('NCOL')
1702 axis_sz = dim_ids%x
1703 end select
1704
1705 varname = export_varname(pkgname, idt%tagname, mempath)
1706 longname = export_longname(idt%longname, pkgname, idt%tagname, mempath, &
1707 iaux=iaux, &
1708 component_type=idt%component_type, &
1709 subcomponent_type=idt%subcomponent_type)
1710
1711 ! reenter define mode and create variable
1712 call nf_verify(nf90_redef(ncid), nc_fname)
1713 call nf_verify(nf90_def_var(ncid, varname, nf90_double, &
1714 (/axis_sz/), var_id), &
1715 nc_fname)
1716
1717 ! NROW/NCOL shapes use default chunking
1718 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1719
1720 ! put attr
1721 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1722 (/nf90_fill_double/)), nc_fname)
1723 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1724 longname), nc_fname)
1725
1726 ! add mf6 attr
1727 call ncvar_mf6attr(ncid, var_id, iaux, nc_tag, nc_fname)
1728
1729 ! exit define mode and write data
1730 call nf_verify(nf90_enddef(ncid), nc_fname)
1731 call nf_verify(nf90_put_var(ncid, var_id, p_mem), &
1732 nc_fname)
1733 else
1734 ! timeseries
1735 call nf_verify(nf90_put_var(ncid, &
1736 var_ids%export, p_mem, &
1737 start=(/1, 1, kper/), &
1738 count=(/dis%ncol, dis%nrow, 1/)), nc_fname)
1739 end if
1740
1741 else
1742
1743 if (iper == 0) then
1744 varname = export_varname(pkgname, idt%tagname, mempath, iaux=iaux)
1745 longname = export_longname(idt%longname, pkgname, idt%tagname, mempath, &
1746 iaux=iaux, &
1747 component_type=idt%component_type, &
1748 subcomponent_type=idt%subcomponent_type)
1749
1750 ! reenter define mode and create variable
1751 call nf_verify(nf90_redef(ncid), nc_fname)
1752 call nf_verify(nf90_def_var(ncid, varname, nf90_double, &
1753 (/dim_ids%x, dim_ids%y, dim_ids%layer/), &
1754 var_id), nc_fname)
1755
1756 ! apply chunking parameters
1757 call ncvar_chunk3d(ncid, var_id, chunk_x, chunk_y, chunk_z, nc_fname)
1758 ! deflate and shuffle
1759 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1760
1761 ! put attr
1762 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1763 (/nf90_fill_double/)), nc_fname)
1764 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1765 longname), nc_fname)
1766
1767 ! add grid mapping and mf6 attr
1768 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .true., &
1769 nc_fname)
1770 call ncvar_mf6attr(ncid, var_id, iaux, nc_tag, nc_fname)
1771
1772 ! exit define mode and write data
1773 call nf_verify(nf90_enddef(ncid), nc_fname)
1774 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1, 1/), &
1775 count=(/dis%ncol, dis%nrow, dis%nlay/)), &
1776 nc_fname)
1777 else
1778 ! timeseries
1779 call nf_verify(nf90_put_var(ncid, &
1780 var_ids%export, p_mem, &
1781 start=(/1, 1, 1, kper/), &
1782 count=(/dis%ncol, dis%nrow, dis%nlay, 1/)), &
1783 nc_fname)
1784 end if
1785 end if
1786 end subroutine nc_export_dbl1d
1787
1788 !> @brief netcdf export 2D double
1789 !<
1790 subroutine nc_export_dbl2d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1791 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1792 shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
1793 real(DP), dimension(:, :), pointer, contiguous, intent(in) :: p_mem
1794 integer(I4B), intent(in) :: ncid
1795 type(structuredncdimidtype), intent(inout) :: dim_ids
1796 type(structuredncvaridtype), intent(inout) :: var_ids
1797 type(distype), pointer, intent(in) :: dis
1798 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1799 character(len=*), intent(in) :: mempath
1800 character(len=*), intent(in) :: nc_tag
1801 character(len=*), intent(in) :: pkgname
1802 character(len=*), intent(in) :: gridmap_name
1803 logical(LGP), intent(in) :: latlon
1804 integer(I4B), intent(in) :: deflate
1805 integer(I4B), intent(in) :: shuffle
1806 integer(I4B), intent(in) :: chunk_z
1807 integer(I4B), intent(in) :: chunk_y
1808 integer(I4B), intent(in) :: chunk_x
1809 character(len=*), intent(in) :: nc_fname
1810 character(len=LINELENGTH) :: varname
1811 integer(I4B) :: var_id
1812
1813 varname = export_varname(pkgname, idt%tagname, mempath)
1814
1815 ! reenter define mode and create variable
1816 call nf_verify(nf90_redef(ncid), nc_fname)
1817 call nf_verify(nf90_def_var(ncid, varname, nf90_double, &
1818 (/dim_ids%x, dim_ids%y/), var_id), &
1819 nc_fname)
1820
1821 ! apply chunking parameters
1822 call ncvar_chunk2d(ncid, var_id, chunk_x, chunk_y, nc_fname)
1823 ! deflate and shuffle
1824 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1825
1826 ! put attr
1827 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1828 (/nf90_fill_double/)), nc_fname)
1829 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1830 idt%longname), nc_fname)
1831
1832 ! add grid mapping and mf6 attr
1833 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .false., &
1834 nc_fname)
1835 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1836
1837 ! exit define mode and write data
1838 call nf_verify(nf90_enddef(ncid), nc_fname)
1839 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1/), &
1840 count=(/dis%ncol, dis%nrow/)), &
1841 nc_fname)
1842 end subroutine nc_export_dbl2d
1843
1844 !> @brief netcdf export 3D double
1845 !<
1846 subroutine nc_export_dbl3d(p_mem, ncid, dim_ids, var_ids, dis, idt, mempath, &
1847 nc_tag, pkgname, gridmap_name, latlon, deflate, &
1848 shuffle, chunk_z, chunk_y, chunk_x, nc_fname)
1849 real(DP), dimension(:, :, :), pointer, contiguous, intent(in) :: p_mem
1850 integer(I4B), intent(in) :: ncid
1851 type(structuredncdimidtype), intent(inout) :: dim_ids
1852 type(structuredncvaridtype), intent(inout) :: var_ids
1853 type(distype), pointer, intent(in) :: dis
1854 type(inputparamdefinitiontype), pointer, intent(in) :: idt
1855 character(len=*), intent(in) :: mempath
1856 character(len=*), intent(in) :: nc_tag
1857 character(len=*), intent(in) :: pkgname
1858 character(len=*), intent(in) :: gridmap_name
1859 logical(LGP), intent(in) :: latlon
1860 integer(I4B), intent(in) :: deflate
1861 integer(I4B), intent(in) :: shuffle
1862 integer(I4B), intent(in) :: chunk_z
1863 integer(I4B), intent(in) :: chunk_y
1864 integer(I4B), intent(in) :: chunk_x
1865 character(len=*), intent(in) :: nc_fname
1866 integer(I4B) :: var_id
1867 character(len=LINELENGTH) :: varname, longname
1868
1869 varname = export_varname(pkgname, idt%tagname, mempath)
1870 longname = export_longname(idt%longname, pkgname, idt%tagname, mempath, &
1871 component_type=idt%component_type, &
1872 subcomponent_type=idt%subcomponent_type)
1873
1874 ! reenter define mode and create variable
1875 call nf_verify(nf90_redef(ncid), nc_fname)
1876 call nf_verify(nf90_def_var(ncid, varname, nf90_double, &
1877 (/dim_ids%x, dim_ids%y, dim_ids%layer/), &
1878 var_id), nc_fname)
1879
1880 ! apply chunking parameters
1881 call ncvar_chunk3d(ncid, var_id, chunk_x, chunk_y, chunk_z, nc_fname)
1882 ! deflate and shuffle
1883 call ncvar_deflate(ncid, var_id, deflate, shuffle, nc_fname)
1884
1885 ! put attr
1886 call nf_verify(nf90_put_att(ncid, var_id, '_FillValue', &
1887 (/nf90_fill_double/)), nc_fname)
1888 call nf_verify(nf90_put_att(ncid, var_id, 'long_name', &
1889 longname), nc_fname)
1890
1891 ! add grid mapping and mf6 attr
1892 call ncvar_gridmap(ncid, var_id, gridmap_name, latlon, .true., &
1893 nc_fname)
1894 call ncvar_mf6attr(ncid, var_id, 0, nc_tag, nc_fname)
1895
1896 ! exit define mode and write data
1897 call nf_verify(nf90_enddef(ncid), nc_fname)
1898 call nf_verify(nf90_put_var(ncid, var_id, p_mem, start=(/1, 1, 1/), &
1899 count=(/dis%ncol, dis%nrow, dis%nlay/)), &
1900 nc_fname)
1901 end subroutine nc_export_dbl3d
1902
1903end 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)
@ brief 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:237
subroutine, public store_error(msg, terminate)
Store an error message.
Definition Sim.f90:92
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition Sim.f90:204
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
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