source: ether_eccad/trunk/API_EXTRACT/src/eccad.c @ 68

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

commit v1 eccad

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