home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / BEAGLE / BDIVERS.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  11KB  |  379 lines

  1. /* bdivers.c  22-3-92  diversity trace display for the Beagle Explorer */
  2. /*** Beagle Explorer: Version 3.1  Copyright (c) 1990, 1991, 1992  Tom Ray ***/
  3.  
  4. #include "beagle.h"
  5. #include "externb.h"
  6.  
  7. extern char   directory[], info_file[];
  8. extern float  xln, yln, xo, yo, xlpl, xxpl, xnpl, ylpl, yxpl, ynpl, shrink;
  9. extern FILE   *info;
  10. extern struct tracest  tr;
  11. extern unsigned  key;
  12. extern int     genotypes;
  13. extern double  ttime;
  14.  
  15. char    x_var[9], y_var[9], datafile[13], rangefile[13], *SimInput = NULL;
  16. HWND         whdi;
  17. TRANSACTION  *ptdi;
  18.  
  19. struct siz {
  20.     long   Time;
  21.     long   NumCell;
  22.     long   NumSize;
  23.     float  SizeDiv;
  24.     long   AgeSize;
  25.     } si;
  26.  
  27. struct gen {
  28.     long   NumGeno;
  29.     float  GenoDiv;
  30.     long   AgeGeno;
  31.     } ge;
  32.  
  33. int diverse(void)
  34. {   int  i;
  35.     static int  first = 1;
  36.     char  sshrink[5];
  37.     HWND  whins;
  38.  
  39.     if(first)
  40.     {   first = 0;
  41.         xnvf = -.05; xxvf = 1.05; ynvf = -.05; yxvf = 1.1; shrink = 1.0;
  42.         sprintf(x_var,"Time"); sprintf(y_var,"GenoDiv");
  43.         sprintf(datafile,"divdat.1"); sprintf(rangefile,"divrange");
  44.     }
  45.     sprintf(sshrink,"%g", shrink);
  46.     while(1) {
  47.     whdi = wigen(3,1,6,55,FRDOUBLE,"diversity");
  48.     ptdi = vpreload(6,0,NULLF,NULLF,NULLF,NULLF);
  49.     for(i = 0; i <= 5; i++) (*(ptdi->def))[i].use_ext = TRUE;
  50.     (*(ptdi->def))[1].refresh = (*(ptdi->def))[2].refresh =
  51.     (*(ptdi->def))[5].refresh = ON;
  52.     vdeffld(ptdi,0,STRING,0,1, "directory  =",-1,40,NULL,directory,NO);
  53.     vdeffld(ptdi,1,STRING,1,1, "x var      =",-1,8,NULL,x_var,NO);
  54.     vdeffld(ptdi,2,STRING,2,1, "y var      =",-1,8,NULL,y_var,NO);
  55.     vdeffld(ptdi,3,STRING,3,1, "data file  =",-1,12,NULL,datafile,NO);
  56.     vdeffld(ptdi,4,STRING,4,1, "range file =",-1,12,NULL,rangefile,NO);
  57.     vdeffld(ptdi,5,STRING,5,1, "shrink factor =",-1,4,NULL,sshrink,NO);
  58.     whins = wiinv(23,13,
  59.         "\x1e\x1f, or Enter to select; Esc to exit; Ctrl-Enter to run");
  60.     visible(whdi,YES,YES);
  61.     ptdi->fld_valfn = RunInfoChk;
  62.     ptdi->fld_setfn = dFieldSet;
  63.     if(DWSUCCESS == vread(ptdi,whdi,YES))
  64.     {   vdelete(whdi,NONE); vdelete(whins,NONE);
  65.         sscanf(sshrink,"%f", &shrink);
  66.         xln = yln = shrink;
  67.         xln *= 1.5;
  68.         xo = (1. - xln) / 2.;
  69.         yo = 1. - yln;
  70.         divers();
  71.     }
  72.     else
  73.     {   vdelete(whdi,NONE); vdelete(whins,NONE);
  74.         return 1;
  75.     }
  76.     }
  77.     vdelete(whins,NONE);
  78.     return 1;
  79. }
  80.  
  81. int dFieldSet(TRANSACTION  *tp)
  82. {   MENUHDR  *disp;
  83.     MENUITEM  *sel;
  84.  
  85.   if(tp->cur_fld == 1 || tp->cur_fld == 2)
  86.   {
  87.     key = tp->thiskey;
  88.     disp = MNUCreateHdr(POPUP);
  89.     disp->toprow = 7;
  90.     disp->topcol = 20;
  91.     disp->uattr  = 8;
  92.     disp->dattr  = 16;
  93.     disp->lattr  = 13;
  94.     MNUAddItem("Time", "elapsed time in instructions   ",'T',0,NULL,disp,NULLF);
  95.     MNUAddItem("NumCell","number of cells",'C',0,NULL,disp,NULLF);
  96.     MNUAddItem("NumSize","number of sizes",'I',0,NULL,disp,NULLF);
  97.     MNUAddItem("SizeDiv","size diversity",'S',0,NULL,disp,NULLF);
  98.     MNUAddItem("AgeSize","average age of sizes",'Z',0,NULL,disp,NULLF);
  99.     MNUAddItem("NumGeno","number of genotypes",'E',0,NULL,disp,NULLF);
  100.     MNUAddItem("GenoDiv","genotype diversity",'G',0,NULL,disp,NULLF);
  101.     MNUAddItem("AgeGeno","average age of genotypes",'N',0,NULL,disp,NULLF);
  102.       sel = MNUDoSelect(disp,NULLF);
  103.       if(sel)
  104.       {   tp->inp_get = dKeySim;
  105.           SimInput = sel->item0;
  106.       }
  107.   }
  108.   else tp->inp_get = (unsigned (*)()) vgetkey;
  109.   return FALSE;
  110. }
  111.  
  112. unsigned dKeySim(unsigned (*fp)(), TRANSACTION *tp)
  113. {   bool  ff = 0;
  114.  
  115.     if(ff) { ff = FALSE; return (unsigned) 11; } /* 11 == FIELD_ERASE */
  116.     if(*SimInput) return (unsigned) *SimInput++;
  117.     else
  118.     {   SimInput = NULL;
  119.         ff = TRUE;
  120.         tp->inp_get = (unsigned (*)()) vgetkey;
  121.         return key;
  122.     }
  123. }
  124.  
  125. int RunInfoChk(TRANSACTION  *tp)
  126. {   char  path[80], errmess[56];
  127.     int   success;
  128.     FILE  *inf;
  129.  
  130.     if(tp->cur_fld == 0)
  131.     {   getcwd(path,79);
  132.         success = chdir(directory);
  133.         chdir(path);
  134.         if(success)
  135.         {   sprintf(errmess,"directory not found: %s", directory);
  136.             wierror(11,10,errmess);
  137.             return FALSE;
  138.         }
  139.         return TRUE;
  140.     }
  141.     if(tp->cur_fld == 3)
  142.     {   sprintf(path,"%s\\%s", directory, datafile);
  143.         inf = fopen(path,"r");
  144.         if(inf == NULL)
  145.         {   sprintf(errmess,"file or path not found: %s", path);
  146.             wierror(11,10,errmess);
  147.             return FALSE;
  148.         }
  149.         fclose(inf);
  150.         return TRUE;
  151.     }
  152.     if(tp->cur_fld == 4)
  153.     {   sprintf(path,"%s\\%s", directory, rangefile);
  154.         inf = fopen(path,"r");
  155.         if(inf == NULL)
  156.         {   sprintf(errmess,"file or path not found: %s", path);
  157.             wierror(11,10,errmess);
  158.             return FALSE;
  159.         }
  160.         fclose(inf);
  161.     }
  162.     return TRUE;
  163. }
  164.  
  165. void TracestSetup(void)
  166. {   if(y_res == 480)
  167.     {   tr.csz = 16; tr.lbd = 10; tr.tbd = 10; tr.bti = 4; tr.tim = 4;
  168.     }
  169.     if(y_res == 200)
  170.     {   tr.csz = 8; tr.lbd = 10; tr.tbd = 5; tr.bti = 4; tr.tim = 4;
  171.     }
  172. }
  173.  
  174. void divers(void)
  175. {   struct point   p = { 0, 0 } ;
  176.     Uint    c;
  177.     FILE    *inf;
  178.     int     first = 1, linecolor, lettercolor;
  179.     int     x, y, binum = 1;
  180.     char    errmess[56], bifile[13];
  181.     Ulong   otime = 0, ctime;
  182.     char    format, data[100];
  183.  
  184.     ttime = 0;
  185.     tracest_setup();
  186.     if(scr)
  187.     {   setgraphmode(graphics_mode);
  188.         lettercolor = getmaxcolor();
  189.         if(lettercolor > 2)
  190.         {   linecolor = lettercolor - 1;
  191.         }
  192.         else if(lettercolor > 1)
  193.         {   linecolor = lettercolor - 1;
  194.         }
  195.         else linecolor = lettercolor;
  196.         if(y_res == 480) settextstyle(0,0,2);
  197.         if(y_res == 200) settextstyle(0,0,1);
  198.         sprintf(data,"T = 0");
  199. /*
  200.         outtextxy(tr.lbd, tr.tbd, data);
  201.         if(pop)
  202.         {   if(genotypes) sprintf(data,"Xp(%d%s)=%d",siz_x,lab_x,(int) xlpl);
  203.             else sprintf(data,"Xp(%i)=%i", siz_x, (int) xlpl);
  204.             outtextxy(tr.lbd + 165, tr.tbd, data);
  205.             if(genotypes) sprintf(data,"Yp(%d%s)=%d",siz_y,lab_y,(int) ylpl);
  206.             else sprintf(data,"Yp(%i)=%i", siz_y, (int) ylpl);
  207.             outtextxy(tr.lbd + 410, tr.tbd, data);
  208.         }
  209. */
  210.     }
  211.     setcolor(linecolor);
  212.     x = setxy(x_var);
  213.     y = setxy(y_var);
  214.     setranges();
  215.  
  216.     sscanf(datafile,"%[^.].%d", bifile, &binum);
  217.     sprintf(info_file,"%s/%s", directory, datafile);
  218.     inf = fopen(info_file,"rb");
  219.     if(inf == NULL)
  220.     {   sprintf(errmess,"data file not found: %s", datafile);
  221.         wierror(2,35,errmess);
  222.         return ;
  223.     }
  224.     fread(&format,sizeof(char),1,inf);
  225.     fread(&genotypes,sizeof(char),1,inf);
  226.     if(format)
  227.     {   fclose(inf);
  228.         inf = fopen(info_file,"r");
  229.         fgets(data,80,inf);
  230.         sscanf(data,"%*d %lf", &ttime);
  231.     }
  232.     else
  233.         fread(&ttime,sizeof(double),1,inf);
  234.     if(!genotypes && x > 5)
  235.     {   restorecrtmode();
  236.         sprintf(errmess,"can not use %s, %s does not include genotypes",
  237.             x_var, datafile);
  238.         wierror(2,15,errmess);
  239.         return;
  240.     }
  241.     if(!genotypes && y > 5)
  242.     {   restorecrtmode();
  243.         sprintf(errmess,"can not use %s, %s does not include genotypes",
  244.             y_var, datafile);
  245.         wierror(2,15,errmess);
  246.         return;
  247.     }
  248.     si.Time = 0.;
  249.     for(;;)
  250.     {   if(format)  /* ascii format */
  251.         {   if(fgets(data,80,inf) == NULL)
  252.             {   binum++;
  253.                 sprintf(info_file,"%s/%s.%d", directory, bifile, binum);
  254.                 fclose(inf);
  255.                 inf = fopen(info_file,"r");
  256.                 if(inf == NULL)
  257.                     break ;
  258.                 if(fgets(data,80,inf) == NULL)
  259.                     break ;
  260.                 sscanf(data,"%*d %lf", &ttime);
  261.                 if(fgets(data,80,inf) == NULL)
  262.                     break ;
  263.             }
  264.             if(genotypes)
  265.                 sscanf(data,"%lx%ld%ld%f%lx%ld%f%lx",
  266.                     &si.Time, &si.NumCell, &si.NumSize, &si.SizeDiv,
  267.                     &si.AgeSize, &ge.NumGeno, &ge.GenoDiv, &ge.AgeGeno);
  268.             else
  269.                 sscanf(data,"%lx%ld%ld%f%lx", &si.Time, &si.NumCell,
  270.                     &si.NumSize, &si.SizeDiv, &si.AgeSize);
  271.         }
  272.         else  /* binary format */
  273.         {   if(!fread(&si,sizeof(struct siz),1,inf))
  274.             {   binum++;
  275.                 sprintf(info_file,"%s/%s.%d", directory, bifile, binum);
  276.                 fclose(inf);
  277.                 inf = fopen(info_file,"rb");
  278.                 if(inf == NULL)
  279.                     break ;
  280.                 fread(data,sizeof(char),2,inf);
  281.                 fread(&ttime,sizeof(double),1,inf);
  282.                 if(!fread(&si,sizeof(struct siz),1,inf))
  283.                     break ;
  284.             }
  285.             if(genotypes)
  286.             fread(&ge,sizeof(struct gen),1,inf);
  287.         }
  288.         ttime += (double) si.Time;
  289.         ctime = (Ulong) si.Time / 1000000uL;
  290.         if(ctime > otime)
  291.         {   setfillstyle(SOLID_FILL,0);
  292.             setcolor(lettercolor);
  293.             sprintf(data,"%i", (int) ctime);
  294.             setcolor(linecolor);
  295.             sline(&p);
  296.             otime = ctime;
  297.         }
  298.         p.x = (float) putout(x); p.y = (float) putout(y);
  299.         p.x = (p.x - xnpl) / xlpl; p.y = (p.y - ynpl) / ylpl;
  300.         if(first) { sline(&p); first = 0; }
  301.         else cline(&p);
  302.         if(gfkbhit())
  303.         {   c = getkey();
  304.             if(c == 'p' || c == 'P') getkey();  /* P or p key */
  305.             if(c == ESC) break ;                /* Esc key */
  306.             if(c == 'g' || c == 'G')            /* G or g key */
  307.                 DumpScreen2Gif("image.gif",VGA,VGAHI,VGAHI,VGAHI);
  308.         }
  309.     }
  310. /*  fclose(inf); */
  311.     if(scr)
  312.     {   vbeep();
  313.         while(1)
  314.         {   if(gfkbhit())
  315.             {   c = getkey();
  316.                 if(c == ESC) break ;                /* Esc key */
  317.                 if(c == 'g' || c == 'G')            /* G or g key */
  318.                     DumpScreen2Gif("image.gif",VGA,VGAHI,VGAHI,VGAHI);
  319.             }
  320.         }
  321.         restorecrtmode();
  322.     }
  323. }
  324.  
  325. void setranges(void)
  326. {   FILE  *inf;
  327.     char  data[81], tmp[8];
  328.     
  329.     sprintf(data,"%s/%s", directory, rangefile);
  330.     inf = fopen(data,"r");
  331.     while(1)
  332.     {   if(fgets(data,80,inf) == NULL) break ;
  333.         sscanf(data,"%s", tmp);
  334.         if(!strcmp(tmp,x_var))
  335.             sscanf(data,"%*s%f%f", &xnpl, &xxpl);
  336.         if(!strcmp(tmp,y_var))
  337.             sscanf(data,"%*s%f%f", &ynpl, &yxpl);
  338.     }
  339.     fclose(inf);
  340.     xlpl = xxpl - xnpl; ylpl = yxpl - ynpl;
  341. }
  342.  
  343. int setxy(s)
  344. char  s[];
  345. {   if(!strcmp(s,"Time"))
  346.         return 1;
  347.     if(!strcmp(s,"NumCell"))
  348.         return 2;
  349.     if(!strcmp(s,"NumSize"))
  350.         return 3;
  351.     if(!strcmp(s,"SizeDiv"))
  352.         return 4;
  353.     if(!strcmp(s,"AgeSize"))
  354.         return 5;
  355.     if(!strcmp(s,"NumGeno"))
  356.         return 6;
  357.     if(!strcmp(s,"GenoDiv"))
  358.         return 7;
  359.     if(!strcmp(s,"AgeGeno"))
  360.         return 8;
  361.     return 0;
  362. }
  363.  
  364. double putout(s)
  365. int  s;
  366. {   switch(s)
  367.     {
  368.     case 1: return ttime;
  369.     case 2: return (double) si.NumCell;
  370.     case 3: return (double) si.NumSize;
  371.     case 4: return (double) si.SizeDiv;
  372.     case 5: return (double) si.AgeSize;
  373.     case 6: return (double) ge.NumGeno;
  374.     case 7: return (double) ge.GenoDiv;
  375.     case 8: return (double) ge.AgeGeno;
  376.     }
  377.     return 0;
  378. }
  379.