nco/ncks.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "nco_getopt.h"
#include <netcdf.h>
#include "libnco.h"

Include dependency graph for ncks.c:

Go to the source code of this file.

Defines

#define MAIN_PROGRAM_FILE

Functions

int main (int argc, char **argv)


Define Documentation

#define MAIN_PROGRAM_FILE
 

Definition at line 68 of file ncks.c.


Function Documentation

int main int  argc,
char **  argv
 

Definition at line 72 of file ncks.c.

References lmt_all_sct::BASIC_DMN, lmt_sct::cnt, copyright_prn(), lmt_all_sct::dmn_nm, lmt_all_sct::dmn_sz_org, lmt_sct::end, EXIT_FAILURE, EXIT_SUCCESS, False, FILE, getopt_long, lmt_sct::id, lmt_sct::is_rec_dmn, lmt_all_sct::lmt_dmn, lmt_all_sct::lmt_dmn_nbr, lmt_sct::lmt_typ, lst_prs_2D(), lmt_sct::max_sng, lmt_sct::min_sng, option::name, NC_FORMAT_64BIT, NC_FORMAT_CLASSIC, NC_FORMAT_NETCDF4, NC_GLOBAL, NC_MAX_DIMS, NC_MAX_NAME, NC_NOERR, NC_NOFILL, NC_NOWRITE, nco__enddef(), nco_att_cpy(), nco_bnr_close(), nco_bnr_open(), nco_bool, nco_close(), nco_cmd_ln_sng(), nco_cmp_get(), nco_cpy_var_dfn(), nco_cpy_var_dfn_lmt(), nco_cpy_var_val(), nco_cpy_var_val_mlt_lmt(), nco_create_mode_prs(), nco_enddef(), nco_err_exit(), 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_dim(), nco_lbr_vrs_prn(), nco_lmt_all_lst_free(), nco_lmt_evl(), nco_lmt_lst_free(), nco_lmt_prs(), nco_lst_comma2hash(), nco_lst_srt_nm_id(), nco_malloc(), nco_mpi_get(), nco_msa_clc_cnt(), nco_msa_prn_var_val(), nco_msa_wrp_splt(), nco_nm_id_lst_free(), nco_open(), nco_prn_att(), nco_prn_var_dfn(), nco_realloc(), NCO_REC_DMN_UNDEFINED, nco_set_fill(), nco_sng_lst_free(), nco_usg_prn(), nco_var_lst_add_crd(), nco_var_lst_ass_crd_add(), nco_var_lst_mk(), nco_var_lst_xcl(), lmt_sct::nm, no_argument, NULL_CEWI, optarg, optind, prg_nm_get(), prg_prs(), required_argument, lmt_sct::srd, lmt_sct::srd_sng, lmt_sct::srt, True, and lmt_all_sct::WRP.

