#!/usr/bin/perl 
#
# This CGI script makes a clickable map with legend from ASCII data files
# containing lat, lon and optional text label. 
#
# The initial call to this script causes mapping options to be displayed 
# to the user. When the user is done setting options, s/he clicks the 
# "Make Map" button to generate a postscript and gif map.  A Submit causes
# this script to call itself with the VIEWMAP argument set to non-NULL. When
# VIEWMAP is non-NULL this script generates and displays the map, 
# and offers the user mapping options again.
#
# The map may be clicked on to generate a new map which is recentered on 
# the point clicked.  If a zoom factor is selected, then the map will be 
# recentered and zoomed.
#
###########################################################################
#
# Two special purpose types of standard input file formats can be used. 
# When these input files are specified, symbol and legend information 
# is generated automatically.
#  
# A STATION_FILE has the format "$latitude $longitude $station $net".  The
# argument LEGEND.  The xy location will be plotted, with the station name,
# and color and symbol will reflect different networks.
#
# An EVENT_FILE has the format "latitude longitude catalog mag depth", or if 
# the arugment "LABELS"="on", "latitude longitude catalog mag depth date time" in
# which case the map may be printed with each event having a numbered tag and
# the legend containing information for each event (if "LABELSCHECKED"="on"). 
# If the argument EVENT_FILE is defined, additional plotting options are offered to 
# allow the user to define data buckets.  The data is bucketed by magnitude 
# and depth.  The legend information includes a two dimensional legend of
# magnitude and catalog, and a depth scale.
#
###########################################################################
#
# One other type of input file may be used with this script.  It is a general
# purpose format, and legend information must be fully specified when this 
# format is used (no defaults).
#
# The XYINPUT input files are ASCII files with lines of: latitude, longitude, 
# 2 fields which may optionally be symbol information in the standard GMT way, 
# or will be ignored, a text label in the fifth column, and optionally 
# in the last column the symbol type.  Fields are separated by white space(s).
#
# The argument XYINPUT is a text string containing sets of file and optional 
# symbol specs, consisting of the keywords "file=", "symbol=" or "cptfile=", 
# optionally "text=", and values.  The sets are separated by colons 
# (i.e., multiple files may be specified).  
#
# XYINPUT="file=filename1;symbol=icolor1/isize1/isymbol1: ..."
#
# Symbol info may be fully specified as 3 numbers, delimited by '/'.  The 
# values represent color, size, and symbol.  Legitimate values are any positive 
# integer.  The size spec increases with value, in the range of 0 9.
# After that, the sizes are repeated.  There are 6 unique symbols (0-5), 
# after which they will be repeated.  Colors will be repeated after (0-12).  To
# loop over the maximum unique color/symbol combinations, iterate the symbol
# type by 1, the color by 2.  Any of the 3 numbers may be left off (e.g., 
# symbol=//0), but all '/' must be present for proper parsing.  If any of the 
# values are left off, the values are expected to be defined in the standard
# GMT way.
#
# Alternatively, symbol information may be defined in standard GMT fashion
# by defining the argument CPTFILE to specify a cpt file, and/or specifying 
# values in the input file in the 3rd and/or 4th column.  
#
# If color is missing from the symbol spec (symbol=/1/1), then a CPTFILE 
# should be specified, and the third column in the input file should 
# specify color in the standard GMT fashion (GMT funciton psxy).
#
# If symbol size is missing in this spec (symbol=0//1), it should
# be specified in the third column (or fourth column if CPTFILE is being 
# used) of the input file in the standard GMT fashion for function psxy.
#
# If symbol type is missing in this spec (symbol=0/1/), it should
# be specified in the last column of the input file in the standard GMT 
# fashion for function psxy.
#
# Example of color, size, and symbol:
#
# file=x;symbol=0/0/0:file=y;symbol=1/1/1:file=z;symbol=2/2/2:...
# 
###########################################################################
#
# The optional argument LEGEND is a text strings containing 
# sets of legend specifications consisting of keywords one or both of 
# "vlabel=" and "hlabel=", "symbol=", and their values.  The sets are 
# separated by colons.  Different legend tables are separated by \n.
# The symbol values are as described above.
#
# Some examples:
#
# LEGEND="vlabel=v1;symbol=s1:vlabel=v2;symbol=s2:vlabel=v3;symbol=s3
#
# If only vlabels are specified a one dimensional vertical legend 
# will be made:
# 
#  	v1	s1 
#	v2	s2
#	v3	s3
#
# If a one dimensional vertical legend has greater than 
# 10 rows, it will be put into two colunns like this:
#
#  	v1 s1 	v13 s13
#	v2 s2	v14 s14
#	v3 s3   v15 s15
#       ....
#
# If only hlabels are specified a one dimensional horizontal legend will 
# be made:
#
# LEGEND="hlabel=h1;symbol=s1:hlabel=h2;symbol=s2:hlabel=v3;symbol=s3"
#
#	h1 	h2	h3
#       s1	s2	s3
#	
# There is no special handling of very long horizontal legend so don't do
# that.
#
# If both hlabels and vlabels are specified a two dimensional legend will 
# be made.  Below is an exampe of a 3x3 (9 symbols) legend specification:
#
# LEGEND="hlabel=h1;vlabel=v1;symbol=s11:
#         hlabel=h2;vlabel=v1;symbol=s12:
#         hlabel=h3;vlabel=v1;symbol=s13:
#         hlabel=h1;vlabel=v2;symbol=s21:
#         hlabel=h2;vlabel=v2;symbol=s22:
#         hlabel=h3;vlabel=v2;symbol=s23:
#         hlabel=h1;vlabel=v3;symbol=s31:
#         hlabel=h2;vlabel=v3;symbol=s32:
#         hlabel=h3;vlabel=v3;symbol=s33"
#
# 	     h1  h2  h3   
#  	v1   s11 s12 s13
#	v1   s21 s22 s23
#	v3   s31 s32 s33
#
# The argument LEGENDTEXT may be set to print out any extra text information
# above the legend. If a legend label or legend text specified, a legend will 
# be created and LEGENDTEXT will be printed in the legend, embedded \n's 
# cause following text to be printed on next line
#
# To have the symbols '>' and '<' in the legend text without confusing the
# browser the characters %gt and  %lt must be used.
#
############################################################################
# 
# The argument MAPFILE, if set, is used as the map file basename.
#
############################################################################
#
# Mapping option arguments....
#
# LABELS - if eq "on", user will be offered the option to label
#
# SCALING - if "auto", the map is scaled to be centered on lon = 0, or if
# the data is close together, on the data itself.  If "region", 
# the arguments WEST, EAST, SOUTH, NORTH are used for the map boundaries.  If
# "resize", the argument FACTOR is used to zoom in or out on the map.
#
# WEST, EAST, SOUTH, NORTH - used as the map boundaries if SCALING eq "region"
#
# FACTOR - the zoom factor, used if SCALING is "resize"
#
# COLORMAP - if eq "Y" or COLORMAP eq "", a color map is generated, else b and w
#
# TITLE - if ne "", TITLE will be printed on map
#
# Programmer's notes: 
#
# The RECENTER functionality is sensitive to the font
# of the Map title and annotation (to map the pixel clicked to a lat/lon,
# the gif size is determined and then offset by the size of the title and
# annotation).  So don't change these fonts!  
#
# -Sandy Stromme 8/14/99
#
# IRIS
#
push(@INC,"/wwwshared/cgi-bin/");
use CGI;

$SIG{'INT'} = 'clean_kill';
$SIG{'QUIT'} = 'clean_kill';
$SIG{'KILL'} = 'clean_kill';

# turn autoflush on
$| = 1;

if ($#ARGV > -1) {
	foreach $pair (@ARGV) {
		($hash, $value) = split(/=/,$pair);
		$input{$hash} = $value;
	}
} else { 
	print "Content-type: text/html\n\n";
	CGI::ReadParse();
	%input = %in;
}
&print_html_head(); 

for $i (keys %input) {
        if ($input{$i} =~ /http/) {
                print << "EOI";
<BODY>
Illegal parameter.
</BODY></HTML>
EOI
        exit;
        }
}


$Name1 = $input{'Name1'};
$Name2 = $input{'Name2'};
$Name3 = $input{'Name3'};
$Name4 = $input{'Name4'};
$Lat1 = $input{'Lat1'};
$Lat2 = $input{'Lat2'};
$Lat3 = $input{'Lat3'};
$Lat4 = $input{'Lat4'};
$Lon1 = $input{'Lon1'};
$Lon2 = $input{'Lon2'};
$Lon3 = $input{'Lon3'};
$Lon4 = $input{'Lon4'};
$Radius1 = $input{'Radius1'};
$Radius2 = $input{'Radius2'};
$Radius3 = $input{'Radius3'};
$Radius4 = $input{'Radius4'};
$EvLat = $input{'EvLat'};
$EvLon = $input{'EvLon'};
$RADIUS   = $input{'RADIUS'};
$USER_DEF = $input{'USER_DEF'};
$XYINPUT = $input{'XYINPUT'};
$COLORMAP   = $input{'COLORMAP'};
if ("$COLORMAP" eq "Y" || "$COLORMAP" eq "") {
	$colselected = "SELECTED";
	$bwselected = "";
} else {
	$colselected = "";
	$bwselected = "SELECTED";
}
$LEGENDTEXT = $input{'LEGENDTEXT'};
$LEGEND = $input{'LEGEND'};
$LEGENDCMD = $input{'LEGENDCMD'};
if ("$LEGENDTEXT" ne "" || "$LEGEND" ne "" || "$LEGENDCMD" ne "") {$makelegend = 1;}
else { $makelegend = 0; }
$MAPFILE = $input{'MAPFILE'};
if ("$MAPFILE" eq "") { $MAPFILE = "mapfile"; }
$title   = $input{'TITLE'};
$title =~ s/;//;

# LABELS is set if labels checkbox should be displayed
$LABELS = $input{'LABELS'};
# LABELSCHECKED is the checkbox
$LABELSCHECKED = $input{'LABELSCHECKED'};
if ("$LABELS" eq "on") {
	if ("$LABELSCHECKED" eq "on") {
        	$labelschecked = "CHECKED";
	} else {
		$labelschecked = "";
	}
}
else {
	$labelschecked = "";
}

$LARGEVIEW = $input{'LARGEVIEW'};
#$LARGEVIEW = "off";
if ("$LARGEVIEW" eq "on") {
	$page_inches = "14.0";
	$largeviewchecked = "CHECKED";
}
else {
	$page_inches = "7.0";
	$largeviewchecked = "";
}
$FRACTURES = $input{'FRACTURES'};
if ("$FRACTURES" eq "on") {
	$fractureschecked = "CHECKED";
} else {
	$fractureschecked = "";
}
$MAGNETIC = $input{'MAGNETIC'};
if ("$MAGNETIC" eq "on") {
	$magneticchecked = "CHECKED";
} else {
	$magneticchecked = "";
}
$PLATES = $input{'PLATES'};
if ("$PLATES" eq "on") {
	$plateschecked = "CHECKED";
} else {
	$plateschecked = "";
}
$VOLCANOES = $input{'VOLCANOES'};
if ("$VOLCANOES" eq "on") {
	$VOLCANOESchecked = "CHECKED";
} else {
	$VOLCANOESchecked = "";
}
$TSUNAMI = $input{'TSUNAMI'};
if ("$TSUNAMI" eq "on") {
	$TSUNAMIchecked = "CHECKED";
} else {
	$TSUNAMIchecked = "";
}
$CITIES = $input{'CITIES'};
if ($CITIES eq "") { $CITIES = "0"; }
if ("$CITIES" eq "4000000") {
	$fourmilselected = "SELECTED";
	$citypop = 4000000;
} elsif ("$CITIES" eq "1000000") {
	$milselected = "SELECTED";
	$citypop = 1000000;
} elsif ("$CITIES" eq "500000") {
	$halfmilselected = "SELECTED";
	$citypop = 500000;
} elsif ("$CITIES" eq "250000") {
	$quartermilselected = "SELECTED";
	$citypop = 250000;
} elsif ("$CITIES" eq "100000") {
	$onehundredthouselected = "SELECTED";
	$citypop = 100000;
} elsif ("$CITIES" eq "capitols") {
	$capitolsselected = "SELECTED";
} elsif ("$CITIES" eq "0") {
	$noneselected = "SELECTED";
	$citypop = 0;
}
$SCALING   = $input{'SCALING'};
$WEST   = $input{'WEST'};
$EAST   = $input{'EAST'};
$SOUTH   = $input{'SOUTH'};
$NORTH   = $input{'NORTH'};
if ("$WEST" eq "" || "$EAST" eq "" || "$SOUTH" eq "" || "$NORTH" eq "") { 
	$WEST = "-180"; 
	$EAST = "180"; 
	$NORTH = "90"; 
	$SOUTH = "-90"; 
}
# check for non numeric entries
if ($WEST !~ /[0-9\-\+]/ || $EAST !~ /[0-9\-\+]/ || $SOUTH !~ /[0-9\-\+]/ || $NORTH !~ /[0-9\-\+]/) { 
	$WEST = "-180"; 
	$EAST = "180"; 
	$NORTH = "90"; 
	$SOUTH = "-90"; 
}
# see if recenter
$mapx   = $input{'RECENTER.x'};
$mapy   = $input{'RECENTER.y'};
# ignore recenter if auto scaling set
if ("$mapx" ne "" && $SCALING ne "auto") {
# recenter map
	($WEST,$EAST,$SOUTH,$NORTH, $lon0, $lat0) = &recenter_map();
# don't want to autoscale if recentering
} else {
	$lon0 = $WEST + ($EAST - $WEST)/2.0;
	$lat0 = $SOUTH + ($NORTH - $SOUTH)/2.0;
}

