/**********************************************************************
*
*  module   : Index
*  fichier  : IndexAssocCosinus.c
*  Auteur   : Jacques DUCLOY
*  Date     : 14.10.98 (ClusterAssocComp.c)
*
***********************************************************************/

#include "SxmlNode.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

static char *dFormat;
static double threshold;
static SxmlNode *assoc;

int getopt();
extern char *optarg;
extern int optind; 

int main(argc, argv)
     int   argc;
     char **argv;
  {    
    int cod_arg;
    char *bufNum;

    bufNum=malloc(20);
    threshold=0;
    dFormat="%5.3f";
    while ((cod_arg = getopt(argc,argv, "t:f:"))!=EOF)
      {switch (cod_arg)
	{
	case 't':
	  threshold=atof(optarg);
	  break;
	case 'f':
	  dFormat=optarg;
	  break;	
	}
      }
    while ((assoc=SxmlInputNextDocumentElement()))
      {
	char *fStr;
	double fi,fj,fij;
	fStr=SxmlLeafText(SxmlGetFirstChildByTagName(assoc,"fij"));
	fij=atof(fStr);
	fStr=SxmlLeafText(SxmlGetFirstChildByTagName(SxmlGetFirstChildByTagName(assoc,"ti"),"f"));
	fi=atof(fStr);
	fStr=SxmlLeafText(SxmlGetFirstChildByTagName(SxmlGetFirstChildByTagName(assoc,"tj"),"f"));
	fj=atof(fStr);
	if((fi>threshold)&&(fi>threshold))
	  {
	    printf(dFormat, sqrt((fij*fij/fi/fj)));
	    sprintf(bufNum, dFormat, sqrt((fij*fij/fi/fj)));
	    SxmlAppendChild(assoc, SxmlLeafCreate("w",bufNum));
	    putchar ('\t');
	    SxmlPrint(assoc);
	    putchar ('\n');
	  }
      }
    exit(0);
    
  }
