SINFONI Pipeline Reference Manual  2.6.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 /*-----------------------------------------------------------------------------
46  Defines
47  -----------------------------------------------------------------------------*/
48 
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
51 #else
52 /* Remove this branch once CPL versions less than 6.3 are no longer supported */
53 #define IRPLIB_FRAMESET_GET_CONST cpl_frameset_get_frame_const
54 #endif
55 
56 
57 /*----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------*/
64 /*-----------------------------------------------------------------------------
65  Functions code
66  -----------------------------------------------------------------------------*/
67 
68 /*----------------------------------------------------------------------------*/
87 /*----------------------------------------------------------------------------*/
88 cpl_error_code
89 irplib_stdstar_write_catalogs(cpl_frameset * set_in,
90  const cpl_frameset * set_raw,
91  const char * recipe_name,
92  const char * pro_cat,
93  const char * pro_type,
94  const char * package_name,
95  const char * ins_name,
96  cpl_table * (*convert_ascii_table)(const char *))
97 {
98  /* Number of catalogs */
99  const cpl_size nb_catalogs = cpl_frameset_get_size(set_raw);
100  cpl_propertylist * plist_ext;
101  char * out_name;
102  cpl_error_code error = CPL_ERROR_NONE;
103  cpl_size i;
104 
105  /* Check entries */
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);
113 
114  /* Define the file name */
115  out_name = cpl_sprintf("%s" CPL_DFS_FITS, recipe_name);
116 
117  plist_ext = cpl_propertylist_new();
118 
119  /* Process the catalogs */
120  for (i = 0; i < nb_catalogs; i++) {
121  /* Get the catalog name */
122  const cpl_frame * cur_frame = IRPLIB_FRAMESET_GET_CONST(set_raw, i);
123  const char * cat_name = cpl_frame_get_filename(cur_frame);
124 
125  cpl_table * out = convert_ascii_table(cat_name);
126 
127  /* Create the output table */
128  if (out == NULL) {
129  error = cpl_error_get_code() ? cpl_error_set_where(cpl_func)
130  : cpl_error_set(cpl_func, CPL_ERROR_UNSPECIFIED);
131  break;
132  }
133 
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'",
138  (int)i+1, cat_name);
139  break;
140  }
141 
142  cpl_propertylist_update_string(plist_ext, "EXTNAME", cat_name);
143 
144  /* Write the table */
145  if (i == 0) {
146  cpl_parameterlist * parlist = cpl_parameterlist_new();
147  cpl_propertylist * plist = cpl_propertylist_new();
148 
149  /* Mandatory keywords */
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,
154  pro_type);
155  }
156 
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);
162  } else {
163  error = cpl_table_save(out, NULL, plist_ext, out_name,
164  CPL_IO_EXTEND);
165  }
166 
167  cpl_table_delete(out);
168 
169  if (error) {
170  (void)cpl_error_set_where(cpl_func);
171  break;
172  }
173  }
174 
175  cpl_propertylist_delete(plist_ext);
176  cpl_free(out_name);
177 
178  return error;
179 }
180 
181 /*----------------------------------------------------------------------------*/
194 /*----------------------------------------------------------------------------*/
196  const char * filename,
197  const char * ext_name)
198 {
199  int next;
200  cpl_table * out;
201  cpl_table * out_cur;
202  cpl_frame * cur_frame;
203  int i;
204 
205  /* Check entries */
206  if (filename == NULL) return NULL;
207  if (ext_name == NULL) return NULL;
208 
209  /* Initialise */
210  out = NULL;
211 
212  /* Get the number of extensions in the catalog */
213  cur_frame = cpl_frame_new();
214  cpl_frame_set_filename(cur_frame, filename);
215  next = cpl_frame_get_nextensions(cur_frame);
216  cpl_frame_delete(cur_frame);
217 
218  /* Loop on the extentions */
219  for (i=0; i<next; i++) {
220  cpl_propertylist * plist;
221  const char * cur_name;
222 
223  /* Check the name of the current extension */
224  if ((plist = cpl_propertylist_load_regexp(filename, i+1, "EXTNAME",
225  0)) == NULL) {
226  cpl_msg_error(cpl_func, "Cannot load header of %d th extension",
227  i+1);
228  return NULL;
229  }
230  cur_name = cpl_propertylist_get_string(plist, "EXTNAME");
231 
232  /* Check the current extension */
233  if (!strcmp(cur_name, ext_name)) {
234  /* Load the table */
235  if (out == NULL) {
236  out = cpl_table_load(filename, i+1, 1);
237  cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
238  cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
239  0, cpl_table_get_nrow(out),
240  cur_name);
241  if (out == NULL) {
242  cpl_msg_error(cpl_func, "Cannot load extension %d", i+1);
243  cpl_propertylist_delete(plist);
244  return NULL;
245  }
246  }
247  } else if (!strcmp(ext_name, "all")) {
248  /* Load the table and append it */
249  if (i==0) {
250  /* Load the first table */
251  out = cpl_table_load(filename, i+1, 1);
252  cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, 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  } else {
262  /* Load the current table */
263  out_cur = cpl_table_load(filename, i+1, 1);
264  if (out_cur == NULL) {
265  cpl_msg_error(cpl_func, "Cannot load extension %d", i+1);
266  cpl_table_delete(out);
267  cpl_propertylist_delete(plist);
268  return NULL;
269  }
270  cpl_table_new_column(out_cur, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
271  cpl_table_fill_column_window_string(out_cur, IRPLIB_STDSTAR_CAT_COL,
272  0, cpl_table_get_nrow(out_cur),
273  cur_name);
274  /* Append the table */
275  if (cpl_table_insert(out, out_cur,
276  cpl_table_get_nrow(out)) != CPL_ERROR_NONE) {
277  cpl_msg_error(cpl_func, "Cannot merge table %d", i+1);
278  cpl_table_delete(out);
279  cpl_table_delete(out_cur);
280  cpl_propertylist_delete(plist);
281  return NULL;
282  }
283  cpl_table_delete(out_cur);
284  }
285  }
286  cpl_propertylist_delete(plist);
287  }
288  return out;
289 }
290 
291 /*----------------------------------------------------------------------------*/
300 /*----------------------------------------------------------------------------*/
302  const cpl_table * catal)
303 {
304  /* Check for all the mandatory columns */
305  if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
306  return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
307  "Missing column: %s",
308  IRPLIB_STDSTAR_STAR_COL);
309  }
310  if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
311  return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
312  "Missing column: %s",
313  IRPLIB_STDSTAR_TYPE_COL);
314  }
315  if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_CAT_COL)) {
316  return cpl_error_set_message(cpl_func,
317  CPL_ERROR_ILLEGAL_INPUT,
318  "Missing column: %s",
319  IRPLIB_STDSTAR_CAT_COL);
320  }
321  if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_RA_COL)) {
322  return cpl_error_set_message(cpl_func,
323  CPL_ERROR_ILLEGAL_INPUT,
324  "Missing column: %s",
325  IRPLIB_STDSTAR_RA_COL);
326  }
327  if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_DEC_COL)) {
328  return cpl_error_set_message(cpl_func,
329  CPL_ERROR_ILLEGAL_INPUT,
330  "Missing column: %s",
331  IRPLIB_STDSTAR_DEC_COL);
332  }
333  return CPL_ERROR_NONE;
334 }
335 
336 /*----------------------------------------------------------------------------*/
348 /*----------------------------------------------------------------------------*/
350  cpl_table * cat,
351  double ra,
352  double dec,
353  double dist)
354 {
355  double distance;
356  int nrows;
357  int i;
358 
359  /* Check entries */
360  if (cat == NULL) return -1;
361 
362  /* Get the number of selected rows */
363  nrows = cpl_table_get_nrow(cat);
364 
365  /* Check if the columns are there */
366  if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
367  cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL);
368  return -1;
369  }
370  if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
371  cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL);
372  return -1;
373  }
374 
375  /* Compute distances of the selected rows */
376  for (i=0; i<nrows; i++) {
377  if (cpl_table_is_selected(cat, i)) {
378  /* The row is selected - compute the distance */
379  distance = irplib_wcs_great_circle_dist(ra, dec,
380  cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
381  cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
382  if (distance > dist) cpl_table_unselect_row(cat, i);
383  }
384  }
385  return 0;
386 }
387 
388 /*----------------------------------------------------------------------------*/
397 /*----------------------------------------------------------------------------*/
399  cpl_table * cat,
400  const char * mag_colname)
401 {
402  /* Check entries */
403  if (cat == NULL) return -1;
404  if (mag_colname == NULL) return -1;
405 
406  /* Check that the table has the mag column */
407  if (!cpl_table_has_column(cat, mag_colname)) {
408  cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
409  mag_colname);
410  return -1;
411  }
412 
413  /* Apply the selection */
414  if (cpl_table_and_selected_double(cat, mag_colname, CPL_NOT_GREATER_THAN,
415  98.0) <= 0) {
416  cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
417  mag_colname);
418  return -1;
419  }
420  return 0;
421 }
422 
423 /*----------------------------------------------------------------------------*/
433 /*----------------------------------------------------------------------------*/
435  const cpl_table * cat,
436  double ra,
437  double dec)
438 {
439  double min_dist, distance;
440  int nrows;
441  int ind;
442  int i;
443 
444  /* Check entries */
445  if (cat == NULL) return -1;
446 
447  /* Initialize */
448  min_dist = 1000.0;
449  ind = -1;
450 
451  /* Get the number of selected rows */
452  nrows = cpl_table_get_nrow(cat);
453 
454  /* Check if the columns are there */
455  if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
456  cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL);
457  return -1;
458  }
459  if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
460  cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL);
461  return -1;
462  }
463 
464  /* Compute distances of the selected rows */
465  for (i=0; i<nrows; i++) {
466  if (cpl_table_is_selected(cat, i)) {
467  /* The row is selected - compute the distance */
468  distance = irplib_wcs_great_circle_dist(ra, dec,
469  cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
470  cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
471  if (distance <= min_dist) {
472  min_dist = distance;
473  ind = i;
474  }
475  }
476  }
477  return ind;
478 }
479 
480 /*----------------------------------------------------------------------------*/
501 /*----------------------------------------------------------------------------*/
503  const char * catfile,
504  double ra,
505  double dec,
506  const char * band,
507  const char * catname,
508  double * mag,
509  char ** name,
510  char ** type,
511  char ** usedcatname,
512  double * star_ra,
513  double * star_dec,
514  double dist_am)
515 {
516  cpl_errorstate prestate = cpl_errorstate_get();
517  cpl_table * catal;
518  const double dist = dist_am / 60.0;
519  int ind;
520 
521  /* Check entries */
522  if (catfile == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
523  if (band == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
524  if (catname == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
525 
526  /* Load the catalog */
527  if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
528  return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
529  "Cannot load the catalog %s from %s",
530  catname, catfile);
531  }
532 
533  /* Check the columns are present */
534  if (irplib_stdstar_check_columns_exist(catal) != CPL_ERROR_NONE) {
535  cpl_table_delete(catal);
536  return cpl_error_set_where(cpl_func);
537  }
538 
539  /* Select stars with known magnitude */
540  if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
541  cpl_table_delete(catal);
542  return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
543  "Cannot select stars in that band");
544  }
545 
546  /* Select stars within a given distance */
547  if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
548  cpl_table_delete(catal);
549  return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
550  "Cannot select close stars");
551  }
552 
553  /* Take the closest */
554  if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
555  cpl_table_delete(catal);
556  return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
557  "Cannot get the closest star with "
558  "known %s magnitude",band);
559  }
560 
561  if(mag != NULL)
562  *mag = cpl_table_get_double(catal, band, ind, NULL);
563 
564  if(name != NULL)
565  {
566  *name = cpl_strdup(cpl_table_get_string(catal,
567  IRPLIB_STDSTAR_STAR_COL, ind));
568 
569  }
570  if(type != NULL)
571  {
572  *type = cpl_strdup(cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL,
573  ind));
574  }
575  if(usedcatname != NULL)
576  {
577  if(strcmp(catname, "all"))
578  *usedcatname = cpl_strdup(catname);
579  else
580  {
581  *usedcatname = cpl_strdup(cpl_table_get_string
582  (catal, IRPLIB_STDSTAR_CAT_COL, ind));
583  }
584  }
585  if(star_ra != NULL)
586  *star_ra = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
587  if(star_dec != NULL)
588  *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
589 
590  /* Free and return */
591  cpl_table_delete(catal);
592  return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
593  : cpl_error_set_where(cpl_func);
594 }
595 
596 /*----------------------------------------------------------------------------*/
609 /*----------------------------------------------------------------------------*/
611  const cpl_bivector * spec,
612  double dit,
613  double surface,
614  double gain,
615  double mag)
616 {
617  double h = 6.62e-27;
618  double c = 3e18;
619  const cpl_vector * wave;
620  const cpl_vector * extr;
621  cpl_vector * out;
622  double factor;
623 
624  /* Test entries */
625  if (spec == NULL) return NULL;
626  if (dit <= 0.0) return NULL;
627 
628  /* Get the extracted spectrum */
629  wave = cpl_bivector_get_x_const(spec);
630  extr = cpl_bivector_get_y_const(spec);
631 
632  /* Get the spectrum */
633  out = cpl_vector_duplicate(extr);
634 
635  /* Divide by DIT */
636  cpl_vector_divide_scalar(out, dit);
637 
638  /* Divide by the surface */
639  cpl_vector_divide_scalar(out, surface);
640 
641  /* Multiply by the gain */
642  cpl_vector_multiply_scalar(out, gain);
643 
644  /* Multiply by the difference magnitude */
645  factor = pow(10, mag/2.5);
646  cpl_vector_multiply_scalar(out, factor);
647 
648  /* Divide by the dispersion */
649  factor = (cpl_vector_get(wave, cpl_vector_get_size(wave)-1) -
650  cpl_vector_get(wave, 0)) / cpl_vector_get_size(wave);
651  cpl_vector_divide_scalar(out, factor);
652 
653  /* Multiply by the energy of the photon */
654  cpl_vector_multiply_scalar(out, h*c);
655  cpl_vector_divide(out, wave);
656 
657  return out;
658 }
659 
660 /*----------------------------------------------------------------------------*/
668 /*----------------------------------------------------------------------------*/
670  const cpl_bivector * sed,
671  const cpl_vector * waves,
672  double cent_wl)
673 {
674  double wmin, wmax, wstep;
675  int nb_sed;
676  const double * sed_x;
677  const double * sed_y;
678  cpl_bivector * sed_loc;
679  double * sed_loc_x;
680  double * sed_loc_y;
681  cpl_vector * out;
682  cpl_bivector * out_biv;
683  double f0_jan, f0_erg, cent_val;
684  int i;
685 
686  /* Test entries */
687  if (sed == NULL) return NULL;
688  if (waves == NULL) return NULL;
689 
690  /* Initialise */
691  nb_sed = cpl_bivector_get_size(sed);
692  sed_x = cpl_bivector_get_x_data_const(sed);
693  sed_y = cpl_bivector_get_y_data_const(sed);
694  wstep = sed_x[1] - sed_x[0];
695  wmin = cpl_vector_get(waves, 0);
696  wmax = cpl_vector_get(waves, cpl_vector_get_size(waves)-1);
697 
698  /* Expand sed with 0 to have it bigger than the required wavelengths */
699  sed_loc = cpl_bivector_new(nb_sed + 4);
700  sed_loc_x = cpl_bivector_get_x_data(sed_loc);
701  sed_loc_y = cpl_bivector_get_y_data(sed_loc);
702  for (i=0; i<nb_sed; i++) {
703  sed_loc_x[i+2] = sed_x[i];
704  sed_loc_y[i+2] = sed_y[i];
705  }
706 
707  /* Low bound */
708  sed_loc_x[1] = sed_loc_x[2] - wstep;
709  if (sed_loc_x[2] < wmin) {
710  sed_loc_x[0] = sed_loc_x[1] - wstep;
711  } else {
712  sed_loc_x[0] = wmin - wstep;
713  }
714  sed_loc_y[0] = 1e-20;
715  sed_loc_y[1] = 1e-20;
716 
717  /* High bound */
718  sed_loc_x[nb_sed+2] = sed_loc_x[nb_sed+1] + wstep;
719  if (sed_loc_x[nb_sed+1] > wmax) {
720  sed_loc_x[nb_sed+3] = sed_loc_x[nb_sed+2] + wstep;
721  } else {
722  sed_loc_x[nb_sed+3] = wmax + wstep;
723  }
724  sed_loc_y[nb_sed+2] = 1e-20;
725  sed_loc_y[nb_sed+3] = 1e-20;
726 
727  /* Create the output bivector */
728  out = cpl_vector_duplicate(waves);
729  IRPLIB_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
730  /* the X entry (waves) is not modified by cpl_bivector_interpolate_linear */
731  out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out);
732  IRPLIB_DIAG_PRAGMA_POP;
733  /* Interpolate */
734  if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
735  cpl_msg_error(cpl_func, "Cannot interpolate the wavelength");
736  cpl_bivector_unwrap_vectors(out_biv);
737  cpl_vector_delete(out);
738  cpl_bivector_delete(sed_loc);
739  return NULL;
740  }
741  cpl_bivector_unwrap_vectors(out_biv);
742  cpl_bivector_delete(sed_loc);
743 
744  /* Compute f0_jan */
745  f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) );
746 
747  /* Convert f0 Jansky -> ergs/s/cm^2/Angstrom */
748  f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4);
749 
750  /* Scale out so that the central value is f0 */
751  cent_val = cpl_vector_get(out, cpl_vector_get_size(out)/2);
752  if (cent_val <= 0.0) {
753  cpl_msg_error(cpl_func, "Negative or 0 central value");
754  cpl_vector_delete(out);
755  return NULL;
756  }
757  cpl_vector_multiply_scalar(out, f0_erg/cent_val);
758 
759  /* Return */
760  return out;
761 }
762 
763 /*----------------------------------------------------------------------------*/
773 /*----------------------------------------------------------------------------*/
774 cpl_bivector * irplib_stdstar_get_sed(
775  const char * seds_file,
776  const char * sptype)
777 {
778  cpl_table * seds;
779  cpl_bivector * out;
780  cpl_vector * wave;
781  cpl_vector * sed;
782  cpl_bivector * tmp;
783  int nlines;
784 
785  /* Test entries */
786  if (seds_file == NULL) return NULL;
787  if (sptype == NULL) return NULL;
788 
789  /* Load the table */
790  if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
791  cpl_msg_error(cpl_func, "Cannot load the table");
792  return NULL;
793  }
794 
795  /* Check if the column is there */
796  if (!cpl_table_has_column(seds, sptype)) {
797  cpl_msg_error(cpl_func, "SED of the requested star not available");
798  cpl_table_delete(seds);
799  return NULL;
800  }
801 
802  /* Get the nb lines */
803  nlines = cpl_table_get_nrow(seds);
804 
805  /* Get the wavelength as a vector */
806  if ((wave = cpl_vector_wrap(nlines,
807  cpl_table_get_data_double(seds, "Wavelength"))) == NULL) {
808  cpl_msg_error(cpl_func, "Cannot get the Wavelength column");
809  cpl_table_delete(seds);
810  return NULL;
811  }
812 
813  /* Get the SED as a vector */
814  if ((sed = cpl_vector_wrap(nlines,
815  cpl_table_get_data_double(seds, sptype))) == NULL) {
816  cpl_msg_error(cpl_func, "Cannot get the SED column");
817  cpl_table_delete(seds);
818  cpl_vector_unwrap(wave);
819  return NULL;
820  }
821  tmp = cpl_bivector_wrap_vectors(wave, sed);
822 
823  /* Create the output bivector */
824  out = cpl_bivector_duplicate(tmp);
825 
826  /* Free */
827  cpl_bivector_unwrap_vectors(tmp);
828  cpl_vector_unwrap(wave);
829  cpl_vector_unwrap(sed);
830  cpl_table_delete(seds);
831 
832  /* Return */
833  return out;
834 }
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_check_columns_exist(const cpl_table *catal)
Check that the table has the relevant columns of a stdstar table.
int irplib_stdstar_select_stars_dist(cpl_table *cat, double ra, double dec, double dist)
Select the stars that are within a given distance.
int irplib_stdstar_find_closest(const cpl_table *cat, double ra, double dec)
Find the closest star.
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.
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_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_table * irplib_stdstar_load_catalog(const char *filename, const char *ext_name)
Load the FITS catalog in a table.
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.