\hypertarget{namespacemom__tracer__z__init}{}\section{mom\+\_\+tracer\+\_\+z\+\_\+init Module Reference}
\label{namespacemom__tracer__z__init}\index{mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}}


\subsection{Detailed Description}
Used to initialize tracers from a depth-\/ (or z$\ast$-\/) space file. \subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
logical function, public \hyperlink{namespacemom__tracer__z__init_aef33387149983391f1ea92bad7344360}{tracer\+\_\+z\+\_\+init} (tr, h, filename, tr\+\_\+name, G, US, missing\+\_\+val, land\+\_\+val)
\begin{DoxyCompactList}\small\item\em This function initializes a tracer by reading a Z-\/space file, returning .true. if this appears to have been successful, and false otherwise. \end{DoxyCompactList}\item 
subroutine, public \hyperlink{namespacemom__tracer__z__init_a2c439cbda792bb711a62094bbe779aec}{tracer\+\_\+z\+\_\+init\+\_\+array} (tr\+\_\+in, z\+\_\+edges, nk\+\_\+data, e, land\+\_\+fill, G, nlay, nlevs, eps\+\_\+z, tr)
\begin{DoxyCompactList}\small\item\em Layer model routine for remapping tracers from pseudo-\/z coordinates into layers defined by target interface positions. \end{DoxyCompactList}\item 
subroutine \hyperlink{namespacemom__tracer__z__init_a9f9a4d16dbd230d126b3f4d255071ad8}{read\+\_\+z\+\_\+edges} (filename, tr\+\_\+name, z\+\_\+edges, nz\+\_\+out, has\+\_\+edges, use\+\_\+missing, missing, scale)
\begin{DoxyCompactList}\small\item\em This subroutine reads the vertical coordinate data for a field from a Net\+C\+DF file. It also might read the missing value attribute for that same field. \end{DoxyCompactList}\item 
subroutine \hyperlink{namespacemom__tracer__z__init_a4cfb4818fc391a8d95ce737c06662d25}{find\+\_\+overlap} (e, Z\+\_\+top, Z\+\_\+bot, k\+\_\+max, k\+\_\+start, k\+\_\+top, k\+\_\+bot, wt, z1, z2)
\begin{DoxyCompactList}\small\item\em Determines the layers bounded by interfaces e that overlap with the depth range between Z\+\_\+top and Z\+\_\+bot, and the fractional weights of each layer. It also calculates the normalized relative depths of the range of each layer that overlaps that depth range. \end{DoxyCompactList}\item 
real function \hyperlink{namespacemom__tracer__z__init_a95177109110908c348d612ceb6923f9f}{find\+\_\+limited\+\_\+slope} (val, e, k)
\begin{DoxyCompactList}\small\item\em This subroutine determines a limited slope for val to be advected with a piecewise limited scheme. \end{DoxyCompactList}\item 
subroutine, public \hyperlink{namespacemom__tracer__z__init_ada3b3c684981ee5dabaaef4998589ca8}{determine\+\_\+temperature} (temp, salt, R\+\_\+tgt, p\+\_\+ref, niter, land\+\_\+fill, h, k\+\_\+start, G, US, eos, h\+\_\+massless)
\begin{DoxyCompactList}\small\item\em This subroutine determines the potential temperature and salinity that is consistent with the target density using provided initial guess. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__tracer__z__init_ada3b3c684981ee5dabaaef4998589ca8}\label{namespacemom__tracer__z__init_ada3b3c684981ee5dabaaef4998589ca8}} 
\index{mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}!determine\+\_\+temperature@{determine\+\_\+temperature}}
\index{determine\+\_\+temperature@{determine\+\_\+temperature}!mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}}
\subsubsection{\texorpdfstring{determine\+\_\+temperature()}{determine\_temperature()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+tracer\+\_\+z\+\_\+init\+::determine\+\_\+temperature (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{temp,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{salt,  }\item[{real, dimension( g \%ke), intent(in)}]{R\+\_\+tgt,  }\item[{real, intent(in)}]{p\+\_\+ref,  }\item[{integer, intent(in)}]{niter,  }\item[{real, intent(in)}]{land\+\_\+fill,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{integer, intent(in)}]{k\+\_\+start,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(eos\+\_\+type), pointer}]{eos,  }\item[{real, intent(in), optional}]{h\+\_\+massless }\end{DoxyParamCaption})}



This subroutine determines the potential temperature and salinity that is consistent with the target density using provided initial guess. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in,out}  & {\em temp} & potential temperature \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em salt} & salinity \mbox{[}P\+SU\mbox{]}\\
\hline
\mbox{\tt in}  & {\em r\+\_\+tgt} & desired potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em p\+\_\+ref} & reference pressure \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em niter} & maximum number of iterations\\
\hline
\mbox{\tt in}  & {\em k\+\_\+start} & starting index (i.\+e. below the buffer layer)\\
\hline
\mbox{\tt in}  & {\em land\+\_\+fill} & land fill value\\
\hline
\mbox{\tt in}  & {\em h} & layer thickness, used only to avoid working on\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em eos} & seawater equation of state control structure\\
\hline
\mbox{\tt in}  & {\em h\+\_\+massless} & A threshold below which a layer is determined to be massless \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 614 of file M\+O\+M\+\_\+tracer\+\_\+\+Z\+\_\+init.\+F90.


\begin{DoxyCode}
614   \textcolor{keywordtype}{type}(ocean\_grid\_type),         \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{    !< The ocean's grid structure}
615   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
616                                  \textcolor{keywordtype}{intent(inout)} :: temp\textcolor{comment}{ !< potential temperature [degC]}
617   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
618                                  \textcolor{keywordtype}{intent(inout)} :: salt\textcolor{comment}{ !< salinity [PSU]}
619   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZK\_(G))},      \textcolor{keywordtype}{intent(in)}    :: r\_tgt\textcolor{comment}{ !< desired potential density [R ~> kg m-3].}
620   \textcolor{keywordtype}{real},                          \textcolor{keywordtype}{intent(in)}    :: p\_ref\textcolor{comment}{ !< reference pressure [R L2 T-2 ~> Pa].}
621   \textcolor{keywordtype}{integer},                       \textcolor{keywordtype}{intent(in)}    :: niter\textcolor{comment}{ !< maximum number of iterations}
622   \textcolor{keywordtype}{integer},                       \textcolor{keywordtype}{intent(in)}    :: k\_start\textcolor{comment}{ !< starting index (i.e. below the buffer layer)}
623   \textcolor{keywordtype}{real},                          \textcolor{keywordtype}{intent(in)}    :: land\_fill\textcolor{comment}{ !< land fill value}
624   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
625                                  \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{   !< layer thickness, used only to avoid working on}
626 \textcolor{comment}{                                                      !! massless layers [H ~> m or kg m-2]}
627   \textcolor{keywordtype}{type}(unit\_scale\_type),         \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{  !< A dimensional unit scaling type}
628   \textcolor{keywordtype}{type}(eos\_type),                \textcolor{keywordtype}{pointer}       :: eos\textcolor{comment}{ !< seawater equation of state control structure}
629   \textcolor{keywordtype}{real},                \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: h\_massless\textcolor{comment}{ !< A threshold below which a layer is}
630 \textcolor{comment}{                                                      !! determined to be massless [H ~> m or kg m-2]}
631 
632   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{parameter} :: t\_max = 31.0, t\_min = -2.0
633   \textcolor{comment}{! Local variables (All of which need documentation!)}
634   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: &
635     t, s, dt, ds, &
636     rho, & \textcolor{comment}{! Layer densities [R ~> kg m-3]}
637     hin, & \textcolor{comment}{! Input layer thicknesses [H ~> m or kg m-2]}
638     drho\_dt, & \textcolor{comment}{! Partial derivative of density with temperature [R degC-1 ~> kg m-3 degC-1]}
639     drho\_ds    \textcolor{comment}{! Partial derivative of density with salinity [R ppt-1 ~> kg m-3 ppt-1]}
640   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: press \textcolor{comment}{! Reference pressures [R L2 T-2 ~> Pa]}
641   \textcolor{keywordtype}{real}    :: dt\_ds\_gauge  \textcolor{comment}{! The relative penalizing of temperature to salinity changes when}
642                           \textcolor{comment}{! minimizing property changes while correcting density [degC ppt-1].}
643   \textcolor{keywordtype}{real}    :: i\_denom      \textcolor{comment}{! The inverse of the magnitude squared of the density gradient in}
644                           \textcolor{comment}{! T-S space streched with dT\_dS\_gauge [ppt2 R-2 ~> ppt2 m6 kg-2]}
645   \textcolor{keywordtype}{logical} :: adjust\_salt, old\_fit
646   \textcolor{keywordtype}{real} :: s\_min, s\_max
647   \textcolor{keywordtype}{real} :: tol\_t    \textcolor{comment}{! The tolerance for temperature matches [degC]}
648   \textcolor{keywordtype}{real} :: tol\_s    \textcolor{comment}{! The tolerance for salinity matches [ppt]}
649   \textcolor{keywordtype}{real} :: tol\_rho  \textcolor{comment}{! The tolerance for density matches [R ~> kg m-3]}
650   \textcolor{keywordtype}{real} :: max\_t\_adj, max\_s\_adj
651   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: eosdom \textcolor{comment}{! The i-computational domain for the equation of state}
652   \textcolor{keywordtype}{integer} :: i, j, k, kz, is, ie, js, je, nz, itt
653 
654   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = g%ke
655 
656   \textcolor{comment}{! These hard coded parameters need to be set properly.}
657   s\_min = 0.5 ; s\_max = 65.0
658   max\_t\_adj = 1.0 ; max\_s\_adj = 0.5
659   tol\_t=1.e-4 ; tol\_s=1.e-4 ; tol\_rho = 1.e-4*us%kg\_m3\_to\_R
660   old\_fit = .true.   \textcolor{comment}{! reproduces siena behavior}
661 
662   \textcolor{comment}{! ### The whole determine\_temperature subroutine needs to be reexamined, both the algorithms}
663   \textcolor{comment}{!     and the extensive use of hard-coded dimensional parameters.}
664 
665   \textcolor{comment}{! We will switch to the newer method which simultaneously adjusts}
666   \textcolor{comment}{! temp and salt based on the ratio of the thermal and haline coefficients, once it is tested.}
667 
668   press(:) = p\_ref
669   eosdom(:) = eos\_domain(g%HI)
670 
671   \textcolor{keywordflow}{do} j=js,je
672     ds(:,:) = 0. \textcolor{comment}{! Needs to be zero everywhere since there is a maxval(abs(dS)) later...}
673     t(:,:) = temp(:,j,:)
674     s(:,:) = salt(:,j,:)
675     hin(:,:) = h(:,j,:)
676     dt(:,:) = 0.0
677     adjust\_salt = .true.
678     iter\_loop: \textcolor{keywordflow}{do} itt = 1,niter
679       \textcolor{keywordflow}{do} k=1,nz
680         \textcolor{keyword}{call }calculate\_density(t(:,k), s(:,k), press, rho(:,k), eos, eosdom )
681         \textcolor{keyword}{call }calculate\_density\_derivs(t(:,k), s(:,k), press, drho\_dt(:,k), drho\_ds(:,k), &
682                                       eos, eosdom )
683 \textcolor{keywordflow}{      enddo}
684       \textcolor{keywordflow}{do} k=k\_start,nz ; \textcolor{keywordflow}{do} i=is,ie
685 \textcolor{comment}{!       if (abs(rho(i,k)-R\_tgt(k))>tol\_rho .and. hin(i,k)>h\_massless .and. abs(T(i,k)-land\_fill) < epsln)
       then}
686         \textcolor{keywordflow}{if} (abs(rho(i,k)-r\_tgt(k))>tol\_rho) \textcolor{keywordflow}{then}
687           \textcolor{keywordflow}{if} (old\_fit) \textcolor{keywordflow}{then}
688             dt(i,k) = max(min((r\_tgt(k)-rho(i,k)) / drho\_dt(i,k), max\_t\_adj), -max\_t\_adj)
689             t(i,k) = max(min(t(i,k)+dt(i,k), t\_max), t\_min)
690           \textcolor{keywordflow}{else}
691             dt\_ds\_gauge = 10.0  \textcolor{comment}{! 10 degC is weighted equivalently to 1 ppt.}
692             i\_denom = 1.0 / (drho\_ds(i,k)**2 + dt\_ds\_gauge**2*drho\_dt(i,k)**2)
693             ds(i,k) = (r\_tgt(k)-rho(i,k)) * drho\_ds(i,k) * i\_denom
694             dt(i,k) = (r\_tgt(k)-rho(i,k)) * dt\_ds\_gauge**2*drho\_dt(i,k) * i\_denom
695 
696             t(i,k) = max(min(t(i,k)+dt(i,k), t\_max), t\_min)
697             s(i,k) = max(min(s(i,k)+ds(i,k), s\_max), s\_min)
698 \textcolor{keywordflow}{          endif}
699 \textcolor{keywordflow}{        endif}
700 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
701       \textcolor{keywordflow}{if} (maxval(abs(dt)) < tol\_t) \textcolor{keywordflow}{then}
702         adjust\_salt = .false.
703         \textcolor{keywordflow}{exit} iter\_loop
704 \textcolor{keywordflow}{      endif}
705 \textcolor{keywordflow}{    enddo} iter\_loop
706 
707     \textcolor{keywordflow}{if} (adjust\_salt .and. old\_fit) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} itt = 1,niter
708       \textcolor{keywordflow}{do} k=1,nz
709         \textcolor{keyword}{call }calculate\_density(t(:,k), s(:,k), press, rho(:,k), eos, eosdom )
710         \textcolor{keyword}{call }calculate\_density\_derivs(t(:,k), s(:,k), press, drho\_dt(:,k), drho\_ds(:,k), &
711                                       eos, eosdom )
712 \textcolor{keywordflow}{      enddo}
713       \textcolor{keywordflow}{do} k=k\_start,nz ; \textcolor{keywordflow}{do} i=is,ie
714 \textcolor{comment}{!       if (abs(rho(i,k)-R\_tgt(k))>tol\_rho .and. hin(i,k)>h\_massless .and. abs(T(i,k)-land\_fill) < epsln )
       then}
