LCOV - code coverage report
Current view: top level - hemco/HEMCO/src/Core - hco_restart_mod.F90 (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 105 0.0 %
Date: 2025-01-13 21:54:50 Functions: 0 6 0.0 %

          Line data    Source code
       1             : !------------------------------------------------------------------------------
       2             : !                   Harmonized Emissions Component (HEMCO)                    !
       3             : !------------------------------------------------------------------------------
       4             : !BOP
       5             : !
       6             : ! !MODULE: hco_restart_mod.F90
       7             : !
       8             : ! !DESCRIPTION: Module HCO\_RESTART\_MOD contains wrapper routines to define,
       9             : ! get and write restart fields.
      10             : !\\
      11             : !\\
      12             : ! Restart variables are required by some of the HEMCO extensions. The
      13             : ! HEMCO restart variables can be organized through the HEMCO restart
      14             : ! diagnostics collection. At the end of a simulation, all diagnostic fields
      15             : ! ('containers') of the restart collection are written to the HEMCO restart
      16             : ! file.
      17             : !\\
      18             : !\\
      19             : ! All fields from the HEMCO restart file can be easily read back into HEMCO
      20             : ! via the HEMCO I/O infrastructure, i.e. by listing them in the HEMCO
      21             : ! configuration file.
      22             : !\\
      23             : !\\
      24             : ! In an ESMF/MAPL environment, restart variables should be organized through
      25             : ! the ESMF internal state object. This is particularly important for
      26             : ! simulation that rely on checkpoint files (e.g. replay simulations).
      27             : ! In this cases, the restart variables are obtained from / written to the
      28             : ! ESMF internal state object, rather than the HEMCO restart file.
      29             : !\\
      30             : !\\
      31             : ! This module contains wrapper routines to define, obtain and write restart
      32             : ! fields for the two aforementioned restart types. The routines work both for
      33             : ! 'traditional' and ESMF restart variables. In an ESMF application, the first
      34             : ! check is always performed within the internal state, e.g. it is first
      35             : ! checked if the given field variable exists in the internal state of the
      36             : ! gridded component that HEMCO sits in. If so, the field is obtained from /
      37             : ! written to the internal state. If no internal state object exist, an
      38             : ! attempt is made to obtain the field through the HEMCO data list, i.e. it
      39             : ! is checked if the restart variable is specified in the HEMCO configuration
      40             : ! file.
      41             : ! A HEMCO diagnostics container is created in the diagnostics restart
      42             : ! collection in both the traditional and the ESMF environment.
      43             : !\\
      44             : !\\
      45             : ! Routine HCO\_RestartDefine should be called during the initialization stage.
      46             : ! Routine HCO\_RestartGet should be called on the first run call and after
      47             : ! each rewinding of the clock. HEMCO routines HcoClock\_First and
      48             : ! HcoClock\_Rewind can be used to determine if it's time to read/update the
      49             : ! restart variable.
      50             : ! HCO\_RestartWrite should be called *on every time step*. This is important
      51             : ! in ESMF applications that rely on checkpoint files (e.g. replay simulations)
      52             : ! that are written out by ESMF/MAPL throughout the simulation. In a non-ESMF
      53             : ! environment, the HCO\_RestartWrite call is basically void but it should be
      54             : ! called nevertheless.
      55             : !\\
      56             : !\\
      57             : ! !INTERFACE:
      58             : !
      59             : MODULE HCO_RESTART_MOD
      60             : !
      61             : ! !USES:
      62             : !
      63             :   USE HCO_ERROR_MOD
      64             : 
      65             :   IMPLICIT NONE
      66             :   PRIVATE
      67             : !
      68             : ! !PUBLIC MEMBER FUNCTIONS:
      69             : !
      70             :   ! defined in all environment
      71             :   PUBLIC :: HCO_RestartDefine
      72             :   PUBLIC :: HCO_RestartGet
      73             :   PUBLIC :: HCO_RestartWrite
      74             : !
      75             : ! !PRIVATE MEMBER FUNCTIONS:
      76             : !
      77             : #if defined(ESMF_)
      78             :   PRIVATE :: HCO_CopyFromIntnal_ESMF
      79             : #endif
      80             : 
      81             :   INTERFACE HCO_RestartDefine
      82             :      MODULE PROCEDURE HCO_RestartDefine_3D
      83             :      MODULE PROCEDURE HCO_RestartDefine_2D
      84             :   END INTERFACE HCO_RestartDefine
      85             : 
      86             :   INTERFACE HCO_RestartGet
      87             :      MODULE PROCEDURE HCO_RestartGet_3D
      88             :      MODULE PROCEDURE HCO_RestartGet_2D
      89             :   END INTERFACE HCO_RestartGet
      90             : 
      91             :   INTERFACE HCO_RestartWrite
      92             :      MODULE PROCEDURE HCO_RestartWrite_3D
      93             :      MODULE PROCEDURE HCO_RestartWrite_2D
      94             :   END INTERFACE HCO_RestartWrite
      95             : !
      96             : ! !REVISION HISTORY:
      97             : !  10 Mar 2015 - C. Keller   - Initial version
      98             : !  See https://github.com/geoschem/hemco for complete history
      99             : !EOP
     100             : !------------------------------------------------------------------------------
     101             : !BOC
     102             : CONTAINS
     103             : !EOC
     104             : !------------------------------------------------------------------------------
     105             : !                   Harmonized Emissions Component (HEMCO)                    !
     106             : !------------------------------------------------------------------------------
     107             : !BOP
     108             : !
     109             : ! !ROUTINE: HCO_RestartDefine_3D
     110             : !
     111             : ! !DESCRIPTION: Subroutine HCO\_RestartDefine\_3D defines a restart diagnostics.
     112             : ! This adds a diagnostics with output frequency 'End' to the HEMCO diagnostics
     113             : ! list. Arr3D is the 3D field of interest. The diagnostics will not copy the
     114             : ! current content of Arr3D but establish a 'link' (e.g. pointer) to it. This
     115             : ! way, any updates to Arr3D will automatically be seen by the diagnostics
     116             : ! and there is no need to explicitly update the content of the diagnostics.
     117             : !\\
     118             : !\\
     119             : ! !INTERFACE:
     120             : !
     121           0 :   SUBROUTINE HCO_RestartDefine_3D( HcoState, Name, Arr3D, Unit, RC )
     122             : !
     123             : ! !USES:
     124             : !
     125             :     USE HCO_DIAGN_MOD,    ONLY : Diagn_Create
     126             :     USE HCO_STATE_MOD,    ONLY : HCO_State
     127             : !
     128             : ! !INPUT ARGUMENTS:
     129             : !
     130             :     TYPE(HCO_State),     POINTER               :: HcoState  ! HEMCO state obj.
     131             :     CHARACTER(LEN=*),    INTENT(IN   )         :: Name      ! Name of restart variable
     132             :     ! Array with data of interest
     133             :     REAL(sp),            INTENT(IN   ), TARGET :: Arr3D(HcoState%NX,HcoState%NY,HcoState%NZ)
     134             :     CHARACTER(LEN=*),    INTENT(IN   )         :: Unit      ! Units of Arr3D
     135             : !
     136             : ! !INPUT/OUTPUT ARGUMENTS:
     137             : !
     138             :     INTEGER,             INTENT(INOUT)         :: RC        ! Return code
     139             : !
     140             : ! !REVISION HISTORY:
     141             : !  11 Mar 2015 - C. Keller - Initial version
     142             : !  See https://github.com/geoschem/hemco for complete history
     143             : !EOP
     144             : !------------------------------------------------------------------------------
     145             : !BOC
     146             : !
     147             : ! !LOCAL VARIABLES:
     148             : !
     149             :     CHARACTER(LEN=255)  :: LOC
     150             :     ! ================================================================
     151             :     ! HCO_RestartDefine_3D begins here
     152             :     ! ================================================================
     153           0 :     LOC = 'HCO_RestartDefine_3D (HCO_RESTART_MOD.F90)'
     154             : 
     155             :     ! Define diagnostics array
     156             :     CALL Diagn_Create ( HcoState,                               &
     157             :                         cName      = TRIM(Name),                &
     158             :                         ExtNr      = -1,                        &
     159             :                         Cat        = -1,                        &
     160             :                         Hier       = -1,                        &
     161             :                         HcoID      = -1,                        &
     162             :                         SpaceDim   =  3,                        &
     163             :                         OutUnit    = TRIM(Unit),                &
     164             :                         COL = HcoState%Diagn%HcoDiagnIDRestart, &
     165             :                         AutoFill   = 0,                         &
     166             :                         Trgt3D     = Arr3D,                     &
     167           0 :                         RC         = RC                          )
     168           0 :     IF ( RC /= HCO_SUCCESS ) THEN
     169           0 :         CALL HCO_ERROR( 'ERROR 0', RC, THISLOC=LOC )
     170           0 :         RETURN
     171             :     ENDIF
     172             : 
     173             :     ! Return w/ success
     174           0 :     RC = HCO_SUCCESS
     175             : 
     176             :   END SUBROUTINE HCO_RestartDefine_3D
     177             : !EOC
     178             : !------------------------------------------------------------------------------
     179             : !                   Harmonized Emissions Component (HEMCO)                    !
     180             : !------------------------------------------------------------------------------
     181             : !BOP
     182             : !
     183             : ! !ROUTINE: HCO_RestartDefine_2D
     184             : !
     185             : ! !DESCRIPTION: Subroutine HCO\_RestartDefine\_2D defines a restart diagnostics.
     186             : ! This adds a diagnostics with output frequency 'End' to the HEMCO diagnostics
     187             : ! list. Arr2D is the 2D field of interest. The diagnostics will not copy the
     188             : ! current content of Arr2D but establish a 'link' (e.g. pointer) to it. This
     189             : ! way, any updates to Arr2D will automatically be seen by the diagnostics
     190             : ! and there is no need to explicitly update the content of the diagnostics.
     191             : !\\
     192             : !\\
     193             : ! !INTERFACE:
     194             : !
     195           0 :   SUBROUTINE HCO_RestartDefine_2D( HcoState, Name, Arr2D, Unit, RC )
     196             : !
     197             : ! !USES:
     198             : !
     199             :     USE HCO_DIAGN_MOD,    ONLY : Diagn_Create
     200             :     USE HCO_STATE_MOD,    ONLY : HCO_State
     201             : !
     202             : ! !INPUT ARGUMENTS:
     203             : !
     204             :     TYPE(HCO_State),     POINTER               :: HcoState  ! HEMCO state obj.
     205             :     CHARACTER(LEN=*),    INTENT(IN   )         :: Name      ! Name of restart variable
     206             :     ! Array with data of interest
     207             :     REAL(sp),            INTENT(IN   ), TARGET :: Arr2D(HcoState%NX,HcoState%NY)
     208             :     CHARACTER(LEN=*),    INTENT(IN   )         :: Unit      ! Units of Arr2D
     209             : !
     210             : ! !INPUT/OUTPUT ARGUMENTS:
     211             : !
     212             :     INTEGER,             INTENT(INOUT)         :: RC        ! Return code
     213             : !
     214             : ! !REVISION HISTORY:
     215             : !  11 Mar 2015 - C. Keller - Initial version
     216             : !  See https://github.com/geoschem/hemco for complete history
     217             : !EOP
     218             : !------------------------------------------------------------------------------
     219             : !BOC
     220             : !
     221             : ! !LOCAL VARIABLES:
     222             : !
     223             :     CHARACTER(LEN=255)  :: LOC
     224             :     ! ================================================================
     225             :     ! HCO_RestartDefine_2D begins here
     226             :     ! ================================================================
     227           0 :     LOC = 'HCO_RestartDefine_2D (HCO_RESTART_MOD.F90)'
     228             : 
     229             :     ! Define diagnostics array
     230             :     CALL Diagn_Create ( HcoState,                               &
     231             :                         cName      = TRIM(Name),                &
     232             :                         ExtNr      = -1,                        &
     233             :                         Cat        = -1,                        &
     234             :                         Hier       = -1,                        &
     235             :                         HcoID      = -1,                        &
     236             :                         SpaceDim   =  2,                        &
     237             :                         OutUnit    = TRIM(Unit),                &
     238             :                         COL = HcoState%Diagn%HcoDiagnIDRestart, &
     239             :                         AutoFill   = 0,                         &
     240             :                         Trgt2D     = Arr2D,                     &
     241           0 :                         RC         = RC                          )
     242           0 :     IF ( RC /= HCO_SUCCESS ) THEN
     243           0 :         CALL HCO_ERROR( 'ERROR 1', RC, THISLOC=LOC )
     244           0 :         RETURN
     245             :     ENDIF
     246             : 
     247             :     ! Return w/ success
     248           0 :     RC = HCO_SUCCESS
     249             : 
     250             :   END SUBROUTINE HCO_RestartDefine_2D
     251             : !EOC
     252             : !------------------------------------------------------------------------------
     253             : !                   Harmonized Emissions Component (HEMCO)                    !
     254             : !------------------------------------------------------------------------------
     255             : !BOP
     256             : !
     257             : ! !ROUTINE: HCO_RestartGet_3D
     258             : !
     259             : ! !DESCRIPTION: Subroutine HCO\_RestartGet\_3D attempts to read a restart field.
     260             : ! In an ESMF environment, it first checks if the given field (name) is included
     261             : ! in the internal state object, in which case the data object is filled with
     262             : ! these values. If not found or if not in an ESMF environment, the HEMCO data
     263             : ! list (specified in the HEMCO configuration file) is searched. A default value
     264             : ! can be specified in case that no field could be imported via ESMF and/or the
     265             : ! HEMCO interface.
     266             : !\\
     267             : !\\
     268             : ! !INTERFACE:
     269             : !
     270           0 :   SUBROUTINE HCO_RestartGet_3D( HcoState, Name,   Arr3D, &
     271           0 :                                 RC,       FILLED, Def3D, DefVal  )
     272             : !
     273             : ! !USES:
     274             : !
     275             :     USE HCO_STATE_MOD,    ONLY : HCO_State
     276             :     USE HCO_EMISLIST_MOD, ONLY : HCO_GetPtr
     277             : !
     278             : ! !INPUT ARGUMENTS:
     279             : !
     280             :     TYPE(HCO_State),     POINTER                 :: HcoState  ! HEMCO state object
     281             :     CHARACTER(LEN=*),    INTENT(IN   )           :: Name      ! Name of restart variable
     282             :     ! Default value to be used if restart variable could not be found
     283             :     REAL(sp),            INTENT(IN   ), OPTIONAL :: Def3D(HcoState%NX,HcoState%NY,HcoState%NZ)
     284             :     ! Default uniform value to be used if restart variable could not be found and
     285             :     ! Def2D is not defined.
     286             :     REAL(sp),            INTENT(IN   ), OPTIONAL :: DefVal
     287             : !
     288             : ! !OUTPUT ARGUMENTS:
     289             : !
     290             :     LOGICAL,             INTENT(  OUT), OPTIONAL :: FILLED    ! Was the restart variable found?
     291             : !
     292             : ! !INPUT/OUTPUT ARGUMENTS:
     293             : !
     294             :     ! Data field with restart variable
     295             :     REAL(sp),            INTENT(INOUT)           :: Arr3D(HcoState%NX,HcoState%NY,HcoState%NZ)
     296             :     INTEGER,             INTENT(INOUT)           :: RC       ! Return code
     297             : !
     298             : ! !REVISION HISTORY:
     299             : !  11 Mar 2015 - C. Keller - Initial version
     300             : !  See https://github.com/geoschem/hemco for complete history
     301             : !EOP
     302             : !------------------------------------------------------------------------------
     303             : !BOC
     304             : !
     305             : ! !LOCAL VARIABLES:
     306             : !
     307           0 :     REAL(sp), POINTER    :: Ptr3D(:,:,:)
     308             :     LOGICAL              :: FLD
     309             :     CHARACTER(LEN=255)   :: MSG, LOC
     310             : 
     311             :     ! ================================================================
     312             :     ! HCO_RestartGet begins here
     313             :     ! ================================================================
     314             : 
     315             :     ! Init
     316           0 :     LOC   = 'HCO_RestartGet (HCO_RESTART_MOD.F90)'
     317           0 :     Ptr3D => NULL()
     318             : 
     319             :     ! Is the output array filled yet?
     320           0 :     FLD = .FALSE.
     321             : 
     322             :     ! ------------------------------------------------------------------
     323             :     ! Try to get from ESMF internal state
     324             :     ! ------------------------------------------------------------------
     325             : #if defined(ESMF_)
     326             :     CALL HCO_CopyFromIntnal_ESMF( HcoState, TRIM(Name),   &
     327             :                                   1,        FLD,      RC, Arr3D=Arr3D )
     328             :     IF ( RC /= HCO_SUCCESS ) THEN
     329             :         CALL HCO_ERROR( 'ERROR 2', RC, THISLOC=LOC )
     330             :         RETURN
     331             :     ENDIF
     332             : 
     333             :     ! Determine if fields has not been filled
     334             :     IF ( FLD ) THEN
     335             : #if defined(MODEL_GEOS)
     336             :        IF ( MAXVAL(Arr3D) <  0.0 ) FLD = .FALSE.
     337             : #else
     338             :        IF ( MAXVAL(Arr3D) <= 0.0 ) FLD = .FALSE.
     339             : #endif
     340             :     ENDIF
     341             : 
     342             :     ! Log output
     343             :     IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     344             :        IF ( HcoState%amIRoot .AND. FLD ) THEN
     345             :           MSG = 'Obtained restart variable from ESMF internal state: '//TRIM(Name)
     346             :           CALL HCO_MSG(HcoState%Config%Err,MSG)
     347             :        ENDIF
     348             :     ENDIF
     349             : #endif
     350             : 
     351             :     ! ------------------------------------------------------------------
     352             :     ! If not yet filled, try to get from HEMCO configuration
     353             :     ! ------------------------------------------------------------------
     354             :     IF ( .NOT. FLD ) THEN
     355             : 
     356             :        ! Try to get pointer from HEMCO configuration
     357           0 :        CALL HCO_GetPtr( HcoState, TRIM(Name), Ptr3D, RC, FILLED=FLD )
     358           0 :        IF ( RC /= HCO_SUCCESS ) THEN
     359           0 :            CALL HCO_ERROR( 'ERROR 3', RC, THISLOC=LOC )
     360           0 :            RETURN
     361             :        ENDIF
     362             : 
     363             :        ! Eventually pass data
     364           0 :        IF ( FLD ) THEN
     365           0 :           Arr3D = Ptr3D
     366             : 
     367             :           ! Log output
     368           0 :           IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     369           0 :              IF ( HcoState%amIRoot ) THEN
     370           0 :                 MSG = 'Obtained restart variable from HEMCO config: '//TRIM(Name)
     371           0 :                 CALL HCO_MSG(HcoState%Config%Err,MSG)
     372             :              ENDIF
     373             :           ENDIF
     374             :        ENDIF
     375             : 
     376             :        ! Cleanup
     377           0 :        Ptr3D => NULL()
     378             :     ENDIF
     379             : 
     380             :     ! ------------------------------------------------------------------
     381             :     ! If still not filled, assign default values
     382             :     ! ------------------------------------------------------------------
     383           0 :     IF ( .NOT. FLD ) THEN
     384           0 :        IF ( PRESENT(Def3D) ) THEN
     385           0 :           Arr3D = Def3D
     386           0 :           FLD   = .TRUE.
     387           0 :           IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     388           0 :              IF ( HcoState%amIRoot ) THEN
     389           0 :                 MSG = 'Filled restart variable with default 3D field: '//TRIM(Name)
     390           0 :                 CALL HCO_MSG(HcoState%Config%Err,MSG)
     391             :              ENDIF
     392             :           ENDIF
     393           0 :        ELSEIF( PRESENT(DefVal) ) THEN
     394           0 :           Arr3D = DefVal
     395           0 :           FLD   = .TRUE.
     396           0 :           IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     397           0 :              IF ( HcoState%amIRoot ) THEN
     398           0 :                 MSG = 'Filled restart variable with default scalar: '//TRIM(Name)
     399           0 :                 CALL HCO_MSG(HcoState%Config%Err,MSG)
     400             :              ENDIF
     401             :           ENDIF
     402             :        ENDIF
     403             :     ENDIF
     404             : 
     405             :     ! ------------------------------------------------------------------
     406             :     ! Leave
     407             :     ! ------------------------------------------------------------------
     408           0 :     IF ( PRESENT(FILLED) ) THEN
     409           0 :        FILLED = FLD
     410             :     ENDIF
     411             : 
     412             :     ! Return w/ success
     413           0 :     RC = HCO_SUCCESS
     414             : 
     415           0 :   END SUBROUTINE HCO_RestartGet_3D
     416             : !EOC
     417             : !------------------------------------------------------------------------------
     418             : !                   Harmonized Emissions Component (HEMCO)                    !
     419             : !------------------------------------------------------------------------------
     420             : !BOP
     421             : !
     422             : ! !ROUTINE: HCO_RestartGet_2D
     423             : !
     424             : ! !DESCRIPTION: Subroutine HCO\_RestartGet\_2D attempts to read a restart field.
     425             : ! In an ESMF environment, it first checks if the given field (name) is included
     426             : ! in the internal state object, in which case the data object is filled with
     427             : ! these values. If not found or if not in an ESMF environment, the HEMCO data
     428             : ! list (specified in the HEMCO configuration file) is searched. A default value
     429             : ! can be specified in case that no field could be imported via ESMF and/or the
     430             : ! HEMCO interface.
     431             : !\\
     432             : !\\
     433             : ! !INTERFACE:
     434             : !
     435           0 :   SUBROUTINE HCO_RestartGet_2D( HcoState, Name,   Arr2D, &
     436           0 :                                 RC,       FILLED, Def2D, DefVal  )
     437             : !
     438             : ! !USES:
     439             : !
     440             :     USE HCO_STATE_MOD,    ONLY : HCO_State
     441             :     USE HCO_EMISLIST_MOD, ONLY : HCO_GetPtr
     442             : !
     443             : ! !INPUT ARGUMENTS:
     444             : !
     445             :     TYPE(HCO_State),     POINTER                 :: HcoState  ! HEMCO state object
     446             :     CHARACTER(LEN=*),    INTENT(IN   )           :: Name      ! Name of restart variable
     447             :     ! Default value to be used if restart variable could not be found
     448             :     REAL(sp),            INTENT(IN   ), OPTIONAL :: Def2D(HcoState%NX,HcoState%NY)
     449             :     ! Default uniform value to be used if restart variable could not be found and
     450             :     ! Def2D is not defined.
     451             :     REAL(sp),            INTENT(IN   ), OPTIONAL :: DefVal
     452             : !
     453             : ! !OUTPUT ARGUMENTS:
     454             : !
     455             :     LOGICAL,             INTENT(  OUT), OPTIONAL :: FILLED    ! Was the restart variable found?
     456             : !
     457             : ! !INPUT/OUTPUT ARGUMENTS:
     458             : !
     459             :     ! Data field with restart variable
     460             :     REAL(sp),            INTENT(INOUT)           :: Arr2D(HcoState%NX,HcoState%NY)
     461             :     INTEGER,             INTENT(INOUT)           :: RC       ! Return code
     462             : !
     463             : ! !REVISION HISTORY:
     464             : !  11 Mar 2015 - C. Keller - Initial version
     465             : !  See https://github.com/geoschem/hemco for complete history
     466             : !EOP
     467             : !------------------------------------------------------------------------------
     468             : !BOC
     469             : !
     470             : ! !LOCAL VARIABLES:
     471             : !
     472           0 :     REAL(sp), POINTER    :: Ptr2D(:,:)
     473             :     LOGICAL              :: FLD
     474             :     CHARACTER(LEN=255)   :: MSG, LOC
     475             : 
     476             :     ! ================================================================
     477             :     ! HCO_RestartGet_2D begins here
     478             :     ! ================================================================
     479             : 
     480             :     ! Init
     481           0 :     LOC   = 'HCO_RestartGet_2D (HCO_RESTART_MOD.F90)'
     482           0 :     Ptr2D => NULL()
     483             : 
     484             :     ! Is the output array filled yet?
     485           0 :     FLD = .FALSE.
     486             : 
     487             :     ! ------------------------------------------------------------------
     488             :     ! Try to get from ESMF internal state
     489             :     ! ------------------------------------------------------------------
     490             : #if defined(ESMF_)
     491             :     CALL HCO_CopyFromIntnal_ESMF( HcoState, TRIM(Name),   &
     492             :                                   1,        FLD,      RC, Arr2D=Arr2D )
     493             :     IF ( RC /= HCO_SUCCESS ) THEN
     494             :         CALL HCO_ERROR( 'ERROR 4', RC, THISLOC=LOC )
     495             :         RETURN
     496             :     ENDIF
     497             : 
     498             :     ! Determine if field has been filled
     499             :     IF ( FLD ) THEN
     500             : #if defined(MODEL_GEOS)
     501             :        IF ( MAXVAL(Arr2D) <  0.0 ) FLD = .FALSE.
     502             : #else
     503             :        IF ( MAXVAL(Arr2D) <= 0.0 ) FLD = .FALSE.
     504             : #endif
     505             :     ENDIF
     506             : 
     507             :     ! Log output
     508             :     IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     509             :        IF ( HcoState%amIRoot .AND. FLD ) THEN
     510             :           MSG = 'Obtained restart variable from ESMF internal state: '//TRIM(Name)
     511             :           CALL HCO_MSG(HcoState%Config%Err,MSG)
     512             :           WRITE(*,*) TRIM(MSG)
     513             :        ENDIF
     514             :     ENDIF
     515             : #endif
     516             : 
     517             :     ! ------------------------------------------------------------------
     518             :     ! If not yet filled, try to get from HEMCO configuration
     519             :     ! ------------------------------------------------------------------
     520             :     IF ( .NOT. FLD ) THEN
     521             : 
     522             :        ! Try to get pointer from HEMCO configuration
     523           0 :        CALL HCO_GetPtr( HcoState, TRIM(Name), Ptr2D, RC, FOUND=FLD )
     524           0 :        IF ( RC /= HCO_SUCCESS ) THEN
     525           0 :            CALL HCO_ERROR( 'ERROR 5', RC, THISLOC=LOC )
     526           0 :            RETURN
     527             :        ENDIF
     528             : 
     529             :        ! Eventually pass data
     530           0 :        IF ( FLD ) THEN
     531           0 :           Arr2D = Ptr2D
     532             : 
     533             :           ! Log output
     534           0 :           IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     535           0 :              IF ( HcoState%amIRoot ) THEN
     536           0 :                 MSG = 'Obtained restart variable from HEMCO config: '//TRIM(Name)
     537           0 :                 CALL HCO_MSG(HcoState%Config%Err,MSG)
     538           0 :                 WRITE(*,*) TRIM(MSG)
     539             :              ENDIF
     540             :           ENDIF
     541             :        ENDIF
     542             : 
     543             :        ! Cleanup
     544           0 :        Ptr2D => NULL()
     545             :     ENDIF
     546             : 
     547             :     ! ------------------------------------------------------------------
     548             :     ! If still not filled, assign default values
     549             :     ! ------------------------------------------------------------------
     550           0 :     IF ( .NOT. FLD ) THEN
     551           0 :        IF ( PRESENT(Def2D) ) THEN
     552           0 :           Arr2D = Def2D
     553           0 :           FLD   = .TRUE.
     554           0 :           IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     555           0 :              IF ( HcoState%amIRoot ) THEN
     556           0 :                 MSG = 'Filled restart variable with default 2D field: '//TRIM(Name)
     557           0 :                 CALL HCO_MSG(HcoState%Config%Err,MSG)
     558           0 :                 WRITE(*,*) TRIM(MSG)
     559             :              ENDIF
     560             :           ENDIF
     561           0 :        ELSEIF( PRESENT(DefVal) ) THEN
     562           0 :           Arr2D = DefVal
     563           0 :           FLD   = .TRUE.
     564           0 :           IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     565           0 :              IF ( HcoState%amIRoot ) THEN
     566           0 :                 MSG = 'Filled restart variable with default scalar: '//TRIM(Name)
     567           0 :                 CALL HCO_MSG(HcoState%Config%Err,MSG)
     568           0 :                 WRITE(*,*) TRIM(MSG)
     569             :              ENDIF
     570             :           ENDIF
     571             :        ENDIF
     572             :     ENDIF
     573             : 
     574             :     ! ------------------------------------------------------------------
     575             :     ! Leave
     576             :     ! ------------------------------------------------------------------
     577           0 :     IF ( PRESENT(FILLED) ) FILLED = FLD
     578             : 
     579             :     ! Verbose
     580           0 :     IF ( HCO_IsVerb(HcoState%Config%Err ) ) THEN
     581           0 :        IF ( HcoState%amIRoot .AND. .NOT. FLD ) THEN
     582           0 :           MSG = 'No restart field found (2D): '//TRIM(Name)
     583           0 :           CALL HCO_MSG(HcoState%Config%Err,MSG)
     584           0 :           WRITE(*,*) TRIM(MSG)
     585             :        ENDIF
     586             :     ENDIF
     587             : 
     588             :     ! Return w/ success
     589           0 :     RC = HCO_SUCCESS
     590             : 
     591           0 :   END SUBROUTINE HCO_RestartGet_2D
     592             : !EOC
     593             : !------------------------------------------------------------------------------
     594             : !                   Harmonized Emissions Component (HEMCO)                    !
     595             : !------------------------------------------------------------------------------
     596             : !BOP
     597             : !
     598             : ! !ROUTINE: HCO_RestartWrite_3D
     599             : !
     600             : ! !DESCRIPTION: Subroutine HCO\_RestartWrite\_3D writes a restart variable to
     601             : ! the ESMF internal state. This is only of relevance in an ESMF environment.
     602             : ! The 'regular' HEMCO diagnostics created in HCO\_RestartDefine becomes
     603             : ! automatically written to disk.
     604             : !\\
     605             : !\\
     606             : ! !INTERFACE:
     607             : !
     608           0 :   SUBROUTINE HCO_RestartWrite_3D( HcoState, Name, Arr3D, RC, FOUND )
     609             : !
     610             : ! !USES:
     611             : !
     612             :     USE HCO_STATE_MOD,    ONLY : HCO_State
     613             : !
     614             : ! !INPUT ARGUMENTS:
     615             : !
     616             :     TYPE(HCO_State),     POINTER                 :: HcoState
     617             :     CHARACTER(LEN=*),    INTENT(IN   )           :: Name
     618             : !
     619             : ! !OUTPUT ARGUMENTS:
     620             : !
     621             :     LOGICAL,             INTENT(  OUT), OPTIONAL :: FOUND
     622             : !
     623             : ! !INPUT/OUTPUT ARGUMENTS:
     624             : !
     625             :     REAL(sp),            INTENT(INOUT)           :: Arr3D(HcoState%NX,HcoState%NY,HcoState%NZ)
     626             :     INTEGER,             INTENT(INOUT)           :: RC
     627             : !
     628             : ! !REVISION HISTORY:
     629             : !  11 Mar 2015 - C. Keller - Initial version
     630             : !  See https://github.com/geoschem/hemco for complete history
     631             : !EOP
     632             : !------------------------------------------------------------------------------
     633             : !BOC
     634             : !
     635             : ! !LOCAL VARIABLES:
     636             : !
     637             :     LOGICAL   :: WRITTEN
     638             : 
     639             :     ! ================================================================
     640             :     ! HCO_RestartWrite_3D begins here
     641             :     ! ================================================================
     642             : 
     643             :     ! Data written to internal state?
     644           0 :     WRITTEN = .FALSE.
     645             : 
     646             : #if defined(ESMF_)
     647             :     CALL HCO_CopyFromIntnal_ESMF( HcoState, TRIM(Name), &
     648             :                                   -1,       WRITTEN,    RC, Arr3D=Arr3D )
     649             : #endif
     650             : 
     651             :     ! Pass to output
     652           0 :     IF ( PRESENT(FOUND) ) THEN
     653           0 :        FOUND = WRITTEN
     654             :     ENDIF
     655             : 
     656             :     ! Return w/ success
     657           0 :     RC = HCO_SUCCESS
     658             : 
     659           0 :   END SUBROUTINE HCO_RestartWrite_3D
     660             : !EOC
     661             : !------------------------------------------------------------------------------
     662             : !                   Harmonized Emissions Component (HEMCO)                    !
     663             : !------------------------------------------------------------------------------
     664             : !BOP
     665             : !
     666             : ! !ROUTINE: HCO_RestartWrite_2D
     667             : !
     668             : ! !DESCRIPTION: Subroutine HCO\_RestartWrite\_2D writes a restart variable to
     669             : ! the ESMF internal state. This is only of relevance in an ESMF environment.
     670             : ! The 'regular' HEMCO diagnostics created in HCO\_RestartDefine becomes
     671             : ! automatically written to disk.
     672             : !\\
     673             : !\\
     674             : ! !INTERFACE:
     675             : !
     676           0 :   SUBROUTINE HCO_RestartWrite_2D( HcoState, Name, Arr2D, RC, FOUND )
     677             : !
     678             : ! !USES:
     679             : !
     680             :     USE HCO_STATE_MOD,    ONLY : HCO_State
     681             : !
     682             : ! !INPUT ARGUMENTS:
     683             : !
     684             :     TYPE(HCO_State),     POINTER                 :: HcoState
     685             :     CHARACTER(LEN=*),    INTENT(IN   )           :: Name
     686             : !
     687             : ! !OUTPUT ARGUMENTS:
     688             : !
     689             :     LOGICAL,             INTENT(  OUT), OPTIONAL :: FOUND
     690             : !
     691             : ! !INPUT/OUTPUT ARGUMENTS:
     692             : !
     693             :     REAL(sp),            INTENT(INOUT)           :: Arr2D(HcoState%NX,HcoState%NY)
     694             :     INTEGER,             INTENT(INOUT)           :: RC
     695             : !
     696             : ! !REVISION HISTORY:
     697             : !  11 Mar 2015 - C. Keller - Initial version
     698             : !  See https://github.com/geoschem/hemco for complete history
     699             : !EOP
     700             : !------------------------------------------------------------------------------
     701             : !BOC
     702             : !
     703             : ! !LOCAL VARIABLES:
     704             : !
     705             :     LOGICAL   :: WRITTEN
     706             : 
     707             :     ! ================================================================
     708             :     ! HCO_RestartWrite_2D begins here
     709             :     ! ================================================================
     710             : 
     711             :     ! Data written to internal state?
     712           0 :     WRITTEN = .FALSE.
     713             : 
     714             : #if defined(ESMF_)
     715             :     CALL HCO_CopyFromIntnal_ESMF( HcoState, TRIM(Name), &
     716             :                                   -1,       WRITTEN,    RC, Arr2D=Arr2D )
     717             : #endif
     718             : 
     719             :     ! Pass to output
     720           0 :     IF ( PRESENT(FOUND) ) THEN
     721           0 :        FOUND = WRITTEN
     722             :     ENDIF
     723             : 
     724             :     ! Return w/ success
     725           0 :     RC = HCO_SUCCESS
     726             : 
     727           0 :   END SUBROUTINE HCO_RestartWrite_2D
     728             : !EOC
     729             : #if defined(ESMF_)
     730             : !------------------------------------------------------------------------------
     731             : !                   Harmonized Emissions Component (HEMCO)                    !
     732             : !------------------------------------------------------------------------------
     733             : !BOP
     734             : !
     735             : ! !ROUTINE: HCO_CopyFromIntnal_ESMF
     736             : !
     737             : ! !DESCRIPTION: Subroutine HCO\_CopyFromIntnal\_ESMF attempts to transfer
     738             : ! data to and from the ESMF/MAPL internal state.
     739             : !\\
     740             : !\\
     741             : ! !INTERFACE:
     742             : !
     743             :   SUBROUTINE HCO_CopyFromIntnal_ESMF ( HcoState,  Name,    &
     744             :                                        Direction, Found, RC, Arr2D, Arr3D )
     745             : !
     746             : ! !USES:
     747             : !
     748             : #include "MAPL_Generic.h"
     749             :     USE ESMF
     750             :     USE ESMFL_MOD
     751             :     USE MAPL_GenericMod
     752             :     USE MAPL_ErrorHandlingMod
     753             :     USE HCO_STATE_MOD,   ONLY : Hco_State
     754             : !
     755             : ! !ARGUMENTS:
     756             : !
     757             :     TYPE(HCO_State),     POINTER                 :: HcoState
     758             :     CHARACTER(LEN=*),    INTENT(IN   )           :: Name
     759             :     INTEGER,             INTENT(IN   )           :: Direction    ! 1: internal to Arr2D; -1: Arr2D to internal
     760             :     LOGICAL,             INTENT(  OUT)           :: Found
     761             :     INTEGER,             INTENT(INOUT)           :: RC
     762             :     REAL(sp),            INTENT(INOUT), OPTIONAL :: Arr2D(HcoState%NX,HcoState%NY)
     763             :     REAL(sp),            INTENT(INOUT), OPTIONAL :: Arr3D(HcoState%NX,HcoState%NY,HcoState%NZ)
     764             : !
     765             : ! !REVISION HISTORY:
     766             : !  10 Mar 2015 - C. Keller - Initial version
     767             : !  See https://github.com/geoschem/hemco for complete history
     768             : !EOP
     769             : !------------------------------------------------------------------------------
     770             : !BOC
     771             : !
     772             : ! !LOCAL VARIABLES:
     773             : !
     774             :     INTEGER                      :: STAT
     775             :     TYPE(MAPL_MetaComp), POINTER :: STATE
     776             :     TYPE(ESMF_STATE)             :: INTERNAL
     777             :     REAL,                POINTER :: Ptr2D(:,:)
     778             :     REAL,                POINTER :: Ptr3D(:,:,:)
     779             : 
     780             :     ! ================================================================
     781             :     ! HCO_CopyFromIntnal_ESMF begins here
     782             :     ! ================================================================
     783             : 
     784             :     ! For MAPL/ESMF error handling (defines Iam and STATUS)
     785             :     __Iam__('HCO_CopyFromIntnal_ESMF (hco_restart_mod.F90)')
     786             : 
     787             :     ! Init
     788             :     Ptr2D => NULL()
     789             :     Ptr3D => NULL()
     790             : 
     791             :     ! Get internal state
     792             :     CALL MAPL_GetObjectFromGC( HcoState%GridComp, STATE, __RC__ )
     793             :     CALL MAPL_Get ( STATE, INTERNAL_ESMF_STATE=INTERNAL, __RC__ )
     794             : 
     795             :     ! Try to import field
     796             :     IF ( PRESENT(Arr2D) ) THEN
     797             :        CALL MAPL_GetPointer( INTERNAL, Ptr2D, TRIM(Name), &
     798             :                              NotFoundOk=.TRUE., __RC__ )
     799             : 
     800             :        ! Eventually copy data to or from output array
     801             :        IF ( ASSOCIATED(Ptr2D) ) THEN
     802             : 
     803             :           ! Make sure we can copy the data
     804             :           ASSERT_(SIZE(Arr2D,1)==SIZE(Ptr2D,1))
     805             :           ASSERT_(SIZE(Arr2D,2)==SIZE(Ptr2D,2))
     806             : 
     807             :           ! transfer direction must be 1 or -1
     808             :           ASSERT_(Direction==1 .OR. Direction==-1)
     809             : 
     810             :           ! Transfer data
     811             :           IF ( Direction == 1 ) THEN
     812             :              Arr2D = Ptr2D
     813             :           ELSEIF ( Direction == -1 ) THEN
     814             :              Ptr2D = Arr2D
     815             :           ENDIF
     816             :           Found = .TRUE.
     817             :        ELSE
     818             :           Found = .FALSE.
     819             :        ENDIF
     820             : 
     821             :        ! Cleanup
     822             :        Ptr2D => NULL()
     823             :     ENDIF
     824             : 
     825             :     ! Try to import field
     826             :     IF ( PRESENT(Arr3D) ) THEN
     827             :        CALL MAPL_GetPointer( INTERNAL, Ptr3D, TRIM(Name), &
     828             :                              NotFoundOk=.TRUE., __RC__ )
     829             : 
     830             :        ! Eventually copy data to or from output array
     831             :        IF ( ASSOCIATED(Ptr3D) ) THEN
     832             : 
     833             :           ! Make sure we can copy the data
     834             :           ASSERT_(SIZE(Arr3D,1)==SIZE(Ptr3D,1))
     835             :           ASSERT_(SIZE(Arr3D,2)==SIZE(Ptr3D,2))
     836             :           ASSERT_(SIZE(Arr3D,3)==SIZE(Ptr3D,3))
     837             : 
     838             :           ! transfer direction must be 1 or -1
     839             :           ASSERT_(Direction==1 .OR. Direction==-1)
     840             : 
     841             :           ! Transfer data
     842             :           IF ( Direction == 1 ) THEN
     843             :              Arr3D = Ptr3D
     844             :           ELSEIF ( Direction == -1 ) THEN
     845             :              Ptr3D = Arr3D
     846             :           ENDIF
     847             :           Found = .TRUE.
     848             :        ELSE
     849             :           Found = .FALSE.
     850             :        ENDIF
     851             : 
     852             :        ! Cleanup
     853             :        Ptr3D => NULL()
     854             :     ENDIF
     855             : 
     856             :     ! Return success
     857             :     RC = HCO_SUCCESS
     858             : 
     859             :   END SUBROUTINE HCO_CopyFromIntnal_ESMF
     860             : !EOC
     861             : #endif
     862             : END MODULE HCO_RESTART_MOD

Generated by: LCOV version 1.14