source: ether_eccad/trunk/API_EXTRACT/src/eccad.c.new @ 70

Last change on this file since 70 was 68, checked in by cbipsl, 14 years ago

commit v1 eccad

  • Property svn:executable set to *
File size: 21.8 KB
Line 
1#include "eccad.h"
2
3
4EccadDB connectEccadDB(const char* server, const char* dbName,
5             const char* user, const char* pass, const char* port)
6{
7  EccadDB conn = PQsetdbLogin(server, port, NULL, NULL, dbName, user, pass);
8  if(!checkConnectionStatus(conn))
9  {
10    if (PQstatus(conn) == CONNECTION_BAD)
11    {
12      fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
13      fprintf(stderr, "%s", PQerrorMessage(conn));
14      PQfinish(conn);
15    }
16    return NULL;
17  }
18  return conn;
19}
20
21void closeEccadDB(EccadDB conn)
22{
23   PQfinish(conn);
24}
25
26gdImagePtr grid2img(EccadDB db, ptrGrid g, ptrParam p, int idMap)
27{
28  gdImagePtr im=NULL;
29  register int i,j;
30  LIST map;
31  /*if( idMap == 48)
32     // colormap dynamique les valeurs devrait etre codï¿œs en [O-1]
33    map= mapOfParamById(db, idMap, (double)0., (double)1.); 
34//     map= mapOfParamById(db, idMap, (double)0., (double)683.92); 
35  else */
36     map= mapOfParamById(db, idMap, p->mini, p->maxi);
37 fprintf(stderr,"start colorAllocate with %d colors\n", map->count);
38  if(map)
39  {
40    if (im= gdImageCreate(g->width, g->height))
41    {
42        fprintf(stderr," width %d height %d\n",g->width, g->height );   
43      colorAllocate(im, map);
44      for (j=0 ;  j < im->sy; j++)
45        for (i=0  ; i < im->sx ; i++)
46        {
47          int value= val2Color(g->datas[j * im->sx +i],  map);
48          gdImageSetPixel(im, i, j, value);
49        }
50    }
51  }
52  fprintf(stderr," ok\n" );
53  return im;
54}
55
56// insertion d'une colormap dans une image gd
57// aucune couleur ne doit etre alloue dans l'image d'origine
58void colorAllocate(gdImagePtr im, LIST map)
59{
60   ptrInterval v;
61   int i, j=0;
62   loop_through_list(map, v, ptrInterval)
63   {
64     i=gdImageColorAllocate(im, v->color->red ,v->color->green, v->color->blue);
65     //fprintf(stderr, "color Allocatealloue a l-ndex %d :%d \n", j, i);
66     j++;
67   }
68}
69
70char * oid2filename(EccadDB db, int oidGrille, int width, int height,
71                    char * idParam, int idMap, char *resultFile)
72{
73   char *fileName=NULL;
74   int grilleId;
75   char query[512];
76
77   PQexec(db,("BEGIN"));
78   if( (grilleId = lo_open(db, oidGrille, INV_READ)) != -1)
79   {
80     ptrGrid g= allocGrid( width, height);
81     if(g)
82     {
83       unsigned long K;
84       unsigned long size;
85
86       size=  (long)g->width * (long)g->height * sizeof(int);
87//fprintf(stderr, "MAXINT %ld 2*MAXINT %ld\n", MAXINT, (unsigned)MAXINT + MAXINT -1);
88       if( (K= lo_read(db, grilleId, (char*)g->datas, size)) == size)
89       {
90         PGresult *res;
91         sprintf(query,
92            "SELECT min_param,max_param FROM parametre WHERE id_param='%s'",
93            idParam);
94         res=PQexec(db,query);
95         if(checkSQLErrors(res))
96         {
97           double mini= atof( PQgetvalue(res, 0, 0));
98           double maxi= atof( PQgetvalue(res, 0, 1));
99           LIST map;
100
101           if(  map = mapOfParamById(db, idMap, mini, maxi))
102           {
103             gdImagePtr im;
104             if (im= gdImageCreate(width, height))
105             {
106               register int i,j;
107               FILE *out;
108fprintf(stderr," oidGrille %d mini %f maxi %f\n", oidGrille, mini,maxi);
109               colorAllocate(im, map);
110               for (j=0 ;  j < im->sy; j++)
111                 for (i=0  ; i < im->sx ; i++)
112                 {
113                   int value= val2Color(g->datas[j * im->sx +i], map);
114// if( g->datas[j * im->sx +i] !=0) printf( " %d\n", g->datas[j * im->sx +i]);
115                   gdImageSetPixel(im, i, j, value);
116                 }
117//               sprintf(query, "/tmp/%d.%d.png", oidGrille, idMap);
118               if( out= fopen(resultFile, "w"))
119               {
120                 gdImagePng(im, out);
121                 fclose(out);
122                 fileName=resultFile;
123               }
124               else fprintf(stderr,"Erreur open File %s\n", fileName);
125               gdImageDestroy (im);
126             }
127             else fprintf(stderr,"Erreur gdImageCreate\n");
128             freeMap(map);
129           }
130           else fprintf(stderr,"Erreur Check Map idMap %d\n", idMap);
131         }
132         else fprintf(stderr,"Erreur Check min max from idParam %s\n", idParam);
133         PQclear(res);
134       }
135       else fprintf(stderr," Erreur Read OID : %ld sur %d octets\n", K, size);
136fprintf (stderr, " now free grid\n");
137       freeGrid(g);
138     }
139     lo_close(db, grilleId);
140   }
141   else fprintf(stderr," Erreur Open OID : %d\n", oidGrille);
142   PQexec(db,("END"));
143   return fileName;
144}
145
146char * getRaster(const char *dbHost, const char * dbName, const char *dbUser, int idGrille, int idMap)
147{
148    char *resultFile;
149    struct stat sts;
150    char query[256];
151
152    sprintf(query,"/tmp/%d.%d.png", idGrille, idMap);
153    resultFile= strdup(query);
154
155    if ( stat (query, &sts) != 0)
156    {
157      char *fileName=NULL;
158      EccadDB db;
159
160      if((db= PQsetdbLogin(dbHost,"5432", NULL, NULL, dbName, dbUser, NULL)) &&
161         (PQstatus(db) != CONNECTION_BAD) )
162
163      {
164        PGresult *res;
165        sprintf(query,
166          "SELECT valeurs_grille, id_param, ncol_grille, nlign_grille FROM grille WHERE id_grille = %d;",
167          idGrille);
168        res=PQexec(db,query);
169        if(checkSQLErrors(res) && PQntuples(res))
170        {
171          int oid=   atoi(PQgetvalue(res, 0, 0));
172          int width=  atoi(PQgetvalue(res, 0, 2));
173          int height= atoi(PQgetvalue(res, 0, 3));
174          fileName= oid2filename( db, oid, width, height,
175                                PQgetvalue(res, 0, 1), idMap, resultFile);
176        }
177        else  fprintf(stderr, "No found id-grille %d\n", idGrille); 
178        PQclear(res);
179        PQfinish(db);
180      }
181      else  fprintf(stderr, "Erreur connect to db %s on host %s\n",
182                                       dbName, dbHost);
183      resultFile= fileName;
184   }
185   return resultFile;
186}
187//      ============================ Fonctions ==================================
188
189
190int checkConnectionStatus(EccadDB conn)
191{
192  char* erreur;
193       
194  if (conn==NULL || PQstatus(conn) == CONNECTION_BAD)
195  {
196     return (int)NULL;
197  }
198  else{
199    // la connection est ok
200    // mise en place de la date en UTC/GMT
201    // db->clock_consumed=clock();
202      PGresult* res=PQexec(conn, "set time zone 'UTC';");
203      if (!(res) ||
204          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
205                  (PQresultStatus(res) != PGRES_COMMAND_OK) ) ){
206                PQclear(res);
207        return 0==1;
208      }
209      res= PQexec(conn, "set enable_seqscan='false';");
210      if (!(res) ||
211          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
212          (PQresultStatus(res) != PGRES_COMMAND_OK) ) )
213      {
214        PQclear(res);
215        return 0==1;
216      }
217      res= PQexec(conn, "set datestyle to 'ISO, YMD';");
218      if (!(res) ||
219          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
220          (PQresultStatus(res) != PGRES_COMMAND_OK) ) )
221      {
222        PQclear(res);
223        return 0==1;
224      }
225      res= PQexec(conn, "set autocommit to 'on';");
226      if (!(res) ||
227          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
228          (PQresultStatus(res) != PGRES_COMMAND_OK) ) )
229      {
230        PQclear(res);
231        return 0==1;
232      }
233    }
234    return 0==0;
235}
236
237int checkSQLErrors(PGresult* res){
238        if ( !(res) ||
239          ((PQresultStatus(res) != PGRES_TUPLES_OK ) &&
240          (PQresultStatus(res) != PGRES_COMMAND_OK) ) )
241      {
242                return 0==1;
243        }
244        return 0==0;
245}
246
247LIST initColors(EccadDB conn)
248{
249    LIST colors= make_list();
250    PGresult *res;
251    res=PQexec(conn,"SELECT * FROM color;");
252    if(checkSQLErrors(res))
253    {
254      register int i;
255      for(i=0; i< PQntuples(res); i++)
256      {
257         ptrRGB c= alloueColor(
258             atoi(PQgetvalue(res, i, 0)),
259             atoi(PQgetvalue(res, i, 1)),
260             atoi(PQgetvalue(res, i, 2)),
261             atoi(PQgetvalue(res, i, 3)));
262        add_to_tail(colors, c);
263      }
264    }
265    PQclear(res);
266
267    return colors;
268}
269LIST initParams(EccadDB conn)
270{
271    LIST params= make_list();
272    PGresult *res;
273    res=PQexec(conn,"SELECT * FROM parametre;");
274    if(checkSQLErrors(res))
275    {
276      register int i;
277      for(i=0; i< PQntuples(res); i++)
278      {
279        double test = 0.00000001;
280       
281         fprintf(stderr, " PQgetvalue*--> maxPar %s   %s", PQgetvalue(res, i, 6), PQgetvalue(res, i, 3));
282         //fprintf(stderr, " PQgetvalue-> maxPar %f", (double)strtod(PQgetvalue(res, i, 6),NULL));
283       
284         ptrParam p= allocParam(
285             atoi(PQgetvalue(res, i, 0)),    //id
286             atoi(PQgetvalue(res, i, 2)),    //id unit
287             PQgetvalue(res, i, 3),         // full name
288             atof(PQgetvalue(res, i, 6)),    // minPpar
289             atof(PQgetvalue(res, i, 7)));   // maxPar
290        add_to_tail(params, p);
291      }
292
293    }
294    PQclear(res);
295    return params;
296}
297
298// generation de la table de couleur d'une map de la base
299//        idMap : id de la base
300//        mini/maxi : valeurs min max du parametre de la base
301//            ces deux valeurs sont nï¿œssaire car les intervalles de couleurs
302//              sont codes en unsigned maxhort / au extremes du param
303
304LIST mapOfParamById(EccadDB conn, int idMap, double mini, double  maxi)
305{
306   LIST map=NULL;
307   static LIST colors=NULL;
308   PGresult *res, *resc;
309   char query[256];
310   
311   int isDynamic = 0;
312   
313   /*****************/
314   mini=-1;
315   maxi=100;
316   int pas=10;
317   /****************/
318
319// mapping des couleurs de la base
320   if(!colors) colors=initColors(conn);
321
322fprintf(stderr,  "Generation interval for idMap %d With  min %f max %f\n",
323    idMap,  mini,  maxi);
324   
325
326//verification de la class map dynamique
327sprintf(query,
328       "SELECT * FROM class_map WHERE clm_id=96500");
329
330//sprintf(query,
331  //     "SELECT * FROM class_map WHERE clm_id=%d;", idClass);
332
333res=PQexec(conn,query);   
334 if(checkSQLErrors(res) && PQntuples(res))
335   {
336           isDynamic = (!strcmp(PQgetvalue(res, 0, 1),"linear") || !strcmp(PQgetvalue(res, 0, 1),"logarithm") || !strcmp(PQgetvalue(res, 0, 1),"exponential"));                         
337   }
338   
339// recherche des interval de la colormap   
340   sprintf(query,"SELECT * FROM class_index JOIN color USING (col_id) WHERE clm_id=%d order by num_order;", idMap);
341   res=PQexec(conn,query);
342   if(checkSQLErrors(res) && PQntuples(res))
343   {
344// mise en place des intervalles base dans la liste map
345      ptrInterval interv;
346      register int i;
347      map = make_list();
348      fprintf(stderr,  "--------*******--------min %ld max %ld\n",
349         v2int(mini,mini,maxi), v2int(maxi,mini,maxi));
350       unsigned int cmin,cmax;
351      for(i=0; i< PQntuples(res); i++)
352      {
353       
354        if (i==0) {
355                cmin=-1;
356                cmax=0;
357        }
358        else {
359                cmin=(i-1)*pas;
360                cmax=i*pas;
361        }
362       
363//      conversion en unsigned int des intervalles
364         /*int id= atoi(PQgetvalue(res, i, 1));   //idColor
365         if ((idMap == 2) || (idMap == 48))  cmin= v2int(atof(PQgetvalue(res, i, 3))* maxi,mini, maxi);
366         else  cmin= v2int(atof(PQgetvalue(res, i, 3)),mini, maxi);
367         if ((idMap == 2) || (idMap == 48))  cmax= v2int(atof(PQgetvalue(res, i, 2))* maxi,mini, maxi);
368         else cmax= v2int(atof(PQgetvalue(res, i, 2)),mini, maxi);
369         */
370         
371         int id=atoi(PQgetvalue(res, i, 1));//idColor
372         /*if (isDynamic)  cmin= v2int(atof(PQgetvalue(res, i, 3))* maxi,mini, maxi);
373         else  cmin= v2int(atof(PQgetvalue(res, i, 3)),mini, maxi);
374         if (isDynamic)  cmax= v2int(atof(PQgetvalue(res, i, 2))* maxi,mini, maxi);
375         else cmax= v2int(atof(PQgetvalue(res, i, 2)),mini, maxi);
376         */
377         
378         if (isDynamic)  cmin= v2int(cmin*maxi,mini, maxi);
379         else  cmin= v2int(cmin,mini, maxi);
380         
381         if (isDynamic)  cmax= v2int(cmax*maxi,mini, maxi);
382         else cmax= v2int(cmin,mini, maxi);
383                 
384                 //affectation des couleurs aux intervalles
385         ptrRGB c;
386         if( c= (ptrRGB)search_list(colors, &id, (PFI)inColorId) )
387         {
388                interv = alloueInterval( c,cmin, cmax);
389                        fprintf(stderr,  "add interval: cmin %s  %ld cmax %s  %ld id -> col %d\n",
390                PQgetvalue(res, i, 3), cmin, PQgetvalue(res, i, 2), cmax, c->id);
391           
392                /*         
393         fprintf(stderr,  "add interval: min %f max %f  id -> col %d\n",
394         int2v(cmin,mini,maxi), int2v(cmax,mini,maxi), c->id);
395                */
396                add_to_tail( map , interv);
397         }
398         else
399         {
400           fprintf(stderr,  "No found color for id %d\n", id);
401           exit(1);
402         }
403      }
404   }
405   else   fprintf(stderr,  "No found color for idMap  %d\n", idMap);
406
407   PQclear(res);
408
409   return map;
410}
411
412//generation d'une table de couleur en fobction d'un parametre //
413LIST mapOfParam(EccadDB conn, ptrParam p)
414{
415   LIST map=NULL;
416   PGresult *res;
417   char query[256];
418   
419   sprintf(query,"%s \'%s\';",
420      "SELECT cm_id FROM param_color NATURAL JOIN parametre WHERE fullName_param = ",p->name);
421   res=PQexec(conn,query);
422   if(checkSQLErrors(res))
423      map=  mapOfParamById(conn, atoi(PQgetvalue(res, 0, 0)), p->mini, p->maxi);
424   PQclear(res);
425   return map;
426}
427
428// w, h : size of origin grid isur zone globale implicite
429// z :  zone a extraire en inputs zone extraite en output
430static ptrGrid loadDatasFromBd(EccadDB db,int oidGrille,int w,int h,ptrZone z)
431{
432// la grille origine sans data alloue
433   Grid g;
434   g.width=w;
435   g.height=h;
436// la grille de sortie
437   ptrGrid grid=NULL;
438   {
439     int id;
440     PQexec(db,("BEGIN"));
441     if( (id = lo_open(db, oidGrille, INV_READ)) != -1)
442     {
443       double eps=0.000001;
444
445       if ((z != NULL) && memcmp(z, &globalZone, sizeof(Zone)) )
446       {
447         int iDeb ,iFin, jDeb, jFin;
448         double  paslon, paslat;
449         float x, y, lat, lon;
450         unsigned long K;
451// le pas dorigine       
452         paslat= pasLat(&globalZone, &g);
453         paslon= pasLon(&globalZone, &g);
454          // la zone qui doit etre extraite
455//         fprintf(stderr,
456//         "Extrait Origin Lat %f %f  pasLat %.2f lon %f %f pasLon %.2f\n",
457//z->latMin,z->latMax,paslat,z->lonMin,z->lonMax,paslon);
458// la zone ï¿œ  extraire
459         if( z->lonMax != z->lonMin)
460             z->lonMax=z->lonMax -eps;
461         if( z->latMax != z->latMin)
462           z->latMax=z->latMax +eps;
463         jDeb= lat2j(z->latMin, &g, &globalZone);
464         jFin= lat2j(z->latMax, &g, &globalZone);
465         iDeb= lon2i(z->lonMin, &g, &globalZone);
466         iFin= lon2i(z->lonMax, &g, &globalZone);
467         //la borne en width et height ne peut etre atteinte
468         if( iDeb == g.width) iDeb--;
469         if( jDeb == g.height) jDeb--;
470         if( iFin == g.width) iFin--;
471         if( jFin == g.height) jFin--;
472         
473         // recalcul de la zone extraite
474         lon = i2lon(iDeb,  &g, &globalZone);
475         lat = j2lat(jDeb,  &g, &globalZone);
476         z->lonMax = i2lon(iFin, &g, &globalZone) ;
477         z->latMax = j2lat(jFin, &g, &globalZone)
478// ajout des limites de domaines
479// car les positions sont aux centres pour les macro I2 et j2
480         z->lonMax += paslon/2.;
481         z->latMax += + paslat/2.;
482         z->lonMin = lon - paslon/2.;
483         z->latMin = lat - paslat/2.;
484      fprintf(stderr,
485           "----------------------------------------------------------------------------Extrait Reel Lat min %f max %f   lon min %f max %f width %d height %d \n",
486z->latMin,z->latMax,z->lonMin,z->lonMax, iFin -iDeb +1, jFin -jDeb +1);
487         
488         // extraction dans une nouvelle grille
489         grid= allocGrid(iFin -iDeb +1, jFin -jDeb +1);fprintf(stderr,"ok grid \n");
490         if(iFin < iDeb)
491         {
492            freeGrid(grid);
493            grid=NULL;
494    fprintf(stderr,"  Not now ok on partial grid crossing in longitude \n iDeb %d Ifin %d jDeb %d jFin %d\n", iDeb ,iFin, jDeb, jFin);
495         }
496         else
497         {
498           int i,j, pos, skip, width;
499           unsigned char *data=(unsigned char *)(grid->datas);
500           pos= (g.width*jDeb + iDeb) * sizeof(unsigned int);
501           lo_lseek(db, id, pos, SEEK_SET);
502           width= (iFin -iDeb +1) * sizeof(unsigned int);
503           skip= g.width * sizeof(unsigned int) - width ;
504// fprintf(stderr,"   ok pos %d width %d skip %d\n", pos/4 , width/4 , skip/4);
505           for(i=jDeb; i<=jFin; i++, data += width)
506           {
507             if((K= lo_read(db, id, (unsigned char*)data, width)) != (unsigned long)width)
508             {
509               fprintf(stderr, "Err readLine ligne %d read %ld sur %d demandes\n", i, K, width);
510               fprintf(stderr, "EXIT trap:\n iDeb %d iFin %d jDeb %d jFin %d\n pos %d width %d skip %d\n",
511                                 iDeb , iFin , jDeb , jFin , pos , width , skip);
512               exit (1);
513             }
514             lo_lseek(db, id, skip, SEEK_CUR);
515           }
516         }
517       }
518       else
519       {
520         unsigned long  K, size= g.width * g.height * sizeof(int);
521         grid= allocGrid(g.width , g.height);
522         if( (K= lo_read(db, id, (char*)grid->datas, size)) != size)
523            fprintf(stderr," Erreur Read OID : %ld sur %d octets\n", K, size);
524//       else
525 //           fprintf(stderr,"  Full Read OID : %ld sur %d octets\n", K, size);
526       }
527       lo_close(db,id);
528     }
529     else fprintf(stderr," Erreur Open OID : %d\n", oidGrille);
530     PQexec(db,("END"));
531   }
532   fprintf(stderr,"Open OID \n");
533   return grid;
534}
535
536ptrGridGL extractGridFromDb(EccadDB db, int idGrille, ptrZone z)
537{
538   static LIST params=NULL;
539   ptrGridGL gl=NULL;
540   PGresult *res;
541   char query[256];
542
543   sprintf(query,
544     "SELECT valeurs_grille, id_param, ncol_grille, nlign_grille, date_grille FROM grille WHERE id_grille = %d;", idGrille);
545   res=PQexec(db,query);
546   if(checkSQLErrors(res) && PQntuples(res))
547   {
548      ptrParam p;
549      int oid=     atoi(PQgetvalue(res, 0, 0));
550      int idParam= atoi(PQgetvalue(res, 0, 1));
551      int w=  atoi(PQgetvalue(res, 0, 2));
552      int h=  atoi(PQgetvalue(res, 0, 3));
553      if(!params) params=initParams(db);
554      if( p= (ptrParam)search_list(params, &idParam, (PFI)inParamId) )
555      {
556        ptrGrid g;
557        Zone zoneExtr;
558        g= loadDatasFromBd( db, oid, w, h, z);
559        strcpy(g->date, PQgetvalue(res, 0, 4) );
560        gl=  allocGridGL(g, z);
561        gl->param=p;
562      }
563      else fprintf(stderr, " No found para for id %d\n", idParam);
564
565    }
566    PQclear(res);
567    return gl; 
568}
569
570LIST getGrids(EccadDB db, int idProduct, ptrParam p, ptrZone z,
571                      const char* dateBegin, const char* dateEnd)
572{
573   int i;
574   LIST grids=NULL;
575   PGresult *res;
576   char query[256];
577   sprintf(query,
578      "SELECT valeurs_grille, ncol_grille,  nlign_grille, date_grille  FROM grille WHERE id_param = %d AND id_produit = %d AND date_grille BETWEEN '%s' AND '%s' order by date_grille",
579          p->id, idProduct, dateBegin, dateEnd);
580   res=PQexec(db,query);
581   fprintf(stderr, "found  %d grilles SQLErrors %d\n", PQntuples(res), checkSQLErrors(res));
582   if(checkSQLErrors(res) && PQntuples(res) )
583   {
584     // origin size
585     int width= atoi(PQgetvalue(res,0,1));
586     int height= atoi(PQgetvalue(res,0,2));
587     for(i=0; i< PQntuples(res); i++)
588     {
589       int oid= atoi(PQgetvalue(res,i,0) );
590/*fprintf(stderr, "Load oid %d from %dx%d grille date %s\n",
591oid,  width, height, PQgetvalue(res, i, 3) );*/
592
593fprintf(stderr,"Avant--------- : Extrait Reel Lat min %f max %f   lon min %f max %f \n",z->latMin,z->latMax,z->lonMin,z->lonMax);
594
595
596       ptrGrid g= loadDatasFromBd( db, oid, width, height, z);
597       
598fprintf(stderr,"Aprï¿œs--------- : Extrait Reel Lat min %f max %f   lon min %f max %f \n",z->latMin,z->latMax,z->lonMin,z->lonMax);
599      // printf("date grille %d\n",g->date);
600       if(g)
601       {
602         char *date= PQgetvalue(res, i, 3);
603         sprintf(g->date, "%.4s%.2s%.2s", date, &date[5], &date[8]);
604         if(!grids) grids = make_list();
605         add_to_tail(grids, g);
606       }
607       else fprintf(stderr, "loadGrid error\n");
608     }
609   }
610   PQclear(res);
611   return grids;
612}
613
614
615
616ptrGrid getGrid(EccadDB db, int idGrille, ptrZone z)
617{
618        fprintf(stderr, "--getGrid \n");fflush(stderr);
619        PGresult *res;
620        char query[256];
621        sprintf(query,
622      "SELECT valeurs_grille, ncol_grille, nlign_grille, date_grille FROM grille WHERE id_grille = %d;", idGrille);
623   fprintf(stderr, "1)-->requete %s \n",query );
624   res=PQexec(db,query);
625   fprintf(stderr, "found  %d grilles SQLErrors %d\n", PQntuples(res), checkSQLErrors(res));
626   fprintf(stderr, "2)-->requete %s \n",query );
627   //fprintf(stderr, "found  %d grilles SQLErrors %d\n", PQntuples(res), checkSQLErrors(res));
628   ptrGrid g = NULL;
629   
630   if(checkSQLErrors(res) && PQntuples(res) )
631   {
632        fprintf(stderr, "organise \n");
633        // origin size
634     int width= atoi(PQgetvalue(res,0,1));
635     int height= atoi(PQgetvalue(res,0,2));
636     
637     int oid= atoi(PQgetvalue(res,0,0) );
638//fprintf(stderr,"Avant--------- : Extrait Reel Lat min %f max %f   lon min %f max %f \n",z->latMin,z->latMax,z->lonMin,z->lonMax);
639       
640     g= loadDatasFromBd( db, oid, width, height, z);
641     
642//fprintf(stderr,"Apres--------- : Extrait Reel Lat min %f max %f   lon min %f max %f \n",z->latMin,z->latMax,z->lonMin,z->lonMax);
643     
644     
645     if(g)
646     {
647        char *date= PQgetvalue(res, 0, 3);
648        sprintf(g->date, "%.4s%.2s%.2s", date, &date[5], &date[8]);
649       
650                                fprintf(stderr, "date grille %s \n", date);
651     }   
652   }
653   
654   return g;
655}
656
657
658double* getMaxParam(int idGrid, EccadDB conn)
659{
660        double* maxParam = NULL;
661        PGresult *res;
662        char query[256];
663    sprintf(query,"SELECT id_param FROM grille WHERE id_grille=%d;", idGrid);
664    res=PQexec(conn,query);
665   
666    if(checkSQLErrors(res) && PQntuples(res) )
667    {
668        int idParam= atoi(PQgetvalue(res,0,0));
669       
670        ptrParam p;
671        LIST params=initParams(conn);
672        if( p= (ptrParam)search_list(params, (NODE)&idParam, (PFI)inParamId) ) 
673                        maxParam = &p->maxi;       
674    }
675        return maxParam;
676}
677
678double* getMinParam(int idGrid, EccadDB conn)
679{
680        double* minParam = NULL;
681        PGresult *res;
682        char query[256];
683    sprintf(query,"SELECT id_param FROM grille WHERE id_grille=%d;", idGrid);
684    res=PQexec(conn,query);
685   
686    if(checkSQLErrors(res) && PQntuples(res) )
687    {
688        int idParam= atoi(PQgetvalue(res,0,0));
689       
690        ptrParam p;
691        LIST params=initParams(conn);
692        if( p= (ptrParam)search_list(params, (NODE)&idParam, (PFI)inParamId) ) 
693                        minParam = &p->mini;       
694    }
695   
696        return minParam;
697}
698
Note: See TracBrowser for help on using the repository browser.