Home > omviz > mcdump.m

mcdump

PURPOSE ^

MCDUMP is a more terse form of the NetCDF utility 'ncdump'.

SYNOPSIS ^

function status = mcdump(file_name, option )

DESCRIPTION ^

 MCDUMP is a more terse form of the NetCDF utility 'ncdump'.

 USAGE: mcdump(file_name,option)
    
 Examples: 

   1.  >> status = mcdump('ecomsi.cdf');

       produces a paged output describing the NetCDF
       file 'ecomsi.cdf'.  The  status  is 0 if the operation
       was successful.  If the file does not exist, or 
       if it is corrupted somehow, a -1 is returned. 

       Information about the dimensions, variables, and
       global attributes is output.


   2.  >> status = mcdump ( 'ecomsi.cdf, 'nc_global' );
       >> status = mcdump ( 'ecomsi.cdf, 'global' );
       >> status = mcdump ( 'ecomsi.cdf, 'gatts' );

       This dumps only the global attributes.

   3.  >> status = mcdump ( 'ecomsi.cdf', 'dim' );
       >> status = mcdump ( 'ecomsi.cdf', 'dims' );

       This dumps only the dimensions.

   4.  >> status = mcdump ( 'ecomsi.cdf', 'x' );
      
       This dumps only information about variable 'x'.

   5.  >> status = mcdump ( 'ecomsi.cdf', [0 2 4 6 8] );

       This dumps information about variables with 
       varids 0, 2, 4, 6, and 8

       See also MEXCDF

       John Evans (jevans@sakhalin.er.usgs.gov)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function status = mcdump(file_name, option )
0002 % MCDUMP is a more terse form of the NetCDF utility 'ncdump'.
0003 %
0004 % USAGE: mcdump(file_name,option)
0005 %
0006 % Examples:
0007 %
0008 %   1.  >> status = mcdump('ecomsi.cdf');
0009 %
0010 %       produces a paged output describing the NetCDF
0011 %       file 'ecomsi.cdf'.  The  status  is 0 if the operation
0012 %       was successful.  If the file does not exist, or
0013 %       if it is corrupted somehow, a -1 is returned.
0014 %
0015 %       Information about the dimensions, variables, and
0016 %       global attributes is output.
0017 %
0018 %
0019 %   2.  >> status = mcdump ( 'ecomsi.cdf, 'nc_global' );
0020 %       >> status = mcdump ( 'ecomsi.cdf, 'global' );
0021 %       >> status = mcdump ( 'ecomsi.cdf, 'gatts' );
0022 %
0023 %       This dumps only the global attributes.
0024 %
0025 %   3.  >> status = mcdump ( 'ecomsi.cdf', 'dim' );
0026 %       >> status = mcdump ( 'ecomsi.cdf', 'dims' );
0027 %
0028 %       This dumps only the dimensions.
0029 %
0030 %   4.  >> status = mcdump ( 'ecomsi.cdf', 'x' );
0031 %
0032 %       This dumps only information about variable 'x'.
0033 %
0034 %   5.  >> status = mcdump ( 'ecomsi.cdf', [0 2 4 6 8] );
0035 %
0036 %       This dumps information about variables with
0037 %       varids 0, 2, 4, 6, and 8
0038 %
0039 %       See also MEXCDF
0040 %
0041 %       John Evans (jevans@sakhalin.er.usgs.gov)
0042 
0043 if ( (nargin < 1) | (nargin > 2) )
0044     help mcdump;
0045     return;
0046 end
0047 
0048 mexcdf('setopts', 0);
0049 
0050 
0051 %
0052 % first figure out what the options are
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 % Try to open the file.  If it's not there, then this will be
0085 % a real short little function...
0086 if nargin == 0,  % if no file provided, ask for one
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 % print out name of file
0104 fprintf ( 2, 'netcdf %s\n\n', strtok ( file_name, '.' ) );
0105 
0106 
0107 %
0108 % print out dimension information
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         % string version of datatype
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         % string version of variable dimensions
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             % Now do all attributes for each variable.
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                 % if bad status, no att id here, just go on
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)  % byte
0219                         att_val = sprintf ('%x ', attrib_values);
0220                     elseif (datatype == 2)  % char
0221                         att_val = sprintf('%s ', attrib_values);
0222                     elseif (datatype == 3)  % short
0223                         att_val = sprintf('%i ', attrib_values);
0224                     elseif (datatype == 4)  % long
0225                         att_val = sprintf('%i ', attrib_values);
0226                     elseif (datatype == 5)  % float
0227                         att_val = sprintf('%.3f ', attrib_values);
0228                     elseif (datatype == 6)  % double
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 % Finally, print out info about the global attributes.  Doesn't seem
0249 % to like the 'GLOBAL' option for some reason.  It works, but is
0250 % also trying to output a warning message that is suppressed by the
0251 % setopts command at the top.
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         % if bad status, no att id here, just go on
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)  % byte
0269                 att_val = sprintf ('%x ', attrib_values);
0270             elseif (datatype == 2)  % char
0271                 att_val = sprintf('%s ', attrib_values);
0272             elseif (datatype == 3)  % short
0273                 att_val = sprintf('%i ', attrib_values);
0274             elseif (datatype == 4)  % long
0275                 att_val = sprintf('%i ', attrib_values);
0276             elseif (datatype == 5)  % float
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)  % double
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

Generated on Thu 01-Dec-2005 08:50:35 by m2html © 2003