# Makefile -- for the GMT antipodes etc. images on my website
# Copyright       : http://www.fsf.org/copyleft/gpl.html
# Author          : Dan Jacobson -- http://jidanni.org/geo/antipodes/
# Created On      : 2002
# Last Modified By: 
# Last Modified On: Mon Jan 17 09:47:41 2011
# Update Count    : 754
# Depends on GMT  : http://gmt.soest.hawaii.edu/

#for Taiwan close up
TT=16 35 6 5
tai_par_arg.ps:
	file=$@ \
	text1="121 24  16 0 6 MC Taiwan" \
	text2="121 24.6 $(TT) Paraguay\n \
	       121 24.7 $(TT) Argentina" \
	common='-Ba30mg30mSW' ./antipodes_gmt 121.3 24.66 0.8
#	D=h common='-Ba30mg30mSW' ./antipodes_gmt 121.3 24.66 0.8
#Debian hasn't high resolution anymore.

#for Paraguay zoomed out
TP=16 0 6 MC
tai_par_arg_wide.ps:
	file=$@ \
	text1="118 27 $(TP) China\n \
	       120.6 23.2 $(TP) Taiwan" \
	text2="122 23.8 $(TP) Paraguay\n \
	      119 24.8 $(TP) Argentina\n \
	      121 29 $(TP) Argentina\n \
	      117.2 21 $(TP) Bolivia\n \
	      125 21 $(TP) Brasil\n \
	      125 29 $(TP) Brasil" \
	common='-Ba5g5SW' ./antipodes_gmt 121.3 24.66 5
#	D=h common='-Ba5g5SW' ./antipodes_gmt 121.3 24.66 5

#to update website images:
#to make png's: copy before exiting viewer
IDR=/home/jidanni/jidanni.org/geo/antipodes/images
web111:$(IDR)/tai_par_arg_wide.png $(IDR)/tai_par_arg.png
$(IDR)/tai_par_arg_wide.png:tai_par_arg_wide.png
	mv $? $@
$(IDR)/tai_par_arg.png:tai_par_arg.png
	mv $? $@
tai_par_arg.png:tai_par_arg.ps
	convert -posterize 3 -crop 0x0 -colors 3 $? $@
tai_par_arg_wide.png:tai_par_arg_wide.ps
	convert -crop 0x0 -colors 4 $? $@

#Hemispheres ----------
h:hemispheres.png
hemispheres.png:hemispheres.ps
#	convert -crop 0x0 -posterize 16 -colors 16 -rotate 90 $? $@
	#made larger PNG.
	#makes 20K PNG:
#	false
	gs -sDEVICE=png16m -sOutputFile=$@ -dNOPAUSE -dBATCH $?
	convert -crop 0x0 -rotate 90 -colors 6 $@ $@
hemispheres.ps:
	./hemispheres 120.87 24.18 13.00 55.58 #Taiwan; Malmo, Sweden

update_prog: hemispheres
	cp $< $(HOME)/website/geo/antipodes/programs/
	cp $<.png $(HOME)/website/geo/antipodes/images
update_makefile_etc:~/jidanni.org/geo/antipodes/programs/worldwetdry.gz
	cp -uv makefile $$HOME/jidanni.org/geo/antipodes/programs/
~/jidanni.org/geo/antipodes/programs/worldwetdry.gz: worldwetdry
	gzip -c $? > $@
#Antipodes of site coordinates
sites:
# Center of Taiwan Monument (Huzishan), and my antipodes, WGS84
	echo -e 120d58\'55.29E 23d58\'25.95N\\n\
	120d51\'57.74E 24d10\'54.14N|cs2cs +proj=latlong -f %f|tee /dev/tty|\
	perl -anwle 'print $$F[0]-180," ",-$$F[1]'|cs2cs +proj=latlong
#no easier way?
#Source, www.sunriver.com.tw atlas, etc.

T=/tmp/t
#Many small circles: to get at a glance the distance of elongated geographical features
_many:
	pscoast $${N--N1} -D$${D=c} -A$${A=333} -W $R $J -K > $T.ps
	if test -n "$(spokes)"; then for i in $(spokes);\
	do project -C121/24 -A$$i -L1/359 -G1;echo \>;done\
	|psxy -M -O $${C+-K} $R $J >> $T.ps; else :; fi
	if test $C;then\
	grdmath $R -I1 $(huzishan84) GDIST 111.13 MUL = $T.grd&&\
	grdcontour  $T.grd $C -A -O $R $J >> $T.ps;else :;fi
	gv $T.ps
