Skip to content

Commit fc10d2a

Browse files
mjrenomjreno
authored andcommitted
add structured netcdf grid write support
1 parent cf516d6 commit fc10d2a

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

autotest/test_netcdf_gwf_vsc03_sfr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def check_output(idx, test, export, gridded_input):
230230
auxarr = xds["rcha-1_auxvar_l1_p1a1"].data.flatten()
231231
elif export == "structured":
232232
rarr = xds["rcha-1_recharge_p1"].data[0].flatten()
233-
auxarr = xds["rcha-1_auxvar_p1a1"].data[0].flatten()
233+
auxarr = xds["rcha-1_temperature_p1"].data[0].flatten()
234234
assert np.allclose(
235235
np.array(irch[0]).flatten() + 1,
236236
xds["rcha-1_irch_p1"].data,

src/Utilities/Export/DisNCStructured.f90

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ subroutine export_input_array(this, pkgtype, pkgname, mempath, idt)
281281
iaux = 0
282282

283283
! set variable name and input attribute string
284-
nc_varname = export_varname(pkgname, idt)
284+
nc_varname = export_varname(pkgname, idt%mf6varname)
285285
input_attr = this%input_attribute(pkgname, idt)
286286

287287
select case (idt%datatype)
@@ -411,8 +411,8 @@ subroutine package_step_ilayer(this, export_pkg, ilayer_varname, ilayer)
411411
'PERIOD', export_pkg%param_names(iparam), &
412412
this%nc_fname)
413413
! set variable name and input attrs
414-
nc_varname = export_varname(export_pkg%mf6_input%subcomponent_name, idt, &
415-
iper=kper)
414+
nc_varname = export_varname(export_pkg%mf6_input%subcomponent_name, &
415+
idt%mf6varname, iper=kper)
416416
input_attr = this%input_attribute(export_pkg%mf6_input%subcomponent_name, &
417417
idt)
418418
! export arrays
@@ -457,13 +457,19 @@ subroutine package_step(this, export_pkg)
457457
use TdisModule, only: kper
458458
use NCModelExportModule, only: ExportPackageType
459459
use DefinitionSelectModule, only: get_param_definition_type
460+
use ConstantsModule, only: DNODATA, LENAUXNAME
460461
class(DisNCStructuredType), intent(inout) :: this
461462
class(ExportPackageType), pointer, intent(in) :: export_pkg
462463
integer(I4B), dimension(:), pointer, contiguous :: int1d
463464
real(DP), dimension(:), pointer, contiguous :: dbl1d
465+
real(DP), dimension(:, :), pointer, contiguous :: dbl2d
466+
real(DP), dimension(:, :, :), pointer, contiguous :: dbl3d
464467
type(InputParamDefinitionType), pointer :: idt
465468
character(len=LINELENGTH) :: nc_varname, input_attr
466-
integer(I4B) :: iparam
469+
character(len=LENAUXNAME) :: aux
470+
type(CharacterStringType), dimension(:), pointer, &
471+
contiguous :: auxname_cst
472+
integer(I4B) :: iparam, n
467473

468474
do iparam = 1, export_pkg%nparam
469475
! set input definition
@@ -474,8 +480,8 @@ subroutine package_step(this, export_pkg)
474480
this%nc_fname)
475481

476482
! set variable name and input attribute string
477-
nc_varname = export_varname(export_pkg%mf6_input%subcomponent_name, idt, &
478-
iper=kper)
483+
nc_varname = export_varname(export_pkg%mf6_input%subcomponent_name, &
484+
idt%mf6varname, iper=kper)
479485
input_attr = this%input_attribute(export_pkg%mf6_input%subcomponent_name, &
480486
idt)
481487

@@ -501,6 +507,26 @@ subroutine package_step(this, export_pkg)
501507
this%gridmap_name, this%latlon, this%deflate, &
502508
this%shuffle, this%chunk_z, this%chunk_y, &
503509
this%chunk_x, kper, this%nc_fname)
510+
case ('DOUBLE2D')
511+
call mem_setptr(dbl2d, idt%mf6varname, export_pkg%mf6_input%mempath)
512+
call mem_setptr(auxname_cst, 'AUXILIARY', export_pkg%mf6_input%mempath)
513+
do n = 1, size(dbl2d, dim=1) ! naux
514+
! reset varname to auxname
515+
aux = auxname_cst(n)
516+
nc_varname = export_varname(export_pkg%mf6_input%subcomponent_name, &
517+
aux, iper=kper)
518+
519+
! export the 1d array as a structured 3d array
520+
dbl3d(1:export_pkg%mshape(3), 1:export_pkg%mshape(2), &
521+
1:export_pkg%mshape(1)) => dbl2d(n, :)
522+
call nc_export_array(this%ncid, this%dim_ids, this%var_ids, &
523+
this%dis, dbl3d, nc_varname, &
524+
export_pkg%mf6_input%subcomponent_name, &
525+
aux, 'NCOL NROW NLAY', idt%longname, input_attr, &
526+
this%gridmap_name, this%latlon, this%deflate, &
527+
this%shuffle, this%chunk_z, this%chunk_y, &
528+
this%chunk_x, kper, n, this%nc_fname)
529+
end do
504530
case default
505531
errmsg = 'EXPORT unsupported datatype='//trim(idt%datatype)
506532
call store_error(errmsg, .true.)
@@ -515,7 +541,7 @@ end subroutine package_step
515541
!<
516542
subroutine export_layer_3d(this, export_pkg, idt, ilayer_read, ialayer, &
517543
dbl1d, nc_varname, input_attr, iaux)
518-
use ConstantsModule, only: DNODATA, DZERO
544+
use ConstantsModule, only: DNODATA, DZERO, LENAUXNAME
519545
use TdisModule, only: kper
520546
use NCModelExportModule, only: ExportPackageType
521547
class(DisNCStructuredType), intent(inout) :: this
@@ -530,12 +556,17 @@ subroutine export_layer_3d(this, export_pkg, idt, ilayer_read, ialayer, &
530556
real(DP), dimension(:, :, :), pointer, contiguous :: dbl3d
531557
integer(I4B) :: n, i, j, k, nvals, idxaux
532558
real(DP), dimension(:, :), contiguous, pointer :: dbl2d_ptr
559+
character(len=LENAUXNAME) :: aux
560+
type(CharacterStringType), dimension(:), pointer, &
561+
contiguous :: auxname_cst
533562

534563
! initialize
535564
idxaux = 0
536565
if (present(iaux)) then
566+
call mem_setptr(auxname_cst, 'AUXILIARY', export_pkg%mf6_input%mempath)
567+
aux = auxname_cst(iaux)
537568
nc_varname = export_varname(export_pkg%mf6_input%subcomponent_name, &
538-
idt, iper=kper, iaux=iaux)
569+
aux, iper=kper)
539570
idxaux = iaux
540571
end if
541572

