All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Types | Public Member Functions | Protected Member Functions
GeopsyCore::TimeRangeList Class Reference

List of time ranges. More...

#include <TimeRangeList.h>

Inheritance diagram for GeopsyCore::TimeRangeList:
GeopsyGui::TimeWindowList

List of all members.

Public Types

typedef QList< TimeRange * >
::const_iterator 
const_iterator
typedef QList< TimeRange * >
::iterator 
iterator

Public Member Functions

void add (double frequency, const WindowingParameters &param, const SparseKeepSignal &keep, const TimeRange &r, QString *log)
void add (QTextStream &s, QString *log)
void add (const TimeRangeList &list)
void addBlanks (double frequency, const WindowingParameters &param, const SparseKeepSignal &keep, const TimeRange &r, QString *log)
TimeRangeaddOne (const TimeRange &r)
TimeRangeaddOne ()
const TimeRangeat (int index) const
TimeRangeat (int index)
iterator begin ()
const_iterator begin () const
bool clear ()
int count () const
iterator end ()
const_iterator end () const
void inverse (double frequency, const WindowingParameters &param, const SparseKeepSignal &keep, const TimeRange &r, QString *log)
bool isEmpty () const
double longestWindow () const
void remove (const TimeRange &r, QString *log)
void remove (int index, QString *log)
TimeRange timeRange () const
 TimeRangeList ()
 TimeRangeList (const TimeRangeList &o, int firstIndex)
QString toString () const
virtual ~TimeRangeList ()

Protected Member Functions

virtual TimeRangecloneTimeRange (TimeRange *r)
virtual void deleteTimeRange (TimeRange *r)
void internalClear ()
virtual TimeRangenewTimeRange (double start, double end)

Detailed Description

List of time ranges.

Subclass this class to store time ranges with more features (e.g. color) and re-implement newTimeRange(), cloneTimeRange(), deleteTimeRange(). TimeRange is not virtual to keep it as basic as possible.


Member Typedef Documentation


Constructor & Destructor Documentation

{}
GeopsyCore::TimeRangeList::TimeRangeList ( const TimeRangeList o,
int  firstIndex 
)

References at(), count(), GeopsyCore::TimeRange::end(), newTimeRange(), GeopsyCore::TimeRange::start(), and TRACE.

{
  TRACE;
  int n=count();
  for(int i=firstIndex;i<n;i++) {
    const TimeRange& r=o.at(i);
    append(newTimeRange(r.start(), r.end()));
  }
}

This function must be re-impleted in all subclasses. Virtual mechanism for deleteTimeRange() does not work for destructors. In each subclass, the list must be cleared to avoid re-delete in lower level classes.

References begin(), end(), internalClear(), and TRACE.

{
  TRACE;
  for(iterator it=begin(); it!=end(); ++it) {
    delete *it;
  }
  internalClear();
}

Member Function Documentation

void GeopsyCore::TimeRangeList::add ( double  frequency,
const WindowingParameters param,
const SparseKeepSignal keep,
const TimeRange r,
QString *  log 
)

Add windows in tw time interval according to windowing parameters param and keep vectors keeps.

References GeopsyCore::WindowingParameters::badSampleTolerance(), CONST_LOCK_SAMPLES, GeopsyCore::KeepSignal::deltaT(), GeopsyCore::TimeRange::end(), GeopsyCore::SparseKeepSignal::keeps(), GeopsyCore::WindowingParameters::maximumLength(), GeopsyCore::WindowingParameters::minimumLength(), newTimeRange(), GeopsyCore::SignalTemplate< sampleType >::nSamples(), GeopsyCore::WindowingParameters::overlap(), GeopsyCore::TimeRange::start(), GeopsyCore::KeepSignal::t0(), QGpCoreTools::tr(), TRACE, and UNLOCK_SAMPLES.

Referenced by addBlanks(), MonoStation::AbstractStation::addWindows(), RealTimeArrayManager::createTasks(), inverse(), and ArrayCore::ArrayProcess::setFrequency().

