1 /*  Copyright (C) 1988-2005 by Brian Doty and the Institute
2                   of Global Environment and Society (IGES).
3 
4     See file COPYRIGHT for more information.   */
5 
6 /* Authored by B. Doty */
7 
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 
11 /* If autoconfed, only include malloc.h when it's presen */
12 #ifdef HAVE_MALLOC_H
13 #include <malloc.h>
14 #endif
15 
16 #else /* undef HAVE_CONFIG_H */
17 
18 #include <malloc.h>
19 
20 #endif /* HAVE_CONFIG_H */
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <math.h>
25 #include "grads.h"
26 #include "gx.h"
27 
28 #if USESDF == 1
29 #include <netcdf.h>
30 #endif
31 
32 #if USEGUI == 1
33 #include "gagui.h"
34 #endif
35 
36 #if USEIMG >= 1
37 #include "gaimg.h"
38 #endif
39 
40 #if USELATS == 1
41 #include "lats.h"
42 #endif
43 
44 /* int gxhpng (char *, int, int, int, int); */
45 
46 /*mf 971022 --- expose Mike Fiorino's global struct to these routines for warning level setting mf*/
47 extern struct gamfcmn mfcmn;
48 
49 static char *cdims[6] = {"None","Lon","Lat","Lev","Time","Val"};
50 static char *ccdims[5] = {"Xdim","Ydim","Zdim","Tdim","Val"};
51 
52 static char pout[256];   /* Build error msgs here */
53 static char chfoo = 'A';        /* check for overrun */
54 static struct gacmn *savpcm;
55 
56 /*  Variables to handle message buffering for the script language */
57 
58 static int msgflg = 0;
59 struct msgbuf {
60   struct msgbuf *forw;
61   int levl;
62   int len;
63   char *msg;
64 };
65 static struct msgbuf *msgstk, *msgcurr, *msgnew;
66 
67 /* Handle all user commands */
68 
gacmd(char * com,struct gacmn * pcm,int exflg)69 int gacmd (char *com, struct gacmn *pcm, int exflg) {
70 struct gafile *pfi,*pfi2;
71 struct gadefn *pdf,*pdf2;
72 char cc[260], bgImage[256], fgImage[256];
73 int rc,reinit,fnum,i,len,retcod,flag,xin,yin,bwin,gifflg,tcolor;
74 char *cmd,*rslt,*ccc,*ch;
75 
76 #if USESDF == 1
77 /* Prototypes for functions to handle SDFOPEN & XDFOPEN keywords */
78     int gasdfopen(char *args, struct gacmn *pcm) ;
79     int gaxdfopen(char *args, struct gacmn *pcm) ;
80 #endif
81 
82   ccc = NULL;
83   gaiomg();   /* enable interpolation message */
84 
85   len = 0;
86   while(*(com+len)) len++;
87   len++;
88   ccc = (char *)malloc(len+1);
89   if (ccc==NULL) {
90     gaprnt(0,"Memory allocation error: Command Proecessing\n");
91     return(1);
92   }
93   for (i=0; i<len; i++) *(ccc+i) = *(com+i);
94   *(ccc+len) = '\0';          /* Maintain mixed case version */
95   cmd = ccc;
96   lowcas (cmd);
97   while (*cmd==' ') cmd++;
98   while (*com==' ') com++;
99 
100   retcod = 0;
101 
102   /* Check for implied define */
103 
104   flag = 0;
105   if (*cmd>='a' && *cmd<='z') {
106     i = 0;
107     ch = cmd;
108     while ( (*ch>='a' && *ch<='z') || (*ch>='0' && *ch<='9' ) ) {
109       i++;
110       if (i>16) break;
111       ch++;
112     }
113     if (i<17) {
114       while (*ch==' ') ch++;
115       if (*ch=='=') {
116         flag = 1;
117         ch++;
118         while (*ch==' ') ch++;
119       }
120     }
121     if (flag) {
122       if (pcm->pfid==NULL) {
123         gaprnt (0,"DEFINE error:  no file open yet\n");
124         retcod = 1;
125         goto retrn;
126       }
127       retcod = gadef (cmd, pcm, 1);
128       goto retrn;
129     }
130   }
131 
132   if ( !(cmpwrd("clear",cmd) || cmpwrd("c",cmd)) ) gxfrme (9);
133   if (*com=='\0' || *com=='\n') goto retrn; /* Just return if null */
134 
135   if (cmpwrd("quit",cmd)) {
136      retcod = -1;
137      goto retrn;
138   } else if (*cmd=='!') {
139     system(com+1);
140     goto retrn;
141 #ifdef MM_READLINE
142 #if READLINE == 1
143     /*
144      * print history or repeat commands from history
145      */
146   }  else if ( cmpwrd("history", cmd) ||
147                cmpwrd("his",     cmd) ||
148 	       cmpwrd("repeat",  cmd) ||
149 	       cmpwrd("r",       cmd)    ) {
150     retcod=gahistory(cmd, com, pcm);
151     goto retrn;
152 #endif
153 #endif
154 #if USEGUI == 1
155   } else if (cmpwrd("gui",cmd)) {
156     char *tmp ;
157     if ( (cmd=nxtwrd(com)) == NULL) {
158       gaprnt (0,"GUI error:  No file name specified\n");
159       retcod = 1;
160       goto retrn;
161     } else {
162       int lentmp ;
163 
164       lentmp = (int) strlen(cmd) ;
165       tmp = (char *) malloc((size_t) lentmp + 1) ;
166       getwrd(tmp, cmd, lentmp) ;
167       retcod = Custom_GUI(tmp);
168       if(tmp) free(tmp);
169       goto retrn;
170     }
171 #endif
172 #if USEIMG >= 1
173   } else if (cmpwrd("wi",cmd) ) {
174     char *tmp,*tmpopt ;
175     if ( (cmd=nxtwrd(com)) == NULL) {
176       gaprnt (0,"IMG Error:  No file name specified\n");
177       retcod = 1;
178       goto retrn;
179     } else {
180       int lentmp ;
181       lentmp = (int) strlen(cmd) ;
182       tmp = (char *) malloc((size_t) lentmp + 1) ;
183       getwrd(tmp, cmd, lentmp) ;
184 
185 /*mf 981115
186   get the format type; default is GIF
187   note that nxtwrd is parsing cmd vice com as above, this is how
188   you rip out individual words in a multi word option (comes in as com)
189 mf*/
190 
191       if ( (cmd=nxtwrd(cmd)) == NULL) {
192 	tmpopt=(char *) malloc((size_t) 4);
193 	strcpy(tmpopt,"GIF");
194       } else {
195 	int lentmp ;
196 	lentmp = (int) strlen(cmd) ;
197 	tmpopt = (char *) malloc((size_t) lentmp + 1) ;
198 	getwrd(tmpopt, cmd, lentmp) ;
199       }
200     }
201 
202     retcod = img_write (tmp,tmpopt);
203     if(tmp) free(tmp);
204     if(tmpopt) free(tmpopt);
205     goto retrn;
206 
207 #endif
208   } else if (cmpwrd("stack",cmd)) {
209     goto retrn;
210   } else if (cmpwrd("flush",cmd)) {
211     goto retrn;
212   } else if (cmpwrd("reset",cmd) || cmpwrd("reinit",cmd)) {
213     pcm->xsiz = pcm->pxsize;
214     pcm->ysiz = pcm->pysize;
215     gxvpag (pcm->xsiz, pcm->ysiz, 0.0, pcm->xsiz, 0.0, pcm->ysiz);
216     gainit();
217     gacln (pcm,1);
218     reinit = 0;
219     if (cmpwrd("reinit",cmd)) {
220       reinit = 1;
221       pdf = pcm->pdf1;
222       while (pdf) {
223         pdf2 = pdf->pforw;
224         pfi = pdf->pfi;
225         free (pfi->rbuf);
226         free (pfi);
227         free (pdf);
228         pdf = pdf2;
229       }
230       pfi = pcm->pfi1;
231       while (pfi) {
232                         /* infile may be null, with dods */
233         if (pfi->infile) fclose (pfi->infile);
234         if (pfi->mfile) fclose(pfi->mfile);
235 #if USESDF == 1
236         if (pfi->sdf_ptr) close_netcdf(pfi->sdf_ptr->cdfid) ;
237 	if (pfi->ncid != -999)  ncclose(pfi->ncid) ;  /* no error checking */
238 #if USEHDF == 1
239 	if (pfi->sdid != -999)  SDend(pfi->sdid) ;    /* no error checking */
240 #endif
241 #endif
242 	if (pfi->bufrdset) {            /* bufr station data */
243 	  gabufr_close(pfi->bufrdset);  /* free memory */
244 	  pfi->bufrdset=NULL;           /* reset the pointer */
245 	}
246 #if USEGADODS
247         if (pfi->dhandle > -999) dodclo(pfi);  /* dodstn */
248 #endif
249         pfi2 = pfi->pforw;
250         frepfi(pfi,0);
251         pfi = pfi2;
252       }
253       pcm->pfi1 = NULL;
254       pcm->pfid = NULL;
255       pcm->fnum = 0;
256       pcm->dfnum = 0;
257       pcm->pdf1 = NULL;
258       pcm->grflg = 0;
259       pcm->devbck = 0;
260       if (pcm->ffile) fclose(pcm->ffile);
261       pcm->ffile = NULL;
262       if (pcm->fwname) free(pcm->fwname);
263       pcm->fwname = NULL;
264       pcm->fwenflg = BYTEORDER;
265       gxhend();
266       gxdbck(pcm->devbck);
267       gxgrey(pcm->grflg);
268       gaprnt (1,"All files closed; all defined objects released\n");
269     }
270     if (pcm->fnum>0 && pcm->pfi1) {
271       pcm->pfid = pcm->pfi1;
272       pcm->dfnum = 1;
273       pfi = pcm->pfi1;
274       if (pfi->type==2 || pfi->wrap) gacmd ("set lon 0 360",pcm,0);
275       else {
276         sprintf (pout,"set x 1 %i",pfi->dnum[0]);
277         gacmd (pout,pcm,0);
278       }
279       if (pfi->type==2) {
280         gacmd ("set lat -90 90",pcm,0);
281         gacmd ("set lev 500",pcm,0);
282       } else {
283         sprintf (pout,"set y 1 %i",pfi->dnum[1]);
284         gacmd (pout,pcm,0);
285 
286 /*mf --- set z to max if x or y = 1 970729 mf*/
287 
288 	if(pfi->type==1 && pfi->dnum[2] > 1
289 	   && ( (pfi->dnum[0] == 1) || (pfi->dnum[1] == 1) ) ) {
290 	  if(pfi->dnum[2] <= 1) {
291 	    sprintf (pout,"set z 1");
292 	  } else {
293 	    sprintf (pout,"set z 1 %i",pfi->dnum[2]);
294 	  }
295 	  gacmd (pout,pcm,0);
296 	} else {
297 	  gacmd ("set z 1",pcm,0);
298 	}
299       }
300       gacmd ("set t 1",pcm,0);
301     }
302     gxfrme (1);
303     if (reinit)
304       gaprnt (1,"All GrADS attributes have been reinitialized\n");
305     else gaprnt (1,"Most GrADS attributes have been reset\n");
306     goto retrn;
307   }
308   else if (cmpwrd("screen",cmd)) {
309     if ( (cmd=nxtwrd(cmd)) == NULL) {
310       gaprnt (0,"Screen Error: Missing keyword\n");
311       retcod = 1;
312       goto retrn;
313     }
314     i = 0;
315     if (cmpwrd("save",cmd)) i = 1;
316     if (cmpwrd("show",cmd)) i = 2;
317     if (cmpwrd("free",cmd)) i = 3;
318     if (i) {
319       if ( (cmd=nxtwrd(cmd)) == NULL) {
320         gaprnt (0,"Screen Error: Missing screen number\n");
321         retcod = 1;
322         goto retrn;
323       }
324       if ( intprs(cmd,&(fnum)) == NULL ) {
325         gaprnt (0,"Screen Error: Invalid screen number\n");
326         retcod = 1;
327         goto retrn;
328       }
329       if (i==1) gxdssv(fnum);
330       if (i==2) gxdssh(fnum);
331       if (i==3) gxdsfr(fnum);
332       gxfrme(9);
333     } else {
334       gaprnt (0,"Screen Error: Unknown keyword\n");
335       retcod = 1;
336       goto retrn;
337     }
338     goto retrn;
339   }
340   else if (cmpwrd("close",cmd)) {
341     if ( (cmd=nxtwrd(cmd)) == NULL) {
342       gaprnt (0,"Close Error: Missing file number\n");
343       retcod = 1;
344       goto retrn;
345     }
346     if ( intprs(cmd,&(fnum)) == NULL ) {
347       gaprnt (0,"Close Error: Invalid file number\n");
348       retcod = 1;
349       goto retrn;
350     }
351     if (fnum != pcm->fnum) {
352       gaprnt (0,"Close Error: Only last file may be closed\n");
353       retcod = 1;
354       goto retrn;
355     }
356     pfi = pcm->pfi1;
357     for (i=0; i<fnum-1 && pfi; i++) pfi = pfi->pforw;
358     if (pfi==NULL || pfi->pforw!=NULL) {
359       gaprnt (0,"Logic Error 4 on Close.  No Action Taken\n");
360       retcod = 1;
361       goto retrn;
362     }
363     if (pfi->infile) fclose (pfi->infile);
364     if (pfi->mfile) fclose(pfi->mfile);
365 #if USESDF == 1
366     if (pfi->sdf_ptr) close_netcdf(pfi->sdf_ptr->cdfid) ;
367     if (pfi->ncid != -999) ncclose(pfi->ncid) ;  /* no error checking */
368 #if USEHDF == 1
369     if (pfi->sdid != -999) SDend(pfi->sdid) ;    /* no error checking */
370 #endif
371 #endif
372     if (pfi->bufrdset) {            /* bufr station data */
373       gabufr_close(pfi->bufrdset);  /* free memory */
374       pfi->bufrdset=NULL;           /* reset the pointer */
375     }
376 #if USEGADODS
377     if (pfi->dhandle > -999) dodclo(pfi);  /* dodstn */
378 #endif
379     frepfi(pfi,0);
380     pcm->fnum--;
381     if (pcm->dfnum==fnum) pcm->dfnum = 1;
382     if (pcm->fnum==0) {
383       pcm->dfnum = 0;
384       pcm->pfi1 = NULL;
385     } else {
386       pfi = pcm->pfi1;
387       for (i=0; i<fnum-2 && pfi; i++) pfi = pfi->pforw;
388       pfi->pforw = NULL;
389     }
390     sprintf (pout,"File %i has been closed\n",fnum);
391     gaprnt (2,pout);
392     goto retrn;
393   }
394   else if (cmpwrd("clear",cmd)||cmpwrd("c",cmd)) {
395     rc = 0;
396     if ( (cmd=nxtwrd(cmd)) != NULL) {
397       rc=99;
398       if (cmpwrd("norset",cmd)) rc = 1;
399       if (cmpwrd("events",cmd)) rc = 2;
400       if (cmpwrd("graphics",cmd)) rc = 3;
401       if (cmpwrd("hbuff",cmd)) rc = 4;
402       if (cmpwrd("button",cmd)) rc = 5;
403       if (cmpwrd("rband",cmd)) rc = 6;
404       if (cmpwrd("dropmenu",cmd)) rc = 7;
405     }
406     if (rc==99) {
407       gaprnt (0,"Invalid option on clear command\n");
408       goto retrn;
409     }
410     if (rc<2) {
411       if (exflg) gxfrme (0);
412       else gxfrme (1);
413     }
414     else if (rc==2) gxfrme(8);
415     else if (rc==3) gxfrme(7);
416     else if (rc==4) gxhfrm(0);
417     else if (rc>4 && rc<8) {
418       if ( (cmd=nxtwrd(cmd)) == NULL) {
419         gaprnt (0,"Invalid or missing widget number on clear command\n");
420         goto retrn;
421       }
422       if ( intprs(cmd,&(fnum)) == NULL ) {
423         gaprnt (0,"Invalid or missing widget number on clear command\n");
424         goto retrn;
425       }
426       gxrs1wd (rc-4, fnum);
427     }
428     if (rc==1) gacln(pcm,0);
429     else if (rc<5 || rc>7) {
430       gacln (pcm,1);
431       pcm->dbflg = 0;
432     }
433     goto retrn;
434   }
435   else if (cmpwrd("swap",cmd)) {
436     if (pcm->dbflg) gxfrme(2);
437     gacln (pcm,1);
438     goto retrn;
439 #if !defined(XLIBEMU)
440   } else if (cmpwrd("outxwd", cmd)) { /* hoop */
441     char *fname, name_file_xwd[256] ; /* hoop */
442     fname = nxtwrd(com) ; /* hoop */
443     if (fname) { /* hoop */
444         if (sscanf(fname, "%s", name_file_xwd) == 1) { /* hoop */
445             if (pcm->dbflg) { /* hoop */
446                 dump_back_buffer(name_file_xwd) ; /* hoop */
447             } else { /* hoop */
448                 dump_front_buffer(name_file_xwd) ; /* hoop */
449             } /* hoop */
450         } /* hoop */
451     } else { /* hoop */
452         gaprnt(0, "command outxwd:  need filename parameter\n") ; /* hoop */
453     } /* hoop */
454     gacln(pcm, 1) ; /* hoop */
455     return(0) ; /* hoop */
456    /* hoop */
457 #endif          /* XLIBEMU */
458   } else if (cmpwrd("q",cmd)||cmpwrd("query",cmd)) {
459     retcod = gaqury (cmd, com, pcm);
460     goto retrn;
461   }
462   else if (cmpwrd("help",cmd)) {
463     retcod = gahelp (cmd, pcm);
464     goto retrn;
465   }
466   else if (cmpwrd("exec",cmd)) {
467     retcod = gaexec (com, pcm);
468     goto retrn;
469   }
470   else if (cmpwrd("run",cmd)) {
471     if ( (cmd=nxtwrd(com)) == NULL) {
472       gaprnt (0,"RUN error:  No file name specified\n");
473       retcod = 1;
474       goto retrn;
475     }
476     savpcm = pcm;
477     rslt = gsfile(cmd,&rc,0);
478     if (rc==0 && rslt!=NULL) gaprnt(2,rslt);
479     if (rslt!=NULL) free(rslt);
480     retcod = rc;
481     goto retrn;
482   }
483   else if (cmpwrd("enable",cmd)) {
484     retcod = gaenab (com, pcm);
485     goto retrn;
486   }
487   else if (cmpwrd("disable",cmd)) {
488     if ( (cmd=nxtwrd(com)) == NULL) gxhend();
489     else {
490       if (cmpwrd("print",cmd)) gxhend();
491       else if (cmpwrd("fwrite",cmd)) {
492 	if (pcm->ffile) fclose(pcm->ffile);  /* don't close a file unless it's open  */
493         pcm->ffile = NULL;
494         if (pcm->fwname) free(pcm->fwname);  /* reset fwrite file name... */
495         pcm->fwname = NULL;                  /* on disable.  not sure this is good. */
496         pcm->fwenflg = BYTEORDER; /* set fwrite to default state */
497         pcm->fwsqflg = 0;        /* default is stream */
498         pcm->fwexflg = 0;        /* default is not exact -- old bad way */
499       }
500       else gaprnt (0,"DISABLE error: Invalid keyword\n");
501     }
502     goto retrn;
503   }
504   else if (cmpwrd("redraw",cmd)) {
505     gardrw(com,pcm);
506     gxfrme(9);
507     goto retrn;
508   }
509   else if (cmpwrd("draw",cmd)) {
510     gadraw(com,pcm);
511     gxfrme (9);          /* flush any buffers as needed */
512     goto retrn;
513   }
514   else if (cmpwrd("print",cmd)) {
515 #ifdef PRINT_EPS
516     gxhprt (com);
517 #else
518     gxhprt ();
519 #endif
520     goto retrn;
521   }
522 #if GXPNG==1
523   else if (cmpwrd("printim",cmd)) {
524     if ( (ch=nxtwrd(com)) == NULL) {
525       gaprnt (0,"PRINTIM error:  missing output file name\n");
526       retcod = 1;
527       goto retrn;
528     }
529     getwrd (cc,ch,256);
530     if ( (cmd=nxtwrd(cmd)) == NULL) {
531       gaprnt (0,"PRINTIM error:  logic error 64\n");
532       retcod = 1;
533       goto retrn;
534     }
535     xin = -999;
536     yin = -999;
537     bwin = -999;
538     gifflg = 9;
539     bgImage[0]='\0';
540     fgImage[0]='\0';
541     tcolor=-1;
542     while ((cmd=nxtwrd(cmd)) != NULL) {
543       if (cmpwrd("black",cmd))  bwin = 0;
544       else if (cmpwrd("white",cmd))  bwin = 1;
545       else if (cmpwrd("gif",cmd))  gifflg = 1;
546 #ifdef PRINTIM_JPEG
547       else if (cmpwrd("jpg",cmd))  gifflg = 3;
548       else if (cmpwrd("jpeg",cmd)) gifflg = 3;
549 #endif
550       else if (cmpwrd("png",cmd))  gifflg = 0;
551       else if (cmpwrd("-b",cmd)) {
552         if((cmd=nxtwrd(cmd)) != NULL) {
553           if(strlen(cmd) < 256){
554             getwrd(bgImage,cmd, 255);
555             sprintf(pout,"Background image file: %s \n", bgImage);
556 	    gaprnt(2,pout);
557           }
558         } else {
559           gaprnt(1,"PRINTIM warning: Background image file name not provided\n");
560 	  if (cmd == NULL) break;
561 	}
562       }
563       else if (cmpwrd("-f",cmd)) {
564         if((cmd=nxtwrd(cmd)) != NULL) {
565           if(strlen(cmd) < 256){
566             getwrd(fgImage,cmd, 255);
567             sprintf(pout,"Foreground image file: %s \n", fgImage);
568 	    gaprnt(2,pout);
569           }
570 	} else {
571           gaprnt(1,"PRINTIM warning: Foreground image file name not provided\n");
572 	  if (cmd == NULL) break;
573 	}
574       }
575       else if (cmpwrd("-t",cmd)) {
576         if((cmd=nxtwrd(cmd)) != NULL) {
577 	  if(sscanf(cmd, "%i", &tcolor) != 1) {
578 	     gaprnt(1,"PRINTIM warning: Invalid transparent color number\n");
579 	  }
580 	} else {
581 	  gaprnt(1,"PRINTIM warning: Missing transparent color number\n");
582 	  if (cmd == NULL) break;
583 	}
584       }
585       else if (*cmd=='x') {
586         if ( intprs(cmd+1,&(xin)) == NULL ) {
587           gaprnt (0,"PRINTIM error:  Invalid x option; ignored\n");
588           xin = -999;
589         }
590       }
591       else if (*cmd=='y') {
592         if ( intprs(cmd+1,&(yin)) == NULL ) {
593           gaprnt (0,"PRINTIM error: Invalid y option; ignored\n");
594           yin = -999;
595         }
596       } else {
597         gaprnt (0,"PRINTIM error: Invalid option; ignored\n");
598       }
599     }
600     if (gifflg == 9) {
601       len = 0;
602       while (*(cc+len)) len++;
603       len = len-4;
604       if (len>0) {
605         if (*(cc+len)=='.' && *(cc+len+1)=='g' &&
606           *(cc+len+2)=='i' && *(cc+len+3)=='f' ) gifflg = 1;
607         if (*(cc+len)=='.' && *(cc+len+1)=='G' &&
608           *(cc+len+2)=='I' && *(cc+len+3)=='F' ) gifflg = 1;
609       }
610       if (gifflg==9) gifflg = 0;
611     }
612     rc = gxhpng (cc,xin,yin,bwin,gifflg,bgImage,fgImage,tcolor);
613     if (rc==1) gaprnt (0,"PRINTIM error: open error\n");
614     if (rc==2) gaprnt (0,"PRINTIM error: output error\n");
615     if (rc==3) gaprnt (0,"PRINTIM error: background image open error\n");
616     if (rc==4) gaprnt (0,"PRINTIM error: foreground image open error\n");
617     if (rc==5) gaprnt (0,"PRINTIM error: background image must be .png\n");
618     if (rc==6) gaprnt (0,"PRINTIM error: foreground image must be .png\n");
619     if (rc==7) gaprnt (0,"PRINTIM error: gdImageCreate failed for background image\n");
620     if (rc==8) gaprnt (0,"PRINTIM error: gdImageCreate failed for foreground image\n");
621     if (rc) retcod = 1;
622     else retcod = 0;
623     goto retrn;
624   }
625 #else
626   else if (cmpwrd("printim",cmd)) {
627     gaprnt (0,"PRINTIM error: command not supported in this build\n");
628     retcod = 1;
629     goto retrn;
630   }
631 #endif
632   else if (cmpwrd("set",cmd)) {
633     retcod = gaset (cmd, com, pcm);
634     goto retrn;
635   }
636   else if (cmpwrd("open",cmd)) {
637     if ( (cmd=nxtwrd(com)) == NULL) {
638       gaprnt (0,"OPEN error:  missing data description file name\n");
639       retcod = 1;
640       goto retrn;
641     }
642     getwrd (cc,cmd,256);
643     retcod = gaopen (cc, pcm);
644     goto retrn;
645 #if USESDF == 1
646 /*i look for new keywords sdfopen and/or xdfopen */
647   } else if (cmpwrd("sdfopen", cmd)) {
648     if ( (cmd = nxtwrd(com)) == NULL) {
649       gaprnt(0, "SDFOPEN error:  missing self-describing file pathname\n") ;
650       retcod = 1;
651       goto retrn;
652     }
653     retcod = gasdfopen(cmd, pcm) ;
654     goto retrn;
655   }
656   else if (cmpwrd("xdfopen", cmd)) {
657     if ((cmd = nxtwrd(com)) == NULL) {
658         gaprnt(0, "XDFOPEN error:  missing data descriptor file name\n") ;
659         retcod = 1 ;
660         goto retrn ;
661     }
662     retcod = gaxdfopen(cmd, pcm) ;
663     goto retrn ;
664 #endif
665   } else if (cmpwrd("d",cmd) || cmpwrd("display",cmd)) {
666     if (pcm->pfid==NULL) {
667       gaprnt (0,"DISPLAY error:  no file open yet\n");
668       retcod = 1;
669       goto retrn;
670     }
671     retcod = gadspl (cmd, pcm);
672     gxfrme (9);          /* flush any buffers as needed */
673     goto retrn;
674   }
675   else if (cmpwrd("coll",cmd) || cmpwrd("collect",cmd)) {
676     if (pcm->pfid==NULL) {
677       gaprnt (0,"COLLECT error:  no file open yet\n");
678       retcod = 1;
679       goto retrn;
680     }
681     retcod = gacoll(cmd, pcm);
682     goto retrn;
683   }
684   else if (cmpwrd("define",cmd)) {
685     if (pcm->pfid==NULL) {
686       gaprnt (0,"DEFINE error:  no file open yet\n");
687       retcod = 1;
688       goto retrn;
689     }
690     retcod = gadef (cmd, pcm, 0);
691     goto retrn;
692   }
693   else if (cmpwrd("undefine",cmd)) {
694     if (pcm->pfid==NULL) {
695       gaprnt (0,"DEFINE error:  no file open yet\n");
696       retcod = 1;
697       goto retrn;
698     }
699     retcod = gaudef (cmd, pcm);
700     goto retrn;
701   }
702   else if (cmpwrd("modify",cmd)) {
703     if (pcm->pfid==NULL) {
704       gaprnt (0,"MODIFY error:  no file open yet\n");
705       retcod = 1;
706       goto retrn;
707     }
708     retcod = gamodf (cmd, pcm);
709     goto retrn;
710   }
711   else {
712     if (pcm->impcmd) {
713       savpcm = pcm;
714       rslt = gsfile(com,&rc,1);
715       if (rc==0 && rslt!=NULL) gaprnt(2,rslt);
716       if (rslt!=NULL) free(rslt);
717       retcod = rc;
718       goto retrn;
719     }
720     gaprnt (0,"Unknown command: ");
721     gaprnt (0,cmd);
722     gaprnt (0,"\n");
723     retcod = 1;
724     goto retrn;
725   }
726 
727 retrn:
728   if (ccc) free(ccc);
729   return (retcod);
730 }
731 
732 /* if flag is one, clean without resetting user options. */
733 
gacln(struct gacmn * pcm,int flg)734 void gacln (struct gacmn *pcm, int flg) {
735 int i;
736   pcm->pass = 0;
737   for (i=0; i<10; i++) pcm->gpass[i] = 0;
738   if (!pcm->ylpflg) pcm->yllow = 0.0;
739   pcm->xexflg = 0; pcm->yexflg = 0;
740   if (flg) {
741     pcm->cstyle = -9;
742     pcm->ccolor = -9;
743     pcm->cthick = 3;
744     pcm->cmark = -9;
745     pcm->cint = 0;
746     pcm->cflag = 0;
747     pcm->ccflg = 0;
748     pcm->cmin = -9.99e33;
749     pcm->cmax = 9.99e33;
750     pcm->blkflg = 0;
751     pcm->aflag = 0;
752     pcm->aflag2 = 0;
753     pcm->axflg = 0;
754     pcm->ayflg = 0;
755     pcm->gridln = -9;
756     pcm->rainmn = pcm->rainmx = 0.0;
757     pcm->grdsflg = 1;
758     pcm->arrflg = 0;
759     pcm->hemflg = -1;
760     pcm->rotate = 0;
761     pcm->xflip = 0;
762     pcm->yflip = 0;
763     if (pcm->xlstr) free(pcm->xlstr);
764     if (pcm->ylstr) free(pcm->ylstr);
765     if (pcm->clstr) free(pcm->clstr);
766     if (pcm->xlabs) free(pcm->xlabs);
767     if (pcm->ylabs) free(pcm->ylabs);
768     pcm->xlstr = NULL;
769     pcm->ylstr = NULL;
770     pcm->clstr = NULL;
771     pcm->xlabs = NULL;
772     pcm->ylabs = NULL;
773     pcm->xlint = 0.0;
774     pcm->ylint = 0.0;
775     pcm->xlflg = 0;
776     pcm->ylflg = 0;
777     pcm->xlpos = 0.0;
778     pcm->ylpos = 0.0;
779     pcm->ylpflg = 0;
780     pcm->yllow = 0.0;
781     pcm->xlside = 0;
782     pcm->ylside = 0;
783     pcm->tlsupp = 0;
784     pcm->ptflg = 0;
785   }
786   pcm->shdcnt = 0;
787   pcm->lastgx = 0;
788   pcm->xdim = -1;
789   pcm->ydim = -1;
790   pcm->xgr2ab = NULL;
791   pcm->ygr2ab = NULL;
792   pcm->xab2gr = NULL;
793   pcm->yab2gr = NULL;
794 }
795 
796 /* Handle redraw command */
797 
gardrw(char * cmd,struct gacmn * pcm)798 int gardrw (char *cmd, struct gacmn *pcm) {
799 struct gbtn btn;
800 char oper[12];
801 int num,state,i,sflg;
802 
803   if ( (cmd=nxtwrd(cmd)) == NULL) {
804     gaprnt (0,"REDRAW error: Missing operand\n");
805     return (1);
806   }
807   getwrd (oper,cmd,10);
808   lowcas (oper);
809 
810   if (cmpwrd("button",oper)) {
811     if ( (cmd = nxtwrd (cmd)) == NULL) goto errrbn;
812     if ( intprs(cmd,&(num)) == NULL ) goto errrbn;
813     if (num<0 || num>255) goto errrbn;
814     state = -1;
815     if ( (cmd = nxtwrd (cmd)) == NULL) goto errrbn;
816     if ( intprs(cmd,&(state)) == NULL ) goto errrbn;
817     btn.bc = pcm->btnbc;
818     btn.fc = pcm->btnfc;
819     btn.oc1 = pcm->btnoc;
820     btn.oc2 = pcm->btnoc2;
821     btn.btc = pcm->btnbtc;
822     btn.ftc = pcm->btnftc;
823     btn.otc1 = pcm->btnotc;
824     btn.otc2 = pcm->btnotc2;
825     btn.thk = pcm->btnthk;
826     btn.state = state;
827 
828     sflg = 2;
829     if ( (cmd = nxtwrd (cmd)) == NULL) {
830       btn.len = 0;
831       btn.ch = NULL;
832     } else {
833       if ( intprs(cmd,&(i)) == NULL ) goto errrbn;
834       if (i) sflg = 3;
835       if ( (cmd = nxtwrd (cmd)) == NULL) {
836         btn.len = 0;
837         btn.ch = NULL;
838       } else {
839         btn.len = 0;
840         while (*(cmd+btn.len)) btn.len++;
841         btn.ch = (char *)malloc(btn.len+1);
842         *(btn.ch+btn.len) = '\0';
843         if (btn.ch==NULL) {
844           gaprnt(0,"Memory allocation error; DRAW BUTTON cmd\n");
845           return(1);
846         }
847         for (i=0; i<btn.len; i++) *(btn.ch+i) = *(cmd+i);
848       }
849     }
850     gxdpbn(num, &btn, sflg, 0, state);
851   } else {
852     gaprnt (0,"REDRAW error:  Invalid operand\n");
853     return(1);
854   }
855   return (0);
856 
857   errrbn:
858   gaprnt (0,"REDRAW error: Syntax is REDRAW BUTTON number state flag text\n");
859   return (1);
860 }
861 
862 
863 /* handle draw command */
864 
865 static float justx[9] = {0.0,0.5,1.0,0.0,0.5,1.0,0.0,0.5,1.0};
866 static float justy[9] = {0.0,0.0,0.0,0.5,0.5,0.5,1.0,1.0,1.0};
867 
gadraw(char * cmd,struct gacmn * pcm)868 int gadraw (char *cmd, struct gacmn *pcm) {
869 char oper[12],chars[250];
870 char *c1,*c2,*ccmd;
871 int i,cnt,newcnt,cflg,ipos,len,mk,wx,thk,col;
872 float x,y,xlo,xhi,ylo,yhi,cs,swide,shite,ang;
873 float *xy, *newxy, llinc;
874 struct gbtn btn;
875 struct gdmu dmu;
876 
877   /* Check initial operands */
878 
879   if ( (cmd=nxtwrd(cmd)) == NULL) {
880     gaprnt (0,"DRAW error: Missing operand\n");
881     return (1);
882   }
883   getwrd (oper,cmd,10);
884   lowcas (oper);
885 
886   if (cmpwrd("map",oper)) {
887     if (pcm->dmax[0]>pcm->dmin[0] && pcm->dmax[1]>pcm->dmin[1]) {
888       gamscl(pcm);
889       gawmap(pcm,0);
890     }
891     return (0);
892   }
893   if (cmpwrd("button",oper)) {
894     if ( (cmd = nxtwrd (cmd)) == NULL) goto errbn;
895     if ( intprs(cmd,&(cnt)) == NULL ) goto errbn;
896     if ( (cmd = nxtwrd (cmd)) == NULL) goto errbn;
897     if ( valprs(cmd,&(btn.x)) == NULL ) goto errbn;
898     if ( (cmd = nxtwrd (cmd)) == NULL) goto errbn;
899     if ( valprs(cmd,&(btn.y)) == NULL ) goto errbn;
900     if ( (cmd = nxtwrd (cmd)) == NULL) goto errbn;
901     if ( valprs(cmd,&(btn.w)) == NULL ) goto errbn;
902     if ( (cmd = nxtwrd (cmd)) == NULL) goto errbn;
903     if ( valprs(cmd,&(btn.h)) == NULL ) goto errbn;
904     btn.bc = pcm->btnbc;
905     btn.fc = pcm->btnfc;
906     btn.oc1 = pcm->btnoc;
907     btn.oc2 = pcm->btnoc2;
908     btn.btc = pcm->btnbtc;
909     btn.ftc = pcm->btnftc;
910     btn.otc1 = pcm->btnotc;
911     btn.otc2 = pcm->btnotc2;
912     btn.thk = pcm->btnthk;
913     btn.state = 0;
914 
915     if ( (cmd = nxtwrd (cmd)) == NULL) goto errbn;
916     btn.len = 0;
917     while (*(cmd+btn.len)) btn.len++;
918     btn.ch = (char *)malloc(btn.len+1);
919     *(btn.ch+btn.len) = '\0';
920     if (btn.ch==NULL) {
921       gaprnt(0,"Memory allocation error; DRAW BUTTON cmd\n");
922       return(1);
923     }
924     for (i=0; i<btn.len; i++) *(btn.ch+i) = *(cmd+i);
925 /*  gxbutn (cnt,&btn);   */
926     gxdpbn(cnt, &btn, 0, 0, -1);
927     return (0);
928 
929     errbn:
930     gaprnt (0,"DRAW error: Syntax is DRAW BUTTON number x y w h ");
931     gaprnt (0,"string\n");
932     return (1);
933   }
934 
935   if (cmpwrd("dropmenu",oper)) {
936     if ( (cmd = nxtwrd (cmd)) == NULL) goto errpm;
937     if ( intprs(cmd,&(cnt)) == NULL ) goto errpm;
938     if ( (cmd = nxtwrd (cmd)) == NULL) goto errpm;
939     if (cmpwrd("cascade", cmd)) {
940       dmu.casc = 1;
941     } else {
942       dmu.casc = 0;
943       if ( valprs(cmd,&(dmu.x)) == NULL ) goto errpm;
944       if ( (cmd = nxtwrd (cmd)) == NULL) goto errpm;
945       if ( valprs(cmd,&(dmu.y)) == NULL ) goto errpm;
946       if ( (cmd = nxtwrd (cmd)) == NULL) goto errpm;
947       if ( valprs(cmd,&(dmu.w)) == NULL ) goto errpm;
948       if ( (cmd = nxtwrd (cmd)) == NULL) goto errpm;
949       if ( valprs(cmd,&(dmu.h)) == NULL ) goto errpm;
950     }
951     dmu.fc = pcm->drvals[0]; dmu.bc = pcm->drvals[1];
952     dmu.oc1 = pcm->drvals[2]; dmu.oc2 = pcm->drvals[3];
953     dmu.tfc = pcm->drvals[4]; dmu.tbc = pcm->drvals[5];
954     dmu.toc1 = pcm->drvals[6]; dmu.toc2 = pcm->drvals[7];
955     dmu.bfc = pcm->drvals[8]; dmu.bbc = pcm->drvals[9];
956     dmu.boc1 = pcm->drvals[10]; dmu.boc2 = pcm->drvals[11];
957     dmu.soc1 = pcm->drvals[12]; dmu.soc2 = pcm->drvals[13];
958     dmu.thk = pcm->drvals[14];
959 
960     if ( (cmd = nxtwrd (cmd)) == NULL) goto errpm;
961     dmu.len = 0;
962     while (*(cmd+dmu.len)) dmu.len++;
963     dmu.ch = (char *)malloc(dmu.len+1);
964     *(dmu.ch+dmu.len) = '\0';
965     if (dmu.ch==NULL) {
966       gaprnt(0,"Memory allocation error; DRAW DROPMENU cmd\n");
967       return(1);
968     }
969     for (i=0; i<dmu.len; i++) {
970       *(dmu.ch+i) = *(cmd+i);
971       if (*(dmu.ch+i)=='|') *(dmu.ch+i) = '\0';
972     }
973     gxdrmu (cnt,&dmu,0,-1);
974     return (0);
975 
976     errpm:
977     gaprnt (0,"DRAW error: Syntax is DRAW DROPMENU number x y w h ");
978     gaprnt (0,"string | string | ...\n");
979     return (1);
980   }
981 
982   if (cmpwrd("text",oper)) {
983     if ( (cmd = nxtwrd (cmd)) == NULL) goto errtx;
984     if ( valprs(cmd,&(x)) == NULL ) goto errtx;
985     if ( (cmd = nxtwrd (cmd)) == NULL) goto errtx;
986     if ( valprs(cmd,&(y)) == NULL ) goto errtx;
987     if ( (cmd = nxtwrd (cmd)) == NULL) goto errtx;
988     gxcolr (pcm->strcol);
989     gxdtxt(cmd,x,y);
990     return (0);
991 
992     errtx:
993     gaprnt (0,"DRAW error: Syntax is DRAW TEXT x y string\n");
994     return(1);
995   }
996 
997   if (cmpwrd("wxsym",oper)) {
998     if ( (cmd = nxtwrd (cmd)) == NULL) goto errwx;
999     if ( intprs(cmd,&wx) == NULL ) goto errwx;
1000     if ( (cmd = nxtwrd (cmd)) == NULL) goto errwx;
1001     if ( valprs(cmd,&x) == NULL ) goto errwx;
1002     if ( (cmd = nxtwrd (cmd)) == NULL) goto errwx;
1003     if ( valprs(cmd,&y) == NULL ) goto errwx;
1004     if ( (cmd = nxtwrd (cmd)) == NULL) goto errwx;
1005     if ( valprs(cmd,&cs) == NULL ) goto errwx;
1006     thk = 3;
1007     col = -1;
1008     if ( (cmd = nxtwrd (cmd)) != NULL) {
1009       if ( intprs(cmd,&col) == NULL ) goto errwx;
1010       if ( (cmd = nxtwrd (cmd)) != NULL) {
1011         if ( intprs(cmd,&thk) == NULL ) goto errwx;
1012       }
1013     }
1014     if (wx<1 || wx>43) goto errwx;
1015     gxwide (thk);
1016     gxstyl(1);
1017     wxsym (wx,x,y,cs,col,pcm->wxcols);
1018     return (0);
1019 
1020     errwx:
1021     gaprnt (0,"DRAW error: Syntax is DRAW WXSYM sym x y siz ");
1022     gaprnt (0,"<color <thick> >\n");
1023     return (1);
1024   }
1025   if (cmpwrd("string",oper)) {
1026     if ( (cmd = nxtwrd (cmd)) == NULL) goto errst;
1027     if ( valprs(cmd,&x) == NULL ) goto errst;
1028     if ( (cmd = nxtwrd (cmd)) == NULL) goto errst;
1029     if ( valprs(cmd,&y) == NULL ) goto errst;
1030     if ( (cmd = nxtwrd (cmd)) == NULL) goto errst;
1031     c1 = cmd;
1032     len=0;
1033     while (*c1!='\0' && *c1!='\n') {len++; c1++;}
1034     gxwide (pcm->strthk);
1035     gxcolr (pcm->strcol);
1036 
1037     swide = 0.2;
1038     gxchln (cmd,len,pcm->strhsz,&swide);
1039     shite = pcm->strvsz;
1040 
1041     ang = pcm->strrot*3.1416/180.0;
1042     x = x - justx[pcm->strjst] * swide * cos(ang);
1043     y = y - justx[pcm->strjst] * swide * sin(ang);
1044     x = x - justy[pcm->strjst] * shite * cos(ang+1.5708);
1045     y = y - justy[pcm->strjst] * shite * sin(ang+1.5708);
1046 
1047     gxchpl (cmd,len,x,y,pcm->strvsz,pcm->strhsz,pcm->strrot);
1048     return (0);
1049 
1050     errst:
1051     gaprnt (0,"DRAW error: Syntax is DRAW STRING x y string\n");
1052     return (1);
1053   }
1054   if (cmpwrd("rec",oper)) {
1055     if ( (cmd = nxtwrd (cmd)) == NULL) goto errrc;
1056     if ( valprs(cmd,&xlo) == NULL ) goto errrc;
1057     if ( (cmd = nxtwrd (cmd)) == NULL) goto errrc;
1058     if ( valprs(cmd,&ylo) == NULL ) goto errrc;
1059     if ( (cmd = nxtwrd (cmd)) == NULL) goto errrc;
1060     if ( valprs(cmd,&xhi) == NULL ) goto errrc;
1061     if ( (cmd = nxtwrd (cmd)) == NULL) goto errrc;
1062     if ( valprs(cmd,&yhi) == NULL ) goto errrc;
1063     if (xlo>=xhi || ylo>=yhi) goto errrc;
1064     gxwide (pcm->linthk);
1065     gxcolr (pcm->lincol);
1066     gxstyl (pcm->linstl);
1067     gxplot (xlo,ylo,3);
1068     gxplot (xhi,ylo,2);
1069     gxplot (xhi,yhi,2);
1070     gxplot (xlo,yhi,2);
1071     gxplot (xlo,ylo,2);
1072     return (0);
1073 
1074     errrc:
1075     gaprnt (0,"DRAW error: Syntax is DRAW REC xlo ylo xhi yhi\n");
1076     return (1);
1077   }
1078   if (cmpwrd("polyf",oper) || cmpwrd("mappoly",oper)) {
1079     i = 0;
1080     ccmd = cmd;
1081     while ( (ccmd = nxtwrd (ccmd)) != NULL) i++;
1082     if (i<6) {
1083       gaprnt (0,"DRAW error: Syntax is DRAW POLYF x1 y1 x2 y2 ...\n");
1084       return (1);
1085     }
1086     xy = (float *)malloc(sizeof(float)*(i+2));
1087     if (xy==NULL) {
1088       gaprnt (0,"DRAW error: Memory allocation error\n");
1089       return (1);
1090     }
1091     i = 0;
1092     while ( (cmd = nxtwrd(cmd)) != NULL) {
1093       if ( valprs(cmd,xy+i) == NULL ) {
1094         gaprnt (0,"DRAW error: Invalid polyf coordinate\n");
1095         free (xy);
1096         return(1);
1097       }
1098       i++;
1099     }
1100     cnt = i/2;
1101     if (*xy != *(xy+(cnt-1)*2) || *(xy+1) != *(xy+(cnt-1)*2+1)) {
1102       *(xy+cnt*2) = *xy;
1103       *(xy+cnt*2+1) = *(xy+1);
1104       cnt++;
1105     }
1106 
1107     /*  Mappoly requires an x/y varying environment, and assumes
1108         input is lon/lat pairs.  Interpolation along the side
1109         is done to insure curvature along the map projection,
1110         then conversion from lon/lat to x/y is performed, then
1111         the polygon is plotted.  */
1112 
1113     if (cmpwrd("mappoly",oper)) {
1114 
1115       /* Check for x/y varying -- not really required, but
1116          cannot think of any reason to allow for other dimension
1117          environments. */
1118 
1119       if (pcm->xdim!=0 && pcm->ydim!=1) {
1120         free (xy);
1121         gaprnt (0,"DRAW MAPPOLY error: Invalid Dimension Environment\n");
1122         gaprnt (0,"                    X and Y must be varying\n");
1123         return (1);
1124       }
1125 
1126       /* Determine increment for interpolation */
1127 
1128       llinc = hypot(pcm->dmax[0]-pcm->dmin[0], pcm->dmax[1]-pcm->dmin[1]);
1129       llinc = llinc/200.0;
1130       if (llinc<0.0001) llinc=0.0001;
1131 
1132       /* Do the interpolation and convert to x,y --
1133          the gxmpoly routine does this and is located in
1134          gxwmap */
1135 
1136       newxy = gxmpoly(xy,cnt,llinc,&newcnt);
1137       free (xy);
1138       if (newxy==NULL) {
1139         gaprnt (0,"DRAW MAPPOLY error: Memory allocation\n");
1140         return (1);
1141       }
1142       xy = newxy;
1143       cnt = newcnt;
1144       gxcolr (pcm->lincol);
1145       /*  draw border to avoid gaps */
1146       gxwide (1);
1147       gxstyl(1);
1148       gxcolr (pcm->lincol);
1149       gxplot (*(xy),*(xy+1),3);
1150       for (i=1; i<cnt; i++) {
1151         gxplot (*(xy+i*2),*(xy+i*2+1),2);
1152       }
1153     }
1154     gxcolr (pcm->lincol);
1155     if (pcm->ptflg) gxptrn (pcm->ptopt,pcm->ptden,pcm->ptang);
1156     gxfill (xy,cnt);
1157     if (pcm->ptflg) gxptrn (1,1,0);
1158     free (xy);
1159     return (0);
1160   }
1161   if (cmpwrd("recf",oper) || cmpwrd("maskrec",oper)) {
1162     if ( (cmd = nxtwrd (cmd)) == NULL) goto errfc;
1163     if ( valprs(cmd,&xlo) == NULL ) goto errfc;
1164     if ( (cmd = nxtwrd (cmd)) == NULL) goto errfc;
1165     if ( valprs(cmd,&ylo) == NULL ) goto errfc;
1166     if ( (cmd = nxtwrd (cmd)) == NULL) goto errfc;
1167     if ( valprs(cmd,&xhi) == NULL ) goto errfc;
1168     if ( (cmd = nxtwrd (cmd)) == NULL) goto errfc;
1169     if ( valprs(cmd,&yhi) == NULL ) goto errfc;
1170     if (xlo>=xhi || ylo>=yhi) goto errfc;
1171     if (cmpwrd("maskrec",oper)) {
1172       gxmaskrec (xlo,xhi,ylo,yhi);
1173       return(0);
1174     }
1175     gxcolr (pcm->lincol);
1176     if (pcm->ptflg) gxptrn (pcm->ptopt,pcm->ptden,pcm->ptang);
1177     gxrecf (xlo,xhi,ylo,yhi);
1178     if (pcm->ptflg) gxptrn (1,1,0);
1179     return (0);
1180 
1181     errfc:
1182     gaprnt (0,"DRAW error: Syntax is DRAW RECF xlo ylo xhi yhi\n");
1183     return (1);
1184   }
1185   if (cmpwrd("line",oper)) {
1186     gxwide (pcm->linthk);
1187     gxcolr (pcm->lincol);
1188     gxstyl (pcm->linstl);
1189     if ( (cmd = nxtwrd (cmd)) == NULL) goto errln;
1190     if ( valprs(cmd,&xlo) == NULL ) goto errln2;
1191     if ( (cmd = nxtwrd (cmd)) == NULL) goto errln;
1192     if ( valprs(cmd,&ylo) == NULL ) goto errln2;
1193     gxplot (xlo,ylo,3);
1194     while ( (cmd = nxtwrd(cmd)) != NULL) {
1195       if ( valprs(cmd,&xlo) == NULL ) goto errln2;
1196       if ( (cmd = nxtwrd (cmd)) == NULL) goto errln3;
1197       if ( valprs(cmd,&ylo) == NULL ) goto errln2;
1198       gxplot (xlo,ylo,2);
1199     }
1200     return (0);
1201 
1202     errln:
1203     gaprnt (0,"DRAW error: Syntax is DRAW LINE x1 y1 x2 y2\n");
1204     return (1);
1205     errln2:
1206     gaprnt (0,"DRAW error: Invalid LINE coordinate\n");
1207     return (1);
1208     errln3:
1209     gaprnt (0,"DRAW LINE error: Missing Y coordinate\n");
1210     return (1);
1211   }
1212   if (cmpwrd("mark",oper)) {
1213     if ( (cmd = nxtwrd (cmd)) == NULL) goto errmk;
1214     if ( intprs(cmd,&mk) == NULL ) goto errmk;
1215     if ( (cmd = nxtwrd (cmd)) == NULL) goto errmk;
1216     if ( valprs(cmd,&x) == NULL ) goto errmk;
1217     if ( (cmd = nxtwrd (cmd)) == NULL) goto errmk;
1218     if ( valprs(cmd,&y) == NULL ) goto errmk;
1219     if ( (cmd = nxtwrd (cmd)) == NULL) goto errmk;
1220     if ( valprs(cmd,&cs) == NULL ) goto errmk;
1221     gxwide (pcm->linthk);
1222     gxcolr (pcm->lincol);
1223     gxstyl (1);
1224     gxmark (mk,x,y,cs);
1225     return (0);
1226 
1227     errmk:
1228     gaprnt (0,"DRAW error: Syntax is DRAW MARK marktype x y size\n");
1229     return (1);
1230   }
1231   if (cmpwrd("title",oper) || cmpwrd("xlab",oper) ||
1232       cmpwrd("ylab",oper)) {
1233 
1234     /* Count number of strings, delete leading blanks, change string
1235        delimeter (the backslash) to nulls. */
1236 
1237     if ( (cmd=nxtwrd(cmd)) == NULL) {
1238       gaprnt (0,"DRAW error: Missing character string \n");
1239       return (1);
1240     }
1241 
1242     cnt = 1;
1243     c1 = cmd;
1244     c2 = chars;
1245     cflg = 1;
1246     while (*c1!='\0' && *c1!='\n') {
1247       if (cflg && *c1==' ') c1++;
1248       else {
1249         *c2 = *c1;
1250         cflg = 0;
1251         if (*c2=='\\') {
1252           *c2 = '\0';
1253           cnt++;
1254           cflg = 1;
1255         }
1256         c1++; c2++;
1257       }
1258     }
1259     *c2 = '\0';
1260 
1261     if (cmpwrd("title",oper)) {
1262       gxcolr (pcm->anncol);
1263       gxwide (pcm->annthk);
1264       cs = 0.2;
1265       if (cnt==2) cs=0.175;
1266       if (cnt==3) cs=0.15;
1267       if (cnt>3) cs=0.125;
1268       x = pcm->xsiz1 + (pcm->xsiz2-pcm->xsiz1)/2.0;
1269       y = pcm->ysiz2 + cs*(float)(cnt-1)*1.7 + cs*0.4 + 0.1;
1270       ipos = 0;
1271       for (i=0; i<cnt; i++) {
1272         swide = 1.0;
1273         gxchln (&(chars[ipos]),500,cs,&swide);
1274         gxchpl (&(chars[ipos]),500,x-swide*0.5,y,cs*1.2,cs,0.0);
1275         y = y - cs*1.7;
1276         while (chars[ipos]) ipos++;
1277         ipos++;
1278       }
1279     }
1280     else if (cmpwrd("xlab",oper)) {
1281       gxcolr (pcm->anncol);
1282       gxwide (pcm->annthk);
1283       cs = 0.16;
1284       if (cnt==2) cs=0.14;
1285       if (cnt>2)  cs=0.12;
1286       x = pcm->xsiz1 + (pcm->xsiz2-pcm->xsiz1)/2.0;
1287       y = pcm->ysiz1 - cs*1.7 - 0.3;
1288       ipos = 0;
1289       for (i=0; i<cnt; i++) {
1290         swide = 1.0;
1291         gxchln (&(chars[ipos]),500,cs,&swide);
1292         gxchpl (&(chars[ipos]),500,x-swide*0.5,y,cs*1.2,cs,0.0);
1293         y = y - cs*1.7;
1294         while (chars[ipos]) ipos++;
1295         ipos++;
1296       }
1297     }
1298     else if (cmpwrd("ylab",oper)) {
1299       gxcolr (pcm->anncol);
1300       gxwide (pcm->annthk);
1301       cs = 0.16;
1302       if (cnt==2) cs=0.14;
1303       if (cnt>2)  cs=0.12;
1304       y = pcm->ysiz1 + (pcm->ysiz2-pcm->ysiz1)/2.0;
1305       x = pcm->xsiz1 - pcm->yllow - cs*1.7*(float)(cnt-1) - cs;
1306       ipos = 0;
1307       for (i=0; i<cnt; i++) {
1308         swide = 1.0;
1309         gxchln (&(chars[ipos]),500,cs,&swide);
1310         gxchpl (&(chars[ipos]),500,x,y-swide*0.5,cs*1.2,cs,90.0);
1311         x = x + cs*1.7;
1312         while (chars[ipos]) ipos++;
1313         ipos++;
1314       }
1315     }
1316     return (0);
1317   }
1318   gaprnt (0,"DRAW error:  Invalid operand\n");
1319   return (1);
1320 }
1321 
1322 /* handle enable command */
1323 
gaenab(char * cmd,struct gacmn * pcm)1324 int gaenab (char *cmd, struct gacmn *pcm) {
1325 int rc;
1326 char cc[256], *ch;
1327 
1328   for (rc=0; rc<190; rc++) cc[rc] = *(cmd+rc);
1329   cc[40]='\0';
1330   lowcas (cc);
1331   if ( (ch=nxtwrd(cc)) == NULL) {
1332     gaprnt (0,"ENABLE error: Missing operand\n");
1333     return (1);
1334   }
1335   if ( !cmpwrd("print",ch)) {
1336     gaprnt (0,"ENABLE error: Invalid operand \n");
1337     return (1);
1338   }
1339   if ( (ch=nxtwrd(ch)) == NULL) {
1340     gaprnt (0,"ENABLE error: Missing file name \n");
1341     return (1);
1342   }
1343   cmd = nxtwrd(cmd);
1344   cmd = nxtwrd(cmd);
1345   getwrd (cc,cmd,256);
1346   rc = gxhbgn(cc);
1347   return (rc);
1348 }
1349 
1350 /* Execute command on behalf of the scripting language.
1351    Return the result in a dynamically allocated buffer */
1352 
gagsdo(char * cmd,int * rc)1353 char *gagsdo (char *cmd, int *rc) {
1354 int savflg,tlen,i;
1355 struct gacmn *pcm;
1356 char *mbuf, *ch;
1357 
1358    savflg = msgflg;
1359    msgflg = 1;          /* Buffer messages */
1360    pcm = savpcm;        /* Get common pointer */
1361    msgstk = NULL;
1362 
1363    *rc = gacmd(cmd, pcm, 0);
1364 
1365    /* Set up output buffer */
1366 
1367    if (msgstk==NULL) {
1368      msgflg = savflg;
1369      return(NULL);
1370    }
1371 
1372    tlen = 0;
1373    msgcurr = msgstk;
1374    while(msgcurr) {
1375      tlen += msgcurr->len;
1376      msgcurr = msgcurr->forw;
1377    }
1378 
1379    mbuf = (char *)malloc(tlen+1);
1380    if (mbuf==NULL) {
1381      printf ("Memory allocation error: Message Return Buffer\n");
1382      msgflg = savflg;
1383      return (NULL);
1384    }
1385 
1386    msgcurr = msgstk;
1387    ch = mbuf;
1388    while(msgcurr) {
1389      for (i=0; i<msgcurr->len; i++) {
1390        *ch = *(msgcurr->msg+i);
1391        ch++;
1392      }
1393      msgcurr = msgcurr->forw;
1394    }
1395    msgcurr = msgstk;
1396    while(msgcurr) {
1397      if (msgcurr->msg) free(msgcurr->msg);
1398      msgstk = msgcurr->forw;
1399      free (msgcurr);
1400      msgcurr = msgstk;
1401    }
1402 
1403    *(mbuf+tlen) = '\0';
1404    msgflg = savflg;
1405    return (mbuf);
1406 }
1407 
1408 /* Handle exec command.  Read exec file, then recursively
1409    call gacmd.  */
1410 
gaexec(char * cmd,struct gacmn * pcm)1411 int gaexec (char *cmd, struct gacmn *pcm) {
1412 FILE *efile;
1413 int i, j, flag, iarg;
1414 char ename[50];
1415 char ccc[500],cout[500];
1416 char *args[10],*ptr;
1417 int rc, savflg, ret;
1418 
1419   efile = NULL;
1420   savflg = msgflg;
1421   msgflg = 0;         /* Don't buffer messages */
1422 
1423   if ( (cmd=nxtwrd(cmd)) == NULL) {
1424     gaprnt (0,"EXEC error:  missing file name \n");
1425     ret = 1;
1426     goto retrn;
1427   }
1428 
1429   /* Get file name and open file.  If we cannot open file, exit */
1430 
1431   getwrd (ename,cmd,49);
1432   efile = fopen(ename,"r");
1433   if (efile==NULL) {
1434     gaprnt (0,"EXEC error:  Can't open file\n");
1435     sprintf (pout,"  File name is: %s\n",ename);
1436     gaprnt (0,pout);
1437     ret = 1;
1438     goto retrn;
1439   }
1440 
1441   /* Locate any arguments, save pointers to them, and terminate them
1442      with a null.  We modify the cmd line at this point.  */
1443 
1444   for (i=0;i<10;i++) args[i]=NULL;
1445   i = 0;
1446   cmd = nxtwrd(cmd);
1447   while (cmd != NULL && i<10) {
1448     args[i] = cmd;
1449     ptr = cmd;
1450     if (*ptr == '~') {
1451       ptr++;
1452       args[i] = ptr;
1453       while (*ptr != '~') ptr++;
1454       cmd = nxtwrd(ptr);
1455       *ptr = '\0';
1456     } else {
1457       cmd = nxtwrd(cmd);
1458       while (*ptr!=' ' && *ptr!='\0' && *ptr!='\n') ptr++;
1459       *ptr = '\0';
1460     }
1461     i++;
1462   }
1463 
1464 
1465   /* Read and execute the commands in the file.  Scan each command for
1466      any arg tokens and replace them with the arg strings */
1467 
1468   while ( (fgets(ccc,300,efile))!=NULL) {
1469     if (*ccc=='*') continue;
1470     i = 0; j = 0;
1471     while (ccc[i]!='\n') {
1472       flag = 1;
1473       if (ccc[i] == '&' && ccc[i+1]>='0' && ccc[i+1]<='9') {
1474         iarg = (int)(ccc[i+1]) - 48;
1475         ptr = args[iarg];
1476         if (ptr!=NULL) {
1477           while (*ptr!='\0') {
1478             cout[j] = *ptr;
1479             j++; ptr++;
1480           }
1481           i+=2;
1482           flag = 0;
1483         }
1484       }
1485       if (flag) {
1486         cout[j] = ccc[i];
1487         i++; j++;
1488       }
1489     }
1490     cout[j] = ccc[i];
1491     cout[j+1] = '\0';
1492     gaprnt (0,cout);
1493     rc = gacmd(cout,pcm,1);
1494     if (rc) {
1495       sprintf(pout,"EXEC error:  error in %s.  EXEC stopped.\n",ename);
1496       gaprnt (0,pout);
1497       ret = rc;
1498       goto retrn;
1499     }
1500   }
1501   sprintf (pout,"EOF EXECuting %s \n",ename);
1502   gaprnt (0,pout);
1503   ret = 0;
1504 
1505 retrn:
1506   if (efile) fclose(efile);
1507   msgflg = savflg;
1508   return (ret);
1509 }
1510 
1511 /* Handle undefine command */
1512 
gaudef(char * cmd,struct gacmn * pcm)1513 int gaudef (char *cmd, struct gacmn *pcm) {
1514 struct gafile *pfi;
1515 struct gadefn *pdf, *opdf;
1516 char name[20];
1517 int i;
1518 
1519   /* Get the define name */
1520 
1521   if ( (cmd=nxtwrd(cmd)) == NULL) {
1522     gaprnt (0,"UNDEFINE error:  name is missing \n");
1523     return(1);
1524   }
1525 
1526   i=0;
1527   while (*cmd!='\0' && *cmd!='\n') {
1528     name[i] = *cmd;
1529     cmd++; i++;
1530     if (i>16) break;
1531   }
1532   name[i] = '\0';
1533 
1534   pdf = pcm->pdf1;
1535   opdf = NULL;
1536   while (pdf!=NULL) {
1537     if (cmpwrd(name,pdf->abbrv)) break;
1538     opdf = pdf;
1539     pdf = pdf->pforw;
1540   }
1541   if (pdf==NULL) {
1542     gaprnt (1,"UNDEFINE Warning:  name not found\n");
1543     return (0);
1544   } else {
1545     if (opdf==NULL) pcm->pdf1 = pdf->pforw;
1546     else opdf->pforw = pdf->pforw;
1547     pfi = pdf->pfi;
1548     free (pfi->rbuf);
1549     for (i=0; i<4; i++) {
1550       free(pfi->grvals[i]);
1551       free(pfi->abvals[i]);
1552     }
1553     free (pfi);
1554     free (pdf);
1555     sprintf (pout,"%s UNDEFINEd and storage released\n",name);
1556     gaprnt (2,pout);
1557   }
1558   return (0);
1559 }
1560 
1561 /* Query or modify define data value -- modify if flag = 1 */
1562 
gaqdef(char * cmd,struct gacmn * pcm,int flag)1563 int gaqdef (char *cmd, struct gacmn *pcm, int flag) {
1564 struct gafile *pfi;
1565 struct gadefn *pdf;
1566 char name[20];
1567 char *ch;
1568 int i,gri,grj;
1569 float grval,*gr,val;
1570 
1571   /* Get the define name */
1572 
1573   if ( (cmd=nxtwrd(cmd)) == NULL) goto err;
1574 
1575   i=0;
1576   ch = cmd;
1577   while (*ch!='\0' && *ch!='\n') {
1578     name[i] = *ch;
1579     ch++; i++;
1580     if (i>16) break;
1581   }
1582   name[i] = '\0';
1583 
1584   /* Get i, j, and value */
1585 
1586   if ( (cmd=nxtwrd(cmd)) == NULL) goto err;
1587   if ( intprs(cmd,&gri) == NULL ) goto err;
1588   if ( (cmd=nxtwrd(cmd)) == NULL) goto err;
1589   if ( intprs(cmd,&grj) == NULL ) goto err;
1590   if (flag) {
1591     if ( (cmd=nxtwrd(cmd)) == NULL) goto err;
1592     if ( valprs(cmd,&grval) == NULL ) goto err;
1593   }
1594 
1595   /* Locate defined object in link list */
1596 
1597   pdf = pcm->pdf1;
1598   while (pdf!=NULL) {
1599     if (cmpwrd(name,pdf->abbrv)) break;
1600     pdf = pdf->pforw;
1601   }
1602   if (pdf==NULL) {
1603     gaprnt (1,"Warning:  defined name not found\n");
1604     return (0);
1605   }
1606 
1607   /* Locate desired value in defined object */
1608 
1609   pfi = pdf->pfi;
1610   gri = gri - (pfi->dimoff[0]+1);
1611   grj = grj - (pfi->dimoff[1]+1);
1612   if (flag) {
1613     if (gri<0 || gri>=pfi->dnum[0] || grj<0 || grj>=pfi->dnum[1]) {
1614       gaprnt (0,"MODIFY DEFVAL Error:  Out of Range\n");
1615       return (1);
1616     }
1617     gr = pfi->rbuf;
1618     gr = gr + (grj*pfi->dnum[0]+gri);
1619     *gr = grval;
1620   } else {
1621     if (gri<0 || gri>=pfi->dnum[0] || grj<0 || grj>=pfi->dnum[1]) {
1622       val = pfi->undef;
1623     } else {
1624       gr = pfi->rbuf;
1625       gr = gr + (grj*pfi->dnum[0]+gri);
1626       val = *gr;
1627     }
1628     sprintf (pout,"DEFVAL is %g\n",val);
1629     gaprnt (2,pout);
1630   }
1631   return(0);
1632 
1633 err:
1634   if (flag) {
1635     gaprnt(0,"MODIFY DEFVAL Error: Syntax is: ");
1636     gaprnt(0,"Modify Defval name i j value\n");
1637   } else {
1638     gaprnt(0,"QUERY DEFVAL Error: Syntax is: ");
1639     gaprnt(0,"Query Defval name i j\n");
1640   }
1641   return(1);
1642 }
1643 
1644 /* Modify attributes or contents of a defined grid */
1645 
gamodf(char * cmd,struct gacmn * pcm)1646 int gamodf (char *cmd, struct gacmn *pcm) {
1647 struct gadefn *pdf;
1648 struct gafile *pfi;
1649 struct dt dtim,otim;
1650 char name[20];
1651 float t1,t2,d1;
1652 int i,flg;
1653 
1654   /* Get the name of the defined grid (2nd arg) */
1655 
1656   if ( (cmd=nxtwrd(cmd)) == NULL) {
1657     gaprnt (0,"MODIFY error:  name is missing \n");
1658     return (1);
1659   }
1660 
1661   i=0;
1662   while (*(cmd+i)!=' ' && *(cmd+i)!='\n' && *(cmd+i)!='\0' && i<17) {
1663     name[i] = *(cmd+i);
1664     i++;
1665   }
1666   name[i] = '\0';
1667 
1668   /* See if the name is a defined grid */
1669 
1670   pdf = pcm->pdf1;
1671   while (pdf!=NULL && !cmpwrd(name,pdf->abbrv)) pdf = pdf->pforw;
1672   if (pdf==NULL) {
1673     sprintf (pout,"MODIFY Error: Defined grid %s not found\n",name);
1674     gaprnt (0,pout);
1675     return (1);
1676   }
1677 
1678   /* Take action based on 3rd argument */
1679 
1680   if ( (cmd=nxtwrd(cmd)) == NULL) {
1681     gaprnt (0,"MODIFY error:  Action keyword is missing\n");
1682     return (1);
1683   }
1684   else if (cmpwrd("seasonal",cmd)||cmpwrd("diurnal",cmd)) {
1685     pfi = pdf->pfi;
1686     if (pfi->dnum[3]==1) {
1687       gaprnt(0,"MODIFY Error:  ");
1688       gaprnt(0,"Time not varying for this defined variable\n");
1689       return(1);
1690     }
1691 
1692     /* Convert starting grid time to world time, add appropriate
1693        increment, and convert back to grid */
1694 
1695     t1 = (float)(pfi->dimoff[3]+1);
1696     gr2t (pfi->grvals[3], t1, &dtim);
1697     otim.yr=0; otim.mo=0; otim.dy=0; otim.hr=0; otim.mn=0;
1698     if (cmpwrd("seasonal",cmd)) {
1699       otim.yr = 1;
1700       flg = 1;
1701     } else {
1702       otim.dy = 1;
1703       flg = 2;
1704     }
1705     timadd (&dtim, &otim);
1706     t2 = t2gr(pfi->abvals[3],&otim);
1707 
1708     /* Check that the final time is integral -- otherwise
1709        a serious problem */
1710 
1711     if (t2<0.0) i = (int)(t2-0.1);
1712     else i = (int)(t2+0.1);
1713     d1 = (float)i;
1714     if (fabs(t2-d1)>0.001 || (flg==1 && (t2-t1>12.5))) {
1715       gaprnt (0,"MODIFY Error:  Invalid time scaling in defined variable\n");
1716       return (1);
1717     }
1718 
1719     /* Calculate cyclic time in grid units, set up pfi block, return */
1720 
1721     pfi->cysiz = (int)(0.1 + t2 - t1);
1722     pfi->climo = flg;
1723     gaprnt (0,"Defined variable is now climatological\n");
1724     return (0);
1725   }
1726   else {
1727     gaprnt (0,"MODIFY error: Invalid operand\n");
1728     gaprnt (0,"  Operand = ");
1729     gaprnt (0,cmd);
1730     gaprnt (0,"\n");
1731     return (1);
1732   }
1733 
1734 }
1735 
1736 
1737 /* Handle define command */
1738 
gadef(char * cmd,struct gacmn * pcm,int impf)1739 int gadef (char *cmd, struct gacmn *pcm, int impf) {
1740 float (*conv) (float *, float);
1741 struct gagrid *pgr, *pgr1;
1742 struct gastat *pst;
1743 struct gafile *pfi, *pfiv, *pfic;
1744 struct gadefn *pdf, *pcurr, *psave;
1745 struct gadefn **prev;
1746 struct dt tmin,tmax;
1747 int itmin,itmax,it,izmin,izmax,iz;
1748 float vmin,vmax,zmin,zmax, *res, *gr;
1749 char name[20];
1750 int i,rc,gsiz,vdz,vdt;
1751 int siz;
1752 
1753   pdf = NULL;
1754   pst = NULL;
1755   pgr1 = NULL;
1756   pfiv = NULL;
1757 
1758   tmax = pcm->tmax;                    /* Save user dim limits   */
1759   zmax = pcm->dmax[2];
1760   tmin = pcm->tmin;
1761   zmin = pcm->dmin[2];
1762   vdz = pcm->vdim[2];
1763   vdt = pcm->vdim[3];
1764 
1765   /* Get the define name */
1766 
1767   if (!impf) {
1768     if ( (cmd=nxtwrd(cmd)) == NULL) {
1769       gaprnt (0,"DEFINE error:  name is missing \n");
1770       goto retrn;
1771     }
1772   }
1773   garemb (cmd);
1774 
1775   i=0;
1776   while ( (*cmd>='a' && *cmd<='z') || (*cmd>='0' && *cmd<='9' ) ) {
1777     name[i] = *cmd;
1778     cmd++; i++;
1779     if (i>16) break;
1780   }
1781   name[i] = '\0';
1782 
1783   if (*cmd!='=') {
1784     gaprnt (0,"DEFINE error:  Name too long; missing '='\n");
1785     goto retrn;
1786   }
1787   cmd++;
1788   if (*cmd=='\0') {
1789     gaprnt (0,"DEFINE error:  expression missing\n");
1790     goto retrn;
1791   }
1792 
1793   /* We are now pointing to the expression.  We need to set up
1794      our looping environment -- we are going to always loop
1795      through Z and T.                                          */
1796 
1797   pfi = pcm->pfid;
1798   if (pfi->type==2 || pfi->type==3) {
1799     gaprnt (0,"Define error:  Define not yet valid for station data\n");
1800     gaprnt (0,"    Default file is a station data file\n");
1801     goto retrn;
1802   }
1803 
1804   conv = pfi->ab2gr[2];                /* Get Z grid limits      */
1805   vmin = conv(pfi->abvals[2],zmin);
1806   vmax = conv(pfi->abvals[2],zmax);
1807   if (vmin==vmax) {
1808     if (vmin<0.0) izmin = (int)(vmin-0.5);
1809     else izmin = (int)(vmin+0.5);
1810     izmax = izmin;
1811   } else {
1812     izmin = (int)(floor(vmin+0.001));
1813     izmax = (int)(ceil(vmax-0.001));
1814   }
1815 
1816   vmin = t2gr(pfi->abvals[3],&tmin);   /* Get T grid limits      */
1817   vmax = t2gr(pfi->abvals[3],&tmax);
1818   if (vmin==vmax) {
1819     if (vmin<0.0) itmin = (int)(vmin-0.5);
1820     else itmin = (int)(vmin+0.5);
1821     itmax = itmin;
1822   } else {
1823     itmin = (int)(floor(vmin+0.001));
1824     itmax = (int)(ceil(vmax-0.001));
1825   }
1826 
1827   pcm->tmax = pcm->tmin;               /* Fix Z and T dimensions */
1828   pcm->dmax[2] = pcm->dmin[2];
1829   pcm->vdim[2] = 0;
1830   pcm->vdim[3] = 0;
1831 
1832   /* Get the first grid.  */
1833 
1834   pst = getpst(pcm);
1835   if (pst==NULL) goto retrn;
1836 
1837   conv = pfi->gr2ab[2];
1838   pst->dmin[2] = conv(pfi->grvals[2],(float)izmin);
1839   pst->dmax[2] = pst->dmin[2];
1840   gr2t (pfi->grvals[3], (float)itmin, &(pst->tmin));
1841   pst->tmax = pst->tmin;
1842 
1843   rc = gaexpr(cmd, pst);
1844   if (!rc) rc = gaqsig();
1845   if (rc) {
1846     gaprnt (0,"DEFINE error:  Invalid expression. \n");
1847     goto retrn;
1848   }
1849 
1850   if (pst->type!=1) {
1851     gaprnt (0,"DEFINE Error:  Define does not yet support station data\n");
1852     gaprnt (0,"    Expression results in station data object\n");
1853     goto retrn;
1854   }
1855 
1856   /* Based on the grid we just got, we can now figure out the size of
1857      the final defined grid and fill in the gafile structure for the
1858      defined grid.  Allocate all the necessary stuff and fill it all
1859      in.  */
1860 
1861   /* Allocate the pdf and pfi blocks */
1862 
1863   pdf = (struct gadefn *)malloc(sizeof(struct gadefn));
1864   if (pdf==NULL) {
1865     gaprnt (0,"Memory Allocation Error:  DEFINE operation\n");
1866     goto retrn;
1867   }
1868   pfiv = (struct gafile *)malloc(sizeof(struct gafile));
1869   if (pfiv==NULL) {
1870     gaprnt (0,"Memory Allocation Error:  DEFINE operation\n");
1871     goto retrn;
1872   }
1873   pdf->pfi = pfiv;
1874   pfiv->rbuf = NULL;
1875 
1876   /* Fill in the pfi block */
1877 
1878   pgr1 = pst->result.pgr;
1879 
1880   pfiv->type = 4;
1881   pfiv->climo = 0;
1882   pfiv->undef = pgr1->undef;
1883   pfiv->dnum[2] = 1 + izmax - izmin;
1884   pfiv->dnum[3] = 1 + itmax - itmin;
1885   pfiv->gr2ab[2] = pfi->gr2ab[2];
1886   pfiv->ab2gr[2] = pfi->ab2gr[2];
1887   pfiv->grvals[2] = cpscal (pfi->grvals[2], pfi->linear[2], 0, 2);
1888   if (pfiv->grvals[2]==NULL) goto etrn;
1889   pfiv->grvals[3] = cpscal (pfi->grvals[3], pfi->linear[3], 0, 3);
1890   if (pfiv->grvals[3]==NULL) goto etrn;
1891   pfiv->abvals[2] = cpscal (pfi->abvals[2], pfi->linear[2], 1, 2);
1892   if (pfiv->abvals[2]==NULL) goto etrn;
1893   pfiv->abvals[3] = cpscal (pfi->abvals[3], pfi->linear[3], 1, 3);
1894   if (pfiv->abvals[3]==NULL) goto etrn;
1895   pfiv->linear[2] = pfi->linear[2];
1896   pfiv->linear[3] = pfi->linear[3];
1897   pfiv->dimoff[2] = izmin-1;
1898   pfiv->dimoff[3] = itmin-1;
1899   pfiv->ppflag = 0;
1900 
1901   if (pgr1->idim>-1 && pgr1->jdim>-1) {
1902     pfiv->gr2ab[0] = pgr1->igrab;
1903     pfiv->ab2gr[0] = pgr1->iabgr;
1904     pfiv->grvals[0] = cpscal (pgr1->ivals, pgr1->ilinr, 0, pgr1->idim);
1905     if (pfiv->grvals[0]==NULL) goto etrn;
1906     pfiv->abvals[0] = cpscal (pgr1->iavals, pgr1->ilinr, 1, pgr1->idim);
1907     if (pfiv->abvals[0]==NULL) goto etrn;
1908     pfiv->linear[0] = pgr1->ilinr;
1909     pfiv->dimoff[0] = pgr1->dimmin[0]-1;
1910     pfiv->dnum[0] = pgr1->isiz;
1911     pfiv->gr2ab[1] = pgr1->jgrab;
1912     pfiv->ab2gr[1] = pgr1->jabgr;
1913     pfiv->grvals[1] = cpscal (pgr1->jvals, pgr1->jlinr, 0, pgr1->jdim);
1914     if (pfiv->grvals[1]==NULL) goto etrn;
1915     pfiv->abvals[1] = cpscal (pgr1->javals, pgr1->jlinr, 1, pgr1->jdim);
1916     if (pfiv->abvals[1]==NULL) goto etrn;
1917     pfiv->linear[1] = pgr1->jlinr;
1918     pfiv->dimoff[1] = pgr1->dimmin[1]-1;
1919     pfiv->dnum[1] = pgr1->jsiz;
1920   }
1921   else if ( pgr1->idim > -1 && pgr1->jdim == -1) {
1922     if (pgr1->idim == 0) {
1923       pfiv->gr2ab[0] = pgr1->igrab;
1924       pfiv->ab2gr[0] = pgr1->iabgr;
1925       pfiv->grvals[0] = cpscal (pgr1->ivals, pgr1->ilinr, 0, pgr1->idim);
1926       if (pfiv->grvals[0]==NULL) goto etrn;
1927       pfiv->abvals[0] = cpscal (pgr1->iavals, pgr1->ilinr, 1, pgr1->idim);
1928       if (pfiv->abvals[0]==NULL) goto etrn;
1929       pfiv->linear[0] = pgr1->ilinr;
1930       pfiv->dimoff[0] = pgr1->dimmin[0]-1;
1931       pfiv->dnum[0] = pgr1->isiz;
1932       pfiv->gr2ab[1] = pfi->gr2ab[1];
1933       pfiv->ab2gr[1] = pfi->ab2gr[1];
1934       pfiv->grvals[1] = cpscal (pfi->grvals[1], pfi->linear[1], 0, 1);
1935       if (pfiv->grvals[1]==NULL) goto etrn;
1936       pfiv->abvals[1] = cpscal (pfi->abvals[1], pfi->linear[1], 1, 1);
1937       if (pfiv->abvals[1]==NULL) goto etrn;
1938       pfiv->linear[1] = pfi->linear[1];
1939       pfiv->dimoff[1] = 0;
1940       pfiv->dnum[1] = 1;
1941     } else {
1942       pfiv->gr2ab[1] = pgr1->igrab;
1943       pfiv->ab2gr[1] = pgr1->iabgr;
1944       pfiv->grvals[1] = cpscal (pgr1->ivals, pgr1->ilinr, 0, pgr1->idim);
1945       if (pfiv->grvals[1]==NULL) goto etrn;
1946       pfiv->abvals[1] = cpscal (pgr1->iavals, pgr1->ilinr, 1, pgr1->idim);
1947       if (pfiv->abvals[1]==NULL) goto etrn;
1948       pfiv->linear[1] = pgr1->ilinr;
1949       pfiv->dimoff[1] = pgr1->dimmin[1]-1;
1950       pfiv->dnum[1] = pgr1->isiz;;
1951       pfiv->gr2ab[0] = pfi->gr2ab[0];
1952       pfiv->ab2gr[0] = pfi->ab2gr[0];
1953       pfiv->grvals[0] = cpscal (pfi->grvals[0], pfi->linear[0], 0, 0);
1954       if (pfiv->grvals[0]==NULL) goto etrn;
1955       pfiv->abvals[0] = cpscal (pfi->abvals[0], pfi->linear[0], 1, 0);
1956       if (pfiv->abvals[0]==NULL) goto etrn;
1957       pfiv->linear[0] = pfi->linear[0];
1958       pfiv->dimoff[0] = 0;
1959       pfiv->dnum[0] = 1;
1960     }
1961   }
1962   else {
1963     pfiv->gr2ab[0] = pfi->gr2ab[0];
1964     pfiv->ab2gr[0] = pfi->ab2gr[0];
1965     pfiv->grvals[0] = cpscal (pfi->grvals[0], pfi->linear[0], 0, 0);
1966     if (pfiv->grvals[0]==NULL) goto etrn;
1967     pfiv->abvals[0] = cpscal (pfi->abvals[0], pfi->linear[0], 1, 0);
1968     if (pfiv->abvals[0]==NULL) goto etrn;
1969     pfiv->linear[0] = pfi->linear[0];
1970     pfiv->dimoff[0] = 0;
1971     pfiv->dnum[0] = 1;
1972     pfiv->gr2ab[1] = pfi->gr2ab[1];
1973     pfiv->ab2gr[1] = pfi->ab2gr[1];
1974     pfiv->grvals[1] = cpscal (pfi->grvals[1], pfi->linear[1], 0, 1);
1975     if (pfiv->grvals[1]==NULL) goto etrn;
1976     pfiv->abvals[1] = cpscal (pfi->abvals[1], pfi->linear[1], 1, 1);
1977     if (pfiv->abvals[1]==NULL) goto etrn;
1978     pfiv->linear[1] = pfi->linear[1];
1979     pfiv->dimoff[1] = 0;
1980     pfiv->dnum[1] = 1;
1981   }
1982 
1983   /* If the first grid is all the data we need, then we are pretty
1984      much done.  */
1985 
1986   if (izmin==izmax && itmin==itmax) {
1987     if (pgr1->idim<0) {
1988       pfiv->rbuf = (float *)malloc(sizeof(float));
1989       if (pfiv->rbuf==NULL) {
1990         gaprnt (0,"Define Error:  Unable to allocate data memory\n");
1991         goto retrn;
1992       }
1993       *(pfiv->rbuf) = *(pgr1->grid);
1994     } else {
1995       pfiv->rbuf = pgr1->grid;
1996     }
1997     pgr1->grid = NULL;
1998     siz = pgr1->isiz;
1999     siz = siz * pgr1->jsiz;
2000   } else {
2001 
2002     /* We need to get multiple grids.  Set this up.  */
2003 
2004     if (pgr1->idim>-1) free(pgr1->grid);
2005     pgr1->grid = NULL;
2006     pst->result.pgr = NULL;
2007 
2008     /* Size and allocate the storage for the defined object */
2009 
2010     gsiz = pgr1->isiz*pgr1->jsiz;
2011     siz = gsiz * pfiv->dnum[2];
2012     siz = siz * pfiv->dnum[3];
2013     pfiv->rbuf = (float *)malloc(sizeof(float)*siz);
2014     if (pfiv->rbuf==NULL) {
2015       gaprnt (0,"Define Error:  Unable to allocate data memory\n");
2016       sprintf (pout,"  Size of request was %i grid elements\n",siz);
2017       gaprnt (0,pout);
2018       goto retrn;
2019     }
2020 
2021     /* Now we can loop and get all the data */
2022 
2023     res = pfiv->rbuf;
2024     for (it=itmin; it<=itmax; it++) {
2025       for (iz=izmin; iz<=izmax; iz++) {
2026         pst->dmin[2] = conv(pfi->grvals[2],(float)iz);
2027         pst->dmax[2] = pst->dmin[2];
2028         gr2t (pfi->grvals[3], (float)it, &(pst->tmin));
2029         pst->tmax = pst->tmin;
2030 
2031         rc = gaexpr(cmd, pst);
2032         if (!rc) rc = gaqsig();
2033         if (rc) {
2034           gaprnt (0,"DEFINE error:  Invalid expression. \n");
2035           goto retrn;
2036         }
2037 
2038         pgr = pst->result.pgr;
2039         if (pgr->idim!=pgr1->idim || pgr->jdim!=pgr1->jdim ||
2040             gagchk(pgr,pgr1,pgr1->idim) ||
2041             gagchk(pgr,pgr1,pgr1->jdim) ) {
2042           gaprnt (0,"Define Error: Internal Logic Check 4\n");
2043           goto retrn;
2044         }
2045 
2046         gr = pgr->grid;
2047         for (i=0; i<gsiz; i++) {
2048           *res = *gr;
2049           res++; gr++;
2050         }
2051 
2052         gafree (pst);
2053       }
2054     }
2055   }
2056 
2057   siz = siz * sizeof(float);
2058   sprintf (pout,"Define memory allocation size = %i bytes\n",siz);
2059   gaprnt (2,pout);
2060 
2061   /* Now we will chain our new object to the chain of define blocks
2062      hung off the common area */
2063 
2064   pcurr = pcm->pdf1;
2065   prev = &(pcm->pdf1);
2066   while (pcurr!=NULL) {
2067     if (cmpwrd(name,pcurr->abbrv)) {
2068       gaprnt (2,"Name already DEFINEd:  ");
2069       gaprnt (2,name);
2070       gaprnt (2,".   Will be deleted and replaced.\n");
2071       pfic = pcurr->pfi;
2072       free (pfic->rbuf);
2073       free (pfic);
2074       *prev = pcurr->pforw;
2075       psave = pcurr;
2076       pcurr = pcurr->pforw;
2077       free (psave);
2078       continue;
2079     }
2080     prev = &(pcurr->pforw);
2081     pcurr = pcurr->pforw;
2082   }
2083 
2084   *prev = pdf;          /* Chain it up */
2085   pdf->pforw = NULL;
2086   getwrd(pdf->abbrv,name,19);
2087   free (pgr1);
2088   free (pst);
2089   pcm->tmax = tmax;                    /* Restore user dim limits*/
2090   pcm->dmax[2] = zmax;
2091   pcm->vdim[2] = vdz;
2092   pcm->vdim[3] = vdt;
2093   return (0);
2094 
2095 etrn:
2096   gaprnt(0,"Memory allocation error for Define\n");
2097 
2098 retrn:
2099 
2100   if (pst!=NULL) {
2101     gafree (pst);
2102     free (pst);
2103   }
2104   if (pgr1!=NULL) {
2105     if (pgr1->grid!=NULL && pgr1->idim>-1) free(pgr1->grid);
2106     free(pgr1);
2107   }
2108   if (pdf!=NULL) free(pdf);
2109   if (pfiv!=NULL) {
2110     if (pfiv->rbuf!=NULL) free(pfiv->rbuf);
2111     free(pfiv);
2112   }
2113   pcm->tmax = tmax;                    /* Restore user dim limits*/
2114   pcm->dmax[2] = zmax;
2115   pcm->vdim[2] = vdz;
2116   pcm->vdim[3] = vdt;
2117   return(1);
2118 }
2119 
2120 /* Handle query command */
2121 
2122 char *gxnms[23] = {"Clear","Contour","Shaded","Vector","Grid","Fgrid",
2123          "Line","Scatter","Bar","Stream","Value","Model","Wxsym","Line",
2124          "Line","Barb","GrFill","LineFill","","","Fwrite","Fstn","Stat"};
2125 /* messages for query gxout */
2126 static char *gxout0D[3] = {"Display", "Stat", "Print"};
2127 static char *gxout1D[5] = {"0", "Line", "Bar", "Errbar", "Linefile"};
2128 static char *gxout1Da[3] = { "0", "Vector", "Barb"};
2129 static char *gxout2Da[22] = {"0", "Contour", "Shaded","Grid", "4", "5", "Fgrid",
2130 			 "Fwrite", "8", "9", "Grfill", "Pgrid", "12", "13",
2131 			 "14","15","16","17","18","19","Latsgrid","Latsdata"};
2132 static char *gxout2Db[10] = {"0", "1", "2", "Grid", "Vector", "Scatter", "6", "7",
2133 			   "Stream", "Barb"};
2134 static char *gxoutStn[8] = {"0", "Value", "Barb", "Findstn", "Model", "Wxsym", "6"
2135 			    "Stnmark"};
2136 
2137 char *dweek[8] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat","???"};
2138 
gaqury(char * cmd,char * ccc,struct gacmn * pcm)2139 int gaqury (char *cmd, char *ccc, struct gacmn *pcm) {
2140 float (*conv) (float *, float);
2141 struct gafile *pfi;
2142 struct gavar *pvar;
2143 struct gadefn *pdf;
2144 struct dt dtim;
2145 struct xinfo xinf;
2146 struct gdlg dlg;
2147 int i, ii, j, cnt, fnum, flag, etype, info[10];
2148 float v,x,y,v1,v2,lon,lat,rinfo[10];
2149 char *arg,lab[20],lab2[20],*ch;
2150 int hdrflg,hdrflgd,rc,error,n_atts,n_gatts;
2151 char *varnam;
2152 #if USESDF == 1
2153 struct dt tdef,tdefi;
2154 char *tfile,*tfile2;
2155 int sdid,ncid,closethisfilelater=0;
2156 VAR_INFO *sdfpvar;
2157 #endif
2158   arg = nxtwrd(cmd);
2159   if (arg == NULL) {
2160     gaprnt (2,"GrADS Version " GRADS_VERSION " --- " GRADS_DESC "\n");
2161     gaprnt (2,"query or q Options: \n");
2162     gaprnt (2,"  q attr     Returns global and variable attributes for an open file\n");
2163     gaprnt (2,"  q config   Returns GrADS configuration information\n");
2164     gaprnt (2,"  q ctlinfo  Returns contents of data descriptor file\n");
2165     gaprnt (2,"  q define   Lists currently defined variables\n");
2166     gaprnt (2,"  q defval   Returns value of a defined variable at a point\n");
2167     gaprnt (2,"  q dialog   Launches a dialog box\n");
2168     gaprnt (2,"  q dims     Returns current dimension environment\n");
2169     gaprnt (2,"  q files    Lists all open files\n");
2170     gaprnt (2,"  q file     Returns info on a particular file\n");
2171     gaprnt (2,"  q fwrite   Returns status of fwrite output file\n");
2172     gaprnt (2,"  q gxinfo   Returns graphics environment info\n");
2173     gaprnt (2,"  q gxout:   Gives current gxout settings\n");
2174 #if USELATS == 1
2175     gaprnt (2,"  q lats     Returns status of GrADS-LATS Interface\n");
2176 #endif
2177     gaprnt (2,"  q pos      Waits for mouse click, returns position and widget info\n");
2178     gaprnt (2,"  q shades   Returns colors and levels of shaded contours\n");
2179     gaprnt (2,"  q string   Returns width of a string\n");
2180     gaprnt (2,"  q time     Returns info about time settings\n");
2181     gaprnt (2,"  q udft     Returns the user defined function table\n");
2182     gaprnt (2,"  q xinfo    Returns characteristics of graphics display window\n");
2183     gaprnt (2,"  q xy2w     Converst XY screen to world coordinates\n");
2184     gaprnt (2,"  q xy2gr    Converts XY screen to grid coordinates\n");
2185     gaprnt (2,"  q w2xy     Converts world to XY screen coordinates\n");
2186     gaprnt (2,"  q w2gr     Converts world to grid coordinates\n");
2187     gaprnt (2,"  q gr2w     Converts grid to world coordinates\n");
2188     gaprnt (2,"  q gr2xy    Converts grid to XY screen coordinates\n");
2189     gaprnt (2,"  q pp2xy    Converts virtual page XY to real page XY coordinates\n");
2190     gaprnt (2,"Details about argument syntax for some of these options are in the \n");
2191     gaprnt (2,"online documentation: http://www.iges.org/grads/gadoc/gradcomdquery.html\n");
2192   }
2193   else if (cmpwrd(arg,"dialog")) {
2194     if ( (arg = nxtwrd (arg)) == NULL) goto errdl;
2195     if ( valprs(arg,&(dlg.x)) == NULL ) goto dialog;
2196     if ( (arg = nxtwrd (arg)) == NULL) goto errdl;
2197     if ( valprs(arg,&(dlg.y)) == NULL ) goto errdl;
2198     if ( (arg = nxtwrd (arg)) == NULL) goto errdl;
2199     if ( valprs(arg,&(dlg.w)) == NULL ) goto errdl;
2200     if ( (arg = nxtwrd (arg)) == NULL) goto errdl;
2201     if ( valprs(arg,&(dlg.h)) == NULL ) goto errdl;
2202     dlg.pc = pcm->dlgpc;
2203     dlg.fc = pcm->dlgfc;
2204     dlg.bc = pcm->dlgbc;
2205     dlg.oc = pcm->dlgoc;
2206     dlg.th = pcm->dlgth;
2207     dlg.nu = pcm->dlgnu;
2208 
2209     if ( (arg = nxtwrd (arg)) == NULL) goto errdl;
2210     dlg.len = 0;
2211     while (*(arg+dlg.len)) dlg.len++;
2212     dlg.ch = (char *)malloc(dlg.len+1);
2213     *(dlg.ch+dlg.len) = '\0';
2214     if (dlg.ch==NULL) {
2215       gaprnt(0,"Memory allocation error; QUERY DIALOG cmd\n");
2216      return(1);
2217     }
2218     for (i=0; i<dlg.len; i++) *(dlg.ch+i) = *(ccc+(arg-cmd)+i);
2219     ch = gxdlg(&dlg);
2220     gaprnt (2,ch);
2221     free (ch);
2222     return (0);
2223 
2224     errdl:
2225     gaprnt (0,"QUERY error: Syntax is QUERY DIALOG x y w h ");
2226     gaprnt (0,"string\n");
2227     return (1);
2228 
2229     dialog:
2230     dlg.x = -1;
2231     dlg.y = -1;
2232     dlg.w = -1;
2233     dlg.h = -1;
2234     dlg.pc = pcm->dlgpc;
2235     dlg.fc = pcm->dlgfc;
2236     dlg.bc = pcm->dlgbc;
2237     dlg.oc = pcm->dlgoc;
2238     dlg.th = pcm->dlgth;
2239     dlg.nu = pcm->dlgnu;
2240 
2241     dlg.len = 0;
2242     while (*(arg+dlg.len)) dlg.len++;
2243     dlg.ch = (char *)malloc(dlg.len+1);
2244     *(dlg.ch+dlg.len) = '\0';
2245     if (dlg.ch==NULL) {
2246       gaprnt(0,"Memory allocation error; QUERY DIALOG cmd\n");
2247       return(1);
2248     }
2249     for (i=0; i<dlg.len; i++) *(dlg.ch+i) = *(ccc+(arg-cmd)+i);
2250     ch = gxdlg(&dlg);
2251     gaprnt (2,ch);
2252     free (ch);
2253     return (0);
2254   }
2255   else if (cmpwrd(arg,"string")) {
2256     if ( (arg = nxtwrd (arg)) == NULL) {
2257       gaprnt(0,"Query String Error: Missing String\n");
2258     } else {
2259       i = 0;
2260       while (*(arg+i)!='\0' && *(arg+i)!='\n') i++;
2261       v = 0.2;
2262       gxchln (ccc+(arg-cmd),i,pcm->strhsz,&v);
2263       sprintf (pout,"String Width = %g\n",v);
2264       gaprnt (2,pout);
2265     }
2266   }
2267   else if (cmpwrd(arg,"fwrite")) {
2268     if (pcm->ffile) gaprnt(2,"FWrite file is open\n");
2269     else gaprnt(2,"FWrite file is closed\n");
2270     if (pcm->fwname) {
2271       gaprnt (2,"FWrite file name is: ");
2272       gaprnt (2,pcm->fwname);
2273       gaprnt (2,"\n");
2274     } else gaprnt (2,"FWrite file name is: grads.fwrite\n");
2275     if (pcm->fwenflg==0) {
2276       gaprnt (2,"FWrite byte order is little_endian ");
2277     } else {
2278       gaprnt (2,"FWrite byte order is big_endian ");
2279     }
2280     if (BYTEORDER==0) {
2281       gaprnt (2,"; machine byte order is little_endian\n");
2282     } else {
2283       gaprnt (2,"; machine byte order is big_endian\n");
2284     }
2285   }
2286 
2287   /* Configuration options */
2288   else if (cmpwrd(arg,"config")) {
2289       gacfg(2);
2290   }
2291 #if USELATS == 1
2292 
2293   /* LATS state */
2294   else if (cmpwrd(arg,"lats")) {
2295 
2296     sprintf(pout,"GrADS-LATS Interface State:\n");
2297     gaprnt (2,pout);
2298 
2299     /*=== lats_parmtab ===*/
2300     sprintf(pout,"%s \t:parameter table file (by \"set lats parmtab \")\n",pcm->glats.ptname);
2301     gaprnt (2,pout);
2302 
2303     /*=== lats_create ===*/
2304     sprintf(pout,"%d \t: data CONVENTION parameter (by \"set lats convention\")\n",pcm->glats.convention);
2305     gaprnt (2,pout);
2306 
2307     sprintf(pout,"%d \t: data CALENDAR parameter (by \"set lats calendar\")\n",pcm->glats.calendar);
2308     gaprnt (2,pout);
2309 
2310     sprintf(pout,"%d \t: data time FREQEUENCY  parameter (by \"set lats frequency\")\n",pcm->glats.frequency);
2311     gaprnt (2,pout);
2312 
2313     sprintf(pout,"%d \t: DELTAT time (# of intervals) (by \"set lats deltat\")\n",pcm->glats.deltat);
2314     gaprnt (2,pout);
2315 
2316     sprintf(pout,"%d \t: BASETIME year (# of intervals) (by \"set lats basetime id_file yr mo da hr\")\n",pcm->glats.lyr);
2317     gaprnt (2,pout);
2318 
2319     sprintf(pout,"%d \t: BASETIME month (# of intervals) (e.g., \"set lats basetime 1 1997 5 31 00\")\n",pcm->glats.lmo);
2320     gaprnt (2,pout);
2321 
2322     sprintf(pout,"%d \t: BASETIME day (# of intervals) (e.g., \"set lats basetime 1 1997 5 31 00\")\n",pcm->glats.lda);
2323     gaprnt (2,pout);
2324 
2325     sprintf(pout,"%d \t: BASETIME hour (# of intervals) (e.g., \"set lats basetime 1 1997 5 31 00\")\n",pcm->glats.lhr);
2326     gaprnt (2,pout);
2327 
2328     sprintf(pout,"%s \t: MODEL (by \"set lats model\")\n",pcm->glats.model);
2329     gaprnt (2,pout);
2330 
2331     sprintf(pout,"%s \t: data CENTER (by \"set lats center\")\n",pcm->glats.center);
2332     gaprnt (2,pout);
2333 
2334     sprintf(pout,"%s \t: COMMENT (by \"set lats comment\")\n",pcm->glats.comment);
2335     gaprnt (2,pout);
2336 
2337     sprintf(pout,"%s \t: output file (by \"set lats open \")\n",pcm->glats.oname);
2338     gaprnt (2,pout);
2339 
2340 
2341 /*=== lats_grid ===*/
2342     sprintf(pout,"%d \t: GRIDTYPE grid type parameter(by \"set lats gridtype\")\n",pcm->glats.gridtype);
2343     gaprnt (2,pout);
2344 
2345     sprintf(pout,"%s \t: GRIDNAME grid type parameter(by \"set lats gridname\")\n",pcm->glats.gridname);
2346     gaprnt (2,pout);
2347 
2348     sprintf(pout,"%4d \t: linear in longitude (0=no,1=yes)\n",pcm->glats.ilinear);
2349     gaprnt (2,pout);
2350     sprintf(pout,"%4d \t: linear in latitude  (0=no,1=yes)\n",pcm->glats.jlinear);
2351     gaprnt (2,pout);
2352     sprintf(pout,"%4d \t: # longitude points (nlon) \n",pcm->glats.nlon);
2353     gaprnt (2,pout);
2354     sprintf(pout,"%4d \t: # latitude  points (nlat) \n",pcm->glats.nlat);
2355     gaprnt (2,pout);
2356 
2357     sprintf(pout,"    ( %10.4f , %10.4f ) \t: (   1,   1) lon lat\n",pcm->glats.lon_1,pcm->glats.lat_1);
2358     gaprnt (2,pout);
2359     sprintf(pout,"    ( %10.4f , %10.4f ) \t: (nlat,nlon) lon lat\n",pcm->glats.lon_nlon,pcm->glats.lat_nlat);
2360     gaprnt (2,pout);
2361 
2362 
2363 
2364 /*=== lats_vertdim ===*/
2365     sprintf(pout,"%s \t: vertical dimension by \"set lats levels (lev1 ... levN) vertdimname\")\n",pcm->glats.vertdimname);
2366     gaprnt (2,pout);
2367 
2368     sprintf(pout,"%d \t: # of vertical levels (by \"set lats levels (lev1 ... levN) vertdimname\")\n",pcm->glats.nlev);
2369     gaprnt (2,pout);
2370 
2371     for(i=0; i<(pcm->glats.nlev) ; i++) {
2372       sprintf(pout," %g",pcm->glats.levels[i]);
2373       gaprnt (2,pout);
2374     }
2375 
2376     sprintf(pout,"vertical levels\n");
2377     gaprnt (2,pout);
2378 
2379 
2380 /*=== lats_var ===*/
2381 
2382     sprintf(pout,"%s \t: VAR output parameter (by \"set lats var (name timestat [level])\")\n",pcm->glats.var);
2383     gaprnt (2,pout);
2384 
2385     sprintf(pout,"%d \t: VAR TIMESTAT time statistic parameter\n",pcm->glats.timestat);
2386     gaprnt (2,pout);
2387 
2388 /*==== LATS internal id's ===*/
2389 
2390     sprintf(pout,"%d \t: id_file LATS internal ID\n",pcm->glats.id_file);
2391     gaprnt (2,pout);
2392 
2393     sprintf(pout,"%d \t: id_grid LATS internal ID\n",pcm->glats.id_grid);
2394     gaprnt (2,pout);
2395 
2396     sprintf(pout,"%d \t: id_lev LATS internal ID\n",pcm->glats.id_lev);
2397     gaprnt (2,pout);
2398 
2399     sprintf(pout,"%d \t: id_var LATS internal ID\n",pcm->glats.id_var);
2400     gaprnt (2,pout);
2401 
2402     sprintf(pout,"%d \t: id_user_file LATS internal ID\n",pcm->glats.id_user_file);
2403     gaprnt (2,pout);
2404 
2405     sprintf(pout,"%d \t: id_user_grid LATS internal ID\n",pcm->glats.id_user_grid);
2406     gaprnt (2,pout);
2407 
2408     sprintf(pout,"%d \t: id_user_var LATS internal ID\n",pcm->glats.id_user_var);
2409     gaprnt (2,pout);
2410 
2411     sprintf(pout,"%d \t: id_user_lev LATS internal ID\n",pcm->glats.id_user_lev);
2412     gaprnt (2,pout);
2413 
2414     sprintf(pout,"%d \t: id_user_write LATS internal ID\n",pcm->glats.id_user_write);
2415     gaprnt (2,pout);
2416 
2417   }
2418 
2419 #endif
2420 
2421   else if (cmpwrd(arg,"dims")||cmpwrd(arg,"dim")) {
2422     if (pcm->pfi1==NULL) {
2423       gaprnt (0,"No files open\n");
2424       return (1);
2425     }
2426     pfi = pcm->pfid;
2427     sprintf (pout,"Default file number is: %i \n",pcm->dfnum);
2428     gaprnt (2,pout);
2429 
2430     if (pfi->type==2) {
2431       v1 = pcm->dmin[0];
2432       v2 = pcm->dmax[0];
2433     } else {
2434       conv = pfi->ab2gr[0];
2435       v1 = conv(pfi->abvals[0],pcm->dmin[0]);
2436       v2 = conv(pfi->abvals[0],pcm->dmax[0]);
2437     }
2438     if (pcm->dmin[0]==pcm->dmax[0]) {
2439       sprintf (pout,"X is fixed     Lon = %g  X = %g\n",pcm->dmin[0],v1);
2440     } else {
2441       sprintf (pout,"X is varying   Lon = %g to %g   X = %g to %g\n",
2442            pcm->dmin[0],pcm->dmax[0],v1,v2);
2443     }
2444     gaprnt (2,pout);
2445 
2446     if (pfi->type==2) {
2447       v1 = pcm->dmin[1];
2448       v2 = pcm->dmax[1];
2449     } else {
2450       conv = pfi->ab2gr[1];
2451       v1 = conv(pfi->abvals[1],pcm->dmin[1]);
2452       v2 = conv(pfi->abvals[1],pcm->dmax[1]);
2453     }
2454     if (pcm->dmin[1]==pcm->dmax[1]) {
2455       sprintf (pout,"Y is fixed     Lat = %g  Y = %g\n",pcm->dmin[1],v1);
2456     } else {
2457       sprintf (pout,"Y is varying   Lat = %g to %g   Y = %g to %g\n",
2458            pcm->dmin[1],pcm->dmax[1],v1,v2);
2459     }
2460     gaprnt (2,pout);
2461 
2462     if (pfi->type==2) {
2463       v1 = pcm->dmin[2];
2464       v2 = pcm->dmax[2];
2465     } else {
2466       conv = pfi->ab2gr[2];
2467       v1 = conv(pfi->abvals[2],pcm->dmin[2]);
2468       v2 = conv(pfi->abvals[2],pcm->dmax[2]);
2469     }
2470     if (pcm->dmin[2]==pcm->dmax[2]) {
2471       sprintf (pout,"Z is fixed     Lev = %g  Z = %g\n",pcm->dmin[2],v1);
2472     } else {
2473       sprintf (pout,"Z is varying   Lev = %g to %g   Z = %g to %g\n",
2474            pcm->dmin[2],pcm->dmax[2],v1,v2);
2475     }
2476     gaprnt (2,pout);
2477 
2478     v1 = t2gr(pfi->abvals[3],&(pcm->tmin));
2479     v2 = t2gr(pfi->abvals[3],&(pcm->tmax));
2480     if (pcm->tmin.mn==0) gat2ch(&(pcm->tmin),4,lab);
2481     else gat2ch (&(pcm->tmin),5,lab);
2482     if (pcm->tmax.mn==0) gat2ch(&(pcm->tmax),4,lab2);
2483     else gat2ch (&(pcm->tmax),5,lab2);
2484     if (v1==v2) {
2485       sprintf (pout,"T is fixed     Time = %s  T = %g\n",lab,v1);
2486     } else {
2487       sprintf (pout,"T is varying   Time = %s to %s  T = %g to %g\n",
2488                   lab,lab2,v1,v2);
2489     }
2490     gaprnt (2,pout);
2491   }
2492   else if (cmpwrd(arg,"w2xy") || cmpwrd(arg,"w2gr")) {
2493     if (pcm->xdim<0 || pcm->ydim<0) {
2494       gaprnt (2,"No scaling environment\n");
2495       return (0);
2496     }
2497     flag = 0;
2498     if (cmpwrd(arg,"w2gr")) flag = 1;
2499     if ( (arg = nxtwrd (arg)) == NULL) goto errw;
2500     if (pcm->xdim == 3) {
2501       if ( adtprs(arg,&(pcm->tmin),&dtim) == NULL) goto errw;
2502       lon = t2gr(pcm->xgrval,&dtim);
2503     } else {
2504       if ( valprs(arg,&lon) == NULL ) goto errw;
2505     }
2506     if ( (arg = nxtwrd (arg)) == NULL) goto errw;
2507     if (pcm->ydim == 3) {
2508       if ( adtprs(arg,&(pcm->tmin),&dtim) == NULL) goto errw;
2509       lat = t2gr(pcm->ygrval,&dtim);
2510     } else {
2511       if ( valprs(arg,&lat) == NULL ) goto errw;
2512     }
2513 
2514     if (flag) {
2515       conv = pcm->xab2gr;
2516       x = lon;
2517       if (conv && pcm->xdim!=3) x = conv(pcm->xabval, lon);
2518       conv = pcm->yab2gr;
2519       y = lat;
2520       if (conv && pcm->ydim!=3) y = conv(pcm->yabval, lat);
2521       sprintf (pout,"%s = %g  %s = %g\n",ccdims[pcm->xdim],x,
2522                   ccdims[pcm->ydim],y);
2523       gaprnt(2,pout);
2524     } else {
2525       gxconv (lon,lat,&x,&y,2);
2526       sprintf (pout,"X = %g  Y = %g\n",x,y);
2527       gaprnt(2,pout);
2528     }
2529 
2530     return (0);
2531 
2532     errw:
2533     if (flag) sprintf(pout,"Query Error: Syntax is QUERY W2GR %s %s\n",
2534              cdims[pcm->xdim+1],cdims[pcm->ydim+1]);
2535     else sprintf(pout,"Query Error: Syntax is QUERY W2XY %s %s\n",
2536              cdims[pcm->xdim+1],cdims[pcm->ydim+1]);
2537     gaprnt (0,pout);
2538     return (1);
2539   }
2540   else if (cmpwrd(arg,"gr2w") || cmpwrd(arg,"gr2xy")) {
2541     if (pcm->xdim<0 || pcm->ydim<0) {
2542       gaprnt (2,"No scaling environment\n");
2543       return (0);
2544     }
2545     flag = 0;
2546     if (cmpwrd(arg,"gr2w")) flag = 1;
2547     if ( (arg = nxtwrd (arg)) == NULL) goto errgr;
2548     if ( valprs(arg,&x) == NULL ) goto errgr;
2549     if ( (arg = nxtwrd (arg)) == NULL) goto errgr;
2550     if ( valprs(arg,&y) == NULL ) goto errgr;
2551     conv = pcm->xgr2ab;
2552     lon = x;
2553     if (conv && pcm->xdim!=3) lon = conv(pcm->xgrval, x);
2554     conv = pcm->ygr2ab;
2555     lat = y;
2556     if (conv && pcm->ydim!=3) lat = conv(pcm->ygrval, y);
2557     if (flag) {
2558       if (pcm->xdim==3) {
2559         gr2t (pcm->xgrval, lon, &dtim);
2560         gat2ch (&dtim, 5, lab);
2561         sprintf (pout,"%s = %s  %s = %g\n",cdims[pcm->xdim+1],lab,
2562                     cdims[pcm->ydim+1],lat);
2563         gaprnt(2,pout);
2564       } else if (pcm->ydim==3) {
2565         gr2t (pcm->ygrval, lat, &dtim);
2566         gat2ch (&dtim, 5, lab);
2567         sprintf (pout,"%s = %g  %s = %s\n",cdims[pcm->xdim+1],lon,
2568                     cdims[pcm->ydim+1],lab);
2569         gaprnt(2,pout);
2570       } else {
2571         sprintf (pout,"%s = %g  %s = %g\n",cdims[pcm->xdim+1],lon,
2572                     cdims[pcm->ydim+1],lat);
2573         gaprnt(2,pout);
2574       }
2575     } else {
2576       gxconv (lon,lat,&x,&y,2);
2577       sprintf (pout,"X = %g  Y = %g\n",x,y);
2578       gaprnt(2,pout);
2579     }
2580     return (0);
2581 
2582     errgr:
2583     if (flag) sprintf(pout,"Query Error: Syntax is QUERY GR2W %s %s\n",
2584              ccdims[pcm->xdim],ccdims[pcm->ydim]);
2585     else sprintf(pout,"Query Error: Syntax is QUERY GR2XY %s %s\n",
2586              ccdims[pcm->xdim],ccdims[pcm->ydim]);
2587     gaprnt (0,pout);
2588     return (1);
2589   }
2590   else if (cmpwrd(arg,"pp2xy")) {
2591     if ( (arg = nxtwrd (arg)) == NULL) goto errpp;
2592     if ( valprs(arg,&x) == NULL ) goto errpp;
2593     if ( (arg = nxtwrd (arg)) == NULL) goto errpp;
2594     if ( valprs(arg,&y) == NULL ) goto errpp;
2595     gxppvp (x, y, &x, &y);
2596     sprintf (pout,"X = %g  Y = %g\n",x,y);
2597     gaprnt(2,pout);
2598     return (0);
2599 
2600     errpp:
2601     gaprnt (0,"Query Error: Syntax is QUERY PP2XY ppx ppy\n");
2602     return (1);
2603   }
2604   else if (cmpwrd(arg,"xy2w") || cmpwrd(arg,"xy2gr")) {
2605     flag = 0;
2606     if (cmpwrd(arg,"xy2gr")) flag = 1;
2607     if ( (arg = nxtwrd (arg)) == NULL) goto errxy;
2608     if ( valprs(arg,&x) == NULL ) goto errxy;
2609     if ( (arg = nxtwrd (arg)) == NULL) goto errxy;
2610     if ( valprs(arg,&y) == NULL ) goto errxy;
2611     if (pcm->xdim<0 || pcm->ydim<0) {
2612       gaprnt (2,"No scaling environment\n");
2613     } else {
2614       gxxy2w (x,y,&lon,&lat);
2615       if (flag) {
2616         conv = pcm->xab2gr;
2617         x = lon;
2618         if (conv && pcm->xdim!=3) x = conv(pcm->xabval, lon);
2619         conv = pcm->yab2gr;
2620         y = lat;
2621         if (conv && pcm->ydim!=3) y = conv(pcm->yabval, lat);
2622         sprintf (pout,"%s = %g  %s = %g\n",ccdims[pcm->xdim],x,
2623                     ccdims[pcm->ydim],y);
2624         gaprnt(2,pout);
2625       } else {
2626         if (pcm->xdim==3) {
2627           gr2t (pcm->xgrval, lon, &dtim);
2628           gat2ch (&dtim, 5, lab);
2629           sprintf (pout,"%s = %s  %s = %g\n",cdims[pcm->xdim+1],lab,
2630                       cdims[pcm->ydim+1],lat);
2631           gaprnt(2,pout);
2632         } else if (pcm->ydim==3) {
2633           gr2t (pcm->ygrval, lat, &dtim);
2634           gat2ch (&dtim, 5, lab);
2635           sprintf (pout,"%s = %g  %s = %s\n",cdims[pcm->xdim+1],lon,
2636                       cdims[pcm->ydim+1],lab);
2637           gaprnt(2,pout);
2638         } else {
2639           sprintf (pout,"%s = %g  %s = %g\n",cdims[pcm->xdim+1],lon,
2640                       cdims[pcm->ydim+1],lat);
2641           gaprnt(2,pout);
2642         }
2643       }
2644     }
2645     return (0);
2646 
2647     errxy:
2648     if (flag) gaprnt (0,"Query Error: Syntax is QUERY XY2GR x y\n");
2649     else gaprnt (0,"QUERY error: Syntax is QUERY XY2W x y\n");
2650     return (1);
2651   }
2652   else if (cmpwrd(arg,"ll2xy")) {
2653     if ( (arg = nxtwrd (arg)) == NULL) goto errll;
2654     if ( valprs(arg,&lon) == NULL ) goto errll;
2655     if ( (arg = nxtwrd (arg)) == NULL) goto errll;
2656     if ( valprs(arg,&lat) == NULL ) goto errll;
2657     gxconv (lon,lat,&x,&y,2);
2658     sprintf (pout,"%g %g\n",x,y);
2659     gaprnt (2,pout);
2660     return (0);
2661 
2662     errll:
2663     gaprnt (0,"QUERY error: Syntax is QUERY LL2XY lon lat\n");
2664     return (1);
2665   }
2666   else if (cmpwrd(arg,"defval")) {
2667     i = gaqdef (arg, pcm, 0);
2668     return (i);
2669   }
2670   else if (cmpwrd(arg,"udft")) {
2671     gaqufb();
2672     return (0);
2673   }
2674   else if (cmpwrd(arg,"ctlinfo")) {
2675     if (pcm->pfi1==NULL) {
2676       gaprnt (0,"No Files Open\n");
2677       return(1);
2678     }
2679     if ( (arg = nxtwrd (arg)) == NULL) {
2680       pfi = pcm->pfid;
2681       fnum = pcm->dfnum;
2682     } else {
2683       if ( intprs(arg,&fnum) == NULL ) {
2684         i = 0;
2685         while (*arg!=' '&&*arg!='\0'&&*arg!='\n'&&i<19) {
2686           lab[i] = *arg;
2687           arg++;
2688           i++;
2689         }
2690         lab[i] = '\0';
2691         sprintf (pout,"Invalid QUERY CTLINFO argument: %s \n",lab);
2692         gaprnt (0,pout);
2693         return (1);
2694       }
2695       pfi = pcm->pfi1;
2696       for (i=0; i<fnum-1; i++) {
2697         pfi = pfi->pforw;
2698         if (pfi==NULL) {
2699           sprintf (pout,"QUERY CTLINFO Error:  file %i not open\n",fnum);
2700           gaprnt (0,pout);
2701           return(1);
2702         }
2703       }
2704     }
2705 
2706     sprintf (pout,"dset %s\n",pfi->name);
2707     gaprnt (2,pout);
2708     sprintf (pout,"title %s\n",pfi->title);
2709     gaprnt (2,pout);
2710     sprintf (pout,"undef %g\n",pfi->undef);
2711     gaprnt (2,pout);
2712 
2713     if (pfi->type==2) {
2714       gaprnt (2,"dtype station\n");
2715       sprintf (pout,"  Tsize = %i\n",pfi->dnum[3]);
2716       gaprnt(2,pout);
2717     } else {
2718 
2719       sprintf (pout,"xdef %i",pfi->dnum[0]);
2720       gaprnt(2,pout);
2721       if (pfi->linear[0]) {
2722         conv = pfi->gr2ab[0];
2723         sprintf (pout," linear %g %g\n",conv(pfi->grvals[0],1.0),
2724                                            *(pfi->grvals[0]) );
2725         gaprnt (2,pout);
2726       } else {
2727         gaprnt(2," levels");
2728         conv = pfi->gr2ab[0];
2729         cnt = 3;
2730         for (i=1; i<=pfi->dnum[0]; i++) {
2731           sprintf (pout," %g",conv(pfi->grvals[0],(float)i));
2732           gaprnt (2,pout);
2733           cnt++;
2734           if (cnt>10 && i!=pfi->dnum[0]) {
2735             gaprnt (2,"\n"); cnt = 1;
2736           }
2737         }
2738         gaprnt (2,"\n");
2739       }
2740 
2741       sprintf (pout,"ydef %i",pfi->dnum[1]);
2742       gaprnt(2,pout);
2743       if (pfi->linear[1]) {
2744         conv = pfi->gr2ab[1];
2745         sprintf (pout," linear %g %g\n",conv(pfi->grvals[1],1.0),
2746                                            *(pfi->grvals[1]) );
2747         gaprnt (2,pout);
2748       } else {
2749         gaprnt(2," levels");
2750         conv = pfi->gr2ab[1];
2751         cnt = 3;
2752         for (i=1; i<=pfi->dnum[1]; i++) {
2753           sprintf (pout," %g",conv(pfi->grvals[1],(float)i));
2754           gaprnt (2,pout);
2755           cnt++;
2756           if (cnt>10 && i!=pfi->dnum[1]) {
2757             gaprnt (2,"\n"); cnt = 1;
2758           }
2759         }
2760         gaprnt (2,"\n");
2761       }
2762 
2763       sprintf (pout,"zdef %i",pfi->dnum[2]);
2764       gaprnt(2,pout);
2765       if (pfi->linear[2]) {
2766         conv = pfi->gr2ab[2];
2767         sprintf (pout," linear %g %g\n",conv(pfi->grvals[2],1.0),
2768                                            *(pfi->grvals[2]) );
2769         gaprnt (2,pout);
2770       } else {
2771         gaprnt(2," levels");
2772         conv = pfi->gr2ab[2];
2773         cnt = 3;
2774         for (i=1; i<=pfi->dnum[2]; i++) {
2775           sprintf (pout," %g",conv(pfi->grvals[2],(float)i));
2776           gaprnt (2,pout);
2777           cnt++;
2778           if (cnt>10 && i!=pfi->dnum[2]) {
2779             gaprnt (2,"\n"); cnt = 1;
2780           }
2781         }
2782         gaprnt (2,"\n");
2783       }
2784 
2785       gr2t (pfi->grvals[3],1.0,&dtim);
2786       if (dtim.mn==0) gat2ch (&dtim,4,lab);
2787       else gat2ch (&dtim,5,lab);
2788       if (*(pfi->grvals[3]+5)!=0) {
2789         sprintf (pout,"tdef %i linear %s %gmo\n",pfi->dnum[3],lab,
2790                  *(pfi->grvals[3]+5));
2791       } else {
2792         sprintf (pout,"tdef %i linear %s %gmn\n",pfi->dnum[3],lab,
2793                  *(pfi->grvals[3]+6));
2794       }
2795       gaprnt (2,pout);
2796 
2797       sprintf (pout,"vars %i\n",pfi->vnum);
2798       gaprnt (2,pout);
2799       pvar = pfi->pvar1;
2800       for (i=0;i<pfi->vnum;i++) {
2801         if (pvar->units[1] != -999) {
2802           if (pvar->units[2] != -999) {
2803             if (pvar->units[3] != -999) {
2804 	      sprintf (pout,"%s %i %i,%i,%i,%i %s\n",
2805 		       pvar->abbrv,pvar->levels,pvar->units[0],pvar->units[1],
2806 		       pvar->units[2],pvar->units[3],pvar->varnm);
2807             } else {
2808 	      sprintf (pout,"%s %i %i,%i,%i %s\n",
2809 		       pvar->abbrv,pvar->levels,pvar->units[0],pvar->units[1],
2810 		       pvar->units[2],pvar->varnm);
2811             }
2812           } else {
2813 	    sprintf (pout,"%s %i %i,%i %s\n",
2814 		     pvar->abbrv,pvar->levels,pvar->units[0],pvar->units[1],pvar->varnm);
2815           }
2816         } else {
2817 	  sprintf (pout,"%s %i %i %s\n",pvar->abbrv,pvar->levels,pvar->units[0], pvar->varnm);
2818         }
2819         gaprnt(2,pout);
2820         pvar++;
2821       }
2822     }
2823   }
2824   else if (cmpwrd(arg,"file")) {
2825     if (pcm->pfi1==NULL) {
2826       gaprnt (0,"No Files Open\n");
2827       return(1);
2828     }
2829     if ( (arg = nxtwrd (arg)) == NULL) {
2830       pfi = pcm->pfid;
2831       fnum = pcm->dfnum;
2832     } else {
2833       if ( intprs(arg,&fnum) == NULL ) {
2834         i = 0;
2835         while (*arg!=' '&&*arg!='\0'&&*arg!='\n'&&i<19) {
2836           lab[i] = *arg;
2837           arg++;
2838           i++;
2839         }
2840         lab[i] = '\0';
2841         sprintf (pout,"Invalid QUERY FILE argument: %s \n",lab);
2842         gaprnt (0,pout);
2843         return (1);
2844       }
2845       pfi = pcm->pfi1;
2846       for (i=0; i<fnum-1; i++) {
2847         pfi = pfi->pforw;
2848         if (pfi==NULL) {
2849           sprintf (pout,"QUERY FILE Error:  file %i not open\n",fnum);
2850           gaprnt (0,pout);
2851           return(1);
2852         }
2853       }
2854     }
2855 
2856     sprintf (pout,"File %i : %s\n",fnum,pfi->title);
2857     gaprnt (2,pout);
2858     sprintf (pout,"  Descriptor: %s\n",pfi->dnam);
2859     gaprnt (2,pout);
2860     sprintf (pout,"  Binary: %s\n",pfi->name);
2861     gaprnt (2,pout);
2862 
2863     if (pfi->type==2) {
2864       if (pfi->bufrflg) {
2865 	gaprnt (2,"  Type = BUFR Station Data\n");
2866       } else {
2867 	gaprnt (2,"  Type = Station Data\n");
2868       }
2869       sprintf (pout,"  Tsize = %i\n",pfi->dnum[3]);
2870       gaprnt(2,pout);
2871     } else {
2872       gaprnt (2,"  Type = Gridded\n");
2873       sprintf (pout,"  Xsize = %i  Ysize = %i  Zsize = %i  Tsize = %i\n",
2874          pfi->dnum[0],pfi->dnum[1],pfi->dnum[2],pfi->dnum[3]);
2875       gaprnt(2,pout);
2876     }
2877 
2878     sprintf (pout,"  Number of Variables = %i\n",pfi->vnum);
2879     gaprnt (2,pout);
2880     pvar = pfi->pvar1;
2881     for (i=0;i<pfi->vnum;i++) {
2882       sprintf (pout,"    %s %i %i %s\n",pvar->abbrv,pvar->levels,pvar->units[0],pvar->varnm);
2883       gaprnt(2,pout);
2884       pvar++;
2885     }
2886 
2887   }
2888   else if (cmpwrd(arg,"gxout")) {
2889     sprintf (pout,"General = %s\n",gxout0D[pcm->gout1]);
2890     gaprnt(2,pout);
2891     sprintf (pout,"1D Graphic, 1 expr = %s\n",gxout1D[pcm->gout1]);
2892     gaprnt(2,pout);
2893     sprintf (pout,"1D Graphic, 2 expr = %s\n",gxout1Da[pcm->gout1a]);
2894     gaprnt(2,pout);
2895     sprintf (pout,"2D Graphic, 1 expr = %s\n",gxout2Da[pcm->gout2a]);
2896     gaprnt(2,pout);
2897     sprintf (pout,"2D Graphic, 2 expr = %s\n",gxout2Db[pcm->gout2b]);
2898     gaprnt(2,pout);
2899     sprintf (pout,"Station data = %s\n",gxoutStn[pcm->goutstn]);
2900     gaprnt(2,pout);
2901   }
2902   else if (cmpwrd(arg,"gxinfo")) {
2903     sprintf (pout,"Last Graphic = %s\n",gxnms[pcm->lastgx]);
2904     gaprnt(2,pout);
2905     sprintf (pout,"Page Size = %g by %g\n",pcm->xsiz,pcm->ysiz);
2906     gaprnt(2,pout);
2907     sprintf (pout,"X Limits = %g to %g\n",pcm->xsiz1,pcm->xsiz2);
2908     gaprnt(2,pout);
2909     sprintf (pout,"Y Limits = %g to %g\n",pcm->ysiz1,pcm->ysiz2);
2910     gaprnt(2,pout);
2911     sprintf (pout,"Xaxis = %s  Yaxis = %s\n",cdims[pcm->xdim+1],
2912                   cdims[pcm->ydim+1]);
2913     gaprnt(2,pout);
2914     sprintf (pout,"Mproj = %d\n",pcm->mproj);
2915     gaprnt(2,pout);
2916   }
2917   else if (cmpwrd(arg,"xinfo")) {
2918     if (pcm->batflg) {
2919       gaprnt(2,"Batch Mode\n");
2920     } else {
2921       if ( win_data (&xinf) ) {
2922         sprintf (pout,"Window ID = %d\n",xinf.winid);
2923         gaprnt(2,pout);
2924         sprintf (pout,"Window X = %d\n",xinf.winx);
2925         gaprnt(2,pout);
2926         sprintf (pout,"Window Y = %d\n",xinf.winy);
2927         gaprnt(2,pout);
2928         sprintf (pout,"Window Width = %d\n",xinf.winw);
2929         gaprnt(2,pout);
2930         sprintf (pout,"Window Height = %d\n",xinf.winh);
2931         gaprnt(2,pout);
2932         sprintf (pout,"Window Border = %d\n",xinf.winb);
2933         gaprnt(2,pout);
2934       } else {
2935         gaprnt(2,"Error\n");
2936       }
2937     }
2938   }
2939   else if (cmpwrd(arg,"shades")) {
2940     if (pcm->shdcnt<1) {
2941       gaprnt(2,"None\n");
2942     } else {
2943       sprintf(pout,"Number of levels = %i\n",pcm->shdcnt);
2944       gaprnt(2,pout);
2945       for (i=0; i<pcm->shdcnt; i++) {
2946         if (i==0) sprintf (pout,"%i < %g\n",pcm->shdcls[i],
2947                pcm->shdlvs[1]);
2948         else if (i==pcm->shdcnt-1) sprintf(pout,"%i %g >\n",
2949                pcm->shdcls[i],pcm->shdlvs[i]);
2950         else sprintf (pout,"%i %g %g\n",pcm->shdcls[i],
2951               pcm->shdlvs[i],pcm->shdlvs[i+1]);
2952         gaprnt(2,pout);
2953       }
2954     }
2955   }
2956   else if (cmpwrd(arg,"time")) {
2957     if (pcm->pfi1==NULL) {
2958       gaprnt (0,"No Files Open\n");
2959       return(1);
2960     }
2961     if (pcm->tmin.mn==0) gat2ch(&(pcm->tmin),4,lab);
2962     else gat2ch (&(pcm->tmin),5,lab);
2963     if (pcm->tmax.mn==0) gat2ch(&(pcm->tmax),4,lab2);
2964     else gat2ch (&(pcm->tmax),5,lab2);
2965     sprintf(pout,"Time = %s to %s",lab,lab2);
2966     gaprnt (2,pout);
2967     sprintf(pout,"  %s to %s\n",dweek[dayweek(&(pcm->tmin))],
2968                          dweek[dayweek(&(pcm->tmax))]);
2969     gaprnt (2,pout);
2970   }
2971   else if (cmpwrd(arg,"bpos")||cmpwrd(arg,"pos")) {
2972     i = 1;
2973     if ( (arg = nxtwrd (arg)) != NULL){
2974       if (cmpwrd(arg,"nowait")) i = 0;
2975     }
2976     gxdbtn (i, &x, &y, &i, &etype, info, rinfo);
2977     if (etype<1) {
2978       sprintf (pout,"Position = %g %g %i %i\n",x,y,i,etype);
2979     } else if (etype==1) {
2980       sprintf (pout,"Position = %g %g %i %i %i %i\n",x,y,i,
2981                                           etype,*info,*(info+1));
2982     } else if (etype==2) {
2983       sprintf (pout,"Position = %g %g %i %i %i %g %g\n",x,y,i,
2984                                    etype,*info,*rinfo,*(rinfo+1));
2985     } else if (etype==3) {
2986       sprintf (pout,"Position = %g %g %i %i %i %i %i %i %i %i %i %i\n",
2987          x,y,i,etype,*info,*(info+1),*(info+2),*(info+3),*(info+4),
2988          *(info+5),*(info+6),*(info+7));
2989     }
2990     gaprnt (2,pout);
2991   }
2992   else if (cmpwrd(arg,"define")) {
2993     if (pcm->pdf1==NULL) {
2994       gaprnt (1,"No Defined Variables\n");
2995       return(0);
2996     } else {
2997       pdf = pcm->pdf1;
2998       while (pdf) {
2999         pfi = pdf->pfi;
3000         sprintf (pout,"%s %g\n",pdf->abbrv,*(pfi->rbuf));
3001         gaprnt (2,pout);
3002         pdf = pdf->pforw;
3003       }
3004     }
3005     return(0);
3006   }
3007   else if (cmpwrd(arg,"files")) {
3008     pfi = pcm->pfi1;
3009     if (pfi==NULL) {
3010       gaprnt (2,"No files open\n");
3011     } else {
3012       j = 1;
3013       while (pfi!=NULL) {
3014         sprintf (pout,"File %i : %s\n",j,pfi->title);
3015         gaprnt (2,pout);
3016         sprintf (pout,"  Descriptor: %s\n",pfi->dnam);
3017         gaprnt (2,pout);
3018         sprintf (pout,"  Binary: %s\n",pfi->name);
3019         gaprnt (2,pout);
3020         pfi = pfi->pforw;
3021         j++;
3022       }
3023     }
3024   }
3025   else if (cmpwrd(arg,"attr")) {
3026     if (pcm->pfi1==NULL) {
3027       gaprnt (0,"No Files Open\n");
3028       return(1);
3029     }
3030     if ( (arg = nxtwrd (arg)) == NULL) {
3031       pfi = pcm->pfid;
3032       fnum = pcm->dfnum;
3033     } else {
3034       if ( intprs(arg,&fnum) == NULL ) {
3035         i = 0;
3036         while (*arg!=' ' && *arg!='\0' && *arg!='\n' && i<19) {
3037           lab[i] = *arg;
3038           arg++;
3039           i++;
3040         }
3041         lab[i] = '\0';
3042         sprintf (pout,"Invalid QUERY ATTR argument: %s \n",lab);
3043         gaprnt (0,pout);
3044         return (1);
3045       }
3046       pfi = pcm->pfi1;
3047       for (i=0; i<fnum-1; i++) {
3048         pfi = pfi->pforw;
3049         if (pfi==NULL) {
3050           sprintf (pout,"QUERY ATTR Error: file %i not open\n",fnum);
3051           gaprnt (0,pout);
3052           return(1);
3053         }
3054       }
3055     }
3056 
3057     /* Print attributes from the descriptor file */
3058     hdrflgd=1;
3059     if (pfi->attr) {
3060       /* Print global and coordinate attributes */
3061       hdrflgd = prntgaattr (pfi, "global", hdrflgd, fnum);
3062       hdrflgd = prntgaattr (pfi, "lon",  hdrflgd, fnum);
3063       hdrflgd = prntgaattr (pfi, "lat",  hdrflgd, fnum);
3064       hdrflgd = prntgaattr (pfi, "lev",  hdrflgd, fnum);
3065       hdrflgd = prntgaattr (pfi, "time", hdrflgd, fnum);
3066       /* Print variable attributes associated with the GrADS variable name, pvar->abbrv */
3067       pvar = pfi->pvar1;
3068       for (i=0; i<pfi->vnum; i++) {
3069 	hdrflgd = prntgaattr (pfi, pvar->abbrv, hdrflgd, fnum);
3070 	pvar++;
3071       }
3072       /* Print a blank line between descriptor attrbutes and native attributes for GDS parsing */
3073       if (!hdrflgd) gaprnt(2,"\n");
3074     }
3075 
3076 
3077 #if USESDF == 1
3078     /* Print native attributes for DTYPE NetCDF, HDFSDS, and SDF/XDF data sets */
3079 
3080     /* Global attributes for dtype netcdf or sdf/xdf data set */
3081     hdrflg=1;
3082     if (pfi->ncflg==1 || pfi->ncflg==3) {
3083       /* open each data file in template set until we find one that exists */
3084       if (pfi->tmplat) {
3085 	ncid=-999; rc=0; error=0;
3086 	gr2t(pfi->grvals[3], 1.0, &tdefi);
3087 	tfile = gafndt(pfi->name, &tdefi, &tdefi, pfi->abvals[3], pfi->pchsub1, 1);
3088 #if USEHDF == 1
3089 	ncid = ncopen(tfile, NC_NOWRITE);
3090 	if (ncid == -1) error=1;
3091 #else
3092 	rc = nc_open(tfile, NC_NOWRITE, &ncid);
3093 	if (rc != NC_NOERR) error=1;
3094 #endif
3095 	if (!error) {
3096 	  closethisfilelater=1;
3097 	} else {
3098 	  for (i=2; i<=pfi->dnum[3]; i++) {
3099 	    ncid=-999; rc=0; error=0;
3100 	    gr2t(pfi->grvals[3], (float)i, &tdef);
3101 	    tfile2 = gafndt(pfi->name, &tdef, &tdefi, pfi->abvals[3], pfi->pchsub1, 1);
3102 	    if (strcmp(tfile,tfile2)!=0) {
3103 	      free(tfile);
3104 	      tfile = tfile2;
3105 #if USEHDF == 1
3106 	      ncid = ncopen(tfile2, NC_NOWRITE);
3107 	      if (ncid == -1) error=1;
3108 #else
3109 	      rc = nc_open(tfile2, NC_NOWRITE, &ncid);
3110 	      if (rc != NC_NOERR) error=1;
3111 #endif
3112 	      if (!error) {
3113 		closethisfilelater=1;
3114 		break;
3115 	      }
3116 	    }
3117 	  }
3118 	}
3119 	free(tfile);
3120       }
3121       else {
3122 	/* Copy the netcdf file id from the file structure to the local variable ncid */
3123 	if (pfi->ncflg == 1) {
3124 	  ncid = pfi->ncid;
3125 	}
3126 	else if (pfi->ncflg == 3) {
3127 	  ncid = pfi->sdf_ptr->cdfid;
3128 	}
3129 	closethisfilelater=0;
3130       }
3131       /* Retrieve netcdf global attributes */
3132       n_gatts = ncpattrs(ncid, "NC_GLOBAL", "global", hdrflg, fnum, pfi->title);
3133       if (hdrflg && n_gatts>0) hdrflg=0;
3134     }
3135 
3136 #if USEHDF == 1
3137     /* Global attributes for DTYPE HDFSDS */
3138     else if (pfi->ncflg == 2) {
3139       /* open each data file in template set until we find one that exists */
3140       if (pfi->tmplat) {
3141 	sdid=-999;
3142 	gr2t(pfi->grvals[3], 1.0, &tdefi);
3143 	tfile = gafndt(pfi->name, &tdefi, &tdefi, pfi->abvals[3], pfi->pchsub1, 1);
3144 	sdid = SDstart(tfile,1);
3145 	if (sdid != -1) {
3146 	  closethisfilelater=1;
3147 	} else {
3148 	  for (i=2; i<=pfi->dnum[3]; i++) {
3149 	    sdid=-999;
3150 	    gr2t(pfi->grvals[3], (float)i, &tdef);
3151 	    tfile2 = gafndt(pfi->name, &tdef, &tdefi, pfi->abvals[3], pfi->pchsub1, 1);
3152 	    if (strcmp(tfile,tfile2)!=0) {
3153 	      free(tfile);
3154 	      tfile = tfile2;
3155 	      sdid = SDstart(tfile2,1);
3156 	      if (sdid != -1) {
3157 		closethisfilelater=1;
3158 		break;
3159 	      }
3160 	    }
3161 	  }
3162 	}
3163 	free(tfile);
3164       }
3165       else {
3166 	/* Copy the hdf file id from the file structure to the local variable sdid */
3167 	sdid = pfi->sdid;
3168 	closethisfilelater=0;
3169       }
3170 
3171       /* Retrieve HDF global attributes */
3172       n_gatts = hdfpattrs(sdid, "foo", "global", hdrflg, fnum, pfi->title);
3173       if (hdrflg && n_gatts>0) hdrflg=0;
3174     }
3175 #endif
3176 
3177     /* Print SDF/XDF coordinate attributes */
3178     if (pfi->ncflg == 3) {
3179       n_atts = ncpattrs(pfi->sdf_ptr->cdfid, "lon", "lon", hdrflg, fnum, pfi->title);
3180       if (hdrflg && n_atts>0) hdrflg=0;
3181       n_atts = ncpattrs(pfi->sdf_ptr->cdfid, "lat", "lat", hdrflg, fnum, pfi->title);
3182       if (hdrflg && n_atts>0) hdrflg=0;
3183       n_atts = ncpattrs(pfi->sdf_ptr->cdfid, "lev", "lev", hdrflg, fnum, pfi->title);
3184       if (hdrflg && n_atts>0) hdrflg=0;
3185       n_atts = ncpattrs(pfi->sdf_ptr->cdfid, "time", "time", hdrflg, fnum, pfi->title);
3186       if (hdrflg && n_atts>0) hdrflg=0;
3187     }
3188 
3189     /* Print Variable attributes for dtype netcdf and dtype hdf. */
3190     if ((pfi->ncflg == 1) || (pfi->ncflg == 2)) {
3191       pvar = pfi->pvar1;
3192       for (i=0; i<pfi->vnum; i++) {
3193 	if (pvar->longnm) {
3194 	  varnam = pvar->longnm;
3195 	}
3196 	else {
3197 	  varnam = pvar->abbrv;
3198 	}
3199 	/* Print NetCDF variable attributes */
3200 	if (pfi->ncflg==1) {
3201 	  n_atts = ncpattrs(ncid, varnam, pvar->abbrv, hdrflg, fnum, pfi->title);
3202 	  if (hdrflg && n_atts>0) hdrflg=0;
3203 	}
3204 #if USEHDF == 1
3205 	/* Print HDF variable attributes */
3206 	if (pfi->ncflg==2) {
3207 	  n_atts = hdfpattrs(sdid, varnam, pvar->abbrv, hdrflg, fnum, pfi->title);
3208 	  if (hdrflg && n_atts>0) hdrflg=0;
3209 	}
3210 #endif
3211 	pvar++;
3212       }
3213     }
3214 
3215     /* Print variable attributes for SDF/XDF files */
3216     if (pfi->ncflg == 3) {
3217       sdfpvar = pfi->sdf_ptr->var;
3218       while (sdfpvar) {
3219 	if (sdfpvar->dimmap[0] != -1) {  /* don't print attributes for coordinate variables */
3220 	  n_atts = ncpattrs(ncid, sdfpvar->varnam, sdfpvar->gradsabbr, hdrflg, fnum, pfi->title);
3221 	  if (hdrflg && n_atts>0) hdrflg=0;
3222 	}
3223 	sdfpvar = sdfpvar->next;
3224       }
3225     }
3226 
3227     /* close the file we opened to get the attributes*/
3228     if (closethisfilelater) {
3229       if (pfi->ncflg==1 || pfi->ncflg==3) {
3230 #if USEHDF == 1
3231 	ncclose(ncid);
3232 #else
3233 	nc_close(ncid);
3234 #endif
3235       }
3236 #if USEHDF == 1
3237       if (pfi->ncflg == 2)  SDend(sdid);
3238 #endif
3239     }
3240 #endif
3241 
3242     if (hdrflg && hdrflgd) {
3243       sprintf(pout,"No Attributes for File %i : %s \n",fnum,pfi->title);
3244       gaprnt(2,pout);
3245     }
3246 
3247   } /* Matches  else if (cmpwrd(arg,"attr")) { */
3248 
3249   else {
3250     i = 0;
3251     while (*arg!=' '&&*arg!='\0'&&*arg!='\n'&&i<19) {
3252       lab[i] = *arg;
3253       arg++;
3254       i++;
3255     }
3256     lab[i] = '\0';
3257     sprintf (pout,"Invalid QUERY argument: %s \n",lab);
3258     gaprnt(0,pout);
3259   }
3260   return (0);
3261 }
3262 
3263 
3264 /* Handle help command */
3265 
gahelp(char * cmd,struct gacmn * pcm)3266 int gahelp (char *cmd, struct gacmn *pcm) {
3267 
3268   printf ("\nFor Complete Information See:  http://grads.iges.org/grads\n\n");
3269   printf ("Basic Commands:\n");
3270   printf (" OPEN <descr>      opens a data file \n");
3271   printf (" Query             shows current status \n");
3272   printf (" Clear             clears graphics window \n");
3273   printf (" SET <args>        sets options \n");
3274   printf (" Display expr      displays a variable graphically \n");
3275   printf (" QUIT              exits the program \n");
3276   return (0);
3277 }
3278 
3279 /* Handle collect command */
3280 
gacoll(char * cmd,struct gacmn * pcm)3281 int gacoll (char *cmd, struct gacmn *pcm) {
3282 struct gastat *pst;
3283 struct gaclct *clct,*clct2;
3284 int rc,i,flag,clnm;
3285 
3286   if ( (cmd=nxtwrd(cmd)) == NULL) goto collerr1;
3287   if ( intprs(cmd,&clnm) == NULL) goto collerr1;
3288   if (clnm<0 || clnm>31) goto collerr1;
3289   if ( (cmd=nxtwrd(cmd)) == NULL) goto collerr1;
3290 
3291   if (cmpwrd("free",cmd)) {
3292     clct = pcm->clct[clnm];
3293     while (clct) {
3294       free (clct->stn);
3295       clct2 = clct->forw;
3296       free (clct);
3297       clct = clct2;
3298     }
3299     pcm->clct[clnm] = NULL;
3300     return (0);
3301   }
3302 
3303   /* Check environment */
3304 
3305   if (pcm->vdim[0]!=0 || pcm->vdim[1]!=0 || (pcm->vdim[2]==1 &&
3306      pcm->vdim[3]==1) || (pcm->vdim[2] == 0 && pcm->vdim[3] == 0)) {
3307     gaprnt (0,"Collect Error: Z or T must be the only varying dimension\n");
3308     return (1);
3309   }
3310 /*  if ( (cmd=nxtwrd(cmd)) == NULL) {
3311     gaprnt (0,"Collect command error:  No expression provided\n");
3312     return (1);
3313   }*/
3314   garemb (cmd);
3315 
3316   /* Evaluate expression(s)  */
3317 
3318   pst = getpst(pcm);
3319   if (pst==NULL) return(1);
3320   rc = gapars(cmd, pst, pcm);
3321   if (rc) goto collerr2;
3322 
3323   /* Make sure returned objects are station data */
3324 
3325   flag = 0;
3326   if (pcm->type[0]!=0) flag = 1;
3327   if (pcm->numgrd>1 && pcm->type[1]!=0) flag = 1;
3328   if (flag) {
3329     gaprnt(0,"Collect Error: Station Data Required\n");
3330     rc = 1;
3331     goto collerr2;
3332   }
3333 
3334   /* Chain up what we have collected */
3335 
3336   clct = (struct gaclct *)malloc(sizeof(struct gaclct));
3337   if (clct==NULL) {
3338     gaprnt (0,"Memory allocation error in collect\n");
3339     rc = 1;
3340     goto collerr2;
3341   }
3342   clct->forw = NULL;
3343   if (pcm->clct[clnm]==NULL) pcm->clct[clnm] = clct;
3344   else {
3345     clct2 = pcm->clct[clnm];
3346     while (clct2->forw) clct2 = clct2->forw;
3347     clct2->forw = clct;
3348   }
3349   clct->stn = pcm->result[0].stn;
3350   pcm->clctnm[clnm]++;
3351 
3352   /* Free any possible extra stuff */
3353 
3354   if (pcm->numgrd > 1) {
3355     for (i=1; i<pcm->numgrd; i++) {
3356       if (pcm->type[i]==1) gagfre (pcm->result[i].pgr);
3357       else gasfre (pcm->result[i].stn);
3358     }
3359   }
3360   pcm->numgrd = 0;
3361   pcm->relnum = 0;
3362 
3363   free (pst);
3364   return (0);
3365 
3366 collerr1:
3367 
3368   gaprnt (0,"Collect command error:  Invalid Syntax\n");
3369   gaprnt (0,"  Format is:  collect n expr\n");
3370   return (1);
3371 
3372 collerr2:
3373   gagrel(pcm);
3374   free(pst);
3375   return(rc);
3376 
3377 }
3378 
3379 /* Handle display command */
3380 
gadspl(char * cmd,struct gacmn * pcm)3381 int gadspl (char *cmd, struct gacmn *pcm) {
3382 struct gastat *pst;
3383 struct gafile *pfi;
3384 int l,l1,l2,vcnt,i,lflg,ldim,rc;
3385 float *vals,v,xl,yl,s1,s2;
3386 float (*conv) (float *, float);
3387 int llen, rcode, labsv;
3388 char lab[30];
3389 static int dcolor[10] = {-1, 1, 3, 7, 2, 6, 9, 10, 11, 12 };
3390 
3391   if (pcm->impflg) gacmd (pcm->impnam, pcm, 0);
3392   rcode = 1;
3393   if ( (cmd=nxtwrd(cmd)) == NULL) {
3394     gaprnt (0,"Display command error:  No expression provided\n");
3395     return (1);
3396   }
3397   garemb (cmd);
3398 
3399   pst = getpst(pcm);
3400   if (pst==NULL) return(1);
3401   vcnt = 0;
3402   for (i=0; i<4; i++) if (pcm->vdim[i]) vcnt++;
3403   lflg = pcm->loopflg;
3404   if (vcnt>2) lflg = 1;
3405 
3406   if (!lflg) {
3407     pcm->mcolor = 8;
3408     if (pcm->ccolor==-9) {
3409       if (vcnt==2 && pcm->grflg==0) pcm->ccolor = dcolor[pcm->pass];
3410       else pcm->ccolor = dcolor[pcm->pass+1];
3411     }
3412     if (pcm->cmark==-9) {
3413       pcm->cmark = pcm->pass+2;
3414       while (pcm->cmark>5) pcm->cmark-=5;
3415     }
3416     if (pcm->ccolor<0 || pcm->grflg) pcm->mcolor = 15;
3417     rc = gapars(cmd, pst, pcm);
3418     if (rc) goto retrn;
3419     gaplot (pcm);
3420     gagrel (pcm);
3421     pcm->pass++;
3422     pcm->ccolor = -9;
3423     pcm->cstyle = -9;
3424     pcm->cmark = -9;
3425     pcm->cint = 0.0;
3426     pcm->cflag = 0;
3427     pcm->ccflg = 0;
3428     pcm->cmin = -9.99e33;
3429     pcm->cmax = 9.99e33;
3430     pcm->blkflg = 0;
3431     pcm->rainmn = pcm->rainmx = 0.0;
3432     pcm->arrflg = 0;
3433     pcm->ptflg = 0;
3434     pcm->xexflg = 0;  pcm->yexflg = 0;
3435   } else {
3436     pcm->mcolor = 15;
3437     if (pcm->ccolor==-9) {
3438       pcm->ccolor = -1;
3439       if (vcnt<2 || pcm->grflg) pcm->ccolor = 1;
3440     }
3441     if (pcm->cmark==-9) {
3442       pcm->cmark = pcm->pass+2;
3443       while (pcm->cmark>5) pcm->cmark-=5;
3444     }
3445     pfi = pcm->pfid;
3446     ldim = pcm->loopdim;
3447     if (pfi->type > 1 && ldim != 3) {
3448       gaprnt (0,"Display command error:  Invalid looping environment\n");
3449       gaprnt (0,"  Cannot loop on stn data through X, Y, or Z\n");
3450       return (1);
3451     }
3452     if (ldim < 3) {
3453       conv = pfi->ab2gr[ldim];
3454       vals = pfi->abvals[ldim];
3455       v = conv(vals,pcm->dmin[ldim]);
3456       l1 = (int)v;
3457       v = conv(vals,pcm->dmax[ldim]);
3458       l2 = (int)(v+0.999);
3459     } else {
3460       vals = pfi->abvals[3];
3461       v = t2gr(vals,&(pcm->tmin));
3462       l1 = (int)v;
3463       v = t2gr(vals,&(pcm->tmax));
3464       l2 = (int)(v+0.999);
3465     }
3466     vals = pfi->grvals[ldim];
3467     if (ldim<3) conv = pfi->gr2ab[ldim];
3468     gxfrme (2);
3469     pcm->pass = 0;
3470     labsv = pcm->clab;
3471     for (l=l1;l<=l2;l++) {
3472       if (ldim < 3 ) {
3473         pst->dmin[ldim] = conv(vals,(float)l);
3474         pst->dmax[ldim] = pst->dmin[ldim];
3475       } else {
3476         gr2t (vals, (float)l, &(pst->tmin));
3477         pst->tmax = pst->tmin;
3478       }
3479       rc = gapars(cmd, pst, pcm);
3480       if (rc) goto retrn;
3481       pcm->clab = 0;
3482       if (l==l2) pcm->clab = labsv;
3483       gaplot (pcm);
3484       if (ldim<3) sprintf(lab,"%g",pst->dmin[ldim]);
3485       else sprintf(lab,"%li:%li:%li:%li",
3486          pst->tmin.yr,pst->tmin.mo,pst->tmin.dy,pst->tmin.hr);
3487       llen=0; while (lab[llen]) llen++;
3488       xl = pcm->xsiz - (0.11*(float)(llen));
3489       xl -= 0.02;
3490       yl = 0.02;
3491       s1 = 0.13;
3492       s2 = 0.11;
3493       gxwide(1);
3494       gxchpl (lab,llen,xl,yl,s1,s2,0.0);
3495       gxfrme (2);
3496       gagrel (pcm);
3497       pcm->aflag = -1;
3498       pcm->aflag2 = -1;
3499     }
3500     printf ("Press enter to continue::::: ");
3501     gxfrme (0);
3502     pcm->dbflg = 0;
3503     pcm->pass = 0;
3504     pcm->ccolor = -9;
3505     pcm->cstyle = -9;
3506     pcm->cmark = -9;
3507     pcm->cint = 0.0;
3508     pcm->cflag = 0;
3509     pcm->ccflg = 0;
3510     pcm->cmin = -9.99e33;
3511     pcm->cmax = 9.99e33;
3512     pcm->blkflg = 0;
3513     pcm->rainmn = pcm->rainmx = 0.0;
3514     pcm->aflag = 0;
3515     pcm->aflag2 = 0;
3516     pcm->axflg = 0;
3517     pcm->ayflg = 0;
3518     pcm->grdsflg = 1;
3519     pcm->arrflg = 0;
3520     pcm->ptflg = 0;
3521     pcm->xexflg = 0; pcm->yexflg = 0;
3522   }
3523   free (pst);
3524   return (0);
3525 
3526 retrn:
3527   free (pst);
3528   return(1);
3529 
3530 }
3531 
3532 /* Parse compound expression (multiple expressions seperated
3533    by ;'s), get data, hand data off pcm, return */
3534 
gapars(char * cmd,struct gastat * pst,struct gacmn * pcm)3535 int gapars (char *cmd, struct gastat *pst, struct gacmn *pcm) {
3536 char *pos;
3537 char *expr;
3538 int num, i, rc;
3539 
3540   expr = (char *)malloc((strlen(cmd) + 1) * sizeof(char));
3541   strcpy (expr, cmd);
3542 
3543   /* Convert all the ;'s to nulls and count the number of
3544      sub-expressions.                                           */
3545 
3546   num  = 0;
3547   pos = expr;
3548   while (*pos!='\0') {
3549     if (*pos==';') {
3550       *pos = '\0';
3551       num++;
3552     }
3553     pos++;
3554   }
3555   num++;
3556 
3557   /* Evaluate all the subexpressions */
3558   pos = expr;
3559   for (i=0; i<num; i++) {
3560     rc = gaexpr (pos, pst);
3561     if (!rc) rc = gaqsig();
3562     if (rc) goto err;
3563     pcm->type[i] = pst->type;
3564     pcm->result[i] = pst->result;
3565     while (*pos!='\0') pos++;
3566     pos++;
3567   }
3568   pcm->numgrd = num;
3569   pcm->relnum = num;
3570   free(expr);
3571   return (0);
3572 
3573 err:
3574   gaprnt (0,"DISPLAY error:  Invalid expression \n");
3575   gaprnt (0,"  Expression = ");
3576   gaprnt (0,pos);
3577   gaprnt (0,"\n");
3578   pcm->numgrd = i;
3579   pcm->relnum = i;
3580   gagrel (pcm);
3581   free(expr);
3582   return (1);
3583 }
3584 
gagrel(struct gacmn * pcm)3585 void gagrel (struct gacmn *pcm) {
3586 int i;
3587 struct gagrid *pgr;
3588 
3589    for (i=0; i<pcm->relnum; i++) {
3590      if (pcm->type[i]==1) gagfre (pcm->result[i].pgr);
3591      else gasfre (pcm->result[i].stn);
3592    }
3593    pcm->numgrd = 0;
3594    pcm->relnum = 0;
3595 }
3596 
3597 /* Handle set command */
3598 
3599 char *justs[9] = {"bl","bc","br","l","c","r","tl","tc","tr"};
3600 
gaset(char * cmd,char * com,struct gacmn * pcm)3601 int gaset (char *cmd, char *com, struct gacmn *pcm) {
3602 int kwrd,i,i1,i2,num,id,itt,itt1,itt2,itmp[5];
3603 float v1,v2,*vals,tt,tt1,tt2,xlo,xhi,ylo,yhi;
3604 char *ch,*strng,*pat,*cmd1;
3605 float (*conv) (float *, float);
3606 struct dt tdef;
3607 struct gawgds *wgds;
3608 struct gafile *pfi;
3609 int xx,yy,red,green,blue;
3610 int rc;
3611 static char *kwds[111] = {"X","Y","Z","T","LON","LAT","LEV","TIME",
3612                          "CINT","CSTYLE","CCOLOR","LOOPDIM",
3613                          "LOOPING","LOOPINCR","DFILE","VRANGE",
3614                          "CSMOOTH","GRID","CMARK","XAXIS","YAXIS",
3615                          "GXOUT","BLACK","DIGNUM","DIGSIZ","CMIN",
3616                          "CMAX","ARRSCL","ARRSIZ","CLEVS","STID",
3617                          "GRADS","CLAB","MPROJ","CTERP","XSIZE",
3618                          "CCOLS","MPVALS","MPDSET","VPAGE","PAREA",
3619                          "LINE","STRING","STRSIZ","RGB","FGVALS",
3620                          "MAP","BARBASE","BARGAP","CTHICK","MPDRAW",
3621                          "POLI","DBUFF","XYREV","XFLIP","YFLIP",
3622                          "ANNOT","DISPLAY","BACKGROUND","RBCOLS",
3623                          "RBRANGE","MISSCONN","IMPRUN","ZLOG","STRMDEN",
3624                          "FRAME","CLIP","VRANGE2","ARROWHEAD","MDLOPTS",
3625                          "XLOPTS","YLOPTS","CLOPTS","XLAB","YLAB",
3626                          "XLEVS","YLEVS","XLINT","YLINT","MISSWARN",
3627                          "BUTTON","DEFVAL","BAROPTS","LFCOLS","WXCOLS",
3628                          "FONT","FWRITE","XLPOS","YLPOS","CLSKIP","RBAND",
3629                          "ARRLAB","MPT","WXOPT","XLABS","YLABS","FILL",
3630                          "DROPMENU","LATS","TIMELAB","WARN","STNPRINT",
3631                          "STAT","TLSUPP","GRIDLN","HEMPREF","PRNOPTS",
3632                          "DATAWARN","DIALOG","WRITEGDS","COSLAT"};
3633 
3634   pfi = pcm->pfid;
3635   cmd1 = cmd;
3636   if ( (cmd = nxtwrd (cmd))==NULL) {
3637     gaprnt (0,"SET error:  missing operand\n");
3638     return (1);
3639   }
3640   else if (cmpwrd("defval",cmd)) {
3641     i1 = gaqdef (cmd, pcm, 1);
3642     return (i1);
3643   }
3644   else if (cmpwrd("hempref",cmd)) {
3645     kwrd=105;
3646     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3647     if (cmpwrd("auto",cmd))  pcm->hemflg = -1;
3648     else if (cmpwrd("shem",cmd))  pcm->hemflg = 1;
3649     else if (cmpwrd("nhem",cmd))  pcm->hemflg = 0;
3650     else goto err;
3651   }
3652   else if (cmpwrd("gridln",cmd)) {
3653     kwrd=104;
3654     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3655     i = 1;
3656     if (cmpwrd("auto",cmd)) { pcm->gridln = -9; i=0; }
3657     if (cmpwrd("off",cmd)) { pcm->gridln = -1; i=0; }
3658     if (i) {
3659       if ( intprs(cmd,&itt) == NULL ) goto err;
3660       pcm->gridln = itt;
3661     }
3662     return(0);
3663   }
3664   else if (cmpwrd("tlsupp",cmd)) {
3665     kwrd=103;
3666     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3667     if (cmpwrd("yr",cmd)) pcm->tlsupp = 1;
3668     if (cmpwrd("year",cmd)) pcm->tlsupp = 1;
3669     if (cmpwrd("mo",cmd)) pcm->tlsupp = 2;
3670     if (cmpwrd("month",cmd)) pcm->tlsupp = 2;
3671     return(0);
3672   }
3673   else if (cmpwrd("baropts",cmd)) {
3674     kwrd=82;
3675     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3676     if (cmpwrd("outline",cmd)) pcm->barolin = 1;
3677     if (cmpwrd("filled",cmd)) pcm->barolin = 0;
3678     return(0);
3679   }
3680   else if (cmpwrd("barbase",cmd)) {
3681     kwrd = 47;
3682     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3683     if (cmpwrd("bottom",cmd)) {
3684       pcm->barflg = 0;
3685       return (0);
3686     }
3687     if (cmpwrd("top",cmd)) {
3688       pcm->barflg = -1;
3689       return (0);
3690     }
3691     if ( valprs(cmd,&pcm->barbase) == NULL ) goto err;
3692     pcm->barflg = 1;
3693   }
3694   else if (cmpwrd("mdlopts",cmd)) {
3695     kwrd = 69;
3696     while ( (cmd = nxtwrd (cmd)) != NULL) {
3697       i1 = 0;
3698       if (cmpwrd("noblank",cmd)) {pcm->mdlblnk = 0; i1 = 1;}
3699       if (cmpwrd("blank",cmd)) {pcm->mdlblnk = 1; i1 = 1;}
3700       if (cmpwrd("dig3",cmd)) {pcm->mdldig3 = 1; i1 = 1;}
3701       if (cmpwrd("nodig3",cmd)) {pcm->mdldig3 = 0; i1 = 1;}
3702       if (i1==0) goto err;
3703     }
3704   }
3705   else if (cmpwrd("bargap",cmd)) {
3706     kwrd = 48;
3707     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3708     if ( intprs(cmd,&itt) == NULL ) goto err;
3709     if (itt<0 || itt>100) {
3710       gaprnt(0,"SET BARGAP Error: gap must be 0 to 99\n");
3711       return(1);
3712     }
3713     pcm->bargap = itt;
3714   }
3715   else if (cmpwrd("font",cmd)) {
3716     kwrd = 85;
3717     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3718     if ( intprs(cmd,&itt) == NULL ) goto err;
3719     if (itt<0 || itt>9) {
3720       gaprnt(0,"SET FONT Error: font must be 0 to 9\n");
3721       return(1);
3722     }
3723     gxchdf(itt);
3724   }
3725   else if (cmpwrd("clip",cmd)) {
3726     kwrd = 66;
3727     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3728     if ( valprs(cmd,&xlo) == NULL ) goto err;
3729     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3730     if ( valprs(cmd,&xhi) == NULL ) goto err;
3731     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3732     if ( valprs(cmd,&ylo) == NULL ) goto err;
3733     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3734     if ( valprs(cmd,&yhi) == NULL ) goto err;
3735     if (xlo<0.0 || ylo<0.0) goto err;
3736     if (xhi>pcm->pxsize || yhi>pcm->pysize) goto err;
3737     if (yhi<=ylo || xhi<=xlo) goto err;
3738     gxclip(xlo,xhi,ylo,yhi);
3739   }
3740   else if (cmpwrd("vpage",cmd)) {
3741     kwrd = 39;
3742     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3743     if (cmpwrd("off",cmd)) {
3744       pcm->xsiz = pcm->pxsize;
3745       pcm->ysiz = pcm->pysize;
3746       gxvpag (pcm->xsiz, pcm->ysiz, 0.0, pcm->xsiz, 0.0, pcm->ysiz);
3747       gacln(pcm,1);
3748       return (0);
3749     }
3750     if ( valprs(cmd,&xlo) == NULL ) goto err;
3751     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3752     if ( valprs(cmd,&xhi) == NULL ) goto err;
3753     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3754     if ( valprs(cmd,&ylo) == NULL ) goto err;
3755     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3756     if ( valprs(cmd,&yhi) == NULL ) goto err;
3757     if (xlo<0.0 || ylo<0.0 || xhi>pcm->pxsize || yhi>pcm->pysize) {
3758       gaprnt (0,"SET Error: vpage values beyond real page limits\n");
3759       return(1);
3760     }
3761     if (yhi<=ylo || xhi<=xlo) goto err;
3762     if ((yhi-ylo)/(xhi-xlo) > pcm->pysize/pcm->pxsize) {
3763       pcm->ysiz = pcm->pysize;
3764       pcm->xsiz = pcm->pysize * (xhi-xlo)/(yhi-ylo);
3765     } else {
3766       pcm->xsiz = pcm->pxsize;
3767       pcm->ysiz = pcm->pxsize * (yhi-ylo)/(xhi-xlo);
3768     }
3769     gxvpag (pcm->xsiz, pcm->ysiz, xlo, xhi, ylo, yhi);
3770     sprintf (pout,"Virtual page size = %g %g \n",pcm->xsiz,pcm->ysiz);
3771     gaprnt (2,pout);
3772     gacln(pcm,1);
3773   }
3774   else if (cmpwrd("mpt",cmd)) {
3775     kwrd = 92;
3776     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3777     if (cmpwrd("*",cmd))  itt = -999;
3778     else {
3779       if ( intprs(cmd,&itt) == NULL ) goto err;
3780       if (itt < 0) itt = 0;
3781       if (itt>255) itt = 255;
3782     }
3783     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3784     green = -999;
3785     blue = -999;
3786     if (cmpwrd("off",cmd))  red = -9;
3787     else {
3788       if ( intprs(cmd,&red) == NULL ) goto err;
3789       if (red < -1) red = -1;
3790       if ( (cmd = nxtwrd (cmd)) != NULL) {
3791         if ( intprs(cmd,&green) == NULL ) goto err;
3792         if (green < 1) green = 1;
3793         if ( (cmd = nxtwrd (cmd)) != NULL) {
3794           if ( intprs(cmd,&blue) == NULL ) goto err;
3795           if (blue < 1) blue = 1;
3796         }
3797       }
3798     }
3799     if (itt == -999) {
3800       itt1 = 0; itt2 = 256;
3801     } else {
3802       itt1 = itt; itt2 = itt+1;
3803     }
3804     for (i=itt1; i<itt2; i++) {
3805       pcm->mpcols[i] = red;
3806       if (green != -999) pcm->mpstls[i] = green;
3807       if (blue != -999) pcm->mpthks[i] = blue;
3808     }
3809   }
3810   else if (cmpwrd("rgb",cmd)) {
3811     kwrd = 44;
3812     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3813     if ( intprs(cmd,&itt) == NULL ) goto err;
3814     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3815     if ( intprs(cmd,&red) == NULL ) goto err;
3816     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3817     if ( intprs(cmd,&green) == NULL ) goto err;
3818     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3819     if ( intprs(cmd,&blue) == NULL ) goto err;
3820     if (itt<16 || itt>99 || red<0 || red>255 ||
3821         green<0 || green>255 || blue<0 || blue>255) {
3822       gaprnt (0,"SET RGB Error:  Invalid color number or rgb value\n");
3823       gaprnt (0,"  Color number must be 16-99, rgb value 0-255\n");
3824       return(1);
3825     }
3826     if ( !gxacol (itt,red,green,blue) ) {
3827       sprintf (pout,"Color R:%i G:%i B:%i Unavailable: closest color assigned\n",
3828                                           red,green,blue);
3829       gaprnt (3,pout);
3830     }
3831   }
3832   else if (cmpwrd("stat",cmd)) {
3833     kwrd = 102;
3834     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3835     if (cmpwrd("off",cmd)) pcm->statflg = 0;
3836     else if (cmpwrd("on",cmd)) pcm->statflg = 1;
3837     else goto err;
3838   }
3839   else if (cmpwrd("arrlab",cmd)) {
3840     kwrd = 91;
3841     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3842     if (cmpwrd("off",cmd)) pcm->arlflg = 0;
3843     else if (cmpwrd("on",cmd)) pcm->arlflg = 1;
3844     else goto err;
3845   }
3846   else if (cmpwrd("parea",cmd)) {
3847     kwrd = 40;
3848     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3849     if (cmpwrd("off",cmd)) {
3850       pcm->paflg = 0;
3851       return (0);
3852     }
3853     if ( valprs(cmd,&xlo) == NULL ) goto err;
3854     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3855     if ( valprs(cmd,&xhi) == NULL ) goto err;
3856     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3857     if ( valprs(cmd,&ylo) == NULL ) goto err;
3858     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3859     if ( valprs(cmd,&yhi) == NULL ) goto err;
3860     if (xlo<0.0 || ylo<0.0 || xhi>pcm->xsiz || yhi>pcm->ysiz) {
3861       gaprnt (0,"SET Error: parea values beyond page limits\n");
3862       return(1);
3863     }
3864     if (yhi<=ylo || xhi<=xlo) goto err;
3865     pcm->pxmin = xlo;
3866     pcm->pxmax = xhi;
3867     pcm->pymin = ylo;
3868     pcm->pymax = yhi;
3869     pcm->paflg = 1;
3870   }
3871   else if (cmpwrd("arrowhead",cmd)) {
3872     kwrd = 68;
3873     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3874     if ( valprs(cmd,&(pcm->ahdsiz)) == NULL ) goto err;
3875     sprintf (pout,"Arrowhead = %g \n",pcm->ahdsiz);
3876     gaprnt (2,pout);
3877   }
3878   else if (cmpwrd("cint",cmd)) {
3879     kwrd = 8;
3880     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3881     if ( valprs(cmd,&(pcm->cint)) == NULL ) goto err;
3882     sprintf (pout,"cint = %g \n",pcm->cint);
3883     gaprnt (2,pout);
3884   }
3885   else if (cmpwrd("xlint",cmd)) {
3886     kwrd = 77;
3887     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3888     if ( valprs(cmd,&(pcm->xlint)) == NULL ) goto err;
3889     sprintf (pout,"xlint = %g \n",pcm->xlint);
3890     gaprnt (2,pout);
3891   }
3892   else if (cmpwrd("ylint",cmd)) {
3893     kwrd = 78;
3894     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3895     if ( valprs(cmd,&(pcm->ylint)) == NULL ) goto err;
3896     sprintf (pout,"ylint = %g \n",pcm->ylint);
3897     gaprnt (2,pout);
3898   }
3899   else if (cmpwrd("xsize",cmd)) {
3900     kwrd = 35;
3901     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3902     if ( intprs(cmd,&xx) == NULL ) goto err;
3903     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3904     if ( intprs(cmd,&yy) == NULL ) goto err;
3905     gxdxsz(xx,yy);
3906     gxfrme (9);
3907   }
3908   else if (cmpwrd("mpvals",cmd)) {
3909     kwrd = 37;
3910     i1 = 0;
3911     if ( (ch = nxtwrd (cmd)) == NULL) goto err;
3912     if ( cmpwrd("off",ch)) {
3913       pcm->mpflg = 0;
3914       gaprnt (2,"mpvals have been turned off\n");
3915     } else {
3916       while ( (cmd = nxtwrd (cmd)) != NULL) {
3917         if ( valprs(cmd,&(pcm->mpvals[i1])) == NULL ) goto err;
3918         i1++;
3919         if (i1>9) goto err;
3920       }
3921       pcm->mpflg = i1;
3922       gaprnt (2,"mpvals have been set\n");
3923     }
3924   }
3925   else if (cmpwrd("fgvals",cmd)) {
3926     kwrd = 45;
3927     i1 = 0;
3928     while ( (cmd = nxtwrd (cmd)) != NULL) {
3929       if ( intprs(cmd,&(pcm->fgvals[i1])) == NULL ) goto err;
3930       if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
3931       if ( intprs(cmd,&(pcm->fgcols[i1])) == NULL ) goto err;
3932       i1++;
3933       if (i1>48) goto err;
3934     }
3935     pcm->fgcnt = i1;
3936     gaprnt (2,"fgvals set\n");
3937   }
3938   else if (cmpwrd("clevs",cmd)) {
3939     kwrd = 29;
3940     i1 = 0;
3941     while ( (cmd = nxtwrd (cmd)) != NULL) {
3942       if ( valprs(cmd,&(pcm->clevs[i1])) == NULL ) goto err;
3943       i1++;
3944       if (i1>98) goto err;
3945     }
3946     pcm->cflag = i1;
3947     sprintf (pout,"Number of clevs = %i \n",i1);
3948     gaprnt (2,pout);
3949   }
3950   else if (cmpwrd("xlevs",cmd)) {
3951     kwrd = 75;
3952     i1 = 0;
3953     while ( (cmd = nxtwrd (cmd)) != NULL) {
3954       if ( valprs(cmd,&(pcm->xlevs[i1])) == NULL ) goto err;
3955       i1++;
3956       if (i1>49) goto err;
3957     }
3958     pcm->xlflg = i1;
3959     sprintf (pout,"Number of xlevs = %i \n",i1);
3960     gaprnt (2,pout);
3961   }
3962   else if (cmpwrd("ylevs",cmd)) {
3963     kwrd = 76;
3964     i1 = 0;
3965     while ( (cmd = nxtwrd (cmd)) != NULL) {
3966       if ( valprs(cmd,&(pcm->ylevs[i1])) == NULL ) goto err;
3967       i1++;
3968       if (i1>49) goto err;
3969     }
3970     pcm->ylflg = i1;
3971     sprintf (pout,"Number of ylevs = %i \n",i1);
3972     gaprnt (2,pout);
3973   }
3974   else if (cmpwrd("rbcols",cmd)) {
3975     kwrd = 59;
3976     i1 = 0;
3977     while ( (cmd = nxtwrd (cmd)) != NULL) {
3978       if (i1==0 && cmpwrd("auto",cmd)) break;
3979       if ( intprs(cmd,&(pcm->rbcols[i1])) == NULL ) goto err;
3980       i1++;
3981       if (i1>99) goto err;
3982     }
3983     pcm->rbflg = i1;
3984     if (i1==0) gaprnt(2,"Rainbow colors set to auto\n");
3985     else {
3986       sprintf (pout,"Number of rainbow colors = %i\n",i1);
3987       gaprnt (2,pout);
3988     }
3989   }
3990   else if (cmpwrd("dropmenu",cmd)) {
3991     kwrd = 97;
3992     i1 = 0;
3993     while ( (cmd = nxtwrd (cmd)) != NULL) {
3994       if (i1>14) goto drerr;
3995       if ( intprs(cmd,&(pcm->drvals[i1])) == NULL ) goto drerr;
3996       i1++;
3997     }
3998     if (i1 == 0) goto drerr;
3999   }
4000   else if (cmpwrd("ccols",cmd)) {
4001     kwrd = 36;
4002     i1 = 0;
4003     while ( (cmd = nxtwrd (cmd)) != NULL) {
4004       if ( intprs(cmd,&(pcm->ccols[i1])) == NULL ) goto err;
4005       i1++;
4006       if (i1>98) goto err;
4007     }
4008     pcm->ccflg = i1;
4009     sprintf (pout,"Number of ccols = %i\n",i1);
4010     gaprnt (2,pout);
4011     if (pcm->cflag==0) {
4012       gaprnt (2,"ccols won't take effect unless clevs are set.\n");
4013     }
4014   }
4015   else if (cmpwrd("cmin",cmd)) {
4016     kwrd = 25;
4017     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4018     if ( valprs(cmd,&(pcm->cmin)) == NULL ) goto err;
4019     sprintf (pout,"cmin = %g \n",pcm->cmin);
4020     gaprnt (2,pout);
4021   }
4022   else if (cmpwrd("cmax",cmd)) {
4023     kwrd = 26;
4024     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4025     if ( valprs(cmd,&(pcm->cmax)) == NULL ) goto err;
4026     sprintf (pout,"cmax = %g \n",pcm->cmax);
4027     gaprnt (2,pout);
4028   }
4029   else if (cmpwrd("cmark",cmd)) {
4030     kwrd = 18;
4031     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4032     if ( intprs(cmd,&(pcm->cmark)) == NULL ) goto err;
4033     sprintf (pout,"cmark = %i \n",pcm->cmark);
4034     gaprnt (2,pout);
4035   }
4036   else if (cmpwrd("mproj",cmd)) {
4037     kwrd = 33;
4038     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4039     if ( cmpwrd("off",cmd)) pcm->mproj = 0;
4040     else if ( cmpwrd("scaled",cmd)) pcm->mproj = 1;
4041     else if ( cmpwrd("latlon",cmd)) pcm->mproj = 2;
4042     else if ( cmpwrd("nps",cmd)) pcm->mproj = 3;
4043     else if ( cmpwrd("sps",cmd)) pcm->mproj = 4;
4044     else if ( cmpwrd("robinson",cmd)) pcm->mproj = 5;
4045 /*------- DKRZ appends Projections
4046           10.08.95   Karin Meier (karin.meier@dkrz.de) -------*/
4047 
4048     else if ( cmpwrd("mollweide",cmd)) pcm->mproj = 6;
4049     else if ( cmpwrd("orthogr",cmd)) pcm->mproj = 7;
4050     else if ( cmpwrd("orthographic",cmd)) pcm->mproj = 7;
4051     else if ( cmpwrd("ortho",cmd)) pcm->mproj = 7;
4052     else if ( cmpwrd("lambert",cmd)) pcm->mproj = 13;
4053 
4054 
4055 /*------- DKRZ appendingd end ------*/
4056 
4057     else goto err;
4058   }
4059   else if (cmpwrd("xyrev",cmd)) {
4060     kwrd = 53;
4061     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4062     if ( cmpwrd("off",cmd)) pcm->rotate = 0;
4063     else if ( cmpwrd("on",cmd)) pcm->rotate = 1;
4064     else goto err;
4065   }
4066   else if (cmpwrd("xflip",cmd)) {
4067     kwrd = 54;
4068     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4069     if ( cmpwrd("off",cmd)) pcm->xflip = 0;
4070     else if ( cmpwrd("on",cmd)) pcm->xflip = 1;
4071     else goto err;
4072   }
4073   else if (cmpwrd("yflip",cmd)) {
4074     kwrd = 55;
4075     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4076     if ( cmpwrd("off",cmd)) pcm->yflip = 0;
4077     else if ( cmpwrd("on",cmd)) pcm->yflip = 1;
4078     else goto err;
4079   }
4080   else if (cmpwrd("writegds",cmd)) {
4081     wgds = pcm->wgds;
4082     kwrd = 109;
4083     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4084     if (*cmd=='-') {
4085       itt = 0;
4086       while (*(cmd+itt)!=' '&&*(cmd+itt)!='\n'&&*(cmd+itt)!='\0') itt++;
4087       ch = (char *)malloc(itt+2);
4088       if (ch==NULL) {
4089         gaprnt (0,"Memory allocation Error\n");
4090         goto err;
4091       }
4092       i2 = cmd - cmd1;
4093       for (i1=0; i1<itt; i1++) *(ch+i1) = *(com+i1+i2);
4094       *(ch+i1) = '\0';
4095       if (wgds->opts) free(wgds->opts);
4096       wgds->opts = ch;
4097     }
4098     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4099     itt = 0;
4100     while (*(cmd+itt)!=' '&&*(cmd+itt)!='\n'&&*(cmd+itt)!='\0') itt++;
4101     ch = (char *)malloc(itt+2);
4102     if (ch==NULL) {
4103       gaprnt (0,"Memory allocation Error\n");
4104       goto err;
4105     }
4106     i2 = cmd - cmd1;
4107     for (i1=0; i1<itt; i1++) *(ch+i1) = *(com+i1+i2);
4108     *(ch+i1) = '\0';
4109     if (wgds->fname) free(wgds->fname);
4110     wgds->fname = ch;
4111     if (wgds->opts) {
4112       sprintf (pout,"WRITEGDS file name = %s  Opts = %s\n",ch,wgds->opts);
4113     } else {
4114       sprintf (pout,"WRITEGDS file name = %s\n",ch);
4115     }
4116     gaprnt (2,pout);
4117   }
4118   /* Following is for the so-called 'exact fwrite' to
4119      workaround the bug with scaling for hires files */
4120   else if (cmpwrd("fwex",cmd)) {
4121     pcm->fwexflg = 1;
4122   }
4123   else if (cmpwrd("fwrite",cmd)) {
4124     kwrd = 86;
4125     if (pcm->ffile) {
4126       gaprnt (0,"SET FWrite Error:  fwrite file is open\n");
4127       gaprnt (0,"Use DISABLE FWRITE command to close file\n");
4128     } else {
4129       if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4130       while ( cmpwrd("-be",cmd) || cmpwrd("-le",cmd) ||
4131 	      cmpwrd("-ap",cmd) || cmpwrd("-cl",cmd) ||
4132               cmpwrd("-sq",cmd) || cmpwrd("-st",cmd) ||
4133               cmpwrd("-ex",cmd) ) {
4134         if ( cmpwrd("-be",cmd) ) pcm->fwenflg = 1;
4135         if ( cmpwrd("-le",cmd) ) pcm->fwenflg = 0;
4136         if ( cmpwrd("-sq",cmd) ) pcm->fwsqflg = 1;
4137         if ( cmpwrd("-st",cmd) ) pcm->fwsqflg = 0;
4138         if ( cmpwrd("-ap",cmd) ) pcm->fwappend = 1;
4139         if ( cmpwrd("-cl",cmd) ) pcm->fwappend = 0;
4140         if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4141       }
4142       itt = 0;
4143       while (*(cmd+itt)!=' '&&*(cmd+itt)!='\n'&&*(cmd+itt)!='\0') itt++;
4144       ch = (char *)malloc(itt+2);
4145       if (ch==NULL) {
4146         gaprnt (0,"Memory allocation Error\n");
4147         goto err;
4148       }
4149       i2 = cmd - cmd1;
4150       for (i1=0; i1<itt; i1++) *(ch+i1) = *(com+i1+i2);
4151       *(ch+i1) = '\0';
4152       if (pcm->fwname) free(pcm->fwname);
4153       pcm->fwname = ch;
4154       sprintf (pout,"FWrite file name = %s\n",ch);
4155       gaprnt (2,pout);
4156       if (pcm->fwenflg == 0) {
4157         gaprnt (2,"FWwrite byte order is little_endian; format is ");
4158       } else {
4159         gaprnt (2,"FWwrite byte order is big_endian; format is ");
4160       }
4161       if (pcm->fwsqflg == 1) gaprnt (2,"sequential\n");
4162       else gaprnt (2,"stream\n");
4163       if (pcm->fwappend) {
4164 	gaprnt (2,"Fwrite appending to an existing file\n");
4165       } else {
4166 	gaprnt (2,"Fwrite replacing an existing file if it exist\n");
4167       }
4168     }
4169   }
4170   else if (cmpwrd("imprun",cmd)) {
4171     kwrd = 62;
4172     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4173     if (cmpwrd("off",cmd)) {
4174       if (pcm->impflg) free(pcm->impnam);
4175       pcm->impflg = 0;
4176       gaprnt (2,"IMPrun is off\n");
4177     } else {
4178       itt = 0;
4179       while (*(cmd+itt)!=' '&&*(cmd+itt)!='\n'&&*(cmd+itt)!='\0') itt++;
4180       ch = (char *)malloc(itt+6);
4181       if (ch==NULL) {
4182         gaprnt (0,"Memory allocation Error\n");
4183         goto err;
4184       }
4185       for (i1=0; i1<itt; i1++) *(ch+i1+4) = *(cmd+i1);
4186       *(ch+i1+4) = '\0';
4187       *ch='r'; *(ch+1)='u'; *(ch+2)='n'; *(ch+3)=' ';
4188       if (pcm->impflg) free(pcm->impnam);
4189       pcm->impflg = 1;
4190       pcm->impnam = ch;
4191       sprintf (pout,"Imprun file name = %s\n",ch);
4192       gaprnt (2,pout);
4193     }
4194   }
4195   else if (cmpwrd("zlog",cmd)) {
4196     kwrd = 63;
4197     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4198     if ( cmpwrd("on",cmd)) pcm->zlog = 1;
4199     else if ( cmpwrd("off",cmd)) pcm->zlog = 0;
4200     else goto err;
4201   }
4202   else if (cmpwrd("coslat",cmd)) {
4203     kwrd = 110;
4204     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4205     if ( cmpwrd("on",cmd)) pcm->coslat = 1;
4206     else if ( cmpwrd("off",cmd)) pcm->coslat = 0;
4207     else goto err;
4208   }
4209   else if (cmpwrd("missconn",cmd)) {
4210     kwrd = 61;
4211     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4212     if ( cmpwrd("on",cmd)) pcm->miconn = 1;
4213     else if ( cmpwrd("off",cmd)) pcm->miconn = 0;
4214     else goto err;
4215   }
4216   else if (cmpwrd("mpdraw",cmd)) {
4217     kwrd = 50;
4218     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4219     if ( cmpwrd("on",cmd)) pcm->mpdraw = 1;
4220     else if ( cmpwrd("off",cmd)) pcm->mpdraw = 0;
4221     else goto err;
4222   }
4223   else if (cmpwrd("dbuff",cmd)) {
4224     kwrd = 52;
4225     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4226     if ( cmpwrd("on",cmd)) {
4227       pcm->dbflg = 1;
4228       gxfrme(2);
4229       gacln(pcm,1);
4230     }
4231     else if ( cmpwrd("off",cmd)) {
4232       if (pcm->dbflg) {
4233         pcm->dbflg = 0;
4234         gxfrme(1);
4235         gacln (pcm,1);
4236       }
4237     }
4238     else goto err;
4239   }
4240   else if (cmpwrd("poli",cmd)) {
4241     kwrd = 51;
4242     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4243     if ( cmpwrd("on",cmd)) {
4244       if (pcm->mpcols[1] == -9) pcm->mpcols[1] = -1;
4245       if (pcm->mpcols[2] == -9) pcm->mpcols[2] = -1;
4246     }
4247     else if ( cmpwrd("off",cmd)) {
4248       pcm->mpcols[1] = -9;
4249       pcm->mpcols[2] = -9;
4250     }
4251     else goto err;
4252   }
4253   else if (cmpwrd("mpdset",cmd)) {
4254     kwrd = 38;
4255     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4256     for (xx=0; xx<8; xx++) {
4257       if (pcm->mpdset[xx]) free(pcm->mpdset[xx]);
4258       pcm->mpdset[xx] = NULL;
4259     }
4260     xx = 0;
4261     itt = 0;
4262     while (xx<8) {
4263       itt2 = itt;
4264       while (*(cmd+itt)!=' '&&*(cmd+itt)!='\n'&&*(cmd+itt)!='\0') itt++;
4265       ch = (char *)malloc(itt+2-itt2);
4266       if (ch==NULL) {
4267         gaprnt (0,"Memory allocation Error\n");
4268         goto err;
4269       }
4270       for (i1=itt2; i1<itt; i1++) *(ch+i1-itt2) = *(cmd+i1);
4271       *(ch+i1-itt2) = '\0';
4272       pcm->mpdset[xx] = ch;
4273       sprintf (pout,"MPDSET file name = %s\n",ch);
4274       gaprnt (2,pout);
4275       while (*(cmd+itt)==' ') itt++;
4276       if (*(cmd+itt)=='\n'||*(cmd+itt)=='\0') break;
4277       xx++;
4278     }
4279   }
4280   else if (cmpwrd("rbrange",cmd)) {
4281     kwrd = 60;
4282     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4283     if ( valprs(cmd,&v1) == NULL ) goto err;
4284     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4285     if ( valprs(cmd,&v2) == NULL ) goto err;
4286     if (v1>=v2) goto err;
4287     pcm->rainmn = v1;
4288     pcm->rainmx = v2;
4289   }
4290   else if (cmpwrd("black",cmd)) {
4291     kwrd = 22;
4292     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4293     if ( cmpwrd("off",cmd)) pcm->blkflg = 0;
4294     else {
4295       if ( valprs(cmd,&(pcm->blkmin)) == NULL ) goto err;
4296       if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4297       if ( valprs(cmd,&(pcm->blkmax)) == NULL ) goto err;
4298       pcm->blkflg = 1;
4299     }
4300   }
4301   else if (cmpwrd("display",cmd)) {
4302     kwrd = 57;
4303     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4304     if ( cmpwrd("color",cmd)) pcm->grflg=0;
4305     else if ( cmpwrd("grey",cmd)) pcm->grflg=1;
4306     else if ( cmpwrd("greyscale",cmd)) pcm->grflg=1;
4307     else goto err;
4308     if ( (cmd = nxtwrd (cmd)) != NULL) {
4309       if ( cmpwrd("white",cmd)) pcm->devbck = 1;
4310       else if ( cmpwrd("black",cmd)) pcm->devbck = 0;
4311       else goto err;
4312     }
4313     gxdbck(pcm->devbck);
4314     gxgrey(pcm->grflg);
4315   }
4316   else if (cmpwrd("gxout",cmd)) {
4317     kwrd = 21;
4318     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4319     pcm->gout0 = 9;
4320     if ( cmpwrd("contour",cmd)) pcm->gout2a = 1;
4321     else if ( cmpwrd("shaded",cmd)) pcm->gout2a = 2;
4322     else if ( cmpwrd("grid",cmd)) {pcm->gout2a = 3; pcm->gout2b=3;}
4323     else if ( cmpwrd("vector",cmd)) {pcm->gout2b = 4;
4324                  pcm->goutstn = 6; pcm->gout1a = 1;}
4325     else if ( cmpwrd("scatter",cmd)) pcm->gout2b = 5;
4326     else if ( cmpwrd("fgrid",cmd)) pcm->gout2a = 6;
4327     else if ( cmpwrd("fwrite",cmd)) pcm->gout2a = 7;
4328 #if USELATS == 1
4329     else if ( cmpwrd("latsgrid",cmd)) pcm->gout2a = 20;
4330     else if ( cmpwrd("latsdata",cmd)) pcm->gout2a = 21;
4331 #endif
4332     else if ( cmpwrd("stream",cmd)) pcm->gout2b = 8;
4333     else if ( cmpwrd("grfill",cmd)) pcm->gout2a = 10;
4334     else if ( cmpwrd("pgrid",cmd)) pcm->gout2a = 11;
4335     else if ( cmpwrd("value",cmd)) pcm->goutstn = 1;
4336     else if ( cmpwrd("barb",cmd)) {pcm->goutstn = 2;
4337                        pcm->gout2b = 9; pcm->gout1a = 2;}
4338     else if ( cmpwrd("findstn",cmd)) pcm->goutstn = 3;
4339     else if ( cmpwrd("model",cmd)) pcm->goutstn = 4;
4340     else if ( cmpwrd("wxsym",cmd)) pcm->goutstn = 5;
4341     else if ( cmpwrd("stnmark",cmd)) pcm->goutstn = 7;
4342     else if ( cmpwrd("line",cmd)) {pcm->gout1 = 1; pcm->tser = 0;}
4343     else if ( cmpwrd("bar",cmd)) pcm->gout1 = 2;
4344     else if ( cmpwrd("errbar",cmd)) pcm->gout1 = 3;
4345     else if ( cmpwrd("linefill",cmd)) pcm->gout1 = 4;
4346     else if ( cmpwrd("stat",cmd)) pcm->gout0 = 1;
4347     else if ( cmpwrd("print",cmd)) pcm->gout0 = 2;
4348     else if ( cmpwrd("writegds",cmd)) pcm->gout0 = 3;
4349     else if ( cmpwrd("tserwx",cmd)) pcm->tser = 1;
4350     else if ( cmpwrd("tserbarb",cmd)) pcm->tser = 2;
4351     else goto err;
4352     if (pcm->gout0==9) pcm->gout0 = 0;
4353   }
4354   else if (cmpwrd("arrscl",cmd)) {
4355     kwrd = 27;
4356     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4357     if ( valprs(cmd,&(pcm->arrsiz)) == NULL ) goto err;
4358     if ( (cmd = nxtwrd (cmd)) == NULL) {
4359       pcm->arrmag = -999.0;
4360     } else {
4361       if ( valprs(cmd,&(pcm->arrmag)) == NULL ) goto err;
4362     }
4363     pcm->arrflg = 1;
4364   }
4365   else if (cmpwrd("xlabs",cmd)||cmpwrd("ylabs",cmd)) {
4366     if (cmpwrd("xlabs",cmd)) {kwrd=94; strng=pcm->xlabs;}
4367     if (cmpwrd("ylabs",cmd)) {kwrd=95; strng=pcm->ylabs;}
4368     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4369     i1 = 0;
4370     if (cmpwrd("off",cmd)) {
4371       if (strng) free(strng);
4372       strng = NULL;
4373     } else {
4374       com = nxtwrd(com);
4375       com = nxtwrd(com);
4376       num = 0;
4377       while(*(com+num)!='\0' && *(com+num)!='\n') num++;
4378       if (strng) free(strng);
4379       strng = (char *)malloc(num+2);
4380       if (strng==NULL) {
4381         gaprnt(0,"Memory Allocation Error: Set XLABS/YLABS\n");
4382         goto err;
4383       }
4384       num = 0;
4385       while(*(com+num)!='\0' && *(com+num)!='\n') {
4386         *(strng+num) = *(com+num);
4387         if (*(strng+num)=='|') {
4388           *(strng+num) = '\0';
4389           i1++;
4390         }
4391         num++;
4392       }
4393       *(strng+num) = '\0';
4394       i1++;
4395     }
4396     if (kwrd==94) { pcm->xlabs=strng; pcm->ixlabs = i1; }
4397     if (kwrd==95) { pcm->ylabs=strng; pcm->iylabs = i1; }
4398   }
4399   else if (cmpwrd("clab",cmd)||cmpwrd("xlab",cmd)||cmpwrd("ylab",cmd)) {
4400     if (cmpwrd("clab",cmd)) {kwrd=32; strng=pcm->clstr; i1=pcm->clab;}
4401     if (cmpwrd("xlab",cmd)) {kwrd=73; strng=pcm->xlstr; i1=pcm->xlab;}
4402     if (cmpwrd("ylab",cmd)) {kwrd=74; strng=pcm->ylstr; i1=pcm->ylab;}
4403     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4404     if (cmpwrd("on",cmd)) i1 = 1;
4405     else if (cmpwrd("off",cmd)) i1 = 0;
4406     else if (cmpwrd("forced",cmd)) i1 = 2;
4407     else if (cmpwrd("auto",cmd)) {
4408       if (strng) free(strng);
4409       strng = NULL;
4410     }
4411     else {
4412       com = nxtwrd(com);
4413       com = nxtwrd(com);
4414       num = 0;
4415       while(*(com+num)!='\0' && *(com+num)!='\n') num++;
4416       if (strng) free(strng);
4417       strng = (char *)malloc(num+2);
4418       if (strng==NULL) {
4419         gaprnt(0,"Memory Allocation Error: Set ?LAB\n");
4420         goto err;
4421       }
4422       num = 0;
4423       while(*(com+num)!='\0' && *(com+num)!='\n') {
4424         *(strng+num) = *(com+num);
4425         num++;
4426       }
4427       *(strng+num) = '\0';
4428       gaprnt (2,"Substitution string is: ");
4429       gaprnt (2,strng);
4430       gaprnt (2,"\n");
4431     }
4432     if (kwrd==32) {pcm->clstr=strng; pcm->clab=i1;}
4433     if (kwrd==73) {pcm->xlstr=strng; pcm->xlab=i1;}
4434     if (kwrd==74) {pcm->ylstr=strng; pcm->ylab=i1;}
4435   }
4436   else if (cmpwrd("prnopts",cmd)) {
4437     kwrd = 106;
4438     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4439     com = nxtwrd(com);
4440     com = nxtwrd(com);
4441     num = 0;
4442     while(*(com+num)!='\0' && *(com+num)!='\n' && *(com+num)!=' ') num++;
4443     if (pcm->prstr) free(pcm->prstr);
4444     strng = (char *)malloc(num+2);
4445     if (strng==NULL) {
4446       gaprnt(0,"Memory Allocation Error: Set PRNOPTS\n");
4447       goto err;
4448     }
4449     num = 0;
4450     while(*(com+num)!='\0' && *(com+num)!='\n' && *(com+num)!=' ') {
4451       *(strng+num) = *(com+num);
4452       num++;
4453     }
4454     *(strng+num) = '\0';
4455     pcm->prstr = strng;
4456     if ( (cmd = nxtwrd (cmd)) != NULL) {
4457       if ( intprs(cmd,&itt) == NULL ) goto err;
4458       pcm->prlnum = itt;
4459       if ( (cmd = nxtwrd (cmd)) != NULL) {
4460         if ( intprs(cmd,&itt) == NULL ) goto err;
4461         pcm->prbnum = itt;
4462         if ( (cmd = nxtwrd (cmd)) != NULL) {
4463           pcm->prudef = 0;
4464           if (*cmd=='u') pcm->prudef = 1;
4465         }
4466       }
4467     }
4468   }
4469   else if (cmpwrd("frame",cmd)) {
4470     kwrd = 65;
4471     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4472     if (cmpwrd("on",cmd)) pcm->frame = 1;
4473     else if (cmpwrd("off",cmd)) pcm->frame = 0;
4474     else if (cmpwrd("circle",cmd)) pcm->frame = 2;
4475     else goto err;
4476   }
4477   else if (cmpwrd("grid",cmd)) {
4478     kwrd = 17;
4479     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4480     if (cmpwrd("on",cmd)) pcm->grflag = 1;
4481     else if (cmpwrd("off",cmd)) pcm->grflag = 0;
4482     else if (cmpwrd("horizontal",cmd)) pcm->grflag = 2;
4483     else if (cmpwrd("vertical",cmd)) pcm->grflag = 3;
4484     else goto err;
4485     if ( (cmd = nxtwrd (cmd)) != NULL) {
4486       if ( intprs(cmd,&itt) == NULL ) goto err;
4487       else pcm->grstyl = itt;
4488       if ( (cmd = nxtwrd (cmd)) != NULL) {
4489         if ( intprs(cmd,&itt) == NULL ) goto err;
4490         else pcm->grcolr = itt;
4491       }
4492     }
4493     if (pcm->grflag) {
4494       sprintf (pout,"grid is on, style %i color %i \n",
4495          pcm->grstyl, pcm->grcolr);
4496       gaprnt (2,pout);
4497     } else {
4498       gaprnt (2,"grid is off\n");
4499     }
4500   }
4501   else if (cmpwrd("clskip",cmd)) {
4502     kwrd = 89;
4503     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4504     if ( intprs(cmd,&itt) == NULL ) goto err;
4505     if (itt<1) goto err;
4506     if ( (cmd = nxtwrd (cmd)) != NULL) {
4507       if ( valprs(cmd,&tt) == NULL ) goto err;
4508       gxclmn(tt);
4509     }
4510     pcm->clskip = itt;
4511   }
4512   else if (cmpwrd("clopts",cmd)) {
4513     kwrd = 72;
4514     itt = pcm->clcol;
4515     itt1 = pcm->clthck;
4516     tt = pcm->clsiz;
4517     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4518     if ( intprs(cmd,&itt) == NULL ) goto err;
4519     if ( (cmd = nxtwrd (cmd)) != NULL) {
4520       if ( intprs(cmd,&itt1) == NULL ) goto xlerr;
4521       else {
4522         if ( (cmd = nxtwrd (cmd)) != NULL) {
4523           if ( valprs(cmd,&tt) == NULL ) goto xlerr;
4524         }
4525       }
4526     }
4527     pcm->clcol = itt;
4528     pcm->clthck = itt1;
4529     pcm->clsiz = tt;
4530     sprintf (pout,"SET CLOPTS values:  Color = %i Thickness = %i",
4531       pcm->clcol, pcm->clthck);
4532     gaprnt (2,pout);
4533     sprintf (pout," Size = %g\n",pcm->clsiz);
4534     gaprnt (2,pout);
4535   }
4536   else if (cmpwrd("wxopt",cmd)) {
4537     kwrd = 93;
4538     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4539     else if ( cmpwrd("wxsym",cmd)) pcm->wxopt = 1;
4540     else if ( cmpwrd("mark",cmd)) pcm->wxopt = 2;
4541     else if ( cmpwrd("char",cmd)) pcm->wxopt = 3;
4542     else goto err;
4543   }
4544   else if (cmpwrd("wxcols",cmd)) {
4545     kwrd = 84;
4546     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4547     if ( intprs(cmd,itmp) == NULL ) goto err;
4548     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4549     if ( intprs(cmd,itmp+1) == NULL ) goto err;
4550     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4551     if ( intprs(cmd,itmp+2) == NULL ) goto err;
4552     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4553     if ( intprs(cmd,itmp+3) == NULL ) goto err;
4554     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4555     if ( intprs(cmd,itmp+4) == NULL ) goto err;
4556     for (i1=0; i1<5; i1++) pcm->wxcols[i1] = itmp[i1];
4557     gaprnt (2,"New WXCOLS have been set\n");
4558   }
4559   else if (cmpwrd("lfcols",cmd)) {
4560     kwrd = 83;
4561     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4562     if ( intprs(cmd,&itt) == NULL ) goto err;
4563     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4564     if ( intprs(cmd,&itt1) == NULL ) goto err;
4565     pcm->lfc1 = itt;
4566     pcm->lfc2 = itt1;
4567     sprintf (pout,"LineFill Colors: Above = %i  Below = %i\n",
4568       pcm->lfc1, pcm->lfc2);
4569     gaprnt (2,pout);
4570   }
4571   else if (cmpwrd("rband",cmd)) {
4572     kwrd = 90;
4573     itt = -1;
4574     if ( (cmd = nxtwrd (cmd)) == NULL) goto rbberr;
4575     if ( intprs(cmd,&i1) == NULL ) goto rbberr;
4576     if ( (cmd = nxtwrd (cmd)) == NULL) goto rbberr;
4577     if ( *cmd=='m' && *(cmd+1)=='b') {
4578       cmd += 2;
4579       if ( intprs(cmd,&(itt)) == NULL ) goto rbberr;
4580       if ( (cmd = nxtwrd (cmd)) == NULL) goto rbberr;
4581       if (itt>3) itt = 3;
4582     }
4583     if (cmpwrd("box",cmd)) i2 = 1;
4584     else if (cmpwrd("line",cmd)) i2 = 2;
4585     else goto rbberr;
4586     if ( (cmd = nxtwrd (cmd)) == NULL) goto rbberr;
4587     if ( valprs(cmd,&xlo) == NULL ) goto rbberr;
4588     if ( (cmd = nxtwrd (cmd)) == NULL) goto rbberr;
4589     if ( valprs(cmd,&ylo) == NULL ) goto rbberr;
4590     if ( (cmd = nxtwrd (cmd)) == NULL) goto rbberr;
4591     if ( valprs(cmd,&xhi) == NULL ) goto rbberr;
4592     if ( (cmd = nxtwrd (cmd)) == NULL) goto rbberr;
4593     if ( valprs(cmd,&yhi) == NULL ) goto rbberr;
4594     gxdrbb (i1,i2,xlo,ylo,xhi,yhi,itt);
4595   }
4596   else if (cmpwrd("button",cmd)) {
4597     kwrd = 80;
4598     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4599     if ( intprs(cmd,&itt) == NULL ) goto err;
4600     pcm->btnfc = itt;
4601     pcm->btnftc = itt;
4602     if ( (cmd = nxtwrd (cmd)) != NULL) {
4603      if ( intprs(cmd,&itt) == NULL ) goto err;
4604      pcm->btnbc = itt;
4605      pcm->btnbtc = itt;
4606      if ( (cmd = nxtwrd (cmd)) != NULL) {
4607       if ( intprs(cmd,&itt) == NULL ) goto err;
4608       pcm->btnoc = itt;
4609       pcm->btnoc2 = itt;
4610       pcm->btnotc = itt;
4611       pcm->btnotc2 = itt;
4612       if ( (cmd = nxtwrd (cmd)) != NULL) {
4613        if ( intprs(cmd,&itt) == NULL ) goto err;
4614        pcm->btnoc2 = itt;
4615        pcm->btnotc2 = itt;
4616        if ( (cmd = nxtwrd (cmd)) != NULL) {
4617         if ( intprs(cmd,&itt) == NULL ) goto err;
4618         pcm->btnftc = itt;
4619         if ( (cmd = nxtwrd (cmd)) != NULL) {
4620          if ( intprs(cmd,&itt) == NULL ) goto err;
4621          pcm->btnbtc = itt;
4622          if ( (cmd = nxtwrd (cmd)) != NULL) {
4623           if ( intprs(cmd,&itt) == NULL ) goto err;
4624           pcm->btnotc = itt;
4625           pcm->btnotc2 = itt;
4626           if ( (cmd = nxtwrd (cmd)) != NULL) {
4627            if ( intprs(cmd,&itt) == NULL ) goto err;
4628            pcm->btnotc2 = itt;
4629            if ( (cmd = nxtwrd (cmd)) != NULL) {
4630             if ( intprs(cmd,&itt) == NULL ) goto err;
4631             pcm->btnthk = itt;
4632            }
4633           }
4634          }
4635         }
4636        }
4637       }
4638      }
4639     }
4640     sprintf (pout,"SET BUTTON values:  Fc, Bc, Oc, Oc2 = %i %i %i %i ",
4641       pcm->btnfc,pcm->btnbc,pcm->btnoc,pcm->btnoc2);
4642     gaprnt (2,pout);
4643     sprintf (pout,"Toggle Fc, Bc, Oc, Oc2 = %i %i %i %i ",
4644       pcm->btnftc,pcm->btnbtc,pcm->btnotc,pcm->btnotc2);
4645     sprintf (pout,"Thick = %i\n",pcm->btnthk);
4646     gaprnt (2,pout);
4647   }
4648   else if (cmpwrd("dialog",cmd)) {
4649     kwrd = 108;
4650     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4651     if ( intprs(cmd,&itt) == NULL ) goto err;
4652     pcm->dlgpc = itt;
4653     if ( (cmd = nxtwrd (cmd)) != NULL) {
4654      if ( intprs(cmd,&itt) == NULL ) goto err;
4655      pcm->dlgfc = itt;
4656      if ( (cmd = nxtwrd (cmd)) != NULL) {
4657       if ( intprs(cmd,&itt) == NULL ) goto err;
4658       pcm->dlgbc = itt;
4659       if ( (cmd = nxtwrd (cmd)) != NULL) {
4660        if ( intprs(cmd,&itt) == NULL ) goto err;
4661        pcm->dlgoc = itt;
4662        if ( (cmd = nxtwrd (cmd)) != NULL) {
4663         if ( intprs(cmd,&itt) == NULL ) goto err;
4664         pcm->dlgth = itt;
4665         if ( (cmd = nxtwrd (cmd)) != NULL) {
4666          if ( cmpwrd("n",cmd)||cmpwrd("numeric",cmd)) pcm->dlgnu = 1;
4667         } else pcm->dlgnu = 0;
4668        }
4669       }
4670      }
4671     }
4672     sprintf (pout,"SET DIALOG values:  Pc, Fc, Bc, Oc = %i %i %i %i ",
4673       pcm->dlgpc,pcm->dlgfc,pcm->dlgbc,pcm->dlgoc);
4674     gaprnt (2,pout);
4675     if (pcm->dlgnu) {
4676       sprintf (pout,"Thick = %i ",pcm->dlgth);
4677       gaprnt (2,pout);
4678       sprintf (pout,"Args = numeric\n",pcm->dlgnu);
4679       gaprnt (2,pout);
4680     } else {
4681       sprintf (pout,"Thick = %i\n",pcm->dlgth);
4682       gaprnt (2,pout);
4683     }
4684   }
4685   else if (cmpwrd("xlpos",cmd)) {
4686     kwrd = 87;
4687     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4688     if ( valprs(cmd,&tt) == NULL ) goto xlerr2;
4689     pcm->xlpos = tt;
4690     if ( (cmd = nxtwrd (cmd)) != NULL) {
4691       if ( cmpwrd("b",cmd)||cmpwrd("bottom",cmd)) pcm->xlside = 0;
4692       if ( cmpwrd("t",cmd)||cmpwrd("top",cmd)) pcm->xlside = 1;
4693     }
4694     sprintf (pout,"SET XLPOS values:  Offset = %g  Side = ",tt);
4695     gaprnt (2,pout);
4696     if (pcm->xlside) gaprnt(2,"Top\n");
4697     else gaprnt(2,"Bottom\n");
4698   }
4699   else if (cmpwrd("ylpos",cmd)) {
4700     kwrd = 88;
4701     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4702     if ( valprs(cmd,&tt) == NULL ) goto xlerr2;
4703     pcm->ylpos = tt;
4704     pcm->ylpflg = 1;
4705     if ( (cmd = nxtwrd (cmd)) != NULL) {
4706       if ( cmpwrd("r",cmd)||cmpwrd("right",cmd)) pcm->ylside = 1;
4707       if ( cmpwrd("l",cmd)||cmpwrd("left",cmd)) pcm->ylside = 0;
4708     }
4709     sprintf (pout,"SET YLPOS values:  Offset = %g  Side = ",tt);
4710     gaprnt (2,pout);
4711     if (pcm->ylside) gaprnt(2,"Right\n");
4712     else gaprnt(2,"Left\n");
4713   }
4714   else if (cmpwrd("xlopts",cmd)) {
4715     kwrd = 70;
4716     itt = pcm->xlcol;
4717     itt1 = pcm->xlthck;
4718     tt = pcm->xlsiz;
4719     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4720     if ( intprs(cmd,&itt) == NULL ) goto err;
4721     if ( (cmd = nxtwrd (cmd)) != NULL) {
4722       if ( intprs(cmd,&itt1) == NULL ) goto xlerr;
4723       else {
4724         if ( (cmd = nxtwrd (cmd)) != NULL) {
4725           if ( valprs(cmd,&tt) == NULL ) goto xlerr;
4726         }
4727       }
4728     }
4729     pcm->xlcol = itt;
4730     pcm->xlthck = itt1;
4731     pcm->xlsiz = tt;
4732     sprintf (pout,"SET XLOPTS values:  Color = %i Thickness = %i",
4733       pcm->xlcol, pcm->xlthck);
4734     gaprnt (2,pout);
4735     sprintf (pout," Size = %g\n",pcm->xlsiz);
4736     gaprnt (2,pout);
4737   }
4738   else if (cmpwrd("ylopts",cmd)) {
4739     kwrd = 71;
4740     itt = pcm->ylcol;
4741     itt1 = pcm->ylthck;
4742     tt = pcm->ylsiz;
4743     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4744     if ( intprs(cmd,&itt) == NULL ) goto err;
4745     if ( (cmd = nxtwrd (cmd)) != NULL) {
4746       if ( intprs(cmd,&itt1) == NULL ) goto xlerr;
4747       else {
4748         if ( (cmd = nxtwrd (cmd)) != NULL) {
4749           if ( valprs(cmd,&tt) == NULL ) goto xlerr;
4750         }
4751       }
4752     }
4753     pcm->ylcol = itt;
4754     pcm->ylthck = itt1;
4755     pcm->ylsiz = tt;
4756     sprintf (pout,"SET YLOPTS values:  Color = %i Thickness = %i",
4757       pcm->ylcol, pcm->ylthck);
4758     gaprnt (2,pout);
4759     sprintf (pout," Size = %g\n",pcm->ylsiz);
4760     gaprnt (2,pout);
4761   }
4762   else if (cmpwrd("annot",cmd)) {
4763     kwrd = 56;
4764     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4765     if ( intprs(cmd,&itt) == NULL ) goto err;
4766     pcm->anncol = itt;
4767     if ( (cmd = nxtwrd (cmd)) != NULL) {
4768       if ( intprs(cmd,&itt) == NULL ) {
4769         gaprnt(0,"SET ANNOT Error: Invalid thickness value\n");
4770       } else {
4771         pcm->annthk = itt;
4772       }
4773     }
4774     sprintf (pout,"SET ANNOT values:  color = %i  thickness = %i\n",
4775       pcm->anncol, pcm->annthk);
4776     gaprnt (2,pout);
4777   }
4778   else if (cmpwrd("line",cmd)) {
4779     kwrd = 41;
4780     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4781     if ( intprs(cmd,&itt) == NULL ) goto err;
4782     pcm->lincol = itt;
4783     if ( (cmd = nxtwrd (cmd)) != NULL) {
4784       if ( intprs(cmd,&itt) == NULL ){
4785         gaprnt(0,"SET LINE Error: Invalid linestyle value\n");
4786       } else {
4787         pcm->linstl = itt;
4788         if ( (cmd = nxtwrd (cmd)) != NULL) {
4789           if ( intprs(cmd,&itt) == NULL ){
4790             gaprnt(0,"SET LINE Error: Invalid thickness value\n");
4791           } else {
4792             pcm->linthk = itt;
4793           }
4794         }
4795       }
4796     }
4797     sprintf (pout,"SET LINE values:  color = %i  style = %i",
4798       pcm->lincol, pcm->linstl);
4799     gaprnt (2,pout);
4800     sprintf (pout,"  thickness = %i\n",pcm->linthk);
4801     gaprnt (2,pout);
4802   }
4803   else if (cmpwrd("map",cmd)) {
4804     kwrd = 46;
4805     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4806     if (cmpwrd("auto",cmd)) {
4807       pcm->mapcol = -9;
4808     } else {
4809       if ( intprs(cmd,&itt) == NULL ) goto err;
4810       pcm->mapcol = itt;
4811       if ( (cmd = nxtwrd (cmd)) != NULL) {
4812         if ( intprs(cmd,&itt) == NULL ){
4813           gaprnt(0,"SET MAP Error: Invalid linestyle value\n");
4814         } else {
4815           pcm->mapstl = itt;
4816           if ( (cmd = nxtwrd (cmd)) != NULL) {
4817             if ( intprs(cmd,&itt) == NULL ){
4818               gaprnt(0,"SET MAP Error: Invalid thickness value\n");
4819             } else {
4820               pcm->mapthk = itt;
4821             }
4822           }
4823         }
4824       }
4825     }
4826     if (pcm->mapcol < 0 ) {
4827       gaprnt (2,"SET MAP values:  auto\n");
4828     } else {
4829       sprintf (pout,"SET MAP values:  color = %i  style = %i",
4830         pcm->mapcol, pcm->mapstl);
4831       gaprnt (2,pout);
4832       sprintf (pout,"  thickness = %i\n",pcm->mapthk);
4833       gaprnt (2,pout);
4834     }
4835   }
4836   else if (cmpwrd("string",cmd)) {
4837     kwrd = 42;
4838     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4839     if ( intprs(cmd,&itt) == NULL ) goto err;
4840     pcm->strcol = itt;
4841     if ( (cmd = nxtwrd (cmd)) != NULL) {
4842       itt = -1;
4843       for (i1=0; i1<9; i1++) if (cmpwrd(justs[i1],cmd)) itt=i1;
4844       if ( itt<0) {
4845         gaprnt(0,"SET STRING Error: Invalid justification value\n");
4846       } else {
4847         pcm->strjst = itt;
4848         if ( (cmd = nxtwrd (cmd)) != NULL) {
4849           if ( intprs(cmd,&itt) == NULL ){
4850             gaprnt(0,"SET STRING Error: Invalid thickness value\n");
4851           } else {
4852             pcm->strthk = itt;
4853             if ( (cmd = nxtwrd (cmd)) != NULL) {
4854               if ( valprs(cmd,&tt) == NULL ){
4855                 gaprnt(0,"SET STRING Error: Invalid rotation value\n");
4856               } else {
4857                 pcm->strrot = tt;
4858               }
4859             }
4860           }
4861         }
4862       }
4863     }
4864     sprintf (pout,"SET STRING values:  color = %i  just = %s",
4865       pcm->strcol, justs[pcm->strjst]);
4866     gaprnt (2,pout);
4867     sprintf (pout,"  thickness = %i  rotation = %g\n",
4868       pcm->strthk, pcm->strrot);
4869     gaprnt (2,pout);
4870   }
4871   else if (cmpwrd("strsiz",cmd)) {
4872     kwrd = 43;
4873     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4874     if ( valprs(cmd,&tt) == NULL ) goto err;
4875     pcm->strhsz = tt;
4876     i1 = 1;
4877     if ( (cmd = nxtwrd (cmd)) != NULL) {
4878       if ( valprs(cmd,&tt) == NULL ){
4879         gaprnt(0,"SET STRSIZ Error:  Invalid vsize value\n");
4880       } else {pcm->strvsz = tt; i1 = 0;}
4881     }
4882     if (i1) pcm->strvsz = pcm->strhsz;
4883     sprintf (pout,"SET STRSIZ values:  hsize = %g  vsize = %g\n",
4884       pcm->strhsz, pcm->strvsz);
4885     gaprnt (2,pout);
4886   }
4887   else if (cmpwrd("xaxis",cmd)) {
4888     kwrd = 19;
4889     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4890     if ( valprs(cmd,&(pcm->axmin)) == NULL ) goto err;
4891     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4892     if ( valprs(cmd,&(pcm->axmax)) == NULL ) goto err;
4893     pcm->axflg = 1;
4894     pcm->axint = 0.0;
4895     if ( (cmd = nxtwrd (cmd)) != NULL) {
4896       if ( valprs(cmd,&tt) != NULL ) pcm->axint = tt;
4897     }
4898     sprintf (pout,"xaxis labels range %g %g incr %g \n",pcm->axmin,
4899       pcm->axmax,pcm->axint);
4900     gaprnt (2,pout);
4901   }
4902   else if (cmpwrd("yaxis",cmd)) {
4903     kwrd = 20;
4904     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4905     if ( valprs(cmd,&(pcm->aymin)) == NULL ) goto err;
4906     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4907     if ( valprs(cmd,&(pcm->aymax)) == NULL ) goto err;
4908     pcm->ayflg = 1;
4909     pcm->ayint = 0.0;
4910     if ( (cmd = nxtwrd (cmd)) != NULL) {
4911       if ( valprs(cmd,&tt) == NULL ) goto err;
4912       else pcm->ayint = tt;
4913     }
4914     sprintf (pout,"yaxis labels range %g %g incr %g \n",pcm->aymin,
4915       pcm->aymax,pcm->ayint);
4916     gaprnt (2,pout);
4917   }
4918   else if (cmpwrd("misswarn",cmd)) {
4919     kwrd = 79;
4920     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4921     if (cmpwrd("on",cmd)) itt = 1;
4922     else if (cmpwrd("off",cmd)) itt=0;
4923     else goto err;
4924     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4925     if ( intprs(cmd,&id) == NULL ) goto err;
4926     for (i1=0; i1<id-1; i1++) {
4927       pfi = pfi->pforw;
4928       if (pfi==NULL) {
4929         sprintf (pout,"SET MISSWARN error:  file %i not open\n",id);
4930         gaprnt (0,pout);
4931         return(1);
4932       }
4933     }
4934     pfi->errflg = itt;
4935   }
4936   else if (cmpwrd("dfile",cmd)) {
4937     kwrd = 14;
4938     if (pcm->pfid==NULL) goto errf;
4939     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4940     if ( intprs(cmd,&id) == NULL ) goto err;
4941     pfi = pcm->pfi1;
4942     for (i1=0; i1<id-1; i1++) {
4943       pfi = pfi->pforw;
4944       if (pfi==NULL) {
4945         sprintf (pout,"SET DFILE error:  file %i not open\n",id);
4946         gaprnt (0,pout);
4947         return(1);
4948       }
4949     }
4950     sprintf (pout,"Default file set to: %s \n",pfi->name);
4951     gaprnt (2,pout);
4952     pcm->pfid = pfi;
4953     pcm->dfnum = id;
4954   }
4955   else if (cmpwrd("background",cmd)) {
4956     kwrd = 58;
4957     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4958     if ( intprs(cmd,&itt) == NULL ) goto err;
4959     sprintf (pout,"background = %i \n",itt);
4960     gaprnt (2,pout);
4961     gxbckg(itt);
4962   }
4963   else if (cmpwrd("cthick",cmd)) {
4964     kwrd = 49;
4965     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4966     if ( intprs(cmd,&(pcm->cthick)) == NULL ) goto err;
4967     sprintf (pout,"cthick = %i \n",pcm->cthick);
4968     gaprnt (2,pout);
4969   }
4970   else if (cmpwrd("cstyle",cmd)) {
4971     kwrd = 9;
4972     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4973     if ( intprs(cmd,&(pcm->cstyle)) == NULL ) goto err;
4974     if(pcm->cstyle==0) {
4975       sprintf (pout,"WARNING cstyle = 0 ; no lines will be plotted; I suggest 1 ...\n",pcm->cstyle);
4976     } else {
4977       sprintf (pout,"cstyle = %i \n",pcm->cstyle);
4978     }
4979     gaprnt (2,pout);
4980   }
4981   else if (cmpwrd("digsiz",cmd) || cmpwrd("digsize",cmd)) {
4982     kwrd = 24;
4983     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4984     if ( valprs(cmd,&(pcm->digsiz)) == NULL ) goto err;
4985     sprintf (pout,"digsiz = %g \n",pcm->digsiz);
4986     gaprnt (2,pout);
4987   }
4988   else if (cmpwrd("dignum",cmd)) {
4989     kwrd = 23;
4990     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
4991     if ( intprs(cmd,&(pcm->dignum)) == NULL ) goto err;
4992     if (pcm->dignum<0 || pcm->dignum>8) {
4993       gaprnt (0,"Invalid dignum value:  must be 0 to 8\n");
4994     } else {
4995       sprintf (pout,"dignum = %i \n",pcm->dignum);
4996       gaprnt (2,pout);
4997     }
4998   }
4999   else if (cmpwrd("axlim",cmd)||cmpwrd("vrange",cmd)) {
5000     kwrd = 15;
5001     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5002     if (cmpwrd("auto",cmd)) pcm->aflag = 0;
5003     else {
5004       if ( valprs(cmd,&(pcm->rmin)) == NULL ) goto err;
5005       pcm->aflag = 0;
5006       if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5007       if ( valprs(cmd,&(pcm->rmax)) == NULL ) goto err;
5008       pcm->aflag = -1;
5009       sprintf (pout, "1-D axis limits set: %g to %g \n",
5010            pcm->rmin, pcm->rmax);
5011       gaprnt (2,pout);
5012     }
5013   }
5014   else if (cmpwrd("vrange2",cmd)) {
5015     kwrd = 67;
5016     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5017     if ( valprs(cmd,&(pcm->rmin2)) == NULL ) goto err;
5018     pcm->aflag2 = 0;
5019     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5020     if ( valprs(cmd,&(pcm->rmax2)) == NULL ) goto err;
5021     pcm->aflag2 = -1;
5022     sprintf (pout, "Scatter Y axis limits set: %g to %g \n",
5023          pcm->rmin2, pcm->rmax2);
5024     gaprnt (2,pout);
5025   }
5026   else if (cmpwrd("strmden",cmd)) {
5027     kwrd = 64;
5028     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5029     if ( intprs(cmd,&(pcm->strmden)) == NULL ) goto err;
5030   }
5031   else if (cmpwrd("ccolor",cmd)) {
5032     kwrd = 10;
5033     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5034     if (cmpwrd("rainbow",cmd)) {
5035       pcm->ccolor = -1;
5036       gaprnt (2,"ccolor = rainbow \n");
5037     } else if (cmpwrd("revrain",cmd)) {
5038       pcm->ccolor = -2;
5039       gaprnt (2,"ccolor = reverse rainbow \n");
5040     } else {
5041       if ( intprs(cmd,&(pcm->ccolor)) == NULL ) goto err;
5042       sprintf (pout,"ccolor = %i \n",pcm->ccolor);
5043       gaprnt (2,pout);
5044     }
5045   }
5046   else if (cmpwrd("stid",cmd)) {
5047     kwrd = 30;
5048     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5049     if (cmpwrd("on",cmd)) pcm->stidflg = 1;
5050     else if (cmpwrd("off",cmd)) pcm->stidflg = 0;
5051     else goto err;
5052   }
5053   else if (cmpwrd("csmooth",cmd)) {
5054     kwrd = 16;
5055     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5056     if (cmpwrd("on",cmd)) pcm->csmth = 1;
5057     else if (cmpwrd("off",cmd)) pcm->csmth = 0;
5058     else if (cmpwrd("linear",cmd)) pcm->csmth = 2;
5059     else goto err;
5060   }
5061   else if (cmpwrd("cterp",cmd)) {
5062     kwrd = 34;
5063     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5064     if (cmpwrd("on",cmd)) pcm->cterp = 1;
5065     else if (cmpwrd("off",cmd)) pcm->cterp = 0;
5066     else goto err;
5067   }
5068   else if (cmpwrd("loopdim",cmd)) {
5069     kwrd = 11;
5070     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5071     if (cmpwrd("x",cmd)) pcm->loopdim=0;
5072     else if (cmpwrd("y",cmd)) pcm->loopdim=1;
5073     else if (cmpwrd("z",cmd)) pcm->loopdim=2;
5074     else if (cmpwrd("t",cmd)) pcm->loopdim=3;
5075     else goto err;
5076   }
5077   else if (cmpwrd("loopincr",cmd)) {
5078     kwrd = 13;
5079     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5080     if ( valprs(cmd,&(pcm->loopincr)) == NULL ) goto err;
5081     sprintf (pout,"loopincr = %g \n",pcm->loopincr);
5082     gaprnt (2,pout);
5083   }
5084   else if (cmpwrd("grads",cmd)) {
5085     kwrd = 31;
5086     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5087     if ( cmpwrd("on",cmd) ) pcm->grdsflg = 1;
5088     else if (cmpwrd("off",cmd) ) {
5089       pcm->grdsflg = 0;
5090       pcm->timelabflg = 0;
5091     }
5092     else goto err;
5093   }
5094   else if (cmpwrd("timelab",cmd)) {
5095     kwrd = 100;
5096     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5097     if ( cmpwrd("on",cmd) ) pcm->timelabflg = 1;
5098     else if (cmpwrd("off",cmd) ) pcm->timelabflg = 0;
5099     else goto err;
5100   }
5101   else if (cmpwrd("stnprint",cmd)) {
5102     kwrd = 102;
5103     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5104     if ( cmpwrd("on",cmd) ) pcm->stnprintflg = 1;
5105     else if (cmpwrd("off",cmd) ) pcm->stnprintflg = 0;
5106     else goto err;
5107   }
5108   else if (cmpwrd("warn",cmd)) {
5109     kwrd = 101;
5110     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5111     if ( cmpwrd("on",cmd) ) mfcmn.warnflg = 1;
5112     else if (cmpwrd("off",cmd) ) mfcmn.warnflg = 0;
5113     else goto err;
5114   }
5115   else if (cmpwrd("looping",cmd)) {
5116     kwrd = 12;
5117     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5118     if ( cmpwrd("on",cmd) ) {
5119       pcm->loopflg = 1;
5120       gaprnt (2,"Looping is on \n");
5121     } else if (cmpwrd("off",cmd) ) {
5122       pcm->loopflg = 0;
5123       gaprnt (2,"Looping is off \n");
5124     } else goto err;
5125   }
5126   else if (cmpwrd("x",cmd) || cmpwrd("y",cmd) ||
5127            cmpwrd("z",cmd) || cmpwrd("t",cmd) ) {
5128     if (*cmd=='x') kwrd=0;
5129     if (*cmd=='y') kwrd=1;
5130     if (*cmd=='z') kwrd=2;
5131     if (*cmd=='t') kwrd=3;
5132     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5133     if (pcm->pfid==NULL) goto errf;
5134     pfi = pcm->pfid;
5135     num = 1;
5136     if (kwrd==3 && cmpwrd("last",cmd)) {
5137       v1 = pfi->dnum[3];
5138     } else {
5139       if ( valprs(cmd,&v1) == NULL ) goto err;
5140     }
5141     v2 = v1;
5142     if ( (cmd = nxtwrd (cmd)) != NULL) {
5143       num = 2;
5144       if (kwrd==3 && cmpwrd("last",cmd)) {
5145         v2 = pfi->dnum[3];
5146       } else {
5147         if ( valprs(cmd,&v2) == NULL ) goto err;
5148       }
5149       if (v1==v2) num=1;
5150     }
5151     pcm->vdim[kwrd] = num-1;
5152 
5153     /* Try to save grid dims for write flag -ex */
5154 
5155     if (pfi->type==1 && num==2) {
5156       if (kwrd==0) {
5157         pcm->xexflg = 1;
5158         pcm->x1ex = (int)(v1+0.001);
5159         pcm->x2ex = (int)(v2+0.001);
5160       }
5161       if (kwrd==1) {
5162         pcm->yexflg = 1;
5163         pcm->y1ex = (int)(v1+0.001);
5164         pcm->y2ex = (int)(v2+0.001);
5165       }
5166     }
5167 
5168     if (pfi->type==1 && num==1) {
5169       v1 = floor(v1+0.5);
5170       pcm->vdim[kwrd] = 0;
5171     }
5172     if (kwrd==3) {
5173       vals = pfi->grvals[3];
5174       gr2t (vals,v1,&(pcm->tmin));
5175       if (num==1) pcm->tmax = pcm->tmin;
5176       else gr2t(vals,v2,&(pcm->tmax));
5177       gaprnt (2,"Time values set: ");
5178       sprintf (pout,"%li:%li:%li:%li ",pcm->tmin.yr,pcm->tmin.mo,
5179         pcm->tmin.dy,pcm->tmin.hr);
5180       gaprnt (2,pout);
5181       sprintf (pout,"%li:%li:%li:%li \n",pcm->tmax.yr,pcm->tmax.mo,
5182         pcm->tmax.dy,pcm->tmax.hr);
5183       gaprnt (2,pout);
5184     } else {
5185       if (pfi->type==1) {
5186         conv = pfi->gr2ab[kwrd];
5187         vals = pfi->grvals[kwrd];
5188         pcm->dmin[kwrd] = conv(vals,v1);
5189         if (num==1)
5190 	  pcm->dmax[kwrd] = pcm->dmin[kwrd];
5191         else
5192 	  pcm->dmax[kwrd] = conv(vals,v2);
5193       } else {
5194         pcm->dmin[kwrd] = v1;
5195         if (num==1)
5196 	  pcm->dmax[kwrd] = pcm->dmin[kwrd];
5197         else
5198 	  pcm->dmax[kwrd] = v2;
5199       }
5200       sprintf (pout,"%s set to %g %g \n",kwds[kwrd+4], pcm->dmin[kwrd], pcm->dmax[kwrd]);
5201       gaprnt (2,pout);
5202     }
5203   }
5204   else if (cmpwrd("lon",cmd) || cmpwrd("lat",cmd) ||
5205            cmpwrd("lev",cmd) ) {
5206     if (cmpwrd("lon",cmd)) {kwrd=4; id=0;}
5207     if (cmpwrd("lat",cmd)) {kwrd=5; id=1;}
5208     if (cmpwrd("lev",cmd)) {kwrd=6; id=2;}
5209     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5210     if (pcm->pfid==NULL) goto errf;
5211     num = 1;
5212     if ( valprs(cmd,&(pcm->dmin[id])) == NULL ) goto err;
5213     pcm->dmax[id] = pcm->dmin[id];
5214     if ( (cmd = nxtwrd (cmd)) != NULL) {
5215       num = 2;
5216       if ( valprs(cmd,&(pcm->dmax[id])) == NULL ) goto err;
5217       if (pcm->dmin[id]==pcm->dmax[id]) num=1;
5218     }
5219     pcm->vdim[id] = num-1;
5220     pfi = pcm->pfid;
5221     if (pfi->type==1 && num==1) {
5222       pcm->vdim[id] = 0;
5223       conv = pfi->ab2gr[id];
5224       vals = pfi->abvals[id];
5225       v1 = conv(vals,pcm->dmin[id]);
5226       v1 = floor(v1+0.5);
5227       conv = pfi->gr2ab[id];
5228       vals = pfi->grvals[id];
5229       pcm->dmin[id] = conv(vals,v1);
5230       pcm->dmax[id] = pcm->dmin[id];
5231     }
5232     sprintf (pout,"%s set to %g %g \n",kwds[id+4],pcm->dmin[id],pcm->dmax[id]);
5233     gaprnt (2,pout);
5234   }
5235   else if (cmpwrd("time",cmd)) {
5236     kwrd=7; id=3;
5237     if (pcm->pfid==NULL) goto errf;
5238     if ( (cmd = nxtwrd(cmd)) == NULL) goto err;
5239     num = 1;
5240     tdef = pcm->tmin;
5241     if ( adtprs(cmd,&tdef,&(pcm->tmin)) == NULL ) goto err;
5242     pcm->tmax = pcm->tmin;
5243     if ( (cmd = nxtwrd (cmd)) != NULL) {
5244       num = 2;
5245       if ( adtprs(cmd,&tdef,&(pcm->tmax)) == NULL ) goto err;
5246     }
5247     pcm->vdim[3] = 1;
5248     if (num==1) {
5249       pcm->vdim[3] = 0;
5250       pfi = pcm->pfid;
5251       vals = pfi->abvals[3];
5252       v1 = t2gr(vals,&(pcm->tmin));
5253       v1 = floor(v1+0.5);
5254       vals = pfi->grvals[3];
5255       gr2t (vals,v1,&(pcm->tmin));
5256       pcm->tmax = pcm->tmin;
5257     }
5258     gaprnt (2,"Time values set: ");
5259     sprintf (pout,"%li:%li:%li:%li ",pcm->tmin.yr,pcm->tmin.mo,
5260       pcm->tmin.dy,pcm->tmin.hr);
5261     gaprnt (2,pout);
5262     sprintf (pout,"%li:%li:%li:%li \n",pcm->tmax.yr,pcm->tmax.mo,
5263       pcm->tmax.dy,pcm->tmax.hr);
5264     gaprnt (2,pout);
5265   }
5266   else if (cmpwrd("datawarn",cmd)) {
5267     kwrd = 107;
5268     if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5269     if ( cmpwrd("on",cmd) ) pcm->dwrnflg = 1;
5270     else if (cmpwrd("off",cmd) ) pcm->dwrnflg = 0;
5271     else goto err;
5272   }
5273   else if (cmpwrd("fill",cmd)) {
5274     kwrd = 96;
5275     pat = (char *)malloc(5);
5276     if ( (cmd = nxtwrd (cmd)) == NULL) goto pterr;
5277     if ( cmpwrd("on",cmd) ) {
5278       sprintf(pat,"%s","on");
5279       pcm->ptflg = 1;
5280     }
5281     else if ( cmpwrd("off",cmd) ) {
5282       sprintf(pat,"%s","off");
5283       pcm->ptflg = 0;
5284     }
5285     else if ( cmpwrd("open",cmd)) {
5286       pcm->ptflg = 1;
5287       pcm->ptopt = 0;
5288       sprintf(pat,"%s","open");
5289     }
5290     else if ( cmpwrd("solid",cmd)) {
5291       sprintf(pat,"%s","solid");
5292       pcm->ptflg = 1;
5293       pcm->ptopt = 1;
5294     }
5295     else if ( cmpwrd("dot",cmd)) {
5296       sprintf(pat,"%s","dot");
5297       pcm->ptflg = 1;
5298       pcm->ptopt = 2;
5299       if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5300       if ( intprs(cmd,&num) == NULL ) goto err;
5301       if (num<1 || num>6)
5302         gaprnt (0,"Invalid ptrnden value:  must be integer 1 to 6\n");
5303       else
5304         pcm->ptden=num;
5305     }
5306     else if ( cmpwrd("line",cmd)) {
5307       sprintf(pat,"%s","line");
5308       pcm->ptflg = 1;
5309       pcm->ptopt = 3;
5310       if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5311       if ( intprs(cmd,&num) == NULL ) goto err;
5312       if (num<1 || num>5)
5313         gaprnt (0,"Invalid ptrnden value:  must be integer 1 to 5\n");
5314       else
5315         pcm->ptden=num;
5316       if ( (cmd = nxtwrd (cmd)) == NULL) goto err;
5317       if ( intprs(cmd,&num) == NULL ) goto err;
5318       if (num!=-90&&num!=90&&num!=-60&&num!=60&&num!=-30&&num!=30
5319 	  &&num!=-45&&num!=45&&num!=0) {
5320         gaprnt (0,"Invalid ptrnang value:  must be -90, -60, -45 -30\n");
5321         gaprnt (0," 	                        0, 30. 45, 60, or 90\n");
5322       }
5323       else
5324         pcm->ptang=num;
5325     }
5326     else goto err;
5327 
5328     if ( cmpwrd("line",pat) )
5329       sprintf (pout,"SET FILL values: %s %d %d\n",pat,pcm->ptden,pcm->ptang);
5330     else if ( cmpwrd("dot",pat) )
5331       sprintf (pout,"SET FILL values: %s %d\n",pat,pcm->ptden);
5332     else
5333       sprintf (pout,"SET FILL values: %s\n",pat);
5334     gaprnt (2,pout);
5335     free(pat);
5336   }
5337 
5338 #if USELATS == 1
5339 /*mf
5340   961125 - LATS setup  interface
5341 mf*/
5342 
5343   else if (cmpwrd("lats",cmd)) {
5344 
5345     kwrd = 98;
5346 
5347     if ( (cmd = nxtwrd (cmd)) == NULL) {
5348       gaprnt (0,"SET LATS error:  No arguments....\n");
5349       gaprnt (0,"  valid arguments are: \n");
5350       gaprnt (0,"  parmtab [FILENAME] (e.g., set lats parmtab lats.ncep.MRFtable)\n");
5351       gaprnt (0,"  convention [grads_grib|grib_only|coards] (e.g., set lats convention grib)\n");
5352       gaprnt (0,"  calendar [standard|noleap|clim|climleap] (e.g., set lats calendar standard)\n");
5353       gaprnt (0,"  frequency [yearly|monthly|monthly_table_comp|weekly|daily|hourly|forecast_hourlyfixed] (e.g., set lats frequency hourly)\n");
5354       gaprnt (0,"  delatat [N] (integer number of freq units per time output, e.g., set lats deltat 6)\n");
5355       gaprnt (0,"  fhour [N] (integer forecast hour,  e.g., set lats fhour 120)\n");
5356       gaprnt (0,"  model [MODEL_NAME] (e.g., set lats model MRF)\n");
5357       gaprnt (0,"  center [CENTER_NAME] (e.g., set lats center NCEP)\n");
5358       gaprnt (0,"  create [FILENAME] (e.g., set lats create MRF.EXP1)\n");
5359       gaprnt (0,"  comment [COMMENT (e.g., set lats comment \"R1.6.1 of MRF with convection update\")\n");
5360       gaprnt (0,"  gridtype [linear|gaussian|generic] (e.g., set lats gridtype gaussian\n");
5361       gaprnt (0,"  vertdim [DIMNAME val1 val2 ... valN] (e.g., set lats vertdim plev 1000 850 500 200)\n");
5362       gaprnt (0,"  var [VARNAME average|accum|instant LEVEL_ID)] (e.g., set lats var u instant 1)\n");
5363       gaprnt (0,"  timeoption [grid|dim_env (e.g., set lats v timeoption dim_env (use the GrADS dimension environment)\n");
5364       gaprnt (0,"  write [VAR_ID|(LEVEL)] (e.g., set lats write 1 500 (return from t lats var)\n");
5365       gaprnt (0,"  close (e.g., set lats close)\n\n");
5366       return(1);
5367 
5368     }
5369 
5370 /*-------------------
5371   lats_parmtab interface
5372 -------------------*/
5373 
5374     if(cmpwrd("parmtab",cmd)) {
5375       if ( (cmd = nxtwrd (cmd)) == NULL) {
5376 	gaprnt (0,"SET LATS PARMTAB error:  No arguments....\n");
5377 	return(1);
5378       } else strcpy(pcm->glats.ptname,cmd);
5379       rc=galats(pcm,0,0);
5380       sprintf(pout,"LATS PARMTAB ID = %d\n",rc);
5381       gaprnt(2,pout);
5382     }
5383 
5384 /*----------
5385   lats_create interface
5386 ---------*/
5387 
5388     else if (cmpwrd("convention",cmd)) {
5389       if ( (cmd = nxtwrd (cmd)) == NULL) {
5390 	gaprnt (0,"SET LATS CONVENTION error:  No arguments....\n");
5391 	return(1);
5392       } else if( cmpwrd("grads_grib",cmd) ) pcm->glats.convention=LATS_GRADS_GRIB;
5393       else if( cmpwrd("coards",cmd) ) pcm->glats.convention=LATS_COARDS;
5394       else if( cmpwrd("grib_only",cmd) ) pcm->glats.convention=LATS_GRIB_ONLY;
5395       else {
5396 	gaprnt (0,"SET LATS CONVENTION error:  invalid argument using the default...\n");
5397 	return(1);
5398       }
5399     }
5400 
5401     else if (cmpwrd("calendar",cmd)) {
5402       if ( (cmd = nxtwrd (cmd)) == NULL) {
5403 	gaprnt (0,"SET LATS CALENDAR error:  No arguments....\n");
5404 	return(1);
5405       }
5406       else if( cmpwrd("standard",cmd) ) pcm->glats.calendar=LATS_STANDARD;
5407       else if( cmpwrd("noleap",cmd) ) pcm->glats.calendar=LATS_NOLEAP;
5408       else if( cmpwrd("clim",cmd) ) pcm->glats.calendar=LATS_CLIM;
5409       else if( cmpwrd("climleap",cmd) ) pcm->glats.calendar=LATS_CLIMLEAP;
5410       else {
5411 	gaprnt (0,"SET LATS CALENDAR error:  Invalid argument using the default...\n");
5412 	return(1);
5413       }
5414     }
5415 
5416     else if(cmpwrd("frequency",cmd)) {
5417       if ( (cmd = nxtwrd (cmd)) == NULL) {
5418 	gaprnt (0,"SET LATS FREQUENCY error:  No arguments....\n");
5419 	return(1);
5420       } else if( cmpwrd("yearly",cmd) ) pcm->glats.frequency=LATS_YEARLY;
5421       else if( cmpwrd("monthly",cmd) ) pcm->glats.frequency=LATS_MONTHLY;
5422       else if( cmpwrd("monthly_table_comp",cmd) ) pcm->glats.frequency=LATS_MONTHLY_TABLE_COMP;
5423       else if( cmpwrd("weekly",cmd) ) pcm->glats.frequency=LATS_WEEKLY;
5424       else if( cmpwrd("daily",cmd) ) pcm->glats.frequency=LATS_DAILY;
5425       else if( cmpwrd("hourly",cmd) ) pcm->glats.frequency=LATS_HOURLY;
5426       else if( cmpwrd("forecast_hourly",cmd) ) {
5427 	pcm->glats.frequency=LATS_FORECAST_HOURLY;
5428 	pcm->glats.time_opt=3;
5429       }
5430       else if( cmpwrd("fixed",cmd) ) pcm->glats.frequency=LATS_FIXED;
5431       else {
5432 	gaprnt (0,"SET LATS FREQUENCY error:  Invalid arguments LATS_CREATE WILL FAIL....\n");
5433 	return(1);
5434       }
5435     }
5436 
5437     else if(cmpwrd("deltat",cmd)) {
5438       if ( (cmd = nxtwrd (cmd)) == NULL) {
5439 	gaprnt (0,"SET LATS DELTAT error:  No arguments....\n");
5440 	return(1);
5441       }
5442       if ( intprs(cmd,&(i)) == NULL ) {
5443 	gaprnt (0,"SET LATS DELTAT error:  invalid or missing increment\n");
5444 	return(1);
5445       } else if (i<0) {
5446 	gaprnt (0,"SET LATS DELTAT error:  < 0 LATS_CREATE WILL FAIL...\n");
5447 	return(1);
5448       } else {
5449 	pcm->glats.deltat=i;
5450       }
5451     }
5452 
5453     else if(cmpwrd("fhour",cmd)) {
5454       if ( (cmd = nxtwrd (cmd)) == NULL) {
5455 	gaprnt (0,"SET LATS FHOUR error:  No arguments....\n");
5456 	return(1);
5457       }
5458       if ( intprs(cmd,&(i)) == NULL ) {
5459 	gaprnt (0,"SET LATS FHOUR error:  invalid or missing increment\n");
5460 	return(1);
5461       } else if (i<0) {
5462 	gaprnt (0,"SET LATS FHOUR error:  < 0 LATS WILL FAIL...\n");
5463 	return(1);
5464       } else {
5465 	pcm->glats.fhour=i;
5466       }
5467     }
5468 
5469     else if(cmpwrd("basetime",cmd)) {
5470       if ( (cmd = nxtwrd (cmd)) == NULL) {
5471 	gaprnt (0,"SET LATS BASETIME error:  No arguments....\n");
5472 	return(1);
5473       }
5474       if ( intprs(cmd,&(i)) == NULL ) {
5475 	gaprnt (0,"SET LATS BASETIME ID_FILE error:  missing\n");
5476 	return(1);
5477       } else if (i<0) {
5478 	gaprnt (0,"SET LATS BASETIME ID_FILE error:  id_file < 0\n");
5479 	return(1);
5480       } else {
5481 	if(i >= 0 && i <= pcm->glats.id_file) {
5482 	  pcm->glats.id_user_file=i;
5483 	} else {
5484 	  sprintf(pout,"SET LATS BASETIME error:  FILE ID is %d but the max FILE ID is %d\n",
5485 		  i,pcm->glats.id_file);
5486 	  gaprnt(0,pout);
5487 	  return(1);
5488 	}
5489       }
5490 
5491       if ( (cmd = nxtwrd (cmd)) == NULL) {
5492 	gaprnt (0,"SET LATS BASETIME YEAR error:  missing\n");
5493 	return(1);
5494       }
5495       if ( intprs(cmd,&(i)) == NULL ) {
5496 	gaprnt (0,"SET LATS BASETIME YEAR error:  invalid or missing\n");
5497 	return(1);
5498       } else if (i<0) {
5499 	gaprnt (0,"SET LATS BASETIME YEAR error:  year < 0\n");
5500 	return(1);
5501       } else {
5502 	pcm->glats.lyr=i;
5503       }
5504 
5505       if ( (cmd = nxtwrd (cmd)) == NULL) {
5506 	gaprnt (0,"SET LATS BASETIME MONTH error:  missing\n");
5507 	return(1);
5508       }
5509       if ( intprs(cmd,&(i)) == NULL ) {
5510 	gaprnt (0,"SET LATS BASETIME MONTH error:  invalid or missing\n");
5511 	return(1);
5512       } else if (i<0) {
5513 	gaprnt (0,"SET LATS BASETIME MONTH error:  year < 0\n");
5514 	return(1);
5515       } else {
5516 	pcm->glats.lmo=i;
5517       }
5518 
5519       if ( (cmd = nxtwrd (cmd)) == NULL) {
5520 	gaprnt (0,"SET LATS BASETIME DAY error:  missing\n");
5521 	return(1);
5522       }
5523       if ( intprs(cmd,&(i)) == NULL ) {
5524 	gaprnt (0,"SET LATS BASETIME DAY error:  invalid or missing\n");
5525 	return(1);
5526       } else if (i<0) {
5527 	gaprnt (0,"SET LATS BASETIME DAY error:  year < 0\n");
5528 	return(1);
5529       } else {
5530 	pcm->glats.lda=i;
5531       }
5532 
5533       if ( (cmd = nxtwrd (cmd)) == NULL) {
5534 	gaprnt (0,"SET LATS BASETIME HOUR error:  missing\n");
5535 	return(1);
5536       }
5537       if ( intprs(cmd,&(i)) == NULL ) {
5538 	gaprnt (0,"SET LATS BASETIME HOUR error:  invalid or missing\n");
5539 	return(1);
5540       } else if (i<0) {
5541 	gaprnt (0,"SET LATS BASETIME HOUR error:  year < 0\n");
5542 	return(1);
5543       } else {
5544 	pcm->glats.lhr=i;
5545       }
5546 
5547       rc=galats(pcm,10,0);  /* set the basetime */
5548 
5549     }
5550 
5551     else if(cmpwrd("model",cmd)) {
5552       if ( (cmd = nxtwrd (cmd)) == NULL) {
5553 	gaprnt (0,"SET LATS MODEL error:  No arguments....\n");
5554 	return(1);
5555       } else strcpy(pcm->glats.model,cmd);
5556     }
5557 
5558     else if(cmpwrd("center",cmd)) {
5559       if ( (cmd = nxtwrd (cmd)) == NULL) {
5560 	gaprnt (0,"SET LATS CENTER error:  No arguments....\n");
5561 	return(1);
5562       } else strcpy(pcm->glats.center,cmd);
5563     }
5564 
5565     else if(cmpwrd("comment",cmd)) {
5566       if ( (cmd = nxtwrd (cmd)) == NULL) {
5567 	gaprnt (0,"SET LATS COMMENT error:  No arguments....\n");
5568 	return(1);
5569       } else strcpy(pcm->glats.comment,cmd);
5570     }
5571 
5572     else if(cmpwrd("varcomment",cmd)) {
5573       if ( (cmd = nxtwrd (cmd)) == NULL) {
5574 	gaprnt (0,"SET LATS VARCOMMENT error:  No arguments....\n");
5575 	return(1);
5576       } else strcpy(pcm->glats.var_comment,cmd);
5577     }
5578 
5579     else if(cmpwrd("create",cmd)) {
5580       if ( (cmd = nxtwrd (cmd)) == NULL) {
5581 	gaprnt (0,"SET LATS CREATE error:  Missing or invalid arguments");
5582 	return (1);
5583       } else strcpy(pcm->glats.oname,cmd);
5584       rc=galats(pcm,1,0);  /* open the lats file */
5585       sprintf(pout,"LATS FILE ID = %d\n",rc);
5586       gaprnt(2,pout);
5587     }
5588 
5589 
5590 /*-----------------
5591   lats_grid interface
5592 -----------------*/
5593 
5594     else if (cmpwrd("gridtype",cmd)) {
5595       if ( (cmd = nxtwrd (cmd)) == NULL) {
5596 	gaprnt (0,"SET LATS GRIDTYPE error:  No arguments....\n");
5597 	return(1);
5598       } else if( cmpwrd("linear",cmd) ) pcm->glats.gridtype=LATS_LINEAR;
5599       else if( cmpwrd("gaussian",cmd) ) pcm->glats.gridtype=LATS_GAUSSIAN;
5600       else if( cmpwrd("generic",cmd) ) pcm->glats.gridtype=LATS_GENERIC;
5601       else goto err;
5602     }
5603 
5604     else if(cmpwrd("gridname",cmd)) {
5605       if ( (cmd = nxtwrd (cmd)) == NULL) {
5606 	gaprnt (0,"SET LATS GRIDNAME error:  No arguments....\n");
5607 	return(1);
5608       } else strcpy(pcm->glats.gridname,cmd);
5609     }
5610 
5611 /*------------
5612   lats_vertdim interface
5613 -------------*/
5614 
5615     else if(cmpwrd("vertdim",cmd)) {
5616       pcm->glats.nlev=0;
5617       if ( (cmd = nxtwrd (cmd)) == NULL) {
5618 	gaprnt (0,"SET LATS VERTDIM error:  No arguments....\n");
5619 	return(1);
5620       } else {
5621 	i=0;
5622 	while(*(cmd+i) != ' ') {
5623 	  pcm->glats.vertdimname[i]=*(cmd+i);
5624 	  i++;
5625 	}
5626 	pcm->glats.vertdimname[i]='\0';
5627       }
5628 
5629       if ( (cmd = nxtwrd (cmd)) == NULL) {
5630 	gaprnt (0,"SET LATS VERTDIM error:  no levels given....\n");
5631 	return(1);
5632       }
5633 
5634       while( valprs(cmd,&(v1)) != NULL) {
5635 	pcm->glats.levels[pcm->glats.nlev]=(double)v1;
5636 	pcm->glats.nlev++;
5637 	if ( (cmd = nxtwrd (cmd)) == NULL) break;
5638       }
5639 
5640       rc=galats(pcm,2,0);  /* set the vertical dimension */
5641       sprintf(pout,"LATS VERTDIM ID = %d\n",rc);
5642       gaprnt(2,pout);
5643 
5644     }
5645 
5646 /*------
5647   lats_var interface
5648 ------*/
5649 
5650     else if(cmpwrd("var",cmd)) {
5651 
5652       if ( (cmd = nxtwrd (cmd)) == NULL) {
5653 	sprintf(pout,"LATS VAR ID = -1\n");
5654 	gaprnt(2,pout);
5655 	gaprnt (0,"SET LATS VAR error:  No arguments! args: fileid varname datatype gridid levid\n");
5656 	return(1);
5657 
5658       }
5659 
5660       /* --- file id */
5661 
5662       if ( intprs(cmd,&(i)) == NULL ) {
5663 	sprintf(pout,"LATS VAR ID = -1\n");
5664 	gaprnt(2,pout);
5665 	gaprnt (0,"SET LATS VAR FILE ID:  invalid FILE ID value given\n");
5666 	return(1);
5667       } else if(i >= 0 && i <= pcm->glats.id_file) {
5668 	pcm->glats.id_user_file=i;
5669       } else {
5670 	sprintf(pout,"LATS VAR ID = -1\n");
5671 	gaprnt(2,pout);
5672 	sprintf(pout,
5673 		"SET LATS VAR error:  FILE ID is %d but the max FILE ID is %d\n",
5674 		i,pcm->glats.id_file);
5675 	gaprnt(0,pout);
5676 	return(1);
5677 
5678       }
5679 
5680       /* --- stat type */
5681 
5682       if ( (cmd = nxtwrd (cmd)) == NULL) {
5683 	gaprnt (0,"SET LATS VAR error:  variable name not given...\n");
5684 	return(1);
5685       }else {
5686 	i=0;
5687 	while(*(cmd+i) != ' ') {
5688 	  pcm->glats.var[i]=*(cmd+i);
5689 	  i++;
5690 	}
5691 	pcm->glats.var[i]='\0';
5692       }
5693 
5694       if ( (cmd = nxtwrd (cmd)) == NULL) {
5695 	gaprnt (0,"SET LATS VAR error:  variable statistic type given...\n");
5696 	return(1);
5697       }else if( cmpwrd("average",cmd) ) pcm->glats.timestat=LATS_AVERAGE;
5698       else if( cmpwrd("accum",cmd) ) pcm->glats.timestat=LATS_ACCUM;
5699       else if( cmpwrd("instant",cmd) ) pcm->glats.timestat=LATS_INSTANT;
5700       else {
5701 	gaprnt (0,"SET LATS VAR error:  invalid variable statistic type given...\n");
5702 	return(1);
5703       }
5704 
5705       /* --- level grid id */
5706 
5707       if ( (cmd = nxtwrd (cmd)) == NULL) {
5708 	sprintf(pout,"LATS VAR ID = -1\n");
5709 	gaprnt(2,pout);
5710 	gaprnt (0,"SET LATS VAR error:  missing GRID ID\n");
5711 	return(1);
5712       }
5713 
5714       if ( intprs(cmd,&(i)) == NULL ) {
5715 	sprintf(pout,"LATS VAR ID = -1\n");
5716 	gaprnt(2,pout);
5717 	gaprnt (0,"SET LATS VAR error:  invalid GRID ID value given\n");
5718 	return(1);
5719       } else if(i >= 0 && i <= pcm->glats.id_grid) {
5720 	pcm->glats.id_user_grid=i;
5721       } else {
5722 	sprintf(pout,"LATS VAR ID = -1\n");
5723 	gaprnt(2,pout);
5724 	sprintf(pout,
5725 		"SET LATS VAR error:  GRID ID is %d but the max GRID ID is %d\n",
5726 		i,pcm->glats.id_user_grid);
5727 	gaprnt(0,pout);
5728 	return(1);
5729 
5730       }
5731 
5732       /* --- level id */
5733 
5734       if ( (cmd = nxtwrd (cmd)) == NULL) {
5735 	sprintf(pout,"LATS VAR ID = -1\n");
5736 	gaprnt(2,pout);
5737 	gaprnt (0,"SET LATS VAR error:  missing LEVEL ID\n");
5738 	gaprnt (0,"                     use a value of 0 for surface variables\n");
5739 	return(1);
5740       }
5741 
5742       if ( intprs(cmd,&(i)) == NULL ) {
5743 	sprintf(pout,"LATS VAR ID = -1\n");
5744 	gaprnt(2,pout);
5745 	gaprnt (0,"SET LATS VAR error:  invalid LEVEL ID value given\n");
5746 	gaprnt (0,"                     use a value of 0 for surface variable\n");
5747 	return(1);
5748       } else if(i >= 0 && i <= pcm->glats.id_lev) {
5749 	pcm->glats.id_user_lev=i;
5750       } else {
5751 	sprintf(pout,"LATS VAR ID = -1\n");
5752 	gaprnt(2,pout);
5753 	sprintf(pout,
5754 		"SET LATS VAR error:  LEVEL ID is %d but the max LEVEL ID is %d\n",
5755 		i,pcm->glats.id_user_lev);
5756 	gaprnt(0,pout);
5757 	gaprnt(0,"              use a value of 0 for surface variables...\n");
5758 	return(1);
5759 
5760       }
5761 
5762       /* --- call lats_var */
5763 
5764       rc=galats(pcm,4,0);
5765       sprintf(pout,"LATS VAR ID = %d\n",rc);
5766       gaprnt(2,pout);
5767 
5768     }
5769 
5770 
5771 /*-------------------
5772   lats_write interface
5773 -------------------*/
5774 
5775     else if(cmpwrd("write",cmd)) {
5776 
5777       pcm->glats.id_user_write=0;
5778 
5779       /* --- file id */
5780 
5781       if ( (cmd = nxtwrd (cmd)) == NULL) {
5782 	gaprnt (0,"SET LATS WRITE error:  No arguments (fileid varid)\n");
5783 	return(1);
5784       }
5785 
5786       if ( intprs(cmd,&(i)) == NULL ) {
5787 	gaprnt (0,"SET LATS WRITE error:  missing or invalid FILE ID given...\n");
5788 	return(1);
5789 
5790       } else {
5791 
5792 	pcm->glats.id_user_file=i;
5793 	if(pcm->glats.id_user_file<0 || (pcm->glats.id_user_file > pcm->glats.id_file) ) {
5794 	  sprintf(pout,
5795 		  "SET LATS WRITE error: FILE ID is outside the valid range of 1 - %d\n",
5796 		  pcm->glats.id_file);
5797 	  gaprnt (0,pout);
5798 	  return(1);
5799 	}
5800       }
5801 
5802       /* --- var id */
5803 
5804       if ( (cmd = nxtwrd (cmd)) == NULL) {
5805 	gaprnt (0,"SET LATS WRITE error:  No arguments...\n");
5806 	return(1);
5807       }
5808 
5809       if ( intprs(cmd,&(i)) == NULL ) {
5810 	gaprnt (0,"SET LATS WRITE error:  missing or invalid VAR ID  given...\n");
5811 	return(1);
5812 
5813       } else {
5814 
5815 	pcm->glats.id_user_var=i;
5816 	if(pcm->glats.id_user_var<0 || (pcm->glats.id_user_var > pcm->glats.id_var) ) {
5817 	  sprintf(pout,
5818 		  "SET LATS WRITE error: VAR ID is outside the valid range of 1 - %d\n",
5819 		  pcm->glats.id_var);
5820 	  gaprnt (0,pout);
5821 	  return(1);
5822 	}
5823       }
5824 
5825       if ( (cmd = nxtwrd (cmd)) != NULL) {
5826 	valprs(cmd,&(v1)) ;
5827 	pcm->glats.varlev=(double)v1;
5828       } else {
5829 	pcm->glats.varlev=0.0;
5830       }
5831       pcm->glats.id_user_write=1;
5832 
5833       sprintf(pout,"LATS WRITE ID = %d\n",pcm->glats.id_user_write);
5834       gaprnt(2,pout);
5835 
5836     }
5837 
5838     else if (cmpwrd("timeoption",cmd)) {
5839       if ( (cmd = nxtwrd (cmd)) == NULL) {
5840 	gaprnt (0,"SET LATS TIMEOPTION error:  No arguments....\n");
5841 	return(1);
5842       } else if( cmpwrd("grid",cmd) ) pcm->glats.time_opt=0;
5843       else if( cmpwrd("dim_env",cmd) ) pcm->glats.time_opt=1;
5844       else if( cmpwrd("settime",cmd) ) pcm->glats.time_opt=2;
5845       else {
5846 	gaprnt (0,"SET LATS TIMEOPTION error:  invalid option ....\n");
5847 	return(1);
5848       }
5849     }
5850 
5851 /*-------------------
5852   lats_close interface
5853 -------------------*/
5854 
5855     else if(cmpwrd("close",cmd)) {
5856 
5857       if ( (cmd = nxtwrd (cmd)) == NULL) {
5858 	gaprnt (0,"SET LATS CLOSE error:  No arguments, specify a file id...\n");
5859 	return(1);
5860       }
5861 
5862       if ( intprs(cmd,&(i)) == NULL ) {
5863 	gaprnt (0,"SET LATS CLOSE error:  missing or invalid FILE ID given...\n");
5864 	return(1);
5865 
5866       } else {
5867 
5868 	if(i<0 || i>pcm->glats.id_file){
5869 	  sprintf(pout,
5870 		  "SET LATS CLOSE error: FILE ID is outside the valid range of 1 - %d\n",
5871 		  pcm->glats.id_file);
5872 	  gaprnt (0,pout);
5873 	  return(1);
5874 	}
5875 
5876       }
5877 
5878       rc=galats(pcm,6,i);  /* close the lats file */
5879 
5880     }
5881 
5882     else if(cmpwrd("reset",cmd)) {
5883       rc=galats(pcm,7,0);  /* reset the lats state  */
5884     }
5885 
5886 
5887     else {
5888 
5889       gaprnt (0,"SET LATS error:  Missing or invalid arguments:\n ");
5890       sprintf (pout,"for %s option\n",cmd);
5891       gaprnt (0,pout);
5892       return (1);
5893 
5894     }
5895 
5896   }
5897 #else
5898 
5899   else if (cmpwrd("lats",cmd)) {
5900     gaprnt (0,"LATS not installed in this GrADS build\n");
5901     return (1);
5902   }
5903 
5904 #endif
5905 
5906   else {
5907     gaprnt (0,"SET error: Invalid operand\n");
5908     gaprnt (0,"  Operand = ");
5909     gaprnt (0,cmd);
5910     gaprnt (0,"\n");
5911     return (1);
5912   }
5913   return (0);
5914 
5915 err:
5916   gaprnt (0,"SET error:  Missing or invalid arguments ");
5917   sprintf (pout,"for %s option\n",kwds[kwrd]);
5918   gaprnt (0,pout);
5919   return (1);
5920 
5921 errf:
5922   gaprnt (0,"SET Error:  No files open yet\n");
5923   return (1);
5924 
5925 xlerr:
5926   gaprnt (0,"SET XLOPTS(YLOPTS,CLOPTS) Syntax Error");
5927   gaprnt (0,"  Syntax is: SET XLOPTS color thickness size");
5928   return (1);
5929 
5930 xlerr2:
5931   gaprnt (0,"SET XLPOS(YLPOS) Syntax Error");
5932   gaprnt (0,"  Syntax is: SET XLPOS offset side\n");
5933   return (1);
5934 
5935 rbberr:
5936   gaprnt (0,"SET RBAND Syntax Error.");
5937   gaprnt (0,"  Syntax is: SET RBAND num type xlo ylo xhi yhi\n");
5938   return (1);
5939 
5940 pterr:
5941   gaprnt (0,"SET FILL Syntax Error.");
5942   gaprnt (0,"  Syntax is: SET FILL type [density] [angle]\n");
5943   return (1);
5944 
5945 drerr:
5946   gaprnt (0,"SET DROPMENU Syntax Error.");
5947   gaprnt (0,"  Syntax is: SET DROPMENU fc bc oc1 oc2 tfc tbc toc1 toc2\n");
5948   gaprnt (0,"      bfc bbc boc1 boc2 soc1 soc2 thick\n");
5949   return (1);
5950 }
5951 
5952 /* Open a data set by reading the descriptor file for that
5953    data set, and create a gafile structure.  Chain the gafile
5954    structure on to the list anchored in the gastat.                   */
5955 
gaopen(char * name,struct gacmn * pcm)5956 int  gaopen (char *name, struct gacmn *pcm) {
5957 struct gafile *pfi, *pfio;
5958 struct gavar *pvar;
5959 int size;
5960 int rc,i;
5961 
5962   pfi = getpfi();
5963   if (pfi==NULL) {
5964     gaprnt (0,"Memory Allocation Error: On File Open\n");
5965     return (1);
5966   }
5967 
5968 #if USEGADODS
5969   /* if name starts with http:// then it's a DODS url */
5970   if (*name=='h' && *(name+1)=='t' && *(name+2)=='t' &&
5971       *(name+3)=='p' && *(name+4)==':' && *(name+5)=='/' &&
5972       *(name+6)=='/' ) {    /* dodstn */
5973     rc = dodpfi (name,pfi); /* dodstn */
5974   } else {        /* dodstn */
5975     gaprnt (2,"Scanning description file:  ");
5976     gaprnt (2,name);
5977     gaprnt (2,"\n");
5978 
5979     rc = gaddes(name, pfi, 1);
5980   }          /* dodstn */
5981 #else
5982   gaprnt (2,"Scanning description file:  ");
5983   gaprnt (2,name);
5984   gaprnt (2,"\n");
5985 
5986   rc = gaddes(name, pfi, 1);
5987 #endif
5988 
5989   if (rc) {
5990     frepfi (pfi,0);
5991     return (rc);
5992   }
5993   pcm->fseq = pcm->fseq + 1;
5994   pfi->fseq = pcm->fseq;       /* opened files get unique sequence number */
5995 
5996   if (pfi->tmplat==0 && pfi->dhandle == -999) {
5997     if (pfi->ncflg) {
5998       if (pfi->ncflg==1) {    /* dtype netcdf */
5999 	rc = gaopnc(pfi,0,1);
6000 	if (rc) {
6001 	  frepfi (pfi,0);
6002 	  return (1);
6003 	}
6004       }
6005       else if (pfi->ncflg==2) {    /* dtype hdfsds */
6006 	rc = gaophdf(pfi,0,1);
6007 	if (rc) {
6008 	  frepfi (pfi,0);
6009 	  return (1);
6010 	}
6011       }
6012     } else if (!pfi->bufrflg) {
6013       pfi->infile = fopen (pfi->name, "rb");
6014       if (pfi->infile==NULL) {
6015 	gaprnt (0,"Open Error:  Can't open binary data file\n");
6016 	gaprnt (0,"  File name = ");
6017 	gaprnt (0,pfi->name);
6018 	gaprnt (0,"\n");
6019 	frepfi (pfi,0);
6020 	return (1);
6021       }
6022     }
6023   }
6024 
6025   if (pcm->pfi1==NULL) {
6026     pcm->pfi1 = pfi;
6027   } else {
6028     pfio = pcm->pfi1;
6029     while (pfio->pforw!=NULL) pfio = pfio->pforw;
6030     pfio->pforw = pfi;
6031   }
6032   pfi->pforw = NULL;
6033   pcm->fnum++;
6034 
6035   if (pcm->fnum==1) {pcm->pfid = pcm->pfi1; pcm->dfnum = 1;}
6036   sprintf (pout,"Data file %s is open as file %i\n",pfi->name,pcm->fnum);
6037   gaprnt (2,pout);
6038 
6039   /* If first file open, set up some default dimension
6040      ranges for the user */
6041 
6042   if (pcm->fnum==1) {
6043     if (pfi->type==2 || pfi->wrap ) gacmd ("set lon 0 360",pcm,0);
6044     else {
6045       sprintf (pout,"set x 1 %i",pfi->dnum[0]);
6046       gacmd (pout,pcm,0);
6047     }
6048     if (pfi->type==2) {
6049       gacmd ("set lat -90 90",pcm,0);
6050       gacmd ("set lev 500",pcm,0);
6051     } else {
6052       sprintf (pout,"set y 1 %i",pfi->dnum[1]);
6053       gacmd (pout,pcm,0);
6054 
6055 /*mf --- set z to max if x or y = 1 970729 mf*/
6056 
6057      if( (pfi->type==1 && pfi->dnum[2] >= 1)
6058 	 && ( (pfi->dnum[0] == 1) || (pfi->dnum[1] == 1) ) ) {
6059 	sprintf (pout,"set z 1 %i",pfi->dnum[2]);
6060 	gacmd (pout,pcm,0);
6061       } else {
6062 	gacmd ("set z 1",pcm,0);
6063       }
6064     }
6065     gacmd ("set t 1",pcm,0);
6066   }
6067 
6068   if (pfi->ppflag) {
6069     sprintf (pout,"Notice: Implied interpolation for file %s\n",name);
6070     gaprnt (1,pout);
6071     gaprnt (1," Interpolation will be performed on any data ");
6072     gaprnt (1,"displayed from this file\n");
6073   }
6074 
6075   return (0);
6076 }
6077 
getpst(struct gacmn * pcm)6078 struct gastat *getpst (struct gacmn *pcm) {
6079 struct gastat *pst;
6080 int size,i,vcnt,lflg,ll;
6081 
6082   size = sizeof(struct gastat);
6083   pst = (struct gastat *)malloc(size);
6084   pst->pfi1 = pcm->pfi1;
6085   pst->pfid = pcm->pfid;
6086   pst->fnum = pcm->fnum;
6087   pst->pclct = (struct gaclct **)&(pcm->clct);
6088   for (i=0;i<3;i++) {
6089     pst->dmin[i] = pcm->dmin[i];
6090     pst->dmax[i] = pcm->dmax[i];
6091   }
6092   pst->tmin = pcm->tmin;
6093   pst->tmax = pcm->tmax;
6094   pst->type = 1;
6095   pst->result.pgr = NULL;
6096   pst->pdf1 = pcm->pdf1;
6097 
6098   vcnt = 0;
6099   for (i=0; i<4; i++) if (pcm->vdim[i]) vcnt++;
6100   lflg = pcm->loopflg;
6101   if (vcnt>2) lflg = 1;
6102 
6103   ll=0;
6104   pst->idim = -1;
6105   pst->jdim = -1;
6106   if (pcm->vdim[0]) {
6107     if (pcm->dmin[0]>pcm->dmax[0]) {
6108       gaprnt (0,"Operation error:  Invalid dimension environment\n");
6109       sprintf (pout,"  Min longitude > max longitude: %g %g \n",
6110          pcm->dmin[0],pcm->dmax[0]);
6111       gaprnt (0,pout);
6112       return (NULL);
6113     }
6114     if (pcm->loopdim!=0||!lflg) {pst->idim=0;ll++;}
6115   }
6116   if (pcm->vdim[1]) {
6117     if (pcm->dmin[1]>pcm->dmax[1]) {
6118       gaprnt (0,"Operation error:  Invalid dimension environment\n");
6119       sprintf (pout,"  Min latitude > max latitude: %g %g \n",
6120          pcm->dmin[1],pcm->dmax[1]);
6121       gaprnt (0, pout);
6122       return (NULL);
6123     }
6124     if (pcm->loopdim!=1||!lflg) {
6125       if (ll>0) pst->jdim = 1;
6126       else pst->idim = 1;
6127       ll++;
6128     }
6129   }
6130   if (pcm->vdim[2]) {
6131     if (pcm->loopdim!=2||!lflg) {
6132       if (ll>0) pst->jdim = 2;
6133       else pst->idim = 2;
6134       ll++;
6135     }
6136   }
6137   if (pcm->vdim[3]) {
6138     if (pcm->loopdim!=3||!lflg) {
6139       if (ll>0) pst->jdim = 3;
6140       else pst->idim = 3;
6141       ll++;
6142     }
6143   }
6144 
6145   if (lflg && (vcnt==ll) ) {
6146     gaprnt (0,"Operation error:  Invalid dimension environment\n");
6147     gaprnt (0,"  Looping dimension does not vary\n");
6148     free (pst);
6149     return (NULL);
6150   }
6151   if (ll>2) {
6152     gaprnt (0,"Operation error:  Invalid dimension environment\n");
6153     gaprnt (0,"  Too many varying dimensions \n");
6154     free (pst);
6155     return (NULL);
6156   }
6157 
6158   return (pst);
6159 }
6160 
6161 
gaprnt(int level,char * msg)6162 void gaprnt (int level, char *msg) {
6163 int len;
6164 
6165   if (msgflg) {
6166     len = 0;
6167     while (*(msg+len)) len++;
6168     len++;
6169     msgnew = (struct msgbuf *)malloc(sizeof(struct msgbuf));
6170     if (msgnew==NULL) {
6171       msgflg = 0;
6172       printf ("Memory allocation error:  msg buffers\n");
6173       return;
6174     }
6175     msgnew->msg = (char *)malloc(len);
6176     if (msgnew->msg==NULL) {
6177       msgflg = 0;
6178       printf ("Memory allocation error:  msg buffers\n");
6179       free (msgnew);
6180       return;
6181     }
6182     len = 0;
6183     while (*(msg+len)) {
6184       *(msgnew->msg+len) = *(msg+len);
6185       len++;
6186     }
6187     *(msgnew->msg+len) = '\0';
6188     msgnew->len = len;
6189     msgnew->forw = NULL;
6190     if (msgstk==NULL) msgstk = msgnew;
6191     else msgcurr->forw = msgnew;
6192     msgcurr = msgnew;
6193   }
6194   if (!msgflg || level<2) {
6195     printf ("%s",msg);
6196   }
6197 }
6198 
prntgaattr(struct gafile * pfi,char * name,int hdrflg,int fnum)6199 int prntgaattr (struct gafile *pfi, char *name, int hdrflg, int fnum) {
6200   struct gaattr *attr;
6201 
6202   if (pfi->attr) {
6203     attr=pfi->attr;
6204     while (attr) {
6205       if (strcmp(attr->varname,name)==0) {
6206 	if (hdrflg) {
6207 	  sprintf(pout,"Descriptor Attributes for File %i : %s \n",fnum,pfi->title);
6208 	  gaprnt(2,pout);
6209 	  hdrflg=0;
6210 	}
6211 	sprintf(pout,"%s %s %s %s \n",attr->varname,attr->type,attr->name,attr->value);
6212 	gaprnt(2,pout);
6213       }
6214       attr=attr->next;
6215     }
6216   }
6217   return (hdrflg);
6218 }
6219 
6220 
6221 #ifdef MM_READLINE
6222 #if READLINE == 1
6223     /*
6224      * print history or repeat commands from history
6225      */
6226 
gahistory(char * cmd,char * com,struct gacmn * pcm)6227 int gahistory(char*cmd, char *com, struct gacmn *pcm) {
6228 
6229 #include <readline/history.h>
6230 
6231   extern int history_length;
6232   HIST_ENTRY **his_cmd;
6233   char *logfile;    /* history filename */
6234   int pformat;    /* format for printing */
6235   FILE *logid;      /* history file id */
6236   int i,is,ie,in=0; /* counter, history boundaries, # boundary argument */
6237   int nerror=0;     /* count error in repeated commands */
6238   int fileout=0;    /* flag: output to a file? */
6239   int retcod=0;     /* return code */
6240   int verbose=1;    /* Give feetback */
6241   int readhis=0;    /* Read history */
6242 
6243   is=1;
6244   ie=history_length;
6245   com=nxtwrd(com);
6246   if (com) {
6247     if ( cmpwrd("-s",com) ) { /* script style: quote commands */
6248       pformat=1;
6249       com=nxtwrd(com);
6250     } else if ( cmpwrd("-x",com) ) { /* exec style: no command number */
6251       pformat=2;
6252       com=nxtwrd(com);
6253     } else if ( cmpwrd("-q",com) ) {
6254       verbose=0;
6255       com=nxtwrd(com);
6256     } else if ( cmpwrd("-r",com) ) {
6257       readhis=1;
6258       com=nxtwrd(com);
6259     }
6260   }
6261 
6262   if(readhis) { /* read history from file */
6263     if(com)
6264       if(verbose)
6265 	printf("Adding content of file '%s' to history.\n",com);
6266       if(read_history(com)) {
6267 	printf("Error reading history from file '%s'.\n",com);
6268 	return(1);
6269       }
6270     return(0);
6271   }
6272   if (com) {
6273     /* try to read requested history boundaries */
6274     in=sscanf(com,"%d%d",&is,&ie);
6275     if (in==1) {
6276       if(is<0) {
6277 	is=history_length+1+is;
6278 	ie=is;
6279       } else if (is==0) {
6280 	is=history_search_pos("quit # (End of session: ", -1,history_length)+2;
6281 	ie=history_length;
6282       } else {
6283 	ie=is;
6284       }
6285     }
6286     if(in==2) {
6287       /* look backward if is, ie < 0 */
6288       if(is<0) is=history_length+1+is;
6289       if(ie<0) ie=history_length+1+ie;
6290       else if(ie==0) ie=history_length;
6291     }
6292   }
6293   if(is>ie) {i=is;is=ie;ie=i;}
6294   if (ie>history_length) ie=history_length;
6295   for (i=1;i<=in;i++) com=nxtwrd(com);
6296   if(com){
6297    if(verbose) printf("Writing command history to file '%s'.\n",com);
6298     fileout=1;
6299     logfile=strtok(com," ");
6300     logid=fopen(logfile,"a");
6301     /* write only commands from current session to file */
6302     if(in==0) is=history_search_pos("quit # (End of session: ", -1,history_length)+2;
6303     if(in<2) ie=history_length-1;
6304     if (strcmp(".gs",com+strlen(com)-3)==0) {
6305       pformat=1;
6306     } else if ( strcmp(".eps",com+strlen(com)-4)==0 ||
6307               strcmp(".EPS",com+strlen(com)-4)==0    ) {
6308       pformat=3;
6309 
6310     } else {
6311       pformat=2;
6312     }
6313   } else
6314     logid=stdout;
6315   his_cmd=history_list();
6316   if (cmpwrd("history",cmd) || cmpwrd("his",cmd) ) {
6317     /* print history */
6318     if (pformat==1)
6319       for(i=is;i<=ie;i++)
6320 	fprintf(logid,"'%s'\n",his_cmd[i-1]->line);
6321     else if(pformat==2)
6322       for(i=is;i<=ie;i++)
6323 	fprintf(logid,"%s\n",his_cmd[i-1]->line);
6324     else if(pformat==3)
6325       for(i=is;i<=ie;i++)
6326         fprintf(logid,"%%ga> %s\n",his_cmd[i-1]->line);
6327     else
6328       for(i=is;i<=ie;i++)
6329 	fprintf(logid,"  [%d] %s\n",i,his_cmd[i-1]->line);
6330     if (fileout) fclose(logid);
6331   } else {
6332     /* repeat history commands */
6333     if(ie==history_length) ie--;
6334     if(is==ie) {
6335       printf("  %s\n",his_cmd[is-1]->line);
6336     }
6337     else {
6338       printf("  Repeating commands\n    [%d] \"%s\" ...\n    [%d] \"%s\"\n",
6339 	   is, his_cmd[is-1]->line, ie,his_cmd[ie-1]->line);
6340     }
6341     for(i=is;i<=ie;i++) {
6342       if ( cmpwrdl(his_cmd[i-1]->line, "his") ||
6343            cmpwrdl(his_cmd[i-1]->line, "history") ||
6344            cmpwrdl(his_cmd[i-1]->line, "r") ||
6345            cmpwrdl(his_cmd[i-1]->line,"repeat" ) ) {
6346 	printf(" [%d] \"%s\" not repeated.\n",i,his_cmd[is-1]->line);
6347 	continue; /* skip repeat commands */
6348       }
6349       retcod = gacmd(his_cmd[i-1]->line,pcm,1);
6350       if (retcod) {
6351 	++nerror;
6352 	sprintf(pout,"  Repeat:  Error in command [%d] \"%s\".\n",i,his_cmd[i-1]->line);
6353 	gaprnt (0,pout);
6354 	if (nerror>50) {
6355 	  sprintf(pout,"  Repeat:  Too many errors (>50 ). Repeat stopped.\n");
6356 	  gaprnt (0,pout);
6357 	  retcod = 1;
6358 	  return (retcod);
6359 	}
6360       }
6361     }
6362   }
6363   return (retcod);
6364 }
6365 #endif /* READLINE == 1 */
6366 #endif /* MM_READLINE */
6367