#include <assert.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <time.h>#include <unistd.h>#include <string>#include "nco_getopt.h"#include <netcdf.h>#include "nco_netcdf.h"#include "ncap2.hh"#include "libnco++.hh"#include "libnco.h"Include dependency graph for ncap2.cc:

Go to the source code of this file.
Defines | |
| #define | MAIN_PROGRAM_FILE |
| #define | NCAP_SPT_NBR_MAX 100 |
Functions | |
| int | main (int argc, char **argv) |
Variables | |
| size_t | ncap_ncl_dpt_crr = 0UL |
| size_t * | ncap_ln_nbr_crr |
| char ** | ncap_fl_spt_glb = NULL |
|
|
|
|
|
Referenced by main(). |
|
||||||||||||
|
Definition at line 81 of file ncap2.cc. References acosf(), acoshf(), aed_overwrite, asinf(), asinhf(), atanf(), atanhf(), aed_sct::att_nm, ceilf(), copyright_prn(), cosf(), coshf(), dbg_lvl_get(), erfcf(), erff(), EXIT_FAILURE, EXIT_SUCCESS, expf(), fabsf(), False, FILE, NcapVarVector::find(), NcapVector< T >::find(), prs_sct::fl_in, prs_sct::fl_out, floorf(), gammaf(), getopt_long, nm_id_sct::id, prs_sct::in_id, int_CEWI, log10f(), logf(), lst_prs_2D(), aed_sct::mode, option::name, NC_FORMAT_64BIT, NC_FORMAT_CLASSIC, NC_FORMAT_NETCDF4, NC_GLOBAL, NC_MAX_ATTRS, NC_MAX_DIMS, NC_MAX_NAME, NC_NOERR, NC_NOFILL, NC_NOWRITE, ncap_att, NCAP_SPT_NBR_MAX, ncap_sym_init(), nco_aed_prc(), nco_att_cpy(), nco_att_lst_mk(), nco_bool, nco_close(), nco_cmd_ln_sng(), nco_cnv_ccm_ccsm_cf_inq(), nco_create_mode_prs(), nco_dmn_dfn(), nco_dmn_dpl(), nco_dmn_fll(), nco_dmn_lmt_mrg(), nco_dmn_lst(), nco_dmn_lst_ass_var(), nco_dmn_lst_free(), nco_dmn_xrf(), nco_enddef(), nco_exit(), nco_exit_gracefully(), nco_fl_lst_mk(), nco_fl_mk_lcl(), nco_fl_nm_prs(), nco_fl_out_cls(), nco_fl_out_open(), nco_fl_rm(), nco_free(), nco_hst_att_cat(), nco_inq(), nco_inq_varid_flg(), nco_lbr_vrs_prn(), nco_lmt_lst_free(), nco_lmt_prs(), nco_lst_srt_nm_id(), nco_malloc(), nco_nm_id_lst_free(), nco_open(), nco_pck_map_nil, nco_pck_plc_nil, nco_realloc(), nco_redef(), nco_set_fill(), nco_sng_lst_free(), nco_usg_prn(), nco_var_dfn(), nco_var_dpl(), nco_var_fll(), nco_var_lst_dvd(), nco_var_lst_free(), nco_var_lst_mk(), nco_var_lst_sub(), nco_var_val_cpy(), nco_xrf_dmn(), nco_xrf_var(), nearbyintf(), nm_id_sct::nm, no_argument, prs_sct::ntl_scn, NULL_CEWI, optarg, optind, prs_sct::out_id, parse_antlr(), prg_nm_get(), prg_prs(), NcapVector< T >::ptr(), prs_sct::ptr_dmn_in_vtr, prs_sct::ptr_dmn_out_vtr, prs_sct::ptr_sym_vtr, prs_sct::ptr_var_vtr, NcapVector< T >::push(), required_argument, rintf(), rnd_nbr(), rnd_nbrf(), roundf(), sinf(), sinhf(), NcapVarVector::size(), NcapVector< T >::size(), size_t_CEWI, sqrtf(), aed_sct::sz, tanf(), tanhf(), True, truncf(), aed_sct::type, aed_sct::val, aed_sct::var_nm, and yyin. 00082 { 00083 FILE *yyin; /* file handle used to check file existance */ 00084 int parse_antlr(prs_sct*,char*,char*); 00085 00086 /* fxm TODO nco652 */ 00087 double rnd_nbr(double); 00088 00089 nco_bool EXCLUDE_INPUT_LIST=False; /* Option c */ 00090 nco_bool EXTRACT_ALL_COORDINATES=False; /* Option c */ 00091 nco_bool EXTRACT_ASSOCIATED_COORDINATES=True; /* Option C */ 00092 nco_bool FILE_RETRIEVED_FROM_REMOTE_LOCATION; 00093 nco_bool FL_LST_IN_FROM_STDIN=False; /* [flg] fl_lst_in comes from stdin */ 00094 nco_bool FORCE_APPEND=False; /* Option A */ 00095 nco_bool FORCE_OVERWRITE=False; /* Option O */ 00096 nco_bool FORTRAN_IDX_CNV=False; /* Option F */ 00097 nco_bool HISTORY_APPEND=True; /* Option h */ 00098 nco_bool CNV_CCM_CCSM_CF; 00099 nco_bool PRN_FNC_TBL=False; /* Option f */ 00100 nco_bool PROCESS_ALL_VARS=True; /* Option v */ 00101 nco_bool REMOVE_REMOTE_FILES_AFTER_PROCESSING=True; /* Option R */ 00102 00103 char **fl_lst_abb=NULL; /* Option n */ 00104 char **fl_lst_in; 00105 char **var_lst_in=NULL_CEWI; 00106 char *cmd_ln; 00107 char *fl_in=NULL; 00108 char *fl_out=NULL; /* Option o */ 00109 char *fl_out_tmp; 00110 char *fl_pth=NULL; /* Option p */ 00111 char *fl_pth_lcl=NULL; /* Option l */ 00112 char *fl_spt_usr=NULL; /* Option s */ 00113 char *lmt_arg[NC_MAX_DIMS]; 00114 char *opt_crr=NULL; /* [sng] String representation of current long-option name */ 00115 #define NCAP_SPT_NBR_MAX 100 00116 char *spt_arg[NCAP_SPT_NBR_MAX]; /* fxm: Arbitrary size, should be dynamic */ 00117 char *spt_arg_cat=NULL; /* [sng] User-specified script */ 00118 char *time_bfr_srt; 00119 00120 const char * const CVS_Id="$Id: ncap2.cc,v 1.7 2006/02/25 22:29:14 zender Exp $"; 00121 const char * const CVS_Revision="$Revision: 1.7 $"; 00122 const char * const opt_sht_lst="4ACcD:d:Ffhl:n:Oo:p:Rrs:S:vx-:"; /* [sng] Single letter command line options */ 00123 00124 dmn_sct **dmn_in=NULL_CEWI; /* [lst] Dimensions in input file */ 00125 dmn_sct **dmn_out=NULL_CEWI; /* [lst] Dimensions written to output file */ 00126 dmn_sct *dmn_new; 00127 dmn_sct *dmn_item; 00128 00129 // template lists 00130 NcapVector<sym_sct*> sym_vtr; 00131 NcapVector<dmn_sct*> dmn_in_vtr; 00132 NcapVector<dmn_sct*> dmn_out_vtr; 00133 00134 // Holder for attributtes and vectors 00135 NcapVarVector var_vtr; 00136 00137 extern char *optarg; 00138 extern int optind; 00139 00140 00141 /* Math float prototypes required by AIX, Solaris, but not by Linux, IRIX */ 00142 /* Basic math: acos, asin, atan, cos, exp, fabs, log, log10, sin, sqrt, tan */ 00143 00144 /* GNU g++ barfs at these float declartions -- remove if g++ used */ 00145 00146 #ifndef __GNUG__ 00147 00148 extern float acosf(float); 00149 extern float asinf(float); 00150 extern float atanf(float); 00151 extern float cosf(float); 00152 extern float expf(float); 00153 extern float fabsf(float); /* 20040629: Only AIX may need this */ 00154 extern float logf(float); 00155 extern float log10f(float); 00156 extern float rnd_nbrf(float); 00157 extern float sinf(float); 00158 extern float sqrtf(float); 00159 extern float tanf(float); 00160 00161 /* Advanced math: erf, erfc, gamma */ 00162 extern float erff(float); 00163 extern float erfcf(float); 00164 extern float gammaf(float); 00165 00166 /* Hyperbolic trigonometric: acosh, asinh, atanh, cosh, sinh, tanh */ 00167 extern float acoshf(float); 00168 extern float asinhf(float); 00169 extern float atanhf(float); 00170 extern float coshf(float); 00171 extern float sinhf(float); 00172 extern float tanhf(float); 00173 00174 /* Basic Rounding: ceil, floor */ 00175 extern float ceilf(float); 00176 extern float floorf(float); 00177 00178 /* Advanced Rounding: nearbyint, rint, round, trunc */ 00179 extern float nearbyintf(float); 00180 extern float rintf(float); 00181 extern float roundf(float); 00182 extern float truncf(float); 00183 #endif 00184 00185 int abb_arg_nbr=0; 00186 int fl_nbr=0; 00187 int fl_out_fmt=NC_FORMAT_CLASSIC; /* [enm] Output file format */ 00188 int fll_md_old; /* [enm] Old fill mode */ 00189 int idx; 00190 int in_id; 00191 int jdx; 00192 int lmt_nbr=0; /* Option d. NB: lmt_nbr gets incremented */ 00193 int nbr_dmn_ass=int_CEWI;/* Number of dimensions in temporary list */ 00194 int nbr_dmn_in=int_CEWI; /* Number of dimensions in dim_in */ 00195 int nbr_dmn_out=0; /* [nbr] Number of dimensions in list dmn_out */ 00196 int nbr_lst_a=0; /* size of xtr_lst_a */ 00197 int nbr_spt=0; /* Option s. NB: nbr_spt gets incremented */ 00198 int nbr_var_fix; /* nbr_var_fix gets incremented */ 00199 int nbr_var_fl;/* number of vars in a file */ 00200 int nbr_var_prc; /* nbr_var_prc gets incremented */ 00201 int nbr_xtr=0; /* nbr_xtr will not otherwise be set for -c with no -v */ 00202 int opt; 00203 int out_id; 00204 int rcd=NC_NOERR; /* [rcd] Return code */ 00205 int var_id; 00206 00207 int sym_tbl_nbr; /* [nbr] Size of symbol table */ 00208 00209 lmt_sct **lmt=NULL_CEWI; 00210 00211 nm_id_sct *dmn_lst=NULL; 00212 nm_id_sct *xtr_lst=NULL; /* Non-processed variables to copy to OUTPUT */ 00213 nm_id_sct *xtr_lst_a=NULL; /* Initialize to ALL variables in OUTPUT file */ 00214 00215 size_t sng_lng; 00216 size_t spt_arg_lng=size_t_CEWI; 00217 00218 time_t time_crr_time_t; 00219 00220 var_sct **var; 00221 var_sct **var_fix; 00222 var_sct **var_fix_out; 00223 var_sct **var_out; 00224 var_sct **var_prc; 00225 var_sct **var_prc_out; 00226 00227 aed_sct att_item; //Used to convert atts in vector to normal form 00228 size_t att_char_posn; 00229 char att_sng[3*NC_MAX_NAME]; 00230 00231 00232 prs_sct prs_arg; /* [sct] Global information required in parser routines */ 00233 00234 static struct option opt_lng[]= 00235 { /* Structure ordered by short option key if possible */ 00236 /* Long options with no argument, no short option counterpart */ 00237 /* Long options with argument, no short option counterpart */ 00238 {"fl_fmt",required_argument,0,0}, 00239 {"file_format",required_argument,0,0}, 00240 /* Long options with short counterparts */ 00241 {"4",no_argument,0,'4'}, 00242 {"64bit",no_argument,0,'4'}, 00243 {"netcdf4",no_argument,0,'4'}, 00244 {"append",no_argument,0,'A'}, 00245 {"coords",no_argument,0,'c'}, 00246 {"crd",no_argument,0,'c'}, 00247 {"no-coords",no_argument,0,'C'}, 00248 {"no-crd",no_argument,0,'C'}, 00249 {"debug",required_argument,0,'D'}, 00250 {"dbg_lvl",required_argument,0,'D'}, 00251 {"dimension",required_argument,0,'d'}, 00252 {"dmn",required_argument,0,'d'}, 00253 {"fnc_tbl",no_argument,0,'f'}, 00254 {"prn_fnc_tbl",no_argument,0,'f'}, 00255 {"ftn",no_argument,0,'F'}, 00256 {"history",no_argument,0,'h'}, 00257 {"hst",no_argument,0,'h'}, 00258 {"local",required_argument,0,'l'}, 00259 {"lcl",required_argument,0,'l'}, 00260 {"nintap",required_argument,0,'n'}, 00261 {"overwrite",no_argument,0,'O'}, 00262 {"ovr",no_argument,0,'O'}, 00263 {"output",required_argument,0,'o'}, 00264 {"fl_out",required_argument,0,'o'}, 00265 {"path",required_argument,0,'p'}, 00266 {"retain",no_argument,0,'R'}, 00267 {"rtn",no_argument,0,'R'}, 00268 {"revision",no_argument,0,'r'}, 00269 {"file",required_argument,0,'S'}, 00270 {"script-file",required_argument,0,'S'}, 00271 {"fl_spt",required_argument,0,'S'}, 00272 {"spt",required_argument,0,'s'}, 00273 {"script",required_argument,0,'s'}, 00274 {"units",no_argument,0,'u'}, 00275 {"variable",no_argument,0,'v'}, 00276 {"version",no_argument,0,'r'}, 00277 {"vrs",no_argument,0,'r'}, 00278 {"exclude",no_argument,0,'x'}, 00279 {"xcl",no_argument,0,'x'}, 00280 {"help",no_argument,0,'?'}, 00281 {0,0,0,0} 00282 }; /* end opt_lng */ 00283 int opt_idx=0; /* Index of current long option into opt_lng array */ 00284 00285 /* Start clock and save command line */ 00286 cmd_ln=nco_cmd_ln_sng(argc,argv); 00287 time_crr_time_t=time((time_t *)NULL); 00288 time_bfr_srt=ctime(&time_crr_time_t); time_bfr_srt=time_bfr_srt; /* Avoid compiler warning until variable is used for something */ 00289 00290 /* Get program name and set program enum (e.g., prg=ncra) */ 00291 prg_nm=prg_prs(argv[0],&prg); 00292 00293 /* Parse command line arguments */ 00294 while(1){ 00295 /* getopt_long_only() allows one dash to prefix long options */ 00296 opt=getopt_long(argc,argv,opt_sht_lst,opt_lng,&opt_idx); 00297 /* NB: access to opt_crr is only valid when long_opt is detected */ 00298 if(opt == EOF) break; /* Parse positional arguments once getopt_long() returns EOF */ 00299 opt_crr=(char *)strdup(opt_lng[opt_idx].name); 00300 00301 /* Process long options without short option counterparts */ 00302 if(opt == 0){ 00303 if(!strcmp(opt_crr,"fl_fmt") || !strcmp(opt_crr,"file_format")) rcd=nco_create_mode_prs(optarg,&fl_out_fmt); 00304 } /* opt != 0 */ 00305 /* Process short options */ 00306 switch(opt){ 00307 case 0: /* Long options have already been processed, return */ 00308 break; 00309 case '4': /* [flg] Catch-all to prescribe output storage format */ 00310 if(!strcmp(opt_crr,"64bit")) fl_out_fmt=NC_FORMAT_64BIT; else fl_out_fmt=NC_FORMAT_NETCDF4; 00311 break; 00312 case 'A': /* Toggle FORCE_APPEND */ 00313 FORCE_APPEND=!FORCE_APPEND; 00314 break; 00315 case 'C': /* Extract all coordinates associated with extracted variables? */ 00316 EXTRACT_ASSOCIATED_COORDINATES=False; 00317 break; 00318 case 'c': 00319 EXTRACT_ALL_COORDINATES=True; 00320 break; 00321 case 'D': /* Debugging level. Default is 0. */ 00322 dbg_lvl=(unsigned short)strtol(optarg,(char **)NULL,10); 00323 break; 00324 case 'd': /* Copy argument for later processing */ 00325 lmt_arg[lmt_nbr]=(char *)strdup(optarg); 00326 lmt_nbr++; 00327 break; 00328 case 'F': /* Toggle index convention. Default is 0-based arrays (C-style). */ 00329 FORTRAN_IDX_CNV=!FORTRAN_IDX_CNV; 00330 break; 00331 case 'f': /* Print function table */ 00332 PRN_FNC_TBL=True; 00333 break; 00334 case 'h': /* Toggle appending to history global attribute */ 00335 HISTORY_APPEND=!HISTORY_APPEND; 00336 break; 00337 case 'l': /* Local path prefix for files retrieved from remote file system */ 00338 fl_pth_lcl=(char *)strdup(optarg); 00339 break; 00340 case 'n': /* NINTAP-style abbreviation of files to process */ 00341 /* Currently not used in ncap but should be to allow processing multiple input files by same script */ 00342 (void)fprintf(stderr,"%s: ERROR %s does not currently implement -n option\n",prg_nm_get(),prg_nm_get()); 00343 fl_lst_abb=lst_prs_2D(optarg,",",&abb_arg_nbr); 00344 if(abb_arg_nbr < 1 || abb_arg_nbr > 3){ 00345 (void)fprintf(stderr,"%s: ERROR Incorrect abbreviation for file list\n",prg_nm); 00346 (void)nco_usg_prn(); 00347 nco_exit(EXIT_FAILURE); 00348 } /* end if */ 00349 break; 00350 case 'O': /* Toggle FORCE_OVERWRITE */ 00351 FORCE_OVERWRITE=!FORCE_OVERWRITE; 00352 break; 00353 case 'o': /* Name of output file */ 00354 fl_out=(char *)strdup(optarg); 00355 break; 00356 case 'p': /* Common file path */ 00357 fl_pth=(char *)strdup(optarg); 00358 break; 00359 case 'R': /* Toggle removal of remotely-retrieved-files. Default is True. */ 00360 REMOVE_REMOTE_FILES_AFTER_PROCESSING=!REMOVE_REMOTE_FILES_AFTER_PROCESSING; 00361 break; 00362 case 'r': /* Print CVS program information and copyright notice */ 00363 (void)copyright_prn(CVS_Id,CVS_Revision); 00364 (void)nco_lbr_vrs_prn(); 00365 nco_exit(EXIT_SUCCESS); 00366 break; 00367 case 's': /* Copy command script for later processing */ 00368 spt_arg[nbr_spt++]=(char *)strdup(optarg); 00369 if(nbr_spt == NCAP_SPT_NBR_MAX-1) (void)fprintf(stderr,"%s: WARNING No more than %d script arguments allowed. TODO #24\n",prg_nm_get(),NCAP_SPT_NBR_MAX); 00370 break; 00371 case 'S': /* Read command script from file rather than from command line */ 00372 fl_spt_usr=(char *)strdup(optarg); 00373 break; 00374 case 'v': /* Variables to extract/exclude */ 00375 PROCESS_ALL_VARS=False; 00376 nbr_xtr=0; 00377 break; 00378 case 'x': /* Exclude rather than extract variables specified with -v */ 00379 EXCLUDE_INPUT_LIST=True; 00380 if(EXCLUDE_INPUT_LIST) (void)fprintf(stderr,"%s: ERROR %s does not currently implement -x option\n",prg_nm_get(),prg_nm_get()); 00381 nco_exit(EXIT_FAILURE); 00382 break; 00383 case '?': /* Print proper usage */ 00384 (void)nco_usg_prn(); 00385 nco_exit(EXIT_SUCCESS); 00386 break; 00387 case '-': /* Long options are not allowed */ 00388 (void)fprintf(stderr,"%s: ERROR Long options are not available in this build. Use single letter options instead.\n",prg_nm_get()); 00389 nco_exit(EXIT_FAILURE); 00390 break; 00391 default: /* Print proper usage */ 00392 (void)nco_usg_prn(); 00393 nco_exit(EXIT_FAILURE); 00394 break; 00395 } /* end switch */ 00396 if(opt_crr != NULL) opt_crr=(char *)nco_free(opt_crr); 00397 } /* end while loop */ 00398 00399 /* Append ";\n" to command-script arguments, then concatenate them */ 00400 for(idx=0;idx<nbr_spt;idx++){ 00401 sng_lng=strlen(spt_arg[idx]); 00402 if(idx == 0){ 00403 spt_arg_cat=(char *)nco_malloc(sng_lng+3); 00404 strcpy(spt_arg_cat,spt_arg[idx]); 00405 strcat(spt_arg_cat,";\n"); 00406 spt_arg_lng=sng_lng+3; 00407 }else{ 00408 spt_arg_lng+=sng_lng+2; 00409 spt_arg_cat=(char *)nco_realloc(spt_arg_cat,spt_arg_lng); 00410 strcat(spt_arg_cat,spt_arg[idx]); 00411 strcat(spt_arg_cat,";\n"); 00412 } /* end else */ 00413 } /* end if */ 00414 00415 00416 /* Create function table */ 00417 sym_tbl_nbr= /* fxm: Make this dynamic */ 00418 +12 /* Basic math: acos, asin, atan, cos, exp, fabs, log, log10, rnd_nbr, sin, sqrt, tan */ 00419 +1 /* Basic math synonyms: ln */ 00420 +6 /* Hyperbolic trigonometric: acosh, asinh, atanh, cosh, sinh, tanh */ 00421 +2 /* Basic Rounding: ceil, floor */ 00422 +4 /* Advanced Rounding: nearbyint, rint, round, trunc */ 00423 +3 /* Advanced math: erf, erfc, gamma */ 00424 ; 00425 /* Basic math: acos, asin, atan, cos, exp, log, log10, rnd_nbr, sin, sqrt, tan */ 00426 00427 sym_vtr.push(ncap_sym_init("acos",acos,acosf)); 00428 sym_vtr.push(ncap_sym_init("asin",asin,asinf)); 00429 sym_vtr.push(ncap_sym_init("atan",atan,atanf)); 00430 sym_vtr.push(ncap_sym_init("cos",cos,cosf)); 00431 sym_vtr.push(ncap_sym_init("exp",exp,expf)); 00432 sym_vtr.push(ncap_sym_init("fabs",fabs,fabsf)); 00433 sym_vtr.push(ncap_sym_init("log",log,logf)); 00434 sym_vtr.push(ncap_sym_init("log10",log10,log10f)); 00435 //sym_vtr.push(ncap_sym_init("rnd_nbr",rnd_nbr,rnd_nbrf)); 00436 sym_vtr.push(ncap_sym_init("sin",sin,sinf)); 00437 sym_vtr.push(ncap_sym_init("sqrt",sqrt,sqrtf)); 00438 sym_vtr.push(ncap_sym_init("tan",tan,tanf)); 00439 00440 /* Basic math synonyms: ln */ 00441 sym_vtr.push(ncap_sym_init("ln",log,logf)); /* ln() is synonym for log() */ 00442 00443 /* Basic Rounding: ceil, fl<oor */ 00444 sym_vtr.push(ncap_sym_init("ceil",ceil,ceilf)); /* Round up to nearest integer */ 00445 sym_vtr.push(ncap_sym_init("floor",floor,floorf)); /* Round down to nearest integer */ 00446 00447 /* fxm: Change whole function symbol table section to autotools format #if HAVE_ERF ... */ 00448 00449 /* Advanced math: erf, erfc, gamma 00450 LINUX*, MACOSX*, and SUN* provide these functions with C89 00451 20020122 and 20020422: AIX, CRAY, SGI*, WIN32 do not define erff(), erfcf(), gammaf() with C89 00452 20050610: C99 mandates support for erf(), erfc(), tgamma() 00453 Eventually users without C99 will forego ncap */ 00454 #if defined(LINUX) || defined(LINUXAMD64) || defined(MACOSX) 00455 sym_vtr.push(ncap_sym_init("erf",erf,erff)); 00456 sym_vtr.push(ncap_sym_init("erfc",erfc,erfcf)); 00457 sym_vtr.push(ncap_sym_init("gamma",tgamma,tgammaf)); 00458 #endif /* !LINUX */ 00459 00460 /* Hyperbolic trigonometric: acosh, asinh, atanh, cosh, sinh, tanh 00461 20020703: AIX, SGI*, WIN32 do not define acoshf, asinhf, atanhf 00462 20050610: C99 mandates support for acosh(), asinh(), atanh(), cosh(), sinh(), tanh() 00463 Eventually users without C99 will forego ncap */ 00464 #if defined(LINUX) || defined(LINUXAMD64) 00465 sym_vtr.push(ncap_sym_init("acosh",acosh,acoshf)); 00466 sym_vtr.push(ncap_sym_init("asinh",asinh,asinhf)); 00467 sym_vtr.push(ncap_sym_init("atanh",atanh,atanhf)); 00468 sym_vtr.push(ncap_sym_init("cosh",cosh,coshf)); 00469 sym_vtr.push(ncap_sym_init("sinh",sinh,sinhf)); 00470 sym_vtr.push(ncap_sym_init("tanh",tanh,tanhf)); 00471 #endif /* !LINUX */ 00472 00473 /* 20020703: AIX, MACOSX, SGI*, WIN32 do not define rintf 00474 Only LINUX* supplies all of these and I do not care about them enough 00475 to activate them on LINUX* but not on MACOSX* and SUN* */ 00476 /* Advanced Rounding: nearbyint, rint, round, trunc */ 00477 /* Advanced Rounding: nearbyint, round, trunc */ 00478 /* sym_vtr.push(ncap_sym_init("nearbyint",nearbyint,nearbyintf)); *//* Round to integer value in floating point format using current rounding direction, do not raise inexact exceptions */ 00479 /* sym_vtr.push(ncap_sym_init("round",round,roundf)); *//* Round to nearest integer away from zero */ 00480 /* sym_vtr.push(ncap_sym_init("trunc",trunc,truncf)); *//* Round to nearest integer not larger in absolute value */ 00481 /* sym_vtr.push(ncap_sym_init("rint",rint,rintf)); *//* Round to integer value in floating point format using current rounding direction, raise inexact exceptions */ 00482 00483 if(PRN_FNC_TBL){ 00484 /* ncap TODO #43: alphabetize this list */ 00485 (void)fprintf(stdout,"Maths functions available in %s:\n",prg_nm_get()); 00486 (void)fprintf(stdout,"Name\tFloat\tDouble\n"); 00487 for(idx=0;idx<sym_vtr.size();idx++) 00488 (void)fprintf(stdout,"%s\t%c\t%c\n",sym_vtr[idx]->nm, (sym_vtr[idx]->fnc_flt ? 'y' : 'n'),(sym_vtr[idx]->fnc_dbl ? 'y' : 'n')); 00489 nco_exit(EXIT_SUCCESS); 00490 } /* end if PRN_FNC_TBL */ 00491 00492 /* Process positional arguments and fill in filenames */ 00493 fl_lst_in=nco_fl_lst_mk(argv,argc,optind,&fl_nbr,&fl_out,&FL_LST_IN_FROM_STDIN); 00494 00495 /* Make uniform list of user-specified dimension limits */ 00496 if(lmt_nbr > 0) lmt=nco_lmt_prs(lmt_nbr,lmt_arg); 00497 00498 /* Parse filename */ 00499 fl_in=nco_fl_nm_prs(fl_in,0,&fl_nbr,fl_lst_in,abb_arg_nbr,fl_lst_abb,fl_pth); 00500 /* Make sure file is on local system and is readable or die trying */ 00501 fl_in=nco_fl_mk_lcl(fl_in,fl_pth_lcl,&FILE_RETRIEVED_FROM_REMOTE_LOCATION); 00502 /* Open file for reading */ 00503 rcd=nco_open(fl_in,NC_NOWRITE,&in_id); 00504 00505 /* Form list of all dimensions in file */ 00506 dmn_lst=nco_dmn_lst(in_id,&nbr_dmn_in); 00507 00508 //dmn_in=(dmn_sct **)nco_malloc(nbr_dmn_in*sizeof(dmn_sct *)); 00509 for(idx=0;idx<nbr_dmn_in;idx++) 00510 dmn_in_vtr.push(nco_dmn_fll(in_id,dmn_lst[idx].id,dmn_lst[idx].nm)); 00511 dmn_in=dmn_in_vtr.ptr(0); 00512 00513 00514 /* Merge hyperslab limit information into dimension structures */ 00515 if(lmt_nbr > 0) (void)nco_dmn_lmt_mrg(dmn_in,nbr_dmn_in,lmt,lmt_nbr); 00516 00517 /* Open output file */ 00518 fl_out_tmp=nco_fl_out_open(fl_out,FORCE_APPEND,FORCE_OVERWRITE,fl_out_fmt,&out_id); 00519 00520 /* Copy global attributes */ 00521 (void)nco_att_cpy(in_id,out_id,NC_GLOBAL,NC_GLOBAL,True); 00522 00523 /* Catenate time-stamped command line to "history" global attribute */ 00524 if(HISTORY_APPEND) (void)nco_hst_att_cat(out_id,cmd_ln); 00525 00526 (void)nco_enddef(out_id); 00527 00528 00529 /* Set arguments for script execution */ 00530 prs_arg.fl_in=fl_in; /* [sng] Input data file */ 00531 prs_arg.in_id=in_id; /* [id] Input data file ID */ 00532 prs_arg.fl_out=fl_out; /* [sng] Output data file */ 00533 prs_arg.out_id=out_id; /* [id] Output data file ID */ 00534 00535 prs_arg.ptr_dmn_in_vtr=&dmn_in_vtr; 00536 prs_arg.ptr_dmn_out_vtr=&dmn_out_vtr; 00537 prs_arg.ptr_sym_vtr=&sym_vtr; 00538 prs_arg.ptr_var_vtr=&var_vtr; 00539 00540 prs_arg.ntl_scn=False; //[flg] Initial scan of script */ 00541 00542 00543 if(fl_spt_usr == NULL){ 00544 /* No script file specified, look for command-line scripts */ 00545 if(nbr_spt == 0){ 00546 (void)fprintf(stderr,"%s: ERROR no script file or command line scripts specified\n",prg_nm_get()); 00547 (void)fprintf(stderr,"%s: HINT Use, e.g., -s \"foo=bar\"\n",prg_nm_get()); 00548 nco_exit(EXIT_FAILURE); 00549 } /* end if */ 00550 00551 /* Print all command-line scripts */ 00552 if(dbg_lvl_get() > 0){ 00553 for(idx=0;idx<nbr_spt;idx++) (void)fprintf(stderr,"spt_arg[%d] = %s\n",idx,spt_arg[idx]); 00554 } /* endif debug */ 00555 00556 /* Parse command line scripts */ 00557 fl_spt_usr=(char *)strdup("Command-line script"); 00558 }else{ /* ...endif command-line scripts, begin script file... */ 00559 /* Open script file for reading */ 00560 if((yyin=fopen(fl_spt_usr,"r")) == NULL){ 00561 (void)fprintf(stderr,"%s: ERROR Unable to open script file %s\n",prg_nm_get(),fl_spt_usr); 00562 nco_exit(EXIT_FAILURE); 00563 } /* end if */ 00564 fclose(yyin); 00565 } /* end else script file */ 00566 00567 /* Invoke ANTLR parser */ 00568 00569 rcd=parse_antlr(&prs_arg,fl_spt_usr,spt_arg_cat); 00570 00571 00572 /* Tidy up */ 00573 fl_spt_usr=(char*)nco_free(fl_spt_usr); 00574 00575 00576 /* Get number of variables in output file */ 00577 rcd=nco_inq(out_id,(int *)NULL,&nbr_var_fl,(int *)NULL,(int*)NULL); 00578 00579 /* Make list of all new variables in output_file */ 00580 xtr_lst_a=nco_var_lst_mk(out_id,nbr_var_fl,var_lst_in,False,&nbr_lst_a); 00581 00582 if(PROCESS_ALL_VARS){ 00583 /* Get number of variables in input file */ 00584 rcd=nco_inq(in_id,(int *)NULL,&nbr_var_fl,(int *)NULL,(int *)NULL); 00585 00586 /* Form initial list of all variables in input file */ 00587 xtr_lst=nco_var_lst_mk(in_id,nbr_var_fl,var_lst_in,False,&nbr_xtr); 00588 }else{ 00589 /* Make list of variables of new attributes whose parent variable is only in input file */ 00590 xtr_lst=nco_att_lst_mk(in_id,out_id,var_vtr,&nbr_xtr); 00591 } /* endif */ 00592 00593 /* Find dimensions associated with xtr_lst */ 00594 /* Write to O only new dims 00595 Add apropriate coordinate variables to extraction list 00596 options -c -process all cordinates 00597 i.e., add coordinates to var list 00598 Also add their dims 00599 00600 options --none -process associated co-ords 00601 loop though dim_out and append to var list 00602 00603 options -C no co-ordinates Do nothing */ 00604 00605 /* Subtract list A again */ 00606 /* Finally extract variables on list */ 00607 00608 /* Subtract list A */ 00609 if(nbr_lst_a > 0) xtr_lst=nco_var_lst_sub(xtr_lst,&nbr_xtr,xtr_lst_a,nbr_lst_a); 00610 00611 /* Put file in define mode to allow metadata writing */ 00612 (void)nco_redef(out_id); 00613 00614 /* Free current list of all dimensions in input file */ 00615 dmn_lst=nco_nm_id_lst_free(dmn_lst,nbr_dmn_in); 00616 00617 /* Make list of dimensions of variables in xtr_lst */ 00618 if(nbr_xtr > 0) dmn_lst=nco_dmn_lst_ass_var(in_id,xtr_lst,nbr_xtr,&nbr_dmn_ass); 00619 00620 /* Find and add any new dimensions to output */ 00621 for(idx=0;idx<nbr_dmn_ass;idx++){ 00622 dmn_item=dmn_out_vtr.find(dmn_lst[idx].nm); 00623 if(dmn_item != NULL) continue; 00624 dmn_item=dmn_in_vtr.find(dmn_lst[idx].nm); 00625 if(dmn_item == NULL) continue; 00626 dmn_new=nco_dmn_dpl(dmn_item); 00627 (void)nco_dmn_dfn(fl_out,out_id,&dmn_new,1); 00628 (void)nco_dmn_xrf(dmn_new,dmn_item); 00629 (void)dmn_out_vtr.push(dmn_new); 00630 } /* end loop over idx */ 00631 00632 /* Free current list of all dimensions in input file */ 00633 dmn_lst=nco_nm_id_lst_free(dmn_lst,nbr_dmn_ass); 00634 00635 /* Dimensions for manually specified extracted variables are now defined in output file 00636 Add coordinate variables to extraction list 00637 If EXTRACT_ALL_COORDINATES then write associated dimension to output */ 00638 00639 00640 if(EXTRACT_ASSOCIATED_COORDINATES){ 00641 for(idx=0;idx<dmn_in_vtr.size();idx++){ 00642 if(!dmn_in_vtr[idx]->is_crd_dmn) continue; 00643 00644 if(EXTRACT_ALL_COORDINATES && !dmn_in_vtr[idx]->xrf){ 00645 /* Add dimensions to output list dmn_out */ 00646 dmn_item=dmn_in_vtr[idx]; 00647 dmn_new=nco_dmn_dpl(dmn_item); 00648 (void)nco_dmn_xrf(dmn_new,dmn_item); 00649 /* Write dimension to output */ 00650 (void)nco_dmn_dfn(fl_out,out_id,&dmn_new,1); 00651 (void)dmn_out_vtr.push(dmn_new); 00652 } /* end if */ 00653 /* Add coordinate variable to extraction list, dimension has already been output */ 00654 if(dmn_in_vtr[idx]->xrf){ 00655 for(jdx=0;jdx<nbr_xtr;jdx++) 00656 if(!strcmp(xtr_lst[jdx].nm,dmn_in_vtr[idx]->nm)) break; 00657 00658 if(jdx != nbr_xtr) continue; 00659 /* If coordinate is not on list then add it to extraction list */ 00660 xtr_lst=(nm_id_sct *)nco_realloc(xtr_lst,(nbr_xtr+1)*sizeof(nm_id_sct)); 00661 xtr_lst[nbr_xtr].nm=(char *)strdup(dmn_in_vtr[idx]->nm); 00662 xtr_lst[nbr_xtr++].id=dmn_in_vtr[idx]->cid; 00663 } /* endif */ 00664 } /* end loop over idx */ 00665 } /* end if */ 00666 00667 00668 /* Subtract list A again (it may contain re-defined coordinates) */ 00669 if(nbr_xtr > 0) xtr_lst=nco_var_lst_sub(xtr_lst,&nbr_xtr,xtr_lst_a,nbr_lst_a); 00670 00671 /* Sort extraction list for faster I/O */ 00672 if(nbr_xtr > 1) xtr_lst=nco_lst_srt_nm_id(xtr_lst,nbr_xtr,False); 00673 00674 /* Is this an CCM/CCSM/CF-format history tape? */ 00675 CNV_CCM_CCSM_CF=nco_cnv_ccm_ccsm_cf_inq(in_id); 00676 00677 /* Write "fixed" variables */ 00678 var=(var_sct **)nco_malloc(nbr_xtr*sizeof(var_sct *)); 00679 var_out=(var_sct **)nco_malloc(nbr_xtr*sizeof(var_sct *)); 00680 for(idx=0;idx<nbr_xtr;idx++){ 00681 var[idx]=nco_var_fll(in_id,xtr_lst[idx].id,xtr_lst[idx].nm,dmn_in_vtr.ptr(0),dmn_in_vtr.size()); 00682 var_out[idx]=nco_var_dpl(var[idx]); 00683 (void)nco_xrf_var(var[idx],var_out[idx]); 00684 (void)nco_xrf_dmn(var_out[idx]); 00685 } /* end loop over idx */ 00686 00687 00688 /* NB: ncap is not well-suited for nco_var_lst_dvd() */ 00689 /* Divide variable lists into lists of fixed variables and variables to be processed */ 00690 (void)nco_var_lst_dvd(var,var_out,nbr_xtr,CNV_CCM_CCSM_CF,nco_pck_plc_nil,nco_pck_map_nil,(dmn_sct **)NULL,(int)0,&var_fix,&var_fix_out,&nbr_var_fix,&var_prc,&var_prc_out,&nbr_var_prc); 00691 00692 /* csz: Why not call this with var_fix? */ 00693 /* Define non-processed vars */ 00694 (void)nco_var_dfn(in_id,fl_out,out_id,var_out,nbr_xtr,(dmn_sct **)NULL,(int)0,nco_pck_plc_nil,nco_pck_map_nil); 00695 00696 00697 /* Write out new attributes possibly overwriting old ones */ 00698 for(idx=0;idx<var_vtr.size();idx++){ 00699 00700 // Check if attrribute 00701 if( var_vtr[idx]->type != ncap_att) continue; 00702 att_char_posn=var_vtr[idx]->s_va_nm.find("@"); 00703 if(att_char_posn == std::string::npos) continue; 00704 (void)strcpy(att_sng,var_vtr[idx]->s_va_nm.c_str()); 00705 att_sng[att_char_posn]='\0'; 00706 00707 att_item.att_nm=(att_sng+att_char_posn+1); 00708 att_item.var_nm=att_sng; 00709 att_item.sz=var_vtr[idx]->var->sz; 00710 att_item.type=var_vtr[idx]->var->type; 00711 att_item.val=var_vtr[idx]->var->val; 00712 att_item.mode=aed_overwrite; 00713 00714 if(!strcmp(att_item.var_nm,"global")) 00715 var_id=NC_GLOBAL; 00716 else { 00717 rcd=nco_inq_varid_flg(out_id,att_item.var_nm,&var_id); 00718 if(rcd != NC_NOERR) continue; 00719 } 00720 // Check size; 00721 if(att_item.sz > NC_MAX_ATTRS ){ 00722 (void)fprintf(stdout,"%s: Attribute %s size %ld excceeds maximium %d\n",prg_nm_get(),att_item.att_nm,att_item.sz, NC_MAX_ATTRS ); 00723 continue; 00724 } 00725 /* NB: These attributes should probably be written prior to last data mode */ 00726 (void)nco_aed_prc(out_id,var_id,att_item); 00727 // delete NcapVar 00728 delete var_vtr[idx]; 00729 }/* end for */ 00730 00731 /* Turn off default filling behavior to enhance efficiency */ 00732 rcd=nco_set_fill(out_id,NC_NOFILL,&fll_md_old); 00733 00734 /* Take output file out of define mode */ 00735 (void)nco_enddef(out_id); 00736 00737 /* Copy non-processed vars */ 00738 (void)nco_var_val_cpy(in_id,out_id,var_fix,nbr_var_fix); 00739 00740 /* Close input netCDF file */ 00741 rcd=nco_close(in_id); 00742 00743 /* Remove local copy of file */ 00744 if(FILE_RETRIEVED_FROM_REMOTE_LOCATION && REMOVE_REMOTE_FILES_AFTER_PROCESSING) (void)nco_fl_rm(fl_in); 00745 00746 /* Close output file and move it from temporary to permanent location */ 00747 (void)nco_fl_out_cls(fl_out,fl_out_tmp,out_id); 00748 00749 /* ncap-unique memory */ 00750 /* fxm: ncap-specific memory freeing instructions go here */ 00751 /* 00752 for(idx=0;idx<sym_tbl_nbr;idx++){ 00753 sym_tbl[idx]->nm=(char*)nco_free(sym_tbl[idx]->nm); 00754 sym_tbl[idx]=(sym_sct*)nco_free(sym_tbl[idx]); 00755 } 00756 sym_tbl=(sym_sct **)nco_free(sym_tbl); */ 00757 if(fl_spt_usr != NULL) fl_spt_usr=(char *)nco_free(fl_spt_usr); 00758 00759 /* Free extraction lists */ 00760 xtr_lst=nco_nm_id_lst_free(xtr_lst,nbr_xtr); 00761 xtr_lst_a=nco_nm_id_lst_free(xtr_lst_a,nbr_lst_a); 00762 00763 /* Free command line algebraic arguments, if any */ 00764 for(idx=0;idx<nbr_spt;idx++) spt_arg[idx]=(char *)nco_free(spt_arg[idx]); 00765 if(spt_arg_cat != NULL) spt_arg_cat=(char *)nco_free(spt_arg_cat); 00766 00767 /* NCO-generic clean-up */ 00768 /* Free individual strings/arrays */ 00769 if(cmd_ln != NULL) cmd_ln=(char *)nco_free(cmd_ln); 00770 if(fl_in != NULL) fl_in=(char*)nco_free(fl_in); 00771 if(fl_out != NULL) fl_out=(char *)nco_free(fl_out); 00772 if(fl_out_tmp != NULL) fl_out_tmp=(char *)nco_free(fl_out_tmp); 00773 if(fl_pth != NULL) fl_pth=(char *)nco_free(fl_pth); 00774 if(fl_pth_lcl != NULL) fl_pth_lcl=(char *)nco_free(fl_pth_lcl); 00775 /* Free lists of strings */ 00776 if(fl_lst_in != NULL && fl_lst_abb == NULL) fl_lst_in=nco_sng_lst_free(fl_lst_in,fl_nbr); 00777 if(fl_lst_in != NULL && fl_lst_abb != NULL) fl_lst_in=nco_sng_lst_free(fl_lst_in,1); 00778 if(fl_lst_abb != NULL) fl_lst_abb=nco_sng_lst_free(fl_lst_abb,abb_arg_nbr); 00779 /* Free limits */ 00780 for(idx=0;idx<lmt_nbr;idx++) lmt_arg[idx]=(char *)nco_free(lmt_arg[idx]); 00781 if(lmt_nbr > 0) lmt=nco_lmt_lst_free(lmt,lmt_nbr); 00782 /* Free dimension lists */ 00783 if(nbr_dmn_in > 0) dmn_in=nco_dmn_lst_free(dmn_in,nbr_dmn_in); 00784 if(nbr_dmn_out > 0) dmn_out=nco_dmn_lst_free(dmn_out,nbr_dmn_out); 00785 /* Free variable lists */ 00786 if(nbr_xtr > 0) var=nco_var_lst_free(var,nbr_xtr); 00787 if(nbr_xtr > 0) var_out=nco_var_lst_free(var_out,nbr_xtr); 00788 var_prc=(var_sct **)nco_free(var_prc); 00789 var_prc_out=(var_sct **)nco_free(var_prc_out); 00790 var_fix=(var_sct **)nco_free(var_fix); 00791 var_fix_out=(var_sct **)nco_free(var_fix_out); 00792 00793 nco_exit_gracefully(); 00794 return EXIT_SUCCESS; 00795 } /* end main() */
|
|
|
Definition at line 78 of file ncap2.cc. Referenced by glb_init_free(), and main(). |
|
|
Definition at line 77 of file ncap2.cc. Referenced by glb_init_free(), and main(). |
|
|
Definition at line 76 of file ncap2.cc. Referenced by glb_init_free(), and main(). |
1.4.4