LCOV - code coverage report
Current view: top level - synthesis/MeasurementComponents - MSMetaInfoForCal.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 95 185 51.4 %
Date: 2024-11-06 17:42:47 Functions: 11 16 68.8 %

          Line data    Source code
       1             : //# MSMetaInfoForCal.cc: Definition of MSMetaInfoForCal
       2             : //# Copyright (C) 2016
       3             : //# Associated Universities, Inc. Washington DC, USA.
       4             : //#
       5             : //# This library is free software; you can redistribute it and/or modify it
       6             : //# under the terms of the GNU Library General Public License as published by
       7             : //# the Free Software Foundation; either version 2 of the License, or (at your
       8             : //# option) any later version.
       9             : //#
      10             : //# This library is distributed in the hope that it will be useful, but WITHOUT
      11             : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12             : //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
      13             : //# License for more details.
      14             : //#
      15             : //# You should have received a copy of the GNU Library General Public License
      16             : //# along with this library; if not, write to the Free Software Foundation,
      17             : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
      18             : //#
      19             : //# Correspondence concerning AIPS++ should be adressed as follows:
      20             : //#        Internet email: casa-feedback@nrao.edu.
      21             : //#        Postal address: AIPS++ Project Office
      22             : //#                        National Radio Astronomy Observatory
      23             : //#                        520 Edgemont Road
      24             : //#                        Charlottesville, VA 22903-2475 USA
      25             : //#
      26             : //#
      27             : 
      28             : 
      29             : #include <casacore/ms/MSOper/MSMetaData.h>
      30             : #include <casacore/ms/MeasurementSets/MeasurementSet.h>
      31             : #include <casacore/tables/Tables/TableUtil.h>
      32             : #include <synthesis/MeasurementComponents/MSMetaInfoForCal.h>
      33             : #include <msvis/MSVis/SimpleSimVi2.h>
      34             : #include <casacore/casa/aips.h>
      35             : #include <iostream>
      36             : 
      37             : using namespace casacore;
      38             : 
      39             : namespace casa { //# NAMESPACE CASA - BEGIN
      40             : 
      41             : using namespace vi;
      42             :   
      43             : // Constructor
      44         151 : MSMetaInfoForCal::MSMetaInfoForCal(String msname) : 
      45         151 :   msname_(msname), 
      46         151 :   msOk_(False),
      47         151 :   nAnt_(4),
      48         151 :   nSpw_(1),
      49         151 :   nFld_(1),
      50         151 :   centerFreqs_(0),
      51         151 :   ms_(NULL),
      52         151 :   msmd_(NULL),
      53         151 :   ssp_(NULL)
      54             : {
      55             : 
      56             :   // If the specified MS is available, make non-trivial meta-info accessors
      57         302 :   if (Table::isReadable(msname_) &&
      58         302 :       TableUtil::tableInfo(msname_).type()=="Measurement Set") {
      59             : 
      60         151 :     ms_ = new MeasurementSet(msname_);
      61             : 
      62             :     // 95% of the size of an Int column, or 50 MB (whichever smaller)
      63             :     //  This ensures that smallish time-to-Id maps will get cached
      64             :     //   (instead of one-off column info needed to calculate them)
      65             :     //  This is appropriate for the the calibration context
      66         151 :     Float msmdcache=0.95*4.0f*Float(ms_->nrow())/1e6;
      67         151 :     msmdcache=min(msmdcache,50.0);
      68         151 :     msmd_ = new MSMetaData(ms_,msmdcache);
      69             : 
      70             :     // MS seems to be available
      71         151 :     msOk_=True;
      72             :     
      73             :     // Fill from msmd
      74         151 :     nAnt_=msmd_->nAntennas();
      75         151 :     nSpw_=msmd_->nSpw(True);
      76         151 :     nFld_=msmd_->nFields();
      77             : 
      78             :   }
      79             :   // else, String output will be spoofed
      80             : 
      81         151 : }
      82             : 
      83             : // Construct from a supplied MS object
      84         463 : MSMetaInfoForCal::MSMetaInfoForCal(const MeasurementSet& ms) :
      85         463 :   msname_(ms.tableName()), 
      86         463 :   msOk_(True),      // A good MS was supplied, presumably...
      87         463 :   nAnt_(0),
      88         463 :   nSpw_(0),
      89         463 :   nFld_(0),
      90         463 :   centerFreqs_(0),
      91         463 :   ms_(NULL),        // ... but we won't have our own MS pointer
      92           0 :   msmd_(new MSMetaData(&ms,  // Form MSMetaData directly (not more than 50MB)
      93         463 :                        min(50.0,0.95f*4.0f*Float(ms.nrow())/1e6))),
      94         463 :   ssp_(NULL)
      95             : {
      96             :   // Fill counters from msmd
      97         463 :   nAnt_=msmd_->nAntennas();
      98         463 :   nSpw_=msmd_->nSpw(True);
      99         463 :   nFld_=msmd_->nFields();
     100         463 : }
     101             : 
     102         123 : MSMetaInfoForCal::MSMetaInfoForCal(const MeasurementSet& ms, String msname) :
     103         123 :   msname_(msname), 
     104         123 :   msOk_(True),      // A good MS was supplied, presumably...
     105         123 :   nAnt_(0),
     106         123 :   nSpw_(0),
     107         123 :   nFld_(0),
     108         123 :   centerFreqs_(0),
     109         123 :   ms_(NULL),        // ... but we won't have our own MS pointer
     110           0 :   msmd_(new MSMetaData(&ms,  // Form MSMetaData directly (not more than 50MB)
     111         123 :                        min(50.0,0.95f*4.0f*Float(ms.nrow())/1e6))),
     112         123 :   ssp_(NULL)
     113             : {
     114             :   // Fill counters from msmd
     115         123 :   nAnt_=msmd_->nAntennas();
     116         123 :   nSpw_=msmd_->nSpw(True);
     117         123 :   nFld_=msmd_->nFields();
     118         123 : }
     119             : 
     120           0 : MSMetaInfoForCal::MSMetaInfoForCal(uInt nAnt,uInt nSpw,uInt nFld) : 
     121           0 :   msname_("<noms>"), 
     122           0 :   msOk_(False),
     123           0 :   nAnt_(nAnt),
     124           0 :   nSpw_(nSpw),
     125           0 :   nFld_(nFld),
     126           0 :   centerFreqs_(0),
     127           0 :   ms_(NULL),
     128           0 :   msmd_(NULL),
     129           0 :   ssp_(NULL)
     130             : {
     131             :   // Nothing to do
     132           0 : }
     133             : 
     134             : 
     135             : // Construct from SimpleSimVi2Paremeters
     136             : //   (useful for testing w/ actual (spoofed) data iteration
     137           0 : MSMetaInfoForCal::MSMetaInfoForCal(const vi::SimpleSimVi2Parameters& sspar) :
     138           0 :   msname_("<noms>"), 
     139           0 :   msOk_(False),
     140           0 :   nAnt_(sspar.nAnt_),
     141           0 :   nSpw_(sspar.nSpw_),
     142           0 :   nFld_(sspar.nField_),
     143           0 :   centerFreqs_(0),
     144           0 :   ms_(NULL),
     145           0 :   msmd_(NULL),
     146           0 :   ssp_(new SimpleSimVi2Parameters(sspar))  // a copy
     147             : {
     148             :   // Nothing to do
     149           0 : }
     150             : 
     151             : 
     152             : 
     153             : // Destructor
     154         737 : MSMetaInfoForCal::~MSMetaInfoForCal()
     155             : {
     156         737 :   if (msmd_)
     157         737 :     delete msmd_;
     158         737 :   if (ms_)
     159         151 :     delete ms_;
     160         737 :   if (ssp_)
     161           0 :     delete ssp_;
     162         737 : }
     163             : 
     164             : // Return access to MSMetaData object (if avail)
     165         246 : MSMetaData& MSMetaInfoForCal::msmd() const {
     166             : 
     167         246 :   if (msOk_ && msmd_)
     168         246 :     return *msmd_;
     169             :   else
     170           0 :     throw(AipsError("MSMetaDataForCal::msmd():  No MSMetaData object available!"));
     171             : }
     172             : 
     173           0 : const SimpleSimVi2Parameters& MSMetaInfoForCal::ssp() const {
     174           0 :   if (ssp_)
     175           0 :     return *ssp_;
     176             :   else
     177           0 :     throw(AipsError("MSMetaDataForCal::ssp():  No SimpleSimVi2Parameters object available!"));
     178             : }
     179             : 
     180             : // Antenna name, by index
     181         242 : String MSMetaInfoForCal::antennaName(uInt iant) const {
     182         242 :   if (msOk_ && msmd_) {
     183         242 :     std::map<String, uInt> mymap;
     184         242 :     vector<uInt> ids(1);
     185         242 :     ids[0]=iant;
     186         242 :     uInt nAnt=msmd_->nAntennas();
     187         242 :     if (iant < nAnt)
     188         484 :       return msmd_->getAntennaNames(mymap,ids)[0];
     189             :     else {
     190           0 :       throw(AipsError(
     191           0 :                       "Specified iant="+
     192           0 :                       String::toString(iant)+
     193           0 :                       " > nAnt="+
     194           0 :                       String::toString(nAnt)));
     195             :     }
     196         242 :   }
     197             :   else {
     198           0 :     if (iant < nAnt_)
     199           0 :       return "AntennaId"+String::toString(iant);
     200             :     else {
     201           0 :       throw(AipsError(
     202           0 :                       "Specified iant="+
     203           0 :                       String::toString(iant)+
     204           0 :                       " > nAnt="+
     205           0 :                       String::toString(nAnt_)));
     206             :     }
     207             :   }
     208             : }
     209             : 
     210             : // Antenna names list, by index
     211           9 : void MSMetaInfoForCal::antennaNames(Vector<String>& antnames) const {
     212           9 :   if (msOk_ && msmd_) {
     213           9 :     std::map<String, uInt> mymap;
     214          18 :     Vector<String> asV(msmd_->getAntennaNames(mymap));
     215           9 :     antnames.reference(asV);
     216           9 :   }
     217             :   else {
     218           0 :     antnames.resize(nAnt_);
     219           0 :     for (uInt iant=0;iant<nAnt_;++iant)
     220           0 :       antnames[iant]="AntennaId"+String::toString(iant);
     221             :   }
     222           9 : }
     223             : 
     224             : // Field name, by index
     225           0 : String MSMetaInfoForCal::spwName(uInt ispw) const {
     226             : 
     227           0 :   if (msOk_ && msmd_) {
     228           0 :     uInt nSpw=msmd_->nSpw(True);
     229           0 :     if (ispw<nSpw)
     230           0 :       return msmd_->getSpwNames()[ispw];
     231             :     else {
     232           0 :       throw(AipsError(
     233           0 :                       "Specified ispw="+
     234           0 :                       String::toString(ispw)+
     235           0 :                       " > nSpw="+
     236           0 :                       String::toString(nSpw)));
     237             :     }
     238             :   }
     239             :   else
     240           0 :     if (ispw<nSpw_)
     241           0 :       return "SpwId"+String::toString(ispw);
     242             :     else {
     243           0 :       throw(AipsError(
     244           0 :                       "Specified ispw="+
     245           0 :                       String::toString(ispw)+
     246           0 :                       " > nSpw="+
     247           0 :                       String::toString(nSpw_)));
     248             :     }
     249             : 
     250             : }
     251             : 
     252             : // Field name, by index
     253          85 : String MSMetaInfoForCal::fieldName(uInt ifld) const {
     254             : 
     255          85 :   if (msOk_ && msmd_) {
     256          85 :     vector<uInt> ids(1);
     257          85 :     ids[0]=ifld;
     258          85 :     uInt nFld=msmd_->nFields();
     259          85 :     if (ifld<nFld)
     260         170 :       return msmd_->getFieldNamesForFieldIDs(ids)[0];
     261             :     else {
     262           0 :       throw(AipsError(
     263           0 :                       "Specified ifld="+
     264           0 :                       String::toString(ifld)+
     265           0 :                       " > nFld="+
     266           0 :                       String::toString(nFld)));
     267             :     }
     268          85 :   }
     269             :   else
     270           0 :     if (ifld<nFld_)
     271           0 :       return "FieldId"+String::toString(ifld);
     272             :     else {
     273           0 :       throw(AipsError(
     274           0 :                       "Specified ifld="+
     275           0 :                       String::toString(ifld)+
     276           0 :                       " > nFld="+
     277           0 :                       String::toString(nFld_)));
     278             :     }
     279             : 
     280             : }
     281             : 
     282           9 : void MSMetaInfoForCal::fieldNames(Vector<String>& fldnames) const {
     283           9 :   if (msOk_ && msmd_) {
     284           9 :     Vector<String> asV(msmd_->getFieldNames());
     285           9 :     fldnames.reference(asV);
     286           9 :   }
     287             :   else {
     288           0 :     fldnames.resize(nFld_);
     289           0 :     for (uInt ifld=0;ifld<nFld_;++ifld)
     290           0 :       fldnames[ifld]="FieldId"+String::toString(ifld);
     291             :   }
     292           9 : }
     293             : 
     294             : // Field id at time
     295       34507 : Int MSMetaInfoForCal::fieldIdAtTime(Double time) const {
     296       34507 :   if (msOk_ && msmd_) {
     297             :     // Assume 0.1s is adequate time resolution (may need to revisit?)
     298       34507 :     return *(msmd_->getFieldsForTimes(time,0.1).begin());
     299             :   }
     300             :   else
     301           0 :     return -1;  // no information, essentially
     302             : }
     303             : 
     304             : // Scan number at time
     305       34507 : Int MSMetaInfoForCal::scanNumberAtTime(Double time) const {
     306       34507 :   if (msOk_ && msmd_) {
     307             :     // Assume 0.1s is adequate time resolution (may need to revisit?)
     308       34507 :     return *(msmd_->getScansForTimes(time,0.1,-1,-1).begin());
     309             :   }
     310             :   else
     311           0 :     return -1;  // no information, essentially
     312             : 
     313             : }
     314             : 
     315           0 : Double MSMetaInfoForCal::centerFreq(uInt ispw) const {
     316             :   
     317             :   // If centerFreqs_ not yet filled, do it
     318           0 :   if (centerFreqs_.nelements()==0) {
     319           0 :     if (msOk_ && msmd_) {
     320           0 :       centerFreqs_.resize(nSpw());
     321           0 :       vector<Quantity> centerFreqQ=msmd_->getCenterFreqs();
     322           0 :       for (uInt ispw=0;ispw<nSpw();++ispw) {
     323           0 :         centerFreqs_[ispw]=centerFreqQ[ispw].getValue("GHz");
     324             :       }
     325           0 :     }
     326             :   }
     327             : 
     328             :   // Return value for requested spw, if available
     329           0 :   if ( ispw<centerFreqs_.nelements() ) {
     330           0 :     return centerFreqs_[ispw];
     331             :   }
     332             :   else {
     333           0 :     return 0.0f;
     334             : 
     335             :   }
     336             : }
     337             : 
     338             : 
     339             : } //# NAMESPACE CASA - END

Generated by: LCOV version 1.16