casa  $Rev:20696$
PlotMSCache2.h
Go to the documentation of this file.
00001 //# PlotMSCache2.h: 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 PLOTMSCACHE2_H_
00028 #define PLOTMSCACHE2_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/PlotMSVBAverager.h>
00035 
00036 #include <casa/aips.h>
00037 #include <casa/Arrays.h>
00038 #include <casa/Containers/Block.h>
00039 #include <synthesis/MSVis/VisIterator.h>
00040 #include <synthesis/MSVis/VisBuffer.h>
00041 #include <synthesis/MSVis/VisBufferUtil.h>
00042 
00043 namespace casa {
00044 
00045 //# Forward declarations.
00046 class PlotMSApp;
00047 class PlotMSIndexer;
00048 
00049 class PMSCacheVolMeter {
00050 
00051 public:
00052 
00053   // Constructor/Destructor
00054   PMSCacheVolMeter();
00055   PMSCacheVolMeter(const MeasurementSet& ms, const PlotMSAveraging ave,
00056                    const Vector<Vector<Slice> >& chansel,
00057                    const Vector<Vector<Slice> >& corrsel);
00058   ~PMSCacheVolMeter();
00059 
00060   // reset (as if default ctor was run)
00061   void reset();
00062 
00063   // add in via a VisBuffer
00064   void add(const VisBuffer& vb);
00065 
00066   // add in via counts
00067   void add(Int DDID,Int nRows);
00068 
00069   // evaluate the volume for specified axes, and complain if 
00070   String evalVolume(map<PMS::Axis,Bool> axes,Vector<Bool> axesmask);
00071 
00072 private:
00073 
00074   // The number of DATA_DESCRIPTIONs
00075   Int nDDID_;
00076 
00077   // Counters
00078   Vector<uInt64> nPerDDID_,nRowsPerDDID_,nChanPerDDID_,nCorrPerDDID_;
00079 
00080   // The number of antennas (max)
00081   Int nAnt_;
00082 
00083 };
00084 
00085 class PlotMSCache2 {
00086     
00087     // Friend class declarations.
00088     friend class PlotMSData;
00089     friend class PlotMSIndexer;
00090 
00091 public:    
00092 
00093     // Varieties of cache
00094     enum Type {MS, CAL};
00095 
00096     static const PMS::Axis METADATA[];
00097     static const unsigned int N_METADATA;
00098     
00099     static bool axisIsMetaData(PMS::Axis axis);
00100     
00101     static const unsigned int THREAD_SEGMENT;
00102 
00103   // Constructor which takes parent PlotMS.
00104   PlotMSCache2(PlotMSApp* parent);
00105   
00106   // Destructor
00107   virtual ~PlotMSCache2();
00108 
00109   // Identify myself
00110   PlotMSCache2::Type cacheType() { return PlotMSCache2::MS; };
00111 
00112   // Reference an indexer
00113   Int nIter() { return indexer_.nelements(); };
00114   PlotMSIndexer& indexer(uInt i) {return (*indexer_[i]);};
00115 
00116   // Report the number of chunks
00117   Int nChunk() const { return nChunk_; };
00118 
00119   // Returns whether cache is filled
00120   bool cacheReady() const { return dataLoaded_; }
00121 
00122   // Report the data shapes
00123   inline Matrix<Int>& chunkShapes() {return chshapes_;};
00124 
00125   // A chunk is good (T) if it contains data
00126   //  (when averaging, some chunks may have nrows=0)
00127   inline Bool goodChunk(Int ichunk) {return goodChunk_(ichunk); };
00128 
00129   // Is there a reference value for the specified axis?
00130   inline bool hasReferenceValue(PMS::Axis axis) { return (axis==PMS::TIME && cacheReady()); };
00131   inline double referenceValue(PMS::Axis axis) { return (hasReferenceValue(axis) ? refTime() : 0.0); };
00132   
00133   // Report the reference time for this cache (in seconds)
00134   inline Double refTime() const { return refTime_p; };
00135 
00136   // Returns which axes have been loaded into the cache, including metadata.
00137   // Also includes the size (number of points) for each axis (which will
00138   // eventually be used for a cache manager to let the user know the
00139   // relative memory use of each axis).
00140   vector<pair<PMS::Axis, unsigned int> > loadedAxes() const;
00141 
00142   // Access to averaging state in the cache:
00143   PlotMSAveraging& averaging() { return averaging_; }
00144 
00145   // Access to transformations state in the cache
00146   PlotMSTransformations& transformations() { return transformations_; }
00147 
00148   // Access to channel averaging bounds
00149   Matrix<Int>& chanAveBounds(Int spw) { return chanAveBounds_p(spw); };
00150 
00151   // Loads the cache for the given axes and data
00152   // columns.  IMPORTANT: this method assumes that any currently loaded data is
00153   // valid for the given VisIter; i.e., if the meta-information or either of
00154   // the axes are already loaded, then they don't need to be reloaded.  If this
00155   // is not the case, then clear() should be called BEFORE append().  If a
00156   // PlotMSCacheThreadHelper object is given, it will be used to report
00157   // progress information.
00158   virtual void load(const vector<PMS::Axis>& axes,
00159                     const vector<PMS::DataColumn>& data,
00160                     const String& msname,
00161                     const PlotMSSelection& selection,
00162                     const PlotMSAveraging& averaging,
00163                     const PlotMSTransformations& transformations,
00164                     PlotMSCacheThread* thread = NULL);
00165   
00166   // Clears the cache of all stored values.  This should be called when the
00167   // underlying MS or MS selection is changed, thus invalidating stored data.
00168   void clear();
00169   
00170   // Releases the given axes from the cache.
00171   void release(const vector<PMS::Axis>& axes);
00172   
00173   // Set up indexing for the plot
00174   void setUpIndexer(PMS::Axis iteraxis=PMS::SCAN,
00175                     Bool globalXRange=False, Bool globalYRange=False);
00176 
00177   // Access to flags per chunk
00178   inline Array<Bool>& flag(Int chunk) { return *flag_[chunk]; };
00179   inline Vector<Bool>& flagrow(Int chunk) { return *flagrow_[chunk]; };
00180 
00181   // Axis-specific generic gets
00182   inline Double getScan(Int chnk,Int irel)     { return scan_(chnk);   (void)irel; };
00183   inline Double getField(Int chnk,Int irel)    { return field_(chnk);  (void)irel; };
00184   inline Double getTime(Int chnk,Int irel)     { return time_(chnk);  (void)irel; };
00185   inline Double getTimeIntr(Int chnk,Int irel) { return timeIntr_(chnk);  (void)irel; };
00186   inline Double getSpw(Int chnk,Int irel)      { return spw_(chnk);  (void)irel; };
00187 
00188   inline Double getFreq(Int chnk,Int irel) { return *(freq_[chnk]->data()+irel); };
00189   inline Double getVel(Int chnk,Int irel)  { return *(vel_[chnk]->data()+irel); };
00190   inline Double getChan(Int chnk,Int irel) { return *(chan_[chnk]->data()+irel); };
00191   inline Double getCorr(Int chnk,Int irel) { return *(corr_[chnk]->data()+irel); };
00192   inline Double getAnt1(Int chnk,Int irel) { return *(antenna1_[chnk]->data()+irel); };
00193   inline Double getAnt2(Int chnk,Int irel) { return *(antenna2_[chnk]->data()+irel); };
00194   inline Double getBsln(Int chnk,Int irel) { return *(baseline_[chnk]->data()+irel); };
00195   inline Double getUVDist(Int chnk,Int irel) { return *(uvdist_[chnk]->data()+irel); };
00196   inline Double getUVDistL(Int chnk,Int irel) { return *(uvdistL_[chnk]->data()+irel); };
00197   inline Double getU(Int chnk,Int irel) { return *(u_[chnk]->data()+irel); };
00198   inline Double getV(Int chnk,Int irel) { return *(v_[chnk]->data()+irel); };
00199   inline Double getW(Int chnk,Int irel) { return *(w_[chnk]->data()+irel); };
00200   inline Double getUwave(Int chnk,Int irel) { return *(uwave_[chnk]->data()+irel); };
00201   inline Double getVwave(Int chnk,Int irel) { return *(vwave_[chnk]->data()+irel); };
00202   inline Double getWwave(Int chnk,Int irel) { return *(wwave_[chnk]->data()+irel); };
00203 
00204   inline Double getAmp(Int chnk,Int irel)  { return *(amp_[chnk]->data()+irel); };
00205   inline Double getPha(Int chnk,Int irel)  { return *(pha_[chnk]->data()+irel); };
00206   inline Double getReal(Int chnk,Int irel) { return *(real_[chnk]->data()+irel); };
00207   inline Double getImag(Int chnk,Int irel) { return *(imag_[chnk]->data()+irel); };
00208 
00209   inline Double getFlag(Int chnk,Int irel) { return *(flag_[chnk]->data()+irel); };
00210   inline Double getFlagRow(Int chnk,Int irel) { return *(flagrow_[chnk]->data()+irel); };
00211   inline Double getRow(Int chnk,Int irel) { return *(row_[chnk]->data()+irel); };
00212 
00213   inline Double getWt(Int chnk,Int irel) { return *(wt_[chnk]->data()+irel); };
00214 
00215   // These are array-global (one value per chunk)
00216   inline Double getAz0(Int chnk,Int irel) { return az0_(chnk);  (void)irel; };
00217   inline Double getEl0(Int chnk,Int irel) { return el0_(chnk);  (void)irel; };
00218   inline Double getHA0(Int chnk,Int irel) { return ha0_(chnk);  (void)irel; };
00219   inline Double getPA0(Int chnk,Int irel) { return pa0_(chnk);  (void)irel; };
00220 
00221   // These are antenna-based
00222   inline Double getAntenna(Int chnk,Int irel) { return *(antenna_[chnk]->data()+irel); };
00223   inline Double getAz(Int chnk,Int irel)      { return *(az_[chnk]->data()+irel); };
00224   inline Double getEl(Int chnk,Int irel)      { return *(el_[chnk]->data()+irel); };
00225   inline Double getParAng(Int chnk,Int irel)  { return *(parang_[chnk]->data()+irel); };
00226 
00227 protected:
00228     
00229   // Forbid copy for now
00230   PlotMSCache2(const PlotMSCache2& mc);
00231 
00232   // Increase the number of chunks
00233   void increaseChunks(Int nc=0);
00234 
00235   // Clean up the PtrBlocks
00236   void deleteCache();
00237   void deleteIndexer();
00238 
00239   // Setup the VisIter
00240   void setUpVisIter(const String& msname,
00241                     const PlotMSSelection& selection,
00242                     Bool readonly=True,
00243                     Bool chanselect=True,
00244                     Bool corrselect=True);
00245 
00246   // Count the chunks required in the cache
00247   void countChunks(ROVisibilityIterator& vi,PlotMSCacheThread* thread);  // old
00248   void countChunks(ROVisibilityIterator& vi, Vector<Int>& nIterPerAve,  // supports time-averaging 
00249                    const PlotMSAveraging& averaging,PlotMSCacheThread* thread);
00250 
00251   // Trap attempt to use to much memory (too many points)
00252   void trapExcessVolume(map<PMS::Axis,Bool> pendingLoadAxes);
00253 
00254   // Loop over VisIter, filling the cache
00255   void loadChunks(ROVisibilityIterator& vi,
00256                   const vector<PMS::Axis> loadAxes,
00257                   const vector<PMS::DataColumn> loadData,
00258                   const PlotMSAveraging& averaging,
00259                   PlotMSCacheThread* thread);
00260   void loadChunks(ROVisibilityIterator& vi,
00261                   const PlotMSAveraging& averaging,
00262                   const Vector<Int>& nIterPerAve,
00263                   const vector<PMS::Axis> loadAxes,
00264                   const vector<PMS::DataColumn> loadData,
00265                   PlotMSCacheThread* thread);
00266 
00267   // Force read on vb for requested axes 
00268   //   (so pre-cache averaging treats all data it should)
00269   void forceVBread(VisBuffer& vb,
00270                    vector<PMS::Axis> loadAxes,
00271                    vector<PMS::DataColumn> loadData);
00272 
00273   // Tell time averager which data column to read
00274   void discernData(vector<PMS::Axis> loadAxes,
00275                    vector<PMS::DataColumn> loadData,
00276                    PlotMSVBAverager& vba);
00277 
00278   // Loads the specific axis/metadata into the cache using the given VisBuffer.
00279   void loadAxis(VisBuffer& vb, Int vbnum, PMS::Axis axis,
00280                 PMS::DataColumn data = PMS::DEFAULT_DATACOLUMN);
00281   
00282   // Set the net axes mask (defines how to collapse flags for the chosen plot axes)
00283   void setAxesMask(PMS::Axis axis,Vector<Bool>& axismask);
00284 
00285   // Return the net axes mask for the currently set plot axes
00286   Vector<Bool> netAxesMask(PMS::Axis xaxis,PMS::Axis yaxis);
00287 
00288   // Derive the plot mask by appropriately collapsing the flags
00289   void setPlotMask();           // all chunks
00290   void setPlotMask(Int chunk);  // per chunk
00291 
00292   // Delete the whole plot mask
00293   void deletePlotMask();
00294 
00295   // Set flags in the MS
00296   virtual void flagToDisk(const PlotMSFlagging& flagging,
00297                           Vector<Int>& chunks, 
00298                           Vector<Int>& relids,
00299                           Bool flag,
00300                           PlotMSIndexer* indexer);
00301 
00302   // Returns the number of points loaded for the given axis or 0 if not loaded.
00303   unsigned int nPointsForAxis(PMS::Axis axis) const;
00304   
00305   // Convenience methods that call log() with the given method name and the
00306   // appropriate event type.
00307   // <group>
00308   void logInfo(const String& method, const String& message) {
00309       log(method, message, PlotLogger::MSG_INFO); }
00310   void logDebug(const String& method, const String& message) {
00311       log(method, message, PlotLogger::MSG_DEBUG); }
00312   void logWarn(const String& method, const String& message) {
00313       log(method, message, PlotLogger::MSG_WARN); }
00314   void logError(const String& method, const String& message) {
00315       log(method, message, PlotLogger::MSG_ERROR); }
00316   
00317   void logLoad(const String& message) {
00318       log(PMS::LOG_ORIGIN_LOAD_CACHE, message, PMS::LOG_EVENT_LOAD_CACHE); }
00319   void logFlag(const String& message) {
00320       log(PMS::LOG_ORIGIN_FLAG, message, PMS::LOG_EVENT_FLAG); }
00321   // </group>
00322   
00323   // Logs the given message from the given method name as the given event type
00324   // (see PlotLogger).
00325   void log(const String& method, const String& message, int eventType);
00326 
00327   // Private data
00328   
00329   // Parent plotms.
00330   //  (used only for access to logger, so far)
00331   PlotMSApp* plotms_;
00332 
00333   // The indexer into the cache
00334   PtrBlock<PlotMSIndexer*> indexer_;
00335 
00336   // The number of chunks in the cache
00337   Int nChunk_;
00338 
00339   // The reference time for this cache, in seconds
00340   Double refTime_p;
00341 
00342   // The number of antennas
00343   Int nAnt_;
00344 
00345   // Global min/max
00346   Double minX_,maxX_,minY_,maxY_;
00347 
00348   // The fundamental meta-data cache
00349   Matrix<Int> chshapes_;
00350   Vector<Bool> goodChunk_;
00351   Vector<Double> time_, timeIntr_;
00352   Vector<Int> field_, spw_, scan_;
00353   PtrBlock<Vector<uInt>*> row_;
00354   PtrBlock<Vector<Int>*> antenna1_, antenna2_, baseline_;
00355   PtrBlock<Vector<Double>*> uvdist_, u_, v_, w_;
00356   PtrBlock<Matrix<Double>*> uvdistL_, uwave_, vwave_, wwave_;
00357   PtrBlock<Vector<Double>*> freq_, vel_;
00358   PtrBlock<Vector<Int>*> chan_;
00359   PtrBlock<Vector<Int>*> corr_;
00360 
00361   // Optional parts of the cache
00362   PtrBlock<Vector<Float>*> pa_;
00363 
00364   // Data (the heavy part)
00365   PtrBlock<Array<Float>*> amp_, pha_, real_, imag_;
00366   PtrBlock<Array<Bool>*> flag_;
00367   PtrBlock<Vector<Bool>*> flagrow_;
00368   
00369   PtrBlock<Array<Float>*> wt_;
00370 
00371   PtrBlock<Vector<Float>*> parang_;
00372   PtrBlock<Vector<Int>*> antenna_;
00373   PtrBlock<Vector<Double>*> az_,el_;
00374 
00375   Vector<Double> az0_,el0_,ha0_,pa0_;
00376 
00377   // Current setup/state.
00378   bool dataLoaded_;
00379   PMS::Axis currentX_, currentY_;
00380   map<PMS::Axis, bool> loadedAxes_;
00381   map<PMS::Axis, PMS::DataColumn> loadedAxesData_;
00382 
00383   // Global ranges
00384   Double xminG_,yminG_,xflminG_,yflminG_,xmaxG_,ymaxG_,xflmaxG_,yflmaxG_;
00385 
00386   // A copy of the Data parameters 
00387   String msname_;
00388   PlotMSSelection selection_;
00389   PlotMSAveraging averaging_;
00390   PlotMSTransformations transformations_;
00391 
00392   // Axes mask
00393   Vector<Bool> netAxesMask_;
00394 
00395   // collapsed flag mask for plotting
00396   PtrBlock<Array<Bool>*> plmask_;
00397 
00398   // meta info for locate output
00399   Vector<String> antnames_;      
00400   Vector<String> stanames_;      
00401   Vector<String> antstanames_;   
00402   Vector<String> fldnames_;      
00403 
00404   // A container for channel averaging bounds
00405   Vector<Matrix<Int> > chanAveBounds_p;
00406 
00407   // Provisional flagging helpers
00408   Vector<Int> nVBPerAve_;
00409 
00410   // VisIterator pointer
00411   ROVisIterator* rvi_p;
00412   VisIterator* wvi_p;
00413 
00414   // VisBufferUtil for freq/vel calculations
00415   VisBufferUtil vbu_;
00416 
00417   // Volume meter for volume calculation
00418   PMSCacheVolMeter vm_;
00419 
00420     
00421 };
00422 typedef CountedPtr<PlotMSCache2> PlotMSCache2Ptr;
00423 
00424 
00425 }
00426 
00427 #endif /* PLOTMSCACHE2_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines