int main (int argc, char **argv) { int c, verbose = 0, errflg = 0; char *dbinname=malloc(1024); char *dboutname=malloc(1024); Point *poly; double lat,lon; char *auth=strdup("regions2polygon"); char *ptype= strdup("rp"); char *dir=strdup("."); char *dfile=strdup("polygons"); int ftype=polyFLOAT; char *name; int nregions, nvertices; Tbl *sortkeys, *groupkeys; Dbptr dbin,dbout,dbi,dbo,dbg,dbb; int i,from,to,nv; int vertex; elog_init ( argc, argv ) ; while ((c = getopt (argc, argv, "vV")) != -1) { switch (c) { case 'v': verbose++ ; break; case 'V': usage (); break; case '?': errflg++; break ; } } if ((errflg) || argc < 3) usage (); dbinname = argv[optind++]; dboutname= argv[optind++]; if (dbopen(dbinname,"r",&dbin)) { elog_die(1,"cannot open database %s",dbinname); } dbi=dblookup(dbin,0,"regions",0,0); sortkeys=newtbl(2); pushtbl(sortkeys,"regname"); pushtbl(sortkeys,"vertex"); groupkeys=newtbl(1); pushtbl(groupkeys,"regname"); dbi=dbsort(dbi,sortkeys,0,"regions.sorted"); dbg=dbgroup(dbi,groupkeys,0,0); dbquery(dbg,dbRECORD_COUNT,&nregions); if (nregions <1) { elog_die(0,"table regions seems to be empty (or not present)"); } if (verbose) elog_notify(0,"creating database descriptor %s",dboutname); if (dbcreate(dboutname,"polygon1.2",0,0,0)) { elog_die(1,"cannot create database %s",dboutname); } dbopen(dboutname,"r+",&dbout); dbo=dblookup(dbout,0,"polygon",0,0); for (i=0; i< nregions; i++) { dbg.record=i; dbgetv(dbg,0,"regname",name,"bundle",&dbb,0); dbget_range(dbb,&from,&to); nvertices= to - from; if (verbose) elog_notify(0,"%s (%i nvertices)",name,nvertices); poly=malloc(2 * nvertices * sizeof(double)); nv=0; for (dbi.record=from; dbi.record<to; dbi.record++) { dbgetv(dbi,0, "regname",name, "vertex",&vertex, "lat",&lat,"lon",&lon, 0); poly[nv].lat=lat; poly[nv].lon=lon; nv++; } writePolygonData(dbo,poly,nv,name,1,0,ptype,auth,dir,dfile,ftype); free(poly); } /* */ return 0; }
int main (int argc, char **argv) { int c, verbose = 0, errflg = 0; char *dbinname=malloc(1024); char *dboutname=malloc(1024); Point *poly; double lat,lon; char *subset_expr=NULL; char *name=malloc(100); long nregions, nvertices; Tbl *sortkeys; Dbptr dbin,dbout,dbi,dbo,dbs; long i,from,to,nv; long vertex; elog_init ( argc, argv ) ; while ((c = getopt (argc, argv, "s:vV")) != -1) { switch (c) { case 's': subset_expr=optarg; break; case 'v': verbose++ ; break; case 'V': usage (); break; case '?': errflg++; break ; } } if ((errflg) || argc < 3) usage (); dbinname = argv[optind++]; dboutname= argv[optind++]; if (dbopen(dbinname,"r",&dbin)) { elog_die(1,"cannot open database %s",dbinname); } dbi=dblookup(dbin,0,"polygon",0,0); if (subset_expr) { dbi=dbsubset(dbi,subset_expr,NULL); } sortkeys=newtbl(1); pushtbl(sortkeys,"pname"); dbs=dbsort(dbi,sortkeys,0,"sorted"); dbquery(dbs,dbRECORD_COUNT,&nregions); if (nregions <1) { elog_die(0,"table regions seems to be empty (or not present)"); } if (verbose) elog_notify(0,"creating database descriptor %s",dboutname); if (dbcreate(dboutname,"places1.2",0,0,0)) { elog_die(1,"cannot create database %s",dboutname); } dbopen(dboutname,"r+",&dbout); dbo=dblookup(dbout,0,"regions",0,0); for (i=0; i< nregions; i++) { dbs.record=i; dbgetv(dbs,0,"pname",name,NULL ); nvertices=readPolygon(dbs,&poly); for (nv=0;nv < nvertices;nv++) { lat=poly[nv].lat; lon=poly[nv].lon; dbaddv(dbo,0, "regname",name, "vertex",nv, "lat",lat,"lon",lon, NULL ); } free(poly); } dbclose(dbo); /* */ return 0; }
int main(int argc, char *argv[]) { unsigned int irand; int isamp,wsamp,exists,iret,jdate,nsamp,wfid,itype,i,half; long int nw; double samprate,time,endtime,toff,width; float *s,*n,del,nfactor,mean,stdv,dummy,h; char sta[7],chan[9],*database,*type; char dir[65],dfile[33],outfile[100]; Dbptr db,dbw; FILE *fp; database = malloc(80); type = malloc(20); if (argc < 13) { printf("Usage: %s type toff width nfactor sta chan time samprate nsamp database dir filename\n",argv[0]); return 1; } type = argv[1]; sscanf(argv[2],"%lf",&toff); sscanf(argv[3],"%lf",&width); sscanf(argv[4],"%f",&nfactor); strcpy(sta,argv[5]); strcpy(chan,argv[6]); sscanf(argv[7],"%lf",&time); sscanf(argv[8],"%lf",&samprate); sscanf(argv[9],"%d",&nsamp); database = argv[10]; strcpy(dir,argv[11]); strcpy(dfile,argv[12]); s = malloc(nsamp*sizeof(float)); n = malloc(nsamp*sizeof(float)); /*Open the output database.*/ exists = 1; if ( (fp = fopen(database,"r")) == NULL) exists = 0; else fclose(fp); /*It exists, so check if it can be opened and written to.*/ if (exists) { if (dbopen(database,"r+",&db) < 0) { complain(0,"Could not open existing database for writing.\n"); return 1; } } else { if (dbcreate(database,"css3.0",0,0,0) != 0) { complain(0,"Could not create new database.\n"); return 1; } else { if (dbopen(database,"r+",&db) != 0) { complain(0,"Could not open new database for writing.\n"); return 1; } } } dbw = dblookup(db,0,"wfdisc",0,0); iret = dbquery(dbw,dbRECORD_COUNT,&nw); printf("nw = %ld\n",nw); wfid = dbnextid(db,"wfid"); /*Create the synthetic trace.*/ if (strcmp(type,"zeros" ) == 0) itype = 1; if (strcmp(type,"delta" ) == 0) itype = 2; if (strcmp(type,"boxcar" ) == 0) itype = 3; if (strcmp(type,"triangle" ) == 0) itype = 4; if (strcmp(type,"sine" ) == 0) itype = 5; if (strcmp(type,"impulse" ) == 0) itype = 6; if (strcmp(type,"noise" ) == 0) itype = 7; /*Generate a zero-mean, unit rms, random noise series, whether used or not.*/ irand = now()-1200000000; srand(irand); /* sets the RNG with a random start integer*/ for (i=0;i<nsamp;i++) { /* Use a trick to make sure some abnormal number is not generated.*/ n[i] = 10.0; while (fabs(n[i]) > 9.9) n[i] = sqrt(-2.0*log(1.0 - ((float) rand())/RAND_MAX))*cos(6.2832 * ((float) rand())/RAND_MAX); } del = 1/samprate; printf("itype = %d\n",itype); switch (itype) { case 1: /*This creates a series of zeroes.*/ for (i=0;i<nsamp;i++) s[i] = 0.0; break; case 2: /*This creates a delta function.*/ for (i=0;i<nsamp;i++) s[i] = 0.0; isamp = toff*samprate; s[isamp] = 1.0; if (nfactor > 0) { for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i]; } break; case 3: /*This creates a boxcar function.*/ for (i=0;i<nsamp;i++) s[i] = 0.0; wsamp = width*samprate; /*Make it odd number of points, with center point splitting the boxcar.*/ if (wsamp%2 == 0) wsamp = wsamp + 1; isamp = toff*samprate; for (i=isamp;i<isamp+wsamp;i++) s[i] = 1.0; if (nfactor > 0) { for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i]; } break; case 4: /*This creates a triangle function.*/ for (i=0;i<nsamp;i++) s[i] = 0.0; wsamp = width*samprate; /*Make it odd number of points, with peak at center point.*/ if (wsamp%2 == 0) wsamp = wsamp + 1; half = wsamp/2; isamp = toff*samprate; for (i=isamp;i<isamp+half;i++) s[i] = 1.0*(i-isamp)/(float)half; s[isamp+half] = 1.0; for (i=isamp+half+1;i<isamp+wsamp;i++) s[i] = 1.0*(isamp+wsamp-i-1)/(float)half; if (nfactor > 0) { for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i]; } break; case 5: /*This creates a 1/width Hz sine wave.*/ for (i=0;i<nsamp;i++) s[i] = sin(2*pi*i*del/width); if (nfactor > 0) { for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i]; } break; case 6: /*This creates an exponentially decaying sine wave, like a seismometer delta response (damping = 0.7).*/ h = 0.7; for (i=0;i<nsamp;i++) s[i] = 0.0; isamp = toff*samprate; for (i=0;i<nsamp-isamp;i++) s[i+isamp] = sin(2*pi*sqrt(1-h*h)*i*del/width)*exp(-0.7*2*pi*i*del/width); if (nfactor > 0) { for (i=0;i<nsamp;i++) s[i] = s[i] + nfactor*n[i]; } break; case 7: /*This creates a random white-noise series.*/ for (i=0;i<nsamp;i++) s[i] = n[i]; iret = stats(s,nsamp,2,&mean,&stdv,&dummy,&dummy); printf("mean,stdv = %f %f\n",mean,stdv); break; default: printf("Input 'type' does not match any options.\n"); return 1; } /*Write out the new trace.*/ memcpy(outfile,"\0",1); strcat(outfile,dir); strcat(outfile,"/"); strcat(outfile,dfile); printf("outfile = %s\n",outfile); if((fp=fopen(outfile,"w")) == NULL) { fprintf(stderr,"Can't open %s\n",outfile); return 1; } fwrite(s,sizeof(float),(unsigned int)nsamp,fp); fclose(fp); /*Add entry to wfdisc. Note that, due to problem with dbaddv, we force the adding of the record with dbputv after a null record is created.*/ dbw.record = dbaddnull(dbw); jdate = yearday(time); endtime = time + (nsamp - 1)/samprate; if (dbputv(dbw,0, "sta", sta, "chan", chan, "time", time, "endtime", endtime, "jdate", jdate, "nsamp", nsamp, "samprate", samprate, "datatype", "u4", "dir", dir, "dfile", dfile, "foff", 0, "wfid",wfid, NULL) < 0) { complain(0,"Could not add record to wfdisc file.\n"); return 1; } dbclose(db); } /*End main routine.*/
void mexFunction ( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { Dbptr db; Point *polygon; char *pname; char *ptype; char *auth; char *dir; char *dfile; char *s_pcode; int pcode; double *lat; double *lon; long nsamp; mwSize nlat,nlon; const mwSize *dimensions; int i; int retcode; char *dbfile_name; char *dbschema_name; char errmsg[STRSZ]; if ( nrhs != 9 ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( ! get_dbptr( prhs[0], &db ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsDouble( prhs[1] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsDouble( prhs[2] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[3] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[4] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[5] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[6] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[7] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[8] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } dimensions= (mwSize*) mxGetDimensions( prhs[1] ); if (dimensions[1] != 1 && dimensions[0] != 1) { mexErrMsgTxt("writepolygondata: LAT must have dimensions Nx1 or 1xN\n"); } if (dimensions[1] == 1) { nlat= dimensions[0]; } else { nlat= dimensions[1]; } dimensions= (mwSize *)mxGetDimensions( prhs[2] ); if (dimensions[1] != 1 && dimensions[0] != 1) { mexErrMsgTxt("writepolygondata: LON must have dimensions Nx1 or 1xN\n"); } if (dimensions[1] == 1) { nlon= dimensions[0]; } else { nlon= dimensions[1]; } if (nlat != nlon) { mexErrMsgTxt("writepolygondata: LAT and LON must be of same size\n"); } if( ( retcode = dbquery(db, dbDATABASE_FILENAME, &dbfile_name) ) < 0 ) { mexErrMsgTxt("writepolygondata: problem getting descriptor filename\n"); } if ( is_file(dbfile_name)==1 ) { if( ( retcode = dbquery(db, dbSCHEMA_NAME, &dbschema_name) ) < 0 ) { mexErrMsgTxt("writepolygondata: problem getting schema name\n"); } else { if (strmatches(dbschema_name,"polygon1.2.*", 0) != 1) { sprintf(errmsg, "writepolygondata: wrong schema '%s' for output database '%s'\n",dbschema_name,dbfile_name); mexErrMsgTxt(errmsg); } } } else { sprintf(errmsg, "writepolygondata: please create database schema '%s' with schema polygon1.2\n",dbfile_name); mexErrMsgTxt(errmsg); //there might be a fix, but for the moment this simply does NOT work if (dbcreate(dbfile_name,"polygon1.2",0, 0, 0) != 0 ) { mexErrMsgTxt("writepolygondata: problem with dbcreate\n"); } } nsamp= (long) nlat; lat= mxGetPr(prhs[1]); lon= mxGetPr(prhs[2]); polygon= mxCalloc(nsamp+1,sizeof(Point)); for (i= 0; i < nsamp; i++) { polygon[i].lat=lat[i]; polygon[i].lon=lon[i]; } get_malloced_string(prhs[3],&pname); get_malloced_string(prhs[4],&ptype); get_malloced_string(prhs[5],&auth); get_malloced_string(prhs[6],&dir); get_malloced_string(prhs[7],&dfile); get_malloced_string(prhs[8],&s_pcode); pcode= polycode(s_pcode); /*maybe nsamp -1, check later...*/ writePolygonData(db,polygon,nsamp,pname,1,1,ptype,auth,dir,dfile,pcode); antelope_mex_clear_register(1); mxFree(polygon); mxFree(s_pcode); mxFree(dfile); mxFree(dir); mxFree(auth); mxFree(ptype); mxFree(pname); }
int main(int argc, char **argv) { int verbose = 0; char *dbname = malloc(STRSZ); char *datafilename = malloc(STRSZ); char *name = malloc(STRSZ); char *line = malloc(STRSZ); char str1[20], str2[20], *str3 = malloc(1024); int level = 0; int closed = 0, close = 0; int maxpoints = 5000; Point *poly = malloc(maxpoints * sizeof(double)); double lat, lon; long npoints; char *auth = malloc(STRSZ); char ptype[STRSZ]; char *dir = malloc(STRSZ), *dfile = malloc(STRSZ), *tname = malloc(STRSZ), numstr[10]; int ftype = polyFLOAT; Dbptr db; FILE *fh; int putit; int polycounter = 0; elog_init(argc, argv); my_username(auth); strcat(auth, ":gmt"); strcpy(name, "-"); strcpy(dir, "-"); strcpy(dfile, "gmt.bin"); strcpy(ptype, "-"); for (argc--, argv++; argc > 0; argc--, argv++) { if (!strcmp(*argv, "-auth")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -auth argument.\n"); usage(); exit(1); } auth = *argv; } else if (!strcmp(*argv, "-dir")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -dir argument.\n"); usage(); exit(1); } dir = *argv; } else if (!strcmp(*argv, "-dfile")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -dfile argument.\n"); usage(); exit(1); } dfile = *argv; } else if (!strcmp(*argv, "-pname")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -pname argument.\n"); usage(); exit(1); } name = *argv; } else if (!strcmp(*argv, "-ptype")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -ptype argument.\n"); usage(); exit(1); } strncpy(ptype, *argv, 2); } else if (!strcmp(*argv, "-level")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -level argument.\n"); usage(); exit(1); } level = atoi(*argv); } else if (!strcmp(*argv, "-close_polygons")) { close = 1; } else if (!strcmp(*argv, "-v")) { verbose++; } else if (**argv != '-') { break; } else { elog_complain(0, "unrecognized argument '%s'\n", *argv); usage(); exit(1); } } if (argc < 2) { elog_complain(0, "Need datfile and db arguments.\n"); usage(); exit(1); } datafilename = *argv; argc--; argv++; dbname = *argv; if (access(datafilename, 4)) { elog_die(0, "datafile %s should be READABLE!\n", datafilename); } if (dbcreate(dbname, "polygon1.2", 0, 0, 0)) { elog_die(1, "can't create database %s\n", dbname); } if (!strcmp(name, "-")) { strcpy(name, datafilename); } dbopen(dbname, "r+", &db); db = dblookup(db, 0, "polygon", 0, 0); fh = fopen(datafilename, "r"); putit = 0; npoints = 0; polycounter = 0; while (fgets(line, 80, fh) != 0) { if (line[0] == '>') { if (putit) { strcpy(tname, name); if (polycounter > 0) { sprintf(numstr, ".%d", polycounter); tname = strcat(tname, numstr); } if (verbose) { elog_notify(0, "%s: (%ld points n file %s/%s)\n", tname, npoints, dir, dfile); } if (npoints > 2 && (close || ( (poly[0].lat == poly[npoints - 1].lat) && (poly[0].lon == poly[npoints - 1].lon)))) { closed = 1; } writePolygonData(db, poly, npoints, tname, closed, level, ptype, auth, dir, dfile, ftype); closed = 0; polycounter++; } strtrim(line); npoints = 0; putit = 1; } else { sscanf(line, "%s %s %s", str1, str2, str3); lon = atof(str1); lat = atof(str2); if (npoints > maxpoints - 2) { maxpoints += 5000; poly = realloc(poly, maxpoints); } poly[npoints].lat = lat; poly[npoints].lon = lon; npoints++; } } if (npoints > 0) { strcpy(tname, name); if (polycounter > 0) { sprintf(numstr, ".%d", polycounter); strcat(tname, numstr); } if (verbose) { elog_notify(0, "%s: (%ld points n file %s/%s)\n", tname, npoints, dir, dfile); } if (npoints > 2 && (close || ( (poly[0].lat == poly[npoints - 1].lat) && (poly[0].lon == poly[npoints - 1].lon)))) { closed = 1; } writePolygonData(db, poly, npoints, tname, closed, level, ptype, auth, dir, dfile, ftype); closed = 0; } fclose(fh); elog_print(0, 0); return 0; }