nco_c++/nco_utl.hh File Reference

#include <iostream>
#include <string>
#include <stdlib.h>
#include <netcdf.h>
#include <nco_att.hh>
#include <nco_dmn.hh>
#include <nco_var.hh>

Include dependency graph for nco_utl.hh:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void nco_err_exit (const int &rcd, const std::string &msg, const std::string &msg_opt="")
void nco_wrn_prn (const std::string &msg, const std::string &msg_opt="")
int nco_inq_varsz (const int &nc_id, const int &var_id, size_t &var_sz)
size_t nco_inq_varsz (const int &nc_id, const int &var_id)
int nco_inq_varsrt (const int &nc_id, const int &var_id, std::valarray< size_t > &srt, const int &rcd_opt=NC_NOERR)
int nco_typ_lng (const nc_type &nco_typ)
std::string nco_typ_sng (const nc_type &nco_typ)
std::string nco_c_typ_sng (const nc_type &nco_typ)
std::string nco_ftn_typ_sng (const nc_type &nco_typ)
void nco_dfl_case_nctype_err (void)
nc_type nco_get_xtype (const float &var_val)
nc_type nco_get_xtype (const double &var_val)


Function Documentation

std::string nco_c_typ_sng const nc_type nco_typ  ) 
 

Definition at line 177 of file nco_utl.cc.

References NC_BYTE, NC_CHAR, NC_DOUBLE, NC_FLOAT, NC_INT, NC_SHORT, and nco_dfl_case_nctype_err().

00178 {
00179   /* Purpose: Divine internal (native) C type string from netCDF external type enum
00180      fxm: This breaks on Crays where both NC_FLOAT and NC_DOUBLE are native type float */
00181   switch(nco_typ){
00182   case NC_FLOAT:
00183     return "float";
00184   case NC_DOUBLE:
00185     return "double";
00186   case NC_INT:
00187     return "long";
00188   case NC_SHORT:
00189     return "short";
00190   case NC_CHAR:
00191     return "unsigned char";
00192   case NC_BYTE:
00193     return "signed char";
00194   default: nco_dfl_case_nctype_err(); break;
00195   } // end switch
00196   // Some C compilers, e.g., SGI cc, need a return statement at the end of non-void functions
00197   return static_cast<std::string>("");
00198 } // end nco_c_typ_sng()

void nco_dfl_case_nctype_err void   ) 
 

Definition at line 226 of file nco_utl.cc.

References EXIT_FAILURE.

Referenced by nco_c_typ_sng(), nco_ftn_typ_sng(), nco_typ_lng(), and nco_typ_sng().

00227 {
00228   /* Purpose: Convenience routine for printing error and exiting when
00229      switch(nctype) statement receives an illegal default case
00230      NCO emits warnings when compiled by GCC with -DNETCDF2_ONLY since, 
00231      apparently, there are a whole bunch of things besides numeric
00232      types in the old nctype enum and gcc warns about enums that are
00233      not exhaustively considered in switch() statements. 
00234      All these default statements can be removed with netCDF3 interface
00235      so perhaps these should be surrounded with #ifdef NETCDF2_ONLY
00236      constructs, but they actually do make sense for netCDF3 as well
00237      so I have implemented a uniform error function, nco_dfl_case_nctype_err(), 
00238      to be called by all routines which emit errors only when compiled with
00239      NETCDF2_ONLY.
00240      This makes the behavior easy to modify or remove in the future.
00241 
00242      Placing this in its own routine also has the virtue of saving many lines 
00243      of code since this function is used in many many switch() statements. */
00244   const std::string sbr_nm("nco_dfl_case_nctype_err()");
00245   std::cout << sbr_nm << ": ERROR switch(nctype) statement fell through to default case, which is illegal.\nNot handling the default case causes gcc to emit warnings when compiling NCO with the NETCDF2_ONLY token (because nctype defintion is braindead in netCDF2). Exiting..." << std::endl;
00246 #ifdef ABORT_ON_ERROR
00247   std::abort(); // [fnc] Produce core dump
00248 #else
00249 #ifndef SGIMP64 // fxm: SGI IRIX CC does not support std::exit nor have cstdlib
00250     std::exit(EXIT_FAILURE); // [fnc] Exit nicely
00251 #else // SGIMP64
00252     exit(EXIT_FAILURE); // [fnc] Exit nicely
00253 #endif // SGIMP64
00254 #endif // !ABORT_ON_ERROR
00255 
00256 } // end nco_dfl_case_nctype_err()

void nco_err_exit const int &  rcd,
const std::string &  msg,
const std::string &  msg_opt = ""
 

