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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2017 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.
*/

Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
#if defined(HAVE_CONFIG_H)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
20
21
#  include "config.h"
#endif

22
#include <time.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23

Ralf Mueller's avatar
Ralf Mueller committed
24
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
28
29
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"


Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
#if defined(HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
32
33
34
#  include "netcdf.h"
#endif

static int nvars_ml = 4;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
static const char strfiletype_ml[]  = "Initial file spectral";
static const char strfiletype_res[] = "Restart history file";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


typedef struct {
  int  gridtype;
  int  zaxistype;
  int  code;
  char *name;
  char *longname;
  char *units;
  int gridID;
  int zaxisID;
  int gridsize;
  int nlev;
  double *ptr;
} VAR;


Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
55
56
57
58
59
60
61
62
63
64
65
typedef struct {
  int    naint;
  int    naflt;
  int    natxt;
  char   *aintname[1024];
  int    *aintentry[1024];
  char   *afltname[1024];
  double *afltentry[1024];
  char   *atxtname[1024];
  char   *atxtentry[1024];
} ATTS;

66
67
static
void iniatts(ATTS *atts)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
69
70
71
72
73
{
  atts->naint = 0;
  atts->naflt = 0;
  atts->natxt = 0;
}

74
75
static
void inivar(VAR *var, int gridtype, int zaxistype, int code, const char *name,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
	       const char *longname, const char *units)
77
{  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
80
  var->gridtype  = gridtype;
  var->zaxistype = zaxistype;
  var->code      = code;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
82
83
84
85
86
  var->name      = NULL;
  var->longname  = NULL;
  var->units     = NULL;
  if ( name )     var->name      = strdup(name);
  if ( longname ) var->longname  = strdup(longname);
  if ( units )    var->units     = strdup(units);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
88
}

