irplib_stdstar.c

00001 /* $Id: irplib_stdstar.c,v 1.25 2009/02/17 10:58:36 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: 2009/02/17 10:58:36 $
00024  * $Revision: 1.25 $
00025  * $Name: naco-4_1_2 $
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 
00045 /**** Workaround wainting for CPL 4.2 ****/
00046 #ifndef CPL_DFS_PRO_CATG
00047 #define CPL_DFS_PRO_CATG    "ESO PRO CATG"
00048 #endif
00049 #ifndef CPL_DFS_PRO_TYPE
00050 #define CPL_DFS_PRO_TYPE    "ESO PRO TYPE"
00051 #endif
00052 /*****************************************/
00053 
00054 /*----------------------------------------------------------------------------*/
00058 /*----------------------------------------------------------------------------*/
00061 /*-----------------------------------------------------------------------------
00062                                    Functions code
00063  -----------------------------------------------------------------------------*/
00064 
00065 /*----------------------------------------------------------------------------*/
00084 /*----------------------------------------------------------------------------*/
00085 int irplib_stdstar_write_catalogs(
00086         cpl_frameset        *   set_in,
00087         const cpl_frameset  *   set_raw,
00088         const char          *   recipe_name,
00089         const char          *   pro_cat,
00090         const char          *   pro_type,
00091         const char          *   package_name,
00092         const char          *   ins_name,
00093         cpl_table * (*convert_ascii_table)(const char *)) 
00094 {
00095     cpl_table           *   out ;
00096     cpl_propertylist    *   plist ;
00097     cpl_propertylist    *   plist_ext ;
00098     cpl_parameterlist   *   parlist ;
00099     const char          *   cat_name ;
00100     char                *   out_name ;
00101     int                     nb_catalogs ;
00102     const cpl_frame     *   cur_frame ;
00103     int                     i ;
00104 
00105     /* Check entries */
00106     if (set_in == NULL) return -1 ;
00107     if (set_raw == NULL) return -1 ;
00108     if (recipe_name == NULL) return -1 ;
00109     if (pro_cat == NULL) return -1 ;
00110     if (ins_name == NULL) return -1 ;
00111 
00112     /* Define the file name */
00113     out_name = irplib_sprintf("%s.fits", recipe_name) ;
00114     
00115     /* Number of catalogs */
00116     nb_catalogs = cpl_frameset_get_size(set_raw) ;
00117 
00118     /* Get the catalog name */
00119     cur_frame = cpl_frameset_get_frame_const(set_raw, 0) ;
00120     cat_name = cpl_frame_get_filename(cur_frame) ;
00121     
00122     /* Create the output table */
00123     if ((out = convert_ascii_table(cat_name)) == NULL) {
00124         cpl_free(out_name) ;
00125         return -1 ;
00126     }
00127 
00128     /* Some keywords  */
00129     plist = cpl_propertylist_new() ;
00130     cpl_propertylist_append_string(plist, "INSTRUME", ins_name) ;
00131 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00132     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, pro_cat) ;
00133 #endif
00134     if (pro_type != NULL) {
00135         cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, pro_type) ;
00136     }
00137     plist_ext = cpl_propertylist_new() ;
00138     cpl_propertylist_append_string(plist_ext, "EXTNAME", cat_name) ;
00139 
00140     /* Write the table */
00141     parlist = cpl_parameterlist_new() ;
00142 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00143     cpl_dfs_save_table(set_in, 
00144             NULL,
00145             parlist,
00146             set_raw,
00147             NULL,
00148             out,
00149             plist_ext,
00150             recipe_name,
00151             plist,
00152             NULL,
00153             package_name,
00154             out_name) ;
00155 #else
00156     cpl_dfs_save_table(set_in,
00157             parlist,
00158             set_raw,
00159             out,
00160             plist_ext,
00161             recipe_name,
00162             pro_cat,
00163             plist,
00164             NULL,
00165             package_name,
00166             out_name) ;
00167 #endif
00168     cpl_parameterlist_delete(parlist) ;
00169     cpl_propertylist_delete(plist) ;
00170     cpl_propertylist_delete(plist_ext) ;
00171     cpl_table_delete(out) ;
00172     
00173     /* Append the next catalogs */
00174     for (i=1 ; i<nb_catalogs ; i++) {
00175         /* Get the catalog name */
00176         cur_frame = cpl_frameset_get_frame_const(set_raw, i) ;
00177         cat_name = cpl_frame_get_filename(cur_frame) ;
00178      
00179         /* Create the output table */
00180         if ((out = convert_ascii_table(cat_name)) == NULL) {
00181             cpl_free(out_name) ;
00182             return -1 ;
00183         }
00184 
00185         plist_ext = cpl_propertylist_new() ;
00186         cpl_propertylist_append_string(plist_ext, "EXTNAME", cat_name) ;
00187         cpl_table_save(out, NULL, plist_ext, out_name, CPL_IO_EXTEND) ;
00188         cpl_table_delete(out) ;
00189         cpl_propertylist_delete(plist_ext) ;
00190     }
00191     cpl_free(out_name) ;
00192     return 0 ;
00193 }
00194 
00195 /*----------------------------------------------------------------------------*/
00208 /*----------------------------------------------------------------------------*/
00209 cpl_table * irplib_stdstar_load_catalog(
00210         const char  *   filename, 
00211         const char  *   ext_name)
00212 {
00213     int                     next ;
00214     cpl_propertylist    *   plist ;
00215     const char          *   cur_name ;
00216     cpl_table           *   out ;
00217     cpl_table           *   out_cur ;
00218     cpl_frame           *   cur_frame ;
00219     int                     i ;
00220 
00221     /* Check entries */
00222     if (filename == NULL) return NULL ;
00223     if (ext_name == NULL) return NULL ;
00224     
00225     /* Initialise */
00226     out = NULL ;
00227     
00228     /* Get the number of extensions in the catalog */
00229     cur_frame = cpl_frame_new() ;
00230     cpl_frame_set_filename(cur_frame, filename) ;
00231     next = cpl_frame_get_nextensions(cur_frame) ;
00232     cpl_frame_delete(cur_frame) ;
00233 
00234     /* Loop on the extentions */
00235     for (i=0 ; i<next ; i++) {
00236         /* Check the name of the current extension */
00237         if ((plist = cpl_propertylist_load_regexp(filename, i+1, "EXTNAME", 
00238                         0)) == NULL) {
00239             cpl_msg_error(cpl_func, "Cannot load header of %d th extension",
00240                     i+1) ;
00241             return NULL ;
00242         }
00243         cur_name = cpl_propertylist_get_string(plist, "EXTNAME") ;
00244         
00245         /* Check the current extension */
00246         if (!strcmp(cur_name, ext_name)) {
00247             /* Load the table */
00248             if (out == NULL) {
00249                 out = cpl_table_load(filename, i+1, 0) ;
00250                 if (out == NULL) {
00251                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1) ;
00252                     cpl_propertylist_delete(plist) ;
00253                     return NULL ;
00254                 }
00255             }
00256         } else if (!strcmp(ext_name, "all")) {
00257             /* Load the table and append it */
00258             if (i==0) {
00259                 /* Load the first table */
00260                 out = cpl_table_load(filename, i+1, 0) ;
00261                 if (out == NULL) {
00262                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1) ;
00263                     cpl_propertylist_delete(plist) ; 
00264                     return NULL ;
00265                 }
00266             } else {
00267                 /* Load the current table */
00268                 out_cur = cpl_table_load(filename, i+1, 0) ;
00269                 if (out_cur == NULL) {
00270                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1) ;
00271                     cpl_table_delete(out) ;
00272                     cpl_propertylist_delete(plist) ; 
00273                     return NULL ;
00274                 }
00275                 /* Append the table */
00276                 if (cpl_table_insert(out, out_cur, 
00277                             cpl_table_get_nrow(out)) != CPL_ERROR_NONE) {
00278                     cpl_msg_error(cpl_func, "Cannot merge table %d", i+1) ;
00279                     cpl_table_delete(out) ;
00280                     cpl_table_delete(out_cur) ;
00281                     cpl_propertylist_delete(plist) ; 
00282                     return NULL ;
00283                 }
00284                 cpl_table_delete(out_cur) ;
00285             }
00286         }
00287         cpl_propertylist_delete(plist) ;
00288     }
00289     return out ;
00290 }
00291 
00292 /*----------------------------------------------------------------------------*/
00304 /*----------------------------------------------------------------------------*/
00305 int irplib_stdstar_select_stars_dist(
00306         cpl_table   *   cat, 
00307         double          ra, 
00308         double          dec, 
00309         double          dist)
00310 {
00311     double              distance ;
00312     int                 nrows ;
00313     int                 i ;
00314     
00315     /* Check entries */
00316     if (cat == NULL) return -1 ;
00317 
00318     /* Get the number of selected rows */
00319     nrows = cpl_table_get_nrow(cat) ;
00320     
00321     /* Check if the columns are there */
00322     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00323         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL) ;
00324         return -1 ;
00325     }
00326     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00327         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL) ;
00328         return -1 ;
00329     }
00330     
00331     /* Compute distances of the selected rows */
00332     for (i=0 ; i<nrows ; i++) {
00333         if (cpl_table_is_selected(cat, i)) {
00334             /* The row is selected - compute the distance */
00335             distance = irplib_stdstar_great_circle_dist(ra, dec, 
00336                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00337                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
00338             if (distance > dist) cpl_table_unselect_row(cat, i) ;
00339         }
00340     }
00341     return 0 ;
00342 }
00343 
00344 /*----------------------------------------------------------------------------*/
00353 /*----------------------------------------------------------------------------*/
00354 int irplib_stdstar_select_stars_mag(
00355         cpl_table   *   cat, 
00356         const char  *   mag) 
00357 {
00358     /* Check entries */
00359     if (cat == NULL) return -1 ;
00360     if (mag == NULL) return -1 ;
00361 
00362     /* Check that the table has the mag column */
00363     if (!cpl_table_has_column(cat, mag)) {
00364         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00365                 mag) ;
00366         return -1 ;
00367     }
00368 
00369     /* Apply the selection */
00370     if (cpl_table_and_selected_double(cat, mag, CPL_NOT_GREATER_THAN, 
00371                 98.0) <= 0) {
00372         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00373                 mag) ;
00374         return -1 ;
00375     }
00376     return 0 ;
00377 }
00378 
00379 /*----------------------------------------------------------------------------*/
00389 /*----------------------------------------------------------------------------*/
00390 int irplib_stdstar_find_closest(
00391         const cpl_table     *   cat, 
00392         double                  ra, 
00393         double                  dec)
00394 {
00395     double              min_dist, distance ;
00396     int                 nrows ;
00397     int                 ind ;
00398     int                 i ;
00399 
00400     /* Check entries */
00401     if (cat == NULL) return -1 ;
00402 
00403     /* Initialize */
00404     min_dist = 1000.0 ;
00405     ind = -1 ;
00406 
00407     /* Get the number of selected rows */
00408     nrows = cpl_table_get_nrow(cat) ;
00409 
00410     /* Check if the columns are there */
00411     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00412         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL) ;
00413         return -1 ;
00414     }
00415     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00416         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL) ;
00417         return -1 ;
00418     }
00419     
00420     /* Compute distances of the selected rows */
00421     for (i=0 ; i<nrows ; i++) {
00422         if (cpl_table_is_selected(cat, i)) {
00423             /* The row is selected - compute the distance */
00424             distance = irplib_stdstar_great_circle_dist(ra, dec,
00425                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00426                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
00427             if (distance <= min_dist) {
00428                 min_dist = distance ;
00429                 ind = i ;
00430             }
00431         }
00432     }
00433     return ind ;
00434 }
00435 
00436 /*----------------------------------------------------------------------------*/
00452 /*----------------------------------------------------------------------------*/
00453 int irplib_stdstar_get_mag(
00454         const char          *   catfile,
00455         double                  ra, 
00456         double                  dec,
00457         const char          *   band,
00458         const char          *   catname,
00459         double              *   mag,
00460         char                *   name,
00461         char                *   type,
00462         double                  dist_am)
00463 {
00464     cpl_table   *   catal ;
00465     double          dist = dist_am / 60.0 ;
00466     int             ind ;
00467 
00468     /* Check entries */
00469     if (catfile == NULL) return -1 ;
00470     if (band == NULL) return -1 ;
00471     if (catname == NULL) return -1 ;
00472     if (mag == NULL) return -1 ;
00473     if (name == NULL) return -1 ;
00474     if (type == NULL) return -1 ;
00475     
00476     /* Load the catalog */
00477     if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
00478         cpl_msg_error(cpl_func, "Cannot load the catalog %s from %s",
00479                 catname, catfile) ;
00480         return -1 ;
00481     }
00482     
00483     /* Select stars with known magnitude */
00484     cpl_msg_info(cpl_func, "Select stars with a known magnitude in band %s",
00485             band) ;
00486     if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
00487         cpl_msg_error(cpl_func, "Cannot select stars") ;
00488         cpl_table_delete(catal) ;
00489         return -1 ;
00490     }
00491 
00492     /* Select stars within a given distance */
00493     cpl_msg_info(cpl_func, "Select stars within %g arc minutes", dist_am) ;
00494     if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
00495         cpl_msg_error(cpl_func, "Cannot select stars") ;
00496         cpl_table_delete(catal) ;
00497         return -1 ;
00498     }
00499     cpl_msg_info(cpl_func, "%d stars found in the area", 
00500             cpl_table_count_selected(catal)) ;
00501 
00502     /* Take the closest */
00503     cpl_msg_info(cpl_func, "Select the closest") ;
00504     if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
00505         cpl_msg_error(cpl_func, "Cannot select the closest star") ;
00506         cpl_table_delete(catal) ;
00507         return -1 ;
00508     }
00509 
00510     *mag = cpl_table_get_double(catal, band, ind, NULL) ;
00511     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
00512         cpl_msg_error(cpl_func, "Missing %s column",
00513                 IRPLIB_STDSTAR_STAR_COL) ;
00514         cpl_table_delete(catal) ;
00515         return -1 ;
00516     }
00517     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
00518         cpl_msg_error(cpl_func, "Missing %s column",
00519                 IRPLIB_STDSTAR_TYPE_COL) ;
00520         cpl_table_delete(catal) ;
00521         return -1 ;
00522     }
00523     /* FIXME: Buffer overflow possible due to unknown source length!!!!!!!!!!
00524        :-((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((*/
00525     strcpy(name, cpl_table_get_string(catal, IRPLIB_STDSTAR_STAR_COL, ind)) ;
00526     strcpy(type, cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL, ind)) ;
00527     cpl_table_delete(catal) ;
00528     return 0 ;
00529 }
00530 
00531 /*----------------------------------------------------------------------------*/
00552 /*----------------------------------------------------------------------------*/
00553 cpl_error_code irplib_stdstar_find_star(
00554         const char          *   catfile,
00555         double                  ra, 
00556         double                  dec,
00557         const char          *   band,
00558         const char          *   catname,
00559         double              *   mag,
00560         char                *   name,
00561         char                *   type,
00562         double              *   star_ra,
00563         double              *   star_dec,
00564         double                  dist_am)
00565 {
00566     cpl_table   *   catal ;
00567     double          dist = dist_am / 60.0 ;
00568     int             ind ;
00569 
00570     /* Check entries */
00571     if (catfile == NULL) return -1 ;
00572     if (band == NULL) return -1 ;
00573     if (catname == NULL) return -1 ;
00574     
00575     /* Load the catalog */
00576     if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
00577         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00578                                            __FILE__, __LINE__,
00579                                            "Cannot load the catalog %s from %s",
00580                                            catname, catfile);
00581     }
00582     
00583     /* Select stars with known magnitude */
00584     if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
00585         cpl_table_delete(catal) ;
00586         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00587                                            __FILE__, __LINE__,
00588                                            "Cannot select stars");
00589     }
00590 
00591     /* Select stars within a given distance */
00592     if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
00593         cpl_table_delete(catal) ;
00594         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00595                                            __FILE__, __LINE__,
00596                                            "Cannot select close stars");
00597     }
00598 
00599     /* Take the closest */
00600     if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
00601         cpl_table_delete(catal) ;
00602         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00603                                            __FILE__, __LINE__,
00604                                            "Cannot select the closest star");
00605     }
00606 
00607     if(mag != NULL)
00608         *mag = cpl_table_get_double(catal, band, ind, NULL) ;
00609     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
00610         cpl_table_delete(catal) ;
00611         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00612                                            __FILE__, __LINE__,
00613                                            "Missing column: %s",
00614                                            IRPLIB_STDSTAR_STAR_COL);
00615     }
00616     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
00617         cpl_table_delete(catal) ;
00618         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00619                                            __FILE__, __LINE__,
00620                                            "Missing column: %s",
00621                                            IRPLIB_STDSTAR_TYPE_COL);
00622     }
00623     /* FIXME: Buffer overflow possible due to unknown source length!!!!!!!!!!
00624        :-((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((*/
00625     if(name != NULL)
00626         strcpy(name, cpl_table_get_string(catal, IRPLIB_STDSTAR_STAR_COL, ind));
00627     if(type != NULL)
00628         strcpy(type, cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL, ind));
00629     if(star_ra != NULL)
00630         *star_ra  = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
00631     if(star_dec != NULL)
00632     *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
00633     
00634     /* Free and return */
00635     cpl_table_delete(catal);
00636     return cpl_error_get_code();
00637 }
00638 
00639 /*----------------------------------------------------------------------------*/
00652 /*----------------------------------------------------------------------------*/
00653 cpl_vector * irplib_stdstar_get_conversion(
00654         const cpl_bivector  *   spec,
00655         double                  dit,
00656         double                  surface,
00657         double                  gain,
00658         double                  mag)
00659 {
00660     double                      h = 6.62e-27 ;
00661     double                      c = 3e18 ;
00662     const cpl_vector    *       wave ;
00663     const cpl_vector    *       extr ;
00664     cpl_vector          *       out ;
00665     double                      factor ;
00666 
00667     /* Test entries */
00668     if (spec == NULL) return NULL ;
00669     if (dit <= 0.0) return NULL ;
00670 
00671     /* Get the extracted spectrum */
00672     wave = cpl_bivector_get_x_const(spec) ;
00673     extr = cpl_bivector_get_y_const(spec) ;
00674 
00675     /* Get the spectrum */
00676     out = cpl_vector_duplicate(extr) ;
00677 
00678     /* Divide by DIT */
00679     cpl_vector_divide_scalar(out, dit) ;
00680 
00681     /* Divide by the surface */
00682     cpl_vector_divide_scalar(out, surface) ;
00683 
00684     /* Multiply by the gain */
00685     cpl_vector_multiply_scalar(out, gain) ;
00686 
00687     /* Multiply by the difference magnitude */
00688     factor = pow(10, mag/2.5) ;
00689     cpl_vector_multiply_scalar(out, factor) ;
00690 
00691     /* Divide by the dispersion */
00692     factor = (cpl_vector_get(wave, cpl_vector_get_size(wave)-1) -
00693             cpl_vector_get(wave, 0)) / cpl_vector_get_size(wave) ;
00694     cpl_vector_divide_scalar(out, factor) ;
00695 
00696     /* Multiply by the energy of the photon */
00697     cpl_vector_multiply_scalar(out, h*c) ;
00698     cpl_vector_divide(out, wave) ;
00699 
00700     return out ;
00701 }
00702 
00703 /*----------------------------------------------------------------------------*/
00711 /*----------------------------------------------------------------------------*/
00712 cpl_vector * irplib_stdstar_get_mag_zero(
00713         const cpl_bivector  *   sed,
00714         const cpl_vector    *   waves,
00715         double                  cent_wl)
00716 {
00717     double              wmin, wmax, wstep ;
00718     int                 nb_sed ;
00719     const double    *   sed_x ;
00720     const double    *   sed_y ;
00721     cpl_bivector    *   sed_loc ;
00722     double          *   sed_loc_x ;
00723     double          *   sed_loc_y ;
00724     cpl_vector      *   out ;
00725     cpl_bivector    *   out_biv ;
00726     double              f0_jan, f0_erg, cent_val ;
00727     int                 i ;
00728 
00729     /* Test entries */
00730     if (sed == NULL) return NULL ;
00731     if (waves == NULL) return NULL ;
00732 
00733     /* Initialise */
00734     nb_sed = cpl_bivector_get_size(sed) ;
00735     sed_x = cpl_bivector_get_x_data_const(sed) ;
00736     sed_y = cpl_bivector_get_y_data_const(sed) ;
00737     wstep = sed_x[1] - sed_x[0] ;
00738     wmin = cpl_vector_get(waves, 0) ;
00739     wmax = cpl_vector_get(waves, cpl_vector_get_size(waves)-1) ;
00740 
00741     /* Expand sed with 0 to have it bigger than the required wavelengths */
00742     sed_loc = cpl_bivector_new(nb_sed + 4) ;
00743     sed_loc_x = cpl_bivector_get_x_data(sed_loc) ;
00744     sed_loc_y = cpl_bivector_get_y_data(sed_loc) ;
00745     for (i=0 ; i<nb_sed ; i++) {
00746         sed_loc_x[i+2] = sed_x[i] ;
00747         sed_loc_y[i+2] = sed_y[i] ;
00748     }
00749 
00750     /* Low bound */
00751     sed_loc_x[1] = sed_loc_x[2] - wstep ;
00752     if (sed_loc_x[2] < wmin) {
00753         sed_loc_x[0] = sed_loc_x[1] - wstep ;
00754     } else {
00755         sed_loc_x[0] = wmin - wstep ;
00756     }
00757     sed_loc_y[0] = 1e-20 ;
00758     sed_loc_y[1] = 1e-20 ;
00759 
00760     /* High bound */
00761     sed_loc_x[nb_sed+2] = sed_loc_x[nb_sed+1] + wstep ;
00762     if (sed_loc_x[nb_sed+1] > wmax) {
00763         sed_loc_x[nb_sed+3] = sed_loc_x[nb_sed+2] + wstep ;
00764     } else {
00765         sed_loc_x[nb_sed+3] = wmax + wstep ;
00766     }
00767     sed_loc_y[nb_sed+2] = 1e-20 ;
00768     sed_loc_y[nb_sed+3] = 1e-20 ;
00769 
00770     /* Create the output bivector */
00771     out = cpl_vector_duplicate(waves) ;
00772     out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out) ;
00773 
00774     /* Interpolate */
00775     if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
00776         cpl_msg_error(cpl_func, "Cannot interpolate the wavelength") ;
00777         cpl_bivector_unwrap_vectors(out_biv) ;
00778         cpl_vector_delete(out) ;
00779         cpl_bivector_delete(sed_loc) ;
00780         return NULL ;
00781     }
00782     cpl_bivector_unwrap_vectors(out_biv) ;
00783     cpl_bivector_delete(sed_loc) ;
00784 
00785     /* Compute f0_jan */
00786     f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) ) ;
00787 
00788     /* Convert f0 Jansky -> ergs/s/cm^2/Angstrom */
00789     f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4) ;
00790 
00791     /* Scale out so that the central value is f0 */
00792     cent_val = cpl_vector_get(out, cpl_vector_get_size(out)/2) ;
00793     if (cent_val <= 0.0) {
00794         cpl_msg_error(cpl_func, "Negative or 0 central value") ;
00795         cpl_vector_delete(out) ;
00796         return NULL ;
00797     }
00798     cpl_vector_multiply_scalar(out, f0_erg/cent_val) ;
00799 
00800     /* Return */
00801     return out ;
00802 }
00803 
00804 /*----------------------------------------------------------------------------*/
00814 /*----------------------------------------------------------------------------*/
00815 cpl_bivector * irplib_stdstar_get_sed(
00816         const char  *   seds_file,
00817         const char  *   sptype)
00818 {
00819     cpl_table           *   seds ;
00820     cpl_bivector        *   out ;
00821     cpl_vector          *   wave ;
00822     cpl_vector          *   sed ;
00823     cpl_bivector        *   tmp ;
00824     int                     nlines ;
00825 
00826     /* Test entries */
00827     if (seds_file == NULL) return NULL ;
00828     if (sptype == NULL) return NULL ;
00829 
00830     /* Load the table */
00831     if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
00832         cpl_msg_error(cpl_func, "Cannot load the table") ;
00833         return NULL ;
00834     }
00835 
00836     /* Check if the column is there */
00837     if (!cpl_table_has_column(seds, sptype)) {
00838         cpl_msg_error(cpl_func, "SED of the requested star not available") ;
00839         cpl_table_delete(seds) ;
00840         return NULL ;
00841     }
00842 
00843     /* Get the nb lines */
00844     nlines = cpl_table_get_nrow(seds) ;
00845 
00846     /* Get the wavelength as a vector */
00847     if ((wave = cpl_vector_wrap(nlines,
00848             cpl_table_get_data_double(seds, "Wavelength"))) == NULL) {
00849         cpl_msg_error(cpl_func, "Cannot get the Wavelength column") ;
00850         cpl_table_delete(seds) ;
00851         return NULL ;
00852     }
00853 
00854     /* Get the SED as a vector */
00855     if ((sed = cpl_vector_wrap(nlines,
00856             cpl_table_get_data_double(seds, sptype))) == NULL) {
00857         cpl_msg_error(cpl_func, "Cannot get the SED column") ;
00858         cpl_table_delete(seds) ;
00859         cpl_vector_unwrap(wave) ;
00860         return NULL ;
00861     }
00862     tmp = cpl_bivector_wrap_vectors(wave, sed) ;
00863 
00864     /* Create the output bivector */
00865     out = cpl_bivector_duplicate(tmp) ;
00866 
00867     /* Free */
00868     cpl_bivector_unwrap_vectors(tmp) ;
00869     cpl_vector_unwrap(wave) ;
00870     cpl_vector_unwrap(sed) ;
00871     cpl_table_delete(seds) ;
00872 
00873     /* Return  */
00874     return out ;
00875 }
00876 
00877 /*----------------------------------------------------------------------------*/
00887 /*----------------------------------------------------------------------------*/
00888 double irplib_stdstar_great_circle_dist(double ra1,
00889                                         double dec1,
00890                                         double ra2,
00891                                         double dec2)
00892 {
00893 
00894     /* Convert all input from degrees to radian - and back for the result */
00895     const double dra  = sin( CPL_MATH_RAD_DEG * (ra2  - ra1 )/2.0 );
00896     const double ddec = sin( CPL_MATH_RAD_DEG * (dec2 - dec1)/2.0 );
00897 
00898     dec1 *= CPL_MATH_RAD_DEG;
00899     dec2 *= CPL_MATH_RAD_DEG;
00900 
00901     return 2.0 * asin(sqrt( ddec*ddec + cos(dec1)*cos(dec2)*dra*dra))
00902         * CPL_MATH_DEG_RAD;
00903 }

Generated on Fri Jul 3 11:23:58 2009 for NACO Pipeline Reference Manual by  doxygen 1.5.8