\hypertarget{namespacemom__sum__output}{}\section{mom\+\_\+sum\+\_\+output Module Reference}
\label{namespacemom__sum__output}\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}


\subsection{Detailed Description}
Reports integrated quantities for monitoring the model state. 

By Robert Hallberg, April 1994 -\/ June 2002.

This file contains the subroutine (write\+\_\+energy) that writes horizontally integrated quantities, such as energies and layer volumes, and other summary information to an output file. Some of these quantities (A\+PE or resting interface height) are defined relative to the global histogram of topography. The subroutine that compiles that histogram (depth\+\_\+list\+\_\+setup) is also included in this file.

In addition, if the number of velocity truncations since the previous call to write\+\_\+energy exceeds maxtrunc or the total energy exceeds a very large threshold, a fatal termination is triggered. \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__sum__output_1_1depth__list}{depth\+\_\+list}}
\begin{DoxyCompactList}\small\item\em A list of depths and corresponding globally integrated ocean area at each depth and the ocean volume below each depth. \end{DoxyCompactList}\item 
type \mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em The control structure for the M\+O\+M\+\_\+sum\+\_\+output module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__sum__output_a05003e74cbb39fbdf760d23617390445}{mom\+\_\+sum\+\_\+output\+\_\+init}} (G, US, param\+\_\+file, directory, ntrnc, Input\+\_\+start\+\_\+time, CS)
\begin{DoxyCompactList}\small\item\em M\+O\+M\+\_\+sum\+\_\+output\+\_\+init initializes the parameters and settings for the M\+O\+M\+\_\+sum\+\_\+output module. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__sum__output_ae54994f461b38198510274dadbce8fb5}{mom\+\_\+sum\+\_\+output\+\_\+end}} (CS)
\begin{DoxyCompactList}\small\item\em M\+O\+M\+\_\+sum\+\_\+output\+\_\+end deallocates memory used by the M\+O\+M\+\_\+sum\+\_\+output module. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__sum__output_ad3cc692dd515100ec8cf92d740c91e72}{write\+\_\+energy}} (u, v, h, tv, day, n, G, GV, US, CS, tracer\+\_\+\+C\+Sp, O\+BC, dt\+\_\+forcing)
\begin{DoxyCompactList}\small\item\em This subroutine calculates and writes the total model energy, the energy and mass of each layer, and other globally integrated physical quantities. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__sum__output_a773833f7ac86b8b65b0e6e8f91f05ec4}{accumulate\+\_\+net\+\_\+input}} (fluxes, sfc\+\_\+state, tv, dt, G, US, CS)
\begin{DoxyCompactList}\small\item\em This subroutine accumates the net input of volume, salt and heat, through the ocean surface for use in diagnosing conservation. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__sum__output_a53d3dcd50cba41760dd8713228785a8d}{depth\+\_\+list\+\_\+setup}} (G, US, CS)
\begin{DoxyCompactList}\small\item\em This subroutine sets up an ordered list of depths, along with the cross sectional areas at each depth and the volume of fluid deeper than each depth. This might be read from a previously created file or it might be created anew. (For now only new creation occurs. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__sum__output_a1db5001777c2171a7f3f16122b4bacd1}{create\+\_\+depth\+\_\+list}} (G, CS)
\begin{DoxyCompactList}\small\item\em create\+\_\+depth\+\_\+list makes an ordered list of depths, along with the cross sectional areas at each depth and the volume of fluid deeper than each depth. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__sum__output_a955e777980a668bfc611c78de5de1895}{write\+\_\+depth\+\_\+list}} (G, US, CS, filename, list\+\_\+size)
\begin{DoxyCompactList}\small\item\em This subroutine writes out the depth list to the specified file. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__sum__output_a4f45b1c7dbff4fe064e102ccb2967daf}{read\+\_\+depth\+\_\+list}} (G, US, CS, filename)
\begin{DoxyCompactList}\small\item\em This subroutine reads in the depth list to the specified file and allocates and sets up CSDL and CSlist\+\_\+size . \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__sum__output_a928dd1160bb6c0033a417c618b9d01ef}{get\+\_\+depth\+\_\+list\+\_\+checksums}} (G, depth\+\_\+chksum, area\+\_\+chksum)
\begin{DoxyCompactList}\small\item\em Return the checksums required to verify D\+E\+P\+T\+H\+\_\+\+L\+I\+S\+T\+\_\+\+F\+I\+LE contents. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Variables}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{namespacemom__sum__output_a5191c3198dcd24f50da9279ce7ebbc60}\label{namespacemom__sum__output_a5191c3198dcd24f50da9279ce7ebbc60}} 
integer, parameter \mbox{\hyperlink{namespacemom__sum__output_a5191c3198dcd24f50da9279ce7ebbc60}{num\+\_\+fields}} = 17
\begin{DoxyCompactList}\small\item\em Number of diagnostic fields. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__sum__output_a4502602fe6c41088fc5a7f658070b386}\label{namespacemom__sum__output_a4502602fe6c41088fc5a7f658070b386}} 
character($\ast$), parameter \mbox{\hyperlink{namespacemom__sum__output_a4502602fe6c41088fc5a7f658070b386}{depth\+\_\+chksum\+\_\+attr}} = \char`\"{}bathy\+T\+\_\+checksum\char`\"{}
\begin{DoxyCompactList}\small\item\em Checksum attribute name of GbathyT over the compute domain. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__sum__output_af8db0fa8cff32727c5f23cc328b67a7c}\label{namespacemom__sum__output_af8db0fa8cff32727c5f23cc328b67a7c}} 
character($\ast$), parameter \mbox{\hyperlink{namespacemom__sum__output_af8db0fa8cff32727c5f23cc328b67a7c}{area\+\_\+chksum\+\_\+attr}} = \char`\"{}mask2d\+T\+\_\+area\+T\+\_\+checksum\char`\"{}
\begin{DoxyCompactList}\small\item\em Checksum attribute of name of Gmask2dT $\ast$ GareaT over the compute domain. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__sum__output_a773833f7ac86b8b65b0e6e8f91f05ec4}\label{namespacemom__sum__output_a773833f7ac86b8b65b0e6e8f91f05ec4}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!accumulate\+\_\+net\+\_\+input@{accumulate\+\_\+net\+\_\+input}}
\index{accumulate\+\_\+net\+\_\+input@{accumulate\+\_\+net\+\_\+input}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{accumulate\+\_\+net\+\_\+input()}{accumulate\_net\_input()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+sum\+\_\+output\+::accumulate\+\_\+net\+\_\+input (\begin{DoxyParamCaption}\item[{type(forcing), intent(in)}]{fluxes,  }\item[{type(surface), intent(in)}]{sfc\+\_\+state,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



This subroutine accumates the net input of volume, salt and heat, through the ocean surface for use in diagnosing conservation. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em fluxes} & A structure containing pointers to any possible forcing fields. Unused fields are unallocated.\\
\hline
\mbox{\tt in}  & {\em sfc\+\_\+state} & A structure containing fields that describe the surface state of the ocean.\\
\hline
\mbox{\tt in}  & {\em tv} & A structure pointing to various thermodynamic variables.\\
\hline
\mbox{\tt in}  & {\em dt} & The amount of time over which to average \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & The control structure returned by a previous call to M\+O\+M\+\_\+sum\+\_\+output\+\_\+init. \\
\hline
\end{DoxyParams}


Definition at line 953 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
953   \textcolor{keywordtype}{type}(forcing),         \textcolor{keywordtype}{intent(in)} :: fluxes\textcolor{comment}{ !< A structure containing pointers to any possible}
954 \textcolor{comment}{                                              !! forcing fields.  Unused fields are unallocated.}
955   \textcolor{keywordtype}{type}(surface),         \textcolor{keywordtype}{intent(in)} :: sfc\_state\textcolor{comment}{ !< A structure containing fields that}
956 \textcolor{comment}{                                              !! describe the surface state of the ocean.}
957   \textcolor{keywordtype}{type}(thermo\_var\_ptrs), \textcolor{keywordtype}{intent(in)} :: tv\textcolor{comment}{     !< A structure pointing to various}
958 \textcolor{comment}{                                              !! thermodynamic variables.}
959   \textcolor{keywordtype}{real},                  \textcolor{keywordtype}{intent(in)} :: dt\textcolor{comment}{     !< The amount of time over which to average [T ~> s].}
960   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{      !< The ocean's grid structure.}
961   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)} :: US\textcolor{comment}{     !< A dimensional unit scaling type}
962   \textcolor{keywordtype}{type}(Sum\_output\_CS),   \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{     !< The control structure returned by a previous call}
963 \textcolor{comment}{                                              !! to MOM\_sum\_output\_init.}
964   \textcolor{comment}{! Local variables}
965   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: &
966     FW\_in, &   \textcolor{comment}{! The net fresh water input, integrated over a timestep [kg].}
967     salt\_in, & \textcolor{comment}{! The total salt added by surface fluxes, integrated}
968                \textcolor{comment}{! over a time step [ppt kg].}
969     heat\_in    \textcolor{comment}{! The total heat added by surface fluxes, integrated}
970                \textcolor{comment}{! over a time step [J].}
971   \textcolor{keywordtype}{real} :: FW\_input   \textcolor{comment}{! The net fresh water input, integrated over a timestep}
972                      \textcolor{comment}{! and summed over space [kg].}
973   \textcolor{keywordtype}{real} :: salt\_input \textcolor{comment}{! The total salt added by surface fluxes, integrated}
974                      \textcolor{comment}{! over a time step and summed over space [ppt kg].}
975   \textcolor{keywordtype}{real} :: heat\_input \textcolor{comment}{! The total heat added by boundary fluxes, integrated}
976                      \textcolor{comment}{! over a time step and summed over space [J].}
977   \textcolor{keywordtype}{real} :: RZL2\_to\_kg \textcolor{comment}{! A combination of scaling factors for mass [kg R-1 Z-1 L-2 ~> 1]}
978   \textcolor{keywordtype}{real} :: QRZL2\_to\_J \textcolor{comment}{! A combination of scaling factors for heat [J Q-1 R-1 Z-1 L-2 ~> 1]}
979 
980   \textcolor{keywordtype}{type}(EFP\_type) :: &
981     FW\_in\_EFP,   &   \textcolor{comment}{! The net fresh water input, integrated over a timestep}
982                      \textcolor{comment}{! and summed over space [kg].}
983     salt\_in\_efp, &   \textcolor{comment}{! The total salt added by surface fluxes, integrated}
984                      \textcolor{comment}{! over a time step and summed over space [ppt kg].}
985     heat\_in\_efp      \textcolor{comment}{! The total heat added by boundary fluxes, integrated}
986                      \textcolor{comment}{! over a time step and summed over space [J].}
987 
988   \textcolor{keywordtype}{real} :: inputs(3)   \textcolor{comment}{! A mixed array for combining the sums}
989   \textcolor{keywordtype}{integer} :: i, j, is, ie, js, je, isr, ier, jsr, jer
990 
991   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec
992 
993   rzl2\_to\_kg = us%L\_to\_m**2*us%RZ\_to\_kg\_m2
994   qrzl2\_to\_j = rzl2\_to\_kg*us%Q\_to\_J\_kg
995 
996   fw\_in(:,:) = 0.0
997   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%evap)) \textcolor{keywordflow}{then}
998     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%lprec) .and. \textcolor{keyword}{associated}(fluxes%fprec)) \textcolor{keywordflow}{then}
999       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1000         fw\_in(i,j) = rzl2\_to\_kg * dt*g%areaT(i,j)*(fluxes%evap(i,j) + &
1001             (((fluxes%lprec(i,j) + fluxes%vprec(i,j)) + fluxes%lrunoff(i,j)) + &
1002               (fluxes%fprec(i,j) + fluxes%frunoff(i,j))))
1003 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1004     \textcolor{keywordflow}{else}
1005       \textcolor{keyword}{call }mom\_error(warning, &
1006         \textcolor{stringliteral}{"accumulate\_net\_input called with associated evap field, but no precip field."})
1007 \textcolor{keywordflow}{    endif}
1008 \textcolor{keywordflow}{  endif}
1009 
1010   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%seaice\_melt)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1011     fw\_in(i,j) = fw\_in(i,j) + rzl2\_to\_kg*dt * &
1012                  g%areaT(i,j) * fluxes%seaice\_melt(i,j)
1013 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
1014 
1015   salt\_in(:,:) = 0.0 ; heat\_in(:,:) = 0.0
1016   \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
1017 
1018     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%sw)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1019       heat\_in(i,j) = heat\_in(i,j) + dt * qrzl2\_to\_j * g%areaT(i,j) * (fluxes%sw(i,j) + &
1020              (fluxes%lw(i,j) + (fluxes%latent(i,j) + fluxes%sens(i,j))))
1021 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
1022 
1023     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%seaice\_melt\_heat)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1024        heat\_in(i,j) = heat\_in(i,j) + dt * qrzl2\_to\_j * g%areaT(i,j) * &
1025                                      fluxes%seaice\_melt\_heat(i,j)
1026 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
1027 
1028     \textcolor{comment}{! smg: new code}
1029     \textcolor{comment}{! include heat content from water transport across ocean surface}
1030 \textcolor{comment}{!    if (associated(fluxes%heat\_content\_lprec)) then ; do j=js,je ; do i=is,ie}
1031 \textcolor{comment}{!      heat\_in(i,j) = heat\_in(i,j) + dt * QRZL2\_to\_J * G%areaT(i,j) * &}
1032 \textcolor{comment}{!         (fluxes%heat\_content\_lprec(i,j)   + (fluxes%heat\_content\_fprec(i,j)   &}
1033 \textcolor{comment}{!       + (fluxes%heat\_content\_lrunoff(i,j) + (fluxes%heat\_content\_frunoff(i,j) &}
1034 \textcolor{comment}{!       + (fluxes%heat\_content\_cond(i,j)    + (fluxes%heat\_content\_vprec(i,j)   &}
1035 \textcolor{comment}{!       +  fluxes%heat\_content\_massout(i,j)))))))}
1036 \textcolor{comment}{!    enddo ; enddo ; endif}
1037 
1038     \textcolor{comment}{! smg: old code}
1039     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%TempxPmE)) \textcolor{keywordflow}{then}
1040       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1041         heat\_in(i,j) = heat\_in(i,j) + (fluxes%C\_p * qrzl2\_to\_j*g%areaT(i,j)) * tv%TempxPmE(i,j)
1042 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1043     \textcolor{keywordflow}{elseif} (\textcolor{keyword}{associated}(fluxes%evap)) \textcolor{keywordflow}{then}
1044       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1045         heat\_in(i,j) = heat\_in(i,j) + (us%Q\_to\_J\_kg*fluxes%C\_p * sfc\_state%SST(i,j)) * fw\_in(i,j)
1046 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1047 \textcolor{keywordflow}{    endif}
1048 
1049     \textcolor{comment}{! The following heat sources may or may not be used.}
1050     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%internal\_heat)) \textcolor{keywordflow}{then}
1051       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1052         heat\_in(i,j) = heat\_in(i,j) + (fluxes%C\_p * qrzl2\_to\_j*g%areaT(i,j)) * tv%internal\_heat(i,j)
1053 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1054 \textcolor{keywordflow}{    endif}
1055     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%frazil)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1056       heat\_in(i,j) = heat\_in(i,j) + qrzl2\_to\_j * g%areaT(i,j) * tv%frazil(i,j)
1057 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
1058     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%heat\_added)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1059       heat\_in(i,j) = heat\_in(i,j) + qrzl2\_to\_j * dt*g%areaT(i,j) * fluxes%heat\_added(i,j)
1060 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
1061 \textcolor{comment}{!    if (associated(sfc\_state%sw\_lost)) then ; do j=js,je ; do i=is,ie}
1062 \textcolor{comment}{!      heat\_in(i,j) = heat\_in(i,j) - US%L\_to\_m**2*G%areaT(i,j) * sfc\_state%sw\_lost(i,j)}
1063 \textcolor{comment}{!    enddo ; enddo ; endif}
1064 
1065     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%salt\_flux)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1066       \textcolor{comment}{! convert salt\_flux from kg (salt)/(m^2 s) to ppt * [m s-1].}
1067       salt\_in(i,j) = rzl2\_to\_kg * dt * &
1068                      g%areaT(i,j)*(1000.0*fluxes%salt\_flux(i,j))
1069 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
1070 \textcolor{keywordflow}{  endif}
1071 
1072   \textcolor{keywordflow}{if} ((cs%use\_temperature) .or. \textcolor{keyword}{associated}(fluxes%lprec) .or. &
1073       \textcolor{keyword}{associated}(fluxes%evap)) \textcolor{keywordflow}{then}
1074     \textcolor{comment}{! The on-PE sums are stored here, but the sums across PEs are deferred to}
1075     \textcolor{comment}{! the next call to write\_energy to avoid extra barriers.}
1076     isr = is - (g%isd-1) ; ier = ie - (g%isd-1) ; jsr = js - (g%jsd-1) ; jer = je - (g%jsd-1)
1077     fw\_in\_efp   = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum__efp}{reproducing\_sum\_efp}}(fw\_in,   isr, ier, jsr, jer, only\_on\_pe=.true.)
1078     heat\_in\_efp = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum__efp}{reproducing\_sum\_efp}}(heat\_in, isr, ier, jsr, jer, only\_on\_pe=.true.)
1079     salt\_in\_efp = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum__efp}{reproducing\_sum\_efp}}(salt\_in, isr, ier, jsr, jer, only\_on\_pe=.true.)
1080 
1081     cs%fresh\_water\_in\_EFP = cs%fresh\_water\_in\_EFP + fw\_in\_efp
1082     cs%net\_salt\_in\_EFP    = cs%net\_salt\_in\_EFP    + salt\_in\_efp
1083     cs%net\_heat\_in\_EFP    = cs%net\_heat\_in\_EFP    + heat\_in\_efp
1084 \textcolor{keywordflow}{  endif}
1085 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__sum__output_a1db5001777c2171a7f3f16122b4bacd1}\label{namespacemom__sum__output_a1db5001777c2171a7f3f16122b4bacd1}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!create\+\_\+depth\+\_\+list@{create\+\_\+depth\+\_\+list}}
\index{create\+\_\+depth\+\_\+list@{create\+\_\+depth\+\_\+list}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{create\+\_\+depth\+\_\+list()}{create\_depth\_list()}}
{\footnotesize\ttfamily subroutine mom\+\_\+sum\+\_\+output\+::create\+\_\+depth\+\_\+list (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



create\+\_\+depth\+\_\+list makes an ordered list of depths, along with the cross sectional areas at each depth and the volume of fluid deeper than each depth. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
 & {\em cs} & The control structure set up in M\+O\+M\+\_\+sum\+\_\+output\+\_\+init, in which the ordered depth list is stored. \\
\hline
\end{DoxyParams}


Definition at line 1123 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
1123   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{  !< The ocean's grid structure.}
1124   \textcolor{keywordtype}{type}(Sum\_output\_CS),   \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{ !< The control structure set up in MOM\_sum\_output\_init,}
1125 \textcolor{comment}{                                          !! in which the ordered depth list is stored.}
1126   \textcolor{comment}{! Local variables}
1127   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(G%Domain%niglobal*G%Domain%njglobal + 1)} :: &
1128     Dlist, &  !< The global list of bottom depths [Z ~> m].
1129     AreaList\textcolor{comment}{  !< The global list of cell areas [L2 ~> m2].}
1130   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(G%Domain%niglobal*G%Domain%njglobal+1)} :: &
1131     indx2\textcolor{comment}{     !< The position of an element in the original unsorted list.}
1132   \textcolor{keywordtype}{real}    :: Dnow\textcolor{comment}{  !< The depth now being considered for sorting [Z ~> m].}
1133   \textcolor{keywordtype}{real}    :: Dprev\textcolor{comment}{ !< The most recent depth that was considered [Z ~> m].}
1134   \textcolor{keywordtype}{real}    :: vol\textcolor{comment}{   !< The running sum of open volume below a deptn [Z L2 ~> m3].}
1135   \textcolor{keywordtype}{real}    :: area\textcolor{comment}{  !< The open area at the current depth [L2 ~> m2].}
1136   \textcolor{keywordtype}{real}    :: D\_list\_prev\textcolor{comment}{ !< The most recent depth added to the list [Z ~> m].}
1137   \textcolor{keywordtype}{logical} :: add\_to\_list\textcolor{comment}{ !< This depth should be included as an entry on the list.}
1138 
1139   \textcolor{keywordtype}{integer} :: ir, indxt
1140   \textcolor{keywordtype}{integer} :: mls, list\_size
1141   \textcolor{keywordtype}{integer} :: list\_pos, i\_global, j\_global
1142   \textcolor{keywordtype}{integer} :: i, j, k, kl
1143 
1144   mls = g%Domain%niglobal*g%Domain%njglobal
1145 
1146 \textcolor{comment}{! Need to collect the global data from compute domains to a 1D array for sorting.}
1147   dlist(:) = 0.0
1148   arealist(:) = 0.0
1149   \textcolor{keywordflow}{do} j=g%jsc,g%jec ; \textcolor{keywordflow}{do} i=g%isc,g%iec
1150     \textcolor{comment}{! Set global indices that start the global domain at 1 (Fortran convention).}
1151     j\_global = j + g%jdg\_offset - (g%jsg-1)
1152     i\_global = i + g%idg\_offset - (g%isg-1)
1153 
1154     list\_pos = (j\_global-1)*g%Domain%niglobal + i\_global
1155     dlist(list\_pos) = g%bathyT(i,j)
1156     arealist(list\_pos) = g%mask2dT(i,j) * g%areaT(i,j)
1157 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
1158 
1159   \textcolor{comment}{! These sums reproduce across PEs because the arrays are only nonzero on one PE.}
1160   \textcolor{keyword}{call }sum\_across\_pes(dlist, mls+1)
1161   \textcolor{keyword}{call }sum\_across\_pes(arealist, mls+1)
1162 
1163   \textcolor{keywordflow}{do} j=1,mls+1 ; indx2(j) = j ;\textcolor{keywordflow}{ enddo}
1164   k = mls / 2  + 1 ; ir = mls
1165   \textcolor{keywordflow}{do}
1166     \textcolor{keywordflow}{if} (k > 1) \textcolor{keywordflow}{then}
1167       k = k - 1
1168       indxt = indx2(k)
1169       dnow = dlist(indxt)
1170     \textcolor{keywordflow}{else}
1171       indxt = indx2(ir)
1172       dnow = dlist(indxt)
1173       indx2(ir) = indx2(1)
1174       ir = ir - 1
1175       \textcolor{keywordflow}{if} (ir == 1) \textcolor{keywordflow}{then} ; indx2(1) = indxt ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif}
1176 \textcolor{keywordflow}{    endif}
1177     i=k ; j=k*2
1178     \textcolor{keywordflow}{do} ; \textcolor{keywordflow}{if} (j > ir) \textcolor{keywordflow}{exit}
1179       \textcolor{keywordflow}{if} (j < ir .AND. dlist(indx2(j)) < dlist(indx2(j+1))) j = j + 1
1180       \textcolor{keywordflow}{if} (dnow < dlist(indx2(j))) \textcolor{keywordflow}{then} ; indx2(i) = indx2(j) ; i = j ; j = j + i
1181       \textcolor{keywordflow}{else} ; j = ir+1 ;\textcolor{keywordflow}{ endif}
1182 \textcolor{keywordflow}{    enddo}
1183     indx2(i) = indxt
1184 \textcolor{keywordflow}{  enddo}
1185 
1186 \textcolor{comment}{!  At this point, the lists should perhaps be culled to save memory.}
1187 \textcolor{comment}{! Culling: (1) identical depths (e.g. land) - take the last one.}
1188 \textcolor{comment}{!          (2) the topmost and bottommost depths are always saved.}
1189 \textcolor{comment}{!          (3) very close depths}
1190 \textcolor{comment}{!          (4) equal volume changes.}
1191 
1192   \textcolor{comment}{! Count the unique elements in the list.}
1193   d\_list\_prev = dlist(indx2(mls))
1194   list\_size = 2
1195   \textcolor{keywordflow}{do} k=mls-1,1,-1
1196     \textcolor{keywordflow}{if} (dlist(indx2(k)) < d\_list\_prev-cs%D\_list\_min\_inc) \textcolor{keywordflow}{then}
1197       list\_size = list\_size + 1
1198       d\_list\_prev = dlist(indx2(k))
1199 \textcolor{keywordflow}{    endif}
1200 \textcolor{keywordflow}{  enddo}
1201 
1202   cs%list\_size = list\_size
1203   \textcolor{keyword}{allocate}(cs%DL(cs%list\_size+1))
1204 
1205   vol = 0.0 ; area = 0.0
1206   dprev = dlist(indx2(mls))
1207   d\_list\_prev = dprev
1208 
1209   kl = 0
1210   \textcolor{keywordflow}{do} k=mls,1,-1
1211     i = indx2(k)
1212     vol = vol + area * (dprev - dlist(i))
1213     area = area + arealist(i)
1214 
1215     add\_to\_list = .false.
1216     \textcolor{keywordflow}{if} ((kl == 0) .or. (k==1)) \textcolor{keywordflow}{then}
1217       add\_to\_list = .true.
1218     \textcolor{keywordflow}{elseif} (dlist(indx2(k-1)) < d\_list\_prev-cs%D\_list\_min\_inc) \textcolor{keywordflow}{then}
1219       add\_to\_list = .true.
1220       d\_list\_prev = dlist(indx2(k-1))
1221 \textcolor{keywordflow}{    endif}
1222 
1223     \textcolor{keywordflow}{if} (add\_to\_list) \textcolor{keywordflow}{then}
1224       kl = kl+1
1225       cs%DL(kl)%depth = dlist(i)
1226       cs%DL(kl)%area = area
1227       cs%DL(kl)%vol\_below = vol
1228 \textcolor{keywordflow}{    endif}
1229     dprev = dlist(i)
1230 \textcolor{keywordflow}{  enddo}
1231 
1232   \textcolor{keywordflow}{do} \textcolor{keywordflow}{while} (kl < list\_size)
1233     \textcolor{comment}{! I don't understand why this is needed... RWH}
1234     kl = kl+1
1235     cs%DL(kl)%vol\_below = cs%DL(kl-1)%vol\_below * 1.000001
1236     cs%DL(kl)%area = cs%DL(kl-1)%area
1237     cs%DL(kl)%depth = cs%DL(kl-1)%depth
1238 \textcolor{keywordflow}{  enddo}
1239 
1240   cs%DL(cs%list\_size+1)%vol\_below = cs%DL(cs%list\_size)%vol\_below * 1000.0
1241   cs%DL(cs%list\_size+1)%area = cs%DL(cs%list\_size)%area
1242   cs%DL(cs%list\_size+1)%depth = cs%DL(cs%list\_size)%depth
1243 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__sum__output_a53d3dcd50cba41760dd8713228785a8d}\label{namespacemom__sum__output_a53d3dcd50cba41760dd8713228785a8d}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!depth\+\_\+list\+\_\+setup@{depth\+\_\+list\+\_\+setup}}
\index{depth\+\_\+list\+\_\+setup@{depth\+\_\+list\+\_\+setup}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{depth\+\_\+list\+\_\+setup()}{depth\_list\_setup()}}
{\footnotesize\ttfamily subroutine mom\+\_\+sum\+\_\+output\+::depth\+\_\+list\+\_\+setup (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine sets up an ordered list of depths, along with the cross sectional areas at each depth and the volume of fluid deeper than each depth. This might be read from a previously created file or it might be created anew. (For now only new creation occurs. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & The control structure returned by a previous call to M\+O\+M\+\_\+sum\+\_\+output\+\_\+init. \\
\hline
\end{DoxyParams}


Definition at line 1093 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
1093   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{   !< The ocean's grid structure}
1094   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)} :: US\textcolor{comment}{  !< A dimensional unit scaling type}
1095   \textcolor{keywordtype}{type}(Sum\_output\_CS),   \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{  !< The control structure returned by a}
1096 \textcolor{comment}{                                           !! previous call to MOM\_sum\_output\_init.}
1097   \textcolor{comment}{! Local variables}
1098   \textcolor{keywordtype}{integer} :: k
1099 
1100   \textcolor{keywordflow}{if} (cs%read\_depth\_list) \textcolor{keywordflow}{then}
1101     \textcolor{keywordflow}{if} (file\_exists(cs%depth\_list\_file)) \textcolor{keywordflow}{then}
1102       \textcolor{keyword}{call }read\_depth\_list(g, us, cs, cs%depth\_list\_file)
1103     \textcolor{keywordflow}{else}
1104       \textcolor{keywordflow}{if} (is\_root\_pe()) \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"depth\_list\_setup: "}// &
1105         trim(cs%depth\_list\_file)//\textcolor{stringliteral}{" does not exist.  Creating a new file."})
1106       \textcolor{keyword}{call }create\_depth\_list(g, cs)
1107 
1108       \textcolor{keyword}{call }write\_depth\_list(g, us, cs, cs%depth\_list\_file, cs%list\_size+1)
1109 \textcolor{keywordflow}{    endif}
1110   \textcolor{keywordflow}{else}
1111     \textcolor{keyword}{call }create\_depth\_list(g, cs)
1112 \textcolor{keywordflow}{  endif}
1113 
1114   \textcolor{keywordflow}{do} k=1,g%ke
1115     cs%lH(k) = cs%list\_size
1116 \textcolor{keywordflow}{  enddo}
1117 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__sum__output_a928dd1160bb6c0033a417c618b9d01ef}\label{namespacemom__sum__output_a928dd1160bb6c0033a417c618b9d01ef}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!get\+\_\+depth\+\_\+list\+\_\+checksums@{get\+\_\+depth\+\_\+list\+\_\+checksums}}
\index{get\+\_\+depth\+\_\+list\+\_\+checksums@{get\+\_\+depth\+\_\+list\+\_\+checksums}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{get\+\_\+depth\+\_\+list\+\_\+checksums()}{get\_depth\_list\_checksums()}}
{\footnotesize\ttfamily subroutine mom\+\_\+sum\+\_\+output\+::get\+\_\+depth\+\_\+list\+\_\+checksums (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{character(len=16), intent(out)}]{depth\+\_\+chksum,  }\item[{character(len=16), intent(out)}]{area\+\_\+chksum }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Return the checksums required to verify D\+E\+P\+T\+H\+\_\+\+L\+I\+S\+T\+\_\+\+F\+I\+LE contents. 

This function computes checksums for the bathymetry (GbathyT) and masked area (mask2dT $\ast$ areaT) fields of the model grid G, which are used to compute the depth list. A difference in checksum indicates that a different method was used to compute the grid data, and that any results using the depth list, such as A\+PE, will not be reproducible.

Checksums are saved as hexadecimal strings, in order to avoid potential datatype issues with net\+C\+DF attributes.


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & Ocean grid structure\\
\hline
\mbox{\tt out}  & {\em depth\+\_\+chksum} & Depth checksum hexstring\\
\hline
\mbox{\tt out}  & {\em area\+\_\+chksum} & Area checksum hexstring \\
\hline
\end{DoxyParams}


Definition at line 1499 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
1499   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{          !< Ocean grid structure}
1500   \textcolor{keywordtype}{character(len=16)}, \textcolor{keywordtype}{intent(out)} :: depth\_chksum\textcolor{comment}{  !< Depth checksum hexstring}
1501   \textcolor{keywordtype}{character(len=16)}, \textcolor{keywordtype}{intent(out)} :: area\_chksum\textcolor{comment}{   !< Area checksum hexstring}
1502 
1503   \textcolor{keywordtype}{integer} :: i, j
1504   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{allocatable} :: field(:,:)
1505 
1506   \textcolor{keyword}{allocate}(field(g%isc:g%iec, g%jsc:g%jec))
1507 
1508   \textcolor{comment}{! Depth checksum}
1509   \textcolor{keywordflow}{do} j=g%jsc,g%jec ; \textcolor{keywordflow}{do} i=g%isc,g%iec
1510     field(i,j) = g%bathyT(i,j)
1511 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
1512   \textcolor{keyword}{write}(depth\_chksum, \textcolor{stringliteral}{'(Z16)'}) mpp\_chksum(field(:,:))
1513 
1514   \textcolor{comment}{! Area checksum}
1515   \textcolor{keywordflow}{do} j=g%jsc,g%jec ; \textcolor{keywordflow}{do} i=g%isc,g%iec
1516     field(i,j) = g%mask2dT(i,j) * g%US%L\_to\_m**2*g%areaT(i,j)
1517 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
1518   \textcolor{keyword}{write}(area\_chksum, \textcolor{stringliteral}{'(Z16)'}) mpp\_chksum(field(:,:))
1519 
1520   \textcolor{keyword}{deallocate}(field)
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__sum__output_ae54994f461b38198510274dadbce8fb5}\label{namespacemom__sum__output_ae54994f461b38198510274dadbce8fb5}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!mom\+\_\+sum\+\_\+output\+\_\+end@{mom\+\_\+sum\+\_\+output\+\_\+end}}
\index{mom\+\_\+sum\+\_\+output\+\_\+end@{mom\+\_\+sum\+\_\+output\+\_\+end}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{mom\+\_\+sum\+\_\+output\+\_\+end()}{mom\_sum\_output\_end()}}
{\footnotesize\ttfamily subroutine mom\+\_\+sum\+\_\+output\+::mom\+\_\+sum\+\_\+output\+\_\+end (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



M\+O\+M\+\_\+sum\+\_\+output\+\_\+end deallocates memory used by the M\+O\+M\+\_\+sum\+\_\+output module. 


\begin{DoxyParams}{Parameters}
{\em cs} & The control structure returned by a previous call to M\+O\+M\+\_\+sum\+\_\+output\+\_\+init. \\
\hline
\end{DoxyParams}


Definition at line 284 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
284   \textcolor{keywordtype}{type}(Sum\_output\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{  !< The control structure returned by a}
285 \textcolor{comment}{                                      !! previous call to MOM\_sum\_output\_init.}
286   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}
287     \textcolor{keywordflow}{if} (cs%do\_APE\_calc) \textcolor{keywordflow}{then}
288       \textcolor{keyword}{deallocate}(cs%lH, cs%DL)
289 \textcolor{keywordflow}{    endif}
290 
291     \textcolor{keyword}{deallocate}(cs)
292 \textcolor{keywordflow}{  endif}
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__sum__output_a05003e74cbb39fbdf760d23617390445}\label{namespacemom__sum__output_a05003e74cbb39fbdf760d23617390445}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!mom\+\_\+sum\+\_\+output\+\_\+init@{mom\+\_\+sum\+\_\+output\+\_\+init}}
\index{mom\+\_\+sum\+\_\+output\+\_\+init@{mom\+\_\+sum\+\_\+output\+\_\+init}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{mom\+\_\+sum\+\_\+output\+\_\+init()}{mom\_sum\_output\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+sum\+\_\+output\+::mom\+\_\+sum\+\_\+output\+\_\+init (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{character(len=$\ast$), intent(in)}]{directory,  }\item[{integer, intent(inout), target}]{ntrnc,  }\item[{type(time\+\_\+type), intent(in)}]{Input\+\_\+start\+\_\+time,  }\item[{type(\mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



M\+O\+M\+\_\+sum\+\_\+output\+\_\+init initializes the parameters and settings for the M\+O\+M\+\_\+sum\+\_\+output module. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters.\\
\hline
\mbox{\tt in}  & {\em directory} & The directory where the energy file goes.\\
\hline
\mbox{\tt in,out}  & {\em ntrnc} & The integer that stores the number of times the velocity has been truncated since the last call to write\+\_\+energy.\\
\hline
\mbox{\tt in}  & {\em input\+\_\+start\+\_\+time} & The start time of the simulation.\\
\hline
 & {\em cs} & A pointer that is set to point to the control structure for this module. \\
\hline
\end{DoxyParams}


Definition at line 136 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
136   \textcolor{keywordtype}{type}(ocean\_grid\_type),  \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{          !< The ocean's grid structure.}
137   \textcolor{keywordtype}{type}(unit\_scale\_type),  \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{         !< A dimensional unit scaling type}
138   \textcolor{keywordtype}{type}(param\_file\_type),  \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for run-time}
139 \textcolor{comment}{                                                      !! parameters.}
140   \textcolor{keywordtype}{character(len=*)},       \textcolor{keywordtype}{intent(in)}    :: directory\textcolor{comment}{  !< The directory where the energy file goes.}
141   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{target},        \textcolor{keywordtype}{intent(inout)} :: ntrnc\textcolor{comment}{      !< The integer that stores the number of times}
142 \textcolor{comment}{                                                      !! the velocity has been truncated since the}
143 \textcolor{comment}{                                                      !! last call to write\_energy.}
144   \textcolor{keywordtype}{type}(time\_type),        \textcolor{keywordtype}{intent(in)}    :: Input\_start\_time\textcolor{comment}{ !< The start time of the simulation.}
145   \textcolor{keywordtype}{type}(Sum\_output\_CS),    \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{         !< A pointer that is set to point to the}
146 \textcolor{comment}{                                                      !! control structure for this module.}
147   \textcolor{comment}{! Local variables}
148   \textcolor{keywordtype}{real} :: Time\_unit \textcolor{comment}{! The time unit in seconds for ENERGYSAVEDAYS.}
149   \textcolor{keywordtype}{real} :: Rho\_0     \textcolor{comment}{! A reference density [kg m-3]}
150   \textcolor{keywordtype}{real} :: maxvel    \textcolor{comment}{! The maximum permitted velocity [m s-1]}
151 \textcolor{comment}{! This include declares and sets the variable "version".}
152 \textcolor{preprocessor}{#include "version\_variable.h"}
153 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_sum\_output"} \textcolor{comment}{! This module's name.}
154   \textcolor{keywordtype}{character(len=200)} :: energyfile  \textcolor{comment}{! The name of the energy file.}
155   \textcolor{keywordtype}{character(len=32)} :: filename\_appendix = \textcolor{stringliteral}{''} \textcolor{comment}{!fms appendix to filename for ensemble runs}
156 
157   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}
158     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"MOM\_sum\_output\_init called with associated control structure."})
159     \textcolor{keywordflow}{return}
160 \textcolor{keywordflow}{  endif}
161   \textcolor{keyword}{allocate}(cs)
162 
163   \textcolor{comment}{! Read all relevant parameters and write them to the model log.}
164   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""})
165   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CALCULATE\_APE"}, cs%do\_APE\_calc, &
166                  \textcolor{stringliteral}{"If true, calculate the available potential energy of "}//&
167                  \textcolor{stringliteral}{"the interfaces.  Setting this to false reduces the "}//&
168                  \textcolor{stringliteral}{"memory footprint of high-PE-count models dramatically."}, &
169                  default=.true.)
170   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"WRITE\_STOCKS"}, cs%write\_stocks, &
171                  \textcolor{stringliteral}{"If true, write the integrated tracer amounts to stdout "}//&
172                  \textcolor{stringliteral}{"when the energy files are written."}, default=.true.)
173   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENABLE\_THERMODYNAMICS"}, cs%use\_temperature, &
174                  \textcolor{stringliteral}{"If true, Temperature and salinity are used as state "}//&
175                  \textcolor{stringliteral}{"variables."}, default=.true.)
176   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DT"}, cs%dt\_in\_T, &
177                  \textcolor{stringliteral}{"The (baroclinic) dynamics time step."}, &
178                  units=\textcolor{stringliteral}{"s"}, scale=us%s\_to\_T, fail\_if\_missing=.true.)
179   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAXTRUNC"}, cs%maxtrunc, &
180                  \textcolor{stringliteral}{"The run will be stopped, and the day set to a very "}//&
181                  \textcolor{stringliteral}{"large value if the velocity is truncated more than "}//&
182                  \textcolor{stringliteral}{"MAXTRUNC times between energy saves.  Set MAXTRUNC to 0 "}//&
183                  \textcolor{stringliteral}{"to stop if there is any truncation of velocities."}, &
184                  units=\textcolor{stringliteral}{"truncations save\_interval-1"}, default=0)
185 
186   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAX\_ENERGY"}, cs%max\_Energy, &
187                  \textcolor{stringliteral}{"The maximum permitted average energy per unit mass; the "}//&
188                  \textcolor{stringliteral}{"model will be stopped if there is more energy than "}//&
189                  \textcolor{stringliteral}{"this.  If zero or negative, this is set to 10*MAXVEL^2."}, &
190                  units=\textcolor{stringliteral}{"m2 s-2"}, default=0.0)
191   \textcolor{keywordflow}{if} (cs%max\_Energy <= 0.0) \textcolor{keywordflow}{then}
192     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAXVEL"}, maxvel, &
193                  \textcolor{stringliteral}{"The maximum velocity allowed before the velocity "}//&
194                  \textcolor{stringliteral}{"components are truncated."}, units=\textcolor{stringliteral}{"m s-1"}, default=3.0e8)
195     cs%max\_Energy = 10.0 * maxvel**2
196     \textcolor{keyword}{call }log\_param(param\_file, mdl, \textcolor{stringliteral}{"MAX\_ENERGY as used"}, cs%max\_Energy)
197 \textcolor{keywordflow}{  endif}
198 
199   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGYFILE"}, energyfile, &
200                  \textcolor{stringliteral}{"The file to use to write the energies and globally "}//&
201                  \textcolor{stringliteral}{"summed diagnostics."}, default=\textcolor{stringliteral}{"ocean.stats"})
202 
203   \textcolor{comment}{!query fms\_io if there is a filename\_appendix (for ensemble runs)}
204   \textcolor{keyword}{call }get\_filename\_appendix(filename\_appendix)
205   \textcolor{keywordflow}{if} (len\_trim(filename\_appendix) > 0) \textcolor{keywordflow}{then}
206      energyfile = trim(energyfile) //\textcolor{stringliteral}{'.'}//trim(filename\_appendix)
207 \textcolor{keywordflow}{  endif}
208 
209   cs%energyfile = trim(slasher(directory))//trim(energyfile)
210   \textcolor{keyword}{call }log\_param(param\_file, mdl, \textcolor{stringliteral}{"output\_path/ENERGYFILE"}, cs%energyfile)
211 \textcolor{preprocessor}{#ifdef STATSLABEL}
212 \textcolor{preprocessor}{}  cs%energyfile = trim(cs%energyfile)//\textcolor{stringliteral}{"."}//trim(adjustl(statslabel))
213 \textcolor{preprocessor}{#endif}
214 \textcolor{preprocessor}{}
215   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DATE\_STAMPED\_STDOUT"}, cs%date\_stamped\_output, &
216                  \textcolor{stringliteral}{"If true, use dates (not times) in messages to stdout"}, &
217                  default=.true.)
218   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TIMEUNIT"}, cs%Timeunit, &
219                  \textcolor{stringliteral}{"The time unit in seconds a number of input fields"}, &
220                  units=\textcolor{stringliteral}{"s"}, default=86400.0)
221   \textcolor{keywordflow}{if} (cs%Timeunit < 0.0) cs%Timeunit = 86400.0
222 
223 
224 
225   \textcolor{keywordflow}{if} (cs%do\_APE\_calc) \textcolor{keywordflow}{then}
226     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"READ\_DEPTH\_LIST"}, cs%read\_depth\_list, &
227                    \textcolor{stringliteral}{"Read the depth list from a file if it exists or "}//&
228                    \textcolor{stringliteral}{"create that file otherwise."}, default=.false.)
229     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEPTH\_LIST\_MIN\_INC"}, cs%D\_list\_min\_inc, &
230                    \textcolor{stringliteral}{"The minimum increment between the depths of the "}//&
231                    \textcolor{stringliteral}{"entries in the depth-list file."}, &
232                    units=\textcolor{stringliteral}{"m"}, default=1.0e-10, scale=us%m\_to\_Z)
233     \textcolor{keywordflow}{if} (cs%read\_depth\_list) \textcolor{keywordflow}{then}
234       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEPTH\_LIST\_FILE"}, cs%depth\_list\_file, &
235                    \textcolor{stringliteral}{"The name of the depth list file."}, default=\textcolor{stringliteral}{"Depth\_list.nc"})
236       \textcolor{keywordflow}{if} (scan(cs%depth\_list\_file,\textcolor{stringliteral}{'/'}) == 0) &
237         cs%depth\_list\_file = trim(slasher(directory))//trim(cs%depth\_list\_file)
238 
239       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"REQUIRE\_DEPTH\_LIST\_CHECKSUMS"}, &
240                      cs%require\_depth\_list\_chksum, &
241                  \textcolor{stringliteral}{"Require that matching checksums be in Depth\_list.nc "}//&
242                  \textcolor{stringliteral}{"when reading the file."}, default=.true.)
243       \textcolor{keywordflow}{if} (.not. cs%require\_depth\_list\_chksum) &
244         \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"UPDATE\_DEPTH\_LIST\_CHECKSUMS"}, &
245                      cs%update\_depth\_list\_chksum, &
246                  \textcolor{stringliteral}{"Automatically update the Depth\_list.nc file if the "}//&
247                  \textcolor{stringliteral}{"checksums are missing or do not match current values."}, &
248                  default=.false.)
249 \textcolor{keywordflow}{    endif}
250 
251     \textcolor{keyword}{allocate}(cs%lH(g%ke))
252     \textcolor{keyword}{call }depth\_list\_setup(g, us, cs)
253   \textcolor{keywordflow}{else}
254     cs%list\_size = 0
255 \textcolor{keywordflow}{  endif}
256 
257   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TIMEUNIT"}, time\_unit, &
258                  \textcolor{stringliteral}{"The time unit for ENERGYSAVEDAYS."}, &
259                  units=\textcolor{stringliteral}{"s"}, default=86400.0)
260   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGYSAVEDAYS"},cs%energysavedays, &
261                  \textcolor{stringliteral}{"The interval in units of TIMEUNIT between saves of the "}//&
262                  \textcolor{stringliteral}{"energies of the run and other globally summed diagnostics."},&
263                  default=set\_time(0,days=1), timeunit=time\_unit)
264   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGYSAVEDAYS\_GEOMETRIC"},cs%energysavedays\_geometric, &
265                  \textcolor{stringliteral}{"The starting interval in units of TIMEUNIT for the first call "}//&
266                  \textcolor{stringliteral}{"to save the energies of the run and other globally summed diagnostics. "}//&
267                  \textcolor{stringliteral}{"The interval increases by a factor of 2. after each call to write\_energy."},&
268                  default=set\_time(seconds=0), timeunit=time\_unit)
269 
270   \textcolor{keywordflow}{if} ((time\_type\_to\_real(cs%energysavedays\_geometric) > 0.) .and. &
271      (cs%energysavedays\_geometric < cs%energysavedays)) \textcolor{keywordflow}{then}
272          cs%energysave\_geometric = .true.
273   \textcolor{keywordflow}{else}
274          cs%energysave\_geometric = .false.
275 \textcolor{keywordflow}{  endif}
276 
277   cs%Start\_time = input\_start\_time
278   cs%ntrunc => ntrnc
279 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__sum__output_a4f45b1c7dbff4fe064e102ccb2967daf}\label{namespacemom__sum__output_a4f45b1c7dbff4fe064e102ccb2967daf}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!read\+\_\+depth\+\_\+list@{read\+\_\+depth\+\_\+list}}
\index{read\+\_\+depth\+\_\+list@{read\+\_\+depth\+\_\+list}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{read\+\_\+depth\+\_\+list()}{read\_depth\_list()}}
{\footnotesize\ttfamily subroutine mom\+\_\+sum\+\_\+output\+::read\+\_\+depth\+\_\+list (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}), pointer}]{CS,  }\item[{character(len=$\ast$), intent(in)}]{filename }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine reads in the depth list to the specified file and allocates and sets up CSDL and CSlist\+\_\+size . 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & The control structure returned by a previous call to M\+O\+M\+\_\+sum\+\_\+output\+\_\+init.\\
\hline
\mbox{\tt in}  & {\em filename} & The path to the depth list file to read. \\
\hline
\end{DoxyParams}