89
90
static
void inivars_ml(VAR **vars)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
{
92
  *vars = (VAR*) Malloc((nvars_ml+1)*sizeof(VAR));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93

Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
97
  inivar(&(*vars)[0], GRID_GAUSSIAN, ZAXIS_HYBRID,  133, "Q",   "specific humidity", "kg/kg");
  inivar(&(*vars)[1], GRID_SPECTRAL, ZAXIS_HYBRID,  138, "SVO", "vorticity", "1/s");
  inivar(&(*vars)[2], GRID_SPECTRAL, ZAXIS_HYBRID,  155, "SD",  "divergence", "1/s");
  inivar(&(*vars)[3], GRID_SPECTRAL, ZAXIS_HYBRID,  130, "STP", "temperature", "K");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
  /* Don't change the order (lsp must be the last one)! */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
  inivar(&(*vars)[4], GRID_SPECTRAL, ZAXIS_SURFACE, 152, "LSP", "log surface pressure", "");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
101
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
102
#if defined(HAVE_LIBNETCDF)
103
104
static
void nce(int istat)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
106
{
  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
    This routine provides a simple interface to NetCDF error message routine.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
109
110
111
112
113
  */

  if ( istat != NC_NOERR ) cdoAbort(nc_strerror(istat));
}
#endif

114
115
static
int import_e5ml(const char *filename, VAR **vars)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
  int nvars = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
#if defined(HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
120
121
122
123
124
125
  int nc_dim_id, nc_var_id;
  size_t dimlen, nvals;
  size_t start[3];
  size_t count[3];
  int nlon, nlat, nlev, nlevp1, nvct, nsp, i, iv;
  int gridIDgp, gridIDsp, zaxisIDml, zaxisIDsfc;
  int gridtype, zaxistype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
128
  int nc_file_id;
  char filetype[256];
  size_t attlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130
  double *xvals, *yvals, *vct, *levs;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
  /* open file and check file type */
132
  /* nce(nc_open(filename, NC_NOWRITE, &nc_file_id)); */
133
  nc_file_id = cdf_openread(filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
136
137
138

  nce(nc_get_att_text(nc_file_id, NC_GLOBAL, "file_type", filetype));
  nce(nc_inq_attlen(nc_file_id, NC_GLOBAL, "file_type", &attlen));
  filetype[attlen] = 0;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
  if ( strcmp(filetype, strfiletype_ml) != 0 ) return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
141
142

  inivars_ml(vars);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  /* read dimensions */

  nce(nc_inq_dimid(nc_file_id, "lon", &nc_dim_id));
  nce(nc_inq_dimlen(nc_file_id, nc_dim_id, &dimlen));
  nlon = (int) dimlen;

  nce(nc_inq_dimid(nc_file_id, "lat", &nc_dim_id));
  nce(nc_inq_dimlen(nc_file_id, nc_dim_id, &dimlen));
  nlat = (int) dimlen;

  gridIDgp = gridCreate(GRID_GAUSSIAN, nlon*nlat);
  gridDefXsize(gridIDgp, nlon);
  gridDefYsize(gridIDgp, nlat);

  nce(nc_inq_dimid(nc_file_id, "nsp", &nc_dim_id));
  nce(nc_inq_dimlen(nc_file_id, nc_dim_id, &dimlen));
  nsp = (int) dimlen;

  gridIDsp = gridCreate(GRID_SPECTRAL, nsp*2);
162
  gridDefComplexPacking(gridIDsp, 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
166
167
168
169
170
171
172

  nce(nc_inq_dimid(nc_file_id, "nlev", &nc_dim_id));
  nce(nc_inq_dimlen(nc_file_id, nc_dim_id, &dimlen));
  nlev = (int) dimlen;
  nlevp1 = nlev + 1;
  nvct = nlevp1*2;

  zaxisIDsfc = zaxisCreate(ZAXIS_SURFACE, 1);
  zaxisIDml  = zaxisCreate(ZAXIS_HYBRID, nlev);

173
  levs = (double*) Malloc(nlev*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
175
  for ( i = 0; i < nlev; i++ ) levs[i] = i+1;
  zaxisDefLevels(zaxisIDml, levs);
176
  Free(levs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
178
179

  /* read variables */

180
181
  xvals = (double*) Malloc(nlon*sizeof(double));
  yvals = (double*) Malloc(nlat*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
184
185
186
187
188
189
190
191

  nce(nc_inq_varid(nc_file_id, "lon", &nc_var_id));
  nce(nc_get_var_double(nc_file_id, nc_var_id, xvals));

  nce(nc_inq_varid(nc_file_id, "lat", &nc_var_id));
  nce(nc_get_var_double(nc_file_id, nc_var_id, yvals));

  gridDefXvals(gridIDgp, xvals);
  gridDefYvals(gridIDgp, yvals);

192
193
  Free(xvals);
  Free(yvals);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194

195
  vct   = (double*) Malloc(nvct*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
197
198
199
200
201
202
203

  nce(nc_inq_varid(nc_file_id, "vct_a", &nc_var_id));
  nce(nc_get_var_double(nc_file_id, nc_var_id, vct));

  nce(nc_inq_varid(nc_file_id, "vct_b", &nc_var_id));
  nce(nc_get_var_double(nc_file_id, nc_var_id, vct+nlevp1));

  zaxisDefVct(zaxisIDml, 2*nlevp1, vct);
204
  Free(vct);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
207
208
209
210
211
212

  for ( iv = 0; iv < nvars_ml; iv++ )
    {
      nvals = 0;

      gridtype  = (*vars)[iv].gridtype;
      zaxistype = (*vars)[iv].zaxistype;

213
214
      UNUSED(zaxistype);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
      if ( gridtype == GRID_GAUSSIAN )
	{
	  (*vars)[iv].gridID = gridIDgp;
	  nvals += nlon*nlat;
	}
      else
	{
	  (*vars)[iv].gridID = gridIDsp;
	  nvals += nsp*2;
	}

      (*vars)[iv].zaxisID   = zaxisIDml;
      (*vars)[iv].gridsize  = nvals;
      (*vars)[iv].nlev      = nlev;

230
      (*vars)[iv].ptr = (double*) Malloc(nlev*nvals*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
231
232
233
234
235
236
237
238
239
240
      
      for ( i = 0; i < nlev; i++ )
	{
	  if ( gridtype == GRID_GAUSSIAN )
	    {
	      start[0] = 0;     start[1] = i;  start[2] = 0;
	      count[0] = nlat;  count[1] = 1;  count[2] = nlon;     
	    }
	  else
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
242
	      start[0] = 0;     start[1] = 0;  start[2] = i;
	      count[0] = nsp;   count[1] = 2;  count[2] = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
	    }

	  nce(nc_inq_varid(nc_file_id, (*vars)[iv].name, &nc_var_id));
	  nce(nc_get_vara_double(nc_file_id, nc_var_id, start, count, (*vars)[iv].ptr+i*nvals));
	}
    }

  /* read lsp */

  (*vars)[nvars_ml].gridID    = gridIDsp;
  (*vars)[nvars_ml].zaxisID   = zaxisIDsfc;
  (*vars)[nvars_ml].gridsize  = nsp*2;
  (*vars)[nvars_ml].nlev      = 1;

  start[0] = 0;    start[1] = 0;  start[2] = nlev;
  count[0] = nsp;  count[1] = 2;  count[2] = 1;

260
  (*vars)[nvars_ml].ptr = (double*) Malloc(nsp*2*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
262
263
264

  nce(nc_inq_varid(nc_file_id, "STP", &nc_var_id));
  nce(nc_get_vara_double(nc_file_id, nc_var_id, start, count, (*vars)[nvars_ml].ptr));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
267
268
269
270
  /*close input file */
  nce(nc_close(nc_file_id));

  nvars = nvars_ml + 1;

#else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
  cdoAbort("NetCDF support not compiled in!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
273
#endif

274
  return nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276

277
278
static
void export_e5ml(const char *filename, VAR *vars, int nvars, int vdate, int vtime, int ntr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
280
#if defined(HAVE_LIBNETCDF)
281
282
  int nc_var_id;
  size_t nvals;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283
284
  size_t start[3], count[3];
  int dimidsp[9];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
  int varid, code;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
287
  int ilev;
  int lon, lat;
288
  int nlon, nlat, nlev, nlevp1/*, nvct*/, nsp, n2, i, nvclev;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
  int lat_dimid, lon_dimid, nlev_dimid, nlevp1_dimid, nsp_dimid, nvclev_dimid, n2_dimid;
290
  int gridIDgp = -1, gridIDsp, zaxisIDml = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
291
292
293
  int gridtype, zaxistype;
  int nc_file_id;
  int nc_stpid, lspid;
294
  double *xvals, *yvals;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
295
  const double *vct;
296
297
  char atttext[1024];
  size_t attlen;
298
  //int attint;
299
  const char *username;
300
301
302
  char timestr[30];
  time_t date_and_time_in_sec;
  struct tm *date_and_time;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
304
  int writemode = NC_CLOBBER;
  unsigned long  data_size;
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  
  date_and_time_in_sec = time(NULL);
  timestr[0] = 0;

  if ( date_and_time_in_sec != -1 )
    {
      date_and_time = localtime(&date_and_time_in_sec);
      (void) strftime(timestr, sizeof(timestr), "%d/%m/%Y %H:%M", date_and_time);
    }

  username = getenv("LOGNAME");
  if ( username == NULL )
    {
      username = getenv("USER");
      if ( username == NULL ) username = "unknown";
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321

Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

  n2 = 2;

  lon = 0; lat = 0; nsp = 0; nlev = 0; nlevp1 = 0; nvclev = 0;
  for ( varid = 0; varid < nvars; ++varid )
    {
      gridtype  = vars[varid].gridtype;
      zaxistype = vars[varid].zaxistype;

      if ( gridtype == GRID_GAUSSIAN && lat == 0 )
	{
	  gridIDgp = vars[varid].gridID;
	  lon = gridInqXsize(vars[varid].gridID);
	  lat = gridInqYsize(vars[varid].gridID);
	}
      else if ( gridtype == GRID_SPECTRAL && nsp == 0 )
	{
	  gridIDsp = vars[varid].gridID;
340
	  UNUSED(gridIDsp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
	  nsp = gridInqSize(vars[varid].gridID);
	  nsp = nsp/2;
	}

      if ( zaxistype == ZAXIS_HYBRID && nlev == 0 )
	{
	  zaxisIDml = vars[varid].zaxisID;
	  nlev = zaxisInqSize(vars[varid].zaxisID);
	  nlevp1 = nlev + 1;
	  nvclev = nlev + 1;
	}
    }

  if ( lat  == 0 ) cdoAbort("Gaussian grid not found!");
  if ( nsp  == 0 ) cdoAbort("Spectral data not found!");
  if ( nlev == 0 ) cdoAbort("Hybrid level not found!");

  nlon = lon;
  nlat = lat;


  data_size = nlon+nlat + 2*nvclev + 2*nsp*2*nlev + nsp*2*nlevp1 + nlon*nlat*nlev;

  if ( data_size*8 > 2147000000 )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
#if defined(NC_64BIT_OFFSET)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
368
369
370
371
372
      writemode = NC_CLOBBER | NC_64BIT_OFFSET;
#else
      cdoWarning("Datasize > 2GB and NC_64BIT_OFFSET not available!");
#endif
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
  /* create file */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
  nce(nc_create(filename, writemode, &nc_file_id));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
375

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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
  strcpy(atttext, "IEEE");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "source_type", attlen, atttext));

  strcpy(atttext, commandLine());
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "history", attlen, atttext));

  strcpy(atttext, username);
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "user", attlen, atttext));

  strcpy(atttext, timestr);
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "created", attlen, atttext));

  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "label_1", attlen, atttext));

  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "label_2", attlen, atttext));

  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "label_3", attlen, atttext));

  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "label_4", attlen, atttext));

  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "label_5", attlen, atttext));

  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "label_6", attlen, atttext));

  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "label_7", attlen, atttext));

  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "label_8", attlen, atttext));

  nce(nc_put_att_int(nc_file_id, NC_GLOBAL, "fdate", NC_INT, 1, &vdate));
  nce(nc_put_att_int(nc_file_id, NC_GLOBAL, "ftime", NC_INT, 1, &vtime));

  nce(nc_put_att_int(nc_file_id, NC_GLOBAL, "vdate", NC_INT, 1, &vdate));
  nce(nc_put_att_int(nc_file_id, NC_GLOBAL, "vtime", NC_INT, 1, &vtime));

