set white	{255 255 255}
set black	{0 0 0}
set gray	{190 190 190}
set medgray	{120 120 120}

set blue			{  0   0 255}
set royal			{  0 165 255}
set cyan			{  0 255 255}
set spring		{  0 255 165}
set green			{  0 255   0}
set lime			{165 255   0}
set yellow		{255 255   0}
set orange		{255 165   0}
set red				{255   0   0}
set pink			{255   0 165}
set magenta		{255   0 255}
set purple		{165   0 255}

set mblue			{  0   0 192}
set mroyal		{  0 122 192}
set mcyan			{  0 192 192}
set mspring		{  0 192 122}
set mgreen		{  0 192   0}
set mlime			{122 192   0}
set myellow		{192 192   0}
set morange		{192 122   0}
set mred			{192   0   0}
set mpink			{192   0 122}
set mmagenta	{192   0 192}
set mpurple		{122   0 192}

set lblue			{172 172 255}
set lroyal		{172 224 255}
set lcyan			{172 255 255}
set lspring		{172 255 224}
set lgreen		{172 255 172}
set llime			{224 255 172}
set lyellow		{255 255 172}
set lorange		{255 224 192}
set lred			{255 172 192}
set lpink			{255 172 224}
set lmagenta	{255 172 255}
set lpurple		{224 172 255}

set llblue			{192 192 255}
set llroyal		{192 232 255}
set llcyan			{192 255 255}
set llspring		{192 255 232}
set llgreen		{192 255 192}
set lllime			{232 255 192}
set llyellow		{255 255 192}
set llorange		{255 232 192}
set llred			{255 192 192}
set llpink			{255 192 232}
set llmagenta	{255 192 255}
set llpurple		{232 192 255}

set lllblue		{212 212 255}
set lllroyal		{212 240 255}
set lllcyan		{212 255 255}
set llspring		{212 255 240}
set lllgreen		{212 255 212}
set llllime		{240 255 212}
set lllyellow	{255 255 212}
set lllorange	{255 240 212}
set lllred			{255 212 212}
set lllpink		{255 212 240}
set lllmagenta	{255 212 255}
set lllpurple	{240 212 255}

set llllblue		{232 232 255}
set llllroyal	{232 246 255}
set llllcyan		{232 255 255}
set llllspring	{232 255 246}
set llllgreen	{232 255 232}
set llllime		{246 255 232}
set llllyellow	{255 255 232}
set llllorange	{255 246 232}
set llllred		{255 232 232}
set llllpink		{255 232 246}
set llllmagenta	{255 232 255}
set llllpurple	{246 232 255}

set dblue			{  0   0 128}
set droyal		{  0  81 128}
set dcyan			{  0 128 128}
set dspring		{  0 128  81}
set dgreen		{  0 128   0}
set dlime			{ 81 128   0}
set dyellow		{128 128   0}
set dorange		{128  81   0}
set dred			{128   0   0}
set dpink			{128   0  81}
set dmagenta	{128   0 128}
set dpurple		{ 81   0 128}

set pfont /usr/local/etc/httpd/htdocs/fonts/ms/arial.ttf
set pbfont /usr/local/etc/httpd/htdocs/fonts/ms/arialbd.ttf
set pifont /usr/local/etc/httpd/htdocs/fonts/ms/ariali.ttf
set gcpt 7
set gipt 8
set gspt 9
set gmpt 12
set glpt 20
set pi [expr acos(-1)]
set aglup [expr $pi/2]
set agldn [expr -$pi/2]

#text margin
set txm 4

#tick length
set tkl 4

#point size
set psize 3

proc bbwidth {bb} {
	set lx [lindex $bb 0]
	foreach {x y} $bb {
		set dx [expr abs($x-$lx)]
		if {$dx > 0} {
			return $dx
		}
	}
	return 0
}
proc bbheight {bb} {
	set ly [lindex $bb 1]
	foreach {x y} $bb {
		set dy [expr abs($y-$ly)]
		if {$dy > 0} {
			return $dy
		}
	}
	return 0
}

proc uppoint {img c x y} {
	global psize
	set s $psize
	set xa [expr $x-$s]
	set ya [expr $y+$s]
	set xb [expr $x+$s]
	set yb [expr $y-$s]
	gd line $img $c $xa $ya $x $y
	gd line $img $c $x $y $xb $ya
	gd line $img $c $xa [expr $ya+1] $x [expr $y+1]
	gd line $img $c $x [expr $y+1] $xb [expr $ya+1]
}

