\documentclass{article}
\title{Poročilo Laktatni test}
\author{Simon Cirnski}

\usepackage[margin=0.7in]{geometry}

\usepackage{makecell}
\usepackage{multirow}

\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}

\begin{document}

  \maketitle

  \begin{table}[h]
    \centering
    \begin{tabular}{r||c|c|c|c|}
                           & Power [W]                        & Heart Rate [bpm]                      & Power to Weight Ratio [W/kg]                                 & Blood Lactate [mMol]                           \\ \hline \hline
      \BLOCK{ for i in range( inputs['power'] | length) }
        \VAR{ loop.index } & \VAR{ inputs['power'][i] | int } & \VAR{ inputs['heart_rate'][i] | int } & \VAR{ '%.2f' | format(inputs['power'][i]/inputs['weight']) } & \VAR{ '%.1f' | format(inputs['lactate'][i]) }  \BLOCK{ if not loop.last }\\\BLOCK{ endif }
      \BLOCK{ endfor }
    \end{tabular}
    \caption{Meritve}
  \end{table}

  \def\lacpoly(#1){\VAR{ lac_poly.poly.coef[0] }*(#1)^3 + \VAR{ lac_poly.poly.coef[1] }*(#1)^2 +  \VAR{ lac_poly.poly.coef[2] }*(#1) + \VAR{ lac_poly.poly.coef[3] }}
  \def\coordinates{
    \BLOCK{ for i in range( inputs['power'] | length) }
      (\VAR{ inputs['power'][i] }, \VAR{ inputs['lactate'][i] })
    \BLOCK{ endfor }
  }
  \def\xmin{\VAR{ inputs['power'][0] }}
  \def\xmax{\VAR{ inputs['power'][-1] }}
  \def\ymin{0}
  \def\ymax{\VAR{ inputs['lactate'][-1] }}

  \pgfplotsset{
    axis y line=left,
    ylabel near ticks,
    ylabel=Blood Lactate {[}mMol{]},
    ytick={0,2,...,50},
    axis x line=bottom,
    xlabel=Power {[}W{]},
    xtick={0, 50,..., 500},
    width=8.5cm,
    height=8.5cm,
    xmin=\xmin - 40,
    xmax=\xmax + 40,
    ymin=0,
    ymax=\ymax + 2,
    grid,
  }

  \begin{tikzpicture}
  \begin{axis}
    \coordinate (start) at (axis cs: \VAR{ dmax['start_point'][0] }, \VAR{ dmax['start_point'][1] });
    \coordinate (dmax) at (axis cs: \VAR{ dmax['power'] }, \VAR{ dmax['lactate'] });
    \coordinate (end) at (axis cs: \VAR{ dmax['end_point'][0] }, \VAR{ dmax['end_point'][1] });
    \coordinate (dmax') at ($(start)!(dmax)!(end)$);
    \coordinate (dmax'') at (axis cs: \VAR{ dmax['power'] }, 0);

    \addplot [domain=\xmin-20:\xmax+20, thick] {\lacpoly(x)};
    \addplot [only marks] coordinates {\coordinates};
    \draw [thick] (start) -- (end);
    \draw [thick] (dmax') -- (dmax);
    \draw [thick] (dmax'') -- (dmax) node[pos=0.5, right] {$P=\VAR{ dmax['power'] | int }W$};
  \end{axis}
  \end{tikzpicture}
  \begin{tikzpicture}[
    extended line/.style={shorten >=-#1},
    extended line/.default=.5cm,
  ]
  \begin{axis}
    \coordinate (start) at (axis cs: \VAR{ cross['start_point'][0] }, \VAR{ cross['start_point'][1] });
    \coordinate (cross) at (axis cs: \VAR{ cross['cross'][0] }, \VAR{ cross['cross'][1] });
    \coordinate (end) at (axis cs: \VAR{ cross['end_point'][0] }, \VAR{ cross['end_point'][1] });
    \coordinate (cross') at (axis cs: \VAR{ cross['cross'][0] }, 0);

    \addplot [domain=\xmin-20:\xmax+20, thick] {\lacpoly(x)};
    \addplot [only marks] coordinates {\coordinates};
    \draw [thick, extended line] (start) -- (cross);
    \draw [thick, extended line] (end) -- (cross);
    \draw [thick] (cross') -- (cross) node[pos=0.5, right] {$P=\VAR{ cross['power'] | int }W$};
  \end{axis}
  \end{tikzpicture}

  \begin{table}[h]
    \centering
    \begin{tabular}{r|r||c|c|c|c}
                                                         &                     & Power [W]                     & Heart Rate [bpm]                   & \makecell{Power to Weight \\ Ratio [W/kg]}                & \makecell{Blood Lactate \\ {[}mMol]}       \\
      \hline \hline
      \multirow{3}{*}{\makecell{Anaerobic \\ threshold}} & Modified D-max      & \VAR{ dmax['power'] | int }   & \VAR{ dmax['heart_rate'] | int }   & \VAR{ '%.2f' | format(dmax['power']/inputs['weight']) }   & \VAR{ '%.1f' | format(dmax['lactate']) }   \\
                                                         & Cross method        & \VAR{ cross['power'] | int }  & \VAR{ cross['heart_rate'] | int }  & \VAR{ '%.2f' | format(cross['power']/inputs['weight']) }  & \VAR{ '%.1f' | format(cross['lactate']) }  \\
                                                         & AT4 4 mMol          & \VAR{ at4['power'] | int }    & \VAR{ at4['heart_rate'] | int }    & \VAR{ '%.2f' | format(at4['power']/inputs['weight']) }    & \VAR{ '%.1f' | format(at4['lactate']) }    \\
      \hline
      \multirow{2}{*}{\makecell{Aerobic \\ threshold}}   & Baseline + 0.5 mMol & \VAR{ dmax['start_point'][0] | int }   & \VAR{ dmax['start_hr'] | int }   & \VAR{ '%.2f' | format(dmax['start_point'][0]/inputs['weight']) }   & \VAR{ '%.1f' | format(dmax['start_point'][1]) } \\
                                                         & AT2 2 mMol          & \VAR{ at2['power'] | int }    & \VAR{ at2['heart_rate'] | int }    & \VAR{ '%.2f' | format(at2['power']/inputs['weight']) }    & \VAR{ '%.1f' | format(at2['lactate']) }
    \end{tabular}
    \caption{Rezultati testa}
  \end{table}

  \begin{table}[h]
    \centering
    \begin{tabular}{r|l||c|c|c}
      Zone  & Training             & Low watts                              & High watts                             & Notes     \\
      \hline \hline
      1     & Active recovery      & 0                                      & \VAR{ (dmax['power'] * 0.50) | int }W  & 10h+      \\
      2     & Endurance            & \VAR{ (dmax['power'] * 0.50 + 1) | int }W  & \VAR{ (dmax['power'] * 0.75) | int }W  & 2-10h     \\
      3     & Tempo                & \VAR{ (dmax['power'] * 0.75 + 1) | int }W  & \VAR{ (dmax['power'] * 0.87) | int }W  & 30-90min  \\
      3a    & SubThreshold         & \VAR{ (dmax['power'] * 0.87 + 1) | int }W  & \VAR{ (dmax['power'] * 0.93) | int }W  & 10-60min  \\
      4     & Threshold            & \VAR{ (dmax['power'] * 0.93 + 1) | int }W  & \VAR{ (dmax['power'] * 1.04) | int }W  & 8-30min   \\
      5     & VO2max               & \VAR{ (dmax['power'] * 1.04 + 1) | int }W  & \VAR{ (dmax['power'] * 1.30) | int }W  & 2-8min    \\
      6     & Anaerobic capacity   & \VAR{ (dmax['power'] * 1.30 + 1) | int }W  & \VAR{ (dmax['power'] * 2.00) | int }W  & 30-90s    \\
      7     & Neuromuscular power  & \VAR{ (dmax['power'] * 2.00 + 1) | int }W  & max                                    & 5-15s     \\
    \end{tabular}
    \caption{Zones}
  \end{table}


\end{document}
