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

« back to all changes in this revision

Viewing changes to stdred/do/src/tbgroup.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 Massachusetss Ave, Cambridge, 
 
17
  MA 02139, USA.
 
18
 
 
19
  Corresponding 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        tbread.c
 
31
.LANGUAGE    C
 
32
.AUTHOR      IPG-ESO Garching
 
33
.CATEGORY    table utilities
 
34
.COMMENTS       This module implements the following Midas commands:
 
35
\begin{TeX}
 
36
\begin{enumerate}
 
37
\item {\tt GROUP/ROW} table incol outcol flag
 
38
\end{enumerate}
 
39
\end{TeX}
 
40
 
 
41
.VERSION  1.0   15-Mar-1993   Definition     M. Peron
 
42
 
 
43
 090831         last modif
 
44
----------------------------------------------------------*/
 
45
#include <atype.h>
 
46
#include <tbldef.h>
 
47
#include <tblsys.h>
 
48
#include <tblerr.h>
 
49
#include <midas_def.h>
 
50
#include <macrogen.h>
 
51
#include <proto_tbl.h>
 
52
 
 
53
 
 
54
#define issign(c)               ((c == '+') || (c == '-'))
 
55
#define PARLEN 80
 
56
 
 
57
 
 
58
extern int  tbl_hist();
 
59
 
 
60
void readtreei();
 
61
void readtreec();
 
62
 
 
63
 
 
64
 
 
65
struct treec {
 
66
     char name[256];    
 
67
     int *seqnum;
 
68
     int noelem;
 
69
     struct treec *left;
 
70
     struct treec *right;
 
71
};
 
72
struct treec *headc;
 
73
struct treei {
 
74
     int ival;    
 
75
     int *seqnum;
 
76
     int noelem;
 
77
     struct treei *left;
 
78
     struct treei *right;
 
79
};
 
80
struct treei *headi;
 
81
int tid,gnum,icol,ocol,ncol,incol[256];
 
82
int len;
 
83
 
 
84
 
 
85
 
 
86
int main()
 
87
{
 
88
int status,nrow;
 
89
int null, dummy;
 
90
int sel,dtype,ival;
 
91
int i,j;
 
92
char icolumn[PARLEN],ocolumn[PARLEN];
 
93
char form[1+TBL_FORLEN];
 
94
char intab[60], action[2];
 
95
char cval[256];
 
96
double dval;
 
97
struct treec *s1dtreec();
 
98
struct treei *s1dtreei();
 
99
 
 
100
 
 
101
SCSPRO("tbgroup");
 
102
SCKGETC("IN_A",1L,60L,&dummy,intab);
 
103
SCKGETC("INPUTC",1L,80L,&dummy,icolumn);
 
104
SCKGETC("OUTPUTC",1L,80L,&dummy,ocolumn);
 
105
SCKGETC("ACTION",1L,1L,&dummy,action);
 
106
status = TCTOPN(intab,F_IO_MODE,&tid);
 
107
if (status != ERR_NORMAL) {
 
108
      SCTPUT("Error opening input data table");
 
109
      SCSEPI();
 
110
}
 
111
TCIGET(tid,&ncol,&nrow,&dummy,&dummy,&dummy);
 
112
TCCSER(tid,icolumn,&icol);
 
113
if (icol <= 0) {
 
114
       SCTPUT("Input column not found");
 
115
       SCSEPI();  
 
116
     }
 
117
TCFGET(tid,icol,form,&len,&dtype);
 
118
TCCSER(tid,ocolumn,&ocol);
 
119
if (ocol <= 0) TCCINI(tid,D_I4_FORMAT,1,"I3"," ",ocolumn,&ocol);
 
120
gnum = 0;
 
121
if (dtype == D_C_FORMAT) {
 
122
 for (i=1; i<=nrow; i++) {
 
123
    TCSGET(tid,i,&sel);
 
124
    if (!sel) continue;
 
125
    TCERDC(tid,i,icol,cval,&null);
 
126
    if (null) continue;
 
127
    if (!headc) headc = s1dtreec(headc,headc,cval,i,nrow);
 
128
    else s1dtreec(headc,headc,cval,i,nrow,gnum);
 
129
    }
 
130
incol[0] = icol;
 
131
j=1;
 
132
 for (i=1; i<=ncol; i++) if (i != icol) {
 
133
        incol[j] = i;
 
134
        j++;
 
135
        } 
 
136
if (toupper(*action) == 'R')  readtreec(headc);
 
137
 }
 
138
else {
 
139
 for (i=1; i<=nrow; i++) {
 
140
    TCSGET(tid,i,&sel);
 
141
    if (!sel) continue;
 
142
    TCERDD(tid,i,icol,&dval,&null);
 
143
    ival = (int ) dval;
 
144
    if (null) continue;
 
145
    if (!headi) headi = s1dtreei(headi,headi,ival,i,nrow);
 
146
    else s1dtreei(headi,headi,ival,i,nrow,gnum);
 
147
    }
 
148
incol[0] = icol;
 
149
j=1;
 
150
 for (i=1; i<=ncol; i++) if (i != icol) {
 
151
        incol[j] = i;
 
152
        j++;
 
153
        } 
 
154
 
 
155
if (toupper(*action) == 'R') readtreei(headi);
 
156
 }
 
157
 
 
158
status = tbl_hist(tid);
 
159
SCSEPI();
 
160
return 0;
 
161
}
 
