49cpl_error_code
sc_weights(cpl_table *skyspec, cpl_table *fitpar,
50 cpl_table *groups, cpl_parameterlist *parlist)
84 return CPL_ERROR_NONE;
89 cpl_parameterlist *parlist,
90 const cpl_table *groups)
129 int nlin = 0, i = 0, groupa = 0, groupb = 0, maxa = 0, maxb = 0, minb = 0;
130 int deg = 0, ncoef = 0, npar = 0, j = 0, group = 0, syst = 0;
133 cpl_table_new_column(fitpar,
"type", CPL_TYPE_STRING);
134 cpl_table_new_column(fitpar,
"N", CPL_TYPE_INT);
135 cpl_table_new_column(fitpar,
"system", CPL_TYPE_INT);
136 cpl_table_new_column(fitpar,
"relevance", CPL_TYPE_INT);
137 cpl_table_new_column(fitpar,
"fit", CPL_TYPE_INT);
138 cpl_table_new_column(fitpar,
"value", CPL_TYPE_DOUBLE);
139 cpl_table_new_column(fitpar,
"err_fit", CPL_TYPE_DOUBLE);
140 cpl_table_new_column(fitpar,
"err_est", CPL_TYPE_DOUBLE);
141 cpl_table_new_column(fitpar,
"N_lin", CPL_TYPE_INT);
144 nlin = cpl_table_get_nrow(groups);
148 for (i = 0; i < nlin; i++) {
149 groupa = cpl_table_get(groups,
"groupA", i, NULL);
150 groupb = cpl_table_get(groups,
"groupB", i, NULL);
163 p = cpl_parameter_new_value(
"n_groupA", CPL_TYPE_INT,
"",
"", maxa);
164 cpl_parameterlist_append(parlist, p);
165 p = cpl_parameter_new_value(
"n_groupB", CPL_TYPE_INT,
"",
"", maxb);
166 cpl_parameterlist_append(parlist, p);
167 p = cpl_parameter_new_value(
"n_system", CPL_TYPE_INT,
"",
"", -minb);
168 cpl_parameterlist_append(parlist, p);
172 p = cpl_parameterlist_find(parlist,
"cheby_max");
173 deg = cpl_parameter_get_int(p);
176 }
else if (deg == 0) {
185 npar = maxa + maxb + ncoef;
186 cpl_table_set_size(fitpar, npar);
190 for (j = 0; j < npar; j++) {
192 if (j >= maxa + maxb) {
193 cpl_table_set_string(fitpar,
"type", j,
"w");
194 if (j == maxa + maxb) {
195 p = cpl_parameterlist_find(parlist,
"cheby_const");
196 cpl_table_set(fitpar,
"value", j,
197 cpl_parameter_get_double(p));
198 }
else if (j == maxa + maxb + 1) {
199 cpl_table_set(fitpar,
"value", j, 1.);
201 cpl_table_set(fitpar,
"value", j, 0.);
205 cpl_table_set_string(fitpar,
"type", j,
"A");
207 cpl_table_set_string(fitpar,
"type", j,
"B");
209 cpl_table_set(fitpar,
"value", j, 1.);
212 if (j == maxa + maxb) {
214 }
else if (j == maxa) {
219 cpl_table_set_int(fitpar,
"N", j, group);
225 cpl_table_fill_column_window(fitpar,
"relevance", 0, maxa + maxb, 1);
226 cpl_table_fill_column_window(fitpar,
"fit", 0, maxa + maxb, 1);
227 cpl_table_fill_column_window(fitpar,
"relevance", maxa + maxb, ncoef, 0);
228 cpl_table_fill_column_window(fitpar,
"fit", maxa + maxb, ncoef, 0);
231 cpl_table_fill_column_window(fitpar,
"system", 0, npar, 0);
232 for (i = 0; i < nlin; i++) {
233 syst = cpl_table_get(groups,
"system", i, NULL);
234 groupa = cpl_table_get(groups,
"groupA", i, NULL);
235 groupb = cpl_table_get(groups,
"groupB", i, NULL);
236 cpl_table_set_int(fitpar,
"system", groupa - 1, syst);
238 cpl_table_set_int(fitpar,
"system", maxa + groupb - 1, syst);
243 cpl_table_fill_column_window(fitpar,
"err_fit", 0, npar, 0.);
244 cpl_table_fill_column_window(fitpar,
"err_est", 0, npar, 0.);
247 cpl_table_fill_column_window(fitpar,
"N_lin", 0, npar, 0);
249 return CPL_ERROR_NONE;
255 const cpl_parameterlist *parlist)
280 const cpl_parameter *p;
282 cpl_table *groupspec;
285 int nrow = 0, varfwhm = 0, i = 0, ngroup = 0, mingroup = 0, group = 0;
286 double lcut = 0., ucut = 0., fwhm = 0., speedpar = 0., meanlam = 0.;
289 nrow = cpl_table_get_nrow(skyspec);
290 cpl_table_new_column(skyspec,
"f_group", CPL_TYPE_DOUBLE);
291 cpl_table_new_column(skyspec,
"f_all", CPL_TYPE_DOUBLE);
294 groupspec = cpl_table_new(0);
299 cpl_table_unselect_all(groups);
300 lcut = cpl_table_get(groupspec,
"minlam", 0, NULL);
301 cpl_table_or_selected_double(groups,
"lambda", CPL_LESS_THAN, lcut);
302 cpl_table_erase_selected(groups);
303 cpl_table_unselect_all(groups);
304 ucut = cpl_table_get(groupspec,
"minlam", cpl_table_get_nrow(groupspec)-1,
306 cpl_table_or_selected_double(groups,
"lambda", CPL_GREATER_THAN, ucut);
307 cpl_table_erase_selected(groups);
308 cpl_table_select_all(groups);
312 p = cpl_parameterlist_find_const(parlist,
"fwhm");
313 fwhm = cpl_parameter_get_double(p) *
SC_SAMPFAC;
316 p = cpl_parameterlist_find_const(parlist,
"varfwhm");
317 varfwhm = cpl_parameter_get_int(p);
327 p = cpl_parameterlist_find_const(parlist,
"meanlam");
328 meanlam = cpl_parameter_get_double(p);
332 for (i = 0; i < 2; i++) {
335 cpl_table_fill_column_window(skyspec,
"f_group", 0, nrow, 0.);
336 cpl_table_fill_column_window(skyspec,
"f_all", 0, nrow, 0.);
340 sprintf(ncolname,
"ng%c", grouptype[i]);
341 cpl_table_new_column_array(skyspec, ncolname, CPL_TYPE_DOUBLE,
343 sprintf(wcolname,
"wg%c", grouptype[i]);
344 cpl_table_new_column_array(skyspec, wcolname, CPL_TYPE_DOUBLE,
346 sprintf(dcolname,
"dg%c", grouptype[i]);
347 cpl_table_new_column(skyspec, dcolname, CPL_TYPE_INT);
351 array = cpl_array_new(
SC_COLDEPTH, CPL_TYPE_DOUBLE);
352 cpl_array_fill_window_invalid(array, 0,
SC_COLDEPTH);
353 cpl_table_fill_column_window_array(skyspec, ncolname, 0, nrow, array);
354 cpl_table_fill_column_window_array(skyspec, wcolname, 0, nrow, array);
355 cpl_array_delete(array);
358 sprintf(grouppar,
"n_group%c", grouptype[i]);
359 p = cpl_parameterlist_find_const(parlist, grouppar);
360 ngroup = cpl_parameter_get_int(p);
370 p = cpl_parameterlist_find_const(parlist,
"n_system");
371 mingroup = -cpl_parameter_get_int(p);
378 for (group = mingroup; group <= ngroup; group++) {
383 grouptype[i], group);
406 cpl_table_delete(groupspec);
409 cpl_table_erase_column(skyspec,
"f_group");
410 cpl_table_erase_column(skyspec,
"f_all");
412 return CPL_ERROR_NONE;
417 const cpl_table *skyspec)
437 int nrow = 0, npix = 0, i = 0, j = 0, k = 0;
438 double llam = 0., clam = 0., ulam = 0., dlam = 0., llim = 0., lam = 0.;
441 cpl_table_new_column(groupspec,
"lam", CPL_TYPE_DOUBLE);
442 cpl_table_new_column(groupspec,
"minlam", CPL_TYPE_DOUBLE);
443 cpl_table_new_column(groupspec,
"flux", CPL_TYPE_DOUBLE);
447 nrow = cpl_table_get_nrow(skyspec);
449 cpl_table_set_size(groupspec, npix);
452 cpl_table_fill_column_window(groupspec,
"flux", 0, npix, 0.);
456 for (i = 1; i < nrow - 1; i++) {
461 llam = cpl_table_get(skyspec,
"lambda", i - 1, NULL);
462 clam = cpl_table_get(skyspec,
"lambda", i, NULL);
468 ulam = cpl_table_get(skyspec,
"lambda", i + 1, NULL);
476 llim = (llam + clam) / 2
478 lam = llim + dlam / 2;
482 cpl_table_set(groupspec,
"lam", j, lam);
483 cpl_table_set(groupspec,
"minlam", j, llim);
493 llim = (llam + clam) / 2;
494 lam = llim + dlam / 2;
498 cpl_table_set(groupspec,
"lam", j, lam);
499 cpl_table_set(groupspec,
"minlam", j, llim);
509 llim = (clam + ulam) / 2;
510 lam = llim + dlam / 2;
514 cpl_table_set(groupspec,
"lam", j, lam);
515 cpl_table_set(groupspec,
"minlam", j, llim);
524 return CPL_ERROR_NONE;
529 cpl_table *fitpar, cpl_table *groups,
530 const cpl_parameterlist *parlist,
531 const char grouptype,
const int group)
558 const cpl_parameter *p;
560 cpl_boolean found = CPL_FALSE;
561 int nsel = 0, npix = 0, row = 0, i = 0, j = 0;
562 double llim = 0., lam = 0., lflux = 0., ulim = 0., dlam = 0., pflux = 0.;
565 cpl_table_unselect_all(groups);
566 sprintf(groupcol,
"group%c", grouptype);
567 nsel = cpl_table_or_selected_int(groups, groupcol, CPL_EQUAL_TO, group);
570 npix = cpl_table_get_nrow(groupspec);
571 cpl_table_fill_column_window(groupspec,
"flux", 0, npix, 0);
575 if (grouptype ==
'B') {
576 p = cpl_parameterlist_find_const(parlist,
"n_groupA");
577 row = cpl_parameter_get_int(p) + group - 1;
582 cpl_table_set(fitpar,
"relevance", row, 0);
583 cpl_table_set(fitpar,
"fit", row, 0);
585 return CPL_ERROR_NONE;
590 llim = cpl_table_get(groupspec,
"lam", 0, NULL);
591 for (i = 0; i < cpl_table_get_nrow(groups); i++) {
592 if (cpl_table_is_selected(groups, i) == 1) {
593 lam = cpl_table_get(groups,
"lambda", i, NULL);
594 lflux = cpl_table_get(groups,
"flux", i, NULL);
597 if (j >= npix - 1)
break;
598 ulim = cpl_table_get(groupspec,
"lam", j+1, NULL);
599 if (lam >= llim && lam < ulim) {
602 pflux = cpl_table_get(groupspec,
"flux", j, NULL);
603 pflux += lflux / dlam;
604 cpl_table_set(groupspec,
"flux", j, pflux);
609 }
while (found == CPL_FALSE && j < npix - 1);
614 cpl_table_select_all(groups);
616 return CPL_ERROR_NONE;
621 const double reflam,
const double speedpar)
647 cpl_array *flux = NULL, *convflux = NULL, *kernel = NULL;
649 int m = 0, range[2] = {0, 0}, i = 0;
650 double llam = 0., ulam = 0., clam = 0., cfwhm = 0.;
654 m = cpl_table_get_nrow(groupspec);
656 sprintf(errtxt,
"%s: cpl_table *groupspec", SC_ERROR_NDA_TXT);
657 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
662 sprintf(errtxt,
"%s: fwhm < 0", SC_ERROR_IIP_TXT);
663 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
666 sprintf(errtxt,
"%s: reflam <= 0", SC_ERROR_IIP_TXT);
667 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
670 sprintf(errtxt,
"%s: speedpar < 0", SC_ERROR_IIP_TXT);
671 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
675 if (cpl_table_has_column(groupspec,
"lam") != 1) {
676 sprintf(errtxt,
"%s: cpl_table *groupspec (no 'lam' column)",
678 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
680 if (cpl_table_has_column(groupspec,
"flux") != 1) {
681 sprintf(errtxt,
"%s: cpl_table *groupspec (no 'flux' column)",
683 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
687 vec = cpl_table_get_data_double(groupspec,
"flux");
688 flux = cpl_array_new(m, CPL_TYPE_DOUBLE);
689 cpl_array_copy_data_double(flux, vec);
692 convflux = cpl_array_new(m, CPL_TYPE_DOUBLE);
693 cpl_array_fill_window_double(convflux, 0, m, 0.);
696 lam = cpl_table_get_data_double(groupspec,
"lam");
699 kernel = cpl_array_new(0, CPL_TYPE_DOUBLE);
706 llam = lam[range[0]];
707 ulam = llam * (1 + speedpar);
711 while (i < m && lam[i] < ulam) {
721 ulam = lam[range[1]];
722 clam = (ulam + llam) / 2;
725 if (speedpar == HUGE_VAL) {
728 cfwhm = fwhm * clam / reflam;
738 range[0] = range[1] + 1;
740 }
while (range[0] < m);
743 vec = cpl_array_get_data_double(convflux);
744 cpl_table_copy_data_double(groupspec,
"flux", vec);
747 cpl_array_delete(flux);
748 cpl_array_delete(convflux);
749 cpl_array_delete(kernel);
751 return CPL_ERROR_NONE;
774 int nkpix = 0, k = 0, nx = 0, refpix = 0, i = 0;
776 double sigma = 0, xmax = 0, xmin = 0, dx = 0, x = 0, sum = 0;
781 sprintf(errtxt,
"%s: fwhm < 0", SC_ERROR_IIP_TXT);
782 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
786 sigma = fwhm / CPL_MATH_FWHM_SIG;
789 nkpix = 2 * ceil(fwhm *
SC_KERNFAC / 2 - 0.5) + 1;
790 cpl_array_set_size(kernel, nkpix);
794 cpl_array_set(kernel, 0, 1.);
795 return CPL_ERROR_NONE;
799 cpl_array_fill_window_double(kernel, 0, nkpix, 0.);
800 kern = cpl_array_get_data_double(kernel);
813 refpix = floor(nkpix / 2.);
817 for (k = nkpix - 1; k >= refpix; k--) {
833 for (i = 0; i < nx; i++) {
834 kern[k] += exp(-0.5 * pow(x / sigma, 2));
838 kern[k] /= (double) nx;
849 for (k = refpix - 1; k >= 0; k--) {
850 kern[k] = kern[nkpix - k - 1];
855 for (k = 0; k < nkpix; k++) {
863 for (k = 0; k < nkpix; k++) {
867 return CPL_ERROR_NONE;
872 const cpl_table *groupspec)
893 int nrow = 0, npix = 0, jmin = 0, jmax = 0, jrow = 0, j = 0, k = 0, i = 0;
895 const double * g_flux = NULL;
898 nrow = cpl_table_get_nrow(skyspec);
899 npix = cpl_table_get_nrow(groupspec);
906 sprintf(errtxt,
"%s: cpl_table *groupspec (unexpected pixel number)",
908 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
913 if (cpl_table_count_invalid(groupspec,
"flux") == 0)
914 g_flux = cpl_table_get_data_double_const(groupspec,
"flux");
915 for (j = jmin; j <= jmax; j++) {
918 g_flux[j] : cpl_table_get(groupspec,
"flux", j, NULL);
922 cpl_table_set(skyspec,
"f_group", i, flux);
930 cpl_table_add_columns(skyspec,
"f_all",
"f_group");
932 return CPL_ERROR_NONE;
937 const char grouptype,
960 cpl_array *narray = NULL, *warray = NULL;
962 int nrow = 0, i = 0, size = 0, nfree = 0, ngroup = 0;
966 sprintf(ncolname,
"ng%c", grouptype);
967 sprintf(wcolname,
"wg%c", grouptype);
970 nrow = cpl_table_get_nrow(skyspec);
974 for (i = 0; i < nrow; i++) {
976 flux = cpl_table_get(skyspec,
"f_group", i, NULL);
981 narray = cpl_array_duplicate(cpl_table_get_array(skyspec,
983 warray = cpl_array_duplicate(cpl_table_get_array(skyspec,
987 size = cpl_array_get_size(narray);
991 nfree = cpl_array_count_invalid(narray);
998 cpl_table_set_column_depth(skyspec, ncolname, size+1);
999 cpl_table_set_column_depth(skyspec, wcolname, size+1);
1000 cpl_array_set_size(narray, size+1);
1001 cpl_array_set_size(warray, size+1);
1005 ngroup = size - nfree + 1;
1008 cpl_array_set(narray, ngroup-1, group);
1009 cpl_array_set(warray, ngroup-1, flux);
1012 cpl_table_set_array(skyspec, ncolname, i, narray);
1013 cpl_table_set_array(skyspec, wcolname, i, warray);
1016 cpl_array_delete(narray);
1017 cpl_array_delete(warray);
1023 return CPL_ERROR_NONE;
1028 const char grouptype)
1047 cpl_array *warray = NULL;
1049 int nrow = 0, i = 0;
1050 double sumflux = 0.;
1053 sprintf(wcolname,
"wg%c", grouptype);
1056 nrow = cpl_table_get_nrow(skyspec);
1060 for (i = 0; i < nrow; i++) {
1062 sumflux = cpl_table_get(skyspec,
"f_all", i, NULL);
1067 warray = cpl_array_duplicate(cpl_table_get_array(skyspec,
1070 cpl_array_divide_scalar(warray, sumflux);
1076 cpl_table_set_array(skyspec, wcolname, i, warray);
1079 cpl_array_delete(warray);
1085 return CPL_ERROR_NONE;
1090 const cpl_parameterlist *parlist,
1091 const char grouptype)
1113 const cpl_parameter *p;
1114 cpl_array *narray = NULL, *warray = NULL;
1117 int nrow = 0, initid = -99, depth = 0, i = 0, n = 0, j = 0, group = 0;
1119 double weightlim = 0., mwgroup = 0., wgroup = 0.;
1122 sprintf(ncolname,
"ng%c", grouptype);
1123 sprintf(wcolname,
"wg%c", grouptype);
1124 sprintf(dcolname,
"dg%c", grouptype);
1127 nrow = cpl_table_get_nrow(skyspec);
1130 cpl_table_fill_column_window(skyspec, dcolname, 0, nrow, initid);
1133 depth = cpl_table_get_column_depth(skyspec, ncolname);
1137 return CPL_ERROR_NONE;
1141 p = cpl_parameterlist_find_const(parlist,
"weightlim");
1142 weightlim = cpl_parameter_get_double(p);
1146 for (i = 0; i < nrow; i++) {
1149 narray = cpl_array_duplicate(cpl_table_get_array(skyspec, ncolname,
1151 warray = cpl_array_duplicate(cpl_table_get_array(skyspec, wcolname,
1155 n = depth - cpl_array_count_invalid(narray);
1159 cpl_array_delete(narray);
1160 cpl_array_delete(warray);
1165 for (mwgroup = 0., j = 0; j < n; j++) {
1166 group = cpl_array_get(narray, j, NULL);
1167 wgroup = cpl_array_get(warray, j, NULL);
1168 if (wgroup > mwgroup) {
1176 if (mwgroup >= weightlim) {
1177 cpl_table_set(skyspec, dcolname, i, mgroup);
1181 cpl_array_delete(narray);
1182 cpl_array_delete(warray);
1186 return CPL_ERROR_NONE;
cpl_error_code sc_basic_convolvewindow(cpl_array *convflux, const cpl_array *flux, const int range[2], const cpl_array *kernel)
cpl_error_code sc_weights_fillgrouparrays(cpl_table *skyspec, const char grouptype, const int group)
cpl_error_code sc_weights_createwavegrid(cpl_table *groupspec, const cpl_table *skyspec)
cpl_error_code sc_weights_calckernel(cpl_array *kernel, const double fwhm)
cpl_error_code sc_weights_normpixcontrib(cpl_table *skyspec, const char grouptype)
cpl_error_code sc_weights(cpl_table *skyspec, cpl_table *fitpar, cpl_table *groups, cpl_parameterlist *parlist)
cpl_error_code sc_weights_getgrouplines(cpl_table *groupspec, cpl_table *fitpar, cpl_table *groups, const cpl_parameterlist *parlist, const char grouptype, const int group)
cpl_error_code sc_weights_getpixcontrib(cpl_table *skyspec, cpl_table *fitpar, cpl_table *groups, const cpl_parameterlist *parlist)
cpl_error_code sc_weights_rebinspec(cpl_table *skyspec, const cpl_table *groupspec)
cpl_error_code sc_weights_getdomgroups(cpl_table *skyspec, const cpl_parameterlist *parlist, const char grouptype)
cpl_error_code sc_weights_initfitpar(cpl_table *fitpar, cpl_parameterlist *parlist, const cpl_table *groups)
cpl_error_code sc_weights_convolve(cpl_table *groupspec, const double fwhm, const double reflam, const double speedpar)