Beispiel #1
0
int
main (int argc, char **argv)
{
  MSRecord *msr = 0;
  MSTraceGroup *mstg = 0;
  MSTrace *mst;
  int retcode;

  int totalrecs  = 0;
  int totalsamps = 0;
  int packedsamples;
  int packedrecords;
  int lastrecord;
  int iseqnum = 1;
  
#ifndef WIN32
  /* Signal handling, use POSIX calls with standardized semantics */
  struct sigaction sa;
  
  sa.sa_flags = SA_RESTART;
  sigemptyset (&sa.sa_mask);
  
  sa.sa_handler = term_handler;
  sigaction (SIGINT, &sa, NULL);
  sigaction (SIGQUIT, &sa, NULL);
  sigaction (SIGTERM, &sa, NULL);
  
  sa.sa_handler = SIG_IGN;
  sigaction (SIGHUP, &sa, NULL);
  sigaction (SIGPIPE, &sa, NULL);
#endif
  
  /* Process given parameters (command line and parameter file) */
  if (parameter_proc (argc, argv) < 0)
    return -1;
  
  /* Setup input encoding format if specified */
  if ( encodingstr )
    {
      int inputencoding = strtoul (encodingstr, NULL, 10);
      
      if ( inputencoding == 0 && errno == EINVAL )
	{
	  ms_log (2, "Error parsing input encoding format: %s\n", encodingstr);
	  return -1;
	}
      
      MS_UNPACKENCODINGFORMAT (inputencoding);
    }
  
  /* Init MSTraceGroup */
  mstg = mst_initgroup (mstg);
  
  /* Loop over the input file */
  while ( (retcode = ms_readmsr (&msr, inputfile, reclen, NULL, &lastrecord,
				 1, 1, verbose)) == MS_NOERROR )
    {
      totalrecs++;
      totalsamps += msr->samplecnt;
      
      msr_print (msr, ppackets);
      
      if ( packreclen >= 0 )
	msr->reclen = packreclen;
      else
	packreclen = msr->reclen;
      
      if ( packencoding >= 0 )
	msr->encoding = packencoding;
      else
	packencoding = msr->encoding;
      
      if ( byteorder >= 0 )
	msr->byteorder = byteorder;
      else
	byteorder = msr->byteorder;
      
      /* After unpacking the record, the start time in msr->starttime
	 is a potentially corrected start time, if correction has been
	 applied make sure the correction bit flag is set as it will
	 be used as a packing template. */
      if ( msr->fsdh->time_correct && ! (msr->fsdh->act_flags & 0x02) )
	{
	  ms_log (1, "Setting time correction applied flag for %s_%s_%s_%s\n",
		  msr->network, msr->station, msr->location, msr->channel);
	  msr->fsdh->act_flags |= 0x02;
	}
      
      /* If no samples in the record just pack the header */
      if ( outfile && msr->numsamples == 0 )
	{
	  msr_pack_header (msr, 1, verbose);
	  record_handler (msr->record, msr->reclen, NULL);
	}
      
      /* Pack each record individually */
      else if ( outfile && ! tracepack )
	{
	  msr->sequence_number = iseqnum;
	  
	  packedrecords = msr_pack (msr, &record_handler, NULL, &packedsamples, 1, verbose);
	  
	  if ( packedrecords == -1 )
	    ms_log (2, "Cannot pack records\n"); 
	  else
	    ms_log (1, "Packed %d records\n", packedrecords); 
	  
	  iseqnum = msr->sequence_number;
	}
      
      /* Pack records from a MSTraceGroup */
      else if ( outfile && tracepack )
	{
	  mst = mst_addmsrtogroup (mstg, msr, 0, -1.0, -1.0);
	  
	  if ( ! mst )
	    {
	      ms_log (2, "Error adding MSRecord to MStrace!\n");
	      break;
	    }
	  	  
	  /* Reset sequence number and free previous template */
	  if ( mst->prvtptr )
	    {
	      MSRecord *tmsr = (MSRecord *) mst->prvtptr;
	      
	      /* Retain sequence number from previous template */
	      msr->sequence_number = tmsr->sequence_number;
	      
	      msr_free (&tmsr);
	    }
	  else
	    {
	      msr->sequence_number = 1;
	    }
	  
	  /* Copy MSRecord and store as template */
	  mst->prvtptr = msr_duplicate (msr, 0);
	  
	  if ( ! mst->prvtptr )
	    {
	      ms_log (2, "Error duplicating MSRecord for template!\n");
	      break;
	    }
	  
	  /* Pack traces based on selected method */
	  packedrecords = 0;
	  if ( tracepack == 1 )
	    {
	      mst = mstg->traces;
	      while ( mst )
		{
		  packedrecords += mst_pack (mst, &record_handler, NULL, packreclen,
					     packencoding, byteorder, &packedsamples,
					     lastrecord, verbose, (MSRecord *)mst->prvtptr);
		  mst = mst->next;
		}
	      
	      ms_log (1, "Packed %d records\n", packedrecords);
	    }
	  if ( tracepack == 2 && lastrecord )
	    {
	      mst = mstg->traces;
	      while ( mst )
		{
		  packedrecords += mst_pack (mst, &record_handler, NULL, packreclen,
					     packencoding, byteorder, &packedsamples,
					     lastrecord, verbose, (MSRecord *)mst->prvtptr);
		  mst = mst->next;
		}
	      
	      ms_log (1, "Packed %d records\n", packedrecords);
	    }
	}
    }
  
  if ( retcode != MS_ENDOFFILE )
    ms_log (2, "Error reading %s: %s\n", inputfile, ms_errorstr(retcode));
  
  /* Make sure everything is cleaned up */
  ms_readmsr (&msr, NULL, 0, NULL, NULL, 0, 0, 0);
  mst_freegroup (&mstg);
  
  if ( outfile )
    fclose (outfile);
  
  getchar();

  return 0;
}  /* End of main() */
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);
}
Beispiel #3
0
int
main (int argc, char **argv)
{
  MSTraceGroup *mstg = 0;
  MSTrace *mst;
  MSRecord *msr = 0;
  
  struct listnode *flp;
  
  int retcode;
  int totalsamps = 0;

  
  /* Process given parameters (command line and parameter file) */
  if (parameter_proc (argc, argv) < 0)
    return -1;
  
  /* Init MSTraceGroup */
  mstg = mst_initgroup (mstg);
  
  /* Read input miniSEED files into MSTraceGroup */
  flp = filelist;

      if ( verbose )
        fprintf (stderr, "Reading %s\n", flp->data);
      
      while ( (retcode = ms_readmsr(&msr, flp->data, reclen, NULL, NULL,
				    1, 1, verbose-1)) == MS_NOERROR )
	{
	  if ( verbose > 1)
	    msr_print (msr, verbose - 2);
	  
	  mst_addmsrtogroup (mstg, msr, 1, timetol, sampratetol);
	  

	  totalsamps += msr->samplecnt;
	}
      
      if ( retcode != MS_ENDOFFILE )
	fprintf (stderr, "Error reading %s: %s\n", flp->data, ms_errorstr(retcode));
      
      /* Make sure everything is cleaned up */
      ms_readmsr (&msr, NULL, 0, NULL, NULL, 0, 0, 0);
      
      /* If processing each file individually, write ASCII and reset */
      if ( indifile )
	{
	  mst = mstg->traces;
	  while ( mst )
	    {
	      writeascii (mst);
	      mst = mst->next;
	    }
	   mstg = mst_initgroup (mstg);
	}
      
  
  /* Make sure everything is cleaned up */
  mst_freegroup (&mstg);
  
  if ( ofp )
    fclose (ofp);
  
  return 0;
}  /* End of main() */
Beispiel #4
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;
}