LoadDataFile <- function(environment) {
  # This function loads data and information from a data file into the 
  # environment specified as parameter. It is intended for internal usage
  # from the Load() function.
  # The loaded data is put in a common format, an array with the following
  # dimensions in the following order:
  #   1) Longitudes 
  #   2) Latitudes
  #   3) Members (even if is not a file per member experiment)
  #   4) Lead-times
  #
  # The specified environment must contain the following defined variables:
  #  - dataset_type
  #  - replace_values
  #  - output
  #  - is_atm_var
  #  - grid
  #  - dataset_info
  #  - remap
  #  - namevar
  #  - var
  # and will be extended with the following relevant variables:
  #  - filename
  #  - file_found
  #  - tmp
  #  - lat
  #  - lon
  #  - lat_mod/lat_obs
  #  - lon_mod/lon_obs
  #
  # Args:
  #   environment: Environment to be extended and retrieved data from
  #
  # Returns:
  #   This function returns nothing. Extends the specified environment instead
  #
  # History:
  #   0.1  #  2015-01  (N. Manubens, nicolau.manubens@ic3.cat)  # Original code
  
  with(environment, {

  # The pattern path to the file is built in function of the name of the 
  # variable and the dataset and other parameters, 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)
  # The names of all data files in the directory of the repository that match 
  # the pattern are obtained.
  files <- system(paste('ls', filename), intern = TRUE, ignore.stderr = TRUE)
  
  # If we don't find any, we set the flag 'file_found' with a false value.
  if (length(files) > 0) {
    # The first file that matches the pattern is chosen and read.
    filename <- files[length(files)]
    file_found <- TRUE

    ## TODO: Deactivate regrid always that user selects 'areave' output. Is this fine?
    ##       Before only in obs!
    # The data is regridded if it corresponds to an atmospheric variable. When
    # The chosen output type is 'areave' the data is not regridded to not 
    # waste computing time.
    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
    }
    
    fnc <- open.ncdf(filein)
    tmp <- get.var.ncdf(fnc, namevar)
    
    # Disable of large values.
    ## TODO: How to deal with the disable of large values? New parameter?
    ##       Besides... the limits before were different:
    ##tmp[which(tmp > 1e11)] <- NA
    ##tmp[which(tmp < (-1e33))] <- NA
    tmp[which(tmp > 1e19)] <- NA
    tmp[which(tmp < (-1e19))] <- NA
    if (var == 'tos' | var =='tas' | var == 'prlr') { 
      tmp[which(tmp > 599 | tmp < (-999))] <- NA 
    }
    
    # The data is arranged in the array '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 (is_atm_var) {
      ## 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]
      }
    } 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 {
    file_found <- FALSE
  }

  })
}
