Created
April 15, 2019 06:58
-
-
Save matteodefelice/b88dacc81ce7ee556753361e8160527b to your computer and use it in GitHub Desktop.
Poor man's correlation between two 3D arrays in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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