Testing SQL idea for UanPropModelBh
authorltracy
Tue Apr 07 16:49:08 2009 -0700 (10 months ago)
changeset 4314edd3003429c7
parent 4313 6dbd1950cc01
child 4315 24321b91c50b
Testing SQL idea for UanPropModelBh
src/devices/uan/uan-prop-model-bh.h
src/devices/uan/uan-prop-model.h
uan-apps/create-dat.cc
     1.1 --- a/src/devices/uan/uan-prop-model-bh.h	Fri Apr 03 00:07:22 2009 -0700
     1.2 +++ b/src/devices/uan/uan-prop-model-bh.h	Tue Apr 07 16:49:08 2009 -0700
     1.3 @@ -151,6 +151,7 @@
     1.4    bool Configure(std::string configFileName);
     1.5  
     1.6  
     1.7 +  BhIndex MakeIndex(uint32_t freqHz, uint32_t srcDepthM, uint32_t recDepthM, uint32_t rangeM);
     1.8  
     1.9  private:
    1.10    ///Updates information in dat;
    1.11 @@ -164,7 +165,7 @@
    1.12    BhConfig m_Config;
    1.13    void RetreiveFromFile(BhIndex index);
    1.14    BellhopResp &GetResp(uint32_t freqHz, uint32_t srcDepthM, uint32_t recDepthM, uint32_t rangeM);
    1.15 -  BhIndex MakeIndex(uint32_t freqHz, uint32_t srcDepthM, uint32_t recDepthM, uint32_t rangeM);
    1.16 +
    1.17  
    1.18  };
    1.19  
     2.1 --- a/src/devices/uan/uan-prop-model.h	Fri Apr 03 00:07:22 2009 -0700
     2.2 +++ b/src/devices/uan/uan-prop-model.h	Tue Apr 07 16:49:08 2009 -0700
     2.3 @@ -42,6 +42,7 @@
     2.4   */
     2.5  class Tap {
     2.6  public:
     2.7 +  Tap() : m_amplitude(0), m_delay(Seconds(0)) { }
     2.8    /**
     2.9     * \param delay Time delay (usually from first arrival) of signal
    2.10     * \param amp Complex amplitude of arrival
     3.1 --- a/uan-apps/create-dat.cc	Fri Apr 03 00:07:22 2009 -0700
     3.2 +++ b/uan-apps/create-dat.cc	Tue Apr 07 16:49:08 2009 -0700
     3.3 @@ -30,6 +30,8 @@
     3.4  
     3.5  //#include "ns3/log.h"
     3.6  
     3.7 +#define USE_SQL_DAT
     3.8 +
     3.9  #include <iostream>
    3.10  #include <fstream>
    3.11  #include <algorithm>
    3.12 @@ -39,6 +41,9 @@
    3.13  #include <set>
    3.14  #include <vector>
    3.15  #include <iomanip>
    3.16 +#ifdef USE_SQL_DAT
    3.17 +#include <sqlite3.h>
    3.18 +#endif
    3.19  
    3.20  using namespace ns3;
    3.21  using namespace std;
    3.22 @@ -48,7 +53,15 @@
    3.23  
    3.24  NS_LOG_COMPONENT_DEFINE("BhCreateDat");
    3.25  
    3.26 -
    3.27 +static int
    3.28 +sqlCallback(void *vd, int32_t argc, char **argv, char **cname)
    3.29 +{
    3.30 +  for(int32_t i=0;i<argc;i++)
    3.31 +    {
    3.32 +      std::cout << "Column: " << cname[i] << " is " << argv[i] << std::endl;
    3.33 +    }
    3.34 +  return 0;
    3.35 +}
    3.36  class DatMaker : public BhConfig
    3.37  {
    3.38  
    3.39 @@ -58,6 +71,9 @@
    3.40    virtual bool Config(const char *cfgFile);
    3.41  
    3.42  private:
    3.43 +  typedef std::list<pair<double, double> > SspList;
    3.44 +  //pair < delay in seconds, amplitude >
    3.45 +  typedef std::vector<pair<double, complex<double> > > TapVec;
    3.46    string m_BhPath;
    3.47    uint32_t m_Nmedia;
    3.48    string m_Ssopt;
    3.49 @@ -65,7 +81,7 @@
    3.50    uint32_t m_TopSigma;
    3.51    double m_Zdepth;
    3.52  
    3.53 -  string m_Ssp;  //string containing entire SSP
    3.54 +  SspList m_sspVals;
    3.55    string m_BotSigma;
    3.56    double m_BotRough;
    3.57  
    3.58 @@ -86,12 +102,23 @@
    3.59  
    3.60    uint32_t m_PreTaps;
    3.61    uint32_t m_TotalTaps;
    3.62 -
    3.63 +  double m_resolution;
    3.64  
    3.65    void WriteEnvFile(string filename, uint32_t freqHz);
    3.66 +  void WriteEnvFile(string filename, uint32_t freqHz, SspList ssp);
    3.67    void ReadArrivals(list<BellhopResp> &results);
    3.68    set<string> FillParams(void);
    3.69 +
    3.70 +#ifdef USE_SQL_DAT
    3.71 +  void WriteResults(list<BellhopResp> &results, sqlite3 *sqldb);
    3.72 +  sqlite3 *CreateDb(string dbfile);
    3.73 +#else
    3.74    void WriteResults(list<BellhopResp> &results);
    3.75 +#endif
    3.76 +  list<TapVec> SumTapVecList(list<TapVec> &a, list<TapVec> &b);
    3.77 +  list<DatMaker::TapVec> ProcessTaps(list<BellhopResp> &results);
    3.78 +  SspList PerturbSsp(void);
    3.79 +
    3.80  };
    3.81  
    3.82  void
    3.83 @@ -105,6 +132,11 @@
    3.84  void
    3.85  DatMaker::WriteEnvFile(string filename, uint32_t freqHz)
    3.86  {
    3.87 +  WriteEnvFile(filename, freqHz, m_sspVals);
    3.88 +}
    3.89 +void
    3.90 +DatMaker::WriteEnvFile(string filename, uint32_t freqHz, SspList ssp)
    3.91 +{
    3.92  
    3.93    ofstream env(filename.c_str());
    3.94  
    3.95 @@ -112,13 +144,12 @@
    3.96    env << freqHz << endl << m_Nmedia << endl << m_Ssopt << endl;
    3.97    env << m_Nmesh << " " << m_TopSigma << " " << m_Zdepth << endl;
    3.98  
    3.99 -//  list<pair<double, double> >::iterator it = m_Ssp.begin();
   3.100 +  list<pair<double, double> >::iterator it = ssp.begin();
   3.101  
   3.102 -//  for(;it != m_Ssp.end(); it++)
   3.103 -//  {
   3.104 -//      env << (*it).first << " " << (*it).second << " /" << endl;
   3.105 -//    }
   3.106 -  env << m_Ssp;
   3.107 +  for(;it != ssp.end(); it++)
   3.108 +    {
   3.109 +      env << (*it).first << " " << (*it).second << " /" << endl;
   3.110 +    }
   3.111  
   3.112    env << m_BotSigma << " " << m_BotRough << endl;
   3.113    env << m_BotArgs << endl;
   3.114 @@ -181,6 +212,7 @@
   3.115    params.insert("tapsbeforemax");
   3.116    params.insert("totaltaps");
   3.117    params.insert("bottomargument");
   3.118 +  params.insert("resolution");
   3.119  
   3.120    return params;
   3.121  }
   3.122 @@ -234,8 +266,15 @@
   3.123                    break;
   3.124                  }
   3.125                buf << line << endl;
   3.126 +              uint32_t comPos = line.find_first_of(",");
   3.127 +              if(comPos == string::npos)
   3.128 +                {
   3.129 +                  NS_FATAL_ERROR("Invalid SSP entry: " << line);
   3.130 +                }
   3.131 +              string firstArg = trim(line.substr(0,comPos));
   3.132 +              string scdArg = trim(line.substr(comPos+1, line.length() - comPos + 1));
   3.133 +              m_sspVals.push_back(make_pair(atof(firstArg.c_str()), atof(scdArg.c_str())));
   3.134              }
   3.135 -          m_Ssp = buf.str();
   3.136          }
   3.137        else
   3.138          {
   3.139 @@ -364,6 +403,10 @@
   3.140                m_TotalTaps = atoi(configValue.c_str());
   3.141                NS_LOG_DEBUG("Set total taps: " << m_TotalTaps << " taps");
   3.142              }
   3.143 +          else if(configParam == "resolution")
   3.144 +            {
   3.145 +              m_resolution = atof(configValue.c_str());
   3.146 +            }
   3.147            else
   3.148              continue;
   3.149  
   3.150 @@ -381,7 +424,7 @@
   3.151          {
   3.152            NS_LOG_WARN(*it);
   3.153          }
   3.154 -
   3.155 +      NS_FATAL_ERROR("Missing parameters");
   3.156        return false;
   3.157      }
   3.158    return true;
   3.159 @@ -406,79 +449,175 @@
   3.160  
   3.161  }
   3.162  
   3.163 +list<DatMaker::TapVec>
   3.164 +DatMaker::SumTapVecList(list<TapVec> &a, list<TapVec> &b)
   3.165 +{
   3.166 +  NS_ASSERT(a.size() == b.size());
   3.167 +  list<TapVec> out;
   3.168 +
   3.169 +  list<TapVec>::iterator ait = a.begin();
   3.170 +  list<TapVec>::iterator bit = b.begin();
   3.171 +  for(;ait != a.end();ait++, bit++)
   3.172 +    {
   3.173 +      NS_ASSERT(ait->size() == bit->size());
   3.174 +      TapVec outvec(ait->size());
   3.175 +      for(uint32_t i=0;i < ait->size();i++)
   3.176 +        {
   3.177 +          TapVec &av = *ait;
   3.178 +          TapVec &bv = *bit;
   3.179 +          outvec[i] = make_pair(av[i].first, abs(av[i].second) + abs(bv[i].second));
   3.180 +        }
   3.181 +      out.push_back(outvec);
   3.182 +    }
   3.183 +  return out;
   3.184 +}
   3.185 +
   3.186 +DatMaker::SspList
   3.187 +DatMaker::PerturbSsp(void)
   3.188 +{
   3.189 +  NormalVariable nrv(m_PerturbMean, m_PerturbVariance);
   3.190 +
   3.191 +  SspList outlist;
   3.192 +  for(SspList::iterator it = m_sspVals.begin();it != m_sspVals.end();it++)
   3.193 +    {
   3.194 +      outlist.push_back(make_pair(it->first, it->second + nrv.GetValue()));
   3.195 +    }
   3.196 +  return outlist;
   3.197 +}
   3.198 +
   3.199  void
   3.200  DatMaker::MakeDatFiles(void)
   3.201  {
   3.202    string envFile("make-dat-envfile.env");
   3.203 -  //uint32_t freq = m_MinFreqHz;
   3.204    ostringstream bhcommand;
   3.205    bhcommand << m_BhPath << " < " << envFile << " > /dev/null";
   3.206  
   3.207 -  list<BellhopResp> results;
   3.208 -  //list<BellhopResp> procResults;
   3.209 +#ifdef USE_SQL_DAT
   3.210 +  sqlite3 *sqldb = CreateDb("sqldbtest.db");
   3.211 +#endif
   3.212  
   3.213    for(uint32_t fr = 0; fr < m_NumFreqs; fr++)
   3.214      {
   3.215 -      //TODO:  Implement environment perturbation
   3.216 -      //PerturbEnvironment();
   3.217 -      WriteEnvFile(envFile, m_MinFreqHz + m_FreqStepHz * fr);
   3.218 -      system(bhcommand.str().c_str());
   3.219 -      //NS_ASSERT(0);
   3.220 -      results.clear();
   3.221 -      ReadArrivals(results);
   3.222 -      //TODO:  Implement results combining after perturbation
   3.223 -      //ProcessResults()
   3.224 +
   3.225 +      list<BellhopResp> results;
   3.226 +
   3.227 +      list<BellhopResp> runResults;
   3.228 +      list<TapVec> finalTaps;
   3.229 +      for(uint32_t avg = 0; avg <= m_NumAverages;avg++)
   3.230 +        {
   3.231 +          //Do run
   3.232 +          if(m_NumAverages > 0)
   3.233 +            WriteEnvFile(envFile, m_MinFreqHz + m_FreqStepHz * fr, PerturbSsp());
   3.234 +          else
   3.235 +            WriteEnvFile(envFile, m_MinFreqHz + m_FreqStepHz * fr);
   3.236 +          system(bhcommand.str().c_str());
   3.237 +          ReadArrivals(runResults);
   3.238 +          list<TapVec> run = ProcessTaps(runResults);
   3.239 +          if(avg == 0)
   3.240 +            {
   3.241 +              finalTaps = run;
   3.242 +            }
   3.243 +          else
   3.244 +            {
   3.245 +              NS_LOG_DEBUG("Summing taps: finalTaps.size()=" << finalTaps.size()
   3.246 +                           << " run.size()=" << run.size());
   3.247 +              finalTaps = SumTapVecList(finalTaps, run);
   3.248 +            }
   3.249 +
   3.250 +        }
   3.251 +      list<TapVec>::iterator fit = finalTaps.begin();
   3.252 +      list<BellhopResp>::iterator bhrit = runResults.begin();
   3.253 +      UanPdp avgdPdp;
   3.254 +      for(;fit != finalTaps.end();fit++, bhrit++)
   3.255 +        {
   3.256 +          TapVec &a = (*fit);
   3.257 +          double pLoss = 0;
   3.258 +          for(uint32_t i=0; i < a.size(); i++)
   3.259 +            {
   3.260 +              if(m_NumAverages > 0)
   3.261 +                {
   3.262 +                  avgdPdp.AddTap(Tap(Seconds(a[i].first), (a[i].second) / ((double) m_NumAverages)));
   3.263 +                  pLoss += abs(a[i].second) / m_NumAverages;
   3.264 +                }
   3.265 +              else
   3.266 +                {
   3.267 +                  avgdPdp.AddTap(Tap(Seconds(a[i].first), a[i].second));
   3.268 +                  pLoss += abs(a[i].second);
   3.269 +                }
   3.270 +            }
   3.271 +          BellhopResp r(bhrit->GetFreqHz(), bhrit->GetSrcDepthM(),
   3.272 +              bhrit->GetRecDepthM(), bhrit->GetRangeM(), avgdPdp, pLoss);
   3.273 +          results.push_back(r);
   3.274 +        }
   3.275 +
   3.276 +#ifdef USE_SQL_DAT
   3.277 +      WriteResults(results, sqldb);
   3.278 +#else
   3.279        WriteResults(results);
   3.280 +#endif
   3.281      }
   3.282  
   3.283  }
   3.284  
   3.285 -void
   3.286 -DatMaker::WriteResults(list<BellhopResp> &results)
   3.287 +
   3.288 +
   3.289 +
   3.290 +list<DatMaker::TapVec>
   3.291 +DatMaker::ProcessTaps(list<BellhopResp> &results)
   3.292  {
   3.293 -  ostringstream datFileName;
   3.294 -  //expecting results to be in the order read into in ReadArrivals.
   3.295 -  //i.e. sd1 rd1 rr1 .. rrn rd2 rr1 .. rrn .. rdn rr1 rrn sd2 rd1 rr1 ... sdn
   3.296 +  list<BellhopResp>::iterator it = results.begin();
   3.297 +  list<TapVec> out;
   3.298  
   3.299 -  ofstream outf;
   3.300 -  //list<BellhopResp>::iterator it = results.front();
   3.301 -  for (uint32_t sd = 0; sd < m_NumSrcDepth; sd++)
   3.302 +  for(;it != results.end();it++)
   3.303      {
   3.304 -      NS_ASSERT(!results.empty());
   3.305 -      datFileName.str("");
   3.306 -      datFileName << fixed << setprecision(1) << m_DataPath << results.front().GetSrcDepthM() << "-" << results.front().GetFreqHz() << ".dat";
   3.307 -      outf.open(datFileName.str().c_str());
   3.308 -      if(!outf.good())
   3.309 +      //Combine taps
   3.310 +      UanPdp pdp = it->GetPdp();
   3.311 +
   3.312 +      TapVec tempTap;
   3.313 +      UanPdp::Iterator pit = pdp.GetBegin();
   3.314 +      for(;pit != pdp.GetEnd();pit++)
   3.315          {
   3.316 -          NS_FATAL_ERROR("Fatal error opening file: " << datFileName.str());
   3.317 +          double runTime = 0;
   3.318 +          complex<double> tapSum = 0;
   3.319 +          while(pit != pdp.GetEnd() && runTime < m_resolution)
   3.320 +            {
   3.321 +              runTime += pit->GetDelay().GetSeconds();
   3.322 +              tapSum += pit->GetAmp();
   3.323 +            }
   3.324 +          tempTap.push_back(make_pair(tempTap.size()*m_resolution, tapSum));
   3.325          }
   3.326 -      for (uint32_t rd = 0; rd < m_NumRecDepth; rd++)
   3.327 +      //Find max index
   3.328 +      uint32_t maxIndex = 0;
   3.329 +      double maxValue = -1;
   3.330 +      for(uint32_t i=0;i<tempTap.size();i++)
   3.331          {
   3.332 -          outf << "rd " << m_MinRecDepthM + rd*m_RecDepthStepM << endl;
   3.333 -          for (uint32_t rr = 0; rr < m_NumRanges; rr++)
   3.334 +          if(abs(tempTap[i].second) > maxValue )
   3.335              {
   3.336 -              BellhopResp resp = results.front();
   3.337 -              results.pop_front();
   3.338 -              NS_LOG_DEBUG("Results source depth: " << resp.GetSrcDepthM() << " Expected depth: " << m_MinSrcDepthM + sd*m_SrcDepthStepM);
   3.339 -              NS_LOG_DEBUG("Results receiver depth: " << resp.GetRecDepthM() << " Expected depth: " << m_MinRecDepthM + rd*m_RecDepthStepM);
   3.340 -              NS_LOG_DEBUG("Results range: " << resp.GetRangeM() << " Expected range: " << 1000*(m_MinRangeKm + rr*m_RangeStepKm));
   3.341 -              NS_ASSERT(resp.GetSrcDepthM() == m_MinSrcDepthM + sd* m_SrcDepthStepM);
   3.342 -              //NS_ASSERT(resp.GetRangeM() == uint32_t (1000*(m_MinRangeKm+ rr * m_RangeStepKm)));
   3.343 -              NS_ASSERT(resp.GetRecDepthM() == m_MinRecDepthM + rd*m_RecDepthStepM);
   3.344 -
   3.345 -              outf << "rr " << resp.GetRangeM() << endl;
   3.346 -              UanPdp pdp = resp.GetPdp();
   3.347 -              outf << resp.GetPathLossDb() << endl;
   3.348 -              outf << pdp;
   3.349 -              outf << endl;
   3.350 -
   3.351 +              maxIndex = i;
   3.352 +              maxValue = abs(tempTap[i].second);
   3.353              }
   3.354          }
   3.355 -      outf.close();
   3.356 +      TapVec final;
   3.357 +      //Align taps and fill in output TapVec
   3.358 +      uint32_t finalIndex = 0;
   3.359 +      for(;(int32_t) finalIndex < (int32_t) m_PreTaps - (int32_t) maxIndex;finalIndex++)
   3.360 +        {
   3.361 +          final.push_back(make_pair(m_resolution*finalIndex, 0.0));
   3.362 +        }
   3.363 +      uint32_t tempIndex = maxIndex - (m_PreTaps - finalIndex);
   3.364 +      for(;finalIndex < m_TotalTaps && tempIndex < tempTap.size();finalIndex++, tempIndex++)
   3.365 +        {
   3.366 +          final.push_back(make_pair(m_resolution*finalIndex,
   3.367 +                                 tempTap[tempIndex].second));
   3.368 +        }
   3.369 +      for(;finalIndex < m_TotalTaps;finalIndex++)
   3.370 +        {
   3.371 +          final.push_back(make_pair(m_resolution*finalIndex, 0.0));
   3.372 +        }
   3.373 +      out.push_back(final);
   3.374      }
   3.375 -
   3.376 +  return out;
   3.377  }
   3.378 -
   3.379  void
   3.380  DatMaker::ReadArrivals(list<BellhopResp> &results)
   3.381  {
   3.382 @@ -535,7 +674,7 @@
   3.383            {
   3.384  
   3.385              inf >> numArrivals;
   3.386 -			NS_LOG_DEBUG("Looking for " << numArrivals << " arrivals at range " << *rangeIt << " and frequency " << freq << " (NOTE:  These are converted in integers in next line)");
   3.387 +            NS_LOG_DEBUG("Looking for " << numArrivals << " arrivals at range " << *rangeIt << " and frequency " << freq << " (NOTE:  These are converted in integers in next line)");
   3.388  
   3.389              BhIndex newIndex((uint32_t) freq, *srcDepIt, *recDepIt, (uint32_t) *rangeIt);
   3.390  
   3.391 @@ -558,7 +697,7 @@
   3.392  
   3.393              for(;tapsIt != tapPairs.end();tapsIt++)
   3.394                {
   3.395 -                newPdp.AddTap(Tap((*tapsIt).first, (*tapsIt).second/pathLoss));
   3.396 +                newPdp.AddTap(Tap((*tapsIt).first, (*tapsIt).second));
   3.397                }
   3.398              pathLoss = 20*log10(pathLoss);
   3.399              BellhopResp newResp((uint32_t) freq, *srcDepIt, *recDepIt, (uint32_t) *rangeIt, newPdp, pathLoss);
   3.400 @@ -571,5 +710,162 @@
   3.401  }
   3.402  
   3.403  
   3.404 +#ifdef USE_SQL_DAT
   3.405 +void
   3.406 +DatMaker::WriteResults(list<BellhopResp> &results, sqlite3 *sqldb)
   3.407 +{
   3.408 +  ostringstream sqlStr;
   3.409 +  //expecting results to be in the order read into in ReadArrivals.
   3.410 +  //i.e. sd1 rd1 rr1 .. rrn rd2 rr1 .. rrn .. rdn rr1 rrn sd2 rd1 rr1 ... sdn
   3.411  
   3.412 +  char *err;
   3.413 +  int32_t rc;
   3.414 +  static uint32_t tapid = 0;
   3.415  
   3.416 +  //list<BellhopResp>::iterator it = results.front();
   3.417 +  for (uint32_t sd = 0; sd < m_NumSrcDepth; sd++)
   3.418 +    {
   3.419 +      NS_ASSERT(!results.empty());
   3.420 +      uint32_t srcDepth = results.front().GetSrcDepthM();
   3.421 +      uint32_t freq = results.front().GetFreqHz();
   3.422 +
   3.423 +      for (uint32_t rd = 0; rd < m_NumRecDepth; rd++)
   3.424 +        {
   3.425 +          uint32_t recDepth = m_MinRecDepthM + rd*m_RecDepthStepM;
   3.426 +
   3.427 +          for (uint32_t rr = 0; rr < m_NumRanges; rr++)
   3.428 +            {
   3.429 +              tapid++;
   3.430 +              BellhopResp resp = results.front();
   3.431 +              results.pop_front();
   3.432 +              NS_LOG_DEBUG("Results source depth: " << resp.GetSrcDepthM() << " Expected depth: " << m_MinSrcDepthM + sd*m_SrcDepthStepM);
   3.433 +              NS_LOG_DEBUG("Results receiver depth: " << resp.GetRecDepthM() << " Expected depth: " << m_MinRecDepthM + rd*m_RecDepthStepM);
   3.434 +              NS_LOG_DEBUG("Results range: " << resp.GetRangeM() << " Expected range: " << 1000*(m_MinRangeKm + rr*m_RangeStepKm));
   3.435 +              NS_ASSERT(resp.GetSrcDepthM() == m_MinSrcDepthM + sd* m_SrcDepthStepM);
   3.436 +              NS_ASSERT(resp.GetRecDepthM() == m_MinRecDepthM + rd*m_RecDepthStepM);
   3.437 +              uint32_t range = 1000*(m_MinRangeKm + rr*m_RangeStepKm);
   3.438 +
   3.439 +              UanPdp pdp = resp.GetPdp();
   3.440 +              sqlStr.str("");
   3.441 +              sqlStr << "INSERT INTO indextable (srcdepth, recdepth, range, freq) VALUES ("
   3.442 +                     << srcDepth << ", " << recDepth << ", " << range << ", " << freq << ");";
   3.443 +              rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err);
   3.444 +              if(rc)
   3.445 +                {
   3.446 +                  sqlite3_close(sqldb);
   3.447 +                  NS_FATAL_ERROR("SQL error inserting index into indextable.\nError: " << err << "\nQuery: " << sqlStr.str());
   3.448 +                }
   3.449 +
   3.450 +              UanPdp::Iterator tapit = pdp.GetBegin();
   3.451 +              for(;tapit != pdp.GetEnd();tapit++)
   3.452 +                {
   3.453 +                  sqlStr.str("");
   3.454 +                  sqlStr << "INSERT INTO taptable (tapid, delay, realamp, imagamp) VALUES " <<
   3.455 +                            "(" << tapid << ", " << tapit->GetDelay().GetSeconds() <<
   3.456 +                            ", " << real(tapit->GetAmp()) << ", " << imag(tapit->GetAmp()) << ");";
   3.457 +                  std::cout << sqlStr.str() << endl;
   3.458 +                  rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err);
   3.459 +                  if(rc)
   3.460 +                    {
   3.461 +                      sqlite3_close(sqldb);
   3.462 +                      NS_FATAL_ERROR("SQL error inserting tap into taptable in.\nError: " << err << "\nQuery: " << sqlStr.str());
   3.463 +                    }
   3.464 +
   3.465 +                }
   3.466 +            }
   3.467 +        }
   3.468 +
   3.469 +    }
   3.470 +}
   3.471 +
   3.472 +sqlite3 *
   3.473 +DatMaker::CreateDb(string dbfile)
   3.474 +{
   3.475 +
   3.476 +  char *err;
   3.477 +  int32_t rc;
   3.478 +  ostringstream sqlStr;
   3.479 +
   3.480 +  std::ifstream tstfile(dbfile.c_str());
   3.481 +  if(tstfile.is_open())
   3.482 +    {
   3.483 +      NS_FATAL_ERROR("DB file " << dbfile << " already exists!");
   3.484 +    }
   3.485 +  sqlite3 *sqldb;
   3.486 +  rc = sqlite3_open(dbfile.c_str(), &sqldb);
   3.487 +  if(rc)
   3.488 +    {
   3.489 +      sqlite3_close(sqldb);
   3.490 +      NS_FATAL_ERROR("Could not open DB: " << dbfile);
   3.491 +    }
   3.492 +  sqlStr << "CREATE TABLE indextable (tapid INTEGER PRIMARY KEY AUTOINCREMENT, "
   3.493 +         << "srcdepth INTEGER, recdepth INTEGER, range INTEGER, freq INTEGER);";
   3.494 +
   3.495 +  rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err);
   3.496 +  if(rc)
   3.497 +    {
   3.498 +      sqlite3_close(sqldb);
   3.499 +      NS_FATAL_ERROR("SQL Error creating table, indextable, in " << dbfile << ".\nError: " << err);
   3.500 +    }
   3.501 +
   3.502 +  sqlStr.str("");
   3.503 +  sqlStr << "CREATE TABLE taptable (tapid INTEGER, "
   3.504 +         << "delay DECIMAL, realamp DECIMAL, imagamp DECIMAL);";
   3.505 +
   3.506 +  rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err);
   3.507 +  if(rc)
   3.508 +    {
   3.509 +      sqlite3_close(sqldb);
   3.510 +      NS_FATAL_ERROR("SQL Error creating table, taptable, in " << dbfile << ".\nError: " << err);
   3.511 +    }
   3.512 +  return sqldb;
   3.513 +}
   3.514 +#else
   3.515 +{
   3.516 +  void
   3.517 +  DatMaker::WriteResults(list<BellhopResp> &results)
   3.518 +  {
   3.519 +    ostringstream datFileName;
   3.520 +    //expecting results to be in the order read into in ReadArrivals.
   3.521 +    //i.e. sd1 rd1 rr1 .. rrn rd2 rr1 .. rrn .. rdn rr1 rrn sd2 rd1 rr1 ... sdn
   3.522 +
   3.523 +    ofstream outf;
   3.524 +    //list<BellhopResp>::iterator it = results.front();
   3.525 +    for (uint32_t sd = 0; sd < m_NumSrcDepth; sd++)
   3.526 +      {
   3.527 +        NS_ASSERT(!results.empty());
   3.528 +        datFileName.str("");
   3.529 +        datFileName << fixed << setprecision(1) << m_DataPath << results.front().GetSrcDepthM() << "-" << results.front().GetFreqHz() << ".dat";
   3.530 +        outf.open(datFileName.str().c_str());
   3.531 +        if(!outf.good())
   3.532 +          {
   3.533 +            NS_FATAL_ERROR("Fatal error opening file: " << datFileName.str());
   3.534 +          }
   3.535 +        for (uint32_t rd = 0; rd < m_NumRecDepth; rd++)
   3.536 +          {
   3.537 +            outf << "rd " << m_MinRecDepthM + rd*m_RecDepthStepM << endl;
   3.538 +            for (uint32_t rr = 0; rr < m_NumRanges; rr++)
   3.539 +              {
   3.540 +                BellhopResp resp = results.front();
   3.541 +                results.pop_front();
   3.542 +                NS_LOG_DEBUG("Results source depth: " << resp.GetSrcDepthM() << " Expected depth: " << m_MinSrcDepthM + sd*m_SrcDepthStepM);
   3.543 +                NS_LOG_DEBUG("Results receiver depth: " << resp.GetRecDepthM() << " Expected depth: " << m_MinRecDepthM + rd*m_RecDepthStepM);
   3.544 +                NS_LOG_DEBUG("Results range: " << resp.GetRangeM() << " Expected range: " << 1000*(m_MinRangeKm + rr*m_RangeStepKm));
   3.545 +                NS_ASSERT(resp.GetSrcDepthM() == m_MinSrcDepthM + sd* m_SrcDepthStepM);
   3.546 +                //NS_ASSERT(resp.GetRangeM() == uint32_t (1000*(m_MinRangeKm+ rr * m_RangeStepKm)));
   3.547 +                NS_ASSERT(resp.GetRecDepthM() == m_MinRecDepthM + rd*m_RecDepthStepM);
   3.548 +
   3.549 +                outf << "rr " << resp.GetRangeM() << endl;
   3.550 +                UanPdp pdp = resp.GetPdp();
   3.551 +                outf << resp.GetPathLossDb() << endl;
   3.552 +                outf << pdp;
   3.553 +                outf << endl;
   3.554 +
   3.555 +              }
   3.556 +          }
   3.557 +        outf.close();
   3.558 +      }
   3.559 +
   3.560 +  }
   3.561 +}
   3.562 +#endif //USQ_SQL_DAT