430
  //attint = 31;
431
432
433
434
  nce(nc_put_att_int(nc_file_id, NC_GLOBAL, "spherical_truncation_n", NC_INT, 1, &ntr));
  nce(nc_put_att_int(nc_file_id, NC_GLOBAL, "spherical_truncation_m", NC_INT, 1, &ntr));
  nce(nc_put_att_int(nc_file_id, NC_GLOBAL, "spherical_truncation_k", NC_INT, 1, &ntr));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "file_type", strlen(strfiletype_ml), strfiletype_ml));

  
  nce(nc_def_dim(nc_file_id, "lat", lat, &lat_dimid));

  nce(nc_def_dim(nc_file_id, "lon", lon, &lon_dimid));

  nce(nc_def_dim(nc_file_id, "nlev", nlev, &nlev_dimid));
  nce(nc_def_dim(nc_file_id, "nlevp1", nlevp1, &nlevp1_dimid));

  nce(nc_def_dim(nc_file_id, "nsp", nsp, &nsp_dimid));

  nce(nc_def_dim(nc_file_id, "nvclev", nvclev, &nvclev_dimid));

  nce(nc_def_dim(nc_file_id, "n2", n2, &n2_dimid));

  nce(nc_enddef(nc_file_id));

  /* define gaussian grid */

455
456
  xvals = (double*) Malloc(nlon*sizeof(double));
  yvals = (double*) Malloc(nlat*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
457
458
459
460
461
462

  gridInqXvals(gridIDgp, xvals);
  gridInqYvals(gridIDgp, yvals);

  nce(nc_redef(nc_file_id));
  nce(nc_def_var(nc_file_id, "lat", NC_DOUBLE, 1, &lat_dimid, &nc_var_id));
463
464
465
466
467
468
  strcpy(atttext, "Gaussian latitude");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, nc_var_id, "long_name", attlen, atttext));
  strcpy(atttext, "degrees_N");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, nc_var_id, "units", attlen, atttext));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
469
470
471
472
473
  nce(nc_enddef(nc_file_id));
  nce(nc_put_var_double(nc_file_id, nc_var_id, yvals));
   
  nce(nc_redef(nc_file_id));
  nce(nc_def_var(nc_file_id, "lon", NC_DOUBLE, 1, &lon_dimid, &nc_var_id));
474
475
476
477
478
479
  strcpy(atttext, "longitude");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, nc_var_id, "long_name", attlen, atttext));
  strcpy(atttext, "degrees_E");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, nc_var_id, "units", attlen, atttext));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
480
481
482
  nce(nc_enddef(nc_file_id));
  nce(nc_put_var_double(nc_file_id, nc_var_id, xvals));

483
484
  Free(xvals);
  Free(yvals);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
487

  /* define model level */

488
  // nvct = nvclev*2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
489

