-
Notifications
You must be signed in to change notification settings - Fork 2
/
ebv_datacubepaths.R
274 lines (255 loc) · 8.1 KB
/
ebv_datacubepaths.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#' Get datacubepaths of EBV netCDF
#' @description Get the paths to the datacubes of the EBV netCDF to access the
#' data.
#'
#' @param filepath Character. Path to the netCDF file.
#' @param verbose Logical. Default: TRUE. Turn off additional prints by setting
#' it to FALSE.
#'
#' @return Dataframe containing the paths to access the datacubes and
#' descriptions of scenario, metric and entity if existing.
#' @export
#'
#' @examples
#' #set path to EBV netCDF
#' file <- system.file(file.path("extdata","martins_comcom_subset.nc"), package="ebvcube")
#'
#' #get all datacubepaths of EBV netCDF
#' datacubes <- ebv_datacubepaths(file)
ebv_datacubepaths <- function(filepath, verbose = TRUE){
####initial tests start ----
# ensure file and all datahandles are closed on exit
withr::defer(
if(exists('hdf')){
if(rhdf5::H5Iis_valid(hdf)==TRUE){rhdf5::H5Fclose(hdf)}
}
)
withr::defer(
if(exists('gid')){
if(rhdf5::H5Iis_valid(gid)==TRUE){rhdf5::H5Gclose(gid)}
}
)
#are all arguments given?
if(missing(filepath)){
stop('Filepath argument is missing.')
}
#check verbose
if(checkmate::checkLogical(verbose, len=1, any.missing=FALSE) != TRUE){
stop('Verbose must be of type logical.')
}
#filepath check
if (checkmate::checkCharacter(filepath) != TRUE){
stop('Filepath must be of type character.')
}
if (checkmate::checkFileExists(filepath) != TRUE){
stop(paste0('File does not exist.\n', filepath))
}
if (!endsWith(filepath, '.nc')){
stop(paste0('File ending is wrong. File cannot be processed.'))
}
#######initial test end ----
#file overview
dump <- rhdf5::h5dump(filepath, load=FALSE, recursive=FALSE)
#check structure
if('entity' %in% names(dump)){
new <- TRUE
} else{
new <- FALSE
}
#check 4D or 3D
is_4D <- ebv_i_4D(filepath)
#get all datasets ----
remove <- c('crs', 'dim_entity', 'lat', 'lon', 'crs', 'time', 'var_entity',
'entities', 'entity', 'nchar', 'nchar_entity', 'entity_levels',
'entity_list', 'nchar_taxonlist', 'taxonlevel', 'nchar_lsid',
'entity_lsid')
for (r in remove){
dump[[r]] <- NULL
}
#get paths ----
datacubepaths <- c()
dump <- names(dump)
#scenario and metrics
if('scenario_1' %in% dump){
hdf <- rhdf5::H5Fopen(filepath, flags = "H5F_ACC_RDONLY")
gid <- rhdf5::H5Gopen(hdf, 'scenario_1')
dump_m <- names(rhdf5::h5dump(gid, load=FALSE, recursive=FALSE))
rhdf5::H5Fclose(hdf)
rhdf5::H5Gclose(gid)
for (s in dump){
for (m in dump_m){
p <- paste0(s, '/', m, '/ebv_cube')
datacubepaths <- c(datacubepaths, p)
}
}
}else{
#only metric
for (e in dump){
p <- paste0(e, '/ebv_cube')
datacubepaths <- c(datacubepaths, p)
}
}
#open file
hdf <- rhdf5::H5Fopen(filepath, flags="H5F_ACC_RDONLY")
#ebv_subgroups
if (! new){
subgroups <- ebv_i_read_att(hdf, 'ebv_subgroups', verbose)
} else{
subgroups <- c()
}
if(!is_4D){
#process 3D----
entity_names <- c()
#scenario and metric ----
if(('scenario' %in% subgroups)|| stringr::str_detect(datacubepaths[1], 'scenario')){
scenario_names <- c()
metric_names <- c()
for (p in datacubepaths){
#entity longname
dh <- hdf&p
if (rhdf5::H5Aexists(dh, 'label')){
e <- ebv_i_read_att(dh, 'label')
} else if (rhdf5::H5Aexists(dh, 'standard_name')) {
e <- ebv_i_read_att(dh, 'standard_name')
} else {
e <- 'not defined'
}
rhdf5::H5Dclose(dh)
#scenario longname
scenario <- stringr::str_split(p, '/')[[1]][1]
dh <- hdf&scenario
if (rhdf5::H5Aexists(dh, 'label')){
s <- ebv_i_read_att(dh, 'label')
} else if (rhdf5::H5Aexists(dh, 'standard_name')) {
s <- ebv_i_read_att(dh, 'standard_name')
} else {
s <- 'not defined'
}
rhdf5::H5Gclose(dh)
#metric longname
metric <- paste0(scenario, '/', stringr::str_split(p, '/')[[1]][2])
dh <- hdf&metric
if (rhdf5::H5Aexists(dh, 'label')){
m <- ebv_i_read_att(dh, 'label')
} else if (rhdf5::H5Aexists(dh, 'standard_name')) {
m <- ebv_i_read_att(dh, 'standard_name')
} else {
m <- 'not defined'
}
rhdf5::H5Gclose(dh)
#collect infos
scenario_names <- c(scenario_names, s)
metric_names <- c(metric_names, m)
entity_names <- c(entity_names, e)
}
#build result data.frame
if(!new || !any(entity_names=='not defined')){
result <- data.frame(datacubepaths, scenario_names, metric_names, entity_names)
} else{
result <- data.frame(datacubepaths, scenario_names, metric_names)
}
} else{
# only metric ----
metric_names <- c()
for (p in datacubepaths){
#entity longname
dh <- hdf&p
if (rhdf5::H5Aexists(dh, 'label')){
e <- ebv_i_read_att(dh, 'label')
} else if (rhdf5::H5Aexists(dh, 'standard_name')) {
e <- ebv_i_read_att(dh, 'standard_name')
} else {
e <- 'not defined'
}
rhdf5::H5Dclose(dh)
#metric longname
metric <- stringr::str_split(p, '/')[[1]][1]
#delete 'if' when hennekens is corrected! - all datasets must have a metric!
if (metric == ''){
m <- 'none'
}else{
dh <- hdf&metric
if (rhdf5::H5Aexists(dh, 'label')){
m <- ebv_i_read_att(dh, 'label')
} else if (rhdf5::H5Aexists(dh, 'standard_name')) {
m <- ebv_i_read_att(dh, 'standard_name')
} else {
m <- 'not defined'
}
rhdf5::H5Gclose(dh)
}
entity_names <- c(entity_names, e)
metric_names <- c(metric_names, m)
}
#build result data.frame
if(!new || ! any(entity_names=='not defined')){
result <- data.frame(datacubepaths, metric_names, entity_names)
} else{
result <- data.frame(datacubepaths, metric_names)
}
}
} else{
#process 4D----
#scenario and metric ----
if(('scenario' %in% subgroups)|| stringr::str_detect(datacubepaths[1], 'scenario')){
scenario_names <- c()
metric_names <- c()
#scenario longname
for (p in datacubepaths){
scenario <- stringr::str_split(p, '/')[[1]][1]
dh <- hdf&scenario
if (rhdf5::H5Aexists(dh, 'label')){
s <- ebv_i_read_att(dh, 'label')
} else if (rhdf5::H5Aexists(dh, 'standard_name')) {
s <- ebv_i_read_att(dh, 'standard_name')
} else {
s <- 'not defined'
}
rhdf5::H5Gclose(dh)
#metric longname
metric <- paste0(scenario, '/', stringr::str_split(p, '/')[[1]][2])
dh <- hdf&metric
if (rhdf5::H5Aexists(dh, 'label')){
m <- ebv_i_read_att(dh, 'label')
} else if (rhdf5::H5Aexists(dh, 'standard_name')) {
m <- ebv_i_read_att(dh, 'standard_name')
} else {
m <- 'not defined'
}
rhdf5::H5Gclose(dh)
#collect infos
scenario_names <- c(scenario_names, s)
metric_names <- c(metric_names, m)
}
#build result data.frame
result <- data.frame(datacubepaths, scenario_names, metric_names)
} else{
# only metric ----
metric_names <- c()
for (p in datacubepaths){
#metric longname
metric <- stringr::str_split(p, '/')[[1]][1]
#delete 'if' when hennekens is corrected! - all datasets must have a metric!
if (metric == ''){
m <- 'none'
}else{
dh <- hdf&metric
if (rhdf5::H5Aexists(dh, 'label')){
m <- ebv_i_read_att(dh, 'label')
} else if (rhdf5::H5Aexists(dh, 'standard_name')) {
m <- ebv_i_read_att(dh, 'standard_name')
} else {
m <- 'not defined'
}
rhdf5::H5Gclose(dh)
}
metric_names <- c(metric_names, m)
}
#build result data.frame
result <- data.frame(datacubepaths, metric_names)
}
}
#close file
rhdf5::H5Fclose(hdf)
return(result)
}