subroutine ESMF_StateWrite(state, fileName, rc)
!
! !ARGUMENTS:
type(ESMF_State), intent(in) :: state
character (len=*), intent(in) :: fileName
integer, intent(out), optional :: rc
!
! !DESCRIPTION:
! Currently limited to write out all Arrays of a State object to a
! netCDF file. Future releases will enable more item types of a State to
! be written to files of various formats.
!
! Writing is currently limited to PET 0; future versions of ESMF will allow
! parallel writing, as well as parallel reading.
!
! See Section~\ref{example:StateRdWr} for an example.
!
! Note that the third party NetCDF library must be installed. For more
! details, see the "ESMF Users Guide",
! "Building and Installing the ESMF, Third Party Libraries, NetCDF" and
! the website http://www.unidata.ucar.edu/software/netcdf.
!
! The arguments are:
! \begin{description}
! \item[state]
! The {\tt ESMF\_State} from which to write items. Currently limited to
! Arrays.
! \item[fileName]
! File to be written.
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! Equals {\tt ESMF\_RC\_LIB\_NOT\_PRESENT} if the NetCDF library is
! not present.
! \end{description}
!
!EOP
! TODO: use item flag ESMF_STATEITEM_ARRAY<BUNDLE>
integer :: localrc
! Initialize return code; assume failure until success is certain
if (present(rc)) rc = ESMF_RC_NOT_IMPL
localrc = ESMF_RC_NOT_IMPL
! check input variables
ESMF_INIT_CHECK_DEEP(ESMF_StateGetInit,state,rc)
if (fileName == ' ') then
if (ESMF_LogFoundError (ESMF_RC_ARG_VALUE, msg='File name required', &
ESMF_CONTEXT, rcToReturn=rc)) return
end if
! invoke C to C++ entry point
call c_ESMC_StateWrite(state, state%statep%base, trim (fileName), localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
if (present(rc)) rc = ESMF_SUCCESS
end subroutine ESMF_StateWrite