0

我试图通过将其变量的时间分辨率除以 2 来减小 netcdf 文件的大小,方法如下:

infilename = 'original_file.nc4';
outfilename = 'new_file.nc4';
%% CREATE OUTPUT NETCDF FILE
ncid_out = netcdf.create(outfilename,'NETCDF4');
%% OPEN THE INPUT NETCDF FILE
ncid_in  = netcdf.open(infilename,'NOWRITE'); % open original file in read-only mode
[ndims,nvars] = netcdf.inq(ncid_in);
%% DEFINE NEW DIMENSIONS
for d = 0 : ndims-1
    [dimname,dimlen] = netcdf.inqDim(ncid_in,d); % get dimension from input file
    if strcmp(dimname,'time')
        netcdf.defDim(ncid_out,dimname,dimlen/2); % new time dimension with half the resolution
    else netcdf.defDim(ncid_out,dimname,dimlen); % other dimensions remain unchanged
    end
end
%% DEFINE NEW VARIABLES AND ATTRIBUTES
for v = 0 : nvars-1
    [varname,~,dimids] = netcdf.inqVar(ncid_in,v);
    out_varid = netcdf.defVar(ncid_out,varname,xtype,dimids);
    for attnum = 0 : natts-1
        attname = netcdf.inqAttName(ncid_in,v,attnum);
        netcdf.copyAtt(ncid_in,v,attname,ncid_out,out_varid);
    end
end
%% LEAVE DEFINE MODE AND ENTER DATA MODE
netcdf.endDef(ncid_out);
for v = 0 : nvars-1
    [varname,xtype,dimids,natts] = netcdf.inqVar(ncid_in,v);
    var = netcdf.getVar(ncid_in,v);
    out_varid = netcdf.inqVarID(ncid_out,varname);
    if ~isempty(find(dimids==netcdf.inqDimID(ncid_in,'time'),1)) % if time is one of the dimensions
        indt = knnsearch(dimids',netcdf.inqDimID(ncid_in,'time')); % find which one it is
        S = cell(1,length(dimids));
        for f = dimids
            [~,dimlen] = netcdf.inqDim(ncid_in,f); % length of the dimension
            if netcdf.inqDimID(ncid_in,netcdf.inqDim(ncid_out,f)) == dimids(indt) % if this dimension is time
                S{indt} = 1:2:dimlen; % reduce this dimension
            else S{knnsearch(dimids',netcdf.inqDimID(ncid_in,netcdf.inqDim(ncid_in,f)))} = 1:dimlen;
            end
        end
        netcdf.putVar(ncid_out,out_varid,var(S{1:end})); % assign reduced variable
    else netcdf.putVar(ncid_out,out_varid,var); % assign full variable
    end
end
%% CLOSE INPUT AND AND OUTPUT NETCDF FILES
netcdf.close(ncid_in);
netcdf.close(ncid_out);

代码运行没有错误,并且新文件确实包含时间维度为原始时间维度一半的变量。

原始文件的大小是 1.1 Go,但新文件的大小是 1.4 Go。我希望生成一个大小约为原始文件一半的文件,因为我将时间分辨率减半。我不确定这是怎么发生的。

你能解释一下吗?

4

1 回答 1

2

NetCDF4 文件可以使用通缩(无损压缩)来减小大小。你的原始文件可能是用通货紧缩写的,而你写的新文件不是。您需要使用以下命令指定通货紧缩netcdf.defVarDeflate

netcdf.defVarDeflate(ncid,varid,shuffle,deflate,deflateLevel) 

因此,请尝试在通话后添加此行defVar,这将使您的通货紧缩级别为 7,并打开随机播放:

out_varid = netcdf.defVar(ncid_out,varname,xtype,dimids);
netcdf.defVarDeflate(ncid_out,out_varid, true, true, 7);

有关详细信息,请参阅: https ://www.mathworks.com/help/matlab/ref/netcdf.defvardeflate.html?requestedDomain=www.mathworks.com

于 2017-01-16T21:57:14.267 回答