A pipeline to extract and process Copernicus NetCDF data in a tidy format.
The script is based on the library tydinc. For details of the and tutorial see this blog https://ropensci.org/blog/2019/11/05/tidync/
The examples are adapted from Copernicus Marine Training Workshop for the Mediterranean sea - Edition 2022.
quit
library(tidyverse, quietly = T)
library(tidync)
First download the SST data (or whatever data you are interested in) from the Copernicus Marine Service Catalogue using Python and the code below. You will need to replace your Copernicus login credentials in the code. You can also change the file name and output directory to your needs.
Python can be accessed through Rstudio using the reticulate library.
If you don’t have motuclient in Python, you will need to install it using:
!python -m pip install motuclient==1.8.4 --no-cache-dir
import motuclient
import motuclient
!python -m motuclient --motu https://nrt.cmems-du.eu/motu-web/Motu --service-id MEDSEA_ANALYSISFORECAST_PHY_006_013-TDS --product-id med-cmcc-tem-an-fc-h --longitude-min -17.2917 --longitude-max 36.2917 --latitude-min 30.1875 --latitude-max 45.9792 --date-min "2022-05-22 23:30:00" --date-max "2022-05-22 23:30:00" --depth-min 1.0182 --depth-max 1.0183 --variable thetao --out-dir nc_files --out-name temp_18_june.nc --user jatalah --pwd Estela2012
2022-06-16 04:19:55.322 [ INFO] Asynchronous mode set
2022-06-16 04:19:55.323 [ INFO] Authenticating user jatalah for service https://nrt.cmems-du.eu/motu-web/Motu
2022-06-16 04:19:56.333 [ INFO] Requesting file to download (this can take a while)...
2022-06-16 04:20:01.892 [ INFO] Authenticating user jatalah for service https://nrt.cmems-du.eu/motu-web/Motu
2022-06-16 04:20:08.398 [ INFO] The product is ready for download
2022-06-16 04:20:08.398 [ INFO] Downloading file (this can take a while)...
2022-06-16 04:20:08.616 [ INFO] File type: application/x-netcdf
2022-06-16 04:20:08.616 [ INFO] File size: 2.0 MB (1965744 B)
2022-06-16 04:20:08.616 [ INFO] Downloading file C:\Users\javiera\OneDrive - Cawthron\UA\copernicus_training\nc_files\temp_18_june.nc
2022-06-16 04:20:08.710 [ INFO] - 2.0 MB (3.3%)
2022-06-16 04:20:08.755 [ INFO] - 2.0 MB (6.7%)
2022-06-16 04:20:08.756 [ INFO] - 2.0 MB (10.0%)
2022-06-16 04:20:08.804 [ INFO] - 2.0 MB (13.3%)
2022-06-16 04:20:08.848 [ INFO] - 2.0 MB (16.7%)
2022-06-16 04:20:08.851 [ INFO] - 2.0 MB (20.0%)
2022-06-16 04:20:08.853 [ INFO] - 2.0 MB (23.3%)
2022-06-16 04:20:08.854 [ INFO] - 2.0 MB (26.7%)
2022-06-16 04:20:08.857 [ INFO] - 2.0 MB (30.0%)
2022-06-16 04:20:08.900 [ INFO] - 2.0 MB (33.3%)
2022-06-16 04:20:08.903 [ INFO] - 2.0 MB (36.7%)
2022-06-16 04:20:08.903 [ INFO] - 2.0 MB (40.0%)
2022-06-16 04:20:08.905 [ INFO] - 2.0 MB (43.3%)
2022-06-16 04:20:08.950 [ INFO] - 2.0 MB (46.7%)
2022-06-16 04:20:08.951 [ INFO] - 2.0 MB (50.0%)
2022-06-16 04:20:08.952 [ INFO] - 2.0 MB (53.3%)
2022-06-16 04:20:08.953 [ INFO] - 2.0 MB (56.7%)
2022-06-16 04:20:08.956 [ INFO] - 2.0 MB (60.0%)
2022-06-16 04:20:08.999 [ INFO] - 2.0 MB (63.3%)
2022-06-16 04:20:09.000 [ INFO] - 2.0 MB (66.7%)
2022-06-16 04:20:09.003 [ INFO] - 2.0 MB (70.0%)
2022-06-16 04:20:09.004 [ INFO] - 2.0 MB (73.3%)
2022-06-16 04:20:09.008 [ INFO] - 2.0 MB (76.7%)
2022-06-16 04:20:09.051 [ INFO] - 2.0 MB (80.0%)
2022-06-16 04:20:09.053 [ INFO] - 2.0 MB (83.3%)
2022-06-16 04:20:09.054 [ INFO] - 2.0 MB (86.7%)
2022-06-16 04:20:09.056 [ INFO] - 2.0 MB (90.0%)
2022-06-16 04:20:09.058 [ INFO] - 2.0 MB (93.3%)
2022-06-16 04:20:09.059 [ INFO] - 2.0 MB (96.7%)
2022-06-16 04:20:09.101 [ INFO] - 2.0 MB (100.0%)
2022-06-16 04:20:09.101 [ INFO] Processing time : 0:00:13.294766
2022-06-16 04:20:09.101 [ INFO] Downloading time : 0:00:00.485000
2022-06-16 04:20:09.101 [ INFO] Total time : 0:00:13.779766
2022-06-16 04:20:09.101 [ INFO] Download rate : 2.8 MB/s
2022-06-16 04:20:09.101 [ INFO] Done
sst_may_22 <- tidync('nc_files/temp_22_may.nc')
ncmeta::nc_grids('nc_files/temp_22_may.nc') %>% tidyr::unnest(cols = c(variables))
ncmeta::nc_vars('nc_files/temp_22_may.nc')
NA
ncmeta::nc_atts('nc_files/temp_22_may.nc', "time") %>%
dplyr::select(value) %>%
flatten_df() %>%
pull(units)
[1] "minutes since 1900-01-01 00:00:00"
sst_may_22_t <-
sst_may_22 %>%
hyper_tibble() %>%
mutate(time = lubridate::ymd_hms("1900-01-01 00:00:00", tz = "UTC") + time * 60)
# get countries data for the map
library(rnaturalearth)
library(rnaturalearthdata)
countries <- ne_countries(scale = "medium", returnclass = "sf")
# plot
ggplot() +
# add raster layer
geom_tile(aes(x = lon, y = lat, fill = thetao), data = sst_may_22_t) +
# define color palette of raster layer
scale_fill_viridis_c(option = 'A', name = "°C") +
# add countries layers
geom_sf(
fill = grey(0.9),
color = grey(0.6),
lwd = 0.2,
data = countries
) +
# define spatial extent
coord_sf(
xlim = range(sst_may_22_t$lon),
ylim = range(sst_may_22_t$lat),
expand = F,
ndiscr = 500
) +
labs(title = "Sea Surface Temperature (SST) - Sunday 22 May 2022",
y = "Latitude") +
theme_minimal()
We now use the model data to plot some chlorophyll vertical profiles, to visualize how the chlorophyll concentration changes with depth.
First we read the data and check the time units to correct them.
path <- 'nc_files/mod_bio.nc'
mod_bio_dat <- tidync(path)
ncmeta::nc_grids(path) %>% tidyr::unnest(cols = c(variables))
ncmeta::nc_vars(path)
ncmeta::nc_atts(path, "time") %>%
dplyr::select(value) %>%
flatten_df() %>%
pull(units)
[1] "seconds since 1970-01-01 00:00:00"
Then we define a location and two dates to plot using the hyper_filter function of the tidync library.
mod_bio_dat_t <-
mod_bio_dat %>%
hyper_filter(longitude = between(longitude, 5, 5),
latitude = between(latitude, 42.02, 42.03)) %>%
hyper_tibble() %>%
mutate(time = lubridate::ymd_hms("1970-01-01 00:00:00", tz = "UTC") + time,
month = lubridate::month(time))
We can now plot the vertical profile using ggplot
ggplot(mod_bio_dat_t, aes(depth, chl, color = factor(month))) +
geom_path () +
coord_flip() +
scale_x_reverse() +
labs(x= 'Depth (m)', y = Chl-italic(a)~(mg/m^3), parse = T) +
theme_minimal() +
scale_color_discrete(name = "month")
This scrip is used to plot temperature on a vertical slice along a line.
First we define the coordinates of the vertical slice using the tidync function hyper_filter.
Then we convert it into a tibble suing hyper_tibble and convert the time format.
## plot vertical slice
mod_bio_dat_slice <-
mod_bio_dat %>%
hyper_filter(
longitude = between(longitude, 13.2, 16.5),
latitude = between(latitude, 43.68, 43.7)
) %>%
hyper_tibble() %>%
mutate(
time = lubridate::ymd_hms("1970-01-01 00:00:00", tz = "UTC") + time,
month = lubridate::month(time)
)
Now we geom_tile from the ggplot library to visualize the temperature vertical profile along the vertical slice.