StateFilter.F90 Source File


This file depends on

sourcefile~~statefilter.f90~~EfferentGraph sourcefile~statefilter.f90 StateFilter.F90 sourcefile~fieldutils.f90 FieldUtils.F90 sourcefile~statefilter.f90->sourcefile~fieldutils.f90 sourcefile~mapl_exceptionhandling.f90 MAPL_ExceptionHandling.F90 sourcefile~statefilter.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~statearithmeticparser.f90 StateArithmeticParser.F90 sourcefile~statefilter.f90->sourcefile~statearithmeticparser.f90 sourcefile~statemasking.f90 StateMasking.F90 sourcefile~statefilter.f90->sourcefile~statemasking.f90 sourcefile~fieldbinaryoperations.f90 FieldBinaryOperations.F90 sourcefile~fieldutils.f90->sourcefile~fieldbinaryoperations.f90 sourcefile~fieldblas.f90 FieldBLAS.F90 sourcefile~fieldutils.f90->sourcefile~fieldblas.f90 sourcefile~fieldpointerutilities.f90 FieldPointerUtilities.F90 sourcefile~fieldutils.f90->sourcefile~fieldpointerutilities.f90 sourcefile~fieldunaryfunctions.f90 FieldUnaryFunctions.F90 sourcefile~fieldutils.f90->sourcefile~fieldunaryfunctions.f90 sourcefile~fieldutilities.f90 FieldUtilities.F90 sourcefile~fieldutils.f90->sourcefile~fieldutilities.f90 sourcefile~mapl_errorhandling.f90 MAPL_ErrorHandling.F90 sourcefile~mapl_exceptionhandling.f90->sourcefile~mapl_errorhandling.f90 sourcefile~mapl_throw.f90 MAPL_Throw.F90 sourcefile~mapl_exceptionhandling.f90->sourcefile~mapl_throw.f90 sourcefile~statearithmeticparser.f90->sourcefile~fieldutils.f90 sourcefile~statearithmeticparser.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~base_base.f90 Base_Base.F90 sourcefile~statearithmeticparser.f90->sourcefile~base_base.f90 sourcefile~mapl_comms.f90 MAPL_Comms.F90 sourcefile~statearithmeticparser.f90->sourcefile~mapl_comms.f90 sourcefile~statemasking.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~statemasking.f90->sourcefile~statearithmeticparser.f90 sourcefile~statemasking.f90->sourcefile~base_base.f90 sourcefile~constants.f90 Constants.F90 sourcefile~statemasking.f90->sourcefile~constants.f90 sourcefile~esmfl_mod.f90 ESMFL_Mod.F90 sourcefile~statemasking.f90->sourcefile~esmfl_mod.f90 sourcefile~mapl_keywordenforcer.f90 MAPL_KeywordEnforcer.F90 sourcefile~statemasking.f90->sourcefile~mapl_keywordenforcer.f90 sourcefile~base_base.f90->sourcefile~constants.f90 sourcefile~base_base.f90->sourcefile~mapl_keywordenforcer.f90 sourcefile~mapl_range.f90 MAPL_Range.F90 sourcefile~base_base.f90->sourcefile~mapl_range.f90 sourcefile~maplgrid.f90 MaplGrid.F90 sourcefile~base_base.f90->sourcefile~maplgrid.f90 sourcefile~internalconstants.f90 InternalConstants.F90 sourcefile~constants.f90->sourcefile~internalconstants.f90 sourcefile~mathconstants.f90 MathConstants.F90 sourcefile~constants.f90->sourcefile~mathconstants.f90 sourcefile~physicalconstants.f90 PhysicalConstants.F90 sourcefile~constants.f90->sourcefile~physicalconstants.f90 sourcefile~esmfl_mod.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~esmfl_mod.f90->sourcefile~base_base.f90 sourcefile~esmfl_mod.f90->sourcefile~constants.f90 sourcefile~esmfl_mod.f90->sourcefile~mapl_comms.f90 sourcefile~mapl_abstractgridfactory.f90 MAPL_AbstractGridFactory.F90 sourcefile~esmfl_mod.f90->sourcefile~mapl_abstractgridfactory.f90 sourcefile~mapl_gridmanager.f90 MAPL_GridManager.F90 sourcefile~esmfl_mod.f90->sourcefile~mapl_gridmanager.f90 sourcefile~fieldbinaryoperations.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~fieldbinaryoperations.f90->sourcefile~fieldpointerutilities.f90 sourcefile~fieldblas.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~fieldblas.f90->sourcefile~fieldpointerutilities.f90 sourcefile~fieldpointerutilities.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~fieldunaryfunctions.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~fieldunaryfunctions.f90->sourcefile~fieldpointerutilities.f90 sourcefile~fieldutilities.f90->sourcefile~fieldpointerutilities.f90 sourcefile~fieldutilities.f90->sourcefile~mapl_errorhandling.f90 sourcefile~mapl_comms.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~mapl_comms.f90->sourcefile~base_base.f90 sourcefile~mapl_comms.f90->sourcefile~constants.f90 sourcefile~shmem.f90 Shmem.F90 sourcefile~mapl_comms.f90->sourcefile~shmem.f90 sourcefile~mapl_errorhandling.f90->sourcefile~mapl_throw.f90

