File: rdm_data.c

package info (click to toggle)
plotmtv 1.4.1-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,024 kB
  • ctags: 5,006
  • sloc: ansic: 51,179; makefile: 1,976; fortran: 1,277; sh: 510; csh: 439
file content (519 lines) | stat: -rw-r--r-- 18,420 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
163
164
165
166
167
168
169
170
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
271
272
273
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * rdm_data.c - read MTV-format data from a file 
 *
 * The data is read and saved in the
 * "CNdataset" data-structure, pointed to by "CNdatasetptr".  
 *
 * The datasets are assumed to be separated using a $DATA=xxx line
 */

/*
 * #ifdef INTEL_ONLY are used to denote sections of code that have been
 * disabled.  These are primarily the hierarchical mesh formats which
 * are specialized for process/device modeling
 * Also the XDR machine-independent format has been disabled.
 *
 * The ifdef statements are left here to allow easier comparison with
 * intel specific code which is not available for public distribution
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "CNplot.h"

void CNread_plotmtv();
void CNread_plotmtv_from_file();
void CNread_plotmtv_from_pipe();
void CNread_plotmtv_data();
static int read_datatype();
static int read_data();
static int CNmtv_read_unrecog_data();


/*
 * read the ascii/binary data from a file/pipe
 * return NULL upon failure
 *
 * A single data-file or stream could contain more than one dataset.
 * This collection of routines reads the data and puts each separate
 * set of data into a linked list of datasets.  The global_dataset
 * linked list is used ONLY for reading mesh quantities, which require 
 * a matching mesh structure (presumably inside the global list).
 */
void CNread_plotmtv(filename, fp, source, 
                    global_datahead, global_datatail,
                    datahead, datatail, dataID, verbose)
char         *filename;        /* The name of the file                 */
FILE         *fp;              /* The pipe descriptor                  */
int          source;           /* Data source : CN_FILE or CN_PIPE     */
CNdatasetptr global_datahead;  /* Pointers to existing plot data       */
CNdatasetptr global_datatail;  /* Pointers to existing plot data       */
CNdslistptr  *datahead;        /* Pointers to plot data                */
CNdslistptr  *datatail;        /* Pointers to plot data                */
int          *dataID;          /* data ID                              */
int          verbose;          /* Verbosity flag                       */
{
   /* Initialize */
   *datahead = NULL;
   *datatail = NULL;

   /* Branch according to file/pipe source */
   if (source == CN_FILE) {

      /* Read from file */
      CNread_plotmtv_from_file(filename, 
                               global_datahead, global_datatail,
                               datahead, datatail, dataID, verbose);

   } else {

      /* Read from pipe */
      CNread_plotmtv_from_pipe(fp, 
                               global_datahead, global_datatail,
                               datahead, datatail, dataID, verbose);

   }

}


/*
 * read the data from a file 
 * return NULL upon failure
 */
void CNread_plotmtv_from_file(filename, 
                              global_datahead, global_datatail,
                              datahead, datatail, dataID, verbose)
char         *filename;        /* The name of the file                 */
CNdatasetptr global_datahead;  /* Pointers to existing plot data       */
CNdatasetptr global_datatail;  /* Pointers to existing plot data       */
CNdslistptr  *datahead;        /* Pointers to plot data                */
CNdslistptr  *datatail;        /* Pointers to plot data                */
int          *dataID;          /* data ID                              */
int          verbose;          /* Verbosity flag                       */
{
   FILE         *fp;
   int          fp_ispipe=0;

   /* open the file */
   if (!CNopen_file(filename,&fp,&fp_ispipe)) return;

   /* Now process the file */
   CNread_plotmtv_data(fp,filename,
                       global_datahead, global_datatail,
                       datahead,datatail,dataID,verbose);

   /* close the file */
   CNclose_file(fp, fp_ispipe);

   /* Print out warning message */
   if (*datahead == NULL) 
   (void) fprintf(stderr,"   ***Error! PlotMTV file-read was unsuccessful.\n");
}


/* 
 * read the data from a pipe 
 * return NULL upon failure
 */
void CNread_plotmtv_from_pipe(fp, 
                              global_datahead, global_datatail,
                              datahead, datatail, dataID, verbose)
