Tải dữ liệu khí hậu từ Copernicus Climate Data Store

Giới thiệu

Copernicus Climate Data Store là một cơ sở dữ liệu khí hậu mở của Liên minh Châu Âu và được vận hành bởi European Centre for Medium-Range Weather Forecasts. Thông qua Copernicus Climate Data Store, người dùng có thể tiếp cận các bộ dữ liệu khí hậu mới nhất như:

Chuẩn bị

  1. Tải thư viện ecmwfr và các thư viện khác
library(ecmwfr)    # tải dữ liệu cds
library(tidyverse) # xử lý và trực quan hóa dữ liệu
library(sf)        # xử lý dữ liệu không gian dạng vector
library(stars)     # xử lý dữ liệu không gian dạng raster
  1. Tạo tài khoản trên trang European Centre for Medium-Range Weather Forecasts
  2. Đăng nhập ở trang Copernicus Climate Data Store (trang đăng nhập sẽ tự động được điều hướng sang trang European Centre for Medium-Range Weather Forecasts để đăng nhập bằng tài khoản đã tạo ở bước 2).
  3. Lấy mã truy cập dữ liệu (Personal Access Token) ở trang profile của Copernicus Climate Data Store và đặt mã truy cập. Lưu ý tránh để lộ mã truy cập này.
