--- syntheseW.c.orig	2006-07-21 10:31:42.000000000 +0200
+++ syntheseW.c	2006-07-21 16:17:47.000000000 +0200
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include <math.h>
 #include  <time.h>
-#include </mnt/porto/sdb1/otig/frison/lib/hdf4/hdf.h>
+#include "hdf.h"
 #include <dirent.h>
 #include <limits.h>
 #define MIN_SIG -32.0
@@ -54,11 +54,17 @@
                  p->lon[1]=atof(argv[5]);
                  p->lat[0]=atof(argv[6]);
                  p->lat[1]=atof(argv[7]);
-                 p->dossier=(char*)malloc(sizeof(char)*(strlen(argv[8]+1)));
+                 /* BUG
+                  * attention, la dans le code original on faisait un strlen()
+                  * sur l'adresse de argv[x] + 1, donc on fait un acces memoire illegal.
+                  * Confusion avec strlen(argv[x]) + 1 pour allouer suffisamment de
+                  * place pour la chaine de caracteres et son caractere NUL terminal.
+                  */
+                 p->dossier=(char*)malloc(sizeof(char)*(strlen(argv[8])+1));
                  strcpy(p->dossier,argv[8]);
-                 p->sortie=(char*)malloc(sizeof(char)*(strlen(argv[9]+1)));
+                 p->sortie=(char*)malloc(sizeof(char)*(strlen(argv[9])+1));
                  strcpy(p->sortie,argv[9]);
