-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_grib_to_cog.R
112 lines (79 loc) · 2.4 KB
/
aws_grib_to_cog.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
library(tidyverse)
library(sf)
library(terra)
library(exactextractr)
library(glue)
library(aws.s3)
source("R/cloud_storage_tools.R")
source("R/grib_tools.R")
run_date <- Sys.Date()
run_mo <- format(run_date, "%Y-%m")
# returns a nicely formatted bucket
bucket_df <- get_formatted_bucket_df(
bucket = Sys.getenv("BUCKET_NAME")
)
# get the bigger files w/ the names of current month included
bucket_df_filt <- bucket_df %>%
filter(size_mb > 12) %>%
filter(str_detect(date, run_mo))
# Load Gribs --------------------------------------------------------------
# loop through gribs, and read with rast()
td <- file.path(tempdir(), "ecmwf_gribs")
lr <- map2(
bucket_df_filt$Key, bucket_df_filt$filename,
\(keytmp, fntmp){
fn <- file.path(
td,
fntmp
)
save_object(bucket = Sys.getenv("BUCKET_NAME"), object = keytmp, file = fn, overwrite = T)
r <- rast(fn)
return(r)
}
)
# Handle Grib MetaData ----------------------------------------------------
grib_files <- list.files(td, full.names = T)
grib_files_filt <- str_subset(grib_files, pattern = run_mo)
lr <- load_mf_ensemble_mean(file_paths = grib_files_filt)
r <- rast(lr)
# Write Rasters to Storage ------------------------------------------------
td_cog <- file.path(tempdir(), "ecmwf_cogs")
pub_date <- floor_date(Sys.Date(), "month")
prefix_cog_name <- format(pub_date, "%Y%m")
cog_name <- paste0(prefix_cog_name, "-ECMWF_SEAS-V_i_a-ensemble_mean.tif")
# this doesn't work, I believe due to GDAL (at least my version) handling of temp paths
tf_cog <- file.path(td_cog, cog_name)
# therefore I will just write to working directory and delete
# write that to COG
terra::writeRaster(
r,
filename = cog_name,
# filename = tf_cog, # someone else can see if this works, but not for me
filetype = "COG",
gdal = c(
"COMPRESS=DEFLATE",
"SPARSE_OK=YES",
"OVERVIEW_RESAMPLING=AVERAGE"
), overwrite = T
)
## AWS Bucket ####
aws.s3::put_object(
file = cog_name,
object = file.path(
"ECMWF_COGS",
cog_name
),
bucket = Sys.getenv("BUCKET_NAME")
)
## Azure Blob ####
es <- azure_endpoint_url()
# storage endpoint
se <- AzureStor::storage_endpoint(es, sas = Sys.getenv("DSCI_AZ_SAS_DEV"))
sc_global <- AzureStor::storage_container(se, "global")
AzureStor::upload_blob(
container = sc_global,
src = cog_name,
dest = paste0("raster/cogs/",cog_name)
)
unlink(cog_name)
unlink(paste0(cog_name, ".aux.json"))