r117: *** empty log message ***
[ctsim.git] / src / ctrec.cpp
index 6ed8bd98fc2ad4f621f5ca510c52ff66cbc758eb..39688cfb817e74967519ceff0de6100b60c1385f 100644 (file)
@@ -1,59 +1,15 @@
 /*****************************************************************************
-**  This is part of the CTSim program
-**  Copyright (C) 1983-2000 Kevin Rosenberg
-**
-**  $Id: ctrec.cpp,v 1.3 2000/06/07 10:12:05 kevin Exp $
-**  $Log: ctrec.cpp,v $
-**  Revision 1.3  2000/06/07 10:12:05  kevin
-**  Upgraded from MPI to MPI++
-**
-**  Revision 1.2  2000/06/07 07:43:19  kevin
-**  Converted to IF data files and C++
-**
-**  Revision 1.1  2000/06/07 02:29:05  kevin
-**  Initial C++ versions
-**
-**  Revision 1.13  2000/06/05 01:33:25  kevin
-**  BSpline changes
-**
-**  Revision 1.12  2000/05/24 22:50:04  kevin
-**  Added support for new SGP library
-**
-**  Revision 1.11  2000/05/16 04:33:59  kevin
-**  Improved option processing
-**
-**  Revision 1.10  2000/05/11 01:06:30  kevin
-**  Changed sprintf to snprintf
+** FILE IDENTIFICATION
 **
-**  Revision 1.9  2000/05/08 20:02:32  kevin
-**  ANSI C changes
-**
-**  Revision 1.8  2000/05/04 18:16:34  kevin
-**  renamed filter definitions
-**
-**  Revision 1.7  2000/05/03 08:49:50  kevin
-**  Code cleanup
-**
-**  Revision 1.6  2000/05/02 15:31:47  kevin
-**  code cleaning
-**
-**  Revision 1.5  2000/04/30 11:41:06  kevin
-**  Cleaned up debugging code
-**
-**  Revision 1.4  2000/04/30 10:13:27  kevin
-**  Fixed MPI bugs
-**
-**  Revision 1.3  2000/04/30 04:06:13  kevin
-**  Update Raysum i/o routines
-**  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
-**
-**  Revision 1.2  2000/04/29 23:24:56  kevin
-**  *** empty log message ***
-**
-**  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
-**  Initial CVS import for first public release
+**   Name:          ctrec.cpp
+**   Purpose:       Reconstruct an image from projections
+**   Programmer:    Kevin Rosenberg
+**   Date Started:  Aug 1984
 **
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
 **
+**  $Id: ctrec.cpp,v 1.13 2000/06/22 10:17:28 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ******************************************************************************/
 
-/* FILE
- *   ctrec.c           Reconstruct an image from raysums
- *
- * DATE
- *   Aug 84
- *   Jul 99  -- Converted to ANSI C
- *              Added MPI parallel processing
- */
-
 #include "ct.h"
+#include "timer.h"
+
 
 enum {O_INTERP, O_FILTER, O_FILTER_PARAM, O_BACKPROJ, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
 
@@ -96,108 +45,89 @@ static struct option my_options[] =
   {0, 0, 0, 0}
 };
 