{
  TRACE;
  //App::stream() << tr("add time windows from %1 to %2").arg(r.start()).arg(r.end()) << endl;
  const QVector<KeepSignal *>& keeps=keep.keeps();
  for(QVector<KeepSignal *>::const_iterator it=keeps.begin(); it!=keeps.end(); it++) {
    const KeepSignal * keepSig=*it;

    double deltaT=keepSig->deltaT();
    double freqSamp=1.0/deltaT;

    int nMin=(int)round(param.minimumLength(frequency)*freqSamp);
    int nMax=(int)round(param.maximumLength(frequency)*freqSamp);
    int nOverlap=(int)round((param.overlap()*0.01)*(double)nMax);
    int nBadTol=(int)round(param.badSampleTolerance()*freqSamp);

    int nStart=(int)round((r.start()-keepSig->t0())*freqSamp);
    if(nStart>keepSig->nSamples()) {
      continue;
    } else if(nStart<0) {
      nStart=0;
    }
    int nEnd=(int)round((r.end()-keepSig->t0())*freqSamp);
    if(nEnd<0) {
      continue;
    } else if(nEnd>keepSig->nSamples()) {
      nEnd=keepSig->nSamples();
    }
    //printf("nStart %i nEnd %i\n",nStart, nEnd);
    CONST_LOCK_SAMPLES(int, kSamp, keepSig)
      int i=nStart;
      int * badSamples=new int[nMax];
      int nBadSample, winStart, winLength;
      while(i<nEnd) {
        while(i<nEnd && kSamp[i]==-1) { // find beginning of first window
          i++;
        }
        int acceptableSample=i;
        while(i<nEnd && kSamp[i]==0) { // finds beginning of first window
          i++;
        }
        i-=nBadTol; // Admits nbad_tol bad samples so go back
        if(i<acceptableSample) {
          i=acceptableSample;
        }
        nBadSample=0;
        winStart=-1;
        winLength=0;
        // Try to find a window at least with ntmin samples and with less than
        // bad_tol number of bad samples
        do {
          if(winStart==-1) {
            winStart=i;
          } else {
            // Restarting because n_bad_sample==bad_tol and i-start_w+1<ntmin
            // This means a too short window with too much bad samples
            // Restart window at the first bad_sample
            winLength-=badSamples[0]+1-winStart;
            winStart=badSamples[0] + 1;
            // move all bad samples in the vector
            for(int j=1; j<nBadSample; j++) {
              badSamples[j-1]=badSamples[j];
            }
            nBadSample--;
            //printf("Restarting i=%i winStart=%i winLength=%i nBadSample=%i\n",i,
            //     winStart,winLength,nBadSample);
          }
          while(i<nEnd && nBadSample<=nBadTol && winLength<nMax) {
            if(kSamp[i]==0) {
              badSamples[nBadSample]=i;
              nBadSample++;
              //printf("Found bad sample at %i, nBadSample=%i\n",i,nBadSample);
            } else if(kSamp[i]==-1) {
              // This sample cannot be tolerated
              //printf("Found very bad sample at %i, restarting\n",i);
              break;
            }
            winLength++;
            i++;
          }
          if(kSamp[i]==-1) {
            break;
          }
        } while(i<nEnd && winLength<nMin);
        // Found a window
        if(winLength>=nMin) {
          TimeRange * newRange=newTimeRange(winStart*deltaT+keepSig->t0(),
                                            i*deltaT+keepSig->t0());
          //App::stream() << tr("good win: start=%1 end=%2").arg(newRange->start()).arg(newRange->end()) << endl;
          append(newRange);
          if(log) {
            (*log)+=tr("Adding window from %1 to %2 s.\n").arg(newRange->start()).arg(newRange->end());
          }
          // Due to overlap, the next window can start before current i
          int effOverlap=nOverlap-1;
          if(effOverlap>=winLength) {
            effOverlap=winLength-1;
          }
          i-=effOverlap;
        }
      }
      delete [] badSamples;
    UNLOCK_SAMPLES(keepSig)
  }
}
void GeopsyCore::TimeRangeList::add ( QTextStream &  s,
QString *  log 
)

