Skip to content
Snippets Groups Projects
fslcheck.cc 3.74 KiB
/*  fslcheck.cc - Utility to check validity of avw header file 
    
    Peter Bannister and Matthew Webster, FMRIB Image Analysis Group
    
    Copyright (C) 1999-2007 University of Oxford  */

/*  CCOPYRIGHT */


#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <sstream>

#include "newmatap.h"
#include "newmatio.h"
#include "newimage/newimageall.h"
#include "utils/options.h"
#include "newimage/fmribmain.h"

using namespace MISCMATHS;
using namespace NEWMAT;
using namespace NEWIMAGE;
using namespace Utilities;

string title="fslcheck (Version 1.0)\nChecks Header file info for voxel dimensions\nCopyright(c) 2001, University of Oxford (Peter R Bannister)";
string examples="fslcheck -i <header_file>\n";

Option<bool> help(string("-h,--help"), false,
		  string("display this message"),
		  false, no_argument);
Option<bool> verbose(string("-v,--verbose"), false, 
		     string("switch on diagnostic messages"), 
		     false, no_argument);
Option<string> inputname(string("-i,--in"), string(""),
			 string("filename of input timeseries"),
			 true, requires_argument);
const float tolerance = 0.00000001;

template <class T>
int fmrib_main(int argc, char* argv[])
{
  volume4D<T> timeseries;
  volumeinfo vinfo;
  float x_dim, y_dim, z_dim, t_dim;
  short datatype;

  if (inputname.set()) {
    if (verbose. value()) { cout << "Reading header file" << endl; }
    read_volume4D_hdr_only(timeseries,inputname. value(),vinfo);
  }

  if (fabs(timeseries. xdim()) <= tolerance) {
    do {
      cerr << "Invalid voxel x-dimension (" << timeseries. xdim() << " mm): Please enter a new value" << endl;
      cin >> x_dim;
    } while (fabs(x_dim) <= tolerance);
    timeseries. setxdim(x_dim);
  }

  if (fabs(timeseries. ydim()) <= tolerance) {
    do {
      cerr << "Invalid voxel y-dimension (" << timeseries. ydim() << " mm): Please enter a new value" << endl;
      cin >> y_dim;
    } while (fabs(y_dim) <= tolerance);
    timeseries. setydim(y_dim);
  }
  
  if (fabs(timeseries. zdim()) <= tolerance) {
    do {
      cerr << "Invalid voxel z-dimension (" << timeseries. zdim() << " mm): Please enter a new value" << endl;
      cin >> z_dim;
    } while (fabs(z_dim) <= tolerance);
    timeseries. setzdim(z_dim);
  }
  
  if (fabs(timeseries. tdim()) <= tolerance) {
    do {
      cerr << "Invalid TR (" << timeseries. tdim() << " secs): Please enter a new value" << endl;
      cin >> t_dim;
    } while (fabs(t_dim) <= tolerance);
    timeseries. settdim(t_dim);
  }
  
  // set bitpix correctly (via FslSetDataType) - NB: type T = datatype
  FslGetDataType(&vinfo,&datatype);
  FslSetDataType(&vinfo,datatype);
  // read in whole file(!) and save it again
  volume4D<T> tmp;
  read_volume4D(tmp, inputname.value());
  save_volume4D_filetype(tmp, inputname.value(), FslGetFileType(&vinfo), vinfo);
  return 0;
}

int main (int argc,char** argv)
{
  Tracer tr("main");

  OptionParser options(title, examples);

  try {
    options.add(inputname);
    options.add(help);
    options.add(verbose);

    options.parse_command_line(argc, argv);

    if ( (help.value()) || (!options.check_compulsory_arguments(true)) )
      {
	options.usage();
	exit(EXIT_FAILURE);
      }
        
    if ( inputname.unset()) 
      {
	options.usage();
	cerr << endl 
	     << "--in or -i MUST be used." 
	     << endl;
	exit(EXIT_FAILURE);
      }
    
  }  catch(X_OptionError& e) {
    options.usage();
    cerr << endl << e.what() << endl;
    exit(EXIT_FAILURE);
  } catch(std::exception &e) {
    cerr << e.what() << endl;
  } 

  int retval=call_fmrib_main(dtype(inputname.value()), argc, argv);
  if (retval!=0) {
    cerr << "Failed to correctly read file, please check the .hdr using fslhd" << endl;
  } else return retval;
}