1 | #include <stdarg.h> |
---|
2 | #include <string.h> |
---|
3 | #include <limits.h> |
---|
4 | #include <dirent.h> |
---|
5 | #include <sys/stat.h> |
---|
6 | #include <sys/types.h> |
---|
7 | #include <fcntl.h> |
---|
8 | #include <errno.h> |
---|
9 | #include <float.h> |
---|
10 | #include <ctype.h> |
---|
11 | #include <unistd.h> |
---|
12 | #include <math.h> |
---|
13 | |
---|
14 | #include <libxml/tree.h> |
---|
15 | #include <libxml/valid.h> |
---|
16 | #include <libxml/parser.h> |
---|
17 | |
---|
18 | #include "eccad.h" |
---|
19 | #include "xml.h" |
---|
20 | #include "utils/gnuplot_i.h" |
---|
21 | |
---|
22 | |
---|
23 | ptrGrid getPoints(LIST grids, LIST args, ptrResponse r, ptrParam p) |
---|
24 | { |
---|
25 | fprintf (stderr,"ok getPoints \n" ); |
---|
26 | ptrGrid g= get_list_head(grids); |
---|
27 | if( (g->width == 1) && (g->height ==1)) // un seul pt |
---|
28 | { |
---|
29 | loop_through_list(grids, g, ptrGrid) |
---|
30 | { |
---|
31 | if(!(r->points)) r->points= make_list(); |
---|
32 | |
---|
33 | add_to_tail(r->points, |
---|
34 | allocPt(g->date, int2v(g->datas[0],p->mini,p->maxi))); |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | } |
---|
39 | } |
---|
40 | else errorResponse( r, "fonction getGrahique inapplicaple sur surfaces"); |
---|
41 | return (ptrGrid)NULL; |
---|
42 | } |
---|
43 | |
---|
44 | ptrGrid getStat(LIST grids, LIST args, ptrResponse r, ptrParam p) |
---|
45 | { |
---|
46 | |
---|
47 | |
---|
48 | ptrGrid g= get_list_head(grids); |
---|
49 | char buf[64]; |
---|
50 | if( (g->width == 1) && (g->height ==1)) |
---|
51 | { |
---|
52 | double moy, ect=0., sum=0.; |
---|
53 | unsigned int min=g->datas[0], max=min; |
---|
54 | |
---|
55 | // fprintf(stderr, "Calcul sur 1 pt sur %d grilles\n", grids->count); |
---|
56 | loop_through_list(grids, g, ptrGrid) |
---|
57 | { |
---|
58 | sum += int2v(g->datas[0], p->mini, p->maxi); |
---|
59 | if(g->datas[0] > max ) max= g->datas[0]; |
---|
60 | if(g->datas[0] < min ) min= g->datas[0]; |
---|
61 | } |
---|
62 | moy= sum / grids->count; |
---|
63 | loop_through_list(grids, g, ptrGrid) |
---|
64 | ect += pow((double)(int2v(g->datas[0],p->mini,p->maxi)) - moy, (double)2); |
---|
65 | ect = sqrt(ect/ grids->count); |
---|
66 | r->valeurs= make_list(); |
---|
67 | |
---|
68 | sprintf(buf, "%f", sum); |
---|
69 | add_to_tail(r->valeurs, allocArg("somme", buf)); |
---|
70 | sprintf(buf, "%f", moy); |
---|
71 | add_to_tail(r->valeurs, allocArg("moyenne", buf)); |
---|
72 | sprintf(buf, "%f", int2v(min, p->mini, p->maxi)); |
---|
73 | add_to_tail(r->valeurs, allocArg("min", buf)); |
---|
74 | sprintf(buf, "%f", int2v(max, p->mini, p->maxi)); |
---|
75 | add_to_tail(r->valeurs, allocArg("max", buf)); |
---|
76 | sprintf(buf, "%f", ect); |
---|
77 | add_to_tail(r->valeurs, allocArg("ecartType", buf)); |
---|
78 | |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | ptrGrid gfin= get_list_tail(grids); |
---|
83 | |
---|
84 | if( strcmp(g->date, gfin->date)) |
---|
85 | errorResponse( r, "fonction getstat inapplicaple sur dates et surfaces"); |
---|
86 | else |
---|
87 | { |
---|
88 | double moy, ect=0., sum=0.; |
---|
89 | unsigned int min=g->datas[0], max=min; |
---|
90 | int k, size= g->width * g->height; |
---|
91 | |
---|
92 | |
---|
93 | fprintf(stderr, "*--*-*-*-*-*-*-*min: %.10f, max: %.10f", (double)p->mini, (double)p->maxi); |
---|
94 | |
---|
95 | for (k=0; k < size; k++) |
---|
96 | { |
---|
97 | sum += int2v(g->datas[k], p->mini, p->maxi); |
---|
98 | if(g->datas[k] > max ) max= g->datas[k]; |
---|
99 | if(g->datas[k] < min ) min= g->datas[k]; |
---|
100 | } |
---|
101 | moy= sum / size; |
---|
102 | |
---|
103 | fprintf(stderr, "*--*-*-*-*-*-*-*SUM: %.10f, MOY: %.10f", (double)sum, (double)moy); |
---|
104 | |
---|
105 | for (k=0; k < size; k++) |
---|
106 | ect += pow((double)(int2v(g->datas[k],p->mini,p->maxi))-moy, (double)2); |
---|
107 | ect = sqrt(ect / size); |
---|
108 | r->valeurs= make_list(); |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | sprintf(buf, "%.12f", sum); |
---|
114 | add_to_tail(r->valeurs, allocArg("somme", buf)); |
---|
115 | sprintf(buf, "%.12f", moy); |
---|
116 | add_to_tail(r->valeurs, allocArg("moyenne", buf)); |
---|
117 | sprintf(buf, "%.12f", int2v(min, p->mini, p->maxi)); |
---|
118 | add_to_tail(r->valeurs, allocArg("min", buf)); |
---|
119 | sprintf(buf, "%.12f", int2v(max, p->mini, p->maxi)); |
---|
120 | add_to_tail(r->valeurs, allocArg("max", buf)); |
---|
121 | sprintf(buf, "%.12f", ect); |
---|
122 | add_to_tail(r->valeurs, allocArg("ecartType", buf)); |
---|
123 | } |
---|
124 | } |
---|
125 | fprintf(stderr, "FIN*--*-*-*-*-*-*-*"); |
---|
126 | return (ptrGrid)NULL; |
---|
127 | } |
---|
128 | ptrGrid ecartTypeGrid(LIST grids, LIST args, ptrResponse r, ptrParam p) |
---|
129 | { |
---|
130 | // warn la table de couleur n'est surement pas approprie |
---|
131 | ptrGrid ect=NULL; |
---|
132 | ptrGrid g= get_list_head(grids); |
---|
133 | if( grids->count > 1) |
---|
134 | { |
---|
135 | int k, size= g->width * g->height; |
---|
136 | double *datas= (double *)calloc( size, sizeof(double)); |
---|
137 | double *ects= (double *)calloc( size, sizeof(double)); |
---|
138 | double maxi= MINDOUBLE; |
---|
139 | double mini= MAXDOUBLE; |
---|
140 | // calcul de moyenne dans datas en double |
---|
141 | ect= allocGrid(g->width, g->height); |
---|
142 | loop_through_list(grids, g, ptrGrid) |
---|
143 | { |
---|
144 | for (k=0; k < size; k++) |
---|
145 | datas[k] += int2v(g->datas[k], p->mini, p->maxi); |
---|
146 | } |
---|
147 | for (k=0; k < size; k++) |
---|
148 | datas[k] /= grids->count; |
---|
149 | // calcul ecart type |
---|
150 | loop_through_list(grids, g, ptrGrid) |
---|
151 | { |
---|
152 | for (k=0; k < size; k++) |
---|
153 | ects[k] += pow((double)(int2v(g->datas[k], p->mini, p->maxi)) - datas[k], (double)2); |
---|
154 | } |
---|
155 | for (k=0; k < size; k++) |
---|
156 | { |
---|
157 | ects[k] = sqrt(ects[k]/ grids->count); |
---|
158 | if( ects[k] > r->maxi) r->maxi=ects[k]; |
---|
159 | if( ects[k] < r->mini) r->mini=ects[k]; |
---|
160 | } |
---|
161 | |
---|
162 | // codage en entier sur echelle dynamique |
---|
163 | for (k=0; k < size; k++) |
---|
164 | ect->datas[k] = v2int(ects[k], r->mini, r->maxi); |
---|
165 | p->mini = r->mini; |
---|
166 | p->maxi = r->maxi; |
---|
167 | |
---|
168 | fprintf(stderr, "Champs: Mini %f maxi %f Param: Min %f max %f\n", |
---|
169 | r->mini, r->maxi, p->mini, p->maxi); |
---|
170 | |
---|
171 | free(datas); |
---|
172 | free(ects); |
---|
173 | } |
---|
174 | else if( grids->count) |
---|
175 | { |
---|
176 | int k, size= g->width * g->height; |
---|
177 | ptrGrid ect= allocGrid(g->width, g->height); |
---|
178 | for (k=0; k < size; k++) ect->datas[k] = 1 ; |
---|
179 | } |
---|
180 | |
---|
181 | // r->centerValue = (double)int2v( ect->datas[(int)((ect->height*ect->width)/2)], p->mini, p->maxi ); |
---|
182 | |
---|
183 | |
---|
184 | return ect; |
---|
185 | } |
---|
186 | |
---|
187 | ptrGrid maxGrid(LIST grids, LIST args, ptrResponse R, ptrParam p) |
---|
188 | { |
---|
189 | |
---|
190 | ptrGrid g= get_list_head(grids); |
---|
191 | if( grids->count > 1) |
---|
192 | { |
---|
193 | int k, size= g->width * g->height; |
---|
194 | ptrGrid max= allocGrid(g->width, g->height); |
---|
195 | |
---|
196 | loop_through_list(grids, g, ptrGrid) |
---|
197 | { |
---|
198 | for (k=0; k < size; k++) |
---|
199 | if(g->datas[k] > max->datas[k] ) max->datas[k]= g->datas[k]; |
---|
200 | if (size == 1) fprintf(stderr,"******************valeur %f", max->datas[k]); |
---|
201 | } |
---|
202 | |
---|
203 | // R->centerValue = (double)int2v( max->datas[(int)((max->height*max->width)/2)], p->mini, p->maxi ); |
---|
204 | return max; |
---|
205 | } |
---|
206 | else if( grids->count) |
---|
207 | { |
---|
208 | // R->centerValue = (double)int2v( g->datas[(int)((g->height*g->width)/2)], p->mini, p->maxi ); |
---|
209 | return dupGrid(g); |
---|
210 | } |
---|
211 | else return (ptrGrid)NULL; |
---|
212 | } |
---|
213 | |
---|
214 | ptrGrid minGrid(LIST grids, LIST args, ptrResponse R, ptrParam p) |
---|
215 | { |
---|
216 | |
---|
217 | ptrArg a; |
---|
218 | ptrGrid g= get_list_head(grids); |
---|
219 | // loop_through_list(args, a, ptrArg) |
---|
220 | // fprintf(stderr, "Arg name %s value %s\n", a->name, a->value); |
---|
221 | |
---|
222 | if( grids->count > 1) |
---|
223 | { |
---|
224 | int k, size= g->width * g->height; |
---|
225 | ptrGrid min= allocGrid(g->width, g->height); |
---|
226 | memcpy(min->datas, g->datas, size * sizeof(int)); |
---|
227 | |
---|
228 | loop_through_list(grids, g, ptrGrid) |
---|
229 | { |
---|
230 | for (k=0; k < size; k++) |
---|
231 | if(g->datas[k] < min->datas[k] ) min->datas[k]= g->datas[k]; |
---|
232 | if (size == 1)fprintf(stderr,"******************valeur %f", min->datas[k]); |
---|
233 | } |
---|
234 | // R->centerValue = (double)int2v( min->datas[(int)((min->height*min->width)/2)], p->mini, p->maxi ); |
---|
235 | return min; |
---|
236 | } |
---|
237 | else if( grids->count) |
---|
238 | { |
---|
239 | return dupGrid(g); |
---|
240 | // R->centerValue = (double)int2v( g->datas[(int)((g->height*g->width)/2)], p->mini, p->maxi ); |
---|
241 | } |
---|
242 | else return (ptrGrid)NULL; |
---|
243 | } |
---|
244 | |
---|
245 | ptrGrid moyGrid(LIST grids,LIST args, ptrResponse R, ptrParam p) |
---|
246 | { |
---|
247 | fprintf(stderr,"---> avant ----------reponse :min %.10f max %.10f",p->mini, p->maxi ); |
---|
248 | double minMoy, maxMoy; |
---|
249 | |
---|
250 | minMoy = MAXDOUBLE; |
---|
251 | maxMoy = MINDOUBLE; |
---|
252 | |
---|
253 | ptrGrid g= get_list_head(grids); |
---|
254 | if( grids->count > 1) |
---|
255 | { |
---|
256 | int k, size= g->width * g->height; |
---|
257 | ptrGrid moy= allocGrid(g->width, g->height); |
---|
258 | double *datas= (double *)calloc( size, sizeof(double)); |
---|
259 | |
---|
260 | loop_through_list(grids, g, ptrGrid) |
---|
261 | for (k=0; k < size; k++) |
---|
262 | { |
---|
263 | datas[k] += (double) int2v(g->datas[k],p->mini,p->maxi); |
---|
264 | if (size == 1) fprintf(stderr,"******************valeur %f", datas[k]); |
---|
265 | |
---|
266 | } |
---|
267 | for (k=0; k < size; k++) |
---|
268 | { |
---|
269 | datas[k] = ((datas[k])/grids->count); |
---|
270 | if (datas[k] > maxMoy) maxMoy = datas[k]; |
---|
271 | if (datas[k] < minMoy) minMoy = datas[k]; |
---|
272 | } |
---|
273 | |
---|
274 | p->mini = minMoy; |
---|
275 | |
---|
276 | p->maxi = maxMoy; |
---|
277 | //p->maxi = (double)1.; |
---|
278 | for (k=0; k < size; k++) |
---|
279 | moy->datas[k] = (unsigned int)v2int(datas[k], p->mini, p->maxi); |
---|
280 | |
---|
281 | |
---|
282 | //p->maxi = (double)4.; |
---|
283 | |
---|
284 | |
---|
285 | R->mini = p->mini; |
---|
286 | R->maxi = p->maxi; |
---|
287 | fprintf(stderr,"---> <---- Reponse :min %f max %f",p->mini, p->maxi ); |
---|
288 | free(datas); |
---|
289 | // R->centerValue = (double)int2v( moy->datas[(int)((moy->height*moy->width)/2)], p->mini, p->maxi ); |
---|
290 | fprintf(stderr,"moy->height %d moy->width %d", moy->height, moy->width ); |
---|
291 | |
---|
292 | // R->centerValue = 9999999; |
---|
293 | return moy; |
---|
294 | } |
---|
295 | else if( grids->count) |
---|
296 | { |
---|
297 | // R->centerValue = (double)int2v( g->datas[(int)((g->height * g->width)/2)], p->mini, p->maxi ); |
---|
298 | return dupGrid(g); |
---|
299 | } |
---|
300 | else return (ptrGrid)NULL; |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | ptrGrid sumGrid(LIST grids,LIST args, ptrResponse R, ptrParam p) |
---|
305 | { |
---|
306 | fprintf(stderr,"---> avant ----------reponse :min %f max %f",p->mini, p->maxi ); |
---|
307 | double minSum, maxSum; |
---|
308 | |
---|
309 | minSum = MAXDOUBLE; |
---|
310 | maxSum = MINDOUBLE; |
---|
311 | |
---|
312 | ptrGrid g= get_list_head(grids); |
---|
313 | if( grids->count > 1) |
---|
314 | { |
---|
315 | int k, size= g->width * g->height; |
---|
316 | ptrGrid sum= allocGrid(g->width, g->height); |
---|
317 | double *datas= (double *)calloc( size, sizeof(double)); |
---|
318 | |
---|
319 | loop_through_list(grids, g, ptrGrid) |
---|
320 | for (k=0; k < size; k++) |
---|
321 | { |
---|
322 | datas[k] += (double) int2v(g->datas[k],p->mini,p->maxi); |
---|
323 | if (size == 1) fprintf(stderr,"******************valeur %f", datas[k]); |
---|
324 | |
---|
325 | if (datas[k] > maxSum) maxSum = datas[k]; |
---|
326 | if (datas[k] < minSum) minSum = datas[k]; |
---|
327 | |
---|
328 | } |
---|
329 | |
---|
330 | p->mini = minSum; |
---|
331 | p->maxi = maxSum; |
---|
332 | //p->maxi = (double)1.; |
---|
333 | for (k=0; k < size; k++) |
---|
334 | sum->datas[k] = (unsigned int)v2int(datas[k], p->mini, p->maxi); |
---|
335 | |
---|
336 | R->mini = p->mini; |
---|
337 | R->maxi = p->maxi; |
---|
338 | fprintf(stderr,"---> <---- Reponse :min %f max %f",p->mini, p->maxi ); |
---|
339 | free(datas); |
---|
340 | // R->centerValue = (double)int2v( sum->datas[(int)((sum->height*sum->width)/2)], p->mini, p->maxi ); |
---|
341 | return sum; |
---|
342 | } |
---|
343 | else if( grids->count) |
---|
344 | { |
---|
345 | //R->centerValue = (double)int2v( g->datas[(int)((g->height*g->width)/2)], p->mini, p->maxi ); |
---|
346 | return dupGrid(g); |
---|
347 | } |
---|
348 | else return (ptrGrid)NULL; |
---|
349 | } |
---|
350 | |
---|
351 | |
---|
352 | ptrGrid testGrid(LIST grids,LIST args, ptrResponse R, ptrParam p) |
---|
353 | { |
---|
354 | |
---|
355 | ptrGrid g= get_list_head(grids); |
---|
356 | char * v= ((ptrArg)get_list_head(args))->value; |
---|
357 | if( grids->count > 1) |
---|
358 | { |
---|
359 | int k, size= g->width * g->height; |
---|
360 | ptrGrid moy= allocGrid(g->width, g->height); |
---|
361 | double *datas= (double *)calloc( size, sizeof(double)); |
---|
362 | int fact= atoi(v); |
---|
363 | |
---|
364 | loop_through_list(grids, g, ptrGrid) |
---|
365 | { |
---|
366 | for (k=0; k < size; k++) |
---|
367 | datas[k] += (double)(g->datas[k]); |
---|
368 | } |
---|
369 | for (k=0; k < size; k++) |
---|
370 | moy->datas[k] = (unsigned int)(datas[k]/grids->count) *fact; |
---|
371 | free(datas); |
---|
372 | return moy; |
---|
373 | } |
---|
374 | else if( grids->count) return dupGrid(g); |
---|
375 | else return (ptrGrid)NULL; |
---|
376 | } |
---|
377 | |
---|
378 | ptrGrid getGridRaster(LIST args, ptrResponse R, ptrParam p) |
---|
379 | { |
---|
380 | ptrArg arg; |
---|
381 | fprintf(stderr, "getGrid function \n"); |
---|
382 | if ( args->count > 0 ) |
---|
383 | { |
---|
384 | fprintf(stderr, "args->count %d \n", args->count); |
---|
385 | loop_through_list(args, arg, ptrArg) |
---|
386 | { |
---|
387 | if (!strcmp(arg->name, "grille")) |
---|
388 | { |
---|
389 | int id = atoi(arg->value); |
---|
390 | //fprintf(stderr, "grille %d \n", id); |
---|
391 | sprintf(R->file,"/tmp/%d", id); |
---|
392 | return getGrid(R->db, id, R->zone); |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | else |
---|
397 | { |
---|
398 | errorResponse( R, "parameter missef for function getGridRaster"); |
---|
399 | return (ptrGrid)NULL; |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | /* traitements des emissions */ |
---|
404 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
405 | ptrGrid getEmissionGrid(LIST grids,LIST args, ptrResponse R, ptrParam p) /////////////////////////////// |
---|
406 | { |
---|
407 | ptrGrid g= get_list_head(grids); |
---|
408 | int size= g->width * g->height; |
---|
409 | |
---|
410 | int k, id, code; |
---|
411 | ptrGrid grid=NULL, emissionF=NULL, biomassD=NULL, burningE=NULL, pixelA=NULL, classe=NULL, emission=NULL; |
---|
412 | LIST vegetationClasses=NULL; |
---|
413 | ptrArg arg; |
---|
414 | |
---|
415 | double minEmissionF, maxEmissionF, minBiomassD, maxBiomassD, minBurningE, maxBurningE, minVegetationC, |
---|
416 | maxVegetationC, minBurntA, maxBurntA, minEmission, maxEmission; |
---|
417 | |
---|
418 | if ( args->count > 3 ) |
---|
419 | { |
---|
420 | loop_through_list(args, arg, ptrArg) |
---|
421 | { |
---|
422 | if (!strcmp(arg->name, "gaz")) |
---|
423 | { |
---|
424 | id = atoi(arg->value); |
---|
425 | emissionF = getGrid(R->db, id, (ptrZone)NULL); |
---|
426 | |
---|
427 | if ( !( ( minEmissionF = *(getMinParam(id, R->db)) ) || ( maxEmissionF = *(getMaxParam(id, R->db)) ) ) ) |
---|
428 | { |
---|
429 | errorResponse(R, "Can't find min/max param EF"); |
---|
430 | return (ptrGrid)NULL; |
---|
431 | } |
---|
432 | |
---|
433 | fprintf(stderr,"minEmissionF %f\n", (float)minEmissionF); |
---|
434 | |
---|
435 | fprintf(stderr,"maxEmissionF %f\n",(float)maxEmissionF ); |
---|
436 | } |
---|
437 | else if (!strcmp(arg->name, "biomassDensity")) |
---|
438 | { |
---|
439 | id = atoi(arg->value); |
---|
440 | biomassD = getGrid(R->db, id, (ptrZone)NULL); |
---|
441 | |
---|
442 | if ( !( ( minBiomassD = *getMinParam(id, R->db) ) || ( maxBiomassD = *(getMaxParam(id, R->db)) ) ) ) |
---|
443 | { |
---|
444 | errorResponse(R, "Can't find min/max param BD"); |
---|
445 | return (ptrGrid)NULL; |
---|
446 | } |
---|
447 | fprintf(stderr,"minBiomassD %f\n", (float)minBiomassD); |
---|
448 | |
---|
449 | fprintf(stderr,"maxBiomassD %f\n", (float)maxBiomassD); |
---|
450 | } |
---|
451 | else if (!strcmp(arg->name, "burningEfficiency")) |
---|
452 | { |
---|
453 | id = atoi(arg->value); |
---|
454 | burningE = getGrid(R->db, id, (ptrZone)NULL); |
---|
455 | |
---|
456 | if ( !( ( minBurningE = *getMinParam(id, R->db) ) || ( maxBurningE = *(getMaxParam(id, R->db)) ) ) ) |
---|
457 | { |
---|
458 | errorResponse(R, "Can't find min/max param BE"); |
---|
459 | return (ptrGrid)NULL; |
---|
460 | } |
---|
461 | fprintf(stderr,"minBurningE %f\n", (float)minBurningE); |
---|
462 | |
---|
463 | fprintf(stderr,"maxBurningE %f\n",(float)maxBurningE ); |
---|
464 | } |
---|
465 | |
---|
466 | } |
---|
467 | |
---|
468 | |
---|
469 | |
---|
470 | if ( emissionF == NULL || biomassD == NULL || burningE == NULL ) |
---|
471 | errorResponse(R, "Can't find all chimical data"); |
---|
472 | else |
---|
473 | { |
---|
474 | //fprintf(stderr,"---> reponse : %s ", rep); |
---|
475 | //test d extraction des emissions |
---|
476 | int sizeArray = emissionF->width * emissionF->height; |
---|
477 | |
---|
478 | //fprintf("taille %i\n",sizeArray ); |
---|
479 | |
---|
480 | for (k=0; k < sizeArray; k++) |
---|
481 | //printf("valeurs emissionF unsigned int %f\n", (float)(emissionF->datas[k])); |
---|
482 | fprintf(stderr,"valeurs emissionF %f\n", (float)int2v(emissionF->datas[k],0,1613 )); |
---|
483 | for (k=0; k < sizeArray; k++) |
---|
484 | fprintf(stderr,"valeurs biomassDensity %f\n", (float)int2v(biomassD->datas[k],0,36.7 )); |
---|
485 | for (k=0; k < sizeArray; k++) |
---|
486 | fprintf(stderr,"valeurs burningEff(ptrzone)iciency %f\n", (float)int2v(burningE->datas[k],0,13 )); |
---|
487 | |
---|
488 | // recuperation des min max param |
---|
489 | |
---|
490 | //la somme des valeurs des classes de vegetations |
---|
491 | //ptrGrid sommeVclasses= allocGrid(g->width, g->height); |
---|
492 | double *sommeVclasses= (double *)calloc( size, sizeof(double)); |
---|
493 | loop_through_list(args, arg, ptrArg) |
---|
494 | { |
---|
495 | if (strcmp(arg->name, "gaz") && strcmp(arg->name, "biomassDensity") && strcmp(arg->name, "burningEfficiency")) |
---|
496 | { |
---|
497 | |
---|
498 | if (!isnan((float)atoi(arg->name))) code = atoi(arg->name); |
---|
499 | else{ |
---|
500 | errorResponse(R, "Can't code of vegetation classe"); |
---|
501 | return (ptrGrid)NULL; |
---|
502 | } |
---|
503 | if (!isnan((float)atoi(arg->value))) id = atoi(arg->value); |
---|
504 | else{ |
---|
505 | errorResponse(R, "Can't id_grille of vegetation classe"); |
---|
506 | return (ptrGrid)NULL; |
---|
507 | } |
---|
508 | |
---|
509 | classe = getGrid(R->db, id, R->zone); |
---|
510 | |
---|
511 | |
---|
512 | if ( !( ( minVegetationC = *(getMinParam(id, R->db)) ) || ( maxVegetationC = *(getMaxParam(id, R->db)) ) ) ) |
---|
513 | { |
---|
514 | errorResponse(R, "Can't find min/max param vegetationSpecies"); |
---|
515 | return (ptrGrid)NULL; |
---|
516 | } |
---|
517 | |
---|
518 | fprintf(stderr,"minVegetationC %f\n", (float)minVegetationC); |
---|
519 | |
---|
520 | fprintf(stderr,"maxVegetationC %f\n",(float)maxVegetationC ); |
---|
521 | fprintf(stderr,"emissionF %f\n",(float)int2v(emissionF->datas[code],minEmissionF,maxEmissionF) ); |
---|
522 | fprintf(stderr,"biomassD %f\n",(float)int2v(biomassD->datas[code],minBiomassD,maxBiomassD) ); |
---|
523 | fprintf(stderr,"burningE %f\n",(float)int2v(burningE->datas[code],minBurningE,maxBurningE) ); |
---|
524 | |
---|
525 | |
---|
526 | for (k=0; k < size; k++) |
---|
527 | sommeVclasses[k] += (double)( (float)(((float)int2v(classe->datas[k],minVegetationC,maxVegetationC))/100)*(float)(int2v(emissionF->datas[code],minEmissionF,maxEmissionF)) * (float)(int2v(biomassD->datas[code],minBiomassD,maxBiomassD)) * (float)(int2v(burningE->datas[code],minBurningE,maxBurningE))); |
---|
528 | } |
---|
529 | } |
---|
530 | |
---|
531 | |
---|
532 | //boucle sur les grids ... multiplication et somme |
---|
533 | emission= allocGrid(g->width, g->height); |
---|
534 | double *emissionD= (double *)calloc( size, sizeof(double)); |
---|
535 | |
---|
536 | minEmission = (double)0.; |
---|
537 | maxEmission = (double)0.; |
---|
538 | |
---|
539 | loop_through_list(grids, grid, ptrGrid) |
---|
540 | { |
---|
541 | for (k=0; k < size; k++) |
---|
542 | { |
---|
543 | emissionD[k] = (double)((((float)(int2v(grid->datas[k],p->mini,p->maxi))) * (float)sommeVclasses[k])/ 1000 ); |
---|
544 | if (emissionD[k] > maxEmission) |
---|
545 | { |
---|
546 | maxEmission = emissionD[k]; |
---|
547 | fprintf(stderr,"----------- Somme Vclasses %f -----------datas %f\n",(float)sommeVclasses[k], (int2v(grid->datas[k],p->mini,p->maxi))); |
---|
548 | } |
---|
549 | } |
---|
550 | } |
---|
551 | |
---|
552 | /*minEmission = minVegetationC * minEmissionF * minBiomassD * minBurningE * grids->count*p->mini; |
---|
553 | maxEmission = maxVegetationC * maxEmissionF * maxBiomassD * maxBurningE * grids->count*p->maxi;*/ |
---|
554 | |
---|
555 | fprintf(stderr,"maxEmission %f\n",(float)maxEmission ); |
---|
556 | p->mini = minEmission; |
---|
557 | p->maxi = maxEmission; |
---|
558 | for (k=0; k < size; k++) |
---|
559 | emission->datas[k] = v2int(emissionD[k],minEmission,maxEmission); |
---|
560 | |
---|
561 | |
---|
562 | R->mini = minEmission; |
---|
563 | R->maxi = maxEmission; |
---|
564 | |
---|
565 | free(sommeVclasses); |
---|
566 | free(emissionD); |
---|
567 | } |
---|
568 | } |
---|
569 | else errorResponse(R, "parameters requeired"); |
---|
570 | return emission; |
---|
571 | } |
---|
572 | |
---|
573 | |
---|
574 | ptrGrid getGraph(LIST grids, LIST args, ptrResponse r, ptrParam p) |
---|
575 | { |
---|
576 | |
---|
577 | ptrGrid g= get_list_head(grids); |
---|
578 | if( (g->width == 1) && (g->height ==1)) // un seul pt |
---|
579 | { |
---|
580 | loop_through_list(grids, g, ptrGrid) |
---|
581 | { |
---|
582 | if(!(r->points)) r->points= make_list(); |
---|
583 | add_to_tail(r->points, |
---|
584 | allocPt(g->date, int2v(g->datas[0],p->mini,p->maxi))); |
---|
585 | } |
---|
586 | } |
---|
587 | else errorResponse( r, "fonction getGrahique inapplicaple sur surfaces"); |
---|
588 | |
---|
589 | ///////////////partie gnu plot///////////////////// |
---|
590 | |
---|
591 | |
---|
592 | gnuplot_ctrl *h ; |
---|
593 | char x[r->points->count][10]; |
---|
594 | double y[r->points->count] ; |
---|
595 | int i ; |
---|
596 | |
---|
597 | h = gnuplot_init() ; |
---|
598 | |
---|
599 | if( r->points) |
---|
600 | { |
---|
601 | ptrPt p; |
---|
602 | int i =0; |
---|
603 | loop_through_list(r->points, p, ptrPt) |
---|
604 | { |
---|
605 | |
---|
606 | strcpy(x[i], p->date); |
---|
607 | //memcpy(&x[i], &p->date, 10*sizeof(char)); |
---|
608 | //x[i] = (double)i; |
---|
609 | y[i] = (double)p->value; |
---|
610 | |
---|
611 | //printf(" Reponse -y[i] %f x[i] %s date%s\n", (float) y[i], (char*)x[i], (char*)p->date); |
---|
612 | |
---|
613 | i++; |
---|
614 | |
---|
615 | } |
---|
616 | } |
---|
617 | |
---|
618 | |
---|
619 | gnuplot_plot_datey(h, x, y, r->points->count, "graphique") ; |
---|
620 | gnuplot_close(h); |
---|
621 | |
---|
622 | |
---|
623 | |
---|
624 | |
---|
625 | |
---|
626 | return (ptrGrid)NULL; |
---|
627 | } |
---|
628 | |
---|
629 | ptrGrid getCeneter(LIST grids, LIST args, ptrResponse R, ptrParam p,ptrZone zCenter) |
---|
630 | { |
---|
631 | ptrResponse Rprovisoire= allocResponse((ptrZone)zCenter, (ptrTemporal)NULL); |
---|
632 | ptrGrid g= getPoints(grids, args, Rprovisoire, p); |
---|
633 | |
---|
634 | ptrPt pt; |
---|
635 | int nbre = 0; |
---|
636 | |
---|
637 | |
---|
638 | if( Rprovisoire->points){ |
---|
639 | ptrPt pt = (ptrPt)Rprovisoire->points->first->value; |
---|
640 | R->centerValue = pt->value; |
---|
641 | |
---|
642 | } |
---|
643 | else |
---|
644 | errorResponse(R, "can't found center value : error"); |
---|
645 | |
---|
646 | return NULL; |
---|
647 | } |
---|
648 | |
---|
649 | |
---|
650 | BKF bkf[]= |
---|
651 | { |
---|
652 | { "ecartCarte", (PFG)ecartTypeGrid}, |
---|
653 | { "moyenneCarte", (PFG)moyGrid}, |
---|
654 | { "minimumCarte", (PFG)minGrid}, |
---|
655 | { "maximumCarte", (PFG)maxGrid}, |
---|
656 | { "sommeCarte", (PFG)sumGrid}, |
---|
657 | { "getStatistique", (PFG)getStat}, |
---|
658 | { "getGraphique", (PFG)getPoints}, |
---|
659 | { "testCarte", (PFG)testGrid}, |
---|
660 | { "getEmission", (PFG)getEmissionGrid}, |
---|
661 | { "getGraphiquePng", (PFG)getGraph}, |
---|
662 | { NULL, (PFG)NULL} |
---|
663 | }; |
---|
664 | |
---|
665 | ptrTemporal allocTemporal(char *deb, char *fin) |
---|
666 | { |
---|
667 | ptrTemporal t= (ptrTemporal)malloc(sizeof(Temporal)); |
---|
668 | if( t) |
---|
669 | { |
---|
670 | strcpy(t->debut, deb); |
---|
671 | strcpy(t->fin, fin); |
---|
672 | } |
---|
673 | return t; |
---|
674 | } |
---|
675 | void freeTemporal( ptrTemporal t) { free(t);} |
---|
676 | |
---|
677 | ptrResponse allocResponse(ptrZone z, ptrTemporal t) |
---|
678 | { |
---|
679 | ptrResponse r= (ptrResponse)malloc(sizeof(Response)); |
---|
680 | |
---|
681 | if( r) |
---|
682 | { |
---|
683 | r->error[0]= '\0'; |
---|
684 | r->file[0]= '\0'; |
---|
685 | r->zone=z; |
---|
686 | r->temporal=t; |
---|
687 | r->paslat= 0.; |
---|
688 | r->paslon= 0.; |
---|
689 | r->mini= MAXDOUBLE; |
---|
690 | r->maxi= -MAXDOUBLE; |
---|
691 | r->valeurs=NULL; |
---|
692 | r->points=NULL; |
---|
693 | } |
---|
694 | return r; |
---|
695 | } |
---|
696 | |
---|
697 | void freeResponse( ptrResponse r) { |
---|
698 | if( r->temporal) free(r->temporal); |
---|
699 | if( r->zone) free(r->zone); |
---|
700 | if( r->valeurs) |
---|
701 | { |
---|
702 | ptrArg a; |
---|
703 | loop_through_list(r->valeurs, a, ptrArg) |
---|
704 | freeArg(a); |
---|
705 | free_list(r->valeurs); |
---|
706 | } |
---|
707 | if( r->points) |
---|
708 | { |
---|
709 | ptrPt p; |
---|
710 | loop_through_list(r->points, p, ptrPt) |
---|
711 | freePt(p); |
---|
712 | } |
---|
713 | free(r); |
---|
714 | } |
---|
715 | |
---|
716 | /* now in define see xml.h |
---|
717 | void errorResponse( ptrResponse r, char * msg) { |
---|
718 | strcpy(r->error, msg); |
---|
719 | }*/ |
---|
720 | |
---|
721 | // serialisation XML de la structure reponse |
---|
722 | char *response2xml(ptrResponse r) |
---|
723 | { |
---|
724 | char rep[320000]; |
---|
725 | strcpy(rep, "<reponse>"); |
---|
726 | fprintf(stderr,"---> reponse : %s ", rep); |
---|
727 | if( r->zone) |
---|
728 | { |
---|
729 | strcat(rep, "\n <domaine>"); |
---|
730 | strcat(rep, "\n <lat-max>"); |
---|
731 | sprintf(&rep[strlen(rep)], "%.2f", r->zone->latMin); |
---|
732 | |
---|
733 | strcat(rep, "</lat-max>"); |
---|
734 | strcat(rep, "\n <lat-min>"); |
---|
735 | sprintf(&rep[strlen(rep)], "%.2f", r->zone->latMax); |
---|
736 | strcat(rep, "</lat-min>"); |
---|
737 | strcat(rep, "\n <lon-max>"); |
---|
738 | sprintf(&rep[strlen(rep)], "%.2f", r->zone->lonMax); |
---|
739 | strcat(rep, "</lon-max>"); |
---|
740 | strcat(rep, "\n <lon-min>"); |
---|
741 | sprintf(&rep[strlen(rep)], "%.2f", r->zone->lonMin); |
---|
742 | strcat(rep, "</lon-min>"); |
---|
743 | strcat(rep, "\n </domaine>"); |
---|
744 | } |
---|
745 | fprintf(stderr,"---> reponse : %s ", rep); |
---|
746 | if( r->temporal) |
---|
747 | { |
---|
748 | strcat(rep, "\n <temporel>"); |
---|
749 | strcat(rep, "\n <begin>"); |
---|
750 | strcat(rep, r->temporal->debut); |
---|
751 | strcat(rep, "</begin>"); |
---|
752 | strcat(rep, "\n <end>"); |
---|
753 | strcat(rep, r->temporal->fin); |
---|
754 | strcat(rep, "</end>"); |
---|
755 | strcat(rep, "\n </temporel>"); |
---|
756 | } |
---|
757 | |
---|
758 | fprintf(stderr,"---> reponse : %s ", rep); |
---|
759 | |
---|
760 | if( r->file[0]) |
---|
761 | { |
---|
762 | strcat(rep, "\n <image-path>"); |
---|
763 | strcat(rep, r->file); |
---|
764 | strcat(rep, "</image-path>"); |
---|
765 | } |
---|
766 | |
---|
767 | // if (r->centerValue) |
---|
768 | // { |
---|
769 | strcat(rep, "\n <center>"); |
---|
770 | strcat(rep, "\n <center-value>"); |
---|
771 | sprintf(&rep[strlen(rep)], "%.12f", r->centerValue); |
---|
772 | strcat(rep, " </center-value>"); |
---|
773 | |
---|
774 | strcat(rep, "\n <center-lat>"); |
---|
775 | sprintf(&rep[strlen(rep)], "%.2f", r->centerLat); |
---|
776 | strcat(rep, " </center-lat>"); |
---|
777 | |
---|
778 | strcat(rep, "\n <center-lon>"); |
---|
779 | sprintf(&rep[strlen(rep)], "%.2f", r->centerLon); |
---|
780 | strcat(rep, " </center-lon>"); |
---|
781 | |
---|
782 | strcat(rep, "\n </center>"); |
---|
783 | |
---|
784 | // } |
---|
785 | |
---|
786 | if( r->error[0]) |
---|
787 | { |
---|
788 | strcat(rep, "\n <error>"); |
---|
789 | strcat(rep, r->error); |
---|
790 | strcat(rep, "\n </error>"); |
---|
791 | } |
---|
792 | if( r->paslat) |
---|
793 | { |
---|
794 | strcat(rep, "\n <pas>"); |
---|
795 | strcat(rep, "\n <pas-lat>"); |
---|
796 | sprintf(&rep[strlen(rep)], "%.2f", r->paslat); |
---|
797 | strcat(rep, "</pas-lat>"); |
---|
798 | strcat(rep, "\n <pas-lon>"); |
---|
799 | sprintf(&rep[strlen(rep)], "%.2f", r->paslon); |
---|
800 | strcat(rep, "</pas-lon>"); |
---|
801 | strcat(rep, "\n </pas>"); |
---|
802 | } |
---|
803 | fprintf(stderr,"---> reponse : %s ", rep); |
---|
804 | if( r->maxi != -MAXDOUBLE) |
---|
805 | { |
---|
806 | strcat(rep, "\n <extremes>"); |
---|
807 | strcat(rep, "\n <mini>"); |
---|
808 | sprintf(&rep[strlen(rep)], "%.12f", r->mini); |
---|
809 | strcat(rep, "</mini>"); |
---|
810 | strcat(rep, "\n <maxi>"); |
---|
811 | sprintf(&rep[strlen(rep)], "%.12f", r->maxi); |
---|
812 | strcat(rep, "</maxi>"); |
---|
813 | strcat(rep, "\n </extremes>"); |
---|
814 | |
---|
815 | } |
---|
816 | |
---|
817 | |
---|
818 | if( r->valeurs) |
---|
819 | { |
---|
820 | ptrArg a; |
---|
821 | strcat(rep, "\n <statistique>"); |
---|
822 | loop_through_list(r->valeurs, a, ptrArg) |
---|
823 | { |
---|
824 | strcat(rep, "\n <variable>"); |
---|
825 | sprintf(&rep[strlen(rep)], "\n <nom>%s</nom>", a->name); |
---|
826 | sprintf(&rep[strlen(rep)], "\n <valeur>%s</valeur>", a->value); |
---|
827 | strcat(rep, "\n </variable>"); |
---|
828 | } |
---|
829 | strcat(rep, "\n </statistique>"); |
---|
830 | } |
---|
831 | if( r->points) |
---|
832 | { |
---|
833 | ptrPt p; |
---|
834 | strcat(rep, "\n <graphe>"); |
---|
835 | int nbre = 0; |
---|
836 | //fprintf(stderr,"---> nombre de point traites \n:"); |
---|
837 | loop_through_list(r->points, p, ptrPt) |
---|
838 | { |
---|
839 | nbre++; |
---|
840 | fprintf(stderr,"* %d \n",nbre); |
---|
841 | strcat(rep, "\n <point>"); |
---|
842 | sprintf(&rep[strlen(rep)], "\n <x>%s</x>", p->date); |
---|
843 | sprintf(&rep[strlen(rep)], "\n <y>%f</y>", p->value); |
---|
844 | /*strcat(rep, "\n <x>20000611</x>"); |
---|
845 | strcat(rep, "\n <y>1.000000</y>");*/ |
---|
846 | |
---|
847 | strcat(rep, "\n </point>"); |
---|
848 | } |
---|
849 | strcat(rep, "\n </graphe>"); |
---|
850 | } |
---|
851 | strcat(rep, "\n</reponse>"); |
---|
852 | freeResponse(r); |
---|
853 | //return &rep[0]; |
---|
854 | return strdup(rep); |
---|
855 | } |
---|
856 | |
---|
857 | |
---|
858 | // |
---|
859 | // routines recursives pour trouver les noeuds d'un documents |
---|
860 | // |
---|
861 | // 1. recuperation contenu d'un element et suppression des blancs parasites |
---|
862 | xmlChar * contentOfNode(xmlDocPtr doc, xmlNodePtr p) |
---|
863 | { |
---|
864 | xmlChar *v=NULL; |
---|
865 | xmlChar *value= xmlNodeListGetString(doc, p->xmlChildrenNode, 1); |
---|
866 | if(v= value) |
---|
867 | { |
---|
868 | char *c= value + strlen(value) -1; |
---|
869 | while(*v == ' ') v++; |
---|
870 | while(*c == ' ') {*c=(char)0;c--;} |
---|
871 | v= strdup(v); |
---|
872 | xmlFree(value); |
---|
873 | } |
---|
874 | return v; |
---|
875 | } |
---|
876 | |
---|
877 | // recherche du contenu d'un element de nom NAME et |
---|
878 | // suppression des blancs parasites |
---|
879 | // a partir du noeud P |
---|
880 | xmlChar * contentOfNodeNamed(xmlDocPtr doc, xmlNodePtr p, char *name) |
---|
881 | { |
---|
882 | xmlChar *v=NULL; |
---|
883 | if(p) |
---|
884 | { |
---|
885 | if( xmlStrcmp(p->name,name) == 0 ) |
---|
886 | return contentOfNode(doc, p); |
---|
887 | else if(p->children && (v= contentOfNodeNamed(doc,p->children, name))) |
---|
888 | return v; |
---|
889 | else v= contentOfNodeNamed(doc,p->next, name); |
---|
890 | } |
---|
891 | return v; |
---|
892 | } |
---|
893 | // 2. recherche d'un element a partir du noeud P |
---|
894 | xmlNodePtr getFirstNode( xmlNodePtr p, xmlChar *name) |
---|
895 | { |
---|
896 | xmlNodePtr p1=p; |
---|
897 | if(!p) return(p); |
---|
898 | if(!xmlIsBlankNode(p) && (xmlStrcmp(p->name,name) ==0)) return p; |
---|
899 | if(p->children && (p1= getFirstNode(p->children, name))) return p1; |
---|
900 | else return getFirstNode(p->next, name); |
---|
901 | } |
---|
902 | |
---|
903 | ptrZone loadXmlZone(xmlDocPtr doc, xmlNodePtr domaine) |
---|
904 | { |
---|
905 | ptrZone z; |
---|
906 | if(domaine && ( z=(ptrZone)malloc(sizeof(Zone)) ) ) |
---|
907 | { |
---|
908 | z->latMin= atof(contentOfNodeNamed(doc, domaine->children, "lat-max") ); |
---|
909 | if( (z->latMin <= globalZone.latMin) && (z->latMin >= globalZone.latMax)) |
---|
910 | { |
---|
911 | z->latMax= atof(contentOfNodeNamed(doc, domaine->children, "lat-min") ); |
---|
912 | if( (z->latMax <= globalZone.latMin) && (z->latMax >= globalZone.latMax)) |
---|
913 | { |
---|
914 | z->lonMin= atof(contentOfNodeNamed(doc, domaine->children, "lon-min") ); |
---|
915 | if( (z->lonMin >= globalZone.lonMin) && (z->lonMin <= globalZone.lonMax)) |
---|
916 | { |
---|
917 | z->lonMax= atof(contentOfNodeNamed(doc, domaine->children, "lon-max") ); |
---|
918 | if( (z->lonMax >= globalZone.lonMin) && (z->lonMax <= globalZone.lonMax)) |
---|
919 | return z; |
---|
920 | } |
---|
921 | } |
---|
922 | } |
---|
923 | free(z); |
---|
924 | } |
---|
925 | return (ptrZone)NULL; |
---|
926 | } |
---|
927 | |
---|
928 | // routine d'erreurs xml |
---|
929 | void myError (void *ctx, const char *fmt, ...) |
---|
930 | { |
---|
931 | if (fmt) |
---|
932 | { |
---|
933 | static char p[512]; |
---|
934 | va_list ap; |
---|
935 | printf("NACK error Dtd: "); |
---|
936 | va_start(ap, fmt); |
---|
937 | vsnprintf(p,512,fmt,ap); |
---|
938 | va_end(ap); |
---|
939 | printf(p); |
---|
940 | } |
---|
941 | } |
---|
942 | |
---|
943 | void myWarning (void *ctx, const char *fmt, ...) |
---|
944 | { |
---|
945 | if (fmt) |
---|
946 | { |
---|
947 | static char p[512]; |
---|
948 | va_list ap; |
---|
949 | printf("NACK warning Dtd: "); |
---|
950 | va_start(ap, fmt); |
---|
951 | vsnprintf(p,512,fmt,ap); |
---|
952 | va_end(ap); |
---|
953 | printf(p); |
---|
954 | } |
---|
955 | } |
---|
956 | |
---|
957 | ptrArg allocArg(char *name, char *value) |
---|
958 | { |
---|
959 | ptrArg arg=(ptrArg)malloc(sizeof(Arg)); |
---|
960 | if(name) arg->name=strdup(name); |
---|
961 | if(value) arg->value=strdup(value); |
---|
962 | return arg; |
---|
963 | } |
---|
964 | void freeArg(ptrArg arg) |
---|
965 | { |
---|
966 | if(arg->name) free(arg->name); |
---|
967 | if(arg->value) free(arg->value); |
---|
968 | free (arg); |
---|
969 | } |
---|
970 | |
---|
971 | // Execution d'une requete emise en XML (doc) |
---|
972 | static char * execXml(xmlDocPtr doc, ptrResponse R) |
---|
973 | { |
---|
974 | char buf[256]; |
---|
975 | xmlChar *host, *dbName, *user; |
---|
976 | xmlNodePtr argNode, node= getFirstNode(doc->children, "connexion"); |
---|
977 | |
---|
978 | if( (host= contentOfNodeNamed(doc, node->children, "host")) && |
---|
979 | (dbName= contentOfNodeNamed(doc, node->children, "dbname")) && |
---|
980 | (user= contentOfNodeNamed(doc, node->children, "user") )) |
---|
981 | { |
---|
982 | EccadDB db; |
---|
983 | if(db= connectEccadDB((char *)host, (char *)dbName, |
---|
984 | (char *)user, "", "5432") ) |
---|
985 | { |
---|
986 | int idProduit, idParam, idColormap; |
---|
987 | LIST params=initParams(db); |
---|
988 | LIST args=NULL; |
---|
989 | |
---|
990 | xmlChar *dateDeb, *dateFin, *function; |
---|
991 | ptrZone z=loadXmlZone(doc,getFirstNode(doc->children,"domaine")); |
---|
992 | if( z) |
---|
993 | { |
---|
994 | ptrParam p; |
---|
995 | idProduit=atoi(contentOfNodeNamed(doc, doc->children,"produit")); |
---|
996 | idParam= atoi(contentOfNodeNamed(doc,doc->children,"parametre")); |
---|
997 | idColormap= atoi(contentOfNodeNamed(doc, doc->children, "colormap")); |
---|
998 | node= getFirstNode(doc->children, "temporel"); |
---|
999 | if (node) |
---|
1000 | { |
---|
1001 | dateDeb=contentOfNodeNamed(doc, node->children, "begin"); |
---|
1002 | dateFin=contentOfNodeNamed(doc, node->children, "end"); |
---|
1003 | } |
---|
1004 | if( node= getFirstNode(doc->children, "fonction")) |
---|
1005 | { |
---|
1006 | function= contentOfNodeNamed(doc, node->children, "nom"); |
---|
1007 | |
---|
1008 | node= getFirstNode(node->children, "params"); |
---|
1009 | if (node) |
---|
1010 | { |
---|
1011 | |
---|
1012 | for (argNode = node->children; argNode; argNode = argNode->next) |
---|
1013 | { |
---|
1014 | ptrArg a; |
---|
1015 | if( xmlIsBlankNode(argNode)) continue; |
---|
1016 | if( !args) args=make_list(); |
---|
1017 | a= allocArg(contentOfNodeNamed(doc, argNode->children, "nom"), |
---|
1018 | contentOfNodeNamed(doc, argNode->children, "valeur")); |
---|
1019 | add_to_tail(args, a); |
---|
1020 | fprintf(stderr,"found params (%s: %s)\n", a->name, a->value); |
---|
1021 | } |
---|
1022 | |
---|
1023 | } |
---|
1024 | // else |
---|
1025 | // fprintf(stderr," No args for %s fonction\n",function ); |
---|
1026 | } |
---|
1027 | |
---|
1028 | if( p= (ptrParam)search_list(params, (NODE)&idParam, (PFI)inParamId) ) |
---|
1029 | { |
---|
1030 | LIST grids; |
---|
1031 | fprintf (stderr,"host %s dbname %s user %s\n", host, dbName, user); |
---|
1032 | fprintf (stderr,"------------> latMin %f latMax %f lonMin %f lonMax %f\n", |
---|
1033 | z->latMin, z->latMax, z->lonMin, z->lonMax); |
---|
1034 | fprintf(stderr,"fonction %s idProduit %d Parametre %s p->id %d idColormap %d\n", |
---|
1035 | function, idProduit, p->name, p->id, idColormap); |
---|
1036 | ptrGrid g=NULL; |
---|
1037 | |
---|
1038 | R->zone= z; |
---|
1039 | R->db = db; |
---|
1040 | |
---|
1041 | //cas de getGridRaster |
---|
1042 | if (!strcmp(function,"getGridRaster")) |
---|
1043 | { |
---|
1044 | g = getGridRaster(args, R, p); |
---|
1045 | sprintf(R->file,"%s.%d.png",R->file, idColormap); |
---|
1046 | |
---|
1047 | ///initialisation du centre |
---|
1048 | R->centerLon = ((z->lonMax + z->lonMin)/2); |
---|
1049 | R->centerLat = ((z->latMax + z->latMin)/2); |
---|
1050 | |
---|
1051 | |
---|
1052 | ptrZone zCenter=(ptrZone)malloc(sizeof(Zone)); |
---|
1053 | zCenter->latMax= R->centerLat; |
---|
1054 | zCenter->latMin= R->centerLat; |
---|
1055 | zCenter->lonMax= R->centerLon; |
---|
1056 | zCenter->lonMin= R->centerLon; |
---|
1057 | |
---|
1058 | |
---|
1059 | ptrResponse Rp= allocResponse((ptrZone)zCenter, (ptrTemporal)NULL); |
---|
1060 | Rp->db = R->db; |
---|
1061 | ptrGrid ptGr = getGridRaster(args, Rp, p); |
---|
1062 | LIST gridss = make_list(); |
---|
1063 | add_to_tail(gridss, ptGr); |
---|
1064 | |
---|
1065 | //grids= getGrids(db,idProduit, p, zCenter, g->date, g->date); |
---|
1066 | |
---|
1067 | getCeneter(gridss, args, R, p, z); |
---|
1068 | //////////////////////////////////////////////////////// |
---|
1069 | |
---|
1070 | fprintf (stderr,"centerValue %f\n",R->centerValue ); |
---|
1071 | fprintf (stderr,"centerLon %f\n",R->centerLon ); |
---|
1072 | fprintf (stderr,"centerLat %f\n",R->centerLat ); |
---|
1073 | } |
---|
1074 | else if ( (grids= getGrids(db,idProduit, p, z, dateDeb, dateFin) ) && grids->count) |
---|
1075 | { |
---|
1076 | fprintf (stderr,"dateDeb %s dateFin %s\n", dateDeb, dateFin); |
---|
1077 | register int i=0; |
---|
1078 | ptrGrid gdeb, gfin; |
---|
1079 | gdeb= get_list_head(grids); |
---|
1080 | gfin= get_list_tail(grids); |
---|
1081 | R->temporal= allocTemporal(gdeb->date, gfin->date); |
---|
1082 | R->paslat= pasLat(z, gdeb); |
---|
1083 | R->paslon= pasLon(z, gdeb); |
---|
1084 | |
---|
1085 | while (bkf[i].name) |
---|
1086 | { |
---|
1087 | if( !strcmp(bkf[i].name, function) ) |
---|
1088 | { |
---|
1089 | |
---|
1090 | double pMini= p->mini; |
---|
1091 | double pMaxi = p->maxi; |
---|
1092 | fprintf (stderr,"pMini %f pMaxi %f\n", pMini, pMaxi); |
---|
1093 | ptrParam pProvisoire =(ptrParam)malloc(sizeof(ptrParam)); |
---|
1094 | memcpy(&pProvisoire, &p, sizeof(ptrParam)); |
---|
1095 | |
---|
1096 | g= bkf[i].function(grids, args, R, p); |
---|
1097 | |
---|
1098 | if (!strcmp(bkf[i].name, "ecartCarte") || !strcmp(bkf[i].name, "moyenneCarte") || !strcmp(bkf[i].name, "minimumCarte") || !strcmp(bkf[i].name, "maximumCarte") || !strcmp(bkf[i].name, "sommeCarte") ) |
---|
1099 | { |
---|
1100 | p->mini = pMini; |
---|
1101 | p->maxi = pMaxi; |
---|
1102 | ///initialisation du centre |
---|
1103 | R->centerLon = ((z->lonMax + z->lonMin)/2); |
---|
1104 | R->centerLat = ((z->latMax + z->latMin)/2); |
---|
1105 | |
---|
1106 | ptrZone zCenter=(ptrZone)malloc(sizeof(Zone)); |
---|
1107 | zCenter->latMax= R->centerLat; |
---|
1108 | zCenter->latMin= R->centerLat; |
---|
1109 | zCenter->lonMax= R->centerLon; |
---|
1110 | zCenter->lonMin= R->centerLon; |
---|
1111 | |
---|
1112 | ptrResponse Rprovisoire= allocResponse((ptrZone)zCenter, (ptrTemporal)NULL); |
---|
1113 | fprintf (stderr,"<-------------------------------------------------------------------------------------------------------->" ); |
---|
1114 | LIST gridsProvisoire= getGrids(db,idProduit, p, zCenter, dateDeb, dateFin); |
---|
1115 | ptrGrid gProvisoire= bkf[i].function(gridsProvisoire, args, Rprovisoire, pProvisoire); |
---|
1116 | R->centerValue = int2v(gProvisoire->datas[0],p->mini,p->maxi); |
---|
1117 | p->mini = pMini; |
---|
1118 | p->maxi = pMaxi; |
---|
1119 | ///////////////////////////////////////////////// |
---|
1120 | fprintf (stderr,"centerValue %f\n",R->centerValue ); |
---|
1121 | fprintf (stderr,"centerLon %f\n",R->centerLon ); |
---|
1122 | fprintf (stderr,"centerLat %f\n",R->centerLat ); |
---|
1123 | } |
---|
1124 | |
---|
1125 | break; |
---|
1126 | } |
---|
1127 | else i++; |
---|
1128 | } |
---|
1129 | if(!bkf[i].name) |
---|
1130 | { |
---|
1131 | sprintf(buf," no found fonction %s", function); |
---|
1132 | } |
---|
1133 | freeGrids(grids); |
---|
1134 | sprintf(R->file, "%s/%d-%d-%d_%s-%s_%.1fn%.1fs%.1fo%.1fe_%s.png", RESULT_PATH, |
---|
1135 | idProduit, idParam, idColormap, |
---|
1136 | R->temporal->debut, R->temporal->fin, |
---|
1137 | z->latMin, z->latMax, z->lonMin,z->lonMax, function); |
---|
1138 | } |
---|
1139 | |
---|
1140 | |
---|
1141 | //fprintf(stderr, "end calcul now grid2img\n");///////////////////////////////////////////////////*/*****************///// |
---|
1142 | if( g) |
---|
1143 | { |
---|
1144 | |
---|
1145 | |
---|
1146 | |
---|
1147 | //utilisation de la colormap dynamique pour le calcul statistuqe |
---|
1148 | /*if (!strcmp("ecartCarte", function) || !strcmp("moyenneCarte", function) || !strcmp("sommeCarte", function)) |
---|
1149 | idColormap = -1;*/ |
---|
1150 | |
---|
1151 | // la fonction a rendu une grid on la transforme en image |
---|
1152 | gdImagePtr im; |
---|
1153 | //return "idColormap av = "+idColormap; |
---|
1154 | if( im= grid2img( db, g, p, idColormap)) |
---|
1155 | { |
---|
1156 | FILE *out; |
---|
1157 | |
---|
1158 | /*sprintf(R->file, "%s/%d-%d-%d_%s-%s_%.1fn%.1fs%.1fo%.1fe_%s.png", RESULT_PATH, |
---|
1159 | idProduit, idParam, idColormap, |
---|
1160 | "02001200", "00123300", |
---|
1161 | z->latMin, z->latMax, z->lonMin,z->lonMax, function);*/ |
---|
1162 | |
---|
1163 | fprintf(stderr, "end grid2img\n"); |
---|
1164 | if( out= fopen(R->file, "w")) |
---|
1165 | { |
---|
1166 | gdImagePng(im, out); |
---|
1167 | fclose(out); |
---|
1168 | } |
---|
1169 | else errorResponse(R,"Can'ot open output File"); |
---|
1170 | gdImageDestroy(im); |
---|
1171 | } |
---|
1172 | else errorResponse(R,"grid2img error"); |
---|
1173 | freeGrid (g); |
---|
1174 | } |
---|
1175 | //else la fonction ne rend pas de grille -> tout est dans reponse |
---|
1176 | /*else |
---|
1177 | { |
---|
1178 | sprintf(buf,"no found grids for product %d and param %s", |
---|
1179 | idProduit, p->name); |
---|
1180 | errorResponse(R, buf); |
---|
1181 | }*/ |
---|
1182 | } |
---|
1183 | else errorResponse(R," no found idParam"); |
---|
1184 | } |
---|
1185 | else errorReponse(R, " out of globalZone"); |
---|
1186 | PQfinish(db); |
---|
1187 | } |
---|
1188 | else errorResponse(R,"pb connect database"); |
---|
1189 | } |
---|
1190 | else errorResponse(R,"Illegal database connection parameters"); |
---|
1191 | } |
---|
1192 | |
---|
1193 | char * xmlRequest(char * request) |
---|
1194 | { |
---|
1195 | ptrResponse R= allocResponse((ptrZone)NULL, (ptrTemporal)NULL); |
---|
1196 | xmlDtdPtr dtd; |
---|
1197 | char buf[256]; |
---|
1198 | sprintf(buf, "%s/Requete.dtd", DATA_PATH); |
---|
1199 | |
---|
1200 | dtd = xmlParseDTD(NULL, buf); |
---|
1201 | if(dtd) |
---|
1202 | { |
---|
1203 | xmlDocPtr doc; // the resulting document tree |
---|
1204 | if( doc = xmlParseMemory(request, strlen(request))) |
---|
1205 | { |
---|
1206 | xmlValidCtxtPtr vCtxt= xmlNewValidCtxt(); |
---|
1207 | if( vCtxt ) |
---|
1208 | { |
---|
1209 | vCtxt->error = (xmlValidityErrorFunc) myError; |
---|
1210 | vCtxt->warning = (xmlValidityWarningFunc) myWarning; |
---|
1211 | if (!xmlValidateDtd(vCtxt, doc, dtd)) |
---|
1212 | { |
---|
1213 | sprintf(buf, |
---|
1214 | "Document does not validate against %s/Requete.dtd\n",DATA_PATH); |
---|
1215 | errorResponse(R, buf); |
---|
1216 | } |
---|
1217 | else |
---|
1218 | { |
---|
1219 | execXml( doc, R); |
---|
1220 | } |
---|
1221 | xmlFreeValidCtxt(vCtxt); |
---|
1222 | } |
---|
1223 | xmlFreeDoc(doc); |
---|
1224 | } |
---|
1225 | else |
---|
1226 | { |
---|
1227 | sprintf(buf, "Failed to parse request string (%s)", request); |
---|
1228 | errorResponse(R, buf); |
---|
1229 | } |
---|
1230 | } |
---|
1231 | else |
---|
1232 | { |
---|
1233 | sprintf(buf, "Parse dtd error %s/Requete.dtd\n",DATA_PATH); |
---|
1234 | errorResponse(R, buf); |
---|
1235 | } |
---|
1236 | // fprintf(stderr, "end execXML\n"); |
---|
1237 | |
---|
1238 | return response2xml(R); |
---|
1239 | } |
---|
1240 | |
---|