Changeset 2466


Ignore:
Timestamp:
04/13/12 21:33:47 (13 years ago)
Author:
Kana Sugimoto
Message:

New Development: No

JIRA Issue: No (a bug fix)

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs: unit test of sdcal, using averageall=True

Put in Release Notes: No

Module(s):

Description:

Fixed a bug recently found by Malte in asapmath.new_average, with compel=True, or
sdcal with averageall=True.
The bug caused the method/task crash, when the spectra to be averaged over have
IFs with negative and positive frequency increments mixed.


File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/STMath.cpp

    r2437 r2466  
    30453045      uInt rows = tmpin[itable]->nrow() ;
    30463046      uInt freqnrows = tmpin[itable]->frequencies().table().nrow() ;
     3047      freqIDCol.attach( tmpin[itable]->table(), "FREQ_ID" ) ;
     3048      //ifnoCol.attach( tmpin[itable]->table(), "IFNO" ) ;
    30473049      for ( uInt irow = 0 ; irow < rows ; irow++ ) {
    30483050        if ( freqid[itable].size() == freqnrows ) {
     
    30503052        }
    30513053        else {
    3052           freqIDCol.attach( tmpin[itable]->table(), "FREQ_ID" ) ;
    3053           ifnoCol.attach( tmpin[itable]->table(), "IFNO" ) ;
    30543054          uInt id = freqIDCol( irow ) ;
    30553055          if ( freqid[itable].size() == 0 || count( freqid[itable].begin(), freqid[itable].end(), id ) == 0 ) {
    30563056            //os << "itable = " << itable << ": IF " << id << " is included in the list" << LogIO::POST ;
    30573057            vector<double> abcissa = tmpin[itable]->getAbcissa( irow ) ;
     3058            double lbedge, rbedge;
    30583059            freqid[itable].push_back( id ) ;
    3059             iffreq[itable].push_back( abcissa[0] - 0.5 * ( abcissa[1] - abcissa[0] ) ) ;
    3060             iffreq[itable].push_back( abcissa[abcissa.size()-1] + 0.5 * ( abcissa[1] - abcissa[0] ) ) ;
     3060            lbedge = abcissa[0] - 0.5 * ( abcissa[1] - abcissa[0] );
     3061            rbedge = abcissa[abcissa.size()-1] + 0.5 * ( abcissa[1] - abcissa[0] );
     3062            iffreq[itable].push_back( min(lbedge, rbedge) ) ;
     3063            iffreq[itable].push_back( max(lbedge, rbedge) ) ;
    30613064          }
    30623065        }
     
    31433146    // Grouping continuous IF groups (without frequency gap)
    31443147    // freqgrp: list of IF group indexes in each frequency group
    3145     // freqrange: list of minimum and maximum frequency in each frequency group
    31463148    // freqgrp[numgrp][nummember]
    31473149    // freqgrp: [[ifgrp00, ifgrp01, ifgrp02, ...],
     
    31493151    //           ...
    31503152    //           [ifgrpn0, ifgrpn1, ifgrpn2, ...]]
    3151     // freqrange[numgrp*2]
    3152     // freqrange: [min_grp0, max_grp0, min_grp1, max_grp1, ...]
    31533153    vector< vector<uInt> > freqgrp ;
    31543154    double freqrange = 0.0 ;
     
    33883388        double resol = abcissa[1] - abcissa[0] ;
    33893389        //os << "abcissa range  : [" << abcissa[0] << "," << abcissa[nchan-1] << "]" << LogIO::POST ;
    3390         if ( minfreq <= abcissa[0] )
    3391           nminchan = 0 ;
     3390        uInt imin, imax;
     3391        int sigres;
     3392        if ( resol >= 0. ) {
     3393          imin = 0;
     3394          imax = nchan - 1 ;
     3395          sigres = 1 ;
     3396        } else {
     3397          imin = nchan - 1 ;
     3398          imax = 0 ;
     3399          sigres = -1 ;
     3400          resol = abs(resol) ;
     3401        }
     3402        if ( minfreq <= abcissa[imin] )
     3403          nminchan = imin ;
    33923404        else {
    33933405          //double cfreq = ( minfreq - abcissa[0] ) / resol ;
    3394           double cfreq = ( minfreq - abcissa[0] + 0.5 * resol ) / resol ;
    3395           nminchan = int(cfreq) + ( ( cfreq - int(cfreq) <= 0.5 ) ? 0 : 1 ) ;
     3406          double cfreq = ( minfreq - abcissa[imin] + 0.5 * resol ) / resol ;
     3407          nminchan = imin + sigres * (int(cfreq) + ( ( cfreq - int(cfreq) <= 0.5 ) ? 0 : 1 )) ;
    33963408        }
    3397         if ( maxfreq >= abcissa[abcissa.size()-1] )
    3398           nmaxchan = abcissa.size() - 1 ;
     3409        if ( maxfreq >= abcissa[imax] )
     3410          nmaxchan = imax;
    33993411        else {
    34003412          //double cfreq = ( abcissa[abcissa.size()-1] - maxfreq ) / resol ;
    3401           double cfreq = ( abcissa[abcissa.size()-1] - maxfreq + 0.5 * resol ) / resol ;
    3402           nmaxchan = abcissa.size() - 1 - int(cfreq) - ( ( cfreq - int(cfreq) >= 0.5 ) ? 1 : 0 ) ;
     3413          double cfreq = ( abcissa[imax] - maxfreq + 0.5 * resol ) / resol ;
     3414          nmaxchan = imax - sigres * (int(cfreq) +( ( cfreq - int(cfreq) >= 0.5 ) ? 1 : 0 )) ;
    34033415        }
    3404         //os << "channel range (" << irow << "): [" << nminchan << "," << nmaxchan << "]" << LogIO::POST ;
     3416        //os << "freqrange = [" << abcissa[nminchan] << ", " << abcissa[nmaxchan] << "]"<< LogIO::POST;
     3417        if ( nmaxchan < nminchan ) {
     3418          int tmp = nmaxchan ;
     3419          nmaxchan = nminchan ;
     3420          nminchan = tmp ;
     3421        }
    34053422        if ( nmaxchan > nminchan ) {
    34063423          newin[itable]->reshapeSpectrum( nminchan, nmaxchan, irow ) ;
    34073424          int newchan = nmaxchan - nminchan + 1 ;
    3408           if ( count( freqIdUpdate.begin(), freqIdUpdate.end(), freqIdVec[itable][irow] ) == 0 && newchan < nchan )
     3425          if ( count( freqIdUpdate.begin(), freqIdUpdate.end(), freqIdVec[itable][irow] ) == 0 && newchan < nchan ) {
    34093426            freqIdUpdate.push_back( freqIdVec[itable][irow] ) ;
     3427
     3428            // Update before nminchan is lost
     3429            uInt freqId = freqIdVec[itable][irow] ;
     3430            Double refpix ;
     3431            Double refval ;
     3432            Double increment ;
     3433            newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ;
     3434            //refval = refval - ( refpix - nminchan ) * increment ;
     3435            refval = abcissa[nminchan] ;
     3436            refpix = 0 ;
     3437            newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ;
     3438          }
    34103439        }
    34113440        else {
     
    34133442        }
    34143443      }
    3415       for ( uInt i = 0 ; i < freqIdUpdate.size() ; i++ ) {
    3416         uInt freqId = freqIdUpdate[i] ;
    3417         Double refpix ;
    3418         Double refval ;
    3419         Double increment ;
     3444//       for ( uInt i = 0 ; i < freqIdUpdate.size() ; i++ ) {
     3445//      uInt freqId = freqIdUpdate[i] ;
     3446//      Double refpix ;
     3447//      Double refval ;
     3448//      Double increment ;
    34203449       
    3421         // update row
    3422         newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ;
    3423         refval = refval - ( refpix - nminchan ) * increment ;
    3424         refpix = 0 ;
    3425         newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ;
    3426       }   
     3450//      // update row
     3451//      newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ;
     3452//      refval = refval - ( refpix - nminchan ) * increment ;
     3453//      refpix = 0 ;
     3454//      newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ;
     3455//       }   
    34273456    }
    34283457
     
    34613490            if ( nchan < minchan ) {
    34623491              minchan = nchan ;
    3463               maxdnu = dnu ;
     3492              maxdnu = abs(dnu) ;
    34643493              newin[tableid]->frequencies().getEntry( refpixref, refvalref, refinc, rowid ) ;
    34653494              refreqid = rowid ;
    34663495              reftable = tableid ;
     3496              // Spectra are reversed when dnu < 0
     3497              if (dnu < 0)
     3498                refpixref = minchan - 1 -refpixref ;
    34673499            }
    34683500          }
     
    34793511          //os << "   regridChannel applied to " ;
    34803512          //if ( tableid != reftable )
    3481           refreqid = newin[tableid]->frequencies().addEntry( refpixref, refvalref, refinc ) ;
     3513          refreqid = newin[tableid]->frequencies().addEntry( refpixref, refvalref, maxdnu ) ;
    34823514          for ( uInt irow = 0 ; irow < newin[tableid]->table().nrow() ; irow++ ) {
    34833515            uInt tfreqid = freqIdVec[tableid][irow] ;
     
    35003532          vector<double> abcissa = newin[tableid]->getAbcissa( index ) ;
    35013533          minchan = abcissa.size() ;
    3502           maxdnu = abcissa[1] - abcissa[0] ;
     3534          maxdnu = abs( abcissa[1] - abcissa[0]) ;
    35033535        }
    35043536      }
     
    35383570    // combine frequency group
    35393571    os << "Combine spectra based on frequency grouping" << LogIO::POST ;
    3540     os << "IFNO is renumbered as frequency group ID (see above)" << LogIO::POST ;
     3572    //os << "IFNO is renumbered as frequency group ID (see above)" << LogIO::POST ;
     3573    os << "IFNO is renumbered as IF group ID (see above)" << LogIO::POST ;
    35413574    vector<string> coordinfo = tmpout->getCoordInfo() ;
    35423575    oldinfo[0] = coordinfo[0] ;
     
    35703603          vector<double> abcissa = tmpout->getAbcissa( irow ) ;
    35713604          double bw = ( abcissa[1] - abcissa[0] ) * abcissa.size() ;
    3572           int nchan = (int)( bw / gmaxdnu[igrp] ) ;
     3605          int nchan = (int) abs( bw / gmaxdnu[igrp] ) ;
     3606          // All spectra will have positive frequency increments
    35733607          tmpout->regridChannel( nchan, gmaxdnu[igrp], irow ) ;
    35743608          break ;
     
    35933627    cols[0] = String("POLNO") ;
    35943628    TableIterator iter( tab, cols ) ;
    3595     bool done = false ;
    35963629    vector< vector<uInt> > sizes( freqgrp.size() ) ;
    35973630    while( !iter.pastEnd() ) {
     
    36443677//       }
    36453678      // get a list of number of channels for each frequency group member
    3646       if ( !done ) {
    3647         for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) {
    3648           sizes[igrp].resize( freqgrp[igrp].size() ) ;
    3649           for ( uInt imem = 0 ; imem < freqgrp[igrp].size() ; imem++ ) {
    3650             for ( uInt irow = 0 ; irow < iter.table().nrow() ; irow++ ) {
    3651               uInt ifno = ifnoCol( irow ) ;
    3652               if ( ifno == freqgrp[igrp][imem] ) {
    3653                 Vector<Float> spec = specCols( irow ) ;
    3654                 sizes[igrp][imem] = spec.nelements() ;
    3655                 break ;
    3656               }               
    3657             }
    3658           }
    3659         }
    3660         done = true ;
     3679      for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) {
     3680        sizes[igrp].resize( freqgrp[igrp].size() ) ;
     3681        for ( uInt imem = 0 ; imem < freqgrp[igrp].size() ; imem++ ) {
     3682          for ( uInt irow = 0 ; irow < iter.table().nrow() ; irow++ ) {
     3683            uInt ifno = ifnoCol( irow ) ;
     3684            if ( ifno == freqgrp[igrp][imem] ) {
     3685              Vector<Float> spec = specCols( irow ) ;
     3686              sizes[igrp][imem] = spec.nelements() ;
     3687              break ;
     3688            }
     3689          }
     3690        }
    36613691      }
    36623692      // combine spectra
     
    37053735          specColOut.put( irow, newspec ) ;
    37063736          flagColOut.put( irow, newflag ) ;
    3707           // IFNO renumbering
     3737          // IFNO renumbering (renumbered as frequency group ID)
    37083738          ifnoColOut.put( irow, igrp ) ;
    37093739        }
     
    37163746      uInt index = 0 ;
    37173747      uInt pixShift = 0 ;
     3748
    37183749      while ( freqgrp[igrp][index] != gmemid[igrp] ) {
    37193750        pixShift += sizes[igrp][index++] ;
    37203751      }
    37213752      for ( uInt irow = 0 ; irow < out->table().nrow() ; irow++ ) {
    3722         if ( ifnoColOut( irow ) == gmemid[igrp] && !updated[igrp] ) {
     3753        //if ( ifnoColOut( irow ) == gmemid[igrp] && !updated[igrp] ) {
     3754        if ( ifnoColOut( irow ) == igrp && !updated[igrp] ) {
    37233755          uInt freqidOut = freqidColOut( irow ) ;
    37243756          //os << "freqgrp " << igrp << " freqidOut = " << freqidOut << LogIO::POST ;
     
    37273759          double increm ;
    37283760          out->frequencies().getEntry( refpix, refval, increm, freqidOut ) ;
     3761          if (increm < 0)
     3762            refpix = sizes[igrp][index] - 1 - refpix ; // reversed
    37293763          refpix += pixShift ;
    3730           out->frequencies().setEntry( refpix, refval, increm, freqidOut ) ;
     3764          out->frequencies().setEntry( refpix, refval, gmaxdnu[igrp], freqidOut ) ;
    37313765          updated[igrp] = true ;
    37323766        }
Note: See TracChangeset for help on using the changeset viewer.