+
 void 
 ctrec_usage (const char *program)
 {
-  fprintf(stdout,"usage: %s raysum-file image-file nx-image ny-image [OPTIONS]\n", kbasename(program));
-  fprintf(stdout,"Image reconstruction from raysum projections\n");
-  fprintf(stdout,"\n");
-  fprintf(stdout,"   raysum-file     Input raysum file\n");
-  fprintf(stdout,"   image-file      Output image file in SDF2D format\n");
-  fprintf(stdout,"   nx-image        Number of columns in output image\n");
-  fprintf(stdout,"   ny-image        Number of rows in output image\n");
-  fprintf(stdout,"   --interp        Interpolation method during backprojection\n");
-  fprintf(stdout,"       nearest     Nearest neighbor interpolation\n");
-  fprintf(stdout,"       linear      Linear interpolation\n");
+  cout << "usage: " << fileBasename(program) << " raysum-file image-file nx-image ny-image [OPTIONS]" << endl;
+  cout << "Image reconstruction from raysum projections" << endl;
+  cout << endl;
+  cout << "   raysum-file     Input raysum file" << endl;
+  cout << "   image-file      Output image file in SDF2D format" << endl;
+  cout << "   nx-image        Number of columns in output image" << endl;
+  cout << "   ny-image        Number of rows in output image" << endl;
+  cout << "   --interp        Interpolation method during backprojection" << endl;
+  cout << "       nearest     Nearest neighbor interpolation" << endl;
+  cout << "       linear      Linear interpolation" << endl;
 #if HAVE_BSPLINE_INTERP
-  fprintf(stdout,"       bspline     B-spline interpolation\n");
+  cout << "       bspline     B-spline interpolation" << endl;
 #endif
-  fprintf(stdout,"    --filter       Filter name\n");
-  fprintf(stdout,"       abs_bandlimit Abs * Bandlimiting (default)\n");
-  fprintf(stdout,"       abs_sinc      Abs * Sinc\n");
-  fprintf(stdout,"       abs_cos       Abs * Cosine\n");
-  fprintf(stdout,"       abs_hamming   Abs * Hamming\n");
-  fprintf(stdout,"       shepp         Shepp-Logan\n");
-  fprintf(stdout,"       bandlimit     Bandlimiting\n");
-  fprintf(stdout,"       sinc          Sinc\n");
-  fprintf(stdout,"       cos           Cosine\n");
-  fprintf(stdout,"       triangle      Triangle\n");
-  fprintf(stdout,"       hamming       Hamming\n");
-  fprintf(stdout,"    --backproj     Backprojection Method\n");
-  fprintf(stdout,"       trig        Trigometric functions at every point\n");
-  fprintf(stdout,"       table       Trigometric functions with precalculated table\n");
-  fprintf(stdout,"       diff        Difference method\n");
-  fprintf(stdout,"       diff2       Optimized difference method (default)\n");
-  fprintf(stdout,"       idiff2      Optimized difference method with integer math\n");
-  fprintf(stdout,"    --filter-param Alpha level for Hamming filter\n");
-  fprintf(stdout,"    --trace        Set tracing to level\n");
-  fprintf(stdout,"         none      No tracing (default)\n");
-  fprintf(stdout,"         text      Text level tracing\n");
-  fprintf(stdout,"         phm       Trace phantom\n");
-  fprintf(stdout,"         rays      Trace allrays\n");
-  fprintf(stdout,"         plot      Trace plotting\n");
-  fprintf(stdout,"         clipping  Trace clipping\n");
-  fprintf(stdout,"    --verbose      Turn on verbose mode\n");
-  fprintf(stdout,"    --debug        Turn on debug mode\n");
-  fprintf(stdout,"    --version      Print version\n");
-  fprintf(stdout,"    --help         Print this help message\n");
+  cout << "    --filter       Filter name" << endl;
+  cout << "       abs_bandlimit Abs * Bandlimiting (default)" << endl;
+  cout << "       abs_sinc      Abs * Sinc" << endl;
+  cout << "       abs_cos       Abs * Cosine" << endl;
+  cout << "       abs_hamming   Abs * Hamming" << endl;
+  cout << "       shepp         Shepp-Logan" << endl;
+  cout << "       bandlimit     Bandlimiting" << endl;
+  cout << "       sinc          Sinc" << endl;
+  cout << "       cos           Cosine" << endl;
+  cout << "       triangle      Triangle" << endl;
+  cout << "       hamming       Hamming" << endl;
+  cout << "    --backproj     Backprojection Method" << endl;
+  cout << "       trig        Trigometric functions at every point" << endl;
+  cout << "       table       Trigometric functions with precalculated table" << endl;
+  cout << "       diff        Difference method" << endl;
+  cout << "       diff2       Optimized difference method (default)" << endl;
+  cout << "       idiff2      Optimized difference method with integer math" << endl;
+  cout << "    --filter-param Alpha level for Hamming filter" << endl;
+  cout << "    --trace        Set tracing to level" << endl;
+  cout << "         none      No tracing (default)" << endl;
+  cout << "         text      Text level tracing" << endl;
+  cout << "         phm       Trace phantom" << endl;
+  cout << "         rays      Trace allrays" << endl;
+  cout << "         plot      Trace plotting" << endl;
+  cout << "         clipping  Trace clipping" << endl;
+  cout << "    --verbose      Turn on verbose mode" << endl;
+  cout << "    --debug        Turn on debug mode" << endl;
+  cout << "    --version      Print version" << endl;
+  cout << "    --help         Print this help message" << endl;
 }
 
 
 #ifdef HAVE_MPI
-static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
+static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int debug);
+static void ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal);
 #endif
 
