%- macro version_description()
%- include 'version_description'
%- endmacro

%- macro metrics_meta_table_text()
%- include 'metrics_meta_table_text'
%- endmacro

%- macro data_validation_pre_text()
%- include 'data_validation_pre_text'
%- endmacro

%- macro data_validation_post_text()
%- include 'data_validation_post_text'
%- endmacro

%- macro obsfx_table_text()
%- include 'obsfx_table_text'
%- endmacro

%- macro data_resampling_preamble()
%- include 'data_resampling_preamble'
%- endmacro

%- macro data_resampling_postamble()
%- include 'data_resampling_postamble'
%- endmacro

%- macro metrics_meta_table()
\VAR{metrics_meta_table_text() | link_filter | html_to_tex}
\begin{table}[h]
  \caption{Table of metrics metadata.}
  \small
  \begin{tabu} to \linewidth {
      X[l] | l l l
    }
    Name & Normalization & Deadband(\%) & Cost Parameters\\
    \midrule
    %- for pfxobs in report.raw_report.processed_forecasts_observations
    \VAR{pfxobs.name | html_to_tex} & \VAR{pfxobs.normalization_factor} & \VAR{pfxobs.uncertainty} & \hyperref[cost:\VAR{pfxobs.cost.name | html_to_tex | replace('_', '+')}]{\VAR{pfxobs.cost.name | html_to_tex}} \\
    %- endfor
  \end{tabu}
\end{table}
%- endmacro

%- macro metric_table(report_metrics, category, metric_ordering)
%- set human_category = human_categories[category]
\FloatBarrier
\clearpage
\begin{landscape}
  \pagestyle{empty}
  \small
  \begin{longtabu} to \linewidth {
      X *{\VAR{metric_ordering | length}}{| l}
    }
    \caption{Table of \VAR{human_category} metrics.} \\
    \toprule
    Forecast\BLOCK{for m in metric_ordering} & \VAR{human_metrics[m].replace('^', '\\textasciicircum')}\BLOCK{endfor} \\
    \midrule
    \endfirsthead
    \caption{Table of \VAR{human_category} metrics (cont.)} \\
    \toprule
    Forecast\BLOCK{for m in metric_ordering} & \VAR{human_metrics[m].replace('^', '\\textasciicircum')}\BLOCK{endfor} \\
    \midrule
    \endhead
    \endfoot
    \bottomrule
    \endlastfoot
    %- for fx_result in report_metrics
    \VAR{fx_result.name | html_to_tex}
    %- for metric in metric_ordering
    %- set ns = namespace(found=false)
    %- for mval in fx_result.values
    %- if mval.category == category and mval.metric == metric
    %- set ns.found = true
    %- if -0.01 < mval.value < 0.01
    & \VAR{'%.2e' | format(mval.value)}
    %- else
    & \VAR{'%0.3g' | format(mval.value)}
    %- endif
    %- endif
    %- endfor
    %- if not ns.found
    &
    %- endif
    %- endfor
    \\
    %- endfor
  \end{longtabu}
\end{landscape}
\clearpage
\FloatBarrier
%- endmacro

