コード例 #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;
}
コード例 #2
0
static int
get_instype (Dbptr db, char *instype, double *samprate)
{
    static Dbptr    dbsi = INVALID_DBPTR;
    static Hook    *hook = 0;
    long            nmatches;
    Tbl            *tbl;
    char            sta[MAX_STA_SIZE],
                    chan[MAX_CHAN_SIZE];
    double          time;
    char           *s;
    int             errors = 0;

    if (dbsi.database < 0) {
	Dbptr           dbsensor,
	                dbinstrument;
	dbsensor = dblookup (db, 0, "sensor", 0, 0);
	dbinstrument = dblookup (db, 0, "instrument", 0, 0);
	dbsi = dbjoin (dbsensor, dbinstrument, 0, 0, 0, 0, 0);
    }
    nmatches = dbmatches (db, dbsi, 0, 0, &hook, &tbl);
    switch (nmatches) {
      case dbINVALID:
      case 0:
	dbgetv (db, 0, "sta", sta, "chan", chan, "time", &time, NULL);
	complain (0, "no matches in sensor/instrument join for %s:%s @ %s",
		  sta, chan, s = strydtime (time));
	free (s);
	errors++;
	break;

      default:
	dbgetv (db, 0, "sta", sta, "chan", chan, "time", &time, NULL);
	complain (0, "too many matches (%ld) in sensor/instrument join for %s:%s @ %s",
		  nmatches, sta, chan, s = strydtime (time));
	free (s);
	errors++;
	/* FALLTHRU */

      case 1:
	dbsi.record = (long) gettbl (tbl, 0);
	dbgetv (dbsi, 0, "instype", instype, "samprate", samprate, NULL);
	break;
    }

    freetbl (tbl, 0);
    return errors;
}
コード例 #3
0
Dbptr join_tables(Dbptr db, Pf *pf, Arr *tables)
{
	char *table_name;
	int *ilogic;
	Tbl *t;
	Dbptr dbj;
	long int nrec;
	int i;
	int ntables=0;

	/* This is an exact copy of above, but it is duplicated because
	this function could get stolen by another future program
	because it is pretty general */
	t = pfget_tbl(pf,"join_tables");
	if(t == NULL)
	{
		elog_die(0,"No list of tables to be joined\n");
	}
	for(i=0;i<maxtbl(t);++i)
	{
		table_name = gettbl(t,i);
		ilogic = (int *)getarr(tables,table_name);
		if(ilogic == NULL)
		{
			elog_die(0,"Table %s was not handled previously by check_tables.\nProgramming logic error\n",
				table_name);
		}
		else if(*ilogic)
		{
			if(ntables == 0)
				dbj = dblookup(db,0,table_name,0,0);
			else
				dbj=dbjoin(dbj,dblookup(db,0,table_name,0,0),
					NULL,NULL,0,NULL,0);
			++ntables;
			dbquery(dbj,dbRECORD_COUNT,&nrec);
			if(nrec == 0)
			{
				elog_complain(0,
					"join_tables error\njoined database has 0 length after joining table %s\n",
					table_name);
				dbj.record = dbINVALID;
				return(dbj);
			}
		}
	}
	return(dbj);
}
コード例 #4
0
ファイル: db2sql.c プロジェクト: ittaikurzon/antelope_contrib
Tbl *
dbschema2sqlcreate( Dbptr db, long flags )
{
	Tbl	*sql;
	char	*cmd;
	Tbl	*tables;
	char	*table;
	long	itable;

	sql = newtbl( 0 );

	if( db.table >= 0 ) {
		
		cmd = generate_sqltable_create( db, flags );

		pushtbl( sql, cmd );

	} else {

		dbquery( db, dbSCHEMA_TABLES, &tables );

		for( itable = 0; itable < maxtbl( tables ); itable++ ) {

			table = gettbl( tables, itable );

			db = dblookup( db, "", table, "", "" );
			
			cmd = generate_sqltable_create( db, flags );

			pushtbl( sql, cmd );
		}
	}

	return sql;
}
コード例 #5
0
ファイル: util.c プロジェクト: battistuz/antelope_contrib
int opendb( Dset *dset )
{
 
   char key[256];
   Tbl *sort_sta_ch_tm;
 
   if (dbopen_database ( dset->dbname, "r+", &(dset->db) ) == dbINVALID )
         die (0, "Can't open database %s\n",  dset->dbname );
 
    dset->db = dblookup ( dset->db, 0, "wfdisc", 0, 0);
    if ( dset->db.table == dbINVALID )
       die (0, "Can't open '%s' wfdisc table.\n", dset->dbname );
 
    /* Select specified sta&chan */
 
    sprintf( key,"(chan =~ /%s/ && sta=~ /%s/)\0", dset->chan, dset->sta );
    dset->db = dbsubset(  dset->db, key, 0 );
    sort_sta_ch_tm = strtbl("sta", "chan", "time", 0 ) ;
    dset->db = dbsort ( dset->db, sort_sta_ch_tm, 0, 0 ) ;
 
    dbquery ( dset->db, dbRECORD_COUNT, &(dset->dbrec) );
    if( dset->dbrec <= 0 )
       die( 0, " no record with sta == %s and chan == %s in %s.\n", 
                 dset->sta, dset->chan, dset->dbname );
 
 
    return 1;
 
}  
コード例 #6
0
/*This function adds a single row to the origerr table for this event.

arguments:
	orid - orid assign to this solution
	h - Hypocenter object for this solution
	dbo - output db

This version doesn't set much of this large table.  Eventually, it 
needs to include all the error terms, but that function is not 
written yet. 

Author:  Gary L. Pavlis
Written:  February 1997
*/
void save_origerr(long orid, Hypocenter h, double **C, Dbptr dbo)
{
	double sdobs; 
	double lddate;
	/* Intentionally ignored: smajax,sminax,strike,sdepth,stime,
					conf,commid */

	dbo = dblookup(dbo,0,"origerr",0,0);

	/* Bad news here is that we can't save the more useful 
	statistics that this program calculates.  css3.0 only allows
	sdobs = sswr/ndgf */

	sdobs = h.rms_raw;
	lddate = std_now();
	if(dbaddv(dbo,0,
		"orid",orid,
                "sxx",C[0][0],
                "syy",C[1][1],
                "szz",C[2][2],
                "stt",C[3][3],
                "sxy",C[0][1],
                "sxz",C[0][2],
                "syz",C[1][2],
                "stx",C[0][3],
                "sty",C[1][3],
                "stz",C[2][3],
		"sdobs",sdobs,
                "lddate",lddate,
			NULL) == dbINVALID)
	{
		elog_die(1,"save_origerr: dbaddv error writing origerr record for orid %ld\n",
				orid);
	}
}
コード例 #7
0
/* This routines cautiously saves the emodel vector.  It is cautious
because the emodel table is not part of the css3.0 schema.  If it
cannot find info on the emodel table, it issues an error and returns.
Otherwise, it will save the emodel vector to this special table.
Author:  Gary L. Pavlis
Written: June 29, 1998 */
int save_emodel(int orid, float *emodel, Dbptr db)
{
	Dbptr dbemod;  

	dbemod = dblookup(db, 0,"emodel",0,0);
	if(dbemod.table == dbINVALID)
	{
		elog_log(0,"emodel table not defined for output database\nAdd genloc mods\n");
		return(1);
	}
	else
	{
		if(dbaddv(dbemod,0,
			"orid",orid,
			"emodelx",emodel[0],
			"emodely",emodel[1],
			"emodelz",emodel[2],
			"emodelt",emodel[3],
				NULL ) == dbINVALID)
		{
			elog_log(0,"dbaddv error for emodel table\n");
			return(-1);
		}
	}
	return(0);
}
コード例 #8
0
ファイル: dbpmel.c プロジェクト: battistuz/antelope_contrib
void save_run_parameters(Dbptr db,Pf *pf)
{
	char *dir,*dfile;
	char filename[512];
	char *vm,*vm3d;
	int ierr;
	
	dir = pfget_string(pf,"pmelrun_archive_directory");
	if(dir==NULL)elog_die(0,"Parameter pmelrun_archive_directory not in parameter file\n");
	if(makedir(dir))
		elog_die(0,"makedir failed on directory %s\n",dir);
	dfile = pfget_string(pf,"pmel_run_name");

	
	vm = pfget_string(pf,"travel_time_model");
	vm3d=pfget_string(pf,"3Dreference_model");
	if( (vm==NULL) || (vm3d==NULL) )
		elog_die(0,"Missing required velocity model definitions\nCheck parameters travel_time model and 3Dreference_model\n");
	db = dblookup(db,0,"pmelruns",0,0);
	ierr=dbaddv(db,0,"pmelrun",dfile,
		"vmodel",vm,
		"vmodel3d",vm3d,
		"dir",dir,
		"dfile",dfile,0);
	if(ierr < 0) elog_die(0,
		   "dbaddv error on pmelrun table\nVerify schema extensions for dbpmel and that the pmel_run_name parameter is unique\n");

	strcpy(filename,dir);
	strcat(filename,"/");
	strcat(filename,dfile);
	if(pfwrite(filename,pf))
		elog_die(0,"pfwrite error for file %s\n",filename);
}
コード例 #9
0
static int
dbrows2orb(Dbptr db, int orb, char *prefix)
{
	Packet         *pkt;
	char            srcname[ORBSRCNAME_SIZE];
	double          time;
	char           *packet;
	int             nbytes, packetsize = 0;
	Dbptr           tmpdb;
	long            t, nrecords, r, ntables;
	Arr            *records = NULL;
	Tbl            *tables = NULL, *static_tables;
	char           *thistablename;
	Stbl           *stbl;
	char           *s;

	dbuntangle(db, &records);
	tables = keysarr(records);
	ntables = maxtbl(tables);
	if (ntables > 0)
		pkt = newPkt();
	if (prefix)
		strncpy(pkt->parts.src_net, prefix, PKT_TYPESIZE);
	pkt->pkttype = suffix2pkttype("db");
	for (t = 0; t < ntables; t++) {
		thistablename = gettbl(tables, t);
		tmpdb = dblookup(db, 0, thistablename, 0, 0);
		stbl = (Stbl *) getarr(records, thistablename);
		nrecords = maxstbl(stbl);
		if (nrecords > 0) {
			for (r = 0; r < nrecords; r++) {
				tmpdb.record = (long) getstbl(stbl, r);
				pkt->db = tmpdb;
				if (stuffPkt(pkt, srcname, &time, &packet, &nbytes, &packetsize) < 0) {
					elog_complain(0, "stuffPkt fails for pf packet");
					return (-1);
				}
				if (orbput(orb, srcname, time, packet, nbytes) < 0) {
					elog_complain(0, "Couldn't send packet to orb\n");
					return (-1);
				}
			}
		}
	}
	freetbl(tables, 0);
	dbfree_untangle(records);
	freePkt(pkt);
	if (verbose) {
		elog_notify(0, "%s: %d patcket(s) sent with sourcename: %s\n", s = strtime(now()), nrecords, srcname);
		free(s);
	}
	return (0);




}
コード例 #10
0
/* This first little function returns 0 if the table defined by
the string t is a valid table name for the database pointed
to by the database pointer db.  Otherwise it returns a 1.
This allows a general way to safely check for the validity of 
extension tables to a common schema like css3.0.  General usage would be:

if(dbtable_invalid(db,t)) elog_die(0,"blah blah");

Generally this routine should be called once for a given table, but
it can be called repeatedly at the cost of efficiency.  The later
is specially bad if the table involved is hit often.  
*/
int dbtable_invalid(Dbptr db,char *t)
{
	Dbptr dbcheck;

	dbcheck = dblookup(db,0,t,0,0);
	if(dbcheck.table == dbINVALID)
		return(-1);
	else
		return(0);
}
コード例 #11
0
/* returns true if the table t is empty */
int dbtable_empty(Dbptr db,char *t)
{
	long nrows;
	db=dblookup(db,0,t,0,0);
	dbquery(db,dbRECORD_COUNT,&nrows);
	if(nrows>0) 
		return(0);
	else
		return(1);
}
コード例 #12
0
void set_shot_variable(Dbptr db, Arr *tables, int evid, SEGYTraceHeader *h)
{
	int *ok;
	char ss_string[30];
	long int ntest;
	double dnorth, deast, elev, edepth;

	ok = getarr(tables,"shot");
	if(ok)
	{
		db = dblookup(db,0,"shot",0,0);
		sprintf(ss_string,"evid == %d",evid);
		db = dbsubset(db,ss_string,0);
		dbquery(db,dbRECORD_COUNT,&ntest);
		if(ntest <= 0)
		{
			elog_complain(0,"evid %d not found in shot table\nShot coordinates will not be saved in segy headers\n",
				evid);
			dbfree(db);
			return;
		}
		else if(ntest > 1)
		{
			elog_notify(0,"multiple rows in shot found for evid %d\n",
				evid);
		}
		db.record = 0;
		if(dbgetv(db,0,
			"dnorth",&dnorth,
			"deast", &deast,
			"elev", &elev,
			"edepth",&edepth,
				NULL) == dbINVALID)
		{
			elog_complain(0,"dbgetv error for evid %d\nShot coordinates will not be saved in segy headers\n",
				evid);
			return;
		}
		/*convert to m from km */
		deast *= 1000.0;
		dnorth *= 1000.0;
		h->sourceLongOrX          = htonl((int32_t) deast);
		h->sourceLatOrY           = htonl((int32_t) dnorth);
		h->sourceSurfaceElevation = htonl((int32_t) elev);
		h->sourceDepth            = htonl((int32_t) edepth);
		/* WARNING:  This assumes receiver coordinates have already been set */
		h->sourceToRecDist = htonl( (int32_t) hypot(
					dnorth - ((double)(ntohl(h->recLatOrY ))),
					deast  - ((double)(ntohl(h->recLongOrX)))
		));
	}
	dbfree(db);
}
コード例 #13
0
int save_origin(int nass, int evid, Dbptr master_db, Dbptr dbtmp,
			Hypocenter h,Location_options o, int orb)
{
	int ndef;
	char dtype[2];
        char algorithm[16]="ggnloc";
	char auth[16]="orbgenloc";
	int grn, srn;
	int orid;  /* orid returned */

	orid = dbnextid(master_db,"orid");
	if(orid  < 0 )
		elog_die(1,"save_origin:  dbnextid failure asking for new orid\n");
        if(o.fix[2])
        {
                ndef = h.degrees_of_freedom + 3;
                strcpy(dtype,"r");
        }
        else
        {
                ndef = h.degrees_of_freedom + 4;
                strcpy(dtype,"f");
        }
	grn = grnumber(h.lat, h.lon) ;
	srn = srnumber ( grn ) ;
	dbtmp = dblookup(dbtmp,0,"origin",0,0);
	dbtmp.record = dbSCRATCH;
	if((dbputv(dbtmp,0,
                "lat",h.lat,
                "lon",h.lon,
                "depth",h.z,
                "time",h.time,
                "orid",orid,
		"evid",evid,
		"nass",nass,
		"grn", grn,
		"srn", srn,
		"dtype",dtype,
		"ndef",ndef,
		"algorithm",algorithm,
		"auth",auth,0))
			 == dbINVALID)
	{
		elog_complain(0,"dbputv error while building origin record for orid %d\nNo results saved\n",
			orid);
	}
	else
	{
		if(save_dbrecord(dbtmp,orb))
			elog_complain(0,"Errors saving orid %d\n",orid);
	}
	return(orid);
}
コード例 #14
0
/*This function adds a single row to the origerr table for this event.

arguements:
	orid - orid assign to this solution
	h - Hypocenter object for this solution
	dbo - output db

This version doesn't set much of this large table.  Eventually, it 
needs to include all the error terms, but that function is not 
written yet. 

Author:  Gary L. Pavlis
Written:  February 1997
*/
void save_origerr(int orid, Hypocenter h, double **C, Dbptr db, int orb)
{
	double sdobs; 
	int rc;
	double smajax,sminax,strike,sdepth,stime;
	double conf=0.683;

	db = dblookup(db,0,"origerr",0,0);
	db.record = dbSCRATCH;

	/* computer error ellipse parameters */
	rc = project_covariance(C,CHI_SQUARE,&conf,
			h.rms_weighted,h.degrees_of_freedom,
			&smajax,&sminax,&strike,&sdepth,&stime);

	/* Bad news here is that we can't save the more useful 
	statistics that this program calculates.  css3.0 only allows
	sdobs = sswr/ndgf */

	sdobs = h.rms_raw;
	if(dbputv(db,0,
		"orid",orid,
                "sxx",C[0][0],
                "syy",C[1][1],
                "szz",C[2][2],
                "stt",C[3][3],
                "sxy",C[0][1],
                "sxz",C[0][2],
                "syz",C[1][2],
                "stx",C[0][3],
                "sty",C[1][3],
                "stz",C[2][3],
		"sdobs",sdobs,
		"smajax",smajax,
		"sminax",sminax,
		"strike",strike,
		"sdepth",sdepth,
		"stime",stime,
		"conf",conf,
			0) == dbINVALID)
	{
		elog_complain(0,"save_origerr: dbputv error writing origerr record for orid %d\norigerr record not saved anywhere\n",
				orid);
	}
	else
	{
		if(save_dbrecord(db,orb))
			elog_complain(0,"Error saving origerr record for orid %d\n",
				orid);
	}

}
コード例 #15
0
/* Initializes the scratch records for each table used in this program.  
That is, origin, origerr, and assoc.  This only needs to be done
once.  The procedure loads NULL files into each attribute of the
scratch records for these tables.  This function is based
on a tricky initialization condition using dbget.  Setting
the db record to dbNULL and then calling dbget(db,0) loads the
scratch record with the contents of the NULL record.  This works
because the NULL record is initialized the first time it is
accessed, and passing 0 to dbget is a shortcut that directly 
copies the NULL record to the scratch record.  IMPORTANT:  note
that because this program uses dbputv directly to the scratch
record the assumption is that the SAME set of attributes are
always written to the scratch record of each table.  This is
correct for this program, but you are warned if you try to use
functions from this program elsewhere.  

Arguments:  db - db pointer of open database 

returns 0 on success.  Any errors will always result in
a return of dbINVALID.  The later should, of course, be
trapped but should probably not normally be fatal.
*/
initialize_scratch_records(Dbptr db)
{
	int return_code;
	int error_count=0;

	db = dblookup(db,0,"origin",0,0);
	db.record = dbNULL;
	return_code = dbget(db,0);
	if(return_code) ++error_count;
	db = dblookup(db,0,"origerr",0,0);
	db.record = dbNULL;
	return_code = dbget(db,0);
	if(return_code) ++error_count;
	db = dblookup(db,0,"assoc",0,0);
	db.record = dbNULL;
	return_code = dbget(db,0);
	if(return_code) ++error_count;
	if(error_count)
		return(dbINVALID);
	else
		return(0);
}
コード例 #16
0
/* This routine looks for a Tbl in the parameter space that
defines a set of auxiliary tables that are to be used.  It
cautiously checks to see if that table is defined in the
schema and is nonempty.  It sets logical variables in associative
array it returns that define if the table is "ok".  This is serious
overkill added to make the code for expandable in the future.
At the moment the only table that would be used is "shot".
It would, however, be easy to add similar auxiliary tables for
segy constructs like statics, mute definition, etc.  In that
case there probably should be a more general mechanism than this
but I'm more or less laying out a useful functionality here rather
than doing it in a completely general way.
*/
Arr *check_tables(Dbptr db, Pf *pf)
{
	char *table;
	int *ilogic;
	int i;
	Tbl *t;
	Dbptr dbtmp;
	int table_ok;
	long int nrec;
	Arr *a;

	a = newarr(0);
	t = pfget_tbl(pf,"join_tables");
	if(t == NULL)
	{
		elog_die(0,"No list of tables to be joined\n");
	}
	for(i=0;i<maxtbl(t);++i)
	{
		/* This series of conditionals is safe:  It certifies
		a table (a) is defined in this schema and (b) is
		not empty.  */
		table = (char *)gettbl(t,i);
		dbtmp = dblookup(db,0,table,0,0);
		if(dbtmp.table == dbINVALID)
			table_ok = 0;
		else
			table_ok = 1;
		if(table_ok)
		{
			dbquery(dbtmp,dbRECORD_COUNT,&nrec);
			if(nrec > 0)
				table_ok = 1;
			else
				table_ok = 0;
		}
		ilogic = (int *) malloc(sizeof(int));
		if(ilogic == NULL) elog_die(0,"malloc error\n");
		*ilogic = table_ok;
		setarr(a,table,ilogic);
	}
	return(a);
}
コード例 #17
0
/* Changed to int from void by JN in order to return evid. */
long save_event(Dbptr dbi, long is, long ie, long orid, Dbptr dbo)

