115 #include <datrwxx/readany.h> 116 #include <libtime++.h> 117 #include <aff/series.h> 129 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
132 const char *field_names[] = {
"date",
"time",
"tdate",
"station",
"channel",
"auxid",
"samps",
"dt",
133 "calib",
"calper",
"instype",
"trace"};
134 int number_of_fields;
135 int date_field, time_field, tdate_field, station_field, channel_field;
136 int auxid_field, samps_field, dt_field, calib_field, calper_field;
137 int instype_field, trace_field;
138 std::vector<int> ntraces;
139 ntraces.reserve(100);
142 number_of_fields =
sizeof(field_names) /
sizeof(*field_names);
146 mexErrMsgTxt(
"ERROR: missing input filename!");
149 mexErrMsgTxt(
"ERROR: too many input arguments!");
151 else if ((nrhs == 1) && !mxIsChar(prhs[0]))
152 mexErrMsgTxt(
"ERROR: input value must be a string!");
154 else if (nrhs == 2 && (!mxIsChar(prhs[0]) || !mxIsChar(prhs[1])))
155 mexErrMsgTxt(
"Error: input values must be strings!");
157 else if (nrhs == 2 && ((mxGetM(prhs[0]) != 1) || mxGetM(prhs[1]) != 1))
158 mexErrMsgTxt(
"Error: input isn't valid!");
159 else if (nrhs == 3 && (!mxIsChar(prhs[0]) || !mxIsChar(prhs[1]) || !mxIsChar(prhs[2])))
160 mexErrMsgTxt(
"Error: input values must be strings!");
161 else if (nrhs == 3) {
162 for(
int i = 0; i < 3; ++i) {
163 if(mxGetM(prhs[i]) != 1)
164 mexErrMsgTxt(
"Error: input isn't valid!");
169 std::string filetype;
173 infile = std::string(mxArrayToString(prhs[0]));
181 if (nrhs == 1 && infile.rfind(
".") != string::npos) {
182 filetype = infile.substr(infile.rfind(
".") + 1);
185 mexErrMsgTxt(
"Error: not a valid filename!");
190 infile = std::string(mxArrayToString(prhs[0]));
191 filetype = std::string(mxArrayToString(prhs[1]));
195 std::string dtype(
"double");
199 infile = std::string(mxArrayToString(prhs[0]));
200 filetype = std::string(mxArrayToString(prhs[1]));
202 std::string arg2(mxArrayToString(prhs[2]));
204 if (arg2 ==
"int" || arg2 ==
"double") {
208 mexErrMsgTxt(
"Error: third argument is not valid!");
213 transform(filetype.begin(), filetype.end(), filetype.begin(), tolower);
218 std::ifstream ifs(infile.c_str(), datrw::ianystream::openmode(filetype));
219 datrw::ianystream is(ifs, filetype);
232 ntraces.push_back(wid2.nsamples);
241 mwSize dims[2] = {1, trace};
243 plhs[0] = mxCreateStructArray(2, dims, number_of_fields, field_names);
245 date_field = mxGetFieldNumber(plhs[0],
"date");
246 time_field = mxGetFieldNumber(plhs[0],
"time");
247 tdate_field = mxGetFieldNumber(plhs[0],
"tdate");
248 station_field = mxGetFieldNumber(plhs[0],
"station");
249 auxid_field = mxGetFieldNumber(plhs[0],
"auxid");
250 channel_field = mxGetFieldNumber(plhs[0],
"channel");
251 samps_field = mxGetFieldNumber(plhs[0],
"samps");
252 dt_field = mxGetFieldNumber(plhs[0],
"dt");
253 calib_field = mxGetFieldNumber(plhs[0],
"calib");
254 calper_field = mxGetFieldNumber(plhs[0],
"calper");
255 instype_field = mxGetFieldNumber(plhs[0],
"instype");
256 trace_field = mxGetFieldNumber(plhs[0],
"trace");
259 std::ifstream ifs(infile.c_str(), datrw::ianystream::openmode(filetype));
260 datrw::ianystream is(ifs, filetype);
266 mxArray *field_value;
269 if (dtype ==
"int") {
270 datrw::Tiseries iseries;
276 field_value = mxCreateNumericMatrix(ntraces[i], 1, mxINT32_CLASS,
279 int *p = iseries.pointer();
280 int *ptr =
static_cast<int*
>(mxGetData(field_value));
281 for(
int j = 0; j < iseries.size(); ++j) {
286 mxSetFieldByNumber(plhs[0], i, trace_field, field_value);
290 datrw::Tdseries dseries;
295 field_value = mxCreateNumericMatrix(ntraces[i], 1, mxDOUBLE_CLASS,
298 double *ptr = mxGetPr(field_value);
299 double *tmp = dseries.pointer();
300 for(
int j = 0; j < dseries.size(); ++j) {
304 mxSetFieldByNumber(plhs[0], i, trace_field, field_value);
313 std::ostringstream str;
314 str << wid2.date.year() <<
"/" 315 << wid2.date.month() <<
"/" 316 << wid2.date.day() << std::ends;
317 std::string datestring;
318 datestring.append(str.str());
319 mxSetFieldByNumber(plhs[0], i, date_field,
320 mxCreateString(datestring.c_str()));
325 std::ostringstream str;
326 std::string timestring;
327 str << wid2.date.hour() <<
":" 328 << wid2.date.minute() <<
":" 329 << wid2.date.second() <<
"." 330 << wid2.date.milsec() << std::ends;
331 timestring.append(str.str());
332 mxSetFieldByNumber(plhs[0], i, time_field,
333 mxCreateString(timestring.c_str()));
338 time [0] = wid2.date.year();
339 time [1] = wid2.date.month();
340 time [2] = wid2.date.day();
341 time [3] = wid2.date.hour();
342 time [4] = wid2.date.minute();
343 time [5] = wid2.date.second() + wid2.date.milsec() * 0.001;
345 field_value = mxCreateDoubleMatrix(1,
346 sizeof(time) /
sizeof(time[0]), mxREAL);
347 memmove(mxGetPr(field_value), &time,
348 sizeof(time) /
sizeof(time[0]) *
sizeof(
double));
349 mxSetFieldByNumber(plhs[0], i, tdate_field, field_value);
352 mxSetFieldByNumber(plhs[0], i, station_field,
353 mxCreateString(wid2.station.c_str()));
356 mxSetFieldByNumber(plhs[0], i, channel_field,
357 mxCreateString(wid2.channel.c_str()));
360 mxSetFieldByNumber(plhs[0], i, auxid_field,
361 mxCreateString(wid2.auxid.c_str()));
364 field_value = mxCreateDoubleMatrix(1, 1, mxREAL);
365 *mxGetPr(field_value) = wid2.nsamples;
366 mxSetFieldByNumber(plhs[0], i, samps_field, field_value);
369 field_value = mxCreateDoubleMatrix(1, 1, mxREAL);
370 *mxGetPr(field_value) = wid2.dt;
371 mxSetFieldByNumber(plhs[0], i, dt_field, field_value);
374 field_value = mxCreateDoubleMatrix(1, 1, mxREAL);
375 *mxGetPr(field_value) = wid2.calib;
376 mxSetFieldByNumber(plhs[0], i, calib_field, field_value);
379 field_value = mxCreateDoubleMatrix(1, 1, mxREAL);
380 *mxGetPr(field_value) = wid2.calper;
381 mxSetFieldByNumber(plhs[0], i, calper_field, field_value);
384 mxSetFieldByNumber(plhs[0], i, instype_field,
385 mxCreateString(wid2.instype.c_str()));
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mexFunction of any2matlab to read seismic data to MATLAB