stfinvbase.cc 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*! \file stfinvbase.cc
 * \brief C++ interface and abstract base class (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
 * \author Thomas Forbriger
 * \date 06/05/2011
 * 
 * C++ interface and abstract base class (implementation)
 * 
 * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach) 
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
 * 
 * REVISIONS and CHANGES 
 *  - 06/05/2011   V1.0   Thomas Forbriger
 *  - 30/09/2011   V1.1   implemented handling of additional time series pairs
33
 *  - 14/10/2015   V1.2   new end-user usage functions
34 35 36 37
 * 
 * ============================================================================
 */
#define STFINV_STFINVBASE_CC_VERSION \
38
  "STFINV_STFINVBASE_CC   V1.2"
39 40 41 42

#include <sstream>
#include <cmath>
#include <stfinv/stfinvbase.h>
43 44
#include <stfinv/stfinvbase_summary_usage.h>
#include <stfinv/stfinvbase_description_usage.h>
45 46 47 48
#include <stfinv/debug.h>

namespace stfinv {

49 50 51 52
  /*!
   * Constructor stores all references to time series data passed by the
   * caller.
   */
53 54 55 56 57 58 59 60 61 62 63 64
  STFBaseEngine::STFBaseEngine(const stfinv::Tvectoroftriples& triples,
                               const stfinv::Waveform& stf,
                               const stfinv::Tvectorofpairs& pairs,
                               const std::string& parameters)
    : Mtriples(triples), Mstf(stf), Mpairs(pairs),
      Mweights(0,triples.size()-1)
  {
    this->initialize(parameters);
  } // STFBaseEngine::STFBaseEngine(const stfinv::Tvectoroftriples& triples,

  /*----------------------------------------------------------------------*/

65 66 67 68
  /*!
   * Constructor stores all references to time series data passed by the
   * caller.
   */
69 70 71 72 73 74 75 76 77 78 79
  STFBaseEngine::STFBaseEngine(const stfinv::Tvectoroftriples& triples,
                               const stfinv::Waveform& stf,
                               const std::string& parameters)
    : Mtriples(triples), Mstf(stf), Mweights(0,triples.size()-1)
  {
    Mpairs.clear();
    this->initialize(parameters);
  } // STFBaseEngine::STFBaseEngine(const stfinv::Tvectoroftriples& triples,

  /*----------------------------------------------------------------------*/

80 81 82 83 84 85 86 87 88 89 90
  /*!
   * -# The STFBaseEngine::initialize() function parses the parameter string.
   *    Parsed values are stored in Mparamap.
   * -# Extract settings for parameters:
   *     - DEBUG: set debug output level
   *     - verbose: activate verbose output
   *     - exp: set power-law exponent and store weights in member data Mweights
   * -# Check consistency of data
   * -# Align index ranges of aff::Series objects such that the first element
   *    (sample) has index zero
   */
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  void STFBaseEngine::initialize(const std::string& parameters)
  {
    this->parseparameters(parameters);

    // extract parameters
    {
      std::istringstream is(this->parameter("DEBUG","0"));
      is >> Mdebug;
    }
    STFINV_debug(Mdebug>0, "STFBaseEngine::STFBaseEngine",
                 "initializing base class");
    Mverbose=(this->parameter("verbose","false")=="true");
    if (this->parameterisset("exp")) 
    {
      double k;
      std::istringstream is(this->parameter("exp","0."));
      is >> k;
      for (unsigned int i=0; i<Mtriples.size(); ++i)
      {
        double offset=Mtriples[i].offset();
        Mweights(i)=pow(offset,k);
      }
    } 
    else
    {
      Mweights=1.;
    }

    this->checkconsistency();

    // align index ranges; first index will be zero
    if (Mstf.series.first()!=0) { Mstf.series.shift(-Mstf.series.first()); }

    {
      stfinv::Tvectoroftriples::iterator I=Mtriples.begin();
      while (I!=Mtriples.end())
      {
        if (I->data.first()!=0) { I->data.shift(-(I->data.first())); }
        if (I->synthetics.first()!=0) 
        { I->synthetics.shift(-(I->synthetics.first())); }
        if (I->convolvedsynthetics.first()!=0) 
        { I->convolvedsynthetics.shift(-(I->convolvedsynthetics.first())); }
        ++I;
      } // while (I!=Mtriples.end())
    }

    {
      stfinv::Tvectorofpairs::iterator I=Mpairs.begin();
      while (I!=Mpairs.end())
      {
        if (I->synthetics.first()!=0) 
        { I->synthetics.shift(-(I->synthetics.first())); }
        if (I->convolvedsynthetics.first()!=0) 
        { I->convolvedsynthetics.shift(-(I->convolvedsynthetics.first())); }
        ++I;
      } // while (I!=Mpairs.end())
    }
  } // void STFBaseEngine::initialize(const std::string& parameters)

