\documentclass[a4paper,12pt]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[colorinlistoftodos]{todonotes}
\addtolength{\oddsidemargin}{-.875in}
\addtolength{\evensidemargin}{-.875in}
\addtolength{\textwidth}{1.75in}

\addtolength{\topmargin}{-.875in}
\addtolength{\textheight}{1.75in}
\title{s2dverification: Time series visualization function}

\author{N. Manubens, A. Hunter}

\date{\today}

\begin{document}
\maketitle

%\begin{abstract}

%\end{abstract}

\section{PlotTimeSeries}
\label{sec:introduction}

This is a preliminary report describing the updated time series plotting function, \emph{PlotTimeSeries}, and presents several examples that highlight its improved functionality. The new function is an agglomerate of the previous time series plotting functions in the R package \emph{s2dverification}, with additional functionality. The function is still evolving, to ensure that it is compatible with the Common Data Model (CDM) R data structure which is currently being discussed within the QA4Seas project.
\\

In brief, the \emph{PlotTimeSeries} function takes arrays of any number of (named) dimensions and creates scatter plots or draws curves along them, as well as plotting their means and standard errors. The inputted data can include metadata that is automatically represented on the plot, such as the name of the plotted variable, the start dates of a set of provided seasonal forecasts, the date at each forecast time step or the names of the members. The user can manually specify which dimensions to colour or style along, but the function is designed to anticipate the user's requirements. The advantages of the new function are its increased ease-of-use and versatility, as well as the improved quality of the final plots due to the use of the \emph{ggplot2} package. 

\section{Examples}

The default settings of the \emph{PlotTimeSeries} function assume that the data to be plotted is either in the CDM format or it is an array of any number of dimensions, and the desired output is a scatter plot with different colours for the start dates and different shapes for the model members, with a smoothing function fitted along the start dates (Loess) and its standard error. However, if these dimensions cannot be automatically found in the data provided, the default settings are disregarded. The function also accepts bare R arrays without dimension names or meta-data, or can be adjusted to recognize any custom convention for the dimension names, allowing for its application to other research fields than climate prediction. 
\\

Plots of the mean sea surface temperature anomalies are shown in Fig. \ref{fig:1}, using the sample dataset that is loaded with the code in the Appendix.
\begin{verbatim}
PlotTimeSeries(ano_exp)
PlotTimeSeries(ano_exp, curves_along = 'member')
PlotTimeSeries(ano_exp, curves_along = NA)
PlotTimeSeries(ano_exp, curves_along = 3) 
\end{verbatim}
The function allows the user to plot individual model members with or without the mean, or to display only the mean and the maximum and minimum.  

\begin{figure}
	\centering
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig1a.png}
		\caption{}
		\label{fig:1a}
	\end{subfigure}
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig1b.png}
		\caption{}
		\label{fig:1b}
	\end{subfigure}

	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig1c.png}
		\caption{}
		\label{fig:1c}
	\end{subfigure}
		\begin{subfigure}[b]{0.49\textwidth}
			\includegraphics[width=\textwidth]{fig1d.png}
			\caption{}
			\label{fig:1d}
		\end{subfigure}
	\caption{Drift-corrected anomalies of monthly sea surface temperature averages over the whole globe, from the i00k experiment run at BSC-CNS using the EC-Earth (v2.3.0) climate model, initialized every November 1st for the start dates 1985, 1990, 1995, 2000 and 2005. The forecast duration is 5 years}\label{fig:1}
\end{figure}

Although the default setting is for the function to colour along the start dates, the user can change this to any of the named dimensions, as in Fig \ref{fig:2}.
\begin{figure}
	\centering
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig2a.png}
		\caption{}
		\label{fig:2a}
	\end{subfigure}
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=\textwidth]{fig2b.png}
		\caption{}
		\label{fig:2b}
	\end{subfigure}
	
	\caption{Drift-corrected anomalies of monthly sea surface temperature averages over the whole globe, from the i00k experiment run at BSC-CNS using the EC-Earth (v2.3.0) climate model, initialized every November 1st for the start dates 1985, 1990, 1995, 2000 and 2005. The forecast duration is 5 years }\label{fig:2}
\end{figure}

\section{Future work}
\
The function is being refined to accommodate a wide range of user requirements. Specifically, the user will also be able to plot the confidence intervals and errors bars, when these values are included in the data. This work is being conducted alongside changes currently being made to the \emph{s2dverification} and \emph{downscaleR} packages to ensure a common R data structure. 

\section{Appendix}
R code for reproducing the figures.
\begin{verbatim}
# Loading example data
library(s2dverification)
data_path <- system.file('sample_data', package = 's2dverification')
expA <- list(name = 'experiment', path = file.path(data_path, 
'model/$EXP_NAME$/$STORE_FREQ$_mean/$VAR_NAME$_3hourly',
'$VAR_NAME$_$START_DATE$.nc'))
obsX <- list(name = 'observation', path = file.path(data_path,
'$OBS_NAME$/$STORE_FREQ$_mean/$VAR_NAME$',
'$VAR_NAME$_$YEAR$$MONTH$.nc'))

# Now we are ready to use Load().
startDates <- c('19851101', '19901101', '19951101', '20001101', '20051101')
sampleData <- Load('tos', list(expA), list(obsX), startDates,
output = 'areave', latmin = 27, latmax = 48, 
lonmin = -12, lonmax = 40)

# Doing example calculations
clim <- Clim(sampleData$mod, sampleData$obs)
ano_exp <- Ano(sampleData$mod, clim$clim_exp)
ano_exp <- Smoothing(ano_exp)
ano_obs <- Ano(sampleData$obs, clim$clim_obs)
ano_obs <- Smoothing(ano_obs)

# Transforming to 'hypothetical' common data structure
ano_exp <- list(Data = ano_exp,
Variable = sampleData$Variable,
Datasets = sampleData$Datasets$exp,
Dates = sampleData$Dates)
class(ano_exp) <- 'multiCDM'
ano_obs <- list(Data = ano_obs,
Variable = sampleData$Variable,
Datasets = sampleData$Datasets$obs,
Dates = sampleData$Dates)
class(ano_obs) <- 'multiCDM'

# Building extra objects with data for only 1 startdate (exp2) and for 
# only 1 forecast time step (exp3)
exp2 <- ano_exp
exp2$Data <- Subset(exp2$Data, 'sdate', 1, drop = 'selected')
exp2$Dates$start <- exp2$Dates$start[1:60]
exp2$Dates$end <- exp2$Dates$end[1:60]
exp3 <- ano_exp
exp3$Data <- Subset(exp3$Data, 'ftime', 30, drop = 'selected')
exp3$Dates$start <- exp3$Dates$start[seq(1, 300, 60)]
exp3$Dates$end <- exp3$Dates$end[seq(1, 300, 60)]


source('PlotTimeSeries.R')
PlotTimeSeries(ano_exp)
PlotTimeSeries(ano_exp, curves_along = 'member')
PlotTimeSeries(ano_exp, curves_along = NA)
PlotTimeSeries(ano_exp, curves_along = 3)
PlotTimeSeries(ano_exp, colour_along = 'ftime')
PlotTimeSeries(ano_exp, mean_along = NA)
\end{verbatim}

\end{document}