/* This file contains the output routines.

   06/20/94 AW  Created.  Prints header into files that require it.
   06/22/94 AW  Routines for printing per timestep and after reproduction.
*/
#include <stdio.h>
#include <stdlib.h>
#include "types.h"

/* ----- prototype files ------------------------------------------------ */
#include "output.h"       /* prototypes file */

/* ----- constants ------------------------------------------------------ */

/* ----- ifdef constants ------------------------------------------------ */

/* ----- subroutines ---------------------------------------------------- */

/***** write_header *****/
/* parameters:	the_context
		output_file
   called by:	init_output_files()
   actions:	prints out header information including parameter
		values, start time, and run number.
*/
void write_header(Context *the_context, Output_file *output_file)
   {
   int i;
   FILE *fp;
   time_t now;
   char time_string[100];
 
#ifdef DEBUG
   printf("    ---in write_header---\n");
#endif

   time(&now);
   strftime(time_string, 99, " %m/%d/%y  %H:%M:%S ", localtime(&now));

   for (i=0; i<NUM_OUTPUT_FILES; i++)
      {
      if (output_file[i].header)
         {
         fp = output_file[i].fp;
         fprintf(fp, " Run #%d \t\t\tStart time: %s\n", the_context->run_num,
                 time_string);
         fprintf(fp, " Maximum number of time steps: %d\n", 
                 the_context->max_steps);
         fprintf(fp, " Mutation rate per bit: %lf\n", 
                 the_context->mutation_prob);
         }  /* if print header */
      }  /* for i */

#ifdef DEBUG
   printf("    ---end write_header---\n");
#endif

   }  /* write_header */

/***** write_each_timestep *****/
/* parameters:	the_context
		output_file
		stats
   called by:
   actions:	Called at the end of each timestep and
		writes to any files that need to be updated.
  		Prints one line per timestep in the following
 		order:	num_critters
			num_hosts
			num_parasites
			num_species
			avg_num_para_per_host
			avg_num_offspring
			avg_age
			avg_energy
			avg_size
		(Only to files that are enabled in types.h)
*/
void write_each_timestep(Context *the_context,
			 Output_file *output_file,
                         Stats *stats)
   {
#ifdef DEBUG
   printf("   ---in write_each_timestep---\n");
#endif

#ifdef PR_STATS
   fprintf(output_file[STATS_FILE].fp,
   	" %d %d %d %d %d %lf %lf %lf %lf %lf\n",
	the_context->time_step,
    	stats->num_critters,
    	stats->num_hosts,
    	stats->num_parasites,
    	stats->num_species,
    	stats->avg_num_para_per_host,
    	stats->avg_num_offspring,
    	stats->avg_age,
    	stats->avg_energy,
    	stats->avg_size);
#endif

#ifdef DEBUG
   printf("   ---end write_each_timestep---\n");
#endif
   }  /* write_each_timestep */

/***** write_geneology *****/
/* parameters:	output_file	structure of output files
		timestep	when the reproduction occurred
		critter_id	id number of new critter
		parent_id	id number of parent
		critter_species metabolism (=species) of offspring
		parent_species  metabolism (=species) of parent
   called by:
   actions:	Each time a new critter is born, this records
		the new critter and its parent's id numbers to
		a file (if the file is enabled in types.h).

		file format: 3 lines, followed by a blank line, per 
		offspring.  First line gives:
		timestep, offspring id, parent id
		Second and third lines give offspring,parent metabolisms:
		# reactions, reaction type(1=CREATION), first, second, type...
*/
void write_geneology(Output_file *output_file, int timestep,
                     int critter_id, int parent_id,
		     Reaction *critter_species,
		     Reaction *parent_species)
   {
     Reaction *r;
     int react_count;

#ifdef DEBUG
   printf("   ---in write_geneology---\n");
#endif

#ifdef PR_GENEOLOGY
     fprintf(output_file[GENE_FILE].fp,
	     "%d %d %d\n",                 
	     timestep, critter_id, parent_id);
     
     r = critter_species;
     react_count = 0;
     while(r!=NULL)
       {
	 react_count++;
	 r = r->next_reaction;
       }
     fprintf(output_file[GENE_FILE].fp,
	     "%d ",react_count);
     r = critter_species;
     while(r!=NULL)
       {
	 fprintf(output_file[GENE_FILE].fp,
		 "%d %d %d  ",(r->reaction_type == CREATION),
		 r->first_reactant,r->second_reactant);
	 r = r->next_reaction;
       }
     fprintf(output_file[GENE_FILE].fp,"\n");

     r = parent_species;
     react_count = 0;
     while(r!=NULL)
       {
	 react_count++;
	 r = r->next_reaction;
       }
     fprintf(output_file[GENE_FILE].fp,
	     "%d ",react_count);
     r = parent_species;
     while(r!=NULL)
       {
	 fprintf(output_file[GENE_FILE].fp,
		 "%d %d %d  ",(r->reaction_type == CREATION),
		 r->first_reactant,r->second_reactant);
	 r = r->next_reaction;
       }
     fprintf(output_file[GENE_FILE].fp,"\n\n");
       
   
#endif

#ifdef DEBUG
   printf("   ---end write_geneology---\n");
#endif
   }  /* write_geneology */





