91 cpl_error_code err_code = CPL_ERROR_NONE;
95 double *xr, *yr, *xo, *yo;
97 long nrows = cpl_table_get_nrow(spec),
104 if (cpl_table_has_column(spec,
"cflux") == 0) {
105 cpl_table_new_column(spec,
"cflux", CPL_TYPE_DOUBLE);
107 if (cpl_table_has_column(spec,
"lflux") == 0) {
108 cpl_table_new_column(spec,
"lflux", CPL_TYPE_DOUBLE);
112 xr = (
double *)calloc(nrows,
sizeof(
double));
113 yr = (
double *)calloc(nrows,
sizeof(
double));
114 xo = (
double *)calloc(nrows,
sizeof(
double));
115 yo = (
double *)calloc(nrows,
sizeof(
double));
118 for (i = 0; i < nrows; i++) {
119 *(xo+i) = cpl_table_get_double(spec,
"lambda", i, NULL);
121 if (cpl_table_get_int(spec,
"class", i, NULL) == 0) {
122 *(xr+ncont) = cpl_table_get_double(spec,
"lambda", i, NULL);
123 *(yr+ncont) = cpl_table_get_double(spec,
"flux", i, NULL);
129 tab = cpl_table_new(ncont);
130 cpl_table_new_column(tab,
"cflux", CPL_TYPE_DOUBLE);
131 for (i = 0; i < ncont; i++) {
132 cpl_table_set_double(tab,
"cflux", i, *(yr+i));
135 for (i = 0; i < ncont; i++) {
136 *(yr+i) = cpl_table_get_double(tab,
"cflux", i, NULL);
138 cpl_table_delete(tab);
144 for (i = 0; i < nrows; i++) {
145 if (cpl_table_get_int(spec,
"class", i, NULL) == 0) {
147 cpl_table_set_double(spec,
"cflux", i,
148 cpl_table_get_double(spec,
"flux", i, NULL));
151 cpl_table_set_double(spec,
"cflux", i, *(yo + i));
154 cpl_table_set_double(spec,
"lflux", i,
155 cpl_table_get_double(spec,
"flux", i, NULL));
159 cpl_table_subtract_columns(spec,
"lflux",
"cflux");
172 const cpl_table *linetab,
173 const double fluxlim,
174 const cpl_parameterlist *parlist)
199 cpl_table *selgroups, *subtab = NULL;
200 const cpl_parameter *p;
212 lam_spec0, lam_spec1,
223 selgroups = cpl_table_duplicate(groups);
226 n = cpl_table_get_nrow(spec);
227 m = cpl_table_get_nrow(selgroups);
230 lam_spec0 = cpl_table_get_column_min(spec,
"lambda");
231 lam_spec1 = cpl_table_get_column_max(spec,
"lambda");
232 cpl_table_unselect_all(selgroups);
233 for (i = 0; i < m; i++) {
234 lam_list = cpl_table_get_double(selgroups,
"lambda", i, NULL);
235 if (lam_list < lam_spec0 || lam_list > lam_spec1) {
236 cpl_table_select_row(selgroups, i);
239 cpl_table_erase_selected(selgroups);
242 m = cpl_table_get_nrow(selgroups);
245 dlam = cpl_table_get_double(spec,
"lambda", 1, NULL) -
246 cpl_table_get_double(spec,
"lambda", 0, NULL);
249 p = cpl_parameterlist_find_const(parlist,
"fwhm");
250 fwhm = cpl_parameter_get_double(p);
253 p = cpl_parameterlist_find_const(parlist,
"varfwhm");
254 varfwhm = cpl_parameter_get_int(p);
257 p = cpl_parameterlist_find_const(parlist,
"meanlam");
258 meanlam = cpl_parameter_get_double(p);
261 for (i = 0; i < m; i++) {
262 flux = cpl_table_get_double(selgroups,
"flux", i, NULL) /
263 CPL_MATH_SQRT2PI / CPL_MATH_SIG_FWHM / fwhm / dlam;
266 lam_list = cpl_table_get_double(selgroups,
"lambda", i, NULL);
267 flux *= meanlam / lam_list;
269 cpl_table_set_double(selgroups,
"flux", i, flux);
273 subtab = cpl_table_duplicate(spec);
274 cpl_table_new_column(subtab,
"flux_list", CPL_TYPE_DOUBLE);
275 sc_basic_rebin(subtab,
"lambda",
"flux_list", selgroups,
"lambda",
279 meanflux = cpl_table_get_column_mean(subtab,
"flux_list");
280 ratio = cpl_table_get_column_mean(subtab,
"lflux") / meanflux;
283 for (meanlflux = 0., i = 0; i < n; i++) {
284 if ((lflux = cpl_table_get(subtab,
"lflux", i, NULL)) > 0) {
288 ratio = meanlflux / meanflux;
290 cpl_table_multiply_scalar(selgroups,
"flux", ratio);
293 if (cpl_table_get_nrow(linetab) == 0) {
295 minflux = fluxlim * cpl_table_get_column_mean(selgroups,
"flux");
298 minflux = fluxlim * cpl_table_get_column_median(linetab,
"peak_flux");
300 if (minflux > cpl_table_get_column_max(selgroups,
"flux")) {
301 minflux = fluxlim * cpl_table_get_column_mean(selgroups,
"flux");
307 for (i = 0, j = 0; i < n-1; ) {
315 lam_spec0 = cpl_table_get_double(spec,
"lambda", i, NULL);
316 lam_spec1 = cpl_table_get_double(spec,
"lambda", i+1, NULL);
319 lam_list = cpl_table_get_double(selgroups,
"lambda", j, NULL);
320 flux = cpl_table_get_double(selgroups,
"flux", j, NULL);
322 if (lam_list < lam_spec0) {
328 if (flux < minflux) {
334 if (lam_list < lam_spec1) {
337 if (lam_list-lam_spec0 < lam_spec1-lam_list) {
345 if (cpl_table_get_int(spec,
"class", mid, NULL) != 3) {
346 cpl_table_set_int(spec,
"class", mid, 2);
353 if (lam_list > lam_spec0) {
362 for (i = 0; i < n-1; i++) {
364 if (cpl_table_get_int(spec,
"class", i, NULL) >= 2) {
370 lpix = nsig * fwhm / CPL_MATH_FWHM_SIG;
373 flux = cpl_table_get_double(spec,
"flux", i, NULL);
374 lam_list = cpl_table_get_double(spec,
"lambda", i, NULL);
378 flux *= lam_list / meanlam;
382 if (flux > 10 * fluxlim) {
388 lpix *= lam_list / meanlam;
393 for (j = 1; j < (int) floor(lpix + 0.5); j++) {
396 if (cpl_table_get_int(spec,
"class", i-j, NULL) < 1) {
398 cpl_table_set_int(spec,
"class", i-j, 1);
403 if (cpl_table_get_int(spec,
"class", i+j, NULL) < 1) {
405 cpl_table_set_int(spec,
"class", i+j, 1);
415 cpl_table_delete(subtab);
416 cpl_table_delete(selgroups);
418 return cpl_error_get_code();
442 int nall = 0, ncont = 0, flag = 0;
443 double minlam = 0., maxlam = 0., range = 0., crange = 0.;
447 double minfpix = 0.2;
448 double minfrange = 0.9;
451 nall = cpl_table_get_nrow(spec);
457 cpl_table_unselect_all(spec);
458 cpl_table_or_selected_int(spec,
"class", CPL_EQUAL_TO, 0);
459 subtab = cpl_table_extract_selected(spec);
460 cpl_table_select_all(spec);
463 ncont = cpl_table_get_nrow(subtab);
464 if ((
double) ncont / (
double) nall < minfpix) {
469 minlam = cpl_table_get(spec,
"lambda", 0, NULL);
470 maxlam = cpl_table_get(spec,
"lambda", nall-1, NULL);
471 range = maxlam - minlam;
474 if (ncont >= 2 * count) {
475 minlam = cpl_table_get(subtab,
"lambda", count-1, NULL);
476 maxlam = cpl_table_get(subtab,
"lambda", ncont-count, NULL);
477 crange = maxlam - minlam;
478 if (crange / range < minfrange) {
486 cpl_table_delete(subtab);
488 cpl_error_code err = cpl_error_get_code();
489 if (err != CPL_ERROR_NONE) {
490 cpl_errorstate_dump(0, CPL_FALSE, NULL);
514 int n = 0, i = 0, mask = 0;
518 n = cpl_table_get_nrow(spec);
521 for (i = n - 1; i >= 0; i--) {
522 lam = cpl_table_get(spec,
"lambda", i, NULL);
523 mask = cpl_table_get(spec,
"class", i, NULL);
525 cpl_table_set(spec,
"class", i, 0);
529 return cpl_error_get_code();
cpl_error_code sc_basic_rebin(cpl_table *outspec, const char *outlam, const char *outflux, const cpl_table *inspec, const char *inlam, const char *influx)
cpl_error_code sc_basic_filtermedian(cpl_table *spec, const char *colname, const int npix)
cpl_error_code sc_basic_interpollin(const double *x_out, double *y_out, const long n_out, const double *x_ref, const double *y_ref, const long n_ref, const int extrapolate)
int sc_contsub_check(cpl_table *spec)
cpl_error_code sc_contsub_skipthermir(cpl_table *spec)
cpl_error_code sc_contsub_identcont(cpl_table *spec, const cpl_table *groups, const cpl_table *linetab, const double fluxlim, const cpl_parameterlist *parlist)
cpl_error_code sc_contsub(cpl_table *spec)