Fixing errors associated with R's ncdf "get" function
Reading in data from netcdf files can be a bit of a pain the first few times you have to deal with them - this is rarely helped by error occurrences that mean next to nothing to you. Whilst trying to get some variable data (“z”) from a netcdf file associated with a newly defined R object “my_z” as here:
I was receiving the error:
"Error in mv * 1e-05 : non-numeric argument to binary operator"
The fix for this was found here followed by here and requires a re-definition of the ncdf package function “get.var.ncdf”. As per the original post I found fixing this, follow this protocol so get rid of the error:
- Print out the function get.var.ncdf by typing exactly that in the console
- Copy the results to a new script window
- Redefine the function as getx.var.ncdf
- Find the two places in the function where it says: “ mv <- nc$var[[nc$varid2Rindex[varid]]]$missval “
- Replace each with: mv = -1.000000e+30
- Run the new function: “data1 <- getx.var.ncdf( nc, v1 )” will retrieve the data.
If you want to just copy and paste the “fixed function - now called getx.var.ncdf - you can get a copy here.