---
title: "Map: Various Projections"
author: "An-Chi Ho"
date: "2023-11-21"
output: html_document
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Map: Various Projections}
  %\usepackage[utf8]{inputenc}
---

# Visualization of different projections

This vignette shows how to use function `VizRobinson()`, which can plot maps with the projection of your choice.
The projection types and IDs can be found on https://epsg.io/. Note that different PROJ and GDAL module versions may have different CRS codes. In this vignettes, GDAL_3.5.2 and PROJ_9.1.0 are used.

We will use the data loaded in [Data Loading](data_loading.md) in this vignette. 
Please follow that vignette first to get the data if you want to replicate the results in this vignette. Load global data with `region = "global"` and european data with `region = "euro"` in the loading script.

## Global Robinson Projection

First, we use s2dv function `Clim()` and `Ano()` to calculate anomalies, then use `RMSSS()` to calculate 
root mean square error skill score between ensemble-mean experiment SEAS5 and the observation ERA5.

```r
exp1 <- ClimProjDiags::Subset(exp, c("dat", "var"), list(1, 1), drop = "selected")
exp1 <- s2dv::MeanDims(exp1, "ensemble")
obs1 <- ClimProjDiags::Subset(obs, c("dat", "var"), list(1, 1), drop = "selected")

# Calculate anomalies and trend
clim <- s2dv::Clim(exp1, obs1, dat_dim = NULL, memb_dim = NULL)
ano_exp <- s2dv::Ano(exp1, clim$clim_exp)
ano_obs <- s2dv::Ano(obs1, clim$clim_obs)

# Calculate RMSSS
rmsss <- s2dv::RMSSS(ano_exp, ano_obs, 
                     time_dim = 'sdate', dat_dim = NULL,
                     pval = FALSE, sign = TRUE, alpha = 0.05,
                     sig_method = "Random Walk", sig_method.type = "two.sided")
```

We plot the RMSSS result where pass 95% significance test. The points that are not significant, we can use parameter `mask` to not plot them or use parameter `dots` to mark the unsignificance.

```r
lats <- attributes(exp)$Variables$system5c3s$lat
lons <- attributes(exp)$Variables$system5c3s$lon

# Create adequete colorbar elements for the plot
cols <- ClimColors(52, palette = "purpleorange")
col_inf <- cols[1]
col_sup <- tail(cols, 1)
cols <- cols[c(2:51)]
brks <- seq(-1, 1, length.out = 51)

```
The code of Robinson projection is 54030 or 53030.
Note that different PROJ and GDAL module versions have different CRS codes. If on workstation, use "54030"; if on hub, use "54030"; if on Nord3v2, please use "ESRI:53030" or "+proj=robin" to get the correct Robinson projection.

The plot style can be either "point" or "polygen", which is more time-consuming. There are two types of legend, 'ggplot2' or 'bar'. You can also set `legend` as `NULL` to not plot legend.

```r
VizRobinson(rmsss$rmsss[1, , ], lon = lons, lat = lats,
            target_proj = "ESRI:54030",
            dots = !rmsss$sign[1, , ], dot_size = 0.7,
            brks = brks, cols = cols, col_inf = col_inf, 
            style = 'point', drawleg = 'bar',
            toptitle = "RMSSS SEAS5-ERA5 monthly tas anomaly Nov 2011-2016", 
            caption = "Projection: Robinson ESRI:54030",
            width = 10, height = 6,
            fileout = 'map_robinson_rmsss_global_robin.png')

```

<img src="./Figures/map_robinson_rmsss_global_robin.png" width="600" />


## Global Mollweide Projection

You can find more projection types through the link at the beginning. `VizRobinson()` is mainly designed for Robinson projection, but it is possible to use other projections as well.
For example, Mollweide projection is "ESRI:54009" or "ESRI:53009". We use parameter "mask" instead of "dots" to ignore unsignificant points.

```r
VizRobinson(rmsss$rmsss[1, , ], lon = lons, lat = lats,
            target_proj = "ESRI:54009",
            mask = !rmsss$sign[1, , ],
            color_fun = ClimPalette("yellowred"),
            brks = 21, bar_limits = c(-1, 1),
            style = 'point', drawleg = 'bar',
            toptitle = "RMSSS SEAS5-ERA5 monthly tas anomaly Nov 2011-2016", 
            caption = "Projection: Mollweide ESRI:54009",
            width = 10, height = 6,
            fileout = 'map_robinson_rmsss_global_moll.png')


```
<img src="./Figures/map_robinson_rmsss_global_moll.png" width="600" />

