---
author: "Nicolau Manubens"
date: "`r Sys.Date()`"
output: html_document
vignette: >
  %\VignetteEngine{R.rsp::md}
  %\VignetteIndexEntry{Snippets}
  %\usepackage[utf8]{inputenc}
---
Snippets
========
This page contains extended snippets of code used in some sections of the wiki 
page to obtain figures or results.

The example data used is the same used in [**Example**](example.md) and 
throughout in the vignettes referenced in [**Overview**](../README.md):
```r
library(s2dv)

expA <- list(name = 'experimentA',
             path = file.path('/path/to/experiments/$EXP_NAME$/monthly_mean',
                              '$VAR_NAME$/$VAR_NAME$_$START_DATE$.nc'))
expB <- list(name = 'experimentB',
             path = file.path('/path/to/experiments/$EXP_NAME$/monthly_mean',
                              '$VAR_NAME$/$VAR_NAME$_$START_DATE$.nc'))
obsX <- list(name = 'observationX',
             path = file.path('/path/to/observations/$OBS_NAME$/monthly_mean',
                              '$VAR_NAME$/$VAR_NAME$_$YEAR$$MONTH$.nc'))
```

Snippet 1<a name="snippet1"></a>
---------
Plots 3 equidistant projection maps on a single figure with a single color bar.
See in [context](example.md#snippet1).
```r
sdates <- paste0(1991:2000, '1101')
data <- Load('tas', list(expA, expB), list(obsX),
             sdates = sdates,
             leadtimemin = 2, leadtimemax = 13,
             latmin = -10, latmax = 60,
             lonmin = 100, lonmax = 250,
             output = 'lonlat', grid = 't106grid',
             method = 'distance-weighted')
brks <- seq(min(data$mod, data$obs, na.rm = TRUE), 
            max(data$mod, data$obs, na.rm = TRUE), 
            length.out = 21)
cols <- clim.colors(20)
png('snip1_equi_map_raw_all.png', width = 800, height = 300, type = "Xlib")
layout(matrix(c(1, 1, 2, 2, 3, 3, 
                0, 4, 4, 4, 4, 0), 
              2, 6, byrow = TRUE),
       heights = c(5, 1))
PlotEquiMap(data$mod[1,1,1,1,,], data$lon, data$lat, brks = brks, cols = cols, drawleg = FALSE, toptitle = 'Experiment A: tas (K)', sizetit = 0.7)
PlotEquiMap(data$mod[2,1,1,1,,], data$lon, data$lat, brks = brks, cols = cols, drawleg = FALSE, toptitle = 'Experiment B: tas (K)', sizetit = 0.7)
PlotEquiMap(data$obs[1,1,1,1,,], data$lon, data$lat, brks = brks, cols = cols, drawleg = FALSE, toptitle = 'Observation X: tas (K)', sizetit = 0.7)
ColorBar(brks, cols, vert = FALSE, subsampleg = 5)
dev.off()
```
<img src="snip1_equi_map_raw_all.png" />

Snippet 2<a name="snippet2"></a>
---------
Plots time correlations (along actual time) of Decembers of Experiment A and 
Experiment B at each grid point over the Atlantic. The black dots mean the 
correlation at that point exceeds the 95% significance level. See in 
[context](visualisation.md#snippet2).
```r
map_data <- Load('tas', list(expA, expB), list(obsX),
                 sdates = sdates,
                 leadtimemin = 2, leadtimemax = 13,
                 latmin = -30, latmax = 60,
                 lonmin = -90, lonmax = 20,
                 output = 'lonlat', grid = 't106grid',
                 method = 'distance-weighted')
ano <- Ano_CrossValid(map_data$mod, map_data$obs)
corr <- Corr(Mean1Dim(ano$ano_exp, 2), Mean1Dim(ano$ano_obs, 2))

cols <- clim.colors(50)
corr_min <- min(corr, na.rm = TRUE)
corr_max <- max(corr, na.rm = TRUE)
corr_brks <- round(seq(corr_min, corr_max, length.out = length(cols) + 1), 2)

png('snip2_equimap_corr_raw_expA_obsX.png', width = 800, height = 600)
PlotEquiMap(corr[1, 1, 2, 1, , ], map_data$lon, map_data$lat,
            toptitle = "Exp. A: 'tas' correlation along Decembers with Obs. X.",
            units = "K", brks = corr_brks, cols = cols, subsampleg = 5,
            dots = t(corr[1, 1, 2, 1, , ] > corr[1, 1, 4, 1, , ]))
dev.off()

png('snip2_equimap_corr_raw_expB_obsX.png', width = 800, height = 600)
PlotEquiMap(corr[2, 1, 2, 1, , ], map_data$lon, map_data$lat,
            toptitle = "Exp. B: 'tas' correlation along Decembers with Obs. X.",
            units = "K", brks = corr_brks, cols = cols, subsampleg = 5,
            dots = t(corr[2, 1, 2, 1, , ] > corr[2, 1, 4, 1, , ]))
dev.off()
```
<img src="snip2_equimap_corr_raw_expA_obsX.png" width="600" />
<img src="snip2_equimap_corr_raw_expB_obsX.png" width="600" />

And generates the animations of the actual time correlations of Experiment A 
and B against Observation X over the Atlantic, with black dots on values that 
reach a 95% significance level:

```r
AnimVsLTime(corr, map_data$lon, map_data$lat, monini = 12,
            msk95lev = TRUE,
            toptitle = c("Exp. A: actual time correlation with Obs. X.",
                         "Exp. B: actual time correlation with Obs. X."), 
            units = "K",
            brk = corr_brks, col = cols, subsampleg = 5,
            fileout = c("snip2_anim_corr_expA_obsX",
                        "snip2_anim_corr_expB_obsX"))
```

<img src="snip2_anim_corr_expA_obsX.gif" width="600" />
<img src="snip2_anim_corr_expB_obsX.gif" width="600" />