# scaling can be auto, region, userdata or resize

$FACTOR   = $input{'FACTOR'};

if ("$FACTOR" eq "") { $FACTOR = "nozoom"; }
if ("$FACTOR" eq "0.1") { $negten = "SELECTED"; }
elsif ("$FACTOR" eq "0.25") { $negfour = "SELECTED"; }
elsif ("$FACTOR" eq "0.5") { $negtwo = "SELECTED"; }
elsif ("$FACTOR" eq "2") { $postwo = "SELECTED"; }
elsif ("$FACTOR" eq "4") { $posfour = "SELECTED"; }
elsif ("$FACTOR" eq "10") { $posten = "SELECTED"; }
elsif ("$FACTOR" eq "nozoom") { $nozoom = "SELECTED"; }
if ("$SCALING" eq "") {
	# default, first call to makemap
	$SCALING = "auto";
	$autochecked = "CHECKED";
	$regionchecked = "";
	$resizechecked = "";
} elsif ("$SCALING" eq "resize") {
	# resize map
	if ("$FACTOR" ne "nozoom") {
		($WEST,$EAST,$SOUTH,$NORTH) = &resize_map();
	}
	# zoom is sticky
	$resizechecked = "CHECKED";
} elsif ("$SCALING" eq "region") {
	# region is sticky
	$regionchecked = "CHECKED";
	$autochecked = "";
	$resizechecked = "";
} elsif ("$SCALING" eq "auto") {
	# auto scale isn't sticky - set scaling to region
	$regionchecked = "CHECKED";
	$autochecked = "";
	$resizechecked = "";
	$userdatachecked = "";
} elsif ("$SCALING" eq "userdata") {
	if (("$Lat1" ne "" && "$Lon1" ne "" && "$Radius1" ne "") || 
		("$Lat2" ne "" && "$Lon2" ne "" && "$Radius2" ne "" ) ||
		("$Lat3" ne "" && "$Lon3" ne "" && "$Radius3" ne "")  ||
		("$Lat4" ne "" && "$Lon4" ne "" && "$Radius4" ne "")  ||
		("$EvLat" ne "" && "$EvLon" ne "")) { 
		$userdatachecked = "CHECKED";
		$autochecked = "";
		$regionchecked = "";
		$resizechecked = "";
	} else {
		$SCALING = "auto";
		$autochecked = "CHECKED";
		$regionchecked = "";
		$resizechecked = "";
		$userdatachecked = "";
	}
}
$user_defined_data = "
<table border=\"2\">
<tr><td>
	<table>
	<tr><td colspan=\"4\">
	User Defined Stations:
	</td></tr>
	<tr><td>Name<br>(optional)</td><td>Latitude</td><td>Longitude</td><td>Calculated<BR>Diameter
	</td>
	</tr>
	<tr>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Name1\" SIZE=\"8\" VALUE=\"$Name1\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Lat1\" SIZE=\"8\" VALUE=\"$Lat1\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Lon1\" SIZE=\"8\" VALUE=\"$Lon1\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Radius1\" SIZE=\"8\" VALUE=\"$Radius1\" onFocus=\"this.select();\"></td>
	</tr><tr>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Name2\" SIZE=\"8\" VALUE=\"$Name2\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Lat2\" SIZE=\"8\" VALUE=\"$Lat2\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Lon2\" SIZE=\"8\" VALUE=\"$Lon2\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Radius2\" SIZE=\"8\" VALUE=\"$Radius2\" onFocus=\"this.select();\">
	</td>
	</tr><tr>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Name3\" SIZE=\"8\" VALUE=\"$Name3\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Lat3\" SIZE=\"8\" VALUE=\"$Lat3\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Lon3\" SIZE=\"8\" VALUE=\"$Lon3\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Radius3\" SIZE=\"8\" VALUE=\"$Radius3\" onFocus=\"this.select();\">
	</tr><tr>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Name4\" SIZE=\"8\" VALUE=\"$Name4\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Lat4\" SIZE=\"8\" VALUE=\"$Lat4\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Lon4\" SIZE=\"8\" VALUE=\"$Lon4\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"Radius4\" SIZE=\"8\" VALUE=\"$Radius4\" onFocus=\"this.select();\"></td>
	</tr>
	</table>
	<table>
	<tr><td align=\"center\" colspan=\"2\">
	User Defined Event:</td>
	</tr><tr>
	<td>Latitude</td><td>Longitude</td></tr>
	<td><INPUT TYPE=\"TEXT\" NAME=\"EvLat\" SIZE=\"8\" VALUE=\"$EvLat\" onFocus=\"this.select();\"></td>
	<td><INPUT TYPE=\"TEXT\" NAME=\"EvLon\" SIZE=\"8\" VALUE=\"$EvLon\" onFocus=\"this.select();\"></td>
	</tr></table>
	<INPUT TYPE=\"RADIO\" $userdatachecked VALUE=\"userdata\" NAME=\"SCALING\">Scale map to User Defined Data
</td></tr></table>
";
$region = "$WEST/$EAST/$SOUTH/$NORTH";
$VIEWMAP   = $input{'VIEWMAP'};
$EVENT_FILE   = $input{'EVENT_FILE'};
$EVENT_FILE_TEXT   = $input{'EVENT_FILE_TEXT'};

$MAXBUCKET   = $input{'MAXBUCKET'};
#if ("$MAXBUCKET" eq "") { $MAXBUCKET = "0"; }
if ("$MAXBUCKET" eq "") { $MAXBUCKET = "8"; }
$BUCKETDELTA   = $input{'BUCKETDELTA'};
if ("$BUCKETDELTA" eq "") { $BUCKETDELTA = "1.0"; }

if ("$BUCKETDELTA" eq "0.1") { $bpointone = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "0.2") { $bpointtwo = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "0.3") { $bpointthree = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "0.4") { $bpointfour = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "0.5") { $bpointfive = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "0.6") { $bpointsix = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "0.7") { $bpointseven = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "0.8") { $bpointeight = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "0.9") { $bpointnine = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "1.0") { $bone = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "1.5") { $bonepointfive = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "2") { $btwo = "SELECTED"; }
elsif ("$BUCKETDELTA" eq "scale to data") { $bauto = "SELECTED"; }

$NBUCKETS   = $input{'NBUCKETS'};
if ("$NBUCKETS" eq "") { $NBUCKETS = "5"; }
if ("$NBUCKETS" eq "1") { $onebucket = "SELECTED"; }
elsif ("$NBUCKETS" eq "2") { $twobuckets = "SELECTED"; }
elsif ("$NBUCKETS" eq "3") { $threebuckets = "SELECTED"; }
elsif ("$NBUCKETS" eq "4") { $fourbuckets = "SELECTED"; }
elsif ("$NBUCKETS" eq "5") { $fivebuckets = "SELECTED"; }

$MAXSYMSIZE   = $input{'MAXSYMSIZE'};
if ("$MAXSYMSIZE" eq "") { $MAXSYMSIZE = "9"; }
if ("$MAXSYMSIZE" eq "4") { $size4 = "SELECTED"; }
elsif ("$MAXSYMSIZE" eq "5") { $size5 = "SELECTED"; }
elsif ("$MAXSYMSIZE" eq "6") { $size6 = "SELECTED"; }
elsif ("$MAXSYMSIZE" eq "7") { $size7 = "SELECTED"; }
elsif ("$MAXSYMSIZE" eq "8") { $size8 = "SELECTED"; }
elsif ("$MAXSYMSIZE" eq "9") { $size9 = "SELECTED"; }

$STATION_FILE   = $input{'STATION_FILE'};
if ("$VIEWMAP" ne "") {
	# generate a map...

	if (! -e "/ftp/pub/userdata/MAPS") { 
		system ("mkdir /ftp/pub/userdata/MAPS; chmod 777 /ftp/pub/userdata/MAPS"); 
	} 

	chdir "/ftp/pub/userdata/MAPS";
	#chdir "/wwwshared/HTML/tmp/MAPS";

	if ($EVENT_FILE) { &process_event_file(); }
	if ($STATION_FILE) { &process_station_file(); }

	# generate file names
	if ("$COLORMAP" eq "Y" || "$COLORMAP" eq "") {
		$psfile  = $MAPFILE . "." . $$ . `date '+%M%S'` . ".col.ps";
		$psfile  =~ s/\n//g;
	} else {
		$psfile  = $MAPFILE . "." . $$ . `date '+%M%S'` . ".bw.ps";
		$psfile  =~ s/\n//g;
	} 
	$giffile = $psfile;
	$giffile =~ s/.ps$/.gif/;

        $gmtpath = "/usr/local/GMT/bin";

        if (-e "/opt/csw/bin/convert") {
                $impath = "/opt/csw/bin";
        } else {
                $impath = "/usr/bin";
        }

        $ENV{PATH} = "$gmtpath:$impath:$ENV{PATH}";
        $ENV{TZ}="UTC";
        $ENV{HOME}="/ftp/pub/userdata/MAPS";

	&make_gmt_map();
        $psfile =~ s/;//;
        # grep Bounding $psfile | egrep "[0-9] [0-9][0-9][0-9][0-9][0-9]"
        $check = `grep Bounding $psfile | egrep "[0-9] [0-9][0-9][0-9][0-9][0-9]"`;
        if ($check =~ /Bounding/) {
                print << "EOI";
<BODY>
Problem generating map.
</BODY></HTML>
EOI
        exit;
        }

        $convertcmd = "convert";
        $syscmd = "$convertcmd $psfile $giffile";
        system ($syscmd);
	if ($makelegend) {
		$pslegend = $psfile;
		$pslegend =~ s/.ps$/.legend.ps/;
		$giflegend = $pslegend;
		$giflegend =~ s#.ps$#.gif#;
		&make_legend_file(); 
                $pslegend =~ s/;//;
        # grep Bounding $pslegend | egrep "[0-9] [0-9][0-9][0-9][0-9][0-9]"
        $check = `grep Bounding $pslegend | egrep "[0-9] [0-9][0-9][0-9][0-9][0-9]"`;
        if ($check =~ /Bounding/) {
                print << "EOI";
<BODY>
Problem generating map.
</BODY></HTML>
EOI
        exit;
        }
		$convertcmd = "convert -density 72x72 -trim +repage -transparent white";
                $syscmd = "$convertcmd $pslegend $giflegend";
                system ($syscmd);
	}
}

