ncdf4a13/ncgen/main.c File Reference

#include <config.h>
#include <stdio.h>
#include <string.h>
#include <netcdf.h>
#include "generic.h"
#include "ncgen.h"
#include "genlib.h"

Include dependency graph for main.c:

Go to the source code of this file.

Defines

#define SEP   '/'

Functions

int yyparse (void)
static const char * ubasename (const char *av0)
static void usage (void)
int main (int argc, char **argv)
int main (int argc, char *argv[])

Variables

const char * progname
const char * cdlname
int c_flag
int fortran_flag
int netcdf_flag
int cmode_modifier
int nofill_flag
char * netcdf_name = NULL
FILE * yyin = (FILE *) 0


Define Documentation

#define SEP   '/'
 

Referenced by ubasename().


Function Documentation

int main int  argc,
char *  argv[]
 

Definition at line 72 of file main.c.

References c_flag, cdlname, cmode_modifier, derror(), emalloc(), FILE, fortran_flag, getopt, NC_64BIT_OFFSET, NC_CLASSIC_MODEL, NC_NETCDF4, netcdf_flag, netcdf_name, nofill_flag, optarg, opterr, optind, progname, ubasename(), usage(), yyin, and yyparse().

00075 {
00076     extern int optind;
00077     extern int opterr;
00078     extern char *optarg;
00079     int c;
00080     FILE *fp;
00081 
00082 #ifdef __hpux
00083     setlocale(LC_CTYPE,"");
00084 #endif
00085     
00086 #ifdef MDEBUG
00087         malloc_debug(2) ;       /* helps find malloc/free errors on Sun */
00088 #endif /* MDEBUG */
00089 
00090     opterr = 1;                 /* print error message if bad option */
00091     progname = ubasename(argv[0]);
00092     cdlname = "-";
00093 
00094     c_flag = 0;
00095     fortran_flag = 0;
00096     netcdf_flag = 0;
00097     cmode_modifier = 0;
00098     nofill_flag = 0;
00099 
00100 #if _CRAYMPP && 0
00101     /* initialize CRAY MPP parallel-I/O library */
00102     (void) par_io_init(32, 32);
00103 #endif
00104 
00105     while ((c = getopt(argc, argv, "bcfl:no:v:x")) != EOF)
00106       switch(c) {
00107         case 'c':               /* for c output, old version of "-lc" */
00108           c_flag = 1;
00109           break;
00110         case 'f':               /* for fortran output, old version of "-lf" */
00111           fortran_flag = 1;
00112           break;
00113         case 'b':               /* for binary netcdf output, ".nc" extension */
00114           netcdf_flag = 1;
00115           break;
00116         case 'l':               /* specify language, instead of using -c or -f */
00117             {
00118                 char *lang_name = (char *) emalloc(strlen(optarg)+1);
00119                 if (! lang_name) {
00120                     derror ("%s: out of memory", progname);
00121                     return(1);
00122                 }
00123                 (void)strcpy(lang_name, optarg);
00124                 if (strcmp(lang_name, "c") == 0 || strcmp(lang_name, "C") == 0) {
00125                     c_flag = 1;
00126                 }
00127                 else if (strcmp(lang_name, "f77") == 0 || 
00128                          strcmp(lang_name, "fortran77") == 0 ||
00129                          strcmp(lang_name, "Fortran77") == 0) {
00130                     fortran_flag = 1;
00131                 } else {        /* Fortran90, Java, C++, Perl, Python, Ruby, ... */
00132                     derror("%s: output language %s not implemented", 
00133                            progname, lang_name);
00134                     return(1);
00135                 }
00136             }
00137           break;
00138         case 'n':               /* old version of -b, uses ".cdf" extension */
00139           netcdf_flag = -1;
00140           break;
00141         case 'o':               /* to explicitly specify output name */
00142           netcdf_flag = 1;
00143           netcdf_name = (char *) emalloc(strlen(optarg)+1);
00144           if (! netcdf_name) {
00145               derror ("%s: out of memory", progname);
00146               return(1);
00147           }
00148           (void)strcpy(netcdf_name,optarg);
00149           break;
00150         case 'x':               /* set nofill mode to speed up creation of large files */
00151           nofill_flag = 1;
00152           break;
00153         case 'v':               /* for creating 64-bit offset files, specify version 2 */
00154             {
00155                 char *version_name = (char *) emalloc(strlen(optarg)+1);
00156                 if (! version_name) {
00157                     derror ("%s: out of memory", progname);
00158                     return(1);
00159                 }
00160                 (void)strcpy(version_name, optarg);
00161                 /* The default version is version 1, with 32-bit offsets */
00162                 if (strcmp(version_name, "1") == 0 || 
00163                     strcmp(version_name, "classic") == 0) {
00164                     cmode_modifier = 0;
00165                 }
00166                 /* The 64-bit offset version (2)  should only be used if actually needed */
00167                 else if (strcmp(version_name, "2") == 0 || 
00168                     strcmp(version_name, "64-bit-offset") == 0) {
00169                     cmode_modifier |= NC_64BIT_OFFSET;
00170                 }
00171 #ifdef USE_NETCDF4
00172                 /* NetCDF-4 */
00173                 else if (strcmp(version_name, "3") == 0 || 
00174                     strcmp(version_name, "netcdf-4") == 0) {
00175                     cmode_modifier |= NC_NETCDF4;
00176                 }
00177                 /* NetCDF-4 classic */
00178                 else if (strcmp(version_name, "4") == 0 ||
00179                     strcmp(version_name, "netcdf-4-classic") == 0) {
00180                     cmode_modifier |= NC_NETCDF4 | NC_CLASSIC_MODEL;
00181                 }
00182 #endif 
00183                 /* The 64-bit offset version (2)  should only be used if actually needed */
00184                 else 
00185                 {
00186                    derror("Invalid format, try 1, 2, 3, or 4");
00187                    return 2;
00188                 }
00189             }
00190           break;
00191         case '?':
00192           usage();
00193           return(8);
00194       }
00195 
00196     if (fortran_flag && c_flag) {
00197         derror("Only one of -c or -f may be specified");
00198         return(8);
00199       }
00200 
00201     argc -= optind;
00202     argv += optind;
00203 
00204     if (argc > 1) {
00205         derror ("%s: only one input file argument permitted",progname);
00206         return(6);
00207     }
00208 
00209     fp = stdin;
00210     if (argc > 0 && strcmp(argv[0], "-") != 0) {
00211         if ((fp = fopen(argv[0], "r")) == NULL) {
00212             derror ("can't open file %s for reading: ", argv[0]);
00213             perror("");
00214             return(7);
00215         }
00216         cdlname = argv[0];
00217     }
00218     yyin = fp;
00219     return (yyparse());
00220 }

