cdo.cc 85.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-2018 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  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.
*/

18
#ifdef  HAVE_CONFIG_H
19
#include "config.h"
20
21
#endif

22
23
24
25
#include "module_definitions.h"
#include <iostream>
#include <vector>
#include "operator_help.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
27
28
29
#if defined (HAVE_EXECINFO_H)
#include <execinfo.h>
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
31
#include <signal.h>
#include <fenv.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
/*#include <malloc.h>*/ /* mallopt and malloc_stats */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
#include <sys/stat.h>
34
35
#ifdef  HAVE_GETRLIMIT
#ifdef  HAVE_SYS_RESOURCE_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
#include <sys/time.h>       /* getrlimit */
#include <sys/resource.h>   /* getrlimit */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
#endif
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
40
#include <unistd.h>         /* sysconf, gethostname */
41
42
#include <thread>

Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
#if defined(SX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
46
47
48
#define RLIM_T  long long
#else
#define RLIM_T  rlim_t
#endif

Ralf Mueller's avatar
Ralf Mueller committed
49
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
51
#include "cdo.h"
#include "cdo_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
#include "cdo_task.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53

Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
#include "cdo_getopt.h"
55
#include "cdoDebugOutput.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56

Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
#if defined(HAVE_LIBPTHREAD)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
59
60
61
62
#include "pstream_int.h"
#include "pthread_debug.h"
#endif

#include "modules.h"
63
#include "process.h"
64
#include "error.h"
65
#include "grid_proj.h"
66
#include "percentiles.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67

Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
#if defined(_OPENMP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
70
71
#  include <omp.h>
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
#if ! defined(VERSION)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
75
#  define  VERSION  "0.0.1"
#endif

76
#define MAX_NUM_VARNAMES 256
77

78
79
#include <string>

80
81
82
83
84
static int Debug = 0;
static int Version = 0;
static int Help = 0;
static int DebugLevel = 0;
static int numThreads = 0;
85
static int timer_total;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
87
static int CDO_netcdf_hdr_pad = 0;
static int CDO_Rusage = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88
const char *CDO_username;
89

90
91
92
93
extern "C" {
void streamGrbDefDataScanningMode(int scanmode);
}

