#!/usr/local/bin/perl # Author: Dipul Patel # This Perl Script takes a home site chosen by the user, then calculates # the distance from that home site to every site in the system and prints # in a HTML table. #--------------------------------------------------------------------- # Process information for main site #--------------------------------------------------------------------- $lat_long = "/tmp/sitesLL"; $filepath = "/data1/PHL/neighbors"; $query_string = $ENV{'QUERY_STRING'}; @search = split(/&/,$query_string); @info = split(/=/, $search[0]); @siteinfo = split(/-/, $info[1]); print "Content-type: text/html\n\n"; #read in lat and long file open(LL, $lat_long); @lat_long = ; close(LL); #Get long,lat,g,h,j for main site @dataline = grep /^$siteinfo[0]/, @lat_long; @dataparse = split(/\|/, $dataline[0]); ($sitelat, $sitelong) = split(/,/, $dataparse[3]); $g = $dataparse[4]; $h = $dataparse[5]; $j = $dataparse[6]; #--------------------------------------------------------------------- # Process information for every other site, one at a time #--------------------------------------------------------------------- #open directory and get sites names opendir DIR, $filepath; @allfiles = grep !/^\.\.?$/, readdir DIR; closedir DIR; @allfiles = sort @allfiles; $totalsite=@allfiles; @allfiles2=@allfiles; foreach $site(@allfiles) { chomp $site; @frst = split(/[a-z][a-z]/, $site); @cell = split(/[A-Z]/, $frst[1]); push(@othersiteinfo, $cell[0]); } #Open assiociated neighbor list file open(IN, "$filepath/$siteinfo[1]"); @data=; close(IN); #get sector numbers for neighbors foreach $line(@data){ #chomp $line; @information = split (/\|/, $line); push (@neighbor,$information[1]); } $n=-1; $temphigh = 0; while($n<$totalsite){ $n++; @dataline2 = grep /^$othersiteinfo[$n]/, @lat_long; @dataparse2 = split(/\|/, $dataline2[0]); ($sitelat2, $sitelong2) = split(/,/, $dataparse2[3]); $g2 = $dataparse2[4]; $h2 = $dataparse2[5]; $j2 = $dataparse2[6]; #--------------------------------------------------------------------- # calculate distance from main site to other sites #--------------------------------------------------------------------- $a=($g2-$g)*($g2-$g); $b=($h2-$h)*($h2-$h); $c=($j2-$j)*($j2-$j); $abc=($a+$b+$c); $sqabc=sqrt($abc); $distance=(3960*$sqabc); $distance = sprintf "%1.2f", $distance; $distance2=$distance; $distance3=$distance; $distance4=$distance; if ($temphighval >= $temphigh){ $temphigh=$temphighval; } if ( $distance > 10){ next; } # if ( $distance == 0){ # next; # } # The following code, first compares the sector number of the current site to that of the neighbor list, # then it complies 2 arrays, one with all the neighbors, the other array with the other sites. $element = $othersiteinfo[$n]; if (grep {$_ eq $element} @neighbor) { @array3=($allfiles[$n],$distance2); push (@test1, @array3); push (@highval, $distance2); } else{ @array2=($allfiles[$n],$distance); push (@test2, @array2); } }#end whileloop my $done_sort=0; # or some more meaningful name while( $cond) { # probably some code here unless( $done_sort) { sort_the_array( @array); $done_sort=1; } # more code here } #create hash and sorted array for non-neighbors %distance=@test2; @sorted = sort { $distance{$a} <=> $distance{$b} } keys %distance; #extract sector numbers from sitename for use in hyperlinks. $k=0; @nonneighsect=@sorted; for ($h=0; $h<=$#nonneighsect; $h++) { $nonneighsect[$h] =~ s/\w{2}(\d{4})\w+/$1/; } #create hash and sorted array for neighbors %distance2=@test1; @sorted2 = sort { $distance2{$a} <=> $distance2{$b} } keys %distance2; #extract sector numbers from sitename for use in hyperlinks. $j=0; @neighsect=@sorted2; for ($i=0; $i<=$#neighsect; $i++) { $neighsect[$i] =~ s/\w{2}(\d{4})\w+/$1/; } #-------------------- # the following code prints a table, with all the sites sorted first by neighbor, then distance. It also provides links # to automattically calculate distances for other sites, as well as map them. #-------------------- print "\n"; print "Distance Tool\n"; print "\n"; print "
Distance From $siteinfo[1] To:"; # print " 1\n"; # print " 2\n"; # print " 3\n"; print ""; print "
SiteDistanceNeighborMap(sector)\n"; foreach $site3 (@sorted2) { print "
$site3$distance2{$site3}YES\n"; print " 1\n"; print " 2\n"; print " 3\n"; $j++; } foreach $site2 (@sorted) { print "
$site2$distance{$site2}NO\n"; print " 1\n"; print " 2\n"; print " 3\n"; $k++; } print ""; print "
\n"; print "\n"; #### if ( $distance > 10){ next; } #### if (grep {$_ eq $element} @neighbor) { @array3=($allfiles[$n],$distance2); push (@test1, @array3); push (@highval, $distance2); }