source: trunk/src/seismic_processing/glass/src/modules/Publisher/publish.cpp @ 2189

Revision 2189, 8.7 KB checked in by paulf, 13 years ago (diff)

first inclusion

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/******************************************************************
2 *   THIS FILE IS UNDER CVS - DO NOT MODIFY UNLESS YOU HAVE
3 *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
4 *
5 *    $Id$
6 *
7 *    Revision history:
8 *     $Log$
9 *     Revision 1.1  2006/05/24 15:04:30  paulf
10 *     first inclusion
11 *
12 *     Revision 1.1.1.1  2005/06/22 19:30:48  michelle
13 *     new directory tree built from files in HYDRA_NEWDIR_2005-06-20 tagged hydra and earthworm projects
14 *
15 *     Revision 1.5  2005/02/28 18:27:22  davidk
16 *     Added code to filter out spurious associated phases at time of publication.
17 *     Modified call to AlgPrepOriginForPublication() to include a pointer to the
18 *     publishing module, so that the Prep funciton can call the module's
19 *     PrunePick() which eventually communicates to the associator module
20 *     that the pick should be pruned
21 *     from the origin.
22 *
23 *     Revision 1.4  2005/02/15 21:55:59  davidk
24 *     Added Lat/Lon/Time to the PubQuake struct that describes quakes
25 *     elligible to be published, in order to debug the publishing mechanism.
26 *     Now when a quake is deleted, a record of it is still kept in the publishing
27 *     display.
28 *
29 *     Revision 1.3  2004/11/02 19:07:26  davidk
30 *     Added code to mark any quakes as dead that appear to be deleted(no info in glint)
31 *     when they attempt to publish.
32 *     Addresses a problem where because of a glass::delete() bug, the publisher was
33 *     filling up the log file with <can't publish deleted quake> messages.
34 *     Once the quake is gone, it's not coming back, so this is just a redundant step
35 *     to delete (deleted) quakes.  An error is still issued, but this keeps the
36 *     log file from becoming unmanagable.
37 *
38 *     Revision 1.2  2004/09/16 01:16:05  davidk
39 *     Cleaned up logging messages.
40 *     Downgraded some debugging messages.
41 *     Set module to exit on all MAJOR_ERRORs.
42 *
43 *     Revision 1.1  2004/08/06 00:07:51  davidk
44 *     Code that controls the publishing mechanism.  Handles Origin change notifications,
45 *     and tracks publication.
46 *
47 *
48 *
49 *******************************************************************/
50
51#include <stdlib.h>
52#include <string.h>
53#include <Debug.h>
54#include "publish.h"
55#include <PublisherMod.h>
56#include <windows.h>
57
58/* global vars */
59PubQuake * QList;
60int iOldestQuake;
61int iYoungestQuake;
62int nQuakeListSize;
63time_t tLastChange;
64IGlint *pGlint;
65int iLastOriginChanged;
66
67static CMod * pMod;
68static int bInitialized=false;
69
70/* funcitons implemented by publish.c */
71
72int init(CMod * IN_pMod)
73{
74  nQuakeListSize = 100;
75
76  QList = (PubQuake *)calloc(1, nQuakeListSize * sizeof(PubQuake));
77  if(!QList)
78  {
79    CDebug::Log(DEBUG_MAJOR_ERROR, "Publisher:  Could not allocate %d bytes for %d Quake List elements.\n",
80                nQuakeListSize * sizeof(PubQuake), nQuakeListSize);
81    exit(-1);
82    return(-1);
83  }
84
85  iOldestQuake  = 0;
86  iYoungestQuake = -1;
87
88  pMod = IN_pMod;
89  pGlint = pMod->pGlint;
90
91  bInitialized = true;
92  tLastChange = 0;
93  iLastOriginChanged = -1;
94
95  AlgInit();
96
97  return(0);
98}
99
100int MakeSpaceInQList()
101{
102  int iMove;
103
104  /* if there are some dead quakes we can throw off the
105     end of the list, then do it.
106     Otherwise throw the oldest live quake off the end of
107     the list.
108   *****************************************************/
109  if(iOldestQuake > 0)
110   iMove = iOldestQuake - 0;
111  else
112  {
113   iMove = 1;
114   iOldestQuake++;
115  }
116
117  memmove(&QList[0], &QList[iOldestQuake], 
118          (nQuakeListSize - iOldestQuake) * sizeof(PubQuake));
119
120  /* decrement the youngest by number of quakes dropped (moved) */
121  iYoungestQuake -= iMove;
122
123  /* set the oldest back to 0 */
124  iOldestQuake = 0;
125
126  /* in case we deleted a live quake, and there are for some reason
127     dead quakes behind it, move the OldestQuake marker, so that
128     they will be deleted the next time through
129   *****************************************************/
130  for(int i=0; i <= iYoungestQuake; i++)
131  {
132    if(QList[i].bDead)
133      iOldestQuake++;
134    else 
135      break;
136  }
137
138  return(0);
139}  /* end MakeSpaceInQList() */
140
141
142int GetIndexOfNewQuake()
143{
144  if(iYoungestQuake == (nQuakeListSize - 1))
145    MakeSpaceInQList();
146
147  iYoungestQuake++;
148  memset(&QList[iYoungestQuake], 0, sizeof(PubQuake));
149  return(iYoungestQuake);
150}  /* end GetIndexOfNewQuake() */
151
152
153int GetIndexOfOrigin(char * idOrigin)
154{
155  int i;
156  for(i = iYoungestQuake; i >= iOldestQuake; i--)
157  {
158    if(strcmp(QList[i].idOrigin, idOrigin) == 0)
159      return(i);
160  }
161  return(-1);
162}  /* end GetIndexOfNewQuake() */
163
164
165int OriginChanged(char * idOrigin, OriginChangeType ectChangeType)
166{
167  int iQuake;
168  ORIGIN * pOrg;
169
170  time(&tLastChange);
171  iQuake = GetIndexOfOrigin(idOrigin);
172  if(iQuake < 0)
173  {
174    /* Unknown Quake */
175    if(ectChangeType == ORIGIN_CREATED)
176    {
177      /* new quake - create an entry for it*/
178      iQuake = GetIndexOfNewQuake();
179      if(iQuake < 0)
180      {
181        CDebug::Log(DEBUG_MINOR_ERROR, "GetIndexOfNewQuake() returned error(%d).  Serious! Aborting!\n",
182                    iQuake);
183        return(-1);
184      }
185      /* Create the Quake */
186      memset(&QList[iQuake], 0, sizeof(QList[iQuake]));
187      strncpy(QList[iQuake].idOrigin, idOrigin, sizeof(QList[iQuake].idOrigin)-1);
188      time(&QList[iQuake].tDeclare);
189
190      /* Get the Origin info from Glint */
191      if((pOrg = pGlint->getOrigin(idOrigin))==NULL)
192      {
193        CDebug::Log(DEBUG_MINOR_ERROR,"OriginChanged(): Could not obtain "
194                    "origin for id(%s)\n", idOrigin);
195        return(1);
196      }
197     
198      QList[iQuake].iOrigin = pOrg->iOrigin;
199      QList[iQuake].dLat = (float)pOrg->dLat;
200      QList[iQuake].dLon = (float)pOrg->dLon;
201      QList[iQuake].tOrigin = (float)pOrg->dT;
202
203      AlgOriginChanged(&QList[iQuake], ectChangeType);
204      time(&tLastChange);
205      iLastOriginChanged = iQuake;
206
207    }
208    else
209    {
210      CDebug::Log(DEBUG_MAJOR_WARNING, "OriginChanged(): Unknown idOrigin (%d)(%d).\n",
211                  idOrigin, (int)ectChangeType);
212      return(1);
213    }
214  }  /* end if quake not found */
215  else
216  {
217    time(&tLastChange);
218    iLastOriginChanged = iQuake;
219    QList[iQuake].iNumChanges++;
220
221    // ignore dead quakes
222    if(QList[iQuake].bDead)
223      return(0);
224
225    if(ectChangeType == ORIGIN_DELETED)
226    {
227      CDebug::Log(DEBUG_MAJOR_INFO, 
228                  "OriginChanged():  Marking quake(%s/%d/%d) as dead, got Delete Quake message.\n",
229                  idOrigin, iQuake, QList[iQuake].iOrigin);
230      QList[iQuake].bDead = true;
231      if(QList[iQuake].bPublished)
232        pMod->Retract(QList[iQuake].idOrigin, QList[iQuake].iOrigin);
233    }
234    else
235    {
236      /* Get the Origin info from Glint */
237      if((pOrg = pGlint->getOrigin(idOrigin))==NULL)
238      {
239        CDebug::Log(DEBUG_MINOR_ERROR,"OriginChanged(): Could not obtain "
240                    "origin for id(%s)\n", idOrigin);
241        return(1);
242      }
243     
244      QList[iQuake].dLat = (float)pOrg->dLat;
245      QList[iQuake].dLon = (float)pOrg->dLon;
246      QList[iQuake].tOrigin = (float)pOrg->dT;
247      AlgOriginChanged(&QList[iQuake], ectChangeType);
248    }
249  }
250
251  return(0);
252}  /* end OriginChanged() */
253
254  /* this is where we go through the list of events, and determine if any
255     need to be published
256   ***********************************************************************/
257int Process()
258{
259  int i;
260  time_t tNow;
261
262  if(!bInitialized)
263    return(0);
264
265
266  time(&tNow);
267
268  for(i=iOldestQuake; i <= iYoungestQuake; i++)
269  {
270    // Ignore dead quakes
271    if(QList[i].bDead)
272      continue;
273
274    if(QList[i].tNextPub > 0 &&  QList[i].tNextPub < tNow)
275    {
276      PublishOrigin(&QList[i]);
277      time(&tLastChange);
278      iLastOriginChanged = i;
279    }
280    if(AlgExamineOrigin(&QList[i]) == 0)
281    {
282      time(&tLastChange);
283      iLastOriginChanged = i;
284    }
285  }
286  return(0);
287}
288
289int PublishOrigin(PubQuake * pOrigin)
290{
291  time_t tNow;
292
293  AlgPrepOriginForPublication(pOrigin,pMod);
294
295  time(&tNow);
296  {
297    ORIGIN * pOrg;
298    pOrg = pGlint->getOrigin(pOrigin->idOrigin);
299    if(pOrg)
300      CDebug::Log(DEBUG_MINOR_INFO, 
301                  "Publishing Event %d with %d phases at time %s\n",
302                  pOrg->iOrigin, pOrg->nPh, ctime(&tNow));
303    else
304    {
305      CDebug::Log(DEBUG_MINOR_ERROR, 
306                  "Attempting to publish (dead?(%d)) Origin(%s)  at time %s. Ignoring pub request.\n"
307                  "  Marking quake as dead!\n",
308                  pOrigin->bDead, pOrigin->idOrigin, ctime(&tNow));
309      OriginChanged(pOrigin->idOrigin, ORIGIN_DELETED);
310      return(1);
311    }
312  }
313
314  pMod->Publish(pOrigin->idOrigin, pOrigin->iNumPub+1);
315
316  /* reset the PubQuake params */
317  pOrigin->tLastPub = tNow;
318  pOrigin->tNextPub = 0;
319  pOrigin->iNumPub++;
320  pOrigin->iNumChanges = 0;
321  if(!pOrigin->bPublished)
322    pOrigin->bPublished = true;
323
324  AlgOriginPublished(pOrigin);
325  return(0);
326}
327
328
329
330
Note: See TracBrowser for help on using the repository browser.