-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patham-env_to_ee.qmd
152 lines (129 loc) · 3.48 KB
/
am-env_to_ee.qmd
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
---
title: "Upload AquaMaps 1/2° env rasters to Earth Engine"
format: html
editor: visual
editor_options:
chunk_output_type: console
---
```{r}
# libraries ----
Sys.setenv(
EARTHENGINE_GCLOUD = "/usr/bin/gcloud",
EARTHENGINE_PYTHON = "/opt/venv/rgee/bin/python",
EARTHENGINE_ENV = "rgee")
if (!"librarian" %in% installed.packages())
install.packages("librarian")
librarian::shelf(
raquamaps/aquamapsdata,
dplyr, glue, here,
rgee, sf, terra)
# variables & paths -----
tif <- here("data/am-hcaf_v1-simple.tif")
```
```{r}
# aquamaps database ----
am_db <- default_db("sqlite") # ~/.config/aquamaps/am.db
# get aquamaps table of env values ----
d <- am_hcaf() |>
select(
hcaf_id = ID,
ctr_lon = CenterLong,
ctr_lat = CenterLat,
FAOArea = FAOAreaM,
Depth = DepthMean,
Temp = SSTAnMean, # surface; TODO: bottom
Salinity = SalinityMean,
PrimProd = PrimProdMean,
IceCon = IceConAnn) |>
collect()
# raster template ----
# range(d$ctr_lon) # -179.75 179.75
# range(d$ctr_lat) # - 78.25 89.75
r_template <- rast(
xmin = min(d$ctr_lon) - 0.25,
xmax = max(d$ctr_lon) + 0.25,
ymin = min(d$ctr_lat) - 0.25,
ymax = max(d$ctr_lat) + 0.25,
resolution = c(0.5, 0.5),
crs = "epsg:4326")
# data frame to points ----
p <- d |>
st_as_sf(
coords = c("ctr_lon", "ctr_lat"),
remove = F,
crs = 4326) |>
arrange(ctr_lon, ctr_lat)
# p[1:25,] |>
# mapview::mapView()
# points to raster ----
get_r <- function(v){
r <- rasterize(p, r_template, field = v)
names(r) <- v
r
}
r <- NULL
for (v in names(d)){
message(glue("v: {v}"))
if (is.null(r)){
r <- get_r(v)
} else {
r <- rast(list(r, get_r(v)))
}
}
# plot(r["ctr_lat"])
writeRaster(r, tif, overwrite=T)
```
```{r}
gcs_bucket <- "eq-am-fine"
r <- rast(tif)
Sys.setenv(
"GCS_DEFAULT_BUCKET" = gcs_bucket,
"GCS_AUTH_FILE" = "/share/data/eq-am-fine-98c68d6f5f96.json")
librarian::shelf(
googleCloudStorageR, jsonlite,
offshorewindhabitat/offhabr, purrr)
# gcs_delete_object(basename(tif))
gcs_upload(
file = tif,
name = basename(tif))
gcs_update_object_acl(
basename(tif), entity_type = "allUsers")
# gcs_to_gee(
# gcs_name = basename(tif),
# gcs_bucket = gcs_bucket,
# gee_asset = "users/ben-ecoquants/aquamaps-downscaled",
# missing_data = NA)
# https://github.com/offshorewindhabitat/offhabr/blob/ac5bb1491ff2a6e6475a2f5d08c9b093af961a9a/R/gcloud.R#L51-L77
gcs_name = basename(tif)
gcs_bucket = gcs_bucket
gee_asset = "projects/eq-am-fine/assets/sdmpredictors"
gee_name = fs::path_ext_remove(gcs_name)
properties = list(
commments = "only sea surface Temp(erature)")
f_json <- tempfile(fileext = ".json")
properties_json <- toJSON(properties, pretty=T, auto_unbox=T)
bands_categorical <- c("hcaf_id", "FAOArea")
bands_json <- tibble(
id = names(r)) |>
mutate(
tileset_band_index = 0:(n() - 1),
pyramiding_policy = map_chr(
id,
\(x) ifelse(
x %in% bands_categorical,
"MODE",
"MEAN"))) |>
toJSON(pretty=T, auto_unbox=T) # cat(bands_json)
glue(
'{{
"name": "{gee_asset}/{gee_name}",
"tilesets":[{{"sources":[{{"uris":["gs://{gcs_bucket}/{gcs_name}"]}}]}}],
"pyramidingPolicy":"MEAN",
"bands": {bands_json},
"properties": {properties_json}
}}') |>
writeLines(f_json)
# readLines(f_json) |> cat()
cmd <- glue::glue("/opt/venv/rgee/bin/earthengine upload image --manifest '{f_json}'")
system(cmd)
```