#!/bin/bash

\rm xyValues.* xyLines.sdds

# Make a distribution of 10k x and y values

sddssampledist xyValues.sdds -samples=10000 \
    -gaussian=column=x,mean=0.002,sigma=0.0005,units=m \
    -uniform=column=y,minimum=-0.003,maximum=0.001,units=m 

# Make a 100x100 2d histogram, then add some noise
# Reorganize it into 100 columns of 100 values
# This simulates a video frame with 100 horizontal lines
sddshist2d xyValues.sdds -pipe=out -column=x,y -xparam=100,-0.01,0.01 -yparam=100,-0.01,0.01 \
    | sddsprocess -pipe "-redefine=column,frequency,frequency 2 2 grndl * abs +" \
    | tee xyValues.h2d \
    | sddsbreak -pipe -rowlimit=100 \
    | sddstranspose -pipe -noOldColumnNames \
    | sddscombine -merge -pipe \
    | sddsprocess -pipe=in xyLines.sdds \
    "-define=column,Index,i_row,type=long" 

# Display the 2d histogram
sddscontour -shade xyValues.h2d "-title=Original noisy image data"

# Display the lineouts
sddsplot "-title=Lineouts from the image data" -column=Index,Column??? xyLines.sdds -sep -same 

# Analyze the lineouts to find parameters of the spot
sddsspotanalysis xyLines.sdds xyLines.span -spotImage=xyLines.spot \
    -imageColumns=Column??? -ROI=x0v=0,x1v=99,y0v=0,y1v=99 -spotROI=xvalue=50,yvalue=50 \
    -background=antiloner,symmetric,antihalo

# plot the spot
sddscontour -shade xyLines.spot "-title=Spot found by sddsspotanalysis"

# print values from analysis
sddsexpand xyLines.span -pipe=out | sddsprintout -pipe -param -width=80