-static void print_raysum_info(const RAYSUM *rs);
 
 int 
-ctrec_main (const int argc, char *const argv[])
+ctrec_main (int argc, char * argv[])
 {
-  ImageFile *im_global = NULL;
-  RAYSUM *rs_global = NULL;
-  char *rs_name, *im_filename = NULL;
-  char remark[MAXREMARK];
-  char filt_name[80];
-  double time_start = 0, time_end = 0;
+  ImageFile *imGlobal = NULL;
+  Projections projGlobal;
+  char *pj_name, *im_filename = NULL;
+  string remark;
   char *endptr;
   int opt_verbose = 0;
   int opt_debug = 0;
   int opt_trace = TRACE_NONE;
   double opt_filter_param = 1;
-  FilterType opt_filter = FILTER_ABS_BANDLIMIT;
-  InterpolationType opt_interp = I_LINEAR;
+  string optFilterName = "abs_bandlimit";
+  string optInterpName = "linear";
+  string optBackprojName = "idiff2";
   int opt_interp_param = 1;
-  BackprojType opt_backproj = O_BPROJ_DIFF2;
   int nx, ny;
 #ifdef HAVE_MPI
-  ImageFile *im_local;
-  RAYSUM *rs_local;
-  int mpi_argc = argc;
-  char **mpi_argv = (char **) argv;
+  ImageFile *imLocal;
   int mpi_nview, mpi_ndet;
   double mpi_detinc, mpi_rotinc, mpi_phmlen;
-  double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
-
-  MPI::Init (mpi_argc, mpi_argv);
-  mpi_ct.comm = MPI::COMM_WORLD.Dup();
-  mpi_ct.nproc = mpi_ct.comm.Get_size();
-  mpi_ct.my_rank = mpi_ct.comm.Get_rank();
-
-  if (mpi_ct.nproc > CT_MPI_MAX_PROCESS) {
-    sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
-             mpi_ct.nproc, CT_MPI_MAX_PROCESS);
-    exit(1);
-  }
+  MPIWorld mpiWorld (argc, argv);
 #endif
 