c_taiwan:
	make R=-R0/360/-90/90 C=-C1000 J=-JE121/24/25c _many
c_paraguay:
	make R=-R0/360/-90/90 C=-C1000 J=-JE-59/-24/25c _many
c_europe:
	make R=-R-10/40/30/70 C=-C400 J=-Jm1:50000000 _many
c_e_na:
	make N=-Na D=i R=-R-90/-70/30/50 C=-C100 J=-Jm1:10000000 _many
c_ne_na:
	make N=-Na D=i R=-R-90/-70/40/60 C=-C100 J=-Jm1:10000000 _many
c_texas:
	make N=-Na D=i R=-R-98/-90/25/31 C=-C100 J=-Jm1:10000000 _many
c_indonesia:
	make R=-R90/140/-10/10 D=l C=-C200 J=-Jm1:50000000 _many
c_seasia:
	make N=-Na R=-R90/140/-10/25 D=l C=-C200 J=-Jm1:50000000 _many
c_malacca:
	make N=-Na R=-R96/107/0/8 D=i C=-C3180 A=1 J=-Jm1:10000000 _many
c_antarctica:
	make R=-R0/360/-90/-60 C=-C1000 D=i A=11 J=-JE0/-90/15c _many
#Draw great circle from Taiwan that run along landforms, coastlines.
spokes:
	make spokes="24 31.5 34 50 60 74 93 141 147 160" \
	R=-R0/360/-90/90 J=-JE121/24/5c N= A=22222 _many
spokes5:
	set `seq 0 5 180`; make spokes="$$*" \
	R=-R0/360/-90/90 J=-JE121/24/25c N= A=22222 _many
bc_alberta:
	make spokes="31 32" D=i N=a R=-R-121/-111/48/55 C=-C10000 J=-Jm1:10000000 _many

%.png:%.ps
	gs -sDEVICE=pngmono -sOutputFile=$@ -dNOPAUSE -dBATCH $?
	convert -crop 0x0 -rotate 90 $@ $@
	xli $@
%.ps:%
	mv $T.ps $@
#2004.5 lerned about grdmath
#Show details of the half the world away line
huzishan84=120.982025 23.973875 #Center of Taiwan Monument, WGS84, from 'make sites' above
_half.png:
	pscoast -Na -Di -W $R $J -K $A > $T.ps
	grdmath $R -I1 $(huzishan84) GDIST = $T.grd
	grdcontour  $T.grd -C90 -O $R $J >> $T.ps
	gs -sDEVICE=pngmono -sOutputFile=$T.png -dNOPAUSE -dBATCH $T.ps
	convert -crop 0x0 -rotate 90 $T.png $@
	rm $T.grd $T.ps $T.png
	display $@
europe.png: #WORKS
	make R=-R-10/18/32/56 J=-Jm1:70000000 A=-A99 _half.png
	mv _half.png $@
canada_wide.png:
	make R=-R-127/-81/41/66 J=-JT-104/6c A=-A6666 _half.png
	mv _half.png $@
nwusa.png: #WORKS
	make R=-R-125/-112/41/54 J=-Jm1:70000000 _half.png
	mv _half.png $@
e_africa.png: #WORKS
	make R=-R27/50/-26/7 J=-Jm1:70000000 A=-A444 _half.png
	mv _half.png $@
e_antarctica.png:
	make R=-R0/360/-90/-60 J=-JE0/-90/20c A=-B _half.png
	mv _half.png $@

#Draw concentric small circles to find the closest place in China to me.
#Perhaps do for various origin cities
dan84=120.866039 24.181706
_fujian.png:
	> $T.ps
	pscoast -Na -Di -W $R $J -K $A >> $T.ps
	grdmath $R -I3m $(LL) GDIST 111.13 MUL = $T.grd
	grdcontour  $T.grd $C -A -O $R $J >> $T.ps
	gs -sDEVICE=pngmono -sOutputFile=$T.png -dNOPAUSE -dBATCH $T.ps
	convert -crop 0x0 -rotate 90 $T.png $@
	rm $T.grd $T.ps $T.png
	display $@
