Matlab拼接(merge)两个nc文件

创建 nc 文件主要分为以下几个步骤
  1. define input and output directories
  2. define dimensions
  3. define variables
  4. read nc files and merge variables(实际中可跳过)
  5. put variables into nc
  6. put attribute into nc
  7. define global attribution

拼接前的 ncdisp 结果为:

>> ncdisp('domain.lnd.140x80pt_ECMWF_noocean.201129_b22.nc')
Source:
           I:\merge_China_srfdata\domain.lnd.140x80pt_ECMWF_noocean.201129_b22.nc
Format:
           classic
Global Attributes:
           title              = 'CESM domain data:'
           Conventions        = 'CF-1.0'
           source_code        = 'SVN $Id: gen_domain.F90 65202 2014-11-06 21:07:45Z mlevy@ucar.edu $'
           SVN_url            = ' $URL: https://svn-ccsm-models.cgd.ucar.edu/tools/mapping/gen_domain/trunk/src/gen_domain.F90 $'
           Compiler_Optimized = 'TRUE'
           hostname           = ''
           history            = 'created by +1.0000000000000000'
           source             = 'map_140x80pt_ECMWF_noocean_to_140x80pt_ECMWF_nomask_aave_da_201129.nc'
           map_domain_a       = '/datahuge/wdy_cesmfiles/cesm_2_2_0/components/clm/tools/mkmapgrids/SCRIPgrid_140x80pt_ECMWF_noocean_c201129.nc'
           map_domain_b       = '/datahuge/wdy_cesmfiles/cesm_2_2_0/components/clm/tools/mkmapgrids/SCRIPgrid_140x80pt_ECMWF_nomask_c201129.nc'
           map_grid_file_ocn  = '/datahuge/wdy_cesmfiles/cesm_2_2_0/components/clm/tools/mkmapgrids/SCRIPgrid_140x80pt_ECMWF_noocean_c201129.nc'
           map_grid_file_atm  = '/datahuge/wdy_cesmfiles/cesm_2_2_0/components/clm/tools/mkmapgrids/SCRIPgrid_140x80pt_ECMWF_nomask_c201129.nc'
Dimensions:
           n  = 11200
           ni = 140
           nj = 80
           nv = 4
Variables:
    xc  
           Size:       140x80
           Dimensions: ni,nj
           Datatype:   double
           Attributes:
                       long_name = 'longitude of grid cell center'
                       units     = 'degrees_east'
                       bounds    = 'xv'
    yc  
           Size:       140x80
           Dimensions: ni,nj
           Datatype:   double
           Attributes:
                       long_name = 'latitude of grid cell center'
                       units     = 'degrees_north'
                       bounds    = 'yv'
    xv  
           Size:       4x140x80
           Dimensions: nv,ni,nj
           Datatype:   double
           Attributes:
                       long_name = 'longitude of grid cell verticies'
                       units     = 'degrees_east'
    yv  
           Size:       4x140x80
           Dimensions: nv,ni,nj
           Datatype:   double
           Attributes:
                       long_name = 'latitude of grid cell verticies'
                       units     = 'degrees_north'
    mask
           Size:       140x80
           Dimensions: ni,nj
           Datatype:   int32
           Attributes:
                       long_name   = 'domain mask'
                       note        = 'unitless'
                       coordinates = 'xc yc'
                       comment     = '0 value indicates cell is not active'
    area
           Size:       140x80
           Dimensions: ni,nj
           Datatype:   double
           Attributes:
                       long_name   = 'area of grid cell in radians squared'
                       coordinates = 'xc yc'
                       units       = 'radian2'
    frac
           Size:       140x80
           Dimensions: ni,nj
           Datatype:   double
           Attributes:
                       long_name   = 'fraction of grid cell that is active'
                       coordinates = 'xc yc'
                       note        = 'unitless'
                       filter1     = 'error if frac> 1.0+eps or frac < 0.0-eps; eps = 0.1000000E-11'
                       filter2     = 'limit frac to [fminval,fmaxval]; fminval= 0.1000000E-02 fmaxval=  1.000000'

拼接要求:将两个140x80的网格,拼成一个280x80的网格区域,原来文件中的所有变量都进行合并。

clear all; clc
% define input and output directories
inDir   = 'I:\merge_China_srfdata';
outDir  = 'I:\merge_China_srfdata';

