casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MSMetaDataOnDemand.h
Go to the documentation of this file.
00001 //# MSMetaData.h
00002 //# Copyright (C) 1998,1999,2000,2001
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 #ifndef MS_MSMETADATAONDEMAND_H
00028 #define MS_MSMETADATAONDEMAND_H
00029 
00030 #include <ms/MeasurementSets/MSMetaData.h>
00031 
00032 #include <measures/Measures/MPosition.h>
00033 #include <ms/MeasurementSets/MeasurementSet.h>
00034 
00035 #include <tr1/memory>
00036 
00037 namespace casa {
00038 
00039 // <summary>
00040 // Class to interrogate  an MS for metadata. Interrogation happens on demand
00041 // and resulting metadata are stored if the cache has not exceeded the specified
00042 // limit.
00043 // </summary>
00044 
00045 class MSMetaDataOnDemand : public MSMetaData {
00046 
00047 public:
00048 
00049         // construct an object which stores a pointer to the MS and queries the MS
00050         // only as necessary. The MeasurementSet pointer passed in should not go out
00051         // of scope in the calling code until the caller has finished with this object,
00052         // or else subsequent method calls on this object will result in a segmentation
00053         // fault; the pointer is not copied.
00054         // <src>maxCacheSizeMB</src> is the maximum cache size in megabytes. <=0 means
00055         // do not use a cache.
00056         MSMetaDataOnDemand(const MeasurementSet *const &ms, const Float maxCacheSizeMB);
00057 
00058         virtual ~MSMetaDataOnDemand();
00059 
00060         // number of unique states (number of rows from the STATE table)
00061         uInt nStates();
00062 
00063         // get unique scan numbers
00064         std::set<Int> getScanNumbers();
00065 
00066         std::set<Int> getScansForState(const Int stateID);
00067 
00068         std::set<String> getIntentsForScan(const Int scan);
00069 
00070         // get all intents, in no particular (nor guaranteed) order.
00071         std::set<String> getIntents();
00072 
00073         // get a set of spectral windows for which the specified <src>intent</src>
00074         // applies.
00075         virtual std::set<uInt> getSpwsForIntent(const String& intent);
00076 
00077         // get number of spectral windows
00078         uInt nSpw(Bool includewvr);
00079 
00080         // get the number of visibilities
00081         uInt nRows();
00082 
00083         uInt nRows(CorrelationType cType);
00084 
00085         uInt nRows(
00086                 CorrelationType cType, Int arrayID, Int observationID,
00087                 Int scanNumber, Int fieldID
00088         );
00089 
00090         uInt nRows(CorrelationType cType, Int fieldID);
00091 
00092         // get a set of intents corresponding to the specified spectral window
00093         std::set<String> getIntentsForSpw(const uInt spw);
00094 
00095 
00096         // get a set of intents corresponding to a specified field
00097         virtual std::set<String> getIntentsForField(Int fieldID);
00098 
00099         // get the number of fields.
00100         uInt nFields();
00101 
00102         // get a set of spectral windows corresponding to the specified fieldID
00103         std::set<uInt> getSpwsForField(const Int fieldID);
00104 
00105         // get a set of spectral windows corresponding to the specified field name
00106         std::set<uInt> getSpwsForField(const String& fieldName);
00107 
00108         // get the set of field IDs corresponding to the specified spectral window.
00109         std::set<Int> getFieldIDsForSpw(const uInt spw);
00110 
00111         // get the set of field names corresponding to the specified spectral window.
00112         std::set<String> getFieldNamesForSpw(const uInt spw);
00113 
00114         // get the number of scans in the dataset
00115         uInt nScans();
00116 
00117         // get the number of observations (from the OBSERVATIONS table) in the dataset
00118         uInt nObservations();
00119 
00120         // get the number of arrays (from the ARRAY table) in the dataset
00121         uInt nArrays();
00122 
00123         // get the set of spectral windows for the specified scan number.
00124         std::set<uInt> getSpwsForScan(const Int scan);
00125 
00126         // get the set of scan numbers for the specified spectral window.
00127         std::set<Int> getScansForSpw(const uInt spw);
00128 
00129         // get the number of antennas in the ANTENNA table
00130         uInt nAntennas();
00131 
00132         // get the name of the antenna for the specified antenna ID
00133         vector<String> getAntennaNames(
00134                 std::map<String, uInt>& namesToIDsMap,
00135                 const vector<uInt>& antennaIDs=vector<uInt>(0)
00136         );
00137 
00138         // get the antenna ID for the antenna with the specified name.
00139         vector<uInt> getAntennaIDs(     const vector<String>& antennaNames);
00140 
00141         // get set of spectral windows used for TDM. These are windows that have
00142         // 64, 128, or 256 channels
00143         std::set<uInt> getTDMSpw();
00144 
00145         // get set of spectral windows used for FDM. These are windows that do not
00146         // have 1, 4, 64, 128, or 256 channels.
00147         std::set<uInt> getFDMSpw();
00148 
00149         // get spectral windows that have been averaged. These are windows with 1 channel.
00150         std::set<uInt> getChannelAvgSpw();
00151 
00152         // Get the spectral window set used for WVR measurements. These have 4 channels each.
00153         std::set<uInt> getWVRSpw();
00154 
00155         // Get the scans which fail into the specified time range (center-tol to center+tol)
00156         std::set<Int> getScansForTimes(const Double center, const Double tol);
00157 
00158         // Get the times for the specified scans
00159         std::set<Double> getTimesForScans(const std::set<Int>& scans);
00160 
00161         // get the time range for the specified scan. The vector returned will contain two elements,
00162         // the start and stop time of the scan, determined from min(TIME_CENTROID(x)-0.5*INTERVAL(x)) and
00163         // max(TIME_CENTROID(x)-0.5*INTERVAL(x))
00164         std::vector<Double> getTimeRangeForScan(Int scan);
00165 
00166         // get the times for the specified scan
00167         // std::set<Double> getTimesForScan(const uInt scan) const;
00168 
00169         // get the stateIDs associated with the specified scan number.
00170         std::set<Int> getStatesForScan(const Int scan);
00171 
00172         // get the scans associated with the specified intent
00173         std::set<Int> getScansForIntent(const String& intent);
00174 
00175         // get the scan numbers associated with the specified field ID.
00176         std::set<Int> getScansForFieldID(const Int fieldID);
00177 
00178         // get the field IDs for the specified field name. Case insensitive.
00179         std::set<Int> getFieldIDsForField(const String& field);
00180 
00181         // get field IDs associated with the specified scan number.
00182         std::set<Int> getFieldsForScan(const Int scan);
00183 
00184         // get the field IDs associated with the specified scans
00185         std::set<Int> getFieldsForScans(const std::set<Int>& scans);
00186 
00187         // get the field IDs associated with the specified intent.
00188         std::set<Int> getFieldsForIntent(const String& intent);
00189 
00190         // get the field names associated with the specified field IDs. If <src>fieldIDs</src>
00191         // is empty, a vector of all the field names is returned.
00192         vector<String> getFieldNamesForFieldIDs(const vector<Int>& fieldIDs);
00193 
00194         // Get the fields which fail into the specified time range (center-tol to center+tol)
00195         std::set<Int> getFieldsForTimes(Double center, Double tol);
00196 
00197         // get the times for which the specified field was observed
00198         std::set<Double> getTimesForField(Int fieldID);
00199 
00200         // get telescope names in the order they are listed in the OBSERVATION table. These are
00201         // the telescopes (observatories), not the antenna names.
00202         vector<String> getObservatoryNames();
00203 
00204         // get the position of the specified telescope (observatory).
00205         MPosition getObservatoryPosition(uInt which);
00206 
00207         // get the positions of the specified antennas. If <src>which</src> is empty, return
00208         // all antenna positions.
00209         vector<MPosition> getAntennaPositions(const vector<uInt>& which=std::vector<uInt>(0));
00210 
00211         // <src>names</src> cannot be empty.
00212         vector<MPosition> getAntennaPositions(const vector<String>& names);
00213 
00214         // get the position of the specified antenna relative to the observatory position.
00215         // the three vector returned represents the longitudinal, latitudinal, and elevation
00216         // offsets (elements 0, 1, and 2 respectively). The longitude and latitude offsets are
00217         // measured along the surface of a sphere centered at the earth's center and whose surface
00218         // intersects the position of the observatory.
00219         Quantum<Vector<Double> > getAntennaOffset(uInt which);
00220 
00221         Quantum<Vector<Double> > getAntennaOffset(const String& name);
00222 
00223         // if not empty, <src>positions</src> must contain the same number of elements as the
00224         // number of antennas in the MS. These will be used instead of also retrieving the antenna
00225         // positions from the MS.
00226         vector<Quantum<Vector<Double> > > getAntennaOffsets(
00227                 const vector<MPosition>& positions=vector<MPosition>(0)
00228         );
00229 
00230         //std::map<Double, Double> getExposuresForTimes() const;
00231 
00232         // get the unique baselines in the MS. These are not necessarily every combination of the
00233         // n(n-1)/2 possible antenna pairs, but rather the number of unique baselines represented in
00234         // the main MS table, which in theory can be less than n(n-1)/2 (for example if samples for
00235         // certain antenna pairs are not recorded. The returned Matrix is nAnts x nAnts in size. Pairs
00236         // that are true represent baselines represented in the main MS table.
00237         Matrix<Bool> getUniqueBaselines();
00238 
00239         // get the effective total exposure time. This is the effective time spent collecting unflagged data.
00240         Quantity getEffectiveTotalExposureTime();
00241 
00242         // get the number of unflagged rows
00243         Double nUnflaggedRows();
00244 
00245         Double nUnflaggedRows(CorrelationType cType);
00246 
00247         Double nUnflaggedRows(
00248                 CorrelationType cType, Int arrayID, Int observationID,
00249                 Int scanNumber, Int fieldID
00250         );
00251 
00252         Double nUnflaggedRows(CorrelationType cType, Int fieldID);
00253 
00254         inline Float getCache() const { return _cacheMB;}
00255 
00256         vector<Double> getBandWidths();
00257 
00258         vector<Quantum<Vector<Double> > > getChanFreqs();
00259 
00260         vector<vector<Double> > getChanWidths();
00261 
00262         vector<Int> getNetSidebands();
00263 
00264         vector<Quantity> getMeanFreqs();
00265 
00266         vector<uInt> nChans();
00267 
00268         vector<vector<Double> > getEdgeChans();
00269 
00270         vector<uInt> getBBCNos();
00271 
00272         vector<String> getSpwNames();
00273 
00274         std::map<uInt, Double> getAverageIntervalsForScan(Int scan);
00275 
00276 
00277 private:
00278         const MeasurementSet* _ms;
00279         Float _cacheMB;
00280         const Float _maxCacheMB;
00281         uInt _nStates, _nACRows, _nXCRows, _nSpw, _nFields, _nAntennas,
00282                 _nObservations, _nScans, _nArrays, _nrows;
00283         std::set<String> _uniqueIntents;
00284         std::map<Int, std::set<uInt> > _scanToSpwsMap;
00285         std::set<Int> _uniqueScanNumbers, _uniqueFieldIDs, _uniqueStateIDs;
00286         std::set<uInt> _avgSpw, _tdmSpw, _fdmSpw, _wvrSpw;
00287         std::tr1::shared_ptr<Vector<Int> > _antenna1, _antenna2, _scans, _fieldIDs,
00288                 _stateIDs, _dataDescIDs, _observationIDs, _arrayIDs;
00289         std::tr1::shared_ptr<AOSFMapI> _scanToNACRowsMap, _scanToNXCRowsMap;
00290         std::tr1::shared_ptr<std::map<Int, uInt> > _fieldToNACRowsMap, _fieldToNXCRowsMap;
00291         std::map<Int, uInt> _dataDescIDToSpwMap;
00292         std::map<Int, std::set<String> > _scanToIntentsMap;
00293         vector<std::set<String> > _stateToIntentsMap, _spwToIntentsMap, _fieldToIntentsMap;
00294         vector<SpwProperties> _spwInfo;
00295         std::map<Int, std::set<uInt> > _fieldToSpwMap;
00296         vector<std::set<Int> > _spwToFieldIDsMap, _spwToScansMap;
00297         std::map<Int, std::set<Int> > _scanToStatesMap, _scanToFieldsMap, _fieldToScansMap,
00298                 _fieldToStatesMap, _stateToFieldsMap;
00299         vector<String> _fieldNames, _antennaNames, _observatoryNames;
00300         std::map<String, uInt> _antennaNameToIDMap;
00301         std::tr1::shared_ptr<Vector<Double> > _times;
00302         std::tr1::shared_ptr<std::map<Int, std::set<Double> > > _scanToTimesMap;
00303         std::map<String, std::set<Int> > _intentToFieldIDMap, _intentToScansMap;
00304         std::tr1::shared_ptr<std::map<Int, std::set<Double> > > _fieldToTimesMap;
00305         std::tr1::shared_ptr<std::map<Double, std::set<Int> > > _timeToFieldsMap;
00306 
00307         vector<MPosition> _observatoryPositions, _antennaPositions;
00308         vector<Quantum<Vector<Double> > > _antennaOffsets;
00309         Matrix<Bool> _uniqueBaselines;
00310         Quantity _exposureTime;
00311         Double _nUnflaggedACRows, _nUnflaggedXCRows;
00312         std::tr1::shared_ptr<std::map<Int, Double> > _unflaggedFieldNACRows, _unflaggedFieldNXCRows;
00313         std::tr1::shared_ptr<AOSFMapD> _unflaggedScanNACRows, _unflaggedScanNXCRows;
00314         const String _taqlTableName;
00315         const vector<const Table*> _taqlTempTable;
00316         std::tr1::shared_ptr<ArrayColumn<Bool> > _flagsColumn;
00317         std::map<Int, vector<Double> > _scanToTimeRangeMap;
00318         std::map<Int, std::map<uInt, Double> > _scanSpwToIntervalMap;
00319 
00320         // disallow copy constructor and = operator
00321         MSMetaDataOnDemand(const MSMetaDataOnDemand&);
00322         MSMetaDataOnDemand operator =(const MSMetaDataOnDemand&);
00323 
00324         // This comment from thunter in the original ValueMapping python class
00325         // # Determine the number of polarizations for the first OBSERVE_TARGET intent.
00326     // # Used by plotbandpass for BPOLY plots since the number of pols cannot be inferred
00327     // # correctly from the caltable alone.  You cannot not simply use the first row, because
00328     // # it may be a pointing scan which may have different number of polarizations than what
00329     // # the TARGET and BANDPASS calibrator will have.
00330     // # -- T. Hunter
00331         // uInt _getNumberOfPolarizations();
00332 
00333         void _setSpwInfo(const MeasurementSet& ms);
00334 
00335         // set metadata from OBSERVATION table
00336         void _setObservation(const MeasurementSet& ms);
00337 
00338         // static vector<uInt> _toUIntVector(const Vector<Int>& v);
00339 
00340         static void _checkScan(const Int scan, const std::set<Int> allScans);
00341 
00342         Bool _hasIntent(const String& intent);
00343 
00344         void _checkFieldID(const Int fieldID);
00345 
00346         void _checkFieldIDs(const vector<Int>& fieldIDs);
00347 
00348         void _checkStateID(const Int stateID);
00349 
00350         vector<std::set<String> > _getSpwToIntentsMap();
00351 
00352         void _getAntennas(
00353                 std::tr1::shared_ptr<Vector<Int> >& ant1,
00354                 std::tr1::shared_ptr<Vector<Int> >& ant2
00355         );
00356 
00357         std::tr1::shared_ptr<Vector<Int> > _getScans();
00358 
00359         std::tr1::shared_ptr<Vector<Int> > _getObservationIDs();
00360 
00361         std::tr1::shared_ptr<Vector<Int> > _getArrayIDs();
00362 
00363         std::tr1::shared_ptr<Vector<Int> > _getFieldIDs();
00364 
00365         std::tr1::shared_ptr<Vector<Int> > _getStateIDs();
00366 
00367         std::tr1::shared_ptr<Vector<Int> > _getDataDescIDs();
00368 
00369         std::tr1::shared_ptr<Vector<Double> > _getTimes();
00370 
00371         std::tr1::shared_ptr<ArrayColumn<Bool> > _getFlags();
00372 
00373         std::set<Int> _getUniqueFiedIDs();
00374 
00375         std::map<Int, std::set<Int> > _getScanToStatesMap();
00376 
00377         Bool _cacheUpdated(const Float incrementInBytes);
00378 
00379         void _getStateToIntentsMap(
00380                 vector<std::set<String> >& statesToIntentsMap,
00381                 std::set<String>& uniqueIntents
00382         );
00383 
00384         vector<SpwProperties> _getSpwInfo(
00385                         std::set<uInt>& avgSpw, std::set<uInt>& tdmSpw,
00386                         std::set<uInt>& fdmSpw, std::set<uInt>& wvrSpw
00387         );
00388 
00389         static uInt _sizeof(std::map<Int, std::set<uInt> >& map);
00390 
00391         static uInt _sizeof(std::map<Int, std::set<Int> >& map);
00392 
00393         static uInt _sizeof(vector<std::set<Int> >& v);
00394 
00395         void _getFieldsAndSpwMaps(
00396                 std::map<Int, std::set<uInt> >& fieldToSpwMap,
00397                 vector<std::set<Int> >& spwToFieldMap
00398         );
00399 
00400         void _getScansAndSpwMaps(
00401                 std::map<Int, std::set<uInt> >& scanToSpwMap,
00402                 vector<std::set<Int> >& spwToScanMap
00403         );
00404 
00405         void _getFieldsAndIntentsMaps(
00406                 vector<std::set<String> >& fieldToIntentsMap,
00407                 std::map<String, std::set<Int> >& intentToFieldsMap
00408         );
00409 
00410         static uInt _sizeof(std::map<Int, std::set<String> >& m);
00411 
00412         static uInt _sizeof(std::map<String, std::set<Int> >& m);
00413 
00414         static uInt _sizeof(vector<std::set<String> >& m);
00415 
00416         static uInt _sizeof(std::map<Int, std::set<Double> >& m);
00417 
00418         static uInt _sizeof(std::map<Double, std::set<Int> >& m);
00419 
00420         void _getScansAndIntentsMaps(
00421                 std::map<Int, std::set<String> >& scanToIntentsMap,
00422                 std::map<String, std::set<Int> >& intentToScansMap
00423         );
00424 
00425         void _getFieldsAndScansMaps(
00426                 std::map<Int, std::set<Int> >& fieldToScansMap,
00427                 std::map<Int, std::set<Int> >& scanToFieldsMap
00428         );
00429 
00430         void _getFieldsAndStatesMaps(
00431                 std::map<Int, std::set<Int> >& fieldToStatesMap,
00432                 std::map<Int, std::set<Int> >& stateToFieldsMap
00433         );
00434 
00435         void _getFieldsAndTimesMaps(
00436                 std::tr1::shared_ptr<std::map<Int, std::set<Double> > >& fieldToTimesMap,
00437                 std::tr1::shared_ptr<std::map<Double, std::set<Int> > >& timesToFieldMap
00438         );
00439 
00440         std::map<Int, uInt> _getDataDescIDToSpwMap();
00441 
00442         vector<String> _getFieldNames();
00443 
00444         std::tr1::shared_ptr<std::map<Int, std::set<Double> > > _getScanToTimesMap();
00445 
00446         void _getRowStats(
00447                 uInt& nACRows, uInt& nXCRows,
00448                 std::tr1::shared_ptr<AOSFMapI>& scanToNACRowsMap,
00449                 std::tr1::shared_ptr<AOSFMapI>& scanToNXCRowsMap,
00450                 std::tr1::shared_ptr<std::map<Int, uInt> >& fieldToNACRowsMap,
00451                 std::tr1::shared_ptr<std::map<Int, uInt> >& fieldToNXCRowsMap
00452         );
00453 
00454         void _getUnflaggedRowStats(
00455                 Double& nACRows, Double& nXCRows,
00456                 std::tr1::shared_ptr<AOSFMapD>& scanToNACRowsMap,
00457                 std::tr1::shared_ptr<AOSFMapD>& scanToNXCRowsMap,
00458                 std::tr1::shared_ptr<std::map<Int, Double> >& fieldToNACRowsMap,
00459                 std::tr1::shared_ptr<std::map<Int, Double> >& fieldToNXCRowsMap
00460         );
00461 
00462         void _getTimesAndInvervals(
00463                 std::map<Int, vector<Double> >& scanToTimeRangeMap,
00464                 std::map<Int, std::map<uInt, Double> >& scanSpwToIntervalMap
00465         );
00466 
00467 };
00468 }
00469 
00470 #endif /* MSMETADATA_H_ */