recursive subroutine MAPL_GenericStateSave( GC, IMPORT, EXPORT, CLOCK, RC )
type(ESMF_GridComp), intent(inout) :: GC ! composite gridded component
type(ESMF_State), intent(inout) :: IMPORT ! import state
type(ESMF_State), intent(inout) :: EXPORT ! export state
type(ESMF_Clock), intent(inout) :: CLOCK ! the clock
integer, optional, intent( out) :: RC ! Error code:
type(mapl_metacomp), pointer :: state
integer :: i,filetype
character(len=14) :: datestamp
character(len=1) :: separator
character(len=ESMF_MAXSTR) :: filetypechar
character(len=4) :: extension
integer :: hdr
integer :: status
character(len=:), allocatable :: tmpstr
character(len=ESMF_MAXSTR) :: filename
character(len=ESMF_MAXSTR) :: CFILETYPE
type(ESMF_GridComp), pointer :: gridcomp
type(ESMF_State), pointer :: child_import_state
type(ESMF_State), pointer :: child_export_state
type (ESMF_State), pointer :: internal_state
_UNUSED_DUMMY(EXPORT)
call MAPL_InternalStateRetrieve(GC, STATE, RC=status)
_VERIFY(status)
call MAPL_GetResource( STATE, FILENAME, &
LABEL="IMPORT_CHECKPOINT_FILE:", &
RC=status)
if(status==ESMF_SUCCESS) then
_ASSERT(.not.allocated(state%initial_state%imp_fname),"can only save one state")
STATE%initial_state%IMP_FNAME = FILENAME
end if
call MAPL_GetResource( STATE , filename, &
LABEL="INTERNAL_CHECKPOINT_FILE:", &
RC=status)
if(status==ESMF_SUCCESS) then
_ASSERT(.not.allocated(state%initial_state%int_fname),"can only save one state")
STATE%initial_state%INT_FNAME = FILENAME
end if
do I=1,STATE%get_num_children()
gridcomp => STATE%GET_CHILD_GRIDCOMP(I)
child_import_state => STATE%get_child_import_state(i)
child_export_state => STATE%get_child_export_state(i)
call MAPL_GenericStateSave (gridcomp, &
child_import_state, &
child_export_state, &
CLOCK, RC=status )
_VERIFY(status)
enddo
call MAPL_DateStampGet(clock, datestamp, rc=status)
_VERIFY(status)
filetype=state%initial_state%filetype
if (FILETYPE /= MAPL_Write2Disk) then
separator = '*'
else
separator = '.'
end if
if (allocated(state%initial_state%imp_fname)) then
call MAPL_GetResource( STATE, filetypechar, LABEL="IMPORT_CHECKPOINT_TYPE:", RC=status )
if ( status/=ESMF_SUCCESS .or. filetypechar == "default" ) then
call MAPL_GetResource( STATE, filetypechar, LABEL="DEFAULT_CHECKPOINT_TYPE:", default='pnc4', RC=status )
_VERIFY(status)
end if
filetypechar = ESMF_UtilStringLowerCase(filetypechar,rc=status)
_VERIFY(status)
if (filetypechar == 'pnc4') then
extension = '.nc4'
else
extension = '.bin'
end if
tmpstr=trim(state%initial_state%imp_fname)
deallocate(state%initial_state%imp_fname)
STATE%initial_state%IMP_FNAME = tmpstr // separator // DATESTAMP // extension
deallocate(tmpstr)
end if
if (allocated(state%initial_state%int_fname)) then
call MAPL_GetResource( STATE, hdr, LABEL="INTERNAL_HEADER:", default=0, RC=status )
_VERIFY(status)
call MAPL_GetResource( STATE, filetypechar, LABEL="INTERNAL_CHECKPOINT_TYPE:", RC=status )
if ( status/=ESMF_SUCCESS .or. filetypechar == "default" ) then
call MAPL_GetResource( STATE, filetypechar, LABEL="DEFAULT_CHECKPOINT_TYPE:", default='pnc4', RC=status )
_VERIFY(status)
end if
filetypechar = ESMF_UtilStringLowerCase(filetypechar,rc=status)
_VERIFY(status)
if (filetypechar == 'pnc4') then
extension = '.nc4'
else
extension = '.bin'
end if
tmpstr=trim(state%initial_state%int_fname)
deallocate(state%initial_state%int_fname)
STATE%initial_state%INT_FNAME = tmpstr // separator // DATESTAMP // extension
deallocate(tmpstr)
end if
if (allocated(state%initial_state%imp_fname)) then
call MAPL_GetResource( STATE, CFILETYPE, LABEL="IMPORT_CHECKPOINT_TYPE:", RC=status )
if ( status/=ESMF_SUCCESS .or. CFILETYPE == "default" ) then
call MAPL_GetResource( STATE, CFILETYPE, LABEL="DEFAULT_CHECKPOINT_TYPE:", default='pnc4', RC=status )
_VERIFY(status)
end if
call MAPL_ESMFStateWriteToFile(IMPORT, CLOCK, &
STATE%initial_state%IMP_FNAME, &
CFILETYPE, STATE, .FALSE., write_with_oserver = state%grid%write_restart_by_oserver, &
RC=status)
_VERIFY(status)
end if
if (allocated(state%initial_state%int_fname)) then
call MAPL_GetResource( STATE, hdr, LABEL="INTERNAL_HEADER:", default=0, RC=status )
_VERIFY(status)
call MAPL_GetResource( STATE, CFILETYPE, LABEL="INTERNAL_CHECKPOINT_TYPE:", RC=status )
if ( status/=ESMF_SUCCESS .or. CFILETYPE == "default" ) then
call MAPL_GetResource( STATE, CFILETYPE, LABEL="DEFAULT_CHECKPOINT_TYPE:", default='pnc4', RC=status )
_VERIFY(status)
end if
internal_state => STATE%get_internal_state()
call MAPL_ESMFStateWriteToFile(internal_state, CLOCK, &
STATE%initial_state%INT_FNAME, &
CFILETYPE, STATE, hdr/=0, write_with_oserver = state%grid%write_restart_by_oserver, &
RC=status)
_VERIFY(status)
end if
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_GenericStateSave