  /*----------------------------------------------------------------------*/

152 153 154 155 156 157 158
  /*!
   * Check consistency of time series data passed to the STFBaseEngine
   * with respect to
   *  -# number of samples
   *  -# sampling interval
   *
   */
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  void STFBaseEngine::checkconsistency() const
  {
    const unsigned int& n=Mstf.sampling.n;
    const double& dt=Mstf.sampling.dt;
    STFINV_assert(Mstf.sampling.n==Mstf.series.size(),
                  "inconsistent number of samples");
    const double tolerance=1.e-4;

    {
      stfinv::Tvectoroftriples::const_iterator I=Mtriples.begin();
      while (I!=Mtriples.end())
      {
        CTripleHeader header=I->header;
        STFINV_assert(header.sampling.n==n,
                      "inconsistent number of samples");
        STFINV_assert(header.sampling.n==I->data.size(),
                      "inconsistent number of samples");
        STFINV_assert(header.sampling.n==I->synthetics.size(),
                      "inconsistent number of samples");
        STFINV_assert(header.sampling.n==I->convolvedsynthetics.size(),
                      "inconsistent number of samples");
        STFINV_assert(std::abs(1.-header.sampling.dt/dt)<tolerance,
                      "inconsistent values of sampling interval");
        ++I;
      } // while (I!=Mtriples.end())
    }

    {
      stfinv::Tvectorofpairs::const_iterator I=Mpairs.begin();
      while (I!=Mpairs.end())
      {
        CWaveformHeader sampling=I->sampling;
        STFINV_assert(sampling.n==n,
                      "inconsistent number of samples");
        STFINV_assert(sampling.n==I->synthetics.size(),
                      "inconsistent number of samples");
        STFINV_assert(sampling.n==I->convolvedsynthetics.size(),
                      "inconsistent number of samples");
        STFINV_assert(std::abs(1.-sampling.dt/dt)<tolerance,
                      "inconsistent values of sampling interval");
        ++I;
      } // while (I!=Mpairs.end())
    }
  }

  /*----------------------------------------------------------------------*/

  void STFBaseEngine::help(std::ostream& os) const
  {
    STFBaseEngine::classhelp(os);
  } // void STFBaseEngine::help(std::ostream& os) const

  /*----------------------------------------------------------------------*/

  void STFBaseEngine::classhelp(std::ostream& os) 
  {
215
    os << stfinvbase_summary_usage;
216 217 218 219
  } // void STFBaseEngine::classhelp(std::ostream& os)

  /*----------------------------------------------------------------------*/

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  void STFBaseEngine::usage(std::ostream& os) const
  {
    STFBaseEngine::classusage(os);
  } // void STFBaseEngine::usage(std::ostream& os) const

  /*----------------------------------------------------------------------*/

  void STFBaseEngine::classusage(std::ostream& os) 
  {
    os << stfinvbase_description_usage;
  } // void STFBaseEngine::classusage(std::ostream& os)

