Skip to content

Commit

Permalink
Merge pull request #3006 from GEOS-ESM/feature/pchakrab/regrid-param-…
Browse files Browse the repository at this point in the history
…varspec

Enable import items to specify regrid parameter
  • Loading branch information
tclune authored Sep 8, 2024
2 parents 532f6c9 + 7dca821 commit 71c8504
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 42 deletions.
33 changes: 2 additions & 31 deletions generic3g/specs/FieldSpec.F90
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module mapl3g_FieldSpec
use mapl3g_geom_mgr, only: MAPL_SameGeom
use mapl3g_FieldDictionary
use mapl3g_GriddedComponentDriver
use mapl3g_VariableSpec
use mapl3g_VariableSpec, only: VariableSpec
use udunits2f, only: UDUNITS_are_convertible => are_convertible, udunit
use gftl2_StringVector
use esmf
Expand Down Expand Up @@ -165,7 +165,6 @@ function new_FieldSpec_geom(unusable, geom, vertical_grid, vertical_dim_spec, ty
! optional args last
real, optional, intent(in) :: default_value

type(ESMF_RegridMethod_Flag), allocatable :: regrid_method
integer :: status

if (present(geom)) field_spec%geom = geom
Expand All @@ -181,8 +180,6 @@ function new_FieldSpec_geom(unusable, geom, vertical_grid, vertical_dim_spec, ty

! regrid_param
field_spec%regrid_param = EsmfRegridderParam() ! use default regrid method
regrid_method = get_regrid_method_(field_spec%standard_name)
field_spec%regrid_param = EsmfRegridderParam(regridmethod=regrid_method)
if (present(regrid_param)) field_spec%regrid_param = regrid_param

if (present(default_value)) field_spec%default_value = default_value
Expand All @@ -199,41 +196,15 @@ function new_FieldSpec_varspec(variable_spec) result(field_spec)
_SET_FIELD(field_spec, variable_spec, typekind)
_SET_FIELD(field_spec, variable_spec, ungridded_dims)
_SET_FIELD(field_spec, variable_spec, attributes)
_SET_FIELD(field_spec, variable_spec, regrid_param)
_SET_ALLOCATED_FIELD(field_spec, variable_spec, standard_name)
_SET_ALLOCATED_FIELD(field_spec, variable_spec, units)
_SET_ALLOCATED_FIELD(field_spec, variable_spec, default_value)

field_spec%long_name = 'unknown'

field_spec%regrid_param = EsmfRegridderParam() ! use default regrid method
regrid_method = get_regrid_method_(field_spec%standard_name)
field_spec%regrid_param = EsmfRegridderParam(regridmethod=regrid_method)


end function new_FieldSpec_varspec

function get_regrid_method_(stdname, rc) result(regrid_method)
type(ESMF_RegridMethod_Flag) :: regrid_method
character(:), allocatable, intent(in) :: stdname
integer, optional, intent(out) :: rc

character(len=*), parameter :: field_dictionary_file = "field_dictionary.yml"
type(FieldDictionary) :: field_dict
logical :: file_exists
integer :: status

regrid_method = ESMF_REGRIDMETHOD_BILINEAR ! default value
if (allocated(stdname)) then
inquire(file=trim(field_dictionary_file), exist=file_exists)
if (file_exists) then
field_dict = FieldDictionary(filename=field_dictionary_file, _RC)
regrid_method = field_dict%get_regrid_method(stdname, _RC)
end if
end if

_RETURN(_SUCCESS)
end function get_regrid_method_

subroutine set_geometry(this, geom, vertical_grid, rc)
class(FieldSpec), intent(inout) :: this
type(ESMF_Geom), optional, intent(in) :: geom
Expand Down
88 changes: 77 additions & 11 deletions generic3g/specs/VariableSpec.F90
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "MAPL_Generic.h"

module mapl3g_VariableSpec
Expand All @@ -14,6 +13,8 @@ module mapl3g_VariableSpec
use mapl_ErrorHandling
use mapl3g_StateRegistry
use mapl3g_StateItem
use mapl3g_EsmfRegridder, only: EsmfRegridderParam
use mapl3g_FieldDictionary
use esmf
use gFTL2_StringVector
use nuopc
Expand All @@ -32,6 +33,7 @@ module mapl3g_VariableSpec
type(ESMF_StateIntent_Flag) :: state_intent
character(:), allocatable :: short_name
type(ESMF_TypeKind_Flag) :: typekind = ESMF_TYPEKIND_R4
type(EsmfRegridderParam) :: regrid_param

! Metadata
character(:), allocatable :: standard_name
Expand All @@ -51,9 +53,9 @@ module mapl3g_VariableSpec
type(StringVector) :: dependencies
contains
procedure :: make_virtualPt

procedure :: make_dependencies
procedure :: initialize
procedure, private :: set_regrid_param_
end type VariableSpec

interface VariableSpec
Expand All @@ -67,7 +69,7 @@ function new_VariableSpec( &
units, substate, itemtype, typekind, vertical_dim_spec, ungridded_dims, default_value, &
service_items, attributes, &
bracket_size, &
dependencies) result(var_spec)
dependencies, regrid_param, rc) result(var_spec)

type(VariableSpec) :: var_spec
type(ESMF_StateIntent_Flag), intent(in) :: state_intent
Expand All @@ -87,6 +89,11 @@ function new_VariableSpec( &
type(StringVector), optional, intent(in) :: attributes
integer, optional, intent(in) :: bracket_size
type(StringVector), optional, intent(in) :: dependencies
type(EsmfRegridderParam), optional, intent(in) :: regrid_param
integer, optional, intent(out) :: rc

type(ESMF_RegridMethod_Flag), allocatable :: regrid_method
integer :: status

var_spec%state_intent = state_intent
var_spec%short_name = short_name
Expand All @@ -110,6 +117,8 @@ function new_VariableSpec( &
_SET_OPTIONAL(bracket_size)
_SET_OPTIONAL(dependencies)

call var_spec%set_regrid_param_(regrid_param, _RC)

_UNUSED_DUMMY(unusable)
end function new_VariableSpec

Expand All @@ -126,7 +135,7 @@ subroutine initialize(this, config)
this%units = ESMF_HConfigAsString(config,keyString='units')

contains

function get_itemtype(config) result(itemtype)
type(ESMF_StateItem_Flag) :: itemtype
type(ESMF_HConfig), intent(in) :: config
Expand All @@ -136,13 +145,13 @@ function get_itemtype(config) result(itemtype)

itemtype = MAPL_STATEITEM_FIELD ! default
if (.not. ESMF_HConfigIsDefined(config,keyString='itemtype')) return
itemtype_as_string = ESMF_HConfigAsString(config,keyString='itemtype',rc=status)

itemtype_as_string = ESMF_HConfigAsString(config,keyString='itemtype',rc=status)
if (status /= 0) then
itemtype = MAPL_STATEITEM_UNKNOWN
return
end if

select case (itemtype_as_string)
case ('field')
itemtype = MAPL_STATEITEM_FIELD
Expand All @@ -161,9 +170,9 @@ function get_itemtype(config) result(itemtype)
case default
itemtype = MAPL_STATEITEM_UNKNOWN
end select

end function get_itemtype

end subroutine initialize

function make_virtualPt(this) result(v_pt)
Expand All @@ -179,7 +188,7 @@ subroutine fill_units(this, units, rc)
class(VariableSpec), intent(in) :: this
character(:), allocatable, intent(out) :: units
integer, optional, intent(out) :: rc

character(len=ESMF_MAXSTR) :: canonical_units
integer :: status

Expand All @@ -195,7 +204,7 @@ subroutine fill_units(this, units, rc)
call NUOPC_FieldDictionaryGetEntry(this%standard_name, canonical_units, status)
_ASSERT(status == ESMF_SUCCESS,'Units not found for standard name: <'//this%standard_name//'>')
units = trim(canonical_units)

_RETURN(_SUCCESS)
end subroutine fill_units

Expand All @@ -217,4 +226,61 @@ function make_dependencies(this, rc) result(dependencies)
_RETURN(_SUCCESS)
end function make_dependencies

subroutine set_regrid_param_(this, regrid_param, rc)
class(VariableSpec), intent(inout) :: this
type(EsmfRegridderParam), optional, intent(in) :: regrid_param
integer, optional, intent(out) :: rc

type(ESMF_RegridMethod_Flag) :: regrid_method
integer :: status

if (present(regrid_param)) then
this%regrid_param = regrid_param
_RETURN(_SUCCESS)
end if

! if (NUOPC_FieldDictionaryHasEntry(this%standard_name, rc=status)) then
! call NUOPC_FieldDictionaryGetEntry(this%standard_name, regrid_method, rc=status)
! if (status==ESMF_SUCCESS) then
! this%regrid_param = EsmfRegridderParam(regridmethod=regrid_method)
! _RETURN(_SUCCESS)
! end if
! end if
regrid_method = get_regrid_method_from_field_dict_(this%standard_name, rc=status)
if (status==ESMF_SUCCESS) then
this%regrid_param = EsmfRegridderParam(regridmethod=regrid_method)
_RETURN(_SUCCESS)
end if

this%regrid_param = EsmfRegridderParam() ! last resort - use default regrid method

_RETURN(_SUCCESS)
end subroutine set_regrid_param_

function get_regrid_method_from_field_dict_(stdname, rc) result(regrid_method)
type(ESMF_RegridMethod_Flag) :: regrid_method
character(:), allocatable, intent(in) :: stdname
integer, optional, intent(out) :: rc

character(len=*), parameter :: field_dictionary_file = "field_dictionary.yml"
type(FieldDictionary) :: field_dict
logical :: file_exists
integer :: status

inquire(file=trim(field_dictionary_file), exist=file_exists)
if (.not. file_exists) then
rc = _FAILURE
return
end if

field_dict = FieldDictionary(filename=field_dictionary_file, _RC)
if (.not. allocated(stdname)) then
rc = _FAILURE
return
end if
regrid_method = field_dict%get_regrid_method(stdname, _RC)

_RETURN(_SUCCESS)
end function get_regrid_method_from_field_dict_

end module mapl3g_VariableSpec

0 comments on commit 71c8504

Please sign in to comment.