专注系列化高质量的R语言教程

推文索引 | 联系小编 | 付费合集


netCDF是一种数据储存格式,文件后缀名为.nc,用来储存N维变量,一般是地理数据,例如温度是经度、维度和海拔的函数。netCDF文件可以同时储存一个或多个变量。

在R语言中,可以使用ncdf4工具包来读入、修改和创建netCDF文件。

library(ncdf4)

本篇目录如下:

  • 1 读入数据
  • 1.1 字段
  • 1.2 变量
  • 1.3 缺失值

1 读入数据

对于已有的netCDF文件,可以使用nc_open()函数读取到R环境中(输出类型为ncdf4),使用ncvar_get()函数可以提取相应的变量值(输出类型为基本数据结构array)。

这里使用的示例数据是从网站http://inventory.pku.edu.cn/download/download.html上下载的2014年PM2.5排放总量,数据精度为0.1经度 0.1维度 1月(也可在公众号后台发送关键词示例数据获取)。

## 读取数据
PM25_2014 <- nc_open("182.PM2.5_2014_total.nc")
class(PM25_2014)
## [1] "ncdf4"

## 提取变量
emission <- ncvar_get(PM25_2014, "emission")
class(emission)
## [1] "array"

1.1 字段

nc_open()函数读入的数据是一个复杂的列表结构,它包括如下字段:

  • filename:文件名称;
  • ndims:数据的坐标维度数;这里为3,分别为经度、维度和时间;
  • nvars:数据除维度以外的变量数目;这里为2,分别为格点面积grid_cell_area和PM2.5排放量emission
  • natts:全局属性数目;
  • unlimdimid:长度不固定的维度id;若无则为-1;
  • dim:维度数据;
  • var:维度以外的数据,这里即grid_cell_areaemission
  • writable:文件是否可修改。

PM25_2014有2个变量、3个维度、4个全局属性:

PM25_2014
## File 182.PM2.5_2014_total.nc (NC_FORMAT_CLASSIC):
## 
##      2 variables (excluding dimension variables):
##         float grid_cell_area[lon,lat]   
##             units: km2
##             long_name: Grid cell area (0.1 degree)
##         float emission[lon,lat,time]   
##             units: g ,PM25/km2/month
##             long_name: Gridded emission densities of PM25
## 
##      3 dimensions:
##         lon  Size:3600 
##             long_name: longitude midpoints
##             units: degrees_east
##         lat  Size:1800 
##             long_name: latitude midpoints
##             units: degrees_north
##         time  Size:12 
##             long_name: time
##             units: YYYYMM
##             calendar: Gregorian
## 
##     4 global attributes:
##         title: global monthly 0.1x0.1 emission inventory of PM25 in 2014
##         institution: Laboratory for Earth Surface Processes, College of Urban and Environmental Sciences, Peking University
##         citation: Huang, Y., et al., Quantification of global primary emissions of pm2.5, pm10, and tsp from combustion and industrial process sources. Environ. Sci. Technol. 2014, 48, 13834-13843.
##         projection: Latitude-Longitude gridded data at a 0.1 x 0.1 decimal degrees spatial resolution

1.2 变量

维度变量和非维度变量都可以使用ncvar_get()函数提取。它们的名称可以分别在dimvar中看到:

names(PM25_2014$dim) 
## [1] "lon"  "lat"  "time"

names(PM25_2014$var)
## [1] "grid_cell_area" "emission"

维度变量:

lon <- ncvar_get(PM25_2014, "lon") 

times <- ncvar_get(PM25_2014, "time") 
times
##  [1] 201401 201402 201403 201404 201405 201406 201407 201408 201409 201410
## [11] 201411 201412

非维度变量:

area <- ncvar_get(PM25_2014, "grid_cell_area")

emission <- ncvar_get(PM25_2014, "emission")

ncatt_get()函数可以查询变量的属性值,语法结构如下:

ncatt_get(nc, varid, attname = NA, verbose = FALSE)
  • varid:变量名;varid = 0时查询全局属性;
  • attname:属性名;缺失时查询该变量的所有属性。
## 维度变量的属性
ncatt_get(PM25_2014, "lon") 
## $long_name
## [1] "longitude midpoints"
## 
## $units
## [1] "degrees_east"

## 非维度变量的属性
ncatt_get(PM25_2014, "emission") 
## $units
## [1] "g ,PM25/km2/month"
## 
## $long_name
## [1] "Gridded emission densities of PM25"
ncatt_get(PM25_2014, "emission", "units") 
## $hasatt
## [1] TRUE
## 
## $value
## [1] "g ,PM25/km2/month"

## 全局属性
ncatt_get(PM25_2014, 0, "projection")
## $hasatt
## [1] TRUE
## 
## $value
## [1] "Latitude-Longitude gridded data at a 0.1 x 0.1 decimal degrees spatial resolution"

1.3 缺失值

在R语言中,使用NA表示缺失值,而netCDF一般使用极端值表示缺失值。

例如,emission的字段missval1e+30(),即缺失值:

PM25_2014$var$emission$missval
## [1] 1e+30

关于netCDF数据处理的案例可参见如下推文:

  • 技巧 | 栅格的属性数据和经纬度是分开的两个文件,怎么将它们整合到同一个文件上