#!/bin/tcsh

@global_parse `basename $0` "$*" ; if ($status) exit 0

#set version   = "1.0";  set rev_dat   = "Apr 6, 2020"
#   + start: for qc images of intermed and final align in SSW
#
# ----------------------------------------------------------------

set this_prog = "@djunct_edgy_align_check"
set tpname    = "${this_prog:gas/@djunct_//}"
set here      = "$PWD"


# ----------------------- set defaults --------------------------

set ulay    = ""
set olay    = ""

set odir    = ""
set opref   = ""

set umin    = "2%"
set umax    = "98%"

set Nx      = 6
set Ny      = 1

set refbox_add     = ( "-pass" "-pass" )

set DO_CLEAN = 1

# ------------------- process options, a la rr ----------------------

if ( $#argv == 0 ) goto SHOW_HELP

set ac = 1
while ( $ac <= $#argv )
    # terminal options
    if ( ("$argv[$ac]" == "-h" ) || ("$argv[$ac]" == "-help" )) then
        goto SHOW_HELP
    endif
    if ( "$argv[$ac]" == "-ver" ) then
        goto SHOW_VERSION
    endif

    #  ---------- inputs: required ---------------

    if ( "$argv[$ac]" == "-ulay" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set ulay = "$argv[$ac]"

    else if ( "$argv[$ac]" == "-olay" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set olay = "$argv[$ac]"

    #  ---------- opts ---------------

    else if ( "$argv[$ac]" == "-prefix" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set opref = `basename "$argv[$ac]"`
        set odir  = `dirname  "$argv[$ac]"`

    # [PT: Oct 18, 2018] can get focus box, still centered on FOV
    # center
    else if ( "$argv[$ac]" == "-box_focus_slices" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set refbox_add[1] = "-box_focus_slices"
        set refbox_add[2] = "$argv[$ac]"

    else if ( "$argv[$ac]" == "-ulay_range" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set umin = "$argv[$ac]"
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set umax = "$argv[$ac]"

    # [PT: Dec 30, 2019] extra montage feature: cbar to use
    else if ( "$argv[$ac]" == "-cbar" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set cbar = "$argv[$ac]"

    # [PT: Feb 19, 2020] extra montage feature: nslices in x 
    else if ( "$argv[$ac]" == "-montx" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set Nx = "$argv[$ac]"

    # [PT: Feb 19, 2020] extra montage feature: nslices in x 
    else if ( "$argv[$ac]" == "-monty" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set Ny = "$argv[$ac]"

    else
        echo "\n\n** ERROR: unexpected option #$ac = '$argv[$ac]'\n\n"
        goto BAD_EXIT
        
    endif
    @ ac += 1
end

# =======================================================================
# ============================ ** SETUP ** ==============================
# =======================================================================

if ( "$ulay" == "" ) then
    echo "** ERROR: missing ulay file.  Use '-ulay ..'"
    goto BAD_EXIT
endif

if ( "$olay" == "" ) then
    echo "** ERROR: missing olay file.  Use '-olay ..'"
    goto BAD_EXIT
endif

if ( "$opref" == "" || "$odir" == "" ) then
    echo "** ERROR: missing output dir and/or prefix.  Use '-prefix ..'"
    goto BAD_EXIT
endif

# ===================== output dir + wdir =======================
 
# check output directory, use input one if nothing given

if ( ! -e $odir ) then
    echo "++ Making new output directory: $odir"
    \mkdir -p $odir
endif

# =========================== Actual Plots ==============================

set pppp           = "`3dnewid -fun11`"
set tpref          = "${odir}/__ssw_intermed_${pppp}"

set pref           = "${odir}/${opref}"

@djunct_edgy_align_check               \
    -montx            ${Nx}            \
    -monty            ${Ny}            \
    -ulay_range       "${umin}" "${umax}" \
    -ulay             "${ulay}"        \
    -olay             "${olay}"        \
    -box_focus_slices "${olay}"        \
    -prefix           ${tpref}

2dcat                                  \
    -gap 5                             \
    -gap_col 255 199 11                \
    -nx 1                              \
    -ny 3                              \
    -prefix ${pref}                    \
    ${tpref}*jpg


if ( $DO_CLEAN == 1 ) then
    echo "\n+* Removing temporary files '${tpref}*'\n"
    \rm ${tpref}*
else
    echo "\n+* NOT removing temporary files '${tpref}*'\n"
endif

echo ""
echo "++ DONE! Image output:"
echo "       ${pref}"
echo ""


goto GOOD_EXIT

# ========================================================================
# ========================================================================

SHOW_HELP:
cat << EOF
-------------------------------------------------------------------------

OVERVIEW ~1~

More helpful helpfile description coming (some day...)

This is just a helper script for other things (like SSW).

written by PA Taylor

# --------------------------------------------------------------------

COMMAND OPTIONS ~1~

-hview
-help
-ver
-ulay
-olay
-prefix
-box_focus_slices
-montgap
-cbar
-ulay_range
-montx
-monty
-no_clean

NOTES TO SELF ~1~

If using -box_focus_slices, don't use the AMASK_FOCUS_OLAY keyword,
but instead repeat the name of the olay explicitly.  This is because
this program creates an edgified version of the olay, which gets
passed into @chauffeur_afni, and then using the AMASK* functionality
would try to 'automask' that dset, typically leaves no voxels and
leads to an error.  Repeating the name of the input olay leads to
correct behavior.  (Ask me how I discovered *this* tidbit of
knowledge?)

EOF

# ----------------------------------------------------------------------

    goto GOOD_EXIT

SHOW_VERSION:
   echo "version  $version (${rev_dat})"
   goto GOOD_EXIT

FAIL_MISSING_ARG:
    echo "** ERROR! Missing an argument after option flag: '$argv[$ac]'"
    goto BAD_EXIT

BAD_EXIT:
    exit 1

GOOD_EXIT:
    exit 0
