コード例 #1
0
ファイル: _stock.c プロジェクト: battistuz/antelope_contrib
static PyObject *
python_yearday( PyObject *self, PyObject *args ) {
	char	*usage = "Usage: _stock._yearday( epoch )\n";
	PyObject *obj;
	double	e;
	long	yd;

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

		USAGE;

		return NULL;
	}

	yd = yearday( e );

	obj = Py_BuildValue( "i", yd );

	return obj;
}
コード例 #2
0
int main(int argc, char *argv[])
{
  unsigned int irand;
  int      isamp,wsamp,exists,iret,jdate,nsamp,wfid,itype,i,half;
  long int nw;
  double   samprate,time,endtime,toff,width;
  float    *s,*n,del,nfactor,mean,stdv,dummy,h;
  char     sta[7],chan[9],*database,*type;
  char     dir[65],dfile[33],outfile[100];
  Dbptr    db,dbw;
  FILE     *fp;
 
  database = malloc(80);
  type = malloc(20);

  if (argc < 13) 
  {
    printf("Usage: %s type toff width nfactor sta chan time samprate nsamp database dir filename\n",argv[0]);
    return 1;
  }

  type = argv[1];
  sscanf(argv[2],"%lf",&toff);
  sscanf(argv[3],"%lf",&width);
  sscanf(argv[4],"%f",&nfactor);
  strcpy(sta,argv[5]);
  strcpy(chan,argv[6]);
  sscanf(argv[7],"%lf",&time);
  sscanf(argv[8],"%lf",&samprate);
  sscanf(argv[9],"%d",&nsamp);
  database = argv[10];
  strcpy(dir,argv[11]);
  strcpy(dfile,argv[12]);
  s = malloc(nsamp*sizeof(float));
  n = malloc(nsamp*sizeof(float));

/*Open the output database.*/

  exists = 1;
  if ( (fp = fopen(database,"r")) == NULL)
    exists = 0;
  else
    fclose(fp);

/*It exists, so check if it can be opened and written to.*/

  if (exists)
  {
    if (dbopen(database,"r+",&db) < 0)
    {
      complain(0,"Could not open existing database for writing.\n");
      return 1;
    }
  }
  else
  {
    if (dbcreate(database,"css3.0",0,0,0) != 0)
    {
      complain(0,"Could not create new database.\n");
      return 1;
    }
    else
    {
      if (dbopen(database,"r+",&db) != 0)
      {
        complain(0,"Could not open new database for writing.\n");
        return 1;
      }
    }
  }

  dbw = dblookup(db,0,"wfdisc",0,0);
  iret = dbquery(dbw,dbRECORD_COUNT,&nw);
  printf("nw = %ld\n",nw);
  wfid = dbnextid(db,"wfid");

/*Create the synthetic trace.*/
  if (strcmp(type,"zeros"    ) == 0) itype = 1;
  if (strcmp(type,"delta"    ) == 0) itype = 2;
  if (strcmp(type,"boxcar"   ) == 0) itype = 3;
  if (strcmp(type,"triangle" ) == 0) itype = 4;
  if (strcmp(type,"sine"     ) == 0) itype = 5;
  if (strcmp(type,"impulse"  ) == 0) itype = 6;
  if (strcmp(type,"noise"    ) == 0) itype = 7;

/*Generate a zero-mean, unit rms, random noise series, whether used or not.*/
  irand = now()-1200000000;
  srand(irand);        /* sets the RNG with a random start integer*/
  for (i=0;i<nsamp;i++)
  {
/*    Use a trick to make sure some abnormal number is not generated.*/
      n[i] = 10.0;
      while (fabs(n[i]) > 9.9) n[i] = sqrt(-2.0*log(1.0 - 
        ((float) rand())/RAND_MAX))*cos(6.2832 * ((float) rand())/RAND_MAX);
  }
  del = 1/samprate;
  printf("itype = %d\n",itype);
  switch (itype) 
  {
    case 1:
      /*This creates a series of zeroes.*/
      for (i=0;i<nsamp;i++) s[i] = 0.0;
      break;
    case 2:
      /*This creates a delta function.*/
      for (i=0;i<nsamp;i++) s[i] = 0.0;
      isamp = toff*samprate;
      s[isamp] = 1.0;
      if (nfactor > 0)
      { 
        for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i];
      }
      break;
    case 3:
      /*This creates a boxcar function.*/
      for (i=0;i<nsamp;i++) s[i] = 0.0;
      wsamp = width*samprate;
      /*Make it odd number of points, with center point splitting the boxcar.*/
      if (wsamp%2 == 0) wsamp = wsamp + 1;
      isamp = toff*samprate;
      for (i=isamp;i<isamp+wsamp;i++) s[i] = 1.0;
      if (nfactor > 0)
      { 
        for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i];
      }
      break;
    case 4:
      /*This creates a triangle function.*/
      for (i=0;i<nsamp;i++) s[i] = 0.0;
      wsamp = width*samprate;
      /*Make it odd number of points, with peak at center point.*/
      if (wsamp%2 == 0) wsamp = wsamp + 1;
      half = wsamp/2;
      isamp = toff*samprate;
      for (i=isamp;i<isamp+half;i++) s[i] = 1.0*(i-isamp)/(float)half;
      s[isamp+half] = 1.0;
      for (i=isamp+half+1;i<isamp+wsamp;i++) s[i] = 1.0*(isamp+wsamp-i-1)/(float)half;
      if (nfactor > 0)
      { 
        for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i];
      }
      break;
    case 5:
      /*This creates a 1/width Hz sine wave.*/ 
      for (i=0;i<nsamp;i++) s[i] = sin(2*pi*i*del/width);
      if (nfactor > 0)
      { 
        for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i];
      }
      break;
    case 6:
      /*This creates an exponentially decaying sine wave, like a seismometer
        delta response (damping = 0.7).*/
      h = 0.7;
      for (i=0;i<nsamp;i++) s[i] = 0.0;
      isamp = toff*samprate;
      for (i=0;i<nsamp-isamp;i++) 
        s[i+isamp] = sin(2*pi*sqrt(1-h*h)*i*del/width)*exp(-0.7*2*pi*i*del/width);
      if (nfactor > 0)
      { 
        for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i];
      }
      break;
    case 7:
      /*This creates a random white-noise series.*/
      for (i=0;i<nsamp;i++) s[i] = n[i];
      iret = stats(s,nsamp,2,&mean,&stdv,&dummy,&dummy);
      printf("mean,stdv = %f %f\n",mean,stdv);
      break;
    default:
      printf("Input 'type' does not match any options.\n");
      return 1;
  }

