11907
|
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} {} ncwrite (@var{filename}, @var{varname}, @var{x}) |
|
18 ## @deftypefnx {Function File} {} ncwrite (@var{filename}, @var{varname}, @var{x}, @var{start}, @var{stride}) |
11907
|
19 ## |
|
20 ## Write array @var{x} to the the variable @var{varname} in the NetCDF file |
|
21 ## @var{filename}. |
|
22 ## |
|
23 ## The variable with the name @var{varname} and the appropriate dimension must |
|
24 ## already exist in the NetCDF file. |
|
25 ## |
|
26 ## If @var{start} and @var{stride} are present, a subset of the |
|
27 ## variable is written. The parameter @var{start} contains the starting indices |
|
28 ## (1-based) and @var{stride} the |
|
29 ## increment between two successive elements. These parameters are vectors whose |
|
30 ## length is equal to the number of dimension of the variable. |
|
31 ## |
|
32 ## If the variable has the _FillValue attribute, then the values equal to NaN |
|
33 ## are replaced by corresponding fill value NetCDF attributes scale_factor |
|
34 ## (default 1) and add_oddset (default 0) are use the transform the variable |
|
35 ## during the writting: |
|
36 ## |
|
37 ## x_in_file = (x - add_offset)/scale_factor |
|
38 ## |
|
39 ## @seealso{ncread,nccreate} |
|
40 ## |
|
41 ## @end deftypefn |
|
42 |
|
43 function ncwrite(filename,varname,x,start,stride) |
|
44 |
|
45 ncid = netcdf_open(filename,'NC_WRITE'); |
|
46 [gid,varid] = ncvarid(ncid,varname); |
|
47 [varname_,xtype,dimids,natts] = netcdf_inqVar(gid,varid); |
|
48 |
|
49 % number of dimenions |
|
50 nd = length(dimids); |
|
51 |
|
52 sz = zeros(1,nd); |
|
53 count = zeros(1,nd); |
|
54 |
|
55 for i=1:length(dimids) |
|
56 [dimname, sz(i)] = netcdf_inqDim(gid,dimids(i)); |
|
57 count(i) = size(x,i); |
|
58 end |
|
59 |
|
60 if nargin < 4 |
|
61 start = ones(1,nd); |
|
62 end |
|
63 |
|
64 if nargin < 5 |
|
65 stride = ones(1,nd); |
|
66 end |
|
67 |
|
68 |
|
69 % apply attributes |
|
70 |
|
71 factor = []; |
|
72 offset = []; |
|
73 fv = []; |
|
74 |
|
75 for i = 0:natts-1 |
|
76 attname = netcdf_inqAttName(gid,varid,i); |
|
77 |
|
78 if strcmp(attname,'scale_factor') |
|
79 factor = netcdf_getAtt(gid,varid,'scale_factor'); |
|
80 elseif strcmp(attname,'add_offset') |
|
81 offset = netcdf_getAtt(gid,varid,'add_offset'); |
|
82 elseif strcmp(attname,'_FillValue') |
|
83 fv = netcdf_getAtt(gid,varid,'_FillValue'); |
|
84 end |
|
85 end |
|
86 |
|
87 if ~isempty(offset) |
|
88 x = x - offset; |
|
89 end |
|
90 |
|
91 if ~isempty(factor) |
|
92 x = x / factor; |
|
93 end |
|
94 |
12711
|
95 if ~isempty(fv) |
|
96 x(isnan(x)) = fv; |
|
97 end |
|
98 |
11907
|
99 netcdf_putVar(gid,varid,start-1,count,stride,x); |
|
100 |
|
101 netcdf_close(ncid); |