static double getflt(void) { register int c, i, dp; double d, e; int f; d = 0.; dp = 0; do c = get(); while(c == ' ' || c == '\t'); l1: if(c >= '0' && c <= '9') { d = d*10. + c-'0'; if(dp) dp++; c = get(); goto l1; } if(c == '.') { dp++; c = get(); goto l1; } if(dp) dp--; if(c == '+' || c == '-') { f = 0; if(c == '-') f++; i = 0; c = get(); while(c >= '0' && c <= '9') { i = i*10 + c-'0'; c = get(); } if(f) i = -i; dp -= i; } e = 1.; i = dp; if(i < 0) i = -i; while(i--) e *= 10.; if(dp < 0) d *= e; else d /= e; if(c == '|') return(d/getflt()); peekc = c; return(d); }
static double ogetflt() /* get a floating point number */ { extern double getflt(); double r; r = getflt(infp); if (feof(infp)) octerror(USER, "truncated octree"); return(r); }
static int convr(struct unit *up) { register struct unit *p; register int c; register char *cp; char name[LINE_MAX+1]; int den, err; p = up; for(c=0; c<NDIM; c++) p->dim[c] = 0; p->factor = getflt(); if(p->factor == 0.) p->factor = 1.0; err = 0; den = 0; cp = name; loop: switch(c=get()) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': case '/': case ' ': case '\t': case '\n': if(cp != name) { *cp++ = 0; cp = name; err |= lookup(cp, p, den, c); } if(c == '/') den++; if(c == '\n') return(err); goto loop; } if (cp >= &name[sizeof name - 2]) cp--; *cp++ = c; goto loop; }
/* pscanf: minimal scanf */ void pscanf(char *fmt, ...) { va_list ap; int *iptr; float *fptr; char *cptr, *sptr, *p; va_start(ap, fmt); for (p=fmt; *p != '\0'; p++) { if (*p != '%') continue; switch (*++p) { case 'c': cptr = va_arg(ap, char *); *cptr = getch(); break; case 'd': iptr = va_arg(ap, int *); *iptr = getint(); break; case 'f': fptr = va_arg(ap, float *); *fptr = getflt(); break; case 's': sptr = va_arg(ap, char *); sptr = getstr(sptr); break; default: break; } } va_end(ap); }
/* Read a BSDF mesh interpolant from the given binary stream */ int load_bsdf_rep(FILE *ifp) { RBFNODE rbfh; int from_ord, to_ord; int i; clear_bsdf_rep(); if (ifp == NULL) return(0); if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | !input_orient | !output_orient) { fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", progname); return(0); } rbfh.next = NULL; /* read each DSF */ rbfh.ejl = NULL; while ((rbfh.ord = getint(4, ifp)) >= 0) { RBFNODE *newrbf; rbfh.invec[0] = getflt(ifp); rbfh.invec[1] = getflt(ifp); rbfh.invec[2] = getflt(ifp); if (normalize(rbfh.invec) == 0) { fprintf(stderr, "%s: zero incident vector\n", progname); return(0); } rbfh.vtotal = getflt(ifp); rbfh.nrbf = getint(4, ifp); newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(rbfh.nrbf-1)); if (newrbf == NULL) goto memerr; memcpy(newrbf, &rbfh, sizeof(RBFNODE)-sizeof(RBFVAL)); for (i = 0; i < rbfh.nrbf; i++) { newrbf->rbfa[i].peak = getflt(ifp); newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff; newrbf->rbfa[i].gx = getint(1, ifp) & 0xff; newrbf->rbfa[i].gy = getint(1, ifp) & 0xff; } if (feof(ifp)) goto badEOF; /* insert in global list */ if (insert_dsf(newrbf) != rbfh.ord) { fprintf(stderr, "%s: error adding DSF\n", progname); return(0); } } /* read each migration matrix */ while ((from_ord = getint(4, ifp)) >= 0 && (to_ord = getint(4, ifp)) >= 0) { RBFNODE *from_rbf = get_dsf(from_ord); RBFNODE *to_rbf = get_dsf(to_ord); MIGRATION *newmig; int n; if ((from_rbf == NULL) | (to_rbf == NULL)) { fprintf(stderr, "%s: bad DSF reference in migration edge\n", progname); return(0); } n = from_rbf->nrbf * to_rbf->nrbf; newmig = (MIGRATION *)malloc(sizeof(MIGRATION) + sizeof(float)*(n-1)); if (newmig == NULL) goto memerr; newmig->rbfv[0] = from_rbf; newmig->rbfv[1] = to_rbf; memset(newmig->mtx, 0, sizeof(float)*n); for (i = 0; ; ) { /* read sparse data */ int zc = getint(1, ifp) & 0xff; if ((i += zc) >= n) break; if (zc == 0xff) continue; newmig->mtx[i++] = getflt(ifp); } if (feof(ifp)) goto badEOF; /* insert in edge lists */ newmig->enxt[0] = from_rbf->ejl; from_rbf->ejl = newmig; newmig->enxt[1] = to_rbf->ejl; to_rbf->ejl = newmig; /* push onto global list */ newmig->next = mig_list; mig_list = newmig; } return(1); /* success! */ memerr: fprintf(stderr, "%s: Out of memory in load_bsdf_rep()\n", progname); exit(1); badEOF: fprintf(stderr, "%s: Unexpected EOF in load_bsdf_rep()\n", progname); return(0); }
main(int ac,char **av) { FILE *fpr, *fpw, *fopfile(); float *per, *resid, vs30, cdst, xcos, ycos, tmin, tmax; float *bias, *sigma, *sigma0, *cl90m, *cl90p; int nstat, nper, nval, i; float min_cdst = -1e+15; float max_cdst = 1e+15; float min_vs30 = -1e+15; float max_vs30 = 1e+15; float min_xcos = -1e+15; float max_xcos = 1e+15; float min_ycos = -1e+15; float max_ycos = 1e+15; char residfile[256], fileroot[256], comp[16], rdcomp[16]; char *sptr, string[2048]; setpar(ac,av); mstpar("residfile","s",residfile); mstpar("fileroot","s",fileroot); mstpar("comp","s",comp); mstpar("nstat","d",&nstat); mstpar("nper","d",&nper); getpar("min_cdst","f",&min_cdst); getpar("max_cdst","f",&max_cdst); getpar("min_vs30","f",&min_vs30); getpar("max_vs30","f",&max_vs30); getpar("min_xcos","f",&min_xcos); getpar("max_xcos","f",&max_xcos); getpar("min_ycos","f",&min_ycos); getpar("max_ycos","f",&max_ycos); endpar(); per = (float *) check_malloc (nper*sizeof(float)); bias = (float *) check_malloc (nper*sizeof(float)); sigma = (float *) check_malloc (nper*sizeof(float)); sigma0 = (float *) check_malloc (nper*sizeof(float)); cl90m = (float *) check_malloc (nper*sizeof(float)); cl90p = (float *) check_malloc (nper*sizeof(float)); resid = (float *) check_malloc (nstat*nper*sizeof(float)); fpr = fopfile(residfile,"r"); fgets(string,2048,fpr); sptr = skipval(13,string); for(i=0;i<nper;i++) sptr = getflt(&per[i],sptr); nval = 0; while(fgets(string,2048,fpr) != NULL) { sscanf(string,"%*s %*f %*s %*f %*f %*d %f %f %f %f %f %f %s",&vs30,&cdst,&xcos,&ycos,&tmin,&tmax,rdcomp); if((vs30 >= min_vs30 && vs30 <= max_vs30) && (cdst >= min_cdst && cdst <= max_cdst) && (xcos >= min_xcos && xcos <= max_xcos) && (ycos >= min_ycos && ycos <= max_ycos) && (strcmp(rdcomp,comp)==0) ) { sptr = skipval(13,string); for(i=0;i<nper;i++) sptr = getflt(&resid[i+nval*nper],sptr); nval++; } if(nval>nstat) { fprintf(stderr,"(nval= %d) > (nstat= %d), exiting...\n",nval,nstat); exit(-1); } } fclose(fpr); fprintf(stderr,"nval= %d\n",nval); uncert(nval,nper,resid,bias,sigma,sigma0,cl90m,cl90p); sprintf(string,"%s.bias",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],bias[i]); fclose(fpw); sprintf(string,"%s.sigma",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],sigma[i]); fclose(fpw); sprintf(string,"%s.sigma0",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],sigma0[i]); fclose(fpw); sprintf(string,"%s.m90",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],cl90m[i]); fclose(fpw); sprintf(string,"%s.p90",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],cl90p[i]); fclose(fpw); }
static GwyDataField* read_data_field(const guchar *buffer, guint size, guchar version, gchar **title, gint *direction, GError **error) { enum { MIN_REMAINDER = 2620 }; /* information offsets in different versions, in r5+ relative to data * start, in order: * data offset, * pixel dimensions, * physical dimensions, * value multiplier, * unit string, * data type, (if zero, use channel title) * channel title (if zero, use data type) */ const guint offsets34[] = { 0x0104, 0x0196, 0x01a2, 0x01b2, 0x01c2, 0x0400, 0x0000 }; const guint offsets56[] = { 0x0104, 0x025c, 0x0268, 0x0288, 0x02a0, 0x0708, 0x0000 }; const guint offsets7[] = { 0x0104, 0x029c, 0x02a8, 0x02c8, 0x02e0, 0x0000, 0x0b90 }; gint xres, yres, doffset, i, power10, type; gdouble xreal, yreal, q, z0; GwyDataField *dfield; GwySIUnit *unitxy, *unitz; gdouble *data; const guint *offset; const guchar *p, *r, *last; /* get floats in single precision from r4 but double from r5+ */ gdouble (*getflt)(const guchar**); *title = NULL; *direction = -1; if (version == '5' || version == '6' || version == '7') { /* There are more headers in r5, * try to find something that looks like #R5. */ last = r = buffer; while ((p = memchr(r, '#', size - (r - buffer) - MIN_REMAINDER))) { if (p[1] == 'R' && p[2] == version && p[3] == '.') { gwy_debug("pos: %ld", (long)(p - buffer)); last = p; r = p + MIN_REMAINDER-1; } else r = p + 1; } offset = (version == '7') ? &offsets7[0] : &offsets56[0]; buffer = last; getflt = &gwy_get_gdouble_le; } else { offset = &offsets34[0]; getflt = &get_gfloat_le_as_double; } p = buffer + *(offset++); doffset = gwy_get_guint32_le(&p); /* this appears to be the same number as in the ASCII miniheader -- so get it here since it's easier */ gwy_debug("data offset = %u", doffset); p = buffer + *(offset++); xres = gwy_get_guint32_le(&p); yres = gwy_get_guint32_le(&p); if (err_DIMENSION(error, xres) || err_DIMENSION(error, yres)) return NULL; p = buffer + *(offset++); xreal = -getflt(&p); xreal += getflt(&p); yreal = -getflt(&p); yreal += getflt(&p); if (!((xreal = fabs(xreal)) > 0)) { g_warning("Real x size is 0.0, fixing to 1.0"); xreal = 1.0; } if (!((yreal = fabs(yreal)) > 0)) { g_warning("Real y size is 0.0, fixing to 1.0"); yreal = 1.0; } p = buffer + *(offset++); q = getflt(&p); z0 = getflt(&p); gwy_debug("xreal.raw = %g, yreal.raw = %g, q.raw = %g, z0.raw = %g", xreal, yreal, q, z0); p = buffer + *(offset++); unitz = gwy_si_unit_new_parse(p, &power10); q *= pow10(power10); z0 *= pow10(power10); unitxy = gwy_si_unit_new_parse(p + 10, &power10); xreal *= pow10(power10); yreal *= pow10(power10); gwy_debug("xres = %d, yres = %d, xreal = %g, yreal = %g, q = %g, z0 = %g", xres, yres, xreal, yreal, q, z0); gwy_debug("unitxy = %s, unitz = %s", p, p + 10); if (offset[1]) { /* We know channel title */ offset++; p = buffer + *(offset++); *title = g_strndup(p, size - (p - buffer)); gwy_debug("title = <%s>", *title); } else { /* We know data type */ p = buffer + *(offset++); type = gwy_get_guint16_le(&p); *direction = gwy_get_guint16_le(&p); gwy_debug("type = %d, dir = %d", type, *direction); offset++; *title = type_to_title(type); } p = buffer + doffset; if (err_SIZE_MISMATCH(error, 2*xres*yres, size - (p - buffer), FALSE)) return NULL; dfield = gwy_data_field_new(xres, yres, xreal, yreal, FALSE); gwy_data_field_set_si_unit_xy(dfield, unitxy); g_object_unref(unitxy); gwy_data_field_set_si_unit_z(dfield, unitz); g_object_unref(unitz); data = gwy_data_field_get_data(dfield); for (i = 0; i < xres*yres; i++) data[i] = (p[2*i] + 256.0*p[2*i + 1])*q + z0; return dfield; }
int readfargs( /* read function arguments from stream */ FUNARGS *fa, FILE *fp ) { #define getstr(s) (fgetword(s,sizeof(s),fp)!=NULL) #define getint(s) (getstr(s) && isint(s)) #define getflt(s) (getstr(s) && isflt(s)) char sbuf[MAXSTR]; int n, i; if (!getint(sbuf) || (n = atoi(sbuf)) < 0) return(0); if ( (fa->nsargs = n) ) { fa->sarg = (char **)malloc(n*sizeof(char *)); if (fa->sarg == NULL) return(-1); for (i = 0; i < fa->nsargs; i++) { if (!getstr(sbuf)) return(0); fa->sarg[i] = savestr(sbuf); } } else fa->sarg = NULL; if (!getint(sbuf) || (n = atoi(sbuf)) < 0) return(0); #ifdef IARGS if (fa->niargs = n) { fa->iarg = (long *)malloc(n*sizeof(long)); if (fa->iarg == NULL) return(-1); for (i = 0; i < n; i++) { if (!getint(sbuf)) return(0); fa->iarg[i] = atol(sbuf); } } else fa->iarg = NULL; #else if (n != 0) return(0); #endif if (!getint(sbuf) || (n = atoi(sbuf)) < 0) return(0); if ( (fa->nfargs = n) ) { fa->farg = (RREAL *)malloc(n*sizeof(RREAL)); if (fa->farg == NULL) return(-1); for (i = 0; i < n; i++) { if (!getflt(sbuf)) return(0); fa->farg[i] = atof(sbuf); } } else fa->farg = NULL; return(1); #undef getflt #undef getint #undef getstr }