@@ -1454,27 +1485,21 @@ end subroutine nc_export_dbl3d
14541485

14551486
!> @brief build netcdf variable name
14561487
!<
1457-
function export_varname(pkgname, idt, iper, iaux) result(varname)
1488+
function export_varname(pkgname, varname, iper) result(fullname)
14581489
use InputOutputModule, only: lowcase
14591490
character(len=*), intent(in) :: pkgname
1460-
type(InputParamDefinitionType), pointer, intent(in) :: idt
1491+
character(len=*), intent(in) :: varname
14611492
integer(I4B), optional, intent(in) :: iper
1462-
integer(I4B), optional, intent(in) :: iaux
1463-
character(len=LINELENGTH) :: varname
1493+
character(len=LINELENGTH) :: fullname
14641494
character(len=LINELENGTH) :: pname, vname
14651495
pname = pkgname
1466-
vname = idt%mf6varname
1496+
vname = varname
14671497
call lowcase(pname)
14681498
call lowcase(vname)
14691499
if (present(iper)) then
1470-
if (present(iaux)) then
1471-
write (varname, '(a,i0,a,i0)') trim(pname)//'_'//trim(vname)// &
1472-
'_p', iper, 'a', iaux
1473-
else
1474-
write (varname, '(a,i0)') trim(pname)//'_'//trim(vname)//'_p', iper
1475-
end if
1500+
write (fullname, '(a,i0)') trim(pname)//'_'//trim(vname)//'_p', iper
14761501
else
1477-
varname = trim(pname)//'_'//trim(vname)
1502+
fullname = trim(pname)//'_'//trim(vname)
14781503
end if
14791504
end function export_varname
14801505

src/Utilities/Export/NCExportCreate.f90

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ subroutine create_export_pkglist(pkglist, loaders, iout)
144144
use InputLoadTypeModule, only: ModelDynamicPkgsType
145145
use InputLoadTypeModule, only: DynamicPkgLoadBaseType
146146
use AsciiInputLoadTypeModule, only: AsciiDynamicPkgLoadBaseType
147-
use Mf6FileGridInputModule, only: BoundGridInputType
147+
use LayerArrayLoadModule, only: LayerArrayLoadType
148+
use GridArrayLoadModule, only: GridArrayLoadType
148149
use IdmMf6FileModule, only: Mf6FileDynamicPkgLoadType
149150
type(ListType), intent(inout) :: pkglist
150151
type(ModelDynamicPkgsType), pointer, intent(in) :: loaders
@@ -154,7 +155,7 @@ subroutine create_export_pkglist(pkglist, loaders, iout)
154155
type(ExportPackageType), pointer :: export_pkg
155156
integer(I4B), pointer :: export_arrays
156157
class(*), pointer :: obj
157-
logical(LGP) :: found
158+
logical(LGP) :: found, readasarrays
158159
integer(I4B) :: n
159160

160161
! create list of in scope loaders
@@ -170,12 +171,21 @@ subroutine create_export_pkglist(pkglist, loaders, iout)
170171
call mem_set_value(export_arrays, 'EXPORT_NC', &
171172
dynamic_pkg%mf6_input%mempath, found)
172173

173-
if (export_arrays > 0 .and. dynamic_pkg%readasarrays) then
174+
readasarrays = (dynamic_pkg%readarray_layer .or. dynamic_pkg%readarray_grid)
175+
if (export_arrays > 0 .and. readasarrays) then
174176
select type (dynamic_pkg)
175177
type is (Mf6FileDynamicPkgLoadType)
176178
rp_loader => dynamic_pkg%rp_loader
177179
select type (rp_loader)
178-
type is (BoundGridInputType)
180+
type is (LayerArrayLoadType)
181+
! create the export object
182+
allocate (export_pkg)
183+
call export_pkg%init(rp_loader%mf6_input, &
184+
rp_loader%bound_context%mshape, &
185+
rp_loader%param_names, rp_loader%nparam)
186+
obj => export_pkg
187+
call pkglist%add(obj)
188+
type is (GridArrayLoadType)
179189
! create the export object
180190
allocate (export_pkg)
181191
call export_pkg%init(rp_loader%mf6_input, &

0 commit comments

Comments
 (0)