Debian Codegraph @

gui/XAlice/libsrc/Amgauss.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 .VERSION
28 090825 last modif
29
30 ===========================================================================*/
31
32
33 #include <midas_def.h>
34 #include <alice_util.h>
35 #include <alice_global.h>
36 #include "mpfit.h"
37 #include "cpl_matrix.h"
38
39 #include <stdio.h> /* generic definitions */
40 #include <stdlib.h> /* function definitions */
41 #include <string.h> /* string definitions */
42 #include <unistd.h> /* string definitions */
43 #include <math.h>
44
45 #define ALICETABLE "TMPalice.tbl"
46 #define EPSILON 1e-15
47
48 float fit_cont();
49 void fpoly();
50 void ChangeCurs(), put_iterations(), DefaultCurs(), out_errors();
51 void draw_sgauss(), save_cont(), calc_fit(), put_table_values();
52 void ratint(), create_table(), read_init_guess(), read_fit_values();
53
54
55
56 int tid;
57 int col_ident, col_fgauss, col_fline, col_xcen, col_xfwhm,
58 col_cont, col_eqwt;
59 int col_error, col_ystart, col_yend, currline;
60 float line_error, xint1, xint2, yint1, yint2;
61 double fitContError;
62
63
64 static double gauss(double x, double amp, double mean, double sigma)
65 {
66 double v = x - mean;
67 return amp * exp(-(v * v) / (2. * (sigma * sigma)));
68 }
69
70 static double mgauss(double x, double * p, int n)
71 {
72 double r = 0;
73 int j;
74 for (j = 0; j < n; j+=3) {
75 r += gauss(x, p[j], p[j + 1], p[j + 2]);
76 }
77 return r;
78 }
79
80 typedef struct {
81 double * x;
82 double * y;
83 } priv_data;
84
85 int fit_mgauss(int m, int n, double *p, double *deviates,
86 double **derivs, priv_data *d)
87 {
88 int i;
89 for (i=0; i<m; i++) {
90 deviates[i] = d->y[i] - mgauss(d->x[i], p, n);
91 }
92
93 return 0;
94 }
95
96
97 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
98 void Amgauss() : execute a multi-gaussian fitting and plot the solution.
99 ---------------------------------------------------------------------*/
100 void Amgauss()
101
102 {
103 double *xgauss, *ygauss;
104 double *a;
105 double contin;
106 int ndata, ma;
107 int i, j;
108 double getFitValue();
109
110 ma = gaussNumOfSol * 3;
111 ndata = specNpix[0];
112 a = malloc(ma * sizeof(*a));
113 xgauss = malloc(ndata * sizeof(*xgauss));
114 ygauss = malloc(ndata * sizeof(*ygauss));
115 for (i = 0; i < ma; i++)
116 a[i] = (double) gaussFitValues[i];
117 /* get relative parameters */
118 for (j = 1; j <= ma; j++)
119 a[j - 1] = getFitValue(a, j, ma);
120
121 i = 0;
122 while ((specX[i] < specXcen - specDx) & (specX[i + 1] < specXmax)) i++;
123 j = 0;
124 while ((specX[i + j] < specXcen + specDx) & (specX[i + j] < specXmax))
125 {
126 xgauss[j] = specX[i + j];
127 contin = fit_cont(xgauss[j]);
128 ygauss[j] = specY[i + j] - contin;
129 j++;
130 }
131 ndata = j;
132
133 /* old code had ability to define gauss parameters relative to previous
134 * gauss, but the code for that was buggy and likely never worked, so the
135 * feature is not considered here */
136 {
137 mp_par * pars = calloc(ma, sizeof(*pars));
138 priv_data pdata = {xgauss, ygauss};
139 mp_result result;
140 mp_config config;
141 memset(&config, 0, sizeof(config));
142 config.maxiter = gaussMaxIterations;
143 memset(&result, 0, sizeof(result));
144 result.xerror = malloc(ma * sizeof(result.xerror[0]));
145 for (i = 0; i < ma; i++) {
146 if (gaussFixOpt[i])
147 pars[i].fixed = 1;
148 }
149 mpfit((mp_func)&fit_mgauss, ndata, ma,
150 a, pars, &config, &pdata, &result);
151
152 put_iterations(result.niter);
153 DefaultCurs();
154
155 for (i = 0; i < ma; i++) {
156 gaussFitValues[i] = (float) a[i];
157 gaussErrors[i] = result.xerror[i];
158 }
159 fitRms = result.bestnorm;
160
161 free(result.xerror);
162 free(pars);
163 }
164
165 out_errors();
166 ndata = specNpix[0];
167 free(ygauss);
168 free(xgauss);
169 free(a);
170 }
171
172 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
173 int sgauss(ycen, xcen, xstep) : execute a single gaussian fitting and
174 plot the solution.
175 xcen , ycen : x-cent and amplitude.
176 xstep : sigma.
177 ---------------------------------------------------------------------*/
178 int sgauss(ycen, xcen, xstep)
179 float xcen, ycen, xstep;
180
181 {
182 double *xgauss, *ygauss;
183 double contin;
184 double fwhm, area, eqwt, conta2;
185 int ndata, ma;
186 int i, j;
187 char out[80];
188 double a[3];
189
190 ma = 3;
191 ndata = specNpix[0];
192 xgauss = malloc(ndata * sizeof(*xgauss));
193 ygauss = malloc(ndata * sizeof(*ygauss));
194 a[0] = (double) ycen - fit_cont(xcen);
195 a[1] = (double) xcen;
196 a[2] = (double) xstep;
197 i = 0;
198 while ((specX[i] < specXcen - specDx) & (specX[i + 1] < specXmax)) i++;
199 j = 0;
200 while ((specX[i + j] < specXcen + specDx) & (specX[i + j] < specXmax))
201 {
202 xgauss[j] = specX[i + j];
203 contin = fit_cont(xgauss[j]);
204 ygauss[j] = specY[i + j] - contin;
205 j++;
206 }
207 ndata = j;
208 {
209 priv_data pdata = {xgauss, ygauss};
210 mpfit((mp_func)&fit_mgauss, ndata, ma,
211 a, NULL, NULL, &pdata, NULL);
212 }
213
214 draw_sgauss(a[0], a[1], a[2], 4);
215 fwhm = 2.35482 * a[2];
216 conta2 = fit_cont(a[1]);
217 if (conta2 == 0.0)
218 specFluxReal = 0.0;
219 eqwt = specFluxReal / conta2;
220 area = 2.50663 * a[0] * a[2];
221 line_error = fitContError *
222 sqrt(fabs(specStep * (eqwt + 2 * (xint2 - xint1))));
223 sprintf(out, "%8.5f %8.5f %6.0f %9.5g %8.5f %8.5f", a[1],
224 fwhm, conta2, specFluxReal, eqwt, line_error);
225 SCTPUT(out);
226 put_table_values(specFrameIdent, area, specFluxReal, a[1], fwhm,
227 conta2, eqwt, line_error, specLineNum, specLineNum + specLineStep - 1);
228 currline++;
229 ndata = specNpix[0];
230 free(ygauss);
231 free(xgauss);
232 return 0;
233 }
234
235 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
236 int draw_gauss() : draw the multi-gaussian fitting solution.
237 ---------------------------------------------------------------------*/
238 int draw_gauss()
239 {
240 double wl, wr, contin;
241 double *a;
242 float xline[1000], yline[1000], yres;
243 float fit_cont();
244 int i;
245
246 a = malloc(3 * gaussNumOfSol * sizeof(*a));
247 for (i = 0; i < 3 * gaussNumOfSol; i++)
248 a[i] = (double) gaussFitValues[i];
249 wl = specXcen - specDx;
250 wr = specXcen + specDx;
251 for (i = 0; i < 1000; i++)
252 {
253 xline[i] = (float) i *(wr - wl) / 1000.0 + wl;
254
255 yres = mgauss(xline[i], a, 3 * gaussNumOfSol);
256 contin = fit_cont(xline[i]);
257 yline[i] = yres + contin;
258 }
259 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
260 AG_MOPN(PLOTAPP);
261 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
262 AG_WDEF(specXcen - specDx, specXcen + specDx,
263 specYcen - specDy, specYcen + specDy);
264 AG_SSET("lstyle=0;lwidt=2;color=2");
265 AG_GPLL(xline, yline, 1000);
266 AG_VUPD();
267 AG_MCLS();
268 AG_CLS();
269 free(a);
270 return 0;
271 }
272
273 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
274 int draw_error() : draw the residual.
275 ---------------------------------------------------------------------*/
276 int draw_error()
277
278 {
279 double contin;
280 double *a;
281 float xline[MAXVALUES], yline[MAXVALUES], yres;
282 float fit_cont();
283 int i;
284
285 a = malloc(3 * gaussNumOfSol * sizeof(*a));
286 for (i = 0; i < 3 * gaussNumOfSol; i++)
287 a[i] = (double) gaussFitValues[i];
288 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
289 AG_MOPN(PLOTAPP);
290 AG_SSET("lwidth=0;lstyle=0");
291 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
292 AG_WDEF(specXcen - specDx, specXcen + specDx,
293 specYcen - specDy, specYcen + specDy);
294 xline[0] = specX[0];
295 xline[1] = specX[specNpix[0] - 1];
296 yline[0] = specYcen + specDy / 1.5;
297 yline[1] = specYcen + specDy / 1.5;
298 AG_GPLL(xline, yline, 2);
299 for (i = 0; i < specNpix[0]; i++)
300 {
301 xline[i] = specX[i];
302 yres = mgauss(xline[i], a, 3 * gaussNumOfSol);
303 contin = fit_cont(xline[i]);
304 yline[i] = yres + contin - specY[i] + specYcen + specDy / 1.5;
305 }
306 AG_SSET("color = 2");
307 AG_GPLL(xline, yline, specNpix[0]);
308 AG_VUPD();
309 AG_MCLS();
310 AG_CLS();
311 free(a);
312
313 return 0;
314 }
315
316 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
317 void draw_sgauss(a1, a2, a3, color) : draw a single gaussian curve.
318 a1 : x-cent.
319 a2 : Amplitude.
320 a3 : sigma.
321 ---------------------------------------------------------------------*/
322 void draw_sgauss(a1, a2, a3, color)
323 float a1, a2, a3;
324 int color;
325
326 {
327 double wl, wr, contin;
328 double a[3];
329 float xline[100], yline[100], yres;
330 float fit_cont();
331 int i;
332 char sset[80];
333
334 sprintf(sset, "lstyle=2;lwidth=0;color=%d", color);
335 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
336 AG_MOPN(PLOTAPP);
337 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
338 AG_WDEF(specXcen - specDx, specXcen + specDx,
339 specYcen - specDy, specYcen + specDy);
340 AG_SSET(sset);
341 AG_MCLS();
342 AG_SSET("lstyle=0");
343 AG_MOPN(PLOTAPP);
344 a[0] = (double) a1;
345 a[1] = (double) a2;
346 a[2] = (double) a3;
347 wl = a[1] - 3 * a[2];
348 wr = a[1] + 3 * a[2];
349 for (i = 0; i < 100; i++)
350 {
351 xline[i] = (float) i *(wr - wl) / 100.0 + wl;
352
353 yres = mgauss(xline[i], a, 3);
354 contin = fit_cont(xline[i]);
355 yline[i] = yres + contin;
356 }
357 AG_GPLL(xline, yline, 100);
358 AG_VUPD();
359 AG_MCLS();
360 /* AG_SSET("lstyle=0"); AG_GPLL(xline,yline,100); */
361 AG_CLS();
362 }
363
364 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
365 int draw_number_comp(comp, sol, color) : put the number of the gaussian
366 component over the solution.
367 ---------------------------------------------------------------------*/
368 int draw_number_comp(comp, sol, color)
369 int comp, sol, color;
370
371 {
372 char out[80];
373
374 sprintf(out, "color=%d;chdi=1.2,1.2;changl=0.0", color);
375 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
376 AG_MOPN(PLOTAPP);
377 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
378 AG_WDEF(specXcen - specDx, specXcen + specDx,
379 specYcen - specDy, specYcen + specDy);
380 AG_SSET(out);
381 sprintf(out, "%d", sol + 1);
382 AG_GTXT(gaussFitValues[comp * 3 + 1],
383 gaussFitValues[comp * 3] + fit_cont(gaussFitValues[comp * 3 + 1]), out, 12);
384 AG_VUPD();
385 AG_MCLS();
386 AG_CLS();
387
388 return 0;
389 }
390
391 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
392 void plot_fit(xv, yv, npt, n, color)
393 xv,yv : x and y values.
394 npt : number of data.
395 n : fit degree.
396 color : plot color.
397 ---------------------------------------------------------------------*/
398 void plot_fit(xv_, yv_, npt, n, color)
399 float xv_[], yv_[];
400 int npt, n, color;
401
402 {
403 int i, k;
404 double *a, *sig, **covar, deltax, *powx;
405 float xl[2], yl[2], fit_cont();
406 float SQR();
407 char sset[40];
408 double *xv, *yv;
409
410 a = dvector(1, n);
411 xv = dvector(1, npt);
412 yv = dvector(1, npt);
413 sig = dvector(1, npt);
414 covar = dmatrix(1, n, 1, n);
415 powx = dvector(1, n);
416 for (i = 1; i <= npt; i++) {
417 xv[i] = xv_[i];
418 yv[i] = yv_[i];
419 sig[i] = 1.0;
420 }
421 lsqfit_nr(xv, yv, sig, npt, a, n, fpoly);
422 for (i = 0; i <= fitDegree; i++)
423 fitPolyValues[i] = a[i + 1];
424 for (; i < 20; i++)
425 fitPolyValues[i] = 0.0;
426 deltax = 2 * specDx / 100.0;
427 fpoly(specXcen - specDx, powx, n);
428 xl[1] = specXcen - specDx;
429 yl[1] = a[1] * powx[1];
430 for (i = 2; i <= n; i++)
431 yl[1] += a[i] * powx[i];
432 sprintf(sset, "lstyle=1;lwidth=0;color=%d", color);
433 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
434 AG_MOPN(PLOTAPP);
435 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
436 AG_WDEF(specXcen - specDx, specXcen + specDx,
437 specYcen - specDy, specYcen + specDy);
438 AG_SSET(sset);
439 AG_MCLS();
440 AG_SSET("lstyle=0");
441 AG_MOPN(PLOTAPP);
442 for (k = 1; k <= 99; k++)
443 {
444 xl[0] = xl[1];
445 yl[0] = yl[1];
446 xl[1] += deltax;
447 fpoly(xl[1], powx, n);
448 yl[1] = a[1] * powx[1];
449 for (i = 2; i <= n; i++)
450 yl[1] += a[i] * powx[i];
451 AG_GPLL(xl, yl, 2);
452 }
453 AG_MCLS();
454 AG_CLS();
455 fitContError = 0.0;
456 for (i = 1; i <= npt; i++)
457 fitContError += SQR(fit_cont(xv[i]) - yv[i]);
458 fitContError = (float) sqrt(fitContError / npt);
459 free_dvector(powx, 1, n);
460 free_dmatrix(covar, 1, n, 1, n);
461 free_dvector(xv, 1, npt);
462 free_dvector(yv, 1, npt);
463 free_dvector(sig, 1, npt);
464 free_dvector(a, 1, n);
465 save_cont("TMPcont.bdf");
466 }
467
468 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
469 void calc_fit(xv, yv, npt, n) : calculate the polynomial fit of degree n
470 and set the global fit array.
471 ---------------------------------------------------------------------*/
472 void calc_fit(xv_, yv_, npt, n)
473 float xv_[], yv_[];
474 int npt, n;
475
476 {
477 int i;
478 double *a, *sig, **covar, *powx;
479 double *xv, *yv;
480
481 a = dvector(1, n);
482 xv = dvector(1, npt);
483 yv = dvector(1, npt);
484 sig = dvector(1, npt);
485 covar = dmatrix(1, n, 1, n);
486 powx = dvector(1, n);
487 for (i = 1; i <= npt; i++) {
488 xv[i] = xv_[i];
489 yv[i] = yv_[i];
490 sig[i] = 1.0;
491 }
492 lsqfit_nr( xv, yv, sig, npt, a, n, fpoly);
493 for (i = 0; i <= fitDegree; i++)
494 fitPolyValues[i] = a[i + 1];
495 for (; i < 20; i++)
496 fitPolyValues[i] = 0.0;
497 free_dvector(powx, 1, n);
498 free_dmatrix(covar, 1, n, 1, n);
499 free_dvector(xv, 1, npt);
500 free_dvector(yv, 1, npt);
501 free_dvector(sig, 1, npt);
502 free_dvector(a, 1, n);
503 }
504
505 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
506 void plot_spline(n, color) : plot a line using splaine interpolation.
507 ---------------------------------------------------------------------*/
508 void plot_spline(n, color)
509 int n, color;
510
511 {
512 int k;
513 float xl[2], yl[2], fit_cont(), deltax;
514 char sset[40];
515
516 deltax = 2 * specDx / 100.0;
517 sprintf(sset, "lstyle=1;lwidth=0;color=%d", color);
518 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
519 AG_MOPN(PLOTAPP);
520 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
521 AG_WDEF(specXcen - specDx, specXcen + specDx,
522 specYcen - specDy, specYcen + specDy);
523 AG_SSET(sset);
524 AG_MCLS();
525 AG_SSET("lstyle=0");
526 AG_MOPN(PLOTAPP);
527 xl[1] = specXcen - specDx;
528 yl[1] = fit_cont(xl[1]);
529 for (k = 1; k <= 99; k++)
530 {
531 xl[0] = xl[1];
532 yl[0] = yl[1];
533 xl[1] += deltax;
534 yl[1] = fit_cont(xl[1]);
535 AG_GPLL(xl, yl, 2);
536 }
537 AG_MCLS();
538 AG_CLS();
539 save_cont("TMPcont.bdf");
540 }
541
542 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
543 float fit_cont(xcont) : return the interpolated y value using polynomial or
544 spline fitting.
545 ---------------------------------------------------------------------*/
546 float fit_cont(xcont)
547 float xcont;
548
549 {
550 int i;
551 float yret, dy;
552 double *powx;
553
554 if (fitMode == POLYFITMODE)
555 {
556 powx = dvector(1, fitDegree + 1);
557 fpoly(xcont, powx, fitDegree + 1);
558 yret = fitPolyValues[0] * powx[1];
559 for (i = 1; i <= fitDegree; i++)
560 yret += fitPolyValues[i] * powx[i + 1];
561 free_dvector(powx, 1, fitDegree + 1);
562 return (yret);
563 } else if (fitMode == SPLINEFITMODE)
564 {
565 ratint(specXaux, specYaux, gaussNumOfFitData, xcont, &yret, &dy);
566 return (yret);
567 } else
568 {
569 printf("\rContinuum error : 0.0 returned\n");
570 return (0);
571 }
572 }
573
574 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
575 void add_fit(color) : add new x values to the fitting X array.
576 color : arrow color.
577 ---------------------------------------------------------------------*/
578 void add_fit(color)
579 int color;
580
581 {
582 int key, pix;
583 float xfit1, xfit2, yfit1, yfit2, aux;
584 char sset[20];
585
586 key = 1;
587 fitAddFit = 1;
588 sprintf(sset, "COLOR=%d", color);
589 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
590 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
591 AG_WDEF(specXcen - specDx, specXcen + specDx,
592 specYcen - specDy, specYcen + specDy);
593 AG_SSET(sset);
594 AG_SSET("CURSOR = 2");
595 xfit1 = specXcen;
596 yfit1 = aux = specYcen;
597 if (fitMode == POLYFITMODE)
598 {
599 AG_SSET("SCALE = 1.5");
600 while (key == 1)
601 {
602 xfit1 = xfit2;
603 yfit1 = aux;
604 AG_VLOC(&xfit1, &yfit1, &key, &pix);
605 if (key == 1)
606 {
607 for (i = 0; specX[i] < xfit1; i++);
608 AG_GTXT(xfit1, specY[i], "\\downarro", 2);
609 xfit2 = xfit1;
610 yfit2 = yfit1;
611 AG_VLOC(&xfit2, &yfit2, &key, &pix);
612 if (key == 1)
613 {
614 for (i = 0; specX[i] < xfit2; i++);
615 AG_GTXT(xfit2, specY[i], "\\downarro", 2);
616 if (xfit1 > xfit2)
617 {
618 aux = xfit1;
619 xfit1 = xfit2;
620 xfit2 = aux;
621 }
622 aux = yfit1;
623 for (i = 0; specX[i] < xfit1; i++);
624 yfit1 = specY[i];
625 fitXminPair[fitPairNum] = xfit1;
626 fitXmaxPair[fitPairNum] = xfit2;
627 fitPairNum++;
628 for (; specX[i] < xfit2; i++, gaussNumOfFitData++)
629 {
630 specXaux[gaussNumOfFitData] = specX[i];
631 specYaux[gaussNumOfFitData] = specY[i];
632 }
633 yfit2 = specY[i];
634 }
635 }
636 }
637 AG_SSET("SCALE = 1.0");
638 } else
639 while (key == 1)
640 {
641 xfit1 = xfit2;
642 yfit1 = aux;
643 AG_VLOC(&xfit1, &yfit1, &key, &pix);
644 if (key == 1)
645 {
646 gaussNumOfFitData++;
647 specXaux[gaussNumOfFitData] = xfit1;
648 specYaux[gaussNumOfFitData] = yfit1;
649 AG_GPLM(specXaux + gaussNumOfFitData, specYaux + gaussNumOfFitData,
650 1, 5);
651 }
652 }
653 AG_VUPD();
654 AG_CLS();
655 }
656
657 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
658 int auto_fit(spec) : calculate the continuum for the mentionated spectrum.
659 spec : 0 = current spectrum; 1-15 overplot spectrum.
660 ---------------------------------------------------------------------*/
661 int auto_fit(spec)
662 int spec;
663
664 {
665 int i, j = -1;
666 float *yvalues, *xvalues;
667 /* extern ifit(); */
668
669 xvalues = spec == 0 ? specX : OverX[spec - 1];
670 yvalues = spec == 0 ? specY : OverY[spec - 1];
671
672 for (i = 0; i < fitPairNum; i++)
673 {
674 for (j = 0; xvalues[j] < fitXminPair[i]; j++);
675 for (; xvalues[j] < fitXmaxPair[i]; j++, gaussNumOfFitData++)
676 {
677 specXaux[gaussNumOfFitData] = xvalues[j];
678 specYaux[gaussNumOfFitData] = yvalues[j];
679 }
680 }
681
682 return 0;
683 }
684
685 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
686 int integrate() : called for Integrate push button.
687 ---------------------------------------------------------------------*/
688 int integrate()
689 {
690 int key, pix;
691 float aux, xcen, ycen;
692 char sset[20];
693
694 key = 1;
695 create_table();
696 SCTPUT("\n");
697 SCTPUT("Center FWHM Contin Flux EQWT Error");
698 SCTPUT("--------------------------------------------------------------------");
699 sprintf(sset, "COLOR=4");
700 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
701 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
702 AG_WDEF(specXcen - specDx, specXcen + specDx,
703 specYcen - specDy, specYcen + specDy);
704 AG_SSET(sset);
705 AG_SSET("CURSOR = 2");
706 xint1 = specXcen;
707 yint1 = aux = specYcen;
708 AG_SSET("SCALE = 1.5");
709 while (key == 1)
710 {
711 xint1 = xint2;
712 yint1 = aux;
713 AG_VLOC(&xint1, &yint1, &key, &pix);
714 if (key == 1)
715 {
716 for (i = 0; specX[i] < xint1; i++);
717 AG_GTXT(xint1, specY[i], "\\downarro", 2);
718 xint2 = xint1;
719 yint2 = yint1;
720 AG_VLOC(&xint2, &yint2, &key, &pix);
721 if (key == 1)
722 {
723 for (i = 0; specX[i] < xint2; i++);
724 AG_GTXT(xint2, specY[i], "\\downarro", 2);
725 if (xint1 > xint2)
726 {
727 aux = xint1;
728 xint1 = xint2;
729 xint2 = aux;
730 }
731 aux = yint1;
732 for (i = 0; specX[i] < xint1; i++);
733 ycen = specY[i] - fit_cont(specX[i]);
734 xcen = specX[i];
735 specFluxReal = 0.0;
736 for (; specX[i] < xint2; i++)
737 {
738 aux = specY[i] - fit_cont(specX[i]);
739 specFluxReal += specStep * aux;
740 if (aux * aux > ycen * ycen)
741 {
742 ycen = aux;
743 xcen = specX[i];
744 }
745 }
746 AG_CLS();
747 sgauss(ycen, xcen, specStep);
748 AG_VDEF("graph_wnd0/n:", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
749 AG_CDEF(specClip[0], specClip[1], specClip[2], specClip[3]);
750 AG_WDEF(specXcen - specDx, specXcen + specDx,
751 specYcen - specDy, specYcen + specDy);
752 AG_SSET(sset);
753 AG_SSET("CURSOR = 2");
754 AG_SSET("SCALE = 1.5");
755 }
756 }
757 }
758 AG_SSET("SCALE = 1.0");
759 AG_VUPD();
760 AG_CLS();
761 TCTCLO(tid);
762 return 0;
763 }
764
765 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
766 void print_plot(mode, printer) : print the current ploted frame.
767 mode : LANDSCAPE or PORTRAIT mode
768 ---------------------------------------------------------------------*/
769 void print_plot(mode, printer)
770 int mode;
771 char *printer;
772
773 {
774 char sys[60];
775 char dname[20];
776 int iac;
777
778 system("rm -f pscrplot.*");
779
780 (void) strcpy(dname, printer);
781 if (mode == NORMALPRINT)
782 (void) strcat( dname, ":");
783 else
784 (void) strcat( dname, ".p:");
785 AG_VDEF(dname, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
786 AG_MRDW(PLOTNAME);
787 AG_MRDW("alicel.plt");
788 AG_CLS();
789
790 (void) SCKGETC( "SYSCOMS", 1, 20, &iac, sys );
791 (void) strcat( sys, printer );
792 (void) strcat( sys, " pscrplot.0");
793 system(sys);
794 }
795
796 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
797 void print_statistics() : print the current statistics on the Statistics
798 interface.
799 ---------------------------------------------------------------------*/
800 void print_statistics()
801
802 {
803 FILE *stat;
804 char line[80];
805 double area, total = 0.0;
806
807 if ((stat = fopen("TMPalice.stat", "w")) != NULL)
808 {
809 fprintf(stat, "Alice output file\n\n");
810 fprintf(stat, "Input file : %s ", specImageName);
811 if (specLineStep > 1)
812 fprintf(stat, "Lines %d to %d\n\n",
813 specLineNum, specLineNum + specLineStep - 1);
814 else
815 fprintf(stat, "Line %d\n\n", specLineNum);
816 fprintf(stat,
817 "Frame limits \n\txmin: %f xmax: %f\n\tymin: %f ymax %f\n\n",
818 specXcen - specDx, specXcen + specDx,
819 specYcen - specDy, specYcen + specDy);
820 fprintf(stat, "Gaussian Values:\n");
821 fprintf(stat, "\t\t\tInitial Guess\n\n Component\tAmplitude\t\tPosition\t\tWidth\n");
822 read_init_guess();
823 for (i = 0; i < gaussNumOfComp; i++)
824 {
825 sprintf(line, " %d\t\t%f\t\t%f\t\t%f\n", i + 1, gaussFitValues[i * 3],
826 gaussFitValues[i * 3 + 1], gaussFitValues[i * 3 + 2]);
827 fprintf(stat, "%s", line);
828 }
829 fprintf(stat, "\n");
830 sprintf(line, "\t\t\tSolution\n\n Component\tAmplitude\t\tPosition\t\tWidth\n");
831 fprintf(stat, "%s", line);
832 read_fit_values();
833 for (i = 0; i < gaussNumOfComp; i++)
834 {
835 sprintf(line, " %d\t\t%f\t\t%f\t\t%f\n", i + 1, gaussFitValues[i * 3],
836 gaussFitValues[i * 3 + 1], gaussFitValues[i * 3 + 2]);
837 fprintf(stat, "%s", line);
838 }
839 fprintf(stat, "\n\n Statistics\n");
840 fprintf(stat, "\t\tGaussian Standard Deviation\n\n");
841 fprintf(stat, " Component\tAmplitude\t\tPosition\t\tWidth\n");
842 for (i = 0; i < gaussNumOfComp; i++)
843 {
844 sprintf(line, " %d\t\t%f\t\t%f\t\t%f\n", i + 1, gaussErrors[i * 3],
845 gaussErrors[i * 3 + 1], gaussErrors[i * 3 + 2]);
846 fprintf(stat, "%s", line);
847 }
848 fprintf(stat, "\n RMS : %f\n", fitRms);
849 fprintf(stat, "\n\t\tIntegration:\n");
850 fprintf(stat, " Component\tflux\t\t\tfwhm\n");
851 for (i = 0; i < gaussNumOfComp; i++)
852 {
853 area = 2.50663 * gaussFitValues[i * 3] * gaussFitValues[i * 3 + 2];
854 total += area;
855 fprintf(stat, " %d\t\t%f\t\t%f\n", i + 1, area,
856 2.35482 * gaussFitValues[i * 3 + 2]);
857 }
858 fprintf(stat, "Total flux: \t%f\n", total);
859 fclose(stat);
860 } else
861 printf("Can't open tmp file\n");
862 }
863
864 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
865 void create_table() : create a table for the integration output.
866 ---------------------------------------------------------------------*/
867 void create_table()
868
869 {
870 TCTINI(ALICETABLE, F_TRANS, F_O_MODE, 20, 300, &tid);
871 TCCINI(tid, D_C_FORMAT, 32, "A32", "", "IDENT", &col_ident);
872 TCCINI(tid, D_R4_FORMAT, 1, "F10.3", "", "FLUX_GAUSS", &col_fgauss);
873 TCCINI(tid, D_R4_FORMAT, 1, "F10.3", "", "FLUX_LINE", &col_fline);
874 TCCINI(tid, D_R4_FORMAT, 1, "F10.3", "", "XCEN", &col_xcen);
875 TCCINI(tid, D_R4_FORMAT, 1, "F10.3", "", "XFWHM", &col_xfwhm);
876 TCCINI(tid, D_R4_FORMAT, 1, "F10.3", "", "CONT", &col_cont);
877 TCCINI(tid, D_R4_FORMAT, 1, "F10.3", "", "EQWT", &col_eqwt);
878 TCCINI(tid, D_R4_FORMAT, 1, "F10.3", "", "ERROR", &col_error);
879 TCCINI(tid, D_I4_FORMAT, 1, "I8", "", "YSTART", &col_ystart);
880 TCCINI(tid, D_I4_FORMAT, 1, "I8", "", "YEND", &col_yend);
881 currline = 1;
882 }
883
884 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
885 void put_table_values()
886 ---------------------------------------------------------------------*/
887 void put_table_values(ident, fluxg, fluxl, xcen, xfwhm,
888 cont, eqwt, error, ystart, yend)
889 char *ident;
890 float fluxg, fluxl, xcen, xfwhm, cont, eqwt, error;
891 int ystart, yend;
892
893 {
894 float fvalues[20];
895 int ivalues[20];
896 int colnum[20];
897 int column, row, nsort, allcol, allrow;
898
899 TCIGET(tid, &column, &row, &nsort, &allcol, &allrow);
900 colnum[0] = col_fgauss;
901 colnum[1] = col_fline;
902 colnum[2] = col_xcen;
903 colnum[3] = col_xfwhm;
904 colnum[4] = col_cont;
905 colnum[5] = col_eqwt;
906 colnum[6] = col_error;
907 fvalues[0] = fluxg;
908 fvalues[1] = fluxl;
909 fvalues[2] = xcen;
910 fvalues[3] = xfwhm;
911 fvalues[4] = cont;
912 fvalues[5] = eqwt;
913 fvalues[6] = error;
914 TCRWRR(tid, currline, 7, colnum, fvalues);
915 colnum[0] = col_ystart;
916 colnum[1] = col_yend;
917 ivalues[0] = ystart;
918 ivalues[1] = yend;
919 TCRWRI(tid, currline, 2, colnum, ivalues);
920 TCEWRC(tid, currline, col_ident, ident);
921 }
922
923 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
924 void out_integration() : print the integration output.
925 ---------------------------------------------------------------------*/
926 void out_integration()
927 {
928 int i;
929 char out[80];
930 float fit_cont();
931 double area, fwhm, conta2, xl, xr, aux, total = 0.0;
932
933 read_fit_values();
934 SCTPUT("\n");
935 SCTPUT("------------------------------------------------------");
936 SCTPUT(" Comp\tArea\t\tFWHM\t\tContin");
937 xl = gaussFitValues[1] - 3 * gaussFitValues[2];
938 xr = gaussFitValues[1] + 3 * gaussFitValues[2];
939 for (i = 1; i < gaussNumOfSol; i++)
940 {
941 if ((aux = gaussFitValues[3 * i + 1] + 3 * gaussFitValues[3 * i + 2]) > xr)
942 xr = aux;
943 if ((aux = gaussFitValues[3 * i + 1] - 3 * gaussFitValues[3 * i + 2]) < xl)
944 xl = aux;
945 }
946 for (i = 0; specX[i] < xl; i++);
947 specFluxReal = 0.0;
948 for (; specX[i] < xr; i++)
949 specFluxReal += specStep * (specY[i] - fit_cont(specX[i]));
950 for (i = 0; i < gaussNumOfSol; i++)
951 {
952 fwhm = 2.35482 * gaussFitValues[i * 3 + 2];
953 conta2 = fit_cont(gaussFitValues[i * 3 + 1]);
954 area = 2.50663 * gaussFitValues[i * 3] * gaussFitValues[i * 3 + 2];
955 total += area;
956 sprintf(out, " %d\t%8.5f\t%8.5f\t%6.0f", i + 1, area, fwhm, conta2);
957 SCTPUT(out);
958 }
959 sprintf(out, "\n Total area : \t%f", total);
960 SCTPUT(out);
961 sprintf(out, " Real flux : \t%f\t(x: %g - %g)", specFluxReal, xl, xr);
962 SCTPUT(out);
963 SCTPUT("------------------------------------------------------\n");
964 }
965
966 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
967 float SQR(sq)
968 ---------------------------------------------------------------------*/
969 float SQR(sq)
970
971 float sq;
972 {
973 return (sq * sq);
974 }