int main int  argc,
char **  argv
 

Definition at line 42 of file tst.cc.

References EXIT_SUCCESS, NC_CLOBBER, NC_ENOTINDEFINE, NC_GLOBAL, NC_INT, NC_NOWRITE, nco_close(), nco_create(), nco_def_dim(), nco_def_var(), nco_enddef(), nco_get_var(), nco_get_xtype(), nco_inq_dimlen(), nco_inq_ndims(), nco_open(), nco_put_att(), nco_put_var(), nco_typ_sng(), TKN2SNG, and type.

00043 {
00044   const std::string fl_in("in.nc"); // [sng] Input file
00045   const std::string fl_out("out.nc"); // [sng] Output file
00046   int rcd(0); // [rcd] Return success code
00047   long bnd_nbr(1); // [nbr] Number of bands
00048   long lat_nbr(1); // [nbr] Number of latitudes
00049   long lon_nbr(1); // [nbr] Number of longitudes
00050   long sz_nbr(1); // [nbr] Number of sizes
00051   register long idx; // [idx] Counting index
00052 
00053   const std::string CVS_Date("$Date: 2005/01/07 23:54:59 $"); // [sng] CVS date string
00054   const std::string CVS_Header("$Header: /cvsroot/nco/nco/src/nco_c++/tst.cc,v 1.16 2005/01/07 23:54:59 zender Exp $"); // [sng] CVS header string
00055   const std::string CVS_Id("$Id: tst.cc,v 1.16 2005/01/07 23:54:59 zender Exp $"); // [sng] CVS identification string
00056   const std::string CVS_Revision("$Revision: 1.16 $"); // [sng] CVS revision string
00057   const std::string date_cvs(CVS_Date.length() > 7 ? CVS_Date.substr(7,19) : static_cast<std::string>("Unknown")); // [sng] Date from CVS
00058   const std::string sbr_nm("main"); // [sng] Subroutine name
00059   const std::string prg_nm("libnco_c++"); // [sng] Program name
00060   const std::string vrs_cvs(CVS_Revision.length() > 10 ? CVS_Revision.substr(10,4) : static_cast<std::string>("Unknown")); // [sng] Version from CVS
00061 #define XTKN2SNG(x) #x
00062 #define TKN2SNG(x) XTKN2SNG(x)
00063   const std::string date_cpp(__DATE__); // [sng] Date from C pre-processor
00064   const std::string time_cpp(__TIME__); // [sng] Time from C pre-processor
00065   const std::string vrs_cpp(TKN2SNG(VERSION)); // [sng] Version from C pre-processor
00066   const std::string hst_cpp(TKN2SNG(HOSTNAME)); // [sng] Hostname from C pre-processor
00067   const std::string usr_cpp(TKN2SNG(USER)); // [sng] Hostname from C pre-processor
00068   if(vrs_cvs == "Unknown") std::cerr << prg_nm << " version " << vrs_cpp << " built " << date_cpp << " on " << hst_cpp << " by " << usr_cpp << std::endl;
00069   if(vrs_cvs != "Unknown") std::cerr << prg_nm << " version " << vrs_cvs << " last modified " << date_cvs << " built " << date_cpp << " on " << hst_cpp << " by " << usr_cpp << std::endl;
00070 
00071   // Open input file
00072   int nc_id=nco_open(fl_in,NC_NOWRITE); // [fnc] Open netCDF file
00073   // Input required data
00074   const long wvl_nbr(nco_inq_dimlen(nc_id,static_cast<std::string>("wvl"))); // [nbr] Number of wavelengths
00075   std::cerr << "Number of wavelengths in "+fl_in+" is "<< wvl_nbr << std::endl;
00076   /* netCDF C++ interface automatically allocates memory required by get_var()
00077      User is responsible for freeing this memory when no longer needed
00078      Currently this is done after input array is written to output file */
00079   prc_cmp *wvl; // [m] Wavelength
00080   rcd=nco_get_var(nc_id,static_cast<std::string>("wvl"),wvl); // [m] Wavelength
00081   std::cerr << "Value of wvl[0] in "+fl_in+" is "<< wvl[0] << std::endl;
00082   rcd=nco_close(nc_id); // [fnc] Close netCDF file
00083   std::valarray<prc_cmp> wvl_valarray(0.0,wvl_nbr); // [frc] 
00084 
00085   // Allocate dynamic arrays
00086   prc_cmp *sz=new prc_cmp[sz_nbr]; // [m] Size at bin center
00087   long double *sz_ldb=new long double[sz_nbr]; // [m] Size at bin center
00088 
00089   // Dummy data
00090   for(idx=0;idx<sz_nbr;idx++){
00091     sz[idx]=0.5e-6; // [m] Size at bin center
00092     sz_ldb[idx]=sz[idx]; // [m] Size at bin center
00093   } // end loop over idx
00094 
00095   // Open output file
00096   const int nc_out(nco_create(fl_out,NC_CLOBBER)); 
00097   const nc_type nco_xtyp(nco_get_xtype(static_cast<prc_cmp>(1.0))); // [enm] External netCDF type
00098   std::cout << "INFO External netCDF type of prc_cmp variables will be " << nco_typ_sng(nco_xtyp) << std::endl;
00099  
00100   // Create dimensions
00101   const int sz_dmn(nco_def_dim(nc_out,static_cast<std::string>("sz"),sz_nbr)); // [dmn] Size dimension
00102   const int bnd_dmn(nco_def_dim(nc_out,static_cast<std::string>("bnd"),bnd_nbr)); // [dmn] Band dimension
00103   const int wvl_dmn(nco_def_dim(nc_out,static_cast<std::string>("wvl"),wvl_nbr)); // [dmn] Wavelength dimension
00104 
00105   // Derive dimensions
00106   const int dmn_bnd_sz[2]={bnd_dmn,sz_dmn};
00107   const int *dmn_sz(&sz_dmn); // [dmn] Pointer to size dimension
00108   const int *dmn_wvl(&wvl_dmn); // [dmn] Pointer to wavelength dimension
00109   const int CEWI_int(-2147483647); // [frc] Compiler Error Warning Initializer for int
00110   const int *dmn_scl(&CEWI_int); // [dmn] Dummy argument, not used
00111 
00112   std::cerr << "Currently there are " << nco_inq_ndims(nc_out) << " dimensions defined" << std::endl;
00113 
00114   // Global attributes
00115   rcd=nco_put_att(nc_out,NC_GLOBAL,"CVS_Id",CVS_Id);
00116 
00117   var_mtd_sct var_mtd[]={
00118     {0,"wvl",nco_xtyp,1,dmn_wvl,"long_name","Wavelength at band center","units","meter"},
00119     {0,"wvl_valarray",nco_xtyp,1,dmn_wvl,"long_name","valarray","units","meter"},
00120     {0,"sz",nco_xtyp,1,dmn_sz,"long_name","Size at bin center","units","meter"},
00121     {0,"sz_ldb",nco_xtyp,1,dmn_sz,"long_name","Size at bin center","units","meter"},
00122     {0,"sz_nbr",NC_INT,0,dmn_scl,"long_name","Number of sizes","units","number"},
00123   }; // end var_mtd_sct var_mtd[]
00124   const int var_mtd_nbr(sizeof(var_mtd)/sizeof(var_mtd_sct));
00125 
00126   sng2var_mtd_map var_mtd_map;
00127   for(idx=0;idx<var_mtd_nbr;idx++){
00128     /* fxm: Define variables before inserting into map, because map values 
00129        seem to be unwritable (read-only) once they are in map. */
00130     rcd=nco_def_var(nc_out,var_mtd[idx].nm,var_mtd[idx].type,var_mtd[idx].dmn_nbr,var_mtd[idx].dmn_id,var_mtd[idx].var_id);
00131     var_mtd_map.insert(sng2var_mtd_map::value_type(var_mtd[idx].nm,var_mtd[idx]));
00132   } // end loop over itr
00133 
00134   sng2var_mtd_map::const_iterator var_mtd_itr;
00135   for(var_mtd_itr=var_mtd_map.begin();var_mtd_itr!=var_mtd_map.end();++var_mtd_itr){
00136     // Write first attribute (long_name)
00137     rcd=nco_put_att(nc_out,var_mtd_itr->second.var_id,var_mtd_itr->second.att_1_nm,var_mtd_itr->second.att_1_val);
00138     // Write second attribute (units)
00139     rcd=nco_put_att(nc_out,var_mtd_itr->second.var_id,var_mtd_itr->second.att_2_nm,var_mtd_itr->second.att_2_val);
00140     std::cout << "Defined " << var_mtd_itr->first << " with long_name = " << var_mtd_itr->second.att_1_val << " and units = " << var_mtd_itr->second.att_2_val << std::endl;
00141   } // end loop over var_mtd_itr
00142 
00143   // Leave define mode
00144   rcd=nco_enddef(nc_out,NC_ENOTINDEFINE); // [fnc] Leave define mode
00145 
00146   // Write data and delete dynamic arrays
00147   // Syntax valid for statically allocated arrays foo[dim1][dim2]
00148   // rcd=nco_put_var(nc_out,static_cast<std::string>("tpt_d2d"),&tpt_d2d[0]);
00149   // Syntax valid for vector< vector<prc_cmp> > foo(dim1,vector<prc_cmp>(dim2))
00150   // rcd=nco_put_var(nc_out,static_cast<std::string>("tpt_v2d"),&tpt_v2d[0][0]);
00151   // Syntax valid for vector<prc_cmp> foo(dim1*dim2)
00152   // rcd=nco_put_var(nc_out,static_cast<std::string>("tpt_v1d"),&tpt_v1d[0]);
00153   // Syntax valid for a2d_cls<prc_cmp> foo(dim1,dim2)
00154   // rcd=nco_put_var(nc_out,static_cast<std::string>("tpt_a2d"),&tpt_a2d(0,0));
00155   rcd=nco_put_var(nc_out,static_cast<std::string>("wvl_valarray"),wvl_valarray);
00156   rcd=nco_put_var(nc_out,static_cast<std::string>("wvl"),wvl); delete []wvl;
00157   rcd=nco_put_var(nc_out,static_cast<std::string>("sz"),sz); delete []sz;
00158   rcd=nco_put_var(nc_out,static_cast<std::string>("sz_ldb"),sz_ldb); delete []sz_ldb;
00159   rcd=nco_put_var(nc_out,static_cast<std::string>("sz_nbr"),sz_nbr);
00160 
00161   // Close output file
00162   rcd=nco_close(nc_out); // [fnc] Close netCDF file
00163   std::cerr << "Wrote results to " << fl_out << std::endl;
00164   std::cerr << "ncks: ncks -C -H -F -m -u -d wvl,0.5e-6 -v wvl " << fl_out << std::endl;
00165 
00166   // Fix all unused identifiers at end where no harm can be done
00167   idx=0*(rcd+lat_nbr+lon_nbr+dmn_bnd_sz[0]+argc+sizeof(argv)); // [idx] Counting index CEWI
00168 
00169   return EXIT_SUCCESS;
00170 } // end main()

