cdo.c 27.2 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.

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2011 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
#endif
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
34
#include <unistd.h>         /* sysconf, gethostname */
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 cdoDiag              = FALSE;
int cdoDisableHistory    = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
82
int cdoCompType          = COMPRESS_NONE;  // compression type
int cdoCompLevel         = 0;              // compression level
83
84
int cdoLogOff            = FALSE;
int cdoSilentMode        = FALSE;
85
int cdoOverwriteMode     = FALSE;
86
87
88
89
90
91
92
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
93
94
int cdoRegulargrid       = FALSE;

95
char cdo_file_suffix[32];
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); \
	      } \
	  } \
      }


127
128
static
void cdo_version(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130
131
132
133
{
  fprintf(stderr, "%s\n", CDO_Version);
#if defined (COMPILER)
  fprintf(stderr, "Compiler: %s\n", COMPILER);
#endif
134
135
136
#if defined (COMP_VERSION)
  fprintf(stderr, " version: %s\n", COMP_VERSION);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
  fprintf(stderr, "    with:");
138
139
140
141
142
143
#if defined (HAVE_LIBPTHREAD)
  fprintf(stderr, " PTHREADS");
#endif
#if defined (_OPENMP)
  fprintf(stderr, " OpenMP");
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
146
#if  defined  (HAVE_NETCDF4)
  fprintf(stderr, " NC4");
#endif
147
148
149
#if  defined  (HAVE_LIBNC_DAP)
  fprintf(stderr, " OPeNDAP");
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
#if defined (HAVE_LIBSZ)
151
  fprintf(stderr, " SZ");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
#endif
153
154
155
#if defined (HAVE_LIBZ)
  fprintf(stderr, " Z");
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
157
158
#if defined (HAVE_LIBJASPER)
  fprintf(stderr, " JASPER");
#endif
159
#if defined (HAVE_LIBPROJ)
160
  fprintf(stderr, " PROJ.4");
161
#endif
162
163
164
#if defined (HAVE_LIBMAGICS)
  fprintf(stderr, " MAGICS");
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
#if defined (HAVE_LIBDRMAA)
166
  fprintf(stderr, " DRMAA");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
#endif
#if defined (HAVE_LIBCURL)
169
  fprintf(stderr, " CURL");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
#endif
  fprintf(stderr, "\n");
172
173
174
#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
175
176
177
178
179
#endif
  cdiPrintVersion();
  fprintf(stderr, "\n");
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
181
static
void usage(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
184
185
186
187
{
  int id = 0;
  char *name;

  /*  fprintf(stderr, "%s\n", CDO_Version);*/
  /*  fprintf(stderr, "\n");*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
  fprintf(stderr, "usage : cdo  [Options]  Operator1  [-Operator2  [-OperatorN]]\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
190
  fprintf(stderr, "\n");
  fprintf(stderr, "  Options:\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
  fprintf(stderr, "    -a             Generate an absolute time axis\n");
192
  fprintf(stderr, "    -b <nbits>     Set the number of bits for the output precision\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
  fprintf(stderr, "                   (I8/I16/I32/F32/F64 for nc/nc2/nc4/nc4c; F32/F64 for grb2/srv/ext/ieg; P1 - P24 for grb/grb2)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194
  fprintf(stderr, "                   Add L or B to set the byteorder to Little or Big endian\n");
195
  fprintf(stderr, "    -f <format>    Format of the output file. (grb/grb2/nc/nc2/nc4/nc4c/srv/ext/ieg)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
  fprintf(stderr, "    -g <grid>      Set default grid name or file. Available grids: \n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
  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
198
199
200
201
202
203
204
205
206
207
  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
208
  fprintf(stderr, "    -M             Switch to indicate that the I/O streams have missing values\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
  fprintf(stderr, "    -m <missval>   Set the default missing value (default: %g)\n", cdiInqMissval());
210
  fprintf(stderr, "    -O             Overwrite existing output file, if checked\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
212
213
#if defined (_OPENMP)
  fprintf(stderr, "    -P <nthreads>  Set number of OpenMP threads\n");
#endif
214
  fprintf(stderr, "    -Q             Alphanumeric sorting of netCDF parameter names\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
  fprintf(stderr, "    -R             Convert GRIB1 data from reduced to regular grid (only with cgribex)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
  fprintf(stderr, "    -r             Generate a relative time axis\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
218
  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");
219
  fprintf(stderr, "    -s             Silent mode\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
  fprintf(stderr, "    -t <partab>    Set default parameter table name or file\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
223
224
225
226
227
228
  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
229
230
  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
231
  fprintf(stderr, "        zip        Deflate compression of netCDF4 variables\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234
235
236
237
  fprintf(stderr, "\n");

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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
245
static
void cdoPrintHelp(char *phelp[]/*, char *xoperator*/)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
247
248
249
250
{
  if ( phelp == NULL )
    printf("No help available for this operator!\n");
  else
    {
251
252
      int lprint;
      while ( *phelp )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
	{
254
255
256
257
258
259
260
261
	  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
262
263
264
265
	}
    }
}

266
267
268
269
270
271
272
273
274
275
276
277
278

void cdoGenFileSuffix(char *filesuffix, size_t maxlen, int filetype, int vlistID)
{
  if ( strncmp(cdo_file_suffix, "NULL", 4) )
    {
      if ( cdo_file_suffix[0] != 0 )
	{
	  strncat(filesuffix, cdo_file_suffix, maxlen-1);
	}
      else
	{
	  strncat(filesuffix, streamFilesuffix(filetype), maxlen-1);
	  if ( cdoDefaultFileType == FILETYPE_GRB )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
	    if ( vlistIsSzipped(vlistID) || cdoCompType == COMPRESS_SZIP )
280
281
282
283
284
	      strncat(filesuffix, ".sz", maxlen-1);
	}
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
286
static
void cdoSetDebug(int level)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{
  /*
    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
304
#if  defined  (HAVE_LIBPTHREAD)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
306
307
308
309
310
311
312
  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
313
314
static
int cdoGetopt(int argc, char * const argv[], const char *optstring)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
{
  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
364
365
static
void setDefaultDataType(char *datatypestr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
{
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
  int nbits = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
  enum {D_UINT, D_INT, D_FLT, D_CPX};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  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
387
388
389
390
391
  else if ( *datatypestr == 'c' || *datatypestr == 'C' )
    {
      dtype = D_CPX;
      datatypestr++;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392

393
394
  if ( isdigit((int) *datatypestr) )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
      nbits = atoi(datatypestr);
      if ( nbits < 10 )
397
398
399
400
	datatypestr += 1;
      else
	datatypestr += 2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
401
      if ( dtype == -1 )
402
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
404
405
406
407
408
409
410
411
	  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;
412
	  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
414
	    {
	      fprintf(stderr, "Unsupported number of bits %d!\n", nbits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415
	      fprintf(stderr, "Use I8/I16/I32/F32/F64 for nc/nc2/nc4/nc4c; F32/F64 for grb2/srv/ext/ieg; P1 - P24 for grb/grb2.\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
416
417
	      exit(EXIT_FAILURE);
	    }
418
419
420
	}
      else
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
	  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
453
454
455
456
457
458
459
460
461
462
	  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);
		}
	    }
463
464
465
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
466
  if ( *datatypestr != 0 )
467
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
      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);
	}
483
484
485
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
486
487
static
void setDefaultDataTypeByte(char *datatypestr)
488
489
490
491
{
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
  int datatype = -1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
493
494
495
496
  if ( isdigit((int) *datatypestr) )
    {
      datatype = atoi(datatypestr);
      datatypestr++;

497
498
499
500
501
      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
502
503
504
      else
	{
	  fprintf(stderr, "Unsupported datatype %d!\n", datatype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
505
	  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
506
507
508
509
	  exit(EXIT_FAILURE);
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
510
  if ( *datatypestr != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
511
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
      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
527
528
529
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
530
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
532
533
534
void setDefaultFileType(char *filetypestr, int labort)
{
  if ( filetypestr )
    {
535
536
      char *ftstr = filetypestr;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
      if      ( memcmp(filetypestr, "grb2", 4)  == 0 ) { ftstr += 4; cdoDefaultFileType = FILETYPE_GRB2;}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
538
      else if ( memcmp(filetypestr, "grb1", 4)  == 0 ) { ftstr += 4; cdoDefaultFileType = FILETYPE_GRB; }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
539
540
      else if ( memcmp(filetypestr, "grb",  3)  == 0 ) { ftstr += 3; cdoDefaultFileType = FILETYPE_GRB; }
      else if ( memcmp(filetypestr, "nc2",  3)  == 0 ) { ftstr += 3; cdoDefaultFileType = FILETYPE_NC2; }
541
      else if ( memcmp(filetypestr, "nc4c", 4)  == 0 ) { ftstr += 4; cdoDefaultFileType = FILETYPE_NC4C;}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
543
544
545
546
      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
547
548
549
550
551
      else
	{
	  if ( labort )
	    {
	      fprintf(stderr, "Unsupported filetype %s!\n", filetypestr);
552
	      fprintf(stderr, "Available filetypes: grb/grb2/nc/nc2/nc4/nc4c/srv/ext/ieg\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
553
554
555
556
557
558
559
560
	      exit(EXIT_FAILURE);
	    }
	  else
	    {
	      return;
	    }
	}

561
      if ( cdoDefaultFileType != CDI_UNDEFID && *ftstr != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
562
	{
563
	  if ( *ftstr == '_' )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
	    {
565
	      ftstr++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
566

567
568
569
570
571
572
	      setDefaultDataType(ftstr);
	    }
	  else
	    {
	      fprintf(stderr, "Unexpected character >%c< in file type >%s<!\n", *ftstr, filetypestr);
	      fprintf(stderr, "Use format[_nbits] with:\n");
573
	      fprintf(stderr, "    format = grb, grb2, nc, nc2, nc4, nc4c, srv, ext or ieg\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
	      fprintf(stderr, "    nbits  = 32/64 for grb2/nc/nc2/nc4/nc4c/srv/ext/ieg; 1 - 24 for grb/grb2\n");
575
	      exit(EXIT_FAILURE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
576
577
578
579
580
	    }
	}
    }
}

581

Uwe Schulzweida's avatar
Uwe Schulzweida committed
582
583
584
585
586
int cdoFiletype(void)
{
  if ( cdoDefaultFileType == CDI_UNDEFID )
    {
      cdoDefaultFileType = FILETYPE_GRB;
587
588
      if ( ! cdoSilentMode )
	cdoPrint("Set default filetype to GRIB");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
589
590
591
592
593
    }

  return (cdoDefaultFileType);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
static
595
596
597
598
void defineCompress(const char *arg)
{
  size_t len = strlen(arg);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
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);
	}
    }

650
651
652
653
654
655
656
657
658
659
660
661
662
  cdo_file_suffix[0] = 0;

  envstr = getenv("CDO_FILE_SUFFIX");
  if ( envstr )
    {
      if ( envstr[0] )
	{
	  strncat(cdo_file_suffix, envstr, sizeof(cdo_file_suffix)-1);
	  if ( cdoVerbose )
	    fprintf(stderr, "CDO_FILE_SUFFIX = %s\n", envstr);
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
663
664
665
666
667
  envstr = getenv("CDO_DISABLE_FILESUFFIX");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
	{
668
	  strcat(cdo_file_suffix, "NULL");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
	  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);
	}
    }
}

686

Uwe Schulzweida's avatar
Uwe Schulzweida committed
687
688
689
690
691
692
693
694
695
int main(int argc, char *argv[])
{
  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
696
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
697
  int numThreads = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
698
699
700
701
702
  char *operatorName = NULL;
  char *operatorArg = NULL;
  char *argument = NULL;
  extern int dmemory_ExitOnError;

703
704
  init_is_tty();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
705
706
707
708
709
710
711
712
  dmemory_ExitOnError = 1;

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

  Progname = getProgname(argv[0]);

713
  if ( memcmp(Progname, "cdo", 3) == 0 && strlen(Progname) > 3 ) noff = 3;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
714
715

  /* old versions !!!! */
716
717
  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
718
719
720

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

721
  while ( (c = cdoGetopt(argc, argv, "f:b:e:P:p:g:i:l:m:t:D:z:aBcdhMOQRrsSTuVvXZ")) != -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
722
723
724
725
726
727
728
    {
      switch (c)
	{
	case 'a':
	  cdoDefaultTimeType = TAXIS_ABSOLUTE;
	  break;
	case 'b':
729
730
731
	  setDefaultDataType(cdoOptarg);
	  break;
	case 'B':
Uwe Schulzweida's avatar
Uwe Schulzweida committed
732
733
	  cdoBenchmark = TRUE;
	  break;
734
735
736
	case 'c':
	  cdoCheckDatarange = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
737
738
739
740
741
742
743
	case 'd':
	  Debug = 1;
	  break;
	case 'D':
	  Debug = 1;
	  DebugLevel = atoi(cdoOptarg);
	  break;
744
745
	case 'e':
	  {
746
#if defined (HAVE_GETHOSTNAME)
747
748
749
750
751
752
753
754
	  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;
755
756
757
758
#else
          fprintf(stderr, "Function gethostname not available!\n");
	  exit(EXIT_FAILURE);
#endif
759
760
          break;
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
	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;
779
780
	case 'M':
	  cdiDefGlobal("HAVE_MISSVAL", TRUE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
781
	  break;
782
783
784
	case 'O':
	  cdoOverwriteMode = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785
	case 'P':
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
787
788
789
790
	  if ( *cdoOptarg < '1' || *cdoOptarg > '9' )
	    {
	      fprintf(stderr, "Unexpected character in number of OpenMP threads (-P <nthreads>): %s!\n", cdoOptarg);
	      exit(EXIT_FAILURE);
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
791
792
	  numThreads = atoi(cdoOptarg);
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
793
	case 'p':
Uwe Schulzweida's avatar
Uwe Schulzweida committed
794
	  fprintf(stderr, "CDO option -p is obsolete and will be removed in the next release, please switch to -b <bits>!\n");
795
	  setDefaultDataTypeByte(cdoOptarg);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
	  break;
797
798
799
	case 'Q':
	  cdiDefGlobal("SORTNAME", TRUE);
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
800
801
	case 'R':
	  cdoRegulargrid = TRUE;
802
	  cdiDefGlobal("REGULARGRID", TRUE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
803
804
805
806
	  break;
	case 'r':
	  cdoDefaultTimeType = TAXIS_RELATIVE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
807
808
809
	case 'S':
	  cdoDiag = TRUE;
	  break;
810
811
812
	case 's':
	  cdoSilentMode = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
813
814
815
816
817
818
	case 'T':
	  cdoTimer = TRUE;
	  break;
	case 't':
	  cdoDefaultTableID = defineTable(cdoOptarg);
	  break;
819
820
821
	case 'u':
	  cdoInteractive = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
822
823
824
825
826
827
	case 'V':
	  Version = 1;
	  break;
	case 'v':
	  cdoVerbose = TRUE;
	  break;
828
829
830
	case 'X': /* multi threaded I/O */
	  cdoParIO = TRUE;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
831
832
833
	case 'Z':
	  cdoCompress = TRUE;
          break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
834
	case 'z':
835
836
	  defineCompress(cdoOptarg);
          break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
837
838
839
840
841
842
843
	case ':':
	  fprintf(stderr, "\nmissing parameter for one of the options\n\n");	  
	  Help = 1;
	  break;
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
844
  get_env_vars();
845

846
  if ( Debug || Version ) cdo_version();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
847
848
849

  if ( Debug )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
851
      char *envstr;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
852
853
854
855
856
857
858
859
860
      if ( DebugLevel == 0 ) DebugLevel = 1;
      cdoSetDebug(DebugLevel);
      fprintf(stderr, "\n");
      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
861
862
863
864
865
866
867
868
869
870
      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
871
872
873
874
875
#if defined (_OPENMP)
      fprintf(stderr, "Predefined: _OPENMP\n");
#endif 
#if defined (__SSE2__)
      fprintf(stderr, "Predefined: __SSE2__\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
877
878
#endif 
#if defined (__GNUC__)
      fprintf(stderr, "Predefined: __GNUC__\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
879
880
881
#endif 
      fprintf(stderr, "\n");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
882
883
884
885
886
887
888
889
#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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
#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
906
      fprintf(stderr, "FP_FAST_FMA         = defined\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
#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
931
#if defined (HAVE_GETRLIMIT)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
932
933
934
935
936
937
938
939
#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
940
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
941
942
943
944
#endif
      fprintf(stderr, "\n");
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
945
#if defined (HAVE_GETRLIMIT)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
946
947
948
949
950
#if defined (RLIMIT_STACK)
  {
#define  MIN_STACK_SIZE  67108864L  /* 64MB */
    int status;
    struct rlimit rlim;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
951
    RLIM_T min_stack_size = MIN_STACK_SIZE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
952

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
955
    if ( status == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
956
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
957
958
	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
959
	  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
961
962
963
	    rlim.rlim_cur = min_stack_size;

	    status = setrlimit(RLIMIT_STACK, &rlim);
	    if ( Debug )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
964
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
965
966
967
968
969
970
971
		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
972
973
974
975
	      }
	  }
      }
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
976
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
977
978
979
980
#endif

  if ( Debug )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
981
982
983
984
      print_pthread_info();
    }

#if defined (_OPENMP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
985
986
  if ( numThreads <= 0 ) numThreads = 1;
  omp_set_num_threads(numThreads);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
987
  ompNumThreads = omp_get_max_threads();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
988
  if ( omp_get_max_threads() > omp_get_num_procs() )
989
    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
990
991
992
993
994
995
996
997
998
  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
999
1000
#endif

For faster browsing, not all history is shown. View entire blame