Skip to content

Commit 7b2a14e

Browse files
mjrenomjreno
authored andcommitted
add netcdf read and write support
1 parent aa0b7de commit 7b2a14e

8 files changed

Lines changed: 625 additions & 568 deletions

File tree

autotest/test_netcdf_gwf_vsc03_sfr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def check_output(idx, test, export, gridded_input):
227227
aux = getattr(rch, "aux").array
228228
if export == "ugrid":
229229
rarr = xds["rcha-1_recharge_l1_p1"].data.flatten()
230-
auxarr = xds["rcha-1_auxvar_l1_p1a1"].data.flatten()
230+
auxarr = xds["rcha-1_temperature_l1_p1"].data.flatten()
231231
elif export == "structured":
232232
rarr = xds["rcha-1_recharge_p1"].data[0].flatten()
233233
auxarr = xds["rcha-1_temperature_p1"].data[0].flatten()

src/Utilities/Export/DisNCMesh.f90

Lines changed: 207 additions & 164 deletions
Large diffs are not rendered by default.

src/Utilities/Export/DisNCStructured.f90

Lines changed: 147 additions & 199 deletions
Large diffs are not rendered by default.

src/Utilities/Export/DisvNCMesh.f90

Lines changed: 182 additions & 135 deletions
Large diffs are not rendered by default.

src/Utilities/Export/MeshNCModel.f90

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ module MeshModelModule
2626
public :: ncvar_deflate
2727
public :: ncvar_gridmap
2828
public :: ncvar_mf6attr
29-
public :: export_varname
3029

3130
!> @brief type for storing model export dimension ids
3231
!<
@@ -564,35 +563,4 @@ subroutine ncvar_mf6attr(ncid, varid, layer, iper, iaux, nc_tag, nc_fname)
564563
end if
565564
end subroutine ncvar_mf6attr
566565

567-
!> @brief build netcdf variable name
568-
!<
569-
function export_varname(varname, layer, iper, iaux) result(vname)
570-
use InputOutputModule, only: lowcase
571-
character(len=*), intent(in) :: varname
572-
integer(I4B), optional, intent(in) :: layer
573-
integer(I4B), optional, intent(in) :: iper
574-
integer(I4B), optional, intent(in) :: iaux
575-
character(len=LINELENGTH) :: vname
576-
vname = ''
577-
if (varname /= '') then
578-
vname = varname
579-
call lowcase(vname)
580-
if (present(layer)) then
581-
if (layer > 0) then
582-
write (vname, '(a,i0)') trim(vname)//'_l', layer
583-
end if
584-
end if
585-
if (present(iper)) then
586-
if (iper > 0) then
587-
write (vname, '(a,i0)') trim(vname)//'_p', iper
588-
end if
589-
end if
590-
if (present(iaux)) then
591-
if (iaux > 0) then
592-
write (vname, '(a,i0)') trim(vname)//'a', iaux
593-
end if
594-
end if
595-
end if
596-
end function export_varname
597-
598566
end module MeshModelModule

src/Utilities/Export/NCModel.f90

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module NCModelExportModule
2424
public :: NCExportAnnotation
2525
public :: ExportPackageType
2626
public :: NETCDF_UNDEF, NETCDF_STRUCTURED, NETCDF_MESH2D
27-
public :: export_longname
27+
public :: export_longname, export_varname
2828

2929
!> @brief netcdf export types enumerator
3030
!<
@@ -410,14 +410,62 @@ function input_attribute(this, pkgname, idt) result(attr)
410410
end if
411411
end function input_attribute
412412

