I've recently started working with spatial data in R. I am currently working on a project that uses data in both raster and netcdf formats, and I'd be happy to convert the latter to the former, however I have certain doubts about the procedure.
First I tried the very simple approach of just opening the netcdf using stack(), so basically just:
library(raster) filename <- "C:/.../X.nc" pol_st <- stack(filename)
This seems to work fine, I can plot the rasters etc. However, the resulting rasters do not have the proper extent:
> pol_st class : RasterStack dimensions : 576, 992, 571392, 4475 (nrow, ncol, ncell, nlayers) resolution : 1, 1 (x, y) extent : 0.5, 992.5, 0.5, 576.5 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 names : X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, ...
Now, I know the actual extent should be somewhere around 73, 135, 18, 54. I suppose it wouldn't be a problem to change the extent of the raster (right?), but:
- Can I be sure that the projection is correct? (Do I understand correctly that there is no projection in case of netcdf, just an array of longitudes, latitudes and values?)
- I still need to get the exact extent from the netcdf data.
I've tried to recreate the methods shown in those threads, but have difficulties in creating rasters of lat and lon, that is, for:
library(raster) filename <- "C:/.../X.nc" vname <- "lat" pol_st <- stack(filename, varname = vname)
Error in .varName(nc, varname) : varname: lat does not exist in the file. Select one from: NO2
I've tried different names for the variable too, but the "Select one from: NO2" makes it look like the data is missing lon and lat?
Unfortunately I am unable to share the data, but if I run:
library(ncdf4) polnc <- nc_open(filename) polnc
File C:/.../X.nc (NC_FORMAT_NETCDF4_CLASSIC): 3 variables (excluding dimension variables): float latitude[lat] float longitude[lon] float NO2[lon,lat,time] 3 dimensions: lat Size:576 lon Size:992 time Size:4475