void
mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  MSTraceGroup *mstg = NULL;
  MSTrace *mst = NULL;
  const char **my_fnames = NULL;
  char *filename;
  int buflen;
  int reclen;
  double timetol, sampratetol;
  flag dataquality, skipnodata, dataflag, verbose;
  int i, j, nfields;
  mxArray *tmp_val;
  double *tmp_val_ptr;
  int32_t *data;

  /* Sanity check input and output */
  if ( nrhs != 8 )
    {
      mexPrintf ("mexMsReadTracesNative - Read Mini-SEED data into Matlab\n\n");
      mexPrintf ("Usage: mexMsReadTracesNative (filename, reclen, timetol, sampratetol, dataquality, skipnotdata, dataflag, verbosity)\n");
      mexPrintf ("  filename    - Name of file to read Mini-SEED data from\n");
      mexPrintf ("  reclen      - Mini-SEED data record length, -1 for autodetection\n");
      mexPrintf ("  timetol     - Time tolerance, use -1.0 for 1/2 sample period\n");
      mexPrintf ("  sampratetol - Sample rate tolerance, use -1.0 for default tolerance\n");
      mexPrintf ("  dataquality - Include data quality in determination of unique time series, use 0 or 1\n");
      mexPrintf ("  skipnotdata - Skip blocks in input file that are not Mini-SEED data\n");
      mexPrintf ("  dataflag    - Flag to control return of data samples or not, 0 or 1\n");
      mexPrintf ("  verbosity   - Level of diagnostic messages, use 0 - 3\n\n");
      mexErrMsgTxt ("8 input arguments required.");
    }
  else if ( nlhs > 1 )
    {
      mexErrMsgTxt ("Too many output arguments.");
    }

  /* Redirect libmseed logging messages to Matlab functions */
  ms_loginit ((void *)&mexPrintf, NULL, (void *)&mexWarnMsgTxt, NULL);

  /* Get the length of the input string */
  buflen = (mxGetM (prhs[0]) * mxGetN (prhs[0])) + 1;

  /* Allocate memory for input string */
  filename = mxCalloc (buflen, sizeof (char));

  /* Assign the input arguments to variables */
  if ( mxGetString (prhs[0], filename, buflen) )
    mexErrMsgTxt ("Not enough space. Filename string is truncated.");
  reclen = (int) mxGetScalar(prhs[1]);
  timetol = mxGetScalar(prhs[2]);
  sampratetol = mxGetScalar(prhs[3]);
  dataquality = (flag) mxGetScalar(prhs[4]);
  skipnodata = (flag) mxGetScalar(prhs[5]);
  dataflag = (flag) mxGetScalar(prhs[6]);
  verbose = (flag) mxGetScalar(prhs[7]);

  /* Read the file */
  if ( ms_readtraces (&mstg, filename, reclen, timetol, sampratetol, dataquality,
		      skipnodata, dataflag, verbose) != MS_NOERROR )
    mexErrMsgTxt ("Error reading files");

  /* Print some information to the Matlab command prompt */
  mst_printtracelist (mstg, 0, verbose, 1);

  /* Create the Matlab output structure */
  mst = mstg->traces;
  for (i=0; i < mstg->numtraces; i++)
    {
      if (i==0)
   	{
	  nfields = 13;
	  my_fnames = mxCalloc (nfields, sizeof (*my_fnames));
	  my_fnames[0] = "network";
	  my_fnames[1] = "station";
	  my_fnames[2] = "location";
	  my_fnames[3] = "channel";
	  my_fnames[4] = "dataquality";
	  my_fnames[5] = "type";
	  my_fnames[6] = "startTime";
	  my_fnames[7] = "endTime";
	  my_fnames[8] = "sampleRate";
	  my_fnames[9] = "sampleCount";
	  my_fnames[10] = "numberOfSamples";
	  my_fnames[11] = "sampleType";
	  my_fnames[12] = "data";
	  plhs[0] = mxCreateStructMatrix(mstg->numtraces, 1, nfields, my_fnames);
	  mxFree(my_fnames);
   	}

      /* Copy the data of the mst structure to the matlab output structure. */
      data = (int32_t*) mst->datasamples;
      tmp_val = mxCreateDoubleMatrix(mst->numsamples, 1, mxREAL);
      tmp_val_ptr = mxGetPr(tmp_val);
      for (j = 0; j < mst->numsamples; j++)
      {
# if ENDIANNESS
        tmp_val_ptr[j] = (double) data[j];
# else
        tmp_val_ptr[j] = (double) (int32_t)(__builtin_bswap32 (data[j]));
# endif
        //tmp_val_ptr[j] = 100.0;
      }

      mxSetFieldByNumber(plhs[0], i, 0, mxCreateString(mst->network));
      mxSetFieldByNumber(plhs[0], i, 1, mxCreateString(mst->station));
      mxSetFieldByNumber(plhs[0], i, 2, mxCreateString(mst->location));
      mxSetFieldByNumber(plhs[0], i, 3, mxCreateString(mst->channel));
      mxSetFieldByNumber(plhs[0], i, 4, mxCreateDoubleScalar((int)mst->dataquality));
      mxSetFieldByNumber(plhs[0], i, 5, mxCreateDoubleScalar((int)mst->type));
      mxSetFieldByNumber(plhs[0], i, 6, mxCreateDoubleScalar(mst->starttime));
      mxSetFieldByNumber(plhs[0], i, 7, mxCreateDoubleScalar(mst->endtime));
      mxSetFieldByNumber(plhs[0], i, 8, mxCreateDoubleScalar(mst->samprate));
      mxSetFieldByNumber(plhs[0], i, 9, mxCreateDoubleScalar(mst->samplecnt));
      mxSetFieldByNumber(plhs[0], i, 10, mxCreateDoubleScalar(mst->numsamples));
      mxSetFieldByNumber(plhs[0], i, 11, mxCreateDoubleScalar((int)mst->sampletype));
      mxSetFieldByNumber(plhs[0], i, 12, tmp_val);

      mst = mst->next;
    }

  mst_freegroup (&mstg);
}
Example #2
0
static PyObject*
mseed_get_traces (PyObject *dummy, PyObject *args)
{
    char          *filename;
    MSTraceGroup  *mstg = NULL;
    MSTrace       *mst = NULL;
    int           retcode;
    npy_intp      array_dims[1] = {0};
    PyObject      *array = NULL;
    PyObject      *out_traces = NULL;
    PyObject      *out_trace = NULL;
    int           numpytype;
    char          strbuf[BUFSIZE];
    PyObject      *unpackdata = NULL;

    if (!PyArg_ParseTuple(args, "sO", &filename, &unpackdata)) {
        PyErr_SetString(MSeedError, "usage get_traces(filename, dataflag)" );
        return NULL;
    }

    if (!PyBool_Check(unpackdata)) {
        PyErr_SetString(MSeedError, "Second argument must be a boolean" );
        return NULL;
    }
  
    /* get data from mseed file */
    retcode = ms_readtraces (&mstg, filename, 0, -1.0, -1.0, 0, 1, (unpackdata == Py_True), 0);
    if ( retcode < 0 ) {
        snprintf (strbuf, BUFSIZE, "Cannot read file '%s': %s", filename, ms_errorstr(retcode));
        PyErr_SetString(MSeedError, strbuf);
        return NULL;
    }

    if ( ! mstg ) {
        snprintf (strbuf, BUFSIZE, "Error reading file");
        PyErr_SetString(MSeedError, strbuf);
        return NULL;
    }

    /* check that there is data in the traces */
    if (unpackdata == Py_True) {
        mst = mstg->traces;
        while (mst) {
            if (mst->datasamples == NULL) {
                snprintf (strbuf, BUFSIZE, "Error reading file - datasamples is NULL");
                PyErr_SetString(MSeedError, strbuf);
                return NULL;
            }
            mst = mst->next;
        }
    }

    out_traces = Py_BuildValue("[]");

    mst = mstg->traces;

    /* convert data to python tuple */

    while (mst) {
        
        if (unpackdata == Py_True) {
            array_dims[0] = mst->numsamples;
            switch (mst->sampletype) {
                case 'i':
                    assert( ms_samplesize('i') == 4 );
                    numpytype = NPY_INT32;
                    break;
                case 'a':
                    assert( ms_samplesize('a') == 1 );
                    numpytype = NPY_INT8;
                    break;
                case 'f':
                    assert( ms_samplesize('f') == 4 );
                    numpytype = NPY_FLOAT32;
                    break;
                case 'd':
                    assert( ms_samplesize('d') == 8 );
                    numpytype = NPY_FLOAT64;
                    break;
                default:
                    snprintf (strbuf, BUFSIZE, "Unknown sampletype %c\n", mst->sampletype);
                    PyErr_SetString(MSeedError, strbuf);
                    Py_XDECREF(out_traces);
                    return NULL;
            }
            array = PyArray_SimpleNew(1, array_dims, numpytype);
            memcpy( PyArray_DATA(array), mst->datasamples, mst->numsamples*ms_samplesize(mst->sampletype) );
        } else {
            Py_INCREF(Py_None);
            array = Py_None;
        }

        out_trace = Py_BuildValue( "(c,s,s,s,s,L,L,d,N)",
                                    mst->dataquality,
                                    mst->network,
                                    mst->station,
                                    mst->location,
                                    mst->channel,
                                    mst->starttime,
                                    mst->endtime,
                                    mst->samprate,
                                    array );

        
        PyList_Append(out_traces, out_trace);
        Py_DECREF(out_trace);
        mst = mst->next;
    }

    mst_freegroup (&mstg);

    return out_traces;
}