94
void gridsearch_set_method(const char *methodstr);
95

Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
97
#define PRINT_RLIMIT(resource) \
      { \
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        int status; \
        struct rlimit rlim; \
        status = getrlimit(resource, &rlim); \
        if ( status == 0 ) \
          { \
            if ( sizeof(RLIM_T) > sizeof(long) ) \
              { \
                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
114
115
      }

116
#define ITSME  (STR_IS_EQ(CDO_username, "\x6d\x32\x31\x34\x30\x30\x33"))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117

Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
static
void cdo_stackframe(void)
{
#if defined HAVE_EXECINFO_H && defined HAVE_BACKTRACE
  void *callstack[32];
  int frames = backtrace(callstack, 32);
  char **messages = backtrace_symbols(callstack, frames);

  fprintf(stderr, "[bt] Execution path:\n");
  if ( messages ) {
    for ( int i = 0; i < frames; ++i )
      fprintf(stderr, "[bt] %s\n", messages[i]);
    free(messages);
  }
#endif
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
static
136
int cdo_feenableexcept(int excepts)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
{
138
#if defined HAVE_FEENABLEEXCEPT
139
140
  int feenableexcept(int);
  int old_excepts = feenableexcept(excepts);
141
142
  return old_excepts;
#else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
  static fenv_t fenv;
144
145
  unsigned new_excepts = ((unsigned)excepts) & FE_ALL_EXCEPT;
  int old_excepts = -1;  // previous masks
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147

  if ( fegetenv(&fenv) ) return -1;
148
149
#if defined(HAVE_FENV_T___CONTROL) && defined(HAVE_FENV_T___MXCSR)
  old_excepts = (int) (fenv.__control & FE_ALL_EXCEPT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150

151
152
153
  // unmask
  fenv.__control &= ~new_excepts;
  fenv.__mxcsr   &= ~(new_excepts << 7);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
155
156
#endif

  return ( fesetenv(&fenv) ? -1 : (int)old_excepts );
157
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
159
160
161
162
163
164
165
166
167
168
169
}

static
void cdo_sig_handler(int signo)
{
  if ( signo == SIGFPE )
    {
      cdo_stackframe();
      cdoAbort("floating-point exception!");
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
static
void cdo_set_digits(const char *optarg)
{
  char *ptr1 = 0;
  if ( optarg != 0 && (int) strlen(optarg) > 0 && optarg[0] != ',' )
    CDO_flt_digits = (int)strtol(optarg, &ptr1, 10);

  if ( CDO_flt_digits < 1 || CDO_flt_digits > 20 )
    cdoAbort("Unreasonable value for float significant digits: %d", CDO_flt_digits);

  if ( ptr1 && *ptr1 == ',' )
    {
      char *ptr2 = 0;
      CDO_dbl_digits = (int)strtol(ptr1+1, &ptr2, 10);
      if  ( ptr2 == ptr1+1 || CDO_dbl_digits < 1 || CDO_dbl_digits > 20 )
        cdoAbort("Unreasonable value for double significant digits: %d", CDO_dbl_digits);
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
190
191
static
void cdo_version(void)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
192
193
  const int   filetypes[] = {CDI_FILETYPE_SRV, CDI_FILETYPE_EXT, CDI_FILETYPE_IEG, CDI_FILETYPE_GRB, CDI_FILETYPE_GRB2, CDI_FILETYPE_NC, CDI_FILETYPE_NC2, CDI_FILETYPE_NC4, CDI_FILETYPE_NC4C, CDI_FILETYPE_NC5};
  const char* typenames[] = {        "srv",        "ext",        "ieg",       "grb1",        "grb2",       "nc1",        "nc2",        "nc4",        "nc4c",        "nc5"};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194

Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
  fprintf(stderr, "%s\n", CDO_version);
196
#if defined(USER_NAME) && defined(HOST_NAME) && defined(SYSTEM_TYPE)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
  fprintf(stderr, "Compiled: by %s on %s (%s) %s %s\n", USER_NAME, HOST_NAME, SYSTEM_TYPE, __DATE__, __TIME__);
198
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
200
201
202
203
#if defined(CXX_COMPILER)
  fprintf(stderr, "CXX Compiler: %s\n", CXX_COMPILER);
#if defined(CXX_VERSION)
  fprintf(stderr, "CXX version : %s\n", CXX_VERSION);
#endif
204
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
207
208
#if defined(C_COMPILER)
  fprintf(stderr, "C Compiler: %s\n", C_COMPILER);
#if defined(C_VERSION)
  fprintf(stderr, "C version : %s\n", C_VERSION);
209
210
211
212
213
214
215
#endif
#endif
#if defined(F77_COMPILER)
  fprintf(stderr, "F77 Compiler: %s\n", F77_COMPILER);
#if defined(F77_VERSION)
  fprintf(stderr, "F77 version : %s\n", F77_VERSION);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
217
218
219
#endif

  printFeatures();
  printLibraries();
220

Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
  fprintf(stderr, "Filetypes: ");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
  set_text_color(stderr, BRIGHT, GREEN);
223
224
  for ( size_t i = 0; i < sizeof(filetypes)/sizeof(int); ++i )
    if ( cdiHaveFiletype(filetypes[i]) ) fprintf(stderr, "%s ", typenames[i]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
  reset_text_color(stderr);
226
227
  fprintf(stderr, "\n");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
229
230
231
  cdiPrintVersion();
  fprintf(stderr, "\n");
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
void cdo_usage(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
{
235
  const char *name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236

Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
  /*  fprintf(stderr, "%s\n", CDO_version);*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
  /*  fprintf(stderr, "\n");*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
  fprintf(stderr, "usage : cdo  [Options]  Operator1  [-Operator2  [-OperatorN]]\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
241
  fprintf(stderr, "\n");
  fprintf(stderr, "  Options:\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
  set_text_color(stderr, RESET, BLUE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
  fprintf(stderr, "    -a             Generate an absolute time axis\n");
244
  fprintf(stderr, "    -b <nbits>     Set the number of bits for the output precision\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
  fprintf(stderr, "                   (I8/I16/I32/F32/F64 for nc1/nc2/nc4/nc4c/nc5; F32/F64 for grb2/srv/ext/ieg; P1 - P24 for grb1/grb2)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
  fprintf(stderr, "                   Add L or B to set the byteorder to Little or Big endian\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
  fprintf(stderr, "    --cmor         CMOR conform NetCDF output\n");
  fprintf(stderr, "    -C, --color    Colorized output messages\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
250
  fprintf(stderr, "    --enableexcept <except>\n");
  fprintf(stderr, "                   Set individual floating-point traps (DIVBYZERO, INEXACT, INVALID, OVERFLOW, UNDERFLOW, ALL_EXCEPT)\n");
251
  fprintf(stderr, "    -f, --format <format>\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
  fprintf(stderr, "                   Format of the output file. (grb1/grb2/nc1/nc2/nc4/nc4c/nc5/srv/ext/ieg)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
  fprintf(stderr, "    -g <grid>      Set default grid name or file. Available grids: \n");
254
  fprintf(stderr, "                   n<N>, t<RES>, tl<RES>, global_<DXY>, r<NX>x<NY>, g<NX>x<NY>, gme<NI>, lon=<LON>/lat=<LAT>\n");
255
  fprintf(stderr, "    -h, --help     Help information for the operators\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
256
  fprintf(stderr, "    --history      Do not append to NetCDF \"history\" global attribute\n");
257
  fprintf(stderr, "    --netcdf_hdr_pad, --hdr_pad, --header_pad <nbr>\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
  fprintf(stderr, "                   Pad NetCDF output header with nbr bytes\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
260
261
  /*
  fprintf(stderr, "    -i <inst>      Institution name/file\n");
  fprintf(stderr, "                   Predefined instituts: ");
262
  for ( int id = 0; id < institutInqNumber; id++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
263
264
265
266
267
    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
268
269
  fprintf(stderr, "    -k <chunktype> NetCDF4 chunk type: auto, grid or lines\n");
  fprintf(stderr, "    -L             Lock IO (sequential access)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
270
  fprintf(stderr, "    -M             Switch to indicate that the I/O streams have missing values\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
  fprintf(stderr, "    -m <missval>   Set the missing value of non NetCDF files (default: %g)\n", cdiInqMissval());
272
  fprintf(stderr, "    --no_warnings  Inhibit warning messages\n");
273
  fprintf(stderr, "    -O             Overwrite existing output file, if checked\n");
274
  fprintf(stderr, "    --operators    List of all operators\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
#if defined(_OPENMP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276
277
  fprintf(stderr, "    -P <nthreads>  Set number of OpenMP threads\n");
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
279
  fprintf(stderr, "    --percentile <method>\n");
  fprintf(stderr, "                   Percentile method: nrank, nist, numpy, numpy_lower, numpy_higher, numpy_nearest\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
280
281
  fprintf(stderr, "    --precision <float_digits[,double_digits]>\n");
  fprintf(stderr, "                   Precision to use in displaying floating-point data (default: 7,15)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
  fprintf(stderr, "    --reduce_dim   Reduce NetCDF dimensions\n");
283
  if ( ITSME )
284
    fprintf(stderr, "    --remap_genweights 0/1\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
  fprintf(stderr, "    -R, --regular  Convert GRIB1 data from reduced to regular grid (cgribex only)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
  fprintf(stderr, "    -r             Generate a relative time axis\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
288
  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");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
  fprintf(stderr, "    -s, --silent   Silent mode\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290
  fprintf(stderr, "    --sortname     Alphanumeric sorting of NetCDF parameter names\n");
291
  fprintf(stderr, "    -t <codetab>   Set GRIB1 default parameter code table name or file (cgribex only)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292
  fprintf(stderr, "                   Predefined tables: ");
293
  for ( int id = 0; id < tableInqNumber(); id++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295
296
297
    if ( (name = tableInqNamePtr(id)) )
      fprintf(stderr, " %s", name);
  fprintf(stderr, "\n");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
299
  fprintf(stderr, "    --timestat_date <srcdate>\n");
  fprintf(stderr, "                   Target timestamp (time statistics): first, middle, midhigh or last source timestep.\n");
300
301
  fprintf(stderr, "    -V, --version  Print the version number\n");
  fprintf(stderr, "    -v, --verbose  Print extra details for some operators\n");
302
  fprintf(stderr, "    -W             Print extra warning messages\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
  fprintf(stderr, "    -z szip        SZIP compression of GRIB1 records\n");
304
  fprintf(stderr, "       aec         AEC compression of GRIB2 records\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
  fprintf(stderr, "       jpeg        JPEG compression of GRIB2 records\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
  fprintf(stderr, "        zip[_1-9]  Deflate compression of NetCDF4 variables\n");
307
308
309
310
#ifdef HIRLAM_EXTENSIONS
  fprintf(stderr, "    --Dkext <debLev>   Setting debugLevel for extensions\n");
  fprintf(stderr, "    --outputGribDataScanningMode <mode>   Setting grib scanning mode for data in output file <0, 64, 96>; Default is 64\n");
#endif // HIRLAM_EXTENSIONS
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
  reset_text_color(stderr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
313
314
  fprintf(stderr, "\n");

  fprintf(stderr, "  Operators:\n");
315
316
  fprintf(stderr, "    Use option --operators for a list of all operators.\n");
  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
  set_text_color(stderr, RESET, GREEN);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
  operatorPrintAll();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
319
  reset_text_color(stderr);
320
  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321
322

  fprintf(stderr, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
  fprintf(stderr, "  CDO version %s, Copyright (C) 2003-2018 Uwe Schulzweida\n", VERSION);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
  //  fprintf(stderr, "  Available from <http://mpimet.mpg.de/cdo>\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
  fprintf(stderr, "  This is free software and comes with ABSOLUTELY NO WARRANTY\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
326
  fprintf(stderr, "  Report bugs to <http://mpimet.mpg.de/cdo>\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
327
328
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
329
330
331
332
333
static
void cdo_init_is_tty(void)
{
  struct stat statbuf;
  fstat(0, &statbuf);
334
  if ( S_ISCHR(statbuf.st_mode) ) stdin_is_tty = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
  fstat(1, &statbuf);
336
  if ( S_ISCHR(statbuf.st_mode) ) stdout_is_tty = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
  fstat(2, &statbuf);
338
  if ( S_ISCHR(statbuf.st_mode) ) stderr_is_tty = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
339
340
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
static
342
void cdoPrintHelp(std::vector<std::string> help/*, char *xoperator*/)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
{
344
  if (help.empty())
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345
    fprintf(stderr, "No help available for this operator!\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
347
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
      bool lprint;
349
      for(unsigned long i =  0; i < help.size(); i++)
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
350
        {
351
          lprint = !(help[i][0] == '\0'  && help[i+1][0] == ' ');
352

Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
353
354
355
356
          if ( lprint )
            {
              if ( COLOR_STDOUT )
                {
357
358
359
360
361
362
363
364
365
                  if ( (help[i].compare( "NAME")        == 0) ||
                       (help[i].compare( "SYNOPSIS")    == 0) ||
                       (help[i].compare( "DESCRIPTION") == 0) ||
                       (help[i].compare( "OPERATORS")   == 0) ||
                       (help[i].compare( "NAMELIST")    == 0) ||
                       (help[i].compare( "PARAMETER")   == 0) ||
                       (help[i].compare( "ENVIRONMENT") == 0) ||
                       (help[i].compare( "NOTE")        == 0) ||
                       (help[i].compare( "EXAMPLES")    == 0) )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
366
367
                    {
                      set_text_color(stdout, BRIGHT, BLACK);
368
                      fprintf(stdout, "%s", help[i].c_str());
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
369
370
371
372
                      reset_text_color(stdout);
                      fprintf(stdout, "\n");
                    }
                  else
373
                    fprintf(stdout, "%s\n", help[i].c_str());
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
374
375
376
                }
              else
                {
377
                  fprintf(stdout, "%s\n", help[i].c_str());
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
378
379
380
                }
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
382
383
384
385
386
    }
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
static
388
void setDefaultDataType(const char *datatypestr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
390
{
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
391
  int nbits = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
  enum {D_UINT, D_INT, D_FLT, D_CPX};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
394
  int dtype = -1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
398
399
400
  int datatype = tolower(*datatypestr);
  if      ( datatype == 'i' ) { dtype = D_INT;  datatypestr++; }
  else if ( datatype == 'u' ) { dtype = D_UINT; datatypestr++; }
  else if ( datatype == 'f' ) { dtype = D_FLT;  datatypestr++; }
  else if ( datatype == 'c' ) { dtype = D_CPX;  datatypestr++; }
  else if ( datatype == 'p' ) { datatypestr++; }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
401

402
403
  if ( isdigit((int) *datatypestr) )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
      nbits = atoi(datatypestr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
406
      datatypestr += 1;
      if ( nbits >= 10 ) datatypestr += 1;
407

Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
      if ( dtype == -1 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
409
410
411
412
        {
          if      ( nbits > 0 && nbits < 32 ) cdoDefaultDataType = nbits;
          else if ( nbits == 32 )
            {
413
              if ( cdoDefaultFileType == CDI_FILETYPE_GRB )
414
                cdoDefaultDataType = CDI_DATATYPE_PACK32;
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
415
              else
416
                cdoDefaultDataType = CDI_DATATYPE_FLT32;
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
417
            }
418
          else if ( nbits == 64 ) cdoDefaultDataType = CDI_DATATYPE_FLT64;
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
419
420
421
          else
            {
              fprintf(stderr, "Unsupported number of bits %d!\n", nbits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
              fprintf(stderr, "Use I8/I16/I32/F32/F64 for nc1/nc2/nc4/nc4c/nc5; F32/F64 for grb2/srv/ext/ieg; P1 - P24 for grb1/grb2.\n");
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
423
424
425
              exit(EXIT_FAILURE);
            }
        }
426
      else
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
427
428
429
        {
          if ( dtype == D_INT )
            {
430
431
432
              if      ( nbits ==  8 ) cdoDefaultDataType = CDI_DATATYPE_INT8;
              else if ( nbits == 16 ) cdoDefaultDataType = CDI_DATATYPE_INT16;
              else if ( nbits == 32 ) cdoDefaultDataType = CDI_DATATYPE_INT32;
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
433
434
435
436
437
438
439
440
              else
                {
                  fprintf(stderr, "Unsupported number of bits = %d for datatype INT!\n", nbits);
                  exit(EXIT_FAILURE);
                }
            }
          else if ( dtype == D_UINT )
            {
441
442
443
              if      ( nbits ==  8 ) cdoDefaultDataType = CDI_DATATYPE_UINT8;
              else if ( nbits == 16 ) cdoDefaultDataType = CDI_DATATYPE_UINT16;
              else if ( nbits == 32 ) cdoDefaultDataType = CDI_DATATYPE_UINT32;
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
444
445
446
447
448
449
450
451
              else
                {
                  fprintf(stderr, "Unsupported number of bits = %d for datatype UINT!\n", nbits);
                  exit(EXIT_FAILURE);
                }
            }
          else if ( dtype == D_FLT )
            {
452
453
              if      ( nbits == 32 ) cdoDefaultDataType = CDI_DATATYPE_FLT32;
              else if ( nbits == 64 ) cdoDefaultDataType = CDI_DATATYPE_FLT64;
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
454
455
456
457
458
459
460
461
              else
                {
                  fprintf(stderr, "Unsupported number of bits = %d for datatype FLT!\n", nbits);
                  exit(EXIT_FAILURE);
                }
            }
          else if ( dtype == D_CPX )
            {
462
463
              if      ( nbits == 32 ) cdoDefaultDataType = CDI_DATATYPE_CPX32;
              else if ( nbits == 64 ) cdoDefaultDataType = CDI_DATATYPE_CPX64;
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
464
465
466
467
468
469
470
              else
                {
                  fprintf(stderr, "Unsupported number of bits = %d for datatype CPX!\n", nbits);
                  exit(EXIT_FAILURE);
                }
            }
        }
471
472
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
  if ( *datatypestr != 0 )
474
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
475
      if ( *datatypestr == 'l' || *datatypestr == 'L' )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
476
477
478
479
        {
          if ( IsBigendian() ) cdoDefaultByteorder = CDI_LITTLEENDIAN;
          datatypestr++;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
480
      else if ( *datatypestr == 'b' || *datatypestr == 'B' )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
481
482
483
484
        {
          if ( ! IsBigendian() ) cdoDefaultByteorder = CDI_BIGENDIAN;
          datatypestr++;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
      else
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
486
487
488
489
        {
          fprintf(stderr, "Unsupported character in number of bytes: >%s< !\n", datatypestr);
          exit(EXIT_FAILURE);
        }
490
491
    }
}
492
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
494
static
void setDefaultDataTypeByte(char *datatypestr)
495
496
497
498
{
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
  int datatype = -1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
499
500
501
502
503
  if ( isdigit((int) *datatypestr) )
    {
      datatype = atoi(datatypestr);
      datatypestr++;

504
505
506
507
508
      if      ( datatype == 1 ) cdoDefaultDataType = CDI_DATATYPE_PACK8;
      else if ( datatype == 2 ) cdoDefaultDataType = CDI_DATATYPE_PACK16;
      else if ( datatype == 3 ) cdoDefaultDataType = CDI_DATATYPE_PACK24;
      else if ( datatype == 4 ) cdoDefaultDataType = CDI_DATATYPE_FLT32;
      else if ( datatype == 8 ) cdoDefaultDataType = CDI_DATATYPE_FLT64;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
      else
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
510
511
        {
          fprintf(stderr, "Unsupported datatype %d!\n", datatype);
512
          fprintf(stderr, "Use 4/8 for filetype nc/srv/ext/ieg and 1/2/3 for grb1/grb2.\n");
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
513
514
          exit(EXIT_FAILURE);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
515
516
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
  if ( *datatypestr != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
518
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
      if ( *datatypestr == 'l' || *datatypestr == 'L' )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
520
521
        {
          if ( IsBigendian() ) cdoDefaultByteorder = CDI_LITTLEENDIAN;
522
          datatypestr++;setDefaultDataTypeByte
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
523
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
524
      else if ( *datatypestr == 'b' || *datatypestr == 'B' )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
525
526
527
528
        {
          if ( ! IsBigendian() ) cdoDefaultByteorder = CDI_BIGENDIAN;
          datatypestr++;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
      else
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
530
531
532
533
        {
          fprintf(stderr, "Unsupported character in number of bytes: %s!\n", datatypestr);
          exit(EXIT_FAILURE);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
534
535
    }
}
536
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
static
538
void setDefaultFileType(const char *filetypestr, int labort)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
539
540
541
{
  if ( filetypestr )
    {
542
      const char *ftstr = filetypestr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
544
      size_t len;

545
      // clang-format off
546
547
548
549
550
551
      if      ( cmpstrlen(filetypestr, "grb2", len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_GRB2;}
      else if ( cmpstrlen(filetypestr, "grb1", len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_GRB; }
      else if ( cmpstrlen(filetypestr, "grb",  len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_GRB; }
      else if ( cmpstrlen(filetypestr, "nc2",  len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_NC2; }
      else if ( cmpstrlen(filetypestr, "nc4c", len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_NC4C;}
      else if ( cmpstrlen(filetypestr, "nc4",  len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_NC4; }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
      else if ( cmpstrlen(filetypestr, "nc5",  len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_NC5; }
553
554
555
556
557
      else if ( cmpstrlen(filetypestr, "nc1",  len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_NC;  }
      else if ( cmpstrlen(filetypestr, "nc",   len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_NC2; }
      else if ( cmpstrlen(filetypestr, "srv",  len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_SRV; }
      else if ( cmpstrlen(filetypestr, "ext",  len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_EXT; }
      else if ( cmpstrlen(filetypestr, "ieg",  len)  == 0 ) { ftstr += len; cdoDefaultFileType = CDI_FILETYPE_IEG; }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
558
      else
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
559
560
561
562
        {
          if ( labort )
            {
              fprintf(stderr, "Unsupported filetype %s!\n", filetypestr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
563
              fprintf(stderr, "Available filetypes: grb1/grb2/nc1/nc2/nc4/nc4c/nc5/srv/ext/ieg\n");
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
564
565
566
567
568
569
570
              exit(EXIT_FAILURE);
            }
          else
            {
              return;
            }
        }
571
      // clang-format on
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572

573
      if ( cdoDefaultFileType != CDI_UNDEFID && *ftstr != 0 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
574
575
576
        {
          if ( *ftstr == '_' )
            {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
577
              setDefaultDataType(++ftstr);
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
578
579
580
581
582
            }
          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
583
584
              fprintf(stderr, "    format = grb1, grb2, nc1, nc2, nc4, nc4c, nc5, srv, ext or ieg\n");
              fprintf(stderr, "    nbits  = 32/64 for grb2/nc1/nc2/nc4/nc4c/nc5/srv/ext/ieg; 1 - 24 for grb1/grb2\n");
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
585
586
587
              exit(EXIT_FAILURE);
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
589
590
    }
}

591
#define NTESTS 11
592
#include <inttypes.h>
593
594
595
596
597
598
599
600
601
602
static
int getMemAlignment(void)
{
  int ma = -1;
  double *ptr[NTESTS];
  int64_t iptr;
  size_t tsize[NTESTS] = {1, 3, 5, 9, 17, 33, 69, 121, 251, 510, 1025};
  size_t ma_check[4] = {8, 16, 32, 64};
  int ma_result[4] = {1, 1, 1, 1};

Uwe Schulzweida's avatar
Uwe Schulzweida committed
603
  for ( int i = 0; i < NTESTS; ++i )
604
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
605
      ptr[i] = (double*) malloc(tsize[i]);
606
      iptr = (int64_t) ptr[i];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
      for ( int k = 0; k < 4; ++k ) if ( iptr%ma_check[k] ) ma_result[k] = 0; 
608
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
609
  for ( int i = 0; i < NTESTS; ++i ) free(ptr[i]);
610

Uwe Schulzweida's avatar
Uwe Schulzweida committed
611
  for ( int i = NTESTS-1; i >= 0; i-- )
612
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
613
      ptr[i] = (double*) malloc(tsize[i]+5);
614
      iptr = (int64_t) ptr[i];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
615
      for ( int k = 0; k < 4; ++k ) if ( iptr%ma_check[k] ) ma_result[k] = 0; 
616
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
617
  for ( int i = 0; i < NTESTS; ++i ) free(ptr[i]);
618

Uwe Schulzweida's avatar
Uwe Schulzweida committed
619
  for ( int k = 0; k < 4; ++k ) if ( ma_result[k] ) ma = ma_check[k];
620

Uwe Schulzweida's avatar
Uwe Schulzweida committed
621
  return ma;
622
623
}

624

Uwe Schulzweida's avatar
Uwe Schulzweida committed
625
static
626
627
628
629
void defineCompress(const char *arg)
{
  size_t len = strlen(arg);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
630
  if      ( strncmp(arg, "szip", len) == 0 )
631
    {
632
      cdoCompType  = CDI_COMPRESS_SZIP;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633
      cdoCompLevel = 0;
634
    }
635
  else if ( strncmp(arg, "aec", len) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
    {
637
      cdoCompType = CDI_COMPRESS_AEC;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
638
      cdoCompLevel = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
639
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
  else if ( strncmp(arg, "jpeg", len) == 0 )
641
    {
642
      cdoCompType = CDI_COMPRESS_JPEG;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
643
      cdoCompLevel = 0;
644
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
645
  else if ( strncmp(arg, "zip", 3) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646
    {
647
      cdoCompType  = CDI_COMPRESS_ZIP;
648
      if ( len == 5 && arg[3] == '_' && isdigit(arg[4]) )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
649
        cdoCompLevel = atoi(&arg[4]);
650
      else
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
651
        cdoCompLevel = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
652
    }
653
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
654
655
656
657
658
659
660
661
662
    {
      fprintf(stderr, "Compression type '%s' unsupported!\n", arg);
      exit(EXIT_FAILURE);
    }
}

static
void defineChunktype(const char *arg)
{
663
664
665
  if      ( STR_IS_EQ("auto",  arg) ) cdoChunkType = CDI_CHUNK_AUTO;
  else if ( STR_IS_EQ("grid",  arg) ) cdoChunkType = CDI_CHUNK_GRID;
  else if ( STR_IS_EQ("lines", arg) ) cdoChunkType = CDI_CHUNK_LINES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
666
667
668
669
670
  else
    {
      fprintf(stderr, "Chunk type '%s' unsupported!\n", arg);
      exit(EXIT_FAILURE);
    }
671
672
}

673
674
675
676
677
678
679
680
681
682
static
void defineVarnames(const char *arg)
{
  size_t len = strlen(arg);
  size_t istart = 0;
  while ( istart < len && (arg[istart] == ' ' || arg[istart] == ',') ) istart++;

  len -= istart;

  if ( len )
683
    {      
684
      cdoVarnames = (char **) Malloc(MAX_NUM_VARNAMES*sizeof(char *));
685

686
      char *pbuf = strdup(arg+istart);
687
688
      cdoVarnames[cdoNumVarnames++] = pbuf;    

689
      char *commapos = pbuf;
690
      while ( (commapos = strchr(commapos, ',')) != NULL )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
691
692
693
694
695
696
697
698
699
700
        {
          *commapos++ = '\0';
          if ( strlen(commapos) )
            {
              if ( cdoNumVarnames >= MAX_NUM_VARNAMES )
                cdoAbort("Too many variable names (limit=%d)!", MAX_NUM_VARNAMES);

              cdoVarnames[cdoNumVarnames++] = commapos;
            }
        }
701
702
      /*
      for ( int i = 0; i < cdoNumVarnames; ++i )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
703
        printf("varname %d: %s\n", i+1, cdoVarnames[i]);
704
705
706
707
      */
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
708
709
710
static
void get_env_vars(void)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
711
712
  CDO_username = getenv("LOGNAME");
  if ( CDO_username == NULL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
713
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
714
715
      CDO_username = getenv("USER");
      if ( CDO_username == NULL ) CDO_username = "unknown";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
717
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
718
  char *envstr = getenv("CDO_GRID_SEARCH_DIR");
719
720
721
722
  if ( envstr )
    {
      size_t len = strlen(envstr);
      if ( len > 0 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
723
724
        {
          len += 2;
725
          cdoGridSearchDir = (char*) Malloc(len);
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
726
727
728
729
730
731
732
          memcpy(cdoGridSearchDir, envstr, len-1);
          if ( cdoGridSearchDir[len-3] != '/' )
            {
              cdoGridSearchDir[len-2] = '/';
              cdoGridSearchDir[len-1] = 0;
            }
        }
733
734
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
735
736
737
738
  envstr = getenv("CDO_DISABLE_HISTORY");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
739
740
741
742
743
        {
          CDO_Reset_History = TRUE;
          if ( cdoVerbose )
            fprintf(stderr, "CDO_DISABLE_HISTORY = %s\n", envstr);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
744
745
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
746
747
748
749
  envstr = getenv("CDO_RESET_HISTORY");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
750
751
752
753
754
        {
          CDO_Reset_History = TRUE;
          if ( cdoVerbose )
            fprintf(stderr, "CDO_RESET_HISTORY = %s\n", envstr);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
755
756
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
757
758
759
760
761
762
763
764
765
766
767
768
  envstr = getenv("CDO_HISTORY_INFO");
  if ( envstr )
    {
      int ival = atoi(envstr);
      if ( ival == 0 || ival == 1 )
        {
          CDO_Append_History = ival;
          if ( cdoVerbose )
            fprintf(stderr, "CDO_HISTORY_INFO = %s\n", envstr);
        }
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
  CDO_File_Suffix[0] = 0;
770
771
772
773
774

  envstr = getenv("CDO_FILE_SUFFIX");
  if ( envstr )
    {
      if ( envstr[0] )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
775
776
777
778
779
        {
          strncat(CDO_File_Suffix, envstr, sizeof(CDO_File_Suffix)-1);
          if ( cdoVerbose )
            fprintf(stderr, "CDO_FILE_SUFFIX = %s\n", envstr);
        }
780
781
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
782
783
784
785
  envstr = getenv("CDO_DISABLE_FILESUFFIX");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
786
787
788
789
790
        {
          strcat(CDO_File_Suffix, "NULL");
          if ( cdoVerbose )
            fprintf(stderr, "CDO_DISABLE_FILESUFFIX = %s\n", envstr);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
791
792
793
794
795
796
    }

  envstr = getenv("CDO_DIAG");
  if ( envstr )
    {
      if ( atoi(envstr) == 1 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
797
798
799
800
801
        {
          cdoDiag = TRUE;
          if ( cdoVerbose )
            fprintf(stderr, "CDO_DIAG = %s\n", envstr);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
802
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
803

804
805
806
807
808
  envstr = getenv("CDO_USE_FFTW");
  if ( envstr )
    {
      int ival = atoi(envstr);
      if ( ival == 0 || ival == 1 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
809
810
811
812
813
        {
          CDO_Use_FFTW = ival;
          if ( cdoVerbose )
            fprintf(stderr, "CDO_Use_FFTW = %s\n", envstr);
        }
814
815
    }

816
817
818
819
820
821
822
823
824
825
826
827
  envstr = getenv("CDO_VERSION_INFO");
  if ( envstr )
    {
      int ival = atoi(envstr);
      if ( ival == 0 || ival == 1 )
        {
          CDO_Version_Info = ival;
          if ( cdoVerbose )
            fprintf(stderr, "CDO_Version_Info = %s\n", envstr);
        }
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
829
830
  envstr = getenv("CDO_COLOR");
  if ( envstr )
    {
831
832
      int ival = atoi(envstr);
      if ( ival == 0 || ival == 1 )
Yvonne Kuestermann's avatar
Yvonne Kuestermann committed
833
834
835
836
837
        {
          CDO_Color = ival;
          if ( cdoVerbose )
            fprintf(stderr, "CDO_COLOR = %s\n", envstr);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
838
    }
839
  else if ( CDO_Color == FALSE && ITSME ) CDO_Color = TRUE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
840
841
}

842
843
static
void print_system_info()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
844
{
845
  fprintf(stderr, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
846
847
848
  fprintf(stderr, "CDO_Color           = %d\n", CDO_Color);
  fprintf(stderr, "CDO_Reset_History   = %d\n", CDO_Reset_History);
  fprintf(stderr, "CDO_File_Suffix     = %s\n", CDO_File_Suffix);
849
850
851
852
853
  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");
854

855
  const char *envstr;
856
857
858
859
860
861
862
863
864
  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
865

866
867
#if defined(_ARCH_PWR6)
  fprintf(stderr, "Predefined: _ARCH_PWR6\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
868
#elif defined(_ARCH_PWR7)
869
  fprintf(stderr, "Predefined: _ARCH_PWR7\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
870
871
872
873
874
#endif

#if defined(__AVX2__)
  fprintf(stderr, "Predefined: __AVX2__\n");
#elif defined(__AVX__)
875
  fprintf(stderr, "Predefined: __AVX__\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
877
878
879
880
881
882
883
#elif defined(__SSE4_2__)
  fprintf(stderr, "Predefined: __SSE4_2__\n");
#elif defined(__SSE4_1__)
  fprintf(stderr, "Predefined: __SSE4_1__\n");
#elif defined(__SSE3__)
  fprintf(stderr, "Predefined: __SSE3__\n");
#elif defined(__SSE2__)
  fprintf(stderr, "Predefined: __SSE2__\n");
884
885
#endif 
  fprintf(stderr, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
886

887
  fprintf(stderr, "sizeof(size_t)      = %zu\n", sizeof(size_t));
888
  fprintf(stderr, "mem alignment       = %d\n\n", getMemAlignment());
Uwe Schulzweida's avatar
Uwe Schulzweida committed
889

Uwe Schulzweida's avatar
Uwe Schulzweida committed
890
#if defined(HAVE_MMAP)
891
892
  fprintf(stderr, "HAVE_MMAP\n");
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
893
#if defined(HAVE_MEMORY_H)
894
895
896
  fprintf(stderr, "HAVE_MEMORY_H\n");
#endif
  fprintf(stderr, "\n");
897

Uwe Schulzweida's avatar
Uwe Schulzweida committed
898
#if defined(_OPENACC)
899
900
  fprintf(stderr, "OPENACC VERSION     = %d\n", _OPENACC);
#endif
901
902
  /* OPENMP 3:  201107 */
  /* OPENMP 4:  201307 gcc 4.9 */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
903
#if defined(_OPENMP)
904
905
  fprintf(stderr, "OPENMP VERSION      = %d\n", _OPENMP);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
906
907
908
#if defined(__cplusplus)
  fprintf(stderr, "__cplusplus         = %ld\n", __cplusplus);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
#if defined(__GNUC__)
910
911
  fprintf(stderr, "GNUC VERSION        = %d\n", __GNUC__);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
912
#if defined(__GNUC_MINOR__)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
913
914
  fprintf(stderr, "GNUC MINOR          = %d\n", __GNUC_MINOR__);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
915
#if defined(__ICC)
916
917
  fprintf(stderr, "ICC VERSION         = %d\n", __ICC);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
918
#if defined(__STDC__)
919
920
  fprintf(stderr, "STD ANSI C          = %d\n", __STDC__);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
921
#if defined(__STD_VERSION__)
922
923
  fprintf(stderr, "STD VERSION         = %ld\n", __STD_VERSION__);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
924
#if defined(__STDC_VERSION__)
925
926
  fprintf(stderr, "STDC VERSION        = %ld\n", __STDC_VERSION__);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
927
#if defined(__STD_HOSTED__)
928
929
  fprintf(stderr, "STD HOSTED          = %d\n", __STD_HOSTED__);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
930
#if defined(FLT_EVAL_METHOD)
931
932
  fprintf(stderr, "FLT_EVAL_METHOD     = %d\n", FLT_EVAL_METHOD);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
933
#if defined(FP_FAST_FMA)
934
  fprintf(stderr, "FP_FAST_FMA         = defined\n");
935
936
937
#endif
#if defined(__FAST_MATH__)
  fprintf(stderr, "__FAST_MATH__       = defined\n");
938
939
940
#endif
  fprintf(stderr, "\n");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
941
#if defined(_SC_VERSION)
942
943
  fprintf(stderr, "POSIX.1 VERSION     = %ld\n", sysconf(_SC_VERSION));
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
944
#if defined(_SC_ARG_MAX)
945
946
  fprintf(stderr, "POSIX.1 ARG_MAX     = %ld\n", sysconf(_SC_ARG_MAX));
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
947
#if defined(_SC_CHILD_MAX)
948
949
  fprintf(stderr, "POSIX.1 CHILD_MAX   = %ld\n", sysconf(_SC_CHILD_MAX));
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
950
#if defined(_SC_STREAM_MAX)
951
952
  fprintf(stderr, "POSIX.1 STREAM_MAX  = %ld\n", sysconf(_SC_STREAM_MAX));
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
953
#if defined(_SC_OPEN_MAX)
954
955
  fprintf(stderr, "POSIX.1 OPEN_MAX    = %ld\n", sysconf(_SC_OPEN_MAX));
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
956
#if defined(_SC_PAGESIZE)
957
958
  fprintf(stderr, "POSIX.1 PAGESIZE    = %ld\n", sysconf(_SC_PAGESIZE));
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
959

960
  fprintf(stderr, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
961

Uwe Schulzweida's avatar
Uwe Schulzweida committed
962
963
#if defined(HAVE_GETRLIMIT)
#if defined(RLIMIT_FSIZE)
964
965
  PRINT_RLIMIT(RLIMIT_FSIZE);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
966
#if defined(RLIMIT_NOFILE)
967
968
  PRINT_RLIMIT(RLIMIT_NOFILE);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
969
#if defined(RLIMIT_STACK)
970
971