0001 function status = mcdump(file_name, option )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 if ( (nargin < 1) | (nargin > 2) )
0044 help mcdump;
0045 return;
0046 end
0047
0048 mexcdf('setopts', 0);
0049
0050
0051
0052
0053 do_all = 0;
0054 do_gatts = 0;
0055 do_vars = 0;
0056 do_varids = 0;
0057 do_varstring = 0;
0058 do_dims = 0;
0059 if ( nargin == 1 )
0060 do_all = 1;
0061 end
0062
0063 if ( nargin == 2 )
0064 if ( isstr (option) )
0065 if ( (strcmp(lower(option),'nc_global')) | ...
0066 (strcmp(lower(option),'global')) | ...
0067 (strcmp(lower(option),'gatts')) )
0068 do_gatts = 1;
0069 elseif ( (strcmp(lower(option),'dim')) | (strcmp(option,'dims')) )
0070 do_dims = 1;
0071 else
0072 do_vars = 1;
0073 do_varstring = 1;
0074 varstring_to_do = option;
0075 end
0076 else
0077 do_vars = 1;
0078 do_varids = 1;
0079 varids_to_do = option;
0080 end
0081 end
0082
0083
0084
0085
0086 if nargin == 0,
0087 [buf, path]=uigetfile('*.cdf','Select a netCDF file');
0088 file_name=[path buf];
0089 clear path buf
0090 end
0091 file_id = mexcdf('open',file_name,'nowrite');
0092 if ( file_id == -1 )
0093 fprintf ('Can''t seem to open %s. Bummer!\n', file_name);
0094 status = -1;
0095 return;
0096 end
0097
0098 [num_dims,num_vars,num_global_attribs,rec_dim] ...
0099 = mexcdf('inquire', file_id);
0100
0101
0102
0103
0104 fprintf ( 2, 'netcdf %s\n\n', strtok ( file_name, '.' ) );
0105
0106
0107
0108
0109 dim_name = [];
0110 for dim_id = 0:num_dims-1
0111 [name,dim_size(dim_id+1),status] = mexcdf('diminq', file_id, dim_id);
0112 dim_name = str2mat ( dim_name, name );
0113 end
0114 dim_name(1,:) = [];
0115
0116
0117 if ( do_all | do_dims )
0118 fprintf ( 2, 'dimensions:\n' );
0119 for dim_id = 0:num_dims-1
0120 if ( dim_id == rec_dim )
0121 fprintf( '\t%s = UNLIMITED ; (%i currently)\n', deblank(dim_name(dim_id+1,:)), dim_size(dim_id+1) );
0122 else
0123 fprintf ( '\t%s = %i ;\n', dim_name(dim_id+1,:), dim_size(dim_id+1) );
0124 end
0125 end
0126 fprintf('\n\n');
0127 end
0128
0129
0130 if ( do_all | do_vars )
0131 fprintf ( 'variables:\n' );
0132 for var_id = 0:num_vars-1
0133 [var_name, datatype, num_dims, dim_vector, num_atts] ...
0134 = mexcdf( 'varinq', file_id, var_id );
0135
0136
0137
0138 if ( datatype == 1 )
0139 dtype = 'byte';
0140 elseif ( datatype == 2 )
0141 dtype = 'char';
0142 elseif ( datatype == 3 )
0143 dtype = 'short';
0144 elseif ( datatype == 4 )
0145 dtype = 'long';
0146 elseif ( datatype == 5 )
0147 dtype = 'float';
0148 elseif ( datatype == 6 )
0149 dtype = 'double';
0150 end
0151
0152
0153
0154 if ( isempty(dim_vector) )
0155 var_dim_str = '';
0156 else
0157 [dim_name,dim_len,status] = mexcdf('diminq', file_id, dim_vector(1) );
0158 var_dim_str = sprintf ( '%s', dim_name );
0159
0160 for dix = 2:length(dim_vector)
0161 [dim_name,dim_len,status] = mexcdf('diminq', file_id, dim_vector(dix) );
0162
0163 var_dim_str = sprintf ( '%s, %s', var_dim_str, dim_name );
0164 end
0165 end
0166
0167
0168
0169 if ( isempty(dim_vector) )
0170 shape_str = '';
0171 else
0172 shape_str = sprintf ( '%i ', dim_size(dim_vector+1));
0173 end
0174 shape_str = sprintf ( '[ %s]', shape_str );
0175
0176 do_this_one = 1;
0177
0178 if ( do_varstring )
0179 if ( strcmp(varstring_to_do, var_name ) )
0180 do_this_one = 1;
0181 else
0182 do_this_one = 0;
0183 end
0184 end
0185
0186 if ( do_varids )
0187 if ( ~isempty(find(varids_to_do == var_id ) ) )
0188 do_this_one = 1;
0189 else
0190 do_this_one = 0;
0191 end
0192 end
0193
0194
0195 if ( do_this_one )
0196 fprintf ( '\t%s %s(%s), varid %i, shape = %s\n', ...
0197 dtype, ...
0198 var_name, ...
0199 var_dim_str, ...
0200 var_id, ...
0201 shape_str );
0202
0203
0204
0205
0206 att_id = 0;
0207 outed_atts = 0;
0208 while ( outed_atts < num_atts )
0209 [att_name, status] = mexcdf('attname', file_id, var_id, att_id);
0210
0211
0212
0213 if ( status ~= -1 )
0214
0215 outed_atts = outed_atts+1;
0216 [datatype,num_attrib_vals,status] = mexcdf('attinq', file_id, var_id, att_name);
0217 [attrib_values,status] = mexcdf ('attget', file_id, var_id, att_name);
0218 if (datatype == 1)
0219 att_val = sprintf ('%x ', attrib_values);
0220 elseif (datatype == 2)
0221 att_val = sprintf('%s ', attrib_values);
0222 elseif (datatype == 3)
0223 att_val = sprintf('%i ', attrib_values);
0224 elseif (datatype == 4)
0225 att_val = sprintf('%i ', attrib_values);
0226 elseif (datatype == 5)
0227 att_val = sprintf('%.3f ', attrib_values);
0228 elseif (datatype == 6)
0229 att_val = sprintf('%.3f ', attrib_values);
0230 end
0231 end
0232
0233 fprintf('\t\t%s:%s = %s\n', var_name, att_name, att_val);
0234 att_id = att_id + 1;
0235
0236 end
0237 end
0238
0239 end
0240 fprintf ( '\n\n' );
0241 end
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 if ( do_all | do_gatts )
0253 fprintf ( '//global attributes:\n' );
0254
0255 outed_atts = 0;
0256 att_id = 0;
0257
0258 while ( outed_atts < num_global_attribs )
0259 [att_name, status] = mexcdf('attname', file_id, 'GLOBAL', att_id);
0260
0261
0262
0263 if ( status ~= -1 )
0264
0265 outed_atts = outed_atts+1;
0266 [datatype,num_attrib_vals,status] = mexcdf('attinq', file_id, 'GLOBAL', att_name);
0267 attrib_values = mexcdf ('attget', file_id, 'GLOBAL', att_name);
0268 if (datatype == 1)
0269 att_val = sprintf ('%x ', attrib_values);
0270 elseif (datatype == 2)
0271 att_val = sprintf('%s ', attrib_values);
0272 elseif (datatype == 3)
0273 att_val = sprintf('%i ', attrib_values);
0274 elseif (datatype == 4)
0275 att_val = sprintf('%i ', attrib_values);
0276 elseif (datatype == 5)
0277 if (attrib_values(1) < 1e-4)
0278 att_val = sprintf('%.3e ', attrib_values);
0279 else
0280 att_val = sprintf('%.3f ', attrib_values);
0281 end
0282 elseif (datatype == 6)
0283 att_val = sprintf('%.4f ', attrib_values);
0284 end
0285 end
0286
0287 fprintf('\t\t:%s = %s\n', att_name, att_val);
0288 att_id = att_id + 1;
0289
0290 end
0291 end
0292
0293
0294
0295 status = mexcdf('close', file_id);
0296
0297
0298 return;
0299
0300
0301