36#include "irplib_stdstar.h"
37#include "irplib_utils.h"
38#include "irplib_wcs.h"
78 const cpl_frameset * set_raw,
79 const char * recipe_name,
81 const char * pro_type,
82 const char * package_name,
83 const char * ins_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;
94 if (set_in == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
95 if (set_raw == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
96 if (recipe_name == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
97 if (pro_cat == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
98 if (ins_name == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
99 if (convert_ascii_table == NULL)
return
100 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
103 out_name = cpl_sprintf(
"%s" CPL_DFS_FITS, recipe_name);
105 plist_ext = cpl_propertylist_new();
108 for (i = 0; i < nb_catalogs; i++) {
110 const cpl_frame * cur_frame = cpl_frameset_get_position_const(set_raw,
112 const char * cat_name = cpl_frame_get_filename(cur_frame);
114 cpl_table * out = convert_ascii_table(cat_name);
118 error = cpl_error_get_code() ? cpl_error_set_where(cpl_func)
119 : cpl_error_set(cpl_func, CPL_ERROR_UNSPECIFIED);
123 if (cpl_table_get_nrow(out) == 0) {
124 cpl_table_delete(out);
125 error = cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
126 "Empty catalogue %d in '%s'",
131 cpl_propertylist_update_string(plist_ext,
"EXTNAME", cat_name);
135 cpl_parameterlist * parlist = cpl_parameterlist_new();
136 cpl_propertylist * plist = cpl_propertylist_new();
139 cpl_propertylist_append_string(plist,
"INSTRUME", ins_name);
140 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, pro_cat);
141 if (pro_type != NULL) {
142 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
146 error = cpl_dfs_save_table(set_in, NULL, parlist, set_raw, NULL,
147 out, plist_ext, recipe_name, plist,
148 NULL, package_name, out_name);
149 cpl_parameterlist_delete(parlist);
150 cpl_propertylist_delete(plist);
152 error = cpl_table_save(out, NULL, plist_ext, out_name,
156 cpl_table_delete(out);
159 (void)cpl_error_set_where(cpl_func);
164 cpl_propertylist_delete(plist_ext);
185 const char * filename,
186 const char * ext_name)
191 cpl_frame * cur_frame;
195 if (filename == NULL)
return NULL;
196 if (ext_name == NULL)
return NULL;
202 cur_frame = cpl_frame_new();
203 cpl_frame_set_filename(cur_frame, filename);
204 next = cpl_frame_get_nextensions(cur_frame);
205 cpl_frame_delete(cur_frame);
208 for (i=0; i<next; i++) {
209 cpl_propertylist * plist;
210 const char * cur_name;
213 if ((plist = cpl_propertylist_load_regexp(filename, i+1,
"EXTNAME",
215 cpl_msg_error(cpl_func,
"Cannot load header of %d th extension",
219 cur_name = cpl_propertylist_get_string(plist,
"EXTNAME");
222 if (!strcmp(cur_name, ext_name)) {
225 out = cpl_table_load(filename, i+1, 1);
226 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
227 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
228 0, cpl_table_get_nrow(out),
231 cpl_msg_error(cpl_func,
"Cannot load extension %d", i+1);
232 cpl_propertylist_delete(plist);
236 }
else if (!strcmp(ext_name,
"all")) {
240 out = cpl_table_load(filename, i+1, 1);
241 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
242 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
243 0, cpl_table_get_nrow(out),
246 cpl_msg_error(cpl_func,
"Cannot load extension %d", i+1);
247 cpl_propertylist_delete(plist);
252 out_cur = cpl_table_load(filename, i+1, 1);
253 if (out_cur == NULL) {
254 cpl_msg_error(cpl_func,
"Cannot load extension %d", i+1);
255 cpl_table_delete(out);
256 cpl_propertylist_delete(plist);
259 cpl_table_new_column(out_cur, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
260 cpl_table_fill_column_window_string(out_cur, IRPLIB_STDSTAR_CAT_COL,
261 0, cpl_table_get_nrow(out_cur),
264 if (cpl_table_insert(out, out_cur,
265 cpl_table_get_nrow(out)) != CPL_ERROR_NONE) {
266 cpl_msg_error(cpl_func,
"Cannot merge table %d", i+1);
267 cpl_table_delete(out);
268 cpl_table_delete(out_cur);
269 cpl_propertylist_delete(plist);
272 cpl_table_delete(out_cur);
275 cpl_propertylist_delete(plist);
291 const cpl_table * catal)
294 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
295 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
296 "Missing column: %s",
297 IRPLIB_STDSTAR_STAR_COL);
299 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
300 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
301 "Missing column: %s",
302 IRPLIB_STDSTAR_TYPE_COL);
304 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_CAT_COL)) {
305 return cpl_error_set_message(cpl_func,
306 CPL_ERROR_ILLEGAL_INPUT,
307 "Missing column: %s",
308 IRPLIB_STDSTAR_CAT_COL);
310 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_RA_COL)) {
311 return cpl_error_set_message(cpl_func,
312 CPL_ERROR_ILLEGAL_INPUT,
313 "Missing column: %s",
314 IRPLIB_STDSTAR_RA_COL);
316 if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_DEC_COL)) {
317 return cpl_error_set_message(cpl_func,
318 CPL_ERROR_ILLEGAL_INPUT,
319 "Missing column: %s",
320 IRPLIB_STDSTAR_DEC_COL);
322 return CPL_ERROR_NONE;
346 if (cat == NULL)
return -1;
349 nrows = cpl_table_get_nrow(cat);
352 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
353 cpl_msg_error(cpl_func,
"Missing column: " IRPLIB_STDSTAR_RA_COL);
356 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
357 cpl_msg_error(cpl_func,
"Missing column: " IRPLIB_STDSTAR_DEC_COL);
361 if (cpl_table_count_selected(cat) == 0) {
362 cpl_msg_error(cpl_func,
"All %d row(s) already deselected", (
int)nrows);
367 for (i = 0; i < nrows; i++) {
368 if (cpl_table_is_selected(cat, i)) {
370 const double distance = irplib_wcs_great_circle_dist(ra, dec,
371 cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
372 cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
373 if (distance > dist) cpl_table_unselect_row(cat, i);
391 const char * mag_colname)
394 if (cat == NULL)
return -1;
395 if (mag_colname == NULL)
return -1;
398 if (!cpl_table_has_column(cat, mag_colname)) {
399 cpl_msg_error(cpl_func,
"Column %s does not exist in the catalog",
405 if (cpl_table_and_selected_double(cat, mag_colname, CPL_NOT_GREATER_THAN,
407 cpl_msg_error(cpl_func,
"Column %s does not exist in the catalog",
426 const cpl_table * cat,
430 double min_dist, distance;
436 if (cat == NULL)
return -1;
443 nrows = cpl_table_get_nrow(cat);
446 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
447 cpl_msg_error(cpl_func,
"Missing %s column", IRPLIB_STDSTAR_RA_COL);
450 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
451 cpl_msg_error(cpl_func,
"Missing %s column", IRPLIB_STDSTAR_DEC_COL);
456 for (i=0; i<nrows; i++) {
457 if (cpl_table_is_selected(cat, i)) {
459 distance = irplib_wcs_great_circle_dist(ra, dec,
460 cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
461 cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
462 if (distance <= min_dist) {
494 const char * catfile,
498 const char * catname,
507 cpl_errorstate prestate = cpl_errorstate_get();
509 const double dist = dist_am / 60.0;
513 if (catfile == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
514 if (band == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
515 if (catname == NULL)
return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
519 return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
520 "Cannot load the catalog %s from %s",
526 cpl_table_delete(catal);
527 return cpl_error_set_where(cpl_func);
532 cpl_table_delete(catal);
533 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
534 "Cannot select stars in that band");
539 cpl_table_delete(catal);
540 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
541 "Cannot select close stars");
546 cpl_table_delete(catal);
547 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
548 "Cannot get the closest star with "
549 "known %s magnitude",band);
553 *mag = cpl_table_get_double(catal, band, ind, NULL);
557 *name = cpl_strdup(cpl_table_get_string(catal,
558 IRPLIB_STDSTAR_STAR_COL, ind));
563 *type = cpl_strdup(cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL,
566 if(usedcatname != NULL)
568 if(strcmp(catname,
"all"))
569 *usedcatname = cpl_strdup(catname);
572 *usedcatname = cpl_strdup(cpl_table_get_string
573 (catal, IRPLIB_STDSTAR_CAT_COL, ind));
577 *star_ra = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
579 *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
582 cpl_table_delete(catal);
583 return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
584 : cpl_error_set_where(cpl_func);
602 const cpl_bivector * spec,
610 const cpl_vector * wave;
611 const cpl_vector * extr;
616 if (spec == NULL)
return NULL;
617 if (dit <= 0.0)
return NULL;
620 wave = cpl_bivector_get_x_const(spec);
621 extr = cpl_bivector_get_y_const(spec);
624 out = cpl_vector_duplicate(extr);
627 cpl_vector_divide_scalar(out, dit);
630 cpl_vector_divide_scalar(out, surface);
633 cpl_vector_multiply_scalar(out, gain);
636 factor = pow(10, mag/2.5);
637 cpl_vector_multiply_scalar(out, factor);
640 factor = (cpl_vector_get(wave, cpl_vector_get_size(wave)-1) -
641 cpl_vector_get(wave, 0)) / cpl_vector_get_size(wave);
642 cpl_vector_divide_scalar(out, factor);
645 cpl_vector_multiply_scalar(out, h*c);
646 cpl_vector_divide(out, wave);
661 const cpl_bivector * sed,
662 const cpl_vector * waves,
665 double wmin, wmax, wstep;
667 const double * sed_x;
668 const double * sed_y;
669 cpl_bivector * sed_loc;
673 cpl_bivector * out_biv;
674 double f0_jan, f0_erg, cent_val;
678 if (sed == NULL)
return NULL;
679 if (waves == NULL)
return NULL;
682 nb_sed = cpl_bivector_get_size(sed);
683 sed_x = cpl_bivector_get_x_data_const(sed);
684 sed_y = cpl_bivector_get_y_data_const(sed);
685 wstep = sed_x[1] - sed_x[0];
686 wmin = cpl_vector_get(waves, 0);
687 wmax = cpl_vector_get(waves, cpl_vector_get_size(waves)-1);
690 sed_loc = cpl_bivector_new(nb_sed + 4);
691 sed_loc_x = cpl_bivector_get_x_data(sed_loc);
692 sed_loc_y = cpl_bivector_get_y_data(sed_loc);
693 for (i=0; i<nb_sed; i++) {
694 sed_loc_x[i+2] = sed_x[i];
695 sed_loc_y[i+2] = sed_y[i];
699 sed_loc_x[1] = sed_loc_x[2] - wstep;
700 if (sed_loc_x[2] < wmin) {
701 sed_loc_x[0] = sed_loc_x[1] - wstep;
703 sed_loc_x[0] = wmin - wstep;
705 sed_loc_y[0] = 1e-20;
706 sed_loc_y[1] = 1e-20;
709 sed_loc_x[nb_sed+2] = sed_loc_x[nb_sed+1] + wstep;
710 if (sed_loc_x[nb_sed+1] > wmax) {
711 sed_loc_x[nb_sed+3] = sed_loc_x[nb_sed+2] + wstep;
713 sed_loc_x[nb_sed+3] = wmax + wstep;
715 sed_loc_y[nb_sed+2] = 1e-20;
716 sed_loc_y[nb_sed+3] = 1e-20;
719 out = cpl_vector_duplicate(waves);
720 IRPLIB_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
722 out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out);
723 IRPLIB_DIAG_PRAGMA_POP;
725 if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
726 cpl_msg_error(cpl_func,
"Cannot interpolate the wavelength");
727 cpl_bivector_unwrap_vectors(out_biv);
728 cpl_vector_delete(out);
729 cpl_bivector_delete(sed_loc);
732 cpl_bivector_unwrap_vectors(out_biv);
733 cpl_bivector_delete(sed_loc);
736 f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) );
739 f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4);
742 cent_val = cpl_vector_get(out, cpl_vector_get_size(out)/2);
743 if (cent_val <= 0.0) {
744 cpl_msg_error(cpl_func,
"Negative or 0 central value");
745 cpl_vector_delete(out);
748 cpl_vector_multiply_scalar(out, f0_erg/cent_val);
766 const char * seds_file,
777 if (seds_file == NULL)
return NULL;
778 if (sptype == NULL)
return NULL;
781 if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
782 cpl_msg_error(cpl_func,
"Cannot load the table");
787 if (!cpl_table_has_column(seds, sptype)) {
788 cpl_msg_error(cpl_func,
"SED of the requested star not available");
789 cpl_table_delete(seds);
794 nlines = cpl_table_get_nrow(seds);
797 if ((wave = cpl_vector_wrap(nlines,
798 cpl_table_get_data_double(seds,
"Wavelength"))) == NULL) {
799 cpl_msg_error(cpl_func,
"Cannot get the Wavelength column");
800 cpl_table_delete(seds);
805 if ((sed = cpl_vector_wrap(nlines,
806 cpl_table_get_data_double(seds, sptype))) == NULL) {
807 cpl_msg_error(cpl_func,
"Cannot get the SED column");
808 cpl_table_delete(seds);
809 cpl_vector_unwrap(wave);
812 tmp = cpl_bivector_wrap_vectors(wave, sed);
815 out = cpl_bivector_duplicate(tmp);
818 cpl_bivector_unwrap_vectors(tmp);
819 cpl_vector_unwrap(wave);
820 cpl_vector_unwrap(sed);
821 cpl_table_delete(seds);
int irplib_stdstar_select_stars_dist(cpl_table *cat, double ra, double dec, double dist)
Deselect the stars that are beyond a given distance.
int irplib_stdstar_select_stars_mag(cpl_table *cat, const char *mag_colname)
Select the stars that have a known magnitude.
cpl_error_code irplib_stdstar_check_columns_exist(const cpl_table *catal)
Check that the table has the relevant columns of a stdstar table.
int irplib_stdstar_find_closest(const cpl_table *cat, double ra, double dec)
Find the closest star.
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_table * irplib_stdstar_load_catalog(const char *filename, const char *ext_name)
Load the FITS catalog in a table.
cpl_vector * irplib_stdstar_get_conversion(const cpl_bivector *spec, double dit, double surface, double gain, double mag)
Get the conversion.
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.
cpl_vector * irplib_stdstar_get_mag_zero(const cpl_bivector *sed, const cpl_vector *waves, double cent_wl)
Get the 0 magnitude spectrum.
cpl_bivector * irplib_stdstar_get_sed(const char *seds_file, const char *sptype)
Get the SED.