コード例 #1
0
main() {
  outfile = fopen("int32_Steim2_littleEndian.mseed", "w");
  int psamples;
  int precords;
  MSTrace *mst;
  char srcname[50];
  psamples = 0;

  int *mychar;
  mychar = (int *) malloc(50 * sizeof(int));
  // Write integers from 1 to 50.
  int i = 1;
  for(i; i<51; i++){
    mychar[i-1] = i;
	  }

  mst = mst_init (NULL);

  /* Populate MSTrace values */
  strcpy (mst->network, "XX");
  strcpy (mst->station, "TEST");
  strcpy (mst->channel, "BHE");
  mst->starttime = ms_seedtimestr2hptime ("2004,350,00:00:00.000000");
  mst->samprate = 1.0;
  /* The datasamples pointer and numsamples counter will be adjusted by
     the packing routine, the datasamples array must be dynamic memory
     allocated by the malloc() family of routines. */
  mst->datasamples = mychar;
  mst->numsamples = 50; 
  mst->samplecnt = 50;
  mst->sampletype = 'i';

  mst_srcname (mst, srcname, 0); 

  /* Pack 512 byte, big-endian records, ´write Chars */
  precords = mst_pack (mst, &record_handler, srcname, 256, DE_STEIM2,
                                     0, &psamples, 1, verbose, NULL);

  ms_log (0, "Packed %d samples into %d records\n", psamples, precords);
  fclose(outfile);
  mst_free (&mst);
}
コード例 #2
0
main() {
  outfile = fopen("fullASCII_bigEndian.mseed", "w");
  int psamples;
  int precords;
  MSTrace *mst;
  char srcname[50];
  psamples = 0;

  // Allocate char array. One more allocated char is necessary for
  // the zero terminating last char.
  char *mychar = (char *) malloc(96 * sizeof(char));
  // Copy contents to char. In this case copy all ASCII chars.
  strcpy(mychar, " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");

  mst = mst_init (NULL);

  /* Populate MSTrace values */
  strcpy (mst->network, "XX");
  strcpy (mst->station, "TEST");
  strcpy (mst->channel, "BHE");
  mst->starttime = ms_seedtimestr2hptime ("2004,350,00:00:00.000000");
  mst->samprate = 1.0;
  /* The datasamples pointer and numsamples counter will be adjusted by
     the packing routine, the datasamples array must be dynamic memory
     allocated by the malloc() family of routines. */
  mst->datasamples = mychar; /* pointer to 8-bit ascii data samples */  
  mst->numsamples = 95; 
  mst->samplecnt = 95;
  mst->sampletype = 'a';      /* declare type to be 8 bit ASCII */

  mst_srcname (mst, srcname, 0); 

  /* Pack 256 byte, big-endian records, write chars */
  precords = mst_pack (mst, &record_handler, srcname, 256, DE_ASCII,
                                     1, &psamples, 1, verbose, NULL);

  ms_log (0, "Packed %d samples into %d records\n", psamples, precords);
  fclose(outfile);
  mst_free (&mst);
}
コード例 #3
0
ファイル: mseed_ext.c プロジェクト: carinaj/pyrocko
static PyObject*
mseed_store_traces (PyObject *dummy, PyObject *args)
{
    char          *filename;
    MSTrace       *mst = NULL;
    PyObject      *array = NULL;
    PyObject      *in_traces = NULL;
    PyObject      *in_trace = NULL;
    PyArrayObject *contiguous_array = NULL;
    int           i;
    char          *network, *station, *location, *channel;
    char          mstype;
    int           msdetype;
    int           psamples, precords;
    int           numpytype;
    int           length;
    FILE          *outfile;

    if (!PyArg_ParseTuple(args, "Os", &in_traces, &filename)) {
        PyErr_SetString(MSeedError, "usage store_traces(traces, filename)" );
        return NULL;
    }
    if (!PySequence_Check( in_traces )) {
        PyErr_SetString(MSeedError, "Traces is not of sequence type." );
        return NULL;
    }

    outfile = fopen(filename, "w" );
    if (outfile == NULL) {
        PyErr_SetString(MSeedError, "Error opening file.");
        return NULL;
    }

    for (i=0; i<PySequence_Length(in_traces); i++) {
        
        in_trace = PySequence_GetItem(in_traces, i);
        if (!PyTuple_Check(in_trace)) {
            PyErr_SetString(MSeedError, "Trace record must be a tuple of (network, station, location, channel, starttime, endtime, samprate, data)." );
            Py_DECREF(in_trace);
            return NULL;
        }
        mst = mst_init (NULL);
        
        if (!PyArg_ParseTuple(in_trace, "ssssLLdO",
                                    &network,
                                    &station,
                                    &location,
                                    &channel,
                                    &(mst->starttime),
                                    &(mst->endtime),
                                    &(mst->samprate),
                                    &array )) {
            PyErr_SetString(MSeedError, "Trace record must be a tuple of (network, station, location, channel, starttime, endtime, samprate, data)." );
            mst_free( &mst );  
            Py_DECREF(in_trace);
            return NULL;
        }

        strncpy( mst->network, network, 10);
        strncpy( mst->station, station, 10);
        strncpy( mst->location, location, 10);
        strncpy( mst->channel, channel, 10);
        mst->network[10] = '\0';
        mst->station[10] = '\0';
        mst->location[10] ='\0';
        mst->channel[10] = '\0';
        
        if (!PyArray_Check(array)) {
            PyErr_SetString(MSeedError, "Data must be given as NumPy array." );
            mst_free( &mst );  
            Py_DECREF(in_trace);
            return NULL;
        }
        numpytype = PyArray_TYPE(array);
        switch (numpytype) {
                case NPY_INT32:
                    assert( ms_samplesize('i') == 4 );
                    mstype = 'i';
                    msdetype = DE_STEIM1;
                    break;
                case NPY_INT8:
                    assert( ms_samplesize('a') == 1 );
                    mstype = 'a';
                    msdetype = DE_ASCII;
                    break;
                case NPY_FLOAT32:
                    assert( ms_samplesize('f') == 4 );
                    mstype = 'f';
                    msdetype = DE_FLOAT32;
                    break;
                case NPY_FLOAT64:
                    assert( ms_samplesize('d') == 8 );
                    mstype = 'd';
                    msdetype = DE_FLOAT64;
                    break;
                default:
                    PyErr_SetString(MSeedError, "Data must be of type float64, float32, int32 or int8.");
                    mst_free( &mst );  
                    Py_DECREF(in_trace);
                    return NULL;
            }
        mst->sampletype = mstype;

        contiguous_array = PyArray_GETCONTIGUOUS((PyArrayObject*)array);

        length = PyArray_SIZE(contiguous_array);
        mst->numsamples = length;
        mst->samplecnt = length;

        mst->datasamples = calloc(length,ms_samplesize(mstype));
        memcpy(mst->datasamples, PyArray_DATA(contiguous_array), length*ms_samplesize(mstype));
        Py_DECREF(contiguous_array);

        precords = mst_pack (mst, &record_handler, outfile, 4096, msdetype,
                                     1, &psamples, 1, 0, NULL);
        mst_free( &mst );
        Py_DECREF(in_trace);
    }
    fclose( outfile );

    Py_INCREF(Py_None);
    return Py_None;
}
コード例 #4
0
ファイル: mst_ip.c プロジェクト: hi2arun/mstunnel
int mst_insert_ip_tuple (unsigned sip, unsigned dip, mst_ip_dir_t ip_dir, unsigned sid, int mnp_id)
{
    unsigned bucket_id = 0;
    mst_nw_ip_flow_t *mst_nw_ip_bkt;
    mst_ip_tuple_t *ip_tuple;
    mst_ip_tuple_t *temp_tuple;


    ip_tuple = (mst_ip_tuple_t *)mst_malloc(sizeof(mst_ip_tuple_t), __func__);
    assert(ip_tuple);

    ip_tuple->next = NULL;
    ip_tuple->prev = NULL;
    if (E_NW_IN == ip_dir) {
        ip_tuple->sip = dip;
        ip_tuple->dip = sip;
    }
    else {
        ip_tuple->sip = sip;
        ip_tuple->dip = dip;
    }
    ip_tuple->mnp = mnp_id;
    ip_tuple->sid = sid;
    ip_tuple->hits = 1;
    
    bucket_id = jhash_2words(ip_tuple->sip, ip_tuple->dip, 0);
    
    bucket_id %= D_IP_FLOW_TABLE_SIZE;
    mst_nw_ip_bkt = &mst_ip_ct[bucket_id];
    
    fprintf(stderr, "Adding SIP: "D_IPV4_STR_FMT", DIP: "D_IPV4_STR_FMT"\n", M_NIPQUAD(&ip_tuple->sip), M_NIPQUAD(&ip_tuple->dip));

    pthread_mutex_lock(&mst_nw_ip_bkt->b_lock);
    // Check if this bucket has all LRU slots filled
    if (mst_nw_ip_bkt->slots >= D_IP_SLOTS_PER_FLOW) {
        // Add this new node to head and chuck tail off
        ip_tuple->next = mst_nw_ip_bkt->head;
        mst_nw_ip_bkt->head->prev = ip_tuple;
        mst_nw_ip_bkt->head = ip_tuple;

        temp_tuple = mst_nw_ip_bkt->tail->prev; // New tail
        temp_tuple->next = NULL;
        mst_free(mst_nw_ip_bkt->tail, __func__); // free old tail
        mst_nw_ip_bkt->tail = temp_tuple; // Set new tail
    }
    else {
        // Just update head and do not disturb tail (NO circular LRU here)
        if (mst_nw_ip_bkt->head) {
            ip_tuple->next = mst_nw_ip_bkt->head;
            mst_nw_ip_bkt->head->prev = ip_tuple;
            mst_nw_ip_bkt->head = ip_tuple;
        }
        else {
            // This is the first node
            mst_nw_ip_bkt->head = ip_tuple;
            mst_nw_ip_bkt->tail = ip_tuple;
        }
        mst_nw_ip_bkt->slots++;
    }

    pthread_mutex_unlock(&mst_nw_ip_bkt->b_lock);

    return 0;
}