/* ---------------------------------------------------	*/
/* src_creel/species_estimated_total_caught.c		*/
/* ---------------------------------------------------	*/
/* Freely available software: see Appaserver.org	*/
/* ---------------------------------------------------	*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "timlib.h"
#include "piece.h"
#include "list.h"
#include "dictionary.h"
#include "appaserver_library.h"
#include "appaserver_error.h"
#include "document.h"
#include "process.h"
#include "html_table.h"
#include "hashtbl.h"
#include "appaserver_parameter_file.h"
#include "application_constants.h"
#include "environ.h"
#include "application.h"
#include "total_caught.h"

/* Enumerated Types */
/* ---------------- */
enum output_medium { output_medium_stdout, text_file, table };

/* Constants */
/* --------- */
#define ROWS_BETWEEN_HEADING			20
#define DEFAULT_OUTPUT_MEDIUM			text_file

#define OUTPUT_TEMPLATE		"%s/data/estimated_total_caught_%s_%d.csv"
#define FTP_PREPEND_TEMPLATE	"http://data/%s/estimated_total_caught_%s_%d.csv"
#define FTP_NONPREPEND_TEMPLATE "/data/estimated_total_caught_%s_%d.csv"
#define TOTALS_FILENAME_LABEL	"totals"
#define GRAND_FILENAME_LABEL	"grand"

/* Prototypes */
/* ---------- */
void output_total_sheet_total_row_text_file(
					FILE *output_file,
					TOTAL_ROW *total_row );

void output_month_sheet_total_row_text_file(
					FILE *output_file,
					MONTH_ROW *total_row );

void output_month_sheet_list_text_file(
					LIST *month_sheet_list,
					char *appaserver_mount_point,
					pid_t process_id,
					char *application_name,
					LIST *species_list,
					int begin_month );

void output_grand_total_sheet_text_file(double grand_areas_1_5_total_kept,
					double grand_areas_1_5_total_released,
					double grand_areas_1_5_total_caught,
					double grand_areas_1_6_total_kept,
					double grand_areas_1_6_total_released,
					double grand_areas_1_6_total_caught,
					int year,
					char *appaserver_mount_point,
					pid_t process_id,
					char *application_name,
					int begin_month,
					int end_month,
					LIST *species_list );

void output_total_sheet_text_file(	TOTAL_SHEET *total_sheet,
					int year,
					char *appaserver_mount_point,
					pid_t process_id,
					char *application_name,
					LIST *species_list );

void output_month_sheet_text_file(	FILE *output_file,
					MONTH_SHEET *month_sheet );

void output_text_file(		ESTIMATED_TOTAL_CAUGHT_OUTPUT *output,
				int begin_month,
				int end_month,
				char *application_name,
				char *process_name,
				char *appaserver_mount_point,
				LIST *species_list,
				int year );

enum output_medium get_output_medium(
				char *output_medium_string );

