source: Roms_tools/mexcdf/snctools/nc_addvar.m @ 1

Last change on this file since 1 was 1, checked in by cholod, 13 years ago

import Roms_Agrif

File size: 5.9 KB
Line 
1function nc_addvar ( ncfile, varstruct )
2% NC_ADDVAR:  adds a variable to a NetCDF file
3%
4% USAGE:  nc_addvar ( ncfile, varstruct );
5%
6% PARAMETERS:
7% Input
8%    ncfile:
9%    varstruct:
10%        This is a structure with four fields:
11%
12%        Name
13%        Nctype
14%        Dimension
15%        Attribute
16%
17%      "Name" is just that, the name of the variable to be defined.
18%
19%      "Nctype" should be
20%          'double', 'float', 'int', 'short', or 'byte', or 'char'
21%          'NC_DOUBLE', 'NC_FLOAT', 'NC_INT', 'NC_SHORT', 'NC_BYTE', 'NC_CHAR'
22%
23%      "Dimension" is a cell array of dimension names.
24%
25%      "Attribute" is also a structure array.  Each element has two
26%      fields, "Name", and "Value".
27%
28% Output:
29%     None.  In case of an error, an exception is thrown.
30%
31% AUTHOR:
32%    john.g.evans.ne@gmail.com
33%
34
35
36error(nargchk(2,2,nargin,'struct'));
37
38if  ~ischar(ncfile)
39    error ( 'SNCTOOLS:NC_ADDVAR:badInput', 'file argument must be character' );
40end
41
42if ( ~isstruct(varstruct) )
43    error ( 'SNCTOOLS:NC_ADDVAR:badInput', '2nd argument must be a structure' );
44end
45
46
47varstruct = validate_varstruct ( varstruct );
48
49
50switch ( version('-release') )
51        case { '11', '12', '13', '14', '2006a', '2006b', '2007a', '2007b', '2008a' }
52                nc_addvar_mexnc(ncfile,varstruct);
53        otherwise
54                nc_addvar_tmw(ncfile,varstruct);
55end
56
57% Now just use nc_attput to put in the attributes
58for j = 1:length(varstruct.Attribute)
59    attname = varstruct.Attribute(j).Name;
60    attval = varstruct.Attribute(j).Value;
61    nc_attput ( ncfile, varstruct.Name, attname, attval );
62end
63
64
65
66
67
68%--------------------------------------------------------------------------
69function nc_addvar_tmw(ncfile,varstruct)
70
71ncid = netcdf.open(ncfile, nc_write_mode );
72
73%
74% determine the dimids of the named dimensions
75num_dims = length(varstruct.Dimension);
76dimids = zeros(1,num_dims);
77for j = 1:num_dims
78    dimids(1,j) = netcdf.inqDimID(ncid, varstruct.Dimension{j} );
79end
80
81% If we are old school, we need to flip the dimensions.
82if ~getpref('SNCTOOLS','PRESERVE_FVD',false)
83    dimids = fliplr(dimids);
84end
85
86%
87% go into define mode
88netcdf.reDef(ncid);
89
90netcdf.defVar(ncid, varstruct.Name, varstruct.Nctype, dimids );
91
92netcdf.endDef(ncid );
93netcdf.close(ncid );
94
95
96
97
98
99
100
101   
102%--------------------------------------------------------------------------
103function nc_addvar_mexnc(ncfile,varstruct)
104
105[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
106if ( status ~= 0 )
107    ncerr = mexnc ( 'strerror', status );
108    error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:OPEN', ...
109        'OPEN failed on %s, ''%s''', ncfile, ncerr);
110end
111
112%
113% determine the dimids of the named dimensions
114num_dims = length(varstruct.Dimension);
115dimids = zeros(num_dims,1);
116for j = 1:num_dims
117    [dimids(j), status] = mexnc ( 'dimid', ncid, varstruct.Dimension{j} );
118    if ( status ~= 0 )
119        mexnc ( 'close', ncid );
120        ncerr = mexnc ( 'strerror', status );
121        error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:DIMID', ncerr );
122    end
123end
124
125        % If preserving the fastest varying dimension in mexnc, we have to
126        % reverse their order.
127        if getpref('SNCTOOLS','PRESERVE_FVD',false)
128                dimids = flipud(dimids);
129        end
130
131
132status = mexnc ( 'redef', ncid );
133if ( status ~= 0 )
134    ncerr = mexnc ( 'strerror', status );
135    mexnc ( 'close', ncid );
136    error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:REDEF', ncerr );
137end
138
139[varid, status] = mexnc ( 'DEF_VAR', ncid, varstruct.Name, varstruct.Nctype, num_dims, dimids );
140if ( status ~= 0 )
141    ncerr = mexnc ( 'strerror', status );
142    mexnc ( 'endef', ncid );
143    mexnc ( 'close', ncid );
144    error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:DEF_VAR', ncerr );
145end
146
147status = mexnc ( 'enddef', ncid );
148if ( status ~= 0 )
149    ncerr = mexnc ( 'strerror', status );
150    mexnc ( 'close', ncid );
151    error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:ENDDEF', ncerr );
152end
153
154status = mexnc ( 'close', ncid );
155if ( status ~= 0 )
156    ncerr = mexnc ( 'strerror', status );
157    error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:CLOSE', ncerr );
158end
159
160
161
162
163
164return
165
166
167
168
169%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170function varstruct = validate_varstruct ( varstruct )
171
172%
173% Check that required fields are there.
174% Must at least have a name.
175if ~isfield ( varstruct, 'Name' )
176    error ( 'SNCTOOLS:NC_ADDVAR:badInput', 'structure argument must have at least the ''Name'' field.' );
177end
178
179%
180% Check that required fields are there.
181% Default Nctype is double.
182if ~isfield ( varstruct, 'Nctype' )
183    varstruct.Nctype = 'double';
184end
185
186%
187% Are there any unrecognized fields?
188fnames = fieldnames ( varstruct );
189for j = 1:length(fnames)
190    fname = fnames{j};
191    switch ( fname )
192
193    case { 'Nctype', 'Name', 'Dimension', 'Attribute' }
194        %
195        % These are used to create the variable.  They are ok.
196       
197    case { 'Unlimited', 'Size', 'Rank' }
198        %
199        % These come from the output of nc_getvarinfo.  We don't
200        % use them, but let's not give the user a warning about
201        % them either.
202
203    otherwise
204        fprintf ( 2, '%s:  unrecognized field name ''%s''.  Ignoring it...\n', mfilename, fname );
205    end
206end
207
208% If the datatype is not a string.
209% Change suggested by Brian Powell
210if ( isa(varstruct.Nctype, 'double') && varstruct.Nctype < 7 )
211    types={ 'byte' 'char' 'short' 'int' 'float' 'double'};
212    varstruct.Nctype = char(types(varstruct.Nctype));
213end
214
215
216%
217% Check that the datatype is known.
218switch ( varstruct.Nctype )
219case { 'NC_DOUBLE', 'double', ...
220    'NC_FLOAT', 'float', ...
221    'NC_INT', 'int', ...
222    'NC_SHORT', 'short', ...
223    'NC_BYTE', 'byte', ...
224    'NC_CHAR', 'char'  }
225    %
226    % Do nothing
227otherwise
228    error ( 'SNCTOOLS:NC_ADDVAR:unknownDatatype', 'unknown type ''%s''\n', mfilename, varstruct.Nctype );
229end
230
231
232%
233% Check that required fields are there.
234% Default Dimension is none.  Singleton scalar.
235if ~isfield ( varstruct, 'Dimension' )
236    varstruct.Dimension = [];
237end
238
239%
240% Check that required fields are there.
241% Default Attributes are none
242if ~isfield ( varstruct, 'Attribute' )
243    varstruct.Attribute = [];
244end
245
Note: See TracBrowser for help on using the repository browser.