00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <time.h>
00026 #include <ctype.h>
00027 #include <math.h>
00028 #include <errno.h>
00029 #include <unistd.h>
00030 #include <string.h>
00031 #include <sys/types.h>
00032 #include <sys/stat.h>
00033 #include <fcntl.h>
00034 #include <dirent.h>
00035 #include <assert.h>
00036 #include <signal.h>
00037 #include <string.h>
00038 #include <search.h>
00039
00040 #include "common.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #define MAXSTA 1024
00061 int nsta=0;
00062 char station[MAXSTA][16];
00063 unsigned long long trigger[MAXSTA];
00064 int windowLength=0;
00065 int ignoreLength=0;
00066 unsigned long long minsta=0;
00067 time_t CT;
00068 int timeDevider=1;
00069
00070 int ParseMergeTriggerOptions(char *cmd)
00071 {
00072 char *s=cmd;
00073 int next,error;
00074 if (*s==0)
00075 {
00076 return(0);
00077 }
00078 if (*s!=',')
00079 {
00080 return(-1);
00081 }
00082 while (*s)
00083 {
00084 s++;
00085 next=0;
00086 error=1;
00087 if (strncmp(s,"i=",2)==0)
00088 {
00089 if (sscanf(s+2,"%d%n",&ignoreLength,&next)!=1)
00090 {
00091 break;
00092 }
00093
00094 if (ignoreLength<1)
00095 {
00096 PrintError("ignore length must be positive\n");
00097 return(-1);
00098 }
00099 while (ignoreLength/timeDevider>63)
00100 {
00101 timeDevider++;
00102 }
00103 PrintDebug("ignoreLength=%d\n",ignoreLength);
00104 error=0;
00105 next+=2;
00106 }
00107 if (strncmp(s,"w=",2)==0)
00108 {
00109 if (sscanf(s+2,"%d%n",&windowLength,&next)!=1)
00110 {
00111 break;
00112 }
00113
00114 if (windowLength<1)
00115 {
00116 PrintError("window length must be positive\n");
00117 return(-1);
00118 }
00119 while (windowLength/timeDevider>63)
00120 {
00121 timeDevider++;
00122 }
00123 PrintDebug("windowLength=%d\n",windowLength);
00124 error=0;
00125 next+=2;
00126 }
00127 if (strncmp(s,"m=",2)==0)
00128 {
00129 if (sscanf(s+2,"%Lu%n",&minsta,&next)!=1)
00130 {
00131 break;
00132 }
00133
00134 if (minsta<1)
00135 {
00136 PrintError("minsta must be positive\n");
00137 return(-1);
00138 }
00139 PrintDebug("minsta=%d\n",minsta);
00140 error=0;
00141 next+=2;
00142 }
00143 if (error)
00144 {
00145 PrintError("could not parse trigger option string\n");
00146 return(-1);
00147 }
00148 s+=next;
00149 }
00150 if ((minsta==0)||(windowLength==0))
00151 {
00152 return(-1);
00153 }
00154 if (ignoreLength<windowLength)
00155 {
00156 ignoreLength=windowLength;
00157 }
00158 return(0);
00159 }
00160
00161
00162
00163
00164
00165
00166 int StationIndex(char *sta)
00167 {
00168 int i;
00169
00170 if (sta[0]=='-')
00171 {
00172 return(MAXSTA-1);
00173 }
00174 for (i=0; i<nsta; i++)
00175 {
00176 if (0==strcmp(sta,station[i]))
00177 {
00178 return(i);
00179 }
00180 }
00181 assert(i<MAXSTA-1);
00182 strcpy(station[i],sta);
00183 nsta++;
00184 return(i);
00185 }
00186
00187 int MergeTriggers(char *cmd)
00188 {
00189 char sta[16];
00190 time_t T;
00191 int i;
00192 int count;
00193 char key[16];
00194 unsigned long long mask,imask;
00195 char line[1024];
00196 int shift;
00197 if (ParseMergeTriggerOptions(cmd)<0)
00198 {
00199 return(-1);
00200 }
00201
00202 memset(trigger,0,sizeof(unsigned long long)*MAXSTA);
00203
00204 mask=(1LL<<(windowLength/timeDevider))-1;
00205 imask=(1LL<<(ignoreLength/timeDevider))-1;
00206
00207 while (!feof(stdin))
00208 {
00209
00210 if (NULL==fgets(line,1023,stdin))
00211 {
00212 break;
00213 }
00214
00215 if (line[0]=='#')
00216 {
00217 continue;
00218 }
00219 if (sscanf(line,"%s %s %ld",sta,key,&T)!=3)
00220 {
00221 continue;
00222 }
00223 sta[15]=0;
00224 key[15]=0;
00225
00226 if (0!=strcmp(key,"stalta"))
00227 {
00228 continue;
00229 }
00230 shift=(T-CT)/timeDevider;
00231
00232 if (shift>63)
00233 {
00234
00235 memset(trigger,0,sizeof(unsigned long long)*MAXSTA);
00236 }
00237 else
00238 {
00239 if (shift>0)
00240 {
00241
00242 for (i=0; i<nsta; i++)
00243 {
00244 trigger[i]<<=shift;
00245 }
00246 trigger[MAXSTA-1]<<=shift;
00247 }
00248 else
00249 {
00250
00251 }
00252 }
00253
00254 CT=T;
00255
00256 i=StationIndex(sta);
00257 trigger[i]|=1ULL;
00258
00259 for (i=0; i<nsta; i++)
00260 {
00261 trigger[i]&=mask;
00262 }
00263 trigger[MAXSTA-1]&=imask;
00264
00265 if (trigger[MAXSTA-1])
00266 {
00267 continue;
00268 }
00269
00270 count=0;
00271 for (i=0; i<nsta; i++)
00272 {
00273 if (trigger[i])
00274 {
00275 count++;
00276 }
00277 }
00278
00279 if (count>=minsta)
00280 {
00281
00282 PrintError("TRIGGER : %d [%d/%Ld]: %.23s ",CT,count,minsta,time2str((double)CT));
00283 for (i=0; i<nsta; i++)
00284 {
00285 if (trigger[i])
00286 {
00287 fprintf(stderr," %s", station[i]);
00288 }
00289 }
00290 fprintf(stderr,"\n");
00291
00292 trigger[MAXSTA-1]|=1;
00293 printf("- stalta %ld\n",CT);
00294
00295 }
00296 }
00297 return(0);
00298 }