proc dnpoint {img c x y} {
	global psize
	set s $psize
	set xa [expr $x-$s]
	set ya [expr $y+$s]
	set xb [expr $x+$s]
	set yb [expr $y-$s]
	gd line $img $c $xb $yb $x $y
	gd line $img $c $x $y $xa $yb
	gd line $img $c $xb [expr $yb-1] $x [expr $y-1]
	gd line $img $c $x [expr $y-1] $xa [expr $yb-1]
}

proc diamond {img c x y} {
	global psize
	set s $psize
	set xa [expr $x-$s]
	set ya [expr $y+$s]
	set xb [expr $x+$s]
	set yb [expr $y-$s]
	gd line $img $c $xa $y $x $yb
	gd line $img $c $x $yb $xb $y
	gd line $img $c $xb $y $x $ya
	gd line $img $c $x $ya $xa $y
	gd line $img $c $x $ya $x $yb
}

proc sclock {img c x y} {
	global psize
	set r 10
	gd arc $img $c $x $y $r $r 0 360
	gd line $img $c $x $y $x [expr $y-2]
	gd line $img $c $x $y [expr $x+2] [expr $y+2]
}

proc ppoint {img c x y} {
	global psize
	set s 1
	set xa [expr $x-$s]
	set xb [expr $x+$s]
	set ya [expr $y-$s]
	set yb [expr $y+$s]
	gd line $img $c $xa $ya $xa $yb
	gd line $img $c $xb $ya $xb $yb
	gd line $img $c $x $ya $x $yb
}

proc cpoint {img c x y} {
	global psize
	set r [expr round($psize*2)]
	gd arc $img $c $x $y $r $r 0 360
}

proc xpoint {img c x y} {
	global psize
	set s $psize
	set xa [expr $x-$s]
	set ya [expr $y+$s]
	set xb [expr $x+$s]
	set yb [expr $y-$s]
	gd line $img $c $xa $yb $xb $ya
	gd line $img $c $xa $ya $xb $yb
	gd line $img $c $xa [expr $yb-1] $xb [expr $ya-1]
	gd line $img $c $xa [expr $ya-1] $xb [expr $yb-1]
}

