【问题标题】:Size of netcdf file is larger after attempt to reduce it with matlab netcdf package尝试使用 matlab netcdf 包减小后,netcdf 文件的大小变大
【发布时间】:2017-01-17 06:47:29
【问题描述】:

我尝试通过将其变量的时间分辨率除以 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。我希望生成一个大小约为原始文件一半的文件,因为我将时间分辨率减半。我不确定这是怎么发生的。

你能解释一下吗?

【问题讨论】:

    标签: matlab file netcdf netcdf4


    【解决方案1】:

    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

    【讨论】:

    • 感谢里奇的回答!
    猜你喜欢
    • 2019-08-08
    • 2019-09-08
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2015-02-21
    • 2018-12-01
    相关资源
    最近更新 更多