Fortran SFF API to data I/O streams in C++
wid2container.cc
Go to the documentation of this file.
1 
34 #define TF_WID2CONTAINER_CC_VERSION \
35  "TF_WID2CONTAINER_CC V1.0 "
36 
37 #include <cstring>
38 #include <fapidxx/helper.h>
39 #include <fapidxx/wid2container.h>
40 #include <fapidxx/error.h>
41 
42 namespace fapidxx {
43 
54  void WID2container::setwid2(const std::string& line)
55  {
56  std::istringstream is(line);
57  this->wid2.read(is);
58  this->wasascii=true;
59  } // void WID2container::setwid2(const std::string& line)
60 
61  /*----------------------------------------------------------------------*/
62 
75  void WID2container::setwid2(char *fstring, ftnlen slen)
76  {
77  /*
78  * copy content immediately such that we do not rely on the ID string
79  * being placed at byte offset 0 in WID2container::WID2struct
80  */
81  int w2size=sizeof(WID2container::WID2struct);
82  FAPIDXX_assert(w2size<=slen,
83  "WID2container::setwid2: string is too short");
85  std::memcpy(ws, fstring, w2size);
86  // check for special ID, expect ASCII encoding if not ID does not match
87  if (std::strncmp(ws->ID, WID2container::WID2struct::encodeID,
89  {
90  this->setwid2(stringfromfstring(fstring, slen));
91  this->wasascii=true;
92  }
93  else
94  {
95  /*
96  * The byte sequence fstring points to is the content of a
97  * WID2container::WID2struct struct. Create a WID2container::WID2struct
98  * in memory, copy the memory contents fstring points to into
99  * this struct and read the WID2 data from it.
100  */
101  this->wid2=ws->get();
102  this->wasascii=false;
103  }
104  delete ws;
105  } // void WID2container::setwid2(char *fstring, ftnlen slen)
106 
107  /*----------------------------------------------------------------------*/
108 
120  void WID2container::encode(char *fstring, ftnlen slen) const
121  {
122  if (this->wasascii)
123  {
124  this->encodeascii(fstring, slen);
125  }
126  else
127  {
128  this->encodebinary(fstring, slen);
129  }
130  } // void WID2container::encode(char *fstring, ftnlen slen)
131 
132  /*----------------------------------------------------------------------*/
133 
144  void WID2container::encodebinary(char *fstring, ftnlen slen) const
145  {
147  ws.set(this->wid2);
148  int w2size=sizeof(WID2container::WID2struct);
149  FAPIDXX_assert(w2size<=slen,
150  "WID2container::encode: struct is too large");
151  std::memcpy(fstring, &ws, w2size);
152  } // void WID2container::encodebinary(char *fstring, ftnlen slen)
153 
154  /*----------------------------------------------------------------------*/
155 
166  void WID2container::encodeascii(char *fstring, ftnlen slen) const
167  {
168  fapidxx::fillfstring(this->wid2.line(), fstring, slen);
169  } // void WID2container::encodeascii(char *fstring, ftnlen slen)
170 
171  /*======================================================================*/
172  // WID2contsiner::WID2struct
173 
176  const char* WID2container::WID2struct::encodeID="WIDY";
177 
178  /*----------------------------------------------------------------------*/
179 
180  void WID2container::WID2struct::set(const ::sff::WID2& wid2)
181  {
182  strncpy(this->ID, WID2container::WID2struct::encodeID, idlen);
183  strncpy(this->station, wid2.station.c_str(), slen);
184  strncpy(this->channel, wid2.channel.c_str(), clen);
185  strncpy(this->auxid, wid2.auxid.c_str(), alen);
186  strncpy(this->instype, wid2.instype.c_str(), ilen);
187  this->station[slen]='\0';
188  this->channel[clen]='\0';
189  this->auxid[alen]='\0';
190  this->instype[ilen]='\0';
191  this->hour=wid2.date.hour();
192  this->minute=wid2.date.minute();
193  this->second=wid2.date.second();
194  this->milsec=wid2.date.milsec();
195  this->micsec=wid2.date.micsec();
196  this->year=wid2.date.year();
197  this->month=wid2.date.month();
198  this->day=wid2.date.day();
199  this->calib=wid2.calib;
200  this->calper=wid2.calper;
201  this->dt=wid2.dt;
202  this->nsamples=wid2.nsamples;
203  this->vang=wid2.vang;
204  this->hang=wid2.hang;
205  } // void WID2container::WID2struct::set(const ::sff::WID2& wid2)
206 
207  /*----------------------------------------------------------------------*/
208 
209  ::sff::WID2 WID2container::WID2struct::get() const
210  {
211  ::sff::WID2 retval;
212  libtime::TAbsoluteTime date(this->year, this->month, this->day,
213  this->hour, this->minute, this->second,
214  this->milsec, this->micsec);
215  retval.date=date;
216  retval.station=std::string(this->station);
217  retval.channel=std::string(this->channel);
218  retval.auxid=std::string(this->auxid);
219  retval.instype=std::string(this->instype);
220  retval.dt=this->dt;
221  retval.nsamples=this->nsamples;
222  retval.hang=this->hang;
223  retval.vang=this->vang;
224  retval.calib=this->calib;
225  retval.calper=this->calper;
226  return(retval);
227  } // ::sff::WID2 WID2container::WID2struct::get() const
228 
229 } // namespace fapidxx
230 
231 /* ----- END OF wid2container.cc ----- */
void encodeascii(char *fstring, ftnlen slen) const
encode data into a standard SFF character string
static const unsigned short ilen
char channel[clen+1]
FDSN channel code.
Definition: error.cc:44
char instype[ilen+1]
instrument type
double dt
sampling interval (sec)
double calib
calibration factor
libtime::timeint micsec
time of first sample (microsecond)
void setwid2(const std::string &line)
fill data fields from a conventional WID2 line
sff::WID2 wid2
WID2 data.
libtime::timeint month
date of first sample (month value)
libtime::timeint day
date of first sample (day value)
double vang
veritcal orientation
container for WID2 data (prototypes)
static const char * encodeID
ID indicating the WID2 is encoded in binary form in the WID2 line.
libtime::timeint year
date of first sample (year value)
void fillfstring(const std::string &s, char *fstring, ftnlen slen)
fill a Fortran string with a C++ string
Definition: helper.cc:60
exceptions and error handling macros (prototypes)
void encodebinary(char *fstring, ftnlen slen) const
encode data into a binary character string
::sff::WID2 get() const
return values to WID2
static const unsigned short slen
bool wasascii
true, if original data was ASCII encoded
char auxid[alen+1]
Auxiliary identification code.
int nsamples
number of samples
static const unsigned short clen
static const unsigned short alen
#define FAPIDXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:157
libtime::timeint hour
time of first sample (hour)
libtime::timeint minute
time of first sample (minute)
long int ftnlen
Fortran string size type.
Definition: fapidsff.h:70
double calper
calibration reference period
double hang
horizontal orientation
std::string stringfromfstring(char *fstring, ftnlen slen)
create a C++ string from a Fortran string
Definition: helper.cc:51
char station[slen+1]
Station code.
void encode(char *fstring, ftnlen slen) const
encode data into a character string depending on wasascii flag
This struct ist used by fapidxx::WID2container to store WID2 line data.
void set(const ::sff::WID2 &wid2)
set values from WID2
libtime::timeint milsec
time of first sample (millisecond)
libtime::timeint second
time of first sample (second)
some helper functions (prototypes)
static const unsigned short idlen