Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

raster - Plotting netcdf file with levels in R

I have recently started to work with netcdf in R. Sample data is here:

http://www.earthstat.org/data-download/ > Harvested area and yield for 175 crops > individual crops > soybean_HarvAreaYield2000_NetCDF

In this folder, there is a netcdf file called soybean_AreaYieldProduction.nc

This is how I open the netcdf

 library(ncdf4)

 dat <- nc_open("soybean_AreaYieldProduction.nc")
 print(soy)

1 variables (excluding dimension variables):
    float soybeanData[longitude,latitude,level,time]  
LayerDescriptions: struct(5).Data(:,:,1/2/3/4/5/6) to access data layer: 1=Harvested Area fraction, 2=Yield 3=Harvested Area data quality, 4=Yield data quality, 5=Harvested Area in hectares, 6= Production
        Units: Harvested Area Fraction(1)=percent of gridcell that was harvested, Yield(2)=metric tons per hectare, Harvested Area Hectares(5)=total hectares harvested per gridcell, Production(6)=Metric Tons
        DataQuality: In levels 3 and 4, a value of 1 = county; .75 = state; .5 = interpolated from within 2 degrees lat/long; .25 = country; 0 = missing.
4 dimensions:
        longitude  Size:4320
        units: longitude
        latitude  Size:2160
        units: latitude
        level  Size:6
        time  Size:1

I want to plot each level but do not know how to extract data for each level.

This is how I extract lon and lat data:

lon <- ncvar_get(dat,"longitude") # extract long

lat <- ncvar_get(dat,"latitude") # extract lat

But how do I extract individual levels?

level.1 <- ncvar_get(dat, ????) 

Ultimate aim is to visualize each levels I want to visualize using the following command:

image(lon,lat, level)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is probably easiest to use the raster package:

library(raster)
r1 <- raster("soybean_AreaYieldProduction.nc", level=1)
r2 <- raster("soybean_AreaYieldProduction.nc", level=2)

plot(r1)
image(r1) 
s <- stack(r1, r2)
plot(s)

Other plotting methods

spplot(s)

library(rasterVis)
levelplot(r1) 
levelplot(s)

And see other mapping packages on CRAN


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...