------------------------------------------------
 The SubjectTools -> Setup Structural Scans menu
 sets up to convert and average raw data sets
 in preparation for surface reconstruction.
------------------------------------------------
When this panel opens, it scans $subject/mri/orig
for subdirectories and makes of list of them at
the left of the panel.  If there are no subdirs,
the list will be blank.

Select an mri/orig subdir containing a single
scan and press the Structual Scan Dir button.
This configures it as a directory to convert
and possibly average.  At any time, you can
press Ignore Dir to unconfigure a directory.
Ignore Dir doesn't touch anything inside the
directory.

Now, select a scan or scan file in the combo box
labeled "Image File:", and press READ HEADER.
This should fill in all the fields below.  
Verify the the fields are correct.

Simple Conversion

After configuring one or more directories,
press CONVERT/AVERAGE.  This does a simple
average generating the following images:

 images: $SUBJECTS_DIR/$name/mri/orig/COR-???
 header: $SUBJECTS_DIR/$name/mri/orig/COR-.info

Auto align with AFNI 3dvolreg

For better alignment of routine images, first go
to Preferences -> Expert Preferences -> Conv.
Then choose "Auto align with AFNI 3dvolreg"
and OK/Save.  Back at the Setup Structural Scans
panel, after several scans have been configured,
pressing CONVERT/AVERAGE will convert each
scan to COR format, flip it if necessary (see
below), and average after registration is done.

Manual Register Before Average

For manual alignment (e.g., for very different
images, partial images) go to Preferences ->
Expert Preferences -> Conv.  Then choose "Manual
align with tkregister" and OK/Save.  Back at the
Setup Structural Scans panel, after several scans
have been configured, pressing CONVERT/AVERAGE
will convert each scan to COR format, flip it if
necessary (see below), start Register possibly
multiple times, and finally, average after
registration is done.

The first Register will start with the first
configured scan as the TARGET and the second scan
as the MOVEABLE.  After manually registering and
saving the matrix (SAVE REG), quit that register
(double-click register interface at upper left).
If there is a third scan, this will start another
Register set up to align the third scan with the
first.

When the last Register is quit, the registered
scans will then be averaged (*don't* click the
purple QuitCONVERT/AVE button or you will kill
the averaging before it finishes--that button
will turn gray again when the averaging is
done).

If the mid-sagittal plane of the first (TARGET)
scan is badly skewed, click "Adjust Target
Scan..." before CONVERT/AVERAGE.  This will bring
up the first scan as both the MOVEABLE and TARGET
allowing you to adjust it before doing any
alignments between scans.

To read unformatted data sets or data sets for
which the header is not recognized, set the
Scan Type to:

  noheader     => headerless 2-byte images
  ignoreheader => read x*y*2 bytes from file end

(when "Manual align with tkregister" is set), and
enter the pixel size and slice thickness manually.

High Resolution Surface Reconstruction

To reconstruct a less-than-1x1x1mm scan as
if it was a 1x1x1mm scan (in order obtain a
higher resolution surface with more vertices and
smaller faces), start with an AFNI brik (or other
x-y-z data brik), READ HEADER, and then use the
following roundabout method to fool FreeSurfer
into thinking the scan is actually 1x1x1mm.
This example is for a 240x320 inplane 0.75mm
x 0.75mm x 0.75mm sagittal Siemens MPRAGE with
208 slices/partitions:

     Scan Type: noheadbrik   [was brik]
   Slice Count: 208          [leave as is]
  Image X Size: 240          [leave as is]
  Image Y Size: 320          [leave as is]
 In Plane (mm): 1.0          [leave as is]
Thickness (mm): 1.0          [leave as is]
 Other Options: -fov 256 -t 1.0 ... [e.g.,-p 2.5]
          Flip: x=>-z  y=>y z=>x

After the surface has been reconstructed, to
use it for standard functional overlays, first
duplicate the subject (to preserve the high
resolution data).  Then resample the 3D data to
1x1x1 by running CONVERT/AVERAGE the standard way
(*without* resetting Scan Type to noheadbrik,
and after removing the -fov and -t entires from
Other Options).  Delete the copied directories
of the other processing stages (T1, brain, wm,
filled) and move the "orig" data set to the
"T1" directory, which is the Target scan for
tkregister when you click Align=>Struct in Setup
Functional Scan Parameters for an Align scan.
Finally, rescale the surfaces to match.  Here is
how to do that with a tcl script. First make a
file TmpTclScript.tcl:

open_window
read_binary_curv
make_lateral_view
set cslope 3.0
redraw
really_scale_brain 0.75 0.75 0.75
redraw
set outsurf $insurf
write_binary_surface
exit              

Then run the tmp tcl script on all 12 surfaces
(example in csh):

#! /bin/csh -f
set name = marty
set tmpscript = TmpTclScript.tcl
foreach hemi ( lh rh )
  foreach surf ( orig smoothwm inflated \
                 white graymid pial )
    tksurfer -${name} $hemi $surf -tcl $tmpscript
  end
end

The supplied script, scalesurfaces, can do
this for all surfaces of a subject.  Remember,
this permanently alters the surface, so you will
typically want to run it on a copy.

The new (copied) subject will now have a higher
resolution surface aligned with a 1x1x1mm
Target scan.  Note that the effective width
of the smoothing kernel for a given number of
Smoothing Steps (in View Functional Data) will
now be reduced.

Input and Output Files (in $SUBUECTS_DIR)
 
   [for two input subdirs]
 
 raw images1:   $name/mri/orig/scan1/165-2-10.ima
 input images1: $name/mri/orig/scan1/COR-???
 input header1: $name/mri/orig/scan1/COR-.info
 input xform1:  $name/mri/orig/scan1/register.dat

 raw images1:   $name/mri/orig/scan1/165-3-171.ima
 input images2: $name/mri/orig/scan2/COR-???
 input header2: $name/mri/orig/scan2/COR-.info
 input xform2:  $name/mri/orig/scan2/register.dat

 output images: $name/mri/orig/COR-???
 output header: $name/mri/orig/COR-.info

If you redo a manual register average, it will
start with the previously saved transformation
matrix.  To redo a manual register average from
scatch, go into each of the raw scan directories
and remove (or move aside) the register.dat
file.

Flipping

At conversion time, the data set can be flipped.
Caution: this can mirror-image the data set
(sometimes necessary).  Typically this is done
after the data set has already been converted
once without any flipping.  Set the view to
CORONAL first.  The 3 directions are defined
in the CORONAL view as:

  x => left   -> right
  y => bottom -> top
  z => near   -> far

Then determine which direction you would like
the current x (left-right) to be flipped to.
You can use a negative sign.  For example,
to left right mirror-image a data set, set
the fields like this:

  x => -x
  y => y
  z => z

This should be done for each page/directory.

To flip a sagittal acquired right->left to
coronal, use:

  x => -z
  y => y
  z => x

Data sets can also be interactively flipped by running
tkmedit on the command line and typing:

  flip <[-]{x,y,z}> <[-]{x,y,z}> <[-]{x,y,z}>

at the tcl prompt.

Some flips we have used:

  -> Siemens sagittal -> coronal: -z  y  x
  -> Varian mp axial  -> coronal:  x -z  y
  -> GE FSPGR axial   -> coronal:  x -z -y

