#!/bin/sh
# Sunearcsv_test10_intersection - for program SUNEARCSV
# Author: Andre Latour, Oct  2022
#
  echo "----------------------------------------------------------------------------"
  echo "---  Using suprofcsv, make a simple profile which intersects itself.        "
  echo "---  This test also indicates how to deal with coil surveys where the       "
  echo "---  coils overlap each other (or situations where some parts of the        "
  echo "---  profile just get NEAR other parts of the profile).                     "
  echo "---                                                                         "
  echo "---  Create null traces with straight line XYs alongside that profile.      "
  echo "---  This test generates null traces and XYs with sx=gx and sy=gy.          "
  echo "---  Which means sdel=sx=gx and gdel=gx=gy.                                 "
  echo "---  So all traces would have offset=0 if offset was computed.              "
  echo "---                                                                         "
  echo "---  Importantly: also create station numbers in the Q-file                 "
  echo "---               and in the null traces (use the gaps key name).           "
  echo "---                                                                         "
  echo "---  Use Sunearcsv to assign traces to nearest cdps USING JUST XYs herein.  "
  echo "---                                                                         "
  echo "---  The problem occurs for the trace near the intersection.                "
  echo "---  Note the printout shows it is assigned cdp=40 rather than cdp=8.       "
  echo "---  That is because cdp=40 is nearest to that trace midpoint.              "
  echo "----------------------------------------------------------------------------"
#
 suprofcsv keyx=gx keyy=gy keyp=asis gx=1000,2000,2000,1220,1220 gy=1000,1000,2000,2000,800 \
           gaps=101,201,301,371,541 \
           chordi=100 nmaxa=3 nmaxs=3 chordf=100 \
           qout=testout101.csv
#
 sunull nt=4 ntr=10 >tnull101.su
 sushw <tnull101.su >tnull102.su key=sx,sy,gx,gy,grnlof,gaps a=650,1000,650,1000,65,65 b=100,0,100,0,10,10
#
 sunearcsv qin=testout101.csv <tnull102.su dimx=mgx dimy=mgy >tnear101.su keyp=cdp okeys=cdp 
#
# Use suchw to compute the midpoint XYs and put them into sdel,gdel. 
# And also the midpoint station number and put it into swdep.        
# Note that this is done AFTER sunearcsv just so you are sure that sunearcsv is not using them itself.
# (We just want to be able to print/plot them).
#
 suchw <tnear101.su >tnear102.su key1=sdel,gdel,swdep key2=sx,sy,grnlof key3=gx,gy,gaps b=1,1,1 c=1,1,1 d=2,2,2 
 sugethw <tnear102.su key=sx,sy,gx,gy,sdel,gdel,swdep,cdp,igi,igc
#
# Use sed to remove the first lines (C_SU header lines).
# Use cut with a deliminator of comma to retain only fields 4 and 5
# Use sed to replace every comma with a blank.               
# Use a2b to convert ascii to binary                        
# Use sugethw to output x,y values in binary.                                   
# Use cat to combine the 2 sets of XYs into one file.                           
#
# Use xgraph to plot 2 sets of points with marks of Xs and triangles.
#
 sed '/C_SU/d' testout101.csv >testout102.csv
 cut -d, -f4,5 <testout102.csv >testout103.csv
 sed '1,$ s/,/ /g' testout103.csv >testout103.txt
 a2b <testout103.txt >testout103.bin
#
 sugethw <tnear102.su output=binary key=sdel,gdel >testout104.bin
 cat testout103.bin testout104.bin >testout105.bin
#
 xgraph <testout105.bin n=51,10 linecolor=2,3 mark=2,6 marksize=10,10 linewidth=0 \
         width=1000 height=900 x1beg=0 x1end=2400 x2beg=-400 x2end=2200
