1#!/bin/tcsh -f
2
3# Jan, 2017: PT reformatted helpfile, which was hard to read in
4# eeeemacs viewer.
5
6@global_parse `basename $0` "$*" ; if ($status) exit 0
7
8set stat = 0
9set CurDir = $PWD
10set tmps = `3dnewid -fun`
11
12goto PARSE_COMMAND
13Ret_PARSE_COMMAND:
14
15goto CHECK_PROGRAMS
16Ret_CHECK_PROGRAMS:
17
18goto SET_VARIABLES
19Ret_SET_VARIABLES:
20
21goto CHECK_EXISTENCE
22Ret_CHECK_EXISTENCE:
23
24goto PRE_XFORM
25Ret_PRE_XFORM:
26
27goto DOWNSAMPLE
28Ret_DOWNSAMPLE:
29
30goto STRIP_EXP_VOL
31Ret_STRIP_EXP_VOL:
32
33goto ALIGN_CENTERS
34Ret_ALIGN_CENTERS:
35
36goto RESAMPLE
37Ret_RESAMPLE:
38
39goto REGISTER
40Ret_REGISTER:
41
42echo "Done."
43goto END
44
45
46PARSE_COMMAND:
47
48   #continue parsing for new options
49   set followers = ()
50   set alli_opt = ""
51   set skstr_opt = ""
52   set folinterp = NN
53   set cleanup = 1
54   set Narg = $#
55   set cnt = 1
56   set cropt = '-coarserot'
57   set prefix = ''
58   set UseWarp = 0
59   set clpbelow = ''
60   set SA_in = ''
61   set EA_in = ''
62   set DownSample = 0
63   set StripSkull = 0
64   set okchangeview = 0
65   set overwrite_resp = ''      # O(verwrite), S(kip) or Q(uit), else ask
66   set no_center = 1;
67   set prexform = ""
68   set OutResOpt = ""
69   set rmlist = ()
70   set echo_opt = ""            # '' or '-echo' so it can propagate
71   while ($cnt <= $Narg)
72      set donext = 1;
73      if ($donext && "$argv[$cnt]" == '-h' || "$argv[$cnt]" == '-help') then
74         goto USAGE
75      endif
76
77      if ($donext && "$argv[$cnt]" == "-echo") then
78         set echo_opt = "-echo"
79         set echo
80         set donext = 0
81      endif
82
83      if ($donext && "$argv[$cnt]" == "-dxyz") then
84         set pLoc = $cnt
85         if ($pLoc == $Narg) then
86            echo "Need value after -dxyz"
87            goto END
88         else
89            @ cnt ++
90            set DownSample = "$argv[$cnt]"
91            if ( ! `ccalc -i "ispositive($DownSample - 0.3)"` || \
92                 ! `ccalc -i "ispositive(4 - $DownSample)"` ) then
93               echo "Error: Bad value for dxyz ($argv[$cnt])"
94               echo ""
95               goto BEND
96            endif
97            set donext = 0
98         endif
99      endif
100      if ($donext && "$argv[$cnt]" == "-init_xform") then
101         set pLoc = $cnt
102         if ($pLoc == $Narg) then
103            echo "Need xform file after -init_xform"
104            goto END
105         else
106            @ cnt ++
107            set prexform = `ParseName -out FullName "$argv[$cnt]"`
108            if ( ! -f $prexform) then
109               echo "$prexform not found"
110               goto BEND
111            endif
112            set donext = 0
113         endif
114      endif
115      if ($donext && "$argv[$cnt]" == "-out_dxyz") then
116         set pLoc = $cnt
117         if ($pLoc == $Narg) then
118            echo "Need value after -out_dxyz"
119            goto END
120         else
121            @ cnt ++
122            set OutResOpt = "$argv[$cnt]"
123            if ( ! `ccalc -i "ispositive($OutResOpt - 0.1)"` || \
124                 ! `ccalc -i "ispositive(4 - $OutResOpt)"` ) then
125               echo "Error: Bad value for out_dxyz ($argv[$cnt])"
126               echo ""
127               goto BEND
128            endif
129            set OutResOpt = "-mast_dxyz $OutResOpt "
130            set donext = 0
131         endif
132      endif
133      if ($donext && "$argv[$cnt]" == "-EA_clip_below") then
134         set pLoc = $cnt
135         if ($pLoc == $Narg) then
136            echo "Need Zmm after -EA_clip_below"
137            goto END
138         else
139            @ cnt ++
140            set clpbelow = "$argv[$cnt]"
141            set donext = 0
142         endif
143      endif
144      if ($donext && "$argv[$cnt]" == "-prefix") then
145         set pLoc = $cnt
146         if ($pLoc == $Narg) then
147            echo "Need a string after -prefix"
148            goto END
149         else
150            @ cnt ++
151            set prefix = `@GetAfniPrefix "$argv[$cnt]"`
152            set donext = 0
153         endif
154      endif
155      if ($donext && "$argv[$cnt]" == "-surf_anat") then
156         set pLoc = $cnt
157         if ($pLoc == $Narg) then
158            echo "Need a volume after -surf_anat"
159            goto END
160         else
161            @ cnt ++
162            set SA_in = "$argv[$cnt]"
163            set donext = 0
164         endif
165      endif
166      if ($donext && "$argv[$cnt]" == "-exp_anat") then
167         set pLoc = $cnt
168         if ($pLoc == $Narg) then
169            echo "Need a volume after -exp_anat"
170            goto END
171         else
172            @ cnt ++
173            set EA_in = "$argv[$cnt]"
174            set donext = 0
175
176            # NIFTI is not handled, warn users  26 Feb 2020 [rickr]
177            if ( `3dinfo -is_nifti $EA_in` == 1 ) then
178               set newpre = `3dinfo -prefix_noext $EA_in`
179               echo "** NIFTI is not currently allowed for -exp_anat, sorry"
180               echo "   consider: 3dcopy $EA_in $newpre"
181               goto END
182            endif
183         endif
184      endif
185
186      if ($donext && "$argv[$cnt]" == "-wd") then
187         set UseWarp = 1;
188         set donext = 0
189      endif
190
191      if ($donext && "$argv[$cnt]" == "-al") then
192         set UseWarp = 2;
193         set donext = 0
194      endif
195
196      if ($donext && "$argv[$cnt]" == "-al_opt") then
197         set pLoc = $cnt
198         if ($pLoc == $Narg) then
199            echo "Need parameter(s) after -al_opt"
200            goto END
201         else
202            @ cnt ++
203            set alli_opt = "$argv[$cnt]"
204            set donext = 0
205         endif
206      endif
207      if ($donext && "$argv[$cnt]" == "-skull_strip_opt") then
208         set pLoc = $cnt
209         if ($pLoc == $Narg) then
210            echo "Need parameter(s) after -skull_strip_opt"
211            goto END
212         else
213            @ cnt ++
214            set skstr_opt = "$argv[$cnt]"
215            set donext = 0
216         endif
217      endif
218      if ($donext && "$argv[$cnt]" == "-align_centers") then
219         set no_center = 0;
220         set donext = 0
221      endif
222
223      if ($donext && "$argv[$cnt]" == "-ok_change_view") then
224         set okchangeview = 1;
225         set donext = 0
226      endif
227
228      if ($donext && "$argv[$cnt]" == "-strip_skull") then
229         set pLoc = $cnt
230         if ($pLoc == $Narg) then
231            echo "Need a parameter after -strip_skull"
232            goto END
233         else
234            @ cnt ++
235            if ("$argv[$cnt]" == "exp_anat") then
236               set StripSkull = 1;
237            else if ("$argv[$cnt]" == "both") then
238               set StripSkull = 3;
239            else if ("$argv[$cnt]" == "surf_anat") then
240               set StripSkull = 2;
241            else if ("$argv[$cnt]" == "neither" || \
242                     "$argv[$cnt]" == "none") then
243               set StripSkull = 0;
244            else
245               echo "Acceptable options for -strip_skull are:"
246               echo "'neither', 'exp_anat', 'surf_anat', or 'both'"
247               goto END
248            endif
249        endif
250        set donext = 0
251      endif
252
253      if ($donext && "$argv[$cnt]" == "-surf_anat_followers") then
254         set pLoc = $cnt
255         if ($pLoc == $Narg) then
256            echo "Need at least a volume after -surf_anat_followers"
257            goto END
258         else
259            @ cnt ++
260            set followers = ($argv[$cnt-])
261            foreach fol ($followers)
262               set exx = `@CheckForAfniDset $fol`
263               if ( $status) then
264                  echo "Error follower dset $fol cannot be found"
265                  echo "Note that option -surf_anat_followers must be "
266                  echo "the last one on the command line"
267                  goto BEND
268               endif
269               if ( $exx < 2) then
270                  echo "Error follower dset $fol appears not to be a dset"
271                  echo "Note that option -surf_anat_followers must be "
272                  echo "the last one on the command line"
273                  goto BEND
274               endif
275            end
276            set cnt = `expr $cnt + $#followers - 1`
277            set donext = 0
278         endif
279      endif
280
281      if ($donext && "$argv[$cnt]" == "-atlas_followers") then
282         set pLoc = $cnt
283
284         if ("$SA_in" == '') then
285            echo "-atlas_followers must follow -surf_anat"
286            goto BEND
287         endif
288
289         set folpath = `ParseName -out AbsPath $SA_in`
290         #echo $folpath
291         set followers = ()
292         if ( -d $folpath ) then
293            cd $folpath
294            # avoid wildcards (maybe we should make an afni_glob program?)
295            set follcand = (`find . -maxdepth 1 -name '*.nii'` \
296                            `find . -maxdepth 1 -name '*.nii.gz'`)
297            #echo $follcand
298            set followers = ()
299            if ($#follcand > 0) then
300               foreach foll ($follcand)
301                  set isatl = `3dinfo -is_atlas $foll`
302                  if ("$isatl" == 1) then
303                     set followers = ($followers $folpath/$foll)
304                  endif
305               end
306            endif
307            cd -
308         endif
309         foreach fol ($followers)
310            set exx = `@CheckForAfniDset $fol`
311            if ( $status) then
312               echo "Error follower dset $fol cannot be verified"
313               goto BEND
314            endif
315            if ( $exx < 2) then
316               echo "Error follower dset $fol appears not to be a dset"
317               goto BEND
318            endif
319         end
320         echo "Adding $followers as followers"
321         set donext = 0
322      endif
323
324      if ($donext && "$argv[$cnt]" == "-followers_interp") then
325         set pLoc = $cnt
326         if ($pLoc == $Narg) then
327            echo "Need an interpolation kernel after -followers_interp"
328            goto END
329         else
330            @ cnt ++
331            set folinterp = $argv[$cnt]
332            if (  $folinterp != NN  && \
333                  $folinterp != linear  && \
334                  $folinterp != cubic  && \
335                  $folinterp != quintic  ) then
336               echo "Interpolation kernel $folinterp is not recognized"
337               echo "Choose one of: NN, linear, cubic, or quintic"
338               echo ""
339               goto END
340            endif
341         endif
342         set donext = 0
343      endif
344
345      if ($donext && "$argv[$cnt]" == "-ncr") then
346         set cropt = '';
347         set donext = 0
348      endif
349
350      if ($donext && "$argv[$cnt]" == "-keep_tmp") then
351         set cleanup = 0;
352         set donext = 0
353      endif
354
355      if ($donext && "$argv[$cnt]" == "-overwrite_resp") then
356         set pLoc = $cnt
357         if ($pLoc == $Narg) then
358            echo "Need O/S/Q/A parameter after -overwrite_resp"
359            goto END
360         else
361            @ cnt ++
362            set or = "$argv[$cnt]"
363            set donext = 0
364            if ( $or != 'O' && $or != 'S' && $or != 'Q' && $or != 'A' ) then
365                echo "** -overwrite_resp parameter '$or'"
366                echo "   should be one of O/S/Q/A"
367                goto END
368            endif
369            set overwrite_resp = $or
370         endif
371      endif
372
373      #ADD NO NEW OPTIONS BELOW THIS LINE
374
375      if ($donext == 1 && $cnt == 1) then
376         set EA_in = "$argv[$cnt]"
377         echo "Note: Guessing that '$EA_in' is the experiment anatomy."
378         echo "Better use -exp_anat flag in your command."
379         echo ""
380         set donext = 0
381      endif
382
383      if ($donext == 1 && $cnt == 2) then
384         set SA_in = "$argv[$cnt]"
385         echo ""
386         echo "Note: Guessing that '$SA_in' is the surface anatomy."
387         echo "Better use -surf_anat flag in your command."
388         echo ""
389         set donext = 0
390      endif
391
392      if ($donext == 1 && $cnt == 3) then
393         #probably the dxyz parameter
394         set DownSample = $argv[$cnt]
395         if ("$DownSample" =~ *[^0-9]*) then
396            echo "Error: Option '$argv[$cnt]' not understood"
397            echo ""
398            goto BEND
399         endif
400         if ($DownSample < 0.3 || $DownSample > 4) then
401            echo "Error: Option '$argv[$cnt]' not understood"
402            echo ""
403            goto BEND
404         endif
405         set donext = 0
406      endif
407      if ($donext == 1) then
408         echo "Error: Option or parameter '$argv[$cnt]' not understood"
409         apsearch -popt `basename $0` -word $argv[$cnt]
410         goto BEND
411      endif
412
413      @ cnt ++
414   end
415   if ($cnt < 2) then
416      echo "Error: Too few parameters. See `basename $0` -help"
417      goto BEND
418   endif
419
420   if ("$SA_in" == "" || "$EA_in" == "") then
421      echo "Error: Failed to find surface and/or experiment anatomies on command line."
422      goto BEND
423   else
424         set ExpAnatPrefix = `@GetAfniPrefix $EA_in`
425         set ExpAnatView = `@GetAfniView $EA_in`
426         set SA_in_noaext = `ParseName -out FNameNoAfniExt $SA_in`
427         set SA_in_orig = ''
428         set SA_in_aview = `3dinfo -av_space $SA_in`
429         if ( "`3dinfo -is_nifti $SA_in`" == 1 ) then
430            set SA_in_orig = $SA_in
431            3dcopy $SA_in $SA_in_noaext$SA_in_aview
432            set SA_in = $SA_in_noaext$SA_in_aview
433            set rmlist = ($rmlist $SA_in_noaext$SA_in_aview.????*)
434         endif
435         set SurfAnatPrefix = `@GetAfniPrefix $SA_in`
436         set SurfAnatView = `@GetAfniView $SA_in`
437         set SurfAnatPath = $SA_in:h
438         if ( $SurfAnatPath == $SA_in:t ) then
439            #no path given
440            set SurfAnatPath = .
441         endif
442   endif
443
444   goto Ret_PARSE_COMMAND
445
446SET_VARIABLES:
447   set ExpAnat = $ExpAnatPrefix$ExpAnatView
448   set SurfAnat = $SurfAnatPrefix$SurfAnatView
449   set Resampled = 0 #flag set when resampling is done
450   set SkullStripped = 0
451   goto Ret_SET_VARIABLES
452
453PRE_XFORM:
454   set Skip = 0
455   set nopre_anat_in = $SurfAnat
456   if ($prexform != '') then
457      echo "applying initial xform"
458      cd $SurfAnatPath
459      3dWarp   -overwrite -matvec_out2in $prexform \
460               -prefix pre.${tmps}.${SurfAnatPrefix} $SurfAnat
461      #This operation is the same as
462      #  cat_matvec $prexform -ONELINE > d1.$prexform
463      #  3dAllineate -1Dmatrix_apply d1.$prexform \
464      #            -prefix Horse2 struct+orig.
465      # The 3dAllineate is the way users should verify
466      # if their init_xform is good.
467      set SurfAnatPrefix = pre.${tmps}.${SurfAnatPrefix}
468      set SurfAnat = $SurfAnatPrefix$SurfAnatView
469   endif
470   goto Ret_PRE_XFORM
471
472DOWNSAMPLE:
473   set Skip = 0
474   if ($DownSample != 0) then
475      echo "Downsampling ..."
476      cd $SurfAnatPath
477      set tmpPrefix = $SurfAnatPrefix"_"$DownSample"mm"
478      if (-f $tmpPrefix$SurfAnatView.BRIK) then
479         if ( $overwrite_resp == "" ) then
480            echo ""
481            echo "Warning: $tmpPrefix$SurfAnatView exists in $SurfAnatPath"
482            echo "Enter O/S/Q to Overwrite/Skip/Quit:"
483            set sel = $<
484         else
485            set sel = $overwrite_resp
486         endif
487         if ("$sel" == 'O' || "$sel" == 'o') then
488            rm -f $tmpPrefix$SurfAnatView.BRIK $tmpPrefix$SurfAnatView.HEAD
489         endif
490         if ("$sel" == 'S' || "$sel" == 's') then
491            echo "Skipping downsampling, existing data set $DownSample will be used"
492            set Skip = 1
493         endif
494         if ("$sel" == 'Q' || "$sel" == 'q') then
495            echo "Quitting ..."
496            goto END
497         endif
498         echo ""
499      endif
500      if ($Skip == 0) then
501         echo "running: adwarp -force -prefix $tmpPrefix -apar $SurfAnat -dpar $SurfAnat -dxyz $DownSample -resam Cu"
502         adwarp -force -prefix $tmpPrefix -apar $SurfAnat -dpar $SurfAnat -dxyz $DownSample -resam Cu
503         if (!(-f $tmpPrefix$SurfAnatView.BRIK || -f $tmpPrefix$SurfAnatView.BRIK.gz || -f $tmpPrefix$SurfAnatView.BRIK.Z || -f $tmpPrefix$SurfAnatView.BRIK.bz2 )) then
504            goto NO_DOWNGOOD_BADBAD
505         endif
506
507      endif
508      set SurfAnatPrefix = $tmpPrefix
509      set SurfAnat = $SurfAnatPrefix$SurfAnatView
510      cd $CurDir
511   endif
512   goto Ret_DOWNSAMPLE
513
514STRIP_EXP_VOL:
515   #strip skull off of EXP_VOL
516   set Skip = 0
517   cd $CurDir
518   if ($StripSkull == 1 || $StripSkull == 3 ) then
519      set ExpAnat_NoSkullPrefix = $ExpAnatPrefix"_ns"
520      set ExpAnat_NoSkull = $ExpAnatPrefix"_ns"$ExpAnatView
521      echo "Stripping exp vol ..."
522      if (`@CheckForAfniDset $ExpAnat_NoSkull` > 0) then
523         if ( $overwrite_resp == "" ) then
524            echo ""
525            echo "Warning: $ExpAnat_NoSkull exists in $CurDir"
526            echo "Enter O/S/Q to Overwrite/Skip/Quit:"
527            set sel = $<
528         else
529            set sel = $overwrite_resp
530         endif
531         if ("$sel" == 'O' || "$sel" == 'o') then
532            rm -f $ExpAnat_NoSkull.BRIK* $ExpAnat_NoSkull.HEAD
533         endif
534         if ("$sel" == 'S' || "$sel" == 's') then
535            echo "Skipping stripping, existing data set "
536            echo " $ExpAnat_NoSkull will be used"
537            set Skip = 1
538         endif
539         if ("$sel" == 'Q' || "$sel" == 'q') then
540            echo "Quitting ..."
541            goto END
542         endif
543         echo ""
544      endif
545      if ($Skip == 0) then
546         3dSkullStrip -input $ExpAnat -prefix $ExpAnat_NoSkull $skstr_opt
547         if (`@CheckForAfniDset $ExpAnat_NoSkull` < 2) then
548            goto NO_SS
549         endif
550      endif
551      set ExpAnatPrefix = $ExpAnat_NoSkullPrefix
552      set ExpAnat = $ExpAnat_NoSkull
553      set SkullStripped = 1
554   endif
555   if ($StripSkull == 2 || $StripSkull == 3 ) then
556      set Skip = 0
557      set SurfAnat_NoSkullPrefix = $SurfAnatPrefix"_ns"
558      set SurfAnat_NoSkull = $SurfAnatPrefix"_ns"$SurfAnatView
559      echo "Stripping surf vol ..."
560      if (`@CheckForAfniDset $SurfAnatPath/$SurfAnat_NoSkull` > 0) then
561         if ( $overwrite_resp == "" ) then
562            echo ""
563            echo "Warning: $SurfAnatPath/$SurfAnat_NoSkull exists in $CurDir"
564            echo "Enter O/S/Q to Overwrite/Skip/Quit:"
565            set sel = $<
566         else
567            set sel = $overwrite_resp
568         endif
569         if ("$sel" == 'O' || "$sel" == 'o') then
570            rm -f $SurfAnatPath/$SurfAnat_NoSkull.BRIK* $SurfAnatPath/$SurfAnat_NoSkull.HEAD
571         endif
572         if ("$sel" == 'S' || "$sel" == 's') then
573            echo "Skipping stripping, existing data set $SurfAnatPath/$SurfAnat_NoSkull will be used"
574            set Skip = 1
575         endif
576         if ("$sel" == 'Q' || "$sel" == 'q') then
577            echo "Quitting ..."
578            goto END
579         endif
580         echo ""
581      endif
582      if ($Skip == 0) then
583         3dSkullStrip -input $SurfAnatPath/$SurfAnat -prefix $SurfAnatPath/$SurfAnat_NoSkull $skstr_opt
584         if (`@CheckForAfniDset $SurfAnatPath/$SurfAnat_NoSkull` < 2) then
585            goto NO_SS
586         endif
587      endif
588      set SurfAnatOri = $SurfAnat
589      set SurfAnatPrefix = $SurfAnat_NoSkullPrefix
590      set SurfAnat = $SurfAnat_NoSkull
591      set SkullStripped = 2
592   endif
593   goto Ret_STRIP_EXP_VOL
594
595ALIGN_CENTERS:
596   if ($no_center == 0) then
597      echo "Calculating center shift..."
598      rm -f $ExpAnatPrefix"_shft$ExpAnatView."* >& /dev/null
599      # echo_opt is "-echo" or "", to propagate
600      @Align_Centers -base $SurfAnatPath/$SurfAnat.HEAD \
601                     $echo_opt                          \
602                     -dset $ExpAnat.HEAD
603      set mpref = $ExpAnatPrefix
604      set center_mat = $mpref"_shft.1D"
605      if ( ! -f $center_mat) then
606         echo "Failed to create shift matrix"
607         goto END
608      endif
609      set ExpAnatPrefixOrig = $ExpAnatPrefix
610      set ExpAnatPrefix = $ExpAnatPrefixOrig"_shft"
611      set ExpAnatOrig = $ExpAnat
612      set ExpAnat = $ExpAnatPrefix$ExpAnatView
613   else
614      #Nothing really...
615      set ExpAnatOrig = $ExpAnat
616      set ExpAnatPrefixOrig = $ExpAnatPrefix
617   endif
618   goto Ret_ALIGN_CENTERS
619
620RESAMPLE:
621   #resample Experiment volume to look like SurfAnat
622   set Skip = 0
623   cd $CurDir
624   set ExpAnat_ResampPrefix = $ExpAnatPrefix"_resamp_"$clpbelow
625   set ExpAnat_Resamp = $ExpAnat_ResampPrefix$SurfAnatView
626   echo ""
627   echo "resampling $ExpAnat to match $SurfAnatPath/$SurfAnat"
628   if (`@CheckForAfniDset  $ExpAnat_Resamp.BRIK` > 0) then
629      echo ""
630      echo "Warning: $ExpAnat_Resamp exists in $CurDir"
631      echo "Enter O/S/Q to Overwrite/Skip/Quit:"
632      set sel = $<
633      if ("$sel" == 'O' || "$sel" == 'o') then
634         rm -f $ExpAnat_Resamp.BRIK* $ExpAnat_Resamp.HEAD
635      endif
636      if ("$sel" == 'S' || "$sel" == 's') then
637         echo "Skipping resampling, existing data set $ExpAnat_Resamp "
638         echo "  will be used"
639         set Skip = 1
640      endif
641      if ("$sel" == 'Q' || "$sel" == 'q') then
642         echo "Quitting ..."
643         goto END
644      endif
645      echo ""
646   endif
647   if ($Skip == 0) then
648      set tmpref = '___tmp__rs'"$ExpAnat_ResampPrefix"
649      if ($clpbelow != '') then
650         \rm -rf ${tmpref}*.???? >& /dev/null
651         @clip_volume -below $clpbelow -input $ExpAnat -prefix $tmpref
652         set nxt = "$tmpref""$ExpAnatView"
653         if (!(-f $nxt.BRIK || -f $nxt.BRIK.gz || -f $nxt.BRIK.Z || -f $nxt.BRIK.bz2)) then
654            goto NO_RESAMP
655         endif
656      else
657         set nxt = "$ExpAnat"
658      endif
659      echo "3dresample -master $SurfAnatPath/$SurfAnat -prefix $ExpAnat_ResampPrefix -rmode Cu -inset $nxt"
660      echo ""
661      3dresample -master $SurfAnatPath/$SurfAnat -prefix $ExpAnat_ResampPrefix -rmode Cu -inset $nxt
662      if (  ! -f $ExpAnat_Resamp.HEAD && \
663            !(-f $ExpAnat_Resamp.BRIK || -f $ExpAnat_Resamp.BRIK.gz || \
664              -f $ExpAnat_Resamp.BRIK.Z || -f $ExpAnat_Resamp.BRIK.bz2)) then
665         goto NO_RESAMP
666      endif
667      \rm -rf ${tmpref}*.???? >& /dev/null
668   endif
669   set ExpAnatPrefix = $ExpAnat_ResampPrefix
670   set ExpAnat = $ExpAnat_Resamp
671   set Resampled = 1
672   goto Ret_RESAMPLE
673
674REGISTER:
675   cd $CurDir
676   if ($prefix == '') then
677      set VolRegPrefix = $SurfAnatPrefix"_Alnd_Exp"
678   else
679      set VolRegPrefix = $prefix
680   endif
681   echo ""
682   if (-f $VolRegPrefix$SurfAnatView.BRIK) then
683      echo ""
684      echo "Warning: $VolRegPrefix$SurfAnatView exists in $CurDir"
685      echo "Enter O/Q to Overwrite/Quit:"
686      set sel = $<
687      if ("$sel" == 'O' || "$sel" == 'o') then
688         rm -f $VolRegPrefix$SurfAnatView.BRIK $VolRegPrefix$SurfAnatView.HEAD
689      endif
690      if ("$sel" == 'Q' || "$sel" == 'q') then
691         echo "Quitting ..."
692         goto END
693      endif
694      echo ""
695   endif
696
697   echo "Registering $SurfAnatPath/$SurfAnat to $ExpAnat"
698   if ($UseWarp == 0) then
699      echo "3dvolreg -wtrim -clipit -twopass -twodup -zpad 8 \"
700      echo "         -rotcom -verbose -base $ExpAnat \"
701      echo "         -prefix $VolRegPrefix $SurfAnatPath/$SurfAnat"
702      echo ""
703      if ( -f ${VolRegPrefix}.A2E.1D) then
704        \rm -f ${VolRegPrefix}.A2E.1D
705      endif
706      3dvolreg -wtrim -clipit -twopass \
707               -twodup -zpad 8 -rotcom \
708               -verbose -base $ExpAnat \
709               -1Dmatrix_save  ${VolRegPrefix}.A2E.1D \
710               -prefix ./$VolRegPrefix \
711               -cubic   \
712               $SurfAnatPath/$SurfAnat
713      if ( ! ( -f $VolRegPrefix$SurfAnatView.BRIK || \
714               -f $VolRegPrefix$SurfAnatView.BRIK.gz || \
715               -f $VolRegPrefix$SurfAnatView.BRIK.Z || \
716               -f $VolRegPrefix$SurfAnatView.BRIK.bz2)) then
717         echo "Failed to find $VolRegPrefix$SurfAnatView"
718         goto NO_VOLREG
719      endif
720      #differing views?
721      #mv $SurfAnatPath/$VolRegPrefix* ./
722      if ("$ExpAnatView" !~ "" && "$SurfAnatView" != "$ExpAnatView") then
723         echo "Changing view from $SurfAnatView to $ExpAnatView."
724         if ($okchangeview == 0) then
725            echo "Note that only rigid-body registration was used here."
726            echo "So change of view may not be appropriate."
727         endif
728         3drefit -view $ExpAnatView $VolRegPrefix$SurfAnatView
729         set fview = $ExpAnatView
730      else
731         set fview = $SurfAnatView
732      endif
733   else if ($UseWarp == 1) then
734      set tmpref = '___tmp__zp'"$ExpAnatPrefix$SurfAnatPrefix"
735      \rm -rf ${tmpref}*.???? >& /dev/null
736      set pd = 8
737
738      #echo "3dZeropad -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \"
739      #echo "          -mm -prefix ./${tmpref}_pad $SurfAnatPath/$SurfAnat"
740      #echo ""
741cat <<EOF
7423dZeropad -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \
743          -mm -prefix ./${tmpref}_pad $SurfAnatPath/$SurfAnat
744
745EOF
746
747      3dZeropad   -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \
748                  -mm -prefix ./${tmpref}_pad $SurfAnatPath/$SurfAnat
749      3dZeropad   -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \
750                  -mm -prefix ./${tmpref}_refpad $ExpAnat
751
752      if ( -f ${VolRegPrefix}.A2E.1D) then
753        \rm -f ${VolRegPrefix}.A2E.1D
754      endif
755
756      3dWarpDrive $cropt -twopass -verb -affine_general \
757                  -cubic -final quintic \
758                  -1Dmatrix_save ${VolRegPrefix}.A2E.1D \
759                  -base ./${tmpref}_refpad$SurfAnatView \
760                  -prefix ./${tmpref}_pad_wd ./${tmpref}_pad$SurfAnatView
761      if ($StripSkull >= 2) then
762         #Have to apply same transform to original SurfAnat
763         3dZeropad   -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \
764                     -mm \
765                     -prefix ./${tmpref}_padori $SurfAnatPath/$SurfAnatOri
766         3dWarp   -matparent ./${tmpref}_pad_wd$SurfAnatView \
767                  -quintic \
768                  -prefix ./${tmpref}_pad_wdori ./${tmpref}_padori$SurfAnatView
769         set wdnext = ${tmpref}_pad_wdori
770      else
771         set wdnext = ${tmpref}_pad_wd
772      endif
773
774      3dZeropad   -I -$pd -S -$pd -A -$pd -P -$pd -L -$pd -R -$pd \
775                  -mm \
776                  -prefix ./$VolRegPrefix ${wdnext}$SurfAnatView
777      #copy the WARPDRIVE_MATVEC_ fields to the zero unpadded dude
778      set atrlist = (`3dAttribute -all ${wdnext}$SurfAnatView | \
779                        \sort | \grep WARPDRIVE | \cut -f 1 -d ' '`)
780      foreach atr ($atrlist)
781         if ($atr == WARPDRIVE_INPUT_IDCODE  || \
782             $atr == WARPDRIVE_INPUT_NAME  || \
783             $atr == WARPDRIVE_BASE_IDCODE || \
784             $atr == WARPDRIVE_MATVEC_FOR_000000 || \
785             $atr == WARPDRIVE_MATVEC_INV_000000 ) then
786            3drefit  -atrcopy ${wdnext}$SurfAnatView $atr   \
787                                    $VolRegPrefix$SurfAnatView
788         endif
789      end
790      #differing views?
791      if ("$SurfAnatView" != "$ExpAnatView") then
792         echo "Changing view from $SurfAnatView to $ExpAnatView."
793         3drefit -view $ExpAnatView $VolRegPrefix$SurfAnatView
794         set fview = $ExpAnatView
795      else
796         set fview = $SurfAnatView
797      endif
798      if ( ! ( -f ./$VolRegPrefix$fview.BRIK || \
799               -f ./$VolRegPrefix$fview.BRIK.gz || \
800               -f ./$VolRegPrefix$fview.BRIK.Z || \
801               -f ./$VolRegPrefix$fview.BRIK.bz2)) then
802         goto NO_WARPDRIVE
803      endif
804      if ($cleanup == 1) then
805         \rm -rf ${tmpref}*.???? ${tmpref}*.????.* >& /dev/null
806      endif
807   else
808      set tmpref = '___tmp__zp'"$ExpAnatPrefix$SurfAnatPrefix"
809      \rm -rf ${tmpref}*.???? >& /dev/null
810      set pd = 8
811
812      #echo "3dZeropad -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \"
813      #echo "          -mm -prefix ./${tmpref}_pad $SurfAnatPath/$SurfAnat"
814      #echo ""
815cat <<EOF
8163dZeropad -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \
817          -mm -prefix ./${tmpref}_pad $SurfAnatPath/$SurfAnat
818EOF
819
820      3dZeropad   -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \
821                  -mm -prefix ./${tmpref}_pad $SurfAnatPath/$SurfAnat
822      3dZeropad   -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \
823                  -mm -prefix ./${tmpref}_refpad $ExpAnat
824
825      if ( -f ${VolRegPrefix}.A2E.1D) then
826        \rm -f ${VolRegPrefix}.A2E.1D
827      endif
828
829      3dAllineate -twopass -verb -warp affine_general \
830                  -cubic -final quintic \
831                  -1Dmatrix_save ${VolRegPrefix}.A2E.1D \
832                  -base ./${tmpref}_refpad$SurfAnatView \
833                  -cost lpa   \
834                  -prefix ./${tmpref}_pad_al ./${tmpref}_pad$SurfAnatView  \
835                  $alli_opt
836      if ($StripSkull >= 2) then
837         #Have to apply same transform to original SurfAnat
838         3dZeropad   -I $pd -S $pd -A $pd -P $pd -L $pd -R $pd \
839                     -mm \
840                     -prefix ./${tmpref}_padori $SurfAnatPath/$SurfAnatOri
841         3dAllineate -1Dmatrix_apply ${VolRegPrefix}.A2E.1D \
842            -input ./${tmpref}_padori$SurfAnatView \
843            -prefix ./${tmpref}_pad_alori  \
844            -overwrite  \
845            -final quintic
846         set wdnext = ${tmpref}_pad_alori
847      else
848         set wdnext = ${tmpref}_pad_al
849      endif
850
851      3dZeropad   -I -$pd -S -$pd -A -$pd -P -$pd -L -$pd -R -$pd \
852                  -mm \
853                  -prefix ./$VolRegPrefix ${wdnext}$SurfAnatView
854      #copy the WARPDRIVE_MATVEC_ fields to the zero unpadded dude
855      3drefit  -atrcopy ${wdnext}$SurfAnatView \
856               ALLINEATE_MATVEC_B2S_000000 $VolRegPrefix$SurfAnatView
857      3drefit  -atrcopy ${wdnext}$SurfAnatView \
858               ALLINEATE_MATVEC_S2B_000000 $VolRegPrefix$SurfAnatView
859      #differing views?
860      if ("$SurfAnatView" != "$ExpAnatView") then
861         echo "Changing view from $SurfAnatView to $ExpAnatView."
862         3drefit -view $ExpAnatView $VolRegPrefix$SurfAnatView
863         set fview = $ExpAnatView
864      else
865         set fview = $SurfAnatView
866      endif
867      if ( ! ( -f ./$VolRegPrefix$fview.BRIK || \
868               -f ./$VolRegPrefix$fview.BRIK.gz || \
869               -f ./$VolRegPrefix$fview.BRIK.Z || \
870               -f ./$VolRegPrefix$fview.BRIK.bz2)) then
871         goto NO_WARPDRIVE
872      endif
873      if ($cleanup == 1) then
874         \rm -rf ${tmpref}*.???? ${tmpref}*.????.* >& /dev/null
875      endif
876   endif
877
878   #If have center shift, combine and reapply
879   if ($no_center == 0 || $prexform != '') then
880      echo "Combining xform with init xform and/or shift..."
881      mv ${VolRegPrefix}.A2E.1D ${VolRegPrefix}".A2E_shft".1D
882      if ($no_center == 0 && $prexform != '') then
883         cat_matvec -ONELINE $center_mat -I \
884                             ${VolRegPrefix}".A2E_shft".1D $prexform \
885               > ${VolRegPrefix}.A2E.1D
886         set SurfAnatPrefix = `@GetAfniPrefix $nopre_anat_in`
887         set SurfAnat = $SurfAnatPrefix$SurfAnatView
888      else if ($prexform != '') then
889         cat_matvec -ONELINE ${VolRegPrefix}".A2E_shft".1D $prexform \
890               > ${VolRegPrefix}.A2E.1D
891         set SurfAnatPrefix = `@GetAfniPrefix $nopre_anat_in`
892         set SurfAnat = $SurfAnatPrefix$SurfAnatView
893      else if ($no_center == 0) then
894         cat_matvec -ONELINE $center_mat -I ${VolRegPrefix}".A2E_shft".1D \
895               > ${VolRegPrefix}.A2E.1D
896      else
897         echo "Real bad logic error"
898         goto BEND
899      endif
900      #set echo
901      3dAllineate -master $ExpAnatOrig \
902            -1Dmatrix_apply ${VolRegPrefix}.A2E.1D \
903            -input $SurfAnatPath/$SurfAnat \
904            -prefix ./${VolRegPrefix}$fview  \
905            -overwrite  \
906            $OutResOpt  \
907            -final quintic
908   else
909      if ("$OutResOpt" != "") then
910         #Reproduce output with 3dAllineate, when no shifting is needed.
911         set echo
912         3dAllineate -master $ExpAnatOrig \
913               -1Dmatrix_apply ${VolRegPrefix}.A2E.1D \
914               -input $SurfAnatPath/$SurfAnat \
915               -prefix ./${VolRegPrefix}$fview  \
916               -overwrite  \
917               $OutResOpt  \
918               -final quintic
919      endif
920   endif
921
922FOLLOWERS:
923   #If you have follower dsets, do them here
924   if ($#followers) then
925      foreach fol ($followers)
926         #set echo
927         set folPrefix = `@GetAfniPrefix $fol`
928         echo "Applying same transform to $folPrefix..."
929         set isatl = `3dinfo -is_atlas $fol`
930         if ("$isatl" == 1) then
931            echo "Follower $fol is an atlas, giving it the special treatment."
932            3dAllineate -master $ExpAnatOrig \
933               -1Dmatrix_apply ${VolRegPrefix}.A2E.1D \
934               -input $fol \
935               -prefix ./${folPrefix:r}"_Alnd_Exp"$fview  \
936               -overwrite  \
937               -final NN
938            @Atlasize -dset ./${folPrefix:r}"_Alnd_Exp"$fview
939         else
940         3dAllineate -master $ExpAnatOrig \
941               -1Dmatrix_apply ${VolRegPrefix}.A2E.1D \
942               -input $fol \
943               -prefix ./${folPrefix:r}"_Alnd_Exp"$fview  \
944               -overwrite  \
945               -final $folinterp
946         endif
947      end
948   endif
949
950   #remove _resamp_
951   if ($Resampled == 1 && $cleanup == 1) then
952      echo "removing temporary bricks ..."
953      rm -f $ExpAnat_Resamp".HEAD"
954      rm -f $ExpAnat_Resamp".BRIK"
955      if ($no_center == 0) then
956         rm -f $ExpAnatPrefixOrig"_shft"$ExpAnatView"."* >& /dev/null
957         rm -f ${VolRegPrefix}".A2E_shft".1D >& /dev/null
958      endif
959   endif
960
961   #add a history note
962   3dNotes -h "`basename $0` $argv[*]" $VolRegPrefix$fview
963
964   goto Ret_REGISTER
965
966CHECK_EXISTENCE:
967   if (`@CheckForAfniDset $ExpAnat` < 2) goto NO_EXPANAT
968
969   #if (!(-f $ExpAnat.HEAD || -f $ExpAnat)) goto NO_EXPANAT
970   #if (!(-f $ExpAnat.BRIK || -f $ExpAnat.BRIK.gz || -f $ExpAnat.BRIK.Z || -f $ExpAnat.BRIK.bz2)) then
971   #   goto NO_EXPANAT
972   #endif
973
974   if (`@CheckForAfniDset $SurfAnatPath/$SurfAnat` < 2) goto NO_SURFANAT
975
976   #test -f $SurfAnatPath/$SurfAnat.HEAD || goto NO_SURFANAT
977   #if (!(-f $SurfAnatPath/$SurfAnat.BRIK || -f $SurfAnatPath/$SurfAnat.BRIK.gz || -f $SurfAnatPath/$SurfAnat.BRIK.Z || -f $SurfAnatPath/$SurfAnat.BRIK.bz2)) then
978   #   goto NO_SURFANAT
979   #endif
980
981   if ($no_center) then
982      #check for large center difference
983      set dist_cent = `@Center_Distance -dset $ExpAnat $SurfAnatPath/$SurfAnat`
984      if ($status) then
985         echo "Failed to get center distance"
986         goto BEND
987      endif
988      set ishuge = `ccalc -form int -eval "step($dist_cent - 80)"`
989      set isbig = `ccalc -form int -eval "step($dist_cent - 30)"`
990      if ($isbig) then
991         echo ""
992         echo "*********** Warning *************"
993         echo "Dataset centers are $dist_cent mm"
994         echo "apart. If registration fails, try"
995         echo "adding option -align_centers"
996         #echo "recentering all the input data using" /**** The olde WAY ****/
997         #echo "@Align_Centers -base $SurfAnatPath/$SurfAnat.HEAD -dset $ExpAnat.HEAD -child CHILDREN"
998         #echo "and rerun `basename $0` using the shifted dset"
999         #echo "See @Align_Centers for more help."
1000         #echo "*********************************"
1001         if ($ishuge) then
1002            echo ""
1003            echo "Hit Enter to proceed or ctrl+c to quit."
1004            echo ""
1005            set junk = $<
1006         endif
1007      else
1008         echo "Center distance of $dist_cent mm"
1009      endif
1010      set center_mat = ''
1011   else
1012      #Center shifting is best done after skull stripping, if any is needed
1013   endif
1014
1015   goto Ret_CHECK_EXISTENCE
1016
1017CHECK_PROGRAMS:
1018    set failed_pgms = 0
1019    foreach test_command ( 3dvolreg adwarp @GetAfniView @GetAfniPrefix )
1020      (which $test_command) >& /dev/null
1021
1022      if ( $status ) then
1023          echo "program not found in path: $test_command"
1024          @ failed_pgms ++
1025      endif
1026    end
1027
1028    if ( $failed_pgms ) then
1029      echo "$failed_pgms program(s) not found"
1030      goto END
1031    endif
1032
1033   goto Ret_CHECK_PROGRAMS
1034
1035NO_SURFANAT:
1036   echo ""
1037   echo "Error: No Surface Anatomy $SurfAnat (.HEAD or .BRIK) "
1038   echo "  volume in $SurfAnatPath"
1039   echo ""
1040   goto BEND
1041
1042NO_EXPANAT:
1043   echo ""
1044   echo "Error: No Experiment Anatomy $ExpAnat (.HEAD or .BRIK) "
1045   echo "  in current directory"
1046   echo ""
1047   goto BEND
1048
1049NO_DOWNGOOD_BADBAD:
1050   echo ""
1051   echo "Error: adwarp failed."
1052   echo ""
1053   goto BEND
1054
1055NO_SS:
1056   echo ""
1057   echo "Error: SkullStripping failed."
1058   echo ""
1059   goto BEND
1060
1061NO_VOLREG:
1062   echo ""
1063   echo "Error: 3dvolreg failed."
1064   echo ""
1065   goto BEND
1066
1067NO_WARPDRIVE:
1068   echo ""
1069   echo "Error: 3dwarpdrive failed."
1070   echo ""
1071   goto BEND
1072
1073NO_RESAMP:
1074   echo ""
1075   echo "Error: 3dresample failed."
1076   echo ""
1077   goto BEND
1078
1079USAGE:
1080
1081cat << EOF
1082
1083Usage: 
1084
1085@SUMA_AlignToExperiment                                                 \
1086    <-exp_anat Experiment_Anatomy> <-surf_anat Surface_Anatomy>         \
1087    [dxyz] [-wd] [-prefix PREFIX]                                       \
1088    [-EA_clip_below CLP] [-align_centers] [-ok_change_view]             \
1089    [-strip_skull WHICH]
1090
1091Creates a version of Surface Anatomy that is registered to Experiment
1092Anatomy.
1093
1094Mandatory parameters:
1095
1096    <-exp_anat Experiment_Anatomy>
1097        Name of high resolution anatomical data set in register with
1098        experimental data.
1099
1100    <-surf_anat Surface_Anatomy> 
1101        Path and name of high resolution antomical data set used to
1102        create the surface.
1103
1104  NOTE: In the old usage, there were no -exp_anat and -surf_anat flags
1105  and the two volumes had to appear first on the command line and in
1106  the proper order.
1107
1108Optional parameters:
1109
1110   [-dxyz DXYZ]: This optional parameter indicates that the anatomical
1111                volumes must be downsampled to dxyz mm voxel
1112                resolution before registration. That is only necessary
1113                if 3dvolreg runs out of memory.  You MUST have
1114                3dvolreg that comes with afni distributions newer than
1115                version 2.45l. It contains an option for reducing
1116                memory usage and thus allow the registration of large
1117                data sets.
1118
1119   [-out_dxyz DXYZ]: Output the final aligned volume at a cubic
1120                voxelsize of DXYZmm. The default is based on the grid
1121                of ExpVol.
1122
1123   [-wd]: Use 3dWarpDrive's general affine transform (12 param)
1124                instead of 3dvolreg's 6 parameters.  If the anatomical
1125                coverage differs markedly between 'Experiment Anatomy'
1126                and 'Surface Anatomy', you might need to use
1127                -EA_clip_below option or you could end up with a very
1128                distorted brain.  The default now is to use -coarserot
1129                option with 3dWarpDrive, this should make the program
1130                more robust. If you want to try running without it the
1131                add -ncr with -wd I would be interested in examining
1132                cases where -wd option failed to produce a good
1133                alignment.
1134
1135   [-al]: Use 3dAllineate to do the 12 parameter alignment. Cost
1136                function is 'lpa'.
1137
1138   [-al_opt 'Options for 3dAllineate']: Specify set of options between
1139                quotes to pass to 3dAllineate.
1140
1141   [-ok_change_view]: Be quiet when view of registered volume is
1142                changed to match that of the Experiment_Anatomy, even
1143                when rigid body registration is used.
1144
1145   [-strip_skull WHICH]: Use 3dSkullStrip to remove non-brain tissue
1146                and potentially improve the alignment. WHICH can be
1147                one of 'exp_anat', 'surf_anat', 'both', or 'neither'
1148                (default).  In the first case, the skull is removed
1149                from Experiment_Anatomy dataset, in the second it is
1150                removed from the surf_anat dataset.  With 'both' the
1151                skull is removed from Experiment_Anatomy and
1152                Surface_Anatomy.
1153
1154   [-skull_strip_opt 'Options For 3dSkullStrip']: Pass the options
1155                between quotes to 3dSkullStrip.
1156
1157   [-align_centers]: Adds an additional transformation to align the
1158                volume centers. This is a good option to use when
1159                volumes are severely out of alignment.
1160
1161   [-init_xform XFORM0.1D]: Apply affine transform in XFORM0.1D to
1162                Surface_Anatomy before beginning registration.  After
1163                convergence, combine XFORM.1D and the the registration
1164                matrix to create the output volume To verify that
1165                XFORM0.1D does what you think it should be doing, try:
1166                    3dWarp -matvec_out2in XFORM0.1D           \
1167                           -prefix pre.SurfVol SurfVol+orig
1168                and verify that 'pre.SurfVol+orig' is transformed by
1169                XFORM0.1D as you expected it to be.
1170                XFORM0.1D can be obtained in a variety of ways. One
1171                of which involves extracting it from a transformed
1172                volume.  For example, say you want to perform an
1173                initial rotation that is equivalent to:
1174                    3drotate -matvec_order RotMat.1D          \
1175                          -prefix struct.r struct+orig 
1176                The equivalent XFORM0.1D is obtained with:
1177                    cat_matvec 'struct.r+orig::ROTATE_MATVEC_000000' -I  \
1178                           > XFORM0.1D  
1179                See cat_matvec -help for more details on extracting
1180                appropriate affine transforms from dataset headers.
1181                See also Example 4 below.
1182
1183   [-EA_clip_below CLP]: Set slices below CLPmm in 'Experiment
1184                Anatomy' to zero.  Use this if the coverage of
1185                'Experiment Anatomy' dataset extends far below the
1186                data in 'Surface Anatomy' dataset.  To get the value
1187                of CLP, use AFNI to locate the slice below which you
1188                want to clip and set CLP to the z coordinate from
1189                AFNI's top left corner. Coordinate must be in RAI,
1190                DICOM.
1191
1192   [-prefix PREFIX]: Use PREFIX for the output volume. Default is the
1193                prefix
1194
1195   [-surf_anat_followers Fdset1 Fdset2 ...]: Apply the same alignment
1196                transform to datasets Fdset1, Fdset2, etc.  This must
1197                be the last option on the command line.  All
1198                parameters following it are considered datasets.  You
1199                can transform other follower dsets manually by
1200                executing:
1201                    3dAllineate -master Experiment_Anatomy              \
1202                        -1Dmatrix_apply Surface_Anatomy_Alnd_Exp.A2E.1D \
1203                        -input Fdset                                    \
1204                        -prefix Fdset_Alnd_Exp+orig                     \
1205                        -final NN
1206
1207   [-followers_interp KERNEL]: Set the interpolation mode for the
1208                follower datasets. Default is NN, which is appropriate
1209                for ROI datasets.  Allowed KERNEL values are: NN,
1210                linear, cubic, or quintic
1211                Note: For atlas datasets, the KERNEL is forced to NN
1212                regardless of what you set here.
1213        of the 'Surface Anatomy' suffixed by _Alnd_Exp.
1214
1215   [-atlas_followers]: Automatically set the followers to be atlases
1216                in the directory of -surf_anat. This way all the
1217                parcellations will get aligned to the experiment.
1218
1219   [-echo]: Echo all commands to terminal for debugging
1220
1221   [-keep_tmp]: Keep temporary files for debugging. Note that you
1222                should delete temporary files before rerunning the
1223                script.
1224
1225   [-overwrite_resp RESP]: Answer 'overwrite' questions automatically.
1226                RESP (response) should be one of O/S/Q/A:
1227                    O : overwrite previous result
1228                    S : skip this step (applying previous result)
1229                    Q : quit the script
1230                    A : pause script and ask at each occurance
1231
1232
1233NOTE: You must run the script from the directory where Experiment
1234Anatomy resides.
1235
1236Example 1: For datasets with no relative distortion and comparable
1237           coverage.  Using 6 param. rigid body transform.
1238@SUMA_AlignToExperiment                                   \
1239    -exp_anat DemoSubj_spgrsa+orig.                       \
1240    -surf_anat ../FreeSurfer/SUMA/DemoSubj_SurfVol+orig.
1241
1242Example 2: For datasets with some distortion and different coverage.
1243           Using 12 param. transform and clipping of areas below
1244           cerebellum:
1245@SUMA_AlignToExperiment                                      \
1246    -exp_anat ABanat+orig. -surf_anat DemoSubj_SurfVol+orig. \
1247    -wd -prefix DemoSubj_SurfVol_WD_Alnd_Exp                 \
1248    -EA_clip_below -30
1249
1250Example 3: For two monkey T1 volumes with very different resolutions
1251           and severe shading artifacts.
1252
1253@SUMA_AlignToExperiment                                      \
1254    -surf_anat MOanat+orig. -al                              \
1255    -exp_anat MoExpanat+orig.                                \
1256    -strip_skull both -skull_strip_opt -monkey               \
1257    -align_centers                                           \
1258    -out_dxyz 0.3
1259
1260Example 4: When -exp_anat and -surf_anat have very different
1261           orientations Here is an egregious case where -exp_anat (EA)
1262           was rotated severely out of whack relative to -surf_anat
1263           (SV), AND volume centers were way off.  With the 'Nudge
1264           Dataset' plugin, it was determined that a 60deg.  rotation
1265           got SV oriented more like ExpAnat.  The plugin can be made
1266           to spit out an the 3dRotate command to apply the roation:
1267                3drotate                                        \
1268                    -quintic -clipit                            \
1269                    -rotate 0.00I 60.00R 0.00A                  \
1270                    -ashift 0.00S 0.00L 0.00P                   \
1271                    -prefix ./SV_rotated+orig SV+orig
1272           We will get XFROM.1D from that rotated volume:
1273                cat_matvec 'SV_rotated+orig::ROTATE_MATVEC_000000' -I \
1274                    > XFORM0.1D
1275           and tell @SUMA_AlignToExperiment to apply both center
1276           alignment and XFORM0.1D
1277                @SUMA_AlignToExperiment                        \
1278                    -init_xform XFORM0.1D -align_centers       \
1279                    -surf_anat SV+orig  -exp_anat EA+orig      \
1280                    -prefix SV_A2E_autAUTPre   
1281           Note 1: 'Nudge Dataset' can also be used to get the centers
1282           aligned, but that would be more buttons to press.
1283           Note 2: -init_xform does not need to be accurate, it is
1284           just meant to get -surf_anat to have a comparable
1285           orientation.
1286
1287Global Help Options:
1288--------------------
1289
1290   -h_web: Open webpage with help for this program
1291   -hweb: Same as -h_web
1292   -h_view: Open -help output in a GUI editor
1293   -hview: Same as -hview
1294   -all_opts: List all of the options for this script
1295   -h_find WORD: Search for lines containing WORD in -help
1296                 output. Seach is approximate.
1297
1298More help may be found at:
1299https://afni.nimh.nih.gov/ssc/ziad/SUMA/SUMA_doc.htm
1300
1301Ziad Saad (saadz@mail.nih.gov)
1302SSCC/NIMH/ National Institutes of Health, Bethesda Maryland
1303
1304EOF
1305
1306   goto END
1307
1308BEND:
1309   echo "Failed."
1310   set stat = 1
1311   goto END
1312
1313END:
1314   cd $CurDir
1315   if ($cleanup) then
1316      \rm -f pre.${tmps}.* >& /dev/null
1317      foreach ff ($rmlist)
1318         \rm -f $ff >& /dev/null
1319      end
1320   endif
1321   exit $stat
1322