static bool_t DEHT_findFirstBlockForBucket(DEHT * ht, ulong_t bucketIndex, DEHT_DISK_PTR * blockOffset) { bool_t ret = FALSE; TRACE_FUNC_ENTRY(); CHECK(NULL != ht); CHECK(NULL != blockOffset); CHECK(bucketIndex < ht->header.numEntriesInHashTable); if (NULL != ht->hashTableOfPointersImageInMemory) { *blockOffset = ht->hashTableOfPointersImageInMemory[bucketIndex]; } else { CHECK_MSG(ht->sKeyfileName, (pfread(ht->keyFP, KEY_FILE_OFFSET_TO_FIRST_BLOCK_PTRS(ht) + bucketIndex * sizeof(DEHT_DISK_PTR), (byte_t *) blockOffset, sizeof(*blockOffset)))); } ret = TRUE; goto LBL_CLEANUP; LBL_ERROR: ret = FALSE; *blockOffset = 0; TRACE_FUNC_ERROR(); LBL_CLEANUP: TRACE_FUNC_EXIT(); return ret; }
static bool_t DEHT_enumerateBucket(DEHT * ht, int bucketIndex, byte_t * blockBuffer, DEHT_enumerationCallback_t callback, void * param) { bool_t ret = FALSE; DEHT_DISK_PTR blockOffset = 0; TRACE_FUNC_ENTRY(); CHECK(NULL != ht); CHECK(NULL != blockBuffer); CHECK(NULL != callback); CHECK(DEHT_findFirstBlockForBucket(ht, bucketIndex, &blockOffset)); while (0 != blockOffset) { CHECK_MSG(ht->sKeyfileName, (pfread(ht->keyFP, blockOffset, blockBuffer, KEY_FILE_BLOCK_SIZE(ht)))); CHECK(DEHT_enumerateBlock(ht, blockBuffer, bucketIndex, callback, param)); blockOffset = GET_NEXT_BLOCK_PTR(ht, blockBuffer); } ret = TRUE; goto LBL_CLEANUP; LBL_ERROR: TRACE_FUNC_ERROR(); ret = FALSE; LBL_CLEANUP: TRACE_FUNC_EXIT(); return ret; }
static bool_t DEHT_readDataAtOffset(DEHT * ht, DEHT_DISK_PTR dataBlockOffset, byte_t * data, ulong_t dataMaxAllowedLength, ulong_t * bytesRead) { bool_t ret = FALSE; byte_t dataLen = 0; TRACE_FUNC_ENTRY(); CHECK(NULL != ht); CHECK(NULL != data); CHECK(NULL != bytesRead); *bytesRead = 0; CHECK_MSG(ht->sDatafileName, (pfread(ht->dataFP, dataBlockOffset, &dataLen, sizeof(dataLen)))); TRACE_FPRINTF((stderr, "TRACE: %s:%d (%s): data size is %d\n", __FILE__, __LINE__, __FUNCTION__, dataLen)); *bytesRead = fread(data, 1, MIN(dataMaxAllowedLength, dataLen), ht->dataFP); CHECK(0 <= *bytesRead); ret = TRUE; goto LBL_CLEANUP; LBL_ERROR: *bytesRead = 0; ret = FALSE; TRACE_FUNC_ERROR(); LBL_CLEANUP: TRACE_FUNC_EXIT(); return ret; }
int read_DEHT_pointers_table(DEHT *ht) { int ret = DEHT_STATUS_FAIL; TRACE_FUNC_ENTRY(); CHECK(NULL != ht); if (NULL != ht->hashTableOfPointersImageInMemory) { return DEHT_STATUS_NOT_NEEDED; } /* alloc cache */ ht->hashTableOfPointersImageInMemory = malloc(KEY_FILE_FIRST_BLOCK_PTRS_SIZE(ht)); CHECK_MSG("malloc", (NULL != ht->hashTableOfPointersImageInMemory)); /* read the offset table */ CHECK_MSG(ht->sKeyfileName, (pfread(ht->keyFP, KEY_FILE_OFFSET_TO_FIRST_BLOCK_PTRS(ht), (byte_t *) ht->hashTableOfPointersImageInMemory, KEY_FILE_FIRST_BLOCK_PTRS_SIZE(ht)))); ret = DEHT_STATUS_SUCCESS; goto LBL_CLEANUP; LBL_ERROR: /* on error, free the buffer */ FREE(ht->hashTableOfPointersImageInMemory); ret = DEHT_STATUS_FAIL; TRACE_FUNC_ERROR(); LBL_CLEANUP: TRACE_FUNC_EXIT(); return ret; }
int main (int argc, char **argv) { int c, errflg = 0; Dbptr db; char *table; int verbose = 0; int errors = 0; long n; Pf *pf; elog_init (argc, argv); if (pfread (Program_Name, &pf) != 0) die (0, "Can't read parameter file\n"); Segtype = pfget_arr (pf, "segtype"); while ((c = getopt (argc, argv, "vV")) != -1) { switch (c) { case 'v': verbose++; break; case 'V': banner (Program_Name, 0) ; exit (0); default: errflg++; break; } } if (errflg || argc - optind != 1) usage (); table = argv[optind++]; n = dbopen_table (table, "r", &db); switch (n) { case dbINVALID: die (0, "Can't open input table '%s'", table); break; case 0: die (0, "No records in input table '%s'", table); break; } for (db.record = 0; db.record < n; db.record++) { errors += autodrm_response (db); } return (errors == 0 ? 0 : 1); }
bool PStreamFile::read(pUint32 nbytes, pUint32& bytesRead, pUint8* buffer) { if (m_file == P_NULL) { pLogError("Unable to open file for reading"); return false; } bytesRead = pfread(buffer, sizeof(pUint8), nbytes, m_file); return true; }
pint32 P_APIENTRY pAssetRead(PAsset *asset, void *buffer, puint32 count) { PASSERT(asset != P_NULL); if (asset != P_NULL) { PFile *fp = (PFile *)asset->pHandle; puint32 readLen = pfread(buffer, sizeof(puint8), count, fp); PASSERT(readLen == count); return readLen; } return 0; }
void init( ) { Pf *Param; Tbl *Site; DAS *das; struct Site site; char *istr; char dasid[6]; int pkttype; int ntrec, i, j; int nsite; int *num = 0; if(pfread( pfile, &Param) != 0) die(0, "Can't read parameter file\n"); /* Get Input & Network tables */ Site = pfget_tbl(Param, "Site"); nsite = maxtbl(Site); if( nsite <= 0 ) die( 0, "init(): parameter file is not complete.\n"); Dases = newarr( 0 ); Dasid = newarr( 0 ); for( i = 0; i < nsite; i++ ) { istr = (char *) gettbl(Site, i); sscanf(istr, STE_SCS, STE_RVL(&site)); if( !strcmp(site.up, "Y") ) { sprintf( dasid, "%d\0", site.sid ); if( ( das = ( DAS *) getarr( Dases, dasid ) ) == NULL ) { das = new_das( site.sid, site.name ); setarr(Dases, (char *) &dasid[0], (char *) das ); } if( (num = ( int *) getarr( Dasid, site.name ) ) == NULL ) { allot( int *, num, 1 ); *num = site.sid; setarr(Dasid, (char *) site.name, (char *) num ); } }
const void * P_APIENTRY pAssetGetBuffer(PAsset *asset) { PASSERT(asset != P_NULL); if (asset != P_NULL) { puint32 len = pAssetGetSize(asset); PFile *fp = (PFile *)asset->pHandle; puint8 *buffer = PNEWARRAY(puint8, len); pfseek(fp, 0, P_FILE_SEEK_FROM_BEGINNING); puint32 readLen = pfread(buffer, sizeof(puint8), len, fp); PASSERT(readLen == len); PASSERT(asset->pData == P_NULL); asset->pData = buffer; return asset->pData; } return P_NULL; }
int DEHT_getUserBytes(DEHT * ht, byte_t * * bufPtr, ulong_t * bufSize) { int ret = DEHT_STATUS_FAIL; TRACE_FUNC_ENTRY(); CHECK(NULL != ht); CHECK(NULL != bufPtr); CHECK(NULL != bufSize); if (NULL != ht->userBuf) { *bufPtr = ht->userBuf; *bufSize = ht->header.numUnrelatedBytesSaved; ret = DEHT_STATUS_NOT_NEEDED; goto LBL_CLEANUP; } ht->userBuf = malloc(ht->header.numUnrelatedBytesSaved); CHECK_MSG("malloc", (NULL != ht->userBuf)); CHECK_MSG(ht->sDatafileName, (pfread(ht->dataFP, DATA_FILE_OFFSET_TO_USER_BYTES, ht->userBuf, ht->header.numUnrelatedBytesSaved))); *bufSize = ht->header.numUnrelatedBytesSaved; *bufPtr = ht->userBuf; ret = DEHT_STATUS_SUCCESS; goto LBL_CLEANUP; LBL_ERROR: ret = DEHT_STATUS_FAIL; TRACE_FUNC_ERROR(); LBL_CLEANUP: if (DEHT_STATUS_FAIL == ret) { FREE(ht->userBuf); } TRACE_FUNC_EXIT(); return ret; }
static bool_t DEHT_findLastBlockForBucketDumb(DEHT * ht, ulong_t bucketIndex, DEHT_DISK_PTR * lastBlockOffset) { bool_t ret = FALSE; DEHT_DISK_PTR blockOffset = 0; TRACE_FUNC_ENTRY(); CHECK(NULL != ht); CHECK(NULL != lastBlockOffset); CHECK(bucketIndex < ht->header.numEntriesInHashTable); CHECK(DEHT_findFirstBlockForBucket(ht, bucketIndex, &blockOffset)); if (0 == blockOffset) { *lastBlockOffset = 0; ret = TRUE; goto LBL_CLEANUP; } *lastBlockOffset = blockOffset; /* scan chain */ while (0 != blockOffset) { *lastBlockOffset = blockOffset; /* read the offset to the next block from disk */ CHECK_MSG(ht->sKeyfileName, (pfread(ht->keyFP, blockOffset + KEY_FILE_BLOCK_SIZE(ht) - sizeof(DEHT_DISK_PTR), (byte_t *) &blockOffset, sizeof(blockOffset)))); } ret = TRUE; goto LBL_CLEANUP; LBL_ERROR: *lastBlockOffset = 0; TRACE_FUNC_ERROR(); LBL_CLEANUP: TRACE_FUNC_EXIT(); return ret; }
int main(int argc, char **argv) { SegyReel reel; SegyHead *header; char *dbin; char *outfile; FILE *fp; Pf *pf; Arr *channels; /* channel order list */ Arr *table_list; /* array of valid tables */ int nchan; char *stest; float **traces; char reel1[3200]; Dbptr db, trdb, dbj; Dbptr trdbss; int nsamp0; double time0, endtime0, samprate0; long int nsamp; double samprate; int i,j; char stime[30],etime[30]; char s[128]; double tlength; double phi, theta; char *newchan_standard[3]={"X1","X2","X3"}; char *trsubset="chan=~/X./"; char *newchan[3]={"R","T","Z"}; Tbl *sortkeys=newtbl(0); char sta[10],chan[10]; double lat, lon, elev, dnorth, deast, edepth; char refsta[10]; int total_traces=0; char *time_str; long int evid,shotid=1; int rotate=0; long int ntraces; int ichan; int map_to_cdp; /* logical switch to output data like cdp stacked data */ char *fmt="%Y %j %H %M %S %s"; char *pfname; int Verbose=0; /* New features added 2009 */ /* this is a boolean. If true (nonzero) it is assumed stdin will contain four numbers: time,lat, lon, elev. If false, only the time field is read and remainder of any input on each line is dropped.*/ int input_source_coordinates; /* scale factor for source coordinates. Needed because segy uses an int to store source coordinates. Sensible choices are 3600 for arc seconds and 10000 for a pseudodecimal. Note this parameter is ignored unless input_source_coordinates is true.*/ int coordScale; /* If true use passcal 32 bit extension num_samps as record length. SEGY standard uses a 16 bit entry that easily overflows with large shots at long offset. In this ase assume the 16 bit quantity is meaningless. */ int use_32bit_nsamp; /* This is switched on by argument switch. When set to a nonzero (default) the reel headers are written. When 0 ` the reel heades will not be written -- used by seismic unix r and passcal*/ int write_reel_headers=1; char *substr=NULL; if(argc < 3) usage(); dbin = argv[1]; outfile = argv[2]; pfname = NULL; for(i=3;i<argc;++i) { if(!strcmp(argv[i],"-pf")) { ++i; pfname = argv[i]; } else if(!strcmp(argv[i],"-SU")) { write_reel_headers=0; } else if(!strcmp(argv[i],"-v")) { Verbose=1; } else if(!strcmp(argv[i],"-ss")) { ++i; substr=argv[i]; } else { usage(); } } if(pfname == NULL) pfname = strdup("db2segy"); elog_init(argc, argv); if(pfread(pfname,&pf)) elog_die(0,"pfread error for pf file %s.pf\n",argv[0]); /* rotation parameters */ rotate=pfget_boolean(pf,"rotate"); if(rotate) { phi = pfget_double(pf,"phi"); theta = pfget_double(pf,"theta"); } /* This function creates the channel order list keyed by station channel names */ channels = build_stachan_list(pf,&nchan,Verbose); map_to_cdp = pfget_boolean(pf,"map_to_cdp"); if(map_to_cdp && Verbose) fprintf(stdout,"Casting data as CDP stacked section\n"); if(dbopen(dbin,"r",&db) == dbINVALID) { fprintf(stderr,"Cannot open db %s\n", dbin); usage(); } /* We grab the sample rate and trace length (in seconds) and use this to define global sample rates for the data. segy REQUIRES fixed length records and sample rates, so irregular sample rates will cause this program to die. One could add a decimate/interpolate function, but this is not currently implemented */ samprate0 = pfget_double(pf,"sample_rate"); tlength = pfget_double(pf,"trace_length"); nsamp0 = (int)(tlength*samprate0); use_32bit_nsamp=pfget_boolean(pf,"use_32bit_nsamp"); /* nsamp in segy is a 16 bit field. Handling depends on setting of use_32bit_nsamp boolean */ if(nsamp0 > 32767) { if(use_32bit_nsamp) { elog_notify(0,"Warning: segy ues a 16 bit entity to store number of samples\nThat field is garbage. Using the 32 bit extension field.\n"); } else { elog_complain(0, "Warning: segy uses a 16 bit entity to store number of samples\nRequested %d samples per trace. Trucated to 32767\n",nsamp0); nsamp0 = 32767; } } input_source_coordinates=pfget_boolean(pf,"input_source_coordinates"); if(input_source_coordinates) { coordScale=pfget_int(pf,"coordinate_scale_factor"); } else { coordScale=1; } /* boolean. When nonzero set coordinates as geographic arc seconds values */ int use_geo_coordinates=pfget_boolean(pf,"use_geo_coordinates"); /* check list of tables defined in pf. Return array of logicals that define which tables are valid and join tables. */ table_list = check_tables(db,pf); check_for_required_tables(table_list); dbj = join_tables(db,pf,table_list); if(dbj.record == dbINVALID) elog_die(0,"dbjoin error\n"); if(substr!=NULL) dbj=dbsubset(dbj,substr,0); long int ndbrows; dbquery(dbj,dbRECORD_COUNT,&ndbrows); if(ndbrows<=0) { fprintf(stderr,"Working database view is empty\n"); if(substr!=NULL) fprintf(stderr,"Subset condtion =%s a likely problem\n", substr); usage(); } fp = fopen(outfile,"w"); if(fp == NULL) { fprintf(stderr,"Cannot open output file %s\n",outfile); usage(); } /* These are needed for sort below */ pushtbl(sortkeys,"sta"); pushtbl(sortkeys,"chan"); /*The reel1 header in true blue segy is ebcdic. We are goingto just fill it with nulls and hope for the best */ for(i=0;i<3200;i++) reel1[i] = '\0'; /* Just blindly write this turkey. Bad form, but tough*/ if(write_reel_headers) fwrite(reel1,1,3200,fp); /* memory allocation for trace data. This is a large matrix that is cleared for each event. This model works because of segy's fixed length format. This routine is a descendent of numerical recipes routine found in libgenloc. This is not the most efficient way to do this, but it simplifies the algorithm a lot. */ traces = matrix(0,nchan,0,nsamp0); if(traces == NULL) elog_die(0,"Cannot alloc trace data matrix work space of size %d by %d\n", nchan, nsamp0); header = (SegyHead *)calloc((size_t)nchan,sizeof(SegyHead)); if(header == NULL) elog_die(0,"Cannot alloc memory for %d segy header workspace\n",nchan); if(write_reel_headers) { /* now fill in the binary reel header and write it */ reel.kjob = 1; reel.kline = 1; reel.kreel = 1; reel.kntr = (int16_t)nchan; reel.knaux = 0; reel.sr = (int16_t)(1000000.0/samprate0); reel.kfldsr = reel.sr; reel.knsamp = (int16_t)nsamp0; reel.kfsamp = (int16_t)nsamp0; reel.dsfc=5; /* This is ieee floats*/ reel.kmfold = 0; if(map_to_cdp) reel.ksort = 2; else reel.ksort = 1; reel.kunits = 1; /* This sets units to always be meters */ for(i=0;i<344;++i)reel.unused2[i]='\0'; if(fwrite((void *)(&reel),sizeof(SegyReel),1,fp) != 1) { fprintf(stderr,"Write error for binary reel header\n"); exit(-2); } } /* Now we enter a loop over stdin reading start times. Program will blindly ask for data from each start time to time+tlength. The trace buffer will be initialized to zeros at the top of the loop always. If nothing is found only zeros will be written to output. */ while((stest=fgets(s,80,stdin)) != NULL) { double slat,slon,selev; /* Used when reading source location*/ if(Verbose) fprintf(stdout,"Processing: %s\n",s); for(i=0;i<nchan;++i) { initialize_header(&(header[i])); header[i].lineSeq = total_traces + i + 1; header[i].reelSeq = header[i].lineSeq; if(map_to_cdp) { header[i].cdpEns = i + 1; header[i].traceInEnsemble = 1; /* 1 trace per cdp faked */ } else { header[i].channel_number = i + 1; } header[i].event_number = shotid; header[i].energySourcePt=shotid; for(j=0;j<nsamp0;++j) traces[i][j] = (Trsample)0.0; } if(input_source_coordinates) { char stmp[40]; sscanf(s,"%s%ld%lf%lf%lf",stmp,&shotid,&slon,&slat,&selev); time0=str2epoch(stmp); } else { time0 = str2epoch(s); } endtime0 = time0 + tlength; sprintf(stime,"%20.4f",time0); sprintf(etime,"%20.4f",endtime0); trdb.database = -1; if(trload_css(dbj,stime,etime,&trdb,0, 0) < 0) { if(Verbose) { fprintf(stdout,"trload_css failed for shotid=%ld",shotid); fprintf(stdout," No data in time range %s to %s\n", strtime(time0),strtime(endtime0) ); fprintf(stdout,"No data written for this shotid block."); fprintf(stdout," Handle this carefully in geometry definitions.\n"); } continue; } /* This does gap processing */ repair_gaps(trdb); trapply_calib(trdb); if(rotate) { if(rotate_to_standard(trdb,newchan_standard)) elog_notify(0,"Data loss in rotate_to_standard for event %s to %s\n", stime, etime); /* This is need to prevent collisions of channel names */ trdbss = dbsubset(trdb,trsubset,0); if(trrotate(trdbss,phi,theta,newchan)) elog_notify(0,"Data loss in trrotate for event %s to %s\n", stime, etime); } if(Verbose) fprintf(stdout,"Station chan_name chan_number seq_number shotid evid\n"); trdb = dbsort(trdb,sortkeys,0,0); dbquery(trdb,dbRECORD_COUNT,&ntraces); if(Verbose) fprintf(stdout,"Read %ld traces for event at time%s\n", ntraces,strtime(time0)); for(trdb.record=0;trdb.record<ntraces;++trdb.record) { Trsample *trdata; if(dbgetv(trdb,0, "evid",&evid, "sta",sta, "chan",chan, "nsamp", &nsamp, "samprate",&samprate, "data",&trdata, "lat", &lat, "lon", &lon, "elev",&elev, "refsta",refsta, "dnorth",&dnorth, "deast",&deast, "edepth",&edepth, NULL) == dbINVALID) { elog_complain(0," dbgetv error reading record %ld\nTrace will be skipped for station %s and channel %s\n", trdb.record,sta,chan); continue; } /* Allow 1 percent samprate error before killing */ double fsrskew=fabs((samprate-samprate0)/samprate0); double frskewcut=0.01; if(fsrskew>frskewcut) { elog_complain(0,"%s:%s sample rate %f is significantly different from base sample rate of %f\nTrace skipped -- segy requires fixed sample rates\n", sta,chan,samprate,samprate0); continue; } if(nsamp > nsamp0) { elog_complain(0,"%s:%s trace has extra samples=%ld\nTruncated to length %d\n", sta, chan, nsamp, nsamp0); nsamp = nsamp0; } else if(nsamp < nsamp0) { elog_complain(0,"%s:%s trace is shorter than expected %d samples\nZero padded after sample %ld\n", sta, chan, nsamp0, nsamp); } ichan = get_channel_index(channels,sta,chan); if(ichan > nchan) elog_die(0,"Channel index %d outside limit of %d\nCannot continue\n", ichan, nchan); if(ichan >= 0) { if(Verbose) fprintf(stdout,"%s:%s\t%-d\t%-d\t%-ld\t%-ld\n", sta,chan,ichan+1, header[ichan].reelSeq, shotid, evid); header[ichan].traceID = 1; for(j=0;j<nsamp;++j) traces[ichan][j] = (float)trdata[j]; /* header fields coming from trace table */ header[ichan].samp_rate = (int32_t) (1000000.0/samprate0); if(!use_geo_coordinates && ( coordScale==1)) { header[ichan].recLongOrX = (int32_t)(deast*1000.0); header[ichan].recLatOrY = (int32_t)(dnorth*1000.0); } else { /* Note negative here. This is a oddity of segy that - means divide by this to get actual. Always make this negative in case user inputs a negative number. */ header[ichan].coordScale=-abs(coordScale); /* Force 2 = geographic coordinates. Standard says when this is so units are arc seconds, hence we multiply deg by 3600*coordScale */ if(use_geo_coordinates) { header[ichan].coordUnits=2; header[ichan].recLongOrX =(int32_t)(lon*3600.0*(double)coordScale); header[ichan].recLatOrY =(int32_t)(lat*3600.0*(double)coordScale); } else { header[ichan].recLongOrX =(int32_t)(lon*(double)coordScale); header[ichan].recLatOrY =(int32_t)(lat*(double)coordScale); } } header[ichan].recElevation = (int32_t)(elev*1000.0); header[ichan].deltaSample = (int16_t) (1000000.0/samprate0); header[ichan].sampleLength = (int16_t)nsamp0; header[ichan].num_samps = (int32_t)nsamp0; /* This cracks the time fields */ time_str = epoch2str(time0,fmt); sscanf(time_str,"%hd %hd %hd %hd %hd %hd", &header[ichan].year, &header[ichan].day, &header[ichan].hour, &header[ichan].minute, &header[ichan].second, &header[ichan].m_secs); /* These are PASSCAL extensions, but we'll go ahead and set them anyway.*/ header[ichan].trigyear = header[ichan].year; header[ichan].trigday = header[ichan].day; header[ichan].trighour = header[ichan].hour; header[ichan].trigminute = header[ichan].minute; header[ichan].trigsecond = header[ichan].second; free(time_str); if(input_source_coordinates) { if(use_geo_coordinates) { slat*=3600.0; slon*=3600.0; } header[ichan].sourceLongOrX =(int32_t)(slon*(double)coordScale); header[ichan].sourceLatOrY =(int32_t)(slat*(double)coordScale); header[ichan].sourceSurfaceElevation =(int32_t)selev; /* No easy way to specify both elev and depth*/ header[ichan].sourceDepth=0; } else if(map_to_cdp) { /* When faking CDP data we make this look like a zero offset, single fold data set */ header[ichan].sourceLongOrX = header[ichan].recLongOrX; header[ichan].sourceLatOrY = header[ichan].recLatOrY; header[ichan].sourceSurfaceElevation = header[ichan].recElevation; header[ichan].sourceDepth = 0; header[ichan].sourceToRecDist = 0; } else { /* This is the mechanism for adding other information with added tables. The one table currently supported is a "shot" table that holds shot coordinates. If other tables were added new functions could be added with a similar calling sequence. This procedure silently does nothing if a shot table is not present.*/ set_shot_variable(db,table_list, evid,&header[ichan]); } } else { if(Verbose) fprintf(stdout,"Station %s and channel %s skipped\n", sta,chan); } } /* Now we write the data */ for(i=0;i<nchan;++i) { if(fwrite((void *)(&(header[i])),sizeof(SegyHead),1,fp) != 1) elog_die(0,"Write error on header for trace %d\n",total_traces+i); if(fwrite((void *)traces[i],sizeof(float), (size_t)nsamp0,fp) != nsamp0) elog_die(0,"Write error while writing data for trace %d\n", total_traces+i); } total_traces += nchan; trdestroy(&trdb); if(!input_source_coordinates) ++shotid; } return 0 ; }
/*! \brief parse the file request and send the requested file to the host * * \param pxNetCon Input. The netconn to use to send and receive data. * \param filename Input. The incoming filename. * */ static void prvweb_SendFile( struct netconn *pxNetCon, char* filename ) { portLONG size, header_size; portLONG fd = -1; portCHAR *resp_data; unsigned portSHORT i; err_t error; pf_read_src pfread = read; // Set the default read to a file read. // NAKED_TRACE_COM2( "SendFile(): %s", filename ); /* allocate response */ resp_data = (portCHAR *)pvPortMalloc(webMAX_DATA_TO_SEND); if (resp_data != NULL) { /* add the header */ header_size = prulweb_BuildHeaders(resp_data, 200, "OK", "", "", "text/html"); if ((fd = open((const char *)filename, O_RDONLY)) < 0) { // Set the read function to read from the pcDefaultPage pfread = prv_read_default_page; size = x_default_page_len; NAKED_TRACE_COM2( "SendFile(): file not found. Default page len:%d", x_default_page_len ); } else /* get the file size */ { size = fsaccess_file_get_size(fd); } /* check if file should be sent in a single frame */ if ( size <= (webMAX_DATA_TO_SEND - header_size)) { /* get the data from filesystem */ if( pfread(fd,&resp_data[header_size],size) != size) { NAKED_TRACE_COM2( "SendFile(): short file read access error" ); prvweb_SendErrorPage( pxNetCon, 404, filename, "Error during read access." ); goto quit_send_page; } /* send the response to network */ error = netconn_write(pxNetCon, resp_data, (u16_t)(header_size + size), NETCONN_COPY ); if (error != 0) { NAKED_TRACE_COM2( "SendFile(): short file nw write error" ); goto quit_send_page; } } else { /* send the header */ error = netconn_write(pxNetCon, resp_data, header_size, NETCONN_COPY ); if (error != 0) { NAKED_TRACE_COM2( "SendFile(): long file hdr nw write error" ); goto quit_send_page; } /* try to send the biggest frame contained in the file */ for (i = webNB_SECTOR_TO_SEND ; i > 0 ; i--) { /* get sectors of maximum size */ while(size > i * FS_SIZE_OF_SECTOR) { /* get the data from filesystem */ if( pfread(fd,resp_data, i * FS_SIZE_OF_SECTOR) != i * FS_SIZE_OF_SECTOR) { NAKED_TRACE_COM2( "SendFile(): long file read access error" ); prvweb_SendErrorPage( pxNetCon, 404, filename, "Error during read access." ); goto quit_send_page; } /* send the response to network */ error = netconn_write(pxNetCon, resp_data, (u16_t)(i * FS_SIZE_OF_SECTOR), NETCONN_COPY ); if (error != 0) { NAKED_TRACE_COM2( "SendFile(): long file nw write error" ); goto quit_send_page; } size -= (i * FS_SIZE_OF_SECTOR); } } /* finish with the few data remaining (less than 1 sector)*/ if ( size > 0 ) { /* get the data from filesystem */ if( pfread(fd,resp_data,size) != size) { NAKED_TRACE_COM2( "SendFile(): long file end read access error" ); prvweb_SendErrorPage( pxNetCon, 404, filename, "Error during read access." ); goto quit_send_page; } /* send the response to network */ error = netconn_write(pxNetCon, resp_data, (u16_t)size, NETCONN_COPY ); if (error != 0) { NAKED_TRACE_COM2( "SendFile(): long file end nw write error" ); goto quit_send_page; } } } }// end if response != NULL else { NAKED_TRACE_COM2( "SendFile(): mem alloc failed" ); __asm__ __volatile__ ("nop"); } quit_send_page: // vPortFree() handles the case where resp_data==NULL vPortFree(resp_data); /* close the file */ // close() handles the case where fd is an invalid file descriptor. close(fd); }
int main (int argc, char **argv) { Dbptr master_db, dbtmp; char dbname[512]; /* dbtmp name assigned by maketmpdb */ char *orbname; char *pffile=NULL; Pf *pf; /* Input pf object handle */ Arr *arr_sta; Arr *arr_a; /*Array object associative array -- purely a place holder*/ Arr *arr_phase; int i; char *statefile=NULL; Point origin; int orbin,orbout; /* We establish both a read and write connection on seperate sockets so we can use orbreap on the input */ int quit=0,last_pktid; double last_pkttime; int exhume_rcode; /* value returned by exhume*/ char *packet=0; int orid_used; Location_options o; RTlocate_Options rt_opts; ORB_Hypocenter hyp; /* This initialization is necessary for the orb_arrivals_in routine to work correctly on the first pass*/ hyp.assocs = NULL; elog_init(argc, argv); elog_notify (0, "$Revision$ $Date$") ; if(argc < 2) usage(argv[0]); orbname = argv[1]; for(i=2;i<argc;++i) { if(!strcmp(argv[i],"-pf")) { ++i; pffile = argv[i]; } else if(!strcmp(argv[i],"-S")) { ++i; statefile = argv[i]; } else { /* For this kind of program it seems wise to make it a fatal error to have the arguments botched */ elog_complain(0,"Unrecognized argument %s\n",argv[i]); usage(argv[0]); } } /* set default this way*/ if(pffile == NULL) pffile = strdup(DEFAULT_PFFILE); if(statefile == NULL) statefile = strdup(DEFAULT_STATEFILE); /* parse parameter file and form all the genloc control and internal static data structures */ i = pfread(pffile,&pf); if(i != 0) elog_die(1,"Pfread error\n"); o = parse_options_pf (pf); arr_sta = load_station_table(pf); arr_a = load_array_table(pf); arr_phase = parse_phase_parameter_file(pf); /* Note this is a slightly different use of these variables than that used by other genloc routines. Here we use it like a coordinate system origin to select range of distances to use. We actually reset these again in the location function, but check them here to make sure these variables are in the parameter space. pfget_double will cause the program to die if these aren't defined.*/ origin.lat = pfget_double(pf,"center_latitude"); origin.lon = pfget_double(pf,"center_longitude"); origin.z = 0.0; rt_opts = parse_rt_options(pf); if(dbopen(rt_opts.work_db,"r+",&master_db ) == dbINVALID) elog_die(1,"Unable to open master database %s\n", rt_opts.work_db); /* Now we open the orb server connections */ if( (orbin=orbopen(orbname,"r&")) < 0) elog_die(0,"Cannot open ring buffer %s for reading\n",orbname); if(orbselect(orbin,"/db/event|/db/origin|/db/assoc|/db/arrival") < 0) elog_die(0,"Cannot select any db records from ring buffer %s\n", orbname); /* These are the state saving routines. quit is set nonzero whenever the program catches a signal. We call bury below when this happens. exhume_state is a function because I expect it could be used again */ exhume_rcode = exhume ( statefile, 0, 0, 0 ); exhume_state(exhume_rcode); if ( orbresurrect ( orbin, &last_pktid, &last_pkttime ) == 0 ) elog_complain( 0, "resurrection successful: repositioned to pktid #%d\n", last_pktid ) ; else { orbseek (orbin, ORBOLDEST); last_pktid = orbtell(orbin); elog_complain( 0, "resurrection unsuccessful\nStarting at beginning of current orb at packet id %d\n",last_pktid ) ; } /* The following is basically a trick to create a db pointer that never references any tables. This is the preferred approach for orbpkt2db records which utilize the scratch record of this database pointer. The fact that we destroy the file this creates turns out to be a feature of datascope we can exploit here. */ if (maketmpdb ("css3.0", &dbtmp, dbname) < 0) { elog_complain(0, "maketmpdb() error.\n"); exit (1); } /* This little routine initilizes the null record for each table used here. This was necessary because we assemble records in the scratch record. This sets proper nulls in fields that are not referenced by this program. */ if(initialize_scratch_records(dbtmp) == dbINVALID) elog_complain(0,"Warning: errors initializing null records in tables. May generate invalid data in some fields\n"); /* unlink (dbname); */ if( (orbout=orbopen(orbname,"w&")) < 0) elog_die(0,"Cannot open ring buffer %s for writing\n",orbname); /* This loop is broken only by an error. We call bury after each event is processed saving the current packet id. This should effectively skip events that cause orbgenloc to die for some reason. */ while(1) { int return_code; return_code = orb_arrivals_in(orbin, dbtmp, &hyp, &last_pktid,rt_opts); if(return_code) { if(return_code < 0) elog_complain(0,"Error reading db records from orb\nCurrent event skipped\n"); else elog_complain(0,"Sequencing error reading db packets from orbassoc.\nOne or more events were probably skipped\n"); continue; } if(bury()) elog_complain(0, "bury failed writing statefile %s\n",statefile); compute_location(o,rt_opts,arr_sta,arr_a,arr_phase, pf,master_db, dbtmp, hyp, orbout); /* when last_pktid is -1 orb_arrivals_in does not do an orbseek, so we always reset it here */ last_pktid = -1; /* This is the only appropriate place to release this space. This block is malloced in orb_arrivals_in*/ free(hyp.assocs); hyp.assocs = NULL; } }
void main(int argc, char **argv) { Dbptr db, dbv, dbge, dbgg; /*ensemble_mode is primary grouping, grouping_on set if secondary on*/ int ensemble_mode,grouping_on; Tbl *ensemble_keys; Tbl *group_keys; int i; char *pfi=NULL; Pf *pf,*pfo_head,*pfo_total; Tbl *process_list; int dryrun=0; char *sift_exp=NULL; int sift; /*Attributes listed in require will abort program if missing passthrough parameters will generate an error message but not cause the program to abort. Both contain pointers to Attribute_map*/ Tbl *require,*passthrough; Attribute_map *amap; FILE *fp; Tbl *error_list; char *tag; int sleep_time; DB2PFS_verbose=0; /*crack the command line*/ if(argc<3) usage(); elog_init(argc,argv); if(dbopen(argv[1],"r",&db)==dbINVALID) { elog_complain(0,"dbopen failed on database %s\n",argv[1]); usage(); } for(i=3;i<argc;++i) { if(!strcmp(argv[i],"-pf")) { ++i; if(i>argc) usage(); pfi=argv[i]; } else if(!strcmp(argv[i],"-V")) usage(); else if(!strcmp(argv[i],"-v")) DB2PFS_verbose=1; else if(!strcmp(argv[i],"-n")) dryrun=1; else if(!strcmp(argv[i],"-sift")) { ++i; if(i>argc) usage(); sift=1; sift_exp=argv[i]; } else usage(); } if(!dryrun) { fp=fopen(argv[2],"r+"); if(fp==NULL) usage(); } if(pfi==NULL) pfi=strdup("db2pfstream"); if(pfread(pfi,&pf)) elog_die(0,"Error reading parameter file %s.pf\n",pfi); sleep_time=pfget_int(pf,"sleep_time"); /* The output view gets a virtual table name that tags the overall collection of stuff. This comes from the parameter file to make this program general, BUT it must be coordinated with the reader code. */ tag = pfget_string(pf,"virtual_table_name"); if(tag==NULL)elog_die(0,"Required parameter (virtual_table_name) missing from parameter file\n"); ensemble_mode = pfget_boolean(pf,"ensemble_mode"); if(ensemble_mode) { ensemble_keys=pfget_tbl(pf,"ensemble_keys"); if(ensemble_keys==NULL) elog_die(0, "ensemble_mode is on, but no grouping keys defined\nCheck parameter file\n"); group_keys=pfget_tbl(pf,"group_keys"); if(group_keys==NULL) grouping_on = 0; else { if(maxtbl(group_keys)>0) grouping_on = 1; else grouping_on = 0; } } if(DB2PFS_verbose && ensemble_mode) { char sm[128]; char *key; strcpy(sm,"Defining ensemble with keys: "); for(i=0;i<maxtbl(ensemble_keys);++i) { key=gettbl(ensemble_keys,i); strcat(sm,key); } elog_log(0,"%s\n",sm); if(grouping_on) { strcpy(sm,"Grouping ensemble with keys: "); for(i=0;i<maxtbl(group_keys);++i) { key = gettbl(group_keys,i); strcat(sm,key); } elog_log(0,"%s\n",sm); } } /*This function calls dbprocess using a tbl from pf*/ dbv=dbform_working_view(db,pf,"dbprocess_list"); if(dbv.record==dbINVALID) elog_die(0,"dbprocess failed: check database and parameter file parameter dbprocess_list\n"); if(sift_exp!=NULL)dbv=dbsubset(dbv,sift_exp,0); /*Now we form the groupings if needed */ if(ensemble_mode) { dbge = dbgroup(dbv,ensemble_keys,"ensemble",1); if(dbge.record == dbINVALID) elog_die(0,"dbgroup with ensemble keys failed\n"); if(grouping_on) { dbgg = dbgroup(dbv,group_keys,"groups",2); if(dbgg.record == dbINVALID) elog_die(0,"dbgroup on secondary grouping keys failed\n"); } } /*This builds the maps of which attributes will be saved */ require = pfget_Attribute_map(pf,"require"); if(require==NULL) elog_die(0,"Error in parameter file for parameter require\n"); passthrough = pfget_Attribute_map(pf,"passthrough"); if(passthrough==NULL) elog_die(0,"Error in parameter file for parameter passthrough\n"); /* Unfortunately, we have two different loops for ensemble mode and single object mode */ if(ensemble_mode) { int number_ensembles; Dbptr db_bundle_1; Dbptr db_bundle_2; int is_ensemble, ie_ensemble; int isg, ieg; int number_groups; pfo_head=pfnew(0); pfput_tbl(pfo_head,"ensemble_keys",ensemble_keys); if(grouping_on) pfput_tbl(pfo_head,"group_keys",group_keys); dbquery(dbge,dbRECORD_COUNT,&number_ensembles); if(DB2PFS_verbose) elog_log(0,"database has %d ensembles\n", number_ensembles); if(grouping_on) { dbgg.record=0; dbquery(dbgg,dbRECORD_COUNT,&number_groups); if(DB2PFS_verbose) elog_log(0,"database has %d subgroups\n", number_groups); } for(dbge.record=0;dbge.record<number_ensembles; ++dbge.record) { Pf_ensemble *pfe; int nmembers; char *grp_records; Tbl *grplist; dbgetv(dbge,0,"bundle",&db_bundle_1,0); dbget_range(db_bundle_1,&is_ensemble,&ie_ensemble); nmembers = ie_ensemble-is_ensemble; /* pfe does not need to hold the group records for this application so the ngroups variable is passed as 0*/ pfe=create_Pf_ensemble(nmembers,0); /*Now loop through and load the array of pf's with parameters defined by the attribute maps*/ for(i=0,dbv.record=is_ensemble;i<nmembers; ++i,++dbv.record) { pfe->pf[i]=pfnew(PFARR); error_list=db2pf(dbv,require,pfe->pf[i]); if(maxtbl(error_list)>0) { if(dryrun) { elog_log(0,"Ensemble %d at input database view record %d lacks required attributes\n", dbge.record,dbv.record); log_error_list(error_list); } else { fprintf(fp,"%s\n",END_OF_DATA_SENTINEL); fflush(fp); elog_log(0,"FATAL ERROR: ensemble %d at input database view record %d lacks required attributes\nOUTPUT DATA STREAM TRUNCATED\n", i,dbv.record); log_error_list(error_list); exit(-1); } } freetbl(error_list,free); error_list=db2pf(dbv,passthrough,pfe->pf[i]); if(DB2PFS_verbose) { elog_log(0,"Warning: null passthrough attributes for ensemble %d at row %d\n", dbge.record,dbv.record); log_error_list(error_list); } freetbl(error_list,free); pfput_boolean(pfe->pf[i],"data_valid",1); } if(grouping_on) { grplist=newtbl(0); do { dbgetv(dbgg,0,"bundle",&db_bundle_2,0); dbget_range(db_bundle_2,&isg,&ieg); grp_records=malloc(30); sprintf(grp_records,"%d %d", isg-is_ensemble,ieg-is_ensemble-1); pushtbl(grplist,grp_records); ++dbgg.record; } while (ieg<ie_ensemble); pfput_tbl(pfo_head,"group_records",grplist); } pfo_total=build_ensemble(1,tag,pfo_head,pfe); free_Pf_ensemble(pfe); if(!dryrun) { pfout(fp,pfo_total); fprintf(fp,"%s\n",ENDPF_SENTINEL); fflush(fp); } pffree(pfo_total); if(grouping_on)freetbl(grplist,free); } } else { /*This is the simpler, single block mode */ int nrecords; Pf *pfo; dbquery(dbv,dbRECORD_COUNT,&nrecords); pfo=pfnew(PFARR); for(dbv.record=0;dbv.record<nrecords;++dbv.record) { error_list=db2pf(dbv,require,pfo); if(maxtbl(error_list)>0) { if(dryrun) { elog_log(0,"Input database view at row %d lacks required parameters\n", dbv.record); log_error_list(error_list); } else { fprintf(fp,"%s\n",END_OF_DATA_SENTINEL); fflush(fp); elog_log(0,"FATAL: input database view at row %d lacks required parameters\n", dbv.record); log_error_list(error_list); exit(-1); } } freetbl(error_list,free); error_list=db2pf(dbv,passthrough,pfo); pfput_boolean(pfo,"data_valid",1); if(DB2PFS_verbose) { elog_log(0,"Warning: null passthrough attributes for row %d of input database view\n", dbv.record); } if(!dryrun) { pfout(fp,pfo); fprintf(fp,"%s\n",ENDPF_SENTINEL); } } } if(!dryrun) { fprintf(fp,"%s\n",END_OF_DATA_SENTINEL); fflush(fp); sleep(sleep_time); fclose(fp); } exit(0); }
int main(int argc, char **argv) { double modified_after =now() , last_lddate, last_mtime, mtime; char *orbname = NULL; char *dbname = NULL; int orb; int naptime = -1, check_lddate_interval = -1; Dbptr db, dbt, dbs; char *prefix = NULL; struct stat filestat; int i; Tbl *tablenames, *tables_containing_dfile, *check_tables = NULL, *ignore_tables = NULL; long table_present, recc, is_view; char *tablename, *schemaname; char *filename; int counter = 0, force_check = 0; char expr[512]; char *statefilename = NULL, *pfname = "dbnew2orb"; Pf *pf = NULL; void *priv_dfile = (void *) NULL; void *private = (void *) NULL; int pmsi; /* poor man's string index, replacment for * searchtbl... */ char *pmsp; double lastburytime; Relic relic; char *s; Expression *expr_lddate; double *mtimes; double *lddates; elog_init(argc, argv); if (argc < 2) { usage(); exit(1); } for (argc--, argv++; argc > 0; argc--, argv++) { if (!strcmp(*argv, "-modified_after")) { argc--; argv++; if (argc < 1) { complain(0, "Need -modified_after argument.\n"); usage(); exit(1); } modified_after = str2epoch(*argv); } else if (!strcmp(*argv, "-prefix")) { argc--; argv++; if (argc < 1) { complain(0, "Need -prefix argument.\n"); usage(); exit(1); } prefix = *argv; } else if (!strcmp(*argv, "-pf")) { argc--; argv++; if (argc < 1) { complain(0, "Need -pf argument.\n"); usage(); exit(1); } pfname = *argv; } else if (!strcmp(*argv, "-state")) { argc--; argv++; if (argc < 1) { complain(0, "Need -state argument.\n"); usage(); exit(1); } statefilename = *argv; } else if (!strcmp(*argv, "-sleep")) { argc--; argv++; if (argc < 1) { complain(0, "Need -sleep argument.\n"); usage(); exit(1); } naptime = atoi(*argv); } else if (!strcmp(*argv, "-check_lddate_interval")) { argc--; argv++; if (argc < 1) { complain(0, "Need -check_lddate_interval argument.\n"); usage(); exit(1); } check_lddate_interval = atoi(*argv); } else if (!strcmp(*argv, "-v")) { verbose++; } else if (**argv != '-') { break; } else { complain(0, "Unrecognized argument '%s'.\n", *argv); usage(); exit(1); } } if (pfread(pfname, &pf)) { elog_die(0, "parse_pf: pfread('%s') error.\n", pfname); } if (check_lddate_interval < 1) { if (parse_param(pf, "check_lddate_interval", P_LINT, 1, &check_lddate_interval) < 0) { elog_die(1, "parse_pf: sleep check_lddate_interval needed!\n"); } else { if (check_lddate_interval < 0) { check_lddate_interval = 1; } } } if (naptime < 1) { if (parse_param(pf, "sleep", P_LINT, 1, &naptime) < 0) { elog_die(1, "parse_pf: sleep value needed!\n"); } else { if (naptime < 0) { naptime = 1; } } } if (!prefix) { if (parse_param(pf, "prefix", P_STR, 0, &prefix) < 0) { printf("NO PREFIX!\n"); prefix = NULL; } } parse_param(pf, "check_tables", P_TBL, 0, &check_tables); if (check_tables) { if (maxtbl(check_tables) < 1) { freetbl(check_tables, 0); check_tables = NULL; } } parse_param(pf, "ignore_tables", P_TBL, 0, &ignore_tables); if (ignore_tables) { if (maxtbl(ignore_tables) < 1) { freetbl(ignore_tables, 0); ignore_tables = NULL; } } /* * no good here, would erase the table above pffree(pf); */ if (argc < 1) { complain(0, "Need db argument.\n"); usage(); exit(1); } dbname = *argv; argc--; argv++; if (argc < 1) { complain(0, "Need orb argument.\n"); usage(); exit(1); } orbname = *argv; argc--; argv++; if (argc > 0) { complain(0, "Unrecognized argument '%s'.\n", *argv); usage(); exit(1); } if (dbopen(dbname, "r", &db) < 0) { elog_complain(0, "Can't open database"); exit(1); } dbquery(db, dbSCHEMA_NAME, &schemaname); orb = orbopen(orbname, "w&"); if (orb < 0) { elog_die(0, "orbopen(%s) error\n", orbname); } /* * prepare for later call to dbquery(dbFIELD_TABLES) to find only * tables containing lddate */ /* * dbtables is much better, does not require the existence of table * origin dbf = dblookup(db, 0, "origin", "lddate", "dbNULL"); * dbquery(dbf, dbFIELD_TABLES, &tablenames); */ dbex_compile(db, "max(lddate)", &expr_lddate, dbTIME); tablenames = dbtables(db, "lddate"); tables_containing_dfile = dbtables(db, "dfile"); /* waste a few bytes... */ ntables = maxtbl(tablenames); mtimes = malloc(ntables * sizeof(double)); lddates = malloc(ntables * sizeof(double)); bury_times = malloc(ntables * sizeof(double)); static_flags = malloc(ntables * sizeof(long)); if (statefilename) { if (exhume(statefilename, &Stop, 10, mortician)) { elog_notify(0, "read old state file\n"); } else { elog_complain(0, "could not read old statefile\n"); } } for (i = 0; i < ntables; i++) { /* * mtimes[i] = modified_after; lddates[i] = modified_after; */ static_flags[i] = NEW_TABLE; } for (;;) { tablenames = dbtables(db, "lddate"); for (i = 0; i < ntables; i++) { tablename = gettbl(tablenames, i); if (!tablename) { continue; } dbt = dblookup(db, 0, tablename, 0, 0); dbquery(dbt, dbTABLE_PRESENT, &table_present); if (!table_present) { continue; } dbquery(dbt, dbTABLE_IS_VIEW, &is_view); if (is_view) { continue; } /* lastid is not a good idea (my personal choice)... */ if (strcmp(tablename, "lastid") == 0) { continue; } /* remove after Dan fixed the bug with remark */ if (strcmp(tablename, "remark") == 0) { continue; } if (findtbl(tablename, tables_containing_dfile)) { continue; } if (check_tables) { if (!findtbl(tablename,check_tables)) { if (verbose > 1 && static_flags[i]==NEW_TABLE) elog_notify(0,"ignoring table %s because it's NOT in 'check_tables'\n",tablename); continue; } } if (ignore_tables) { if (findtbl(tablename,ignore_tables)) { if (verbose > 1 && static_flags[i]==NEW_TABLE) elog_notify(0,"ignoring table %s because it's in 'ignore_tables'\n",tablename); continue; } } dbquery(dbt, dbRECORD_COUNT, &recc); if (recc < 1) { continue; } if (statefilename) { if (static_flags[i] == NEW_TABLE) { relic.dp = &bury_times[i]; if (resurrect(tablename, relic, TIME_RELIC) == 0) { mtimes[i] = bury_times[i]; lddates[i] = bury_times[i]; if (verbose > 1) { elog_notify(0, "resurrection successful: check %s after %s\n", tablename, s = strtime(bury_times[i])); free(s); } } else { bury_times[i] = modified_after; mtimes[i] = modified_after; lddates[i] = modified_after; if (verbose > 1) { elog_notify(0, "resurrection unsuccessful: check %s after %s\n", tablename, s = strtime(modified_after)); free(s); } } static_flags[i] = TABLE_SEEN; } } else { if (static_flags[i] == NEW_TABLE) { bury_times[i] = modified_after; mtimes[i] = modified_after; lddates[i] = modified_after; static_flags[i] = TABLE_SEEN; } } dbquery(dbt, dbTABLE_FILENAME, &filename); if (stat(filename, &filestat) < 0) { elog_die(1, "stat(%s) error.\n", filename); } last_mtime = mtimes[i]; last_lddate = lddates[i]; mtime = filestat.st_mtime; /* * the whole mtime stuff is not soo good: mtime is * typically > lddate, so setting modified_after to * mtime will certainly ignore the last value. To get * everything, I will have to keep 2 arrays: mtimes * to detect file modifications and lddates to get * the actual entries... */ if (force_check || mtime > last_mtime) { sprintf(expr, "lddate > %f", last_lddate); dbs = dbsubset(dbt, expr, 0); dbquery(dbs, dbRECORD_COUNT, &recc); if (recc > 0) { if (dbrows2orb(dbs, orb, prefix) == 0) { /* * dbex_evalstr(dbs, * "max(lddate)", dbTIME, * &lddates[i]); */ dbex_eval(dbs, expr_lddate, 0, &lddates[i]); mtimes[i] = mtime; bury_times[i] = lddates[i]; } } dbfree(dbs); } /* * a call to dbfree(dbt) would remove it from the * list of tablenames, all later calls to tablename * would return NIL... */ if (Stop) { bury(); return (0); } } sleep(naptime); if ((counter + 1) >= check_lddate_interval) { counter = 0; force_check = 1; } else { force_check = 0; counter++; } if (statefilename) { double nowtime; nowtime = now(); if (nowtime - lastburytime > 600.0) { lastburytime = nowtime; bury(); } } } }
ESR_ReturnCode PFileReadLCHAR(PFile* self, LCHAR* value, size_t len) { size_t i, bufferSize, count, totalRead = 0; ESR_ReturnCode rc = ESR_SUCCESS; /* Skip whitespace before token */ do { count = pfread(value, sizeof(LCHAR), len, self); totalRead += count; if (count < len) { if (pferror(self)) { rc = ESR_READ_ERROR; PLogError(ESR_rc2str(rc)); goto CLEANUP; } else { rc = ESR_INVALID_STATE; PLogError(L("%s: reached end of file before finding token"), ESR_rc2str(rc)); goto CLEANUP; } } /* locate first non-whitespace character */ for (i = 0; i < count && LISSPACE(value[i]); ++i); } while (i == count); bufferSize = count - i; /* Fill remainder of buffer */ if (bufferSize < len) { count = pfread(value + bufferSize, sizeof(LCHAR), len - bufferSize, self); bufferSize += count; totalRead += count; if (count < len - bufferSize && pferror(self)) { rc = ESR_READ_ERROR; PLogError(ESR_rc2str(rc)); goto CLEANUP; } } /* locate first whitespace character */ for (i = 0; i < bufferSize && !LISSPACE(value[i]); ++i); if (i < bufferSize) { /* unread anything after the token */ if (PFileSeek(self, -(int)(bufferSize - i), SEEK_CUR)) { rc = ESR_SEEK_ERROR; PLogError(ESR_rc2str(rc)); } totalRead -= bufferSize - i; value[i] = L('\0'); } return rc; CLEANUP: if (PFileSeek(self, - (int) count, SEEK_CUR)) PLogError(L("ESR_SEEK_ERROR")); return rc; }
ESR_ReturnCode PFileReadInt(PFile* self, int* value) { LCHAR number[MAX_INT_DIGITS+1]; size_t i, bufferSize, count, totalRead = 0; ESR_ReturnCode rc; /* Skip whitespace before token */ do { count = pfread(number, sizeof(LCHAR), MAX_INT_DIGITS, self); totalRead += count; if (count < MAX_INT_DIGITS) { if (pferror(self)) { rc = ESR_READ_ERROR; PLogError(ESR_rc2str(rc)); goto CLEANUP; } else { rc = ESR_INVALID_STATE; PLogError(L("%s: reached end of file before finding token"), ESR_rc2str(rc)); goto CLEANUP; } } /* locate first non-whitespace character */ for (i = 0; i < count && LISSPACE(number[i]); ++i); } while (i == count); bufferSize = count - i; /* Fill remainder of buffer */ if (bufferSize < MAX_INT_DIGITS) { count = pfread(number + bufferSize, sizeof(LCHAR), MAX_INT_DIGITS - bufferSize, self); bufferSize += count; totalRead += count; if (count < MAX_INT_DIGITS - bufferSize && pferror(self)) { rc = ESR_READ_ERROR; PLogError(ESR_rc2str(rc)); goto CLEANUP; } } /* locate first whitespace character */ for (i = 0; i < bufferSize && !LISSPACE(number[i]); ++i); if (i < bufferSize) { /* unread anything after the token */ if (PFileSeek(self, - (int)(bufferSize - i), SEEK_CUR)) { rc = ESR_SEEK_ERROR; PLogError(ESR_rc2str(rc)); } totalRead -= bufferSize - i; number[i] = L('\0'); } if (number[0] != L('-') && !LISDIGIT(number[0])) { rc = ESR_INVALID_STATE; PLogError(L("%s: token was not number (%s)"), ESR_rc2str(rc), number); goto CLEANUP; } CHKLOG(rc, lstrtoi(number, value, 10)); return rc; CLEANUP: if (PFileSeek(self, - (int) count, SEEK_CUR)) PLogError(L("ESR_SEEK_ERROR")); return rc; }
static bool_t DEHT_allocEmptyLocationInBucket(DEHT * ht, ulong_t bucketIndex, byte_t * blockDataOut, ulong_t blockDataLen, DEHT_DISK_PTR * blockDiskPtr, ulong_t * firstFreeIndex) { bool_t ret = FALSE; DEHT_DISK_PTR newBlock = 0; TRACE_FUNC_ENTRY(); CHECK(NULL != ht); CHECK(bucketIndex < ht->header.numEntriesInHashTable); CHECK(NULL != blockDataOut); CHECK(KEY_FILE_BLOCK_SIZE(ht) <= blockDataLen); CHECK(NULL != blockDiskPtr); CHECK(NULL != firstFreeIndex); CHECK(DEHT_findLastBlockForBucket(ht, bucketIndex, blockDiskPtr)); if (0 == *blockDiskPtr) { /* if this is the first ever block, call DEHT_findFirstBlockForBucketAndAlloc() which will also alloc the first block */ CHECK(DEHT_findFirstBlockForBucketAndAlloc(ht, bucketIndex, blockDiskPtr)); CHECK(0 != *blockDiskPtr); } /* read it */ CHECK_MSG(ht->sKeyfileName, (pfread(ht->keyFP, *blockDiskPtr, blockDataOut, KEY_FILE_BLOCK_SIZE(ht)))); /* get the current used block count */ *firstFreeIndex = GET_USED_RECORD_COUNT(blockDataOut); /* see if this block is full */ if (*firstFreeIndex >= ht->header.nPairsPerBlock) { newBlock = DEHT_allocKeyBlock(ht); CHECK(0 != newBlock); /* update old block */ SET_NEXT_BLOCK_PTR(ht, blockDataOut, newBlock); /* update last block reference if cache is present */ if (NULL != ht->hashPointersForLastBlockImageInMemory) { ht->hashPointersForLastBlockImageInMemory[bucketIndex] = newBlock; } CHECK_MSG(ht->sKeyfileName, (pfwrite(ht->keyFP, *blockDiskPtr, blockDataOut, KEY_FILE_BLOCK_SIZE(ht)))); /* now, just return the reference to the start of the new block */ memset(blockDataOut, 0, KEY_FILE_BLOCK_SIZE(ht)); *blockDiskPtr = newBlock; *firstFreeIndex = 0; } /* increase used pairs count by one. If & when the user will update the block on disk, this will be updated as well */ SET_USED_RECORD_COUNT(blockDataOut, GET_USED_RECORD_COUNT(blockDataOut) + 1); ret = TRUE; goto LBL_CLEANUP; LBL_ERROR: *blockDiskPtr = 0; *firstFreeIndex = 0; ret = FALSE; TRACE_FUNC_ERROR(); LBL_CLEANUP: TRACE_FUNC_EXIT(); return ret; }
static short load_short(PFile* fp) { short v; pfread(&v, sizeof(short), 1, fp); return v; }
static void parse_orbname( char *orbname, char *orb_address, int *orb_port ) { char *split_orbname; Tbl *orbname_parts; char orbname_port[STRSZ]; Hook *hook = 0; static Pf *pfnames = 0; int len = 0; if( STREQ( orbname, ":" ) ) { strcpy( orb_address, "127.0.0.1" ); strcpy( orbname_port, "" ); } else { split_orbname = strdup( orbname ); orbname_parts = split( split_orbname, ':' ); if( maxtbl( orbname_parts ) == 1 && orbname[0] == ':' ) { strcpy( orb_address, "127.0.0.1" ); strcpy( orbname_port, poptbl( orbname_parts ) ); } else if( maxtbl( orbname_parts ) == 1 ) { strcpy( orb_address, shifttbl( orbname_parts ) ); strcpy( orbname_port, "" ); } else if( maxtbl( orbname_parts ) == 2 ) { strcpy( orb_address, shifttbl( orbname_parts ) ); strcpy( orbname_port, poptbl( orbname_parts ) ); } else { elog_complain( 0, "pforbstat: unexpected error translating orb2orb argument <%s>\n", orbname ); strcpy( orb_address, "" ); strcpy( orbname_port, "" ); } free( split_orbname ); freetbl( orbname_parts, 0 ); } if( ( len = strlen( orbname_port ) ) > 0 ) { if( orbname_port[len-1] == '@' ) { orbname_port[len-1] = '\0'; } } if( STREQ( orbname_port, "" ) ) { *orb_port = ORB_TCP_PORT; } else if( strmatches( orbname_port, "^[0-9]+$", &hook ) ) { *orb_port = atoi( orbname_port ); } else { if( pfnames == 0 ) { pfread( "orbserver_names", &pfnames ); } if( pfget_string( pfnames, orbname_port ) == 0 ) { elog_complain( 0, "pforbstat: couldn't translate orb port \":%s\"\n", orbname_port ); *orb_port = 0; } else { *orb_port = pfget_int( pfnames, orbname_port ); } } return; }
int main(int argc, char **argv) { SEGYBinaryFileHeader reel; SEGYTraceHeader *header; char *dbin; char *outfile; FILE *fp; Pf *pf; Arr *channels; /* channel order list */ Arr *table_list; /* array of valid tables */ int nchan; char *stest; float **traces; char text_file_header[SEGY_TEXT_HEADER_SIZE]; Dbptr db, trdb, dbj; Dbptr trdbss; int nsamp0; double time0, endtime0, samprate0; long int nsamp; double samprate; int i,j; char stime[30],etime[30]; char s[128]; double tlength; double phi, theta; char *newchan_standard[3]={"X1","X2","X3"}; char *trsubset="chan=~/X./"; char *newchan[3]={"R","T","Z"}; Tbl *sortkeys=newtbl(0); char sta[10],chan[10]; double lat, lon, elev, dnorth, deast, edepth; char segtype; char refsta[10]; int total_traces=0; char *time_str; long int evid,shotid=1; int rotate=0; long int ntraces; int ichan; int map_to_cdp; /* logical switch to output data like cdp stacked data */ char *fmt="%Y %j %H %M %S %s"; char *pfname; int Verbose=0; /* New features added 2009 */ /* this is a boolean. If true (nonzero) it is assumed stdin will contain four numbers: time,lat, lon, elev. If false, only the time field is read and remainder of any input on each line is dropped.*/ int input_source_coordinates; /* scale factor for source coordinates. Needed because segy uses an int to store source coordinates. Sensible choices are 3600 for arc seconds and 10000 for a pseudodecimal. Note this parameter is ignored unless input_source_coordinates is true.*/ int coordScale; /* If true use passcal 32 bit extension num_samps as record length. SEGY standard uses a 16 bit entry that easily overflows with large shots at long offset. In this ase assume the 16 bit quantity is meaningless. */ int use_32bit_nsamp; /* This is switched on by argument switch. When set to a nonzero (default) the reel headers are written. When 0 ` the reel headers will not be written -- used by seismic unix and passcal*/ int write_reel_headers=1; /* SEG-Y version to output. Default is original 1975 spec (rev 0) */ int16_t segy_format = SEGY_FORMAT_REV_0; /* dbsubset query string */ char *substr=NULL; /* text_header_description is a buffer holding a user-supplied description * to be placed in the 3200-byte text header block. It is controlled by * the parameter file value text_header_description or by the -d command * line option, with the latter taking precedence */ char* text_header_description=NULL; if(argc < 3) usage(); dbin = argv[1]; outfile = argv[2]; pfname = NULL; for(i=3;i<argc;++i) { if(!strcmp(argv[i],"-pf")) { ++i; pfname = argv[i]; } else if(!strcmp(argv[i],"-SU")) { write_reel_headers=0; } else if(!strcmp(argv[i],"-v")) { Verbose=1; } else if(!strcmp(argv[i],"-d")) { ++i; text_header_description = strdup(argv[i]); } else if(!strcmp(argv[i],"-ss")) { ++i; substr=argv[i]; } else if(!strcmp(argv[i],"-V")) { ++i; if (!strcmp(argv[i],"0")) {segy_format = SEGY_FORMAT_REV_0;} else if(!strcmp(argv[i],"1")) {segy_format = SEGY_FORMAT_REV_1_0;} else if(!strcmp(argv[i],"SU")) { segy_format = SEGY_FORMAT_SU; write_reel_headers=0; } else { elog_complain(0, "SEG-Y Version must be either 1 or 0"); usage(); } } else { usage(); } } /* Command-line parameter sanity checking */ if (write_reel_headers==0 && segy_format != SEGY_FORMAT_SU){ complain(0, "The SU option cannot be used with the -V option"); usage(); } if(pfname == NULL) pfname = strdup("db2segy"); elog_init(argc, argv); if(pfread(pfname,&pf)) { elog_die(0,"pfread error for pf file %s.pf\n",argv[0]); } /* Read the text_header_description if we weren't passed the -d option */ if (!text_header_description) { text_header_description=pfget_string(pf, "text_header_description"); } /* rotation parameters */ rotate=pfget_boolean(pf,"rotate"); if(rotate) { phi = pfget_double(pf,"phi"); theta = pfget_double(pf,"theta"); } /* This function creates the channel order list keyed by station channel names */ channels = build_stachan_list(pf,&nchan,Verbose); map_to_cdp = pfget_boolean(pf,"map_to_cdp"); if(map_to_cdp && Verbose) elog_notify(0,"Casting data as CDP stacked section\n"); if(dbopen(dbin,"r",&db) == dbINVALID) { elog_complain(1,"Cannot open db %s\n", dbin); usage(); } /* We grab the sample rate and trace length (in seconds) and use this to define global sample rates for the data. SEG-Y REV0 REQUIRES fixed length records and sample rates, so irregular sample rates will cause this program to die. One could add a decimate/interpolate function, but this is not currently implemented */ samprate0 = pfget_double(pf,"sample_rate"); tlength = pfget_double(pf,"trace_length"); nsamp0 = (int)(tlength*samprate0); use_32bit_nsamp=pfget_boolean(pf,"use_32bit_nsamp"); if (ntohs(segy_format) >= 0x0100 && use_32bit_nsamp) { elog_complain(0,"The 32-bit extension field is incompatible with SEG-Y REV 1. Ignoring 'use_32bit_nsamp' from the parameter file"); use_32bit_nsamp=0; } /* nsamp in segy is a 16 bit field. Handling depends on setting of use_32bit_nsamp boolean */ if(nsamp0 > SEGY_MAX_NSAMP) { if(use_32bit_nsamp) { elog_notify(0,"Warning: segy uses a 16 bit entity to store number of samples\nThat field is garbage. Using the 32 bit extension field."); } else { elog_complain(0, "Warning: segy uses a 16 bit entity to store number of samples. Requested %d samples per trace. Trucated to %d", nsamp0, SEGY_MAX_NSAMP); nsamp0 = SEGY_MAX_NSAMP; } } /* boolean. When nonzero set coordinates as geographic arc seconds values */ int use_geo_coordinates=pfget_boolean(pf,"use_geo_coordinates"); /* boolean. When nonzero, output decimal degrees instead of arcseconds if * the requested output format supports it (rev1 only) */ int prefer_decimal_degrees=pfget_boolean(pf, "prefer_decimal_degrees"); /* We now have enough information to decide the coordUnits for all traces */ int coordUnits = 0; if (!use_geo_coordinates) { coordUnits=SEGY_TRACE_COORDUNITS_LENGTH; } else if (ntohs(segy_format) >= 0x0100 && prefer_decimal_degrees) { coordUnits=SEGY_TRACE_COORDUNITS_DECIMAL_DEGREES; } else { coordUnits=SEGY_TRACE_COORDUNITS_ARCSECONDS; } /* We should have set our coordinate units now */ assert(coordUnits!=0); input_source_coordinates=pfget_boolean(pf,"input_source_coordinates"); if(input_source_coordinates) { coordScale=pfget_int(pf,"coordinate_scale_factor"); } else if (coordUnits==SEGY_TRACE_COORDUNITS_DECIMAL_DEGREES) { /* Use a sane scalar for decimal degrees. 10000 gives four decimal * places of accuracy, which matches the CSS3.0 spec for lat and lon */ coordScale=10000; } else { coordScale=1; } /* Print a diagnostic message if the user gave a sub-optimal value for the * coordScale */ if (coordUnits == SEGY_TRACE_COORDUNITS_DECIMAL_DEGREES && coordScale < 10000) { elog_alert(0, "The supplied parameter 'coordinate_scale_factor' value of %d is less than 10000, and will cause loss of precision for decimal degree coordinates.", coordScale); } else if (coordUnits == SEGY_TRACE_COORDUNITS_ARCSECONDS) { if (coordScale > 1000) { elog_alert(0, "The supplied parameter 'coordinate_scale_factor' value of %d is greater than 1000, and will cause loss of precision for arcsecond coordinates.", coordScale); } } /* trace_gain_type: signed int */ int16_t trace_gain_type = pfget_int(pf,"trace_gain_type"); if (trace_gain_type < 0) { die(0, "The trace_gain_type must be zero or greater"); } else { trace_gain_type=htons(trace_gain_type); } /* check list of tables defined in pf. Return array of logicals that define which tables are valid and join tables. */ table_list = check_tables(db,pf); check_for_required_tables(table_list); dbj = join_tables(db,pf,table_list); if(dbj.record == dbINVALID) elog_die(0,"dbjoin error\n"); if(substr!=NULL) dbj=dbsubset(dbj,substr,0); long int ndbrows; dbquery(dbj,dbRECORD_COUNT,&ndbrows); if(ndbrows<=0) { elog_complain(1,"Working database view is empty\n"); if(substr!=NULL) elog_complain(0,"Subset condtion =%s a likely problem\n", substr); usage(); } fp = fopen(outfile,"w"); if(fp == NULL) { elog_complain(0,"Cannot open output file %s\n",outfile); usage(); } /* These are needed for sort below */ pushtbl(sortkeys,"sta"); pushtbl(sortkeys,"chan"); /* Set up and write the Textual File Header */ initialize_text_header(text_file_header, segy_format, text_header_description); if(write_reel_headers){ if ( fwrite(text_file_header,1,SEGY_TEXT_HEADER_SIZE,fp) \ != SEGY_TEXT_HEADER_SIZE ) { elog_die(1,"An error occurred writing the textual file header"); } } /* memory allocation for trace data. This is a large matrix that is cleared for each event. This model works because of segy's fixed length format.*/ traces = calloc(nchan, sizeof(float*)); if(traces == NULL) elog_die(1,"out of memory"); for (int r = 0; r < nchan; r++) { traces[r] = calloc(nsamp0, sizeof(float)); if(traces[r] == NULL) elog_die(1,"out of memory"); } header = (SEGYTraceHeader *)calloc((size_t)nchan,sizeof(SEGYTraceHeader)); if(header == NULL) elog_die(0,"Cannot alloc memory for %d segy header workspace\n",nchan); if(write_reel_headers) { if (Verbose) { elog_debug(0,"Binary Headers - Using segy_format code 0x%04X\n", ntohs(segy_format)); } initialize_binary_file_header(&reel, segy_format); /* now fill in the binary reel header and write it */ reel.kjob = htonl(1); reel.kline = htonl(1); reel.kreel = htonl(1); reel.kntr = htons((int16_t)nchan); reel.knaux = htons(0); reel.sr = htons((int16_t)(1000000.0/samprate0)); reel.kfldsr = reel.sr; reel.knsamp = htons((int16_t)nsamp0); reel.kfsamp = htons((int16_t)nsamp0); reel.dsfc = htons(5); /* This is ieee floats*/ reel.kmfold = htons(0); if(map_to_cdp) reel.ksort = htons(2); else reel.ksort = htons(1); reel.kunits = htons(1); /* This sets units to always be meters */ if(fwrite((void *)(&reel),sizeof(SEGYBinaryFileHeader),1,fp) != 1) { elog_die(1,"Write error for binary reel header"); } } /* Now we enter a loop over stdin reading start times. Program will blindly ask for data from each start time to time+tlength. The trace buffer will be initialized to zeros at the top of the loop always. If nothing is found only zeros will be written to output. */ while((stest=fgets(s,80,stdin)) != NULL) { double slat,slon,selev; /* Used when reading source location*/ if(Verbose) elog_notify(0,"Processing: %s\n",s); for(i=0;i<nchan;++i) { initialize_trace_header(&(header[i]), segy_format); header[i].gainType = trace_gain_type; header[i].lineSeq = htonl(total_traces + i + 1); header[i].reelSeq = header[i].lineSeq; if(map_to_cdp) { header[i].cdpEns = htonl(i + 1); header[i].traceInEnsemble = htonl(1);/* 1 trace per cdp faked */ } else { header[i].channel_number = htonl(i + 1); } header[i].event_number = htonl(shotid); header[i].energySourcePt = htonl(shotid); for(j=0;j<nsamp0;++j) traces[i][j] = htonf((Trsample)0.0); } if(input_source_coordinates) { char stmp[40]; sscanf(s,"%s%ld%lf%lf%lf",stmp,&shotid,&slon,&slat,&selev); time0=str2epoch(stmp); if(coordUnits == SEGY_TRACE_COORDUNITS_ARCSECONDS) { slat*=3600.0; slon*=3600.0; } slat *= (double)coordScale; slon *= (double)coordScale; } else { time0 = str2epoch(s); } endtime0 = time0 + tlength; sprintf(stime,"%20.4f",time0); sprintf(etime,"%20.4f",endtime0); trdb.database = -1; if(trload_css(dbj,stime,etime,&trdb,0, 0) < 0) { if(Verbose) { elog_notify(0,"trload_css failed for shotid=%ld",shotid); elog_notify(0," No data in time range %s to %s\n", strtime(time0),strtime(endtime0) ); elog_notify(0,"No data written for this shotid block."); elog_notify(0," Handle this carefully in geometry definitions.\n"); } continue; } /* This does gap processing */ repair_gaps(trdb); trapply_calib(trdb); if(rotate) { if(rotate_to_standard(trdb,newchan_standard)) elog_notify(0,"Data loss in rotate_to_standard for event %s to %s\n", stime, etime); /* This is need to prevent collisions of channel names */ trdbss = dbsubset(trdb,trsubset,0); if(trrotate(trdbss,phi,theta,newchan)) elog_notify(0,"Data loss in trrotate for event %s to %s\n", stime, etime); } if(Verbose) elog_notify(0,"Station chan_name chan_number seq_number shotid evid\n"); trdb = dbsort(trdb,sortkeys,0,0); dbquery(trdb,dbRECORD_COUNT,&ntraces); if(Verbose) elog_debug(0,"Read %ld traces for event at time%s\n", ntraces,strtime(time0)); for(trdb.record=0;trdb.record<ntraces;++trdb.record) { Trsample *trdata; if(dbgetv(trdb,0, "evid",&evid, "sta",sta, "chan",chan, "nsamp", &nsamp, "samprate",&samprate, "data",&trdata, "lat", &lat, "lon", &lon, "elev",&elev, "refsta",refsta, "dnorth",&dnorth, "deast",&deast, "edepth",&edepth, "segtype",&segtype, NULL) == dbINVALID) { elog_complain(0," dbgetv error reading record %ld. Trace will be skipped for station %s and channel %s", trdb.record,sta,chan); continue; } /* Allow 1 percent samprate error before killing */ double fsrskew=fabs((samprate-samprate0)/samprate0); double frskewcut=0.01; if(fsrskew>frskewcut) { elog_complain(0,"%s:%s sample rate %f is significantly different from base sample rate of %f. Trace skipped -- segy requires fixed sample rates", sta,chan,samprate,samprate0); continue; } if(nsamp > nsamp0) { elog_complain(0,"%s:%s trace has extra samples=%ld. Truncated to length %d", sta, chan, nsamp, nsamp0); nsamp = nsamp0; } else if(nsamp < nsamp0) { elog_complain(0,"%s:%s trace is shorter than expected %d samples. Zero padded after sample %ld", sta, chan, nsamp0, nsamp); } ichan = get_channel_index(channels,sta,chan); if(ichan > nchan) { elog_die(0,"Channel index %d outside limit of %d. Cannot continue", ichan, nchan); } if(ichan >= 0) { if(Verbose) elog_debug(0,"%s:%s\t%-d\t%-d\t%-ld\t%-ld\n", sta,chan,ichan+1, ntohl(header[ichan].reelSeq), shotid, evid); header[ichan].traceID = get_trace_id_code_from_segtype(segtype); for(j=0;j<nsamp;++j) { traces[ichan][j] = htonf((float)trdata[j]); } /* header fields coming from trace table */ header[ichan].samp_rate = htonl( (int32_t) (1000000.0/samprate0)); /* according to the behavior specified in the man page: * if use_geo_coordinates is false: * - coordUnits is length (meters) * - therefore, we use deast for X and dnorth for Y * if use_geo_coordinates is true: * - we're using either arcseconds or decimal degrees * - and therefore, we use lon for X and lat for Y * * coordUnits is based on use_arcseconds and the requested * version of segY */ /* set the coordinate units in the trace header */ header[ichan].coordUnits = coordUnits; /* Pick the source db fields for our receiver X and Y */ double recLongOrX = 0; double recLatOrY = 0; if (coordUnits == SEGY_TRACE_COORDUNITS_LENGTH) { /* Use deast and dnorth * CSS3.0 Schema specifies deast and dnorth are in KM. * SEG-Y specifies easting and northing as meters, * hence the 1000.0 multiplier here. */ recLongOrX = deast * 1000.0; recLatOrY = dnorth * 1000.0; } else if (coordUnits == SEGY_TRACE_COORDUNITS_ARCSECONDS){ /* Use lat and lon, converted to arcseconds */ recLongOrX = lon * 3600.0; recLatOrY = lat * 3600.0; } else { /* Default case, which covers decimal degrees */ recLongOrX = lon; recLatOrY = lat; } /* Apply our coordScale - the user can specify negative numbers, * but they are treated as inverting the value, not as a divisor * as in the SEG-Y field usage. See below where we always treat * the scalar as a divisor in the SEG-Y field */ recLongOrX *= (double)coordScale; recLatOrY *= (double)coordScale; /* Set the coordScale in the header. * Note negative here. This is a oddity of segy that - means * divide by this to get actual. Always make this negative in * case user inputs a negative number. * Don't set it -1 for cosmetic reasons */ if (abs(coordScale) == 1) { header[ichan].coordScale = htons(1); } else { header[ichan].coordScale = htons(-abs(coordScale)); } /* Finally, write out the X and Y */ header[ichan].recLongOrX = htonl((int32_t)recLongOrX); header[ichan].recLatOrY = htonl((int32_t)recLatOrY); /* CSS3.0 specfies elev as being in km, SEG-Y wants it in m */ header[ichan].recElevation = htonl((int32_t)(elev*1000.0)); header[ichan].deltaSample = htons( (int16_t) (1000000.0/samprate0)); header[ichan].sampleLength = htons((int16_t)nsamp0); if (ntohs(segy_format)<0x0100) { header[ichan].num_samps = htonl((int32_t)nsamp0); } /* This cracks the time fields */ time_str = epoch2str(time0,fmt); int16_t hyear, hday, hhour, hminute, hsecond, hm_secs; hyear=hday=hhour=hminute=hsecond=hm_secs=0; sscanf(time_str,"%hd %hd %hd %hd %hd %hd", &hyear, &hday, &hhour, &hminute, &hsecond, &hm_secs); header[ichan].year = htons(hyear); header[ichan].day = htons(hday); header[ichan].hour = htons(hhour); header[ichan].minute = htons(hminute); header[ichan].second = htons(hsecond); header[ichan].m_secs = htons(hm_secs); if (ntohs(segy_format)<0x0100) { /* These are IRIS-PASSCAL extensions */ header[ichan].trigyear = header[ichan].year; header[ichan].trigday = header[ichan].day; header[ichan].trighour = header[ichan].hour; header[ichan].trigminute = header[ichan].minute; header[ichan].trigsecond = header[ichan].second; } free(time_str); if(input_source_coordinates) { /* Write out our pre-scaled and optionally * arcsecond-converted source lat/lon plus our elevation */ header[ichan].sourceLongOrX = htonl((int32_t)slon); header[ichan].sourceLatOrY = htonl((int32_t)slat); header[ichan].sourceSurfaceElevation = htonl((int32_t)selev); /* No easy way to specify both elev and depth*/ header[ichan].sourceDepth=htonl(0); } else if(map_to_cdp) { /* When faking CDP data we make this look like a zero offset, single fold data set */ header[ichan].sourceLongOrX = header[ichan].recLongOrX; header[ichan].sourceLatOrY = header[ichan].recLatOrY; header[ichan].sourceSurfaceElevation = header[ichan].recElevation; header[ichan].sourceDepth = htonl(0); header[ichan].sourceToRecDist = htonl(0); } else { /* This is the mechanism for adding other information with added tables. The one table currently supported is a "shot" table that holds shot coordinates. If other tables were added new functions could be added with a similar calling sequence. This procedure silently does nothing if a shot table is not present.*/ set_shot_variable(db,table_list, evid,&header[ichan]); } } else { if(Verbose) elog_notify(0,"Station %s and channel %s skipped\n", sta,chan); } } /* Now we write the data */ for(i=0;i<nchan;++i) { if(fwrite((void *)(&(header[i])),sizeof(SEGYTraceHeader),1,fp) != 1) elog_die(0,"Write error on header for trace %d\n",total_traces+i); if(fwrite((void *)traces[i],sizeof(float), (size_t)nsamp0,fp) != nsamp0) elog_die(0,"Write error while writing data for trace %d\n", total_traces+i); } total_traces += nchan; trdestroy(&trdb); if(!input_source_coordinates) ++shotid; } return 0 ; }
int init_newton_transform(preprocessed *prep, float reqscale, char *filename, int dimen) /* */ { int ii, jj; unsigned short matdim; double scale, onerow[MAX_DIMEN]; PFile* dfpt; long foffset; double xfp; /* Open file */ ASSERT(prep); ASSERT(filename); dfpt = file_must_open(NULL, filename, ("rb"), ESR_TRUE); prep->post_proc |= LIN_TRAN; prep->use_dim = dimen; pfread(&matdim, sizeof(short), 1, dfpt); if (matdim > MAX_DIMEN) SERVICE_ERROR(BAD_IMELDA); create_linear_transform(prep, matdim, 1); pfread(&scale, sizeof(double), 1, dfpt); if (reqscale != 0) scale = reqscale; #if DEBUG PLogMessage("L: LDA Suggested scale is %.1f\n", scale); #endif if (!prep->dim) prep->dim = matdim; else if (prep->dim != matdim) { log_report("Data (%d) and LDA (%d) dimensions don't match\n", prep->dim, matdim); SERVICE_ERROR(BAD_IMELDA); } /* Eigenvalues, ignored */ pfread(onerow, sizeof(double), matdim, dfpt); /* Translation Vector */ pfread(onerow, sizeof(double), matdim, dfpt); for (ii = 0; ii < matdim; ii++) { xfp = scale * (onerow[ii] - UTB_MEAN) + UTB_MEAN; if (xfp > 0.0) xfp += 0.5; else if (xfp < 0.0) xfp -= 0.5; prep->offset[ii] = (imeldata) xfp; } /* The imelda matrix */ for (ii = 0; ii < matdim; ii++) { pfread(onerow, sizeof(double), matdim, dfpt); for (jj = 0; jj < matdim; jj++) prep->imelda[ii][jj] = (covdata)(scale * onerow[jj]); } prep->imel_shift = scale_matrix_for_fixedpoint(prep->matrix, prep->imelda, matdim); /* The inverse imelda matrix */ foffset = pftell(dfpt); pfread(onerow, sizeof(double), matdim, dfpt); if (pfeof(dfpt) != 0) { #ifdef SREC_ENGINE_VERBOSE_LOGGING PLogMessage("W: Inverting imelda matrix"); #endif invert_matrix(prep->imelda, prep->inverse, prep->dim); } else { pfseek(dfpt, foffset, SEEK_SET); for (ii = 0; ii < matdim; ii++) { pfread(onerow, sizeof(double), matdim, dfpt); for (jj = 0; jj < matdim; jj++) prep->inverse[ii][jj] = (covdata)(onerow[jj] / scale); } } prep->inv_shift = scale_matrix_for_fixedpoint(prep->invmat, prep->inverse, matdim); pfclose(dfpt); return (0); }
static int DEHT_queryEx(DEHT *ht, const unsigned char *key, int keyLength, const unsigned char *data, int dataMaxAllowedLength, byte_t * keyBlockOut, ulong_t keyBlockSize, DEHT_DISK_PTR * keyBlockDiskOffset, ulong_t * keyIndex) { int ret = DEHT_STATUS_FAIL; int hashTableIndex = 0; byte_t * validationKey = NULL; KeyFilePair_t * currPair = NULL; DEHT_DISK_PTR dataBlockOffset = 0; ulong_t bytesRead = 0; TRACE_FUNC_ENTRY(); CHECK(NULL != ht); CHECK(NULL != key); CHECK(NULL != data); CHECK(NULL != keyBlockOut); CHECK(KEY_FILE_BLOCK_SIZE(ht) <= keyBlockSize); CHECK(NULL != keyBlockDiskOffset); CHECK(NULL != keyIndex); TRACE_FPRINTF((stderr, "TRACE: %s:%d (%s): key=%s\n", __FILE__, __LINE__, __FUNCTION__, key)); /* calc hash for key */ CHECK(NULL != ht->hashFunc); hashTableIndex = ht->hashFunc(key, keyLength, ht->header.numEntriesInHashTable); TRACE_FPRINTF((stderr, "TRACE: %s:%d (%s): bucket index=%#x\n", __FILE__, __LINE__, __FUNCTION__, (uint_t) hashTableIndex)); CHECK(DEHT_findFirstBlockForBucketAndAlloc(ht, hashTableIndex, keyBlockDiskOffset)); CHECK(0 != *keyBlockDiskOffset); TRACE_FPRINTF((stderr, "TRACE: %s:%d (%s): first block for bucket %d at offset=%#x\n", __FILE__, __LINE__, __FUNCTION__, hashTableIndex, (uint_t) *keyBlockDiskOffset)); /* If there is no block for this bucket, return with nothing */ if (0 == *keyBlockDiskOffset) { ret = DEHT_STATUS_NOT_NEEDED; goto LBL_CLEANUP; } /* alloc space and calc validation key */ validationKey = malloc(ht->header.nBytesPerValidationKey); CHECK_MSG("malloc", (NULL != validationKey)); /*! Note: return value isn't checked since the spec does not include details regarding the key validation function interface */ CHECK(NULL != ht->comparisonHashFunc); (void) ht->comparisonHashFunc(key, keyLength, validationKey); while (0 != *keyBlockDiskOffset) { /* read block to mem */ CHECK_MSG(ht->sKeyfileName, (pfread(ht->keyFP, *keyBlockDiskOffset, keyBlockOut, KEY_FILE_BLOCK_SIZE(ht)))); /* scan this block */ for(*keyIndex = 0; *keyIndex < ht->header.nPairsPerBlock; ++*keyIndex) { currPair = GET_N_REC_PTR_IN_BLOCK(ht, keyBlockOut, *keyIndex); if (0 == memcmp(currPair->key, validationKey, ht->header.nBytesPerValidationKey)) { break; } } if (*keyIndex < ht->header.nPairsPerBlock) { break; } /* disk offset of the next pointer is the last element in the block */ *keyBlockDiskOffset = *( (DEHT_DISK_PTR *) (keyBlockOut + KEY_FILE_BLOCK_SIZE(ht) - sizeof(DEHT_DISK_PTR)) ); TRACE_FPRINTF((stderr, "TRACE: %s:%d (%s): next ptr from disk: %#x\n", __FILE__, __LINE__, __FUNCTION__, (uint_t) *keyBlockDiskOffset)); } dataBlockOffset = 0; if (*keyIndex < ht->header.nPairsPerBlock) { dataBlockOffset = currPair->dataOffset; } else { /* we scanned everything, but found no matching key */ ret = DEHT_STATUS_NOT_NEEDED; TRACE("key not found"); goto LBL_CLEANUP; } bytesRead = 0; CHECK(DEHT_readDataAtOffset(ht, dataBlockOffset, (byte_t *) data, dataMaxAllowedLength, &bytesRead)); TRACE("key found"); ret = bytesRead; goto LBL_CLEANUP; LBL_ERROR: ret = DEHT_STATUS_FAIL; TRACE_FUNC_ERROR(); LBL_CLEANUP: FREE(validationKey); TRACE_FUNC_EXIT(); return ret; }
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); } }
main ( int argc, char **argv ) { char orbname[20]="zagsun6"; int orb; tstations_struc stations ; char name[5] = "ORB" ; char sname[5] = "Q003" ; short int data_mask = CSIM_DATA ; pclient_struc client ; pclient_station station ; pdata_user data ; boolean alert_flag ; int ret ; int i, j ; Steim *blockette; int *sdata, npts; PktChannel pktchan; unsigned short int quality = 0; unsigned short int pinno = 0; char *packet = NULL; int nbytes = 0; int bufsiz = 0; char srcname[256]; int fix_MOTA=0; int fix_WATA=0; int fix_SQTA=0; /* double fouryears=epoch(2004000)-epoch(1996000); double eightyears=epoch(2004000)-epoch(1996000); double twelveyears=epoch(2008000)-epoch(1996000); double twentyyears=epoch(2016000)-epoch(1996000); */ double sixteenyears=epoch(2012000)-epoch(1996000); Pf *mypf; /* Niko, leap second July 2012 */ /* subtract another second for the leap-second 2006-01-01 */ /* no more... Udo corrected the time again... eightyears -= 1; sixteenyears -= 1.0; */ /*schaltsekunde 2009-01-01*/ /*double twokohnine=epoch(2009001); */ elog_init ( argc, argv ) ; if (argc < 2) { usage(); exit (1); } /*strcpy(orbname,"zagsun6");*/ /*orbname = argv[2];*/ strncpy (sname, argv[1], 4); sname[4] = '\0'; for (argc-=3,argv+=3; argc>0; argc--,argv++) { if (!strcmp(*argv, "-v")) { verbose = 1; } else { fprintf (stderr, "cs2orb: Unrecognized option '%s'.\n", *argv); usage(); exit (1); } } putenv("PFPATH=/home/comserv/pf"); if ( pfread ( "cs2orb", &mypf ) != 0 ) die ( 1, "Can't read parameter file\n" ) ; fix_MOTA=pfget_int(mypf,"MOTA"); fix_WATA=pfget_int(mypf,"WATA"); fix_SQTA=pfget_int(mypf,"SQTA"); /* orb setup stuff */ orb = orbopen (orbname, "w&"); if (orb < 0) { clear_register (1); fprintf (stderr, "cs2orb: orbopen() error for '%s'.\n", orbname); exit (1); } /* comserv setup stuff */ cs_setup (&stations, name, sname, TRUE, TRUE, 10, 5, data_mask , 6000) ; client = cs_gen ( &stations ) ; /* Trace setup stuff */ blockette = newsteim(); blockette->record_size = 512 ; /* Data packet read loop */ while (1) { /* Look for a packet */ ret = cs_scan ( client , &alert_flag ) ; /* Sleep and loop if nothing there */ if (ret == -1) { sleep (1); continue; } /* Grab the station structure pointer */ station = (pclient_station) ((long int) client + client->offsets[ret]) ; /* Printout station status */ if (alert_flag) { complain (0, "Station %4s status: %s\n", long_str(station->name.l), &(stats[station->status])) ; } /* Sleep and loop if no valid data */ if (!(station->valdbuf)) { sleep (1); continue; } /* Grab the data buffer pointer */ data = (pdata_user) ((long int) client + station->dbufoffset) ; /* Loop over number of data buffers */ for (i=0; i<station->valdbuf; i++,data=(pdata_user) ((long int)data + station->dbufsize)) { /* Set data blockette record */ blockette->record = (pvoid) &(data->data_bytes) ; /* Parse the SEED header */ if ( parse_seed_data_header( blockette ) ) { complain ( 0, "Problems parsing SEED header or 1000 blockette.\n" ) ; continue ; } /* niko netzname at fuer antelope */ strcpy (blockette->sdh.net,"OE"); sprintf (srcname, "%s_%s_%s", blockette->sdh.net, blockette->sdh.sta, blockette->sdh.chan); if (verbose) printf ("%s:", srcname); /* Not a data packet */ if (!(blockette->sdh.nsamp) || !(blockette->sdh.samprate_factor)) { if (verbose) printf (" No data\n"); continue; } if (verbose) printf ("%s", strtime(blockette->sdh.epoch)); /* Uncompress the data */ if ( usteim (blockette, &sdata, &npts) ) { clear_register (1); continue; } if (verbose) { for (j=0; j<10; j++) printf (" %5d", sdata[j]); printf ("\n"); } /* Stuff packet for orb */ strcpy (pktchan.net, blockette->sdh.net); strcpy (pktchan.sta, blockette->sdh.sta); strcpy (pktchan.chan, blockette->sdh.chan); /* pktchan.time = blockette->sdh.epoch+eightyears; pktchan.time = blockette->sdh.epoch+twelveyears; */ pktchan.time = blockette->sdh.epoch+sixteenyears; /*pktchan.time = blockette->sdh.epoch+twentyyears;*/ pktchan.samprate = blockette->sdh.samprate; switch (pktchan.chan[0]) { case 'H': pktchan.calib = 1.589446; break; /* case 'E': pktchan.calib = 0.06/0.707; changed to 0.257 (~0.36*0.707) */ case 'E': pktchan.calib = 0.257; /* incl. dämpfung -> stimmt wenn antelope diesen wert mit TF multipl. */ break; default: pktchan.calib = 1.0; break; } if (!strcmp(pktchan.sta,"GAGA")) { /*Uhrenkorrektur template */ pktchan.time -= 1.0; } if (!strcmp(pktchan.sta,"no-MOTA")) { /*Uhrenkorrektur MOTA */ pktchan.time -= 1.0; } if ( fix_MOTA != 0 && !strcmp(pktchan.sta,"MOTA")) { /*Uhrenkorrektur MOTA */ pktchan.time += (double)(fix_MOTA); } if (fix_SQTA != 0 && !strcmp(pktchan.sta,"SQTA")) { /*Uhrenkorrektur SQTA */ pktchan.time += (double)(fix_SQTA); } if ( fix_WATA !=0 && !strcmp(pktchan.sta,"WATA")) { /*Uhrenkorrektur WATA */ pktchan.time += (double)(fix_WATA); } /* solve the problem with omitted segtype, which chops the db */ if (!strcmp(pktchan.sta,"OBKA")) { switch (pktchan.chan[1]) { case 'H': strcpy(pktchan.segtype,"V"); break; case 'L': strcpy(pktchan.segtype,"A"); break; default: ; break; } } /* pktchan.calib = 1.0; */ pktchan.nsamp = blockette->sdh.nsamp; pktchan.data = sdata; pktchan.nbytes = 4*pktchan.nsamp; pktchan.datatype = trINT; if (!stuff_iwc_tracebuf ( quality, pinno, &pktchan, &packet, &nbytes, &bufsiz ) ) { complain (0, "stuff_iwc_tracebuf error.\n"); exit (1); } /* Put to orb */ if ( orbput ( orb, srcname, pktchan.time, packet, nbytes ) ) { complain (0, "orbput error.\n"); exit (1); } orbflush (orb); } } }
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 ); } } } }
main(int argc, char **argv) { FILE *fpr, *fpw; char msg[BUFSIZ]; char erbuf[BUFSIZ], ewbuf[BUFSIZ], rbuf[BUFSIZ], wbuf[BUFSIZ]; char pbuf[1], pfbuf[1]; size_t mbytes, rbytes, wbytes; size_t ritems, witems; size_t (*readptr) (); /* pointer to efread() or pfread() */ size_t efread(); /* must be declared to use ptr */ size_t pfread(); /* must be declared to use ptr */ initargs(argc, argv); /* Exercise efread and efwrite */ fpw = efopen("junk.fwr", "w+"); strcpy(ewbuf, " Writing with efwrite\n"); witems = strlen(ewbuf); efwrite(ewbuf, 1, witems, fpw); erewind(fpw); fread(rbuf, 1, witems, fpw); erewind(fpw); strcpy(msg, "***efwrite from file to buffer ..."); mbytes = strlen(msg); fwrite(msg, 1, mbytes, stdout); fwrite(rbuf, 1, witems, stdout); fpr = fopen("junk.frd", "w+"); strcpy(wbuf, " Reading with efread\n"); ritems = strlen(wbuf); fwrite(wbuf, 1, ritems, fpr); erewind(fpr); strcpy(wbuf, " efread saw zero bytes\n"); witems = strlen(wbuf); strcpy(msg, "***efread from file to buffer ..."); mbytes = strlen(msg); fwrite(msg, 1, mbytes, stdout); if (!efread(erbuf, 1, ritems, fpr)) { fwrite(wbuf, 1, witems, stdout); } else { fwrite(erbuf, 1, ritems, stdout); } erewind(fpr); strcpy(wbuf, " Reading byte by byte with efread\n"); ritems = strlen(wbuf); fwrite(wbuf, 1, ritems, fpr); erewind(fpr); strcpy(wbuf, " exit loop: efread returned zero\n"); witems = strlen(wbuf); strcpy(msg, "***efread file byte by byte to buffer ..."); mbytes = strlen(msg); fwrite(msg, 1, mbytes, stdout); while (efread(erbuf, 1, 1, fpr)) { fwrite(erbuf, 1, 1, stdout); } erewind(fpr); fwrite(wbuf, 1, witems, stdout); strcpy(wbuf, ""); ritems = strlen(wbuf); fwrite(wbuf, 1, ritems, fpr); erewind(fpr); strcpy(wbuf, " efread saw zero bytes\n"); witems = strlen(wbuf); strcpy(msg, "***efread from EMPTY file to buffer ..."); mbytes = strlen(msg); fwrite(msg, 1, mbytes, stdout); efread(erbuf, 1, ritems, fpr); erewind(fpr); fwrite(wbuf, 1, witems, stdout); efclose(fpw); efclose(fpr); eremove("junk.frd"); eremove("junk.fwr"); /* Exercise pfread and efread */ /* Set appropriate read function for input filetype */ switch(filestat(STDIN)) { case TTY: err("input can't be tty"); break; case DISK: case TAPE: readptr = efread; strcpy(ewbuf, "***Disk stdin: use efread ... "); witems = strlen(ewbuf); efwrite(ewbuf, 1, witems, stdout); break; case PIPE: readptr = pfread; strcpy(ewbuf, "***Pipe stdin: use pfread ... "); witems = strlen(ewbuf); efwrite(ewbuf, 1, witems, stdout); break; default: err("undefined input filetype %s", printstat(STDIN)); break; } while ((*readptr)(pfbuf, 1, 1, stdin)) { efwrite(pfbuf, 1, 1, stdout); } return EXIT_SUCCESS; }
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); }
int main (int argc, char **argv) { int c; int errflg = 0; Dbptr db; char *database; int verbose = 0; int primary = 0; Pf *pf = NULL; char *xmlstring = NULL; char *rootname = NULL; char *rowname = NULL; char *stylesheet = NULL; char xslt_processor[STRSZ]; char command[STRSZ]; char tempfile[FILENAME_MAX]; FILE *tempfs = NULL; Tbl *fields = 0; Tbl *expressions = 0; int rc; int flags = 0; elog_init( argc, argv ) ; while ((c = getopt (argc, argv, "pd:r:t:v")) != -1) { switch (c) { case 'v': verbose++; break; case 't': stylesheet = strdup( optarg ); break; case 'd': rootname = strdup( optarg ); break; case 'p': primary++; break; case 'r': rowname = strdup( optarg ); break; default: errflg++; break ; } } if( errflg || argc - optind < 1 || (((argc-optind) % 2) != 1)) { usage (); } pfread( "db2xml", &pf ); database = argv[optind++]; if( dbopen_table( database, "r+", &db ) < 0 ) { elog_die( 0, "Can't open database %s\n", database ); } if( argc - optind > 0 ) { fields = newtbl( 20 ); expressions = newtbl( 20 ); while( argc - optind > 0 ) { pushtbl( fields, argv[optind++] ); pushtbl( expressions, argv[optind++] ); } if( primary ) { elog_complain( 1, "Useless use of '-p' with specified expressions, ignoring\n" ); } } else if( primary ) { flags |= DBXML_PRIMARY; } rc = db2xml( db, rootname, rowname, fields, expressions, (void **) &xmlstring, flags ); if( rc < 0 || xmlstring == NULL ) { elog_clear_register( 1 ); elog_die( 0, "db2xml failed\n" ); } else if( stylesheet != NULL ) { sprintf( tempfile, "/tmp/db2xml_%d_%d.xml", getuid(), getpid() ); tempfs = fopen( tempfile, "w" ); fwrite( xmlstring, sizeof(char), strlen( xmlstring ), tempfs ); fclose( tempfs ); sprintf( xslt_processor, "%s", pfget_string( pf, "xslt_processor" ) ); strsub( xslt_processor, "STYLESHEET", stylesheet, xslt_processor ); strsub( xslt_processor, "XMLFILE", tempfile, command ); system( command ); unlink( tempfile ); } else { fwrite( xmlstring, sizeof(char), strlen( xmlstring ), stdout ); } free( xmlstring ); return 0; }