int main( int argc, char **argv )
{
	char *application_name;
	char *process_name;
	DOCUMENT *document;
	APPASERVER_PARAMETER_FILE *appaserver_parameter_file;
	char *database_string = {0};
	enum output_medium output_medium;
	char *begin_month_string;
	char *end_month_string;
	int begin_month_integer;
	int end_month_integer;
	char *year_string;
	char *family_list_string;
	char *genus_list_string;
	char *species_list_string;
	char *output_medium_string;
	ESTIMATED_TOTAL_CAUGHT *estimated_total_caught;

	if ( argc != 10 )
	{
		fprintf( stderr,
"Usage: %s application process_name begin_month end_month year family genus species output_medium\n",
			 argv[ 0 ] );
		exit ( 1 );
	}

	application_name = argv[ 1 ];
	process_name = argv[ 2 ];

	begin_month_string = argv[ 3 ];
	begin_month_integer = atoi( begin_month_string );

	end_month_string = argv[ 4 ];
	end_month_integer = atoi( end_month_string );

	if ( !*end_month_string
	||   strcmp( end_month_string, "end_month" ) == 0 )
	{
		end_month_string = begin_month_string;
	}

	year_string = argv[ 5 ];
	family_list_string = argv[ 6 ];
	genus_list_string = argv[ 7 ];
	species_list_string = argv[ 8 ];
	output_medium_string = argv[ 9 ];

	if ( timlib_parse_database_string(	&database_string,
						application_name ) )
	{
		environ_set_environment(
			APPASERVER_DATABASE_ENVIRONMENT_VARIABLE,
			database_string );
	}

	appaserver_error_starting_argv_append_file(
				argc,
				argv,
				application_name );

	add_dot_to_path();
	add_utility_to_path();
	add_src_appaserver_to_path();
	add_relative_source_directory_to_path( application_name );

	appaserver_parameter_file = new_appaserver_parameter_file();

	if ( begin_month_integer < 1
	||   end_month_integer > 12
	||   begin_month_integer > end_month_integer )
	{
		document_quick_output_body(
				application_name,
				appaserver_parameter_file->
					appaserver_mount_point );
		printf( "<h3>Error: invalid month integers.</h3>\n" );
		document_close();
		exit( 0 );
	}

	output_medium = get_output_medium( output_medium_string );

	estimated_total_caught = estimated_total_caught_new(
					begin_month_integer,
					end_month_integer,
					atoi( year_string ),
					application_name );

	estimated_total_caught->input->species_list =
		total_caught_get_species_list(
			application_name,
			family_list_string,
			genus_list_string,
			species_list_string );

	if ( !list_length( estimated_total_caught->input->species_list ) )
	{
		document_quick_output_body(
				application_name,
				appaserver_parameter_file->
					appaserver_mount_point );
		printf( "<h3>Error: cannot get species list.</h3>\n" );
		document_close();
		exit( 0 );
	}

	estimated_total_caught->input->fishing_trip_hash_table =
		total_caught_get_fishing_trip_hash_table(
			&estimated_total_caught->
				input->fishing_trip_list,
			application_name,
			estimated_total_caught->input->begin_month,
			estimated_total_caught->input->end_month,
			estimated_total_caught->input->year );

	total_caught_populate_catch_list(
		estimated_total_caught->input->fishing_trip_hash_table,
		estimated_total_caught->input->species_list,
			application_name,
			estimated_total_caught->input->begin_month,
			estimated_total_caught->input->end_month,
			estimated_total_caught->input->year );

	estimated_total_caught->output =
		total_caught_get_output(
			estimated_total_caught->input->begin_month,
			estimated_total_caught->input->end_month,
			estimated_total_caught->input->year,
			estimated_total_caught->input->species_list,
			estimated_total_caught->input->
				trailer_count_dictionary,
			estimated_total_caught->input->
				fishing_trip_list,
			estimated_total_caught->input->
				weekend_creel_census_dictionary );

	if ( output_medium == table )
	{
		document = document_new( "", application_name );
		document_set_output_content_type( document );
	
		document_output_head(
				document->application_name,
				document->title,
				document->output_content_type,
				appaserver_parameter_file->
					appaserver_mount_point,
				document->javascript_module_list,
				document->stylesheet_filename,
				application_get_relative_source_directory(
					application_name ),
				0 /* not with_dynarch_menu */ );
	
		document_output_body(
				document->application_name,
				document->onload_control_string );

		document_close();
	}
	else
	if ( output_medium == text_file )
	{
		document = document_new( "", application_name );
		document_set_output_content_type( document );
	
		document_output_head(
				document->application_name,
				document->title,
				document->output_content_type,
				appaserver_parameter_file->
					appaserver_mount_point,
				document->javascript_module_list,
				document->stylesheet_filename,
				application_get_relative_source_directory(
					application_name ),
				0 /* not with_dynarch_menu */ );
	
		document_output_body(
				document->application_name,
				document->onload_control_string );

		output_text_file(
				estimated_total_caught->output,
				estimated_total_caught->input->begin_month,
				estimated_total_caught->input->end_month,
				application_name,
				process_name,
				appaserver_parameter_file->
					appaserver_mount_point,
				estimated_total_caught->input->species_list,
				estimated_total_caught->input->year );

		document_close();
	}
	else
	if ( output_medium == output_medium_stdout )
	{
	}

	process_increment_execution_count(
				application_name,
				process_name,
				appaserver_parameter_file_get_dbms() );
	return 0;
} /* main() */