/*Write out the new trace.*/
  memcpy(outfile,"\0",1);
  strcat(outfile,dir);
  strcat(outfile,"/");
  strcat(outfile,dfile);
  printf("outfile = %s\n",outfile);
  if((fp=fopen(outfile,"w")) == NULL)
  {
    fprintf(stderr,"Can't open %s\n",outfile);
    return 1;
  }
  fwrite(s,sizeof(float),(unsigned int)nsamp,fp);
  fclose(fp);

/*Add entry to wfdisc. Note that, due to problem with dbaddv, we force the
  adding of the record with dbputv after a null record is created.*/
  dbw.record = dbaddnull(dbw);
  jdate = yearday(time);
  endtime = time + (nsamp - 1)/samprate;
  if (dbputv(dbw,0,
        "sta", sta,
        "chan", chan,
        "time", time,
        "endtime", endtime,
        "jdate", jdate,
        "nsamp", nsamp,
        "samprate", samprate,
        "datatype", "u4",
        "dir", dir,
        "dfile", dfile,
        "foff", 0,
        "wfid",wfid,
        NULL) < 0)
  {
      complain(0,"Could not add record to wfdisc file.\n");
      return 1;
  }

  dbclose(db);

} /*End main routine.*/
コード例 #3
0
int load_station_geometry(Dbptr db, Arr *a, double time)
{
	Dbptr dbs;
	int i,j,nset;
	Tbl *t;
	char *key;
	int yrday;
	Dbptr dbscr;
	static Hook *hook=NULL;
	Tbl *match_tbl;
	MWstation *s;
	int ondate,offdate;
	char refsta[10];  /*This is used as input buffer that
				is duped to store in each s object*/
	char refsta0[10];  /* Used for comparison to guarantee there
				is no change in refsta */

	yrday = yearday(time);
	t = keysarr(a);
	db = dblookup(db,0,"site",0,0);
	dbs = dblookup(db,0,"site",0,0);
	dbs.record = dbSCRATCH;	
	dbaddnull(dbs);

	for(i=0,nset=0;i<maxtbl(t);i++)
	{
		int nmatch;
		key = gettbl(t,i);
		s = (MWstation *)getarr(a,key);
		dbputv(dbs,0,"sta",s->sta,0);
		nmatch = dbmatches(dbs,db,0,0,&hook,&match_tbl);
		if(nmatch == dbINVALID)
			elog_die(0,"dbmatches error looking for entries for station %s\n",s->sta);
		else if(nmatch < 1)
		{
			elog_notify(0,"load_station_geometry:  cannot find station %s in site table -- deleted from geometry list\n",
				s->sta);
			delarr(a,s->sta);
			free_MWstation(s);
		}
		else
		{
			/* when when there is only one match, we just use it, otherwise
			we have to search for the correct entry based on ondate/offdate */

			if(nmatch == 1)
			{
				db.record = (int )gettbl(match_tbl,0);
			}
			else
			{
				for(j=0;j<maxtbl(match_tbl);j++)
				{
					db.record = (int)gettbl(match_tbl,j);
					if(dbgetv(db,0,"ondate",&ondate,
							"offdate",&offdate,0)
						== dbINVALID)
					{
						elog_notify(0,"load_station_geometry:  dbgetv error while searching for ondate/offdate match for station %s\nBlundering on\n", 
							s->sta);
						continue;
					}
					if((yrday >= ondate) && (yrday <= offdate)) break;
				}
			}
			/* note if the date match fails,we still end up here 
			using the last record in the db */
			freetbl(match_tbl,0);
			if(dbgetv(db,0,
				"lat",&(s->lat),
				"lon",&(s->lon),
				"elev",&(s->elev),
				"dnorth",&(s->dnorth),
				"deast",&(s->deast),
				"refsta",refsta,
				0) == dbINVALID)
			{
				elog_notify(0,"load_station_geometry:  dbgetv error reading record %d for station %s in site table\nStation deleted from list\n",
					db.record,s->sta);
				delarr(a,s->sta);
				free_MWstation(s);
			}
			else
			{
				if(nset==0) strcpy(refsta0,refsta);
				s->refsta = strdup(refsta0);
				if(!strcmp(s->sta,refsta0))
				{
					if(((s->dnorth)!=0.0) 
						|| ((s->deast)!=0.0))
						elog_die(0,"load_station_geometry:  reference station in site table must have dnorth and deast set to 0.0\nFound refstat = %s with (dnorth,deast)=(%lf,%lf)\n",
							s->sta,s->dnorth,
							s->deast);
					++nset;
				}
				else
				/* This deletes stations != refsta with dnorth 
				and deast not set properly.  */
				{
					if(((s->dnorth)==0.0) && ((s->deast)==0.0))
					{
						elog_notify(0,"load_station_geometry:  unset dnorth and deast entries for station %s\nStation deleted from list\n",
							s->sta);
						delarr(a,s->sta);
						free_MWstation(s);
					}
					else if(strcmp(refsta0,refsta))
						elog_die(0,"load_station_geometry:  reference station change not allowed.\nAll stations to be processed must have a common refsta in site table\n");
					else
						++nset;
				}
			}
		}
	}		
	return(nset);
}
コード例 #4
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);
}