FILE         *fp;              /* The pipe-descriptor                  */
CNdatasetptr global_datahead;  /* Pointers to existing plot data       */
CNdatasetptr global_datatail;  /* Pointers to existing plot data       */
CNdslistptr  *datahead;        /* Pointers to plot data                */
CNdslistptr  *datatail;        /* Pointers to plot data                */
int          *dataID;          /* data ID                              */
int          verbose;          /* Verbosity flag                       */
{
   /* check the pipe */
   if (fp == NULL) {
      (void) fprintf(stderr,"Error! Cannot read from NULL pipe!\n");
      return;
   }

   /* Now process the pipe */
   CNread_plotmtv_data(fp,"PIPE",
                       global_datahead, global_datatail,
                       datahead,datatail,dataID,verbose);

   /* Print out warning message */
   if (*datahead == NULL) 
   (void) fprintf(stderr,"   ***Error! PlotMTV pipe-read was unsuccessful.\n");
}

#define XDRDATA -3
#define QUIT    -2
#define UNDEFN   0
#define CURVE2D  1
#define CURVE3D  2
#define CONTCURV 3
#define RECTGRID 4
#define GRID4D   5
#define MESH4D   6
#define POLYGON  7
#define VECTOR   8
#define MESH     9
#define COLUMN   10
#define PROBAB   11
#define HISTOG   12
#define BARCHART 13
#define UNRECOG  14

/* 
 * read in the data 
 * return NULL upon failure
 */
void CNread_plotmtv_data(fp, filename, 
                         global_datahead, global_datatail,
                         datahead, datatail, dataID, verbose)
FILE         *fp;              /* File pointer                         */
char         *filename;        /* The name of the file/pipe source     */
CNdatasetptr global_datahead;  /* Pointers to existing plot data       */
CNdatasetptr global_datatail;  /* Pointers to existing plot data       */
CNdslistptr  *datahead;        /* Pointers to plot data                */
CNdslistptr  *datatail;        /* Pointers to plot data                */
int          *dataID;          /* Data ID                              */
int          verbose;          /* Verbosity flag                       */
{
   char         header[BUFSIZ];
   char         dataname[BUFSIZ];
   int          lineno = 0, datatype, status=0;
   int          datacount=0;

   /*
    * The file format consists of :
    *    $ DATA=XX            # header lines
    *    x y                  # data
    *    # comments
    *    % options
    * Everytime a "$" sign is encountered the routine branches to
    * the appropiate reader.  The reader is passed the header arguments 
    * and the file-pointer, and it ends when a new line starting with
    * a "$" sign is encountered.
    */

   /* Print out some information */
   (void) fprintf(stdout,"\nReading MTV data from file \"%s\" : \n",filename);

   /* Count the initial number of datasets - should be an empty list */
   datacount = CNcount_dslists(*datahead, *datatail);

   /*
    * Use CNread_header() to scan through the lines
    * Find the first header ("$") or the first occurence of a '$' or
    * an alphanumeric character is encountered.
    * The '$' and alphanumeric characters are put back on the stream.
    */
   (void) strcpy(header,"");
   while ((status=CNread_header(fp,&lineno,header,BUFSIZ))!=EOF && status)
      /*EMPTY*/
      ;

   while (status != EOF && status != QUIT && status != XDRDATA) {
      /* Read the data-type */
      (void) strcpy(dataname, CN_NONAME);
      status=read_datatype(header,&datatype,dataname,verbose);
      if (status == QUIT) continue;
      if (status == XDRDATA) continue;

      /* Check the dataname */
      if (strcmp(dataname, CN_NONAME)==0) 
      (void) sprintf(dataname,"%s.%d",CNstring_concat(filename),
                     (*dataID)-datacount);

      /* Read the data - this stops at EOF or at another "$ xxx " line */
      status = read_data(fp, filename, &lineno, header,
                         global_datahead, global_datatail,
                         datahead, datatail, dataID,
                         datatype, dataname, verbose);
   } /* End while */

#ifdef INTEL_ONLY
   if (status == XDRDATA) {
      /*
       * Read in XDR format 
       */
      CNread_plotxdr_data(fp,filename,
                       global_datahead, global_datatail,
                       datahead,datatail,dataID,verbose);
   }
#endif

   /* Print more information */
   (void) fprintf(stdout,"Found %d datasets.\n",
                  CNcount_dslists(*datahead, *datatail)-datacount);
}

typedef struct {
  char *label;
  int   value;
} MPdata_format;