enum output_medium get_output_medium( char *output_medium_string )
{
	return DEFAULT_OUTPUT_MEDIUM;

	if ( strcmp( output_medium_string, "output_medium" ) == 0
	||   !*output_medium_string )
	{
		return DEFAULT_OUTPUT_MEDIUM;
	}
	else
	if ( strcmp( output_medium_string, "text_file" ) == 0
	||   strcmp( output_medium_string, "transmit" ) == 0 )
	{
		return text_file;
	}
	else
	if ( strcmp( output_medium_string, "table" ) == 0 )
	{
		return table;
	}
	else
	if ( strcmp( output_medium_string, "stdout" ) == 0 )
	{
		return output_medium_stdout;
	}
	else
	{
		return DEFAULT_OUTPUT_MEDIUM;
	}
} /* get_output_medium_string() */

void output_text_file(	ESTIMATED_TOTAL_CAUGHT_OUTPUT *output,
			int begin_month,
			int end_month,
			char *application_name,
			char *process_name,
			char *appaserver_mount_point,
			LIST *species_list,
			int year )
{
	pid_t process_id = getpid();
	char buffer[ 128 ];

	printf( "<h1>%s<br></h1>\n",
		format_initial_capital( buffer, process_name ) );
	printf( "<h2>\n" );
	fflush( stdout );
	system( "date '+%x %H:%M'" );
	fflush( stdout );
	printf( "</h2>\n" );

	output_grand_total_sheet_text_file(
					output->grand_areas_1_5_total_kept,
					output->grand_areas_1_5_total_released,
					output->grand_areas_1_5_total_caught,
					output->grand_areas_1_6_total_kept,
					output->grand_areas_1_6_total_released,
					output->grand_areas_1_6_total_caught,
					year,
					appaserver_mount_point,
					process_id,
					application_name,
					begin_month,
					end_month,
					species_list );

	output_total_sheet_text_file(	output->total_sheet,
					year,
					appaserver_mount_point,
					process_id,
					application_name,
					species_list );

	output_month_sheet_list_text_file(
					output->month_sheet_list,
					appaserver_mount_point,
					process_id,
					application_name,
					species_list,
					begin_month );

	printf(
"<br><a href=\"/src_creel/species_estimated_total_caught_algorithm.pdf\">Press to view the algorithm.</a><br>\n" );

} /* output_text_file() */

