reduce size of netcdf file

I merge the binary file and save in NetCDF is this way
clear all
close all
numfiles = 365;
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf('D:\\....\\CMORPH (%d)',k);
if fopen(myfilename)>= 1
fclose('all');
mydata{k} = fread(fopen(myfilename),'float');
end
end
CMO_2001=cat(3,mydata{:});
precip=reshape(CMO_2001,1440,480,365);
clear CMO_2001
%% convert to netcdf
ncfile='CMORPH_CRT_V1.0_0.25_2001.nc';
lon=[0.125:0.25:359.875];
nx=1440;
lat=[-59.875:0.25:59.875];
ny=480;
% Longtitude
nccreate(ncfile,'lon','Dimensions',{'lon',1,nx});
ncwrite(ncfile,'lon',lon);
ncwriteatt(ncfile,'lon','long_name','longitude');
% Latitude
nccreate(ncfile,'lat','Dimensions',{'lat', 1, ny})
ncwrite(ncfile,'lat',lat)
ncwriteatt(ncfile,'lat','long_name','latitude');
% Precipitation
nccreate(ncfile,'time','Dimensions',{'time',1,Inf}) ;
nccreate(ncfile,'precip','Dimensions',{'lon',nx,'lat',ny,'time',Inf})
ncwriteatt(ncfile,'precip','long_name','precipitation');
ncwrite(ncfile,'precip',precip);
It work but the size of NetCDF is 1.9GB. This is heavy to work. How can I reduce the size of netCDF?

Answers (1)

PierreMB
PierreMB on 5 Feb 2020
I would guess that you may have found the answer since then. But, just in case, you can add a 'DeflateLevel' in the arguments of nccreate followed by an integer from 0 to 9 that controls how much compression you want on the data. By default it is set to 0 (for no compression), and you can set it for instance at 2 which can do a fairly good job with fast loading an writing times.
Also, based on the file size you gave us, it tells me that you saved the data using double floats (64 bits) instead of single floats (32 bits) even if you are loading 32 bits floats from your original files. Like the compression level, you can specify the data format in the nccreate arguments with 'DataType' followed by 'single' and make sure that your matrix precip is in single.
You can see for yourself all that information by the documentation by typing "doc nccreate" or by going on the web page of the function : https://www.mathworks.com/help/matlab/ref/nccreate.html

Tags

Asked:

on 18 Jun 2019

Answered:

on 5 Feb 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!