@@ -121,10 +121,10 @@ RES_FILE <- c(
121121# ' described below. Default: `"r1i1p1f1"`.
122122# ' If `NULL`, all possible variants are returned.
123123# '
124- # ' * `r`: realization_index (<k>) = realization number (integer >0)
125- # ' * `i`: initialization_index (<l>) = index for variant of initialization method (integer >0)
126- # ' * `p`: physics_index (<m>) = index for model physics variant (integer >0)
127- # ' * `f`: forcing_index (<n>) = index for variant of forcing (integer >0)
124+ # ' * `r`: realization_index (` <k>` ) = realization number (integer >0)
125+ # ' * `i`: initialization_index (` <l>` ) = index for variant of initialization method (integer >0)
126+ # ' * `p`: physics_index (` <m>` ) = index for model physics variant (integer >0)
127+ # ' * `f`: forcing_index (` <n>` ) = index for variant of forcing (integer >0)
128128# '
129129# ' @param replica Whether the record is the "master" copy, or a replica. Use
130130# ' `FALSE` to return only originals and `TRUE` to return only replicas.
@@ -264,55 +264,71 @@ esgf_query <- function(activity = "ScenarioMIP",
264264 variant = " variant_label"
265265 )
266266
267- pair <- function (x , first = FALSE ) {
267+ pair <- function (x , encode = TRUE ) {
268+ checkmate :: assert_vector(x , TRUE , null.ok = TRUE )
269+ 268270 # get name
269271 var <- deparse(substitute(x ))
272+ 270273 # skip if empty
271274 if (is.null(x ) || length(x ) == 0 ) {
272275 return ()
273276 }
274277 # get key name
275278 key <- dict [names(dict ) == var ]
276279 if (! length(key )) key <- var
280+ 277281 if (is.logical(x )) x <- tolower(x )
278- s <- paste0(key , " =" , paste0(x , collapse = " %2C" )) # %2C = ","
279- if (first ) s else paste0(" &" , s )
282+ 283+ if (encode ) x <- query_param_encode(as.character(x ))
284+ 285+ paste0(key , " =" , paste0(x , collapse = query_param_encode(" ," )))
280286 }
281287
282- `%and%` <- function (lhs , rhs ) if (is.null(rhs )) lhs else paste0(lhs , rhs )
288+ `%and%` <- function (lhs , rhs ) {
289+ if (is.null(rhs )) {
290+ lhs
291+ } else if (lhs == url_base ) {
292+ paste(lhs , rhs , sep = " " , collapse = " " )
293+ } else {
294+ paste(lhs , rhs , sep = " &" , collapse = " &" )
295+ }
296+ }
283297
284298 project <- " CMIP6"
285- format <- " application%2Fsolr%2Bjson"
299+ format <- " application/solr+json"
300+ offset <- 0L
286301
287302 resolution <- c(
288303 gsub(" " , " " , resolution , fixed = TRUE ),
289304 gsub(" " , " +" , resolution , fixed = TRUE )
290305 )
291306
307+ # use `fileds` to directly subset data from responses
308+ if (type == " Dataset" ) {
309+ fields <- RES_DATASET
310+ } else if (type == " File" ) {
311+ fields <- RES_FILE
312+ }
313+ 292314 q <- url_base %and %
293- pair(project , TRUE ) %and %
315+ pair(offset ) %and %
316+ pair(limit ) %and %
317+ pair(type ) %and %
318+ pair(replica ) %and %
319+ pair(latest ) %and %
320+ pair(project ) %and %
294321 pair(activity ) %and %
295322 pair(experiment ) %and %
296323 pair(source ) %and %
297324 pair(variable ) %and %
298- pair(resolution ) %and %
325+ pair(resolution , FALSE ) %and %
299326 pair(variant ) %and %
300327 pair(data_node ) %and %
301328 pair(frequency ) %and %
302- pair(replica ) %and %
303- pair(latest ) %and %
304- pair(type ) %and %
305- pair(limit ) %and %
329+ pair(fields ) %and %
306330 pair(format )
307331
308- # use `fileds` to directly subset data from responses
309- if (type == " Dataset" ) {
310- fields <- RES_DATASET
311- } else if (type == " File" ) {
312- fields <- RES_FILE
313- }
314- q <- q %and % pair(fields )
315- 316332 q <- tryCatch(jsonlite :: read_json(q ), warning = function (w ) w , error = function (e ) e )
317333
318334 # nocov start
@@ -573,7 +589,7 @@ init_cmip6_index <- function(activity = "ScenarioMIP",
573589 data.table :: fwrite(dt , file.path(.data_dir(TRUE ), " cmip6_index.csv" ))
574590 verbose(" Data file index saved to '" , normalizePath(file.path(.data_dir(TRUE ), " cmip6_index.csv" )), " '" )
575591
576- EPWSHIFTR_ENV $ index_db <- data.table :: copy(dt )
592+ this $ index_db <- data.table :: copy(dt )
577593 }
578594
579595 dt
@@ -596,10 +612,10 @@ init_cmip6_index <- function(activity = "ScenarioMIP",
596612# ' @importFrom data.table copy fread
597613# ' @export
598614load_cmip6_index <- function (force = FALSE ) {
599- if (is.null(EPWSHIFTR_ENV $ index_db )) force <- TRUE
615+ if (is.null(this $ index_db )) force <- TRUE
600616
601617 if (! force ) {
602- idx <- data.table :: copy(EPWSHIFTR_ENV $ index_db )
618+ idx <- data.table :: copy(this $ index_db )
603619 } else {
604620 f <- normalizePath(file.path(.data_dir(force = FALSE ), " cmip6_index.csv" ), mustWork = FALSE )
605621 if (! file.exists(f )) {
@@ -661,7 +677,7 @@ load_cmip6_index <- function(force = FALSE) {
661677 }
662678
663679 # udpate package internal stored file index
664- EPWSHIFTR_ENV $ index_db <- data.table :: copy(idx )
680+ this $ index_db <- data.table :: copy(idx )
665681
666682 idx []
667683}
@@ -707,7 +723,7 @@ set_cmip6_index <- function(index, save = FALSE) {
707723 }
708724
709725 # udpate package internal stored file index
710- EPWSHIFTR_ENV $ index_db <- data.table :: copy(index )
726+ this $ index_db <- data.table :: copy(index )
711727
712728 invisible (index )
713729}
0 commit comments