print << "EOI";
<BODY BGCOLOR="FFFFFF" LINK="#6633CC" VLINK="#38B0DE">
<FORM NAME="map" METHOD="GET" ACTION="/quakes/make_event_search_map.pl" ENCTYPE="application/x-www-form-urlencoded" onSubmit="return verify(this)">
<INPUT TYPE="hidden" NAME="USER_DEF" VALUE="$USER_DEF">
<INPUT TYPE="hidden" NAME="XYINPUT" VALUE="$XYINPUT">
<INPUT TYPE="hidden" NAME="LEGENDCMD" VALUE="$LEGENDCMD">
<INPUT TYPE="hidden" NAME="LEGEND" VALUE="$LEGEND">
<INPUT TYPE="hidden" NAME="MAPFILE" VALUE="$MAPFILE">
<INPUT TYPE="hidden" NAME="GIFFILE" VALUE="$giffile">
<INPUT TYPE="hidden" NAME="STATION_FILE" VALUE="$STATION_FILE">
<INPUT TYPE="hidden" NAME="EVENT_FILE" VALUE="$EVENT_FILE">
<INPUT TYPE="hidden" NAME="EVENT_FILE_TEXT" VALUE="$EVENT_FILE_TEXT">
<INPUT TYPE="hidden" NAME="VIEWMAP" VALUE="recenter">
<INPUT TYPE="hidden" NAME="LEGENDTEXT" VALUE="$LEGENDTEXT">
<INPUT TYPE="hidden" NAME="LABELS" VALUE="$LABELS">
EOI
# <INPUT TYPE="button" VALUE="Print This Page" onClick="print_page()"><P>
if ($VIEWMAP) {
	print << "EOI";
<P>
<FONT SIZE="2" FACE="Helvetica, Arial">
<BIG>Download the postscript 
<a href="http://www.iris.washington.edu/pub/userdata/MAPS/$psfile"> 
map</A> 
EOI
	if ($makelegend) {
		print << "EOI";
and its 
<a href="http://www.iris.washington.edu/pub/userdata/MAPS/$pslegend">
legend</A> 
EOI
	}
	print << "EOI";
.
To generate a new map click on the <B>"Make Map"</B> button or <B>click on the 
map</B> to recenter.</B></BIG>
<P>
<small><FONT COLOR="#000000" FACE="helvetica, arial">[</FONT><A HREF="/SeismiQuery/map_help.html"><FONT FACE="helvetica, arial">How map options work</FONT></A><FONT COLOR="#000000" FACE="helvetica, arial">]</FONT></small>
<P>
</FONT>
EOI
	print << "EOI";
<TABLE ALIGN="TOP" BORDER="2" WIDTH="100%">
<TR>
	<TD ROWSPAN=3 VALIGN="TOP">
	<INPUT TYPE=IMAGE NAME="RECENTER"
	SRC="http://www.iris.washington.edu/pub/userdata/MAPS/$giffile" BORDER=0
	OnSubmit="return verify(this)">
EOI
	if ("$XYINPUT" ne "" || "$STATION_FILE" ne "" || "$EVENT_FILE" ne "" ) {
		print << "EOI";
	<TD VALIGN="TOP">
	<INPUT TYPE="radio" $autochecked VALUE="auto" NAME="SCALING">
	Scale map to data
	</TD>
EOI
	}
	print << "EOI";
<TR>
	<TD VALIGN="TOP">
	<INPUT TYPE="radio" $resizechecked VALUE="resize" NAME="SCALING">
	Zoom map by
	<SELECT NAME="FACTOR" onChange="set_resize();">
	<OPTION VALUE="0.1" $negten> -10
	<OPTION VALUE="0.25" $negfour> -4
	<OPTION VALUE="0.5"$negtwo> -2
	<OPTION VALUE="nozoom" $nozoom> No Zoom
	<OPTION VALUE="2" $postwo> +2
	<OPTION VALUE="4" $posfour> +4 
	<OPTION VALUE="10" $posten> +10
	</SELECT>
<TR>
	<TD VALIGN="TOP">
	<INPUT TYPE="RADIO" $regionchecked VALUE="region" NAME="SCALING">
	Map a region:

          <table width="180" height="205" align="center" cellpadding="4">
          <tr align="center">
          <td bgcolor="#FFFFFF">&nbsp;</td>
          <td valign="bottom" bgcolor="#CCCCCC"> NORTH<br> <input type="TEXT" id="NORTH" name="NORTH"
                size="6" maxlength="6" value="$NORTH"
                onFocus="this.select();" onChange="if (checklat(this)) set_region();"> </td>
          <td bgcolor="#FFFFFF">&nbsp;</td>
          </tr>
          <tr align="center" bgcolor="#CCCCCC">
          <td>WEST<br> <input name="WEST" type="TEXT" id="WEST"
                        onFocus="this.select();" onChange="if (checklon(this)) set_region();" value="$WEST" size="8" maxlength="8">
          </td>
          <td valign="middle" bgcolor="#FFFFFF"><a href="javascript:set_region();getCoordsFromMap();">
<img src="/SeismiQuery/IAGT/CoordinateAcquisitionTool/globe.gif"
alt="Get Coordinates from Map" style="border:0" title="Get Coordinates from Map" width="50" height="52"></a></td>
          <td> EAST <br> <input name="EAST" type="TEXT" id="EAST"
                        onFocus="this.select();" onChange="if (checklon(this)) set_region();" value="$EAST" size="8" maxlength="8">
          </td>
          <tr align="center">
          <td bgcolor="#FFFFFF">&nbsp;</td>
          <td valign="top" bgcolor="#CCCCCC">SOUTH <br> <input type="TEXT" name="SOUTH" id="SOUTH" size="6"
maxlength="6" value="$SOUTH" onFocus="this.select();" onChange="if (checklat(this)) set_region();"> </td>
          <td bgcolor="#FFFFFF">&nbsp;</td>
          </tr>
<tr><td colspan="3">
       <span class="small">
<center>
        <b>click on the globe icon</b><br>to set coordinates from a map
</center>
</td></tr>
          </table>

	<SELECT NAME="COLORMAP">
	<OPTION VALUE="Y" $colselected> Color Map
	<OPTION VALUE="N" $bwselected> Black and White
	</SELECT><BR>
	<INPUT TYPE="checkbox" $largeviewchecked NAME="LARGEVIEW">
	Large View<BR>
EOI
	if ("$LABELS" eq "on") {
		print << "EOI";
		<INPUT TYPE="checkbox" $labelschecked NAME="LABELSCHECKED">
		Labels On<BR>
EOI
	}
	#print << "EOI";
	#<INPUT TYPE="checkbox" $fractureschecked NAME="FRACTURES">
	#Fractures
	#<BR>
	#<INPUT TYPE="checkbox" $magneticchecked NAME="MAGNETIC">
	#Magnetic Lineations
	#<BR>
	#<INPUT TYPE="checkbox" $plateschecked NAME="PLATES">
	#Plates
	#<BR>
	#<INPUT TYPE="checkbox" $VOLCANOESchecked NAME="VOLCANOES">
	#Volcanic (to 1997) <FONT COLOR="#FF6600">V</FONT>
	#<BR>
	#<INPUT TYPE="checkbox" $TSUNAMIchecked NAME="TSUNAMI">
	#Tsunami (to 1998) <FONT COLOR="#FF66FF">T</FONT>
	print << "EOI";
	<INPUT TYPE="checkbox" $plateschecked NAME="PLATES">
	Plates <img src = "/gifs/plates.gif">
	<BR>
	Cities
	<SELECT NAME="CITIES">
	<OPTION VALUE="0" $noneselected> none
	<OPTION VALUE="4000000" $fourmilselected> &gt 4,000,000
	<OPTION VALUE="1000000" $milselected> &gt 1,000,000
	<OPTION VALUE="500000" $halfmilselected> &gt 500,000
	<OPTION VALUE="250000" $quartermilselected> &gt 250,000
	<OPTION VALUE="100000" $onehundredthouselected> &gt 100,000
	<OPTION VALUE="capitols" $capitolsselected> Capitols
	</SELECT>
	<P>
	Map Title:<BR>
	<INPUT TYPE="text" NAME="TITLE" SIZE=20 VALUE="$title" MAXLENGTH="30" onFocus="this.select();">
	<INPUT TYPE="submit" NAME="VIEWMAP" VALUE="Make Map">
	<INPUT TYPE="reset" NAME="RESET" VALUE="Reset">
	<P>
EOI
	if ("$EVENT_FILE" ne "") {
		$href_file = $EVENT_FILE;
		$href_file =~ s#/wwwshared/HTML##;
		print << "EOI";
	<P>
	<A HREF="#event_options">Set Advanced Options</A>
EOI
	}
	print << "EOI";
</TABLE>
EOI

	if ($makelegend) {
		print << "EOI";
<P>
<img src = "http://www.iris.washington.edu/pub/userdata/MAPS/$giflegend"><P>
EOI
	}
	if ("$EVENT_FILE" ne "") {
		print "<HR>";
		print "<A NAME=\"event_options\"></A>\n";
		&print_event_options();
	} elsif ("$USER_DEF" ne "") {
		print "<HR>\n";
		print "<A NAME=\"event_options\"></A>\n";
		&print_user_defined();
	}
	print << "EOI";
<A HREF="http://imina.soest.hawaii.edu/gmt/">
                <img src = "/gifs/maps-by-gmt.gif" HEIGHT="50" WIDTH="110">
                </A>
EOI
} else { 
	print << "EOI";
<P>
<TABLE WIDTH="80%">
	<TR>
	<TD VALIGN="TOP">
	<TABLE>
		<TR>
EOI
	if ("$XYINPUT" ne "" || "$STATION_FILE" ne "" || "$EVENT_FILE" ne "") {
		print << "EOI";
		<TD VALIGN="TOP"> <INPUT TYPE="radio" $autochecked VALUE="auto" NAME="SCALING">
		Scale map to data
		</TD>
EOI
	} else {
		print << "EOI";
<TD></TD>
EOI
		$regionchecked = "CHECKED";
	}
	print << "EOI";
		<TD>
		<SELECT NAME="COLORMAP">
		<OPTION VALUE="Y" $colselected> Color Map
		<OPTION VALUE="N" $bwselected> Black and White
		</SELECT>
		</TD>
		<TR>
		<TD>
		<INPUT TYPE="RADIO" $regionchecked VALUE="region" NAME="SCALING">
		Map a region:

          <table width="180" height="205" align="center" cellpadding="4">
          <tr align="center">
          <td bgcolor="#FFFFFF">&nbsp;</td>
          <td valign="bottom" bgcolor="#CCCCCC"> NORTH<br> <input type="TEXT" id="NORTH" name="NORTH"
                size="6" maxlength="6" value="$NORTH"
                onFocus="this.select();" onChange="if (checklat(this)) set_region();"> </td>
          <td bgcolor="#FFFFFF">&nbsp;</td>
          </tr>
          <tr align="center" bgcolor="#CCCCCC">
          <td>WEST<br> <input name="WEST" type="TEXT" id="WEST"
                        onFocus="this.select();" onChange="if (checklon(this)) set_region();" value="$WEST" size="8" maxlength="8">
          </td>
          <td valign="middle" bgcolor="#FFFFFF"><a href="javascript:set_region();getCoordsFromMap();">
<img src="/SeismiQuery/IAGT/CoordinateAcquisitionTool/globe.gif"
alt="Get Coordinates from Map" style="border:0" title="Get Coordinates from Map" width="50" height="52"></a></td>
          <td> EAST <br> <input name="EAST" type="TEXT" id="EAST"
                        onFocus="this.select();" onChange="if (checklon(this)) set_region();" value="$EAST" size="8" maxlength="8">
          </td>
          <tr align="center">
          <td bgcolor="#FFFFFF">&nbsp;</td>
          <td valign="top" bgcolor="#CCCCCC">SOUTH <br> <input type="TEXT" name="SOUTH" id="SOUTH" size="6"
maxlength="6" value="$SOUTH" onFocus="this.select();" onChange="if (checklat(this)) set_region();"> </td>
          <td bgcolor="#FFFFFF">&nbsp;</td>
          </tr>
<tr><td colspan="3">
       <span class="small">
<center>
        <b>click on the globe icon</b><br>to set coordinates from a map
</center>
</td></tr>
          </table>
		</TD><TD>
		<INPUT TYPE="checkbox" $largeviewchecked NAME="LARGEVIEW">
		Large View<BR>
EOI
	if ("$LABELS" eq "on") {
		print << "EOI";
			<INPUT TYPE="checkbox" $labelschecked NAME="LABELSCHECKED">
			Labels On<BR>
EOI
	}
	#print << "EOI";
	#<INPUT TYPE="checkbox" $fractureschecked NAME="FRACTURES">
	#Fractures
	#<BR>
	#<INPUT TYPE="checkbox" $magneticchecked NAME="MAGNETIC">
	#Magnetic Lineations
	#<BR>
	#<INPUT TYPE="checkbox" $plateschecked NAME="PLATES">
	#Plates
	#<P>
	#<INPUT TYPE="checkbox" $TSUNAMIchecked NAME="TSUNAMI">
	#Tsunami (to 1998) <FONT COLOR="#FF66FF">T</FONT>
	#<BR>
	#<INPUT TYPE="checkbox" $VOLCANOESchecked NAME="VOLCANOES">
	#Volcanic (to 1997) <FONT COLOR="#FF6600">V</FONT>
	print << "EOI";
		<INPUT TYPE="checkbox" $plateschecked NAME="PLATES">
			Plates <img src = "/gifs/plates.gif">
			<BR>
			Cities
			<SELECT NAME="CITIES">
			<OPTION VALUE="0" $noneselected> none
			<OPTION VALUE="4000000" $fourmilselected> &gt 4,000,000
			<OPTION VALUE="1000000" $milselected> &gt 1,000,000
			<OPTION VALUE="500000" $halfmilselected> &gt 500,000
			<OPTION VALUE="250000" $quartermilselected> &gt 250,000
			<OPTION VALUE="100000" $onehundredthouselected> &gt 100,000
			<OPTION VALUE="capitols" $capitolsselected> Capitols
			</SELECT>
			<P>
			Map Title:<BR>
			<INPUT TYPE="text" NAME="TITLE" SIZE=20 VALUE="$title" MAXLENGTH="30">
			</TD></TR>
			<TR><TD>
		<INPUT TYPE="submit" NAME="VIEWMAP" VALUE="Make Map">
		<INPUT TYPE="reset" NAME="RESET" VALUE="Reset">
		</TD>
		<TD><A HREF="http://imina.soest.hawaii.edu/gmt/">
		<img src = "/gifs/maps-by-gmt.gif" HEIGHT="50" WIDTH="105">
		</A></TD></TR>
<TR><TD>
	</TD>
	</TR>
		<TR><TD></TD><TD>
		<small><FONT COLOR="#000000" FACE="helvetica, arial">[</FONT>
                <A HREF="../SeismiQuery/map_help.html">
		<FONT FACE="helvetica, arial">How map options work</FONT>
		</A><FONT COLOR="#000000" FACE="helvetica, arial">]</FONT></small>
		</TD></TR>

	</TABLE>
EOI
	if ("$EVENT_FILE" ne "") {
		print "<TD>\n";
		&print_event_options();
		print "</TD>\n";
	} elsif ("$USER_DEF" ne "") {
		print "<TD>\n";
		&print_user_defined();
		print "</TD>\n";
	}

}
if ("$EVENT_FILE" eq "") {
	print << "EOI";
	<INPUT TYPE="hidden" NAME="MAXBUCKET" VALUE="">
EOI
}

