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/PlotMS/PlotMSTransformations.h>
00034 #include <plotms/PlotMS/PlotMSCalibration.h>
00035 
00036 #include <casa/aips.h>
00037 #include <casa/Arrays.h>
00038 #include <casa/Containers/Block.h>
00039 #include <measures/Measures/MFrequency.h>
00040 
00041 #include <QVector>
00042 
00043 namespace casa {
00044 
00045 //# Forward declarations.
00046 class PlotMSApp;
00047 class PlotMSIndexer;
00048 class ThreadCommunication;
00049 
00050 class PlotMSCacheBase {
00051   
00052   // Friend class declarations.
00053   friend class PlotMSIndexer;
00054 
00055   //TBD:    friend class PlotMSData;
00056 
00057 public:    
00058 
00059   // Varieties of cache
00060   // TBD: move to PlotMSConstants?
00061   enum Type {MS, CAL};
00062   
00063   static const unsigned int THREAD_SEGMENT;
00064   static const PMS::Axis METADATA[];
00065   static const unsigned int N_METADATA;
00066     
00067   static bool axisIsMetaData(PMS::Axis axis);
00068 
00069   
00070   // Constructor which takes parent PlotMS.
00071   PlotMSCacheBase(PlotMSApp* parent);
00072   
00073   // Destructor
00074   virtual ~PlotMSCacheBase();
00075 
00076   // Identify myself
00077   //  pure virtual
00078   virtual PlotMSCacheBase::Type cacheType() const = 0;
00079 
00080   // Access to pol names
00081   virtual String polname(Int ipol)=0;
00082 
00083   // keep MS/CT filename (set cal type for CT)
00084   virtual void setFilename(String filename) = 0;
00085   String calType() const { return calType_; };
00086   bool polnRatio() const { return polnRatio_; };
00087 
00088   // Meta axes info
00089   int nmetadata() const {return N_METADATA;};
00090   PMS::Axis metadata(int i) {return METADATA[i];};
00091   
00092   // Reference an indexer; returns -1 if there is no indexer
00093   // for the given dataIndex.
00094   Int nIter( int dataIndex ) const;
00095 
00096   PlotMSIndexer& indexer( int dataIndex, uInt i) {
00097           return (*indexer_[dataIndex][i]);
00098   };
00099   PlotMSIndexer& indexer0() {
00100           return *indexer0_;
00101   };
00102   void resizeIndexer( int size );
00103   int getDataCount() const {
00104           return currentX_.size();
00105   }
00106 
00107   PMS::Axis getIterAxis() const;
00108 
00109   // Report the number of chunks
00110   Int nChunk() const { return nChunk_; };
00111 
00112   // Returns whether cache is filled
00113   bool cacheReady() const { return dataLoaded_; }
00114 
00115   // Returns whether user canceled during loading chunks
00116   bool wasCanceled() const { return userCanceled_; }
00117 
00118   // Report the data shapes
00119   inline Matrix<Int>& chunkShapes() {return chshapes_;};
00120 
00121   // A chunk is good (T) if it contains data
00122   //  (when averaging, some chunks may have nrows=0)
00123   inline Bool goodChunk(Int ichunk) {return goodChunk_(ichunk); };
00124 
00125   // Is there a reference value for the specified axis?
00126   // TBD: actually make this axis-dep?
00127   inline bool hasReferenceValue(PMS::Axis axis) { return (axis==PMS::TIME && cacheReady()); };
00128   inline double referenceValue(PMS::Axis axis) { return (hasReferenceValue(axis) ? refTime() : 0.0); };
00129   
00130   // Report the reference time for this cache (in seconds)
00131   inline Double refTime() const { return refTime_p; };
00132 
00133   // Frequency frame in original MS or requested by user
00134   inline MFrequency::Types getFreqFrame() const { return freqFrame_; };
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   std::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   // Loads the cache for the given axes and data
00149   // columns.  IMPORTANT: this method assumes that any currently loaded data is
00150   // valid for the given VisIter; i.e., if the meta-information or either of
00151   // the axes are already loaded, then they don't need to be reloaded.  If this
00152   // is not the case, then clear() should be called BEFORE append().  If a
00153   // PlotMSCacheThreadHelper object is given, it will be used to report
00154   // progress information.
00155   virtual void load(const std::vector<PMS::Axis>& axes,
00156                     const std::vector<PMS::DataColumn>& data,
00157                     const String& filename,
00158                     const PlotMSSelection& selection,
00159                     const PlotMSAveraging& averaging,
00160                     const PlotMSTransformations& transformations,
00161                     const PlotMSCalibration& calibration,
00162                     /*PlotMSCacheThread**/ThreadCommunication* thread = NULL);
00163   
00164   // Clears the cache of all stored values.  This should be called when the
00165   // underlying MS or MS selection is changed, thus invalidating stored data.
00166   void clear();
00167   void clearRanges();
00168   
00169   // Releases the given axes from the cache.
00170   void release(const std::vector<PMS::Axis>& axes);
00171   
00172   // Set up indexing for the plot
00173   bool isIndexerInitialized( PMS::Axis iteraxis, Bool globalXRange,
00174                 Bool globalYRange, int dataIndex ) const;
00175   void setUpIndexer(PMS::Axis iteraxis=PMS::SCAN,
00176                     Bool globalXRange=False, Bool globalYRange=False, int dataIndex = 0);
00177 
00178   // Access to flags per chunk
00179   inline Array<Bool>& flag(Int chunk) { return *flag_[chunk]; };
00180   inline Vector<Bool>& flagrow(Int chunk) { return *flagrow_[chunk]; };
00181 
00182   // Axis-specific generic gets
00183   inline Double getScan(Int chnk,Int irel)     { return scan_(chnk);   (void)irel; };
00184   inline Double getField(Int chnk,Int irel)    { return field_(chnk);  (void)irel; };
00185   inline Double getTime(Int chnk,Int irel)     { return time_(chnk);  (void)irel; };
00186   inline Double getTimeIntr(Int chnk,Int irel) { return timeIntr_(chnk);  (void)irel; };
00187   inline Double getSpw(Int chnk,Int irel)      { return spw_(chnk);  (void)irel; };
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 getRow(Int chnk,Int irel) { return *(row_[chnk]->data()+irel); };
00196   inline Double getObsid(Int chnk,Int irel) { return *(obsid_[chnk]->data()+irel); };
00197   inline Double getIntent(Int chnk,Int irel) { return *(intent_[chnk]->data()+irel); };
00198   inline Double getFeed1(Int chnk,Int irel) { return *(feed1_[chnk]->data()+irel); };
00199   inline Double getFeed2(Int chnk,Int irel) { return *(feed2_[chnk]->data()+irel); };
00200 
00201   inline Double getAmp(Int chnk,Int irel)  { return *(amp_[chnk]->data()+irel); };
00202   inline Double getPha(Int chnk,Int irel)  { return *(pha_[chnk]->data()+irel); };
00203   inline Double getReal(Int chnk,Int irel) { return *(real_[chnk]->data()+irel); };
00204   inline Double getImag(Int chnk,Int irel) { return *(imag_[chnk]->data()+irel); };
00205   inline Double getFlag(Int chnk,Int irel) { return *(flag_[chnk]->data()+irel); };
00206   inline Double getFlagRow(Int chnk,Int irel) { return *(flagrow_[chnk]->data()+irel); };
00207 
00208   inline Double getWt(Int chnk,Int irel) { return *(wt_[chnk]->data()+irel); };
00209   inline Double getWtxAmp(Int chnk, Int irel) { return *(wtxamp_[chnk]->data()+irel); }
00210   inline Double getWtSp(Int chnk,Int irel) { return *(wtsp_[chnk]->data()+irel); };
00211   inline Double getSigma(Int chnk,Int irel) { return *(sigma_[chnk]->data()+irel); };
00212   inline Double getSigmaSp(Int chnk,Int irel) { return *(sigmasp_[chnk]->data()+irel); };
00213 
00214   inline Double getUVDist(Int chnk,Int irel) { return *(uvdist_[chnk]->data()+irel); };
00215   inline Double getUVDistL(Int chnk,Int irel) { return *(uvdistL_[chnk]->data()+irel); };
00216   inline Double getU(Int chnk,Int irel) { return *(u_[chnk]->data()+irel); };
00217   inline Double getV(Int chnk,Int irel) { return *(v_[chnk]->data()+irel); };
00218   inline Double getW(Int chnk,Int irel) { return *(w_[chnk]->data()+irel); };
00219   inline Double getUwave(Int chnk,Int irel) { return *(uwave_[chnk]->data()+irel); };
00220   inline Double getVwave(Int chnk,Int irel) { return *(vwave_[chnk]->data()+irel); };
00221   inline Double getWwave(Int chnk,Int irel) { return *(wwave_[chnk]->data()+irel); };
00222 
00223   // These are array-global (one value per chunk)
00224   inline Double getAz0(Int chnk,Int irel) { return az0_(chnk);  (void)irel; };
00225   inline Double getEl0(Int chnk,Int irel) { return el0_(chnk);  (void)irel; };
00226   inline Double getRadialVelocity0(Int chnk, Int irel){ return radialVelocity_(chnk); (void)irel;};
00227   inline Double getRHO0(Int chnk, Int irel){return rho_(chnk); (void)irel; };
00228   inline Double getHA0(Int chnk,Int irel) { return ha0_(chnk);  (void)irel; };
00229   inline Double getPA0(Int chnk,Int irel) { return pa0_(chnk);  (void)irel; };
00230 
00231   // These are antenna-based
00232   inline Double getAntenna(Int chnk,Int irel) { return *(antenna_[chnk]->data()+irel); };
00233   inline Double getAz(Int chnk,Int irel)      { return *(az_[chnk]->data()+irel); };
00234   inline Double getEl(Int chnk,Int irel)      { return *(el_[chnk]->data()+irel); };
00235   inline Double getParAng(Int chnk,Int irel)  { return *(parang_[chnk]->data()+irel); };
00236 
00237   // These support generic non-complex calibration
00238   inline Double getPar(Int chnk,Int irel)  { return *(par_[chnk]->data()+irel); };
00239   inline Double getSnr(Int chnk,Int irel)  { return *(snr_[chnk]->data()+irel); };
00240 
00241   // Returns a list of channel numbers that were averaged together in that chunk
00242   inline Vector<Int> getChansPerBin(Int chnk,Int irel) { return (*chansPerBin_[chnk])[irel]; };
00243 
00244   Record locateInfo(int plotIterIndex, const Vector<PlotRegion>& regions,
00245                 bool showUnflagged, bool showFlagged, bool selectAll );
00246 
00247   PlotLogMessage* locateRange( int plotIterIndex, const Vector<PlotRegion> & regions,
00248                 bool showUnflagged, bool showFlagged);
00249 
00250   PlotLogMessage* flagRange( int plotIterIndex, casa::PlotMSFlagging& flagging,
00251                 const Vector<PlotRegion>& regions, bool showFlagged);
00252 
00253   //Return a formatted string for time iteration plots giving the time range.
00254   String getTimeBounds( int iterValue );
00255   // Return the time as doubles 
00256   pair<Double,Double> getTimeBounds() const;
00257   // Return the axes ranges
00258   pair<Double,Double> getXAxisBounds() const;
00259   pair<Double,Double> getYAxisBounds() const;
00260 
00261 protected:
00262     
00263   // Forbid copy for now
00264   PlotMSCacheBase(const PlotMSCacheBase&);
00265 
00266   // Increase the number of chunks
00267   void increaseChunks(Int nc=0);
00268 
00269   // Specialized method for loading the cache
00270   //  (pure virtual: implemented specifically in child classes)
00271   virtual void loadIt(std::vector<PMS::Axis>& loadAxes,
00272                       std::vector<PMS::DataColumn>& loadData,
00273                       /*PlotMSCacheThread**/ThreadCommunication* thread = NULL)=0;
00274 
00275   virtual void flagToDisk(const PlotMSFlagging& flagging,
00276                           Vector<Int>& chunks, 
00277                           Vector<Int>& relids,
00278                           Bool flag,
00279                           PlotMSIndexer* indexer, int dataIndex)=0;
00280   
00281   // Clean up the PtrBlocks
00282   void deleteCache();
00283   void deleteIndexer();
00284 
00285 
00286 
00287   virtual bool isEphemeris() {return false;};
00288   bool isEphemerisAxis( PMS::Axis axis ) const;
00289   // Set the net axes mask (defines how to collapse flags for the chosen plot axes)
00290   void setAxesMask(PMS::Axis axis,Vector<Bool>& axismask);
00291 
00292   // Return the net axes mask for the currently set plot axes
00293   Vector<Bool> netAxesMask(PMS::Axis xaxis,PMS::Axis yaxis);
00294 
00295   // Derive the plot mask by appropriately collapsing the flags
00296   void setPlotMask( Int dataIndex);           // all chunks
00297   void setPlotMask(Int dataIndex, Int chunk);  // per chunk
00298 
00299   // Delete the whole plot mask
00300   void deletePlotMask();
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   //Return the color lookup index for the chunk.
00328   int findColorIndex( int chunk, bool initialize );
00329 
00330 
00331 
00332   // Private data
00333   
00334   // Parent plotms.
00335   //  (used only for access to logger, so far)
00336   PlotMSApp* plotms_;
00337 
00338   // An empty indexer (its an empty PlotData object used for initialization)
00339   PlotMSIndexer* indexer0_;
00340 
00341   // The indexer into the cache
00342   std::vector<PtrBlock<PlotMSIndexer*> > indexer_;
00343   
00344   // The number of chunks in the cache
00345   Int nChunk_;
00346 
00347   // The reference time for this cache, in seconds
00348   Double refTime_p;
00349 
00350   // The number of antennas
00351   Int nAnt_;
00352 
00353   // Set frame from VI if not specified by user
00354   // (for VI2::getFrequencies and axis label)
00355   MFrequency::Types freqFrame_;
00356 
00357   // Global min/max
00358   Double minX_,maxX_,minY_,maxY_;
00359 
00360   // The fundamental meta-data cache
00361   Matrix<Int> chshapes_;
00362   Vector<Bool> goodChunk_;
00363   Vector<Double> time_, timeIntr_;
00364   Vector<Int> field_, spw_, scan_;
00365   PtrBlock<Vector<uInt>*> row_;
00366   PtrBlock<Vector<Int>*> antenna1_, antenna2_, baseline_;
00367   PtrBlock<Vector<Double>*> uvdist_, u_, v_, w_;
00368   PtrBlock<Matrix<Double>*> uvdistL_, uwave_, vwave_, wwave_;
00369   PtrBlock<Vector<Double>*> freq_, vel_;
00370   PtrBlock<Vector<Int>*> chan_;
00371   PtrBlock<Array<Int>*> chansPerBin_;
00372   PtrBlock<Vector<Int>*> corr_;
00373   PtrBlock<Vector<Int>*> obsid_;
00374   PtrBlock<Vector<Int>*> intent_;
00375   PtrBlock<Vector<Int>*> feed1_, feed2_;
00376 
00377   // Optional parts of the cache
00378   PtrBlock<Vector<Float>*> pa_;
00379 
00380   // Data (the heavy part)
00381   PtrBlock<Array<Float>*> amp_, pha_, real_, imag_, wtxamp_;
00382   PtrBlock<Array<Bool>*> flag_;
00383   PtrBlock<Vector<Bool>*> flagrow_;
00384   
00385   PtrBlock<Array<Float>*> wt_,wtsp_;
00386   PtrBlock<Array<Float>*> sigma_,sigmasp_;
00387 
00388   PtrBlock<Vector<Float>*> parang_;
00389   PtrBlock<Vector<Int>*> antenna_;
00390   PtrBlock<Vector<Double>*> az_,el_;
00391 
00392 
00393   Vector<Double> radialVelocity_, rho_;
00394   Vector<Double> az0_,el0_,ha0_,pa0_;
00395 
00396   PtrBlock<Array<Float>*> par_, snr_, tsys_;
00397 
00398   // Current setup/state.
00399   bool dataLoaded_;
00400   bool userCanceled_;
00401   std::vector<PMS::Axis> currentX_;
00402   std::vector<PMS::Axis> currentY_;
00403   map<PMS::Axis, bool> loadedAxes_;
00404   map<PMS::Axis, PMS::DataColumn> loadedAxesData_;
00405   map<PMS::Axis, bool> pendingLoadAxes_;
00406 
00407   // Global ranges
00408   Double xminG_,yminG_,xflminG_,yflminG_,xmaxG_,ymaxG_,xflmaxG_,yflmaxG_;
00409 
00410   // A copy of the Data parameters 
00411   String filename_;
00412   PlotMSSelection selection_;
00413   PlotMSAveraging averaging_;
00414   PlotMSTransformations transformations_;
00415   PlotMSCalibration calibration_;
00416 
00417   // Axes mask
00418   std::vector<Vector<Bool> > netAxesMask_;
00419 
00420   // collapsed flag mask for plotting
00421   std::vector<PtrBlock<Array<Bool>* > > plmask_;
00422 
00423   // meta info for locate output
00424   Vector<String> antnames_;      
00425   Vector<String> stanames_;      
00426   Vector<String> antstanames_;   
00427   Vector<String> fldnames_;
00428   Vector<String> intentnames_;
00429   Array<Double> positions_;
00430 
00431   PMS::Axis iterAxis;
00432   bool ephemerisInitialized;
00433   ::QVector<double> uniqueTimes;
00434 
00435   // The calibration type (Table subType)
00436   String calType_;
00437   // polarization selection is ratio ("/")
00438   bool polnRatio_;
00439 
00440 private:
00441   void _updateAntennaMask( Int a, Vector<Bool>& antMask, const Vector<Int> selectedAntennas );
00442   bool axisIsValid(PMS::Axis axis, const PlotMSAveraging& averaging);
00443 
00444 };
00445 typedef CountedPtr<PlotMSCacheBase> PlotMSCacheBasePtr;
00446 
00447 
00448 }
00449 
00450 #endif /* PLOTMSCACHEBASE_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1