ncdf4a13/cxx/netcdfcpp.h

Go to the documentation of this file.
00001 /*********************************************************************
00002  *   Copyright 1992, University Corporation for Atmospheric Research
00003  *   See netcdf/README file for copying and redistribution conditions.
00004  *
00005  *   Purpose:   C++ class interface for netCDF
00006  *
00007  *   $Header: /upc/share/CVS/netcdf-3/cxx/netcdfcpp.h,v 1.9 2005/05/02 22:38:42 russ Exp $
00008  *********************************************************************/
00009 
00010 #ifndef NETCDF_HH
00011 #define NETCDF_HH
00012 
00013 extern "C" {
00014 #include "netcdf.h"             // the C interface
00015 }
00016 #include "ncvalues.h"          // arrays that know their element type
00017 
00018 typedef const char* NcToken;    // names for netCDF objects
00019 typedef unsigned int NcBool;    // many members return 0 on failure
00020 
00021 class NcDim;                    // dimensions
00022 class NcVar;                    // variables
00023 class NcAtt;                    // attributes
00024 
00025 
00026 /*
00027  * ***********************************************************************
00028  * A netCDF file.
00029  * ***********************************************************************
00030  */
00031 class NcFile
00032 {
00033   public:
00034 
00035     virtual ~NcFile( void );
00036 
00037     enum FileMode {
00038         ReadOnly,       // file exists, open read-only
00039         Write,          // file exists, open for writing
00040         Replace,        // create new file, even if already exists
00041         New             // create new file, fail if already exists
00042       };
00043 
00044     enum FileFormat {
00045        Classic,         // netCDF classic format (i.e. version 1 format)
00046        Offset64Bits,    // netCDF 64-bit offset format
00047        Netcdf4,         // netCDF-4 using HDF5 format
00048        Netcdf4Classic,  // netCDF-4 using HDF5 format using only netCDF-3 calls
00049        BadFormat
00050     };
00051 
00052     NcFile( const char * path, FileMode = ReadOnly ,
00053             size_t *chunksizeptr = NULL, // optional tuning parameters
00054             size_t initialsize = 0,
00055             FileFormat = Classic );
00056 
00057     NcBool is_valid( void ) const;         // opened OK in ctr, still valid
00058 
00059     int num_dims( void ) const;            // number of dimensions
00060     int num_vars( void ) const;            // number of variables
00061     int num_atts( void ) const;            // number of (global) attributes
00062 
00063     NcDim* get_dim( NcToken ) const;       // dimension by name
00064     NcVar* get_var( NcToken ) const;       // variable by name
00065     NcAtt* get_att( NcToken ) const;       // global attribute by name
00066 
00067     NcDim* get_dim( int ) const;           // n-th dimension
00068     NcVar* get_var( int ) const;           // n-th variable
00069     NcAtt* get_att( int ) const;           // n-th global attribute
00070     NcDim* rec_dim( void ) const;          // unlimited dimension, if any
00071     
00072     // Add new dimensions, variables, global attributes.
00073     // These put the file in "define" mode, so could be expensive.
00074     NcDim* add_dim( NcToken dimname, long dimsize );
00075     NcDim* add_dim( NcToken dimname );     // unlimited
00076 
00077     NcVar* add_var( NcToken varname, NcType type,       // scalar
00078                     const NcDim* dim0=0,                // 1-dim
00079                     const NcDim* dim1=0,                // 2-dim
00080                     const NcDim* dim2=0,                // 3-dim
00081                     const NcDim* dim3=0,                // 4-dim
00082                     const NcDim* dim4=0 );              // 5-dim
00083     NcVar* add_var( NcToken varname, NcType type,       // n-dim
00084                           int ndims, const NcDim** dims );
00085 
00086     NcBool add_att( NcToken attname, char );             // scalar attributes
00087     NcBool add_att( NcToken attname, ncbyte );
00088     NcBool add_att( NcToken attname, short );
00089     NcBool add_att( NcToken attname, long );
00090     NcBool add_att( NcToken attname, int );
00091     NcBool add_att( NcToken attname, float );
00092     NcBool add_att( NcToken attname, double );
00093     NcBool add_att( NcToken attname, const char*);       // string attribute
00094     NcBool add_att( NcToken attname, int, const char* ); // vector attributes
00095     NcBool add_att( NcToken attname, int, const ncbyte* );
00096     NcBool add_att( NcToken attname, int, const short* );
00097     NcBool add_att( NcToken attname, int, const long* );
00098     NcBool add_att( NcToken attname, int, const int* );
00099     NcBool add_att( NcToken attname, int, const float* );
00100     NcBool add_att( NcToken attname, int, const double* );
00101 
00102     enum FillMode {
00103         Fill = NC_FILL,                    // prefill (default)
00104         NoFill = NC_NOFILL,                // don't prefill
00105         Bad
00106       };
00107 
00108     NcBool set_fill( FillMode = Fill );    // set fill-mode
00109     FillMode get_fill( void ) const;       // get fill-mode
00110     FileFormat get_format( void ) const;   // get format version
00111 
00112     NcBool sync( void );                   // synchronize to disk
00113     NcBool close( void );                  // to close earlier than dtr
00114     NcBool abort( void );                  // back out of bad defines
00115     
00116     // Needed by other Nc classes, but users will not need them
00117     NcBool define_mode( void ); // leaves in define mode, if possible
00118     NcBool data_mode( void );   // leaves in data mode, if possible
00119     int id( void ) const;       // id used by C interface
00120 
00121   protected:
00122     int the_id;
00123     int in_define_mode;
00124     FillMode the_fill_mode;
00125     NcDim** dimensions;
00126     NcVar** variables;
00127     NcVar* globalv;             // "variable" for global attributes
00128 };
00129 
00130 /*
00131  * For backward compatibility.  We used to derive NcOldFile and NcNewFile
00132  * from NcFile, but that was over-zealous inheritance.
00133  */
00134 #define NcOldFile NcFile
00135 #define NcNewFile NcFile
00136 #define Clobber Replace
00137 #define NoClobber New
00138 
00139 /*
00140  * **********************************************************************
00141  * A netCDF dimension, with a name and a size.  These are only created
00142  * by NcFile member functions, because they cannot exist independently
00143  * of an open netCDF file.
00144  * **********************************************************************
00145  */
00146 class NcDim
00147 {
00148   public:
00149     NcToken name( void ) const;
00150     long size( void ) const;
00151     NcBool is_valid( void ) const;
00152     NcBool is_unlimited( void ) const;
00153     NcBool rename( NcToken newname );
00154     int id( void ) const;
00155     NcBool sync( void );
00156 
00157   private:
00158     NcFile *the_file;           // not const because of rename
00159     int the_id;
00160     char *the_name;
00161 
00162     NcDim(NcFile*, int num);    // existing dimension
00163     NcDim(NcFile*, NcToken name, long sz); // defines a new dim
00164     virtual ~NcDim( void );
00165     
00166     // to construct dimensions, since constructor is private
00167     friend class NcFile;
00168 };
00169 
00170 
00171 /*
00172  * **********************************************************************
00173  * Abstract base class for a netCDF variable or attribute, both of which
00174  * have a name, a type, and associated values.  These only exist as
00175  * components of an open netCDF file.
00176  * **********************************************************************
00177  */
00178 class NcTypedComponent
00179 {
00180   public:
00181     virtual NcToken name( void ) const = 0;
00182     virtual NcType type( void ) const = 0;
00183     virtual NcBool is_valid( void ) const = 0;
00184     virtual long num_vals( void ) const = 0; 
00185     virtual NcBool rename( NcToken newname ) = 0;
00186     virtual NcValues* values( void ) const = 0; // block of all values
00187 
00188     // The following member functions provide conversions from the value
00189     // type to a desired basic type.  If the value is out of range,
00190     // the default "fill-value" for the appropriate type is returned.
00191 
00192     virtual ncbyte as_ncbyte( long n ) const;    // nth value as an unsgnd char
00193     virtual char as_char( long n ) const;        // nth value as char
00194     virtual short as_short( long n ) const;      // nth value as short
00195     virtual int as_int( long n ) const;          // nth value as int
00196     virtual int as_nclong( long n ) const;       // nth value as nclong (deprecated)
00197     virtual long as_long( long n ) const;        // nth value as long
00198     virtual float as_float( long n ) const;      // nth value as floating-point
00199     virtual double as_double( long n ) const;    // nth value as double
00200     virtual char* as_string( long n ) const;     // nth value as string
00201 
00202   protected:
00203     NcFile *the_file;
00204     NcTypedComponent( NcFile* );
00205     virtual NcValues* get_space( long numVals = 0 ) const;  // to hold values
00206 };
00207 
00208 
00209 /*
00210  * **********************************************************************
00211  * netCDF variables.  In addition to a name and a type, these also have
00212  * a shape, given by a list of dimensions
00213  * **********************************************************************
00214  */
00215 class NcVar : public NcTypedComponent
00216 {
00217   public:
00218     virtual ~NcVar( void );
00219     NcToken name( void ) const;
00220     NcType type( void ) const;
00221     NcBool is_valid( void ) const;
00222     int num_dims( void ) const;         // dimensionality of variable
00223     NcDim* get_dim( int ) const;        // n-th dimension
00224     long* edges( void ) const;          // dimension sizes
00225     int num_atts( void ) const;         // number of attributes
00226     NcAtt* get_att( NcToken ) const;    // attribute by name
00227     NcAtt* get_att( int ) const;        // n-th attribute
00228     long num_vals( void ) const;        // product of dimension sizes
00229     NcValues* values( void ) const;     // all values
00230     
00231     // Put scalar or 1, ..., 5 dimensional arrays by providing enough
00232     // arguments.  Arguments are edge lengths, and their number must not
00233     // exceed variable's dimensionality.  Start corner is [0,0,..., 0] by
00234     // default, but may be reset using the set_cur() member.  FALSE is
00235     // returned if type of values does not match type for variable.
00236     NcBool put( const ncbyte* vals,
00237                 long c0=0, long c1=0, long c2=0, long c3=0, long c4=0 );
00238     NcBool put( const char* vals,
00239                 long c0=0, long c1=0, long c2=0, long c3=0, long c4=0 );
00240     NcBool put( const short* vals,
00241                 long c0=0, long c1=0, long c2=0, long c3=0, long c4=0 );
00242     NcBool put( const int* vals,
00243                 long c0=0, long c1=0, long c2=0, long c3=0, long c4=0 );
00244     NcBool put( const long* vals,
00245                 long c0=0, long c1=0, long c2=0, long c3=0, long c4=0 );
00246     NcBool put( const float* vals,
00247                 long c0=0, long c1=0, long c2=0, long c3=0, long c4=0 );
00248     NcBool put( const double* vals,
00249                 long c0=0, long c1=0, long c2=0, long c3=0, long c4=0 );
00250 
00251     // Put n-dimensional arrays, starting at [0, 0, ..., 0] by default,
00252     // may be reset with set_cur().
00253     NcBool put( const ncbyte* vals, const long* counts );
00254     NcBool put( const char* vals, const long* counts );
00255     NcBool put( const short* vals, const long* counts );
00256     NcBool put( const int* vals, const long* counts );
00257     NcBool put( const long* vals, const long* counts );
00258     NcBool put( const float* vals, const long* counts );
00259     NcBool put( const double* vals, const long* counts );
00260 
00261     // Get scalar or 1, ..., 5 dimensional arrays by providing enough
00262     // arguments.  Arguments are edge lengths, and their number must not
00263     // exceed variable's dimensionality.  Start corner is [0,0,..., 0] by
00264     // default, but may be reset using the set_cur() member.
00265     NcBool get( ncbyte* vals, long c0=0, long c1=0,
00266                 long c2=0, long c3=0, long c4=0 ) const;
00267     NcBool get( char* vals, long c0=0, long c1=0,
00268                 long c2=0, long c3=0, long c4=0 ) const;
00269     NcBool get( short* vals, long c0=0, long c1=0,
00270                 long c2=0, long c3=0, long c4=0 ) const;
00271     NcBool get( int* vals, long c0=0, long c1=0,
00272                 long c2=0, long c3=0, long c4=0 ) const;
00273     NcBool get( long* vals, long c0=0, long c1=0,
00274                 long c2=0, long c3=0, long c4=0 ) const;
00275     NcBool get( float* vals, long c0=0, long c1=0,
00276                 long c2=0, long c3=0, long c4=0 ) const;
00277     NcBool get( double* vals, long c0=0, long c1=0,
00278                 long c2=0, long c3=0, long c4=0 ) const; 
00279 
00280     // Get n-dimensional arrays, starting at [0, 0, ..., 0] by default,
00281     // may be reset with set_cur().
00282     NcBool get( ncbyte* vals, const long* counts ) const;
00283     NcBool get( char* vals, const long* counts ) const;
00284     NcBool get( short* vals, const long* counts ) const;
00285     NcBool get( int* vals, const long* counts ) const;
00286     NcBool get( long* vals, const long* counts ) const;
00287     NcBool get( float* vals, const long* counts ) const;
00288     NcBool get( double* vals, const long* counts ) const;
00289 
00290     NcBool set_cur(long c0=-1, long c1=-1, long c2=-1,
00291                          long c3=-1, long c4=-1);
00292     NcBool set_cur(long* cur);
00293 
00294     // these put file in define mode, so could be expensive
00295     NcBool add_att( NcToken, char );             // add scalar attributes
00296     NcBool add_att( NcToken, ncbyte );
00297     NcBool add_att( NcToken, short );
00298     NcBool add_att( NcToken, int );
00299     NcBool add_att( NcToken, long );
00300     NcBool add_att( NcToken, float );
00301     NcBool add_att( NcToken, double );
00302     NcBool add_att( NcToken, const char* );      // string attribute
00303     NcBool add_att( NcToken, int, const char* ); // vector attributes
00304     NcBool add_att( NcToken, int, const ncbyte* );
00305     NcBool add_att( NcToken, int, const short* );
00306     NcBool add_att( NcToken, int, const int* );
00307     NcBool add_att( NcToken, int, const long* );
00308     NcBool add_att( NcToken, int, const float* );
00309     NcBool add_att( NcToken, int, const double* );
00310 
00311     NcBool rename( NcToken newname );
00312 
00313     long rec_size ( void );             // number of values per record
00314     long rec_size ( NcDim* );           // number of values per dimension slice
00315 
00316     // Though following are intended for record variables, they also work
00317     // for other variables, using first dimension as record dimension.
00318 
00319     // Get a record's worth of data
00320     NcValues *get_rec(void);            // get current record
00321     NcValues *get_rec(long rec);        // get specified record
00322     NcValues *get_rec(NcDim* d);        // get current dimension slice
00323     NcValues *get_rec(NcDim* d, long slice); // get specified dimension slice
00324 
00325     // Put a record's worth of data in current record
00326     NcBool put_rec( const ncbyte* vals );
00327     NcBool put_rec( const char* vals );
00328     NcBool put_rec( const short* vals );
00329     NcBool put_rec( const int* vals );
00330     NcBool put_rec( const long* vals );
00331     NcBool put_rec( const float* vals );
00332     NcBool put_rec( const double* vals );
00333 
00334     // Put a dimension slice worth of data in current dimension slice
00335     NcBool put_rec( NcDim* d, const ncbyte* vals );
00336     NcBool put_rec( NcDim* d, const char* vals );
00337     NcBool put_rec( NcDim* d, const short* vals );
00338     NcBool put_rec( NcDim* d, const int* vals );
00339     NcBool put_rec( NcDim* d, const long* vals );
00340     NcBool put_rec( NcDim* d, const float* vals );
00341     NcBool put_rec( NcDim* d, const double* vals );
00342 
00343     // Put a record's worth of data in specified record
00344     NcBool put_rec( const ncbyte* vals, long rec );
00345     NcBool put_rec( const char* vals, long rec );
00346     NcBool put_rec( const short* vals, long rec );
00347     NcBool put_rec( const int* vals, long rec );
00348     NcBool put_rec( const long* vals, long rec );
00349     NcBool put_rec( const float* vals, long rec );
00350     NcBool put_rec( const double* vals, long rec );
00351 
00352     // Put a dimension slice worth of data in specified dimension slice
00353     NcBool put_rec( NcDim* d, const ncbyte* vals, long slice );
00354     NcBool put_rec( NcDim* d, const char* vals, long slice );
00355     NcBool put_rec( NcDim* d, const short* vals, long slice );
00356     NcBool put_rec( NcDim* d, const int* vals, long slice );
00357     NcBool put_rec( NcDim* d, const long* vals, long slice );
00358     NcBool put_rec( NcDim* d, const float* vals, long slice );
00359     NcBool put_rec( NcDim* d, const double* vals, long slice );
00360 
00361     // Get first record index corresponding to specified key value(s)
00362     long get_index( const ncbyte* vals );
00363     long get_index( const char* vals );
00364     long get_index( const short* vals );
00365     long get_index( const int* vals );
00366     long get_index( const long* vals );
00367     long get_index( const float* vals );
00368     long get_index( const double* vals );
00369 
00370     // Get first index of specified dimension corresponding to key values
00371     long get_index( NcDim* d, const ncbyte* vals );
00372     long get_index( NcDim* d, const char* vals );
00373     long get_index( NcDim* d, const short* vals );
00374     long get_index( NcDim* d, const int* vals );
00375     long get_index( NcDim* d, const long* vals );
00376     long get_index( NcDim* d, const float* vals );
00377     long get_index( NcDim* d, const double* vals );
00378 
00379     // Set current record
00380     void set_rec ( long rec );
00381     // Set current dimension slice
00382     void set_rec ( NcDim* d, long slice );
00383 
00384     int id( void ) const;               // rarely needed, C interface id
00385     NcBool sync( void );
00386     
00387   private:
00388     int dim_to_index(NcDim* rdim);
00389     int the_id;
00390     long* the_cur;
00391     char* the_name;
00392     long* cur_rec;
00393 
00394     // private constructors because only an NcFile creates these
00395     NcVar( void );
00396     NcVar(NcFile*, int);
00397 
00398     int attnum( NcToken attname ) const;
00399     NcToken attname( int attnum ) const;
00400     void init_cur( void );
00401 
00402     // to make variables, since constructor is private
00403   friend class NcFile;
00404 };
00405 
00406 
00407 /*
00408  * **********************************************************************
00409  * netCDF attributes.  In addition to a name and a type, these are each
00410  * associated with a specific variable, or are global to the file.
00411  * **********************************************************************
00412  */
00413 class NcAtt : public NcTypedComponent
00414 {
00415   public:          
00416     virtual ~NcAtt( void );
00417     NcToken name( void ) const;
00418     NcType type( void ) const;
00419     NcBool is_valid( void ) const;
00420     long num_vals( void ) const; 
00421     NcValues* values( void ) const;
00422     NcBool rename( NcToken newname );
00423     NcBool remove( void );
00424 
00425   private:
00426     const NcVar* the_variable;
00427     char* the_name;
00428     // protected constructors because only NcVars and NcFiles create
00429     // attributes
00430     NcAtt( NcFile*, const NcVar*, NcToken);
00431     NcAtt( NcFile*, NcToken); // global attribute
00432     
00433     // To make attributes, since constructor is private
00434   friend class NcFile;
00435   friend NcAtt* NcVar::get_att( NcToken ) const;
00436 };
00437 
00438 
00439 /*
00440  * **********************************************************************
00441  * To control error handling.  Declaring an NcError object temporarily
00442  * changes the error-handling behavior until the object is destroyed, at
00443  * which time the previous error-handling behavior is restored.
00444  * **********************************************************************
00445  */
00446 class NcError {
00447   public:
00448     enum Behavior {
00449         silent_nonfatal = 0,
00450         verbose_nonfatal = NC_VERBOSE,
00451         silent_fatal = NC_FATAL,
00452         verbose_fatal = NC_FATAL | NC_VERBOSE      
00453       };
00454 
00455     // constructor saves previous error state, sets new state
00456     NcError( Behavior b = verbose_fatal );
00457 
00458     // destructor restores previous error state
00459     virtual ~NcError( void );
00460 
00461     int get_err( void );                 // returns most recent error
00462 
00463   private:
00464     int the_old_state;
00465     int the_old_err;
00466 };
00467 
00468 #endif                          /* NETCDF_HH */

Generated on Thu Mar 16 18:10:07 2006 for nco by  doxygen 1.4.4