Definition at line 32 of file nco_utl.cc.

References EXIT_FAILURE, NC_NOERR, and nc_strerror.

00035 {
00036   /* Purpose: Print netCDF error message, routine name, and exit 
00037      Routine is called by all wrappers when fatal error is encountered
00038      msg variable allows wrapper to pass more descriptive information than 
00039      netCDF-defined error message contains.
00040      Use msg to print, e.g., name of variable which caused error */
00041   const std::string sbr_nm("nco_err_exit()");
00042   if(rcd != NC_NOERR){
00043     std::cout << sbr_nm << ": ERROR netCDF library returned error code " << rcd << std::endl;
00044     std::cout << sbr_nm << ": ERROR " << msg << std::endl << nc_strerror(rcd) << std::endl;
00045     if(msg_opt != "") std::cout << sbr_nm << ": " << msg_opt << std::endl;
00046 #ifdef ABORT_ON_ERROR
00047     std::abort(); // [fnc] Produce core dump
00048 #else // !ABORT_ON_ERROR
00049 #ifndef SGIMP64 // fxm: SGI IRIX CC does not support std::exit nor have cstdlib
00050     std::exit(EXIT_FAILURE); // [fnc] Exit nicely
00051 #else // SGIMP64
00052     exit(EXIT_FAILURE); // [fnc] Exit nicely
00053 #endif // SGIMP64
00054 #endif // !ABORT_ON_ERROR
00055   } // endif error
00056 } // end nco_err_exit()

std::string nco_ftn_typ_sng const nc_type nco_typ  ) 
 

Definition at line 202 of file nco_utl.cc.

References NC_BYTE, NC_CHAR, NC_DOUBLE, NC_FLOAT, NC_INT, NC_SHORT, and nco_dfl_case_nctype_err().

00203 {
00204   /* Purpose: Divine internal (native) Fortran type string from netCDF external type enum
00205      fxm: This breaks on Crays where both NC_FLOAT and NC_DOUBLE are native type real */
00206   switch(nco_typ){
00207   case NC_FLOAT:
00208     return "real";
00209   case NC_DOUBLE:
00210     return "double precision";
00211   case NC_INT:
00212     return "integer";
00213   case NC_SHORT:
00214     return "integer*2";
00215   case NC_CHAR:
00216     return "character";
00217   case NC_BYTE:
00218     return "char";
00219   default: nco_dfl_case_nctype_err(); break;
00220   } /* end switch */
00221   // Some C compilers, e.g., SGI cc, need a return statement at the end of non-void functions
00222   return static_cast<std::string>("");
00223 } // end nco_ftn_typ_sng()

nc_type nco_get_xtype const double &  var_val  ) 
 

Definition at line 274 of file nco_utl.cc.

References NC_DOUBLE.

00275 {
00276   /* Purpose: Return "best" netCDF external type for argument
00277      Algorithm assumes internal representation is IEEE
00278      Thus algorithm fails for Crays, where native float is 8 B (double precision) */
00279   double dbl_foo=var_val; // CEWI
00280   dbl_foo++; // Squelch "declared but never referenced" warnings
00281   return NC_DOUBLE;
00282 } // end nco_get_xtype()

nc_type nco_get_xtype const float &  var_val  ) 
 

Definition at line 262 of file nco_utl.cc.

References NC_FLOAT.

00263 {
00264   /* Purpose: Return "best" netCDF external type for argument
00265      Algorithm assumes internal representation is IEEE
00266      Thus algorithm fails for Crays, where native float is 8 B (double precision) */
00267   float flt_foo=var_val; // CEWI
00268   flt_foo++; // Squelch "declared but never referenced" warnings
00269   return NC_FLOAT;
00270 } // end nco_get_xtype()

int nco_inq_varsrt const int &  nc_id,
const int &  var_id,
std::valarray< size_t > &  srt,
const int &  rcd_opt = NC_NOERR
 

Definition at line 114 of file nco_utl.cc.

References nco_inq_varndims().

00118 {
00119   // Purpose: Return valarray of default starting indices
00120   int dmn_nbr; // I [nbr] Number of dimensions
00121   int rcd=nco_inq_varndims(nc_id,var_id,dmn_nbr,rcd_opt);
00122   srt.resize(dmn_nbr);
00123   srt=1;
00124   return rcd;
00125 } // end nco_inq_varsrt() 

size_t nco_inq_varsz const int &  nc_id,
const int &  var_id
 

Definition at line 100 of file nco_utl.cc.

References nco_inq_varsz().