413+
!> @brief build netcdf variable name
414+
!<
415+
function export_varname(pkgname, tagname, mempath, layer, iper, iaux) &
416+
result(varname)
417+
use MemoryManagerModule, only: mem_setptr
418+
use CharacterStringModule, only: CharacterStringType
419+
use InputOutputModule, only: lowcase
420+
character(len=*), intent(in) :: pkgname
421+
character(len=*), intent(in) :: tagname
422+
character(len=*), intent(in) :: mempath
423+
integer(I4B), optional, intent(in) :: layer
424+
integer(I4B), optional, intent(in) :: iper
425+
integer(I4B), optional, intent(in) :: iaux
426+
character(len=LINELENGTH) :: varname
427+
type(CharacterStringType), dimension(:), pointer, &
428+
contiguous :: auxnames
429+
character(len=LINELENGTH) :: pname, vname
430+
vname = tagname
431+
pname = pkgname
432+
433+
if (present(iaux)) then
434+
if (iaux > 0) then
435+
if (tagname == 'AUX') then
436+
! reset vname to auxiliary variable name
437+
call mem_setptr(auxnames, 'AUXILIARY', mempath)
438+
vname = auxnames(iaux)
439+
end if
440+
end if
441+
end if
442+
443+
call lowcase(vname)
444+
call lowcase(pname)
445+
varname = trim(pname)//'_'//trim(vname)
446+
447+
if (present(layer)) then
448+
if (layer > 0) then
449+
!write (varname, '(a,i0)') trim(varname)//'_L', layer
450+
write (varname, '(a,i0)') trim(varname)//'_l', layer
451+
end if
452+
end if
453+
if (present(iper)) then
454+
if (iper > 0) then
455+
!write (varname, '(a,i0)') trim(varname)//'_SP', iper
456+
write (varname, '(a,i0)') trim(varname)//'_p', iper
457+
end if
458+
end if
459+
end function export_varname
460+
413461
!> @brief build netcdf variable longname
414462
!<
415463
function export_longname(longname, pkgname, tagname, layer, iper) result(lname)
416464
use InputOutputModule, only: lowcase
417465
character(len=*), intent(in) :: longname
418466
character(len=*), intent(in) :: pkgname
419467
character(len=*), intent(in) :: tagname
420-
integer(I4B), intent(in) :: layer
468+
integer(I4B), optional, intent(in) :: layer
421469
integer(I4B), optional, intent(in) :: iper
422470
character(len=LINELENGTH) :: lname
423471
character(len=LINELENGTH) :: pname, vname
@@ -430,8 +478,10 @@ function export_longname(longname, pkgname, tagname, layer, iper) result(lname)
430478
else
431479
lname = longname
432480
end if
433-
if (layer > 0) then
434-
write (lname, '(a,i0)') trim(lname)//' layer=', layer
481+
if (present(layer)) then
482+
if (layer > 0) then
483+
write (lname, '(a,i0)') trim(lname)//' layer=', layer
484+
end if
435485
end if
436486
if (present(iper)) then
437487
if (iper > 0) then

