#! /bin/csh -f
# patchcrawl3d - to get local 3d cross-correlations between two volumes at
# a grid of positions.
#
#  $Id: patchcrawl3d,v 391b6e79e7d1 2008/01/04 07:03:38 mast $
#  Log at end of file

nohup
if ($#argv < 16 || $#argv > 20) then
cat <<EOCAT
Usage: 
patchcrawl3d  XSIZE YSIZE ZSIZE  NX NY NZ  XLO XHI YLO YHI ZLO ZHI  MAX_SHIFT \
       FILEA FILEB OUTPUT_FILE [TRANSFORM_FILE ORIGINAL_FILEB BORDERS] \
       [BOUNDARY_MODEL]
Finds the displacement of the volume in FILEB relative to the volume in FILEA,
 in a series of local volumes of size XSIZE by YSIZE by ZSIZE,
 at grid positions determined by:
   NX, NY, NZ: number of positions in X, Y and Z
   XLO, XHI, YLO, YHI, ZLO, ZHI: minimum and maximum coordinates of the block
       from which local volumes will be extracted 
 MAX_SHIFT sets the maximum displacement that will be determined by local 
   search rather than FFT-based cross-correlation
 It places the list of coordinates and displacements in OUTPUT_FILE
EOCAT
exit 1
endif

set tmpdir = /usr/tmp
if ($?IMOD_DIR) then
    setenv PATH "$IMOD_DIR/bin:$PATH"
    if (-e "$IMOD_DIR/bin/settmpdir") source "$IMOD_DIR/bin/settmpdir"
endif

set modelfile = ""

@ nxl = $1; shift
@ nyl = $1; shift
@ nzl = $1; shift
@ nx = $1; shift
@ ny = $1; shift
@ nz = $1; shift
@ xbl = $1 - 1; shift
@ xhi = $1; shift
@ ybl = $1 - 1; shift
@ yhi = $1; shift
@ zbl = $1 - 1; shift
@ zhi = $1; shift
set maxshift = $1; shift
set bsource = ""
set xffile = ""
set srcborders = "0,0,0,0"
#
set nxyz =  `header -si $argv[1] | sed '/[[:cntrl:]]/s///g'`
@ nxt = $nxyz[1]
@ nyt = $nxyz[2]
@ nzt = $nxyz[3]

@ xbh = $nxt - $xhi
@ ybh = $nyt - $yhi
@ zbh = $nzt - $zhi

set axiserr = ""
if ($xbl < 0 || $xbh < 0) set axiserr = "X"
if ($ybl < 0 || $ybh < 0) set axiserr = "Y"
if ($zbl < 0 || $zbh < 0) set axiserr = "Z"
if ($axiserr != "") then
    echo "ERROR: PATCHCRAWL3D - $axiserr COORDINATES OUTSIDE LIMITS OF VOLUME"
    exit 1
endif

# If argument list is even, the last must be a patch model file
#
if ($#argv == 4 || $#argv == 7) then
    set modelfile = $argv[$#argv]
endif

# If a transform file and original volume are entered, pass that on

if ($#argv >= 5) then
    set bsource = $argv[5]
    set xffile = $argv[4]
    
    set needborders = 0
    if ($#argv == 5) then
	# if there are 5 arguments, it is old-style without model, need to 
	# supply borders
	set needborders = 1

    else if ($#argv == 7) then

	# if there are 7 args, the sixth is borders
	set srcborders = $argv[6]
    else

	# if there are 6, see if sixth has commas or not
	set bordertest = `echo $argv[6] | sed '/,/s// /g'`
	if ($#bordertest > 1) then

	    # it is borders
	    set srcborders = $argv[6]
	else

	    set needborders = 1
	    set modelfile = $argv[6]
	endif
    endif
    if ($needborders == "1") then
	set srcborders = "36,36,36,36"
	echo
	echo "WARNING - THIS IS AN OLD-STYLE CALL TO PATCHCRAWL3D WITHOUT"
	echo "  BORDERS FOR THE SOURCE OF THE SECOND FILE; ASSUMING BORDERS $srcborders"
	echo
    endif
endif
#
@ nxtap = ($nxl + 9) / 10
@ nytap = ($nyl + 9) / 10
@ nztap = ($nzl + 9) / 10

corrsearch3d <<EOF
$argv[1]
$argv[2]
$argv[3]
patchtmp.$$
$modelfile
$nxl,$nyl,$nzl
$nx,$ny,$nz
$xbl,$xbh,$ybl,$ybh,$zbl,$zbh
$nxtap,$nytap,$nztap
$maxshift
$bsource
$xffile
$srcborders
EOF
set retval = $status
if (-e patchtmp.$$) \rm patchtmp.$$
exit $retval

#  $Author: mast $
#
#  $Date: 2008/01/04 07:03:38 $
#
#  $Revision: 391b6e79e7d1 $
#
#  $Log$
#  Revision 3.10  2006/02/16 06:47:09  mast
#  Stripped control chars from output of sed/header etc for Windows
#
#  Revision 3.9  2005/12/01 17:38:20  mast
#  Needed to initialize srcborders to legal program entry
#
#  Revision 3.8  2005/11/19 04:31:26  mast
#  Quote path setting to preserve spaces
#
#  Revision 3.7  2004/06/29 03:42:46  mast
#  Added nohup
#
#  Revision 3.6  2004/06/16 17:56:33  mast
#  Added superfluous error checking on limits
#
#  Revision 3.5  2003/10/24 02:14:00  mast
#  source settmpdir to get tmpdir, better for Windows
#
#  Revision 3.4  2003/06/20 20:06:39  mast
#  Let tmpdir be defined and fall back to /tmp if /usr/tmp does not exist
#
#  Revision 3.3  2002/09/06 00:37:42  mast
#  Made it catch error status from corrsearch3d and pass it on
#
#  Revision 3.2  2002/07/24 16:30:41  mast
#  Added ability to detect presence of new border parameter and supply if
#  necessary, to avoid breaking existing command files.
#
#  Revision 3.1  2002/07/21 19:35:13  mast
#  Removed analysis of border positions based on transformation of second
#  volume and passed appropriate parameters to Corrsearch3d to do this instead
#
