casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MSMetaData.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_MSMETADATA_H
00028 #define MS_MSMETADATA_H
00029 
00030 #include <casa/Quanta/Quantum.h>
00031 #include <tables/Tables/ArrayColumn.h>
00032 
00033 #include <map>
00034 #include <set>
00035 
00036 namespace casa {
00037 
00038 class MeasurementSet;
00039 class MPosition;
00040 
00041 template <class T> class Matrix;
00042 template <class T> class Vector;
00043 
00044 // <summary>
00045 // Abstract base class to hold metadata pertaining to a measurement set.
00046 // </summary>
00047 // <todo>
00048 // This initial implementation mostly parallels the implementation
00049 // in ValueMapping to minimize differences between the two. A second
00050 // implementation iteration should be done to improve performance and/or
00051 // minimize memory usage.
00052 // </todo>
00053 
00054 class MSMetaData {
00055 public:
00056 
00057         // for retrieving stats
00058         enum CorrelationType {
00059                 AUTO,
00060                 CROSS,
00061                 BOTH
00062         };
00063 
00064         virtual ~MSMetaData();
00065 
00066         // construct an object which loads metadata from the specified MS. If onDemand=False,
00067         // the MS is
00068         // interrogated at construction time, so this operation can take some time
00069         // for a large MS, the benefit being that all metadata are immediately accessible.
00070         // If onDemand=True, a pointer to the MS is maintained internally, and portions of
00071         // the MS are queried only when necessary. The benefit being that construction time
00072         // is short. However, queries may take longer than if onDemand=False and because the
00073         // MS is interrogated only when the query is run and the query results are not stored
00074         // (which allows query methods to be const). The MS object cannot go out of scope in the calling code while
00075         // the MSMetaData object exists or seg faults will likely occur.
00076         // Use onDemand=True if MS is large and/or only a small number of queries are needed.
00077         // MSMetaData(const MeasurementSet& ms, Bool onDemand);
00078 
00079         // number of unique states (number of rows from the STATE table)
00080         virtual uInt nStates() = 0;
00081 
00082         // get unique scan numbers
00083         virtual std::set<Int> getScanNumbers() = 0;
00084 
00085         virtual std::set<Int> getScansForState(const Int stateID) = 0;
00086 
00087         virtual std::set<String> getIntentsForScan(const Int scan) = 0;
00088 
00089         // get all intents, in no particular (nor guaranteed) order.
00090         virtual std::set<String> getIntents() = 0;
00091 
00092         // get a set of spectral windows for which the specified <src>intent</src>
00093         // applies.
00094         virtual std::set<uInt> getSpwsForIntent(const String& intent) = 0;
00095 
00096         // get number of spectral windows
00097         virtual uInt nSpw(Bool includewvr) = 0;
00098 
00099         // get the number of rows
00100         virtual uInt nRows()  = 0;
00101 
00102         virtual uInt nRows(CorrelationType cType) = 0;
00103 
00104         virtual uInt nRows(
00105                 CorrelationType cType, Int arrayID, Int observationID,
00106                 Int scanNumber, Int fieldID
00107         ) = 0;
00108 
00109         virtual uInt nRows(CorrelationType cType, Int fieldID) = 0;
00110 
00111         // get a set of intents corresponding to the specified spectral window
00112         virtual std::set<String> getIntentsForSpw(const uInt spw) = 0;
00113 
00114         // get a set of intents corresponding to a specified field
00115         virtual std::set<String> getIntentsForField(Int fieldID) = 0;
00116 
00117         // get the number of fields.
00118         virtual uInt nFields() = 0;
00119 
00120         // get a set of spectral windows corresponding to the specified fieldID
00121         virtual std::set<uInt> getSpwsForField(const Int fieldID) = 0;
00122 
00123         // get a set of spectral windows corresponding to the specified field name
00124         virtual std::set<uInt> getSpwsForField(const String& fieldName) = 0;
00125 
00126         // get the set of field IDs corresponding to the specified spectral window.
00127         virtual std::set<Int> getFieldIDsForSpw(const uInt spw) = 0;
00128 
00129         // get the set of field names corresponding to the specified spectral window.
00130         virtual std::set<String> getFieldNamesForSpw(const uInt spw) = 0;
00131 
00132         // get the number of scans in the dataset
00133         virtual uInt nScans() = 0;
00134 
00135         // get the number of observations (from the OBSERVATIONS table) in the dataset
00136         virtual uInt nObservations() = 0;
00137 
00138         // get the number of arrays (from the ARRAY table) in the dataset
00139         virtual uInt nArrays() = 0;
00140 
00141         // get the set of spectral windows for the specified scan number.
00142         virtual std::set<uInt> getSpwsForScan(const Int scan) = 0;
00143 
00144         // get the set of scan numbers for the specified spectral window.
00145         virtual std::set<Int> getScansForSpw(const uInt spw) = 0;
00146 
00147         // get the number of antennas in the ANTENNA table
00148         virtual uInt nAntennas() = 0;
00149 
00150         // get the name of the antenna for the specified antenna ID
00151         virtual vector<String> getAntennaNames(
00152                 std::map<String, uInt>& namesToIDsMap,
00153                 const vector<uInt>& antennaIDs=vector<uInt>(0)
00154         ) = 0;
00155 
00156         // get the antenna ID for the antenna with the specified name.
00157         virtual vector<uInt> getAntennaIDs(const vector<String>& antennaName) = 0;
00158 
00159         // get set of spectral windows used for TDM. These are windows that have
00160         // 64, 128, or 256 channels
00161         virtual std::set<uInt> getTDMSpw() = 0;
00162 
00163         // get set of spectral windows used for FDM. These are windows that do not
00164         // have 1, 4, 64, 128, or 256 channels.
00165         virtual std::set<uInt> getFDMSpw() = 0;
00166 
00167         // get spectral windows that have been averaged. These are windows with 1 channel.
00168         virtual std::set<uInt> getChannelAvgSpw() = 0;
00169 
00170         // Get the spectral window set used for WVR measurements. These have 4 channels each.
00171         virtual std::set<uInt> getWVRSpw() = 0;
00172 
00173         // Get the scans which fail into the specified time range (center-tol to center+tol)
00174         virtual std::set<Int> getScansForTimes(const Double center, const Double tol) = 0;
00175 
00176         // Get the times for the specified scans
00177         virtual std::set<Double> getTimesForScans(const std::set<Int>& scans) = 0;
00178 
00179         // get the times for the specified scan. No need to be implemented or overridden in subclasses.
00180         // The return values come from the TIME column.
00181         std::set<Double> getTimesForScan(const Int scan);
00182 
00183         // get the time range for the specified scan. The vector returned will contain two elements,
00184         // the start and stop time of the scan, determined from min(TIME_CENTROID(x)-0.5*INTERVAL(x)) and
00185         // max(TIME_CENTROID(x)-0.5*INTERVAL(x))
00186         virtual std::vector<Double> getTimeRangeForScan(Int scan) = 0;
00187 
00188         // get the stateIDs associated with the specified scan number.
00189         virtual std::set<Int> getStatesForScan(const Int scan) = 0;
00190 
00191         // get the scans associated with the specified intent
00192         virtual std::set<Int> getScansForIntent(const String& intent) = 0;
00193 
00194         // get the scan numbers associated with the specified field ID.
00195         virtual std::set<Int> getScansForFieldID(const Int fieldID) = 0;
00196 
00197         // get the field IDs for the specified field name. Case insensitive.
00198         virtual std::set<Int> getFieldIDsForField(const String& field) = 0;
00199 
00200         // get the scan numbers associated with the specified field. Subclasses should not implement or override.
00201         std::set<Int> getScansForField(const String& field);
00202 
00203         // get field IDs associated with the specified scan number.
00204         virtual std::set<Int> getFieldsForScan(const Int scan) = 0;
00205 
00206         // get the field IDs associated with the specified scans
00207         virtual std::set<Int> getFieldsForScans(const std::set<Int>& scans) = 0;
00208 
00209         // get the field IDs associated with the specified intent.
00210         virtual std::set<Int> getFieldsForIntent(const String& intent) = 0;
00211 
00212         // get the field names associated with the specified field IDs. If <src>fieldIDs</src>
00213         // is empty, a list of all field names will be returned.
00214         virtual vector<String> getFieldNamesForFieldIDs(const vector<Int>& fieldIDs) = 0;
00215 
00216         // Get the fields which fail into the specified time range (center-tol to center+tol)
00217         virtual std::set<Int> getFieldsForTimes(Double center, Double tol) = 0;
00218 
00219         // get the times for which the specified field was observed
00220         virtual std::set<Double> getTimesForField(Int fieldID) = 0;
00221 
00222         // get telescope names in the order they are listed in the OBSERVATION table. These are
00223         // the telescopes (observatories), not the antenna names.
00224         virtual vector<String> getObservatoryNames() = 0;
00225 
00226         // get the position of the specified telescope (observatory).
00227         virtual MPosition getObservatoryPosition(uInt which) = 0;
00228 
00229         // get the position of the specified antennas. If <src>which</src> is empty,
00230         // all antenna positions will be returned.
00231         virtual vector<MPosition> getAntennaPositions(const vector<uInt>& which=vector<uInt>(0)) = 0;
00232 
00233         // <src>names</src> cannot be empty.
00234         virtual vector<MPosition> getAntennaPositions(const vector<String>& names) = 0;
00235 
00236         // get the position of the specified antenna relative to the observatory position.
00237         // the three vector returned represents the longitudinal, latitudinal, and elevation
00238         // offsets (elements 0, 1, and 2 respectively). The longitude and latitude offsets are
00239         // measured along the surface of a sphere centered at the earth's center and whose surface
00240         // intersects the position of the observatory.
00241         virtual Quantum<Vector<Double> > getAntennaOffset(uInt which) = 0;
00242 
00243         virtual Quantum<Vector<Double> > getAntennaOffset(const String& name) = 0;
00244 
00245         virtual vector<Quantum<Vector<Double> > > getAntennaOffsets(
00246                 const vector<MPosition>& positions
00247         ) = 0;
00248 
00249         // get a map relating time stamps to exposure times. An exception is thrown if there
00250         // are different exposure lengths for records with the same time stamp
00251         //virtual std::map<Double, Double> getExposuresForTimes() const = 0;
00252 
00253         // get the unique baselines in the MS. These are not necessarily every combination of the
00254         // n(n-1)/2 possible antenna pairs, but rather the number of unique baselines represented in
00255         // the main MS table, which in theory can be less than n(n-1)/2 (for example if samples for
00256         // certain antenna pairs are not recorded. The returned Matrix is nAnts x nAnts in size. Pairs
00257         // that are true represent baselines represented in the main MS table.
00258         virtual Matrix<Bool> getUniqueBaselines() = 0;
00259 
00260         // get the number of unique baselines represented in the main MS table which in theory can be
00261         // less than n*(n-1)/2
00262         virtual uInt nBaselines();
00263 
00264         // get the effective total exposure time. This is the effective time spent collecting unflagged data.
00265         virtual Quantity getEffectiveTotalExposureTime() = 0;
00266 
00267         // get the number of unflagged rows
00268         virtual Double nUnflaggedRows() = 0;
00269 
00270         virtual Double nUnflaggedRows(CorrelationType cType) = 0;
00271 
00272         virtual Double nUnflaggedRows(
00273                 CorrelationType cType, Int arrayID, Int observationID,
00274                 Int scanNumber, Int fieldID
00275         ) = 0;
00276 
00277         virtual Double nUnflaggedRows(CorrelationType cType, Int fieldID) = 0;
00278 
00279         inline virtual Float getCache() const { return 0;}
00280 
00281         static Bool hasBBCNo(const MeasurementSet& ms);
00282 
00283         virtual vector<Double> getBandWidths() = 0;
00284 
00285         virtual vector<Quantum<Vector<Double> > > getChanFreqs() = 0;
00286 
00287         virtual vector<vector<Double> > getChanWidths() = 0;
00288 
00289         virtual vector<Int> getNetSidebands() = 0;
00290 
00291         virtual vector<Quantity> getMeanFreqs() = 0;
00292 
00293         virtual vector<uInt> nChans() = 0;
00294 
00295         virtual vector<vector<Double> > getEdgeChans() = 0;
00296 
00297         virtual vector<uInt> getBBCNos() = 0;
00298 
00299         virtual vector<String> getSpwNames() = 0;
00300 
00301         // the returned map are the average intervals for each spectral window for the
00302         // specified scan
00303         virtual std::map<uInt, Double> getAverageIntervalsForScan(Int scan) = 0;
00304 
00305 protected:
00306 
00307         // (array_id, observation_id, scan_number, field_id) -> stuff mappings
00308         typedef std::map<Int, std::map<Int, std::map<Int, std::map<Int, uInt> > > > AOSFMapI;
00309         typedef std::map<Int, std::map<Int, std::map<Int, std::map<Int, Double> > > > AOSFMapD;
00310 
00311         struct SpwProperties {
00312                 Double bandwidth;
00313                 Quantum<Vector<Double> > chanfreqs;
00314                 vector<Double> chanwidths;
00315                 Int netsideband;
00316                 Quantity meanfreq;
00317                 uInt nchans;
00318                 vector<Double> edgechans;
00319                 uInt bbcno;
00320                 String name;
00321         };
00322 
00323         static uInt _getNStates(const MeasurementSet& ms);
00324 
00325         static void _getStateToIntentsMap(
00326                 vector<std::set<String> >& statesToIntentsMap,
00327                 std::set<String>& uniqueIntents,
00328                 const MeasurementSet& ms
00329         );
00330 
00331         static Vector<Int> _getScans(const MeasurementSet& ms);
00332 
00333         // state IDs can be < 0
00334         static std::map<Int, std::set<Int> > _getScanToStatesMap(
00335                 const Vector<Int>& scans, const Vector<Int>& states
00336         );
00337 
00338         static vector<SpwProperties>  _getSpwInfo(
00339                 std::set<uInt>& avgSpw, std::set<uInt>& tdmSpw, std::set<uInt>& fdmSpw,
00340                 std::set<uInt>& wvrSpw, const MeasurementSet& ms
00341         );
00342 
00343         static Vector<Int> _getDataDescIDs(const MeasurementSet& ms);
00344 
00345         static Vector<Int> _getStates(const MeasurementSet& ms);
00346 
00347         static Vector<Int> _getObservationIDs(const MeasurementSet& ms);
00348 
00349         static Vector<Int> _getArrayIDs(const MeasurementSet& ms);
00350 
00351         static std::map<Int, uInt> _getDataDescIDToSpwMap(const MeasurementSet& ms);
00352 
00353         static Vector<Int> _getFieldIDs(const MeasurementSet& ms);
00354 
00355         static vector<String> _getFieldNames(const MeasurementSet& ms);
00356 
00357         static void _checkTolerance(const Double tol);
00358 
00359         static Vector<Double> _getTimes(const MeasurementSet& ms);
00360 
00361         static Vector<Double> _getTimeCentroids(const MeasurementSet& ms);
00362 
00363         static Vector<Double> _getIntervals(const MeasurementSet& ms);
00364 
00365         static Vector<Bool> _getFlagRows(const MeasurementSet& ms);
00366 
00367         static ArrayColumn<Bool>* _getFlags(const MeasurementSet& ms);
00368 
00369         static std::map<Int, std::set<Double> > _getScanToTimesMap(
00370                 const Vector<Int>& scans, const Vector<Double>& times
00371         );
00372 
00373         static vector<MPosition> _getObservatoryPositions(
00374                 vector<String>& names, const MeasurementSet& ms
00375         );
00376 
00377         static vector<String> _getAntennaNames(
00378                 std::map<String, uInt>& namesToIDs, const MeasurementSet& ms
00379         );
00380 
00381         // get all antenna positions. A vector of antenna names is also returned.
00382         static vector<MPosition> _getAntennaPositions(
00383                 vector<String>& antennaNames, const MeasurementSet& ms
00384         );
00385 
00386         static vector<MPosition> _getAntennaPositions(
00387                 const MeasurementSet& ms
00388         );
00389 
00390         static vector<Quantum<Vector<Double> > > _getAntennaOffsets(
00391                 const vector<MPosition>& antennaPositions,
00392                 const MPosition& observatoryPosition
00393         );
00394 
00395         static std::map<Int, uInt> _toUIntMap(const Vector<Int>& v);
00396 
00397 
00398         static std::map<Double, Double> _getTimeToAggregateExposureMap(
00399                 const vector<Double>& times, const vector<Double>& exposures
00400         );
00401 
00402         Matrix<Bool> _getUniqueBaselines(
00403                 const Vector<Int>& antanna1, const Vector<Int>& antenna2
00404         );
00405 
00406         Quantity _getTotalExposureTime(
00407                 const MeasurementSet& ms, const std::map<Double, Double>& timeToBWMap,
00408                 const vector<SpwProperties>& spwProperties,
00409                 const std::map<Int, uInt>& dataDescToSpwIdMap
00410         );
00411 
00412         static std::map<Double, Double> _getTimeToTotalBWMap(
00413                 const Vector<Double>& times, const Vector<Int>& ddIDs,
00414                 const std::map<Int, uInt>& dataDescIDToSpwMap,
00415                 const vector<MSMetaData::SpwProperties>& spwInfo
00416         );
00417 
00418         static void _getAntennas(
00419                 Vector<Int>& ant1, Vector<Int>& ant2, const MeasurementSet& ms
00420         );
00421 
00422         void _getRowStats(
00423                 uInt& nACRows, uInt& nXCRows,
00424                 AOSFMapI*& scanToNACRowsMap,
00425                 AOSFMapI*& scanToNXCRowsMap,
00426                 std::map<Int, uInt>*& fieldToNACRowsMap,
00427                 std::map<Int, uInt>*& fieldToNXCRowsMap,
00428                 const Vector<Int>& ant1, const Vector<Int>& ant2,
00429                 const Vector<Int>& scans, const Vector<Int>& fieldIDs,
00430                 const Vector<Int>& obsIDs, const Vector<Int>& arIDs
00431         );
00432 
00433         void _getUnflaggedRowStats(
00434                 Double& nACRows, Double& nXCRows,
00435                 std::map<Int, Double>*& fieldNACRows, std::map<Int, Double>*& fieldNXCRows,
00436                 AOSFMapD*& scanNACRows,
00437                 AOSFMapD*& scanNXCRows,
00438                 const Vector<Int>& ant1, const Vector<Int>& ant2,
00439                 const Vector<Bool>& flagRow, const Vector<Int>& dataDescIDs,
00440                 const std::map<Int, uInt>& dataDescIDToSpwMap,
00441                 const vector<SpwProperties>& spwInfo,
00442                 const ArrayColumn<Bool>& flags,
00443                 const Vector<Int>& fieldIDs, const Vector<Int>& scan,
00444                 const Vector<Int>& obsIDs, const Vector<Int>& arIDs
00445         );
00446 
00447         static std::map<Int, vector<Double> > _getScanToTimeRangeMap(
00448                 std::map<Int, std::map<uInt, Double> >& scanSpwToAverageIntervalMap,
00449                 const Vector<Int>& scans, const Vector<Double>& timeCentroids,
00450                 const Vector<Double>& intervals, const Vector<Int>& dataDescIDs,
00451                 const std::map<Int, uInt>& dataDesIDToSpwMap,
00452                 const std::set<Int>& uniqueScans
00453         );
00454 
00455 
00456 private:
00457 
00458         // This comment from thunter in the original ValueMapping python class
00459         // # Determine the number of polarizations for the first OBSERVE_TARGET intent.
00460     // # Used by plotbandpass for BPOLY plots since the number of pols cannot be inferred
00461     // # correctly from the caltable alone.  You cannot not simply use the first row, because
00462     // # it may be a pointing scan which may have different number of polarizations than what
00463     // # the TARGET and BANDPASS calibrator will have.
00464     // # -- T. Hunter
00465         void _setNumberOfPolarizations(const MeasurementSet& ms);
00466 
00467         // set metadata from OBSERVATION table
00468         void _setObservation(const MeasurementSet& ms);
00469 
00470 };
00471 }
00472 
00473 #endif /* MSMETADATA_H_ */