36 #include "irplib_stdstar.h"
37 #include "irplib_utils.h"
38 #include "irplib_wcs.h"
49 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(6, 3, 0)
50 #define IRPLIB_FRAMESET_GET_CONST cpl_frameset_get_position_const
53 #define IRPLIB_FRAMESET_GET_CONST cpl_frameset_get_frame_const
90 const cpl_frameset * set_raw,
91 const char * recipe_name,
93 const char * pro_type,
94 const char * package_name,
95 const char * ins_name,
96 cpl_table * (*convert_ascii_table)(
const char *))
99 const cpl_size nb_catalogs = cpl_frameset_get_size(set_raw);
100 cpl_propertylist * plist_ext;
102 cpl_error_code error = CPL_ERROR_NONE;
106 if (set_in == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
107 if (set_raw == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
108 if (recipe_name == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
109 if (pro_cat == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
110 if (ins_name == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
111 if (convert_ascii_table == NULL)
return
112 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
115 out_name = cpl_sprintf(
"%s" CPL_DFS_FITS, recipe_name);
117 plist_ext = cpl_propertylist_new();
120 for (i = 0; i < nb_catalogs; i++) {
122 const cpl_frame * cur_frame = IRPLIB_FRAMESET_GET_CONST(set_raw, i);
123 const char * cat_name = cpl_frame_get_filename(cur_frame);
125 cpl_table * out = convert_ascii_table(cat_name);
129 error = cpl_error_get_code() ? cpl_error_set_where(cpl_func)
130 : cpl_error_set(cpl_func, CPL_ERROR_UNSPECIFIED);
134 if (cpl_table_get_nrow(out) == 0) {
135 cpl_table_delete(out);
136 error = cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
137 "Empty catalogue %d in '%s'",
142 cpl_propertylist_update_string(plist_ext,
"EXTNAME", cat_name);
146 cpl_parameterlist * parlist = cpl_parameterlist_new();
147 cpl_propertylist * plist = cpl_propertylist_new();
150 cpl_propertylist_append_string(plist,
"INSTRUME", ins_name);
151 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, pro_cat);
152 if (pro_type != NULL) {
153 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
157 error = cpl_dfs_save_table(set_in, NULL, parlist, set_raw, NULL,
158 out, plist_ext, recipe_name, plist,
159 NULL, package_name, out_name);
160 cpl_parameterlist_delete(parlist);
161 cpl_propertylist_delete(plist);
163 error = cpl_table_save(out, NULL, plist_ext, out_name,
167 cpl_table_delete(out);
170 (void)cpl_error_set_where(cpl_func);
175 cpl_propertylist_delete(plist_ext);
196 const char * filename,
197 const char * ext_name)
200 cpl_propertylist * plist;
201 const char * cur_name;
204 cpl_frame * cur_frame;
208 if (filename == NULL)
return NULL;
209 if (ext_name == NULL)
return NULL;
215 cur_frame = cpl_frame_new();
216 cpl_frame_set_filename(cur_frame, filename);
217 next = cpl_frame_get_nextensions(cur_frame);
218 cpl_frame_delete(cur_frame);
221 for (i=0; i<next; i++) {
223 if ((plist = cpl_propertylist_load_regexp(filename, i+1,
"EXTNAME",
225 cpl_msg_error(cpl_func,
"Cannot load header of %d th extension",
229 cur_name = cpl_propertylist_get_string(plist,
"EXTNAME");
232 if (!strcmp(cur_name, ext_name)) {
235 out = cpl_table_load(filename, i+1, 1);
236 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
237 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
238 0, cpl_table_get_nrow(out),
241 cpl_msg_error(cpl_func,
"Cannot load extension %d", i+1);
242 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, CPL_TYPE_STRING);
252 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
253 0, cpl_table_get_nrow(out),
256 cpl_msg_error(cpl_func,
"Cannot load extension %d", i+1);
257 cpl_propertylist_delete(plist);
262 out_cur = cpl_table_load(filename, i+1, 1);
263 if (out_cur == NULL) {
264 cpl_msg_error(cpl_func,
"Cannot load extension %d", i+1);
265 cpl_table_delete(out);
266 cpl_propertylist_delete(plist);
269 cpl_table_new_column(out_cur, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
270 cpl_table_fill_column_window_string(out_cur, IRPLIB_STDSTAR_CAT_COL,
271 0, cpl_table_get_nrow(out_cur),
274 if (cpl_table_insert(out, out_cur,
275 cpl_table_get_nrow(out)) != CPL_ERROR_NONE) {
276 cpl_msg_error(cpl_func,
"Cannot merge table %d", i+1);
277 cpl_table_delete(out);
278 cpl_table_delete(out_cur);
279 cpl_propertylist_delete(plist);
282 cpl_table_delete(out_cur);
285 cpl_propertylist_delete(plist);
301 const cpl_table * catal)
304 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
305 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
306 "Missing column: %s",
307 IRPLIB_STDSTAR_STAR_COL);
309 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
310 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
311 "Missing column: %s",
312 IRPLIB_STDSTAR_TYPE_COL);
314 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_CAT_COL)) {
315 return cpl_error_set_message(cpl_func,
316 CPL_ERROR_ILLEGAL_INPUT,
317 "Missing column: %s",
318 IRPLIB_STDSTAR_CAT_COL);
320 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_RA_COL)) {
321 return cpl_error_set_message(cpl_func,
322 CPL_ERROR_ILLEGAL_INPUT,
323 "Missing column: %s",
324 IRPLIB_STDSTAR_RA_COL);
326 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_DEC_COL)) {
327 return cpl_error_set_message(cpl_func,
328 CPL_ERROR_ILLEGAL_INPUT,
329 "Missing column: %s",
330 IRPLIB_STDSTAR_DEC_COL);
332 return CPL_ERROR_NONE;
359 if (cat == NULL)
return -1;
362 nrows = cpl_table_get_nrow(cat);
365 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
366 cpl_msg_error(cpl_func,
"Missing %s column", IRPLIB_STDSTAR_RA_COL);
369 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
370 cpl_msg_error(cpl_func,
"Missing %s column", IRPLIB_STDSTAR_DEC_COL);
375 for (i=0; i<nrows; i++) {
376 if (cpl_table_is_selected(cat, i)) {
378 distance = irplib_wcs_great_circle_dist(ra, dec,
379 cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
380 cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
381 if (distance > dist) cpl_table_unselect_row(cat, i);
399 const char * mag_colname)
402 if (cat == NULL)
return -1;
403 if (mag_colname == NULL)
return -1;
406 if (!cpl_table_has_column(cat, mag_colname)) {
407 cpl_msg_error(cpl_func,
"Column %s does not exist in the catalog",
413 if (cpl_table_and_selected_double(cat, mag_colname, CPL_NOT_GREATER_THAN,
415 cpl_msg_error(cpl_func,
"Column %s does not exist in the catalog",
434 const cpl_table * cat,
438 double min_dist, distance;
444 if (cat == NULL)
return -1;
451 nrows = cpl_table_get_nrow(cat);
454 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
455 cpl_msg_error(cpl_func,
"Missing %s column", IRPLIB_STDSTAR_RA_COL);
458 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
459 cpl_msg_error(cpl_func,
"Missing %s column", IRPLIB_STDSTAR_DEC_COL);
464 for (i=0; i<nrows; i++) {
465 if (cpl_table_is_selected(cat, i)) {
467 distance = irplib_wcs_great_circle_dist(ra, dec,
468 cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
469 cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
470 if (distance <= min_dist) {
502 const char * catfile,
506 const char * catname,
515 cpl_errorstate prestate = cpl_errorstate_get();
517 const double dist = dist_am / 60.0;
521 if (catfile == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
522 if (band == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
523 if (catname == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
527 return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
528 "Cannot load the catalog %s from %s",
534 cpl_table_delete(catal);
535 return cpl_error_set_where(cpl_func);
540 cpl_table_delete(catal);
541 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
542 "Cannot select stars in that band");
547 cpl_table_delete(catal);
548 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
549 "Cannot select close stars");
554 cpl_table_delete(catal);
555 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
556 "Cannot get the closest star with "
557 "known %s magnitude",band);
561 *mag = cpl_table_get_double(catal, band, ind, NULL);
565 *name = cpl_strdup(cpl_table_get_string(catal,
566 IRPLIB_STDSTAR_STAR_COL, ind));
571 *type = cpl_strdup(cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL,
574 if(usedcatname != NULL)
576 if(strcmp(catname,
"all"))
577 *usedcatname = cpl_strdup(catname);
580 *usedcatname = cpl_strdup(cpl_table_get_string
581 (catal, IRPLIB_STDSTAR_CAT_COL, ind));
585 *star_ra = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
587 *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
590 cpl_table_delete(catal);
591 return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
592 : cpl_error_set_where(cpl_func);
610 const cpl_bivector * spec,
618 const cpl_vector * wave;
619 const cpl_vector * extr;
624 if (spec == NULL)
return NULL;
625 if (dit <= 0.0)
return NULL;
628 wave = cpl_bivector_get_x_const(spec);
629 extr = cpl_bivector_get_y_const(spec);
632 out = cpl_vector_duplicate(extr);
635 cpl_vector_divide_scalar(out, dit);
638 cpl_vector_divide_scalar(out, surface);
641 cpl_vector_multiply_scalar(out, gain);
644 factor = pow(10, mag/2.5);
645 cpl_vector_multiply_scalar(out, factor);
648 factor = (cpl_vector_get(wave, cpl_vector_get_size(wave)-1) -
649 cpl_vector_get(wave, 0)) / cpl_vector_get_size(wave);
650 cpl_vector_divide_scalar(out, factor);
653 cpl_vector_multiply_scalar(out, h*c);
654 cpl_vector_divide(out, wave);
669 const cpl_bivector * sed,
670 const cpl_vector * waves,
673 double wmin, wmax, wstep;
675 const double * sed_x;
676 const double * sed_y;
677 cpl_bivector * sed_loc;
681 cpl_bivector * out_biv;
682 double f0_jan, f0_erg, cent_val;
686 if (sed == NULL)
return NULL;
687 if (waves == NULL)
return NULL;
690 nb_sed = cpl_bivector_get_size(sed);
691 sed_x = cpl_bivector_get_x_data_const(sed);
692 sed_y = cpl_bivector_get_y_data_const(sed);
693 wstep = sed_x[1] - sed_x[0];
694 wmin = cpl_vector_get(waves, 0);
695 wmax = cpl_vector_get(waves, cpl_vector_get_size(waves)-1);
698 sed_loc = cpl_bivector_new(nb_sed + 4);
699 sed_loc_x = cpl_bivector_get_x_data(sed_loc);
700 sed_loc_y = cpl_bivector_get_y_data(sed_loc);
701 for (i=0; i<nb_sed; i++) {
702 sed_loc_x[i+2] = sed_x[i];
703 sed_loc_y[i+2] = sed_y[i];
707 sed_loc_x[1] = sed_loc_x[2] - wstep;
708 if (sed_loc_x[2] < wmin) {
709 sed_loc_x[0] = sed_loc_x[1] - wstep;
711 sed_loc_x[0] = wmin - wstep;
713 sed_loc_y[0] = 1e-20;
714 sed_loc_y[1] = 1e-20;
717 sed_loc_x[nb_sed+2] = sed_loc_x[nb_sed+1] + wstep;
718 if (sed_loc_x[nb_sed+1] > wmax) {
719 sed_loc_x[nb_sed+3] = sed_loc_x[nb_sed+2] + wstep;
721 sed_loc_x[nb_sed+3] = wmax + wstep;
723 sed_loc_y[nb_sed+2] = 1e-20;
724 sed_loc_y[nb_sed+3] = 1e-20;
727 out = cpl_vector_duplicate(waves);
728 IRPLIB_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
730 out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out);
731 IRPLIB_DIAG_PRAGMA_POP;
733 if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
734 cpl_msg_error(cpl_func,
"Cannot interpolate the wavelength");
735 cpl_bivector_unwrap_vectors(out_biv);
736 cpl_vector_delete(out);
737 cpl_bivector_delete(sed_loc);
740 cpl_bivector_unwrap_vectors(out_biv);
741 cpl_bivector_delete(sed_loc);
744 f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) );
747 f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4);
750 cent_val = cpl_vector_get(out, cpl_vector_get_size(out)/2);
751 if (cent_val <= 0.0) {
752 cpl_msg_error(cpl_func,
"Negative or 0 central value");
753 cpl_vector_delete(out);
756 cpl_vector_multiply_scalar(out, f0_erg/cent_val);
774 const char * seds_file,
785 if (seds_file == NULL)
return NULL;
786 if (sptype == NULL)
return NULL;
789 if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
790 cpl_msg_error(cpl_func,
"Cannot load the table");
795 if (!cpl_table_has_column(seds, sptype)) {
796 cpl_msg_error(cpl_func,
"SED of the requested star not available");
797 cpl_table_delete(seds);
802 nlines = cpl_table_get_nrow(seds);
805 if ((wave = cpl_vector_wrap(nlines,
806 cpl_table_get_data_double(seds,
"Wavelength"))) == NULL) {
807 cpl_msg_error(cpl_func,
"Cannot get the Wavelength column");
808 cpl_table_delete(seds);
813 if ((sed = cpl_vector_wrap(nlines,
814 cpl_table_get_data_double(seds, sptype))) == NULL) {
815 cpl_msg_error(cpl_func,
"Cannot get the SED column");
816 cpl_table_delete(seds);
817 cpl_vector_unwrap(wave);
820 tmp = cpl_bivector_wrap_vectors(wave, sed);
823 out = cpl_bivector_duplicate(tmp);
826 cpl_bivector_unwrap_vectors(tmp);
827 cpl_vector_unwrap(wave);
828 cpl_vector_unwrap(sed);
829 cpl_table_delete(seds);