52#define EXECUTABLE X_OK
61 cpl_parameterlist *parlist,
62 const cpl_table *groups)
107 cpl_error_code status = CPL_ERROR_NONE;
117 double deriv1_lim=0.;
120 double flux_low_lim=0., flux_up_lim=0.;
133 cpl_table *derivatives;
145 if ( (cpl_table_has_column(input_spectrum,
"lambda")!= 1 ) ||
146 (cpl_table_has_column(input_spectrum,
"flux") != 1 ) )
148 sprintf(err_msg,
"Either column \"lambda\" or \"flux\" missing in "
149 "input spectrum CPL table.\n"
150 "Cannot continue. Emergency stop.");
151 cpl_msg_debug(cpl_func,
"-------------------------------------------");
152 cpl_msg_debug(cpl_func,
"Incorrect input spectrum CPL table "
154 cpl_table_dump_structure(input_spectrum, NULL);
155 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
158 in_nrow=cpl_table_get_nrow(input_spectrum);
161 sprintf(err_msg,
"Input spectrum has length zero.\n"
162 "Cannot continue. Emergency stop.");
163 cpl_msg_debug(cpl_func,
"-------------------------------------------");
164 cpl_msg_debug(cpl_func,
"Incorrect input spectrum CPL table "
166 cpl_table_dump_structure(input_spectrum, NULL);
167 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
170 if (cpl_error_get_code() != CPL_ERROR_NONE) {
171 return cpl_error_get_code();
186 derivatives=cpl_table_new(in_nrow);
187 cpl_table_duplicate_column(derivatives,
"lambda",
188 input_spectrum,
"lambda");
189 cpl_table_duplicate_column(derivatives,
"flux",
190 input_spectrum,
"flux");
191 cpl_table_duplicate_column(derivatives,
"weight",
192 input_spectrum,
"weight");
193 cpl_table_new_column(derivatives,
"first", CPL_TYPE_DOUBLE);
194 cpl_table_fill_column_window(derivatives,
"first", 0, in_nrow, 0.);
196 if (cpl_error_get_code() != CPL_ERROR_NONE) {
197 return cpl_error_get_code();
204 cpl_table_set_double(derivatives,
"first", 0, 0.);
205 for(loop=0;loop<in_nrow-1;loop++)
207 if (cpl_table_get_double(input_spectrum,
"weight", loop, NULL)
209 cpl_table_get_double(input_spectrum,
"weight", loop+1, NULL)
211 cpl_table_get_double(input_spectrum,
"lambda", loop, NULL)
214 value=cpl_table_get_double(input_spectrum,
"flux", loop+1, NULL)-
215 cpl_table_get_double(input_spectrum,
"flux", loop, NULL);
216 cpl_table_set_double(derivatives,
"first", loop+1, value);
219 if (cpl_error_get_code() != CPL_ERROR_NONE) {
220 return cpl_error_get_code();
227 cpl_table_unselect_all(derivatives);
228 cpl_table_or_selected_double(derivatives,
"first", CPL_NOT_EQUAL_TO, 0.);
229 tmptable = cpl_table_extract_selected(derivatives);
230 cpl_table_select_all(derivatives);
231 cpl_table_abs_column(tmptable,
"first");
232 sig=cpl_table_get_column_stdev(tmptable,
"first");
233 mean=cpl_table_get_column_mean(tmptable,
"first");
235 cpl_table_delete(tmptable);
236 if (sig / mean > maxrat) {
237 deriv1_lim = maxrat * mean;
249 if (cpl_error_get_code() != CPL_ERROR_NONE) {
250 return cpl_error_get_code();
258 flux_low_lim=-lim_fac*deriv1_lim;
259 flux_up_lim=lim_fac*deriv1_lim;
260 sprintf(col1,
"%s",
"first");
261 sprintf(col2,
"%s",
"peaks");
264 if (status != CPL_ERROR_NONE) {
266 cpl_table_delete(derivatives);
276 if (cpl_table_has_column(input_spectrum,
"class") == 0) {
277 cpl_table_duplicate_column(input_spectrum,
"class", derivatives,
280 cpl_table_copy_data_int(input_spectrum,
"class",
281 cpl_table_get_data_int_const(derivatives,
"range"));
284 for(loop=0;loop<in_nrow;loop++)
286 if(cpl_table_get_int(derivatives,
"peaks", loop, NULL)==2)
288 cpl_table_set_int(input_spectrum,
"class", loop, 2);
295 if (status != CPL_ERROR_NONE) {
297 cpl_table_delete(derivatives);
303 if (status != CPL_ERROR_NONE) {
305 cpl_table_delete(derivatives);
318 if (status != CPL_ERROR_NONE) {
320 cpl_table_delete(derivatives);
325 pptr = cpl_parameterlist_find(parlist,
"output_name");
326 sprintf(plot_title,
"%s",cpl_parameter_get_string(pptr));
335 cpl_table_erase_column(input_spectrum,
"linelist");
339 cpl_table_delete(derivatives);
341 return cpl_error_get_code();
347 cpl_table *input_spectrum,
348 cpl_parameterlist *parlist)
425 int peak_flag=0, peak_loc=0;
427 int line_width=0, px_bef_peak=0, px_aft_peak=-1;
428 int line_dist=0, line_count=0;
431 double min_line_dist=0,
439 int line_start_flag=0, line_px_start=0;
440 double peak_lambda=0., peak_flux=0.;
441 int found=0, index=0;
442 int dist_bef=0, dist_aft=0, symm=0;
451 p = cpl_parameterlist_find(parlist,
"iteration");
452 iteration = cpl_parameter_get_int(p);
454 cpl_parameter_set_int(p,iteration);
457 p = cpl_parameterlist_find(parlist,
"fwhm");
458 fwhm = cpl_parameter_get_double(p);
461 p = cpl_parameterlist_find(parlist,
"varfwhm");
462 varfwhm = cpl_parameter_get_int(p);
465 p = cpl_parameterlist_find(parlist,
"meanlam");
466 meanlam = cpl_parameter_get_double(p);
469 p = cpl_parameterlist_find(parlist,
"min_line_dist_fac");
470 min_line_dist_0 = fwhm * cpl_parameter_get_double(p);
473 in_nrow=cpl_table_get_nrow(input_spectrum);
476 for(loop=1;loop<in_nrow-1;loop++)
488 while(cpl_table_get_int(input_spectrum,
"class", loop, NULL)>=1)
491 if (line_start_flag==0)
499 peak_flag=cpl_table_get_int(input_spectrum,
"class", loop,
504 peak_lambda=cpl_table_get_double(input_spectrum,
"lambda",
506 peak_flux=cpl_table_get_double(input_spectrum,
"flux",
508 line_list_val=cpl_table_get_int(input_spectrum,
"linelist",
526 peak_flux *= peak_lambda / meanlam;
530 if (found==1 && peak_flux > 0.)
533 cpl_table_set_size(linetab, linetabsize);
534 cpl_table_set_int(linetab,
"width", linetabsize-1, line_width);
535 cpl_table_set_int(linetab,
"peak_loc", linetabsize-1, peak_loc);
536 cpl_table_set_double(linetab,
"peak_lam", linetabsize-1,
538 cpl_table_set_double(linetab,
"peak_flux", linetabsize-1,
540 cpl_table_set_int(linetab,
"px_bef", linetabsize-1, px_bef_peak);
541 cpl_table_set_int(linetab,
"px_aft", linetabsize-1, px_aft_peak);
542 cpl_table_set_int(linetab,
"line_px_start", linetabsize-1,
544 cpl_table_set_int(linetab,
"line_px_end", linetabsize-1, loop-1);
545 if (iteration == 1) {
546 cpl_table_set_double(linetab,
"fwhm", linetabsize-1, 0.);
548 cpl_table_set_double(linetab,
"fwhm", linetabsize-1, fwhm);
550 cpl_table_set_int(linetab,
"line_list", linetabsize-1,
570 for(loop=0;loop<linetabsize;loop++)
572 peak_lambda=cpl_table_get_double(linetab,
"peak_lam", loop, NULL);
577 dist_bef=cpl_table_get_int(linetab,
"line_px_start", loop, NULL);
578 cpl_table_set_int(linetab,
"dist_bef", loop, dist_bef);
582 dist_bef=cpl_table_get_int(linetab,
"line_px_start", loop, NULL)-
583 cpl_table_get_int(linetab,
"line_px_end", loop-1, NULL);
584 cpl_table_set_int(linetab,
"dist_bef", loop, dist_bef);
587 if(loop==linetabsize-1)
589 dist_aft=in_nrow-cpl_table_get_int(linetab,
"line_px_end", loop,
591 cpl_table_set_int(linetab,
"dist_aft", loop, dist_aft);
595 dist_aft=cpl_table_get_int(linetab,
"line_px_start", loop+1, NULL)-
596 cpl_table_get_int(linetab,
"line_px_end", loop, NULL);
597 cpl_table_set_int(linetab,
"dist_aft", loop, dist_aft);
600 symm=abs(cpl_table_get_int(linetab,
"px_bef", loop, NULL)-
601 cpl_table_get_int(linetab,
"px_aft", loop,
606 min_line_dist = min_line_dist_0 * peak_lambda / meanlam;
608 min_line_dist = min_line_dist_0;
612 if( ( dist_bef >= min_line_dist ) &&
613 ( dist_aft >= min_line_dist ) &&
616 index = cpl_table_get_int(linetab,
"peak_loc", loop, NULL);
617 cpl_table_set_int(input_spectrum,
"class", index, 3);
618 cpl_table_set_int(linetab,
"isol_flag", loop, 1);
623 cpl_table_set_int(linetab,
"isol_flag", loop, 0);
627 return cpl_error_get_code();
637 const double lower_threshold,
638 const double upper_threshold)
667 double loc_max_val=0.;
672 cpl_table *temporary;
678 nrow=cpl_table_get_nrow(input_table);
679 temporary=cpl_table_new(nrow);
682 cpl_table_new_column(temporary,
"found", CPL_TYPE_INT);
683 cpl_table_new_column(temporary,
"range", CPL_TYPE_INT);
684 cpl_table_new_column(temporary,
"range_red", CPL_TYPE_INT);
685 cpl_table_new_column(temporary,
"range_blue", CPL_TYPE_INT);
688 cpl_table_new_column(input_table, newcolname, CPL_TYPE_INT);
689 cpl_table_fill_column_window_int(input_table, newcolname, 0, nrow, 0);
692 cpl_table_duplicate_column(temporary,
"target_col", input_table, colname);
695 sprintf(col1,
"%s",
"target_col");
696 sprintf(col2,
"%s",
"found");
711 for (loop=0;loop<nrow-1;loop++)
713 found_flag=cpl_table_get_int(temporary,
"found", loop, NULL);
714 if ( found_flag == 1 )
718 if ((cpl_table_get_int(temporary,
"found", loop, NULL) ==-1 &&
719 cpl_table_get_int(temporary,
"found", loop+1, NULL) == 0) ||
720 (cpl_table_get_int(temporary,
"found", loop, NULL) ==-1 &&
721 cpl_table_get_int(temporary,
"found", loop+1, NULL) == 1))
725 cpl_table_set_int(temporary,
"range_red", loop, range_flag);
729 for ( loop = nrow-2; loop > 0; loop-- )
731 found_flag = cpl_table_get_int(temporary,
"found", loop, NULL);
732 if ( found_flag == -1 )
736 if (( cpl_table_get_int(temporary,
"found", loop+1, NULL) == 1 &&
737 cpl_table_get_int(temporary,
"found", loop, NULL) == 0 ) ||
738 ( cpl_table_get_int(temporary,
"found", loop+1, NULL) == 1 &&
739 cpl_table_get_int(temporary,
"found", loop, NULL) == -1 ))
743 cpl_table_set_int(temporary,
"range_blue", loop, range_flag);
745 cpl_table_set_int(temporary,
"range_blue", loop, 0);
748 cpl_table_new_column(input_table,
"range", CPL_TYPE_INT);
750 for (loop=0;loop<nrow;loop++)
752 if (( cpl_table_get_int(temporary,
"range_red", loop, NULL) == 1 ) &&
753 ( cpl_table_get_int(temporary,
"range_blue", loop, NULL) == 1 ) &&
754 ( cpl_table_get_double(input_table,
"lambda", loop, NULL)
757 cpl_table_set_int(input_table,
"range", loop, 1);
761 cpl_table_set_int(input_table,
"range", loop, 0);
771 range_flag = cpl_table_get_int(input_table,
"range", loop, NULL);
779 while ( cpl_table_get_int(input_table,
"range", loop, NULL) == 1)
781 value=cpl_table_get_double(input_table,
"flux", loop, NULL);
782 if (value > loc_max_val)
790 cpl_table_set_int(input_table, newcolname, loc_max, 2);
795 cpl_table_delete(temporary);
796 return cpl_error_get_code();
843 nrow=cpl_table_get_nrow(input_table);
845 for(loop=0;loop<nrow;loop++)
847 cpl_table_set_int(input_table, newcolname, loop, 0);
848 value=cpl_table_get(input_table, colname, loop, NULL);
849 if( (value <= lowlim) )
851 cpl_table_set_int(input_table, newcolname, loop, -1);
853 if ((value >= uplim) )
855 cpl_table_set_int(input_table, newcolname, loop, 1);
860 return CPL_ERROR_NONE;
883 in_nrow=cpl_table_get_nrow(intable);
885 for(loop=0;loop<in_nrow-1;loop++)
887 n_lines=n_lines+cpl_table_get_int(intable,
"peaks", loop, NULL);
895 const cpl_table *input_table,
896 char *col_name,
const int n_bins)
919 double col_min=0., col_max=0.;
923 double data_point=0.;
924 double lowlim=0., uplim=0.;
926 cpl_table *bin_limits;
930 tab_nrow=cpl_table_get_nrow(input_table);
932 col_min=cpl_table_get_column_min(input_table, col_name);
933 col_max=cpl_table_get_column_max(input_table, col_name);
936 bin_limits=cpl_table_new(n_bins);
937 cpl_table_new_column(bin_limits,
"lowlim", CPL_TYPE_DOUBLE);
938 cpl_table_new_column(bin_limits,
"uplim", CPL_TYPE_DOUBLE);
940 for (loop=0;loop<=n_bins-1;loop++)
942 bin_loop=col_min+loop*(col_max-col_min)/n_bins;
943 cpl_table_set_double(bin_limits,
"lowlim", loop, bin_loop);
944 bin_loop=col_min+(loop+1)*(col_max-col_min)/n_bins;
945 cpl_table_set_double(bin_limits,
"uplim", loop, bin_loop);
948 for (loop=0;loop<=n_bins-1;loop++)
950 lowlim=cpl_table_get_double(bin_limits,
"lowlim", loop, NULL);
951 uplim=cpl_table_get_double(bin_limits,
"uplim", loop, NULL);
955 for (loop2=0;loop2<=tab_nrow-1;loop2++)
957 data_point=cpl_table_get(input_table, col_name, loop2, NULL);
958 if( ( data_point >= lowlim ) && ( data_point < uplim ) )
963 cpl_table_set(hist,
"bins", loop, lowlim+(uplim-lowlim)/2);
964 cpl_table_set(hist,
"counts", loop, count);
968 cpl_table_delete(bin_limits);
969 return CPL_ERROR_NONE;
1006 cpl_table_new_column(linetab,
"width", CPL_TYPE_INT);
1007 cpl_table_new_column(linetab,
"peak_loc", CPL_TYPE_INT);
1008 cpl_table_new_column(linetab,
"peak_lam", CPL_TYPE_DOUBLE);
1009 cpl_table_new_column(linetab,
"peak_flux", CPL_TYPE_DOUBLE);
1010 cpl_table_new_column(linetab,
"px_bef", CPL_TYPE_INT);
1011 cpl_table_new_column(linetab,
"px_aft", CPL_TYPE_INT);
1012 cpl_table_new_column(linetab,
"line_px_start", CPL_TYPE_INT);
1013 cpl_table_new_column(linetab,
"line_px_end", CPL_TYPE_INT);
1014 cpl_table_new_column(linetab,
"dist_bef", CPL_TYPE_INT);
1015 cpl_table_new_column(linetab,
"dist_aft", CPL_TYPE_INT);
1016 cpl_table_new_column(linetab,
"isol_flag", CPL_TYPE_INT);
1017 cpl_table_new_column(linetab,
"fwhm", CPL_TYPE_DOUBLE);
1018 cpl_table_new_column(linetab,
"line_list", CPL_TYPE_INT);
1024 const cpl_table *groups,
1025 const cpl_parameterlist *parlist)
1047 double fwhm = 0, fwhm_crit = 0, fwhm_crit_fac = 0.1,
1053 double lam_spec0 = 0., lam_spec1 = 0.,
1054 lam_spec2 = 0., lam_list = 0.,
1055 lam_lim1 = 0., lam_lim2 = 0.;
1059 int line_arr_size=0;
1068 cpl_array *line_arr;
1070 const cpl_parameter *p;
1077 listlength = cpl_table_get_nrow(groups);
1078 n_loops = cpl_table_get_nrow(spec);
1079 if (cpl_table_has_column(spec,
"linelist") != 1)
1081 cpl_table_new_column(spec,
"linelist", CPL_TYPE_INT);
1085 for (i = 0;i < n_loops; i++)
1087 cpl_table_set_int(spec,
"linelist", i, 0);
1091 p = cpl_parameterlist_find_const(parlist,
"fwhm");
1092 fwhm = cpl_parameter_get_double(p);
1094 pxscl = (cpl_table_get_double(spec,
"lambda",n_loops-1,NULL) -
1095 cpl_table_get_double(spec,
"lambda",0,NULL) ) / n_loops;
1096 fwhm_crit_0 = fwhm_crit_fac * fwhm * pxscl;
1099 p = cpl_parameterlist_find_const(parlist,
"varfwhm");
1100 varfwhm = cpl_parameter_get_int(p);
1103 p = cpl_parameterlist_find_const(parlist,
"meanlam");
1104 meanlam = cpl_parameter_get_double(p);
1109 for (i = 1; i < n_loops-1; i++) {
1110 const double * pdlam;
1118 lam_spec0 = cpl_table_get_double(spec,
"lambda", i-1, NULL);
1119 lam_spec1 = cpl_table_get_double(spec,
"lambda", i, NULL);
1120 lam_spec2 = cpl_table_get_double(spec,
"lambda", i+1, NULL);
1123 lam_lim1 = lam_spec1 - (lam_spec1 - lam_spec0) / 2;
1124 lam_lim2 = lam_spec1 + (lam_spec2 - lam_spec1) / 2;
1128 fwhm_crit = fwhm_crit_0 * lam_spec1 / meanlam;
1130 fwhm_crit = fwhm_crit_0;
1135 line_arr = cpl_array_new(line_arr_size, CPL_TYPE_DOUBLE);
1138 pdlam = cpl_table_get_data_double_const(groups,
"lambda");
1139 nbad = cpl_table_count_invalid(groups,
"lambda");
1142 for (j = 0; j < listlength; j++)
1146 lam_list = pdlam[j];
1149 lam_list = cpl_table_get_double(groups,
"lambda", j, NULL);
1152 if (lam_list < lam_spec0)
1161 if ( ( lam_list > lam_lim1 ) && ( lam_list <= lam_lim2 ) )
1164 cpl_array_set_size(line_arr,line_arr_size);
1165 cpl_array_set_double(line_arr,line_arr_size-1,lam_list);
1166 line_flux_sum += cpl_table_get_double(groups,
1177 if (line_arr_size > 1)
1180 while (k < line_arr_size-1)
1182 if ( cpl_array_get_double(line_arr,k+1,NULL) -
1183 cpl_array_get_double(line_arr,k,NULL) < fwhm_crit )
1192 cpl_array_delete(line_arr);
1193 cpl_table_set_int(spec,
"linelist", i, peakcount1);
1196 return cpl_error_get_code();
1217 nrow = cpl_table_get_nrow(spec);
1219 for (loop = 0; loop < nrow; loop++)
1221 if( ( cpl_table_get_int(spec,
"class", loop, NULL) >= 2 ) &&
1222 ( cpl_table_get_int(spec,
"linelist", loop, NULL) == 5 ) )
1224 cpl_table_set_int(spec,
"linelist", loop, 3);
1228 return cpl_error_get_code();
1237 const double lowlim)
1267 nrow=cpl_table_get_nrow(input_table);
1268 cpl_table_new_column(input_table, newcolname, CPL_TYPE_INT);
1270 for(loop=0;loop<nrow;loop++)
1272 cpl_table_set_int(input_table, newcolname, loop, 0);
1273 value=cpl_table_get(input_table, colname, loop, NULL);
1274 if( (value>=lowlim) )
1276 cpl_table_set_int(input_table, newcolname, loop, 1);
1280 return CPL_ERROR_NONE;
cpl_error_code sc_specdiss_find_valuesoutside(cpl_table *input_table, char *newcolname, char *colname, const double lowlim, const double uplim)
void sc_specdiss_init_linetab(cpl_table *linetab)
int sc_specdiss_count_lines(const cpl_table *intable)
cpl_error_code sc_specdiss_find_emissionlines(cpl_table *input_spectrum, cpl_table *linetab, cpl_parameterlist *parlist, const cpl_table *groups)
cpl_error_code sc_specdiss_identify_airglowlines(cpl_table *spec)
cpl_error_code sc_specdiss_merge_speclinelist(cpl_table *spec, const cpl_table *groups, const cpl_parameterlist *parlist)
cpl_error_code sc_specdiss_create_hist(cpl_table *hist, const cpl_table *input_table, char *col_name, const int n_bins)
cpl_error_code sc_specdiss_find_valuesabove(cpl_table *input_table, char *newcolname, char *colname, const double lowlim)
cpl_error_code sc_specdiss_find_isolatedlines(cpl_table *linetab, cpl_table *input_spectrum, cpl_parameterlist *parlist)
cpl_error_code sc_specdiss_find_localmaxima(cpl_table *input_table, char *newcolname, char *colname, const double lower_threshold, const double upper_threshold)
Header for spectral analysis library.