-#ifdef HAVE_MPI
-  time_start = MPI::Wtime();
-#else
-  time_start = td_current_sec();
-#endif
+  Timer timerProgram;
 
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0) {
+  if (mpiWorld.getRank() == 0) {
 #endif
     while (1) {
       int c = getopt_long(argc, argv, "", my_options, NULL);
@@ -209,22 +139,13 @@ ctrec_main (const int argc, char *const argv[])
       switch (c)
        {
        case O_INTERP:
-         if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
-           ctrec_usage(argv[0]);
-           return (1);
-         }
+         optInterpName = optarg;
          break;
        case O_FILTER:
-         if ((opt_filter = opt_set_filter(optarg)) < 0) {
-           ctrec_usage(argv[0]);
-           return (1);
-         }
+         optFilterName = optarg;
          break;
        case O_BACKPROJ:
-         if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
-             ctrec_usage(argv[0]);
-             return (1);
-         }
+         optBackprojName = optarg;
          break;
        case O_FILTER_PARAM:
          opt_filter_param = strtod(optarg, &endptr);
@@ -246,11 +167,11 @@ ctrec_main (const int argc, char *const argv[])
          break;
         case O_VERSION:
 #ifdef VERSION
-         fprintf(stdout, "Version %s\n", VERSION);
+         cout <<  "Version " <<  VERSION << endl;
 #else
-         fprintf(stderr, "Unknown version number");
+          cout << "Unknown version number" << endl;
 #endif
-         exit(0);
+         return (0);
        case O_HELP:
        case '?':
          ctrec_usage(argv[0]);
@@ -266,228 +187,191 @@ ctrec_main (const int argc, char *const argv[])
       return (1);
     }
 
-    rs_name = argv[optind];
+    pj_name = argv[optind];
   
     im_filename = argv[optind + 1];
   
     nx = strtol(argv[optind + 2], &endptr, 10);
     ny = strtol(argv[optind + 3], &endptr, 10);
   
-    if (opt_filter == FILTER_G_HAMMING || opt_filter == FILTER_ABS_G_HAMMING)
-      snprintf (filt_name, sizeof(filt_name), "%s: alpha = %.2f",
-              name_of_filter (opt_filter), opt_filter_param)
+    ostringstream filterDesc;
+    if (opt_filter_param >= 0)
+      filterDesc << optFilterName << ": alpha=" << opt_filter_param
     else
-      snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
-  
-    snprintf (remark, sizeof(remark), "Reconstruct: %dx%d, %s, %s, %s",
-            nx, ny, filt_name, name_of_interpolation (opt_interp), name_of_backproj(opt_backproj));
+      filterDesc << optFilterName;
+
+    ostringstream label;
+    label << "Reconstruct: " << nx << "x" << ny << ", " << filterDesc.str() << ", " << optInterpName << ", " << optBackprojName;
+    remark = label.str();
   
     if (opt_verbose)
-      fprintf (stdout, "%s\n", remark);
+      cout << "Remark: " << remark << endl;
 #ifdef HAVE_MPI
   }
 #endif
 
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0) {
-    rs_global = raysum_open (rs_name);
-    raysum_read (rs_global);
+  if (mpiWorld.getRank() == 0) {
+    projGlobal.read (pj_name);
     if (opt_verbose)
-      print_raysum_info(rs_global);
+      projGlobal.printScanInfo();
 
-    mpi_ndet = rs_global->ndet;
-    mpi_nview = rs_global->nview;
-    mpi_detinc = rs_global->det_inc;
-    mpi_phmlen = rs_global->phmlen;
-    mpi_rotinc = rs_global->rot_inc;
+    mpi_ndet = projGlobal.nDet();
+    mpi_nview = projGlobal.nView();
+    mpi_detinc = projGlobal.detInc();
+    mpi_phmlen = projGlobal.phmLen();
+    mpi_rotinc = projGlobal.rotInc();
   }
 
-  mpi_t1 = MPI::Wtime();
-  mpi_ct.comm.Bcast (&opt_verbose, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_debug, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_trace, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_filter, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_interp, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&opt_interp_param, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_backproj, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&mpi_ndet, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&mpi_nview, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&nx, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&ny, 1, MPI::INT, 0);
-  if (opt_verbose) {
-    mpi_t2 = MPI::Wtime();
-    mpi_t = mpi_t2 - mpi_t1;
-    mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
-    if (mpi_ct.my_rank == 0)
-      printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
-  }
-
-  mpi_ct_calc_work_units(mpi_nview);
+  TimerCollectiveMPI timerBcast (mpiWorld.getComm());
+  mpiWorld.BcastString (optBackprojName);
+  mpiWorld.BcastString (optFilterName);
+  mpiWorld.BcastString (optInterpName);
+  mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&opt_interp_param, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&mpi_ndet, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&mpi_nview, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&nx, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&ny, 1, MPI::INT, 0);
+  if (opt_verbose)
+      timerBcast.timerEndAndReport ("Time to broadcast variables");
 
-  rs_local = raysum_create (NULL, mpi_ct.local_work_units[mpi_ct.my_rank], mpi_ndet);
+  mpiWorld.setTotalWorkUnits (mpi_nview);
 
