cdo.c 26.3 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2
  This file is part of CDO. CDO is a collection of Operators to
Uwe Schulzweida's avatar
Uwe Schulzweida committed
3
4
  manipulate and analyse Climate model Data.

5
  Copyright (C) 2003-2010 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  See COPYING file for copying and redistribution conditions.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

#if  defined  (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/*#include <malloc.h>*/ /* mallopt and malloc_stats */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
29
#if  defined (HAVE_GETRLIMIT)
#if  defined (HAVE_SYS_RESOURCE_H)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
31
#include <sys/time.h>       /* getrlimit */
#include <sys/resource.h>   /* getrlimit */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
34
#endif
#include <unistd.h>         /* sysconf */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35

Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
38
39
40
41
42
#if defined (SX)
#define RLIM_T  long long
#else
#define RLIM_T  rlim_t
#endif


Ralf Mueller's avatar
Ralf Mueller committed
43
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
46
47
#include "cdo.h"
#include "cdo_int.h"


Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
#if  defined  (HAVE_LIBPTHREAD)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "pstream_int.h"
#include "pthread_debug.h"
#endif

#include "modules.h"
#include "util.h"

#if defined (_OPENMP)
#  include <omp.h>
#endif

#if ! defined (VERSION)
#  define  VERSION  "0.0.1"
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
char CDO_Version[] = "Climate Data Operators version "VERSION" (http://code.zmaw.de/projects/cdo)";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66
67
68
69
70


char *Progname;

int ompNumThreads = 1;

71
72
73
74
75
76
77
78
79
80
int cdoDefaultFileType   = CDI_UNDEFID;
int cdoDefaultDataType   = CDI_UNDEFID;
int cdoDefaultTimeType   = CDI_UNDEFID;
int cdoDefaultByteorder  = CDI_UNDEFID;
int cdoDefaultTableID    = CDI_UNDEFID;

int cdoCheckDatarange    = FALSE;

int cdoHaveNC4           = FALSE;
int cdoDiag              = FALSE;
81
int cdoDisableFilesuffix = FALSE;
82
83
84
85
86
87
88
89
90
91
92
93
int cdoDisableHistory    = FALSE;
int cdoZtype             = COMPRESS_NONE;
int cdoZlevel            = 0;
int cdoLogOff            = FALSE;
int cdoSilentMode        = FALSE;
int cdoBenchmark         = FALSE;
int cdoTimer             = FALSE;
int cdoVerbose           = FALSE;
int cdoDebug             = 0;
int cdoCompress          = FALSE;
int cdoInteractive       = FALSE;
int cdoParIO             = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
int cdoRegulargrid       = FALSE;

96
97
98

int cdoExpMode           = -1;
char *cdoExpName         = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
void exp_run(int argc, char *argv[], char *cdoExpName);
100

101

