2

I am trying to extract data from a .nc file. Since there are 7 variables in my file, I want to loop the ncvar_get function through all 7 using foreach.

Here is my code:

 # EXTRACTING CLIMATE DATA FROM NETCDF4 FILE

library(dplyr)
library(data.table)
library(lubridate)
library(ncdf4)
library(parallel)
library(foreach)
library(doParallel)

# SET WORKING DIRECTORY
setwd('/storage/hpc/data/htnb4d/RIPS/UW_climate_data/')

# SETTING UP
cores <- detectCores()
cl <- makeCluster(cores) 
registerDoParallel(cl)

# READING INPUT FILE
infile <- nc_open("force_SERC_8th.1979_2016.nc")
vars <- attributes(infile$var)$names
climvars <- vars[1:7]

# EXTRACTING INFORMATION OF STUDY DOMAIN:
tab <- read.csv('SDGridArea.csv', header = T)
point <- sort(unique(tab$PointID)) #6013 points in the study area

# EXTRACTING DATA (P, TMAX, TMIN, LW, SW AND RH):
clusterEvalQ(cl, {
  library(ncdf4)
})
clusterExport(cl, c('infile','climvars','point'))
foreach(i = climvars) %dopar% {
   climvar <- ncvar_get(infile, varid = i) # all data points 13650 points
   dim <- dim(climvar)
   climMX <- aperm(climvar,c(3,2,1))
   dim(climMX) <- c(dim[3],dim[1]*dim[2])
   climdt <- data.frame(climMX[,point]) #getting 6013 points in the study area
   write.table(climdt,paste0('SD',i,'daily.csv'), sep = ',', row.names = F)
}
stopCluster(cl)

And the error is:

Error in { : task 1 failed - "error returned from C call"
Calls: %dopar% -> <Anonymous>
Execution halted

Could you please explain what is wrong with this code? I assume it has something to do with the fact that the cluster couldn't find out which variable to get from the file, since 'error returned from C call' usually comes from ncvar_get varid argument.

sirandy
  • 1,834
  • 5
  • 27
  • 32
Hai nguyen
  • 43
  • 4

2 Answers2

3

I had the same problem (identical error message) running a similar R script on my MacBook Pro (OSX 10.12.5). The problem seems to be that the different workers from the foreach loop try to access the same .nc file at the same time with ncvar_get. This can be solved by using ncvar_get outside the foreach loop (storing all the data in a big array) and accessing that array from within the foreach loop.

Obviously, another solution would be to appropriately split up the .nc file before and then accessing the different .nc files from within the foreach loop. This should lower memory consumption since copying of the big array to each worker is avoided.

Abdirizak
  • 80
  • 1
  • 7
  • 1
    Thanks for your hint, it worked for me too (although memory consumption is huge for reading all my files in advance). – thiagoveloso Jun 08 '20 at 16:42
0

I had the same issue on a recently acquired work machine. However, the same code runs fine on my home server.

The difference is that on my server I build the netCDF libraries with parallel access enabled (which requires HDF5 compiled with some MPI compiler).

I suspect this feature can prevent the OP's error from happening.

EDIT:

In order to have NetCDF with paralel I/O, first you need to build HDF5 with the following arguments:

./configure --prefix=/opt/software CC=/usr/bin/mpicc CXX=/usr/bin/mpicxx FC=/usr/bin/mpifort

And then, when building the NetCDF C and Fortran libraries, you can also enable tests with the parallel I/O to make sure everything works fine:

./configure -prefix=/opt/software --enable-parallel-tests CC=/usr/bin/mpicc CXX=/usr/bin/mpicxx (C version)

./configure --prefix=/opt/software --enable-parallel-tests CC=/usr/bin/mpicc FC=/usr/bin/mpifort F77=/usr/bin/mpifort (Fortran version)

Of course, in order to do that you need to have some kind of MPI library (MPICH, OpenMPI) installed on your computer.

thiagoveloso
  • 2,537
  • 3
  • 28
  • 57
  • 1
    Hi Thanks a lot for answering my question. Would you mind pointing me to how to build a netCDF libraries with parallel access? – Hai nguyen Mar 05 '21 at 23:38
  • Sure, I've edited my answer to include some useful hints. Feel free to choose it as the best answer if it's helpful to you. – thiagoveloso Mar 06 '21 at 01:19
  • Hi, I actually just posted a question regarding this issue. Could you please take a look? https://stackoverflow.com/questions/66501475/how-to-install-i-o-parallel-for-netcdf4-on-linux – Hai nguyen Mar 06 '21 at 01:21