void output_month_sheet_list_text_file(
					LIST *month_sheet_list,
					char *appaserver_mount_point,
					pid_t process_id,
					char *application_name,
					LIST *species_list,
					int begin_month )
{
	MONTH_SHEET *month_sheet;
	char output_filename[ 256 ];
	char ftp_filename[ 256 ];
	char ftp_prompt[ 256 ];
	char *month_abbreviation;
	FILE *output_file;
	SPECIES *species;

	if ( !list_rewind( month_sheet_list ) ) return;

	do {
		month_sheet = list_get_pointer( month_sheet_list );

		month_abbreviation =
			timlib_get_three_character_month_string(
				month_sheet->month - 1 );

		sprintf( output_filename, 
			 OUTPUT_TEMPLATE,
			 appaserver_mount_point,
			 month_abbreviation, 
			 process_id );

		if ( ! ( output_file = fopen( output_filename, "w" ) ) )
		{
			printf( "<H2>ERROR: Cannot open output file %s\n",
				output_filename );
			document_close();
			exit( 1 );
		}

		if ( application_get_prepend_http_protocol_yn(
					application_name ) == 'y' )
		{
			sprintf(ftp_filename, 
			 	FTP_PREPEND_TEMPLATE, 
			 	appaserver_library_get_server_address(),
			 	month_abbreviation,
			 	process_id );
		}
		else
		{
			sprintf(ftp_filename, 
			 	FTP_NONPREPEND_TEMPLATE, 
			 	month_abbreviation,
			 	process_id );
		}

		fprintf( output_file,
			 "%s,Trailers,,,Vessels,,,Trips,,,",
			 timlib_integer2full_month( begin_month++ ) );

		list_rewind( species_list );
		do {
			species = list_get_pointer( species_list );
			fprintf( output_file,
			",Sample Areas 1-5,,,Sample Areas 1-6,," );
			fprintf( output_file,
			",Estimated Areas 1-5,,,Estimated Areas 1-6,," );
		} while( list_next( species_list ) );

		fprintf( output_file, "\n" );

		fprintf( output_file,
"Day,Florida Bay,Whitewater Bay,Total,Area 1-5,Area 6,Area 1-6,Flamingo Fishing,Flamingo Boating,Park Fishing,Park Boating" );

		list_rewind( species_list );
		do {
			species = list_get_pointer( species_list );

			/* Output actual */
			/* ------------- */
			fprintf( output_file,
		",%d Kept,%d Released,%d Caught,%d Kept,%d Released,%d Caught",
				 species->florida_state_code,
				 species->florida_state_code,
				 species->florida_state_code,
				 species->florida_state_code,
				 species->florida_state_code,
				 species->florida_state_code );

			/* Output estimated total */
			/* ---------------------- */
			fprintf( output_file,
		",%d Kept,%d Released,%d Caught,%d Kept,%d Released,%d Caught",
				 species->florida_state_code,
				 species->florida_state_code,
				 species->florida_state_code,
				 species->florida_state_code,
				 species->florida_state_code,
				 species->florida_state_code );

		} while( list_next( species_list ) );

		fprintf( output_file, "\n" );

		output_month_sheet_text_file(	output_file,
						month_sheet );

		fclose( output_file );

		sprintf( ftp_prompt,
	"%s: &lt;Left Click&gt; to view or &lt;Right Click&gt; to save.",
			 month_abbreviation );

		appaserver_library_output_ftp_prompt(
			ftp_filename,
			ftp_prompt,
			(char *)0 /* target */,
			(char *)0 /* application_type */ );

		printf( "<br>\n" );

	} while( list_next( month_sheet_list ) );

} /* output_month_sheet_list_text_file() */

void output_month_sheet_text_file(	FILE *output_file,
					MONTH_SHEET *month_sheet )
{
	MONTH_ROW *month_row;
	CATCH_AREA *catch_area;

	if ( !list_rewind( month_sheet->row_list ) ) return;

	do {
		month_row = list_get_pointer( month_sheet->row_list );

		fprintf( output_file,
"%d,%d,%d,%d,%.0lf,%.0lf,%.0lf,%.1lf,%.1lf,%.1lf,%.1lf",
			 month_row->day,
			 month_row->vessel.florida_bay_trailer_count_day,
			 month_row->vessel.whitewater_bay_trailer_count_day,
			 month_row->vessel.total_trailer_count_day,
			 month_row->vessel.areas_1_5_vessels,
			 month_row->vessel.area_6_vessels,
			 month_row->vessel.areas_1_6_vessels,
			 month_row->fishing_trip.flamingo_fishing_trips_day,
			 month_row->fishing_trip.flamingo_boating_trips_day,
			 month_row->fishing_trip.park_fishing_trips_day,
			 month_row->fishing_trip.park_boating_trips_day );

		if ( list_rewind( month_row->catch_area_list ) )
		{
			do {
				catch_area = list_get_pointer(
						month_row->catch_area_list );

				fprintf( output_file,
				 ",%.1lf,%.1lf,%.1lf,%.1lf,%.1lf,%.1lf",
				 catch_area->
					areas_1_5_sample_kept,
				 catch_area->
					areas_1_5_sample_released,
				 catch_area->
					areas_1_5_sample_caught,
				 catch_area->
					areas_1_6_sample_kept,
				 catch_area->
					areas_1_6_sample_released,
				 catch_area->
					areas_1_6_sample_caught );

				fprintf( output_file,
				 ",%.2lf,%.2lf,%.2lf,%.2lf,%.2lf,%.2lf",
				 catch_area->
					areas_1_5_estimated_total_kept,
				 catch_area->
					areas_1_5_estimated_total_released,
				 catch_area->
					areas_1_5_estimated_total_caught,
				 catch_area->
					areas_1_6_estimated_total_kept,
				 catch_area->
					areas_1_6_estimated_total_released,
				 catch_area->
					areas_1_6_estimated_total_caught );

			} while( list_next( month_row->catch_area_list ) );
		}

		fprintf( output_file, "\n" );

	} while( list_next( month_sheet->row_list ) );

	output_month_sheet_total_row_text_file(
				output_file,
				month_sheet->total_row );

} /* output_month_sheet_text_file() */

