USGS

Isis 3.0 Application Source Code Reference

Home

SpiceKernel.h

Go to the documentation of this file.
00001 #ifndef SpiceKernel_h
00002 #define SpiceKernel_h
00003 /**                                                                       
00004  * @file                                                                  
00005  * $Revision: 4943 $ 
00006  * $Date: 2013-01-04 11:02:32 -0700 (Fri, 04 Jan 2013) $
00007  *                                                                        
00008  *   Unless noted otherwise, the portions of Isis written by the USGS are 
00009  *   public domain. See individual third-party library and package descriptions 
00010  *   for intellectual property information, user agreements, and related  
00011  *   information.                                                         
00012  *                                                                        
00013  *   Although Isis has been used by the USGS, no warranty, expressed or   
00014  *   implied, is made by the USGS as to the accuracy and functioning of such 
00015  *   software and related material nor shall the fact of distribution     
00016  *   constitute any such warranty, and no responsibility is assumed by the
00017  *   USGS in connection therewith.                                        
00018  *                                                                        
00019  *   For additional information, launch                                   
00020  *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html                
00021  *   in a browser or see the Privacy & Disclaimers page on the Isis website,
00022  *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
00023  *   http://www.usgs.gov/privacy.html.
00024  *  
00025  *   $Id: SpiceKernel.h 4943 2013-01-04 18:02:32Z janderson $
00026  */                                                                       
00027 
00028 #include <cmath>
00029 #include <string>
00030 #include <vector>
00031 #include <iostream>
00032 #include <sstream>
00033 #include <functional>
00034 #include <algorithm>
00035 
00036 #include "IString.h"
00037 #include "IException.h"
00038 #include "SpiceSegment.h"
00039 
00040 namespace Isis {
00041 
00042 /**
00043  * @brief Container for SPICE kernel creation
00044  * 
00045  * This class serves as a container for ISIS cube files to prep for writing the
00046  * contents to a NAIF SPICE kernel.  Each file added is a CK or SPK segment 
00047  * depending upon the type specified in the K template parameter. When the ISIS 
00048  * cube SPICE segment is added, the contents of the Table BLOB 
00049  * (InstrumentRotation for CKs, InstrumentPosition for SPKs) have been read and 
00050  * transformed to the appropriate state intended to be compatible with kernels 
00051  * issued by each mission source. 
00052  * 
00053  * It is designed for ease of use.  Here is an example to create the most basic
00054  * of CK kernel from a single ISIS file:
00055  * @code
00056  *   SpiceKernel<SpkSegment> kernel;
00057  *   kernel.add("mycube.cub");
00058  * @endcode
00059  * 
00060  * Note that processing ISIS cubes is expensive in terms of NAIF kernel
00061  * management.  Lots of NAIF kernel activity is incurred in resolving all the
00062  * necessary requirements to get the SPICE data in a form that satisfies NAIF
00063  * kernel specifications.
00064  *  
00065  * To get access to the segments a Visitor design pattern is used whereby the 
00066  * generic Visitor class need only write an paren operator that accepts a 
00067  * segment of the K type.  This class interates through all the segments calling 
00068  * the operator(K &segment) method for each one in the container. 
00069  *  
00070  * Note that when a new K Segment is added, the list is immediately sorted based 
00071  * on time.  The sort is a stable sort, meaning if the segments are added in a 
00072  * time chronologically increasing time, the original list order is preserved. 
00073  *  
00074  * @ingroup Utility
00075  * 
00076  * @author 2010-11-22 Kris Becker
00077  * @internal
00078  * @history 2010-12-09 Kris Becker Add documentation and example 
00079  * @history 2011-04-02 Kris Becker Modified to a template container class and 
00080  *          updated documentation
00081  */
00082 template <typename K>
00083   class SpiceKernel {
00084     public:
00085       typedef K SegmentType;
00086       typedef std::vector<SegmentType> SegmentList;
00087       typedef typename SegmentList::iterator       SegmentIter;
00088       typedef typename SegmentList::const_iterator ConstSegmentIter;
00089   
00090       /** Constructor  */
00091       SpiceKernel() { }
00092       /** Destructor  */
00093       virtual ~SpiceKernel() { }
00094   
00095       /** Returns the number of segments */
00096       int size() const { return (_segments.size()); }
00097 
00098       /**
00099        * Const iterator into list
00100        *
00101        * @return ConstSegmentIter Returns a const iterator to the list
00102        */
00103       ConstSegmentIter begin() const {
00104         return _segments.begin();
00105       }
00106 
00107       /**
00108        * Const iterator to end of list
00109        *
00110        * @return ConstSegmentIter  Returns the const end of the list
00111        */
00112       ConstSegmentIter end() const {
00113         return _segments.end();
00114       }
00115 
00116       /**
00117        * Returns the start of the list for iterating purposes
00118        *
00119        * @return SegmentIter Returns an iterator on the collection
00120        */
00121       SegmentIter begin() {
00122         return _segments.begin();
00123       }
00124 
00125       /**
00126        * Returns the end of the list
00127        *
00128        * @return SegmentIter Returns the end of the list for determining the end
00129        *         of the iteration loop
00130        */
00131       SegmentIter end() {
00132         return _segments.end();
00133       }
00134 
00135       /**
00136        * @brief Add a new segment to the kernel
00137        * 
00138        * This method accepts a new segment and inserts it in the list - actually
00139        * appends it and then resorts the list so they are ordered by time. 
00140        *  
00141        * @param segment New segment to add to list
00142        */
00143       void add(const K &segment) {
00144         _segments.push_back(segment);
00145         std::stable_sort(_segments.begin(), _segments.end());
00146       }
00147 
00148       /**
00149        * @brief Template method Visitor implementation
00150        * 
00151        * This method accepts a Visitor that defines an operator() of the form
00152        * Visitor::operator()(K &segment).  For every segment in the list,
00153        * the operator will be called.  The caller is assured the list is sorted
00154        * via the operator<(const K &segment) of the segment type when they were 
00155        * added. 
00156        *  
00157        * @param Visitor  Visitor class that accepts each segment in the list
00158        */
00159       template <typename Visitor>
00160         void Accept(Visitor &v) const {
00161            ConstSegmentIter k(begin());
00162            while (k != end()) {
00163              v(*k);
00164              k++;
00165            }
00166           return;  
00167         }
00168 
00169       /**
00170        * @brief Const template methoid visitor implementation
00171        * 
00172        * This is a const Visitor method implementation with same functionality
00173        * as the non-const version.
00174        * 
00175        * @param Visitor  Visitor class that accepts each segment in the list
00176        */
00177       template <typename Visitor>
00178         void Accept(const Visitor &v) const {
00179            ConstSegmentIter k(begin());
00180            while (k != end()) {
00181              v(*k);
00182              k++;
00183            }
00184           return;  
00185         }
00186 
00187 
00188     private:
00189       SegmentList  _segments;  ///< List of arbitrary segments
00190   };
00191 
00192 
00193 
00194 
00195 };     // namespace Isis
00196 #endif
00197