PeaksWorkspace.cpp 32.1 KB
Newer Older
Peterson, Peter's avatar
Peterson, Peter committed
1
2
3
#include "MantidKernel/Logger.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/DateAndTime.h"
4
#include "MantidKernel/PhysicalConstants.h"
Peterson, Peter's avatar
Peterson, Peter committed
5
6
7
8
9
10
11
12
13
#include "MantidAPI/ColumnFactory.h"
#include "MantidGeometry/Quat.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/V3D.h"
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/MatrixWorkspace.h"
14
#include <boost/shared_ptr.hpp>
Peterson, Peter's avatar
Peterson, Peter committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <algorithm>
#include <string>
#include <ostream>
#include <exception>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

namespace Mantid
{
namespace DataObjects
{
  /// Register the workspace as a type
  DECLARE_WORKSPACE(PeaksWorkspace );

32
  //double pi = 3.1415926535;
Peterson, Peter's avatar
Peterson, Peter committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*//moved to header file
  std::vector< std::string > DetNames;

  std::vector< double* > DetInfo;

  Kernel::DateAndTime  C_experimentDate;
  */
  Kernel::Logger& PeaksWorkspace::g_log = Kernel::Logger::get("PeaksWorkspace");


  /** Constructor. Create a table with all the required columns.
   *
   * @return PeaksWorkspace object
   */
  PeaksWorkspace::PeaksWorkspace():TableWorkspace( 0 )
  {

    TableWorkspace::addColumn( std::string( "V3D" )  ,  std::string( "hkl" ) );
    TableWorkspace::addColumn( std::string( "V3D" ) ,  std::string( "position" ) );
    TableWorkspace::addColumn( std::string( "V3D" ) ,  std::string( "sample_orientation" ) );
    TableWorkspace::addColumn( std::string( "int" ) ,  std::string( "reflag" ) );
    TableWorkspace::addColumn( std::string( "int" ) ,  std::string( "bank" ) );
    TableWorkspace::addColumn( std::string( "int" ) ,  std::string( "run" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "ipeak" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "inti" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "sigi" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "moncount" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "row" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "col" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "chan" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "L1" ) );
64

Peterson, Peter's avatar
Peterson, Peter committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "time" ) );
    TableWorkspace::addColumn( std::string( "double" ) ,  std::string( "t_offset" ) );

  }


  /** Destructor */
  PeaksWorkspace::~PeaksWorkspace()
  {
    ClearDeleteCalibrationData();
  }

  void PeaksWorkspace:: ClearDeleteCalibrationData()
  {
    DetNames.clear();// elements std::string so will delete self??

    for( size_t n=DetInfo.size(); n >0;)
    {
      double* val = DetInfo[n-1];
      DetInfo.pop_back();
      delete val;

      n=DetInfo.size();
    }
  }

91
92
93
94
95
96
  void PeaksWorkspace::initialize(    const double      L1 ,               //m
                                      const double      time_offset ,      //microseconds
                                      const std::string Facility ,
                                      const std::string Instrument ,
                                      const Kernel::DateAndTime  experimentDate ,
                                      const std::string version ,
Peterson, Peter's avatar
Peterson, Peter committed
97

98
99
                                      const std::vector<std::string> &PanelNames,
                                      const std::vector< double*> &PanelInfo
Peterson, Peter's avatar
Peterson, Peter committed
100
101
102
103
                                      )
     {
       C_L1 = L1;
       C_time_offset = time_offset;
104
105
106
107
       C_Facility = std::string( Facility);
       C_Instrument = std::string( Instrument);
       C_version = std::string( version );
       C_experimentDate = Kernel::DateAndTime( experimentDate);
Peterson, Peter's avatar
Peterson, Peter committed
108
       ClearDeleteCalibrationData();
109
       for( size_t i=0; i<PanelNames.size() ; i++)
Peterson, Peter's avatar
Peterson, Peter committed
110
111
         DetNames.push_back( std::string(PanelNames[i]));

112
       for( size_t i=0; i<PanelInfo.size(); i++)
Peterson, Peter's avatar
Peterson, Peter committed
113
114
115
116
117
118
119
120
121
122
123
124
125
       {
         double* data = PanelInfo[i];
         double* data1 = new double[15];
         for( int j=0; j<15;j++)
         {
           data1[j] = data[j];
           if( (j>=2) &&( j <=8))
             data1[j] *=100;
         }
         DetInfo.push_back(data1);
       }
     }