void output_month_sheet_total_row_text_file(
				FILE *output_file,
				MONTH_ROW *total_row )
{
	CATCH_AREA *catch_area;

	fprintf( output_file,
	 "Total,%d,%d,%d,%.0lf,%.0lf,%.0lf,%.0lf,%.0lf,%.0lf,%.0lf",
	 total_row->vessel.florida_bay_trailer_count_day,
	 total_row->vessel.whitewater_bay_trailer_count_day,
	 total_row->vessel.total_trailer_count_day,
	 total_row->vessel.areas_1_5_vessels,
	 total_row->vessel.area_6_vessels,
	 total_row->vessel.areas_1_6_vessels,
	 total_row->fishing_trip.flamingo_fishing_trips_day,
	 total_row->fishing_trip.flamingo_boating_trips_day,
	 total_row->fishing_trip.park_fishing_trips_day,
	 total_row->fishing_trip.park_boating_trips_day );

	if ( !list_rewind( total_row->catch_area_list ) )
	{
		fprintf(stderr,
			"ERROR in %s/%s()/%d: empty species area list.\n",
			__FILE__,
			__FUNCTION__,
			__LINE__ );
		exit( 1 );
	}

	do {
		catch_area =
			list_get_pointer(
				total_row->catch_area_list );

		fprintf( output_file,
		 ",,,,,,,%.0lf,%.0lf,%.0lf,%.0lf,%.0lf,%.0lf",
		 catch_area->
			areas_1_5_estimated_total_kept,
		 catch_area->
			areas_1_5_estimated_total_released,
		 catch_area->
			areas_1_5_estimated_total_caught,
		 catch_area->
			areas_1_6_estimated_total_kept,
		 catch_area->
			areas_1_6_estimated_total_released,
		 catch_area->
			areas_1_6_estimated_total_caught );

	} while( list_next( total_row->catch_area_list ) );
	fprintf( output_file, "\n" );

} /* output_month_sheet_total_row_text_file() */

void output_total_sheet_text_file(	TOTAL_SHEET *total_sheet,
					int year,
					char *appaserver_mount_point,
					pid_t process_id,
					char *application_name,
					LIST *species_list )