Files dependent on this one

sourcefile~~statefilter.f90~~AfferentGraph sourcefile~statefilter.f90 StateFilter.F90 sourcefile~stateutils.f90 StateUtils.F90 sourcefile~stateutils.f90->sourcefile~statefilter.f90 sourcefile~extdataderived.f90 ExtDataDerived.F90 sourcefile~extdataderived.f90->sourcefile~stateutils.f90 sourcefile~extdatagridcompmod.f90 ExtDataGridCompMod.F90 sourcefile~extdatagridcompmod.f90->sourcefile~stateutils.f90 sourcefile~extdatagridcompng.f90 ExtDataGridCompNG.F90 sourcefile~extdatagridcompng.f90->sourcefile~stateutils.f90 sourcefile~extdataoldtypescreator.f90 ExtDataOldTypesCreator.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdataoldtypescreator.f90 sourcefile~extdatatypedef.f90 ExtDataTypeDef.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdatatypedef.f90 sourcefile~extdataconfig.f90 ExtDataConfig.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdataconfig.f90 sourcefile~extdataderivedexportvector.f90 ExtDataDerivedExportVector.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdataderivedexportvector.f90 sourcefile~extdataprimaryexportvector.f90 ExtDataPrimaryExportVector.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdataprimaryexportvector.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~stateutils.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdataderived.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdatatypedef.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdataconfig.f90 sourcefile~extdataroot_gridcomp.f90 ExtDataRoot_GridComp.F90 sourcefile~extdataroot_gridcomp.f90->sourcefile~stateutils.f90 sourcefile~mapl.f90 MAPL.F90 sourcefile~extdataroot_gridcomp.f90->sourcefile~mapl.f90 sourcefile~varspecdescription.f90 VarspecDescription.F90 sourcefile~extdataroot_gridcomp.f90->sourcefile~varspecdescription.f90 sourcefile~extdatatypedef.f90->sourcefile~stateutils.f90 sourcefile~mapl.f90->sourcefile~stateutils.f90 sourcefile~mapl_historygridcomp.f90 MAPL_HistoryGridComp.F90 sourcefile~mapl_historygridcomp.f90->sourcefile~stateutils.f90 sourcefile~test_statearithmetic.pf Test_StateArithmetic.pf sourcefile~test_statearithmetic.pf->sourcefile~stateutils.f90 sourcefile~test_statefilter.pf Test_StateFilter.pf sourcefile~test_statefilter.pf->sourcefile~stateutils.f90 sourcefile~test_statemask.pf Test_StateMask.pf sourcefile~test_statemask.pf->sourcefile~stateutils.f90 sourcefile~capdriver.f90 CapDriver.F90 sourcefile~capdriver.f90->sourcefile~extdataroot_gridcomp.f90 sourcefile~capdriver.f90->sourcefile~mapl.f90 sourcefile~comp_testing_driver.f90 Comp_Testing_Driver.F90 sourcefile~comp_testing_driver.f90->sourcefile~mapl.f90 sourcefile~mapl_capgridcomp.f90 MAPL_CapGridComp.F90 sourcefile~comp_testing_driver.f90->sourcefile~mapl_capgridcomp.f90 sourcefile~driver.f90~2 driver.F90 sourcefile~driver.f90~2->sourcefile~mapl.f90 sourcefile~gridcomp.f90 GridComp.F90 sourcefile~driver.f90~2->sourcefile~gridcomp.f90 sourcefile~extdataconfig.f90->sourcefile~extdataderived.f90 sourcefile~extdataderivedexportvector.f90->sourcefile~extdatatypedef.f90 sourcefile~extdatadriver.f90 ExtDataDriver.F90 sourcefile~extdatadriver.f90->sourcefile~extdataroot_gridcomp.f90 sourcefile~extdatadriver.f90->sourcefile~mapl.f90 sourcefile~extdatadrivergridcomp.f90 ExtDataDriverGridComp.F90 sourcefile~extdatadriver.f90->sourcefile~extdatadrivergridcomp.f90 sourcefile~extdatadrivermod.f90 ExtDataDriverMod.F90 sourcefile~extdatadriver.f90->sourcefile~extdatadrivermod.f90 sourcefile~extdatadrivergridcomp.f90->sourcefile~extdatagridcompmod.f90 sourcefile~extdatadrivergridcomp.f90->sourcefile~extdatagridcompng.f90 sourcefile~extdatadrivergridcomp.f90->sourcefile~mapl.f90 sourcefile~extdatadrivergridcomp.f90->sourcefile~mapl_historygridcomp.f90 sourcefile~extdatadrivermod.f90->sourcefile~extdataroot_gridcomp.f90 sourcefile~extdatadrivermod.f90->sourcefile~mapl.f90 sourcefile~extdatadrivermod.f90->sourcefile~extdatadrivergridcomp.f90 sourcefile~extdataprimaryexportvector.f90->sourcefile~extdatatypedef.f90 sourcefile~gridcomp.f90->sourcefile~mapl.f90 sourcefile~mapl_capgridcomp.f90->sourcefile~extdatagridcompmod.f90 sourcefile~mapl_capgridcomp.f90->sourcefile~extdatagridcompng.f90 sourcefile~mapl_capgridcomp.f90->sourcefile~mapl_historygridcomp.f90 sourcefile~mapl_demo_fargparse.f90 MAPL_demo_fargparse.F90 sourcefile~mapl_demo_fargparse.f90->sourcefile~mapl.f90 sourcefile~pfio_mapl_demo.f90 pfio_MAPL_demo.F90 sourcefile~pfio_mapl_demo.f90->sourcefile~mapl.f90 sourcefile~regrid_util.f90 Regrid_Util.F90 sourcefile~regrid_util.f90->sourcefile~mapl.f90 sourcefile~time_ave_util.f90 time_ave_util.F90 sourcefile~time_ave_util.f90->sourcefile~mapl.f90 sourcefile~ut_extdata.f90 ut_ExtData.F90 sourcefile~ut_extdata.f90->sourcefile~extdatagridcompmod.f90 sourcefile~varspecdescription.f90->sourcefile~mapl.f90 sourcefile~mapl_cap.f90 MAPL_Cap.F90 sourcefile~mapl_cap.f90->sourcefile~mapl_capgridcomp.f90

