加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

r 打开.nc 文件

(2015-11-26 01:54:03)
标签:

r

.nc

netcdf

分类: SAS_STATA_GAMS_MATLAB_Hadoop

How to Read Data in netCDF Format with R

Overview: 

R  is a software package for statistical computing and graphics. Data in netCDF format can be imported to R by using netCDF-enabled R-packages.  This recipe shows how to import a gridded data file by using either of two R-packages, RNetCDF and ncdf.

Best When: 

The data is in CF-complaint netCDF format.  We suggest the use of  R 3.0 or later. 

Task: 
Viewing Data
Example: 

This example shows how to import a data file from Tropical Rainfall Measuring Mission (TRMM) 3-hourly precipitation into R.

Procedure: 
1) Getting a data file in netCDF format

The original data format of TRMM products is the Hierarchical Data Format (HDF) There are a number of ways to obtain the data as a CF-compliant netCDF file from the Goddard Earth Sciences Data and Information Services Center (GES DISC).  In this example, the 3-hourly TRMM data at 2006-01-01T03:00:00Z was downloaded from OPeNDAP service at the following location:

http://disc2.nascom.nasa.gov/opendap/hyrax/ncml/TRMM_L3/TRMM_3B42/2006/001/3B42.20060101.03.7A.HDF.Z.ncml.html

Clicking on the “Get as NetCDF” button to download the file as:

 nc_3B42.20060101.03.7A.HDF.Z.ncml.nc
2) Installing R-packages 
  • 
    Download and Install R-base first from r-project, if R is not already installed. In this example, Windows R 3.1.2 and Linux R 3.1.2 are used. 
    

(We found that the instructions at the Ohio State University (http://www.stat.osu.edu/computer-support/mathstatistics-packages/installing-r-libraries-locally-your-home-directory) are helpful for the  installation process.)
  • Install netCDF enabled R-packages:

ncdf  This is a R interface for handling netCDF. In this example, ncdf_v1.6.8 is used.   For data in netCDF4, an R-package, ncdf4, is available. 

or

RNetCDF  This is another R interface to handle netCDF . In this example, RNetCDF_v1.6.3-1 is used.

  • Install R-packages for plotting images:

fields:  Tools for handling spatial data. 

maptools Tools for reading and handling spatial objects

 

3) Importing data 
  • Start R
  • Example-1: Read the data with the R-package “RNetCDF”

(Save the following as a R script file )

#Change to your working directory

setwd("C:/Downloads/data")

#Load package RNetCDF

library(RNetCDF)

#Open data file (TRMM 3-hourly )

fname<-"nc_3B42.20060101.03.7A.HDF.Z.nc"

fid<-open.nc(fname)

#Print dataset summary information (see Figure 1)

print.nc(fid)

#Read data

dat<-read.nc(fid)

z<-dat$precipitation

ylat<-dat$nlat

xlon<-dat$nlon

#Close file

close.nc(fid)

 

http://disc.sci.gsfc.nasa.gov/recipes/sites/default/files/media/recipe-R01_fig1(1).png打开.nc 文件" />

Figure 1:  Sample file metadata from R-package “RNetCDF”.

 

  • Example-2: Read data with R-package “ncdf”

(Save the following as a R script file)

#Change to your working directory

setwd("C:/Downloads/data")

#Load package ncdf

library(ncdf)

#Open file ( TRMM 3-hourly )

fname<-"nc_3B42.20060101.03.7A.HDF.Z.nc"

fid1<-open.ncdf(fname)

#Print dataset summary information (see Figure 2)

print.ncdf(fid1)

#Read data

z<-get.var.ncdf(fid1,"precipitation")

ylat<-fid1$dim$nlat$vals

xlon<-fid1$dim$nlon$vals

#Close file

close.ncdf(fid1)

 

http://disc.sci.gsfc.nasa.gov/recipes/sites/default/files/media/recipe-R01_fig2.png打开.nc 文件" />

Figure 2:  Sample file metadata from R-package “ncdf”.

 

4) Plotting an image 

Sample script to plot an image (as in Figure 3):

#Load Plotting package

library(fields)

library(maptools)

data(wrld_simpl)

#Define min/max values to plot and colors for plotting

zmin=0.

zmax=20.

clevs<-c(0,2,4,6,8,10,12,14,16,18,20,50)

ccols<-c("#5D00FF", "#002EFF","#00B9FF","#00FFB9" ,"#00FF2E","#5DFF00","#E8FF00", "#FF8B00","red", "#FF008B","#E800FF")

palette(ccols)

#Plot image  (see result in Figure 3)

par(mfrow=c(1,1))

image.plot(xlon,ylat,t(z), zlim=c(zmin,zmax), breaks=clevs, col=palette(ccols))

plot(wrld_simpl,add=TRUE)

 

To save the image as a PNG file:

par(mfrow=c(1,1))

png("image.png", width=700, height=500)

image.plot(xlon,ylat,t(z), zlim=c(zmin,zmax), breaks=clevs, col=palette(ccols))

plot(wrld_simpl,add=TRUE)

dev.off()

 

http://disc.sci.gsfc.nasa.gov/recipes/sites/default/files/media/recipe-R01_fig3.png打开.nc 文件" />

Figure 3:  Sample screen shot of the R-Console window and the R-Graphics window.


source: http://disc.sci.gsfc.nasa.gov/recipes/?q=recipes/How-to-Read-Data-in-netCDF-Format-with-R

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有