if [ $# -lt 1 ]; then 
echo ' ' 
echo 'InterCardiacFramesRegistrationBy4DDeformation_V1_multiResolution'
echo 'input: subject_ID          (i.e. seq; file format seq_seg.img.?)'
echo '       Total years         (optional;             default: 10)' 
echo '       Iter                (optional;             default: 16)'
echo '       LaplSmooth_spatial  ((0.45~0.6)optional;   default: 0.58)'
echo '       LaplSmooth_temporal ((0.1~0.5) optional;   default: 0.10)'
echo '       SmoothingTime       ((optional;            default: 5)'
echo '       XY_image size       ((optional;            default: 256)'
echo ' ' 
echo ' ' 
exit
fi
####################################################
## Register Inter-Cardiac Frames By 4D Deformation #
## File name requirement:                         ##
##    original:  *_seg.img.*                      ##
##    segmented: *_org.img.*                      ##
####################################################


SubjFile="$1"


# total frame number
 Tyears="10" #total year
   if [ "$2". != . ]; 
   then
       Tyears="$2"
   fi 
 echo 'Total years = '$Tyears''


#rename file names for the generality of the following algorithm 
 i="1"
 while [ $i -le $Tyears ]
   do 
     echo 'i='$i''
      cp "${SubjFile}_seg.img.${i}" "Subj_seg.img${i}"
     i=`expr $i + 1`
   done


# action ...
SegmentedSubjFile="Subj_seg.img"


#deformable registration

              #Nov 14 #Oct 14 #Sept23 # Sept 19 (Very Good)
Low_Iter="10" #10     #30     #10     #20 
Mid_Iter="10" #10     #30     #10     #20 
HGH_Iter="16" #16     #30     #15     #30 
  if [ "$3". != . ]; 
  then
      Low_Iter="$3"
      Mid_Iter="$3"
      HGH_Iter="$3"
  fi 
 Mid_FnlID=`expr $Mid_Iter - 1`
 echo 'Mid_FnlID= '$Mid_FnlID''
 HGH_FnlID=`expr $HGH_Iter - 1`
 echo 'HGH_FnlID= '$HGH_FnlID''


 # Laplacian coeff
 laplacianSpatialCoeff="0.58"
   if [ "$4". != . ]; 
   then
       laplacianSpatialCoeff="$4"
   fi 
   echo 'laplacianSpatialCoeff= '${laplacianSpatialCoeff}''
 # laplacianTemporalCoeff
 laplacianTemporalCoeff=""
   if [ "$5". != . ]; 
   then
       laplacianTemporalCoeff="-L$5"
   fi 
   echo 'laplacianTemporalCoeff= '${laplacianTemporalCoeff}''



Low_rad="12" #10->12
Mid_rad="10"
HGH_rad="8"


SmthOption="-E5" # "-E0"
   if [ "$6". != . ]; 
   then
       SmthOption="-E$6"
   fi 
   echo 'SmthOption= '${SmthOption}''

XY_img="256"
   if [ "$7". != . ]; 
   then
       XY_img="$7"
   fi 
   echo 'XY_img= '${XY_img}''


#remove previous results
\rm *sample?.*gz




#Low resoultion: 4
echo ' ' 
echo '*******************************************************************' 
echo 'Low resoultion:'
echo '*******************************************************************' 
echo 'Segmenting '$ImgFile' in low-resolution'

InterCardiacFramesRegistrationBy4DDeformation_V1 $SegmentedSubjFile -R0.050 -i$Low_Iter -r$Low_rad -m0.40 -s4 -c0.5 -S${laplacianSpatialCoeff} ${laplacianTemporalCoeff}   -T${Tyears}   $SmthOption  -X${XY_img} -Y #-Y

 i="0"
 while [ $i -lt $Tyears ]
   do 
     echo 'i='$i''
       splitfile DeformationField.float.img.t$i NoUse 8 Field.4.img.t$i
     i=`expr $i + 1`
   done 
 \rm Res.img.sample4.*



#Mid resoultion: 2
echo ' ' 
echo '*******************************************************************' 
echo 'Middle resoultion:'
echo '*******************************************************************' 
echo 'Segmenting '$ImgFile' in middle-resolution'

InterCardiacFramesRegistrationBy4DDeformation_V1 $SegmentedSubjFile -R0.050 -i$Mid_Iter -r$Mid_rad -m0.40 -s2 -c0.5 -S${laplacianSpatialCoeff} ${laplacianTemporalCoeff}    -F  -T${Tyears}  -C1.0 $SmthOption  -X${XY_img} -Y #-Y

 i="0"
 while [ $i -lt $Tyears ]
   do 
     echo 'i='$i''
       mv Res.img.sample2.${Mid_FnlID}.t${i}            Warped.SubjSeg.img.2.t${i}
       splitfile DeformationField.float.img.t$i NoUse 8 Field.2.img.t$i
     i=`expr $i + 1`
   done 
 \rm Res.img.sample2.*



#High resoultion: 1
echo ' ' 
echo '*******************************************************************' 
echo 'High resoultion:'
echo '*******************************************************************' 
echo 'Segmenting '$ImgFile' in high-resolution' 

InterCardiacFramesRegistrationBy4DDeformation_V1 $SegmentedSubjFile -R0.050 -i$HGH_Iter -r$HGH_rad -m0.40 -s1 -c0.5 -S${laplacianSpatialCoeff}  ${laplacianTemporalCoeff}    -F  -T${Tyears}  -C1.0 $SmthOption  -X${XY_img} -Y #-Y


 i="0"
 while [ $i -lt $Tyears ]
   do 
     echo 'i='$i''
       splitfile DeformationField.float.img.t$i NoUse 8 Field.1.img.t$i
     i=`expr $i + 1`
   done 
 \rm Res.img.sample1.*

 # remove the files that are named in the previous step (resolution 2)
 \rm Warped.SubjOrig.img.2.t? Warped.SubjSeg.img.2.t? Warped.SubjOrig.img.2.t?? Warped.SubjSeg.img.2.t??
 \rm Field.4.img.t?  Field.2.img.t? Field.4.img.t??  Field.2.img.t??



#rm other files
\rm DeformationField.float.img*
\rm MdlImg_*
\rm ObjImg_*
\rm NoUse
\rm MDL.faces.hi
\rm model_segmented.img*
\rm object_cbq.img*
\rm object_segmented.*
\rm model_mask.img*



# Get deformation fields from current frame to next frame 
 i="0"
 while [ $i -lt $Tyears ]
   do
    echo 'i='$i''

     # subject to model 
     iP1=`expr $i + 1`
     iP2=`expr $i + 2`
      iP2Cy=$iP2
      if [ $iP2 -gt $Tyears ]; 
       then
           iP2Cy="1"
       fi 
     mv Field.1.img.t${i}  Field.1.img.${iP1}to${iP2}
     PerformDeformationOnImgUsingVectorField Field.1.img.${iP1}to${iP2} ${SubjFile}_seg.img.${iP2Cy} Warped.seg.${iP2}to${iP1}.img -v${XY_img} -s${XY_img} -I
     PerformDeformationOnImgUsingVectorField Field.1.img.${iP1}to${iP2} ${SubjFile}_org.img.${iP2Cy} Warped.org.${iP2}to${iP1}.img -v${XY_img} -s${XY_img}

    i=`expr $i + 1`
   done


\rm Subj_seg.img*

# -m0.6 -> -m0.4