Source Code

#include "MAPL_ErrLog.h"
module MAPL_StateFilter
   use ESMF
   use MAPL_ExceptionHandling
   use MAPL_FieldUtils
   use MAPL_StateArithmeticParserMod
   use MAPL_StateMaskMod
   use, intrinsic :: iso_fortran_env, only: REAL32, REAL64
   implicit none
   private

   public StateFilterItem
   character(len=1), parameter :: var_placeholder = "@"
   character(len=1), parameter :: separator = "."

   interface StateFilterItem
      procedure StateFilter_R4_2D
      procedure StateFilter_R4_3D
   end interface

   contains

   subroutine StateFilter_R4_2D(state, config, itemName, array, rc)
      type(ESMF_State), intent(inout) :: state
      type(ESMF_Config), intent(inout) :: config
      character(len=*), intent(in) :: itemName
      real(REAL32), allocatable, intent(out) :: array(:,:)
      integer, optional, intent(out) :: rc

      integer :: status, rank
      character(len=ESMF_MAXSTR) :: filter_expression, field_name
      character(len=:), allocatable :: processed_expression
      type(ESMF_Field) :: new_field, old_field
      logical :: name_Present, default_Present
      real(REAL32), pointer :: ptr2d_new(:,:), ptr2d_old(:,:)
      type(ESMF_TYPEKIND_FLAG) :: tk
      type(StateMask) :: mask

      call ESMF_StateGet(state, itemName, old_field, _RC)
      call ESMF_FieldGet(old_field, typeKind=tk, rank=rank, _RC) 
      _ASSERT(tk==ESMF_TYPEKIND_R4,"wrong typekind when call MAPL_StateFilter")
      _ASSERT(rank==2,"wrong rank when call MAPL_StateFilter")

      call ESMF_FieldGet(old_field, 0, farrayPtr=ptr2d_old, _RC)
      allocate(array( lbound(ptr2d_old,1):ubound(ptr2d_old,1) , lbound(ptr2d_old,2):ubound(ptr2d_old,2) ),  _STAT) 
      array = ptr2d_old
 
      call ESMF_ConfigFindLabel(config, "FILTER"//separator//trim(itemName)//":", isPresent=name_Present, _RC)
      if (name_Present) then
         call ESMF_ConfigGetAttribute(config, filter_expression, label="FILTER"//separator//trim(itemName)//":", _RC)
      else
         call ESMF_ConfigFindLabel(config, "FILTER"//separator//var_placeholder//":", isPresent=default_Present, _RC)
         _RETURN_UNLESS(default_present)
         call ESMF_ConfigGetAttribute(config, filter_expression, label="FILTER"//separator//var_placeholder//":", _RC)
      end if

      call FieldClone(old_field, new_field, _RC)
      call ESMF_FieldGet(old_field, name=field_name, _RC)
      call ESMF_FieldGet(new_field, 0, farrayPtr=ptr2d_new, _RC)
      ptr2d_new = ptr2d_old

      processed_expression = substitute_name(filter_expression, field_name) 
      if (index(processed_expression,"mask") > 0) then
         mask = StateMask(processed_expression)
         call mask%evaluate_mask(state, new_field, _RC)
      else
         call MAPL_StateEval(state, processed_expression, new_field, _RC)
      end if
      array = ptr2d_new

      call ESMF_FieldDestroy(new_field, noGarbage=.true., _RC)
      _RETURN(_SUCCESS)

   end subroutine StateFilter_R4_2D

   subroutine StateFilter_R4_3D(state, config, itemName, array, rc)
      type(ESMF_State), intent(inout) :: state
      type(ESMF_Config), intent(inout) :: config
      character(len=*), intent(in) :: itemName
      real(REAL32), allocatable, intent(out) :: array(:,:,:)
      integer, optional, intent(out) :: rc

      integer :: status, rank
      character(len=ESMF_MAXSTR) :: filter_expression, field_name
      character(len=:), allocatable :: processed_expression
      type(ESMF_Field) :: new_field, old_field
      logical :: name_Present, default_Present
      real(REAL32), pointer :: ptr3d_new(:,:,:), ptr3d_old(:,:,:)
      type(ESMF_TYPEKIND_FLAG) :: tk
      type(StateMask) :: mask

      call ESMF_StateGet(state, itemName, old_field, _RC)
      call ESMF_FieldGet(old_field, typeKind=tk, rank=rank, _RC) 
      _ASSERT(tk==ESMF_TYPEKIND_R4,"wrong typekind when call MAPL_StateFilter")
      _ASSERT(rank==3,"wrong rank when call MAPL_StateFilter")

      call ESMF_FieldGet(old_field, 0, farrayPtr=ptr3d_old, _RC)
      allocate(array( lbound(ptr3d_old,1):ubound(ptr3d_old,1) , lbound(ptr3d_old,2):ubound(ptr3d_old,2), lbound(ptr3d_old,3):ubound(ptr3d_old,3) ),  _STAT) 
      array = ptr3d_old
 
      call ESMF_ConfigFindLabel(config, "FILTER"//separator//trim(itemName)//":", isPresent=name_Present, _RC)
      if (name_Present) then
         call ESMF_ConfigGetAttribute(config, filter_expression, label="FILTER"//separator//trim(itemName)//":", _RC)
      else
         call ESMF_ConfigFindLabel(config, "FILTER"//separator//var_placeholder//":", isPresent=default_Present, _RC)
         _RETURN_UNLESS(default_present)
         call ESMF_ConfigGetAttribute(config, filter_expression, label="FILTER"//separator//var_placeholder//":", _RC)
      end if

      call FieldClone(old_field, new_field, _RC)
      call ESMF_FieldGet(old_field, name=field_name, _RC)
      call ESMF_FieldGet(new_field, 0, farrayPtr=ptr3d_new, _RC)
      ptr3d_new = ptr3d_old

      processed_expression = substitute_name(filter_expression, field_name) 
      if (index(processed_expression,"mask") > 0) then
         mask = StateMask(processed_expression)
         call mask%evaluate_mask(state, new_field, _RC)
      else
         call MAPL_StateEval(state, processed_expression, new_field, _RC)
      end if
      array = ptr3d_new

      call ESMF_FieldDestroy(new_field, noGarbage=.true., _RC)
      _RETURN(_SUCCESS)

   end subroutine StateFilter_R4_3d


   function substitute_name(filter_expression, field_name, rc) result(processed_expression)
      character(len=:), allocatable :: processed_expression
      character(len=*), intent(in) :: filter_expression
      character(len=*), intent(in) :: field_name
      integer, optional, intent(out) :: rc

      integer :: placeholder_loc
      character(len=:), allocatable :: temp_before, temp_after
      placeholder_loc = index(filter_expression, var_placeholder)
      _ASSERT(placeholder_loc > 0, "expression for filter does not have a @ in it")
      temp_before = filter_expression(1:placeholder_loc-1)
      temp_after = filter_expression(placeholder_loc+1:)
      processed_expression = temp_before//trim(field_name)//temp_after
      _RETURN(_SUCCESS)
   end function 
      
end module MAPL_StateFilter