Main Content

导入 NetCDF 文件和 OPeNDAP 数据

本文先后使用 netcdf 包中的高级函数和低级函数读取 NetCDF 文件。

MATLAB NetCDF 功能

网络通用数据表 (NetCDF) 是一组软件库及与机器无关的数据格式,支持创建、访问和共享面向数组的科学数据。NetCDF 广泛用于工程和科学领域,这些领域需要一种标准的数据存储方式,以便共享数据。

MATLAB® 高级函数简化了从 NetCDF 文件或 OPeNDAP NetCDF 数据源导入数据的过程。MATLAB 低级函数通过提供对 NetCDF C 库中例程的访问权限来实现对导入过程的更多控制。要有效地使用低级函数,您应该熟悉 NetCDF C 接口。NetCDF 文档可在 Unidata 网站上获取。

注意

有关导入具有单独的、不兼容格式的通用数据格式 (CDF) 文件的信息,请参阅Import CDF Files Using Low-Level Functions

连接到 OPeNDAP 服务器时的安全注意事项

强烈建议您只连接到受信任的 OPeNDAP 服务器。在 R2020b 中,默认情况下,MATLAB NetCDF 接口仅通过执行服务器证书和主机名验证连接到受信任的数据访问协议 (DAP) 端点。以前,当您访问 OPeNDAP 服务器时,默认情况下服务器证书和主机名验证都处于禁用状态。

如果要禁用服务器证书和主机名验证,请在当前目录的 .dodsrc 文件中添加以下行:

[mylocaltestserver.lab] HTTP.SSL.VALIDATE=0

这样,MATLAB NetCDF 接口会连接到在 URI mylocaltestserver.lab 中指定名称的 OPeNDAP 服务器,而无需对服务器证书或主机名执行任何验证。此更改将在以后的 MATLAB 会话中持续存在。有关 OPeNDAP 服务器身份验证和主机名验证的详细信息,请参阅 netCDF 授权支持

使用高级函数读取 NetCDF 文件

此示例说明如何使用高级函数显示和读取 NetCDF 文件的内容。

显示示例 NetCDF 文件 example.nc 的内容。

ncdisp('example.nc')
Source:
           \\matlabroot\toolbox\matlab\demos\example.nc
Format:
           netcdf4
Global Attributes:
           creation_date = '29-Mar-2010'
Dimensions:
           x = 50
           y = 50
           z = 5
Variables:
    avagadros_number
           Size:       1x1
           Dimensions: 
           Datatype:   double
           Attributes:
                       description = 'this variable has no dimensions'
    temperature     
           Size:       50x1
           Dimensions: x
           Datatype:   int16
           Attributes:
                       scale_factor = 1.8
                       add_offset   = 32
                       units        = 'degrees_fahrenheight'
    peaks           
           Size:       50x50
           Dimensions: x,y
           Datatype:   int16
           Attributes:
                       description = 'z = peaks(50);'
Groups:
    /grid1/
        Attributes:
                   description = 'This is a group attribute.'
        Dimensions:
                   x    = 360
                   y    = 180
                   time = 0     (UNLIMITED)
        Variables:
            temp
                   Size:       []
                   Dimensions: x,y,time
                   Datatype:   int16
    
    /grid2/
        Attributes:
                   description = 'This is another group attribute.'
        Dimensions:
                   x    = 360
                   y    = 180
                   time = 0     (UNLIMITED)
        Variables:
            temp
                   Size:       []
                   Dimensions: x,y,time
                   Datatype:   int16

ncdisp 显示文件中的所有组、维度和变量定义。无限维度用标签 UNLIMITED 标识。

peaks 变量中读取数据。

peaksData  = ncread('example.nc','peaks');

显示有关 peaksData 输出的信息。

whos peaksData
  Name            Size            Bytes  Class    Attributes

  peaksData      50x50             5000  int16  

读取与变量相关联的 description 属性。

peaksDesc  = ncreadatt('example.nc','peaks','description')
peaksDesc =

z = peaks(50);

创建变量数据的三维曲面图。用 description 属性的值作为图标题。

surf(double(peaksData))
title(peaksDesc);

读取与 /grid1/ 组相关联的 description 属性。将组名指定为 ncreadatt 函数的第二个输入。

g = ncreadatt('example.nc','/grid1/','description')
g =

This is a group attribute.

读取全局属性 creation_date。对于全局属性,将 ncreadatt 的第二个输入参数指定为 '/'

creation_date = ncreadatt('example.nc','/','creation_date')
creation_date =

29-Mar-2010

在 NetCDF 文件中查找所有无限维度

此示例说明如何使用高级函数在 NetCDF 文件中查找一个组的所有无限维度。

使用 ncinfo 函数获取示例文件 example.nc/grid2/ 组的相关信息。

ginfo = ncinfo('example.nc','/grid2/')
ginfo = 

      Filename: '\\matlabroot\toolbox\matlab\demos\example.nc'
          Name: 'grid2'
    Dimensions: [1x3 struct]
     Variables: [1x1 struct]
    Attributes: [1x1 struct]
        Groups: []
        Format: 'netcdf4'

