Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / routines / get_multi_series.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
6  *   TISEAN is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   TISEAN is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with TISEAN; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 /*Author: Rainer Hegger Last modified: Sep 3, 1999 */
21 /*Note: Keep in mind that the first index runs the dimension,
22   the second the time series index */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "tsa.h"
28 #include "tisean_cec.h"
29
30 #define SIZE_STEP 1000
31 extern void check_alloc(void*);
32
33 double **get_multi_series(char *name,unsigned long *l,unsigned long ex,
34                           unsigned int *col,char *which,char colfix,
35                           unsigned int verbosity)
36 {
37   char *input,**format;
38   int i,j;
39   unsigned int *hcol,maxcol=0,colcount=0;
40   unsigned long count,max_size=SIZE_STEP,hl,allcount;
41   int input_size=INPUT_SIZE;
42   double **x;
43   FILE *fin;
44
45   if (strlen(which) > 0) {
46     colcount=1;
47     for (i=0;i<strlen(which)-1;i++) {
48       if (!isdigit((unsigned int)which[i]) && (which[i] != ',')) {
49         fprintf(stderr,"Wrong format in the column string."
50                 " Has to be num,num,num,...,num\n");
51         exit(GET_MULTI_SERIES_WRONG_TYPE_OF_C);
52       }
53       if (which[i] == ',') {
54         colcount++;
55         which[i]=' ';
56       }
57     }
58     if (!isdigit((unsigned int)which[strlen(which)-1])) {
59         fprintf(stderr,"Wrong format in the column string."
60                 " Has to be num,num,num,...,num\n");
61         exit(GET_MULTI_SERIES_WRONG_TYPE_OF_C);
62     }      
63   }
64   if (!colfix && (*col < colcount))
65     *col=colcount;
66
67   check_alloc(input=(char*)calloc((size_t)input_size,(size_t)1));
68   check_alloc(hcol=(unsigned int*)malloc(sizeof(unsigned int)* *col));
69   while ((int)(*which) && isspace((unsigned int)(*which)))
70     which++;
71   if (*which)
72     for (i=0;i< *col-1;i++) {
73       sscanf(which,"%u",&hcol[i]);
74       if (hcol[i] > maxcol)
75         maxcol=hcol[i];
76       while ((int)(*which) && !isspace((unsigned int)(*which)))
77         which++;
78       while ((int)(*which) && isspace((unsigned int)(*which)))
79         which++;
80       if (!((int)(*which)))
81         break;
82     }
83   else
84     i= -1;
85   
86   if (*which)
87     sscanf(which,"%u",&hcol[i]);
88   else
89     for (j=i+1;j< *col;j++)
90       hcol[j]= ++maxcol;
91   
92   if (verbosity&VER_INPUT) {
93     fprintf(stderr,"Using columns: ");
94     for (i=0;i< *col;i++)
95       fprintf(stderr,"%d ",hcol[i]);
96     fprintf(stderr,"\n");
97   }
98
99   check_alloc(format=(char**)malloc(sizeof(char*)* *col));
100   for (i=0;i< *col;i++) {
101     check_alloc(format[i]=(char*)calloc((size_t)(4*hcol[i]),(size_t)1));
102     strcpy(format[i],"");
103     for (j=1;j<hcol[i];j++)
104       strcat(format[i],"%*lf");
105     strcat(format[i],"%lf");
106   }
107   free(hcol);
108   
109   check_alloc(x=(double**)malloc(sizeof(double*)* *col));
110   for (i=0;i< *col;i++)
111     check_alloc(x[i]=(double*)malloc(sizeof(double)*max_size));
112   hl= *l;
113
114   count=0;
115   allcount=0;
116   if (name == NULL) {
117     for (i=0;i<ex;i++)
118       if ((input=myfgets(input,&input_size,stdin,verbosity)) == NULL)
119         break;
120     while ((count < hl) && 
121            ((input=myfgets(input,&input_size,stdin,verbosity)) != NULL)) {
122       if (count == max_size) {
123         max_size += SIZE_STEP;
124         for (i=0;i< *col;i++)
125           check_alloc(x[i]=(double*)realloc(x[i],sizeof(double)*max_size));
126       }
127       allcount++;
128       for (i=0;i< *col;i++)
129         if (sscanf(input,format[i],&x[i][count]) != 1) {
130           if (verbosity&VER_INPUT)
131             fprintf(stderr,"Line %lu ignored: %s",allcount,input);
132           break;
133         }
134       if (i == *col)
135         count++;
136     }
137   }
138   else {
139     fin=fopen(name,"r");
140     for (i=0;i<ex;i++)
141       if ((input=myfgets(input,&input_size,fin,verbosity)) == NULL)
142         break;
143     while ((count < hl) && 
144            ((input=myfgets(input,&input_size,fin,verbosity)) != NULL)) {
145       if (count == max_size) {
146         max_size += SIZE_STEP;
147         for (i=0;i< *col;i++)
148           check_alloc(x[i]=(double*)realloc(x[i],sizeof(double)*max_size));
149       }
150       allcount++;
151       for (i=0;i< *col;i++)
152         if (sscanf(input,format[i],&x[i][count]) != 1) {
153           if (verbosity&VER_INPUT)
154             fprintf(stderr,"Line %lu ignored: %s",allcount,input);
155           break;
156         }
157       if ((count == 0) && (i == *col) && (verbosity&VER_FIRST_LINE)) {
158         fprintf(stderr,"get_multi_series: first data item(s) used:\n");
159         for (i=0;i< *col;i++)
160           fprintf(stderr,"%lf ",x[i][0]);
161         fprintf(stderr,"\n");
162       }
163       if (i == *col)
164         count++;
165     }
166     fclose(fin);
167   }
168   
169   for (i=0;i< *col;i++)
170     free(format[i]);
171   free(format);
172   free(input);
173
174   *l = count;  
175   if (*l == 0) {
176     fprintf(stderr,"0 lines read. It makes no sense to continue. Exiting!\n");
177     exit(GET_MULTI_SERIES_NO_LINES);
178   }
179   else {
180     if (verbosity&VER_INPUT)
181       fprintf(stderr,"Use %lu lines.\n",*l);
182   }
183
184   if (max_size > count)
185     for (i=0;i< *col;i++) 
186       check_alloc(x[i]=(double*)realloc(x[i],sizeof(double)*count));
187   
188   return x;
189 }
190 #undef SIZE_STEP