zoukankan      html  css  js  c++  java
  • 制作nc文件(Matlab)

    首先看一个nc文件中包含哪些部分,例如一个标准的 FVCOM 输入文件 wind.nc

    netcdf wind {
    dimensions:
    	nele = 36858 ;
    	node = 18718 ;
    	time = UNLIMITED ; // (151 currently)
    variables:
    	int iint(time) ;
    		iint:long_name = "internal mode iteration number" ;
    	float time(time) ;
    		time:long_name = "time" ;
    		time:units = "days since 0.0" ;
    		time:time_zone = "none" ;
    	float uwind_speed(time, nele) ;
    		uwind_speed:long_name = "Eastward Wind Speed" ;
    		uwind_speed:standard_name = "Wind Speed" ;
    		uwind_speed:units = "m/s" ;
    		uwind_speed:grid = "fvcom_grid" ;
    		uwind_speed:type = "data" ;
    	float vwind_speed(time, nele) ;
    		vwind_speed:long_name = "Northward Wind Speed" ;
    		vwind_speed:standard_name = "Wind Speed" ;
    		vwind_speed:units = "m/s" ;
    		vwind_speed:grid = "fvcom_grid" ;
    		vwind_speed:type = "data" ;
    
    // global attributes:
    		:title = "wind.nc" ;
    		:institution = "School for Marine Science and Technology" ;
    		:source = "fvcom grid (unstructured) surface forcing" ;
    		:history = "FILE CREATED: Sun Jul 31 13:48:52 2011" ;
    		:references = "http://fvcom.smast.umassd.edu,http://codfish.smast.umassd.edu" ;
    		:Conventions = "CF-1.0" ;
    		:CoordinateSystem = "Cartesian" ;
    		:CoordinateProjection = "none" ;
    }
    

    nc 文件主要内容

    可以看出 nc 文件包含两个主要部分:

    1. dimensions:各个变量维度大小
    2. variables:变量

    dimensions

    在上面文件中包含三个维度:nelenodetime。其中 nelenode 两个是固定长度的,而 time 则是 UNLIMITED,代表其为任意长度。

    variables

    variables 下,每个变量形式为

    float uwind_speed(time, nele)
    

    括号包含了变量的维度,每个维度变量指定了该变量某个维度的长度。

    netcdf 定义中数组是按行排列,因此循环是由右至左。而在matlab中,数组则是按列排列,因此储存的数组会将各个维度顺序交换,即 uwind_speed(nele, time)

    生成nc文件方法

    使用 matlab 的 netcdf 工具箱生成文件时需要按照以下顺序:

    1. 定义文件内维度与变量,包括
    • 定义维度
    • 定义变量
    1. 储存变量

    以生成一个如上的wind.nc文件为例

    定义维度

    首先定义文件中维度:nelenodetime
    注意 time 长度是不固定的。

    %% creat new netcdf file
    
    ncid = netcdf.create('wind.nc','CLOBBER');
    
    % definition 
    ele_dim = netcdf.defDim(ncid,'nele', eleNum);
    node_dim = netcdf.defDim(ncid,'node', nodeNum);
    time_dim = netcdf.defDim(ncid,'time', netcdf.getConstant('NC_UNLIMITED'));
    

    定义维度之后便可根据维度定义变量。
    注意,由于 time 维度是不定长度的,因此其必须作为变量最后一个维度(元素循环由左向右)

    iint_var_id = netcdf.defVar(ncid,'init','int', time_dim);
    time_var_id = netcdf.defVar(ncid,'time','float', time_dim);
    u_var_id = netcdf.defVar(ncid,'uwind_speed','double',[ele_dim, time_dim]);
    v_var_id = netcdf.defVar(ncid,'vwind_speed','double',[ele_dim, time_dim]);
    
    % end definition
    netcdf.endDef(ncid);
    netcdf.close(ncid);
    

    定义完变量之后便可向nc文件中储存数据了。
    在这里需要注意的是,由于matlab 调用 c 语言的 NetCDF函数库,因此在其函数中数组下标是由0开始。(详见 help netcdf

    % write data
    ncid = netcdf.open('wind.nc', 'WRITE');
    netcdf.putVar(ncid, time_var_id, 0, nstep, 1, time(1:nstep));
    

    如上述代码中,0 为起始序号,nstep 为数据总数, 1 为储存数据间隔,time 即使储存在matlab中变量名。

    采用相同方法将 uwind_speedvwind_speed 循环储存在文件内。

    for itime = 1:nstep
        ...
        
        netcdf.putVar(ncid, u_var_id, [0, itime-1], [eleNum, 1], u_interp);
        
        ...
        netcdf.putVar(ncid, v_var_id, [0, itime-1], [eleNum, 1], v_interp);
        
        fprintf('Processing: %f 
    ', itime/nstep);
    end% for
    

    在这里,uwind_speedvwind_speed 是多维数组,因此指定其起始位置也要采用一个向量 [0, itime-1]0 代表 nele 维度起始序号;'itime-1' 则代表 time 维度起始序号)。[eleNum, 1] 为这次要储存的数据占个维度个数,明显我们要储存一整个时间步的数据,所以长度分别为单元个数与时间步数 1。最后 u_interpv_interp 则是变量名。

    结语

    最终,我们来检查下生成的nc文件,使用 matlab 版本为 R2014b,NetCDF 版本号为4.1.3。

    Source:
               /Users/mac/Documents/MATLAB/temp/forZhangNa/FVCOM_wind/wind.nc
    Format:
               classic
    Dimensions:
               nele = 21284
               node = 10951
               time = 3     (UNLIMITED)
    Variables:
        init       
               Size:       3x1
               Dimensions: time
               Datatype:   int32
        time       
               Size:       3x1
               Dimensions: time
               Datatype:   single
        uwind_speed
               Size:       21284x3
               Dimensions: nele,time
               Datatype:   double
        vwind_speed
               Size:       21284x3
               Dimensions: nele,time
               Datatype:   double
    
  • 相关阅读:
    VMware报错:无法连接 MKS: 套接字连接尝试次数太多;正在放弃
    java进阶学习计划
    js实现动态建立表格-------Day59
    Java通过POI技术操作Excel(3)----数据导出
    java通过POI技术操作Excel(2)----模板读取,录入数据
    Form表达元素美化篇----好看的搜索框(1)
    java通过POI技术操作Excel(1)----模板导出
    hibernate中 org.hibernate.MappingException: invalid configuration 报错
    中高级PHP程序员应该掌握哪些技术?
    Win10下Chrome浏览器无法安装 Adobe Flash Player 如何解决
  • 原文地址:https://www.cnblogs.com/li12242/p/5222996.html
Copyright © 2011-2022 走看看