#!/usr/bin/python

################################################################
#                                                              #
#     +->-> PYTHON EVOLUTION vs INTELLIGENT DESIGN ->->-+      #
#     |                                                 |      #
#     ^           ( TIMELINE VISUALIZATION )            V      #
#     |                                                 |      #
#     +-<---<---<---<---<---<---<---<---<---<---<---<---+      #
#                                                              #
#                                                              #
#                     COPYRIGHT  2004 2005                     #
#                       Alexander Kozik                        #
#                                                              #
#                     http://www.atgc.org/                     #
#                                                              #
#             UCD Genome Center. R.Michelmore group            #
#                                                              #
################################################################

def Create_The_World(script_name, out_name, data_file, end_of_time, current_year, pilf_dir):

	##################################
	####     CANVAS BACKGROUND    ####
	out_file1 = out_name + '.large.png'
	out_file2 = out_name + '.small.png'
	out_file3 = out_name + '.html'
	out_file4 = out_name + '.part.png'
	##################################
	boxed_style = "TRUE"
	##################################
	html_open =  open(out_file3, "wb")

	### LOAD FONTS ###
	font__def  = ImageFont.load_default()
	# font_C12R  = ImageFont.load(pilf_dir + "/" + "courR12.pil")
	# font_C12B  = ImageFont.load(pilf_dir + "/" + "courB12.pil")
	font_H12R  = ImageFont.load(pilf_dir + "/" + "helvR12.pil")
	font_H14R  = ImageFont.load(pilf_dir + "/" + "helvR14.pil")
	font_H18R  = ImageFont.load(pilf_dir + "/" + "helvR18.pil")
	font_H24R  = ImageFont.load(pilf_dir + "/" + "helvR24.pil")
	font_H24B  = ImageFont.load(pilf_dir + "/" + "helvB24.pil")

	font_copyright = "Copyright (c) 1987 Adobe Systems, Inc., Portions Copyright 1988 Digital Equipment Corp."

	list_of_fonts = ["helvR12.pil", "helvR14.pil", "helvR18.pil", "helvR24.pil", "helvB24.pil"]
	list_of_pbm   = ["helvR12.pbm", "helvR14.pbm", "helvR18.pbm", "helvR24.pbm", "helvB24.pbm"]

	### HTML HEADER CONTENT ###
	html_header = """<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="python script">
<title>Universe Timeline</title>
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#00aaaa" alink="#aa0000">

<center>
<font face="Helvetica, Arial, sans-serif">
<br>
<b>
<h2>
Universe Timeline
<br>
<i>
by Alexander Kozik
</i>
<br>
</h2>
</b>
</center>
</font>

<font face="Helvetica, Arial, sans-serif">
<p style="margin:50px">
"""
	### TABLE HEADER CONTENT ###
	table_header="""
<br>

<table border=2>

<tr>
<td align=center>
<b><font color="#ffffff">__</font>#<font color="#ffffff">__</font></b>
</td>
<td align=center>
<b>Time</b>
</td>
<td align=center>
<b>Metric</b>
</td>
<td align=center>
<b>Event</b>
</td>
<td align=center>
<b>Type</b>
</td>
<td align=center>
<b>Google search</b>
</td>
</tr>

"""
	###    HTML WEB PAGE    ###
	html_open.write(html_header)
	###        SCRIPT       ###
	html_open.write("This web page and corresponding images have been generated using <b><a href=\"" + \
						script_name + "\">" + script_name + "</a> </b> script. " + '\n')
	### EXAMPLE INPUT FILE ##
	html_open.write("Script takes as an input this <b><a href=\"" + \
						data_file + "\">" + data_file + "</a> </b> datafile. " + '\n')
	html_open.write("Script generates a poster-size image of <b>timeline in logarithmic</b> scale as well as this web page." + '\n')
	html_open.write("<br><br>" + '\n' + '\n')
	### EXAMPLE OF USAGE  ###
	html_open.write("Example of script usage: " + '\n' + "<br><br><b><font color=\"#aa0077\">" + '\n')

	html_open.write("$python " + script_name + " " + out_name + " " + data_file + " " + `end_of_time` + " " + `current_year` + " " + pilf_dir)

	html_open.write("</b></font>" + '\n' + "<br><br>" + '\n')

	###     ARGUMENTS     ###
	html_open.write("where: " + '\n' + "<br><b>" + out_name + "</b> - prefix for output files <br>" + '\n')
	html_open.write("<b>" + data_file + "</b> - input datafile <br>" + '\n')
	html_open.write("<b>" + `end_of_time` + "</b> - end of timeline <br>" + '\n')
	html_open.write("<b>" + `current_year` + "</b> - current year <br>" + '\n')
	html_open.write("<b>" + pilf_dir + "</b> - directory with PIL fonts" + '\n' + "<br><br>" + '\n' + '\n')

	###   OUTPUT FILES    ###
	html_open.write("Upon execution script generates four output files: <br>" + '\n')
	html_open.write("<b><a href=\"" + out_file1 + "\">" + out_file1 + "</a></b> - large timeline image <br>" + '\n')
	html_open.write("<b><a href=\"" + out_file2 + "\">" + out_file2 + "</a></b> - small timeline image <br>" + '\n')
	html_open.write("<b><a href=\"" + out_file4 + "\">" + out_file4 + "</a></b> - cropped part of large timeline image <br>" + '\n')
	html_open.write("<b><a href=\"" + out_file3 + "\">" + out_file3 + "</a></b> - this web page <br>" + '\n')

	### INPUT FILE FORMAT ###
	html_open.write("<br>" + '\n' + '\n')
	html_open.write("<b><font color=\"#aa0077\">Input file format <a href=\"" + data_file + "\">" + data_file + "</a>: </b></font><br>" + '\n')
	html_open.write("<b>1-st column - <font color=\"#0000aa\">time</font></b> [three formats: <b>YA</b> or  <b>BC</b> or  <b>AD</b>]" + '\n' + "<br>" + '\n')
	html_open.write("<b>2-nd column - <font color=\"#0000aa\">time metric</font></b> [<b>YA</b> - years ago; <b>BC</b> - Before Christ (Before Common Era);  <b>AD</b> - Anno Domini (Common Era)]" + '\n' + "<br>" + '\n')
	html_open.write("<b>3 -d column - <font color=\"#0000aa\">event</font></b> [description of an event/datapoint]" + '\n' + "<br>" + '\n')
	html_open.write("<b>4-th column - <font color=\"#0000aa\">event scale</font></b> [<b>great</b>; <b>large</b>; <b>small</b>]" + '\n' + "<br>" + '\n')
	html_open.write("<b>5-th column - <font color=\"#0000aa\">event type</font></b> [<b>time</b>; <b>geo</b>; <b>life</b>; <b>plant</b>; <b>info</b>; <b>tech</b>; <b>web</b>; <b>hist</b>]" + '\n' + "<br>" + '\n')
	html_open.write("<b>6-th column - <font color=\"#0000aa\">display/hide</font></b> [<b>\"+\"</b> - to display; <b>\"-\"</b> - to hide (skip)]" + '\n' + "<br>" + '\n')
	html_open.write("<b>7-th column - <font color=\"#0000aa\">google keywords</font></b> [Keywords for Google Web search]" + '\n' + "<br>" + '\n')
	html_open.write("<br>" + '\n' + '\n')

	###     PIL FONTS     ###
	html_open.write(`len(list_of_fonts)` + " PIL fonts were used in this script:<br>" + '\n')
	p = 1
	while p <= len(list_of_fonts):
		html_open.write("<b><a href=\"./" + pilf_dir + "/" + list_of_fonts[len(list_of_fonts)-p] + "\">" + list_of_fonts[len(list_of_fonts)-p] + "</a></b> " + '\n')
		p = p + 1
	html_open.write("<br>" + '\n')
	p = 1
	while p <= len(list_of_pbm):
		html_open.write("<b><a href=\"./" + pilf_dir + "/" + list_of_pbm[len(list_of_pbm)-p] + "\">" + list_of_pbm[len(list_of_pbm)-p] + "</a></b> " + '\n')
		p = p + 1
	html_open.write("<br>" + '\n')
	html_open.write(font_copyright + "<br>" + '\n')
	html_open.write("<br>" + '\n' + '\n')

	###   PARTIAL IMAGE   ###
	html_open.write("part of large image (download full size image <a href=\"" + out_file1 + "\">here</a>): <br>" + '\n')
	html_open.write("<img src=\"" + out_file4 + "\">" + "<br><br><br>" + '\n' + '\n')

	###      CREDITS      ###
	html_open.write("It is hard to express/display all credits for data obtained via Web search. Instead of the full list of credits a column \"<b>Google search</b>\" of this table displays <b><i>keywords</b></i> which were used to collect data.<br>" + '\n' + '\n')

	### WRITE TABLE HEADER ##
	html_open.write(table_header)

	##################################
	###       DATA PROCESSING      ###
	print ""
	print ""
	print "==========================================="
	print "SCRIPT NAME: " + script_name
	print "OUTPUT FILE: " + out_name
	print "DATA FILE:   " + data_file
	print "FONT DIR:    " + pilf_dir
	print "==========================================="
	print ""

	### IMAGE SIZE ###
	margin_x = 100
	margin_y = 200
	margin_z = 100	# RESERVED SPACE TO PRINT NUMBERS ON TIME RULER ON THE LEFT SIDE
	image_x  = 3600	# X DIMENSION IN PIXELS
	image_y  = 4400	# Y DIMENSION IN PIXELS
	image_z  = 4	# SCALING FACTOR FOR SMALL IMAGE

	### TIMELINE PARAMETERS ###
	# block_n = 5
	block_n = 6
	# block_scale = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
	block_scale = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100]
	block_step  = 100
	# block_list  = [1, 100, 10000, 1000000, 100000000]
	block_list  = [0.01, 1, 100, 10000, 1000000, 100000000]
	###########################
	block_w = 25	# BAR WIDTH IN PIXELS
	###########################
	block_array_start = {}		# TIME WINDOW FOR EACH BLOCK ( START )
	block_array_end   = {}		# TIME WINDOW FOR EACH BLOCK (  END  )
	block_position    = {}		# COORDINATES ON IMAGE FOR EACH BLOCK
	###########################

	###    DRAWING FIELD    ###
	block_x = (image_x - margin_x*2)/block_n
	block_y =  image_y - margin_y*2
	###########################

	print ""
	print "==========================================="
	print "  BLOCK 1 -=- " + `block_list[0]`
	print "  BLOCK 2 -=- " + `block_list[1]`
	print "  BLOCK 3 -=- " + `block_list[2]`
	print "  BLOCK 4 -=- " + `block_list[3]`
	print "  BLOCK 5 -=- " + `block_list[4]`
	print "  BLOCK 6 -=- " + `block_list[5]`
	print "  DRAWING FIELD PER BLOCK: " + `block_x` + " -X- " + `block_y`
	print "==========================================="
	print ""
	###########################
	time.sleep(2)
	### CREATE NEW IMAGE ###
	dim_x = image_x+margin_z
	dim_y = image_y
	time_image = Image.new("RGB", (dim_x, dim_y), (255, 255, 255))
	draw_png   = ImageDraw.Draw(time_image)

	###########################
	###     TEST DRAWING    ###
	# draw_png.text([20,20],fill=(0,0,0),text=("UNIVERSE TIMELINE"),font=font_H24B)
	# draw_png.text([20,300],fill=(200,0,200),text=("UNIVERSE TIMELINE"),font=font__def)
	# draw_png.line([40, 60,120,140],fill=(0,0,255))
	# draw_png.line([40,140,120, 60],fill=(0,0,255))
	# draw_png.ellipse([200,60,300,160],fill=(0,255,0),outline=(255,0,0))
	# draw_png.rectangle([400,70,500,170],fill=(255,200,0),outline=(255,0,0))
	### END OF TEST DRAWING ###
	###########################

	###     PRINT TITLE     ###
	draw_png.text([300,40],fill=(0,0,0),text=("UNIVERSE TIMELINE"),font=font_H24B)

	###     PRINT CREDITS   ###
	draw_png.text([2800,(dim_y-100)],fill=(0,0,0),text=("Created with Python script: http://www.atgc.org/TimeLine/ by Alexander Kozik"),font=font_H12R)

	###     DRAW BLOCKS     ###
	n = 0
	while n < block_n:
		x1 = margin_x + n*block_x + margin_z
		x2 = x1 + block_w
		y1 = margin_y
		y2 = image_y - margin_y
		draw_png.rectangle([x1,y1,x2,y2],fill=(220,220,120),outline=(0,0,0))
		draw_png.line([(x1+5),y1,(x1+5),y2],fill=(0,0,0))
		###   DRAW RULER   ###
		current_block  = block_list[n]
		####################################
		block_array_start[n] = block_list[n]
		block_array_end[n]   = block_list[n]*block_step
		block_position[n]    = x1
		####################################
		tick_step = block_y/2
		b = 0
		h = 1
		v = 10
		### FINE RULER 1 - 10 ###
		while h <= 9.9:
			tick10 = h*10
			y = margin_y + tick_step*math.log10(tick10) - tick_step
			draw_png.line([(x1-2*v),y,x2-20,y],fill=(0,0,0))
			h = h + 0.1
			v = v - 1
			if v == 0:
				v = 10
		### TIME RULER ###
		for item in block_scale:
			if item < 10:
				item10 = item*10
				y = margin_y + tick_step*math.log10(item10) - tick_step
			if item == 10:
				item10 = item*10
				y = margin_y + tick_step*math.log10(item10) - tick_step
				y_correction = y + 0 - margin_y
			if item > 10:
				item10 = item
				y = margin_y + tick_step*math.log10(item10) - tick_step + y_correction
			################################################
			draw_png.line([(x1-20),y,(x2+5),y],fill=(0,0,0))
			draw_png.line([(x1-15),(y+1),(x2+5),(y+1)],fill=(0,0,0))
			draw_png.line([(x1-15),(y-1),(x2+5),(y-1)],fill=(0,0,0))
			################################################
			current_time = item*current_block
			if current_time >= 100:
				current_time = int(round(current_time))
			item_label = str(current_time)
			################################################
			item_label = comma_me(item_label)
			################################################
			item_len = len(item_label)
			item_cor = item_len*10 + n*2 + 40
			draw_png.text([(x1-item_cor),y],fill=(0,0,0),text=(item_label),font=font_H18R)
			if block_scale[b] == 100:
				ya_label = "Years Ago"
				item_len = len(ya_label)
				item_cor = item_len*10 + n*2 + 50
				draw_png.text([x1-item_cor,y+24],fill=(0,0,0),text=(ya_label),font=font_H18R)
			if current_time <= end_of_time:
				ad_time  = end_of_time - current_time
				ad_label = str(ad_time) + " AD"
				item_len = len(ad_label)
				item_cor = item_len*10 + n*2 + 50
				if ad_time == current_year:
					draw_png.text([(x1-item_cor),(y-24)],fill=(250,0,0),text=(ad_label),font=font_H18R)
				if ad_time < current_year:
					draw_png.text([(x1-item_cor),(y-24)],fill=(0,0,250),text=(ad_label),font=font_H18R)
				if ad_time > current_year:
					draw_png.text([(x1-item_cor),(y-24)],fill=(250,0,250),text=(ad_label),font=font_H18R)
			if current_time > end_of_time and current_time < 10000:
				bc_time  = current_time - end_of_time
				bc_label = str(bc_time) + " BC"
				item_len = len(bc_label)
				item_cor = item_len*10 + n*2 + 50
				draw_png.text([(x1-item_cor),(y+24)],fill=(0,200,0),text=(bc_label),font=font_H18R)
			b = b + 1
		##############
		h = 10
		v = 10
		### FINE RULER 10 - 100 ###
		while h <= 99:
			tick10 = h
			y = margin_y + tick_step*math.log10(tick10) - tick_step
			draw_png.line([(x1-2*v),(y+y_correction),x2-20,(y+y_correction)],fill=(0,0,0))
			h = h + 1
			v = v - 1
			if v == 0:
				v = 10
		##############
		n = n + 1
	###########################
	###      DATA FILE      ###
	data_plotted = 0
	data_skipped = 0
	###########################
	data_open =  open(data_file,  "rb")
	while 1:
		t = data_open.readline()
		if t == '':
			break
		if '\n' in t:
			t = t[:-1]
		if '\r' in t:
			t = t[:-1]
		t = t.split('\t')
		event_time     = t[0]
		event_time     = float(event_time)
		time_display   = t[0]
		if float(time_display) >= 10000:
			time_display = comma_me(time_display)
		event_metric   = t[1]
		event_label    = t[2]
		event_scale    = t[3]
		event_type     = t[4]
		event_display  = t[5]
		event_google   = t[6]
		font_scale = font_H12R
		font_Kx    = 24
		font_Ky    = 24
		rd = 0
		gd = 0
		bd = 0
		rc = 0
		gc = 0
		bc = 0
		rf = 250
		gf = 250
		bf = 250
		html_bg = "#ffffff"
		box_size_x =  30
		box_size_y = 300
		box_size_k =  10
		box_size_m = 250
		box_Kx     = 0
		box_Ky     = 0
		if event_type == "time":
			rc = 100
			gc = 100
			bc = 100
			rf = 250
			gf = 250
			bf = 250
			html_bg = "#ffffff"
		if event_type == "geo":
			rc = 0
			gc = 0
			bc = 0
			rf = 200
			gf = 200
			bf = 200
			html_bg = "#cccccc"
		if event_type == "life":
			rc = 0
			gc = 0
			bc = 180
			rf = 200
			gf = 200
			bf = 250
			html_bg = "#ccccff"
		if event_type == "plant":
			rc = 0
			gc = 180
			bc = 0
			rf = 200
			gf = 250
			bf = 200
			html_bg = "#ccffcc"
		if event_type == "info":
			rc = 180
			gc = 0
			bc = 0
			rf = 250
			gf = 200
			bf = 200
			html_bg = "#ffcccc"
		if event_type == "tech":
			rc = 180
			gc = 0
			bc = 180
			rf = 250
			gf = 200
			bf = 250
			html_bg = "#ffccff"
		if event_type == "web":
			rc = 0
			gc = 100
			bc = 100
			rf = 200
			gf = 250
			bf = 250
			html_bg = "#ccffff"
		if event_type == "hist":
			rc = 100
			gc = 100
			bc = 0
			rf = 250
			gf = 250
			bf = 200
			html_bg = "#ffffcc"
		#########################
		time_correction = 0
		if event_metric == "YA":
			event_time = event_time
		if event_metric == "AD":
			event_time = end_of_time - event_time
		if event_metric == "BC":
			event_time = end_of_time + event_time
		#########################
		if event_scale == "great":
			font_scale = font_H24R
			font_Kx     = 50
			font_Ky     = 35
			box_size_y  = 43
			box_size_k =  19
			box_size_m = 350
			box_Ky     = 5
		if event_scale == "large":
			font_scale = font_H18R
			font_Kx     = 50
			font_Ky     = 28
			box_size_y  = 33
			box_size_k =  13
			box_size_m = 300
			box_Ky     = 3
		if event_scale == "small":
			font_scale = font_H14R
			font_Kx     = 50
			font_Ky     = 20
			box_size_y  = 25
			box_size_k =  10
			box_size_m = 250
			box_Ky     = 3
		if event_type == "time":
			# font_scale = font_C12B
			# font_Kx     = 53
			# font_Ky     =  8
			# box_size_y  = 22
			# box_size_k =   9
			font_scale = font_H14R
			font_Kx     = 55
			font_Ky     = 13
			box_size_y  = 25
			box_size_k =  10
			box_size_m = 250
			box_Ky     = 10
		##########################
		event_length = len(event_label)
		box_size_x = event_length*box_size_k
		if box_size_x <= box_size_m:
			box_size_x = box_size_m + 0
		if box_size_x >= 500:
			box_size_x = 500
		event_tail   = 48 - event_length
		event_text   = event_label
		if event_tail > 0:
			event_addition = " "*event_tail
			event_text = event_label + event_addition
		##########################
		if event_display == "+":
			data_plotted = data_plotted + 1
			### GOOGLE SEARCH WEB PAGE ###
			### http://www.google.com/search?hl=en&q=break-up+pangaea+timeline&btnG=Google+Search ###
			google_prefix = "http://www.google.com/search?hl=en&q="
			google_suffix = "&btnG=Google+Search"
			html_open.write("<tr>" + '\n')
			html_open.write("<td align=center><i>" + '\n')
			html_open.write(" " + str(data_plotted) + " ")
			html_open.write("</i></td>" + '\n')
			###
			if event_metric == "YA":
				html_open.write("<td align=right><font color=\"black\">" + '\n')
			if event_metric == "BC":
				html_open.write("<td align=left><font color=\"green\">" + '\n')
			if event_metric == "AD":
				html_open.write("<td align=left><font color=\"blue\">" + '\n')
			html_open.write(time_display)
			html_open.write("</font></td>" + '\n')
			###
			html_open.write("<td align=center>" + '\n')
			html_open.write(event_metric)
			html_open.write("</td>" + '\n')
			###
			html_open.write("<td align=center bgcolor=\"")
			html_open.write(html_bg)
			html_open.write("\">" + '\n')
			event_label_mod = re.sub(">", "&#155;", event_label)
			event_label_mod = re.sub("<", "&#139;", event_label_mod)
			html_open.write(event_label_mod)
			html_open.write("</td>" + '\n')
			###
			html_open.write("<td align=center bgcolor=\"")
			html_open.write(html_bg)
			html_open.write("\">" + '\n')
			html_open.write(event_type)
			html_open.write("</td>" + '\n')
			### GOOGLE COLUMN ###
			html_open.write("<td align=center>" + '\n')
			html_open.write("<a href=\"")
			html_open.write(google_prefix)
			google_list = event_google.split(" ")
			google_len = len(google_list)
			a = 1
			for key_word in google_list:
				html_open.write(key_word)
				if a < google_len:
					html_open.write("+")
				a = a + 1
			html_open.write(google_suffix)
			html_open.write("\">")
			html_open.write(event_google)
			html_open.write("</a>")
			html_open.write("</td>" + '\n')
			html_open.write("</tr>" + '\n')
			##############################
			q = 0
			while q < block_n:
				s_time = block_array_start[q]
				e_time = block_array_end[q]
				if event_time >= s_time and event_time <= e_time:
					x1 = block_position[q]
					x2 = x1 + block_w + 15
					time_K = block_list[q]
					item = event_time/time_K
					######################
					if item < 10:
						item10 = item*10
						y = margin_y + tick_step*math.log10(item10) - tick_step
					if item == 10:
						item10 = item*10
						y = margin_y + tick_step*math.log10(item10) - tick_step
						y_correction = y + 0 - margin_y
					if item > 10:
						item10 = item
						y = margin_y + tick_step*math.log10(item10) - tick_step + y_correction
					######################
					if boxed_style == "TRUE":
						draw_png.rectangle([x2+3,(y+box_Ky),(x2+3+box_size_x),(y+box_Ky-box_size_y)],\
											fill=(rf,gf,bf),outline=(0,0,0))
						draw_png.line([(x1+7),y,(x2+5),y],fill=(rd,gd,bd))
						draw_png.line([(x1+7),y+1,x2,y+1],fill=(rd,gd,bd))
						draw_png.line([(x1+7),y-1,x2,y-1],fill=(rd,gd,bd))
						draw_png.text([(x1+font_Kx),(y-font_Ky)],fill=(rd,gd,bd),text=(event_label),font=font_scale)
					if boxed_style == "FALSE":
						draw_png.line([(x1+7),y,(x2+5),y],fill=(rc,gc,bc))
						draw_png.line([(x1+7),y+1,x2,y+1],fill=(rc,gc,bc))
						draw_png.line([(x1+7),y-1,x2,y-1],fill=(rc,gc,bc))
						draw_png.text([(x1+font_Kx),(y-font_Ky)],fill=(rc,gc,bc),text=(event_label),font=font_scale)
					######################
					print " | EVENT:  " + event_text + " | SCALE: " + event_scale + " | TIME: " \
							+ str(round(event_time,2)) + '\t' + " |  +     PLOTTED | " + \
							`data_plotted`
				q = q + 1
		if event_display == "-":
			data_skipped = data_skipped + 1
			print " | EVENT:  " + event_text + " | SCALE: " + event_scale + " | TIME: " \
							+ str(round(event_time,2)) + '\t' + " |  - NOT PLOTTED | " + \
							`data_skipped`

	###########################

	html_footer = """
email to: <a href="mailto:akozik@atgc.org">akozik@atgc.org</a> Alexander Kozik
<br>
<br>
</font>
</p>
</body>
</html>
"""
	###  CLOSE TABLE  ###
	html_open.write("</table>" + '\n' + '\n' + "<br>" + '\n')
	###  LARGE IMAGE  ###
	html_open.write("<img src=\"" + out_file2 + "\">" + "<br><br>" + '\n' + '\n')
	###    DOWNLOAD   ###
	html_open.write("Download full size image <a href=\"" + out_file1 + "\"><b>" + out_file1 + "</b></a> <br><br>" + '\n')
	###    FEEDBACK   ###
	html_open.write("feedback and comments are very welcome" + '\n' + "<br><br>" + '\n')
	### LAST MODIFIED ###
	html_open.write("last modified: " + time.strftime('%m/%d/%y') + "<br>" + '\n')
	###   CLOSE HTML  ###
	html_open.write(html_footer)

	###########################
	data_open.close()
	html_open.close()
	###########################

	print ""
	print "==========================================="
	print "PLOTTED DATA: " + `data_plotted` + "    SKIPPED DATA: " + `data_skipped`
	print "==========================================="
	print ""

	print ""
	print "==========================================="
	print "PROCESSING LARGE SIZE"
	print "==========================================="
	print ""

	time_image.save(out_file1)

	#########   SCALING THE IMAGE   ###########
	im = Image.open(out_file1)
	width, height = im.size
	print ""
	print "==========================================="
	print "IMAGE SIZE:  " + `width` + " X " + `height`
	print "==========================================="
	print ""

	print ""
	print "==========================================="
	print "PROCESSING SMALL SIZE"
	print "==========================================="
	print ""

	####        BOX TO CROP (SELECT)      ####
	bx1 = 2850
	bx2 = 3600
	by1 = 1230
	by2 = 1780
	pretty_box = (bx1,by1,bx2,by2)
	####         CROP PRETTY PART          ####
	im_part  = im.crop(pretty_box)
	im_part.save(out_file4, im_part.format)
	###########################################
	####         DRAW CROPPED BOX          ####
	draw_im    = ImageDraw.Draw(im)
	draw_im.line([bx1,by1,bx2,by1],fill=(0,0,0))
	draw_im.line([bx1,(by1-1),bx2,(by1-1)],fill=(0,0,0))
	draw_im.line([bx1,by2,bx2,by2],fill=(0,0,0))
	draw_im.line([bx1,(by2+1),bx2,(by2+1)],fill=(0,0,0))
	draw_im.line([bx1,by1,bx1,by2],fill=(0,0,0))
	draw_im.line([(bx1-1),by1,(bx1-1),by2],fill=(0,0,0))
	draw_im.line([bx2,by1,bx2,by2],fill=(0,0,0))
	draw_im.line([(bx2+1),by1,(bx2+1),by2],fill=(0,0,0))
	####       PROCESSING SMALL SIZE       ####
	im_small = im.resize((width/image_z,height/image_z),Image.ANTIALIAS)
	im_small = im_small.filter(ImageFilter.SHARPEN)
	# im_small = im
	im_small.save(out_file2, im_small.format)
	###########################################

	print ""
	print "==========================================="
	print "                DONE!                      "
	print "         UNIVERSE WAS CREATED              "
	print "==========================================="
	print ""
	print ""

def comma_me(amount):
    orig = amount
    new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', amount)
    if orig == new:
        return new
    else:
        return comma_me(new)

import re
import sys
import math
import time
import Image
import ImageDraw
import ImageFont
import ImageFilter

if __name__ == "__main__":
	if len(sys.argv) <= 5 or len(sys.argv) > 6:
		print ""
		print "Program usage: "
		print "[output_file] [data_file] [end_of_time] [current_year] [pil_fonts_directory]"
		print "end_of_time should be within 2000 - 2100 AD"
		print ""
		sys.exit()
	if len(sys.argv) == 6:
		script_name  = sys.argv[0]
		out_name     = sys.argv[1]
		data_file    = sys.argv[2]
		end_of_time  = sys.argv[3]
		current_year = sys.argv[4]
		pilf_dir     = sys.argv[5]

		end_of_time  = int(end_of_time)
		current_year = int(current_year)

		Create_The_World(script_name, out_name, data_file, end_of_time, current_year, pilf_dir)

### THE END ###
