irplib_stdstar.c

00001 /* $Id: irplib_stdstar.c,v 1.39 2012/03/02 09:01:03 amodigli 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: amodigli $
00023  * $Date: 2012/03/02 09:01:03 $
00024  * $Revision: 1.39 $
00025  * $Name: detmon-1_2_0 $
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),
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_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00297                 __FILE__, __LINE__,
00298                 "Missing column: %s",
00299                 IRPLIB_STDSTAR_STAR_COL);
00300     }
00301     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
00302         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00303                 __FILE__, __LINE__,
00304                 "Missing column: %s",
00305                 IRPLIB_STDSTAR_TYPE_COL);
00306     }
00307     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_CAT_COL)) {
00308         return cpl_error_set_message_macro(cpl_func, 
00309                 CPL_ERROR_ILLEGAL_INPUT,
00310                 __FILE__, __LINE__,
00311                 "Missing column: %s",
00312                 IRPLIB_STDSTAR_CAT_COL);
00313     }
00314     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_RA_COL)) {
00315         return cpl_error_set_message_macro(cpl_func, 
00316                 CPL_ERROR_ILLEGAL_INPUT,
00317                 __FILE__, __LINE__,
00318                 "Missing column: %s",
00319                 IRPLIB_STDSTAR_RA_COL);
00320     }
00321     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_DEC_COL)) {
00322         return cpl_error_set_message_macro(cpl_func, 
00323                 CPL_ERROR_ILLEGAL_INPUT,
00324                 __FILE__, __LINE__,
00325                 "Missing column: %s",
00326                 IRPLIB_STDSTAR_DEC_COL);
00327     }
00328     return cpl_error_get_code();
00329 }
00330 
00331 /*----------------------------------------------------------------------------*/
00343 /*----------------------------------------------------------------------------*/
00344 int irplib_stdstar_select_stars_dist(
00345         cpl_table   *   cat, 
00346         double          ra, 
00347         double          dec, 
00348         double          dist)
00349 {
00350     double              distance ;
00351     int                 nrows ;
00352     int                 i ;
00353     
00354     /* Check entries */
00355     if (cat == NULL) return -1;
00356 
00357     /* Get the number of selected rows */
00358     nrows = cpl_table_get_nrow(cat) ;
00359     
00360     /* Check if the columns are there */
00361     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00362         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL) ;
00363         return -1 ;
00364     }
00365     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00366         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL) ;
00367         return -1 ;
00368     }
00369     
00370     /* Compute distances of the selected rows */
00371     for (i=0 ; i<nrows ; i++) {
00372         if (cpl_table_is_selected(cat, i)) {
00373             /* The row is selected - compute the distance */
00374             distance = irplib_wcs_great_circle_dist(ra, dec, 
00375                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00376                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));            
00377             if (distance > dist) cpl_table_unselect_row(cat, i) ;
00378         }
00379     }
00380     return 0;
00381 }
00382 
00383 /*----------------------------------------------------------------------------*/
00392 /*----------------------------------------------------------------------------*/
00393 int irplib_stdstar_select_stars_mag(
00394         cpl_table   *   cat, 
00395         const char  *   mag_colname)
00396 {
00397     /* Check entries */
00398     if (cat == NULL) return -1 ;
00399     if (mag_colname == NULL) return -1 ;
00400 
00401     /* Check that the table has the mag column */
00402     if (!cpl_table_has_column(cat, mag_colname)) {
00403         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00404                 mag_colname) ;
00405         return -1 ;
00406     }
00407 
00408     /* Apply the selection */
00409     if (cpl_table_and_selected_double(cat, mag_colname, CPL_NOT_GREATER_THAN, 
00410                 98.0) <= 0) {
00411         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00412                 mag_colname) ;
00413         return -1 ;
00414     }
00415     return 0 ;
00416 }
00417 
00418 /*----------------------------------------------------------------------------*/
00428 /*----------------------------------------------------------------------------*/
00429 int irplib_stdstar_find_closest(
00430         const cpl_table     *   cat, 
00431         double                  ra, 
00432         double                  dec)
00433 {
00434     double              min_dist, distance ;
00435     int                 nrows ;
00436     int                 ind ;
00437     int                 i ;
00438 
00439     /* Check entries */
00440     if (cat == NULL) return -1 ;
00441 
00442     /* Initialize */
00443     min_dist = 1000.0 ;
00444     ind = -1 ;
00445 
00446     /* Get the number of selected rows */
00447     nrows = cpl_table_get_nrow(cat) ;
00448 
00449     /* Check if the columns are there */
00450     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00451         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL) ;
00452         return -1 ;
00453     }
00454     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00455         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL) ;
00456         return -1 ;
00457     }
00458     
00459     /* Compute distances of the selected rows */
00460     for (i=0 ; i<nrows ; i++) {
00461         if (cpl_table_is_selected(cat, i)) {
00462             /* The row is selected - compute the distance */
00463             distance = irplib_wcs_great_circle_dist(ra, dec,
00464                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00465                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
00466             if (distance <= min_dist) {
00467                 min_dist = distance ;
00468                 ind = i ;
00469             }
00470         }
00471     }
00472     return ind ;
00473 }
00474 
00475 /*----------------------------------------------------------------------------*/
00496 /*----------------------------------------------------------------------------*/
00497 cpl_error_code irplib_stdstar_find_star(
00498         const char          *   catfile,
00499         double                  ra, 
00500         double                  dec,
00501         const char          *   band,
00502         const char          *   catname,
00503         double              *   mag,
00504         char                **  name,
00505         char                **  type,
00506         char                **  usedcatname,
00507         double              *   star_ra,
00508         double              *   star_dec,
00509         double                  dist_am)
00510 {
00511     cpl_table   *   catal ;
00512     const double    dist = dist_am / 60.0 ;
00513     int             ind ;
00514 
00515     /* Check entries */
00516     if (catfile == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00517     if (band    == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00518     if (catname == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00519     
00520     /* Load the catalog */
00521     if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
00522         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
00523                                            __FILE__, __LINE__,
00524                                            "Cannot load the catalog %s from %s",
00525                                            catname, catfile);
00526     }
00527     
00528     /* Check the columns are present */
00529     if (irplib_stdstar_check_columns_exist(catal) != CPL_ERROR_NONE) {
00530         cpl_table_delete(catal);
00531         return cpl_error_set_where(cpl_func);
00532     }
00533     
00534     /* Select stars with known magnitude */
00535     if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
00536         cpl_table_delete(catal) ;
00537         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00538                                            __FILE__, __LINE__,
00539                                            "Cannot select stars in that band");
00540     }
00541 
00542     /* Select stars within a given distance */
00543     if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
00544         cpl_table_delete(catal) ;
00545         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00546                                            __FILE__, __LINE__,
00547                                            "Cannot select close stars");
00548     }
00549 
00550     /* Take the closest */
00551     if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
00552         cpl_table_delete(catal) ;
00553         return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00554                                            __FILE__, __LINE__,
00555                                            "Cannot get the closest star with "
00556                                            "known %s magnitude",band);
00557     }
00558 
00559     if(mag != NULL)
00560         *mag = cpl_table_get_double(catal, band, ind, NULL) ;
00561 
00562     if(name != NULL)
00563     {
00564         *name = cpl_strdup(cpl_table_get_string(catal,
00565                                                 IRPLIB_STDSTAR_STAR_COL, ind));
00566 
00567     }
00568     if(type != NULL)
00569     {
00570         *type = cpl_strdup(cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL,
00571                                                 ind));
00572     }
00573     if(usedcatname != NULL)
00574     {
00575         if(strcmp(catname, "all"))
00576             *usedcatname = cpl_strdup(catname);
00577         else
00578         {
00579             *usedcatname = cpl_strdup(cpl_table_get_string
00580                                       (catal, IRPLIB_STDSTAR_CAT_COL, ind));
00581         }
00582     }
00583     if(star_ra != NULL)
00584         *star_ra  = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
00585     if(star_dec != NULL)
00586         *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
00587     
00588     /* Free and return */
00589     cpl_table_delete(catal);
00590     return cpl_error_get_code();
00591 }
00592 
00593 /*----------------------------------------------------------------------------*/
00606 /*----------------------------------------------------------------------------*/
00607 cpl_vector * irplib_stdstar_get_conversion(
00608         const cpl_bivector  *   spec,
00609         double                  dit,
00610         double                  surface,
00611         double                  gain,
00612         double                  mag)
00613 {
00614     double                      h = 6.62e-27 ;
00615     double                      c = 3e18 ;
00616     const cpl_vector    *       wave ;
00617     const cpl_vector    *       extr ;
00618     cpl_vector          *       out ;
00619     double                      factor ;
00620 
00621     /* Test entries */
00622     if (spec == NULL) return NULL ;
00623     if (dit <= 0.0) return NULL ;
00624 
00625     /* Get the extracted spectrum */
00626     wave = cpl_bivector_get_x_const(spec) ;
00627     extr = cpl_bivector_get_y_const(spec) ;
00628 
00629     /* Get the spectrum */
00630     out = cpl_vector_duplicate(extr) ;
00631 
00632     /* Divide by DIT */
00633     cpl_vector_divide_scalar(out, dit) ;
00634 
00635     /* Divide by the surface */
00636     cpl_vector_divide_scalar(out, surface) ;
00637 
00638     /* Multiply by the gain */
00639     cpl_vector_multiply_scalar(out, gain) ;
00640 
00641     /* Multiply by the difference magnitude */
00642     factor = pow(10, mag/2.5) ;
00643     cpl_vector_multiply_scalar(out, factor) ;
00644 
00645     /* Divide by the dispersion */
00646     factor = (cpl_vector_get(wave, cpl_vector_get_size(wave)-1) -
00647             cpl_vector_get(wave, 0)) / cpl_vector_get_size(wave) ;
00648     cpl_vector_divide_scalar(out, factor) ;
00649 
00650     /* Multiply by the energy of the photon */
00651     cpl_vector_multiply_scalar(out, h*c) ;
00652     cpl_vector_divide(out, wave) ;
00653 
00654     return out ;
00655 }
00656 
00657 /*----------------------------------------------------------------------------*/
00665 /*----------------------------------------------------------------------------*/
00666 cpl_vector * irplib_stdstar_get_mag_zero(
00667         const cpl_bivector  *   sed,
00668         const cpl_vector    *   waves,
00669         double                  cent_wl)
00670 {
00671     double              wmin, wmax, wstep ;
00672     int                 nb_sed ;
00673     const double    *   sed_x ;
00674     const double    *   sed_y ;
00675     cpl_bivector    *   sed_loc ;
00676     double          *   sed_loc_x ;
00677     double          *   sed_loc_y ;
00678     cpl_vector      *   out ;
00679     cpl_bivector    *   out_biv ;
00680     double              f0_jan, f0_erg, cent_val ;
00681     int                 i ;
00682 
00683     /* Test entries */
00684     if (sed == NULL) return NULL ;
00685     if (waves == NULL) return NULL ;
00686 
00687     /* Initialise */
00688     nb_sed = cpl_bivector_get_size(sed) ;
00689     sed_x = cpl_bivector_get_x_data_const(sed) ;
00690     sed_y = cpl_bivector_get_y_data_const(sed) ;
00691     wstep = sed_x[1] - sed_x[0] ;
00692     wmin = cpl_vector_get(waves, 0) ;
00693     wmax = cpl_vector_get(waves, cpl_vector_get_size(waves)-1) ;
00694 
00695     /* Expand sed with 0 to have it bigger than the required wavelengths */
00696     sed_loc = cpl_bivector_new(nb_sed + 4) ;
00697     sed_loc_x = cpl_bivector_get_x_data(sed_loc) ;
00698     sed_loc_y = cpl_bivector_get_y_data(sed_loc) ;
00699     for (i=0 ; i<nb_sed ; i++) {
00700         sed_loc_x[i+2] = sed_x[i] ;
00701         sed_loc_y[i+2] = sed_y[i] ;
00702     }
00703 
00704     /* Low bound */
00705     sed_loc_x[1] = sed_loc_x[2] - wstep ;
00706     if (sed_loc_x[2] < wmin) {
00707         sed_loc_x[0] = sed_loc_x[1] - wstep ;
00708     } else {
00709         sed_loc_x[0] = wmin - wstep ;
00710     }
00711     sed_loc_y[0] = 1e-20 ;
00712     sed_loc_y[1] = 1e-20 ;
00713 
00714     /* High bound */
00715     sed_loc_x[nb_sed+2] = sed_loc_x[nb_sed+1] + wstep ;
00716     if (sed_loc_x[nb_sed+1] > wmax) {
00717         sed_loc_x[nb_sed+3] = sed_loc_x[nb_sed+2] + wstep ;
00718     } else {
00719         sed_loc_x[nb_sed+3] = wmax + wstep ;
00720     }
00721     sed_loc_y[nb_sed+2] = 1e-20 ;
00722     sed_loc_y[nb_sed+3] = 1e-20 ;
00723 
00724     /* Create the output bivector */
00725     out = cpl_vector_duplicate(waves) ;
00726     out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out) ;
00727 
00728     /* Interpolate */
00729     if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
00730         cpl_msg_error(cpl_func, "Cannot interpolate the wavelength") ;
00731         cpl_bivector_unwrap_vectors(out_biv) ;
00732         cpl_vector_delete(out) ;
00733         cpl_bivector_delete(sed_loc) ;
00734         return NULL ;
00735     }
00736     cpl_bivector_unwrap_vectors(out_biv) ;
00737     cpl_bivector_delete(sed_loc) ;
00738 
00739     /* Compute f0_jan */
00740     f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) ) ;
00741 
00742     /* Convert f0 Jansky -> ergs/s/cm^2/Angstrom */
00743     f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4) ;
00744 
00745     /* Scale out so that the central value is f0 */
00746     cent_val = cpl_vector_get(out, cpl_vector_get_size(out)/2) ;
00747     if (cent_val <= 0.0) {
00748         cpl_msg_error(cpl_func, "Negative or 0 central value") ;
00749         cpl_vector_delete(out) ;
00750         return NULL ;
00751     }
00752     cpl_vector_multiply_scalar(out, f0_erg/cent_val) ;
00753 
00754     /* Return */
00755     return out ;
00756 }
00757 
00758 /*----------------------------------------------------------------------------*/
00768 /*----------------------------------------------------------------------------*/
00769 cpl_bivector * irplib_stdstar_get_sed(
00770         const char  *   seds_file,
00771         const char  *   sptype)
00772 {
00773     cpl_table           *   seds ;
00774     cpl_bivector        *   out ;
00775     cpl_vector          *   wave ;
00776     cpl_vector          *   sed ;
00777     cpl_bivector        *   tmp ;
00778     int                     nlines ;
00779 
00780     /* Test entries */
00781     if (seds_file == NULL) return NULL ;
00782     if (sptype == NULL) return NULL ;
00783 
00784     /* Load the table */
00785     if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
00786         cpl_msg_error(cpl_func, "Cannot load the table") ;
00787         return NULL ;
00788     }
00789 
00790     /* Check if the column is there */
00791     if (!cpl_table_has_column(seds, sptype)) {
00792         cpl_msg_error(cpl_func, "SED of the requested star not available") ;
00793         cpl_table_delete(seds) ;
00794         return NULL ;
00795     }
00796 
00797     /* Get the nb lines */
00798     nlines = cpl_table_get_nrow(seds) ;
00799 
00800     /* Get the wavelength as a vector */
00801     if ((wave = cpl_vector_wrap(nlines,
00802             cpl_table_get_data_double(seds, "Wavelength"))) == NULL) {
00803         cpl_msg_error(cpl_func, "Cannot get the Wavelength column") ;
00804         cpl_table_delete(seds) ;
00805         return NULL ;
00806     }
00807 
00808     /* Get the SED as a vector */
00809     if ((sed = cpl_vector_wrap(nlines,
00810             cpl_table_get_data_double(seds, sptype))) == NULL) {
00811         cpl_msg_error(cpl_func, "Cannot get the SED column") ;
00812         cpl_table_delete(seds) ;
00813         cpl_vector_unwrap(wave) ;
00814         return NULL ;
00815     }
00816     tmp = cpl_bivector_wrap_vectors(wave, sed) ;
00817 
00818     /* Create the output bivector */
00819     out = cpl_bivector_duplicate(tmp) ;
00820 
00821     /* Free */
00822     cpl_bivector_unwrap_vectors(tmp) ;
00823     cpl_vector_unwrap(wave) ;
00824     cpl_vector_unwrap(sed) ;
00825     cpl_table_delete(seds) ;
00826 
00827     /* Return  */
00828     return out ;
00829 }

Generated on 7 Mar 2012 for DETMON Pipeline Reference Manual by  doxygen 1.6.1