/* @(#)associate.c 17.1.1.1 (ESO-DMD) 01/25/02 17:50:04 */ /*=========================================================================== 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 tbassoap.c .LANGUAGE C .AUTHOR IPG-ESO Garching .CATEGORY Data Organizer utilities .COMMENTS This module implements the following Midas commands: \begin{TeX} \begin{enumerate} \item {\tt ASSOCIATE/IMA} table columns exptype table_rule outtab \end{enumerate} \end{TeX} .VERSION 1.0 15-Mar-1993 Definition M. Peron 991130 -----------------------------------------------------------*/ #include #include #include #include #include #include #include #include #define issign(c) ((c == '+') || (c == '-')) #define ATTSIZE 49 #define BUFM 30 /* synchronize with tbcomsel !! */ #define COUNSIZE 8 #define RANGESIZE 16 #define COLS 10 #define STRINGLEN 256 double tdtrue,tdfalse; char *osmmget(); char *line,*token; int first,nochar,nline,nsci,associate,*rownumber; int refrow,token_type,action,exist,sort_off,nconst[BUFM]; int number_of_rows; int tmno[BUFM],tmnoc[BUFM]; double atof(); /* */ int associate_ima(intabdat,exptype,incol,intabrul,outtab,fc,imaname,ocolname, icount) char intabdat[60],intabrul[60],exptype[10],outtab[60],incol[80]; char ocolname[TBL_LABLEN+1],fc[2],imaname[60]; int icount; { int status,tid1,tid2,tid3,nrow,nrow2,natt,ncand,oldnrow; int null, len, lennew, dummy,clos,icomp, ibuf[7]; int col,coltype,colatt,colname,is; int colfunct,colran1,colran2,colweight; int outcol1,outcol2,outcol3,outrow,icol[COLS],flag[COLS],ival[COLS]; int inull[COLS],jval[COLS],nc,ec,el,ed,new,sel,nsel,flagone; int imarow,ifexist,outmast; static int one = 1; static int zero = 0; int i,j,k,l,start,mlen; char selrules[256], column[1+TBL_LABLEN]; int unit; char exposure[256],cval[8],qual[15]; char fname_1[61],fname_2[61],assoname[61]; char range_1[256],range_2[256],*func_att,ref_func_att[256]; char attcol[1+TBL_LABLEN]; char ws[4]; char *linesave,*linesecond; char mastname[60]; float tblsel; double *data[BUFM],*range1[BUFM],*range2[BUFM]; double *metric,rank[BUFM],quality; int *expoint; char *filename, *tempcrit; char *string[BUFM],*cdata[BUFM]; char myerr[81]; clos = 1; natt = 0; action = 1; for (i=0; i0 ) TCESRC(tid3, outcol1,fname_1,1,strlen(fname_1),1,&ifexist); if (ifexist <=0 ) { nsci = outrow; outrow++; TCEWRC(tid3,outrow,outcol1,fname_1); } else { outrow = ifexist-1; nsci = outrow; } flagone = 2; refrow = imarow; assoone(tid1,tid3,nrow,nrow2,colname,exptype,outcol2,outcol3,filename, outmast,refrow,outrow,icount,linesecond,range1, range2,rank,func_att,metric,data,cdata,string,flagone); strcpy(line,linesave); nline = strlen(line); } else if (stucmp(exposure,exptype) == 0) { associate = 1; number_of_rows = 1; rownumber = (int *)osmmget((icount + 1 ) * sizeof(int)); *rownumber = imarow; for (i=1; i=0; j--) { tempmast[j] = filename+60*j; strcat(tempmast[j],tempmast[j+1]); } sort_start = tempmast[0]; sort_off = icount * 60; Qsorta(tempmast,0,icount-1); for (l=0,perm = (int *)tempmast; l first) && (diff_m8(data[j], px) > 0)) j--; if (i <= j) { if (i < j) t = data[i], data[i] = data[j], data[j] = t; i++; j--; } } while (i <= j); if (first < j) Qsort8 (data, first, j); if (i < last) Qsort8 (data, i, last ); } int diff8 (s1, s2, order) /*++++++++++++++++ .PURPOSE Compute difference between two doubles .RETURNS s1-s2 (negative value if s1s2, null if s1=s2) .REMARKS NULL values always at end -----------------*/ double *s1, *s2; int order; { int i ; for (i = 1; --i >= 0; s1++, s2++){ if (*s1 == *s2) continue; if (*s1 > *s2) return (order); if (*s1 < *s2) return (-order); } return(0); } int diff_m8(s1,s2) char *s1,*s2; { int result,i; result = diff8(s1,s2,-1); if (result == 0) result = diff8(s1+sort_off,s2+sort_off,1); return(result); } /* */ int decrypt(column,rule,selecrit,tempcrit,range) char *rule,*selecrit,*column,*tempcrit; double *range; { char *delim = "!&"; int i,j,k,str; str = 0; if (!*rule) return(0); oscfill(tempcrit,256,'\0'); strcat(selecrit,column); if (!*rule) return(0); else { if (find_string(rule,"~*[=")) { /* strcat(selecrit,".EQ."); */ if (*rule == '=') rule++; } else if ((*rule == '!') && (*(rule+1) == '=')) { rule += 2; str = 1; strcat(selecrit,".NE."); } else if (*rule == '<') { str = 1; rule++; if (*rule == '=') { strcat(selecrit,".LE."); rule++; } else strcat(selecrit,".LT."); } else if (*rule == '>') { str = 1; rule++; if (*rule == '=') { strcat(selecrit,".GE."); rule++; } else strcat(selecrit,".GT."); } /* else strcat(selecrit,".EQ."); */ i = 0; while(!fidelim(rule)) { *(tempcrit+i) = *rule++; i++; } k = strindex(tempcrit,".."); if (tempcrit[k]) { strcat(selecrit,".GE."); strncat(selecrit,tempcrit,k); *range = atof(tempcrit); strcat(selecrit,".AND."); strcat(selecrit,column); tempcrit = tempcrit+k+2; *(range+1) = atof(tempcrit); strcat(selecrit,".LE."); strcat(selecrit,tempcrit); } else { if (!str) strcat(selecrit,".EQ."); if (stsnum(tempcrit) <= 0) { if (stumatch(tempcrit,"REFVAL(") != 7) strcat(selecrit,"\""); strcat(selecrit,tempcrit); if (stumatch(tempcrit,"REFVAL(") != 7) strcat(selecrit,"\""); } else { *range = atof(tempcrit); strcat(selecrit,tempcrit); } } if (*rule == '|') strcat(selecrit,".OR."); else if (*rule == '&') strcat(selecrit,".AND."); rule++; decrypt(column,rule,selecrit,tempcrit,range); } } int fidelim(s) char *s; { if (find_string(s,"|&") || *s==9 || *s=='\r' || *s==0) return 1; return 0; } /* */ int Qsorta(data, first, last) /*++++++++++++++++ .PURPOSE Quick Sort .METHOD Quick sort modified with insert sort for small partitions. The routine sorts the array of pointers to actual data. The comparison routine is external. .RETURNS --- -----------------*/ char **data; /* MOD: The arrays of pointers to sort */ int first, last; /* IN: Indexes of first / last pointer */ { char *px, *t; int i,j; i = first; j = last; px = data[(first+last)/2]; do { while ( (i < last ) && (diff_a(data[i], px) < 0)) i++; while ( (j > first) && (diff_a(data[j], px) > 0)) j--; if (i <= j) { if (i < j) t = data[i], data[i] = data[j], data[j] = t; i++; j--; } } while (i <= j); if (first < j) Qsorta (data, first, j); if (i < last) Qsorta (data, i, last ); } int diffa(s1,s2) /*++++++++++++++++ .PURPOSE Compute difference between two char strings .RETURNS s1-s2 (negative value if s1s2, null if s1=s2) .REMARKS NULL values always at end -----------------*/ char *s1, *s2; { int i; int sort_len,sort_order; sort_len = sort_off; sort_order = 1; for (i = sort_len; --i >= 0; s1++, s2++){ if (*s1 == *s2) continue; if (*s1 == NULL1) return(1); /* NULL value at end */ if (*s2 == NULL1) return(-1); /* NULL value at end */ if (*s1 > *s2) return (sort_order); if (*s1 < *s2) return (-sort_order); } return(0); } int diff_a(s1,s2) unsigned char *s1,*s2; { int sort_len,sort_order,i; sort_len = sort_off; sort_order = 1; for (i = sort_len; --i >= 0; s1++, s2++){ if (*s1 > *s2) return (sort_order); if (*s1 < *s2) return (-sort_order); } }