casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ComponentList.h
Go to the documentation of this file.
00001 //# ComponentList: this defines ComponentList.h
00002 //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002
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: ComponentList.h 21229 2012-04-02 12:00:20Z gervandiepen $
00028 
00029 #ifndef COMPONENTS_COMPONENTLIST_H
00030 #define COMPONENTS_COMPONENTLIST_H
00031 
00032 
00033 #include <casa/aips.h>
00034 #include <components/ComponentModels/SkyComponent.h>
00035 #include <measures/Measures/MDirection.h>
00036 #include <measures/Measures/MFrequency.h>
00037 #include <casa/Containers/Block.h>
00038 #include <tables/Tables/Table.h>
00039 
00040 namespace casa { //# NAMESPACE CASA - BEGIN
00041 
00042 class String;
00043 class Path;
00044 class MVDirection;
00045 class MVFrequency;
00046 class MVAngle;
00047 class Unit;
00048 template <class T> class Vector;
00049 template <class Ms> class MeasRef;
00050 
00051 // <summary> A class for manipulating groups of components </summary>
00052 
00053 // <use visibility=export>
00054 
00055 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00056 // </reviewed>
00057 
00058 // <prerequisite>
00059 //   <li> <linkto class="SkyCompBase">SkyCompBase</linkto>
00060 // </prerequisite>
00061 //
00062 // <etymology>
00063 //  Because the SkyComponents were originally stored in a linked list
00064 //  ComponentList seemed like a good name for this class. Now the list is gone
00065 //  (in favour of a Block<SkyComponent>) but the name has stuck.
00066 // </etymology>
00067 //
00068 // <synopsis> 
00069 
00070 // This class is a container that allows many SkyComponents to be grouped
00071 // together and manipulated as one large compound component. The major
00072 // operations of this class are:
00073 // <dl>
00074 // <dt> Functions to traverse the list and extract individual components
00075 // <dd> See the <src>nelements</src> & <src>component</src> functions
00076 // <dt> Functions to add and delete components.
00077 // <dd> See the <src>add</src>, <src>remove</src> & <src>copy</src> functions
00078 // <dt> Functions to designate components as being selected or not.
00079 // <dd> See the <src>select</src>, <src>deselect</src> & <src>selected</src>
00080 //      functions
00081 // <dt> Functions to sample the flux of the components in any direction and
00082 //      frequency or to grid them onto an Image.
00083 // <dd> See the <src>sample</src> & <src>project</src> functions.
00084 // <dt> Functions to save the components to a table and read them back 
00085 //      again
00086 // <dd> See the <src>rename</src> function and the appropriate constructor.
00087 // <dt> Functions to manipulate the flux of all components
00088 // <dd> See the <src>setFlux</src>, <src>convertFluxUnit</src> &
00089 //      <src>convertFluxPol</src> functions.
00090 // <dt> Functions to manipulate the reference direction of all components
00091 // <dd> See the <src>setRefDirection</src> & <src>convertRefFrame</src>
00092 //      functions.
00093 // <dt> functions to sort the components
00094 // <dd> See the <src>sort</src>, <src>type</src> &
00095 //      <src>namel</src> functions.
00096 // </dl>
00097 
00098 // ComponentLists are memory based objects that can write their contents to and
00099 // from disk (ie a Table). To read a componentList from a Table the appropriate
00100 // constructor must be used. Alternatively an empty componentlist can be
00101 // created that is not associated with a Table. To save the list to disk it
00102 // must be given a name. This is done using the rename function. This will
00103 // rename the Table if the ComponentList was already associated with a Table.
00104 // Saving changes to the table on disk happens automatically when the list
00105 // is destructed or reset via the assignment operator (unless the table was
00106 // opened read-only).
00107 
00108 // The elements of a componentlist (ie., SkyComponents) are accessed via the
00109 // component functions. These functions return the SkyComponent by reference
00110 // and hence manipulating the supplied component manipulates the specified
00111 // element of the list.
00112 
00113 // </synopsis>
00114 //
00115 // <example>
00116 // These examples are coded in the tComponentList.h file.
00117 // <h4>Example 1:</h4>
00118 // In this example a ComponentList object is created and used to calculate the
00119 // ...
00120 // <srcblock>
00121 // </srcblock>
00122 // </example>
00123 //
00124 // <motivation>
00125 // A way was needed to read/write groups of components to disk and
00126 // manipulate them as a whole.
00127 // </motivation>
00128 //
00129 // <thrown>
00130 // <li> AipsError - If an internal inconsistancy is detected, when compiled in 
00131 // debug mode only.
00132 // </thrown>
00133 //
00134 // <todo asof="1998/05/22">
00135 //   <li> Nothing I hope. But I expect users will disagree.
00136 // </todo>
00137 
00138 class ComponentList {
00139 public:
00140   // Sorting criteria for components
00141   enum SortCriteria {
00142     // Sort the components by ABS(I flux), largest first.
00143     FLUX = 0,
00144     // Sort the components by distance from the reference, closest first.
00145     POSITION,
00146     // Sort the components by fractional polarisation, biggest first.
00147     POLARISATION,
00148     // No sorting is necessary
00149     UNSORTED,
00150     // The number of criteria in this enumerator
00151     NUMBER_CRITERIA
00152   };
00153   // Construct a componentList with no members in the list
00154   ComponentList();
00155 
00156   // Read a componentList from an existing table. By default the Table is
00157   // opened read-write.  Any subsequent changes made to a list opened 
00158   // read-only (i.e. via add(), remove(), or editing a non-const member 
00159   // component returned by component()) will not be saved to disk.
00160   ComponentList(const Path& fileName, const Bool readOnly=False);
00161 
00162   // The Copy constructor uses reference semantics
00163   ComponentList(const ComponentList& other);
00164 
00165   // The destructor saves the list to disk if it has a name (assigned using the
00166   // setName member function)
00167   ~ComponentList();
00168   
00169   // The assignment operator uses reference semantics
00170   ComponentList& operator=(const ComponentList& other);
00171 
00172   // Returns True if all the specified components are physically plausable. See
00173   // the  isPhysical function in the 
00174   // <linkto class="SkyCompBase">SkyCompBase</linkto> class 
00175   // for a precise definition of what this means.
00176   // <thrown>
00177   // <li> AipsError - If the index is equal to or larger than the number of
00178   //                  elements in the list or less than zero
00179   // </thrown>
00180   Bool isPhysical(const Vector<Int>& index) const;
00181 
00182   // Calculate the integrated flux of all the members of the componentlist at
00183   // the specified direction & frequency, in a pixel of specified size.
00184   Flux<Double> sample(const MDirection& sampleDir,
00185                       const MVAngle& pixelLatSize,
00186                       const MVAngle& pixelLongSize,
00187                       const MFrequency& centerFreq) const;
00188 
00189   // Same as the previous function except that many directions & frequencies
00190   // are done at once.  The Flux is added into the values supplied in the
00191   // samples argument and this cube must have dimensions of:
00192   // [4, nDirs, nFreqs]. The polarisation and units of the flux added are
00193   // specified with the reqUnit and reqPol arguments.
00194   void sample(Cube<Double>& samples,
00195               const Unit& reqUnit,
00196               const Vector<MVDirection>& directions, 
00197               const MeasRef<MDirection>& dirRef, 
00198               const MVAngle& pixelLatSize, 
00199               const MVAngle& pixelLongSize, 
00200               const Vector<MVFrequency>& frequencies,
00201               const MeasRef<MFrequency>& freqRef) const;
00202 
00203   // Add a SkyComponent to the end of the ComponentList. The list length is
00204   // increased by one when using this function. By default the newly added
00205   // component is not selected.  Note that it is possible to add a component
00206   // to a list that was opened read-only; however, the table on disk will 
00207   // not be updated with new component.  
00208   void add(SkyComponent component);
00209 
00210   // Add a SkyComponents in a ComponentList to the end of this ComponentList. The list length is
00211   // increased by the number of components in the added list. By default the newly added
00212   // components are not selected.  Note that it is possible to add a component list
00213   // to a list that was opened read-only; however, the table on disk will
00214   // not be updated with new components.
00215   void addList(const ComponentList& list);
00216 
00217   // Remove the specified SkyComponent(s) from the ComponentList. After
00218   // removing a component all the components with an indices greater than this
00219   // one will be reduced by one. For example in a five element list removing
00220   // elements [0,2,4] will result in a two element list, now indexed as
00221   // elements zero and one, containing what was previously the second and
00222   // fourth components.  Note that it is possible to remove a component
00223   // from a list that was opened read-only; however, the table on disk will 
00224   // not be updated accordingly.  
00225   // <thrown>
00226   // <li> AipsError - If the index is equal to or larger than the number of
00227   //                  elements in the list or is negative.
00228   // </thrown>
00229   // <group>
00230   void remove(const uInt& index);
00231   void remove(const Vector<Int>& indices);
00232   // </group>
00233 
00234   // returns how many components are in the list.
00235   uInt nelements() const;
00236 
00237   // deselect the specified component. Throws an exception (AipsError) if any
00238   // element in the index is out of range, ie. index >= nelements().
00239   // <thrown>
00240   // <li> AipsError - If the index is equal to or larger than the number of
00241   //                  elements in the list or less than zero
00242   // </thrown>
00243   void deselect(const Vector<Int>& index);
00244 
00245   // select the specified component. Throws an exception (AipsError) if any
00246   // element in the index is out of range, ie. index >= nelements().
00247   // <thrown>
00248   // <li> AipsError - If the index is equal to or larger than the number of
00249   //                  elements in the list or less than zero
00250   // </thrown>
00251   void select(const Vector<Int>& index);
00252 
00253   // Returns a Vector whose indices indicate which components are selected
00254   Vector<Int> selected() const;
00255 
00256 
00257   // set the label on the specified components to the specified string
00258   // <thrown>
00259   // <li> AipsError - If the index is equal to or larger than the number of
00260   //                  elements in the list or less than zero
00261   // </thrown>
00262   void setLabel(const Vector<Int>& which,
00263                 const String& newLabel);
00264 
00265   // get the the flux as a double
00266   // param: which - the component number (0 based)
00267   // return The flux as a Quantity
00268   void getFlux(Vector<Quantity>& fluxQuant, const Int& which) const;
00269   void getFlux(Vector<Quantum<Complex> >& fluxQuant, const Int& which);
00270 
00271   // get the associated polarizations as a vector of strings for the
00272   // specified component. Returned vector always has 4 elements.
00273   Vector<String> getStokes(const Int& which) const;
00274 
00275   // set the flux on the specified components to the specified flux
00276   // <thrown>
00277   // <li> AipsError - If the index is equal to or larger than the number of
00278   //                  elements in the list or less than zero
00279   // </thrown>
00280   void setFlux(const Vector<Int>& which,
00281                const Flux<Double>& newFlux);
00282 
00283   // convert the flux on the specified components to the specified units
00284   // <thrown>
00285   // <li> AipsError - If the index is equal to or larger than the number of
00286   //                  elements in the list or less than zero
00287   // </thrown>
00288   void convertFluxUnit(const Vector<Int>& which,
00289                        const Unit& unit);
00290   
00291   // convert the flux on the specified components to the specified 
00292   // polarisation representation
00293   // <thrown>
00294   // <li> AipsError - If the index is equal to or larger than the number of
00295   //                  elements in the list or less than zero
00296   // </thrown>
00297   void convertFluxPol(const Vector<Int>& which,
00298                       ComponentType::Polarisation pol);
00299   
00300   // set the reference direction on the specified components to the specified
00301   // direction. The reference frame is not changed, use the
00302   // <src>setRefFrame</src> function to do that.
00303   // <thrown>
00304   // <li> AipsError - If the index is equal to or larger than the number of
00305   //                  elements in the list or less than zero
00306   // </thrown>
00307   void setRefDirection(const Vector<Int>& which,
00308                        const MVDirection& newDir);
00309 
00310   // set the reference direction frame on the specified components to the
00311   // specified one. Does not convert the direction values.
00312   // <thrown>
00313   // <li> AipsError - If the index is equal to or larger than the number of
00314   //                  elements in the list or less than zero
00315   // </thrown>
00316   void setRefDirectionFrame(const Vector<Int>& which,
00317                             MDirection::Types newFrame);
00318 
00319   // Convert the reference direction frame on the specified components to the
00320   // specified one. Changes the direction values.
00321   // <thrown>
00322   // <li> AipsError - If the index is equal to or larger than the number of
00323   //                  elements in the list or less than zero
00324   // </thrown>
00325   void convertRefDirection(const Vector<Int>& which,
00326                            MDirection::Types newFrame);
00327 
00328   MDirection getRefDirection(Int which) const;
00329 
00330   // set the shape on the specified components to the specified one.
00331   // <thrown>
00332   // <li> AipsError - If the index is equal to or larger than the number of
00333   //                  elements in the list or less than zero
00334   // </thrown>
00335   void setShape(const Vector<Int>& which,
00336                 const ComponentShape& newShape);
00337 
00338   // get the shape of the specified component as a const pointer. No need to
00339   // delete it, it will be deleted when the variable goes out of scope.
00340   const ComponentShape* getShape(Int which) const;
00341 
00342   // set the shape on the specified components to the specified one. However
00343   // this function unlike the previous one does not change the reference
00344   // direction to the one specified in the newShape object.
00345   // <thrown>
00346   // <li> AipsError - If the index is equal to or larger than the number of
00347   //                  elements in the list or less than zero
00348   // </thrown>
00349   void setShapeParms(const Vector<Int>& which,
00350                      const ComponentShape& newShape);
00351 
00352   
00353   void setOptParms(const Vector<Int>& which,
00354                    const ComponentShape& newShape);
00355 
00356   // set the spectrum on the specified components to the specified one.
00357   // <thrown>
00358   // <li> AipsError - If the index is equal to or larger than the number of
00359   //                  elements in the list or less than zero
00360   // </thrown>
00361   void setSpectrum(const Vector<Int>& which,
00362                    const SpectralModel& newSpectrum);
00363 
00364   // set the spectrum on the specified components to the specified one. However
00365   // this function unlike the previous one does not change the reference
00366   // frequency to the one specified in the newSpectrum object.
00367   // <thrown>
00368   // <li> AipsError - If the index is equal to or larger than the number of
00369   //                  elements in the list or less than zero
00370   // </thrown>
00371   void setSpectrumParms(const Vector<Int>& which,
00372                         const SpectralModel& newSpectrum);
00373 
00374   // set the reference frequency on the specified components to the specified
00375   // frequency. The reference frame is not changed, use the
00376   // <src>setRefFrequencyFrame</src> function to do that.
00377   // <thrown>
00378   // <li> AipsError - If the index is equal to or larger than the number of
00379   //                  elements in the list or less than zero
00380   // </thrown>
00381   void setRefFrequency(const Vector<Int>& which, const MVFrequency& newFreq);
00382 
00383   // set the reference frequency frame on the specified components to the
00384   // specified one. Does not convert the frequency values.
00385   // <thrown>
00386   // <li> AipsError - If the index is equal to or larger than the number of
00387   //                  elements in the list or less than zero
00388   // </thrown>
00389   void setRefFrequencyFrame(const Vector<Int>& which,
00390                             MFrequency::Types newFrame);
00391 
00392   // set the reference frequency unit on the specified components to the
00393   // specified one. The unit must have the same dimensions as the Hz.
00394   // <thrown>
00395   // <li> AipsError - If the index is equal to or larger than the number of
00396   //                  elements in the list or less than zero
00397   // </thrown>
00398   void setRefFrequencyUnit(const Vector<Int>& which, const Unit& unit);
00399 
00400   // returns a reference to the specified element in the list.
00401   // <thrown>
00402   // <li> AipsError - If the list is associated with a table that was opened
00403   //                  readonly (non-const version only).
00404   // <li> AipsError - If the index is equal to or larger than the number of
00405   //                  elements in the list.
00406   // </thrown>
00407   // <group>
00408   const SkyComponent& component(const uInt& index) const;
00409   SkyComponent& component(const uInt& index);
00410   // </group>
00411 
00412   // Make the ComponentList persistant by supplying a filename. If the
00413   // ComponentList is already associated with a Table then the Table will be
00414   // renamed. Hence this function cannot be used with ComponentLists that are
00415   // constructed with readonly=True.
00416   // <thrown>
00417   // <li> AipsError - If the list is associated with a table that was opened
00418   //                  readonly
00419   // <li> AipsError - If option is Table::Old as this does not make sense
00420   // </thrown>
00421   void rename(const Path& newName, 
00422               const Table::TableOption option=Table::New);
00423 
00424   // Make a real copy of this componentList. As the copy constructor and the
00425   // assignment operator use reference semantics this is the only way to get a
00426   // distinct version of the componentList.
00427   ComponentList copy() const;
00428 
00429   // Sort the components in the list using the given criteria.
00430   void sort(ComponentList::SortCriteria criteria); 
00431 
00432   // Convert the SortCriteria enumerator to a string
00433   static String name(ComponentList::SortCriteria enumerator);
00434 
00435   // Convert a given String to a Type enumerator
00436   static ComponentList::SortCriteria type(const String& criteria);
00437 
00438   // Function which checks the internal data of this class for consistant
00439   // values. Returns True if everything is fine otherwise returns False.
00440   Bool ok() const;
00441 
00442   // methods to store itself as a Record and recover from a Record its state
00443 
00444   Bool fromRecord(String& error, const RecordInterface& inRec);
00445   Bool toRecord(String& error, RecordInterface& outRec) const;
00446 
00447   // Summarize specified component as a formatted string.
00448   String summarize(uInt index) const;
00449 
00450 private:
00451   // Privarte function to create the Table which will hold the components
00452   //void createTable(const Path& fileName, const Table::TableOption option, const Bool addOptCol);
00453   void createTable(const Path& fileName, const Table::TableOption option);
00454   // Private function to write the components to disk
00455   // <thrown>
00456   // <li> AipsError - If the table is not writable
00457   // </thrown>
00458   void writeTable();
00459 
00460   // Private function to read the components from disk
00461   // <thrown>
00462   // <li> AipsError - If the table is not readable
00463   // <li> AipsError - If the table is not writable (and readOnly==False)
00464   // </thrown>
00465   void readTable(const Path& fileName, const Bool readOnly);
00466   Block<SkyComponent> itsList;
00467   uInt itsNelements;
00468   Table itsTable;
00469   Bool itsROFlag;
00470   Block<Bool> itsSelectedFlags;
00471   Block<uInt> itsOrder;
00472   Bool itsAddOptCol;
00473 };
00474 
00475 } //# NAMESPACE CASA - END
00476 
00477 #endif
00478