78 const cpl_frameset *set_raw,
79 const char *recipe_name,
82 const char *package_name,
84 cpl_table *(*convert_ascii_table)(
const char *))
87 const cpl_size nb_catalogs = cpl_frameset_get_size(set_raw);
88 cpl_propertylist *plist_ext;
90 cpl_error_code error = CPL_ERROR_NONE;
95 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
97 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
98 if (recipe_name == NULL)
99 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
101 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
102 if (ins_name == NULL)
103 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
104 if (convert_ascii_table == NULL)
105 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
108 out_name = cpl_sprintf(
"%s" CPL_DFS_FITS, recipe_name);
110 plist_ext = cpl_propertylist_new();
113 for (i = 0; i < nb_catalogs; i++) {
115 const cpl_frame *cur_frame =
116 cpl_frameset_get_position_const(set_raw, i);
117 const char *cat_name = cpl_frame_get_filename(cur_frame);
119 cpl_table *out = convert_ascii_table(cat_name);
123 error = cpl_error_get_code()
124 ? cpl_error_set_where(cpl_func)
125 : cpl_error_set(cpl_func, CPL_ERROR_UNSPECIFIED);
129 if (cpl_table_get_nrow(out) == 0) {
130 cpl_table_delete(out);
131 error = cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
132 "Empty catalogue %d in '%s'",
133 (
int)i + 1, cat_name);
137 cpl_propertylist_update_string(plist_ext,
"EXTNAME", cat_name);
141 cpl_parameterlist *parlist = cpl_parameterlist_new();
142 cpl_propertylist *plist = cpl_propertylist_new();
145 cpl_propertylist_append_string(plist,
"INSTRUME", ins_name);
146 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, pro_cat);
147 if (pro_type != NULL) {
148 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
152 error = cpl_dfs_save_table(set_in, NULL, parlist, set_raw, NULL,
153 out, plist_ext, recipe_name, plist, NULL,
154 package_name, out_name);
155 cpl_parameterlist_delete(parlist);
156 cpl_propertylist_delete(plist);
160 cpl_table_save(out, NULL, plist_ext, out_name, CPL_IO_EXTEND);
163 cpl_table_delete(out);
166 (void)cpl_error_set_where(cpl_func);
171 cpl_propertylist_delete(plist_ext);
197 cpl_frame *cur_frame;
201 if (filename == NULL)
203 if (ext_name == NULL)
210 cur_frame = cpl_frame_new();
211 cpl_frame_set_filename(cur_frame, filename);
212 next = cpl_frame_get_nextensions(cur_frame);
213 cpl_frame_delete(cur_frame);
216 for (i = 0; i < next; i++) {
217 cpl_propertylist *plist;
218 const char *cur_name;
221 if ((plist = cpl_propertylist_load_regexp(filename, i + 1,
"EXTNAME",
223 cpl_msg_error(cpl_func,
"Cannot load header of %d th extension",
227 cur_name = cpl_propertylist_get_string(plist,
"EXTNAME");
230 if (!strcmp(cur_name, ext_name)) {
233 out = cpl_table_load(filename, i + 1, 1);
234 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL,
236 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
237 0, cpl_table_get_nrow(out),
240 cpl_msg_error(cpl_func,
"Cannot load extension %d", i + 1);
241 cpl_propertylist_delete(plist);
246 else if (!strcmp(ext_name,
"all")) {
250 out = cpl_table_load(filename, i + 1, 1);
251 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL,
253 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
254 0, cpl_table_get_nrow(out),
257 cpl_msg_error(cpl_func,
"Cannot load extension %d", i + 1);
258 cpl_propertylist_delete(plist);
264 out_cur = cpl_table_load(filename, i + 1, 1);
265 if (out_cur == NULL) {
266 cpl_msg_error(cpl_func,
"Cannot load extension %d", i + 1);
267 cpl_table_delete(out);
268 cpl_propertylist_delete(plist);
271 cpl_table_new_column(out_cur, IRPLIB_STDSTAR_CAT_COL,
273 cpl_table_fill_column_window_string(out_cur,
274 IRPLIB_STDSTAR_CAT_COL, 0,
275 cpl_table_get_nrow(out_cur),
278 if (cpl_table_insert(out, out_cur, cpl_table_get_nrow(out)) !=
280 cpl_msg_error(cpl_func,
"Cannot merge table %d", i + 1);
281 cpl_table_delete(out);
282 cpl_table_delete(out_cur);
283 cpl_propertylist_delete(plist);
286 cpl_table_delete(out_cur);
289 cpl_propertylist_delete(plist);
308 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
309 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
310 "Missing column: %s",
311 IRPLIB_STDSTAR_STAR_COL);
313 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
314 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
315 "Missing column: %s",
316 IRPLIB_STDSTAR_TYPE_COL);
318 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_CAT_COL)) {
319 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
320 "Missing column: %s",
321 IRPLIB_STDSTAR_CAT_COL);
323 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_RA_COL)) {
324 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
325 "Missing column: %s",
326 IRPLIB_STDSTAR_RA_COL);
328 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_DEC_COL)) {
329 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
330 "Missing column: %s",
331 IRPLIB_STDSTAR_DEC_COL);
333 return CPL_ERROR_NONE;
361 nrows = cpl_table_get_nrow(cat);
364 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
365 cpl_msg_error(cpl_func,
"Missing column: " IRPLIB_STDSTAR_RA_COL);
368 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
369 cpl_msg_error(cpl_func,
"Missing column: " IRPLIB_STDSTAR_DEC_COL);
373 if (cpl_table_count_selected(cat) == 0) {
374 cpl_msg_error(cpl_func,
"All %d row(s) already deselected", (
int)nrows);
379 for (i = 0; i < nrows; i++) {
380 if (cpl_table_is_selected(cat, i)) {
382 const double distance = irplib_wcs_great_circle_dist(
384 cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
385 cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
387 cpl_table_unselect_row(cat, i);
443 double min_dist, distance;
457 nrows = cpl_table_get_nrow(cat);
460 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
461 cpl_msg_error(cpl_func,
"Missing %s column", IRPLIB_STDSTAR_RA_COL);
464 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
465 cpl_msg_error(cpl_func,
"Missing %s column", IRPLIB_STDSTAR_DEC_COL);
470 for (i = 0; i < nrows; i++) {
471 if (cpl_table_is_selected(cat, i)) {
473 distance = irplib_wcs_great_circle_dist(
475 cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
476 cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
477 if (distance <= min_dist) {
522 cpl_errorstate prestate = cpl_errorstate_get();
524 const double dist = dist_am / 60.0;
529 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
531 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
533 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
537 return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
538 "Cannot load the catalog %s from %s",
544 cpl_table_delete(catal);
545 return cpl_error_set_where(cpl_func);
550 cpl_table_delete(catal);
551 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
552 "Cannot select stars in that band");
557 cpl_table_delete(catal);
558 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
559 "Cannot select close stars");
564 cpl_table_delete(catal);
565 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
566 "Cannot get the closest star with "
567 "known %s magnitude",
572 *mag = cpl_table_get_double(catal, band, ind, NULL);
576 cpl_table_get_string(catal, IRPLIB_STDSTAR_STAR_COL, ind));
580 cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL, ind));
582 if (usedcatname != NULL) {
583 if (strcmp(catname,
"all"))
584 *usedcatname = cpl_strdup(catname);
586 *usedcatname = cpl_strdup(
587 cpl_table_get_string(catal, IRPLIB_STDSTAR_CAT_COL, ind));
592 cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
593 if (star_dec != NULL)
595 cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
598 cpl_table_delete(catal);
599 return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
600 : cpl_error_set_where(cpl_func);
626 const cpl_vector *wave;
627 const cpl_vector *extr;
638 wave = cpl_bivector_get_x_const(spec);
639 extr = cpl_bivector_get_y_const(spec);
642 out = cpl_vector_duplicate(extr);
645 cpl_vector_divide_scalar(out, dit);
648 cpl_vector_divide_scalar(out, surface);
651 cpl_vector_multiply_scalar(out, gain);
654 factor = pow(10, mag / 2.5);
655 cpl_vector_multiply_scalar(out, factor);
658 factor = (cpl_vector_get(wave, cpl_vector_get_size(wave) - 1) -
659 cpl_vector_get(wave, 0)) /
660 cpl_vector_get_size(wave);
661 cpl_vector_divide_scalar(out, factor);
664 cpl_vector_multiply_scalar(out, h * c);
665 cpl_vector_divide(out, wave);
681 const cpl_vector *waves,
684 double wmin, wmax, wstep;
688 cpl_bivector *sed_loc;
692 cpl_bivector *out_biv;
693 double f0_jan, f0_erg, cent_val;
703 nb_sed = cpl_bivector_get_size(sed);
704 sed_x = cpl_bivector_get_x_data_const(sed);
705 sed_y = cpl_bivector_get_y_data_const(sed);
706 wstep = sed_x[1] - sed_x[0];
707 wmin = cpl_vector_get(waves, 0);
708 wmax = cpl_vector_get(waves, cpl_vector_get_size(waves) - 1);
711 sed_loc = cpl_bivector_new(nb_sed + 4);
712 sed_loc_x = cpl_bivector_get_x_data(sed_loc);
713 sed_loc_y = cpl_bivector_get_y_data(sed_loc);
714 for (i = 0; i < nb_sed; i++) {
715 sed_loc_x[i + 2] = sed_x[i];
716 sed_loc_y[i + 2] = sed_y[i];
720 sed_loc_x[1] = sed_loc_x[2] - wstep;
721 if (sed_loc_x[2] < wmin) {
722 sed_loc_x[0] = sed_loc_x[1] - wstep;
725 sed_loc_x[0] = wmin - wstep;
727 sed_loc_y[0] = 1e-20;
728 sed_loc_y[1] = 1e-20;
731 sed_loc_x[nb_sed + 2] = sed_loc_x[nb_sed + 1] + wstep;
732 if (sed_loc_x[nb_sed + 1] > wmax) {
733 sed_loc_x[nb_sed + 3] = sed_loc_x[nb_sed + 2] + wstep;
736 sed_loc_x[nb_sed + 3] = wmax + wstep;
738 sed_loc_y[nb_sed + 2] = 1e-20;
739 sed_loc_y[nb_sed + 3] = 1e-20;
742 out = cpl_vector_duplicate(waves);
743 IRPLIB_DIAG_PRAGMA_PUSH_IGN(-Wcast - qual);
745 out_biv = cpl_bivector_wrap_vectors((cpl_vector *)waves, out);
746 IRPLIB_DIAG_PRAGMA_POP;
748 if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
749 cpl_msg_error(cpl_func,
"Cannot interpolate the wavelength");
750 cpl_bivector_unwrap_vectors(out_biv);
751 cpl_vector_delete(out);
752 cpl_bivector_delete(sed_loc);
755 cpl_bivector_unwrap_vectors(out_biv);
756 cpl_bivector_delete(sed_loc);
759 f0_jan = 5513.15 / (pow(cent_wl, 3) * (exp(1.2848 / cent_wl) - 1));
763 f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl * cent_wl * 1e4 * 1e4);
766 cent_val = cpl_vector_get(out, cpl_vector_get_size(out) / 2);
767 if (cent_val <= 0.0) {
768 cpl_msg_error(cpl_func,
"Negative or 0 central value");
769 cpl_vector_delete(out);
772 cpl_vector_multiply_scalar(out, f0_erg / cent_val);
800 if (seds_file == NULL)
806 if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
807 cpl_msg_error(cpl_func,
"Cannot load the table");
812 if (!cpl_table_has_column(seds, sptype)) {
813 cpl_msg_error(cpl_func,
"SED of the requested star not available");
814 cpl_table_delete(seds);
819 nlines = cpl_table_get_nrow(seds);
822 if ((wave = cpl_vector_wrap(
823 nlines, cpl_table_get_data_double(seds,
"Wavelength"))) == NULL) {
824 cpl_msg_error(cpl_func,
"Cannot get the Wavelength column");
825 cpl_table_delete(seds);
830 if ((sed = cpl_vector_wrap(nlines,
831 cpl_table_get_data_double(seds, sptype))) ==
833 cpl_msg_error(cpl_func,
"Cannot get the SED column");
834 cpl_table_delete(seds);
835 cpl_vector_unwrap(wave);
838 tmp = cpl_bivector_wrap_vectors(wave, sed);
841 out = cpl_bivector_duplicate(tmp);
844 cpl_bivector_unwrap_vectors(tmp);
845 cpl_vector_unwrap(wave);
846 cpl_vector_unwrap(sed);
847 cpl_table_delete(seds);
cpl_error_code irplib_stdstar_find_star(const char *catfile, double ra, double dec, const char *band, const char *catname, double *mag, char **name, char **type, char **usedcatname, double *star_ra, double *star_dec, double dist_am)
Find the closest star to ra, dec in the catalog.
cpl_error_code irplib_stdstar_write_catalogs(cpl_frameset *set_in, const cpl_frameset *set_raw, const char *recipe_name, const char *pro_cat, const char *pro_type, const char *package_name, const char *ins_name, cpl_table *(*convert_ascii_table)(const char *))
Write the ASCII catalogs as FITS files.