inFile_b21  = 'domain.lnd.140x80pt_ECMWF_noocean.201128_b21.nc';
inDirFile_b21 = [inDir, '\', inFile_b21];
inFile_b22  = 'domain.lnd.140x80pt_ECMWF_noocean.201129_b22.nc';
inDirFile_b22 = [inDir, '\', inFile_b22];

outFile = 'domain.lnd.280x80pt_South_China_ECMWF_noocean.201201.nc';
outDirFile = [outDir, '\', outFile];
outid = netcdf.create(outFile,'64BIT_OFFSET');

% define dimensions
ndimID  = netcdf.defDim(outid, 'n', 11200);
nidimID = netcdf.defDim(outid, 'ni', 280);
njdimID = netcdf.defDim(outid, 'nj', 80);
nvdimID = netcdf.defDim(outid, 'nv', 4);

% define variables
xc_id   = netcdf.defVar(outid, 'xc', 'NC_DOUBLE', [nidimID, njdimID]);
yc_id   = netcdf.defVar(outid, 'yc', 'NC_DOUBLE', [nidimID, njdimID]);
xv_id   = netcdf.defVar(outid, 'xv', 'NC_DOUBLE', [nvdimID, nidimID, njdimID]);
yv_id   = netcdf.defVar(outid, 'yv', 'NC_DOUBLE', [nvdimID, nidimID, njdimID]);
mask_id = netcdf.defVar(outid, 'mask', 'NC_INT', [nidimID, njdimID]);
area_id = netcdf.defVar(outid, 'area', 'NC_DOUBLE', [nidimID, njdimID]);
frac_id = netcdf.defVar(outid, 'frac', 'NC_DOUBLE', [nidimID, njdimID]);

netcdf.endDef(outid);  % 重要,结束定义模式!

% read nc files and merge variables
% dayon comment:  cat(dimensionID, f01, f02), dimensionID 表示拼接的维度,顺序是从左至右,f01在前,f02在后。
xc_b21 = ncread(inDirFile_b21, 'xc');
xc_b22 = ncread(inDirFile_b22, 'xc');
xc_cmb = cat(1, xc_b21, xc_b22);  

yc_b21 = ncread(inDirFile_b21, 'yc');
yc_b22 = ncread(inDirFile_b22, 'yc');
yc_cmb = cat(1, yc_b21, yc_b22);

xv_b21 = ncread(inDirFile_b21, 'xv');
xv_b22 = ncread(inDirFile_b22, 'xv');
xv_cmb = cat(2, xv_b21, xv_b22);

yv_b21 = ncread(inDirFile_b21, 'yv');
yv_b22 = ncread(inDirFile_b22, 'yv');
yv_cmb = cat(2, yv_b21, yv_b22);

mask_b21 = ncread(inDirFile_b21, 'mask');
mask_b22 = ncread(inDirFile_b22, 'mask');
mask_cmb = cat(1, mask_b21, mask_b22);

area_b21 = ncread(inDirFile_b21, 'area');
area_b22 = ncread(inDirFile_b22, 'area');
area_cmb = cat(1, area_b21, area_b22);

frac_b21 = ncread(inDirFile_b21, 'frac');
frac_b22 = ncread(inDirFile_b22, 'frac');
frac_cmb = cat(1, frac_b21, frac_b22);

% put variables into nc
netcdf.putVar(outid, xc_id, xc_cmb);
netcdf.putVar(outid, yc_id, yc_cmb);
netcdf.putVar(outid, xv_id, xv_cmb);
netcdf.putVar(outid, yv_id, yv_cmb);
netcdf.putVar(outid, mask_id, mask_cmb);
netcdf.putVar(outid, area_id, area_cmb);
netcdf.putVar(outid, frac_id, frac_cmb);

netcdf.reDef(outid); % 重要,重新进入定义模式!

% put attribute into nc
netcdf.putAtt(outid, xc_id, 'longname', 'longitude of grid cell center');
netcdf.putAtt(outid, xc_id, 'units', 'degrees_east');
netcdf.putAtt(outid, xc_id, 'bounds', 'xv');

netcdf.putAtt(outid, yc_id, 'longname', 'latitude of grid cell center');
netcdf.putAtt(outid, yc_id, 'units', 'degrees_north');
netcdf.putAtt(outid, yc_id, 'bounds', 'yv');

netcdf.putAtt(outid, xv_id, 'longname', 'longitude of grid cell verticies');
netcdf.putAtt(outid, xv_id, 'units', 'degrees_east');

netcdf.putAtt(outid, yv_id, 'longname', 'latitude of grid cell verticies');
netcdf.putAtt(outid, yv_id, 'units', 'degrees_north');

netcdf.putAtt(outid, mask_id, 'longname', 'domain mask');
netcdf.putAtt(outid, mask_id, 'note', 'unitless');
netcdf.putAtt(outid, mask_id, 'coordinates', 'xc yc');
netcdf.putAtt(outid, mask_id, 'comment', '0 value indicates cell is not active');

netcdf.putAtt(outid, area_id, 'longname', 'area of grid cell in radians squared');
netcdf.putAtt(outid, area_id, 'coordinates', 'xc yc');
netcdf.putAtt(outid, area_id, 'units', 'radian2');

netcdf.putAtt(outid, frac_id, 'longname', 'fraction of grid cell that is active');
netcdf.putAtt(outid, frac_id, 'coordinates', 'xc yc');
netcdf.putAtt(outid, frac_id, 'note', 'unitless');
netcdf.putAtt(outid, frac_id, 'filter1', 'error if frac> 1.0+eps or frac < 0.0-eps; eps = 0.1000000E-11');
netcdf.putAtt(outid, frac_id, 'filter2', 'limit frac to [fminval,fmaxval]; fminval= 0.1000000E-02 fmaxval=  1.000000');

% define global attributions
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'title', 'CESM domain data');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Conventions', 'CF-1.0');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'source_code', 'SVN $Id: gen_domain.F90 65202 2014-11-06 21:07:45Z mlevy@ucar.edu $');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'SVN_url', '$URL: https://svn-ccsm-models.cgd.ucar.edu/tools/mapping/gen_domain/trunk/src/gen_domain.F90 $');
netcdf.putAtt(outid, netcdf.geetConstant('NC_GLOBAL'), 'Compiler_Optimized', 'TRUE');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'hostname', '');

netcdf.close(outid);
合并多个nc文件,可以使用MATLAB中的ncread和ncwrite函数。以下是一个示例代码,演示如何读取多个nc文件,并将它们合并成一个单独的nc文件。 ```matlab % 输入文件名 filename1 = 'file1.nc'; filename2 = 'file2.nc'; filename3 = 'file3.nc'; output_filename = 'merged_file.nc'; % 读取第一个文件的变量名和维度信息 vars1 = ncread(filename1, 'var'); dims1 = ncread(filename1, 'dim'); % 创建新文件并写入第一个文件的变量和维度信息 ncid = netcdf.create(output_filename, 'NC_WRITE'); for i = 1:length(dims1) dimid(i) = netcdf.defDim(ncid, dims1{i}, length(ncread(filename1, dims1{i}))); end for i = 1:length(vars1) varid(i) = netcdf.defVar(ncid, vars1{i}, 'double', dimid); end netcdf.endDef(ncid); for i = 1:length(vars1) netcdf.putVar(ncid, varid(i), ncread(filename1, vars1{i})); end % 读取并追加第二个和第三个文件的变量和维度信息 vars2 = ncread(filename2, 'var'); dims2 = ncread(filename2, 'dim'); vars3 = ncread(filename3, 'var'); dims3 = ncread(filename3, 'dim'); for i = 1:length(dims2) if ~ismember(dims2{i}, dims1) dimid(length(dims1)+i) = netcdf.defDim(ncid, dims2{i}, length(ncread(filename2, dims2{i}))); end end for i = 1:length(vars2) if ~ismember(vars2{i}, vars1) varid(length(vars1)+i) = netcdf.defVar(ncid, vars2{i}, 'double', dimid(length(dims1)+find(strcmp(dims2, ncread(filename2, vars2{i}))))); end end for i = 1:length(dims3) if ~ismember(dims3{i}, [dims1 dims2]) dimid(length(dims1)+length(dims2)+i) = netcdf.defDim(ncid, dims3{i}, length(ncread(filename3, dims3{i}))); end end for i = 1:length(vars3) if ~ismember(vars3{i}, [vars1 vars2]) varid(length(vars1)+length(vars2)+i) = netcdf.defVar(ncid, vars3{i}, 'double', dimid(length(dims1)+length(dims2)+find(strcmp(dims3, ncread(filename3, vars3{i}))))); end end netcdf.endDef(ncid); for i = 1:length(vars2) netcdf.putVar(ncid, varid(length(vars1)+i), ncread(filename2, vars2{i})); end for i = 1:length(vars3) netcdf.putVar(ncid, varid(length(vars1)+length(vars2)+i), ncread(filename3, vars3{i})); end % 关闭文件 netcdf.close(ncid); ``` 在这个示例代码中,我们首先读取第一个nc文件的变量名和维度信息,并创建一个新的nc文件。然后,我们读取第二个和第三个nc文件的变量和维度信息,并将它们追加到新文件中。注意,我们需要检查变量和维度是否已经存在于新文件中,如果存在,就不需要重复定义。最后,我们将所有变量写入新文件中,并关闭文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大雨海深

感谢您的支持和鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值