  /*----------------------------------------------------------------------*/

  /*!
   * Pass the parameter string (as usually passed along the chain of
   * constructors to the constructor and initialize() function of
   * STFBaseEngine).
   * The values are stored in the member data
   * STFBaseEngine::Mparamap
   */
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  void STFBaseEngine::parseparameters(std::string parameters) 
  {
    while (parameters.length()>0)
    {
      std::string value=stfinv::tools::clipstring(parameters,":");
      std::string key
        =stfinv::tools::trimws(stfinv::tools::clipstring(value,"="));
      if (value.length()==0)
      {
        this->Mparamap[key]="true";
      }
      else
      {
        this->Mparamap[key]=value;
      }
    } // while (parameters.length()>0)
  } // void STFBaseEngine::parseparameters(std::string parameters)

  /*----------------------------------------------------------------------*/
  
  /*! \brief check is parameter was set
   *
   * \param key parameter key
   * \return true, if parameter was set by user
   */
  bool STFBaseEngine::parameterisset(const std::string& key) const
  {
    return (Mparamap.count(key)>0);
  } // bool STFBaseEngine::parameter(const std::string& key) const

  /*----------------------------------------------------------------------*/
  
  /*! \brief return the value of a parameters
   *
   * \param key parameter key
   * \param defvalue default value to be used is not set externally
   * \return parameter value or default value if key is not present.
   */
  std::string STFBaseEngine::parameter(const std::string& key,
                                       const std::string& defvalue) const
  {
    std::string retval;
    if (this->parameterisset(key))
    {
      retval=Mparamap[key];
    }
    else
    {
      retval=defvalue;
    }
    return retval;
  } // std::string STFBaseEngine::parameter()

  /*----------------------------------------------------------------------*/

  const char* STFBaseEngine::name() const
  {
    return("STFBaseEngine");
  } //  const char const* STFBaseEngine::name() const

  /*----------------------------------------------------------------------*/

  Tseries::Tcoc STFBaseEngine::recording(const unsigned int& i) const 
  {
    this->checkreceiverindex(i);
    return(Mtriples[i].data);
  }

  /*----------------------------------------------------------------------*/

  Tseries::Tcoc STFBaseEngine::synthetic(const unsigned int& i) const 
  {
    this->checkreceiverindex(i);
    return(Mtriples[i].synthetics);
  }

  /*----------------------------------------------------------------------*/

  Tseries STFBaseEngine::convolvedsynthetic(const unsigned int& i) const 
  {
    this->checkreceiverindex(i);
    return(Mtriples[i].convolvedsynthetics);
  }

  /*----------------------------------------------------------------------*/

  Tseries::Tcoc STFBaseEngine::series(const unsigned int& i) const 
  {
    this->checkseriesindex(i);
    return(Mpairs[i].synthetics);
  }

  /*----------------------------------------------------------------------*/

  Tseries STFBaseEngine::convolvedseries(const unsigned int& i) const 
  {
    this->checkseriesindex(i);
    return(Mpairs[i].convolvedsynthetics);
  }

  /*----------------------------------------------------------------------*/

  void STFBaseEngine::checkreceiverindex(const unsigned int& i) const 
  {
    STFINV_assert(i<Mtriples.size(),
                  "ERROR: receiver index out of range");
  }

  /*----------------------------------------------------------------------*/

  void STFBaseEngine::checkseriesindex(const unsigned int& i) const 
  {
    STFINV_assert(i<Mpairs.size(),
                  "ERROR: series index out of range");
  }

  /*======================================================================*/

  double WaveformTriple::offset() const
  {
    double dx=header.rx-header.sx;
    double dy=header.ry-header.sy;
    return (sqrt(dx*dx+dy*dy));
  } // double WaveformTriple::offset() const

} // namespace stfinv

/* ----- END OF stfinvbase.cc ----- */