5 #include <boost/numeric/conversion/cast.hpp>
6 #include <boost/filesystem/operations.hpp>
7 #include <boost/cstdint.hpp>
14 SeismicDataComp::SeismicDataComp() :
15 stla(0), stlo(0), stel(0), stdp(0), evla(0), evlo(0), evel(0), evdp(0),
16 mag(0), dist(0), az(0), baz(0), gcarc(0), b(0), dataformat(sac)
22 stla(0), stlo(0), stel(0), stdp(0), evla(0), evlo(0), evel(0), evdp(0),
23 mag(0), dist(0), az(0), baz(0), gcarc(0), b(0), dataformat(sac)
34 this->stla = source.stla;
35 this->stlo = source.stlo;
36 this->stel = source.stel;
37 this->stdp = source.stdp;
38 this->evla = source.evla;
39 this->evlo = source.evlo;
40 this->evel = source.evel;
41 this->evdp = source.evdp;
42 this->mag = source.mag;
43 this->dist = source.dist;
45 this->baz = source.baz;
46 this->gcarc = source.gcarc;
55 this->dataformat = source.dataformat;
60 void SeismicDataComp::ReadAscii(
const std::string &filename)
62 ifstream infile(filename.c_str());
63 double time, amp, oldtime;
65 infile >> oldtime >> amp;
71 infile >> time >> amp;
72 assert(time - oldtime > 0.0);
74 SetDt(time - oldtime);
75 if (!
GetData().empty() && infile.good())
79 infile >> time >> amp;
85 void SeismicDataComp::ReadSac(
const std::string &filename)
87 boost::int32_t ihd[40];
90 ifstream infile(filename.c_str());
91 infile.read((
char *) (&fhd), 70 * 4);
94 throw FatalException(
"Cannot read from file: " + filename);
99 infile.read((
char *) (&ihd), 40 * 4);
100 infile.read((
char *) (&chd), 24 * 8);
115 boost::int32_t npts = ihd[9];
116 assert(fhd[0] > 0.0);
120 float *temp =
new float[npts];
121 infile.read((
char *) (temp), npts *
sizeof(
float));
124 copy(temp, temp + npts, back_inserter(
GetData()));
137 if (!boost::filesystem::exists(filename))
148 cerr <<
"Unknown dataformat ! Cannot read " << filename << endl;
152 cerr <<
"Unknown dataformat ! Cannot read " << filename << endl;
163 boost::int32_t ihd[40];
170 outfile.open(filename.c_str());
174 temp =
new float[npts];
175 std::string dummyline =
"-12345 -12345 -12345 ";
177 for (i = 0; i < 40; i++)
179 for (i = 0; i < 70; i++)
181 for (i = 0; i < 8; i++)
182 std::copy(dummyline.begin(), dummyline.end(), chd[i]);
207 outfile.write((
char *) (&fhd), 70 * 4);
208 outfile.write((
char *) (&ihd), 40 * 4);
209 outfile.write((
char *) (&chd), 24 * 8);
210 outfile.write((
char *) (temp), npts *
sizeof(
float));
218 ofstream outfile(filename.c_str());
220 for (
unsigned int i = 0; i <
GetData().size(); ++i)
222 outfile << b + i *
GetDt() <<
" " <<
GetData().at(i) << endl;
238 cerr <<
"Unknown dataformat ! Cannot write " <<
GetName() << endl;
242 cerr <<
"Unknown dataformat ! Cannot write !" <<
GetName() << endl;
int ReadData(const std::string &filename, tseismicdataformat format=sac)
Read in data from a file, as we cannot determine the type from the ending we have to provide it...
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
void CopyHeader(const SeismicDataComp &source)
Copy the information in the header from another object.
std::string GetName() const
Return name of the component.
void SetDt(const double dt)
Set delta t in s.
virtual ~SeismicDataComp()
void SetName(const std::string &n)
Modify name of the component.
int WriteBack() const
Write the data in the format it was read in and with the same filename.
double GetDt() const
Return dt in s.
virtual SeismicDataComp & operator=(const SeismicDataComp &source)
int WriteAsSac(const std::string &filename) const
Write the data in sac binary format.
int WriteAsAscii(const std::string &filename) const
Write the data in plain ascii format.
The basic exception class for all errors that arise in gplib.
TimeSeriesComponent & operator=(const TimeSeriesComponent &source)