#!/export/share/bin/perl

$[ = 1;			# set array base to 0
$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator

$k9 = 0;
$ktot = 0;

$style = 2;        #  style: 1=points, 2=lines, 3=polygons(fill)
$col = 15;         # color of stroke
$code = "0";       # geographic code 0-9,a-z,A-Z
$name ="GMT";      # flag for name


open(FILE1, ">arc.poly.centers") || die "Can't open file arc.poly.centers\n";

$k = 1;
@lon = ();
@lat = ();

sub MAX {
       local($max) = pop(@_);
       foreach $foo(@_){
        $max = $foo if $max < $foo;
        }
       $max;
}
 

sub MIN {
       local($min) = pop(@_);
       foreach $foo(@_){
        $min = $foo if $min > $foo;
        }
       $min;
}
 
while (<>) {
    chop;	# strip record separator
    @Fld = split(' ', $_, 9999);
     #print "$#Fld : $_";

if ($#Fld == 3) {
        $idno = $Fld[1];
        $k=1;
       #  @lon[$k] = $Fld[2];
       #  @lat[$k] = $Fld[3];
       #  $k++;
       print FILE1 "$idno $Fld[3] $Fld[2]" ;
}



    if ( ($#Fld == 1 )&&  ($Fld[1] eq 'END') && ($k > 1)) {
              $n = $k-1;
              $minlon = &MIN(@lon);
              $maxlon = &MAX(@lon);

              $minlat = &MIN(@lat);
              $maxlat = &MAX(@lat);
                $style = 3;
               if( $idno <  60 ) {$col = 2;  $style = 3;}
               if( $idno >=  60 ) {$col = 12;  $style = 3;}

              # if( $idno == 50 ) {$col = 13; $style = 2;}

               $label = join('_', $name, $idno);

              print ">  $label  $n $style $col $code $minlat $maxlat $maxlon $minlon";
              
            # print " this is the number in the array $k" ;
              for( $i = 1; $i <= $n ; $i++)  { print "@lat[$i] @lon[$i]" } ;
             $k = 1;
             @lon = ();
             @lat = ();
               $col = 15;
                $ktot++;
              }

	if ( ($#Fld == 1) && ($Fld[1] eq '-99999')) {
            $idno = 99999;
               $col = 13;
               $k9++;
             do {
                   $_ = <STDIN>;
                 } until /END/ ;
       
            
	}

    if ($#Fld == 2) {
        @lon[$k] = $Fld[1];
        @lat[$k] = $Fld[2];
       #print " HI: k=$k  $_   @lon[$k]   @lat[$k]      ";

        $k++;
	# print $_;
    }




}
print STDERR  "  $ktot strokes" ;
print STDERR  " the -99999 flag hit $k9 times" ;
