CR2RE Pipeline Reference Manual 1.7.0
irplib_stdstar.c
1/* $Id: irplib_stdstar.c,v 1.45 2013-03-01 10:27:07 llundin Exp $
2 *
3 * This file is part of the irplib package
4 * Copyright (C) 2002,2003 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: llundin $
23 * $Date: 2013-03-01 10:27:07 $
24 * $Revision: 1.45 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35
36#include "irplib_stdstar.h"
37#include "irplib_utils.h"
38#include "irplib_wcs.h"
39#include <cpl.h>
40
41#include <string.h>
42#include <math.h>
43#include <float.h>
44
45/*----------------------------------------------------------------------------*/
49/*----------------------------------------------------------------------------*/
51
52/*-----------------------------------------------------------------------------
53 Functions code
54 -----------------------------------------------------------------------------*/
55
56/*----------------------------------------------------------------------------*/
75/*----------------------------------------------------------------------------*/
76cpl_error_code
77irplib_stdstar_write_catalogs(cpl_frameset *set_in,
78 const cpl_frameset *set_raw,
79 const char *recipe_name,
80 const char *pro_cat,
81 const char *pro_type,
82 const char *package_name,
83 const char *ins_name,
84 cpl_table *(*convert_ascii_table)(const char *))
85{
86 /* Number of catalogs */
87 const cpl_size nb_catalogs = cpl_frameset_get_size(set_raw);
88 cpl_propertylist *plist_ext;
89 char *out_name;
90 cpl_error_code error = CPL_ERROR_NONE;
91 cpl_size i;
92
93 /* Check entries */
94 if (set_in == NULL)
95 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
96 if (set_raw == NULL)
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);
100 if (pro_cat == NULL)
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);
106
107 /* Define the file name */
108 out_name = cpl_sprintf("%s" CPL_DFS_FITS, recipe_name);
109
110 plist_ext = cpl_propertylist_new();
111
112 /* Process the catalogs */
113 for (i = 0; i < nb_catalogs; i++) {
114 /* Get the catalog name */
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);
118
119 cpl_table *out = convert_ascii_table(cat_name);
120
121 /* Create the output table */
122 if (out == NULL) {
123 error = cpl_error_get_code()
124 ? cpl_error_set_where(cpl_func)
125 : cpl_error_set(cpl_func, CPL_ERROR_UNSPECIFIED);
126 break;
127 }
128
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);
134 break;
135 }
136
137 cpl_propertylist_update_string(plist_ext, "EXTNAME", cat_name);
138
139 /* Write the table */
140 if (i == 0) {
141 cpl_parameterlist *parlist = cpl_parameterlist_new();
142 cpl_propertylist *plist = cpl_propertylist_new();
143
144 /* Mandatory keywords */
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,
149 pro_type);
150 }
151
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);
157 }
158 else {
159 error =
160 cpl_table_save(out, NULL, plist_ext, out_name, CPL_IO_EXTEND);
161 }
162
163 cpl_table_delete(out);
164
165 if (error) {
166 (void)cpl_error_set_where(cpl_func);
167 break;
168 }
169 }
170
171 cpl_propertylist_delete(plist_ext);
172 cpl_free(out_name);
173
174 return error;
175}
176
177/*----------------------------------------------------------------------------*/
190/*----------------------------------------------------------------------------*/
191cpl_table *
192irplib_stdstar_load_catalog(const char *filename, const char *ext_name)
193{
194 int next;
195 cpl_table *out;
196 cpl_table *out_cur;
197 cpl_frame *cur_frame;
198 int i;
199
200 /* Check entries */
201 if (filename == NULL)
202 return NULL;
203 if (ext_name == NULL)
204 return NULL;
205
206 /* Initialise */
207 out = NULL;
208
209 /* Get the number of extensions in the catalog */
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);
214
215 /* Loop on the extentions */
216 for (i = 0; i < next; i++) {
217 cpl_propertylist *plist;
218 const char *cur_name;
219
220 /* Check the name of the current extension */
221 if ((plist = cpl_propertylist_load_regexp(filename, i + 1, "EXTNAME",
222 0)) == NULL) {
223 cpl_msg_error(cpl_func, "Cannot load header of %d th extension",
224 i + 1);
225 return NULL;
226 }
227 cur_name = cpl_propertylist_get_string(plist, "EXTNAME");
228
229 /* Check the current extension */
230 if (!strcmp(cur_name, ext_name)) {
231 /* Load the table */
232 if (out == NULL) {
233 out = cpl_table_load(filename, i + 1, 1);
234 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL,
235 CPL_TYPE_STRING);
236 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
237 0, cpl_table_get_nrow(out),
238 cur_name);
239 if (out == NULL) {
240 cpl_msg_error(cpl_func, "Cannot load extension %d", i + 1);
241 cpl_propertylist_delete(plist);
242 return NULL;
243 }
244 }
245 }
246 else if (!strcmp(ext_name, "all")) {
247 /* Load the table and append it */
248 if (i == 0) {
249 /* Load the first table */
250 out = cpl_table_load(filename, i + 1, 1);
251 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL,
252 CPL_TYPE_STRING);
253 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
254 0, cpl_table_get_nrow(out),
255 cur_name);
256 if (out == NULL) {
257 cpl_msg_error(cpl_func, "Cannot load extension %d", i + 1);
258 cpl_propertylist_delete(plist);
259 return NULL;
260 }
261 }
262 else {
263 /* Load the current table */
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);
269 return NULL;
270 }
271 cpl_table_new_column(out_cur, IRPLIB_STDSTAR_CAT_COL,
272 CPL_TYPE_STRING);
273 cpl_table_fill_column_window_string(out_cur,
274 IRPLIB_STDSTAR_CAT_COL, 0,
275 cpl_table_get_nrow(out_cur),
276 cur_name);
277 /* Append the table */
278 if (cpl_table_insert(out, out_cur, cpl_table_get_nrow(out)) !=
279 CPL_ERROR_NONE) {
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);
284 return NULL;
285 }
286 cpl_table_delete(out_cur);
287 }
288 }
289 cpl_propertylist_delete(plist);
290 }
291 return out;
292}
293
294/*----------------------------------------------------------------------------*/
303/*----------------------------------------------------------------------------*/
304cpl_error_code
306{
307 /* Check for all the mandatory columns */
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);
312 }
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);
317 }
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);
322 }
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);
327 }
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);
332 }
333 return CPL_ERROR_NONE;
334}
335
336/*----------------------------------------------------------------------------*/
347/*----------------------------------------------------------------------------*/
348int
350 double ra,
351 double dec,
352 double dist)
353{
354 cpl_size i, nrows;
355
356 /* Check entries */
357 if (cat == NULL)
358 return -1;
359
360 /* Get the number of selected rows */
361 nrows = cpl_table_get_nrow(cat);
362
363 /* Check if the columns are there */
364 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
365 cpl_msg_error(cpl_func, "Missing column: " IRPLIB_STDSTAR_RA_COL);
366 return -1;
367 }
368 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
369 cpl_msg_error(cpl_func, "Missing column: " IRPLIB_STDSTAR_DEC_COL);
370 return -1;
371 }
372
373 if (cpl_table_count_selected(cat) == 0) {
374 cpl_msg_error(cpl_func, "All %d row(s) already deselected", (int)nrows);
375 return -1;
376 }
377
378 /* Compute distances of the selected rows */
379 for (i = 0; i < nrows; i++) {
380 if (cpl_table_is_selected(cat, i)) {
381 /* The row is selected - compute the distance */
382 const double distance = irplib_wcs_great_circle_dist(
383 ra, dec,
384 cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
385 cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
386 if (distance > dist)
387 cpl_table_unselect_row(cat, i);
388 }
389 }
390 return 0;
391}
392
393/*----------------------------------------------------------------------------*/
402/*----------------------------------------------------------------------------*/
403int
404irplib_stdstar_select_stars_mag(cpl_table *cat, const char *mag_colname)
405{
406 /* Check entries */
407 if (cat == NULL)
408 return -1;
409 if (mag_colname == NULL)
410 return -1;
411
412 /* Check that the table has the mag column */
413 if (!cpl_table_has_column(cat, mag_colname)) {
414 cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
415 mag_colname);
416 return -1;
417 }
418
419 /* Apply the selection */
420 if (cpl_table_and_selected_double(cat, mag_colname, CPL_NOT_GREATER_THAN,
421 98.0) <= 0) {
422 cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
423 mag_colname);
424 return -1;
425 }
426 return 0;
427}
428
429/*----------------------------------------------------------------------------*/
439/*----------------------------------------------------------------------------*/
440int
441irplib_stdstar_find_closest(const cpl_table *cat, double ra, double dec)
442{
443 double min_dist, distance;
444 int nrows;
445 int ind;
446 int i;
447
448 /* Check entries */
449 if (cat == NULL)
450 return -1;
451
452 /* Initialize */
453 min_dist = 1000.0;
454 ind = -1;
455
456 /* Get the number of selected rows */
457 nrows = cpl_table_get_nrow(cat);
458
459 /* Check if the columns are there */
460 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
461 cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL);
462 return -1;
463 }
464 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
465 cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL);
466 return -1;
467 }
468
469 /* Compute distances of the selected rows */
470 for (i = 0; i < nrows; i++) {
471 if (cpl_table_is_selected(cat, i)) {
472 /* The row is selected - compute the distance */
473 distance = irplib_wcs_great_circle_dist(
474 ra, dec,
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) {
478 min_dist = distance;
479 ind = i;
480 }
481 }
482 }
483 return ind;
484}
485
486/*----------------------------------------------------------------------------*/
507/*----------------------------------------------------------------------------*/
508cpl_error_code
509irplib_stdstar_find_star(const char *catfile,
510 double ra,
511 double dec,
512 const char *band,
513 const char *catname,
514 double *mag,
515 char **name,
516 char **type,
517 char **usedcatname,
518 double *star_ra,
519 double *star_dec,
520 double dist_am)
521{
522 cpl_errorstate prestate = cpl_errorstate_get();
523 cpl_table *catal;
524 const double dist = dist_am / 60.0;
525 int ind;
526
527 /* Check entries */
528 if (catfile == NULL)
529 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
530 if (band == NULL)
531 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
532 if (catname == NULL)
533 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
534
535 /* Load the catalog */
536 if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
537 return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
538 "Cannot load the catalog %s from %s",
539 catname, catfile);
540 }
541
542 /* Check the columns are present */
543 if (irplib_stdstar_check_columns_exist(catal) != CPL_ERROR_NONE) {
544 cpl_table_delete(catal);
545 return cpl_error_set_where(cpl_func);
546 }
547
548 /* Select stars with known magnitude */
549 if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
550 cpl_table_delete(catal);
551 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
552 "Cannot select stars in that band");
553 }
554
555 /* Select stars within a given distance */
556 if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
557 cpl_table_delete(catal);
558 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
559 "Cannot select close stars");
560 }
561
562 /* Take the closest */
563 if ((ind = irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
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",
568 band);
569 }
570
571 if (mag != NULL)
572 *mag = cpl_table_get_double(catal, band, ind, NULL);
573
574 if (name != NULL) {
575 *name = cpl_strdup(
576 cpl_table_get_string(catal, IRPLIB_STDSTAR_STAR_COL, ind));
577 }
578 if (type != NULL) {
579 *type = cpl_strdup(
580 cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL, ind));
581 }
582 if (usedcatname != NULL) {
583 if (strcmp(catname, "all"))
584 *usedcatname = cpl_strdup(catname);
585 else {
586 *usedcatname = cpl_strdup(
587 cpl_table_get_string(catal, IRPLIB_STDSTAR_CAT_COL, ind));
588 }
589 }
590 if (star_ra != NULL)
591 *star_ra =
592 cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
593 if (star_dec != NULL)
594 *star_dec =
595 cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
596
597 /* Free and return */
598 cpl_table_delete(catal);
599 return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
600 : cpl_error_set_where(cpl_func);
601}
602
603/*----------------------------------------------------------------------------*/
616/*----------------------------------------------------------------------------*/
617cpl_vector *
618irplib_stdstar_get_conversion(const cpl_bivector *spec,
619 double dit,
620 double surface,
621 double gain,
622 double mag)
623{
624 double h = 6.62e-27;
625 double c = 3e18;
626 const cpl_vector *wave;
627 const cpl_vector *extr;
628 cpl_vector *out;
629 double factor;
630
631 /* Test entries */
632 if (spec == NULL)
633 return NULL;
634 if (dit <= 0.0)
635 return NULL;
636
637 /* Get the extracted spectrum */
638 wave = cpl_bivector_get_x_const(spec);
639 extr = cpl_bivector_get_y_const(spec);
640
641 /* Get the spectrum */
642 out = cpl_vector_duplicate(extr);
643
644 /* Divide by DIT */
645 cpl_vector_divide_scalar(out, dit);
646
647 /* Divide by the surface */
648 cpl_vector_divide_scalar(out, surface);
649
650 /* Multiply by the gain */
651 cpl_vector_multiply_scalar(out, gain);
652
653 /* Multiply by the difference magnitude */
654 factor = pow(10, mag / 2.5);
655 cpl_vector_multiply_scalar(out, factor);
656
657 /* Divide by the dispersion */
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);
662
663 /* Multiply by the energy of the photon */
664 cpl_vector_multiply_scalar(out, h * c);
665 cpl_vector_divide(out, wave);
666
667 return out;
668}
669
670/*----------------------------------------------------------------------------*/
678/*----------------------------------------------------------------------------*/
679cpl_vector *
680irplib_stdstar_get_mag_zero(const cpl_bivector *sed,
681 const cpl_vector *waves,
682 double cent_wl)
683{
684 double wmin, wmax, wstep;
685 int nb_sed;
686 const double *sed_x;
687 const double *sed_y;
688 cpl_bivector *sed_loc;
689 double *sed_loc_x;
690 double *sed_loc_y;
691 cpl_vector *out;
692 cpl_bivector *out_biv;
693 double f0_jan, f0_erg, cent_val;
694 int i;
695
696 /* Test entries */
697 if (sed == NULL)
698 return NULL;
699 if (waves == NULL)
700 return NULL;
701
702 /* Initialise */
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);
709
710 /* Expand sed with 0 to have it bigger than the required wavelengths */
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];
717 }
718
719 /* Low bound */
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;
723 }
724 else {
725 sed_loc_x[0] = wmin - wstep;
726 }
727 sed_loc_y[0] = 1e-20;
728 sed_loc_y[1] = 1e-20;
729
730 /* High bound */
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;
734 }
735 else {
736 sed_loc_x[nb_sed + 3] = wmax + wstep;
737 }
738 sed_loc_y[nb_sed + 2] = 1e-20;
739 sed_loc_y[nb_sed + 3] = 1e-20;
740
741 /* Create the output bivector */
742 out = cpl_vector_duplicate(waves);
743 IRPLIB_DIAG_PRAGMA_PUSH_IGN(-Wcast - qual);
744 /* the X entry (waves) is not modified by cpl_bivector_interpolate_linear */
745 out_biv = cpl_bivector_wrap_vectors((cpl_vector *)waves, out);
746 IRPLIB_DIAG_PRAGMA_POP;
747 /* Interpolate */
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);
753 return NULL;
754 }
755 cpl_bivector_unwrap_vectors(out_biv);
756 cpl_bivector_delete(sed_loc);
757
758 /* Compute f0_jan */
759 f0_jan = 5513.15 / (pow(cent_wl, 3) * (exp(1.2848 / cent_wl) - 1));
760
761 /* Convert f0 Jansky -> ergs/s/cm^2/Angstrom */
762 f0_erg =
763 f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl * cent_wl * 1e4 * 1e4);
764
765 /* Scale out so that the central value is f0 */
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);
770 return NULL;
771 }
772 cpl_vector_multiply_scalar(out, f0_erg / cent_val);
773
774 /* Return */
775 return out;
776}
777
778/*----------------------------------------------------------------------------*/
788/*----------------------------------------------------------------------------*/
789cpl_bivector *
790irplib_stdstar_get_sed(const char *seds_file, const char *sptype)
791{
792 cpl_table *seds;
793 cpl_bivector *out;
794 cpl_vector *wave;
795 cpl_vector *sed;
796 cpl_bivector *tmp;
797 int nlines;
798
799 /* Test entries */
800 if (seds_file == NULL)
801 return NULL;
802 if (sptype == NULL)
803 return NULL;
804
805 /* Load the table */
806 if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
807 cpl_msg_error(cpl_func, "Cannot load the table");
808 return NULL;
809 }
810
811 /* Check if the column is there */
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);
815 return NULL;
816 }
817
818 /* Get the nb lines */
819 nlines = cpl_table_get_nrow(seds);
820
821 /* Get the wavelength as a vector */
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);
826 return NULL;
827 }
828
829 /* Get the SED as a vector */
830 if ((sed = cpl_vector_wrap(nlines,
831 cpl_table_get_data_double(seds, sptype))) ==
832 NULL) {
833 cpl_msg_error(cpl_func, "Cannot get the SED column");
834 cpl_table_delete(seds);
835 cpl_vector_unwrap(wave);
836 return NULL;
837 }
838 tmp = cpl_bivector_wrap_vectors(wave, sed);
839
840 /* Create the output bivector */
841 out = cpl_bivector_duplicate(tmp);
842
843 /* Free */
844 cpl_bivector_unwrap_vectors(tmp);
845 cpl_vector_unwrap(wave);
846 cpl_vector_unwrap(sed);
847 cpl_table_delete(seds);
848
849 /* Return */
850 return out;
851}
852
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.