References newTimeRange(), QGpCoreTools::tr(), and TRACE.

{
  TRACE;
  QString line;
  while( !s.atEnd()) {
    line=s.readLine().simplified();
    if(line.contains( "### End Time Windows ###" ))
      return ;
    while( !s.atEnd() && line.left(1)=="#" ) {
      line=s.readLine().trimmed().simplified();
      ;
      if(line.contains( "### End Time Windows ###" ))
        return ;
    }
    double winStart=line.section(QRegExp( "[ \t]" ), 0, 0).toDouble();
    double winEnd=line.section(QRegExp( "[ \t]" ), 1, 1).toDouble();
    if(winStart < winEnd) {
      append(newTimeRange(winStart, winEnd));
      if(log) {
        (*log)+= tr( "Adding window from %1 to %2 s.\n" ).arg(winStart).arg(winEnd);
      }
    }
  }
}

References begin(), cloneTimeRange(), end(), and TRACE.

{
  TRACE;
  for(const_iterator it=list.begin();it!=list.end();++it) {
    append(cloneTimeRange(*it));
  }
}
void GeopsyCore::TimeRangeList::addBlanks ( double  frequency,
const WindowingParameters param,
const SparseKeepSignal keep,
const TimeRange r,
QString *  log 
)

Add windows according to param in available blanks

References add(), GeopsyCore::SparseKeepSignal::copySamplesFrom(), GeopsyCore::WindowingParameters::overlap(), and TRACE.

Referenced by MonoStation::AbstractStation::addWindows().

{
  TRACE;
  SparseKeepSignal localKeep=keep;
  localKeep.copySamplesFrom(keep);
  blanksToKeep(localKeep, param.overlap());
  add(frequency, param, localKeep, r, log);
}

Reimplemented in GeopsyGui::TimeWindowList.

References GeopsyCore::TimeRange::end(), newTimeRange(), and GeopsyCore::TimeRange::start().

{
  TimeRange * rr=newTimeRange(r.start(), r.end());
  append(rr);
  return *rr;
}

Reimplemented in GeopsyGui::TimeWindowList.

References newTimeRange().

{
  TimeRange * r=newTimeRange(0.0, 0.0);
  append(r);
  return *r;
}
const TimeRange& GeopsyCore::TimeRangeList::at ( int  index) const [inline]
TimeRange& GeopsyCore::TimeRangeList::at ( int  index) [inline]

Reimplemented in GeopsyGui::TimeWindowList.

{return *QList<TimeRange *>::operator[](index);}

Returns false if it is already empty

References begin(), deleteTimeRange(), end(), internalClear(), isEmpty(), and TRACE.

Referenced by MonoStation::AbstractStation::clearAllWindows(), LinearFKActiveArrayStations::clearTimeWindows(), inverse(), and ArrayCore::ArrayProcess::setFrequency().

{
  TRACE;
  if(isEmpty()) {
    return false;
  } else {
    for(iterator it=begin(); it!=end(); ++it) {
      deleteTimeRange(*it);
    }
    internalClear();
    return true;
  }
}
virtual TimeRange* GeopsyCore::TimeRangeList::cloneTimeRange ( TimeRange r) [inline, protected, virtual]

Reimplemented in GeopsyGui::TimeWindowList.

Referenced by add().

{return new TimeRange(*r);}
int GeopsyCore::TimeRangeList::count ( ) const [inline]
virtual void GeopsyCore::TimeRangeList::deleteTimeRange ( TimeRange r) [inline, protected, virtual]

Reimplemented in GeopsyGui::TimeWindowList.

Referenced by clear(), and remove().

{delete r;}
void GeopsyCore::TimeRangeList::internalClear ( ) [inline, protected]
void GeopsyCore::TimeRangeList::inverse ( double  frequency,
const WindowingParameters param,
const SparseKeepSignal keep,
const TimeRange r,
QString *  log 
)

References add(), clear(), GeopsyCore::SparseKeepSignal::initValues(), GeopsyCore::WindowingParameters::overlap(), QGpCoreTools::tr(), and TRACE.

