Filedes.c 10 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.

5
  Copyright (C) 2003-2012 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
88
89
90
  char pstr[32];
  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
91
  int chunktype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
94
95
96
97
98
99
100
      
  nvars  = vlistNvars(vlistID);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  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
122
	  fprintf(fp, "&parameter\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
126
127
128
129
	  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
130
131
  for ( varID = 0; varID < nvars; varID++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
      fprintf(fp, "&parameter\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134
135
136
137
138
139
      
      varname[0]     = 0;
      varlongname[0] = 0;
      varunits[0]    = 0;
      code     = vlistInqVarCode(vlistID, varID);
      tableID  = vlistInqVarTable(vlistID, varID);
      tabnum   = tableInqNum(tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
      missval  = vlistInqVarMissval(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
143
144
145
146
147
      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
148
            
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
      fprintf(fp, "  name=%s\n", varname);
150
151
      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
152
      if ( strlen(varstdname) )
153
	fprintf(fp, "  standard_name=%s\n", varstdname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
      if ( strlen(varlongname) )
155
	fprintf(fp, "  long_name=\"%s\"\n", varlongname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
      if ( strlen(varunits) )
157
	fprintf(fp, "  units=\"%s\"\n", varunits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
160
161
      if ( datatype == -1 )
	if ( datatype2str(vlistInqVarDatatype(vlistID, varID), pstr) == 0 )
	  fprintf(fp, "  datatype=%s\n", pstr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
163
164
165
166
167
168
169
170

      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
171
172
      
      if ( option == 2 ) printAtts(fp, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
174
      if ( option == 2 ) 
	fprintf(fp, "  missing_value=%g\n", missval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
176
177
178
179
180
181
182
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
      
      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);
    }
  
  printf("\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
246
247
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249
void *Filedes(void *argument)
{
250
  int GRIDDES, GRIDDES2, ZAXISDES, VCT, VCT2, PARDES, FILEDES, VLIST, PARTAB, PARTAB2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
252
253
254
255
256
257
258
259
  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
260
261
262
263
264
265
266
267
268
  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);
269
  PARTAB2  = cdoOperatorAdd("partab2",   0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
270
271
272
273
274
275
276
277
278
279
280

  operatorID = cdoOperatorID();

  streamID = streamOpenRead(cdoStreamName(0));

  vlistID = streamInqVlist(streamID);

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

281
  if ( operatorID == GRIDDES || operatorID == GRIDDES2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
    {
283
284
      int opt = 0;
      if ( operatorID == GRIDDES ) opt = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
      for ( index = 0; index < ngrids; index++ )
286
	gridPrint(vlistGrid(vlistID, index), opt);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
288
289
290
291
292
    }
  else if ( operatorID == ZAXISDES )
    {
      for ( index = 0; index < nzaxis; index++ )
	zaxisPrint(vlistZaxis(vlistID, index));
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
  else if ( operatorID == VCT || operatorID == VCT2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295
296
297
298
    {
      for ( index = 0; index < nzaxis; index++)
	{
	  zaxisID = vlistZaxis(vlistID, index);
	  type = zaxisInqType(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
299
	  if ( type == ZAXIS_HYBRID || type == ZAXIS_HYBRID_HALF )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
300
301
302
303
304
305
306
	    {
	      int i, vctsize;
	      const double *vct;

	      vctsize = zaxisInqVctSize(zaxisID);
	      vct     = zaxisInqVctPtr(zaxisID);
		
307
	      if ( vctsize%2 == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
		{
		  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");
		    }
		}
332
333
334
	      else
		for ( i = 0; i < vctsize; i++ )
		  fprintf(stdout, "%5d %25.17f\n", i, vct[i]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336

	      break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
338
339
340
341
342
343
	    }
	}
    }
  else if ( operatorID == VLIST )
    {
      vlistPrint(vlistID);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
  else if ( operatorID == PARDES )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345
346
    {
      int varID, code;
347
      char varname[CDI_MAX_NAME], varlongname[CDI_MAX_NAME], varunits[CDI_MAX_NAME];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

      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");
	}   
    }
368
  else if ( operatorID == PARTAB || operatorID == PARTAB2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
      int option = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371

Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373
374
      if ( operatorID == PARTAB2 ) option = 2;
      
      partab(stdout, vlistID, option);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
375
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
377
  else if ( operatorID == FILEDES )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
      filedes(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
380
381
382
383
384
385
386
    }

  streamClose(streamID);

  cdoFinish();

  return (0);
}