1#!/usr/bin/env bash
2# Testing gmt spatial nearest neighbor options
3#
4# Input was made this way.
5#gmt math -T0/9/1 -o1 0 10 RAND = x
6#gmt math -T0/9/1 -o1 0 10 RAND = y
7#gmt math -T0/9/1 -o1 0 100 RAND = z
8#paste x y z > points.txt
9ps=nn.ps
10DATA=$(gmt which -G @nn_points.txt)
11# NN analysis
12gmt spatial -Aa0k -fg $DATA > results.txt
13gmt set MAP_FRAME_TYPE plain
14gmt psxy -R-0.5/10.5/-0.5/10.5 -JM4.5i -P -Bag1 -BWSne $DATA -Sc0.1i -Gred -K -Xc > $ps
15awk '{print $1, $2, NR-1}' $DATA | gmt pstext -R -J -O -K -F+f12+jCB -Dj0.1i >> $ps
16rm -f tmp
17# Draw links of nearest neighbors
18while read x y z w d A B; do
19	echo "> $A to $B" >> tmp
20	let A=A+1
21	let B=B+1
22	sed -n ${A}p $DATA >> tmp
23	sed -n ${B}p $DATA >> tmp
24done < results.txt
25gmt psxy -R -J -O -K -W1p,green tmp >> $ps
26# NN averaging
27gmt spatial -Aa75k -fg $DATA > results.txt
28gmt psxy -R -J -O -K -Bag1 -BWsne $DATA -Sc0.1i -Glightgray -Y4.75i $DATA >> $ps
29awk '{print $1, $2, 0, 150, 150}' results.txt | gmt psxy -R -J -O -K -SE -W0.5p,blue >> $ps
30gmt psxy -R -J -O results.txt -Sc0.1i -Gred >> $ps
31