Check how to plot arctic polar stereographic projection (EPSG:3995) in [Map: Stereographic Projection](map_proj_stereo.md#vizrobinson)

## European region

Load the European region data with vignette [Data Loading](data_loading.md) by assigin `region = "euro"` in the loading script.

```r
exp1_eu <- ClimProjDiags::Subset(exp_eu, c("dat", "var"), list(1, 1), drop = "selected")
exp1_eu <- s2dv::MeanDims(exp1_eu, "ensemble")
obs1_eu <- ClimProjDiags::Subset(obs_eu, c("dat", "var"), list(1, 1), drop = "selected")

# Calculate anomalies and trend
clim_eu <- s2dv::Clim(exp1_eu, obs1_eu, dat_dim = NULL, memb_dim = NULL)
ano_exp_eu <- s2dv::Ano(exp1_eu, clim_eu$clim_exp)
ano_obs_eu <- s2dv::Ano(obs1_eu, clim_eu$clim_obs)

# Calculate RMSSS
rmsss_eu <- s2dv::RMSSS(ano_exp_eu, ano_obs_eu, 
                        time_dim = 'sdate', dat_dim = NULL, 
                        pval = FALSE, sign = TRUE, alpha = 0.05, 
                        sig_method = "Random Walk", sig_method.type = "two.sided")

```

The parameter `crop_coastlines` helps to adjust the map coastline plotting. You can test and find the best range. Note that this parameter is not suitable for the case that longitude range exceeds half of the earth (i.e., 180 degrees.)

We can also choose to plot the map by polygon instead of point. Note that polygon is relatively **slow** so it takes a long time for global map.

Note that `target_proj = 102014` does not work well on Nord3v2. Use PROJ.4 string `target_proj = "+proj=lcc +lat_0=30 +lon_0=10 +lat_1=43 +lat_2=62 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs +type=crs"` instead.

```r
lats_eu <- attributes(exp_eu)$Variables$system5c3s$lat
lons_eu <- attributes(exp_eu)$Variables$system5c3s$lon

## style = point
VizRobinson(rmsss_eu$rmsss[1, , ], lon = lons_eu, lat = lats_eu,
            target_proj = "ESRI:102014",
            style = 'point', drawleg = 'bar',
            mask = !rmsss_eu$sign[1, , ],
            brks = 21, bar_limits = c(-0.8, 0.8),
            bar_extra_margin = c(0.6, 3, 0.6, 3),
            point_size = 1.3,
            crop_coastlines = c(lonmin = -20, lonmax = 40, latmin = 27, latmax = 70),
            toptitle = "RMSSS SEAS5-ERA5 tas Nov 1997-2016", 
            caption = paste0("Projection: Lambert Europe\nESRI:102014\n", "Europe"),
            vertical = F, width = 6, height = 6,
            fileout = 'map_robinson_rmsss_euro_lambert_point.png')

## style = polygon
VizRobinson(rmsss_eu$rmsss[1, , ], lon = lons_eu, lat = lats_eu,
            target_proj = "ESRI:102014",
            style = 'polygon', drawleg = 'bar',
            mask = !rmsss_eu$sign[1, , ],
            brks = 21, bar_limits = c(-0.8, 0.8),
            bar_extra_margin = c(0.6, 3, 0.6, 3),
            point_size = 1.3,
            crop_coastlines = c(lonmin = -20, lonmax = 40, latmin = 27, latmax = 70),
            toptitle = "RMSSS SEAS5-ERA5 tas Nov 1997-2016", 
            caption = paste0("Projection: Lambert Europe\nESRI:102014\n", "Europe"),
            vertical = F, width = 6, height = 6,
            fileout = 'map_robinson_rmsss_euro_lambert_poly.png')
```

<img src="./Figures/map_robinson_rmsss_euro_lambert_point.png" width="450" />

<img src="./Figures/map_robinson_rmsss_euro_lambert_poly.png" width="450" />
