]> Creatis software - CreaPhase.git/blob - octave_packages/octcdf-1.1.4/ncdump.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / octcdf-1.1.4 / ncdump.m
1 ## Copyright (C) 2005 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 -*-
17 ## @deftypefn {Loadable Function} ncdump(@var{filename})
18 ## @deftypefnx {Loadable Function} ncdump(@var{filename},@var{output_filename})
19 ## This function writes the content of the NetCDF file @var{filename} except the actual values of the variables
20 ## to the screen or to the file @var{output_filename} is this argument is provided. 
21 ## The output used the same syntax that the octcdf toolbox. Therefore ncdump can be used to
22 ## create a program that produces a NetCDF file with the same metadata than the NetCDF file
23 ## @var{filename}.
24 ## @end deftypefn
25 ##
26 ## @seealso{ncdim,ncvar,ncatt}
27
28 ## Author: Alexander Barth <barth.alexander@gmail.com>
29
30
31
32 function ncdump(fname,output);
33
34 if (nargin == 1)
35   fid = 1;
36 else
37   fid = fopen(output,'w');
38 end
39
40 nc = netcdf(fname,'r');
41
42
43 fprintf(fid,'nc = netcdf(''%s'',''noclobber'');\n\n',fname);
44 fprintf(fid,'%% dimensions\n\n');
45
46 % query all dimensions
47
48 nd = ncdim(nc);
49
50 for i=1:length(nd)
51   fprintf(fid,'nc(''%s'') = %d;\n',ncname(nd{i}),nd{i}(:));
52 end
53
54 fprintf(fid,'\n%% variables\n\n');
55
56 % query all variables
57
58 nv = ncvar(nc);
59
60 for i=1:length(nv)
61   varname = ncname(nv{i});
62
63   fprintf(fid,'nc{''%s''} = nc%s(',varname,ncdatatype(nv{i}));
64    
65   % print all dimension of the variable
66   nd = ncdim(nv{i});
67   n = length(nd);
68   
69   for j=1:n
70     % dimension name in quotes    
71     fprintf(fid,'''%s''',ncname(nd{j}));
72
73     % separator
74     if (j~=n)
75       fprintf(fid,',');
76     end    
77   end
78   
79   fprintf(fid,');  %% %d elements \n',numel(nv{i}));
80
81   % print all attributes of the variable
82   
83   na = ncatt(nv{i});
84
85   for j=1:length(na)  
86     datatype = ncdatatype(na{j});
87     f = type2format(datatype);
88   
89     fprintf(fid,['nc{''%s''}.%s = nc%s(%s);\n'],varname,ncname(na{j}),datatype,att2str(na{j}));
90   end
91
92   fprintf(fid,'\n');  
93 end
94
95 fprintf(fid,'%% global attributes\n\n');
96
97 % query all global attributes
98
99 na = ncatt(nc);
100
101 for j=1:length(na)  
102   datatype = ncdatatype(na{j});
103   f = type2format(datatype);
104   
105   fprintf(fid,['nc.%s = nc%s(%s);\n'],ncname(na{j}),datatype,att2str(na{j}));
106 end
107
108 fprintf(fid,'close(nc)\n');
109
110 if (fid ~= 1)
111   fclose(fid);
112 end
113
114
115
116 function f = type2format(datatype)
117
118 if (strcmp(datatype,'char'))
119     f = '''%s''';
120 elseif (strcmp(datatype,'byte'))
121     f = '%d';
122 else
123     f = '%g';
124 end
125   
126
127 function s = att2str(att)
128  datatype = ncdatatype(att);
129  f = type2format(datatype);
130  
131  n = length(att);
132  val = att(:);
133  
134  if (n == 1 || strcmp(datatype,'char'))
135    s = sprintf(f,val);   
136  else
137    s = '[';
138    
139    for i=1:n-1
140      s = [s sprintf(f,val(i)) ' '];
141    end
142
143    s = [s  sprintf(f,val(n)) ']'];
144    
145  end
146  
147