annotate main/netcdf/inst/nccreate.m @ 12631:5818fde6abd8 octave-forge

Fix bug #45190, netcdf(nccreate) to enable DeflateLevel in nccreate
author abarth93
date Wed, 10 Jun 2015 13:17:57 +0000
parents bdcfe5e6756d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11912
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
1 ## Copyright (C) 2013 Alexander Barth
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
2 ##
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
4 ## it under the terms of the GNU General Public License as published by
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
6 ## (at your option) any later version.
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
7 ##
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
8 ## This program is distributed in the hope that it will be useful,
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
11 ## GNU General Public License for more details.
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
12 ##
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
13 ## You should have received a copy of the GNU General Public License
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
15
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
16 ## -*- texinfo -*-
11968
105114c0833c fix texinfo documentation (1)
abarth93
parents: 11912
diff changeset
17 ## @deftypefn {Function File} {} nccreate(@var{filename},@var{varname})
105114c0833c fix texinfo documentation (1)
abarth93
parents: 11912
diff changeset
18 ## @deftypefnx {Function File} {} nccreate(@var{filename},@var{varname},"property",@var{value},...)
11912
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
19 ##
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
20 ## Create the variable @var{varname} in the file @var{filename}.
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
21 ##
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
22 ## The following properties can be used:
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
23 ## @itemize
12364
b504f9b83371 capitalize keyword arguments in documentation
abarth93
parents: 11968
diff changeset
24 ## @item "Dimensions": a cell array with the dimension names followed by their
11912
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
25 ## length or Inf if the dimension is unlimited. If the property is ommited, a
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
26 ## scalar variable is created.
12364
b504f9b83371 capitalize keyword arguments in documentation
abarth93
parents: 11968
diff changeset
27 ## @item "Datatype": a string with the Octave data type name
11912
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
28 ## (see @code{ncinfo} for the correspondence between Octave and NetCDF data
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
29 ## types). The default data type is a "double".
12364
b504f9b83371 capitalize keyword arguments in documentation
abarth93
parents: 11968
diff changeset
30 ## @item "Format": This can be "netcdf4_classic" (default), "classic", "64bit"
11912
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
31 ## or "netcdf4".
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
32 ## @item "FillValue": the value used for undefined elements of the NetCDF
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
33 ## variable.
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
34 ## @item "ChunkSize": the size of the data chunks. If omited, the variable is
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
35 ## not chunked.
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
36 ## @item "DeflateLevel": The deflate level for compression. It can be the string
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
37 ## "disable" (default) for no compression or an integer between 0 (no
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
38 ## compression) and 9 (maximum compression).
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
39 ## @item "Shuffle": true for enabling the shuffle filter or false (default) for
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
40 ## disabling it.
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
41 ## @end itemize
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
42 ##
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
43 ## Example:
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
44 ## @example
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
45 ## nccreate("test.nc","temp","Dimensions",@{"lon",10,"lat",20@},"Format","classic");
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
46 ## ncdisp("test.nc");
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
47 ## @end example
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
48 ## @seealso{ncwrite}
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
49 ## @end deftypefn
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
50
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
51
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
52 function nccreate(filename,varname,varargin)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
53
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
54 dimensions = {};
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
55 datatype = 'double';
11912
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
56 ncformat = 'netcdf4_classic';
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
57 FillValue = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
58 ChunkSize = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
59 DeflateLevel = 'disable';
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
60 Shuffle = false;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
61
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
62
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
63
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
64
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
65 for i = 1:2:length(varargin)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
66 if strcmp(varargin{i},'Dimensions')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
67 dimensions = varargin{i+1};
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
68 elseif strcmp(varargin{i},'Datatype')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
69 datatype = varargin{i+1};
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
70 elseif strcmp(varargin{i},'Format')
11912
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
71 ncformat = varargin{i+1};
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
72 elseif strcmp(varargin{i},'FillValue')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
73 FillValue = varargin{i+1};
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
74 elseif strcmp(varargin{i},'ChunkSize')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
75 ChunkSize = varargin{i+1};
12631
5818fde6abd8 Fix bug #45190, netcdf(nccreate) to enable DeflateLevel in nccreate
abarth93
parents: 12503
diff changeset
76 elseif strcmp(varargin{i},'DeflateLevel')
5818fde6abd8 Fix bug #45190, netcdf(nccreate) to enable DeflateLevel in nccreate
abarth93
parents: 12503
diff changeset
77 DeflateLevel = varargin{i+1};
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
78 elseif strcmp(varargin{i},'Shuffle')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
79 Shuffle = varargin{i+1};
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
80 else
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
81 error(['unknown keyword ' varargin{i} '.']);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
82 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
83 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
84
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
85 if ~isempty(stat(filename))
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
86 ncid = netcdf_open(filename,'NC_WRITE');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
87 netcdf_reDef(ncid);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
88 else
11912
27d9f37e720d more documentation
abarth93
parents: 11907
diff changeset
89 mode = format2mode(ncformat);
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
90 ncid = netcdf_create(filename,mode);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
91 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
92
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
93 % create dimensions
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
94
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
95 dimids = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
96 i = 1;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
97
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
98 while i <= length(dimensions)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
99
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
100 if i == length(dimensions)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
101 dimids(end+1) = netcdf_inqDimID(ncid,dimensions{i});
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
102 i = i+1;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
103 elseif ischar(dimensions{i+1})
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
104 dimids(end+1) = netcdf_inqDimID(ncid,dimensions{i});
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
105 i = i+1;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
106 else
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
107 try
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
108 if isinf(dimensions{i+1})
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
109 dimensions{i+1} = netcdf_getConstant('NC_UNLIMITED');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
110 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
111 dimids(end+1) = netcdf_defDim(ncid,dimensions{i},dimensions{i+1});
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
112 catch
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
113 dimids(end+1) = netcdf_inqDimID(ncid,dimensions{i});
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
114 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
115 i = i+2;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
116 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
117 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
118
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
119
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
120 varid = netcdf_defVar(ncid,varname,oct2nctype(datatype),dimids);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
121
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
122 if ~isempty(ChunkSize)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
123 netcdf_defVarChunking(ncid,varid,'chunked',ChunkSize);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
124 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
125
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
126 if ~isempty(FillValue)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
127 % value of nofill?
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
128 netcdf_defVarFill(ncid,varid,false,FillValue);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
129 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
130
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
131 if isnumeric(DeflateLevel)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
132 netcdf_defVarDeflate(ncid,varid,Shuffle,true,DeflateLevel);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
133 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
134
12503
bdcfe5e6756d Implement DeflateLevel in nccreate
abarth93
parents: 12364
diff changeset
135 netcdf_close(ncid);