Skip to content

Instantly share code, notes, and snippets.

@matteodefelice
Created April 15, 2019 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteodefelice/b88dacc81ce7ee556753361e8160527b to your computer and use it in GitHub Desktop.
Save matteodefelice/b88dacc81ce7ee556753361e8160527b to your computer and use it in GitHub Desktop.
Poor man's correlation between two 3D arrays in R
library(ncdf4)
# files are 229x161x8760
FILE_1 = nc_open('/emhires-data/data-warehouse/gridded/ERA5/era5-hourly-100m_v_component_of_wind-2018.europe.nc')
FILE_2 = nc_open('/emhires-data/data-warehouse/gridded/ERA5/era5-hourly-10m_v_component_of_wind-2018.europe.nc')
lat = ncvar_get(FILE_1, 'latitude')
lon = ncvar_get(FILE_1, 'longitude')
data_1 = ncvar_get(FILE_1, 'v100') # 2.5 GB in memory
data_2 = ncvar_get(FILE_2, 'v10')
nc_close(FILE_1)
nc_close(FILE_2)
cor_value = matrix(NA, ncol = length(lat), nrow = length(lon))
pb = txtProgressBar(max = length(lon), style = 3)
for (i in seq(1, length(lon))) {
setTxtProgressBar(pb, i)
for (j in seq(1, length(lat))) {
cor_value[i,j] = cor(data_1[i,j,], data_2[i,j,])
}
}
close(pb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment