File: PowCreateGraph.c

package info (click to toggle)
ftools-fv 5.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,072 kB
  • sloc: tcl: 50,774; ansic: 17,858; exp: 2,004; makefile: 172; cpp: 169; sh: 109; csh: 10
file content (343 lines) | stat: -rw-r--r-- 10,218 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "pow.h"

void PowCreateGraph(char *graph_name, char *curves, char *images,
		    char *xunits, char *yunits, char *xlabel, char *ylabel,
		    int *xdimdisp, int *ydimdisp, double *xmin_in, 
		    double *ymin_in, double *xmax_in, double *ymax_in, 
		    int *status) {
char whichPowCanvas[9]=".pow.pow";

PowCreateGraph_internal(graph_name, curves, images, xunits, yunits,
			xlabel, ylabel, xdimdisp, ydimdisp, xmin_in, 
			ymin_in, xmax_in, ymax_in, whichPowCanvas, 
			status) ;

}




void PowCreateGraph_internal(char *graph_name, char *curves, char *images,
		    char *xunits, char *yunits, char *xlabel, char *ylabel,
		    int *xdimdisp, int *ydimdisp, double *xmin_in, 
		    double *ymin_in, double *xmax_in, double *ymax_in, 
		    char *whichPowCanvas, int *status) {
  /* xdimdisp and ydimdisp are the *displayed* size of the new graph it will be
     zoomed or shrunk by an integral or 1/integral factor to come as
     close as possible to filling this requested space. If xdimdisp and 
     ydimdisp are both 0, the graph will appear at Magstep 1 */

  PowGraph *graph_instance;
  Tcl_HashEntry *entry_ptr;
  int new = 0;
  double xmin,xmax,ymin,ymax,xdim,ydim,tmp;
  int xmargin,ymargin;
  double xoff, yoff;
  char bbox[128];
  char extraparams[256], *pPtr;
  char *str_ptr;
  char *aspect="no";
  int in_limits;
  int x_points_right,y_points_up;
  int len;
  int zoomed;
  char *idxStr;
  const char *graphType;
  int xCount, yCount;
  const char *WCSvalue;
  char errormsg[512];

  in_limits = 1;

  entry_ptr = Tcl_CreateHashEntry(&PowGraphTable, graph_name, &new);

  if ( new ) {
    graph_instance = (PowGraph *) ckalloc(sizeof(PowGraph));
    if(graph_instance == NULL) {
      *status = TCL_ERROR;
      Tcl_SetResult( interp, "Couldn't ckalloc graph structure space",
                     TCL_VOLATILE );
      Tcl_DeleteHashEntry(entry_ptr);
      return;
    }

    Tcl_SetHashValue( entry_ptr, graph_instance);

    /*  Copy graph_name into graph's structure  */

    str_ptr = ckalloc(strlen(graph_name)+1);
    strcpy(str_ptr,graph_name);
    graph_instance->graph_name = str_ptr;

  } else {

#ifdef DEBUG  
    printf("Reusing graph name: %s\n",graph_name);
#endif

    graph_instance = (PowGraph *) Tcl_GetHashValue( entry_ptr );

    /*  Free up old string pointers  */

    ckfree(graph_instance->xunits);
    ckfree(graph_instance->yunits);
    ckfree(graph_instance->xlabel);
    ckfree(graph_instance->ylabel);

  }

  if (xmin_in != NULL && xmax_in != NULL && *xmin_in > *xmax_in) {
     x_points_right = 0;
  } else {
     x_points_right = 1;
  }

  if (ymin_in != NULL && ymax_in != NULL && *ymin_in > *ymax_in) {
     y_points_up = 0;
  } else {
     y_points_up = 1;
  }

  graph_instance->WCS.haveWCSinfo = 0;

  PowWCSInitGraph( graph_instance, curves, images,
		   x_points_right, y_points_up);

/*
  FillinWCSStructure ( &graph_instance->WCS );
  image_instance = PowFindImage(images);
  FillinWCSStructure ( &image_instance->WCS );
*/

  /*  Do we need to keep a fixed Aspect ratio?  */

  if( graph_instance->WCS.type[0]
      || ( images != NULL && strstr(images,"NULL") == NULL ) )
      aspect = "yes";


  /*
   *   If any of the min/max values are not specified, search
   *   the graph's contents for its bounding box.
   */

  if( xmin_in==NULL || xmax_in==NULL || ymin_in==NULL || ymax_in==NULL ) {

     if( PowFindGraphBBox( graph_instance, images, curves,
                           &xmin, &xmax, &ymin, &ymax       ) != TCL_OK ) {
        *status = TCL_ERROR;
        Tcl_AppendResult( interp, "\nError locating curves' bounding boxes",
                          NULL );
        ckfree( (char *)graph_instance->graph_name );
        ckfree( (char *)graph_instance );
        Tcl_DeleteHashEntry(entry_ptr);
        return;
     }
  }

  /*  Now apply supplied bounding box values  */

  if (xmin_in != NULL) xmin = *xmin_in;
  if (xmax_in != NULL) xmax = *xmax_in;
  if (ymin_in != NULL) ymin = *ymin_in;
  if (ymax_in != NULL) ymax = *ymax_in;

  if (xmin == xmax) {
    if(xmin == 0) {
      xmax = 1;
    } else {
      xmin *= 0.9;
      xmax *= 1.1;
    }
  }

  if (ymin == ymax) {
    if (ymin == 0) {
      ymax = 1;
    } else {
      ymin *= 0.9;
      ymax *= 1.1;
    }
  }

  len    = strlen(graph_name)+15;
  idxStr = (char *) ckalloc( len*sizeof(char) );
  sprintf(idxStr, "%s,%s", "graphType", graph_name);
  graphType = Tcl_GetVar2(interp,"powPlotParam",idxStr,TCL_GLOBAL_ONLY);
  ckfree(idxStr);

  len    = strlen(graph_name)+15;
  idxStr = (char *) ckalloc( len*sizeof(char) );
  sprintf(idxStr, "%s,%s", "zoomed", graph_name);
  zoomed = atoi(Tcl_GetVar2(interp,"powPlotParam",idxStr,TCL_GLOBAL_ONLY));
  ckfree(idxStr);

  xCount = atoi(Tcl_GetVar2(interp,"xCount",graph_name,TCL_GLOBAL_ONLY));
  yCount = atoi(Tcl_GetVar2(interp,"yCount",graph_name,TCL_GLOBAL_ONLY));

  if ( graph_instance->WCS.type[0] == '\0' && strcmp(graphType, "binary") == 0 && xCount % 2 != 0 ) {
     graph_instance->WCS.cdFrwd[0][0] = 1.0;
  } 
  if ( graph_instance->WCS.type[0] == '\0' && strcmp(graphType, "binary") == 0 && yCount % 2 != 0 ) {
     graph_instance->WCS.cdFrwd[1][1] = 1.0;
  }

  if( PowSortGraphMinMax(graph_instance,&xmin,&xmax,&ymin,&ymax,&xdim,&ydim) ) {
     /*  Bounding box is invalid.  Force default bounding box.  */

     PowFindGraphBBox( graph_instance, images, curves, &xmin, &xmax, &ymin, &ymax );
     PowSortGraphMinMax(graph_instance,&xmin,&xmax,&ymin,&ymax,&xdim,&ydim);
  }

  WCSvalue = Tcl_GetVar(interp,"powWCSTranslation",TCL_GLOBAL_ONLY);

  if (WCSvalue[0] != '0') {
    sprintf(errormsg, "\nError translating WCS information. error:<%s>.", WCSvalue);
    *status = TCL_ERROR;
    Tcl_AppendResult( interp, errormsg, NULL );
    Tcl_DeleteHashEntry(entry_ptr);
    return;
  }

  /* Chai 06/29/2007:
      We are not actually fliping the coordinates on the canvas. If tk allows this, then there is
      no need to do the following. What the logic below is to trick pow to think that the point on
      the canvas has been flipped. The xCount and yCount indicate if the graph has been flipped
      before. So if X has been previously flipped, the next flipping occurs on Y, the logic inside
      ..Count % 2 will make sure the information on previous flip still maintained. */

  if ( graph_instance->WCS.type[0] == '\0' && strcmp(graphType, "binary") == 0 && xCount % 2 != 0 ) {
     /* previous flip */
     tmp = xmin;
     xmin = xmax;
     xmax = tmp;
  }

  if ( graph_instance->WCS.type[0] == '\0' && strcmp(graphType, "binary") == 0 && yCount % 2 != 0 ) {
     /* previous flip */
     tmp = ymin;
     ymin = ymax;
     ymax = tmp;
  }

  graph_instance->xleft  = xmin;
  graph_instance->xright = xmax;
  graph_instance->ybot   = ymin;
  graph_instance->ytop   = ymax;

  PowPosToPix( xmin, ymin, &graph_instance->WCS, &xoff, &yoff );
  graph_instance->WCS.refPix[0] -= xoff;
  graph_instance->WCS.refPix[1] -= yoff;

  graph_instance->xoff -= xoff;
  graph_instance->yoff -= yoff;

  str_ptr = ckalloc(strlen(xunits)+1);
  strncpy(str_ptr,xunits,strlen(xunits)+1);
  graph_instance->xunits = str_ptr;
  str_ptr = ckalloc(strlen(yunits)+1);
  strncpy(str_ptr,yunits,strlen(yunits)+1);
  graph_instance->yunits = str_ptr;
  str_ptr = ckalloc(strlen(xlabel)+1);
  strncpy(str_ptr,xlabel,strlen(xlabel)+1);
  graph_instance->xlabel = str_ptr;
  str_ptr = ckalloc(strlen(ylabel)+1);
  strncpy(str_ptr,ylabel,strlen(ylabel)+1);
  graph_instance->ylabel = str_ptr;

  
  sprintf(bbox," %#.17g %#.17g %#.17g %#.17g", graph_instance->xleft, 
	  graph_instance->xright, graph_instance->ybot, graph_instance->ytop);

  if( xdimdisp && *xdimdisp<=0 ) *xdimdisp = (int)xdim;
  if( ydimdisp && *ydimdisp<=0 ) *ydimdisp = (int)ydim;

  xmargin = 80;
  ymargin = 60;

  sprintf(extraparams," %#.17g %#.17g ", xdim, ydim );

  /*  Handle possible NULL value of dimdisp's  */

  pPtr = extraparams + strlen(extraparams);
  if( xdimdisp )
     sprintf(pPtr, "%d ", *xdimdisp);
  else
     sprintf(pPtr, "NULL ");
  pPtr += strlen( pPtr );

  if( ydimdisp )
     sprintf(pPtr, "%d ", *ydimdisp);
  else
     sprintf(pPtr, "NULL ");
  pPtr += strlen( pPtr );

  sprintf(pPtr, "%s %d %d ",aspect, xmargin, ymargin);

  if ( Tcl_VarEval(interp, "powInitGraph ", graph_name, bbox," {",
		   xunits,"} {", yunits,"} {",xlabel,"} {",ylabel,"} ",
		   whichPowCanvas, extraparams, (char *) NULL) == TCL_ERROR) {
    *status = TCL_ERROR;
    Tcl_AppendResult( interp, "\nError initializing graph.", NULL );
    Tcl_DeleteHashEntry(entry_ptr);
    return;
  };

  if( images==NULL ) images="NULL";
  if( curves==NULL ) curves="NULL";
  if ( Tcl_VarEval(interp, "powBuildGraph ", graph_name,
		   " [list ", images," ] ", " [list ", curves," ] ",
		   whichPowCanvas, (char *) NULL)
       == TCL_ERROR) {
     *status =  TCL_ERROR;
     Tcl_AppendResult( interp, "\nError building graph.", NULL );
     Tcl_DeleteHashEntry(entry_ptr);
     return;
  }

  if ( !strcmp( whichPowCanvas, ".pow.pow" ) ) {
     if ( Tcl_VarEval(interp, "powSelectGraph ", graph_name, (char *) NULL)
	  == TCL_ERROR) {
	*status =  TCL_ERROR;
        Tcl_AppendResult( interp, "\nError selecting graph.", NULL );
	Tcl_DeleteHashEntry(entry_ptr);
	return;
     }
  }
}



