#!/bin/sh
# antipodes_xearth -- Antipodes: Display the antipodes [farthest away points on earth]
# Copyright       : http://www.fsf.org/copyleft/gpl.html
# Author          : Dan Jacobson -- http://jidanni.org/
# Created On      : Mon Oct 15 09:22:46 2001
# Last Modified By: 
# Last Modified On: Mon Jan 17 09:51:47 2011
# Update Count    : 82
# Status          : OK for GNU/Linux...

case "$@" in
    --random) set -- $((RANDOM%180-90)) $((RANDOM%360-180));;
    *--help*|*-\?*) cat <<EOF
Antipodes: Display the antipodes [farthest away points on earth], with
center of image as given on command line.

Introduction: looking at http://www.tuxedo.org/~esr/travelrules.html ,
we see software whiz Eric R. was full of cheer "until a user's group
put me through the special hell that is a twenty-two-hour
Philadelphia-to-Melbourne run in economy class. Never again."

OK, that got me wondering, gosh, no wonder he was feeling run down,
those two places are almost antipodes!  Now how to display them
etc. with vigor?...

So I wrote this program that stands on the shoulders of other programs
I found on my GNU/Linux system.

Results: at a glance one can see that the only signifigant areas of
land with other land under them are the China-Argentina, and
Antartca-Greenland groups.  Note we aren't looking at a transparent
globe!  See below.

EOF
    set introed #so will get usage message
esac

test $# -ne 2 && { cat <<EOF 1>&2
usage: $0 latitude longitude, in form 32 or 32.333 but not 32:20
More options below.
Example: to see antipodes centered at Philadelphia: $0 40 -75
To make a gif: G=gif $0 40 -75; then look in /tmp...
However I don't know why running through ImageMagick's convert and
combine commands bloat the image file so much.

>Because ImageMagick is freeware, it doesn't do LZW compression by default...
>The problem with larger-than-expected GIFS is mentioned in the README.txt
>that comes with ImageMagick...
>You could use PNG instead and get better compression.
>If you must have GIFs, then use a licensed third-party application to
>recompress them. --Glenn

To save the image file. copy it from /tmp before you kill xli :-)

To do a transparent globe instead of antipodes, add C= like:
C= $0 40 -75

$0 --random picks random locations. try
while :;do $0 --random; done

I ought to make better options...
EOF
    test "x$1" = xintroed || echo $0 --help for introduction 1>&2
    exit 11;}
set -e
#calculate coordinates of antipode:
set dummy `awk "BEGIN{print $1*-1,$2+($2>0?-180:180)}"` $1 $2
T=/tmp/antipodes$$
P= #"-proj cyl" #but default orthographic projection is best
XE="xearth -nostars -noshade -grid $P"
: ${G=ppm} #ppm or gif, ppm much faster in "combine", but MUCH larger...
trap "rm $T.[1234].$G" 0 #so copy the tmp file before you exit the
#viewer, if you want it
set -x
#"markerfile not relevant for GIF or PPM output", oh
$XE -pos "fixed $4 $5" -$G > $T.1.$G #make front side
$XE -pos "fixed $2 $3" ${C--rot 180} -$G > $T.2.$G #make back side
convert -flop $T.2.$G $T.3.$G #flop the back side
combine -compose ReplaceGreen $T.3.$G $T.1.$G $T.4.$G #patse together
#somebody tell me better colors please
xli -title "${C-Antipodes} (image center: lat $4, long $5)" $T.4.$G #view it

#to view several random's but not wait for computing the next one to compute:
#while :;do antipodes --random;done&while :;do antipodes --random;done
#but then the new one pops up before were finished looking at the old one...
#hmm perhaps each time view one, create the next in the backgound but don't
#view it... bla bla, etc.

#埔里的對蹠點 Antipode of Puli is 5073-II on
#http://www.omnimap.com/cgi-bin/omni/graphic.pl?par50w.gif
#Dan's antipode is on 5072-I