{
	TOTAL_ROW *total_row;
	CATCH_AREA *catch_area;
	SPECIES *species;
	char output_filename[ 256 ];
	char ftp_filename[ 256 ];
	char ftp_prompt[ 256 ];
	FILE *output_file;
	char buffer[ 256 ];

	sprintf( output_filename, 
		 OUTPUT_TEMPLATE,
		 appaserver_mount_point,
		 TOTALS_FILENAME_LABEL,
		 process_id );

	if ( ! ( output_file = fopen( output_filename, "w" ) ) )
	{
		printf( "<H2>ERROR: Cannot open output file %s\n",
			output_filename );
		document_close();
		exit( 1 );
	}

	if ( application_get_prepend_http_protocol_yn(
				application_name ) == 'y' )
	{
		sprintf(ftp_filename, 
		 	FTP_PREPEND_TEMPLATE, 
		 	appaserver_library_get_server_address(),
		 	TOTALS_FILENAME_LABEL,
		 	process_id );
	}
	else
	{
		sprintf(ftp_filename, 
		 	FTP_NONPREPEND_TEMPLATE, 
		 	TOTALS_FILENAME_LABEL,
		 	process_id );
	}

	fprintf( output_file,
		 ",Trailers,,,Vessels,,,Trips,,," );

	list_rewind( species_list );
	do {
		species = list_get_pointer( species_list );
		fprintf( output_file,
		",Estimated Areas 1-5,,,Estimated Areas 1-6,," );
	} while( list_next( species_list ) );

	fprintf( output_file, "\n" );

	fprintf( output_file,
"%d,Florida Bay,Whitewater Bay,Total,Area 1-5,Area 6,Area 1-6,Flamingo Fishing,Flamingo Boating,Park Fishing,Park Boating",
		 year );

	list_rewind( species_list );
	do {
		species = list_get_pointer( species_list );

		/* Output estimated total */
		/* ---------------------- */
		fprintf( output_file,
		",%d Kept,%d Released,%d Caught,%d Kept,%d Released,%d Caught",
			 species->florida_state_code,
			 species->florida_state_code,
			 species->florida_state_code,
			 species->florida_state_code,
			 species->florida_state_code,
			 species->florida_state_code );

	} while( list_next( species_list ) );

	fprintf( output_file, "\n" );

	if ( !list_rewind( total_sheet->total_row_list ) ) return;

	do {
		total_row = list_get_pointer( total_sheet->total_row_list );

		fprintf( output_file,
			 "%s,%d,%d,%d,%.1lf,%.1lf,%.1lf,%1lf,%1lf,%1lf,%1lf",
			 total_row->month,
			 total_row->florida_bay_trailer_count_day,
			 total_row->whitewater_bay_trailer_count_day,
			 total_row->total_trailer_count_day,
			 total_row->areas_1_5_vessels,
			 total_row->area_6_vessels,
			 total_row->areas_1_6_vessels,
			 total_row->flamingo_fishing_trips_day,
			 total_row->flamingo_boating_trips_day,
			 total_row->park_fishing_trips_day,
			 total_row->park_boating_trips_day );

		if ( list_rewind( total_row->catch_area_list ) )
		{
			do {
				catch_area = list_get_pointer(
						total_row->catch_area_list );

				fprintf( output_file,
				 ",%.1lf,%.1lf,%.1lf,%.1lf,%.1lf,%.1lf",
				 catch_area->
					areas_1_5_estimated_total_kept,
				 catch_area->
					areas_1_5_estimated_total_released,
				 catch_area->
					areas_1_5_estimated_total_caught,
				 catch_area->
					areas_1_6_estimated_total_kept,
				 catch_area->
					areas_1_6_estimated_total_released,
				 catch_area->
					areas_1_6_estimated_total_caught );

			} while( list_next( total_row->catch_area_list ) );
		}

		fprintf( output_file, "\n" );
	} while( list_next( total_sheet->total_row_list ) );

	output_total_sheet_total_row_text_file(
					output_file,
					total_sheet->total_row );

	fclose( output_file );

	sprintf( ftp_prompt,
	"%s: &lt;Left Click&gt; to view or &lt;Right Click&gt; to save.",
		 format_initial_capital( buffer,
		 			 TOTALS_FILENAME_LABEL ) );

	appaserver_library_output_ftp_prompt(
		ftp_filename,
		ftp_prompt,
		(char *)0 /* target */,
		(char *)0 /* application_type */ );

	printf( "<br>\n" );

} /* output_total_sheet_text_file() */

void output_total_sheet_total_row_text_file(
					FILE *output_file,
					TOTAL_ROW *total_row )
{
	CATCH_AREA *catch_area;

	fprintf( output_file,
	 "Total,%d,%d,%d,%.1lf,%.1lf,%.1lf,%.1lf,%.1lf,%.1lf,%.1lf",
	 total_row->florida_bay_trailer_count_day,
	 total_row->whitewater_bay_trailer_count_day,
	 total_row->total_trailer_count_day,
	 total_row->areas_1_5_vessels,
	 total_row->area_6_vessels,
	 total_row->areas_1_6_vessels,
	 total_row->flamingo_fishing_trips_day,
	 total_row->flamingo_boating_trips_day,
	 total_row->park_fishing_trips_day,
	 total_row->park_boating_trips_day );

	if ( !list_rewind( total_row->catch_area_list ) )
	{
		fprintf(stderr,
			"ERROR in %s/%s()/%d: empty species area list.\n",
			__FILE__,
			__FUNCTION__,
			__LINE__ );
		exit( 1 );
	}

	do {
		catch_area =
			list_get_pointer(
				total_row->catch_area_list );

		fprintf( output_file,
		 ",%.1lf,%.1lf,%.1lf,%.1lf,%.1lf,%.1lf",
		 catch_area->
			areas_1_5_estimated_total_kept,
		 catch_area->
			areas_1_5_estimated_total_released,
		 catch_area->
			areas_1_5_estimated_total_caught,
		 catch_area->
			areas_1_6_estimated_total_kept,
		 catch_area->
			areas_1_6_estimated_total_released,
		 catch_area->
			areas_1_6_estimated_total_caught );

	} while( list_next( total_row->catch_area_list ) );
	fprintf( output_file, "\n" );

} /* output_total_sheet_total_row_text_file() */