00073 {
00074   nco_bool ALPHABETIZE_OUTPUT=True; /* Option a */
00075   nco_bool EXCLUDE_INPUT_LIST=False; /* Option c */
00076   nco_bool FILE_RETRIEVED_FROM_REMOTE_LOCATION;
00077   nco_bool FL_LST_IN_FROM_STDIN=False; /* [flg] fl_lst_in comes from stdin */
00078   nco_bool FORCE_APPEND=False; /* Option A */
00079   nco_bool FORCE_OVERWRITE=False; /* Option O */
00080   nco_bool FORTRAN_IDX_CNV=False; /* Option F */
00081   nco_bool HISTORY_APPEND=True; /* Option h */
00082   nco_bool NCO_BNR_WRT=False; /* [flg] Write binary file */
00083   nco_bool PRN_DMN_IDX_CRD_VAL=True; /* [flg] Print leading dimension/coordinate indices/values Option Q */
00084   nco_bool PRN_DMN_UNITS=False; /* [flg] Print dimensional units Option u */
00085   nco_bool PRN_DMN_UNITS_TGL=False; /* [flg] Toggle print dimensional units Option u */
00086   nco_bool PRN_GLB_METADATA=False; /* [flg] Print global metadata */
00087   nco_bool PRN_GLB_METADATA_TGL=False; /* [flg] Toggle print global metadata Option M */
00088   nco_bool PRN_QUIET=False; /* [flg] Turn off all printing to screen */
00089   nco_bool PRN_VAR_DATA=False; /* [flg] Print variable data */
00090   nco_bool PRN_VAR_DATA_TGL=False; /* [flg] Toggle print variable data Option H */
00091   nco_bool PRN_VAR_METADATA=False; /* [flg] Print variable metadata */
00092   nco_bool PRN_VAR_METADATA_TGL=False; /* [flg] Toggle print variable metadata Option m */
00093   nco_bool PRN_VRB=False; /* [flg] Print data and metadata by default */
00094   nco_bool PROCESS_ALL_COORDINATES=False; /* Option c */
00095   nco_bool PROCESS_ASSOCIATED_COORDINATES=True; /* Option C */
00096   nco_bool REMOVE_REMOTE_FILES_AFTER_PROCESSING=True; /* Option R */
00097 
00098   char **fl_lst_abb=NULL; /* Option a */
00099   char **fl_lst_in;
00100   char **var_lst_in=NULL;
00101   char *cmd_ln;
00102   char *dlm_sng=NULL;
00103   char *fl_bnr=NULL; /* [sng] Unformatted binary output file */
00104   char *fl_in=NULL;
00105   char *fl_out=NULL; /* Option o */
00106   char *fl_out_tmp=NULL_CEWI;
00107   char *fl_pth=NULL; /* Option p */
00108   char *fl_pth_lcl=NULL; /* Option l */
00109   char *lmt_arg[NC_MAX_DIMS];
00110   char *opt_crr=NULL; /* [sng] String representation of current long-option name */
00111   char *optarg_lcl=NULL; /* [sng] Local copy of system optarg */
00112   char *time_bfr_srt;
00113   char dmn_nm[NC_MAX_NAME];
00114 
00115   const char * const CVS_Id="$Id: ncks.c,v 1.155 2006/02/26 07:41:55 zender Exp $"; 
00116   const char * const CVS_Revision="$Revision: 1.155 $";
00117   const char * const opt_sht_lst="4aABb:CcD:d:FHhl:MmOo:Pp:qQrRs:uv:x-:";
00118 
00119   extern char *optarg;
00120   extern int optind;
00121   
00122   FILE *fp_bnr=NULL_CEWI; /* [fl] Unformatted binary output file handle */
00123 
00124   int abb_arg_nbr=0;
00125   int fl_nbr=0;
00126   int fl_out_fmt=NC_FORMAT_CLASSIC; /* [enm] Output file format */
00127   int fll_md_old; /* [enm] Old fill mode */
00128   int glb_att_nbr;
00129   int idx;
00130   int in_id;  
00131   int jdx;
00132   int lmt_nbr=0; /* Option d. NB: lmt_nbr gets incremented */
00133   int nbr_dmn_fl;
00134   int nbr_var_fl;
00135   int nbr_xtr=0; /* nbr_xtr will not otherwise be set for -c with no -v */
00136   int opt;
00137   int rcd=NC_NOERR; /* [rcd] Return code */
00138   int rec_dmn_id=NCO_REC_DMN_UNDEFINED;
00139   int var_lst_in_nbr=0;
00140     
00141   lmt_sct **lmt;
00142   lmt_sct **lmt_rgl; /* Regular limits */
00143 
00144   lmt_all_sct **lmt_all_lst; /* List of *lmt_all structures */
00145   lmt_all_sct *lmt_all_crr; /* Current lmt_all structure */
00146 
00147   long dmn_sz;
00148 
00149   nm_id_sct *xtr_lst=NULL; /* xtr_lst may be alloc()'d from NULL with -c option */
00150 
00151   size_t hdr_pad=0UL; /* [B] Pad at end of header section */
00152 
00153   time_t time_crr_time_t;
00154 
00155   static struct option opt_lng[]=
00156     { /* Structure ordered by short option key if possible */
00157       /* Long options with no argument, no short option counterpart */
00158       {"cmp",no_argument,0,0},
00159       {"compiler",no_argument,0,0},
00160       {"mpi_implementation",no_argument,0,0},
00161       /* Long options with argument, no short option counterpart */
00162       {"fl_fmt",required_argument,0,0},
00163       {"file_format",required_argument,0,0},
00164       {"hdr_pad",required_argument,0,0},
00165       {"header_pad",required_argument,0,0},
00166       /* Long options with short counterparts */
00167       {"4",no_argument,0,'4'},
00168       {"64bit",no_argument,0,'4'},
00169       {"netcdf4",no_argument,0,'4'},
00170       {"abc",no_argument,0,'a'},
00171       {"alphabetize",no_argument,0,'a'},
00172       {"append",no_argument,0,'A'},
00173       {"apn",no_argument,0,'A'},
00174       {"bnr",no_argument,0,'B'},
00175       {"binary",no_argument,0,'B'},
00176       {"binary-file",required_argument,0,'b'},
00177       {"fl_bnr",required_argument,0,'b'},
00178       {"coords",no_argument,0,'c'},
00179       {"crd",no_argument,0,'c'},
00180       {"no-coords",no_argument,0,'C'},
00181       {"no-crd",no_argument,0,'C'},
00182       {"debug",required_argument,0,'D'},
00183       {"dbg_lvl",required_argument,0,'D'},
00184       {"dimension",required_argument,0,'d'},
00185       {"dmn",required_argument,0,'d'},
00186       {"fortran",no_argument,0,'F'},
00187       {"ftn",no_argument,0,'F'},
00188       {"history",no_argument,0,'h'},
00189       {"hst",no_argument,0,'h'},
00190       {"hieronymus",no_argument,0,'H'}, /* fxm: need better mnemonic for -H */
00191       {"local",required_argument,0,'l'},
00192       {"lcl",required_argument,0,'l'},
00193       {"lcl",required_argument,0,'l'},
00194       {"metadata",no_argument,0,'m'},
00195       {"mtd",no_argument,0,'m'},
00196       {"Metadata",no_argument,0,'M'},
00197       {"Mtd",no_argument,0,'M'},
00198       {"overwrite",no_argument,0,'O'},
00199       {"ovr",no_argument,0,'O'},
00200       {"output",required_argument,0,'o'},
00201       {"fl_out",required_argument,0,'o'},
00202       {"print",required_argument,0,'P'},
00203       {"prn",required_argument,0,'P'},
00204       {"path",required_argument,0,'p'},
00205       {"quiet",no_argument,0,'q'},
00206       {"retain",no_argument,0,'R'},
00207       {"rtn",no_argument,0,'R'},
00208       {"revision",no_argument,0,'r'},
00209       {"version",no_argument,0,'r'},
00210       {"vrs",no_argument,0,'r'},
00211       {"sng_fmt",required_argument,0,'s'},
00212       {"string",required_argument,0,'s'},
00213       {"units",no_argument,0,'u'},
00214       {"variable",required_argument,0,'v'},
00215       {"exclude",no_argument,0,'x'},
00216       {"xcl",no_argument,0,'x'},
00217       {"help",no_argument,0,'?'},
00218       {0,0,0,0}
00219     }; /* end opt_lng */
00220   int opt_idx=0; /* Index of current long option into opt_lng array */
00221 
00222   /* Start clock and save command line */ 
00223   cmd_ln=nco_cmd_ln_sng(argc,argv);
00224   time_crr_time_t=time((time_t *)NULL);
00225   time_bfr_srt=ctime(&time_crr_time_t); time_bfr_srt=time_bfr_srt; /* Avoid compiler warning until variable is used for something */
00226   fl_bnr=(char *)strdup("ncks.bnr");
00227   
00228   /* Get program name and set program enum (e.g., prg=ncra) */
00229   prg_nm=prg_prs(argv[0],&prg);
00230 
00231   /* Parse command line arguments */
00232   while(1){
00233     /* getopt_long_only() allows one dash to prefix long options */
00234     opt=getopt_long(argc,argv,opt_sht_lst,opt_lng,&opt_idx);
00235     /* NB: access to opt_crr is only valid when long_opt is detected */
00236     if(opt == EOF) break; /* Parse positional arguments once getopt_long() returns EOF */
00237     opt_crr=(char *)strdup(opt_lng[opt_idx].name);
00238 
00239     /* Process long options without short option counterparts */
00240     if(opt == 0){
00241       if(!strcmp(opt_crr,"cmp") || !strcmp(opt_crr,"compiler")){
00242         (void)fprintf(stdout,"%s\n",nco_cmp_get());
00243         nco_exit(EXIT_SUCCESS);
00244       } /* endif "cmp" */
00245       if(!strcmp(opt_crr,"fl_fmt") || !strcmp(opt_crr,"file_format")) rcd=nco_create_mode_prs(optarg,&fl_out_fmt);
00246       if(!strcmp(opt_crr,"hdr_pad") || !strcmp(opt_crr,"header_pad")) hdr_pad=strtoul(optarg,(char **)NULL,10);
00247       if(!strcmp(opt_crr,"mpi_implementation")){
00248         (void)fprintf(stdout,"%s\n",nco_mpi_get());
00249         nco_exit(EXIT_SUCCESS);
00250       } /* endif "mpi" */
00251     } /* opt != 0 */
00252     /* Process short options */
00253     switch(opt){
00254     case 0: /* Long options have already been processed, return */
00255       break;
00256     case '4': /* [flg] Catch-all to prescribe output storage format */
00257       if(!strcmp(opt_crr,"64bit")) fl_out_fmt=NC_FORMAT_64BIT; else fl_out_fmt=NC_FORMAT_NETCDF4; 
00258       break;
00259     case 'a': /* Toggle ALPHABETIZE_OUTPUT */
00260       ALPHABETIZE_OUTPUT=!ALPHABETIZE_OUTPUT;
00261       break;
00262     case 'A': /* Toggle FORCE_APPEND */
00263       FORCE_APPEND=!FORCE_APPEND;
00264       break;
00265     case 'B': /* Set NCO_BNR_WRT */
00266       NCO_BNR_WRT=True;
00267       break;
00268     case 'b': /* Set file for binary output */
00269       NCO_BNR_WRT=True;
00270       fl_bnr=(char *)strdup(optarg);
00271       break;
00272     case 'C': /* Extract all coordinates associated with extracted variables? */
00273       PROCESS_ASSOCIATED_COORDINATES=!PROCESS_ASSOCIATED_COORDINATES;
00274       break;
00275     case 'c': /* Add all coordinates to extraction list? */
00276       PROCESS_ALL_COORDINATES=True;
00277       break;
00278     case 'D': /* Debugging level. Default is 0. */
00279       dbg_lvl=(unsigned short)strtol(optarg,(char **)NULL,10);
00280       break;
00281     case 'd': /* Copy argument for later processing */
00282       lmt_arg[lmt_nbr]=(char *)strdup(optarg);
00283       lmt_nbr++;
00284       break;
00285     case 'H': /* Toggle printing data to screen */
00286       PRN_VAR_DATA_TGL=True;
00287       break;
00288     case 'h': /* Toggle appending to history global attribute */
00289       HISTORY_APPEND=!HISTORY_APPEND;
00290       break;
00291     case 'l': /* Local path prefix for files retrieved from remote file system */
00292       fl_pth_lcl=(char *)strdup(optarg);
00293       break;
00294     case 'm': /* Toggle printing variable metadata to screen */
00295       PRN_VAR_METADATA_TGL=True;
00296       break;
00297     case 'M': /* Toggle printing global metadata to screen */
00298       PRN_GLB_METADATA_TGL=True;
00299       break;
00300     case 'O': /* Toggle FORCE_OVERWRITE */
00301       FORCE_OVERWRITE=!FORCE_OVERWRITE;
00302       break;
00303     case 'o': /* Name of output file */
00304       fl_out=(char *)strdup(optarg);
00305       break;
00306     case 'F': /* Toggle index convention. Default is 0-based arrays (C-style). */
00307       FORTRAN_IDX_CNV=!FORTRAN_IDX_CNV;
00308       break;
00309     case 'P': /* Print data to screen, maximal verbosity */
00310       PRN_VRB=True;
00311       PROCESS_ASSOCIATED_COORDINATES=!PROCESS_ASSOCIATED_COORDINATES;
00312       break;
00313     case 'p': /* Common file path */
00314       fl_pth=(char *)strdup(optarg);
00315       break;
00316     case 'q': /* [flg] Turn off all printing to screen */
00317       PRN_QUIET=True; /* [flg] Turn off all printing to screen */
00318       break;
00319     case 'Q': /* Turn off printing of dimension indices and coordinate values */
00320       PRN_DMN_IDX_CRD_VAL=!PRN_DMN_IDX_CRD_VAL;
00321       break;
00322     case 'R': /* Toggle removal of remotely-retrieved-files. Default is True. */
00323       REMOVE_REMOTE_FILES_AFTER_PROCESSING=!REMOVE_REMOTE_FILES_AFTER_PROCESSING;
00324       PRN_QUIET=True; /* [flg] Turn off all printing to screen */
00325       break;
00326     case 'r': /* Print CVS program information and copyright notice */
00327       (void)copyright_prn(CVS_Id,CVS_Revision);
00328       (void)nco_lbr_vrs_prn();
00329       nco_exit(EXIT_SUCCESS);
00330       break;
00331     case 's': /* User specified delimiter string for printed output */
00332       dlm_sng=(char *)strdup(optarg);
00333       break;
00334     case 'u': /* Toggle printing dimensional units */
00335       PRN_DMN_UNITS_TGL=True;
00336       break;
00337     case 'v': /* Variables to extract/exclude */
00338       /* Replace commas with hashes when within braces (convert back later) */
00339       optarg_lcl=(char *)strdup(optarg);
00340       (void)nco_lst_comma2hash(optarg_lcl);
00341       var_lst_in=lst_prs_2D(optarg_lcl,",",&var_lst_in_nbr);
00342       optarg_lcl=(char *)nco_free(optarg_lcl);
00343       nbr_xtr=var_lst_in_nbr;
00344        break;
00345     case 'x': /* Exclude rather than extract variables specified with -v */
00346       EXCLUDE_INPUT_LIST=True;
00347       break;
00348     case '?': /* Print proper usage */
00349       (void)nco_usg_prn();
00350       nco_exit(EXIT_FAILURE);
00351       break;
00352     case '-': /* Long options are not allowed */
00353       (void)fprintf(stderr,"%s: ERROR Long options are not available in this build. Use single letter options instead.\n",prg_nm_get());
00354       nco_exit(EXIT_FAILURE);
00355       break;
00356     default: /* Print proper usage */
00357       (void)nco_usg_prn();
00358       nco_exit(EXIT_FAILURE);
00359       break;
00360     } /* end switch */
00361     if(opt_crr != NULL) opt_crr=(char *)nco_free(opt_crr);
00362   } /* end while loop */
00363 
00364   /* Process positional arguments and fill in filenames */
00365   fl_lst_in=nco_fl_lst_mk(argv,argc,optind,&fl_nbr,&fl_out,&FL_LST_IN_FROM_STDIN);
00366   
00367   /* Make uniform list of user-specified dimension limits */
00368   lmt=nco_lmt_prs(lmt_nbr,lmt_arg);
00369   
00370   /* Parse filename */
00371   fl_in=nco_fl_nm_prs(fl_in,0,&fl_nbr,fl_lst_in,abb_arg_nbr,fl_lst_abb,fl_pth);
00372   /* Make sure file is on local system and is readable or die trying */
00373   fl_in=nco_fl_mk_lcl(fl_in,fl_pth_lcl,&FILE_RETRIEVED_FROM_REMOTE_LOCATION);
00374   /* Open file for reading */
00375   rcd=nco_open(fl_in,NC_NOWRITE,&in_id);
00376   
00377   /* Get number of variables, dimensions, and global attributes in file */
00378   (void)nco_inq(in_id,&nbr_dmn_fl,&nbr_var_fl,&glb_att_nbr,&rec_dmn_id);
00379   
00380   /* Form initial extraction list which may include extended regular expressions */
00381   xtr_lst=nco_var_lst_mk(in_id,nbr_var_fl,var_lst_in,PROCESS_ALL_COORDINATES,&nbr_xtr);
00382 
00383   /* Change included variables to excluded variables */
00384   if(EXCLUDE_INPUT_LIST) xtr_lst=nco_var_lst_xcl(in_id,nbr_var_fl,xtr_lst,&nbr_xtr);
00385 
00386   /* Add all coordinate variables to extraction list */
00387   if(PROCESS_ALL_COORDINATES) xtr_lst=nco_var_lst_add_crd(in_id,nbr_dmn_fl,xtr_lst,&nbr_xtr);
00388 
00389   /* Make sure coordinates associated extracted variables are also on extraction list */
00390   if(PROCESS_ASSOCIATED_COORDINATES) xtr_lst=nco_var_lst_ass_crd_add(in_id,xtr_lst,&nbr_xtr);
00391 
00392   /* Sort extraction list alphabetically or by variable ID */
00393   if(nbr_xtr > 1) xtr_lst=nco_lst_srt_nm_id(xtr_lst,nbr_xtr,ALPHABETIZE_OUTPUT);
00394     
00395   /* We now have final list of variables to extract. Phew. */
00396   
00397   /* fxm: subroutineize this code block */
00398   /* Place all dimensions in lmt_all_lst */
00399   lmt_all_lst=(lmt_all_sct **)nco_malloc(nbr_dmn_fl*sizeof(lmt_all_sct *));
00400 
00401   /* Unlimited dimensions are stored in */
00402   lmt_rgl=(lmt_sct **)nco_malloc(nbr_dmn_fl*sizeof(lmt_sct*));
00403 
00404   for(idx=0;idx<nbr_dmn_fl;idx++){
00405     (void)nco_inq_dim(in_id,idx,dmn_nm,&dmn_sz);
00406     lmt_all_crr=lmt_all_lst[idx]=(lmt_all_sct *)nco_malloc(sizeof(lmt_all_sct));
00407     lmt_all_crr->lmt_dmn=(lmt_sct **)nco_malloc(sizeof(lmt_sct *));
00408     lmt_all_crr->dmn_nm=strdup(dmn_nm);
00409     lmt_all_crr->lmt_dmn_nbr=1;
00410     lmt_all_crr->dmn_sz_org=dmn_sz;
00411     lmt_all_crr->WRP=False;
00412     lmt_all_crr->BASIC_DMN=True;
00413 
00414     /* Initialize lmt_rgl struct */
00415     lmt_rgl[idx]=(lmt_sct *)nco_malloc(sizeof(lmt_sct));
00416     lmt_rgl[idx]->nm=strdup(lmt_all_crr->dmn_nm);
00417     lmt_rgl[idx]->id=idx;
00418     lmt_rgl[idx]->is_rec_dmn=(idx == rec_dmn_id ? True : False);
00419     lmt_rgl[idx]->srt=0L;
00420     lmt_rgl[idx]->end=dmn_sz-1L;
00421     lmt_rgl[idx]->cnt=dmn_sz;
00422     lmt_rgl[idx]->srd=1L;
00423     lmt_rgl[idx]->min_sng=NULL;
00424     lmt_rgl[idx]->max_sng=NULL;
00425     lmt_rgl[idx]->srd_sng=NULL;
00426     /* A hack so we know struct has been initialized */
00427     lmt_rgl[idx]->lmt_typ=-1;
00428   
00429     lmt_all_crr->lmt_dmn[0]=lmt_rgl[idx];
00430   } /* end loop over dimensions */
00431 
00432   /* subroutineize this code block */
00433   /* Add user specified limits lmt_all_lst */
00434   for(idx=0;idx<lmt_nbr;idx++){
00435     /* Find coordinate/dimension values associated with user-specified limits
00436      NB: nco_lmt_evl() with same nc_id contains OpenMP critical region */
00437     (void)nco_lmt_evl(in_id,lmt[idx],0L,FORTRAN_IDX_CNV);
00438     for(jdx=0;jdx<nbr_dmn_fl;jdx++) {
00439       if(!strcmp(lmt[idx]->nm,lmt_all_lst[jdx]->dmn_nm)){   
00440         lmt_all_crr=lmt_all_lst[jdx];
00441         lmt_all_crr->BASIC_DMN=False;
00442         if(lmt_all_crr->lmt_dmn[0]->lmt_typ == -1) { 
00443           lmt_all_crr->lmt_dmn[0]=lmt[idx]; 
00444         }else{ 
00445           lmt_all_crr->lmt_dmn=(lmt_sct **)nco_realloc(lmt_all_crr->lmt_dmn,((lmt_all_crr->lmt_dmn_nbr)+1)*sizeof(lmt_sct *));
00446           lmt_all_crr->lmt_dmn[(lmt_all_crr->lmt_dmn_nbr)++]=lmt[idx];
00447         } /* endif */
00448         break;
00449       } /* end if */
00450     } /* end loop over dimensions */
00451     /* Dimension in limit not found */
00452     if(jdx == nbr_dmn_fl){
00453       (void)fprintf(stderr,"Unable to find limit dimension %s in list\n ",lmt[idx]->nm);
00454       nco_exit(EXIT_FAILURE);
00455     } /* end if err */
00456   } /* end loop over idx */       
00457   
00458   /* Split up wrapped limits */
00459   for(idx=0;idx<nbr_dmn_fl;idx++)
00460     if(lmt_all_lst[idx]->BASIC_DMN == False)
00461       (void)nco_msa_wrp_splt(lmt_all_lst[idx]);
00462   
00463   /* Find and store final size of each dimension */
00464   for(idx=0;idx<nbr_dmn_fl;idx++){
00465     (void)nco_msa_clc_cnt(lmt_all_lst[idx]);
00466   } /* end loop over dimensions */
00467   
00468   if(PRN_VRB || (fl_out == NULL && !PRN_VAR_DATA_TGL && !PRN_VAR_METADATA_TGL && !PRN_GLB_METADATA_TGL)){
00469     /* Verbose printing simply means assume user wants the deluxe frills by default */
00470     if(PRN_DMN_UNITS_TGL) PRN_DMN_UNITS=False; else PRN_DMN_UNITS=True;
00471     if(PRN_VAR_DATA_TGL) PRN_VAR_DATA=False; else PRN_VAR_DATA=True;
00472     if(PRN_VAR_METADATA_TGL) PRN_VAR_METADATA=False; else PRN_VAR_METADATA=True;
00473     /* Assume user wants to see global metadata unless variable extraction is invoked */
00474     if(var_lst_in == NULL) PRN_GLB_METADATA=True;
00475     if(PRN_GLB_METADATA_TGL) PRN_GLB_METADATA=!PRN_GLB_METADATA;
00476   }else{ /* end if PRN_VRB */
00477     /* Default is to print data and metadata to screen if output file is not specified */
00478     if(PRN_DMN_UNITS_TGL) PRN_DMN_UNITS=True; else PRN_DMN_UNITS=False;
00479     if(PRN_VAR_DATA_TGL) PRN_VAR_DATA=True; else PRN_VAR_DATA=False;
00480     if(PRN_VAR_METADATA_TGL) PRN_VAR_METADATA=True; else PRN_VAR_METADATA=False;
00481     if(PRN_GLB_METADATA_TGL) PRN_GLB_METADATA=True; else PRN_GLB_METADATA=False;
00482   } /* endelse */
00483 
00484   /* PRN_QUIET is catch-all which turns off all printing to screen */
00485   if(PRN_QUIET) PRN_VAR_DATA=PRN_VAR_METADATA=PRN_GLB_METADATA=False;
00486 
00487   if(NCO_BNR_WRT && fl_out == NULL){
00488     /* Native binary files depend on writing netCDF file to enter generic I/O logic */
00489     (void)fprintf(stdout,"%s: ERROR Native binary files cannot be written unless netCDF output filename also specified. HINT: Repeat command with throw-away netCDF file specified for output file (e.g., -o foo.nc)\n",prg_nm_get());
00490   } /* endif NCO_BNR_WRT */
00491     
00492   if(fl_out != NULL){
00493     int out_id;  
00494     
00495     /* Open output file */
00496     fl_out_tmp=nco_fl_out_open(fl_out,FORCE_APPEND,FORCE_OVERWRITE,fl_out_fmt,&out_id);
00497     
00498     /* Copy global attributes */
00499     (void)nco_att_cpy(in_id,out_id,NC_GLOBAL,NC_GLOBAL,True);
00500     
00501     /* Catenate timestamped command line to "history" global attribute */
00502     if(HISTORY_APPEND) (void)nco_hst_att_cat(out_id,cmd_ln);
00503     
00504     for(idx=0;idx<nbr_xtr;idx++){
00505       int var_out_id;
00506 
00507       /* Define variable in output file */
00508       if(lmt_nbr > 0) var_out_id=nco_cpy_var_dfn_lmt(in_id,out_id,rec_dmn_id,xtr_lst[idx].nm,lmt_all_lst,nbr_dmn_fl); else var_out_id=nco_cpy_var_dfn(in_id,out_id,rec_dmn_id,xtr_lst[idx].nm);
00509       /* Copy variable's attributes */
00510       (void)nco_att_cpy(in_id,out_id,xtr_lst[idx].id,var_out_id,True);
00511     } /* end loop over idx */
00512     
00513     /* Turn off default filling behavior to enhance efficiency */
00514     rcd=nco_set_fill(out_id,NC_NOFILL,&fll_md_old);
00515   
00516     /* Take output file out of define mode */
00517     if(hdr_pad == 0UL){
00518       (void)nco_enddef(out_id);
00519     }else{
00520       (void)nco__enddef(out_id,hdr_pad);
00521       if(dbg_lvl > 1) (void)fprintf(stderr,"%s: INFO Padding header with %lu extra bytes \n",prg_nm_get(),(unsigned long)hdr_pad);
00522     } /* hdr_pad */
00523     
00524     /* [fnc] Open unformatted binary data file for writing */
00525     if(NCO_BNR_WRT) fp_bnr=nco_bnr_open(fl_bnr);
00526 
00527     /* Copy variable data */
00528     for(idx=0;idx<nbr_xtr;idx++){
00529       if(dbg_lvl > 2 && !NCO_BNR_WRT) (void)fprintf(stderr,"%s, ",xtr_lst[idx].nm);
00530       if(dbg_lvl > 0) (void)fflush(stderr);
00531       /* Old hyperslab routines */
00532       /* NB: nco_cpy_var_val_lmt() contains OpenMP critical region */
00533       /* if(lmt_nbr > 0) (void)nco_cpy_var_val_lmt(in_id,out_id,fp_bnr,NCO_BNR_WRT,xtr_lst[idx].nm,lmt,lmt_nbr); else (void)nco_cpy_var_val(in_id,out_id,fp_bnr,NCO_BNR_WRT,xtr_lst[idx].nm); */
00534       /* Multi-slab routines */
00535       /* NB: nco_cpy_var_val_mlt_lmt() contains OpenMP critical region */
00536       if(lmt_nbr > 0) (void)nco_cpy_var_val_mlt_lmt(in_id,out_id,fp_bnr,NCO_BNR_WRT,xtr_lst[idx].nm,lmt_all_lst,nbr_dmn_fl); else (void)nco_cpy_var_val(in_id,out_id,fp_bnr,NCO_BNR_WRT,xtr_lst[idx].nm);
00537     } /* end loop over idx */
00538 
00539     /* [fnc] Close unformatted binary data file */
00540     if(NCO_BNR_WRT) (void)nco_bnr_close(fp_bnr,fl_bnr);
00541 
00542     /* Close output file and move it from temporary to permanent location */
00543     (void)nco_fl_out_cls(fl_out,fl_out_tmp,out_id);
00544 
00545   } /* end if fl_out != NULL */
00546   
00547   if(PRN_GLB_METADATA){
00548     (void)fprintf(stdout,"Opened file %s: dimensions = %i, variables = %i, global atts. = %i, id = %i\n",fl_in,nbr_dmn_fl,nbr_var_fl,glb_att_nbr,in_id);
00549     if(rec_dmn_id != NCO_REC_DMN_UNDEFINED){
00550       char rec_dmn_nm[NC_MAX_NAME];
00551       long rec_dmn_sz;
00552       
00553       (void)nco_inq_dim(in_id,rec_dmn_id,rec_dmn_nm,&rec_dmn_sz);
00554       (void)fprintf(stdout,"Record dimension: name = %s, size = %li\n\n",rec_dmn_nm,rec_dmn_sz);
00555     } /* end if */
00556     
00557     /* Print all global attributes */
00558     (void)nco_prn_att(in_id,NC_GLOBAL);
00559   } /* endif PRN_GLB_METADATA */
00560   
00561   if(PRN_VAR_METADATA){
00562     for(idx=0;idx<nbr_xtr;idx++){
00563       /* Print variable's definition */
00564       (void)nco_prn_var_dfn(in_id,xtr_lst[idx].nm);
00565       /* Print variable's attributes */
00566       (void)nco_prn_att(in_id,xtr_lst[idx].id);
00567     } /* end loop over idx */
00568   } /* end if PRN_VAR_METADATA */
00569 
00570   /* if(PRN_VAR_DATA){ */
00571   /* NB: nco_prn_var_val_lmt() with same nc_id contains OpenMP critical region */
00572   /* for(idx=0;idx<nbr_xtr;idx++) (void)nco_prn_var_val_lmt(in_id,xtr_lst[idx].nm,lmt,lmt_nbr,dlm_sng,FORTRAN_IDX_CNV,PRN_DMN_UNITS,PRN_DMN_IDX_CRD_VAL);
00573      } */
00574 
00575   if(PRN_VAR_DATA){
00576     /* NB: nco_msa_prn_var_val() with same nc_id contains OpenMP critical region */
00577     for(idx=0;idx<nbr_xtr;idx++) (void)nco_msa_prn_var_val(in_id,xtr_lst[idx].nm,lmt_all_lst,nbr_dmn_fl,dlm_sng,FORTRAN_IDX_CNV,PRN_DMN_UNITS,PRN_DMN_IDX_CRD_VAL);
00578   } /* end if PRN_VAR_DATA */
00579   /* Extraction list no longer needed */
00580   xtr_lst=nco_nm_id_lst_free(xtr_lst,nbr_xtr);
00581   
00582   /* Close input netCDF file */
00583   nco_close(in_id);
00584   
00585   /* Remove local copy of file */
00586   if(FILE_RETRIEVED_FROM_REMOTE_LOCATION && REMOVE_REMOTE_FILES_AFTER_PROCESSING) (void)nco_fl_rm(fl_in);
00587 
00588   /* ncks-unique memory */
00589   if(fl_bnr != NULL) fl_bnr=(char *)nco_free(fl_bnr);
00590   if(nbr_dmn_fl > 0) lmt_all_lst=nco_lmt_all_lst_free(lmt_all_lst,nbr_dmn_fl);
00591   if(nbr_dmn_fl > 0) lmt_rgl=nco_lmt_lst_free(lmt_rgl,nbr_dmn_fl);
00592 
00593   /* NCO-generic clean-up */
00594   /* Free individual strings/arrays */
00595   if(cmd_ln != NULL) cmd_ln=(char *)nco_free(cmd_ln);
00596   if(fl_in != NULL) fl_in=(char *)nco_free(fl_in);
00597   if(fl_out != NULL) fl_out=(char *)nco_free(fl_out);
00598   if(fl_out_tmp != NULL) fl_out_tmp=(char *)nco_free(fl_out_tmp);
00599   if(fl_pth != NULL) fl_pth=(char *)nco_free(fl_pth);
00600   if(fl_pth_lcl != NULL) fl_pth_lcl=(char *)nco_free(fl_pth_lcl);
00601   /* Free lists of strings */
00602   if(fl_lst_in != NULL && fl_lst_abb == NULL) fl_lst_in=nco_sng_lst_free(fl_lst_in,fl_nbr); 
00603   if(fl_lst_in != NULL && fl_lst_abb != NULL) fl_lst_in=nco_sng_lst_free(fl_lst_in,1);
00604   if(fl_lst_abb != NULL) fl_lst_abb=nco_sng_lst_free(fl_lst_abb,abb_arg_nbr);
00605   if(var_lst_in_nbr > 0) var_lst_in=nco_sng_lst_free(var_lst_in,var_lst_in_nbr);
00606   /* Free limits */
00607   for(idx=0;idx<lmt_nbr;idx++) lmt_arg[idx]=(char *)nco_free(lmt_arg[idx]);
00608   if(lmt_nbr > 0) lmt=nco_lmt_lst_free(lmt,lmt_nbr);
00609 
00610   if(rcd != NC_NOERR) nco_err_exit(rcd,"main");
00611   nco_exit_gracefully();
00612   return EXIT_SUCCESS;
00613 } /* end main() */


Generated on Thu Mar 16 18:15:05 2006 for nco by  doxygen 1.4.4