home *** CD-ROM | disk | FTP | other *** search
- /* This program draws a spectrum, possibly with error bars
- possible parameters:
- ash sp[1:1024] {-y 0} {+y 1000} {-b} {-v} {-c}
- sp[min:max] specifies spectrum name and range (default is stdin)
- -y %E specifies the lower bound
- +y %E specifies the upper bound
- -lx logarithmic x-scale
- -ly logarithmic y-scale
- -grid draw grid
- -d display only (don't draw axis)
- -xn don't print x-axis numbers
- -yn don't print y-axis numbers
- -xi x-axis numbers are integers
- -yi y-axis numbers are integers
- -xf x-axis numbers in format a.b
- -yf y-axis numbers in format a.b
- -xe x-axis numbers in format a.bEn
- -ye y-axis numbers in format a.bEn
- -x2 only print every second x number
- -pag clear screen before drawing
- -use use old parameters
- -cur prints cursor position after drawing
- -b draws error bars \
- -v draws vectors - default is point only
- -c draws circles /
- The boundaries of the real data area of the screen are saved
- to /tmp/ashbdry as ASCII integers
-
- Programmer: RAKO
- */
-
- #include <stdio.h>
- #ifdef AMIGA
- #include <gfxamiga.h>
- #include <workbench/workbench.h>
- #include <workbench/startup.h>
- #include <workbench/icon.h>
- extern struct WBStartup *WBenchMsg;
- extern struct IconBase *IconBase;
- #endif
- #ifdef UNIX
- #include <gfx.h>
- #endif
-
- char xfmt[20], yfmt[20];
-
- int dot_flg=TRUE,
- bar_flg=FALSE,
- vec_flg=FALSE,
- point_style=0,
- point_size=0,
- logx=FALSE,
- logy=FALSE,
- vecpos=0;
- int flag_xnum = 1,
- flag_ynum = 1,
- flag_grid = 0,
- xn_count = 0;
- int leftmargin = 520,
- bottommargin = 120,
- rightmargin = _TEKXMAX-120,
- topmargin = _TEKYMAX-120;
- int reversex = 0,
- reversey = 0;
- float *spc,*err,*tim;
-
- help()
- {
- printf("Draw a spectrum with axis\n");
- printf(" possible parameters:\n");
- printf(" ash sp[1:1024] {-y 0} {+y 1000} {-b} {-v} {-c}\n");
- printf(" sp[min:max] specifies spectrum name and range (default is stdin)\n");
- printf(" -y n.m specifies the lower bound\n");
- printf(" +y n.m specifies the upper bound\n");
- printf(" -x n.m specifies starting x\n");
- printf(" +x n.m specifies last x\n");
- printf(" -bnmr leaves space between axis and first/last point\n");
- printf(" -lx logarithmic x-scale\n");
- printf(" -ly logarithmic y-scale\n");
- printf(" -d display only (don't draw axis)\n");
- printf(" -grid draw a grid\n");
- printf(" -zero draw a horizontal line at y=0\n");
- printf(" -xn don't print x-axis numbers\n");
- printf(" -yn don't print y-axis numbers\n");
- printf(" -cn don't print comment\n");
- printf(" -rx reverse x numbering\n");
- printf(" -ry reverse y numbering\n");
- printf(" -xi x-axis numbers are integers\n");
- printf(" -yi y-axis numbers are integers\n");
- printf(" -xf x-axis numbers in format a.b\n");
- printf(" -yf y-axis numbers in format a.b\n");
- printf(" -xe x-axis numbers in format a.bEn\n");
- printf(" -ye y-axis numbers in format a.bEn\n");
- printf(" -x%% cfmt x-axis numbers in FLOAT C format cfmt (needs additional %)\n");
- printf(" -y%% cfmt y-axis numbers in FLOAT C format cfmt (needs additional %)\n");
- printf(" -x2 only print every second x number\n");
- printf(" -pag clear screen before drawing\n");
- printf(" -use use old parameters\n");
- printf(" -cur prints cursor position after drawing\n");
- printf(" -b draws error bars \\\n");
- printf(" -v draws vectors - default is point only\n");
- printf(" -c draws circles /\n");
- printf(" -cir n draws circles with radius n.m\n");
- printf(" -crf n draws filled circles with radius n\n");
- printf(" -tri n draws triangles with baseline n (negative n gives\n");
- printf(" inverted triangles)\n");
- printf(" -trf n draws filled triangles with baseline n\n");
- printf(" -qua n draws quadrates with baseline n\n");
- printf(" -qaf n draws filled quadrates with baseline n\n");
- printf(" -mb n defines bottom margin\n");
- printf(" -mt n defines top margin\n");
- printf(" -mr n defines right margin\n");
- printf(" -ml n defines left margin\n");
- return(0);
- }
-
- minimax(spc,xmax,min,max)
- float spc[],*min,*max;
- int xmax;
- { /* findig maximum and minimum of spectrum */
- int n;
- *max = spc[0]; *min = spc[0];
- for(n=0;n<xmax;n++) {
- if(spc[n] > *max) *max = spc[n];
- if(spc[n] < *min) *min = spc[n];
- }
- if((*max == 0.0) && (*min == 0.0)) {
- *min = -1.0; *max = 1.0;
- }
- return(0);
- }
-
- disp(y,e,t) /* plot a point in the window */
- float y,e,t;
- {
- int n,xx,yy,ee;
- float fx1,fx2,fy1,fy2,fr,phi,finc;
-
- xx=t; yy=y; ee=e;
- if((y < bottommargin) || (y > topmargin)) return(0);
- if((xx < leftmargin) || (xx > rightmargin)) return(0);
- if(dot_flg) {
- posita(xx,yy); vectoa(xx,yy);
- }
- if(bar_flg) {
- posita(xx,yy-ee); vectoa(xx,yy+ee);
- posita(xx,yy);
- }
- if(vec_flg) {
- if(vecpos==0) {
- vecpos=1;
- posita(xx,yy);
- }
- vectoa(xx,yy);
- }
- switch(point_style) {
- case 0:
- return(0);
- break;
- case 1: /* circle */
- fr=point_size;
- xx = t ; yy = y+fr; posita(xx,yy);
- finc=1.57079/fr;
- phi=finc;
- fx1=0; fy1=fr;
- for(n=0; n < (4*point_size); n++) {
- fx2=fr*sin(phi); fy2=fr*cos(phi);
- xx = t + fx2; yy = y + fy2; vectoa(xx,yy);
- phi=phi+finc;
- }
- xx=t; yy=y; posita(xx,yy);
- break;
- case 2: /* filled circle */
- fr=point_size;
- phi=0.0;
- finc=1.57079/fr;
- fx1=0; fy1=fr;
- for(n=0;n <= (point_size + 1);n++) {
- fx2=fr*sin(phi); fy2=fr*cos(phi);
- xx=(t+fx1); yy=(y+fy1); posita(xx,yy);
- xx=(t-fx1); vectoa(xx,yy);
- yy=(y-fy1); posita(xx,yy);
- xx=(t+fx1); vectoa(xx,yy);
- fx1=fx2; fy1=fy2;
- phi=phi+finc;
- }
- xx=t; yy=y; posita(xx,yy);
- break;
- case 3: /* triangle */
- xx=t+point_size; yy=y-point_size; posita(xx,yy);
- xx=t-point_size; vectoa(xx,yy);
- yy=y+point_size; xx=t; vectoa(xx,yy);
- xx=t+point_size; yy=y-point_size; vectoa(xx,yy);
- xx=t; yy=y; posita(xx,yy);
- break;
- case 4: /* filled triangle */
- xx=t-point_size;
- for(n=0;n<(2*point_size);n++) {
- yy=y+point_size; posita((int)t,yy);
- yy=y-point_size; vectoa(xx,yy);
- xx=xx+1;
- }
- xx=t; yy=y; posita(xx,yy);
- break;
- case 5: /* quadrate */
- xx=t+point_size; yy=y-point_size; posita(xx,yy);
- xx=t-point_size; vectoa(xx,yy);
- yy=y+point_size; vectoa(xx,yy);
- xx=t+point_size; vectoa(xx,yy);
- yy=y-point_size; vectoa(xx,yy);
- xx=t; yy=y; posita(xx,yy);
- break;
- case 6: /* filled quadrate */
- yy=y-point_size;
- for(n=0;n<(2*point_size);n++) {
- xx=t+point_size; posita(xx,yy);
- xx=t-point_size; vectoa(xx,yy);
- yy=yy+1;
- }
- xx=t; yy=y; posita(xx,yy);
- break;
- }
- }
-
- float slog(x)
- float x;
- {
- float erg;
- erg = 0.0;
- if( x > 0.0) erg = log(x); /* / 2.3026; */
- return(erg);
- }
-
- float pow10intlog10(x) /* same as n=pow(10,floor(log10( (double)d + 1.0E-10))) */
- float x;
- {
- float xx;
-
- xx=1.0;
- if(x > 1.0) {
- while(xx < x) xx = xx * 10.0;
- xx = xx / 10.0;
- }
- if(x < 1.0) {
- while(xx > x) xx = xx / 10.0;
- }
- return(xx);
- }
-
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int n,xmax;
- float x,
- ymax = 1.0,
- ymin = 0.0,
- yfak = 1.0,
- tmin = 0.0,
- tmax = _MAXSPCLEN,
- tfak = 1.0,
- xoffset = 0,
- yoffset = 0;
- char *z,*comment;
- double ftmp;
- FILE *fp;
- #ifdef AMIGA
- struct WBArg *Arg;
- #endif
-
- z = (char *) malloc(80);
- comment = (char *) malloc(80);
-
- checkopt(argc,argv,"-dummy",z);
-
- spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
- err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
- tim= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
- if(tim==NULL) {
- printf("sorry, not enough memory\n");
- exit(-1);
- }
-
- if(argc > 1) strcpy(z,argv[1]);
- xmax=readspec(z,spc,err,tim,comment);
- /* findig maximum and minimum of spectrum */
- minimax(spc,xmax,&ymin,&ymax);
- if(ymax < 0.0) {
- ymax = 0.9 * ymax;
- } else {
- ymax = 1.1 * ymax;
- }
- if(ymin < 0.0) {
- ymin = 1.1 * ymin;
- } else {
- ymin = 0.9 * ymin;
- }
- minimax(tim,xmax,&tmin,&tmax);
-
-
- if(checkopt(argc,argv,"-use",z)) {
- fp=fopen(ASHBDRY,"r");
- if(fp == NULL) {
- printf("%s not found!\n",ASHBDRY);
- exit(0);
- }
- fscanf(fp,"%d\n%d\n%d\n",&leftmargin,&bottommargin,&rightmargin);
- fscanf(fp,"%d\n%d\n",&topmargin,&_win_flg);
- fscanf(fp,"%lf\n",&ftmp); tmin = (float) ftmp;
- fscanf(fp,"%lf\n",&ftmp); tmax = (float) ftmp;
- fscanf(fp,"%lf\n",&ftmp); ymin = (float) ftmp;
- fscanf(fp,"%lf\n",&ftmp); ymax = (float) ftmp;
- fclose(fp);
- }
-
- if(checkopt(argc,argv,"-bnmr",z)) {
- x = 0.05 * (tmax - tmin);
- tmin = tmin - x;
- tmax = tmax + x;
- }
- if(checkopt(argc,argv,"-grid",z)) flag_grid = 1;
- if(checkopt(argc,argv,"-zero",z)) flag_grid = 2;
- if(checkopt(argc,argv,"-lx",z)) logx=TRUE;
- if(checkopt(argc,argv,"-ly",z)) logy=TRUE;
- flag_xnum = 1; flag_ynum = 1;
- if(checkopt(argc,argv,"-xn",z)) flag_xnum = 0;
- if(checkopt(argc,argv,"-yn",z)) flag_ynum = 0;
- if(checkopt(argc,argv,"-xf",z)) flag_xnum = 2;
- if(checkopt(argc,argv,"-yf",z)) flag_ynum = 2;
- if(checkopt(argc,argv,"-xe",z)) flag_xnum = 3;
- if(checkopt(argc,argv,"-ye",z)) flag_ynum = 3;
- if(checkopt(argc,argv,"-xi",z)) flag_xnum = 4;
- if(checkopt(argc,argv,"-yi",z)) flag_ynum = 4;
- if(checkopt(argc,argv,"-x%",xfmt)) flag_xnum = 5;
- if(checkopt(argc,argv,"-y%",yfmt)) flag_ynum = 6;
- reversex = 0; if(checkopt(argc,argv,"-rx",z)) reversex = 1;
- reversey = 0; if(checkopt(argc,argv,"-ry",z)) reversey = 1;
- if(checkopt(argc,argv,"-x2",z)) xn_count = -1;
-
- if(checkopt(argc,argv,"-y",z)) ymin=atosf(z);
- if(checkopt(argc,argv,"+y",z)) ymax=atosf(z);
- if(checkopt(argc,argv,"-x",z)) tmin=atosf(z);
- if(checkopt(argc,argv,"+x",z)) tmax=atosf(z);
- if(checkopt(argc,argv,"-b",z)) {
- dot_flg=FALSE;
- bar_flg=TRUE;
- }
- if(checkopt(argc,argv,"-v",z)) {
- dot_flg=FALSE;
- vec_flg=TRUE;
- }
- if(checkopt(argc,argv,"-c",z)) {
- dot_flg=FALSE;
- point_style=1;
- point_size=16;
- }
- if(checkopt(argc,argv,"-cir",z)) {
- dot_flg=FALSE;
- point_style=1;
- point_size=atoi(z);
- }
- if(checkopt(argc,argv,"-crf",z)) {
- dot_flg=FALSE;
- point_style=2;
- point_size=atoi(z);
- }
- if(checkopt(argc,argv,"-tri",z)) {
- dot_flg=FALSE;
- point_style=3;
- point_size=atoi(z);
- }
- if(checkopt(argc,argv,"-trf",z)) {
- dot_flg=FALSE;
- point_style=4;
- point_size=atoi(z);
- }
- if(checkopt(argc,argv,"-qua",z)) {
- dot_flg=FALSE;
- point_style=5;
- point_size=atoi(z);
- }
- if(checkopt(argc,argv,"-qaf",z)) {
- dot_flg=FALSE;
- point_style=6;
- point_size=atoi(z);
- }
- if(checkopt(argc,argv,"-mt",z)) topmargin = atoi(z);
- if(checkopt(argc,argv,"-mb",z)) bottommargin = atoi(z);
- if(checkopt(argc,argv,"-ml",z)) leftmargin = atoi(z);
- if(checkopt(argc,argv,"-mr",z)) rightmargin = atoi(z);
-
- tekopen();
-
- if(checkopt(argc,argv,"-pag",z)) dperas();
-
- #ifdef AMIGA
- if(argc == 0) {
- IconBase = (struct IconBase *) OpenLibrary("icon.library", 0L);
- Arg = WBenchMsg->sm_ArgList; Arg++;
- CurrentDir(Arg->wa_Lock);
- strcpy(z,Arg->wa_Name);
- if(strlen(z) < 2) exit(0);
- dperas();
- }
- #endif
-
- if(logy) {
- for(n=0;n<xmax;n++) {
- spc[n] = slog(spc[n]);
- err[n] = slog(err[n]);
- }
- ymin = slog(ymin);
- ymax = slog(ymax);
- }
-
- if(logx) {
- for(n=0;n<xmax;n++) {
- tim[n] = slog(tim[n]);
- }
- tmin = slog(tmin);
- tmax = slog(tmax);
- }
-
- yfak=(topmargin-bottommargin)/(ymax-ymin);
- tfak=(rightmargin-leftmargin)/(tmax-tmin);
- xoffset= ((- tmin) * tfak) + leftmargin;
- yoffset= ((- ymin) * yfak) + bottommargin;
- if(reversex == 1) {
- xoffset = leftmargin + (tmax * tfak);
- tfak = -1 * tfak;
- }
- if(reversey == 1) {
- yoffset = bottommargin + (ymax * yfak);
- yfak = -1 * yfak;
- }
-
- if(!checkopt(argc,argv,"-d",z)) {
- axis(leftmargin,bottommargin,rightmargin,topmargin,tmin,tmax,ymin,ymax);
- }
- for(n=0;n<xmax;n++) {
- disp(spc[n] * yfak + yoffset , err[n] * yfak , tim[n] * tfak + xoffset);
- }
-
- if((!checkopt(argc,argv,"-d",z)) && (!checkopt(argc,argv,"-cn",z))) {
- posita(leftmargin+80,topmargin+32);
- gfxtext(comment,0.0);
- }
- gfxflush();
-
- unlink(ASHBDRY);
- /* print out boundaries of usable data area */
- fp=fopen(ASHBDRY,"w");
- fprintf(fp,"%d\n%d\n%d\n",leftmargin,bottommargin,rightmargin);
- fprintf(fp,"%d\n%d\n%E\n",topmargin,_win_flg,tmin);
- fprintf(fp,"%E\n%E\n%E\n",tmax,ymin,ymax);
- fprintf(fp,"%E\n",_tica);
- fclose(fp);
- close(_tek4014);
- free(z); free(comment);
- free(spc); free(err); free(tim);
- return(0);
- }
-
- /* ----------------------------------------------------------------------
- Draw axis and numbers
- ---------------------------------------------------------------------- */
-
- prettystr1(s,x,flag) /* generate string from fp number */
- char s[];
- float x;
- int flag;
- {
- int ix,fx;
- int i,l;
-
- if(flag == 5) {sprintf(s,xfmt,x); return(0);}
- if(flag == 6) {sprintf(s,yfmt,x); return(0);}
- if(flag == 0) {s[0] = 0; return(0);}
- if(flag == 4) {fx = (x + 0.4); sprintf(s,"%-d",fx); return(0);}
- if(flag == 3) {sprintf(s,"%-1.1E",x); return(0);}
- sprintf(s,"%-3.5f",x);
- l = strlen(s) - 1;
- if(instr(".",s) >= 0) while(s[l] == '0') s[l--] = 0;
- if(flag == 2) return(0);
- ix=x; fx=ix; if((x - fx) == 0.0) sprintf(s,"%-d",ix);
- if(fabs(x) < 0.001) sprintf(s,"%-.3E",x);
- if(fabs(x) > 99999.999) sprintf(s,"%-1.1E",x);
- if(fabs(x)<1.0E-6) strcpy(s,"0");
- while(s[0] < 33) {
- l = strlen(s);
- for(i = 0; i <= l; i++) s[i] = s[i+1];
- }
- return(0);
- }
-
- prettystr(s,x,flag)
- char s[];
- float x;
- int flag;
- {
- int i,n;
- char *z1, *z2;
-
- prettystr1(s,x,flag);
-
- z1 = (char *) malloc(80);
- z2 = (char *) malloc(80);
-
- n = instr("E",s);
- if(n > 0) {
- if(xn_count == 0) xn_count = 1;
- midstr(z1,s,0,n-1);
- midstr(z2,s,n+1,strlen(s));
- i = atoi(z2);
- sprintf(s,"%s\\u . \\d10\\u%d\\d",z1,i);
- }
- free(z1); free(z2);
- return(0);
- }
-
- axis(sx,sy,ex,ey,xmin,xmax,ymin,ymax) /* draw frame, ticks aand numbers */
- int sx,sy,ex,ey;
- float xmin,xmax,ymin,ymax;
- {
- int x,y,lx2;
- int txth, txth1;
- float nx,ny,ninc;
- char *s;
-
- s = (char *) malloc(256);
-
- txth = textheight();
- txth1 = txth + 30;
-
- /* draw frame */
- posita(sx,sy);
- vectoa(ex,sy);
- vectoa(ex,ey);
- vectoa(sx,ey);
- vectoa(sx,sy);
-
- x = (sx + ex) / 2; y = (sy + ey) / 2;
-
- calcnums(xmin,xmax,&nx,&ninc);
- if(logx) nx = pow10intlog10(xmin);
- while((x < (ex + 1)) && (x > (sx - 1))) {
- x = sx + ((ex-sx) * ((nx-xmin)/(xmax-xmin)));
- if(logx) x = sx + ((ex-sx) * ((slog(nx)-xmin)/(xmax-xmin)));
- if(reversex == 1) x = ex - ((ex-sx) * ((nx-xmin)/(xmax-xmin)));
- if((x>ex) || (x<sx)) break;
- posita(x,sy);
- if(flag_grid == 1) {
- vectoa(x,ey);
- } else {
- vectoa(x,sy+20);
- posita(x,ey); vectoa(x,ey-20);
- }
- xn_count = -1 * xn_count;
- if(xn_count >= 0) {
- prettystr(s,nx,flag_xnum);
- lx2 = textlen(s) >> 1;
- if((x + lx2) < rightmargin) {
- posita(x - lx2,sy - txth1);
- gfxtext(s,0.0);
- }
- }
- if(logx) {
- if(nx == 0) nx = 0.1;
- nx = nx * 10;
- } else {
- nx = nx + ninc;
- }
- }
-
- calcnums(ymin,ymax,&ny,&ninc);
- if(logy) ny = pow10intlog10(ymin);
- while((y < (ey + 1)) && (y > (sy -1))) {
- y = sy + ((ey-sy) * ((ny-ymin)/(ymax-ymin)));
- if(logy) y = sy + ((ey-sy) * ((slog(ny)-ymin)/(ymax-ymin)));
- if(reversey == 1) y = ey - ((ey-sy) * ((ny-ymin)/(ymax-ymin)));
- if((y>ey) || (y<sy)) break;
- posita(sx,y);
- if(flag_grid == 1) {
- vectoa(ex,y);
- } else {
- vectoa(sx+20,y);
- posita(ex,y); vectoa(ex-20,y);
- }
- if((fabs(ny) < 0.0001) && (flag_grid == 2)) {
- posita(sx,y);
- vectoa(ex,y);
- }
- prettystr(s,ny,flag_ynum);
- x = sx - textlen(s) - 20;
- if(x < 0 ) x = 1;
- posita(x,y - (txth1 >> 1));
- gfxtext(s,0.0);
- if(logy) {
- if(ny == 0) ny = 0.1;
- ny = ny * 10;
- } else {
- ny = ny + ninc;
- }
- }
- free(s);
- return(0);
- }
-
-
- calcnums(min,max,ny,ninc) /* calculate increment for axis numbers */
- float min,max,*ny,*ninc;
- {
- float d,n;
- int m;
-
- d=fabs(max-min);
- n=pow10intlog10(d);
- m=d/n;
- switch(m) {
- case 1:
- *ninc=n/10;
- break;
- case 2:
- *ninc=n/4;
- break;
- default:
- *ninc=n;
- break;
- }
- *ny=n * floor(min/n);
- while(*ny < min) *ny = *ny + *ninc;
- if((d / *ninc) > 10.0) *ninc = *ninc * 2;
- return(0);
- }