126
     void PeaksWorkspace::initialize(  const std::string DetCalFileName)
Peterson, Peter's avatar
Peterson, Peter committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
     {
       ClearDeleteCalibrationData();

       try
       {
         std::ifstream in( DetCalFileName.c_str() );

         std::string s = readHeader( in );
         in.close();
       }catch( char * str)
       {
         std::cout <<"Exception reading detector info "<<str <<std::endl;
       }

     }

  void PeaksWorkspace::addPeak( const Geometry::V3D position ,
144
145
146
147
148
149
150
151
152
153
154
155
156
157
                                const double time ,
                                const Geometry::V3D hkl ,
                                const Geometry::V3D sample_orientation ,  //radians ,  phi,chi,omega
                                const int  reflag ,
                                const int  runNum ,
                                const double monCount ,
                                const int bankName ,
                                const double PeakCellCount ,
                                const double row ,
                                const double col ,
                                const double chan ,
                                const double PeakIntegrateCount ,
                                const double PeakIntegrateError
         )
Peterson, Peter's avatar
Peterson, Peter committed
158
159
160
  {
    int i = rowCount();
    insertRow( i );
161
    //double T = time;
Peterson, Peter's avatar
Peterson, Peter committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

    try
    {

      getRef< double >( std::string( "ipeak" ) ,  i ) = PeakCellCount;
      getRef< double >( std::string( "inti" ) ,  i ) = PeakIntegrateCount;
      getRef< double >( std::string( "sigi" ) ,  i ) = PeakIntegrateError;
      getRef< double >( std::string( "moncount" ) ,  i ) = monCount;

      getRef< Geometry::V3D >( std::string( "position" ) ,  i ) =
          Geometry::V3D( position );

      getRef< Geometry::V3D >( std::string( "sample_orientation" ) ,  i ) =
          Geometry::V3D( sample_orientation );

      getRef< Geometry::V3D >( std::string( "hkl" ) ,  i ) =
          Geometry::V3D( hkl );

      getRef< double >( std::string( "L1" ) ,  i ) = C_L1;
      getRef< double >( std::string( "time" ) ,  i ) = time;
      getRef< double >( std::string( "t_offset" ) ,  i ) = C_time_offset;
      getRef< int >( std::string( "run" ) ,  i ) = runNum;

      getRef< double >( std::string( "chan" ) ,  i ) = chan;
186
      //getRef< double >( std::string( "L2" ) ,  i ) = L2;
Peterson, Peter's avatar
Peterson, Peter committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
      getRef< int >( std::string( "bank" ) ,  i ) =
          ( bankName );

      getRef< double >( std::string( "row" ) ,  i ) = row;
      getRef< double >( std::string( "col" ) ,  i ) = col;
      getRef< int >( std::string( "reflag" ) ,  i ) = reflag;

    }catch( char * str )
    {
      std::cout <<  "exception occurred " <<  str <<  "\n";
    }

  }


  /**
   * @return the number of peaks
   */
  int PeaksWorkspace::getNumberPeaks() const
  {
    return rowCount();
  }

  /** Remove a peak
   *
   * @param peakNum :: peak index to remove
   */
214
  void PeaksWorkspace::removePeak( const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  {
    removeRow( peakNum );
  }


  //returns true if strictly less than
  bool compareAsVecs( std::vector< int > arg1, std::vector< int > arg2 )
  {
    if( arg1.size() < 3 )
    {
      if( arg2.size() < 3 )
        return true;
      else
        return false;
    }

    if( arg2.size() < 3 )
      return false;   //hopefully bad values go to back

    int r1 = arg1[0];
    int r2 = arg2[0];

    if( r1 < r2 )
      return true;

    if( r1 > r2 )
      return false;

    r1 = arg1[1];
    r2 = arg2[1];

    if( r1 < r2 )
      return true;

    return false;
  }



  /** Write an ISAW-style .peaks file
   *
   * @param filename :: filename to write to
   */
  void PeaksWorkspace::write( std::string filename )
  {
    try
    {
      std::ofstream out( filename.c_str() );
      std::string date =C_experimentDate.to_ISO8601_string();

      out <<  "Version: " <<  C_version <<  " Facility: " <<  C_Facility  ;
      out <<  " Instrument: " <<  C_Instrument <<  " Date: " ;
      out<< std::setw(date.length())<<date<< std::endl;

      out <<  "6        L1    T0_SHIFT" <<  std::endl;
      out << "7 "<< std::setw( 11 )  ;
      out <<   std::setprecision( 4 ) <<  std::fixed <<  ( C_L1*100 ) ;
      out << std::setw( 12 ) <<  std::setprecision( 3 ) <<  std::fixed  ;
      out << ( C_time_offset ) <<  std::endl;

      out <<  "4 DETNUM  NROWS  NCOLS   WIDTH   HEIGHT     DEPTH   DETD  "
          <<  " CenterX   CenterY   CenterZ   BaseX    BaseY    BaseZ    "
          <<  "  UpX      UpY      UpZ" <<  std::endl;

      for(size_t i = 0 ; i < DetNames.size() ; i++ )
      {
        double* info = DetInfo[ i ];
        out <<  "5" <<  std::setw( 7 ) <<  DetNames[ i ];

        out <<  std::setw( 7 ) <<  (int)info[ 0 ] <<  std::setw( 7 );
        out  <<  (int)info[ 1 ]<<  std::setw( 9 ) <<  std::setprecision( 4 );
        out  <<  std::fixed <<  info[ 2 ];

        for( int j = 3 ; j < 15 ; j++ )
        {
          out <<  std::setw( 9 ) <<  std::setprecision( 4 ) <<  std::fixed;
          out <<  info[ j ];

        }
        out  <<  std::endl;

      }


      std::vector< std::vector< int > > SortKeys;

      for( int i = 0 ; i < rowCount() ; i++ )
      {
        std::vector<int > dat ;

        dat.push_back(getRef< int >( "run" ,  i ) );
        dat.push_back( getRef< int >( "bank" ,  i ) );

        dat.push_back( i );
        SortKeys.push_back( dat );
      }

      stable_sort( SortKeys.begin() ,  SortKeys.end() ,  compareAsVecs );

      int LastRun = -1;;
      int LastDet;

      int thisRun ,  thisReflag;
      double this_h ,  this_k ,  this_l ,   this_row ,  this_col ,
      this_chan ,  this_L2 ,  this_az ,  this_polar ,  this_time ,
      this_inti ,  this_ipk ,   this_sigi ,  this_chi ,  this_phi ,
      this_omega ,   this_monCt ,  this_tOffset ,  this_L1;

      int thisDet;
      Geometry::V3D thisSampleOrientation;
      int seqNum = 0;
      std::vector< std::vector< int > >::iterator it;

      for ( it = SortKeys.begin(); it != SortKeys.end(); ++it )
      {
        seqNum++;
        std::vector<int > elt = *it;
        int peakNum =elt[2];

        this_row =
            getRef< double  >( std::string( "row" ) ,  peakNum );
        Geometry::V3D hkl = getRef< Geometry::V3D  > ( std::string( "hkl" ) ,
            peakNum );
        this_h = hkl.X();
        this_k = hkl.Y();
        this_l = hkl.Z();
        this_col = getRef < double >( std::string( "col" ) ,  peakNum );
        this_chan = getRef< double >( std::string( "chan" ) ,  peakNum );
        this_ipk = getRef< double >( "ipeak" ,  peakNum );
        this_inti = getRef< double >( "inti" ,  peakNum );
        this_sigi = getRef< double >( "sigi" ,  peakNum );

        this_monCt = getRef< double >( std::string( "moncount" ) ,  peakNum );
        this_L1 = getRef< double >( "L1" ,  peakNum );
        this_tOffset = getRef< double >( "t_offset" ,  peakNum );
        this_time = getRef< double >( "time" ,  peakNum );
        thisRun = getRef< int >( "run" ,  peakNum );
        thisReflag = Int( peakNum ,  3 );
        thisDet = getRef< int >( "bank" ,  peakNum );

        Geometry::V3D position = getRef< Geometry::V3D >( "position" ,
            peakNum );

        double x = position.X();
        double y = position.Y();
        double z = position.Z();
        this_L2 = sqrt( x*x + y*y + z*z );
        this_polar = acos(  z/this_L2 );
        this_az = atan2( y ,  x );


        thisSampleOrientation = getRef<Geometry::V3D >( "sample_orientation" ,
            peakNum );
        double mult = 180/3.1415926535897932384626433832795;
        this_chi = thisSampleOrientation.Y()*mult;
        this_phi = thisSampleOrientation.X()*mult;
        this_omega = thisSampleOrientation.Z()*mult;

        if( thisRun != LastRun  || LastDet != thisDet )
        {
          out <<  "0 NRUN DETNUM    CHI    PHI  OMEGA MONCNT" <<  std::endl;
          out <<  "1" <<  std::setw( 5 ) <<  thisRun <<  std::setw( 7 ) <<
              std::right <<  thisDet;

          out  <<  std::setw( 7 ) <<  std::fixed <<  std::setprecision( 2 )
          <<  this_chi;

          out  <<  std::setw( 7 ) <<  std::fixed <<  std::setprecision( 2 )
          <<  this_phi;

          out  <<  std::setw( 7 ) <<  std::fixed <<  std::setprecision( 2 )
          <<  this_omega;
          out  <<  std::setw( 7 ) <<  (int)( .5 + this_monCt ) <<  std::endl;
          LastRun = thisRun;
          LastDet = thisDet;
          out <<  "2   SEQN    H    K    L     COL     ROW    CHAN       L2  "
              <<  "2_THETA       AZ        WL        D   IPK      INTI   "
              << "SIGI RFLG" <<  std::endl;
        }

        int h = (int)floor( this_h+.5) , //assume when set via UB those
            k = (int)floor( this_k +.5 ) , //not close enuf get 0's
            l = (int)floor( this_l +.5 );


        out <<  "3" <<  std::setw( 7 ) <<  seqNum <<  std::setw( 5 ) <<  h
            <<  std::setw( 5 ) <<  k <<  std::setw( 5 ) <<  l;
        out <<  std::setw( 8 ) <<  std::fixed << std::setprecision( 2 )
        << this_col;

        out << std::setw( 8 ) << std::fixed << std::setprecision( 2 )
        << this_row;

        out << std::setw( 8 ) << std::fixed << std::setprecision( 2 )
        << this_chan;


        out << std::setw( 9 ) << std::fixed << std::setprecision( 3 )
        << ( this_L2*100 );

        out << std::setw( 9 ) << std::fixed << std::setprecision( 5 )
        << this_polar;

        out << std::setw( 9 ) << std::fixed << std::setprecision( 5 )
        << this_az;


        std::vector< double >xx ,  yy;
        xx.push_back( this_time );
        Kernel::Units::Wavelength wl;
        wl.fromTOF( xx ,  yy ,  this_L1 ,  this_L2 ,  this_polar ,
            0 ,  1.2 ,  1.2 );

        out << std::setw( 10 ) << std::fixed << std::setprecision( 6 )
        << xx[ 0 ];

        xx[ 0 ] = this_time;
        Kernel::Units::dSpacing dsp;

        dsp.fromTOF( xx ,  yy ,  this_L1 ,  this_L2 ,  this_polar ,
            0 ,  1.2 ,  1.2 );

        out << std::setw( 9 ) << std::fixed << std::setprecision( 4 )
        <<  xx[ 0 ];

        out << std::setw( 6 ) << (int)this_ipk << std::setw( 10 )
        << std::fixed << std::setprecision( 2 ) << this_inti;

        out << std::setw( 7 ) << std::fixed << std::setprecision( 2 )
        << this_sigi << std::setw( 5 ) << thisReflag << std::endl;

      }

      out.flush();
      out.close();

    }catch( char *s )
    {
      g_log.error(std::string(s));
      throw( std::string(s));
    }
   catch( std::exception &e)
   {
     g_log.error(e.what());
          throw( e.what());
   }catch(...)
   {
     g_log.error("Unknown Error");
     throw( "Unknown Error");
   }
  }


  /** Remove all the peaks from the workspace */
  void PeaksWorkspace::removeAllPeaks()
  {
    for( int i = rowCount() - 1 ; i >= 0 ; i-- )
      removeRow( i );
  }


  //stops at spaces and \n. strips
  std::string getWord( std::ifstream &in ,  bool consumeEOL )
  {
    std::string s;
    char c = 0;
    if( in.good() )
      for(  c = in.get() ; c == ' ' && in.good() ; c = in.get() )
      {}
    else
      return std::string();

    if( c == '\n' )
    {
      if( !consumeEOL )
        in.putback( c );

      return std::string();
    }

    s.push_back( c );

    if( in.good() )
      for(  c = in.get() ; in.good()&&c != ' ' && c != '\n' && c != '\r' ; c = in.get() )
        s.push_back( c );

    if( ((c == '\n') || (c == '\r')) && !consumeEOL )
      in.putback( c );

    return s;
  }


  void readToEndOfLine( std::ifstream& in ,  bool ConsumeEOL )
  {
    while( in.good() && getWord( in ,  false ).length() > 0  )
      getWord( in ,  false );

    if( !ConsumeEOL )
      return ;

    getWord( in ,  true );
  }


  /** Reads the header of a .peaks file
   *
   * @param in :: stream of the input file
   * @return TODO: I don't know what here
   */
  std::string PeaksWorkspace::readHeader( std::ifstream& in )
  {

    std::string r = getWord( in ,  false );

    if( r.length() < 1 )
      throw std::logic_error( std::string( "No first line of Peaks file" ) );

    if( r.compare( std::string( "Version:" ) ) != 0 )
      throw std::logic_error(
          std::string( "No Version: on first line of Peaks file" ) );

    C_version = getWord( in ,  false );

    if( C_version.length() < 1 )
      throw  std::logic_error( std::string( "No Version for Peaks file" ) );

    C_Facility = getWord( in ,  false );

    if( C_Facility.length() < 1  )
      throw  std::logic_error(
          std::string( "No Facility tag for Peaks file" ) );

    if( C_Facility.compare( std::string( "Facility:" ) ) != 0 )
      throw  std::logic_error(
          std::string( "No Facility tag for Peaks file" ) );

    C_Facility = getWord( in ,  false );

    if( C_Facility.length() < 1 )
      throw  std::logic_error( std::string( "No Facility in Peaks file" ) );

    C_Instrument = getWord( in ,  false );

    if( C_Instrument.length() < 1 )
      throw  std::logic_error(
          std::string( "No Instrument tag for Peaks file" ) );

    if( C_Instrument.compare( std::string( "Instrument:" ) ) != 0 )
      throw  std::logic_error(
          std::string( "No Instrument tag for Peaks file" ) );

    C_Instrument = getWord( in ,  false );

    if( C_Instrument.length() < 1 )
      throw std::logic_error(
          std::string( "No Instrument for Peaks file" ) );

    std::string date = getWord( in ,  false );

    if(date .length()< 1)
    {
      Kernel::DateAndTime x = Kernel::DateAndTime::get_current_time();
      C_experimentDate.set_from_time_t(x.to_time_t());
    }

    else
      if( date.compare( std::string( "Date:" ) ) == 0 )
      {
        date = getWord( in ,  false );
        C_experimentDate = Kernel::DateAndTime( date );
      }


    while( getWord( in ,  false ).length() > 0 )
      getWord( in ,  false );

    // Now get info on detector banks

    getWord( in ,  true );
    std::string s = getWord( in ,  false );
    if( s.compare( "6" ) != 0 )
      throw std::logic_error(
          std::string( "Peaks File has no L0 and T0 header info" ) );

    readToEndOfLine( in ,  true );
    s = getWord( in ,  false );

    if( s.compare( "7" ) != 0 )
      throw std::logic_error(
          std::string( "Peaks File has no L0 and T0 info" ) );

    C_L1 = strtod( getWord( in ,  false ).c_str() ,  0 )/100;
    C_time_offset = strtod( getWord( in ,  false ).c_str() ,  0 );


    readToEndOfLine( in ,  true );
    for( s = getWord( in ,  false ) ; s.length() < 1 ; getWord( in ,  true ) )
    {
      s = getWord( in ,  false );
    }

    if( s.compare( "4" ) != 0 )
      throw std::logic_error( std::string( "Peaks File has no bank descriptor line" ) );

    readToEndOfLine( in ,  true );
    s = getWord( in ,  false );
    while( s.length() < 1 && in.good() )
    {
      s = getWord( in ,  true );
      s = getWord( in ,  false );
    }

    if(  !in.good() )
      throw std::runtime_error( std::string( "Peaks File Quit too fast" ) );

    if( s.compare( std::string( "5" ) ) != 0 )
      throw std::logic_error( "No information on banks given" );//log this only


    bool err = false;
    while( s.compare( std::string( "5" ) ) == 0  )
    {
      double* data = new double[ 15 ];//Use boost pointers here??? does gc
      s = getWord( in ,  false );

      if( s.length() < 1 )
        err = true;
      else
        DetNames.push_back( s );

      for( int i = 0 ; i < 15 && !err ; i++ )
      {
        s = getWord( in ,  false );
        if( s.length() < 1 )
          err = true;
        else
          data[ i ] = strtod( s.c_str() ,  0 );

      }

      if( err )
        s = std::string( "2" );
      else
      {
        DetInfo.push_back( data );

        for( s = getWord( in ,  false ) ; s.length() > 0 ; s = getWord( in ,  false ) )
        {}

        s = getWord( in ,  true );
        s = getWord( in ,  false );//no blank lines will be allowed so far
      }
    }


    if( err )
      return std::string();

    return s;
  }



  std::string readPeak( std::string lastStr ,  std::ifstream& in ,
      double& h , double& k ,  double& l ,  double& col ,
      double& row , double& chan ,  double& L2 ,
      double&  ScatAng , double& Az ,  double& wl ,
      double& D ,  double& IPK , double& Inti ,
      double& SigI ,  int &iReflag )
  {

    std::string s = lastStr;

    if( s.length() < 1 && in.good() )//blank line
    {
      readToEndOfLine( in ,  true );

      s = getWord( in ,  false );;
    }

    if( s.length() < 1 )
      return 0;


    if( s.compare( "2" ) == 0 )
    {
      readToEndOfLine( in ,  true );

      for( s = getWord( in ,  false ) ; s.length() < 1 && in.good() ;
          s = getWord( in ,  true ) )
      {
        s = getWord( in ,  false );
      }
    }

    if( s.length() < 1 )
      return 0;

    if( s.compare( "3" ) != 0 )
      return s;

    int seqNum = atoi( getWord( in ,  false ).c_str() );

    h = strtod( getWord( in ,  false ).c_str() ,  0 ) ;
    k = strtod( getWord( in ,  false ).c_str() ,  0 ) ;
    l = strtod( getWord( in ,  false ).c_str() ,  0 ) ;

    col = strtod( getWord( in ,  false ).c_str() ,  0 ) ;
    row = strtod( getWord( in ,  false ).c_str() ,  0 ) ;
    chan = strtod( getWord( in ,  false ).c_str() ,  0 ) ;
    L2 = strtod( getWord( in ,  false ).c_str() ,  0 )/100 ;
    ScatAng = strtod( getWord( in ,  false ).c_str() ,  0 ) ;

    Az = strtod( getWord( in , false ).c_str() , 0 ) ;
    wl = strtod( getWord( in , false ).c_str() , 0 ) ;
    D = strtod( getWord( in , false ).c_str() , 0 ) ;
    IPK = strtod( getWord( in , false ).c_str() , 0 ) ;

    Inti = strtod( getWord( in , false ).c_str() , 0 ) ;
    SigI = strtod( getWord( in , false ).c_str() , 0 ) ;
    iReflag = atoi( getWord( in , false ).c_str() ) ;


    readToEndOfLine( in ,  true );
    return getWord( in , false );
  }


  std::string readPeakBlockHeader( std::string lastStr ,  std::ifstream &in ,
      int&run ,int& detName ,
      double&chi , double&phi ,
      double&omega , double&monCount )
  {
    std::string s = lastStr;

    if( s.length() < 1 && in.good() )//blank line
    {
      readToEndOfLine( in ,  true );
      s = getWord( in , false );
    }

    if( s.length() < 1 )
      return std::string();

    if( s.compare( "0" ) == 0 )
    {
      readToEndOfLine( in ,  true );
      s = getWord( in , false );
      while( s.length() < 1 )
      {
        readToEndOfLine( in ,  true );
        s = getWord( in , false );
      }
    }

    if( s.compare( std::string( "1" ) ) != 0 )
      return s;

    run = atoi( getWord( in , false ).c_str() );
    detName =atoi( getWord( in , false ).c_str());
    chi = strtod( getWord( in , false ).c_str() , 0 );
    phi = strtod( getWord( in , false ).c_str() , 0 );

    omega = strtod( getWord( in , false ).c_str() , 0 );
    monCount = strtod( getWord( in , false ).c_str() , 0 );
    readToEndOfLine( in ,  true );

    return getWord( in , false );

  }
  //special formatted file Use clear peaks to no append

  /** Append the peaks from a .peaks file into the workspace
   *
   * @param filename :: path to the .peaks file
   */
  void PeaksWorkspace::append( std::string filename )
  {
    try
    {

      std::ifstream in( filename.c_str() );

      std::string s = readHeader( in );

      if( !in.good() || s.length() < 1 )
        throw std::runtime_error(
            std::string( "End of Peaks file before peaks" ) );

      if( s.compare( std::string( "0" ) ) != 0 )
        throw std::logic_error(
            std::string( "No header for Peak segments" ) );

      readToEndOfLine( in ,  true );
      s = getWord( in , false );

      int run , Reflag, detName;
      double chi , phi , omega , monCount;

      double h , k , l , col , row , chan , L2 , ScatAng , Az , wl , D ,
      IPK , Inti , SigI;
      double x , y , z , r , time;

      while( in.good() )
      {
        s = readPeakBlockHeader( s ,  in  , run , detName , chi , phi ,
            omega , monCount );

        s = readPeak( s , in , h , k , l , col , row , chan , L2 ,
            ScatAng , Az , wl , D , IPK , Inti , SigI , Reflag );

827
828
829
        chi *= M_PI/180;
        phi *= M_PI/180;
        omega *= M_PI/180;
Peterson, Peter's avatar
Peterson, Peter committed
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847

        z = L2*cos( ScatAng );
        r = sqrt( L2*L2 - z*z );
        x = r*cos( Az );
        y = r*sin( Az );
        //Would use the V3D spherical stuff, but it seemed weird
        std::vector< double >xx , yy;
        xx.push_back( wl );

        Kernel::Units::Wavelength WL;
        WL.toTOF( xx , yy , C_L1 , L2 , ScatAng , 12 , 12.1 , 12.1 );
        time = xx[ 0 ];


        this->addPeak( Geometry::V3D( x , y , z ) , time ,
            Geometry::V3D( h , k , l ) ,
            Geometry::V3D( phi , chi , omega ) ,
            Reflag , run , monCount , detName , IPK ,
848
            row , col , chan ,  Inti , SigI );
Peterson, Peter's avatar
Peterson, Peter committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
      }
  }catch( std::exception & xe)
  {
    g_log.error( xe.what());
    throw (std::logic_error(xe.what()));
  }catch( char* str)
  {


    g_log.error( std::string(str));
    throw (std::logic_error(std::string(str)));
  }catch(...)
  {

    g_log.error( "Unknown Error");
        throw ("Unknown Error");
  }

  }


  /** Get the d spacing of the given peak
   *
   * @param peakNum :: index of the peak
   * @return double, d_spacing
   */
875
  double  PeaksWorkspace::get_dspacing(const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
876
  {
877
878
879
    double time = getTime(peakNum);
    double L1   =get_L1(peakNum );
    Geometry::V3D position = getPosition( peakNum);
Peterson, Peter's avatar
Peterson, Peter committed
880
881
882

    double rho, polar,az;
    position.getSpherical( rho, polar, az);
883
    polar *= M_PI/180;
Peterson, Peter's avatar
Peterson, Peter committed
884
885
886
887
888
889
890
891
892
893
    double L2=rho;
    std::vector< double >xx , yy;
    xx.push_back( time );

    Kernel::Units::dSpacing dsp;
    dsp.fromTOF( xx , yy , L1 , L2 , polar , 12 , 12.1 , 12.1 );
    return xx[ 0 ];

  }

894
  double  PeaksWorkspace::get_wavelength( const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
895
896
897
898
899
900
  {
    double time = cell< double >(peakNum, ItimeCol);
    double L1   =cell< double >(peakNum, IL1Col );
    Geometry::V3D position = cell< Geometry::V3D >( peakNum , IpositionCol);
    double rho, polar,az;
    position.getSpherical( rho, polar, az);
901
    polar *= M_PI/180;
Peterson, Peter's avatar
Peterson, Peter committed
902
903
904
905
906
907
908
909
910
911
912
    double L2=rho;
    std::vector< double >xx , yy;
    xx.push_back( time );

    Kernel::Units::Wavelength wl;
    wl.fromTOF( xx , yy , L1 , L2 , polar , 12 , 12.1 , 12.1 );
    return xx[ 0 ];
  }


  //I believe their |Q| = 2pi/d.  This is what is returned.
913
  double  PeaksWorkspace::get_Qmagnitude( const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
914
915
916
  {


917
918
919
    double time =getTime(peakNum);
    double L1   = get_L1(peakNum );
    Geometry::V3D position = getPosition( peakNum );
Peterson, Peter's avatar
Peterson, Peter committed
920
921
    double rho, polar,az;
    position.getSpherical( rho, polar, az);
922
    polar *= M_PI/180;
Peterson, Peter's avatar
Peterson, Peter committed
923
924
925
926
927
928
    double L2=rho;
    std::vector< double >xx , yy;
    xx.push_back( time );

    Kernel::Units::MomentumTransfer Q;
    Q.fromTOF( xx , yy , L1 , L2 , polar , 12 , 12.1 , 12.1 );
929
    return xx[ 0 ]/2/M_PI;
Peterson, Peter's avatar
Peterson, Peter committed
930
931
  }

932
  Geometry::V3D    PeaksWorkspace::get_Qlab( const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
933
934
935
936
937
  {
    double MagQ = get_Qmagnitude( peakNum);


    Geometry::V3D position =
938
        getPosition( peakNum );
Peterson, Peter's avatar
Peterson, Peter committed
939
940
941
942
943
944
945
946
947
948
    position =Geometry::V3D(position);
    position /=position.norm();
    position.setZ( position.Z()-1);
    double nrm= position.norm();
    position *=MagQ/nrm;
    return position;


  }

949
  Geometry::V3D     PeaksWorkspace::get_QXtal(const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
950
951
952
953
954
955
956
957
  {

    Geometry::V3D Qvec= Geometry::V3D( get_Qlab( peakNum) );

    Geometry::V3D sampOrient = getSampleOrientation( peakNum );

    //phi, chi, omega
    Geometry::Quat rot;
958
    sampOrient *=180/M_PI;
Peterson, Peter's avatar
Peterson, Peter committed
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
    rot.setAngleAxis( -sampOrient[2],Geometry::V3D( 0,1,0));

    rot.rotate( Qvec );

    rot.setAngleAxis( -sampOrient[1] ,Geometry::V3D( 0,0,1));
    rot.rotate( Qvec );

    rot.setAngleAxis( -sampOrient[0],Geometry::V3D( 0,1,0));

    rot.rotate( Qvec );


    return Qvec;
  }

974
  Geometry::V3D   PeaksWorkspace::get_hkl( const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
975
976
977
978
979
  {

    return Geometry::V3D( cell< Geometry::V3D >(peakNum , IhklCol ));
  }

980
  double     PeaksWorkspace::get_row(const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
981
982
983
984
  {
    return cell< double >( peakNum , IPeakRowCol );
  }

985
  double     PeaksWorkspace::get_ipk(const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
986
987
  {

988
    return cell< double >( peakNum , IPeakIntensityCol );
Peterson, Peter's avatar
Peterson, Peter committed
989
990
  }

991
  double     PeaksWorkspace::get_column( const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
992
993
994
995
996
  {

    return cell< double >( peakNum , IPeakColCol );
  }

997
  double     PeaksWorkspace::get_time_channel( const int peakNum )
Peterson, Peter's avatar
Peterson, Peter committed
998
999
1000
  {

    return cell< double >( peakNum , IPeakChanCol );