Chapter 9 microclim

9.1 How to use microclim

  1. Go to the figshare repository.

  2. Select the dataset (zip file) you are looking for from the list and click it.

  3. Click on the down arrow to download the data.

  4. Open the file using nc <- nc_open("filename") and extract the values using ncvar_get(nc). Pay attention to the dimension of the data. Oftentimes, data are arranged as array[longitude, latitude, time]. nc$dim$longitude$vals and nc$dim$latitude$vals let you see the values of longitude and latitude respectively, which are necessary to obtain the index of the coordinates you want.

9.2 Code example

Getting solar radiation in Maricao Forest, Puerto Rico (-67°, 18.15°) for January, 2017

Download solar_radiation_Wm2.zip from here.

nc <- nc_open(PATH_TO_NCFILE)
ncvar <- ncvar_get(nc)
# dimension: (lon, lat, hour) = (2159, 852, 24)
  
lonInd <- match.closest(-67, nc$dim$longitude$vals)
lat <- sort(nc$dim$latitude$vals)[match.closest(18.15, sort(nc$dim$latitude$vals))]
latInd <- match(lat, nc$dim$latitude$vals)
  
array <- c()
for (i in 1:24) {
  array <- c(array, ncvar[lonInd, latInd, i])
}
  
dates <- rep(paste0("2017-01-15"), 24)
  
df <- data.frame("Date" = dates, 
                 "Hour" = rep(0:23), 
                 "Data" = array)
  
df$Date <- format(as.POSIXct(paste0(df$Date, " ", df$Hour, ":00")), format = "%Y-%m-%d %H:%M")