src/Utilities/Idm/netcdf/NCArrayReader.f90

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,11 @@ subroutine load_double1d_spd(dbl1d, mf6_input, mshape, idt, nc_vars, &
462462
character(len=*), intent(in) :: input_fname
463463
integer(I4B), optional, intent(in) :: iaux
464464
real(DP), dimension(:, :, :), contiguous, pointer :: dbl3d
465-
integer(I4B) :: nvals, varid
465+
integer(I4B) :: varid
466466
integer(I4B) :: n, i, j, k
467467

468468
! initialize
469-
nvals = 0
469+
n = 0
470470

471471
! set varid
472472
if (present(iaux)) then
@@ -475,20 +475,22 @@ subroutine load_double1d_spd(dbl1d, mf6_input, mshape, idt, nc_vars, &
475475
varid = nc_vars%varid(idt%mf6varname, period=iper)
476476
end if
477477

478-
if (idt%shape == 'NODES') then
479-
! TODO future support
480-
write (errmsg, '(a)') &
481-
'IDM NetCDF load_double1d_spd NODES var shape not supported => '// &
482-
trim(idt%tagname)
483-
call store_error(errmsg)
484-
call store_error_filename(input_fname)
485-
else if (idt%shape == 'NCPL' .or. idt%shape == 'NAUX NCPL') then
478+
if (size(mshape) == 3) then
479+
allocate (dbl3d(mshape(3), mshape(2), mshape(1)))
480+
call nf_verify(nf90_get_var(nc_vars%ncid, varid, dbl3d), &
481+
nc_vars%nc_fname)
486482

487-
if (size(mshape) == 3) then
488-
allocate (dbl3d(mshape(3), mshape(2), mshape(1)))
489-
call nf_verify(nf90_get_var(nc_vars%ncid, varid, dbl3d), &
490-
nc_vars%nc_fname)
491-
n = 0
483+
if (idt%shape == 'NODES' .or. idt%shape == 'NAUX NODES') then
484+
do k = 1, size(dbl3d, dim=3)
485+
do i = 1, size(dbl3d, dim=2)
486+
do j = 1, size(dbl3d, dim=1)
487+
n = n + 1
488+
dbl1d(n) = dbl3d(j, i, k)
489+
end do
490+
end do
491+
end do
492+
493+
else if (idt%shape == 'NCPL' .or. idt%shape == 'NAUX NCPL') then
492494
do k = 1, size(dbl3d, dim=3)
493495
do i = 1, size(dbl3d, dim=2)
494496
do j = 1, size(dbl3d, dim=1)
@@ -503,15 +505,10 @@ subroutine load_double1d_spd(dbl1d, mf6_input, mshape, idt, nc_vars, &
503505
end do
504506
end do
505507
end do
506-
507-
else if (size(mshape) == 2) then
508-
! TODO
509-
write (errmsg, '(a)') &
510-
'IDM NetCDF load_double1d_spd DISV model not supported => '// &
511-
trim(idt%tagname)
512-
call store_error(errmsg)
513-
call store_error_filename(input_fname)
514508
end if
509+
510+
! clean up
511+
deallocate (dbl3d)
515512
end if
516513
end subroutine load_double1d_spd
517514

@@ -561,29 +558,33 @@ subroutine load_double1d_layered_spd(dbl1d, mf6_input, mshape, idt, nc_vars, &
561558
integer(I4B), optional, intent(in) :: iaux
562559
integer(I4B), dimension(:), allocatable :: layer_shape
563560
integer(I4B) :: nlay, varid
564-
integer(I4B) :: k, n, ncpl
565-
integer(I4B) :: index_start, index_stop
561+
integer(I4B) :: k, n, ncpl, idx
566562
real(DP), dimension(:), contiguous, pointer :: dbl1d_ptr
567563

568564
call get_layered_shape(mshape, nlay, layer_shape)
569565
ncpl = product(layer_shape)
570566
allocate (dbl1d_ptr(ncpl))
571567

572568
do k = 1, nlay
573-
index_start = 1
574-
index_stop = index_start + ncpl - 1
575569
if (present(iaux)) then
576570
varid = nc_vars%varid(idt%mf6varname, layer=k, period=iper, iaux=iaux)
577571
else
578572
varid = nc_vars%varid(idt%mf6varname, layer=k, period=iper)
579573
end if
580574
call nf_verify(nf90_get_var(nc_vars%ncid, varid, dbl1d_ptr), &
581575
nc_vars%nc_fname)
582-
do n = 1, ncpl
583-
if (dbl1d_ptr(n) /= DNODATA) then
584-
dbl1d(n) = dbl1d_ptr(n)
585-
end if
586-
end do
576+
if (idt%shape == 'NODES' .or. idt%shape == 'NAUX NODES') then
577+
do n = 1, ncpl
578+
idx = (k - 1) * ncpl + n
579+
dbl1d(idx) = dbl1d_ptr(n)
580+
end do
581+
else if (idt%shape == 'NCPL' .or. idt%shape == 'NAUX NCPL') then
582+
do n = 1, ncpl
583+
if (dbl1d_ptr(n) /= DNODATA) then
584+
dbl1d(n) = dbl1d_ptr(n)
585+
end if
586+
end do
587+
end if
587588
end do
588589

589590
! cleanup

src/Utilities/Idm/netcdf/NCFileVars.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function ncvars_varid(this, tagname, layer, period, iaux) result(varid)
116116
write (errmsg, '(a)') &
117117
'NetCDF variable not found, tagname="'//trim(tagname)//'"'
118118
if (present(layer)) then
119-
write (errmsg, '(a,i0)') trim(errmsg)//', ilayer=', layer
119+
write (errmsg, '(a,i0)') trim(errmsg)//', layer=', layer
120120
end if
121121
if (present(period)) then
122122
write (errmsg, '(a,i0)') trim(errmsg)//', period=', period

0 commit comments

Comments
 (0)