Uwe Schulzweida's avatar
Uwe Schulzweida committed
102
int timer_total, timer_read, timer_write;
103
int timer_remap, timer_remap_sort, timer_remap_con, timer_remap_con2, timer_remap_con3;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105


Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
107
108
109
110
111
112
#define PRINT_RLIMIT(resource) \
      { \
	int status; \
	struct rlimit rlim; \
	status = getrlimit(resource, &rlim); \
	if ( status == 0 ) \
	  { \
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
	    if ( sizeof(RLIM_T) > sizeof(long) ) \
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116
117
118
119
120
121
122
123
124
125
126
	      { \
		fprintf(stderr, "CUR %-15s = %llu\n", #resource, (long long) rlim.rlim_cur); \
		fprintf(stderr, "MAX %-15s = %llu\n", #resource, (long long) rlim.rlim_max); \
	      } \
	    else \
	      { \
		fprintf(stderr, "CUR %-15s = %lu\n", #resource, (long) rlim.rlim_cur); \
		fprintf(stderr, "MAX %-15s = %lu\n", #resource, (long) rlim.rlim_max); \
	      } \
	  } \
      }


Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129
130
131
132
static void version(void)
{
  fprintf(stderr, "%s\n", CDO_Version);
#if defined (COMPILER)
  fprintf(stderr, "Compiler: %s\n", COMPILER);
#endif
133
134
135
#if defined (COMP_VERSION)
  fprintf(stderr, " version: %s\n", COMP_VERSION);
#endif
136
#if defined (HAVE_LIBPTHREAD) || defined (HAVE_LIBSZ) || defined (HAVE_LIBPROJ) || defined (HAVE_LIBDRMAA) || defined (HAVE_LIBCURL) || defined (_OPENMP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
  fprintf(stderr, "    with:");
138
139
  if ( cdoHaveNC4 ) 
    fprintf(stderr, " NC4");
140
141
142
#if defined (HAVE_LIBPTHREAD)
  fprintf(stderr, " PTHREADS");
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
#if defined (HAVE_LIBSZ)
144
  fprintf(stderr, " SZ");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
#endif
146
147
148
#if defined (HAVE_LIBZ)
  fprintf(stderr, " Z");
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
151
#if defined (HAVE_LIBJASPER)
  fprintf(stderr, " JASPER");
#endif
152
#if defined (HAVE_LIBPROJ)
153
  fprintf(stderr, " PROJ.4");
154
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
#if defined (HAVE_LIBDRMAA)
156
  fprintf(stderr, " DRMAA");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
#endif
#if defined (HAVE_LIBCURL)
159
  fprintf(stderr, " CURL");
160
161
162
#endif
#if defined (_OPENMP)
  fprintf(stderr, " OpenMP");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
#endif
  fprintf(stderr, "\n");
#endif
166
167
168
#if defined (USER_NAME) && defined(HOST_NAME) && defined(SYSTEM_TYPE)
  fprintf(stderr, "Compiled: by %s on %s (%s) %s %s\n",
	  USER_NAME, HOST_NAME, SYSTEM_TYPE, __DATE__, __TIME__);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
170
171
172
173
174
#endif
  cdiPrintVersion();
  fprintf(stderr, "\n");
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
176
static
void usage(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
178
179
180
181
182
{
  int id = 0;
  char *name;

  /*  fprintf(stderr, "%s\n", CDO_Version);*/
  /*  fprintf(stderr, "\n");*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
  fprintf(stderr, "usage : cdo  [Options]  Operator1  [-Operator2  [-OperatorN]]\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
185
  fprintf(stderr, "\n");
  fprintf(stderr, "  Options:\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
  fprintf(stderr, "    -a             Generate an absolute time axis\n");
187
  fprintf(stderr, "    -b <nbits>     Set the number of bits for the output precision\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
  fprintf(stderr, "                   (I8/I16/I32/F32/F64 for nc/nc2/nc4; F32/F64 for srv/ext/ieg; 1 - 32 for grb/grb2)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
  fprintf(stderr, "                   Add L or B to set the byteorder to Little or Big endian\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
  fprintf(stderr, "    -f <format>    Format of the output file. (grb, grb2, nc, nc2, nc4, srv, ext or ieg)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
  fprintf(stderr, "    -g <grid>      Set default grid name or file. Available grids: \n");
192
  fprintf(stderr, "                   n<N>, t<RES>, tl<RES>, r<NX>x<NY>, g<NX>x<NY>, gme<NI>, lon=<LON>_lat=<LAT>\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
194
195
196
197
198
199
200
201
202
  fprintf(stderr, "    -h             Help information for the operators\n");
  /*
  fprintf(stderr, "    -i <inst>      Institution name/file\n");
  fprintf(stderr, "                   Predefined instituts: ");
  for ( id = 0; id < institutInqNumber; id++ )
    if ( (name = institutInqNamePtr(id)) )
      fprintf(stderr, " %s", name);
  fprintf(stderr, "\n");
  */
  /* fprintf(stderr, "    -l <level>     Level file\n"); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
  fprintf(stderr, "    -M             Switch to indicate that the I/O streams have missing values\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
  fprintf(stderr, "    -m <missval>   Set the default missing value (default: %g)\n", cdiInqMissval());
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
207
#if defined (_OPENMP)
  fprintf(stderr, "    -P <nthreads>  Set number of OpenMP threads\n");
#endif
208
  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
  fprintf(stderr, "    -p <prec>      Set the precision of the output data in bytes\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
  fprintf(stderr, "                   (4/8 for nc, nc2, nc4, srv, ext, ieg; 1/2/3 for grb)\n");
211
  */
212
  fprintf(stderr, "    -Q             Sort netCDF variable names\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213
  fprintf(stderr, "    -R             Convert GRIB data from reduced to regular grid\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
  fprintf(stderr, "    -r             Generate a relative time axis\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
  fprintf(stderr, "    -S             Create an extra output stream for the module TIMSTAT. This stream\n");
  fprintf(stderr, "                   contains the number of non missing values for each output period.\n");
217
  fprintf(stderr, "    -s             Silent mode\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
  fprintf(stderr, "    -t <partab>    Set default parameter table name or file\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
220
221
222
223
224
225
226
  fprintf(stderr, "                   Predefined tables: ");
  for ( id = 0; id < tableInqNumber(); id++ )
    if ( (name = tableInqNamePtr(id)) )
      fprintf(stderr, " %s", name);
  fprintf(stderr, "\n");

  fprintf(stderr, "    -V             Print the version number\n");
  fprintf(stderr, "    -v             Print extra details for some operators\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
228
  fprintf(stderr, "    -z szip        SZIP compression of GRIB1 records\n");
  fprintf(stderr, "       jpeg        JPEG compression of GRIB2 records\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
  fprintf(stderr, "        zip        Deflate compression of netCDF4 variables\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230
231
232
233
234
235
  fprintf(stderr, "\n");

  fprintf(stderr, "  Operators:\n");
  operatorPrintAll();

  fprintf(stderr, "\n");
236
  fprintf(stderr, "  CDO version %s, Copyright (C) 2003-2010 Uwe Schulzweida\n", VERSION);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
  //  fprintf(stderr, "  Available from <http://code.zmaw.de/projects/cdo>\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
  fprintf(stderr, "  This is free software and comes with ABSOLUTELY NO WARRANTY\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
  fprintf(stderr, "  Report bugs to <http://code.zmaw.de/projects/cdo>\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
241
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
243
static
void cdoPrintHelp(char *phelp[]/*, char *xoperator*/)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
245
246
247
248
{
  if ( phelp == NULL )
    printf("No help available for this operator!\n");
  else
    {
249
250
      int lprint;
      while ( *phelp )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
	{
252
253
254
255
256
257
258
259
	  lprint = TRUE;
	  if ( *phelp[0] == '\0' )
	    if ( *(phelp+1) )
	      if ( *(phelp+1)[0] == ' ' ) lprint = FALSE;
	  
	  if ( lprint ) printf("%s\n", *phelp);

	  phelp++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
261
262
263
	}
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
265
static
void cdoSetDebug(int level)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{
  /*
    level   0: off
    level   1: on
    level   2: cdi
    level   4: memory
    level   8: file
    level  16: format
    level  32: cdo
    level  64: stream
    level 128: pipe
    level 256: pthread
   */
  cdiDebug(level);

  if ( level == 1 || level &  32 ) cdoDebug = 1;
  if ( level == 1 || level &  64 ) pstreamDebug(1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283
#if  defined  (HAVE_LIBPTHREAD)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
285
286
287
288
289
290
291
  if ( level == 1 || level & 128 ) pipeDebug(1);
  if ( level == 1 || level & 256 ) Pthread_debug(1);
#endif
}

static int cdoOptind = 1;
static char *cdoOptarg;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
292
293
static
int cdoGetopt(int argc, char * const argv[], const char *optstring)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{
  static int optpos = 0;
  int optval = -1, value;
  int opthasarg = 0;
  int optstrlen = strlen(optstring);
  int iargc;

  cdoOptarg = NULL;

  while ( optpos < optstrlen && cdoOptind < argc )
    {
      value = optstring[optpos];
      optpos++;
      if ( optstring[optpos] == ':' )
	{
	  opthasarg = 1;
	  optpos++;
	}
      else
	opthasarg = 0;

      for ( iargc = 1; iargc < argc; iargc++ )
	{
	  if ( *argv[iargc] == '-' && strlen(argv[iargc]) == 2 )
	    {
	      if ( (argv[iargc][1]) == value )
		{
		  optval = value;
		  cdoOptind++;
		  if ( opthasarg )
		    {
		      cdoOptarg = argv[iargc+1];
		      cdoOptind++;
		    }
		  break;
		}
	    }
	}
      if ( iargc < argc ) break;
    }

  if ( opthasarg && cdoOptarg == NULL ) optval = ':';

  return (optval);
}

#undef  IsBigendian
#define IsBigendian()  ( u_byteorder.c[sizeof(long) - 1] )

Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
344
static
void setDefaultDataType(char *datatypestr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345
346
{
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
  int nbits = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
  enum {D_UINT, D_INT, D_FLT, D_CPX};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
  int dtype = -1;

  if      ( *datatypestr == 'i' || *datatypestr == 'I' )
    {
      dtype = D_INT;
      datatypestr++;
    }
  else if ( *datatypestr == 'u' || *datatypestr == 'U' )
    {
      dtype = D_UINT;
      datatypestr++;
    }
  else if ( *datatypestr == 'f' || *datatypestr == 'F' )
    {
      dtype = D_FLT;
      datatypestr++;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
368
369
370
  else if ( *datatypestr == 'c' || *datatypestr == 'C' )
    {
      dtype = D_CPX;
      datatypestr++;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371

372
373
  if ( isdigit((int) *datatypestr) )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
375
      nbits = atoi(datatypestr);
      if ( nbits < 10 )
376
377
378
379
	datatypestr += 1;
      else
	datatypestr += 2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
380
      if ( dtype == -1 )
381
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382
383
384
385
386
387
388
389
390
	  if      ( nbits > 0 && nbits < 32 ) cdoDefaultDataType = nbits;
	  else if ( nbits == 32 )
	    {
	      if ( cdoDefaultFileType == FILETYPE_GRB )
		cdoDefaultDataType = DATATYPE_PACK32;
	      else
		cdoDefaultDataType = DATATYPE_FLT32;
	    }
	  else if ( nbits == 64 ) cdoDefaultDataType = DATATYPE_FLT64;
391
	  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
393
	    {
	      fprintf(stderr, "Unsupported number of bits %d!\n", nbits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
	      fprintf(stderr, "Use 32/64 for filetype nc/srv/ext/ieg and 1-32 for grb/grb2.\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
	      exit(EXIT_FAILURE);
	    }
397
398
399
	}
      else
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
	  if ( dtype == D_INT )
	    {
	      if      ( nbits ==  8 ) cdoDefaultDataType = DATATYPE_INT8;
	      else if ( nbits == 16 ) cdoDefaultDataType = DATATYPE_INT16;
	      else if ( nbits == 32 ) cdoDefaultDataType = DATATYPE_INT32;
	      else
		{
		  fprintf(stderr, "Unsupported number of bits = %d for datatype INT!\n", nbits);
		  exit(EXIT_FAILURE);
		}
	    }
	  /*
	  else if ( dtype == D_UINT )
	    {
	      if      ( nbits ==  8 ) cdoDefaultDataType = DATATYPE_UINT8;
	      else
		{
		  fprintf(stderr, "Unsupported number of bits = %d for datatype UINT!\n", nbits);
		  exit(EXIT_FAILURE);
		}
	    }
	  */
	  else if ( dtype == D_FLT )
	    {
	      if      ( nbits == 32 ) cdoDefaultDataType = DATATYPE_FLT32;
	      else if ( nbits == 64 ) cdoDefaultDataType = DATATYPE_FLT64;
	      else
		{
		  fprintf(stderr, "Unsupported number of bits = %d for datatype FLT!\n", nbits);
		  exit(EXIT_FAILURE);
		}
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
433
434
435
436
437
438
439
440
441
	  else if ( dtype == D_CPX )
	    {
	      if      ( nbits == 32 ) cdoDefaultDataType = DATATYPE_CPX32;
	      else if ( nbits == 64 ) cdoDefaultDataType = DATATYPE_CPX64;
	      else
		{
		  fprintf(stderr, "Unsupported number of bits = %d for datatype CPX!\n", nbits);
		  exit(EXIT_FAILURE);
		}
	    }
442
443
444
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
445
  if ( *datatypestr != 0 )
446
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
      if ( *datatypestr == 'l' || *datatypestr == 'L' )
	{
	  if ( IsBigendian() ) cdoDefaultByteorder = CDI_LITTLEENDIAN;
	  datatypestr++;
	}
      else if ( *datatypestr == 'b' || *datatypestr == 'B' )
	{
	  if ( ! IsBigendian() ) cdoDefaultByteorder = CDI_BIGENDIAN;
	  datatypestr++;
	}
      else
	{
	  fprintf(stderr, "Unsupported character in number of bytes: >%s< !\n", datatypestr);
	  exit(EXIT_FAILURE);
	}
462
463
464
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
465
466
static
void setDefaultDataTypeByte(char *datatypestr)
467
468
469
470
{
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
  int datatype = -1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
471
472
473
474
475
  if ( isdigit((int) *datatypestr) )
    {
      datatype = atoi(datatypestr);
      datatypestr++;

476
477
478
479
480
      if      ( datatype == 1 ) cdoDefaultDataType = DATATYPE_PACK8;
      else if ( datatype == 2 ) cdoDefaultDataType = DATATYPE_PACK16;
      else if ( datatype == 3 ) cdoDefaultDataType = DATATYPE_PACK24;
      else if ( datatype == 4 ) cdoDefaultDataType = DATATYPE_FLT32;
      else if ( datatype == 8 ) cdoDefaultDataType = DATATYPE_FLT64;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
482
483
      else
	{
	  fprintf(stderr, "Unsupported datatype %d!\n", datatype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
	  fprintf(stderr, "Use 4/8 for filetype nc/srv/ext/ieg and 1/2/3 for grb/grb2.\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
487
488
	  exit(EXIT_FAILURE);
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
  if ( *datatypestr != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
490
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
      if ( *datatypestr == 'l' || *datatypestr == 'L' )
	{
	  if ( IsBigendian() ) cdoDefaultByteorder = CDI_LITTLEENDIAN;
	  datatypestr++;
	}
      else if ( *datatypestr == 'b' || *datatypestr == 'B' )
	{
	  if ( ! IsBigendian() ) cdoDefaultByteorder = CDI_BIGENDIAN;
	  datatypestr++;
	}
      else
	{
	  fprintf(stderr, "Unsupported character in number of bytes: %s!\n", datatypestr);
	  exit(EXIT_FAILURE);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
508
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
510
511
512
513
void setDefaultFileType(char *filetypestr, int labort)
{
  if ( filetypestr )
    {
514
515
      char *ftstr = filetypestr;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
516
      if      ( memcmp(filetypestr, "grb2", 4)  == 0 ) { ftstr += 4; cdoDefaultFileType = FILETYPE_GRB2;}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
      else if ( memcmp(filetypestr, "grb1", 4)  == 0 ) { ftstr += 4; cdoDefaultFileType = FILETYPE_GRB; }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
518
519
520
521
522
523
524
      else if ( memcmp(filetypestr, "grb",  3)  == 0 ) { ftstr += 3; cdoDefaultFileType = FILETYPE_GRB; }
      else if ( memcmp(filetypestr, "nc2",  3)  == 0 ) { ftstr += 3; cdoDefaultFileType = FILETYPE_NC2; }
      else if ( memcmp(filetypestr, "nc4",  3)  == 0 ) { ftstr += 3; cdoDefaultFileType = FILETYPE_NC4; }
      else if ( memcmp(filetypestr, "nc",   2)  == 0 ) { ftstr += 2; cdoDefaultFileType = FILETYPE_NC;  }
      else if ( memcmp(filetypestr, "srv",  3)  == 0 ) { ftstr += 3; cdoDefaultFileType = FILETYPE_SRV; }
      else if ( memcmp(filetypestr, "ext",  3)  == 0 ) { ftstr += 3; cdoDefaultFileType = FILETYPE_EXT; }
      else if ( memcmp(filetypestr, "ieg",  3)  == 0 ) { ftstr += 3; cdoDefaultFileType = FILETYPE_IEG; }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
526
527
528
529
      else
	{
	  if ( labort )
	    {
	      fprintf(stderr, "Unsupported filetype %s!\n", filetypestr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
530
	      fprintf(stderr, "Available filetypes: grb, grb2, nc, nc2, nc4, srv, ext and ieg\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
532
533
534
535
536
537
538
	      exit(EXIT_FAILURE);
	    }
	  else
	    {
	      return;
	    }
	}

539
      if ( cdoDefaultFileType != CDI_UNDEFID && *ftstr != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
540
	{
541
	  if ( *ftstr == '_' )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
	    {
543
	      ftstr++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
544

545
546
547
548
549
550
	      setDefaultDataType(ftstr);
	    }
	  else
	    {
	      fprintf(stderr, "Unexpected character >%c< in file type >%s<!\n", *ftstr, filetypestr);
	      fprintf(stderr, "Use format[_nbits] with:\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
	      fprintf(stderr, "    format = grb, grb2, nc, nc2, nc4, srv, ext or ieg\n");
	      fprintf(stderr, "    nbits  = 32/64 for nc/nc2/nc4/srv/ext/ieg; 1 - 32 for grb/grb2\n");
553
	      exit(EXIT_FAILURE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
554
555
556
557
558
	    }
	}
    }
}

559

Uwe Schulzweida's avatar
Uwe Schulzweida committed
560
561
562
563
564
int cdoFiletype(void)
{
  if ( cdoDefaultFileType == CDI_UNDEFID )
    {
      cdoDefaultFileType = FILETYPE_GRB;
565
566
      if ( ! cdoSilentMode )
	cdoPrint("Set default filetype to GRIB");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
568
569
570
571
572
    }

  return (cdoDefaultFileType);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
573
574
575
#if  defined  (HAVE_LIBNETCDF)
#include "netcdf.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
576
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
577
578
579
580
581
582
583
584
585
586
587
588
589
int have_netCDF4(void)
{
  int haveNC4 = FALSE;

#if  defined  (HAVE_LIBNETCDF)
#if  defined  (NC_NETCDF4)
  haveNC4 = TRUE;
#endif
#endif

  return (haveNC4);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
590
static
591
592
593
594
void defineCompress(const char *arg)
{
  size_t len = strlen(arg);

595
  if      ( memcmp(arg, "szip", len) == 0 )
596
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
597
598
      cdoZtype  = COMPRESS_SZIP;
      cdoZlevel = 0;
599
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
601
602
603
604
  else if ( memcmp(arg, "jpeg", len) == 0 )
    {
      cdoZtype = COMPRESS_JPEG;
      cdoZlevel = 0;
    }
605
  else if ( memcmp(arg, "gzip", len) == 0 )
606
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
608
      cdoZtype  = COMPRESS_GZIP;
      cdoZlevel = 6;
609
    }
610
  else if ( memcmp(arg, "zip", len) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
611
612
613
614
    {
      cdoZtype  = COMPRESS_ZIP;
      cdoZlevel = 1;
    }
615
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
616
    fprintf(stderr, "%s compression unsupported!\n", arg);
617
618
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
static
void get_env_vars(void)
{
  char *envstr;

  envstr = getenv("CDO_LOG_OFF");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
	{
	  cdoLogOff = TRUE;
	  if ( cdoVerbose )
	    fprintf(stderr, "CDO_LOG_OFF         = %s\n", envstr);
	}
    }

  envstr = getenv("CDO_DISABLE_HISTORY");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
	{
	  cdoDisableHistory = TRUE;
	  if ( cdoVerbose )
	    fprintf(stderr, "CDO_DISABLE_HISTORY = %s\n", envstr);
	}
    }

  envstr = getenv("CDO_DISABLE_FILESUFFIX");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
	{
	  cdoDisableFilesuffix = TRUE;
	  if ( cdoVerbose )
	    fprintf(stderr, "CDO_DISABLE_FILESUFFIX = %s\n", envstr);
	}
    }

  envstr = getenv("CDO_DIAG");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
	{
	  cdoDiag = TRUE;
	  if ( cdoVerbose )
	    fprintf(stderr, "CDO_DIAG = %s\n", envstr);
	}
    }
}

669

Uwe Schulzweida's avatar
Uwe Schulzweida committed
670
671
int main(int argc, char *argv[])
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
672
  static const char *func = "main";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
673
674
675
676
677
678
679
  int c;
  int Debug = 0;
  int Version = 0;
  int Help = 0;
  int DebugLevel = 0;
  int lstop = FALSE;
  int noff = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
680
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
681
  int numThreads = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
682
683
684
685
686
687
688
689
690
691
692
693
694
  char *operatorName = NULL;
  char *operatorArg = NULL;
  char *argument = NULL;
  extern int dmemory_ExitOnError;

  dmemory_ExitOnError = 1;

  /* mallopt(M_MMAP_MAX, 0); */
 
  setCommandLine(argc, argv);

  Progname = getProgname(argv[0]);

695
  if ( memcmp(Progname, "cdo", 3) == 0 && strlen(Progname) > 3 ) noff = 3;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
696
697

  /* old versions !!!! */
698
699
  if ( memcmp(Progname, "gdo", 3) == 0 && strlen(Progname) > 3 ) noff = 3;
  if ( memcmp(Progname, "gm",  2) == 0 && strlen(Progname) > 2 ) noff = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
700
701
702

  if ( noff ) setDefaultFileType(Progname+noff, 0);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
703
704
  cdoHaveNC4 = have_netCDF4();

705
  while ( (c = cdoGetopt(argc, argv, "f:b:e:P:p:g:i:l:m:t:D:z:aBcdhMQRrsSTuVvXZ")) != -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
707
708
709
710
711
712
    {
      switch (c)
	{
	case 'a':
	  cdoDefaultTimeType = TAXIS_ABSOLUTE;
	  break;
	case 'b':
713
714
715
	  setDefaultDataType(cdoOptarg);
	  break;
	case 'B':
Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
717
	  cdoBenchmark = TRUE;
	  break;
718
719
720
	case 'c':
	  cdoCheckDatarange = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
721
722
723
724
725
726
727
	case 'd':
	  Debug = 1;
	  break;
	case 'D':
	  Debug = 1;
	  DebugLevel = atoi(cdoOptarg);
	  break;
728
729
	case 'e':
	  {
730
#if defined (HAVE_GETHOSTNAME)
731
732
733
734
735
736
737
738
	  char host[1024];
	  gethostname(host, sizeof(host));
	  cdoExpName = cdoOptarg;
	  /* printf("host: %s %s\n", host, cdoExpName); */
	  if ( strcmp(host, cdoExpName) == 0 )
	    cdoExpMode = CDO_EXP_REMOTE;
	  else
            cdoExpMode = CDO_EXP_LOCAL;
739
740
741
742
#else
          fprintf(stderr, "Function gethostname not available!\n");
	  exit(EXIT_FAILURE);
#endif
743
744
          break;
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
	case 'f':
	  setDefaultFileType(cdoOptarg, 1);
	  break;
	case 'g':
	  defineGrid(cdoOptarg);
	  break;
	case 'h':	
	  Help = 1;
	  break;
	case 'i':
	  defineInstitution(cdoOptarg);
	  break;
	case 'l':
	  defineZaxis(cdoOptarg);
	  break;
	case 'm':
	  cdiDefMissval(atof(cdoOptarg));
	  break;
763
764
	case 'M':
	  cdiDefGlobal("HAVE_MISSVAL", TRUE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
765
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766
767
768
	case 'P':
	  numThreads = atoi(cdoOptarg);
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
	case 'p':
Uwe Schulzweida's avatar
Uwe Schulzweida committed
770
	  fprintf(stderr, "CDO option -p is obsolete and will be removed in the next release, please switch to -b <bits>!\n");
771
	  setDefaultDataTypeByte(cdoOptarg);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
772
	  break;
773
774
775
	case 'Q':
	  cdiDefGlobal("SORTNAME", TRUE);
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
776
777
	case 'R':
	  cdoRegulargrid = TRUE;
778
	  cdiDefGlobal("REGULARGRID", TRUE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
779
780
781
782
	  break;
	case 'r':
	  cdoDefaultTimeType = TAXIS_RELATIVE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783
784
785
	case 'S':
	  cdoDiag = TRUE;
	  break;
786
787
788
	case 's':
	  cdoSilentMode = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
789
790
791
792
793
794
	case 'T':
	  cdoTimer = TRUE;
	  break;
	case 't':
	  cdoDefaultTableID = defineTable(cdoOptarg);
	  break;
795
796
797
	case 'u':
	  cdoInteractive = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
798
799
800
801
802
803
	case 'V':
	  Version = 1;
	  break;
	case 'v':
	  cdoVerbose = TRUE;
	  break;
804
805
806
	case 'X': /* multi threaded I/O */
	  cdoParIO = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
807
808
809
	case 'Z':
	  cdoCompress = TRUE;
          break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
810
	case 'z':
811
812
	  defineCompress(cdoOptarg);
          break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
813
814
815
816
817
818
819
	case ':':
	  fprintf(stderr, "\nmissing parameter for one of the options\n\n");	  
	  Help = 1;
	  break;
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
820
  get_env_vars();
821

Uwe Schulzweida's avatar
Uwe Schulzweida committed
822
823
824
825
  if ( Debug || Version ) version();

  if ( Debug )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
826
827
      char *envstr;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
829
830
      if ( DebugLevel == 0 ) DebugLevel = 1;
      cdoSetDebug(DebugLevel);
      fprintf(stderr, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
831
832
833
834
      if ( cdoHaveNC4 )
	fprintf(stderr, "cdoHaveNC4          = TRUE\n");
      else
	fprintf(stderr, "cdoHaveNC4          = FALSE\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
835
836
837
838
839
840
      fprintf(stderr, "cdoDefaultFileType  = %d\n", cdoDefaultFileType);
      fprintf(stderr, "cdoDefaultDataType  = %d\n", cdoDefaultDataType);
      fprintf(stderr, "cdoDefaultByteorder = %d\n", cdoDefaultByteorder);
      fprintf(stderr, "cdoDefaultTableID   = %d\n", cdoDefaultTableID);
      fprintf(stderr, "\n");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
841
842
843
844
845
846
847
848
849
850
      envstr = getenv("HOSTTYPE");
      if ( envstr ) fprintf(stderr, "HOSTTYPE            = %s\n", envstr);
      envstr = getenv("VENDOR");
      if ( envstr ) fprintf(stderr, "VENDOR              = %s\n", envstr);
      envstr = getenv("OSTYPE");
      if ( envstr ) fprintf(stderr, "OSTYPE              = %s\n", envstr);
      envstr = getenv("MACHTYPE");
      if ( envstr ) fprintf(stderr, "MACHTYPE            = %s\n", envstr);
      fprintf(stderr, "\n");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
851
852
853
854
855
856
857
858
#if defined (HAVE_MMAP)
      fprintf(stderr, "HAVE_MMAP\n");
#endif
#if defined (HAVE_MEMORY_H)
      fprintf(stderr, "HAVE_MEMORY_H\n");
#endif
      fprintf(stderr, "\n");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
#if defined (__STDC__)
      fprintf(stderr, "STD ANSI C          = %d\n", __STDC__);
#endif
#if defined (__STD_VERSION__)
      fprintf(stderr, "STD VERSION         = %ld\n", __STD_VERSION__);
#endif
#if defined (__STDC_VERSION__)
      fprintf(stderr, "STDC VERSION        = %ld\n", __STDC_VERSION__);
#endif
#if defined (__STD_HOSTED__)
      fprintf(stderr, "STD HOSTED          = %d\n", __STD_HOSTED__);
#endif
#if defined (FLT_EVAL_METHOD)
      fprintf(stderr, "FLT_EVAL_METHOD     = %d\n", FLT_EVAL_METHOD);
#endif
#if defined (FP_FAST_FMA)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
875
      fprintf(stderr, "FP_FAST_FMA         = defined\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
#endif
      fprintf(stderr, "\n");

#if defined (_SC_VERSION)
      fprintf(stderr, "POSIX.1 VERSION     = %ld\n", sysconf(_SC_VERSION));
#endif
#if defined (_SC_ARG_MAX)
      fprintf(stderr, "POSIX.1 ARG_MAX     = %ld\n", sysconf(_SC_ARG_MAX));
#endif
#if defined (_SC_CHILD_MAX)
      fprintf(stderr, "POSIX.1 CHILD_MAX   = %ld\n", sysconf(_SC_CHILD_MAX));
#endif
#if defined (_SC_STREAM_MAX)
      fprintf(stderr, "POSIX.1 STREAM_MAX  = %ld\n", sysconf(_SC_STREAM_MAX));
#endif
#if defined (_SC_OPEN_MAX)
      fprintf(stderr, "POSIX.1 OPEN_MAX    = %ld\n", sysconf(_SC_OPEN_MAX));
#endif
#if defined (_SC_PAGESIZE)
      fprintf(stderr, "POSIX.1 PAGESIZE    = %ld\n", sysconf(_SC_PAGESIZE));
#endif

      fprintf(stderr, "\n");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
900
#if defined (HAVE_GETRLIMIT)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
901
902
903
904
905
906
907
908
#if defined (RLIMIT_FSIZE)
      PRINT_RLIMIT(RLIMIT_FSIZE);
#endif
#if defined (RLIMIT_NOFILE)
      PRINT_RLIMIT(RLIMIT_NOFILE);
#endif
#if defined (RLIMIT_STACK)
      PRINT_RLIMIT(RLIMIT_STACK);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
910
911
912
913
#endif
      fprintf(stderr, "\n");
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
914
#if defined (HAVE_GETRLIMIT)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
915
916
917
918
919
#if defined (RLIMIT_STACK)
  {
#define  MIN_STACK_SIZE  67108864L  /* 64MB */
    int status;
    struct rlimit rlim;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
920
    RLIM_T min_stack_size = MIN_STACK_SIZE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
921

Uwe Schulzweida's avatar
Uwe Schulzweida committed
922
923
    status = getrlimit(RLIMIT_STACK, &rlim);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
924
    if ( status == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
925
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
926
927
	if ( min_stack_size > rlim.rlim_max ) min_stack_size = rlim.rlim_max;
	if ( rlim.rlim_cur < min_stack_size )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
928
	  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
929
930
931
932
	    rlim.rlim_cur = min_stack_size;

	    status = setrlimit(RLIMIT_STACK, &rlim);
	    if ( Debug )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
933
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
934
935
936
937
938
939
940
		if ( status == 0 )
		  {
		    fprintf(stderr, "Set stack size to %ld\n", MIN_STACK_SIZE);
		    PRINT_RLIMIT(RLIMIT_STACK);
		  }
		else
		  fprintf(stderr, "Set stack size to %ld failed!\n", MIN_STACK_SIZE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
941
942
943
944
	      }
	  }
      }
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
945
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
946
947
948
949
#endif

  if ( Debug )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
950
951
952
953
      print_pthread_info();
    }

#if defined (_OPENMP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
954
955
  if ( numThreads <= 0 ) numThreads = 1;
  omp_set_num_threads(numThreads);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
956
  ompNumThreads = omp_get_max_threads();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
957
  if ( omp_get_max_threads() > omp_get_num_procs() )
958
    fprintf(stderr, "Warning: Number of OMP threads is greater than number of CPUs=%d!\n", omp_get_num_procs());
Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
960
961
962
963
964
965
966
967
  if ( cdoVerbose )
    fprintf(stderr, " OpenMP:  num_procs = %d  max_threads = %d\n",
	    omp_get_num_procs(), omp_get_max_threads());
#else
  if ( numThreads > 0 )
    {
      fprintf(stderr, "Option -P failed, OpenMP support not compiled in!\n");
      return(-1);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
968
969
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988

  if ( cdoOptind < argc )
    {
      operatorArg = argv[cdoOptind];
      argument = makeArgument(argc-cdoOptind, &argv[cdoOptind]);
    }
  else
    {
      if ( ! Version && ! Help )
	{
	  fprintf(stderr, "\nno operator given\n\n");
	  usage();
	  status = 1;
	}

      if ( Help ) usage();
      lstop = TRUE;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
989
  if ( lstop ) return (status);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
990

991
992
993
994
995
  if ( cdoDefaultTableID != CDI_UNDEFID ) cdiDefTableID(cdoDefaultTableID);

  operatorName = getOperatorName(operatorArg);

  if ( Help )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
996
    {
997
      cdoPrintHelp(operatorHelp(operatorName)/*, operatorName*/);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
998
    }
999
1000
  else if ( cdoExpMode == CDO_EXP_LOCAL )
    {
For faster browsing, not all history is shown. View entire blame