casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PlotMSCacheBase.h
Go to the documentation of this file.
00001 //# PlotMSCacheBase.h: Generic Data cache for plotms.
00002 //# Copyright (C) 2009
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 //# $Id: $
00027 #ifndef PLOTMSCACHEBASE_H_
00028 #define PLOTMSCACHEBASE_H_
00029 
00030 #include <plotms/PlotMS/PlotMSAveraging.h>
00031 #include <plotms/PlotMS/PlotMSConstants.h>
00032 #include <plotms/PlotMS/PlotMSFlagging.h>
00033 #include <plotms/Actions/PlotMSCacheThread.qo.h>
00034 //#include <plotms/Data/PlotMSIndexer.h>
00035 
00036 #include <casa/aips.h>
00037 #include <casa/Arrays.h>
00038 #include <casa/Containers/Block.h>
00039 
00040 namespace casa {
00041 
00042 //# Forward declarations.
00043 class PlotMSApp;
00044 class PlotMSIndexer;
00045 
00046 
00047 class PlotMSCacheBase {
00048   
00049   // Friend class declarations.
00050   friend class PlotMSIndexer;
00051 
00052   //TBD:    friend class PlotMSData;
00053 
00054 public:    
00055 
00056   // Varieties of cache
00057   // TBD: move to PlotMSConstants?
00058   enum Type {MS, CAL};
00059   
00060   static const unsigned int THREAD_SEGMENT;
00061   static const PMS::Axis METADATA[];
00062   static const unsigned int N_METADATA;
00063     
00064   static bool axisIsMetaData(PMS::Axis axis);
00065 
00066   
00067   // Constructor which takes parent PlotMS.
00068   PlotMSCacheBase(PlotMSApp* parent);
00069   
00070   // Destructor
00071   virtual ~PlotMSCacheBase();
00072 
00073   // Identify myself
00074   //  pure virtual
00075   virtual PlotMSCacheBase::Type cacheType()=0;
00076 
00077   // Access to pol names
00078   virtual String polname(Int ipol)=0;
00079 
00080   // Meta axes info
00081   int nmetadata() const {return N_METADATA;};
00082   PMS::Axis metadata(int i) {return METADATA[i];};
00083   
00084   // Reference an indexer
00085   Int nIter() const { return indexer_.nelements(); };
00086   PlotMSIndexer& indexer(uInt i) {return (*indexer_[i]);};
00087   PlotMSIndexer& indexer0() { return *indexer0_; };
00088 
00089   // Report the number of chunks
00090   Int nChunk() const { return nChunk_; };
00091 
00092   // Returns whether cache is filled
00093   bool cacheReady() const { return dataLoaded_; }
00094 
00095   // Report the data shapes
00096   inline Matrix<Int>& chunkShapes() {return chshapes_;};
00097 
00098   // A chunk is good (T) if it contains data
00099   //  (when averaging, some chunks may have nrows=0)
00100   inline Bool goodChunk(Int ichunk) {return goodChunk_(ichunk); };
00101 
00102   // Is there a reference value for the specified axis?
00103   // TBD: actually make this axis-dep?
00104   inline bool hasReferenceValue(PMS::Axis axis) { return (axis==PMS::TIME && cacheReady()); };
00105   inline double referenceValue(PMS::Axis axis) { return (hasReferenceValue(axis) ? refTime() : 0.0); };
00106   
00107   // Report the reference time for this cache (in seconds)
00108   inline Double refTime() const { return refTime_p; };
00109 
00110   // Returns which axes have been loaded into the cache, including metadata.
00111   // Also includes the size (number of points) for each axis (which will
00112   // eventually be used for a cache manager to let the user know the
00113   // relative memory use of each axis).
00114   vector<pair<PMS::Axis, unsigned int> > loadedAxes() const;
00115 
00116   // Access to averaging state in the cache:
00117   PlotMSAveraging& averaging() { return averaging_; }
00118 
00119   // Access to transformations state in the cache
00120   PlotMSTransformations& transformations() { return transformations_; }
00121 
00122   // Loads the cache for the given axes and data
00123   // columns.  IMPORTANT: this method assumes that any currently loaded data is
00124   // valid for the given VisIter; i.e., if the meta-information or either of
00125   // the axes are already loaded, then they don't need to be reloaded.  If this
00126   // is not the case, then clear() should be called BEFORE append().  If a
00127   // PlotMSCacheThreadHelper object is given, it will be used to report
00128   // progress information.
00129   virtual void load(const vector<PMS::Axis>& axes,
00130                     const vector<PMS::DataColumn>& data,
00131                     const String& filename,
00132                     const PlotMSSelection& selection,
00133                     const PlotMSAveraging& averaging,
00134                     const PlotMSTransformations& transformations,
00135                     PlotMSCacheThread* thread = NULL);
00136   
00137   // Clears the cache of all stored values.  This should be called when the
00138   // underlying MS or MS selection is changed, thus invalidating stored data.
00139   void clear();
00140   
00141   // Releases the given axes from the cache.
00142   void release(const vector<PMS::Axis>& axes);
00143   
00144   // Set up indexing for the plot
00145   void setUpIndexer(PMS::Axis iteraxis=PMS::SCAN,
00146                     Bool globalXRange=False, Bool globalYRange=False);
00147 
00148   // Access to flags per chunk
00149   inline Array<Bool>& flag(Int chunk) { return *flag_[chunk]; };
00150   inline Vector<Bool>& flagrow(Int chunk) { return *flagrow_[chunk]; };
00151 
00152   // Axis-specific generic gets
00153   inline Double getScan(Int chnk,Int irel)     { return scan_(chnk);   (void)irel; };
00154   inline Double getField(Int chnk,Int irel)    { return field_(chnk);  (void)irel; };
00155   inline Double getTime(Int chnk,Int irel)     { return time_(chnk);  (void)irel; };
00156   inline Double getTimeIntr(Int chnk,Int irel) { return timeIntr_(chnk);  (void)irel; };
00157   inline Double getSpw(Int chnk,Int irel)      { return spw_(chnk);  (void)irel; };
00158 
00159   inline Double getFreq(Int chnk,Int irel) { return *(freq_[chnk]->data()+irel); };
00160   inline Double getVel(Int chnk,Int irel)  { return *(vel_[chnk]->data()+irel); };
00161   inline Double getChan(Int chnk,Int irel) { return *(chan_[chnk]->data()+irel); };
00162   inline Double getCorr(Int chnk,Int irel) { return *(corr_[chnk]->data()+irel); };
00163   inline Double getAnt1(Int chnk,Int irel) { return *(antenna1_[chnk]->data()+irel); };
00164   inline Double getAnt2(Int chnk,Int irel) { return *(antenna2_[chnk]->data()+irel); };
00165   inline Double getBsln(Int chnk,Int irel) { return *(baseline_[chnk]->data()+irel); };
00166   inline Double getUVDist(Int chnk,Int irel) { return *(uvdist_[chnk]->data()+irel); };
00167   inline Double getUVDistL(Int chnk,Int irel) { return *(uvdistL_[chnk]->data()+irel); };
00168   inline Double getU(Int chnk,Int irel) { return *(u_[chnk]->data()+irel); };
00169   inline Double getV(Int chnk,Int irel) { return *(v_[chnk]->data()+irel); };
00170   inline Double getW(Int chnk,Int irel) { return *(w_[chnk]->data()+irel); };
00171   inline Double getUwave(Int chnk,Int irel) { return *(uwave_[chnk]->data()+irel); };
00172   inline Double getVwave(Int chnk,Int irel) { return *(vwave_[chnk]->data()+irel); };
00173   inline Double getWwave(Int chnk,Int irel) { return *(wwave_[chnk]->data()+irel); };
00174 
00175   inline Double getAmp(Int chnk,Int irel)  { return *(amp_[chnk]->data()+irel); };
00176   inline Double getPha(Int chnk,Int irel)  { return *(pha_[chnk]->data()+irel); };
00177   inline Double getReal(Int chnk,Int irel) { return *(real_[chnk]->data()+irel); };
00178   inline Double getImag(Int chnk,Int irel) { return *(imag_[chnk]->data()+irel); };
00179 
00180   inline Double getFlag(Int chnk,Int irel) { return *(flag_[chnk]->data()+irel); };
00181   inline Double getFlagRow(Int chnk,Int irel) { return *(flagrow_[chnk]->data()+irel); };
00182   inline Double getRow(Int chnk,Int irel) { return *(row_[chnk]->data()+irel); };
00183 
00184   inline Double getWt(Int chnk,Int irel) { return *(wt_[chnk]->data()+irel); };
00185   inline Double getWtxAmp(Int chnk, Int irel) { return *(wtxamp_[chnk]->data()+irel); }
00186 
00187   // These are array-global (one value per chunk)
00188   inline Double getAz0(Int chnk,Int irel) { return az0_(chnk);  (void)irel; };
00189   inline Double getEl0(Int chnk,Int irel) { return el0_(chnk);  (void)irel; };
00190   inline Double getHA0(Int chnk,Int irel) { return ha0_(chnk);  (void)irel; };
00191   inline Double getPA0(Int chnk,Int irel) { return pa0_(chnk);  (void)irel; };
00192 
00193   // These are antenna-based
00194   inline Double getAntenna(Int chnk,Int irel) { return *(antenna_[chnk]->data()+irel); };
00195   inline Double getAz(Int chnk,Int irel)      { return *(az_[chnk]->data()+irel); };
00196   inline Double getEl(Int chnk,Int irel)      { return *(el_[chnk]->data()+irel); };
00197   inline Double getParAng(Int chnk,Int irel)  { return *(parang_[chnk]->data()+irel); };
00198 
00199   // These support generic non-complex calibration
00200   inline Double getPar(Int chnk,Int irel)  { return *(par_[chnk]->data()+irel); };
00201 
00202 
00203 protected:
00204     
00205   // Forbid copy for now
00206   PlotMSCacheBase(const PlotMSCacheBase&);
00207 
00208   // Increase the number of chunks
00209   void increaseChunks(Int nc=0);
00210 
00211   // Specialized method for loading the cache
00212   //  (pure virtual: implemented specifically in child classes)
00213   virtual void loadIt(vector<PMS::Axis>& loadAxes,
00214                       vector<PMS::DataColumn>& loadData,
00215                       PlotMSCacheThread* thread = NULL)=0;
00216 
00217   virtual void flagToDisk(const PlotMSFlagging& flagging,
00218                           Vector<Int>& chunks, 
00219                           Vector<Int>& relids,
00220                           Bool flag,
00221                           PlotMSIndexer* indexer)=0;
00222   
00223   // Clean up the PtrBlocks
00224   void deleteCache();
00225   void deleteIndexer();
00226 
00227   // Set the net axes mask (defines how to collapse flags for the chosen plot axes)
00228   void setAxesMask(PMS::Axis axis,Vector<Bool>& axismask);
00229 
00230   // Return the net axes mask for the currently set plot axes
00231   Vector<Bool> netAxesMask(PMS::Axis xaxis,PMS::Axis yaxis);
00232 
00233   // Derive the plot mask by appropriately collapsing the flags
00234   void setPlotMask();           // all chunks
00235   void setPlotMask(Int chunk);  // per chunk
00236 
00237   // Delete the whole plot mask
00238   void deletePlotMask();
00239 
00240   // Returns the number of points loaded for the given axis or 0 if not loaded.
00241   unsigned int nPointsForAxis(PMS::Axis axis) const;
00242   
00243   // Convenience methods that call log() with the given method name and the
00244   // appropriate event type.
00245   // <group>
00246   void logInfo(const String& method, const String& message) {
00247       log(method, message, PlotLogger::MSG_INFO); }
00248   void logDebug(const String& method, const String& message) {
00249       log(method, message, PlotLogger::MSG_DEBUG); }
00250   void logWarn(const String& method, const String& message) {
00251       log(method, message, PlotLogger::MSG_WARN); }
00252   void logError(const String& method, const String& message) {
00253       log(method, message, PlotLogger::MSG_ERROR); }
00254   
00255   void logLoad(const String& message) {
00256       log(PMS::LOG_ORIGIN_LOAD_CACHE, message, PMS::LOG_EVENT_LOAD_CACHE); }
00257   void logFlag(const String& message) {
00258       log(PMS::LOG_ORIGIN_FLAG, message, PMS::LOG_EVENT_FLAG); }
00259   // </group>
00260   
00261   // Logs the given message from the given method name as the given event type
00262   // (see PlotLogger).
00263   void log(const String& method, const String& message, int eventType);
00264 
00265   // Private data
00266   
00267   // Parent plotms.
00268   //  (used only for access to logger, so far)
00269   PlotMSApp* plotms_;
00270 
00271   // An empty indexer (its an empty PlotData object used for initialization)
00272   PlotMSIndexer* indexer0_;
00273 
00274   // The indexer into the cache
00275   PtrBlock<PlotMSIndexer*> indexer_;
00276   
00277   // The number of chunks in the cache
00278   Int nChunk_;
00279 
00280   // The reference time for this cache, in seconds
00281   Double refTime_p;
00282 
00283   // The number of antennas
00284   Int nAnt_;
00285 
00286   // Global min/max
00287   Double minX_,maxX_,minY_,maxY_;
00288 
00289   // The fundamental meta-data cache
00290   Matrix<Int> chshapes_;
00291   Vector<Bool> goodChunk_;
00292   Vector<Double> time_, timeIntr_;
00293   Vector<Int> field_, spw_, scan_;
00294   PtrBlock<Vector<uInt>*> row_;
00295   PtrBlock<Vector<Int>*> antenna1_, antenna2_, baseline_;
00296   PtrBlock<Vector<Double>*> uvdist_, u_, v_, w_;
00297   PtrBlock<Matrix<Double>*> uvdistL_, uwave_, vwave_, wwave_;
00298   PtrBlock<Vector<Double>*> freq_, vel_;
00299   PtrBlock<Vector<Int>*> chan_;
00300   PtrBlock<Vector<Int>*> corr_;
00301 
00302   // Optional parts of the cache
00303   PtrBlock<Vector<Float>*> pa_;
00304 
00305   // Data (the heavy part)
00306   PtrBlock<Array<Float>*> amp_, pha_, real_, imag_, wtxamp_;
00307   PtrBlock<Array<Bool>*> flag_;
00308   PtrBlock<Vector<Bool>*> flagrow_;
00309   
00310   PtrBlock<Array<Float>*> wt_;
00311 
00312   PtrBlock<Vector<Float>*> parang_;
00313   PtrBlock<Vector<Int>*> antenna_;
00314   PtrBlock<Vector<Double>*> az_,el_;
00315 
00316   Vector<Double> az0_,el0_,ha0_,pa0_;
00317 
00318   PtrBlock<Array<Float>*> par_;
00319 
00320   // Current setup/state.
00321   bool dataLoaded_;
00322   PMS::Axis currentX_, currentY_;
00323   map<PMS::Axis, bool> loadedAxes_;
00324   map<PMS::Axis, PMS::DataColumn> loadedAxesData_;
00325   map<PMS::Axis, bool> pendingLoadAxes_;
00326 
00327   // Global ranges
00328   Double xminG_,yminG_,xflminG_,yflminG_,xmaxG_,ymaxG_,xflmaxG_,yflmaxG_;
00329 
00330   // A copy of the Data parameters 
00331   String filename_;
00332   PlotMSSelection selection_;
00333   PlotMSAveraging averaging_;
00334   PlotMSTransformations transformations_;
00335 
00336   // Axes mask
00337   Vector<Bool> netAxesMask_;
00338 
00339   // collapsed flag mask for plotting
00340   PtrBlock<Array<Bool>*> plmask_;
00341 
00342   // meta info for locate output
00343   Vector<String> antnames_;      
00344   Vector<String> stanames_;      
00345   Vector<String> antstanames_;   
00346   Vector<String> fldnames_;      
00347 
00348 };
00349 typedef CountedPtr<PlotMSCacheBase> PlotMSCacheBasePtr;
00350 
00351 
00352 }
00353 
00354 #endif /* PLOTMSCACHEBASE_H_ */