irplib_stdstar.c

00001 /* $Id: irplib_stdstar.c,v 1.29 2009/05/12 13:13:23 llundin 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: llundin $
00023  * $Date: 2009/05/12 13:13:23 $
00024  * $Revision: 1.29 $
00025  * $Name: HEAD $
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 /**** Workaround wainting for CPL 4.2 ****/
00047 #ifndef CPL_DFS_PRO_CATG
00048 #define CPL_DFS_PRO_CATG    "ESO PRO CATG"
00049 #endif
00050 #ifndef CPL_DFS_PRO_TYPE
00051 #define CPL_DFS_PRO_TYPE    "ESO PRO TYPE"
00052 #endif
00053 /*****************************************/
00054 
00055 /*----------------------------------------------------------------------------*/
00059 /*----------------------------------------------------------------------------*/
00062 /*-----------------------------------------------------------------------------
00063                                    Functions code
00064  -----------------------------------------------------------------------------*/
00065 
00066 /*----------------------------------------------------------------------------*/
00085 /*----------------------------------------------------------------------------*/
00086 int irplib_stdstar_write_catalogs(
00087         cpl_frameset        *   set_in,
00088         const cpl_frameset  *   set_raw,
00089         const char          *   recipe_name,
00090         const char          *   pro_cat,
00091         const char          *   pro_type,
00092         const char          *   package_name,
00093         const char          *   ins_name,
00094         cpl_table * (*convert_ascii_table)(const char *)) 
00095 {
00096     cpl_table           *   out ;
00097     cpl_propertylist    *   plist ;
00098     cpl_propertylist    *   plist_ext ;
00099     cpl_parameterlist   *   parlist ;
00100     const char          *   cat_name ;
00101     char                *   out_name ;
00102     int                     nb_catalogs ;
00103     const cpl_frame     *   cur_frame ;
00104     int                     i ;
00105 
00106     /* Check entries */
00107     if (set_in == NULL) return -1 ;
00108     if (set_raw == NULL) return -1 ;
00109     if (recipe_name == NULL) return -1 ;
00110     if (pro_cat == NULL) return -1 ;
00111     if (ins_name == NULL) return -1 ;
00112 
00113     /* Define the file name */
00114     out_name = cpl_sprintf("%s.fits", recipe_name) ;
00115     
00116     /* Number of catalogs */
00117     nb_catalogs = cpl_frameset_get_size(set_raw) ;
00118 
00119     /* Get the catalog name */
00120     cur_frame = cpl_frameset_get_frame_const(set_raw, 0) ;
00121     cat_name = cpl_frame_get_filename(cur_frame) ;
00122     
00123     /* Create the output table */
00124     if ((out = convert_ascii_table(cat_name)) == NULL) {
00125         cpl_free(out_name) ;
00126         return -1 ;
00127     }
00128 
00129     /* Some keywords  */
00130     plist = cpl_propertylist_new() ;
00131     cpl_propertylist_append_string(plist, "INSTRUME", ins_name) ;
00132     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, pro_cat) ;
00133     if (pro_type != NULL) {
00134         cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, pro_type) ;
00135     }
00136     plist_ext = cpl_propertylist_new() ;
00137     cpl_propertylist_append_string(plist_ext, "EXTNAME", cat_name) ;
00138 
00139     /* Write the table */
00140     parlist = cpl_parameterlist_new() ;
00141     cpl_dfs_save_table(set_in, 
00142             NULL,
00143             parlist,
00144             set_raw,
00145             NULL,
00146             out,
00147             plist_ext,
00148             recipe_name,
00149             plist,
00150             NULL,
00151             package_name,
00152             out_name) ;
00153     cpl_parameterlist_delete(parlist) ;
00154     cpl_propertylist_delete(plist) ;
00155     cpl_propertylist_delete(plist_ext) ;
00156     cpl_table_delete(out) ;
00157     
00158     /* Append the next catalogs */
00159     for (i=1 ; i<nb_catalogs ; i++) {
00160         /* Get the catalog name */
00161         cur_frame = cpl_frameset_get_frame_const(set_raw, i) ;
00162         cat_name = cpl_frame_get_filename(cur_frame) ;
00163      
00164         /* Create the output table */
00165         if ((out = convert_ascii_table(cat_name)) == NULL) {
00166             cpl_free(out_name) ;
00167             return -1 ;
00168         }
00169 
00170         plist_ext = cpl_propertylist_new() ;
00171         cpl_propertylist_append_string(plist_ext, "EXTNAME", cat_name) ;
00172         cpl_table_save(out, NULL, plist_ext, out_name, CPL_IO_EXTEND) ;
00173         cpl_table_delete(out) ;
00174         cpl_propertylist_delete(plist_ext) ;
00175     }
00176     cpl_free(out_name) ;
00177     return 0 ;
00178 }
00179 
00180 /*----------------------------------------------------------------------------*/
00193 /*----------------------------------------------------------------------------*/
00194 cpl_table * irplib_stdstar_load_catalog(
00195         const char  *   filename, 
00196         const char  *   ext_name)
00197 {
00198     int                     next ;
00199     cpl_propertylist    *   plist ;
00200     const char          *   cur_name ;
00201     cpl_table           *   out ;
00202     cpl_table           *   out_cur ;
00203     cpl_frame           *   cur_frame ;
00204     int                     i ;
00205 
00206     /* Check entries */
00207     if (filename == NULL) return NULL ;
00208     if (ext_name == NULL) return NULL ;
00209     
00210     /* Initialise */
00211     out = NULL ;
00212     
00213     /* Get the number of extensions in the catalog */
00214     cur_frame = cpl_frame_new() ;
00215     cpl_frame_set_filename(cur_frame, filename) ;
00216     next = cpl_frame_get_nextensions(cur_frame) ;
00217     cpl_frame_delete(cur_frame) ;
00218 
00219     /* Loop on the extentions */
00220     for (i=0 ; i<next ; i++) {
00221         /* Check the name of the current extension */
00222         if ((plist = cpl_propertylist_load_regexp(filename, i+1, "EXTNAME", 
00223                         0)) == NULL) {
00224             cpl_msg_error(cpl_func, "Cannot load header of %d th extension",
00225                     i+1) ;
00226             return NULL ;
00227         }
00228         cur_name = cpl_propertylist_get_string(plist, "EXTNAME") ;
00229         
00230         /* Check the current extension */
00231         if (!strcmp(cur_name, ext_name)) {
00232             /* Load the table */
00233             if (out == NULL) {
00234                 out = cpl_table_load(filename, i+1, 0) ;
00235                 if (out == NULL) {
00236                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1) ;
00237                     cpl_propertylist_delete(plist) ;
00238                     return NULL ;
00239                 }
00240             }
00241         } else if (!strcmp(ext_name, "all")) {
00242             /* Load the table and append it */
00243             if (i==0) {
00244                 /* Load the first table */
00245                 out = cpl_table_load(filename, i+1, 0) ;
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, 0) ;
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                 /* Append the table */
00261                 if (cpl_table_insert(out, out_cur, 
00262                             cpl_table_get_nrow(out)) != CPL_ERROR_NONE) {
00263                     cpl_msg_error(cpl_func, "Cannot merge table %d", i+1) ;
00264                     cpl_table_delete(out) ;
00265                     cpl_table_delete(out_cur) ;
00266                     cpl_propertylist_delete(plist) ; 
00267                     return NULL ;
00268                 }
00269                 cpl_table_delete(out_cur) ;
00270             }
00271         }
00272         cpl_propertylist_delete(plist) ;
00273     }
00274     return out ;
00275 }
00276 
00277 /*----------------------------------------------------------------------------*/
00289 /*----------------------------------------------------------------------------*/
00290 int irplib_stdstar_select_stars_dist(
00291         cpl_table   *   cat, 
00292         double          ra, 
00293         double          dec, 
00294         double          dist)
00295 {
00296     double              distance ;
00297     int                 nrows ;
00298     int                 i ;
00299     
00300     /* Check entries */
00301     if (cat == NULL) return -1 ;
00302 
00303     /* Get the number of selected rows */
00304     nrows = cpl_table_get_nrow(cat) ;
00305     
00306     /* Check if the columns are there */
00307     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00308         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL) ;
00309         return -1 ;
00310     }
00311     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00312         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL) ;
00313         return -1 ;
00314     }
00315     
00316     /* Compute distances of the selected rows */
00317     for (i=0 ; i<nrows ; i++) {
00318         if (cpl_table_is_selected(cat, i)) {
00319             /* The row is selected - compute the distance */
00320             distance = irplib_wcs_great_circle_dist(ra, dec, 
00321                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00322                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));            
00323             if (distance > dist) cpl_table_unselect_row(cat, i) ;
00324         }
00325     }
00326     return 0 ;
00327 }
00328 
00329 /*----------------------------------------------------------------------------*/
00338 /*----------------------------------------------------------------------------*/
00339 int irplib_stdstar_select_stars_mag(
00340         cpl_table   *   cat, 
00341         const char  *   mag) 
00342 {
00343     /* Check entries */
00344     if (cat == NULL) return -1 ;
00345     if (mag == NULL) return -1 ;
00346 
00347     /* Check that the table has the mag column */
00348     if (!cpl_table_has_column(cat, mag)) {
00349         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00350                 mag) ;
00351         return -1 ;
00352     }
00353 
00354     /* Apply the selection */
00355     if (cpl_table_and_selected_double(cat, mag, CPL_NOT_GREATER_THAN, 
00356                 98.0) <= 0) {
00357         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00358                 mag) ;
00359         return -1 ;
00360     }
00361     return 0 ;
00362 }
00363 
00364 /*----------------------------------------------------------------------------*/
00374 /*----------------------------------------------------------------------------*/
00375 int irplib_stdstar_find_closest(
00376         const cpl_table     *   cat, 
00377         double                  ra, 
00378         double                  dec)
00379 {
00380     double              min_dist, distance ;
00381     int                 nrows ;
00382     int                 ind ;
00383     int                 i ;
00384 
00385     /* Check entries */
00386     if (cat == NULL) return -1 ;
00387 
00388     /* Initialize */
00389     min_dist = 1000.0 ;
00390     ind = -1 ;
00391 
00392     /* Get the number of selected rows */
00393     nrows = cpl_table_get_nrow(cat) ;
00394 
00395     /* Check if the columns are there */
00396     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00397         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL) ;
00398         return -1 ;
00399     }
00400     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00401         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL) ;
00402         return -1 ;
00403     }
00404     
00405     /* Compute distances of the selected rows */
00406     for (i=0 ; i<nrows ; i++) {
00407         if (cpl_table_is_selected(cat, i)) {
00408             /* The row is selected - compute the distance */
00409             distance = irplib_wcs_great_circle_dist(ra, dec,
00410                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00411                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
00412             if (distance <= min_dist) {
00413                 min_dist = distance ;
00414                 ind = i ;
00415             }
00416         }
00417     }
00418     return ind ;
00419 }
00420 
00421 /*----------------------------------------------------------------------------*/
00437 /*----------------------------------------------------------------------------*/
00438 int irplib_stdstar_get_mag(
00439         const char          *   catfile,
00440         double                  ra, 
00441         double                  dec,
00442         const char          *   band,
00443         const char          *   catname,
00444         double              *   mag,
00445         char                *   name,
00446         char                *   type,
00447         double                  dist_am)
00448 {
00449     cpl_table   *   catal ;
00450     double          dist = dist_am / 60.0 ;
00451     int             ind ;
00452 
00453     /* Check entries */
00454     if (catfile == NULL) return -1 ;
00455     if (band == NULL) return -1 ;
00456     if (catname == NULL) return -1 ;
00457     if (mag == NULL) return -1 ;
00458     if (name == NULL) return -1 ;
00459     if (type == NULL) return -1 ;
00460     
00461     /* Load the catalog */
00462     if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
00463         cpl_msg_error(cpl_func, "Cannot load the catalog %s from %s",
00464                 catname, catfile) ;
00465         return -1 ;
00466     }
00467     
00468     /* Select stars with known magnitude */
00469     cpl_msg_info(cpl_func, "Select stars with a known magnitude in band %s",
00470             band) ;
00471     if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
00472         cpl_msg_error(cpl_func, "Cannot select stars") ;
00473         cpl_table_delete(catal) ;
00474         return -1 ;
00475     }
00476 
00477     /* Select stars within a given distance */
00478     cpl_msg_info(cpl_func, "Select stars within %g arc minutes", dist_am) ;
00479     if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
00480         cpl_msg_error(cpl_func, "Cannot select stars") ;
00481         cpl_table_delete(catal) ;
00482         return -1 ;
00483     }
00484     cpl_msg_info(cpl_func, "%d stars found in the area", 
00485             cpl_table_count_selected(catal)) ;
00486 
00487     /* Take the closest */
00488     cpl_msg_info(cpl_func, "Select the closest") ;
00489     if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
00490         cpl_msg_error(cpl_func, "Cannot select the closest star") ;
00491         cpl_table_delete(catal) ;
00492         return -1 ;
00493     }
00494 
00495     *mag = cpl_table_get_double(catal, band, ind, NULL) ;
00496     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
00497         cpl_msg_error(cpl_func, "Missing %s column",
00498                 IRPLIB_STDSTAR_STAR_COL) ;
00499         cpl_table_delete(catal) ;
00500         return -1 ;
00501     }
00502     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
00503         cpl_msg_error(cpl_func, "Missing %s column",
00504                 IRPLIB_STDSTAR_TYPE_COL) ;
00505         cpl_table_delete(catal) ;
00506         return -1 ;
00507     }
00508     /* FIXME: Buffer overflow possible due to unknown source length!!!!!!!!!!
00509        :-((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((*/
00510     strcpy(name, cpl_table_get_string(catal, IRPLIB_STDSTAR_STAR_COL, ind)) ;
00511     strcpy(type, cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL, ind)) ;
00512     cpl_table_delete(catal) ;
00513     return 0 ;
00514 }
00515 
00516 /*----------------------------------------------------------------------------*/
00537 /*----------------------------------------------------------------------------*/
00538 cpl_error_code irplib_stdstar_find_star(
00539         const char          *   catfile,
00540         double                  ra, 
00541         double                  dec,
00542         const char          *   band,
00543         const char          *   catname,
00544         double              *   mag,
00545         char                *   name,
00546         char                *   type,
00547         double              *   star_ra,
00548         double              *   star_dec,
00549         double                  dist_am)
00550 {
00551     cpl_table   *   catal ;
00552     double          dist = dist_am / 60.0 ;
00553     int             ind ;
00554 
00555     /* Check entries */
00556     if (catfile == NULL) return -1 ;
00557     if (band == NULL) return -1 ;
00558     if (catname == NULL) return -1 ;
00559     
00560     /* Load the catalog */
00561     if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
00562         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
00563                                            __FILE__, __LINE__,
00564                                            "Cannot load the catalog %s from %s",
00565                                            catname, catfile);
00566     }
00567     
00568     /* Select stars with known magnitude */
00569     if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
00570         cpl_table_delete(catal) ;
00571         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00572                                            __FILE__, __LINE__,
00573                                            "Cannot select stars in that band");
00574     }
00575 
00576     /* Select stars within a given distance */
00577     if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
00578         cpl_table_delete(catal) ;
00579         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00580                                            __FILE__, __LINE__,
00581                                            "Cannot select close stars");
00582     }
00583 
00584     /* Take the closest */
00585     if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
00586         cpl_table_delete(catal) ;
00587         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00588                                            __FILE__, __LINE__,
00589                                            "Cannot get the closest star with "
00590                                            "known %s magnitude",band);
00591     }
00592 
00593     if(mag != NULL)
00594         *mag = cpl_table_get_double(catal, band, ind, NULL) ;
00595     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
00596         cpl_table_delete(catal) ;
00597         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00598                                            __FILE__, __LINE__,
00599                                            "Missing column: %s",
00600                                            IRPLIB_STDSTAR_STAR_COL);
00601     }
00602     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
00603         cpl_table_delete(catal) ;
00604         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00605                                            __FILE__, __LINE__,
00606                                            "Missing column: %s",
00607                                            IRPLIB_STDSTAR_TYPE_COL);
00608     }
00609     /* FIXME: Buffer overflow possible due to unknown source length!!!!!!!!!!
00610        :-((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((*/
00611     if(name != NULL)
00612         strcpy(name, cpl_table_get_string(catal, IRPLIB_STDSTAR_STAR_COL, ind));
00613     if(type != NULL)
00614         strcpy(type, cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL, ind));
00615     if(star_ra != NULL)
00616         *star_ra  = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
00617     if(star_dec != NULL)
00618     *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
00619     
00620     /* Free and return */
00621     cpl_table_delete(catal);
00622     return cpl_error_get_code();
00623 }
00624 
00625 /*----------------------------------------------------------------------------*/
00638 /*----------------------------------------------------------------------------*/
00639 cpl_vector * irplib_stdstar_get_conversion(
00640         const cpl_bivector  *   spec,
00641         double                  dit,
00642         double                  surface,
00643         double                  gain,
00644         double                  mag)
00645 {
00646     double                      h = 6.62e-27 ;
00647     double                      c = 3e18 ;
00648     const cpl_vector    *       wave ;
00649     const cpl_vector    *       extr ;
00650     cpl_vector          *       out ;
00651     double                      factor ;
00652 
00653     /* Test entries */
00654     if (spec == NULL) return NULL ;
00655     if (dit <= 0.0) return NULL ;
00656 
00657     /* Get the extracted spectrum */
00658     wave = cpl_bivector_get_x_const(spec) ;
00659     extr = cpl_bivector_get_y_const(spec) ;
00660 
00661     /* Get the spectrum */
00662     out = cpl_vector_duplicate(extr) ;
00663 
00664     /* Divide by DIT */
00665     cpl_vector_divide_scalar(out, dit) ;
00666 
00667     /* Divide by the surface */
00668     cpl_vector_divide_scalar(out, surface) ;
00669 
00670     /* Multiply by the gain */
00671     cpl_vector_multiply_scalar(out, gain) ;
00672 
00673     /* Multiply by the difference magnitude */
00674     factor = pow(10, mag/2.5) ;
00675     cpl_vector_multiply_scalar(out, factor) ;
00676 
00677     /* Divide by the dispersion */
00678     factor = (cpl_vector_get(wave, cpl_vector_get_size(wave)-1) -
00679             cpl_vector_get(wave, 0)) / cpl_vector_get_size(wave) ;
00680     cpl_vector_divide_scalar(out, factor) ;
00681 
00682     /* Multiply by the energy of the photon */
00683     cpl_vector_multiply_scalar(out, h*c) ;
00684     cpl_vector_divide(out, wave) ;
00685 
00686     return out ;
00687 }
00688 
00689 /*----------------------------------------------------------------------------*/
00697 /*----------------------------------------------------------------------------*/
00698 cpl_vector * irplib_stdstar_get_mag_zero(
00699         const cpl_bivector  *   sed,
00700         const cpl_vector    *   waves,
00701         double                  cent_wl)
00702 {
00703     double              wmin, wmax, wstep ;
00704     int                 nb_sed ;
00705     const double    *   sed_x ;
00706     const double    *   sed_y ;
00707     cpl_bivector    *   sed_loc ;
00708     double          *   sed_loc_x ;
00709     double          *   sed_loc_y ;
00710     cpl_vector      *   out ;
00711     cpl_bivector    *   out_biv ;
00712     double              f0_jan, f0_erg, cent_val ;
00713     int                 i ;
00714 
00715     /* Test entries */
00716     if (sed == NULL) return NULL ;
00717     if (waves == NULL) return NULL ;
00718 
00719     /* Initialise */
00720     nb_sed = cpl_bivector_get_size(sed) ;
00721     sed_x = cpl_bivector_get_x_data_const(sed) ;
00722     sed_y = cpl_bivector_get_y_data_const(sed) ;
00723     wstep = sed_x[1] - sed_x[0] ;
00724     wmin = cpl_vector_get(waves, 0) ;
00725     wmax = cpl_vector_get(waves, cpl_vector_get_size(waves)-1) ;
00726 
00727     /* Expand sed with 0 to have it bigger than the required wavelengths */
00728     sed_loc = cpl_bivector_new(nb_sed + 4) ;
00729     sed_loc_x = cpl_bivector_get_x_data(sed_loc) ;
00730     sed_loc_y = cpl_bivector_get_y_data(sed_loc) ;
00731     for (i=0 ; i<nb_sed ; i++) {
00732         sed_loc_x[i+2] = sed_x[i] ;
00733         sed_loc_y[i+2] = sed_y[i] ;
00734     }
00735 
00736     /* Low bound */
00737     sed_loc_x[1] = sed_loc_x[2] - wstep ;
00738     if (sed_loc_x[2] < wmin) {
00739         sed_loc_x[0] = sed_loc_x[1] - wstep ;
00740     } else {
00741         sed_loc_x[0] = wmin - wstep ;
00742     }
00743     sed_loc_y[0] = 1e-20 ;
00744     sed_loc_y[1] = 1e-20 ;
00745 
00746     /* High bound */
00747     sed_loc_x[nb_sed+2] = sed_loc_x[nb_sed+1] + wstep ;
00748     if (sed_loc_x[nb_sed+1] > wmax) {
00749         sed_loc_x[nb_sed+3] = sed_loc_x[nb_sed+2] + wstep ;
00750     } else {
00751         sed_loc_x[nb_sed+3] = wmax + wstep ;
00752     }
00753     sed_loc_y[nb_sed+2] = 1e-20 ;
00754     sed_loc_y[nb_sed+3] = 1e-20 ;
00755 
00756     /* Create the output bivector */
00757     out = cpl_vector_duplicate(waves) ;
00758     out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out) ;
00759 
00760     /* Interpolate */
00761     if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
00762         cpl_msg_error(cpl_func, "Cannot interpolate the wavelength") ;
00763         cpl_bivector_unwrap_vectors(out_biv) ;
00764         cpl_vector_delete(out) ;
00765         cpl_bivector_delete(sed_loc) ;
00766         return NULL ;
00767     }
00768     cpl_bivector_unwrap_vectors(out_biv) ;
00769     cpl_bivector_delete(sed_loc) ;
00770 
00771     /* Compute f0_jan */
00772     f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) ) ;
00773 
00774     /* Convert f0 Jansky -> ergs/s/cm^2/Angstrom */
00775     f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4) ;
00776 
00777     /* Scale out so that the central value is f0 */
00778     cent_val = cpl_vector_get(out, cpl_vector_get_size(out)/2) ;
00779     if (cent_val <= 0.0) {
00780         cpl_msg_error(cpl_func, "Negative or 0 central value") ;
00781         cpl_vector_delete(out) ;
00782         return NULL ;
00783     }
00784     cpl_vector_multiply_scalar(out, f0_erg/cent_val) ;
00785 
00786     /* Return */
00787     return out ;
00788 }
00789 
00790 /*----------------------------------------------------------------------------*/
00800 /*----------------------------------------------------------------------------*/
00801 cpl_bivector * irplib_stdstar_get_sed(
00802         const char  *   seds_file,
00803         const char  *   sptype)
00804 {
00805     cpl_table           *   seds ;
00806     cpl_bivector        *   out ;
00807     cpl_vector          *   wave ;
00808     cpl_vector          *   sed ;
00809     cpl_bivector        *   tmp ;
00810     int                     nlines ;
00811 
00812     /* Test entries */
00813     if (seds_file == NULL) return NULL ;
00814     if (sptype == NULL) return NULL ;
00815 
00816     /* Load the table */
00817     if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
00818         cpl_msg_error(cpl_func, "Cannot load the table") ;
00819         return NULL ;
00820     }
00821 
00822     /* Check if the column is there */
00823     if (!cpl_table_has_column(seds, sptype)) {
00824         cpl_msg_error(cpl_func, "SED of the requested star not available") ;
00825         cpl_table_delete(seds) ;
00826         return NULL ;
00827     }
00828 
00829     /* Get the nb lines */
00830     nlines = cpl_table_get_nrow(seds) ;
00831 
00832     /* Get the wavelength as a vector */
00833     if ((wave = cpl_vector_wrap(nlines,
00834             cpl_table_get_data_double(seds, "Wavelength"))) == NULL) {
00835         cpl_msg_error(cpl_func, "Cannot get the Wavelength column") ;
00836         cpl_table_delete(seds) ;
00837         return NULL ;
00838     }
00839 
00840     /* Get the SED as a vector */
00841     if ((sed = cpl_vector_wrap(nlines,
00842             cpl_table_get_data_double(seds, sptype))) == NULL) {
00843         cpl_msg_error(cpl_func, "Cannot get the SED column") ;
00844         cpl_table_delete(seds) ;
00845         cpl_vector_unwrap(wave) ;
00846         return NULL ;
00847     }
00848     tmp = cpl_bivector_wrap_vectors(wave, sed) ;
00849 
00850     /* Create the output bivector */
00851     out = cpl_bivector_duplicate(tmp) ;
00852 
00853     /* Free */
00854     cpl_bivector_unwrap_vectors(tmp) ;
00855     cpl_vector_unwrap(wave) ;
00856     cpl_vector_unwrap(sed) ;
00857     cpl_table_delete(seds) ;
00858 
00859     /* Return  */
00860     return out ;
00861 }

Generated on Thu Mar 24 11:59:39 2011 for VISIR Pipeline Reference Manual by  doxygen 1.5.8