%- macro data_parameters_table()
\FloatBarrier
\clearpage
\begin{landscape}
  \renewcommand{\arraystretch}{1.7}
  \pagestyle{empty}
  \small
  \begin{longtabu} to \linewidth {
      X[1.5, l] X[l] X[l] *{3}{ | X[1.5, l] X[l] X[l]}
    }
    \caption{Table of data alignment parameters.} \\
    \toprule
    \multicolumn{3}{c}{Aligned Pairs} &
    \multicolumn{3}{c}{Observations} &
    \multicolumn{3}{c}{Forecasts} &
    \multicolumn{3}{c}{Reference Forecasts} \\
    Name & Interval Label & Interval Length &
    Name & Interval Label & Interval Length &
    Name & Interval Label & Interval Length &
    Name & Interval Label & Interval Length \\
    \midrule
    \endfirsthead
    \caption{Table of data alignment parameters (cont.)} \\
    \endhead
    \endfoot
    \bottomrule
    \endlastfoot
    %- for pfxobs in report.raw_report.processed_forecasts_observations
    \VAR{pfxobs.name | html_to_tex} &
    \VAR{pfxobs.interval_label} &
    \VAR{pfxobs.interval_length.total_seconds()//60 | int} min &
    %- if pfxobs.original.observation is defined
    \href{\VAR{dash_url + '/observations/' + pfxobs.original.observation.observation_id}}{\VAR{pfxobs.original.observation.name}} &
    %- else
    \href{\VAR{dash_url + '/aggregates/' + pfxobs.original.aggregate.aggregate_id}}{\VAR{pfxobs.original.aggregate.name}} &
    %- endif
    \VAR{pfxobs.original.data_object.interval_label} &
    \VAR{pfxobs.original.data_object.interval_length.total_seconds()//60 | int} min &
    %- if pfxobs.original.forecast.constant_values is defined
    \href{\VAR{dash_url + '/forecasts/cdf/' + pfxobs.original.forecast.forecast_id}}{\VAR{pfxobs.original.forecast.name}} &
    %- elif pfxobs.original.forecast.constant_value is defined
    \href{\VAR{dash_url + '/forecasts/cdf/single/' + pfxobs.original.forecast.forecast_id}}{\VAR{pfxobs.original.forecast.name}} &
    %- else
    \href{\VAR{dash_url + '/forecasts/single/' + pfxobs.original.forecast.forecast_id}}{\VAR{pfxobs.original.forecast.name}} &
    %- endif
    \VAR{pfxobs.original.forecast.interval_label} &
    \VAR{pfxobs.original.forecast.interval_length.total_seconds()//60 | int} min &
    %- if pfxobs.original.reference_forecast != none
    %- if pfxobs.original.reference_forecast.constant_values is defined
    \href{\VAR{dash_url + '/forecasts/cdf/' + pfxobs.original.reference_forecast.forecast_id}}{\VAR{pfxobs.original.reference_forecast.name}} &
    %- elif pfxobs.original.forecast.constant_value is defined
    \href{\VAR{dash_url + '/forecasts/cdf/single/' + pfxobs.original.reference_forecast.forecast_id}}{\VAR{pfxobs.original.reference_forecast.name}} &
    %- else
    \href{\VAR{dash_url + '/forecasts/single/' + pfxobs.original.reference_forecast.forecast_id}}{\VAR{pfxobs.original.reference_forecast.name}} &
    %- endif
    \VAR{pfxobs.original.reference_forecast.interval_label} &
    \VAR{pfxobs.original.reference_forecast.interval_length.total_seconds()//60 | int} min
    %- else
    None &
    %- endif
    \\
    %- endfor
  \end{longtabu}
\end{landscape}
\FloatBarrier
\clearpage
%- endmacro


%- macro data_validation_table()
\VAR{data_validation_pre_text() | html_to_tex}
\VAR{data_validation_post_text() | html_to_tex}
%- for filter in report.report_parameters.filters
%- if filter.__class__.__name__ == 'QualityFlagFilter'
\VAR{validation_table(report.raw_report.processed_forecasts_observations, filter.quality_flags)}
%- endif
%- endfor
%- endmacro


%- macro validation_table(proc_fxobs_list, quality_filters)
\begin{landscape}
  \pagestyle{empty}
  \small
  \renewcommand{\arraystretch}{1.7}
  \begin{longtabu} to \linewidth {
      X[2] | X[2] | *{\VAR{quality_filters | length}}{ | X[l]}
    }
    \caption{Table of data validation results.} \\
    \toprule
    \multicolumn{1}{l}{} &  \multicolumn{1}{l ||}{} &
    \multicolumn{\VAR{quality_filters | length}}{c}{Quality Flag} \\
    \cline{3-\VAR{(quality_filters | length) + 2 }}
    \multirow{1}{*}{Aligned Pair} &  \multirow{1}{*}{Observation}
    %- for qfilter in quality_filters
    & \ccbr{\VAR{qfilter}}
    %- endfor
    \\
    \midrule
    \endfirsthead
    \caption{Table of data validation results (cont.)} \\
    \multirow{1}{*}{Aligned Pair} &  \multirow{1}{*}{Observation}
    %- for qfilter in quality_filters
    & \ccbr{\VAR{qfilter}}
    %- endfor
    \\
    \midrule
    \endhead
    \endfoot
    \bottomrule
    \endlastfoot
    %- for pfxobs in proc_fxobs_list
    \VAR{pfxobs.name | html_to_tex} & \VAR{pfxobs.original.data_object.name}
    %- for qfilter in quality_filters
    %- set ns  = namespace(found=false)
    %- for res in pfxobs.validation_results
    %- if res.flag == qfilter
    %- set ns.found = true
    & \VAR{res.count}
    %- endif
    %- endfor
    %- if not ns.found
    &
    %- endif
    %- endfor
    \\
    %- endfor
  \end{longtabu}
\end{landscape}
%- endmacro


%- macro data_resampling_section()
\VAR{data_resampling_preamble() | html_to_tex}
\VAR{data_resampling_postamble() | html_to_tex}
\VAR{data_preprocessing_table(report.raw_report.processed_forecasts_observations)}
%- endmacro

%- macro data_preprocessing_table(proc_fxobs_list)
%- set preproc_steps = []
%- for proc_fxobs in proc_fxobs_list
%- for res in proc_fxobs.preprocessing_results
%- if res.name not in preproc_steps
%- set preproc_steps = preproc_steps.append(res.name)
%- endif
%- endfor
%- endfor
\clearpage
\begin{landscape}
  \pagestyle{empty}
  \small
  \renewcommand{\arraystretch}{1.7}
  \begin{longtabu} to \linewidth {
      X[2, l] *{\VAR{preproc_steps | length}}{ | X[1, l]}
    }
    \caption{Table of data preprocessing results.} \\
    \toprule
    & \multicolumn{\VAR{preproc_steps | length}}{c}{Number of points} \\
    %- for step in preproc_steps
    & \ccbr{\VAR{step}}
    %- endfor
    \\
    \midrule
    \endfirsthead
    \caption{Table of data preprocessing results (cont.)} \\
    %- for step in preproc_steps
    & \ccbr{\VAR{step}}
    %- endfor
    \\
    \midrule
    \endhead
    \endfoot
    \bottomrule
    \endlastfoot
    %- for pfxobs in proc_fxobs_list
    \VAR{pfxobs.name | html_to_tex}
    %- for pstep in preproc_steps
    %- set ns = namespace(found=false)
    %- for res in pfxobs.preprocessing_results
    %- if res.name == pstep
    %- set ns.found = true
    & \VAR{res.count}
    %- endif
    %- endfor
    %- if not ns.found:
    &
    %- endif
    %- endfor
    \\
    %- endfor
  \end{longtabu}
\end{landscape}
%- endmacro
