00001 #include "SeismicDataComp.h"
00002 #include <algorithm>
00003 #include <fstream>
00004 #include <cassert>
00005 #include <boost/numeric/conversion/cast.hpp>
00006 #include <boost/filesystem/operations.hpp>
00007 #include <boost/cstdint.hpp>
00008 #include "FatalException.h"
00009
00010 using namespace std;
00011
00012 namespace gplib
00013 {
00014 SeismicDataComp::SeismicDataComp() :
00015 stla(0), stlo(0), stel(0), stdp(0), evla(0), evlo(0), evel(0), evdp(0),
00016 mag(0), dist(0), az(0), baz(0), gcarc(0), b(0), dataformat(sac)
00017 {
00018 }
00019
00020 SeismicDataComp::SeismicDataComp(const std::string &filename,
00021 tseismicdataformat format) :
00022 stla(0), stlo(0), stel(0), stdp(0), evla(0), evlo(0), evel(0), evdp(0),
00023 mag(0), dist(0), az(0), baz(0), gcarc(0), b(0), dataformat(sac)
00024 {
00025 ReadData(filename, format);
00026 }
00027
00028 SeismicDataComp::~SeismicDataComp()
00029 {
00030 }
00031
00032 void SeismicDataComp::CopyHeader(const SeismicDataComp& source)
00033 {
00034 this->stla = source.stla;
00035 this->stlo = source.stlo;
00036 this->stel = source.stel;
00037 this->stdp = source.stdp;
00038 this->evla = source.evla;
00039 this->evlo = source.evlo;
00040 this->evel = source.evel;
00041 this->evdp = source.evdp;
00042 this->mag = source.mag;
00043 this->dist = source.dist;
00044 this->az = source.az;
00045 this->baz = source.baz;
00046 this->gcarc = source.gcarc;
00047 this->b = source.b;
00048 }
00049
00050 SeismicDataComp& SeismicDataComp::operator=(const SeismicDataComp& source)
00051 {
00052 if (this == &source)
00053 return *this;
00054 CopyHeader(source);
00055 this->dataformat = source.dataformat;
00056 TimeSeriesComponent::operator=(source);
00057 return *this;
00058 }
00059
00060 void SeismicDataComp::ReadAscii(const std::string &filename)
00061 {
00062 ifstream infile(filename.c_str());
00063 double time, amp, oldtime;
00064
00065 infile >> oldtime >> amp;
00066 if (infile.good())
00067 GetData().push_back(amp);
00068 else
00069 throw FatalException("Cannot read from file: " + filename);
00070 b = oldtime;
00071 infile >> time >> amp;
00072 assert(time - oldtime > 0.0);
00073 GetData().push_back(amp);
00074 SetDt(time - oldtime);
00075 if (!GetData().empty() && infile.good())
00076 GetData().clear();
00077 while (infile.good())
00078 {
00079 infile >> time >> amp;
00080 if (infile.good())
00081 GetData().push_back(amp);
00082 }
00083 }
00084
00085 void SeismicDataComp::ReadSac(const std::string &filename)
00086 {
00087 boost::int32_t ihd[40];
00088 float fhd[70];
00089 char chd[8][24];
00090 ifstream infile(filename.c_str());
00091 infile.read((char *) (&fhd), 70 * 4);
00092 if (infile.fail())
00093 {
00094 throw FatalException("Cannot read from file: " + filename);
00095 }
00096 else
00097 {
00098
00099 infile.read((char *) (&ihd), 40 * 4);
00100 infile.read((char *) (&chd), 24 * 8);
00101
00102 stla = fhd[31];
00103 stlo = fhd[32];
00104 stel = fhd[33];
00105 stdp = fhd[34];
00106 evla = fhd[35];
00107 evlo = fhd[36];
00108 evel = fhd[37];
00109 evdp = fhd[38];
00110 mag = fhd[39];
00111 dist = fhd[50];
00112 az = fhd[51];
00113 baz = fhd[52];
00114 gcarc = fhd[53];
00115 boost::int32_t npts = ihd[9];
00116 assert(fhd[0] > 0.0);
00117 SetDt(fhd[0]);
00118 b = fhd[5];
00119 assert(npts > 0);
00120 float *temp = new float[npts];
00121 infile.read((char *) (temp), npts * sizeof(float));
00122 if (!GetData().empty())
00123 GetData().clear();
00124 copy(temp, temp + npts, back_inserter(GetData()));
00125
00126
00127
00128
00129 delete[] temp;
00130
00131 }
00132 }
00133
00134 int SeismicDataComp::ReadData(const std::string &filename,
00135 tseismicdataformat format)
00136 {
00137 if (!boost::filesystem::exists(filename))
00138 throw FatalException("File does not exist : " + filename);
00139 switch (format)
00140 {
00141 case sac:
00142 ReadSac(filename);
00143 break;
00144 case ascii:
00145 ReadAscii(filename);
00146 break;
00147 case unknownseis:
00148 cerr << "Unknown dataformat ! Cannot read " << filename << endl;
00149 return -100;
00150 break;
00151 default:
00152 cerr << "Unknown dataformat ! Cannot read " << filename << endl;
00153 return -100;
00154 break;
00155 }
00156 SetName(filename);
00157 return 0;
00158 }
00159
00160 int SeismicDataComp::WriteAsSac(const std::string &filename) const
00161 {
00162 int npts;
00163 boost::int32_t ihd[40];
00164 float fhd[70];
00165 char chd[8][24];
00166 ofstream outfile;
00167 float *temp = NULL;
00168 int i;
00169
00170 outfile.open(filename.c_str());
00171
00172 npts = GetData().size();
00173
00174 temp = new float[npts];
00175 std::string dummyline = "-12345 -12345 -12345 ";
00176 copy(GetData().begin(), GetData().end(), temp);
00177 for (i = 0; i < 40; i++)
00178 ihd[i] = -12345;
00179 for (i = 0; i < 70; i++)
00180 fhd[i] = -12345.00;
00181 for (i = 0; i < 8; i++)
00182 std::copy(dummyline.begin(), dummyline.end(), chd[i]);
00183
00184
00185 ihd[35] = 1;
00186 ihd[15] = 1;
00187 ihd[6] = 6;
00188
00189 fhd[31] = stla;
00190 fhd[32] = stlo;
00191 fhd[33] = stel;
00192 fhd[34] = stdp;
00193 fhd[35] = evla;
00194 fhd[36] = evlo;
00195 fhd[37] = evel;
00196 fhd[38] = evdp;
00197 fhd[39] = mag;
00198 fhd[50] = dist;
00199 fhd[51] = az;
00200 fhd[52] = baz;
00201 fhd[53] = gcarc;
00202
00203 ihd[9] = npts;
00204 fhd[0] = GetDt();
00205 fhd[5] = b;
00206
00207 outfile.write((char *) (&fhd), 70 * 4);
00208 outfile.write((char *) (&ihd), 40 * 4);
00209 outfile.write((char *) (&chd), 24 * 8);
00210 outfile.write((char *) (temp), npts * sizeof(float));
00211 outfile.close();
00212 delete[] temp;
00213 return (0);
00214 }
00215
00216 int SeismicDataComp::WriteAsAscii(const std::string &filename) const
00217 {
00218 ofstream outfile(filename.c_str());
00219
00220 for (unsigned int i = 0; i < GetData().size(); ++i)
00221 {
00222 outfile << b + i * GetDt() << " " << GetData().at(i) << endl;
00223 }
00224 return 0;
00225 }
00226
00227 int SeismicDataComp::WriteBack() const
00228 {
00229 switch (dataformat)
00230 {
00231 case sac:
00232 WriteAsSac(GetName());
00233 break;
00234 case ascii:
00235 WriteAsAscii(GetName());
00236 break;
00237 case unknownseis:
00238 cerr << "Unknown dataformat ! Cannot write " << GetName() << endl;
00239 return -100;
00240 break;
00241 default:
00242 cerr << "Unknown dataformat ! Cannot write !" << GetName() << endl;
00243 return -100;
00244 break;
00245 }
00246 return 0;
00247 }
00248 }