Search code examples
rrasternetcdf4

R crashes while opening netcdf file


I downloaded a netcdf file and trying to open it in R. Here's my code

download.file("https://data.giss.nasa.gov/impacts/agmipcf/agmerra/AgMERRA_1980_prate.nc4",destfile = "AgMERRA_1980_prate.nc4", method="libcurl")

I want to open the netcdf file using R

library(ncdf4)
my.file <- nc_open("AgMERRA_1980_prate.nc4")

However, everytime I do this, R crashes.

enter image description here

Is there something wrong in my code or is it something wrong with R studio?

sessionInfo() R version 3.5.0 (2018-04-23) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

EDIT

If I manually download the file, I am able to open it. So there must be something wrong in the way I am downloading it. Any suggestions?


Solution

  • I suspect this question is duplicate of Downloading NetCDF files with R: Manually works, download.file produces error. @Luis's suggestion there of using mode = "wb" rather than the default of mode = "w" was successful in avoiding nc_open() crashes for me with R 4.0.2, RStudio 1.3.959, and ncdf 1.17. wb tells download.file() to treat the file as binary, consistent with the netCDF format.

    For the data of interest here, it would be

    download.file("https://data.giss.nasa.gov/impacts/agmipcf/agmerra/AgMERRA_1980_prate.nc4", destfile = "AgMERRA_1980_prate.nc4", method = "libcurl", mode = "wb")