annotate main/netcdf/inst/ncwrite.m @ 12711:a4de85b2e6a0 octave-forge

fix bug 47014 and make test code work in matlab
author abarth93
date Fri, 19 Feb 2016 13:45:14 +0000
parents 105114c0833c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
1 ## Copyright (C) 2013 Alexander Barth
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
2 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
6 ## (at your option) any later version.
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
7 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
11 ## GNU General Public License for more details.
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
12 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
15
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
16 ## -*- texinfo -*-
11968
105114c0833c fix texinfo documentation (1)
abarth93
parents: 11907
diff changeset
17 ## @deftypefn {Function File} {} ncwrite (@var{filename}, @var{varname}, @var{x})
105114c0833c fix texinfo documentation (1)
abarth93
parents: 11907
diff changeset
18 ## @deftypefnx {Function File} {} ncwrite (@var{filename}, @var{varname}, @var{x}, @var{start}, @var{stride})
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
19 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
20 ## Write array @var{x} to the the variable @var{varname} in the NetCDF file
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
21 ## @var{filename}.
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
22 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
23 ## The variable with the name @var{varname} and the appropriate dimension must
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
24 ## already exist in the NetCDF file.
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
25 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
26 ## If @var{start} and @var{stride} are present, a subset of the
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
27 ## variable is written. The parameter @var{start} contains the starting indices
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
28 ## (1-based) and @var{stride} the
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
29 ## increment between two successive elements. These parameters are vectors whose
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
30 ## length is equal to the number of dimension of the variable.
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
31 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
32 ## If the variable has the _FillValue attribute, then the values equal to NaN
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
33 ## are replaced by corresponding fill value NetCDF attributes scale_factor
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
34 ## (default 1) and add_oddset (default 0) are use the transform the variable
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
35 ## during the writting:
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
36 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
37 ## x_in_file = (x - add_offset)/scale_factor
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
38 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
39 ## @seealso{ncread,nccreate}
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
40 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
41 ## @end deftypefn
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
42
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
43 function ncwrite(filename,varname,x,start,stride)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
44
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
45 ncid = netcdf_open(filename,'NC_WRITE');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
46 [gid,varid] = ncvarid(ncid,varname);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
47 [varname_,xtype,dimids,natts] = netcdf_inqVar(gid,varid);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
48
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
49 % number of dimenions
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
50 nd = length(dimids);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
51
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
52 sz = zeros(1,nd);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
53 count = zeros(1,nd);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
54
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
55 for i=1:length(dimids)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
56 [dimname, sz(i)] = netcdf_inqDim(gid,dimids(i));
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
57 count(i) = size(x,i);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
58 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
59
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
60 if nargin < 4
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
61 start = ones(1,nd);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
62 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
63
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
64 if nargin < 5
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
65 stride = ones(1,nd);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
66 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
67
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
68
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
69 % apply attributes
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
70
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
71 factor = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
72 offset = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
73 fv = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
74
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
75 for i = 0:natts-1
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
76 attname = netcdf_inqAttName(gid,varid,i);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
77
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
78 if strcmp(attname,'scale_factor')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
79 factor = netcdf_getAtt(gid,varid,'scale_factor');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
80 elseif strcmp(attname,'add_offset')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
81 offset = netcdf_getAtt(gid,varid,'add_offset');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
82 elseif strcmp(attname,'_FillValue')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
83 fv = netcdf_getAtt(gid,varid,'_FillValue');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
84 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
85 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
86
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
87 if ~isempty(offset)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
88 x = x - offset;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
89 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
90
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
91 if ~isempty(factor)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
92 x = x / factor;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
93 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
94
12711
a4de85b2e6a0 fix bug 47014 and make test code work in matlab
abarth93
parents: 11968
diff changeset
95 if ~isempty(fv)
a4de85b2e6a0 fix bug 47014 and make test code work in matlab
abarth93
parents: 11968
diff changeset
96 x(isnan(x)) = fv;
a4de85b2e6a0 fix bug 47014 and make test code work in matlab
abarth93
parents: 11968
diff changeset
97 end
a4de85b2e6a0 fix bug 47014 and make test code work in matlab
abarth93
parents: 11968
diff changeset
98
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
99 netcdf_putVar(gid,varid,start-1,count,stride,x);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
100
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
101 netcdf_close(ncid);