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;
}
Example #2
0
static PyObject *
python_strydtime( PyObject *self, PyObject *args ) {
	char	*usage = "Usage: _stock._strydtime( epoch )\n";
	PyObject *obj;
	double	epoch;
	char 	*s;

	if( ! PyArg_ParseTuple( args, "d", &epoch ) ) {

		USAGE;

		return NULL;
	}

	s = strydtime( epoch );

	obj = Py_BuildValue( "s", s );

	if( s ) {

		free( s );
	}

	return obj;
}
static int
autodrm_response (Dbptr db)
{

    Dbptr           dbstage;
    static Hook    *hook = 0;
    char            sta[MAX_STA_SIZE],
                    chan[MAX_CHAN_SIZE];
    double          time,
                    endtime;
    long            stageid;
    char           *s;
    Response       *response;
    Response_group *group;
    char            iunits[96],
                    ounits[96];
    char            gtype[100];
    long            i,
                    j;
    Paz            *paz;
    Fir            *fir;
    Fap            *fap;
    Fap2           *fap2;
    FILE           *file;
    double          samprate,
                    gnom,
                    gcalib;
    double          calper=0.0;
    long            decifac;
    char            dir[100],
                    dfile[100];
    char            filename[STRSZ];
    long            nmatches;
    int             errors = 0;
    Tbl            *tbl,
                   *stbl;
    double          mintime=0,
                    maxtime=0;
    char            segtype;
    static Tbl     *keys1 = 0,
                   *keys2 = 0;

    if (keys1 == 0) {
	keys1 = strtbl ("sta", "chan", "time", NULL);
	keys2 = strtbl ("sta", "chan", "time::endtime", NULL);
    }
    dbstage = dblookup (db, 0, "stage", 0, 0);
    nmatches = dbmatches (db, dbstage, &keys1, &keys2, &hook, &tbl);

    switch (nmatches) {
      case dbINVALID:
      case 0:
	dbgetv (db, 0, "sta", sta, "chan", chan, "time", &time, NULL);
	complain (0, "Can't match record for %s:%s @ %s in stage table",
		  sta, chan, s = strydtime (time));
	free (s);
	errors++;
	break;

      default:
	stbl = newtbl (maxtbl (tbl));
	for (i = 0; i < nmatches; i++) {
	    dbstage.record = (long) gettbl (tbl, i);
	    dbgetv (dbstage, 0,
		    "stageid", &stageid,
		    "time", &time,
		    "endtime", &endtime,
		    NULL);
	    if (i == 0) {
		mintime = time;
		maxtime = endtime;
	    } else {
		mintime = MAX (time, mintime);
		maxtime = MIN (endtime, maxtime);
	    }
	    settbl (stbl, stageid - 1, (char *) i);
	}
	if (maxtbl (tbl) != maxtbl (stbl)) {
	    dbgetv (db, 0, "sta", sta, "chan", chan, "time", &time, NULL);
	    complain (0, "stageid numbers for %s:%s @ %s don't add up.",
		      sta, chan, s = strydtime (time));
	    free (s);
	    errors++;
	} else {
	    errors += write_cal2 (db, mintime, maxtime, &calper);

	    for (i = 0; i < nmatches; i++) {
		j = (long) gettbl (stbl, i);
		dbstage.record = (long) gettbl (tbl, j);
		dbgetv (dbstage, 0,
			"sta", sta,
			"chan", chan,
			"time", &time,
			"endtime", &endtime,
			"stageid", &stageid,
			"decifac", &decifac,
			"samprate", &samprate,
			"gnom", &gnom,
			"gcalib", &gcalib,
			"dir", dir,
			"dfile", dfile,
			"gtype", gtype,
			"iunits", iunits,
			"ounits", ounits,
			NULL);

		if (gcalib > 0.0) {
		    gnom *= gcalib;
		} else if (gcalib < 0.0) {
		    complain (0, "gcalib = %10.3f < 0. is invalid for %s:%s @ %s.\n",
			      gcalib, sta, chan, s = strydtime (time));
		    free (s);
		    errors++;
		}
		if (*dir != '-' || *dfile != '-') {
		    long            mark;
		    dbextfile (dbstage, "stage", filename);
		    mark = elog_mark ();
		    elog_log (0, "response file is '%s'", filename);
		    if ((file = fopen (filename, "r")) == 0
			    || read_response (file, &response) < 0) {
			register_error (0,
			      "Can't read response file %s  for %s_%s @ %s",
				 filename, sta, chan, s = strydtime (time));
			free (s);
			fclose (file);
			errors++;
		    } else {
			fclose (file);
			if (response->ngroups > 1) {
			    register_error (0,
			      "stage response file %s has %d stages, not 1",
					    filename, response->ngroups);
			    errors++;
			} else {

			    group = response->groups;

			    switch (group->id) {

			      case PAZ:
				/* The normalization frequency chosen in the
				 * response file may not necessarily be the
				 * same as the one chosen by calibration
				 * table for insertion into the seed volumes.
				 * Consequently, we have to adjust the
				 * specified gnom to be correct for the seed
				 * normalization frequency.  Since the gain
				 * is
				 * 
				 * G(f) = gnom_db * A_response_file * P(f) =
				 * gnom_seed * A_seed * P(f)
				 * 
				 * We have
				 * 
				 * gnom_seed = gnom_db * A_response_file /
				 * A_seed
				 * 
				 * gnom_db is just the gnom from the stage
				 * table. A_response_file is the
				 * normalization from the response file, left
				 * in the stage structure. Below, we
				 * calculate A_seed by setting
				 * A_response_file to 1.0.
				 * 
				 */

				paz = (Paz *) group->pvt;
				for (j = 0; j < strlen (iunits); j++) {
				    iunits[j] = tolower (iunits[j]);
				}
				s = getarr (Segtype, iunits);
				if (s == 0) {
				    segtype = 'D';
				} else {
				    segtype = *s;
				}
				adwrite_paz (stageid, 0, gnom, segtype, ounits, paz);
				break;

			      case IIR:
				paz = (Paz *) group->pvt;
				for (j = 0; j < strlen (iunits); j++) {
				    iunits[j] = tolower (iunits[j]);
				}
				s = getarr (Segtype, iunits);
				if (s == 0) {
				    segtype = 'D';
				} else {
				    segtype = *s;
				}
				adwrite_paz (stageid, 1, gnom, segtype, ounits, paz);
				break;

			      case FIR:
				fir = (Fir *) group->pvt;
				errors += adwrite_fir (stageid, gnom, fir);
				break;

			      case FAP:
				fap = (Fap *) group->pvt;
				errors += adwrite_fap (stageid, ounits, fap);
				break;

			      case FAP2:
				fap2 = (Fap2 *) group->pvt;
				errors += adwrite_fap2 (stageid, ounits, fap2);
				break;


			      default:
				complain (0, "Unknown filter type %d in response file %s\n",
					  group->id, filename);
				errors++;
				break;

			    }
			}
		    }
		    elog_flush (mark, 0);
		} else {
		    char           *desc = "";

		    if (gcalib > 0.0) {
			gnom *= gcalib;
		    } else if (gcalib < 0.0) {
			complain (0, "gcalib = %10.3f < 0. is an invalid value.\n", gcalib);
			errors++;
		    }
		    if (strcmp (gtype, "digitizer") == 0
		    /* following hack for psd2db */
			    || strcmp (gtype, "sensor") == 0) {
			fprintf (stdout, "DIG2 %2ld %15.8e %11.5f %s\n",
				 stageid, gnom, samprate, desc);
		    } else if (strcmp (gtype, "amplifier") == 0) {
			/* no corners */
			fprintf (stdout, "GEN2 %2ld %c %15.8e %7.3f                 0 %s\n",
				 stageid, *ounits, gnom, calper, desc);
		    } else {
			complain (0, "Unrecognized gtype='%s' for %s:%s @ %s",
				  gtype, sta, chan, s = strydtime (time));
			free (s);
			errors++;
		    }
		}
	    }
	}
	freetbl (stbl, 0);
	break;

    }
    freetbl (tbl, 0);
    return errors;
}
static int
write_cal2 (Dbptr db, double time, double endtime, double *calperp)
{
    static Dbptr    dbx = INVALID_DBPTR;
    static double   null_endtime = 0.0;
    Dbptr           dbcalibration;
    long            nmatches;
    char            sta[MAX_STA_SIZE],
                    chan[MAX_CHAN_SIZE],
                    units[16],
                    instype[16];
    double          calib,
                    calper=0;
    long            j;
    int             errors = 0;
    Tbl            *tbl;
    static Hook    *hook = 0;
    char           *ontime,
                   *offtime;
    char           *aux = "";
    char           *s,
                   *t;
    char            segtype;
    double          samprate;

    if (dbx.database == dbINVALID) {
	dbx = dblookup (db, 0, "wfdisc", 0, 0);
	dbx.record = dbNULL;
	dbgetv (dbx, 0, "endtime", &null_endtime, NULL);
	dbx.record = dbSCRATCH;
    }
    dbgetv (db, 0, "sta", sta, "chan", chan, NULL);
    dbputv (dbx, 0, "sta", sta, "chan", chan, "time", time, "endtime", endtime, NULL);

    errors += get_instype (dbx, instype, &samprate);

    dbcalibration = dblookup (db, 0, "calibration", 0, 0);
    nmatches = dbmatches (dbx, dbcalibration, 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 calibration table for %s:%s @ %s",
		  sta, chan, s = strydtime (time));
	free (s);
	break;

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

      case 1:
	dbcalibration.record = (long) gettbl (tbl, 0);
	dbgetv (dbcalibration, 0,
		"sta", sta,
		"chan", chan,
		"calib", &calib,
		"calper", &calper,
		"units", units,
		NULL);
	*calperp = calper;

	for (j = 0; j < strlen (units); j++) {
	    units[j] = tolower (units[j]);
	}
	s = getarr (Segtype, units);
	if (s == 0) {
	    segtype = 'D';
	} else {
	    segtype = *s;
	}
	switch (segtype) {
	  case 'A':
	    calib *= SQR (calper / (2 * M_PI));
	    break;

	  case 'V':
	    calib *= calper / (2 * M_PI);
	    break;

	  default:
	    break;
	}

	ontime = epoch2str (time, "%Y/%m/%d %H:%M");
	if (endtime == null_endtime) {
	    offtime = strdup ("");
	} else {
	    offtime = epoch2str (endtime, "%Y/%m/%d %H:%M");
	}
	fprintf (stdout, "\nCAL2 %-5.5s %-3.3s %4.4s %-6.6s %15.8e %7.3f %11.5f %s %s\n",
	 sta, chan, aux, instype, calib, calper, samprate, ontime, offtime);
	free (ontime);
	free (offtime);
	break;
    }

    freetbl (tbl, 0);
    return errors;
}
Example #5
0
void
show_msd ( Msd *msd, Flags flags,FILE *FD,double starttime,double endtime,long *offset, double *time0) 
{
    char net[16], sta[16], chan[16], loc[16] ; 
    char filename[FILENAME_MAX] ; 
    double samprate, nominal, time, samptime, reltime ; 
    int nsamp, has_sdh, has_b100, has_b1000, has_b1001 ;
    int dataformat, fpr, fdf, sparc_order, activity, io, 
    	level, quality, samprate_flags, record_size, correction ; 
    int has_opaque ; 
    Tbl *opaque ;
    char *s ;
	long i;
	static int *data;
	long npts=0;
	
    msdget ( msd, 
	MSD_NET, net, 
	MSD_STA, sta, 
	MSD_CHAN, chan, 
	MSD_LOC, loc, 
	MSD_SAMPRATE, &samprate, 
	MSD_NOMINAL_SAMPRATE, &nominal,
	MSD_TIME, &time, 
	MSD_NSAMP, &nsamp, 
	MSD_HAS_SEED_HEADER, &has_sdh, 
	MSD_HAS_B100, &has_b100, 
	MSD_HAS_B1000, &has_b1000, 
	MSD_HAS_B1001, &has_b1001, 
	MSD_LEVEL, &level,
	MSD_DATAFORMAT, &dataformat, 
	MSD_FRAMES_PER_RECORD, &fpr, 
	MSD_FIRST_DATA_FRAME, &fdf, 
	MSD_SPARC_ORDER, &sparc_order, 
	MSD_FILENAME, filename, 
	MSD_ACTIVITY_FLAGS, &activity, 
	MSD_IO_FLAGS, &io, 
	MSD_QUALITY_FLAGS, &quality, 
	MSD_SAMPRATE_FLAGS, &samprate_flags, 
	MSD_RECORD_SIZE, &record_size, 
	MSD_TIME_CORRECTION, &correction, 
	MSD_HAS_OPAQUE, &has_opaque,
	MSD_OPAQUE, &opaque,
         0 ) ; 

	if ( dataformat> 0) { 
		if ( umsd ( msd, &data, &npts ) ) { 
			complain ( 0, "umsd failed" ) ; 
		} else { 
			if (flags.verbose) {
				printf ( "%2s %5s %3s %2s  %s  %5d %10.4f\n", 
					net, sta, chan, loc, s=strydtime(time), nsamp, samprate ) ; 
				free(s) ; 
			}
			for ( i=0 ; i<npts ; i++ ) { 
				//samptime=SAMP2TIME(time,samprate,i);
				samptime=SAMP2TIME(time,samprate,i);
				if ((samptime >= starttime) && (samptime <= endtime) ) {
					reltime=SAMP2TIME(0,samprate,*offset);
					if (*time0==0.0) {
						*time0=SAMP2TIME(time,samprate,i);
						fprintf(FD,"time0=%s\n",s=strtime(*time0));free(s);
					}
					fprintf (FD, "%ld 00:00:%06.3f %f\n", *offset,reltime,flags.calib * data[i] ) ; 
					//fprintf (FD, "%8ld %s %8d\n", *offset,s=epoch2str(samptime,"%H:%M:%S.%s"),data[i] ) ; 
					//free(s);
					(*offset)+=1;
				}
			}
		}
	}
}