コード例 #1
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);
}
コード例 #2
0
int
write_trace (Dbptr db, char *sta, char *chan, char *dir, char *dfile, Trace *trace, double tstart, double tend, int overwrite)

{
    int fd;
    char outdir[1024];
    char outbase[1024];
    char fname[1024];
    int sz, size, ret;
    int is, ie, ns;
    Dbvalue dbv;

    is = (tstart-trace->tstart)/trace->dt + 0.0001;
    ie = (tend-trace->tstart)/trace->dt - 0.0001;
    if (is > trace->nsamps-1) return (1);
    if (ie < 0) return (1);
    if (is < 0) is = 0;
    if (ie > trace->nsamps-1) ie = trace->nsamps-1;
    ns = ie - is + 1;
    if (ns < 1) return (1);
    db = dblookup (db, 0, "wfdisc", 0, 0);
    if (dir[0] == '/') {
        sprintf (fname, "%s/%s", dir, dfile);
    } else {
        dbquery (db, dbTABLE_DIRNAME, &dbv);
        strcpy (outbase, dbv.t);
        sprintf (fname, "%s/%s/%s", outbase, dir, dfile);
    }
    if (!overwrite) {
        if (zaccess(fname, F_OK) != -1) {
            fprintf (stderr, "write_trace: wf file conflict for '%s'.\n",
                     fname);
            return (0);
        }
    }
    dirbase (fname, outdir, outbase);
    if (makedir(outdir) == -1) {
        fprintf (stderr, "write_trace: Unable to create %s\n", outdir);
        return (0);
    }
    fd = open (fname, (O_RDWR|O_CREAT|O_TRUNC), 0666);
    if (fd < 0) {
        fprintf (stderr, "write_trace: Open error on '%s'.\n", fname);
        return (0);
    }
    sz = atoi(&trace->rawdata_format[strlen(trace->rawdata_format)-1]);
    size = sz*ns;
    ret = write (fd, &(((char *)trace->raw_data)[is*sz]), size);
    close (fd);
    if (ret < size) {
        fprintf (stderr, "write_trace: Write error on '%s'.\n", fname);
        return (0);
    }
    db.record = dbNULL;
    db.field = dbALL;
    dbget(db, 0);
    db.record = dbSCRATCH;
    dbputv (db, 0,	"sta", sta,
            "chan", chan,
            "time", trace->tstart+is*trace->dt,
            "wfid", dbnextid(db, "wfid"),
            /*			"chanid", chanid,*/
            "jdate", yearday(trace->tstart),
            "endtime", trace->tstart+trace->dt*(trace->nsamps-1),
            "nsamp", (long)ns,
            "samprate", 1.0/trace->dt,
            "calib", trace->calib,
            "calper", trace->calper,
            "instype", "BEAM",
            "segtype", trace->rawdata_type,
            "datatype", trace->rawdata_format,
            /*			"clip", clip,*/
            "dir", dir,
            "dfile", dfile,
            "foff", (long)0,
            NULL);
    dbadd (db, 0);
    return (1);
}