Referenced by MonoStation::AbstractStation::inverseWindows().

{
  TRACE;
  SparseKeepSignal localKeep=keep;
  localKeep.initValues(1);
  blanksToKeep(localKeep, param.overlap());
  clear();
  if(log) {
    (*log)+= tr( "Inverse windows\n" );
  }
  add(frequency, param, localKeep, r, log);
}
bool GeopsyCore::TimeRangeList::isEmpty ( ) const [inline]

Returns the length in seconds of the longest time window

References begin(), end(), and TRACE.

{
  TRACE;
  double dtmax=0.0;
  for(const_iterator it=begin();it!=end();++it) {
    double dt=(*it)->lengthSeconds();
    if(dt > dtmax)
      dtmax=dt;
  }
  return dtmax;
}
virtual TimeRange* GeopsyCore::TimeRangeList::newTimeRange ( double  start,
double  end 
) [inline, protected, virtual]

Reimplemented in GeopsyGui::TimeWindowList.

Referenced by add(), addOne(), and TimeRangeList().

{return new TimeRange(start, end);}
void GeopsyCore::TimeRangeList::remove ( const TimeRange globalRange,
QString *  log 
)

Remove all time windows inside or partially inside tw.

References at(), count(), deleteTimeRange(), GeopsyCore::TimeRange::end(), GeopsyCore::TimeRange::intersects(), GeopsyCore::TimeRange::start(), QGpCoreTools::tr(), and TRACE.

Referenced by MonoStation::AbstractStation::clearGrayWindows(), and MonoStation::AbstractStation::removeWindows().

{
  TRACE;
  for(int i=0;i < count(); ) {
    TimeRange& r=at(i);
    if(r.intersects(globalRange)) {
      if(log) {
        (*log)+= tr( "Removing window from %1 to %2 s.\n" ).arg(r.start()).arg(r.end());
      }
      deleteTimeRange(&r);
      removeAt(i);
    } else
      ++i;
  }
}
void GeopsyCore::TimeRangeList::remove ( int  index,
QString *  log 
)

References at(), deleteTimeRange(), GeopsyCore::TimeRange::end(), GeopsyCore::TimeRange::start(), QGpCoreTools::tr(), and TRACE.

{
  TRACE;
  TimeRange& r=at(index);
  if(log) {
    (*log)+= tr( "Removing window from %1 to %2 s.\n" ).arg(r.start()).arg(r.end());
  }
  deleteTimeRange(&r);
  QList<TimeRange *>::removeAt(index);
}

References begin(), GeopsyCore::TimeRange::end(), end(), isEmpty(), GeopsyCore::TimeRange::setEnd(), GeopsyCore::TimeRange::setStart(), GeopsyCore::TimeRange::start(), and TRACE.

Referenced by RealTimeArrayManager::createTasks().

{
  TRACE;
  if(isEmpty()) return TimeRange();
  const_iterator it=begin();
  TimeRange globalRange=**it;
  for(it++;it!=end();++it) {
    const TimeRange& r=**it;
    if(r.start()<globalRange.start()) {
      globalRange.setStart(r.start());
    }
    if(r.end()>globalRange.end()) {
      globalRange.setEnd(r.end());
    }
  }
  return globalRange;
}

References begin(), count(), GeopsyCore::TimeRange::end(), end(), GeopsyCore::TimeRange::lengthSeconds(), GeopsyCore::TimeRange::start(), str, QGpCoreTools::tr(), TRACE, and w.

Referenced by MonoStation::AbstractTool::setLog().

{
  TRACE;
  QString str;
  str+="### Time Windows ###\n";
  str+=tr("# Number= %1\n").arg(count());
  str+=tr("# Start time \t End Time \t Window length\n");
  for(const_iterator it=begin();it!=end();++it) {
    TimeRange& w=**it;
    str+=QString("%1\t%2\t%3\n").arg(w.start()).arg(w.end()).arg(w.lengthSeconds());
  }
  str+="### End Time Windows ###\n";
  return str;
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines