casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ArrayLattice.h
Go to the documentation of this file.
00001 //# ArrayLattice: Object which converts an Array to a Lattice.
00002 //# Copyright (C) 1994,1995,1996,1997,1998,1999,2000,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: ArrayLattice.h 20229 2008-01-29 15:19:06Z gervandiepen $
00028 
00029 #ifndef LATTICES_ARRAYLATTICE_H
00030 #define LATTICES_ARRAYLATTICE_H
00031 
00032 //# Includes
00033 #include <lattices/Lattices/Lattice.h>
00034 #include <casa/Arrays/Array.h>
00035 
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 // <summary>
00040 // A memory resident Lattice
00041 // </summary>
00042 
00043 // <use visibility=export>
00044 
00045 // <reviewed reviewer="Peter Barnes" date="1999/10/30" tests="tArrayLattice" demos="">
00046 // </reviewed>
00047 
00048 // <prerequisite>
00049 //   <li> <linkto class=Lattice>Lattice</linkto>
00050 //   <li> <linkto class=Array>Array</linkto>
00051 // </prerequisite>
00052 
00053 // <etymology>
00054 // The ArrayLattice name reflects its role as a Lattice interface to an Array
00055 // object.
00056 // </etymology>
00057 
00058 // <synopsis> 
00059 // An ArrayLattice is a concrete Lattice class where the data is stored in
00060 // memory as opposed to the <linkto class=PagedArray>PagedArray</linkto> class
00061 // where the data is stored on disk. As a result this class is much more
00062 // suitable to problems which require small Lattices that can fit into the
00063 // memory of a computer. 
00064 //
00065 // ArrayLattice imposes another layer of function calls on top of a an
00066 // Array. As a result they should not be used for generic Array
00067 // manipulation. They are useful if you have an Array that needs to use
00068 // Lattice functions or needs to be used with PagedArrays or other Lattice
00069 // derivatives (like <linkto class=LatticeExpr>LatticeExpr</linkto> or
00070 // <linkto class=SubLattice>SubLattice</linkto>).
00071 // For example the LatticeIterator class can iterate through an Array in
00072 // more ways than any of the ArrayIterator classes can. The examples below
00073 // illustrate some uses for ArrayLattices. 
00074 // </synopsis> 
00075 
00076 // <example>
00077 // All the examples in this section are available in
00078 // <src>dArrayLattice.cc</src>
00079 //
00080 // <h4>Example 1:</h4>
00081 // In this example an Array of data is converted into an ArrayLattice so that
00082 // the copyData function can be used to write the data to a PagedArray which
00083 // will be stored on disk.
00084 // <srcblock>
00085 // // make an Array and fill it with data.
00086 // Array<Float> myArray(IPosition(3, 64, 64, 2));
00087 // indgen(myArray); // fills the Array with 0,1,2,....,64*64*2-1
00088 // // construct the ArrayLattice
00089 // ArrayLattice<Float> myLattice(myArray);
00090 // // make a PagedArray to store the data on disk
00091 // PagedArray<Float> myPagedArray(myLattice.shape(), "myTestData.array");
00092 // // now copy the data onto disk
00093 // myPagedArray.copyData (myLattice);
00094 // </srcblock>
00095 // Note that it could be done in a somewhat simpler way as:
00096 // <srcblock>
00097 // // make an Array and fill it with data.
00098 // Array<Float> myArray(IPosition(3, 64, 64, 2));
00099 // indgen(myArray); // fills the Array with 0,1,2,....,64*64*2-1
00100 // // make a PagedArray to store the data on disk
00101 // PagedArray<Float> myPagedArray(myLattice.shape(), "myTestData.array");
00102 // // now put the data onto disk
00103 // myPagedArray.put (myArray);
00104 // </srcblock>
00105 //
00106 // <h4>Example 2:</h4>
00107 // The <linkto class=ArrayIterator>ArrayIterator</linkto> class (or its
00108 // derivatives the <linkto class=VectorIterator>VectorIterator</linkto> and the
00109 // <linkto class=MatrixIterator>MatrixIterator</linkto> classes) do not allow
00110 // the user to specify a cursor shape. In this example a Cube class will be
00111 // converted into an ArrayLattice so that an ArrLatticeIter can be used to
00112 // access the data spectrum by spectrum (assuming the z-axis is frequency).
00113 //
00114 // <srcblock>
00115 // Cube<Float> arr(64,64,128);
00116 // // assume that the data gets put into the cube somehow
00117 // // now construct an ArrayLattice from this cube.
00118 // ArrayLattice<Float> lat(arr);
00119 // // Construct an iterator that returns the 128-element spectra one at a time
00120 // ArrLatticeIter<Float> iter(lat, IPosition(3,1,1,128));
00121 // // construct a Matrix to hold the results
00122 // Matrix<Float> channelSum(64,64);
00123 // // and do the summation one spectrum at a time
00124 // for (iter.reset(); !iter.atEnd(); iter++)
00125 //    channelSum(iter.position().getFirst(2)) = sum(iter.cursor());
00126 // </srcblock>
00127 //
00128 //  There are more examples in the <linkto class=Lattice>Lattice</linkto> class
00129 //  and many of the examples in the 
00130 // <linkto class=PagedArray>PagedArray</linkto> class will also be instructive.
00131 // </example>
00132 
00133 // <motivation>
00134 // We needed a way of creating Lattices but with AIPS++ Array characteristics.
00135 // </motivation>
00136 
00137 //# <todo asof="1997/05/31">
00138 //# </todo>
00139 
00140 // <linkfrom anchor="ArrayLattice" classes="Lattice PagedArray">
00141 //  <here>ArrayLattice</here> - a memory based Lattice.
00142 // </linkfrom>
00143 
00144 
00145 template <class T> class ArrayLattice : public Lattice<T>
00146 {
00147   //# Make members of parent class known.
00148 public:
00149   using Lattice<T>::ndim;
00150 
00151 public: 
00152   // The default constructor creates a ArrayLattice that is useless for just
00153   // about everything, except that it can be assigned to with the assignment
00154   // operator.
00155   ArrayLattice();
00156 
00157   // Construct an ArrayLattice with the specified shape.
00158   // It results in a writable lattice.
00159   explicit ArrayLattice (const IPosition& shape);
00160 
00161   // Construct an ArrayLattice that references the given Array.
00162   // By default it results in a writable lattice.
00163   ArrayLattice (Array<T>& array, Bool isWritable = True);
00164 
00165   // Construct an ArrayLattice that references the given Array.
00166   // It results in a non-writable lattice.
00167   ArrayLattice (const Array<T>& array);
00168 
00169   // The copy constructor uses reference semantics.
00170   ArrayLattice (const ArrayLattice<T>& other);
00171 
00172   virtual ~ArrayLattice();
00173 
00174   // The assignment operator uses copy semantics.
00175   ArrayLattice<T>& operator= (const ArrayLattice<T>& other);
00176 
00177   // Make a copy of the object (reference semantics).
00178   virtual Lattice<T>* clone() const;
00179 
00180   // The lattice data can be referenced as an array section.
00181   virtual Bool canReferenceArray() const;
00182 
00183   // Is the lattice writable?
00184   virtual Bool isWritable() const;
00185 
00186   // returns the shape of the ArrayLattice.
00187   virtual IPosition shape() const; 
00188   
00189   // Set all of the elements in the Lattice to a value.
00190   virtual void set (const T& value);
00191 
00192   // Return the Array of the data within this Lattice.
00193   // <group>
00194   Array<T>& asArray();
00195   const Array<T>& asArray() const;
00196   // </group>
00197 
00198   // Return the value of the single element located at the argument
00199   // IPosition.  
00200   // Note that operator() (defined in the base class) can also be used.
00201   virtual T getAt (const IPosition& where) const;
00202   
00203   // Put the value of a single element.
00204   virtual void putAt (const T& value, const IPosition& where);
00205 
00206   // Check for internal consistency. Returns False if
00207   // something nasty has happened to the ArrayLattice.
00208   virtual Bool ok() const;
00209   
00210   // Returns the maximum recommended number of pixels for a cursor.
00211   // For this class this is equal to the number of pixels in the lattice.
00212   virtual uInt advisedMaxPixels() const;
00213 
00214   // Get a slice in an optimized way (specifically for ArrLatticeIter).
00215   // It returns in <src>buffer</src> a reference to the lattice array.
00216   void getIterSlice (Array<T>& buffer, const IPosition& start,
00217                      const IPosition& end, const IPosition& incr);
00218 
00219 protected:
00220   // Do the actual getting of an array of values.
00221   virtual Bool doGetSlice (Array<T>& buffer, const Slicer& section);
00222 
00223   // Do the actual putting of an array of values.
00224   virtual void doPutSlice (const Array<T>& sourceBuffer,
00225                            const IPosition& where,
00226                            const IPosition& stride);
00227   
00228 private:
00229   Array<T> itsData;
00230   Bool     itsWritable;
00231 };
00232 
00233 
00234 
00235 } //# NAMESPACE CASA - END
00236 
00237 #ifndef CASACORE_NO_AUTO_TEMPLATES
00238 #include <lattices/Lattices/ArrayLattice.tcc>
00239 #endif //# CASACORE_NO_AUTO_TEMPLATES
00240 #endif