irplib_stdstar.c

00001 /* $Id: irplib_stdstar.c,v 1.45 2013-03-01 10:27:07 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: 2013-03-01 10:27:07 $
00024  * $Revision: 1.45 $
00025  * $Name: not supported by cvs2svn $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "irplib_stdstar.h"
00037 #include "irplib_utils.h"
00038 #include "irplib_wcs.h"
00039 #include <cpl.h>
00040 
00041 #include <string.h>
00042 #include <math.h>
00043 #include <float.h>
00044 
00045 /*-----------------------------------------------------------------------------
00046                                    Defines
00047  -----------------------------------------------------------------------------*/
00048 
00049 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(6, 3, 0)
00050 #define IRPLIB_FRAMESET_GET_CONST cpl_frameset_get_position_const
00051 #else
00052 /* Remove this branch once CPL versions less than 6.3 are no longer supported */
00053 #define IRPLIB_FRAMESET_GET_CONST cpl_frameset_get_frame_const
00054 #endif
00055 
00056 
00057 /*----------------------------------------------------------------------------*/
00061 /*----------------------------------------------------------------------------*/
00064 /*-----------------------------------------------------------------------------
00065                                    Functions code
00066  -----------------------------------------------------------------------------*/
00067 
00068 /*----------------------------------------------------------------------------*/
00087 /*----------------------------------------------------------------------------*/
00088 cpl_error_code
00089 irplib_stdstar_write_catalogs(cpl_frameset        *   set_in,
00090                               const cpl_frameset  *   set_raw,
00091                               const char          *   recipe_name,
00092                               const char          *   pro_cat,
00093                               const char          *   pro_type,
00094                               const char          *   package_name,
00095                               const char          *   ins_name,
00096                               cpl_table * (*convert_ascii_table)(const char *))
00097 {
00098     /* Number of catalogs */
00099     const cpl_size     nb_catalogs = cpl_frameset_get_size(set_raw);
00100     cpl_propertylist * plist_ext;
00101     char             * out_name;
00102     cpl_error_code     error = CPL_ERROR_NONE;
00103     cpl_size           i;
00104 
00105     /* Check entries */
00106     if (set_in == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00107     if (set_raw == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00108     if (recipe_name == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00109     if (pro_cat == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00110     if (ins_name == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00111     if (convert_ascii_table == NULL) return
00112         cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00113 
00114     /* Define the file name */
00115     out_name = cpl_sprintf("%s" CPL_DFS_FITS, recipe_name);
00116     
00117     plist_ext = cpl_propertylist_new();
00118     
00119     /* Process the catalogs */
00120     for (i = 0; i < nb_catalogs; i++) {
00121         /* Get the catalog name */
00122         const cpl_frame * cur_frame = IRPLIB_FRAMESET_GET_CONST(set_raw, i);
00123         const char      * cat_name = cpl_frame_get_filename(cur_frame);
00124 
00125         cpl_table       * out = convert_ascii_table(cat_name);
00126      
00127         /* Create the output table */
00128         if (out == NULL) {
00129             error = cpl_error_get_code() ? cpl_error_set_where(cpl_func)
00130                 : cpl_error_set(cpl_func, CPL_ERROR_UNSPECIFIED);
00131             break;
00132         }
00133 
00134         if (cpl_table_get_nrow(out) == 0) {
00135             cpl_table_delete(out);
00136             error = cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00137                                           "Empty catalogue %d in '%s'",
00138                                           (int)i+1, cat_name);
00139             break;
00140         }
00141 
00142         cpl_propertylist_update_string(plist_ext, "EXTNAME", cat_name);
00143 
00144         /* Write the table */
00145         if (i == 0) {
00146             cpl_parameterlist * parlist = cpl_parameterlist_new();
00147             cpl_propertylist  * plist   = cpl_propertylist_new();
00148 
00149             /* Mandatory keywords */
00150             cpl_propertylist_append_string(plist, "INSTRUME", ins_name);
00151             cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, pro_cat);
00152             if (pro_type != NULL) {
00153                 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
00154                                                pro_type);
00155             }
00156 
00157             error = cpl_dfs_save_table(set_in, NULL, parlist, set_raw, NULL,
00158                                        out, plist_ext, recipe_name, plist,
00159                                        NULL, package_name, out_name);
00160             cpl_parameterlist_delete(parlist);
00161             cpl_propertylist_delete(plist);
00162         } else {
00163             error = cpl_table_save(out, NULL, plist_ext, out_name,
00164                                    CPL_IO_EXTEND);
00165         }
00166 
00167         cpl_table_delete(out);
00168 
00169         if (error) {
00170             (void)cpl_error_set_where(cpl_func);
00171             break;
00172         }
00173     }
00174 
00175     cpl_propertylist_delete(plist_ext);
00176     cpl_free(out_name);
00177 
00178     return error;
00179 }
00180 
00181 /*----------------------------------------------------------------------------*/
00194 /*----------------------------------------------------------------------------*/
00195 cpl_table * irplib_stdstar_load_catalog(
00196         const char  *   filename, 
00197         const char  *   ext_name)
00198 {
00199     int                     next;
00200     cpl_propertylist    *   plist;
00201     const char          *   cur_name;
00202     cpl_table           *   out;
00203     cpl_table           *   out_cur;
00204     cpl_frame           *   cur_frame;
00205     int                     i;
00206 
00207     /* Check entries */
00208     if (filename == NULL) return NULL;
00209     if (ext_name == NULL) return NULL;
00210     
00211     /* Initialise */
00212     out = NULL;
00213     
00214     /* Get the number of extensions in the catalog */
00215     cur_frame = cpl_frame_new();
00216     cpl_frame_set_filename(cur_frame, filename);
00217     next = cpl_frame_get_nextensions(cur_frame);
00218     cpl_frame_delete(cur_frame);
00219 
00220     /* Loop on the extentions */
00221     for (i=0; i<next; i++) {
00222         /* Check the name of the current extension */
00223         if ((plist = cpl_propertylist_load_regexp(filename, i+1, "EXTNAME", 
00224                         0)) == NULL) {
00225             cpl_msg_error(cpl_func, "Cannot load header of %d th extension",
00226                     i+1);
00227             return NULL;
00228         }
00229         cur_name = cpl_propertylist_get_string(plist, "EXTNAME");
00230         
00231         /* Check the current extension */
00232         if (!strcmp(cur_name, ext_name)) {
00233             /* Load the table */
00234             if (out == NULL) {
00235                 out = cpl_table_load(filename, i+1, 1);
00236                 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
00237                 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
00238                                                     0, cpl_table_get_nrow(out),
00239                                                     cur_name);
00240                 if (out == NULL) {
00241                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1);
00242                     cpl_propertylist_delete(plist);
00243                     return NULL;
00244                 }
00245             }
00246         } else if (!strcmp(ext_name, "all")) {
00247             /* Load the table and append it */
00248             if (i==0) {
00249                 /* Load the first table */
00250                 out = cpl_table_load(filename, i+1, 1);
00251                 cpl_table_new_column(out, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
00252                 cpl_table_fill_column_window_string(out, IRPLIB_STDSTAR_CAT_COL,
00253                                                     0, cpl_table_get_nrow(out),
00254                                                     cur_name);
00255                 if (out == NULL) {
00256                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1);
00257                     cpl_propertylist_delete(plist); 
00258                     return NULL;
00259                 }
00260             } else {
00261                 /* Load the current table */
00262                 out_cur = cpl_table_load(filename, i+1, 1);
00263                 if (out_cur == NULL) {
00264                     cpl_msg_error(cpl_func, "Cannot load extension %d", i+1);
00265                     cpl_table_delete(out);
00266                     cpl_propertylist_delete(plist); 
00267                     return NULL;
00268                 }
00269                 cpl_table_new_column(out_cur, IRPLIB_STDSTAR_CAT_COL, CPL_TYPE_STRING);
00270                 cpl_table_fill_column_window_string(out_cur, IRPLIB_STDSTAR_CAT_COL,
00271                                                     0, cpl_table_get_nrow(out_cur),
00272                                                     cur_name);
00273                 /* Append the table */
00274                 if (cpl_table_insert(out, out_cur, 
00275                             cpl_table_get_nrow(out)) != CPL_ERROR_NONE) {
00276                     cpl_msg_error(cpl_func, "Cannot merge table %d", i+1);
00277                     cpl_table_delete(out);
00278                     cpl_table_delete(out_cur);
00279                     cpl_propertylist_delete(plist); 
00280                     return NULL;
00281                 }
00282                 cpl_table_delete(out_cur);
00283             }
00284         }
00285         cpl_propertylist_delete(plist);
00286     }
00287     return out;
00288 }
00289 
00290 /*----------------------------------------------------------------------------*/
00299 /*----------------------------------------------------------------------------*/
00300 cpl_error_code irplib_stdstar_check_columns_exist(
00301         const cpl_table  *   catal)
00302 {
00303     /* Check for all the mandatory columns */
00304     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_STAR_COL)) {
00305         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00306                 "Missing column: %s",
00307                 IRPLIB_STDSTAR_STAR_COL);
00308     }
00309     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_TYPE_COL)) {
00310         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00311                 "Missing column: %s",
00312                 IRPLIB_STDSTAR_TYPE_COL);
00313     }
00314     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_CAT_COL)) {
00315         return cpl_error_set_message(cpl_func, 
00316                 CPL_ERROR_ILLEGAL_INPUT,
00317                 "Missing column: %s",
00318                 IRPLIB_STDSTAR_CAT_COL);
00319     }
00320     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_RA_COL)) {
00321         return cpl_error_set_message(cpl_func, 
00322                 CPL_ERROR_ILLEGAL_INPUT,
00323                 "Missing column: %s",
00324                 IRPLIB_STDSTAR_RA_COL);
00325     }
00326     if (!cpl_table_has_column(catal, IRPLIB_STDSTAR_DEC_COL)) {
00327         return cpl_error_set_message(cpl_func, 
00328                 CPL_ERROR_ILLEGAL_INPUT,
00329                 "Missing column: %s",
00330                 IRPLIB_STDSTAR_DEC_COL);
00331     }
00332     return CPL_ERROR_NONE;
00333 }
00334 
00335 /*----------------------------------------------------------------------------*/
00347 /*----------------------------------------------------------------------------*/
00348 int irplib_stdstar_select_stars_dist(
00349         cpl_table   *   cat, 
00350         double          ra, 
00351         double          dec, 
00352         double          dist)
00353 {
00354     double              distance;
00355     int                 nrows;
00356     int                 i;
00357     
00358     /* Check entries */
00359     if (cat == NULL) return -1;
00360 
00361     /* Get the number of selected rows */
00362     nrows = cpl_table_get_nrow(cat);
00363     
00364     /* Check if the columns are there */
00365     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00366         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL);
00367         return -1;
00368     }
00369     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00370         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL);
00371         return -1;
00372     }
00373     
00374     /* Compute distances of the selected rows */
00375     for (i=0; i<nrows; i++) {
00376         if (cpl_table_is_selected(cat, i)) {
00377             /* The row is selected - compute the distance */
00378             distance = irplib_wcs_great_circle_dist(ra, dec, 
00379                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00380                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));            
00381             if (distance > dist) cpl_table_unselect_row(cat, i);
00382         }
00383     }
00384     return 0;
00385 }
00386 
00387 /*----------------------------------------------------------------------------*/
00396 /*----------------------------------------------------------------------------*/
00397 int irplib_stdstar_select_stars_mag(
00398         cpl_table   *   cat, 
00399         const char  *   mag_colname)
00400 {
00401     /* Check entries */
00402     if (cat == NULL) return -1;
00403     if (mag_colname == NULL) return -1;
00404 
00405     /* Check that the table has the mag column */
00406     if (!cpl_table_has_column(cat, mag_colname)) {
00407         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00408                 mag_colname);
00409         return -1;
00410     }
00411 
00412     /* Apply the selection */
00413     if (cpl_table_and_selected_double(cat, mag_colname, CPL_NOT_GREATER_THAN, 
00414                 98.0) <= 0) {
00415         cpl_msg_error(cpl_func, "Column %s does not exist in the catalog",
00416                 mag_colname);
00417         return -1;
00418     }
00419     return 0;
00420 }
00421 
00422 /*----------------------------------------------------------------------------*/
00432 /*----------------------------------------------------------------------------*/
00433 int irplib_stdstar_find_closest(
00434         const cpl_table     *   cat, 
00435         double                  ra, 
00436         double                  dec)
00437 {
00438     double              min_dist, distance;
00439     int                 nrows;
00440     int                 ind;
00441     int                 i;
00442 
00443     /* Check entries */
00444     if (cat == NULL) return -1;
00445 
00446     /* Initialize */
00447     min_dist = 1000.0;
00448     ind = -1;
00449 
00450     /* Get the number of selected rows */
00451     nrows = cpl_table_get_nrow(cat);
00452 
00453     /* Check if the columns are there */
00454     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_RA_COL)) {
00455         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_RA_COL);
00456         return -1;
00457     }
00458     if (!cpl_table_has_column(cat, IRPLIB_STDSTAR_DEC_COL)) {
00459         cpl_msg_error(cpl_func, "Missing %s column", IRPLIB_STDSTAR_DEC_COL);
00460         return -1;
00461     }
00462     
00463     /* Compute distances of the selected rows */
00464     for (i=0; i<nrows; i++) {
00465         if (cpl_table_is_selected(cat, i)) {
00466             /* The row is selected - compute the distance */
00467             distance = irplib_wcs_great_circle_dist(ra, dec,
00468                     cpl_table_get_double(cat, IRPLIB_STDSTAR_RA_COL, i, NULL),
00469                     cpl_table_get_double(cat, IRPLIB_STDSTAR_DEC_COL, i, NULL));
00470             if (distance <= min_dist) {
00471                 min_dist = distance;
00472                 ind = i;
00473             }
00474         }
00475     }
00476     return ind;
00477 }
00478 
00479 /*----------------------------------------------------------------------------*/
00500 /*----------------------------------------------------------------------------*/
00501 cpl_error_code irplib_stdstar_find_star(
00502         const char          *   catfile,
00503         double                  ra, 
00504         double                  dec,
00505         const char          *   band,
00506         const char          *   catname,
00507         double              *   mag,
00508         char                **  name,
00509         char                **  type,
00510         char                **  usedcatname,
00511         double              *   star_ra,
00512         double              *   star_dec,
00513         double                  dist_am)
00514 {
00515     cpl_errorstate prestate = cpl_errorstate_get();
00516     cpl_table   *   catal;
00517     const double    dist = dist_am / 60.0;
00518     int             ind;
00519 
00520     /* Check entries */
00521     if (catfile == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00522     if (band    == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00523     if (catname == NULL) return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00524     
00525     /* Load the catalog */
00526     if ((catal = irplib_stdstar_load_catalog(catfile, catname)) == NULL) {
00527         return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
00528                                            "Cannot load the catalog %s from %s",
00529                                            catname, catfile);
00530     }
00531     
00532     /* Check the columns are present */
00533     if (irplib_stdstar_check_columns_exist(catal) != CPL_ERROR_NONE) {
00534         cpl_table_delete(catal);
00535         return cpl_error_set_where(cpl_func);
00536     }
00537     
00538     /* Select stars with known magnitude */
00539     if (irplib_stdstar_select_stars_mag(catal, band) == -1) {
00540         cpl_table_delete(catal);
00541         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00542                                            "Cannot select stars in that band");
00543     }
00544 
00545     /* Select stars within a given distance */
00546     if (irplib_stdstar_select_stars_dist(catal, ra, dec, dist) == -1) {
00547         cpl_table_delete(catal);
00548         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00549                                            "Cannot select close stars");
00550     }
00551 
00552     /* Take the closest */
00553     if ((ind=irplib_stdstar_find_closest(catal, ra, dec)) < 0) {
00554         cpl_table_delete(catal);
00555         return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00556                                            "Cannot get the closest star with "
00557                                            "known %s magnitude",band);
00558     }
00559 
00560     if(mag != NULL)
00561         *mag = cpl_table_get_double(catal, band, ind, NULL);
00562 
00563     if(name != NULL)
00564     {
00565         *name = cpl_strdup(cpl_table_get_string(catal,
00566                                                 IRPLIB_STDSTAR_STAR_COL, ind));
00567 
00568     }
00569     if(type != NULL)
00570     {
00571         *type = cpl_strdup(cpl_table_get_string(catal, IRPLIB_STDSTAR_TYPE_COL,
00572                                                 ind));
00573     }
00574     if(usedcatname != NULL)
00575     {
00576         if(strcmp(catname, "all"))
00577             *usedcatname = cpl_strdup(catname);
00578         else
00579         {
00580             *usedcatname = cpl_strdup(cpl_table_get_string
00581                                       (catal, IRPLIB_STDSTAR_CAT_COL, ind));
00582         }
00583     }
00584     if(star_ra != NULL)
00585         *star_ra  = cpl_table_get_double(catal, IRPLIB_STDSTAR_RA_COL, ind, NULL);
00586     if(star_dec != NULL)
00587         *star_dec = cpl_table_get_double(catal, IRPLIB_STDSTAR_DEC_COL, ind, NULL);
00588     
00589     /* Free and return */
00590     cpl_table_delete(catal);
00591     return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
00592         : cpl_error_set_where(cpl_func);
00593 }
00594 
00595 /*----------------------------------------------------------------------------*/
00608 /*----------------------------------------------------------------------------*/
00609 cpl_vector * irplib_stdstar_get_conversion(
00610         const cpl_bivector  *   spec,
00611         double                  dit,
00612         double                  surface,
00613         double                  gain,
00614         double                  mag)
00615 {
00616     double                      h = 6.62e-27;
00617     double                      c = 3e18;
00618     const cpl_vector    *       wave;
00619     const cpl_vector    *       extr;
00620     cpl_vector          *       out;
00621     double                      factor;
00622 
00623     /* Test entries */
00624     if (spec == NULL) return NULL;
00625     if (dit <= 0.0) return NULL;
00626 
00627     /* Get the extracted spectrum */
00628     wave = cpl_bivector_get_x_const(spec);
00629     extr = cpl_bivector_get_y_const(spec);
00630 
00631     /* Get the spectrum */
00632     out = cpl_vector_duplicate(extr);
00633 
00634     /* Divide by DIT */
00635     cpl_vector_divide_scalar(out, dit);
00636 
00637     /* Divide by the surface */
00638     cpl_vector_divide_scalar(out, surface);
00639 
00640     /* Multiply by the gain */
00641     cpl_vector_multiply_scalar(out, gain);
00642 
00643     /* Multiply by the difference magnitude */
00644     factor = pow(10, mag/2.5);
00645     cpl_vector_multiply_scalar(out, factor);
00646 
00647     /* Divide by the dispersion */
00648     factor = (cpl_vector_get(wave, cpl_vector_get_size(wave)-1) -
00649             cpl_vector_get(wave, 0)) / cpl_vector_get_size(wave);
00650     cpl_vector_divide_scalar(out, factor);
00651 
00652     /* Multiply by the energy of the photon */
00653     cpl_vector_multiply_scalar(out, h*c);
00654     cpl_vector_divide(out, wave);
00655 
00656     return out;
00657 }
00658 
00659 /*----------------------------------------------------------------------------*/
00667 /*----------------------------------------------------------------------------*/
00668 cpl_vector * irplib_stdstar_get_mag_zero(
00669         const cpl_bivector  *   sed,
00670         const cpl_vector    *   waves,
00671         double                  cent_wl)
00672 {
00673     double              wmin, wmax, wstep;
00674     int                 nb_sed;
00675     const double    *   sed_x;
00676     const double    *   sed_y;
00677     cpl_bivector    *   sed_loc;
00678     double          *   sed_loc_x;
00679     double          *   sed_loc_y;
00680     cpl_vector      *   out;
00681     cpl_bivector    *   out_biv;
00682     double              f0_jan, f0_erg, cent_val;
00683     int                 i;
00684 
00685     /* Test entries */
00686     if (sed == NULL) return NULL;
00687     if (waves == NULL) return NULL;
00688 
00689     /* Initialise */
00690     nb_sed = cpl_bivector_get_size(sed);
00691     sed_x = cpl_bivector_get_x_data_const(sed);
00692     sed_y = cpl_bivector_get_y_data_const(sed);
00693     wstep = sed_x[1] - sed_x[0];
00694     wmin = cpl_vector_get(waves, 0);
00695     wmax = cpl_vector_get(waves, cpl_vector_get_size(waves)-1);
00696 
00697     /* Expand sed with 0 to have it bigger than the required wavelengths */
00698     sed_loc = cpl_bivector_new(nb_sed + 4);
00699     sed_loc_x = cpl_bivector_get_x_data(sed_loc);
00700     sed_loc_y = cpl_bivector_get_y_data(sed_loc);
00701     for (i=0; i<nb_sed; i++) {
00702         sed_loc_x[i+2] = sed_x[i];
00703         sed_loc_y[i+2] = sed_y[i];
00704     }
00705 
00706     /* Low bound */
00707     sed_loc_x[1] = sed_loc_x[2] - wstep;
00708     if (sed_loc_x[2] < wmin) {
00709         sed_loc_x[0] = sed_loc_x[1] - wstep;
00710     } else {
00711         sed_loc_x[0] = wmin - wstep;
00712     }
00713     sed_loc_y[0] = 1e-20;
00714     sed_loc_y[1] = 1e-20;
00715 
00716     /* High bound */
00717     sed_loc_x[nb_sed+2] = sed_loc_x[nb_sed+1] + wstep;
00718     if (sed_loc_x[nb_sed+1] > wmax) {
00719         sed_loc_x[nb_sed+3] = sed_loc_x[nb_sed+2] + wstep;
00720     } else {
00721         sed_loc_x[nb_sed+3] = wmax + wstep;
00722     }
00723     sed_loc_y[nb_sed+2] = 1e-20;
00724     sed_loc_y[nb_sed+3] = 1e-20;
00725 
00726     /* Create the output bivector */
00727     out = cpl_vector_duplicate(waves);
00728     IRPLIB_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
00729     /* the X entry (waves) is not modified by cpl_bivector_interpolate_linear */
00730     out_biv = cpl_bivector_wrap_vectors((cpl_vector*)waves, out);
00731     IRPLIB_DIAG_PRAGMA_POP;
00732     /* Interpolate */
00733     if (cpl_bivector_interpolate_linear(out_biv, sed_loc) != CPL_ERROR_NONE) {
00734         cpl_msg_error(cpl_func, "Cannot interpolate the wavelength");
00735         cpl_bivector_unwrap_vectors(out_biv);
00736         cpl_vector_delete(out);
00737         cpl_bivector_delete(sed_loc);
00738         return NULL;
00739     }
00740     cpl_bivector_unwrap_vectors(out_biv);
00741     cpl_bivector_delete(sed_loc);
00742 
00743     /* Compute f0_jan */
00744     f0_jan = 5513.15 / ( pow(cent_wl,3) * (exp(1.2848/cent_wl)-1) );
00745 
00746     /* Convert f0 Jansky -> ergs/s/cm^2/Angstrom */
00747     f0_erg = f0_jan * 1e-26 * 1e7 * 3e18 / (1e4 * cent_wl*cent_wl*1e4*1e4);
00748 
00749     /* Scale out so that the central value is f0 */
00750     cent_val = cpl_vector_get(out, cpl_vector_get_size(out)/2);
00751     if (cent_val <= 0.0) {
00752         cpl_msg_error(cpl_func, "Negative or 0 central value");
00753         cpl_vector_delete(out);
00754         return NULL;
00755     }
00756     cpl_vector_multiply_scalar(out, f0_erg/cent_val);
00757 
00758     /* Return */
00759     return out;
00760 }
00761 
00762 /*----------------------------------------------------------------------------*/
00772 /*----------------------------------------------------------------------------*/
00773 cpl_bivector * irplib_stdstar_get_sed(
00774         const char  *   seds_file,
00775         const char  *   sptype)
00776 {
00777     cpl_table           *   seds;
00778     cpl_bivector        *   out;
00779     cpl_vector          *   wave;
00780     cpl_vector          *   sed;
00781     cpl_bivector        *   tmp;
00782     int                     nlines;
00783 
00784     /* Test entries */
00785     if (seds_file == NULL) return NULL;
00786     if (sptype == NULL) return NULL;
00787 
00788     /* Load the table */
00789     if ((seds = cpl_table_load(seds_file, 1, 0)) == NULL) {
00790         cpl_msg_error(cpl_func, "Cannot load the table");
00791         return NULL;
00792     }
00793 
00794     /* Check if the column is there */
00795     if (!cpl_table_has_column(seds, sptype)) {
00796         cpl_msg_error(cpl_func, "SED of the requested star not available");
00797         cpl_table_delete(seds);
00798         return NULL;
00799     }
00800 
00801     /* Get the nb lines */
00802     nlines = cpl_table_get_nrow(seds);
00803 
00804     /* Get the wavelength as a vector */
00805     if ((wave = cpl_vector_wrap(nlines,
00806             cpl_table_get_data_double(seds, "Wavelength"))) == NULL) {
00807         cpl_msg_error(cpl_func, "Cannot get the Wavelength column");
00808         cpl_table_delete(seds);
00809         return NULL;
00810     }
00811 
00812     /* Get the SED as a vector */
00813     if ((sed = cpl_vector_wrap(nlines,
00814             cpl_table_get_data_double(seds, sptype))) == NULL) {
00815         cpl_msg_error(cpl_func, "Cannot get the SED column");
00816         cpl_table_delete(seds);
00817         cpl_vector_unwrap(wave);
00818         return NULL;
00819     }
00820     tmp = cpl_bivector_wrap_vectors(wave, sed);
00821 
00822     /* Create the output bivector */
00823     out = cpl_bivector_duplicate(tmp);
00824 
00825     /* Free */
00826     cpl_bivector_unwrap_vectors(tmp);
00827     cpl_vector_unwrap(wave);
00828     cpl_vector_unwrap(sed);
00829     cpl_table_delete(seds);
00830 
00831     /* Return  */
00832     return out;
00833 }
Generated on Mon Feb 17 15:01:44 2014 for NACO Pipeline Reference Manual by  doxygen 1.6.3