CR2RE Pipeline Reference Manual 1.6.7
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/*----------------------------------------------------------------------------*/
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) 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);
101
102 /* Define the file name */
103 out_name = cpl_sprintf("%s" CPL_DFS_FITS, recipe_name);
104
105 plist_ext = cpl_propertylist_new();
106
107 /* Process the catalogs */
108 for (i = 0; i < nb_catalogs; i++) {
109 /* Get the catalog name */
110 const cpl_frame * cur_frame = cpl_frameset_get_position_const(set_raw,
111 i);
112 const char * cat_name = cpl_frame_get_filename(cur_frame);
113
114 cpl_table * out = convert_ascii_table(cat_name);
115
116 /* Create the output table */
117 if (out == NULL) {
118 error = cpl_error_get_code() ? cpl_error_set_where(cpl_func)
119 : cpl_error_set(cpl_func, CPL_ERROR_UNSPECIFIED);
120 break;
121 }
122
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'",
127 (int)i+1, cat_name);
128 break;
129 }
130
131 cpl_propertylist_update_string(plist_ext, "EXTNAME", cat_name);
132
133 /* Write the table */
134 if (i == 0) {
135 cpl_parameterlist * parlist = cpl_parameterlist_new();
136 cpl_propertylist * plist = cpl_propertylist_new();
137
138 /* Mandatory keywords */
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,
143 pro_type);
144 }
145
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);
151 } else {
152 error = cpl_table_save(out, NULL, plist_ext, out_name,
153 CPL_IO_EXTEND);
154 }
155
156 cpl_table_delete(out);
157
158 if (error) {
159 (void)cpl_error_set_where(cpl_func);
160 break;
161 }
162 }
163
164 cpl_propertylist_delete(plist_ext);
165 cpl_free(out_name);
166
167 return error;
168}
169
170/*----------------------------------------------------------------------------*/
183/*----------------------------------------------------------------------------*/
185 const char * filename,
186 const char * ext_name)
187{
188 int next;
189 cpl_table * out;
190 cpl_table * out_cur;
191 cpl_frame * cur_frame;
192 int i;
193
194 /* Check entries */
195 if (filename == NULL) return NULL;
196 if (ext_name == NULL) return NULL;
197
198 /* Initialise */
199 out = NULL;
200
201 /* Get the number of extensions in the catalog */
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);
206
207 /* Loop on the extentions */
208 for (i=0; i<next; i++) {
209 cpl_propertylist * plist;
210 const char * cur_name;
211
212 /* Check the name of the current extension */
213 if ((plist = cpl_propertylist_load_regexp(filename, i+1, "EXTNAME",
214 0)) == NULL) {
215 cpl_msg_error(cpl_func, "Cannot load header of %d th extension",
216 i+1);
217 return NULL;
218 }
219 cur_name = cpl_propertylist_get_string(plist, "EXTNAME");
220
221 /* Check the current extension */
222 if (!strcmp(cur_name, ext_name)) {
223 /* Load the table */
224 if (out == NULL) {
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),
229 cur_name);
230 if (out == NULL) {
231 cpl_msg_error(cpl_func, "Cannot load extension %d", i+1);
232 cpl_propertylist_delete(plist);
233 return NULL;
234 }
235 }
236 } else if (!strcmp(ext_name, "all")) {
237 /* Load the table and append it */
238 if (i==0) {
239 /* Load the first table */
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),
244 cur_name);
245 if (out == NULL) {
246 cpl_msg_error(cpl_func, "Cannot load extension %d", i+1);
247 cpl_propertylist_delete(plist);
248 return NULL;
249 }
250 } else {
251 /* Load the current table */
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);
257 return NULL;
258 }
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),
262 cur_name);
263 /* Append the table */
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);
270 return NULL;
271 }
272 cpl_table_delete(out_cur);
273 }
274 }
275 cpl_propertylist_delete(plist);
276 }
277 return out;
278}
279
280/*----------------------------------------------------------------------------*/
289/*----------------------------------------------------------------------------*/
291 const cpl_table * catal)
292{
293 /* Check for all the mandatory columns */
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);
298 }
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);
303 }
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);
309 }
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);
315 }
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);
321 }
322 return CPL_ERROR_NONE;
323}
324
325/*----------------------------------------------------------------------------*/
336/*----------------------------------------------------------------------------*/
338 cpl_table * cat,
339 double ra,
340 double dec,
341 double dist)
342{
343 cpl_size i, nrows;
344
345 /* Check entries */
346 if (cat == NULL) return -1;
347
348 /* Get the number of selected rows */
349 nrows = cpl_table_get_nrow(cat);
350
351 /* Check if the columns are there */
352 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
353 cpl_msg_error(cpl_func, "Missing column: " IRPLIB_STDSTAR_RA_COL);
354 return -1;
355 }
356 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
357 cpl_msg_error(cpl_func, "Missing column: " IRPLIB_STDSTAR_DEC_COL);
358 return -1;
359 }
360
361 if (cpl_table_count_selected(cat) == 0) {
362 cpl_msg_error(cpl_func, "All %d row(s) already deselected", (int)nrows);
363 return -1;
364 }
365
366 /* Compute distances of the selected rows */
367 for (i = 0; i < nrows; i++) {
368 if (cpl_table_is_selected(cat, i)) {
369 /* The row is selected - compute the distance */
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);
374 }
375 }
376 return 0;
377}
378
379/*----------------------------------------------------------------------------*/
388/*----------------------------------------------------------------------------*/
390 cpl_table * cat,
391 const char * mag_colname)
392{
393 /* Check entries */
394 if (cat == NULL) return -1;
395 if (mag_colname == NULL) return -1;
396
397 /* Check that the table has the mag column */
398 if (!cpl_table_has_column(cat, mag_colname)) {
399 cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
400 mag_colname);
401 return -1;
402 }
403
404 /* Apply the selection */
405 if (cpl_table_and_selected_double(cat, mag_colname, CPL_NOT_GREATER_THAN,
406 98.0) <= 0) {
407 cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
408 mag_colname);
409 return -1;
410 }
411 return 0;
412}
413
414/*----------------------------------------------------------------------------*/
424/*----------------------------------------------------------------------------*/
426 const cpl_table * cat,
427 double ra,
428 double dec)
429{
430 double min_dist, distance;
431 int nrows;
432 int ind;
433 int i;
434
435 /* Check entries */
436 if (cat == NULL) return -1;
437
438 /* Initialize */
439 min_dist = 1000.0;
440 ind = -1;
441
442 /* Get the number of selected rows */
443 nrows = cpl_table_get_nrow(cat);
444
445 /* Check if the columns are there */
446 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
447 cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL);
448 return -1;
449 }
450 if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
451 cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL);
452 return -1;
453 }
454
455 /* Compute distances of the selected rows */
456 for (i=0; i<nrows; i++) {
457 if (cpl_table_is_selected(cat, i)) {
458 /* The row is selected - compute the distance */
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) {
463 min_dist = distance;
464 ind = i;
465 }
466 }
467 }
468 return ind;
469}
470
471/*----------------------------------------------------------------------------*/
492/*----------------------------------------------------------------------------*/
494 const char * catfile,
495 double ra,
496 double dec,
497 const char * band,
498 const char * catname,
499 double * mag,
500 char ** name,
501 char ** type,
502 char ** usedcatname,
503 double * star_ra,
504 double * star_dec,
505 double dist_am)
506{
507 cpl_errorstate prestate = cpl_errorstate_get();
508 cpl_table * catal;
509 const double dist = dist_am / 60.0;
510 int ind;
511
512 /* Check entries */
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);
516
517 /* Load the catalog */
518 if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
519 return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
520 "Cannot load the catalog %s from %s",
521 catname, catfile);
522 }
523
524 /* Check the columns are present */
525 if (irplib_stdstar_check_columns_exist(catal) != CPL_ERROR_NONE) {
526 cpl_table_delete(catal);
527 return cpl_error_set_where(cpl_func);
528 }
529
530 /* Select stars with known magnitude */
531 if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
532 cpl_table_delete(catal);
533 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
534 "Cannot select stars in that band");
535 }
536
537 /* Select stars within a given distance */
538 if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
539 cpl_table_delete(catal);
540 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
541 "Cannot select close stars");
542 }
543
544 /* Take the closest */
545 if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
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);
550 }
551
552 if(mag != NULL)
553 *mag = cpl_table_get_double(catal, band, ind, NULL);
554
555 if(name != NULL)
556 {
557 *name = cpl_strdup(cpl_table_get_string(catal,
558 IRPLIB_STDSTAR_STAR_COL, ind));
559
560 }
561 if(type != NULL)
562 {
563 *type = cpl_strdup(cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL,
564 ind));
565 }
566 if(usedcatname != NULL)
567 {
568 if(strcmp(catname, "all"))
569 *usedcatname = cpl_strdup(catname);
570 else
571 {
572 *usedcatname = cpl_strdup(cpl_table_get_string
573 (catal, IRPLIB_STDSTAR_CAT_COL, ind));
574 }
575 }
576 if(star_ra != NULL)
577 *star_ra = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
578 if(star_dec != NULL)
579 *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
580
581 /* Free and return */
582 cpl_table_delete(catal);
583 return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
584 : cpl_error_set_where(cpl_func);
585}
586
587/*----------------------------------------------------------------------------*/
600/*----------------------------------------------------------------------------*/
602 const cpl_bivector * spec,
603 double dit,
604 double surface,
605 double gain,
606 double mag)
607{
608 double h = 6.62e-27;
609 double c = 3e18;
610 const cpl_vector * wave;
611 const cpl_vector * extr;
612 cpl_vector * out;
613 double factor;
614
615 /* Test entries */
616 if (spec == NULL) return NULL;
617 if (dit <= 0.0) return NULL;
618
619 /* Get the extracted spectrum */
620 wave = cpl_bivector_get_x_const(spec);
621 extr = cpl_bivector_get_y_const(spec);
622
623 /* Get the spectrum */
624 out = cpl_vector_duplicate(extr);
625
626 /* Divide by DIT */
627 cpl_vector_divide_scalar(out, dit);
628
629 /* Divide by the surface */
630 cpl_vector_divide_scalar(out, surface);
631
632 /* Multiply by the gain */
633 cpl_vector_multiply_scalar(out, gain);
634
635 /* Multiply by the difference magnitude */
636 factor = pow(10, mag/2.5);
637 cpl_vector_multiply_scalar(out, factor);
638
639 /* Divide by the dispersion */
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);
643
644 /* Multiply by the energy of the photon */
645 cpl_vector_multiply_scalar(out, h*c);
646 cpl_vector_divide(out, wave);
647
648 return out;
649}
650
651/*----------------------------------------------------------------------------*/
659/*----------------------------------------------------------------------------*/
661 const cpl_bivector * sed,
662 const cpl_vector * waves,
663 double cent_wl)
664{
665 double wmin, wmax, wstep;
666 int nb_sed;
667 const double * sed_x;
668 const double * sed_y;
669 cpl_bivector * sed_loc;
670 double * sed_loc_x;
671 double * sed_loc_y;
672 cpl_vector * out;
673 cpl_bivector * out_biv;
674 double f0_jan, f0_erg, cent_val;
675 int i;
676
677 /* Test entries */
678 if (sed == NULL) return NULL;
679 if (waves == NULL) return NULL;
680
681 /* Initialise */
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);
688
689 /* Expand sed with 0 to have it bigger than the required wavelengths */
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];
696 }
697
698 /* Low bound */
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;
702 } else {
703 sed_loc_x[0] = wmin - wstep;
704 }
705 sed_loc_y[0] = 1e-20;
706 sed_loc_y[1] = 1e-20;
707
708 /* High bound */
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;
712 } else {
713 sed_loc_x[nb_sed+3] = wmax + wstep;
714 }
715 sed_loc_y[nb_sed+2] = 1e-20;
716 sed_loc_y[nb_sed+3] = 1e-20;
717
718 /* Create the output bivector */
719 out = cpl_vector_duplicate(waves);
720 IRPLIB_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
721 /* the X entry (waves) is not modified by cpl_bivector_interpolate_linear */
722 out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out);
723 IRPLIB_DIAG_PRAGMA_POP;
724 /* Interpolate */
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);
730 return NULL;
731 }
732 cpl_bivector_unwrap_vectors(out_biv);
733 cpl_bivector_delete(sed_loc);
734
735 /* Compute f0_jan */
736 f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) );
737
738 /* Convert f0 Jansky -> ergs/s/cm^2/Angstrom */
739 f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4);
740
741 /* Scale out so that the central value is f0 */
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);
746 return NULL;
747 }
748 cpl_vector_multiply_scalar(out, f0_erg/cent_val);
749
750 /* Return */
751 return out;
752}
753
754/*----------------------------------------------------------------------------*/
764/*----------------------------------------------------------------------------*/
766 const char * seds_file,
767 const char * sptype)
768{
769 cpl_table * seds;
770 cpl_bivector * out;
771 cpl_vector * wave;
772 cpl_vector * sed;
773 cpl_bivector * tmp;
774 int nlines;
775
776 /* Test entries */
777 if (seds_file == NULL) return NULL;
778 if (sptype == NULL) return NULL;
779
780 /* Load the table */
781 if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
782 cpl_msg_error(cpl_func, "Cannot load the table");
783 return NULL;
784 }
785
786 /* Check if the column is there */
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);
790 return NULL;
791 }
792
793 /* Get the nb lines */
794 nlines = cpl_table_get_nrow(seds);
795
796 /* Get the wavelength as a vector */
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);
801 return NULL;
802 }
803
804 /* Get the SED as a vector */
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);
810 return NULL;
811 }
812 tmp = cpl_bivector_wrap_vectors(wave, sed);
813
814 /* Create the output bivector */
815 out = cpl_bivector_duplicate(tmp);
816
817 /* Free */
818 cpl_bivector_unwrap_vectors(tmp);
819 cpl_vector_unwrap(wave);
820 cpl_vector_unwrap(sed);
821 cpl_table_delete(seds);
822
823 /* Return */
824 return out;
825}
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.