Skip to content

Commit

Permalink
Resolve final issues from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
darianboggs committed Jun 4, 2024
1 parent 27118be commit bd54516
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
36 changes: 24 additions & 12 deletions base/MAPL_ESMF_InfoKeys.F90
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ module mapl3g_esmf_info_keys

implicit none

public :: make_dim_key

! FieldSpec info keys
character(len=*), parameter :: PREFIX = 'MAPL/'
character(len=*), parameter :: KEY_UNGRIDDED_DIM = PREFIX // 'ungridded_dims/'
character(len=*), parameter :: KEY_UNGRIDDED_DIMS = PREFIX // 'ungridded_dims/'
character(len=*), parameter :: KEY_VERT_DIM = PREFIX // 'vertical_dim/'
character(len=*), parameter :: KEY_VERT_GEOM = PREFIX // 'vertical_geom/'
character(len=*), parameter :: KEY_UNITS = PREFIX // 'units'
Expand All @@ -21,30 +23,40 @@ module mapl3g_esmf_info_keys
character(len=*), parameter :: KEY_VLOC = KEY_VERT_DIM // 'vloc'

! UngriddedDims info keys
character(len=*), parameter :: KEY_NUM_UNGRID_DIMS = KEY_UNGRIDDED_DIM // 'num_ungridded_dimensions'
character(len=*), parameter :: KEYSTUB_DIM = KEY_UNGRIDDED_DIM // 'dim_'
character(len=*), parameter :: KEY_NUM_UNGRID_DIMS = KEY_UNGRIDDED_DIMS // 'num_ungridded_dimensions'
character(len=*), parameter :: KEYSTUB_DIM = KEY_UNGRIDDED_DIMS // 'dim_'

! UngriddedDim info keys
character(len=*), parameter :: KEY_UNGRIDDED_NAME = 'name'
character(len=*), parameter :: KEY_UNGRIDDED_UNITS = 'units'
character(len=*), parameter :: KEY_UNGRIDDED_COORD = 'coordinates'

