Filedes.c 10.3 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-2014 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  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
19
20
/*
   This module contains the following operators:

Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
      Filedes    pardes          Parameter description
22
23
24
25
      Filedes    griddes         Grid description
      Filedes    vct             Vertical coordinate table
*/

Ralf Mueller's avatar
Ralf Mueller committed
26
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
#include "util.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
32


Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
void printAtts(FILE *fp, int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
#define MAXATT 8192
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
  int natts, ia;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
39
40
41
42
43
  char attname[1024];
  int atttype, attlen;
  char atttxt[MAXATT];
  int attint[MAXATT];
  double attflt[MAXATT];
  int i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
46
47
48

  vlistInqNatts(vlistID, varID, &natts);

  for ( ia = 0; ia < natts; ++ia )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
      vlistInqAtt(vlistID, varID, ia, attname, &atttype, &attlen);
      if ( atttype == DATATYPE_INT )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
53
	  if ( attlen > MAXATT ) attlen = MAXATT;
	  vlistInqAttInt(vlistID, varID, attname, attlen, attint);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
	  fprintf(fp, "  %s=", attname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56
	  for ( i = 0; i < attlen; ++i)
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
58
	      if ( i > 0 ) fprintf(fp, ", ");
	      fprintf(fp, "%d", attint[i]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
	  fprintf(fp, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
      else if ( atttype == DATATYPE_FLT )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
63
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
65
	  if ( attlen > MAXATT ) attlen = MAXATT;
	  vlistInqAttFlt(vlistID, varID, attname, MAXATT, attflt);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
	  fprintf(fp, "  %s=", attname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
	  for ( i = 0; i < attlen; ++i)
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
70
	      if ( i > 0 ) fprintf(fp, ", ");
	      fprintf(fp, "%g", attflt[i]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
	  fprintf(fp, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
      else if ( atttype == DATATYPE_TXT )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
	  vlistInqAttTxt(vlistID, varID, attname, sizeof(atttxt), atttxt);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
	  atttxt[attlen] = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
80
81
82
83
84
85
	  fprintf(fp, "  %s=\"%s\"\n", attname, atttxt);
	}
    }
}

static
void partab(FILE *fp, int vlistID, int option)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
  int varID, code, tabnum, tableID, datatype = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
  int param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88
  char pstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91
92
  char varname[CDI_MAX_NAME], varlongname[CDI_MAX_NAME], varstdname[CDI_MAX_NAME], varunits[CDI_MAX_NAME];
  int natts;
  int nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
  int chunktype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96
97
98
99
100
101
102
      
  nvars  = vlistNvars(vlistID);

  if ( option == 2 )
    {
      vlistInqNatts(vlistID, CDI_GLOBAL, &natts);
      if ( natts > 0 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
104
	  fprintf(fp, "&parameter\n");
	  fprintf(fp, "  name=_GLOBAL_\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
106
	  printAtts(fp, vlistID, CDI_GLOBAL);
	  fprintf(fp, "/\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
	}
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109

Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  if ( nvars > 1 )
    {
      datatype = vlistInqVarDatatype(vlistID, 0);
      for ( varID = 1; varID < nvars; varID++ )
	{
	  if ( datatype != vlistInqVarDatatype(vlistID, varID) )
	    {
	      datatype = -1;
	      break;
	    }
	}

      if ( datatype != -1 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
	  fprintf(fp, "&parameter\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
126
127
128
129
130
131
	  fprintf(fp, "  name=_default_\n");
	  if ( datatype2str(datatype, pstr) == 0 )
	    fprintf(fp, "  datatype=%s\n", pstr);
	  fprintf(fp, "/\n");
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
133
  for ( varID = 0; varID < nvars; varID++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
      fprintf(fp, "&parameter\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
137
138
139
      
      varname[0]     = 0;
      varlongname[0] = 0;
      varunits[0]    = 0;
      code     = vlistInqVarCode(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
      param    = vlistInqVarParam(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
      tableID  = vlistInqVarTable(vlistID, varID);
      tabnum   = tableInqNum(tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
      missval  = vlistInqVarMissval(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
146
147
148
149
150
      vlistInqVarName(vlistID, varID, varname);
      /* printf("1>%s<\n", varname); */
      vlistInqVarStdname(vlistID, varID, varstdname);
      /* printf("2>%s<\n", varname); */
      vlistInqVarLongname(vlistID, varID, varlongname);
      /* printf("3>%s<\n", varname); */
      vlistInqVarUnits(vlistID, varID, varunits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
            
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
      fprintf(fp, "  name=%s\n", varname);
153
154
      if ( code   > 0 ) fprintf(fp, "  code=%d\n", code);
      if ( tabnum > 0 ) fprintf(fp, "  table=%d\n", tabnum);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
156
157
158
159
      if ( code   > 0 )
	{
	  cdiParamToString(param, paramstr, sizeof(paramstr));
	  fprintf(fp, "  param=%s\n", paramstr);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
      if ( strlen(varstdname) )
161
	fprintf(fp, "  standard_name=%s\n", varstdname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
      if ( strlen(varlongname) )
163
	fprintf(fp, "  long_name=\"%s\"\n", varlongname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
      if ( strlen(varunits) )
165
	fprintf(fp, "  units=\"%s\"\n", varunits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
169
      if ( datatype == -1 )
	if ( datatype2str(vlistInqVarDatatype(vlistID, varID), pstr) == 0 )
	  fprintf(fp, "  datatype=%s\n", pstr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172
173
174
175
176
177
178

      chunktype = vlistInqVarChunkType(vlistID, varID);
      if ( chunktype == CHUNK_AUTO )
	fprintf(fp, "  chunktype=auto\n");
      else if ( chunktype == CHUNK_GRID )
	fprintf(fp, "  chunktype=grid\n");
      if ( chunktype == CHUNK_LINES )
	fprintf(fp, "  chunktype=lines\n");
	
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
      
      if ( option == 2 ) printAtts(fp, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
182
      if ( option == 2 ) 
	fprintf(fp, "  missing_value=%g\n", missval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
      
      fprintf(fp, "/\n");
    }   
}

static
void filedes(int streamID)
{
  int filetype;

  printf("\n");
  filetype = streamInqFiletype(streamID);
  switch ( filetype )
    {
    case FILETYPE_GRB:
      printf("  GRIB data\n");
      break;
    case FILETYPE_NC:
      printf("  netCDF data\n");
      break;
    case FILETYPE_NC2:
      printf("  netCDF2 data\n");
      break;
    case FILETYPE_NC4:
      printf("  netCDF4 data\n");
      break;
    case FILETYPE_NC4C:
      printf("  netCDF4 classic data\n");
      break;
    case FILETYPE_SRV:
      printf("  SERVICE data\n");
      switch ( streamInqByteorder(streamID) )
	{
	case CDI_BIGENDIAN:
	  printf("  byteorder is BIGENDIAN\n"); break;
	case CDI_LITTLEENDIAN:
	  printf("  byteorder is LITTLEENDIAN\n"); break;
	default:
	  printf("  byteorder %d undefined\n", streamInqByteorder(streamID)); break;
	}
      break;
    case FILETYPE_EXT:
      printf("  EXTRA data\n");
      switch ( streamInqByteorder(streamID) )
	{
	case CDI_BIGENDIAN:
	  printf("  byteorder is BIGENDIAN\n"); break;
	case CDI_LITTLEENDIAN:
	  printf("  byteorder is LITTLEENDIAN\n"); break;
	default:
	  printf("  byteorder %d undefined\n", streamInqByteorder(streamID)); break;
	}
      break;
    case FILETYPE_IEG:
      printf("  IEG data\n");
      switch ( streamInqByteorder(streamID) )
	{
	case CDI_BIGENDIAN:
	  printf("  byteorder is BIGENDIAN\n"); break;
	case CDI_LITTLEENDIAN:
	  printf("  byteorder is LITTLEENDIAN\n"); break;
	default:
	  printf("  byteorder %d undefined\n", streamInqByteorder(streamID)); break;
	}
      break;
    default:
      printf("  unsupported filetype %d\n" , filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
      break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
252
253
    }
  
  printf("\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
255
256
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
258
void *Filedes(void *argument)
{
259
  int GRIDDES, GRIDDES2, ZAXISDES, VCT, VCT2, PARDES, FILEDES, VLIST, PARTAB, PARTAB2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
261
262
263
264
265
266
267
268
  int operatorID;
  int streamID = 0;
  int zaxisID;
  int nvars, ngrids, nzaxis;
  int type, index;
  int vlistID;

  cdoInitialize(argument);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
270
271
272
273
274
275
276
277
  GRIDDES  = cdoOperatorAdd("griddes",   0, 0, NULL);
  GRIDDES2 = cdoOperatorAdd("griddes2",  0, 0, NULL);
  ZAXISDES = cdoOperatorAdd("zaxisdes",  0, 0, NULL);
  VCT      = cdoOperatorAdd("vct",       0, 0, NULL);
  VCT2     = cdoOperatorAdd("vct2",      0, 0, NULL);
  PARDES   = cdoOperatorAdd("pardes",    0, 0, NULL);
  FILEDES  = cdoOperatorAdd("filedes",   0, 0, NULL);
  VLIST    = cdoOperatorAdd("vlist",     0, 0, NULL);
  PARTAB   = cdoOperatorAdd("partab",    0, 0, NULL);
278
  PARTAB2  = cdoOperatorAdd("partab2",   0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
280
281
282
283
284
285
286
287
288
289

  operatorID = cdoOperatorID();

  streamID = streamOpenRead(cdoStreamName(0));

  vlistID = streamInqVlist(streamID);

  nvars  = vlistNvars(vlistID);
  ngrids = vlistNgrids(vlistID);
  nzaxis = vlistNzaxis(vlistID);

290
  if ( operatorID == GRIDDES || operatorID == GRIDDES2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
291
    {
292
293
      int opt = 0;
      if ( operatorID == GRIDDES ) opt = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
      for ( index = 0; index < ngrids; index++ )
295
	gridPrint(vlistGrid(vlistID, index), opt);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
296
297
298
299
300
301
    }
  else if ( operatorID == ZAXISDES )
    {
      for ( index = 0; index < nzaxis; index++ )
	zaxisPrint(vlistZaxis(vlistID, index));
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
  else if ( operatorID == VCT || operatorID == VCT2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
304
305
306
307
    {
      for ( index = 0; index < nzaxis; index++)
	{
	  zaxisID = vlistZaxis(vlistID, index);
	  type = zaxisInqType(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308
	  if ( type == ZAXIS_HYBRID || type == ZAXIS_HYBRID_HALF )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
310
311
312
313
314
315
	    {
	      int i, vctsize;
	      const double *vct;

	      vctsize = zaxisInqVctSize(zaxisID);
	      vct     = zaxisInqVctPtr(zaxisID);
		
316
	      if ( vctsize%2 == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
		{
		  if ( operatorID == VCT )
		    {
		      for ( i = 0; i < vctsize/2; i++ )
			fprintf(stdout, "%5d %25.17f %25.17f\n", i, vct[i], vct[vctsize/2+i]);
		    }
		  else
		    {
		      int nbyte0, nbyte;
		      fprintf(stdout, "vctsize   = %d\n", vctsize);
		      nbyte0 = fprintf(stdout, "vct       = ");
		      nbyte = nbyte0;
		      for ( i = 0; i < vctsize; i++ )
			{
			  if ( nbyte > 70 || i == vctsize/2 )
			    {
			      fprintf(stdout, "\n%*s", nbyte0, "");
			      nbyte = nbyte0;
			    }
			  nbyte += fprintf(stdout, "%.9g ", vct[i]);
			}
		      fprintf(stdout, "\n");
		    }
		}
341
342
343
	      else
		for ( i = 0; i < vctsize; i++ )
		  fprintf(stdout, "%5d %25.17f\n", i, vct[i]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
345

	      break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
347
348
349
350
351
352
	    }
	}
    }
  else if ( operatorID == VLIST )
    {
      vlistPrint(vlistID);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353
  else if ( operatorID == PARDES )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
355
    {
      int varID, code;
356
      char varname[CDI_MAX_NAME], varlongname[CDI_MAX_NAME], varunits[CDI_MAX_NAME];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376

      for ( varID = 0; varID < nvars; varID++ )
	{
	  varname[0]     = 0;
	  varlongname[0] = 0;
	  varunits[0]    = 0;
	  code     = vlistInqVarCode(vlistID, varID);
	  vlistInqVarName(vlistID, varID, varname);
	  vlistInqVarLongname(vlistID, varID, varlongname);
	  vlistInqVarUnits(vlistID, varID, varunits);
	  fprintf(stdout, "%4d  %-12s", code, varname);
	  if ( strlen(varlongname) )
	    {
	      fprintf(stdout, "  %s", varlongname);
	      if ( strlen(varunits) )
		fprintf(stdout, " [%s]", varunits);
	    }
	  fprintf(stdout, "\n");
	}   
    }
377
  else if ( operatorID == PARTAB || operatorID == PARTAB2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
      int option = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380

Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
382
383
      if ( operatorID == PARTAB2 ) option = 2;
      
      partab(stdout, vlistID, option);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
385
386
  else if ( operatorID == FILEDES )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
      filedes(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
389
390
391
392
393
394
395
    }

  streamClose(streamID);

  cdoFinish();

  return (0);
}