Search code examples
rrasternetcdf

Read Netcdf sub categories and convert to grid


I also posted this question on stack gis 1. From the netcdf4 data that have sub categories, I want to be able to read "Retrieval/fs" variable. I also want to read them and convert to raster girds, but it seems that raster doesn't support netcdf4. I appreciate any suggestions.

library(ncdf4)
library(raster)
file <- "http://140906_B7101Ar_150909171225s.nc4"

names(file$var)
"latitude" ... "longitude"... "Retrieval/fs"

lat <- raster(file, varname="latitude")
lon <- raster(file, varname="longitude")

Error in (function (classes, fdef, mtable)  :
unable to find an inherited method for function ‘raster’ for signature ‘"ncdf4"’

Solution

  • Here is the answer to the question I asked. Since the data is not gridded, I retrieve the lon and lat information along with the variables to create a dataframe.

    fs <- ncvar_get(ncfile, "Retrieval/fs")
    xlon <- ncvar_get(ncfile, "longitude")
    xlat <- ncvar_get(ncfile, "latitude")
    d <- data.frame( as.vector(xlon),as.vector(xlat), as.vector(fs))# create a dataframe
    coordinates(d) <- c("xlon","xlat")
    proj4string(d) <- CRS("+proj=longlat") 
    spoint <- SpatialPoints(coords = d) #create a spatial point object