715         \textcolor{keywordflow}{if} (abs(rho(i,k)-r\_tgt(k)) > tol\_rho) \textcolor{keywordflow}{then}
716           ds(i,k) = max(min((r\_tgt(k)-rho(i,k)) / drho\_ds(i,k), max\_s\_adj), -max\_s\_adj)
717           s(i,k) = max(min(s(i,k)+ds(i,k), s\_max), s\_min)
718 \textcolor{keywordflow}{        endif}
719 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
720       \textcolor{keywordflow}{if} (maxval(abs(ds)) < tol\_s) \textcolor{keywordflow}{exit}
721 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}
722 
723     temp(:,j,:) = t(:,:)
724     salt(:,j,:) = s(:,:)
725 \textcolor{keywordflow}{  enddo}
726 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__tracer__z__init_a95177109110908c348d612ceb6923f9f}\label{namespacemom__tracer__z__init_a95177109110908c348d612ceb6923f9f}} 
\index{mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}!find\+\_\+limited\+\_\+slope@{find\+\_\+limited\+\_\+slope}}
\index{find\+\_\+limited\+\_\+slope@{find\+\_\+limited\+\_\+slope}!mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}}
\subsubsection{\texorpdfstring{find\+\_\+limited\+\_\+slope()}{find\_limited\_slope()}}
{\footnotesize\ttfamily real function mom\+\_\+tracer\+\_\+z\+\_\+init\+::find\+\_\+limited\+\_\+slope (\begin{DoxyParamCaption}\item[{real, dimension(\+:), intent(in)}]{val,  }\item[{real, dimension(\+:), intent(in)}]{e,  }\item[{integer, intent(in)}]{k }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine determines a limited slope for val to be advected with a piecewise limited scheme. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em val} & An column the values that are being interpolated.\\
\hline
\mbox{\tt in}  & {\em e} & A column\textquotesingle{}s interface heights \mbox{[}Z $\sim$$>$ m\mbox{]} or other units.\\
\hline
\mbox{\tt in}  & {\em k} & The layer whose slope is being determined.\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The normalized slope in the intracell distribution of val. 
\end{DoxyReturn}


Definition at line 580 of file M\+O\+M\+\_\+tracer\+\_\+\+Z\+\_\+init.\+F90.


\begin{DoxyCode}
580   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{intent(in)} :: val\textcolor{comment}{ !< An column the values that are being interpolated.}
581   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{intent(in)} :: e\textcolor{comment}{   !< A column's interface heights [Z ~> m] or other units.}
582   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{intent(in)} :: k\textcolor{comment}{   !< The layer whose slope is being determined.}
583   \textcolor{keywordtype}{real} :: slope\textcolor{comment}{ !< The normalized slope in the intracell distribution of val.}
584   \textcolor{comment}{! Local variables}
585   \textcolor{keywordtype}{real} :: amn, cmn
586   \textcolor{keywordtype}{real} :: d1, d2
587 
588   \textcolor{keywordflow}{if} ((val(k)-val(k-1)) * (val(k)-val(k+1)) >= 0.0) \textcolor{keywordflow}{then}
589     slope = 0.0 \textcolor{comment}{! ; curvature = 0.0}
590   \textcolor{keywordflow}{else}
591     d1 = 0.5*(e(k-1)-e(k+1)) ; d2 = 0.5*(e(k)-e(k+2))
592     \textcolor{keywordflow}{if} (d1*d2 > 0.0) \textcolor{keywordflow}{then}
593       slope = ((d1**2)*(val(k+1) - val(k)) + (d2**2)*(val(k) - val(k-1))) * &
594               (e(k) - e(k+1)) / (d1*d2*(d1+d2))
595       \textcolor{comment}{! slope = 0.5*(val(k+1) - val(k-1))}
596       \textcolor{comment}{! This is S.J. Lin's form of the PLM limiter.}
597       amn = min(abs(slope), 2.0*(max(val(k-1), val(k), val(k+1)) - val(k)))
598       cmn = 2.0*(val(k) - min(val(k-1), val(k), val(k+1)))
599       slope = sign(1.0, slope) * min(amn, cmn)
600 
601       \textcolor{comment}{! min(abs(slope), 2.0*(max(val(k-1),val(k),val(k+1)) - val(k)), &}
602       \textcolor{comment}{!                 2.0*(val(k) - min(val(k-1),val(k),val(k+1))))}
603       \textcolor{comment}{! curvature = 0.0}
604     \textcolor{keywordflow}{else}
605       slope = 0.0 \textcolor{comment}{! ; curvature = 0.0}
606 \textcolor{keywordflow}{    endif}
607 \textcolor{keywordflow}{  endif}
608 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__tracer__z__init_a4cfb4818fc391a8d95ce737c06662d25}\label{namespacemom__tracer__z__init_a4cfb4818fc391a8d95ce737c06662d25}} 
\index{mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}!find\+\_\+overlap@{find\+\_\+overlap}}
\index{find\+\_\+overlap@{find\+\_\+overlap}!mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}}
\subsubsection{\texorpdfstring{find\+\_\+overlap()}{find\_overlap()}}
{\footnotesize\ttfamily subroutine mom\+\_\+tracer\+\_\+z\+\_\+init\+::find\+\_\+overlap (\begin{DoxyParamCaption}\item[{real, dimension(\+:), intent(in)}]{e,  }\item[{real, intent(in)}]{Z\+\_\+top,  }\item[{real, intent(in)}]{Z\+\_\+bot,  }\item[{integer, intent(in)}]{k\+\_\+max,  }\item[{integer, intent(in)}]{k\+\_\+start,  }\item[{integer, intent(out)}]{k\+\_\+top,  }\item[{integer, intent(out)}]{k\+\_\+bot,  }\item[{real, dimension(\+:), intent(out)}]{wt,  }\item[{real, dimension(\+:), intent(out)}]{z1,  }\item[{real, dimension(\+:), intent(out)}]{z2 }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Determines the layers bounded by interfaces e that overlap with the depth range between Z\+\_\+top and Z\+\_\+bot, and the fractional weights of each layer. It also calculates the normalized relative depths of the range of each layer that overlaps that depth range. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em e} & Column interface heights, \mbox{[}Z $\sim$$>$ m\mbox{]} or other units.\\
\hline
\mbox{\tt in}  & {\em z\+\_\+top} & Top of range being mapped to, in the units of e \mbox{[}Z $\sim$$>$ m\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em z\+\_\+bot} & Bottom of range being mapped to, in the units of e \mbox{[}Z $\sim$$>$ m\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em k\+\_\+max} & Number of valid layers.\\
\hline
\mbox{\tt in}  & {\em k\+\_\+start} & Layer at which to start searching.\\
\hline
\mbox{\tt out}  & {\em k\+\_\+top} & Indices of top layers that overlap with the depth range.\\
\hline
\mbox{\tt out}  & {\em k\+\_\+bot} & Indices of bottom layers that overlap with the depth range.\\
\hline
\mbox{\tt out}  & {\em wt} & Relative weights of each layer from k\+\_\+top to k\+\_\+bot \mbox{[}nondim\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em z1} & Depth of the top limits of the part of a layer that contributes to a depth level, relative to the cell center and normalized by the cell thickness \mbox{[}nondim\mbox{]}. Note that -\/1/2 $<$= z1 $<$ z2 $<$= 1/2.\\
\hline
\mbox{\tt out}  & {\em z2} & Depths of the bottom limit of the part of a layer that contributes to a depth level, relative to the cell center and normalized by the cell thickness \mbox{[}nondim\mbox{]}. Note that -\/1/2 $<$= z1 $<$ z2 $<$= 1/2. \\
\hline
\end{DoxyParams}


Definition at line 518 of file M\+O\+M\+\_\+tracer\+\_\+\+Z\+\_\+init.\+F90.


\begin{DoxyCode}
518   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{intent(in)}  :: e\textcolor{comment}{      !< Column interface heights, [Z ~> m] or other units.}
519   \textcolor{keywordtype}{real},               \textcolor{keywordtype}{intent(in)}  :: z\_top\textcolor{comment}{  !< Top of range being mapped to, in the units of e [Z ~> m].}
520   \textcolor{keywordtype}{real},               \textcolor{keywordtype}{intent(in)}  :: z\_bot\textcolor{comment}{  !< Bottom of range being mapped to, in the units of e [Z ~> m].}
521   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{intent(in)}  :: k\_max\textcolor{comment}{  !< Number of valid layers.}
522   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{intent(in)}  :: k\_start\textcolor{comment}{ !< Layer at which to start searching.}
523   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{intent(out)} :: k\_top\textcolor{comment}{  !< Indices of top layers that overlap with the depth range.}
524   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{intent(out)} :: k\_bot\textcolor{comment}{  !< Indices of bottom layers that overlap with the depth range.}
525   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{intent(out)} :: wt\textcolor{comment}{     !< Relative weights of each layer from k\_top to k\_bot [nondim].}
526   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{intent(out)} :: z1\textcolor{comment}{     !< Depth of the top limits of the part of}
527 \textcolor{comment}{       !! a layer that contributes to a depth level, relative to the cell center and normalized}
528 \textcolor{comment}{       !! by the cell thickness [nondim].  Note that -1/2 <= z1 < z2 <= 1/2.}
529   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{intent(out)}   :: z2\textcolor{comment}{     !< Depths of the bottom limit of the part of}
530 \textcolor{comment}{       !! a layer that contributes to a depth level, relative to the cell center and normalized}
531 \textcolor{comment}{       !! by the cell thickness [nondim].  Note that -1/2 <= z1 < z2 <= 1/2.}
532   \textcolor{comment}{! Local variables}
533   \textcolor{keywordtype}{real}    :: ih, e\_c, tot\_wt, i\_totwt
534   \textcolor{keywordtype}{integer} :: k
535 
536   wt(:) = 0.0 ; z1(:) = 0.0 ; z2(:) = 0.0 ; k\_bot = k\_max
537 
538   \textcolor{keywordflow}{do} k=k\_start,k\_max ; \textcolor{keywordflow}{if} (e(k+1) < z\_top) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}
539   k\_top = k
540   \textcolor{keywordflow}{if} (k\_top > k\_max) \textcolor{keywordflow}{return}
541 
542   \textcolor{comment}{! Determine the fractional weights of each layer.}
543   \textcolor{comment}{! Note that by convention, e and Z\_int decrease with increasing k.}
544   \textcolor{keywordflow}{if} (e(k+1) <= z\_bot) \textcolor{keywordflow}{then}
545     wt(k) = 1.0 ; k\_bot = k
546     ih = 0.0 ; \textcolor{keywordflow}{if} (e(k) /= e(k+1)) ih = 1.0 / (e(k)-e(k+1))
547     e\_c = 0.5*(e(k)+e(k+1))
548     z1(k) = (e\_c - min(e(k), z\_top)) * ih
549     z2(k) = (e\_c - z\_bot) * ih
550   \textcolor{keywordflow}{else}
551     wt(k) = min(e(k),z\_top) - e(k+1) ; tot\_wt = wt(k) \textcolor{comment}{! These are always > 0.}
552     \textcolor{keywordflow}{if} (e(k) /= e(k+1)) \textcolor{keywordflow}{then}
553       z1(k) = (0.5*(e(k)+e(k+1)) - min(e(k), z\_top)) / (e(k)-e(k+1))
554     \textcolor{keywordflow}{else} ; z1(k) = -0.5 ;\textcolor{keywordflow}{ endif}
555     z2(k) = 0.5
556     k\_bot = k\_max
557     \textcolor{keywordflow}{do} k=k\_top+1,k\_max
558       \textcolor{keywordflow}{if} (e(k+1) <= z\_bot) \textcolor{keywordflow}{then}
559         k\_bot = k
560         wt(k) = e(k) - z\_bot ; z1(k) = -0.5
561         \textcolor{keywordflow}{if} (e(k) /= e(k+1)) \textcolor{keywordflow}{then}
562           z2(k) = (0.5*(e(k)+e(k+1)) - z\_bot) / (e(k)-e(k+1))
563         \textcolor{keywordflow}{else} ; z2(k) = 0.5 ;\textcolor{keywordflow}{ endif}
564       \textcolor{keywordflow}{else}
565         wt(k) = e(k) - e(k+1) ; z1(k) = -0.5 ; z2(k) = 0.5
566 \textcolor{keywordflow}{      endif}
567       tot\_wt = tot\_wt + wt(k) \textcolor{comment}{! wt(k) is always > 0.}
568       \textcolor{keywordflow}{if} (k>=k\_bot) \textcolor{keywordflow}{exit}
569 \textcolor{keywordflow}{    enddo}
570 
571     i\_totwt = 0.0 ; \textcolor{keywordflow}{if} (tot\_wt > 0.0) i\_totwt = 1.0 / tot\_wt
572     \textcolor{keywordflow}{do} k=k\_top,k\_bot ; wt(k) = i\_totwt*wt(k) ;\textcolor{keywordflow}{ enddo}
573 \textcolor{keywordflow}{  endif}
574 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__tracer__z__init_a9f9a4d16dbd230d126b3f4d255071ad8}\label{namespacemom__tracer__z__init_a9f9a4d16dbd230d126b3f4d255071ad8}} 
\index{mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}!read\+\_\+z\+\_\+edges@{read\+\_\+z\+\_\+edges}}
\index{read\+\_\+z\+\_\+edges@{read\+\_\+z\+\_\+edges}!mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}}
\subsubsection{\texorpdfstring{read\+\_\+z\+\_\+edges()}{read\_z\_edges()}}
{\footnotesize\ttfamily subroutine mom\+\_\+tracer\+\_\+z\+\_\+init\+::read\+\_\+z\+\_\+edges (\begin{DoxyParamCaption}\item[{character(len=$\ast$), intent(in)}]{filename,  }\item[{character(len=$\ast$), intent(in)}]{tr\+\_\+name,  }\item[{real, dimension(\+:), intent(out), allocatable}]{z\+\_\+edges,  }\item[{integer, intent(out)}]{nz\+\_\+out,  }\item[{logical, intent(out)}]{has\+\_\+edges,  }\item[{logical, intent(inout)}]{use\+\_\+missing,  }\item[{real, intent(inout)}]{missing,  }\item[{real, intent(in)}]{scale }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine reads the vertical coordinate data for a field from a Net\+C\+DF file. It also might read the missing value attribute for that same field. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em filename} & The name of the file to read from.\\
\hline
\mbox{\tt in}  & {\em tr\+\_\+name} & The name of the tracer in the file.\\
\hline
\mbox{\tt out}  & {\em z\+\_\+edges} & The depths of the vertical edges of the tracer array\\
\hline
\mbox{\tt out}  & {\em nz\+\_\+out} & The number of vertical layers in the tracer array\\
\hline
\mbox{\tt out}  & {\em has\+\_\+edges} & If true the values in z\+\_\+edges are the edges of the tracer cells, otherwise they are the cell centers\\
\hline
\mbox{\tt in,out}  & {\em use\+\_\+missing} & If false on input, see whether the tracer has a missing value, and if so return true\\
\hline
\mbox{\tt in,out}  & {\em missing} & The missing value, if one has been found\\
\hline
\mbox{\tt in}  & {\em scale} & A scaling factor for z\+\_\+edges into new units. \\
\hline
\end{DoxyParams}


Definition at line 385 of file M\+O\+M\+\_\+tracer\+\_\+\+Z\+\_\+init.\+F90.


\begin{DoxyCode}
385   \textcolor{keywordtype}{character(len=*)}, \textcolor{keywordtype}{intent(in)}    :: filename\textcolor{comment}{ !< The name of the file to read from.}
386   \textcolor{keywordtype}{character(len=*)}, \textcolor{keywordtype}{intent(in)}    :: tr\_name\textcolor{comment}{ !< The name of the tracer in the file.}
387   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{allocatable}, &
388                     \textcolor{keywordtype}{intent(out)}   :: z\_edges\textcolor{comment}{ !< The depths of the vertical edges of the tracer array}
389   \textcolor{keywordtype}{integer},          \textcolor{keywordtype}{intent(out)}   :: nz\_out\textcolor{comment}{  !< The number of vertical layers in the tracer array}
390   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{intent(out)}   :: has\_edges\textcolor{comment}{ !< If true the values in z\_edges are the edges of the}
391 \textcolor{comment}{                                             !! tracer cells, otherwise they are the cell centers}
392   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{intent(inout)} :: use\_missing\textcolor{comment}{ !< If false on input, see whether the tracer has a}
393 \textcolor{comment}{                                             !! missing value, and if so return true}
394   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(inout)} :: missing\textcolor{comment}{ !< The missing value, if one has been found}
395   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)}    :: scale\textcolor{comment}{   !< A scaling factor for z\_edges into new units.}
396 
397   \textcolor{comment}{!   This subroutine reads the vertical coordinate data for a field from a}
398   \textcolor{comment}{! NetCDF file.  It also might read the missing value attribute for that same field.}
399   \textcolor{keywordtype}{character(len=32)} :: mdl
400   \textcolor{keywordtype}{character(len=120)} :: dim\_name, edge\_name, tr\_msg, dim\_msg
401   \textcolor{keywordtype}{logical} :: monotonic
402   \textcolor{keywordtype}{integer} :: ncid, status, intid, tr\_id, layid, k
403   \textcolor{keywordtype}{integer} :: nz\_edge, ndim, tr\_dim\_ids(nf90\_max\_var\_dims)
404 
405   mdl = \textcolor{stringliteral}{"MOM\_tracer\_Z\_init read\_Z\_edges: "}
406   tr\_msg = trim(tr\_name)//\textcolor{stringliteral}{" in "}//trim(filename)
407 
408   status = nf90\_open(filename, nf90\_nowrite, ncid)
409   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
410     \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" Difficulties opening "}//trim(filename)//&
411         \textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
412     nz\_out = -1 ; \textcolor{keywordflow}{return}
413 \textcolor{keywordflow}{  endif}
414 
415   status = nf90\_inq\_varid(ncid, tr\_name, tr\_id)
416   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
417     \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" Difficulties finding variable "}//&
418         trim(tr\_msg)//\textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
419     nz\_out = -1 ; status = nf90\_close(ncid) ; \textcolor{keywordflow}{return}
420 \textcolor{keywordflow}{  endif}
421   status = nf90\_inquire\_variable(ncid, tr\_id, ndims=ndim, dimids=tr\_dim\_ids)
422   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
423     \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" cannot inquire about "}//trim(tr\_msg))
424   \textcolor{keywordflow}{elseif} ((ndim < 3) .or. (ndim > 4)) \textcolor{keywordflow}{then}
425     \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" "}//trim(tr\_msg)//&
426          \textcolor{stringliteral}{" has too many or too few dimensions."})
427     nz\_out = -1 ; status = nf90\_close(ncid) ; \textcolor{keywordflow}{return}
428 \textcolor{keywordflow}{  endif}
429 
430   \textcolor{keywordflow}{if} (.not.use\_missing) \textcolor{keywordflow}{then}
431     \textcolor{comment}{! Try to find the missing value from the dataset.}
432     status = nf90\_get\_att(ncid, tr\_id, \textcolor{stringliteral}{"missing\_value"}, missing)
433     \textcolor{keywordflow}{if} (status /= nf90\_noerr) use\_missing = .true.
434 \textcolor{keywordflow}{  endif}
435 
436   \textcolor{comment}{! Get the axis name and length.}
437   status = nf90\_inquire\_dimension(ncid, tr\_dim\_ids(3), dim\_name, len=nz\_out)
438   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
439     \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" cannot inquire about dimension(3) of "}//&
440                     trim(tr\_msg))
441 \textcolor{keywordflow}{  endif}
442 
443   dim\_msg = trim(dim\_name)//\textcolor{stringliteral}{" in "}//trim(filename)
444   status = nf90\_inq\_varid(ncid, dim\_name, layid)
445   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
446     \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" Difficulties finding variable "}//&
447         trim(dim\_msg)//\textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
448     nz\_out = -1 ; status = nf90\_close(ncid) ; \textcolor{keywordflow}{return}
449 \textcolor{keywordflow}{  endif}
450   \textcolor{comment}{! Find out if the Z-axis has an edges attribute}
451   status = nf90\_get\_att(ncid, layid, \textcolor{stringliteral}{"edges"}, edge\_name)
452   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
453     \textcolor{keyword}{call }mom\_mesg(mdl//\textcolor{stringliteral}{" "}//trim(dim\_msg)//&
454          \textcolor{stringliteral}{" has no readable edges attribute - "}//trim(nf90\_strerror(status)))
455     has\_edges = .false.
456   \textcolor{keywordflow}{else}
457     has\_edges = .true.
458     status = nf90\_inq\_varid(ncid, edge\_name, intid)
459     \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
460       \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" Difficulties finding edge variable "}//&
461           trim(edge\_name)//\textcolor{stringliteral}{" in "}//trim(filename)//\textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
462       has\_edges = .false.
463 \textcolor{keywordflow}{    endif}
464 \textcolor{keywordflow}{  endif}
465 
466   nz\_edge = nz\_out ; \textcolor{keywordflow}{if} (has\_edges) nz\_edge = nz\_out+1
467   \textcolor{keyword}{allocate}(z\_edges(nz\_edge)) ; z\_edges(:) = 0.0
468 
469   \textcolor{keywordflow}{if} (nz\_out < 1) \textcolor{keywordflow}{return}
470 
471   \textcolor{comment}{! Read the right variable.}
472   \textcolor{keywordflow}{if} (has\_edges) \textcolor{keywordflow}{then}
473     dim\_msg = trim(edge\_name)//\textcolor{stringliteral}{" in "}//trim(filename)
474     status = nf90\_get\_var(ncid, intid, z\_edges)
475     \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
476       \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" Difficulties reading variable "}//&
477           trim(dim\_msg)//\textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
478       nz\_out = -1 ; status = nf90\_close(ncid) ; \textcolor{keywordflow}{return}
479 \textcolor{keywordflow}{    endif}
480   \textcolor{keywordflow}{else}
481     status = nf90\_get\_var(ncid, layid, z\_edges)
482     \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keywordflow}{then}
483       \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" Difficulties reading variable "}//&
484           trim(dim\_msg)//\textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
485       nz\_out = -1 ; status = nf90\_close(ncid) ; \textcolor{keywordflow}{return}
486 \textcolor{keywordflow}{    endif}
487 \textcolor{keywordflow}{  endif}
488 
489   status = nf90\_close(ncid)
490   \textcolor{keywordflow}{if} (status /= nf90\_noerr) \textcolor{keyword}{call }mom\_error(warning, mdl// &
491     \textcolor{stringliteral}{" Difficulties closing "}//trim(filename)//\textcolor{stringliteral}{" - "}//trim(nf90\_strerror(status)))
492 
493   \textcolor{comment}{! z\_edges should be montonically decreasing with our sign convention.}
494   \textcolor{comment}{! Change the sign sign convention if it looks like z\_edges is increasing.}
495   \textcolor{keywordflow}{if} (z\_edges(1) < z\_edges(2)) \textcolor{keywordflow}{then}
496     \textcolor{keywordflow}{do} k=1,nz\_edge ; z\_edges(k) = -z\_edges(k) ;\textcolor{keywordflow}{ enddo}
497 \textcolor{keywordflow}{  endif}
498   \textcolor{comment}{! Check that z\_edges is now monotonically decreasing.}
499   monotonic = .true.
500   \textcolor{keywordflow}{do} k=2,nz\_edge ; \textcolor{keywordflow}{if} (z\_edges(k) >= z\_edges(k-1)) monotonic = .false. ;\textcolor{keywordflow}{ enddo}
501   \textcolor{keywordflow}{if} (.not.monotonic) &
502     \textcolor{keyword}{call }mom\_error(warning,mdl//\textcolor{stringliteral}{" "}//trim(dim\_msg)//\textcolor{stringliteral}{" is not monotonic."})
503 
504   \textcolor{keywordflow}{if} (scale /= 1.0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz\_edge ; z\_edges(k) = scale*z\_edges(k) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
505 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__tracer__z__init_aef33387149983391f1ea92bad7344360}\label{namespacemom__tracer__z__init_aef33387149983391f1ea92bad7344360}} 
\index{mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}!tracer\+\_\+z\+\_\+init@{tracer\+\_\+z\+\_\+init}}
\index{tracer\+\_\+z\+\_\+init@{tracer\+\_\+z\+\_\+init}!mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}}
\subsubsection{\texorpdfstring{tracer\+\_\+z\+\_\+init()}{tracer\_z\_init()}}
{\footnotesize\ttfamily logical function, public mom\+\_\+tracer\+\_\+z\+\_\+init\+::tracer\+\_\+z\+\_\+init (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(out)}]{tr,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{character(len=$\ast$), intent(in)}]{filename,  }\item[{character(len=$\ast$), intent(in)}]{tr\+\_\+name,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, intent(in), optional}]{missing\+\_\+val,  }\item[{real, intent(in), optional}]{land\+\_\+val }\end{DoxyParamCaption})}



