#!/bin/tcsh -f

if ("$1" == "-h" || "$1" == "-help") then
   goto HELP
endif


set old = 'CA_EZ_v1.3c-May0806'
set new = 'CA_EZ_v1.5-July3107'

set odir = ${new}_DIFF_${old}
test -d $odir || mkdir $odir
if ($status) then
   echo "Failed  to create output dir $odir"
   goto END
endif
set olog = $odir/${new}_DIFF_${old}.log
echo "Date: `date`" >> $olog
echo "PWD: $PWD" >> $olog
echo "host: `whoami`@`hostname -s`" >> $olog
echo "Command: `basename $0` $argv" >> $olog
echo "----------------------" >> $olog
set lold = (${old}/MNIa*.HEAD)
set lnew = (${new}/MNIa*.HEAD)


NAMES:
echo "Name Matching Block" | tee -a $olog
set cntmax = $#lold
if ($#lnew > $cntmax) set cntmax = $#lnew
set cnt = 1
set match = 0
set nomatch = 0
set lmatchnew = ''
set lmatchold = ''
while ($cnt <= $cntmax) 
   set cmpstr = '' 
   set s1 = `@parse_afni_name $lold[$cnt]`
   #try to find a match
   if ( `@CheckForAfniDset ${new}/$s1[2]$s1[3]` == 2) then
      set lmatchnew = ($lmatchnew ${new}/$s1[2]$s1[3]*.HEAD)
      set lmatchold = ($lmatchold $lold[$cnt])
      set str = "Name.....: Match"
      @ match ++
      #More comparisons
      rm -f $odir/$s1[2]"_unq"* >& /dev/null
      3dhistog -unq $odir/$s1[2]"_unqold.1D" $lold[$cnt] >& /dev/null
      3dhistog -unq $odir/$s1[2]"_unqnew.1D" $lmatchnew[$match] >& /dev/null
      diff $odir/$s1[2]"_unqnew.1D" $odir/$s1[2]"_unqold.1D" > $odir/$s1[2]"_unqdiff"
      
      set ato = `3dAttribute -name DATASET_RANK $lold[$cnt]`
      set atn = `3dAttribute -name DATASET_RANK $lmatchnew[$match]`
      set cmpstr = ($cmpstr", Nsb $ato[4] -- $atn[4]")
      set str_Nsb = "Nsb......: No Match! -- Perhaps new PMaps? Make sure you figure out labels in MPMs"
      if ("$ato" == "$atn") then
         set str_Nsb = "Nsb......: Match" 
      endif
      set ato = `3dAttribute -name ORIGIN $lold[$cnt]`
      set atn = `3dAttribute -name ORIGIN $lmatchnew[$match]`
      set cmpstr = ($cmpstr", Origin $ato[3-] -- $atn[3-]")
      set str_Ori = "Origin...: No Match! -- Danger Danger"
      if ("$ato" == "$atn") then
         set str_Ori = "Origin...: Match" 
      endif
      
      set ato = `3dAttribute -name DELTA $lold[$cnt]`
      set atn = `3dAttribute -name DELTA $lmatchnew[$match]`
      set cmpstr = ($cmpstr", Delta $ato[3-] -- $atn[3-]")
      set str_Delta = "Delta....: No Match! -- Danger Danger"
      if ("$ato" == "$atn") then
         set str_Delta = "Delta....: Match" 
      endif
      
      set ato = `3dAttribute -name ORIENT_SPECIFIC $lold[$cnt]`
      set atn = `3dAttribute -name ORIENT_SPECIFIC $lmatchnew[$match]`
      set cmpstr = ($cmpstr", OrientSpec $ato[3-] -- $atn[3-]")
      set str_Orn = "Orient...: No Match! -- Danger Danger"
      if ("$ato" == "$atn") then
         set str_Orn = "Orient...: Match" 
      endif
      
      set ato = `3dAttribute -name BRICK_TYPES $lold[$cnt]`
      set atn = `3dAttribute -name BRICK_TYPES $lmatchnew[$match]`
      set cmpstr = ($cmpstr", Brick Type $ato[3] -- $atn[3]")
      set str_Typ = "Brick Tp.: No Match! -- Perhaps because of more sub-bricks?"
      if ("$ato" == "$atn") then
         set str_Typ = "Brick Tp.: Match" 
      endif
      set ato = `3dAttribute -name BRICK_STATS $lold[$cnt]`
      set atn = `3dAttribute -name BRICK_STATS $lmatchnew[$match]`
      set cmpstr = ($cmpstr", Range $ato[3-] -- $atn[3-]")
      set str_Rng = "Range....: No Match! -- OK if there are new areas"
      if ("$ato" == "$atn") then
         set str_Rng = "Range....: Match" 
      endif
      
   else
      set str = "No match"
      @ nomatch ++
   endif
   
   
   echo "$cnt $cmpstr" | tee -a $olog
   if ($cnt <= $#lold) then
      echo "   old: $lold[$cnt]" | tee -a $olog
   endif
   if ($cnt <= $#lnew) then
      echo "   new: $lnew[$cnt]" | tee -a $olog
   endif
   if ("$str" == "Name.....: Match") then
      echo "   $str" | tee -a $olog
      echo "   $str_Ori" | tee -a $olog
      echo "   $str_Delta" | tee -a $olog
      echo "   $str_Orn" | tee -a $olog
      echo "   $str_Nsb" | tee -a $olog
      echo "   $str_Typ" | tee -a $olog
      echo "   $str_Rng" | tee -a $olog
   endif 
   echo "" | tee -a $olog
   @ cnt ++
end
echo "$#lold old dsets, $#lnew new dsets" | tee -a $olog
echo "$match matching names" | tee -a $olog
echo "$nomatch not matching names" | tee -a $olog
echo "" | tee -a $olog
echo "Pausing:"
echo "Hit Enter to proceed..."
set jnk = $<

INFO:
#Compare the matching sets, making sure that no old labels have changed!
set cnt = 1
foreach dset ($lmatchnew)
   set ato = `@parse_afni_name $lmatchnew[$cnt]`
   set nsb = `3dAttribute -name DATASET_RANK $lmatchold[$cnt]`
   set nsb = $nsb[4]
   set sb = 0
   while ($sb < $nsb)   
      set opref = $odir/$ato[2]"_New-Old_SB$sb"
      rm -f ${opref}* >& /dev/null
      3dcalc -a $lmatchnew[$cnt]"[$sb]" -b $lmatchold[$cnt]"[$sb]" -expr "(a-b)" -prefix $opref
      echo "Zee"
      set res = `3dBrickStat -min -max -sum ${opref}+tlrc `
      echo "Jee"
      echo "Checking New-Old for $ato[2]'[$sb]' Sum $res[3], Min $res[1], Max $res[2]..." | tee -a $olog
      if ($res[3] == 0 && $res[2] == 0) then
         echo "   No difference." | tee -a $olog
      endif
      if ($res[2] != 0) then
         echo "   Potential Bad news in tennis shoes:" | tee -a $olog
         echo "   Examine the datasets." | tee -a $olog
      endif
      if ($res[1] != 0) then
         echo "   Looks like there are new labels." | tee -a $olog
      endif
      @ sb ++
   end
   @ cnt ++
end

echo "----- END ---------" | tee -a $olog

echo "Log file appended in $olog"

goto END

HELP:
   echo " A script to compare two versions of a database, "
   echo " to alert one for potential differences."
   echo " See also @Prep_New_CA_EZ, CA_EZ_Prep.m"
   goto END
   

END:
