irplib_stdstar.c

00001 /* $Id: irplib_stdstar.c,v 1.41 2012/05/04 13:58:12 cgarcia Exp $
00002  *
00003  * This file is part of the irplib package
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: cgarcia $
00023  * $Date: 2012/05/04 13:58:12 $
00024  * $Revision: 1.41 $
00025  * $Name: uves-5_0_8 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <math.h>
00038 #include <float.h>
00039 
00040 #include <cpl.h>
00041 
00042 #include "irplib_stdstar.h"
00043 #include "irplib_utils.h"
00044 #include "irplib_wcs.h"
00045 
00046 
00047 /*----------------------------------------------------------------------------*/
00051 /*----------------------------------------------------------------------------*/
00054 /*-----------------------------------------------------------------------------
00055                                    Functions code
00056  -----------------------------------------------------------------------------*/
00057 
00058 /*----------------------------------------------------------------------------*/
00077 /*----------------------------------------------------------------------------*/
00078 int irplib_stdstar_write_catalogs(
00079         cpl_frameset        *   set_in,
00080         const cpl_frameset  *   set_raw,
00081         const char          *   recipe_name,
00082         const char          *   pro_cat,
00083         const char          *   pro_type,
00084         const char          *   package_name,
00085         const char          *   ins_name,
00086         cpl_table * (*convert_ascii_table)(const char *))
00087 {
00088     cpl_table           *   out ;
00089     cpl_propertylist    *   plist ;
00090     cpl_propertylist    *   plist_ext ;
00091     cpl_parameterlist   *   parlist ;
00092     const char          *   cat_name ;
00093     char                *   out_name ;
00094     int                     nb_catalogs ;
00095     const cpl_frame     *   cur_frame ;
00096     int                     i ;
00097 
00098     /* Check entries */
00099     if (set_in == NULL) return CPL_ERROR_NULL_INPUT;
00100     if (set_raw == NULL) return CPL_ERROR_NULL_INPUT;
00101     if (recipe_name == NULL) return CPL_ERROR_NULL_INPUT;
00102     if (pro_cat == NULL) return CPL_ERROR_NULL_INPUT;
00103     if (ins_name == NULL) return CPL_ERROR_NULL_INPUT;
00104 
00105     /* Define the file name */
00106     out_name = cpl_sprintf("%s.fits", recipe_name) ;
00107     
00108     /* Number of catalogs */
00109     nb_catalogs = cpl_frameset_get_size(set_raw) ;
00110 
00111     /* Get the catalog name */
00112     cur_frame = cpl_frameset_get_frame_const(set_raw, 0) ;
00113     cat_name = cpl_frame_get_filename(cur_frame) ;
00114     
00115     /* Create the output table */
00116     if ((out = convert_ascii_table(cat_name)) == NULL) {
00117         cpl_free(out_name) ;
00118         return CPL_ERROR_UNSPECIFIED;
00119     }
00120 
00121     /* Some keywords  */
00122     plist = cpl_propertylist_new() ;
00123     cpl_propertylist_append_string(plist, "INSTRUME", ins_name) ;
00124     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, pro_cat) ;
00125     if (pro_type != NULL) {
00126         cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, pro_type) ;
00127     }
00128     plist_ext = cpl_propertylist_new() ;
00129     cpl_propertylist_append_string(plist_ext, "EXTNAME", cat_name) ;
00130 
00131     /* Write the table */
00132     parlist = cpl_parameterlist_new() ;
00133     cpl_dfs_save_table(set_in, 
00134             NULL,
00135             parlist,
00136             set_raw,
00137             NULL,
00138             out,
00139             plist_ext,
00140             recipe_name,
00141             plist,
00142             NULL,
00143             package_name,
00144             out_name) ;
00145     cpl_parameterlist_delete(parlist) ;
00146     cpl_propertylist_delete(plist) ;
00147     cpl_propertylist_delete(plist_ext) ;
00148     cpl_table_delete(out) ;
00149     
00150     /* Append the next catalogs */
00151     for (i=1 ; i<nb_catalogs ; i++) {
00152         /* Get the catalog name */
00153         cur_frame = cpl_frameset_get_frame_const(set_raw, i) ;
00154         cat_name = cpl_frame_get_filename(cur_frame) ;
00155      
00156         /* Create the output table */
00157         if ((out = convert_ascii_table(cat_name)) == NULL) {
00158             cpl_free(out_name) ;
00159             return CPL_ERROR_UNSPECIFIED;
00160         }
00161 
00162         plist_ext = cpl_propertylist_new() ;
00163         cpl_propertylist_append_string(plist_ext, "EXTNAME", cat_name) ;
00164         cpl_table_save(out, NULL, plist_ext, out_name, CPL_IO_EXTEND) ;
00165         cpl_table_delete(out) ;
00166         cpl_propertylist_delete(plist_ext) ;
00167     }
00168     cpl_free(out_name) ;
00169     return 0 ;
00170 }
00171 
00172 /*----------------------------------------------------------------------------*/
00185 /*----------------------------------------------------------------------------*/
00186 cpl_table * irplib_stdstar_load_catalog(
00187         const char  *   filename, 
00188         const char  *   ext_name)
00189 {
00190     int                     next ;
00191     cpl_propertylist    *   plist ;
00192     const char          *   cur_name ;
00193     cpl_table           *   out ;
00194     cpl_table           *   out_cur ;
00195     cpl_frame           *   cur_frame ;
00196     int                     i ;
00197 
00198     /* Check entries */
00199     if (filename == NULL) return NULL ;
00200     if (ext_name == NULL) return NULL ;
00201     
00202     /* Initialise */
00203     out = NULL ;
00204     
00205     /* Get the number of extensions in the catalog */
00206     cur_frame = cpl_frame_new() ;
00207     cpl_frame_set_filename(cur_frame, filename) ;
00208     next = cpl_frame_get_nextensions(cur_frame) ;
00209     cpl_frame_delete(cur_frame) ;
00210 
00211     /* Loop on the extentions */
00212     for (i=0 ; i<next ; i++) {
00213         /* Check the name of the current extension */
00214         if ((plist = cpl_propertylist_load_regexp(filename, i+1, "EXTNAME", 
00215                         0)) == NULL) {
00216             cpl_msg_error(cpl_func, "Cannot load header of %d th extension",
00217                     i+1);
00218             return NULL ;
00219         }
00220         cur_name = cpl_propertylist_get_string(plist, "EXTNAME") ;
00221         
00222         /* Check the current extension */
00223         if (!strcmp(cur_name, ext_name)) {
00224             /* Load the table */
00225             if (out == NULL) {
00226                 out = cpl_table_load(filename, i+1, 1) ;
00227                 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
00228                 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
00229                                                     0, cpl_table_get_nrow(out),
00230                                                     cur_name);
00231                 if (out == NULL) {
00232                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1) ;
00233                     cpl_propertylist_delete(plist) ;
00234                     return NULL ;
00235                 }
00236             }
00237         } else if (!strcmp(ext_name, "all")) {
00238             /* Load the table and append it */
00239             if (i==0) {
00240                 /* Load the first table */
00241                 out = cpl_table_load(filename, i+1, 1) ;
00242                 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
00243                 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
00244                                                     0, cpl_table_get_nrow(out),
00245                                                     cur_name);
00246                 if (out == NULL) {
00247                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1) ;
00248                     cpl_propertylist_delete(plist) ; 
00249                     return NULL ;
00250                 }
00251             } else {
00252                 /* Load the current table */
00253                 out_cur = cpl_table_load(filename, i+1, 1) ;
00254                 if (out_cur == NULL) {
00255                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1) ;
00256                     cpl_table_delete(out) ;
00257                     cpl_propertylist_delete(plist) ; 
00258                     return NULL ;
00259                 }
00260                 cpl_table_new_column(out_cur, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
00261                 cpl_table_fill_column_window_string(out_cur, IRPLIB_STDSTAR_CAT_COL,
00262                                                     0, cpl_table_get_nrow(out_cur),
00263                                                     cur_name);
00264                 /* Append the table */
00265                 if (cpl_table_insert(out, out_cur, 
00266                             cpl_table_get_nrow(out)) != CPL_ERROR_NONE) {
00267                     cpl_msg_error(cpl_func, "Cannot merge table %d", i+1) ;
00268                     cpl_table_delete(out) ;
00269                     cpl_table_delete(out_cur) ;
00270                     cpl_propertylist_delete(plist) ; 
00271                     return NULL ;
00272                 }
00273                 cpl_table_delete(out_cur) ;
00274             }
00275         }
00276         cpl_propertylist_delete(plist) ;
00277     }
00278     return out ;
00279 }
00280 
00281 /*----------------------------------------------------------------------------*/
00290 /*----------------------------------------------------------------------------*/
00291 cpl_error_code irplib_stdstar_check_columns_exist(
00292         const cpl_table  *   catal)
00293 {
00294     /* Check for all the mandatory columns */
00295     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
00296         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00297                 "Missing column: %s",
00298                 IRPLIB_STDSTAR_STAR_COL);
00299     }
00300     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
00301         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00302                 "Missing column: %s",
00303                 IRPLIB_STDSTAR_TYPE_COL);
00304     }
00305     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_CAT_COL)) {
00306         return cpl_error_set_message(cpl_func, 
00307                 CPL_ERROR_ILLEGAL_INPUT,
00308                 "Missing column: %s",
00309                 IRPLIB_STDSTAR_CAT_COL);
00310     }
00311     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_RA_COL)) {
00312         return cpl_error_set_message(cpl_func, 
00313                 CPL_ERROR_ILLEGAL_INPUT,
00314                 "Missing column: %s",
00315                 IRPLIB_STDSTAR_RA_COL);
00316     }
00317     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_DEC_COL)) {
00318         return cpl_error_set_message(cpl_func, 
00319                 CPL_ERROR_ILLEGAL_INPUT,
00320                 "Missing column: %s",
00321                 IRPLIB_STDSTAR_DEC_COL);
00322     }
00323     return CPL_ERROR_NONE;
00324 }
00325 
00326 /*----------------------------------------------------------------------------*/
00338 /*----------------------------------------------------------------------------*/
00339 int irplib_stdstar_select_stars_dist(
00340         cpl_table   *   cat, 
00341         double          ra, 
00342         double          dec, 
00343         double          dist)
00344 {
00345     double              distance ;
00346     int                 nrows ;
00347     int                 i ;
00348     
00349     /* Check entries */
00350     if (cat == NULL) return -1;
00351 
00352     /* Get the number of selected rows */
00353     nrows = cpl_table_get_nrow(cat) ;
00354     
00355     /* Check if the columns are there */
00356     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00357         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL) ;
00358         return -1 ;
00359     }
00360     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00361         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL) ;
00362         return -1 ;
00363     }
00364     
00365     /* Compute distances of the selected rows */
00366     for (i=0 ; i<nrows ; i++) {
00367         if (cpl_table_is_selected(cat, i)) {
00368             /* The row is selected - compute the distance */
00369             distance = irplib_wcs_great_circle_dist(ra, dec, 
00370                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00371                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));            
00372             if (distance > dist) cpl_table_unselect_row(cat, i) ;
00373         }
00374     }
00375     return 0;
00376 }
00377 
00378 /*----------------------------------------------------------------------------*/
00387 /*----------------------------------------------------------------------------*/
00388 int irplib_stdstar_select_stars_mag(
00389         cpl_table   *   cat, 
00390         const char  *   mag_colname)
00391 {
00392     /* Check entries */
00393     if (cat == NULL) return -1 ;
00394     if (mag_colname == NULL) return -1 ;
00395 
00396     /* Check that the table has the mag column */
00397     if (!cpl_table_has_column(cat, mag_colname)) {
00398         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00399                 mag_colname) ;
00400         return -1 ;
00401     }
00402 
00403     /* Apply the selection */
00404     if (cpl_table_and_selected_double(cat, mag_colname, CPL_NOT_GREATER_THAN, 
00405                 98.0) <= 0) {
00406         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00407                 mag_colname) ;
00408         return -1 ;
00409     }
00410     return 0 ;
00411 }
00412 
00413 /*----------------------------------------------------------------------------*/
00423 /*----------------------------------------------------------------------------*/
00424 int irplib_stdstar_find_closest(
00425         const cpl_table     *   cat, 
00426         double                  ra, 
00427         double                  dec)
00428 {
00429     double              min_dist, distance ;
00430     int                 nrows ;
00431     int                 ind ;
00432     int                 i ;
00433 
00434     /* Check entries */
00435     if (cat == NULL) return -1 ;
00436 
00437     /* Initialize */
00438     min_dist = 1000.0 ;
00439     ind = -1 ;
00440 
00441     /* Get the number of selected rows */
00442     nrows = cpl_table_get_nrow(cat) ;
00443 
00444     /* Check if the columns are there */
00445     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00446         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL) ;
00447         return -1 ;
00448     }
00449     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00450         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL) ;
00451         return -1 ;
00452     }
00453     
00454     /* Compute distances of the selected rows */
00455     for (i=0 ; i<nrows ; i++) {
00456         if (cpl_table_is_selected(cat, i)) {
00457             /* The row is selected - compute the distance */
00458             distance = irplib_wcs_great_circle_dist(ra, dec,
00459                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00460                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
00461             if (distance <= min_dist) {
00462                 min_dist = distance ;
00463                 ind = i ;
00464             }
00465         }
00466     }
00467     return ind ;
00468 }
00469 
00470 /*----------------------------------------------------------------------------*/
00491 /*----------------------------------------------------------------------------*/
00492 cpl_error_code irplib_stdstar_find_star(
00493         const char          *   catfile,
00494         double                  ra, 
00495         double                  dec,
00496         const char          *   band,
00497         const char          *   catname,
00498         double              *   mag,
00499         char                **  name,
00500         char                **  type,
00501         char                **  usedcatname,
00502         double              *   star_ra,
00503         double              *   star_dec,
00504         double                  dist_am)
00505 {
00506     cpl_errorstate prestate = cpl_errorstate_get();
00507     cpl_table   *   catal ;
00508     const double    dist = dist_am / 60.0 ;
00509     int             ind ;
00510 
00511     /* Check entries */
00512     if (catfile == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00513     if (band    == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00514     if (catname == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00515     
00516     /* Load the catalog */
00517     if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
00518         return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
00519                                            "Cannot load the catalog %s from %s",
00520                                            catname, catfile);
00521     }
00522     
00523     /* Check the columns are present */
00524     if (irplib_stdstar_check_columns_exist(catal) != CPL_ERROR_NONE) {
00525         cpl_table_delete(catal);
00526         return cpl_error_set_where(cpl_func);
00527     }
00528     
00529     /* Select stars with known magnitude */
00530     if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
00531         cpl_table_delete(catal) ;
00532         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00533                                            "Cannot select stars in that band");
00534     }
00535 
00536     /* Select stars within a given distance */
00537     if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
00538         cpl_table_delete(catal) ;
00539         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00540                                            "Cannot select close stars");
00541     }
00542 
00543     /* Take the closest */
00544     if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
00545         cpl_table_delete(catal) ;
00546         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00547                                            "Cannot get the closest star with "
00548                                            "known %s magnitude",band);
00549     }
00550 
00551     if(mag != NULL)
00552         *mag = cpl_table_get_double(catal, band, ind, NULL) ;
00553 
00554     if(name != NULL)
00555     {
00556         *name = cpl_strdup(cpl_table_get_string(catal,
00557                                                 IRPLIB_STDSTAR_STAR_COL, ind));
00558 
00559     }
00560     if(type != NULL)
00561     {
00562         *type = cpl_strdup(cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL,
00563                                                 ind));
00564     }
00565     if(usedcatname != NULL)
00566     {
00567         if(strcmp(catname, "all"))
00568             *usedcatname = cpl_strdup(catname);
00569         else
00570         {
00571             *usedcatname = cpl_strdup(cpl_table_get_string
00572                                       (catal, IRPLIB_STDSTAR_CAT_COL, ind));
00573         }
00574     }
00575     if(star_ra != NULL)
00576         *star_ra  = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
00577     if(star_dec != NULL)
00578         *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
00579     
00580     /* Free and return */
00581     cpl_table_delete(catal);
00582     return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
00583         : cpl_error_set_where(cpl_func);
00584 }
00585 
00586 /*----------------------------------------------------------------------------*/
00599 /*----------------------------------------------------------------------------*/
00600 cpl_vector * irplib_stdstar_get_conversion(
00601         const cpl_bivector  *   spec,
00602         double                  dit,
00603         double                  surface,
00604         double                  gain,
00605         double                  mag)
00606 {
00607     double                      h = 6.62e-27 ;
00608     double                      c = 3e18 ;
00609     const cpl_vector    *       wave ;
00610     const cpl_vector    *       extr ;
00611     cpl_vector          *       out ;
00612     double                      factor ;
00613 
00614     /* Test entries */
00615     if (spec == NULL) return NULL ;
00616     if (dit <= 0.0) return NULL ;
00617 
00618     /* Get the extracted spectrum */
00619     wave = cpl_bivector_get_x_const(spec) ;
00620     extr = cpl_bivector_get_y_const(spec) ;
00621 
00622     /* Get the spectrum */
00623     out = cpl_vector_duplicate(extr) ;
00624 
00625     /* Divide by DIT */
00626     cpl_vector_divide_scalar(out, dit) ;
00627 
00628     /* Divide by the surface */
00629     cpl_vector_divide_scalar(out, surface) ;
00630 
00631     /* Multiply by the gain */
00632     cpl_vector_multiply_scalar(out, gain) ;
00633 
00634     /* Multiply by the difference magnitude */
00635     factor = pow(10, mag/2.5) ;
00636     cpl_vector_multiply_scalar(out, factor) ;
00637 
00638     /* Divide by the dispersion */
00639     factor = (cpl_vector_get(wave, cpl_vector_get_size(wave)-1) -
00640             cpl_vector_get(wave, 0)) / cpl_vector_get_size(wave) ;
00641     cpl_vector_divide_scalar(out, factor) ;
00642 
00643     /* Multiply by the energy of the photon */
00644     cpl_vector_multiply_scalar(out, h*c) ;
00645     cpl_vector_divide(out, wave) ;
00646 
00647     return out ;
00648 }
00649 
00650 /*----------------------------------------------------------------------------*/
00658 /*----------------------------------------------------------------------------*/
00659 cpl_vector * irplib_stdstar_get_mag_zero(
00660         const cpl_bivector  *   sed,
00661         const cpl_vector    *   waves,
00662         double                  cent_wl)
00663 {
00664     double              wmin, wmax, wstep ;
00665     int                 nb_sed ;
00666     const double    *   sed_x ;
00667     const double    *   sed_y ;
00668     cpl_bivector    *   sed_loc ;
00669     double          *   sed_loc_x ;
00670     double          *   sed_loc_y ;
00671     cpl_vector      *   out ;
00672     cpl_bivector    *   out_biv ;
00673     double              f0_jan, f0_erg, cent_val ;
00674     int                 i ;
00675 
00676     /* Test entries */
00677     if (sed == NULL) return NULL ;
00678     if (waves == NULL) return NULL ;
00679 
00680     /* Initialise */
00681     nb_sed = cpl_bivector_get_size(sed) ;
00682     sed_x = cpl_bivector_get_x_data_const(sed) ;
00683     sed_y = cpl_bivector_get_y_data_const(sed) ;
00684     wstep = sed_x[1] - sed_x[0] ;
00685     wmin = cpl_vector_get(waves, 0) ;
00686     wmax = cpl_vector_get(waves, cpl_vector_get_size(waves)-1) ;
00687 
00688     /* Expand sed with 0 to have it bigger than the required wavelengths */
00689     sed_loc = cpl_bivector_new(nb_sed + 4) ;
00690     sed_loc_x = cpl_bivector_get_x_data(sed_loc) ;
00691     sed_loc_y = cpl_bivector_get_y_data(sed_loc) ;
00692     for (i=0 ; i<nb_sed ; i++) {
00693         sed_loc_x[i+2] = sed_x[i] ;
00694         sed_loc_y[i+2] = sed_y[i] ;
00695     }
00696 
00697     /* Low bound */
00698     sed_loc_x[1] = sed_loc_x[2] - wstep ;
00699     if (sed_loc_x[2] < wmin) {
00700         sed_loc_x[0] = sed_loc_x[1] - wstep ;
00701     } else {
00702         sed_loc_x[0] = wmin - wstep ;
00703     }
00704     sed_loc_y[0] = 1e-20 ;
00705     sed_loc_y[1] = 1e-20 ;
00706 
00707     /* High bound */
00708     sed_loc_x[nb_sed+2] = sed_loc_x[nb_sed+1] + wstep ;
00709     if (sed_loc_x[nb_sed+1] > wmax) {
00710         sed_loc_x[nb_sed+3] = sed_loc_x[nb_sed+2] + wstep ;
00711     } else {
00712         sed_loc_x[nb_sed+3] = wmax + wstep ;
00713     }
00714     sed_loc_y[nb_sed+2] = 1e-20 ;
00715     sed_loc_y[nb_sed+3] = 1e-20 ;
00716 
00717     /* Create the output bivector */
00718     out = cpl_vector_duplicate(waves) ;
00719     out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out) ;
00720 
00721     /* Interpolate */
00722     if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
00723         cpl_msg_error(cpl_func, "Cannot interpolate the wavelength") ;
00724         cpl_bivector_unwrap_vectors(out_biv) ;
00725         cpl_vector_delete(out) ;
00726         cpl_bivector_delete(sed_loc) ;
00727         return NULL ;
00728     }
00729     cpl_bivector_unwrap_vectors(out_biv) ;
00730     cpl_bivector_delete(sed_loc) ;
00731 
00732     /* Compute f0_jan */
00733     f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) ) ;
00734 
00735     /* Convert f0 Jansky -> ergs/s/cm^2/Angstrom */
00736     f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4) ;
00737 
00738     /* Scale out so that the central value is f0 */
00739     cent_val = cpl_vector_get(out, cpl_vector_get_size(out)/2) ;
00740     if (cent_val <= 0.0) {
00741         cpl_msg_error(cpl_func, "Negative or 0 central value") ;
00742         cpl_vector_delete(out) ;
00743         return NULL ;
00744     }
00745     cpl_vector_multiply_scalar(out, f0_erg/cent_val) ;
00746 
00747     /* Return */
00748     return out ;
00749 }
00750 
00751 /*----------------------------------------------------------------------------*/
00761 /*----------------------------------------------------------------------------*/
00762 cpl_bivector * irplib_stdstar_get_sed(
00763         const char  *   seds_file,
00764         const char  *   sptype)
00765 {
00766     cpl_table           *   seds ;
00767     cpl_bivector        *   out ;
00768     cpl_vector          *   wave ;
00769     cpl_vector          *   sed ;
00770     cpl_bivector        *   tmp ;
00771     int                     nlines ;
00772 
00773     /* Test entries */
00774     if (seds_file == NULL) return NULL ;
00775     if (sptype == NULL) return NULL ;
00776 
00777     /* Load the table */
00778     if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
00779         cpl_msg_error(cpl_func, "Cannot load the table") ;
00780         return NULL ;
00781     }
00782 
00783     /* Check if the column is there */
00784     if (!cpl_table_has_column(seds, sptype)) {
00785         cpl_msg_error(cpl_func, "SED of the requested star not available") ;
00786         cpl_table_delete(seds) ;
00787         return NULL ;
00788     }
00789 
00790     /* Get the nb lines */
00791     nlines = cpl_table_get_nrow(seds) ;
00792 
00793     /* Get the wavelength as a vector */
00794     if ((wave = cpl_vector_wrap(nlines,
00795             cpl_table_get_data_double(seds, "Wavelength"))) == NULL) {
00796         cpl_msg_error(cpl_func, "Cannot get the Wavelength column") ;
00797         cpl_table_delete(seds) ;
00798         return NULL ;
00799     }
00800 
00801     /* Get the SED as a vector */
00802     if ((sed = cpl_vector_wrap(nlines,
00803             cpl_table_get_data_double(seds, sptype))) == NULL) {
00804         cpl_msg_error(cpl_func, "Cannot get the SED column") ;
00805         cpl_table_delete(seds) ;
00806         cpl_vector_unwrap(wave) ;
00807         return NULL ;
00808     }
00809     tmp = cpl_bivector_wrap_vectors(wave, sed) ;
00810 
00811     /* Create the output bivector */
00812     out = cpl_bivector_duplicate(tmp) ;
00813 
00814     /* Free */
00815     cpl_bivector_unwrap_vectors(tmp) ;
00816     cpl_vector_unwrap(wave) ;
00817     cpl_vector_unwrap(sed) ;
00818     cpl_table_delete(seds) ;
00819 
00820     /* Return  */
00821     return out ;
00822 }

Generated on 21 May 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1