static const char * ubasename const char *  av0  )  [static]
 

Definition at line 42 of file main.c.

References SEP.

Referenced by main().

00044 {
00045         const char *logident ;
00046 #ifdef VMS
00047 #define SEP     ']'
00048 #endif
00049 #ifdef MSDOS
00050 #define SEP     '\\'
00051 #endif
00052 #ifndef SEP
00053 #define SEP     '/'
00054 #endif
00055         if ((logident = strrchr(av0, SEP)) == NULL)
00056                 logident = av0 ;
00057         else
00058             logident++ ;
00059         return logident ;
00060 }

static void usage void   )  [static]
 

Definition at line 63 of file main.c.

References derror(), nc_inq_libvers, and progname.

00064 {
00065     derror("Usage: %s [ -b ] [ -c ] [ -f ] [ -v version ] [ -x ] [ -o outfile]  [ file ... ]",
00066            progname);
00067     derror("netcdf library version %s", nc_inq_libvers());
00068 }

int yyparse void   ) 
 

Referenced by main().


Variable Documentation

int c_flag
 

Definition at line 26 of file main.c.

const char* cdlname
 

Definition at line 24 of file main.c.

Referenced by derror(), and main().

int cmode_modifier
 

Definition at line 29 of file main.c.

Referenced by gen_c(), gen_fortran(), gen_netcdf(), and main().

int fortran_flag
 

Definition at line 27 of file main.c.

int netcdf_flag
 

Definition at line 28 of file main.c.

char* netcdf_name = NULL
 

Definition at line 31 of file main.c.

Referenced by define_netcdf(), and main().

int nofill_flag
 

Definition at line 30 of file main.c.

Referenced by gen_c(), gen_fortran(), gen_netcdf(), and main().

const char* progname
 

Definition at line 23 of file main.c.

FILE * yyin = (FILE *) 0
 

Definition at line 266 of file ncgenyy.c.

Referenced by main().


Generated on Thu Mar 16 18:12:22 2006 for nco by  doxygen 1.4.4