多个netcdf文件的合并(matlab)

说明:下载CCMP再分析风场,一个nc文件存储一天的数据,而ECMWF-Interim再分析风场则是一个nc文件存储一个月的数据,有时候我们需要的是一个nc文件里头包含很多个月的数据,这时候就需要把每天或者每月的数据合并到一个nc文件里头了。

工具:matlab 2010b netcdf包

代码如下:

%% 
% desciption: merge multiple netcdf files for sepcific domain

% usage: 
%    1. filenumber is up to the number of your netcdf file to be processed.
%    2. for different domain you want to process, you can change the number
% in the latitude0, longitude0, uwind0, vwind0.

% author:
%    huang xue zhi, dalian university of technology

% revison history
%    2018-09-25 first verison. 

%%

clear;clc;

% begin to merge multiple netcdf files,for example,ccmp wind field reanalysis.

%% batch reading from the netcdf file 

% define the data path and filelist
datadir='/home/hxz/numsim/ind-oce/testnc/ccmp/';
filelist=dir([datadir,'*.nc']);
% define the total numbers of netcdf files to be processed.
filenumber=184;
% batch reading the variable to another arrays.
for i=1:filenumber
    ncid=[datadir,filelist(i).name];
    latitude0=ncread(ncid,'latitude');
    latitude=latitude0(74:435);
    longitude0=ncread(ncid,'longitude');
    longitude=longitude0(80:481);
    time(:,i)=ncread(ncid,'time');
    uwind0(:,:,:,i)=ncread(ncid,'uwnd');
    uwind(:,:,:,i)=uwind0(80:481,74:435,:,i);
    vwind0(:,:,:,i)=ncread(ncid,'vwnd');
    vwind(:,:,:,i)=vwind0(80:481,74:435,:,i);
end
%% end batch reading


%% create the merged netcdf file to store the result.

    cid=netcdf.create('ccmp201505to10.nc','clobber');   
%define global attributes
    netcdf.putAtt(cid,netcdf.getConstant('NC_GLOBAL'),'Conventions','CF-1.6');
    netcdf.putAtt(cid,netcdf.getConstant('NC_GLOBAL'),'geospatial_lat_min','-78.375 degrees');
    netcdf.putAtt(cid,netcdf.getConstant('NC_GLOBAL'),'geospatial_lat_max','78.375 degrees');
    netcdf.putAtt(cid,netcdf.getConstant('NC_GLOBAL'),'geospatial_lon_min','0.125 degrees');
    netcdf.putAtt(cid,netcdf.getConstant('NC_GLOBAL'),'geospatial_lon_max','359.875 degrees');
    netcdf.putAtt(cid,netcdf.getConstant('NC_GLOBAL'),'institution','RSS');
    
% define the variable dimension
    dimlon=netcdf.defDim(cid,'longitude',402);
    dimlat=netcdf.defDim(cid,'latitude',362);
    dimtime=netcdf.defDim(cid,'time',filenumber*4);% 4 hours one day
% end define the dimension
    
% define the variable and their attributes
    varid1=netcdf.defVar(cid,'time','NC_DOUBLE',dimtime);
    netcdf.putAtt(cid,varid1,'standard_name','time');
    netcdf.putAtt(cid,varid1,'long_name','Time of analysis');
    netcdf.putAtt(cid,varid1,'units','hours since 1987-01-01 00:00:00');
    netcdf.putAtt(cid,varid1,'delta_t','0000-00-00 06:00:00');

    varid2=netcdf.defVar(cid,'latitude','NC_FLOAT',dimlat);
    netcdf.putAtt(cid,varid2,'standard_name','time');
    netcdf.putAtt(cid,varid2,'units','degrees_north');
    netcdf.putAtt(cid,varid2,'long_name','Latitude in degrees north');
    netcdf.putAtt(cid,varid2,'_Fillvalue','-9999.0');
    netcdf.putAtt(cid,varid2,'axis','Y');

    
    varid3=netcdf.defVar(cid,'longitude','NC_FLOAT',dimlon);
    netcdf.putAtt(cid,varid3,'standard_name','longitude');
    netcdf.putAtt(cid,varid3,'units','degrees_east');
    netcdf.putAtt(cid,varid3,'long_name','Longitude in degrees east');
    netcdf.putAtt(cid,varid3,'_Fillvalue','-9999.0');
    netcdf.putAtt(cid,varid3,'axis','X');

    
    varid4=netcdf.defVar(cid,'u10','NC_FLOAT',[dimlon dimlat dimtime]);
    netcdf.putAtt(cid,varid4,'standard_name','eastward_wind');
    netcdf.putAtt(cid,varid4,'long_name','u-wind vector component at 10 meters');
    netcdf.putAtt(cid,varid4,'units','m s-1');
    netcdf.putAtt(cid,varid4,'_Fillvalue','-9999.0');
    netcdf.putAtt(cid,varid4,'coordinates','time latitude longitude')

    
    varid5=netcdf.defVar(cid,'v10','NC_FLOAT',[dimlon dimlat dimtime]);  
    netcdf.putAtt(cid,varid5,'standard_name','northward_wind');
    netcdf.putAtt(cid,varid5,'long_name','v-wind vector component at 10 meters');
    netcdf.putAtt(cid,varid5,'units','m s-1');
    netcdf.putAtt(cid,varid5,'_Fillvalue','-9999.0');
    netcdf.putAtt(cid,varid5,'coordinates','time latitude longitude')

    netcdf.endDef(cid);
% end define the varible and attributes


%% write variables value to merged netcdf file
    netcdf.putVar(cid,varid1,time);
    netcdf.putVar(cid,varid2,latitude);
    netcdf.putVar(cid,varid3,longitude);
    netcdf.putVar(cid,varid4,uwind);
    netcdf.putVar(cid,varid5,vwind);
    
    netcdf.close(cid);

代码很粗糙,稍微看一下应该就懂了。

如果觉得有用,请给我点赞哈。

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值