int proc_sta( Dbptr db, int nrec, Event *event )
{
    double ts, te, et ;
    double stime, endtime, calib;
    double samprate;
    int pos, retcode;
    int nsamp, arid, npts;
    float *buf=0;
    char sta[8], key[64];



    for( db.record = 0 ; db.record < nrec; db.record++)   {

       if( (dbgetv( db, 0,
	  "time", &stime,
          "endtime", &endtime,
          "calib", &calib,
          "samprate", &samprate,
          "arid", &arid,
          "nsamp", &nsamp,
          "sta", sta,
           0 )) == dbINVALID )
           elog_die( 0, "dbgetv faild for record #%d\n", db.record );

/*
fprintf( stderr, "%s: %lf %lf %d %lf \n", sta, stime, endtime, nsamp,event->otime );
*/
       if( strncmp( event->sta, sta, strlen(sta) )) continue;

       if( stime >= event->etime )   {
           elog_complain( 0, "can't get data for %s %lf - %lf.\n", 
                         sta, event->otime, event->etime);
           return 0;
       }
       if( endtime <= event->stime ) continue;
       if( arid != event->arid ) continue;
      
       trgetwf(
           db, 0, &buf, 0, event->otime, event->etime, &ts, &te, &npts, 0, 0) ; 
       if( npts <= 0 )  {
           if(buf != 0) free(buf);
	   buf = 0;
           continue;
       }

/*
fprintf( stderr, "%lf %lf - %lf %lf %d \n", 
         event->otime, event->etime, ts, te, npts );

*/
       if( buf == 0 )  { 
           elog_complain( 0, "can't get data for %s from %lf to %lf\n", 
                        event->sta, event->otime, event->etime ); 
           continue;
       }
       if( te <= event->otime ) continue;
       if( ts > event->stime ) {
           elog_complain( 0, "trace start time is more than SWA time. %lf > %lf\n",
                     ts, event->stime );
           if(buf) {
	    	free(buf);
		buf = 0;
	   }
	   return 0;
       }  else {
           if( ( pos = (int) ( event->stime - ts ) * samprate) >= npts ) {
               elog_complain( 0,  
               "no data for %s from %lf to %lf.\n", sta, event->stime, event->etime ); 
           	if(buf) {
	 	   free(buf);
		   buf = 0;
		}
                continue;
           }

           if( !(retcode = domag( buf, ts, samprate, npts, pos, calib, event)) ) 
               elog_complain( 0,  
               "can't calculate MS for %s. ( no data or too many FULL scale values).\n", sta); 
           if( buf != 0 && *buf != 0 ) free( buf );
           return retcode;
       }
  }

}
示例#2
0
Trace *
read_trace (Dbptr db, double tstart, double tend)

{
    char fname[1024];
    char dtype[8];
    char segtype[8];
    long foff, nsamp;
    void *data;
    Trace *trace;
    double time, samprate;
    double calib, calper;
    double ts, te;
    int size;
    int i, ret;

    if (dbextfile (db, "wfdisc", fname) < 1) {
        fprintf (stderr, "read_trace: Unable to find input file '%s'\n",
                 fname);
        return (NULL);
    }
    dbgetv (db, 0, "time", &time, "samprate", &samprate,
            "nsamp", &nsamp, "datatype", dtype,
            "segtype", segtype, "foff", &foff,
            "calib", &calib, "calper", &calper, NULL);

    data = NULL;
    ret = trgetwf (db, NULL, (Trsample **) &data, 0, tstart, tend,
                   &ts, &te, &nsamp, 0, 0);
    size = 4;

    switch (ret) {
    default:
    case 0:
        if (ret < 0) data = NULL;
        if (ts == 0.0 && te == 0.0) data = NULL;
        break;

    case -9: /* no data */
        clear_register (0);
        data = NULL;
        break;

    case -1:
    case -2:
    case -3:
    case -5:
    case -6:
    case -7:
    case -8:
        complain (0,
                  "read_trace: trgetwf() error.\n");
        return (NULL);
    }

    if (data == (void *) -1) {
        data = NULL;
    }

    if (data == (void *) -2) {
        clear_register (0);
        data = NULL;
    }
    trace = newtrace();
    if (trace == NULL) {
        fprintf (stderr, "read_trace: newtrace() error.\n");
        my_free (data);
        return (NULL);
    }
    trace->scv = NULL;
    trace->tstart = ts;
    trace->dt = 1.0/samprate;
    trace->nsamps = nsamp;
    trace->calib = calib;
    trace->calper = calper;
    strcpy (trace->rawdata_format, "t4");
    strcpy (trace->rawdata_type, segtype);
    strcpy (trace->input_units, "");
    strcpy (trace->output_units, "");
    trace->data = NULL;
    trace->data_free = NULL;
    trace->data_malloc = 0;
    trace->raw_data = data;
    trace->rawdata_free = data;
    if (data) trace->rawdata_malloc = nsamp*size;
    else trace->rawdata_malloc = 0;
    trace->prev = NULL;
    trace->next = NULL;
    for (i=0; i<trace->nsamps; i++) if (((float *)data)[i] < 0.9e30) break;
    if (i >= trace->nsamps) {
        trace->nsamps = 0;
        trace->raw_data = NULL;
        trace->rawdata_free = NULL;
        free (data);
    } else {
        if (data) {
            trace = (Trace *) SCV_trace_fixgaps (trace, "segment");
        }
    }
    return (trace);
}