r 打开.nc 文件
标签:
r.ncnetcdf |
分类: SAS_STATA_GAMS_MATLAB_Hadoop |
How to Read Data in netCDF Format with R
The data is in
CF-complaint netCDF format.
This example shows how to import a data file from Tropical Rainfall Measuring Mission (TRMM) 3-hourly precipitation into R.
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
-
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 theinstallation process.)
- Install netCDF enabled R-packages:
ncdf
or
RNetCDF
- Install R-packages for plotting images:
fields:
maptools:
- 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: |
- 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 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 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:
source: |

加载中…