LCOV - code coverage report
Current view: top level - mstransform/MSTransform - MSTransformRegridder.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 902 1253 72.0 %
Date: 2024-12-11 20:54:31 Functions: 5 7 71.4 %

          Line data    Source code
       1             : //# MSTransformRegridder.cc: This file contains the implementation of the MSTransformRegridder class.
       2             : //#
       3             : //#  CASA - Common Astronomy Software Applications (http://casa.nrao.edu/)
       4             : //#  Copyright (C) Associated Universities, Inc. Washington DC, USA 2011, All rights reserved.
       5             : //#  Copyright (C) European Southern Observatory, 2011, All rights reserved.
       6             : //#
       7             : //#  This library is free software; you can redistribute it and/or
       8             : //#  modify it under the terms of the GNU Lesser General Public
       9             : //#  License as published by the Free software Foundation; either
      10             : //#  version 2.1 of the License, or (at your option) any later version.
      11             : //#
      12             : //#  This library is distributed in the hope that it will be useful,
      13             : //#  but WITHOUT ANY WARRANTY, without even the implied warranty of
      14             : //#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             : //#  Lesser General Public License for more details.
      16             : //#
      17             : //#  You should have received a copy of the GNU Lesser General Public
      18             : //#  License along with this library; if not, write to the Free Software
      19             : //#  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
      20             : //#  MA 02111-1307  USA
      21             : //# $Id: $
      22             : 
      23             : #include <mstransform/MSTransform/MSTransformRegridder.h>
      24             : 
      25             : using namespace casacore;
      26             : namespace casa { //# NAMESPACE CASA - BEGIN
      27             : 
      28             : /////////////////////////////////////////////
      29             : /// MSTransformRegridder implementation ///
      30             : /////////////////////////////////////////////
      31             : 
      32             : // -----------------------------------------------------------------------
      33             : //
      34             : // -----------------------------------------------------------------------
      35           0 : MSTransformRegridder::MSTransformRegridder()
      36             : {
      37           0 :         return;
      38             : }
      39             : 
      40             : // -----------------------------------------------------------------------
      41             : //
      42             : // -----------------------------------------------------------------------
      43           0 : MSTransformRegridder::~MSTransformRegridder()
      44             : {
      45           0 :         return;
      46           0 : }
      47             : 
      48             : 
      49             : // -----------------------------------------------------------------------
      50             : // Make one spectral window from all SPWs given by the SPW Ids vector
      51             : // -----------------------------------------------------------------------
      52          90 : Bool MSTransformRegridder::combineSpws( LogIO& os,
      53             :                                                                                 String msName,
      54             :                                                                                 const Vector<Int>& spwids,
      55             :                                                                                 Vector<Double>& newCHAN_FREQ,
      56             :                                                                                 Vector<Double>& newCHAN_WIDTH,
      57             :                                                                                 vector<vector<Int> >& averageWhichChan,
      58             :                                                                                 vector<vector<Int> >& averageWhichSPW,
      59             :                                                                                 vector<vector<Double> >& averageChanFrac,
      60             :                                                                                 Bool verbose)
      61             : {
      62          90 :         MeasurementSet ms_p(msName, Table::Old);
      63          90 :         Bool result = false;
      64          90 :         result = combineSpwsCore(os,ms_p,spwids,newCHAN_FREQ,newCHAN_WIDTH,
      65             :                                                          averageWhichChan, averageWhichSPW, averageChanFrac, verbose);
      66          90 :         return result;
      67          90 : }
      68             : 
      69             : // -----------------------------------------------------------------------
      70             : // Make one spectral window from all SPWs given by the SPW Ids vector
      71             : // -----------------------------------------------------------------------
      72         242 : Bool MSTransformRegridder::combineSpwsCore(     LogIO& os,
      73             :                                                                                         MeasurementSet& ms_p,
      74             :                                                                                         const Vector<Int>& spwids,
      75             :                                                                                         Vector<Double>& newCHAN_FREQ,
      76             :                                                                                         Vector<Double>& newCHAN_WIDTH,
      77             :                                                                                         vector<vector<Int> >& averageWhichChan,
      78             :                                                                                         vector<vector<Int> >& averageWhichSPW,
      79             :                                                                                         vector<vector<Double> >& averageChanFrac,
      80             :                                                                                         Bool verbose)
      81             : {
      82             :         // Analyze SPW Ids
      83         242 :         if (spwids.nelements() == 0)
      84             :         {
      85           0 :                 os << LogIO::WARN << LogOrigin("MSTransformRegridder", __FUNCTION__)
      86           0 :                                 << "No SPWs selected for combination ..." << LogIO::POST;
      87           0 :                 return true;
      88             :         }
      89             : 
      90             :         // Find all existing spws,
      91         242 :         MSSpectralWindow spwtable = ms_p.spectralWindow();
      92         242 :         Int origNumSPWs = spwtable.nrow();
      93             : 
      94         242 :         vector<Int> spwsToCombine;
      95         242 :         Vector<Bool> includeIt(origNumSPWs, false);
      96             : 
      97             :         // jagonzal: This covers for the case when we want to combine all the input SPWs
      98         242 :         if (spwids(0) == -1)
      99             :         {
     100         714 :                 for (Int i = 0; i < origNumSPWs; i++)
     101             :                 {
     102         624 :                         spwsToCombine.push_back(i);
     103         624 :                         includeIt(i) = true;
     104             :                 }
     105             :         }
     106             :         // jagonzal: Nominal case when we want to combine a sub-set of the input SPWs
     107             :         else
     108             :         {
     109         579 :                 for (uInt i = 0; i < spwids.nelements(); i++)
     110             :                 {
     111         427 :                         if (spwids(i) < origNumSPWs && spwids(i) >= 0)
     112             :                         {
     113         427 :                                 spwsToCombine.push_back(spwids(i));
     114         427 :                                 includeIt(spwids(i)) = true;
     115             :                         }
     116             :                         else
     117             :                         {
     118           0 :                                 os      << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
     119             :                                         << "Invalid SPW ID selected for combination "
     120           0 :                                         << spwids(i) << "valid range is 0 - "
     121           0 :                                         << origNumSPWs - 1 << ")" << LogIO::POST;
     122           0 :                                 return false;
     123             :                         }
     124             :                 }
     125             :         }
     126             : 
     127             :         // jagonzal: Marginal case when there is no actual SPW combination
     128         242 :         if (spwsToCombine.size() <= 1)
     129             :         {
     130           0 :                 if (verbose)
     131             :                 {
     132           0 :                         os      << LogIO::NORMAL << LogOrigin("MSTransformRegridder", __FUNCTION__)
     133             :                                 << "Less than two SPWs selected. No combination necessary."
     134           0 :                                 << LogIO::POST;
     135             :                 }
     136           0 :                 return true;
     137             :         }
     138             : 
     139             :         // Sort the SPW Ids
     140         242 :         std::sort(spwsToCombine.begin(), spwsToCombine.end());
     141             : 
     142         242 :         uInt nSpwsToCombine = spwsToCombine.size();
     143             : 
     144             :         // Prepare access to the SPW table
     145         242 :         MSSpWindowColumns SPWColrs(spwtable);
     146         242 :         ScalarColumn<Int> numChanColr = SPWColrs.numChan();
     147         242 :         ArrayColumn<Double> chanFreqColr = SPWColrs.chanFreq();
     148         242 :         ArrayColumn<Double> chanWidthColr = SPWColrs.chanWidth();
     149         242 :         ScalarColumn<Int> measFreqRefColr = SPWColrs.measFreqRef();
     150         242 :         ArrayColumn<Double> effectiveBWColr = SPWColrs.effectiveBW();
     151         242 :         ScalarColumn<Double> refFrequencyColr = SPWColrs.refFrequency();
     152         242 :         ArrayColumn<Double> resolutionColr = SPWColrs.resolution();
     153         242 :         ScalarColumn<Double> totalBandwidthColr = SPWColrs.totalBandwidth();
     154             : 
     155             :         // Create a list of the SPW Ids sorted by first (lowest) channel frequency
     156         242 :         vector<Int> spwsSorted(nSpwsToCombine);
     157         242 :         Vector<Bool> isDescending(origNumSPWs, false);
     158         242 :         Bool negChanWidthWarned = false;
     159             : 
     160             : 
     161         242 :         Double* firstFreq = new Double[nSpwsToCombine];
     162         242 :         uInt count = 0;
     163        1299 :         for (uInt i = 0; (Int) i < origNumSPWs; i++)
     164             :         {
     165        1057 :                 if (includeIt(i))
     166             :                 {
     167        1051 :                         Vector<Double> CHAN_FREQ(chanFreqColr(i));
     168             : 
     169             :                         // If frequencies are ascending, take the first channel, otherwise the last
     170        1051 :                         uInt nCh = CHAN_FREQ.nelements();
     171        1051 :                         if (CHAN_FREQ(0) <= CHAN_FREQ(nCh - 1))
     172             :                         {
     173        1023 :                                 firstFreq[count] = CHAN_FREQ(0);
     174             :                         }
     175             :                         else
     176             :                         {
     177          28 :                                 firstFreq[count] = CHAN_FREQ(nCh - 1);
     178          28 :                                 isDescending(i) = true;
     179             :                         }
     180        1051 :                         count++;
     181        1051 :                 }
     182             :         }
     183             : 
     184         242 :         Sort sort;
     185         242 :         sort.sortKey(firstFreq, TpDouble); // define sort key
     186         242 :         Vector<uInt> inx(nSpwsToCombine);
     187         242 :         sort.sort(inx, nSpwsToCombine);
     188        1293 :         for (uInt i = 0; i < nSpwsToCombine; i++)
     189             :         {
     190        1051 :                 spwsSorted[i] = spwsToCombine[inx(i)];
     191             :         }
     192         242 :         delete[] firstFreq;
     193             : 
     194             : 
     195         242 :         Int id0 = spwsSorted[0];
     196             : 
     197         242 :         uInt newNUM_CHAN = numChanColr(id0);
     198         242 :         newCHAN_FREQ.assign(chanFreqColr(id0));
     199         242 :         newCHAN_WIDTH.assign(chanWidthColr(id0));
     200         242 :         Bool newIsDescending = isDescending(id0);
     201             :         {
     202         242 :                 Bool negativeWidths = false;
     203       27178 :                 for (uInt i = 0; i < newNUM_CHAN; i++)
     204             :                 {
     205       26936 :                         if (newCHAN_WIDTH(i) < 0.)
     206             :                         {
     207         384 :                                 negativeWidths = true;
     208         384 :                                 newCHAN_WIDTH(i) = -newCHAN_WIDTH(i);
     209             :                         }
     210             :                 }
     211         242 :                 if (negativeWidths && verbose)
     212             :                 {
     213           6 :                         os      << LogIO::NORMAL << LogOrigin("MSTransformRegridder", __FUNCTION__)
     214             :                                 << " *** Encountered negative channel widths in SPECTRAL_WINDOW table."
     215           6 :                                 << LogIO::POST;
     216           3 :                         negChanWidthWarned = true;
     217             :                 }
     218             :         }
     219             : 
     220             :         // Need to reverse the order for processing
     221         242 :         if (newIsDescending)
     222             :         {
     223           3 :                 Vector<Double> tempF, tempW;
     224           3 :                 tempF.assign(newCHAN_FREQ);
     225           3 :                 tempW.assign(newCHAN_WIDTH);
     226         387 :                 for (uInt i = 0; i < newNUM_CHAN; i++)
     227             :                 {
     228         384 :                         newCHAN_FREQ(i) = tempF(newNUM_CHAN - 1 - i);
     229         384 :                         newCHAN_WIDTH(i) = tempW(newNUM_CHAN - 1 - i);
     230             :                 }
     231           3 :         }
     232             : 
     233         242 :         Vector<Double> newEFFECTIVE_BW(effectiveBWColr(id0));
     234         242 :         Int newMEAS_FREQ_REF = measFreqRefColr(id0);
     235         242 :         Vector<Double> newRESOLUTION(resolutionColr(id0));
     236             : 
     237         242 :         vector<Int> averageN; // For each new channel store the number of old channels to average over
     238             :         //vector<vector<Int> > averageWhichSPW; // For each new channel store the (old) SPWs to average over
     239             :         //vector<vector<Int> > averageWhichChan; // For each new channel store the channel numbers to av. over
     240             :         //vector<vector<Double> > averageChanFrac; // For each new channel store the channel fraction for each old channel
     241             : 
     242             :         // Initialize the averaging vectors
     243       27178 :         for (uInt i = 0; i < newNUM_CHAN; i++)
     244             :         {
     245       26936 :                 averageN.push_back(1);
     246       26936 :                 vector<Int> tv; // Just a temporary auxiliary vector
     247       26936 :                 tv.push_back(id0);
     248       26936 :                 averageWhichSPW.push_back(tv);
     249       26936 :                 if (newIsDescending)
     250             :                 {
     251         384 :                         tv[0] = newNUM_CHAN - 1 - i;
     252             :                 }
     253             :                 else
     254             :                 {
     255       26552 :                         tv[0] = i;
     256             :                 }
     257       26936 :                 averageWhichChan.push_back(tv);
     258       26936 :                 vector<Double> tvd; // another one
     259       26936 :                 tvd.push_back(1.);
     260       26936 :                 averageChanFrac.push_back(tvd);
     261       26936 :         }
     262             : 
     263         242 :         if (verbose)
     264             :         {
     265         180 :                 os      << LogIO::NORMAL << LogOrigin("MSTransformRegridder", __FUNCTION__)
     266             :                         << "Input SPWs sorted by first (lowest) channel frequency:"
     267         180 :                         << LogIO::POST;
     268             : 
     269          90 :                 ostringstream oss; // needed for iomanip functions
     270          90 :                 oss     << "   SPW " << std::setw(3) << id0 << ": " << std::setw(5)
     271          90 :                         << newNUM_CHAN << " channels, first channel = "
     272          90 :                         << std::setprecision(9) << std::setw(14) << std::scientific
     273          90 :                         << newCHAN_FREQ(0) << " Hz";
     274          90 :                 if (newNUM_CHAN > 1)
     275             :                 {
     276             :                         oss << ", last channel = " << std::setprecision(9)
     277          78 :                                 << std::setw(14) << std::scientific << newCHAN_FREQ(newNUM_CHAN - 1) << " Hz";
     278             :                 }
     279         180 :                 os << LogIO::NORMAL << LogOrigin("MSTransformRegridder", __FUNCTION__)
     280         180 :                                 << oss.str() << LogIO::POST;
     281          90 :         }
     282             : 
     283             :         // Loop over remaining given SPWs
     284        1051 :         for (uInt i = 1; i < nSpwsToCombine; i++)
     285             :         {
     286         809 :                 Int idi = spwsSorted[i];
     287             : 
     288         809 :                 uInt newNUM_CHANi = numChanColr(idi);
     289         809 :                 Vector<Double> newCHAN_FREQi(chanFreqColr(idi));
     290         809 :                 Vector<Double> newCHAN_WIDTHi(chanWidthColr(idi));
     291         809 :                 Bool newIsDescendingI = isDescending(idi);
     292             :                 {
     293         809 :                         Bool negativeWidths = false;
     294       79238 :                         for (uInt ii = 0; ii < newNUM_CHANi; ii++)
     295             :                         {
     296       78429 :                                 if (newCHAN_WIDTHi(ii) < 0.)
     297             :                                 {
     298        3200 :                                         negativeWidths = true;
     299        3200 :                                         newCHAN_WIDTHi(ii) = -newCHAN_WIDTHi(ii);
     300             :                                 }
     301             :                         }
     302         809 :                         if (negativeWidths && !negChanWidthWarned && verbose)
     303             :                         {
     304           0 :                                 os      << LogIO::NORMAL << LogOrigin("MSTransformRegridder", __FUNCTION__)
     305             :                                         << " *** Encountered negative channel widths in SPECTRAL_WINDOW table."
     306           0 :                                         << LogIO::POST;
     307           0 :                                 negChanWidthWarned = true;
     308             :                         }
     309             :                 }
     310             : 
     311             :                 // need to reverse the order for processing
     312         809 :                 if (newIsDescendingI)
     313             :                 {
     314          25 :                         Vector<Double> tempF, tempW;
     315          25 :                         tempF.assign(newCHAN_FREQi);
     316          25 :                         tempW.assign(newCHAN_WIDTHi);
     317        3225 :                         for (uInt ii = 0; ii < newNUM_CHANi; ii++)
     318             :                         {
     319        3200 :                                 newCHAN_FREQi(ii) = tempF(newNUM_CHANi - 1 - ii);
     320        3200 :                                 newCHAN_WIDTHi(ii) = tempW(newNUM_CHANi - 1 - ii);
     321             :                         }
     322          25 :                 }
     323             : 
     324         809 :                 Vector<Double> newEFFECTIVE_BWi(effectiveBWColr(idi));
     325         809 :                 Int newMEAS_FREQ_REFi = measFreqRefColr(idi);
     326         809 :                 Vector<Double> newRESOLUTIONi(resolutionColr(idi));
     327             : 
     328         809 :                 if (verbose)
     329             :                 {
     330         534 :                         ostringstream oss;
     331         534 :                         oss << "   SPW " << std::setw(3) << idi << ": " << std::setw(5)
     332         534 :                                 << newNUM_CHANi << " channels, first channel = "
     333         534 :                                 << std::setprecision(9) << std::setw(14)
     334         534 :                                 << std::scientific << newCHAN_FREQi(0) << " Hz";
     335             : 
     336         534 :                         if (newNUM_CHANi > 1)
     337             :                         {
     338             :                                 oss << ", last channel = " << std::setprecision(9)
     339         522 :                                         << std::setw(14) << std::scientific
     340         522 :                                         << newCHAN_FREQi(newNUM_CHANi - 1) << " Hz";
     341             :                         }
     342         534 :                         os << LogIO::NORMAL << oss.str() << LogIO::POST;
     343         534 :                 }
     344             : 
     345         809 :                 vector<Double> mergedChanFreq;
     346         809 :                 vector<Double> mergedChanWidth;
     347         809 :                 vector<Double> mergedEffBW;
     348         809 :                 vector<Double> mergedRes;
     349         809 :                 vector<Int> mergedAverageN;
     350         809 :                 vector<vector<Int> > mergedAverageWhichSPW;
     351         809 :                 vector<vector<Int> > mergedAverageWhichChan;
     352         809 :                 vector<vector<Double> > mergedAverageChanFrac;
     353             : 
     354             :                 // check for compatibility
     355         809 :                 if (newMEAS_FREQ_REFi != newMEAS_FREQ_REF)
     356             :                 {
     357           0 :                         os      << LogIO::WARN  << LogOrigin("MSTransformRegridder", __FUNCTION__)
     358             :                                 << "SPW " << idi
     359             :                                 << " cannot be combined with SPW " << id0
     360           0 :                                 << ". Non-matching ref. frame." << LogIO::POST;
     361           0 :                         return false;
     362             :                 }
     363             : 
     364             :                 // Append or prepend spw to new spw overlap at all?
     365         809 :                 if (newCHAN_FREQ(newNUM_CHAN - 1) + newCHAN_WIDTH(newNUM_CHAN - 1)
     366         809 :                                 / 2. < newCHAN_FREQi(0) - newCHAN_WIDTHi(0) / 2.)
     367             :                 {
     368             :                         // No overlap, and need to append
     369        8862 :                         for (uInt j = 0; j < newNUM_CHAN; j++)
     370             :                         {
     371        8809 :                                 mergedChanFreq.push_back(newCHAN_FREQ(j));
     372        8809 :                                 mergedChanWidth.push_back(newCHAN_WIDTH(j));
     373        8809 :                                 mergedEffBW.push_back(newEFFECTIVE_BW(j));
     374        8809 :                                 mergedRes.push_back(newRESOLUTION(j));
     375        8809 :                                 mergedAverageN.push_back(averageN[j]);
     376        8809 :                                 mergedAverageWhichSPW.push_back(averageWhichSPW[j]);
     377        8809 :                                 mergedAverageWhichChan.push_back(averageWhichChan[j]);
     378        8809 :                                 mergedAverageChanFrac.push_back(averageChanFrac[j]);
     379             :                         }
     380             : 
     381          53 :                         vector<Int> tv;
     382          53 :                         tv.push_back(idi); // Origin is SPW Id-i
     383          53 :                         vector<Int> tv2;
     384          53 :                         tv2.push_back(0);
     385          53 :                         vector<Double> tvd;
     386          53 :                         tvd.push_back(1.); // Fraction is 1.
     387        6235 :                         for (uInt j = 0; j < newNUM_CHANi; j++)
     388             :                         {
     389        6182 :                                 mergedChanFreq.push_back(newCHAN_FREQi(j));
     390        6182 :                                 mergedChanWidth.push_back(newCHAN_WIDTHi(j));
     391        6182 :                                 mergedEffBW.push_back(newEFFECTIVE_BWi(j));
     392        6182 :                                 mergedRes.push_back(newRESOLUTIONi(j));
     393        6182 :                                 mergedAverageN.push_back(1); // so far only one channel
     394        6182 :                                 mergedAverageWhichSPW.push_back(tv);
     395        6182 :                                 if (newIsDescendingI)
     396             :                                 {
     397           0 :                                         tv2[0] = newNUM_CHANi - 1 - j;
     398             :                                 }
     399             :                                 else
     400             :                                 {
     401        6182 :                                         tv2[0] = j;
     402             :                                 }
     403        6182 :                                 mergedAverageWhichChan.push_back(tv2); // channel number is j
     404        6182 :                                 mergedAverageChanFrac.push_back(tvd);
     405             :                         }
     406          53 :                 }
     407         756 :                 else if (newCHAN_FREQ(0) - newCHAN_WIDTH(0) / 2. > newCHAN_FREQi(
     408         756 :                                 newNUM_CHANi - 1) + newCHAN_WIDTHi(newNUM_CHANi - 1) / 2.)
     409             :                 {
     410             :                         // No overlap, need to prepend
     411           0 :                         vector<Int> tv;
     412           0 :                         tv.push_back(idi); // origin is SPW Id-i
     413           0 :                         vector<Int> tv2;
     414           0 :                         tv2.push_back(0);
     415           0 :                         vector<Double> tvd;
     416           0 :                         tvd.push_back(1.); // fraction is 1.
     417           0 :                         for (uInt j = 0; j < newNUM_CHANi; j++)
     418             :                         {
     419           0 :                                 mergedChanFreq.push_back(newCHAN_FREQi(j));
     420           0 :                                 mergedChanWidth.push_back(newCHAN_WIDTHi(j));
     421           0 :                                 mergedEffBW.push_back(newEFFECTIVE_BWi(j));
     422           0 :                                 mergedRes.push_back(newRESOLUTIONi(j));
     423           0 :                                 mergedAverageN.push_back(1); // so far only one channel
     424           0 :                                 mergedAverageWhichSPW.push_back(tv);
     425           0 :                                 if (newIsDescendingI)
     426             :                                 {
     427           0 :                                         tv2[0] = newNUM_CHANi - 1 - j;
     428             :                                 }
     429             :                                 else
     430             :                                 {
     431           0 :                                         tv2[0] = j;
     432             :                                 }
     433           0 :                                 mergedAverageWhichChan.push_back(tv2); // channel number is j
     434           0 :                                 mergedAverageChanFrac.push_back(tvd);
     435             :                         }
     436             : 
     437           0 :                         for (uInt j = 0; j < newNUM_CHAN; j++)
     438             :                         {
     439           0 :                                 mergedChanFreq.push_back(newCHAN_FREQ(j));
     440           0 :                                 mergedChanWidth.push_back(newCHAN_WIDTH(j));
     441           0 :                                 mergedEffBW.push_back(newEFFECTIVE_BW(j));
     442           0 :                                 mergedRes.push_back(newRESOLUTION(j));
     443           0 :                                 mergedAverageN.push_back(averageN[j]);
     444           0 :                                 mergedAverageWhichSPW.push_back(averageWhichSPW[j]);
     445           0 :                                 mergedAverageWhichChan.push_back(averageWhichChan[j]);
     446           0 :                                 mergedAverageChanFrac.push_back(averageChanFrac[j]);
     447             :                         }
     448           0 :                 }
     449             :                 // there is overlap
     450             :                 else
     451             :                 {
     452         756 :                         Int id0StartChan = 0;
     453             :                         // SPW Id-i starts before SPW Id-0
     454         756 :                         if (newCHAN_FREQi(0) - newCHAN_WIDTHi(0) / 2. < newCHAN_FREQ(
     455         756 :                                         newNUM_CHAN - 1) - newCHAN_WIDTH(newNUM_CHAN - 1) / 2.)
     456             :                         {
     457             : 
     458             : 
     459             :                                 // Some utilities for the averaging info
     460         405 :                                 vector<Int> tv; // temporary vector
     461         405 :                                 tv.push_back(idi); // origin is spw idi
     462         405 :                                 vector<Int> tv2;
     463         405 :                                 tv2.push_back(0);
     464         405 :                                 vector<Double> tvd;
     465         405 :                                 tvd.push_back(1.); // fraction is 1.
     466             : 
     467             :                                 // Find the first overlapping channel and prepend non-overlapping channels
     468         405 :                                 Double ubound0 = newCHAN_FREQ(0) + newCHAN_WIDTH(0) / 2.;
     469         405 :                                 Double lbound0 = newCHAN_FREQ(0) - newCHAN_WIDTH(0) / 2.;
     470         405 :                                 Double uboundk = 0.;
     471         405 :                                 Double lboundk = 0.;
     472             :                                 uInt k;
     473         405 :                                 for (k = 0; k < newNUM_CHANi; k++)
     474             :                                 {
     475         405 :                                         uboundk = newCHAN_FREQi(k) + newCHAN_WIDTHi(k) / 2.;
     476         405 :                                         lboundk = newCHAN_FREQi(k) - newCHAN_WIDTHi(k) / 2.;
     477         405 :                                         if (lbound0 < uboundk)
     478             :                                         {
     479         405 :                                                 break;
     480             :                                         }
     481           0 :                                         mergedChanFreq.push_back(newCHAN_FREQi(k));
     482           0 :                                         mergedChanWidth.push_back(newCHAN_WIDTHi(k));
     483           0 :                                         mergedEffBW.push_back(newEFFECTIVE_BWi(k));
     484           0 :                                         mergedRes.push_back(newRESOLUTIONi(k));
     485           0 :                                         mergedAverageN.push_back(1); // So far only one channel
     486           0 :                                         mergedAverageWhichSPW.push_back(tv);
     487           0 :                                         if (newIsDescendingI)
     488             :                                         {
     489           0 :                                                 tv2[0] = newNUM_CHANi - 1 - k;
     490             :                                         }
     491             :                                         else
     492             :                                         {
     493           0 :                                                 tv2[0] = k;
     494             :                                         }
     495           0 :                                         mergedAverageWhichChan.push_back(tv2); // Channel number is k
     496           0 :                                         mergedAverageChanFrac.push_back(tvd);
     497             :                                 }
     498             : 
     499             :                                 // k-th is the one, actual overlap, need to merge channel k with channel 0
     500         405 :                                 if (lbound0 < uboundk && lboundk < lbound0)
     501             :                                 {
     502           0 :                                         Double newWidth = ubound0 - lboundk;
     503           0 :                                         Double newCenter = lboundk + newWidth / 2.;
     504           0 :                                         mergedChanFreq.push_back(newCenter);
     505           0 :                                         mergedChanWidth.push_back(newWidth);
     506           0 :                                         mergedEffBW.push_back(newWidth);
     507           0 :                                         mergedRes.push_back(newWidth);
     508           0 :                                         mergedAverageN.push_back(averageN[0] + 1); // one more channel contributes
     509             : 
     510             :                                         // Channel k is from SPW Id-i
     511           0 :                                         if (newIsDescendingI)
     512             :                                         {
     513           0 :                                                 tv2[0] = newNUM_CHANi - 1 - k;
     514             :                                         }
     515             :                                         else
     516             :                                         {
     517           0 :                                                 tv2[0] = k;
     518             :                                         }
     519             : 
     520           0 :                                         for (int j = 0; j < averageN[0]; j++)
     521             :                                         {
     522           0 :                                                 tv.push_back(averageWhichSPW[0][j]); // additional contributors
     523           0 :                                                 tv2.push_back(averageWhichChan[0][j]); // channel 0 from SPW Id-0
     524           0 :                                                 tvd.push_back(averageChanFrac[0][j]);
     525             :                                         }
     526           0 :                                         mergedAverageWhichSPW.push_back(tv);
     527           0 :                                         mergedAverageWhichChan.push_back(tv2);
     528           0 :                                         mergedAverageChanFrac.push_back(tvd);
     529           0 :                                         id0StartChan = 1;
     530             :                                 }
     531         405 :                         }
     532             : 
     533             :                         // Now move along SPW id0 and merge until end of id0 is reached, then just copy
     534      500732 :                         for (uInt j = id0StartChan; j < newNUM_CHAN; j++)
     535             :                         {
     536      499976 :                                 mergedChanFreq.push_back(newCHAN_FREQ(j));
     537      499976 :                                 mergedChanWidth.push_back(newCHAN_WIDTH(j));
     538      499976 :                                 mergedEffBW.push_back(newEFFECTIVE_BW(j));
     539      499976 :                                 mergedRes.push_back(newRESOLUTION(j));
     540             : 
     541    77198662 :                                 for (uInt k = 0; k < newNUM_CHANi; k++)
     542             :                                 {
     543    76698686 :                                         Double overlap_frac = 0.;
     544             : 
     545             :                                         // Does channel j in SPW Id-0 overlap with channel k in SPW Id-i?
     546    76698686 :                                         Double uboundj = newCHAN_FREQ(j) + newCHAN_WIDTH(j)/ 2.;
     547    76698686 :                                         Double uboundk = newCHAN_FREQi(k) + newCHAN_WIDTHi(k)/ 2.;
     548    76698686 :                                         Double lboundj = newCHAN_FREQ(j) - newCHAN_WIDTH(j)/ 2.;
     549    76698686 :                                         Double lboundk = newCHAN_FREQi(k) - newCHAN_WIDTHi(k)/ 2.;
     550             : 
     551             :                                         // Determine fraction
     552             : 
     553             :                                         // Chan k is completely covered by chan j
     554    76698686 :                                         if (lboundj <= lboundk && uboundk <= uboundj)
     555             :                                         {
     556         288 :                                                 overlap_frac = 1.;
     557             :                                         }
     558             :                                         // chan j is completely covered by chan k
     559    76698398 :                                         else if (lboundk <= lboundj && uboundj <= uboundk)
     560             :                                         {
     561           0 :                                                 overlap_frac = newCHAN_WIDTH(j) / newCHAN_WIDTHi(k);
     562             :                                         }
     563             :                                         // lower end of k is overlapping with j
     564    76698398 :                                         else if (lboundj < lboundk && lboundk < uboundj && uboundj < uboundk)
     565             :                                         {
     566       10686 :                                                 overlap_frac = (uboundj - lboundk) / newCHAN_WIDTHi(k);
     567             :                                         }
     568             :                                         // upper end of k is overlapping with j
     569    76687712 :                                         else if (lboundk < lboundj && lboundj < uboundk && lboundj < uboundk)
     570             :                                         {
     571       10311 :                                                 overlap_frac = (uboundk - lboundj) / newCHAN_WIDTHi(k);
     572             :                                         }
     573             : 
     574             :                                         // Update averaging info
     575    76698686 :                                         if (overlap_frac > 0.)
     576             :                                         {
     577       21285 :                                                 averageN[j] += 1;
     578       21285 :                                                 averageWhichSPW[j].push_back(idi);
     579       21285 :                                                 if (newIsDescendingI)
     580             :                                                 {
     581        1277 :                                                         averageWhichChan[j].push_back(newNUM_CHANi - 1 - k);
     582             :                                                 }
     583             :                                                 else
     584             :                                                 {
     585       20008 :                                                         averageWhichChan[j].push_back(k);
     586             :                                                 }
     587       21285 :                                                 averageChanFrac[j].push_back(overlap_frac);
     588             :                                         }
     589             :                                 } // End loop over SPW Id-i
     590             : 
     591             : 
     592             :                                 // Append this channel with updated averaging info
     593      499976 :                                 mergedAverageN.push_back(averageN[j]);
     594      499976 :                                 mergedAverageWhichSPW.push_back(averageWhichSPW[j]);
     595      499976 :                                 mergedAverageWhichChan.push_back(averageWhichChan[j]);
     596      499976 :                                 mergedAverageChanFrac.push_back(averageChanFrac[j]);
     597             : 
     598             :                         } // End loop over SPW Id-0
     599             : 
     600             :                         // SPW Id-i still continues, find the last overlapping channel
     601         756 :                         if (newCHAN_FREQ(newNUM_CHAN - 1) + newCHAN_WIDTH(
     602         756 :                                         newNUM_CHAN - 1) / 2. < newCHAN_FREQi(newNUM_CHANi - 1)
     603         756 :                                         + newCHAN_WIDTHi(newNUM_CHANi - 1) / 2.)
     604             :                         {
     605         728 :                                 Int j = newNUM_CHAN - 1;
     606         728 :                                 Double uboundj = newCHAN_FREQ(j) + newCHAN_WIDTH(j) / 2.;
     607         728 :                                 Double lboundj = newCHAN_FREQ(j) - newCHAN_WIDTH(j) / 2.;
     608         728 :                                 Double uboundk = 0;
     609         728 :                                 Double lboundk = 0;
     610             :                                 Int k;
     611       61648 :                                 for (k = newNUM_CHANi - 1; k >= 0; k--)
     612             :                                 {
     613       61648 :                                         uboundk = newCHAN_FREQi(k) + newCHAN_WIDTHi(k) / 2.;
     614       61648 :                                         lboundk = newCHAN_FREQi(k) - newCHAN_WIDTHi(k) / 2.;
     615       61648 :                                         if (lboundk <= uboundj)
     616             :                                         {
     617         728 :                                                 break;
     618             :                                         }
     619             :                                 }
     620             : 
     621             :                                 // k-th is the one
     622             : 
     623             :                                 // actual overlap
     624         728 :                                 if (lboundk < uboundj && uboundj < uboundk)
     625             :                                 {
     626         375 :                                         Double overlap_frac = (uboundj - lboundk) / newCHAN_WIDTHi(k);
     627             : 
     628             :                                         // Merge channel k completely with channel j
     629         375 :                                         if (overlap_frac > 0.01)
     630             :                                         {
     631         374 :                                                 Double newWidth = uboundk - lboundj;
     632         374 :                                                 Double newCenter = (lboundj + uboundk) / 2.;
     633         374 :                                                 mergedChanFreq[j] = newCenter;
     634         374 :                                                 mergedChanWidth[j] = newWidth;
     635         374 :                                                 mergedEffBW[j] = newWidth;
     636         374 :                                                 mergedRes[j] = newWidth;
     637         374 :                                                 mergedAverageChanFrac[j][mergedAverageN[j] - 1] = 1.;
     638             :                                         }
     639             :                                         // Create separate, (slightly) more narrow channel
     640             :                                         else
     641             :                                         {
     642           1 :                                                 Double newWidth = uboundk - uboundj;
     643           1 :                                                 Double newCenter = (uboundj + uboundk) / 2.;
     644           1 :                                                 vector<Int> tv;
     645           1 :                                                 tv.push_back(idi); // Origin is SPW Id-i
     646           1 :                                                 vector<Int> tv2;
     647           1 :                                                 tv2.push_back(0);
     648           1 :                                                 vector<Double> tvd;
     649           1 :                                                 tvd.push_back(1.); // Fraction is 1.
     650           1 :                                                 mergedChanFreq.push_back(newCenter);
     651           1 :                                                 mergedChanWidth.push_back(newWidth);
     652           1 :                                                 mergedEffBW.push_back(newWidth);
     653           1 :                                                 mergedRes.push_back(newWidth);
     654           1 :                                                 mergedAverageN.push_back(1); // So far only one channel
     655           1 :                                                 mergedAverageWhichSPW.push_back(tv);
     656           1 :                                                 if (newIsDescendingI)
     657             :                                                 {
     658           0 :                                                         tv2[0] = newNUM_CHANi - 1 - k;
     659             :                                                 }
     660             :                                                 else
     661             :                                                 {
     662           1 :                                                         tv2[0] = k;
     663             :                                                 }
     664           1 :                                                 mergedAverageWhichChan.push_back(tv2); // Channel number is k
     665           1 :                                                 mergedAverageChanFrac.push_back(tvd);
     666           1 :                                         }
     667             : 
     668         375 :                                         k++; // Start appending remaining channels after k
     669             :                                 }
     670             : 
     671             :                                 // Append the remaining channels
     672         728 :                                 vector<Int> tv;
     673         728 :                                 tv.push_back(idi); // Origin is SPW Id-i
     674         728 :                                 vector<Int> tv2;
     675         728 :                                 tv2.push_back(0);
     676         728 :                                 vector<Double> tvd;
     677         728 :                                 tvd.push_back(1.); // Fraction is 1.
     678             : 
     679       62001 :                                 for (uInt m = k; m < newNUM_CHANi; m++)
     680             :                                 {
     681       61273 :                                         mergedChanFreq.push_back(newCHAN_FREQi(m));
     682       61273 :                                         mergedChanWidth.push_back(newCHAN_WIDTHi(m));
     683       61273 :                                         mergedEffBW.push_back(newEFFECTIVE_BWi(m));
     684       61273 :                                         mergedRes.push_back(newRESOLUTIONi(m));
     685       61273 :                                         mergedAverageN.push_back(1); // So far only one channel
     686       61273 :                                         mergedAverageWhichSPW.push_back(tv);
     687             : 
     688       61273 :                                         if (newIsDescendingI)
     689             :                                         {
     690        2546 :                                                 tv2[0] = newNUM_CHANi - 1 - m;
     691             :                                         }
     692             :                                         else
     693             :                                         {
     694       58727 :                                                 tv2[0] = m;
     695             :                                         }
     696       61273 :                                         mergedAverageWhichChan.push_back(tv2); // Channel number is m
     697       61273 :                                         mergedAverageChanFrac.push_back(tvd);
     698             :                                 }
     699         728 :                         } // End if SPW Id-i still continues
     700             :                 } // End if there is overlap
     701             : 
     702             : 
     703         809 :                 newNUM_CHAN = mergedChanFreq.size();
     704         809 :                 newCHAN_FREQ.assign(Vector<Double> (mergedChanFreq));
     705         809 :                 newCHAN_WIDTH.assign(Vector<Double> (mergedChanWidth));
     706         809 :                 newEFFECTIVE_BW.assign(Vector<Double> (mergedEffBW));
     707         809 :                 newRESOLUTION.assign(Vector<Double> (mergedRes));
     708         809 :                 averageN = mergedAverageN;
     709         809 :                 averageWhichSPW.assign(mergedAverageWhichSPW.begin(), mergedAverageWhichSPW.end());
     710         809 :                 averageChanFrac.assign(mergedAverageChanFrac.begin(), mergedAverageChanFrac.end());
     711         809 :                 averageWhichChan.assign(mergedAverageWhichChan.begin(), mergedAverageWhichChan.end());
     712         809 :         }
     713             : 
     714         242 :         return true;
     715         242 : }
     716             : 
     717             : // -----------------------------------------------------------------------
     718             : // A wrapper for regridChanBounds() which takes the user interface type re-gridding parameters
     719             : // The ready-made grid is returned in newCHAN_FREQ and newCHAN_WIDTH
     720             : // -----------------------------------------------------------------------
     721         831 : Bool MSTransformRegridder::calcChanFreqs(       LogIO& os,
     722             :                                                                                 Vector<Double>& newCHAN_FREQ,
     723             :                                                                                 Vector<Double>& newCHAN_WIDTH,
     724             :                                                                                 Double& weightScale,
     725             :                                                                                 const Vector<Double>& oldCHAN_FREQ,
     726             :                                                                                 const Vector<Double>& oldCHAN_WIDTH,
     727             :                                                                                 const MDirection  phaseCenter,
     728             :                                                                                 const MFrequency::Types theOldRefFrame,
     729             :                                                                                 const MEpoch theObsTime,
     730             :                                                                                 const MPosition mObsPos,
     731             :                                                                                 const String& mode,
     732             :                                                                                 const int nchan,
     733             :                                                                                 const String& start,
     734             :                                                                                 const String& width,
     735             :                                                                                 const String& restfreq,
     736             :                                                                                 const String& outframe,
     737             :                                                                                 const String& veltype,
     738             :                                                                                 const Bool verbose,
     739             :                                                                                 const MRadialVelocity mRV)
     740             : {
     741         831 :         Vector<Double> newChanLoBound;
     742         831 :         Vector<Double> newChanHiBound;
     743         831 :         String t_phasec;
     744             : 
     745         831 :         String t_mode;
     746         831 :         String t_outframe;
     747         831 :         String t_regridQuantity;
     748             :         Double t_restfreq;
     749         831 :         String t_regridInterpMeth;
     750             :         Double t_cstart;
     751             :         Double t_bandwidth;
     752             :         Double t_cwidth;
     753             :         Bool t_centerIsStart;
     754             :         Bool t_startIsEnd;
     755             :         Int t_nchan;
     756             :         Int t_width;
     757             :         Int t_start;
     758             : 
     759         831 :         if (!convertGridPars(os, mode, nchan, start,width,
     760             :                                                 "linear", // A dummy value in this context
     761             :                                                 restfreq, outframe,veltype,
     762             :                                                 ////// output ////
     763             :                                                 t_mode, t_outframe, t_regridQuantity, t_restfreq,
     764             :                                                 t_regridInterpMeth, t_cstart, t_bandwidth, t_cwidth,
     765             :                                                 t_centerIsStart, t_startIsEnd, t_nchan, t_width, t_start))
     766             :         {
     767             :                 // An error occurred
     768           0 :                 return false;
     769             :         }
     770             : 
     771             :         // Reference frame transformation
     772         831 :         Bool needTransform = true;
     773         831 :         Bool doRadVelCorr = false;
     774             :         MFrequency::Types theFrame;
     775         831 :         String oframe = outframe;
     776         831 :         oframe.upcase();
     777             : 
     778             :         // No output reference frame given
     779         831 :         if (outframe == "")
     780             :         {
     781             :                 // Keep the reference frame as is
     782         286 :                 theFrame = theOldRefFrame;
     783         286 :                 needTransform = false;
     784             :         }
     785             :         // GEO transformation + radial velocity correction
     786         545 :         else if (oframe == "SOURCE")
     787             :         {
     788           9 :                 theFrame = MFrequency::GEO;
     789           9 :                 doRadVelCorr = true;
     790             :         }
     791         536 :         else if (!MFrequency::getType(theFrame, outframe))
     792             :         {
     793           0 :                 os << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
     794           0 :                                 << "Parameter \"outframe\" value " << outframe << " is invalid." << LogIO::POST;
     795           0 :                 return false;
     796             :         }
     797         536 :         else if (theFrame == theOldRefFrame)
     798             :         {
     799         128 :                 needTransform = false;
     800             :         }
     801             : 
     802         831 :         uInt oldNUM_CHAN = oldCHAN_FREQ.size();
     803         831 :         if (oldNUM_CHAN == 0)
     804             :         {
     805           0 :                 newCHAN_FREQ.resize(0);
     806           0 :                 newCHAN_WIDTH.resize(0);
     807           0 :                 return true;
     808             :         }
     809             : 
     810         831 :         if (oldNUM_CHAN != oldCHAN_WIDTH.size())
     811             :         {
     812           0 :                 os              << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
     813             :                                 << "Internal error: inconsistent dimensions of input channel frequency and width arrays."
     814           0 :                                 << LogIO::POST;
     815           0 :                 return false;
     816             :         }
     817             : 
     818         831 :         Vector<Double> absOldCHAN_WIDTH;
     819         831 :         absOldCHAN_WIDTH.assign(oldCHAN_WIDTH);
     820         831 :         Bool negativeWidths = false;
     821      283349 :         for (uInt i = 0; i < oldCHAN_WIDTH.size(); i++)
     822             :         {
     823      282518 :                 if (oldCHAN_WIDTH(i) < 0.)
     824             :                 {
     825       24439 :                         negativeWidths = true;
     826       24439 :                         absOldCHAN_WIDTH(i) = -oldCHAN_WIDTH(i);
     827             :                 }
     828             :         }
     829             : 
     830         831 :         if (negativeWidths && verbose)
     831             :         {
     832          64 :                 os              << LogIO::NORMAL << LogOrigin("MSTransformRegridder", __FUNCTION__)
     833             :                                 << " *** Encountered negative channel widths in input spectral window."
     834          64 :                                 << LogIO::POST;
     835             :         }
     836             : 
     837         831 :         Vector<Double> transNewXin;
     838         831 :         Vector<Double> transCHAN_WIDTH(oldNUM_CHAN);
     839             : 
     840         831 :         if (needTransform)
     841             :         {
     842         417 :                 transNewXin.resize(oldNUM_CHAN);
     843             : 
     844             :                 // Set up conversion
     845         417 :                 Unit unit(String("Hz"));
     846             :                 MFrequency::Ref fromFrame = MFrequency::Ref(theOldRefFrame,
     847         417 :                                 MeasFrame(phaseCenter, mObsPos, theObsTime));
     848             :                 MFrequency::Ref toFrame = MFrequency::Ref(theFrame,
     849         417 :                                 MeasFrame(phaseCenter, mObsPos, theObsTime));
     850         417 :                 MFrequency::Convert freqTrans(unit, fromFrame, toFrame);
     851             : 
     852         417 :                 MDoppler radVelCorr;
     853         417 :                 Bool radVelSignificant = false;
     854             : 
     855             :                 // Prepare correction for radial velocity if requested and possible
     856         417 :                 if (doRadVelCorr)
     857             :                 {
     858           9 :                         Quantity mrv = mRV.get("m/s");
     859           9 :                         radVelCorr = MDoppler(-mrv); // NOTE: Opposite sign to achieve correction!
     860           9 :                         Double mRVVal = mrv.getValue();
     861           9 :                         if (fabs(mRVVal) > 1E-6)
     862             :                         {
     863           5 :                                 radVelSignificant = true;
     864             :                         }
     865             : 
     866           9 :                         if (verbose)
     867             :                         {
     868          10 :                                 os      << LogIO::NORMAL << LogOrigin("MSTransformRegridder", __FUNCTION__)
     869             :                                         << "Note: The given additional radial velocity of "
     870             :                                         << mRVVal << " m/s will be taken into account."
     871          10 :                                         << LogIO::POST;
     872             :                         }
     873           9 :                 }
     874             : 
     875      107054 :                 for (uInt i = 0; i < oldNUM_CHAN; i++)
     876             :                 {
     877      106637 :                         transNewXin[i] = freqTrans(oldCHAN_FREQ[i]).get(unit).getValue();
     878             : 
     879             :                         // eliminate possible offsets
     880      106637 :                         transCHAN_WIDTH[i] = freqTrans(oldCHAN_FREQ[i] + absOldCHAN_WIDTH[i] / 2.).
     881      213274 :                                                                 get(unit).getValue() - freqTrans(oldCHAN_FREQ[i] - absOldCHAN_WIDTH[i] / 2.).
     882      106637 :                                                                 get(unit).getValue();
     883             :                 }
     884             : 
     885             :                 // Correct in addition for radial velocity
     886         417 :                 if (radVelSignificant)
     887             :                 {
     888           5 :                         transNewXin = radVelCorr.shiftFrequency(transNewXin);
     889             : 
     890             :                         //shiftFrequency is a scaling, so channel widths scale as well
     891           5 :                         transCHAN_WIDTH = radVelCorr.shiftFrequency(transCHAN_WIDTH);
     892             :                 }
     893             : 
     894         417 :         }
     895             :         else
     896             :         {
     897             :                 // Just copy
     898         414 :                 transNewXin.assign(oldCHAN_FREQ);
     899         414 :                 transCHAN_WIDTH.assign(absOldCHAN_WIDTH);
     900             :         }
     901             : 
     902             :         // Calculate new grid
     903             : 
     904         831 :         String message;
     905             : 
     906         831 :         if (!regridChanBounds(  newChanLoBound, newChanHiBound, t_cstart,
     907             :                                                         t_bandwidth, t_cwidth, t_restfreq, t_regridQuantity, transNewXin,
     908             :                                                         transCHAN_WIDTH, message, t_centerIsStart, t_startIsEnd, t_nchan,
     909             :                                                         t_width, t_start))
     910             :         {
     911             :                 // There was an error
     912           2 :                 os << LogIO::WARN << LogOrigin("MSTransformRegridder", __FUNCTION__) << message << LogIO::POST;
     913           2 :                 return false;
     914             :         }
     915             : 
     916         829 :         if (verbose)
     917             :         {
     918         641 :                 os << LogIO::NORMAL << LogOrigin("MSTransformRegridder", __FUNCTION__) << message << LogIO::POST;
     919             :         }
     920             : 
     921             :         // We have a useful set of channel boundaries
     922         829 :         uInt newNUM_CHAN = newChanLoBound.size();
     923             : 
     924             :         // Complete the calculation of the new channel centers and widths
     925             :         // from newNUM_CHAN, newChanLoBound, and newChanHiBound
     926         829 :         newCHAN_FREQ.resize(newNUM_CHAN);
     927         829 :         newCHAN_WIDTH.resize(newNUM_CHAN);
     928      178215 :         for (uInt i = 0; i < newNUM_CHAN; i++)
     929             :         {
     930      177386 :                 newCHAN_FREQ[i] = (newChanLoBound[i] + newChanHiBound[i]) / 2.;
     931      177386 :                 newCHAN_WIDTH[i] = newChanHiBound[i] - newChanLoBound[i];
     932             :         }
     933             : 
     934         829 :         weightScale = newCHAN_WIDTH[0]/transCHAN_WIDTH[0];
     935             : 
     936         829 :         return true;
     937         831 : }
     938             : 
     939             : // -----------------------------------------------------------------------
     940             : // Helper function for handling the re-gridding parameter user input
     941             : // -----------------------------------------------------------------------
     942         831 : Bool MSTransformRegridder::convertGridPars(     LogIO& os,
     943             :                                                                                         const String& mode,
     944             :                                                                                         const int nchan,
     945             :                                                                                         const String& start,
     946             :                                                                                         const String& width,
     947             :                                                                                         const String& interp,
     948             :                                                                                         const String& restfreq,
     949             :                                                                                         const String& outframe,
     950             :                                                                                         const String& veltype,
     951             :                                                                                         String& t_mode,
     952             :                                                                                         String& t_outframe,
     953             :                                                                                         String& t_regridQuantity,
     954             :                                                                                         Double& t_restfreq,
     955             :                                                                                         String& t_regridInterpMeth,
     956             :                                                                                         Double& t_cstart,
     957             :                                                                                         Double& t_bandwidth,
     958             :                                                                                         Double& t_cwidth,
     959             :                                                                                         Bool& t_centerIsStart,
     960             :                                                                                         Bool& t_startIsEnd,
     961             :                                                                                         Int& t_nchan,
     962             :                                                                                         Int& t_width,
     963             :                                                                                         Int& t_start)
     964             : {
     965         831 :         Bool rstat(false);
     966             : 
     967             :         try {
     968             : 
     969         831 :                 casacore::QuantumHolder qh;
     970         831 :                 String error;
     971             : 
     972         831 :                 t_mode = mode;
     973         831 :                 t_restfreq = 0.;
     974         831 :                 if (!restfreq.empty() && !(restfreq == "[]"))
     975             :                 {
     976         493 :                         if (qh.fromString(error, restfreq))
     977             :                         {
     978         493 :                                 t_restfreq = qh.asQuantity().getValue("Hz");
     979             :                         }
     980             :                         else
     981             :                         {
     982           0 :                                 os << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
     983           0 :                                                 << "restfreq: " << error << LogIO::POST;
     984           0 :                                 return false;
     985             :                         }
     986             :                 }
     987             : 
     988             :                 // Determine grid
     989         831 :                 t_cstart = -9e99; // Default value indicating that the original start of the SPW should be used
     990         831 :                 t_bandwidth = -1.; // Default value indicating that the original width of the SPW should be used
     991         831 :                 t_cwidth = -1.; // Default value indicating that the original channel width of the SPW should be used
     992         831 :                 t_nchan = -1;
     993         831 :                 t_width = 0;
     994         831 :                 t_start = -1;
     995         831 :                 t_startIsEnd = false;   // false means that start specifies the lower end in frequency (default)
     996             :                                                                 // true means that start specifies the upper end in frequency
     997             : 
     998             :                 // start was set
     999         831 :                 if (!start.empty() && !(start == "[]"))
    1000             :                 {
    1001         802 :                         if (t_mode == "channel")
    1002             :                         {
    1003         696 :                                 t_start = atoi(start.c_str());
    1004             :                         }
    1005         802 :                         if (t_mode == "channel_b")
    1006             :                         {
    1007           2 :                                 t_cstart = Double(atoi(start.c_str()));
    1008             :                         }
    1009         800 :                         else if (t_mode == "frequency")
    1010             :                         {
    1011          70 :                                 if (qh.fromString(error, start))
    1012             :                                 {
    1013          70 :                                         t_cstart = qh.asQuantity().getValue("Hz");
    1014             :                                 }
    1015             :                                 else
    1016             :                                 {
    1017           0 :                                         os << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
    1018           0 :                                                         << "start: " << error << LogIO::POST;
    1019           0 :                                         return false;
    1020             :                                 }
    1021             :                         }
    1022         730 :                         else if (t_mode == "velocity")
    1023             :                         {
    1024          34 :                                 if (qh.fromString(error, start))
    1025             :                                 {
    1026          34 :                                         t_cstart = qh.asQuantity().getValue("m/s");
    1027             :                                 }
    1028             :                                 else
    1029             :                                 {
    1030           0 :                                         os << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
    1031           0 :                                                         << "start: " << error << LogIO::POST;
    1032           0 :                                         return false;
    1033             :                                 }
    1034             :                         }
    1035             :                 }
    1036             : 
    1037             :                 // channel width was set
    1038         831 :                 if (!width.empty() && !(width == "[]"))
    1039             :                 {
    1040         802 :                         if (t_mode == "channel")
    1041             :                         {
    1042         696 :                                 Int w = atoi(width.c_str());
    1043         696 :                                 t_width = abs(w);
    1044         696 :                                 if (w < 0)
    1045             :                                 {
    1046           4 :                                         t_startIsEnd = true;
    1047             :                                 }
    1048             :                         }
    1049         106 :                         else if (t_mode == "channel_b")
    1050             :                         {
    1051           2 :                                 Double w = atoi(width.c_str());
    1052           2 :                                 t_cwidth = abs(w);
    1053           2 :                                 if (w < 0)
    1054             :                                 {
    1055           1 :                                         t_startIsEnd = true;
    1056             :                                 }
    1057             :                         }
    1058         104 :                         else if (t_mode == "frequency")
    1059             :                         {
    1060          66 :                                 if (qh.fromString(error, width))
    1061             :                                 {
    1062          66 :                                         Double w = qh.asQuantity().getValue("Hz");
    1063          66 :                                         t_cwidth = abs(w);
    1064          66 :                                         if (w < 0)
    1065             :                                         {
    1066           8 :                                                 t_startIsEnd = true;
    1067             :                                         }
    1068             :                                 }
    1069             :                                 else
    1070             :                                 {
    1071           0 :                                         os << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
    1072           0 :                                                         << "width: " << error << LogIO::POST;
    1073           0 :                                         return false;
    1074             :                                 }
    1075             :                         }
    1076          38 :                         else if (t_mode == "velocity")
    1077             :                         {
    1078          38 :                                 if (qh.fromString(error, width))
    1079             :                                 {
    1080          38 :                                         Double w = qh.asQuantity().getValue("m/s");
    1081          38 :                                         t_cwidth = abs(w);
    1082          38 :                                         if (w >= 0)
    1083             :                                         {
    1084          29 :                                                 t_startIsEnd = true;
    1085             :                                         }
    1086             :                                 }
    1087             :                                 else
    1088             :                                 {
    1089           0 :                                         os << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
    1090           0 :                                                         << "width: " << error << LogIO::POST;
    1091           0 :                                         return false;
    1092             :                                 }
    1093             :                         }
    1094             :                 }
    1095             :                 // width was not set
    1096             :                 else
    1097             :                 {
    1098             :                         // For the velocity mode the default t_startIsEnd is true if the sign of width is not known
    1099          29 :                         if (t_mode == "velocity")
    1100             :                         {
    1101          15 :                                 t_startIsEnd = true;
    1102             :                         }
    1103             :                 }
    1104             : 
    1105             :                 // Number of output channels was set
    1106         831 :                 if (nchan > 0)
    1107             :                 {
    1108         267 :                         if (t_mode == "channel_b")
    1109             :                         {
    1110           1 :                                 if (t_cwidth > 0)
    1111             :                                 {
    1112           1 :                                         t_bandwidth = Double(nchan * t_cwidth);
    1113             :                                 }
    1114             :                                 else
    1115             :                                 {
    1116           0 :                                         t_bandwidth = Double(nchan);
    1117             :                                 }
    1118             :                         }
    1119             :                         else
    1120             :                         {
    1121         266 :                                 t_nchan = nchan;
    1122             :                         }
    1123             :                 }
    1124             : 
    1125         831 :                 if (t_mode == "channel")
    1126             :                 {
    1127         696 :                         t_regridQuantity = "freq";
    1128             :                 }
    1129         135 :                 else if (t_mode == "channel_b")
    1130             :                 {
    1131           2 :                         t_regridQuantity = "chan";
    1132             :                 }
    1133         133 :                 else if (t_mode == "frequency")
    1134             :                 {
    1135          80 :                         t_regridQuantity = "freq";
    1136             :                 }
    1137          53 :                 else if (t_mode == "velocity")
    1138             :                 {
    1139          53 :                         if (t_restfreq == 0.)
    1140             :                         {
    1141           0 :                                 os << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
    1142             :                                                 << "Need to set restfreq in velocity mode."
    1143           0 :                                                 << LogIO::POST;
    1144           0 :                                 return false;
    1145             :                         }
    1146             : 
    1147          53 :                         t_regridQuantity = "vrad";
    1148          53 :                         if (veltype == "optical")
    1149             :                         {
    1150           8 :                                 t_regridQuantity = "vopt";
    1151             :                         }
    1152          45 :                         else if (veltype != "radio")
    1153             :                         {
    1154           4 :                                 os << LogIO::WARN << LogOrigin("MSTransformRegridder", __FUNCTION__)
    1155             :                                                 << "Invalid velocity type " << veltype
    1156           4 :                                                 << ", setting type to \"radio\"" << LogIO::POST;
    1157             :                         }
    1158             : 
    1159             :                 }
    1160             :                 else
    1161             :                 {
    1162           0 :                         os << LogIO::WARN << LogOrigin("MSTransformRegridder", __FUNCTION__)
    1163           0 :                                         << "Invalid mode " << t_mode << LogIO::POST;
    1164           0 :                         return false;
    1165             :                 }
    1166             : 
    1167         831 :                 t_outframe = outframe;
    1168         831 :                 t_regridInterpMeth = interp;
    1169         831 :                 t_centerIsStart = true;
    1170             : 
    1171             :                 // End prepare re-gridding parameters
    1172         831 :                 rstat = true;
    1173             : 
    1174         831 :         }
    1175           0 :         catch (AipsError x)
    1176             :         {
    1177           0 :                 os << LogIO::SEVERE << LogOrigin("MSTransformRegridder", __FUNCTION__)
    1178           0 :                                 << "Exception Reported: " << x.getMesg() << LogIO::POST;
    1179           0 :                 rstat = false;
    1180           0 :         }
    1181             : 
    1182         831 :         return rstat;
    1183             : }
    1184             : 
    1185             : // -----------------------------------------------------------------------
    1186             : // Calculate the final new channel boundaries from the re-regridding parameters and
    1187             : // the old channel boundaries (already transformed to the desired reference frame).
    1188             : // Returns false if input parameters were invalid and no useful boundaries could be created
    1189             : // -----------------------------------------------------------------------
    1190         831 : Bool MSTransformRegridder::regridChanBounds(    Vector<Double>& newChanLoBound,
    1191             :                                                                                                 Vector<Double>& newChanHiBound,
    1192             :                                                                                                 const Double regridCenterC,
    1193             :                                                                                                 const Double regridBandwidth,
    1194             :                                                                                                 const Double regridChanWidthC,
    1195             :                                                                                                 const Double regridVeloRestfrq,
    1196             :                                                                                                 const String regridQuant,
    1197             :                                                                                                 const Vector<Double>& transNewXinC,
    1198             :                                                                                                 const Vector<Double>& transCHAN_WIDTHC,
    1199             :                                                                                                 String& message,
    1200             :                                                                                                 const Bool centerIsStartC,
    1201             :                                                                                                 const Bool startIsEndC,
    1202             :                                                                                                 const Int nchanC,
    1203             :                                                                                                 const Int width,
    1204             :                                                                                                 const Int startC)
    1205             : {
    1206         831 :         ostringstream oss;
    1207             : 
    1208         831 :         Vector<Double> transNewXin(transNewXinC);
    1209         831 :         Vector<Double> transCHAN_WIDTH(transCHAN_WIDTHC);
    1210         831 :         Bool centerIsStart = centerIsStartC;
    1211         831 :         Bool startIsEnd = startIsEndC;
    1212         831 :         Double regridChanWidth = regridChanWidthC;
    1213         831 :         Double regridCenter = regridCenterC;
    1214         831 :         Int nchan = nchanC;
    1215         831 :         Int start = startC;
    1216             : 
    1217         831 :         Int oldNUM_CHAN = transNewXin.size();
    1218             : 
    1219             :         // Detect spectral windows defined with descending frequency
    1220         831 :         Bool isDescending = false;
    1221      282518 :         for (uInt i = 1; i < transNewXin.size(); i++)
    1222             :         {
    1223      281687 :                 if (transNewXin(i) < transNewXin(i - 1))
    1224             :                 {
    1225       24396 :                         isDescending = true;
    1226             :                 }
    1227             :                 // i.e. descending was detected but now we encounter ascending
    1228      257291 :                 else if (isDescending)
    1229             :                 {
    1230           0 :                         oss << "Channel frequencies are neither in ascending nor in descending order. Cannot process.";
    1231           0 :                         message = oss.str();
    1232           0 :                         return false;
    1233             :                 }
    1234             :         }
    1235             : 
    1236             :         // Need to reverse the order for processing and later reverse the result
    1237         831 :         if (isDescending)
    1238             :         {
    1239          36 :                 uInt n = transNewXin.size();
    1240          36 :                 Vector<Double> tempF, tempW;
    1241          36 :                 tempF.assign(transNewXin);
    1242          36 :                 tempW.assign(transCHAN_WIDTH);
    1243       24468 :                 for (uInt i = 0; i < n; i++)
    1244             :                 {
    1245       24432 :                         transNewXin(i) = tempF(n - 1 - i);
    1246       24432 :                         transCHAN_WIDTH(i) = tempW(n - 1 - i);
    1247             :                 }
    1248             : 
    1249             :                 // Also need to adjust the start values
    1250          36 :                 if (startC >= 0)
    1251             :                 {
    1252          22 :                         start = n - 1 - startC;
    1253          22 :                         if (centerIsStartC)
    1254             :                         {
    1255          22 :                                 startIsEnd = !startIsEnd;
    1256             :                         }
    1257             :                 }
    1258          36 :         }
    1259             : 
    1260             :         // Verify regridCenter, regridBandwidth, and regridChanWidth
    1261             :         // Note: these are in the units corresponding to regridQuant!
    1262             : 
    1263         831 :         if (regridQuant == "chan")
    1264             :         {
    1265             :                 // Channel numbers ...
    1266           2 :                 Int regridCenterChan = -1;
    1267           2 :                 Int regridBandwidthChan = -1;
    1268           2 :                 Int regridChanWidthChan = -1;
    1269             : 
    1270             :                 // Not set
    1271           2 :                 if (regridCenter < -1E30)
    1272             :                 {
    1273             :                         // Find channel center closest to center of bandwidth
    1274           0 :                         lDouble BWCenterF = (transNewXin[0] + transNewXin[oldNUM_CHAN - 1]) / 2.;
    1275           0 :                         for (Int i = 0; i < oldNUM_CHAN; i++)
    1276             :                         {
    1277           0 :                                 if (transNewXin[i] >= BWCenterF)
    1278             :                                 {
    1279           0 :                                         regridCenterChan = i;
    1280           0 :                                         break;
    1281             :                                 }
    1282             :                         }
    1283           0 :                         centerIsStart = false;
    1284             :                 }
    1285             :                 // Valid input
    1286           2 :                 else if (0. <= regridCenter && regridCenter < Double(oldNUM_CHAN))
    1287             :                 {
    1288           2 :                         regridCenterChan = (Int) floor(regridCenter);
    1289             :                 }
    1290             :                 // Invalid
    1291             :                 else
    1292             :                 {
    1293           0 :                         if (centerIsStart)
    1294             :                         {
    1295           0 :                                 oss << "SPW start ";
    1296             :                         }
    1297             :                         else
    1298             :                         {
    1299           0 :                                 oss << "SPW center ";
    1300             :                         }
    1301           0 :                         oss << regridCenter << " outside valid range which is " << 0 << " - " << oldNUM_CHAN - 1 << ".";
    1302           0 :                         message = oss.str();
    1303           0 :                         return false;
    1304             :                 }
    1305             : 
    1306             :                 // Not set or nchan set
    1307           2 :                 if (regridBandwidth <= 0. || nchan > 0)
    1308             :                 {
    1309           1 :                         if (nchan > 0)
    1310             :                         {
    1311           0 :                                 regridBandwidthChan = nchan;
    1312             :                         }
    1313             :                         else
    1314             :                         {
    1315           1 :                                 regridBandwidthChan = oldNUM_CHAN;
    1316             :                         }
    1317             :                 }
    1318             :                 else
    1319             :                 {
    1320           1 :                         regridBandwidthChan = (Int) floor(regridBandwidth);
    1321             :                 }
    1322             : 
    1323           2 :                 if (centerIsStart)
    1324             :                 {
    1325           2 :                         if (startIsEnd)
    1326             :                         {
    1327           1 :                                 regridCenterChan = regridCenterChan - regridBandwidthChan / 2;
    1328             :                         }
    1329             :                         else
    1330             :                         {
    1331           1 :                                 regridCenterChan = regridCenterChan + regridBandwidthChan / 2;
    1332             :                         }
    1333           2 :                         centerIsStart = false;
    1334             :                 }
    1335             : 
    1336             :                 // Center too close to lower edge
    1337           2 :                 if (regridCenterChan - regridBandwidthChan / 2 < 0)
    1338             :                 {
    1339           0 :                         regridBandwidthChan = 2 * regridCenterChan + 1;
    1340           0 :                         oss << " *** Requested output SPW width too large." << endl;
    1341             :                 }
    1342             :                 // Center too close to upper edge
    1343           2 :                 if (oldNUM_CHAN < regridCenterChan + regridBandwidthChan / 2)
    1344             :                 {
    1345           0 :                         regridBandwidthChan = 2 * (oldNUM_CHAN - regridCenterChan);
    1346           0 :                         oss << " *** Requested output SPW width too large." << endl;
    1347             :                 }
    1348             : 
    1349           2 :                 if (regridChanWidth < 1.)
    1350             :                 {
    1351           0 :                         regridChanWidthChan = 1;
    1352             :                 }
    1353           2 :                 else if (regridChanWidth > Double(regridBandwidthChan))
    1354             :                 {
    1355           0 :                         regridChanWidthChan = regridBandwidthChan; // i.e. SPW = a single channel
    1356           0 :                         oss << " *** Requested output channel width too large. Adjusted to maximum possible value." << endl;
    1357             :                 }
    1358             :                 // Valid input
    1359             :                 else
    1360             :                 {
    1361           2 :                         regridChanWidthChan = (Int) floor(regridChanWidth);
    1362           2 :                         if (nchan > 0)
    1363             :                         {
    1364           0 :                                 regridBandwidthChan = nchan * regridChanWidthChan;
    1365             :                         }
    1366             :                 }
    1367             : 
    1368           2 :                 if (regridBandwidthChan != floor(regridBandwidth))
    1369             :                 {
    1370           1 :                         oss << " *** Output SPW width set to " << regridBandwidthChan << " original channels" << endl;
    1371           1 :                         oss << "     in an attempt to keep center of output SPW close to center of requested SPW." << endl;
    1372             :                 }
    1373             : 
    1374             :                 // Calculate newChanLoBound and newChanHiBound from
    1375             :                 // regridCenterChan, regridBandwidthChan, and regridChanWidthChan
    1376           2 :                 Int bwLowerEndChan = regridCenterChan - regridBandwidthChan / 2;
    1377           2 :                 Int bwUpperEndChan = bwLowerEndChan + regridBandwidthChan - 1;
    1378           2 :                 Int numNewChanDown = 0;
    1379           2 :                 Int numNewChanUp = 0;
    1380             : 
    1381             :                 // Only one new channel
    1382           2 :                 if (regridChanWidthChan == regridBandwidthChan)
    1383             :                 {
    1384           0 :                         newChanLoBound.resize(1);
    1385           0 :                         newChanHiBound.resize(1);
    1386           0 :                         newChanLoBound[0] = transNewXin[bwLowerEndChan] - transCHAN_WIDTH[bwLowerEndChan] / 2.;
    1387           0 :                         newChanHiBound[0] = transNewXin[bwUpperEndChan] + transCHAN_WIDTH[bwUpperEndChan] / 2.;
    1388           0 :                         numNewChanUp = 1;
    1389             :                 }
    1390             :                 // Have more than one new channel
    1391             :                 else
    1392             :                 {
    1393             :                         // Need to accommodate the possibility that the original channels are not contiguous!
    1394             : 
    1395             :                         // The numbers of the Channels from which the lower bounds will be taken for the new channels
    1396           2 :                         vector<Int> loNCBup;
    1397             :                         // Starting from the central channel going up
    1398           2 :                         vector<Int> hiNCBup; // The numbers of the Channels from which the high
    1399             :                         // Bounds will be taken for the new channels starting from the central channel going up
    1400           2 :                         vector<Int> loNCBdown; // The numbers of the Channels from which the
    1401             :                         // Lower bounds will be taken for the new channels starting from the central channel going down
    1402           2 :                         vector<Int> hiNCBdown;    // The numbers of the Channels from which the high bounds will be taken
    1403             :                                                                         // for the new channels starting from the central channel going down.
    1404             :                                                                         // Want to keep the center of the center channel at the center of the new center
    1405             :                                                                         // channel if the bandwidth is an odd multiple of the new channel width
    1406             :                                                                         // otherwise the center channel is the lower edge of the new center channel
    1407             :                         Int startChan;
    1408           2 :                         lDouble tnumChan = regridBandwidthChan / regridChanWidthChan;
    1409           2 :                         if ((Int) tnumChan % 2 != 0)
    1410             :                         {
    1411             :                                 // Odd multiple
    1412           1 :                                 startChan = regridCenterChan - regridChanWidthChan / 2;
    1413             :                         }
    1414             :                         else
    1415             :                         {
    1416           1 :                                 startChan = regridCenterChan;
    1417             :                         }
    1418             : 
    1419             :                         // Upper half
    1420        1219 :                         for (Int i = startChan; i <= bwUpperEndChan; i += regridChanWidthChan)
    1421             :                         {
    1422        1217 :                                 loNCBup.push_back(i);
    1423        1217 :                                 if (i + regridChanWidthChan - 1 <= bwUpperEndChan)
    1424             :                                 {
    1425             :                                         // Can go one more normal step up
    1426        1217 :                                         hiNCBup.push_back(i + regridChanWidthChan - 1);
    1427             :                                 }
    1428             :                                 else
    1429             :                                 {
    1430             :                                         // Create narrower channels at the edges if necessary
    1431           0 :                                         oss             << " *** Last channel at upper edge of new SPW made only "
    1432           0 :                                                         << bwUpperEndChan - i + 1
    1433           0 :                                                         << " original channels wide to fit given total bandwidth."
    1434           0 :                                                         << endl;
    1435           0 :                                         hiNCBup.push_back(bwUpperEndChan);
    1436             :                                 }
    1437             :                         }
    1438             : 
    1439             :                         // Lower half
    1440        1218 :                         for (Int i = startChan - 1; i >= bwLowerEndChan; i -= regridChanWidthChan)
    1441             :                         {
    1442        1216 :                                 hiNCBdown.push_back(i);
    1443        1216 :                                 if (i - regridChanWidthChan + 1 >= bwLowerEndChan)
    1444             :                                 {
    1445             :                                         // Can go one more normal step down
    1446        1216 :                                         loNCBdown.push_back(i - regridChanWidthChan + 1);
    1447             :                                 }
    1448             :                                 else
    1449             :                                 {
    1450             :                                         // Create narrower channels at the edges if necessary
    1451           0 :                                         oss             << " *** First channel at lower edge of new SPW made only "
    1452           0 :                                                         << i - bwLowerEndChan + 1
    1453           0 :                                                         << " original channels wide to fit given total bandwidth."
    1454           0 :                                                         << endl;
    1455           0 :                                         loNCBdown.push_back(bwLowerEndChan);
    1456             :                                 }
    1457             :                         }
    1458             : 
    1459             :                         // The number of channels below the central one
    1460           2 :                         numNewChanDown = loNCBdown.size();
    1461             : 
    1462             :                         // The number of channels above and including the central one
    1463           2 :                         numNewChanUp = loNCBup.size();
    1464             : 
    1465           2 :                         newChanLoBound.resize(numNewChanDown + numNewChanUp);
    1466           2 :                         newChanHiBound.resize(numNewChanDown + numNewChanUp);
    1467        1218 :                         for (Int i = 0; i < numNewChanDown; i++)
    1468             :                         {
    1469        1216 :                                 Int k = numNewChanDown - i - 1; // Need to assign in reverse
    1470        1216 :                                 newChanLoBound[i] = transNewXin[loNCBdown[k]] - transCHAN_WIDTH[loNCBdown[k]] / 2.;
    1471        1216 :                                 newChanHiBound[i] = transNewXin[hiNCBdown[k]] + transCHAN_WIDTH[hiNCBdown[k]] / 2.;
    1472             :                         }
    1473             : 
    1474        1219 :                         for (Int i = 0; i < numNewChanUp; i++)
    1475             :                         {
    1476        1217 :                                 newChanLoBound[i + numNewChanDown] = transNewXin[loNCBup[i]] - transCHAN_WIDTH[loNCBup[i]] / 2.;
    1477        1217 :                                 newChanHiBound[i + numNewChanDown] = transNewXin[hiNCBup[i]] + transCHAN_WIDTH[hiNCBup[i]] / 2.;
    1478             :                         }
    1479           2 :                 } // end if
    1480             : 
    1481           2 :                 oss     << " New channels defined based on original channels" << endl
    1482           2 :                                 << " Central channel contains original channel "
    1483           2 :                                 << regridCenterChan << endl << " Channel width = "
    1484           2 :                                 << regridChanWidthChan << " original channels" << endl
    1485           2 :                                 << " Total width of SPW = " << regridBandwidthChan
    1486           2 :                                 << " original channels == " << numNewChanDown + numNewChanUp
    1487           2 :                                 << " new channels" << endl;
    1488             : 
    1489           2 :                 uInt nc = newChanLoBound.size();
    1490           2 :                 oss << " Total width of SPW (in output frame) = "
    1491           2 :                         << newChanHiBound[nc- 1] - newChanLoBound[0] << " Hz" << endl;
    1492           2 :                 oss << " Lower edge = " << std::setprecision(9) << newChanLoBound[0] << " Hz,"
    1493           2 :                     << " upper edge = " << std::setprecision(9) << newChanHiBound[nc - 1] << " Hz" << endl;
    1494             : 
    1495           2 :                 if (isDescending)
    1496             :                 {
    1497           0 :                         Vector<Double> tempL, tempU;
    1498           0 :                         tempL.assign(newChanLoBound);
    1499           0 :                         tempU.assign(newChanHiBound);
    1500           0 :                         for (uInt i = 0; i < nc; i++)
    1501             :                         {
    1502           0 :                                 newChanLoBound(i) = tempL(nc - 1 - i);
    1503           0 :                                 newChanHiBound(i) = tempU(nc - 1 - i);
    1504             :                         }
    1505           0 :                 }
    1506             : 
    1507           2 :                 message = oss.str();
    1508             : 
    1509           2 :                 return true;
    1510             :         }
    1511             :         // We operate on real numbers /////////////////
    1512             :         else
    1513             :         {
    1514             :                 // First transform them to frequencies
    1515         829 :                 lDouble regridCenterF = -1.; // Initialize as "not set"
    1516         829 :                 lDouble regridBandwidthF = -1.;
    1517         829 :                 lDouble regridChanWidthF = -1.;
    1518             : 
    1519         829 :                 if (regridQuant == "vrad")
    1520             :                 {
    1521             :                         // radio velocity need restfrq
    1522          45 :                         if (regridVeloRestfrq < -1E30)  // means "not set"
    1523             :                         {
    1524           0 :                                 oss << "Parameter \"restfreq\" needs to be set if regrid_quantity==vrad. Cannot proceed with regridSpw ...";
    1525           0 :                                 message = oss.str();
    1526           0 :                                 return false;
    1527             :                         }
    1528          45 :                         else if (regridVeloRestfrq < 0. || regridVeloRestfrq > 1E30)
    1529             :                         {
    1530           0 :                                 oss << "Parameter \"restfreq\" value " << regridVeloRestfrq << " is invalid.";
    1531           0 :                                 message = oss.str();
    1532           0 :                                 return false;
    1533             :                         }
    1534             : 
    1535             :                         lDouble regridCenterVel;
    1536          45 :                         if (regridCenter > -C::c)
    1537             :                         {
    1538             :                                 // (We deal with invalid values later)
    1539          28 :                                 if (centerIsStart)
    1540             :                                 {
    1541             :                                         Double tcWidth;
    1542          28 :                                         if (regridChanWidth > 0.)
    1543             :                                         {
    1544          17 :                                                 tcWidth = regridChanWidth;
    1545             :                                         }
    1546             :                                         else
    1547             :                                         {
    1548          11 :                                                 tcWidth = vrad( transNewXin[0] - transCHAN_WIDTH[0] / 2.,
    1549          11 :                                                                                 regridVeloRestfrq) - vrad(
    1550          11 :                                                                                 transNewXin[0] + transCHAN_WIDTH[0] / 2.,
    1551             :                                                                                 regridVeloRestfrq);
    1552             :                                         }
    1553             : 
    1554             :                                         // start is the center of the last channel (in freq)
    1555          28 :                                         if (startIsEnd)
    1556             :                                         {
    1557          23 :                                                 regridCenter -= tcWidth / 2.;
    1558             :                                         }
    1559             :                                         // start is the center of the first channel (in freq)
    1560             :                                         else
    1561             :                                         {
    1562           5 :                                                 regridCenter += tcWidth / 2.;
    1563             :                                         }
    1564             :                                 }
    1565             : 
    1566          28 :                                 regridCenterF = freq_from_vrad(regridCenter, regridVeloRestfrq);
    1567             : 
    1568          28 :                                 regridCenterVel = regridCenter;
    1569             :                         }
    1570             :                         // center was not specified
    1571             :                         else
    1572             :                         {
    1573          17 :                                 regridCenterF = (transNewXin[0] + transNewXin[oldNUM_CHAN - 1])/ 2.;
    1574          17 :                                 regridCenterVel = vrad(regridCenterF, regridVeloRestfrq);
    1575          17 :                                 centerIsStart = false;
    1576             :                         }
    1577          45 :                         if (nchan > 0)
    1578             :                         {
    1579          40 :                                 if (regridChanWidth > 0.)
    1580             :                                 {
    1581          27 :                                         lDouble chanUpperEdgeF = freq_from_vrad(regridCenterVel - regridChanWidth / 2.,regridVeloRestfrq);
    1582          27 :                                         regridChanWidthF = 2. * (chanUpperEdgeF - regridCenterF);
    1583             :                                 }
    1584             :                                 // take channel width from first channel
    1585             :                                 else
    1586             :                                 {
    1587          13 :                                         regridChanWidthF = transCHAN_WIDTH[0];
    1588             :                                 }
    1589             : 
    1590          40 :                                 regridBandwidthF = nchan * regridChanWidthF;
    1591             :                                 // Can convert start to center
    1592          40 :                                 if (centerIsStart)
    1593             :                                 {
    1594          28 :                                         if (startIsEnd)
    1595             :                                         {
    1596          23 :                                                 regridCenterF = regridCenterF - regridBandwidthF / 2.;
    1597             :                                         }
    1598             :                                         else
    1599             :                                         {
    1600           5 :                                                 regridCenterF = regridCenterF + regridBandwidthF / 2.;
    1601             :                                         }
    1602          28 :                                         centerIsStart = false;
    1603             :                                 }
    1604             :                         }
    1605           5 :                         else if (regridBandwidth > 0.)
    1606             :                         {
    1607             :                                 // Can convert start to center
    1608           0 :                                 if (centerIsStart)
    1609             :                                 {
    1610           0 :                                         if (startIsEnd)
    1611             :                                         {
    1612           0 :                                                 regridCenterVel = regridCenter + regridBandwidth / 2.;
    1613             :                                         }
    1614             :                                         else
    1615             :                                         {
    1616           0 :                                                 regridCenterVel = regridCenter - regridBandwidth / 2.;
    1617             :                                         }
    1618           0 :                                         regridCenterF = freq_from_vrad(regridCenterVel,regridVeloRestfrq);
    1619           0 :                                         centerIsStart = false;
    1620             :                                 }
    1621           0 :                                 lDouble bwUpperEndF = freq_from_vrad(regridCenterVel - regridBandwidth / 2.,regridVeloRestfrq);
    1622           0 :                                 regridBandwidthF = 2. * (bwUpperEndF - regridCenterF);
    1623             :                         }
    1624             : 
    1625          45 :                         if (regridChanWidth > 0. && regridChanWidthF < 0.)
    1626             :                         {
    1627           4 :                                 lDouble chanUpperEdgeF = freq_from_vrad(regridCenterVel - regridChanWidth / 2.,regridVeloRestfrq);
    1628           4 :                                 regridChanWidthF = 2. * (chanUpperEdgeF - freq_from_vrad(regridCenterVel, regridVeloRestfrq));
    1629             :                         }
    1630             :                 }
    1631         784 :                 else if (regridQuant == "vopt")
    1632             :                 {
    1633             :                         // Optical velocity need restfrq
    1634           8 :                         if (regridVeloRestfrq < -1E30) // means "not set"
    1635             :                         {
    1636           0 :                                 oss << "Parameter \"restfreq\" needs to be set if regrid_quantity==vopt. Cannot proceed with regridSpw ...";
    1637           0 :                                 message = oss.str();
    1638           0 :                                 return false;
    1639             :                         }
    1640           8 :                         else if (regridVeloRestfrq <= 0. || regridVeloRestfrq > 1E30)
    1641             :                         {
    1642           0 :                                 oss << "Parameter \"restfreq\" value " << regridVeloRestfrq << " is invalid.";
    1643           0 :                                 message = oss.str();
    1644           0 :                                 return false;
    1645             :                         }
    1646             : 
    1647             :                         lDouble regridCenterVel;
    1648           8 :                         if (regridCenter > -C::c)
    1649             :                         {
    1650           6 :                                 if (centerIsStart)
    1651             :                                 {
    1652             :                                         Double tcWidth;
    1653           6 :                                         if (regridChanWidth > 0.)
    1654             :                                         {
    1655           6 :                                                 tcWidth = regridChanWidth;
    1656             :                                         }
    1657             :                                         else
    1658             :                                         {
    1659           0 :                                                 tcWidth = vopt( transNewXin[0] - transCHAN_WIDTH[0] / 2.,
    1660           0 :                                                                                 regridVeloRestfrq) - vopt(
    1661           0 :                                                                                 transNewXin[0] + transCHAN_WIDTH[0] / 2.,
    1662             :                                                                                 regridVeloRestfrq);
    1663             :                                         }
    1664             : 
    1665             :                                         // start is the center of the last channel (in freq)
    1666           6 :                                         if (startIsEnd)
    1667             :                                         {
    1668           5 :                                                 regridCenter -= tcWidth / 2.;
    1669             :                                         }
    1670             :                                         // start is the center of the first channel (in freq)
    1671             :                                         else
    1672             :                                         {
    1673           1 :                                                 regridCenter += tcWidth / 2.;
    1674             :                                         }
    1675             :                                 }
    1676             : 
    1677             :                                 // (We deal with invalid values later)
    1678           6 :                                 regridCenterF = freq_from_vopt(regridCenter, regridVeloRestfrq);
    1679           6 :                                 regridCenterVel = regridCenter;
    1680             :                         }
    1681             :                         // Center was not specified
    1682             :                         else
    1683             :                         {
    1684           2 :                                 regridCenterF = (transNewXin[0] - transCHAN_WIDTH[0]
    1685           2 :                                                  + transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1]) / 2.;
    1686           2 :                                 regridCenterVel = vopt(regridCenterF, regridVeloRestfrq);
    1687           2 :                                 centerIsStart = false;
    1688             :                         }
    1689             : 
    1690           8 :                         if (nchan > 0)
    1691             :                         {
    1692             :                                 lDouble cw;
    1693           6 :                                 lDouble divbytwo = 0.5;
    1694           6 :                                 if (centerIsStart)
    1695             :                                 {
    1696           6 :                                         divbytwo = 1.;
    1697             :                                 }
    1698           6 :                                 if (regridChanWidth > 0.)
    1699             :                                 {
    1700           6 :                                         cw = regridChanWidth;
    1701             :                                 }
    1702             :                                 // Determine channel width from first channel
    1703             :                                 else
    1704             :                                 {
    1705           0 :                                         lDouble upEdge = vopt(transNewXin[0] - transCHAN_WIDTH[0],regridVeloRestfrq);
    1706           0 :                                         lDouble loEdge = vopt(transNewXin[0] + transCHAN_WIDTH[0],regridVeloRestfrq);
    1707           0 :                                         cw = abs(upEdge - loEdge);
    1708             :                                 }
    1709           6 :                                 lDouble bwUpperEndF = 0.;
    1710             : 
    1711             :                                 // Start is end in velocity
    1712           6 :                                 if (centerIsStart && !startIsEnd)
    1713             :                                 {
    1714           1 :                                         bwUpperEndF = freq_from_vopt(regridCenterVel - (lDouble) nchan * cw * divbytwo,regridVeloRestfrq);
    1715             :                                 }
    1716             :                                 else
    1717             :                                 {
    1718           5 :                                         bwUpperEndF = freq_from_vopt(regridCenterVel + (lDouble) nchan * cw * divbytwo,regridVeloRestfrq);
    1719             :                                 }
    1720             : 
    1721           6 :                                 regridBandwidthF = abs(bwUpperEndF - regridCenterF) / divbytwo;
    1722             : 
    1723             :                                 // Can convert start to center
    1724           6 :                                 if (centerIsStart)
    1725             :                                 {
    1726           6 :                                         if (startIsEnd)
    1727             :                                         {
    1728           5 :                                                 regridCenterVel = regridCenterVel + (lDouble) nchan * cw / 2.;
    1729             :                                         }
    1730             :                                         else
    1731             :                                         {
    1732           1 :                                                 regridCenterVel = regridCenterVel - (lDouble) nchan * cw / 2.;
    1733             :                                         }
    1734             : 
    1735           6 :                                         regridCenterF = freq_from_vopt(regridCenterVel,regridVeloRestfrq);
    1736           6 :                                         centerIsStart = false;
    1737             :                                 }
    1738           6 :                                 nchan = 0; // indicate that nchan should not be used in the following
    1739             :                         }
    1740           2 :                         else if (regridBandwidth > 0.)
    1741             :                         {
    1742             :                                 // can convert start to center
    1743           0 :                                 if (centerIsStart)
    1744             :                                 {
    1745           0 :                                         if (startIsEnd)
    1746             :                                         {
    1747           0 :                                                 regridCenterVel = regridCenter + regridBandwidth / 2.;
    1748             :                                         }
    1749             :                                         else
    1750             :                                         {
    1751           0 :                                                 regridCenterVel = regridCenter - regridBandwidth / 2.;
    1752             :                                         }
    1753           0 :                                         regridCenterF = freq_from_vopt(regridCenterVel, regridVeloRestfrq);
    1754           0 :                                         centerIsStart = false;
    1755             :                                 }
    1756             : 
    1757           0 :                                 lDouble bwUpperEndF = freq_from_vopt(regridCenterVel - regridBandwidth / 2.,regridVeloRestfrq);
    1758           0 :                                 regridBandwidthF = 2. * (bwUpperEndF - regridCenterF);
    1759             :                         }
    1760             : 
    1761           8 :                         if (regridChanWidth > 0. && regridChanWidthF < 0.)
    1762             :                         {
    1763           7 :                                 lDouble chanUpperEdgeF = freq_from_vopt(regridCenterVel - regridChanWidth / 2.,regridVeloRestfrq);
    1764           7 :                                 regridChanWidthF = 2. * (chanUpperEdgeF - freq_from_vopt(regridCenterVel, regridVeloRestfrq));
    1765             :                         }
    1766             :                 }
    1767         776 :                 else if (regridQuant == "freq")
    1768             :                 {
    1769             :                         // width parameter overrides regridChanWidth
    1770         776 :                         if (width > 0)
    1771             :                         {
    1772         696 :                                 regridChanWidth = width * transCHAN_WIDTH[0];
    1773             :                         }
    1774             : 
    1775         776 :                         if (start >= 0)
    1776             :                         {
    1777         696 :                                 Int firstChan = start;
    1778         696 :                                 if (start >= (Int) transNewXin.size())
    1779             :                                 {
    1780           1 :                                         oss << " *** Parameter start exceeds total number of channels which is "
    1781           1 :                                                 << transNewXin.size() << ". Set to 0." << endl;
    1782           1 :                                         firstChan = 0;
    1783           1 :                                         startIsEnd = false;
    1784             :                                 }
    1785             : 
    1786         696 :                                 if (startIsEnd)
    1787             :                                 {
    1788          24 :                                         regridCenter = transNewXin[firstChan] + transCHAN_WIDTH[firstChan] / 2.;
    1789             :                                 }
    1790             :                                 else
    1791             :                                 {
    1792         672 :                                         regridCenter = transNewXin[firstChan] - transCHAN_WIDTH[firstChan] / 2.;
    1793             :                                 }
    1794         696 :                                 centerIsStart = true;
    1795             :                         }
    1796             :                         else
    1797             :                         {
    1798             :                                 // start is the center of the first channel
    1799          80 :                                 if (centerIsStart)
    1800             :                                 {
    1801             :                                         Double tcWidth;
    1802          80 :                                         if (regridChanWidth > 0.)
    1803             :                                         {
    1804          66 :                                                 tcWidth = regridChanWidth;
    1805             :                                         }
    1806             :                                         else
    1807             :                                         {
    1808          14 :                                                 tcWidth = transCHAN_WIDTH[0];
    1809             :                                         }
    1810             : 
    1811          80 :                                         if (startIsEnd)
    1812             :                                         {
    1813           8 :                                                 regridCenter += tcWidth / 2.;
    1814             :                                         }
    1815             :                                         else
    1816             :                                         {
    1817          72 :                                                 regridCenter -= tcWidth / 2.;
    1818             :                                         }
    1819             :                                 }
    1820             :                         }
    1821         776 :                         regridCenterF = regridCenter;
    1822         776 :                         regridBandwidthF = regridBandwidth;
    1823         776 :                         regridChanWidthF = regridChanWidth;
    1824             :                 }
    1825           0 :                 else if (regridQuant == "wave") {
    1826             :                         // wavelength ...
    1827             :                         lDouble regridCenterWav;
    1828           0 :                         if (regridCenter > 0.)
    1829             :                         {
    1830           0 :                                 if (centerIsStart)
    1831             :                                 {
    1832             :                                         Double tcWidth;
    1833           0 :                                         if (regridChanWidth > 0.)
    1834             :                                         {
    1835           0 :                                                 tcWidth = regridChanWidth;
    1836             :                                         }
    1837             :                                         else
    1838             :                                         {
    1839           0 :                                                 tcWidth = lambda(transNewXin[0] - transCHAN_WIDTH[0] / 2.) -
    1840           0 :                                                                 lambda(transNewXin[0] + transCHAN_WIDTH[0]/ 2.);
    1841             :                                         }
    1842             : 
    1843             :                                         // start is the center of the last channel (in freq)
    1844           0 :                                         if (startIsEnd)
    1845             :                                         {
    1846           0 :                                                 regridCenter -= tcWidth / 2.;
    1847             :                                         }
    1848             :                                         // start is the center of the first channel (in freq)
    1849             :                                         else
    1850             :                                         {
    1851           0 :                                                 regridCenter += tcWidth / 2.;
    1852             :                                         }
    1853             :                                 }
    1854           0 :                                 regridCenterF = freq_from_lambda(regridCenter);
    1855           0 :                                 regridCenterWav = regridCenter;
    1856             :                         }
    1857             :                         // center was not specified
    1858             :                         else
    1859             :                         {
    1860           0 :                                 regridCenterF = (transNewXin[0] + transNewXin[oldNUM_CHAN - 1])/ 2.;
    1861           0 :                                 regridCenterWav = lambda(regridCenterF);
    1862           0 :                                 centerIsStart = false;
    1863             :                         }
    1864           0 :                         if (nchan > 0)
    1865             :                         {
    1866             :                                 lDouble cw;
    1867           0 :                                 lDouble divbytwo = 0.5;
    1868           0 :                                 if (centerIsStart)
    1869             :                                 {
    1870           0 :                                         divbytwo = 1.;
    1871             :                                 }
    1872             : 
    1873           0 :                                 if (regridChanWidth > 0.)
    1874             :                                 {
    1875           0 :                                         cw = regridChanWidth;
    1876             :                                 }
    1877             :                                 // Determine channel width from first channel
    1878             :                                 else
    1879             :                                 {
    1880           0 :                                         lDouble upEdge = lambda(transNewXin[0] - transCHAN_WIDTH[0]);
    1881           0 :                                         lDouble loEdge = lambda(transNewXin[0] + transCHAN_WIDTH[0]);
    1882           0 :                                         cw = abs(upEdge - loEdge);
    1883             :                                 }
    1884             : 
    1885           0 :                                 lDouble bwUpperEndF = 0.;
    1886           0 :                                 if (centerIsStart && !startIsEnd)
    1887             :                                 {
    1888           0 :                                         bwUpperEndF = freq_from_lambda(regridCenterWav - (lDouble) nchan * cw * divbytwo);
    1889             :                                 }
    1890             :                                 else
    1891             :                                 {
    1892           0 :                                         bwUpperEndF = freq_from_lambda(regridCenterWav + (lDouble) nchan * cw * divbytwo);
    1893             :                                 }
    1894             : 
    1895           0 :                                 regridBandwidthF = (bwUpperEndF - regridCenterF) / divbytwo;
    1896             : 
    1897             :                                 // Can convert start to center
    1898           0 :                                 if (centerIsStart)
    1899             :                                 {
    1900           0 :                                         if (startIsEnd)
    1901             :                                         {
    1902           0 :                                                 regridCenterWav = regridCenterWav + (lDouble) nchan* cw / 2.;
    1903             :                                         }
    1904             :                                         else
    1905             :                                         {
    1906           0 :                                                 regridCenterWav = regridCenterWav - (lDouble) nchan* cw / 2.;
    1907             :                                         }
    1908           0 :                                         regridCenterF = freq_from_lambda(regridCenterWav);
    1909           0 :                                         centerIsStart = false;
    1910             :                                 }
    1911           0 :                                 nchan = 0; // indicate that nchan should not be used in the following
    1912             : 
    1913             :                         }
    1914           0 :                         else if (regridBandwidth > 0. && regridBandwidth / 2.< regridCenterWav)
    1915             :                         {
    1916             :                                 // Can convert start to center
    1917           0 :                                 if (centerIsStart)
    1918             :                                 {
    1919           0 :                                         if (startIsEnd)
    1920             :                                         {
    1921           0 :                                                 regridCenterWav = regridCenter + regridBandwidth / 2.;
    1922             :                                         }
    1923             :                                         else
    1924             :                                         {
    1925           0 :                                                 regridCenterWav = regridCenter - regridBandwidth / 2.;
    1926             :                                         }
    1927           0 :                                         regridCenterF = freq_from_lambda(regridCenterWav);
    1928           0 :                                         centerIsStart = false;
    1929             :                                 }
    1930           0 :                                 lDouble bwUpperEndF = lambda(regridCenterWav - regridBandwidth / 2.);
    1931           0 :                                 regridBandwidthF = 2. * (bwUpperEndF - regridCenterF);
    1932             :                         }
    1933             : 
    1934           0 :                         if (regridChanWidth > 0. && regridChanWidth / 2. < regridCenterWav)
    1935             :                         {
    1936           0 :                                 lDouble chanUpperEdgeF = lambda(regridCenterWav - regridChanWidth / 2.);
    1937           0 :                                 regridChanWidthF = 2. * (chanUpperEdgeF - regridCenterF);
    1938             :                         }
    1939             :                 }
    1940             :                 else
    1941             :                 {
    1942           0 :                         oss << "Invalid value " << regridQuant << " for parameter \"mode\".";
    1943           0 :                         message = oss.str();
    1944           0 :                         return false;
    1945             :                 }
    1946             :                 // (transformation of regrid parameters to frequencies completed)
    1947             : 
    1948             :                 // Then determine the actually possible parameters
    1949             :                 lDouble theRegridCenterF;
    1950             :                 lDouble theRegridBWF;
    1951             :                 lDouble theCentralChanWidthF;
    1952             : 
    1953             :                 // For vrad and vopt also need to keep this adjusted value
    1954         829 :                 lDouble theChanWidthX = -1.;
    1955             : 
    1956         829 :                 if (regridCenterF < 0.) // means "not set"
    1957             :                 {
    1958             :                         // Keep regrid center as it is in the data
    1959          20 :                         theRegridCenterF = (transNewXin[0] - transCHAN_WIDTH[0] / 2.
    1960          20 :                                                                 + transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.) / 2.;
    1961          20 :                         centerIsStart = false;
    1962             :                 }
    1963             :                 // regridCenterF was set
    1964             :                 else
    1965             :                 {
    1966             :                         // Keep center in limits
    1967         809 :                         theRegridCenterF = regridCenterF;
    1968         809 :                         if ((theRegridCenterF - (transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.)) > 1.)  // 1 Hz tolerance
    1969             :                         {
    1970           1 :                                 oss << "*** Requested center of SPW " << theRegridCenterF
    1971           1 :                                                 << " Hz is too large by "
    1972           1 :                                                 << theRegridCenterF - transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.
    1973           1 :                                                 << " Hz\n";
    1974           1 :                                 theRegridCenterF = transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.;
    1975           1 :                                 oss << "*** Reset to maximum possible value " << theRegridCenterF << " Hz";
    1976             :                         }
    1977         808 :                         else if (theRegridCenterF < (transNewXin[0] - transCHAN_WIDTH[0] / 2.))
    1978             :                         {
    1979          23 :                                 Double diff = (transNewXin[0] - transCHAN_WIDTH[0] / 2.) - theRegridCenterF;
    1980             : 
    1981             :                                 // Cope with numerical accuracy problems
    1982          23 :                                 if (diff > 1.)
    1983             :                                 {
    1984          22 :                                         oss << "*** Requested center of SPW (" << theRegridCenterF
    1985          22 :                                                 << " Hz) is smaller than minimum possible value";
    1986          22 :                                         oss << " by " << diff << " Hz";
    1987             :                                 }
    1988             : 
    1989          23 :                                 theRegridCenterF = transNewXin[0] - transCHAN_WIDTH[0] / 2.;
    1990          23 :                                 if (diff > 1.)
    1991             :                                 {
    1992          22 :                                         oss << "\n*** Reset to minimum possible value " << theRegridCenterF << " Hz";
    1993             :                                 }
    1994             :                         }
    1995             :                 }
    1996             : 
    1997         829 :                 if (regridBandwidthF <= 0. || nchan != 0) // "not set" or use nchan instead
    1998             :                 {
    1999             :                         // Keep bandwidth as is
    2000         823 :                         theRegridBWF = transNewXin[oldNUM_CHAN - 1] - transNewXin[0]
    2001         823 :                                         + transCHAN_WIDTH[0] / 2. + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.;
    2002             : 
    2003             :                         // Use nchan parameter if available
    2004         823 :                         if (nchan != 0)
    2005             :                         {
    2006         823 :                                 if (nchan < 0)
    2007             :                                 {
    2008         563 :                                         if (regridQuant == "freq" || regridQuant == "vrad")  // i.e. equidistant in freq
    2009             :                                         {
    2010             :                                                 // Define via width of first channel to avoid numerical problems
    2011             : 
    2012             :                                                 // channel width not set
    2013         561 :                                                 if (regridChanWidthF <= 0.)
    2014             :                                                 {
    2015           4 :                                                         theRegridBWF = transCHAN_WIDTH[0] *
    2016           2 :                                                                         floor((theRegridBWF + transCHAN_WIDTH[0] * 0.01)/ transCHAN_WIDTH[0]);
    2017             :                                                 }
    2018             :                                                 else
    2019             :                                                 {
    2020         559 :                                                         theRegridBWF = regridChanWidthF *
    2021         559 :                                                                         floor((theRegridBWF + regridChanWidthF * 0.01)/ regridChanWidthF);
    2022             :                                                 }
    2023             :                                         }
    2024             :                                 }
    2025             :                                 // Channel width not set
    2026         260 :                                 else if (regridChanWidthF <= 0.)
    2027             :                                 {
    2028          13 :                                         theRegridBWF = transCHAN_WIDTH[0] * nchan;
    2029             :                                 }
    2030             :                                 else
    2031             :                                 {
    2032         247 :                                         theRegridBWF = regridChanWidthF * nchan;
    2033             :                                 }
    2034             : 
    2035             :                                 // Center was not set by user but calculated
    2036         823 :                                 if (regridCenterF <= 0. || regridCenter < -C::c)
    2037             :                                 {
    2038             :                                         // Need to update
    2039          39 :                                         theRegridCenterF = transNewXin[0] - transCHAN_WIDTH[0] / 2. + theRegridBWF / 2.;
    2040          39 :                                         centerIsStart = false;
    2041             :                                 }
    2042             :                                 // Center but not nchan was set by user
    2043         784 :                                 else if (nchan < 0)
    2044             :                                 {
    2045             :                                         // Verify that the bandwidth is correct
    2046         552 :                                         if (centerIsStart)
    2047             :                                         {
    2048         552 :                                                 if (startIsEnd)
    2049             :                                                 {
    2050          19 :                                                         theRegridBWF = theRegridCenterF - transNewXin[0] + transCHAN_WIDTH[0] / 2.;
    2051             :                                                 }
    2052             :                                                 // start is start
    2053             :                                                 else
    2054             :                                                 {
    2055         533 :                                                         theRegridBWF = transNewXin[oldNUM_CHAN - 1] +
    2056         533 :                                                                         transCHAN_WIDTH[oldNUM_CHAN - 1] / 2. -
    2057             :                                                                         theRegridCenterF;
    2058             :                                                 }
    2059         552 :                                                 if (regridQuant == "freq" || regridQuant == "vrad") // i.e. equidistant in freq
    2060             :                                                 {
    2061             :                                                         // Define via width of first channel to avoid numerical problems
    2062         552 :                                                         if (regridChanWidthF <= 0.) { // channel width not set
    2063           2 :                                                                 theRegridBWF = transCHAN_WIDTH[0]
    2064           1 :                                                                                * floor((theRegridBWF + transCHAN_WIDTH[0]* 0.01) / transCHAN_WIDTH[0]);
    2065             :                                                         }
    2066             :                                                         else
    2067             :                                                         {
    2068         551 :                                                                 theRegridBWF = regridChanWidthF
    2069         551 :                                                                                 * floor((theRegridBWF+ regridChanWidthF* 0.01)/ regridChanWidthF);
    2070             :                                                         }
    2071             :                                                 }
    2072             :                                         }
    2073             :                                         // center is center
    2074             :                                         else {
    2075           0 :                                                 theRegridBWF = 2. * min((Double) (theRegridCenterF - transNewXin[0]- transCHAN_WIDTH[0]),
    2076           0 :                                                                 (Double) (transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1] - theRegridCenterF));
    2077             :                                         }
    2078             :                                 }
    2079             :                         }
    2080             : 
    2081             :                         // Now can convert start to center
    2082         823 :                         if (centerIsStart)
    2083             :                         {
    2084         756 :                                 if (startIsEnd)
    2085             :                                 {
    2086          31 :                                         theRegridCenterF = theRegridCenterF - theRegridBWF / 2.;
    2087             :                                 }
    2088             :                                 else
    2089             :                                 {
    2090         725 :                                         theRegridCenterF = theRegridCenterF + theRegridBWF / 2.;
    2091             :                                 }
    2092         756 :                                 centerIsStart = false;
    2093             :                         }
    2094             :                 }
    2095             :                 // regridBandwidthF was set
    2096             :                 else {
    2097             :                         // Determine actually possible bandwidth
    2098             :                         // width will be truncated to the maximum width possible
    2099             :                         // symmetrically around the value given by "regrid_center"
    2100           6 :                         theRegridBWF = regridBandwidthF;
    2101             : 
    2102             :                         // Now can convert start to center
    2103           6 :                         if (centerIsStart)
    2104             :                         {
    2105           0 :                                 if (startIsEnd)
    2106             :                                 {
    2107           0 :                                         theRegridCenterF = theRegridCenterF - theRegridBWF / 2.;
    2108             :                                 }
    2109             :                                 else
    2110             :                                 {
    2111           0 :                                         theRegridCenterF = theRegridCenterF + theRegridBWF / 2.;
    2112             :                                 }
    2113           0 :                                 centerIsStart = false;
    2114             :                         }
    2115             : 
    2116           6 :                         Double rangeTol = 1.; // Hz
    2117           6 :                         if ((regridQuant == "vopt" || regridQuant == "wave")) // i.e. if the center is the center w.r.t. wavelength
    2118             :                         {
    2119           6 :                                 rangeTol = transCHAN_WIDTH[0];
    2120             :                         }
    2121             : 
    2122           6 :                         if ((theRegridCenterF + theRegridBWF / 2.) - (transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.) > rangeTol)
    2123             :                         {
    2124             :                                 oss     << " *** Input spectral window exceeds upper end of original window. "
    2125           0 :                                                 "Adjusting to max. possible value." << endl;
    2126           0 :                                 theRegridBWF = min(     (Double) fabs(transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.- theRegridCenterF),
    2127           0 :                                                                         (Double) fabs(theRegridCenterF - transNewXin[0]+ transCHAN_WIDTH[0] / 2.)) * 2.;
    2128             : 
    2129           0 :                                 if (theRegridBWF < transCHAN_WIDTH[0])
    2130             :                                 {
    2131           0 :                                         theRegridCenterF = (    transNewXin[0]
    2132           0 :                                                                 + transCHAN_WIDTH[oldNUM_CHAN - 1]
    2133           0 :                                                                 + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.
    2134           0 :                                                                 - transCHAN_WIDTH[0] / 2.) / 2.;
    2135           0 :                                         theRegridBWF = transCHAN_WIDTH[oldNUM_CHAN - 1]
    2136           0 :                                                        - transNewXin[0] + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2. + transCHAN_WIDTH[0] / 2.;
    2137             :                                 }
    2138             :                         }
    2139           6 :                         if ((theRegridCenterF - theRegridBWF / 2.) - (transNewXin[0] - transCHAN_WIDTH[0] / 2.) < -rangeTol)
    2140             :                         {
    2141             :                                 oss << " *** Input spectral window exceeds lower end of original window. "
    2142           2 :                                                 " Adjusting to max. possible value."<< endl;
    2143             : 
    2144           2 :                                 theRegridBWF = min( (Double) fabs(transNewXin[oldNUM_CHAN - 1] + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2. - theRegridCenterF),
    2145           2 :                                                                         (Double) fabs(theRegridCenterF - transNewXin[0] + transCHAN_WIDTH[0] / 2.)) * 2.;
    2146             : 
    2147           2 :                                 if (theRegridBWF < transCHAN_WIDTH[0])
    2148             :                                 {
    2149           0 :                                         theRegridCenterF = (transNewXin[0]
    2150           0 :                                                             + transCHAN_WIDTH[oldNUM_CHAN - 1]
    2151           0 :                                                             + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.
    2152           0 :                                                             - transCHAN_WIDTH[0] / 2.) / 2.;
    2153           0 :                                         theRegridBWF = transCHAN_WIDTH[oldNUM_CHAN - 1]
    2154           0 :                                                        - transNewXin[0]
    2155           0 :                                                        + transCHAN_WIDTH[oldNUM_CHAN - 1] / 2.
    2156           0 :                                                        + transCHAN_WIDTH[0] / 2.;
    2157             :                                 }
    2158             :                         }
    2159             :                 }
    2160             : 
    2161         829 :                 if (regridChanWidthF <= 0.)  // "not set"
    2162             :                 {
    2163          16 :                         if (nchan != 0 || centerIsStartC) // use first channel
    2164             :                         {
    2165          16 :                                 theCentralChanWidthF = transCHAN_WIDTH[0];
    2166             :                         }
    2167             :                         else
    2168             :                         {
    2169             :                                 // keep channel width similar to the old one
    2170           0 :                                 theCentralChanWidthF = transCHAN_WIDTH[oldNUM_CHAN / 2]; // use channel width from
    2171             :                                 // near central channel
    2172             :                         }
    2173             :                 }
    2174             :                 // regridChanWidthF was set
    2175             :                 else
    2176             :                 {
    2177             :                         // Keep in limits
    2178         813 :                         theCentralChanWidthF = regridChanWidthF;
    2179             : 
    2180             :                         // Too large => make a single channel
    2181         813 :                         if (theCentralChanWidthF > theRegridBWF)
    2182             :                         {
    2183           0 :                                 theCentralChanWidthF = theRegridBWF;
    2184             :                                 oss
    2185           0 :                                                 << " *** Requested new channel width exceeds defined SPW width."
    2186           0 :                                                 << endl
    2187           0 :                                                 << "     Creating a single channel with the defined SPW width."
    2188           0 :                                                 << endl;
    2189             :                         }
    2190             :                         // Check if too small
    2191         813 :                         else if (theCentralChanWidthF < transCHAN_WIDTH[0])
    2192             :                         {
    2193             :                                 // Determine smallest channel width
    2194           8 :                                 lDouble smallestChanWidth = 1E30;
    2195           8 :                                 Int ii = 0;
    2196        6104 :                                 for (Int i = 0; i < oldNUM_CHAN; i++)
    2197             :                                 {
    2198        6096 :                                         if (transCHAN_WIDTH[i] < smallestChanWidth)
    2199             :                                         {
    2200          15 :                                                 smallestChanWidth = transCHAN_WIDTH[i];
    2201          15 :                                                 ii = i;
    2202             :                                         }
    2203             :                                 }
    2204             : 
    2205             :                                 // 1 Hz tolerance to cope with numerical accuracy problems
    2206           8 :                                 if (theCentralChanWidthF < smallestChanWidth - 1.)
    2207             :                                 {
    2208           2 :                                         oss << " *** Requested new channel width (" << theCentralChanWidthF << " Hz) "
    2209           2 :                                             << "is smaller than smallest original channel width" << endl;
    2210           2 :                                         oss << "     which is " << smallestChanWidth << " Hz" << endl;
    2211             : 
    2212           2 :                                         if (regridQuant == "vrad")
    2213             :                                         {
    2214           1 :                                                 oss << "     or "
    2215           1 :                                                         << (vrad(transNewXin[ii],regridVeloRestfrq)
    2216           1 :                                                           - vrad(transNewXin[ii] + transCHAN_WIDTH[ii] / 2.,regridVeloRestfrq)) * 2. << " m/s";
    2217             :                                         }
    2218             : 
    2219           2 :                                         if (regridQuant == "vopt")
    2220             :                                         {
    2221           0 :                                                 oss << "     or "
    2222           0 :                                                     << (vopt(transNewXin[ii],regridVeloRestfrq)
    2223           0 :                                                           - vopt(transNewXin[ii] + transCHAN_WIDTH[ii] / 2.,regridVeloRestfrq)) * 2. << " m/s";
    2224             :                                         }
    2225             : 
    2226           2 :                                         message = oss.str();
    2227           2 :                                         return false;
    2228             : 
    2229             :                                 }
    2230             :                                 // input channel width was OK, memorize
    2231             :                                 else
    2232             :                                 {
    2233           6 :                                         theChanWidthX = regridChanWidth;
    2234             :                                 }
    2235             :                         }
    2236             :                 }
    2237             : 
    2238         827 :                 oss << " Channels equidistant in " << regridQuant << endl
    2239         827 :                         << " Central frequency (in output frame) = " << theRegridCenterF << " Hz";
    2240             : 
    2241         827 :                 if (regridQuant == "vrad")
    2242             :                 {
    2243          44 :                         oss << " == " << vrad(theRegridCenterF, regridVeloRestfrq) << " m/s radio velocity";
    2244             :                 }
    2245         783 :                 else if (regridQuant == "vopt")
    2246             :                 {
    2247           8 :                         oss << " == " << vopt(theRegridCenterF, regridVeloRestfrq) << " m/s optical velocity";
    2248             :                 }
    2249         775 :                 else if (regridQuant == "wave")
    2250             :                 {
    2251           0 :                         oss << " == " << lambda(theRegridCenterF) << " m wavelength";
    2252             :                 }
    2253         827 :                 oss << endl;
    2254             : 
    2255         827 :                 if (isDescending)
    2256             :                 {
    2257          36 :                         oss << " Channel central frequency is decreasing with increasing channel number." << endl;
    2258             :                 }
    2259             : 
    2260         827 :                 oss << " Width of central channel (in output frame) = " << theCentralChanWidthF << " Hz";
    2261             : 
    2262         827 :                 if (regridQuant == "vrad")
    2263             :                 {
    2264          44 :                         oss << " == " << vrad(theRegridCenterF - theCentralChanWidthF,regridVeloRestfrq)
    2265          44 :                                                - vrad(theRegridCenterF,regridVeloRestfrq) << " m/s radio velocity";
    2266             :                 }
    2267         783 :                 else if (regridQuant == "vopt")
    2268             :                 {
    2269           8 :                         oss << " == " << vopt(theRegridCenterF - theCentralChanWidthF,regridVeloRestfrq)
    2270           8 :                                                - vopt(theRegridCenterF,regridVeloRestfrq) << " m/s optical velocity";
    2271             :                 }
    2272         775 :                 else if (regridQuant == "wave")
    2273             :                 {
    2274           0 :                         oss << " == " << lambda(theRegridCenterF - theCentralChanWidthF)
    2275           0 :                                                - lambda(theRegridCenterF) << " m wavelength";
    2276             :                 }
    2277         827 :                 oss << endl;
    2278             : 
    2279             :                 // Now calculate newChanLoBound, and newChanHiBound from theRegridCenterF, theRegridBWF, theCentralChanWidthF
    2280         827 :                 vector<lDouble> loFBup; // The lower bounds for the new channels starting from the central channel going up
    2281         827 :                 vector<lDouble> hiFBup; // The lower bounds for the new channels starting from the central channel going up
    2282         827 :                 vector<lDouble> loFBdown; // The lower bounds for the new channels starting from the central channel going down
    2283         827 :                 vector<lDouble> hiFBdown; // The lower bounds for the new channels starting from the central channel going down
    2284             : 
    2285         827 :                 lDouble edgeTolerance = theCentralChanWidthF * 0.01; // Needed to avoid numerical accuracy problems
    2286             : 
    2287             :                 // Re-gridding in radio velocity ...
    2288         827 :                 if (regridQuant == "vrad")
    2289             :                 {
    2290             :                         // Create freq boundaries equidistant and contiguous in radio velocity
    2291          44 :                         lDouble upperEndF = theRegridCenterF + theRegridBWF / 2.;
    2292          44 :                         lDouble lowerEndF = theRegridCenterF - theRegridBWF / 2.;
    2293          44 :                         lDouble upperEndV = vrad(upperEndF, regridVeloRestfrq);
    2294          44 :                         lDouble lowerEndV = vrad(lowerEndF, regridVeloRestfrq);
    2295             :                         lDouble velLo;
    2296             :                         lDouble velHi;
    2297             : 
    2298             :                         // Want to keep the center of the center channel at the center
    2299             :                         // of the new center channel if the bandwidth is an odd multiple
    2300             :                         // of the new channel width, otherwise the center channel is the
    2301             :                         // lower edge of the new center channel
    2302          44 :                         lDouble tnumChan = floor((theRegridBWF + edgeTolerance) / theCentralChanWidthF);
    2303             : 
    2304          44 :                         if ((Int) tnumChan % 2 != 0)
    2305             :                         {
    2306             :                                 // Odd multiple
    2307           5 :                                 loFBup.push_back(theRegridCenterF - theCentralChanWidthF / 2.);
    2308           5 :                                 hiFBup.push_back(theRegridCenterF + theCentralChanWidthF / 2.);
    2309           5 :                                 loFBdown.push_back(theRegridCenterF - theCentralChanWidthF / 2.);
    2310           5 :                                 hiFBdown.push_back(theRegridCenterF + theCentralChanWidthF / 2.);
    2311             :                         }
    2312             :                         else
    2313             :                         {
    2314          39 :                                 loFBup.push_back(theRegridCenterF);
    2315          39 :                                 hiFBup.push_back(theRegridCenterF + theCentralChanWidthF);
    2316          39 :                                 loFBdown.push_back(theRegridCenterF);
    2317          39 :                                 hiFBdown.push_back(theRegridCenterF + theCentralChanWidthF);
    2318             :                         }
    2319             : 
    2320             :                         // Cannot use original channel width in velocity units
    2321          44 :                         if (theChanWidthX < 0)
    2322             :                         {
    2323             :                                 // Need to calculate back from central channel width in Hz
    2324          38 :                                 theChanWidthX = vrad(loFBup[0], regridVeloRestfrq) - vrad(hiFBup[0], regridVeloRestfrq);
    2325             :                         }
    2326             : 
    2327             :                         // calc velocity corresponding to the upper end (in freq) of the
    2328             :                         // last added channel which is the lower end of the next channel
    2329          44 :                         velLo = vrad(hiFBup[0], regridVeloRestfrq);
    2330             : 
    2331             :                         // calc velocity corresponding to the upper end (in freq) of the next channel
    2332          44 :                         velHi = velLo - theChanWidthX; // vrad goes down as freq goes up!
    2333             : 
    2334             :                         // (Preventing accuracy problems)
    2335        2522 :                         while (upperEndV - theChanWidthX / 10. < velHi)
    2336             :                         {
    2337             :                                 // calc frequency of the upper end (in freq) of the next channel
    2338        2478 :                                 lDouble freqHi = freq_from_vrad(velHi, regridVeloRestfrq);
    2339             : 
    2340             :                                 // End of bandwidth not yet reached
    2341        2478 :                                 if (freqHi <= upperEndF + edgeTolerance)
    2342             :                                 {
    2343        2478 :                                         loFBup.push_back(hiFBup.back());
    2344        2478 :                                         hiFBup.push_back(freqHi);
    2345             :                                 }
    2346           0 :                                 else if (freqHi < upperEndF + edgeTolerance)
    2347             :                                 {
    2348           0 :                                         loFBup.push_back(hiFBup.back());
    2349           0 :                                         hiFBup.push_back(upperEndF);
    2350           0 :                                         break;
    2351             :                                 }
    2352             :                                 else
    2353             :                                 {
    2354           0 :                                         break;
    2355             :                                 }
    2356             : 
    2357             :                                 // calc velocity corresponding to the upper end (in freq) of the added channel
    2358        2478 :                                 velLo = vrad(hiFBup.back(), regridVeloRestfrq);
    2359             : 
    2360             :                                 // calc velocity corresponding to the upper end (in freq) of the next channel
    2361        2478 :                                 velHi = velLo - theChanWidthX; // vrad goes down as freq goes up
    2362             :                         }
    2363             : 
    2364             :                         // calc velocity corresponding to the lower end (in freq) of the
    2365             :                         // Last added channel which is the upper end of the next channel
    2366          44 :                         velHi = vrad(loFBdown[0], regridVeloRestfrq);
    2367             : 
    2368             :                         // calc velocity corresponding to the lower end (in freq) of the next channel
    2369          44 :                         velLo = velHi + theChanWidthX; // vrad goes up as freq goes down!
    2370             : 
    2371             :                         // (Preventing accuracy problems)
    2372        2561 :                         while (velLo < lowerEndV + theChanWidthX / 10.)
    2373             :                         {
    2374             :                                 // calc frequency of the lower end (in freq) of the next channel
    2375        2517 :                                 lDouble freqLo = freq_from_vrad(velLo, regridVeloRestfrq);
    2376             : 
    2377             :                                 // End of bandwidth not yet reached
    2378        2517 :                                 if (freqLo >= lowerEndF - edgeTolerance)
    2379             :                                 {
    2380        2517 :                                         hiFBdown.push_back(loFBdown.back());
    2381        2517 :                                         loFBdown.push_back(freqLo);
    2382             :                                 }
    2383           0 :                                 else if (freqLo > lowerEndF - edgeTolerance)
    2384             :                                 {
    2385           0 :                                         hiFBdown.push_back(loFBdown.back());
    2386           0 :                                         loFBdown.push_back(lowerEndF);
    2387           0 :                                         break;
    2388             :                                 }
    2389             :                                 else
    2390             :                                 {
    2391           0 :                                         break;
    2392             :                                 }
    2393             : 
    2394             :                                 // calc velocity corresponding to the upper end of the next channel
    2395        2517 :                                 velHi = vrad(loFBdown.back(), regridVeloRestfrq);
    2396             : 
    2397             :                                 // calc velocity corresponding to the lower end (in freq) of the next channel
    2398        2517 :                                 velLo = velHi + theChanWidthX; // vrad goes up as freq goes down
    2399             :                         }
    2400             :                 }
    2401             :                 // Regridding in optical velocity ...
    2402         783 :                 else if (regridQuant == "vopt")
    2403             :                 {
    2404             :                         // Create freq boundaries equidistant and contiguous in optical velocity
    2405           8 :                         lDouble upperEndF = theRegridCenterF + theRegridBWF / 2.;
    2406           8 :                         lDouble lowerEndF = theRegridCenterF - theRegridBWF / 2.;
    2407           8 :                         lDouble upperEndV = vopt(upperEndF, regridVeloRestfrq);
    2408           8 :                         lDouble lowerEndV = vopt(lowerEndF, regridVeloRestfrq);
    2409             :                         lDouble velLo;
    2410             :                         lDouble velHi;
    2411             : 
    2412             :                         // Want to keep the center of the center channel at the center
    2413             :                         // of the new center channel if the bandwidth is an odd multiple
    2414             :                         // of the new channel width, otherwise the center channel is the
    2415             :                         // lower edge of the new center channel
    2416             : 
    2417             :                         // Enlarged edge tolerance since channels non-equidistant in freq
    2418           8 :                         lDouble tnumChan = floor((theRegridBWF + edgeTolerance) / theCentralChanWidthF);
    2419             : 
    2420             :                         // Odd multiple
    2421           8 :                         if ((Int) tnumChan % 2 != 0)
    2422             :                         {
    2423             : 
    2424           4 :                                 loFBup.push_back(theRegridCenterF - theCentralChanWidthF / 2.);
    2425           4 :                                 hiFBup.push_back(theRegridCenterF + theCentralChanWidthF / 2.);
    2426           4 :                                 loFBdown.push_back(theRegridCenterF - theCentralChanWidthF / 2.);
    2427           4 :                                 hiFBdown.push_back(theRegridCenterF + theCentralChanWidthF / 2.);
    2428             :                         }
    2429             :                         else
    2430             :                         {
    2431           4 :                                 loFBup.push_back(theRegridCenterF);
    2432           4 :                                 hiFBup.push_back(theRegridCenterF + theCentralChanWidthF);
    2433           4 :                                 loFBdown.push_back(theRegridCenterF);
    2434           4 :                                 hiFBdown.push_back(theRegridCenterF + theCentralChanWidthF);
    2435             :                         }
    2436             : 
    2437             :                         // Cannot use original channel width in velocity units
    2438           8 :                         if (theChanWidthX < 0)
    2439             :                         {
    2440             :                                 // Need to calculate back from central channel width in Hz
    2441           8 :                                 theChanWidthX = vopt(loFBup[0], regridVeloRestfrq) - vopt(hiFBup[0], regridVeloRestfrq);
    2442             :                         }
    2443             : 
    2444             :                         // calc velocity corresponding to the upper end (in freq) of the
    2445             :                         // last added channel which is the lower end of the next channel
    2446           8 :                         velLo = vopt(hiFBup[0], regridVeloRestfrq);
    2447             : 
    2448             :                         // calc velocity corresponding to the upper end (in freq) of the next channel
    2449           8 :                         velHi = velLo - theChanWidthX; // vopt goes down as freq goes up!
    2450             : 
    2451             :                         // (Preventing accuracy problems)
    2452         108 :                         while (upperEndV - velHi < theChanWidthX / 10.)
    2453             :                         {
    2454             :                                 // calc frequency of the upper end (in freq) of the next channel
    2455         101 :                                 lDouble freqHi = freq_from_vopt(velHi, regridVeloRestfrq);
    2456             : 
    2457             :                                 // End of bandwidth not yet reached
    2458         101 :                                 if (freqHi <= upperEndF + edgeTolerance)
    2459             :                                 {
    2460         100 :                                         loFBup.push_back(hiFBup.back());
    2461         100 :                                         hiFBup.push_back(freqHi);
    2462             :                                 }
    2463           1 :                                 else if (freqHi < upperEndF + edgeTolerance)
    2464             :                                 {
    2465           0 :                                         loFBup.push_back(hiFBup.back());
    2466           0 :                                         hiFBup.push_back(upperEndF);
    2467           1 :                                         break;
    2468             :                                 }
    2469             :                                 else
    2470             :                                 {
    2471           1 :                                         break;
    2472             :                                 }
    2473             : 
    2474             :                                 // calc velocity corresponding to the upper end (in freq) of the added channel
    2475         100 :                                 velLo = vopt(hiFBup.back(), regridVeloRestfrq);
    2476             :                                 // calc velocity corresponding to the upper end (in freq) of the next channel
    2477         100 :                                 velHi = velLo - theChanWidthX; // vopt goes down as freq goes up
    2478             :                         }
    2479             : 
    2480             :                         // calc velocity corresponding to the lower end (in freq) of the
    2481             :                         // last added channel which is the upper end of the next channel
    2482           8 :                         velHi = vopt(loFBdown[0], regridVeloRestfrq);
    2483             : 
    2484             :                         // calc velocity corresponding to the lower end (in freq) of the next channel
    2485           8 :                         velLo = velHi + theChanWidthX; // vopt goes up as freq goes down!
    2486             : 
    2487             :                         // (Preventing accuracy problems)
    2488         113 :                         while (velLo - lowerEndV < theChanWidthX / 10.)
    2489             :                         {
    2490             :                                 // calc frequency of the lower end (in freq) of the next channel
    2491         105 :                                 lDouble freqLo = freq_from_vopt(velLo, regridVeloRestfrq);
    2492             : 
    2493             :                                 // End of bandwidth not yet reached
    2494         105 :                                 if (freqLo >= lowerEndF - edgeTolerance)
    2495             :                                 {
    2496         105 :                                         hiFBdown.push_back(loFBdown.back());
    2497         105 :                                         loFBdown.push_back(freqLo);
    2498             :                                 }
    2499           0 :                                 else if (freqLo > lowerEndF - edgeTolerance)
    2500             :                                 {
    2501           0 :                                         hiFBdown.push_back(loFBdown.back());
    2502           0 :                                         loFBdown.push_back(lowerEndF);
    2503           0 :                                         break;
    2504             :                                 }
    2505             :                                 else
    2506             :                                 {
    2507           0 :                                         break;
    2508             :                                 }
    2509             : 
    2510             :                                 // calc velocity corresponding to the upper end of the next channel
    2511         105 :                                 velHi = vopt(loFBdown.back(), regridVeloRestfrq);
    2512             :                                 // calc velocity corresponding to the lower end (in freq) of the next channel
    2513         105 :                                 velLo = velHi + theChanWidthX; // vopt goes up as freq goes down
    2514             :                         }
    2515             :                 }
    2516             :                 // Re-gridding in frequency  ...
    2517         775 :                 else if (regridQuant == "freq")
    2518             :                 {
    2519             :                         // Create freq boundaries equidistant and contiguous in frequency
    2520         775 :                         lDouble upperEndF = theRegridCenterF + theRegridBWF / 2.;
    2521         775 :                         lDouble lowerEndF = theRegridCenterF - theRegridBWF / 2.;
    2522             : 
    2523             :                         // Want to keep the center of the center channel at the center
    2524             :                         // of the new center channel if the bandwidth is an odd multiple
    2525             :                         // of the new channel width, otherwise the center channel is the
    2526             :                         // lower edge of the new center channel
    2527         775 :                         lDouble tnumChan = floor((theRegridBWF + edgeTolerance) / theCentralChanWidthF);
    2528             : 
    2529             :                         // Odd multiple
    2530         775 :                         if ((Int) tnumChan % 2 != 0)
    2531             :                         {
    2532         301 :                                 loFBup.push_back(theRegridCenterF - theCentralChanWidthF / 2.);
    2533         301 :                                 hiFBup.push_back(theRegridCenterF + theCentralChanWidthF / 2.);
    2534         301 :                                 loFBdown.push_back(theRegridCenterF - theCentralChanWidthF / 2.);
    2535         301 :                                 hiFBdown.push_back(theRegridCenterF + theCentralChanWidthF / 2.);
    2536             :                         }
    2537             :                         else
    2538             :                         {
    2539         474 :                                 loFBup.push_back(theRegridCenterF);
    2540         474 :                                 hiFBup.push_back(theRegridCenterF + theCentralChanWidthF);
    2541         474 :                                 loFBdown.push_back(theRegridCenterF);
    2542         474 :                                 hiFBdown.push_back(theRegridCenterF + theCentralChanWidthF);
    2543             :                         }
    2544             : 
    2545       85001 :                         while (hiFBup.back() < upperEndF + edgeTolerance)
    2546             :                         {
    2547             :                                 // calc frequency of the upper end of the next channel
    2548       85001 :                                 lDouble freqHi = hiFBup.back() + theCentralChanWidthF;
    2549             : 
    2550             :                                 // End of bandwidth not yet reached
    2551       85001 :                                 if (freqHi <= upperEndF + edgeTolerance)
    2552             :                                 {
    2553       84226 :                                         loFBup.push_back(hiFBup.back());
    2554       84226 :                                         hiFBup.push_back(freqHi);
    2555             :                                 }
    2556             :                                 else
    2557             :                                 {
    2558         775 :                                         break;
    2559             :                                 }
    2560             :                         }
    2561             : 
    2562       85475 :                         while (loFBdown.back() > lowerEndF - edgeTolerance)
    2563             :                         {
    2564             :                                 // calc frequency of the lower end of the next channel
    2565       85475 :                                 lDouble freqLo = loFBdown.back() - theCentralChanWidthF;
    2566             : 
    2567             :                                 // End of bandwidth not yet reached
    2568       85475 :                                 if (freqLo >= lowerEndF - edgeTolerance)
    2569             :                                 {
    2570       84700 :                                         hiFBdown.push_back(loFBdown.back());
    2571       84700 :                                         loFBdown.push_back(freqLo);
    2572             :                                 }
    2573             :                                 else
    2574             :                                 {
    2575         775 :                                         break;
    2576             :                                 }
    2577             :                         }
    2578             :                 }
    2579             :                 // Re-gridding in wavelength  ...
    2580           0 :                 else if (regridQuant == "wave")
    2581             :                 {
    2582             :                         // Create freq boundaries equidistant and contiguous in wavelength
    2583           0 :                         lDouble upperEndF = theRegridCenterF + theRegridBWF / 2.;
    2584           0 :                         lDouble lowerEndF = theRegridCenterF - theRegridBWF / 2.;
    2585           0 :                         lDouble upperEndL = lambda(upperEndF);
    2586           0 :                         lDouble lowerEndL = lambda(lowerEndF);
    2587             :                         lDouble lambdaLo;
    2588             :                         lDouble lambdaHi;
    2589             : 
    2590             :                         // Want to keep the center of the center channel at the center
    2591             :                         // of the new center channel if the bandwidth is an odd multiple
    2592             :                         // of the new channel width, otherwise the center channel is the
    2593             :                         // lower edge of the new center channel
    2594           0 :                         lDouble tnumChan = floor((theRegridBWF + edgeTolerance) / theCentralChanWidthF);
    2595             : 
    2596             :                         // Odd multiple
    2597           0 :                         if ((Int) tnumChan % 2 != 0)
    2598             :                         {
    2599           0 :                                 loFBup.push_back(theRegridCenterF - theCentralChanWidthF / 2.);
    2600           0 :                                 hiFBup.push_back(theRegridCenterF + theCentralChanWidthF / 2.);
    2601           0 :                                 loFBdown.push_back(theRegridCenterF - theCentralChanWidthF / 2.);
    2602           0 :                                 hiFBdown.push_back(theRegridCenterF + theCentralChanWidthF / 2.);
    2603             :                         }
    2604             :                         else
    2605             :                         {
    2606           0 :                                 loFBup.push_back(theRegridCenterF);
    2607           0 :                                 hiFBup.push_back(theRegridCenterF + theCentralChanWidthF);
    2608           0 :                                 loFBdown.push_back(theRegridCenterF);
    2609           0 :                                 hiFBdown.push_back(theRegridCenterF + theCentralChanWidthF);
    2610             :                         }
    2611             : 
    2612             :                         // Cannot use original channel width in wavelength units
    2613           0 :                         if (theChanWidthX < 0)
    2614             :                         {
    2615             :                                 // Need to calculate back from central channel width in Hz
    2616           0 :                                 theChanWidthX = lambda(loFBup[0]) - lambda(hiFBup[0]);
    2617             :                         }
    2618             : 
    2619             :                         // calc wavelength corresponding to the upper end (in freq) of the
    2620             :                         // last added channel which is the lower end of the next channel
    2621           0 :                         lambdaLo = lambda(hiFBup[0]);
    2622             : 
    2623             :                         // calc wavelength corresponding to the upper end (in freq) of the next channel
    2624           0 :                         lambdaHi = lambdaLo - theChanWidthX; // lambda goes down as freq goes up!
    2625             : 
    2626             :                         // (Preventing accuracy problems)
    2627           0 :                         while (upperEndL - lambdaHi < theChanWidthX / 10.)
    2628             :                         {
    2629             :                                 // calc frequency of the upper end (in freq) of the next channel
    2630           0 :                                 lDouble freqHi = freq_from_lambda(lambdaHi);
    2631             : 
    2632             :                                 // End of bandwidth not yet reached
    2633           0 :                                 if (freqHi <= upperEndF + edgeTolerance)
    2634             :                                 {
    2635           0 :                                         loFBup.push_back(hiFBup.back());
    2636           0 :                                         hiFBup.push_back(freqHi);
    2637             :                                 }
    2638           0 :                                 else if (freqHi < upperEndF + edgeTolerance)
    2639             :                                 {
    2640           0 :                                         loFBup.push_back(hiFBup.back());
    2641           0 :                                         hiFBup.push_back(upperEndF);
    2642           0 :                                         break;
    2643             :                                 }
    2644             :                                 else
    2645             :                                 {
    2646           0 :                                         break;
    2647             :                                 }
    2648             : 
    2649             :                                 // calc wavelength corresponding to the upper end (in freq) of the added channel
    2650           0 :                                 lambdaLo = lambda(hiFBup.back());
    2651             :                                 // calc wavelength corresponding to the upper end (in freq) of the next channel
    2652           0 :                                 lambdaHi = lambdaLo - theChanWidthX; // lambda goes down as freq goes up
    2653             :                         }
    2654             : 
    2655             :                         // calc wavelength corresponding to the lower end (in freq) of the
    2656             :                         // last added channel which is the upper end of the next channel
    2657           0 :                         lambdaHi = lambda(loFBdown[0]);
    2658             : 
    2659             :                         // calc wavelength corresponding to the lower end (in freq) of the next channel
    2660           0 :                         lambdaLo = lambdaHi + theChanWidthX; // lambda goes up as freq goes down!
    2661             : 
    2662             : 
    2663             :                         // (Preventing accuracy problems)
    2664           0 :                         while (lambdaLo - lowerEndL < theChanWidthX / 10.)
    2665             :                         {
    2666             :                                 // calc frequency of the lower end (in freq) of the next channel
    2667           0 :                                 lDouble freqLo = freq_from_lambda(lambdaLo);
    2668             : 
    2669             :                                 // End of bandwidth not yet reached
    2670           0 :                                 if (freqLo >= lowerEndF - edgeTolerance)
    2671             :                                 {
    2672           0 :                                         hiFBdown.push_back(loFBdown.back());
    2673           0 :                                         loFBdown.push_back(freqLo);
    2674             :                                 }
    2675           0 :                                 else if (freqLo > lowerEndF - edgeTolerance)
    2676             :                                 {
    2677           0 :                                         hiFBdown.push_back(loFBdown.back());
    2678           0 :                                         loFBdown.push_back(lowerEndF);
    2679           0 :                                         break;
    2680             :                                 }
    2681             :                                 else
    2682             :                                 {
    2683           0 :                                         break;
    2684             :                                 }
    2685             : 
    2686             :                                 // calc wavelength corresponding to the upper end of the next channel
    2687           0 :                                 lambdaHi = lambda(loFBdown.back());
    2688             :                                 // calc wavelength corresponding to the lower end (in freq) of the next channel
    2689           0 :                                 lambdaLo = lambdaHi + theChanWidthX; // wavelength goes up as freq goes down
    2690             :                         }
    2691             : 
    2692             :                 }
    2693             :                  // should not get here
    2694             :                 else
    2695             :                 {
    2696           0 :                         oss << "Invalid value " << regridQuant << " for parameter \"mode\".";
    2697           0 :                         message = oss.str();
    2698           0 :                         return false;
    2699             :                 }
    2700             : 
    2701         827 :                 Int numNewChanDown = loFBdown.size();
    2702         827 :                 Int numNewChanUp = loFBup.size();
    2703             : 
    2704             :                 // central channel contained in both vectors
    2705         827 :                 newChanLoBound.resize(numNewChanDown + numNewChanUp - 1);
    2706             : 
    2707         827 :                 newChanHiBound.resize(numNewChanDown + numNewChanUp - 1);
    2708       88976 :                 for (Int i = 0; i < numNewChanDown; i++)
    2709             :                 {
    2710       88149 :                         Int k = numNewChanDown - i - 1; // Need to assign in reverse
    2711       88149 :                         newChanLoBound[i] = loFBdown[k];
    2712       88149 :                         newChanHiBound[i] = hiFBdown[k];
    2713             :                 }
    2714       87631 :                 for (Int i = 1; i < numNewChanUp; i++) // Start at 1 to omit the central channel here
    2715             :                 {
    2716       86804 :                         newChanLoBound[i + numNewChanDown - 1] = loFBup[i];
    2717       86804 :                         newChanHiBound[i + numNewChanDown - 1] = hiFBup[i];
    2718             :                 }
    2719             : 
    2720         827 :                 uInt nc = newChanLoBound.size();
    2721         827 :                 oss << " Number of channels = " << nc << endl;
    2722         827 :                 oss << " Total width of SPW (in output frame) = " << newChanHiBound[nc - 1] - newChanLoBound[0] << " Hz" << endl;
    2723         827 :                 oss << " Lower edge = " << newChanLoBound[0] << " Hz,"
    2724         827 :                         << " upper edge = " << newChanHiBound[nc - 1] << " Hz" << endl;
    2725             : 
    2726             :                 // Original SPW was in reverse order, need to restore that
    2727         827 :                 if (isDescending)
    2728             :                 {
    2729          36 :                         Vector<Double> tempL, tempU;
    2730          36 :                         tempL.assign(newChanLoBound);
    2731          36 :                         tempU.assign(newChanHiBound);
    2732       17266 :                         for (uInt i = 0; i < nc; i++)
    2733             :                         {
    2734       17230 :                                 newChanLoBound(i) = tempL(nc - 1 - i);
    2735       17230 :                                 newChanHiBound(i) = tempU(nc - 1 - i);
    2736             :                         }
    2737          36 :                 }
    2738             : 
    2739         827 :                 message = oss.str();
    2740             : 
    2741         827 :                 return true;
    2742             : 
    2743         827 :         } // end if (regridQuant== ...
    2744         831 : }
    2745             : 
    2746             : } //# NAMESPACE CASA - END

Generated by: LCOV version 1.16