-
Notifications
You must be signed in to change notification settings - Fork 5
/
FUN_nc_easywrite.m
95 lines (82 loc) · 3.52 KB
/
FUN_nc_easywrite.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function FUN_nc_easywrite(filename,varname,data,varargin)
% FUN_nc_easywrite(filename,varname,data,varargin)
% FUN_NC_easywrite(filename,varname,data,[dim_name])
% FUN_NC_easywrite(filename,varname,data,[dim_name],[global_discriptions])
%
% Create a netcdf file containing **one** variable with minimal parameters.
% ** For more complex cases, please use "FUN_nc_easywrite_enhanced" **
%
% -------------------------------------------------------------------------
% INPUT:
% filename [string]: name of the netcdf file to be created
% variable [string]: name of variables to be created. e.g., 'sst'
% data [array]: data array to be writtened to the netcdf file.
% dim_name [cell, optional]: name of dimensions of the variable.
% (Default value: dimension_name = {'x','y','z','t'}).
% global_discriptions [string,optional]: a global attribute will be added
% to the netcdf file if it is not empty
%
% **Please use "FUN_nc_easywrite_enhanced" for writting more than one
% variable.
% -------------------------------------------------------------------------
% OUTPUT:
% N/A
% -------------------------------------------------------------------------
% Example:
%
% FUN_NC_easywrite('temp.nc','T',Temp); or
% FUN_NC_easywrite('temp.nc','T',Temp,{'x','y','z','t'}); or
% FUN_NC_easywrite('temp.nc','T',Temp,{'x','y','z','t'},'This is an example');
%
% 2013-12-19 V1.00 By L. Chi (L.Chi.Ocean@outlook.com)
if exist(filename,'file')
delete(filename)
end
%% 1 Create Netcdf
% cid = netcdf.create(filename, mode)
% mode£º
% 'NC_NOCLOBBER' Prevent overwriting of existing file with the same name.
% 'NC_SHARE' Allow synchronous file updates.
% 'NC_64BIT_OFFSET' Allow easier creation of files and variables which are larger than two gigabytes.
ndims = length(size(data));
ncid = netcdf.create(filename,'NETCDF4');
%% 2 Define Dimensions
% dimid = netcdf.defDim(ncid,dimname,dimlen)
dim_name_default = {'x','y','z','t'};
if length(varargin) >=1
dim_name = varargin{1};
else
dim_name = dim_name_default;
end
for ii = 1:ndims
dimID(ii) = netcdf.defDim(ncid,dim_name{ii},size(data,ii));
end
%% 3. Define variables
% varid = netcdf.defVar(ncid,varname,xtype,dimids)
varid = netcdf.defVar(ncid,varname,'double',dimID);
% [compression]------------------------------------------------------------
% netcdf.defVarDeflate(ncid,varid,shuffle,deflate,deflateLevel)
%
% Descriptionnetcdf.defVarDeflate(ncid,varid,shuffle,deflate,deflateLevel) sets the compression parameters for the NetCDF variable specified by varid in the location specified by ncid.
% shuffle:
% Boolean value. To turn on the shuffle filter, set this argument to true. The shuffle filter can assist with the compression of integer data by changing the byte order in the data stream.
% deflate:
% Boolean value. To turn on compression, set this argument to true and set the deflateLevelargument to the desired compression level.
% deflateLevel:
% Numeric value between 0 and 9 specifying the amount of compression, where 0 is no compression and 9 is the most compression.
netcdf.defVarDeflate(ncid,varid,true,true,1);
if length(varargin) >= 2
%% ** Add a global attribute
netcdf.putAtt(ncid,0,'description',varargin{2});
end
%% 4 exit definition mode
netcdf.endDef(ncid)
%% 5 write data
% netcdf.putVar(ncid,varid,data)
% netcdf.putVar(ncid,varid,start,data)
% netcdf.putVar(ncid,varid,start,count,data)
% netcdf.putVar(ncid,varid,start,count,stride,data)
netcdf.putVar(ncid,varid,data)
%% 6 close file
netcdf.close(ncid);
return