490
  /* vct   = (double*) Malloc(nvct*sizeof(double)); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
491
492
493
494
495

  vct = zaxisInqVctPtr(zaxisIDml);

  nce(nc_redef(nc_file_id));
  nce(nc_def_var(nc_file_id, "vct_a", NC_DOUBLE, 1, &nvclev_dimid, &nc_var_id));
496
497
498
499
500
501
  strcpy(atttext, "vertical-coordinate parameter set A");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, nc_var_id, "long_name", attlen, atttext));
  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, nc_var_id, "units", attlen, atttext));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
503
504
505
506
  nce(nc_enddef(nc_file_id));
  nce(nc_put_var_double(nc_file_id, nc_var_id, vct));

  nce(nc_redef(nc_file_id));
  nce(nc_def_var(nc_file_id, "vct_b", NC_DOUBLE, 1, &nvclev_dimid, &nc_var_id));
507
508
509
510
511
512
  strcpy(atttext, "vertical-coordinate parameter set B");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, nc_var_id, "long_name", attlen, atttext));
  strcpy(atttext, "");
  attlen = strlen(atttext);
  nce(nc_put_att_text(nc_file_id, nc_var_id, "units", attlen, atttext));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
513
514
515
  nce(nc_enddef(nc_file_id));
  nce(nc_put_var_double(nc_file_id, nc_var_id, vct+nlevp1));

516
  /* Free(vct); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
518
519
520
521
522
523
524

  lspid = -1;
  nc_stpid = -1;

  for ( varid = 0; varid < nvars; varid++ )
    {
      nvals = 0;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
      code      = vars[varid].code;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527
528
529
530
531
532
      gridtype  = vars[varid].gridtype;
      zaxistype = vars[varid].zaxistype;

      ilev = zaxisInqSize(vars[varid].zaxisID);

      if ( ilev == 1 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
533
534
535
536
537
538
	  if ( code == 152 )
	    {
	      lspid = varid;
	      if ( gridtype != GRID_SPECTRAL )
		cdoAbort("%s has wrong gridtype!", vars[varid].name);
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
	  continue;
	}

      if ( nlev != ilev )
	cdoAbort("Unexpected number of level %d!", ilev);

      if ( gridtype == GRID_GAUSSIAN )
	{
	  nvals = nlon*nlat;

	  dimidsp[0] = lat_dimid;
	  dimidsp[1] = nlev_dimid;
	  dimidsp[2] = lon_dimid;
	}
      else if ( gridtype == GRID_SPECTRAL )
	{
	  nvals = nsp*2;

	  dimidsp[0] = nsp_dimid;
	  dimidsp[1] = n2_dimid;

	  if ( strcmp(vars[varid].name, "STP") == 0 || strcmp(vars[varid].name, "T") == 0 )
	    dimidsp[2] = nlevp1_dimid;
	  else
	    dimidsp[2] = nlev_dimid;
	}
      else
	cdoAbort("Unsupported grid!");

      nce(nc_redef(nc_file_id));
      nce(nc_def_var(nc_file_id, vars[varid].name, NC_DOUBLE, 3, dimidsp, &nc_var_id));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
570
      if ( vars[varid].longname && *vars[varid].longname)
571
	nce(nc_put_att_text(nc_file_id, nc_var_id, "long_name", strlen(vars[varid].longname), vars[varid].longname));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572
      if ( vars[varid].units && *vars[varid].units)
573
	nce(nc_put_att_text(nc_file_id, nc_var_id, "units", strlen(vars[varid].units), vars[varid].units));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
      nce(nc_enddef(nc_file_id));

      if (  dimidsp[2] == nlevp1_dimid ) nc_stpid = nc_var_id;

      for ( i = 0; i < nlev; i++ )
	{
	  if ( gridtype == GRID_GAUSSIAN )
	    {
	      start[0] = 0;     start[1] = i;  start[2] = 0;
	      count[0] = nlat;  count[1] = 1;  count[2] = nlon;     
	    }
	  else
	    {
	      start[0] = 0;     start[1] = 0;  start[2] = i;
	      count[0] = nsp;   count[1] = 2;  count[2] = 1;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
590
	  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
591
592
593
594
595
596
597
598
599
600
601
	  nce(nc_put_vara_double(nc_file_id, nc_var_id, start, count, vars[varid].ptr+i*nvals));
	}
    }

  if ( lspid == -1 ) cdoAbort("LSP not found!");
  if ( nc_stpid == -1 ) cdoAbort("STP not found!");

  /* write lsp */
  start[0] = 0;    start[1] = 0;  start[2] = nlev;
  count[0] = nsp;  count[1] = 2;  count[2] = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
602
  nce(nc_put_vara_double(nc_file_id, nc_stpid, start, count, vars[lspid].ptr));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
603
604
605
606
607

  /*close input file */
  nce(nc_close(nc_file_id));

