/*****************************************************************************/
void graph_year (pchoice,qchoice)
int pchoice,
    qchoice; {
  fprintf(fyear, "%%!\n");
  fprintf(fyear, "72 72 translate\n");
  fprintf(fyear, "/AvantGarde-Demi findfont 12 scalefont setfont\n");
  fprintf(fyear, "0 0 moveto 500 0 rlineto stroke\n");
    fprintf(fyear, "0 0 moveto 0 504 rlineto stroke\n");
    fprintf(fyear, "-20 72 moveto (1.0) show\n");
  fprintf(fyear, "0 72 lineto stroke\n");
    fprintf(fyear, "-20 144 moveto (2.0) show\n");
  fprintf(fyear, "0 144 lineto stroke\n");
    fprintf(fyear, "-20 216 moveto (3.0) show\n");
  fprintf(fyear, "0 216 lineto stroke\n");
    fprintf(fyear, "-20 288 moveto (4.0) show\n");
  fprintf(fyear, "0 288 lineto stroke\n");
    fprintf(fyear, "-20 360 moveto (5.0) show\n");
  fprintf(fyear, "0 360 lineto stroke\n");
    fprintf(fyear, "-20 432 moveto (6.0) show\n");
  fprintf(fyear, "0 432 lineto stroke\n");
    fprintf(fyear, "-20 504 moveto (7.0) show\n");
  fprintf(fyear, "0 504 lineto stroke\n");
  fprintf(fyear, "50 -20 moveto (1) show\n");
  fprintf(fyear, "50 -7 moveto 50 0 lineto stroke\n");
  fprintf(fyear, "100 -20 moveto (2) show\n");
  fprintf(fyear, "100 -7 moveto 100 0 lineto stroke\n");
  fprintf(fyear, "150 -20 moveto (3) show\n");
  fprintf(fyear, "150 -7 moveto 150 0 lineto stroke\n");
  fprintf(fyear, "200 -20 moveto (4) show\n");
    fprintf(fyear, "200 -7 moveto 200 0 lineto stroke\n");
  fprintf(fyear, "250 -20 moveto (5) show\n");
  fprintf(fyear, "250 -7 moveto 250 0 lineto stroke\n");
  fprintf(fyear, "300 -20 moveto (6) show\n");
  fprintf(fyear, "300 -7 moveto 300 0 lineto stroke\n");
  fprintf(fyear, "350 -20 moveto (7) show\n");
  fprintf(fyear, "350 -7 moveto 350 0 lineto stroke\n");
  fprintf(fyear, "400 -20 moveto (8) show\n");
  fprintf(fyear, "400 -7 moveto 400 0 lineto stroke\n");
  fprintf(fyear, "450 -20 moveto (9) show\n");
  fprintf(fyear, "450 -7 moveto 450 0 lineto stroke\n");
  fprintf(fyear, "500 -20 moveto (10) show\n");
  fprintf(fyear, "500 -7 moveto 500 0 lineto stroke\n");
  fprintf(fyear, "-20 540 moveto (MEAN) show\n");
  fprintf(fyear, "470 -40 moveto (YEAR) show\n");
  fprintf(fyear, "180 666 moveto (Average Trend Curves) show\n");
  fprintf(fyear, "226 648 moveto (for the) show\n");
  fprintf(fyear, "176 630 moveto (Mean Annual Condition) show\n");
  fprintf(fyear, "0 594 moveto (No. of levels : %d) show\n", levels);
  fprintf(fyear, "370 594 moveto (Choice of P matrix : %d) show\n", pchoice);  
  fprintf(fyear, "370 576 moveto (Choice of Q matrix : %d) show\n", qchoice);
  fprintf(fyear, "0 576 moveto (I Value: %d,) show\n", ivalue);
  fprintf(fyear, "65 576 moveto (J Value: %d) show\n", jvalue);
  fprintf(fyear, "/AvantGarde-Demi findfont 6 scalefont setfont\n");
}
/*****************************************************************************/
void print_tree(start,z)        
int start[];
short z; {
  short k, optcount=0;
  static int oldlm;
  int temp;
  float mean, sdev, squared, sdev2, mean2, levmean=0.0;

  temp=start[z];
  fprintf(fdata, "\nLEVEL %d\n", z);
  while(temp<stop[z]) {
    if(node[temp]->optflag==1) {
      optcount++;
      mean=squared=sdev=0.0;
      for(k=0;k<8;k++) {
        mean+=(k*(node[temp]->value[k]));
        squared+=(k*k*(node[temp]->value[k]));
      }
      levmean+=mean;
      sdev=sqrt(squared-(mean*mean));
      mean2=72*mean;
      sdev2=sdev*144;
      fprintf(fsens, "%d %d moveto (%d%d) show\n", (int)sdev2,
      		   (int)mean2, node[temp]->Opti,node[temp]->Optj);
      /*fprintf(fyear, "%d %d moveto (X) show\n", z*50, (int)mean2); */
      fprintf(fdata, "Jmin=%9.6f, Opti=%d, Optj=%d, mean=%8.6f",
              node[temp]->Jmin, node[temp]->Opti, node[temp]->Optj, mean);
      if (constrain)
        fprintf(fdata, ", prob=%f\n",node[temp]->prob);
      else
        fprintf(fdata, "\n");
    }
    temp++;
  }
  if (constrain) {
    levmean=levmean/optcount;
    if (z)
      fprintf(fyear, "%d %d moveto %d %d lineto stroke\n", (z-1)*50+4, oldlm, 
              z*50, ((int)(72*levmean))+2);
    fprintf(fyear, "%d %d moveto (O) show\n", z*50, ((int)(72*levmean)));
    oldlm=((int)(72*levmean)) + 2;
  }
  fprintf(fsens, "showpage\n");
}
/*****************************************************************************/
void make_sens(start,z, pchoice, qchoice)
int start;
short z; {
  fprintf(fsens, "%%!\n");
  fprintf(fsens, "/NewCenturySchlbk-BoldItalic findfont 12 scalefont setfont\n");
  if (z==0)
    fprintf(fsens, "288 288 moveto (Jmin=%f) show\n", node[start]->Jmin);
  fprintf(fsens, "72 72 translate\n"); 
  fprintf(fsens, "/NewCenturySchlbk-BoldItalic findfont 12 scalefont setfont\n");
  fprintf(fsens, "0 0 moveto 432 0 rlineto stroke\n");
    fprintf(fsens, "0 0 moveto 0 504 rlineto stroke\n");
    fprintf(fsens, "-20 72 moveto (1.0) show\n");
  fprintf(fsens, "0 72 lineto stroke\n");
    fprintf(fsens, "-20 144 moveto (2.0) show\n");
  fprintf(fsens, "0 144 lineto stroke\n");
    fprintf(fsens, "-20 216 moveto (3.0) show\n");
  fprintf(fsens, "0 216 lineto stroke\n");
    fprintf(fsens, "-20 288 moveto (4.0) show\n");
  fprintf(fsens, "0 288 lineto stroke\n");
    fprintf(fsens, "-20 360 moveto (5.0) show\n");
  fprintf(fsens, "0 360 lineto stroke\n");
    fprintf(fsens, "-20 432 moveto (6.0) show\n");
  fprintf(fsens, "0 432 lineto stroke\n");
    fprintf(fsens, "-20 504 moveto (7.0) show\n");
  fprintf(fsens, "0 504 lineto stroke\n");
  fprintf(fsens, "140 -20 moveto (1.0) show\n");
  fprintf(fsens, "140 -7 moveto 140 0 lineto stroke\n");
  fprintf(fsens, "288 -20 moveto (2.0) show\n");
  fprintf(fsens, "288 -7 moveto 288 0 lineto stroke\n");
  fprintf(fsens, "432 -20 moveto (3.0) show\n");
  fprintf(fsens, "432 -7 moveto 432 0 lineto stroke\n");
  fprintf(fsens, "-20 530 moveto (MEAN) show\n");
  fprintf(fsens, "432 -40 moveto (SDEV) show\n");
  fprintf(fsens, "190 684 moveto (Optimum Policies) show\n");
  fprintf(fsens, "175 666 moveto (for all Possible States) show\n");
  fprintf(fsens, "185 648 moveto (of the Information) show\n");
  fprintf(fsens, "0 612 moveto (Initial State of the Information:) show\n");
  fprintf(fsens,
         "220 612 moveto (Pi[0]=%1.2f Pi[1]=%1.2f Pi[2]=%1.2f Pi[3]=%1.2f) show\n",
          node[start]->value[0], node[start]->value[1], node[start]->value[2], 
          node[start]->value[3]);
  fprintf(fsens, 
         "220 594 moveto (Pi[4]=%1.2f Pi[5]=%1.2f Pi[6]=%1.2f Pi[7]=%1.2f) show\n",
          node[start]->value[4], node[start]->value[5], node[start]->value[6],
          node[start]->value[7]);
  fprintf(fsens, "0 558 moveto (No. of levels : %d) show\n", levels); 
  fprintf(fsens, "0 576 moveto (Level %d) show\n", z);
  fprintf(fsens, "350 576 moveto (Choice of P matrix: %d) show\n", pchoice);
  fprintf(fsens, "350 558 moveto (Choice of Q matrix: %d) show\n", qchoice);
  fprintf(fsens, "/NewCenturySchlbk-BoldItalic findfont 6 scalefont setfont\n");
}
/*****************************************************************************/
void data_head(alpha, rnd, pchoice, qchoice, time, start)
float alpha, rnd;
int pchoice, qchoice;
int start[];
short time[3]; {
  FILE *finm, *finh, *ffact;
  short ftime[3],i;
  char b,c;

  clock(ftime);
  if((ftime[2]-=time[2])<0) {
    ftime[2]+=60;
    ftime[1]--;
  }
  if((ftime[1]-=time[1])<0) {
    ftime[1]+=60;
    ftime[0]--;
  }
  ftime[0]-=time[0];

  ffact=fopen("/mit/smmadana/graph.fact","w");

  fprintf(fdata,"Run Time: %d:%02d:%02d\n",ftime[0],ftime[1],ftime[2]);
  fprintf(ffact,"Run Time: %d:%02d:%02d\n",ftime[0],ftime[1],ftime[2]);

  system("machtype -c > .mach");
  finm=fopen("/mit/smmadana/.mach","r");
  fprintf(fdata,"Machine: ");
  fprintf(ffact,"Machine: ");
  b=getc(finm);
  while((c=getc(finm))!=EOF) {
    fprintf(fdata,"%c",b);
    fprintf(ffact,"%c",b);
    b=c;
  }
  fprintf(fdata," ");
  fprintf(ffact," ");
  fclose(finm);
  unlink("/mit/smmadana/.mach");

  system("hostname > .host");
  finh=fopen("/mit/smmadana/.host","r");
  while((c=getc(finh))!=EOF){
    fprintf(fdata,"%c",c);
    fprintf(ffact,"%c",c);
  }
  fprintf(fdata,"\n\n");
  fclose(finh);
  unlink("/mit/smmadana/.host");

  fprintf(ffact,"Nodes/Level: 0001 ");
  for(i=0;i<levels;i++) {
    if(i==11) fprintf(ffact,"            ");
    fprintf(ffact,"%04d ",nnod[i]);
  }
  fprintf(ffact,"\n");

  fprintf(fdata, "pchoice=%d, qchoice=%d, alpha=%1.4f, interval=%1.3f\n",
          pchoice, qchoice, alpha, rnd);
  fprintf(ffact, 
    "pchoice=%d, qchoice=%d, alpha=%1.4f, interval=%1.3f, levels=%d\n",
          pchoice, qchoice, alpha, rnd, levels);

  write_pi(node[start[0]]->value);
  fprintf(ffact," ------ ------ ------ ------ ------ ------ ------ ------ \n");
  for (i=0;i<8;i++)
    fprintf(ffact,"| %.2f ",node[start[0]]->value[i]);
  fprintf(ffact,"|\n");
  fprintf(ffact," ------ ------ ------ ------ ------ ------ ------ ------ \n");

  fprintf(ffact,"Jmin=%.3f\n",node[start[0]]->Jmin);
  fprintf(ffact,"\n\n");

  fclose(ffact);

  time[0]=ftime[0];
  time[1]=ftime[1];
  time[2]=ftime[2];
}

/*****************************************************************************/
void open_files() {

  fsens= fopen("/mit/smmadana/graphsense.PS", "w"); 
  fyear=fopen("/mit/smmadana/graphyear.PS", "w"); 
  fdata=fopen("/mit/smmadana/graph.data", "w");
}
/*****************************************************************************/
void close_files() {

  fclose(fsens);
  fprintf(fyear,"showpage\n");
  fclose(fyear);
  fclose(fdata);
}

 /*****************************************************************************/
 void write_pi(pi)
 float pi[]; {
 
 int i;
 
   for (i=0;i<8;i++) {
     fprintf(fdata,"pi[%d]=%.2f   ",i,pi[i]);
     if (i==3)
       fprintf(fdata,"\n");
   }
   fprintf(fdata,"\n\n");
 }
 /*****************************************************************************/
 