This function initializes a tracer by reading a Z-\/space file, returning .true. if this appears to have been successful, and false otherwise. 

\begin{DoxyReturn}{Returns}
A return code indicating if the initialization has been successful
\end{DoxyReturn}

\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 out}  & {\em tr} & The tracer to initialize\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em filename} & The name of the file to read from\\
\hline
\mbox{\tt in}  & {\em tr\+\_\+name} & The name of the tracer in the file\\
\hline
\mbox{\tt in}  & {\em missing\+\_\+val} & The missing value for the tracer\\
\hline
\mbox{\tt in}  & {\em land\+\_\+val} & A value to use to fill in land points \\
\hline
\end{DoxyParams}


Definition at line 31 of file M\+O\+M\+\_\+tracer\+\_\+\+Z\+\_\+init.\+F90.


\begin{DoxyCode}
31   \textcolor{keywordtype}{logical} :: tracer\_z\_init\textcolor{comment}{ !< A return code indicating if the initialization has been successful}
32   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{    !< The ocean's grid structure}
33   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type}
34   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
35                          \textcolor{keywordtype}{intent(out)}   :: tr\textcolor{comment}{   !< The tracer to initialize}
36   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
37                          \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H ~> m or kg m-2]}
38   \textcolor{keywordtype}{character(len=*)},      \textcolor{keywordtype}{intent(in)}    :: filename\textcolor{comment}{ !< The name of the file to read from}
39   \textcolor{keywordtype}{character(len=*)},      \textcolor{keywordtype}{intent(in)}    :: tr\_name\textcolor{comment}{ !< The name of the tracer in the file}
40 \textcolor{comment}{! type(param\_file\_type), intent(in)    :: param\_file !< A structure to parse for run-time parameters}
41   \textcolor{keywordtype}{real},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: missing\_val\textcolor{comment}{ !< The missing value for the tracer}
42   \textcolor{keywordtype}{real},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: land\_val\textcolor{comment}{ !< A value to use to fill in land points}
43 
44   \textcolor{comment}{!   This function initializes a tracer by reading a Z-space file, returning true if this}
45   \textcolor{comment}{! appears to have been successful, and false otherwise.}
46 \textcolor{comment}{!}
47   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{save} :: init\_calls = 0
48 \textcolor{comment}{! This include declares and sets the variable "version".}
49 \textcolor{preprocessor}{#include "version\_variable.h"}
50 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_tracer\_Z\_init"} \textcolor{comment}{! This module's name.}
51   \textcolor{keywordtype}{character(len=256)} :: mesg    \textcolor{comment}{! Message for error messages.}
52 
53   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:,:)} :: &
54     tr\_in   \textcolor{comment}{! The z-space array of tracer concentrations that is read in.}
55   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:)} :: &
56     z\_edges, &  \textcolor{comment}{! The depths of the cell edges or cell centers (depending on}
57                 \textcolor{comment}{! the value of has\_edges) in the input z* data [Z ~> m].}
58     tr\_1d, &    \textcolor{comment}{! A copy of the input tracer concentrations in a column.}
59     wt, &   \textcolor{comment}{! The fractional weight for each layer in the range between}
60             \textcolor{comment}{! k\_top and k\_bot, nondim.}
61     z1, &   \textcolor{comment}{! z1 and z2 are the depths of the top and bottom limits of the part}
62     z2      \textcolor{comment}{! of a z-cell that contributes to a layer, relative to the cell}
63             \textcolor{comment}{! center and normalized by the cell thickness, nondim.}
64             \textcolor{comment}{! Note that -1/2 <= z1 <= z2 <= 1/2.}
65   \textcolor{keywordtype}{real}    :: e(szk\_(g)+1)  \textcolor{comment}{! The z-star interface heights [Z ~> m].}
66   \textcolor{keywordtype}{real}    :: landval    \textcolor{comment}{! The tracer value to use in land points.}
67   \textcolor{keywordtype}{real}    :: sl\_tr      \textcolor{comment}{! The normalized slope of the tracer}
68                         \textcolor{comment}{! within the cell, in tracer units.}
69   \textcolor{keywordtype}{real}    :: htot(szi\_(g)) \textcolor{comment}{! The vertical sum of h [H ~> m or kg m-2].}
70   \textcolor{keywordtype}{real}    :: dilate     \textcolor{comment}{! The amount by which the thicknesses are dilated to}
71                         \textcolor{comment}{! create a z-star coordinate, nondim or in m3 kg-1.}
72   \textcolor{keywordtype}{real}    :: missing    \textcolor{comment}{! The missing value for the tracer.}
73 
74   \textcolor{keywordtype}{logical} :: has\_edges, use\_missing, zero\_surface
75   \textcolor{keywordtype}{character(len=80)} :: loc\_msg
76   \textcolor{keywordtype}{integer} :: k\_top, k\_bot, k\_bot\_prev, k\_start
77   \textcolor{keywordtype}{integer} :: i, j, k, kz, is, ie, js, je, nz, nz\_in
78   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = g%ke
79 
80   landval = 0.0 ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(land\_val)) landval = land\_val
81 
82   zero\_surface = .false. \textcolor{comment}{! Make this false for errors to be fatal.}
83 
84   use\_missing = .false.
85   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(missing\_val)) \textcolor{keywordflow}{then}
86     use\_missing = .true. ; missing = missing\_val
87 \textcolor{keywordflow}{  endif}
88 
89   \textcolor{comment}{! Find out the number of input levels and read the depth of the edges,}
90   \textcolor{comment}{! also modifying their sign convention to be monotonically decreasing.}
91   \textcolor{keyword}{call }read\_z\_edges(filename, tr\_name, z\_edges, nz\_in, has\_edges, use\_missing, &
92                     missing, scale=us%m\_to\_Z)
93   \textcolor{keywordflow}{if} (nz\_in < 1) \textcolor{keywordflow}{then}
94     tracer\_z\_init = .false.
95     \textcolor{keywordflow}{return}
96 \textcolor{keywordflow}{  endif}
97 
98   \textcolor{keyword}{allocate}(tr\_in(g%isd:g%ied,g%jsd:g%jed,nz\_in)) ; tr\_in(:,:,:) = 0.0
99   \textcolor{keyword}{allocate}(tr\_1d(nz\_in)) ; tr\_1d(:) = 0.0
100   \textcolor{keyword}{call }mom\_read\_data(filename, tr\_name, tr\_in(:,:,:), g%Domain)
101 
102   \textcolor{comment}{! Fill missing values from above?  Use a "close" test to avoid problems}
103   \textcolor{comment}{! from type-conversion rounoff.}
104   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(missing\_val)) \textcolor{keywordflow}{then}
105     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
106       \textcolor{keywordflow}{if} (g%mask2dT(i,j) == 0.0) \textcolor{keywordflow}{then}
107         tr\_in(i,j,1) = landval
108       \textcolor{keywordflow}{elseif} (abs(tr\_in(i,j,1) - missing\_val) <= 1e-6*abs(missing\_val)) \textcolor{keywordflow}{then}
109         \textcolor{keyword}{write}(loc\_msg,\textcolor{stringliteral}{'(f7.2," N ",f7.2," E")'}) g%geoLatT(i,j), g%geoLonT(i,j)
110         \textcolor{keywordflow}{if} (zero\_surface) \textcolor{keywordflow}{then}
111           \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"tracer\_Z\_init: Missing value of "}// &
112                 trim(tr\_name)//\textcolor{stringliteral}{" found in an ocean point at "}//trim(loc\_msg)// &
113                 \textcolor{stringliteral}{" in "}//trim(filename) )
114           tr\_in(i,j,1) = 0.0
115         \textcolor{keywordflow}{else}
116           \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"tracer\_Z\_init: Missing value of "}// &
117                 trim(tr\_name)//\textcolor{stringliteral}{" found in an ocean point at "}//trim(loc\_msg)// &
118                 \textcolor{stringliteral}{" in "}//trim(filename) )
119 \textcolor{keywordflow}{        endif}
120 \textcolor{keywordflow}{      endif}
121 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
122     \textcolor{keywordflow}{do} k=2,nz\_in ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
123       \textcolor{keywordflow}{if} (abs(tr\_in(i,j,k) - missing\_val) <= 1e-6*abs(missing\_val)) &
124         tr\_in(i,j,k) = tr\_in(i,j,k-1)
125 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
126 \textcolor{keywordflow}{  endif}
127 
128   \textcolor{keyword}{allocate}(wt(nz\_in+1)) ; \textcolor{keyword}{allocate}(z1(nz\_in+1)) ; \textcolor{keyword}{allocate}(z2(nz\_in+1))
129 
130   \textcolor{comment}{! This is a placeholder, and will be replaced with our full vertical}
131   \textcolor{comment}{! interpolation machinery when it is in place.}
132   \textcolor{keywordflow}{if} (has\_edges) \textcolor{keywordflow}{then}
133     \textcolor{keywordflow}{do} j=js,je
134       \textcolor{keywordflow}{do} i=is,ie ; htot(i) = 0.0 ;\textcolor{keywordflow}{ enddo}
135       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; htot(i) = htot(i) + h(i,j,k) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
136 
137       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (g%mask2dT(i,j)*htot(i) > 0.0) \textcolor{keywordflow}{then}
138         \textcolor{comment}{! Determine the z* heights of the model interfaces.}
139         dilate = (g%bathyT(i,j) - 0.0) / htot(i)
140         e(nz+1) = -g%bathyT(i,j)
141         \textcolor{keywordflow}{do} k=nz,1,-1 ; e(k) = e(k+1) + dilate * h(i,j,k) ;\textcolor{keywordflow}{ enddo}
142 
143         \textcolor{comment}{! Create a single-column copy of tr\_in.  Efficiency is not an issue here.}
144         \textcolor{keywordflow}{do} k=1,nz\_in ; tr\_1d(k) = tr\_in(i,j,k) ;\textcolor{keywordflow}{ enddo}
145         k\_bot = 1 ; k\_bot\_prev = -1
146         \textcolor{keywordflow}{do} k=1,nz
147           \textcolor{keywordflow}{if} (e(k+1) > z\_edges(1)) \textcolor{keywordflow}{then}
148             tr(i,j,k) = tr\_1d(1)
149           \textcolor{keywordflow}{elseif} (e(k) < z\_edges(nz\_in+1)) \textcolor{keywordflow}{then}
150             tr(i,j,k) = tr\_1d(nz\_in)
151           \textcolor{keywordflow}{else}
152             k\_start = k\_bot \textcolor{comment}{! The starting point for this search}
153             \textcolor{keyword}{call }find\_overlap(z\_edges, e(k), e(k+1), nz\_in, &
154                               k\_start, k\_top, k\_bot, wt, z1, z2)
155             kz = k\_top
156             \textcolor{keywordflow}{if} (kz /= k\_bot\_prev) \textcolor{keywordflow}{then}
157               \textcolor{comment}{! Calculate the intra-cell profile.}
158               sl\_tr = 0.0 \textcolor{comment}{! ; cur\_tr = 0.0}
159               \textcolor{keywordflow}{if} ((kz < nz\_in) .and. (kz > 1)) &
160                 sl\_tr = find\_limited\_slope(tr\_1d, z\_edges, kz)
161 \textcolor{keywordflow}{            endif}
162             \textcolor{comment}{! This is the piecewise linear form.}
163             tr(i,j,k) = wt(kz) * (tr\_1d(kz) + 0.5*sl\_tr*(z2(kz) + z1(kz)))
164             \textcolor{comment}{! For the piecewise parabolic form add the following...}
165             \textcolor{comment}{!     + C1\_3*cur\_tr*(z2(kz)**2 + z2(kz)*z1(kz) + z1(kz)**2))}
166             \textcolor{keywordflow}{do} kz=k\_top+1,k\_bot-1
167               tr(i,j,k) = tr(i,j,k) + wt(kz)*tr\_1d(kz)
168 \textcolor{keywordflow}{            enddo}
169             \textcolor{keywordflow}{if} (k\_bot > k\_top) \textcolor{keywordflow}{then}
170               kz = k\_bot
171               \textcolor{comment}{! Calculate the intra-cell profile.}
172               sl\_tr = 0.0 \textcolor{comment}{! ; cur\_tr = 0.0}
173               \textcolor{keywordflow}{if} ((kz < nz\_in) .and. (kz > 1)) &
174                 sl\_tr = find\_limited\_slope(tr\_1d, z\_edges, kz)
175               \textcolor{comment}{! This is the piecewise linear form.}
176               tr(i,j,k) = tr(i,j,k) + wt(kz) * &
177                   (tr\_1d(kz) + 0.5*sl\_tr*(z2(kz) + z1(kz)))
178               \textcolor{comment}{! For the piecewise parabolic form add the following...}
179               \textcolor{comment}{!     + C1\_3*cur\_tr*(z2(kz)**2 + z2(kz)*z1(kz) + z1(kz)**2))}
180 \textcolor{keywordflow}{            endif}
181             k\_bot\_prev = k\_bot
182 
183             \textcolor{comment}{!   Now handle the unlikely case where the layer partially extends}
184             \textcolor{comment}{! past the valid range of the input data by extrapolating using}
185             \textcolor{comment}{! the top or bottom value.}
186             \textcolor{keywordflow}{if} ((e(k) > z\_edges(1)) .and. (z\_edges(nz\_in+1) > e(k+1))) \textcolor{keywordflow}{then}
187               tr(i,j,k) = (((e(k) - z\_edges(1)) * tr\_1d(1) + &
188                            (z\_edges(1) - z\_edges(nz\_in)) * tr(i,j,k)) + &
189                            (z\_edges(nz\_in+1) - e(k+1)) * tr\_1d(nz\_in)) / &
190                           (e(k) - e(k+1))
191             \textcolor{keywordflow}{elseif} (e(k) > z\_edges(1)) \textcolor{keywordflow}{then}
192               tr(i,j,k) = ((e(k) - z\_edges(1)) * tr\_1d(1) + &
193                            (z\_edges(1) - e(k+1)) * tr(i,j,k)) / &
194                           (e(k) - e(k+1))
195             \textcolor{keywordflow}{elseif} (z\_edges(nz\_in) > e(k+1)) \textcolor{keywordflow}{then}
196               tr(i,j,k) = ((e(k) - z\_edges(nz\_in+1)) * tr(i,j,k) + &
197                            (z\_edges(nz\_in+1) - e(k+1)) * tr\_1d(nz\_in)) / &
198                           (e(k) - e(k+1))
199 \textcolor{keywordflow}{            endif}
200 \textcolor{keywordflow}{          endif}
201 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! k-loop}
202       \textcolor{keywordflow}{else}
203         \textcolor{keywordflow}{do} k=1,nz ; tr(i,j,k) = landval ;\textcolor{keywordflow}{ enddo}
204 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-loop}
205 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! j-loop}
206   \textcolor{keywordflow}{else}
207     \textcolor{comment}{! Without edge values, integrate a linear interpolation between cell centers.}
208     \textcolor{keywordflow}{do} j=js,je
209       \textcolor{keywordflow}{do} i=is,ie ; htot(i) = 0.0 ;\textcolor{keywordflow}{ enddo}
210       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; htot(i) = htot(i) + h(i,j,k) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
211 
212       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (g%mask2dT(i,j)*htot(i) > 0.0) \textcolor{keywordflow}{then}
213         \textcolor{comment}{! Determine the z* heights of the model interfaces.}
214         dilate = (g%bathyT(i,j) - 0.0) / htot(i)
215         e(nz+1) = -g%bathyT(i,j)
216         \textcolor{keywordflow}{do} k=nz,1,-1 ; e(k) = e(k+1) + dilate * h(i,j,k) ;\textcolor{keywordflow}{ enddo}
217 
218         \textcolor{comment}{! Create a single-column copy of tr\_in.  Efficiency is not an issue here.}
219         \textcolor{keywordflow}{do} k=1,nz\_in ; tr\_1d(k) = tr\_in(i,j,k) ;\textcolor{keywordflow}{ enddo}
220         k\_bot = 1
221         \textcolor{keywordflow}{do} k=1,nz
222           \textcolor{keywordflow}{if} (e(k+1) > z\_edges(1)) \textcolor{keywordflow}{then}
223             tr(i,j,k) = tr\_1d(1)
224           \textcolor{keywordflow}{elseif} (z\_edges(nz\_in) > e(k)) \textcolor{keywordflow}{then}
225             tr(i,j,k) = tr\_1d(nz\_in)
226           \textcolor{keywordflow}{else}
227             k\_start = k\_bot \textcolor{comment}{! The starting point for this search}
228             \textcolor{keyword}{call }find\_overlap(z\_edges, e(k), e(k+1), nz\_in-1, &
229                               k\_start, k\_top, k\_bot, wt, z1, z2)
230 
231             kz = k\_top
232             \textcolor{keywordflow}{if} (k\_top < nz\_in) \textcolor{keywordflow}{then}
233               tr(i,j,k) = wt(kz)*0.5*((tr\_1d(kz) + tr\_1d(kz+1)) + &
234                                       (tr\_1d(kz+1) - tr\_1d(kz))*(z2(kz)+z1(kz)))
235             \textcolor{keywordflow}{else}
236               tr(i,j,k) = wt(kz)*tr\_1d(nz\_in)
237 \textcolor{keywordflow}{            endif}
238             \textcolor{keywordflow}{do} kz=k\_top+1,k\_bot-1
239               tr(i,j,k) = tr(i,j,k) + wt(kz)*0.5*(tr\_1d(kz) + tr\_1d(kz+1))
240 \textcolor{keywordflow}{            enddo}
241             \textcolor{keywordflow}{if} (k\_bot > k\_top) \textcolor{keywordflow}{then}
242               kz = k\_bot
243               tr(i,j,k) = tr(i,j,k) + wt(kz)*0.5*((tr\_1d(kz) + tr\_1d(kz+1)) + &
244                                         (tr\_1d(kz+1) - tr\_1d(kz))*(z2(kz)+z1(kz)))
245 \textcolor{keywordflow}{            endif}
246 
247             \textcolor{comment}{! Now handle the case where the layer partially extends past}
248             \textcolor{comment}{! the valid range of the input data.}
249             \textcolor{keywordflow}{if} ((e(k) > z\_edges(1)) .and. (z\_edges(nz\_in) > e(k+1))) \textcolor{keywordflow}{then}
250               tr(i,j,k) = (((e(k) - z\_edges(1)) * tr\_1d(1) + &
251                            (z\_edges(1) - z\_edges(nz\_in)) * tr(i,j,k)) + &
252                            (z\_edges(nz\_in) - e(k+1)) * tr\_1d(nz\_in)) / &
253                           (e(k) - e(k+1))
254             \textcolor{keywordflow}{elseif} (e(k) > z\_edges(1)) \textcolor{keywordflow}{then}
255               tr(i,j,k) = ((e(k) - z\_edges(1)) * tr\_1d(1) + &
256                            (z\_edges(1) - e(k+1)) * tr(i,j,k)) / &
257                           (e(k) - e(k+1))
258             \textcolor{keywordflow}{elseif} (z\_edges(nz\_in) > e(k+1)) \textcolor{keywordflow}{then}
259               tr(i,j,k) = ((e(k) - z\_edges(nz\_in)) * tr(i,j,k) + &
260                            (z\_edges(nz\_in) - e(k+1)) * tr\_1d(nz\_in)) / &
261                           (e(k) - e(k+1))
262 \textcolor{keywordflow}{            endif}
263 \textcolor{keywordflow}{          endif}
264 \textcolor{keywordflow}{        enddo}
265       \textcolor{keywordflow}{else}
266         \textcolor{keywordflow}{do} k=1,nz ; tr(i,j,k) = landval ;\textcolor{keywordflow}{ enddo}
267 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-loop}
268 \textcolor{keywordflow}{    enddo}  \textcolor{comment}{! j-loop}
269 \textcolor{keywordflow}{  endif}
270 
271   \textcolor{keyword}{deallocate}(tr\_in) ; \textcolor{keyword}{deallocate}(tr\_1d) ; \textcolor{keyword}{deallocate}(z\_edges)
272   \textcolor{keyword}{deallocate}(wt) ; \textcolor{keyword}{deallocate}(z1) ; \textcolor{keyword}{deallocate}(z2)
273 
274   tracer\_z\_init = .true.
275 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__tracer__z__init_a2c439cbda792bb711a62094bbe779aec}\label{namespacemom__tracer__z__init_a2c439cbda792bb711a62094bbe779aec}} 
\index{mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}!tracer\+\_\+z\+\_\+init\+\_\+array@{tracer\+\_\+z\+\_\+init\+\_\+array}}
\index{tracer\+\_\+z\+\_\+init\+\_\+array@{tracer\+\_\+z\+\_\+init\+\_\+array}!mom\+\_\+tracer\+\_\+z\+\_\+init@{mom\+\_\+tracer\+\_\+z\+\_\+init}}
\subsubsection{\texorpdfstring{tracer\+\_\+z\+\_\+init\+\_\+array()}{tracer\_z\_init\_array()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+tracer\+\_\+z\+\_\+init\+::tracer\+\_\+z\+\_\+init\+\_\+array (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),nk\+\_\+data), intent(in)}]{tr\+\_\+in,  }\item[{real, dimension(nk\+\_\+data+1), intent(in)}]{z\+\_\+edges,  }\item[{integer, intent(in)}]{nk\+\_\+data,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),nlay+1), intent(in)}]{e,  }\item[{real, intent(in)}]{land\+\_\+fill,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{integer, intent(in)}]{nlay,  }\item[{integer, dimension(szi\+\_\+(g),szj\+\_\+(g)), intent(in)}]{nlevs,  }\item[{real, intent(in)}]{eps\+\_\+z,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),nlay), intent(out)}]{tr }\end{DoxyParamCaption})}