Definition at line 1343 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
1343   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{   !< The ocean's grid structure}
1344   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)} :: US\textcolor{comment}{  !< A dimensional unit scaling type}
1345   \textcolor{keywordtype}{type}(Sum\_output\_CS),   \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{  !< The control structure returned by a}
1346 \textcolor{comment}{                                           !! previous call to MOM\_sum\_output\_init.}
1347   \textcolor{keywordtype}{character(len=*)},      \textcolor{keywordtype}{intent(in)} :: filename\textcolor{comment}{ !< The path to the depth list file to read.}
1348   \textcolor{comment}{! Local variables}
1349   \textcolor{keywordtype}{character(len=32)} :: mdl
1350   \textcolor{keywordtype}{character(len=240)} :: var\_name, var\_msg
1351   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{allocatable} :: tmp(:)
1352   \textcolor{keywordtype}{integer} :: ncid, status, varid, list\_size, k
1353   \textcolor{keywordtype}{integer} :: ndim, len, var\_dim\_ids(NF90\_MAX\_VAR\_DIMS)
1354   \textcolor{keywordtype}{character(len=16)} :: depth\_file\_chksum, depth\_grid\_chksum
1355   \textcolor{keywordtype}{character(len=16)} :: area\_file\_chksum, area\_grid\_chksum
1356   \textcolor{keywordtype}{integer} :: depth\_attr\_status, area\_attr\_status
1357 
1358   mdl = \textcolor{stringliteral}{"MOM\_sum\_output read\_depth\_list:"}
1359 
1360   status = nf90\_open(filename, nf90\_nowrite, ncid)
1361   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
1362     \textcolor{keyword}{call }mom\_error(fatal,mdl//\textcolor{stringliteral}{" Difficulties opening "}//trim(filename)// &
1363         \textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
1364 \textcolor{keywordflow}{  endif}
1365 
1366   \textcolor{comment}{! Check bathymetric consistency}
1367   depth\_attr\_status = nf90\_get\_att(ncid, nf90\_global, depth\_chksum\_attr, &
1368                                    depth\_file\_chksum)
1369   area\_attr\_status = nf90\_get\_att(ncid, nf90\_global, area\_chksum\_attr, &
1370                                   area\_file\_chksum)
1371 
1372   \textcolor{keywordflow}{if} (any([depth\_attr\_status, area\_attr\_status] == nf90\_enotatt)) \textcolor{keywordflow}{then}
1373     var\_msg = trim(cs%depth\_list\_file) // \textcolor{stringliteral}{" checksums are missing;"}
1374     \textcolor{keywordflow}{if} (cs%require\_depth\_list\_chksum) \textcolor{keywordflow}{then}
1375       \textcolor{keyword}{call }mom\_error(fatal, trim(var\_msg) // \textcolor{stringliteral}{" aborting."})
1376     \textcolor{keywordflow}{elseif} (cs%update\_depth\_list\_chksum) \textcolor{keywordflow}{then}
1377       \textcolor{keyword}{call }mom\_error(warning, trim(var\_msg) // \textcolor{stringliteral}{" updating file."})
1378       \textcolor{keyword}{call }create\_depth\_list(g, cs)
1379       \textcolor{keyword}{call }write\_depth\_list(g, us, cs, cs%depth\_list\_file, cs%list\_size+1)
1380       \textcolor{keywordflow}{return}
1381     \textcolor{keywordflow}{else}
1382       \textcolor{keyword}{call }mom\_error(warning, &
1383         trim(var\_msg) // \textcolor{stringliteral}{" some diagnostics may not be reproducible."})
1384 \textcolor{keywordflow}{    endif}
1385   \textcolor{keywordflow}{else}
1386     \textcolor{comment}{! Validate netCDF call}
1387     \textcolor{keywordflow}{if} (depth\_attr\_status /= nf90\_noerr) \textcolor{keywordflow}{then}
1388       var\_msg = mdl // \textcolor{stringliteral}{"Failed to read "} // trim(filename) // \textcolor{stringliteral}{":"} &
1389                 // depth\_chksum\_attr
1390       \textcolor{keyword}{call }mom\_error(fatal, &
1391         trim(var\_msg) // \textcolor{stringliteral}{" - "} // nf90\_strerror(depth\_attr\_status))
1392 \textcolor{keywordflow}{    endif}
1393 
1394     \textcolor{keywordflow}{if} (area\_attr\_status /= nf90\_noerr) \textcolor{keywordflow}{then}
1395       var\_msg = mdl // \textcolor{stringliteral}{"Failed to read "} // trim(filename) // \textcolor{stringliteral}{":"} &
1396                 // area\_chksum\_attr
1397       \textcolor{keyword}{call }mom\_error(fatal, &
1398         trim(var\_msg) // \textcolor{stringliteral}{" - "} // nf90\_strerror(area\_attr\_status))
1399 \textcolor{keywordflow}{    endif}
1400 
1401     \textcolor{keyword}{call }get\_depth\_list\_checksums(g, depth\_grid\_chksum, area\_grid\_chksum)
1402 
1403     \textcolor{keywordflow}{if} (depth\_grid\_chksum /= depth\_file\_chksum &
1404             .or. area\_grid\_chksum /= area\_file\_chksum) \textcolor{keywordflow}{then}
1405       var\_msg = trim(cs%depth\_list\_file) // \textcolor{stringliteral}{" checksums do not match;"}
1406       \textcolor{keywordflow}{if} (cs%require\_depth\_list\_chksum) \textcolor{keywordflow}{then}
1407         \textcolor{keyword}{call }mom\_error(fatal, trim(var\_msg) // \textcolor{stringliteral}{" aborting."})
1408       \textcolor{keywordflow}{elseif} (cs%update\_depth\_list\_chksum) \textcolor{keywordflow}{then}
1409         \textcolor{keyword}{call }mom\_error(warning, trim(var\_msg) // \textcolor{stringliteral}{" updating file."})
1410         \textcolor{keyword}{call }create\_depth\_list(g, cs)
1411         \textcolor{keyword}{call }write\_depth\_list(g, us, cs, cs%depth\_list\_file, cs%list\_size+1)
1412         \textcolor{keywordflow}{return}
1413       \textcolor{keywordflow}{else}
1414         \textcolor{keyword}{call }mom\_error(warning, &
1415           trim(var\_msg) // \textcolor{stringliteral}{" some diagnostics may not be reproducible."})
1416 \textcolor{keywordflow}{      endif}
1417 \textcolor{keywordflow}{    endif}
1418 \textcolor{keywordflow}{  endif}
1419 
1420   var\_name = \textcolor{stringliteral}{"depth"}
1421   var\_msg = trim(var\_name)//\textcolor{stringliteral}{" in "}//trim(filename)//\textcolor{stringliteral}{" - "}
1422   status = nf90\_inq\_varid(ncid, var\_name, varid)
1423   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(fatal,mdl// &
1424         \textcolor{stringliteral}{" Difficulties finding variable "}//trim(var\_msg)//&
1425         trim(nf90\_strerror(status)))
1426 
1427   status = nf90\_inquire\_variable(ncid, varid, ndims=ndim, dimids=var\_dim\_ids)
1428   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
1429     \textcolor{keyword}{call }mom\_error(fatal,mdl//\textcolor{stringliteral}{" cannot inquire about "}//trim(var\_msg)//&
1430         trim(nf90\_strerror(status)))
1431   \textcolor{keywordflow}{elseif} (ndim > 1) \textcolor{keywordflow}{then}
1432     \textcolor{keyword}{call }mom\_error(fatal,mdl//\textcolor{stringliteral}{" "}//trim(var\_msg)//&
1433          \textcolor{stringliteral}{" has too many or too few dimensions."})
1434 \textcolor{keywordflow}{  endif}
1435 
1436   \textcolor{comment}{! Get the length of the list.}
1437   status = nf90\_inquire\_dimension(ncid, var\_dim\_ids(1), len=list\_size)
1438   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(fatal,mdl// &
1439         \textcolor{stringliteral}{" cannot inquire about dimension(1) of "}//trim(var\_msg)//&
1440         trim(nf90\_strerror(status)))
1441 
1442   cs%list\_size = list\_size-1
1443   \textcolor{keyword}{allocate}(cs%DL(list\_size))
1444   \textcolor{keyword}{allocate}(tmp(list\_size))
1445 
1446   status = nf90\_get\_var(ncid, varid, tmp)
1447   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(fatal,mdl// &
1448         \textcolor{stringliteral}{" Difficulties reading variable "}//trim(var\_msg)//&
1449         trim(nf90\_strerror(status)))
1450 
1451   \textcolor{keywordflow}{do} k=1,list\_size ; cs%DL(k)%depth = us%m\_to\_Z*tmp(k) ;\textcolor{keywordflow}{ enddo}
1452 
1453   var\_name = \textcolor{stringliteral}{"area"}
1454   var\_msg = trim(var\_name)//\textcolor{stringliteral}{" in "}//trim(filename)//\textcolor{stringliteral}{" - "}
1455   status = nf90\_inq\_varid(ncid, var\_name, varid)
1456   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(fatal,mdl// &
1457         \textcolor{stringliteral}{" Difficulties finding variable "}//trim(var\_msg)//&
1458         trim(nf90\_strerror(status)))
1459   status = nf90\_get\_var(ncid, varid, tmp)
1460   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(fatal,mdl// &
1461         \textcolor{stringliteral}{" Difficulties reading variable "}//trim(var\_msg)//&
1462         trim(nf90\_strerror(status)))
1463 
1464   \textcolor{keywordflow}{do} k=1,list\_size ; cs%DL(k)%area = us%m\_to\_L**2*tmp(k) ;\textcolor{keywordflow}{ enddo}
1465 
1466   var\_name = \textcolor{stringliteral}{"vol\_below"}
1467   var\_msg = trim(var\_name)//\textcolor{stringliteral}{" in "}//trim(filename)
1468   status = nf90\_inq\_varid(ncid, var\_name, varid)
1469   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(fatal,mdl// &
1470         \textcolor{stringliteral}{" Difficulties finding variable "}//trim(var\_msg)//&
1471         trim(nf90\_strerror(status)))
1472   status = nf90\_get\_var(ncid, varid, tmp)
1473   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(fatal,mdl// &
1474         \textcolor{stringliteral}{" Difficulties reading variable "}//trim(var\_msg)//&
1475         trim(nf90\_strerror(status)))
1476 
1477   \textcolor{keywordflow}{do} k=1,list\_size ; cs%DL(k)%vol\_below = us%m\_to\_Z*us%m\_to\_L**2*tmp(k) ;\textcolor{keywordflow}{ enddo}
1478 
1479   status = nf90\_close(ncid)
1480   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, mdl// &
1481     \textcolor{stringliteral}{" Difficulties closing "}//trim(filename)//\textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
1482 
1483   \textcolor{keyword}{deallocate}(tmp)
1484 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__sum__output_a955e777980a668bfc611c78de5de1895}\label{namespacemom__sum__output_a955e777980a668bfc611c78de5de1895}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!write\+\_\+depth\+\_\+list@{write\+\_\+depth\+\_\+list}}
\index{write\+\_\+depth\+\_\+list@{write\+\_\+depth\+\_\+list}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{write\+\_\+depth\+\_\+list()}{write\_depth\_list()}}
{\footnotesize\ttfamily subroutine mom\+\_\+sum\+\_\+output\+::write\+\_\+depth\+\_\+list (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}), pointer}]{CS,  }\item[{character(len=$\ast$), intent(in)}]{filename,  }\item[{integer, intent(in)}]{list\+\_\+size }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine writes out the depth list to the specified file. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & The control structure returned by a previous call to M\+O\+M\+\_\+sum\+\_\+output\+\_\+init.\\
\hline
\mbox{\tt in}  & {\em filename} & The path to the depth list file to write.\\
\hline
\mbox{\tt in}  & {\em list\+\_\+size} & The size of the depth list. \\
\hline
\end{DoxyParams}


Definition at line 1248 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
1248   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{   !< The ocean's grid structure.}
1249   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)} :: US\textcolor{comment}{  !< A dimensional unit scaling type}
1250   \textcolor{keywordtype}{type}(Sum\_output\_CS),   \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{  !< The control structure returned by a}
1251 \textcolor{comment}{                                           !! previous call to MOM\_sum\_output\_init.}
1252   \textcolor{keywordtype}{character(len=*)},      \textcolor{keywordtype}{intent(in)} :: filename\textcolor{comment}{ !< The path to the depth list file to write.}
1253   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)} :: list\_size\textcolor{comment}{ !< The size of the depth list.}
1254   \textcolor{comment}{! Local variables}
1255   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{allocatable} :: tmp(:)
1256   \textcolor{keywordtype}{integer} :: ncid, dimid(1), Did, Aid, Vid, status, k
1257   \textcolor{keywordtype}{character(len=16)} :: depth\_chksum, area\_chksum
1258 
1259   \textcolor{comment}{! All ranks are required to compute the global checksum}
1260   \textcolor{keyword}{call }get\_depth\_list\_checksums(g, depth\_chksum, area\_chksum)
1261 
1262   \textcolor{keywordflow}{if} (.not.is\_root\_pe()) \textcolor{keywordflow}{return}
1263 
1264   \textcolor{keyword}{allocate}(tmp(list\_size)) ; tmp(:) = 0.0
1265 
1266   status = nf90\_create(filename, 0, ncid)
1267   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
1268     \textcolor{keyword}{call }mom\_error(warning, filename//trim(nf90\_strerror(status)))
1269     \textcolor{keywordflow}{return}
1270 \textcolor{keywordflow}{  endif}
1271 
1272   status = nf90\_def\_dim(ncid, \textcolor{stringliteral}{"list"}, list\_size, dimid(1))
1273   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1274       filename//trim(nf90\_strerror(status)))
1275 
1276   status = nf90\_def\_var(ncid, \textcolor{stringliteral}{"depth"}, nf90\_double, dimid, did)
1277   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1278       filename//\textcolor{stringliteral}{" depth "}//trim(nf90\_strerror(status)))
1279   status = nf90\_put\_att(ncid, did, \textcolor{stringliteral}{"long\_name"}, \textcolor{stringliteral}{"Sorted depth"})
1280   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1281       filename//\textcolor{stringliteral}{" depth "}//trim(nf90\_strerror(status)))
1282   status = nf90\_put\_att(ncid, did, \textcolor{stringliteral}{"units"}, \textcolor{stringliteral}{"m"})
1283   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1284       filename//\textcolor{stringliteral}{" depth "}//trim(nf90\_strerror(status)))
1285 
1286   status = nf90\_def\_var(ncid, \textcolor{stringliteral}{"area"}, nf90\_double, dimid, aid)
1287   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1288       filename//\textcolor{stringliteral}{" area "}//trim(nf90\_strerror(status)))
1289   status = nf90\_put\_att(ncid, aid, \textcolor{stringliteral}{"long\_name"}, \textcolor{stringliteral}{"Open area at depth"})
1290   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1291       filename//\textcolor{stringliteral}{" area "}//trim(nf90\_strerror(status)))
1292   status = nf90\_put\_att(ncid, aid, \textcolor{stringliteral}{"units"}, \textcolor{stringliteral}{"m2"})
1293   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1294       filename//\textcolor{stringliteral}{" area "}//trim(nf90\_strerror(status)))
1295 
1296   status = nf90\_def\_var(ncid, \textcolor{stringliteral}{"vol\_below"}, nf90\_double, dimid, vid)
1297   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1298       filename//\textcolor{stringliteral}{" vol\_below "}//trim(nf90\_strerror(status)))
1299   status = nf90\_put\_att(ncid, vid, \textcolor{stringliteral}{"long\_name"}, \textcolor{stringliteral}{"Open volume below depth"})
1300    \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1301       filename//\textcolor{stringliteral}{" vol\_below "}//trim(nf90\_strerror(status)))
1302   status = nf90\_put\_att(ncid, vid, \textcolor{stringliteral}{"units"}, \textcolor{stringliteral}{"m3"})
1303   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1304       filename//\textcolor{stringliteral}{" vol\_below "}//trim(nf90\_strerror(status)))
1305 
1306   \textcolor{comment}{! Dependency checksums}
1307   status = nf90\_put\_att(ncid, nf90\_global, depth\_chksum\_attr, depth\_chksum)
1308   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1309       filename//\textcolor{stringliteral}{" "}//depth\_chksum\_attr//\textcolor{stringliteral}{" "}//trim(nf90\_strerror(status)))
1310 
1311   status = nf90\_put\_att(ncid, nf90\_global, area\_chksum\_attr, area\_chksum)
1312   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1313       filename//\textcolor{stringliteral}{" "}//area\_chksum\_attr//\textcolor{stringliteral}{" "}//trim(nf90\_strerror(status)))
1314 
1315   status = nf90\_enddef(ncid)
1316   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1317       filename//trim(nf90\_strerror(status)))
1318 
1319   \textcolor{keywordflow}{do} k=1,list\_size ; tmp(k) = us%Z\_to\_m*cs%DL(k)%depth ;\textcolor{keywordflow}{ enddo}
1320   status = nf90\_put\_var(ncid, did, tmp)
1321   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1322       filename//\textcolor{stringliteral}{" depth "}//trim(nf90\_strerror(status)))
1323 
1324   \textcolor{keywordflow}{do} k=1,list\_size ; tmp(k) = us%L\_to\_m**2*cs%DL(k)%area ;\textcolor{keywordflow}{ enddo}
1325   status = nf90\_put\_var(ncid, aid, tmp)
1326   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1327       filename//\textcolor{stringliteral}{" area "}//trim(nf90\_strerror(status)))
1328 
1329   \textcolor{keywordflow}{do} k=1,list\_size ; tmp(k) = us%Z\_to\_m*us%L\_to\_m**2*cs%DL(k)%vol\_below ;\textcolor{keywordflow}{ enddo}
1330   status = nf90\_put\_var(ncid, vid, tmp)
1331   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1332       filename//\textcolor{stringliteral}{" vol\_below "}//trim(nf90\_strerror(status)))
1333 
1334   status = nf90\_close(ncid)
1335   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, &
1336       filename//trim(nf90\_strerror(status)))
1337 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__sum__output_ad3cc692dd515100ec8cf92d740c91e72}\label{namespacemom__sum__output_ad3cc692dd515100ec8cf92d740c91e72}} 
\index{mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}!write\+\_\+energy@{write\+\_\+energy}}
\index{write\+\_\+energy@{write\+\_\+energy}!mom\+\_\+sum\+\_\+output@{mom\+\_\+sum\+\_\+output}}
\subsubsection{\texorpdfstring{write\+\_\+energy()}{write\_energy()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+sum\+\_\+output\+::write\+\_\+energy (\begin{DoxyParamCaption}\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{u,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(g)), intent(in)}]{v,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{type(time\+\_\+type), intent(in)}]{day,  }\item[{integer, intent(in)}]{n,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__sum__output_1_1sum__output__cs}{sum\+\_\+output\+\_\+cs}}), pointer}]{CS,  }\item[{type(tracer\+\_\+flow\+\_\+control\+\_\+cs), optional, pointer}]{tracer\+\_\+\+C\+Sp,  }\item[{type(ocean\+\_\+obc\+\_\+type), optional, pointer}]{O\+BC,  }\item[{type(time\+\_\+type), intent(in), optional}]{dt\+\_\+forcing }\end{DoxyParamCaption})}



