Debian Codegraph @

test/greenspline/gspline_7.sh

raw
1 #!/usr/bin/env bash
2 # Testing 1-D greenspline with mix of data and gradients
3 # This also exercises -Q for 1-D and plots the solution slopes
4 # to show the match the input slopes (arrows).
5
6 # Create 7 data points
7 cat <<- EOF > oneD_data.txt
8 -2.5 4
9 -2 2
10 -1 -0.5
11 0 0
12 1 1
13 1.5 0
14 2 -2
15 EOF
16 # Create 2 gradient constraints
17 cat <<-EOF > oneD_grad.txt
18 0.5 1
19 2.5 -0.5
20 EOF
21 # Fit the data and gradients
22 gmt greenspline oneD_data.txt -AoneD_grad.txt+f0 -Z0 -R-3/3 -I0.01 -Sc -GoneD_out.txt
23 # Just fit the data
24 gmt greenspline oneD_data.txt -Z0 -R-3/3 -I0.01 -Sc -GoneD_noA.txt
25 # Estimate gradient of solution at gradient constraint locations
26 gmt greenspline oneD_data.txt -AoneD_grad.txt+f0 -Z0 -NoneD_grad.txt -Sc -Q -GoneD_slp.txt
27 paste oneD_grad.txt oneD_slp.txt > oneD_grad2.txt
28
29 # Create data and text for slope lines and annotations
30 gmt math -T-2/2/0.5 T = | awk '{printf ">\n-1\t%s\n1\t%s\n", $1, -$1}' > lines.txt
31 cat <<- EOF >> lines.txt
32 > vertical line
33 0 -2
34 0 +2
35 EOF
36 gmt math -T-2/2/0.5 T = | awk '{printf "1 %s %.1lf\n", $1, $1}' > labels.txt
37
38 gmt begin gspline_7 ps
39 gmt plot oneD_data.txt -R-4/4/-5/5 -Jx1i -Sc10p -Gblack -Baf -l"Values"+jBL
40 gmt plot oneD_noA.txt -W3p,gray -l"No slopes used"
41 gmt plot oneD_out.txt -W3p,red -l"With slope constraints"
42 gmt plot oneD_out.txt -W0.25p
43 LEG="-lData-Slopes"
44 LEGS=-l"Solution-Slopes"
45 # Loop over gradients and place grid and labels
46 while read X S X2 Sout; do
47 Y=$(awk -v X=$X '$1==X {print $2}' oneD_out.txt) # finding y-value at gradient
48 # Lay down grid/labels after shifting origin to tangent point
49 gmt plot -W0.25p,blue -X${X}i -Y${Y}i lines.txt
50 the_Sout=$(gmt math -Q ${Sout} NEG =)
51 gmt plot -W0.25p,green ${LEGS} <<- EOF
52 -1 ${the_Sout}
53 1 ${Sout}
54 EOF
55 gmt text -F+f12p+jML -Dj8p labels.txt
56 echo 0 0 1 ${S} | gmt plot -Sv12p+e+s -W1p -Gblack
57 the_X=$(gmt math -Q ${X} NEG =)
58 the_Y=$(gmt math -Q ${Y} NEG =)
59 echo 0 2 s = ${S} | gmt text -F+f12p+jCB -DJ3p
60 # Undo shift and place gradient circle
61 echo $X $Y | gmt plot -Sc10p -W0.5p ${LEG} -X${the_X}i -Y${the_Y}i
62 LEG=
63 LEGS=
64 done < oneD_grad2.txt
65 gmt end