Search code examples
rrasternetcdfr-raster

R: Reading a netCDF file as a raster


I'm trying to read into R a netCDF file as a raster. The netCDF file describes mean annual temperature in the ocean as a function of longitude, latitude, and depth. I'm concerned with the surface ocean (i.e., the first level in the netCDF file) and t_an is the name of the variable in the netCDF file. Therefore, I use the following code:

MyRast <- raster("Temperature.nc", level = 1, varname = "t_an")

This gives me the following warning:

Warning message:
In .getCRSfromGridMap4(atts) : cannot process these parts of the CRS: epsg_code=EPSG:4326

As you can see, the netCDF file has CRS EPSG 4326 (or WGS 84), yet the raster that is created has the following CRS:

+proj=longlat +lon_0=0 +a=6378137 +rf=298.257232666016

Any ideas how I read in the file netCDF with the correct CRS?


Solution

  • All you need to do is setting the projection after you read in the data:

    r <- raster("Temperature.nc",  varname = "t_an")
    proj4string(r)=CRS("+init=EPSG:4326")