sffxx.cc 7.49 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file sffxx.cc
 * \brief SFF library (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id: sffxx.cc,v 1.5 2003-12-23 15:47:22 tforb Exp $
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * \author Thomas Forbriger
 * \date 21/12/2003
 * 
 * SFF library (implementation)
 * 
 * Copyright (c) 2003 by Thomas Forbriger (BFO Schiltach) 
 * 
 * REVISIONS and CHANGES 
 *  - 21/12/2003   V1.0   Thomas Forbriger
 * 
 * ============================================================================
 */
#define TF_SFFXX_CC_VERSION \
  "TF_SFFXX_CC   V1.0   "
#define TF_SFFXX_CC_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
22
  "$Id: sffxx.cc,v 1.5 2003-12-23 15:47:22 tforb Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
23
24

#include <sffxx.h>
thomas.forbriger's avatar
thomas.forbriger committed
25
#include <gsexx.h>
thomas.forbriger's avatar
thomas.forbriger committed
26
27
28
29

namespace sff {

  //! Fortran library version (to ensure compatibility)
thomas.forbriger's avatar
thomas.forbriger committed
30
  const double STAT::libversion=1.10;
thomas.forbriger's avatar
thomas.forbriger committed
31
32
33
34
35
36
  const char* const STAT::LINEID="STAT";
  const char* const FREE::LINEID="FREE";
  const char* const SRCE::LINEID="SRCE";
  const char* const DAST::LINEID="DAST";
  const char* const INFO::LINEID="INFO";

thomas.forbriger's avatar
thomas.forbriger committed
37
38
  /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
39
40
41
42
43
44
  char coosysID(const Ecoosys& csid)
  {
    char retval;
    if (csid == CS_cartesian) retval='C';
    else if (csid == CS_spherical) retval='S';
    else throw
thomas.forbriger's avatar
thomas.forbriger committed
45
      GSE2::Terror("ERROR (sff::coosysID): library inconsistency!");
thomas.forbriger's avatar
thomas.forbriger committed
46
47
48
49
50
    return(retval);
  } // coosysID

  Ecoosys coosysID(const char& csid)
  {
thomas.forbriger's avatar
thomas.forbriger committed
51
52
53
    Ecoosys retval;
    if (csid=='C') { retval=CS_cartesian; }
    else if (csid=='S') { retval=CS_spherical; }
thomas.forbriger's avatar
thomas.forbriger committed
54
    else throw
thomas.forbriger's avatar
thomas.forbriger committed
55
56
      GSE2::Terror("ERROR (sff::coosysID): unknown coordinate system key!");
    return(retval);
thomas.forbriger's avatar
thomas.forbriger committed
57
58
  } // coosysID

thomas.forbriger's avatar
thomas.forbriger committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*======================================================================*/
// SFF structs
// -----------
//
// STAT
// ----

  STAT::STAT(): hasfree(false), hassrce(false) 
  { setstamp(libtime::now()); }

  void STAT::setstamp(const libtime::TAbsoluteTime& date) const
  {
    char stamp[14];
    int yeardigits, century;
    century=int(date.year()/100);
    yeardigits=date.year()-100*century;
    sprintf(stamp, "%2.2i%2.2li%2.2li.%2.2li%2.2li%2.2li",
            yeardigits, date.month(), date.day(),
            date.hour(), date.minute(), date.second());
    timestamp=std::string(stamp);
  }

  std::string STAT::line() const
  {
    this->setstamp(libtime::now());
    char charline[40];
    std::string code("");
    if (this->hasfree) { code.append("F"); }
    if (this->hassrce) { code.append("S"); }
thomas.forbriger's avatar
thomas.forbriger committed
88
    sprintf(charline, "%-4s  %6.2f %-13s %-10s\n",
thomas.forbriger's avatar
thomas.forbriger committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
            STAT::LINEID, 
            STAT::libversion,
            timestamp.c_str(),
            code.c_str());
    std::string retval(charline);
    return(retval);
  } // STAT::line()

/*----------------------------------------------------------------------*/
// SRCE
// ----

  SRCE::SRCE(): 
    type("NSP"), date(libtime::now()),
    cs(CS_cartesian), cx(0.), cy(0.), cz(0.) { }

  std::string SRCE::line() const
  {
    char charline[95];
    int yeardigits, century;
    century=int(date.year()/100);
    yeardigits=date.year()-100*century;
thomas.forbriger's avatar
thomas.forbriger committed
111
    sprintf(charline, "%-4s %-20s %1c %15.6f%15.6f%15.6f "
thomas.forbriger's avatar
thomas.forbriger committed
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
            "%2.2i%2.2li%2.2li %2.2li%2.2li%2.2li.%3.3li\n",
            SRCE::LINEID, 
            type.c_str(), 
            coosysID(cs), cx, cy, cz,
            yeardigits, date.month(), date.day(),
            date.hour(), date.minute(), date.second(), date.milsec());
    std::string retval(charline);
    return(retval);
  } // SRCE::line()

/*----------------------------------------------------------------------*/
// DAST
// ----

  DAST::DAST(): 
    nchar(-1), ampfac(1.), hasfree(false), 
    hasinfo(false), last(false) { }

  std::string DAST::line() const
  {
    char charline[50];
    std::string code("");
    if (this->hasfree) { code.append("F"); }
    if (this->hasinfo) { code.append("I"); }
    if (!this->last) { code.append("D"); }
thomas.forbriger's avatar
thomas.forbriger committed
137
    sprintf(charline, "%-4s  %10i %16.6E %-10s\n",
thomas.forbriger's avatar
thomas.forbriger committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
            DAST::LINEID, 
            nchar, ampfac, code.c_str());
    std::string retval(charline);
    return(retval);
  } // DAST::line()

/*----------------------------------------------------------------------*/
// FREE
// ----

    FREE::FREE() { lines.clear(); }

    void FREE::write(std::ostream& os) const
    {
thomas.forbriger's avatar
thomas.forbriger committed
152
      os << FREE::LINEID << " " << std::endl;
thomas.forbriger's avatar
thomas.forbriger committed
153
154
155
      for(Tlines::const_iterator I=lines.begin();
          I != lines.end(); I++)
      { os << *I << std::endl; }
thomas.forbriger's avatar
thomas.forbriger committed
156
      os << FREE::LINEID << " " << std::endl;
thomas.forbriger's avatar
thomas.forbriger committed
157
158
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
    } // FREE::write

/*----------------------------------------------------------------------*/
// WID2
// ----

  WID2::WID2():
    date(libtime::now()), station("NSP"), channel("NSP"), auxid("NSP"),
    nsamples(-1), dt(-1.), calib(-1.), calper(-1.), instype("NSP"),
    hang(-1.),vang(-1.) { }

  std::string WID2::line() const
  {
    GSE2::waveform::TWID2 wid2line;
    wid2line.Fyear=date.year();
    wid2line.Fmonth=date.month();
    wid2line.Fday=date.day();
    wid2line.Fhour=date.hour();
    wid2line.Fminute=date.minute();
    wid2line.Fseconds=double(date.second())+
      1.e-3*double(date.milsec()+1.e-3*double(date.micsec()));
    wid2line.Fstation=station;
    wid2line.Fchannel=channel;
    wid2line.Fauxid=auxid;
    wid2line.Fsamps=nsamples;
    wid2line.Fsamprate=1./dt;
    wid2line.Fcalib=calib;
    wid2line.Fcalper=calper;
    wid2line.Finstype=instype;
    wid2line.Fhang=hang;
    wid2line.Fvang=vang;
    return(wid2line.line());
  } // WID2::line()

/*----------------------------------------------------------------------*/
// INFO
// ----

  INFO::INFO():
    cs(CS_cartesian), cx(0.), cy(0.), cz(0.), nstacks(0) { }

  std::string INFO::line() const
  {
    char charline[60];
thomas.forbriger's avatar
thomas.forbriger committed
201
    sprintf(charline, "%-4s %1c %15.6f%15.6f%15.6f %4i\n",
thomas.forbriger's avatar
thomas.forbriger committed
202
203
204
205
206
207
            INFO::LINEID, 
            coosysID(cs), cx, cy, cz, nstacks);
    std::string retval(charline);
    return(retval);
  } // INFO::line()

thomas.forbriger's avatar
thomas.forbriger committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*----------------------------------------------------------------------*/
// FileHeader
// ----------

  void FileHeader::write(std::ostream& os) const
  {
    os << Mstat.line();
    if (Mstat.hasfree) { Mfree.write(os); }
    if (Mstat.hassrce) { os << Msrce.line(); }
  }

/*----------------------------------------------------------------------*/
// TraceHeader
// -----------

thomas.forbriger's avatar
thomas.forbriger committed
223
  void TraceHeader::writeheader(std::ostream& os) const
thomas.forbriger's avatar
thomas.forbriger committed
224
225
226
  {
    os << Mdast.line();
    os << Mwid2.line();
thomas.forbriger's avatar
thomas.forbriger committed
227
228
229
230
  }

  void TraceHeader::writetrailer(std::ostream& os) const
  {
thomas.forbriger's avatar
thomas.forbriger committed
231
232
233
234
235
236
237
238
239
240
241
242
    if (Mdast.hasfree) { Mfree.write(os); }
    if (Mdast.hasinfo) { os << Minfo.line(); }
  }

/*----------------------------------------------------------------------*/
// WaveformNormalizer
// ------------------

  const int WaveformNormalizer::limit=0x800000;

  WaveformNormalizer::WaveformNormalizer(const Enormmode& nm, 
                                         const double& maxval):
thomas.forbriger's avatar
thomas.forbriger committed
243
    Mmaxval(maxval), Mnorm(nm)
thomas.forbriger's avatar
thomas.forbriger committed
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
  {
    if (Mnorm == NM_one) 
    { 
      Mampfac=1.; 
      Mscale=false;
      if (Mmaxval > double(WaveformNormalizer::limit)) throw
        GSE2::Terror("ERROR (sff::WaveformNormalizer::scan): "
                     "dynamic range to large for non-normalizing mode");
    }
    else if (Mnorm == NM_ifneeded)
    { 
      Mampfac=1.;
      Mscale=false;
      if (Mmaxval > double(WaveformNormalizer::limit))
      {
        Mampfac=Mmaxval/double(WaveformNormalizer::limit); 
        Mscale=true;
      }
    }
    else if (Mnorm == NM_maxdyn)
    { 
      Mampfac=Mmaxval/double(WaveformNormalizer::limit); 
      Mscale=true;
    }
    else throw
      GSE2::Terror("ERROR (sff::WaveformNormalizer::scan): "
                   "library inconsistency!");
  }

thomas.forbriger's avatar
thomas.forbriger committed
273
274
275
} // namespace sff

/* ----- END OF sffxx.cc ----- */