fujian.png: #WORKS
	make R=-R119/122/24/26 C=-C25 J=-Jm1:4000000 LL="$(dan84)" _$@
	mv _$@ $@
nanridao.png: #blow up
	make R=-R119.2/119.7/24.9/25.4 C=-C5 J=-Jm1:500000 LL="$(dan84)" _fujian.png
	mv _fujian.png $@

#Find wettest and driest great circles passing through Taiwan
wetdry: #Thanks Ben Horner-Johnson. Took 10 minutes at .1 deg resolution
	set $(huzishan84); for i in $$(perl -wle 'for $$x(0..1790){print $$x/10}'); do\
	echo -e $$i\\t$$(project -C$$1/$$2 -A$$i -L0/360 -G.1|\
	gmtselect -R0/360/-90/90 -Di -Ns/k -JE0/-90/1c|wc -l); done|tee $@
dry:wetdry #might have used minmax perhaps
	sort -k 2n $?|sed -n '1p;$$p'|\
	awk 'BEGIN{print "Azm   Dry%"};{printf "%5.1f %.2f\n",$$1,$$2/3600*100}'
plotdry.txt:wetdry #nice, but should I expand the tabs in the output for browsers?
	{ echo "set terminal dumb;unset key;\
	set title 'Great circles through Taiwan (and Paraguay)';\
	set xlabel 'Azimuth'; set ylabel '%Dry';plot '-'";\
	awk '{print $$1,$$2/3600*100}' $?;}|\
	gnuplot|col -b>$@ #I like nice small files.
plotdry.ps:wetdry #needs work
	awk '{print $$1,$$2/3600*100}' $? > $T
	echo "set terminal postscript;plot '$T'"|gnuplot > $@
spokesdry:
	make spokes="59.2 175.9" R=-R0/360/-90/90 J=-JE121/24/3c N= A=22222 _many
#Evenly spaced sampling points on a sphere, using Debian dime package
samples=6 #why the volleyball look at 7?
sphere_gnuplot:
	dxfsphere $(samples)|dxf2vrml|\
	perl -nlwe "BEGIN{print q{unset key;set size square;set ticslevel 0;splot '-'}};\
	print if (/point/../Indexed/)&&/\d/&&s/,//"|gnuplot -persist
/tmp/sphere_sample_points:
	dxfsphere $(samples)|dxf2vrml|\
	perl -nlwe 'use strict;use Math::Trig q{:radial};use Math::Trig;\
	next unless (/point/../Indexed/)&&/\d/;s/,//;my @F=split;\
	@F=cartesian_to_spherical @F;print rad2deg($$F[1])," ",90-rad2deg($$F[2])'>$@
sphere_sample_points_show: /tmp/sphere_sample_points #Good!
	R=-R0/360/-90/90 J=-JA280/30/8c ;\
	pscoast $$R $$J -Dc -W -A22222 -K > $T.ps &&\
	psxy -O -Sp $$R $$J $? >> $T.ps
	gv $T.ps
sphere_sample_points_project: broken!
	dxfsphere 1|dxf2vrml|\
	perl -nlwe 'next unless (/point/../Indexed/)&&/\d/;s/,//;s/^ +//;print'|\
	project -N -C0/0 -A0 #-Fxyzrs #can't figure out how to proceed

#Interesting great circles
#Airports from fgfs.  Below we see where planes would go if they flew great circles
yyz=-79.627148/43.675661#Toronto Lester B Pearson Intl
tpe=121.232824/25.077731#Taipei Chiang Kai Shek Intl
ord=-87.903219/41.980421#Chicago Ohare Intl
_circle:
	pscoast $R $J -Di -Na -W -K > $T.ps
	project  -C$(from) -E$(to) -G1|psxy -O $R $J  >> $T.ps
	gv $T.ps
taibei_chicago:#just skirts North Korea and China
	make R=-R125/139/38/47 J=-JT132/22c from=$(tpe) to=$(ord) _circle
taibei_toronto:#just touches western extremity of Sea of Okhotsk
	make R=-R135/145/50/60 J=-JT140/5c from=$(tpe) to=$(yyz) _circle
