File: ncinfo.m

package info (click to toggle)
octave-netcdf 1.0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,756 kB
  • sloc: sh: 2,943; cpp: 1,812; python: 438; makefile: 223; awk: 51; xml: 20
file content (266 lines) | stat: -rw-r--r-- 8,249 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
## Copyright (C) 2013-2022 Alexander Barth
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn  {Function File} {@var{info} =} ncinfo (@var{filename})
## @deftypefnx  {Function File} {@var{info} =} ncinfo (@var{filename}, @var{varname})
## @deftypefnx  {Function File} {@var{info} =} ncinfo (@var{filename}, @var{groupname})
## Return information about an entire NetCDF file @var{filename} (i.e. the root 
## group "/"), about the variable called @var{varname} or the group called 
## @var{groupname}.
##
## The structure @var{info} has always the following fields:
## @itemize
## @item @var{Filename}: the name of the NetCDF file
## @item @var{Format}: one of the strings "CLASSIC", "64BIT", "NETCDF4"
## or "NETCDF4_CLASSIC"
## @end itemize
##
## The structure @var{info} has additional fields depending on whether a 
## group of variable is queried.
##
## @subsubheading Groups
##
## Groups are returned as an array structure with the following fields:
##
## @itemize
## @item @var{Name}: the group name. The root group is named "/".
## @item @var{Dimensions}: a array structure with the dimensions.
## @item @var{Variables}: a array structure with the variables.
## @item @var{Attributes}: a array structure with global attributes.  
## @item @var{Groups}: a array structure (one for each group) with the 
## same fields as this structure.
## @end itemize
##
## @subsubheading Dimensions
##
## Dimensions are returned as an array structure with the following fields:
## @itemize
##   @item @var{Name}: the name of the dimension
##   @item @var{Length}: the length of the dimension
##   @item @var{Unlimited}: true of the dimension has no fixed limited, false 
## @end itemize
##
## @subsubheading Variables
##
## Variables are returned as an array structure with the following fields:
## @itemize
##   @item @var{Name}: the name of the dimension
##   @item @var{Dimensions}: array structure of all dimensions of this variable 
## with the same structure as above.
##   @item @var{Size}: array with the size of the variable
##   @item @var{Datatype}: string with the corresponding octave data-type 
## (see below)
##   @item @var{Attributes}: a array structure of attributes
##   @item @var{FillValue}: the NetCDF fill value of the variable. If the fill 
## value is not defined, then this attribute is an empty array ([]).
##   @item @var{DeflateLevel}: the NetCDF deflate level between 0 (no 
##   compression) and 9 (maximum compression).
##   @item @var{Shuffle}: is true if the shuffle filter is activated to improve 
##   compression, otherwise false.
##   @item @var{CheckSum}: is set to "fletcher32", if check-sums are used, 
##    otherwise this field is not defined.
## @end itemize
##
## @subsubheading Attributes
##
## Attributes are returned as an array structure with the following fields: 
## @itemize
##   @item @var{Name}: the name of the attribute
##   @item @var{Value}: the value of the attribute (with the corresponding type)
##   @item @var{Unlimited}: true of the dimension has no fixed limited, false 
## @end itemize
##
## @subsubheading Data-types
##
## The following the the correspondence between the Octave and NetCDF 
## data-types:
## 
## @multitable @columnfractions .5 .5
## @headitem Octave type @tab NetCDF type
## @item @code{int8}    @tab @code{NC_BYTE}   
## @item @code{uint8}   @tab @code{NC_UBYTE}  
## @item @code{int16}   @tab @code{NC_SHORT}  
## @item @code{uint16}  @tab @code{NC_USHORT} 
## @item @code{int32}   @tab @code{NC_INT}    
## @item @code{uint32}  @tab @code{NC_UINT}   
## @item @code{int64}   @tab @code{NC_INT64}  
## @item @code{uint64}  @tab @code{NC_UINT64} 
## @item @code{single}  @tab @code{NC_FLOAT}  
## @item @code{double}  @tab @code{NC_DOUBLE} 
## @item @code{char}    @tab @code{NC_CHAR}   
## @end multitable
##
## The output of @code{ncinfo} can be used to create a NetCDF file with the same
## meta-data using @code{ncwriteschema}.
##
## Note: If there are no attributes (or variable or groups), the corresponding 
## field is an empty matrix and not an empty struct array for compatibility
## with matlab.
##
## @seealso{ncread,nccreate,ncwriteschema,ncdisp}
##
## @end deftypefn