static MPdata_format format_pairs[] = {
   {"curve2d"      , CURVE2D },
   {"2d"           , CURVE2D },
   {"twod"         , CURVE2D },
   {"curve3d"      , CURVE3D },
   {"3d"           , CURVE3D },
   {"threed"       , CURVE3D },
   {"contcurv"     , CONTCURV},
   {"contcurve"    , CONTCURV},
   {"contourcurve" , CONTCURV},
   {"contour-curve", CONTCURV},
   {"contour"      , RECTGRID},
   {"rectgrid"     , RECTGRID},
   {"grid4d"       , GRID4D  },
   {"4d"           , GRID4D  },
#ifdef INTEL_ONLY
   {"mesh4d"       , MESH4D  },
   {"stride"       , MESH4D  },
#endif
   {"poly"         , POLYGON },
   {"polygon"      , POLYGON },
   {"vector"       , VECTOR  },
   {"vect"         , VECTOR  },
#ifdef INTEL_ONLY
   {"mesh"         , MESH    },
   {"mtvmesh"      , MESH    },
   {"pifmesh"      , MESH    },
#endif
   {"column"       , COLUMN  },
   {"prob"         , PROBAB  },
   {"probab"       , PROBAB  },
   {"probability"  , PROBAB  },
   {"hist"         , HISTOG  },
   {"histog"       , HISTOG  },
   {"histogram"    , HISTOG  },
   {"bar"          , BARCHART},
   {"barchart"     , BARCHART},
};

/*
 * Read the data-type 
 */
static int read_datatype(header,datatype,dataname,verbose)
char *header;     /* header ($data=xxx) */
int  *datatype;   /* Data type          */
char *dataname;   /* Dataset name/label */
int  verbose;     /* Verbose flag       */
{
   char *argtbl[CN_MAXWORDS], *valtbl[CN_MAXWORDS];
   char newheader[CN_MAXCHAR];
   int  nargs = 0, nvals = 0;
   int  i, j, npairs, status=0, format_found=CN_FALSE, dataspec=CN_FALSE;

   /* Initialize the datatype */
   *datatype = CURVE2D;

   /* CNparse_line wants "command arg=val arg=val" so create a new header */
   (void) sprintf(newheader, "datafile %s",header);

   /* Get the argument-value pairs from the line */
   if (CNparse_line(newheader, CN_MAXCHAR,
                    &nargs, argtbl, CN_MAXWORDS,
                    &nvals, valtbl, CN_MAXWORDS)) {

      /* Go thru the arg-val pairs */
      for (i=0; i<nargs; i++) {
         /*
         (void)fprintf(stdout,"argument=%s value=%s\n",argtbl[i],valtbl[i]);
          */

         if ((strcmp(argtbl[i],"end")==0) || (strcmp(argtbl[i],"quit")==0))
            status = QUIT;

#ifdef INTEL_ONLY
         if (strncmp(argtbl[i],"xdr",3)==0)
            status = XDRDATA;
#endif

         else if (strcmp(argtbl[i],"name")==0) 
            CNassign_string_keyword(dataname,valtbl[i],argtbl[i],verbose);

         else if (strncmp(argtbl[i],"data",4)==0) {
            dataspec = CN_TRUE;
            CNstring_to_lower(valtbl[i]);
            /* Do the comparison */
            format_found = CN_FALSE;
            npairs = sizeof(format_pairs)/sizeof(MPdata_format);
            for (j=0; j<npairs && !format_found; j++) {
               if (strcmp(valtbl[i],format_pairs[j].label)==0) {
                  *datatype = format_pairs[j].value;
                  format_found = CN_TRUE;
               }
            }
            if (!format_found) {
               (void) fprintf(stdout,
               "\n   ***Warning : unrecognized data-type \"%s\"", valtbl[i]);
               *datatype = UNRECOG; 
            }
         }
      }

      /* Clear the tables */
      CNfreewords(&nargs, argtbl);
      CNfreewords(&nvals, valtbl);
   }

   /* Read the first argument-value pair following the % */
   if (verbose && !dataspec && status!=QUIT && status!=XDRDATA) {
      (void) fprintf(stdout,"   Warning : No data-type specified! : ");
      (void) fprintf(stdout,"Assuming CURVE2D format...\n");
      *datatype = CURVE2D;
   }

   /* Return the status (QUIT) */
   return (status);
}

