Debian Codegraph @

gui/XIdent/src/spgrap.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 .IDENT amain.c
30 .MODULE subroutines -- gmidas.exe
31 .LANGUAGE C
32 .AUTHOR Cristian Levin - ESO La Silla
33 .PURPOSE The functions of this module implement the
34 applications of the Alice package and the
35 spectrum manipulation functions.
36 .KEYWORDS spectrum plotting, line integration.
37 .ENVIRONMENT UNIX
38 .VERSION 1.0 1-Mar-1991 Implementation
39
40 090422 last modif
41 ------------------------------------------------------------*/
42
43 #include <stdio.h>
44 #include <string.h>
45
46 #include <gl_defs.h> /* local headers */
47 #include <agl_defs.h> /* local headers */
48 #include <main_defs.h>
49 #include <spec_defs.h>
50
51 #include <fileexts.h> /* midas headers */
52
53 #include <aglerror.h>
54
55 #define FMT_TITLE "File: %s Line: %s Image: %s"
56 #define FMT_OPTIONS "TITLE=%s;LABX=Position;LABY=Pixel value"
57
58 #define ORDMAX 9999999.0
59 #define ORDMIN -9999999.0
60
61 #define NEXT_SPEC 0
62 #define PREV_SPEC 1
63 #define FORM_SPEC 2
64
65 #define MIN_DIF_CUTS 0.01
66
67 /* Definitions for printing a plot */
68 #define CURRFILE "current.plt"
69
70 void init_gmidas ();
71 void end_gmidas ();
72 void end_graphic ();
73 void get_agldev ();
74 void free_fvector();
75 int graphwin_exists();
76 int file_exists();
77
78
79 int Areadim ();
80 void Agetcur ();
81 int Acutx ();
82 int Acuty ();
83 void Aunzoom ();
84 void Ahelp ();
85
86 void open_plotfile ();
87 void close_plotfile ();
88
89 void spec_copy ();
90 void get_image_name ();
91
92 void redraw_spectrum ();
93
94 void define_viewport ();
95 void set_viewport ();
96 static float vwx_min, vwy_min, vwx_max, vwy_max;
97 static float cwx_min, cwy_min, cwx_max, cwy_max;
98
99 float *fvector (), **fmatrix ();
100 double *dvector (), **dmatrix ();
101 int *ivector ();
102 double eval_dpoly ();
103 double exp (), sqrt ();
104
105 static SPEC *Sp[MAXSTACK]; /* stack of spectra */
106 static SPEC *Spcur; /* current spectrum */
107 static int Top = 0; /* index to top of stack */
108 static char DevErase[80], DevNoErase[80]; /* devices for AG_VDEF */
109 static int InitGraphic = FALSE;
110 static int XLimDefined = FALSE;
111 static double XLimits[2];
112
113 /******************************************************
114 * init_gmidas(): initialize Midas & Alice structures.
115 ******************************************************/
116 void init_gmidas (progname)
117 char *progname;
118 {
119 int i;
120
121 SCSPRO (progname);
122
123 for (i = 0; i < MAXSTACK; i++) /* initialize stack */
124 Sp[i] = (SPEC *) osmmget (sizeof (SPEC));
125 Spcur = (SPEC *) osmmget (sizeof (SPEC));
126
127 TCMNUL (&Inull, &Rnull, &Dnull); /* NULL values */
128 }
129
130 int exists_graphic ()
131 {
132 if (!graphwin_exists ())
133 {
134 SCTPUT ("*** Please create the graphic window ***");
135 return (FALSE);
136 }
137 return TRUE;
138 }
139
140 int init_graphic (devtype)
141 int devtype;
142 {
143 if (InitGraphic)
144 return (TRUE);
145
146 if (!graphwin_exists ())
147 {
148 SCTPUT ("*** Please create the graphic window ***");
149 return (FALSE);
150 }
151
152 InitGraphic = TRUE;
153 get_agldev ();
154 switch (devtype)
155 {
156 case DEV_ERASE:
157 AG_VDEF (DevErase, 0.05, 1.0, 0.0, 1.0, 0.0, 0.0);
158 break;
159 case DEV_NO_ERASE:
160 AG_VDEF (DevNoErase, 0.05, 1.0, 0.0, 1.0, 0.0, 0.0);
161 break;
162 }
163 AG_SSET ("FONT=1");
164
165 return (TRUE);
166 }
167
168 void end_graphic ()
169 {
170 if (InitGraphic && graphwin_exists ())
171 AG_CLS ();
172
173 InitGraphic = FALSE;
174 }
175
176 void open_plotfile ()
177 {
178 AG_MOPN (CURRFILE);
179 }
180
181 void close_plotfile ()
182 {
183 AG_MCLS ();
184 }
185
186 void clear_graphic ()
187 {
188 if (InitGraphic && graphwin_exists ())
189 {
190 AG_VERS ();
191 AG_VUPD ();
192 }
193 }
194
195 /******************************************************
196 * end_gmidas(): end Midas & Alice.
197 ******************************************************/
198 void end_gmidas ()
199 {
200 int i;
201
202 for (i = 0; i < MAXSTACK; i++) /* deallocate stack */
203 osmmfree (Sp[i]);
204 osmmfree (Spcur);
205
206 SCSEPI ();
207 }
208
209 /********************************************************************
210 * Areadim(): reads the 'row'nth line plus 'ahead' lines ahead
211 * of 'image_name' and put the average onto the stack.
212 ********************************************************************/
213 int Areadim (image_name, row, ahead, save_lims)
214 char *image_name;
215 int row; /* row number to read */
216 int ahead; /* ahead rows to read */
217 int save_lims; /* TRUE if should save limits */
218 {
219 int i, j;
220 char ident[MAXIDENT + 1];
221 int ncols, nrows, npix[2], naxis;
222 double start, step;
223 float cuts[4];
224 float *group, sum;
225 int framid;
226 int unit;
227 int nulval, retval; /* garbage */
228 double fabs ();
229
230 if (!file_exists (image_name, ".bdf"))
231 return (FALSE);
232
233 SCFOPN (image_name, D_R4_FORMAT, 0, F_IMA_TYPE, &framid);
234
235 SCDRDI (framid, "NPIX", 1, 2, &retval, npix, &unit, &nulval);
236 SCDRDI (framid, "NAXIS", 1, 1, &retval, &naxis, &unit, &nulval);
237 ncols = npix[0];
238 nrows = (naxis == 1) ? 1 : npix[1];
239
240 if (row + ahead > nrows || row + ahead < 1)
241 {
242 row = 1;
243 ahead = 0;
244 }
245
246 SCDRDR (framid, "LHCUTS", 1, 4, &retval, cuts, &unit, &nulval);
247 SCDRDD (framid, "START", 1, 1, &retval, &start, &unit, &nulval);
248 SCDRDD (framid, "STEP", 1, 1, &retval, &step, &unit, &nulval);
249 SCDGETC (framid, "IDENT", 1, MAXIDENT, &retval, ident);
250
251 /* mapping and averaging of "row + ahead" rows */
252 group = fvector (0, (ahead + 1) * ncols - 1);
253 SCFGET (framid, (row - 1) * ncols + 1, (ahead + 1) * ncols, &retval,
254 (char *) group);
255
256 for (i = 0; i < ncols; i++)
257 {
258 sum = 0;
259 for (j = 0; j <= ahead; j++)
260 sum += group[i + j * ncols];
261 Sp[Top]->y[i] = sum / (ahead + 1);
262 }
263 free_fvector (group, 0, (ahead + 1) * ncols - 1);
264
265 for (i = 0; i < ncols; i++)
266 Sp[Top]->x[i] = start + step * i;
267
268
269 if (XLimDefined == FALSE)
270 {
271 Sp[Top]->xmin = start;
272 Sp[Top]->xmax = start + step * (ncols - 1);
273 XLimits[0] = start;
274 XLimits[1] = start + step * (ncols - 1);
275 }
276 else
277 {
278 Sp[Top]->xmin = Spcur->xmin;
279 Sp[Top]->xmax = Spcur->xmax;
280 }
281
282 if (fabs (cuts[0] - cuts[1]) > MIN_DIF_CUTS)
283 {
284 Sp[Top]->ymin = cuts[0];
285 Sp[Top]->ymax = cuts[1];
286 }
287 else if (fabs (cuts[2] - cuts[3]) > MIN_DIF_CUTS)
288 {
289 Sp[Top]->ymin = cuts[2];
290 Sp[Top]->ymax = cuts[3];
291 }
292 else
293 { /* Max. and min. cuts are not assigned */
294 Sp[Top]->ymin = ORDMAX;
295 Sp[Top]->ymax = ORDMIN;
296 for (i = 0; i < ncols; i++)
297 {
298 if (Sp[Top]->y[i] > Sp[Top]->ymax)
299 Sp[Top]->ymax = Sp[Top]->y[i];
300 if (Sp[Top]->y[i] < Sp[Top]->ymin)
301 Sp[Top]->ymin = Sp[Top]->y[i];
302 }
303 cuts[0] = cuts[1] = 0.0;
304 cuts[2] = Sp[Top]->ymin;
305 cuts[3] = Sp[Top]->ymax;
306 SCDWRR (framid, "LHCUTS", cuts, 1, 4, &unit);
307 }
308 SCFCLO (framid);
309
310 strcpy (Sp[Top]->name, image_name);
311 strcpy (Sp[Top]->ident, ident);
312 get_image_name (Sp[Top]->name);
313
314 Sp[Top]->ncols = ncols;
315 Sp[Top]->nrows = nrows;
316 Sp[Top]->start = start;
317 Sp[Top]->step = step;
318 Sp[Top]->row = row;
319 Sp[Top]->ahead = ahead;
320
321 spec_copy (Spcur, Sp[Top]);
322
323 init_graphic (DEV_ERASE);
324 redraw_spectrum ();
325 end_graphic ();
326
327 return (TRUE);
328 }
329
330 void Alabelx (xvals, yvals, num_xvals, color)
331 float *xvals, *yvals;
332 int num_xvals;
333 int color;
334
335 {
336 float zeros[MAXDATA];
337 float wlimits[4]; /* window limits */
338 float x[2], y[2];
339
340 int i;
341
342 char coltext[20];
343
344
345 init_graphic (DEV_NO_ERASE);
346 set_viewport ();
347
348 AG_SSET (LARGE_SYM);
349
350 (void) sprintf(coltext,"color=%1d",color);
351 AG_SSET (coltext);
352 AG_RGET ("WNDL", wlimits);
353
354 for (i = 0; i < num_xvals; i++)
355 zeros[i] = wlimits[YMIN];
356
357 /* first, plot a small tick, enough to see the identified line */
358 AG_GPLM (xvals, zeros, num_xvals, VBAR_MARKER);
359
360 /* now, plot a line as high as the intensity value is, for each
361 identified line */
362 for (i = 0; i < num_xvals; i++)
363 {
364 x[0] = x[1] = xvals[i];
365 y[0] = zeros[i];
366 y[1] = yvals[i];
367 AG_GPLL (x, y, 2);
368 }
369 AG_SSET (DEF_SYM);
370 AG_SSET (DEF_COLOR);
371
372 end_graphic ();
373 }
374
375 void Agetcur ()
376 {
377 float cpx, cpy;
378 int key, valpix;
379 char msg[30];
380
381 init_graphic (DEV_NO_ERASE);
382 set_viewport ();
383
384 cpx = Spcur->xmin;
385 cpy = Spcur->ymin;
386
387 SCTPUT (" ");
388 SCTPUT (" X-axis Y-axis");
389 SCTPUT ("--------------------------");
390 while (1)
391 { /* forever */
392 AG_VLOC (&cpx, &cpy, &key, &valpix);
393 if (key == MID_BUT)
394 break;
395 sprintf (msg, "%10.2f %10.2f", cpx, cpy);
396 SCTPUT (msg);
397 }
398 end_graphic ();
399 }
400
401 int Acutx ()
402 {
403 float cpx[2], cpy[2];
404 int i, key, valpix;
405
406 init_graphic (DEV_NO_ERASE);
407 set_viewport ();
408
409 cpx[0] = Spcur->xmin;
410 cpy[0] = Spcur->ymin;
411
412 /* get cursor position */
413 for (i = 0; i < 2; i++)
414 {
415 AG_VLOC (cpx + i, cpy + i, &key, &valpix);
416 if (key == MID_BUT)
417 {
418 end_graphic ();
419 return (MID_BUT);
420 }
421 }
422
423 if (cpx[0] < cpx[1])
424 {
425 Spcur->xmin = cpx[0];
426 Spcur->xmax = cpx[1];
427 }
428 else
429 {
430 Spcur->xmin = cpx[1];
431 Spcur->xmax = cpx[0];
432 }
433 redraw_spectrum ();
434 end_graphic ();
435
436 XLimDefined = TRUE;
437
438 return (key);
439 }
440
441 int Acuty ()
442 {
443 float cpx[2], cpy[2], cuts[2];
444 int i, key, valpix, id;
445 int unit;
446
447 init_graphic (DEV_NO_ERASE);
448 set_viewport ();
449
450 cpx[0] = Spcur->xmin;
451 cpy[0] = Spcur->ymin;
452
453 /* get cursor position */
454 for (i = 0; i < 2; i++)
455 {
456 AG_VLOC (cpx + i, cpy + i, &key, &valpix);
457 if (key == MID_BUT)
458 {
459 end_graphic ();
460 return (MID_BUT);
461 }
462 }
463
464 if (cpy[0] < cpy[1])
465 {
466 Spcur->ymin = cpy[0];
467 Spcur->ymax = cpy[1];
468 }
469 else
470 {
471 Spcur->ymin = cpy[1];
472 Spcur->ymax = cpy[0];
473 }
474 redraw_spectrum ();
475 end_graphic ();
476
477 cuts[0] = Spcur->ymin;
478 cuts[1] = Spcur->ymax;
479 SCFOPN (Spcur->name, D_R4_FORMAT, 0, F_IMA_TYPE, &id);
480 SCDWRR (id, "LHCUTS", cuts, 1, 2, &unit);
481 SCFCLO (id);
482
483 return (key);
484 }
485
486 int Ashift ()
487 {
488 float cpx, cpy;
489 int key, valpix, middle;
490
491 init_graphic (DEV_NO_ERASE);
492 set_viewport ();
493
494 cpx = Spcur->xmin;
495 cpy = Spcur->ymin;
496
497 /* get cursor position */
498 AG_VLOC (&cpx, &cpy, &key, &valpix);
499 if (key == MID_BUT)
500 {
501 end_graphic ();
502 return (MID_BUT);
503 }
504
505 middle = (Spcur->xmax - Spcur->xmin) / 2.0;
506 if (cpx + middle > Sp[Top]->xmax)
507 {
508 Spcur->xmin = Spcur->xmin + Sp[Top]->xmax - Spcur->xmax;
509 Spcur->xmax = Sp[Top]->xmax;
510 }
511 else if (cpx - middle < Sp[Top]->xmin)
512 {
513 Spcur->xmin = Sp[Top]->xmin;
514 Spcur->xmax = Spcur->xmax + Sp[Top]->xmin - Spcur->xmin;
515 }
516 else
517 {
518 Spcur->xmin = cpx - middle;
519 Spcur->xmax = cpx + middle;
520 }
521 redraw_spectrum ();
522 end_graphic ();
523
524 return (key);
525 }
526
527 void Aunzoom ()
528 {
529 int unit;
530 int retval, nulval, id;
531 float cuts[2];
532
533 init_graphic (DEV_NO_ERASE);
534 set_viewport ();
535
536 spec_copy (Spcur, Sp[Top]);
537
538 SCFOPN (Spcur->name, D_R4_FORMAT, 0, F_IMA_TYPE, &id);
539 SCDRDR (id, "LHCUTS", 3, 2, &retval, cuts, &unit, &nulval);
540 SCDWRR (id, "LHCUTS", cuts, 1, 2, &unit);
541 SCFCLO (id);
542
543 Spcur->xmin = XLimits[0];
544 Spcur->xmax = XLimits[1];
545
546 Spcur->ymin = cuts[0];
547 Spcur->ymax = cuts[1];
548
549 redraw_spectrum ();
550 end_graphic ();
551 }
552
553 void Ahelp ()
554 {
555 }
556
557 /******************************************************
558 * spec_copy()
559 ******************************************************/
560 void spec_copy (sp_to, sp_from)
561 SPEC *sp_to, *sp_from;
562 {
563 int i;
564
565 sp_to->ncols = sp_from->ncols;
566 sp_to->nrows = sp_from->nrows;
567 sp_to->start = sp_from->start;
568 sp_to->step = sp_from->step;
569
570 sp_to->xmin = sp_from->xmin;
571 sp_to->xmax = sp_from->xmax;
572 sp_to->ymin = sp_from->ymin;
573 sp_to->ymax = sp_from->ymax;
574
575 sp_to->row = sp_from->row;
576 sp_to->ahead = sp_from->ahead;
577
578 for (i = 0; i < sp_from->ncols; i++)
579 {
580 sp_to->x[i] = sp_from->x[i];
581 sp_to->y[i] = sp_from->y[i];
582 }
583 strcpy (sp_to->name, sp_from->name);
584 strcpy (sp_to->ident, sp_from->ident);
585 }
586
587 /*************************************************************
588 * get_image_name()
589 *************************************************************/
590 void get_image_name (c)
591 char c[];
592 {
593 int i;
594
595 /* strip root of the filename */
596 for (i = strlen (c); i > 0; i--)
597 if (c[i] == '/')
598 {
599 strcpy (c, c + i + 1);
600 break;
601 }
602
603 /* strip suffix */
604 for (i = strlen (c); i > 0; i--)
605 if (c[i] == '.')
606 {
607 c[i] = '\0';
608 return;
609 }
610 }
611
612 /*********************************************************
613 * get_agldev(): translate IDI device to devices erasable
614 * and non-erasable suitable for AG_VDEF calls.
615 *********************************************************/
616 void get_agldev ()
617 {
618 char device[21]; /* name of assoc. device as in pltdevices.dat */
619
620 /* read & translate type of device to a specific device name */
621 /* SCKGETC( "MID$PLOT", 1, 20, &actval, devkeyw );
622 get_dev( devkeyw, device ); Not by now... */
623 strcpy (device, "GRAPH_WND0");
624
625 /* now make the AGL device names */
626 strcpy (DevErase, device);
627 strcat (DevErase, ":"); /* see AG_VDEF definition */
628
629 strcpy (DevNoErase, device);
630 strcat (DevNoErase, "/n:"); /* see AG_VDEF definition */
631 }
632
633 void define_viewport ()
634 {
635 float vals[4];
636
637 AG_RGET ("WNDL", vals);
638 vwx_min = vals[0];
639 vwx_max = vals[1];
640 vwy_min = vals[2];
641 vwy_max = vals[3];
642 AG_RGET ("CLPL", vals);
643 cwx_min = vals[0];
644 cwx_max = vals[1];
645 cwy_min = vals[2];
646 cwy_max = vals[3];
647 }
648
649 void set_viewport ()
650 {
651 AG_CDEF (cwx_min, cwx_max, cwy_min, cwy_max);
652 AG_WDEF (vwx_min, vwx_max, vwy_min, vwy_max);
653 }
654
655 /***************************************************************
656 * redraw_spectrum(): redraw the current spectrum of the stack.
657 ***************************************************************/
658 void redraw_spectrum ()
659 {
660 char options[512], title[512], interval[512];
661 char small_ident[11];
662
663 if (Spcur->ahead == 0)
664 sprintf (interval, "%d", Spcur->row);
665 else
666 sprintf (interval, "%1d-%1d", Spcur->row, Spcur->row + Spcur->ahead);
667
668 strncpy (small_ident, Spcur->ident, 10);
669 small_ident[10] = '\0';
670 sprintf (title, FMT_TITLE, Spcur->name, interval, small_ident);
671 sprintf (options, FMT_OPTIONS, title);
672
673 clear_graphic ();
674 open_plotfile ();
675
676 AG_AXES (Spcur->xmin, Spcur->xmax, Spcur->ymin, Spcur->ymax, options);
677 define_viewport ();
678
679 AG_GPLL (Spcur->x, Spcur->y, Spcur->ncols);
680
681 close_plotfile ();
682 }