00102 {
00103   // Purpose: Compute size (number of elements) in specified variable
00104   size_t var_sz; // O [nbr] Variable size
00105   int rcd=nco_inq_varsz(nc_id,var_id,var_sz);
00106   rcd+=0; /* CEWI */
00107   return var_sz;
00108 } // end nco_inq_varsz()

int nco_inq_varsz const int &  nc_id,
const int &  var_id,
size_t &  var_sz
 

Definition at line 77 of file nco_utl.cc.

References nco_inq_dimlen(), nco_inq_vardimid(), and nco_inq_varndims().

00080 {
00081   // Purpose: Compute size (number of elements) in specified variable
00082   int dmn_nbr; // [nbr] Number of dimensions
00083   //  int dmn_id[NC_MAX_DIMS]; // [id] Dimension IDs
00084   size_t dmn_sz; // [nbr] Dimension size
00085   int rcd=nco_inq_varndims(nc_id,var_id,dmn_nbr);
00086   int *dmn_id=new int[dmn_nbr]; // [id] Dimension IDs
00087   rcd=nco_inq_vardimid(nc_id,var_id,dmn_id);
00088   // Initialize variable size
00089   var_sz=1; // [nbr] Variable size
00090   for(int dmn_idx=0;dmn_idx<dmn_nbr;dmn_idx++){
00091     rcd=nco_inq_dimlen(nc_id,dmn_id[dmn_idx],dmn_sz);
00092     var_sz*=dmn_sz; // [nbr] Variable size
00093   } // end loop over dmn
00094   delete []dmn_id; // [id] Dimension IDs
00095   return rcd;
00096 } // end nco_inq_varsz()

int nco_typ_lng const nc_type nco_typ  ) 
 

Definition at line 129 of file nco_utl.cc.

References NC_BYTE, NC_CHAR, NC_DOUBLE, NC_FLOAT, NC_INT, NC_SHORT, and nco_dfl_case_nctype_err().

00130 { 
00131   /* Purpose: Return native size of specified netCDF type
00132      Routine is used to determine memory required to store variables in RAM */
00133   switch(nco_typ){ 
00134   case NC_FLOAT: 
00135     return sizeof(float); 
00136   case NC_DOUBLE: 
00137     return sizeof(double); 
00138   case NC_INT: 
00139     return sizeof(long); 
00140   case NC_SHORT: 
00141     return sizeof(short); 
00142   case NC_CHAR: 
00143     return sizeof(unsigned char); 
00144   case NC_BYTE: 
00145     return sizeof(signed char); 
00146   default: nco_dfl_case_nctype_err(); break;
00147   } // end switch
00148   // Some C compilers, e.g., SGI cc, need a return statement at the end of non-void functions
00149   return (int)-1;
00150 } // end nco_typ_lng()

std::string nco_typ_sng const nc_type nco_typ  ) 
 

Definition at line 154 of file nco_utl.cc.

References NC_BYTE, NC_CHAR, NC_DOUBLE, NC_FLOAT, NC_INT, NC_SHORT, and nco_dfl_case_nctype_err().

00155 {
00156   switch(nco_typ){
00157   case NC_FLOAT:
00158     return "NC_FLOAT";
00159   case NC_DOUBLE:
00160     return "NC_DOUBLE";
00161   case NC_INT:
00162     return "NC_INT";
00163   case NC_SHORT:
00164     return "NC_SHORT";
00165   case NC_CHAR:
00166     return "NC_CHAR";
00167   case NC_BYTE:
00168     return "NC_BYTE";
00169   default: nco_dfl_case_nctype_err(); break;
00170   } // end switch
00171   // Some C compilers, e.g., SGI cc, need a return statement at the end of non-void functions
00172   return static_cast<std::string>("");
00173 } /* end nco_typ_sng() */

void nco_wrn_prn const std::string &  msg,
const std::string &  msg_opt = ""
 

Definition at line 60 of file nco_utl.cc.

Referenced by nco_get_att().

00062 {
00063   /* Purpose: Print NCO warning message and return
00064      Routine is called by some wrappers when non-fatal weirdness is encountered
00065      msg variable allows wrapper to pass descriptive information
00066      Use msg to print, e.g., name of variable which caused warning */
00067   const std::string sbr_nm("nco_wrn_prn()");
00068   std::cout << sbr_nm << ": WARNING " << msg << std::endl;
00069   if(msg_opt != "") std::cout << sbr_nm << ": " << msg_opt << std::endl;
00070   return;
00071 } // end nco_wrn_prn()


Generated on Thu Mar 16 18:17:24 2006 for nco by  doxygen 1.4.4