#!$AWK -f
#
#	@(#)make_symbol	1.3  08/31/99
#
#	Paul Wessel, 08-OCT-98
#
#	Script to generate specific awk symbol utility scripts
#	for psxy and psxyz.
#
#	Usage:  make_symbol < symbol.def > symbol.awk
#		chmod +x symbol.awk
#
#	The .def file must follow a specific syntax (# starts comments).
#	There are 4 different input codes, as follows :
#
#	x0 y0 M [-G<fill>] [-W<pen>]	   Start new element at x0,y0
#	x1 y1 D				   Draw straight line to x1, y1
#	x0 y0 r da C [-G<fill>] [-W<pen>]  Draw single circle of radius r
#					   around x0,y0
#					   with step-size da degrees
#	x0 y0 r a1 a2 da A		   Draw arc segment of radius r
#					   from angle a1 to a2 every da 
#					   degrees
#
#	Coordinates x0, y0, etc refer to a square box -R-0.5/0.5/-0.5/0.5
#
#	3-D symbols for psxyz: Use lower case m, d, c, a
#
#	C (and c) produces a single, closed segment.
#	D, A (and d, a) add to existing segment started with M (and m)
#
#	The optional -G, -W switches are the standard switches to set fill
#	and/or pen attributes.  -G implies -L for that segment.
#	The command line -G -L -W are used when nothing is found in the
#	segment header.
#
#	The resulting awk script is to be used with data files
#	(with the format lon, lat, [,z] size) as follows:
#
#	mapproject -R -J.. your_data | symbol.awk | psxy -R0/100/0/100 -Jx1 -M [-G] [-L] [-W] > t.ps
#
#	where the psxy -R is in projected units (hence -Jx1).

BEGIN {
	printf "#!$AWK -f\n# Made by make_symbol\n\nBEGIN { D2R = 0.01745329}\n{\n"
}
{
	if ($3 == "M")	# Start of 2-D line element
	{
		if (NF == 5)
			printf "\tprint \"> %s %s\"\n", $4, $5
		else if (NF == 4)
			printf "\tprint \"> %s\"\n", $4
		else
			print "\tprint \"> Start of new segment\""
		printf "\tprint $1 + $3 * %s, $2 + $3 * %s\n", $1, $2
	}
	else if ($3 == "m")	# Start of 3-D line element
	{
		if (NF == 5)
			printf "\tprint \"> %s %s\"\n", $4, $5
		else if (NF == 4)
			printf "\tprint \"> %s\"\n", $4
		else
			print "\tprint \"> Start of new segment\""
		printf "\tprint $1 + $4 * %s, $2 + $4 * %s, $3\n", $1, $2
	}
	else if ($3 == "D")	# 2-D Line segment
		printf "\tprint $1 + $3 * %s, $2 + $3 * %s\n", $1, $2
	else if ($3 == "d")	# 3-D Line segment
		printf "\tprint $1 + $4 * %s, $2 + $4 * %s, $3\n", $1, $2
	else if ($5 == "C")	# 2-D Circle segment
	{
		if (NF == 7)
			printf "\tprint \"> %s %s\"\n", $6, $7
		else if (NF == 6)
			printf "\tprint \"> %s\"\n", $6
		else
			print "\tprint \"> Start of new segment\""
		printf "\tfor (a = 0; a <= 360; a += %s) print $1 + $3 * (%s + %s * cos (a*D2R)), $2 + $3 * (%s + %s * sin (a*D2R))\n", $4, $1, $3, $2, $3
	}
	else if ($5 == "c")	# 3-D Circle segment
	{
		if (NF == 7)
			printf "\tprint \"> %s %s\"\n", $6, $7
		else if (NF == 6)
			printf "\tprint \"> %s\"\n", $6
		else
			print "\tprint \"> Start of new segment\""
		printf "\tfor (a = 0; a <= 360; a += %s) print $1 + $4 * (%s + %s * cos (a*D2R)), $2 + $4 * (%s + %s * sin (a*D2R)), $3\n", $4, $1, $3, $2, $3
	}
	else if ($7 == "A")	# 2-D Arc segment
	{
		start = $4
		stop = $5
		step = $6
#		Make sure we go in the right direction making this arc
		if (stop < start && step > 0) step = -step
		if (stop > start && step < 0) step = -step
		if (step > 0.0)
			printf "\tfor (a = %s; a <= %s; a += %s) print $1 + $3 * (%s + %s * cos (a*D2R)), $2 + $3 * (%s + %s * sin (a*D2R))\n", start, stop, step, $1, $3, $2, $3
		else
			printf "\tfor (a = %s; a >= %s; a += %s) print $1 + $3 * (%s + %s * cos (a*D2R)), $2 + $3 * (%s + %s * sin (a*D2R))\n", stop, start, step, $1, $3, $2, $3
	}
	else if ($7 == "a")	# 3-D Arc segment
	{
		start = $4
		stop = $5
		step = $6
#		Make sure we go in the right direction making this arc
		if (stop < start && step > 0) step = -step
		if (stop > start && step < 0) step = -step
		if (step > 0.0)
			printf "\tfor (a = %s; a <= %s; a += %s) print $1 + $4 * (%s + %s * cos (a*D2R)), $2 + $4 * (%s + %s * sin (a*D2R)), $3\n", start, stop, step, $1, $3, $2, $3
		else
			printf "\tfor (a = %s; a >= %s; a += %s) print $1 + $3 * (%s + %s * cos (a*D2R)), $2 + $3 * (%s + %s * sin (a*D2R)), $3\n", stop, start, step, $1, $3, $2, $3
	}
}
END {
	printf "}\n"
}
