60cpl_error_code
sc_mpfit(mp_result *result, cpl_table *scispec,
61 cpl_table *skyspec, cpl_table *fitpar,
62 const cpl_parameterlist *parlist)
128 const cpl_parameter *pp;
129 cpl_table *initfitpar = NULL, *tmpfitpar = NULL;
130 cpl_vector *sinc = NULL;
131 mp_config config = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
136 int m = 0, nmin = 0, nmax = 0, mode = 0, ncoef = 0, nloop = 0, pos = 0;
137 int imin = 0, rebintype = 0, i = 0, status = 0, j = 0, calls = 0;
138 int niter = 0, k = 0, nfit = 0, ibest = 0;
139 double wtol = 0., norm = 0., dev = 0., orignorm = 0.;
143 pp = cpl_parameterlist_find_const(parlist,
"ftol");
144 config.ftol = cpl_parameter_get_double(pp);
145 pp = cpl_parameterlist_find_const(parlist,
"xtol");
146 config.xtol = cpl_parameter_get_double(pp);
149 config.maxiter = 100;
152 fitpars.n = cpl_table_get_nrow(fitpar);
156 (
int) SC_ERROR_ISM) {
157 result->status = -99;
158 return (cpl_error_code)SC_ERROR_ISM;
162 m = cpl_table_get_nrow(scispec);
166 (
int) SC_ERROR_ISM) {
167 result->status = -99;
169 return (cpl_error_code)SC_ERROR_ISM;
177 pp = cpl_parameterlist_find_const(parlist,
"cheby_min");
178 nmin = cpl_parameter_get_int(pp);
179 pp = cpl_parameterlist_find_const(parlist,
"cheby_max");
180 nmax = cpl_parameter_get_int(pp);
188 (
int) SC_ERROR_ISM) {
189 result->status = -99;
191 return (cpl_error_code)SC_ERROR_ISM;
198 cpl_table_unselect_all(fitpar);
199 cpl_table_or_selected_string(fitpar,
"type", CPL_EQUAL_TO,
"w");
200 ncoef = cpl_table_count_selected(fitpar);
202 pos = fitpars.n - ncoef - 1;
203 cpl_table_select_all(fitpar);
216 pp = cpl_parameterlist_find_const(parlist,
"wtol");
217 wtol = 1. + cpl_parameter_get_double(pp);
220 pp = cpl_parameterlist_find_const(parlist,
"rebintype");
221 rebintype = cpl_parameter_get_int(pp);
224 sinc = cpl_vector_new(1);
225 if (rebintype == 1) {
236 sc_modsky(scispec, skyspec, fitpar, sinc, parlist);
243 sc_modsky(scispec, skyspec, fitpar, sinc, parlist);
245 for (i = 0; i < nloop; i++) {
255 cpl_table_fill_column_window(fitpar,
"err_fit", 0, fitpars.n,
257 cpl_table_fill_column_window(fitpar,
"err_est", 0, fitpars.n,
262 if (ncoef == 0 || i == 0) {
268 for (norm = 0., j = 0; j < m; j++) {
269 dev = cpl_table_get(scispec,
"dev", j, NULL);
272 result->orignorm = norm;
290 fitpars.pars, &config, (
void *) &v, result);
298 niter += result->niter;
301 cpl_table_copy_data_double(fitpar,
"value", fitpars.p);
304 for (k = 0; k < fitpars.n; k++) {
305 sprintf(type,
"%s", cpl_table_get_string(fitpar,
"type", k));
306 if (type[0] ==
'w' &&
307 cpl_table_get_int(fitpar,
"fit", k, NULL) == 1) {
308 cpl_table_set(fitpar,
"err_fit", k, result->xerror[k]);
313 sc_modsky(scispec, skyspec, fitpar, sinc, parlist);
319 orignorm = result->orignorm;
327 sc_modsky(scispec, skyspec, fitpar, sinc, parlist);
330 cpl_table_unselect_all(fitpar);
331 cpl_table_or_selected_string(fitpar,
"type", CPL_NOT_EQUAL_TO,
"w");
332 cpl_table_and_selected_int(fitpar,
"fit", CPL_EQUAL_TO, 1);
333 nfit = cpl_table_count_selected(fitpar);
334 cpl_table_select_all(fitpar);
344 for (norm = 0., j = 0; j < m; j++) {
345 dev = cpl_table_get(scispec,
"dev", j, NULL);
348 result->bestnorm = norm;
365 initfitpar = cpl_table_duplicate(fitpar);
369 fitpars.pars, &config, (
void *) &v, result);
373 cpl_table_delete(initfitpar);
379 niter += result->niter;
382 cpl_table_copy_data_double(fitpar,
"value", fitpars.p);
385 for (k = 0; k < fitpars.n; k++) {
386 sprintf(type,
"%s", cpl_table_get_string(fitpar,
"type", k));
387 if (type[0] !=
'w' &&
388 cpl_table_get_int(fitpar,
"fit", k, NULL) == 1) {
389 cpl_table_set(fitpar,
"err_fit", k, result->xerror[k]);
395 cpl_table_delete(initfitpar);
398 sc_modsky(scispec, skyspec, fitpar, sinc, parlist);
403 cpl_msg_debug(cpl_func,
"bestnorm: %.3e", result->bestnorm);
407 if (mode == 0 && ncoef > 0) {
412 if (i >= imin && result->bestnorm * wtol >= tmpresult.bestnorm) {
414 if (result->bestnorm >= tmpresult.bestnorm) {
421 }
else if (i == nloop - 1 &&
422 result->bestnorm < tmpresult.bestnorm) {
431 if (i == nloop - 1) {
438 if (mode == 0 && i < nloop - 1) {
441 if (i == 0 || result->bestnorm < tmpresult.bestnorm) {
444 cpl_table_delete(tmpfitpar);
445 tmpfitpar = cpl_table_duplicate(fitpar);
455 cpl_table_set(fitpar,
"relevance", pos, 1);
456 cpl_table_set(fitpar,
"fit", pos, 1);
459 cpl_table_set(fitpar,
"relevance", pos, 1);
462 cpl_table_set(fitpar,
"fit", pos, 1);
471 cpl_msg_debug(cpl_func,
"STOP -> No wavegrid correction");
473 cpl_msg_debug(cpl_func,
"STOP -> Take results of degree %d", ibest);
478 result->orignorm = orignorm;
479 result->niter = niter;
489 sc_modsky(scispec, skyspec, fitpar, sinc, parlist);
498 if (status > 0 && mode == 0) {
500 cpl_table_delete(tmpfitpar);
503 cpl_vector_delete(sinc);
507 result->status = status;
508 sprintf(errtxt,
"%s: mpfit (internal error %d)", SC_ERROR_EIS_TXT,
510 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_EIS,
"%s", errtxt);
513 return CPL_ERROR_NONE;
544 cpl_table *scispec, *skyspec, *fitpar;
546 const cpl_parameterlist *parlist;
555 scispec = v->scispec;
556 skyspec = v->skyspec;
559 parlist = v->parlist;
562 cpl_table_copy_data_double(fitpar,
"value", p);
565 sc_modsky(scispec, skyspec, fitpar, sinc, parlist);
568 assert(m == cpl_table_get_nrow(scispec));
569 cpl_table_fill_invalid_double(scispec,
"dev", 0.);
570 memcpy(dy, cpl_table_get_data_double_const(scispec,
"dev"),
571 cpl_table_get_nrow(scispec) *
sizeof(
double));
573 return CPL_ERROR_NONE;
580 const cpl_parameterlist *parlist)
629 const cpl_parameter *p;
630 double fwhm = 0., siglim = 0., fitlim = 0.;
633 cpl_array *ratio, *tmpratio;
634 cpl_boolean ispeak = CPL_FALSE;
635 int nsci = 0, pix = 0, npeak = 0;
636 double lflux = 0., mlflux = 0., rat = 0., initrat = 0., rms = 0.;
640 int nsky = 0, i = 0, io = 0, im = 0;
643 cpl_table *tmpfitpar;
647 char grouptype[2] =
"AB", ngroupid[
SC_LENLINE+1];
651 cpl_array *corrsyst, *corrfac, *meanratio, *groupsum, *maxrms[2];
652 int nsyst = 0, syst = 0, gsum = 0;
653 double ratsum = 0., fac = 0., meanrat = 0.;
656 cpl_array *gratio, *sgratio;
657 int ngroup[2] = {0, 0}, ming = 0, g = 0;
658 double mrat = 0., msig = 0., omrat = 0.;
663 int maxpix = 0, d = 0, nlin = 0, n = 0, k = 0;
666 cpl_array *selpix, *linid;
667 int sum = 0, j = 0, nlinpix = 0, nclippix = 0;
670 nsci = cpl_table_get_nrow(scispec);
671 nsky = cpl_table_get_nrow(skyspec);
675 ratio = cpl_array_new(nsci, CPL_TYPE_DOUBLE);
677 for (pix = 0, npeak = 0; pix < nsci; pix++) {
678 if (cpl_table_get(scispec,
"class", pix, NULL) >= 1 &&
679 cpl_table_get(scispec,
"weight", pix, NULL) > 0. &&
680 cpl_table_get(scispec,
"mweight", pix, NULL) > 0.) {
682 lflux = cpl_table_get(scispec,
"lflux", pix, NULL);
683 mlflux = cpl_table_get(scispec,
"mlflux", pix, NULL);
687 rat = lflux / mlflux;
689 cpl_array_set_double(ratio, pix, rat);
691 if (cpl_table_get(scispec,
"class", pix, NULL) >= 2) {
696 cpl_array_set_invalid(ratio, pix);
702 cpl_array_delete(ratio);
703 return CPL_ERROR_NONE;
707 p = cpl_parameterlist_find_const(parlist,
"fwhm");
708 fwhm = cpl_parameter_get_double(p);
711 maxpix = (int) floor(fwhm / 2. + 0.5);
714 for (pix = 0; pix < nsci; pix++) {
715 if (cpl_array_is_valid(ratio, pix) == 1 &&
716 cpl_table_get(scispec,
"class", pix, NULL) == 1) {
718 for (d = -maxpix; d <= maxpix; d++) {
719 if (pix + d >= 0 && pix + d < nsci &&
720 cpl_table_get(scispec,
"class", pix + d, NULL) >= 2 &&
721 cpl_array_is_valid(ratio, pix + d) == 1) {
726 if (ispeak == CPL_FALSE) {
727 cpl_array_set_invalid(ratio, pix);
733 tmpfitpar = cpl_table_duplicate(fitpar);
736 tmpratio = cpl_array_duplicate(ratio);
739 for (pix = 0; pix < nsci; pix++) {
740 if (cpl_array_is_valid(tmpratio, pix) == 1 &&
741 cpl_table_get(scispec,
"class", pix, NULL) == 1) {
742 cpl_array_set_invalid(tmpratio, pix);
750 p = cpl_parameterlist_find_const(parlist,
"siglim");
751 siglim = cpl_parameter_get_double(p);
755 for (pix = 0; pix < nsci; pix++) {
756 rat = cpl_array_get(ratio, pix, NULL);
757 if (rat < initrat - siglim * rms ||
758 rat > initrat + siglim * rms) {
759 cpl_array_set_invalid(ratio, pix);
764 cpl_table_set_column_invalid(skyspec,
"frat", 0, nsky);
767 for (i = 0; i < nsky; i++) {
768 pix = cpl_table_get(skyspec,
"mpix", i, NULL);
769 if (pix >= 0 && pix < nsci && cpl_array_is_valid(ratio, pix) == 1) {
770 rat = cpl_array_get(ratio, pix, NULL);
771 cpl_table_set(skyspec,
"frat", i, rat);
776 p = cpl_parameterlist_find_const(parlist,
"n_system");
777 nsyst = cpl_parameter_get_int(p);
778 corrsyst = cpl_array_new(nsyst, CPL_TYPE_DOUBLE);
779 cpl_array_fill_window(corrsyst, 0, nsyst, 1);
782 corrfac = cpl_array_new(nsyst, CPL_TYPE_DOUBLE);
786 meanratio = cpl_array_new(nsyst + 1, CPL_TYPE_DOUBLE);
787 cpl_array_fill_window(meanratio, 0, nsyst + 1, 0.);
788 groupsum = cpl_array_new(nsyst + 1, CPL_TYPE_INT);
789 cpl_array_fill_window(groupsum, 0, nsyst + 1, 0);
792 p = cpl_parameterlist_find_const(parlist,
"fitlim");
793 fitlim = cpl_parameter_get_double(p);
798 for (h = 0; h < 2; h++) {
801 sprintf(ngroupid,
"n_group%c", grouptype[h]);
802 p = cpl_parameterlist_find_const(parlist, ngroupid);
803 ngroup[h] = cpl_parameter_get_int(p);
806 if (ngroup[h] == 0) {
811 sprintf(dcolname,
"dg%c", grouptype[h]);
816 p = cpl_parameterlist_find_const(parlist,
"n_system");
817 ming = -cpl_parameter_get_int(p);
823 maxrms[h] = cpl_array_new(nsyst, CPL_TYPE_DOUBLE);
824 cpl_array_fill_window(maxrms[h], 0, nsyst, 0.);
828 for (g = ming; g <= ngroup[h]; g++) {
831 cpl_table_unselect_all(skyspec);
832 cpl_table_or_selected_int(skyspec, dcolname, CPL_EQUAL_TO, g);
833 cpl_table_and_selected_double(skyspec,
"frat", CPL_NOT_EQUAL_TO,
835 selpix = cpl_table_where_selected(skyspec);
836 cpl_table_and_selected_int(skyspec,
"class",
837 CPL_NOT_LESS_THAN, 2);
838 selpeak = cpl_table_where_selected(skyspec);
839 cpl_table_select_all(skyspec);
842 sum = cpl_array_get_size(selpix);
843 nlin = cpl_array_get_size(selpeak);
858 linid = cpl_array_new(sum, CPL_TYPE_INT);
859 io = cpl_array_get(selpeak, 0, NULL);
860 for (j = 0, k = 1; k <= nlin; k++) {
864 i = cpl_array_get(selpeak, k, NULL);
865 im = (int) ceil(0.5 * (io + i));
868 if (cpl_array_get(selpix, j, NULL) < im) {
869 cpl_array_set(linid, j, k);
879 gratio = cpl_array_new(sum, CPL_TYPE_DOUBLE);
880 for (j = 0; j < sum; j++) {
881 i = cpl_array_get(selpix, j, NULL);
882 cpl_array_set(gratio, j,
883 cpl_table_get(skyspec,
"frat", i, NULL));
887 sgratio = cpl_array_duplicate(gratio);
896 for (n = 0, k = 0; k < nlin; k++) {
899 for (nlinpix = 0, nclippix = 0, j = 0; j < sum; j++) {
900 if (cpl_array_get(linid, j, NULL) == k + 1) {
901 if (cpl_array_is_valid(gratio, j) == 1) {
911 if (nlin == 2 && nlinpix == 0) {
912 for (j = 0; j < sum; j++) {
913 if (cpl_array_get(linid, j, NULL) == k + 1 &&
914 cpl_array_is_valid(gratio, j) != 1) {
915 cpl_array_set(gratio, j,
916 cpl_array_get(sgratio, j,
926 if (nclippix > 2 * nlinpix) {
927 for (nlinpix = 0, j = 0; j < sum; j++) {
928 if (cpl_array_get(linid, j, NULL) == k + 1 &&
929 cpl_array_is_valid(gratio, j) == 1) {
930 cpl_array_set_invalid(gratio, j);
946 mrat = cpl_array_get(gratio, 0, NULL);
949 mrat = cpl_array_get_mean(gratio);
950 msig = cpl_array_get_stdev(gratio);
954 for (j = 0; j < sum; j++) {
955 if (cpl_array_is_valid(gratio, j) == 1) {
956 i = cpl_array_get(selpix, j, NULL);
957 pix = cpl_table_get(skyspec,
"mpix", i, NULL);
958 if (h == 0 && g > 0 && pix >= 0 && pix < nsci) {
959 cpl_table_set(scispec,
"sigclip", pix, 0);
965 cpl_array_delete(linid);
966 cpl_array_delete(sgratio);
967 cpl_array_delete(gratio);
977 cpl_array_delete(selpix);
978 cpl_array_delete(selpeak);
981 pig = cpl_table_get_data_int(skyspec, dcolname);
982 for (i = 0; i < nsky; i++) {
983 if (pig[i] == g && cpl_table_is_valid(skyspec, dcolname, i) &&
984 cpl_table_is_valid(skyspec,
"frat", i)) {
985 rat = cpl_table_get(skyspec,
"frat", i, NULL);
986 cpl_table_set(skyspec,
"frat", i, rat / mrat);
994 row = ngroup[0] + g - 1;
999 if (h == 1 && g > 0 && nlin == 0) {
1000 syst = cpl_table_get(fitpar,
"system", row, NULL);
1002 cpl_array_set(corrsyst, syst-1, 0);
1007 if (g > 0 && n >= 2) {
1008 syst = cpl_table_get(fitpar,
"system", row, NULL);
1010 if (rms > cpl_array_get(maxrms[h], syst-1, NULL)) {
1011 cpl_array_set(maxrms[h], syst-1, rms);
1018 cpl_table_set(fitpar,
"value", row, mrat);
1020 cpl_table_set(fitpar,
"err_est", row, msig);
1022 cpl_table_set(fitpar,
"N_lin", row, n);
1025 cpl_array_set(corrfac, -g-1, mrat);
1033 for (row = 0; row < ngroup[0] + ngroup[1]; row++) {
1034 syst = cpl_table_get(fitpar,
"system", row, NULL);
1035 n = cpl_table_get(fitpar,
"N_lin", row, NULL);
1036 if (row >= ngroup[0]) {
1041 if (syst > 0 && n == 1) {
1042 cpl_table_set(fitpar,
"err_est", row,
1043 cpl_array_get(maxrms[h], syst-1, NULL));
1048 for (row = ngroup[0]; row < ngroup[0] + ngroup[1]; row++) {
1049 syst = cpl_table_get(fitpar,
"system", row, NULL);
1050 if (syst > 0 && cpl_array_get(corrsyst, syst-1, NULL) == 0) {
1051 cpl_table_set(fitpar,
"value", row, 1.);
1058 for (row = 0; row < ngroup[0] + ngroup[1]; row++) {
1059 syst = cpl_table_get(fitpar,
"system", row, NULL);
1063 mrat = cpl_table_get(fitpar,
"value", row, NULL);
1064 msig = cpl_table_get(fitpar,
"err_est", row, NULL);
1065 fac = cpl_array_get(corrfac, syst-1, NULL);
1067 cpl_array_get(corrsyst, syst-1, NULL) != 0) {
1068 if (row < ngroup[0]) {
1069 cpl_table_set(fitpar,
"value", row, mrat * fac);
1070 cpl_table_set(fitpar,
"err_est", row, msig * fac);
1072 cpl_table_set(fitpar,
"value", row, mrat / fac);
1073 cpl_table_set(fitpar,
"err_est", row, msig / fac);
1079 for (row = 0; row < ngroup[0] + ngroup[1]; row++) {
1080 n = cpl_table_get(fitpar,
"N_lin", row, NULL);
1082 omrat = cpl_table_get(tmpfitpar,
"value", row, NULL);
1083 mrat = cpl_table_get(fitpar,
"value", row, NULL);
1084 nmrat = omrat * mrat;
1090 cpl_table_set(fitpar,
"value", row, nmrat);
1091 msig = cpl_table_get(fitpar,
"err_est", row, NULL);
1093 cpl_table_set(fitpar,
"err_est", row, omrat * msig);
1100 for (row = 0; row < ngroup[0]; row++) {
1101 n = cpl_table_get(fitpar,
"N_lin", row, NULL);
1103 syst = cpl_table_get(fitpar,
"system", row, NULL);
1104 mrat = cpl_table_get(fitpar,
"value", row, NULL);
1106 ratsum = cpl_array_get(meanratio, syst, NULL) + mrat;
1107 cpl_array_set(meanratio, syst, ratsum);
1108 gsum = cpl_array_get(groupsum, syst, NULL) + 1;
1109 cpl_array_set(groupsum, syst, gsum);
1112 ratsum = cpl_array_get(meanratio, 0, NULL) + mrat;
1113 cpl_array_set(meanratio, 0, ratsum);
1114 gsum = cpl_array_get(groupsum, 0, NULL) + 1;
1115 cpl_array_set(groupsum, 0, gsum);
1120 cpl_array_divide(meanratio, groupsum);
1123 for (row = 0; row < ngroup[0]; row++) {
1124 n = cpl_table_get(fitpar,
"N_lin", row, NULL);
1126 syst = cpl_table_get(fitpar,
"system", row, NULL);
1127 if (cpl_array_is_valid(meanratio, syst) != 1) {
1131 if (cpl_array_is_valid(meanratio, syst) != 1) {
1133 cpl_table_set(fitpar,
"value", row, 1.);
1135 cpl_table_set(fitpar,
"value", row,
1136 cpl_array_get(meanratio, syst, NULL));
1144 for (row = 0; row < ngroup[0]; row++) {
1145 n = cpl_table_get(fitpar,
"N_lin", row, NULL);
1146 syst = cpl_table_get(fitpar,
"system", row, NULL);
1147 if (n > 0 && n < SC_MINNLIN && syst > 0 &&
1148 cpl_array_is_valid(meanratio, syst) == 1) {
1149 mrat = cpl_table_get(fitpar,
"value", row, NULL);
1150 meanrat = cpl_array_get(meanratio, syst, NULL);
1151 if (meanrat == 0 || mrat / meanrat < 1. /
SC_MAXRELFAC ||
1153 cpl_table_set(fitpar,
"value", row, meanrat);
1161 for (row = 0; row < ngroup[0] + ngroup[1]; row++) {
1162 mrat = cpl_table_get(fitpar,
"value", row, NULL);
1163 msig = cpl_table_get(fitpar,
"err_est", row, NULL);
1164 if (mrat == 0 || msig ==
SC_DEFERRVAL || msig / mrat < fitlim) {
1165 cpl_table_set(fitpar,
"fit", row, 0);
1167 cpl_table_set(fitpar,
"fit", row, 1);
1172 cpl_array_delete(tmpratio);
1173 cpl_array_delete(ratio);
1174 cpl_array_delete(corrsyst);
1175 cpl_array_delete(corrfac);
1176 cpl_array_delete(meanratio);
1177 cpl_array_delete(groupsum);
1178 cpl_array_delete(maxrms[0]);
1179 cpl_array_delete(maxrms[1]);
1180 cpl_table_delete(tmpfitpar);
1182 return CPL_ERROR_NONE;
1209 int npar = 0, i = 0, fit = 0;
1212 npar = cpl_table_get_nrow(fitpar);
1213 if (fitpars->n != npar) {
1214 sprintf(errtxt,
"%s: # of parameters: "
1215 "scpars *fitpars != cpl_table *fitpar", SC_ERROR_IDG_TXT);
1216 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IDG,
"%s", errtxt);
1221 for (i = 0; i < npar; i++) {
1223 sprintf(type,
"%s", cpl_table_get_string(fitpar,
"type", i));
1225 fitpars->p[i] = cpl_table_get(fitpar,
"value", i, NULL);
1227 fit = cpl_table_get(fitpar,
"fit", i, NULL);
1229 if (type[0] ==
'w') {
1231 if (fittype ==
'w') {
1232 fitpars->pars[i].fixed = 1 - fit;
1234 fitpars->pars[i].fixed = 1;
1236 fitpars->pars[i].limited[0] = 0;
1237 fitpars->pars[i].limited[1] = 0;
1238 fitpars->pars[i].limits[0] = 0.;
1239 fitpars->pars[i].limits[1] = 0.;
1240 fitpars->pars[i].relstep = 0.01;
1243 if (fittype ==
'l') {
1244 fitpars->pars[i].fixed = 1 - fit;
1246 fitpars->pars[i].fixed = 1;
1248 fitpars->pars[i].limited[0] = 1;
1249 fitpars->pars[i].limited[1] = 1;
1252 fitpars->pars[i].relstep = 0.01;
1255 sprintf(name,
"%c%d", type[0],
1256 cpl_table_get_int(fitpar,
"N", i, NULL));
1257 strcpy(fitpars->pars[i].parname, name);
1260 return CPL_ERROR_NONE;
1279 cpl_boolean fl_mem = CPL_TRUE;
1287 fitpars->p = (
double *) calloc(fitpars->n,
sizeof(
double));
1288 if (fitpars->p == NULL) {
1290 sprintf(errtxt,
"%s: scpars *fitpars", SC_ERROR_ISM_TXT);
1291 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_ISM,
"%s", errtxt);
1295 fitpars->pars = (mp_par *) calloc(fitpars->n,
sizeof(mp_par));
1296 if (fitpars->pars == NULL) {
1300 sprintf(errtxt,
"%s: scpars *fitpars", SC_ERROR_ISM_TXT);
1301 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_ISM,
"%s", errtxt);
1306 for (it = 0; it < 2; it++) {
1307 for (i = 0; i < fitpars->n; i++) {
1308 fitpars->pars[i].parname = (
char *) calloc(nchar,
sizeof(
char));
1309 if (it == 0 && fitpars->pars[i].parname == NULL) {
1315 if (it == 0 && fl_mem == CPL_TRUE) {
1317 }
else if (it == 1 && fl_mem == CPL_FALSE) {
1321 free(fitpars->pars);
1322 fitpars->pars = NULL;
1323 sprintf(errtxt,
"%s: scpars *fitpars", SC_ERROR_ISM_TXT);
1324 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_ISM,
"%s",
1329 return CPL_ERROR_NONE;
1351 if (fitpars->p != NULL) {
1357 if (fitpars->pars != NULL) {
1358 for (i = 0; i < fitpars->n; i++) {
1359 free(fitpars->pars[i].parname);
1360 fitpars->pars[i].parname = NULL;
1362 free(fitpars->pars);
1363 fitpars->pars = NULL;
1369 return CPL_ERROR_NONE;
1395 result->covar = NULL;
1398 result->resid = (
double *) calloc(m,
sizeof(
double));
1399 if (result->resid == NULL) {
1400 result->status = -99;
1401 result->xerror = NULL;
1402 sprintf(errtxt,
"%s: mp_result *result", SC_ERROR_ISM_TXT);
1403 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_ISM,
"%s", errtxt);
1407 result->xerror = (
double *) calloc(n,
sizeof(
double));
1408 if (result->xerror == NULL) {
1409 result->status = -99;
1410 free(result->resid);
1411 result->resid = NULL;
1412 sprintf(errtxt,
"%s: mp_result *result", SC_ERROR_ISM_TXT);
1413 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_ISM,
"%s", errtxt);
1416 return CPL_ERROR_NONE;
1441 result->bestnorm = HUGE_VAL;
1442 result->orignorm = HUGE_VAL;
1445 result->status = 99;
1448 result->npegged = 0;
1450 for (i = 0; i < result->nfunc; i++) {
1451 result->resid[i] = 0;
1453 for (j = 0; j < result->npar; j++) {
1454 result->xerror[j] = 0;
1457 return CPL_ERROR_NONE;
1462 const mp_result *inresult)
1480 outresult->bestnorm = inresult->bestnorm;
1481 outresult->orignorm = inresult->orignorm;
1482 outresult->niter = inresult->niter;
1483 outresult->nfev = inresult->nfev;
1484 outresult->status = inresult->status;
1485 outresult->npar = inresult->npar;
1486 outresult->nfree = inresult->nfree;
1487 outresult->npegged = inresult->npegged;
1488 outresult->nfunc = inresult->nfunc;
1489 for (i = 0; i < outresult->nfunc; i++) {
1490 outresult->resid[i] = inresult->resid[i];
1492 for (j = 0; j < outresult->npar; j++) {
1493 outresult->xerror[j] = inresult->xerror[j];
1495 outresult->covar = inresult->covar;
1496 strcpy(outresult->version, inresult->version);
1498 return CPL_ERROR_NONE;
1522 if (result->resid != NULL) {
1523 free(result->resid);
1524 result->resid = NULL;
1528 if (result->xerror != NULL) {
1529 free(result->xerror);
1530 result->xerror = NULL;
1533 return CPL_ERROR_NONE;
1538 const cpl_table *initfitpar,
1539 const cpl_parameterlist *parlist)
1563 const cpl_parameter *p;
1565 int ngroupa = 0, ngroupb = 0, ngroup = 0, i = 0, syst = 0;
1566 double val = 0., err = 0., rat = 0.;
1569 p = cpl_parameterlist_find_const(parlist,
"n_groupA");
1570 ngroupa = cpl_parameter_get_int(p);
1571 p = cpl_parameterlist_find_const(parlist,
"n_groupB");
1572 ngroupb = cpl_parameter_get_int(p);
1573 ngroup = ngroupa + ngroupb;
1576 p = cpl_parameterlist_find_const(parlist,
"n_system");
1577 minrat = cpl_array_new(cpl_parameter_get_int(p), CPL_TYPE_DOUBLE);
1578 cpl_array_fill_window(minrat, 0, cpl_parameter_get_int(p), HUGE_VAL);
1583 for (i = 0; i < ngroupa; i++) {
1585 if (cpl_table_get(fitpar,
"fit", i, NULL) == 1) {
1588 val = cpl_table_get(fitpar,
"value", i, NULL);
1589 err = cpl_table_get(fitpar,
"err_fit", i, NULL);
1599 cpl_table_set(fitpar,
"value", i,
1600 cpl_table_get(initfitpar,
"value", i, NULL));
1605 syst = cpl_table_get(fitpar,
"system", i, NULL);
1606 if (syst > 0 && rat < cpl_array_get(minrat, syst - 1, NULL)) {
1607 cpl_array_set(minrat, syst - 1, rat);
1617 for (i = ngroupa; i < ngroup; i++) {
1619 if (cpl_table_get(fitpar,
"fit", i, NULL) == 1) {
1623 syst = cpl_table_get(fitpar,
"system", i, NULL);
1625 cpl_array_get(minrat, syst - 1, NULL) >
SC_MAXPARERR) {
1626 cpl_table_set(fitpar,
"value", i,
1627 cpl_table_get(initfitpar,
"value", i, NULL));
1636 cpl_array_delete(minrat);
1638 return CPL_ERROR_NONE;
cpl_error_code sc_basic_clipmean(double *mean, double *rms, cpl_array *arr, const cpl_boolean clip)
cpl_error_code sc_basic_copytable_content(cpl_table *outtab, const cpl_table *intab)
cpl_error_code sc_basic_calcsinc(cpl_vector *sinc)
cpl_error_code sc_modsky(cpl_table *scispec, cpl_table *skyspec, cpl_table *fitpar, const cpl_vector *sinc, const cpl_parameterlist *parlist)
cpl_error_code sc_mpfit_allocmempar(scpars *fitpars, const int npar)
cpl_error_code sc_mpfit_substbadfitpar(cpl_table *fitpar, const cpl_table *initfitpar, const cpl_parameterlist *parlist)
cpl_error_code sc_mpfit(mp_result *result, cpl_table *scispec, cpl_table *skyspec, cpl_table *fitpar, const cpl_parameterlist *parlist)
cpl_error_code sc_mpfit_setpar(scpars *fitpars, const cpl_table *fitpar, const char fittype)
cpl_error_code sc_mpfit_allocmemresult(mp_result *result, const int m, const int n)
cpl_error_code sc_mpfit_freememresult(mp_result *result)
cpl_error_code sc_mpfit_freemempar(scpars *fitpars)
cpl_error_code sc_mpfit_initresult(mp_result *result, const int m, const int n)
cpl_error_code sc_mpfit_modinitpar(cpl_table *fitpar, cpl_table *scispec, cpl_table *skyspec, const cpl_parameterlist *parlist)
int sc_mpfit_calcdev(int m, int n, double *p, double *dy, double **dvec, void *vars)
cpl_error_code sc_mpfit_copyresult(mp_result *outresult, const mp_result *inresult)