162
 
 
163
struct treec *s1dtreec(root,r,cval,row,nrow)
 
164
struct treec *root;
 
165
struct treec *r;
 
166
char cval[256];
 
167
int row,nrow;
 
168
{
 
169
if (!r) {
 
170
        r = (struct treec *)osmmget(sizeof(struct treec));
 
171
        r->left = (struct treec *)0;
 
172
        r->right = (struct treec *)0;
 
173
        strcpy(r->name,cval);
 
174
        r->seqnum = (int *) osmmget(sizeof(int)*nrow);
 
175
        gnum++;
 
176
        r->noelem = 1;
 
177
        r->seqnum[0] = gnum;
 
178
        r->seqnum[r->noelem] = row;
 
179
        if (!root) {
 
180
             TCEWRI(tid,row,ocol,r->seqnum);
 
181
             return r;
 
182
             }
 
183
        if (strcmp(cval,root->name) < 0) {
 
184
                    root->left = r;
 
185
                    TCEWRI(tid,row,ocol,r->seqnum);
 
186
                    }
 
187
        else if (strcmp(cval,root->name) > 0) {
 
188
                    root->right = r;
 
189
                    TCEWRI(tid,row,ocol,r->seqnum);
 
190
                    }
 
191
        else {
 
192
             root->noelem++ ;
 
193
             root->seqnum[root->noelem] = row;
 
194
             TCEWRI(tid,row,ocol,r->seqnum);
 
195
        }
 
196
        return r;
 
197
}
 
198
if  (strcmp(cval,r->name) < 0) s1dtreec(r,r->left,cval,row,nrow);
 
199
else if (strcmp(cval,r->name) > 0) s1dtreec(r,r->right,cval,row,nrow);
 
200
else {
 
201
     r->noelem++;
 
202
     r->seqnum[r->noelem] = row;
 
203
     TCEWRI(tid,row,ocol,r->seqnum);
 
204
     }
 
205
return r;
 
206
}        
 
207
 
 
208
void readtreec(head)
 
209
struct treec *head;
 
210
{
 
211
int i;
 
212
char line[256];
 
213
int nulls[256];
 
214
if (head->left) readtreec(head->left);
 
215
TCRRDC(tid,head->seqnum[1],ncol,incol,line,nulls);
 
216
SCTPUT(line);
 
217
oscfill(line,len+1,' ');
 
218
for (i=2; i<= head->noelem;i++){
 
219
        TCRRDC(tid,head->seqnum[i],ncol-1,&incol[1],line+len+1,nulls);
 
220
        SCTPUT(line);
 
221
        }
 
222
if (head->right) readtreec(head->right);
 
223
}
 
224
 
 
225
 
 
226
 
 
227
struct treei *s1dtreei(root,r,ival,row,nrow)
 
228
struct treei *root;
 
229
struct treei *r;
 
230
int ival;
 
231
int row,nrow;
 
232
{
 
233
if (!r) {
 
234
        r = (struct treei *)osmmget(sizeof(struct treei));
 
235
        r->left = (struct treei *)0;
 
236
        r->right = (struct treei *)0;
 
237
        r->ival = ival;
 
238
        r->seqnum = (int *) osmmget(sizeof(int)*nrow);
 
239
        gnum++;
 
240
        r->noelem = 1;
 
241
        r->seqnum[0] = gnum;
 
242
        r->seqnum[r->noelem] = row;
 
243
        if (!root) {
 
244
             TCEWRI(tid,row,ocol,r->seqnum);
 
245
             return r;
 
246
             }
 
247
        if (ival < root->ival ) {
 
248
                    root->left = r;
 
249
                    TCEWRI(tid,row,ocol,r->seqnum);
 
250
                    }
 
251
        else if (ival > root->ival) {
 
252
                    root->right = r;
 
253
                    TCEWRI(tid,row,ocol,r->seqnum);
 
254
                    }
 
255
        else {
 
256
             root->noelem++ ;
 
257
             root->seqnum[root->noelem] = row;
 
258
             TCEWRI(tid,row,ocol,r->seqnum);
 
259
        }
 
260
        return r;
 
261
}
 
262
if  (ival < r->ival) s1dtreei(r,r->left,ival,row,nrow);
 
263
else if (ival > r->ival) s1dtreei(r,r->right,ival,row,nrow);
 
264
else {
 
265
     r->noelem++;
 
266
     r->seqnum[r->noelem] = row;
 
267
     TCEWRI(tid,row,ocol,r->seqnum);
 
268
     }
 
269
return r;
 
270
}        
 
271
 
 
272
 
 
273
 
 
274
void readtreei(head)
 
275
struct treei *head;
 
276
{
 
277
int i;
 
278
char line[256];
 
279
int nulls[256];
 
280
if (head->left) readtreei(head->left);
 
281
TCRRDC(tid,head->seqnum[1],ncol,incol,line,nulls);
 
282
SCTPUT(line);
 
283
oscfill(line,len+1,' ');
 
284
for (i=2; i<= head->noelem;i++){
 
285
        TCRRDC(tid,head->seqnum[i],ncol-1,&incol[1],line+len+1,nulls);
 
286
        SCTPUT(line);
 
287
        }
 
288
if (head->right) readtreei(head->right);
 
289
}