/* @(#)tbcomsel.c 17.1.1.1 (ESO-IPG) 01/25/02 17:47:10 */ /*=========================================================================== Copyright (C) 1995 European Southern Observatory (ESO) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. Correspondence concerning ESO-MIDAS should be addressed as follows: Internet e-mail: midas@eso.org Postal address: European Southern Observatory Data Management Division Karl-Schwarzschild-Strasse 2 D 85748 Garching bei Muenchen GERMANY ===========================================================================*/ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .TYPE Module .NAME tbcompute.c .LANGUAGE C .AUTHOR IPG-ESO Garching .CATEGORY table utilities .COMMENTS This module implements the following Midas commands: \begin{TeX} \begin{enumerate} \item {\tt SELECT/TABLE} table column expresion \item {\tt COMPUTE/TABLE} table column = expression \end{enumerate} \end{TeX} .VERSION 1.0 7-may-1992 Definition M. Peron 010329 last modif ---------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include #define PARLEN 256 #define STRINGLEN 256 #define CRITLEN 8000 /* from 512 before */ #define TBL_tmp "MIDDUMcom.tbl" #define BUFM 30 /* from 15 */ #define BUFK 2000 #define DEGREE 0.01745329251994329576923 #define INT(x) (x < 0 ) ? (int) (x-0.5) : (int) (x+0.5) #define issign(c) ((c == '+') || (c == '-')) char *functions[] = { "SQRT","LN","LOG10","EXP","SIN","COS", "TAN","ASIN","ACOS","ATAN","SINH","COSH", "TANH","ABS","INT","FRAC","MIN","MAX","MOD", "TOLOWER","TOUPPER","CONCAT","COLLAPSE","TOCHAR" }; char *delim = "+-*/(),"; char *opera[] = { "LE","LT","GE","GT","EQ","NE", "AND","OR","NOT" }; int get_token(),readata(),writedata(),arithm1(),arithm2(); int calfun(),constfun(),stucmp(), logchar(); int level1(),level2(),level3(),level4(),level5(),level6(); char *line,*token; int token_type,action,refrow,*rownumber,number_of_rows,associate; int first,otype,oitem,exist,nochar,colitem; double tdtrue,tdfalse; int tmno[BUFM],tmnoc[BUFM],what[BUFM]; char oform[TBL_FORLEN+1]; char outcol[1+TBL_LABLEN]; double fmod(); #ifdef NO_FMOD double fmod(x,y) double x,y; { double res; res = x - y* ((int) (x/y)); return res; } #endif tbl_comp() /*++++++++++++++++++ .PURPOSE compute table column .RETURNS Status ------------------*/ { int i,status; int dummy, nline, end; int ibuf[7],tid, width, nconst[BUFM]; int ocol,nrow,nrall,ncol,inull,nitem,dunit,ipos; char intable[82],type; float tblsel; double *data[BUFM],consta[BUFM]; char *string[BUFM], *cdata[BUFM],history[80]; char *linesave, *mline; /* get machine characteristics*/ TCMCON(&tblsel,&tdtrue,&tdfalse); action = 0; associate = 0; token = osmmget(PARLEN); mline = osmmget(STRINGLEN+2); line = mline; linesave = osmmget(STRINGLEN+2); oscfill(line,STRINGLEN+2,'\0'); for (i=0 ; i0) status = SCKWRI("MID$SELIDX",isel,1,msel,&unit); } status = SCKWRI("OUTPUTI",&nsel,1,1,&cdummy); line -= nline; } osmmfree(mline); osmmfree(token); free(isel); for (i=0; i0) { n = len; token_type = 3; while (n--) *temp++ = *line++; *temp = '\0'; } else if (*line == '.') { *line ++; while (!sdelim(line)) *temp++ = *line++; *temp = '\0'; for (i=0; i<9 ;i++) if ((n = stucmp(token,opera[i])) == 0 ){ (void) sprintf(token,"%d",i); token_type = 5; break; } if (n != 0) { if (token[strloc(token,' ')]) token[strloc(token,' ')] = '\0'; (void) sprintf(text,"Unknown operator : %s",token); SCTPUT(text); SCSEPI(); } *line++; } else { while (!sdelim(line)) *temp++ = *line++; *temp = '\0'; if (stuindex(token,"null") == 0) { token_type = 3; *token = 'N'; } else if ((stuindex(token,"seq") == 0) || (stuindex(token,"sel") == 0)) { token_type = 2; *token = toupper(token[2]); } else if ((n = stucmp(token,"REFVAL")) == 0) { token_type = 7; line++; temp = token; while (!sdelim(line) && *line != ' ' ) *temp++ = *line++; *temp = '\0'; line++; } else { for (i=0; i<24 ;i++) if ((n = stucmp(token,functions[i])) == 0 ){ token_type = 4; (void) sprintf(token,"%d",i); break; } if (n != 0) { if (token[strloc(token,' ')]) token[strloc(token,' ')] = '\0'; (void) sprintf(text,"Unknown function : %s",token); SCTPUT(text); SCSEPI(); } } } } int level00(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; { char newstring[1]; register int ope,ct; register int sens,sensc,s1,s2; level0(ibuf,data,cdata,consta,nconst,string); /*if (token_type !=0 && token_type !=5) { SCTPUT("Missing Operand"); SCSEPI(); }*/ while ((token_type == 5) && ((ope = atoi(token)) >= 6)) { sens = ibuf[3]; sensc = first; get_token(); ct = token_type; s2 = ibuf[4]; s1 = ibuf[6]; level0(ibuf,data,cdata,consta,nconst,string); sens = sens-ibuf[3]; s1 = s1-ibuf[6]; s2 = s2-ibuf[4]; if (sens < 0 && sensc ==0 ) { logic2(ope,data[ibuf[3]-1],data[ibuf[3]],ibuf[2]); ibuf[3] = ibuf[3]-1; } else { logic1(ope,data[ibuf[3]],ibuf[2],consta[ibuf[5]],sens); ibuf[5] = ibuf[5]-1; first=0; } } } int level0(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; { register int ope,ct; register int sens,sensc,s1,s2; char newstring[1]; level1(ibuf,data,cdata,consta,nconst,string); while ((token_type == 5) && ((ope = atoi(token)) < 6)) { sens = ibuf[3]; sensc = first; ope = atoi(token); get_token(); ct = token_type; s1 = ibuf[6]; s2 = ibuf[4]; level1(ibuf,data,cdata,consta,nconst,string); sens = sens-ibuf[3]; s1 = s1-ibuf[6]; s2 = s2-ibuf[4]; if (ct == 6 || (ct == 7 && s2 < 0)) logchar(ope,data,cdata,string[ibuf[4]],nconst,ibuf,0); else if (s1 != 0 && s2 == 0 ) { newstring[0] = '\0'; logchar(ope,data,cdata,newstring,nconst,ibuf,1); } else if (sens < 0 && sensc ==0 ) { logic2(ope,data[ibuf[3]-1],data[ibuf[3]],ibuf[2]); ibuf[3] = ibuf[3]-1; } else { logic1(ope,data[ibuf[3]],ibuf[2],consta[ibuf[5]],sens); ibuf[5] = ibuf[5]-1; first=0; } } } int level1(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; { register char op; register int sens,sensc; char text[80]; level2(ibuf,data,cdata,consta,nconst,string); /* holds the addition and subtraction */ while((op = *token) == '+' || op == '-'){ sens = ibuf[3]; sensc = first; get_token(); if (token_type == 0 || (token_type == 1 && *token != '+' && *token!= '-') && *token != '(') { SCTPUT("Missing Operand"); SCSEPI(); } level2(ibuf,data,cdata,consta,nconst,string); sens = sens-ibuf[3]; if ( sens == 0 && sensc == 1 ) { arithm0(op,&consta[ibuf[5]-1],&consta[ibuf[5]]); ibuf[5] = ibuf[5]-1; } else if (sens < 0 && sensc ==0 ) { arithm2(op,data[ibuf[3]-1],data[ibuf[3]],ibuf[2]); ibuf[3] = ibuf[3]-1; } else { arithm1(op,data[ibuf[3]],ibuf[2],consta[ibuf[5]],sens); ibuf[5] = ibuf[5]-1; first = 0; } } } int level2(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; { register char op; register int sens,sensc; char text[80]; level3(ibuf,data,cdata,consta,nconst,string); /* holds the multiplication and division */ while((op = *token) == '*' || op == '/'){ get_token(); if (token_type == 0 || (token_type == 1 && *token != '+' && *token!= '-') && *token != '(') { SCTPUT("Missing Operand"); SCSEPI(); } sens = ibuf[3]; sensc = first; level3(ibuf,data,cdata,consta,nconst,string); sens = sens-ibuf[3]; if ( sens == 0 && sensc == 1 ) { arithm0(op,&consta[ibuf[5]-1],&consta[ibuf[5]]); ibuf[5] = ibuf[5]-1; } else if (sens < 0 && sensc ==0 ) { arithm2(op,data[ibuf[3]-1],data[ibuf[3]],ibuf[2]); ibuf[3] = ibuf[3]-1; } else { arithm1(op,data[ibuf[3]],ibuf[2],consta[ibuf[5]],sens); ibuf[5] = ibuf[5]-1; first = 0; } } } int level4(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; { /* holds the exponent*/ register char op; register int sens,sensc; char text[80]; level5(ibuf,data,cdata,consta,nconst,string); if (*token== '^') { get_token(); if (token_type == 0 || (token_type == 1 && *token != '+' && *token!= '-') && *token != '(') { SCTPUT("Missing Operand"); SCSEPI(); } sens = ibuf[3]; sensc = first; level4(ibuf,data,cdata,consta,nconst,string); sens = sens-ibuf[3]; if ( sens == 0 && sensc == 1 ) { arithm0('^',&consta[ibuf[5]-1],&consta[ibuf[5]]); ibuf[5] = ibuf[5]-1; } else if (sens < 0 && sensc ==0 ) { arithm2('^',data[ibuf[3]-1],data[ibuf[3]],ibuf[2]); ibuf[3] = ibuf[3]-1; } else { arithm1('^',data[ibuf[3]],ibuf[2],consta[ibuf[5]],sens); ibuf[5] = ibuf[5]-1; first = 0; } } } int level3(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; /* handles the unary operation */ { register char op; int sens; op = 0; if ( (*token == '-' || *token == '+') && token_type != 6){ sens = ibuf[3]; op = *token; get_token(); } level4(ibuf,data,cdata,consta,nconst,string); if (op) if (sens == ibuf[3] && op == '-' ) consta[ibuf[5]] = -consta[ibuf[5]]; else unary(op,data[ibuf[3]],ibuf[2]); } int level5(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; /* handles the functions */ { register int fu; int sens; if (token_type == 4){ fu = atoi(token); get_token(); if (*token != '(') { SCTPUT("Missing parenthesis"); SCSEPI(); } sens = ibuf[3]; level6(ibuf,data,cdata,consta,nconst,string); if (fu == 23) convchar(cdata,data,nconst,ibuf); else if (fu > 18) { charfun(fu,cdata,nconst,string,ibuf); } else if (sens == ibuf[3]) { constfun(fu,data[ibuf[3]],&consta[ibuf[5]],ibuf[2]); } else if (fu > 15) { if (ibuf[3]-sens == 2) { calfun(fu,data[ibuf[3]-1],data[ibuf[3]],ibuf[2]); ibuf[3] = ibuf[3]-1; } else { constfun(fu,data[ibuf[3]],&consta[ibuf[5]],ibuf[2]); ibuf[5] = ibuf[5]-1; first = 0; } } else calfun(fu,data[ibuf[3]],data[ibuf[3]],ibuf[2]); } else level6(ibuf,data,cdata,consta,nconst,string); } int level6(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; { if ((token_type == 5) && (*token == '8')) { get_token(); level7(ibuf,data,cdata,consta,nconst,string); logic1(8,data[ibuf[3]],ibuf[2],consta[ibuf[5]],0); } else level7(ibuf,data,cdata,consta,nconst,string); } int level7(ibuf,data,cdata,consta,nconst,string) int ibuf[7],nconst[]; double *data[],consta[]; char *string[], *cdata[]; { if (*token == '(') { get_token(); level00(ibuf,data,cdata,consta,nconst,string); nochar = ibuf[4]; if (*token == ',') { get_token(); level1(ibuf,data,cdata,consta,nconst,string); nochar = nochar-ibuf[4]; } if (*token != ')' ) { SCTPUT("Unbalanced parenthesis"); SCSEPI(); } } else readata(ibuf,data,cdata,consta,nconst,string); if (token_type == 3) first = 1; else if (token_type != 1) first = 0; get_token(); } int find_string(ch,s) char *ch,*s; { while (*s) if (*s++==*ch) return 1; return 0; } int sdelim(s) char *s; { if (find_string(s,"+-/*(),.") || *s==9 || *s=='\r' || *s==0) return 1; return 0; } int stsnum(s) char *s; /* IN: String to scan */ { char *p, x,y; p = s; if (issign(*p)) p++; while (isdigit(*p)) p++; if (*p == '.') { x = toupper(*(p+1)); /* if (x != '\0' && !isdigit(x)) return(0);*/ y = toupper(*(p+2)); if (( x == 'G') || (x == 'L') || (x == 'N') || (x == 'A') || (x == 'O')) return (p-s); else if ((x == 'E') && (y == 'Q')) return (p-s); else { for (++p; isdigit(*p); p++) ; x = toupper(*p); if ( (x == 'E') || (x == 'D')) /* Look for Exponent */ { p++; if (issign(*p)) p++; while (isdigit(*p)) p++; } } } else { x=toupper(*p); if ((x == 'E') || (x == 'D')) { if (issign(*(p+1)) || isdigit(*(p+1))) { p++; if (issign(*p)) p++; while (isdigit(*p)) p++; } } } return (p-s); } int arithm2(op,x,y,nrow) char op; double *x,*y; int nrow; { int i; double pow(); switch (op) { case '+': for (i=0; i 1 ) toNULLD(x+i) ; else *(x+i) = asin(*(x+i))/DEGREE; break; case 8: for (i=0; i 1 ) toNULLD(x+i) ; else *(x+i) = acos(*(x+i))/DEGREE; break; case 9: /* atan */ for (i=0; i MAXLONG)) toNULLD(x+i); else *(x+i) = INT(*(x+i)); break; case 15: /*frac*/ for (i=0; i 1 )) toNULLD(dvar) ; else *dvar = asin(*dvar)/DEGREE; break; case 8: if ( isNULLD(dvar) || (ABSOLUTE(*dvar) > 1 )) toNULLD(dvar) ; else *dvar = acos(*dvar)/DEGREE; break; case 9: if(!isNULLD(dvar)) *dvar = atan(*dvar)/DEGREE; break; case 10: if(!isNULLD(dvar)) *dvar = sinh(*dvar); break; case 11: if(!isNULLD(dvar)) *dvar = cosh(*dvar); case 12: if(!isNULLD(dvar)) *dvar = tanh(*dvar); break; case 13: if(!isNULLD(dvar)) *dvar = ABSOLUTE(*dvar); break; case 14: if(!isNULLD(dvar)) *dvar = INT(*dvar); break; case 15: if(!isNULLD(dvar)) *dvar = *dvar - (int) (*dvar); break; case 16: for (i=0; i= co) *(x+i) = tdtrue; else *(x+i) = tdfalse; else if (co >= *(x+i)) *(x+i) = tdtrue; else *(x+i) = tdfalse; break; case 3: for (i=0; i co) *(x+i) = tdtrue; else *(x+i) = tdfalse; else if (co > *(x+i)) *(x+i) = tdtrue; else *(x+i) = tdfalse; break; case 4: for (i=0; i= *(y+i)) *(x+i) = tdtrue; else *(x+i) = tdfalse; break; case 3: for (i=0; i *(y+i)) *(x+i) = tdtrue; else *(x+i) = tdfalse; break; case 4: for (i=0; i