Filedes.c 9.83 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-2006 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
26
      Filedes    griddes         Grid description
      Filedes    vct             Vertical coordinate table
*/


Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29
30
31
32
33
34
35
36
#include <string.h>

#include "cdi.h"
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"


void *Filedes(void *argument)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
  int GRIDDES, GRIDDES2, ZAXISDES, VCT, PARDES, TAXISDES, FILEDES, VLIST, PARTAB;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
39
40
41
42
43
44
45
46
47
  int operatorID;
  int streamID = 0;
  int zaxisID;
  int nvars, ngrids, nzaxis;
  int type, index;
  int vlistID;

  cdoInitialize(argument);

  GRIDDES  = cdoOperatorAdd("griddes",  0, 0, NULL);
48
  GRIDDES2 = cdoOperatorAdd("griddes2", 0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
  ZAXISDES = cdoOperatorAdd("zaxisdes", 0, 0, NULL);
50
  TAXISDES = cdoOperatorAdd("taxisdes", 0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
  VCT      = cdoOperatorAdd("vct",      0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
  PARDES   = cdoOperatorAdd("pardes",   0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
54
  FILEDES  = cdoOperatorAdd("filedes",  0, 0, NULL);
  VLIST    = cdoOperatorAdd("vlist",    0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
  PARTAB   = cdoOperatorAdd("partab",   0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
57
58
59
60
61
62
63
64
65
66
67

  operatorID = cdoOperatorID();

  streamID = streamOpenRead(cdoStreamName(0));
  if ( streamID < 0 ) cdiError(streamID, "Open failed on %s", cdoStreamName(0));

  vlistID = streamInqVlist(streamID);

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

68
  if ( operatorID == GRIDDES || operatorID == GRIDDES2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
    {
70
71
      int opt = 0;
      if ( operatorID == GRIDDES ) opt = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
      for ( index = 0; index < ngrids; index++ )
73
	gridPrint(vlistGrid(vlistID, index), opt);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
76
77
78
79
    }
  else if ( operatorID == ZAXISDES )
    {
      for ( index = 0; index < nzaxis; index++ )
	zaxisPrint(vlistZaxis(vlistID, index));
    }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  else if ( operatorID == TAXISDES )
    {
      int vdate, vtime, ntsteps, nrecs;
      int year, month, day, hour, minute;
      int taxisID, tsID;

      taxisID = vlistInqTaxis(vlistID);
      ntsteps = vlistNtsteps(vlistID);

      fprintf(stdout, "   Time axis  : ");
      if ( taxisInqType(taxisID) == TAXIS_RELATIVE )
	fprintf(stdout, "relative");
      else if ( taxisInqType(taxisID) == TAXIS_ABSOLUTE )
	fprintf(stdout, "absolute");
      else
	fprintf(stdout, "unknown");
      fprintf(stdout, "\n");

      taxisID = vlistInqTaxis(vlistID);

      if ( ntsteps != 0 )
	{
	  if ( ntsteps == CDI_UNDEFID )
	    fprintf(stdout, "   Time steps :  unlimited\n");
	  else
	    fprintf(stdout, "   Time steps :  %d\n", ntsteps);

	  if ( taxisID != CDI_UNDEFID )
	    {
	      int calendar, unit;

	      if ( taxisInqType(taxisID) == TAXIS_RELATIVE )
		{
		  vdate = taxisInqRdate(taxisID);
		  vtime = taxisInqRtime(taxisID);

		  decode_date(vdate, &year, &month, &day);
		  decode_time(vtime, &hour, &minute);

		  fprintf(stdout, "     RefTime = %4.4d-%2.2d-%2.2d %2.2d:%2.2d",
			  year, month, day, hour, minute);
		      
		  unit = taxisInqTunit(taxisID);
		  if ( unit != CDI_UNDEFID )
		    {
		      if ( unit == TUNIT_YEAR )
			fprintf(stdout, "  Units = years");
		      else if ( unit == TUNIT_MONTH )
			fprintf(stdout, "  Units = months");
		      else if ( unit == TUNIT_DAY )
			fprintf(stdout, "  Units = days");
		      else if ( unit == TUNIT_HOUR )
			fprintf(stdout, "  Units = hours");
		      else if ( unit == TUNIT_MINUTE )
			fprintf(stdout, "  Units = minutes");
		      else if ( unit == TUNIT_SECOND )
			fprintf(stdout, "  Units = seconds");
		      else
			fprintf(stdout, "  Units = unknown");
		    }
	      
		  calendar = taxisInqCalendar(taxisID);
		  if ( calendar != CDI_UNDEFID )
		    {
		      if ( calendar == CALENDAR_STANDARD )
			fprintf(stdout, "  Calendar = STANDARD");
		      else if ( calendar == CALENDAR_NONE )
			fprintf(stdout, "  Calendar = NONE");
		      else if ( calendar == CALENDAR_360DAYS )
			fprintf(stdout, "  Calendar = 360DAYS");
		      else if ( calendar == CALENDAR_365DAYS )
			fprintf(stdout, "  Calendar = 365DAYS");
		      else if ( calendar == CALENDAR_366DAYS )
			fprintf(stdout, "  Calendar = 366DAYS");
		      else
			fprintf(stdout, "  Calendar = unknown");
		    }

		  fprintf(stdout, "\n");
		}
	    }

	  fprintf(stdout, "  time verification time        lower bound        upper bound\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
	  fprintf(stdout, "  step  YYYY-MM-DD hh:mm   YYYY-MM-DD hh:mm   YYYY-MM-DD hh:mm\n");
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

	  tsID = 0;
	  while ( (nrecs = streamInqTimestep(streamID, tsID)) )
	    {
	      vdate = taxisInqVdate(taxisID);
	      vtime = taxisInqVtime(taxisID);

	      decode_date(vdate, &year, &month, &day);
	      decode_time(vtime, &hour, &minute);

	      tsID++;
	      fprintf(stdout, " %5d %4.4d-%2.2d-%2.2d %2.2d:%2.2d", tsID, year, month, day, hour, minute);
	      fprintf(stdout, "\n");
	    }
	}
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  else if ( operatorID == VCT )
    {
      for ( index = 0; index < nzaxis; index++)
	{
	  zaxisID = vlistZaxis(vlistID, index);
	  type = zaxisInqType(zaxisID);
	  if ( type == ZAXIS_HYBRID )
	    {
	      int i, vctsize;
	      const double *vct;

	      vctsize = zaxisInqVctSize(zaxisID);
	      vct     = zaxisInqVctPtr(zaxisID);
		
194
195
196
197
198
199
	      if ( vctsize%2 == 0 )
		for ( i = 0; i < vctsize/2; i++ )
		  fprintf(stdout, "%5d %25.17f %25.17f\n", i, vct[i], vct[vctsize/2+i]);
	      else
		for ( i = 0; i < vctsize; i++ )
		  fprintf(stdout, "%5d %25.17f\n", i, vct[i]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
201
202
203
204
205
206
	    }
	}
    }
  else if ( operatorID == VLIST )
    {
      vlistPrint(vlistID);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
  else if ( operatorID == PARDES )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    {
      int varID, code;
      char varname[128], varlongname[128], varunits[128];

      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");
	}   
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
231
232
233
  else if ( operatorID == PARTAB )
    {
      int varID, code, tabnum, tableID, prec;
234
      char pstr[4];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235
236
237
238
      char varname[128], varlongname[128], varstdname[128], varunits[128];

      for ( varID = 0; varID < nvars; varID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
	  fprintf(stdout, "&PARAMETER\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
241
242
243
244
245
246
247
248
249
250
251
252

	  varname[0]     = 0;
	  varlongname[0] = 0;
	  varunits[0]    = 0;
	  code     = vlistInqVarCode(vlistID, varID);
	  tableID  = vlistInqVarTable(vlistID, varID);
	  tabnum   = tableInqNum(tableID);
	  vlistInqVarName(vlistID, varID, varname);
	  vlistInqVarStdname(vlistID, varID, varstdname);
	  vlistInqVarLongname(vlistID, varID, varlongname);
	  vlistInqVarUnits(vlistID, varID, varunits);

	  prec = vlistInqVarDatatype(vlistID, varID);
253
254
255
256
257
258
259
260
	  if      ( prec == DATATYPE_PACK   ) strcpy(pstr, "P0");
	  else if ( prec > 0 && prec <= 32  ) sprintf(pstr, "P%d", prec);
	  else if ( prec == DATATYPE_FLT32  ) strcpy(pstr, "F32");
	  else if ( prec == DATATYPE_FLT64  ) strcpy(pstr, "F64");
	  else if ( prec == DATATYPE_INT8   ) strcpy(pstr, "I8");
	  else if ( prec == DATATYPE_INT16  ) strcpy(pstr, "I16");
	  else if ( prec == DATATYPE_INT32  ) strcpy(pstr, "I32");
	  else                                strcpy(pstr, "-1");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261

Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
	  if ( code   > 0 ) fprintf(stdout, "  CODE=%d\n", code);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
263
	  if ( tabnum > 0 ) fprintf(stdout, "  TABLE=%d\n", tabnum);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
	  fprintf(stdout, "  NAME=%s\n", varname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
	  if ( strlen(varstdname) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
266
	    fprintf(stdout, "  STANDARD_NAME=%s\n", varstdname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267
268
269
270
271
	  if ( strlen(varlongname) )
	    fprintf(stdout, "  LONG_NAME=\"%s\"\n", varlongname);
	  if ( strlen(varunits) )
	    fprintf(stdout, "  UNITS=\"%s\"\n", varunits);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
	  /* if ( pstr ) fprintf(stdout, "  DATATYPE=%s\n", pstr); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273

274
	  fprintf(stdout, "/\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
276
	}   
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  else if ( operatorID == FILEDES )
    {
      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_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;
318
319
320
321
322
323
324
325
326
327
328
329
	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;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330
331
332
333
334
335
336
337
338
339
340
341
342
	default:
	  printf("  unsupported filetype %d\n" , filetype);
	}

      printf("\n");
    }

  streamClose(streamID);

  cdoFinish();

  return (0);
}