This subroutine calculates and writes the total model energy, the energy and mass of each layer, and other globally integrated physical quantities. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em u} & The zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em v} & The meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em tv} & A structure pointing to various thermodynamic variables.\\
\hline
\mbox{\tt in}  & {\em day} & The current model time.\\
\hline
\mbox{\tt in}  & {\em n} & The time step number of the current execution.\\
\hline
 & {\em cs} & The control structure returned by a previous call to M\+O\+M\+\_\+sum\+\_\+output\+\_\+init.\\
\hline
 & {\em tracer\+\_\+csp} & tracer control structure.\\
\hline
 & {\em obc} & Open boundaries control structure.\\
\hline
\mbox{\tt in}  & {\em dt\+\_\+forcing} & The forcing time step \\
\hline
\end{DoxyParams}


Definition at line 298 of file M\+O\+M\+\_\+sum\+\_\+output.\+F90.


\begin{DoxyCode}
298   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{   !< The ocean's grid structure.}
299   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{  !< The ocean's vertical grid structure.}
300   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{  !< A dimensional unit scaling type}
301   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, &
302                            \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{   !< The zonal velocity [L T-1 ~> m s-1].}
303   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, &
304                            \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{   !< The meridional velocity [L T-1 ~> m s-1].}
305   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  &
306                            \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{   !< Layer thicknesses [H ~> m or kg m-2].}
307   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),   \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{  !< A structure pointing to various}
308 \textcolor{comment}{                                                !! thermodynamic variables.}
309   \textcolor{keywordtype}{type}(time\_type),         \textcolor{keywordtype}{intent(in)}    :: day\textcolor{comment}{ !< The current model time.}
310   \textcolor{keywordtype}{integer},                 \textcolor{keywordtype}{intent(in)}    :: n\textcolor{comment}{   !< The time step number of the}
311 \textcolor{comment}{                                                !! current execution.}
312   \textcolor{keywordtype}{type}(Sum\_output\_CS),     \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{  !< The control structure returned by a}
313 \textcolor{comment}{                                                !! previous call to MOM\_sum\_output\_init.}
314   \textcolor{keywordtype}{type}(tracer\_flow\_control\_CS), &
315                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}    :: tracer\_CSp\textcolor{comment}{ !< tracer control structure.}
316   \textcolor{keywordtype}{type}(ocean\_OBC\_type),         &
317                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}    :: OBC\textcolor{comment}{ !< Open boundaries control structure.}
318   \textcolor{keywordtype}{type}(time\_type),  \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)} :: dt\_forcing\textcolor{comment}{ !< The forcing time step}
319   \textcolor{comment}{! Local variables}
320   \textcolor{keywordtype}{real} :: eta(SZI\_(G),SZJ\_(G),SZK\_(G)+1) \textcolor{comment}{! The height of interfaces [Z ~> m].}
321   \textcolor{keywordtype}{real} :: areaTm(SZI\_(G),SZJ\_(G)) \textcolor{comment}{! A masked version of areaT [L2 ~> m2].}
322   \textcolor{keywordtype}{real} :: KE(SZK\_(G))  \textcolor{comment}{! The total kinetic energy of a layer [J].}
323   \textcolor{keywordtype}{real} :: PE(SZK\_(G)+1)\textcolor{comment}{! The available potential energy of an interface [J].}
324   \textcolor{keywordtype}{real} :: KE\_tot       \textcolor{comment}{! The total kinetic energy [J].}
325   \textcolor{keywordtype}{real} :: PE\_tot       \textcolor{comment}{! The total available potential energy [J].}
326   \textcolor{keywordtype}{real} :: Z\_0APE(SZK\_(G)+1) \textcolor{comment}{! The uniform depth which overlies the same}
327                        \textcolor{comment}{! volume as is below an interface [Z ~> m].}
328   \textcolor{keywordtype}{real} :: H\_0APE(SZK\_(G)+1) \textcolor{comment}{! A version of Z\_0APE, converted to m, usually positive.}
329   \textcolor{keywordtype}{real} :: toten        \textcolor{comment}{! The total kinetic & potential energies of}
330                        \textcolor{comment}{! all layers [J] (i.e. kg m2 s-2).}
331   \textcolor{keywordtype}{real} :: En\_mass      \textcolor{comment}{! The total kinetic and potential energies divided by}
332                        \textcolor{comment}{! the total mass of the ocean [m2 s-2].}
333   \textcolor{keywordtype}{real} :: vol\_lay(SZK\_(G))  \textcolor{comment}{! The volume of fluid in a layer [Z L2 ~> m3].}
334   \textcolor{keywordtype}{real} :: volbelow     \textcolor{comment}{! The volume of all layers beneath an interface [Z L2 ~> m3].}
335   \textcolor{keywordtype}{real} :: mass\_lay(SZK\_(G)) \textcolor{comment}{! The mass of fluid in a layer [kg].}
336   \textcolor{keywordtype}{real} :: mass\_tot     \textcolor{comment}{! The total mass of the ocean [kg].}
337   \textcolor{keywordtype}{real} :: vol\_tot      \textcolor{comment}{! The total ocean volume [m3].}
338   \textcolor{keywordtype}{real} :: mass\_chg     \textcolor{comment}{! The change in total ocean mass of fresh water since}
339                        \textcolor{comment}{! the last call to this subroutine [kg].}
340   \textcolor{keywordtype}{real} :: mass\_anom    \textcolor{comment}{! The change in fresh water that cannot be accounted for}
341                        \textcolor{comment}{! by the surface fluxes [kg].}
342   \textcolor{keywordtype}{real} :: Salt         \textcolor{comment}{! The total amount of salt in the ocean [ppt kg].}
343   \textcolor{keywordtype}{real} :: Salt\_chg     \textcolor{comment}{! The change in total ocean salt since the last call}
344                        \textcolor{comment}{! to this subroutine [ppt kg].}
345   \textcolor{keywordtype}{real} :: Salt\_anom    \textcolor{comment}{! The change in salt that cannot be accounted for by}
346                        \textcolor{comment}{! the surface fluxes [ppt kg].}
347   \textcolor{keywordtype}{real} :: salin        \textcolor{comment}{! The mean salinity of the ocean [ppt].}
348   \textcolor{keywordtype}{real} :: salin\_chg    \textcolor{comment}{! The change in total salt since the last call}
349                        \textcolor{comment}{! to this subroutine divided by total mass [ppt].}
350   \textcolor{keywordtype}{real} :: salin\_anom   \textcolor{comment}{! The change in total salt that cannot be accounted for by}
351                        \textcolor{comment}{! the surface fluxes divided by total mass [ppt].}
352   \textcolor{keywordtype}{real} :: Heat         \textcolor{comment}{! The total amount of Heat in the ocean [J].}
353   \textcolor{keywordtype}{real} :: Heat\_chg     \textcolor{comment}{! The change in total ocean heat since the last call to this subroutine [J].}
354   \textcolor{keywordtype}{real} :: Heat\_anom    \textcolor{comment}{! The change in heat that cannot be accounted for by the surface fluxes [J].}
355   \textcolor{keywordtype}{real} :: temp         \textcolor{comment}{! The mean potential temperature of the ocean [degC].}
356   \textcolor{keywordtype}{real} :: temp\_chg     \textcolor{comment}{! The change in total heat divided by total heat capacity}
357                        \textcolor{comment}{! of the ocean since the last call to this subroutine, degC.}
358   \textcolor{keywordtype}{real} :: temp\_anom    \textcolor{comment}{! The change in total heat that cannot be accounted for}
359                        \textcolor{comment}{! by the surface fluxes, divided by the total heat}
360                        \textcolor{comment}{! capacity of the ocean [degC].}
361   \textcolor{keywordtype}{real} :: hint         \textcolor{comment}{! The deviation of an interface from H [Z ~> m].}
362   \textcolor{keywordtype}{real} :: hbot         \textcolor{comment}{! 0 if the basin is deeper than H, or the}
363                        \textcolor{comment}{! height of the basin depth over H otherwise [Z ~> m].}
364                        \textcolor{comment}{! This makes PE only include real fluid.}
365   \textcolor{keywordtype}{real} :: hbelow       \textcolor{comment}{! The depth of fluid in all layers beneath an interface [Z ~> m].}
366   \textcolor{keywordtype}{type}(EFP\_type) :: &
367     mass\_EFP, &        \textcolor{comment}{! The total mass of the ocean in extended fixed point form [kg].}
368     salt\_EFP, &        \textcolor{comment}{! The total amount of salt in the ocean in extended fixed point form [ppt kg].}
369     heat\_EFP, &        \textcolor{comment}{! The total amount of heat in the ocean in extended fixed point form [J].}
370     salt\_chg\_EFP, &    \textcolor{comment}{! The change in total ocean salt since the last call to this subroutine [ppt kg].}
371     heat\_chg\_EFP, &    \textcolor{comment}{! The change in total ocean heat since the last call to this subroutine [J].}
372     mass\_chg\_EFP, &    \textcolor{comment}{! The change in total ocean mass of fresh water since}
373                        \textcolor{comment}{! the last call to this subroutine [kg].}
374     salt\_anom\_efp, &   \textcolor{comment}{! The change in salt that cannot be accounted for by the surface}
375                        \textcolor{comment}{! fluxes [ppt kg].}
376     heat\_anom\_efp, &   \textcolor{comment}{! The change in heat that cannot be accounted for by the surface fluxes [J].}
377     mass\_anom\_efp      \textcolor{comment}{! The change in fresh water that cannot be accounted for by the surface}
378                        \textcolor{comment}{! fluxes [kg].}
379   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{dimension(5)} :: EFP\_list \textcolor{comment}{! An array of EFP types for joint global sums.}
380   \textcolor{keywordtype}{real} :: CFL\_Iarea    \textcolor{comment}{! Direction-based inverse area used in CFL test [L-2].}
381   \textcolor{keywordtype}{real} :: CFL\_trans    \textcolor{comment}{! A transport-based definition of the CFL number [nondim].}
382   \textcolor{keywordtype}{real} :: CFL\_lin      \textcolor{comment}{! A simpler definition of the CFL number [nondim].}
383   \textcolor{keywordtype}{real} :: max\_CFL(2)   \textcolor{comment}{! The maxima of the CFL numbers [nondim].}
384   \textcolor{keywordtype}{real} :: Irho0        \textcolor{comment}{! The inverse of the reference density [m3 kg-1].}
385   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: &
386     tmp1               \textcolor{comment}{! A temporary array}
387   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)} :: &
388     PE\_pt              \textcolor{comment}{! The potential energy at each point [J].}
389   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: &
390     Temp\_int, Salt\_int \textcolor{comment}{! Layer and cell integrated heat and salt [J] and [g Salt].}
391   \textcolor{keywordtype}{real} :: HL2\_to\_kg    \textcolor{comment}{! A conversion factor from a thickness-volume to mass [kg H-1 L-2 ~> kg m-3 or 1]}
392   \textcolor{keywordtype}{real} :: KE\_scale\_factor   \textcolor{comment}{! The combination of unit rescaling factors in the kinetic energy}
393                             \textcolor{comment}{! calculation [kg T2 H-1 L-2 s-2 ~> kg m-3 or nondim]}
394   \textcolor{keywordtype}{real} :: PE\_scale\_factor   \textcolor{comment}{! The combination of unit rescaling factors in the potential energy}
395                             \textcolor{comment}{! calculation [kg T2 R-1 Z-1 L-2 s-2 ~> nondim]}
396   \textcolor{keywordtype}{integer} :: num\_nc\_fields  \textcolor{comment}{! The number of fields that will actually go into}
397                             \textcolor{comment}{! the NetCDF file.}
398   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, ns, nz, m, Isq, Ieq, Jsq, Jeq, isr, ier, jsr, jer
399   \textcolor{keywordtype}{integer} :: l, lbelow, labove   \textcolor{comment}{! indices of deep\_area\_vol, used to find Z\_0APE.}
400                                  \textcolor{comment}{! lbelow & labove are lower & upper limits for l}
401                                  \textcolor{comment}{! in the search for the entry in lH to use.}
402   \textcolor{keywordtype}{integer} :: start\_of\_day, num\_days
403   \textcolor{keywordtype}{real}    :: reday, var
404   \textcolor{keywordtype}{character(len=240)} :: energypath\_nc
405   \textcolor{keywordtype}{character(len=200)} :: mesg
406   \textcolor{keywordtype}{character(len=32)}  :: mesg\_intro, time\_units, day\_str, n\_str, date\_str
407   \textcolor{keywordtype}{logical} :: date\_stamped
408   \textcolor{keywordtype}{type}(time\_type) :: dt\_force \textcolor{comment}{! A time\_type version of the forcing timestep.}
409   \textcolor{keywordtype}{real} :: Tr\_stocks(MAX\_FIELDS\_)
410   \textcolor{keywordtype}{real} :: Tr\_min(MAX\_FIELDS\_), Tr\_max(MAX\_FIELDS\_)
411   \textcolor{keywordtype}{real} :: Tr\_min\_x(MAX\_FIELDS\_), Tr\_min\_y(MAX\_FIELDS\_), Tr\_min\_z(MAX\_FIELDS\_)
412   \textcolor{keywordtype}{real} :: Tr\_max\_x(MAX\_FIELDS\_), Tr\_max\_y(MAX\_FIELDS\_), Tr\_max\_z(MAX\_FIELDS\_)
413   \textcolor{keywordtype}{logical} :: Tr\_minmax\_got(MAX\_FIELDS\_) = .false.
414   \textcolor{keywordtype}{character(len=40)}, \textcolor{keywordtype}{dimension(MAX\_FIELDS\_)} :: &
415     Tr\_names, Tr\_units
416   \textcolor{keywordtype}{integer} :: nTr\_stocks
417   \textcolor{keywordtype}{integer} :: iyear, imonth, iday, ihour, iminute, isecond, itick \textcolor{comment}{! For call to get\_date()}
418   \textcolor{keywordtype}{logical} :: local\_open\_BC
419   \textcolor{keywordtype}{type}(OBC\_segment\_type), \textcolor{keywordtype}{pointer} :: segment => null()
420 
421  \textcolor{comment}{! A description for output of each of the fields.}
422   \textcolor{keywordtype}{type}(vardesc) :: vars(NUM\_FIELDS+MAX\_FIELDS\_)
423 
424   \textcolor{comment}{! write\_energy\_time is the next integral multiple of energysavedays.}
425   dt\_force = set\_time(seconds=2) ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dt\_forcing)) dt\_force = dt\_forcing
426   \textcolor{keywordflow}{if} (cs%previous\_calls == 0) \textcolor{keywordflow}{then}
427     \textcolor{keywordflow}{if} (cs%energysave\_geometric) \textcolor{keywordflow}{then}
428       \textcolor{keywordflow}{if} (cs%energysavedays\_geometric < cs%energysavedays) \textcolor{keywordflow}{then}
429         cs%write\_energy\_time = day + cs%energysavedays\_geometric
430         cs%geometric\_end\_time = cs%Start\_time + cs%energysavedays * &
431           (1 + (day - cs%Start\_time) / cs%energysavedays)
432       \textcolor{keywordflow}{else}
433         cs%write\_energy\_time = cs%Start\_time + cs%energysavedays * &
434           (1 + (day - cs%Start\_time) / cs%energysavedays)
435 \textcolor{keywordflow}{      endif}
436     \textcolor{keywordflow}{else}
437       cs%write\_energy\_time = cs%Start\_time + cs%energysavedays * &
438         (1 + (day - cs%Start\_time) / cs%energysavedays)
439 \textcolor{keywordflow}{    endif}
440   \textcolor{keywordflow}{elseif} (day + (dt\_force/2) <= cs%write\_energy\_time) \textcolor{keywordflow}{then}
441     \textcolor{keywordflow}{return}  \textcolor{comment}{! Do not write this step}
442   \textcolor{keywordflow}{else} \textcolor{comment}{! Determine the next write time before proceeding}
443     \textcolor{keywordflow}{if} (cs%energysave\_geometric) \textcolor{keywordflow}{then}
444       \textcolor{keywordflow}{if} (cs%write\_energy\_time + cs%energysavedays\_geometric >= &
445           cs%geometric\_end\_time) \textcolor{keywordflow}{then}
446         cs%write\_energy\_time = cs%geometric\_end\_time
447         cs%energysave\_geometric = .false.  \textcolor{comment}{! stop geometric progression}
448       \textcolor{keywordflow}{else}
449         cs%write\_energy\_time = cs%write\_energy\_time + cs%energysavedays\_geometric
450 \textcolor{keywordflow}{      endif}
451       cs%energysavedays\_geometric = cs%energysavedays\_geometric*2
452     \textcolor{keywordflow}{else}
453       cs%write\_energy\_time = cs%write\_energy\_time + cs%energysavedays
454 \textcolor{keywordflow}{    endif}
455 \textcolor{keywordflow}{  endif}
456 
457   num\_nc\_fields = 17
458   \textcolor{keywordflow}{if} (.not.cs%use\_temperature) num\_nc\_fields = 11
459   vars(1) = var\_desc(\textcolor{stringliteral}{"Ntrunc"},\textcolor{stringliteral}{"Nondim"},\textcolor{stringliteral}{"Number of Velocity Truncations"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
460   vars(2) = var\_desc(\textcolor{stringliteral}{"En"},\textcolor{stringliteral}{"Joules"},\textcolor{stringliteral}{"Total Energy"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
461   vars(3) = var\_desc(\textcolor{stringliteral}{"APE"},\textcolor{stringliteral}{"Joules"},\textcolor{stringliteral}{"Total Interface APE"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'i'})
462   vars(4) = var\_desc(\textcolor{stringliteral}{"KE"},\textcolor{stringliteral}{"Joules"},\textcolor{stringliteral}{"Total Layer KE"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'L'})
463   vars(5) = var\_desc(\textcolor{stringliteral}{"H0"},\textcolor{stringliteral}{"meter"},\textcolor{stringliteral}{"Zero APE Depth of Interface"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'i'})
464   vars(6) = var\_desc(\textcolor{stringliteral}{"Mass\_lay"},\textcolor{stringliteral}{"kg"},\textcolor{stringliteral}{"Total Layer Mass"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'L'})
465   vars(7) = var\_desc(\textcolor{stringliteral}{"Mass"},\textcolor{stringliteral}{"kg"},\textcolor{stringliteral}{"Total Mass"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
466   vars(8) = var\_desc(\textcolor{stringliteral}{"Mass\_chg"},\textcolor{stringliteral}{"kg"},\textcolor{stringliteral}{"Total Mass Change between Entries"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
467   vars(9) = var\_desc(\textcolor{stringliteral}{"Mass\_anom"},\textcolor{stringliteral}{"kg"},\textcolor{stringliteral}{"Anomalous Total Mass Change"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
468   vars(10) = var\_desc(\textcolor{stringliteral}{"max\_CFL\_trans"},\textcolor{stringliteral}{"Nondim"},\textcolor{stringliteral}{"Maximum finite-volume CFL"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
469   vars(11) = var\_desc(\textcolor{stringliteral}{"max\_CFL\_lin"},\textcolor{stringliteral}{"Nondim"},\textcolor{stringliteral}{"Maximum finite-difference CFL"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
470   \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
471     vars(12) = var\_desc(\textcolor{stringliteral}{"Salt"},\textcolor{stringliteral}{"kg"},\textcolor{stringliteral}{"Total Salt"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
472     vars(13) = var\_desc(\textcolor{stringliteral}{"Salt\_chg"},\textcolor{stringliteral}{"kg"},\textcolor{stringliteral}{"Total Salt Change between Entries"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
473     vars(14) = var\_desc(\textcolor{stringliteral}{"Salt\_anom"},\textcolor{stringliteral}{"kg"},\textcolor{stringliteral}{"Anomalous Total Salt Change"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
474     vars(15) = var\_desc(\textcolor{stringliteral}{"Heat"},\textcolor{stringliteral}{"Joules"},\textcolor{stringliteral}{"Total Heat"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
475     vars(16) = var\_desc(\textcolor{stringliteral}{"Heat\_chg"},\textcolor{stringliteral}{"Joules"},\textcolor{stringliteral}{"Total Heat Change between Entries"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
476     vars(17) = var\_desc(\textcolor{stringliteral}{"Heat\_anom"},\textcolor{stringliteral}{"Joules"},\textcolor{stringliteral}{"Anomalous Total Heat Change"},\textcolor{stringliteral}{'1'},\textcolor{stringliteral}{'1'})
477 \textcolor{keywordflow}{  endif}
478 
479   local\_open\_bc = .false.
480   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then}
481     local\_open\_bc = (obc%open\_u\_BCs\_exist\_globally .or. obc%open\_v\_BCs\_exist\_globally)
482 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ endif}
483 
484   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = g%ke
485   isq = g%IscB ; ieq = g%IecB ; jsq = g%JscB ; jeq = g%JecB
486   isr = is - (g%isd-1) ; ier = ie - (g%isd-1) ; jsr = js - (g%jsd-1) ; jer = je - (g%jsd-1)
487 
488 
489   hl2\_to\_kg = gv%H\_to\_kg\_m2*us%L\_to\_m**2
490 
491   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, &
492          \textcolor{stringliteral}{"write\_energy: Module must be initialized before it is used."})
493 
494   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
495     areatm(i,j) = g%mask2dT(i,j)*g%areaT(i,j)
496 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
497 
498   \textcolor{keywordflow}{if} (gv%Boussinesq) \textcolor{keywordflow}{then}
499     tmp1(:,:,:) = 0.0
500     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
501       tmp1(i,j,k) = h(i,j,k) * (hl2\_to\_kg*areatm(i,j))
502 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
503 
504     \textcolor{comment}{! This block avoids using the points beyond an open boundary condition}
505     \textcolor{comment}{! in the accumulation of mass, but perhaps it would be unnecessary if there}
506     \textcolor{comment}{! were a more judicious use of masks in the loops 4 or 7 lines above.}
507     \textcolor{keywordflow}{if} (local\_open\_bc) \textcolor{keywordflow}{then}
508       \textcolor{keywordflow}{do} ns=1, obc%number\_of\_segments
509         segment => obc%segment(ns)
510         \textcolor{keywordflow}{if} (.not. segment%on\_pe .or. segment%specified) cycle
511         i=segment%HI%IsdB ; j=segment%HI%JsdB
512         \textcolor{keywordflow}{if} (segment%direction == obc\_direction\_e) \textcolor{keywordflow}{then}
513           \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=segment%HI%jsd,segment%HI%jed
514             tmp1(i+1,j,k) = 0.0
515 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}
516         \textcolor{keywordflow}{elseif} (segment%direction == obc\_direction\_w) \textcolor{keywordflow}{then}
517           \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=segment%HI%jsd,segment%HI%jed
518             tmp1(i,j,k) = 0.0
519 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}
520         \textcolor{keywordflow}{elseif} (segment%direction == obc\_direction\_n) \textcolor{keywordflow}{then}
521           \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=segment%HI%isd,segment%HI%ied
522             tmp1(i,j+1,k) = 0.0
523 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}
524         \textcolor{keywordflow}{elseif} (segment%direction == obc\_direction\_s) \textcolor{keywordflow}{then}
525           \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=segment%HI%isd,segment%HI%ied
526             tmp1(i,j,k) = 0.0
527 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}
528 \textcolor{keywordflow}{        endif}
529 \textcolor{keywordflow}{      enddo}
530 \textcolor{keywordflow}{    endif}
531 
532     mass\_tot = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum}{reproducing\_sum}}(tmp1, isr, ier, jsr, jer, sums=mass\_lay, efp\_sum=mass\_efp)
533     \textcolor{keywordflow}{do} k=1,nz ; vol\_lay(k) = (us%m\_to\_L**2*gv%H\_to\_Z/gv%H\_to\_kg\_m2)*mass\_lay(k) ;\textcolor{keywordflow}{ enddo}
534   \textcolor{keywordflow}{else}
535     tmp1(:,:,:) = 0.0
536     \textcolor{keywordflow}{if} (cs%do\_APE\_calc) \textcolor{keywordflow}{then}
537       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
538         tmp1(i,j,k) = hl2\_to\_kg * h(i,j,k) * areatm(i,j)
539 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
540       mass\_tot = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum}{reproducing\_sum}}(tmp1, isr, ier, jsr, jer, sums=mass\_lay, efp\_sum=mass\_efp)
541 
542       \textcolor{keyword}{call }find\_eta(h, tv, g, gv, us, eta)
543       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
544         tmp1(i,j,k) = us%Z\_to\_m*us%L\_to\_m**2*(eta(i,j,k)-eta(i,j,k+1)) * areatm(i,j)
545 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
546       vol\_tot = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum}{reproducing\_sum}}(tmp1, isr, ier, jsr, jer, sums=vol\_lay)
547       \textcolor{keywordflow}{do} k=1,nz ; vol\_lay(k) = us%m\_to\_Z*us%m\_to\_L**2 * vol\_lay(k) ;\textcolor{keywordflow}{ enddo}
548     \textcolor{keywordflow}{else}
549       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
550         tmp1(i,j,k) = hl2\_to\_kg * h(i,j,k) * areatm(i,j)
551 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
552       mass\_tot = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum}{reproducing\_sum}}(tmp1, isr, ier, jsr, jer, sums=mass\_lay, efp\_sum=mass\_efp)
553       \textcolor{keywordflow}{do} k=1,nz ; vol\_lay(k) = us%m\_to\_Z*us%m\_to\_L**2*us%kg\_m3\_to\_R * (mass\_lay(k) / gv%Rho0) ;\textcolor{keywordflow}{ enddo}
554 \textcolor{keywordflow}{    endif}
555 \textcolor{keywordflow}{  endif} \textcolor{comment}{! Boussinesq}
556 
557   ntr\_stocks = 0
558   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(tracer\_csp)) \textcolor{keywordflow}{then}
559     \textcolor{keyword}{call }call\_tracer\_stocks(h, tr\_stocks, g, gv, tracer\_csp, stock\_names=tr\_names, &
560                             stock\_units=tr\_units, num\_stocks=ntr\_stocks,&
561                             got\_min\_max=tr\_minmax\_got, global\_min=tr\_min, global\_max=tr\_max, &
562                             xgmin=tr\_min\_x, ygmin=tr\_min\_y, zgmin=tr\_min\_z,&
563                             xgmax=tr\_max\_x, ygmax=tr\_max\_y, zgmax=tr\_max\_z)
564     \textcolor{keywordflow}{if} (ntr\_stocks > 0) \textcolor{keywordflow}{then}
565       \textcolor{keywordflow}{do} m=1,ntr\_stocks
566         vars(num\_nc\_fields+m) = var\_desc(tr\_names(m), units=tr\_units(m), &
567                       longname=tr\_names(m), hor\_grid=\textcolor{stringliteral}{'1'}, z\_grid=\textcolor{stringliteral}{'1'})
568 \textcolor{keywordflow}{      enddo}
569       num\_nc\_fields = num\_nc\_fields + ntr\_stocks
570 \textcolor{keywordflow}{    endif}
571 \textcolor{keywordflow}{  endif}
572 
573   \textcolor{keywordflow}{if} (cs%previous\_calls == 0) \textcolor{keywordflow}{then}
574 
575     cs%mass\_prev\_EFP = mass\_efp
576     cs%fresh\_water\_in\_EFP = real\_to\_efp(0.0)
577     \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
578       cs%net\_salt\_in\_EFP = real\_to\_efp(0.0)  ; cs%net\_heat\_in\_EFP = real\_to\_efp(0.0)
579 \textcolor{keywordflow}{    endif}
580 
581     \textcolor{comment}{!  Reopen or create a text output file, with an explanatory header line.}
582     \textcolor{keywordflow}{if} (is\_root\_pe()) \textcolor{keywordflow}{then}
583       \textcolor{keywordflow}{if} (day > cs%Start\_time) \textcolor{keywordflow}{then}
584         \textcolor{keyword}{call }open\_file(cs%fileenergy\_ascii, trim(cs%energyfile), &
585                        action=append\_file, form=ascii\_file, nohdrs=.true.)
586       \textcolor{keywordflow}{else}
587         \textcolor{keyword}{call }open\_file(cs%fileenergy\_ascii, trim(cs%energyfile), &
588                        action=writeonly\_file, form=ascii\_file, nohdrs=.true.)
589         \textcolor{keywordflow}{if} (abs(cs%timeunit - 86400.0) < 1.0) \textcolor{keywordflow}{then}
590           \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
591             \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'("  Step,",7x,"Day,  Truncs,      &}
592 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &Energy/Mass,      Maximum CFL,  Mean Sea Level,  Total Mass,  Mean Salin, &}
593 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &Mean Temp, Frac Mass Err,   Salin Err,    Temp Err")'})
594             \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'(12x,"[days]",17x,"[m2 s-2]",11x,"[Nondim]",7x,"[m]",13x,&}
595 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &"[kg]",9x,"[PSU]",6x,"[degC]",7x,"[Nondim]",8x,"[PSU]",8x,"[degC]")'})
596           \textcolor{keywordflow}{else}
597             \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'("  Step,",7x,"Day,  Truncs,      &}
598 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &Energy/Mass,      Maximum CFL,  Mean sea level,   Total Mass,    Frac Mass Err")'})
599             \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'(12x,"[days]",17x,"[m2 s-2]",11x,"[Nondim]",8x,"[m]",13x,&}
600 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &"[kg]",11x,"[Nondim]")'})
601 \textcolor{keywordflow}{          endif}
602         \textcolor{keywordflow}{else}
603           \textcolor{keywordflow}{if} ((cs%timeunit >= 0.99) .and. (cs%timeunit < 1.01)) \textcolor{keywordflow}{then}
604             time\_units = \textcolor{stringliteral}{"           [seconds]     "}
605           \textcolor{keywordflow}{elseif} ((cs%timeunit >= 3599.0) .and. (cs%timeunit < 3601.0)) \textcolor{keywordflow}{then}
606             time\_units = \textcolor{stringliteral}{"            [hours]      "}
607           \textcolor{keywordflow}{elseif} ((cs%timeunit >= 86399.0) .and. (cs%timeunit < 86401.0)) \textcolor{keywordflow}{then}
608             time\_units = \textcolor{stringliteral}{"             [days]      "}
609           \textcolor{keywordflow}{elseif} ((cs%timeunit >= 3.0e7) .and. (cs%timeunit < 3.2e7)) \textcolor{keywordflow}{then}
610             time\_units = \textcolor{stringliteral}{"            [years]      "}
611           \textcolor{keywordflow}{else}
612             \textcolor{keyword}{write}(time\_units,\textcolor{stringliteral}{'(9x,"[",es8.2," s]    ")'}) cs%timeunit
613 \textcolor{keywordflow}{          endif}
614 
615           \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
616             \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'("  Step,",7x,"Time, Truncs,      &}
617 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &Energy/Mass,      Maximum CFL,  Mean Sea Level,  Total Mass,  Mean Salin, &}
618 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &Mean Temp, Frac Mass Err,   Salin Err,    Temp Err")'})
619             \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'(A25,10x,"[m2 s-2]",11x,"[Nondim]",7x,"[m]",13x,&}
620 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &"[kg]",9x,"[PSU]",6x,"[degC]",7x,"[Nondim]",8x,"[PSU]",6x,&}
621 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &"[degC]")'}) time\_units
622           \textcolor{keywordflow}{else}
623             \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'("  Step,",7x,"Time, Truncs,      &}
624 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &Energy/Mass,      Maximum CFL,  Mean sea level,   Total Mass,    Frac Mass Err")'})
625             \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'(A25,10x,"[m2 s-2]",11x,"[Nondim]",8x,"[m]",13x,&}
626 \textcolor{stringliteral}{}\textcolor{stringliteral}{                &"[kg]",11x,"[Nondim]")'}) time\_units
627 \textcolor{keywordflow}{          endif}
628 \textcolor{keywordflow}{        endif}
629 \textcolor{keywordflow}{      endif}
630 \textcolor{keywordflow}{    endif}
631 
632     energypath\_nc = trim(cs%energyfile) // \textcolor{stringliteral}{".nc"}
633     \textcolor{keywordflow}{if} (day > cs%Start\_time) \textcolor{keywordflow}{then}
634       \textcolor{keyword}{call }reopen\_file(cs%fileenergy\_nc, trim(energypath\_nc), vars, &
635                        num\_nc\_fields, cs%fields, single\_file, cs%timeunit, &
636                        g=g, gv=gv)
637     \textcolor{keywordflow}{else}
638       \textcolor{keyword}{call }create\_file(cs%fileenergy\_nc, trim(energypath\_nc), vars, &
639                        num\_nc\_fields, cs%fields, single\_file, cs%timeunit, &
640                        g=g, gv=gv)
641 \textcolor{keywordflow}{    endif}
642 \textcolor{keywordflow}{  endif}
643 
644   \textcolor{keywordflow}{if} (cs%do\_APE\_calc) \textcolor{keywordflow}{then}
645     lbelow = 1 ; volbelow = 0.0
646     \textcolor{keywordflow}{do} k=nz,1,-1
647       volbelow = volbelow + vol\_lay(k)
648       \textcolor{keywordflow}{if} ((volbelow >= cs%DL(cs%lH(k))%vol\_below) .and. &
649           (volbelow < cs%DL(cs%lH(k)+1)%vol\_below)) \textcolor{keywordflow}{then}
650         l = cs%lH(k)
651       \textcolor{keywordflow}{else}
652         labove=cs%list\_size+1
653         l = (labove + lbelow) / 2
654         \textcolor{keywordflow}{do} \textcolor{keywordflow}{while} (l > lbelow)
655           \textcolor{keywordflow}{if} (volbelow < cs%DL(l)%vol\_below) \textcolor{keywordflow}{then} ; labove = l
656           \textcolor{keywordflow}{else} ; lbelow = l ;\textcolor{keywordflow}{ endif}
657           l = (labove + lbelow) / 2
658 \textcolor{keywordflow}{        enddo}
659         cs%lH(k) = l
660 \textcolor{keywordflow}{      endif}
661       lbelow = l
662       z\_0ape(k) = cs%DL(l)%depth - (volbelow - cs%DL(l)%vol\_below) / cs%DL(l)%area
663 \textcolor{keywordflow}{    enddo}
664     z\_0ape(nz+1) = cs%DL(2)%depth
665 
666     \textcolor{comment}{!   Calculate the Available Potential Energy integrated over each interface.  With a nonlinear}
667     \textcolor{comment}{! equation of state or with a bulk mixed layer this calculation is only approximate.}
668     \textcolor{comment}{! With an ALE model this does not make sense and should be revisited.}
669     pe\_scale\_factor = us%RZ\_to\_kg\_m2*us%L\_to\_m**2*us%L\_T\_to\_m\_s**2
670     pe\_pt(:,:,:) = 0.0
671     \textcolor{keywordflow}{if} (gv%Boussinesq) \textcolor{keywordflow}{then}
672       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
673         hbelow = 0.0
674         \textcolor{keywordflow}{do} k=nz,1,-1
675           hbelow = hbelow + h(i,j,k) * gv%H\_to\_Z
676           hint = z\_0ape(k) + (hbelow - g%bathyT(i,j))
677           hbot = z\_0ape(k) - g%bathyT(i,j)
678           hbot = (hbot + abs(hbot)) * 0.5
679           pe\_pt(i,j,k) = (0.5 * pe\_scale\_factor * areatm(i,j)) * (gv%Rho0*gv%g\_prime(k)) * &
680                   (hint * hint - hbot * hbot)
681 \textcolor{keywordflow}{        enddo}
682 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
683     \textcolor{keywordflow}{else}
684       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
685         \textcolor{keywordflow}{do} k=nz,1,-1
686           hint = z\_0ape(k) + eta(i,j,k)  \textcolor{comment}{! eta and H\_0 have opposite signs.}
687           hbot = max(z\_0ape(k) - g%bathyT(i,j), 0.0)
688           pe\_pt(i,j,k) = (0.5 * pe\_scale\_factor * areatm(i,j) * (gv%Rho0*gv%g\_prime(k))) * &
689                   (hint * hint - hbot * hbot)
690 \textcolor{keywordflow}{        enddo}
691 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
692 \textcolor{keywordflow}{    endif}
693 
694     pe\_tot = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum}{reproducing\_sum}}(pe\_pt, isr, ier, jsr, jer, sums=pe)
695     \textcolor{keywordflow}{do} k=1,nz+1 ; h\_0ape(k) = us%Z\_to\_m*z\_0ape(k) ;\textcolor{keywordflow}{ enddo}
696   \textcolor{keywordflow}{else}
697     pe\_tot = 0.0
698     \textcolor{keywordflow}{do} k=1,nz+1 ; pe(k) = 0.0 ; h\_0ape(k) = 0.0 ;\textcolor{keywordflow}{ enddo}
699 \textcolor{keywordflow}{  endif}
700 
701 \textcolor{comment}{! Calculate the Kinetic Energy integrated over each layer.}
702   ke\_scale\_factor = hl2\_to\_kg*us%L\_T\_to\_m\_s**2
703   tmp1(:,:,:) = 0.0
704   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
705     tmp1(i,j,k) = (0.25 * ke\_scale\_factor * (areatm(i,j) * h(i,j,k))) * &
706             ((u(i-1,j,k)**2 + u(i,j,k)**2) + (v(i,j-1,k)**2 + v(i,j,k)**2))
707 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
708 
709   ke\_tot = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum}{reproducing\_sum}}(tmp1, isr, ier, jsr, jer, sums=ke)
710 
711   toten = ke\_tot + pe\_tot
712 
713   salt = 0.0 ; heat = 0.0
714   \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
715     temp\_int(:,:) = 0.0 ; salt\_int(:,:) = 0.0
716     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
717       salt\_int(i,j) = salt\_int(i,j) + tv%S(i,j,k) * &
718                       (h(i,j,k)*(hl2\_to\_kg * areatm(i,j)))
719       temp\_int(i,j) = temp\_int(i,j) + (us%Q\_to\_J\_kg*tv%C\_p * tv%T(i,j,k)) * &
720                       (h(i,j,k)*(hl2\_to\_kg * areatm(i,j)))
721 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
722     salt\_efp = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum__efp}{reproducing\_sum\_efp}}(salt\_int, isr, ier, jsr, jer, only\_on\_pe=.true.)
723     heat\_efp = \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum__efp}{reproducing\_sum\_efp}}(temp\_int, isr, ier, jsr, jer, only\_on\_pe=.true.)
724 
725     \textcolor{comment}{! Combining the sums avoids multiple blocking all-PE updates.}
726     efp\_list(1) = salt\_efp ;  efp\_list(2) = heat\_efp ; efp\_list(3) = cs%fresh\_water\_in\_EFP
727     efp\_list(4) = cs%net\_salt\_in\_EFP ; efp\_list(5) = cs%net\_heat\_in\_EFP
728     \textcolor{keyword}{call }\mbox{\hyperlink{interfacemom__coms_1_1efp__sum__across__pes}{efp\_sum\_across\_pes}}(efp\_list, 5)
729     \textcolor{comment}{! Return the globally summed values to the original variables.}
730     salt\_efp = efp\_list(1) ; heat\_efp = efp\_list(2) ; cs%fresh\_water\_in\_EFP = efp\_list(3)
731     cs%net\_salt\_in\_EFP = efp\_list(4) ; cs%net\_heat\_in\_EFP = efp\_list(5)
732 
733     salt = efp\_to\_real(salt\_efp)
734     heat = efp\_to\_real(heat\_efp)
735   \textcolor{keywordflow}{else}
736     \textcolor{keyword}{call }\mbox{\hyperlink{interfacemom__coms_1_1efp__sum__across__pes}{efp\_sum\_across\_pes}}(cs%fresh\_water\_in\_EFP)
737 \textcolor{keywordflow}{  endif}
738 
739 \textcolor{comment}{! Calculate the maximum CFL numbers.}
740   max\_cfl(1:2) = 0.0
741   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=isq,ieq
742     cfl\_iarea = g%IareaT(i,j)
743     \textcolor{keywordflow}{if} (u(i,j,k) < 0.0) &
744       cfl\_iarea = g%IareaT(i+1,j)
745 
746     cfl\_trans = abs(u(i,j,k) * cs%dt\_in\_T) * (g%dy\_Cu(i,j) * cfl\_iarea)
747     cfl\_lin = abs(u(i,j,k) * cs%dt\_in\_T) * g%IdxCu(i,j)
748     max\_cfl(1) = max(max\_cfl(1), cfl\_trans)
749     max\_cfl(2) = max(max\_cfl(2), cfl\_lin)
750 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
751   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=jsq,jeq ; \textcolor{keywordflow}{do} i=is,ie
752     cfl\_iarea = g%IareaT(i,j)
753     \textcolor{keywordflow}{if} (v(i,j,k) < 0.0) &
754       cfl\_iarea = g%IareaT(i,j+1)
755 
756     cfl\_trans = abs(v(i,j,k) * cs%dt\_in\_T) * (g%dx\_Cv(i,j) * cfl\_iarea)
757     cfl\_lin = abs(v(i,j,k) * cs%dt\_in\_T) * g%IdyCv(i,j)
758     max\_cfl(1) = max(max\_cfl(1), cfl\_trans)
759     max\_cfl(2) = max(max\_cfl(2), cfl\_lin)
760 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
761 
762   \textcolor{keyword}{call }sum\_across\_pes(cs%ntrunc)
763   \textcolor{comment}{!   Sum the various quantities across all the processors.  This sum is NOT}
764   \textcolor{comment}{! guaranteed to be bitwise reproducible, even on the same decomposition.}
765   \textcolor{comment}{!   The sum of Tr\_stocks should be reimplemented using the reproducing sums.}
766   \textcolor{keywordflow}{if} (ntr\_stocks > 0) \textcolor{keyword}{call }sum\_across\_pes(tr\_stocks,ntr\_stocks)
767 
768   \textcolor{keyword}{call }max\_across\_pes(max\_cfl, 2)
769   irho0 = 1.0 / (us%R\_to\_kg\_m3*gv%Rho0)
770 
771   \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
772     \textcolor{keywordflow}{if} (cs%previous\_calls == 0) \textcolor{keywordflow}{then}
773       cs%salt\_prev\_EFP = salt\_efp ; cs%heat\_prev\_EFP = heat\_efp
774 \textcolor{keywordflow}{    endif}
775     salt\_chg\_efp = salt\_efp - cs%salt\_prev\_EFP
776     salt\_chg = efp\_to\_real(salt\_chg\_efp)
777     salt\_anom\_efp = salt\_chg\_efp - cs%net\_salt\_in\_EFP
778     salt\_anom = efp\_to\_real(salt\_anom\_efp)
779     heat\_chg\_efp = heat\_efp - cs%heat\_prev\_EFP
780     heat\_chg = efp\_to\_real(heat\_chg\_efp)
781     heat\_anom\_efp = heat\_chg\_efp - cs%net\_heat\_in\_EFP
782     heat\_anom = efp\_to\_real(heat\_anom\_efp)
783 \textcolor{keywordflow}{  endif}
784 
785   mass\_chg\_efp = mass\_efp - cs%mass\_prev\_EFP
786   mass\_anom\_efp = mass\_chg\_efp - cs%fresh\_water\_in\_EFP
787   mass\_anom = efp\_to\_real(mass\_anom\_efp)
788   \textcolor{keywordflow}{if} (cs%use\_temperature .and. .not.gv%Boussinesq) \textcolor{keywordflow}{then}
789     \textcolor{comment}{! net\_salt\_input needs to be converted from ppt m s-1 to kg m-2 s-1.}
790     mass\_anom = mass\_anom - 0.001*efp\_to\_real(cs%net\_salt\_in\_EFP)
791 \textcolor{keywordflow}{  endif}
792   mass\_chg = efp\_to\_real(mass\_chg\_efp)
793 
794   \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
795     salin = salt / mass\_tot ; salin\_anom = salt\_anom / mass\_tot
796    \textcolor{comment}{! salin\_chg = Salt\_chg / mass\_tot}
797     temp = heat / (mass\_tot*us%Q\_to\_J\_kg*tv%C\_p) ; temp\_anom = heat\_anom / (mass\_tot*us%Q\_to\_J\_kg*tv%C\_p)
798 \textcolor{keywordflow}{  endif}
799   en\_mass = toten / mass\_tot
800 
801   \textcolor{keyword}{call }get\_time(day, start\_of\_day, num\_days)
802   date\_stamped = (cs%date\_stamped\_output .and. (get\_calendar\_type() /= no\_calendar))
803   \textcolor{keywordflow}{if} (date\_stamped) &
804     \textcolor{keyword}{call }get\_date(day, iyear, imonth, iday, ihour, iminute, isecond, itick)
805   \textcolor{keywordflow}{if} (abs(cs%timeunit - 86400.0) < 1.0) \textcolor{keywordflow}{then}
806     reday = \textcolor{keywordtype}{REAL}(num\_days)+ (\textcolor{keywordtype}{REAL}(start\_of\_day)/86400.0)
807     mesg\_intro = \textcolor{stringliteral}{"MOM Day "}
808   \textcolor{keywordflow}{else}
809     reday = \textcolor{keywordtype}{REAL}(num\_days)*(86400.0/CS%timeunit) + &
810             \textcolor{keywordtype}{REAL}(start\_of\_day)/abs(CS%timeunit)
811     mesg\_intro = \textcolor{stringliteral}{"MOM Time "}
812 \textcolor{keywordflow}{  endif}
813   \textcolor{keywordflow}{if} (reday < 1.0e8) \textcolor{keywordflow}{then} ;      \textcolor{keyword}{write}(day\_str, \textcolor{stringliteral}{'(F12.3)'}) reday
814   \textcolor{keywordflow}{elseif} (reday < 1.0e11) \textcolor{keywordflow}{then} ; \textcolor{keyword}{write}(day\_str, \textcolor{stringliteral}{'(F15.3)'}) reday
815   \textcolor{keywordflow}{else} ;                         \textcolor{keyword}{write}(day\_str, \textcolor{stringliteral}{'(ES15.9)'}) reday ;\textcolor{keywordflow}{ endif}
816 
817   \textcolor{keywordflow}{if}     (n < 1000000)   \textcolor{keywordflow}{then} ; \textcolor{keyword}{write}(n\_str, \textcolor{stringliteral}{'(I6)'})  n
818   \textcolor{keywordflow}{elseif} (n < 10000000)  \textcolor{keywordflow}{then} ; \textcolor{keyword}{write}(n\_str, \textcolor{stringliteral}{'(I7)'})  n
819   \textcolor{keywordflow}{elseif} (n < 100000000) \textcolor{keywordflow}{then} ; \textcolor{keyword}{write}(n\_str, \textcolor{stringliteral}{'(I8)'})  n
820   \textcolor{keywordflow}{else}                        ; \textcolor{keyword}{write}(n\_str, \textcolor{stringliteral}{'(I10)'}) n ;\textcolor{keywordflow}{ endif}
821 
822   \textcolor{keywordflow}{if} (date\_stamped) \textcolor{keywordflow}{then}
823     \textcolor{keyword}{write}(date\_str,\textcolor{stringliteral}{'("MOM Date",i7,2("/",i2.2)," ",i2.2,2(":",i2.2))'}) &
824        iyear, imonth, iday, ihour, iminute, isecond
825   \textcolor{keywordflow}{else}
826     date\_str = trim(mesg\_intro)//trim(day\_str)
827 \textcolor{keywordflow}{  endif}
828 
829   \textcolor{keywordflow}{if} (is\_root\_pe()) \textcolor{keywordflow}{then}
830     \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
831         \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'(A," ",A,": En ",ES12.6, ", MaxCFL ", F8.5, ", Mass ", &}
832 \textcolor{stringliteral}{}\textcolor{stringliteral}{                & ES18.12, ", Salt ", F15.11,", Temp ", F15.11)'}) &
833           trim(date\_str), trim(n\_str), en\_mass, max\_cfl(1), mass\_tot, salin, temp
834     \textcolor{keywordflow}{else}
835         \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'(A," ",A,": En ",ES12.6, ", MaxCFL ", F8.5, ", Mass ", &}
836 \textcolor{stringliteral}{}\textcolor{stringliteral}{                & ES18.12)'}) &
837           trim(date\_str), trim(n\_str), en\_mass, max\_cfl(1), mass\_tot
838 \textcolor{keywordflow}{    endif}
839 
840     \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
841       \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'(A,",",A,",", I6,", En ",ES22.16, &}
842 \textcolor{stringliteral}{}\textcolor{stringliteral}{                               &", CFL ", F8.5, ", SL ",&}
843 \textcolor{stringliteral}{}\textcolor{stringliteral}{                               &es11.4,", M ",ES11.5,", S",f8.4,", T",f8.4,&}
844 \textcolor{stringliteral}{}\textcolor{stringliteral}{                               &", Me ",ES9.2,", Se ",ES9.2,", Te ",ES9.2)'}) &
845             trim(n\_str), trim(day\_str), cs%ntrunc, en\_mass, max\_cfl(1), &
846             -h\_0ape(1), mass\_tot, salin, temp, mass\_anom/mass\_tot, salin\_anom, &
847             temp\_anom
848     \textcolor{keywordflow}{else}
849       \textcolor{keyword}{write}(cs%fileenergy\_ascii,\textcolor{stringliteral}{'(A,",",A,",", I6,", En ",ES22.16, &}
850 \textcolor{stringliteral}{}\textcolor{stringliteral}{                                &", CFL ", F8.5, ", SL ",&}
851 \textcolor{stringliteral}{}\textcolor{stringliteral}{                                  &ES11.4,", Mass ",ES11.5,", Me ",ES9.2)'}) &
852             trim(n\_str), trim(day\_str), cs%ntrunc, en\_mass, max\_cfl(1), &
853             -h\_0ape(1), mass\_tot, mass\_anom/mass\_tot
854 \textcolor{keywordflow}{    endif}
855 
856     \textcolor{keywordflow}{if} (cs%ntrunc > 0) \textcolor{keywordflow}{then}
857       \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'(A," Energy/Mass:",ES12.5," Truncations ",I0)'}) &
858         trim(date\_str), en\_mass, cs%ntrunc
859 \textcolor{keywordflow}{    endif}
860 
861     \textcolor{keywordflow}{if} (cs%write\_stocks) \textcolor{keywordflow}{then}
862       \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'("    Total Energy: ",Z16.16,ES24.16)'}) toten, toten
863       \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'("    Total Mass: ",ES24.16,", Change: ",ES24.16," Error: ",ES12.5," (",ES8.1,")")'}) &
864             mass\_tot, mass\_chg, mass\_anom, mass\_anom/mass\_tot
865       \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
866         \textcolor{keywordflow}{if} (salt == 0.) \textcolor{keywordflow}{then}
867           \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'("    Total Salt: ",ES24.16,", Change: ",ES24.16," Error: ",ES12.5)'}) &
868               salt*0.001, salt\_chg*0.001, salt\_anom*0.001
869         \textcolor{keywordflow}{else}
870           \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'("    Total Salt: ",ES24.16,", Change: ",ES24.16," Error: ",ES12.5," (",ES8.1,")")'}) &
871               salt*0.001, salt\_chg*0.001, salt\_anom*0.001, salt\_anom/salt
872 \textcolor{keywordflow}{        endif}
873         \textcolor{keywordflow}{if} (heat == 0.) \textcolor{keywordflow}{then}
874           \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'("    Total Heat: ",ES24.16,", Change: ",ES24.16," Error: ",ES12.5)'}) &
875               heat, heat\_chg, heat\_anom
876         \textcolor{keywordflow}{else}
877           \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'("    Total Heat: ",ES24.16,", Change: ",ES24.16," Error: ",ES12.5," (",ES8.1,")")'}) &
878               heat, heat\_chg, heat\_anom, heat\_anom/heat
879 \textcolor{keywordflow}{        endif}
880 \textcolor{keywordflow}{      endif}
881       \textcolor{keywordflow}{do} m=1,ntr\_stocks
882 
883          \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'("      Total ",a,": ",ES24.16,X,a)'}) &
884               trim(tr\_names(m)), tr\_stocks(m), trim(tr\_units(m))
885 
886          \textcolor{keywordflow}{if} (tr\_minmax\_got(m)) \textcolor{keywordflow}{then}
887            \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'(64X,"Global Min:",ES24.16,X,"at: (", f7.2,","f7.2,","f8.2,")"  )'}) &
888                 tr\_min(m),tr\_min\_x(m),tr\_min\_y(m),tr\_min\_z(m)
889            \textcolor{keyword}{write}(*,\textcolor{stringliteral}{'(64X,"Global Max:",ES24.16,X,"at: (", f7.2,","f7.2,","f8.2,")"  )'}) &
890                 tr\_max(m),tr\_max\_x(m),tr\_max\_y(m),tr\_max\_z(m)
891 \textcolor{keywordflow}{        endif}
892 
893 \textcolor{keywordflow}{      enddo}
894 \textcolor{keywordflow}{    endif}
895 \textcolor{keywordflow}{  endif}
896 
897   var = \textcolor{keywordtype}{real}(CS%ntrunc)
898   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(1), var, reday)
899   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(2), toten, reday)
900   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(3), pe, reday)
901   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(4), ke, reday)
902   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(5), h\_0ape, reday)
903   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(6), mass\_lay, reday)
904 
905   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(7), mass\_tot, reday)
906   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(8), mass\_chg, reday)
907   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(9), mass\_anom, reday)
908   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(10), max\_cfl(1), reday)
909   \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(11), max\_cfl(2), reday)
910   \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
911     \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(12), 0.001*salt, reday)
912     \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(13), 0.001*salt\_chg, reday)
913     \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(14), 0.001*salt\_anom, reday)
914     \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(15), heat, reday)
915     \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(16), heat\_chg, reday)
916     \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(17), heat\_anom, reday)
917     \textcolor{keywordflow}{do} m=1,ntr\_stocks
918       \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(17+m), tr\_stocks(m), reday)
919 \textcolor{keywordflow}{    enddo}
920   \textcolor{keywordflow}{else}
921     \textcolor{keywordflow}{do} m=1,ntr\_stocks
922       \textcolor{keyword}{call }write\_field(cs%fileenergy\_nc, cs%fields(11+m), tr\_stocks(m), reday)
923 \textcolor{keywordflow}{    enddo}
924 \textcolor{keywordflow}{  endif}
925 
926   \textcolor{keyword}{call }flush\_file(cs%fileenergy\_nc)
927 
928   \textcolor{comment}{! The second (impossible-looking) test looks for a NaN in En\_mass.}
929   \textcolor{keywordflow}{if} ((en\_mass>cs%max\_Energy) .or. &
930      ((en\_mass>cs%max\_Energy) .and. (en\_mass<cs%max\_Energy))) \textcolor{keywordflow}{then}
931     \textcolor{keyword}{write}(mesg,\textcolor{stringliteral}{'("Energy per unit mass of ",ES11.4," exceeds ",ES11.4)'}) &
932                   en\_mass, cs%max\_Energy
933     \textcolor{keyword}{call }mom\_error(fatal, &
934       \textcolor{stringliteral}{"write\_energy : Excessive energy per unit mass or NaNs forced model termination."})
935 \textcolor{keywordflow}{  endif}
936   \textcolor{keywordflow}{if} (cs%ntrunc>cs%maxtrunc) \textcolor{keywordflow}{then}
937     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"write\_energy : Ocean velocity has been truncated too many times."})
938 \textcolor{keywordflow}{  endif}
939   cs%ntrunc = 0
940   cs%previous\_calls = cs%previous\_calls + 1
941 
942   cs%mass\_prev\_EFP = mass\_efp ; cs%fresh\_water\_in\_EFP = real\_to\_efp(0.0)
943   \textcolor{keywordflow}{if} (cs%use\_temperature) \textcolor{keywordflow}{then}
944     cs%salt\_prev\_EFP = salt\_efp ; cs%net\_salt\_in\_EFP = real\_to\_efp(0.0)
945     cs%heat\_prev\_EFP = heat\_efp ; cs%net\_heat\_in\_EFP = real\_to\_efp(0.0)
946 \textcolor{keywordflow}{  endif}
947 
\end{DoxyCode}