void PowDestroyGraph(char *graph_name, int *status) {
  Tcl_HashEntry *entry_ptr;
  char errormsg[1024];
  PowGraph *graph_ptr;
  
  entry_ptr = Tcl_FindHashEntry(&PowGraphTable,graph_name);
  
  if (entry_ptr == NULL) {
    *status = TCL_ERROR;
    sprintf(errormsg,"Can't find POWGraph Object %s to destroy",graph_name);
    Tcl_SetResult(interp,errormsg,TCL_VOLATILE);
    return;
  }

  Tcl_VarEval(interp,"powUnmapGraph ",graph_name,(char *)NULL);
  Tcl_VarEval(interp,"powFreeGraph ", graph_name,(char *)NULL);

  graph_ptr = (PowGraph *)Tcl_GetHashValue(entry_ptr);

  /*Delete the entry from the master POWData Hash*/
  Tcl_DeleteHashEntry(entry_ptr);
  
  /*free the PowGraph memory itself and the string holding the name and labels,
     although this is small change*/
  ckfree(graph_ptr->graph_name);
  ckfree(graph_ptr->xunits);
  ckfree(graph_ptr->yunits);
  ckfree(graph_ptr->xlabel);
  ckfree(graph_ptr->ylabel);
  ckfree((char*)graph_ptr);

  return;
}