LoadDataFile <- function(environment) {
  with(environment, {

  ## In function of the name of the variable and the dataset and other
  ## parameters we build the path to the file, following the rules 
  ## specified in the configuration file.
  dataset_path <- ifelse(dataset_type == 'exp', '$EXP_FULL_PATH$', 
                                                '$OBS_FULL_PATH$')
  filename <- replaceVariablesInString(dataset_path, replace_values)
  ## We obtain the names of all data files in the directory of the 
  ## repository.
  argfiles <- paste('ls', filename)
  files <- system(argfiles, intern = TRUE, ignore.stderr = TRUE)
  
  ## If we do not find any file there, we print a warning and set as file
  ## name 'nofile.nc'. If we find any, we take the last as file name.
  ## Then we set the grid. If the grid to apply is the default grid of  
  ## the experiment we do not apply changes to the data file, so the file 
  ## name remains the same.
  ## If the grid has to be changed, we use 'cdo' for this purpose and 
  ## then save the changes into a temporary file named 'tmp.nc'.
  if (length(files) > 0) {
    filename <- files[length(files)]
    print(filename)

    # TODO: Deactivate regrid always that user selects 'areave' output. Is this fine?
    #       Before only in obs!
    if (output != 'areave' && is_atm_var) {
      if (is.null(grid)) {
        grid <- replaceVariablesInString(dataset_info[3], replace_values)
      }
      system(paste("cdo ", remap, ",", grid, " -selname,", namevar, " ", 
                   filename, " tmp.nc 2>/dev/null", sep = ""))
      system("ncrename -v lat,latitude -d lat,latitude -v lon,longitude -d lon,longitude tmp.nc")
      filein <- "tmp.nc"
    } else {
      filein <- filename
    }
    
    ## We open the file whichever is its name and we load into the 
    ## variable tmp the data of the specified variable it contains.
    fnc <- open.ncdf(filein)
    tmp <- get.var.ncdf(fnc, namevar)
    
    ## We disable large values.
    tmp[which(tmp > 1e19)] <- NA
    tmp[which(tmp < (-1e19))] <- NA
    if (var == 'tos' | var =='tas' | var == 'prlr') { 
      tmp[which(tmp > 599 | tmp < (-999))] <- NA 
    }
    
    ## Now we have to arrange the data in the matrix tmp with the 
    ## dimensions in a common order:
    ##   1) Longitudes 
    ##   2) Latitudes
    ##   3) Members (even if is not a file per member experiment)
    ##   4) Lead-times
    ##
    ## If the variable is 2-dimensional (atmospheric):
    if (is_atm_var) {
      ## TODO: large values
      ##tmp[which(tmp > 1e11)] <- NA
      ##tmp[which(tmp < (-1e33))] <- NA
      ## If the matrix loaded has only 3 dimensions, it means it only 
      ## contains data for one member so the dimension of the members has
      ## length 1 and has been automatically squeezed.
      ## It has to be added.
      if (length(dim(tmp)) == 2) {
        tmp <- InsertDim(tmp, 3, 1)
      }
      if (length(dim(tmp)) == 3) {
        tmp <- InsertDim(tmp, 3, 1)
      }
      ## We read the longitudes and latitudes from the file.
      lat <- get.var.ncdf(fnc, "latitude")
      lon <- get.var.ncdf(fnc, "longitude")
      lon[which(lon < 0)] <- lon[which(lon < 0)] + 360
      ## We re-order the latitudes if needed.
      if (lat[1] < lat[length(lat)]) {
        tmp <- tmp[, length(lat):1, , ]
        if (length(dim(tmp)) == 2) {
          tmp <- InsertDim(tmp, 3, 1)
        }
        if (length(dim(tmp)) == 3) {
          tmp <- InsertDim(tmp, 3, 1)
        }
        lat <- lat[length(lat):1]
      }
      ## TODO: Where is lat_mod and lon_mod?
    ## If the variable is a global mean (oceanic):
    } else {
      ## In this case, the file should contain data in two dimensions: 
      ## members and lead-times.
      ## If any of them does not appear, it is because there was only one 
      ## member or only one lead-time and was automatically squeezed.
      if (is.null(dim(tmp))) {
        tmp <- InsertDim(tmp, 1, 1)
      }
      if (length(dim(tmp)) < 2) {
        tmp <- InsertDim(tmp, 1, 1)
      }
      ## We add two dimensions corresponding to latitudes and longitudes 
      ## to fulfill the common order.
      tmp <- InsertDim(tmp, 1, 1)
      tmp <- InsertDim(tmp, 1, 1)
      ## We set as output longitudes and latitudes one single point 
      ## (0, 0) to avoid a crash.
      if (dataset_type == 'exp') {
        lat_mod <- lon_mod <- 0
      } else {
        lat_obs <- lon_obs <- 0
      }
    }
    close.ncdf(fnc)    
  } else {
    filename <- 'nofile.nc'
  }

  })
}