/* 
 * Read the data 
 */
static int read_data(fp, filename, lineno, header,
                     global_datahead, global_datatail,
                     datahead, datatail, dataID,
                     datatype, dataname, verbose)
FILE         *fp;              /* The file/pipe source                 */
char         *filename;        /* The name of the file/pipe source     */
int          *lineno;          /* Current line number                  */
char         *header;          /* Header string                        */
CNdatasetptr global_datahead;  /* Pointers to existing plot data       */
CNdatasetptr global_datatail;  /* Pointers to existing plot data       */
CNdslistptr  *datahead;        /* Pointers to plot data                */
CNdslistptr  *datatail;        /* Pointers to plot data                */
int          *dataID;          /* Data ID                              */
int          datatype;         /* Type of data                         */
char         *dataname;        /* Name/Label of data                   */
int          verbose;          /* Verbosity Flag                       */
{
   CNdatasetptr Dptr=NULL;
   int          status;
   int          stopatE = CN_FALSE;

   /*
    * Branch according to the reader 
    * Mesh-type datasets are treated differently because multiple
    * datasets could be contained in a single data definition
    * (mesh structure and multiple quantities)
    */
   switch (datatype) {
   case CURVE2D  : status = CNmtv_read_curve2D_data  (&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
   case CURVE3D  : status = CNmtv_read_curve3D_data  (&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
   case CONTCURV : status = CNmtv_read_contcurve_data(&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
   case RECTGRID : status = CNmtv_read_contour_data  (&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
   case GRID4D   : status = CNmtv_read_grid4D_data   (&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
#ifdef INTEL_ONLY
   case MESH4D   : status = CNmtv_read_mesh4D_data(
                            datahead, datatail,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   Dptr   = NULL;  /* Don't do anything more to the data */
                   break;
#endif
   case POLYGON  : status = CNmtv_read_polygon_data(&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
   case VECTOR   : status = CNmtv_read_vector_data   (&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
#ifdef INTEL_ONLY
   case MESH     : status = CNmtv_read_mtvmesh_data  (
                            global_datahead, global_datatail,
                            datahead, datatail,
                            dataID, dataname, filename,
                            fp,lineno,header,stopatE,verbose); 
                   Dptr   = NULL;  /* Don't do anything more to the data */
                   break;
#endif
   case COLUMN   : status = CNmtv_read_column_data(
                            datahead, datatail,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   Dptr   = NULL;  /* Don't do anything more to the data */
                   break;
   case PROBAB   : status = CNmtv_read_probab_data(&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
   case HISTOG   : status = CNmtv_read_histogram_data(&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
   case BARCHART : status = CNmtv_read_barchart_data(&Dptr,
                            dataID, dataname, filename,
                            fp,lineno,header,verbose); 
                   break;
   case UNRECOG  : status = CNmtv_read_unrecog_data(fp,lineno,header);
                   break;
   default       : (void) fprintf(stderr,
                   "Error! Unrecognized data-type \"%d\n",datatype);
                   break;
   }
 
   /* Store the dataset in the local linked list */
   if (Dptr != NULL) (void) CNinsert_dslist(datahead, datatail, Dptr);
 
   /* Return the status */
   return(status);
}


/*
 * Just go thru the lines until we get to a $ or EOF
 */
static int CNmtv_read_unrecog_data(fp,lineno,header)
FILE         *fp;            /* The file/pipe source                 */
int          *lineno;        /* Current line number                  */
char         *header;        /* Header string                        */
{
   char        line[BUFSIZ];
   int         header_found  = CN_FALSE;
   int         len;

   /* Print info */
   (void) fprintf(stdout,"\n   Skipping UNKNOWN data...(line %d)\n",*lineno);

   /* Keep on reading until a "$" is encountered */
   while (!header_found && CNgetucline(fp, line, lineno, BUFSIZ) != EOF) {

      /*EMPTY*/
      if (((len=strlen(line)) == 0) || (len==1 && line[0]=='\n')) {
         /* Ignore newlines */
         ;
 
      } else if (line[0] == '$') {
         /* Header */
         line[0] = ' ';
         (void) strcpy(header, line);
         header_found = CN_TRUE;

      } 
   } 

   /* return */
   if (!header_found)
      return(EOF);
   else
      return(header_found);
}