Debian Codegraph @

gui/XIdent/src/applic.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 gspec.c
30 .MODULE subroutines -- gmidas.exe
31 .LANGUAGE C
32 .AUTHOR Cristian Levin - ESO La Silla
33 .PURPOSE This routines implement the application part of
34 the Spectra context.
35 .KEYWORDS application routines, Midas related routines.
36 .VERSION 1.0 1-Mar-1991 Implementation
37
38 090423 last modif
39 ------------------------------------------------------------*/
40
41 #include <stdio.h>
42 #include <math.h>
43
44 #include <xm_defs.h>
45 #include <gl_defs.h>
46 #include <agl_defs.h>
47 #include <main_defs.h>
48 #include <spec_defs.h>
49 #include <UxLib.h>
50 #include <midas_def.h>
51 #include <osparms.h>
52
53 #define LINE_ADD_TABLE "lineadd"
54
55 /*******************
56 Local variables
57 *******************/
58 static LCTAB * Lc = NULL; /* Line catalog table */
59 static float * LinePos; /* array of positions of LINE.tbl */
60 static float * LinePeak; /* array of intensities of LINE.tbl */
61 static float * Ident; /* array of :IDENT values of LINE.tbl */
62 static int * LineRow; /* row numbers. of each element of LinePos */
63 static int NumLinePos = 0; /* LinePos cardinality */
64
65 static char * List[MAXTEXT];
66 static int LincatAllocated = FALSE;
67 static char LineTable[MAXLINE];
68
69 /**********************************
70 application external variables
71 **********************************/
72 extern char Wlc[], /* WLC name */
73 WlcIdent[], /* WLC identification */
74 Lincat[]; /* Line catalog name */
75
76 extern float LincatPos[]; /* :WAVE in 'Lincat' pointed by the user */
77 extern float WlcPos[]; /* :X values in LINE matching LincatPos[] */
78 extern int NumLincatPos; /* cardinality of LincatPos[] */
79 extern int NumWlcPos; /* cardinality of WlcPos[] */
80 extern int IdentBegin;
81
82 float *fvector();
83 int *ivector();
84 char **cmatrix();
85 int file_exists(), read_catalog_table();
86 int Areadim();
87 int Acutx(), Acuty(), Ashift();
88
89 void Alabelx(), Agetcur(),Aunzoom();
90
91 void free_fvector();
92 void free_ivector();
93 void free_cmatrix();
94
95 /*******************
96 local functions
97 *******************/
98 void read_params_long();
99 void ident_spec();
100 void label_spec();
101 void free_lincat();
102 void label_spectrum();
103 void init_graphic();
104 void set_viewport();
105 void end_graphic();
106 void redraw_spectrum();
107
108
109
110
111 void read_params_long()
112 {
113 int actval; /* actual values returned */
114 int unit; /* useless */
115 int nulval; /* useless */
116
117 SCKGETC("LINTAB", 1, 20, &actval, LineTable);
118 SCKGETC("WLC", 1, 20, &actval, Wlc);
119 SCKGETC("LINCAT", 1, 20, &actval, Lincat);
120 SCKRDI("WRANG", 1, 2, &actval, Wrang, &unit, &nulval);
121 SCKRDR("IMIN", 1, 1, &actval, &Imin, &unit, &nulval);
122 SCKRDI("YSTART", 1, 1, &actval, &Ystart, &unit, &nulval);
123 SCKRDI("NPIX", 1, 2, &actval, Npix, &unit, &nulval);
124 }
125
126 /*****************************************************************
127 * read_lincat_table(): reads line catalog named by 'Lincat' and
128 * stores the values in the 'Lc' structure, under the
129 * restrictions Wrang & Imin.
130 *****************************************************************/
131 int read_lincat_table()
132 {
133 if ( ! file_exists( Lincat, ".tbl" ) ) {
134 SCTPUT( "*** Line catalogue doesn't exist ***" );
135 return(FALSE);
136 }
137
138 if ( Lc != NULL ) { /* deallocate space */
139 free_fvector(Lc->wave, 0, Lc->nrows - 1);
140 free_fvector(Lc->intens, 0, Lc->nrows - 1);
141 free_cmatrix(Lc->ion, 0, Lc->nrows - 1, 0, MAXION - 1);
142 osmmfree((char *)Lc);
143 }
144 Lc = (LCTAB *)osmmget( sizeof(LCTAB) );
145
146 /* call to a library routine */
147 if( !read_catalog_table( Lc, Lincat, Wrang, Imin ) ) {
148 Lc = NULL;
149 return FALSE;
150 }
151 return TRUE;
152 }
153
154 /***************************************************************
155 * read_line_x(): reads columns :X and :PEAK of LINCAT table,
156 * OUT: float LinePos[] = array of X values.
157 * float LineRow[] = indexes to the rows in the table.
158 * int NumLinePos = no. of elements of LinePos.
159 */
160 int read_line_x( ident_step )
161 int ident_step; /* IDBEGIN ==> Identifications are cleared */
162 /* IDCONT ==> Identifications are read */
163 {
164 int nulval, sortcol, aw, ar, ncols;
165 int i, id, colid, colx, colpeak, colerased, selected;
166 int lines_search = FALSE;
167 char val_ok[2];
168 static int nrows;
169
170 if ( ! file_exists( LineTable, ".tbl" ) )
171 return(FALSE);
172
173 if ( NumLinePos > 0 ) {
174 free_fvector(LinePos, 0, nrows - 1);
175 free_fvector(LinePeak, 0, nrows - 1);
176 free_fvector(Ident, 0, nrows - 1);
177 free_ivector(LineRow, 0, nrows - 1);
178 }
179
180 sprintf(val_ok, "%c", VAL_OK);
181 NumLinePos = 0;
182
183 TCTOPN( LineTable, F_IO_MODE, &id );
184
185 TCIGET( id, &ncols, &nrows, &sortcol, &aw, &ar );
186
187 TCCSER( id, ":X", &colx );
188 TCCSER( id, ":PEAK", &colpeak );
189 TCCSER( id, ":ERASED", &colerased );
190
191 TCCSER( id, ":IDENT", &colid );
192 if ( colid == -1 )
193 TCCINI( id, D_R4_FORMAT, 1, "F10.3", "ANGSTROM", "IDENT", &colid );
194
195
196 /* allocate space for data */
197 LinePos = fvector(0, nrows - 1);
198 LinePeak = fvector(0, nrows - 1);
199 Ident = fvector(0, nrows - 1);
200 LineRow = ivector(0, nrows - 1);
201
202 for ( i = 1; i <= nrows; i++ ) {
203 TCSGET(id, i, &selected);
204 if ( selected ) {
205 lines_search = TRUE;
206 TCERDR( id, i, colx, &LinePos[NumLinePos], &nulval );
207 TCERDR( id, i, colpeak, &LinePeak[NumLinePos], &nulval );
208 Ident[NumLinePos] = Rnull;
209 if ( ident_step == IDBEGIN ) { /* :IDENT <-- NULL */
210 TCEWRR( id, i, colid, &Rnull );
211 if ( colerased != -1 )
212 TCEWRC( id, i, colerased, val_ok );
213 }
214 else
215 TCERDR( id, i, colid, &Ident[NumLinePos], &nulval );
216 LineRow[NumLinePos] = i;
217 NumLinePos++;
218 }
219 }
220 TCTCLO( id );
221
222 if ( !lines_search ) {
223 SCTPUT( "*** YSTART row doesn't have lines searched ***" );
224 return(FALSE);
225 }
226 return(TRUE);
227 }
228
229 int update_line_table( action )
230 int action;
231 {
232 char erased;
233 char invalid_value = VAL_INVALID,
234 valid_value = VAL_OK;
235 int nulval, sortcol, aw, ar, ncols, nrows;
236 int i, id, col;
237
238 if ( ! file_exists( LineTable, ".tbl" ) ) {
239 SCTPUT( "*** Lines have not been searched ***" );
240 return(FALSE);
241 }
242
243 if ( TCTOPN( LineTable, F_IO_MODE, &id ) != 0 ) {
244 SCTPUT( "Table LINE couldn't be opened." );
245 return(FALSE);
246 }
247 TCCSER( id, ":ERASED", &col );
248 if ( col == -1 ) /* nothing to update */
249 return(TRUE);
250
251 TCIGET( id, &ncols, &nrows, &sortcol, &aw, &ar );
252 for ( i = 0; i < nrows; i++ ) {
253 erased = VAL_OK;
254 TCERDC( id, i+1, col, &erased, &nulval );
255 if ( erased == VAL_ERASED && action == DELMARK )
256 TCEWRC( id, i+1, col, &invalid_value );
257 else if ( erased != VAL_OK && action == ADDMARK )
258 TCEWRC( id, i+1, col, &valid_value );
259 }
260 TCTCLO(id);
261 return(TRUE);
262 }
263
264 void display_lincat_table( wlist )
265 Widget wlist;
266 {
267 int i;
268 XmStringTable str_list;
269
270 if ( LincatAllocated )
271 free_lincat();
272 LincatAllocated = TRUE;
273
274 for ( i = 0; i < Lc->nrows; i++ )
275 List[i] = (char *)osmmget( 80 );
276 List[Lc->nrows] = NULL;
277
278 for ( i = 0; i < Lc->nrows; i++ )
279 sprintf( List[i], " %8.2f %10.5g %10s", Lc->wave[i],
280 Lc->intens[i], Lc->ion[i] );
281
282 str_list = (XmStringTable)XtMalloc(Lc->nrows * sizeof(XmString *));
283 for ( i = 0; i < Lc->nrows; i++ )
284 str_list[i] = XmStringCreateSimple(List[i]);
285
286 XmListSetPos(wlist, 1);
287 XmListDeleteAllItems(wlist);
288 XmListAddItems(wlist, str_list, Lc->nrows, 1);
289
290 for ( i = 0; i < Lc->nrows; i++ )
291 XmStringFree(str_list[i]);
292 XtFree((char *)str_list);
293 }
294
295 void free_lincat()
296 {
297 int i;
298
299 for ( i = 0; i < Lc->nrows; i++ )
300 osmmfree( List[i] );
301 }
302
303
304 /*****************************************************************
305 plot_spec()
306 description:
307 plot the starting line (Ystart) of the calibration
308 image (Wlc). If label = LABEL_SPEC, it plots additionally the
309 lines found and the previous identifications.
310 use:
311 line identification.
312 line adding for directed search.
313 */
314 int plot_spec( label )
315 int label;
316 {
317 int unit; /* useless */
318 int nulval, actval; /* useless */
319 int id;
320 int naxis;
321
322 if ( ! file_exists( Wlc, ".bdf" ) ) {
323 SCTPUT( "*** Calibration image invalid ***" );
324 return(FALSE);
325 }
326
327 SCFOPN( Wlc, D_R4_FORMAT, 0, F_IMA_TYPE, &id );
328 SCDRDI( id, "NAXIS", 1, 1, &actval, &naxis, &unit, &nulval );
329 SCDRDI( id, "NPIX", 1, 2, &actval, Npix, &unit, &nulval );
330 SCFCLO(id);
331 if ( Ystart > Npix[1] && naxis > 1 ) {
332 SCTPUT( "*** Starting line out of bound ***" );
333 return(FALSE);
334 }
335
336 Areadim(Wlc, Ystart, 0, FALSE);
337
338 if ( label == LABEL_SPEC )
339 label_spectrum();
340
341 return(TRUE);
342 }
343
344 void get_wlc_value()
345 {
346 float cpx = 0.0, cpy = 0.0;
347 int key, valpix;
348
349 if ( NumWlcPos > NumLincatPos ) /* clicking twice in graph. win */
350 NumWlcPos = NumLincatPos;
351
352 XtSetSensitive((Widget) UxGetWidget(UxFindSwidget("menu3_p2")),FALSE);
353
354 init_graphic(DEV_NO_ERASE);
355 set_viewport();
356 AG_VLOC( &cpx, &cpy, &key, &valpix );
357 end_graphic();
358
359 if ( key == LEFT_BUT )
360 WlcPos[NumWlcPos++] = cpx;
361 else
362 XtSetSensitive((Widget) UxGetWidget(UxFindSwidget("menu3_p2")),TRUE);
363
364 }
365
366 void delete_idents()
367 {
368 float curr_delta, delta, cpx = 0.0, cpy = 0.0;
369 float wlimits[4], x[2], y[2];
370 int i, id, colid, linepos, key, valpix;
371 char str[MAXLINE];
372
373 if ( TCTOPN( LineTable, F_IO_MODE, &id ) != 0 ) {
374 SCTPUT( "Line table could not be opened." );
375 return;
376 }
377 TCCSER( id, ":IDENT", &colid );
378
379 linepos = -1;
380 init_graphic(DEV_NO_ERASE);
381 set_viewport();
382 AG_SSET( BLUE_COLOR );
383 AG_RGET( "WNDL", wlimits );
384 y[0] = wlimits[YMIN];
385
386 while ( 1 ) { /* forever */
387 AG_VLOC( &cpx, &cpy, &key, &valpix );
388 if ( key == MID_BUT )
389 break;
390 delta = MAXDELTA;
391 for ( i = 0; i < NumLinePos; i++ ) {
392 curr_delta = fabs((double)(LinePos[i] - cpx));
393 if ( curr_delta < delta ) {
394 linepos = i;
395 delta = curr_delta;
396 }
397 }
398 x[0] = x[1] = LinePos[linepos];
399 y[1] = LinePeak[linepos];
400 AG_GPLL( x, y, 2 );
401 sprintf( str, "Line removed : %.2f Wavelength: %.2f",
402 LinePos[linepos], Ident[linepos] );
403 SCTPUT(str);
404 Ident[linepos] = Rnull;
405 TCEWRR(id, LineRow[linepos], colid, &Rnull);
406 }
407
408 AG_SSET( DEF_COLOR );
409 TCTCLO(id);
410 end_graphic();
411 }
412
413 void show_current_idents()
414 {
415 int i;
416 char msg[MAXLINE];
417
418 SCTPUT( " " );
419 SCTPUT( "Current identifications" );
420 SCTPUT( "Position Wavelength" );
421 SCTPUT( "-----------------------" );
422 for ( i = 0; i < NumLinePos; i++ )
423 if ( Ident[i] != Rnull ) {
424 sprintf( msg, "%7.1f %9.2f", LinePos[i], Ident[i] );
425 SCTPUT( msg );
426 }
427 SCTPUT( " " );
428 }
429
430 void ident_line()
431 {
432 int i, linepos = 0;
433 int id, colid;
434 double delta = MAXDELTA, curr_delta;
435 float val;
436 float x[1], y[1];
437 char str[MAXLINE];
438
439 val = WlcPos[NumWlcPos-1]; /* last value picked */
440 for ( i = 0; i < NumLinePos; i++ ) {
441 curr_delta = fabs((double)(LinePos[i] - val));
442 if ( curr_delta < delta ) {
443 linepos = i;
444 delta = curr_delta;
445 }
446 }
447 sprintf( str, "Line added : %.2f Wavelength: %.2f",
448 LinePos[linepos], LincatPos[NumWlcPos-1] );
449 SCTPUT( str );
450
451 if ( TCTOPN( LineTable, F_IO_MODE, &id ) != 0 ) {
452 SCTPUT( "Line table could not be opened." );
453 return;
454 }
455 TCCSER( id, ":IDENT", &colid );
456 TCEWRR( id, LineRow[linepos], colid, &LincatPos[NumWlcPos-1] );
457 TCTCLO( id );
458
459 Ident[linepos] = LincatPos[NumWlcPos-1];
460
461 x[0] = LinePos[linepos];
462 y[0] = LinePeak[linepos];
463 Alabelx(x, y, 1, GREEN_COLOR);
464 }
465
466
467 /****************************************************************************
468 exist_descriptor(): checks if the descriptor 'desc' exists in 'image'.
469 */
470 int exist_descriptor( image, desc )
471 char *image; /* name of the image */
472 char *desc; /* descriptor name */
473 {
474 int nulval, id;
475 char type;
476
477 if ( file_exists( image, ".bdf" ) ) {
478 SCFOPN( image, D_R4_FORMAT, 0, F_IMA_TYPE, &id );
479 SCDFND( id, desc, &type, &nulval, &nulval );
480 if ( type != ' ' ) {
481 SCFCLO(id);
482 return(TRUE);
483 }
484 SCFCLO(id);
485 }
486 return(FALSE);
487 }
488
489 /****************************************************************
490 get_lines_to_add(): plot the spectrum and get x-coordinates of
491 the spectral lines to add from the graphics cursor.
492 */
493 void get_lines_to_add( plot_label)
494 int plot_label; /* TRUE --> plot spectrum with existent lines */
495 /* FALSE --> plot spectrum without existent lines */
496 {
497 float wlimits[4], x[2], y[2], cpx = 0.0, cpy = 0.0;
498 int key, valpix;
499
500 if ( !plot_spec(plot_label) ) /* plot 'WLC(Ystart)' */
501 return;
502
503 NToAdd = 0;
504
505 init_graphic(DEV_NO_ERASE);
506 set_viewport();
507
508 AG_SSET( RED_COLOR );
509 AG_RGET( "WNDL", wlimits );
510 y[0] = wlimits[YMIN];
511 y[1] = wlimits[YMAX];
512 while( 1 ) {
513 AG_VLOC( &cpx, &cpy, &key, &valpix );
514 if ( key == MID_BUT )
515 break;
516 LinesToAdd[NToAdd] = cpx;
517 x[0] = x[1] = cpx;
518 AG_GPLL( x, y, 2 );
519 NToAdd++;
520 }
521 AG_SSET( DEF_COLOR );
522
523 end_graphic();
524 }
525
526 /****************************************************************
527 create_add_table(): save the x-coordinates of the spectral lines
528 to add into the table LINEADD.tbl.
529 */
530 void create_add_table( n, lines )
531 int n;
532 float lines[];
533 {
534 int i, id, col;
535 int unit;
536
537 TCTINI( LINE_ADD_TABLE, F_TRANS, F_O_MODE, 3, 100, &id );
538 TCCINI( id, D_R4_FORMAT, 1, "F10.2", "PIXEL", "X", &col );
539 SCDWRI( id, "YSTART", &Ystart, 1, 1, &unit );
540
541 for ( i = 0; i < n; i++ )
542 TCRWRR( id, i+1, 1, &col, lines+i );
543
544 TCTCLO(id);
545 }
546
547 void label_spectrum()
548 {
549 float x[1], y[1];
550 int i;
551
552 Alabelx( LinePos, LinePeak, NumLinePos, BLUE_COLOR );
553
554 for ( i = 0; i < NumLinePos; i++ )
555 if ( Ident[i] != Rnull ) {
556 x[0] = LinePos[i];
557 y[0] = LinePeak[i];
558 Alabelx( x, y, 1, GREEN_COLOR );
559 }
560 }
561
562 int check_plot()
563 {
564 if ( !IdentBegin ) {
565 read_params_long();
566 if ( ! read_lincat_table() )
567 return FALSE;
568 if ( !read_line_x(IDCONT) ) { /* :X of line table */
569 SCTPUT( "*** Lines have not been searched ***" );
570 return FALSE;
571 }
572 if ( !plot_spec(LABEL_SPEC) )
573 return FALSE;
574 }
575 IdentBegin = TRUE;
576 return TRUE;
577 }
578
579 void cutx_spectrum()
580 {
581 if ( !check_plot() )
582 return;
583 while ( Acutx() != MID_BUT )
584 label_spectrum();
585 }
586
587 void cuty_spectrum()
588 {
589 if ( !check_plot() )
590 return;
591 while ( Acuty() != MID_BUT )
592 label_spectrum();
593 }
594
595 void shift_spectrum()
596 {
597 if ( !check_plot() )
598 return;
599 while ( Ashift() != MID_BUT )
600 label_spectrum();
601 }
602
603 void unzoom_spectrum()
604 {
605 if ( !check_plot() )
606 return;
607 Aunzoom();
608 label_spectrum();
609 }
610
611 void getcur_spectrum()
612 {
613 if ( !check_plot() )
614 return;
615 Agetcur();
616 }
617
618 void redraw_plot()
619 {
620 init_graphic(DEV_NO_ERASE);
621 set_viewport();
622 redraw_spectrum();
623 end_graphic();
624
625 label_spectrum();
626 }