{
	/* these are variables in dbi copied to dbo */
	long evid;
	char evname[16];
	
	/*altered in output by this program */
	long prefor;
	char auth[20];
	double lddate;

	/* intentionally ignored:  commid */

	dbo = dblookup(dbo,0,"event",0,0);
	dbi.record = is;
	if( dbgetv(dbi,0,
		"event.evid",&evid,
		"event.evname",evname,	
				NULL) == dbINVALID)
	{
		elog_die(1,"save_event: dbgetv error reading event fields of input view at record %ld\n",
				is);
	}
	prefor = orid;
	my_username(auth);
	lddate = std_now();
	if(dbaddv(dbo,0,
                "evid",evid,
		"evname",evname,
		"prefor",prefor,
                "auth",auth,
                "lddate",lddate,
			NULL ) == dbINVALID)
	{
		elog_die(1,"save_event: dbaddv error writing event record for orid %ld\n",
				orid);
	}
        /* Added by JN */
	return(evid);
}
コード例 #18
0
/* This routine saves array average amplitude estimates in a table
called mwavgamp.  This routine always adds one and only one record.

arguments:

array - array name assigned to the group of stations used to produce
	this set of averages
evid - css3.0 event id
bankid - multiwavelet group id 
phase - phase name as in css3.0
fc - center frequency (in Hz) of the wavelets used
t0 - start time at reference station
twin - length of analysis time window relative to t0
mwamp - computed array average amplitude
erramp - estimate of error in mwamp 
	Note: both mwap and erramp are log10 values derived from nm/s
	converted trace data (assumed).
ndgf - degrees of freedom in estimate of mwamp
db - pointer to output database 

Author:  G Pavlis
Written:  March 2000
*/
int MWdb_save_avgamp(char *array,
	int evid, 
	int bankid,
	char *phase,
	double fc,
	double t0,
	double twin,
	double mwamp,
	double erramp,
	int ndgf,
	Dbptr db)
{

	db = dblookup(db,0,"mwavgamp",0,0);

	/* Note algorithm and ampcomp are frozen here.   */

	if( dbaddv(db,0,"sta",array,
		"evid",evid,
		"bankid",bankid,
		"phase", phase,
		"fc",fc,
		"ampcomp",AMPCOMP,
		"time",t0,
		"twin",twin,
		"mwamp",mwamp,
		"erramp",erramp,
		"ndgf",ndgf,
		"algorithm","mwap",0) < 0) 
	{
		elog_notify(0,
			"dbaddv error for mwavgamp table on evid %d with  fc=%lf\n",
				evid,fc);
		return(-1);
	}
	else
		return(0);
}
コード例 #19
0
ファイル: ryo2orb.c プロジェクト: battistuz/antelope_contrib
void
announce_StachanCalib( void *sccp, char *sta, char *chan ) 
{
	StachanCalib *scc = (StachanCalib *) sccp;
	static Dbptr db = { dbINVALID, dbINVALID, dbINVALID, dbINVALID };

	if( db.database == dbINVALID ) {

		db = dbtmp( "rt1.0" );

		db = dblookup( db, "", "wfoffset", "", "dbSCRATCH" );
	}

	if( abs( scc->offset ) < DBL_MIN ) {
		
		return;
	}

	dbputv( db, 0, "sta", sta, 
		       "chan", chan, 
		       "time", now(), 
		       "endtime", 9999999999.999,
		       "valoffset", scc->offset,
		       0 );

	if( db2orbpkt( db, Orbfd ) < 0 ) {
		
		elog_complain( 0, "Orbput failed for wfoffset packet!\n" );

	} else {
		
		scc->announced++;
	}

	return;
}
コード例 #20
0
int main(int argc, char **argv)
{
	char *sift_expr, *wfdir, *format, *dbin, *dbout, *chan_maps;
	double calper;
	int overwrite=0;
	int ndec_stages;
	char **dec_stages;
	Tbl *chan_in_tbl, *chan_out_tbl;
	Dbptr dbi, dbo;
	Dbvalue dbv;
	Response *resp;
	Tbl *ncoefs, *coefs, *dec_fac;
	int dec_factor;
	int nwi, n, i, j, nchans;
	char string[512];
	char expr[1024];
	Dbptr dbwfi;
	char dbbase[1024];
	char dir[128];
	char dfile[128];
	char fnamei[1024];
	char fnameo[1024];
	char wfdir1[512];
	char wfdir2[512];
	double time, tref;

	/* Get command line args */

	if (!getargs(argc, argv, &sift_expr, &calper, &wfdir, &format, &dbin,
				&dbout, &chan_maps, &ndec_stages, &dec_stages)) {
		usage();
		exit (1);
	}
	if (calper >= 0.0) {
		fprintf (stderr, "dbdec: -c option not operational.\n");
		usage();
		exit (1);
	}

	/* Parse channel maps and wfdir */

	if (!parse_chan_maps(chan_maps, &chan_in_tbl, &chan_out_tbl)) {
		fprintf (stderr, "dbdec: Unable to parse channel maps, '%s'\n",
						chan_maps);
		usage();
		exit (1);
	}
	if (!parse_wfdir(wfdir, wfdir1, wfdir2)) {
		fprintf (stderr, "dbdec: Unable to parse wfdir, '%s'\n",
						wfdir);
		usage();
		exit (1);
	}

	/* Open and read decimation stage files */

	if (!read_dec_files(ndec_stages, dec_stages, &resp, &dec_factor, &ncoefs, &coefs, &dec_fac)) {
		fprintf (stderr, "dbdec: Unable to read decimation stage(s).\n");
		usage();
		exit (1);
	}
	printf ("Total decimation factor = %d\n", dec_factor);

	/* Open the input database */

        if (dbopen (dbin, "r+", &dbi) == dbINVALID) {
                elog_clear_register(1);
                fprintf (stderr, "dbdec: Unable to open input database '%s'.\n",
									dbin);
                exit (1);
        }
	dbi = dblookup (dbi, 0, "wfdisc", 0, 0);
        dbquery (dbi, dbRECORD_COUNT, &nwi);
        if (nwi < 1) {
        	fprintf (stderr, "dbdec: No wfdisc rows for input database '%s'.\n",
									dbin);
                exit (1);
        }

	/* Compose a subset expression for the input channel mappings */

	nchans = maxtbl (chan_in_tbl);
	for (i=0; i<nchans; i++) {
		sprintf (string, "chan == \"%s\"", gettbl(chan_in_tbl, i));
		if (i == 0) {
			strcpy (expr, "(");
			strcat (expr, string);
		} else {
			strcat (expr, " || ");
			strcat (expr, string);
		}
	}
	strcat (expr, ")");
	if (sift_expr) {
		strcat (expr, " && (");
		strcat (expr, sift_expr);
		strcat (expr, ")");
	}

	/* Subset the input wfdisc table */

	dbwfi = dblookup (dbi, 0, "wfdisc", 0, 0);
	dbwfi = dbsubset (dbwfi, expr, 0);
        dbquery (dbwfi, dbRECORD_COUNT, &n);
        if (n < 1) {
        	fprintf (stderr, "dbdec: No input channels for database '%s'.\n",
									dbin);
                exit (1);
        }
	printf ("Processing %d out of %d waveform segments.\n", n, nwi);

	/* Open the output database */

        if (dbopen (dbout, "r+", &dbo) == dbINVALID) {
                elog_clear_register(1);
                fprintf (stderr, "dbdec: Unable to open output database '%s'.\n",
									dbout);
                exit (1);
        }
	dbo = dblookup (dbo, 0, "wfdisc", 0, 0);
        dbquery (dbo, dbTABLE_DIRNAME, &dbv);
        strcpy (dbbase, dbv.t);

	/* Make a pass through the input wfdiscs to look for wf file conflicts */

	if (!overwrite) {
		printf ("Looking for waveform file conflicts...");
		fflush (stdout);
	}
	tref = 1.e30;
	for (dbwfi.record=0; dbwfi.record<n; dbwfi.record++) {
		if (!overwrite && (!strcmp(wfdir1,wfdir2)) ){
			if (!makeoutfname (dbwfi, wfdir1, wfdir2, dbbase, dir, dfile, fnameo)) {
				fprintf (stderr, "dbdec: makeoutfname() error.\n");
				exit (1);
			}
			if (zaccess(fnameo, F_OK) != -1) {
				fprintf (stderr, "\ndbdec(Warning): wf file conflict for '%s'.\n",
									fnameo);
				fprintf(stderr,"Will append but may waste space\n");
			}
		}
		dbgetv (dbwfi, 0, "time", &time, 0);
		if (time < tref) tref = time;
	}
	if (!overwrite) {
		printf ("OK\n");
	}
	tref = (double)((int)tref);

	/* Loop through and do the decimation */

	for (dbwfi.record=0; dbwfi.record<n; dbwfi.record++) {
		Trace *trace=NULL;

		/* Read in trace and convert to float */

		SCV_free_trace (trace);
		trace = NULL;
		trace = read_trace (dbwfi);
		if (trace == NULL) {
			fprintf (stderr, "dbdec: read_trace() error.\n");
			continue;
		}

		/* Decimate float trace */

		if (!decimate_trace (trace, ncoefs, coefs, dec_fac, tref)) {
			fprintf (stderr, "dbdec: decimate_trace() error.\n");
			exit (1);
		}

		/* Convert trace to output units and put back in data gaps */

		trace = convert_trace (trace, format);
		if (trace == NULL) {
			fprintf (stderr, "dbdec: convert_trace() error.\n");
			exit (1);
		}

		/* Write out decimated trace and wfdisc */

		makeoutfname (dbwfi, wfdir1, wfdir2, dbbase, dir, dfile, fnameo);
		if (!write_trace (dbwfi, dbo, dir, dfile, fnameo, 
					chan_in_tbl, chan_out_tbl, trace, overwrite)) {
			fprintf (stderr, "dbdec: write_trace() error.\n");
			exit (1);
		}
	}

	/* Fix up the output sensor, sitechan, etc. tables */

	if (!fixup_tables (dbi, dbo, chan_in_tbl, chan_out_tbl, calper, resp, dec_factor)) {
		fprintf (stderr, "dbdec: fixup_tables() error.\n");
		exit (1);
	}

	/* Normal exit */

	exit (0);
}
コード例 #21
0
/* This function computes the attributes of the predarr table.  
This is seriously complicated by the fact that the timedef and 
slowdef attributes in the css3.0 schema allow independently turning
off time and slowness residuals.  The way we solve this here is to
first save results for all the entries in the Arrival tbl keeping
a temporary list of all the arid values found in the Arrival tbl.  
We then use a stupid linear search algorithm against each arid found
in the Slowness_vector table and only compute a new predarr row if
the arid found for that Slowness vector did not match any found in arid.
The linear search is acceptable here under an assumption that arrival
data will outnumber slowness vector measurements in most locations. 

This algorithm looks for the stavel table that is also defined in the
takeoff schema extensions.  This table defines surface velocities to 
use in computing emergence angles (ema).  If an entry is not found
for a given phase in stavel, the program tries to decide if this is a
P or S phase (by looking at the last character in the phase name) and 
using a default (hardwired below).  This will generate a complain error
because the values in predarr are not necessarily consistent with vmodel
in this situation.

arguments:
	db - database to save results to
	atbl - Tbl of Arrival structures used in genloc
	utbl - Tbl of Slowness_vector structures comparable to a
	h - hypocenter estimate to be used to compute predarr rows
	orid - orid database id assigned to this solution.
	vmodel - name of velocity model being used (key of stavel)

Returns 0 for success, anything else indicates and error.  A positive
return indicates the number of warning messages issued.  A negative
return indicates nothing was written because the predarr table could
not be written due to either:  (1) undefined in the schema or (2) 
table is not writeable. 
Author:  G Pavlis
*/
int save_predarr( Dbptr db,  Tbl *atbl, Tbl *utbl, 
	Hypocenter h, int orid, char *vmodel)
{
	int natimes;
	int nslow;
	long *allarids;
	int errors=0;

	Arrival *atimes;
	Arrival a;
	Station station;
	Slowness_vector *slow;
	Slowness_vector u;
	Seismic_Array sta_array;
	
	Travel_Time_Function_Output tto;
	Slowness_Function_Output u_calc;
	/* These are the fields computed here and saved in predarr */
	double time, utotal, seaz, esaz, ema, dip;

	int stavel_ok;  /* set to 1 if stavel is found valid.  Function
		returns immediately if the predarr table is not defined */

	double ema_null=-1.0;
	int i,k;

	if(dbtable_invalid(db,"predarr"))
	{
		elog_log(0,"predarr table not defined in schema for this database\n");
		return(-1);
	}
	if(dbtable_invalid(db,"stavel"))
	{
		elog_log(0,"stavel table is not defined.  Using defaults for all stations and phases\n");
		stavel_ok = 0;
	}
	else if(dbtable_empty(db,"stavel"))
	{
		stavel_ok=0;  /* do this silently */
	}
	else 
	{
		stavel_ok = 1;
		/* This is the correct way to find the null value for a field*/
		db = dblookup(db,0,"predarr",0,0);
		db.record = dbNULL;
		dbgetv(db,0,"ema",&ema_null,NULL );
	}

	natimes = maxtbl(atbl);
	nslow = maxtbl(utbl);
	if(natimes > 0)
	{
		allarids = (long *)calloc(natimes,sizeof(long));
		if(allarids == NULL) elog_die(0,"save_predarr cannot alloc %d long integers\n",
					natimes);
	}
	/* These quantities need to be explicitly initialized at the top
	of this loop.  They stay fixed for the whole loop.  Only the
	sta_array set really matters. */
	u.ux = 0.0;
	u.uy = 0.0;
	u.array = &sta_array;
	db = dblookup(db,0,"predarr",0,0);

	for(i=0;i<natimes;++i)
	{
		atimes = (Arrival *) gettbl(atbl,i);
		/* discard - phases as predarr makes no sense for them */
		if(strstr(atimes->phase->name,"-")!=NULL) continue;
		allarids[i] = atimes->arid;
		tto = calculate_travel_time(*atimes,h,ALL);
		if(tto.time == TIME_INVALID)
		{
			elog_log(0,"save_predarr failed to compute predicted arrival times for station %s and phase %s\n", 
				atimes->sta->name, atimes->phase->name);
			++errors;
			continue;
		}
		time = h.time + tto.time;
		dip = compute_dip(tto);
		esaz = compute_esaz(tto);

		/* This loads a fake slowness vector for each station.
		Note this is different from the location code and could
		fail in some situations when the location computation 
		did not.   In those situations, slow, ema, and esaz
		cannot be computed.  */
		u.arid = atimes->arid;
		strcpy(u.array->name,atimes->sta->name);
		u.array->lat = atimes->sta->lat;
		u.array->lon = atimes->sta->lon;
		u.array->elev = atimes->sta->elev;
		u.phase = atimes->phase;
		
		u_calc = calculate_slowness_vector(u,h,RESIDUALS_ONLY);
		if(u_calc.ux == SLOWNESS_INVALID)
		{
			elog_log(0,"predarr failed to compute slowness vector for station %s and phase %s\n",
				atimes->sta->name, atimes->phase->name);
			++errors;
			if(dbaddv(db,0,
				"arid",atimes->arid,
				"orid",orid,
				"time",time,
				"esaz",esaz,
				"dip",dip,
				NULL ) == dbINVALID)
			{
				elog_log(0,"dbaddv on predarr table for station %s and phase %s\n",
					atimes->sta->name, atimes->phase->name);
				++errors;
			}
				
		}
		else
		{
			utotal = hypot(u_calc.ux,u_calc.uy);
			/* css3.0 stores slowness is s/deg so we have
			to convert */
			utotal *= KMPERDEG;
			seaz = compute_seaz(u_calc);
			if(stavel_ok) ema = compute_ema(&u,&u_calc,db,vmodel);
			if(ema < 0.0) ema = ema_null;
			if(dbaddv(db,0,
				"arid",atimes->arid,
				"orid",orid,
				"time",time,
				"slow",utotal,
				"seaz",seaz,
				"ema", ema,
				"esaz",esaz,
				"dip",dip,
				NULL ) == dbINVALID)
			{
				elog_log(0,"dbaddv on predarr table for station %s and phase %s\n",
					atimes->sta->name, atimes->phase->name);
				++errors;
			}
		}

	}
	/* Nww we have to hunt for slowness measurements that have not
	travel time computed above.  Here we used a dumb linear search
	against arid for reasons give above.  The rest of the algorithm
	closely parallels the above loop, except the role of arrival 
	and slowness are reversed.  */
	a.time = 0.0;
	a.sta = &station;
	for(i=0;i<nslow;i++)
	{
		slow = (Slowness_vector *) gettbl(utbl,i);
		for(k=0;k<natimes;k++) 
			if(allarids[k] == slow->arid) break;
		if(k>=natimes)
		{
		/* Only when the above search fails will we actually do anything 
		in this loop */
			a.arid = slow->arid;
			strcpy(a.sta->name,slow->array->name);
			a.sta->lat = slow->array->lat;
			a.sta->lon = slow->array->lon;
			a.sta->elev = slow->array->elev;
			a.phase = slow->phase;
			tto = calculate_travel_time(a,h,ALL);
			if(tto.time == TIME_INVALID)
			{
				elog_log(0,"save_predarr failed to compute predicted arrival times for station %s and phase %s\n", 
					atimes->sta->name, atimes->phase->name);
				++errors;
				continue;
			}
			time = h.time + tto.time;
			dip = compute_dip(tto);
					
			u_calc = calculate_slowness_vector(*slow,h,RESIDUALS_ONLY);
			if(u_calc.ux == SLOWNESS_INVALID)
			{
				elog_log(0,"predarr failed to compute slowness vector for station %s and phase %s\n",
					atimes->sta->name, atimes->phase->name);
				++errors;
				if(dbaddv(db,0,
					"arid",atimes->arid,
					"orid",orid,
					"time",time,
					"esaz",esaz,
					"dip",dip,
					NULL ) == dbINVALID)
				{
				    elog_log(0,"dbaddv on predarr table for station %s and phase %s\n",
					atimes->sta->name, atimes->phase->name);
				    ++errors;
				}
				
			}
			else
			{
				utotal = hypot(u_calc.ux,u_calc.uy);
				utotal *= KMPERDEG;
				seaz = compute_seaz(u_calc);
				if(stavel_ok) ema = compute_ema(&u,&u_calc,db,vmodel);
				if(ema < 0.0) ema = ema_null;
				if(dbaddv(db,0,
					"arid",atimes->arid,
					"orid",orid,
					"time",time,
					"slow",utotal,
					"seaz",seaz,
					"ema", ema,
					"esaz",esaz,
					"dip",dip,
					NULL ) == dbINVALID)
				{
				    elog_log(0,"dbaddv on predarr table for station %s and phase %s\n",
					atimes->sta->name, atimes->phase->name);
				    ++errors;
				}
			}
		}
	}
	free(allarids);
	return(errors);
}
コード例 #22
0
double compute_ema (Slowness_vector *u, Slowness_Function_Output *ucalc, 
		Dbptr db, char *vmodel)
{
	Dbptr dbm, dbsv;
	static Tbl *matches;
	static Tbl *kmatch;   /* Used to hold keys for dbmatches.  */
	char *key;
	double velocity;
	static Hook *hook=0;
	long nmatches;
	char cphase;  /* store last char of phase name here */
	int name_len;
	int i; 
	double utotal, ema; 
	
	dbsv = dblookup(db,0,"stavel",0,0);
	dbm = dblookup(db,0,"stavel",0,"dbSCRATCH");
	dbputv(dbm,0,"sta",u->array->name,"phase",u->phase->name,"vmodel",vmodel,NULL );
	/* It seems to be necessary to explicitly define the match keys.  Using natural
	keys defined with null patterns for dbmatches did not work correctly. */
	kmatch = newtbl(0);
	key = strdup("sta");
	pushtbl(kmatch,key);
	key = strdup("phase");
	pushtbl(kmatch,key);
	key = strdup("vmodel");
	pushtbl(kmatch,key);

	dbmatches(dbm,dbsv,&kmatch,&kmatch,&hook,&matches);
	freetbl(kmatch,free);
	nmatches = maxtbl(matches);
	if(nmatches >= 1)
	{
		if(nmatches > 1) elog_log(0,"warning(compute_ema):  multiple records found in stavel table with same primary key\n");  
		dbsv.record = (long) gettbl(matches,0);
		dbgetv(dbsv,0,"velocity",&velocity,NULL );
	}
	else
	{
		/* No matches are found when we end up here.  Revert to default and 
		decide to us P or S velocity by hunting backward from the string
		looking for the first occurence of P or S*/
		if(!strcmp(u->phase->name,"Lg"))
			velocity = DEFAULT_VS;
		else
		{
			name_len = strlen(u->phase->name);
			velocity = DEFAULT_VS;  /* This is the default default */
			for(i=name_len-1;i>=0;i--)
			{
				cphase = u->phase->name[i];
				if(cphase == 'S'){
					velocity = DEFAULT_VS;
					break;
				}
				else if(cphase == 'P'){
					velocity = DEFAULT_VP;
					break;
				}
			}
			elog_log(0,"compute_ema:  no matching entry found in stavel table for station/phase/vmodel = %s/%s/%s\n"
			"Reverting to default surface velocity of %f\n",
				u->array->name,u->phase->name,vmodel,velocity);
		}
		freetbl(matches,0);
	}
	/* now the actual computation is pretty trivial */
	utotal = hypot(ucalc->ux,ucalc->uy);
	ema = asin(utotal*velocity);
	return(deg(ema));
}
コード例 #23
0
int
main (int argc, char **argv)
{
    int             c,
                    verbose = 0,
		    errflg = 0;


	char *dbinname=malloc(1024);
	char *dboutname=malloc(1024);
	Point *poly;
	double lat,lon;
	char *subset_expr=NULL;
	char *name=malloc(100);
	long nregions, nvertices;
	Tbl *sortkeys;
			
	Dbptr dbin,dbout,dbi,dbo,dbs;
	long i,from,to,nv;
	long vertex;
	
    elog_init ( argc, argv ) ; 
    while ((c = getopt (argc, argv, "s:vV")) != -1) {
	switch (c) {

	case 's':
	    subset_expr=optarg;
	    break;
	case 'v':
	    verbose++ ;
	    break;

	case 'V':
	    usage ();
	    break;

	case '?':
	    errflg++;
	    break ;
	}
    }

    if ((errflg) || argc < 3)
	usage ();

	dbinname = argv[optind++];
	dboutname= argv[optind++];

	if (dbopen(dbinname,"r",&dbin)) {
		elog_die(1,"cannot open database %s",dbinname);
	}
	dbi=dblookup(dbin,0,"polygon",0,0);
	if (subset_expr) {
		dbi=dbsubset(dbi,subset_expr,NULL);
	}

	

	sortkeys=newtbl(1);
	pushtbl(sortkeys,"pname");
	
	dbs=dbsort(dbi,sortkeys,0,"sorted");
	dbquery(dbs,dbRECORD_COUNT,&nregions);
	if (nregions <1) {
		elog_die(0,"table regions seems to be empty (or not present)");
	}
	
	if (verbose) elog_notify(0,"creating database descriptor %s",dboutname);
	
	if (dbcreate(dboutname,"places1.2",0,0,0)) {
		elog_die(1,"cannot create database %s",dboutname);
	}
	dbopen(dboutname,"r+",&dbout);
	dbo=dblookup(dbout,0,"regions",0,0);
	
	for (i=0; i< nregions; i++) {
		dbs.record=i;
		dbgetv(dbs,0,"pname",name,NULL );
		nvertices=readPolygon(dbs,&poly);
		for (nv=0;nv < nvertices;nv++) {
			lat=poly[nv].lat;
			lon=poly[nv].lon;
			dbaddv(dbo,0,
					"regname",name,
					"vertex",nv,
					"lat",lat,"lon",lon,
					NULL );
		}
		free(poly);
	}
	dbclose(dbo);
	/*
				*/
    return 0;
}
コード例 #24
0
long save_origin(Dbptr dbi, long is, long ie, int depth_fixed,
	Hypocenter h, Dbptr dbo)
{
	long orid;

	/* These are parameters copied from input db -- names = css names */
	long evid;
	long jdate;
	long grn;
	long srn;
	char etype[8];
	double mb;
	long mbid;
	double ms;
	long msid;
	double ml;
	long mlid;
	/* These obtained from this solution */

	long nass;
	long ndef;
	char dtype[2];
	char algorithm[16]="genloc-nlls";
	char auth[20];
	double lddate;
	

	/* these are intentionally left null: ndp,depdp,commid*/


	/* set but obtained directly from hypo structure 
	lat, lon, depth, time */

	dbo = dblookup(dbo,0,"origin",0,0);
	
	/* Grab what we need from the input db for copying.  Note
	that because the joins used here, each of the input db
	records have the same origin entries.  Thus, we just fetch
	stuff from row is.  */
	dbi.record = is;
	if( dbgetv(dbi,0,
		"origin.evid", &evid,
		"origin.jdate", &jdate,
		"origin.grn", &grn,
		"origin.srn", &srn,
		"origin.etype", etype,
		"origin.mb", &mb,
		"origin.mbid", &mbid,
		"origin.ms", &ms,
		"origin.msid", &msid,
		"origin.ml", &ml,
		"origin.mlid", &mlid,
				NULL) == dbINVALID)
	{
		elog_die(1,"save_origin: dbgetv error reading origin fields of input view at record %ld\n",
				is);
	}
	nass = ie - is;
	/* ndef is potentially wrong by this calculation.  We set ndef 
	from the number of degrees of freedom in the solution + npar
	where npar is 3 when depth is fixed and 4 otherwise.  This does
	not match the real definition of ndef, especially when array 
	data are used because each arrival then then has 3 data points
	used to constrain the solution.  These is either an error in 
	my (glp) understanding of the css3.0 schema or a flaw in its 
	design.*/
	if(depth_fixed)
	{
		ndef = h.degrees_of_freedom + 3;
		strcpy(dtype,"r");
	}
	else
	{
		ndef = h.degrees_of_freedom + 4;
		strcpy(dtype,"f");
	}
	my_username(auth);
	lddate = std_now();
	orid = dbnextid(dbo,"orid");
	if(dbaddv(dbo,0,
                "lat",h.lat,
                "lon",h.lon,
                "depth",h.z,
                "time",h.time,
                "orid",orid,
                "evid",evid,
                "jdate",jdate,
                "nass",nass,
                "ndef",ndef,
                "grn",grn,
                "srn",srn,
                "etype",etype,
                "dtype",dtype,
                "mb",mb,
                "mbid",mbid,
                "ms",ms,
                "msid",msid,
                "ml",ml,
                "mlid",mlid,
                "algorithm",algorithm,
                "auth",auth,
                "lddate",lddate,
			NULL) == dbINVALID)
	{
		elog_die(1,"save_origin: dbaddv error writing orid %ld\n",
				orid);
	}
	return(orid);
}
コード例 #25
0
int main(int argc, char **argv)
{
	char *dbin;  /* Input db name */
	char *dbout;  /* output db name */
	Dbptr db;  /* input db pointer */
	Dbptr dbo;  /* base output db pointer */
	Dbptr dbv;  /* set to view formed by join */
	char *pfin=NULL;  /* input parameter file */
	char *sift_exp;  /* sift expression for subset */
	int sift = 0;  /* default is no sift.  */
	Tbl *sortkeys;
	Tbl *joinkey1, *joinkey2;
	/*Pointers to views returned by dbgroup (keyed to origin and event
	respectively */
	Dbptr dborigin_group;
	Tbl *origin_group;  /* relation keys used in grouping*/
	long nevents;
	/* db row variables */
	long evid;
	long nrows, nrows_raw;

	int useold=0;
	Pf *pf;
	Tbl *ta,*tu;
	Tbl *reason_converged, *residual;
	Location_options o;
	Arr *arr_phase;
	int i;
	Tbl *converge_history;

	Hypocenter h0;
	Hypocenter *hypos;
	long niterations;

	char *vmodel;

	int ret_code;  /* ggnloc return code */
	double **C;   /* covariance matrix*/
	float emodel[4];  

	/* entries for S-P feature */
	long nbcs;
	Arr *badclocks;
	/* need global setting of this to handle fixed depth solutions*/
	int global_fix_depth;

	C=dmatrix(0,3,0,3);

	if(argc < 3) usage();
	dbin = argv[1];
	dbout = argv[2];
	for(i=3;i<argc;++i)
	{
		if(!strcmp(argv[i],"-pf"))
		{
			++i;
			if(i>=argc) usage();
			pfin = argv[i];
		}
		else if(!strcmp(argv[i],"-sift"))
		{
			++i;
			if(i>=argc) usage();
			sift_exp = argv[i];
			sift = 1;
		}
		else if(!strcmp(argv[i],"-useold"))
			useold = 1;
		else
			usage();
	}
	/* set default this way*/
	if(pfin == NULL) pfin = strdup("relocate");


	/* Initialize the error log and write a version notice */
	elog_init (argc, argv) ;
	cbanner("Version $Revision$ $Date$\n",
			"relocate inputdb outputdb [-pf pf -sift expression -useold]\n",
			"Gary Pavlis",
                        "Indiana University",
                        "*****@*****.**");

	/* Alway join assoc, arrival, and site.  We join site 
	to make sure station table is properly dynamic to account for
	time changes.  With this setup, the stations can even move
	around and this should still work.*/


	if(dbopen(dbin,"r",&db) == dbINVALID) 
		elog_die(1,"Unable to open input database %s\n",dbin);
	if(dbopen(dbout,"r+",&dbo) == dbINVALID) 
		elog_die(1,"Unable to open output database %s\n",dbout);

	dbv = dbjoin ( dblookup(db,0,"event",0,0),
		dblookup(db,0,"origin",0,0),
		0,0,0,0,0);
	if(dbv.table == dbINVALID)
		elog_die(1,"event->origin join failed\n");
	dbv = dbjoin ( dbv, dblookup(db,0,"assoc",0,0),
			0,0,0,0,0);
	if(dbv.table == dbINVALID)
		elog_die(1,"event->origin->assoc join failed\n");
	dbv = dbjoin ( dbv, dblookup(db,0,"arrival",0,0),
			0,0,0,0,0);
	if(dbv.table == dbINVALID)
		elog_die(1,"event->origin->assoc->arrival join failed\n");
	/* We will explicitly set the keys for this join because it
	was found to fail sometimes */
	joinkey1 = newtbl(0);
	joinkey2 = newtbl(0);
	pushtbl(joinkey1,"arrival.sta");
	pushtbl(joinkey1,"arrival.time");
	pushtbl(joinkey2,"sta");
	pushtbl(joinkey2,"ondate::offdate");
	dbv = dbjoin ( dbv, dblookup(db,0,"site",0,0),
			&joinkey1,&joinkey2,0,0,0);
	if(dbv.table == dbINVALID)
		elog_die(1,"event->origin->assoc->arrival->site join failed\n");

	/* Subset using sift_key if requested */
	if(sift)
	{
		dbv = dbsubset(dbv,sift_exp,0);
		if(dbv.record == dbINVALID)
			elog_die(1,"dbsubset of %s with expression %s failed\n",
				dbin, sift_exp);
	}
	/* This keeps only the prefered origin records intact */
	dbv = dbsubset(dbv,"orid == prefor", 0);
	if(dbv.record == dbINVALID)
			elog_die(1,"Subset to preferred origin records failed\n");

	/* First we have to run a unique key sort in the following order
	to remove redundant picks made on multiple channels.  We will
	issue a warning if the record count changes. */
	dbquery(dbv, dbRECORD_COUNT, &nrows_raw);
	sortkeys = newtbl(0);
	pushtbl(sortkeys,"evid");
	pushtbl(sortkeys,"sta");
	pushtbl(sortkeys,"phase");
	dbv = dbsort(dbv,sortkeys,UNIQUE,0);
	dbquery(dbv, dbRECORD_COUNT, &nrows);
	if(nrows != nrows_raw)
		elog_complain(0,"Input database has duplicate picks of one or more phases on multiple channels\n\
Which picks will be used here is unpredictable\n\
%ld total picks, %ld unique\nContinuing\n", nrows_raw, nrows);

	/* This sort is the required one for the grouping that follows*/

	sortkeys = newtbl(3);
	pushtbl(sortkeys,"evid");
	pushtbl(sortkeys,"orid");
	pushtbl(sortkeys,"arrival.time");
	dbv = dbsort(dbv,sortkeys,0,0);
	if(dbv.record == dbINVALID)
		elog_die(1,"dbsort on evid,orid,arrival.time failed\n");

	/* Set up grouping by events */
	origin_group = newtbl(0);
	pushtbl(origin_group, "evid");
	dborigin_group = dbgroup(dbv, origin_group, "origin_group",1);
	if(dborigin_group.record == dbINVALID)
		elog_die(1,"dbgroup by origin failed\n");

	dbquery(dborigin_group,dbRECORD_COUNT,&nevents);
	elog_notify(0,"Attempting to relocate %ld events in subsetted database\n",
		nevents);
	

	/* DB is now set up correctly, now we turn to the parameter files */
	i = pfread(pfin,&pf);
	if(i != 0) elog_die(1,"Pfread error\n");

	o = parse_options_pf (pf);
	global_fix_depth=o.fix[2];
 	arr_phase = parse_phase_parameter_file(pf);
	vmodel = pfget_string(pf,"velocity_model_name");

	/* set up minus phase for bad clock problems */
	badclocks = newarr(0);
	if(db_badclock_definition(db,pf,badclocks))
		elog_complain(0,"Warning:  problems in database definitions of bad clock time periods\n");
	pfget_badclocks(pf,badclocks);
	nbcs = cntarr(badclocks);
	if(nbcs>0) fprintf(stdout,"relocate:  bad clock feature enabled\n\n");
        /* Change by JN to output evid and orid. */
        /* fprintf(stdout,"lat lon depth time rms wrms interquartile ndata ndgf iterations\n"); */
	fprintf(stdout,"evid orid lat lon depth time rms wrms interquartile ndata ndgf iterations\n");

	/* Main loop.  We utilize the group views and loop through by 
	events */
	for(dborigin_group.record=0;
		dborigin_group.record< nevents;++dborigin_group.record)
	{
		Dbptr db_bundle;  /* db pointer returned from bundle field 
				of dborigin_group for current event */
		Arr *station_table;
		Arr *array_table;
		long is, ie; 
		long orid;  /* orid assigned relocated event in output db */

		if(dbgetv(dborigin_group,0,"evid", &evid,
			"bundle", &db_bundle,NULL ) == dbINVALID)
			elog_complain(1,"dbgetv error for row %ld of event group\n",
				dborigin_group.record);
		dbget_range(db_bundle,&is,&ie);

		station_table = dbload_station_table(dbv,
						is,ie,pf);
		array_table = dbload_array_table(dbv,
						is,ie,pf);
		ta = dbload_arrival_table(dbv,
				is,ie,station_table, arr_phase);


		tu = dbload_slowness_table(dbv,
				is,ie,array_table, arr_phase);
		/* this actually sets up the minus phase feature for bad clocks*/
		if(nbcs)
		{
			if(minus_phases_arrival_edit(ta,arr_phase,badclocks))
				elog_complain(0,"Warning(relocate):  problems in minus_phase_arrival_edit function\n");
		}
		if(useold)
		{
			char dtype[2];
			h0 = db_load_initial(dbv,is);
			/* keep fixed depth if done before.  
			setting dbv.record here is a bit of
			a potential maintenance problem */
			dbv.record=is;
			dbgetv(dbv,0,"dtype",dtype,NULL );
			if( (!strcmp(dtype,"g")) || (!strcmp(dtype,"r")) )
				o.fix[2]=1;
			
		}
		else
			h0 = initial_locate(ta, tu, o, pf);

		ret_code = ggnloc(h0,ta,tu,o,
			&converge_history,&reason_converged,&residual);
			
		if(ret_code < 0)
		{
			elog_complain(1,"ggnloc failed to produce a solution\n");
		}
		else 
		{
			if(ret_code > 0)
			    elog_complain(1,"%d travel time calculator failures in ggnloc\nSolution ok\n",
				ret_code);
			
			niterations = maxtbl(converge_history);
			hypos = (Hypocenter *)gettbl(converge_history,
								niterations-1);
			predicted_errors(*hypos,ta,tu,o,C,emodel);

                        /* Next 3 calls changed by JN to output evid, orid and number_data */
			orid = save_origin(dbv,is,ie,o.fix[3],*hypos,dbo);
			evid = save_event(dbv,is,ie,orid,dbo);

			fprintf(stdout,"%ld %ld %lf %lf %lf %lf %g %g %g %d %d %ld\n",
					evid,
					orid,
					hypos->lat,hypos->lon,hypos->z,hypos->time,
					hypos->rms_raw, hypos->rms_weighted,
					hypos->interquartile,
					hypos->number_data,
					hypos->degrees_of_freedom,
					niterations);
	
			save_origerr(orid,*hypos,C,dbo);
			save_assoc(dbv,is,ie,orid,vmodel,residual,*hypos,dbo);
			/* These save genloc add on tables */
			save_emodel(orid,emodel,dbo);
			save_predarr(dbo,ta,tu,*hypos,orid,vmodel);
		}
		o.fix[2]=global_fix_depth;
		if(maxtbl(converge_history)>0)freetbl(converge_history,free);
		if(maxtbl(reason_converged)>0)freetbl(reason_converged,free);
		if(maxtbl(residual)>0)freetbl(residual,free);
		destroy_data_tables(tu, ta);
		destroy_network_geometry_tables(station_table,array_table);
	}
	return(0);
}
コード例 #26
0
void save_assoc(Dbptr dbi, long is, long ie, long orid, char *vmodel,
	Tbl *residual,Hypocenter h, Dbptr dbo)
{
	/* These fields are copied from input assoc table */
	long arid;
	char sta[8];
	char phase[10];
	double belief;
	/* These fields are set here */
	double delta;
	double seaz;
	double esaz;
	double timeres;
	double azres;
	double slores;
	double lddate;
	double wgt;
	char timedef[2],slodef[2], azdef[2];
	
	/* intentionally ignored:  emares, commid */


	/* passed through arg list;  orid*/

	/* We use this to produce a keyed arr list of the residual 
	list passed into here as a Tbl */

	Arr *residual_array;
	long i;
	char key[40]; 

	double r, w, reswt,uxresid, uyresid;
	double stalat, stalon; 
	double ux, uy, azimuth;
	double u,phi;  /* polar form of measured slowness vector */
	double duphi;

	dbo = dblookup(dbo,0,"assoc",0,0);
	lddate = std_now();

	/* We build an associate array for the residual tbl keying
	each entry with a sta/phase/type key  where type is 
	set in ggnloc as time, ux, or uy.  This complication is
	needed to sort out array residuals. */
	residual_array = newarr(0);
	for(i=0;i<maxtbl(residual);++i)
	{
		char *s;
		char keysta[10], keyphase[10], keytype[5];
		s = (char *)gettbl(residual,i);
		sscanf(s,"%s %s %s",keysta,keyphase,keytype);
		/* handle S-P case by having the same residual mapped
		to each half of - phase pair */
		if(strchr(keyphase,'-'))
		{
			char *phase1,*phase2;
			/* algorithm to split phase names cloned from dbgenloc */
			phase1 = strdup(keyphase);
			phase2= strchr(phase1,'-');
			*phase2 = '\0';
			++phase2;
			sprintf(key,"%s %s %s",keysta,phase1,keytype);
			setarr(residual_array,key,s);
                        sprintf(key,"%s %s %s",keysta,phase2,keytype);
                        setarr(residual_array,key,s);
			free(phase1);
		}
		else
		{
			/* normal phases are one to one */
			sprintf(key,"%s %s %s",keysta,keyphase,keytype);
			setarr(residual_array,key,s);
		}
	}
		

	for(dbi.record=is;dbi.record < ie;++dbi.record)
	{
		char *time_residual_record;
		char *ux_residual_record,*uy_residual_record;
		if( dbgetv(dbi,0,
      	    		"assoc.arid",&arid,
          		"assoc.sta",sta,
          		"assoc.phase",phase,
          		"assoc.belief",&belief,
				NULL) == dbINVALID)
		{
		  elog_die(1,"save_assoc: dbgetv error reading assoc fields of input view at record %ld\n",
				dbi.record);
		}
		if( dbgetv(dbi,0,
			"site.lat",&stalat,
			"site.lon",&stalon,
				NULL) == dbINVALID)
		{
		  elog_die(1,"save_assoc: dbgetv error reading site fields of input view at record %ld\n",
				dbi.record);
		}
		/* Find the time residual record for this arrival */
		sprintf(key,"%s %s time",sta,phase);
		time_residual_record = (char *)getarr(residual_array,key);
		if(time_residual_record == NULL)
		{
			elog_complain(1,"save_assoc:  getarr mismatch for key %s\nCannot set residual\n",key);
			timeres = TIMENULL;
			wgt = 0.0;
			strcpy(timedef,"n");
		}
		else
		{
                        /* Changed by JN to avoid gcc warning */
			/* sscanf(time_residual_record,"%*s%*s%*s%*lg%lg%lg%lg", */
			sscanf(time_residual_record,"%*s%*s%*s%*g%lg%lg%lg",
				&r,&w,&reswt);
			timeres = r;
			wgt = w*reswt;
			strcpy(timedef,"d");
		}

		sprintf(key,"%s %s ux",sta,phase);		
		ux_residual_record = (char *)getarr(residual_array,key);
		sprintf(key,"%s %s uy",sta,phase);
		uy_residual_record = (char *)getarr(residual_array,key);
                /* Corrected by JN
		if( (ux_residual_record == NULL) 
			|| (ux_residual_record == NULL))
                */
		if( (ux_residual_record == NULL) 
			|| (uy_residual_record == NULL))
		{
			/* This trick is not documented.  By setting 
			the record filed to dbNULL, and then calling dbgetv
			each of the fields will be set to their NULL value */
			dbo.record = dbNULL;
			dbgetv(dbo,0,"azres",&azres,"slores",&slores,NULL );
			strcpy(azdef,"n");
			strcpy(slodef,"n");
		}
		else
		{
		/* This gets nasty because we have to convert to polar 
		coordinates from ux, uy components */
			sscanf(ux_residual_record,"%*s%*s%*s%*g%lg",&uxresid);

			sscanf(uy_residual_record,"%*s%*s%*s%*g%lg",&uyresid);
		/* We fetch the measured slowness vector to convert */
			if( dbgetv(dbi,0,
				"arrival.slow",&u,
				"arrival.azimuth",&phi,
					NULL) == dbINVALID)
			{
		  	  elog_die(1,"save_assoc: dbgetv error reading arrival fields of input view at record %ld\n",
				dbi.record);
			}
			/* css stores slowness in s/deg, but we use
			s/km internally here so we have to convert */

			slores = sqrt(uxresid*uxresid+uyresid*uyresid);
			slores *= KMPERDEG;

			/* this is the azimuth term */
			u /= KMPERDEG;
			duphi = ux*cos(rad(azimuth)) - uy*sin(rad(azimuth));
			duphi /= u;
			azres = deg(duphi);
			strcpy(azdef,"d");
			strcpy(slodef,"d");

		}
		dist(rad(h.lat),rad(h.lon),rad(stalat),rad(stalon),
				&delta,&esaz);
		dist(rad(stalat),rad(stalon),rad(h.lat),rad(h.lon),
				&delta,&seaz);
		delta = deg(delta);
		seaz = deg(seaz);
		esaz = deg(esaz);
			
		if(dbaddv(dbo,0,
                        "arid",arid,
                        "orid",orid,
                        "sta",sta,
                        "phase",phase,
                        "belief",belief,
                        "delta",delta,
                        "seaz",seaz,
                        "esaz",esaz,
                        "timeres",timeres,
                        "timedef",timedef,
                        "azres",azres,
                        "azdef",azdef,
                        "slores",slores,
                        "slodef",slodef,
                        "wgt",wgt,
                        "vmodel",vmodel,
                        "lddate",lddate,
			NULL ) == dbINVALID)
		{
			elog_die(1,"save_assoc: dbaddv error writing assoc record for arid %ld\n",
				arid);
		}
	}
	freearr(residual_array,0);
}
コード例 #27
0
ファイル: orb2rrdc.c プロジェクト: battistuz/antelope_contrib
int
main( int argc, char **argv )
{
	int	c;
	int	errflag = 0;
	int	orb;
	int	stop = 0;
	long	nrecs;
	char	*match = ".*/pf/st";
	char	*from = 0;
	char	*statefile = 0;
	char	*pfname = "orb2rrdc";
	char	*orbname;
	char	*dbcache;
	char	*rrdtool;
	char	command[STRSZ];
	char	net[STRSZ];
	char	sta[STRSZ];
	char	rrdvar[STRSZ];
	char	key[STRSZ];
	char	path[FILENAME_MAX];
	Dbptr	db;
	Dbptr	dbt;
	Pf	*pf;
	char	*Default_network;
	Tbl	*dlslines;
	Arr	*Dls_vars_dsparams;
	Arr	*Dls_vars_rras;
	Tbl	*Dls_vars_keys;
	char	*line;
	char	*dls_var;
	char	*dsparams;
	Tbl	*rras;
	int	i;
	int	j;
	OrbreapThr *ort;
	int	pktid;
	char	srcname[ORBSRCNAME_SIZE];
	double	time = 0;
	char	*packet = 0;
	int	nbytes = 0;
	int	bufsize = 0;
	Packet	*pkt = 0;
	int	rc;
	char	*s;
	Pf	*dlspf;
	Tbl	*dlspfkeys;
	char	*element;
	Tbl	*parts;
	double	val;
	Pf	*pfval = 0;

	elog_init( argc, argv );

	while( ( c = getopt( argc, argv, "vVd:s:p:m:f:" ) ) != -1 ) {

		switch( c ) {

		case 'd':
			CacheDaemon = optarg;
			break;

		case 'f':
			from = optarg;
			break;

		case 'm':
			match = optarg;
			break;

		case 'p': 
			pfname = optarg;
			break;

		case 's':
			statefile = optarg;
			break;
			
		case 'v':
			Verbose++;
			break;

		case 'V':
			VeryVerbose++;
			Verbose++;
			break;
		
		default:
			elog_complain( 0, "Unknown option '%c'\n", c );
			errflag++;
			break;
		}
	}

	if( errflag || argc - optind != 2 ) {

		usage();
	}

	if( Verbose ) {

		elog_notify( 0, "Starting at %s (%s $Revision$ $Date$)\n", 
				zepoch2str( str2epoch( "now" ), "%D %T %Z", "" ),
				Program_Name );
	}

	orbname = argv[optind++];
	dbcache = argv[optind++];

	pfread( pfname, &pf );

	rrdtool = pfget_string( pf, "rrdtool" );

	if( rrdtool == NULL || ! strcmp( rrdtool, "" ) ) {

		elog_die( 0, "Error: no rrdtool executable name specified in parameter file\n" );

	} else if( ( rrdtool[0] == '/' && ! is_present( rrdtool ) ) || ( rrdtool[0] != '/' && ! datafile( "PATH", rrdtool ) ) ) {

		elog_die( 0, "Error: can't find rrdtool executable by name of '%s' (check PATH environment " 
			"variable, or absolute path name if given)\n", rrdtool );

	} else if( rrdtool[0] == '/' ) {

		sprintf( command, "%s -", rrdtool );

	} else {

		sprintf( command, "rrdtool -" );
	}

	Suppress_egrep = pfget_string( pf, "suppress_egrep" );

	if( Suppress_egrep != NULL && strcmp( Suppress_egrep, "" ) ) {
		
		if( ! datafile( "PATH", "egrep" ) ) {

			elog_complain( 0, "Ignoring suppress_egrep parameter: can't find egrep on path\n" ); 

		} else {

			sprintf( command, "%s 2>&1 | egrep -v '%s'", command, Suppress_egrep );
		}
	}

	if( VeryVerbose ) {

		elog_notify( 0, "Executing command: %s\n", command );
	}

	Rrdfp = popen( command, "w" );

	if( Rrdfp == (FILE *) NULL ) {

		elog_die( 0, "Failed to open socket to rrdtool command\n" );
	}

	orb = orbopen( orbname, "r&" );

	if( orb < 0 ) {

		elog_die( 0, "Failed to open orb '%s' for reading. Bye.\n", orbname );
	}

	orbselect( orb, match );

	if( from != NULL && statefile == NULL ) {

		pktid = orbposition( orb, from );

		if( Verbose ) {

			elog_notify( 0, "Positioned to packet %d\n", pktid );
		}

	} else if( from != NULL ) {

		elog_complain( 0, "Ignoring -f in favor of existing state file\n" );
	}

	if( statefile != NULL ) {

		stop = 0;

		exhume( statefile, &stop, 15, 0 );

		orbresurrect( orb, &pktid, &time );

		if( Verbose ) {

			elog_notify( 0, "Resurrecting state to pktid %d, time %s\n",
				pktid, s = strtime( time ) );

			free( s );
		}

		orbseek( orb, pktid );
	}

	dbopen( dbcache, "r+", &db );

	if( db.database < 0 ) {
		
		elog_die( 0, "Failed to open cache database '%s'. Bye.\n", dbcache );

	} else {
		
		db = dblookup( db, "", "rrdcache", "", "" );

		if( db.table < 0 ) {

			elog_die( 0, "Failed to lookup 'rrdcache' table in '%s'. Bye.\n", dbcache );
		}
	}

	dbcrunch( db );

	dbt = dbsubset( db, "endtime == NULL", NULL );

	Rrd_files = newarr( 0 );

	dbquery( dbt, dbRECORD_COUNT, &nrecs );

	for( dbt.record = 0; dbt.record < nrecs; dbt.record++ ) {

		dbgetv( dbt, 0, "net", &net, "sta", &sta, "rrdvar", &rrdvar, NULL );

		dbfilename( dbt, (char *) &path );

		sprintf( key, "%s:%s:%s", net, sta, rrdvar );

		if( ! is_present( path ) ) {
			
			elog_complain( 0, "WARNING: rrd file '%s', listed in database, does not exist. "
				"Removing database entry.\n", path );

			dbmark( dbt );

		} else {

			setarr( Rrd_files, key, strdup( path ) );

			if( VeryVerbose ) {

				elog_notify( 0, "Re-using rrd file '%s' for '%s'\n", path, key );
			}
		}
	}

	Rrdfile_pattern = pfget_string( pf, "rrdfile_pattern" );
	Status_stepsize_sec = pfget_double( pf, "status_stepsize_sec" );
	Default_network = pfget_string( pf, "default_network" );
	dlslines = pfget_tbl( pf, "dls_vars" );

	Dls_vars_dsparams = newarr( 0 );
	Dls_vars_rras = newarr( 0 );

	for( i = 0; i < maxtbl( dlslines ); i++ ) {
		
		line = gettbl( dlslines, i );
		
		strtr( line, "\t", " " );
		rras = split( line, ' ' );

		dls_var = shifttbl( rras );
		dsparams = shifttbl( rras );

		setarr( Dls_vars_dsparams, dls_var, dsparams );
		setarr( Dls_vars_rras, dls_var, rras );
	}

	ort = orbreapthr_new( orb, -1., 0 );

	for( ; stop == 0; ) {

		orbreapthr_get( ort, &pktid, srcname, &time, &packet, &nbytes, &bufsize );

		if( statefile ) {

			rc = bury();

			if( rc < 0 ) {

				elog_complain( 0, "Unexpected failure of bury command! " 
					"(are there two orb2rrdc's running with the same state" 
					"file?)\n" );

				clear_register( 1 );
			}
		}

		rc = unstuffPkt( srcname, time, packet, nbytes, &pkt );

		if( rc == Pkt_pf ) {

			if( VeryVerbose ) {

				/* Parameter files generally too big for elog */

				fprintf( stderr, "Received a parameter-file '%s' at %s\n%s\n\n", 
						srcname, 
						s = strtime( time ), 
						pf2string( pkt->pf ) );

				free( s );

			} else if( Verbose ) {

				elog_notify( 0, "Received a parameter-file '%s' at %s\n", 
						srcname, s = strtime( time ) );

				free( s );
			}

			pfmorph( pkt->pf );

			if( VeryVerbose ) {

				fprintf( stderr, "Morphed parameter-file '%s' to interpret 'opt':\n%s\n\n", 
						srcname, 
						pf2string( pkt->pf ) );
			}

			pfget( pkt->pf, "dls", (void **) &dlspf );

			dlspfkeys = pfkeys( dlspf );
			Dls_vars_keys = keysarr( Dls_vars_dsparams );

			for( i = 0; i < maxtbl( dlspfkeys ); i++ ) {
			   
			   element = gettbl( dlspfkeys, i );

			   if( strcontains( element, "_", 0, 0, 0 ) ) {

				parts = split( (s = strdup( element )), '_' );

				sprintf( net, "%s", (char *) gettbl( parts, 0 ) );
				sprintf( sta, "%s", (char *) gettbl( parts, 1 ) );

				free( s );
				freetbl( parts, 0 );

			   } else {

				sprintf( net, "%s", Default_network );

				sprintf( sta, "%s", element );
			   }

			   for( j = 0; j < maxtbl( Dls_vars_keys ); j++ ) {

			   	dls_var = gettbl( Dls_vars_keys, j );

				sprintf( key, "%s{%s}", element, dls_var );

				if( pfresolve( dlspf, key, 0, &pfval ) < 0 ) {

					elog_complain( 0, "Unable to extract variable '%s' "
						"(not present or wrong type) from element '%s' "
						"in packet from '%s', timestamped '%s'; Skipping\n",
						key, element, srcname, s = strtime( time ) );

					free( s );

					pfval = 0;

					continue;

				} else if( pfval != (Pf *) NULL &&
					   pfval->value.s != (char *) NULL &&
					   ! strcmp( pfval->value.s, "-" ) ) {

					if( VeryVerbose ) {

						elog_notify( 0, "Non-floating point value '-' in variable '%s', "
							"in packet from '%s', timestamped '%s'; Skipping data point\n",
							key, srcname, s = strtime( time ) );

						free( s );
					}

					continue;

				} else {

					val = pfget_double( dlspf, key );
				}

				archive_dlsvar( db, net, sta, dls_var, 
						(char *) getarr( Dls_vars_dsparams, dls_var ),
						(Tbl *) getarr( Dls_vars_rras, dls_var ),
						time, val );
			   }

			}

			freetbl( dlspfkeys, 0 );
			freetbl( Dls_vars_keys, 0 );

		} else if( rc == Pkt_stash ) {

			; /* Do nothing */

		} else {

			if( Verbose ) {

				elog_notify( 0, "Received a packet that's not a parameter file " 
					"(type '%d' from unstuffPkt); skipping\n", rc );
			}
		}
	}
}
コード例 #28
0
int main(int argc, char **argv)
{
	Dbptr db;
	char *dbname;
	Pf *pf;
	char *progname="Xphase2db";
	char line[1024];
	int year,jday,hour,min;
	double sec;
	double twin,deltim;
	int other1,other2,pol;
	char fname[512];
	char tstring[100];
	double time;
	char *seperators;  /* holds seperators passed to strtok*/
	int ista,ichan;  /* token positions of station and channel 
			strings */
	char *phase;
	char *fm,*up="uc",*down="dr";
	char *token;
	char *sta,*chan;
	int i,j;


	if(argc < 2) die(0,"usage:  %s dbout [-phase x]\n",progname);
	if(argc == 4)
	{
		if(!strcmp(argv[2],"-phase"))
			die(0,"usage:  %s dbout [-phase x]\n",progname);
		phase = argv[3];
	}
	else
		phase = strdup("P");
	dbname = argv[1];
	if(pfread(progname,&pf))
		die(0,"Failure reading parameter file %s.pf\n",
				progname);
	if(dbopen(dbname,"r+",&db) == dbINVALID)
		die(0,"dbopen failure for database %s\n",dbname);
	db = dblookup(db,0,"arrival",0,0);
	seperators = pfget_string(pf,"seperators");
	if(seperators == NULL) die(0,"required parameter separators no in parameter file\n");
	ista = pfget_int(pf,"station_token_number");
	ichan = pfget_int(pf,"chan_token_number");

	while( fgets(line,1024,stdin) != NULL)
	{
		sscanf(line,"%d%d%d%d%lf%d%lf%lf%d%d%s",
			&year,&jday,&hour,&min,
			&sec,&other1,&twin,&deltim,&other2,&pol,fname);

		if(pol<0)
			fm = down;
		else
			fm = up;
		sprintf(tstring,"%4d%3d:%2d:%2d:%7.4lf",year,jday,hour,min,sec);
		/* There is a way to avoid this in formats for sprintf, but
		I'll be damned if I can find it.  Easier to do by brute force*/
		for(j=0;j<strlen(tstring);++j) 
			if(tstring[j]==' ')tstring[j]='0';
		time = str2epoch(tstring);
		token = strtok(fname,seperators);
		i=1;
		while(token != NULL)
		{
			if(i == ista)  sta = token;
			if(i == ichan) chan = token;
			++i;
			token = strtok(NULL,seperators);
		}
		dbaddv(db,0,
			"sta",sta,
			"chan",chan,
			"time",time,
			"deltim",deltim,
			"iphase",phase,
			"fm",fm,
			"auth","Xphase",
			0);
	}
}
コード例 #29
0
int
main (int argc, char **argv)
{
    int             c,
                    verbose = 0,
		    errflg = 0;


	char *dbinname=malloc(1024);
	char *dboutname=malloc(1024);
	Point *poly;
	double lat,lon;
	char *auth=strdup("regions2polygon");
	char *ptype= strdup("rp");
	char *dir=strdup(".");
	char *dfile=strdup("polygons");
	int ftype=polyFLOAT;
	char *name;
	int nregions, nvertices;
	Tbl *sortkeys, *groupkeys;
			
	Dbptr dbin,dbout,dbi,dbo,dbg,dbb;
	int i,from,to,nv;
	int vertex;
	
    elog_init ( argc, argv ) ; 
    while ((c = getopt (argc, argv, "vV")) != -1) {
	switch (c) {

	case 'v':
	    verbose++ ;
	    break;

	case 'V':
	    usage ();
	    break;

	case '?':
	    errflg++;
	    break ;
	}
    }

    if ((errflg) || argc < 3)
	usage ();

	dbinname = argv[optind++];
	dboutname= argv[optind++];

	if (dbopen(dbinname,"r",&dbin)) {
		elog_die(1,"cannot open database %s",dbinname);
	}
	dbi=dblookup(dbin,0,"regions",0,0);

	

	sortkeys=newtbl(2);
	pushtbl(sortkeys,"regname");
	pushtbl(sortkeys,"vertex");
	groupkeys=newtbl(1);
	pushtbl(groupkeys,"regname");
	
	dbi=dbsort(dbi,sortkeys,0,"regions.sorted");
	dbg=dbgroup(dbi,groupkeys,0,0);
	dbquery(dbg,dbRECORD_COUNT,&nregions);
	if (nregions <1) {
		elog_die(0,"table regions seems to be empty (or not present)");
	}
	
	if (verbose) elog_notify(0,"creating database descriptor %s",dboutname);
	
	if (dbcreate(dboutname,"polygon1.2",0,0,0)) {
		elog_die(1,"cannot create database %s",dboutname);
	}
	dbopen(dboutname,"r+",&dbout);
	dbo=dblookup(dbout,0,"polygon",0,0);
	
	for (i=0; i< nregions; i++) {
		dbg.record=i;
		dbgetv(dbg,0,"regname",name,"bundle",&dbb,0);
		dbget_range(dbb,&from,&to);
		nvertices= to - from;
		if (verbose) elog_notify(0,"%s (%i nvertices)",name,nvertices);
		poly=malloc(2 * nvertices * sizeof(double));
		nv=0;

		for (dbi.record=from; dbi.record<to; dbi.record++) {
			dbgetv(dbi,0,
					"regname",name,
					"vertex",&vertex,
					"lat",&lat,"lon",&lon,
					0);
			poly[nv].lat=lat;
			poly[nv].lon=lon;
			nv++;
		}
		writePolygonData(dbo,poly,nv,name,1,0,ptype,auth,dir,dfile,ftype);
		free(poly);
	}
	/*
				*/
    return 0;
}
コード例 #30
0
ファイル: dsseis.c プロジェクト: ittaikurzon/antelope_contrib
int grdb_sc_loadcss (Dbptr dbin, char *net_expr, char *sta_expr,
        char *chan_expr, double tstart, double tend, 
        int coords, int ir, int orient, Dbptr *dbscgr, Dbptr *dbsc)
{
	Dbptr dbout, db, dbout2;
	char string[1024];
	char string2[1024];
	char sta_wfdisc[32], chan_wfdisc[32];
	int i, j, n, sensor=0, ok;
	Tbl *pat1, *pat2;
	Tbl *sortfields, *groupfields;
	FILE *file;
	Response *resp;
	int is_view=0;
	Dbptr db_to_clear;

	/* Subset the wfdisc by station-channel-time sifters. */

	dbout = dblookup (dbin, 0, "wfdisc", 0, 0);
	strcpy (string, "");
	if (sta_expr) {
		strcpy (string, "( ");
        	sprintf (string2, "sta =~ /%s/", sta_expr);
        	strcat (string, string2);
	}
	if (chan_expr) {
		if (string[0]) strcat (string, " && ");
		else strcpy (string, "( ");
        	sprintf (string2, "chan =~ /%s/", chan_expr);
        	strcat (string, string2);
	}
	if (tstart != 0.0 || tend != 0.0) {
		if (string[0]) strcat (string, " && ");
		else strcpy (string, "( ");
        	sprintf (string2, "(time < %.5f && endtime > %.5f)", tend, tstart);
        	strcat (string, string2);
	}
	if (string[0]) {
		strcat (string, " )");
		dbout = dbsubset (dbout, string, 0);
		is_view=1;
	}
        dbquery (dbout, dbRECORD_COUNT, &n);
        if (n < 1) {
		register_error (0, "grdb_sc_loadcss: No wfdisc rows to process.\n");
		return (-1);
        }

        /* Make the necessary joins and check for completeness. */

        if (coords) {
        	db = dblookup (dbin, 0, "site", 0, 0);
		if(is_view)db_to_clear=dbout;
        	dbout = dbjoin (dbout, db, 0, 0, 1, 0, 0);
		if(is_view) dbfree(db_to_clear);
		is_view=1;
		
        	dbquery (dbout, dbRECORD_COUNT, &n);
        	if (n < 1) {
			register_error (0, "grdb_sc_loadcss: No data rows to process.\n");
			return (-1);
        	}
        	for (dbout.record=0; dbout.record<n; dbout.record++) {
        		if (dbgetv (dbout, 0, "wfdisc.sta", sta_wfdisc,
        				"wfdisc.chan", chan_wfdisc,
        				"site.sta", string, 0) == dbINVALID) {
			    register_error (0, "grdb_sc_loadcss: dbgetv() error while checking site.\n");
			    return (-1);
			}
        		if (coords > 1 && strcmp(string, sta_wfdisc)) {
        			register_error (0, "grdb_sc_loadcss: Cannot find site parameters for %s %s.\n", 
        									sta_wfdisc, chan_wfdisc);
        			return (-1);
        		}
        	}
        }
        if (ir) {
        	db = dblookup (dbin, 0, "sensor", 0, 0);
		if(is_view)db_to_clear=dbout;
        	dbout = dbjoin (dbout, db, 0, 0, 1, 0, 0);
		if(is_view) dbfree(db_to_clear);
		is_view=1;
        	dbquery (dbout, dbRECORD_COUNT, &n);
        	if (n < 1) {
			register_error (0, "grdb_sc_loadcss: No data rows to process.\n");
			return (-1);
        	}
        	for (dbout.record=0; dbout.record<n; dbout.record++) {
        		if (dbgetv (dbout, 0, "wfdisc.sta", sta_wfdisc,
        				"wfdisc.chan", chan_wfdisc,
        				"sensor.sta", string, 0) == dbINVALID) {
			    register_error (0, "grdb_sc_loadcss: dbgetv() error while checking sensor.\n");
			    return (-1);
			}
        		if (ir > 1 && strcmp(string, sta_wfdisc)) {
        			register_error (0, "grdb_sc_loadcss: Cannot find sensor parameters for %s %s.\n", 
        									sta_wfdisc, chan_wfdisc);
        			return (-1);
        		}
        	}
        	sensor = 1;
		if(is_view)db_to_clear=dbout;
        	db = dblookup (dbin, 0, "instrument", 0, 0);
        	dbout = dbjoin (dbout, db, 0, 0, 1, 0, 0);
		if(is_view) dbfree(db_to_clear);
		is_view=1;
        	dbquery (dbout, dbRECORD_COUNT, &n);
        	if (n < 1) {
			register_error (0, "grdb_sc_loadcss: No data rows to process.\n");
			return (-1);
        	}
        	for (dbout.record=0; dbout.record<n; dbout.record++) {
        		if (dbgetv (dbout, 0, "wfdisc.sta", sta_wfdisc,
        				"wfdisc.chan", chan_wfdisc,
        				"sensor.inid", &j,
        				"instrument.insname", string2,
        				"instrument.inid", &i, 0) == dbINVALID) {
			    register_error (0, "grdb_sc_loadcss: dbgetv() error while checking instrument.\n");
			    return (-1);
			}
        		if (ir > 1 && (i != j)) {
        			register_error (0, "grdb_sc_loadcss: Cannot find instrument parameters for %s %s.\n", 
        									sta_wfdisc, chan_wfdisc);
        			return (-1);
        		}
        		if (i >= 0) {
				if (resp_arr == NULL) {
					resp_arr = newarr (0);
					if (resp_arr == NULL) {
        					register_error (0, "grdb_sc_loadcss: newarr() error.\n");
        					return (-1);
					}
				}
				dbextfile (dbout, "instrument", string);
				resp = (Response *) getarr (resp_arr, string);
				if (resp == NULL) {
					file = fopen (string, "r");
					if (file == NULL) {
						if (ir > 1) {
        						register_error (1, "grdb_sc_loadcss: fopen('%s') error.\n", string);
        						return (-1);
						}
					} else {
						if (read_response (file, &resp)) {
        						register_error (0, "grdb_sc_loadcss: read_response('%s') error.\n", string);
        						return (-1);
						}
						fclose (file);
						resp->insname = strdup(string2);
					}
					setarr (resp_arr, string, resp);
				}
			}
        	}
        }
        if (orient) {
        	ok = 1;
		if(is_view)db_to_clear=dbout;
        	db = dblookup (dbin, 0, "sitechan", 0, 0);
        	dbout2 = dbjoin (dbout, db, 0, 0, 1, 0, 0);
		is_view=1;
        	dbquery (dbout2, dbRECORD_COUNT, &n);
        	if (n < 1) {
        		ok = 0;
        	} else {
        		for (dbout2.record=0; dbout2.record<n; dbout2.record++) {
        			dbgetv (dbout2, 0, "wfdisc.sta", sta_wfdisc,
        				"wfdisc.chan", chan_wfdisc,
        				"sitechan.sta", string, 0);
        			if (strcmp(string, sta_wfdisc)) {
        				ok = 0;
        				break;
        			}
        		}
		}
		if (ok) {
			dbout = dbout2;
			if(is_view) dbfree(db_to_clear);
		} else {
			if (!sensor) {
        			db = dblookup (dbin, 0, "sensor", 0, 0);
				if(is_view)db_to_clear=dbout;
	       			dbout = dbjoin (dbout, db, 0, 0, 1, 0, 0);
				if(is_view) 
				{
					dbfree(dbout2);
					dbfree(db_to_clear);
				}
				is_view=1;
        			dbquery (dbout, dbRECORD_COUNT, &n);
        			if (n < 1) {
					register_error (0, "grdb_sc_loadcss: No data rows to process.\n");
					return (-1);
        			}
        			for (dbout.record=0; dbout.record<n; dbout.record++) {
        				if (dbgetv (dbout, 0, "wfdisc.sta", sta_wfdisc,
        						"wfdisc.chan", chan_wfdisc,
        						"sensor.sta", string, 0) == dbINVALID) {
			    			register_error (0, "grdb_sc_loadcss: dbgetv() error while checking sensor.\n");
			    			return (-1);
					}
        				if (orient > 1 && strcmp(string, sta_wfdisc)) {
        					register_error (0, "grdb_sc_loadcss: Cannot find sensor parameters for %s %s.\n", 
        											sta_wfdisc, chan_wfdisc);
        					return (-1);
        				}
        			}
			}
        		db = dblookup (dbin, 0, "sitechan", 0, 0);
        		pat1 = newtbl(1);
        		if (pat1 == NULL) {
        			register_error (0, "grdb_sc_loadcss: newtbl() error.\n");
        			return (-1);
        		}
        		pat2 = newtbl(1);
        		if (pat2 == NULL) {
        			register_error (0, "grdb_sc_loadcss: newtbl() error.\n");
        			return (-1);
        		}
			if(is_view)db_to_clear=dbout;
        		settbl (pat1, 0, strdup("sensor.chanid"));
        		settbl (pat2, 0, strdup("sitechan.chanid"));
        		dbout = dbjoin (dbout, db, &pat1, &pat2, 1, 0, 0);
			if(is_view) dbfree(db_to_clear);
			is_view=1;
        		freetbl (pat1, free);
        		freetbl (pat2, free);
        		dbquery (dbout, dbRECORD_COUNT, &n);
        		if (n < 1) {
				register_error (0, "grdb_sc_loadcss: No data rows to process.\n");
				return (-1);
        		} else {
        			for (dbout.record=0; dbout.record<n; dbout.record++) {
        				if (dbgetv (dbout, 0, "wfdisc.sta", sta_wfdisc,
        					"wfdisc.chan", chan_wfdisc,
        					"sitechan.sta", string, 0) == dbINVALID) {
			    		   register_error (0, "grdb_sc_loadcss: dbgetv() error while checking sitechan.\n");
			    		   return (-1);
					}
        				if (orient > 1 && strcmp(string, sta_wfdisc)) {
        					register_error (0, "grdb_sc_loadcss: Cannot find sitechan parameters for %s %s.\n", 
        											sta_wfdisc, chan_wfdisc);
        					return (-1);
        				}
        			}
			}
		}
        }

        /* Sort and group the output view. */

	if(is_view)db_to_clear=dbout;
	sortfields = newtbl (3);
	if (sortfields == NULL) {
		register_error (0, "grdb_sc_loadcss: newtbl() error.\n");
		return (-1);
	}
	settbl (sortfields, 0, strdup("wfdisc.sta"));
	settbl (sortfields, 1, strdup("wfdisc.chan"));
	settbl (sortfields, 2, strdup("wfdisc.time"));
        *dbsc = dbsort (dbout, sortfields, 0, 0);
	if(is_view) dbfree(db_to_clear);
	groupfields = newtbl (2);
	if (groupfields == NULL) {
		register_error (0, "grdb_sc_loadcss: newtbl() error.\n");
		return (-1);
	}
	settbl (groupfields, 0, strdup("sta"));
	settbl (groupfields, 1, strdup("chan"));
	*dbscgr = dbgroup (*dbsc, groupfields, 0, 1);
	freetbl (sortfields, free);
	freetbl (groupfields, free);

	/* Normal exit */

	return (0);
}