Skip to content
Snippets Groups Projects
ftoz.cc 3.22 KiB
Newer Older
Mark Woolrich's avatar
Mark Woolrich committed
/*  ftoz.cc
Mark Woolrich's avatar
Mark Woolrich committed

    Mark Woolrich, FMRIB Image Analysis Group

    Copyright (C) 1999-2000 University of Oxford  */

/*  CCOPYRIGHT  */

#include <iostream>
#include <fstream>
Mark Woolrich's avatar
Mark Woolrich committed
#define WANT_STREAM
#define WANT_MATH
Mark Woolrich's avatar
Mark Woolrich committed

#include "newmatap.h"
#include "newmatio.h"
Mark Woolrich's avatar
Mark Woolrich committed

#include "miscmaths/f2z.h"
David Flitney's avatar
David Flitney committed
#include "miscmaths/volume.h"
#include <string>
Mark Woolrich's avatar
Mark Woolrich committed

using namespace NEWMAT;
Mark Woolrich's avatar
Mark Woolrich committed
using namespace MISCMATHS;
Mark Woolrich's avatar
Mark Woolrich committed

// the real defaults are provided in the function parse_command_line

class globaloptions {
public:
Mark Woolrich's avatar
Mark Woolrich committed
  string fsfname;
  string doffile1;
  string doffile2;
Mark Woolrich's avatar
Mark Woolrich committed

  string zscoresfname;
public:
  globaloptions();
  ~globaloptions() {};
};

globaloptions globalopts;


globaloptions::globaloptions()
{
  // set up defaults
  zscoresfname = "zstats";
}

void print_usage(int argc, char *argv[])
{
Mark Woolrich's avatar
Mark Woolrich committed
  cout << "Usage: " << argv[0] << " [options] <fsfile> <dof1> <dof2> \n"
Mark Woolrich's avatar
Mark Woolrich committed
       << "  Available options are:\n"
       << "        -zout <outputvol>            (default is "
                                        << globalopts.zscoresfname << ")\n"
       << "        -help\n\n";
}

void parse_command_line(int argc, char* argv[])
{
  if(argc<2){
    print_usage(argc,argv);
    exit(1);
  }

  int inp = 1;
  int n=1;
  string arg;
  char first;

  while (n<argc) {
    arg=argv[n];
    if (arg.size()<1) { n++; continue; }
    first = arg[0];
    if (first!='-') {
      if(inp == 1)
Mark Woolrich's avatar
Mark Woolrich committed
	globalopts.fsfname = arg;
Mark Woolrich's avatar
Mark Woolrich committed
      else if(inp == 2)
	globalopts.doffile1 = argv[n];
Mark Woolrich's avatar
Mark Woolrich committed
      else if(inp == 3)
	globalopts.doffile2 = argv[n];
Mark Woolrich's avatar
Mark Woolrich committed
      else
	{
	  cerr << "Mismatched argument " << arg << endl;
	  break;
	}
      n++;
      inp++;
      continue;
    }
    
    // put options without arguments here
    if ( arg == "-help" ) {
      print_usage(argc,argv);
      exit(0);
     }

    if (n+1>=argc) 
      { 
	cerr << "Lacking argument to option " << arg << endl;
	break; 
      }

    // put options with 1 argument here
    if ( arg == "-zout") {
      globalopts.zscoresfname = argv[n+1];
      n+=2;
    } else { 
      cerr << "Unrecognised option " << arg << endl;
      n++;
    } 
  }  // while (n<argc)

Mark Woolrich's avatar
Mark Woolrich committed
  if (globalopts.fsfname.size()<1) {
    cerr << "Fs filename not found\n\n";
Mark Woolrich's avatar
Mark Woolrich committed
    print_usage(argc,argv);
    exit(2);
  }
}

int main(int argc,char *argv[])
{ 
  try{
    parse_command_line(argc, argv);
    
Mark Woolrich's avatar
Mark Woolrich committed
    Volume fs;
    fs.read(globalopts.fsfname.c_str());
Mark Woolrich's avatar
Mark Woolrich committed

Mark Woolrich's avatar
Mark Woolrich committed
    int numTS = fs.Nrows();
Mark Woolrich's avatar
Mark Woolrich committed
    cerr << numTS << endl;


    Volume dof1, dof2; 

    if(FslFileExists(globalopts.doffile1.c_str())){  //dof1 is avw file
      dof1.read(globalopts.doffile1); 
    }
    else {
      dof1 = fs;
      dof1 = atof(globalopts.doffile1.c_str());
    }

 if(FslFileExists(globalopts.doffile2.c_str())){     //dof2 is avw file
      dof2.read(globalopts.doffile2); 
    }
    else {
      dof2 = fs;
      dof2 = atof(globalopts.doffile2.c_str());
    }

Mark Woolrich's avatar
Mark Woolrich committed
    Volume zs(numTS);
    F2z::ComputeFStats(fs, dof1, dof2, zs);
Mark Woolrich's avatar
Mark Woolrich committed
    VolumeInfo volinfo = fs.getInfo();
    volinfo.intent_code = NIFTI_INTENT_ZSCORE;
    volinfo.intent_p1 = 0.0;    
    zs.setInfo(volinfo);
Mark Woolrich's avatar
Mark Woolrich committed
    zs.writeAsFloat(globalopts.zscoresfname.c_str());
  }
  catch(Exception p_excp) 
    {
      cerr << p_excp.what() << endl;
      throw;
    }
  catch(...) 
    {
      cerr << "Image error" << endl;
      throw;
    } 
}