print << "EOI";
</TABLE>
</FORM>
</BODY></HTML>
EOI

# The End

exit(0);

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

sub print_user_defined {
        print << "EOI";
<TABLE>
        <TR>
        <TD>
        Set Advanced Options:<br>
[see <a href="/quakes/eventSearchInstructions.htm" target="_blank">instructions for these options</a>]<P>
$user_defined_data
        </TD>
        </TR>
</TABLE>
EOI
}


sub print_event_options {
	print << "EOI";
<TABLE>
	<TR>
	<TD>
	Set Advanced Options:<br>
[see <a href="/quakes/eventSearchInstructions.htm" target="_blank">instructions for these options</a>]<P>
$user_defined_data
EOI

if (defined $EVENT_FILE_TEXT && "$EVENT_FILE_TEXT" ne "") {
	$href = $EVENT_FILE_TEXT;
	$href =~ s#/wwwshared/HTML##;
	print << "EOI";
	<A HREF="$href" TARGET="_blank">View Event Text</A><P>
EOI
}
	print << "EOI";
<P>
	Data will be binned (grouped) by event magnitude.<BR>Define the bins
	and maximum symbol size by setting the options below:<P>

	Top data bin contains magnitudes &gt=
	<INPUT TYPE="TEXT" NAME="MAXBUCKET" SIZE="3" 
		VALUE="$MAXBUCKET" onFocus="blank_text(this);">(0 scales to data)<BR>
	Maximum symbol size:
	<SELECT NAME="MAXSYMSIZE">
EOI
	($color, $size, $symbol) = &get_color_size_symbol("0/4/0");
	print "	<OPTION VALUE=\"4\" $size4> $size\n";
	($color, $size, $symbol) = &get_color_size_symbol("0/5/0");
	print "	<OPTION VALUE=\"5\" $size5> $size\n";
	($color, $size, $symbol) = &get_color_size_symbol("0/6/0");
	print "	<OPTION VALUE=\"6\" $size6> $size\n";
	($color, $size, $symbol) = &get_color_size_symbol("0/7/0");
	print "	<OPTION VALUE=\"7\" $size7> $size\n";
	($color, $size, $symbol) = &get_color_size_symbol("0/8/0");
	print "	<OPTION VALUE=\"8\" $size8> $size\n";
	($color, $size, $symbol) = &get_color_size_symbol("0/9/0");
	print "	<OPTION VALUE=\"9\" $size9> $size\n";

	print << "EOI";

	</SELECT> inches.<BR>
	<img src = "/gifs/map_symbols.gif">
	<BR>
	Bin width (magnitude difference): 
	<SELECT NAME="BUCKETDELTA">
	<OPTION VALUE="0.1" $bpointone> 0.1
	<OPTION VALUE="0.2" $bpointtwo> 0.2
	<OPTION VALUE="0.3" $bpointthree> 0.3
	<OPTION VALUE="0.4" $bpointfour> 0.4
	<OPTION VALUE="0.5" $bpointfive> 0.5
	<OPTION VALUE="0.6" $bpointsix> 0.6
	<OPTION VALUE="scale to data" $bauto> scale to data
	<OPTION VALUE="0.7" $bpointseven> 0.7
	<OPTION VALUE="0.8" $bpointeight> 0.8
	<OPTION VALUE="0.9" $bpointnine> 0.9
	<OPTION VALUE="1.0" $bone> 1.0
	<OPTION VALUE="1.5" $bonepointfive> 1.5
	<OPTION VALUE="2.0" $btwo> 2.0
	</SELECT><BR>
	Number of bins:
	<SELECT NAME="NBUCKETS">
	<OPTION VALUE="1" $onebucket> 1
	<OPTION VALUE="2" $twobuckets> 2
	<OPTION VALUE="3" $threebuckets> 3
	<OPTION VALUE="4" $fourbuckets> 4
	<OPTION VALUE="5" $fivebuckets> 5
	</SELECT>
	<P>
	</TD>
	</TR>
</TABLE>
EOI
}

sub recenter_map {

	local ($w, $e, $s, $n) = ();
	local ($x, $y) = ();
	local ($gifwidth, $gifheight, $giftype) = ();
	local ($lon_delta, $lat_delta) = ();

	$w = $WEST;
	$e = $EAST;
	$s = $SOUTH;
	$n = $NORTH;

	$x = $mapx;
	$y = $mapy;

	# offset for axes labels and map title 
	# (offset depends on whether the labels have minutes and seconds)
	if (($n - $s) > 1.5) { $x -= 32; }
	elsif (($n - $s) > 0.5) { $x -= 54; }
	else { $x -= 73; }
	$y -= 20;
	$giffile   = "/ftp/pub/userdata/MAPS/$input{'GIFFILE'}";
	($gifwidth, $gifheight, $giftype) = &gifsize($giffile);
	# offset for map margin
	if (($n - $s) > 1.5) { $gifwidth -= 64; }
	elsif (($n - $s) > 0.5) { $gifwidth -= 114; }
	else { $gifwidth -= 158; }
	$gifheight -= 69;
	$lon_delta = ($e - $w)/2;
	$lat_delta = ($n - $s)/2;

	# approximate lon clicked on...
	$lon0 = (($e - $w)/$gifwidth)*$x;
	$lon0 += $w;
	$e = $lon0 + $lon_delta;
	$w = $lon0 - $lon_delta;

	# approximate lat clicked on...
	$lat0 = (($n - $s)/$gifheight)*$y;
	$lat0 = $n - $lat0;
	$n = $lat0 + $lat_delta; 
	$s = $lat0 - $lat_delta; 
	($w, $e, $s, $n) = &fix_up_boundaries();
	return ($w, $e, $s, $n, $lon0, $lat0);
}

sub resize_map {

# if factor > 1, zoom, < 1 unzoom...

	local $w, $e, $s, $n;
	local $lon_delta, $lat_delta;

	$w = $WEST;
	$e = $EAST;
	$s = $SOUTH;
	$n = $NORTH;

	$lon_delta = abs(($e - $w))/$FACTOR;
	if ($lon_delta > 360) {
		$lon_delta = 360;
	}
	$e = ($lon0 + $lon_delta/2);
	$w = ($lon0 - $lon_delta/2);

	$lat_delta = abs(($n - $s))/$FACTOR;
	if ($lat_delta > 180) {
		$lat_delta = 180;
	}
	$n = ($lat0 + $lat_delta/2);
	$s = ($lat0 - $lat_delta/2);

	($w, $e, $s, $n) = &fix_up_boundaries;

	return ($w, $e, $s, $n);
	
}

sub fix_up_boundaries {

# this function takes the global variables, $w, $e, $s, $n, $lon0, and returns
# good boundaries that can be passed to pscoast

	# normalize
	if (abs($e) > 360) {
		$e = ($e/360 - int ($e/360))*360;
		if (abs ($w - $e) < 0.00001) {
			$w -= 360;	
		}
	}
	if (abs($w) > 360) {
		$w = ($w/360 - int ($w/360))*360;
		if (abs ($w - $e) < 0.00001) {
			$e += 360;	
		}
	}
	if ($w > $e) {
		if ($w > 0) {
			$w -=360;
		} else {
			$e += 360.;	
		}
	}

	# if delta near 360, assume want full 360, and use int boundaries
	if ($e - $w > 357) {
		$e = int $e;
		$w = int $w;
		if ($e - $w > 360) {
			 # decrease lon range
			$del = -1;
		} else {
			 #increase lon range
			$del = 1;
		}
		while( $e - $w != 360 ) {
			$e += $del;
		}
	}
	# if truncating n, extend s
	if ($n > 90) { 
		$s -= $n - 90; 
	}
	# if truncating s, extend n
	if ($s < -90) { 
		$n += -90 - $s; 
	}
	if ($n > 90) { 
		$n = 90; 
	}
	if ($s < -90) { 
		$s = -90;
	} 
	# if near 180, assume want full 180
	if ($n - $s > 178) { 
		$n = 90; 
		$s = -90;
	}  

	return ($w, $e, $s, $n);

}

