R – Error When Using projectRaster in R

coordinate systemrraster

I'm having a problem with projectRaster on a raster brick in WGS84 to a projected coordinate system in R.

I have obtained some oceanographic data in netCDF format from the Copernicus Marine Environmental Monitoring Service. An example of one of these netCDFs is here.

The CRS (coordinate reference system) of the netCDFs is the WGS84 datum (+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0) and contains 75 depth layers for a single variable (in this case, salinity) for a single month in a single year (in this case, January 1993). I have trimmed the netCDF by both geographical extent and depth (an example of trimmed netCDF here).

I have loaded the trimmed netCDF into R as a raster brick using the following.

library("ncdf4")
ncbr <- brick("../data/env/CMEMS_GLOBAL_REANALYSIS_PHY_001_025_salinity_monthly_mean_1993-01.nclonlat.ncdepthtrim.nc")
ncbr

Which gives the following output

"level" set to 1 (there are 47 levels)class       : RasterBrick 
dimensions  : 97, 113, 10961, 1  (nrow, ncol, ncell, nlayers)
resolution  : 0.25, 0.25  (x, y)
extent      : -71.125, -42.875, 37.875, 62.125  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\xxx\xxx\xxx\xxx\data\env\CMEMS_GLOBAL_REANALYSIS_PHY_001_025_salinity_monthly_mean_1993-01.nclonlat.ncdepthtrim.nc 
names       : X377316 
z-value     : 377316 
varname     : salinity 
level       : 1 

The next step is to project the raster brick into the Canada Albers Equal Area Conic projected coordinate system (+proj=aea +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs). To do this I have used the following code:

canada_eq_ar_proj <- "+proj=aea +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
library("rgdal")
rp <- projectRaster(ncbr, crs = canada_eq_ar_proj)

Which throws me the following error:

Error in .readRowsNetCDF(x = x, row = row, nrows = nrows, col = col, ncols = ncols) : no slot of name "band" for this object of class ".MultipleRasterData"

On Google search I only found one other person who seemed to have this error, but when trying to add a raster layer to a raster brick (so a different problem to mine).

Best Answer

I managed to project a test brick, but your data failed, as you say:

> rp <- projectRaster(ncbr, crs = canada_eq_ar_proj)
Error in .readRowsNetCDF(x = x, row = row, nrows = nrows, col = col, ncols = ncols) : 
  no slot of name "band" for this object of class ".MultipleRasterData"

but you can project the first (and only) layer:

> rp <- projectRaster(ncbr[[1]], crs = canada_eq_ar_proj)
> 

if you have multiple layers you can probably project them all separately and bind them back into a brick if you need it.

Related Question