Chapter 5 GLDAS

5.1 How to use GLDAS

  1. Make an Earthdata account.

  2. Link GES DISC with your account.

  3. Go to this page and select Subset/Get Data in the data access panel on the right.

  4. Under the Download Method, select Get File Subsets using the GES DISC Subsetter.

  5. Refine date range and region and select the variables to download. Keep the file format as netCDF.

  6. Select Get data.

  7. On the pop-up window, select Download links list.

  8. Follow the instructions for one the methods here. wget is fairly straightforward. Follow the methods for To download multiple data files at once… instead of a single file. <your username> and the password are what you set up for the Earthdata account.

5.2 Code example

Getting surface temperatures in Nunn, CO (-104.73°, 40.87°) for June 1-31 in 2017

From this page, download files in netCDF format for AvgSurfT_inst, 2017, June 1-31. Any extent is fine as long as (-104.73°, 40.87°) is included.
Download the Download links list and run wget --load-cookies C:\.urs_cookies --save-cookies C:\.urs_cookies --auth-no-challenge=on --keep-session-cookies --user=<your username> --ask-password --content-disposition -i <url.txt> on wget to download the data.

library(ncdf4)
library(MALDIquant)
library(magrittr)

array <- c()
for (day in 1:31) {
  for (hour in seq(from = 0, to = 21, by = 3)) {
    char_day <- ifelse(day < 10, paste0("0", day), day)
    char_hour <- ifelse(hour < 10, paste0("0", hour), hour)
    filename <- paste0("GLDAS_NOAH025_3H.A201701", char_day, ".", char_hour, "00.021.nc4.SUB.nc4")

    nc <- nc_open(filename)
    ncvar <- ncvar_get(nc, varid = "AvgSurfT_inst")

    lonInd <- match.closest(-104.73, nc$dim$lon$vals)
    lat <- sort(nc$dim$lat$vals)[match.closest(40.87, sort(nc$dim$lat$vals))]
    latInd <- match(lat, nc$dim$lat$vals)
    val <- ncvar[lonInd, latInd]

    array <- c(array, val)
  }
}
array <- array - 273.15 # Converting from kelvin to celcius

offset <- 8 # Data are stored as UCT. So we need adjustment to be aligned to the local time.

roundUp <- ceiling(offset / 3)

days <- c()
for (i in 1:31) {
  days <- c(days, paste0("2017-07-", i))
}

df <- data.frame(Date = rep(days, each = 8),
                 Hour = seq(from = roundUp * 3 - offset, to = 21 + (roundUp * 3 - offset), by = 3))

df <- cbind(df[1 : (31 * 8 - roundUp), ], "Data" = array[(1 + roundUp) : length(array)])

df$Date <- format(as.POSIXct(paste0(df$Date, " ", df$Hour, ":00")), format = "%Y-%m-%d %H:%M")