# Đặt mã truy cập (Personal Access Token)
wf_set_key(key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")

Tải dữ liệu ORAS5

  1. Lấy API tải dữ liệu: chọn các thuộc tính của dữ liệu cần tải bao gồm: kiểu dữ liệu (product type, dữ liệu lịch sử trước 2014 [consolidated] hay dữ liệu mô hình từ 2015 [operational]), cấu trúc dữ liệu (vertical resolution, một lớp hay nhiều lớp), biến (variable), năm (year), tháng (month). Để ví dụ thì mình chọn như sau:
    • kiểu dữ liệu: tất cả
    • cấu trúc dữ liệu: một lớp
    • biến: nhiệt độ bề mặt nước biển (sea surface temperature)
    • năm: 2000
    • tháng: 1

  1. Chuyển đổi API tải dữ liệu từ python sang R bằng addins ECMWF Python to list: API tải dữ liệu mặc định trên Copernicus Climate Data Store là dành cho python nên cần phải chuyển sang ngôn ngữ R.

  1. Nhập API tải dữ liệu: có thể đổi tên của file nếu cần
# API mặc định từ ECMWF Python to list, tên file là "TMPFILE"
request <- list(
  dataset_short_name = "reanalysis-oras5",
  product_type = "consolidated",
  vertical_resolution = "single_level",
  variable = "sea_surface_temperature",
  year = "2000",
  month = "01",
  download_format = "zip",
  target = "TMPFILE"
)

# API với tên file "oras5.zip"
request <- list(
  dataset_short_name = "reanalysis-oras5",
  product_type = "consolidated",
  vertical_resolution = "single_level",
  variable = "sea_surface_temperature",
  year = "2000",
  month = "01",
  download_format = "zip",
  target = "oras5.zip"
)
  1. Tải dữ liệu: tùy thuộc vào lượng dữ liệu cần tải (một hay nhiều lớp, biến, năm, tháng) mà dung lượng dữ liệu có thể dao động từ rất nhỏ (vài MB) đến rất lớn (vài hoặc hàng chục GB).
file <- wf_request(
 request  = request,  # API ở bước 3
 transfer = TRUE,     # TRUE để tải file 
 path     = "."       # Thư mục lưu file, "." nghĩa là sẽ lưu ở thư mục làm việc hiện tại  (working directory)
 )

# kiểm tra thư mục làm việc hiện tại
getwd()
  1. Đọc file sau khi đã tải xong: file được giải nén từ oras5.zip. Do file lớn nên mình
oras5 <- read_ncdf("./sosstsst_control_monthly_highres_2D_200001_CONS_v0.1.nc")

oras5
stars object with 3 dimensions and 1 attribute
attribute(s), summary of first 1e+05 cells:
                  Min.   1st Qu.    Median      Mean    3rd Qu.      Max.  NA's
sosstsst [C] -2.039495 -1.878857 -1.844823 -1.365532 -0.6189743 0.1916933 72215
dimension(s):
             from   to  refsys                       values x/y
x               1 1442  WGS 84     [1442x1021] -180,...,180 [x]
y               1 1021  WGS 84 [1442x1021] -77.01,...,89.95 [y]
time_counter    1    1 POSIXct               2000-01-16 UTC    
curvilinear grid
# cắt dữ liệu gần khu vực Việt Nam
oras5_sub <- oras5[, 100:200, 500:600, ]
# 100:200 là thứ tự chiều kinh độ x
# 500:600 là thứ tự chiều vĩ độ y

oras5_sub 
stars object with 3 dimensions and 1 attribute
attribute(s):
                 Min.  1st Qu.   Median     Mean  3rd Qu.     Max. NA's
sosstsst [C] 15.52323 25.36546 26.96291 26.31623 27.95051 29.54972 4115
dimension(s):
             from  to  refsys                   values x/y
x             100 200  WGS 84 [101x101] 97.5,...,122.5 [x]
y             500 600  WGS 84 [101x101] 0.25,...,24.46 [y]
time_counter    1   1 POSIXct           2000-01-16 UTC    
curvilinear grid
ggplot() +
  geom_stars(data = oras5_sub) +
  scale_fill_distiller(palette = "RdYlBu")

Tải dữ liệu ERA5

  1. Lấy API tải dữ liệu và chuyển đổi API tải dữ liệu từ python sang R (tương tự như bước 1-2 ở phần Tải dữ liệu ORAS5). Các lựa chọn thuộc tính của dữ liệu ở ERA5 bao gồm: kiểu dữ liệu (product type, theo tháng hay theo giờ), biến (variable), năm (year), tháng (month), thời gian (nếu dữ liệu theo giờ), phạm vi địa lý, định dạng dữ liệu. Để ví dụ thì mình chọn như sau:

    • kiểu dữ liệu: theo tháng

    • biến: tổng lượng mưa (Total precipitation)

    • năm: 2000

    • tháng: 1-12

    • thời gian: không áp dụng

    • phạm vi địa lý: kinh độ 97.5°-122.5° và vĩ độ 0.26°-24.46° (quanh khu vực Việt Nam tương tự như ở dữ liệu ORAS5)

    • định dạng dữ liệu: NetCDF4 (file .nc giống như ở dữ liệu ORAS5)

  2. Nhập API và tải dữ liệu

request <- list(
  dataset_short_name = "reanalysis-era5-land-monthly-means",
  product_type = "monthly_averaged_reanalysis",
  variable = "total_precipitation",
  year = "2000",
  month = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"),
  time = "00:00",
  data_format = "netcdf",
  download_format = "zip",
  area = c(24.46, 97.5, 0.26, 122.5),
  target = "era5.zip"
)
file <- wf_request(
 request  = request,  # API
 transfer = TRUE,     # TRUE để tải file 
 path     = "."       # Thư mục lưu file
 )
  1. Đọc file (đã được giải nén)
era5 <- read_ncdf("./data_0.nc")

era5
stars object with 3 dimensions and 1 attribute
attribute(s):
               Min.     1st Qu.      Median        Mean     3rd Qu.       Max.   NA's
tp [m] 1.265877e-06 0.002358371 0.006183212 0.006319944 0.009148257 0.04215525 442164
dimension(s):
           from  to offset delta  refsys                    values x/y
longitude     1 251  97.45   0.1  WGS 84                      NULL [x]
latitude      1 243  24.51  -0.1  WGS 84                      NULL [y]
valid_time    1  12     NA    NA POSIXct 2000-01-01,...,2000-12-01    
ggplot() +
  geom_stars(data = era5) +
  scale_fill_distiller(palette = "GnBu", na.value = "white") +
  facet_wrap(~ valid_time)

References