LCOV - code coverage report
Current view: top level - msvis/MSVis - SubMS.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 0 5018 0.0 %
Date: 2024-10-29 13:38:20 Functions: 0 88 0.0 %

          Line data    Source code
       1             : //# SubMS.cc 
       2             : //# Copyright (C) 1996-2007
       3             : //# Associated Universities, Inc. Washington DC, USA.
       4             : //#
       5             : //# This library is free software; you can redistribute it and/or modify
       6             : //# it under the terms of the GNU General Public License as published by
       7             : //# the Free Software Foundation; either version 2 of the License, or
       8             : //# (at your option) any later version.
       9             : //#
      10             : //# This library is distributed in the hope that it will be useful,
      11             : //# but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : //# GNU General Public License for more details.
      14             : //# 
      15             : //# You should have received a copy of the GNU General Public License
      16             : //# along with this library; if not, write to the Free Software
      17             : //# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
      18             : //#
      19             : //# Correspondence concerning AIPS++ should be addressed 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             : //# $Id: $
      27             : 
      28             : // To make Timer reports in the inner loop of the simple copy,
      29             : // uncomment the following line:
      30             : //#define COPYTIMER
      31             : 
      32             : #include <msvis/MSVis/SubMS.h>
      33             : #include <asdmstman/AsdmStMan.h>
      34             : #include <casacore/ms/MSSel/MSSelection.h>
      35             : //#include <ms/MSSel/MSTimeGram.h>
      36             : //#include <tables/TaQL/ExprNode.h>
      37             : #include <casacore/tables/Tables/RefRows.h>
      38             : #include <casacore/ms/MeasurementSets/MSColumns.h>
      39             : #include <casacore/coordinates/Coordinates/CoordinateUtil.h>
      40             : #include <casacore/casa/Arrays/Matrix.h>
      41             : #include <casacore/casa/Arrays/Cube.h>
      42             : #include <casacore/casa/Arrays/ArrayMath.h>
      43             : #include <casacore/casa/Arrays/ArrayOpsDiffShapes.h>
      44             : #include <casacore/casa/Arrays/ArrayLogical.h>
      45             : #include <casacore/casa/Arrays/ArrayUtil.h>
      46             : #include <casacore/casa/Arrays/IPosition.h>
      47             : #include <casacore/casa/Arrays/Slice.h>
      48             : #include <casacore/casa/BasicSL/Complex.h>
      49             : #include <casacore/casa/Logging/LogIO.h>
      50             : #include <casacore/casa/OS/File.h>
      51             : #include <casacore/casa/OS/Directory.h>
      52             : #include <casacore/casa/OS/HostInfo.h>
      53             : #include <casacore/casa/OS/Memory.h>              // Can be commented out along with
      54             : //                                         // Memory:: calls.
      55             : 
      56             : //#ifdef COPYTIMER
      57             : #include <casacore/casa/OS/Timer.h>
      58             : //#endif
      59             : 
      60             : #include <casacore/casa/Containers/Record.h>
      61             : #include <casacore/casa/BasicSL/String.h>
      62             : #include <casacore/casa/Utilities/Assert.h>
      63             : #include <casacore/casa/Utilities/GenSort.h>
      64             : #include <casacore/casa/System/AppInfo.h>
      65             : #include <casacore/casa/System/ProgressMeter.h>
      66             : #include <casacore/casa/Quanta/QuantumHolder.h>
      67             : #include <msvis/MSVis/GroupProcessor.h>
      68             : //#include <msvis/MSVis/VisSet.h>
      69             : #include <msvis/MSVis/VisBuffer.h>
      70             : #include <msvis/MSVis/VisBufferComponents.h>
      71             : #include <msvis/MSVis/VBGContinuumSubtractor.h>
      72             : #include <msvis/MSVis/VBRemapper.h>
      73             : #include <msvis/MSVis/VisChunkAverager.h>
      74             : #include <msvis/MSVis/VisIterator.h>
      75             : //#include <msvis/MSVis/VisibilityIterator.h>
      76             : #include <casacore/tables/DataMan/IncrementalStMan.h>
      77             : #include <casacore/tables/Tables/ScalarColumn.h>
      78             : #include <casacore/tables/Tables/ScaColDesc.h>
      79             : #include <casacore/tables/Tables/SetupNewTab.h>
      80             : #include <casacore/tables/DataMan/StandardStMan.h>
      81             : #include <casacore/tables/Tables/Table.h>
      82             : #include <casacore/tables/Tables/PlainTable.h>
      83             : #include <casacore/tables/Tables/TableDesc.h>
      84             : #include <casacore/tables/Tables/TableInfo.h>
      85             : #include <casacore/tables/Tables/TableLock.h>
      86             : #include <casacore/tables/Tables/TableRecord.h>
      87             : #include <casacore/tables/Tables/TableCopy.h>
      88             : #include <casacore/tables/Tables/TableRow.h>
      89             : #include <casacore/tables/DataMan/TiledColumnStMan.h>
      90             : #include <casacore/tables/DataMan/TiledShapeStMan.h>
      91             : #include <casacore/tables/DataMan/TiledDataStMan.h>
      92             : #include <casacore/tables/DataMan/TiledStManAccessor.h>
      93             : #include <casacore/ms/MeasurementSets/MSTileLayout.h>
      94             : #include <casacore/scimath/Mathematics/InterpolateArray1D.h>
      95             : #include <casacore/scimath/Mathematics/FFTServer.h>
      96             : #include <sstream>
      97             : #include <iomanip>
      98             : #include <functional>
      99             : #include <map>
     100             : #include <set>
     101             : #include <casacore/measures/Measures/MeasTable.h>
     102             : #include <casacore/scimath/Mathematics/Smooth.h>
     103             : #include <casacore/casa/Quanta/MVTime.h>
     104             : 
     105             : 
     106             : using namespace casacore;
     107             : 
     108             : 
     109             : namespace casa {
     110             : 
     111             : typedef ROVisibilityIterator ROVisIter;
     112             : typedef VisibilityIterator VisIter;
     113             : 
     114             : namespace subms {
     115             : 
     116             : 
     117             : // Weight <-> Sigma routines
     118             : //   A value in zero in either will be interpretted as 
     119             : //    practically zero weight (~inf noise), w/out generating
     120             : //    inf or NaN
     121           0 : Double wtToSigma(Double wt)
     122             : {
     123             :   // sig = 1/sqrt(wt)
     124           0 :   return wt > 0.0 ? 1.0 / sqrt(wt) : FLT_MAX;
     125             : }
     126           0 : Double sigToWeight(Double sig)
     127             : {
     128             :   // wt = 1/square(sig)
     129           0 :   return sig > 0.0 ? 1.0 / square(sig) : FLT_EPSILON;
     130             : }
     131             : 
     132             : } // end namespace subms
     133             : 
     134             : //#warning "Debugging kluge here"
     135             : //inline Complex operator*(const Complex& val, Double f) { return val*Float(f); }
     136             : //
     137             : 
     138           0 :   SubMS::SubMS(String& theMS, Table::TableOption option) :
     139           0 :     ms_p(MeasurementSet(theMS, option)),
     140           0 :     mssel_p(ms_p),
     141           0 :     msc_p(NULL),
     142           0 :     mscIn_p(NULL),
     143           0 :     keepShape_p(true),
     144             :     //    sameShape_p(true),
     145           0 :     antennaSel_p(false),
     146           0 :     timeBin_p(-1.0),
     147           0 :     scanString_p(""),
     148           0 :     intentString_p(""),
     149           0 :     obsString_p(""),
     150           0 :     uvrangeString_p(""),
     151           0 :     taqlString_p(""),
     152           0 :     timeRange_p(""),
     153           0 :     arrayExpr_p(""),
     154           0 :     combine_p(""),
     155           0 :     fitorder_p(-1),
     156           0 :     fitspw_p("*"),
     157           0 :     fitoutspw_p("*"),
     158           0 :     fillMainTable_p(True),
     159           0 :     tvi_debug(False),
     160           0 :     want_cont_p(False)
     161             :   {
     162           0 :   }
     163             :   
     164           0 :   SubMS::SubMS(MeasurementSet& ms) :
     165           0 :     ms_p(ms),
     166           0 :     mssel_p(ms_p),
     167           0 :     msc_p(NULL),
     168           0 :     mscIn_p(NULL),
     169           0 :     keepShape_p(true),
     170             :     //sameShape_p(true),
     171           0 :     antennaSel_p(false),
     172           0 :     timeBin_p(-1.0),
     173           0 :     scanString_p(""),
     174           0 :     intentString_p(""),
     175           0 :     obsString_p(""),
     176           0 :     uvrangeString_p(""),
     177           0 :     taqlString_p(""),
     178           0 :     timeRange_p(""),
     179           0 :     arrayExpr_p(""),
     180           0 :     combine_p(""),
     181           0 :     fitorder_p(-1),
     182           0 :     fitspw_p("*"),
     183           0 :     fitoutspw_p("*"),
     184           0 :     fillMainTable_p(true),
     185           0 :     tvi_debug(False),
     186           0 :     want_cont_p(False)
     187             :   {
     188           0 :   }
     189             :   
     190           0 :   SubMS::~SubMS()
     191             :   {
     192           0 :     delete msc_p;
     193           0 :     msc_p = nullptr;
     194             :     
     195           0 :     delete mscIn_p;
     196           0 :     mscIn_p = nullptr;
     197             : 
     198           0 :     msOut_p=MeasurementSet();
     199             : 
     200             :     // parseColumnNames unavoidably has a static String and Vector<MS::PredefinedColumns>.
     201             :     // Collapse them down to free most of that memory.
     202           0 :     parseColumnNames("None");
     203             : 
     204           0 :   }
     205             : 
     206           0 : std::set<Int> SubMS::findBadSpws(MeasurementSet& ms, Vector<Int> spwv)
     207             : {
     208           0 :   ScalarColumn<Int> spws_in_dd(ms.dataDescription(), 
     209           0 :                                  MSDataDescription::columnName(MSDataDescription::SPECTRAL_WINDOW_ID));
     210           0 :   std::set<Int> uniqSpwsInDD;
     211           0 :   uInt nspwsInDD = spws_in_dd.nrow();
     212           0 :   for(uInt ddrow = 0; ddrow < nspwsInDD; ++ddrow)
     213           0 :     uniqSpwsInDD.insert(spws_in_dd(ddrow));
     214           0 :   std::set<Int>::iterator ddend = uniqSpwsInDD.end();
     215           0 :   std::set<Int> badSelSpwSlots;
     216           0 :   for(uInt k = 0; k < spwv.nelements(); ++k){
     217           0 :     if(uniqSpwsInDD.find(spwv[k]) == ddend){
     218           0 :       badSelSpwSlots.insert(k);
     219             :     }
     220             :   }
     221           0 :   return badSelSpwSlots;
     222           0 : }
     223             :   
     224             :   // This is the version used by split.
     225           0 :   Bool SubMS::selectSpw(const String& spwstr, const Vector<Int>& steps)
     226             :   {
     227           0 :     LogIO os(LogOrigin("SubMS", "selectSpw()"));
     228             : 
     229           0 :     MSSelection mssel;
     230           0 :     String myspwstr(spwstr == "" ? "*" : spwstr);
     231             : 
     232           0 :     mssel.setSpwExpr(myspwstr);
     233             : 
     234           0 :     widths_p = steps.copy();
     235           0 :     if(widths_p.nelements() < 1){
     236           0 :       widths_p.resize(1);
     237           0 :       widths_p[0] = 1;
     238             :     }
     239             :     else{
     240           0 :       for(uInt k = 0; k < widths_p.nelements(); ++k){
     241           0 :         if(widths_p[k] == 0){
     242             :           os << LogIO::WARN
     243             :              << "0 cannot be used for channel width...using 1 instead."
     244           0 :              << LogIO::POST;
     245           0 :           widths_p[k] = 1;
     246             :         }
     247             :       }
     248             :     }
     249             : 
     250             :     // Each row should have spw, start, stop, step
     251             :     // A single width is a default, but multiple widths should be used
     252             :     // literally.
     253           0 :     Matrix<Int> chansel = mssel.getChanList(&ms_p, 1);
     254             : 
     255           0 :     if(chansel.nrow() > 0) {         // Use myspwstr if it selected anything...
     256           0 :       spw_p       = chansel.column(0);
     257           0 :       chanStart_p = chansel.column(1);
     258           0 :       chanEnd_p   = chansel.column(2);
     259           0 :       chanStep_p  = chansel.column(3);
     260             : 
     261           0 :       uInt nspw = chanEnd_p.nelements();
     262           0 :       nchan_p.resize(nspw);
     263             : 
     264             :       // A single width is a default, but multiple widths should be used
     265             :       // literally.
     266           0 :       if(widths_p.nelements() > 1 && widths_p.nelements() != spw_p.nelements()){
     267             :         os << LogIO::SEVERE
     268             :            << "Mismatch between the # of widths specified by width and the # of spws."
     269           0 :            << LogIO::POST;
     270           0 :         return false;
     271             :       }
     272             : 
     273             :       // Copy the default width to all spws.
     274           0 :       if(widths_p.nelements() < nspw){
     275           0 :         widths_p.resize(nspw, true);
     276           0 :         for(uInt k = 1; k < nspw; ++k)
     277           0 :           widths_p[k] = widths_p[0];
     278             :       }
     279             : 
     280           0 :       for(uInt k = 0; k < nspw; ++k){
     281           0 :         if(chanStep_p[k] == 0)  // CAS-2224, triggered by spw='0:2'
     282           0 :           chanStep_p[k] = 1;    // (as opposed to '0:2~2').
     283             :         
     284             :         // if((nchan_p[k] - chanStart_p[k] + 1) % (chanStep_p[k] * widths_p[k]) != 0)
     285             :         //   os << LogIO::WARN
     286             :         //      << "The number of selected channels, " << nchan_p[k]
     287             :         //      << ", for spw " << spw_p[k] << " is not a multiple of the increment, "
     288             :         //      << chanStep_p[k] * widths_p[k] << ".\n"
     289             :         //      << "The reported width and frequency of the final channel may be"
     290             :         //      << "\noff by a fraction of a channel width.\n"
     291             :         //      << "(This is being worked on.)"
     292             :         //      << LogIO::POST;
     293             : 
     294           0 :         nchan_p[k] = 1 + (chanEnd_p[k] -
     295           0 :                           chanStart_p[k]) / (chanStep_p[k] * widths_p[k]);
     296           0 :         if(nchan_p[k] < 1)
     297           0 :           nchan_p[k] = 1;
     298             :       }
     299             :     }
     300             :     else{                            // select everything and rely on widths.
     301           0 :       MSSpWindowColumns mySpwTab(ms_p.spectralWindow());
     302           0 :       uInt nspw = mySpwTab.nrow();
     303             : 
     304           0 :       nchan_p = mySpwTab.numChan().getColumn();
     305             :       
     306           0 :       spw_p.resize(nspw);
     307           0 :       indgen(spw_p);
     308             :       
     309           0 :       chanStart_p.resize(nspw);
     310           0 :       chanStep_p.resize(nspw);
     311           0 :       for(uInt k = 0; k < nspw; ++k){
     312           0 :         chanStart_p[k] = 0;
     313           0 :         chanEnd_p[k]   = nchan_p[k] - 1;
     314           0 :         chanStep_p[k]  = 1;
     315             :       }
     316             : 
     317           0 :       if(widths_p.nelements() != spw_p.nelements()){
     318           0 :         if(widths_p.nelements() == 1){
     319           0 :           widths_p.resize(spw_p.nelements(), true);
     320           0 :           for(uInt k = 1; k < spw_p.nelements(); ++k)
     321           0 :             widths_p[k] = widths_p[0];
     322             :         }
     323             :         else{
     324             :           os << LogIO::SEVERE
     325             :              << "Mismatch between the # of widths specified by width and the # of spws."
     326           0 :              << LogIO::POST;
     327           0 :           return false;
     328             :         }
     329             :       }
     330             : 
     331           0 :       for(uInt k = 0; k < nspw; ++k){
     332             :         // if((nchan_p[k] + 1) % (chanStep_p[k] * widths_p[k]) != 0)
     333             :         //   os << LogIO::WARN
     334             :         //      << "The number of selected channels, " << nchan_p[k]
     335             :         //      << ", for spw " << spw_p[k] << " is not a multiple of the increment, "
     336             :         //      << chanStep_p[k] * widths_p[k] << ".\n"
     337             :         //      << "The reported width and frequency of the final channel may be"
     338             :         //      << "\noff by a fraction of a channel width.\n"
     339             :         //      << "(This is being worked on.)"
     340             :         //      << LogIO::POST;
     341             : 
     342           0 :         nchan_p[k] = 1 + (nchan_p[k] - 1) / widths_p[k];
     343             :       }
     344           0 :     }
     345             :     
     346             :     // Check for and filter out selected spws that aren't included in
     347             :     // DATA_DESCRIPTION.  (See CAS-1673 for an example.)
     348           0 :     std::set<Int> badSelSpwSlots(SubMS::findBadSpws(ms_p, spw_p));
     349           0 :     uInt nbadSelSpwSlots = badSelSpwSlots.size();
     350           0 :     if(nbadSelSpwSlots > 0){
     351           0 :       os << LogIO::WARN << "Selected input spw(s)\n";
     352           0 :       for(std::set<Int>::iterator bbit = badSelSpwSlots.begin();
     353           0 :           bbit != badSelSpwSlots.end(); ++bbit)
     354           0 :         os << spw_p[*bbit] << " ";
     355             :       os << "\nwere not found in DATA_DESCRIPTION and are being excluded."
     356           0 :          << LogIO::POST;
     357             : 
     358           0 :       uInt nSelSpw = spw_p.nelements();
     359           0 :       uInt ngoodSelSpwSlots = nSelSpw - nbadSelSpwSlots;
     360           0 :       Vector<Int> spwc(ngoodSelSpwSlots);
     361           0 :       Vector<Int> chanStartc(ngoodSelSpwSlots);
     362           0 :       Vector<Int> chanEndc(ngoodSelSpwSlots);
     363           0 :       Vector<Int> nchanc(ngoodSelSpwSlots);
     364           0 :       Vector<Int> chanStepc(ngoodSelSpwSlots);
     365           0 :       std::set<Int>::iterator bsend = badSelSpwSlots.end();
     366             :       
     367           0 :       uInt j = 0;
     368           0 :       for(uInt k = 0; k < nSelSpw; ++k){
     369           0 :         if(badSelSpwSlots.find(k) == bsend){
     370           0 :           spwc[j]       = spw_p[k];
     371           0 :           chanStartc[j] = chanStart_p[k];
     372           0 :           chanEndc[j]   = chanEnd_p[k];
     373           0 :           nchanc[j]     = nchan_p[k];
     374           0 :           chanStepc[j]  = chanStep_p[k];
     375           0 :           ++j;
     376             :         }
     377             :       }
     378           0 :       spw_p.resize(ngoodSelSpwSlots);
     379           0 :       spw_p = spwc;
     380           0 :       chanStart_p.resize(ngoodSelSpwSlots);
     381           0 :       chanStart_p = chanStartc;
     382           0 :       chanEnd_p.resize(ngoodSelSpwSlots);
     383           0 :       chanEnd_p = chanEndc;
     384           0 :       nchan_p.resize(ngoodSelSpwSlots);
     385           0 :       nchan_p = nchanc;
     386           0 :       chanStep_p.resize(ngoodSelSpwSlots);
     387           0 :       chanStep_p = chanStepc;
     388           0 :     }
     389             :     
     390           0 :     mssel.getChanSlices(chanSlices_p, &ms_p, 1);
     391           0 :     return true;
     392           0 :   }
     393             :   
     394             :   // This older version is used elsewhere.
     395           0 :   void SubMS::selectSpw(Vector<Int> spw, Vector<Int> nchan, Vector<Int> start, 
     396             :                         Vector<Int> step)
     397             :   {
     398           0 :     spw_p.resize();
     399           0 :     spw_p = spw;
     400             :     
     401             :     //check for default
     402           0 :     if(spw_p.nelements() == 1 && spw_p[0] < 0){
     403           0 :       spw_p.resize(ms_p.spectralWindow().nrow());
     404           0 :       indgen(spw_p);
     405             : 
     406             :       //no may be we have to redo the chan selection
     407             :       
     408           0 :       if (nchan.nelements() != spw_p.nelements()){
     409           0 :         nchan.resize(spw_p.nelements(), true);
     410           0 :         for(uInt k = 1; k < spw_p.nelements(); ++k){
     411           0 :           nchan[k] = nchan[0];
     412             :         }
     413             :       }
     414           0 :       if (start.nelements() != spw_p.nelements()){
     415           0 :         start.resize(spw_p.nelements(), true);
     416           0 :         for(uInt k = 1; k < spw_p.nelements(); ++k){
     417           0 :           start[k] = start[0];
     418             :         }
     419             :       }
     420           0 :       if (step.nelements() != spw_p.nelements()){
     421           0 :         step.resize(spw_p.nelements(), true);
     422           0 :         for(uInt k = 1; k < spw_p.nelements(); ++k){
     423           0 :           step[k] = step[0];
     424             :         }
     425             :       }
     426             :     }
     427             :         
     428           0 :     nchan_p.resize();
     429           0 :     nchan_p = nchan;
     430           0 :     chanStart_p.resize();
     431           0 :     chanStart_p = start;
     432           0 :     chanEnd_p.resize(spw_p.nelements());
     433           0 :     for(uInt k =0; k < spw_p.nelements(); ++k)
     434           0 :       chanEnd_p[k] = nchan[k] - 1;
     435           0 :     chanStep_p.resize();
     436           0 :     chanStep_p = step;
     437             :     // check for defaults
     438           0 :     if(nchan_p[0]<=0 || (nchan_p.nelements() != spw_p.nelements())){
     439           0 :       nchan_p.resize(spw_p.nelements());
     440           0 :       MSSpWindowColumns mySpwTab(ms_p.spectralWindow());
     441           0 :       for (uInt k =0; k < spw_p.nelements(); ++k){
     442           0 :         if(nchan[0]<=0)
     443           0 :           nchan_p[k]=mySpwTab.numChan()(spw_p[k]);
     444             :         else
     445           0 :           nchan_p[k]=nchan[0];
     446           0 :         chanEnd_p[k] = nchan_p[k] - 1;
     447             :       }
     448           0 :       chanStart_p.resize(spw_p.nelements());
     449           0 :       chanStep_p.resize(spw_p.nelements());
     450           0 :       if(chanStart_p.nelements() == start.nelements()){
     451           0 :         chanStart_p=start;
     452             :       }
     453             :       else{
     454           0 :         chanStart_p.set(start[0]);
     455             :       }
     456           0 :       if(chanStep_p.nelements() == step.nelements()){
     457           0 :         chanStep_p=step;
     458             :       }
     459             :       else{
     460           0 :         chanStep_p.set(step[0]);
     461             :       } 
     462           0 :     }    
     463           0 :   }
     464             : 
     465             :   // selectSpw must be called first because this uses spwRelabel_p!
     466           0 :   Bool SubMS::selectCorrelations(const String& corrstr)
     467             :   {
     468           0 :     LogIO os(LogOrigin("SubMS", "selectCorrelations()"));
     469           0 :     MSSelection mssel;
     470           0 :     const Bool areSelecting = corrstr != "" && corrstr != "*";
     471             : 
     472           0 :     if(areSelecting)
     473           0 :       mssel.setPolnExpr(corrstr);
     474           0 :     corrString_p = corrstr;
     475           0 :     mssel.getCorrSlices(corrSlices_p, &ms_p);
     476           0 :     return getCorrMaps(mssel, ms_p, inPolOutCorrToInCorrMap_p, areSelecting);
     477           0 :   }
     478             : 
     479           0 : Bool SubMS::getCorrMaps(MSSelection& mssel, const MeasurementSet& ms,
     480             :                         Vector<Vector<Int> >& outToIn, const Bool areSelecting)
     481             : {
     482           0 :   Bool cando = true;
     483             : 
     484           0 :   uInt npol = ms.polarization().nrow();  // The total number of polids
     485             :     
     486             :   // Nominally empty selection for all polids
     487           0 :   outToIn.resize(npol);
     488           0 :   outToIn.set(Vector<Int>());
     489             :     
     490           0 :   if(areSelecting){
     491             :     // Get the corr indices as an ordered map
     492           0 :     std::map<Int, Vector<Vector<Int> > > corrmap(mssel.getCorrMap(&ms));
     493             : 
     494             :     // Iterate over the ordered map to fill the vector maps
     495           0 :     for( auto mi = corrmap.begin( ); mi != corrmap.end( ); ++mi ) {
     496           0 :       Int pol = mi->first;
     497           0 :       outToIn[pol] = mi->second[0];
     498             :     }
     499           0 :   }
     500             :   else{ // Make outToIn an identity map.
     501           0 :     ScalarColumn<Int> numCorr(ms.polarization(), 
     502           0 :                                 MSPolarization::columnName(MSPolarization::NUM_CORR));
     503             :     
     504           0 :     for(uInt polid = 0; polid < npol; ++polid){
     505           0 :       uInt ncorr = numCorr(polid);
     506             :       
     507           0 :       outToIn[polid].resize(ncorr);
     508           0 :       for(uInt cid = 0; cid < ncorr; ++cid)
     509           0 :         outToIn[polid][cid] = cid;
     510             :     }
     511           0 :   }
     512           0 :   return cando;
     513             : }
     514             : 
     515             :   // This is the one used by split.
     516           0 :   Bool SubMS::setmsselect(const String& spw, const String& field,
     517             :                           const String& baseline, const String& scan,
     518             :                           const String& uvrange, const String& taql,
     519             :                           const Vector<Int>& step, const String& subarray,
     520             :                           const String& correlation, const String& intent,
     521             :                           const String& obs)
     522             :   {
     523           0 :     LogIO os(LogOrigin("SubMS", "setmsselect()"));
     524             :     Bool  ok;
     525             :     
     526           0 :     String myspwstr(spw == "" ? "*" : spw);
     527           0 :     Record selrec = ms_p.msseltoindex(myspwstr, field);
     528             : 
     529           0 :     ok = selectSource(selrec.asArrayInt("field"));
     530             : 
     531             :     // All of the requested selection functions will be tried, even if an
     532             :     // earlier one has indicated its failure.  This allows all of the selection
     533             :     // strings to be tested, yielding more complete feedback for the user
     534             :     // (fewer retries).  This is a matter of taste, though.  If the selections
     535             :     // turn out to be slow, this function should return on the first false.
     536             : 
     537           0 :     if(!selectSpw(myspwstr, step)){
     538           0 :       os << LogIO::SEVERE << "No channels selected." << LogIO::POST;
     539           0 :       ok = false;
     540             :     }
     541             :     
     542           0 :     if(baseline != ""){
     543           0 :       Vector<Int> antid(0);
     544           0 :       Vector<String> antstr(1,baseline);
     545           0 :       selectAntenna(antid, antstr);
     546           0 :     }
     547           0 :     scanString_p    = scan;
     548           0 :     intentString_p  = intent;
     549           0 :     obsString_p     = obs;
     550           0 :     uvrangeString_p = uvrange;
     551           0 :     taqlString_p    = taql;
     552             : 
     553           0 :     if(subarray != "")
     554           0 :       selectArray(subarray);
     555             : 
     556           0 :     if(!selectCorrelations(correlation)){
     557           0 :       os << LogIO::SEVERE << "No correlations selected." << LogIO::POST;
     558           0 :       ok = false;
     559             :     }
     560             : 
     561           0 :     return ok;
     562           0 :   }
     563             : 
     564             :   // This is the older version, used elsewhere.
     565           0 :   void SubMS::setmsselect(const String& spw, const String& field, 
     566             :                           const String& baseline, const String& scan,
     567             :                           const String& obs, const String& uvrange, 
     568             :                           const String& taql, const Vector<Int>& nchan, 
     569             :                           const Vector<Int>& start, const Vector<Int>& step,
     570             :                           const String& subarray, const String& intent)
     571             :   {
     572           0 :     Vector<Int> inchan(1, -1);
     573           0 :     Vector<Int> istart(1, 0);
     574           0 :     Vector<Int> istep(1, 1);
     575           0 :     Record      selrec = ms_p.msseltoindex(spw, field);
     576           0 :     Vector<Int> spwids = selrec.asArrayInt("spw");
     577             : 
     578           0 :     selectSource(selrec.asArrayInt("field"));
     579           0 :     if(spwids.nelements() < 1)
     580           0 :       spwids=Vector<Int>(1, -1);
     581             : 
     582             :     //use nchan if defined else use caret-column syntax of  msselection 
     583           0 :     if((nchan.nelements()>0) && nchan[0] > 0){
     584           0 :       inchan.resize(); inchan=nchan;
     585           0 :       if((step.nelements() >0 ) && (step.nelements() != nchan.nelements()) && (nchan.nelements() >1)){
     586           0 :         istep[0]=step[0];
     587             :       }
     588           0 :       if(step.nelements() != nchan.nelements()){
     589           0 :         istep.resize(nchan.nelements(), true);
     590           0 :         istep.set(istep[0]);
     591             :       }
     592             :       else{
     593           0 :         istep.resize(); istep=step;
     594             :       }
     595           0 :       if((start.nelements() >0 ) && (start.nelements() != nchan.nelements()) && (nchan.nelements() >1)){
     596           0 :         istart[0]=start[0];
     597             :       }
     598           0 :       if(start.nelements() != nchan.nelements()){
     599           0 :         istart.resize(nchan.nelements(), true);
     600           0 :         istart.set(istart[0]);
     601             :       }
     602             :       else{
     603           0 :         istart.resize(); istart=start;
     604             :       }
     605             :     }
     606             :     else{
     607           0 :       Matrix<Int> chansel=selrec.asArrayInt("channel");
     608           0 :       if(chansel.nelements() != 0){
     609           0 :         inchan.resize(chansel.nrow());
     610           0 :         istep.resize(chansel.nrow());
     611           0 :         istart.resize(chansel.nrow());
     612             :         // if the vector step is used ..for averaging ..let's use it
     613           0 :         Bool stepused=false;
     614           0 :         if( (step.nelements() >= 1) && (max(step) > 1))
     615           0 :           stepused=true;
     616           0 :         for (uInt k =0 ; k < chansel.nrow(); ++k){
     617           0 :           if(stepused){
     618           0 :             if(step.nelements() == 1)
     619           0 :               istep[k] = step[0];
     620           0 :             else if(step.nelements() == istep.nelements())
     621           0 :               istep[k] = step[k];
     622             :             else //confused at this stage
     623           0 :               istep[k] = 1;
     624             :           }
     625             :           else{
     626           0 :             istep[k] = chansel.row(k)(3);
     627           0 :             if(istep[k] < 1)
     628           0 :               istep[k] = 1;
     629             :           }
     630           0 :           istart[k] = chansel.row(k)(1);
     631           0 :           inchan[k] = (chansel.row(k)(2) - istart[k] + 1) / istep[k];
     632           0 :           if(inchan[k] < 1)
     633           0 :             inchan[k] = 1;
     634             :         }
     635             :       } 
     636           0 :     }
     637           0 :     selectSpw(spwids, inchan, istart, istep);
     638             :     
     639           0 :     if(baseline != ""){
     640           0 :       Vector<Int> antid(0);
     641           0 :       Vector<String> antstr(1,baseline);
     642           0 :       selectAntenna(antid, antstr);
     643           0 :     }
     644           0 :     scanString_p    = scan;
     645           0 :     obsString_p    = obs;
     646           0 :     uvrangeString_p = uvrange;
     647           0 :     taqlString_p    = taql;
     648           0 :     intentString_p  = intent;
     649             : 
     650           0 :     if(subarray != "")
     651           0 :       selectArray(subarray);
     652           0 :   }  
     653             : 
     654             :   
     655           0 :   Bool SubMS::selectSource(const Vector<Int>& fieldid)
     656             :   {
     657           0 :     LogIO os(LogOrigin("SubMS", "selectSource()"));
     658           0 :     Bool cando = true;
     659             : 
     660           0 :     if(fieldid.nelements() < 1){
     661           0 :       fieldid_p = Vector<Int>(1, -1);
     662             :     }
     663           0 :     else if(fieldid.nelements() > ms_p.field().nrow()){
     664             :       os << LogIO::SEVERE
     665             :          << "More fields were requested than are in the input MS.\n"
     666           0 :          << LogIO::POST;
     667           0 :       cando = false;
     668             :     }
     669           0 :     else if(max(fieldid) >= static_cast<Int>(ms_p.field().nrow())){
     670             :       // Arriving here is very unlikely since if fieldid came from MSSelection
     671             :       // bad fields were presumably already quietly dropped.
     672             :       os << LogIO::SEVERE
     673             :          << "At least 1 field was requested that is not in the input MS.\n"
     674           0 :          << LogIO::POST;      
     675           0 :       cando = false;
     676             :     }
     677             :     else{
     678           0 :       fieldid_p = fieldid;
     679             :     }
     680             : 
     681           0 :     if(fieldid_p.nelements() == 1 && fieldid_p[0] < 0){
     682           0 :       fieldid_p.resize(ms_p.field().nrow());
     683           0 :       indgen(fieldid_p);
     684             :     }
     685           0 :     return cando;
     686           0 :   }
     687             :   
     688           0 : Bool SubMS::pickAntennas(Vector<Int>& selected_antennaids,
     689             :                          Vector<String>& selected_antenna_strs,
     690             :                          const Vector<Int>& antennaids,
     691             :                          const Vector<String>& antennaSel)
     692             : {
     693           0 :   Bool didSelect = true;
     694           0 :   if((antennaids.nelements() == 1) && (antennaids[0] == -1)){
     695           0 :     if(antennaSel[0] == "")
     696           0 :       didSelect = false;
     697             :     else
     698           0 :       selected_antennaids.resize();
     699             :   }
     700             :   else
     701           0 :     selected_antennaids = antennaids;
     702           0 :   selected_antenna_strs = antennaSel;
     703           0 :   return didSelect;
     704             : }
     705             :   
     706           0 :   void SubMS::selectTime(Double timeBin, String timerng)
     707             :   {  
     708           0 :     timeBin_p   = timeBin;
     709           0 :     timeRange_p = timerng;
     710           0 :   }  
     711             :   
     712           0 :   Bool SubMS::makeSubMS(String& msname, String& colname,
     713             :                         const Vector<Int>& tileShape, const String& combine)
     714             :   {
     715           0 :     LogIO os(LogOrigin("SubMS", "makeSubMS()"));
     716             : 
     717             :     /*
     718             :     try{
     719             :     */
     720             : 
     721           0 :       if((spw_p.nelements()>0) && (max(spw_p) >= Int(ms_p.spectralWindow().nrow()))){
     722             :         os << LogIO::SEVERE 
     723             :            << "SpectralWindow selection contains elements that do not exist in "
     724             :            << "this MS"
     725           0 :            << LogIO::POST;
     726           0 :         ms_p=MeasurementSet();
     727           0 :         return false;   
     728             :       }
     729             :       
     730             :       // Watch out!  This throws an AipsError if ms_p doesn't have the
     731             :       // requested columns.
     732           0 :       const Vector<MS::PredefinedColumns> colNamesTok = parseColumnNames(colname, ms_p);
     733             : 
     734           0 :       if(!makeSelection()){
     735             :         //os << LogIO::WARN 
     736             :         //   << "Failed on selection: the combination of spw, field, antenna, correlation, "
     737             :         //   << "and timerange may be invalid." 
     738             :         //   << LogIO::POST;
     739           0 :         ms_p=MeasurementSet();
     740           0 :         return false;
     741             :       }
     742           0 :       mscIn_p=new MSColumns(mssel_p);
     743             :       // Note again the parseColumnNames() a few lines back that stops setupMS()
     744             :       // from being called if the MS doesn't have the requested columns.
     745           0 :       MeasurementSet* outpointer=0;
     746             : 
     747           0 :       if(tileShape.nelements() == 3){
     748           0 :         outpointer = setupMS(msname, nchan_p[0], ncorr_p[0],  
     749             :                              colNamesTok, tileShape);
     750             :       }
     751             : 
     752             :       // the following calls MSTileLayout...  disabled for now because it
     753             :       // forces tiles to be the full spw bandwidth in width (gmoellen, 2010/11/07)
     754             :   
     755           0 :       else if((tileShape.nelements()==1) && (tileShape[0]==0 || tileShape[0]==1)){
     756           0 :         outpointer = setupMS(msname, nchan_p[0], ncorr_p[0],
     757           0 :                              mscIn_p->observation().telescopeName()(0),
     758           0 :                              colNamesTok, tileShape[0]);
     759             :       }
     760             :       /* 
     761             :       else{
     762             :         // Derive tile shape based on input dataset's tiles, borrowed
     763             :         //  from VisSet's scr col tile shape derivation
     764             :         //  (this may need some tweaking for averaging cases)
     765             :         TableDesc td = mssel_p.actualTableDesc();
     766             : 
     767             :         // If a non-DATA column, i.e. CORRECTED_DATA, is being written to DATA,
     768             :         // datacolname must be set to DATA because the tile management in
     769             :         // setupMS() will look for "TiledDATA", not "TiledCorrectedData".
     770             :         String datacolname = MS::columnName(MS::DATA);
     771             :         // But if DATA is not present in the input MS, using it would cause a
     772             :         // segfault.
     773             :         if(!td.isColumn(datacolname))
     774             :           // This is could be any other kind of *DATA column, including
     775             :           // FLOAT_DATA or LAG_DATA, but it is guaranteed to be something.
     776             :           datacolname = MS::columnName(colNamesTok[0]);
     777             : 
     778             :         const ColumnDesc& cdesc = td[datacolname];
     779             : 
     780             :         String dataManType = cdesc.dataManagerType();
     781             :         String dataManGroup = cdesc.dataManagerGroup();
     782             : 
     783             :         Bool tiled = (dataManType.contains("Tiled"));
     784             : 
     785             :         if (tiled) {
     786             :             ROTiledStManAccessor tsm(mssel_p, dataManGroup);
     787             :             uInt nHyper = tsm.nhypercubes();
     788             : 
     789             :             // Test clause
     790             :             if(1){
     791             :               os << LogIO::DEBUG1
     792             :                  << datacolname << "'s max cache size: "
     793             :                  << tsm.maximumCacheSize() << " bytes.\n"
     794             :                  << "\tnhypercubes: " << nHyper << ".\n"
     795             :                  << "\ttshp of row 0: " << tsm.tileShape(0)
     796             :                  << "\n\thypercube shape of row 0: " << tsm.hypercubeShape(0)
     797             :                  << LogIO::POST;
     798             :             }
     799             :     
     800             : 
     801             :             // Find smallest tile shape
     802             :             Int highestProduct=-INT_MAX;
     803             :             Int highestId=0;
     804             :             for (uInt id=0; id < nHyper; id++) {
     805             :               IPosition tshp(tsm.getTileShape(id));
     806             :               Int product = tshp.product();
     807             : 
     808             :               os << LogIO::DEBUG2
     809             :                  << "\thypercube " << id << ":\n"
     810             :                  << "\t\ttshp: " << tshp << "\n"
     811             :                  << "\t\thypercube shape: " << tsm.getHypercubeShape(id)
     812             :                  << ".\n\t\tcache size: " << tsm.getCacheSize(id)
     813             :                  << " buckets.\n\t\tBucket size: " << tsm.getBucketSize(id)
     814             :                  << " bytes."
     815             :                  << LogIO::POST;
     816             : 
     817             :               if (product > 0 && (product > highestProduct)) {
     818             :                 highestProduct = product;
     819             :                 highestId = id;
     820             :               }
     821             :             }
     822             :             Vector<Int> dataTileShape = tsm.getTileShape(highestId).asVector();
     823             : 
     824             :             outpointer = setupMS(msname, nchan_p[0], ncorr_p[0],  
     825             :                                  colNamesTok, dataTileShape);
     826             : 
     827             :         }
     828             :       */
     829             :       else{
     830             :           //Sweep all other cases of bad tileshape to a default one.
     831             :           //  (this probably never happens)
     832           0 :           outpointer = setupMS(msname, nchan_p[0], ncorr_p[0],
     833           0 :                                mscIn_p->observation().telescopeName()(0),  
     834             :                                colNamesTok, 0);
     835             :         
     836             :       }
     837             :       
     838           0 :       combine_p = combine;
     839             : 
     840           0 :       msOut_p= *outpointer;
     841             :       
     842           0 :       if(!fillAllTables(colNamesTok)){
     843           0 :         delete outpointer;
     844           0 :         os << LogIO::WARN << msname << " left unfinished." << LogIO::POST;
     845           0 :         ms_p=MeasurementSet();
     846           0 :         return false;
     847             :       }
     848             :       
     849             :       //  msOut_p.relinquishAutoLocks (true);
     850             :       //  msOut_p.unlock();
     851             :       //Detaching the selected part
     852           0 :       ms_p=MeasurementSet();
     853             :       
     854           0 :       delete outpointer;
     855           0 :       return true;
     856             : 
     857             :     /*
     858             :     }
     859             :     catch(AipsError x){
     860             :       ms_p=MeasurementSet();
     861             :       throw(x);
     862             :     }
     863             :     catch(...){
     864             :       ms_p=MeasurementSet();
     865             :       throw(AipsError("Unknown exception caught"));
     866             :     }
     867             :     */
     868             : 
     869           0 :   }
     870             :   
     871           0 :   MeasurementSet* SubMS::makeScratchSubMS(const String& colname,
     872             :                                           const Bool forceInMemory)
     873             :   {
     874           0 :     return makeScratchSubMS(parseColumnNames(colname, ms_p), forceInMemory);
     875             :   }
     876             :   
     877           0 :   MeasurementSet* SubMS::makeScratchSubMS(const Vector<MS::PredefinedColumns>& whichDataCols,
     878             :                                           const Bool forceInMemory)
     879             :   {
     880           0 :     LogIO os(LogOrigin("SubMS", "makeScratchSubMS()"));
     881             :     
     882           0 :     if(max(fieldid_p) >= Int(ms_p.field().nrow())){
     883             :       os << LogIO::SEVERE 
     884             :          << "Field selection contains elements that do not exist in "
     885             :          << "this MS"
     886           0 :          << LogIO::POST;
     887           0 :       ms_p=MeasurementSet();
     888           0 :       return 0;  
     889             :     }
     890           0 :     if(max(spw_p) >= Int(ms_p.spectralWindow().nrow())){
     891             :       os << LogIO::SEVERE 
     892             :          << "SpectralWindow selection contains elements that do not exist in "
     893             :          << "this MS"
     894           0 :          << LogIO::POST;
     895           0 :       ms_p=MeasurementSet();
     896           0 :       return 0;
     897             :     }
     898             :     
     899           0 :     if(!makeSelection()){
     900             :       os << LogIO::SEVERE 
     901             :          << "Failed on selection: combination of spw and/or field and/or time "
     902             :          << "chosen may be invalid."
     903           0 :          << LogIO::POST;
     904           0 :       ms_p=MeasurementSet();
     905           0 :       return 0;
     906             :     }
     907           0 :     mscIn_p=new MSColumns(mssel_p);
     908           0 :     Double sizeInMB= 1.5 * n_bytes() / (1024.0 * 1024.0);
     909           0 :     String msname=AppInfo::workFileName(uInt(sizeInMB), "TempSubMS");
     910             :     
     911           0 :     MeasurementSet* outpointer=setupMS(msname, nchan_p[0], ncorr_p[0],  
     912           0 :                                        mscIn_p->observation().telescopeName()(0),
     913             :                                        whichDataCols);
     914             :     
     915           0 :     outpointer->markForDelete();
     916             :     //Hmmmmmm....memory...... 
     917           0 :     if(sizeInMB <  (Double)(HostInfo::memoryTotal(true))/(2048.0) 
     918           0 :        || forceInMemory){
     919           0 :       MeasurementSet* a = outpointer;
     920           0 :       outpointer= new MeasurementSet(a->copyToMemoryTable("TmpMemoryMS"));
     921           0 :       outpointer->initRefs();
     922           0 :       delete a;
     923             :     }
     924             :     
     925           0 :     msOut_p = *outpointer;
     926             :     
     927           0 :     if(!fillAllTables(whichDataCols)){
     928           0 :       delete outpointer;
     929           0 :       outpointer = 0;
     930           0 :       ms_p = MeasurementSet();
     931           0 :       return 0;
     932             :     }
     933             :     //Detaching the selected part
     934           0 :     ms_p=MeasurementSet();
     935           0 :     return outpointer;
     936           0 :   }
     937             : 
     938             :   
     939           0 : Bool SubMS::fillAllTables(const Vector<MS::PredefinedColumns>& datacols)
     940             : {
     941             : 
     942           0 :   LogIO os(LogOrigin("SubMS", "fillAllTables()"));
     943           0 :   Bool success = true;
     944             : 
     945             :   // Copy the subtables before doing anything with the main table.  Otherwise
     946             :   // MSColumns won't work.
     947             : 
     948             :   // fill or update
     949           0 :   Timer timer;
     950             : 
     951           0 :   timer.mark();
     952           0 :   success &= copyPointing();
     953             :   os << LogIO::DEBUG1
     954             :      << "copyPointing took " << timer.real() << "s."
     955           0 :      << LogIO::POST;
     956             : 
     957             :   // Optional columns should be set up before msc_p.
     958           0 :   addOptionalColumns(mssel_p.spectralWindow(), msOut_p.spectralWindow(), true);
     959             : 
     960             :   // Force the Measures frames for all the time type columns to have the same
     961             :   // reference as the TIME column of the main table.
     962             :   // Disable the empty table check (with false) because some of the subtables
     963             :   // (like POINTING) might already have been written.
     964             :   // However, empty tables are still empty after setting up the reference codes
     965             :   // here.
     966           0 :   msc_p = new MSColumns(msOut_p);
     967           0 :   msc_p->setEpochRef(MEpoch::castType(mscIn_p->timeMeas().getMeasRef().getType()),
     968             :                      false);
     969             : 
     970             :   // UVW is the only other Measures column in the main table.
     971           0 :   msc_p->uvwMeas().setDescRefCode(Muvw::castType(mscIn_p->uvwMeas().getMeasRef().getType()));
     972             : 
     973           0 :   if(!mscIn_p->flagCategory().isNull() && mscIn_p->flagCategory().isDefined(0))
     974           0 :     msc_p->setFlagCategories(mscIn_p->flagCategories());
     975             : 
     976           0 :   timer.mark();
     977           0 :   if(!fillDDTables())
     978           0 :     return false;
     979             :   os << LogIO::DEBUG1
     980             :      << "fillDDTables took " << timer.real() << "s."
     981           0 :      << LogIO::POST;
     982             : 
     983             :   // SourceIDs need to be remapped around here.  It could not be done in
     984             :   // selectSource() because mssel_p was not setup yet.
     985           0 :   timer.mark();
     986           0 :   relabelSources();
     987             :   os << LogIO::DEBUG1
     988             :      << "relabelSources took " << timer.real() << "s."
     989           0 :      << LogIO::POST;
     990             : 
     991           0 :   success &= fillFieldTable();
     992           0 :   success &= copySource();
     993             : 
     994           0 :   success &= copyAntenna();
     995           0 :   if(!copyFeed())         // Feed table writing has to be after antenna 
     996           0 :     return false;
     997             : 
     998           0 :   success &= copyFlag_Cmd();
     999           0 :   success &= copyHistory();
    1000           0 :   success &= copyObservation();
    1001           0 :   success &= copyProcessor();
    1002           0 :   success &= copyState();
    1003             : 
    1004           0 :   timer.mark();
    1005           0 :   success &= copySyscal();
    1006             :   os << LogIO::DEBUG1
    1007             :      << "copySyscal took " << timer.real() << "s."
    1008           0 :      << LogIO::POST;
    1009             : 
    1010           0 :   timer.mark();
    1011           0 :   success &= copyWeather();
    1012             :   os << LogIO::DEBUG1
    1013             :      << "copyWeather took " << timer.real() << "s."
    1014           0 :      << LogIO::POST;
    1015             :   
    1016           0 :   timer.mark();
    1017           0 :   success &= filterOptSubtable("CALDEVICE");
    1018             :   os << LogIO::DEBUG1
    1019             :      << "CALDEVICE took " << timer.real() << "s."
    1020           0 :      << LogIO::POST;
    1021             : 
    1022           0 :   timer.mark();
    1023           0 :   success &= filterOptSubtable("SYSPOWER");
    1024             :   os << LogIO::DEBUG1
    1025             :      << "SYSPOWER took " << timer.real() << "s."
    1026           0 :      << LogIO::POST;
    1027             : 
    1028             :   // Run this after running the other copy*()s.  Maybe there should be an
    1029             :   // option to *not* run it.
    1030           0 :   success &= copyGenericSubtables();
    1031             : 
    1032             :   //sameShape_p = areDataShapesConstant();
    1033             : 
    1034             :   // jagonzal: Allow main table to be left empty, so that it can be filled by another layer.
    1035           0 :   if (fillMainTable_p)
    1036             :   {
    1037           0 :           if(fitorder_p < 0 && timeBin_p <= 0.0)
    1038           0 :             success &= writeAllMainRows(datacols);
    1039             :           else
    1040           0 :             success &= writeSomeMainRows(datacols);
    1041           0 :           return success;
    1042             :   }
    1043             :   else
    1044             :   {
    1045           0 :           return success;
    1046             :   }
    1047           0 : }
    1048             :   
    1049             :   
    1050           0 :   Bool SubMS::makeSelection(){
    1051             :     
    1052           0 :     LogIO os(LogOrigin("SubMS", "makeSelection()"));
    1053             :     
    1054             :     //VisSet/MSIter will check if the SORTED exists
    1055             :     //and resort if necessary
    1056             :     {
    1057             :       // Matrix<Int> noselection;
    1058             :       // VisSet vs(ms_p, noselection);
    1059           0 :       Block<Int> sort;
    1060           0 :       ROVisibilityIterator(ms_p, sort);
    1061           0 :     }
    1062             :    
    1063             :     const MeasurementSet *elms;
    1064           0 :     elms=&ms_p;
    1065           0 :     MeasurementSet sorted;
    1066           0 :     if (ms_p.keywordSet().isDefined("SORTED_TABLE")) {
    1067           0 :       sorted=ms_p.keywordSet().asTable("SORTED_TABLE");
    1068             :       //If ms is not writable and sort is a subselection...use original ms
    1069           0 :       if( ms_p.nrow() == sorted.nrow())
    1070           0 :         elms=&sorted;
    1071             :     }
    1072             :  
    1073           0 :     MSSelection thisSelection;
    1074           0 :     if(fieldid_p.nelements() > 0)
    1075           0 :       thisSelection.setFieldExpr(MSSelection::indexExprStr(fieldid_p));
    1076           0 :     if(spw_p.nelements() > 0)
    1077           0 :       thisSelection.setSpwExpr(MSSelection::indexExprStr(spw_p));
    1078           0 :     if(antennaSel_p){
    1079           0 :       if(antennaId_p.nelements() > 0){
    1080           0 :         thisSelection.setAntennaExpr(MSSelection::indexExprStr( antennaId_p ));
    1081             :       }
    1082           0 :       if(antennaSelStr_p[0] != "")
    1083           0 :         thisSelection.setAntennaExpr(MSSelection::nameExprStr( antennaSelStr_p));
    1084             :     }
    1085           0 :     if(timeRange_p != "")
    1086           0 :       thisSelection.setTimeExpr(timeRange_p);
    1087             :     
    1088           0 :     thisSelection.setUvDistExpr(uvrangeString_p);
    1089           0 :     thisSelection.setScanExpr(scanString_p);
    1090           0 :     thisSelection.setStateExpr(intentString_p);
    1091           0 :     thisSelection.setObservationExpr(obsString_p);
    1092           0 :     if(arrayExpr_p != "")
    1093           0 :       thisSelection.setArrayExpr(arrayExpr_p);
    1094           0 :     if(corrString_p != "")
    1095           0 :       thisSelection.setPolnExpr(corrString_p);
    1096           0 :     thisSelection.setTaQLExpr(taqlString_p);
    1097             :     
    1098           0 :     TableExprNode exprNode=thisSelection.toTableExprNode(elms);
    1099           0 :     selTimeRanges_p = thisSelection.getTimeList();
    1100           0 :     selObsId_p = thisSelection.getObservationList();
    1101             : 
    1102             :     {      
    1103           0 :       const MSDataDescription ddtable = elms->dataDescription();
    1104             :       ScalarColumn<Int> polId(ddtable, 
    1105           0 :                                 MSDataDescription::columnName(MSDataDescription::POLARIZATION_ID));
    1106           0 :       const MSPolarization poltable = elms->polarization();
    1107             :       ArrayColumn<Int> pols(poltable, 
    1108           0 :                               MSPolarization::columnName(MSPolarization::CORR_TYPE));
    1109             :       
    1110             :       ScalarColumn<Int> spwId(ddtable, 
    1111           0 :                                 MSDataDescription::columnName(MSDataDescription::SPECTRAL_WINDOW_ID));
    1112             : 
    1113           0 :       uInt nddids = polId.nrow();
    1114           0 :       uInt nSpws = spw_p.nelements();
    1115             : 
    1116             :       // # of distinct channel ranges or pol setups (!#pols) per spw.
    1117           0 :       Vector<uInt> nuses_per_spw;
    1118             : 
    1119           0 :       Int highestSpw = max(spw_p);
    1120           0 :       if(highestSpw < 0)
    1121           0 :         highestSpw = 0;
    1122           0 :       spw2ddid_p.resize(highestSpw + 1);
    1123           0 :       nuses_per_spw.resize(highestSpw + 1);
    1124           0 :       spw2ddid_p.set(0);                 // This is a row #, so must be >= 0.
    1125           0 :       nuses_per_spw.set(0);
    1126           0 :       Bool ddidprob = false;
    1127           0 :       for(uInt j = 0; j < nddids; ++j){
    1128           0 :         Int spw = spwId(j);
    1129           0 :         for(uInt k = 0; k < nSpws; ++k){
    1130           0 :           if(spw == spw_p[k]){
    1131           0 :             ++nuses_per_spw[spw];
    1132           0 :             if(nuses_per_spw[spw_p[k]] == 2){
    1133           0 :               ddidprob = true;
    1134             :               os << LogIO::SEVERE
    1135           0 :                  << "Input spw " << spw_p[k] << " was selected for > 1 "
    1136           0 :                  << "channel ranges or polarization setups." << LogIO::POST;
    1137             :             }
    1138           0 :             spw2ddid_p[spw] = j;
    1139             :           }
    1140             :         }
    1141             :       }
    1142             : 
    1143           0 :       if(ddidprob){
    1144             :         os << LogIO::SEVERE
    1145             :            << "split does not yet support more than 1 channel range (';' in spw)"
    1146             :            << "\nor polarization setup per spectral window."
    1147           0 :            << LogIO::POST;
    1148           0 :         return false;
    1149             :       }
    1150             : 
    1151           0 :       Vector<Int> ddids;
    1152           0 :       ddids.resize(nSpws);
    1153             : 
    1154           0 :       inNumCorr_p.resize(nSpws);
    1155           0 :       ncorr_p.resize(nSpws);
    1156           0 :       for(uInt k = 0; k < nSpws; ++k){
    1157           0 :         Int ddid = spw2ddid_p[spw_p[k]];
    1158             :         
    1159           0 :         inNumCorr_p[k] = pols(polId(ddid)).nelements();
    1160           0 :         ncorr_p[k] = inPolOutCorrToInCorrMap_p[polId(ddid)].nelements();
    1161           0 :         if(ncorr_p[k] == 0){
    1162             :           os << LogIO::SEVERE
    1163             :              << "None of the selected correlations are in spectral window "
    1164           0 :              << spw_p[k]
    1165           0 :              << LogIO::POST;
    1166           0 :           return false;
    1167             :         }
    1168             :       }
    1169           0 :     }
    1170             :     
    1171             :     // Now remake the selected ms
    1172           0 :     if(!(exprNode.isNull())){
    1173           0 :       mssel_p = MeasurementSet((*elms)(exprNode));
    1174             :     }
    1175             :     else{
    1176             :       // Null take all the ms ...setdata() blank means that
    1177           0 :       mssel_p = MeasurementSet((*elms));
    1178             :     }
    1179             :     //mssel_p.rename(ms_p.tableName()+"/SELECTED_TABLE", Table::Scratch);
    1180           0 :     if(mssel_p.nrow() == 0)
    1181           0 :       return false;
    1182             : 
    1183             :     // Setup antNewIndex_p now that mssel_p is ready.
    1184           0 :     if(antennaSel_p){
    1185             :       // Watch out! getAntenna*List() and getBaselineList() return negative
    1186             :       // numbers for negated antennas!
    1187             :       //Vector<Int> selAnt1s(thisSelection.getAntenna1List());
    1188             :       //Vector<Int> selAnt2s(thisSelection.getAntenna2List());
    1189           0 :       ScalarColumn<Int> ant1c(mssel_p, MS::columnName(MS::ANTENNA1));
    1190           0 :       ScalarColumn<Int> ant2c(mssel_p, MS::columnName(MS::ANTENNA2));
    1191           0 :       Vector<Int> selAnts(ant1c.getColumn());
    1192           0 :       uInt nAnts = selAnts.nelements();
    1193             : 
    1194           0 :       selAnts.resize(2 * nAnts, true);
    1195           0 :       selAnts(Slice(nAnts, nAnts)) = ant2c.getColumn();
    1196           0 :       nAnts = GenSort<Int>::sort(selAnts, Sort::Ascending, Sort::NoDuplicates);
    1197           0 :       selAnts.resize(nAnts, true);
    1198           0 :       Int maxAnt = max(selAnts);
    1199             : 
    1200           0 :       if(maxAnt < 0){
    1201             :         os << LogIO::SEVERE
    1202             :            << "The maximum selected antenna number, " << maxAnt
    1203             :            << ", seems to be < 0."
    1204           0 :            << LogIO::POST;
    1205           0 :         return false;
    1206             :       }
    1207           0 :       antNewIndex_p.resize(maxAnt + 1);
    1208           0 :       antNewIndex_p.set(-1); //So if you see -1 in the main, feed, or pointing
    1209             :                              //tables, fix it
    1210           0 :       Bool trivial = true;
    1211           0 :       for(uInt k = 0; k < nAnts; ++k){
    1212           0 :         trivial &= (selAnts[k] == static_cast<Int>(k));   // trivial = selAnts == indgen(nAnts)
    1213           0 :         antNewIndex_p[selAnts[k]] = k;
    1214             :       }                                  // It is possible to exclude baselines
    1215           0 :       antennaSel_p = !trivial;           // without excluding any antennas.
    1216           0 :     }                                    // This still gets tripped up by VLA:OUT.
    1217             :     else{        // Make a default antNewIndex_p.
    1218           0 :       antNewIndex_p.resize(mssel_p.antenna().nrow());
    1219           0 :       indgen(antNewIndex_p);
    1220             :     }
    1221             :       
    1222           0 :     if(mssel_p.nrow() < ms_p.nrow()){
    1223             :       os << LogIO::NORMAL
    1224             :          << mssel_p.nrow() << " out of " << ms_p.nrow() << " rows are going to be" 
    1225             :          << " considered due to the selection criteria." 
    1226           0 :          << LogIO::POST;
    1227             :     }
    1228           0 :     return true;
    1229           0 :   }
    1230             : 
    1231           0 :   MeasurementSet* SubMS::setupMS(const String& MSFileName, const Int nchan,
    1232             :                                  const Int nCorr, const String& telescop,
    1233             :                                  const Vector<MS::PredefinedColumns>& colNames,
    1234             :                                  const Int obstype,
    1235             :                                  const Bool compress,
    1236             :                                  const asdmStManUseAlternatives asdmStManUse)
    1237             :   {
    1238             :     //Choose an appropriate tileshape
    1239           0 :     IPosition dataShape(2, nCorr, nchan);
    1240           0 :     IPosition tileShape = MSTileLayout::tileShape(dataShape, obstype, telescop);
    1241           0 :     return setupMS(MSFileName, nchan, nCorr, colNames, tileShape.asVector(),
    1242           0 :                    compress, asdmStManUse);
    1243             :     //return setupMS(MSFileName, nchan, nCorr, colNames, tileShape.asVector());
    1244           0 :   }
    1245           0 :   MeasurementSet* SubMS::setupMS(const String& MSFileName, const Int nchan,
    1246             :                                  const Int nCorr, 
    1247             :                                  const Vector<MS::PredefinedColumns>& colNamesTok,
    1248             :                                  const Vector<Int>& tshape, const Bool compress,
    1249             :                                  const asdmStManUseAlternatives asdmStManUse)
    1250             :   {
    1251           0 :     if(tshape.nelements() != 3)
    1252           0 :       throw(AipsError("TileShape has to have 3 elements ") );
    1253             : 
    1254             :     // This is more to shush a compiler warning than to warn users.
    1255           0 :     LogIO os(LogOrigin("SubMS", "setupMS()"));
    1256           0 :     if(tshape[0] != nCorr)
    1257             :       os << LogIO::DEBUG1
    1258           0 :          << "Warning: using " << tshape[0] << " from the tileshape instead of "
    1259             :          << nCorr << " for the number of correlations."
    1260           0 :          << LogIO::POST;
    1261           0 :     if(tshape[1] != nchan)
    1262             :       os << LogIO::DEBUG1
    1263           0 :          << "Warning: using " << tshape[1] << " from the tileshape instead of "
    1264             :          << nchan << " for the number of channels."
    1265           0 :          << LogIO::POST;
    1266             : 
    1267             :     // Choose an appropriate tileshape
    1268             :     //IPosition dataShape(2,nCorr,nchan);
    1269             :     //IPosition tileShape = MSTileLayout::tileShape(dataShape,obsType, telescop);
    1270             :     //////////////////
    1271             :     
    1272           0 :     IPosition tileShape(tshape);
    1273             : 
    1274             :     // Make the MS table
    1275           0 :     TableDesc td = MS::requiredTableDesc();
    1276           0 :     Vector<String> tiledDataNames;
    1277             : 
    1278             :     // Even though we know the data is going to be the same shape throughout I'll
    1279             :     // still create a column that has a variable shape as this will permit MS's
    1280             :     // with other shapes to be appended.
    1281           0 :     uInt ncols = colNamesTok.nelements();
    1282           0 :     const Bool mustWriteOnlyToData = mustConvertToData(ncols, colNamesTok);
    1283           0 :     Bool hasFloatData = False;
    1284             : 
    1285           0 :     if (mustWriteOnlyToData)
    1286             :       {
    1287           0 :         MS::addColumnToDesc(td, MS::DATA, 2);
    1288           0 :         if(asdmStManUse==DONT){
    1289           0 :           if (compress) MS::addColumnCompression(td,MS::DATA,true);
    1290           0 :           String hcolName=String("Tiled")+String("DATA");
    1291           0 :           td.defineHypercolumn(hcolName, 3,
    1292           0 :                                stringToVector("DATA"));
    1293           0 :           tiledDataNames.resize(1);
    1294           0 :           tiledDataNames[0] = hcolName;
    1295           0 :         }
    1296             :       }
    1297             :     else{
    1298           0 :       tiledDataNames.resize(ncols);
    1299           0 :       for(uInt i = 0; i < ncols; ++i){
    1300             :         // only add data-like columns here
    1301             :         // Unfortunately MS::PredefinedColumns aren't ordered so that I can just check if
    1302             :         // colNamesTok[i] is in the "data range".
    1303           0 :         if(colNamesTok[i] == MS::DATA ||
    1304           0 :            colNamesTok[i] == MS::MODEL_DATA ||
    1305           0 :            colNamesTok[i] == MS::CORRECTED_DATA ||
    1306           0 :            colNamesTok[i] == MS::FLOAT_DATA ||
    1307           0 :            colNamesTok[i] == MS::LAG_DATA) {
    1308           0 :           MS::addColumnToDesc(td, colNamesTok[i], 2);
    1309             :           // compress them unless they are DATA or FLOAT_DATA and the AsdmStMan is in use
    1310           0 :           if(asdmStManUse==DONT ||
    1311           0 :              (colNamesTok[i] != MS::DATA && colNamesTok[i] != MS::FLOAT_DATA)){
    1312           0 :             if (compress) MS::addColumnCompression(td,colNamesTok[i],true);
    1313             :           }
    1314           0 :           if (colNamesTok[i] == MS::FLOAT_DATA) {
    1315             :             // make a note that FLOAT_DATA is being used, for later.
    1316           0 :             hasFloatData = True;
    1317             :           }
    1318             :         }
    1319             :         else {
    1320           0 :           throw(AipsError(MS::columnName(colNamesTok[i]) +
    1321           0 :                           " is not a recognized data column "));
    1322             :         }
    1323             :         //  Add tiled versions for these columns except for DATA and FLOAT_DATA when AsdmStMan is in use.
    1324           0 :         if(asdmStManUse==DONT ||
    1325           0 :            (colNamesTok[i] != MS::DATA && colNamesTok[i] != MS::FLOAT_DATA)){
    1326           0 :           String hcolName = String("Tiled") + MS::columnName(colNamesTok[i]);
    1327           0 :           td.defineHypercolumn(hcolName, 3,
    1328           0 :                                stringToVector(MS::columnName(colNamesTok[i])));
    1329           0 :           tiledDataNames[i] = hcolName;
    1330           0 :         }
    1331             :       }
    1332             :     }
    1333             : 
    1334             :     //other cols for compression
    1335           0 :     if (compress && asdmStManUse!=USE_FOR_DATA_WEIGHT_SIGMA_FLAG) {
    1336           0 :       MS::addColumnCompression(td, MS::WEIGHT, true);
    1337           0 :       MS::addColumnCompression(td, MS::SIGMA, true);
    1338             :     }
    1339             :     
    1340             :     // add this optional column because random group fits has a
    1341             :     // weight per visibility
    1342           0 :     MS::addColumnToDesc(td, MS::WEIGHT_SPECTRUM, 2);
    1343             : 
    1344             :     // if(asdmStManUse==DONT){
    1345             :     //     td.defineHypercolumn("TiledDATA",3,
    1346             :     //                           stringToVector(MS::columnName(MS::DATA)));
    1347             :     // }
    1348           0 :     td.defineHypercolumn("TiledFlagCategory",4,
    1349           0 :                          stringToVector(MS::columnName(MS::FLAG_CATEGORY)));
    1350           0 :     td.defineHypercolumn("TiledWgtSpectrum",3,
    1351           0 :                          stringToVector(MS::columnName(MS::WEIGHT_SPECTRUM)));
    1352           0 :     td.defineHypercolumn("TiledUVW",2,
    1353           0 :                          stringToVector(MS::columnName(MS::UVW)));
    1354           0 :     if(asdmStManUse!=USE_FOR_DATA_WEIGHT_SIGMA_FLAG){
    1355           0 :       td.defineHypercolumn("TiledFlag",3,
    1356           0 :                            stringToVector(MS::columnName(MS::FLAG)));
    1357           0 :       td.defineHypercolumn("TiledWgt",2,
    1358           0 :                            stringToVector(MS::columnName(MS::WEIGHT)));
    1359           0 :       td.defineHypercolumn("TiledSigma", 2,
    1360           0 :                            stringToVector(MS::columnName(MS::SIGMA)));
    1361             :     }
    1362             : 
    1363           0 :     SetupNewTable newtab(MSFileName, td, Table::New);
    1364             :     
    1365           0 :     uInt cache_val=32768;
    1366             :     // Set the default Storage Manager to be the Incr one
    1367           0 :     IncrementalStMan incrStMan ("ISMData",cache_val);
    1368           0 :     newtab.bindAll(incrStMan, true);
    1369             :     //Override the binding for specific columns
    1370             :     
    1371           0 :     IncrementalStMan incrStMan0("Array_ID",cache_val);
    1372           0 :     newtab.bindColumn(MS::columnName(MS::ARRAY_ID), incrStMan0);
    1373           0 :     IncrementalStMan incrStMan1("EXPOSURE",cache_val);
    1374           0 :     newtab.bindColumn(MS::columnName(MS::EXPOSURE), incrStMan1);
    1375           0 :     IncrementalStMan incrStMan2("FEED1",cache_val);
    1376           0 :     newtab.bindColumn(MS::columnName(MS::FEED1), incrStMan2);
    1377           0 :     IncrementalStMan incrStMan3("FEED2",cache_val);
    1378           0 :     newtab.bindColumn(MS::columnName(MS::FEED2), incrStMan3);
    1379           0 :     IncrementalStMan incrStMan4("FIELD_ID",cache_val);
    1380           0 :     newtab.bindColumn(MS::columnName(MS::FIELD_ID), incrStMan4);
    1381             : 
    1382             :     // jagonzal (CAS-6746): Don't use IncrementalStMan with RW cols ///////////////////////////////////////////////////
    1383             :     //IncrementalStMan incrStMan5("FLAG_ROW",cache_val/4);
    1384             :     //newtab.bindColumn(MS::columnName(MS::FLAG_ROW), incrStMan5);
    1385           0 :     StandardStMan aipsStManFlagRow("FLAG_ROW", cache_val/4);
    1386           0 :     newtab.bindColumn(MS::columnName(MS::FLAG_ROW), aipsStManFlagRow);
    1387             :     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1388             : 
    1389           0 :     IncrementalStMan incrStMan6("INTERVAL",cache_val);
    1390           0 :     newtab.bindColumn(MS::columnName(MS::INTERVAL), incrStMan6);
    1391           0 :     IncrementalStMan incrStMan7("OBSERVATION_ID",cache_val);
    1392           0 :     newtab.bindColumn(MS::columnName(MS::OBSERVATION_ID), incrStMan7);
    1393           0 :     IncrementalStMan incrStMan8("PROCESSOR_ID",cache_val);
    1394           0 :     newtab.bindColumn(MS::columnName(MS::PROCESSOR_ID), incrStMan8);
    1395           0 :     IncrementalStMan incrStMan9("SCAN_NUMBER",cache_val);
    1396           0 :     newtab.bindColumn(MS::columnName(MS::SCAN_NUMBER), incrStMan9);
    1397           0 :     IncrementalStMan incrStMan10("STATE_ID",cache_val);
    1398           0 :     newtab.bindColumn(MS::columnName(MS::STATE_ID), incrStMan10);
    1399           0 :     IncrementalStMan incrStMan11("TIME",cache_val);
    1400           0 :     newtab.bindColumn(MS::columnName(MS::TIME), incrStMan11);
    1401           0 :     IncrementalStMan incrStMan12("TIME_CENTROID",cache_val);
    1402           0 :     newtab.bindColumn(MS::columnName(MS::TIME_CENTROID), incrStMan12);
    1403             :     
    1404             :     // Bind ANTENNA1, ANTENNA2 and DATA_DESC_ID to the standardStMan 
    1405             :     // as they may change sufficiently frequently to make the
    1406             :     // incremental storage manager inefficient for these columns.
    1407             :     
    1408             :       
    1409           0 :     StandardStMan aipsStMan0("ANTENNA1", cache_val);
    1410           0 :     newtab.bindColumn(MS::columnName(MS::ANTENNA1), aipsStMan0);
    1411           0 :     StandardStMan aipsStMan1("ANTENNA2", cache_val);
    1412           0 :     newtab.bindColumn(MS::columnName(MS::ANTENNA2), aipsStMan1);
    1413           0 :     StandardStMan aipsStMan2("DATA_DESC_ID", cache_val);
    1414           0 :     newtab.bindColumn(MS::columnName(MS::DATA_DESC_ID), aipsStMan2);
    1415             :     
    1416             :      
    1417             :     //    itsLog << LogOrigin("MSFitsInput", "setupMeasurementSet");
    1418             :     //itsLog << LogIO::NORMAL << "Using tile shape "<<tileShape <<" for "<<
    1419             :     //  array_p<<" with obstype="<< obsType<<LogIO::POST;
    1420             :     
    1421             :     //    TiledShapeStMan tiledStMan1("TiledData",tileShape);
    1422           0 :     TiledShapeStMan tiledStMan1f("TiledFlag",tileShape);
    1423             :     TiledShapeStMan tiledStMan1fc("TiledFlagCategory",
    1424           0 :                                   IPosition(4,tileShape(0),tileShape(1),1,
    1425           0 :                                             tileShape(2)));
    1426           0 :     TiledShapeStMan tiledStMan2("TiledWgtSpectrum",tileShape);
    1427           0 :     TiledColumnStMan tiledStMan3("TiledUVW",IPosition(2, 3, (tileShape(0) * tileShape(1) * tileShape(2)) / 3));
    1428             :     TiledShapeStMan tiledStMan4("TiledWgt", 
    1429           0 :                                 IPosition(2,tileShape(0), tileShape(1) * tileShape(2)));
    1430             :     TiledShapeStMan tiledStMan5("TiledSigma", 
    1431           0 :                                 IPosition(2,tileShape(0), tileShape(1) * tileShape(2)));
    1432             :     
    1433             :     // Bind the DATA or FLOAT_DATA, FLAG & WEIGHT_SPECTRUM columns to the tiled stman or asdmStMan
    1434             : 
    1435           0 :     AsdmStMan sm;
    1436             : 
    1437           0 :     if (mustWriteOnlyToData){
    1438           0 :       if(asdmStManUse==DONT){
    1439           0 :         TiledShapeStMan tiledStMan1Data("TiledDATA",tileShape);
    1440             :       
    1441           0 :         newtab.bindColumn(MS::columnName(MS::DATA), tiledStMan1Data);
    1442           0 :       }
    1443             :       else{
    1444           0 :         if (hasFloatData) {
    1445           0 :           newtab.bindColumn(MS::columnName(MS::FLOAT_DATA), sm);
    1446             :         } else{
    1447           0 :           newtab.bindColumn(MS::columnName(MS::DATA), sm);
    1448             :         }
    1449             :       }
    1450             :     }
    1451             :     else{
    1452           0 :       for(uInt i = 0; i < ncols; ++i){
    1453           0 :         TiledShapeStMan tiledStMan1Data(tiledDataNames[i], tileShape);
    1454             :         
    1455           0 :         newtab.bindColumn(MS::columnName(colNamesTok[i]), tiledStMan1Data);
    1456           0 :       }
    1457           0 :       if(asdmStManUse!=DONT){
    1458           0 :         if (hasFloatData) {
    1459           0 :           newtab.bindColumn(MS::columnName(MS::FLOAT_DATA), sm);
    1460             :         } else {
    1461           0 :           newtab.bindColumn(MS::columnName(MS::DATA), sm);
    1462             :         }
    1463             :       }
    1464             :     }
    1465           0 :     newtab.bindColumn(MS::columnName(MS::FLAG_CATEGORY),tiledStMan1fc);
    1466           0 :     newtab.bindColumn(MS::columnName(MS::WEIGHT_SPECTRUM),tiledStMan2);
    1467             :     
    1468           0 :     newtab.bindColumn(MS::columnName(MS::UVW),tiledStMan3);
    1469           0 :     if(asdmStManUse==USE_FOR_DATA_WEIGHT_SIGMA_FLAG){
    1470           0 :       newtab.bindColumn(MS::columnName(MS::FLAG),sm);
    1471           0 :       newtab.bindColumn(MS::columnName(MS::WEIGHT),sm);
    1472           0 :       newtab.bindColumn(MS::columnName(MS::SIGMA),sm);      
    1473             :     }
    1474             :     else{
    1475           0 :       newtab.bindColumn(MS::columnName(MS::FLAG),tiledStMan1f);
    1476           0 :       newtab.bindColumn(MS::columnName(MS::WEIGHT),tiledStMan4);
    1477           0 :       newtab.bindColumn(MS::columnName(MS::SIGMA),tiledStMan5);
    1478             :     }
    1479             :       
    1480             : 
    1481             :     // avoid lock overheads by locking the table permanently
    1482           0 :     TableLock lock(TableLock::AutoLocking);
    1483           0 :     MeasurementSet *ms = new MeasurementSet (newtab,lock);
    1484             :     
    1485             :     // Set up the subtables for the UVFITS MS
    1486             :     // we make new tables with 0 rows
    1487           0 :     Table::TableOption option=Table::New;
    1488           0 :     createSubtables(*ms, option); 
    1489             : 
    1490             :     { // Set the TableInfo
    1491           0 :       TableInfo& info(ms->tableInfo());
    1492           0 :       info.setType(TableInfo::type(TableInfo::MEASUREMENTSET));
    1493           0 :       info.setSubType(String("UVFITS"));
    1494             :       info.readmeAddLine
    1495           0 :         ("This is a measurement set Table holding astronomical observations");
    1496             :     }
    1497           0 :     return ms;
    1498           0 :   }
    1499             :   
    1500             :   
    1501           0 :   Bool SubMS::fillDDTables()
    1502             :   {  
    1503           0 :     LogIO os(LogOrigin("SubMS", "fillDDTables()"));
    1504             :     
    1505           0 :     MSSpWindowColumns inSpWCols(mssel_p.spectralWindow());
    1506           0 :     MSSpWindowColumns& msSpW(msc_p->spectralWindow());
    1507             :     // Detect which optional columns of SPECTRAL_WINDOW are present.
    1508             :     // inSpWCols and msSpW should agree because addOptionalColumns() was done
    1509             :     // for SPECTRAL_WINDOW in fillAllTables() before making msc_p or calling
    1510             :     // fillDDTables.
    1511           0 :     Bool haveSpwAN  = inSpWCols.assocNature().hasContent();
    1512           0 :     Bool haveSpwASI = inSpWCols.assocSpwId().hasContent();
    1513           0 :     Bool haveSpwBN  = inSpWCols.bbcNo().hasContent();
    1514           0 :     Bool haveSpwBS  = inSpWCols.bbcSideband().hasContent();
    1515           0 :     Bool haveSpwDI  = inSpWCols.dopplerId().hasContent();
    1516             : 
    1517           0 :     MSDataDescColumns& msDD(msc_p->dataDescription());
    1518           0 :     MSPolarizationColumns& msPol(msc_p->polarization());
    1519             :     
    1520             :     //DD table
    1521           0 :     const MSDataDescription ddtable = mssel_p.dataDescription();
    1522             :     ScalarColumn<Int> polId(ddtable, 
    1523           0 :                               MSDataDescription::columnName(MSDataDescription::POLARIZATION_ID));
    1524             :     
    1525             :     //Fill in matching spw to datadesc in old ms 
    1526             :     {
    1527           0 :       MSDataDescColumns msOldDD(ddtable);
    1528           0 :       oldDDSpwMatch_p=msOldDD.spectralWindowId().getColumn();
    1529           0 :     }
    1530             : 
    1531             :     //POLARIZATION table    
    1532           0 :     const MSPolarization poltable= mssel_p.polarization();
    1533             :     ScalarColumn<Int> numCorr (poltable, 
    1534           0 :                                  MSPolarization::columnName(MSPolarization::NUM_CORR));
    1535             :     ArrayColumn<Int> corrType(poltable, 
    1536           0 :                                 MSPolarization::columnName(MSPolarization::CORR_TYPE));
    1537           0 :     ArrayColumn<Int> corrProd(poltable, MSPolarization::columnName(MSPolarization::CORR_PRODUCT));
    1538           0 :     ScalarColumn<Bool> polFlagRow(poltable, MSPolarization::columnName(MSPolarization::FLAG_ROW));
    1539             :     
    1540           0 :     spwRelabel_p.resize(mscIn_p->spectralWindow().nrow());
    1541           0 :     spwRelabel_p.set(-1);
    1542             :     
    1543           0 :     inNumChan_p.resize(spw_p.nelements()); 
    1544             :     
    1545           0 :     polID_p = polId.getColumn();
    1546             :     Bool dum;
    1547           0 :     Sort sort( polID_p.getStorage(dum),sizeof(Int) );
    1548           0 :     sort.sortKey((uInt)0,TpInt);
    1549           0 :     Vector<uInt> index,uniq;
    1550           0 :     sort.sort(index,polID_p.nelements());
    1551           0 :     uInt nPol = sort.unique(uniq,index);
    1552           0 :     Vector<Int> selectedPolId(nPol);      // Map from output polID to input polID.
    1553           0 :     for(uInt k = 0; k < nPol; ++k)
    1554           0 :       selectedPolId[k] = polID_p[index[uniq[k]]];
    1555             :     
    1556             :     // Make map from input to output spws.
    1557           0 :     Sort sortSpws(spw_p.getStorage(dum), sizeof(Int));
    1558           0 :     sortSpws.sortKey((uInt)0, TpInt);
    1559           0 :     Vector<uInt> spwsortindex, spwuniqinds;
    1560           0 :     sortSpws.sort(spwsortindex, spw_p.nelements());
    1561           0 :     uInt nuniqSpws = sortSpws.unique(spwuniqinds, spwsortindex);
    1562           0 :     spw_uniq_p.resize(nuniqSpws);
    1563           0 :     for(uInt k = 0; k < nuniqSpws; ++k){
    1564           0 :       spw_uniq_p[k] = spw_p[spwuniqinds[k]];
    1565           0 :       spwRelabel_p[spw_uniq_p[k]] = k;
    1566             :     }
    1567           0 :     if(nuniqSpws < spw_p.nelements()){
    1568             :       os << LogIO::WARN
    1569             :          << "Multiple channel ranges within an spw may not work.  SOME DATA MAY BE OMITTED!"
    1570             :          << "\nConsider splitting them individually and optionally combining the output MSes with concat."
    1571             :          << "\nEven then, expect problems if exporting to uvfits."
    1572           0 :          << LogIO::POST;
    1573             :     }
    1574             : 
    1575           0 :     Vector<Int> newPolId(nuniqSpws);
    1576           0 :     for(uInt k = 0; k < nuniqSpws; ++k){
    1577           0 :       Bool found = false;
    1578             :       
    1579           0 :       for(uInt j = 0; j < nPol; ++j){ 
    1580           0 :         if(selectedPolId[j] == polID_p[spw2ddid_p[spw_uniq_p[k]]]){
    1581           0 :           newPolId[k] = j;
    1582           0 :           found = true;
    1583           0 :           break;
    1584             :         }
    1585             :       }
    1586           0 :       if(!found){
    1587             :         os << LogIO::SEVERE
    1588             :            << "No polarization ID found for output polarization setup " << k
    1589           0 :            << LogIO::POST;
    1590           0 :         return false;
    1591             :       }
    1592             :     }
    1593           0 :     corrSlice_p.resize(nPol);
    1594           0 :     for(uInt outpid = 0; outpid < nPol; ++outpid){
    1595           0 :       uInt inpid = selectedPolId[outpid];
    1596           0 :       uInt ncorr = inPolOutCorrToInCorrMap_p[inpid].nelements();
    1597           0 :       const Vector<Int> inCT(corrType(inpid));
    1598             :       
    1599             :       // ncorr will be 0 if none of the selected spws have this pid. 
    1600           0 :       if(ncorr > 0 && ncorr < inCT.nelements()){
    1601           0 :         keepShape_p = false;
    1602             : 
    1603             :         // Check whether the requested correlations can be accessed by slicing.
    1604             :         // That means there must be a constant stride.  The most likely (only?)
    1605             :         // way to violate that is to ask for 3 out of 4 correlations.
    1606           0 :         if(ncorr > 2){
    1607             :           os << LogIO::SEVERE
    1608             :              << "Sorry, the requested correlation selection is not unsupported.\n"
    1609             :              << "Try selecting fewer or all of the correlations."
    1610           0 :              << LogIO::POST;
    1611           0 :           return false;
    1612             :         }
    1613             : 
    1614           0 :         corrSlice_p[outpid] = Slice(inPolOutCorrToInCorrMap_p[inpid][0],
    1615             :                                ncorr,
    1616           0 :                                ncorr > 1 ? inPolOutCorrToInCorrMap_p[inpid][1] -
    1617           0 :                                            inPolOutCorrToInCorrMap_p[inpid][0] :
    1618             :                                1);
    1619             :       }
    1620             :       else
    1621           0 :         corrSlice_p[outpid] = Slice(0, ncorr);
    1622             :       
    1623           0 :       msOut_p.polarization().addRow();
    1624           0 :       msPol.numCorr().put(outpid, ncorr);
    1625           0 :       msPol.flagRow().put(outpid, polFlagRow(inpid));
    1626             : 
    1627           0 :       Vector<Int> outCT;
    1628           0 :       const Matrix<Int> inCP(corrProd(inpid));
    1629           0 :       Matrix<Int> outCP;
    1630           0 :       outCT.resize(ncorr);
    1631           0 :       outCP.resize(2, ncorr);
    1632           0 :       for(uInt k = 0; k < ncorr; ++k){
    1633           0 :         Int inCorrInd = inPolOutCorrToInCorrMap_p[inpid][k];
    1634             :         
    1635           0 :         outCT[k] = inCT[inCorrInd];
    1636           0 :         for(uInt feedind = 0; feedind < 2; ++feedind)
    1637           0 :           outCP(feedind, k) = inCP(feedind, inCorrInd);
    1638             :       }
    1639           0 :       msPol.corrType().put(outpid, outCT);
    1640           0 :       msPol.corrProduct().put(outpid, outCP);
    1641           0 :     }
    1642             : 
    1643           0 :     for(uInt k = 0; k < spw_p.nelements(); ++k)
    1644           0 :       inNumChan_p[k] = inSpWCols.numChan()(spw_p[k]);
    1645             :     
    1646           0 :     Vector<Vector<Int> > spwinds_of_uniq_spws(nuniqSpws);
    1647             : 
    1648           0 :     totnchan_p.resize(nuniqSpws);
    1649           0 :     for(uInt k = 0; k < nuniqSpws; ++k){
    1650           0 :       Int maxchan = 0;
    1651           0 :       uInt j = 0;
    1652             : 
    1653           0 :       msOut_p.spectralWindow().addRow();
    1654           0 :       msOut_p.dataDescription().addRow();
    1655             : 
    1656           0 :       totnchan_p[k] = 0;
    1657           0 :       spwinds_of_uniq_spws[k].resize();
    1658           0 :       for(uInt spwind = 0; spwind < spw_p.nelements(); ++spwind){
    1659           0 :         if(spw_p[spwind] == spw_uniq_p[k]){
    1660           0 :           Int highchan = nchan_p[spwind] * chanStep_p[spwind]
    1661           0 :             + chanStart_p[spwind];
    1662             : 
    1663           0 :           if(highchan > maxchan)
    1664           0 :             maxchan = highchan;
    1665             : 
    1666           0 :           totnchan_p[k] += nchan_p[spwind];
    1667             : 
    1668             :           // The true is necessary to avoid scrambling previously assigned
    1669             :           // values.
    1670           0 :           spwinds_of_uniq_spws[k].resize(j + 1, true);
    1671             : 
    1672             :           // Warning!  spwinds_of_uniq_spws[k][j] will compile without warning,
    1673             :           // but dump core at runtime.
    1674           0 :           (spwinds_of_uniq_spws[k])[j] = spwind;
    1675           0 :           ++j;
    1676             :         }
    1677             :       }
    1678           0 :       if(maxchan > inSpWCols.numChan()(spw_uniq_p[k])){
    1679             :         os << LogIO::SEVERE
    1680             :            << " Channel settings wrong; exceeding number of channels in spw "
    1681           0 :            << spw_uniq_p[k] << LogIO::POST;
    1682           0 :         return false;
    1683             :       }
    1684             :     }
    1685             : 
    1686             :     // min_k is an index for getting an spw index via spw_uniq_p[min_k].
    1687             :     // k is an index for getting an spw index via spw_p[k].
    1688           0 :     for(uInt min_k = 0; min_k < nuniqSpws; ++min_k){
    1689           0 :       uInt k = spwinds_of_uniq_spws[min_k][0];
    1690             : 
    1691           0 :       if(spwinds_of_uniq_spws[min_k].nelements() > 1 ||
    1692           0 :          nchan_p[k] != inSpWCols.numChan()(spw_p[k])){
    1693           0 :         Vector<Double> effBWIn = inSpWCols.effectiveBW()(spw_uniq_p[min_k]);
    1694           0 :         Int nOutChan = totnchan_p[min_k];
    1695           0 :         Vector<Double> chanFreqOut(nOutChan);
    1696           0 :         Vector<Double> chanFreqIn = inSpWCols.chanFreq()(spw_uniq_p[min_k]);
    1697           0 :         Vector<Double> chanWidthOut(nOutChan);
    1698           0 :         Vector<Double> chanWidthIn = inSpWCols.chanWidth()(spw_uniq_p[min_k]);
    1699           0 :         Vector<Double> spwResolOut(nOutChan);
    1700           0 :         Vector<Double> spwResolIn = inSpWCols.resolution()(spw_uniq_p[min_k]);
    1701           0 :         Vector<Double> effBWOut(nOutChan);
    1702           0 :         Int outChan = 0;
    1703             : 
    1704           0 :         keepShape_p = false;
    1705             : 
    1706             :         // The sign of CHAN_WIDTH defaults to +.  Its determination assumes
    1707             :         // that chanFreqIn is monotonic, but not that the sign of the
    1708             :         // chanWidthIn is correct.
    1709           0 :         Bool neginc = chanFreqIn[chanFreqIn.nelements() - 1] < chanFreqIn[0];
    1710             : 
    1711           0 :         effBWOut.set(0.0);
    1712           0 :         Double totalBW = 0.0;
    1713           0 :         for(uInt rangeNum = 0;
    1714           0 :             rangeNum < spwinds_of_uniq_spws[min_k].nelements(); ++rangeNum){
    1715           0 :           k = spwinds_of_uniq_spws[min_k][rangeNum];
    1716             : 
    1717           0 :           Int span = chanStep_p[k] * widths_p[k];
    1718             : 
    1719           0 :           for(Int j = 0; j < nchan_p[k]; ++j){
    1720           0 :             Int inpChan = chanStart_p[k] + j * span;
    1721             : 
    1722           0 :             if(span > 1){
    1723           0 :               Int lastChan = inpChan + span - 1;
    1724             : 
    1725           0 :               if(lastChan > chanEnd_p[k]){
    1726             :                 // The averaging width is not a factor of the number of
    1727             :                 // selected input channels, so the last output bin receives
    1728             :                 // fewer input channels than the other bins.
    1729           0 :                 lastChan = chanEnd_p[k];
    1730             : 
    1731           0 :                 Int nchan = lastChan - inpChan + 1;
    1732             :                 os << LogIO::NORMAL
    1733           0 :                    << "The last output channel of spw " << spw_p[k]
    1734           0 :                    << " has only " << nchan << " input channel";
    1735           0 :                 if(nchan > 1)
    1736           0 :                   os << "s.";
    1737             :                 //else
    1738             :                 //  os << ".\nRemember that MS selection ranges (unlike Python), *include* the last number.";
    1739           0 :                 os << LogIO::POST;
    1740             :                 //os << LogIO::WARN
    1741             :                 //   << "You will not be able to export an MS where the width varies by channel to UVFITS!"
    1742             :                 //   << LogIO::POST;
    1743             :               }
    1744             : 
    1745           0 :               chanFreqOut[outChan] = (chanFreqIn[inpChan] +
    1746           0 :                                       chanFreqIn[lastChan]) / 2;
    1747             : 
    1748           0 :               Double sep = chanFreqIn[lastChan] - chanFreqIn[inpChan];
    1749             : 
    1750           0 :               if(neginc)
    1751           0 :                 sep = -sep;
    1752             : 
    1753             :               // The internal abs is necessary because the sign of chanWidthIn
    1754             :               // may be wrong.
    1755           0 :               chanWidthOut[outChan] = sep + 0.5 * abs(chanWidthIn[inpChan] +
    1756           0 :                                                       chanWidthIn[lastChan]);
    1757           0 :               if(neginc)
    1758           0 :                 chanWidthOut[outChan] = -chanWidthOut[outChan];
    1759             :             
    1760           0 :               spwResolOut[outChan] = 0.5 * (spwResolIn[inpChan] +
    1761           0 :                                             spwResolIn[lastChan])
    1762           0 :                                      + sep;
    1763             : 
    1764           0 :               for(Int avgChan = inpChan; avgChan <= lastChan;
    1765           0 :                   avgChan += chanStep_p[k])
    1766           0 :                 effBWOut[outChan] += effBWIn[avgChan];
    1767             :             }
    1768             :             else{
    1769           0 :               chanFreqOut[outChan] = chanFreqIn[inpChan];
    1770           0 :               spwResolOut[outChan] = spwResolIn[inpChan];
    1771           0 :               chanWidthOut[outChan] = chanWidthIn[inpChan];
    1772           0 :               effBWOut[outChan]    = effBWIn[inpChan];
    1773             :             }
    1774           0 :             totalBW += effBWOut[outChan];
    1775           0 :             ++outChan;
    1776             :           }
    1777             :         }
    1778           0 :         --outChan;
    1779             : 
    1780           0 :         msSpW.chanFreq().put(min_k, chanFreqOut);
    1781           0 :         msSpW.refFrequency().put(min_k, min(chanFreqOut[0],chanFreqOut[chanFreqOut.size()-1]));
    1782           0 :         msSpW.resolution().put(min_k, spwResolOut);
    1783           0 :         msSpW.numChan().put(min_k, nOutChan);
    1784           0 :         msSpW.chanWidth().put(min_k, chanWidthOut);
    1785           0 :         msSpW.effectiveBW().put(min_k, spwResolOut);
    1786           0 :         msSpW.totalBandwidth().put(min_k, totalBW);
    1787           0 :       }
    1788             :       else{
    1789           0 :         msSpW.chanFreq().put(min_k, inSpWCols.chanFreq()(spw_p[k]));
    1790           0 :         msSpW.refFrequency().put(min_k, inSpWCols.refFrequency()(spw_p[k]));
    1791           0 :         msSpW.resolution().put(min_k, inSpWCols.resolution()(spw_p[k]));
    1792           0 :         msSpW.numChan().put(min_k, inSpWCols.numChan()(spw_p[k]));    
    1793           0 :         msSpW.chanWidth().put(min_k, inSpWCols.chanWidth()(spw_p[k]));
    1794           0 :         msSpW.effectiveBW().put(min_k, inSpWCols.effectiveBW()(spw_p[k]));
    1795           0 :         msSpW.totalBandwidth().put(min_k, inSpWCols.totalBandwidth()(spw_p[k]));
    1796             :       }
    1797             :       
    1798           0 :       msSpW.flagRow().put(min_k, inSpWCols.flagRow()(spw_p[k]));
    1799           0 :       msSpW.freqGroup().put(min_k, inSpWCols.freqGroup()(spw_p[k]));
    1800           0 :       msSpW.freqGroupName().put(min_k, inSpWCols.freqGroupName()(spw_p[k]));
    1801           0 :       msSpW.ifConvChain().put(min_k, inSpWCols.ifConvChain()(spw_p[k]));
    1802           0 :       msSpW.measFreqRef().put(min_k, inSpWCols.measFreqRef()(spw_p[k]));
    1803           0 :       msSpW.name().put(min_k, inSpWCols.name()(spw_p[k]));
    1804           0 :       msSpW.netSideband().put(min_k, inSpWCols.netSideband()(spw_p[k]));
    1805           0 :       if(haveSpwAN)
    1806           0 :         msSpW.assocNature().put(min_k, inSpWCols.assocNature()(spw_p[k]));
    1807           0 :       if(haveSpwASI)
    1808           0 :         msSpW.assocSpwId().put(min_k, inSpWCols.assocSpwId()(spw_p[k]));
    1809           0 :       if(haveSpwBN)
    1810           0 :         msSpW.bbcNo().put(min_k, inSpWCols.bbcNo()(spw_p[k]));
    1811           0 :       if(haveSpwBS)
    1812           0 :         msSpW.bbcSideband().put(min_k, inSpWCols.bbcSideband()(spw_p[k]));
    1813           0 :       if(haveSpwDI)
    1814           0 :         msSpW.dopplerId().put(min_k, inSpWCols.dopplerId()(spw_p[k]));
    1815             : 
    1816           0 :       msDD.flagRow().put(min_k, false);
    1817           0 :       msDD.polarizationId().put(min_k, newPolId[min_k]);
    1818           0 :       msDD.spectralWindowId().put(min_k, min_k);
    1819             :     }
    1820           0 :     return true;
    1821           0 :   }
    1822             :   
    1823           0 :   Bool SubMS::fillFieldTable() 
    1824             :   {  
    1825           0 :     LogIO os(LogOrigin("SubMS", "fillFieldTable()"));
    1826             :     
    1827             :     //MSField fieldtable= mssel_p.field();
    1828             :     // optionalCols[0] = "EPHEMERIS_ID";
    1829           0 :     uInt nAddedCols = addOptionalColumns(mssel_p.field(), msOut_p.field(),
    1830             :                                          true);
    1831             : 
    1832           0 :     MSFieldColumns msField(msOut_p.field());
    1833             : 
    1834           0 :     const MSFieldColumns& fieldIn = mscIn_p->field(); 
    1835           0 :     ScalarColumn<String> code(fieldIn.code());
    1836           0 :     ArrayColumn<Double>  delayDir(fieldIn.delayDir());
    1837           0 :     ScalarColumn<Bool>   flagRow(fieldIn.flagRow());
    1838           0 :     ScalarColumn<String> name(fieldIn.name());
    1839           0 :     ScalarColumn<Int>    numPoly(fieldIn.numPoly());
    1840           0 :     ArrayColumn<Double>  phaseDir(fieldIn.phaseDir());
    1841           0 :     ArrayColumn<Double>  refDir(fieldIn.referenceDir());
    1842           0 :     ScalarColumn<Int>    sourceId(fieldIn.sourceId());
    1843           0 :     ScalarColumn<Double> time(fieldIn.time());
    1844             :     
    1845           0 :     String refstr;
    1846           0 :     String nameVarRefColDelayDir, nameVarRefColPhaseDir, nameVarRefColRefDir;
    1847             : 
    1848             :     // Need to correctly define the direction measures.
    1849             :     // DelayDir
    1850           0 :     if(delayDir.keywordSet().asRecord("MEASINFO").isDefined("Ref")){
    1851           0 :       delayDir.keywordSet().asRecord("MEASINFO").get("Ref", refstr);
    1852             :       //  MDirection::getType(dir1, refstr);
    1853           0 :       msField.delayDir().rwKeywordSet().asrwRecord("MEASINFO").define("Ref",refstr);
    1854             :     }
    1855           0 :     if(delayDir.keywordSet().asRecord("MEASINFO").isDefined("VarRefCol")){ // it's a variable ref. column
    1856           0 :       delayDir.keywordSet().asRecord("MEASINFO").get("VarRefCol", refstr);
    1857           0 :       msField.delayDir().rwKeywordSet().asrwRecord("MEASINFO").define("VarRefCol",refstr);
    1858           0 :       nameVarRefColDelayDir = refstr;
    1859           0 :       Vector<String> refTypeV;
    1860           0 :       delayDir.keywordSet().asRecord("MEASINFO").get("TabRefTypes", refTypeV);
    1861           0 :       msField.delayDir().rwKeywordSet().asrwRecord("MEASINFO").define("TabRefTypes",refTypeV);      
    1862           0 :       Vector<uInt> refCodeV;
    1863           0 :       delayDir.keywordSet().asRecord("MEASINFO").get("TabRefCodes", refCodeV);
    1864           0 :       msField.delayDir().rwKeywordSet().asrwRecord("MEASINFO").define("TabRefCodes",refCodeV);      
    1865           0 :       Int refid=msField.delayDir().keywordSet().asRecord("MEASINFO").fieldNumber("Ref");
    1866           0 :       if(refid>=0){ // erase the redundant Ref keyword
    1867           0 :         msField.delayDir().rwKeywordSet().asrwRecord("MEASINFO").removeField(RecordFieldId(refid));
    1868             :       }
    1869           0 :     }
    1870             :     // PhaseDir
    1871           0 :     if(phaseDir.keywordSet().asRecord("MEASINFO").isDefined("Ref")){
    1872           0 :       phaseDir.keywordSet().asRecord("MEASINFO").get("Ref", refstr);
    1873           0 :       msField.phaseDir().rwKeywordSet().asrwRecord("MEASINFO").define("Ref", refstr);
    1874             :     }
    1875           0 :     if(phaseDir.keywordSet().asRecord("MEASINFO").isDefined("VarRefCol")){ // it's a variable ref. column
    1876           0 :       phaseDir.keywordSet().asRecord("MEASINFO").get("VarRefCol", refstr);
    1877           0 :       msField.phaseDir().rwKeywordSet().asrwRecord("MEASINFO").define("VarRefCol",refstr);
    1878           0 :       nameVarRefColPhaseDir = refstr;
    1879           0 :       Vector<String> refTypeV;
    1880           0 :       phaseDir.keywordSet().asRecord("MEASINFO").get("TabRefTypes", refTypeV);
    1881           0 :       msField.phaseDir().rwKeywordSet().asrwRecord("MEASINFO").define("TabRefTypes",refTypeV);      
    1882           0 :       Vector<uInt> refCodeV;
    1883           0 :       phaseDir.keywordSet().asRecord("MEASINFO").get("TabRefCodes", refCodeV);
    1884           0 :       msField.phaseDir().rwKeywordSet().asrwRecord("MEASINFO").define("TabRefCodes",refCodeV);      
    1885           0 :       Int refid=msField.phaseDir().keywordSet().asRecord("MEASINFO").fieldNumber("Ref");
    1886           0 :       if(refid>=0){
    1887           0 :         msField.phaseDir().rwKeywordSet().asrwRecord("MEASINFO").removeField(RecordFieldId(refid));
    1888             :       }
    1889           0 :     }
    1890             :     // ReferenceDir
    1891           0 :     if(refDir.keywordSet().asRecord("MEASINFO").isDefined("Ref")){
    1892           0 :       refDir.keywordSet().asRecord("MEASINFO").get("Ref", refstr);
    1893           0 :       msField.referenceDir().rwKeywordSet().asrwRecord("MEASINFO").define("Ref",refstr);
    1894             :     }
    1895           0 :     if(refDir.keywordSet().asRecord("MEASINFO").isDefined("VarRefCol")){ // it's a variable ref. column
    1896           0 :       refDir.keywordSet().asRecord("MEASINFO").get("VarRefCol", refstr);
    1897           0 :       msField.referenceDir().rwKeywordSet().asrwRecord("MEASINFO").define("VarRefCol",refstr);
    1898           0 :       nameVarRefColRefDir = refstr;
    1899           0 :       Vector<String> refTypeV;
    1900           0 :       refDir.keywordSet().asRecord("MEASINFO").get("TabRefTypes", refTypeV);
    1901           0 :       msField.referenceDir().rwKeywordSet().asrwRecord("MEASINFO").define("TabRefTypes",refTypeV);      
    1902           0 :       Vector<uInt> refCodeV;
    1903           0 :       refDir.keywordSet().asRecord("MEASINFO").get("TabRefCodes", refCodeV);
    1904           0 :       msField.referenceDir().rwKeywordSet().asrwRecord("MEASINFO").define("TabRefCodes",refCodeV);
    1905           0 :       Int refid=msField.referenceDir().keywordSet().asRecord("MEASINFO").fieldNumber("Ref");
    1906           0 :       if(refid>=0){
    1907           0 :         msField.referenceDir().rwKeywordSet().asrwRecord("MEASINFO").removeField(RecordFieldId(refid));
    1908             :       }
    1909           0 :     }
    1910             : 
    1911             :     // ...and the time measure...
    1912           0 :     time.keywordSet().asRecord("MEASINFO").get("Ref", refstr);
    1913           0 :     msField.time().rwKeywordSet().asrwRecord("MEASINFO").define("Ref", refstr);
    1914             : 
    1915             :     // fieldRelabel_p size: nrow of a input MS, -1 for unselected field ids 
    1916           0 :     fieldRelabel_p.resize(mscIn_p->field().nrow());
    1917           0 :     fieldRelabel_p.set(-1);
    1918             : 
    1919             :     os << LogIO::DEBUG1
    1920             :        << fieldid_p.nelements() << " fields selected out of "
    1921           0 :        << mscIn_p->field().nrow()
    1922           0 :        << LogIO::POST;
    1923             : 
    1924             :     try{
    1925           0 :       msOut_p.field().addRow(fieldid_p.nelements());
    1926           0 :       for(uInt k = 0; k < fieldid_p.nelements(); ++k){
    1927           0 :         fieldRelabel_p[fieldid_p[k]] = k;
    1928             :       
    1929           0 :         msField.code().put(k, code(fieldid_p[k]));
    1930           0 :         msField.delayDir().put(k, delayDir(fieldid_p[k]));
    1931           0 :         msField.flagRow().put(k, flagRow(fieldid_p[k]));
    1932           0 :         msField.name().put(k, name(fieldid_p[k]));
    1933           0 :         msField.numPoly().put(k, numPoly(fieldid_p[k]));
    1934           0 :         msField.phaseDir().put(k, phaseDir(fieldid_p[k]));
    1935           0 :         msField.referenceDir().put(k, refDir(fieldid_p[k]));
    1936           0 :         msField.time().put(k, time(fieldid_p[k]));
    1937             : 
    1938           0 :         Int inSrcID = sourceId(fieldid_p[k]);
    1939           0 :         if(inSrcID < 0)
    1940           0 :           msField.sourceId().put(k, -1);
    1941             :         else
    1942           0 :           msField.sourceId().put(k, sourceRelabel_p[inSrcID]);
    1943             :       }
    1944             : 
    1945           0 :       if(nAddedCols > 0){
    1946             : 
    1947           0 :         ScalarColumn<Int> eID(fieldIn.ephemerisId());
    1948           0 :         if(eID.hasContent()){
    1949           0 :           String destPathName = Path(msOut_p.field().tableName()).absoluteName();
    1950           0 :           for(uInt k = 0; k < fieldid_p.nelements(); ++k){
    1951             : 
    1952           0 :             Int theEphId = eID(fieldid_p[k]);
    1953             : 
    1954           0 :             if(theEphId>-1){ // there is an ephemeris attached to this field
    1955           0 :               Path ephPath = Path(fieldIn.ephemPath(fieldid_p[k]));
    1956           0 :               if(ephPath.length()>0){ // copy the ephemeris table over to the output FIELD table
    1957           0 :                 Directory origEphemDir(ephPath);
    1958           0 :                 origEphemDir.copy(destPathName+"/"+ephPath.baseName());
    1959           0 :                 os << LogIO::NORMAL << "Transferring ephemeris " << ephPath.baseName() 
    1960           0 :                    << " for output field " << name(fieldid_p[k]) << LogIO::POST;
    1961           0 :               }
    1962           0 :             } 
    1963           0 :             msField.ephemerisId().put(k, theEphId);
    1964             :           }
    1965           0 :         }
    1966             : 
    1967           0 :         if(!nameVarRefColDelayDir.empty()){ // need to copy the reference column
    1968           0 :           ScalarColumn<Int>  dM(mssel_p.field(), nameVarRefColDelayDir);
    1969           0 :           ScalarColumn<Int> cdMDirRef(msOut_p.field(), nameVarRefColDelayDir);
    1970           0 :           for(uInt k = 0; k < fieldid_p.nelements(); ++k){
    1971           0 :             cdMDirRef.put(k, dM(fieldid_p[k]));
    1972             :           }
    1973           0 :         }
    1974           0 :         if(!nameVarRefColPhaseDir.empty()){ // need to copy the reference column
    1975           0 :           ScalarColumn<Int>  dM(mssel_p.field(), nameVarRefColPhaseDir);
    1976           0 :           ScalarColumn<Int> cdMDirRef(msOut_p.field(), nameVarRefColPhaseDir);
    1977           0 :           for(uInt k = 0; k < fieldid_p.nelements(); ++k){
    1978           0 :             cdMDirRef.put(k, dM(fieldid_p[k]));
    1979             :           }
    1980           0 :         }
    1981           0 :         if(!nameVarRefColRefDir.empty()){ // need to copy the reference column
    1982           0 :           ScalarColumn<Int>  dM(mssel_p.field(), nameVarRefColRefDir);
    1983           0 :           ScalarColumn<Int> cdMDirRef(msOut_p.field(), nameVarRefColRefDir);
    1984           0 :           for(uInt k = 0; k < fieldid_p.nelements(); ++k){
    1985           0 :             cdMDirRef.put(k, dM(fieldid_p[k]));
    1986             :           }
    1987           0 :         }
    1988           0 :       }
    1989             : 
    1990             :     }
    1991           0 :     catch(AipsError x){
    1992             :       os << LogIO::EXCEPTION
    1993             :          << "Error " << x.getMesg() << " setting up the output FIELD table."
    1994           0 :          << LogIO::POST;
    1995           0 :     }
    1996           0 :     catch(...){
    1997           0 :       throw(AipsError("Unknown exception caught and released in fillFieldTable()"));
    1998           0 :     }
    1999             :     
    2000           0 :     return true;    
    2001           0 :   }
    2002             : 
    2003             :   // Sets up sourceRelabel_p for mapping input SourceIDs (if any) to output
    2004             :   // ones.  Must be called after fieldid_p is set and before calling
    2005             :   // fillFieldTable() or copySource().
    2006           0 :   void SubMS::relabelSources()
    2007             :   {
    2008           0 :     LogIO os(LogOrigin("SubMS", "relabelSources()"));
    2009             : 
    2010             :     //Source is an optional table, so it may not exist
    2011           0 :     if(Table::isReadable(mssel_p.sourceTableName())){
    2012             :       // Note that mscIn_p->field().sourceId() has ALL of the sourceIDs in
    2013             :       // the input MS, not just the selected ones.
    2014           0 :       const Vector<Int>& inSrcIDs = mscIn_p->field().sourceId().getColumn();
    2015             : 
    2016           0 :       Int highestInpSrc = max(inSrcIDs);
    2017             :     
    2018           0 :       if(highestInpSrc < 0)                   // Ensure space for -1.
    2019           0 :         highestInpSrc = 0;
    2020           0 :       sourceRelabel_p.resize(highestInpSrc + 1);
    2021           0 :       sourceRelabel_p.set(-1);                    // Default to "any".
    2022             : 
    2023             :       // Enable sourceIDs that are actually referred to by selected fields, and
    2024             :       // remap them using j.
    2025           0 :       uInt j = 0;
    2026           0 :       for(uInt k = 0; k < fieldid_p.nelements(); ++k){
    2027           0 :         Int fldInSrcID = inSrcIDs[fieldid_p[k]];
    2028             :         
    2029           0 :         if(fldInSrcID > -1){
    2030           0 :           if(sourceRelabel_p[fldInSrcID] == -1){ // Multiple fields can use the same
    2031           0 :             sourceRelabel_p[fldInSrcID] = j;     // source in a mosaic.
    2032           0 :             ++j;
    2033             :           }
    2034             :         }
    2035             :       }
    2036           0 :     }
    2037             :     else{
    2038             :       os << LogIO::NORMAL
    2039             :          << "The input MS does not have the optional SOURCE table.\n"
    2040             :          << "-1 will be used as a generic source ID."
    2041           0 :          << LogIO::POST;
    2042           0 :       sourceRelabel_p.resize(1);
    2043           0 :       sourceRelabel_p.set(-1);                    // Default to "any".      
    2044             :     }
    2045           0 :   }
    2046             : 
    2047             :   // This method is currently not called in SubMS.  It should really be called
    2048             :   // in setupMS, but that has been made into a static method and it cannot be
    2049             :   // called there.  The ms argument is unused, but it is there to preserve the
    2050             :   // signature, and is commented to prevent a compiler warning.
    2051             :   //
    2052           0 :   void SubMS::verifyColumns(const MeasurementSet&, // ms,
    2053             :                             const Vector<MS::PredefinedColumns>& colNames)
    2054             :   {
    2055           0 :     LogIO os(LogOrigin("SubMS", "verifyColumns()"));
    2056           0 :     for(uInt i=0;i<colNames.nelements();i++)
    2057           0 :       if (!ms_p.tableDesc().isColumn(MS::columnName(colNames[i])))
    2058             :         {
    2059           0 :           ostringstream ostr;
    2060           0 :           ostr << "Desired column (" << MS::columnName(colNames[i])
    2061           0 :                << ") not found in the input MS (" << ms_p.tableName() << ").";
    2062           0 :           throw(AipsError(ostr.str()));
    2063           0 :         }
    2064           0 :   }
    2065             : 
    2066           0 :   Int SubMS::regridSpw(String& regridMessage,
    2067             :                        const String& outframe,
    2068             :                        const String& regridQuant,
    2069             :                        const Double regridVeloRestfrq,
    2070             :                        const String& regridInterpMeth,
    2071             :                        const Double regridCenter, 
    2072             :                        const Double regridBandwidth, 
    2073             :                        const Double regridChanWidth,
    2074             :                        const Bool doHanningSmooth,
    2075             :                        const Int phaseCenterFieldId,
    2076             :                        MDirection phaseCenter,
    2077             :                        const Bool centerIsStart,
    2078             :                        const Bool startIsEnd,
    2079             :                        const Int nchan,
    2080             :                        const Int width,
    2081             :                        const Int start
    2082             :                   ){
    2083             :     
    2084           0 :     LogIO os(LogOrigin("SubMS", "regridSpw()"));
    2085             : 
    2086           0 :     Int rval = -1; // return values: -1 = MS not modified, 1 = MS modified and OK, 
    2087             :                    // 0 = MS modified but not OK 
    2088             : 
    2089             :     // get the original table description of the MS 
    2090           0 :     TableDesc origMSTD(ms_p.actualTableDesc());
    2091           0 :     if(!origMSTD.isColumn("CORRECTED_DATA") &&  !origMSTD.isColumn("DATA") 
    2092           0 :        && !origMSTD.isColumn("FLOAT_DATA") && !origMSTD.isColumn("MODEL_DATA")){ 
    2093           0 :       os << LogIO::WARN << "MS has no DATA columns. Nothing to regrid." << LogIO::POST;
    2094             :     }     
    2095             : 
    2096             :     // Set up a little database to keep track of which pairs (FieldId, SPWId) have already
    2097             :     // been dealt with and what parameters were used
    2098             : 
    2099           0 :     vector<Int> oldSpwId;
    2100           0 :     vector<Int> oldFieldId;
    2101           0 :     vector<Int> newDataDescId;
    2102           0 :     vector<Bool> regrid;
    2103           0 :     vector<Bool> transform;
    2104           0 :     vector<MDirection> theFieldDirV;
    2105           0 :     vector<MPosition> mObsPosV;
    2106           0 :     vector<MFrequency::Types> fromFrameTypeV; // original ref frame of the SPW
    2107           0 :     vector<MFrequency::Ref> outFrameV; // new ref frame
    2108           0 :     vector<MRadialVelocity> outRadVelV; // radial velocity correction applied to the new ref frame
    2109           0 :     vector<Double> weightScaleV; // the scaling factor for the WEIGHTs
    2110           0 :     vector< Vector<Double> > xold; // the frequencies of the original SPW in the old ref frame
    2111           0 :     vector< Vector<Double> > xout; // the frequencies of the new SPW in the new ref frame
    2112           0 :     vector< Vector<Double> > xin;  // the frequencies of the old SPW in the new ref frame
    2113           0 :     vector< Int > method; // interpolation method cast to Int
    2114             :     
    2115             : 
    2116           0 :     Bool msModified = false;
    2117             : 
    2118           0 :     String oframe = outframe;
    2119           0 :     oframe.upcase();
    2120           0 :     Bool doRadVelCorr = (oframe=="SOURCE");
    2121             : 
    2122             :     // Loop 1: Verify the input parameters, no modification of the MS
    2123           0 :     if(!setRegridParameters(oldSpwId,
    2124             :                             oldFieldId,
    2125             :                             newDataDescId,
    2126             :                             regrid,
    2127             :                             transform,
    2128             :                             theFieldDirV,
    2129             :                             mObsPosV,
    2130             :                             fromFrameTypeV,
    2131             :                             outFrameV,
    2132             :                             outRadVelV,
    2133             :                             weightScaleV,
    2134             :                             xold,
    2135             :                             xout, 
    2136             :                             xin, 
    2137             :                             method,
    2138             :                             msModified,
    2139             :                             outframe,
    2140             :                             regridQuant,
    2141             :                             regridVeloRestfrq,
    2142             :                             regridInterpMeth,
    2143             :                             regridCenter,
    2144             :                             regridBandwidth, 
    2145             :                             regridChanWidth,
    2146             :                             phaseCenterFieldId,
    2147             :                             phaseCenter,
    2148             :                             false, // <-----
    2149             :                             os,
    2150             :                             regridMessage,
    2151             :                             centerIsStart,
    2152             :                             startIsEnd,
    2153             :                             nchan,
    2154             :                             width,
    2155             :                             start
    2156             :                             )){ // an error occured
    2157           0 :       return -1;
    2158             :     }
    2159             : 
    2160           0 :     if(oframe=="SOURCE" || oframe=="GEO"){
    2161             :       os << LogIO::NORMAL
    2162             :          << "Note: with outframe==GEO or outframe==SOURCE, the resulting spectral windows with be labeled as having reference frame REST.\n"
    2163             :          << "      All integrations will be Doppler-tracked to correspond to the GEO frame at the beginning of the observation\n"
    2164             :          << "      (with radial velocity corrections in the case of outframe==SOURCE).\n"
    2165             :          << "      No subsequent regridding to other reference frames will be possible."
    2166           0 :          << LogIO::POST;
    2167             :     }
    2168             : 
    2169           0 :     vector<Double> sigmaScaleV(weightScaleV.size());
    2170           0 :     for(uInt i=0; i<weightScaleV.size(); i++){
    2171           0 :       if(weightScaleV[i]<=0.){
    2172           0 :         os << LogIO::WARN << "Internal error: Encountered non-positive weight scaling factor " << weightScaleV[i] <<endl
    2173           0 :            << "Aborting." << LogIO::POST;
    2174           0 :         return -1;
    2175             :       }
    2176           0 :       else if(doHanningSmooth){
    2177           0 :         weightScaleV[i] *= 1.32;
    2178             :       }
    2179           0 :       sigmaScaleV[i] = 1./sqrt(weightScaleV[i]);
    2180             :     }
    2181             : 
    2182             :     // Loop 2: Write modified DD, SPW, and SOURCE tables
    2183             : 
    2184           0 :     if(!setRegridParameters(oldSpwId,
    2185             :                             oldFieldId,
    2186             :                             newDataDescId,
    2187             :                             regrid,
    2188             :                             transform,
    2189             :                             theFieldDirV,
    2190             :                             mObsPosV,
    2191             :                             fromFrameTypeV,
    2192             :                             outFrameV,
    2193             :                             outRadVelV,
    2194             :                             weightScaleV,
    2195             :                             xold,
    2196             :                             xout, 
    2197             :                             xin, 
    2198             :                             method,
    2199             :                             msModified,
    2200             :                             outframe,
    2201             :                             regridQuant,
    2202             :                             regridVeloRestfrq,
    2203             :                             regridInterpMeth,
    2204             :                             regridCenter,
    2205             :                             regridBandwidth, 
    2206             :                             regridChanWidth,
    2207             :                             phaseCenterFieldId,
    2208             :                             phaseCenter,
    2209             :                             true, // <-----
    2210             :                             os,
    2211             :                             regridMessage,
    2212             :                             centerIsStart,
    2213             :                             startIsEnd,
    2214             :                             nchan,
    2215             :                             width,
    2216             :                             start
    2217             :                             )){ // an error occured
    2218           0 :       if(msModified){
    2219           0 :         return 0;
    2220             :       }
    2221             :       else{
    2222           0 :         return -1;
    2223             :       }
    2224             :     }
    2225             :     
    2226           0 :     if(!msModified){ // nothing to be done in terms of regridding
    2227           0 :       if(doHanningSmooth){ // but we still need to Hanning smooth
    2228           0 :         os << LogIO::NORMAL << "Hanning smoothing not applied in regridding step since no regridding was necessary." <<  LogIO::POST;
    2229             :       }
    2230           0 :       return -1;
    2231             :     }
    2232             :     
    2233             :     // now we need to modify the main table ...
    2234             : 
    2235           0 :     Bool needRegridding = false;
    2236           0 :     for(uInt i=0; i<regrid.size(); i++){
    2237           0 :       if(regrid[i]){
    2238           0 :         needRegridding = true;
    2239             :       }
    2240             :     }
    2241             : 
    2242           0 :     if(needRegridding){
    2243             : 
    2244           0 :       os << LogIO::NORMAL << "Main table data array columns will be rewritten." <<  LogIO::POST;        
    2245             : 
    2246             :       // create the "partner" columns, i.e. rename the old array columns to old...
    2247             :       // and create new empty columns with the original names to hold the regridded values
    2248             : 
    2249           0 :       MSMainColumns mCols(ms_p);
    2250           0 :       Int nCorr = mCols.data().shape(0)(0); // the first dimension of DATA
    2251           0 :       IPosition dataShape(2, nCorr, xout[0].size());
    2252           0 :       Int obstype = 0; // default
    2253           0 :       MSObservationColumns obsCols(ms_p.observation());
    2254           0 :       String telescop = obsCols.telescopeName()(mCols.observationId()(0));
    2255           0 :       IPosition tileShape = MSTileLayout::tileShape(dataShape, obstype, telescop);
    2256             : 
    2257           0 :       createPartnerColumn(origMSTD, "CORRECTED_DATA", "oldCORRECTED_DATA", 3, tileShape);
    2258           0 :       createPartnerColumn(origMSTD, "DATA", "oldDATA", 3, tileShape);
    2259           0 :       createPartnerColumn(origMSTD, "FLOAT_DATA", "oldFLOAT_DATA", 3, tileShape);
    2260           0 :       createPartnerColumn(origMSTD, "LAG_DATA", "oldLAG_DATA", 3, tileShape);
    2261           0 :       createPartnerColumn(origMSTD, "MODEL_DATA", "oldMODEL_DATA", 3, tileShape);
    2262           0 :       createPartnerColumn(origMSTD, "SIGMA_SPECTRUM", "oldSIGMA_SPECTRUM", 3, tileShape);
    2263           0 :       createPartnerColumn(origMSTD, "WEIGHT_SPECTRUM", "oldWEIGHT_SPECTRUM", 3, tileShape);
    2264           0 :       createPartnerColumn(origMSTD, "FLAG", "oldFLAG", 3, tileShape);
    2265             : 
    2266           0 :       createPartnerColumn(origMSTD, "FLAG_CATEGORY", "oldFLAG_CATEGORY", 4,
    2267           0 :                           IPosition(4,tileShape(0),tileShape(1),1, tileShape(2)));
    2268           0 :     }
    2269             : 
    2270           0 :     MSMainColumns mainCols(ms_p);
    2271             : 
    2272             :     // columns which depend on the number of frequency channels and may need to be regridded:
    2273             :     // DATA, FLOAT_DATA, CORRECTED_DATA, MODEL_DATA, LAG_DATA, SIGMA_SPECTRUM,
    2274             :     // WEIGHT_SPECTRUM, FLAG, and FLAG_CATEGORY    
    2275           0 :     ArrayColumn<Complex> CORRECTED_DATACol =  mainCols.correctedData();
    2276           0 :     ArrayColumn<Complex>* oldCORRECTED_DATAColP  = 0;
    2277           0 :     ArrayColumn<Complex>  DATACol =  mainCols.data();
    2278           0 :     ArrayColumn<Complex>* oldDATAColP = 0;
    2279           0 :     ArrayColumn<Float> FLOAT_DATACol =  mainCols.floatData();
    2280           0 :     ArrayColumn<Float>* oldFLOAT_DATAColP = 0;
    2281           0 :     ArrayColumn<Complex> LAG_DATACol =  mainCols.lagData();
    2282           0 :     ArrayColumn<Complex>* oldLAG_DATAColP = 0;
    2283           0 :     ArrayColumn<Complex> MODEL_DATACol =  mainCols.modelData();
    2284           0 :     ArrayColumn<Complex>* oldMODEL_DATAColP = 0;
    2285           0 :     ArrayColumn<Float> SIGMA_SPECTRUMCol =  mainCols.sigmaSpectrum();
    2286           0 :     ArrayColumn<Float>* oldSIGMA_SPECTRUMColP = 0;
    2287           0 :     ArrayColumn<Float> WEIGHT_SPECTRUMCol =  mainCols.weightSpectrum();
    2288           0 :     ArrayColumn<Float>* oldWEIGHT_SPECTRUMColP = 0;
    2289           0 :     ArrayColumn<Bool> FLAGCol =  mainCols.flag();
    2290           0 :     ArrayColumn<Bool>* oldFLAGColP = 0;
    2291           0 :     ArrayColumn<Bool> FLAG_CATEGORYCol =  mainCols.flagCategory();
    2292           0 :     ArrayColumn<Bool>* oldFLAG_CATEGORYColP = 0;
    2293             : 
    2294             :     // WEIGHT and SIGMA will possibly need to be modified (these are also arrays but only for corr. product)
    2295           0 :     ArrayColumn<Float> SIGMACol = mainCols.sigma();
    2296           0 :     ArrayColumn<Float> WEIGHTCol = mainCols.weight();
    2297             : 
    2298           0 :     if(needRegridding){
    2299             : 
    2300           0 :       if(doHanningSmooth){
    2301           0 :         os << LogIO::NORMAL << "The following columns will be Hanning-smoothed before regridding: " <<  LogIO::POST;
    2302           0 :         if(!DATACol.isNull()){
    2303           0 :           os << LogIO::NORMAL << " DATA ";
    2304             :         }
    2305           0 :         if(!CORRECTED_DATACol.isNull()){
    2306           0 :           os << LogIO::NORMAL << " CORRECTED_DATA " <<  LogIO::POST;
    2307             :         }
    2308           0 :         if(!LAG_DATACol.isNull()){
    2309           0 :           os << LogIO::NORMAL << " LAG_DATA ";
    2310             :         }
    2311           0 :         if(!FLOAT_DATACol.isNull()){
    2312           0 :           os << LogIO::NORMAL << " FLOAT_DATA ";
    2313             :         }
    2314           0 :         os << LogIO::POST;
    2315             :       }
    2316             : 
    2317             :       // (create column objects for all "partners" of the array columns to be modified)
    2318           0 :       if(!CORRECTED_DATACol.isNull()){
    2319           0 :         oldCORRECTED_DATAColP = new ArrayColumn<Complex>(ms_p, "oldCORRECTED_DATA");
    2320             :       }
    2321           0 :       if(!DATACol.isNull()){
    2322           0 :         oldDATAColP = new ArrayColumn<Complex>(ms_p, "oldDATA");
    2323             :       }
    2324           0 :       if(!FLOAT_DATACol.isNull()){
    2325           0 :         oldFLOAT_DATAColP = new ArrayColumn<Float>(ms_p, "oldFLOAT_DATA");
    2326             :       }
    2327           0 :       if(!LAG_DATACol.isNull()){
    2328           0 :         oldLAG_DATAColP = new ArrayColumn<Complex>(ms_p, "oldLAG_DATA");
    2329             :       }
    2330           0 :       if(!MODEL_DATACol.isNull()){
    2331           0 :         oldMODEL_DATAColP = new ArrayColumn<Complex>(ms_p, "oldMODEL_DATA");
    2332             :       }
    2333           0 :       if(!SIGMA_SPECTRUMCol.isNull()){
    2334           0 :         oldSIGMA_SPECTRUMColP = new ArrayColumn<Float>(ms_p, "oldSIGMA_SPECTRUM");
    2335             :       }
    2336           0 :       if(!WEIGHT_SPECTRUMCol.isNull()){
    2337           0 :         oldWEIGHT_SPECTRUMColP = new ArrayColumn<Float>(ms_p, "oldWEIGHT_SPECTRUM");
    2338             :       }
    2339           0 :       if(!FLAGCol.isNull()){
    2340           0 :         oldFLAGColP = new ArrayColumn<Bool>(ms_p, "oldFLAG"); 
    2341             :       }
    2342           0 :       if(!FLAG_CATEGORYCol.isNull()){
    2343           0 :         oldFLAG_CATEGORYColP = new ArrayColumn<Bool>(ms_p, "oldFLAG_CATEGORY");
    2344             :       }
    2345             :     } // end if needRegridding
    2346             : 
    2347             :     // administrational columns needed from the main table
    2348           0 :     ScalarColumn<Int> fieldIdCol = mainCols.fieldId();
    2349           0 :     ScalarColumn<Int> DDIdCol = mainCols.dataDescId();
    2350           0 :     ScalarMeasColumn<MEpoch> mainTimeMeasCol = mainCols.timeMeas();
    2351             :    
    2352             :     // columns needed from subtables
    2353           0 :     MSDataDescription ddtable=ms_p.dataDescription();
    2354           0 :     MSDataDescColumns DDCols(ddtable);
    2355           0 :     ScalarColumn<Int> SPWIdCol = DDCols.spectralWindowId(); 
    2356           0 :     MSFieldColumns fldCols(ms_p.field());
    2357             : 
    2358             :     // Loop 3: Apply to MAIN table rows
    2359             :     
    2360             :     //    cout << "Modifying main table ..." << endl;
    2361             : 
    2362           0 :     uInt nMainTabRows = ms_p.nrow();
    2363             : 
    2364             :     // create time-sorted index for main table access
    2365           0 :     Vector<uInt> sortedI(nMainTabRows);
    2366             : 
    2367           0 :     Vector<Double> mainTimesV = mainCols.time().getColumn();
    2368           0 :     GenSortIndirect<Double>::sort(sortedI,mainTimesV);
    2369             : 
    2370             :     // prepare progress meter
    2371           0 :     Float progress = 0.2;
    2372           0 :     Float progressStep = 0.2;
    2373           0 :     if(ms_p.nrow()>100000){
    2374           0 :       progress = 0.1;
    2375           0 :       progressStep = 0.1;
    2376             :     }
    2377             : 
    2378             :     // prepare some regridding prerequisites
    2379           0 :     FFTServer<Float, Complex> fFFTServer; // for fftshift, if needed
    2380             :  
    2381             :     // start loop over main table
    2382           0 :     for(uInt mainTabRowI=0; mainTabRowI<nMainTabRows; mainTabRowI++){
    2383             :       
    2384           0 :       uInt mainTabRow = sortedI(mainTabRowI); // i.e. mainTabRow is sorted in time
    2385             : 
    2386             :       //if(mainTabRow!=mainTabRowI){
    2387             :       //  cout << "processing row " << mainTabRow << ", index " << mainTabRowI << endl;
    2388             :       //}
    2389             : 
    2390             :       // For each MAIN table row, the FIELD_ID cell and the DATA_DESC_ID cell are read 
    2391           0 :       Int theFieldId = fieldIdCol(mainTabRow);
    2392           0 :       Int theDataDescId = DDIdCol(mainTabRow);
    2393             :       // and the SPW_ID extracted from the corresponding row in the DATA_DESCRIPTION table.
    2394           0 :       Int theSPWId = SPWIdCol(theDataDescId);
    2395             : 
    2396           0 :       MEpoch theObsTime = mainTimeMeasCol(mainTabRow);
    2397             :       
    2398             :       //  The pair (theFieldId, theSPWId) is looked up in the "done table". 
    2399           0 :       Int iDone = -1;
    2400           0 :       for (uInt i=0; i<oldSpwId.size(); i++){
    2401           0 :         if(oldSpwId[i]==theSPWId && (oldFieldId[i]==theFieldId || phaseCenterFieldId>=-1)){
    2402             :           // if common phase center is given, treat all fields the same
    2403           0 :           iDone = i;
    2404           0 :           break;
    2405             :         }
    2406             :       }
    2407           0 :       if(iDone<0){ // should not occur
    2408             :         os << LogIO::SEVERE << "Internal error: Did not find regrid parameters for field ==" 
    2409           0 :            << theFieldId << " spw ==" <<  theSPWId << LogIO::POST;
    2410           0 :         return 0;
    2411             :       }
    2412             :       
    2413             :       
    2414           0 :       if (DDIdCol(mainTabRow)!=newDataDescId[iDone]){
    2415             :         // If the data description actually changed, then DATA_DESC_ID 
    2416             :         //      of this main table row is set to the new value given in the "done" table
    2417           0 :         DDIdCol.put(mainTabRow, newDataDescId[iDone]);
    2418             :         
    2419             :       }
    2420             :       
    2421             :       //Furthermore, if regrid[iDone] is true, the visibilities and all 
    2422             :       // channel-number-dependent arrays need to be regridded.
    2423           0 :       if(regrid[iDone]){
    2424             : 
    2425           0 :         Bool doExtrapolate = false;
    2426             : 
    2427             :         // regrid the complex columns
    2428           0 :         Array<Complex> yout;
    2429           0 :         Array<Bool> youtFlags;
    2430           0 :         Bool youtFlagsWritten(false);
    2431           0 :         Array<Complex> yin;
    2432           0 :         Array<Bool> yinFlags((*oldFLAGColP)(mainTabRow));
    2433           0 :         Array<Bool> yinFlagsUnsmoothed;
    2434           0 :         Array<Complex> yinIntermediate;
    2435           0 :         Array<Bool> yinFlagsIntermediate;
    2436           0 :         if(doHanningSmooth){
    2437           0 :           yinFlagsUnsmoothed.assign(yinFlags);
    2438             :         }
    2439             : 
    2440           0 :         Vector<Double> xindd(xold[iDone].size());
    2441           0 :         Double theShift = 0.;
    2442             : 
    2443           0 :         if(transform[iDone]){
    2444             : 
    2445             :           // create frequency machine for this time stamp
    2446           0 :           MDirection fldDir = fldCols.phaseDirMeas(theFieldId, mainTimesV(mainTabRow));
    2447             : 
    2448             : 
    2449           0 :           MFrequency::Ref fromFrame = MFrequency::Ref(fromFrameTypeV[iDone], MeasFrame(fldDir, mObsPosV[iDone], theObsTime));
    2450           0 :           Unit unit(String("Hz"));
    2451           0 :           MFrequency::Convert freqTrans2(unit, fromFrame, outFrameV[iDone]);
    2452             : 
    2453           0 :           MDoppler radVelCorr; // no correction
    2454           0 :           Bool radVelSignificant = false;
    2455             : 
    2456             :           // prepare correction for radial velocity if requested and possible
    2457           0 :           if(doRadVelCorr && fldCols.needInterTime(theFieldId)){
    2458           0 :             MRadialVelocity mRV = fldCols.radVelMeas(theFieldId, mainTimesV(mainTabRow));
    2459           0 :             MRadialVelocity::Ref mRVR = mRV.getRef();
    2460           0 :             if(mRVR.getType() == MRadialVelocity::GEO){
    2461           0 :               Quantity mrv = mRV.get("m/s");
    2462           0 :               Quantity offsetMrv = outRadVelV[iDone].get("m/s"); // the radvel by which the out SPW def was shifted 
    2463           0 :               radVelCorr = MDoppler(mrv-(2.*offsetMrv)); 
    2464           0 :               if(fabs(mrv.getValue())>1E-6){
    2465           0 :                 radVelSignificant = true;
    2466             :               }
    2467           0 :             }
    2468             :             else{
    2469             :               os << LogIO::SEVERE << "Cannot perform radial velocity correction with ephemerides of type "  
    2470           0 :                  << MRadialVelocity::showType(mRVR.getType()) << ".\nType needs to be GEO." << LogIO::POST;
    2471           0 :               return 0;
    2472             :             }
    2473           0 :           } 
    2474             :         
    2475             :           // prepare fftshift if necessary
    2476           0 :           if(method[iDone]==(Int)useFFTShift || method[iDone]==(Int)useLinIntThenFFTShift){
    2477           0 :             uInt centerChan = xold[iDone].size()/2;
    2478           0 :             Vector<Double> newFreq(1);
    2479           0 :             newFreq[0] = freqTrans2(xold[iDone][centerChan]).get(unit).getValue();
    2480           0 :             if(radVelSignificant){
    2481           0 :               newFreq = radVelCorr.shiftFrequency(newFreq);
    2482             :             }
    2483             : 
    2484           0 :             theShift = newFreq[0] - xin[iDone][centerChan];
    2485             : 
    2486             :             //cout << "the shift " << theShift << endl;
    2487           0 :             for(uInt i=0; i<xin[iDone].size(); i++){ // cannot use assign due to different data type
    2488           0 :               xindd[i] = xin[iDone][i];
    2489             :             }
    2490           0 :           }
    2491             :           else{
    2492             :             // transform from this timestamp to the one of the output SPW
    2493           0 :             for(uInt i=0; i<xindd.size(); i++){
    2494           0 :               xindd[i] = freqTrans2(xold[iDone][i]).get(unit).getValue();
    2495             :             }
    2496           0 :             if(radVelSignificant){
    2497           0 :               xindd = radVelCorr.shiftFrequency(xindd);
    2498             :             }
    2499             :             //    if(mainTabRow==0){ // debug output
    2500             :             //      Int i = 25;
    2501             :             //      cout << "i " << i << " xin " << setprecision(9) << xin[iDone][i] << " xindd " << setprecision(9) << xindd[i] 
    2502             :             //           << " xout " << setprecision(9) << xout[iDone][i] << endl;
    2503             :             //      cout << "i " << i << " vradxin " << setprecision(9) << vrad(xin[iDone][i], regridVeloRestfrq) 
    2504             :             //           << " vradxindd " << setprecision(9) << vrad(xindd[i], regridVeloRestfrq)  
    2505             :             //           << " xout " << setprecision(9) << vrad(xout[iDone][i], regridVeloRestfrq) << endl;
    2506             :             //    }
    2507             :           }
    2508           0 :         }
    2509             :         else{ // no additional transformation of input grid
    2510           0 :           for(uInt i=0; i<xin[iDone].size(); i++){ // cannot use assign due to different data type
    2511           0 :             xindd[i] = xin[iDone][i];
    2512             :           }
    2513             :         }
    2514             : 
    2515             : 
    2516           0 :         Double relShift = 0.;
    2517           0 :         InterpolateArray1D<Double,Complex>::InterpolationMethod  methodC = InterpolateArray1D<Double,Complex>::linear; // the default
    2518           0 :         InterpolateArray1D<Double,Float>::InterpolationMethod  methodF = InterpolateArray1D<Double,Float>::linear;
    2519             : 
    2520           0 :         if(fabs(theShift)>0. && 
    2521           0 :            (method[iDone]==(Int)useFFTShift || method[iDone]==(Int)useLinIntThenFFTShift)
    2522             :            ){
    2523           0 :           Int endChan = xout[iDone].size()-1;
    2524           0 :           if(endChan<=0){
    2525           0 :             os << LogIO::SEVERE << "Internal error: Cannot regrid a single-channel SPW using FFTshift" << LogIO::POST;
    2526           0 :             return 0;
    2527             :           }
    2528           0 :           Double chanWidth = xout[iDone][1] - xout[iDone][0];
    2529           0 :           relShift = -theShift/(xout[iDone][endChan] - xout[iDone][0] + chanWidth);
    2530             :           //cout << "the relshift " << relShift << endl;
    2531             :         }
    2532             :         else{
    2533           0 :           methodC = (InterpolateArray1D<Double,Complex>::InterpolationMethod) method[iDone];
    2534           0 :           methodF = (InterpolateArray1D<Double,Float>::InterpolationMethod) method[iDone];
    2535             :         }
    2536             : 
    2537           0 :         if(!CORRECTED_DATACol.isNull()){
    2538           0 :           yin.assign((*oldCORRECTED_DATAColP)(mainTabRow));
    2539             : 
    2540             :           // hanning smooth if requested
    2541           0 :           if(doHanningSmooth){
    2542             : 
    2543             :             // copy yin to yinUnsmoothed 
    2544           0 :             Array<Complex> yinUnsmoothed;
    2545           0 :             yinUnsmoothed.assign(yin);
    2546             : 
    2547           0 :             Smooth<Complex>::hanning(yin, // the output
    2548             :                                      yinFlags, // the output flags
    2549             :                                      yinUnsmoothed, // the input
    2550             :                                      yinFlagsUnsmoothed, // the input flags
    2551             :                                      false);  // for flagging: good is not true
    2552           0 :           }
    2553             :           
    2554           0 :           if(method[iDone]==(Int)useLinIntThenFFTShift){
    2555             :             // first interpolate to equidistant grid at initial timestamp
    2556           0 :             InterpolateArray1D<Double,Complex>::interpolate(yinIntermediate, // the new visibilities
    2557             :                                                             yinFlagsIntermediate, // the new flags
    2558           0 :                                                             xout[iDone], // the new channel centers (for the output SPW timestamp)
    2559             :                                                             xindd, // the old channel centers 
    2560             :                                                             yin, // the old visibilities 
    2561             :                                                             yinFlags,// the old flags
    2562             :                                                             InterpolateArray1D<Double,Complex>::linear, // the interpol method
    2563             :                                                             false, // for flagging: good is not true
    2564             :                                                             doExtrapolate // do not extrapolate
    2565             :                                                             );      
    2566             :             // shift from this timestamp to the output SPW timestamp
    2567           0 :             fFFTServer.fftshift(yout, youtFlags, yinIntermediate, yinFlagsIntermediate, 
    2568           0 :                                 1, // axis 1 of the array is the polarisation axis 
    2569             :                                 relShift, 
    2570             :                                 false, // for flagging: good is not true 
    2571             :                                 false);
    2572             : 
    2573             :           }
    2574           0 :           else if(method[iDone]==(Int)useFFTShift){
    2575             :             // shift from this timestamp to the output SPW timestamp
    2576           0 :             fFFTServer.fftshift(yout, youtFlags, yin, yinFlags, 
    2577           0 :                                 1, // axis 1 of the array is the polarisation axis 
    2578             :                                 relShift, 
    2579             :                                 false, // for flagging: good is not true 
    2580             :                                 false);
    2581             :           }
    2582             :           else{
    2583           0 :             InterpolateArray1D<Double,Complex>::interpolate(yout, // the new visibilities
    2584             :                                                             youtFlags, // the new flags
    2585           0 :                                                             xout[iDone], // the new channel centers
    2586             :                                                             xindd, // the old channel centers
    2587             :                                                             yin, // the old visibilities 
    2588             :                                                             yinFlags,// the old flags
    2589             :                                                             methodC, // the interpol method
    2590             :                                                             false, // for flagging: good is not true
    2591             :                                                             doExtrapolate // do not extrapolate
    2592             :                                                             );
    2593             :           }
    2594             : 
    2595           0 :           CORRECTED_DATACol.put(mainTabRow, yout);
    2596           0 :           if(!youtFlagsWritten){ 
    2597           0 :             FLAGCol.put(mainTabRow, youtFlags);
    2598           0 :             youtFlagsWritten = true;
    2599             :           }
    2600             :         }
    2601           0 :         if(!DATACol.isNull()){
    2602           0 :           yin.assign((*oldDATAColP)(mainTabRow));
    2603           0 :           if(doHanningSmooth){
    2604           0 :             Array<Complex> yinUnsmoothed;
    2605           0 :             yinUnsmoothed.assign(yin);
    2606             : 
    2607           0 :             Smooth<Complex>::hanning(yin, yinFlags, yinUnsmoothed, yinFlagsUnsmoothed, false);  
    2608           0 :           }
    2609             : 
    2610           0 :           if(method[iDone]==(Int)useLinIntThenFFTShift){
    2611           0 :             InterpolateArray1D<Double,Complex>::interpolate(yinIntermediate, yinFlagsIntermediate, xout[iDone], 
    2612             :                                                             xindd, yin, yinFlags,
    2613             :                                                             InterpolateArray1D<Double,Complex>::linear, 
    2614             :                                                             false, doExtrapolate);          
    2615           0 :             fFFTServer.fftshift(yout, youtFlags, yinIntermediate, yinFlagsIntermediate, 
    2616           0 :                                 1, relShift, false, false);
    2617             : 
    2618             :           }
    2619           0 :           else if(method[iDone]==(Int)useFFTShift){
    2620           0 :             fFFTServer.fftshift(yout, youtFlags, yin, yinFlags, 
    2621           0 :                                 1, relShift, false, false);
    2622             :           }
    2623             :           else{
    2624           0 :             InterpolateArray1D<Double,Complex>::interpolate(yout, youtFlags, xout[iDone], 
    2625             :                                                             xindd, yin, yinFlags,
    2626             :                                                             methodC, false, doExtrapolate);
    2627             :           }
    2628             : 
    2629           0 :           DATACol.put(mainTabRow, yout);
    2630           0 :           if(!youtFlagsWritten){ 
    2631           0 :             FLAGCol.put(mainTabRow, youtFlags);
    2632           0 :             youtFlagsWritten = true;
    2633             :           }       
    2634             :         }
    2635           0 :         if(!LAG_DATACol.isNull()){
    2636           0 :           yin.assign((*oldLAG_DATAColP)(mainTabRow));
    2637           0 :           if(doHanningSmooth){ 
    2638           0 :             Array<Complex> yinUnsmoothed;
    2639           0 :             yinUnsmoothed.assign(yin);
    2640             : 
    2641           0 :             Smooth<Complex>::hanning(yin, yinFlags, yinUnsmoothed, yinFlagsUnsmoothed, false);  
    2642           0 :           }
    2643           0 :           if(method[iDone]==(Int)useLinIntThenFFTShift){
    2644           0 :             InterpolateArray1D<Double,Complex>::interpolate(yinIntermediate, yinFlagsIntermediate, xout[iDone], 
    2645             :                                                             xindd, yin, yinFlags,
    2646             :                                                             InterpolateArray1D<Double,Complex>::linear, 
    2647             :                                                             false, doExtrapolate);          
    2648           0 :             fFFTServer.fftshift(yout, youtFlags, yinIntermediate, yinFlagsIntermediate, 
    2649           0 :                                 1, relShift, false, false);
    2650             : 
    2651             :           }
    2652           0 :           else if(method[iDone]==(Int)useFFTShift){
    2653           0 :             fFFTServer.fftshift(yout, youtFlags, yin, yinFlags, 
    2654           0 :                                 1, relShift, false, false);
    2655             :           }
    2656             :           else{
    2657           0 :             InterpolateArray1D<Double,Complex>::interpolate(yout, youtFlags, xout[iDone], 
    2658             :                                                             xindd, yin, yinFlags,
    2659             :                                                             methodC, false, doExtrapolate);
    2660             :           }
    2661             : 
    2662           0 :           LAG_DATACol.put(mainTabRow, yout);
    2663             :         }
    2664           0 :         if(!MODEL_DATACol.isNull()){
    2665           0 :           yin.assign((*oldMODEL_DATAColP)(mainTabRow));
    2666             : 
    2667           0 :           if(method[iDone]==(Int)useLinIntThenFFTShift){
    2668           0 :             InterpolateArray1D<Double,Complex>::interpolate(yinIntermediate, yinFlagsIntermediate, xout[iDone], 
    2669             :                                                             xindd, yin, yinFlags,
    2670             :                                                             InterpolateArray1D<Double,Complex>::linear, 
    2671             :                                                             false, doExtrapolate);          
    2672           0 :             fFFTServer.fftshift(yout, youtFlags, yinIntermediate, yinFlagsIntermediate, 
    2673           0 :                                 1, relShift, false, false);
    2674             : 
    2675             :           }
    2676           0 :           else if(method[iDone]==(Int)useFFTShift){
    2677           0 :             fFFTServer.fftshift(yout, youtFlags, yin, yinFlags, 
    2678           0 :                                 1, relShift, false, false);
    2679             :           }
    2680             :           else{
    2681           0 :             InterpolateArray1D<Double,Complex>::interpolate(yout, youtFlags, xout[iDone], 
    2682             :                                                             xindd, yin, yinFlags,
    2683             :                                                             methodC, false, doExtrapolate);
    2684             :           }
    2685             : 
    2686           0 :           MODEL_DATACol.put(mainTabRow, yout);
    2687           0 :           if(!youtFlagsWritten){ 
    2688           0 :             FLAGCol.put(mainTabRow, youtFlags);
    2689           0 :             youtFlagsWritten = true;
    2690             :           }
    2691             :         }
    2692             :         
    2693             :         // regrid the Float columns
    2694           0 :         Array<Float> yinf;
    2695           0 :         Array<Float> youtf;
    2696           0 :         Array<Float> fYinIntermediate;
    2697           0 :         if(!FLOAT_DATACol.isNull()){
    2698           0 :           yinf.assign((*oldFLOAT_DATAColP)(mainTabRow));
    2699           0 :           if(doHanningSmooth){
    2700           0 :             Array<Float> yinfUnsmoothed;
    2701           0 :             yinfUnsmoothed.assign(yinf);
    2702             : 
    2703           0 :             Smooth<Float>::hanning(yinf, yinFlags, yinfUnsmoothed, yinFlagsUnsmoothed, false);  
    2704           0 :           }
    2705             : 
    2706           0 :           if(method[iDone]==(Int)useLinIntThenFFTShift){
    2707           0 :             InterpolateArray1D<Double,Float>::interpolate(fYinIntermediate, yinFlagsIntermediate, xout[iDone], 
    2708             :                                                           xindd, yinf, yinFlags,
    2709             :                                                           InterpolateArray1D<Double,Float>::linear, 
    2710             :                                                           false, doExtrapolate);            
    2711           0 :             fFFTServer.fftshift(youtf, youtFlags, fYinIntermediate, yinFlagsIntermediate, 
    2712           0 :                                 1, relShift, false);
    2713             : 
    2714             :           }
    2715           0 :           else if(method[iDone]==(Int)useFFTShift){
    2716           0 :             fFFTServer.fftshift(youtf, youtFlags, yinf, yinFlags, 
    2717           0 :                                 1, relShift, false);
    2718             :           }
    2719             :           else{
    2720           0 :             InterpolateArray1D<Double, Float>::interpolate(youtf, youtFlags, xout[iDone], xindd, 
    2721             :                                                            yinf, yinFlags, methodF, false, doExtrapolate);
    2722             :           }
    2723             : 
    2724           0 :           FLOAT_DATACol.put(mainTabRow, youtf);
    2725           0 :           if(!youtFlagsWritten){ 
    2726           0 :             FLAGCol.put(mainTabRow, youtFlags);
    2727           0 :             youtFlagsWritten = true;
    2728             :           }
    2729             :         }
    2730             : 
    2731           0 :         if(!SIGMA_SPECTRUMCol.isNull()){
    2732           0 :           yinf.assign((*oldSIGMA_SPECTRUMColP)(mainTabRow));
    2733           0 :           InterpolateArray1D<Double, Float>::interpolate(youtf, youtFlags, xout[iDone], xindd, 
    2734             :                                                          yinf, yinFlags, methodF, false, doExtrapolate);
    2735           0 :           SIGMA_SPECTRUMCol.put(mainTabRow, youtf * sigmaScaleV[iDone]); //  an approximation
    2736             :         }
    2737           0 :         if(!WEIGHT_SPECTRUMCol.isNull() && oldWEIGHT_SPECTRUMColP->isDefined(mainTabRow)){ // required column, but can be empty
    2738           0 :           yinf.assign((*oldWEIGHT_SPECTRUMColP)(mainTabRow));
    2739           0 :           InterpolateArray1D<Double, Float>::interpolate(youtf, youtFlags, xout[iDone],
    2740             :                                                          xindd, yinf, yinFlags,
    2741             :                                                          methodF, false, doExtrapolate);
    2742           0 :           WEIGHT_SPECTRUMCol.put(mainTabRow, youtf * weightScaleV[iDone]); // an approximation
    2743             :         }
    2744             :         
    2745             : 
    2746           0 :         SIGMACol.put(mainTabRow, SIGMACol(mainTabRow)*sigmaScaleV[iDone]);
    2747           0 :         WEIGHTCol.put(mainTabRow, WEIGHTCol(mainTabRow)*weightScaleV[iDone]);
    2748             : 
    2749             : 
    2750             :         // deal with FLAG_CATEGORY
    2751             :         // note: FLAG_CATEGORY is a required column, but it can be undefined (empty)
    2752             :         
    2753           0 :         if(FLAG_CATEGORYCol.isDefined(mainTabRow)){
    2754           0 :           Array<Bool> flagCat((*oldFLAG_CATEGORYColP)(mainTabRow));  
    2755           0 :           IPosition flagCatShape = (*oldFLAG_CATEGORYColP).shape(mainTabRow);
    2756           0 :           Int nCorrelations = flagCatShape(0); // get the dimension of the first axis
    2757           0 :           Int nChannels = flagCatShape(1); // get the dimension of the second axis
    2758           0 :           Int nCat = flagCatShape(2); // the dimension of the third axis ==
    2759             :                                       // number of categories
    2760           0 :           Int nOutChannels = xout[iDone].size();
    2761             :           
    2762           0 :           Vector<Float> dummyYin(nChannels);
    2763           0 :           Vector<Float> dummyYout(nOutChannels);
    2764           0 :           Array<Bool> flagCatOut(IPosition(3, nCorrelations, nOutChannels, nCat)); 
    2765             :           
    2766           0 :           for(Int i=0; i<nCat; i++){
    2767           0 :             IPosition start(0,0,i), length (nCorrelations,nChannels,i), stride (1,1,0);
    2768           0 :             Slicer slicer (start, length, stride, Slicer::endIsLast);
    2769           0 :             yinFlags.assign(flagCat(slicer));
    2770           0 :             InterpolateArray1D<Double, Float>::interpolate(dummyYout, youtFlags,
    2771           0 :                                                            xout[iDone], xindd, 
    2772             :                                                            dummyYin, yinFlags,
    2773             :                                                            methodF, false, false);
    2774             :             // write the slice to the array flagCatOut
    2775           0 :             for(Int j=0; j<nCorrelations; j++){
    2776           0 :               for(Int k=0; k<nOutChannels; k++){
    2777           0 :                 flagCatOut(IPosition(3, j, k, i)) = youtFlags(IPosition(2,j,k));
    2778             :               }
    2779             :             }
    2780           0 :           }
    2781             :           
    2782           0 :           FLAG_CATEGORYCol.put(mainTabRow, flagCatOut);
    2783             :           
    2784           0 :         }
    2785             :         
    2786           0 :         msModified = true;
    2787             :         
    2788           0 :       } // end if regridding necessary
    2789             : 
    2790           0 :       if(mainTabRow>nMainTabRows*progress){
    2791           0 :         cout << "regridSpw progress: " << progress*100 << "% processed ... " << endl;
    2792           0 :         progress += progressStep;
    2793             :       }
    2794             :       
    2795           0 :     } // end loop over main table rows
    2796           0 :     cout << "regridSpw progress: 100% processed." << endl;
    2797             : 
    2798           0 :     if(msModified){
    2799           0 :       if(needRegridding){
    2800             : 
    2801             :         // remove the "partner" columns
    2802           0 :         if(!CORRECTED_DATACol.isNull()){
    2803           0 :           ms_p.removeColumn("oldCORRECTED_DATA");
    2804             :         }
    2805           0 :         if(!DATACol.isNull()){
    2806           0 :           ms_p.removeColumn("oldDATA");
    2807             :         }
    2808           0 :         if(!FLOAT_DATACol.isNull()){
    2809           0 :           ms_p.removeColumn("oldFLOAT_DATA");
    2810             :         }
    2811           0 :         if(!LAG_DATACol.isNull()){
    2812           0 :           ms_p.removeColumn("oldLAG_DATA");
    2813             :         }
    2814           0 :         if(!MODEL_DATACol.isNull()){
    2815           0 :           ms_p.removeColumn("oldMODEL_DATA");
    2816             :         }
    2817           0 :         if(!SIGMA_SPECTRUMCol.isNull()){
    2818           0 :           ms_p.removeColumn("oldSIGMA_SPECTRUM");
    2819             :         }
    2820           0 :         if(!WEIGHT_SPECTRUMCol.isNull()){
    2821           0 :           ms_p.removeColumn("oldWEIGHT_SPECTRUM");
    2822             :         }
    2823           0 :         if(!FLAGCol.isNull()){
    2824           0 :           ms_p.removeColumn("oldFLAG");
    2825             :         } 
    2826           0 :         if(!FLAG_CATEGORYCol.isNull()){
    2827           0 :           ms_p.removeColumn("oldFLAG_CATEGORY");
    2828             :         }
    2829             :       }
    2830             :       
    2831           0 :       rval = 1; // successful modification
    2832             :     }
    2833           0 :     return rval;
    2834             : 
    2835           0 :   }
    2836             : 
    2837             : 
    2838           0 :   Bool SubMS::createPartnerColumn(TableDesc& modMSTD,
    2839             :                                   const String& oldName, 
    2840             :                                   const String& newName,
    2841             :                                   const Int& hypercolumnDim,
    2842             :                                   const IPosition& tileShape
    2843             :                                   ){
    2844           0 :     Bool rval = false;
    2845           0 :     if(modMSTD.isColumn(oldName)){
    2846             :       // get the old column desc
    2847           0 :       ColumnDesc myColDesc(modMSTD.columnDesc(oldName));
    2848             :       // move the column away
    2849           0 :       ms_p.renameColumn(newName, oldName);
    2850             :       // rename the hypercolumn
    2851           0 :       String hcName(myColDesc.dataManagerGroup());
    2852           0 :       String oldHcName = hcName;
    2853           0 :       String newHcName = hcName + "B";
    2854           0 :       if(!oldHcName.empty() && ms_p.actualTableDesc().isHypercolumn(oldHcName)){
    2855           0 :         ms_p.renameHypercolumn(newHcName, oldHcName);
    2856             :       }
    2857             :       // rename the datamanager
    2858           0 :       DataManager* myDM = ms_p.findDataManager(oldHcName);
    2859           0 :       ((TiledStMan*) myDM)->setDataManagerName(newHcName);
    2860             :       // create new hypercolumn and a new column with new data manager
    2861           0 :       TiledShapeStMan*  tiledStMan = new TiledShapeStMan(oldHcName, tileShape);
    2862           0 :       ms_p.addColumn(myColDesc, *tiledStMan);
    2863           0 :       modMSTD.defineHypercolumn(oldHcName, hypercolumnDim, stringToVector(oldName));
    2864             : 
    2865           0 :       rval = true;
    2866           0 :     }
    2867           0 :     return rval;
    2868             :   }
    2869             : 
    2870             : 
    2871           0 :   Bool SubMS::regridChanBounds(Vector<Double>& newChanLoBound, 
    2872             :                                Vector<Double>& newChanHiBound,
    2873             :                                const Double regridCenterC,
    2874             :                                const Double regridBandwidth, 
    2875             :                                const Double regridChanWidthC, 
    2876             :                                const Double regridVeloRestfrq, 
    2877             :                                const String regridQuant,
    2878             :                                const Vector<Double>& transNewXinC, 
    2879             :                                const Vector<Double>& transCHAN_WIDTHC,
    2880             :                                String& message,
    2881             :                                const Bool centerIsStartC,
    2882             :                                const Bool startIsEndC,
    2883             :                                const Int nchanC,
    2884             :                                const Int width,
    2885             :                                const Int startC
    2886             :                                ){
    2887           0 :     ostringstream oss;
    2888             : 
    2889             : //     cout << "regridCenterC " <<  regridCenterC << " regridBandwidth " << regridBandwidth 
    2890             : //       << " regridChanWidthC " << regridChanWidthC << endl;
    2891             : //     cout << " nchanC " << nchanC << " width " << width << " startC " << startC << endl;
    2892             : //     cout << " regridQuant " << regridQuant << " centerIsStartC " <<  centerIsStartC << " startIsEndC " << startIsEndC << endl;
    2893             :     
    2894             : 
    2895           0 :     Vector<Double> transNewXin(transNewXinC);
    2896           0 :     Vector<Double> transCHAN_WIDTH(transCHAN_WIDTHC);
    2897           0 :     Bool centerIsStart = centerIsStartC;
    2898           0 :     Bool startIsEnd = startIsEndC;
    2899           0 :     Double regridChanWidth = regridChanWidthC;
    2900           0 :     Double regridCenter = regridCenterC;
    2901           0 :     Int nchan = nchanC;
    2902           0 :     Int start = startC;
    2903             : 
    2904           0 :     Int oldNUM_CHAN = transNewXin.size();
    2905             : 
    2906             : 
    2907             :     // detect spectral windows defined with descending frequency
    2908           0 :     Bool isDescending=false;
    2909           0 :     for(uInt i=1; i<transNewXin.size(); i++){
    2910           0 :       if(transNewXin(i)<transNewXin(i-1)){
    2911           0 :         isDescending = true;
    2912             :       }
    2913           0 :       else if(isDescending){ // i.e. descending was detected but now we encounter ascending
    2914           0 :         oss << "Channel frequencies are neither in ascending nor in descending order. Cannot process.";
    2915           0 :         message = oss.str();
    2916           0 :         return false;  
    2917             :       } 
    2918             :     }
    2919             : 
    2920           0 :     if(isDescending){ // need to reverse the order for processing and later reverse the result
    2921             :       //cout << "SPW has descending order ..." << endl;
    2922           0 :       uInt n = transNewXin.size();
    2923           0 :       Vector<Double> tempF, tempW;
    2924           0 :       tempF.assign(transNewXin);
    2925           0 :       tempW.assign(transCHAN_WIDTH);
    2926           0 :       for(uInt i=0; i<n; i++){
    2927           0 :         transNewXin(i) = tempF(n-1-i);
    2928           0 :         transCHAN_WIDTH(i) = tempW(n-1-i);
    2929             :         //cout << "i f w " << i << " " << transNewXin(i) << " " << transCHAN_WIDTH(i) << endl;
    2930             :       }
    2931             :       // also need to adjust the start values 
    2932           0 :       if(startC>=0){
    2933           0 :         start = n-1-startC;
    2934           0 :         if(centerIsStartC){
    2935           0 :           startIsEnd = !startIsEnd;
    2936             :         }
    2937             :       }
    2938             :       //}
    2939           0 :     }
    2940             : 
    2941             :     // verify regridCenter, regridBandwidth, and regridChanWidth 
    2942             :     // Note: these are in the units corresponding to regridQuant!
    2943             :     
    2944           0 :     if(regridQuant=="chan"){ ////////////////////////
    2945             :       // channel numbers ...
    2946           0 :       Int regridCenterChan = -1;
    2947           0 :       Int regridBandwidthChan = -1;
    2948           0 :       Int regridChanWidthChan = -1;
    2949             : 
    2950           0 :       if(regridCenter<-1E30){ // not set
    2951             :         // find channel center closest to center of bandwidth
    2952           0 :         lDouble BWCenterF = (transNewXin[0]+transNewXin[oldNUM_CHAN-1])/2.;
    2953           0 :         for(Int i=0; i<oldNUM_CHAN; i++){
    2954           0 :           if(transNewXin[i] >= BWCenterF){
    2955           0 :             regridCenterChan = i;
    2956           0 :             break;
    2957             :           }
    2958             :         }
    2959           0 :         centerIsStart = false;
    2960             :       }
    2961           0 :       else if(0. <= regridCenter && regridCenter < Double(oldNUM_CHAN)){ // valid input
    2962           0 :         regridCenterChan = (Int) floor(regridCenter);  
    2963             :       }
    2964             :       else { // invalid
    2965           0 :         if(centerIsStart){
    2966           0 :           oss << "SPW start ";
    2967             :         }
    2968             :         else{
    2969           0 :           oss << "SPW center ";
    2970             :         }
    2971           0 :         oss << regridCenter << " outside valid range which is "
    2972           0 :             << 0 << " - " << oldNUM_CHAN-1 <<".";
    2973           0 :         message = oss.str();
    2974           0 :         return false;  
    2975             :       }  
    2976             :       
    2977           0 :       if(regridBandwidth<=0.|| nchan>0){ // not set or nchan set
    2978           0 :         if(nchan>0){
    2979           0 :           regridBandwidthChan = nchan;
    2980             :         }
    2981             :         else{
    2982           0 :           regridBandwidthChan = oldNUM_CHAN;
    2983             :         }
    2984             :       }
    2985             :       else{
    2986           0 :         regridBandwidthChan = (Int) floor(regridBandwidth);
    2987             :       }
    2988             : 
    2989           0 :       if(centerIsStart){
    2990           0 :         if(startIsEnd){
    2991           0 :           regridCenterChan = regridCenterChan - regridBandwidthChan/2;
    2992             :         }
    2993             :         else{
    2994           0 :           regridCenterChan = regridCenterChan + regridBandwidthChan/2;
    2995             :         }
    2996           0 :         centerIsStart = false;
    2997             :       }
    2998             : 
    2999           0 :       if(regridCenterChan-regridBandwidthChan/2 < 0) { // center too close to lower edge
    3000           0 :         regridBandwidthChan = 2 * regridCenterChan + 1;
    3001           0 :         oss << " *** Requested output SPW width too large." << endl;
    3002             :       }
    3003           0 :       if( oldNUM_CHAN < regridCenterChan+regridBandwidthChan/2){  // center too close to upper edge
    3004           0 :         regridBandwidthChan = 2*(oldNUM_CHAN - regridCenterChan);
    3005           0 :         oss << " *** Requested output SPW width too large." << endl;
    3006             :       } 
    3007             :       
    3008           0 :       if(regridChanWidth < 1.){
    3009           0 :         regridChanWidthChan = 1;
    3010             :       }
    3011           0 :       else if(regridChanWidth > Double(regridBandwidthChan)){
    3012           0 :         regridChanWidthChan = regridBandwidthChan; // i.e. SPW = a single channel
    3013           0 :         oss << " *** Requested output channel width too large. Adjusted to maximum possible value." << endl;
    3014             :       }
    3015             :       else { // valid input
    3016           0 :         regridChanWidthChan = (Int) floor(regridChanWidth);
    3017           0 :         if(nchan>0){
    3018           0 :           regridBandwidthChan = nchan * regridChanWidthChan;
    3019             :         }
    3020             :       }
    3021             :       
    3022           0 :       if(regridBandwidthChan != floor(regridBandwidth)){
    3023           0 :         oss << " *** Output SPW width set to " << regridBandwidthChan << " original channels" << endl;
    3024           0 :         oss << "     in an attempt to keep center of output SPW close to center of requested SPW." << endl;
    3025             :       } 
    3026             :       
    3027             :       // calculate newChanLoBound and newChanHiBound from regridCenterChan, regridBandwidthChan, and regridChanWidthChan
    3028           0 :       Int bwLowerEndChan = regridCenterChan - regridBandwidthChan/2;
    3029           0 :       Int bwUpperEndChan = bwLowerEndChan + regridBandwidthChan - 1;
    3030           0 :       Int numNewChanDown = 0;
    3031           0 :       Int numNewChanUp = 0;
    3032             :       
    3033           0 :       if(regridChanWidthChan == regridBandwidthChan){ // only one new channel
    3034           0 :         newChanLoBound.resize(1);
    3035           0 :         newChanHiBound.resize(1);
    3036           0 :         newChanLoBound[0] = transNewXin[bwLowerEndChan]-transCHAN_WIDTH[bwLowerEndChan]/2.;
    3037           0 :         newChanHiBound[0] = transNewXin[bwUpperEndChan]+transCHAN_WIDTH[bwUpperEndChan]/2.;
    3038           0 :         numNewChanUp = 1;
    3039             :       }
    3040             :       else { // have more than one new channel
    3041             :         // Need to accomodate the possibility that the original channels are
    3042             :         // not contiguous!
    3043             : 
    3044             :         // the numbers of the Channels from which the lower bounds will be taken for the new channels 
    3045           0 :         vector<Int> loNCBup;
    3046             :         // starting from the central channel going up
    3047           0 :         vector<Int> hiNCBup; // the numbers of the Channels from which the high
    3048             :                              // bounds will be taken for the new channels
    3049             :         // starting from the central channel going up
    3050           0 :         vector<Int> loNCBdown; // the numbers of the Channels from which the
    3051             :                                // lower bounds will be taken for the new
    3052             :                                // channels
    3053             :         // starting from the central channel going down
    3054           0 :         vector<Int> hiNCBdown; // the numbers of the Channels from which the
    3055             :                                // high bounds will be taken for the new
    3056             :                                // channels
    3057             :         // starting from the central channel going down
    3058             :         //    Want to keep the center of the center channel at the center of
    3059             :         //    the new center channel if the bandwidth is an odd multiple of the
    3060             :         //    new channel width 
    3061             :         //    otherwise the center channel is the lower edge of the new center channel
    3062             :         Int startChan;
    3063           0 :         lDouble tnumChan = regridBandwidthChan/regridChanWidthChan;
    3064           0 :         if((Int)tnumChan % 2 != 0 ){
    3065             :           // odd multiple 
    3066           0 :           startChan = regridCenterChan-regridChanWidthChan/2;
    3067             :         }
    3068             :         else{
    3069           0 :           startChan = regridCenterChan;
    3070             :         }
    3071           0 :         for(Int i=startChan; i<=bwUpperEndChan; i+=regridChanWidthChan){ // upper half
    3072           0 :           loNCBup.push_back(i);
    3073           0 :           if(i+regridChanWidthChan-1<=bwUpperEndChan){
    3074             :             // can go one more normal step up
    3075           0 :             hiNCBup.push_back(i+regridChanWidthChan-1);
    3076             :           }
    3077             :           else{
    3078             :             // create narrower channels at the edges if necessary
    3079           0 :             oss << " *** Last channel at upper edge of new SPW made only " << bwUpperEndChan-i+1 
    3080           0 :                 << " original channels wide to fit given total bandwidth." << endl;
    3081           0 :             hiNCBup.push_back(bwUpperEndChan);
    3082             :           }
    3083             :         }
    3084             : 
    3085             :         // lower half
    3086           0 :         for(Int i=startChan - 1; i>=bwLowerEndChan; i-=regridChanWidthChan){ 
    3087           0 :           hiNCBdown.push_back(i);
    3088           0 :           if(i-regridChanWidthChan+1>=bwLowerEndChan){
    3089             :             // can go one more normal step down
    3090           0 :             loNCBdown.push_back(i-regridChanWidthChan+1);
    3091             :           }
    3092             :           else{
    3093             :             // create narrower channels at the edges if necessary
    3094           0 :             oss << " *** First channel at lower edge of new SPW made only " << i-bwLowerEndChan+1 
    3095           0 :                 << " original channels wide to fit given total bandwidth." << endl;
    3096           0 :             loNCBdown.push_back(bwLowerEndChan);
    3097             :           }
    3098             :         }
    3099             : 
    3100             :         // the number of channels below the central one
    3101           0 :         numNewChanDown = loNCBdown.size();
    3102             : 
    3103             :         // the number of channels above and including the central one
    3104           0 :         numNewChanUp = loNCBup.size();
    3105             : 
    3106           0 :         newChanLoBound.resize(numNewChanDown+numNewChanUp);
    3107           0 :         newChanHiBound.resize(numNewChanDown+numNewChanUp);
    3108           0 :         for(Int i=0; i<numNewChanDown; i++){
    3109           0 :           Int k = numNewChanDown-i-1; // need to assign in reverse
    3110           0 :           newChanLoBound[i] = transNewXin[loNCBdown[k]] -
    3111           0 :             transCHAN_WIDTH[loNCBdown[k]]/2.; 
    3112           0 :           newChanHiBound[i] = transNewXin[hiNCBdown[k]] +
    3113           0 :             transCHAN_WIDTH[hiNCBdown[k]]/2.;
    3114             :         }
    3115           0 :         for(Int i=0; i<numNewChanUp; i++){
    3116           0 :           newChanLoBound[i+numNewChanDown] = transNewXin[loNCBup[i]] -
    3117           0 :             transCHAN_WIDTH[loNCBup[i]]/2.;
    3118           0 :           newChanHiBound[i+numNewChanDown] = transNewXin[hiNCBup[i]] +
    3119           0 :             transCHAN_WIDTH[hiNCBup[i]]/2.;
    3120             :         }
    3121           0 :       } // end if 
    3122             : 
    3123           0 :       oss << " New channels defined based on original channels" << endl
    3124           0 :           << " Central channel contains original channel " <<  regridCenterChan << endl 
    3125           0 :           << " Channel width = " << regridChanWidthChan
    3126           0 :           << " original channels" << endl
    3127           0 :           << " Total width of SPW = " <<  regridBandwidthChan << " original channels == " 
    3128           0 :           << numNewChanDown + numNewChanUp << " new channels" << endl;
    3129             : 
    3130           0 :       uInt nc = newChanLoBound.size();
    3131           0 :       oss << " Total width of SPW (in output frame) = " << newChanHiBound[nc-1] - newChanLoBound[0] 
    3132           0 :           << " Hz" << endl;
    3133           0 :       oss << " Lower edge = " << newChanLoBound[0] << " Hz,"
    3134           0 :           << " upper edge = " << newChanHiBound[nc-1] << " Hz" << endl;
    3135             : 
    3136           0 :       if(isDescending){ 
    3137           0 :         Vector<Double> tempL, tempU;
    3138           0 :         tempL.assign(newChanLoBound);
    3139           0 :         tempU.assign(newChanHiBound);
    3140           0 :         for(uInt i=0; i<nc; i++){
    3141           0 :           newChanLoBound(i) = tempL(nc-1-i);
    3142           0 :           newChanHiBound(i) = tempU(nc-1-i);
    3143             :         }
    3144           0 :       }
    3145             : 
    3146           0 :       message = oss.str();
    3147             : 
    3148           0 :       return true;
    3149             :     }
    3150             :     else { // we operate on real numbers /////////////////
    3151             :       // first transform them to frequencies
    3152           0 :       lDouble regridCenterF = -1.; // initialize as "not set"
    3153           0 :       lDouble regridBandwidthF = -1.;
    3154           0 :       lDouble regridChanWidthF = -1.;
    3155             : 
    3156           0 :       if(regridQuant=="vrad"){ ///////////////
    3157             :         // radio velocity ...
    3158             :         // need restfrq
    3159           0 :         if(regridVeloRestfrq<-1E30){ // means "not set"
    3160           0 :           oss << "Parameter \"restfreq\" needs to be set if regrid_quantity==vrad. Cannot proceed with regridSpw ..."; 
    3161           0 :           message = oss.str();
    3162           0 :           return false;
    3163             :         }
    3164           0 :         else if(regridVeloRestfrq < 0. || regridVeloRestfrq > 1E30){
    3165           0 :           oss << "Parameter \"restfreq\" value " << regridVeloRestfrq << " is invalid.";
    3166           0 :           message = oss.str();
    3167           0 :           return false;
    3168             :         }         
    3169             :         lDouble regridCenterVel; 
    3170           0 :         if(regridCenter>-C::c){
    3171             :           // (we deal with invalid values later)
    3172           0 :           if(centerIsStart){
    3173             :             Double tcWidth;
    3174           0 :             if(regridChanWidth > 0.){
    3175           0 :               tcWidth = regridChanWidth;
    3176             :             }
    3177             :             else{
    3178           0 :               tcWidth = vrad(transNewXin[0]-transCHAN_WIDTH[0]/2.,regridVeloRestfrq)
    3179           0 :                 - vrad(transNewXin[0]+transCHAN_WIDTH[0]/2.,regridVeloRestfrq);
    3180             :             }
    3181           0 :             if(startIsEnd){ // start is the center of the last channel (in freq)
    3182           0 :               regridCenter -= tcWidth/2.;
    3183             :             }
    3184             :             else{ // start is the center of the first channel (in freq)
    3185           0 :               regridCenter += tcWidth/2.;
    3186             :             }
    3187             :           }
    3188             : 
    3189           0 :           regridCenterF = freq_from_vrad(regridCenter,regridVeloRestfrq);
    3190             : 
    3191           0 :           regridCenterVel = regridCenter;
    3192             :         }
    3193             :         else{ // center was not specified
    3194           0 :           regridCenterF = (transNewXin[0]+transNewXin[oldNUM_CHAN-1])/2.;
    3195           0 :           regridCenterVel = vrad(regridCenterF,regridVeloRestfrq);
    3196           0 :           centerIsStart = false;
    3197             :         }
    3198           0 :         if(nchan>0){
    3199           0 :           if(regridChanWidth > 0.){
    3200           0 :             lDouble chanUpperEdgeF = freq_from_vrad(regridCenterVel - regridChanWidth/2.,
    3201             :                                                    regridVeloRestfrq);
    3202           0 :             regridChanWidthF = 2.* (chanUpperEdgeF - regridCenterF); 
    3203             :           }
    3204             :           else{ // take channel width from first channel
    3205           0 :             regridChanWidthF = transCHAN_WIDTH[0];
    3206             :           }
    3207           0 :           regridBandwidthF = nchan*regridChanWidthF;
    3208             :           // can convert start to center
    3209           0 :           if(centerIsStart){
    3210           0 :             if(startIsEnd){
    3211           0 :               regridCenterF = regridCenterF - regridBandwidthF/2.;
    3212             :             }
    3213             :             else{
    3214           0 :               regridCenterF = regridCenterF + regridBandwidthF/2.;
    3215             :             }
    3216           0 :             centerIsStart = false;
    3217             :           }
    3218             :         }
    3219           0 :         else if(regridBandwidth > 0.){
    3220             :           // can convert start to center
    3221           0 :           if(centerIsStart){
    3222           0 :             if(startIsEnd){
    3223           0 :               regridCenterVel = regridCenter + regridBandwidth/2.;
    3224             :             }
    3225             :             else{
    3226           0 :               regridCenterVel = regridCenter - regridBandwidth/2.;
    3227             :             }
    3228           0 :             regridCenterF = freq_from_vrad(regridCenterVel,regridVeloRestfrq);
    3229           0 :             centerIsStart = false;
    3230             :           }
    3231           0 :           lDouble bwUpperEndF = freq_from_vrad(regridCenterVel - regridBandwidth/2.,
    3232             :                                               regridVeloRestfrq);
    3233           0 :           regridBandwidthF = 2.* (bwUpperEndF - regridCenterF); 
    3234             :         }
    3235           0 :         if(regridChanWidth > 0. && regridChanWidthF<0.){
    3236           0 :           lDouble chanUpperEdgeF = freq_from_vrad(regridCenterVel - regridChanWidth/2.,
    3237             :                                                   regridVeloRestfrq);
    3238           0 :           regridChanWidthF = 2.* (chanUpperEdgeF - freq_from_vrad(regridCenterVel, regridVeloRestfrq));
    3239             :         }
    3240             :       }
    3241           0 :       else if(regridQuant=="vopt"){ ///////////
    3242             :         // optical velocity ...
    3243             :         // need restfrq
    3244           0 :         if(regridVeloRestfrq < -1E30){ // means "not set"
    3245           0 :           oss << "Parameter \"restfreq\" needs to be set if regrid_quantity==vopt. Cannot proceed with regridSpw ...";
    3246           0 :           message = oss.str();
    3247           0 :           return false;
    3248             :         }
    3249           0 :         else if(regridVeloRestfrq <= 0. || regridVeloRestfrq > 1E30){
    3250           0 :           oss << "Parameter \"restfreq\" value " << regridVeloRestfrq
    3251           0 :               << " is invalid."; 
    3252           0 :           message = oss.str();
    3253           0 :           return false;
    3254             :         }
    3255             :         lDouble regridCenterVel; 
    3256           0 :         if(regridCenter > -C::c){
    3257           0 :           if(centerIsStart){
    3258             :             Double tcWidth;
    3259           0 :             if(regridChanWidth > 0.){
    3260           0 :               tcWidth = regridChanWidth;
    3261             :             }
    3262             :             else{
    3263           0 :               tcWidth = vopt(transNewXin[0]-transCHAN_WIDTH[0]/2.,regridVeloRestfrq)
    3264           0 :                 - vopt(transNewXin[0]+transCHAN_WIDTH[0]/2.,regridVeloRestfrq);
    3265             :             }
    3266           0 :             if(startIsEnd){ // start is the center of the last channel (in freq)
    3267           0 :               regridCenter -= tcWidth/2.;
    3268             :             }
    3269             :             else{ // start is the center of the first channel (in freq)
    3270           0 :               regridCenter += tcWidth/2.;
    3271             :             }
    3272             :           }
    3273             :           // (we deal with invalid values later)
    3274           0 :           regridCenterF = freq_from_vopt(regridCenter,regridVeloRestfrq);
    3275           0 :           regridCenterVel = regridCenter;
    3276             :         }
    3277             :         else{ // center was not specified
    3278           0 :           regridCenterF = (transNewXin[0]-transCHAN_WIDTH[0]+transNewXin[oldNUM_CHAN-1]+transCHAN_WIDTH[oldNUM_CHAN-1])/2.;
    3279           0 :           regridCenterVel = vopt(regridCenterF,regridVeloRestfrq);
    3280           0 :           centerIsStart = false;
    3281             :         }
    3282           0 :         if(nchan>0){
    3283             :           lDouble cw;
    3284           0 :           lDouble divbytwo = 0.5;
    3285           0 :           if(centerIsStart){
    3286           0 :             divbytwo = 1.;
    3287             :           }
    3288           0 :           if(regridChanWidth > 0.){
    3289           0 :             cw = regridChanWidth;
    3290             :           }
    3291             :           else{ // determine channel width from first channel
    3292           0 :             lDouble upEdge = vopt(transNewXin[0]-transCHAN_WIDTH[0],regridVeloRestfrq);
    3293           0 :             lDouble loEdge = vopt(transNewXin[0]+transCHAN_WIDTH[0],regridVeloRestfrq);
    3294           0 :             cw = abs(upEdge-loEdge); 
    3295             :           }
    3296           0 :           lDouble bwUpperEndF = 0.;
    3297           0 :           if(centerIsStart && !startIsEnd){ // start is end in velocity
    3298           0 :             bwUpperEndF = freq_from_vopt(regridCenterVel - (lDouble)nchan*cw*divbytwo,
    3299             :                                          regridVeloRestfrq);
    3300             :           }
    3301             :           else{
    3302           0 :             bwUpperEndF = freq_from_vopt(regridCenterVel + (lDouble)nchan*cw*divbytwo,
    3303             :                                          regridVeloRestfrq);
    3304             :           }
    3305           0 :           regridBandwidthF = abs(bwUpperEndF-regridCenterF)/divbytwo; 
    3306             :           // can convert start to center
    3307           0 :           if(centerIsStart){
    3308           0 :             if(startIsEnd){
    3309           0 :               regridCenterVel = regridCenterVel + (lDouble)nchan*cw/2.;
    3310             :             }
    3311             :             else{
    3312           0 :               regridCenterVel = regridCenterVel - (lDouble)nchan*cw/2.;
    3313             :             }
    3314           0 :             regridCenterF = freq_from_vopt(regridCenterVel,regridVeloRestfrq);
    3315           0 :             centerIsStart = false;
    3316             :           }
    3317           0 :           nchan=0; // indicate that nchan should not be used in the following
    3318             :         }
    3319           0 :         else if(regridBandwidth > 0.){
    3320             :           // can convert start to center
    3321           0 :           if(centerIsStart){
    3322           0 :             if(startIsEnd){
    3323           0 :               regridCenterVel = regridCenter + regridBandwidth/2.;
    3324             :             }
    3325             :             else{
    3326           0 :               regridCenterVel = regridCenter - regridBandwidth/2.;
    3327             :             }
    3328           0 :             regridCenterF = freq_from_vopt(regridCenterVel,regridVeloRestfrq);
    3329           0 :             centerIsStart = false;
    3330             :           }
    3331           0 :           lDouble bwUpperEndF =  freq_from_vopt(regridCenterVel - regridBandwidth/2.,
    3332             :                                                regridVeloRestfrq);
    3333           0 :           regridBandwidthF = 2.* (bwUpperEndF- regridCenterF); 
    3334             :         }
    3335           0 :         if(regridChanWidth > 0. && regridChanWidthF<0.){
    3336           0 :           lDouble chanUpperEdgeF = freq_from_vopt(regridCenterVel - regridChanWidth/2.,
    3337             :                                                  regridVeloRestfrq);
    3338           0 :           regridChanWidthF = 2.* (chanUpperEdgeF - freq_from_vopt(regridCenterVel,regridVeloRestfrq)); 
    3339             :         }
    3340             :       } 
    3341           0 :       else if(regridQuant=="freq"){ ////////////////////////
    3342           0 :         if(width>0){ // width parameter overrides regridChanWidth
    3343           0 :           regridChanWidth = width*transCHAN_WIDTH[0];
    3344             :         }
    3345           0 :         if(start>=0){
    3346           0 :           Int firstChan = start;
    3347           0 :           if(start >= (Int)transNewXin.size()){
    3348           0 :             oss << " *** Parameter start exceeds total number of channels which is "
    3349           0 :                 << transNewXin.size() << ". Set to 0." << endl;
    3350           0 :             firstChan = 0;
    3351           0 :             startIsEnd = false;
    3352             :           }
    3353           0 :           if(startIsEnd){
    3354           0 :             regridCenter = transNewXin[firstChan]+transCHAN_WIDTH[firstChan]/2.;
    3355             :           }
    3356             :           else{
    3357           0 :             regridCenter = transNewXin[firstChan]-transCHAN_WIDTH[firstChan]/2.;
    3358             :           }
    3359           0 :           centerIsStart = true;
    3360             :         }
    3361             :         else{
    3362           0 :           if(centerIsStart){ // start is the center of the first channel
    3363             :             Double tcWidth;
    3364           0 :             if(regridChanWidth > 0.){ 
    3365           0 :               tcWidth = regridChanWidth;
    3366             :             }
    3367             :             else{
    3368           0 :               tcWidth = transCHAN_WIDTH[0];
    3369             :             }
    3370           0 :             if(startIsEnd){
    3371           0 :               regridCenter += tcWidth/2.;
    3372             :             }
    3373             :             else{
    3374           0 :               regridCenter -= tcWidth/2.;
    3375             :             }
    3376             :           }
    3377             :         }
    3378           0 :         regridCenterF = regridCenter;
    3379           0 :         regridBandwidthF = regridBandwidth;
    3380           0 :         regridChanWidthF = regridChanWidth;
    3381             :       }
    3382           0 :       else if(regridQuant=="wave"){ ///////////////////////
    3383             :         // wavelength ...
    3384             :         lDouble regridCenterWav; 
    3385           0 :         if(regridCenter > 0.){
    3386           0 :           if(centerIsStart){
    3387             :             Double tcWidth;
    3388           0 :             if(regridChanWidth > 0.){
    3389           0 :               tcWidth = regridChanWidth;
    3390             :             }
    3391             :             else{
    3392           0 :               tcWidth = lambda(transNewXin[0]-transCHAN_WIDTH[0]/2.)
    3393           0 :                 - lambda(transNewXin[0]+transCHAN_WIDTH[0]/2.);
    3394             :             }
    3395           0 :             if(startIsEnd){ // start is the center of the last channel (in freq)
    3396           0 :               regridCenter -= tcWidth/2.;
    3397             :             }
    3398             :             else{ // start is the center of the first channel (in freq)
    3399           0 :               regridCenter += tcWidth/2.;
    3400             :             }
    3401             :           }
    3402           0 :           regridCenterF = freq_from_lambda(regridCenter); 
    3403           0 :           regridCenterWav = regridCenter;
    3404             :         }
    3405             :         else{ // center was not specified
    3406           0 :           regridCenterF = (transNewXin[0] + transNewXin[oldNUM_CHAN-1])/2.;
    3407           0 :           regridCenterWav = lambda(regridCenterF);
    3408           0 :           centerIsStart = false;
    3409             :         }
    3410           0 :         if(nchan>0){
    3411             :           lDouble cw;
    3412           0 :           lDouble divbytwo = 0.5;
    3413           0 :           if(centerIsStart){
    3414           0 :             divbytwo = 1.;
    3415             :           }
    3416           0 :           if(regridChanWidth > 0.){
    3417           0 :             cw = regridChanWidth;
    3418             :           }
    3419             :           else{ // determine channel width from first channel
    3420           0 :             lDouble upEdge = lambda(transNewXin[0]-transCHAN_WIDTH[0]);
    3421           0 :             lDouble loEdge = lambda(transNewXin[0]+transCHAN_WIDTH[0]);
    3422           0 :             cw = abs(upEdge-loEdge); 
    3423             :           }
    3424           0 :           lDouble bwUpperEndF = 0.;
    3425           0 :           if(centerIsStart && !startIsEnd){
    3426           0 :             bwUpperEndF = freq_from_lambda(regridCenterWav - (lDouble)nchan*cw*divbytwo);           
    3427             :           }
    3428             :           else{
    3429           0 :             bwUpperEndF = freq_from_lambda(regridCenterWav + (lDouble)nchan*cw*divbytwo);
    3430             :           }
    3431           0 :           regridBandwidthF = (bwUpperEndF-regridCenterF)/divbytwo; 
    3432             :           // can convert start to center
    3433           0 :           if(centerIsStart){
    3434           0 :             if(startIsEnd){
    3435           0 :               regridCenterWav = regridCenterWav + (lDouble)nchan*cw/2.;
    3436             :             }
    3437             :             else{
    3438           0 :               regridCenterWav = regridCenterWav - (lDouble)nchan*cw/2.;
    3439             :             }
    3440           0 :             regridCenterF = freq_from_lambda(regridCenterWav);
    3441           0 :             centerIsStart = false;
    3442             :           }
    3443           0 :           nchan=0; // indicate that nchan should not be used in the following
    3444             :         }
    3445           0 :         else if(regridBandwidth > 0. && regridBandwidth/2. < regridCenterWav){
    3446             :           // can convert start to center
    3447           0 :           if(centerIsStart){
    3448           0 :             if(startIsEnd){
    3449           0 :               regridCenterWav = regridCenter + regridBandwidth/2.;
    3450             :             }
    3451             :             else{
    3452           0 :               regridCenterWav = regridCenter - regridBandwidth/2.;
    3453             :             }
    3454           0 :             regridCenterF = freq_from_lambda(regridCenterWav);
    3455           0 :             centerIsStart = false;
    3456             :           }
    3457           0 :           lDouble bwUpperEndF =  lambda(regridCenterWav - regridBandwidth/2.);
    3458           0 :           regridBandwidthF = 2.* (bwUpperEndF - regridCenterF); 
    3459             :         }
    3460           0 :         if(regridChanWidth>0. && regridChanWidth/2.< regridCenterWav){
    3461           0 :           lDouble chanUpperEdgeF =  lambda(regridCenterWav - regridChanWidth/2.);
    3462           0 :           regridChanWidthF = 2.* (chanUpperEdgeF - regridCenterF); 
    3463             :         }
    3464             :       }
    3465             :       else{
    3466           0 :         oss << "Invalid value " << regridQuant << " for parameter \"mode\".";
    3467           0 :         message = oss.str();
    3468           0 :         return false;
    3469             :       }
    3470             :       // (transformation of regrid parameters to frequencies completed)
    3471             :       
    3472             :       // then determine the actually possible parameters
    3473             :       lDouble theRegridCenterF;
    3474             :       lDouble theRegridBWF;
    3475             :       lDouble theCentralChanWidthF;
    3476             :       
    3477             :       // for vrad and vopt also need to keep this adjusted value
    3478           0 :       lDouble theChanWidthX = -1.;
    3479             : 
    3480           0 :       if(regridCenterF < 0.){ //  means "not set"
    3481             :         // keep regrid center as it is in the data
    3482           0 :         theRegridCenterF = (transNewXin[0] - transCHAN_WIDTH[0]/2. 
    3483           0 :                             + transNewXin[oldNUM_CHAN-1]+transCHAN_WIDTH[oldNUM_CHAN-1]/2.)/2.;
    3484           0 :         centerIsStart = false;
    3485             :       }
    3486             :       else { // regridCenterF was set
    3487             :         // keep center in limits
    3488           0 :         theRegridCenterF = regridCenterF;
    3489           0 :         if( (theRegridCenterF - (transNewXin[oldNUM_CHAN-1]+transCHAN_WIDTH[oldNUM_CHAN-1]/2.)) > 1. ){ // 1 Hz tolerance
    3490           0 :           oss << "*** Requested center of SPW " << theRegridCenterF << " Hz is too large by "
    3491           0 :               << theRegridCenterF - transNewXin[oldNUM_CHAN-1]+transCHAN_WIDTH[oldNUM_CHAN-1]/2. << " Hz\n";
    3492           0 :           theRegridCenterF = transNewXin[oldNUM_CHAN-1]+transCHAN_WIDTH[oldNUM_CHAN-1]/2.;
    3493           0 :           oss << "*** Reset to maximum possible value " <<  theRegridCenterF  << " Hz";
    3494             :         }
    3495           0 :         else if( theRegridCenterF < (transNewXin[0]-transCHAN_WIDTH[0]/2.)  ){ 
    3496           0 :           Double diff = (transNewXin[0]-transCHAN_WIDTH[0]/2.) - theRegridCenterF;
    3497             :           // cope with numerical accuracy problems
    3498           0 :           if(diff>1.){
    3499           0 :             oss << "*** Requested center of SPW " << theRegridCenterF << " Hz is smaller than minimum possible value";
    3500           0 :             oss << " by " << diff << " Hz";
    3501             :           }  
    3502           0 :           theRegridCenterF = transNewXin[0]-transCHAN_WIDTH[0]/2.;
    3503           0 :           if(diff>1.){
    3504           0 :             oss << "\n*** Reset to minimum possible value " <<  theRegridCenterF  << " Hz";
    3505             :           }
    3506             :         }
    3507             :       }
    3508           0 :       if(regridBandwidthF<=0.|| nchan!=0){ // "not set" or use nchan instead
    3509             :         // keep bandwidth as is
    3510           0 :         theRegridBWF = transNewXin[oldNUM_CHAN-1] - transNewXin[0] 
    3511           0 :           + transCHAN_WIDTH[0]/2. + transCHAN_WIDTH[oldNUM_CHAN-1]/2.;
    3512           0 :         if(nchan!=0){ // use nchan parameter if available
    3513           0 :           if(nchan<0){
    3514           0 :             if(regridQuant=="freq" || regridQuant=="vrad"){ // i.e. equidistant in freq
    3515             :               // define via width of first channel to avoid numerical problems
    3516           0 :               if(regridChanWidthF <= 0.){ // channel width not set
    3517           0 :                 theRegridBWF = transCHAN_WIDTH[0]*floor((theRegridBWF+transCHAN_WIDTH[0]*0.01)/transCHAN_WIDTH[0]);
    3518             :               }
    3519             :               else{
    3520           0 :                 theRegridBWF = regridChanWidthF*floor((theRegridBWF+regridChanWidthF*0.01)/regridChanWidthF);
    3521             :               }
    3522             :             }
    3523             :           }
    3524           0 :           else if(regridChanWidthF <= 0.){ // channel width not set
    3525           0 :             theRegridBWF = transCHAN_WIDTH[0]*nchan;
    3526             :           }
    3527             :           else{ 
    3528           0 :             theRegridBWF = regridChanWidthF*nchan;
    3529             :           }
    3530             : 
    3531           0 :           if(regridCenterF <= 0.|| regridCenter <-C::c ){ // center was not set by user but calculated
    3532             :             // need to update
    3533           0 :             theRegridCenterF = transNewXin[0] - transCHAN_WIDTH[0]/2. + theRegridBWF/2.;
    3534           0 :             centerIsStart = false;
    3535             :           }
    3536           0 :           else if(nchan<0){ // center but not nchan was set by user
    3537             :             // verify that the bandwidth is correct
    3538           0 :             if(centerIsStart){
    3539           0 :               if(startIsEnd){
    3540           0 :                 theRegridBWF = theRegridCenterF - transNewXin[0] + transCHAN_WIDTH[0]/2.;
    3541             :               }
    3542             :               else{ // start is start
    3543           0 :                 theRegridBWF = transNewXin[oldNUM_CHAN-1] + transCHAN_WIDTH[oldNUM_CHAN-1]/2. - theRegridCenterF;
    3544             :               }
    3545           0 :               if(regridQuant=="freq" || regridQuant=="vrad"){ // i.e. equidistant in freq
    3546             :                 // define via width of first channel to avoid numerical problems
    3547           0 :                 if(regridChanWidthF <= 0.){ // channel width not set
    3548           0 :                   theRegridBWF = transCHAN_WIDTH[0]*floor((theRegridBWF+transCHAN_WIDTH[0]*0.01)/transCHAN_WIDTH[0]);
    3549             :                 }
    3550             :                 else{
    3551           0 :                   theRegridBWF = regridChanWidthF*floor((theRegridBWF+regridChanWidthF*0.01)/regridChanWidthF);
    3552             :                 }
    3553             :               }
    3554             :             }
    3555             :             else{ // center is center
    3556           0 :               theRegridBWF = 2. * min((Double)(theRegridCenterF - transNewXin[0] - transCHAN_WIDTH[0]), 
    3557           0 :                                       (Double)(transNewXin[oldNUM_CHAN-1] + transCHAN_WIDTH[oldNUM_CHAN-1] 
    3558           0 :                                                - theRegridCenterF));
    3559             :             }
    3560             :           }
    3561             :         }
    3562             :         // now can convert start to center
    3563           0 :         if(centerIsStart){
    3564           0 :           if(startIsEnd){
    3565           0 :             theRegridCenterF = theRegridCenterF - theRegridBWF/2.;
    3566             :           }
    3567             :           else{
    3568           0 :             theRegridCenterF = theRegridCenterF + theRegridBWF/2.;
    3569             :           }
    3570           0 :           centerIsStart = false;
    3571             :         }
    3572             :       }
    3573             :       else { // regridBandwidthF was set
    3574             :         // determine actually possible bandwidth:
    3575             :         // width will be truncated to the maximum width possible symmetrically
    3576             :         // around the value given by "regrid_center"
    3577           0 :         theRegridBWF = regridBandwidthF;
    3578             :         // now can convert start to center
    3579           0 :         if(centerIsStart){
    3580           0 :           if(startIsEnd){
    3581           0 :             theRegridCenterF = theRegridCenterF - theRegridBWF/2.;
    3582             :           }
    3583             :           else{
    3584           0 :             theRegridCenterF = theRegridCenterF + theRegridBWF/2.;
    3585             :           }
    3586           0 :           centerIsStart = false;
    3587             :         }
    3588             :         {
    3589           0 :           Double rangeTol = 1.; // Hz
    3590           0 :           if((regridQuant=="vopt" || regridQuant=="wave")){ // i.e. if the center is the center w.r.t. wavelength
    3591           0 :             rangeTol = transCHAN_WIDTH[0];
    3592             :           }
    3593           0 :           if((theRegridCenterF + theRegridBWF / 2.) -  (transNewXin[oldNUM_CHAN-1] + transCHAN_WIDTH[oldNUM_CHAN-1]/2.) > rangeTol ){
    3594           0 :             oss << " *** Input spectral window exceeds upper end of original window. Adjusting to max. possible value." << endl;    
    3595           0 :             theRegridBWF = min((Double)fabs(transNewXin[oldNUM_CHAN-1] + transCHAN_WIDTH[oldNUM_CHAN-1]/2. - theRegridCenterF),
    3596           0 :                                (Double)fabs(theRegridCenterF - transNewXin[0] + transCHAN_WIDTH[0]/2.)) * 2.;
    3597           0 :             if(theRegridBWF<transCHAN_WIDTH[0]){
    3598           0 :               theRegridCenterF = (transNewXin[0]+transCHAN_WIDTH[oldNUM_CHAN-1]+transCHAN_WIDTH[oldNUM_CHAN-1]/2.-transCHAN_WIDTH[0]/2.)/2.;
    3599           0 :               theRegridBWF = transCHAN_WIDTH[oldNUM_CHAN-1]-transNewXin[0]
    3600           0 :                 +transCHAN_WIDTH[oldNUM_CHAN-1]/2. + transCHAN_WIDTH[0]/2.;
    3601             :             }
    3602             :           }
    3603           0 :           if((theRegridCenterF - theRegridBWF/2.) - (transNewXin[0] - transCHAN_WIDTH[0]/2.) < -rangeTol ){
    3604           0 :             oss << " *** Input spectral window exceeds lower end of original window. Adjusting to max. possible value." << endl;
    3605           0 :             theRegridBWF = min((Double)fabs(transNewXin[oldNUM_CHAN-1] + transCHAN_WIDTH[oldNUM_CHAN-1]/2. - theRegridCenterF),
    3606           0 :                                (Double)fabs(theRegridCenterF - transNewXin[0] + transCHAN_WIDTH[0]/2.)) * 2.;
    3607           0 :             if(theRegridBWF<transCHAN_WIDTH[0]){
    3608           0 :               theRegridCenterF = (transNewXin[0]+transCHAN_WIDTH[oldNUM_CHAN-1]+transCHAN_WIDTH[oldNUM_CHAN-1]/2.-transCHAN_WIDTH[0]/2.)/2.;
    3609           0 :               theRegridBWF = transCHAN_WIDTH[oldNUM_CHAN-1]-transNewXin[0]
    3610           0 :                 +transCHAN_WIDTH[oldNUM_CHAN-1]/2. + transCHAN_WIDTH[0]/2.;
    3611             :             }
    3612             :           }
    3613             :         }
    3614             :       }
    3615           0 :       if(regridChanWidthF <= 0.){ // "not set"
    3616           0 :         if(nchan!=0 || centerIsStartC){ // use first channel
    3617           0 :           theCentralChanWidthF = transCHAN_WIDTH[0];
    3618             :         }
    3619             :         else{
    3620             :           // keep channel width similar to the old one 
    3621           0 :           theCentralChanWidthF = transCHAN_WIDTH[oldNUM_CHAN/2]; // use channel width from
    3622             :                                                                  // near central channel
    3623             :         }
    3624             :       }
    3625             :       else { // regridChanWidthF was set
    3626             :         // keep in limits
    3627           0 :         theCentralChanWidthF = regridChanWidthF;
    3628           0 :         if(theCentralChanWidthF>theRegridBWF){ // too large => make a single channel
    3629           0 :           theCentralChanWidthF = theRegridBWF;
    3630           0 :           oss << " *** Requested new channel width exceeds defined SPW width." << endl
    3631           0 :               << "     Creating a single channel with the defined SPW width." << endl;
    3632             :         }
    3633           0 :         else if(theCentralChanWidthF<transCHAN_WIDTH[0]){ // check if too small
    3634             :           // determine smallest channel width
    3635           0 :           lDouble smallestChanWidth = 1E30;
    3636           0 :           Int ii = 0;
    3637           0 :           for(Int i=0; i<oldNUM_CHAN; i++){
    3638           0 :             if(transCHAN_WIDTH[i] < smallestChanWidth){ 
    3639           0 :               smallestChanWidth = transCHAN_WIDTH[i];
    3640           0 :               ii = i;
    3641             :             }
    3642             :           }
    3643           0 :           if(theCentralChanWidthF < smallestChanWidth - 1.){ // 1 Hz tolerance to cope with numerical accuracy problems
    3644           0 :             oss << " *** Requested new channel width is smaller than smallest original channel width" << endl;
    3645           0 :             oss << "     which is " << smallestChanWidth << " Hz" << endl;
    3646           0 :             if(regridQuant == "vrad"){
    3647           0 :               oss << "     or " << (vrad(transNewXin[ii],regridVeloRestfrq) 
    3648           0 :                                     - vrad(transNewXin[ii]+transCHAN_WIDTH[ii]/2.,regridVeloRestfrq)) * 2. << " m/s";
    3649             :             }
    3650           0 :             if(regridQuant == "vopt"){
    3651           0 :               oss << "     or " << (vopt(transNewXin[ii],regridVeloRestfrq) 
    3652           0 :                                     - vopt(transNewXin[ii]+transCHAN_WIDTH[ii]/2.,regridVeloRestfrq)) * 2. << " m/s";
    3653             :             }
    3654           0 :             message = oss.str();
    3655           0 :             return false;  
    3656             :           }
    3657             :           else { // input channel width was OK, memorize
    3658           0 :             theChanWidthX = regridChanWidth;
    3659             :           }
    3660             :         }           
    3661             :       }
    3662             : 
    3663           0 :       oss << " Channels equidistant in " << regridQuant << endl
    3664           0 :           << " Central frequency (in output frame) = " << theRegridCenterF
    3665           0 :           << " Hz";
    3666           0 :       if(regridQuant == "vrad"){
    3667           0 :         oss << " == " << vrad(theRegridCenterF, regridVeloRestfrq) << " m/s radio velocity";
    3668             :       }
    3669           0 :       else if(regridQuant == "vopt"){
    3670           0 :         oss << " == " << vopt(theRegridCenterF, regridVeloRestfrq) << " m/s optical velocity";
    3671             :       }      
    3672           0 :       else if(regridQuant == "wave"){
    3673           0 :         oss << " == " << lambda(theRegridCenterF) << " m wavelength";
    3674             :       }
    3675           0 :       oss << endl;
    3676             : 
    3677           0 :       if(isDescending){
    3678           0 :         oss << " Channel central frequency is decreasing with increasing channel number." << endl;
    3679             :       }
    3680             : 
    3681           0 :       oss << " Width of central channel (in output frame) = "
    3682           0 :           << theCentralChanWidthF << " Hz";
    3683           0 :       if(regridQuant == "vrad"){
    3684           0 :         oss << " == " << vrad(theRegridCenterF - theCentralChanWidthF, regridVeloRestfrq) 
    3685           0 :           -  vrad(theRegridCenterF, regridVeloRestfrq) << " m/s radio velocity";
    3686             :       }
    3687           0 :       else if(regridQuant == "vopt"){
    3688           0 :         oss << " == " << vopt(theRegridCenterF - theCentralChanWidthF, regridVeloRestfrq) 
    3689           0 :           - vopt(theRegridCenterF, regridVeloRestfrq) << " m/s optical velocity";
    3690             :       }      
    3691           0 :       else if(regridQuant == "wave"){
    3692           0 :         oss << " == " << lambda(theRegridCenterF - theCentralChanWidthF) - lambda(theRegridCenterF) << " m wavelength";
    3693             :       }
    3694           0 :       oss << endl;
    3695             :        
    3696             :       // now calculate newChanLoBound, and newChanHiBound from
    3697             :       // theRegridCenterF, theRegridBWF, theCentralChanWidthF
    3698           0 :       vector<lDouble> loFBup; // the lower bounds for the new channels 
    3699             :                              // starting from the central channel going up
    3700           0 :       vector<lDouble> hiFBup; // the lower bounds for the new channels 
    3701             :                              // starting from the central channel going up
    3702           0 :       vector<lDouble> loFBdown; // the lower bounds for the new channels
    3703             :                                // starting from the central channel going down
    3704           0 :       vector<lDouble> hiFBdown; // the lower bounds for the new channels
    3705             :                                // starting from the central channel going down
    3706             :       
    3707           0 :       lDouble edgeTolerance = theCentralChanWidthF*0.01; // needed to avoid numerical accuracy problems
    3708             : 
    3709           0 :       if(regridQuant=="vrad"){
    3710             :         // regridding in radio velocity ...
    3711             :         
    3712             :         // create freq boundaries equidistant and contiguous in radio velocity
    3713           0 :         lDouble upperEndF = theRegridCenterF + theRegridBWF/2.;
    3714           0 :         lDouble lowerEndF = theRegridCenterF - theRegridBWF/2.;
    3715           0 :         lDouble upperEndV = vrad(upperEndF,regridVeloRestfrq);
    3716           0 :         lDouble lowerEndV = vrad(lowerEndF,regridVeloRestfrq);
    3717             :         lDouble velLo;
    3718             :         lDouble velHi;
    3719             : 
    3720             : 
    3721             :         //    Want to keep the center of the center channel at the center of
    3722             :         //    the new center channel if the bandwidth is an odd multiple of the
    3723             :         //    new channel width,
    3724             :         //    otherwise the center channel is the lower edge of the new center channel
    3725           0 :         lDouble tnumChan = floor((theRegridBWF+edgeTolerance)/theCentralChanWidthF);
    3726           0 :         if((Int)tnumChan % 2 != 0 ){
    3727             :           // odd multiple 
    3728           0 :           loFBup.push_back(theRegridCenterF-theCentralChanWidthF/2.);
    3729           0 :           hiFBup.push_back(theRegridCenterF+theCentralChanWidthF/2.);
    3730           0 :           loFBdown.push_back(theRegridCenterF-theCentralChanWidthF/2.);
    3731           0 :           hiFBdown.push_back(theRegridCenterF+theCentralChanWidthF/2.);
    3732             :         }
    3733             :         else{
    3734           0 :           loFBup.push_back(theRegridCenterF);
    3735           0 :           hiFBup.push_back(theRegridCenterF+theCentralChanWidthF);
    3736           0 :           loFBdown.push_back(theRegridCenterF);
    3737           0 :           hiFBdown.push_back(theRegridCenterF+theCentralChanWidthF);
    3738             :         }
    3739             : 
    3740           0 :         if(theChanWidthX<0){ // cannot use original channel width in velocity units
    3741             :           // need to calculate back from central channel width in Hz
    3742           0 :           theChanWidthX = vrad(loFBup[0],
    3743           0 :                                regridVeloRestfrq) - vrad(hiFBup[0],
    3744             :                                                          regridVeloRestfrq);
    3745             :         }
    3746             :         // calc velocity corresponding to the upper end (in freq) of the last
    3747             :         // added channel which is the lower end of the next channel
    3748           0 :         velLo = vrad(hiFBup[0],regridVeloRestfrq);
    3749             :         // calc velocity corresponding to the upper end (in freq) of the next channel
    3750           0 :         velHi = velLo - theChanWidthX; // vrad goes down as freq goes up!
    3751           0 :         while(upperEndV - theChanWidthX/10. < velHi){ // (preventing accuracy problems)
    3752             :           // calc frequency of the upper end (in freq) of the next channel
    3753           0 :           lDouble freqHi = freq_from_vrad(velHi,regridVeloRestfrq);
    3754           0 :           if(freqHi<=upperEndF+edgeTolerance){ // end of bandwidth not yet reached
    3755           0 :             loFBup.push_back(hiFBup.back());
    3756           0 :             hiFBup.push_back(freqHi);
    3757             :           }
    3758           0 :           else if(freqHi<upperEndF+edgeTolerance){ 
    3759           0 :             loFBup.push_back(hiFBup.back());
    3760           0 :             hiFBup.push_back(upperEndF);
    3761           0 :             break;
    3762             :           }
    3763             :           else{
    3764           0 :             break;
    3765             :           }
    3766             :           // calc velocity corresponding to the upper end (in freq) of the added channel
    3767           0 :           velLo = vrad(hiFBup.back(),regridVeloRestfrq);
    3768             :           // calc velocity corresponding to the upper end (in freq) of the next channel
    3769           0 :           velHi = velLo - theChanWidthX; // vrad goes down as freq goes up
    3770             :         }
    3771             : 
    3772             :         // calc velocity corresponding to the lower end (in freq) of the last
    3773             :         // added channel which is the upper end of the next channel
    3774           0 :         velHi = vrad(loFBdown[0],regridVeloRestfrq);
    3775             :         // calc velocity corresponding to the lower end (in freq) of the next channel
    3776           0 :         velLo = velHi + theChanWidthX; // vrad goes up as freq goes down!
    3777           0 :         while(velLo < lowerEndV + theChanWidthX/10.){ // (preventing accuracy problems)  
    3778             :           // calc frequency of the lower end (in freq) of the next channel
    3779           0 :           lDouble freqLo = freq_from_vrad(velLo,regridVeloRestfrq);
    3780           0 :           if(freqLo>=lowerEndF-edgeTolerance){ // end of bandwidth not yet reached
    3781           0 :             hiFBdown.push_back(loFBdown.back());
    3782           0 :             loFBdown.push_back(freqLo);
    3783             :           }
    3784           0 :           else if(freqLo>lowerEndF-edgeTolerance){ 
    3785           0 :             hiFBdown.push_back(loFBdown.back());
    3786           0 :             loFBdown.push_back(lowerEndF);
    3787           0 :             break;
    3788             :           }
    3789             :           else {
    3790           0 :             break;
    3791             :           }
    3792             :           // calc velocity corresponding to the upper end of the next channel
    3793           0 :           velHi = vrad(loFBdown.back(),regridVeloRestfrq);
    3794             :           // calc velocity corresponding to the lower end (in freq) of the next channel
    3795           0 :           velLo = velHi + theChanWidthX; // vrad goes up as freq goes down
    3796             :         }         
    3797             :       }
    3798           0 :       else if(regridQuant=="vopt"){
    3799             :         // regridding in optical velocity ...
    3800             :         
    3801             :         // create freq boundaries equidistant and contiguous in optical velocity
    3802           0 :         lDouble upperEndF = theRegridCenterF + theRegridBWF/2.;
    3803           0 :         lDouble lowerEndF = theRegridCenterF - theRegridBWF/2.;
    3804           0 :         lDouble upperEndV = vopt(upperEndF,regridVeloRestfrq);
    3805           0 :         lDouble lowerEndV = vopt(lowerEndF,regridVeloRestfrq);
    3806             :         lDouble velLo;
    3807             :         lDouble velHi;
    3808             : 
    3809             :         //    Want to keep the center of the center channel at the center of
    3810             :         //    the new center channel if the bandwidth is an odd multiple of the
    3811             :         //    new channel width,
    3812             :         //    otherwise the center channel is the lower edge of the new center
    3813             :         //    channel
    3814             : 
    3815             :         // enlarged edge tolerance since channels non-equidistant in freq
    3816           0 :         lDouble tnumChan = floor((theRegridBWF+edgeTolerance)/theCentralChanWidthF); 
    3817           0 :         if((Int)tnumChan % 2 != 0 ){
    3818             :           // odd multiple 
    3819           0 :           loFBup.push_back(theRegridCenterF-theCentralChanWidthF/2.);
    3820           0 :           hiFBup.push_back(theRegridCenterF+theCentralChanWidthF/2.);
    3821           0 :           loFBdown.push_back(theRegridCenterF-theCentralChanWidthF/2.);
    3822           0 :           hiFBdown.push_back(theRegridCenterF+theCentralChanWidthF/2.);
    3823             :         }
    3824             :         else{
    3825           0 :           loFBup.push_back(theRegridCenterF);
    3826           0 :           hiFBup.push_back(theRegridCenterF+theCentralChanWidthF);
    3827           0 :           loFBdown.push_back(theRegridCenterF);
    3828           0 :           hiFBdown.push_back(theRegridCenterF+theCentralChanWidthF);
    3829             :         }
    3830             : 
    3831           0 :         if(theChanWidthX<0){ // cannot use original channel width in velocity units
    3832             :           // need to calculate back from central channel width in Hz
    3833           0 :           theChanWidthX = vopt(loFBup[0],
    3834           0 :                                regridVeloRestfrq) - vopt(hiFBup[0],regridVeloRestfrq);
    3835             :         }
    3836             :         // calc velocity corresponding to the upper end (in freq) of the last
    3837             :         // added channel which is the lower end of the next channel
    3838           0 :         velLo = vopt(hiFBup[0],regridVeloRestfrq);
    3839             :         // calc velocity corresponding to the upper end (in freq) of the next channel
    3840           0 :         velHi = velLo - theChanWidthX; // vopt goes down as freq goes up!
    3841           0 :         while(upperEndV - velHi < theChanWidthX/10.){ // (preventing accuracy problems)
    3842             :           // calc frequency of the upper end (in freq) of the next channel
    3843           0 :           lDouble freqHi = freq_from_vopt(velHi,regridVeloRestfrq);
    3844           0 :           if(freqHi<=upperEndF+edgeTolerance){ // end of bandwidth not yet reached
    3845           0 :             loFBup.push_back(hiFBup.back());
    3846           0 :             hiFBup.push_back(freqHi);
    3847             :           }
    3848           0 :           else if(freqHi<upperEndF+edgeTolerance){ 
    3849           0 :             loFBup.push_back(hiFBup.back());
    3850           0 :             hiFBup.push_back(upperEndF);
    3851           0 :             break;
    3852             :           }
    3853             :           else{
    3854           0 :             break;
    3855             :           }
    3856             :           // calc velocity corresponding to the upper end (in freq) of the added channel
    3857           0 :           velLo = vopt(hiFBup.back(),regridVeloRestfrq);
    3858             :           // calc velocity corresponding to the upper end (in freq) of the next channel
    3859           0 :           velHi = velLo - theChanWidthX; // vopt goes down as freq goes up
    3860             :         }
    3861             : 
    3862             :         // calc velocity corresponding to the lower end (in freq) of the last
    3863             :         // added channel which is the upper end of the next channel
    3864           0 :         velHi = vopt(loFBdown[0],regridVeloRestfrq);
    3865             :         // calc velocity corresponding to the lower end (in freq) of the next channel
    3866           0 :         velLo = velHi + theChanWidthX; // vopt goes up as freq goes down!
    3867           0 :         while(velLo - lowerEndV < theChanWidthX/10.){ // (preventing accuracy problems)  
    3868             :           // calc frequency of the lower end (in freq) of the next channel
    3869           0 :           lDouble freqLo = freq_from_vopt(velLo,regridVeloRestfrq);
    3870           0 :           if(freqLo>=lowerEndF-edgeTolerance){ // end of bandwidth not yet reached
    3871           0 :             hiFBdown.push_back(loFBdown.back());
    3872           0 :             loFBdown.push_back(freqLo);
    3873             :           }
    3874           0 :           else if(freqLo>lowerEndF-edgeTolerance){ 
    3875           0 :             hiFBdown.push_back(loFBdown.back());
    3876           0 :             loFBdown.push_back(lowerEndF);
    3877           0 :             break;
    3878             :           }
    3879             :           else {
    3880           0 :             break;
    3881             :           }
    3882             :           // calc velocity corresponding to the upper end of the next channel
    3883           0 :           velHi = vopt(loFBdown.back(),regridVeloRestfrq);
    3884             :           // calc velocity corresponding to the lower end (in freq) of the next channel
    3885           0 :           velLo = velHi + theChanWidthX; // vopt goes up as freq goes down
    3886             :         }         
    3887             :       }
    3888           0 :       else if(regridQuant=="freq"){
    3889             :         // regridding in frequency  ...
    3890             :         
    3891             :         // create freq boundaries equidistant and contiguous in frequency
    3892           0 :         lDouble upperEndF = theRegridCenterF + theRegridBWF/2.;
    3893           0 :         lDouble lowerEndF = theRegridCenterF - theRegridBWF/2.;
    3894             : 
    3895             :         //    Want to keep the center of the center channel at the center of
    3896             :         //    the new center channel if the bandwidth is an odd multiple of the
    3897             :         //    new channel width, 
    3898             :         //    otherwise the center channel is the lower edge of the new center channel
    3899           0 :         lDouble tnumChan = floor((theRegridBWF+edgeTolerance)/theCentralChanWidthF);
    3900             : 
    3901             :         //cout << "theRegridBWF " << theRegridBWF << " upperEndF " << upperEndF << " lowerEndF " << lowerEndF << " tnumChan " << tnumChan << endl;
    3902             : 
    3903           0 :         if((Int) tnumChan % 2 != 0){
    3904             :           // odd multiple 
    3905           0 :           loFBup.push_back(theRegridCenterF-theCentralChanWidthF/2.);
    3906           0 :           hiFBup.push_back(theRegridCenterF+theCentralChanWidthF/2.);
    3907           0 :           loFBdown.push_back(theRegridCenterF-theCentralChanWidthF/2.);
    3908           0 :           hiFBdown.push_back(theRegridCenterF+theCentralChanWidthF/2.);
    3909             :         }
    3910             :         else{
    3911           0 :           loFBup.push_back(theRegridCenterF);
    3912           0 :           hiFBup.push_back(theRegridCenterF+theCentralChanWidthF);
    3913           0 :           loFBdown.push_back(theRegridCenterF);
    3914           0 :           hiFBdown.push_back(theRegridCenterF+theCentralChanWidthF);
    3915             :         }
    3916             : 
    3917           0 :         while(hiFBup.back()< upperEndF+edgeTolerance){
    3918             :           // calc frequency of the upper end of the next channel
    3919           0 :           lDouble freqHi = hiFBup.back() + theCentralChanWidthF;
    3920           0 :           if(freqHi<=upperEndF+edgeTolerance){ // end of bandwidth not yet reached
    3921           0 :             loFBup.push_back(hiFBup.back());
    3922           0 :             hiFBup.push_back(freqHi);
    3923             :           }
    3924             :           else{
    3925           0 :             break;
    3926             :           }
    3927             :         }
    3928             : 
    3929           0 :         while(loFBdown.back() > lowerEndF-edgeTolerance){
    3930             :           // calc frequency of the lower end of the next channel
    3931           0 :           lDouble freqLo = loFBdown.back() - theCentralChanWidthF;
    3932           0 :           if(freqLo>=lowerEndF-edgeTolerance){ // end of bandwidth not yet reached
    3933           0 :             hiFBdown.push_back(loFBdown.back());
    3934           0 :             loFBdown.push_back(freqLo);
    3935             :           }
    3936             :           else {
    3937           0 :             break;
    3938             :           }
    3939             :         }         
    3940             :       }
    3941           0 :       else if(regridQuant=="wave"){
    3942             :         // regridding in wavelength  ...
    3943             :         
    3944             :         // create freq boundaries equidistant and contiguous in wavelength
    3945           0 :         lDouble upperEndF = theRegridCenterF + theRegridBWF/2.;
    3946           0 :         lDouble lowerEndF = theRegridCenterF - theRegridBWF/2.;
    3947           0 :         lDouble upperEndL = lambda(upperEndF);
    3948           0 :         lDouble lowerEndL = lambda(lowerEndF);
    3949             :         lDouble lambdaLo;
    3950             :         lDouble lambdaHi;
    3951             : 
    3952             :         //    Want to keep the center of the center channel at the center of
    3953             :         //    the new center channel if the bandwidth is an odd multiple of the
    3954             :         //    new channel width, 
    3955             :         //    otherwise the center channel is the lower edge of the new center
    3956             :         //    channel
    3957           0 :         lDouble tnumChan = floor((theRegridBWF+edgeTolerance)/theCentralChanWidthF);
    3958           0 :         if((Int)tnumChan % 2 != 0){
    3959             :           // odd multiple 
    3960           0 :           loFBup.push_back(theRegridCenterF-theCentralChanWidthF/2.);
    3961           0 :           hiFBup.push_back(theRegridCenterF+theCentralChanWidthF/2.);
    3962           0 :           loFBdown.push_back(theRegridCenterF-theCentralChanWidthF/2.);
    3963           0 :           hiFBdown.push_back(theRegridCenterF+theCentralChanWidthF/2.);
    3964             :         }
    3965             :         else{
    3966           0 :           loFBup.push_back(theRegridCenterF);
    3967           0 :           hiFBup.push_back(theRegridCenterF+theCentralChanWidthF);
    3968           0 :           loFBdown.push_back(theRegridCenterF);
    3969           0 :           hiFBdown.push_back(theRegridCenterF+theCentralChanWidthF);
    3970             :         }
    3971             : 
    3972           0 :         if(theChanWidthX<0){ // cannot use original channel width in wavelength units
    3973             :           // need to calculate back from central channel width in Hz
    3974           0 :           theChanWidthX = lambda(loFBup[0]) - lambda(hiFBup[0]);
    3975             :         }
    3976             :         // calc wavelength corresponding to the upper end (in freq) of the last
    3977             :         // added channel which is the lower end of the next channel
    3978           0 :         lambdaLo = lambda(hiFBup[0]);
    3979             :         // calc wavelength corresponding to the upper end (in freq) of the next channel
    3980           0 :         lambdaHi = lambdaLo - theChanWidthX; // lambda goes down as freq goes up!
    3981           0 :         while(upperEndL - lambdaHi < theChanWidthX/10.){ // (preventing accuracy problems)
    3982             :           // calc frequency of the upper end (in freq) of the next channel
    3983           0 :           lDouble freqHi = freq_from_lambda(lambdaHi);
    3984           0 :           if(freqHi<=upperEndF+edgeTolerance){ // end of bandwidth not yet reached
    3985           0 :             loFBup.push_back(hiFBup.back());
    3986           0 :             hiFBup.push_back(freqHi);
    3987             :           }
    3988           0 :           else if(freqHi<upperEndF+edgeTolerance){ 
    3989           0 :             loFBup.push_back(hiFBup.back());
    3990           0 :             hiFBup.push_back(upperEndF);
    3991           0 :             break;
    3992             :           }
    3993             :           else{
    3994           0 :             break;
    3995             :           }
    3996             :           // calc wavelength corresponding to the upper end (in freq) of the
    3997             :           // added channel
    3998           0 :           lambdaLo = lambda(hiFBup.back());
    3999             :           // calc wavelength corresponding to the upper end (in freq) of the next channel
    4000           0 :           lambdaHi = lambdaLo - theChanWidthX; // lambda goes down as freq goes up
    4001             :         }
    4002             : 
    4003             :         // calc wavelength corresponding to the lower end (in freq) of the last
    4004             :         // added channel which is the upper end of the next channel
    4005           0 :         lambdaHi = lambda(loFBdown[0]);
    4006             :         // calc wavelength corresponding to the lower end (in freq) of the next channel
    4007           0 :         lambdaLo = lambdaHi + theChanWidthX; // lambda goes up as freq goes down!
    4008           0 :         while(lambdaLo - lowerEndL < theChanWidthX/10.){  // (preventing accuracy problems) 
    4009             :           // calc frequency of the lower end (in freq) of the next channel
    4010           0 :           lDouble freqLo = freq_from_lambda(lambdaLo);
    4011           0 :           if(freqLo>=lowerEndF-edgeTolerance){ // end of bandwidth not yet reached
    4012           0 :             hiFBdown.push_back(loFBdown.back());
    4013           0 :             loFBdown.push_back(freqLo);
    4014             :           }
    4015           0 :           else if(freqLo>lowerEndF-edgeTolerance){ 
    4016           0 :             hiFBdown.push_back(loFBdown.back());
    4017           0 :             loFBdown.push_back(lowerEndF);
    4018           0 :             break;
    4019             :           }
    4020             :           else {
    4021           0 :             break;
    4022             :           }
    4023             :           // calc wavelength corresponding to the upper end of the next channel
    4024           0 :           lambdaHi = lambda(loFBdown.back());
    4025             :           // calc wavelength corresponding to the lower end (in freq) of the next channel
    4026           0 :           lambdaLo = lambdaHi + theChanWidthX; // wavelength goes up as freq goes down
    4027             :         }         
    4028             : 
    4029             :       }
    4030             :       else{ // should not get here
    4031           0 :         oss << "Invalid value " << regridQuant << " for parameter \"mode\".";
    4032           0 :         message = oss.str();
    4033           0 :         return false;
    4034             :       }
    4035             : 
    4036           0 :       Int numNewChanDown = loFBdown.size();
    4037           0 :       Int numNewChanUp = loFBup.size();
    4038             : 
    4039             :       // central channel contained in both vectors
    4040           0 :       newChanLoBound.resize(numNewChanDown+numNewChanUp - 1);
    4041             : 
    4042           0 :       newChanHiBound.resize(numNewChanDown+numNewChanUp - 1);
    4043           0 :       for(Int i=0; i<numNewChanDown; i++){ 
    4044           0 :         Int k = numNewChanDown-i-1; // need to assign in reverse
    4045           0 :         newChanLoBound[i] = loFBdown[k];
    4046           0 :         newChanHiBound[i] = hiFBdown[k];
    4047             :       }
    4048           0 :       for(Int i=1; i<numNewChanUp; i++){ // start at 1 to omit the central channel here
    4049           0 :         newChanLoBound[i+numNewChanDown-1] = loFBup[i];
    4050           0 :         newChanHiBound[i+numNewChanDown-1] = hiFBup[i];
    4051             :       }
    4052             :       
    4053           0 :       uInt nc = newChanLoBound.size();
    4054           0 :       oss << " Number of channels = " << nc << endl;
    4055           0 :       oss << " Total width of SPW (in output frame) = " << newChanHiBound[nc-1] - newChanLoBound[0] 
    4056           0 :           << " Hz" << endl;
    4057           0 :       oss << " Lower edge = " << newChanLoBound[0] << " Hz,"
    4058           0 :           << " upper edge = " << newChanHiBound[nc-1] << " Hz" << endl;
    4059             : 
    4060           0 :       if(isDescending){ // original SPW was in reverse order; need to restore that
    4061           0 :         Vector<Double> tempL, tempU;
    4062           0 :         tempL.assign(newChanLoBound);
    4063           0 :         tempU.assign(newChanHiBound);
    4064           0 :         for(uInt i=0; i<nc; i++){
    4065           0 :           newChanLoBound(i) = tempL(nc-1-i);
    4066           0 :           newChanHiBound(i) = tempU(nc-1-i);
    4067             :         }
    4068           0 :       }
    4069             : 
    4070           0 :       message = oss.str();
    4071             : 
    4072           0 :       return true;
    4073             :       
    4074           0 :     } // end if (regridQuant== ...
    4075             : 
    4076           0 :   }
    4077             : 
    4078           0 :   Bool SubMS::convertGridPars(LogIO& os,
    4079             :                               const String& mode, 
    4080             :                               const int nchan, 
    4081             :                               const String& start, 
    4082             :                               const String& width,
    4083             :                               const String& interp, 
    4084             :                               const String& restfreq, 
    4085             :                               const String& outframe,
    4086             :                               const String& veltype,
    4087             :                               String& t_mode,
    4088             :                               String& t_outframe,
    4089             :                               String& t_regridQuantity,
    4090             :                               Double& t_restfreq,
    4091             :                               String& t_regridInterpMeth,
    4092             :                               Double& t_cstart, 
    4093             :                               Double& t_bandwidth,
    4094             :                               Double& t_cwidth,
    4095             :                               Bool& t_centerIsStart, 
    4096             :                               Bool& t_startIsEnd,                         
    4097             :                               Int& t_nchan,
    4098             :                               Int& t_width,
    4099             :                               Int& t_start
    4100             :                               ){
    4101           0 :     Bool rstat(false);
    4102             :     
    4103             :     try {
    4104             :       
    4105           0 :       os << LogOrigin("SubMS", "convertGridPars");
    4106             :       
    4107           0 :       casacore::QuantumHolder qh;
    4108           0 :       String error;
    4109             : 
    4110           0 :       t_mode = mode;
    4111           0 :       t_restfreq = 0.; 
    4112           0 :       if(!restfreq.empty() && !(restfreq=="[]")){
    4113           0 :         if(qh.fromString(error, restfreq)){
    4114           0 :           t_restfreq = qh.asQuantity().getValue("Hz");
    4115             :         }
    4116             :         else{
    4117           0 :           os << LogIO::SEVERE  << "restfreq: " << error << LogIO::POST;         
    4118           0 :           return false;
    4119             :         }
    4120             :       }
    4121             :       
    4122             :       // Determine grid
    4123           0 :       t_cstart = -9e99; // default value indicating that the original start of the SPW should be used
    4124           0 :       t_bandwidth = -1.; // default value indicating that the original width of the SPW should be used
    4125           0 :       t_cwidth = -1.; // default value indicating that the original channel width of the SPW should be used
    4126           0 :       t_nchan = -1; 
    4127           0 :       t_width = 0;
    4128           0 :       t_start = -1;
    4129           0 :       t_startIsEnd = false; // false means that start specifies the lower end in frequency (default)
    4130             :       // true means that start specifies the upper end in frequency
    4131             : 
    4132           0 :       if(!start.empty() && !(start=="[]")){ // start was set
    4133           0 :         if(t_mode == "channel"){
    4134           0 :           t_start = atoi(start.c_str());
    4135             :         }
    4136           0 :         if(t_mode == "channel_b"){
    4137           0 :           t_cstart = Double(atoi(start.c_str()));
    4138             :         }
    4139           0 :         else if(t_mode == "frequency"){
    4140           0 :           if(qh.fromString(error, start)){
    4141           0 :             t_cstart = qh.asQuantity().getValue("Hz");
    4142             :           }
    4143             :           else{
    4144           0 :             os << LogIO::SEVERE  << "start: " << error << LogIO::POST;          
    4145           0 :             return false;
    4146             :           }     
    4147             :         }
    4148           0 :         else if(t_mode == "velocity"){
    4149           0 :           if(qh.fromString(error, start)){
    4150           0 :             t_cstart = qh.asQuantity().getValue("m/s");
    4151             :           }
    4152             :           else{
    4153           0 :             os << LogIO::SEVERE << "start: " << error << LogIO::POST;           
    4154           0 :             return false;
    4155             :           }     
    4156             :         }
    4157             :       }
    4158           0 :       if(!width.empty() && !(width=="[]")){ // channel width was set
    4159           0 :         if(t_mode == "channel"){
    4160           0 :           Int w = atoi(width.c_str());
    4161           0 :           t_width = abs(w);
    4162           0 :           if(w<0){
    4163           0 :             t_startIsEnd = true;
    4164             :           }
    4165             :         }
    4166           0 :         else if(t_mode == "channel_b"){
    4167           0 :           Double w = atoi(width.c_str());
    4168           0 :           t_cwidth = abs(w);
    4169           0 :           if(w<0){
    4170           0 :             t_startIsEnd = true;
    4171             :           }     
    4172             :         }
    4173           0 :         else if(t_mode == "frequency"){
    4174           0 :           if(qh.fromString(error, width)){
    4175           0 :             Double w = qh.asQuantity().getValue("Hz");
    4176           0 :             t_cwidth = abs(w);
    4177           0 :             if(w<0){
    4178           0 :               t_startIsEnd = true;
    4179             :             }   
    4180             :           }
    4181             :           else{
    4182           0 :             os << LogIO::SEVERE << "width: " << error << LogIO::POST;           
    4183           0 :             return false;
    4184             :           }     
    4185             :         }
    4186           0 :         else if(t_mode == "velocity"){
    4187           0 :           if(qh.fromString(error, width)){
    4188           0 :             Double w = qh.asQuantity().getValue("m/s");
    4189           0 :             t_cwidth = abs(w);
    4190           0 :             if(w>=0){
    4191           0 :               t_startIsEnd = true; 
    4192             :             }           
    4193             :           }
    4194             :           else{
    4195           0 :             os << LogIO::SEVERE << "width: " << error << LogIO::POST;           
    4196           0 :             return false;           
    4197             :           }
    4198             :         }
    4199             :       }
    4200             :       else{ // width was not set
    4201             :         // for the velocity mode the default t_startIsEnd is true if the sign of width is not known
    4202           0 :         if(t_mode == "velocity"){
    4203           0 :           t_startIsEnd = true;
    4204             :         }
    4205             :       }
    4206             : 
    4207           0 :       if(nchan > 0){ // number of output channels was set
    4208           0 :         if(t_mode == "channel_b"){
    4209           0 :           if(t_cwidth>0){
    4210           0 :             t_bandwidth = Double(nchan*t_cwidth);
    4211             :           }
    4212             :           else{
    4213           0 :             t_bandwidth = Double(nchan);          
    4214             :           }
    4215             :         }
    4216             :         else{
    4217           0 :           t_nchan = nchan;
    4218             :         }
    4219             :       }
    4220             :       
    4221           0 :       if(t_mode == "channel"){
    4222           0 :         t_regridQuantity = "freq";
    4223             :       }
    4224           0 :       else if(t_mode == "channel_b"){
    4225           0 :         t_regridQuantity = "chan";
    4226             :       }
    4227           0 :       else if(t_mode == "frequency"){
    4228           0 :         t_regridQuantity = "freq";
    4229             :       }
    4230           0 :       else if(t_mode == "velocity"){
    4231           0 :         if(t_restfreq == 0.){
    4232           0 :           os << LogIO::SEVERE << "Need to set restfreq in velocity mode." << LogIO::POST; 
    4233           0 :           return false;
    4234             :         }       
    4235           0 :         t_regridQuantity = "vrad";
    4236           0 :         if(veltype == "optical"){
    4237           0 :           t_regridQuantity = "vopt";
    4238             :         }
    4239           0 :         else if(veltype != "radio"){
    4240             :           os << LogIO::WARN << "Invalid velocity type "<< veltype 
    4241           0 :              << ", setting type to \"radio\"" << LogIO::POST; 
    4242             :         }
    4243             :       }   
    4244             :       else{
    4245           0 :         os << LogIO::WARN << "Invalid mode " << t_mode << LogIO::POST;
    4246           0 :         return false;
    4247             :       }
    4248             :       
    4249           0 :       t_outframe=outframe;
    4250           0 :       t_regridInterpMeth=interp;
    4251           0 :       t_centerIsStart = true;
    4252             :             
    4253             :       // end prepare regridding parameters
    4254             :       
    4255           0 :       rstat = true;
    4256             : 
    4257           0 :     } catch (AipsError x) {
    4258           0 :       os << LogIO::SEVERE << "Exception Reported: " << x.getMesg() << LogIO::POST;
    4259           0 :       rstat = false;
    4260           0 :     }
    4261           0 :     return rstat;
    4262             :   }
    4263             : 
    4264             : 
    4265           0 :   Bool SubMS::calcChanFreqs(LogIO& os,
    4266             :                             Vector<Double>& newCHAN_FREQ, 
    4267             :                             Vector<Double>& newCHAN_WIDTH,
    4268             :                             const Vector<Double>& oldCHAN_FREQ, 
    4269             :                             const Vector<Double>& oldCHAN_WIDTH,
    4270             :                             const MDirection  phaseCenter,
    4271             :                             const MFrequency::Types theOldRefFrame,
    4272             :                             const MEpoch theObsTime,
    4273             :                             const MPosition mObsPos,
    4274             :                             const String& mode, 
    4275             :                             const int nchan, 
    4276             :                             const String& start, 
    4277             :                             const String& width,
    4278             :                             const String& restfreq, 
    4279             :                             const String& outframe,
    4280             :                             const String& veltype,
    4281             :                             const Bool verbose,
    4282             :                             const MRadialVelocity mRV // only used for outframe=="SOURCE"
    4283             :                             ){
    4284             : 
    4285             :     Double weightScale;
    4286           0 :     return calcChanFreqs(os,
    4287             :                          newCHAN_FREQ, 
    4288             :                          newCHAN_WIDTH,
    4289             :                          weightScale,
    4290             :                          oldCHAN_FREQ, 
    4291             :                          oldCHAN_WIDTH,
    4292             :                          phaseCenter,
    4293             :                          theOldRefFrame,
    4294             :                          theObsTime,
    4295             :                          mObsPos,
    4296             :                          mode, 
    4297             :                          nchan, 
    4298             :                          start, 
    4299             :                          width,
    4300             :                          restfreq, 
    4301             :                          outframe,
    4302             :                          veltype,
    4303             :                          verbose,
    4304             :                          mRV // only used for outframe=="SOURCE"
    4305           0 :                          );
    4306             :   }
    4307             : 
    4308             : 
    4309           0 :   Bool SubMS::calcChanFreqs(LogIO& os,
    4310             :                             Vector<Double>& newCHAN_FREQ, 
    4311             :                             Vector<Double>& newCHAN_WIDTH,
    4312             :                             Double& weightScale,
    4313             :                             const Vector<Double>& oldCHAN_FREQ, 
    4314             :                             const Vector<Double>& oldCHAN_WIDTH,
    4315             :                             const MDirection  phaseCenter,
    4316             :                             const MFrequency::Types theOldRefFrame,
    4317             :                             const MEpoch theObsTime,
    4318             :                             const MPosition mObsPos,
    4319             :                             const String& mode, 
    4320             :                             const int nchan, 
    4321             :                             const String& start, 
    4322             :                             const String& width,
    4323             :                             const String& restfreq, 
    4324             :                             const String& outframe,
    4325             :                             const String& veltype,
    4326             :                             const Bool verbose,
    4327             :                             const MRadialVelocity mRV // only used for outframe=="SOURCE"
    4328             :                             ){
    4329             : 
    4330           0 :     Vector<Double> newChanLoBound; 
    4331           0 :     Vector<Double> newChanHiBound;
    4332           0 :     String t_phasec;
    4333             : 
    4334           0 :     String t_mode;
    4335           0 :     String t_outframe;
    4336           0 :     String t_regridQuantity;
    4337             :     Double t_restfreq;
    4338           0 :     String t_regridInterpMeth;
    4339             :     Double t_cstart;
    4340             :     Double t_bandwidth;
    4341             :     Double t_cwidth;
    4342             :     Bool t_centerIsStart;
    4343             :     Bool t_startIsEnd;
    4344             :     Int t_nchan;
    4345             :     Int t_width;
    4346             :     Int t_start;
    4347             : 
    4348           0 :     if(!convertGridPars(os,
    4349             :                         mode, 
    4350             :                         nchan, 
    4351             :                         start, 
    4352             :                         width,
    4353             :                         "linear", // a dummy value in this context
    4354             :                         restfreq, 
    4355             :                         outframe,
    4356             :                         veltype,
    4357             :                         ////// output ////
    4358             :                         t_mode,
    4359             :                         t_outframe,
    4360             :                         t_regridQuantity,
    4361             :                         t_restfreq,
    4362             :                         t_regridInterpMeth,
    4363             :                         t_cstart, 
    4364             :                         t_bandwidth,
    4365             :                         t_cwidth,
    4366             :                         t_centerIsStart, 
    4367             :                         t_startIsEnd,                         
    4368             :                         t_nchan,
    4369             :                         t_width,
    4370             :                         t_start
    4371             :                         )
    4372             :        ){
    4373             :       // an error occured
    4374           0 :       return false;
    4375             :     }
    4376             : 
    4377             :     // reference frame transformation
    4378           0 :     Bool needTransform = true;
    4379           0 :     Bool doRadVelCorr = false;
    4380             :     MFrequency::Types theFrame;
    4381           0 :     String oframe = outframe;
    4382           0 :     oframe.upcase();
    4383           0 :     if(outframe==""){ // no ref frame given 
    4384             :       // keep the reference frame as is
    4385           0 :       theFrame = theOldRefFrame;
    4386           0 :       needTransform = false;
    4387             :     }
    4388           0 :     else if(oframe=="SOURCE"){ // GEO trafo + radial velocity correction
    4389           0 :       theFrame = MFrequency::GEO;
    4390           0 :       doRadVelCorr = true;
    4391             :     }
    4392           0 :     else if(!MFrequency::getType(theFrame, outframe)){
    4393             :       os << LogIO::SEVERE
    4394             :          << "Parameter \"outframe\" value " << outframe << " is invalid." 
    4395           0 :          << LogIO::POST;
    4396           0 :       return false;
    4397             :     }
    4398           0 :     else if (theFrame == theOldRefFrame){
    4399           0 :       needTransform = false;
    4400             :     }
    4401             : 
    4402           0 :     uInt oldNUM_CHAN = oldCHAN_FREQ.size();
    4403           0 :     if(oldNUM_CHAN == 0){
    4404           0 :       newCHAN_FREQ.resize(0);
    4405           0 :       newCHAN_WIDTH.resize(0);
    4406           0 :       return true;
    4407             :     }
    4408             : 
    4409           0 :     if(oldNUM_CHAN != oldCHAN_WIDTH.size()){
    4410             :       os << LogIO::SEVERE
    4411             :          << "Internal error: inconsistent dimensions of input channel freq and width arrays." 
    4412           0 :          << LogIO::POST;
    4413           0 :       return false;
    4414             :     }      
    4415             : 
    4416           0 :     Vector<Double> absOldCHAN_WIDTH;
    4417           0 :     absOldCHAN_WIDTH.assign(oldCHAN_WIDTH);
    4418             :     {
    4419           0 :       Bool negativeWidths = false;
    4420           0 :       for(uInt i=0; i<oldCHAN_WIDTH.size(); i++){
    4421           0 :         if(oldCHAN_WIDTH(i) < 0.){
    4422           0 :           negativeWidths = true;
    4423           0 :           absOldCHAN_WIDTH(i) = -oldCHAN_WIDTH(i);
    4424             :         }
    4425             :       }
    4426           0 :       if(negativeWidths && verbose){
    4427             :         os << LogIO::NORMAL
    4428             :            << " *** Encountered negative channel widths in input spectral window."
    4429           0 :            << LogIO::POST;
    4430             :       }
    4431             :     }
    4432             : 
    4433           0 :     Vector<Double> transNewXin;
    4434           0 :     Vector<Double> transCHAN_WIDTH(oldNUM_CHAN);
    4435             : 
    4436           0 :     if(needTransform){
    4437           0 :       transNewXin.resize(oldNUM_CHAN);
    4438             :       // set up conversion
    4439           0 :       Unit unit(String("Hz"));
    4440           0 :       MFrequency::Ref fromFrame = MFrequency::Ref(theOldRefFrame, MeasFrame(phaseCenter, mObsPos, theObsTime));
    4441           0 :       MFrequency::Ref toFrame = MFrequency::Ref(theFrame, MeasFrame(phaseCenter, mObsPos, theObsTime));
    4442           0 :       MFrequency::Convert freqTrans(unit, fromFrame, toFrame);
    4443             :       
    4444           0 :       MDoppler radVelCorr; // no correction
    4445           0 :       Bool radVelSignificant = false; // is the radial velocity large enough to warrant a shift?
    4446             :       // prepare correction for radial velocity if requested and possible
    4447           0 :       if(doRadVelCorr){
    4448           0 :         Quantity mrv = mRV.get("m/s"); // (const)
    4449           0 :         radVelCorr = MDoppler(-mrv); // NOTE: opposite sign to achieve correction!
    4450           0 :         Double mRVVal = mrv.getValue();
    4451           0 :         if(fabs(mRVVal)>1E-6){
    4452           0 :           radVelSignificant = true;
    4453           0 :           if(verbose){
    4454             :             os << LogIO::NORMAL
    4455             :                << "Note: The given additional radial velocity of " << mRVVal << " m/s will be taken into account."
    4456           0 :                << LogIO::POST;
    4457             :           }
    4458             :         }
    4459           0 :         else if(verbose){
    4460             :           os << LogIO::NORMAL
    4461             :              << "Note: The given additional radial velocity is less than 1E-6 m/s and will not be taken into account."
    4462           0 :              << LogIO::POST;
    4463             :         }
    4464           0 :       } 
    4465             : 
    4466           0 :       for(uInt i=0; i<oldNUM_CHAN; i++){
    4467           0 :         transNewXin[i] = freqTrans(oldCHAN_FREQ[i]).get(unit).getValue();
    4468           0 :         transCHAN_WIDTH[i] = freqTrans(oldCHAN_FREQ[i] +
    4469           0 :                                        absOldCHAN_WIDTH[i]/2.).get(unit).getValue()
    4470           0 :           - freqTrans(oldCHAN_FREQ[i] -
    4471           0 :                       absOldCHAN_WIDTH[i]/2.).get(unit).getValue(); // eliminate possible offsets
    4472             :       }
    4473             : 
    4474           0 :       if(radVelSignificant){ // correct in addition for radial velocity
    4475           0 :         transNewXin  = radVelCorr.shiftFrequency(transNewXin);
    4476           0 :         transCHAN_WIDTH = radVelCorr.shiftFrequency(transCHAN_WIDTH); //shiftFrequency is a scaling, so chan widths scale as well
    4477             :       }
    4478             : 
    4479           0 :     }
    4480             :     else {
    4481             :       // just copy
    4482           0 :       transNewXin.assign(oldCHAN_FREQ);
    4483           0 :       transCHAN_WIDTH.assign(absOldCHAN_WIDTH);
    4484             :     }
    4485             : 
    4486             :     // calculate new grid
    4487             : 
    4488           0 :     String message;
    4489             : 
    4490           0 :     if(!regridChanBounds(newChanLoBound, 
    4491             :                          newChanHiBound,
    4492             :                          t_cstart,  
    4493             :                          t_bandwidth, 
    4494             :                          t_cwidth, 
    4495             :                          t_restfreq,
    4496             :                          t_regridQuantity,
    4497             :                          transNewXin, 
    4498             :                          transCHAN_WIDTH,
    4499             :                          message,
    4500             :                          t_centerIsStart,
    4501             :                          t_startIsEnd,
    4502             :                          t_nchan,
    4503             :                          t_width,
    4504             :                          t_start
    4505             :                          )
    4506             :        ){ // there was an error
    4507           0 :       os << LogIO::WARN << message << LogIO::POST;
    4508           0 :       return false;
    4509             :     }
    4510             :     
    4511           0 :     if(verbose){
    4512           0 :       os << LogIO::NORMAL << message << LogIO::POST;
    4513             :     }
    4514             : 
    4515             :     // we have a useful set of channel boundaries
    4516           0 :     uInt newNUM_CHAN = newChanLoBound.size();
    4517             :     
    4518             :     // complete the calculation of the new channel centers and widths
    4519             :     // from newNUM_CHAN, newChanLoBound, and newChanHiBound 
    4520           0 :     newCHAN_FREQ.resize(newNUM_CHAN);
    4521           0 :     newCHAN_WIDTH.resize(newNUM_CHAN);
    4522           0 :     for(uInt i=0; i<newNUM_CHAN; i++){
    4523           0 :       newCHAN_FREQ[i] = (newChanLoBound[i]+newChanHiBound[i])/2.;
    4524           0 :       newCHAN_WIDTH[i] = newChanHiBound[i]-newChanLoBound[i];
    4525             :       //cout << "new lo hi freq width " << newChanLoBound[i] << " " << newChanHiBound[i] << " " << newCHAN_FREQ[i] << " " << newCHAN_WIDTH[i] << endl;
    4526             :     }
    4527             : 
    4528           0 :     weightScale = newCHAN_WIDTH[0]/transCHAN_WIDTH[0];
    4529             :     
    4530           0 :     return true;
    4531             : 
    4532           0 :   }
    4533             :   
    4534             : 
    4535           0 :   Bool SubMS::setRegridParameters(vector<Int>& oldSpwId,
    4536             :                                   vector<Int>& oldFieldId,
    4537             :                                   vector<Int>& newDataDescId,
    4538             :                                   vector<Bool>& regrid,
    4539             :                                   vector<Bool>& transform,
    4540             :                                   vector<MDirection>& theFieldDirV,
    4541             :                                   vector<MPosition>& mObsPosV,
    4542             :                                   vector<MFrequency::Types>& fromFrameTypeV,
    4543             :                                   vector<MFrequency::Ref>& outFrameV,
    4544             :                                   vector<MRadialVelocity>& outRadVelV,
    4545             :                                   vector< Double >& weightScaleV, 
    4546             :                                   vector< Vector<Double> >& xold, 
    4547             :                                   vector< Vector<Double> >& xout, 
    4548             :                                   vector< Vector<Double> >& xin, 
    4549             :                                   vector<Int>& method,
    4550             :                                   Bool& msModified,
    4551             :                                   const String& outframe,
    4552             :                                   const String& regridQuant,
    4553             :                                   const Double regridVeloRestfrq,
    4554             :                                   const String& regridInterpMeth,
    4555             :                                   const Double regridCenter, 
    4556             :                                   const Double regridBandwidth, 
    4557             :                                   const Double regridChanWidth,
    4558             :                                   const Int regridPhaseCenterFieldId,
    4559             :                                   const MDirection regridPhaseCenter,
    4560             :                                   const Bool writeTables,
    4561             :                                   LogIO& os,
    4562             :                                   String& regridMessage,
    4563             :                                   const Bool centerIsStart,
    4564             :                                   const Bool startIsEnd,
    4565             :                                   const Int nchan,
    4566             :                                   const Int width,
    4567             :                                   const Int start
    4568             :                                   )
    4569             :   {
    4570           0 :     Bool rval = true;
    4571             : 
    4572             :     // reset the "done" table.
    4573           0 :     newDataDescId.resize(0);
    4574           0 :     oldSpwId.resize(0);
    4575           0 :     oldFieldId.resize(0);
    4576           0 :     xold.resize(0);
    4577           0 :     xin.resize(0);
    4578           0 :     xout.resize(0);
    4579           0 :     theFieldDirV.resize(0);
    4580           0 :     mObsPosV.resize(0);
    4581           0 :     fromFrameTypeV.resize(0);
    4582           0 :     outFrameV.resize(0);
    4583           0 :     outRadVelV.resize(0);
    4584           0 :     weightScaleV.resize(0);
    4585           0 :     MFrequency::Ref outFrame;
    4586           0 :     MRadialVelocity outRadVel(Quantity(0, "m/s"), MRadialVelocity::GEO);
    4587           0 :     method.resize(0);
    4588           0 :     regrid.resize(0);   
    4589           0 :     transform.resize(0);        
    4590             :     
    4591             :     // Determine the highest data_desc_id from the DATA_DESCRIPTION table
    4592           0 :     MSDataDescription ddtable = ms_p.dataDescription();
    4593           0 :     Int origNumDataDescs = ddtable.nrow();
    4594           0 :     Int nextDataDescId = origNumDataDescs - 1;
    4595           0 :     Int numNewDataDesc = 0;
    4596             : 
    4597             :     // Determine the highest spw_id in the SPW table
    4598           0 :     MSSpectralWindow spwtable = ms_p.spectralWindow();
    4599           0 :     Int origNumSPWs = spwtable.nrow();
    4600           0 :     Int nextSPWId = origNumSPWs - 1;
    4601           0 :     Int numNewSPWIds = 0;
    4602             : 
    4603             :     // Determine the highest row number in the SOURCE table
    4604           0 :     Int origNumSourceRows = 0;
    4605           0 :     Int nextSourceRow = -1;
    4606           0 :     Int numNewSourceRows = 0;
    4607           0 :     vector<Int> newSourceIds;
    4608           0 :     vector<Int> newSourceSPWIds;
    4609           0 :     MSSource* p_sourcetable = NULL;
    4610           0 :     MSSourceColumns* p_sourceCol = NULL;
    4611           0 :     if(Table::isReadable(ms_p.sourceTableName())){
    4612           0 :       p_sourcetable = &(ms_p.source());
    4613           0 :       p_sourceCol = new MSSourceColumns(*p_sourcetable);
    4614           0 :       origNumSourceRows = p_sourcetable->nrow();
    4615           0 :       nextSourceRow = origNumSourceRows - 1;
    4616             :     }
    4617           0 :     else if(!writeTables) { // there is no source table
    4618           0 :       os << LogIO::NORMAL << "Note: MS does not contain a SOURCE table ..." << LogIO::POST;
    4619           0 :       nextSourceRow = -1;
    4620             :     }
    4621             : 
    4622           0 :     MSMainColumns mainCols(ms_p);
    4623           0 :     ScalarColumn<Int> fieldIdCol = mainCols.fieldId();
    4624           0 :     ScalarColumn<Int> DDIdCol = mainCols.dataDescId();
    4625           0 :     ScalarMeasColumn<MEpoch> mainTimeMeasCol = mainCols.timeMeas();
    4626             :     // other administrational tables
    4627             : 
    4628           0 :     MSDataDescColumns DDCols(ddtable);
    4629           0 :     ScalarColumn<Int> SPWIdCol = DDCols.spectralWindowId(); 
    4630             : 
    4631           0 :     MSSpWindowColumns SPWCols(spwtable);
    4632           0 :     ScalarColumn<Int> numChanCol = SPWCols.numChan(); 
    4633           0 :     ArrayColumn<Double> chanFreqCol = SPWCols.chanFreq(); 
    4634           0 :     ArrayMeasColumn<MFrequency> chanFreqMeasCol = SPWCols.chanFreqMeas();
    4635           0 :     ScalarColumn<Int> measFreqRefCol = SPWCols.measFreqRef();
    4636           0 :     ArrayColumn<Double> chanWidthCol = SPWCols.chanWidth(); 
    4637           0 :     ArrayColumn<Double> effectiveBWCol = SPWCols.effectiveBW();   
    4638           0 :     ScalarColumn<Double> refFrequencyCol = SPWCols.refFrequency(); 
    4639           0 :     ScalarMeasColumn<MFrequency> refFrequencyMeasCol = SPWCols.refFrequencyMeas(); 
    4640           0 :     ArrayColumn<Double> resolutionCol = SPWCols.resolution(); 
    4641           0 :     ScalarColumn<Double> totalBandwidthCol = SPWCols.totalBandwidth();
    4642             : 
    4643           0 :     MSField fieldtable = ms_p.field();
    4644           0 :     MSFieldColumns FIELDCols(fieldtable);
    4645             :     //ArrayMeasColumn<MDirection> referenceDirMeasCol = FIELDCols.referenceDirMeasCol(); 
    4646             :     //ScalarMeasColumn<MEpoch>& timeMeasCol = FIELDCols.timeMeas();
    4647           0 :     ScalarColumn<Int> FIELDsourceIdCol = FIELDCols.sourceId(); 
    4648             : 
    4649             :     // calculate mean antenna position for TOPO transformation
    4650           0 :     MSAntenna anttable = ms_p.antenna();
    4651           0 :     MSAntennaColumns ANTCols(anttable);
    4652           0 :     ROScalarMeasColumn<MPosition> ANTPositionMeasCol = ANTCols.positionMeas(); 
    4653           0 :     ScalarColumn<Bool> ANTflagRowCol = ANTCols.flagRow();
    4654           0 :     Int nAnt = 0;
    4655           0 :     Vector<Double> pos(3); pos=0;
    4656           0 :     for (uInt i=0; i<anttable.nrow(); i++) {
    4657           0 :       if(!ANTflagRowCol(i)){
    4658           0 :         pos+=ANTPositionMeasCol(i).getValue().get();
    4659           0 :         nAnt++;
    4660             :       }
    4661             :     }
    4662           0 :     if(nAnt>0){
    4663           0 :       pos /= Double(nAnt);
    4664             :     }
    4665             :     else {
    4666             :       os << LogIO::WARN << "No unflagged antennas in this MS. Cannot proceed with regridSpw ..." 
    4667           0 :          << LogIO::POST;
    4668           0 :       return rval; 
    4669             :     }
    4670             : 
    4671           0 :     MPosition mObsPos = ANTPositionMeasCol(0); // transfer reference frame
    4672           0 :     mObsPos.set(MVPosition(pos)); // set coordinates
    4673             : 
    4674             :     // but use a tabulated version if available (as in clean)
    4675             :     {
    4676           0 :       MPosition Xpos;
    4677           0 :       String Xobservatory;
    4678           0 :       MSObservationColumns XObsCols(ms_p.observation());
    4679           0 :       if (ms_p.observation().nrow() > 0) {
    4680           0 :         Xobservatory = XObsCols.telescopeName()(mainCols.observationId()(0));
    4681             :       }
    4682           0 :       if (Xobservatory.length() == 0 || 
    4683           0 :           !MeasTable::Observatory(Xpos,Xobservatory)) {
    4684             :         // unknown observatory, use the above calculated centroid position
    4685           0 :         if(!writeTables){
    4686             :           os << LogIO::WARN << "Unknown observatory: \"" << Xobservatory 
    4687           0 :              << "\". Determining observatory position from antenna 0." << LogIO::POST;
    4688             :         }
    4689           0 :         Xpos=MPosition::Convert(ANTPositionMeasCol(0), MPosition::ITRF)();
    4690             :       }
    4691             :       else{
    4692           0 :         if(!writeTables){
    4693             :           os << LogIO::NORMAL << "Using tabulated observatory position for " << Xobservatory << ":"
    4694           0 :              << LogIO::POST;
    4695           0 :           Xpos=MPosition::Convert(Xpos, MPosition::ITRF)();
    4696             :         }
    4697             :       }
    4698           0 :       mObsPos = Xpos;
    4699           0 :       if(!writeTables){
    4700           0 :         ostringstream oss;
    4701           0 :         oss <<  "   " << mObsPos << " (ITRF)";
    4702           0 :         os << LogIO::NORMAL << oss.str() << LogIO::POST;
    4703           0 :       }
    4704           0 :     }
    4705             :     
    4706             :     // create time-sorted index for main table access
    4707           0 :     uInt nMainTabRows = ms_p.nrow();
    4708           0 :     Vector<uInt> sortedI(nMainTabRows);
    4709             :     {
    4710           0 :       Vector<Double> mainTimesV = mainCols.time().getColumn();
    4711           0 :       GenSortIndirect<Double>::sort(sortedI,mainTimesV);
    4712           0 :     }
    4713             :     
    4714           0 :     for(uInt mainTabRowI=0; mainTabRowI<nMainTabRows; mainTabRowI++){
    4715             :     
    4716           0 :       uInt mainTabRow = sortedI(mainTabRowI); // i.e. mainTabRow is sorted in Time
    4717             : 
    4718             :       // For each MAIN table row, the FIELD_ID cell and the DATA_DESC_ID cell are read 
    4719           0 :       Int theFieldId = fieldIdCol(mainTabRow);
    4720           0 :       Int theDataDescId = DDIdCol(mainTabRow);
    4721             :       // and the SPW_ID extracted from the corresponding row in the
    4722             :       // DATA_DESCRIPTION table.
    4723           0 :       Int theSPWId = -2;
    4724           0 :       if (theDataDescId < origNumDataDescs){
    4725           0 :         theSPWId = SPWIdCol(theDataDescId);
    4726             :       }
    4727             :       else {
    4728             :         os << LogIO::SEVERE
    4729             :            << "Incoherent MS: Found at main table row " << mainTabRow
    4730             :            << " reference to non-existing DATA_DESCRIPTION table entry "
    4731             :            << theDataDescId
    4732           0 :            << LogIO::POST;
    4733           0 :         rval = false;
    4734           0 :         return rval;
    4735             :       }
    4736             : 
    4737             :       // variables saying what has to be done for this row
    4738           0 :       Bool needTransform = false;
    4739           0 :       Bool doRegrid = false;
    4740           0 :       Int equivalentSpwFieldPair = -1;
    4741             : 
    4742           0 :       String message;
    4743             : 
    4744             :       //  The pair (theFieldId, theSPWId) is looked up in the "done table". 
    4745           0 :       Int iDone = -1;
    4746           0 :       for (uInt i=0; i<oldSpwId.size(); i++){
    4747           0 :         if(oldSpwId[i]==theSPWId && (oldFieldId[i]==theFieldId || regridPhaseCenterFieldId>=-1)){ 
    4748             :           // if common phase center is given, treat all fields the same
    4749           0 :           iDone = i;
    4750           0 :           break;
    4751             :         }
    4752             :       }
    4753             : 
    4754           0 :       if(iDone<0){ // this (theFieldId, theSPWId) pair was not yet encountered 
    4755             : 
    4756             :         // Determine information for new row in "done" table
    4757             :         //   The information necessary for the transformation is extracted:  
    4758             :         //   1) center frequency of each channel (taken from the CHAN_FREQ cell
    4759             :         //      corresponding to theSPWId in the SPW table)
    4760           0 :         Vector<Double> newXin;
    4761           0 :         newXin.assign(chanFreqCol(theSPWId));
    4762             :         //      -> store in  xin (further below)
    4763             :         //   2) reference frame for these frequencies (taken from the
    4764             :         //      MEAS_FREQ_REF cell corresponding to theSPWId in the SPW table)
    4765           0 :         MFrequency::Types theOldRefFrame = MFrequency::castType(measFreqRefCol(theSPWId));
    4766             :         //      -> store in oldRefFrame[numNewDataDesc] (further below)
    4767             :         //   3) in case either the original or the destination reference frame
    4768             :         //      is TOPO or GEO, we need the observation time
    4769             :         //      (taken from the time of the first integration for this (theFieldId, theSPWId) pair)
    4770             :         //      -> store in obsTime[numNewDataDesc] (further below)
    4771           0 :         MEpoch theObsTime = mainTimeMeasCol(mainTabRow);
    4772             :           ////      (taken uniformly for the whole MS from the first row of the MS
    4773             :           ////      which is also the earliest row because it is time-sorted)
    4774             :           //MEpoch theObsTime = mainTimeMeasCol(0);
    4775             : 
    4776             :         // Determine if a reference frame transformation is necessary
    4777             :         // Bool         getType (MFrequency::Types &tp, const String &in)
    4778           0 :         needTransform = true;
    4779           0 :         Bool doRadVelCorr = false;
    4780             :         MFrequency::Types theFrame;
    4781           0 :         String oframe = outframe;
    4782           0 :         oframe.upcase();
    4783           0 :         if(outframe==""){ // no ref frame given 
    4784             :           // keep the reference frame as is
    4785           0 :           theFrame = theOldRefFrame;
    4786           0 :           needTransform = false;
    4787             :         }
    4788           0 :         else if(oframe=="SOURCE"){ // GEO trafo + radial velocity correction
    4789           0 :           theFrame = MFrequency::GEO;
    4790           0 :           doRadVelCorr = true;
    4791             :         }
    4792           0 :         else if(!MFrequency::getType(theFrame, outframe)){
    4793             :           os << LogIO::SEVERE
    4794             :              << "Parameter \"outframe\" value " << outframe << " is invalid." 
    4795           0 :              << LogIO::POST;
    4796           0 :           return false;
    4797             :         }
    4798           0 :         else if (theFrame == theOldRefFrame){
    4799           0 :           needTransform = false;
    4800             :         }
    4801             : 
    4802           0 :         Double weightScale = 1.;
    4803             : 
    4804             :         //   4) direction of the field, i.e. the phase center
    4805           0 :         MDirection theFieldDir;
    4806           0 :         uInt theFieldIdToUse = theFieldId;
    4807           0 :         if(regridPhaseCenterFieldId<-1){ // take it from the PHASE_DIR cell
    4808             :                                          // corresponding to theFieldId in the FIELD table)
    4809           0 :           theFieldDir = FIELDCols.phaseDirMeas(theFieldId, mainCols.time()(mainTabRow));
    4810             : 
    4811           0 :           if(FIELDCols.needInterTime(theFieldId)){
    4812           0 :             String ephPath = FIELDCols.ephemPath(theFieldId);
    4813           0 :             os << LogIO::NORMAL << "Note: Field with id " << theFieldId; 
    4814           0 :             if(ephPath.size()>0){
    4815           0 :               os << ", uses ephemeris " << ephPath << LogIO::POST;
    4816             :             }
    4817             :             else{
    4818           0 :               os << ", has a time-dependent position described by a polynomial." << LogIO::POST;
    4819             :             }
    4820           0 :           }
    4821             : 
    4822             :         }
    4823           0 :         else if(regridPhaseCenterFieldId==-1){ // use the given direction
    4824           0 :           theFieldDir = regridPhaseCenter;
    4825           0 :           doRadVelCorr = false;
    4826             :         }
    4827           0 :         else if((uInt)regridPhaseCenterFieldId < fieldtable.nrow()){ // use this valid field ID
    4828           0 :           theFieldDir = FIELDCols.phaseDirMeas(regridPhaseCenterFieldId, mainCols.time()(mainTabRow));
    4829           0 :           theFieldIdToUse = regridPhaseCenterFieldId;
    4830           0 :           if(FIELDCols.needInterTime(regridPhaseCenterFieldId)){
    4831           0 :             String ephPath = FIELDCols.ephemPath(regridPhaseCenterFieldId);
    4832           0 :             os << LogIO::NORMAL << "Note: Field to be used as phase center, id " << regridPhaseCenterFieldId; 
    4833           0 :             if(ephPath.size()>0){
    4834           0 :               os << ", uses ephemeris " << ephPath << LogIO::POST;
    4835             :             }
    4836             :             else{
    4837           0 :               os << ", has a time-dependent position described by a polynomial." << LogIO::POST;
    4838             :             }
    4839           0 :           }
    4840             : 
    4841             :         }
    4842             :         else{
    4843             :           os << LogIO::SEVERE << "Field to be used as phase center, id " 
    4844             :              << regridPhaseCenterFieldId 
    4845           0 :              << ", does not exist." << LogIO::POST;
    4846           0 :           return false;
    4847             :         }
    4848             : 
    4849             :         //      cout << "theFieldId = " << theFieldId << ", theObsTime = " << theObsTime
    4850             :         //           << ", theFieldDir = " << theFieldDir.getAngle() << endl;
    4851             : 
    4852             :         //      -> store in fieldDir[numNewDataDesc] (further below)
    4853             :         //   5) in case either the original or the destination reference frame
    4854             :         //      (but not both) are TOPO, we need the observatory position
    4855             :         //      (from the mean antenna position calculated above) 
    4856             :         //      -> store in obsPos[numNewDataDesc] (further below)
    4857             : 
    4858             :         // Perform the pure frequency transformation (no regridding yet)
    4859           0 :         Vector<Double> transNewXin;
    4860             :         // also take care of the other parameters of the spectral window
    4861           0 :         Int oldNUM_CHAN = numChanCol(theSPWId); 
    4862           0 :         Vector<Double> oldCHAN_WIDTH = chanWidthCol(theSPWId);
    4863             :         {
    4864           0 :           Bool negativeWidths = false;
    4865           0 :           for(uInt i=0; i<oldCHAN_WIDTH.nelements(); i++){
    4866           0 :             if(oldCHAN_WIDTH(i) < 0.){
    4867           0 :               negativeWidths = true;
    4868           0 :               oldCHAN_WIDTH(i) = -oldCHAN_WIDTH(i);
    4869             :             }
    4870             :           }
    4871           0 :           if(negativeWidths){
    4872             :             os << LogIO::NORMAL
    4873             :                << " *** Encountered negative channel widths in SPECTRAL_WINDOW table."
    4874           0 :                << LogIO::POST;
    4875             :           }
    4876             :         }
    4877           0 :         MFrequency oldREF_FREQUENCY = refFrequencyMeasCol(theSPWId);
    4878           0 :         Double oldTOTAL_BANDWIDTH = totalBandwidthCol(theSPWId);
    4879           0 :         Vector<Double> oldEFFECTIVE_BW = effectiveBWCol(theSPWId);   
    4880           0 :         Vector<Double> oldRESOLUTION = resolutionCol(theSPWId);
    4881             : 
    4882             :         // storage for values with pure freq trafo applied
    4883           0 :         Vector<Double> transCHAN_WIDTH(oldNUM_CHAN);
    4884           0 :         MFrequency transREF_FREQUENCY;
    4885             :         Double transTOTAL_BANDWIDTH;
    4886           0 :         Vector<Double> transRESOLUTION(oldNUM_CHAN);;
    4887             : 
    4888           0 :         if(needTransform){
    4889             : 
    4890           0 :           transNewXin.resize(oldNUM_CHAN);
    4891             :           // set up conversion
    4892           0 :           Unit unit(String("Hz"));
    4893           0 :           MFrequency::Ref fromFrame = MFrequency::Ref(theOldRefFrame, MeasFrame(theFieldDir, mObsPos, theObsTime));
    4894           0 :           MFrequency::Ref toFrame = MFrequency::Ref(theFrame, MeasFrame(theFieldDir, mObsPos, theObsTime));
    4895           0 :           MFrequency::Convert freqTrans(unit, fromFrame, toFrame);
    4896             :           
    4897           0 :           MDoppler radVelCorr; // no correction
    4898           0 :           Bool radVelSignificant = false; // is the radial velocity large enough to warrant a shift?
    4899             :           // prepare correction for radial velocity if requested and possible
    4900           0 :           if(doRadVelCorr && FIELDCols.needInterTime(theFieldIdToUse)){
    4901           0 :             MRadialVelocity mRV = FIELDCols.radVelMeas(theFieldIdToUse, mainCols.time()(mainTabRow));
    4902           0 :             Quantity mrv = mRV.get("m/s");
    4903           0 :             radVelCorr = MDoppler(-mrv); // NOTE: opposite sign to achieve correction
    4904           0 :             if(fabs(mrv.getValue())>1E-6){
    4905           0 :               radVelSignificant = true;
    4906           0 :               outRadVel = mRV;
    4907             :               os << LogIO::NORMAL
    4908           0 :                  << "Note: The geocentric radial velocity information (ca. " << mrv.getValue()
    4909             :                  << "m/s) from the ephemeris for field " << theFieldIdToUse << " will be taken into account."
    4910           0 :                  << LogIO::POST;
    4911             :             }
    4912             :             else{
    4913           0 :               outRadVel = MRadialVelocity(Quantity(0, "m/s"), MRadialVelocity::GEO);
    4914             :               os << LogIO::NORMAL
    4915             :                  << "Note: The geocentric radial velocity from the ephemeris for field " << theFieldIdToUse 
    4916             :                  << " is less than 1E-6 m/s and will not be taken into account."
    4917           0 :                  << LogIO::POST;
    4918             : 
    4919             :             }
    4920           0 :           } 
    4921             : 
    4922             :           // also create the reference for storage in the "Done" table
    4923           0 :           outFrame = MFrequency::Ref(theFrame, MeasFrame(theFieldDir, mObsPos, theObsTime));
    4924             :           
    4925             : 
    4926           0 :           for(Int i=0; i<oldNUM_CHAN; i++){
    4927           0 :             transNewXin[i] = freqTrans(newXin[i]).get(unit).getValue();
    4928           0 :             transCHAN_WIDTH[i] = freqTrans(newXin[i] +
    4929           0 :                                            oldCHAN_WIDTH[i]/2.).get(unit).getValue()
    4930           0 :               - freqTrans(newXin[i] -
    4931           0 :                           oldCHAN_WIDTH[i]/2.).get(unit).getValue(); // eliminate possible offsets
    4932           0 :             transRESOLUTION[i] = freqTrans(newXin[i] +
    4933           0 :                                            oldRESOLUTION[i]/2.).get(unit).getValue() 
    4934           0 :               - freqTrans(newXin[i]
    4935           0 :                           - oldRESOLUTION[i] / 2.0).get(unit).getValue(); // eliminate possible offsets
    4936             :           }
    4937           0 :           transREF_FREQUENCY = freqTrans(oldREF_FREQUENCY);
    4938           0 :           transTOTAL_BANDWIDTH = fabs(transNewXin[oldNUM_CHAN-1] - transNewXin[0]) +
    4939           0 :             fabs(transCHAN_WIDTH[oldNUM_CHAN-1])/2.  + fabs(transCHAN_WIDTH[0])/2.;
    4940             : 
    4941           0 :           if(radVelSignificant){ // correct in addition for radial velocity
    4942           0 :             transNewXin  = radVelCorr.shiftFrequency(transNewXin);
    4943           0 :             transCHAN_WIDTH = radVelCorr.shiftFrequency(transCHAN_WIDTH); //shiftFrequency is a scaling, so chan widths scale as well
    4944           0 :             transRESOLUTION = radVelCorr.shiftFrequency(transRESOLUTION);
    4945           0 :             Vector<Double> tmpV(1);
    4946           0 :             tmpV(0) = transREF_FREQUENCY.get("Hz").getValue();
    4947           0 :             transREF_FREQUENCY.set( MVFrequency( radVelCorr.shiftFrequency(tmpV)(0) ) );
    4948           0 :             tmpV(0) = transTOTAL_BANDWIDTH;
    4949           0 :             transTOTAL_BANDWIDTH = radVelCorr.shiftFrequency(tmpV)(0);
    4950           0 :           }
    4951             : 
    4952           0 :         }
    4953             :         else {
    4954             :           // just copy
    4955           0 :           transNewXin.assign(newXin);
    4956           0 :           transCHAN_WIDTH.assign(oldCHAN_WIDTH);
    4957           0 :           transRESOLUTION.assign(oldRESOLUTION);
    4958           0 :           transREF_FREQUENCY = oldREF_FREQUENCY;
    4959           0 :           transTOTAL_BANDWIDTH = oldTOTAL_BANDWIDTH;
    4960             :         }
    4961             : 
    4962             :         // (reference frame transformation completed)
    4963             : 
    4964             : 
    4965             :         // storage for values with complete freq trafo + regridding applied
    4966             :         // (set to default values for the case of no regridding)
    4967           0 :         Vector<Double> newXout;
    4968           0 :         newXout.assign(transNewXin);
    4969           0 :         Int newNUM_CHAN = oldNUM_CHAN;
    4970           0 :         Vector<Double> newCHAN_WIDTH;
    4971           0 :         newCHAN_WIDTH.assign(transCHAN_WIDTH);
    4972           0 :         MFrequency newREF_FREQUENCY = transREF_FREQUENCY;
    4973           0 :         Vector<Double> newRESOLUTION;
    4974           0 :         newRESOLUTION.assign(transRESOLUTION);
    4975           0 :         Double newTOTAL_BANDWIDTH = transTOTAL_BANDWIDTH;
    4976           0 :         Vector<Double> newEFFECTIVE_BW;
    4977           0 :         newEFFECTIVE_BW.assign(oldEFFECTIVE_BW);
    4978             :         Int theMethod;
    4979             : 
    4980             :         // check if theSPWId was already handled
    4981           0 :         Int iDone2 = -1;
    4982           0 :         for (uInt i=0; i<oldSpwId.size(); i++){
    4983           0 :           if(oldSpwId[i]==theSPWId){
    4984           0 :             iDone2 = i;
    4985           0 :             break;
    4986             :           }
    4987             :         }
    4988           0 :         equivalentSpwFieldPair = iDone2;
    4989             : 
    4990           0 :         if(equivalentSpwFieldPair>=0 && !needTransform){ 
    4991             :           // a transformation was not needed, 
    4992             :           // i.e. the operation on this SPW is independent of the FIELD
    4993             :           // and (since equivalentSpwFieldPair>=0) this SPW was already processed
    4994             :           // so we can reuse a previous SpwFieldPair
    4995             : 
    4996             :           // get the parameters from the "done table"
    4997           0 :           theSPWId = oldSpwId[equivalentSpwFieldPair];
    4998             :           // don't get the field id!
    4999           0 :           theDataDescId = newDataDescId[equivalentSpwFieldPair] + origNumDataDescs; 
    5000           0 :           transNewXin.assign(xin[equivalentSpwFieldPair]);
    5001           0 :           newXout.assign(xout[equivalentSpwFieldPair]);
    5002           0 :           theMethod = method[equivalentSpwFieldPair];
    5003           0 :           doRegrid = regrid[equivalentSpwFieldPair];
    5004             : 
    5005             :         }
    5006             :         else {
    5007             :           // Determine if regridding is necessary and set the parameters (at
    5008             :           // the same time, determine if the transformation is non-linear. If
    5009             :           // so set trafoNonlin (further below).)
    5010             :           
    5011           0 :           String methodName;
    5012             :           
    5013           0 :           if(regridQuant=="" ||
    5014           0 :              (regridCenter<-1E30 &&  regridBandwidth <= 0. && regridChanWidth <= 1. 
    5015           0 :               && regridQuant=="chan")
    5016             :              ){
    5017             :             // No regridding will take place.
    5018             :             // Set the interpol methods to some dummy value
    5019           0 :             theMethod = (Int) InterpolateArray1D<Double,Complex>::linear;
    5020           0 :             methodName = "linear";
    5021           0 :             message = " output frame = " + MFrequency::showType(theFrame) + " (pure transformation of the channel definition)";
    5022             :             // cout <<  regridQuant << " " << regridCenter << " " << regridBandwidth << " " << regridChanWidth << endl;
    5023             :           }
    5024             :           else { // a regrid quantity was chosen
    5025             :             // determine interpolation method (this is common for all possible
    5026             :             // values of regridQuant)
    5027           0 :             String meth=regridInterpMeth;
    5028           0 :             meth.downcase();
    5029           0 :             if(meth.contains("nearest")){
    5030           0 :               theMethod = (Int) InterpolateArray1D<Double,Complex>::nearestNeighbour;
    5031           0 :               methodName = "nearestNeighbour";
    5032             :             }
    5033           0 :             else if(meth.contains("splin")){
    5034           0 :               theMethod = (Int) InterpolateArray1D<Double,Complex>::spline;
    5035           0 :               methodName = "spline";
    5036             :             }       
    5037           0 :             else if(meth.contains("cub")){
    5038           0 :               theMethod = (Int) InterpolateArray1D<Double,Complex>::cubic;
    5039           0 :               methodName = "cubic spline";
    5040             :             }
    5041           0 :             else if(meth.contains("fft")){
    5042             :               // check if input grid is equidistant in frequency
    5043           0 :               Bool isEquidistant = true;
    5044           0 :               Double sep = fabs(transCHAN_WIDTH(0));
    5045           0 :               for(uInt i=1; i<transCHAN_WIDTH.size(); i++){
    5046           0 :                 if((fabs(transCHAN_WIDTH(i)-transCHAN_WIDTH(i-1))>0.1)
    5047           0 :                    || fabs(fabs(transNewXin(i)-transNewXin(i-1))-sep)>0.1 ){
    5048           0 :                   isEquidistant = false;
    5049           0 :                   break;
    5050             :                 }
    5051           0 :                 sep = fabs(transNewXin(i)-transNewXin(i-1));
    5052             :               }
    5053             : 
    5054           0 :               if(isEquidistant){
    5055           0 :                 theMethod = (Int) useFFTShift;
    5056           0 :                 methodName = "fftshift";
    5057             :               }
    5058             :               else{
    5059           0 :                 theMethod = (Int) useLinIntThenFFTShift;
    5060           0 :                 methodName = "fftshift (preceeded by a linear transform to make grid equidistant)";
    5061             :               }
    5062             :               // for this method to work, the output grid needs to be equidistant in frequency
    5063           0 :               if(!(regridQuant=="freq" || regridQuant=="vrad" 
    5064           0 :                    || (regridQuant=="chan" && isEquidistant) // (chan equidistance depends on input grid) 
    5065             :                    )){
    5066             :                 os << LogIO::SEVERE
    5067             :                    << "Parameter \"interpolation\" value \"" << meth << "\" requires an output grid equidistant in frequency."
    5068             :                    << endl << "Cannot proceed." 
    5069           0 :                    << LogIO::POST;
    5070           0 :                 return false;
    5071             :               }
    5072             :             }
    5073             :             else {
    5074           0 :               if(!meth.contains("linear") && meth!=""){
    5075             :                 os << LogIO::WARN
    5076             :                    << "Parameter \"interpolation\" value \"" << meth << "\" is invalid." 
    5077           0 :                    << LogIO::POST;
    5078           0 :                 return false;
    5079             :               }
    5080           0 :               theMethod = (Int) InterpolateArray1D<Double,Complex>::linear;
    5081           0 :               methodName = "linear";
    5082             :             }
    5083             :             
    5084           0 :             Vector<Double> newChanLoBound; 
    5085           0 :             Vector<Double> newChanHiBound;
    5086             :             
    5087           0 :             if(!regridChanBounds(newChanLoBound, 
    5088             :                                  newChanHiBound,
    5089             :                                  regridCenter,  
    5090             :                                  regridBandwidth, 
    5091             :                                  regridChanWidth, 
    5092             :                                  regridVeloRestfrq,
    5093             :                                  regridQuant,
    5094             :                                  transNewXin, 
    5095             :                                  transCHAN_WIDTH,
    5096             :                                  message,
    5097             :                                  centerIsStart,
    5098             :                                  startIsEnd,
    5099             :                                  nchan,
    5100             :                                  width,
    5101             :                                  start
    5102             :                                  )
    5103             :                ){ // there was an error
    5104           0 :               os << LogIO::SEVERE << message << LogIO::POST;
    5105           0 :               throw(AipsError("Regridding failed."));
    5106             :               //return false;
    5107             :             }
    5108             :             
    5109             :             // we have a useful set of channel boundaries
    5110           0 :             newNUM_CHAN = newChanLoBound.size();
    5111             :             
    5112           0 :             if(theMethod==(Int) useFFTShift
    5113           0 :                && newNUM_CHAN != oldNUM_CHAN){ // need to precede by lin. interpol. after all
    5114           0 :               theMethod = (Int) useLinIntThenFFTShift;
    5115             :             }
    5116             : 
    5117           0 :             message = "input frame = " + MFrequency::showType(theOldRefFrame) 
    5118           0 :               + ", output frame = " + MFrequency::showType(theFrame)
    5119           0 :               + "\n" + message + " Interpolation Method = " + methodName;
    5120             :             
    5121             :             // complete the calculation of the new spectral window parameters
    5122             :             // from newNUM_CHAN, newChanLoBound, and newChanHiBound 
    5123           0 :             newXout.resize(newNUM_CHAN);
    5124           0 :             newCHAN_WIDTH.resize(newNUM_CHAN);
    5125           0 :             newRESOLUTION.resize(newNUM_CHAN);
    5126           0 :             newEFFECTIVE_BW.resize(newNUM_CHAN);
    5127           0 :             for(Int i=0; i<newNUM_CHAN; i++){
    5128           0 :               newXout[i] = (newChanLoBound[i]+newChanHiBound[i])/2.;
    5129           0 :               newCHAN_WIDTH[i] = newChanHiBound[i]-newChanLoBound[i];
    5130           0 :               newRESOLUTION[i] = newCHAN_WIDTH[i]; // to be revisited
    5131           0 :               newEFFECTIVE_BW[i] = newCHAN_WIDTH[i]; // to be revisited
    5132             :             }
    5133             :             // set the reference frequency to the central frequency of the first channel,
    5134             :             // keeping the already changed frame
    5135           0 :             MVFrequency mvf(newXout[0]);
    5136           0 :             newREF_FREQUENCY.set(mvf);
    5137             :             
    5138             :             // trivial definition of the bandwidth (taking into account possibility of descending freqs)
    5139           0 :             newTOTAL_BANDWIDTH = max(newChanHiBound[newNUM_CHAN-1], newChanHiBound[0])
    5140           0 :               -min(newChanLoBound[0],newChanLoBound[newNUM_CHAN-1]);
    5141             : 
    5142             : //          // effective bandwidth needs to be interpolated in quadrature
    5143             : //          Vector<Double> newEffBWSquared(newNUM_CHAN);
    5144             : //          Vector<Double> oldEffBWSquared(oldEFFECTIVE_BW);
    5145             : //          for(Int i=0; i<oldNUM_CHAN; i++){
    5146             : //            oldEffBWSquared[i] *= oldEffBWSquared[i];
    5147             : //          }
    5148             : //          InterpolateArray1D<Double, Double>::interpolate(newEffBWSquared, newXout,
    5149             : //                                                             transNewXin,
    5150             : //                                                             oldEffBWSquared,
    5151             : //                                                             InterpolateArray1D<Double,Double>::linear);
    5152             : //          for(uInt i=0; i<newNUM_CHAN; i++){
    5153             : //            newEFFECTIVE_BW[i] = sqrt(newEffBWSquared[i]);
    5154             : //          }
    5155             : 
    5156           0 :             if(!allEQ(newXout, transNewXin)){ // grids are different
    5157           0 :               doRegrid = true;
    5158             :             }
    5159             :             
    5160           0 :           } // end if (regridQuant=="" ... 
    5161             :           
    5162           0 :           if(writeTables && (needTransform || doRegrid)){
    5163             :             // new SPW amd DD table rows may need to be created
    5164             :             
    5165             :             // Create new row in the SPW table (with ID nextSPWId) by copying
    5166             :             // all information from row theSPWId
    5167           0 :             if(!spwtable.canAddRow()){
    5168             :               os << LogIO::WARN
    5169             :                  << "Unable to add new row to SPECTRAL_WINDOW table. Cannot proceed with regridSpw ..." 
    5170           0 :                  << LogIO::POST;
    5171           0 :               return false; 
    5172             :             }
    5173             :             
    5174           0 :             numNewSPWIds++;
    5175           0 :             nextSPWId++;
    5176             :             
    5177             :             // prepare parameter string for later entry into MS history and report to logger
    5178             :             {    
    5179           0 :               ostringstream param;
    5180           0 :               param << "Regridded spectral window "
    5181           0 :                     << nextSPWId - origNumSPWs << " will be created for field " 
    5182           0 :                     << theFieldId << " with parameters " << endl
    5183           0 :                     << message << endl;
    5184           0 :               regridMessage += param.str(); // append
    5185           0 :               os << LogIO::NORMAL << param.str() << LogIO::POST;
    5186           0 :             }
    5187             :             
    5188           0 :             spwtable.addRow();
    5189           0 :             TableRow SPWRow(spwtable);
    5190           0 :             TableRecord spwRecord = SPWRow.get(theSPWId);
    5191             :             // TODO        Warn if the original channels are not contiguous or overlap!
    5192           0 :             SPWRow.putMatchingFields(nextSPWId, spwRecord);
    5193             :             
    5194             :             // and replacing the following columns with updated information:
    5195             :             // Store xout as new value of CHAN_FREQ.
    5196           0 :             chanFreqCol.put(nextSPWId, newXout);
    5197           0 :             numChanCol.put(nextSPWId, newNUM_CHAN);
    5198           0 :             chanWidthCol.put(nextSPWId,  newCHAN_WIDTH);
    5199           0 :             refFrequencyCol.put(nextSPWId, newREF_FREQUENCY.getValue());
    5200           0 :             if(theFrame==MFrequency::GEO){ // i.e. outframe was GEO or SOURCE
    5201           0 :               measFreqRefCol.put(nextSPWId, (Int)MFrequency::REST);
    5202             :             }
    5203             :             else{
    5204           0 :               measFreqRefCol.put(nextSPWId, (Int)theFrame);
    5205             :             }
    5206             : 
    5207           0 :             totalBandwidthCol.put(nextSPWId, newTOTAL_BANDWIDTH);
    5208           0 :             effectiveBWCol.put(nextSPWId, newEFFECTIVE_BW);
    5209           0 :             resolutionCol.put(nextSPWId, newRESOLUTION);
    5210             :             
    5211           0 :             msModified = true;
    5212             :             //   Create a new row in the DATA_DESCRIPTION table and enter
    5213             :             //   nextSPWId in the SPW_ID column, copy the polarization id and
    5214             :             //   the flag_row content from the old DATA_DESCRIPTION row.
    5215           0 :             if(!ddtable.canAddRow()){
    5216             :               os << LogIO::WARN
    5217             :                  << "Unable to add new row to DATA_DESCRIPTION table.  Cannot proceed with regridSpw ..." 
    5218           0 :                  << LogIO::POST;
    5219           0 :               return false; 
    5220             :             }
    5221           0 :             numNewDataDesc++;
    5222           0 :             nextDataDescId++;
    5223           0 :             ddtable.addRow();
    5224           0 :             TableRow DDRow(ddtable);
    5225           0 :             TableRecord DDRecord = DDRow.get(theDataDescId);
    5226           0 :             DDRow.putMatchingFields(nextDataDescId, DDRecord);
    5227             : 
    5228             :             // anticipate the deletion of the original SPW table rows
    5229           0 :             SPWIdCol.put(nextDataDescId, nextSPWId - origNumSPWs); 
    5230             :             
    5231             :             // writing the value of nextDataDescId into the DATA_DESC_ID cell
    5232             :             // of the present MAIN table row.  will be done in the main regridSpw
    5233             :             // method
    5234           0 :             theDataDescId = nextDataDescId;
    5235             :           
    5236           0 :           } // end if(writeTables && (needTransform || doRegrid)
    5237             : 
    5238           0 :         } // end if there is a reusable SPW row
    5239             : 
    5240           0 :         if(writeTables && (needTransform || doRegrid)){
    5241             :           // a new SOURCE table row has to be created
    5242             : 
    5243             :           // Add a row to the SOURCE table by copying the contents from the row
    5244             :           // identified by the SOURCE_ID cell in the row theFieldId from the
    5245             :           // FIELD table. Set the value of the cell SPECTRAL_WINDOW_ID in this
    5246             :           // new row to the value nextSPWId.
    5247           0 :           if(nextSourceRow>=0){ // there is a source table
    5248           0 :             if(!p_sourcetable->canAddRow()){
    5249             :               os << LogIO::WARN
    5250             :                  << "Unable to add new row to SOURCE table. Cannot proceed with regridSpw ..." 
    5251           0 :                  << LogIO::POST;
    5252           0 :               return false; 
    5253             :             }
    5254           0 :             numNewSourceRows++;
    5255           0 :             nextSourceRow++;
    5256             :             // find the row in the SOURCE table which has
    5257             :             // SOURCE_ID==theSOURCEId and SPW_ID==theSPWId
    5258           0 :             Int theSOURCEId = FIELDsourceIdCol(theFieldId);
    5259           0 :             ScalarColumn<Int> SOURCEsourceIdCol = p_sourceCol->sourceId();
    5260           0 :             ScalarColumn<Int> SOURCESPWIdCol = p_sourceCol->spectralWindowId();
    5261           0 :             Int foundRow = -1;
    5262           0 :             for(int i=0; i<nextSourceRow; i++){
    5263           0 :               if(SOURCEsourceIdCol(i) == theSOURCEId && (SOURCESPWIdCol(i)==theSPWId || SOURCESPWIdCol(i)==-1)){
    5264           0 :                 foundRow = i;
    5265           0 :                 break;
    5266             :               }
    5267             :             }
    5268           0 :             if(foundRow<0){ 
    5269             :               os << LogIO::SEVERE << "Incoherent MS: Did not find SOURCE table entry with SOURCE_ID == " 
    5270             :                  << theSOURCEId << " and  SPECTRAL_WINDOW_ID == " << theSPWId << endl
    5271             :                  <<" even though the FIELD and the DATA_DESCRIPTION table entries for main table row " 
    5272             :                  << mainTabRow << " refer to it." 
    5273           0 :                  << LogIO::POST;
    5274           0 :               return false;
    5275             :             }
    5276             :             else { // found matching row
    5277           0 :               p_sourcetable->addRow();
    5278           0 :               TableRow SOURCERow(*p_sourcetable);
    5279           0 :               TableRecord SOURCERecord = SOURCERow.get(foundRow);
    5280           0 :               SOURCERow.putMatchingFields(nextSourceRow, SOURCERecord);
    5281             : 
    5282             :               // anticipate the deletion of the original SPW rows
    5283           0 :               SOURCESPWIdCol.put(nextSourceRow, nextSPWId - origNumSPWs);
    5284           0 :               newSourceIds.push_back(theSOURCEId);
    5285           0 :               newSourceSPWIds.push_back(nextSPWId - origNumSPWs);
    5286           0 :             }
    5287             :               
    5288           0 :           } // end if there is a source table
    5289             :         }
    5290             : 
    5291           0 :         weightScale = newCHAN_WIDTH[0]/transCHAN_WIDTH[0]; // = deltaNuNew/deltaNuOld (both in outframe)
    5292             : 
    5293             :         //Put a new row into the "done" table.
    5294             :         // (do all the push_backs in one place)
    5295           0 :         oldSpwId.push_back(theSPWId);
    5296           0 :         oldFieldId.push_back(theFieldId);
    5297             : 
    5298             :         // anticipate the deletion of the original DD rows
    5299           0 :         newDataDescId.push_back(theDataDescId - origNumDataDescs);
    5300             : 
    5301           0 :         weightScaleV.push_back(weightScale);
    5302           0 :         xold.push_back(newXin);
    5303           0 :         xin.push_back(transNewXin);
    5304           0 :         xout.push_back(newXout);
    5305           0 :         method.push_back(theMethod);
    5306           0 :         regrid.push_back(doRegrid);
    5307           0 :         transform.push_back(needTransform);
    5308           0 :         theFieldDirV.push_back(theFieldDir);
    5309           0 :         mObsPosV.push_back(mObsPos);
    5310           0 :         fromFrameTypeV.push_back(theOldRefFrame);
    5311           0 :         outFrameV.push_back(outFrame);
    5312           0 :         outRadVelV.push_back(outRadVel);
    5313             : 
    5314           0 :       } // end if(!alreadyDone)
    5315             :       // reference frame transformation and regridding of channel definition completed
    5316             :       ////////////////////
    5317             : 
    5318           0 :     } // end loop over main table
    5319             : 
    5320           0 :     if(writeTables && msModified){
    5321             :       // delete the original rows in DD, SPW, and SOURCE if necessary
    5322           0 :       if(numNewDataDesc>0){
    5323           0 :         for(Int i=0; i<origNumDataDescs; i++){
    5324           0 :           ddtable.removeRow(0);
    5325             :         }
    5326             :       }
    5327           0 :       if(numNewSPWIds>0){
    5328           0 :         for(Int i=0; i<origNumSPWs; i++){
    5329           0 :           spwtable.removeRow(0);
    5330             :         }
    5331             :       }
    5332           0 :       if(numNewSourceRows>0){
    5333           0 :         ScalarColumn<Int> sourceIdCol = p_sourceCol->sourceId();
    5334           0 :         ScalarColumn<Int> spwIdCol = p_sourceCol->spectralWindowId();
    5335             :         // delete duplicate rows among the original Source rows
    5336           0 :         vector<Bool> tBRemRows(origNumSourceRows, false);
    5337           0 :         for(Int i=0; i<origNumSourceRows-1; i++){
    5338           0 :           if(tBRemRows[i]){
    5339           0 :             continue;
    5340             :           }
    5341           0 :           Int tSId = sourceIdCol(i);
    5342           0 :           Int tSpwId = spwIdCol(i);
    5343           0 :           for(Int j=i+1; j<origNumSourceRows; j++){
    5344           0 :             if(sourceIdCol(j)==tSId && spwIdCol(j)==tSpwId){
    5345           0 :               tBRemRows[j]=true;
    5346             :             }
    5347             :           }
    5348             :         }
    5349           0 :         for(Int i=origNumSourceRows-1; i>0; i--){
    5350           0 :           if(tBRemRows[i]){
    5351           0 :             p_sourcetable->removeRow(i);
    5352           0 :             origNumSourceRows--; 
    5353             :           }
    5354             :         }
    5355             : 
    5356             :         // delete those original Source rows with Source/SPW ID pairs also present among the new ones
    5357           0 :         uInt sIndex=0;
    5358           0 :         for(Int i=0; i<origNumSourceRows; i++){
    5359           0 :           Bool sFound=false;
    5360           0 :           for(uInt j=0; j<newSourceIds.size(); j++){
    5361           0 :             if(sourceIdCol(sIndex)==newSourceIds[j] && spwIdCol(sIndex)==newSourceSPWIds[j]){
    5362           0 :               sFound=true;
    5363           0 :               break;
    5364             :             }
    5365             :           }
    5366           0 :           if(sFound){
    5367           0 :             p_sourcetable->removeRow(sIndex);
    5368             :           }
    5369             :           else{
    5370           0 :             sIndex++;
    5371             :           }
    5372             :         }
    5373           0 :       }
    5374             : 
    5375             :       // prepare parameter string for later entry into MS history
    5376           0 :       ostringstream param;
    5377           0 :       param << "Added " << numNewDataDesc
    5378           0 :             << " new rows to the DATA_DESCRIPTION table and deleted "
    5379           0 :             << origNumDataDescs << " old ones." << endl
    5380           0 :             << "Added " << numNewSPWIds
    5381           0 :             << " rows to the SPECTRAL_WINDOW table and deleted " << origNumSPWs 
    5382           0 :             << " old ones." << endl
    5383           0 :             << "Added " << numNewSourceRows
    5384           0 :             << " rows to the SOURCE table and deleted " << origNumSourceRows
    5385           0 :             << " old ones.";
    5386           0 :       regridMessage += param.str() + "\n"; // append
    5387             : 
    5388           0 :       os << LogIO::NORMAL << param.str() <<  LogIO::POST;
    5389           0 :     }
    5390             : 
    5391           0 :     delete p_sourceCol;
    5392             : 
    5393           0 :     return rval;
    5394           0 :   }
    5395             : 
    5396             : 
    5397           0 :   Bool SubMS::combineSpws(const Vector<Int>& spwids,
    5398             :                           const Bool noModify,
    5399             :                           Vector<Double>& newCHAN_FREQ,
    5400             :                           Vector<Double>& newCHAN_WIDTH,
    5401             :                           Bool verbose){
    5402             : 
    5403             : 
    5404             :     using casacore::operator*;
    5405             :     
    5406           0 :     LogIO os(LogOrigin("SubMS", "combineSpws()"));
    5407             :       
    5408             :     // Analyse spwids
    5409             : 
    5410           0 :     if(spwids.nelements()==0){
    5411           0 :       os << LogIO::WARN << "No SPWs selected for combination ..." <<  LogIO::POST;
    5412           0 :       return true;
    5413             :     }
    5414             : 
    5415           0 :     String tempNewName = ms_p.tableName()+".spwCombined"; // temporary name for the MS to store the result
    5416             : 
    5417             :     { // begin scope for MS related objects
    5418             : 
    5419             :       // find all existing spws, 
    5420           0 :       MSSpectralWindow spwtable = ms_p.spectralWindow();
    5421           0 :       Int origNumSPWs = spwtable.nrow();
    5422           0 :       Int newSPWId = origNumSPWs;
    5423             : 
    5424           0 :       vector<Int> spwsToCombine;
    5425           0 :       Vector<Bool> includeIt(origNumSPWs, false);
    5426             : 
    5427             :       // jagonzal: This covers for the case when we want to combine all the input SPWs
    5428           0 :       if(spwids(0) == -1)
    5429             :       {
    5430           0 :           for(Int i=0; i<origNumSPWs; i++)
    5431             :           {
    5432           0 :                   spwsToCombine.push_back(i);
    5433           0 :                   includeIt(i) = true;
    5434             :           }
    5435             :       }
    5436             :       // jagonzal: Nominal case when we want to combine a sub-set of the input SPWs
    5437             :       else
    5438             :       {
    5439           0 :           for(uInt i=0; i<spwids.nelements(); i++)
    5440             :           {
    5441           0 :                   if(spwids(i)<origNumSPWs && spwids(i)>=0)
    5442             :                   {
    5443           0 :                           spwsToCombine.push_back(spwids(i));
    5444           0 :                           includeIt(spwids(i)) = true;
    5445             :                   }
    5446             :                   else
    5447             :                   {
    5448           0 :                           os << LogIO::SEVERE << "Invalid SPW ID selected for combination " << spwids(i)
    5449           0 :                                           << "valid range is 0 - " << origNumSPWs-1 << ")" << LogIO::POST;
    5450           0 :                           return false;
    5451             :                   }
    5452             :           }
    5453             :       }
    5454             :       // jagonzal: Marginal case when there is no actual SPW combination
    5455           0 :       if(spwsToCombine.size()<=1)
    5456             :       {
    5457           0 :           if(verbose)
    5458             :           {
    5459           0 :                   os << LogIO::NORMAL << "Less than two SPWs selected. No combination necessary." << LogIO::POST;
    5460             :           }
    5461           0 :           return true;
    5462             :       }
    5463             :       
    5464             :       // sort the spwids
    5465           0 :       std::sort(spwsToCombine.begin(), spwsToCombine.end());
    5466             : 
    5467           0 :       uInt nSpwsToCombine = spwsToCombine.size();
    5468             : 
    5469             :       // prepare access to the SPW table
    5470           0 :       MSSpWindowColumns SPWColrs(spwtable);
    5471           0 :       ScalarColumn<Int> numChanColr = SPWColrs.numChan(); 
    5472           0 :       ArrayColumn<Double> chanFreqColr = SPWColrs.chanFreq(); 
    5473           0 :       ArrayColumn<Double> chanWidthColr = SPWColrs.chanWidth(); 
    5474             :       //    ArrayMeasColumn<MFrequency> chanFreqMeasColr = SPWColrs.chanFreqMeas();
    5475           0 :       ScalarColumn<Int> measFreqRefColr = SPWColrs.measFreqRef();
    5476           0 :       ArrayColumn<Double> effectiveBWColr = SPWColrs.effectiveBW();   
    5477           0 :       ScalarColumn<Double> refFrequencyColr = SPWColrs.refFrequency(); 
    5478             :       //    ScalarMeasColumn<MFrequency> refFrequencyMeasColr = SPWColrs.refFrequencyMeas(); 
    5479           0 :       ArrayColumn<Double> resolutionColr = SPWColrs.resolution(); 
    5480           0 :       ScalarColumn<Double> totalBandwidthColr = SPWColrs.totalBandwidth();
    5481             : 
    5482             :       // create a list of the spw ids sorted by first (lowest) channel frequency
    5483           0 :       vector<Int> spwsSorted(nSpwsToCombine);
    5484           0 :       Vector<Bool> isDescending(origNumSPWs, false);
    5485           0 :       Bool negChanWidthWarned = false;
    5486             :       {
    5487           0 :         Double* firstFreq = new Double[nSpwsToCombine];
    5488           0 :         uInt count = 0;
    5489           0 :         for(uInt i=0; (Int)i<origNumSPWs; i++){
    5490           0 :           if(includeIt(i)){
    5491           0 :             Vector<Double> CHAN_FREQ(chanFreqColr(i));
    5492             :             // if frequencies are ascending, take the first channel, otherwise the last
    5493           0 :             uInt nCh = CHAN_FREQ.nelements();
    5494           0 :             if(CHAN_FREQ(0)<=CHAN_FREQ(nCh-1)){
    5495           0 :               firstFreq[count] = CHAN_FREQ(0);
    5496             :             }
    5497             :             else{
    5498           0 :               firstFreq[count] = CHAN_FREQ(nCh-1);
    5499           0 :               isDescending(i) = true;
    5500             :             }      
    5501           0 :             count++;
    5502           0 :           }
    5503             :         }
    5504           0 :         Sort sort;
    5505           0 :         sort.sortKey (firstFreq, TpDouble); // define sort key
    5506           0 :         Vector<uInt> inx(nSpwsToCombine);
    5507           0 :         sort.sort(inx, nSpwsToCombine);
    5508           0 :         for (uInt i=0; i<nSpwsToCombine; i++) {
    5509           0 :           spwsSorted[i] = spwsToCombine[inx(i)];
    5510             :         }
    5511           0 :         delete[] firstFreq;
    5512           0 :       }
    5513             : 
    5514           0 :       Int id0 = spwsSorted[0];
    5515             : 
    5516           0 :       uInt newNUM_CHAN = numChanColr(id0);
    5517           0 :       newCHAN_FREQ.assign(chanFreqColr(id0));
    5518           0 :       newCHAN_WIDTH.assign(chanWidthColr(id0));
    5519           0 :       Bool newIsDescending = isDescending(id0);
    5520             :       {
    5521           0 :         Bool negativeWidths = false;
    5522           0 :         for(uInt i=0; i<newNUM_CHAN; i++){
    5523           0 :           if(newCHAN_WIDTH(i) < 0.){
    5524           0 :             negativeWidths = true;
    5525           0 :             newCHAN_WIDTH(i) = -newCHAN_WIDTH(i);
    5526             :           }
    5527             :         }
    5528           0 :         if(negativeWidths && verbose){
    5529             :           os << LogIO::NORMAL
    5530             :              << " *** Encountered negative channel widths in SPECTRAL_WINDOW table."
    5531           0 :              << LogIO::POST;
    5532           0 :           negChanWidthWarned = true;
    5533             :         }
    5534             :       }
    5535             : 
    5536           0 :       if(newIsDescending){ // need to reverse the order for processing 
    5537           0 :         Vector<Double> tempF, tempW;
    5538           0 :         tempF.assign(newCHAN_FREQ);
    5539           0 :         tempW.assign(newCHAN_WIDTH);
    5540           0 :         for(uInt i=0; i<newNUM_CHAN; i++){
    5541           0 :           newCHAN_FREQ(i) = tempF(newNUM_CHAN-1-i);
    5542           0 :           newCHAN_WIDTH(i) = tempW(newNUM_CHAN-1-i);
    5543             :         }
    5544           0 :       }
    5545             : 
    5546           0 :       Vector<Double> newEFFECTIVE_BW(effectiveBWColr(id0));
    5547           0 :       Double newREF_FREQUENCY(refFrequencyColr(id0));
    5548             :       //MFrequency newREF_FREQUENCY = refFrequencyMeasColr(id0);
    5549           0 :       Int newMEAS_FREQ_REF = measFreqRefColr(id0);
    5550           0 :       Vector<Double> newRESOLUTION(resolutionColr(id0));
    5551           0 :       Double newTOTAL_BANDWIDTH = totalBandwidthColr(id0);
    5552             : 
    5553           0 :       vector<Int> averageN; // for each new channel store the number of old channels to average over
    5554           0 :       vector<vector<Int> > averageWhichSPW; // for each new channel store the
    5555             :       // (old) SPWs to average over  
    5556           0 :       vector<vector<Int> > averageWhichChan; // for each new channel store the
    5557             :       // channel numbers to av. over
    5558           0 :       vector<vector<Double> > averageChanFrac; // for each new channel store the
    5559             :       // channel fraction for each old channel
    5560             : 
    5561             :       // initialise the averaging vectors
    5562           0 :       for(uInt i=0; i<newNUM_CHAN; i++){
    5563           0 :         averageN.push_back(1);
    5564           0 :         vector<Int> tv; // just a temporary auxiliary vector
    5565           0 :         tv.push_back(id0);
    5566           0 :         averageWhichSPW.push_back(tv);
    5567           0 :         if(newIsDescending){
    5568           0 :           tv[0] = newNUM_CHAN-1-i;
    5569             :         }
    5570             :         else{
    5571           0 :           tv[0] = i;
    5572             :         }
    5573           0 :         averageWhichChan.push_back(tv);
    5574           0 :         vector<Double> tvd; // another one
    5575           0 :         tvd.push_back(1.); 
    5576           0 :         averageChanFrac.push_back(tvd);
    5577           0 :       }
    5578             : 
    5579           0 :       if(verbose){
    5580           0 :         os << LogIO::NORMAL << "Input SPWs sorted by first (lowest) channel frequency:" << LogIO::POST;
    5581             : 
    5582           0 :         ostringstream oss; // needed for iomanip functions
    5583           0 :         oss << "   SPW " << std::setw(3) << id0 << ": " << std::setw(5) << newNUM_CHAN 
    5584           0 :             << " channels, first channel = " << std::setprecision(9) << std::setw(14) << std::scientific << newCHAN_FREQ(0) << " Hz";
    5585           0 :         if(newNUM_CHAN>1){
    5586           0 :           oss << ", last channel = " << std::setprecision(9) << std::setw(14) << std::scientific << newCHAN_FREQ(newNUM_CHAN-1) << " Hz";
    5587             :         }
    5588           0 :         os << LogIO::NORMAL << oss.str() << LogIO::POST;
    5589           0 :       }
    5590             : 
    5591             :       // loop over remaining given spws
    5592           0 :       for(uInt i=1; i<nSpwsToCombine; i++){
    5593           0 :         Int idi = spwsSorted[i];
    5594             :       
    5595           0 :         uInt newNUM_CHANi = numChanColr(idi);
    5596           0 :         Vector<Double> newCHAN_FREQi(chanFreqColr(idi));
    5597           0 :         Vector<Double> newCHAN_WIDTHi(chanWidthColr(idi));
    5598           0 :         Bool newIsDescendingI = isDescending(idi);
    5599             :         {
    5600           0 :           Bool negativeWidths = false;
    5601           0 :           for(uInt ii=0; ii<newNUM_CHANi; ii++){
    5602           0 :             if(newCHAN_WIDTHi(ii) < 0.){
    5603           0 :               negativeWidths = true;
    5604           0 :               newCHAN_WIDTHi(ii) = -newCHAN_WIDTHi(ii);
    5605             :             }
    5606             :           }
    5607           0 :           if(negativeWidths && !negChanWidthWarned && verbose){
    5608             :             os << LogIO::NORMAL
    5609             :                << " *** Encountered negative channel widths in SPECTRAL_WINDOW table."
    5610           0 :                << LogIO::POST;
    5611           0 :             negChanWidthWarned = true;
    5612             :           }
    5613             :         }
    5614           0 :         if(newIsDescendingI){ // need to reverse the order for processing 
    5615           0 :           Vector<Double> tempF, tempW;
    5616           0 :           tempF.assign(newCHAN_FREQi);
    5617           0 :           tempW.assign(newCHAN_WIDTHi);
    5618           0 :           for(uInt ii=0; ii<newNUM_CHANi; ii++){
    5619           0 :             newCHAN_FREQi(ii) = tempF(newNUM_CHANi-1-ii);
    5620           0 :             newCHAN_WIDTHi(ii) = tempW(newNUM_CHANi-1-ii);
    5621             :           }
    5622           0 :         }
    5623             : 
    5624           0 :         Vector<Double> newEFFECTIVE_BWi(effectiveBWColr(idi));
    5625             :         //Double newREF_FREQUENCYi(refFrequencyColr(idi));
    5626             :         //MFrequency newREF_FREQUENCYi = refFrequencyMeasColr(idi);
    5627           0 :         Int newMEAS_FREQ_REFi = measFreqRefColr(idi);
    5628           0 :         Vector<Double> newRESOLUTIONi(resolutionColr(idi));
    5629             :         //Double newTOTAL_BANDWIDTHi = totalBandwidthColr(idi);
    5630             : 
    5631           0 :         if(verbose){
    5632           0 :           ostringstream oss; // needed for iomanip functions
    5633           0 :           oss << "   SPW " << std::setw(3) << idi << ": " << std::setw(5) << newNUM_CHANi 
    5634           0 :               << " channels, first channel = " << std::setprecision(9) << std::setw(14) << std::scientific << newCHAN_FREQi(0) << " Hz";
    5635           0 :           if(newNUM_CHANi>1){
    5636           0 :             oss << ", last channel = " << std::setprecision(9) << std::setw(14) << std::scientific << newCHAN_FREQi(newNUM_CHANi-1) << " Hz";
    5637             :           }
    5638           0 :           os << LogIO::NORMAL << oss.str() << LogIO::POST;
    5639           0 :         }
    5640             : 
    5641           0 :         vector<Double> mergedChanFreq;
    5642           0 :         vector<Double> mergedChanWidth;
    5643           0 :         vector<Double> mergedEffBW;
    5644           0 :         vector<Double> mergedRes;
    5645           0 :         vector<Int> mergedAverageN;
    5646           0 :         vector<vector<Int> > mergedAverageWhichSPW;
    5647           0 :         vector<vector<Int> > mergedAverageWhichChan;
    5648           0 :         vector<vector<Double> > mergedAverageChanFrac;
    5649             : 
    5650             :         // check for compatibility
    5651           0 :         if(newMEAS_FREQ_REFi != newMEAS_FREQ_REF){
    5652             :           os << LogIO::WARN
    5653             :              << "SPW " << idi << " cannot be combined with SPW " << id0 << ". Non-matching ref. frame."
    5654           0 :              << LogIO::POST;
    5655           0 :           return false; 
    5656             :         }
    5657             : 
    5658             :         // append or prepend spw to new spw
    5659             :         // overlap at all?
    5660           0 :         if(newCHAN_FREQ(newNUM_CHAN-1) + newCHAN_WIDTH(newNUM_CHAN-1)/2. 
    5661           0 :            < newCHAN_FREQi(0) - newCHAN_WIDTHi(0)/2.) {
    5662             :           // no overlap, and need to append
    5663           0 :           for(uInt j=0; j<newNUM_CHAN; j++){
    5664           0 :             mergedChanFreq.push_back(newCHAN_FREQ(j));
    5665           0 :             mergedChanWidth.push_back(newCHAN_WIDTH(j));
    5666           0 :             mergedEffBW.push_back(newEFFECTIVE_BW(j));
    5667           0 :             mergedRes.push_back(newRESOLUTION(j));
    5668           0 :             mergedAverageN.push_back(averageN[j]);
    5669           0 :             mergedAverageWhichSPW.push_back(averageWhichSPW[j]);
    5670           0 :             mergedAverageWhichChan.push_back(averageWhichChan[j]);
    5671           0 :             mergedAverageChanFrac.push_back(averageChanFrac[j]);
    5672             :           }
    5673           0 :           vector<Int> tv;
    5674           0 :           tv.push_back(idi); // origin is spw idi
    5675           0 :           vector<Int> tv2;
    5676           0 :           tv2.push_back(0);
    5677           0 :           vector<Double> tvd;
    5678           0 :           tvd.push_back(1.); // fraction is 1.
    5679           0 :           for(uInt j=0; j<newNUM_CHANi; j++){
    5680           0 :             mergedChanFreq.push_back(newCHAN_FREQi(j));
    5681           0 :             mergedChanWidth.push_back(newCHAN_WIDTHi(j));
    5682           0 :             mergedEffBW.push_back(newEFFECTIVE_BWi(j));
    5683           0 :             mergedRes.push_back(newRESOLUTIONi(j));
    5684           0 :             mergedAverageN.push_back(1); // so far only one channel
    5685           0 :             mergedAverageWhichSPW.push_back(tv);
    5686           0 :             if(newIsDescendingI){
    5687           0 :               tv2[0] = newNUM_CHANi-1-j;
    5688             :             }
    5689             :             else{
    5690           0 :               tv2[0] = j;
    5691             :             }
    5692           0 :             mergedAverageWhichChan.push_back(tv2); // channel number is j
    5693           0 :             mergedAverageChanFrac.push_back(tvd);
    5694             :           }
    5695           0 :         }
    5696           0 :         else if( newCHAN_FREQ(0) - newCHAN_WIDTH(0)/2. 
    5697           0 :                  > newCHAN_FREQi(newNUM_CHANi-1) + newCHAN_WIDTHi(newNUM_CHANi-1)/2.){ 
    5698             :           // no overlap, need to prepend
    5699           0 :           vector<Int> tv;
    5700           0 :           tv.push_back(idi); // origin is spw idi
    5701           0 :           vector<Int> tv2;
    5702           0 :           tv2.push_back(0);
    5703           0 :           vector<Double> tvd;
    5704           0 :           tvd.push_back(1.); // fraction is 1.
    5705           0 :           for(uInt j=0; j<newNUM_CHANi; j++){
    5706           0 :             mergedChanFreq.push_back(newCHAN_FREQi(j));
    5707           0 :             mergedChanWidth.push_back(newCHAN_WIDTHi(j));
    5708           0 :             mergedEffBW.push_back(newEFFECTIVE_BWi(j));
    5709           0 :             mergedRes.push_back(newRESOLUTIONi(j));
    5710           0 :             mergedAverageN.push_back(1); // so far only one channel
    5711           0 :             mergedAverageWhichSPW.push_back(tv);
    5712           0 :             if(newIsDescendingI){
    5713           0 :               tv2[0] = newNUM_CHANi-1-j;
    5714             :             }
    5715             :             else{
    5716           0 :               tv2[0] = j;
    5717             :             }
    5718           0 :             mergedAverageWhichChan.push_back(tv2); // channel number is j
    5719           0 :             mergedAverageChanFrac.push_back(tvd);
    5720             :           }
    5721           0 :           for(uInt j=0; j<newNUM_CHAN; j++){
    5722           0 :             mergedChanFreq.push_back(newCHAN_FREQ(j));
    5723           0 :             mergedChanWidth.push_back(newCHAN_WIDTH(j));
    5724           0 :             mergedEffBW.push_back(newEFFECTIVE_BW(j));
    5725           0 :             mergedRes.push_back(newRESOLUTION(j));
    5726           0 :             mergedAverageN.push_back(averageN[j]);
    5727           0 :             mergedAverageWhichSPW.push_back(averageWhichSPW[j]);
    5728           0 :             mergedAverageWhichChan.push_back(averageWhichChan[j]);
    5729           0 :             mergedAverageChanFrac.push_back(averageChanFrac[j]);
    5730             :           }
    5731           0 :         }
    5732             :         else{ // there is overlap
    5733           0 :           Int id0StartChan = 0;
    5734           0 :           if(newCHAN_FREQi(0) - newCHAN_WIDTHi(0)/2. < 
    5735           0 :              newCHAN_FREQ(newNUM_CHAN-1) - newCHAN_WIDTH(newNUM_CHAN-1)/2.){
    5736             :             // spw idi starts before spw id0
    5737             : 
    5738             :             // some utilities for the averaging info
    5739           0 :             vector<Int> tv; // temporary vector
    5740           0 :             tv.push_back(idi); // origin is spw idi
    5741           0 :             vector<Int> tv2;
    5742           0 :             tv2.push_back(0);
    5743           0 :             vector<Double> tvd;
    5744           0 :             tvd.push_back(1.); // fraction is 1.
    5745             : 
    5746             :             // find the first overlapping channel and prepend non-overlapping channels
    5747           0 :             Double ubound0 = newCHAN_FREQ(0) + newCHAN_WIDTH(0)/2.;
    5748           0 :             Double lbound0 = newCHAN_FREQ(0) - newCHAN_WIDTH(0)/2.;
    5749           0 :             Double uboundk = 0.;
    5750           0 :             Double lboundk = 0.;              
    5751             :             uInt k;
    5752           0 :             for(k=0; k<newNUM_CHANi; k++){
    5753           0 :               uboundk = newCHAN_FREQi(k) + newCHAN_WIDTHi(k)/2.;
    5754           0 :               lboundk = newCHAN_FREQi(k) - newCHAN_WIDTHi(k)/2.;              
    5755           0 :               if(lbound0 < uboundk){
    5756           0 :                 break;
    5757             :               }
    5758           0 :               mergedChanFreq.push_back(newCHAN_FREQi(k));
    5759           0 :               mergedChanWidth.push_back(newCHAN_WIDTHi(k));
    5760           0 :               mergedEffBW.push_back(newEFFECTIVE_BWi(k));
    5761           0 :               mergedRes.push_back(newRESOLUTIONi(k));
    5762           0 :               mergedAverageN.push_back(1); // so far only one channel
    5763           0 :               mergedAverageWhichSPW.push_back(tv);
    5764           0 :               if(newIsDescendingI){
    5765           0 :                 tv2[0] = newNUM_CHANi-1-k;
    5766             :               }
    5767             :               else{
    5768           0 :                 tv2[0] = k;
    5769             :               }
    5770           0 :               mergedAverageWhichChan.push_back(tv2); // channel number is k
    5771           0 :               mergedAverageChanFrac.push_back(tvd);         
    5772             :             }
    5773             :             // k's the one
    5774           0 :             if(lbound0 < uboundk && lboundk < lbound0){ // actual overlap, need to merge channel k with channel 0
    5775           0 :               Double newWidth = ubound0 - lboundk;
    5776           0 :               Double newCenter = lboundk + newWidth/2.;
    5777           0 :               mergedChanFreq.push_back(newCenter);
    5778           0 :               mergedChanWidth.push_back(newWidth);
    5779           0 :               mergedEffBW.push_back(newWidth); 
    5780           0 :               mergedRes.push_back(newWidth); 
    5781           0 :               mergedAverageN.push_back(averageN[0]+1); // one more channel contributes
    5782             :               // channel k is from spw idi
    5783           0 :               if(newIsDescendingI){
    5784           0 :                 tv2[0] = newNUM_CHANi-1-k;
    5785             :               }
    5786             :               else{
    5787           0 :                 tv2[0] = k;
    5788             :               }
    5789           0 :               for(int j=0; j<averageN[0]; j++){
    5790           0 :                 tv.push_back(averageWhichSPW[0][j]); // additional contributors
    5791           0 :                 tv2.push_back(averageWhichChan[0][j]); // channel 0 from spw id0
    5792           0 :                 tvd.push_back(averageChanFrac[0][j]);
    5793             :               }
    5794           0 :               mergedAverageWhichSPW.push_back(tv);
    5795           0 :               mergedAverageWhichChan.push_back(tv2); 
    5796           0 :               mergedAverageChanFrac.push_back(tvd);
    5797           0 :               id0StartChan = 1;
    5798             :             }
    5799           0 :           }
    5800             :           // now move along SPW id0 and merge until end of id0 is reached, then just copy
    5801           0 :           for(uInt j=id0StartChan; j<newNUM_CHAN; j++){
    5802           0 :             mergedChanFreq.push_back(newCHAN_FREQ(j));
    5803           0 :             mergedChanWidth.push_back(newCHAN_WIDTH(j));
    5804           0 :             mergedEffBW.push_back(newEFFECTIVE_BW(j));
    5805           0 :             mergedRes.push_back(newRESOLUTION(j));
    5806           0 :             for(uInt k=0; k<newNUM_CHANi; k++){
    5807           0 :               Double overlap_frac = 0.;
    5808             :               // does channel j in spw id0 overlap with channel k in spw idi?
    5809           0 :               Double uboundj = newCHAN_FREQ(j) + newCHAN_WIDTH(j)/2.;
    5810           0 :               Double uboundk = newCHAN_FREQi(k) + newCHAN_WIDTHi(k)/2.;
    5811           0 :               Double lboundj = newCHAN_FREQ(j) - newCHAN_WIDTH(j)/2.;
    5812           0 :               Double lboundk = newCHAN_FREQi(k) - newCHAN_WIDTHi(k)/2.;
    5813             :               // determine fraction 
    5814           0 :               if(lboundj <= lboundk && uboundk <= uboundj){ // chan k is completely covered by chan j
    5815           0 :                 overlap_frac = 1.;
    5816             :                 //cout << "j " << j << " k " << k << " case 1" << endl;
    5817             :                 //cout << "overlap " << overlap_frac << endl;
    5818             :               }
    5819           0 :               else if(lboundk <= lboundj && uboundj <= uboundk){ // chan j is completely covered by chan k 
    5820           0 :                 overlap_frac = newCHAN_WIDTH(j)/newCHAN_WIDTHi(k);
    5821             :                 //cout << "j " << j << " k " << k << " case 2" << endl;
    5822             :                 //cout << "overlap " << overlap_frac << endl;
    5823             :               }
    5824           0 :               else if(lboundj < lboundk && lboundk < uboundj && uboundj < uboundk){ // lower end of k is overlapping with j
    5825           0 :                 overlap_frac = (uboundj - lboundk)/newCHAN_WIDTHi(k);
    5826             :                 //cout << "j " << j << " k " << k << " case 3" << endl;
    5827             :                 //cout << "overlap " << overlap_frac << endl;
    5828             :               }
    5829           0 :               else if(lboundk < lboundj && lboundj < uboundk && lboundj < uboundk){ // upper end of k is overlapping with j 
    5830           0 :                 overlap_frac = (uboundk - lboundj)/newCHAN_WIDTHi(k);
    5831             :                 //cout << "j " << j << " k " << k << " case 4" << endl;
    5832             :                 //cout << "overlap " << overlap_frac << endl;
    5833             :               }
    5834           0 :               if(overlap_frac > 0.){ // update averaging info
    5835           0 :                 averageN[j] += 1;
    5836           0 :                 averageWhichSPW[j].push_back(idi);
    5837           0 :                 if(newIsDescendingI){
    5838           0 :                   averageWhichChan[j].push_back(newNUM_CHANi-1-k);
    5839             :                 }
    5840             :                 else{
    5841           0 :                   averageWhichChan[j].push_back(k);
    5842             :                 }
    5843           0 :                 averageChanFrac[j].push_back(overlap_frac);
    5844             :               }
    5845             :             } // end loop over spw idi
    5846             :             // append this channel with updated averaging info
    5847           0 :             mergedAverageN.push_back(averageN[j]);
    5848           0 :             mergedAverageWhichSPW.push_back(averageWhichSPW[j]);
    5849           0 :             mergedAverageWhichChan.push_back(averageWhichChan[j]);
    5850           0 :             mergedAverageChanFrac.push_back(averageChanFrac[j]);  
    5851             :           } // end loop over spw id0
    5852           0 :           if(newCHAN_FREQ(newNUM_CHAN-1) + newCHAN_WIDTH(newNUM_CHAN-1)/2.
    5853           0 :              < newCHAN_FREQi(newNUM_CHANi-1) + newCHAN_WIDTHi(newNUM_CHANi-1)/2.){// spw idi still continues!
    5854             :             // find the last overlapping channel
    5855           0 :             Int j = newNUM_CHAN-1;
    5856           0 :             Double uboundj = newCHAN_FREQ(j) + newCHAN_WIDTH(j)/2.;
    5857           0 :             Double lboundj = newCHAN_FREQ(j) - newCHAN_WIDTH(j)/2.;
    5858           0 :             Double uboundk = 0;
    5859           0 :             Double lboundk = 0;       
    5860             :             Int k;
    5861           0 :             for(k=newNUM_CHANi-1; k>=0; k--){
    5862           0 :               uboundk = newCHAN_FREQi(k) + newCHAN_WIDTHi(k)/2.;
    5863           0 :               lboundk = newCHAN_FREQi(k) - newCHAN_WIDTHi(k)/2.;              
    5864           0 :               if(lboundk <= uboundj){
    5865           0 :                 break;
    5866             :               }
    5867             :             }
    5868             :             // k's the one 
    5869           0 :             if(lboundk < uboundj && uboundj < uboundk ){ // actual overlap
    5870           0 :               Double overlap_frac = (uboundj - lboundk)/newCHAN_WIDTHi(k);
    5871           0 :               if(overlap_frac>0.01){ // merge channel k completely with channel j 
    5872           0 :                 Double newWidth = uboundk - lboundj;
    5873           0 :                 Double newCenter = (lboundj+uboundk)/2.;
    5874           0 :                 mergedChanFreq[j] =  newCenter;
    5875           0 :                 mergedChanWidth[j] = newWidth;
    5876           0 :                 mergedEffBW[j] = newWidth; 
    5877           0 :                 mergedRes[j] = newWidth; 
    5878           0 :                 mergedAverageChanFrac[j][mergedAverageN[j]-1] = 1.; 
    5879             :               }
    5880             :               else{ // create separate, (slightly) more narrow channel
    5881           0 :                 Double newWidth = uboundk - uboundj;
    5882           0 :                 Double newCenter = (uboundj+uboundk)/2.;
    5883           0 :                 vector<Int> tv;
    5884           0 :                 tv.push_back(idi); // origin is spw idi
    5885           0 :                 vector<Int> tv2;
    5886           0 :                 tv2.push_back(0);
    5887           0 :                 vector<Double> tvd;
    5888           0 :                 tvd.push_back(1.); // fraction is 1.
    5889           0 :                 mergedChanFreq.push_back(newCenter);
    5890           0 :                 mergedChanWidth.push_back(newWidth);
    5891           0 :                 mergedEffBW.push_back(newWidth);
    5892           0 :                 mergedRes.push_back(newWidth);
    5893           0 :                 mergedAverageN.push_back(1); // so far only one channel
    5894           0 :                 mergedAverageWhichSPW.push_back(tv);
    5895           0 :                 if(newIsDescendingI){
    5896           0 :                   tv2[0] = newNUM_CHANi-1-k;
    5897             :                 }
    5898             :                 else{
    5899           0 :                   tv2[0] = k;
    5900             :                 }
    5901           0 :                 mergedAverageWhichChan.push_back(tv2); // channel number is k
    5902           0 :                 mergedAverageChanFrac.push_back(tvd);
    5903           0 :               }
    5904           0 :               k++; // start appending remaining channels after k
    5905             :             }
    5906             :             // append the remaining channels
    5907           0 :             vector<Int> tv;
    5908           0 :             tv.push_back(idi); // origin is spw idi
    5909           0 :             vector<Int> tv2;
    5910           0 :             tv2.push_back(0);
    5911           0 :             vector<Double> tvd;
    5912           0 :             tvd.push_back(1.); // fraction is 1.
    5913           0 :             for(uInt m=k; m<newNUM_CHANi; m++){
    5914           0 :               mergedChanFreq.push_back(newCHAN_FREQi(m));
    5915           0 :               mergedChanWidth.push_back(newCHAN_WIDTHi(m));
    5916           0 :               mergedEffBW.push_back(newEFFECTIVE_BWi(m));
    5917           0 :               mergedRes.push_back(newRESOLUTIONi(m));
    5918           0 :               mergedAverageN.push_back(1); // so far only one channel
    5919           0 :               mergedAverageWhichSPW.push_back(tv);
    5920           0 :                 if(newIsDescendingI){
    5921           0 :                   tv2[0] = newNUM_CHANi-1-m;
    5922             :                 }
    5923             :                 else{
    5924           0 :                   tv2[0] = m;
    5925             :                 }
    5926           0 :               mergedAverageWhichChan.push_back(tv2); // channel number is m
    5927           0 :               mergedAverageChanFrac.push_back(tvd);
    5928             :             }
    5929           0 :           } // end if spw idi still continues
    5930             :         } // end if there is overlap    
    5931             : 
    5932             : 
    5933           0 :         newNUM_CHAN = mergedChanFreq.size();
    5934           0 :         newCHAN_FREQ.assign(Vector<Double>(mergedChanFreq));
    5935           0 :         newCHAN_WIDTH.assign(Vector<Double>(mergedChanWidth));
    5936           0 :         newEFFECTIVE_BW.assign(Vector<Double>(mergedEffBW));
    5937           0 :         newREF_FREQUENCY = newCHAN_FREQ(0); 
    5938           0 :         newTOTAL_BANDWIDTH = fabs(newCHAN_FREQ(newNUM_CHAN-1) - newCHAN_FREQ(0))
    5939           0 :           + fabs(newCHAN_WIDTH(newNUM_CHAN-1)/2.) + fabs(newCHAN_WIDTH(0)/2.);
    5940           0 :         newRESOLUTION.assign(Vector<Double>(mergedRes));
    5941           0 :         averageN = mergedAverageN;
    5942           0 :         averageWhichSPW = mergedAverageWhichSPW;
    5943           0 :         averageWhichChan = mergedAverageWhichChan;
    5944           0 :         averageChanFrac = mergedAverageChanFrac;
    5945             : 
    5946           0 :       } // end loop over SPWs
    5947             : 
    5948             :       // print channel fractions for debugging
    5949             : //       vector< vector<bool> > wasprinted;
    5950             : //       for(uInt i=0; i<newNUM_CHAN; i++){
    5951             : //      vector<bool> tboolv;
    5952             : //      cout << "i freq width " << i << " " << newCHAN_FREQ(i) << " " << newCHAN_WIDTH(i) << endl;
    5953             : //      for(Int j=0; j<averageN[i]; j++){
    5954             : //        cout << " i, j " << i << ", " << j << " averageWhichChan[i][j] " << averageWhichChan[i][j]
    5955             : //             << " averageWhichSPW[i][j] " << averageWhichSPW[i][j] << endl;
    5956             : //        cout << " averageChanFrac[i][j] " << averageChanFrac[i][j] << endl;
    5957             : //        tboolv.push_back(false);
    5958             : //      }
    5959             : //      wasprinted.push_back(tboolv);
    5960             : //       }
    5961             :       
    5962             :       
    5963           0 :       if(noModify){ // newCHAN_FREQ and newCHAN_WIDTH have been determined now
    5964           0 :         return true;
    5965             :       }
    5966             : 
    5967             :       // now need write access
    5968           0 :       MSSpWindowColumns SPWCols(spwtable);
    5969           0 :       ScalarColumn<Int> numChanCol = SPWCols.numChan(); 
    5970           0 :       ArrayColumn<Double> chanFreqCol = SPWCols.chanFreq(); 
    5971           0 :       ArrayColumn<Double> chanWidthCol = SPWCols.chanWidth(); 
    5972             :       //    ArrayMeasColumn<MFrequency> chanFreqMeasCol = SPWCols.chanFreqMeas();
    5973           0 :       ScalarColumn<Int> measFreqRefCol = SPWCols.measFreqRef();
    5974           0 :       ArrayColumn<Double> effectiveBWCol = SPWCols.effectiveBW();   
    5975           0 :       ScalarColumn<Double> refFrequencyCol = SPWCols.refFrequency(); 
    5976             :       //    ScalarMeasColumn<MFrequency> refFrequencyMeasCol = SPWCols.refFrequencyMeas(); 
    5977           0 :       ArrayColumn<Double> resolutionCol = SPWCols.resolution(); 
    5978           0 :       ScalarColumn<Double> totalBandwidthCol = SPWCols.totalBandwidth();
    5979             : 
    5980           0 :       if(verbose){
    5981             :         os << LogIO::NORMAL << "Combined SPW will have " << newNUM_CHAN 
    5982           0 :            << " channels. May change in later regridding." << LogIO::POST;
    5983             :       }
    5984             : 
    5985             :       // Create new row in the SPW table (with ID nextSPWId) by copying
    5986             :       // all information from row theSPWId
    5987           0 :       if(!spwtable.canAddRow()){
    5988             :         os << LogIO::WARN
    5989             :            << "Unable to add new row to SPECTRAL_WINDOW table. Cannot proceed with spwCombine ..." 
    5990           0 :            << LogIO::POST;
    5991           0 :         return false; 
    5992             :       }
    5993             : 
    5994           0 :       TableRow SPWRow(spwtable);
    5995           0 :       TableRecord spwRecord = SPWRow.get(id0);
    5996             : 
    5997             :       // write new spw to spw table (ID =  newSpwId)
    5998           0 :       spwtable.addRow();
    5999           0 :       SPWRow.putMatchingFields(newSPWId, spwRecord);
    6000             : 
    6001           0 :       chanFreqCol.put(newSPWId, newCHAN_FREQ);
    6002           0 :       numChanCol.put(newSPWId, newNUM_CHAN);
    6003           0 :       chanWidthCol.put(newSPWId,  newCHAN_WIDTH);
    6004           0 :       refFrequencyCol.put(newSPWId, newREF_FREQUENCY);
    6005           0 :       totalBandwidthCol.put(newSPWId, newTOTAL_BANDWIDTH);
    6006           0 :       effectiveBWCol.put(newSPWId, newEFFECTIVE_BW);
    6007           0 :       resolutionCol.put(newSPWId, newRESOLUTION);
    6008             : 
    6009             :       // delete unwanted spws and memorize the new ID of the new merged one.
    6010             :       // (remember the IDs were sorted above)
    6011           0 :       for(int i=nSpwsToCombine-1; i>=0; i--){ // remove highest row numbers first
    6012           0 :         spwtable.removeRow(spwsToCombine[i]);
    6013             :       }
    6014           0 :       newSPWId -= nSpwsToCombine; 
    6015             : 
    6016             :       // other tables to correct: MAIN, FREQ_OFFSET, SYSCAL, FEED, DATA_DESCRIPTION, SOURCE
    6017             : 
    6018             :       // 1) SOURCE (an optional table)
    6019           0 :       uInt numSourceRows = 0;
    6020           0 :       MSSource* p_sourcetable = NULL;
    6021           0 :       MSSourceColumns* p_sourceCol = NULL;
    6022           0 :       if(Table::isReadable(ms_p.sourceTableName())){
    6023           0 :         p_sourcetable = &(ms_p.source());
    6024           0 :         p_sourceCol = new MSSourceColumns(*p_sourcetable);
    6025           0 :         numSourceRows = p_sourcetable->nrow();
    6026           0 :         ScalarColumn<Int> SOURCESPWIdCol = p_sourceCol->spectralWindowId();
    6027             :         // loop over source table rows
    6028           0 :         for(uInt i=0; i<numSourceRows; i++){
    6029           0 :           for(uInt j=0; j<nSpwsToCombine; j++){
    6030             :             // if spw id affected, replace by newSpwId
    6031           0 :             if(SOURCESPWIdCol(i) == spwsToCombine[j]){ // source row i is affected
    6032           0 :               SOURCESPWIdCol.put(i, newSPWId);
    6033             :             }
    6034             :           } // end for j
    6035             :         }// end for i
    6036           0 :       }
    6037           0 :       else if(verbose){ // there is no source table
    6038           0 :         os << LogIO::NORMAL << "Note: MS contains no SOURCE table ..." << LogIO::POST;
    6039             :       }
    6040             : 
    6041             :       // 2) DATA_DESCRIPTION
    6042           0 :       MSDataDescription ddtable = ms_p.dataDescription();
    6043           0 :       uInt numDataDescs = ddtable.nrow();
    6044           0 :       MSDataDescColumns DDCols(ddtable);
    6045           0 :       ScalarColumn<Int> SPWIdCol = DDCols.spectralWindowId();
    6046           0 :       ScalarColumn<Int> PolIdCol = DDCols.polarizationId();
    6047           0 :       vector<uInt> affDDIds;  
    6048           0 :       vector<Bool> DDRowsToDelete(numDataDescs, false);
    6049           0 :       std::map<Int, Int> tempDDIndex; // store relation between old and new DD Ids
    6050           0 :       std::map<Int, Int> DDtoSPWIndex; // store relation between old DD Ids and old SPW Ids 
    6051             :       //  (only for affected SPW IDs)
    6052             :       // loop over DD table rows
    6053           0 :       for(uInt i=0; i<numDataDescs; i++){
    6054             :         // if spw id affected, replace by newSpwId
    6055           0 :         for(uInt j=0; j<nSpwsToCombine; j++){
    6056             :           // if spw id affected, replace by newSpwId
    6057           0 :           if(SPWIdCol(i) == spwsToCombine[j]){ // DD row i is affected
    6058             :             // correct the SPW Id in the DD table
    6059           0 :             SPWIdCol.put(i, newSPWId);
    6060             :             // memorize affected DD IDs in affDDIds
    6061           0 :             affDDIds.push_back(i);
    6062             :             // store relation between old DD Id and old SPW ID for later use in the modification of the MAIN table
    6063           0 :             DDtoSPWIndex.insert(std::pair<Int, Int>(i, spwsToCombine[j])); // note: this relation can be many-to-one  
    6064             :           }     
    6065             :         }
    6066             :       }
    6067             :       // Find redundant DD IDs
    6068             :       // loop over DD table rows
    6069           0 :       for(uInt i=0; i<numDataDescs; i++){
    6070           0 :         Bool affected = false;
    6071           0 :         for(uInt j=0; j<affDDIds.size(); j++){
    6072           0 :           if(i == affDDIds[j] && !DDRowsToDelete[i]){
    6073           0 :             affected = true;
    6074           0 :             break;
    6075             :           }
    6076             :         }
    6077           0 :         if(!affected){
    6078           0 :           continue;
    6079             :         }
    6080             :         else { // i is an affected row
    6081           0 :           Int PolIDi = PolIdCol(i);
    6082           0 :           Int SpwIDi = SPWIdCol(i);
    6083             :           // loop over following DD table rows
    6084           0 :           for(uInt j=i+1; j<numDataDescs; j++){
    6085             :             // if row i and row j redundant?
    6086           0 :             if(PolIDi == PolIdCol(j) && SpwIDi == SPWIdCol(j)){
    6087             :               // mark for deletion
    6088           0 :               DDRowsToDelete[j] = true;
    6089             :               // fill map for DDrenumbering
    6090           0 :               tempDDIndex.insert(std::pair<Int, Int>(j, i));
    6091             :             }
    6092             :           }    
    6093             :         } // end if affected 
    6094             :       }
    6095             :       // delete redundant DD rows
    6096           0 :       Int removed = 0;
    6097           0 :       for(uInt i=0; i<numDataDescs; i++){
    6098           0 :         if(DDRowsToDelete[i]){
    6099           0 :           ddtable.removeRow(i-removed);
    6100           0 :           removed++;
    6101             :         }
    6102             :         else{ // this row is not deleted but changes its number by <removed> due to removal of others
    6103           0 :         tempDDIndex.insert(std::pair<Int, Int>(i, i-removed));
    6104             :         }
    6105             :       }
    6106             : 
    6107             :       // 3) FEED  
    6108           0 :       MSFeed feedtable = ms_p.feed();
    6109           0 :       uInt numFeedRows = feedtable.nrow();
    6110           0 :       MSFeedColumns feedCols(feedtable);
    6111           0 :       ScalarColumn<Int> feedSPWIdCol = feedCols.spectralWindowId();
    6112             :  
    6113             :       // loop over FEED table rows
    6114           0 :       for(uInt i=0; i<numFeedRows; i++){
    6115             :         // if spw id affected, replace by newSpwId
    6116           0 :         for(uint j=0; j<nSpwsToCombine; j++){
    6117             :           // if spw id affected, replace by newSpwId
    6118           0 :           if(feedSPWIdCol(i) == spwsToCombine[j]){ // feed row i is affected
    6119           0 :             feedSPWIdCol.put(i, newSPWId);
    6120             :           }     
    6121             :         }
    6122             :       }
    6123             : 
    6124             :       // TODO: (possibly, not clear if necessary) remove redundant FEED rows and propagate
    6125             : 
    6126             :       // 4) SYSCAL
    6127             : 
    6128             :       // note: syscal is optional
    6129             : 
    6130           0 :       if(!ms_p.sysCal().isNull()){
    6131           0 :         MSSysCal sysCaltable = ms_p.sysCal();
    6132           0 :         uInt numSysCalRows = sysCaltable.nrow();
    6133           0 :         MSSysCalColumns sysCalCols(sysCaltable);
    6134           0 :         ScalarColumn<Int> sysCalSPWIdCol = sysCalCols.spectralWindowId();
    6135             :       
    6136             :         // loop over SYSCAL table rows
    6137           0 :         for(uInt i=0; i<numSysCalRows; i++){
    6138             :           // if spw id affected, replace by newSpwId
    6139           0 :           for(uInt j=0; j<nSpwsToCombine; j++){
    6140             :             // if spw id affected, replace by newSpwId
    6141           0 :             if(sysCalSPWIdCol(i) == spwsToCombine[j]){ // SysCal row i is affected
    6142           0 :               sysCalSPWIdCol.put(i, newSPWId);
    6143             :             }     
    6144             :           }
    6145             :         }
    6146           0 :       }
    6147             : 
    6148             :       // 5) FREQ_OFFSET
    6149             : 
    6150             :       // note: freq_offset is optional
    6151             : 
    6152           0 :       if(!ms_p.freqOffset().isNull()){
    6153           0 :         MSFreqOffset freqOffsettable = ms_p.freqOffset();
    6154           0 :         uInt numFreqOffsetRows = freqOffsettable.nrow();
    6155           0 :         MSFreqOffsetColumns freqOffsetCols(freqOffsettable);
    6156           0 :         ScalarColumn<Int> freqOffsetSPWIdCol = freqOffsetCols.spectralWindowId();
    6157             :       
    6158             :         // loop over FREQ_OFFSET table rows
    6159           0 :         for(uInt i=0; i<numFreqOffsetRows; i++){
    6160             :           // if spw id affected, replace by newSpwId
    6161           0 :           for(uInt j=0; j<nSpwsToCombine; j++){
    6162             :             // if spw id affected, replace by newSpwId
    6163           0 :             if(freqOffsetSPWIdCol(i) == spwsToCombine[j]){ // FreqOffset row i is affected
    6164           0 :               freqOffsetSPWIdCol.put(i, newSPWId);
    6165             :             }     
    6166             :           }
    6167             :         }
    6168           0 :       }
    6169             : 
    6170             :       // 6) MAIN
    6171             : 
    6172             :       Table newMain(TableCopy::makeEmptyTable( tempNewName,
    6173           0 :                                                Record(),
    6174           0 :                                                (Table) ms_p,
    6175             :                                                Table::New,
    6176             :                                                Table::AipsrcEndian,
    6177             :                                                true, // replaceTSM 
    6178             :                                                true // noRows
    6179             :                                                )
    6180           0 :                     );
    6181             :       
    6182           0 :       TableCopy::copySubTables(newMain, ms_p, false);
    6183             :       
    6184           0 :       MSMainColumns mainCols((MeasurementSet&)newMain);
    6185           0 :       MSMainColumns oldMainCols(ms_p);
    6186             :       
    6187           0 :       uInt nMainTabRows = ms_p.nrow();
    6188             :       
    6189             :       // columns which depend on the number of frequency channels and may need to be combined:
    6190             :       // DATA, FLOAT_DATA, CORRECTED_DATA, MODEL_DATA, LAG_DATA, SIGMA_SPECTRUM,
    6191             :       // WEIGHT_SPECTRUM, FLAG, and FLAG_CATEGORY    
    6192           0 :       ArrayColumn<Complex> CORRECTED_DATACol =  mainCols.correctedData();
    6193           0 :       ArrayColumn<Complex> oldCORRECTED_DATACol = oldMainCols.correctedData();
    6194           0 :       ArrayColumn<Complex>  DATACol =  mainCols.data();
    6195           0 :       ArrayColumn<Complex>  oldDATACol = oldMainCols.data();
    6196           0 :       ArrayColumn<Float> FLOAT_DATACol =  mainCols.floatData();
    6197           0 :       ArrayColumn<Float> oldFLOAT_DATACol = oldMainCols.floatData();
    6198           0 :       ArrayColumn<Complex> LAG_DATACol =  mainCols.lagData();
    6199           0 :       ArrayColumn<Complex> oldLAG_DATACol = oldMainCols.lagData();
    6200           0 :       ArrayColumn<Complex> MODEL_DATACol =  mainCols.modelData();
    6201           0 :       ArrayColumn<Complex> oldMODEL_DATACol = oldMainCols.modelData();
    6202           0 :       ArrayColumn<Float> SIGMA_SPECTRUMCol =  mainCols.sigmaSpectrum();
    6203           0 :       ArrayColumn<Float> oldSIGMA_SPECTRUMCol = oldMainCols.sigmaSpectrum();
    6204           0 :       ArrayColumn<Float> WEIGHT_SPECTRUMCol =  mainCols.weightSpectrum();
    6205           0 :       ArrayColumn<Float> oldWEIGHT_SPECTRUMCol = oldMainCols.weightSpectrum();
    6206           0 :       ArrayColumn<Bool> FLAGCol =  mainCols.flag();
    6207           0 :       ArrayColumn<Bool> oldFLAGCol = oldMainCols.flag();
    6208           0 :       ArrayColumn<Bool> FLAG_CATEGORYCol =  mainCols.flagCategory();
    6209           0 :       ArrayColumn<Bool> oldFLAG_CATEGORYCol = oldMainCols.flagCategory();
    6210             :       
    6211             :       // columns which may be different for otherwise matching main table rows
    6212             :       //  and need to be combined
    6213           0 :       ScalarColumn<Bool> flagRowCol = oldMainCols.flagRow();
    6214             : 
    6215             :       // administrational columns needed from the main table
    6216           0 :       ArrayColumn<Float> SIGMACol =  oldMainCols.sigma();
    6217           0 :       ScalarColumn<Int> fieldCol = oldMainCols.fieldId();
    6218           0 :       ScalarColumn<Int> DDIdCol = oldMainCols.dataDescId();
    6219           0 :       ScalarColumn<Int> antenna1Col = oldMainCols.antenna1();
    6220           0 :       ScalarColumn<Int> antenna2Col = oldMainCols.antenna2();
    6221           0 :       ScalarColumn<Double> timeCol = oldMainCols.time(); 
    6222           0 :       ScalarColumn<Double> intervalCol = oldMainCols.interval();
    6223           0 :       ScalarColumn<Double> exposureCol = oldMainCols.exposure();
    6224           0 :       ScalarMeasColumn<MEpoch> mainTimeMeasCol = oldMainCols.timeMeas();
    6225             : 
    6226             :       // arrays for composing the combined columns 
    6227             :       // model them on the first affected row of the main table
    6228             :       
    6229           0 :       Matrix<Complex> newCorrectedData; 
    6230           0 :       Matrix<Complex> newData;
    6231           0 :       Matrix<Float> newFloatData;
    6232           0 :       Matrix<Complex> newLagData;
    6233           0 :       Matrix<Complex> newModelData;
    6234           0 :       Matrix<Float> newSigmaSpectrum;
    6235           0 :       Matrix<Float> newWeightSpectrum;
    6236           0 :       Matrix<Bool> newFlag;
    6237           0 :       Array<Bool> newFlagCategory; // has three dimensions
    6238             :       Bool newFlagRow; 
    6239             : 
    6240             :       // create time-sorted index for main table access
    6241           0 :       Vector<uInt> sortedI(nMainTabRows);
    6242             :       {
    6243           0 :         Vector<Double> mainTimesV = oldMainCols.time().getColumn();
    6244           0 :         GenSortIndirect<Double>::sort(sortedI,mainTimesV);
    6245           0 :       }
    6246             : 
    6247             :       // find the first row affected by the spw combination
    6248           0 :       Int firstAffRow = 0;
    6249           0 :       for(uInt mRow=0; mRow<nMainTabRows; mRow++){
    6250           0 :         uInt sortedMRow = sortedI(mRow);
    6251           0 :         if(DDtoSPWIndex.find(DDIdCol(sortedMRow)) != DDtoSPWIndex.end( )){
    6252           0 :           firstAffRow = sortedMRow;
    6253           0 :           break;
    6254             :         }
    6255             :       }
    6256             :       
    6257             :       // get the number of correlations from the
    6258             :       // dimension of the first axis of the sigma column
    6259           0 :       uInt nCorrelations = SIGMACol(firstAffRow).shape()(0); 
    6260             :       
    6261           0 :       IPosition newShape = IPosition(2, nCorrelations, newNUM_CHAN);
    6262             :       
    6263           0 :       Bool CORRECTED_DATAColIsOK = !CORRECTED_DATACol.isNull();
    6264           0 :       Bool DATAColIsOK = !DATACol.isNull();
    6265           0 :       Bool FLOAT_DATAColIsOK = !FLOAT_DATACol.isNull();
    6266           0 :       Bool LAG_DATAColIsOK = !LAG_DATACol.isNull();
    6267           0 :       Bool MODEL_DATAColIsOK = !MODEL_DATACol.isNull();
    6268           0 :       Bool SIGMA_SPECTRUMColIsOK = !SIGMA_SPECTRUMCol.isNull();
    6269           0 :       Bool WEIGHT_SPECTRUMColIsOK = !WEIGHT_SPECTRUMCol.isNull(); // rechecked further below
    6270           0 :       Bool FLAGColIsOK = !FLAGCol.isNull();
    6271           0 :       Bool FLAG_CATEGORYColIsOK = false; // to be set to the correct value further below
    6272             :       
    6273             :       // initialize arrays to store combined column data
    6274           0 :       if(CORRECTED_DATAColIsOK){
    6275           0 :         newCorrectedData.resize(newShape);
    6276             :       }
    6277           0 :       if(DATAColIsOK){
    6278           0 :         newData.resize(newShape);
    6279             :       }
    6280           0 :       if(FLOAT_DATAColIsOK){
    6281           0 :         newFloatData.resize(newShape);
    6282             :       }
    6283           0 :       if(LAG_DATAColIsOK){
    6284           0 :         newLagData.resize(newShape); 
    6285             :       }
    6286           0 :       if(MODEL_DATAColIsOK){
    6287           0 :         newModelData.resize(newShape);
    6288             :       }
    6289           0 :       if(SIGMA_SPECTRUMColIsOK){
    6290           0 :         newSigmaSpectrum.resize(newShape);
    6291             :       }
    6292           0 :       if(WEIGHT_SPECTRUMColIsOK){
    6293           0 :         if(oldWEIGHT_SPECTRUMCol.isDefined(firstAffRow)){ // required column but may be empty
    6294           0 :           newWeightSpectrum.resize(newShape);
    6295             :         }
    6296             :         else{
    6297           0 :           WEIGHT_SPECTRUMColIsOK = false;
    6298             :         }
    6299             :       }
    6300           0 :       if(FLAGColIsOK){ // required but one never knows (there may be bugs elsewhere)
    6301           0 :         newFlag.resize(newShape);
    6302             :       }
    6303           0 :       IPosition flagCatShape;
    6304           0 :       uInt nCat  = 0;
    6305           0 :       if(!FLAG_CATEGORYCol.isNull()){ 
    6306           0 :         if(oldFLAG_CATEGORYCol.isDefined(firstAffRow)){ // required column but may be empty
    6307           0 :           FLAG_CATEGORYColIsOK = true;
    6308           0 :           flagCatShape = oldFLAG_CATEGORYCol.shape(firstAffRow);
    6309           0 :           nCat = flagCatShape(2); // the dimension of the third axis ==
    6310             :           // number of categories
    6311           0 :           newFlagCategory.resize(IPosition(3, nCorrelations, newNUM_CHAN, nCat));
    6312             :         } 
    6313             :       }
    6314             :       
    6315             :       /////////////////////////////////////////
    6316             :  
    6317             :       // Loop over main table rows
    6318           0 :       uInt mainTabRowI = 0;
    6319           0 :       uInt mainTabRow = sortedI(mainTabRowI);
    6320           0 :       uInt newMainTabRow = 0;
    6321           0 :       uInt nIncompleteCoverage = 0; // number of rows with incomplete SPW coverage
    6322             : 
    6323           0 :       Int prevNewMainTabRow = -1; // needed to reduce redundant warning output
    6324           0 :       vector<Int> prevFailedAvChans;
    6325           0 :       vector<Int> prevFailedAvCorrs;
    6326             : 
    6327             :       // prepare progress meter
    6328           0 :       Float progress = 0.2;
    6329           0 :       Float progressStep = 0.2;
    6330           0 :       if(nMainTabRows>100000){
    6331           0 :         progress = 0.1;
    6332           0 :         progressStep = 0.1;
    6333             :       }
    6334             : 
    6335           0 :       while(mainTabRowI<nMainTabRows &&
    6336           0 :             (mainTabRow = sortedI(mainTabRowI)) < nMainTabRows
    6337             :             ){
    6338             :         
    6339             :         // should row be combined with others, i.e. has SPW changed?
    6340             :         // no -> just renumber DD ID (because of shrunk DD ID table)
    6341             :         
    6342             :         // yes-> find rows from the spws tobe combined with same timestamp, antennas and field
    6343             :         //       merge these rows
    6344             :         //       write merged row over first one, correcting DD ID at the same time
    6345             :         //       set TIME to 0 in other merged rows
    6346             :         //       reduce nMainTabRows accordingly
    6347             :         
    6348             :         // continue
    6349             :         //
    6350             :         // when finished, delete all rows with TIME = 0
    6351             :         
    6352           0 :         Double theTime = timeCol(mainTabRow);
    6353             :         
    6354             :         // row was already combined with a previous row?
    6355           0 :         if(theTime == 0){
    6356             :           //      cout << "skipping row with zero time " << mainTabRow << endl;
    6357           0 :           mainTabRowI++;
    6358           0 :           continue;
    6359             :         }
    6360             :         
    6361           0 :         Int theDataDescId = DDIdCol(mainTabRow);
    6362             :         
    6363             :         // row affected by the spw combination? (uses the old DD numbering)
    6364           0 :         if(DDtoSPWIndex.find(theDataDescId) != DDtoSPWIndex.end( )){
    6365             :           // find matching affected rows with same time stamp, antennas and field
    6366           0 :           Int theAntenna1 = antenna1Col(mainTabRow);
    6367           0 :           Int theAntenna2 = antenna2Col(mainTabRow);
    6368           0 :           Int theField = fieldCol(mainTabRow);
    6369           0 :           Double theInterval = intervalCol(mainTabRow);
    6370           0 :           Double toleratedTimeDiff = theInterval/10.;
    6371           0 :           Double theExposure = exposureCol(mainTabRow);
    6372           0 :           vector<Int> matchingRows;
    6373           0 :           matchingRows.push_back(mainTabRow);
    6374           0 :           vector<Int> matchingRowSPWIds;
    6375           0 :           matchingRowSPWIds.push_back(DDtoSPWIndex.at(theDataDescId));
    6376           0 :           std::map<Int, Int> SPWtoRowIndex;
    6377           0 :           SPWtoRowIndex.insert(std::pair<Int, Int>(matchingRowSPWIds[0], mainTabRow));
    6378             : 
    6379             :           //      cout << "theRow = " << mainTabRow << ", time = " << theTime << " DDID " << theDataDescId << endl;
    6380             :           
    6381           0 :           uInt nextRowI = mainTabRowI+1;
    6382             :           uInt nextRow;
    6383             :           //      cout << "nextRow = " << nextRow << ", time diff  = " << timeCol(nextRow) - theTime << " DDID " << DDIdCol(nextRow) << endl;
    6384           0 :           while(nextRowI<nMainTabRows && 
    6385           0 :                 (nextRow = sortedI(nextRowI))<nMainTabRows && // assignment!
    6386           0 :                 (timeCol(nextRow) - theTime)< toleratedTimeDiff &&
    6387           0 :                 matchingRows.size() < nSpwsToCombine // there should be one matching row per SPW
    6388             :                 ){
    6389             : 
    6390           0 :         if( DDtoSPWIndex.find(DDIdCol(nextRow)) == DDtoSPWIndex.end( ) ||
    6391           0 :                antenna1Col(nextRow) != theAntenna1 ||
    6392           0 :                antenna2Col(nextRow) != theAntenna2 ||
    6393           0 :                fieldCol(nextRow) != theField ){ // not a matching row
    6394           0 :               nextRowI++;
    6395           0 :               continue;
    6396             :             }
    6397             :             // check that the intervals are the same
    6398           0 :             if(fabs(intervalCol(nextRow)-theInterval) > 1E-5){
    6399           0 :               os << LogIO::WARN << "Error: for time " <<  MVTime(theTime/C::day).string(MVTime::DMY,7) << ", baseline (" << theAntenna1 << ", "
    6400             :                  << theAntenna2 << "), field "<< theField << ", DataDescID " << DDIdCol(mainTabRow)
    6401             :                  << " found matching row with DataDescID " << DDIdCol(nextRow) << endl
    6402             :                  << " but the two rows have different intervals: " << theInterval
    6403             :                  << " vs. " << intervalCol(nextRow)
    6404           0 :                  << LogIO::POST;
    6405           0 :               if(!flagRowCol(nextRow)){
    6406           0 :                 os << LogIO::SEVERE << "You need to flag row " << nextRow << " to proceed." << LogIO::POST;
    6407           0 :                 return false;
    6408             :               }
    6409             :               else{
    6410           0 :                 os << LogIO::WARN << "Fortunately, row " << nextRow << " is flagged so this problem will be ignored." << LogIO::POST;
    6411             :               }         
    6412             :             }
    6413             :             // check that the exposures are the same
    6414           0 :             if(fabs(exposureCol(nextRow)-theExposure) > 1E-5){
    6415           0 :               os << LogIO::WARN << "Error: for time " <<  MVTime(theTime/C::day).string(MVTime::DMY,7) << ", baseline (" << theAntenna1 << ", "
    6416             :                  << theAntenna2 << "), field "<< theField << ", DataDescID " << DDIdCol(mainTabRow)
    6417             :                  << " found matching row with DataDescID " << DDIdCol(nextRow) << endl
    6418             :                  << " but the two rows have different exposures: " << theExposure
    6419             :                  << " vs. " << exposureCol(nextRow)
    6420           0 :                  << LogIO::POST;
    6421           0 :               if(!flagRowCol(nextRow)){
    6422           0 :                 os << LogIO::SEVERE << "You need to flag row " << nextRow << " to proceed." << LogIO::POST;
    6423           0 :                 return false;
    6424             :               }
    6425             :               else{
    6426           0 :                 os << LogIO::WARN << "Fortunately row " << nextRow << " is flagged so this problem will be ignored." << LogIO::POST;
    6427             :               }         
    6428             :             }
    6429             :             // found a matching row
    6430           0 :             Int theSPWId = DDtoSPWIndex.at(DDIdCol(nextRow));
    6431           0 :         if(SPWtoRowIndex.find(theSPWId) != SPWtoRowIndex.end( )){ // there should be a one-to-one relation: SPW <-> matching row
    6432           0 :               os << LogIO::WARN << "Error: for time " << MVTime(theTime/C::day).string(MVTime::DMY,7) << ", baseline (" << theAntenna1 << ","
    6433             :                  << theAntenna2 << "), field "<< theField << " found more than one row for SPW "
    6434           0 :                  << theSPWId << LogIO::POST;
    6435           0 :               if(!flagRowCol(nextRow)){
    6436           0 :                 os << LogIO::SEVERE << "You need to flag row " << nextRow << " to proceed." << LogIO::POST;
    6437           0 :                 return false;
    6438             :               }
    6439             :               else{
    6440           0 :                 os << LogIO::WARN << "Fortunately, row " << nextRow << " is flagged so this problem will be ignored." << LogIO::POST;
    6441             :               }         
    6442             :             }
    6443             :             else{ // this SPW not yet covered, memorize SPWId, row number, and relation
    6444           0 :               matchingRowSPWIds.push_back(theSPWId);
    6445           0 :               matchingRows.push_back(nextRow);
    6446           0 :               SPWtoRowIndex.insert(std::pair<Int, Int>(theSPWId, nextRow));
    6447             :               // cout << "matching nextRow = " << nextRow << ", time = " << timeCol(nextRow) << " DDID " << DDIdCol(nextRow) << endl;
    6448             :             }
    6449           0 :             nextRowI++;
    6450             :           } // end while nextRowI ...
    6451             :           
    6452             :           // now we have a set of matching rows
    6453           0 :           uInt nMatchingRows = matchingRows.size();
    6454             :           
    6455           0 :           if(nMatchingRows < nSpwsToCombine){
    6456           0 :             if(nIncompleteCoverage==0){
    6457             :               os << LogIO::WARN << "Incomplete coverage of combined SPW starting at timestamp " 
    6458           0 :                  <<  MVTime(timeCol(mainTabRow)/C::day).string(MVTime::DMY,7)
    6459             :                  << ", baseline ( " << theAntenna1 << ", " << theAntenna2 << " )" << endl
    6460           0 :                  << "In this and further affected rows, the data arrays will be padded with zeros and corresponding channels flagged." <<  LogIO::POST;
    6461             :             }
    6462           0 :             nIncompleteCoverage++;
    6463             :           }
    6464             : 
    6465             :           // reset arrays and prepare input data matrices
    6466             :           
    6467           0 :           if(CORRECTED_DATAColIsOK){
    6468           0 :             newCorrectedData.set(0);
    6469             :           }
    6470           0 :           if(DATAColIsOK){
    6471           0 :             newData.set(0);
    6472             :           }
    6473           0 :           if(FLOAT_DATAColIsOK){
    6474           0 :             newFloatData.set(0);
    6475             :           }
    6476           0 :           if(LAG_DATAColIsOK){
    6477           0 :             newLagData.set(0);
    6478             :           }
    6479           0 :           if(MODEL_DATAColIsOK){
    6480           0 :             newModelData.set(0);
    6481             :           }
    6482           0 :           if(SIGMA_SPECTRUMColIsOK){
    6483           0 :             newSigmaSpectrum.set(0);
    6484             :           }
    6485           0 :           if(WEIGHT_SPECTRUMColIsOK){
    6486           0 :             newWeightSpectrum.set(0);
    6487             :           }
    6488           0 :           if(FLAGColIsOK){
    6489           0 :             newFlag.set(0);
    6490             :           }
    6491           0 :           if(FLAG_CATEGORYColIsOK){
    6492           0 :             newFlagCategory.set(0);
    6493             :           }
    6494             : 
    6495           0 :           vector<Matrix<Complex> > newCorrectedDataI(nSpwsToCombine); 
    6496           0 :           vector<Matrix<Complex> > newDataI(nSpwsToCombine);
    6497           0 :           vector<Matrix<Float> > newFloatDataI(nSpwsToCombine);
    6498           0 :           vector<Matrix<Complex> > newLagDataI(nSpwsToCombine);
    6499           0 :           vector<Matrix<Complex> > newModelDataI(nSpwsToCombine);
    6500           0 :           vector<Matrix<Float> > newSigmaSpectrumI(nSpwsToCombine);
    6501           0 :           vector<Matrix<Float> > newWeightSpectrumI(nSpwsToCombine);
    6502           0 :           vector<Matrix<Bool> > newFlagI(nSpwsToCombine);
    6503           0 :           vector<Array<Bool> > newFlagCategoryI(nSpwsToCombine); // has three dimensions
    6504           0 :           vector<Bool> newFlagRowI(nSpwsToCombine);
    6505             : 
    6506           0 :           for(uInt i=0; i<nSpwsToCombine; i++){
    6507           0 :             Int theRowSPWId = spwsToCombine[i];
    6508           0 :         if(SPWtoRowIndex.find(theRowSPWId) != SPWtoRowIndex.end( )){ // there actually is a matching row for this SPW
    6509           0 :               Int theRow = SPWtoRowIndex[theRowSPWId];
    6510           0 :               if(CORRECTED_DATAColIsOK){
    6511           0 :                 newCorrectedDataI[theRowSPWId].reference(oldCORRECTED_DATACol(theRow));
    6512             :               }
    6513           0 :               if(DATAColIsOK){
    6514           0 :                 newDataI[theRowSPWId].reference(oldDATACol(theRow));
    6515             :               }
    6516           0 :               if(FLOAT_DATAColIsOK){
    6517           0 :                 newFloatDataI[theRowSPWId].reference(oldFLOAT_DATACol(theRow));
    6518             :               }
    6519           0 :               if(LAG_DATAColIsOK){
    6520           0 :                 newLagDataI[theRowSPWId].reference(oldLAG_DATACol(theRow));
    6521             :               }
    6522           0 :               if(MODEL_DATAColIsOK){
    6523           0 :                 newModelDataI[theRowSPWId].reference(oldMODEL_DATACol(theRow));
    6524             :               }
    6525           0 :               if(SIGMA_SPECTRUMColIsOK){
    6526           0 :                 newSigmaSpectrumI[theRowSPWId].reference(oldSIGMA_SPECTRUMCol(theRow));
    6527             :               }
    6528           0 :               if(WEIGHT_SPECTRUMColIsOK){
    6529           0 :                 newWeightSpectrumI[theRowSPWId].reference(oldWEIGHT_SPECTRUMCol(theRow));
    6530             :               }
    6531           0 :               if(FLAGColIsOK){
    6532           0 :                 newFlagI[theRowSPWId].reference(oldFLAGCol(theRow));
    6533             :               }
    6534           0 :               if(FLAG_CATEGORYColIsOK){
    6535           0 :                 newFlagCategoryI[theRowSPWId].reference(oldFLAG_CATEGORYCol(theRow));
    6536             :               }
    6537           0 :               newFlagRowI[theRowSPWId] = flagRowCol(theRow);
    6538             :             } // end if
    6539             :           } // end for i
    6540             :           
    6541             :           // merge data columns from all rows found using the averaging info from above
    6542             :           // averageN[], averageWhichSPW[], averageWhichChan[], averageChanFrac[]
    6543             : 
    6544           0 :           Bool failedAv = false;          
    6545           0 :           vector<Int> failedAvChans;
    6546           0 :           vector<Int> failedAvCorrs;
    6547             : 
    6548             :           // loop over new channels
    6549           0 :           for(uInt i=0; i<newNUM_CHAN; i++){
    6550             :             // initialise special treatment for Bool columns
    6551           0 :             if(FLAGColIsOK){
    6552           0 :               for(uInt k=0; k<nCorrelations; k++){ 
    6553           0 :                 newFlag(k,i) =  true; // overwritten with false below if there is a SPW where this channel is not flagged for this correlator
    6554             :               }
    6555             :             }
    6556           0 :             if(FLAG_CATEGORYColIsOK){
    6557           0 :               for(uInt k=0; k<nCorrelations; k++){ 
    6558           0 :                 for(uInt m=0; m<nCat; m++){ 
    6559           0 :                   newFlagCategory(IPosition(3,k,i,m)) = false;
    6560             :                 }
    6561             :               }
    6562             :             }
    6563             : 
    6564           0 :             Bool haveCoverage = false;
    6565           0 :             Vector<Double> modNorm(nCorrelations, 0.); // normalization for the averaging of the contributions from the SPWs
    6566           0 :             vector<vector<Double> > modAverageChanFrac(averageN[i], vector<Double>(nCorrelations, 0.));
    6567           0 :             for(uInt k=0; k<nCorrelations; k++){
    6568           0 :               Vector<Int> spwCount(nSpwsToCombine, 0);
    6569           0 :               for(Int j=0; j<averageN[i]; j++){
    6570           0 :         if(SPWtoRowIndex.find(averageWhichSPW[i][j]) != SPWtoRowIndex.end( )){
    6571           0 :                   if(!newFlagI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] )){
    6572           0 :                     haveCoverage = true;
    6573           0 :                     if(FLAGColIsOK){
    6574           0 :                       newFlag(k,i) = false; // there is valid data for this channel and correlation => don't flag in output
    6575             :                     }
    6576           0 :                     modAverageChanFrac[j][k] = averageChanFrac[i][j];
    6577           0 :                     modNorm(k) += averageChanFrac[i][j];
    6578           0 :                     if(modAverageChanFrac[j][k]!=1.){
    6579           0 :                       ++spwCount(averageWhichSPW[i][j]); // count the contributions with non-unity overlap fraction for each spw
    6580             :                     }
    6581             :                   }
    6582             :                 }
    6583             :               }
    6584           0 :               if(modNorm(k)>0.){ // there are contributions
    6585           0 :                 for(Int j=0; j<averageN[i]; j++){
    6586             :                   // Second iteration: eliminate contributions from spws with odd numbers of contribs with non-unity overlap fraction
    6587             :                   // which could influence the averaging asymmetrically
    6588           0 :           if(SPWtoRowIndex.find(averageWhichSPW[i][j]) != SPWtoRowIndex.end( )
    6589           0 :                      && spwCount(averageWhichSPW[i][j])<averageN[i] // there are also channels with full overlap 
    6590           0 :                      && spwCount(averageWhichSPW[i][j])%2!=0     // the number of channels with non-unity overlap is odd
    6591           0 :                      && modAverageChanFrac[j][k]!=1.){           // this contributor j has non-unity overlap
    6592             :                     //cout << "spw count " << averageWhichSPW[i][j] << " " << spwCount(averageWhichSPW[i][j]) << endl;
    6593             :                     //cout << "not using i j k spw frac " << i << " " << j << " " << k << " " 
    6594             :                     //     << averageWhichSPW[i][j] << " " << modAverageChanFrac[j][k] << endl;
    6595           0 :                     modNorm(k) -= modAverageChanFrac[j][k];
    6596           0 :                     modAverageChanFrac[j][k] = 0.;
    6597           0 :                     if(modNorm(k)<=0.){ // should only occur in rare cases
    6598           0 :                       if(FLAGColIsOK && !newFlag(k,i)){
    6599           0 :                         failedAv = true;
    6600           0 :                         newFlag(k,i) = true;
    6601           0 :                         if(prevNewMainTabRow<0){
    6602           0 :                           prevNewMainTabRow = newMainTabRow;
    6603             :                         }
    6604           0 :                         failedAvChans.push_back(i);
    6605           0 :                         failedAvCorrs.push_back(k);
    6606             :                       }
    6607           0 :                       modNorm(k) += averageChanFrac[i][j];
    6608           0 :                       modAverageChanFrac[j][k] = averageChanFrac[i][j];
    6609           0 :                       break;
    6610             :                     }
    6611             :                   }             
    6612             :                 }
    6613             :               }
    6614           0 :             } // end loop over correlations
    6615             : 
    6616           0 :             if(haveCoverage){ // there is unflagged data for this channel
    6617             :               // loop over SPWs
    6618           0 :               for(Int j=0; j<averageN[i]; j++){
    6619             :                 // new channel value i 
    6620             :                 //   = SUM{j=0 to averageN[i]}( channelValue(SPW = averageWhichSPW[i][j], CHANNEL = averageWhichChan[i][j]) * averageChanFrac[i][j])
    6621           0 :         if(SPWtoRowIndex.find(averageWhichSPW[i][j]) != SPWtoRowIndex.end( )){
    6622             : 
    6623           0 :                   Double weight = 0.;
    6624             : 
    6625             :                   // loop over first dimension (number of correlations)
    6626           0 :                   for(uInt k=0; k<nCorrelations; k++){
    6627           0 :                     if(!newFlagI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] )
    6628           0 :                        && modNorm(k)>0.){ // this channel is not flagged for the given SPW and correlator and there are contributions
    6629             : 
    6630             :                       // renormalize for the case of missing SPW coverage
    6631           0 :                       weight = modAverageChanFrac[j][k] / modNorm(k);
    6632             : 
    6633           0 :                       if(CORRECTED_DATAColIsOK){
    6634           0 :                         newCorrectedData(k,i) += newCorrectedDataI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) * weight;                      
    6635             :                       }
    6636           0 :                       if(DATAColIsOK){
    6637           0 :                         newData(k,i) += newDataI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) * weight;
    6638             : //                      if (!wasprinted[i][j]){
    6639             : //                        cout << "row " << SPWtoRowIndex(averageWhichSPW[i][j]) << "averageWhichSPW[i][j] " 
    6640             : //                             << averageWhichSPW[i][j] << "  averageWhichChan[i][j] " << averageWhichChan[i][j]
    6641             : //                             << " i, j, k " << i << ", " << j << ", " << k << " modAverageChanFrac[j][k] " << modAverageChanFrac[j][k] 
    6642             : //                             << " modNorm(k) " << modNorm(k) << " newDataI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) "
    6643             : //                             << newDataI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) 
    6644             : //                             << " newData(k,i) " << newData(k,i) 
    6645             : //                             << " weight " << weight << endl;
    6646             : //                        wasprinted[i][j] = true;
    6647             : //                      } 
    6648             :                       }
    6649           0 :                       if(FLOAT_DATAColIsOK){
    6650           0 :                         newFloatData(k,i) += newFloatDataI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) * weight;
    6651             :                       }
    6652           0 :                       if(LAG_DATAColIsOK){
    6653           0 :                         newLagData(k,i) += newLagDataI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) * weight;
    6654             :                       }
    6655           0 :                       if(MODEL_DATAColIsOK){
    6656           0 :                         newModelData(k,i) += newModelDataI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) * weight;
    6657             :                       }
    6658           0 :                       if(SIGMA_SPECTRUMColIsOK){
    6659           0 :                         newSigmaSpectrum(k,i) += newSigmaSpectrumI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) * weight;
    6660             :                       }
    6661           0 :                       if(WEIGHT_SPECTRUMColIsOK){
    6662           0 :                         newWeightSpectrum(k,i) += newWeightSpectrumI[ averageWhichSPW[i][j] ]( k, averageWhichChan[i][j] ) * weight;
    6663             :                       }
    6664             : 
    6665             :                     } // end if flagged
    6666             :                   } // end for k = 0
    6667             :                 
    6668             :                   // special treatment for flag cat
    6669           0 :                   if(FLAG_CATEGORYColIsOK){
    6670           0 :                     for(uInt k=0; k<nCorrelations; k++){ // logical OR of all input spws
    6671           0 :                       for(uInt m=0; m<nCat; m++){ 
    6672           0 :                         newFlagCategory(IPosition(3,k,i,m)) = 
    6673           0 :                           newFlagCategory(IPosition(3,k,i,m)) || newFlagCategoryI[ averageWhichSPW[i][j] ](IPosition(3,k,averageWhichChan[i][j],m));
    6674             :                       }
    6675             :                     }
    6676             :                   }
    6677             : 
    6678             :                 } // end if there is a row for this SPW       
    6679             :               } // end for j=0, loop over SPWs
    6680             :             } // if there is coverage for this channel 
    6681           0 :           } // end for i=0, loop over new channels
    6682             : 
    6683             :           // give warnings about channels with averaging problems
    6684           0 :           if(prevNewMainTabRow>=0){ // may need to issue warnings
    6685           0 :             if(!failedAv 
    6686           0 :                || mainTabRowI==nMainTabRows-1
    6687           0 :                || (prevFailedAvChans.size()>0 && !((failedAvChans==prevFailedAvChans) && (failedAvCorrs==prevFailedAvCorrs)))
    6688             :                ){
    6689           0 :               uInt lastRow = newMainTabRow-1;
    6690           0 :               if(mainTabRowI==nMainTabRows-1){
    6691           0 :                 lastRow = newMainTabRow;
    6692             :               }
    6693             :               os << LogIO::WARN << "Averaging failed for the following channel/correllation pairs from output row " 
    6694           0 :                  << prevNewMainTabRow << " up to " << lastRow << ". Corresponding visibilities will be flagged: " << endl;
    6695           0 :               for(uInt iii=0; iii<prevFailedAvChans.size(); iii++){
    6696           0 :                 os << "(" << prevFailedAvChans[iii] << ", " << prevFailedAvCorrs[iii] << ") ";
    6697             :               }
    6698           0 :               os << LogIO::POST;
    6699           0 :               prevNewMainTabRow = -1;
    6700             :             }
    6701           0 :             prevFailedAvChans = failedAvChans;
    6702           0 :             prevFailedAvCorrs = failedAvCorrs;
    6703             :           }     
    6704             :             
    6705             :           // calculate FLAG_ROW as logical OR of all input rows
    6706           0 :           newFlagRow = newFlagRowI[0];
    6707           0 :           for(uInt i=1; i<nMatchingRows; i++){
    6708           0 :             newFlagRow = newFlagRow || newFlagRowI[i];
    6709             :           }
    6710             : 
    6711             :           // write data into the new main table
    6712           0 :           newMain.addRow(1,false);
    6713             : 
    6714             :           //      cout << "writing new row " << newMainTabRow << endl;
    6715             :           
    6716           0 :           if(CORRECTED_DATAColIsOK){
    6717           0 :             CORRECTED_DATACol.put(newMainTabRow, newCorrectedData);
    6718             :           }
    6719           0 :           if(DATAColIsOK){
    6720             : //          cout << "old " << oldDATACol(mainTabRow).shape() << endl;
    6721             : //          cout << "new " << newData.shape() << endl;
    6722           0 :             DATACol.put(newMainTabRow, newData);
    6723             :           }
    6724           0 :           if(FLOAT_DATAColIsOK){
    6725           0 :             FLOAT_DATACol.put(newMainTabRow, newFloatData);
    6726             :           }
    6727           0 :           if(LAG_DATAColIsOK){
    6728           0 :             LAG_DATACol.put(newMainTabRow, newLagData);
    6729             :           }
    6730           0 :           if(MODEL_DATAColIsOK){
    6731           0 :             MODEL_DATACol.put(newMainTabRow, newModelData);
    6732             :           }
    6733           0 :           if(SIGMA_SPECTRUMColIsOK){
    6734           0 :             SIGMA_SPECTRUMCol.put(newMainTabRow, newSigmaSpectrum);
    6735             :           }
    6736           0 :           if(WEIGHT_SPECTRUMColIsOK){
    6737           0 :             WEIGHT_SPECTRUMCol.put(newMainTabRow, newWeightSpectrum);
    6738             :           }
    6739           0 :           if(FLAGColIsOK){
    6740           0 :             FLAGCol.put(newMainTabRow, newFlag);
    6741             :           }
    6742           0 :           if(FLAG_CATEGORYColIsOK){
    6743           0 :             FLAG_CATEGORYCol.put(newMainTabRow, newFlagCategory);
    6744             :           }
    6745             : 
    6746           0 :           mainCols.flagRow().put(newMainTabRow, newFlagRow);
    6747             :           
    6748           0 :       if(tempDDIndex.find(theDataDescId) != tempDDIndex.end( )){
    6749             :             // do DD ID renumbering (due to shrunk DD table and spw combination )
    6750           0 :             mainCols.dataDescId().put(newMainTabRow, tempDDIndex.at(theDataDescId)); 
    6751             :           }
    6752             :           else{
    6753           0 :             mainCols.dataDescId().put(newMainTabRow,  DDIdCol(mainTabRow)); 
    6754             :           }       
    6755             :           // copy the rest of the row contents from mainTabRow
    6756           0 :           mainCols.sigma().put(newMainTabRow, SIGMACol(mainTabRow));
    6757           0 :           mainCols.weight().put(newMainTabRow, oldMainCols.weight()(mainTabRow));
    6758             : 
    6759           0 :           mainCols.fieldId().put(newMainTabRow, fieldCol(mainTabRow));
    6760           0 :           mainCols.antenna1().put(newMainTabRow, antenna1Col(mainTabRow));
    6761           0 :           mainCols.antenna2().put(newMainTabRow, antenna2Col(mainTabRow));
    6762           0 :           mainCols.time().put(newMainTabRow, timeCol(mainTabRow));
    6763           0 :           mainCols.exposure().put(newMainTabRow, exposureCol(mainTabRow));
    6764           0 :           mainCols.interval().put(newMainTabRow, intervalCol(mainTabRow));
    6765             : 
    6766           0 :           mainCols.uvw().put(newMainTabRow, oldMainCols.uvw()(mainTabRow));
    6767           0 :           mainCols.arrayId().put(newMainTabRow, oldMainCols.arrayId()(mainTabRow));
    6768           0 :           mainCols.feed1().put(newMainTabRow, oldMainCols.feed1()(mainTabRow));
    6769           0 :           mainCols.feed2().put(newMainTabRow, oldMainCols.feed2()(mainTabRow));
    6770           0 :           mainCols.observationId().put(newMainTabRow, oldMainCols.observationId()(mainTabRow));
    6771           0 :           mainCols.processorId().put(newMainTabRow, oldMainCols.processorId()(mainTabRow));
    6772           0 :           mainCols.scanNumber().put(newMainTabRow, oldMainCols.scanNumber()(mainTabRow));
    6773           0 :           mainCols.stateId().put(newMainTabRow, oldMainCols.stateId()(mainTabRow));
    6774           0 :           mainCols.timeCentroid().put(newMainTabRow, oldMainCols.timeCentroid()(mainTabRow));
    6775             : 
    6776             :           //      cout << "Wrote new row " << newMainTabRow << endl;
    6777           0 :           newMainTabRow++;        
    6778             :           // mark other found rows to be ignored, i.e. set their time to zero
    6779           0 :           for(uInt i=1; i<nMatchingRows; i++){ // don't mark the first
    6780             :             //      cout << "setting time to zero in row " << matchingRows[i] << endl;
    6781           0 :             timeCol.put(matchingRows[i], 0);
    6782             :           }
    6783             :           
    6784           0 :         } // end if row is affected
    6785             :         
    6786           0 :         mainTabRowI++;
    6787           0 :         if(mainTabRowI>nMainTabRows*progress){
    6788           0 :           cout << "combineSpws progress: " << progress*100 << "% processed ... " << endl;
    6789           0 :           progress += progressStep;
    6790             :         }
    6791             :       
    6792             :       } // end loop over main table rows
    6793           0 :       cout << "combineSpws progress: 100% processed." << endl;
    6794             :       ////////////////////////////////////////////
    6795             : 
    6796           0 :       if(verbose){
    6797             :         os << LogIO::NORMAL << "Processed " << mainTabRowI << " original rows, wrote "
    6798           0 :            << newMainTabRow << " new ones." << LogIO::POST;
    6799             : 
    6800           0 :         if(nIncompleteCoverage>0){
    6801             :           os << LogIO::WARN << "Incomplete coverage of combined SPW in " << nIncompleteCoverage
    6802           0 :              << " of " <<  newMainTabRow << " output rows." <<  LogIO::POST;
    6803             :         }
    6804             :       }
    6805             : 
    6806           0 :     } // end scope for MS related objects
    6807             :  
    6808           0 :     String oldName(ms_p.tableName());
    6809             : 
    6810             :     // detach old MS
    6811           0 :     ms_p = MeasurementSet();
    6812           0 :     mssel_p = MeasurementSet();
    6813             : 
    6814             :     // rename the result MS overwriting the old MS
    6815             :     {
    6816           0 :       Table tab(tempNewName, Table::Update);
    6817           0 :       tab.rename(oldName, Table::New);
    6818           0 :     }
    6819             : 
    6820             :     // attach new MS
    6821           0 :     ms_p = MeasurementSet(oldName, Table::Update);
    6822           0 :     mssel_p = ms_p;
    6823             : 
    6824           0 :     if(verbose){
    6825           0 :       os << LogIO::NORMAL << "Spectral window combination complete." << LogIO::POST;
    6826             :     }
    6827             : 
    6828           0 :     return true;
    6829             : 
    6830           0 :   }
    6831             : 
    6832           0 :   const Vector<MS::PredefinedColumns>& SubMS::parseColumnNames(String col)
    6833             :   {
    6834             :     // Memoize both for efficiency and so that the info message at the bottom
    6835             :     // isn't unnecessarily repeated.
    6836           0 :     static String my_colNameStr = "";
    6837           0 :     static Vector<MS::PredefinedColumns> my_colNameVect;
    6838             : 
    6839           0 :     col.upcase();
    6840           0 :     if(col == my_colNameStr && col != ""){
    6841           0 :       return my_colNameVect;
    6842             :     }    
    6843           0 :     else if(col == "None"){
    6844           0 :       my_colNameStr = "";
    6845           0 :       my_colNameVect.resize(0);
    6846           0 :       return my_colNameVect;
    6847             :     }
    6848             :  
    6849           0 :     LogIO os(LogOrigin("SubMS", "parseColumnNames()"));
    6850             :     
    6851             :     uInt nNames;
    6852             :     
    6853           0 :     if(col.contains("ALL")){
    6854           0 :       nNames = 3;
    6855           0 :       my_colNameVect.resize(nNames);
    6856           0 :       my_colNameVect[0] = MS::DATA;
    6857           0 :       my_colNameVect[1] = MS::MODEL_DATA;
    6858           0 :       my_colNameVect[2] = MS::CORRECTED_DATA;
    6859             :     }
    6860             :     else{
    6861           0 :       nNames = dataColStrToEnums(col, my_colNameVect);
    6862             :     }
    6863             : 
    6864             :     // Whether or not the MS has the columns is checked by verifyColumns().
    6865             :     // Unfortunately it cannot be done here because this is a static method.
    6866             : 
    6867           0 :     if(col != "NONE"){  // "NONE" is used by ~SubMS().
    6868             :       os << LogIO::NORMAL
    6869           0 :          << "Using ";     // Don't say "Splitting"; this is used elsewhere.
    6870           0 :       for(uInt i = 0; i < nNames; ++i)
    6871           0 :         os << MS::columnName(my_colNameVect[i]) << " ";
    6872           0 :       os << " column" << (my_colNameVect.nelements() > 1 ? "s." : ".")
    6873           0 :          << LogIO::POST;
    6874             :     }
    6875             :     
    6876           0 :     my_colNameStr = col;
    6877           0 :     return my_colNameVect;
    6878           0 :   }
    6879             : 
    6880             : // This version uses the MeasurementSet to check what columns are present,
    6881             : // i.e. it makes col=="all" smarter, and it is not necessary to call
    6882             : // verifyColumns() after calling this.  Unlike the other version, it knows
    6883             : // about FLOAT_DATA and LAG_DATA.  It throws an exception if a
    6884             : // _specifically_ requested column is absent.
    6885           0 : const Vector<MS::PredefinedColumns>& SubMS::parseColumnNames(String col,
    6886             :                                                   const MeasurementSet& msref)
    6887             : {
    6888             :   // Memorize both for efficiency and so that the info message at the bottom
    6889             :   // isn't unnecessarily repeated.
    6890           0 :   static String my_colNameStr = "";
    6891           0 :   static Vector<MS::PredefinedColumns> my_colNameVect;
    6892             : 
    6893           0 :   Vector<MS::PredefinedColumns> wanted;       // Data columns to pick up if present.
    6894             :   
    6895           0 :   col.upcase();
    6896             : // This version of parseColumnNames does not reuse results of previous calls
    6897             : // but always checks the given columns because it cannot be certain that msref
    6898             : // refers to the same MS with every call.
    6899             : 
    6900           0 :   if(col == "None"){
    6901           0 :     my_colNameStr = "";
    6902           0 :     my_colNameVect.resize(0);
    6903           0 :     return my_colNameVect;
    6904             :   }
    6905             :  
    6906           0 :   LogIO os(LogOrigin("SubMS", "parseColumnNames()"));
    6907             :     
    6908             :   // Are we choosy?
    6909           0 :   const Bool doAny = col.contains("ALL") || col.contains("ANY");
    6910             :   
    6911             :   uInt nPoss;
    6912           0 :   if(doAny){
    6913           0 :     nPoss = 5;
    6914           0 :     wanted.resize(nPoss);
    6915           0 :     wanted[0] = MS::DATA;
    6916           0 :     wanted[1] = MS::MODEL_DATA;
    6917           0 :     wanted[2] = MS::CORRECTED_DATA;
    6918           0 :     wanted[3] = MS::FLOAT_DATA;
    6919           0 :     wanted[4] = MS::LAG_DATA;
    6920             :   }
    6921             :   else{ // split name string into individual names
    6922           0 :     nPoss = dataColStrToEnums(col, wanted);
    6923             :   } 
    6924             : 
    6925           0 :   uInt nFound = 0;
    6926           0 :   my_colNameVect.resize(0);
    6927           0 :   for(uInt i = 0; i < nPoss; ++i){
    6928           0 :     if(msref.tableDesc().isColumn(MS::columnName(wanted[i]))){
    6929           0 :       ++nFound;
    6930           0 :       my_colNameVect.resize(nFound, true);
    6931           0 :       my_colNameVect[nFound - 1] = wanted[i];
    6932             :     }
    6933           0 :     else if(!doAny){
    6934           0 :       ostringstream ostr;
    6935           0 :       ostr << "Desired column (" << MS::columnName(wanted[i])
    6936           0 :            << ") not found in the input MS (" << msref.tableName() << ").";
    6937           0 :       throw(AipsError(ostr.str()));
    6938           0 :     }
    6939             :   }
    6940           0 :   if(nFound == 0)
    6941           0 :     throw(AipsError("Did not find and select any data columns."));
    6942             :   
    6943             :   os << LogIO::NORMAL
    6944           0 :      << "Using ";     // Don't say "Splitting"; this is used elsewhere.
    6945           0 :   for(uInt i = 0; i < nFound; ++i)
    6946           0 :     os << MS::columnName(my_colNameVect[i]) << " ";
    6947           0 :   os << "column" << (nFound > 1 ? "s." : ".") << LogIO::POST;
    6948             :     
    6949           0 :   my_colNameStr = col;
    6950           0 :   return my_colNameVect;
    6951           0 : }
    6952             : 
    6953           0 : uInt SubMS::dataColStrToEnums(const String& col, Vector<MS::PredefinedColumns>& colvec)
    6954             : {
    6955           0 :   LogIO os(LogOrigin("SubMS", "dataColStrToEnums()"));
    6956           0 :   String tmpNames(col);
    6957           0 :   Vector<String> tokens;
    6958           0 :   tmpNames.upcase();
    6959             :     
    6960             :   // split name string into individual names
    6961             :   char * pch;
    6962           0 :   Int i = 0;
    6963           0 :   pch = strtok((char*)tmpNames.c_str(), " ,");
    6964           0 :   while (pch != NULL){
    6965           0 :     tokens.resize(i + 1, true);
    6966           0 :     tokens[i] = String(pch);
    6967           0 :     ++i;
    6968           0 :     pch = strtok(NULL, " ,");
    6969             :   }
    6970             : 
    6971           0 :   uInt nNames = tokens.nelements();
    6972             : 
    6973           0 :   uInt nFound = 0;
    6974           0 :   for(uInt i = 0; i < nNames; ++i){
    6975           0 :     colvec.resize(nFound + 1, true);
    6976           0 :     colvec[nFound] = MS::UNDEFINED_COLUMN;
    6977             :             
    6978           0 :     if (tokens[i] == "OBSERVED" || 
    6979           0 :         tokens[i] == "DATA" || 
    6980           0 :         tokens[i] == MS::columnName(MS::DATA)){
    6981           0 :       colvec[nFound++] = MS::DATA;
    6982             :     }
    6983           0 :     else if(tokens[i] == "FLOAT" || 
    6984           0 :             tokens[i] == "FLOAT_DATA" || 
    6985           0 :             tokens[i] == MS::columnName(MS::FLOAT_DATA)){
    6986           0 :       colvec[nFound++] = MS::FLOAT_DATA;
    6987             :     } 
    6988           0 :     else if(tokens[i] == "LAG" || 
    6989           0 :             tokens[i] == "LAG_DATA" || 
    6990           0 :             tokens[i] == MS::columnName(MS::LAG_DATA)){
    6991           0 :       colvec[nFound++] = MS::LAG_DATA;
    6992             :     } 
    6993           0 :     else if(tokens[i] == "MODEL" || 
    6994           0 :             tokens[i] == "MODEL_DATA" || 
    6995           0 :             tokens[i] == MS::columnName(MS::MODEL_DATA)){
    6996           0 :       colvec[nFound++] = MS::MODEL_DATA;
    6997             :     } 
    6998           0 :     else if(tokens[i] == "CORRECTED" || 
    6999           0 :             tokens[i] == "CORRECTED_DATA" || 
    7000           0 :             tokens[i] == MS::columnName(MS::CORRECTED_DATA)){
    7001           0 :       colvec[nFound++] = MS::CORRECTED_DATA;
    7002             :     }
    7003           0 :     else if(tmpNames != "NONE"){  // "NONE" is used by ~SubMS().
    7004           0 :       os << LogIO::SEVERE;
    7005           0 :       if(nFound == 0){
    7006           0 :         colvec[0] = MS::DATA;
    7007           0 :         os << "Unrecognized data column " << tokens[i] << "...trying DATA.";
    7008             :       }
    7009             :       else
    7010           0 :         os << "Skipping unrecognized data column " << tokens[i];
    7011           0 :       os << LogIO::POST;
    7012             :     }
    7013             :   }
    7014           0 :   return nFound;
    7015           0 : }
    7016             : 
    7017             : 
    7018           0 : Bool SubMS::fillAccessoryMainCols(){
    7019           0 :   LogIO os(LogOrigin("SubMS", "fillAccessoryMainCols()"));
    7020           0 :   uInt nrows = mssel_p.nrow();
    7021             : 
    7022           0 :   msOut_p.addRow(nrows, true);
    7023             :   
    7024             :   //#ifdef COPYTIMER
    7025           0 :   Timer timer;
    7026           0 :   timer.mark();
    7027             :   //#endif
    7028           0 :   if(!antennaSel_p){
    7029           0 :     msc_p->antenna1().putColumn(mscIn_p->antenna1().getColumn());
    7030           0 :     msc_p->antenna2().putColumn(mscIn_p->antenna2().getColumn());
    7031             :     os << LogIO::DEBUG1
    7032             :        << "Straight copying ANTENNA* took " << timer.real() << "s."
    7033           0 :        << LogIO::POST;
    7034             :   }
    7035             :   else{
    7036           0 :     Vector<Int> ant1(mscIn_p->antenna1().getColumn());
    7037           0 :     Vector<Int> ant2(mscIn_p->antenna2().getColumn());
    7038             :     
    7039           0 :     for(uInt k = 0; k < nrows; ++k){
    7040           0 :       ant1[k] = antNewIndex_p[ant1[k]];
    7041           0 :       ant2[k] = antNewIndex_p[ant2[k]];
    7042             :     }
    7043           0 :     msc_p->antenna1().putColumn(ant1);
    7044           0 :     msc_p->antenna2().putColumn(ant2);
    7045             :     os << LogIO::DEBUG1
    7046             :        << "Selectively copying ANTENNA* took " << timer.real() << "s."
    7047           0 :        << LogIO::POST;
    7048           0 :   }
    7049             :   
    7050           0 :   timer.mark();
    7051           0 :   msc_p->feed1().putColumn(mscIn_p->feed1().getColumn());
    7052           0 :   msc_p->feed2().putColumn(mscIn_p->feed2().getColumn());
    7053             :   os << LogIO::DEBUG1
    7054             :      << "Copying FEED* took " << timer.real() << "s."
    7055           0 :      << LogIO::POST;
    7056             :   
    7057           0 :   timer.mark();
    7058           0 :   msc_p->exposure().putColumn(mscIn_p->exposure().getColumn());
    7059             :   os << LogIO::DEBUG1
    7060             :      << "Copying EXPOSURE took " << timer.real() << "s."
    7061           0 :      << LogIO::POST;
    7062             : 
    7063           0 :   timer.mark();
    7064           0 :   msc_p->flagRow().putColumn(mscIn_p->flagRow().getColumn());
    7065             :   os << LogIO::DEBUG1
    7066             :      << "Copying flagRow took " << timer.real() << "s."
    7067           0 :      << LogIO::POST;
    7068             : 
    7069           0 :   timer.mark();
    7070           0 :   msc_p->interval().putColumn(mscIn_p->interval().getColumn());
    7071             :   os << LogIO::DEBUG1
    7072             :      << "Copying INTERVAL took " << timer.real() << "s."
    7073           0 :      << LogIO::POST;
    7074             : 
    7075           0 :   timer.mark();
    7076           0 :   msc_p->scanNumber().putColumn(mscIn_p->scanNumber().getColumn());
    7077             :   os << LogIO::DEBUG1
    7078             :      << "Copying scanNumber took " << timer.real() << "s."
    7079           0 :      << LogIO::POST;
    7080             : 
    7081           0 :   timer.mark();
    7082           0 :   msc_p->time().putColumn(mscIn_p->time().getColumn());
    7083             :   os << LogIO::DEBUG1
    7084             :      << "Copying TIME took " << timer.real() << "s."
    7085           0 :      << LogIO::POST;
    7086             : 
    7087           0 :   timer.mark();
    7088           0 :   msc_p->timeCentroid().putColumn(mscIn_p->timeCentroid().getColumn());
    7089             :   os << LogIO::DEBUG1
    7090             :      << "Copying timeCentroid took " << timer.real() << "s."
    7091           0 :      << LogIO::POST;
    7092             :   
    7093             :   // ScalarMeasColumn doesn't have a putColumn() for some reason.
    7094             :   //msc_p->uvwMeas().putColumn(mscIn_p->uvwMeas());
    7095           0 :   timer.mark();
    7096             :   //msc_p->uvw().putColumn(mscIn_p->uvw());      // 98s for 4.7e6 rows
    7097             : 
    7098             :   // 3.06s for 4.7e6 rows
    7099             :   //RefRows refrows(0,  nrows - 1);
    7100             :   //msc_p->uvw().putColumnCells(refrows, mscIn_p->uvw().getColumn());
    7101             : 
    7102           0 :   msc_p->uvw().putColumn(mscIn_p->uvw().getColumn());   // 2.74s for 4.7e6 rows
    7103             :   os << LogIO::DEBUG1
    7104             :      << "Copying uvw took " << timer.real() << "s."
    7105           0 :      << LogIO::POST;
    7106             :   
    7107           0 :   msc_p->arrayId().putColumn(mscIn_p->arrayId().getColumn());
    7108           0 :   msc_p->processorId().putColumn(mscIn_p->processorId().getColumn());
    7109             : 
    7110           0 :   timer.mark();
    7111           0 :   relabelIDs();
    7112             :   os << LogIO::DEBUG1
    7113             :      << "relabelIDs took " << timer.real() << "s."
    7114           0 :      << LogIO::POST;
    7115             : 
    7116           0 :   return true;
    7117           0 : }
    7118             : 
    7119           0 :   Bool SubMS::writeAllMainRows(const Vector<MS::PredefinedColumns>& colNames)
    7120             :   {  
    7121           0 :     LogIO os(LogOrigin("SubMS", "writeAllMainRows()"));
    7122           0 :     Bool success = true;
    7123           0 :     Timer timer;
    7124             : 
    7125           0 :     fillAccessoryMainCols();
    7126             : 
    7127             :     //Deal with data, flags, sigma, and weights.
    7128           0 :     timer.mark();
    7129           0 :     if(keepShape_p){
    7130           0 :       Vector<MS::PredefinedColumns> complexCols;
    7131           0 :       const Bool doFloat = sepFloat(colNames, complexCols);
    7132           0 :       const uInt nDataCols = complexCols.nelements();
    7133           0 :       const Bool writeToDataCol = mustConvertToData(nDataCols, complexCols);
    7134             : 
    7135           0 :       copyDataFlagsWtSp(complexCols, writeToDataCol);
    7136           0 :       if(doFloat)
    7137           0 :         msc_p->floatData().putColumn(mscIn_p->floatData());
    7138           0 :     }
    7139             :     else{
    7140           0 :       doChannelMods(colNames);
    7141             :     }
    7142             :     os << LogIO::DEBUG1
    7143             :        << "Total data read/write time = " << timer.real()
    7144           0 :        << LogIO::POST;
    7145             :     
    7146           0 :     return success;
    7147           0 :   }
    7148             :   
    7149           0 : Bool SubMS::existsFlagCategory() const
    7150             : {
    7151           0 :   Bool hasFC = false;
    7152           0 :   if(!mscIn_p->flagCategory().isNull() &&
    7153           0 :      mscIn_p->flagCategory().isDefined(0)){
    7154           0 :     IPosition fcshape(mscIn_p->flagCategory().shape(0));
    7155           0 :     IPosition fshape(mscIn_p->flag().shape(0));
    7156             : 
    7157             :     // I don't know or care how many flag categories there are.
    7158           0 :     hasFC = fcshape(0) == fshape(0) && fcshape(1) == fshape(1);
    7159           0 :   }
    7160           0 :   return hasFC;
    7161             : }
    7162             : 
    7163           0 :   Bool SubMS::getDataColumn(ArrayColumn<Complex>& data,
    7164             :                             const MS::PredefinedColumns colName)
    7165             :   {
    7166           0 :     if(colName == MS::DATA)
    7167           0 :       data.reference(mscIn_p->data());
    7168           0 :     else if(colName == MS::MODEL_DATA)
    7169           0 :       data.reference(mscIn_p->modelData());
    7170           0 :     else if(colName == MS::LAG_DATA)
    7171           0 :       data.reference(mscIn_p->lagData());
    7172             :     else                                // The honored-by-time-if-nothing-else
    7173           0 :       data.reference(mscIn_p->correctedData()); // default.
    7174           0 :     return true;
    7175             :   }
    7176             : 
    7177           0 :   Bool SubMS::getDataColumn(ArrayColumn<Float>& data,
    7178             :                             const MS::PredefinedColumns colName)
    7179             :   {
    7180           0 :     LogIO os(LogOrigin("SubMS", "getDataColumn()"));
    7181             : 
    7182           0 :     if(colName != MS::FLOAT_DATA)
    7183             :       os << LogIO::WARN
    7184             :          << "Using FLOAT_DATA (because it has type Float) instead of the requested "
    7185             :          << colName
    7186           0 :          << LogIO::POST;
    7187             :     
    7188           0 :     data.reference(mscIn_p->floatData());
    7189           0 :     return true;
    7190           0 :   }
    7191             : 
    7192           0 :   Bool SubMS::putDataColumn(MSColumns& msc, ArrayColumn<Complex>& data, 
    7193             :                             const MS::PredefinedColumns colName,
    7194             :                             const Bool writeToDataCol)
    7195             :   {
    7196           0 :     if(writeToDataCol || colName == MS::DATA) 
    7197           0 :       msc.data().putColumn(data);
    7198           0 :     else if (colName ==  MS::MODEL_DATA)
    7199           0 :       msc.modelData().putColumn(data);
    7200           0 :     else if (colName == MS::CORRECTED_DATA)
    7201           0 :       msc.correctedData().putColumn(data);
    7202             :     //else if(colName == MS::FLOAT_DATA)              // promotion from Float
    7203             :     //  msc.floatData().putColumn(data);              // to Complex is pvt?
    7204           0 :     else if(colName == MS::LAG_DATA)
    7205           0 :       msc.lagData().putColumn(data);
    7206             :     else
    7207           0 :       return false;
    7208           0 :     return true;
    7209             :   }
    7210             : 
    7211           0 : void SubMS::setFitOrder(Int fitorder, Bool advise)
    7212             : {
    7213           0 :   fitorder_p = fitorder;
    7214             : 
    7215           0 :   if(advise){
    7216           0 :     LogIO os(LogOrigin("SubMS", "setFitOrder()"));
    7217             : 
    7218           0 :     if(fitorder < 0)
    7219             :       os << LogIO::NORMAL
    7220           0 :          << "Keeping the continuum.";
    7221           0 :     else if(fitorder > 1)
    7222             :       os << LogIO::WARN
    7223           0 :          << "Fit orders > 1 tend to drastically add noise to line channels.";
    7224           0 :     os << LogIO::POST;
    7225           0 :   }
    7226           0 : }
    7227             : 
    7228           0 : Bool SubMS::shouldWatch(Bool& conflict, const String& col,
    7229             :                         const String& uncombinable,
    7230             :                         const Bool verbose) const
    7231             : {
    7232           0 :   Bool wantWatch = !combine_p.contains(col);
    7233             : 
    7234           0 :   if(!wantWatch && uncombinable.contains(col)){
    7235           0 :     conflict = true;
    7236           0 :     wantWatch = false;
    7237             : 
    7238           0 :     if(verbose){
    7239           0 :       LogIO os(LogOrigin("SubMS", "shouldWatch()"));
    7240             : 
    7241             :       os << LogIO::WARN
    7242             :          << "Combining by " << col
    7243             :          << " was requested, but it is not allowed by this operation and will be ignored."
    7244           0 :          << LogIO::POST;
    7245           0 :     }
    7246             :   }
    7247           0 :   return wantWatch;
    7248             : }
    7249             : 
    7250           0 : Bool SubMS::setSortOrder(Block<Int>& sort, const String& uncombinable,
    7251             :                          const Bool verbose) const
    7252             : {
    7253           0 :   Bool conflict = false;
    7254           0 :   uInt n_cols_to_watch = 7;     // 3 + #(watchables), whether or not they are watched.
    7255             : 
    7256             :   // Already separated by the chunking.
    7257             :   //const Bool watch_array(!combine_p.contains("arr")); // Pirate talk for "array".
    7258             : 
    7259           0 :   Bool watch_obs   = shouldWatch(conflict, "obs",   uncombinable, verbose);
    7260           0 :   Bool watch_scan  = shouldWatch(conflict, "scan",  uncombinable, verbose);
    7261           0 :   Bool watch_spw   = shouldWatch(conflict, "spw",   uncombinable, verbose);
    7262           0 :   Bool watch_state = shouldWatch(conflict, "state", uncombinable, verbose);
    7263             : 
    7264             :   // if(watch_obs)
    7265             :   //   ++n_cols_to_watch;
    7266             :   // if(watch_scan)
    7267             :   //   ++n_cols_to_watch;
    7268             :   // if(watch_spw)
    7269             :   //   ++n_cols_to_watch;
    7270             :   // if(watch_state)
    7271             :   //   ++n_cols_to_watch;
    7272             : 
    7273           0 :   uInt colnum = 1;
    7274             : 
    7275           0 :   sort.resize(n_cols_to_watch);
    7276           0 :   sort[0] = MS::ARRAY_ID;
    7277           0 :   if(watch_scan){
    7278           0 :     sort[colnum] = MS::SCAN_NUMBER;
    7279           0 :     ++colnum;
    7280             :   }
    7281           0 :   if(watch_state){
    7282           0 :     sort[colnum] = MS::STATE_ID;
    7283           0 :     ++colnum;
    7284             :   }
    7285           0 :   sort[colnum] = MS::FIELD_ID;
    7286           0 :   ++colnum;
    7287           0 :   if(watch_spw){
    7288           0 :     sort[colnum] = MS::DATA_DESC_ID;
    7289           0 :     ++colnum;
    7290             :   }
    7291           0 :   sort[colnum] = MS::TIME;
    7292           0 :   ++colnum;  
    7293           0 :   if(watch_obs){
    7294           0 :     sort[colnum] = MS::OBSERVATION_ID;
    7295           0 :     ++colnum;
    7296             :   }
    7297             : 
    7298             :   // Now all the axes that should be combined should be added, so that they end
    7299             :   // up in the same chunk.
    7300           0 :   if(!watch_scan){
    7301           0 :     sort[colnum] = MS::SCAN_NUMBER;
    7302           0 :     ++colnum;
    7303             :   }
    7304           0 :   if(!watch_state){
    7305           0 :     sort[colnum] = MS::STATE_ID;
    7306           0 :     ++colnum;
    7307             :   }
    7308           0 :   if(!watch_spw){
    7309           0 :     sort[colnum] = MS::DATA_DESC_ID;
    7310           0 :     ++colnum;
    7311             :   }
    7312           0 :   if(!watch_obs){
    7313           0 :     sort[colnum] = MS::OBSERVATION_ID;
    7314             :     //++colnum;
    7315             :   }
    7316             : 
    7317           0 :   return !conflict;
    7318             : }
    7319             : 
    7320           0 : Bool SubMS::subtractContinuum(const Vector<MS::PredefinedColumns>& colNames,
    7321             :                               const VBRemapper& vbmaps)
    7322             : {
    7323           0 :   LogIO os(LogOrigin("SubMS", "subtractContinuum()"));
    7324           0 :   Bool retval = true;
    7325             : 
    7326           0 :   if(colNames.nelements() != 1){
    7327             :     os << LogIO::SEVERE
    7328             :        << "The continuum cannot be subtracted from > 1 *DATA column at a time."
    7329           0 :        << LogIO::POST;
    7330           0 :     return false;
    7331             :   }
    7332             : 
    7333           0 :   Block<Int> sort;
    7334           0 :   if(!setSortOrder(sort, "obs,scan,state", false)){
    7335             :     os << LogIO::WARN
    7336             :        << "This version of continuum subtraction intentionally does not support\n"
    7337             :        << "time smearing.  The only recommended (and used) values for combine in\n"
    7338             :        << "this case are '' or 'spw'."
    7339           0 :        << LogIO::POST;
    7340             :   }
    7341             : 
    7342             :   // Aaargh...everywhere else VisIter is used a timeInterval of 0 is treated as
    7343             :   // DBL_MAX, meaning that TIME can be in sort but effectively be ignored for
    7344             :   // major chunking.  Why couldn't they just have said DBL_MAX in the first
    7345             :   // place?
    7346           0 :   ROVisibilityIterator viIn(mssel_p, sort, false, DBL_MIN);
    7347             : 
    7348             :   // Make sure it is initialized before any copies are made.
    7349           0 :   viIn.originChunks();
    7350             : 
    7351           0 :   VBGContinuumSubtractor vbgcs(msOut_p, msc_p, vbmaps, viIn, fitorder_p,
    7352           0 :                                colNames[0], fitspw_p, fitoutspw_p);
    7353           0 :   vbgcs.setTVIDebug(tvi_debug);
    7354           0 :   vbgcs.setWantCont(want_cont_p);
    7355           0 :   GroupProcessor gp(viIn, &vbgcs);
    7356           0 :   gp.setTVIDebug(tvi_debug);
    7357             : 
    7358           0 :   retval = gp.go();
    7359             : 
    7360             :   // TODO: Support uvcontsub(3)'s spw parameter by
    7361             :   //  * filtering the output by fitoutspw_p
    7362             :   //  * remapping DDID as necessary, taking (union)spw into account
    7363             :   //  * filtering and rewriting the DATA_DESC_ID and SPECTRAL_WINDOW subtables.
    7364             : 
    7365           0 :   return retval;
    7366           0 : }
    7367             : 
    7368           0 : void SubMS::fill_vbmaps(std::map<VisBufferComponents::EnumType, std::map<Int, Int> >& vbmaps)
    7369             : {
    7370             :   // In general, _IDs which are row numbers in a subtable must be
    7371             :   // remapped, and those which are not probably shouldn't be.
    7372           0 :   if(antennaSel_p){
    7373           0 :     std::map<Int, Int> antIndexer;
    7374             : 
    7375           0 :     fillAntIndexer(antIndexer, mscIn_p);
    7376           0 :     vbmaps[VisBufferComponents::Ant1] = antIndexer;
    7377           0 :     vbmaps[VisBufferComponents::Ant2] = antIndexer;
    7378           0 :   }
    7379             : 
    7380           0 :   if(!allEQ(spwRelabel_p, spw_p)){
    7381           0 :     std::map<Int, Int> ddidMapper;
    7382             : 
    7383           0 :     for(uInt i = 0; i < oldDDSpwMatch_p.nelements(); ++i)
    7384           0 :       ddidMapper[i] = spwRelabel_p[oldDDSpwMatch_p[i]];
    7385             : 
    7386           0 :     vbmaps[VisBufferComponents::DataDescriptionId] = ddidMapper;
    7387           0 :   }
    7388             : 
    7389           0 :   if(fieldid_p.nelements() < mscIn_p->field().nrow()){
    7390           0 :     std::map<Int, Int> fldMapper;
    7391             :     
    7392           0 :     make_map2(fldMapper, fieldRelabel_p);
    7393           0 :     vbmaps[VisBufferComponents::FieldId] = fldMapper;
    7394           0 :   }
    7395             : 
    7396           0 :   if(selObsId_p.nelements() > 0 && selObsId_p.nelements() < mscIn_p->observation().nrow()){
    7397           0 :     std::map<Int, Int> obsMapper;
    7398             : 
    7399           0 :     make_map(obsMapper, selObsId_p);
    7400           0 :     vbmaps[VisBufferComponents::ObservationId] = obsMapper;
    7401           0 :   }
    7402             : 
    7403             :   //std::map<Int, Int> procMapper;
    7404             :   //make_map(procMapper, mscIn_p->processorId().getColumn());
    7405             : 
    7406           0 :   if(stateRemapper_p.size() < 1)
    7407           0 :     make_map(stateRemapper_p, mscIn_p->stateId().getColumn());
    7408           0 :   if(stateRemapper_p.size() < mscIn_p->state().nrow())
    7409           0 :     vbmaps[VisBufferComponents::StateId] = stateRemapper_p;
    7410           0 : }
    7411             : 
    7412           0 : Bool SubMS::copyDataFlagsWtSp(const Vector<MS::PredefinedColumns>& colNames,
    7413             :                               const Bool writeToDataCol)
    7414             :   {
    7415           0 :     Block<Int> columns;
    7416             :     // include scan and state iteration, for more optimal iteration
    7417           0 :     columns.resize(6);
    7418           0 :     columns[0]=MS::ARRAY_ID;
    7419           0 :     columns[1]=MS::SCAN_NUMBER;
    7420           0 :     columns[2]=MS::STATE_ID;
    7421           0 :     columns[3]=MS::FIELD_ID;
    7422           0 :     columns[4]=MS::DATA_DESC_ID;
    7423           0 :     columns[5]=MS::TIME;
    7424             : 
    7425             : #ifdef COPYTIMER
    7426             :     Timer timer;
    7427             :     timer.mark();
    7428             : 
    7429             :     Vector<Int> inscan, outscan;
    7430             : #endif
    7431             : 
    7432           0 :     ROVisIter viIn(mssel_p,columns,0.0);
    7433           0 :     VisIter viOut(msOut_p,columns,0.0);
    7434           0 :     viIn.setRowBlocking(1000);
    7435           0 :     viOut.setRowBlocking(1000);
    7436           0 :     Int iChunk(0), iChunklet(0);
    7437           0 :     Cube<Complex> data;
    7438           0 :     Cube<Bool> flag;
    7439           0 :     Array<Bool> flagcat;
    7440             : 
    7441           0 :     Matrix<Float> wtmat;
    7442           0 :     viIn.originChunks();                                // Makes me feel better.
    7443           0 :     const Bool doWtSp(viIn.existsWeightSpectrum());
    7444           0 :     Cube<Float> wtsp;
    7445             : 
    7446           0 :     uInt ninrows = mssel_p.nrow();
    7447             :     ProgressMeter meter(0.0, ninrows * 1.0, "split", "rows copied", "", "",
    7448           0 :                         true, 1);
    7449           0 :     uInt inrowsdone = 0;  // only for the meter.
    7450             : 
    7451           0 :     uInt nDataCols = colNames.nelements();
    7452             : 
    7453             :     // Is CORRECTED_DATA being moved to DATA?
    7454           0 :     Bool fromCorrToData = writeToDataCol && nDataCols == 1;
    7455           0 :     if(fromCorrToData)
    7456           0 :       fromCorrToData = colNames[0] == MS::CORRECTED_DATA;
    7457             : 
    7458           0 :     Bool doFC = existsFlagCategory();
    7459             : 
    7460           0 :     for (iChunk=0,viOut.originChunks(),viIn.originChunks();
    7461           0 :          viOut.moreChunks(),viIn.moreChunks();
    7462           0 :          viOut.nextChunk(),viIn.nextChunk(),++iChunk) {
    7463           0 :       inrowsdone += viIn.nRowChunk();
    7464             : 
    7465             :       // The following can help evaluable in/out index alignment
    7466             :       /*
    7467             :       cout << "****iChunk=" << iChunk 
    7468             :            << " scn: " << viIn.scan(inscan)(0) << "/" << viOut.scan(outscan)(0) << "   "
    7469             :            << "fld: " << viIn.fieldId() << "/"  << viOut.fieldId() << "   "
    7470             :            << "ddi: " << viIn.dataDescriptionId() << "/" << viOut.dataDescriptionId() << "   "
    7471             :            << "spw: " << viIn.spectralWindow() << "/"  << viOut.spectralWindow() << "   "
    7472             :            << endl;
    7473             :       */
    7474           0 :       for (iChunklet=0,viIn.origin(),viOut.origin();
    7475           0 :            viIn.more(),viOut.more();
    7476           0 :            viIn++,viOut++,++iChunklet) { //  
    7477             : 
    7478             :         //      cout << "nRows = " << viIn.nRow() << "/" << viOut.nRow() << endl;
    7479             : 
    7480             : #ifdef COPYTIMER
    7481             :         timer.mark();
    7482             : #endif
    7483           0 :         viIn.flag(flag);
    7484           0 :         viOut.setFlag(flag);
    7485           0 :         if(doFC){
    7486           0 :           viIn.flagCategory(flagcat);
    7487           0 :           viOut.setFlagCategory(flagcat);
    7488             :         }
    7489           0 :         if(fromCorrToData) {
    7490           0 :           viIn.weightMat(wtmat);
    7491           0 :           viOut.setWeightMat(wtmat);
    7492           0 :           arrayTransformInPlace(wtmat, subms::wtToSigma);   // for corrected weights.
    7493           0 :           viOut.setSigmaMat(wtmat);
    7494             :         }
    7495             :         else {
    7496           0 :           viIn.sigmaMat(wtmat);           // Yes, I'm reusing wtmat.
    7497           0 :           viOut.setSigmaMat(wtmat);
    7498           0 :           arrayTransformInPlace(wtmat, subms::sigToWeight);   // for corrected weights.
    7499           0 :           viOut.setWeightMat(wtmat);
    7500             :         }         
    7501             : 
    7502           0 :         if(doWtSp){
    7503           0 :           viIn.weightSpectrum(wtsp);
    7504           0 :           viOut.setWeightSpectrum(wtsp);
    7505             :         }
    7506             : 
    7507           0 :         for(uInt colnum = 0; colnum < nDataCols; ++colnum){
    7508           0 :           if(writeToDataCol || colNames[colnum] == MS::DATA) {
    7509             :             // write DATA, MODEL_DATA, or CORRECTED_DATA to DATA
    7510           0 :             switch (colNames[colnum]) {
    7511           0 :             case MS::DATA:
    7512           0 :               viIn.visibility(data,VisibilityIterator::Observed);
    7513           0 :               break;
    7514           0 :             case MS::MODEL_DATA:
    7515           0 :               viIn.visibility(data,VisibilityIterator::Model);
    7516           0 :               break;
    7517           0 :             case MS::CORRECTED_DATA:
    7518           0 :               viIn.visibility(data,VisibilityIterator::Corrected);
    7519           0 :               break;
    7520           0 :             default:
    7521           0 :               throw(AipsError("Unrecognized input column!"));
    7522             :               break;
    7523             :             }
    7524           0 :             viOut.setVis(data,VisibilityIterator::Observed);
    7525             :           }
    7526           0 :           else if (colNames[colnum] ==  MS::MODEL_DATA) {
    7527             :             // write MODEL_DATA to MODEL_DATA
    7528           0 :             viIn.visibility(data,VisibilityIterator::Model);
    7529           0 :             viOut.setVis(data,VisibilityIterator::Model);
    7530             :           }
    7531           0 :           else if (colNames[colnum] == MS::CORRECTED_DATA) {
    7532             :             // write CORRECTED_DATA to CORRECTED_DATA
    7533           0 :             viIn.visibility(data,VisibilityIterator::Corrected);
    7534           0 :             viOut.setVis(data,VisibilityIterator::Corrected);
    7535             :           }
    7536             :           //else if(colNames[colnum] == MS::FLOAT_DATA)              // TBD
    7537             :           //    else if(colNames[colnum] == MS::LAG_DATA)      // TBD
    7538             :           else
    7539           0 :             return false;
    7540             :         }
    7541             : 
    7542             : #ifdef COPYTIMER        
    7543             :         Double t=timer.real();
    7544             :         cout << "Chunk: " << iChunk << " " << iChunklet 
    7545             :              << " scn: " << viIn.scan(inscan)(0) << "/" << viOut.scan(outscan)(0) 
    7546             :              << "   "
    7547             :              << " spw: " << viIn.spectralWindow() << "/"  << viOut.spectralWindow() 
    7548             :              << " : "
    7549             :              << data.nelements() << " cells = " 
    7550             :              << data.nelements()*8.e-6 << " MB in " 
    7551             :              << t << " sec, for " << data.nelements()*8.e-6/t << " MB/s" 
    7552             :              << endl;
    7553             : #endif
    7554             :         
    7555             :       }
    7556           0 :       meter.update(inrowsdone);
    7557             :     }
    7558           0 :     return true;
    7559           0 :   }
    7560             : 
    7561           0 :   Bool SubMS::putDataColumn(MSColumns& msc, ArrayColumn<Float>& data, 
    7562             :                             const MS::PredefinedColumns colName,
    7563             :                             const Bool writeToDataCol)
    7564             :   {
    7565           0 :     LogIO os(LogOrigin("SubMS", "putDataColumn()"));
    7566             : 
    7567           0 :     if(writeToDataCol)
    7568             :       os << LogIO::NORMAL
    7569             :          << "Writing to FLOAT_DATA instead of DATA."
    7570           0 :          << LogIO::POST;
    7571             : 
    7572           0 :     if(colName == MS::FLOAT_DATA){
    7573           0 :       msc.floatData().putColumn(data);
    7574             :     }
    7575             :     else{
    7576             :       os << LogIO::SEVERE
    7577             :          << "Float data cannot be written to "
    7578             :          << MS::columnName(colName)
    7579           0 :          << LogIO::POST;
    7580           0 :       return false;
    7581             :     }
    7582           0 :     return true;
    7583           0 :   }
    7584             : 
    7585             :   // Sets outcol to row numbers in the corresponding subtable of its ms that
    7586             :   // correspond to the values of incol.
    7587             :   //
    7588             :   // Can only be used when incol and outcol have the same # of rows!
    7589             :   //
    7590           0 : void SubMS::remapColumn(ScalarColumn<Int>& outcol,
    7591             :                         const ScalarColumn<Int>& incol)
    7592             : {
    7593           0 :   uInt nrows = incol.nrow();
    7594             :     
    7595           0 :   if(nrows != outcol.nrow()){
    7596           0 :     ostringstream ostr;
    7597             : 
    7598           0 :     ostr << "SubMS::remapColumn(): the # of input rows, " << nrows
    7599           0 :          << ", != the # of output rows, " << outcol.nrow();
    7600           0 :     throw(AipsError(ostr.str()));
    7601           0 :   }
    7602             :     
    7603           0 :   std::map<Int, Int> mapper;
    7604           0 :   Vector<Int> inv(incol.getColumn());
    7605             : 
    7606           0 :   make_map(mapper, inv);
    7607           0 :   if(mapper.size() == 1){
    7608           0 :     outcol.fillColumn(0);        // Just a little optimization.
    7609             :   }
    7610             :   else{
    7611           0 :     Vector<Int> outv(nrows);
    7612             : 
    7613           0 :     if(mapper.size() > 0){
    7614           0 :       for(uInt k = 0; k < nrows; ++k)
    7615           0 :         outv[k] = mapper[inv[k]];
    7616             :     }
    7617             :     else
    7618           0 :       incol.getColumn(outv);
    7619           0 :     outcol.putColumn(outv);
    7620           0 :   }
    7621           0 : }
    7622             : 
    7623           0 : void SubMS::remapColumn(ScalarColumn<Int>& outcol,
    7624             :                         const ScalarColumn<Int>& incol,
    7625             :                         const Vector<Int>& selvals)
    7626             : {
    7627           0 :   uInt nrows = incol.nrow();
    7628             :     
    7629           0 :   if(nrows != outcol.nrow()){
    7630           0 :     ostringstream ostr;
    7631             : 
    7632           0 :     ostr << "SubMS::remapColumn(): the # of input rows, " << nrows
    7633           0 :          << ", != the # of output rows, " << outcol.nrow();
    7634           0 :     throw(AipsError(ostr.str()));
    7635           0 :   }
    7636             :     
    7637           0 :   std::map<Int, Int> mapper;
    7638           0 :   Vector<Int> inv(incol.getColumn());
    7639           0 :   Vector<Int> outv(nrows);
    7640             : 
    7641           0 :   make_map(mapper, selvals);
    7642           0 :   if(mapper.size() == 1){
    7643           0 :     outcol.fillColumn(0);        // Just a little optimization.
    7644             :   }
    7645             :   else{
    7646           0 :     Vector<Int> outv(nrows);
    7647             : 
    7648           0 :     if(mapper.size() > 0){
    7649           0 :       for(uInt k = 0; k < nrows; ++k)
    7650           0 :         outv[k] = mapper[inv[k]];
    7651             :     }
    7652             :     else
    7653           0 :       incol.getColumn(outv);
    7654           0 :     outcol.putColumn(outv);
    7655           0 :   }
    7656           0 : }
    7657             :   
    7658             : // Realigns some _ID vectors so that the output looks like a whole dataset
    7659             : // instead of part of one.  (i.e. we don't want references to unselected spws,
    7660             : // etc.)
    7661           0 : void SubMS::relabelIDs()
    7662             : {
    7663           0 :   const ScalarColumn<Int> inDDID(mscIn_p->dataDescId());
    7664           0 :   const ScalarColumn<Int> fieldId(mscIn_p->fieldId());
    7665             :   
    7666           0 :   for(Int k = inDDID.nrow(); k--;){
    7667           0 :     msc_p->dataDescId().put(k, spwRelabel_p[oldDDSpwMatch_p[inDDID(k)]]);
    7668           0 :     msc_p->fieldId().put(k, fieldRelabel_p[fieldId(k)]);
    7669             :   }
    7670             : 
    7671             :   //remapColumn(msc_p->arrayId(), mscIn_p->arrayId());
    7672           0 :   remapColumn(msc_p->stateId(), mscIn_p->stateId());
    7673             :   //remapColumn(msc_p->processorId(), mscIn_p->processorId());
    7674           0 :   remapColumn(msc_p->observationId(), mscIn_p->observationId(), selObsId_p);
    7675           0 : }
    7676             : 
    7677           0 : Bool SubMS::writeSomeMainRows(const Vector<MS::PredefinedColumns>& colNames)
    7678             : {    
    7679           0 :   LogIO os(LogOrigin("SubMS", "writeSomeMainRows()"));
    7680           0 :   Bool retval = true;
    7681             :     
    7682             :   os << LogIO::DEBUG1 // helpdesk ticket in from Oleg Smirnov (ODU-232630)
    7683             :      << "Before fillAntIndexer(): "
    7684           0 :      << Memory::allocatedMemoryInBytes() / (1024.0 * 1024.0) << " MB"
    7685           0 :      << LogIO::POST;
    7686             : 
    7687             :   // A set of maps from input ID to output ID, keyed by VisBufferComponent.
    7688           0 :   std::map<VisBufferComponents::EnumType, std::map<Int, Int> > vbmaps;
    7689           0 :   fill_vbmaps(vbmaps);
    7690           0 :   VBRemapper remapper(vbmaps);
    7691             : 
    7692             :   //things to be taken care of in doTimeAver() or subtractContinuum...
    7693             :   // flagRow            ScanNumber      uvw             weight          
    7694             :   // sigma              ant1            ant2            time
    7695             :   // timeCentroid       feed1           feed2           exposure
    7696             :   // stateId            processorId     observationId   arrayId
    7697           0 :   if(fitorder_p >= 0)
    7698           0 :     retval = subtractContinuum(colNames, remapper); // writeToDataCol = complexCols == 1
    7699             :   else
    7700           0 :     retval = (corrString_p != "") ? doTimeAverVisIterator(colNames, remapper)
    7701           0 :                                   : doTimeAver(colNames, remapper);
    7702           0 :   return retval;
    7703           0 : }
    7704             : 
    7705           0 : uInt SubMS::addOptionalColumns(const Table& inTab, Table& outTab,
    7706             :                                const Bool beLazy)
    7707             : {
    7708           0 :   uInt nAdded = 0;    
    7709           0 :   const TableDesc& inTD = inTab.actualTableDesc();
    7710             :     
    7711             :   // Only rely on the # of columns if you are sure that inTab and outTab
    7712             :   // can't have the same # of columns without having _different_ columns,
    7713             :   // i.e. use beLazy if outTab.actualTableDesc() is in its default state.
    7714           0 :   uInt nInCol = inTD.ncolumn();
    7715           0 :   if(!beLazy || nInCol > outTab.actualTableDesc().ncolumn()){
    7716           0 :     LogIO os(LogOrigin("SubMS", "addOptionalColumns()"));
    7717             : 
    7718           0 :     Vector<String> oldColNames = inTD.columnNames();
    7719             :       
    7720           0 :     for(uInt k = 0; k < nInCol; ++k){
    7721           0 :       if(!outTab.actualTableDesc().isColumn(oldColNames[k])){
    7722             :         //TableDesc tabDesc;
    7723             :         try{
    7724             :           //M::addColumnToDesc(tabDesc, M::columnType(oldColNames[k]));
    7725             :           //if(tabDesc.ncolumn())                 // The tabDesc[0] is too 
    7726             :           //  outTab.addColumn(tabDesc[0]);       // dangerous otherwise - it 
    7727             :           //else                                  // can dump core without
    7728             :           //  throw(AipsError("Unknown column")); // throwing an exception.
    7729           0 :           outTab.addColumn(inTD.columnDesc(k), false);
    7730           0 :           ++nAdded;
    7731             :         }
    7732           0 :         catch(...){   // NOT AipsError x
    7733             :           os << LogIO::WARN 
    7734           0 :              << "Could not add column " << oldColNames[k] << " to "
    7735             :              << outTab.tableName()
    7736           0 :              << LogIO::POST;
    7737           0 :         }
    7738             :       }
    7739             :     }
    7740           0 :   }
    7741           0 :   return nAdded;
    7742           0 : }
    7743             :   
    7744           0 : Bool SubMS::copyCols(Table& out, const Table& in, const Bool flush)
    7745             : {
    7746           0 :   LogIO os(LogOrigin("SubMS", "copyCols()"));
    7747           0 :   const TableDesc& inTD = in.actualTableDesc();
    7748           0 :   Vector<String> inColNames = inTD.columnNames();
    7749           0 :   uInt nInCol = inTD.ncolumn();
    7750           0 :   Bool retval = true;
    7751             : 
    7752           0 :   if(in.nrow() > out.nrow())
    7753           0 :     out.addRow(in.nrow() - out.nrow());
    7754             : 
    7755           0 :   for(uInt k = 0; k < nInCol; ++k){
    7756             :     // Add the input column desc to out if necessary.
    7757           0 :     if(!out.actualTableDesc().isColumn(inColNames[k])){
    7758             :       try{
    7759           0 :         out.addColumn(inTD.columnDesc(k), false);
    7760             :       }
    7761           0 :       catch(...){   // NOT AipsError x
    7762             :         os << LogIO::WARN 
    7763           0 :            << "Could not add column " << inColNames[k] << " to "
    7764             :            << out.tableName()
    7765           0 :            << LogIO::POST;
    7766           0 :         retval = false;
    7767           0 :       }
    7768             :     }
    7769             : 
    7770             :     // I can't see a way to explicitly* avoid constructing these in each
    7771             :     // iteration.  (attach() would implicitly construct them.)
    7772             :     // * without relying on compiler optimization.
    7773           0 :     TableColumn incol(in, inColNames[k]);
    7774           0 :     TableColumn outcol(out, inColNames[k]);
    7775             : 
    7776           0 :     outcol.putColumn(incol);
    7777           0 :   }
    7778           0 :   if(flush)
    7779           0 :     out.flush();
    7780           0 :   return retval;
    7781           0 : }
    7782             : 
    7783           0 :   Bool SubMS::copyAntenna(){
    7784           0 :     const MSAntenna& oldAnt = mssel_p.antenna();
    7785           0 :     MSAntenna& newAnt = msOut_p.antenna();
    7786           0 :     const MSAntennaColumns incols(oldAnt);
    7787           0 :     MSAntennaColumns         outcols(newAnt);
    7788           0 :     Bool                     retval = false;
    7789             :     
    7790           0 :     outcols.setOffsetRef(MPosition::castType(incols.offsetMeas().getMeasRef().getType()));
    7791           0 :     outcols.setPositionRef(MPosition::castType(incols.positionMeas().getMeasRef().getType()));
    7792             : 
    7793           0 :     if(!antennaSel_p){
    7794           0 :       TableCopy::copyRows(newAnt, oldAnt);
    7795           0 :       retval = true;
    7796             :     }
    7797             :     else{
    7798           0 :       uInt nAnt = antNewIndex_p.nelements();
    7799           0 :       if(nAnt > oldAnt.nrow())                  // Don't use min() here,
    7800           0 :         nAnt = oldAnt.nrow();                   // it's too overloaded.
    7801             : 
    7802           0 :       for(uInt k = 0; k < nAnt; ++k){
    7803           0 :         if(antNewIndex_p[k] > -1)
    7804           0 :           TableCopy::copyRows(newAnt, oldAnt, antNewIndex_p[k], k, 1, false);
    7805             :       }
    7806           0 :       retval = true;
    7807             :     }
    7808           0 :     return retval;    
    7809           0 :   }
    7810             : 
    7811             : 
    7812           0 :   Bool SubMS::copyFeed()
    7813             :   {
    7814           0 :     const MSFeed& oldFeed = mssel_p.feed();
    7815             : 
    7816             :     // if(oldFeed.nrow() < 1)     Useless, because it ignores spw selection
    7817             :     
    7818           0 :     MSFeed& newFeed = msOut_p.feed();
    7819           0 :     const MSFeedColumns incols(oldFeed);
    7820           0 :     MSFeedColumns         outcols(newFeed);
    7821             :     
    7822           0 :     outcols.setDirectionRef(MDirection::castType(incols.beamOffsetMeas().getMeasRef().getType()));
    7823           0 :     outcols.setEpochRef(MEpoch::castType(incols.timeMeas().getMeasRef().getType()));
    7824           0 :     outcols.setPositionRef(MPosition::castType(incols.positionMeas().getMeasRef().getType()));
    7825             : 
    7826           0 :     if(!antennaSel_p && allEQ(spwRelabel_p, spw_p)){
    7827           0 :       TableCopy::copyRows(newFeed, oldFeed);
    7828             :     }
    7829             :     else{
    7830           0 :       const Vector<Int>& antIds = incols.antennaId().getColumn();
    7831           0 :       const Vector<Int>& spwIds = incols.spectralWindowId().getColumn();
    7832             : 
    7833             :       // Copy selected rows.
    7834           0 :       uInt totNFeeds = antIds.nelements();
    7835           0 :       uInt totalSelFeeds = 0;
    7836           0 :       Int maxSelAntp1 = antNewIndex_p.nelements();
    7837           0 :       for (uInt k = 0; k < totNFeeds; ++k){
    7838             :         // antenna must be selected, and spwId must be -1 (any) or selected.
    7839           0 :         if(antIds[k] < maxSelAntp1 && antNewIndex_p[antIds[k]] > -1 &&
    7840           0 :            (spwIds[k] < 0 || spwRelabel_p[spwIds[k]] > -1)){
    7841             :           //                  outtab   intab    outrow       inrow nrows
    7842           0 :           TableCopy::copyRows(newFeed, oldFeed, totalSelFeeds, k, 1, false);
    7843           0 :           ++totalSelFeeds;
    7844             :         }
    7845             :       }
    7846             : 
    7847             :       // Remap antenna and spw #s.
    7848           0 :       ScalarColumn<Int>& antCol = outcols.antennaId();
    7849           0 :       ScalarColumn<Int>& spwCol = outcols.spectralWindowId();
    7850             : 
    7851           0 :       for(uInt k = 0; k < totalSelFeeds; ++k){
    7852           0 :         antCol.put(k, antNewIndex_p[antCol(k)]);
    7853           0 :         if(spwCol(k) > -1)
    7854           0 :           spwCol.put(k, spwRelabel_p[spwCol(k)]);
    7855             :       }
    7856           0 :     }
    7857             : 
    7858           0 :     if(newFeed.nrow() < 1){
    7859           0 :       LogIO os(LogOrigin("SubMS", "copyFeed()"));
    7860           0 :       os << LogIO::SEVERE << "No feeds were selected." << LogIO::POST;
    7861           0 :       return false;
    7862           0 :     }
    7863           0 :     return true;
    7864           0 :   }
    7865             :   
    7866           0 :   Bool SubMS::copyFlag_Cmd(){
    7867             :     // Like POINTING, FLAG_CMD is supposed to exist but is allowed not to.
    7868           0 :     if(Table::isReadable(mssel_p.flagCmdTableName())){
    7869             :       // An attempt to select from FLAG_CMD by timerange.  Fails because the
    7870             :       // TEN refers to the main table, not FLAG_CMD.
    7871             :       // TableExprNode condition;
    7872             : 
    7873             :       // if(timeRange_p != "" &&
    7874             :       //         msTimeGramParseCommand(&ms_p, timeRange_p, condition) == 0){
    7875             :       //        const TableExprNode *timeNode = 0x0;
    7876             :           
    7877             :       //        timeNode = msTimeGramParseNode();
    7878             :       //        if(timeNode && !timeNode->isNull())
    7879             :       //          condition = *timeNode;
    7880             :       // }
    7881             : 
    7882           0 :       const MSFlagCmd& oldFlag_Cmd = mssel_p.flagCmd();
    7883             : 
    7884           0 :       if(oldFlag_Cmd.nrow() > 0){
    7885             :         // Could be declared as Table&
    7886           0 :         MSFlagCmd& newFlag_Cmd = msOut_p.flagCmd();
    7887             : 
    7888           0 :         LogIO os(LogOrigin("SubMS", "copyFlag_Cmd()"));
    7889             : 
    7890             :         // Add optional columns if present in oldFlag_Cmd.
    7891           0 :         uInt nAddedCols = addOptionalColumns(oldFlag_Cmd, newFlag_Cmd, true);
    7892             :         os << LogIO::DEBUG1 << "FLAG_CMD has " << nAddedCols
    7893           0 :            << " optional columns." << LogIO::POST;
    7894             :         
    7895           0 :         const MSFlagCmdColumns oldFCs(oldFlag_Cmd);
    7896           0 :         MSFlagCmdColumns newFCs(newFlag_Cmd);
    7897           0 :         newFCs.setEpochRef(MEpoch::castType(oldFCs.timeMeas().getMeasRef().getType()));
    7898             :         
    7899             :         //if(!antennaSel_p && timeRange_p == ""){
    7900           0 :           TableCopy::copyRows(newFlag_Cmd, oldFlag_Cmd);
    7901             :         // }
    7902             :         // else{
    7903             :         //   const ScalarColumn<Double>& time = oldFCs.time();
    7904             : 
    7905             :         //   uInt nTRanges = selTimeRanges_p.ncolumn();
    7906             : 
    7907             :         //   uInt outRow = 0;
    7908             :         //   for (uInt inRow = 0; inRow < antIds.nrow(); ++inRow){
    7909             :         //     Int newAntInd = antIds(inRow);
    7910             :         //     if(antennaSel_p)
    7911             :         //       newAntInd = antNewIndex_p[newAntInd];
    7912             :         //     Double t = time(inRow);
    7913             :             
    7914             :         //     if(newAntInd > -1){
    7915             :         //       Bool matchT = false;
    7916             :         //       for(uInt tr = 0; tr < nTRanges; ++tr){
    7917             :         //      if(t >= selTimeRanges_p(0, tr) && t <= selTimeRanges_p(1, tr)){
    7918             :         //        matchT = true;
    7919             :         //        break;
    7920             :         //      }
    7921             :         //       }
    7922             :               
    7923             :         //       if(matchT){
    7924             :         //      TableCopy::copyRows(newFlag_Cmd, oldFlag_Cmd, outRow, inRow, 1, false);
    7925             :         //      outants.put(outRow, newAntInd);
    7926             :         //      ++outRow;
    7927             :         //       }
    7928             :         //     }
    7929             :         //   }
    7930           0 :       }
    7931             :     }
    7932           0 :     return true;
    7933             :   }
    7934             :   
    7935           0 :   Bool SubMS::copyHistory(){
    7936           0 :     const MSHistory& oldHistory = mssel_p.history();
    7937             : 
    7938             :     // Could be declared as Table&
    7939           0 :     MSHistory& newHistory = msOut_p.history();
    7940             : 
    7941           0 :     LogIO os(LogOrigin("SubMS", "copyHistory()"));
    7942             : 
    7943             :     // Add optional columns if present in oldHistory.
    7944           0 :     uInt nAddedCols = addOptionalColumns(oldHistory, newHistory, true);
    7945             :     os << LogIO::DEBUG1 << "HISTORY has " << nAddedCols
    7946           0 :        << " optional columns." << LogIO::POST;
    7947             :         
    7948           0 :     const MSHistoryColumns oldHCs(oldHistory);
    7949           0 :     MSHistoryColumns newHCs(newHistory);
    7950           0 :     newHCs.setEpochRef(MEpoch::castType(oldHCs.timeMeas().getMeasRef().getType()));
    7951             :         
    7952           0 :     TableCopy::copyRows(newHistory, oldHistory);
    7953           0 :     return true;
    7954           0 :   }
    7955             :   
    7956           0 :   Bool SubMS::copySource(){
    7957             :     //Source is an optional table, so it may not exist
    7958           0 :     if(Table::isReadable(mssel_p.sourceTableName())){
    7959           0 :       LogIO os(LogOrigin("SubMS", "copySource()"));
    7960             : 
    7961           0 :       const MSSource& oldSource = mssel_p.source();
    7962           0 :       MSSource& newSource = msOut_p.source();
    7963             :       
    7964             :       // Add optional columns if present in oldSource.
    7965           0 :       uInt nAddedCols = addOptionalColumns(oldSource, newSource, true);
    7966             :       os << LogIO::DEBUG1 << "SOURCE has " << nAddedCols
    7967           0 :          << " optional columns." << LogIO::POST;
    7968             :       
    7969           0 :       const MSSourceColumns incols(oldSource);
    7970           0 :       MSSourceColumns         outcols(newSource);
    7971             : 
    7972             :       // Copy the Measures frame info.  This has to be done before filling the
    7973             :       // rows.
    7974           0 :       outcols.setEpochRef(MEpoch::castType(incols.timeMeas().getMeasRef().getType()));
    7975           0 :       outcols.setDirectionRef(MDirection::castType(incols.directionMeas().getMeasRef().getType()));
    7976           0 :       outcols.setPositionRef(MPosition::castType(incols.positionMeas().getMeasRef().getType()));
    7977           0 :       outcols.setFrequencyRef(MFrequency::castType(incols.restFrequencyMeas().getMeasRef().getType()));
    7978           0 :       outcols.setRadialVelocityRef(MRadialVelocity::castType(incols.sysvelMeas().getMeasRef().getType()));
    7979             : 
    7980           0 :       const ScalarColumn<Int>& inSId   = incols.sourceId();
    7981           0 :       ScalarColumn<Int>&       outSId  = outcols.sourceId();
    7982           0 :       const ScalarColumn<Int>& inSPW   = incols.spectralWindowId();
    7983           0 :       ScalarColumn<Int>&       outSPW  = outcols.spectralWindowId();
    7984             : 
    7985             :       // 2009-06-09: It is hard to say whether to remap pulsarID when the
    7986             :       // PULSAR table is not described in the MS v2.0 def'n.
    7987             : //       const ScalarColumn<Int>& inPId   = incols.pulsarId();
    7988             : //       ScalarColumn<Int>&    outPId  = outcols.pulsarId();
    7989             : 
    7990           0 :       uInt outrn = 0;                                   // row number in output.
    7991           0 :       uInt nInputRows = inSId.nrow();
    7992           0 :       Int maxSId = sourceRelabel_p.nelements();  // inSidVal is Int.
    7993           0 :       Int maxSPWId = spwRelabel_p.nelements();
    7994           0 :       for(uInt inrn = 0; inrn < nInputRows; ++inrn){
    7995           0 :         Int inSidVal = inSId(inrn);
    7996           0 :         Int inSPWVal = inSPW(inrn);  // -1 means the source is valid for any SPW.
    7997             :         
    7998           0 :         if(inSidVal >= maxSId)
    7999             :           os << LogIO::WARN
    8000           0 :              << "Invalid SOURCE ID in SOURCE table row " << inrn << LogIO::POST;
    8001           0 :         if(inSPWVal >= maxSPWId)
    8002             :           os << LogIO::WARN
    8003           0 :              << "Invalid SPW ID in SOURCE table row " << inrn << LogIO::POST;
    8004             : 
    8005           0 :         if((inSidVal > -1) && (inSidVal < maxSId) &&
    8006           0 :            (sourceRelabel_p[inSidVal] > -1) && 
    8007           0 :            ((inSPWVal == -1) || (inSPWVal < maxSPWId && spwRelabel_p[inSPWVal] > -1))){
    8008             :           // Copy inrn to outrn.
    8009           0 :           TableCopy::copyRows(newSource, oldSource, outrn, inrn, 1);
    8010           0 :           outSId.put(outrn, sourceRelabel_p[inSidVal]);
    8011           0 :           outSPW.put(outrn, inSPWVal > -1 ? spwRelabel_p[inSPWVal] : -1);
    8012             :           
    8013           0 :           ++outrn;
    8014             :         }
    8015             :       }
    8016             : 
    8017           0 :       return true;
    8018           0 :     }
    8019             :   
    8020           0 :     return false;
    8021             :   }
    8022             :   
    8023           0 : Bool SubMS::copyGenericSubtables(){
    8024           0 :   LogIO os(LogOrigin("SubMS", "copyGenericSubtables()"));
    8025             : 
    8026             :   // Already handled subtables will be removed from this, so a modifiable copy
    8027             :   // is needed.
    8028           0 :   TableRecord inkws(mssel_p.keywordSet());
    8029             : 
    8030             :   // Some of the standard subtables need special handling,
    8031             :   // e.g. DATA_DESCRIPTION, SPECTRAL_WINDOW, and ANTENNA, so they are already
    8032             :   // defined in msOut_p.  Several more (e.g. FLAG_CMD) were also already
    8033             :   // created by MS::createDefaultSubtables().  Don't try to write over them - a
    8034             :   // locking error will result.
    8035           0 :   const TableRecord& outkws = msOut_p.keywordSet();
    8036           0 :   for(uInt i = 0; i < outkws.nfields(); ++i){
    8037             :     // os << LogIO::DEBUG1
    8038             :     //    << "outkws.name(" << i << "): " << outkws.name(i)
    8039             :     //    << LogIO::POST;
    8040           0 :     if(outkws.type(i) == TpTable && inkws.isDefined(outkws.name(i)))
    8041           0 :       inkws.removeField(outkws.name(i));
    8042             :   }
    8043             : 
    8044             :   // Includes a flush. 
    8045             :   //msOut_p.unlock();
    8046             : 
    8047             :   // msOut_p.rwKeywordSet() (pass a reference, not a copy) will put a lock on
    8048             :   // msOut_p.
    8049           0 :   TableCopy::copySubTables(msOut_p.rwKeywordSet(), inkws, msOut_p.tableName(),
    8050             :                            msOut_p.tableType(), mssel_p);
    8051             :   // TableCopy::copySubTables(Table, Table, Bool) includes this other code,
    8052             :   // which seems to be copying subtables at one level deeper, but not
    8053             :   // recursively? 
    8054           0 :   const TableDesc& inDesc = mssel_p.tableDesc();
    8055           0 :   const TableDesc& outDesc = msOut_p.tableDesc();
    8056           0 :   for(uInt i = 0; i < outDesc.ncolumn(); ++i){
    8057             :     // Only writable cols can have keywords (and thus subtables) defined.
    8058           0 :     if(msOut_p.isColumnWritable(i)){
    8059           0 :       const String& name = outDesc[i].name();
    8060             : 
    8061           0 :       if(inDesc.isColumn(name)){
    8062           0 :         TableColumn outCol(msOut_p, name);
    8063           0 :         TableColumn inCol(mssel_p, name);
    8064             :         
    8065           0 :         TableCopy::copySubTables(outCol.rwKeywordSet(), inCol.keywordSet(),
    8066             :                                  msOut_p.tableName(), msOut_p.tableType(),
    8067             :                                  mssel_p);
    8068             : 
    8069             :         // Copy the keywords if column is FLOAT_DATA
    8070           0 :         if (name == "FLOAT_DATA")
    8071           0 :                 copyMainTableKeywords(outCol.rwKeywordSet(), inCol.keywordSet());
    8072             : 
    8073           0 :       }
    8074             :     }
    8075             :   }
    8076             : 
    8077           0 :   return true;
    8078           0 : }
    8079             : 
    8080           0 :   Bool SubMS::copyObservation()
    8081             :   {
    8082           0 :     const MSObservation& oldObs = mssel_p.observation();
    8083           0 :     MSObservation& newObs = msOut_p.observation();
    8084           0 :     const MSObservationColumns oldObsCols(oldObs);
    8085           0 :     MSObservationColumns newObsCols(newObs);
    8086           0 :     newObsCols.setEpochRef(MEpoch::castType(oldObsCols.releaseDateMeas().getMeasRef().getType()));
    8087             : 
    8088           0 :     uInt nObs = selObsId_p.nelements();
    8089           0 :     if(nObs > 0){
    8090           0 :       for(uInt outrn = 0; outrn < nObs; ++outrn)
    8091           0 :         TableCopy::copyRows(newObs, oldObs, outrn, selObsId_p[outrn], 1);
    8092             :     }
    8093             :     else  // '' -> '*'
    8094           0 :       TableCopy::copyRows(newObs, oldObs);
    8095             :       
    8096             :     //W TableCopy::deepCopy(newObs, oldObs, false);
    8097             :     
    8098           0 :     return true;
    8099           0 :   }
    8100             :   
    8101           0 :   Bool SubMS::copyProcessor()
    8102             :   {  
    8103           0 :     const MSProcessor& oldProc = mssel_p.processor();
    8104           0 :     MSProcessor& newProc = msOut_p.processor();
    8105             : 
    8106           0 :     TableCopy::copyRows(newProc, oldProc);
    8107             :     //W TableCopy::deepCopy(newProc, oldProc, false);
    8108             :     
    8109           0 :     return true;
    8110             :   }
    8111             : 
    8112           0 : Bool SubMS::copyState()
    8113             : {  
    8114             :   // STATE is allowed to not exist, even though it is not optional in the MS
    8115             :   // def'n.  For one thing, split dropped it for quite a while.
    8116           0 :   if(Table::isReadable(mssel_p.stateTableName())){
    8117           0 :     LogIO os(LogOrigin("SubMS", "copyState()"));
    8118           0 :     const MSState& oldState = mssel_p.state();
    8119             : 
    8120           0 :     if(oldState.nrow() > 0){
    8121           0 :       MSState& newState = msOut_p.state();
    8122           0 :       const MSStateColumns oldStateCols(oldState);
    8123           0 :       MSStateColumns newStateCols(newState);
    8124             : 
    8125             :       // Initialize stateRemapper_p if necessary.
    8126           0 :       if(stateRemapper_p.size() < 1)
    8127           0 :         make_map(stateRemapper_p, mscIn_p->stateId().getColumn());
    8128             : 
    8129           0 :       uInt nStates = stateRemapper_p.size();
    8130             :       
    8131             :       // stateRemapper_p goes from input to output, as is wanted in most
    8132             :       // places.  Here we need a map going the other way, so make one.
    8133           0 :       Vector<Int> outStateToInState(nStates);
    8134           0 :       std::map<Int, Int>::iterator mit;
    8135             :       
    8136           0 :       for(mit = stateRemapper_p.begin(); mit != stateRemapper_p.end(); ++mit)
    8137           0 :         outStateToInState[(*mit).second] = (*mit).first;
    8138             :       
    8139           0 :       for(uInt outrn = 0; outrn < nStates; ++outrn)
    8140           0 :         TableCopy::copyRows(newState, oldState, outrn,
    8141           0 :                             outStateToInState[outrn], 1);
    8142           0 :     }
    8143           0 :   }
    8144           0 :   return true;
    8145             : }
    8146             : 
    8147           0 : void SubMS::createSubtables(MeasurementSet& ms, Table::TableOption option)
    8148             : {
    8149           0 :   SetupNewTable antennaSetup(ms.antennaTableName(),
    8150           0 :                              MSAntenna::requiredTableDesc(),option);
    8151           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::ANTENNA),
    8152           0 :                              Table(antennaSetup));
    8153           0 :   SetupNewTable dataDescSetup(ms.dataDescriptionTableName(),
    8154           0 :                               MSDataDescription::requiredTableDesc(),option);
    8155           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::DATA_DESCRIPTION), 
    8156           0 :                              Table(dataDescSetup));
    8157           0 :   SetupNewTable feedSetup(ms.feedTableName(),
    8158           0 :                           MSFeed::requiredTableDesc(),option);
    8159           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::FEED), Table(feedSetup));
    8160           0 :   SetupNewTable flagCmdSetup(ms.flagCmdTableName(),
    8161           0 :                              MSFlagCmd::requiredTableDesc(),option);
    8162           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::FLAG_CMD), 
    8163           0 :                              Table(flagCmdSetup));
    8164           0 :   SetupNewTable fieldSetup(ms.fieldTableName(),
    8165           0 :                            MSField::requiredTableDesc(),option);
    8166           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::FIELD), Table(fieldSetup));
    8167           0 :   SetupNewTable historySetup(ms.historyTableName(),
    8168           0 :                              MSHistory::requiredTableDesc(),option);
    8169           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::HISTORY), 
    8170           0 :                              Table(historySetup));
    8171           0 :   SetupNewTable observationSetup(ms.observationTableName(),
    8172           0 :                                  MSObservation::requiredTableDesc(),option);
    8173           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::OBSERVATION), 
    8174           0 :                                 Table(observationSetup));
    8175           0 :   SetupNewTable polarizationSetup(ms.polarizationTableName(),
    8176           0 :                                   MSPolarization::requiredTableDesc(),option);
    8177           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::POLARIZATION),
    8178           0 :                                 Table(polarizationSetup));
    8179           0 :   SetupNewTable processorSetup(ms.processorTableName(),
    8180           0 :                                MSProcessor::requiredTableDesc(),option);
    8181           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::PROCESSOR),
    8182           0 :                                 Table(processorSetup));
    8183           0 :   SetupNewTable spectralWindowSetup(ms.spectralWindowTableName(),
    8184           0 :                                     MSSpectralWindow::requiredTableDesc(),option);
    8185           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::SPECTRAL_WINDOW),  
    8186           0 :                                 Table(spectralWindowSetup));
    8187           0 :   SetupNewTable stateSetup(ms.stateTableName(),
    8188           0 :                            MSState::requiredTableDesc(),option);
    8189           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::STATE),  
    8190           0 :                                 Table(stateSetup));
    8191             : 
    8192             :   // add the optional Source sub table to allow for specification of the rest
    8193             :   // frequency
    8194           0 :   SetupNewTable sourceSetup(ms.sourceTableName(), MSSource::requiredTableDesc(),
    8195           0 :                             option);
    8196           0 :   ms.rwKeywordSet().defineTable(MS::keywordName(MS::SOURCE),
    8197           0 :                                 Table(sourceSetup, 0));
    8198             : 
    8199             :   // update the references to the subtable keywords
    8200           0 :   ms.initRefs();
    8201           0 : }
    8202             :   
    8203           0 :   Bool SubMS::copyPointing(){
    8204           0 :     LogIO os(LogOrigin("SubMS", "copyPointing()"));
    8205             : 
    8206             :     //Pointing is allowed to not exist
    8207           0 :     if(Table::isReadable(mssel_p.pointingTableName())){
    8208             :       // An attempt to select from POINTING by timerange.  Fails because the
    8209             :       // TEN refers to the main table, not POINTING.
    8210             :       // TableExprNode condition;
    8211             : 
    8212             :       // if(timeRange_p != "" &&
    8213             :       //         msTimeGramParseCommand(&ms_p, timeRange_p, condition) == 0){
    8214             :       //        const TableExprNode *timeNode = 0x0;
    8215             :           
    8216             :       //        timeNode = msTimeGramParseNode();
    8217             :       //        if(timeNode && !timeNode->isNull())
    8218             :       //          condition = *timeNode;
    8219             :       // }
    8220             : 
    8221             :       //Wconst Table oldPoint(mssel_p.pointingTableName(), Table::Old);
    8222           0 :       const MSPointing& oldPoint = mssel_p.pointing();
    8223             : 
    8224           0 :       if(!antennaSel_p && timeRange_p == ""){
    8225           0 :         copySubtable(MS::keywordName(MS::POINTING), oldPoint);
    8226             :       }
    8227             :       else{
    8228           0 :         setupNewPointing();
    8229             : 
    8230           0 :         if(oldPoint.nrow() > 0){
    8231             :           // Could be declared as Table&
    8232           0 :           MSPointing& newPoint = msOut_p.pointing();
    8233             :           // Add optional columns if present in oldPoint.
    8234           0 :           uInt nAddedCols = addOptionalColumns(oldPoint, newPoint, true);
    8235             :           os << LogIO::DEBUG1 << "POINTING has " << nAddedCols
    8236           0 :              << " optional columns." << LogIO::POST;
    8237             :         
    8238             :           // W = Works, DW = Doesn't Work
    8239             :           //DW          msOut_p.pointing() = mssel_p.pointing();        
    8240             :           //DW          //TableCopy::copyInfo(newPoint, oldPoint);
    8241             :           //W   TableColumn newTC(newPoint, "DIRECTION");
    8242             :           //W   const ScalarColumn<MDirection> oldTC(oldPoint, "DIRECTION");
    8243             :           //W   const TableColumn oldTC(oldPoint, "DIRECTION");
    8244             :           //W   newTC.rwKeywordSet() = oldTC.keywordSet();
    8245             : 
    8246           0 :           const MSPointingColumns oldPCs(oldPoint);
    8247           0 :           MSPointingColumns newPCs(newPoint);
    8248           0 :           newPCs.setEpochRef(MEpoch::castType(oldPCs.timeMeas().getMeasRef().getType()));
    8249           0 :           newPCs.setDirectionRef(MDirection::castType(oldPCs.directionMeasCol().getMeasRef().getType()));
    8250           0 :           newPCs.setEncoderDirectionRef(MDirection::castType(oldPCs.encoderMeas().getMeasRef().getType()));
    8251             : 
    8252           0 :           const ScalarColumn<Int>& antIds  = oldPCs.antennaId();
    8253           0 :           const ScalarColumn<Double>& time = oldPCs.time();
    8254           0 :           ScalarColumn<Int>&       outants = newPCs.antennaId();
    8255             : 
    8256           0 :           uInt nTRanges = selTimeRanges_p.ncolumn();
    8257             : 
    8258           0 :           uInt outRow = 0;
    8259           0 :           Int maxSelAntp1 = antNewIndex_p.nelements();  // Int for comparison
    8260             :                                                         // with newAntInd.
    8261           0 :           for (uInt inRow = 0; inRow < antIds.nrow(); ++inRow){
    8262           0 :             Int newAntInd = antIds(inRow);
    8263           0 :             if(antennaSel_p)
    8264           0 :               newAntInd = newAntInd < maxSelAntp1 ? antNewIndex_p[newAntInd] : -1;
    8265           0 :             Double t = time(inRow);
    8266             :             
    8267           0 :             if(newAntInd > -1){
    8268           0 :               Bool matchT = false;
    8269           0 :               for(uInt tr = 0; tr < nTRanges; ++tr){
    8270           0 :                 if(t >= selTimeRanges_p(0, tr) && t <= selTimeRanges_p(1, tr)){
    8271           0 :                   matchT = true;
    8272           0 :                   break;
    8273             :                 }
    8274             :               }
    8275             :               
    8276           0 :               if(matchT){
    8277           0 :                 TableCopy::copyRows(newPoint, oldPoint, outRow, inRow, 1, false);
    8278           0 :                 outants.put(outRow, newAntInd);
    8279           0 :                 ++outRow;
    8280             :               }
    8281             :             }
    8282             :           }
    8283           0 :         }
    8284             :       }
    8285             :     }
    8286             :     else
    8287           0 :       setupNewPointing();       // Make an empty stub for MSColumns.
    8288             : 
    8289           0 :     return true;
    8290           0 :   }
    8291             : 
    8292           0 : void SubMS::setupNewPointing()
    8293             : {
    8294           0 :   SetupNewTable pointingSetup(msOut_p.pointingTableName(),
    8295           0 :                               MSPointing::requiredTableDesc(), Table::New);
    8296             :   // POINTING can be large, set some sensible defaults for storageMgrs
    8297           0 :   IncrementalStMan ismPointing ("ISMPointing");
    8298           0 :   StandardStMan ssmPointing("SSMPointing", 32768);
    8299           0 :   pointingSetup.bindAll(ismPointing, true);
    8300           0 :   pointingSetup.bindColumn(MSPointing::columnName(MSPointing::DIRECTION),
    8301             :                            ssmPointing);
    8302           0 :   pointingSetup.bindColumn(MSPointing::columnName(MSPointing::TARGET),
    8303             :                            ssmPointing);
    8304           0 :   pointingSetup.bindColumn(MSPointing::columnName(MSPointing::TIME),
    8305             :                            ssmPointing);
    8306           0 :   msOut_p.rwKeywordSet().defineTable(MS::keywordName(MS::POINTING),
    8307           0 :                                      Table(pointingSetup));
    8308           0 :   msOut_p.initRefs();
    8309           0 : }
    8310             :   
    8311           0 :   Bool SubMS::copyWeather(){
    8312             :     // Weather is allowed to not exist.
    8313           0 :     if(Table::isReadable(mssel_p.weatherTableName())){
    8314           0 :       const MSWeather& oldWeath = mssel_p.weather();
    8315             : 
    8316           0 :       if(oldWeath.nrow() > 0){
    8317             :         // Add a WEATHER subtable to msOut_p with 0 rows for now.
    8318           0 :         Table::TableOption option = Table::New;
    8319           0 :         TableDesc weatherTD = MSWeather::requiredTableDesc();
    8320           0 :         SetupNewTable weatherSetup(msOut_p.weatherTableName(), weatherTD,
    8321           0 :                                    option);
    8322           0 :         msOut_p.rwKeywordSet().defineTable(MS::keywordName(MS::WEATHER),
    8323           0 :                                            Table(weatherSetup,0));
    8324             :         // update the references to the subtable keywords
    8325           0 :         msOut_p.initRefs();
    8326             : 
    8327           0 :         MSWeather& newWeath = msOut_p.weather();  // Could be declared as
    8328             :                                                   // Table&
    8329             : 
    8330           0 :         LogIO os(LogOrigin("SubMS", "copyWeather()"));
    8331             : 
    8332           0 :         uInt nAddedCols = addOptionalColumns(oldWeath, newWeath, true);
    8333             :         os << LogIO::DEBUG1 << "WEATHER has " << nAddedCols
    8334           0 :            << " optional columns." << LogIO::POST;
    8335             :         
    8336           0 :         const MSWeatherColumns oldWCs(oldWeath);
    8337           0 :         MSWeatherColumns newWCs(newWeath);
    8338           0 :         newWCs.setEpochRef(MEpoch::castType(oldWCs.timeMeas().getMeasRef().getType()));
    8339             :         
    8340           0 :         if(!antennaSel_p){
    8341           0 :           TableCopy::copyRows(newWeath, oldWeath);
    8342             :         }
    8343             :         else{
    8344           0 :           const Vector<Int>& antIds(oldWCs.antennaId().getColumn());
    8345           0 :           ScalarColumn<Int>& outants = newWCs.antennaId();
    8346             : 
    8347           0 :           uInt selRow = 0;
    8348           0 :           Int maxSelAntp1 = antNewIndex_p.nelements();
    8349             : 
    8350           0 :           for(uInt k = 0; k < antIds.nelements(); ++k){          
    8351           0 :             if(antIds[k] > -1){         // Weather station is on antenna?
    8352           0 :               if(antIds[k] < maxSelAntp1){
    8353           0 :                 Int newAntInd = antNewIndex_p[antIds[k]]; // remap ant num
    8354             :               
    8355           0 :                 if(newAntInd > -1){                       // Ant selected?
    8356           0 :                   TableCopy::copyRows(newWeath, oldWeath, selRow, k, 1);
    8357           0 :                   outants.put(selRow, newAntInd);
    8358           0 :                   ++selRow;
    8359             :                 }
    8360             :               }
    8361             :             }
    8362             :             else{
    8363           0 :               TableCopy::copyRows(newWeath, oldWeath, selRow, k, 1);
    8364           0 :               outants.put(selRow, -1);  // means valid for all antennas.
    8365           0 :               ++selRow;
    8366             :             }
    8367             :           }
    8368           0 :         }
    8369           0 :       }
    8370             :     }
    8371           0 :     return true;
    8372             :   }
    8373             : 
    8374           0 : Bool SubMS::copySyscal()
    8375             : {
    8376             :   // SYSCAL is allowed to not exist.
    8377           0 :   if(Table::isReadable(mssel_p.sysCalTableName())){
    8378           0 :     const MSSysCal& oldSysc = mssel_p.sysCal();
    8379             : 
    8380           0 :     if(oldSysc.nrow() > 0){
    8381             :       // Add a SYSCAL subtable to msOut_p with 0 rows for now.
    8382           0 :       Table::TableOption option = Table::New;
    8383           0 :       TableDesc sysCalTD = MSSysCal::requiredTableDesc();
    8384           0 :       SetupNewTable sysCalSetup(msOut_p.sysCalTableName(), sysCalTD,
    8385           0 :                                 option);
    8386           0 :       msOut_p.rwKeywordSet().defineTable(MS::keywordName(MS::SYSCAL),
    8387           0 :                                          Table(sysCalSetup, 0));
    8388             :       // update the references to the subtable keywords
    8389           0 :       msOut_p.initRefs();
    8390             : 
    8391             :       // Could be declared as Table&.
    8392           0 :       MSSysCal& newSysc = msOut_p.sysCal();
    8393             : 
    8394           0 :       LogIO os(LogOrigin("SubMS", "copySysCal()"));
    8395             : 
    8396           0 :       uInt nAddedCols = addOptionalColumns(oldSysc, newSysc, true);
    8397             :       os << LogIO::DEBUG1 << "SYSCAL has " << nAddedCols
    8398           0 :          << " optional columns." << LogIO::POST;
    8399             :         
    8400           0 :       const MSSysCalColumns incols(oldSysc);
    8401           0 :       MSSysCalColumns outcols(newSysc);
    8402           0 :       outcols.setEpochRef(MEpoch::castType(incols.timeMeas().getMeasRef().getType()));
    8403             :         
    8404           0 :       if(!antennaSel_p && allEQ(spwRelabel_p, spw_p)){
    8405           0 :         TableCopy::copyRows(newSysc, oldSysc);
    8406             :       }
    8407             :       else{
    8408           0 :         const Vector<Int>& antIds = incols.antennaId().getColumn();
    8409           0 :         const Vector<Int>& spwIds = incols.spectralWindowId().getColumn();
    8410             : 
    8411             :         // Copy selected rows.
    8412           0 :         uInt totNSyscals = antIds.nelements();
    8413           0 :         uInt totalSelSyscals = 0;
    8414           0 :         Int maxSelAntp1 = antNewIndex_p.nelements(); // Int for comparison with antIds.
    8415           0 :         for(uInt k = 0; k < totNSyscals; ++k){
    8416             :           // antenna must be selected, and spwId must be -1 (any) or selected.
    8417           0 :           if(antIds[k] < maxSelAntp1 && antNewIndex_p[antIds[k]] > -1 &&
    8418           0 :              (spwIds[k] < 0 || spwRelabel_p[spwIds[k]] > -1)){
    8419             :             //                  outtab   intab    outrow       inrow nrows
    8420           0 :             TableCopy::copyRows(newSysc, oldSysc, totalSelSyscals, k, 1, false);
    8421           0 :             ++totalSelSyscals;
    8422             :           }
    8423             :         }
    8424             : 
    8425             :         // Remap antenna and spw #s.
    8426           0 :         ScalarColumn<Int>& antCol = outcols.antennaId();
    8427           0 :         ScalarColumn<Int>& spwCol = outcols.spectralWindowId();
    8428             : 
    8429           0 :         for(uInt k = 0; k < totalSelSyscals; ++k){
    8430           0 :           antCol.put(k, antNewIndex_p[antCol(k)]);
    8431           0 :           if(spwCol(k) > -1)
    8432           0 :             spwCol.put(k, spwRelabel_p[spwCol(k)]);
    8433             :         }
    8434           0 :       }
    8435           0 :     }
    8436             :   }
    8437           0 :   return true;
    8438             : }
    8439             : 
    8440           0 : void SubMS::copySubtable(const String& tabName, const Table& inTab,
    8441             :                          const Bool doFilter)
    8442             : {
    8443           0 :   String outName(msOut_p.tableName() + '/' + tabName);
    8444             : 
    8445           0 :   if(PlainTable::tableCache()(outName))
    8446           0 :     PlainTable::tableCache().remove(outName);
    8447           0 :   inTab.deepCopy(outName, Table::New, false, Table::AipsrcEndian,
    8448             :                  doFilter);
    8449           0 :   Table outTab(outName, Table::Update);
    8450           0 :   msOut_p.rwKeywordSet().defineTable(tabName, outTab);
    8451           0 :   msOut_p.initRefs();
    8452           0 : }
    8453             : 
    8454           0 : Bool SubMS::filterOptSubtable(const String& subtabname)
    8455             : {
    8456           0 :   LogIO os(LogOrigin("SubMS", "filterOptSubtable()"));
    8457             : 
    8458             :   // subtabname is allowed to not exist.  Use ms_p instead of mssel_p, because
    8459             :   // the latter has a random name which does NOT necessarily lead to
    8460             :   // subtabname.  (And if selection took care of subtables, we probably
    8461             :   // wouldn't have to do it here.)
    8462           0 :   if(Table::isReadable(ms_p.tableName() + '/' + subtabname)){
    8463           0 :     const Table intab(ms_p.tableName() + '/' + subtabname);
    8464             : 
    8465           0 :     if(intab.nrow() > 0){
    8466             :       // Add feed if selecting by it is ever added.
    8467           0 :       Bool doFilter = antennaSel_p || !allEQ(spwRelabel_p, spw_p);
    8468             : 
    8469           0 :       copySubtable(subtabname, intab, doFilter);
    8470             : 
    8471           0 :       if(doFilter){
    8472             :         // At this point msOut_p has subtab with 0 rows.
    8473           0 :         Table outtab(msOut_p.tableName() + '/' + subtabname, Table::Update);
    8474             : 
    8475           0 :         ScalarColumn<Int> inAntIdCol(intab, "ANTENNA_ID");         // + FEED_ID if it
    8476           0 :         ScalarColumn<Int> inSpwIdCol(intab, "SPECTRAL_WINDOW_ID"); // ever changed.
    8477           0 :         const Vector<Int>& antIds = inAntIdCol.getColumn();
    8478           0 :         const Vector<Int>& spwIds = inSpwIdCol.getColumn();
    8479             : 
    8480             :         // Copy selected rows.
    8481           0 :         uInt totNOuttabs = antIds.nelements();
    8482           0 :         uInt totalSelOuttabs = 0;
    8483           0 :         Int maxSelAntp1 = antNewIndex_p.nelements(); // Int for comparison with
    8484             :                                                      // antIds.
    8485           0 :         Bool haveRemappingProblem = false;
    8486           0 :         for(uInt inrow = 0; inrow < totNOuttabs; ++inrow){
    8487             :           // antenna must be selected, and spwId must be -1 (any) or selected.
    8488             :           // Extra care must be taken because for a while MSes had CALDEVICE
    8489             :           // and SYSPOWER, but the ANTENNA_ID and SPECTRAL_WINDOW_ID of those
    8490             :           // subtables were not being remapped by split.
    8491           0 :           if(antIds[inrow] < maxSelAntp1 && antNewIndex_p[antIds[inrow]] > -1){
    8492             :             
    8493           0 :             if(spwIds[inrow] < 0 || (spwIds[inrow] <
    8494           0 :                                      static_cast<Int>(spwRelabel_p.nelements()) &&
    8495           0 :                                      spwRelabel_p[spwIds[inrow]] > -1)){
    8496           0 :               TableCopy::copyRows(outtab, intab, totalSelOuttabs, inrow, 1, false);
    8497           0 :               ++totalSelOuttabs;
    8498             :             }
    8499             :             // Ideally we'd like to catch antenna errors too.  They are
    8500             :             // avoided, but because of the way antNewIndex_p is made, 
    8501             :             // antIds[inrow] >= maxSelAntp1
    8502             :             // is not necessarily an error.  It's not even possible to catch
    8503             :             // all the spw errors, so we settle for catching the ones we can
    8504             :             // and reliably avoiding segfaults.
    8505           0 :             else if(spwIds[inrow] >= static_cast<Int>(spwRelabel_p.nelements()))
    8506           0 :               haveRemappingProblem = true;
    8507             :           }
    8508             :         }
    8509             : 
    8510           0 :         if(haveRemappingProblem)
    8511             :           os << LogIO::WARN
    8512             :              << "At least one row of " << intab.tableName()
    8513             :              << " has an antenna or spw mismatch.\n"
    8514             :              << "(Presumably from an older split, sorry.)\n"
    8515             :              << "If " << subtabname
    8516             :              << " is important, it should be fixed with tb or browsetable,\n"
    8517             :              << "or by redoing the split that made " << ms_p.tableName()
    8518             :              << " (check its history)."
    8519           0 :              << LogIO::POST;
    8520             : 
    8521             :         // Remap antenna and spw #s.
    8522           0 :         ScalarColumn<Int> outAntCol(outtab, "ANTENNA_ID");
    8523           0 :         ScalarColumn<Int> outSpwCol(outtab, "SPECTRAL_WINDOW_ID");
    8524             : 
    8525           0 :         for(uInt k = 0; k < totalSelOuttabs; ++k){
    8526           0 :           outAntCol.put(k, antNewIndex_p[outAntCol(k)]);
    8527           0 :           if(outSpwCol(k) > -1)
    8528           0 :             outSpwCol.put(k, spwRelabel_p[outSpwCol(k)]);
    8529             :         }
    8530           0 :       }
    8531             :     }
    8532           0 :   }
    8533           0 :   return true;
    8534           0 : }
    8535             : 
    8536             : // writeDiffSpwShape() was the VisIter-using channel averager, which sounds
    8537             : // great, but:
    8538             : // 0. If VisIter's sort order is different from the one in the MS, then if you
    8539             : //    write _any_ columns using VisIter-fetched data, then you must write _all_ of
    8540             : //    them.  This is crucially important for channel averaging in split: normally
    8541             : //    only *DATA, FLAG*, WEIGHT, and SIGMA would have to be updated, but if using
    8542             : //    VisIter, the ANTENNA*, FEED*, STATE_ID, etc. have to be updated too because
    8543             : //    they could be rearranged.
    8544             : //
    8545             : //    There does not appear to be an easy way of getting the MS's starting sort
    8546             : //    order, i.e. AFAIK it is not written into a header.
    8547             : //
    8548             : // 1. If FLOAT_DATA is present, VisIter will ignore DATA.  (G. Moellenbrock,
    8549             : //    =casa-staff, 1/15/2010)
    8550             : 
    8551           0 : Bool SubMS::doChannelMods(const Vector<MS::PredefinedColumns>& datacols)
    8552             : {
    8553           0 :   LogIO os(LogOrigin("SubMS", "doChannelMods()"));
    8554             : 
    8555           0 :   Vector<MS::PredefinedColumns> cmplxColLabels;
    8556           0 :   const Bool doFloat = sepFloat(datacols, cmplxColLabels);
    8557           0 :   const uInt nCmplx = cmplxColLabels.nelements();
    8558           0 :   if(doFloat && nCmplx > 0)           // 2010-12-14
    8559             :     os << LogIO::WARN
    8560             :        << "Using VisIter to average both FLOAT_DATA and another DATA column is extremely experimental."
    8561           0 :        << LogIO::POST;
    8562             : 
    8563             :   //ArrayColumn<Complex> outCmplxCols[nCmplx];
    8564             :   //outDataColMap(outCmplxCols, nCmplx, cmplxColLabels);
    8565           0 :   const Bool writeToData = mustConvertToData(nCmplx, cmplxColLabels);
    8566             : 
    8567             :   // Is CORRECTED_DATA being moved to DATA?
    8568           0 :   Bool fromCorrToData = writeToData && nCmplx == 1;
    8569           0 :   if(fromCorrToData)
    8570           0 :     fromCorrToData = cmplxColLabels[0] == MS::CORRECTED_DATA;
    8571             :   
    8572           0 :   Vector<Int> spwindex(max(spw_p) + 1);
    8573           0 :   spwindex.set(-1);
    8574           0 :   for(uInt k = 0; k < spw_p.nelements(); ++k)
    8575           0 :     spwindex[spw_p[k]] = k;
    8576             : 
    8577           0 :   Block<Int> columns;
    8578             :   // include scan and state iteration, for more optimal iteration
    8579           0 :   columns.resize(6);
    8580           0 :   columns[0]=MS::ARRAY_ID;
    8581           0 :   columns[1]=MS::SCAN_NUMBER;
    8582           0 :   columns[2]=MS::STATE_ID;
    8583           0 :   columns[3]=MS::FIELD_ID;
    8584           0 :   columns[4]=MS::DATA_DESC_ID;
    8585           0 :   columns[5]=MS::TIME;
    8586             : 
    8587             : #ifdef COPYTIMER
    8588             :   Timer timer;
    8589             :   timer.mark();
    8590             : 
    8591             :   Vector<Int> inscan, outscan;
    8592             : #endif  
    8593             : 
    8594           0 :   ROVisIterator viIn(mssel_p, columns, 0.0);
    8595             :   //ROVisibilityIterator viIn(mssel_p, columns, 0.0);
    8596           0 :   VisIter viOut(msOut_p,columns,0.0);
    8597             : 
    8598           0 :   viIn.setRowBlocking(1000);
    8599           0 :   viOut.setRowBlocking(1000);
    8600             :   //viIn.slurp();
    8601             :   //cerr << "Finished slurping." << endl;
    8602             : 
    8603             :   // Translate chanSlices_p into the form vb.channelAve() wants.
    8604           0 :   Vector<Matrix<Int> > chanAveBounds;
    8605           0 :   viIn.slicesToMatrices(chanAveBounds, chanSlices_p, widths_p);
    8606             : 
    8607             :   // // If we don't want to skip every (width - 1) out of width channels,
    8608             :   // // the increments in chanSlices_p must be set to 1.
    8609             :   // if(averageChannel_p){
    8610             :   //   for(uInt spwind = 0; spwind < chanSlices_p.nelements(); ++spwind){
    8611             :   //     Vector<Slice>& spwsls = chanSlices_p[spwind];
    8612             : 
    8613             :   //     for(uInt slnum = 0; slnum < spwsls.nelements(); ++slnum){
    8614             :   //       Slice& sl = spwsls[slnum];
    8615             : 
    8616             :   //       spwsls[slnum] = Slice(sl.start(), sl.length());
    8617             :   //     }
    8618             :   //   }
    8619             :   // }
    8620             : 
    8621             :   // Apply selection
    8622             :   // for(uInt spwind = 0; spwind < spw_p.nelements(); ++spwind)
    8623             :   //   viOut.selectChannel(1, chanStart_p[spwind], nchan_p[spwind],
    8624             :   //                       chanStep_p[spwind], spw_p[spwind]);
    8625           0 :   viIn.selectChannel(chanSlices_p);     // ROVisIterator
    8626           0 :   viIn.selectCorrelation(corrSlices_p);
    8627             :   
    8628           0 :   viIn.originChunks();                                // Makes me feel better.
    8629             : 
    8630           0 :   const Bool doSpWeight = viIn.existsWeightSpectrum();
    8631             : 
    8632           0 :   Bool doFC = existsFlagCategory();
    8633           0 :   uInt rowsdone = 0;
    8634           0 :   ProgressMeter meter(0.0, mssel_p.nrow() * 1.0, "split", "rows averaged", "", "",
    8635           0 :                       true, 1);
    8636           0 :   Cube<Complex> vis;
    8637           0 :   Cube<Float> floatvis;
    8638           0 :   VisBuffer vb(viIn);
    8639           0 :   Matrix<Float> wtmat;
    8640             : 
    8641           0 :   for(viOut.originChunks(), viIn.originChunks();
    8642           0 :       viOut.moreChunks(), viIn.moreChunks();
    8643           0 :       viOut.nextChunk(), viIn.nextChunk()){
    8644           0 :     for(viIn.origin(),viOut.origin(); viIn.more(),viOut.more();
    8645           0 :         ++viIn, ++viOut){
    8646           0 :       uInt rowsnow = vb.nRow();
    8647             : 
    8648           0 :       if(rowsnow > 0){
    8649             :         //RefRows rr(rowsdone, rowsdone + rowsnow - 1);
    8650             : 
    8651             :         // Preload the things that need to be channel averaged.
    8652           0 :         for(uInt colind = 0; colind < nCmplx; ++colind){
    8653           0 :           if(cmplxColLabels[colind] == MS::DATA)
    8654           0 :             vb.visCube();
    8655           0 :           else if(cmplxColLabels[colind] == MS::MODEL_DATA)
    8656           0 :             vb.modelVisCube();
    8657           0 :           else if(cmplxColLabels[colind] == MS::CORRECTED_DATA)
    8658           0 :             vb.correctedVisCube();
    8659             :         }
    8660           0 :         if(doFloat)
    8661           0 :           vb.floatDataCube();
    8662             : 
    8663             :         // The flags and weights are already loaded by this point, UNLESS the
    8664             :         // row flag was true for all the rows.  Make sure they're loaded, or
    8665             :         // they could end up with the wrong shape.
    8666           0 :         vb.flagCube();
    8667             :         // if(viIn.existsWeightSpectrum())
    8668             :         //   vb.weightSpectrum();
    8669             :         // vb.weightMat();
    8670           0 :         if(doFC)
    8671           0 :           vb.flagCategory();
    8672             :       
    8673           0 :         vb.channelAve(chanAveBounds[viIn.spectralWindow()],false);
    8674             : 
    8675           0 :         if(nCmplx > 0){
    8676           0 :           if(vb.flagCube().shape() !=
    8677           0 :              vb.dataCube(cmplxColLabels[0]).shape())
    8678           0 :             throw(AipsError("Shape error after channel averaging!"));
    8679             :         }
    8680           0 :         else if(doFloat){
    8681           0 :           if(vb.flagCube().shape() != vb.floatDataCube().shape())
    8682           0 :             throw(AipsError("Shape error after channel averaging!"));
    8683             :         }
    8684             :         // else  we aren't doing anything, it seems.
    8685             : 
    8686             :         // Write the output.
    8687           0 :         for(uInt colind = 0; colind < nCmplx; ++colind){
    8688           0 :           if(cmplxColLabels[colind] == MS::DATA)
    8689           0 :             viOut.setVis(vb.visCube(), VisibilityIterator::Observed);
    8690           0 :           else if(cmplxColLabels[colind] == MS::MODEL_DATA)
    8691           0 :             viOut.setVis(vb.modelVisCube(),
    8692             :                          writeToData ? VisibilityIterator::Observed :
    8693             :                                        VisibilityIterator::Model);
    8694           0 :           else if(cmplxColLabels[colind] == MS::CORRECTED_DATA)
    8695           0 :             viOut.setVis(vb.correctedVisCube(),
    8696             :                          writeToData ? VisibilityIterator::Observed :
    8697             :                                        VisibilityIterator::Corrected);
    8698             :         }
    8699             :         //if(doFloat)
    8700             :         //  viOut.setFloatData(vb.floatDataCube());    TBD!
    8701           0 :         viOut.setFlag(vb.flagCube());
    8702           0 :         if(doFC)
    8703           0 :           viOut.setFlagCategory(vb.flagCategory());
    8704             : 
    8705             : 
    8706           0 :         if(doSpWeight)
    8707           0 :           viOut.setWeightSpectrum(vb.weightSpectrum());
    8708             : 
    8709             : 
    8710           0 :         if(fromCorrToData) {
    8711           0 :           wtmat.reference(vb.weightMat());
    8712           0 :           viOut.setWeightMat(wtmat);
    8713           0 :           arrayTransformInPlace(wtmat, subms::wtToSigma);   // for corrected weights.
    8714           0 :           viOut.setSigmaMat(wtmat);
    8715             :         }
    8716             :         else {
    8717           0 :           wtmat.reference(vb.sigmaMat());           // Yes, I'm reusing wtmat.
    8718           0 :           viOut.setSigmaMat(wtmat);
    8719           0 :           arrayTransformInPlace(wtmat, subms::sigToWeight);   // for corrected weights.
    8720           0 :           viOut.setWeightMat(wtmat);
    8721             :         }
    8722             : 
    8723           0 :         rowsdone += rowsnow;
    8724             :       }
    8725             :     }
    8726           0 :     meter.update(rowsdone);
    8727             :   }   // End of for(viIn.originChunks(); viIn.moreChunks(); viIn.nextChunk())
    8728           0 :   os << LogIO::NORMAL << "Data binned." << LogIO::POST;
    8729             : 
    8730             :   //const ColumnDescSet& cds = mssel_p.tableDesc().columnDescSet();
    8731             :   //const ColumnDesc& cdesc = cds[MS::columnName(MS::DATA)];
    8732             :   //ROTiledStManAccessor tacc(mssel_p, cdesc.dataManagerGroup());
    8733             :   //tacc.showCacheStatistics(cerr);  // A 99.x% hit rate is good.  0% is bad.
    8734             : 
    8735             :   os << LogIO::DEBUG1 // helpdesk ticket in from Oleg Smirnov (ODU-232630)
    8736           0 :      << "Post binning memory: " << Memory::allocatedMemoryInBytes() / (1024.0 * 1024.0) << " MB"
    8737           0 :      << LogIO::POST;
    8738             : 
    8739           0 :   return true;
    8740           0 : }
    8741             : 
    8742             : // // Sets mapper to the distinct values of mscol, in increasing order.
    8743             : // // A static method that is used by SubMS, but doesn't necessarily have to go
    8744             : // // with it.  It may belong in something more MSColumnsish.
    8745             : // void SubMS::make_map(const Vector<Int>& mscol, Vector<Int>& mapper)
    8746             : // {
    8747             : //   std::set<Int> valSet;
    8748             :   
    8749             : //   for(Int i = mscol.nelements(); i--;)  // Strange, but slightly more
    8750             : //     valSet.insert(mscol[i]);            // efficient than going forward.
    8751             : //   mapper.resize(valSet.size());
    8752             : 
    8753             : //   uInt remaval = 0;
    8754             : //   for(std::set<Int>::const_iterator vs_iter = valSet.begin();
    8755             : //       vs_iter != valSet.end(); ++vs_iter){
    8756             : //     mapper[remaval] = *vs_iter;
    8757             : //     ++remaval;
    8758             : //   }
    8759             : // }
    8760             : 
    8761           0 : void SubMS::make_map(std::map<Int, Int>& mapper, const Vector<Int>& inv)
    8762             : {
    8763           0 :   std::set<Int> valSet;
    8764             :   
    8765           0 :   for(Int i = inv.nelements(); i--;)  // Strange, but slightly more
    8766           0 :     valSet.insert(inv[i]);         // efficient than going forward.
    8767             : 
    8768           0 :   uInt remaval = 0;
    8769           0 :   for(std::set<Int>::const_iterator vs_iter = valSet.begin();
    8770           0 :       vs_iter != valSet.end(); ++vs_iter){
    8771           0 :     mapper[*vs_iter] = remaval;
    8772           0 :     ++remaval;
    8773             :   }
    8774           0 : }
    8775             : 
    8776           0 : void SubMS::make_map2(std::map<Int, Int>& mapper, const Vector<Int>& inv)
    8777             : {
    8778           0 :   LogIO os(LogOrigin("SubMS", "make_map2()"));
    8779             :   // This method assumes the int vector contains mapping scheme already
    8780             :   // if inv[i] is not selected one => -1 so should be skipped
    8781             :   // ith vector element is mapped value of index i
    8782             :   // Created for remapping of field Ids with fieldRelable_p (TT 2012.07.27)
    8783           0 :   for(Vector<Int>::const_iterator vs_iter = inv.begin();
    8784           0 :      vs_iter != inv.end(); ++vs_iter){
    8785           0 :     if (*vs_iter!=-1) {
    8786           0 :       Int dist=std::distance(inv.begin(),vs_iter);
    8787           0 :       mapper.insert(std::make_pair(dist, *vs_iter));
    8788             :       os << LogIO::DEBUG1
    8789           0 :          << " *vs_iter="<< *vs_iter
    8790             :          << " index="<< dist 
    8791           0 :          << LogIO::POST;
    8792             :     }
    8793           0 :   }
    8794           0 : }
    8795             :   //
    8796             : 
    8797           0 : void SubMS::remap(Vector<Int>& col, const Vector<Int>& mapper)
    8798             : {
    8799           0 :   if(mapper.nelements() > 0)
    8800           0 :     for(Int row = col.nelements(); row--;)
    8801           0 :       col[row] = mapper[col[row]];
    8802           0 : }
    8803             : 
    8804           0 : void SubMS::remap(Vector<Int>& col, const std::map<Int, Int>& mapper)
    8805             : {
    8806           0 :   if(mapper.size() > 0)
    8807           0 :     for(Int row = col.nelements(); row--;)
    8808           0 :       col[row] = mapper.find(col[row])->second;
    8809           0 : }
    8810             : 
    8811             : // Returns rv s.t. mapper[rv] == ov, assuming that mapper[i + 1] >= mapper[i]
    8812             : // for 0 <= i < mapper.nelements() - 1.
    8813             : // i can be supplied as the first guess.
    8814           0 : uInt SubMS::remapped(const Int ov, const Vector<Int>& mapper, uInt i=0)
    8815             : {
    8816           0 :   uInt nvals = mapper.nelements();
    8817           0 :   uInt nvalsm1 = nvals - 1;
    8818             : 
    8819           0 :   if(i >= nvals)     // Second guess the guess.
    8820           0 :     i = nvals >> 1;
    8821             :   
    8822             :   // Do a widening search for the brackets, since i is probably close to the
    8823             :   // answer.
    8824           0 :   if(mapper[i] == ov)
    8825           0 :     return i;
    8826             :   
    8827           0 :   uInt inc = 1;
    8828           0 :   uInt lb = i;
    8829             :   uInt ub;
    8830           0 :   if(ov > mapper[lb]){       // Hunt up.
    8831           0 :     if(lb == nvalsm1)
    8832           0 :       return lb;
    8833           0 :     ub = lb + 1;
    8834           0 :     while(ov >= mapper[ub]){
    8835           0 :       lb = ub;
    8836           0 :       inc += inc;
    8837           0 :       ub = lb + inc;
    8838           0 :       if(ub > nvalsm1){
    8839           0 :         ub = nvals;
    8840           0 :         break;
    8841             :       }
    8842             :     }
    8843             :   }
    8844             :   else{                 // Hunt down.
    8845           0 :     ub = lb;
    8846           0 :     --lb;
    8847           0 :     while(ov < mapper[lb]){
    8848           0 :       ub = lb;
    8849           0 :       inc += inc;
    8850           0 :       if(inc >= ub){
    8851           0 :         lb = 0;
    8852           0 :         break;
    8853             :       }
    8854             :       else
    8855           0 :         lb = ub - inc;
    8856             :     }
    8857             :   }
    8858             :         
    8859           0 :   i = lb + ((ub - lb) >> 1);                      // (lb + ub) / 2 might overflow. 
    8860           0 :   while(mapper[i] != ov && ub - lb > 1){
    8861           0 :     if(mapper[i] < ov)
    8862           0 :       lb = i;
    8863             :     else
    8864           0 :       ub = i;
    8865           0 :     i = lb + ((ub - lb) >> 1);
    8866             :   }
    8867           0 :   return i;  
    8868             : }
    8869             : 
    8870           0 : uInt SubMS::fillAntIndexer(std::map<Int, Int>& antIndexer, const MSColumns *msc)
    8871             : {
    8872           0 :   const Vector<Int>& ant1 = msc->antenna1().getColumn();
    8873           0 :   const Vector<Int>& ant2 = msc->antenna2().getColumn();
    8874             : 
    8875           0 :   std::set<Int> ants;
    8876           0 :   for(Int i = ant1.nelements(); i--;){   // Strange, but slightly more
    8877           0 :     ants.insert(ant1[i]);                // efficient than going forward.
    8878           0 :     ants.insert(ant2[i]);
    8879             :   }
    8880           0 :   uInt nant = ants.size();
    8881             :   
    8882           0 :   Vector<Int> selAnt(nant);
    8883           0 :   Int remaval = 0;
    8884           0 :   for(std::set<Int>::const_iterator ant_iter = ants.begin();
    8885           0 :       ant_iter != ants.end(); ++ant_iter){
    8886           0 :     selAnt[remaval] = *ant_iter;
    8887           0 :     ++remaval;
    8888             :   }
    8889             :     
    8890           0 :   for(uInt j = 0; j < nant; ++j)
    8891           0 :     antIndexer[selAnt[j]] = static_cast<Int>(j);
    8892           0 :   return nant;
    8893           0 : }
    8894             : 
    8895           0 : const ArrayColumn<Complex>& SubMS::right_column(const MSColumns *msclala,
    8896             :                                                 const MS::PredefinedColumns col)
    8897             : {
    8898           0 :   if(col == MS::DATA)
    8899           0 :     return msclala->data();
    8900           0 :   else if(col == MS::MODEL_DATA)
    8901           0 :     return msclala->modelData();
    8902             :   //  else if(col == MS::FLOAT_DATA) // Not complex.
    8903             :   //  return msclala->floatData();
    8904           0 :   else if(col == MS::LAG_DATA)
    8905           0 :     return msclala->lagData();
    8906             :   else                                // The honored-by-time-if-nothing-else
    8907           0 :     return msclala->correctedData();  // default.
    8908             : }
    8909             : 
    8910           0 : ArrayColumn<Complex>& SubMS::right_column(MSColumns *msclala,
    8911             :                                           const MS::PredefinedColumns col,
    8912             :                                           const Bool writeToDataCol)
    8913             : {
    8914           0 :   if(writeToDataCol || col == MS::DATA)
    8915           0 :     return msclala->data();
    8916           0 :   else if(col == MS::MODEL_DATA)
    8917           0 :     return msclala->modelData();
    8918             :   //  else if(col == MS::FLOAT_DATA) // Not complex.
    8919             :   //  return msclala->floatData();
    8920           0 :   else if(col == MS::LAG_DATA)
    8921           0 :     return msclala->lagData();
    8922             :   else                                // The honored-by-time-if-nothing-else
    8923           0 :     return msclala->correctedData();  // default.
    8924             : }
    8925             : 
    8926           0 : Bool SubMS::sepFloat(const Vector<MS::PredefinedColumns>& anyDataCols,
    8927             :                      Vector<MS::PredefinedColumns>& complexDataCols)
    8928             : {
    8929             :   // Determine whether FLOAT_DATA is in anyDataCols[], and fill
    8930             :   // complexDataCols[] with the Complex members of anyDataCols[].
    8931           0 :   Bool doFloat = false;
    8932           0 :   uInt ntok = anyDataCols.nelements();
    8933             : 
    8934           0 :   complexDataCols.resize(ntok);
    8935           0 :   uInt j = 0;
    8936           0 :   for(uInt i = 0; i < ntok; ++i){
    8937           0 :     if(anyDataCols[i] != MS::FLOAT_DATA){
    8938           0 :       complexDataCols[j] = anyDataCols[i];
    8939           0 :       ++j;
    8940             :     }
    8941             :     else
    8942           0 :       doFloat = true;
    8943             :   }
    8944           0 :   if(doFloat)
    8945           0 :     complexDataCols.resize(j, true);
    8946           0 :   return doFloat;
    8947             : }
    8948             : 
    8949           0 : Bool SubMS::doTimeAver(const Vector<MS::PredefinedColumns>& dataColNames,
    8950             :                        const VBRemapper& remapper)
    8951             : {
    8952           0 :   LogIO os(LogOrigin("SubMS", "doTimeAver()"));
    8953             : 
    8954             :   //No channel averaging with time averaging ... it's better this way, but
    8955             :   //maybe that should be revisited with VisibilityIterator.
    8956           0 :   if(chanStep_p[0] > 1){
    8957           0 :     throw(AipsError("Simultaneous time and channel averaging is not handled."));
    8958             :     return false;
    8959             :   }
    8960             : 
    8961             :   os << LogIO::DEBUG1 // helpdesk ticket from Oleg Smirnov (ODU-232630)
    8962             :      << "Before msOut_p.addRow(): "
    8963           0 :      << Memory::allocatedMemoryInBytes() / (1024.0 * 1024.0) << " MB"
    8964           0 :      << LogIO::POST;
    8965             : 
    8966           0 :   Vector<MS::PredefinedColumns> cmplxColLabels;
    8967           0 :   const Bool doFloat = sepFloat(dataColNames, cmplxColLabels);
    8968           0 :   const uInt nCmplx = cmplxColLabels.nelements();
    8969           0 :   if(doFloat && cmplxColLabels.nelements() > 0)           // 2010-12-14
    8970             :     os << LogIO::WARN
    8971             :        << "Using VisibilityIterator to average both FLOAT_DATA and another DATA column is extremely experimental."
    8972           0 :        << LogIO::POST;
    8973             : 
    8974             :   // Is CORRECTED_DATA being moved to DATA?
    8975           0 :   Bool fromCorrToData = nCmplx == 1 && cmplxColLabels[0] == MS::CORRECTED_DATA;
    8976             : 
    8977           0 :   ArrayColumn<Complex> *outCmplxCols = new ArrayColumn<Complex>[nCmplx];
    8978           0 :   getDataColMap(outCmplxCols, nCmplx, cmplxColLabels);
    8979             : 
    8980             :   // We may need to watch for chunks (timebins) that should be split because of
    8981             :   // changes in scan, etc. (CAS-2401).  The old split way would have
    8982             :   // temporarily shortened timeBin, but vi.setInterval() does not work without
    8983             :   // calling vi.originChunks(), so that approach does not work with
    8984             :   // VisibilityIterator.  Instead, get VisibilityIterator's sort (which also
    8985             :   // controls how the chunks are split) to do the work.
    8986           0 :   Block<Int> sort;
    8987           0 :   if(!setSortOrder(sort, "spw", false))
    8988             :     os << LogIO::WARN
    8989             :        << "The request to combine spws while time averaging is being ignored."
    8990           0 :        << LogIO::POST;
    8991             : 
    8992             :   // MSIter tends to produce output INTERVALs that are longer than the
    8993             :   // requested interval length, by ~0.5 input integrations for a random
    8994             :   // timeBin_p.  Giving it timeBin_p - 0.5 * interval[0] removes the bias and
    8995             :   // brings it almost in line with binTimes() (which uses -0.5 *
    8996             :   // interval[bin_start]).
    8997             :   //
    8998             :   // late April 2011: MSIter removed the bias, which threw off the correction.
    8999             :   // October 2011: The bias seems to be back, possibly because of a change in
    9000             :   //               when MSInterval's "offset" is reset.
    9001             :   //               But, then, practically, timebins can be cut short but never
    9002             :   //               lengthened by changes in scan, state, or obs ID, so it seems
    9003             :   //               better to leave the bias in!
    9004             :   //
    9005             :   //Double tbin = mscIn_p->interval()(0);
    9006             :   //tbin = timeBin_p > tbin ? timeBin_p - 0.5 * tbin : timeBin_p;
    9007           0 :   ROVisibilityIterator vi(mssel_p, sort, timeBin_p);
    9008             :   //vi.slurp();
    9009             :   //cerr << "Finished slurping." << endl;
    9010             : 
    9011             :   // Apply selection
    9012           0 :   for(uInt spwind = 0; spwind < spw_p.nelements(); ++spwind)
    9013           0 :     vi.selectChannel(1, chanStart_p[spwind], nchan_p[spwind],
    9014           0 :                      chanStep_p[spwind], spw_p[spwind]);
    9015             :   //vi.selectChannel(chanSlices_p);     // ROVisIterator
    9016             :   //vi.selectCorrelation(corrSlices_p);
    9017             : 
    9018             :   // Translate chanSlices_p into the form vb.channelAve() wants.
    9019           0 :   Vector<Matrix<Int> > chanAveBounds;
    9020           0 :   vi.slicesToMatrices(chanAveBounds, chanSlices_p, widths_p);
    9021             : 
    9022           0 :   Matrix<Float> wtmat;
    9023           0 :   const Bool doSpWeight = vi.existsWeightSpectrum();
    9024             : 
    9025             :   //os << LogIO::NORMAL2 << "outNrow = " << msOut_p.nrow() << LogIO::POST;
    9026             : 
    9027             :   // All of this ddid/spw confusion really needs cleaning up.
    9028             :   // a map from input to output
    9029             :   // DATA_DESC_ID.  (ddidmap[input_ddid] = output_ddid.  Setting ddidmap to -1
    9030             :   // for unselected ddids will help make it obvious if unexpected ddids sneak
    9031             :   // through.)  Vector<Int> ddidmap(oldDDSpwMatch_p.nelements());
    9032             :   // ddidmap.set(-1);
    9033             :   // for(uInt i = 0; i < ddidmap.nelements(); ++i){
    9034             :   //   Int oldspwid = oldDDSpwMatch_p[i];
    9035             : 
    9036             :   //   if(oldspwid > -1 && oldspwid < spwRelabel_p.nelements())
    9037             :   //     ddidmap[i] = spwRelabel_p[oldspwid];
    9038             :   // }
    9039             : 
    9040           0 :   uInt rowsdone = 0;    // Output rows, used for the RefRows.
    9041             : 
    9042           0 :   uInt ninrows = mssel_p.nrow();
    9043             :   ProgressMeter meter(0.0, ninrows * 1.0, "split", "rows averaged", "", "",
    9044           0 :                       true, 1);
    9045           0 :   uInt inrowsdone = 0;  // only for the meter.
    9046             : 
    9047           0 :   VisChunkAverager vca(dataColNames, doSpWeight, chanAveBounds);
    9048             : 
    9049           0 :   Bool doFC = existsFlagCategory();
    9050             : 
    9051             :   // Iterate through the chunks.  A timebin will have multiple chunks if it has
    9052             :   // > 1 arrays, fields, or ddids.
    9053           0 :   for(vi.originChunks(); vi.moreChunks(); vi.nextChunk()){
    9054           0 :     vca.reset();        // Should be done at the start of each chunk.
    9055             : 
    9056           0 :     inrowsdone += vi.nRowChunk();
    9057             : 
    9058             :     // Fill and time average vi's current chunk.
    9059           0 :     VisBuffer& avb(vca.average(vi));
    9060           0 :     uInt rowsnow = avb.nRow();
    9061             : 
    9062           0 :     if(rowsnow > 0){
    9063           0 :       RefRows rowstoadd(rowsdone, rowsdone + rowsnow - 1);
    9064             : 
    9065             :       // msOut_p.addRow(rowsnow, true);
    9066           0 :       msOut_p.addRow(rowsnow);            // Try it without initialization.
    9067             : 
    9068             :       // avb.freqAveCubes();  // Watch out, weight must currently be handled separately.
    9069             : 
    9070           0 :       remapper.remap(avb);
    9071             : 
    9072             :       // Fill in the nonaveraging values from slotv0.
    9073           0 :       msc_p->antenna1().putColumnCells(rowstoadd, avb.antenna1());
    9074           0 :       msc_p->antenna2().putColumnCells(rowstoadd, avb.antenna2());
    9075             : 
    9076           0 :       Vector<Int> arrID(rowsnow);
    9077           0 :       arrID.set(avb.arrayId());                              // Don't remap!
    9078           0 :       msc_p->arrayId().putColumnCells(rowstoadd, arrID);
    9079             : 
    9080             :       // outCmplxCols determines whether the input column is output to DATA or not.
    9081           0 :       for(uInt datacol = 0; datacol < nCmplx; ++datacol){
    9082           0 :         if(dataColNames[datacol] == MS::DATA)
    9083           0 :           outCmplxCols[datacol].putColumnCells(rowstoadd, avb.visCube());
    9084           0 :         else if(dataColNames[datacol] == MS::MODEL_DATA)
    9085           0 :           outCmplxCols[datacol].putColumnCells(rowstoadd, avb.modelVisCube());
    9086           0 :         else if(dataColNames[datacol] == MS::CORRECTED_DATA)
    9087           0 :           outCmplxCols[datacol].putColumnCells(rowstoadd, avb.correctedVisCube());
    9088             :       }
    9089           0 :       if(doFloat)
    9090           0 :         msc_p->floatData().putColumnCells(rowstoadd, avb.floatDataCube());
    9091             : 
    9092           0 :       Vector<Int> ddID(rowsnow);
    9093           0 :       ddID.set(avb.dataDescriptionId());
    9094           0 :       msc_p->dataDescId().putColumnCells(rowstoadd, ddID);
    9095             : 
    9096           0 :       msc_p->exposure().putColumnCells(rowstoadd, avb.exposure());
    9097           0 :       msc_p->feed1().putColumnCells(rowstoadd, avb.feed1());
    9098           0 :       msc_p->feed2().putColumnCells(rowstoadd, avb.feed2());
    9099             : 
    9100           0 :       Vector<Int> fieldID(rowsnow);
    9101           0 :       fieldID.set(avb.fieldId());
    9102           0 :       msc_p->fieldId().putColumnCells(rowstoadd, fieldID);
    9103             : 
    9104           0 :       msc_p->flagRow().putColumnCells(rowstoadd, avb.flagRow()); 
    9105           0 :       msc_p->flag().putColumnCells(rowstoadd, avb.flagCube());
    9106             : 
    9107           0 :       if(doFC)
    9108           0 :         msc_p->flagCategory().putColumnCells(rowstoadd, avb.flagCategory());
    9109             : 
    9110           0 :       msc_p->interval().putColumnCells(rowstoadd, avb.timeInterval());
    9111           0 :       msc_p->observationId().putColumnCells(rowstoadd, avb.observationId());
    9112           0 :       msc_p->processorId().putColumnCells(rowstoadd, avb.processorId());
    9113           0 :       msc_p->scanNumber().putColumnCells(rowstoadd, avb.scan());   // Don't remap!
    9114             : 
    9115           0 :       if(doSpWeight)
    9116           0 :         msc_p->weightSpectrum().putColumnCells(rowstoadd, avb.weightSpectrum());
    9117             : 
    9118           0 :       if(fromCorrToData) {                          
    9119           0 :         wtmat.reference(avb.weightMat());
    9120           0 :         msc_p->weight().putColumnCells(rowstoadd, wtmat);
    9121           0 :         arrayTransformInPlace(wtmat, subms::wtToSigma);   // sig=1/sqrt(wt)
    9122           0 :         msc_p->sigma().putColumnCells(rowstoadd, wtmat);
    9123             :       }
    9124             :       else {
    9125           0 :         wtmat.reference(avb.sigmaMat());           // Yes, I'm reusing wtmat.
    9126           0 :         msc_p->sigma().putColumnCells(rowstoadd, wtmat);
    9127           0 :         arrayTransformInPlace(wtmat, subms::sigToWeight);   // wt=1/sig^2
    9128           0 :         msc_p->weight().putColumnCells(rowstoadd, wtmat);
    9129             :       }
    9130             : 
    9131             : 
    9132           0 :       msc_p->stateId().putColumnCells(rowstoadd, avb.stateId());
    9133           0 :       msc_p->time().putColumnCells(rowstoadd, avb.time());
    9134           0 :       msc_p->timeCentroid().putColumnCells(rowstoadd, avb.timeCentroid());
    9135           0 :       msc_p->uvw().putColumnCells(rowstoadd, avb.uvwMat());
    9136             :       
    9137           0 :       rowsdone += rowsnow;
    9138           0 :     }
    9139           0 :     meter.update(inrowsdone);
    9140             :   }   // End of for(vi.originChunks(); vi.moreChunks(); vi.nextChunk())
    9141           0 :   delete [] outCmplxCols;
    9142           0 :   os << LogIO::NORMAL << "Data binned." << LogIO::POST;
    9143             : 
    9144             :   //const ColumnDescSet& cds = mssel_p.tableDesc().columnDescSet();
    9145             :   //const ColumnDesc& cdesc = cds[MS::columnName(MS::DATA)];
    9146             :   //ROTiledStManAccessor tacc(mssel_p, cdesc.dataManagerGroup());
    9147             :   //tacc.showCacheStatistics(cerr);  // A 99.x% hit rate is good.  0% is bad.
    9148             : 
    9149             :   os << LogIO::DEBUG1 // helpdesk ticket in from Oleg Smirnov (ODU-232630)
    9150           0 :      << "Post binning memory: " << Memory::allocatedMemoryInBytes() / (1024.0 * 1024.0) << " MB"
    9151           0 :      << LogIO::POST;
    9152             : 
    9153           0 :   if(rowsdone < 1){
    9154             :     os << LogIO::WARN
    9155             :        << "No rows were written.  Is all the selected input flagged?"
    9156           0 :        << LogIO::POST;
    9157           0 :     return false;
    9158             :   }
    9159           0 :   return true;
    9160           0 : }
    9161             : 
    9162             : // This should become the default soon (with a name change).
    9163           0 : Bool SubMS::doTimeAverVisIterator(const Vector<MS::PredefinedColumns>& dataColNames,
    9164             :                                   const VBRemapper& remapper)
    9165             : {
    9166           0 :   LogIO os(LogOrigin("SubMS", "doTimeAverVisIterator()"));
    9167             : 
    9168             :   //No channel averaging with time averaging ... it's better this way, but
    9169             :   //maybe that should be revisited with VisibilityIterator.
    9170           0 :   if(chanStep_p[0] > 1){
    9171           0 :     throw(AipsError("Simultaneous time and channel averaging is not handled."));
    9172             :     return false;
    9173             :   }
    9174             : 
    9175             :   os << LogIO::DEBUG1 // helpdesk ticket from Oleg Smirnov (ODU-232630)
    9176             :      << "Before msOut_p.addRow(): "
    9177           0 :      << Memory::allocatedMemoryInBytes() / (1024.0 * 1024.0) << " MB"
    9178           0 :      << LogIO::POST;
    9179             : 
    9180           0 :   Vector<MS::PredefinedColumns> cmplxColLabels;
    9181           0 :   const Bool doFloat = sepFloat(dataColNames, cmplxColLabels);
    9182           0 :   const uInt nCmplx = cmplxColLabels.nelements();
    9183           0 :   if(doFloat && nCmplx > 0)                             // 2010-12-14
    9184             :     os << LogIO::WARN
    9185             :        << "Using VisIterator to average both FLOAT_DATA and another DATA column is extremely experimental."
    9186           0 :        << LogIO::POST;
    9187             : 
    9188             :   // Is CORRECTED_DATA being moved to DATA?
    9189           0 :   Bool fromCorrToData =  nCmplx == 1 && cmplxColLabels[0] == MS::CORRECTED_DATA;
    9190             : 
    9191           0 :   ArrayColumn<Complex> *outCmplxCols = new ArrayColumn<Complex>[nCmplx];
    9192           0 :   getDataColMap(outCmplxCols, nCmplx, cmplxColLabels);
    9193             : 
    9194             :   // We may need to watch for chunks (timebins) that should be split because of
    9195             :   // changes in scan, etc. (CAS-2401).  The old split way would have
    9196             :   // temporarily shortened timeBin, but vi.setInterval() does not work without
    9197             :   // calling vi.originChunks(), so that approach does not work with VisIterator.
    9198             :   // Instead, get VisIterator's sort (which also controls how the chunks are split)
    9199             :   // to do the work.
    9200           0 :   Block<Int> sort;
    9201           0 :   if(!setSortOrder(sort, "spw", false))
    9202             :     os << LogIO::WARN
    9203             :        << "The request to combine spws while time averaging is being ignored."
    9204           0 :        << LogIO::POST;
    9205             : 
    9206             :   // MSIter tends to produce output INTERVALs that are longer than the
    9207             :   // requested interval length, by ~0.5 input integrations for a random
    9208             :   // timeBin_p.  Giving it timeBin_p - 0.5 * interval[0] removes the bias and
    9209             :   // brings it almost in line with binTimes() (which uses -0.5 *
    9210             :   // interval[bin_start]).
    9211             :   //
    9212             :   // late April 2011: MSIter removed the bias, which threw off the correction.
    9213             :   // October 2011: The bias seems to be back, possibly because of a change in
    9214             :   //               when MSInterval's "offset" is reset.
    9215             :   //               But, then, practically, timebins can be cut short but never
    9216             :   //               lengthened by changes in scan, state, or obs ID, so it seems
    9217             :   //               better to leave the bias in!
    9218             :   //
    9219             :   //Double tbin = mscIn_p->interval()(0);
    9220             :   //tbin = timeBin_p > tbin ? timeBin_p - 0.5 * tbin : timeBin_p;
    9221           0 :   ROVisIterator vi(mssel_p, sort, timeBin_p);
    9222             :   
    9223             :   // Apply selection
    9224           0 :   vi.selectChannel(chanSlices_p);     // ROVisIterator
    9225           0 :   vi.selectCorrelation(corrSlices_p);
    9226             : 
    9227             :   // Translate chanSlices_p into the form vb.channelAve() wants.
    9228           0 :   Vector<Matrix<Int> > chanAveBounds;
    9229           0 :   vi.slicesToMatrices(chanAveBounds, chanSlices_p, widths_p);
    9230             : 
    9231           0 :   Matrix<Float> wtmat;
    9232           0 :   const Bool doSpWeight = vi.existsWeightSpectrum();
    9233             : 
    9234             :   //os << LogIO::NORMAL2 << "outNrow = " << msOut_p.nrow() << LogIO::POST;
    9235             : 
    9236             :   // All of this ddid/spw confusion really needs cleaning up.
    9237             :   // a map from input to output
    9238             :   // DATA_DESC_ID.  (ddidmap[input_ddid] = output_ddid.  Setting ddidmap to -1
    9239             :   // for unselected ddids will help make it obvious if unexpected ddids sneak
    9240             :   // through.)  Vector<Int> ddidmap(oldDDSpwMatch_p.nelements());
    9241             :   // ddidmap.set(-1);
    9242             :   // for(uInt i = 0; i < ddidmap.nelements(); ++i){
    9243             :   //   Int oldspwid = oldDDSpwMatch_p[i];
    9244             : 
    9245             :   //   if(oldspwid > -1 && oldspwid < spwRelabel_p.nelements())
    9246             :   //     ddidmap[i] = spwRelabel_p[oldspwid];
    9247             :   // }
    9248             : 
    9249           0 :   uInt rowsdone = 0;    // Output rows, used for the RefRows.
    9250             : 
    9251           0 :   uInt ninrows = mssel_p.nrow();
    9252             :   ProgressMeter meter(0.0, ninrows * 1.0, "split", "rows averaged", "", "",
    9253           0 :                       true, 1);
    9254           0 :   uInt inrowsdone = 0;  // only for the meter.
    9255             : 
    9256           0 :   VisChunkAverager vca(dataColNames, doSpWeight, chanAveBounds);
    9257             : 
    9258           0 :   Bool doFC = existsFlagCategory();
    9259             : 
    9260             :   // Iterate through the chunks.  A timebin will have multiple chunks if it has
    9261             :   // > 1 arrays, fields, or ddids.
    9262           0 :   for(vi.originChunks(); vi.moreChunks(); vi.nextChunk()){
    9263           0 :     vca.reset();        // Should be done at the start of each chunk.
    9264             : 
    9265           0 :     inrowsdone += vi.nRowChunk();
    9266             : 
    9267             :     // Fill and time average vi's current chunk.
    9268           0 :     VisBuffer& avb(vca.average(vi));
    9269           0 :     uInt rowsnow = avb.nRow();
    9270             : 
    9271           0 :     if(rowsnow > 0){
    9272           0 :       RefRows rowstoadd(rowsdone, rowsdone + rowsnow - 1);
    9273             : 
    9274             :       // msOut_p.addRow(rowsnow, true);
    9275           0 :       msOut_p.addRow(rowsnow);            // Try it without initialization.
    9276             :         
    9277             :       // avb.freqAveCubes();  // Watch out, weight must currently be handled
    9278             :       // separately.
    9279             : 
    9280           0 :       remapper.remap(avb);
    9281             : 
    9282             :       // Fill in the nonaveraging values from slotv0.
    9283           0 :       msc_p->antenna1().putColumnCells(rowstoadd, avb.antenna1());
    9284           0 :       msc_p->antenna2().putColumnCells(rowstoadd, avb.antenna2());
    9285             : 
    9286           0 :       Vector<Int> arrID(rowsnow);
    9287           0 :       arrID.set(avb.arrayId());                              // Don't remap!
    9288           0 :       msc_p->arrayId().putColumnCells(rowstoadd, arrID);
    9289             : 
    9290             :       // outCmplxCols determines whether the input column is output to DATA or not.
    9291           0 :       for(uInt datacol = 0; datacol < nCmplx; ++datacol){
    9292           0 :         if(dataColNames[datacol] == MS::DATA)
    9293           0 :           outCmplxCols[datacol].putColumnCells(rowstoadd, avb.visCube());
    9294           0 :         else if(dataColNames[datacol] == MS::MODEL_DATA)
    9295           0 :           outCmplxCols[datacol].putColumnCells(rowstoadd, avb.modelVisCube());
    9296           0 :         else if(dataColNames[datacol] == MS::CORRECTED_DATA)
    9297           0 :           outCmplxCols[datacol].putColumnCells(rowstoadd, avb.correctedVisCube());
    9298             :       }
    9299           0 :       if(doFloat)
    9300           0 :         msc_p->floatData().putColumnCells(rowstoadd, avb.floatDataCube());
    9301             : 
    9302           0 :       Vector<Int> ddID(rowsnow);
    9303           0 :       ddID.set(avb.dataDescriptionId());
    9304           0 :       msc_p->dataDescId().putColumnCells(rowstoadd, ddID);
    9305             : 
    9306           0 :       msc_p->exposure().putColumnCells(rowstoadd, avb.exposure());
    9307           0 :       msc_p->feed1().putColumnCells(rowstoadd, avb.feed1());
    9308           0 :       msc_p->feed2().putColumnCells(rowstoadd, avb.feed2());
    9309             : 
    9310           0 :       Vector<Int> fieldID(rowsnow);
    9311           0 :       fieldID.set(avb.fieldId());
    9312           0 :       msc_p->fieldId().putColumnCells(rowstoadd, fieldID);
    9313             : 
    9314           0 :       msc_p->flagRow().putColumnCells(rowstoadd, avb.flagRow()); 
    9315           0 :       msc_p->flag().putColumnCells(rowstoadd, avb.flagCube());
    9316             : 
    9317           0 :       if(doFC)
    9318           0 :         msc_p->flagCategory().putColumnCells(rowstoadd, avb.flagCategory());
    9319             : 
    9320           0 :       msc_p->interval().putColumnCells(rowstoadd, avb.timeInterval());
    9321           0 :       msc_p->observationId().putColumnCells(rowstoadd, avb.observationId());
    9322           0 :       msc_p->processorId().putColumnCells(rowstoadd, avb.processorId());
    9323           0 :       msc_p->scanNumber().putColumnCells(rowstoadd, avb.scan());     // Don't remap!
    9324             : 
    9325           0 :       if(doSpWeight)
    9326           0 :         msc_p->weightSpectrum().putColumnCells(rowstoadd, avb.weightSpectrum());
    9327             : 
    9328           0 :       if(fromCorrToData) {
    9329           0 :         wtmat.reference(avb.weightMat());
    9330           0 :         msc_p->weight().putColumnCells(rowstoadd, wtmat);
    9331           0 :         arrayTransformInPlace(wtmat, subms::wtToSigma);  // sig=1/sqrt(wt)
    9332           0 :         msc_p->sigma().putColumnCells(rowstoadd, wtmat);
    9333             :       }
    9334             :       else {
    9335           0 :         wtmat.reference(avb.sigmaMat());           // Yes, I'm reusing wtmat.     
    9336           0 :         msc_p->sigma().putColumnCells(rowstoadd, wtmat);
    9337           0 :         arrayTransformInPlace(wtmat, subms::sigToWeight);  // wt = 1/sig^2
    9338           0 :         msc_p->weight().putColumnCells(rowstoadd, wtmat);
    9339             :       }
    9340             : 
    9341           0 :       msc_p->stateId().putColumnCells(rowstoadd, avb.stateId());
    9342           0 :       msc_p->time().putColumnCells(rowstoadd, avb.time());
    9343           0 :       msc_p->timeCentroid().putColumnCells(rowstoadd, avb.timeCentroid());
    9344           0 :       msc_p->uvw().putColumnCells(rowstoadd, avb.uvwMat());
    9345             :       
    9346           0 :       rowsdone += rowsnow;
    9347           0 :     }
    9348           0 :     meter.update(inrowsdone);
    9349             :   }   // End of for(vi.originChunks(); vi.moreChunks(); vi.nextChunk())
    9350           0 :   delete [] outCmplxCols;
    9351           0 :   os << LogIO::NORMAL << "Data binned." << LogIO::POST;
    9352             : 
    9353             :   os << LogIO::DEBUG1 // helpdesk ticket in from Oleg Smirnov (ODU-232630)
    9354           0 :      << "Post binning memory: " << Memory::allocatedMemoryInBytes() / (1024.0 * 1024.0) << " MB"
    9355           0 :      << LogIO::POST;
    9356             : 
    9357           0 :   if(rowsdone < 1){
    9358             :     os << LogIO::WARN
    9359             :        << "No rows were written.  Is all the selected input flagged?"
    9360           0 :        << LogIO::POST;
    9361           0 :     return false;
    9362             :   }
    9363           0 :   return true;
    9364           0 : }
    9365             : 
    9366           0 : void SubMS::getDataColMap(MSColumns* msc, ArrayColumn<Complex>* mapper,
    9367             :                           uInt ntok,
    9368             :                           const Vector<MS::PredefinedColumns>& colEnums)
    9369             : {
    9370             :   // Set up a map from dataColumn indices to ArrayColumns in the output.
    9371             :   // mapper has to be a pointer (gasp!), not a Vector, because
    9372             :   // Vector<ArrayColumn<Complex> > mapper(ntok) would implicitly call
    9373             :   // .resize(), which uses =, which is banned for ArrayColumn.
    9374             : 
    9375           0 :   if(mustConvertToData(ntok, colEnums)){
    9376           0 :     mapper[0].reference(msc->data());
    9377             :   }
    9378             :   else{
    9379           0 :     for(uInt i = 0; i < ntok; ++i){
    9380           0 :       if(colEnums[i] == MS::CORRECTED_DATA)
    9381           0 :         mapper[i].reference(msc->correctedData());
    9382           0 :       else if(colEnums[i] == MS::MODEL_DATA)
    9383           0 :         mapper[i].reference(msc->modelData());
    9384           0 :       else if(colEnums[i] == MS::LAG_DATA)
    9385           0 :         mapper[i].reference(msc->lagData());
    9386             :       else                                  // The output default !=
    9387           0 :         mapper[i].reference(msc->data()); // the input default.
    9388             :     }
    9389             :   }
    9390           0 : }
    9391             : 
    9392             : inline Bool SubMS::areDataShapesConstant()
    9393             : {
    9394             :   return allSame(inNumChan_p) && allSame(nchan_p) && allSame(inNumCorr_p) && allSame(ncorr_p);
    9395             : }
    9396             : 
    9397           0 :   Bool isAllColumns(const Vector<MS::PredefinedColumns>& colNames)
    9398             :   {
    9399           0 :     Bool dCol=false, mCol=false, cCol=false;
    9400           0 :     for(uInt i=0;i<colNames.nelements();i++)
    9401             :       {
    9402           0 :         if (colNames[i] == MS::DATA)                dCol=true;
    9403           0 :         else if (colNames[i] == MS::MODEL_DATA)     mCol=true;
    9404           0 :         else if (colNames[i] == MS::CORRECTED_DATA) cCol=true;
    9405             :         // else turn off all?
    9406             :       }
    9407           0 :     return (dCol && mCol && cCol);
    9408             :   }
    9409             : 
    9410             :   // -----------------------------------------------------------------------
    9411             :   // Work-around to copy the keywords of the FLOAT_DATA column to the output MS
    9412             :   // -----------------------------------------------------------------------
    9413           0 :   void SubMS::copyMainTableKeywords (TableRecord& outKeys,
    9414             :                 const TableRecord& inKeys)
    9415             :   {
    9416           0 :         for (uInt i=0; i<inKeys.nfields(); i++) {
    9417           0 :                 if (inKeys.type(i) == TpString) {
    9418             :                         // Add keywords for MAIN table columns such as FLOAT_DATA
    9419           0 :                         String ikey = inKeys.name(i);
    9420           0 :                         if (!outKeys.isDefined (ikey)) {
    9421           0 :                                 String keyval;
    9422           0 :                                 inKeys.get(ikey, keyval);
    9423           0 :                                 outKeys.define(ikey,keyval);
    9424           0 :                         }
    9425             : 
    9426           0 :                 }
    9427             : 
    9428             :         }
    9429           0 :   }
    9430             : 
    9431             : } //#End casa namespace

Generated by: LCOV version 1.16