proc plotline {type h w bf ngrid ntick barw \
	xdecp ydecp xmax ymax ymargins y2label fy2 fy2param \
	title pngname xlabel xformat ylabel slabels series ltypes colors \
	{plabels ""} {yngrid ""} {yntick ""} {xtrabar 1} {info ""} {curx ""} \
	{info2 ""} {ymin ""}} {
	global white black gray medgray
	global blue royal cyan spring green lime yellow orange red pink magenta purple
	global mblue mroyal mcyan mspring mgreen mlime myellow morange mred mpink mmagenta mpurple
	global lblue lroyal lcyan lspring lgreen llime lyellow lorange lred lpink lmagenta lpurple
	global llblue llroyal llcyan llspring llgreen lllime llyellow llorange llred llpink llmagenta llpurple
	global lllblue lllroyal lllcyan lllspring lllgreen llllime lllyellow lllorange lllred lllpink lllmagenta lllpurple
	global llllblue llllroyal llllcyan llllspring llllgreen lllllime llllyellow llllorange llllred llllpink llllmagenta llllpurple
	global dblue droyal dcyan dspring dgreen dlime dyellow dorange dred dpink dmagenta dpurple
	global pfont pbfont pifont 
	global gcpt gipt gspt gmpt glpt 
	global aglup agldn
	global txm tkl
	global psize

	set rgbs [list \
		$mblue $mgreen $mred $mcyan $myellow $mmagenta $mroyal $lime $pink $mspring $orange $purple \
		$blue $green $red $cyan $yellow $magenta $royal $mlime $mpink $spring $morange $mpurple \
		$lblue $lgreen $lred $lcyan $lyellow $lmagenta $lroyal $llime $lpink $lspring $lorange $lpurple \
		$dblue $dgreen $dred $dcyan $dyellow $dmagenta $droyal $dlime $dpink $dspring $dorange $dpurple \
	]

	if {$yngrid == ""} {set yngrid $ngrid}
	if {$yntick == ""} {set yntick $ntick}

	if {$colors != ""} {

		#build list of reserved color values
		set cvals [list]
		foreach c $colors {
			if {$c != ""} {
				lappend cvals [set $c]
			}
		}
	
		#build rgb list
		set i 0
		foreach c $colors {
			if {$c != ""} {
				lappend rgbline [set $c]
			} else {
				while {[lcontain $cvals [lindex $rgbs $i]]} {incr i}
				lappend rgbline [lindex $rgbs $i]
				incr i
			}
		}
	} else {
		set rgbline $rgbs
	}

	#set colors
	set rgbbg $white
	set rgblabel $black
	set rgbgrid $gray
	set rgbfuture {245 245 245}
	set rgbgrid2 $medgray

	#make sure calcs are done in floating point
	set h [expr double($h)]
	set w [expr double($w)]
	set bf [expr double($bf)]
	set ngrid [expr double($ngrid)]
	set ntick [expr double($ntick)]
	set yngrid [expr double($yngrid)]
	set yntick [expr double($yntick)]

	#adjust point sizes for smaller plots
	if {$w <= 300} {
		set cpt [expr $gcpt-1]
		set ipt [expr $gipt-1]
		set spt [expr $gspt-1]
		set mpt [expr $gmpt-2]
		set lpt [expr $glpt-6]
	} else {
		set cpt $gcpt
		set ipt $gipt
		set spt $gspt
		set mpt $gmpt
		set lpt $glpt
	}

	#create image
	set img [gd create [expr round($w)] [expr round($h)]]

	#allocate colors
	set cbg [gd color new $img \
		[lindex $rgbbg 0] [lindex $rgbbg 1] [lindex $rgbbg 2]]
	#gd color transparent $img $cbg
	set clabel [gd color new $img \
		[lindex $rgblabel 0] [lindex $rgblabel 1] [lindex $rgblabel 2]]
	set cgrid [gd color new $img \
		[lindex $rgbgrid 0] [lindex $rgbgrid 1] [lindex $rgbgrid 2]]
	set cgrid2 [gd color new $img \
		[lindex $rgbgrid2 0] [lindex $rgbgrid2 1] [lindex $rgbgrid2 2]]
	set cfuture [gd color new $img \
		[lindex $rgbfuture 0] [lindex $rgbfuture 1] [lindex $rgbfuture 2]]
	foreach rgb $rgbline {
		lappend cline [gd color new $img \
			[lindex $rgb 0] [lindex $rgb 1] [lindex $rgb 2]]
	}

	#find maxes and mins
	set yxl ""
	set ynl ""
	set xxl ""
	set xnl ""
	foreach {xl yl} $series {
		set xl [lindex [intersect3 $xl [list "M"]] 0]
		set yl [lindex [intersect3 $yl [list "M"]] 0]
		if {![lempty $xl]} {
			lappend xxl [eval max $xl]
			lappend xnl [eval min $xl]
		}
		if {![lempty $yl]} {
			lappend yxl [eval max $yl]
			lappend ynl [eval min $yl]
		}
	}
	if {$xxl != ""} {set xx [eval max $xxl]} else {set xx 1}
	if {$yxl != ""} {set yx [eval max $yxl]} else {set yx 1}
	if {$xnl != ""} {set xn [eval min $xnl]} else {set xn 0}
	if {$ynl != ""} {set yn [eval min $ynl]} else {set yn 0}
	if {$ymargins} {
		#if {$yn >= 1} { 
		#	set yn [expr int($yn)-1]
		#} else {
		#	set yn 0
		#}
		if {$ymin ==""} {set yn 0}

		#if {$type == "bar"} {
		#	set yx [expr int($yx)+1]
		#} else {
		#	set yx [expr int($yx)+2]
		#}
		set yx [expr $yx+$yx*.1]
	}

	if {$type == "bar"} {
		if {$xtrabar == "1"} {
			set xx [expr ($xx-$xn)/$ngrid+$xx]
			set ngrid [expr $ngrid+1]
		}
		set yn 0
	}

	if {$ymin != ""} {
		if {$yn > $ymin} {set yn $ymin}
	}
	if {$ymax != ""} {
		if {[string match o* $ymax]} {
			set ymax [crange $ymax 1 end]
			set yx $ymax
		} elseif {$yx < $ymax} {set yx $ymax}
	}
	if {$xmax != ""} {
		set xx $xmax
	}

	if {$yn == $yx} {set yx [expr $yx+1]}

	#write title
	set title [string trim $title]
	set bb [gd text "" $clabel $pbfont $mpt 0 [expr round($w/2)] [expr round($h/2)] $title]
	set y [expr [bbheight $bb]+$txm]
	set x [expr round($w/2-[bbwidth $bb]/2)]
	gd text $img $clabel $pbfont $mpt 0 $x $y $title

	#set title height
	set th [expr $y+$txm]

	#write credit
	if {0} {
		set credit "Created [gmtclock], Colorado Basin River Forecast Center, NWS/NOAA, [gmtclock "" %Y]"
		set bb [gd text "" $clabel $pfont $cpt 0 [expr round($w/2)] [expr round($h/2)] $credit]
		set y [expr round($h-$txm)]
		set x [expr round($w-[bbwidth $bb]-$txm)]
		gd text $img $clabel $pfont $cpt 0 $x $y $credit

		#credit height
		set ch [expr [bbheight $bb]+$txm*2]

	} else {
		set ch $txm
	}

	#write info
	if {$info != ""} {
		set bb [gd text "" $clabel $pfont $spt 0 [expr round($w/2)] [expr round($h/2)] $info]
		set y [expr round($h-$ch-$txm)]
		set x [expr round($w-[bbwidth $bb]-$txm)]
		gd text $img $clabel $pfont $spt 0 $x $y $info

		#info height
		set ih [expr [bbheight $bb]+$txm*2]

	} else {
		set ih 0
	}

	#draw legend
	if {$slabels != ""} {

		#count number of lines
		set x 0
		set tlines 1
		foreach s $slabels {
			if {[string match >* $s]} {
				set x 0
				incr tlines
				set s [crange $s 1 end]
			}
			set bb [gd text "" $clabel $pifont $spt 0 0 0 $s]
			set len [expr $psize*4+$txm*3+[bbwidth $bb]]
			set x [expr $x+$len]
			if {$x > $w} {
				set x $len
				incr tlines
			}
		}

		set x $txm
		set bbh [bbheight $bb]
		set y [expr round($h-$ch-$ih-($tlines-1)*($bbh+$txm))]
		set yg [expr $y-$psize]
		set shaden 0
		foreach s $slabels c $cline ltype $ltypes {xl yl} $series {
			if {$s != ""} {

				#check for empty series
				#set rmxl [lrmdups $xl]
				#set rmyl [lrmdups $yl]
				#if {(([llength $rmxl] == 1) && [lcontain $rmxl M]) \
				#		|| (([llength $rmyl] == 1) && [lcontain $rmyl M])} {
				#	continue
				#}

				#check for newline char >
				if {[string match >* $s]} {
					set y [expr $y+$txm+[bbheight $bb]]
					set yg [expr $y-$psize]
					set x $txm
					set s [crange $s 1 end]
				}

				#check end of line
				set bb [gd text "" $clabel $pifont $spt 0 $x $y $s]
				set len [expr $psize*2+$txm*3+[bbwidth $bb]]
				if {[expr $x+$len] > $w} {
					set y [expr $y+$txm+[bbheight $bb]]
					set yg [expr $y-$psize]
					set x $txm
				}

				#text
				set x [expr $x+$txm]
				set bb [gd text $img $clabel $pifont $spt 0 $x $y $s]
				set x [expr $x+[bbwidth $bb]+$txm]

				#points
				if {[string match *point $ltype]} {
					set x [expr $x+$psize]
					$ltype $img $c $x $yg
					set x [expr $x+$psize]

				#dotted line 
				} elseif {[string match dotted $ltype]} {
					set x [expr $x+$psize]
					ppoint $img $c $x $yg
					set x [expr $x+$psize]
					set x [expr $x+$psize]
					ppoint $img $c $x $yg
					set x [expr $x+$psize]

				#shades
				} elseif {$ltype == "shade"} {
					incr shaden
					if {$shaden == 1} {continue}
					set x0 $x
					set x [expr $x+$psize*4]
					set y2 [expr $yg-3]
					set y3 [expr $yg+3]
					gd fillrectangle $img $c $x0 $y2 $x $y3
		
				#lines and bars
				} else {
					set x0 $x
					set x [expr $x+$psize*4]
					set y2 [expr $yg-1]
					set y3 [expr $yg+1]
					gd line $img $c $x0 $yg $x $yg
					gd line $img $c $x0 $y2 $x $y2
					gd line $img $c $x0 $y3 $x $y3
				}
				set x [expr $x+$txm]
			}
		}

		#set legend height
		set lh [expr round($tlines*([bbheight $bb]+$txm))]
	} else {
		set lh 0
	}

	#write x title
	set bb [gd text "" $clabel $pbfont $spt 0 [expr round($w/2)] [expr round($h/2)] $xlabel]
	set y [expr round($h-$ch-$ih-$lh-$txm)]
	set x [expr round($w/2-[bbwidth $bb]/2)]
	gd text $img $clabel $pbfont $spt 0 $x $y $xlabel

	#set x title height
	set xth [expr round([bbheight $bb]+$txm*2)]

	#set x label height
	set bb [gd text "" $clabel $pfont $spt 0 [expr round($w/2)] [expr round($h/2)] "123"]
	set xlh [expr round([bbheight $bb]+$txm*2)]

	#calc y plot factors
	set ys $th
	set rys [expr round($ys)]
	set ye [expr $h-$ch-$ih-$lh-$xth-$xlh-$tkl]
	set rye [expr round($ye)]
	set yr [expr $h-$th-$ch-$ih-$lh-$xth-$xlh-$tkl]
	set yw [expr $yx-$yn]
	if {$yw != 0} {set ym [expr $yr/$yw]} else {set ym 0}

	#write y title
	set bb [gd text "" $clabel $pbfont $spt $aglup [expr round($w/2)] [expr round($h/2)] $ylabel]
	set y [expr round(($ye-$ys)/2+[bbheight $bb]/2)]
	set x [expr [bbwidth $bb]+$txm]
	gd text $img $clabel $pbfont $spt $aglup $x $y $ylabel

	#set y title width
	set ytw [expr [bbwidth $bb]+$txm*2]

	#write y2 title
	if {$y2label != ""} {
		set bb [gd text "" $clabel $pbfont $spt $agldn [expr round($w/2)] [expr round($h/2)] $y2label]
		set y [expr round(($ye-$ys)/2-[bbheight $bb]/2)]
		set x [expr round($w-[bbwidth $bb]-$txm)]
		gd text $img $clabel $pbfont $spt $agldn $x $y $y2label

		#set y2 title width
		set y2tw [expr [bbwidth $bb]+$txm*2]
	} else {
		set y2tw 0
	}

	#set y label width
	set fmt [cconcat "%.$ydecp" "f"]
	set fyx [format $fmt $yx]
	set bb [gd text "" $clabel $pfont $spt 0 [expr round($w/2)] [expr round($h/2)] $fyx]
	set ylw [expr [bbwidth $bb]+$txm*2]

	#set y2 label width
	if {$y2label != ""} {

		if {$fy2 == ""} {
			set y2lw $ylw
			set y2decp $ydecp
		} else {
			if {$fy2 == "stage"} {
				set y2decp 1
			} elseif {$fy2 == "pool"} {
				set y2decp 2
			} else {
				set y2decp 0
			}
			set y2x [lindex [$fy2 $fy2param [list $yx]] 0]
			set fmt [cconcat "%.$y2decp" "f"]
			set fy2x [format $fmt $y2x]
			set bb [gd text "" $clabel $pfont $spt 0 [expr round($w/2)] [expr round($h/2)] $fy2x]
			set y2lw [expr [bbwidth $bb]+$txm*2]
		}
		set y2tkl $tkl
	} else {

		#set to width of max x label
		if {$xformat != ""} {
			set fx [clock format [expr round($xx)] -gmt 1 -format $xformat]
		} else {
			set fmt [cconcat "%.$xdecp" "f"]
			set fx [format $fmt $xx]
		}
		set bb [gd text "" $clabel $pfont $spt 0 [expr round($w/2)] [expr round($h/2)] $fx]
		set y2lw [expr [bbwidth $bb]+$txm]
		set y2tkl 0
	}

	#calculate plot factors
	set xs [expr $ytw+$ylw+$tkl]
	set rxs [expr round($xs)]
	set xe [expr $w-$y2lw-$y2tw-$y2tkl]
	set rxe [expr round($xe)]
	set xr [expr $w-$ytw-$ylw-$y2lw-$y2tw-$tkl-$y2tkl]
	set xm [expr $xr/[expr $xx-$xn]]

	#draw future shading
	if {$curx != "" && $xn != 0} {
		set curx [expr round(($curx-$xn)*$xm+$xs)]
		gd fillrectangle $img $cfuture $curx $rys $rxe $rye
	}

	#caluculate grid and tick factors
	set xtf [expr ($xx-$xn)/($ngrid*$ntick)]
	set ytf [expr ($yx-$yn)/($yngrid*$yntick)]
	if {$xtf == 0} {set xtf 1}
	if {$ytf == 0} {set ytf 1}

	#plot shades between series
	set j 0
	set k 0
	set plidx 0
	set spdx 0
	foreach {xl yl} $series {c} $cline rgb $rgbline {

		#line type
		if {$ltypes == ""} {
			set ltype line
		} else {
			set ltype [lindex $ltypes $k]
		}

		if {$ltype == "shade"} {
			if {$j != 0} {

				#set tile [gd create 12 1]
				#set tcbg [gd color new $tile \
					[lindex $rgbbg 0] [lindex $rgbbg 1] [lindex $rgbbg 2]]
				#gd color transparent $tile $tcbg
				#set tcfg [gd color new $tile \
					[lindex $rgb 0] [lindex $rgb 1] [lindex $rgb 2]]
				#gd line $tile $tcfg 0 0 7 0
				#gd tile $img $tile

				set i 0
				#draw each segment
				foreach x1 $xl y1 $yl lx1 $lxl ly1 $lyl {

					#check if point missing
					if {$x1 != "M" && $y1 != "M"} {
	
						set h [expr $i-1]
						set x0 [lindex $xl $h]
						set y0 [lindex $yl $h]
						set lx0 [lindex $lxl $h]
						set ly0 [lindex $lyl $h]

						if {$x0 != "M" && $y0 != "M" && $lx0 != "M" && $ly0 != "M" && \
								$x1 != "M" && $y1 != "M" && $lx1 != "M" && $ly1 != "M"} {
							if {$i != 0} {

								#puts "polygon $lx0 $ly0 $lx1 $ly1 $x0 $y0 $x1 $y1"
								gd fillpolygon $img $c \
									[expr round(($lx0-$xn)*$xm+$xs)] \
								 	[expr round($ye-($ly0-$yn)*$ym)] \
									[expr round(($lx1-$xn)*$xm+$xs)] \
									[expr round($ye-($ly1-$yn)*$ym)] \
									[expr round(($x1-$xn)*$xm+$xs)] \
									[expr round($ye-($y1-$yn)*$ym)] \
									[expr round(($x0-$xn)*$xm+$xs)] \
									[expr round($ye-($y0-$yn)*$ym)]

							}
						}
					}
					incr i
				}
				#gd destroy $tile
			}
			set lxl $xl
			set lyl $yl
			incr j
		}
		incr k
	}

	#draw x ticks, grid lines and labels
	set v $xn
	set i 0
	set x $xs 
	set lvtxt ""
	while {[expr round($x) <= $xe]} {

		set rx [expr round($x)]

		#draw tick
		gd line $img $cgrid $rx $rye $rx [expr round($ye+$tkl)]

		if {$i == 0} {

			#draw gridline
			gd line $img $cgrid $rx $rys $rx $rye

			#format label
			if {$xformat != ""} {
				set vtxt [clock format [expr round($v)] -gmt 1 -format $xformat]
				if {$vtxt == $lvtxt} {
					set vtxt ""
				} else {
					set lvtxt $vtxt
				}
			} else {
				set fmt [cconcat "%.$xdecp" "f"]
				set vtxt [format $fmt $v]
			}

			#write label
			if {$xtrabar != "1" || $v != $xx} {
				gd text $img $clabel $pfont $spt 0 $rx [expr round($ye+$tkl+$xlh-$txm)] $vtxt
			}

			#calc next label
			set v [expr $ntick*$xtf+$v]
		}

		#increment
		set x [expr $x+$xm*$xtf]
		incr i
		if {$i == $ntick} {
			set i 0
		}
	}

	#draw y ticks, grid lines and labels
	set v $yn
	set i 0
	set y [expr $ye]
	while {[expr round($y) >= [expr $ys]]} {

		set ry [expr round($y)]

		#draw tick
		gd line $img $cgrid $rxs $ry [expr round($xs-$tkl)] $ry
		if {$y2label != ""} {
			gd line $img $cgrid $rxe $ry [expr round($xe+$tkl)] $ry	
		}

		if {$i == 0} {

			#draw gridline
			gd line $img $cgrid $rxs $ry $rxe $ry

			#format label	
			set fmt [cconcat "%.$ydecp" "f"]
			set vtxt [format $fmt $v]

			#store y2 info
			lappend y2ys $ry
			lappend y2vs $v

			#write label
			set bb [gd text "" $clabel $pfont $spt 0 [expr round($w/2)] $ry $vtxt]
			set x [expr round($ytw+$ylw-[bbwidth $bb]-$txm)]
			gd text $img $clabel $pfont $spt 0 $x $ry $vtxt
			set v [expr $yntick*$ytf+$v]
		}

		#increment
		set y [expr $y-$ym*$ytf]
		incr i
		if {$i == $yntick} {
			set i 0
		}
	}

	#y2 labels
	if {$y2label != ""} {
		if {$fy2 != ""} {
			set v2s [$fy2 $fy2param $y2vs]
		} else {
			set v2s $y2vs
		}
		foreach v2 $v2s y $y2ys {
			set fmt [cconcat "%.$y2decp" "f"]
			set v2txt [format $fmt $v2]
			gd text $img $clabel $pfont $spt 0 [expr round($xe+$tkl+$txm)] $y $v2txt
		}
	}

	#future and past labels
	if {$curx != ""} {
		set bb [gd text "" $cbg $pfont $ipt 0 [expr round($w/2)] [expr round($h/2)] "Future"]
		if {[expr $rxe-$curx] > [expr [bbwidth $bb]+2*$txm]} {
			gd fillrectangle $img $cfuture [expr $curx] [expr $rye-[bbheight $bb]-3*$txm] \
				[expr $curx+[bbwidth $bb]+4*$txm] [expr $rye-$txm]
			gd text $img $cgrid2 $pfont $ipt 0 [expr $curx+3*$txm] [expr $rye-2*$txm] "Future"
		}
		set bb [gd text "" $cbg $pfont $ipt 0 [expr round($w/2)] [expr round($h/2)] "Past"]
		if {[expr $curx-$rxs] > [expr [bbwidth $bb]+2*$txm]} {
			gd fillrectangle $img $cbg [expr $curx-[bbwidth $bb]-4*$txm] [expr $rye-[bbheight $bb]-3*$txm] \
				[expr $curx] [expr $rye-$txm]
			gd text $img $cgrid2 $pfont $ipt 0 [expr $curx-[bbwidth $bb]-3*$txm] [expr $rye-2*$txm] "Past"
		}
		sclock $img $cgrid2 $curx [expr $rye-round([bbheight $bb]/2)-2*$txm]
	}

	#time and credit
	if {$w > 300} {
		set credit "Created [gmtclock]\nNOAA/CBRFC, [gmtclock "" %Y]"
		set ilines [split $credit \n]
		set nil [llength $ilines]
		set iw 0; set ih 0
		set hs [list]
		foreach iline $ilines {
			set bb [gd text "" $cgrid2 $pfont $cpt 0 [expr round($w/2)] [expr round($h/2)] $iline]
			if {[bbwidth $bb] > $iw} {set iw [bbwidth $bb]}
			set ih [expr $ih+[bbheight $bb]]
			lappend hs [bbheight $bb]
		}
		gd fillrectangle $img $cbg [expr $rxe-$iw-3*$txm] \
			[expr $rys+$txm] [expr $rxe-$txm] [expr $rys+$ih+2*$txm+$nil*$txm]
		set y [expr $rys+$txm]
		foreach iline $ilines h $hs {
			set y [expr $y+$h+$txm]
			gd text $img $cgrid2 $pfont $cpt 0 [expr $rxe-$iw-2*$txm] $y $iline
		}
	}

	if {$info2 != ""} {
		set ilines [split $info2 \n]
		set nil [llength $ilines]
		set iw 0; set ih 0
		set hs [list]
		foreach iline $ilines {
			set bb [gd text "" $cgrid2 $pfont $ipt 0 [expr round($w/2)] [expr round($h/2)] $iline]
			if {[bbwidth $bb] > $iw} {set iw [bbwidth $bb]}
			set ih [expr $ih+[bbheight $bb]]
			lappend hs [bbheight $bb]
		}
		gd fillrectangle $img $cbg [expr $rxs+$txm] \
			[expr $rys+$txm] [expr $rxs+$iw+3*$txm] [expr $rys+$ih+2*$txm+$nil*$txm]
		set y [expr $rys+$txm]
		foreach iline $ilines h $hs {
			set y [expr $y+$h+$txm]
			gd text $img $cgrid2 $pfont $ipt 0 [expr $rxs+2*$txm] $y $iline
		}
	}

	#plot lines 
	set j 0
	set plidx 0
	foreach {xl yl} $series {c} $cline {

		#line type
		if {$ltypes == ""} {
			set ltype line
		} else {
			set ltype [lindex $ltypes $j]
			if {$ltype == ""} {
				set ltype line
			}
		}

		set i 0
		#draw each segment
		set ldot 0
		set cdot 0
		foreach x1 $xl y1 $yl {

			#check if point missing
			if {$x1 != "M" && $y1 != "M"} {

				#bar graph
				if {$type == "bar"} {
					set xoff [expr ($barw+1)*$j+1]
					if {$y1 != 0} {
						for {set k 0} {$k < $barw} {incr k} {
							gd line $img $c \
								[expr round(($x1-$xn)*$xm+$xs)+$xoff+$k] \
								[expr round($ye-(0-$yn)*$ym)-1] \
								[expr round(($x1-$xn)*$xm+$xs)+$xoff+$k] \
								[expr round($ye-($y1-$yn)*$ym)]
						}
					}

				#points
				} elseif {[string match *point $ltype]} {
							$ltype $img $c \
								[expr round(($x1-$xn)*$xm+$xs)] \
								[expr round($ye-($y1-$yn)*$ym)]
	
							#label xpoint
							if {$ltype == "xpoint"} {
								set px [expr round(($x1-$xn)*$xm+$xs)+$txm]
								set py [expr round($ye-($y1-$yn)*$ym)]
								set plabel [lindex [lindex $plabels $plidx] $i]
								gd text $img $clabel $pfont $spt 0 $px $py [string trim $plabel]
							}

				} elseif {$ltype == "shade"} {

				#lines
				} else {
					set h [expr $i-1]
					set x0 [lindex $xl $h]
					set y0 [lindex $yl $h]
					if {$x0 != "M" && $y0 != "M"} {
						if {$i != 0} {

							#dashed line, horizontal only
							if {$ltype == "dash" && $y0 == $y1} {
								set dsize 5
								set xa [expr round(($x0-$xn)*$xm+$xs)]
								set xb [expr $xa+$dsize]
								set xc [expr round(($x1-$xn)*$xm+$xs)]
								set y [expr round($ye-($y0-$yn)*$ym)]
								while {$xb <= $xc} {
									gd line $img $c $xa $y $xb $y
									gd line $img $c $xa [expr $y-1] $xb [expr $y-1]
									set xb [expr $xb+$dsize*2]
									set xa [expr $xa+$dsize*2]
								}

							#dotted line
							} elseif {$ltype == "dotted"} {

								set px0 [expr round(($x0-$xn)*$xm+$xs)]
								set py0 [expr round($ye-($y0-$yn)*$ym)]
								set px1 [expr round(($x1-$xn)*$xm+$xs)]
								set py1 [expr round($ye-($y1-$yn)*$ym)]
								set pdx [expr abs($px1-$px0)]
								set pdy [expr abs($py1-$py0)]
								set cdot [expr $cdot+$pdx+$pdy]
								if {[expr $cdot-$ldot] > 10} {
									ppoint $img $c $px1 $py1
									set ldot $cdot
								}

							#solid line
							} else {
								gd line $img $c \
									[expr round(($x0-$xn)*$xm+$xs)] \
									[expr round($ye-($y0-$yn)*$ym)] \
									[expr round(($x1-$xn)*$xm+$xs)] \
									[expr round($ye-($y1-$yn)*$ym)]
								gd line $img $c \
									[expr round(($x0-$xn)*$xm+$xs)] \
									[expr round($ye-($y0-$yn)*$ym)-1] \
									[expr round(($x1-$xn)*$xm+$xs)] \
									[expr round($ye-($y1-$yn)*$ym)-1]
							}
						}
					}
				}
			}
			incr i
		}
		incr j
	}

	#write png
	gd interlace $img true
	set out [open $pngname w]
	gd writePNG $img $out
	close $out
}