-  rs_local->ndet = mpi_ndet;
-  rs_local->nview = mpi_nview;
-  rs_local->det_inc = mpi_detinc;
-  rs_local->phmlen = mpi_phmlen;
-  rs_local->rot_inc = mpi_rotinc;
+  Projections projLocal (mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
+  projLocal.setDetInc (mpi_detinc);
+  projLocal.setPhmLen (mpi_phmlen);
+  projLocal.setRotInc (mpi_rotinc);
 
+  TimerCollectiveMPI timerScatter (mpiWorld.getComm());
+  ScatterProjectionsMPI (mpiWorld, projGlobal, projLocal, opt_debug);
   if (opt_verbose)
-    mpi_t1 = MPI::Wtime();
-  mpi_scatter_rs(rs_global, rs_local, opt_debug);
-  if (opt_verbose) {
-    mpi_t2 = MPI::Wtime();
-    mpi_t = mpi_t2 - mpi_t1;
-    mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
-    if (mpi_ct.my_rank == 0)
-      printf("Time to scatter rs = %f secs, Max time = %f sec\n", mpi_t, mpi_t_g);
-  }
+      timerScatter.timerEndAndReport ("Time to scatter projections");
 
-  if (mpi_ct.my_rank == 0) {
-    im_global = new ImageFile (im_filename, nx, ny);
-    im_global->adf.fileCreate();
+  if (mpiWorld.getRank() == 0) {
+    imGlobal = new ImageFile (im_filename, nx, ny);
+    imGlobal->fileCreate();
   }
-  im_local = new ImageFile (nx, ny);
 
+  imLocal = new ImageFile (nx, ny);
 #else
-  rs_global = raysum_open (rs_name);
-  raysum_read (rs_global);
+  projGlobal.read (pj_name);
   if (opt_verbose)
-    print_raysum_info(rs_global);
-
-  im_global = new ImageFile (im_filename, nx, ny);
-  im_global->adf.fileCreate();
-
-#endif
+    projGlobal.printScanInfo();
 
-#ifdef HAVE_MPI
-  mpi_t1 = MPI::Wtime();
-  proj_reconst (*im_local, rs_local, opt_filter, opt_filter_param, 
-                opt_interp, opt_interp_param, opt_backproj, opt_trace);
-  mpi_t2 = MPI::Wtime();
-  mpi_t = mpi_t2 - mpi_t1;
-  mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
-  if (mpi_ct.my_rank == 0 && opt_verbose)
-    printf("Time to reconstruct = %f, Max time = %f\n", mpi_t, mpi_t_g);
-#else
-  proj_reconst (*im_global, rs_global, opt_filter, opt_filter_param, 
-                opt_interp, opt_interp_param, opt_backproj, opt_trace);
+  imGlobal = new ImageFile (im_filename, nx, ny);
+  imGlobal->fileCreate();
 #endif
 
 #ifdef HAVE_MPI
+  TimerCollectiveMPI timerReconstruct (mpiWorld.getComm());
+  projLocal.reconstruct (*imLocal, optFilterName.c_str(), opt_filter_param, optInterpName.c_str(), opt_interp_param, optBackprojName.c_str(), opt_trace);
   if (opt_verbose)
-    mpi_t1 = MPI::Wtime();
-
-  int nxLocal = im_local->adf.nx();
-  int nyLocal = im_local->adf.ny();
-  ImageFileArray vLocal = im_local->getArray();
-  ImageFileArray vGlobal = im_global->getArray();
-
-  for (int ix = 0; ix < nxLocal; ix++) {
-    void *recvbuf = NULL;
-    if (mpi_ct.my_rank == 0)
-      recvbuf = vGlobal[ix];
-
-    mpi_ct.comm.Reduce(vLocal[ix], recvbuf, nyLocal, MPI::FLOAT, MPI::SUM, 0);
-  }
-
-  if (opt_verbose) {
-    mpi_t2 = MPI::Wtime();
-    mpi_t = mpi_t2 - mpi_t1;
-    mpi_ct.comm.Reduce (&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
-    if (mpi_ct.my_rank == 0)
-      printf("Time to reduce image = %f secs, max time = %f\n", mpi_t, mpi_t_g);
-  }
+      timerReconstruct.timerEndAndReport ("Time to reconstruct");
 
-  if (mpi_ct.my_rank == 0)
-      time_end = MPI::Wtime();
+  TimerCollectiveMPI timerReduce (mpiWorld.getComm());
+  ReduceImageMPI (mpiWorld, imLocal, imGlobal);
+  if (opt_verbose)
+      timerReduce.timerEndAndReport ("Time to reduce image");
 #else
-  time_end = td_current_sec();
+  projGlobal.reconstruct (*imGlobal, optFilterName.c_str(), opt_filter_param, optInterpName.c_str(), opt_interp_param, optBackprojName.c_str(), opt_trace);
 #endif
 
-
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0)
+  if (mpiWorld.getRank() == 0)
 #endif
     {
-      raysum_close (rs_global);
-      double calctime = time_end - time_start;
-      im_global->adf.arrayDataWrite ();
-      im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, rs_global->remark, rs_global->calctime);
-      im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, remark, calctime);
-      im_global->adf.fileClose ();
+      double calcTime = timerProgram.timerEnd();
+      imGlobal->arrayDataWrite ();
+      imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, projGlobal.remark(), projGlobal.calcTime());
+      imGlobal->labelAdd (Array2dFileLabel::L_HISTORY, remark.c_str(), calcTime);
+      imGlobal->fileClose ();
       if (opt_verbose)
-       cout << "Time active = " << calctime << " sec" << endl;
+       cout << "Run time: " << calcTime << " seconds" << endl;
     }
-
 #ifdef HAVE_MPI
-       MPI::Finalize();
+  MPI::Finalize();
 #endif
 
-       return (0);
+  return (0);
 }
 
 
