~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to stdred/do/libsrc/classify.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
  Copyright (C) 1993-2009 European Southern Observatory (ESO)
 
3
 
 
4
  This program is free software; you can redistribute it and/or
 
5
  modify it under the terms of the GNU General Public License as
 
6
  published by the Free Software Foundation; either version 2 of
 
7
  the License, or (at your option) any later version.
 
8
 
 
9
  This program is distributed in the hope that it will be useful,
 
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
  GNU General Public License for more details.
 
13
 
 
14
  You should have received a copy of the GNU General Public
 
15
  License along with this program; if not, write to the Free
 
16
  Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
 
17
  MA 02139, USA.
 
18
 
 
19
  Correspondence concerning ESO-MIDAS should be addressed as follows:
 
20
        Internet e-mail: midas@eso.org
 
21
        Postal address: European Southern Observatory
 
22
                        Data Management Division
 
23
                        Karl-Schwarzschild-Strasse 2
 
24
                        D 85748 Garching bei Muenchen
 
25
                        GERMANY
 
26
===========================================================================*/
 
27
 
 
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
29
.TYPE        Module
 
30
.NAME        tbapplyrul.c
 
31
.LANGUAGE    C
 
32
.AUTHOR      IPG-ESO Garching
 
33
.CATEGORY    Data Organizer utilities
 
34
.COMMENTS       This module implements the following Midas commands:
 
35
\begin{TeX}
 
36
\begin{enumerate}
 
37
\item {\tt CLASSIFY/IMAGE} table rule column outchar
 
38
\end{enumerate}
 
39
\end{TeX}
 
40
 
 
41
.VERSION  1.0   15-Mar-1993   Definition     M. Peron
 
42
 
 
43
 090831         last modif
 
44
-----------------------------------------------------------------------*/
 
45
 
 
46
#include <atype.h>
 
47
#include <tbldef.h>
 
48
#include <tblsys.h>
 
49
#include <tblerr.h>
 
50
#include <midas_def.h>
 
51
#include <macrogen.h>
 
52
 
 
53
#include <stdlib.h>
 
54
#include <str.h>
 
55
 
 
56
 
 
57
#define issign(c)  ((c == '+') || (c == '-'))
 
58
#define BUFM 30                         /* synchronize with tbcomsel !!  */
 
59
#define MAXS 30
 
60
#define PARLEN 256
 
61
#define MAXLEN 80
 
62
 
 
63
extern int get_token();
 
64
extern int level00();
 
65
extern int tbl_hist();
 
66
 
 
67
void deco();
 
68
 
 
69
 
 
70
char *token,*line;
 
71
double tdtrue,tdfalse;
 
72
int token_type,first,nochar,refrow,action,exist;
 
73
int tmno[BUFM],tmnoc[BUFM];
 
74
char *stringstar[MAXS];
 
75
int occ[PARLEN],ocp[PARLEN],principal;
 
76
 
 
77
/*
 
78
 
 
79
*/
 
80
 
 
81
void classify(intable,assod,column,mycval)
 
82
char intable[60],column[1+TBL_LABLEN],assod[256],*mycval;
 
