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