AnisoSurfaceWaveModel.cpp

Go to the documentation of this file.
00001 #include "AnisoSurfaceWaveModel.h"
00002 #include <fstream>
00003 #include <cassert>
00004 #include <algorithm>
00005 #include <numeric>
00006 #include <boost/bind.hpp>
00007 #include <string>
00008 #include <boost/filesystem.hpp>
00009 #include "FatalException.h"
00010 #include <iostream>
00011 
00012 namespace gplib
00013   {
00014     AnisoSurfaceWaveModel::AnisoSurfaceWaveModel()
00015       {
00016       }
00017 
00018     AnisoSurfaceWaveModel::~AnisoSurfaceWaveModel()
00019       {
00020       }
00021 
00022     void AnisoSurfaceWaveModel::ReadModel(const std::string &filename)
00023       {
00024         std::ifstream infile(filename.c_str());
00025         size_t nlayers = 0;
00026         const size_t titlelength = 512;
00027         char title[titlelength];
00028         //the first line of the model is the title that we don't care about
00029         infile.getline(title, titlelength);
00030         //the number of layers in the model does not include the bottom half-space
00031         infile >> nlayers;
00032         //so for us it is one layer more
00033         nlayers += 1;
00034         //there can be comments at the end of each line
00035         //so read the rest of the line, if nothing is there, it only reads the newline
00036         infile.getline(title, titlelength);
00037         //reserve memory for all the parameters
00038         thicknesses.assign(nlayers, 0.0);
00039         vp.assign(nlayers, 0.0);
00040         vs.assign(nlayers, 0.0);
00041         B.assign(nlayers, 0.0);
00042         C.assign(nlayers, 0.0);
00043         E.assign(nlayers, 0.0);
00044         theta.assign(nlayers, 0.0);
00045         phi.assign(nlayers, 0.0);
00046         densities.assign(nlayers, 0.0);
00047         for (size_t i = 0; i < nlayers; ++i)
00048           {
00049             //first line for each layer are the anisotropy angles
00050             infile >> theta.at(i) >> phi.at(i);
00051             //plus a comment
00052             infile.getline(title, titlelength);
00053             //then the other layer parameters
00054             infile >> thicknesses.at(i) >> vp.at(i) >> B.at(i) >> C.at(i)
00055                 >> vs.at(i) >> E.at(i) >> densities.at(i);
00056             //transform from m to km for thickness and velocities and kg/m^3 to g/cm^3 for density
00057             thicknesses.at(i) /= 1000.0;
00058             vp.at(i) /= 1000.0;
00059             vs.at(i) /= 1000.0;
00060             densities.at(i) /= 1000.0;
00061             //swallow comment
00062             infile.getline(title, titlelength);
00063           }
00064         //the file contains depths we need thicknesses
00065         std::adjacent_difference(thicknesses.begin(), thicknesses.end(),
00066             thicknesses.begin());
00067       }
00068 
00069     void AnisoSurfaceWaveModel::WriteModel(const std::string &filename) const
00070       {
00071         const size_t nlayers = thicknesses.size();
00072         //check that all the layers have all parameters
00073         assert(nlayers> 0);
00074         assert(nlayers == vp.size());
00075         assert(nlayers == vs.size());
00076         assert(nlayers == B.size());
00077         assert(nlayers == C.size());
00078         assert(nlayers == E.size());
00079         assert(nlayers == theta.size());
00080         assert(nlayers == phi.size());
00081         assert(nlayers == densities.size());
00082 
00083         std::ofstream outfile(filename.c_str());
00084         //we have to write some title
00085         outfile << "Surface Wave model generated by gplib" << std::endl;
00086         //the code needs the number of layers without the half-space, we count the half-space
00087         outfile << nlayers - 1 << std::endl;
00088         //we have to transform thicknesses to depths
00089         double currdepth = 0.0;
00090         for (size_t i = 0; i < nlayers; ++i)
00091           {
00092             //write the anisotropy angles, the new line is important because the original file format can have comments
00093             outfile << theta.at(i) << " " << phi.at(i) << "\n";
00094             //calculate the depth to the bottom in m
00095             currdepth += thicknesses.at(i) * 1000.0;
00096             //write out the other parameters, again the newline matters
00097             //seismic anisotropic coefficient have to be positive (absolute value)
00098             outfile << currdepth << " " << vp.at(i) * 1000.0 << " " << fabs(
00099                 B.at(i)) << " " << C.at(i) << " " << vs.at(i) * 1000.0 << " "
00100                 << fabs(E.at(i)) << " " << densities.at(i) * 1000.0 << "\n";
00101           }
00102         if (!outfile.good())
00103           throw FatalException("Problem writing to file: " + filename);
00104       }
00105 
00106     void AnisoSurfaceWaveModel::WriteRunFile(const std::string &filename) const
00107       {
00108         std::ofstream runfile(filename.c_str());
00109         boost::filesystem::path Path(filename);
00110         std::string dirname(filename + "_dir");
00111         runfile << "#!/bin/bash" << std::endl;
00112         runfile << "mkdir " << dirname << std::endl;
00113         runfile << "mv " << filename << ".* " << dirname << std::endl;
00114         runfile << "cd " << dirname << std::endl;
00115         runfile << "cp " << Path.leaf() << ".mod animodel" << std::endl;
00116         runfile << "aniprop 2>&1> /dev/null" << std::endl;
00117         runfile << "cp out_cvel_R ../" << filename + ".cvel" << std::endl;
00118         if (!runfile.good())
00119           throw FatalException("Problem writing to file: " + filename);
00120       }
00121 
00122     void AnisoSurfaceWaveModel::WritePlot(const std::string &filename) const
00123       {
00124         std::ofstream outfile(filename.c_str());
00125         double cumthick = 0;
00126         const size_t nlayers = thicknesses.size();
00127         for (unsigned int i = 0; i < nlayers; ++i)
00128           {
00129             //we write S-wave velocity and one anisotropy parameter and strike
00130             //once for the top
00131             outfile << cumthick << "  " << vs.at(i) << "  " << B.at(i) << "  "
00132                 << phi.at(i) << std::endl;
00133             cumthick += thicknesses.at(i);
00134             //and once for the bottom
00135             outfile << cumthick << " " << vs.at(i) << "  " << B.at(i) << "  "
00136                 << phi.at(i) << std::endl;
00137           }
00138         //make sure we get a thick layer for the bottom half-space for plotting
00139         outfile << cumthick * 2.0 << " " << vs.back() << "  " << B.back()
00140             << "  " << phi.back() << std::endl;
00141       }
00142   }

Generated on Tue Nov 3 13:24:13 2009 for GPLIB++ by  doxygen 1.5.8