83
{
 
84
int status,tid,nrow;
 
85
int null, dummy, type;
 
86
int col,cstar[PARLEN],nstar,nstring,pnull[PARLEN];
 
87
int i,j,*index, lola,k,pos,pos1,pos2,ntimes,itimes,tidass,nline,ntoken ;
 
88
int coldescr,colout,colchar,meth,selass,mylen;
 
89
int ibuf[BUFM],nconst[BUFM], unit;
 
90
int kk;
 
91
char colstar[1+TBL_LABLEN];
 
92
char *selecrit;
 
93
char *final,*stringtok[MAXS];
 
94
char descr[256], *cval;
 
95
double *data[BUFM],consta[BUFM];
 
96
char *string[BUFM],*cdata[BUFM];
 
97
float tblsel;
 
98
 
 
99
 
 
100
action = 1;
 
101
exist = 0;
 
102
cval = osmmget(MAXLEN+1);
 
103
line = osmmget(PARLEN);
 
104
token = osmmget(PARLEN);
 
105
TCMCON(&tblsel,&tdtrue,&tdfalse);
 
106
for (i=0; i<MAXS; i++) {
 
107
    stringstar[i] = (char *)0;
 
108
    stringtok[i] = (char *)0;
 
109
    }
 
110
final = (char *)0;
 
111
selecrit = osmmget(PARLEN+1);
 
112
for (i=0 ; i<BUFM; i++) {
 
113
      data[i] = (double *)0;
 
114
      cdata[i] = (char *)0;
 
115
      string[i] = (char *)0;
 
116
      tmno[i] = -1;
 
117
      tmnoc[i] = -1;
 
118
      }
 
119
if (assod[strindex(assod,".tbl")]) {
 
120
     meth = 1;
 
121
     TCTOPN(assod,F_I_MODE,&tidass);
 
122
     TCIGET(tidass,&dummy,&ntimes,&dummy,&dummy,&dummy);
 
123
     TCLSER(tidass,"DESCR",&coldescr);
 
124
     if (coldescr <= 0 ) {
 
125
        SCTPUT("Column DESCR not found");
 
126
        SCSEPI();
 
127
        }
 
128
     TCLSER(tidass,"OUTCOL",&colout);
 
129
     if (colout <= 0 ) {
 
130
        SCTPUT("Column OUTCOL not found");
 
131
        SCSEPI();
 
132
        }
 
133
     TCLSER(tidass,"OUTCHAR",&colchar);
 
134
     if (colchar <= 0 ) {
 
135
        SCTPUT("Column OUTCHAR not found");
 
136
        SCSEPI();
 
137
        }
 
138
 
139
else {
 
140
   ntimes = 1;
 
141
   meth = 0;
 
142
   }
 
143
tid = TCTID(intable);
 
144
if (tid == -1) 
 
145
status = TCTOPN(intable,F_IO_MODE,&tid);
 
146
status = TCIGET(tid,&dummy,&nrow,&dummy,&dummy,&dummy);
 
147
for (itimes=1; itimes<=ntimes; itimes++) {
 
148
   if (!assod[strindex(assod,".tbl")]) {
 
149
      strcpy(cval,mycval);
 
150
      strcpy(descr,assod);
 
151
      }
 
152
   else {
 
153
      TCSGET(tidass,itimes,&selass);
 
154
      if (!selass) continue;
 
155
      TCERDC(tidass,itimes,coldescr,descr,&null);
 
156
      TCERDC(tidass,itimes,colout,column,&null);
 
157
      TCERDC(tidass,itimes,colchar,cval,&null);
 
158
   }
 
159
status = TCCSER(tid,column,&col);
 
160
if (col == -1) TCCINI(tid,D_C_FORMAT,32L,"A16"," ",column,&col);
 
161
 
 
162
   for (i=0; i<PARLEN; i++) selecrit[i] = '\0';
 
163
   if (assod[strindex(assod,":")])  strcpy(selecrit,descr);
 
164
   else status = SCDRDC(tid,descr,1,1,255,&dummy,selecrit,&unit,&null);
 
165
   ibuf[0] = tid;
 
166
   ibuf[2] = nrow;
 
167
   ibuf[3] = -1;
 
168
   ibuf[4] = -1;
 
169
   ibuf[5] = -1;
 
170
   ibuf[6] = -1;
 
171
   strcpy(line,selecrit);
 
172
   nline = strlen(line);
 
173
   get_token();
 
174
   level00(ibuf,data,cdata,consta,nconst,string);
 
175
   line = line - nline; 
 
176
   for (i=0; i<PARLEN; i++) *(line+i) = '\0';
 
177
   for (i=0; i<PARLEN; i++) ocp[i] = 0; 
 
178
   strncpy(line,selecrit,256L);
 
179
   type = 0;
 
180
   index = &lola;
 
181
   i = *index = 0;
 
182
   mylen = strlen(cval);
 
183
   while (cval[0] != '\0') {
 
184
   pos1 = strscans(cval,"[&");
 
185
   if (!cval[pos1]) break;
 
186
   else  type = 1;
 
187
         pos2 = strloc(cval,'[');
 
188
         if (!cval[pos2]) {
 
189
             pos1 = strloc(cval,'&');
 
190
             if (cval[pos1]) {
 
191
                if (!stringstar[*index]) stringstar[*index] = osmmget(MAXLEN);
 
192
                oscfill(stringstar[*index],MAXLEN,'\0');
 
193
                strncpy(stringstar[*index],cval,pos1);
 
194
                while (pos1--) cval++;
 
195
                cval++;
 
196
                occ[*index] = atoi(cval);
 
197
                *index +=1;
 
198
                while (isdigit(*cval)) cval++;
 
199
                }
 
200
             else {
 
201
                if (!stringstar[*index]) stringstar[*index] = osmmget(MAXLEN);
 
202
                oscfill(stringstar[*index],MAXLEN,'\0');
 
203
                strncpy(stringstar[*index],cval,pos1);
 
204
                occ[*index]=0;
 
205
                *index +=1;
 
206
                cval+=pos1; 
 
207
                }
 
208
             }
 
209
         else {
 
210
              deco(index,1,&cval);
 
211
              }
 
212
 
 
213
 }
 
214
if (type ==1 )cval -= mylen;
 
215
nstring = *index;
 
216
 
 
217
  if (type == 1) {
 
218
  nstar = 0;
 
219
  while (*line) {
 
220
   get_token();
 
221
   if (token_type == 2) strcpy(colstar,token);
 
222
   if (token_type ==6 ) {
 
223
         ntoken = strlen(token);
 
224
         pos = strloc(token,'*'); 
 
225
         if (token[pos]) {
 
226
              if (!stringtok[nstar]) stringtok[nstar] = osmmget(MAXLEN);
 
227
              oscfill(stringtok[nstar],MAXLEN,'\0');
 
228
              strncpy(stringtok[nstar],token,MAXLEN);
 
229
              TCCSER(tid,colstar,&cstar[nstar]);
 
230
              nstar++;
 
231
              token = token + pos +1;
 
232
              while(token[strloc(token,'*')]) {
 
233
                 pos = strloc(token,'*'); 
 
234
                 *(stringtok[nstar-1] + pos + 1 ) = '\0';
 
235
                 if (!stringtok[nstar]) stringtok[nstar] = osmmget(MAXLEN);
 
236
                 oscfill(stringtok[nstar],MAXLEN,'\0');
 
237
                 strncpy(stringtok[nstar],token,MAXLEN);
 
238
                 TCCSER(tid,colstar,&cstar[nstar]);
 
239
                 nstar++;
 
240
                 token = token + pos +1;
 
241
                 }
 
242
               }
 
243
         token = token - ntoken;
 
244
         }
 
245
 }
 
246
}
 
247
   if (type == 0) {
 
248
    for (i=0; i<nrow; i++) {
 
249
       if (*(data[0] + i)) status = TCEWRC(tid,i+1,col,cval);
 
250
       } 
 
251
}
 
252
   else {
 
253
    if (!final) final = osmmget(PARLEN);
 
254
    for (i=0; i<PARLEN; i++) {
 
255
       final[i] = '\0';
 
256
  }
 
257
 
 
258
   for (i=0; i<nrow; i++) {
 
259
       if (*(data[0]+i)) {
 
260
          for (j=0; j<PARLEN; j++) final[j] = '\0';
 
261
          for (j=0; j<nstring;  j++) { 
 
262
             if (occ[j] == 0) strcat(final,stringstar[j]);
 
263
             else if (occ[j] > 0) {
 
264
                status = TCERDC(tid,i+1,cstar[occ[j]-1],cval,&pnull[j]);
 
265
                if (pnull[j] == 0) {
 
266
                 if (ocp[j] && final[0] != 0) strcat(final,"+");
 
267
                 if (*stringtok[occ[j]-1] == '*') {
 
268
                   strcat(final,stringstar[j]);
 
269
                   kk = strskip(cval,' ');
 
270
                   k = strbskip(cval+kk,' ');
 
271
                   if (cval[k+kk+1]) cval[k+kk+1] = '\0';
 
272
                   k = strlen(cval)-strlen(stringtok[occ[j]-1])+1;
 
273
                   cval[k] = '\0';
 
274
                   strcat(final,cval+strskip(cval,' '));
 
275
                   }
 
276
                 else {
 
277
                 strcat(final,stringstar[j]);
 
278
                   kk = strskip(cval,' ');
 
279
                   k = strloc(cval+kk,' ');
 
280
                   if (cval[k+kk]) cval[k+kk] = '\0';
 
281
                 strcat(final,cval+strlen(stringtok[occ[j]-1])-1+strskip(cval,' '));
 
282
                 }
 
283
                }
 
284
              }
 
285
               else 
 
286
                   if (pnull[-occ[j]-1] == 0) strcat(final,stringstar[j]);
 
287
             }   
 
288
          TCEWRC(tid,i+1,col,final);
 
289
          }
 
290
      } 
 
291
 
292
/*for (i=0; i<BUFM; i++) if (data[i]) osmmfree((char *)data[i]);
 
293
for (i=0; i<BUFM; i++) if (cdata[i]) osmmfree(cdata[i]);*/
 
294
 
 
295
for (i=0; i<BUFM;i++) {
 
296
   if (tmno[i] != -1) SCFCLO(tmno[i]);
 
297
   if (tmnoc[i] != -1 ) SCFCLO(tmnoc[i]);
 
298
   }
 
299
for (i=0; i<BUFM; i++) if (string[i]) osmmfree(string[i]);
 
300
for (i=0; i<BUFM; i++) {
 
301
      data[i] = (double *)0;
 
302
      cdata[i] = (char *)0;
 
303
      string[i] = (char *)0;
 
304
}
 
305
ibuf[3] = -1;
 
306
ibuf[4] = -1;
 
307
ibuf[5] = -1;
 
308
ibuf[6] = -1;
 
309
if (type == 1) line = line-nline;
 
310
}
 
311
osmmfree(line);
 
312
osmmfree(token);
 
313
osmmfree(selecrit);
 
314
osmmfree(cval);
 
315
if(final) osmmfree(final);
 
316
status = tbl_hist(tid);
 
317
if (meth==1) TCTCLO(tidass);
 
318
status = TCTCLO(tid);
 
319
}
 
320
 
 
321
/*
 
322
 
 
323
*/
 
324
 
 
325
void  deco(index,flag,decval)
 
326
int *index,flag;
 
327
char **decval;
 
328
{
 
329
char *mydecval;
 
330
int pos1,pos2;
 
331
mydecval = *decval;
 
332
pos1 = strloc(mydecval,'[');
 
333
if (pos1 != 0) {
 
334
   if (!stringstar[*index]) stringstar[*index] = osmmget(MAXLEN);
 
335
   oscfill(stringstar[*index],MAXLEN,'\0');
 
336
   strncpy(stringstar[*index],mydecval,pos1);
 
337
   while (pos1--) mydecval++;
 
338
   occ[*index] = 0;
 
339
   *index += 1;
 
340
   }
 
341
mydecval++;
 
342
pos1 = strloc(mydecval,'[');
 
343
pos2 = strloc(mydecval,']');
 
344
if (pos1 > pos2) {
 
345
   pos1 = strloc(mydecval,'&');
 
346
   if (!stringstar[*index]) stringstar[*index] = osmmget(MAXLEN);
 
347
   oscfill(stringstar[*index],MAXLEN,'\0');
 
348
   strncpy(stringstar[*index],mydecval,pos1);
 
349
   while (pos1--) mydecval++;
 
350
   mydecval++;
 
351
   occ[*index] = atoi(mydecval);
 
352
   if (flag == 1)  ocp[*index] = 1;
 
353
   *index += 1;
 
354
   while(isdigit(*mydecval)) mydecval++;
 
355
   if (*mydecval == ']') mydecval++;
 
356
   *decval = mydecval;
 
357
   }
 
358
else {
 
359
   pos2 = strloc(mydecval,'[');
 
360
   while (pos2) {
 
361
          pos1 = strloc(mydecval,'&');
 
362
          if (mydecval[pos1] && (pos1 < pos2)) {
 
363
            if (!stringstar[*index]) stringstar[*index] = osmmget(MAXLEN);
 
364
            oscfill(stringstar[*index],MAXLEN,'\0');
 
365
            strncpy(stringstar[*index],mydecval,pos1);
 
366
            while (pos1--) {mydecval++;pos2--;} 
 
367
            mydecval++; pos2--;
 
368
            occ[*index] = atoi(mydecval);
 
369
            if (flag == 1) ocp[*index] = 1;
 
370
            principal = *index;
 
371
            *index += 1;
 
372
            while (isdigit(*mydecval)) {mydecval++; pos2--;}
 
373
            }
 
374
          else {
 
375
            if (!stringstar[*index]) stringstar[*index] = osmmget(MAXLEN);
 
376
            oscfill(stringstar[*index],MAXLEN,'\0');
 
377
            strncpy(stringstar[*index],mydecval,pos2);
 
378
            mydecval += pos2;
 
379
            pos2 = 0;
 
380
            occ[*index] = -principal -1 ;
 
381
          /*  ocp[*index] = 1; */
 
382
            *index += 1;
 
383
            }
 
384
          }
 
385
*decval = mydecval;
 
386
deco(index,0,decval);
 
387
mydecval = *decval;
 
388
pos1 = strloc(mydecval,']');
 
389
if (pos1 != 0) {
 
390
   if (!stringstar[*index]) stringstar[*index] = osmmget(MAXLEN);
 
391
   oscfill(stringstar[*index],MAXLEN,'\0');
 
392
   strncpy(stringstar[*index],mydecval,pos1);
 
393
   while (pos1--) mydecval++;
 
394
   occ[*index] = -1- principal;
 
395
   *index += 1;
 
396
   mydecval++;
 
397
   if (*mydecval == ']') mydecval++;
 
398
    }
 
399
else mydecval++;
 
400
*decval = mydecval;
 
401
}
 
402
}