+//////////////////////////////////////////////////////////////////////////////////////
+// MPI Support Routines
+//
+//////////////////////////////////////////////////////////////////////////////////////
+
 #ifdef HAVE_MPI
-static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
+static void ScatterProjectionsMPI (MPIWorld& mpiWorld, Projections& projGlobal, Projections& projLocal, const int opt_debug)
 {
-  int iproc;
-  int end_work_unit;
-  int iw = 0;
-
-  if (mpi_ct.my_rank == 0) {
-    for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
-      end_work_unit = mpi_ct.start_work_unit[iproc] + mpi_ct.local_work_units[iproc]  - 1;
-
-      for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
-       mpi_ct.comm.Send(&rs_global->view[iw]->ndet, 1, MPI::INT, iproc, 0);
-       mpi_ct.comm.Send(&rs_global->view[iw]->view_angle, 1, MPI::DOUBLE, iproc, 0);
-       mpi_ct.comm.Send(rs_global->view[iw]->detval, rs_global->ndet, MPI::FLOAT, iproc, 0);
+  if (mpiWorld.getRank() == 0) {
+    for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
+      for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
+       DetectorArray& detarray = projGlobal.getDetectorArray( iw );
+       int nDet = detarray.nDet();
+       DetectorValue* detval = detarray.detValues();
+
+       double viewAngle = detarray.viewAngle();
+       mpiWorld.getComm().Send(&nDet, 1, MPI::INT, iProc, 0);
+       mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, iProc, 0);
+       mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, iProc, 0);
       }
     }
   }
 
-  if (opt_debug)
-    fprintf(stdout, "Receiving rs data in process %d\n", mpi_ct.my_rank);
-
-  end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
-  for (iw = 0; iw <= end_work_unit; iw++) {
+  for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
     MPI::Status status;
-
-    mpi_ct.comm.Recv(&rs_local->view[iw]->ndet, 1, MPI::INT, 0, 0, status);
-    mpi_ct.comm.Recv(&rs_local->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0, status);
-    mpi_ct.comm.Recv(rs_local->view[iw]->detval, rs_local->ndet, MPI::FLOAT, 0, 0, status);
+    int nDet;
+    double viewAngle;
+    DetectorValue* detval = projLocal.getDetectorArray(iw).detValues();
+
+    mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, 0, 0, status);
+    mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, 0, 0, status);
+    mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, 0, 0, status);
+    projLocal.getDetectorArray(iw).setViewAngle( viewAngle );
   }
-  rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
-  if (opt_debug) {
-    mpi_ct.comm.Barrier();
-    fprintf(stdout, "Done with mpi_scatter_rs in process %2d\n", mpi_ct.my_rank);
+}
+
+static void
+ReduceImageMPI (MPIWorld& mpiWorld, ImageFile* imLocal, ImageFile* imGlobal)
+{
+  ImageFileArray vLocal = imLocal->getArray();
+
+  for (int ix = 0; ix < imLocal->nx(); ix++) {
+    void *recvbuf = NULL;
+    if (mpiWorld.getRank() == 0) {
+      ImageFileArray vGlobal = imGlobal->getArray();
+      recvbuf = vGlobal[ix];
+    }
+    mpiWorld.getComm().Reduce (vLocal[ix], recvbuf, imLocal->ny(), imLocal->getMPIDataType(), MPI::SUM, 0);
   }
 }
 
 #endif
 
-static void print_raysum_info(const RAYSUM *rs)
-{
-  printf ("Number of detectors: %d\n", rs->ndet);
-  printf ("    Number of views: %d\n", rs->nview);
-  printf ("             Remark: %s\n", rs->remark);
-  printf ("             phmlen: %f\n", rs->phmlen);
-  printf ("          det_start: %f\n", rs->det_start);
-  printf ("            det_inc: %f\n", rs->det_inc);
-  printf ("          rot_start: %f\n", rs->rot_start);
-  printf ("            rot_inc: %f\n", rs->rot_inc);
-}
 
 #ifndef NO_MAIN
 int 
-main (const int argc, char *const argv[])
+main (int argc, char* argv[])
 {
-  return (ctrec_main(argc, argv));
+  int retval = 1;
+
+  try {
+    retval = ctrec_main(argc, argv);
+  } catch (exception e) {
+    cerr << "Exception: " << e.what() << endl;
+  } catch (...) {
+    cerr << "Unknown exception" << endl;
+  }
+
+  return (retval);
 }
 #endif