function info = ncinfo (filename, name)

  ncid = netcdf_open(filename,"NC_NOWRITE");
  info.Filename = filename;    

  if nargin == 1    
    name = "/";
  endif

  [gid,varid] = ncloc(ncid,name);

  if isempty(varid)
    info = ncinfo_group(info,gid);
  else
    unlimdimIDs = netcdf_inqUnlimDims(gid);
    info = ncinfo_var(info,gid,varid,unlimdimIDs);
  endif

  # NetCDF format
  ncformat = netcdf_inqFormat(ncid);
  info.Format = lower(strrep(ncformat,'FORMAT_',''));    

  netcdf_close(ncid);
endfunction

function dims = ncinfo_dim(ncid,dimids,unlimdimIDs)

  dims = [];
  for i=1:length(dimids)
    tmp = struct();

    [tmp.Name, tmp.Length] = netcdf_inqDim(ncid,dimids(i));
    tmp.Unlimited = any(unlimdimIDs == dimids(i));
    
    if isempty(dims)
      dims = [tmp];
    else
      dims(i) = tmp;
    endif
  endfor
endfunction

function vinfo = ncinfo_var(vinfo,ncid,varid,unlimdimIDs)

  [vinfo.Name,xtype,dimids,natts] = netcdf_inqVar(ncid,varid);

  % Information about dimension
  vinfo.Dimensions = ncinfo_dim(ncid,dimids,unlimdimIDs);
  if isempty(vinfo.Dimensions)
    vinfo.Size = [];
  else
    vinfo.Size = cat(2,vinfo.Dimensions.Length);
  endif

  % Data type
  if xtype >= netcdf_getConstant("NC_FIRSTUSERTYPEID")
    [utype_name, utype_bsize, utype_typeid, utype_nfields, utype_classid]  = netcdf_inqUserType(ncid, xtype);
    vinfo.Datatype = nc2octtype(utype_typeid);
    if utype_classid == netcdf_getConstant("NC_VLEN")
      vinfo.Datatype = [vinfo.Datatype '*'];
    endif
  else
    vinfo.Datatype = nc2octtype(xtype);
  endif

  % Attributes
  vinfo.Attributes = [];

  for i = 0:natts-1  
    tmp = struct();
    tmp.Name = netcdf_inqAttName(ncid,varid,i);
    tmp.Value = netcdf_getAtt(ncid,varid,tmp.Name);
    
    if isempty(vinfo.Attributes)      
      vinfo.Attributes = [tmp];
    else
      vinfo.Attributes(i+1) = tmp;
    endif
  endfor

  % chunking, fillvalue, compression

  [storage,vinfo.ChunkSize] = netcdf_inqVarChunking(ncid,varid);

  [nofill,vinfo.FillValue] = netcdf_inqVarFill(ncid,varid);
  if nofill
    vinfo.FillValue = [];
  endif

  [shuffle,deflate,vinfo.DeflateLevel] = ...
    netcdf_inqVarDeflate(ncid,varid);

  if ~deflate
    vinfo.DeflateLevel = [];
  endif
  vinfo.Shuffle = shuffle;

  # add checksum information if defined (unlike matlab)
  checksum = netcdf_inqVarFletcher32(ncid,varid);
  if ~strcmp(checksum,'nochecksum');
    vinfo.Checksum = checksum;
  endif

endfunction

function info = ncinfo_group (info, ncid)

  info.Name = netcdf_inqGrpName(ncid);
  unlimdimIDs = netcdf_inqUnlimDims(ncid);

  [ndims,nvars,ngatts] = netcdf_inq(ncid);

  % dimensions

  dimids = netcdf_inqDimIDs(ncid);
  info.Dimensions = ncinfo_dim(ncid,dimids,unlimdimIDs);

  % variables
  for i=1:nvars
    info.Variables(i) = ncinfo_var(struct(),ncid,i-1,unlimdimIDs);
  endfor

  % global attributes
  info.Attributes = [];
  gid = netcdf_getConstant('NC_GLOBAL');
  for i = 0:ngatts-1  
    tmp = struct();
    tmp.Name = netcdf_inqAttName(ncid,gid,i);
    tmp.Value = netcdf_getAtt(ncid,gid,tmp.Name);
  
    if isempty(info.Attributes)      
      info.Attributes = [tmp];
    else
      info.Attributes(i+1) = tmp;
    endif
  endfor

  info.Groups = [];
  gids = netcdf_inqGrps(ncid);
  for i = 1:length(gids)
    tmp = ncinfo_group(struct(),gids(i));
  
    if isempty(info.Groups)      
      info.Groups = [tmp];
    else
      info.Groups(i) = tmp;
    endif
  endfor

endfunction