---
title: "Copernicus Climate Data Store"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{copernicus-cds}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

## 1. Register for CDS services

[https://cds.climate.copernicus.eu/user/register](https://cds.climate.copernicus.eu/user/register)


## 2. Setting Access Rights

Copy `UUID` and `API KEY` from:
[https://cds.climate.copernicus.eu/user](https://cds.climate.copernicus.eu/user)

Add these to your R environment file by calling `usethis::edit_r_environ()`
and add the following two key value pairs in R the environment file:

```{r login, eval = FALSE}
"COPERNICUS_CLIMATE_UUID" = "myuuid"
"COPERNICUS_CLIMATE_APIKEY" = "myapikey"
```

Restart R(Studio) now and set your access details as shown below:


```{r set, eval = FALSE}
ecmwfr::wf_set_key(user = Sys.getenv("COPERNICUS_CLIMATE_UUID"),
                   key = Sys.getenv("COPERNICUS_CLIMATE_APIKEY"),
                   service = "cds")

```


## 4. Create a query

Go to: [https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-single-levels?tab=form](https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-single-levels?tab=form)

to create a query. 

Copy to Rstudio -> mark all -> goto Tools -> Addins -> Browse_Addins -> ecmwfr 
-> Python to Mars -> in order to convert query to R list (see: 
https://cran.r-project.org/web/packages/ecmwfr/vignettes/cds_vignette.html for 
details)


## 5. Run your query

```{r run, eval = FALSE}

request <- list(
    product_type = "reanalysis",
    dataset_short_name = "reanalysis-era5-single-levels",
    file_format = "grib"
    variable = "2m_temperature",
    year = c("2010", "2011", "2012", "2013", "2014", 
             "2015", "2016", "2017", "2018", "2019", "2020"),
    month = c("01", "02", "03", "04", "05", "06", 
              "07", "08", "09", "10", "11", "12"),
    day = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
            "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
            "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"),
    time = c("00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00",
             "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00",
             "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00",
             "21:00", "22:00", "23:00"),
    area = c(40, 116, 39, 117),
    target =  "download.grib")


  ecmwfr::wf_request(request = request,
                     transfer = TRUE,
                     path = export_dir,
                     verbose = FALSE)
```

Take a break and wait until request is finished this may take a few hours. To 
check the progress of the request visit: 
[https://cds.climate.copernicus.eu/cdsapp#!/yourrequests](https://cds.climate.copernicus.eu/cdsapp#!/yourrequests)


### 6. Load raster data

```{r load, eval = FALSE}

grib_file <- "download.grib"

meta <- kwb.satellite::get_metadata_era5(grib_file)

temperature <- raster::raster(grib_file)
```