sub make_gmt_map {

#  make gmt maps 


        $gmtpath = "/usr/local/GMT/bin";
        if (-e "/opt/csw/bin/convert") {
                $impath = "/opt/csw/bin";
        } else {
                $impath = "/usr/bin";
        }

        $ENV{PATH} = "$gmtpath:$impath:$ENV{PATH}";
        $ENV{TZ}="UTC";

	sub by_number { 
		if ($a < $b) { 
			-1; 
		} elsif ($a == $b) {
			0; 
		} else { 
			1;
		}		
	}

	if (("$XYINPUT" ne "" && "$SCALING" eq "auto") || "$SCALING" eq "userdata") {
		# if scaling, get min lat and lon for scaling purposes...
		
		@xyinput = split (/:/, $XYINPUT); 
		foreach $file (@xyinput) {
			@specs = split (/;/, $file);
			foreach $spec (@specs) {
				if ($spec =~ /file=/) {
					if ($spec =~ /cptfile=/) {
						next;
					} else { 
						if ("$SCALING" eq "auto") {
							($name, $filename)=split(/=/, $spec,2);
							open (INFILE, $filename)||last;
							while (<INFILE>) {
								if (/^$/) { next };
								@line = split(/\s+/);
        							push @lat, $line[0];
								push @lon, $line[1];
							}	
							close (INFILE);
						}
					}
				}
			}
		}
		if ("$Lat1" ne "") { push @lat, $Lat1; }
		if ("$Lat2" ne "") { push @lat, $Lat2; }
		if ("$Lat3" ne "") { push @lat, $Lat3; }
		if ("$Lat4" ne "") { push @lat, $Lat4; }
		if ("$EvLat" ne "") { push @lat, $EvLat; }
		if ("$Lon1" ne "") { push @lon, $Lon1; }
		if ("$Lon2" ne "") { push @lon, $Lon2; }
		if ("$Lon3" ne "") { push @lon, $Lon3; }
		if ("$Lon4" ne "") { push @lon, $Lon4; }
		if ("$EvLon" ne "") { push @lon, $EvLon; }

		if ($#lat < 0 ) {
			print << "EOI";
	<HTML><Title>Make Maps</Title><BODY BGCOLOR="FFFFFF" LINK="#6633CC" VLINK="#38B0DE">
	<BIG><CENTER>No Map Data found!</CENTER></BIG>
	</BODY></HTML>
EOI
			exit(0);
		}
		@lat = sort by_number @lat;
		@lon = sort by_number @lon;
		
		# minlon2 and maxlon2 are for testing alternate 180 degree proximities
		#        (proximities as on the surface of a cylinder)
		$minlat = $lat[0];
		$maxlat = $lat[$#lat];
		$minlon = $lon[0];
		$maxlon = $lon[$#lon];
		$icount = 0;
		foreach $lon (@lon) {
			if ($lon < 0) { $lon[$icount] += 360 };
			$icount++;
		}
		@lon = sort by_number @lon;
		$minlon2 = $lon[0];
		$maxlon2 = $lon[$#lon];
		
		# set map boundaries - determine gap from widest to border
		$space = ($maxlat - $minlat)/4;
		$space2 = ($maxlon - $minlon)/4;
		$space3 = ($maxlon2 - $minlon2)/4;

		# determine whether to scroll the map over or not
		$map_flip = 0;
		$tolerance = 45;
		if ( ($space2 - $space3) > $tolerance) {
			$map_flip = 1;
			$space2 = $space3;
		}
	
		# narrow down to the most significant spacing
		if ($space < $space2) { $space = $space2; }

# if the significant spacing (var=space) equals 0, then set it arbitrarily to
# 1 degree of spacing - this occurs when all of the stations are in a closely
# packed array such that their lat/lons are all the same or if there is
# only one station in the list - at 1 degree spacing, you will see a single
# star with some possible geographic features around it
		if ($space < 1) { $space = 1.0; }
		if ($map_flip) {
			$westbdry = $minlon2 - $space;
			$eastbdry = $maxlon2 + $space;
		}
		else {
			$westbdry = $minlon - $space;
			$eastbdry = $maxlon + $space;
		}
		$southbdry = $minlat - $space;
		$northbdry = $maxlat + $space;
	
	 # map flip indicates that we are orienting our center more around the date line
	 # than the Greenwich line  (switch to 0-360 degree reference as map does an
	 # approximate 180 degree scrolling to the east)
		if ($map_flip) {
			if ($westbdry <=-180) { $westbdry = -180; }
			if ($eastbdry >=360) { $eastbdry = 360; }
		}
		else {
			if ($westbdry <=-180) { $westbdry = -180; }
			if ($eastbdry >=180) { $eastbdry = 180; }
		}
	}
	else {
		$westbdry = $WEST;
		$eastbdry = $EAST;
		$southbdry = $SOUTH;
		$northbdry = $NORTH;
	}

	if ($southbdry < -90) { $southbdry = -90; }
	if ($northbdry > 90) { $northbdry = 90; }
	$WEST = $westbdry;
	$EAST= $eastbdry;
	$SOUTH = $southbdry;
	$NORTH = $northbdry;
	$region = "$westbdry/$eastbdry/$southbdry/$northbdry";
	$white = "255/255/255";
	$palegray = "204/204/204";
	$lightgray = "153/153/153";
	$gray = "102/102/102";
	$darkgray = "51/51/51";
	$black = "0/0/0";
	$redorange = "255/51/0";

	if ($COLORMAP eq "Y" || "$COLORMAP" eq "") {
  		$ground_color = "102/204/153";
  		$station_color = "255/0/0";
  		$river_color = "51/153/255";
  		$border_color = "102";
  		$symbol_border = "$black";
  		$ocean_color = "204/255/255";
  		$coast_color = "0/102/51";
  		$fracture_color = "102/51/153";
  		$magnetic_color = "$darkgray";
  		$plate_color = "204/153/102";
	}
	else {
  		$ground_color = "$palegray";
  		$ground_color = "$white";
  		$station_color = "$black";
  		$river_color = "$lightgray";
  		$border_color = "$darkgray";
  		$symbol_border = "$darkgray";
  		$ocean_color = "$white";
  		$coast_color = "$darkgray";
  		$fracture_color = "$lightgray";
  		$magnetic_color = "$darkgray";
  		$plate_color = "$gray";
	}
	$rivers = "-I1/0.25/${river_color}p";
	$borders = "-N1/0.75/${border_color}tap -N2/0.50/${border_color}tap";

	# find the scope of the lat/lon distributions
	$delta = $eastbdry - $westbdry;
	$delta2 = $northbdry - $southbdry;
	$min_delta = $delta2;
	if ($delta < $delta2 ) { 
		$min_delta = $delta;
		$delta = $delta2;
	}
	# set up plot scaling, map centering, and border ticks
	$scale = $page_inches / $delta;
	$height = $scale * ($northbdry - $southbdry);
	$width = $scale * ($eastbdry - $westbdry);
	# map may get cut off unless we do this...
	if ($height > 9 && "$LARGEVIEW" eq "on") {
		# seem to crop off top if height is too great, so shrink
		$page_inches = 9;
		$scale = $page_inches / $delta;
		$height = $scale * ($northbdry - $southbdry);
	}
	$width = $scale * ($eastbdry - $westbdry);

	# make resolution pick based on scope of map...
	# larger scale maps do not need
	# as high a resolution as zoomed in maps
	# this also eliminates the 'blueing' problem with zoomed in shots
	if ($delta > 180) {
		$resolution= "c";
		$borders = "";
		$area = "-A5000";
	} elsif ($delta > 150) {
		$resolution= "l";
		$borders = "";
		$area = "-A5000";
	} elsif ($delta > 60) {
		$resolution= "i";
		$area = "-A5000";
	} elsif ($delta > 5) { 
		$resolution= "h";
	} else { 
		$resolution= "f";
	}
	
	$lon0 = $westbdry + ($delta/2.0);
	$lon0 = $WEST + ($EAST - $WEST)/2.0;
	$height = $scale * ($northbdry - $southbdry);
	$degs_per_tick = int $delta / 6;
	if ($degs_per_tick == 0) { 
		$y = 1.0;
		$x = $min_delta/2;
		while ($x < $y) { $y = $y/2; }
		$degs_per_tick = $y;
	}

	$projection = "-Jq$lon0/$scale";
	
	# plot map terrain and border - variable-scaled Equidistant Cylindrical Proj.
system("gmtset DOTS_PR_INCH 72 ANOT_FONT_SIZE 10 HEADER_FONT_SIZE 18 PAPER_MEDIA a4+ PLOT_DEGREE_FORMAT '+ddd:mm:ss' XY_TOGGLE TRUE MEASURE_UNIT inch X_ORIGIN 1 Y_ORIGIN 1.5 PAGE_COLOR 255/255/255");

        #system("gmtset DOTS_PR_INCH 72 ANOT_FONT_SIZE 10 HEADER_FONT_SIZE 18 PAPER_MEDIA a4+ PLOT_DEGREE_FORMAT '+ddd:mm:ss' XY_TOGGLE TRUE MEASURE_UNIT inch X_ORIGIN 1 Y_ORIGIN 1.5 PAGE_COLOR 255/255/255");
	$scalepos = $width - .5 > 2.5 ? $width - .5 : 2.5;
	$scalepos = 0.25; 
	$syscmd = "pscoast $projection -R$region -G$ground_color -D$resolution $area $borders $rivers -S$ocean_color -B$degs_per_tick -P -W.01p/$coast_color -K > $psfile";
#print "<pre>$syscmd</pre>";
#exit;
	# getting rid of ';' - the unix command separator
	$syscmd =~ s/;//;
	system($syscmd);

	$projection =~ s/;//;	
	$region =~ s/;//;	
	$fracture_color =~ s/;//;	
	$magnetic_color =~ s/;//;	
        $plate_color =~ s/;//;
	# until data is found that works for all scales...
	if ("$FRACTURES" eq "on") { 
		system ("cat /wwwshared/HTML/SeismiQuery/gmtmap/gmt_fractures.dat | psxy $projection -R$region -M -O -K -V -P -W0.25p/$fracture_color >> $psfile"); 
	}
	if ("$MAGNETIC" eq "on") { 
		system ("cat /wwwshared/HTML/SeismiQuery/gmtmap/gmt_ml.dat | psxy $projection -R$region -M -O -K -V -P -W0.15p/$magnetic_color >> $psfile"); 
	}

	if ("$PLATES" eq "on") { 
		system ("cat /wwwshared/HTML/SeismiQuery/gmtmap/gmt_plates.dat | psxy $projection -R$region -M -O -K -V -P -W0.25p/$plate_color >> $psfile"); 
	}

	if ("$VOLCANOES" eq "on") { 
		system ("cat /wwwshared/HTML/SeismiQuery/gmtmap/volcanoes.dat | psxy $projection -R$region -Sl0.15/V -G255/102/0 -O -K -P >> $psfile");
	}

	if ("$TSUNAMI" eq "on") { 
		system ("cat /wwwshared/HTML/SeismiQuery/gmtmap/tsunami.dat | psxy $projection -R$region -Sl0.15/T -G102/0/255 -O -K -P >> $psfile");
	}
	if ($citypop > 0) { 
		$awkcmd = "awk '{x=\$4;if (x>$citypop){print \$1, \$2, \$5}}'";
		system("cat /wwwshared/HTML/SeismiQuery/gmtmap/city.dat | $awkcmd | psxy $projection -R$region -S0.075 -G$darkgray -O -K -P >> $psfile");
		$awkcmd = "awk '{x=\$4;if (x>$citypop){print \$1, \$2, \" 9 0 0 1 \", \$3}}'";
		$offset = 0.1*.5;
		system("cat /wwwshared/HTML/SeismiQuery/gmtmap/city.dat | $awkcmd | tr \'_' \' ' | pstext $projection -R$region -D0/$offset -G$darkgray -O -K -P >> $psfile"); 
	}
	if ("$capitolsselected" eq "SELECTED") { 
		$awkcmd = "awk '{x=\$5;if (x==\"a\"){print \$1, \$2, \$5}}'";
		system("cat /wwwshared/HTML/SeismiQuery/gmtmap/city.dat | $awkcmd | psxy $projection -R$region -S0.1 -G$darkgray -O -K -P >> $psfile");
		$awkcmd = "awk '{x=\$5;if (x==\"a\"){print \$1, \$2, \" 9 0 0 1 \", \$3}}'";
		$offset = 0.1*.5;
		system("cat /wwwshared/HTML/SeismiQuery/gmtmap/city.dat | $awkcmd | tr \'_' \' ' | pstext $projection -R$region -D0/$offset -G$darkgray -O -K -P >> $psfile"); 
	}

	@xyinput = split (/:/, $XYINPUT); 
	foreach $file (@xyinput) {
		@specs = split (/;/, $file);
		$symbol = "";
		$size = "";
		$cptfile = "";
		$text = "off";
		foreach $spec (@specs) {
			if ($spec =~ /file=/) {
				if ($spec =~ /cptfile=/) {
					($name,$cptfile) = split (/=/,$spec,2);}
				else { 
					($name, $filename)=split (/=/, $spec,2);
				}
			}
			if ($spec =~ /symbol=/) {
				($name,$symspec) = split (/=/,$spec,2);
				($color, $size, $symbol) = 
					&get_color_size_symbol($symspec); 
			}
			if ($spec =~ /text=/) {($name,$text) = split (/=/,$spec,2);}
		}
		if ("$cptfile" eq "") {
			if ($color eq "") {
				print << "EOI";
		<HTML><Title>Make Maps</Title><BODY BGCOLOR="FFFFFF" LINK="#6633CC" VLINK="#38B0DE">
		<BIG><CENTER>No Color Spec or cpt file defined!</CENTER></BIG>
		</BODY></HTML>
EOI
				exit(0);
			} 
			$colorspec = "-G$color";
		} else { $colorspec = "-C$cptfile"; }

		$cmd = "cat $filename | psxy $projection -R$region $colorspec -S$symbol$size -W0.001p/$symbol_border -O -K -P >> $psfile";
		$cmd =~ s/;//;
		system($cmd);

		if ("$LABELSCHECKED" eq "on" && "$text" eq "on") {
			$awkcmd =  "awk '{print \$1, \$2, \" 11 0 0 1 \", \$5}'";
			if ("$EVENT_FILE" ne "") {
				$offset = .12;
				system("cat $filename | $awkcmd | pstext $projection -R$region -W255/255/255o -D0/$offset -G0 -O -K -P >> $psfile"); 
			} else {
				$offset = $size*.5;
				system("cat $filename | $awkcmd | pstext $projection -R$region -D0/$offset -G0 -O -K -P >> $psfile"); 
			}
		}
	}

	# plot any user defined stuff...
	if (("$Lat1" ne "" && "$Lon1" ne "" && "$Radius1" ne "") || 
		("$Lat2" ne "" && "$Lon2" ne "" && "$Radius2" ne "" ) ||
		("$Lat3" ne "" && "$Lon3" ne "" && "$Radius3" ne "")  ||
		("$Lat4" ne "" && "$Lon4" ne "" && "$Radius4" ne "")  ||
		("$EvLat" ne "" && "$EvLon" ne "")) { 
		$user_data_file1 = "/tmp/userdata1.$$";
		$user_data_file2 = "/tmp/userdata2.$$";
		$user_data_file3 = "/tmp/userdata3.$$";
		open (USERDATA1, "> $user_data_file1");
		open (USERDATA2, "> $user_data_file2");
		open (USERDATA3, "> $user_data_file3");
		if ("$Lat1" ne "" && "$Lon1" ne "" && "$Radius1" ne "") { 
			print USERDATA1 "$Lat1 $Lon1 .35 t\n";
			print USERDATA2 "$Lat1 $Lon1 0 $Radius1 $Radius1\n";
			if ("$Name1" ne "") { print USERDATA3 "$Lat1 $Lon1  11 0 0 1 $Name1\n"; }
		}
		if ("$Lat2" ne "" && "$Lon2" ne "" && "$Radius2" ne "") { 
			print USERDATA1 "$Lat2 $Lon2 .35 t\n";
			print USERDATA2 "$Lat2 $Lon2 0 $Radius2 $Radius2\n";
			if ("$Name2" ne "") { print USERDATA3 "$Lat2 $Lon2  11 0 0 1 $Name2\n"; }
		}

		if ("$Lat3" ne "" && "$Lon3" ne "" && "$Radius3" ne "") { 
			print USERDATA1 "$Lat3 $Lon3 .35 t\n";
			print USERDATA2 "$Lat3 $Lon3 0 $Radius3 $Radius3\n"; 
			if ("$Name3" ne "") { print USERDATA3 "$Lat3 $Lon3  11 0 0 1 $Name3\n"; }
		}

		if ("$Lat4" ne "" && "$Lon4" ne "" && "$Radius4" ne "") { 
			print USERDATA1 "$Lat4 $Lon4 .35 t\n";
			print USERDATA2 "$Lat4 $Lon4 0 $Radius4 $Radius4\n";
			if ("$Name4" ne "") { print USERDATA3 "$Lat4 $Lon4  11 0 0 1 $Name4\n"; }
		}
		if ("$EvLat" ne "" && "$EvLon" ne "" ) { print USERDATA1 "$EvLat $EvLon .45 a\n" }

		close USERDATA1;
		close USERDATA2;
		close USERDATA3;
		system("cat $user_data_file1 | psxy $projection -R$region -G$redorange -W0.02p/$black -O -K -P -S >> $psfile");
		system("cat $user_data_file2 | psxy $projection -R$region -W0.02p/$black -O -K -P -SE>> $psfile");
		system("cat $user_data_file3 | pstext $projection -R$region -G$darkgray -O -K -P >> $psfile");
		unlink $user_data_file1;
		unlink $user_data_file2;
		unlink $user_data_file3;
	}
	if ($title eq "") { $mtitle = " "; }
	else { $mtitle = $title; }
	$titlepos = $width > length($mtitle)/8 ? $width/2 : length($mtitle)/16 + .02;
	if ("$LARGEVIEW" eq "on") {
		system ("echo '1.0 $titlepos 18 0 0 6 $mtitle' | pstext -Jx1 -R0/14/0/10 -P -G0 -O -Y-1.5  >> $psfile");
	} else {
		system ("echo '1.0 $titlepos 18 0 0 6 $mtitle' | pstext -Jx1 -R0/8.5/0/10 -P -G0 -O -Y-1.5  >> $psfile");
        }
	
}

sub make_legend_file {

	local $ltext, $line, @legendtext;
	local $ypos, $legendxpos;
	local $symbolinfo, @symbolinfo;

        $gmtpath = "/usr/local/GMT/bin";
        if (-e "/opt/csw/bin/convert") {
                $impath = "/opt/csw/bin";
        } else {
                $impath = "/usr/bin";
        }

        $ENV{PATH} = "$gmtpath:$impath:$ENV{PATH}";
        $ENV{TZ}="UTC";


        system("gmtset DOTS_PR_INCH 72 ANOT_FONT_SIZE 10 HEADER_FONT_SIZE 18 PAPER_MEDIA a4+ PLOT_DEGREE_FORMAT '+ddd:mm:ss' XY_TOGGLE TRUE MEASURE_UNIT inch X_ORIGIN 1 Y_ORIGIN 1.5 PAGE_COLOR 255/255/255");

	$ypos = 7.75;
	$legendxpos = 0.5;

	# if legend command...
	if ("$LEGENDCMD" ne "") { 
		# parse off new ypos and command
		($ypos, $cmd) = split(/;/, $LEGENDCMD, 2);
		system("$cmd >> $pslegend");
	}

	@symbolinfo = split(/\n/, $LEGEND); 

	foreach $symbolinfo (@symbolinfo) {
		&make_legend_symbol_table;
	}

	if ("$LEGENDTEXT" ne "") {
		$ltext = $LEGENDTEXT;
		$ltext=~ s#%lt#<#g;
		$ltext=~ s#%gt#>#g;
		@legendtext = split(/\n/, $ltext);
		foreach $line (@legendtext) {
			$ypos -= 0.20; 
			system("echo '$ypos $legendxpos 12 0 1 1 $line' | pstext -Jx1 -R0/8.5/0/11 -G0 -O -K -P -N >> $pslegend"); 
		}	
	}
}

sub make_legend_symbol_table {

# WARNING!! - hideous logic in this function to make the legend look good

	local ($maxv, $maxh, $maxsize, $h, $v) = () ;
	local ($file, @specs, $spec, $name, $symbol, $hlabel, $vlabel) = () ;
	local ($maxsize, $yhlabel, $xvlabel) = () ;
	local ($vxpos, $hxpos, $xinc, $yinc) = ();
	local ($xpos) = ();
	local ($second_column) = ();
	local (@symbolinfo) = ();
	local ($count) = ();
	local ($color, $size, $symbol) = ();

	# using 3 globals in this function, $symbolinfo, $ypos and $pslegend.

	@symbolinfo = split (/:/, $symbolinfo); 

	# scale legend
	$maxv = 0;
	$maxh = 0;
	$maxsize = 0;
	foreach $file (@symbolinfo) {
		@specs = split (/;/, $file);
		foreach $spec (@specs) { 
 			if ($spec =~ /hlabel=/) {
				($name,$hlabel) = split (/=/,$spec,2);
				$maxh=length($hlabel)>$maxh?length($hlabel):$maxh;
			} elsif ($spec =~ /vlabel=/) {
				($name,$vlabel) = split (/=/,$spec,2);
				$maxv=length($vlabel)>$maxv?length($vlabel):$maxv;
 			} elsif ($spec =~ /symbol=/) { 
				($name,$symspec) = split (/=/,$spec,2);
				($color, $size, $symbol) = 
					&get_color_size_symbol($symspec); 
				$maxsize = $size>$maxsize?$size:$maxsize;
			}
		}
	}
	# ypos of horizontal legend label
	$yhlabel = $ypos; 

	# xpos of vertical legend label
	$xvlabel = $legendxpos;

	# y increment a function of max symbol size
	$yinc = $maxsize * 1.5;  

	# x increment a function of max horizontal label length
	$xinc = ($maxh+1) / 12.;  

	# starting ypos of legend symbols
	$ypos = $yhlabel - $yinc;

	# starting xpos of legend symbols and labels
	$hxpos = $xvlabel + $maxv/12 + $maxsize + 0.25;

	# if legend is a single vertical column and rows > 10, do two columns
	# if $second_column != 0, this condition is true... 
	$second_column = 0;
	if (!($symbolinfo=~/hlabel=/) && ($symbolinfo=~/vlabel=/) && ($#symbolinfo>10)) {
		$second_column = $xvlabel + $maxsize + $maxv/12 + 1.25;
	}

	# do legend labels, and hash positions for symbols...
	$count = 0;
	foreach $file (@symbolinfo) {
		$count++;
		@specs = split (/;/, $file);
		foreach $spec (@specs) { 
 			if ($spec =~ /hlabel=/) {
				# for horizontal, set xpos
				($name,$hlabel) = split (/=/,$spec,2);
				$hlabel =~ s/\%lt/</g;
				$hlabel =~ s/\%gt/>/g;
				if (! defined $h{$hlabel} && ("$hlabel" ne "")) {
					$h{$hlabel} = $hxpos; 
					system("echo '$yhlabel $hxpos 12 0 1 6 $hlabel' | pstext -Jx1 -R0/8.5/0/10 -G0 -O -K -P >> $pslegend"); 
					$hxpos += $xinc; 
				}
			} elsif ($spec =~ /vlabel=/) {
				# for vertical, set ypos
				($name,$vlabel) = split (/=/,$spec,2);
				$vlabel =~ s/\%lt/</g;
				$vlabel =~ s/\%gt/>/g;
				if (!defined $v{$vlabel} && ("$vlabel" ne "")) {
					$v{$vlabel} = $ypos; 
					if ($second_column && 		
						( $count == int $#symbolinfo/2. + 2 )){
						# start second column
						$ypos = $yhlabel - $yinc;
						$vxpos = $second_column; 
					} elsif ($second_column &&
						( $count>int $#symbolinfo/2. + 2)){
						$vxpos = $second_column; 
					} else { $vxpos = $xvlabel; }
					system("echo '$ypos $vxpos 12 0 1 4 $vlabel' | pstext -Jx1 -R0/8.5/0/10 -G0 -O -K -P >> $pslegend"); 
					$ypos -= $yinc; 
				}
			}
		}
	}
	 
	# do legend symbols
	
	# starting ypos and xpos of legend symbols, used if one dimension legend
	$ypos = $yhlabel - $yinc;
	$xpos = $hxpos;

	if ($second_column) { 
		# increment to where the symbol should be
		$second_column += $maxv/12 + $maxsize + 0.25;
	}
	$count = 0;
	foreach $file (@symbolinfo) {
		$count++;
		@specs = split (/;/, $file);
		foreach $spec (@specs) { 
 			if ($spec =~ /hlabel=/) {
				($name,$hlabel) = split (/=/,$spec,2);
				$hlabel =~ s/\%lt/</g;
				$hlabel =~ s/\%gt/>/g;
			} elsif ($spec =~ /vlabel=/) {
				($name,$vlabel) = split (/=/,$spec,2);
				$vlabel =~ s/\%lt/</g;
				$vlabel =~ s/\%gt/>/g;
 			} elsif ($spec =~ /symbol=/) { 
				($name,$symspec) = split (/=/,$spec,2);
				($color, $size, $symbol) = 
					&get_color_size_symbol($symspec); 
			}
		}
		if (defined $v{$vlabel}) { $ypos = $v{$vlabel}; }
		if (defined $h{$hlabel}) { $xpos = $h{$hlabel}; }
		if ($second_column && ($count==int $#symbolinfo/2. + 2) ){
		# start second column
			$ypos = $yhlabel - $yinc;
			$xpos = $second_column; 
		} elsif ($second_column && ($count>int $#symbolinfo/2. + 2) ){
			$xpos = $second_column; 
		}
		system ("echo '$ypos $xpos' | psxy -Jx1 -R0/8.5/0/10 -S$symbol$size -G$color -W0.01p/$gray -O -P -K >> $pslegend"); 
	}
	$ypos -= $yinc; 
}

sub get_color_size_symbol
{
	local ($symspec) = @_ ;
	local ($color, $size, $symbol) = ();
	local ($icolor, $isize, $isymbol) = ();
	local (@colors, @bwcolors, @sizes, @symbols) = ();

	# define colors
	$red = "255/0/0";
	$purple = "102/51/153";
	$blue = "0/0/255";
	$yellowgreen = "153/255/0";
	$yellow = "255/255/0";
	$milkchocolate = "204/153/102";
	$palered = "255/153/153";
	$orange = "255/102/0";
	$redorange = "255/51/0";
	$cyan = "51/255/255";
	$lightblue = "102/153/255";
	$pinkmagenta = "255/0/153";
	$palemagenta = "255/153/255";
	$violet = "204/153/255";
	$darkviolet = "102/0/204";
	$violetblue = "102/0/255";
	$darkcyan = "51/153/153"; 
	$darkpink = "102/0/51"; 
	$pinkred = "255/0/102";
	$yellowbrown = "204/102/0";
	$chocolate = "102/51/0";
	$white = "255/255/255";
	$palegray = "204/204/204";
	$lightgray = "153/153/153";
	$gray = "102/102/102";
	$darkgray = "51/51/51";
	$black = "0/0/0";
	$green = "0/255/0";
	$darkgreen = "0/102/0";

	# for rainbows, colors should be incremented by 1
	# for contrast, colors can be incremented by 2
	@colors = ($red,$orange,$yellow,$green,$blue,$purple,
		$cyan,$pinkred,$darkcyan,$milkchocolate,$violetblue,
		$darkgreen,$yellowbrown);
	@bwcolors = ($black, $darkgray, $gray, $lightgray, $palegray, $white,
		$white, $black, $darkgray, $gray);

	@sizes = (0.02, 0.03, 0.05, 0.08, 0.12, 0.17, 0.25, 0.36, 0.50, 0.67);

	# define symbols
	$circle = "c";
	$triangle = "t";
	$square = "s";
	$diamond = "d";
	$star = "a";
	$itriangle = "i";
	$hexagon = "h";
	$cross = "x";
	@symbols = ($circle,$triangle,$square,$diamond,$star,$itriangle,$hexagon);

	if ("$COLORMAP" ne "Y" && "$COLORMAP" ne "") { 
		@colors = @bwcolors;
	} 

	($icolor, $isize, $isymbol) = split (/\//, $symspec);

	# color may be defined by cpt file, not symbol spec
	if ($icolor eq "") { $color = ""; }
	else {
		if ($icolor >= @colors) { $icolor = $icolor % @colors; }
		$color = $colors[$icolor];
	}
	# size and symbol may be defined in input file, not symbol spec
	if ($isize eq "") { $size = ""; }
	else {
		if ($isize >= @sizes) { $isize = $isize % @sizes; }
		$size = $sizes[$isize];
	}
	if ($isymbol eq "") { $symbol = ""; }
	else { 
		if ($isymbol >= @symbols) { $isymbol = $isymbol % @symbols; }
		$symbol = $symbols[$isymbol];
	}

	return ($color, $size, $symbol); 

}

sub gifsize
{
	local ($gif) = @_ ;
	local ($w, $w2, $h, $h2, $width, $size, $type) = () ;

	open (GIF, $gif) ; read (GIF, $type, 3) ;
	seek (GIF, 6, 0) ; read (GIF, $w,  1) ;
	read (GIF, $w2, 1) ;
	$width  = ord ($w) + ord ($w2) * 256 ;
	read (GIF, $h,  1) ; read (GIF, $h2, 1) ;
	$height = ord ($h) + ord ($h2) * 256 ;
	close (GIF) ;

	return ($width, $height, $type) ;

}  # end of sub gifsize

sub process_event_file {
	local ($catalogs, @line, @mag, $catalog) = (); 
	local ($minmag, $maxmag) = (); 
	local ($size, $delta) = ();
	local ($maga, $magb, $magc, $magd, $mage)  = ();
	local ($magacount, $magbcount, $magccount, $magdcount, $magecount, $ecount)  = ();
	local ($idepth, @depths, @depthcolors, $depth)   = ();
	local ($savelegend, $upper) = ();
	local ($catsymbol) = ();
	local ($symsize, $c, $size, $s, $symbol) = (); 
	
	# Rewrite input file EVENT_FILE, which has format "latitude longitude catalog mag depth" into
	# file with GMT symbol information based on magnitude, catalog and depth and generate legend.
	# Append new file and legend to XYINPUT and legend.
	open (EVFILEALL, "< ". $EVENT_FILE);
	$catalogs = "";
	while(<EVFILEALL>) {
		@line = split(/\s+/);
		push @mag, $line[3];
		$catalog = $line[2];
		if ($catalogs =~ /$catalog/) { next; }
		else { $catalogs =  $catalogs . "$catalog:"; }
	}
	close (EVFILEALL);
	@mag = sort by_number @mag;
	$minmag = $mag[0];
	if ("$MAXBUCKET" eq "") { $maxmag = $mag[$#mag]; }
	elsif ("$MAXBUCKET" eq "0") { $maxmag = $mag[$#mag]; }
	else  { $maxmag = $MAXBUCKET; }
	if ("$NBUCKETS" eq "") { $NBUCKETS = 5 };

	if ("$BUCKETDELTA" eq "" || "$BUCKETDELTA" eq "scale to data") {
		$delta = ($maxmag - $minmag)/$NBUCKETS;
		# do a couple of things to try and get nice mag buckets...
		if ($delta > 1.5) {
			# for wide range, guess outliers on the high end
			# so correct for this...
			$maxmag -= 1;
			$delta = ($maxmag - $minmag)/$NBUCKETS;
		}
		$delta = int ($delta * 10.)/10.;
		if ($delta >= 2) { $delta = 2; }
		elsif ($delta >= 1.5) { $delta = 1.5; }
		elsif ($delta >= 1) { $delta = 1; }
		if ($delta < .1) { $delta = .1; }
	}
	else { $delta = $BUCKETDELTA; }

	if ("$MAXBUCKET" eq "" || "$MAXBUCKET" eq "0") { 
		if (int($maxmag) > ($maxmag - $delta)) { $maga = int($maxmag); }
		else { $maga = $maxmag - $delta; }
	} else { $maga = $MAXBUCKET; } 

	$magb = $maga-$delta;
	$magc = $maga-2*$delta;
	$magd = $maga-3*$delta;
	
	if ($NBUCKETS == 1) { $maga = 0 ;}	
	if ($NBUCKETS == 2) { $magb = 0 ;}	
	if ($NBUCKETS == 3) { $magc = 0 ;}	
	if ($NBUCKETS == 4) { $magd = 0 ;}	
	
	@depthcolors = (0, 1, 2, 3, 4, 5);
	@depths = (33, 70, 150, 300, 500, 800);
	@catalogs = split (/:/, $catalogs);	
	$tmpevent_file = "$EVENT_FILE".".tmp";
	open (EVFILEALL, "< ". $EVENT_FILE);
	$magacount = 0;
	$magbcount = 0;
	$magccount = 0;
	$magdcount = 0;
	$magecount = 0;
        $ecount = 1;
	$LEGENDTEXT = "#) time                              lat       lon    mag  depth\n";
	open (TMPEVFILEALL, "> ".$tmpevent_file);
	while(<EVFILEALL>) {
		@line = split(/\s+/);
		$idepth = 0;
		foreach $depth (@depths) {
			if ($line[4] <= $depth) { last; }
			$idepth++;
		}	
		$catsymbol = 0;
		foreach $catalog (@catalogs) {
			if ($line[2] eq $catalog) { last; }
			$catsymbol++;
		}
		$symsize = $MAXSYMSIZE;
		if ($line[3] >= $maga) {
			($c, $size, $s) = get_color_size_symbol("0/$symsize/0");
			$magacount++;
		} elsif ($line[3] >= $magb) {
			$symsize -= 1;
			($c, $size, $s) = get_color_size_symbol("0/$symsize/0");
			$magbcount++;
		} elsif ($line[3] >= $magc) {
			$symsize -= 2;
			($c, $size, $s) = get_color_size_symbol("0/$symsize/0");
			$magccount++;
		} elsif ($line[3] >= $magd) {
			$symsize -= 3;
			($c, $size, $s) = get_color_size_symbol("0/$symsize/0");
			$magdcount++;
		} else { 
			$symsize -= 4;
			($c, $size, $s) = get_color_size_symbol("0/$symsize/0");
			$magecount++;
		}

		($c, $s, $symbol) = get_color_size_symbol("0/0/$catsymbol");

		$depth = $depths[$idepth]-1;
		print TMPEVFILEALL "$line[0] $line[1] $depth $size $ecount $symbol\n";
		$LEGENDTEXT .= sprintf("%-2d) %s %s %-6.2f %-7.2f  %-2.1f   %-4d\n",
                        $ecount,$line[5],$line[6],$line[0],$line[1],$line[3],$line[4]);
                $ecount++;

	}
	if ("$LABELS" ne "on" || "$LABELSCHECKED" ne "on") {
                $LEGENDTEXT = "";
        }
	close (EVFILEALL);
	close (TMPEVFILEALL);
	$event_file_gmt = "$EVENT_FILE.gmt.".$$;
	if ("$LABELS" ne "on" || "$LABELSCHECKED" ne "on") {
		system ("cat $tmpevent_file | sort -k 4,4r -k 3,3r > $event_file_gmt");
		unlink ($tmpevent_file);
	} else {
		system ("mv $tmpevent_file $event_file_gmt");
	}

	# prepare legend
	$upper = $maga-.1;
	if ($BUCKETDELTA == .1) { $blabel = $upper." ($magbcount)"; }
	else { $blabel = "$magb-$upper ($magbcount)"; }
	$upper = $magb-.1;
	if ($BUCKETDELTA == .1) { $clabel = $upper." ($magccount)"; }
	else { $clabel = "$magc-$upper ($magccount)"; }
	$upper = $magc-.1;
	if ($BUCKETDELTA == .1) { $dlabel = $upper." ($magdcount)"; } 
	else { $dlabel = "$magd-$upper ($magdcount)"; }
	$catsymbol = 0;
	$savelegend = $LEGEND;
	if ($savelegend ne "") { $savelegend .= "\n"; }
	$LEGEND = "";
	foreach $catalog (@catalogs) {
		$symsize = $MAXSYMSIZE;
		if ($NBUCKETS == 1) { 
			$LEGEND = "vlabel=$catalog;hlabel=($magacount Events);symbol=0/$symsize/$catsymbol:"."$LEGEND";
		} elsif ($NBUCKETS > 1) { 
			$LEGEND = "vlabel=$catalog;hlabel=%gt= $maga ($magacount);symbol=0/$symsize/$catsymbol:"."$LEGEND";
		}
		$symsize--;
		if ($NBUCKETS == 2) { 
			$LEGEND = "vlabel=$catalog;hlabel=%lt $maga ($magbcount);symbol=0/$symsize/$catsymbol:"."$LEGEND"; 
		} elsif ($NBUCKETS > 2) { 
			$LEGEND = "vlabel=$catalog;hlabel=$blabel;symbol=0/$symsize/$catsymbol:"."$LEGEND";
		}
		$symsize--;
		if ($NBUCKETS == 3) { 
			$LEGEND = "vlabel=$catalog;hlabel=%lt $magb ($magccount);symbol=0/$symsize/$catsymbol:"."$LEGEND"; 
		} elsif ($NBUCKETS > 3) { 
			$LEGEND = "vlabel=$catalog;hlabel=$clabel;symbol=0/$symsize/$catsymbol:"."$LEGEND";
		}
		$symsize--;
		if ($NBUCKETS == 4) { 
			$LEGEND = "vlabel=$catalog;hlabel=%lt $magc ($magdcount);symbol=0/$symsize/$catsymbol:"."$LEGEND"; 
		} elsif ($NBUCKETS > 4) { 
			$LEGEND = "vlabel=$catalog;hlabel=$dlabel;symbol=0/$symsize/$catsymbol:"."$LEGEND"; 
		}
		$symsize--;
		if ($NBUCKETS == 5) { 
			$LEGEND = "vlabel=$catalog;hlabel=%lt $magd ($magecount);symbol=0/$symsize/$catsymbol:"."$LEGEND";
		}	
		$catsymbol++;
	}

	#$LEGEND = $savelegend . $LEGEND;
	$makelegend = 1;

	if ("$COLORMAP" eq "Y" || "$COLORMAP" eq "") { 
		$LEGENDCMD = "6.25;psscale -D3.5i/7.5i/6i/0.20ih -O -K -B:Depth:/:: -P < /wwwshared/HTML/SeismiQuery/gmtmap/neicdepth.cpt";
		if ("$LABELS" ne "on" || "$LABELSCHECKED" ne "on") {
			$XYINPUT = "file=$event_file_gmt;cptfile=/wwwshared/HTML/SeismiQuery/gmtmap/neicdepth.cpt:";
		} else {
			$XYINPUT = "file=$event_file_gmt;text=on;cptfile=/wwwshared/HTML/SeismiQuery/gmtmap/neicdepth.cpt:";
		}
	} else {
		$LEGENDCMD = "6.25;psscale -D3.5i/7.5i/6i/0.20ih -O -K -B:Depth:/:: -P < /wwwshared/HTML/SeismiQuery/gmtmap/bwneicdepth.cpt";
		if ("$LABELS" ne "on" || "$LABELSCHECKED" ne "on") {
			$XYINPUT = "file=$event_file_gmt;cptfile=/wwwshared/HTML/SeismiQuery/gmtmap/bwneicdepth.cpt:";
		} else {
			$XYINPUT = "file=$event_file_gmt;text=on;cptfile=/wwwshared/HTML/SeismiQuery/gmtmap/bwneicdepth.cpt:";
		}
	}
}  # process_event_file

sub process_station_file {
	local ($icolor, $isize, $tmp1stafile, $tmp2stafile, $savelegend) = (); 
	local ($latitude, $longitude, $station, $net, $net2) = ();
	
	# a STATION_FILE has the format "$latitude $longitude $station $net".
	# Separate this file into multiple files by network
	
	$icolor = 0;
	$isymbol = 0;
	open (STAFILEALL, "< ". $STATION_FILE);
	$tmp1stafile = "$STATION_FILE".".tmp1";
	$tmp2stafile = "$STATION_FILE".".tmp2";
        $savelegend = $LEGEND;
	if ($savelegend ne "") { $savelegend .= "\n"; }
        $LEGEND = "";
        $XYINPUT = "";
        while(<STAFILEALL>) {
                ($latitude, $longitude, $station, $net) = split(/\s+/);
                
                # open and write net file, and append to XYINPUT
                
                open (NETSTAFILE, "> ". $STATION_FILE.".".$net);
                print NETSTAFILE "$latitude $longitude X X $station \n";
                open (TMPSTAFILE, "> ". $tmp1stafile);
                while(<STAFILEALL>) {
                        ($latitude, $longitude, $station, $net2) = split(/\s+/);
                        if ($net2 eq $net) {
                                print NETSTAFILE 
			"$latitude $longitude X X $station \n";
                        } else {
                                print TMPSTAFILE $_;
                        }
                }
                $XYINPUT .= "file=$STATION_FILE.$net;symbol=$icolor/4/$isymbol;text=on:";
                $LEGEND .= "vlabel=$net;symbol=$icolor/4/$isymbol:";
                $icolor += 2;
                $isymbol++;
                close STAFILEALL;
                close NETSTAFILE;
                close TMPSTAFILE;
                rename ($tmp1stafile, $tmp2stafile);
                open (STAFILEALL, "< ". $tmp2stafile);
        }
	#$LEGEND = $savelegend . $LEGEND;
	$makelegend = 1;
	unlink ($tmp1stafile);
	unlink ($tmp2stafile);
        close STAFILEALL;
} # process_station_file

sub print_html_head() {

print << "EOI";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
<!--

function blank_text( text )
{
	text.value = "";
	return true;
}

function set_resize()
{
	for ( var i = 0; i < document.map.SCALING.length; i++) {
		if (document.map.SCALING[i].value == "resize")
			document.map.SCALING[i].checked = true;
		else
			document.map.SCALING[i].checked = false;
	}

	return true;
}

function set_region()
{
	for ( var i = 0; i < document.map.SCALING.length; i++) {
		if (document.map.SCALING[i].value == "region")
			document.map.SCALING[i].checked = true;
		else
			document.map.SCALING[i].checked = false;
	}

	return true;
}

function check_float(text)
{
	number = parseFloat(text.value);
	if (isNaN(number)) {
		text.value = "";
	}
	return true;
}


function verify( theForm )
{
	alertmsg = "";
	for ( var i = 0; i < theForm.SCALING.length; i++) {
		if (theForm.SCALING[i].value == "region" 
				&& theForm.SCALING[i].checked) {
			west = parseFloat(theForm.WEST.value);
			if (isNaN(west)) {
				alertmsg = alertmsg + " Invalid Western boundary"
			}
			else theForm.WEST.value = west;

			east = parseFloat(theForm.EAST.value);
			if (isNaN(east)) {
				alertmsg = alertmsg + " Invalid Eastern boundary"
			}
			else theForm.EAST.value = east;
			south = parseFloat(theForm.SOUTH.value);
			if (isNaN(south)) { 
				alertmsg = alertmsg + " Invalid Southern boundary"
			}
			else theForm.SOUTH.value = south;
			north = parseFloat(theForm.NORTH.value);
			if (isNaN(north)) {
				alertmsg = alertmsg + " Invalid Northern boundary"
			}
			else theForm.NORTH.value = north;
			if (alertmsg) {
				alert(alertmsg);
				return false;
			}

			if (Math.abs(east) > 360) {
				east = (east/360. - Math.floor(east/360.))*360.;
				if (Math.abs (west - east) < 0.00001) {
					west -= 360.;	
				}
			}
			if (Math.abs(west) > 360) {
				west = (west/360. - Math.ceil(west/360.))*360.;
				if (Math.abs (west - east) < 0.00001) {
				east += 360.;	
				}
			}
			if (west > east) {
				if (west > 0) {
					west -=360;
				} else {
					east += 360.;
				}
			}
	
			if (west > east) {
				alertmsg = " Invalid West/East boundary";
				alert(alertmsg);
				return false;
			}
	
			if (Math.abs (east - west) > 360) {
				alertmsg = " Invalid West/East boundary";
				alert(alertmsg);
				return false;
			}
			theForm.WEST.value = String(west);
			theForm.EAST.value = String(east);

			if (south == north) {
				theForm.SOUTH.value = String(-north);
			}
			if (south < -90) { 
				theForm.SOUTH.value = "-90";
			}
			if (north > 90) { 
				theForm.NORTH.value = "90";
			}
	
			if (south > north) {
			// flip
				tmp = theForm.NORTH.value;
				theForm.NORTH.value = theForm.SOUTH.value;
				theForm.SOUTH.value = tmp;
			}
		 }
	}
	if (theForm.Lat1.value != "" || theForm.Lon1.value != "" || theForm.Radius1.value != "") {
		if (theForm.Lat1.value == "" || theForm.Lon1.value == "" || theForm.Radius1.value == "") {
			alertmsg = alertmsg + 
	" All fields in first lat/lon/radius triplet must be filled";
		} else {
			lat = parseFloat(theForm.Lat1.value);
			if (isNaN(lat)) {
				alertmsg = alertmsg + " Invalid Lat1 ";
			} else if (lat > 90 || lat < -90) {
				alertmsg = alertmsg + " Lat1 Outside Valid Range";
			} else {
				lon = parseFloat(theForm.Lon1.value);
				if (isNaN(lon)) {
					alertmsg = alertmsg + " Invalid Lon1";
				} else if (lon > 180 || lon < -180) {
					alertmsg = alertmsg + " Invalid Lon1 (-180 to 180)";
				} else {
					radius = parseFloat(theForm.Radius1.value);
					if (isNaN(radius)) {
						alertmsg = alertmsg + " Invalid Radius1";
					}
				}
			}
		}
	}

	if (theForm.Lat2.value != "" || theForm.Lon2.value != "" || 
			theForm.Radius2.value != "") {
		if (theForm.Lat2.value == "" || theForm.Lon2.value == "" || 
			theForm.Radius2.value == ""){
			alertmsg = alertmsg + 
	" All fields in second lat/lon/radius triplet must be filled";
		} else {
			// check all 
			lat = parseFloat(theForm.Lat2.value);
			if (isNaN(lat)) {
				alertmsg = alertmsg + " Invalid Lat2";
			} else if (lat > 90 ||  lat < -90) {
				alertmsg = alertmsg + " Lat2 Outside Valid Range";
			} else {
				lon = parseFloat(theForm.Lon2.value);
				if (isNaN(lon)) {
					alertmsg = alertmsg + " Invalid Lon2";
				} else if (lon > 180 || lon < -180) {
					alertmsg = alertmsg + " Invalid Lon2 (-180 to 180)";
				} else {
					radius = parseFloat(theForm.Radius2.value);
					if (isNaN(radius)) {
						alertmsg = alertmsg + " Invalid Radius2";
					}
				}
			}
		}
	}	
	if (theForm.Lat3.value != "" || theForm.Lon3.value != "" || 
			theForm.Radius3.value != "") {
		if (theForm.Lat3.value == "" || theForm.Lon3.value == "" || 
			theForm.Radius3.value == ""){
			alertmsg = alertmsg + 
	" All fields in third lat/lon/radius triplet must be filled";
		} else {
			// check all 
			lat = parseFloat(theForm.Lat3.value);
			if (isNaN(lat)) {
				alertmsg = alertmsg + " Invalid Lat3";
			} else if (lat > 90 ||  lat < -90) {
				alertmsg = alertmsg + " Lat3 Outside Valid Range"+lat;
			} else {
				lon = parseFloat(theForm.Lon3.value);
				if (isNaN(lon)) {
					alertmsg = alertmsg + " Invalid Lon3";
				} else if (lon > 180 || lon < -180) {
					alertmsg = alertmsg + " Invalid Lon3 (-180 to 180)";
				} else {
					radius = parseFloat(theForm.Radius3.value);
					if (isNaN(radius)) {
						alertmsg = alertmsg + " Invalid Radius3";
					}
				}
			}
		}
	}
	if (theForm.Lat4.value != "" || theForm.Lon4.value != "" || 
			theForm.Radius4.value != "") {
		if (theForm.Lat4.value == "" || theForm.Lon4.value == "" || 
			theForm.Radius4.value == ""){
			alertmsg = alertmsg + 
	" All fields in third lat/lon/radius triplet must be filled";
		} else {
			// check all 
			lat = parseFloat(theForm.Lat4.value);
			if (isNaN(lat)) {
				alertmsg = alertmsg + " Invalid Lat4";
			} else if (lat > 90 ||  lat < -90) {
				alertmsg = alertmsg + " Lat4 Outside Valid Range"+lat;
			} else {
				lon = parseFloat(theForm.Lon4.value);
				if (isNaN(lon)) {
					alertmsg = alertmsg + " Invalid Lon4";
				} else if (lon > 180 || lon < -180) {
					alertmsg = alertmsg + " Invalid Lon4 (-180 to 180)";
				} else {
					radius = parseFloat(theForm.Radius4.value);
					if (isNaN(radius)) {
						alertmsg = alertmsg + " Invalid Radius4";
					}
				}
			}
		}
	}

	if ((theForm.MAXBUCKET.value != "0") && 
	    (theForm.MAXBUCKET.value != "")) {
		maxbucket = parseFloat(theForm.MAXBUCKET.value);
		if (isNaN(maxbucket)) {
			alertmsg = alertmsg + " Invalid Magnitude "
			theForm.MAXBUCKET.value = "8";
		}
	}

	if (theForm.MAXBUCKET.value == "") {
		theForm.MAXBUCKET.value = "0";
	}

	if (alertmsg) {
		alert(alertmsg);
		return false;
	}

	return true;
}

function print_page(){
	if (window.print)
		window.print();
	else 
		alert("Sorry - you need Netscape4.x to use this print button!");

}

// IAGT 2007
//Called to launch coordinate getter in popup
function getCoordsFromMap() {
//Open map as popup
window.open('/SeismiQuery/IAGT/CoordinateAcquisitionTool/coordinateGetter.html','name','height=600,width=532,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
//return false;
}

// IAGT 2007
//This is called from the popup
function enterCoords(north,south,east,west) {
        document.getElementById("NORTH").value = north;
        document.getElementById("SOUTH").value = south;
        document.getElementById("EAST").value = east;
        document.getElementById("WEST").value = west;
}
// -->
</SCRIPT>
<TITLE>Make Maps</TITLE>
</HEAD>
EOI
}
