.CheckIndices <- function(x) {
  x
}

.Load <- function(..., pattern_dim = NULL, file_dims = NULL,
                  default_transform  = .CheckIndices) {
                  #, variable_dim = , 
                  #file_dimnames = , return_vars = ) {
  dim_params <- list(...)
  # Split *_var parameters
  var_params_ind <- grep('_var$', names(dim_params))
  var_params <- dim_params[var_params_ind]
  # Check all *_var are NULL or vectors of character strings, and 
  # that they all have a matching dimension param.
  i <- 1
  for (var_param in var_params) {
    if (!is.null(var_param)) {
      if (!is.character(var_param)) {
        stop("All '*_var' parameters must be vectors of character strings or NULL.")
      } else if (!any(grepl(paste0('^', strsplit(names(var_params)[i], 
                                                 '_var$')[[1]][1], '$'),
                            names(dim_params)))) {
        stop(paste0("All '*_var' parameters must be associated to a dimension parameter. Found parameter '", 
                    names(var_params)[i], "' but no parameter '", 
                    strsplit(names(var_params)[i], '_var$')[[1]][1], "'."))
      }
    }
    i <- i + 1
  }
  # Split *_transform parameters
  trans_params_ind <- grep('_transform$', names(dim_params))
  trans_params <- dim_params[trans_params_ind]
  # Check all *_transform are NULL or functions, and that they all have 
  # a matching dimension param.
  i <- 1
  for (trans_param in trans_params) {
    if (!is.null(trans_param)) {
      if (!is.function(trans_param)) {
        stop("All '*_transform' parameters must be functions or NULL.")
      } else if (!any(grepl(paste0('^', strsplit(names(trans_params)[i], 
                                                 '_transform$')[[1]][1], '$'),
                            names(dim_params)))) {
        stop(paste0("All '*_transform' parameters must be associated to a dimension parameter. Found parameter '", 
                    names(trans_params)[i], "' but no parameter '", 
                    strsplit(names(trans_params)[i], '_transform$')[[1]][1], "'."))
      }
    }
    i <- i + 1
  }
  # Leave alone the dimension parameters
  if (length(c(var_params_ind, trans_params_ind)) > 0) {
    dim_params <- dim_params[-c(var_params_ind, trans_params_ind)]
  }
  dim_names <- names(dim_params)
  # Make the keys of 'var_params' and 'trans_params' to be the name of 
  # the corresponding dimension.
  names(var_params) <- gsub('_var$', '', names(var_params))
  names(trans_params) <- gsub('_transform$', '', names(trans_params))

  # Check pattern_dim
  if (is.null(pattern_dim)) {
    print(paste0("Parameter 'pattern_dim' not specified. Taking the first dimension, '", 
                 dim_names[1], "' as 'pattern_dim'."))
    pattern_dim <- dim_names[1]
  } else if (is.character(pattern_dim)) {
    if (length(pattern_dim) > 1) {
      print(paste0("There can be only one 'pattern_dim'. Taking '", pattern_dim[1], 
                   "' as pattern dimension."))
    }
    pattern_dim <- pattern_dim[1]
  } else {
    stop("Parameter 'pattern_dim' must be a character string.")
  }
  # Check all the indices provided for each dimension
  for (dim_name in dim_names) {
    if (dim_name == pattern_dim) {
      if (!is.null(trans_params[[dim_name]])) {
        print(paste0("A transformation for the indices of '", dim_name, 
                     "' has been specified, but it is the pattern dimension and the transformation will be ignored."))
      }
      ## TODO: Check all specifications in pattern_dim are valid
      ##
    } else {
      transformation <- trans_params[[dim_name]]
      if (is.null(transformation)) {
        transformation <- default_transform
      }
      dim_params[[dim_name]] <- transformation(dim_params[[dim_name]])
    }
  }

  # Check file_dims
  if (is.null(file_dims)) {
    file_dims <- pattern_dim
  } else if (is.character(file_dims)) {
    if(any(!(file_dims %in% dim_names))) {
      stop("All names in parameter 'file_dims' must be valid dimension names.")
    }
    if (!(pattern_dim %in% file_dims)) {
      file_dims <- c(file_dims, pattern_dim)
    }
    ## TODO: Order file_dims in function of dim_names
  }

  ## TODO: Replace all file dimensions by * in the first pattern and then
  ## for each file dimension
  ##   replace shell globbing exps and find out how many files for that dimension there are
  ##   call the check indices function
  ## end for
iterate patterns, values of all dimensions outside the file

  list(dim_params, dim_names, var_params, trans_params)
}
