Arr *build_stachan_list(Pf *pf, int *nchan,int verbose) { char sta[10], chan[10]; char *key; Arr *a; int i; Tbl *t; char *line; int *channel_number; /* value stored in arr */ if(verbose) elog_notify(0,"Station Channel_code Channel_number\n"); a = newarr(0); t = pfget_tbl(pf,"channels"); if(t==NULL) elog_die(0,"Parameter file error: no channels table\n"); for(i=0;i<maxtbl(t);++i) { line = gettbl(t,i); sscanf(line,"%s %s",sta,chan); key = make_key(sta,chan); channel_number = (int *) malloc(sizeof(int)); if(channel_number == NULL) elog_die(0,"malloc error for channel_number\n"); *channel_number = i; setarr(a,key,(void *)channel_number); if(verbose) elog_notify(0,"%s %s %d\n",sta,chan,(*channel_number)+1); free(key); } *nchan = maxtbl(t); freetbl(t,free); return(a); }
int add_latencies ( Arr * networks ) { char * net; char * sta; char * statime; Arr * stations; Arr * sinfo; Arr * network; Tbl * stas; Tbl * nets; Pf * oldval; int numstas; int numnets; int i, j; int min = MAXINT; int max = 0; nets = keysarr( networks ); numnets = maxtbl( nets ); for ( i = 0; i < numnets; i++ ) { net = gettbl( nets, i ); network = getpf( networks, net ); stations = getpf( network, STALIST ); stas = keysarr( stations ); numstas = maxtbl( stas ); for ( j = 0, min=MAXINT, max=0; j < numstas; j++ ) { sta = gettbl( stas, j ); sinfo = getpf( stations, sta ); statime = get_station_field( sinfo, LATENCY ); if ( statime && ( atoi( statime ) < min ) ) min = atoi( statime ); if ( statime && ( atoi( statime ) > max ) ) max = atoi( statime ); } freetbl( stas, 0 ); oldval = setarr( network, MINNETLATENCY, create_pf( itoa(min), PFSTRING ) ); if ( oldval ) recurse_free_pf( oldval ); oldval = setarr( network, MAXNETLATENCY, create_pf( itoa(max), PFSTRING ) ); if ( oldval ) recurse_free_pf( oldval ); } freetbl( nets, 0 ); return 1; }
/* This function is called in ttlvz_init to parse the contents of the Pf object containing the velocity model. Both models are specified the same way, so we call this same function for both model tables. Arguments: t = Tbl set by pfget_tbl containing the velocity model table. It is assumed this tbl contains a set of strings that can be read with sscanf as velocity, depth pairs. v = vector of layer velocities (space is alloced for *v and then values are set). This pointer is set to alloced memory area here and on return it contains a vector of layer velocities. z = parallel vector to v for depths to layer tops. n = length of z and n vectors (set by this routine) */ void ttlvz_parse_vmodel(Tbl *t,double **v, double **z, int *n) { int nlayers, i; nlayers = maxtbl(t); if(nlayers <= 0) die(0,"ttlvz_parse_vmodel: No velocity model parameters for calculating travel times\n"); /* the 100 here is an arbitrary constant, and this is only a warning */ if(nlayers > 100) { complain(0,"Warning (ttlvz_parse_vmodel): read data for %d layer velocity model\nConsider using a different travel time calculator for better performance\n", nlayers); } *v = (double *) calloc(nlayers, sizeof(double)); *z = (double *) calloc(nlayers,sizeof(double)); if( (*v == NULL) || (*z == NULL) ) die(1,"Cannot alloc memory in ttlvz_parse_vmodel function for %d layer model\n", nlayers); for(i=0;i<nlayers;++i) { char *line; line = gettbl(t,i); sscanf(line,"%lf %lf",(*v)+i,(*z)+i); } *n = nlayers; }
int load_surface_velocity(Pf *pf, Arr *a) { int nset; Tbl *t; char sta[10]; double vp,vs; char *line; MWstation *s; int i; t = pfget_tbl(pf,"surface_velocities"); if(t == NULL) elog_die(0,"surface_velocities table missing from parameter file\n"); for(i=0,nset=0;i<maxtbl(t);i++) { line = gettbl(t,i); if((sscanf(line,"%s %lf %lf",sta,&vp,&vs)) != 3) { elog_notify(0,"Syntax error reading line from surface_velocity table\nOffending line->%s\n", line); continue; } s = (MWstation *)getarr(a,sta); if(s == NULL) { elog_notify(0,"Station %s listed in surface_velocity table not found in master table\n", sta); continue; } s->vp0 = vp; s->vs0 = vs; ++nset; } return(nset); }
/* This function establishes the list of all stations that will actually be used for processing. I creates an associative array of MWstation objects keyed by the station name. Note it is important to realize the structure this function creates is NOT completely filled in, but every element is at least initialized. (see above). The NULL pointers are especially dangerous if not filled in. The main element of the MWstation structure that this function fills in is the weights vector. This vector is an array of weights used for forming the stack in a given frequency band. That is, weight[i] is the weight given traces for this station in wavelet band i. arguments: pf = input pf object to be parsed nbands = number of frequency bands to use in processing = length of weights vector created in s->weights The weights array is created cautiously using nbands. The input line is parsed and if there are insufficient weights listed in the input a diagnostic will be issued and the undefined weights set to 1.0. If there are more numbers listed than required the list will be silently truncated. History: Created summer 1999 Modified march 2000 Added code for stations with bad timing. */ Arr *create_station_objects(Pf *pf, int nbands) { int i,j,k; Arr *a; Tbl *t; MWstation *s; char *sta; char *line,*word; char *white=" \t"; t = pfget_tbl(pf,"station_weights"); if(t == NULL) elog_die(0,"station_weights table not in parameter file\n"); a = newarr(0); for(i=0;i<maxtbl(t);i++) { line = gettbl(t,i); sta = strtok(line,white); s = (MWstation *)malloc(sizeof(MWstation)); if(s==NULL) elog_die(0,"Cannot malloc MWstation structure for station %s\n",sta); initialize_MWstation(s,nbands); allot(double *,s->weights,nbands); s->sta = strdup(sta); for(j=0;j<nbands;j++) { word = strtok(NULL,white); if(word == NULL) { elog_notify(0,"error in station_weights parameter inputfor station %s\nExpected list of %d weights, found only %d\n", sta,nbands, j); if(j==0) { elog_notify(0,"No weights defined in any band for station %s\nSetting all weights to 1.0\n", sta); for(k=0;k<nbands;++k)s->weights[k]=1.0; } else { elog_notify(0,"Setting weights for station %s above band %d to %lf\n", sta,j-1,s->weights[j-1]); for(k=j;k<nbands;++k) s->weights[k]=s->weights[j-1]; } break; } s->weights[j] = atof(word); } /* we set the clock_is_bad variable false here and depend upon the genloc bad clock definitions to set a station as being always bad with this flag */ s->clock_is_bad = 0; setarr(a,s->sta,s); } return(a); }
Tbl * dbschema2sqlcreate( Dbptr db, long flags ) { Tbl *sql; char *cmd; Tbl *tables; char *table; long itable; sql = newtbl( 0 ); if( db.table >= 0 ) { cmd = generate_sqltable_create( db, flags ); pushtbl( sql, cmd ); } else { dbquery( db, dbSCHEMA_TABLES, &tables ); for( itable = 0; itable < maxtbl( tables ); itable++ ) { table = gettbl( tables, itable ); db = dblookup( db, "", table, "", "" ); cmd = generate_sqltable_create( db, flags ); pushtbl( sql, cmd ); } } return sql; }
void MWcheck_timing(Arr *a,Arr *arrsta,Arr *bcarr) { Tbl *stakeys; char *name; int i; stakeys = keysarr(a); for(i=0;i<maxtbl(stakeys);++i) { Bad_Clock *bc; MWstation *sta; double *atime; name=gettbl(stakeys,i); bc=(Bad_Clock *)getarr(bcarr,name); sta=(MWstation *)getarr(arrsta,name); if(sta==NULL) elog_complain(0,"MWcheck_timing: no match in station table for arrival at station %s\n",name); else { if(bc==NULL) sta->clock_is_bad=0; else { atime = (double *)getarr(a,name); if(clock_is_bad(bc->badtimes,*atime)) sta->clock_is_bad=1; else sta->clock_is_bad=0; } } } }
/* This function is used to exactly duplicate the arrival array of doubles from i to o. If *o is not a null pointer it is assumed the arr already exists and freearr is called to clear it's contents before calling setarr to copy contents.*/ void dup_arrival_array(Arr *in,Arr **o) { Tbl *t; double *value,*copy; char *key; int i; if((*o) == NULL) *o = newarr(0); else { freearr(*o,free); *o=newarr(0); } t = keysarr(in); for(i=0;i<maxtbl(t);++i) { key = gettbl(t,i); value = (double *)getarr(in,key); allot(double *,copy,1); *copy = *value; setarr(*o,key,copy); } freetbl(t,0); }
/* companion to the above, but this version will copy entries in *in to *o. Errors are issued only if *o does not contain an entry for the same key as *in. If *o is null the dup function above is called and the function returns immediately after returning from it. */ void copy_arrival_array(Arr *in,Arr **o) { Tbl *t; double *value,*copy; char *key; int i; if((*o) == NULL) { *o = newarr(0); dup_arrival_array(in,o); } else { t = keysarr(in); for(i=0;i<maxtbl(t);++i) { key = gettbl(t,i); value = (double *)getarr(in,key); copy = (double *)getarr(*o,key); if(copy == NULL) { setarr(*o,key,value); } else { *copy = *value; } } freetbl(t,0); } }
/* This function creates an associative array keyed by an integer event id from a parameter file based descriptor &Tbl like this example: pmel_calibration_events &Tbl{ 10 xyz 11 xyzt } where 10 and 11 are event ids and the string defines coordinates to fix. Author: Gary Pavlis */ Arr *load_calibration_events(Pf *pf) { Tbl *t; int evid; char *evidstr; char fix[5],*line; char *fptr; Arr *a; int i; a = newarr(0); t = pfget_tbl(pf,"pmel_calibration_events"); if(t==NULL) { elog_complain(0, "pmel_calibration_events Tbl not in parameter file\nAssuming no calibration events exist\n"); } else { for(i=0;i<maxtbl(t);++i) { line = (char *)gettbl(t,i); sscanf(line,"%d%s",&evid,fix); evidstr = make_evid_key(evid); fptr=strdup(fix); setarr(a,evidstr,fptr); free(evidstr); } } return(a); }
/* This is a companion routine to load_station_table for array beam code tables. At the moment it is essentially identical to the load_station_table function, but changes may eventually occur in the beam table that will make them diverge so I have produced to seperate functions */ Arr *load_array_table(Pf *pf) { Arr *a; Tbl *t; int i; char *value; Seismic_Array *s; double elev_datum; a = newarr(0); elev_datum = pfget_double_wdef(pf,"elevation_datum",0.0); t = pfget_tbl(pf,"seismic_arrays"); for(i=0;i<maxtbl(t);++i) { s = (Seismic_Array *) malloc(sizeof(Seismic_Array)); if(s == NULL) elog_die(1,"load_array_table: Cannot malloc Seismic_Array structure entry\n"); value = gettbl(t,i); if(sscanf(value,"%s %lf %lf %lf",s->name, &(s->lat),&(s->lon),&(s->elev)) != 4) elog_complain(1,"Warning(load_array_table): \ Read error in array tbl read from parameter file\n\ The following line of the array table was skipped\n%s\n", value); else { s->elev -= elev_datum; setarr(a,s->name,s); } } return(a); }
static int dbrows2orb(Dbptr db, int orb, char *prefix) { Packet *pkt; char srcname[ORBSRCNAME_SIZE]; double time; char *packet; int nbytes, packetsize = 0; Dbptr tmpdb; long t, nrecords, r, ntables; Arr *records = NULL; Tbl *tables = NULL, *static_tables; char *thistablename; Stbl *stbl; char *s; dbuntangle(db, &records); tables = keysarr(records); ntables = maxtbl(tables); if (ntables > 0) pkt = newPkt(); if (prefix) strncpy(pkt->parts.src_net, prefix, PKT_TYPESIZE); pkt->pkttype = suffix2pkttype("db"); for (t = 0; t < ntables; t++) { thistablename = gettbl(tables, t); tmpdb = dblookup(db, 0, thistablename, 0, 0); stbl = (Stbl *) getarr(records, thistablename); nrecords = maxstbl(stbl); if (nrecords > 0) { for (r = 0; r < nrecords; r++) { tmpdb.record = (long) getstbl(stbl, r); pkt->db = tmpdb; if (stuffPkt(pkt, srcname, &time, &packet, &nbytes, &packetsize) < 0) { elog_complain(0, "stuffPkt fails for pf packet"); return (-1); } if (orbput(orb, srcname, time, packet, nbytes) < 0) { elog_complain(0, "Couldn't send packet to orb\n"); return (-1); } } } } freetbl(tables, 0); dbfree_untangle(records); freePkt(pkt); if (verbose) { elog_notify(0, "%s: %d patcket(s) sent with sourcename: %s\n", s = strtime(now()), nrecords, srcname); free(s); } return (0); }
/* This small functions scans the gridlist tbl to get the maximum and minimum gridid values. These are used to subset the working view automatically to reduce the size of the working view (found to be excessive otherwise . gmin and gmax are returned as the maximum and minimum grid id Author: Gary Pavlis Written: July 2001 */ void get_gridid_range(Tbl *gridlist,int *gmin,int *gmax) { int gidmin,gidmax; int gridid; int i; if(maxtbl(gridlist)<=0) elog_die(0,"Empty grid id list\nProbable usage error\n"); gidmin = (int)gettbl(gridlist,0); gidmax = gidmin; for(i=1;i<maxtbl(gridlist);++i) { gridid = (int)gettbl(gridlist,i); gidmin = MIN(gidmin,gridid); gidmax = MAX(gidmax,gridid); } *gmin = gidmin; *gmax = gidmax; }
static PyObject * pf2PyObject( Pf *pf ) { PyObject *obj; Pf *pfvalue; Tbl *keys; char *key; int ivalue; switch( pf->type ) { case PFSTRING: obj = string2PyObject( pfexpand( pf ) ); break; case PFTBL: obj = PyTuple_New( pfmaxtbl( pf ) ); for( ivalue = 0; ivalue < pfmaxtbl( pf ); ivalue++ ) { pfvalue = (Pf *) gettbl( pf->value.tbl, ivalue ); PyTuple_SetItem( obj, ivalue, pf2PyObject( pfvalue ) ); } break; case PFFILE: case PFARR: keys = keysarr( pf->value.arr ); obj = PyDict_New(); for( ivalue = 0; ivalue < maxtbl( keys ); ivalue++ ) { key = gettbl( keys, ivalue ); pfvalue = (Pf *) getarr( pf->value.arr, key ); PyDict_SetItem( obj, Py_BuildValue( "s", key ), pf2PyObject( pfvalue ) ); } break; case PFINVALID: default: obj = (PyObject *) NULL; break; } return obj; }
int sinfo_update ( Arr * sold, Arr * snew ) { int i; int n1, n2; int numfields; char * field; char * value; char * newval; char * oldval; int newp, oldp; Tbl * fields; fields = keysarr( getpf( snew, FIELDS ) ); numfields = maxtbl( fields ); for ( i = 0; i < numfields; i++ ) { field = (char*) poptbl( fields ); value = get_station_field( snew, field ); oldval = get_station_field( sold, field ); if ( strcmp( field, LATENCY ) == 0 ) { newp = atoi( getpf( snew, LATENCY_ORDER ) ); oldp = atoi( getpf( sold, LATENCY_ORDER ) ); if ( ( !oldp && newp ) || ( newp < oldp ) ) { set_station_field( sold, field, strdup(value) ); setarr( sold, LATENCY_ORDER, create_pf( itoa(newp), PFSTRING ) ); } else if ( ( ( newp == oldp ) || ( !newp && !oldp ) ) && ( atoi( value ) < atoi( oldval ) ) ) { set_station_field( sold, field, strdup(value) ); setarr( sold, LATENCY_ORDER, create_pf( itoa(newp), PFSTRING ) ); } } else /* If this is a channels value, we want to merge the values */ if ( strcmp( field, NUMCHANS ) == 0 ) { (!value)? (n1 = 0): (n1 = atoi( value )); (!oldval)? (n2 = 0): (n2 = atoi( oldval )); set_station_field( sold, field, itoa( n1 + n2 ) ); } else if ( value != NULL ) set_station_field( sold, field, strdup(value) ); } freetbl( fields, 0 ); return 1; }
static PyObject * strtbl2PyObject( Tbl *atbl ) { PyObject *obj; int i; if( atbl == NULL ) { return NULL; } obj = PyTuple_New( maxtbl( atbl ) ); for( i = 0; i < maxtbl( atbl ); i++ ) { PyTuple_SetItem( obj, i, PyString_FromString( gettbl( atbl, i ) ) ); } return obj; }
/* Edits the array of phase handles to keep only phases named in the keeplist Tbl of phase names strings. This is complicated by the fact that keeplist is a simple list. The algorithm used converts the keeplist to a temporary associative array then passes through the array of phase handles calling the free routine on phases not found in the keeplist. Author: G Pavlis Written: August 2001 */ void edit_phase_handle(Arr *a,Tbl *keeplist) { Tbl *akeys; Arr *akeeper; int dummy; /* used purely as a placeholder in akeeper*/ char *phase; int i,n; Phase_handle *ph; n = maxtbl(keeplist); if(n<=0) elog_die(0,"List of phases to keep is empty.\n\ Check phases_to_keep parameter setting\n"); akeeper = newarr(0); for(i=0; i<maxtbl(keeplist); ++i) { phase = (char *)gettbl(keeplist,i); setarr(akeeper,phase,&dummy); ph = (Phase_handle *)getarr(a,phase); if(ph==NULL)elog_die(0, "Don't know how to handle required phase %s\n", phase); } akeys = keysarr(a); for(i=0; i<maxtbl(akeys); ++i) { phase = gettbl(akeys,i); if(getarr(akeeper,phase) == NULL) { ph = (Phase_handle *)getarr(a,phase); free_phase_handle(ph); delarr(a,phase); } } freearr(akeeper,0); freetbl(akeys,0); }
void mexFunction ( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { Dbptr tr; char *newchan[3]; Tbl *newchan_tbl; int rc; int i; if( nrhs != 2 ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if( ! get_dbptr( prhs[0], &tr ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if( ! get_stringtbl( prhs[1], &newchan_tbl ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } if( maxtbl( newchan_tbl ) != 3 ) { freetbl( newchan_tbl, 0 ); mexErrMsgTxt( "NEWCHANS array must have three channel names" ); } else { for( i = 0; i < 3; i++ ) { newchan[i] = gettbl( newchan_tbl, i ); } } rc = rotate_to_standard( tr, newchan ); antelope_mex_clear_register( 1 ); freetbl( newchan_tbl, 0 ); plhs[0] = CreateDouble( (double) rc ); if( plhs[0] == NULL ) { mexErrMsgTxt( "trrotate_to_standard: failed to create return value" ); } }
/* poor man's searchtbl */ static int findtbl(char *string, Tbl * table) { int i, found = 0; char *str; for (i = 0; i < maxtbl(table); i++) { str = gettbl(table, i); if (strcmp(string, str) == 0) { found = 1; break; } } return (found); }
/* Recursively removes pf instances from the pf object and returns the * objects' contents in an entirely new structure so that no memory is reused. * Requires the caller have some knowledge about the structure of the data. */ void * pf_collapse( Pf * pf ) { Arr * currarr; Tbl * currtbl; char * key; void * value; int i; int size; if ( pf == NULL ) return NULL; switch( pf->type ) { case PFARR: currtbl = keysarr( pf->value.arr ); size = maxtbl( currtbl ); /* currarr = newarr( pf->value.arr->rb_comp ); */ currarr = newarr( strcmp ); for ( i = 0; i < size; i++ ) { key = poptbl( currtbl ); value = pf_collapse( getarr( pf->value.arr, key ) ); setarr( currarr, key, value ); } freetbl( currtbl, 0 ); return currarr; case PFTBL: size = maxtbl( pf->value.tbl ); currtbl = newtbl( size ); for ( i = 0; i < size; i++ ) settbl( currtbl, i, pf_collapse( gettbl( pf->value.tbl, i ) ) ); return currtbl; default: return strdup( pf->value.s ); } }
Dbptr join_tables(Dbptr db, Pf *pf, Arr *tables) { char *table_name; int *ilogic; Tbl *t; Dbptr dbj; long int nrec; int i; int ntables=0; /* This is an exact copy of above, but it is duplicated because this function could get stolen by another future program because it is pretty general */ t = pfget_tbl(pf,"join_tables"); if(t == NULL) { elog_die(0,"No list of tables to be joined\n"); } for(i=0;i<maxtbl(t);++i) { table_name = gettbl(t,i); ilogic = (int *)getarr(tables,table_name); if(ilogic == NULL) { elog_die(0,"Table %s was not handled previously by check_tables.\nProgramming logic error\n", table_name); } else if(*ilogic) { if(ntables == 0) dbj = dblookup(db,0,table_name,0,0); else dbj=dbjoin(dbj,dblookup(db,0,table_name,0,0), NULL,NULL,0,NULL,0); ++ntables; dbquery(dbj,dbRECORD_COUNT,&nrec); if(nrec == 0) { elog_complain(0, "join_tables error\njoined database has 0 length after joining table %s\n", table_name); dbj.record = dbINVALID; return(dbj); } } } return(dbj); }
void log_error_list(Tbl *t) { int j; char line[512]; int nt; strcpy(line,"Problem attributes: "); nt = maxtbl(t); for(j=0;j<nt;++j) { char *tval; tval = (char *)gettbl(t,j); strcat(line,tval); if(j<(nt-1))strcat(line,","); } elog_notify(0,line); }
static void id_clients( Pf *pf ) { Pf *pfclients; Pf *pfclient; char client_summary[STRSZ]; char *serveraddress; char *serverport; Tbl *client_keys; int ikey; char *client_key; char clientid_key[STRSZ]; char *clientaddress; char *what; char clientid[33]; pfeval( pf, "server{address}", &serveraddress ); pfeval( pf, "server{port}", &serverport ); pfget( pf, "clients", (void **) &pfclients ); client_keys = pfkeys( pfclients ); for( ikey = 0; ikey < maxtbl( client_keys ); ikey++ ) { client_key = gettbl( client_keys, ikey ); pfget( pfclients, client_key, (void **) &pfclient ); clientaddress = pfget_string( pfclient, "address" ); what = pfget_string( pfclient, "what" ); sprintf( client_summary, "%s %s %s %s", serveraddress, serverport, clientaddress, what ); sprintf( clientid_key, "clients{%s}{clientid}", client_key ); mdhex( clientid, client_summary, strlen( client_summary ) ); pfset( pf, clientid_key, clientid ); } freetbl( client_keys, 0 ); return; }
int antelope_mex_elog_callback( int severity, char *string, Tbl *Elog ) { Elog_msg *elog_msg; char repeated[STRSZ]; char *message; int nmessages; int i; nmessages = maxtbl( Elog ); for( i = 0; i < nmessages; i++ ) { elog_msg = (Elog_msg *) gettbl( Elog, i ); if( elog_msg->n > 1 ) { sprintf( repeated, " (repeated %d times)", elog_msg->n ); } else { sprintf( repeated, "" ); } message = (char *) mxCalloc( strlen( elog_msg->msg ) + strlen( repeated ) + 1, sizeof( char ) ); sprintf( message, "%s%s", elog_msg->msg, repeated ); if( severity == ELOG_NOTIFY ) { mexPrintf( "%s", message ); } else if( severity >= ELOG_COMPLAIN ) { mexWarnMsgTxt( message ); } mxFree( message ); } return 1; }
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 ); } }
/* This routine looks for a Tbl in the parameter space that defines a set of auxiliary tables that are to be used. It cautiously checks to see if that table is defined in the schema and is nonempty. It sets logical variables in associative array it returns that define if the table is "ok". This is serious overkill added to make the code for expandable in the future. At the moment the only table that would be used is "shot". It would, however, be easy to add similar auxiliary tables for segy constructs like statics, mute definition, etc. In that case there probably should be a more general mechanism than this but I'm more or less laying out a useful functionality here rather than doing it in a completely general way. */ Arr *check_tables(Dbptr db, Pf *pf) { char *table; int *ilogic; int i; Tbl *t; Dbptr dbtmp; int table_ok; long int nrec; Arr *a; a = newarr(0); t = pfget_tbl(pf,"join_tables"); if(t == NULL) { elog_die(0,"No list of tables to be joined\n"); } for(i=0;i<maxtbl(t);++i) { /* This series of conditionals is safe: It certifies a table (a) is defined in this schema and (b) is not empty. */ table = (char *)gettbl(t,i); dbtmp = dblookup(db,0,table,0,0); if(dbtmp.table == dbINVALID) table_ok = 0; else table_ok = 1; if(table_ok) { dbquery(dbtmp,dbRECORD_COUNT,&nrec); if(nrec > 0) table_ok = 1; else table_ok = 0; } ilogic = (int *) malloc(sizeof(int)); if(ilogic == NULL) elog_die(0,"malloc error\n"); *ilogic = table_ok; setarr(a,table,ilogic); } return(a); }
mxArray * stringtbl2cellstr( Tbl *tbl ) { mxArray *result; mxArray *mystring; char *s; long M; long i; M = maxtbl( tbl ); if( M <= 0 ) { return 0; } else { result = mxCreateCellMatrix( M, 1 ); } for( i = 0; i < M; i++ ) { s = gettbl( tbl, i ); if( s == 0 ) { mxDestroyArray( result ); return 0; } mystring = mxCreateString( s ); if( mystring == 0 ) { mxDestroyArray( result ); return 0; } mxSetCell( result, i, mystring ); } return result; }
/* Creates an associative array of column index positions using pattern from associative array of Station structures keyed by sta name. The order will be controlled by what keysarr gives. Note we use pointers to int to store column indexes because getarr from this array signals a error with a NULL. If we used an int I can't see how we could tell this from 0, which is a valid column index and will, in fact, always be in the resultant. Arguments: sa - associative array of Station * that are counted to produce output indices. Returns an associative array keyed by station names that hold indices to column positions. These are basically count order from sa input. Author: G Pavlis Written: October 2000 */ Arr *create_sta_index(Arr *sa) { int *cindex; int i; Arr *aout; Tbl *keys; char *sta; aout = newarr(0); keys = keysarr(sa); for(i=0;i<maxtbl(keys);++i) { sta = gettbl(keys,i); allot(int *,cindex,1); *cindex = i; setarr(aout,sta,cindex); } return(aout); }
MWbasis *load_multiwavelets_pf(Pf *pf,int *nwavelets) { int i,j,k; char *line; double f0,fw; int nsamples; Tbl *w; MWbasis *mw; int nrows; nsamples = pfget_int(pf,"nsamples"); *nwavelets = pfget_int(pf,"nwavelets"); f0 = pfget_double(pf,"f0"); fw = pfget_double(pf,"fw"); w = pfget_tbl(pf,"wavelets"); nrows = maxtbl(w); if(nrows != (nsamples*(*nwavelets))) elog_die(0,"Size mismatch in wavelet parameter file\nExpected %d from %d wavelets each %d long\nFound %d instead\n", nsamples*(*nwavelets),*nwavelets,nsamples,nrows); mw = (MWbasis *)calloc(*nwavelets, sizeof(MWbasis)); if(mw == NULL) elog_die(0,"Cannot alloc %d multiwavelet structures\n",*nwavelets); for(i=0,k=0;i<(*nwavelets);++i) { mw[i].r = calloc(nsamples,sizeof(float)); mw[i].i = calloc(nsamples,sizeof(float)); if((mw[i].r == NULL) || (mw[i].i == NULL) ) elog_die(0,"Cannot alloc %d samples for complex wavelet %d\n", nsamples,i); mw[i].n = nsamples; mw[i].f0 = f0; mw[i].fw = fw; for(j=0;j<nsamples;j++,k++) { line = gettbl(w,k); if(sscanf(line,"%g%g",&(mw[i].r[j]),&(mw[i].i[j])) != 2) elog_die(0,"Error reading wavelets from parameter file on line %d of wavelet tbl\n", k); } } return(mw); }
int clrarr( Arr * arr, void (*free_value )() ) { Tbl * tbl; char * key; void * oldval; int size; int i; tbl = keysarr( arr ); size = maxtbl( tbl ); for ( i = 0; i < size; i++ ) { key = poptbl( tbl ); oldval = delarr( arr, key ); if ( oldval ) free_value( oldval ); } freetbl( tbl, 0 ); return 1; }