How to visualize a map from a netcdf file?

前端 未结 4 1864
醉酒成梦
醉酒成梦 2021-01-03 04:18

I have a netcdf file that I would like to just visualize the soil depth map

   [1] \"file C:\\\\Users\\\\SoilDepth-gswp.nc has 3 dimensions:\"
     [1] \"x           


        
4条回答
  •  旧时难觅i
    2021-01-03 04:33

    library(ncdf)
    # I'm assuming this is the netcdf file you are working with:
    download.file("http://dods.ipsl.jussieu.fr/gswp/Fixed/SoilDepth.nc", destfile="SoilDepth.nc")
    soil <- open.ncdf("SoilDepth.nc")
    #The way to extract a variable is as following:
    soil$var[[3]] -> var3 # here, as shown in your question, SoilDepth is the 3rd variable
    get.var.ncdf(soil, var3) -> SoilDepth
    
    dim(SoilDepth)
    [1] 15238
    

    As was said in the summary for your netcdf file, the variable SoilDepth depends on dimension land only and not on x and y so I'm not sure where does that leave you when it comes to plotting this dataset.

    Edit

    It turns out there is a key that links x, y and land:

    download.file("http://dods.ipsl.jussieu.fr/gswp/Fixed/landmask_gswp.nc", destfile="landmask.nc")
    landmask <- open.ncdf("landmask.nc")
    landmask$var[[3]] -> varland
    get.var.ncdf(landmask, varland) -> land
    sum(land==1)
    [1] 15238
    

    So in order to plot:

    # The values where stored in an expected order, hence the transpose
    land = t(land)
    land[land==1] <- SoilDepth
    land[land==0] <- NA
    land = t(land)
    image(land)
    

    enter image description here

提交回复
热议问题