#else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
608
  cdoAbort("NetCDF support not compiled in!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
609
610
611
#endif
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
612
#if defined(HAVE_LIBNETCDF)
613
614
static
void read_gg3d(int nc_file_id, const char *name, VAR *var, int gridID, int zaxisID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
{
  int nlev, nlat, nlon, gridsize, i;
  int gridtype, zaxistype;
  int nc_var_id;
  size_t start[3], count[3];

  gridtype = gridInqType(gridID);
  zaxistype = zaxisInqType(zaxisID);

  inivar(var, gridtype, zaxistype,  0, name, NULL, NULL);

  nlon = gridInqXsize(gridID);
  nlat = gridInqYsize(gridID);
  nlev = zaxisInqSize(zaxisID);

  gridsize = nlon*nlat;

  var->gridID    = gridID;
  var->zaxisID   = zaxisID;
  var->gridsize  = gridsize;
  var->nlev      = nlev;

637
  var->ptr = (double*) Malloc(nlev*gridsize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
638
639
640
641
642
643
644
645
646
647
648
649
  
  for ( i = 0; i < nlev; i++ )
    {
      start[0] = 0;     start[1] = i;  start[2] = 0;
      count[0] = nlat;  count[1] = 1;  count[2] = nlon;     

      nce(nc_inq_varid(nc_file_id, name, &nc_var_id));
      nce(nc_get_vara_double(nc_file_id, nc_var_id, start, count, var->ptr+i*gridsize));
    }
}
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
650
#if defined(HAVE_LIBNETCDF)
651
652
static
void read_fc4d(int nc_file_id, const char *name, VAR *var, int gridID, int zaxisID, int nhgl, int nmp1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
653
654
655
656
657
658
659
660
661
662
663
664
665
666
{
  int nlev, nfc, i;
  int gridtype, zaxistype;
  int nc_var_id;
  size_t start[4], count[4];

  gridtype = gridInqType(gridID);
  zaxistype = zaxisInqType(zaxisID);

  inivar(var, gridtype, zaxistype,  0, name, NULL, NULL);

  nfc  = gridInqSize(gridID);
  nlev = zaxisInqSize(zaxisID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
667
  if ( nfc != nhgl*nmp1*2 ) cdoAbort("%s: inconsistent dimension length!", __func__);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
668
669
670
671
672
673

  var->gridID    = gridID;
  var->zaxisID   = zaxisID;
  var->gridsize  = nfc;
  var->nlev      = nlev;

674
  var->ptr = (double*) Malloc(nlev*nfc*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
675
676
677
678
679
680
681
682
683
684
685
686
  
  for ( i = 0; i < nlev; i++ )
    {
      start[0] = 0;     start[1] = 0;    start[2] = 0;  start[3] = i;
      count[0] = nhgl;  count[1] = nmp1; count[2] = 2;  count[3] = 1;     

      nce(nc_inq_varid(nc_file_id, name, &nc_var_id));
      nce(nc_get_vara_double(nc_file_id, nc_var_id, start, count, var->ptr+i*nfc));
    }
}
#endif

687
688
static
int import_e5res(const char *filename, VAR **vars, ATTS *atts)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
689
690
{
  int nvars = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
691
#if defined(HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
692
  int nc_var_id;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
693
  int varid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
694
695
696
697
  size_t nvals;
  size_t start[3], count[3];
  int nlon, nlat, nlev, nvct, nfc, i;
  int gridIDgp, gridIDfc, gridIDhgl, zaxisIDml, zaxisIDmlh, zaxisIDsfc, zaxisIDbsfc, zaxisIDn2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
698
699
700
701
702
703
704
705
  int nc_file_id;
  char filetype[256];
  double *xvals, *yvals, *vct, *levs;
  int ncvarid;
  int ndims, ngatts, unlimdimid;
  int nvdims, nvatts;
  int dimidsp[9];
  int max_vars = 4096;
706
  char name[CDI_MAX_NAME];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
707
708
  int lon_dimid, lat_dimid, nhgl_dimid, nlevp1_dimid, spc_dimid, nvclev_dimid;
  int complex_dimid, nmp1_dimid, belowsurface_dimid, lev_dimid, ilev_dimid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
709
  int /* surface_dimid, height2m_dimid, height10m_dimid,*/ n2_dimid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
710
711
  int lon, lat, nhgl, nlevp1, spc, nvclev;
  int complex, nmp1, belowsurface, lev, ilev;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
713
714
715
  int /* surface, height2m, height10m,*/ n2;
  int iatt;
  int attint;
  double attflt;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
  size_t attlen, dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
717
  nc_type xtype;
718
  char attname[CDI_MAX_NAME];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
719
  const int attstringlen = 8192; char attstring[8192];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
720
721

  /* open file and check file type */
722
  /* nce(nc_open(filename, NC_NOWRITE, &nc_file_id)); */
723
  nc_file_id = cdf_openread(filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724
725
726
727
728

  nce(nc_get_att_text(nc_file_id, NC_GLOBAL, "file_type", filetype));
  nce(nc_inq_attlen(nc_file_id, NC_GLOBAL, "file_type", &attlen));
  filetype[attlen] = 0;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
729
  if ( memcmp(filetype, strfiletype_res, strlen(strfiletype_res)) != 0 ) return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
730

Uwe Schulzweida's avatar
Uwe Schulzweida committed
731
  /* printf("%s\n", filetype); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
732
733

  nce(nc_inq_dimid(nc_file_id, "lon", &lon_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
734
735
  nce(nc_inq_dimlen(nc_file_id, lon_dimid, &dimlen));
  lon = dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
736
737

  nce(nc_inq_dimid(nc_file_id, "lat", &lat_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
738
739
  nce(nc_inq_dimlen(nc_file_id, lat_dimid, &dimlen));
  lat = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
740
741

  nce(nc_inq_dimid(nc_file_id, "nhgl", &nhgl_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
742
743
  nce(nc_inq_dimlen(nc_file_id, nhgl_dimid, &dimlen));
  nhgl = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
744
745

  nce(nc_inq_dimid(nc_file_id, "nlevp1", &nlevp1_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
746
747
  nce(nc_inq_dimlen(nc_file_id, nlevp1_dimid, &dimlen));
  nlevp1 = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
748
749

  nce(nc_inq_dimid(nc_file_id, "spc", &spc_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
750
751
  nce(nc_inq_dimlen(nc_file_id, spc_dimid, &dimlen));
  spc = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
752

753
754
  UNUSED(spc);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
755
  nce(nc_inq_dimid(nc_file_id, "nvclev", &nvclev_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757
  nce(nc_inq_dimlen(nc_file_id, nvclev_dimid, &dimlen));
  nvclev = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
758
759

  nce(nc_inq_dimid(nc_file_id, "complex", &complex_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
760
761
  nce(nc_inq_dimlen(nc_file_id, complex_dimid, &dimlen));
  complex = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
762
763

  nce(nc_inq_dimid(nc_file_id, "nmp1", &nmp1_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
764
765
  nce(nc_inq_dimlen(nc_file_id, nmp1_dimid, &dimlen));
  nmp1 = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766
767

  nce(nc_inq_dimid(nc_file_id, "belowsurface", &belowsurface_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
768
769
  nce(nc_inq_dimlen(nc_file_id, belowsurface_dimid, &dimlen));
  belowsurface = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
770
771

  nce(nc_inq_dimid(nc_file_id, "lev", &lev_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
772
773
  nce(nc_inq_dimlen(nc_file_id, lev_dimid, &dimlen));
  lev = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
774
775

  nce(nc_inq_dimid(nc_file_id, "ilev", &ilev_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
776
777
  nce(nc_inq_dimlen(nc_file_id, ilev_dimid, &dimlen));
  ilev = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
778

779
  UNUSED(ilev);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
781
782
  nce(nc_inq_dimid(nc_file_id, "surface", &surface_dimid));
  nce(nc_inq_dimlen(nc_file_id, surface_dimid, &surface));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783

Uwe Schulzweida's avatar
Uwe Schulzweida committed
784
785
786
787
788
789
790
  nce(nc_inq_dimid(nc_file_id, "height2m", &height2m_dimid));
  nce(nc_inq_dimlen(nc_file_id, height2m_dimid, &height2m));

  nce(nc_inq_dimid(nc_file_id, "height10m", &height10m_dimid));
  nce(nc_inq_dimlen(nc_file_id, height10m_dimid, &height10m));
  */
  nce(nc_inq_dimid(nc_file_id, "n2", &n2_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
791
792
  nce(nc_inq_dimlen(nc_file_id, n2_dimid, &dimlen));
  n2 = (int) dimlen;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
793
794
795
796
797
798
799
800
801
802

  /* define gaussian grid */

  nlon = lon;
  nlat = lat;

  gridIDgp = gridCreate(GRID_GAUSSIAN, nlon*nlat);
  gridDefXsize(gridIDgp, nlon);
  gridDefYsize(gridIDgp, nlat);

803
804
  xvals = (double*) Malloc(nlon*sizeof(double));
  yvals = (double*) Malloc(nlat*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
805
806
807
808
809
810
811
812
813
814

  nce(nc_inq_varid(nc_file_id, "lon", &nc_var_id));
  nce(nc_get_var_double(nc_file_id, nc_var_id, xvals));

  nce(nc_inq_varid(nc_file_id, "lat", &nc_var_id));
  nce(nc_get_var_double(nc_file_id, nc_var_id, yvals));

  gridDefXvals(gridIDgp, xvals);
  gridDefYvals(gridIDgp, yvals);

815
816
  Free(xvals);
  Free(yvals);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
817

Uwe Schulzweida's avatar
Uwe Schulzweida committed
818
819
820
821
822
823
824
825
826
827
828
  /* define fourier grid */

  nfc = nhgl*nmp1*complex;

  gridIDfc = gridCreate(GRID_FOURIER, nfc);

  /* define nhgl grid */

  gridIDhgl = gridCreate(GRID_GENERIC, nhgl);


Uwe Schulzweida's avatar
Uwe Schulzweida committed
829
830
831
832
  /* define surface level */

  zaxisIDsfc = zaxisCreate(ZAXIS_SURFACE, 1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
833
  /* define below surface level */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
834

Uwe Schulzweida's avatar
Uwe Schulzweida committed
835
836
  nlev = belowsurface;
  zaxisIDbsfc = zaxisCreate(ZAXIS_DEPTH_BELOW_LAND, nlev);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
837

838
  levs = (double*) Malloc(nlev*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
839
840
  for ( i = 0; i < nlev; i++ ) levs[i] = 0;
  zaxisDefLevels(zaxisIDbsfc, levs);
841
  Free(levs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
842
843
844
845
846
847


  /* define n2 level */

  nlev = n2;
  zaxisIDn2 = zaxisCreate(ZAXIS_GENERIC, nlev);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
848

849
  levs = (double*) Malloc(nlev*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
851
  for ( i = 0; i < nlev; i++ ) levs[i] = 0;
  zaxisDefLevels(zaxisIDn2, levs);
852
  Free(levs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
853

Uwe Schulzweida's avatar
Uwe Schulzweida committed
854
855
856
857
858
  /* define model level */

  nlev = lev;
  nvct = nvclev*2;

859
  vct   = (double*) Malloc(nvct*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
860
861
862
863
864
865
866

  nce(nc_inq_varid(nc_file_id, "vct_a", &nc_var_id));
  nce(nc_get_var_double(nc_file_id, nc_var_id, vct));

  nce(nc_inq_varid(nc_file_id, "vct_b", &nc_var_id));
  nce(nc_get_var_double(nc_file_id, nc_var_id, vct+nlevp1));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
867
868
869
870
  /* ZAXIS_HYBRID */ 

  zaxisIDml  = zaxisCreate(ZAXIS_HYBRID, nlev);

871
  levs = (double*) Malloc(nlev*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
872
873
  for ( i = 0; i < nlev; i++ ) levs[i] = i+1;
  zaxisDefLevels(zaxisIDml, levs);
874
  Free(levs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
875

Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
  zaxisDefVct(zaxisIDml, 2*nlevp1, vct);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
877
878
879
880
881

  /* ZAXIS_HYBRID_HALF */ 

  zaxisIDmlh  = zaxisCreate(ZAXIS_HYBRID_HALF, nlevp1);

882
  levs = (double*) Malloc(nlevp1*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
883
884
  for ( i = 0; i < nlevp1; i++ ) levs[i] = i;
  zaxisDefLevels(zaxisIDmlh, levs);
885
  Free(levs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
886
887
888

  zaxisDefVct(zaxisIDmlh, 2*nlevp1, vct);

889
  Free(vct);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
890
891
892
893


  nce(nc_inq(nc_file_id, &ndims, &nvars, &ngatts, &unlimdimid));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
894
895
896
897
898
899
900
901
902
903
904
  /* read global attributtes*/
  for ( iatt = 0; iatt < ngatts; iatt++ )
    {
      nce(nc_inq_attname(nc_file_id, NC_GLOBAL, iatt, attname));
      nce(nc_inq_atttype(nc_file_id, NC_GLOBAL, attname, &xtype));
      nce(nc_inq_attlen(nc_file_id, NC_GLOBAL, attname, &attlen));

      if ( xtype == NC_CHAR )
	{
	  if ( (int)attlen > attstringlen )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
905
	      fprintf(stderr, "Attribute %s too large, skipped!\n", attname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
906
907
908
909
910
911
912
913
914
915
916
917
918
	      continue;
	    }
	  nce(nc_get_att_text(nc_file_id, NC_GLOBAL, attname, attstring));
	  attstring[attlen] = 0;

	  atts->atxtname[atts->natxt]  = strdup(attname);
	  atts->atxtentry[atts->natxt] = strdup(attstring);
	  atts->natxt++;
	}
      else if ( xtype == NC_INT )
	{
	  if ( attlen > 1 )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
919
	      fprintf(stderr, "Attribute %s too large, skipped!\n", attname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
920
921
922
923
924
925
926
927
	      continue;
	    }
	  nce(nc_get_att_int(nc_file_id, NC_GLOBAL, attname, &attint));
	}
      else if ( xtype == NC_DOUBLE )
	{
	  if ( attlen > 1 )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
928
	      fprintf(stderr, "Attribute %s too large, skipped!\n", attname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
929
930
931
932
933
934
935
936
937
938
	      continue;
	    }
	  nce(nc_get_att_double(nc_file_id, NC_GLOBAL, attname, &attflt));
	}
      else
	{
	  fprintf(stderr, "Unsupported attribute type for %s\n", attname);
	}
    }

939
  *vars = (VAR*) Malloc(max_vars*sizeof(VAR));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
940
941
942
943
944
945

  varid = 0;
  for ( ncvarid = 0; ncvarid < nvars; ncvarid++ )
    {      
      nce(nc_inq_var(nc_file_id, ncvarid, name, &xtype, &nvdims, dimidsp, &nvatts));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
946
947
      if ( varid >= max_vars ) cdoAbort("Too many variables (max = %d)!", max_vars);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
948
949
950
951
952
      if ( nvdims == 4 )
	{
	  if ( dimidsp[0] == nhgl_dimid    && dimidsp[1] == nmp1_dimid &&
	       dimidsp[2] == complex_dimid && dimidsp[3] == lev_dimid )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
953
954
	      read_fc4d(nc_file_id, name, &(*vars)[varid], gridIDfc, zaxisIDml, nhgl, nmp1);
	      varid++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
955
956
957
958
	    }
	  else if ( dimidsp[0] == nhgl_dimid    && dimidsp[1] == nmp1_dimid &&
	            dimidsp[2] == complex_dimid && dimidsp[3] == ilev_dimid )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
960
	      read_fc4d(nc_file_id, name, &(*vars)[varid], gridIDfc, zaxisIDmlh, nhgl, nmp1);
	      varid++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
961
962
963
	    }
	  else
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
964
	      fprintf(stderr, "4D structure of %s unsupported!\n", name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
965
966
967
968
969
970
971
	    }
	}
      else if ( nvdims == 3 )
	{
	  if ( dimidsp[0] == lat_dimid && dimidsp[1] == lev_dimid &&
	       dimidsp[2] == lon_dimid)
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
972
	      read_gg3d(nc_file_id, name, &(*vars)[varid], gridIDgp, zaxisIDml);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
973
974
975
976
	      varid++;
	    }
	  else if ( dimidsp[0] == lat_dimid && dimidsp[1] == ilev_dimid &&
		    dimidsp[2] == lon_dimid)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
977
978
979
	    {  
	      read_gg3d(nc_file_id, name, &(*vars)[varid], gridIDgp, zaxisIDmlh);
	      varid++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
980
981
982
983
	    }
	  else if ( dimidsp[0] == lat_dimid && dimidsp[1] == belowsurface_dimid &&
		    dimidsp[2] == lon_dimid)
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
984
985
	      read_gg3d(nc_file_id, name, &(*vars)[varid], gridIDgp, zaxisIDbsfc);
	      varid++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
986
987
988
989
	    }
	  else if ( dimidsp[0] == lat_dimid && dimidsp[1] == n2_dimid &&
		    dimidsp[2] == lon_dimid)
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
990
991
	      read_gg3d(nc_file_id, name, &(*vars)[varid], gridIDgp, zaxisIDn2);
	      varid++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
992
993
994
	    }
	  else
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
995
	      fprintf(stderr, "3D structure of %s unsupported!\n", name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
	    }
	}
      else if ( nvdims == 2 )
	{
	  if ( dimidsp[0] == lat_dimid && dimidsp[1] == lon_dimid)
	    {
	      nvals = nlon*nlat;
  
	      inivar(&(*vars)[varid], GRID_GAUSSIAN, ZAXIS_SURFACE,  0, name, NULL, NULL);

	      (*vars)[varid].gridID    = gridIDgp;
	      (*vars)[varid].zaxisID   = zaxisIDsfc;
	      (*vars)[varid].gridsize  = nvals;
	      (*vars)[varid].nlev      = 1;

1011
	      (*vars)[varid].ptr = (double*) Malloc(nvals*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1012

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
	      nce(nc_inq_varid(nc_file_id, name, &nc_var_id));
	      nce(nc_get_var_double(nc_file_id, nc_var_id, (*vars)[varid].ptr));

	      varid++;
	    }
	  else if ( dimidsp[0] == nhgl_dimid && dimidsp[1] == lev_dimid)
	    {
	      nvals = nhgl;
  
	      inivar(&(*vars)[varid], GRID_GENERIC, ZAXIS_HYBRID,  0, name, NULL, NULL);

	      (*vars)[varid].gridID    = gridIDhgl;
	      (*vars)[varid].zaxisID   = zaxisIDml;
	      (*vars)[varid].gridsize  = nvals;
	      (*vars)[varid].nlev      = nlev;

1029
	      (*vars)[varid].ptr = (double*) Malloc(nvals*nlev*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1030

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1031
1032
	      for ( i = 0; i < nlev; i++ )
		{
1033
1034
		  start[0] = 0;      start[1] = i;
		  count[0] = nvals;  count[1] = 1;  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1035

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1036
		  nce(nc_inq_varid(nc_file_id, name, &nc_var_id));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1037
		  nce(nc_get_vara_double(nc_file_id, nc_var_id, start, count, (*vars)[varid].ptr+i*nvals));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1038
1039
1040
1041
1042
1043
		}

	      varid++;
	    }
	  else
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1044
	      fprintf(stderr, "2D structure of %s unsupported!\n", name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
	    }
	}
      else if ( nvdims == 1 )
	{
	  if ( dimidsp[0] == lat_dimid && strcmp(name, "lat") == 0 )
	    {
	    }
	  else if ( dimidsp[0] == lon_dimid && strcmp(name, "lon") == 0 )
	    {
	    }
	  else if ( dimidsp[0] == nvclev_dimid && strcmp(name, "vct_a") == 0 )
	    {
	    }
	  else if ( dimidsp[0] == nvclev_dimid && strcmp(name, "vct_b") == 0 )
	    {
	    }
	  else
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1063
	      fprintf(stderr, "1D structure of %s unsupported!\n", name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1064
1065
1066
1067
	    }
	}
      else
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1068
	  fprintf(stderr, "structure of %s unsupported!\n", name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1069
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1070
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1071
1072
1073
1074
1075
1076
1077

  /*close input file */
  nce(nc_close(nc_file_id));

  nvars = varid;

#else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1078
  cdoAbort("NetCDF support not compiled in!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1079
1080
#endif

1081
  return nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1082
1083
}

1084
1085
static
void export_e5res(const char *filename, VAR *vars, int nvars)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1086
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1087
#if defined(HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1088
  int nc_var_id;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1089
  int varid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1090
  size_t nvals;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1091
  size_t start[4], count[4];
1092
  int nlon, nlat, nlev/*, nvct*/, nfc, i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1093
  int gridIDgp = -1, zaxisIDml = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1094
  int nc_file_id;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1095
1096
  double *xvals, *yvals;
  const double *vct;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1097
1098
1099
  int dimidsp[9];
  int lon_dimid, lat_dimid, nhgl_dimid, nlevp1_dimid, spc_dimid, nvclev_dimid;
  int complex_dimid, nmp1_dimid, belowsurface_dimid, lev_dimid, ilev_dimid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1100
1101
1102
1103
1104
  int /* surface_dimid, height2m_dimid, height10m_dimid,*/ n2_dimid;
  int lon = 0, lat = 0, nhgl = 0, nlevp1 = 0, spc = 0, nvclev = 0;
  int complex, nmp1, belowsurface, lev = 0, ilev = 0;
  int /* surface, height2m, height10m,*/ n2;
  int gridtype, zaxistype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1105

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1106
1107
  /* create file */
  nce(nc_create(filename, NC_CLOBBER, &nc_file_id));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1108

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1109
  nce(nc_put_att_text(nc_file_id, NC_GLOBAL, "file_type", strlen(strfiletype_res), strfiletype_res));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1110

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1111
1112
  n2 = 2;
  complex = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1113

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1114
1115
1116
1117
1118
  lon = 0; lat = 0; nfc = 0; lev = 0; belowsurface = 0;
  for ( varid = 0; varid < nvars; ++varid )
    {
      gridtype  = vars[varid].gridtype;
      zaxistype = vars[varid].zaxistype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1119

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
      if ( gridtype == GRID_GAUSSIAN && lat == 0 )
	{
	  gridIDgp = vars[varid].gridID;
	  lon = gridInqXsize(vars[varid].gridID);
	  lat = gridInqYsize(vars[varid].gridID);
	  nhgl = lat/2;
	}
      else if ( gridtype == GRID_FOURIER && nfc == 0 )
	{
	  nfc = gridInqSize(vars[varid].gridID);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1131

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
      if ( zaxistype == ZAXIS_HYBRID && lev == 0 )
	{
	  zaxisIDml = vars[varid].zaxisID;
	  lev = zaxisInqSize(vars[varid].zaxisID);
	  nlevp1 = lev + 1;
	  ilev = lev + 1;
	  nvclev = ilev;
	}
      else if ( zaxistype == ZAXIS_DEPTH_BELOW_LAND && belowsurface == 0 )
	{
	  belowsurface = zaxisInqSize(vars[varid].zaxisID);
	}
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1145

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1146
1147
1148
  if ( lat == 0 ) cdoAbort("Gaussian grid not found!");
  if ( nfc == 0 ) cdoAbort("Fourier data not found!");
  if ( lev == 0 ) cdoAbort("Hybrid level not found!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1149

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1150
1151
  nmp1 = (nfc/nhgl)/2;
  spc  = nmp1*(nmp1+1)/2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1152

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1153
1154
1155
1156
1157
  nlon = lon;
  nlat = lat;
  nlev = lev;
  
  nce(nc_def_dim(nc_file_id, "lon", lon, &lon_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1158

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1159
  nce(nc_def_dim(nc_file_id, "lat", lat, &lat_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1160

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1161
  nce(nc_def_dim(nc_file_id, "nhgl", nhgl, &nhgl_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1162

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1163
  nce(nc_def_dim(nc_file_id, "nlevp1", nlevp1, &nlevp1_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1164

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1165
  nce(nc_def_dim(nc_file_id, "spc", spc, &spc_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1166

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1167
  nce(nc_def_dim(nc_file_id, "nvclev", nvclev, &nvclev_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1168

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1169
  nce(nc_def_dim(nc_file_id, "complex", complex, &complex_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1170

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1171
  nce(nc_def_dim(nc_file_id, "nmp1", nmp1, &nmp1_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1172

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1173
  nce(nc_def_dim(nc_file_id, "belowsurface", belowsurface, &belowsurface_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1174

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1175
  nce(nc_def_dim(nc_file_id, "lev", lev, &lev_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1176

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1177
  nce(nc_def_dim(nc_file_id, "ilev", ilev, &ilev_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1178

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1179
1180
  /*
  nce(nc_def_dim(nc_file_id, "surface", surface, &surface_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1181

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1182
  nce(nc_def_dim(nc_file_id, "height2m", height2m, &height2m_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1183

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1184
1185
1186
  nce(nc_def_dim(nc_file_id, "height10m", height10m, &height10m_dimid));
  */
  nce(nc_def_dim(nc_file_id, "n2", n2, &n2_dimid));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1187

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1188
  nce(nc_enddef(nc_file_id));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1189
1190


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1191
1192
1193
1194
  for ( varid = 0; varid < nvars; varid++ )
    {
      gridtype  = vars[varid].gridtype;
      zaxistype = vars[varid].zaxistype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1195

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1196
1197
1198
      if ( gridtype == GRID_FOURIER && zaxistype == ZAXIS_HYBRID )
	{
	  nvals = nfc;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1199

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1200
1201
1202
1203
	  dimidsp[0] = nhgl_dimid;
	  dimidsp[1] = nmp1_dimid;
	  dimidsp[2] = complex_dimid;
	  dimidsp[3] = lev_dimid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1204

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1205
1206
1207
	  nce(nc_redef(nc_file_id));
	  nce(nc_def_var(nc_file_id, vars[varid].name, NC_DOUBLE, 4, dimidsp, &nc_var_id));
	  nce(nc_enddef(nc_file_id));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1208

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1209
1210
1211
1212
	  for ( i = 0; i < nlev; i++ )
	    {
	      start[0] = 0;     start[1] = 0;    start[2] = 0;  start[3] = i;
	      count[0] = nhgl;  count[1] = nmp1; count[2] = 2;  count[3] = 1; 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1213

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1214
1215
1216
1217
1218
1219
	      nce(nc_put_vara_double(nc_file_id, nc_var_id, start, count, vars[varid].ptr+i*nvals));
	    }  
	}
      else if ( gridtype == GRID_FOURIER && zaxistype == ZAXIS_HYBRID_HALF )
	{
	  nvals = nfc;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1220

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1221
1222
1223
1224
	  dimidsp[0] = nhgl_dimid;
	  dimidsp[1] = nmp1_dimid;
	  dimidsp[2] = complex_dimid;
	  dimidsp[3] = ilev_dimid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1225

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1226
1227
1228
	  nce(nc_redef(nc_file_id));
	  nce(nc_def_var(nc_file_id, vars[varid].name, NC_DOUBLE, 4, dimidsp, &nc_var_id));
	  nce(nc_enddef(nc_file_id));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1229

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1230
1231
1232
1233
	  for ( i = 0; i < ilev; i++ )
	    {
	      start[0] = 0;     start[1] = 0;    start[2] = 0;  start[3] = i;
	      count[0] = nhgl;  count[1] = nmp1; count[2] = 2;  count[3] = 1;