void output_grand_total_sheet_text_file(double grand_areas_1_5_total_kept,
					double grand_areas_1_5_total_released,
					double grand_areas_1_5_total_caught,
					double grand_areas_1_6_total_kept,
					double grand_areas_1_6_total_released,
					double grand_areas_1_6_total_caught,
					int year,
					char *appaserver_mount_point,
					pid_t process_id,
					char *application_name,
					int begin_month,
					int end_month,
					LIST *species_list )
{
	char output_filename[ 256 ];
	char ftp_filename[ 256 ];
	char ftp_prompt[ 256 ];
	FILE *output_file;
	SPECIES *species;
	char buffer[ 256 ];

	if ( !list_rewind( species_list ) ) return;

	sprintf( output_filename, 
		 OUTPUT_TEMPLATE,
		 appaserver_mount_point,
		 GRAND_FILENAME_LABEL,
		 process_id );

	if ( ! ( output_file = fopen( output_filename, "w" ) ) )
	{
		printf( "<H2>ERROR: Cannot open output file %s\n",
			output_filename );
		document_close();
		exit( 1 );
	}

	if ( application_get_prepend_http_protocol_yn(
				application_name ) == 'y' )
	{
		sprintf(ftp_filename, 
		 	FTP_PREPEND_TEMPLATE, 
		 	appaserver_library_get_server_address(),
		 	GRAND_FILENAME_LABEL,
		 	process_id );
	}
	else
	{
		sprintf(ftp_filename, 
		 	FTP_NONPREPEND_TEMPLATE, 
		 	GRAND_FILENAME_LABEL,
		 	process_id );
	}

	fprintf( output_file,
		 "Year,%d\n",
		 year );

	fprintf( output_file,
		 "Begin Month,%s\n",
		 timlib_integer2full_month( begin_month ) );

	fprintf( output_file,
		 "End Month,%s\n",
		 timlib_integer2full_month( end_month ) );

	fprintf( output_file,
		 "Species" );

	do {
		species = list_get_pointer( species_list );

/*
		fprintf( output_file,
			 ",%s/%s/%s %d",
			 species->family,
			 species->genus,
			 species->species_name,
			 species->florida_state_code );
*/
		fprintf( output_file,
			 ",%d",
			 species->florida_state_code );
	} while( list_next( species_list ) );

	fprintf( output_file, "\n" );

	fprintf( output_file,
		 "Areas 1-5 Total Kept,%.1lf\n",
		 grand_areas_1_5_total_kept );

	fprintf( output_file,
		 "Areas 1-5 Total Released,%.1lf\n",
		 grand_areas_1_5_total_released );

	fprintf( output_file,
		 "Areas 1-5 Total Caught,%.1lf\n",
		 grand_areas_1_5_total_caught );

	fprintf( output_file,
		 "Areas 1-6 Total Kept,%.1lf\n",
		 grand_areas_1_6_total_kept );

	fprintf( output_file,
		 "Areas 1-6 Total Released,%.1lf\n",
		 grand_areas_1_6_total_released );

	fprintf( output_file,
		 "Areas 1-6 Total Caught,%.1lf\n",
		 grand_areas_1_6_total_caught );

	fclose( output_file );

	sprintf( ftp_prompt,
	"%s: &lt;Left Click&gt; to view or &lt;Right Click&gt; to save.",
		 format_initial_capital( buffer,
		 			 GRAND_FILENAME_LABEL ) );

	appaserver_library_output_ftp_prompt(
		ftp_filename,
		ftp_prompt,
		(char *)0 /* target */,
		(char *)0 /* application_type */ );

	printf( "<br>\n" );

} /* output_grand_total_sheet_text_file() */

