Command.c 7.23 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

  Copyright (C) 2003-2010 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
  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.
*/

/*
   This module contains the following operators:
*/

#include <ctype.h>  /* isspace */

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

int streamID = 0;
int vlistID = 0;
int varID = 0;
int levelID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
34
int tsID1 = 0;
int tsID2 = 59;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
double *array = NULL;

#define MAX_LINE 256

int Done = 0;

int com_help(char *);
int com_list(char *);
int com_quit(char *);
int com_stat(char *);
int com_vars(char *);
//int com_stat(char *);


typedef struct {
  char  *name; /* User printable name of the function. */
  int  (*func)(char *); /* Function to call to do the job. */
  char  *doc; /* Documentation for this function. */
}
command_t;

command_t commands[] = {
  { "help", com_help, "Display this text" },
  { "?",    com_help, "Synonym for 'help'" },
  { "list", com_list, "List files in DIR" },
  { "quit", com_quit, "Quit using CDO" },
  { "stat", com_stat, "Statistic for selected field" },
  { "vars", com_vars, "list variables" },
  //  { "stat", com_stat, "Print out statistics on FILE" },
  { NULL, NULL, NULL }
};


/* Return non-zero if ARG is a valid argument for CALLER, else print
an error message and return zero. */
int valid_argument (char *caller, char *arg)
{
  if (!arg || !*arg)
    {
      fprintf (stderr, "%s: Argument required.\n", caller);
      return (0);
    }
  return (1);
}

/* Print out help for ARG, or for all of the commands if ARG is not present. */
int com_help(char *arg)
{
  int i;
  int printed = 0;

  for ( i = 0; commands[i].name; i++ )
    {
      if (!*arg || (strcmp (arg, commands[i].name) == 0))
	{
	  printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
	  printed++;
	}
    }

  if ( !printed )
    {
      printf ("No commands match â%sâ. Possibilties are:\n", arg);
      for (i = 0; commands[i].name; i++)
	{
	  /* Print in six columns. */
	  if ( printed == 6 )
	    {
	      printed = 0;
	      printf ("\n");
	    }
	  printf("%s\t", commands[i].name);
	  printed++;
	}

      if (printed) printf ("\n");
    }

  return (0);
}


/* List the file(s) named in arg. */
int com_list(char *arg)
{
  if (!arg)
    arg = "";

  return (0);
}

/* The user wishes to quit using this program. Just set DONE non-zero. */
int com_quit(char *arg)
{
  Done = 1;

  return (0);
}


int com_stat(char *arg)
{
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
  int tsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139

Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
  for ( tsID = tsID1; tsID <= tsID2; ++tsID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
      nrecs = streamInqTimestep(streamID, tsID);
      if ( nrecs == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
	  fprintf(stderr, "Timestep %d out of range!\n", tsID+1);
	  break;
	}
      else
	{
	  int i;
	  int nmiss;
	  int gridsize;
	  double fmin = 1.e50 , fmax = -1.e50, fmean = 0;
	  counter_t counter;
	  
	  counter_start(&counter);
	  streamReadVarSlice(streamID, varID, levelID, array, &nmiss);
	  gridsize = gridInqSize(vlistInqVarGrid(vlistID, varID));
	  for ( i = 0; i < gridsize; ++i )
	    {
	      if ( array[i] < fmin ) fmin = array[i];
	      if ( array[i] > fmax ) fmax = array[i];
	      fmean += array[i];
	    }
	  fmean /= gridsize;
	  counter_stop(&counter);
	  
	  fprintf(stdout, "%d %d %d %g %g %g (%gs)\n",
		  tsID+1, varID+1, levelID+1, fmin, fmean, fmax,
		  counter_cputime(counter));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
172
173
174
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
	}
    }
  return (0);
}


int com_vars(char *arg)
{
  int varID;
  int nvars = vlistNvars(vlistID);

  if (!arg)
    arg = "";
  printf("com_vars: %s %d\n", arg, nvars);

  for ( varID = 0; varID < nvars; ++varID )
    {
      fprintf(stdout,"varID %d\n", varID);
    }

  return (0);
}

/* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */
command_t *find_command(char *name)
{
  int i;

  for ( i = 0; commands[i].name; i++ )
    if ( strcmp(name, commands[i].name) == 0)
      return (&commands[i]);

  return ((command_t *)NULL);
}

/* Execute a command line. */
int execute_line(char *line)
{
  int i;
  command_t *command;
  char *word;

  /* Isolate the command word. */
  i = 0;
  while ( line[i] && isspace(line[i]))  i++;
  word = line + i;
  while ( line[i] && !isspace(line[i])) i++;

  if ( line[i] ) line[i++] = '\0';

  command = find_command(word);
  if ( !command )
    {
      fprintf (stderr, "%s: No such command!\n", word);
      return (-1);
    }
  /* Get argument to command, if any. */
  while ( isspace(line[i]) ) i++;

  word = line + i;
  /* Call the function. */
  return ((*(command->func)) (word));
}

/* Strip isspace from the start and end of STRING. Return a pointer into STRING. */
char *stripwhite(char *string)
{
  char *s, *t;
  for (s = string; isspace(*s); s++)
    ;
  if (*s == 0)
    return (s);
  t = s + strlen (s) - 1;
  while (t > s && isspace(*t))
    t--;
  *++t = '\0';

  return s;
}


void readcmd(const char *prompt, char *line, int size)
{
  fputs(prompt, stdout);
  fflush(stdout);

  *line = '\0';
  if ( fgets(line, size, stdin) )
    {
      char *newline = strchr(line, '\n'); /* check for trailing '\n' */
      if ( newline )
	*newline = '\0'; /* overwrite the '\n' with a terminating null */
    }
}



void *Command(void *argument)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
  static const char *func = "Command";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
  int recID, varID, levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
274
275
276
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  int gridsize;
  int nmiss;
  int taxisID;
  double s_utime, s_stime;
  double e_utime, e_stime;
  double c_cputime = 0, c_usertime = 0, c_systime = 0;
  char line[MAX_LINE];
  char *s;
 
  cdoInitialize(argument);

  processStartTime(&s_utime, &s_stime);

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

  gridsize = vlistGridsizeMax(vlistID);
  array = (double *) malloc(gridsize*sizeof(double));

  /* Loop reading and executing lines until the user quits. */
  while ( !Done )
    {
      readcmd("cdo cmd> ", line, MAX_LINE);

      /* Remove leading and trailing whitespace from the line.
	 Then, if there is anything left, add it to the history list
	 and execute it. */
      s = stripwhite(line);
      if ( *s ) execute_line(s);
    }


/*
  while ( readline(stdin, line, MAX_LINE) )
    {
      linep = line;
      
      if      ( strcmp(linep, "exit") == 0 ) break;
      else if ( strcmp(linep, "vars") == 0 )
	{
	  int varID;
	  int nvars = vlistNvars(vlistID);
	  for ( varID = 0; varID < nvars; ++nvars )
	    {
	      fprintf(stdout,"varID %d\n", varID);
	    }
	}
    }
*/
  
  streamClose(streamID);

  if ( array ) free(array);
  
  cdoProcessTime(&e_utime, &e_stime);

  c_usertime = e_utime - s_utime;
  c_systime  = e_stime - s_stime;
  c_cputime  = c_usertime + c_systime;

  s_utime = e_utime;
  s_stime = e_stime;

  cdoPrint("%.2fs %.2fs %.2fs", c_usertime, c_systime, c_cputime);

  cdoFinish();

  return (0);
}