Layer model routine for remapping tracers from pseudo-\/z coordinates into layers defined by target interface positions. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in}  & {\em nk\+\_\+data} & The number of levels in the input data\\
\hline
\mbox{\tt in}  & {\em tr\+\_\+in} & The z-\/space array of tracer concentrations that is read in.\\
\hline
\mbox{\tt in}  & {\em z\+\_\+edges} & The depths of the cell edges in the input z$\ast$ data \mbox{[}Z $\sim$$>$ m or m\mbox{]}\\
\hline
\mbox{\tt in}  & {\em nlay} & The number of vertical layers in the target grid\\
\hline
\mbox{\tt in}  & {\em e} & The depths of the target layer interfaces \mbox{[}Z $\sim$$>$ m or m\mbox{]}\\
\hline
\mbox{\tt in}  & {\em land\+\_\+fill} & fill in data over land (1)\\
\hline
\mbox{\tt in}  & {\em nlevs} & The number of input levels with valid data\\
\hline
\mbox{\tt in}  & {\em eps\+\_\+z} & A negligibly thin layer thickness \mbox{[}Z $\sim$$>$ m\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em tr} & tracers in layer space \\
\hline
\end{DoxyParams}


Definition at line 282 of file M\+O\+M\+\_\+tracer\+\_\+\+Z\+\_\+init.\+F90.


