1 ## Copyright (C) 2005 Alexander Barth
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.
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.
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/>.
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
26 ## @seealso{ncdim,ncvar,ncatt}
28 ## Author: Alexander Barth <barth.alexander@gmail.com>
32 function ncdump(fname,output);
37 fid = fopen(output,'w');
40 nc = netcdf(fname,'r');
43 fprintf(fid,'nc = netcdf(''%s'',''noclobber'');\n\n',fname);
44 fprintf(fid,'%% dimensions\n\n');
46 % query all dimensions
51 fprintf(fid,'nc(''%s'') = %d;\n',ncname(nd{i}),nd{i}(:));
54 fprintf(fid,'\n%% variables\n\n');
61 varname = ncname(nv{i});
63 fprintf(fid,'nc{''%s''} = nc%s(',varname,ncdatatype(nv{i}));
65 % print all dimension of the variable
70 % dimension name in quotes
71 fprintf(fid,'''%s''',ncname(nd{j}));
79 fprintf(fid,'); %% %d elements \n',numel(nv{i}));
81 % print all attributes of the variable
86 datatype = ncdatatype(na{j});
87 f = type2format(datatype);
89 fprintf(fid,['nc{''%s''}.%s = nc%s(%s);\n'],varname,ncname(na{j}),datatype,att2str(na{j}));
95 fprintf(fid,'%% global attributes\n\n');
97 % query all global attributes
102 datatype = ncdatatype(na{j});
103 f = type2format(datatype);
105 fprintf(fid,['nc.%s = nc%s(%s);\n'],ncname(na{j}),datatype,att2str(na{j}));
108 fprintf(fid,'close(nc)\n');
116 function f = type2format(datatype)
118 if (strcmp(datatype,'char'))
120 elseif (strcmp(datatype,'byte'))
127 function s = att2str(att)
128 datatype = ncdatatype(att);
129 f = type2format(datatype);
134 if (n == 1 || strcmp(datatype,'char'))
140 s = [s sprintf(f,val(i)) ' '];
143 s = [s sprintf(f,val(n)) ']'];