Example #1
1
int
main (int argc, char **argv)
{

    int             c,
                    errflg = 0;

    char           *in,
                   *out;
    int             orbin,
                    orbout;
    double          maxpkts = VERY_LARGE_NUMBER ;
    int             quit;
    char           *pktmatch = strdup(".*/pf/evtinfo"),
                   *reject = 0;
    int             nmatch;
    int             specified_after = 0;
    double          after = 0.0,
                    until = VERY_LARGE_NUMBER ;
    double          start_time,
                    end_time,
                    delta_t ;
    double          totpkts = 0,
                    totbytes = 0;
    Flags           flags;
    static int      last_pktid = -1;
    static double   last_pkttime = 0.0;
    char           *statefile = 0;
    double          last_burial = 0.0;
    double          decent_interval = 300.0;
    int             mode = PKT_NOSAMPLES;
    int             rcode;
    char            srcname[ORBSRCNAME_SIZE];
    double          pkttime = 0.0 ;
    int             pktid;
    int             nbytes;
    char           *packet = 0;
    int             packetsz = 0;
    Packet         *unstuffed = 0;
	Pf				*pf;
	Tbl				*tbl;
	Arr				*arr;
	char			*arrkey;
	char			*pffilename;

	Tbl				*channels;
	char			*net;
	char			*sta;
	char			*chan;
	char			netstachan[ORBSRCNAME_SIZE];
	Srcname			parts;
	double			maxtime,mintime,starttime,triggertime,duration,min_g,max_g,snr;
	char			*datalogger;
	int				evtfilesize,channelno,maxcts,mincts;
	char			*errors,*evtfilename,*ft,*filter,*srcid;
	char			*line;
	int				i;
	Pf				*pfnew;
	Dbptr			db;
	int 			put_tests=0;
	char 			*chanmatch=0;
	Hook			*hook=0;
	char			*tdummy=0;
	char 			*match=malloc(100);

	


    memset (&flags, 0, sizeof (flags));
    elog_init (argc, argv);
    elog_notify (0, "%s $Revision: 1.5 $ $Date: 2005/05/10 07:34:43 $\n",
		 Program_Name);

    while ((c = getopt (argc, argv, "m:n:r:S:c:tvV")) != -1) {
	switch (c) {
	  case 'm':
	    match = optarg;
		sprintf(pktmatch,"%s/pf/evtinfo",match);
	    break;
	  case 'c':
	 	chanmatch = optarg;
		break;
	  case 't':
	  	put_tests = 1;
		break;
	  case 'n':
	    maxpkts = atoi (optarg);
	    break;

	  case 'r':
	    reject = optarg;
	    break;

	  case 'S':
	    statefile = optarg;
	    break;

	  case 'v':
	    flags.verbose++;
	    break;

	  case 'V':
		flags.verbose++;
		flags.verbose++;
	    break;

	  case '?':
	    errflg++;
	}
    }

    if (errflg || argc - optind < 2 || argc - optind > 4)
	usage ();

    in = argv[optind++];
    out = argv[optind++];

    if (argc > optind) {
	after = str2epoch (argv[optind++]);
	specified_after = 1;
	if (argc > optind) {
	    until = str2epoch (argv[optind++]);
	    if (until < after) {
		until += after ;
	    }
	}
    }
    if ((orbin = orbopen (in, "r&")) < 0)
	die (0, "Can't open input '%s'\n", in);

    if (statefile != 0) {
		char           *s;
		if (exhume (statefile, &quit, RT_MAX_DIE_SECS, 0) != 0) {
	    elog_notify (0, "read old state file\n");
		}
		if (orbresurrect (orbin, &last_pktid, &last_pkttime) == 0) {
	    	elog_notify (0, "resurrection successful: repositioned to pktid #%d @ %s\n",
			 last_pktid, s = strtime (last_pkttime));
	    	free (s);
		} else {
	    	complain (0, "resurrection unsuccessful\n");
		}
    }
    if ((orbout = orbopen (out, "w&")) < 0) {
	die (0, "Can't open output '%s'\n", out);
    }
    if (pktmatch) {
	nmatch = orbselect (orbin, pktmatch);
    }
    if (nmatch < 0) {
	die (1, "select '%s' returned %d\n", pktmatch, nmatch);
    }
    if (reject) {
	nmatch = orbreject (orbin, reject);
    }
    if (nmatch < 0) {
		elog_die (1, "reject '%s' returned %d\n", reject, nmatch);
    } else {
		if (flags.verbose) {
			elog_notify (1,"%d sources selected\n", nmatch);
		}	
    }

    if (specified_after) {
	pktid = orbafter (orbin, after);
	if (pktid < 0) {
	    char           *s;
	    elog_complain (1, "seek to %s failed\n", s = strtime (after));
	    free (s);
	    pktid = forbtell (orbin);
	    elog_complain (1,"pktid is still #%d\n", pktid);
	} else {
	    if (flags.verbose) elog_notify (1,"new starting pktid is #%d\n", pktid);
	}
    }
    start_time = now ();

	db = dbtmp("rt1.0");

    while (!quit && pkttime < until && totpkts < maxpkts) {
	rcode = orbreap (orbin,
		    &pktid, srcname, &pkttime, &packet, &nbytes, &packetsz);

	switch (rcode) {
	  case 0:
	    totpkts++;
	    totbytes += nbytes;

	    if (flags.verbose>2) {
		showPkt (pktid, srcname, pkttime, packet, nbytes, stdout, mode);
	    }
	    if (statefile != 0
		    && last_pkttime - last_burial > decent_interval) {
		bury ();
		last_burial = pkttime;
	    }
	    if ((unstuffPkt (srcname, pkttime, packet, nbytes, &unstuffed))==Pkt_pf) {
		  	pf = 			unstuffed->pf;
			tbl=			pfkeys(pf);
			arrkey=			gettbl(tbl,0);
			pfget(pf,arrkey,&pfnew);
			datalogger=		pfget_string(pfnew,"datalogger");
			duration=		pfget_double(pfnew,"duration");
			errors=			pfget_string(pfnew,"errors");
			evtfilename=	pfget_string(pfnew,"evtfilename");
			evtfilesize=	pfget_int(pfnew,"evtfilesize");
			ft=				pfget_string(pfnew,"ft");
			starttime=		pfget_double(pfnew,"time");
			triggertime=	pfget_double(pfnew,"triggertime");

			channels=pfget_tbl(pfnew,"channels");
			if (strcasecmp(ft,"no")==0 || put_tests == 1) {
				for (i=0; i<maxtbl(channels);i++) {
					line=gettbl(channels,i);
					sscanf(line,"%s %d %lf %d %lf %lf %d %lf",
						netstachan,&channelno,&maxtime,&maxcts,&max_g,&mintime,&mincts,&min_g);
					split_srcname(netstachan,&parts);	
					strcpy(parts.src_suffix,"GENC");
					join_srcname(&parts,netstachan);
					if (flags.verbose>1) printf("%s,%s\n", netstachan,tdummy=strtime(triggertime));	
					if (chanmatch == 0 || 
							strmatches(parts.src_chan,chanmatch,&hook)) {
						if (flags.verbose) elog_notify(1,"putting detev for %s,%s\n", netstachan,tdummy=strtime(triggertime));	
						
						db=dblookup(db,0,"detev",0,"dbSCRATCH");
						dbputv(db,0,"sta",parts.src_sta,"chan",parts.src_chan,
							"filter",K2_FILTER,
							"time",	triggertime,
							"tron",  starttime - triggertime,
							"troff", starttime - triggertime + duration,
							"iphase","K2",
							"snr",(max_g * 1000.0  - min_g * 1000.0)/2.0,
							0);
						db2orbpkt(db,orbout);
					} else {
						if (flags.verbose>1) elog_notify(1,"ignoring %s,%s\n", netstachan,tdummy=strtime(triggertime));	

					}
				}
			}
			
	    }

	    last_pktid = pktid;
	    last_pkttime = pkttime;
	}

    }

    if (statefile != 0)
	bury ();

    end_time = now ();
    delta_t = end_time - start_time;
	if (flags.verbose>1) {
		if (totpkts > 0) {
		elog_notify (1,"\n%.0f %.2f byte packets (%.1f kbytes) in %.3f seconds\n\t%10.3f kbytes/s\n\t%10.3f kbaud\n\t%10.3f pkts/s\n",
			totpkts, totbytes / totpkts, totbytes / 1024,
			delta_t,
			totbytes / delta_t / 1024,
			totbytes / delta_t / 1024 * 8,
			totpkts / delta_t);
		} else {

		elog_notify (1,"\nno packets read\n");
		}
	}

    if (orbclose (orbin)) {
	elog_complain (1, "error closing read orb\n");
    }
    if (orbclose (orbout)) {
	elog_complain (1, "error closing write orb\n");
    }
    return 0;
}
Example #2
0
int DAS_gain( ClientData clientData ,
                char *srcname,
                double pkttime,
                uchar_t *packet )
{
    char cmdstr[256]; 
    char errmsg[512], msg[512];
    char key[256];
    int nchan;
    struct DasPreHdr hdr;
    DPars *dp;
    int i, skip_bytes, doff, chbytes;
    uchar_t *tmphdr;
    ushort_t gain, chid, val;
    short tmpval;
    TclOrb *tclorb = (TclOrb *) clientData;
    Srcname parts ; 

	split_srcname(srcname, &parts) ;
     
        memcpy( &key[0], packet, 256);
        tmphdr = (uchar_t *) &key[0];
 
        memcpy( &tmpval, tmphdr, sizeof(short ));
        tmphdr += sizeof(short );
        hdr.hdrsiz =  ntohs(  tmpval );

/*
printf("src=%s size=%d\n", srcname, hdr.hdrsiz);
fflush(stdout);
*/ 
        /* skip to the 'doff'(offset to real data) value in orb header */
        /* skipping: hdrtype+pkttype+pktsize+
		     calib+srate+
                     datatype+nsamp+nchan */      
        skip_bytes = 0;
	skip_bytes =  sizeof(short)+sizeof(short)+sizeof(short)+
                      sizeof(float)+ sizeof(float)+
                      sizeof(short)+sizeof(short)+sizeof(short);
 
        tmphdr += sizeof(short); /* skip hdrtype valuse */
        tmphdr += sizeof(short); /* skip pkttype valuse */
        tmphdr += sizeof(short); /* skip pktsize valuse */
        tmphdr += sizeof(float); /* skip calib valuse */
        tmphdr += sizeof(float); /* skip samprate valuse */
        tmphdr += sizeof(short); /* skip datatype valuse */
        tmphdr += sizeof(short); /* skip nsamp valuse */
        tmphdr += sizeof(short); /* skip nchan valuse */
        
        /* get doff value */
        memcpy( &tmpval, tmphdr, sizeof( short ));
        doff =  ntohs(  tmpval );

        nchan = packet[hdr.hdrsiz+NCHAN];

/*
printf("doff=%d ch=%d\n", doff, nchan);
fflush(stdout);
*/
	if (nchan <= 0 || nchan > 9  ) {
	    sprintf( errmsg, "Wrong number of channels - %d (%0x)!!!", nchan, packet[hdr.hdrsiz+NCHAN] );
	    sprintf( &msg[0], "%s {%s} \0", ERR_PROC, errmsg);
	    Tcl_Eval ( tclorb->interp, msg );
	    while ( Tk_DoOneEvent (TK_DONT_WAIT) )
	         ;
	    return 0;
        }
        doff += hdr.hdrsiz;
 
        for ( i = 0; i < nchan; i++ )  { 
 
           chid = (uchar_t) packet[doff];
           gain = (uchar_t) packet[doff+1];
            
           val = (uchar_t)packet[doff+2]*256 + (uchar_t)packet[doff+3]; 
           chbytes = val;

/*
printf("id=%d gain=%d byte=%d\n", chid, gain, chbytes);
fflush(stdout);
*/
           sprintf (key, "%s_chan%d\0", parts.src_sta, chid );
           dp = ( DPars *) getarr (tclorb->dpars, key);
           if ( dp != 0) {

               if(gain == 2)  
                   sprintf ( &cmdstr[0], 
		   "%s {%s} {x32} {none}\0", tclorb->parcallback, dp->widget  );
               else if(gain == 1) 
                   sprintf ( &cmdstr[0], 
		   "%s {%s} {x1} {none}\0", tclorb->parcallback, dp->widget  );
               else {
	           sprintf( errmsg, "Unknown gain value - %d for channel %d!", gain, chid);
	           sprintf( &msg[0], "%s {%s} \0", ERR_PROC, errmsg);
	           Tcl_Eval ( tclorb->interp, msg );
	           while ( Tk_DoOneEvent (TK_DONT_WAIT) )
	                ;
	           return 0;

               }
               if (tclorb->verbose > 0) {
	             printf ("%s\n", cmdstr );
	             fflush (stdout);
               }
               Tcl_Eval (tclorb->interp, cmdstr );
            }
            doff += CHDATA_OFF+chbytes; 
        }

     return 1;  

}