-                 p->appendfilename=(char*)malloc(sizeof(char)*(strlen(argv[10]+1)));
+                 p->appendfilename=(char*)malloc(sizeof(char)*(strlen(argv[10])+1));
                  strcpy(p->appendfilename,argv[10]);
              }   else{
                  fprintf(stderr,"./syntheseW [date_deb][nb_jours][maille][long_min][long_max][lat_min][lat_max][dossier entre][dossier sortie][appendfile]\n");
@@ -251,7 +257,11 @@
  */    
  int readFile(double **sommeHH,double **nbvalHH,double **sommeVV,double **nbvalVV,parameter p,int *lignecol,char *filename,double **incidence){
      data mydata;
-     int x,y;
+     /* FIXME
+      * Ajout d'une variable i comme dans CreateImgEcart() pour simplifier
+      * les expressions.
+      */
+     int x,y,i;
      int k=0;
      FILE *file;
      
@@ -264,10 +274,16 @@
      /*
      NE PAS OUBLIER DE LIBERER CES TABLEAUX
      */
-     *nbvalHH=(double*)calloc(lignecol[0]*lignecol[1],sizeof(double));
-     *sommeHH=(double*)calloc(lignecol[0]*lignecol[1],sizeof(double));
-     *nbvalVV=(double*)calloc(lignecol[0]*lignecol[1],sizeof(double));
-     *sommeVV=(double*)calloc(lignecol[0]*lignecol[1],sizeof(double));
+     /* BUG
+      * Cette fonction lit et ecrit un float64 trop loin dans ces tableaux.
+      * Comme il s'agit peut etre d'un comportement correct, par exemple
+      * si on part de x=1 au lieu de x=0, on alloue ici la place necessaire
+      * a cette valeur supplementaire pour eviter un acces memoire illegal.
+      */
+     *nbvalHH=(double*)calloc(lignecol[0]*lignecol[1] +1,sizeof(double));
+     *sommeHH=(double*)calloc(lignecol[0]*lignecol[1] +1,sizeof(double));
+     *nbvalVV=(double*)calloc(lignecol[0]*lignecol[1] +1,sizeof(double));
+     *sommeVV=(double*)calloc(lignecol[0]*lignecol[1] +1,sizeof(double));
   
    printf("******PROCESSING********\n");
    
@@ -291,6 +307,11 @@
      /* calcule des coordonnées en pixel a partir des long/lat de l'entré*/            
          x=fabs((p.lon[0]-longi)/p.maille);
          y=fabs((p.lat[1]-mydata.lat)/p.maille);
+         /* FIXME
+          * Pour simplifier les expressions on utilise i pour les coordonnees
+          * du pixel courant.
+          */
+         i = y*lignecol[1]+x;
               
          /*printf("********************\n");
          printf("lat= %f  lon=%f\n",mydata.lat,mydata.lon);
@@ -300,14 +321,14 @@
           if (mydata.surface==TERRE){
           ++k;
           if(mydata.incidence<50){
-         (*sommeHH)[y*lignecol[1]+x]+=pow(10.0,correctS(mydata.sigma0,mydata.sigma0_attn_map,mydata.incidence)/10);
-         (*nbvalHH)[y*lignecol[1]+x]+=1;
+         (*sommeHH)[i]+=pow(10.0,correctS(mydata.sigma0,mydata.sigma0_attn_map,mydata.incidence)/10);
+         (*nbvalHH)[i]+=1;
          if(mydata.incidence<(*incidence)[0]) (*incidence)[0]=mydata.incidence;
          else if (mydata.incidence>(*incidence)[1]) (*incidence)[1]=mydata.incidence;
          }
          else{
-         (*sommeVV)[y*lignecol[1]+x]+=pow(10.0,correctS(mydata.sigma0,mydata.sigma0_attn_map,mydata.incidence)/10);
-         (*nbvalVV)[y*lignecol[1]+x]+=1;
+         (*sommeVV)[i]+=pow(10.0,correctS(mydata.sigma0,mydata.sigma0_attn_map,mydata.incidence)/10);
+         (*nbvalVV)[i]+=1;
          if(mydata.incidence<(*incidence)[2]) (*incidence)[2]=mydata.incidence;
          else if (mydata.incidence>(*incidence)[3]) (*incidence)[3]=mydata.incidence;
          }     
@@ -316,12 +337,12 @@
          /*Si l'entré est dans la mer*/
           else{  
               if(mydata.incidence<50){
-              (*sommeHH)[y*lignecol[1]+x]=-1;
-              (*nbvalHH)[y*lignecol[1]+x]=-1;     
+              (*sommeHH)[i]=-1;
+              (*nbvalHH)[i]=-1;
               }  
               else{
-              (*sommeVV)[y*lignecol[1]+x]=-1;
-              (*nbvalVV)[y*lignecol[1]+x]=-1;     
+              (*sommeVV)[i]=-1;
+              (*nbvalVV)[i]=-1;
               }
          } 
          
@@ -377,13 +398,24 @@
 double *ecartypeVV;
 data mydata;
 int x,y;
+/* BUG
+ * i etait utilise sans etre initialise ici, d'ou la segmentation fault !
+ * On a suppose que i correspondait aux coordonnees du pixel en cours de
+ * traitement; si ce n'est pas le cas, il faut lui affecter la valeur
+ * qu'il devait avoir pour ce calcul.
+ */
 int i;   
 FILE *outHH=fopen(FecartypeHH,"w");
 FILE *outVV=fopen(FecartypeVV,"w");
 FILE *file=fopen(filename,"r");
- 
-     ecartypeHH=(double*)calloc(lignecol[0]*lignecol[1],sizeof(double));
-     ecartypeVV=(double*)calloc(lignecol[0]*lignecol[1],sizeof(double));
+
+     /* BUG
+      * Meme probleme que dans ReadFile(), on accede a un float64 de trop.
+      * Meme bidouille pour eviter de lire de la memoire qui ne nous appartient
+      * pas.
+      */
+     ecartypeHH=(double*)calloc(lignecol[0]*lignecol[1] +1,sizeof(double));
+     ecartypeVV=(double*)calloc(lignecol[0]*lignecol[1] +1,sizeof(double));
   
      while(feof(file)==0)
      {
@@ -404,20 +436,26 @@
      /* calcule des coordonnées en pixel a partir des long/lat de l'entré*/            
          x=fabs((p.lon[0]-longi)/p.maille);
          y=fabs((p.lat[1]-mydata.lat)/p.maille);
+         i = y*lignecol[1]+x;
               
          /*printf("********************\n");
          printf("lat= %f  lon=%f\n",mydata.lat,mydata.lon);
          printf("sigma0db= %f \n",mydata.sigma0);
          printf("sigma0= %f \n",pow(10.0,mydata.sigma0/10));
          printf("%d %d  \n",x,y);*/
+
+         /* FIXME
+          * Pour simplifier les expressions et toujours sur la supposition que
+          * i correspondait aux coordonnees du pixel courant, on utilise i partout.
+          */
           if (mydata.surface==TERRE){
           if(mydata.incidence<50){
-         ecartypeHH[y*lignecol[1]+x]+=pow(pow(10.0,correctS(mydata.sigma0,mydata.sigma0_attn_map,mydata.incidence)/10)-sommeHH[i]/nbvalHH[i],2);
+         ecartypeHH[i]+=pow(pow(10.0,correctS(mydata.sigma0,mydata.sigma0_attn_map,mydata.incidence)/10)-sommeHH[i]/nbvalHH[i],2);
            if(mydata.incidence<(*incidenceEcart)[0]) (*incidenceEcart)[0]=mydata.incidence;
            else if (mydata.incidence>(*incidenceEcart)[1]) (*incidenceEcart)[1]=mydata.incidence;
          }
          else{
-         ecartypeVV[y*lignecol[1]+x]+=pow(pow(10.0,correctS(mydata.sigma0,mydata.sigma0_attn_map,mydata.incidence)/10)-sommeVV[i]/nbvalVV[i],2);
+         ecartypeVV[i]+=pow(pow(10.0,correctS(mydata.sigma0,mydata.sigma0_attn_map,mydata.incidence)/10)-sommeVV[i]/nbvalVV[i],2);
          if(mydata.incidence<(*incidenceEcart)[2]) (*incidenceEcart)[2]=mydata.incidence;
          else if (mydata.incidence>(*incidenceEcart)[3]) (*incidenceEcart)[3]=mydata.incidence;
          }     
@@ -426,10 +464,10 @@
          /*Si l'entré n'est pas dans la zone souhaité*/
           else{  
               if(mydata.incidence<50){
-              ecartypeHH[y*lignecol[1]+x]=-1;
+              ecartypeHH[i]=-1;
               }  
               else{
-              ecartypeVV[y*lignecol[1]+x]=-1;    
+              ecartypeVV[i]=-1;    
               }
          } 
          }
@@ -610,7 +648,7 @@
 free(p.appendfilename); 
 free(incidence);
 free(incidenceEcart);
-free(suffix);
+/*free(suffix);*/ /* BUG suffix n'a pas ete alloue ! */
 
 
 return 1;    
