Skip to content

Commit d861b25

Browse files
committed
ERA-5: add more metadata (#4), work on time series (#5)
1 parent a86f4fc commit d861b25

7 files changed

+92
-20
lines changed

.github/workflows/pkgdown.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ jobs:
4646
run: |
4747
git config --local user.email "[email protected]"
4848
git config --local user.name "GitHub Actions"
49-
Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)'
49+
Rscript -e 'Rscript -e 'kwb.pkgbuild::deploy_to_branch_with_extra_files(vignettes_file_pattern_to_copy = "\\\.json$|\\\.html$|\\\\\.gif$")''

DESCRIPTION

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ Imports:
3333
tidyr
3434
Suggests:
3535
covr,
36+
cubelyr,
3637
knitr,
38+
kwb.pkgbuild,
3739
raster,
38-
rmarkdown
40+
rmarkdown,
41+
stars
3942
Remotes:
40-
github::kwb-r/kwb.utils
43+
github::kwb-r/kwb.utils,
44+
github::kwb-r/kwb.pkgbuild
4145
VignetteBuilder: knitr

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ importFrom(parallel,detectCores)
1515
importFrom(parallel,makeCluster)
1616
importFrom(parallel,parLapply)
1717
importFrom(parallel,stopCluster)
18+
importFrom(stringr,str_remove)
1819
importFrom(tidyr,pivot_wider)

R/.time-series.R

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
library(kwb.satellite)
2+
3+
grib_file <- "reanalysis-era5-single-levels_total_precipitation_2010-2020.grib"
4+
5+
meta <- kwb.satellite::get_metadata_era5(grib_file)
6+
7+
preci_stars <- stars::read_stars(grib_file)
8+
names(preci_stars) <- "era5_grib"
9+
10+
11+
preci_stars_cube <- cubelyr::as.tbl_cube(preci_stars)
12+
str(preci_stars_cube$dims)
13+
14+
nbands <- length(preci_stars_cube$dims$band)
15+
has_data <- sapply(seq_len(nbands), function(band) {sum(preci_stars_cube$mets[[1]][,,band])>0})
16+
max_data <- sapply(seq_len(nbands), function(band) {max(preci_stars_cube$mets[[1]][,,band])})
17+
18+
bands <- which(has_data)[1:100]
19+
20+
val_max <- ceiling(max(max_data)*1000)
21+
22+
max_col <- 5
23+
24+
cols <- data.frame(breaks = c(0, seq_len(max_col)*val_max/max_col),
25+
col_name = heat.colors(max_col))
26+
27+
animation::saveGIF(expr = for(band in bands) {
28+
label <- sprintf("%s [mm] (%s UTC)", meta$variable[band], meta$time_forecast[band])
29+
preci_stars %>%
30+
dplyr::slice(index = band, along = "band") %>%
31+
### Convert units from [m] to [mm]
32+
dplyr::mutate(era5_grib = era5_grib * 1000) %>%
33+
raster::plot(zlim=c(0,max(max_data)*1000), main = label)
34+
},
35+
movie.name = "era5-precipitation.gif",
36+
interval = 0.1
37+
)
38+
39+
preci_stars_points <- sf::st_as_sf(preci_stars, as_points = TRUE, merge = FALSE)
40+
41+
42+
#summary <- rgdal::GDALinfo(grib_file, silent = TRUE)
43+
44+
# preci_sf <- sf::st_read(grib_file)
45+
# preci <- rgdal::readGDAL(grib_file)
46+
# head(preci@data)
47+
# class(preci)
48+
# raster::image(preci, zlim = c(0, 0.01))
49+
# ggplot2::ggplot(preci)
50+
# image(preci)
51+
# preci_df <- as(preci, "SpatialGridDataFrame")
52+
#
53+
# preci@bbox
54+
#
55+
# plot(preci$band1)

R/get_metadata_era5.R

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#' @importFrom tidyr pivot_wider
99
#' @importFrom gdata startsWith
1010
#' @importFrom dplyr case_when mutate
11+
#' @importFrom stringr str_remove
1112
#' @seealso Code taken from https://gis.stackexchange.com/a/360652
1213
#'
1314
get_metadata_era5 <- function(grib_file) {
@@ -22,6 +23,7 @@ get_metadata_era5 <- function(grib_file) {
2223
"GRIB_ELEMENT",
2324
"GRIB_FORECAST_SECONDS",
2425
"GRIB_REF_TIME",
26+
"GRIB_VALID_TIME",
2527
"GRIB_SHORT_NAME",
2628
"GRIB_UNIT"),
2729
collapse = "|")
@@ -44,6 +46,7 @@ get_metadata_era5 <- function(grib_file) {
4446
is_grib_e <- raw_starts_with('GRIB_ELEMENT')
4547
is_grib_f <- raw_starts_with('GRIB_FORECAST_SECONDS')
4648
is_grib_r <- raw_starts_with('GRIB_REF_TIME')
49+
is_grib_v <- raw_starts_with('GRIB_VALID_TIME')
4750
is_grib_s <- raw_starts_with('GRIB_SHORT_NAME')
4851
is_grib_u <- raw_starts_with('GRIB_UNIT')
4952

@@ -55,17 +58,19 @@ get_metadata_era5 <- function(grib_file) {
5558
is_grib_e ~ sub(".*=", "", raw_),
5659
is_grib_f ~ gsub(".*=(.+) sec.*", "\\1", raw_),
5760
is_grib_r ~ gsub(".*= (.+) sec.*", "\\1", raw_),
61+
is_grib_v ~ gsub(".*= (.+) sec.*", "\\1", raw_),
5862
is_grib_s ~ sub(".*=", "", raw_),
5963
is_grib_u ~ sub(".*=", "", raw_) %>%
6064
stringr::str_remove("\\[") %>%
6165
stringr::str_remove("\\]")
6266
),
6367
column = dplyr::case_when(
6468
is_band ~ 'band',
65-
is_grib_c ~ 'variable',
69+
is_grib_c ~ 'variable_unit',
6670
is_grib_e ~ 'variable_short',
6771
is_grib_f ~ 'forecast_seconds',
68-
is_grib_r ~ 'time',
72+
is_grib_r ~ 'time_ref',
73+
is_grib_v ~ 'time_valid',
6974
is_grib_s ~ 'short_name',
7075
is_grib_u ~ 'unit'
7176
)
@@ -86,8 +91,11 @@ get_metadata_era5 <- function(grib_file) {
8691
values_from = "content"
8792
) %>%
8893
dplyr::mutate(
89-
time = as.POSIXct(as.numeric(time), origin = "1970-01-01", tz = "UTC")
90-
)
94+
time_ref = as.POSIXct(as.numeric(time_ref), origin = "1970-01-01", tz = "UTC"),
95+
time_valid = as.POSIXct(as.numeric(time_valid), origin = "1970-01-01", tz = "UTC"),
96+
time_forecast = time_ref + as.numeric(forecast_seconds),
97+
variable = stringr::str_remove(variable_unit,
98+
pattern = "\\s+\\[.*\\]$"))
9199

92100
grib1 <- grib1[, -1]
93101

man/copernicus_cds.Rd

+8-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/copernicus_cds_parallel.Rd

+9-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)