int save_tcorr( Dset *bset, Dset *gset, double tcorr) { Dbtc.record = dbaddnull( Dbtc ); dbputv( Dbtc, 0, "time", bset->stime, "tcor", tcorr, "gnet", gset->net, "gsta", gset->sta, "gchan", gset->chan, "gsrate", gset->srate, "bnet", bset->net, "bsta", bset->sta, "bchan", bset->chan, "bsrate", bset->srate, "lddate", now(), 0); return 1 ; }
static void archive_dlsvar( Dbptr db, char *net, char *sta, char *dls_var, char *dsparams, Tbl *rras, double time, double val ) { char key[STRSZ]; char *rrd; double start_time; Dbptr dbt; char datasource[STRSZ]; char command[STRSZ]; char cacheopt[FILENAME_MAX]; /* Disable response printing for now (see below) char response[STRSZ]; char *resp_ptr; */ int i; sprintf( key, "%s:%s:%s", net, sta, dls_var ); rrd = getarr( Rrd_files, key ); /* rrdtool in server-mode apparently does not write files until a request occurs to switch to the next file, so the test below doesn't work right. Trust the database to report existing files: if( rrd == NULL || ! is_present( rrd ) ) { */ if( rrd == NULL ) { start_time = time - Status_stepsize_sec; dbt = db; dbt.record = dbaddnull( db ); dbputv( dbt, 0, "net", net, "sta", sta, "rrdvar", dls_var, "time", start_time, NULL ); trwfname( dbt, Rrdfile_pattern, &rrd ); sprintf( datasource, "DS:%s:%s", dls_var, dsparams ); if( Verbose ) { elog_notify( 0, "Creating rrdfile %s\n", rrd ); } sprintf( command, "create %s -b %d -s %f %s", rrd, (int) floor( start_time ), Status_stepsize_sec, datasource ); for( i = 0; i < maxtbl( rras ); i++ ) { strcat( command, " " ); strcat( command, (char *) gettbl( rras, i ) ); } if( VeryVerbose ) { elog_notify( 0, "Issuing rrdtool command: '%s'\n", command ); } fprintf( Rrdfp, "%s\n", command ); /* Disable response printing for now since popen() bi-directional pipes are not supported across all platforms: if( VeryVerbose ) { resp_ptr = getaline( Rrdfp, response, STRSZ ); if( resp_ptr == (char *) NULL ) { elog_notify( 0, "%s\n", "(null)" ); } else { elog_notify( 0, "%s\n", resp_ptr ); } } */ setarr( Rrd_files, key, strdup( rrd ) ); } if( VeryVerbose ) { elog_notify( 0, "Recording time '%f' value '%f' from '%s:%s:%s' in '%s'\n", time, val, net, sta, dls_var, rrd ); } if( CacheDaemon == NULL ) { sprintf( cacheopt, "%s", "" ); } else { sprintf( cacheopt, "--daemon=%s", CacheDaemon ); } sprintf( command, "update %s %s %d:%f", cacheopt, rrd, (int) floor( time ), val ); if( VeryVerbose ) { elog_notify( 0, "Issuing rrdtool command: '%s'\n", command ); } fprintf( Rrdfp, "%s\n", command ); /* Disable response printing for now since popen() bi-directional pipes are not supported across all platforms: if( VeryVerbose ) { resp_ptr = getaline( Rrdfp, response, STRSZ ); if( resp_ptr == (char *) NULL ) { elog_notify( 0, "%s\n", "(null)" ); } else { elog_notify( 0, "%s\n", resp_ptr ); } } */ }
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.*/
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); }
long writePolygonData(Dbptr db, Point *poly, long npoints, char *pname, int closed, int level, char *ptype, char *auth, char *dir, char *dfile, int pcode) { /* writes poygon data into file if dir and o dfile are NULL, use defaults returns record number if successful,dbINVALID else */ long i; char datafilename[PATH_MAX]; FILE *dfh; int foff; char *ftype; int int1,int2; float float1,float2; double north=-360.0,east=-360.0,west=360.0,south=360.0; double lat,lon; int pid; int free_dir=0; int free_dfile=0; char closed_flag[2]; if (closed) { strcpy(closed_flag,"y"); } else { strcpy(closed_flag,"n"); } pid=dbnextid(db,"pid"); db=dblookup(db, 0, "polygon", 0, 0); if ( (db.record=dbaddnull(db) ) ==dbINVALID) { elog_log(0,"writePolygonData: problem adding row..."); return dbINVALID; } if (dir == NULL) { dir = strdup(default_dir); free_dir=1; } if (dfile == NULL) { dfile = strdup(default_dfile); free_dfile=0; } if (dbputv(db,0,"pid",pid,"dir",dir,"dfile",dfile,NULL ) == dbINVALID) { elog_log(0,"writePolygonData: error putting dir %s & dfile %s",dir,dfile); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; } /* some fix needed here when in "relative" directories...*/ switch (dbfilename(db,datafilename)) { case -1: if (makedir(dir)<0) { elog_log(0,"writePolygonData: dir %s is NOT writable!",dir); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; } if ((dfh= fopen(datafilename,"w"))==NULL) { elog_log(0,"writePolygondata: error crating %s/%s",dir,dfile); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; } foff= 0; break; case 0: /*make file*/ if (makedir(dir)<0) { elog_log(0,"problem creating directory %s",dir); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; } if ((dfh= fopen(datafilename,"w"))==NULL) { elog_log(0,"problem creating file %s",dir); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; } foff= 0; break; case 1: /*append*/ dfh= fopen(datafilename,"a"); fseek(dfh,0,2); foff= ftell(dfh); break; case 2: /*.Z exists....*/ elog_log(0,"writePolygonData: compression not yet supported..."); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; break; default: elog_log(0,"writePolygondata: unknown return value from dbfilename, giving up..."); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; } ftype = polytype(pcode); switch (pcode) { case polyINT: for (i=0;i < npoints;i++) { lon=poly[i].lon; west = (lon<west) ? lon : west; east = (lon>east) ? lon : east; int1=lon * 1e6; H2N4((char *)(&int2),(char *)(&int1),1); fwrite(&int2,sizeof(int),1,dfh); lat=poly[i].lat; south = (lat < south) ? lat : south; north = (lat > north) ? lat : north; int1=lat * 1e6; H2N4((char *)(&int2),(char *)(&int1),1); fwrite(&int2,sizeof(int),1,dfh); } fclose(dfh); break; case polyINTELINT: for (i=0;i < npoints;i++) { lon=poly[i].lon; west = (lon<west) ? lon : west; east = (lon>east) ? lon : east; int1=lon * 1e6; #ifdef WORDS_BIGENDIAN swap4(&int1,&int2,1); #else int2=int1; #endif fwrite(&int2,sizeof(int),1,dfh); lat=poly[i].lat; south = (lat < south) ? lat : south; north = (lat > north) ? lat : north; int1=lat * 1e6; #ifdef WORDS_BIGENDIAN swap4(&int1,&int2,1); #else int2=int1; #endif fwrite(&int2,sizeof(int),1,dfh); } fclose(dfh); break; case polyFLOAT: for (i=0;i < npoints;i++) { lon=poly[i].lon; west = (lon<west) ? lon : west; east = (lon>east) ? lon : east; float1=lon; H2N4((char *)(&float2),(char *)(&float1),1); fwrite(&float2,sizeof(float),1,dfh); lat=poly[i].lat; south = (lat < south) ? lat : south; north = (lat > north) ? lat : north; float1=lat; H2N4((char *)(&float2),(char *)(&float1),1); fwrite(&float2,sizeof(float),1,dfh); } fclose(dfh); break; case polyINTELFLOAT: for (i=0;i < npoints;i++) { lon= poly[i].lon; west= (lon<west) ? lon : west; east= (lon>east) ? lon : east; float1=lon; #ifdef WORDS_BIGENDIAN swap4(&float1,&float2,1); #else float2= float1; #endif fwrite(&float2,sizeof(float),1,dfh); lat= poly[i].lat; south= (lat < south) ? lat : south; north= (lat > north) ? lat : north; float1=lat; #ifdef WORDS_BIGENDIAN swap4(&float1,&float2,1); #else float2= float1; #endif fwrite(&float2,sizeof(float),1,dfh); } fclose(dfh); break; case polyGSHHS: elog_log(0,"writePolygonData: storage type %s not yet implemented... ",ftype); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; break; default: elog_log(0,"writePolygonData: unknown storage type pcode, giving up"); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; } if (dbputv(db,0,"north",north, "east",east, "south",south, "west",west, "pname",pname,"closed",closed_flag,"level",level,"ptype",ptype, "auth",auth,"npoints",npoints, "ftype",ftype,"foff",foff,NULL ) == dbINVALID) { elog_log(0,"writePolygonData: error putting values!"); //free (ftype); if (free_dir) free(dir); if (free_dfile) free(dfile); return dbINVALID; } else { //free (ftype); if (free_dir) free(dir); if (free_dfile) free(dfile); return db.record; } }
static void archive_dlsvar( Dbptr db, char *net, char *sta, char *dls_var, char *dsparams, Tbl *rras, double time, double val ) { char key[STRSZ]; char *rrd; double start_time; Dbptr dbt; char datasource[STRSZ]; char command[STRSZ]; char response[STRSZ]; char *resp_ptr; int i; sprintf( key, "%s:%s:%s", net, sta, dls_var ); rrd = getarr( Rrd_files, key ); if( rrd == NULL || ! is_present( rrd ) ) { start_time = time - Status_stepsize_sec; dbt = db; dbt.record = dbaddnull( db ); dbputv( dbt, 0, "net", net, "sta", sta, "rrdvar", dls_var, "time", start_time, 0 ); trwfname( dbt, Rrdfile_pattern, &rrd ); sprintf( datasource, "DS:%s:%s", dls_var, dsparams ); if( Verbose ) { elog_notify( 0, "Creating rrdfile %s\n", rrd ); } sprintf( command, "create %s -b %d -s %f %s", rrd, (int) floor( start_time ), Status_stepsize_sec, datasource ); for( i = 0; i < maxtbl( rras ); i++ ) { strcat( command, " " ); strcat( command, (char *) gettbl( rras, i ) ); } fprintf( Rrdfp, "%s\n", command ); if( VeryVerbose ) { resp_ptr = getaline( Rrdfp, response, STRSZ ); if( resp_ptr == (char *) NULL ) { elog_notify( 0, "%s\n", "(null)" ); } else { elog_notify( 0, "%s\n", resp_ptr ); } } setarr( Rrd_files, key, rrd ); } if( VeryVerbose ) { elog_notify( 0, "Recording time '%f' value '%f' from '%s:%s:%s' in '%s'\n", time, val, net, sta, dls_var, rrd ); } sprintf( command, "update %s %d:%f", rrd, (int) floor( time ), val ); fprintf( Rrdfp, "%s\n", command ); if( VeryVerbose ) { resp_ptr = getaline( Rrdfp, response, STRSZ ); if( resp_ptr == (char *) NULL ) { elog_notify( 0, "%s\n", "(null)" ); } else { elog_notify( 0, "%s\n", resp_ptr ); } } }