ncinfo 返回一个包含组信息的结构体数组。

获取表示该组的无限维度的布尔值的向量。

unlimDims = [ginfo.Dimensions.Unlimited]
unlimDims =

     0     0     1

unlimDims 向量显示无限维度。

disp(ginfo.Dimensions(unlimDims))
         Name: 'time'
       Length: 0
    Unlimited: 1

使用低级函数读取 NetCDF 文件

此示例显示如何使用 netcdf 包中的 MATLAB 低级函数获取有关 NetCDF 文件中的维度、变量和属性的信息。要有效使用这些函数,应该熟悉 NetCDF C 接口。

打开 NetCDF 文件

使用 netcdf.open 函数,以只读访问权限打开示例 NetCDF 文件 example.nc

ncid = netcdf.open('example.nc','NC_NOWRITE')
ncid = 65536

netcdf.open 返回文件标识符。

获取有关 netCDF 文件的信息

使用 netcdf.inq 函数获取有关文件内容的信息。此函数对应于 netCDF 库 C API 中的 nc_inq 函数。

[ndims,nvars,natts,unlimdimID] = netcdf.inq(ncid)
ndims = 3
nvars = 3
natts = 1
unlimdimID = -1

netcdf.inq 返回文件中维度、变量和全局属性的数量,并返回文件中无限维度的标识符。无限维度可以增长。

使用 netcdf.inqAttName 函数获取文件中全局属性的名称。此函数对应于 netCDF 库 C API 中的 nc_inq_attname 函数。要获取属性的名称,必须指定该属性所关联变量的 ID 和属性编号。要访问与特定变量不相关联的全局属性,请使用常量 'NC_GLOBAL' 作为变量 ID。

global_att_name = netcdf.inqAttName(ncid,...
    netcdf.getConstant('NC_GLOBAL'),0)
global_att_name = 
'creation_date'

使用 netcdf.inqAtt 函数获取有关属性的数据类型和长度的信息。此函数对应于 netCDF 库 C API 中的 nc_inq_att 函数。再次使用 netcdf.getConstant('NC_GLOBAL') 指定变量 ID。

[xtype,attlen] = netcdf.inqAtt(ncid,...
    netcdf.getConstant('NC_GLOBAL'),global_att_name)
xtype = 2
attlen = 11

使用 netcdf.getAtt 函数获取属性值。

global_att_value = netcdf.getAtt(ncid,...
    netcdf.getConstant('NC_GLOBAL'),global_att_name)
global_att_value = 
'29-Mar-2010'

使用 netcdf.inqDim 函数获取文件中第一个维度的相关信息。此函数对应于 netCDF 库 C API 中的 nc_inq_dim 函数。netcdf.inqDim 的第二个输入是维度 ID,它是一个标识维度的从 0 开始的索引。第一个维度的索引值为 0

[dimname,dimlen] = netcdf.inqDim(ncid,0)
dimname = 
'x'
dimlen = 50

netcdf.inqDim 返回维度的名称和长度。

使用 netcdf.inqVar 函数获取文件中第一个变量的相关信息。此函数对应于 netCDF 库 C API 中的 nc_inq_var 函数。netcdf.inqVar 的第二个输入是变量 ID,它是一个标识变量的从 0 开始的索引。第一个变量的索引值为 0

[varname,vartype,dimids,natts] = netcdf.inqVar(ncid,0)
varname = 
'avagadros_number'
vartype = 6
dimids =

     []
natts = 1

netcdf.inqVar 返回名称、数据类型、维度 ID 以及与变量相关联的属性的数量。vartype 中返回的数据类型信息是 NetCDF 数据类型常量的数值,例如,NC_INTNC_BYTE。有关这些常量的信息,请参阅 NetCDF 文档。

读取 NetCDF 文件中的数据

使用 netcdf.getVar 函数读取示例文件中与变量 avagadros_number 相关联的数据。netcdf.getVar 的第二个输入是变量 ID,它是一个标识变量的从 0 开始的索引。avagadros_number 变量的索引值为 0

A_number = netcdf.getVar(ncid,0)
A_number = 6.0221e+23

查看 A_number 的数据类型。

whos A_number
  Name          Size            Bytes  Class     Attributes

  A_number      1x1                 8  double              

netcdf 包中的函数自动选择与 NetCDF 数据类型最匹配的 MATLAB 类,但您也可以使用 netcdf.getVar 的可选参数指定返回数据的类。

读取与 avagadros_number 相关联的数据,并以 single 类返回数据。

A_number = netcdf.getVar(ncid,0,'single');
whos A_number
  Name          Size            Bytes  Class     Attributes

  A_number      1x1                 4  single              

关闭 NetCDF 文件

关闭 NetCDF 文件 example.nc

netcdf.close(ncid)

另请参阅

| | |

相关主题

外部网站