\begin{DoxyCode}
282   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}  :: g\textcolor{comment}{     !< The ocean's grid structure}
283   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}  :: nk\_data\textcolor{comment}{ !< The number of levels in the input data}
284   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),nk\_data)}, &
285                               \textcolor{keywordtype}{intent(in)}  :: tr\_in\textcolor{comment}{ !< The z-space array of tracer concentrations that is
       read in.}
286   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk\_data+1)}, \textcolor{keywordtype}{intent(in)}  :: z\_edges\textcolor{comment}{ !< The depths of the cell edges in the input z* data}
287 \textcolor{comment}{                                                          !! [Z ~> m or m]}
288   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}  :: nlay\textcolor{comment}{ !< The number of vertical layers in the target grid}
289   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),nlay+1)}, &
290                               \textcolor{keywordtype}{intent(in)}  :: e\textcolor{comment}{ !< The depths of the target layer interfaces [Z ~> m or m]}
291   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in)}  :: land\_fill\textcolor{comment}{ !< fill in data over land (1)}
292   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}, &
293                               \textcolor{keywordtype}{intent(in)}  :: nlevs\textcolor{comment}{ !< The number of input levels with valid data}
294   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in)}  :: eps\_z\textcolor{comment}{ !< A negligibly thin layer thickness [Z ~> m].}
295   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),nlay)}, &
296                               \textcolor{keywordtype}{intent(out)} :: tr\textcolor{comment}{ !< tracers in layer space}
297 
298   \textcolor{comment}{! Local variables}
299   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk\_data)} :: tr\_1d\textcolor{comment}{ !< a copy of the input tracer concentrations in a column.}
300   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nlay+1)}  :: e\_1d  \textcolor{comment}{! A 1-d column of intreface heights, in the same units as e.}
301   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nlay)}    :: tr\_   \textcolor{comment}{! A 1-d column of output tracer concentrations}
302   \textcolor{keywordtype}{integer} :: k\_top, k\_bot, k\_bot\_prev, kstart
303   \textcolor{keywordtype}{real}    :: sl\_tr    \textcolor{comment}{! The tracer concentration slope times the layer thickness, in tracer units.}
304   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk\_data)} :: wt\textcolor{comment}{ !< The fractional weight for each layer in the range between z1 and z2}
305   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk\_data)} :: z1, z2 \textcolor{comment}{! z1 and z2 are the fractional depths of the top and bottom}
306                                   \textcolor{comment}{! limits of the part of a z-cell that contributes to a layer, relative}
307                                   \textcolor{comment}{! to the cell center and normalized by the cell thickness [nondim].}
308                                   \textcolor{comment}{! Note that -1/2 <= z1 <= z2 <= 1/2.}
309   \textcolor{keywordtype}{integer} :: i, j, k, kz, is, ie, js, je
310 
311   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec
312 
313   \textcolor{keywordflow}{do} j=js,je
314     i\_loop: \textcolor{keywordflow}{do} i=is,ie
315       \textcolor{keywordflow}{if} (nlevs(i,j) == 0 .or. g%mask2dT(i,j) == 0.) \textcolor{keywordflow}{then}
316         tr(i,j,:) = land\_fill
317         cycle i\_loop
318 \textcolor{keywordflow}{      endif}
319 
320       \textcolor{keywordflow}{do} k=1,nk\_data
321         tr\_1d(k) = tr\_in(i,j,k)
322 \textcolor{keywordflow}{      enddo}
323 
324       \textcolor{keywordflow}{do} k=1,nlay+1
325         e\_1d(k) = e(i,j,k)
326 \textcolor{keywordflow}{      enddo}
327       k\_bot = 1 ; k\_bot\_prev = -1
328       \textcolor{keywordflow}{do} k=1,nlay
329         \textcolor{keywordflow}{if} (e\_1d(k+1) > z\_edges(1)) \textcolor{keywordflow}{then}
330           tr(i,j,k) = tr\_1d(1)
331         \textcolor{keywordflow}{elseif} (e\_1d(k) < z\_edges(nlevs(i,j)+1)) \textcolor{keywordflow}{then}
332           tr(i,j,k) = tr\_1d(nlevs(i,j))
333 
334         \textcolor{keywordflow}{else}
335           kstart = k\_bot
336           \textcolor{keyword}{call }find\_overlap(z\_edges, e\_1d(k), e\_1d(k+1), nlevs(i,j), &
337                             kstart, k\_top, k\_bot, wt, z1, z2)
338           kz = k\_top
339           sl\_tr = 0.0 \textcolor{comment}{! ; cur\_tr=0.0}
340           \textcolor{keywordflow}{if} (kz /= k\_bot\_prev) \textcolor{keywordflow}{then}
341             \textcolor{comment}{! Calculate the intra-cell profile.}
342             \textcolor{keywordflow}{if} ((kz < nlevs(i,j)) .and. (kz > 1)) \textcolor{keywordflow}{then}
343               sl\_tr = find\_limited\_slope(tr\_1d, z\_edges, kz)
344 \textcolor{keywordflow}{            endif}
345 \textcolor{keywordflow}{          endif}
346           \textcolor{keywordflow}{if} (kz > nlevs(i,j)) kz = nlevs(i,j)
347           \textcolor{comment}{! This is the piecewise linear form.}
348           tr(i,j,k) = wt(kz) * (tr\_1d(kz) + 0.5*sl\_tr*(z2(kz) + z1(kz)))
349           \textcolor{comment}{! For the piecewise parabolic form add the following...}
350           \textcolor{comment}{!     + C1\_3*wt(kz) * cur\_tr*(z2(kz)**2 + z2(kz)*z1(kz) + z1(kz)**2))}
351           \textcolor{keywordflow}{do} kz=k\_top+1,k\_bot-1
352             tr(i,j,k) = tr(i,j,k) + wt(kz)*tr\_1d(kz)
353 \textcolor{keywordflow}{          enddo}
354 
355           \textcolor{keywordflow}{if} (k\_bot > k\_top) \textcolor{keywordflow}{then}
356             kz = k\_bot
357             \textcolor{comment}{! Calculate the intra-cell profile.}
358             sl\_tr = 0.0 \textcolor{comment}{! ; cur\_tr = 0.0}
359             \textcolor{keywordflow}{if} ((kz < nlevs(i,j)) .and. (kz > 1)) \textcolor{keywordflow}{then}
360               sl\_tr = find\_limited\_slope(tr\_1d, z\_edges, kz)
361 \textcolor{keywordflow}{            endif}
362             \textcolor{comment}{! This is the piecewise linear form.}
363             tr(i,j,k) = tr(i,j,k) + wt(kz) * (tr\_1d(kz) + 0.5*sl\_tr*(z2(kz) + z1(kz)))
364             \textcolor{comment}{! For the piecewise parabolic form add the following...}
365             \textcolor{comment}{!     + C1\_3*cur\_tr*(z2(kz)**2 + z2(kz)*z1(kz) + z1(kz)**2))}
366 \textcolor{keywordflow}{          endif}
367           k\_bot\_prev = k\_bot
368 
369 \textcolor{keywordflow}{        endif}
370 \textcolor{keywordflow}{      enddo} \textcolor{comment}{! k-loop}
371 
372       \textcolor{keywordflow}{do} k=2,nlay  \textcolor{comment}{! simply fill vanished layers with adjacent value}
373         \textcolor{keywordflow}{if} (e\_1d(k)-e\_1d(k+1) <= eps\_z) tr(i,j,k) = tr(i,j,k-1)
374 \textcolor{keywordflow}{      enddo}
375 
376 \textcolor{keywordflow}{    enddo} i\_loop
377 \textcolor{keywordflow}{  enddo}
378 
\end{DoxyCode}