character(len=*), parameter :: KEY_DIM_STRINGS(9) = [ &
KEYSTUB_DIM // '1', KEYSTUB_DIM // '2', KEYSTUB_DIM // '3', &
KEYSTUB_DIM // '4', KEYSTUB_DIM // '5', KEYSTUB_DIM // '6', &
KEYSTUB_DIM // '7', KEYSTUB_DIM // '8', KEYSTUB_DIM // '9']
private

contains

function make_dim_key(n, rc) result(key)
character(len=:), allocatable :: key
integer, intent(in) :: n
integer, optional, intent(out) :: rc
integer, optional, intent(out) :: rc
integer :: status
character(len=*), parameter :: EMPTY_STRING = ''
character(len=20) :: raw

key = EMPTY_STRING
_ASSERT(n >=0, "n must be positive")
character(len=32) :: raw

key = ''
_ASSERT(n > 0, 'Index must be positive.')
if(n <= size(KEY_DIM_STRINGS)) then
key = KEY_DIM_STRINGS(n)
_RETURN(_SUCCESS)
end if
write(raw, fmt='(I0)', iostat=status) n
key = KEYSTUB_DIM // trim(adjustl(raw)) // '/'
_RETURN(status)

_ASSERT(status == 0, 'Write failed')
key = KEYSTUB_DIM // trim(raw)
_RETURN(_SUCCESS)

end function make_dim_key

end module mapl3g_esmf_info_keys
47 changes: 27 additions & 20 deletions gridcomps/History3G/OutputInfo.F90
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ module mapl3g_output_info
use mapl3g_ESMF_Info_Keys
use gFTL2_StringVector
use esmf, only: ESMF_Field, ESMF_FieldBundle, ESMF_FieldBundleGet
use esmf, only: ESMF_Info, ESMF_InfoDestroy, ESMF_InfoIsPresent
use esmf, only: ESMF_InfoGet, ESMF_InfoGetCharAlloc, ESMF_InfoGetFromHost
use esmf, only: ESMF_InfoGetAlloc
use esmf, only: ESMF_Info, ESMF_InfoIsPresent
use esmf, only: ESMF_InfoDestroy, ESMF_InfoCreate
use esmf, only: ESMF_InfoGet, ESMF_InfoGetFromHost
use esmf, only: ESMF_InfoGetAlloc, ESMF_InfoGetCharAlloc
use esmf, only: ESMF_InfoPrint
use Mapl_ErrorHandling

implicit none
Expand Down Expand Up @@ -105,8 +107,6 @@ function get_vertical_dim_spec_names_bundle(bundle, rc) result(names)
type(ESMF_FieldBundle), intent(in) :: bundle
integer, optional, intent(out) :: rc
integer :: status
integer :: i
character(len=:), allocatable :: name
type(ESMF_Info), allocatable :: info(:)

info = create_bundle_info(bundle, _RC)
Expand Down Expand Up @@ -187,7 +187,7 @@ function get_ungridded_dims_bundle_info(info, rc) result(vec)
type(UngriddedDims) :: dims

do i=1, size(info)
dims = make_ungridded_dims(info, _RC)
dims = make_ungridded_dims(info(i), _RC)
call push_ungridded_dims(vec, dims, rc)
end do
_RETURN(_SUCCESS)
Expand All @@ -214,35 +214,43 @@ function make_ungridded_dims(info, rc) result(dims)
integer :: status
integer :: num_dims, i
type(UngriddedDim) :: ungridded
character(len=:), allocatable :: dim_key

call ESMF_InfoGet(info, key=KEY_NUM_UNGRID_DIMS, value=num_dims, _RC)
do i=1, num_dims
dim_key = make_dim_key(i, _RC)
ungridded = make_ungridded_dim(info, dim_key, _RC)
ungridded = make_ungridded_dim(info, i, _RC)
call dims%add_dim(ungridded, _RC)
end do
_RETURN(_SUCCESS)

end function make_ungridded_dims

function make_ungridded_dim(info, key, rc)
function make_ungridded_dim(info, n, rc)
type(UngriddedDim) :: make_ungridded_dim
integer, intent(in) :: n
type(ESMF_Info), intent(in) :: info
character(len=*), intent(in) :: key
integer, optional, intent(out) :: rc
integer :: status
character(len=:), allocatable :: key
type(ESMF_Info) :: dim_info
character(len=:), allocatable :: name
character(len=:), allocatable :: units
real, allocatable :: coordinates(:)
logical :: is_present
character(len=1024) :: json_repr

key = make_dim_key(n, _RC)
call ESMF_InfoGet(info, key=key, isPresent=is_present, _RC)
if(.not. is_present) then
call ESMF_InfoPrint(info, unit=json_repr, _RC)
end if
_ASSERT(is_present, 'Key ' // key // ' not found in ' // trim(json_repr))
dim_info = ESMF_InfoCreate(info, key=key, _RC)
call ESMF_InfoGetCharAlloc(info, key=KEY_UNGRIDDED_NAME, value=name, _RC)
call ESMF_InfoGetCharAlloc(info, key=KEY_UNGRIDDED_UNITS, value=units, _RC)
call ESMF_InfoGetAlloc(info, key=KEY_UNGRIDDED_COORD, values=coordinates, _RC)
make_ungridded_dim = UngriddedDim(name, units, coordinates)
call ESMF_InfoGetCharAlloc(dim_info, key=KEY_UNGRIDDED_NAME, value=name, _RC)
call ESMF_InfoGetCharAlloc(dim_info, key=KEY_UNGRIDDED_UNITS, value=units, _RC)
call ESMF_InfoGetAlloc(dim_info, key=KEY_UNGRIDDED_COORD, values=coordinates, _RC)
call ESMF_InfoDestroy(dim_info, _RC)
make_ungridded_dim = UngriddedDim(name, units, coordinates)
_RETURN(_SUCCESS)

end function make_ungridded_dim

Expand All @@ -254,10 +262,8 @@ subroutine push_ungridded_dims(vec, dims, rc)
integer :: i

do i = 1, dims%get_num_ungridded()
associate (udim => dims%get_ith_dim_spec(i))
call check_duplicate(vec, udim, _RC)
call vec%push_back(udim, _RC)
end associate
call check_duplicate(vec, dims%get_ith_dim_spec(i), _RC)
call vec%push_back(dims%get_ith_dim_spec(i), _RC)
end do
_RETURN(_SUCCESS)

Expand Down Expand Up @@ -292,7 +298,7 @@ subroutine check_duplicate(vec, udim, rc)
call iter%next()
vdim = iter%of()
if(udim%get_name() /= vdim%get_name()) cycle
_ASSERT(udim == vdim)
_ASSERT(udim == vdim, 'UngriddedDim mismatch.')
end do

_RETURN(_SUCCESS)
Expand All @@ -309,6 +315,7 @@ function create_bundle_info(bundle, rc) result(bundle_info)
type(ESMF_Field), allocatable :: fields(:)
type(ESMF_Info) :: info

status = 0
call ESMF_FieldBundleGet(bundle, fieldCount=field_count, _RC)
_ASSERT(field_count > 0, 'Empty bundle')
allocate(fields(field_count))
Expand Down
16 changes: 8 additions & 8 deletions gridcomps/History3G/tests/Test_OutputInfo.pf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# undef SET_RC
#endif
#define SET_RC(A) if(present(rc)) rc = A
#define _SUCCESS 0
#define _FAILURE _SUCCESS-1
#include "MAPL_TestErr.h"
module Test_OutputInfo
use mapl3g_output_info
Expand Down Expand Up @@ -82,7 +84,6 @@ contains
real, allocatable :: coordinates(:)
type(UngriddedDimVector) :: vec
type(UngriddedDim) :: undim
real :: tol = 1E-8

call safe_dealloc(bundle_info)

Expand All @@ -94,7 +95,7 @@ contains
do i=1, N
bundle_info(i) = make_esmf_info(names=EXPECTED_NAMES, units_array=EXPECTED_UNITS, coordinates=EXPECTED_COORDINATES, _RC)
end do
vec = get_ungridded_dims_bundle_info(bundle_info, tol, _RC)
vec = get_ungridded_dims_bundle_info(bundle_info, _RC)
do i=1, N
undim = vec%at(i)
name = undim%get_name()
Expand Down Expand Up @@ -178,7 +179,7 @@ contains
integer :: status, i
character(len=:), allocatable :: names_(:), units_(:)
real, allocatable :: coordinates_(:, :)
character(len=:), allocatable :: dim_key
character(len=:), allocatable :: key
character(len=:), allocatable :: name, units
real, allocatable :: coord(:)

Expand Down Expand Up @@ -213,13 +214,13 @@ contains
call ESMF_InfoSet(info, KEY_NUM_UNGRID_DIMS, num_ungridded, _RC)

do i=1, num_ungridded
dim_key = make_dim_key(i, _RC)
key = make_dim_key(i, _RC)
name = names_(i)
units = units_(i)
coord = coordinates_(i, :)
call ESMF_InfoSet(info, dim_key // KEY_UNGRIDDED_NAME, name, _RC)
call ESMF_InfoSet(info, dim_key // KEY_UNGRIDDED_UNITS, units, _RC)
call ESMF_InfoSet(info, dim_key // KEY_UNGRIDDED_COORD, coord, _RC)
call ESMF_InfoSet(info, key // '/' // KEY_UNGRIDDED_NAME, name, _RC)
call ESMF_InfoSet(info, key // '/' // KEY_UNGRIDDED_UNITS, units, _RC)
call ESMF_InfoSet(info, key // '/' // KEY_UNGRIDDED_COORD, coord, _RC)
end do

SET_RC(status)
Expand All @@ -238,7 +239,6 @@ contains

subroutine deallocate_destroy(info)
type(ESMF_Info), allocatable, intent(inout) :: info(:)
integer :: i

call destroy_all(info)
deallocate(info)
Expand Down

0 comments on commit bd54516

Please sign in to comment.