sinfo_utilities_scired.c

00001 /*
00002  * This file is part of the ESO SINFONI Pipeline
00003  * Copyright (C) 2004,2005 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00018  */
00019 #ifdef HAVE_CONFIG_H
00020 #  include <config.h>
00021 #endif
00022 #include "irplib_stdstar.h"
00023 #include "irplib_plot.h"
00024 
00025 //Used only for sinfo_propertylist_has
00026 #include "sinfo_dfs.h"
00027 
00028 //Used only for sinfo_band
00029 #include "sinfo_tpl_utils.h"
00030 
00031 #include "sinfo_utilities_scired.h"
00032 #include "sinfo_functions.h"
00033 #include "sinfo_pfits.h"
00034 #include "sinfo_spiffi_types.h"
00035 #include "sinfo_utils_wrappers.h"
00036 #include "sinfo_error.h"
00043 static double
00044 sinfo_sess2deg(const double sess);
00045 
00046 static void
00047 sinfo_set_spect_coord1(cpl_propertylist** plist,
00048                const int crpix1,
00049                const double crval1,
00050                const double cdelt1);
00051 
00052 static void
00053 sinfo_set_spect_coord2(cpl_propertylist** plist,
00054                const int crpix2,
00055                const double crval2,
00056                const double cdelt2);
00057 
00058 
00059 static void
00060 sinfo_set_coord1(cpl_propertylist** plist,
00061                  const double crpix1,
00062                  const double crval1,
00063                  const double cdelt1);
00064 static void
00065 sinfo_set_coord2(cpl_propertylist** plist,
00066                  const double crpix2,
00067                  const double crval2,
00068                  const double cdelt2);
00069 static void
00070 sinfo_set_coord3(cpl_propertylist** plist,
00071                  const int crpix3,
00072                  const double crval3,
00073                  const double cdelt3);
00074 
00075 
00076 static void
00077 sinfo_set_cd_matrix2(cpl_propertylist** plist,
00078             const double cd1_1,
00079             const double cd1_2,
00080             const double cd2_1,
00081             const double cd2_2);
00082 
00083 
00084 static void
00085 sinfo_set_cd_matrix3(cpl_propertylist** plist,
00086                      const double cd1_3,
00087                      const double cd2_3,
00088                      const double cd3_1,
00089                      const double cd3_2,
00090                      const double cd3_3);
00091 
00092 
00093 static void
00094 sinfo_new_change_plist_cube (cpl_propertylist * plist,
00095                              float cenLambda,
00096                              float dispersion,
00097                              int   center_z,
00098                              float center_x,
00099                  float center_y );
00100 
00101 
00102 static void
00103 sinfo_new_change_plist_image (cpl_propertylist * plist,
00104                               float center_x,
00105                   float center_y );
00106 
00107 
00108 static void
00109 sinfo_new_change_plist_spectrum (cpl_propertylist * plist,
00110                  double cenLambda,
00111                  double dispersion,
00112                  int   cenpix);
00113 
00114 static cpl_image *
00115 sinfo_new_image_getvig(
00116         cpl_image    *   image_in,
00117         int             loleft_x,
00118         int             loleft_y,
00119         int             upright_x,
00120         int             upright_y);
00121 
00122 
00123 
00131 int
00132 sinfo_check_input_data(object_config* cfg)
00133 {
00134 
00135 
00136     if (cfg == NULL)
00137     {
00138         sinfo_msg_error (" could not parse cpl input!\n") ;
00139         return -1 ;
00140     }
00141 
00142 
00143     if(sinfo_is_fits_file(cfg->wavemap) != 1) {
00144       sinfo_msg_error("Input file wavemap %s is not FITS",cfg->wavemap);
00145       return -1;
00146     }
00147 
00148 
00149     if (cfg->halocorrectInd == 1)
00150     {
00151        if(sinfo_is_fits_file(cfg->halospectrum) != 1) {
00152           sinfo_msg_error("Input file %s is not FITS",cfg->halospectrum);
00153           return -1;
00154        }
00155 
00156     }
00157 
00158     if (cfg->northsouthInd == 0) {
00159       if (sinfo_is_fits_file(cfg->poslist) != 1)
00160     {
00161       sinfo_msg_error("File %s with tag %s is not FITS!",
00162                           cfg->poslist,PRO_SLIT_POS);
00163       return -1 ;
00164     }
00165     } else {
00166 
00167       if (sinfo_is_fits_file(cfg->distlist) != 1)
00168     {
00169       sinfo_msg_error("File %s with tag %s is not FITS!",
00170                            cfg->distlist,PRO_SLITLETS_DISTANCE);
00171       return -1;
00172     }
00173     }
00174 
00175 
00176     return 0;
00177 
00178 
00179 }
00187 static double
00188 sinfo_hms2deg(const double hms)
00189 {
00190   int hrs=0;
00191   int min=0;
00192   double sec=0;
00193   double deg=0;
00194   double rest=hms;
00195   int sign=1;
00196 
00197   //sinfo_msg("hms=%f",hms);
00198 
00199   if(hms<0) {
00200     sign=-1;
00201     rest=-hms;
00202   }
00203   //sinfo_msg("rest=%f",rest);
00204   //sinfo_msg("sign=%d",sign);
00205 
00206   hrs=(int)(rest/10000.);
00207   //sinfo_msg("hrs=%d",hrs);
00208 
00209   rest=rest-(double)(hrs*10000.);
00210   min=(int)(rest/100.);
00211   //sinfo_msg("min=%d",min);
00212 
00213   sec=rest-(double)(min*100.);
00214   //sinfo_msg("sec=%f",sec);
00215 
00216   deg=hrs*15+(double)(min/4.)+(double)(sec/240.);
00217   //sinfo_msg("deg=%f",deg);
00218 
00219   deg=sign*deg;
00220   //sinfo_msg("deg=%f",deg);
00221 
00222   return deg;
00223 
00224 }
00225 
00233 static double
00234 sinfo_sess2deg(const double sess)
00235 {
00236   int grad=0;
00237   int min=0;
00238   double sec=0;
00239   double deg=0;
00240   double rest=sess;
00241   int sign=1;
00242 
00243   //sinfo_msg("sess=%f",sess);
00244 
00245   if(sess<0) {
00246     sign=-1;
00247     rest=-sess;
00248   }
00249   //sinfo_msg("rest=%f",rest);
00250   //sinfo_msg("sign=%d",sign);
00251 
00252   grad=(int)(rest/10000.);
00253   //sinfo_msg("grad=%d",grad);
00254 
00255   rest=rest-(double)(grad*10000.);
00256   min=(int)(rest/100.);
00257   //sinfo_msg("min=%d",min);
00258 
00259   sec=rest-(double)(min*100.);
00260   //sinfo_msg("sec=%f",sec);
00261 
00262   deg=grad+(double)(min/60.)+(double)(sec/3600.);
00263   //sinfo_msg("deg=%f",deg);
00264 
00265   deg=sign*deg;
00266   //sinfo_msg("deg=%f",deg);
00267 
00268   return deg;
00269 
00270 }
00271 
00272 
00285 int
00286 sinfo_auto_size_cube(float* offsetx,
00287                       float* offsety,
00288               const int nframes,
00289                     float* ref_offx,
00290                     float* ref_offy,
00291                     int* size_x,
00292                     int* size_y)
00293 {
00294 
00295   int n=0;
00296   float offx=0;
00297   float offy=0;
00298   float min_offx=0;
00299   float max_offx=0;
00300   float min_offy=0;
00301   float max_offy=0;
00302 
00303   sinfo_msg ("Computation of output cube size") ;
00304   for ( n = 0 ; n < nframes ; n++ ) {
00305     offx = offsetx[n];  /* was - */
00306     offy = offsety[n];  /* was - */
00307     /* sinfo_msg("frame %d offx=%f offy=%f",n,offx,offy); */
00308     if(n==0) {
00309       min_offx=offx;
00310       min_offy=offy;
00311       max_offx=offx;
00312       max_offy=offy;
00313     } else {
00314       if(offx > max_offx) max_offx=offx;
00315       if(offy > max_offy) max_offy=offy;
00316       if(offx < min_offx) min_offx=offx;
00317       if(offy < min_offy) min_offy=offy;
00318     }
00319   }
00320   /*
00321   sinfo_msg("max_offx=%f max_offy=%f",max_offx,max_offy);
00322   sinfo_msg("min_offx=%f min_offy=%f",min_offx,min_offy);
00323   */
00324   *ref_offx=(min_offx+max_offx)/2;
00325   *ref_offy=(min_offy+max_offy)/2;
00326   *size_x+=2*floor(max_offx-min_offx+0.5);
00327   *size_y+=2*floor(max_offy-min_offy+0.5);
00328   sinfo_msg("Output cube size: %d x %d",*size_x,*size_y);
00329   sinfo_msg("Ref offset. x: %f y: %f",*ref_offx,*ref_offy);
00330   sinfo_msg_debug("Max offset. x: %f y: %f",max_offx,max_offy);
00331   sinfo_msg_debug("Min offset. x: %f y: %f",min_offx,min_offy);
00332   return 0;
00333 
00334 }
00335 
00343 int
00344 sinfo_auto_size_cube5(object_config * cfg,
00345                     float* ref_offx, float* ref_offy,
00346                     float* min_offx, float* min_offy,
00347                     float* max_offx, float* max_offy)
00348 {
00349 
00350   char* name =NULL;
00351   int n=0;
00352   float offx=0;
00353   float offy=0;
00354 
00355   cpl_propertylist * plist=NULL;
00356   sinfo_msg ("Automatic computation of output cube size") ;
00357   for ( n = 0 ; n < cfg->nframes ; n++ ) {
00358     name = cfg->framelist[n] ;
00359     plist=cpl_propertylist_load(name,0);
00360     offx = sinfo_pfits_get_cumoffsetx(plist);  /* was - */
00361     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00362       sinfo_msg_warning(" could not read fits header keyword cummoffsetx!");
00363       sinfo_msg_warning(" set it to  0");
00364       offx = 0;
00365       cpl_error_reset();
00366       /* return -1 ; */
00367     }
00368 
00369     offy = sinfo_pfits_get_cumoffsety(plist); /* was - */
00370     sinfo_free_propertylist(&plist);
00371     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00372       sinfo_msg_warning(" could not read fits header keyword! cumoffsety") ;
00373       sinfo_msg_warning(" set it to 0");
00374       offy = 0;
00375       cpl_error_reset();
00376       /* return -1 ; */
00377     }
00378     //sinfo_msg("n=%d offx=%f offy=%f",n,offx,offy);
00379 
00380     if(n==0) {
00381       *min_offx=offx;
00382       *min_offy=offy;
00383       *max_offx=offx;
00384       *max_offy=offy;
00385     } else {
00386       if(offx > *max_offx) *max_offx=offx;
00387       if(offy > *max_offy) *max_offy=offy;
00388       if(offx < *min_offx) *min_offx=offx;
00389       if(offy < *min_offy) *min_offy=offy;
00390     }
00391   }
00392   *ref_offx=(*min_offx+*max_offx)/2;
00393   *ref_offy=(*min_offy+*max_offy)/2;
00394 
00395   if(cfg->size_x == 0) cfg->size_x=2*floor(*max_offx-*min_offx+0.5)+64 ;
00396 /* The formula above doesn't give always the right result (DSF07663)
00397  * for example, if diff in offset is 35.5, the formula gives 72 but 71 would be
00398  * enough, the following candidate is working fine:
00399  *   if(cfg->size_x == 0)
00400       cfg->size_x=floor(2.0*(*max_offx-*min_offx + 0.5 - 0.00001))+64 ;
00401       Currently the ticket is suspended, because the formula above
00402       would change the scientific results for the previous data.
00403       The same is actual for Y axis.
00404 */
00405   if(cfg->size_y == 0) cfg->size_y=2*floor(*max_offy-*min_offy+0.5)+64 ;
00406 
00407   sinfo_msg("Output cube size: %d x %d",cfg->size_x,cfg->size_y);
00408   sinfo_msg("Ref offset. x: %f y: %f",*ref_offx,*ref_offy);
00409   sinfo_msg("Max offset. x: %f y: %f",*max_offx,*max_offy);
00410   sinfo_msg("Min offset. x: %f y: %f",*min_offx,*min_offy);
00411   return 0;
00412 
00413 
00414 }
00415 
00425 float*
00426 sinfo_read_distances(const int nslits, const char* distlist)
00427 {
00428   int i=0;
00429   int* status=NULL;
00430   float * distances = NULL;
00431   float tmp_float=0;
00432   char tbl_distances_name[FILE_NAME_SZ];
00433   cpl_table* tbl_distances = NULL;
00434 
00435   sinfo_msg("Read distances");
00436   distances = (float*) cpl_calloc (nslits - 1, sizeof (float));
00437 
00438   if ( NULL == distances )
00439     {
00440       sinfo_msg_error ("could allocate memory!") ;
00441       return NULL ;
00442     }
00443 
00444             /*READ TFITS TABLE*/
00445   if(cpl_error_get_code() != CPL_ERROR_NONE) {
00446     sinfo_msg_error("Before loading input table");
00447     sinfo_msg_error((char* ) cpl_error_get_message());
00448     return NULL;
00449   }
00450   strcpy(tbl_distances_name,distlist);
00451   tbl_distances = cpl_table_load(tbl_distances_name,1,0);
00452   if(cpl_error_get_code() != CPL_ERROR_NONE) {
00453     sinfo_msg_error("loading input table %s",tbl_distances_name);
00454     sinfo_msg_error((char* ) cpl_error_get_message());
00455     return NULL;
00456   }
00457 
00458   for (i =0 ; i< nslits-1; i++){
00459     tmp_float=cpl_table_get_float(tbl_distances,"slitlet_distance",i,status);
00460     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00461       sinfo_msg_error("reading col %s from table %s","slitlet_distance",
00462                        tbl_distances_name);
00463       sinfo_msg_error((char* ) cpl_error_get_message());
00464       return NULL;
00465     }
00466     sinfo_new_array_set_value(distances,tmp_float,i);
00467   }
00468   cpl_table_delete(tbl_distances);
00469   return distances;
00470 
00471 }
00472 
00473 
00474 
00482 float**
00483 sinfo_read_slitlets_edges(const int nslits, const char* poslist)
00484 {
00485 
00486 
00487   char tbl_slitpos_name[FILE_NAME_SZ];
00488   cpl_table* tbl_slitpos=NULL;
00489   int n=0;
00490   int i=0;
00491   int* status=NULL;
00492   float edge_x=0;
00493   float edge_y=0;
00494   float ** slit_edges = NULL;
00495 
00496   slit_edges = sinfo_new_2Dfloatarray(nslits, 2) ;
00497 
00498   strcpy(tbl_slitpos_name,poslist);
00499   tbl_slitpos = cpl_table_load(tbl_slitpos_name,1,0);
00500   if(cpl_error_get_code() != CPL_ERROR_NONE) {
00501     sinfo_msg_error("error loading tbl %s",tbl_slitpos_name);
00502     sinfo_msg_error((char* ) cpl_error_get_message());
00503     return NULL;
00504   }
00505   n = cpl_table_get_nrow(tbl_slitpos);
00506   if (n != nslits) {
00507     sinfo_msg_error("No of slitlets in table is n = %d != %d !",n,nslits);
00508     return NULL;
00509   }
00510 
00511   for (i =0 ; i< nslits; i++){
00512     edge_x=cpl_table_get_double(tbl_slitpos,"pos1",i,status);
00513     edge_y=cpl_table_get_double(tbl_slitpos,"pos2",i,status);
00514     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00515       sinfo_msg_error("error reading tbl %s row %d",tbl_slitpos_name,i);
00516       sinfo_msg_error((char* ) cpl_error_get_message());
00517       return NULL;
00518     }
00519     sinfo_new_array2D_set_value(slit_edges,edge_x,i,0);
00520     sinfo_new_array2D_set_value(slit_edges,edge_y,i,1);
00521   }
00522   cpl_table_delete(tbl_slitpos);
00523   if(cpl_error_get_code() != CPL_ERROR_NONE) {
00524     sinfo_msg_error("error reading tbl %s",tbl_slitpos_name);
00525     sinfo_msg_error((char* ) cpl_error_get_message());
00526     return NULL;
00527   }
00528 
00529   return slit_edges;
00530 
00531 }
00532 
00533 
00534 /*---------------------------------------------------------------------------*/
00557 /*---------------------------------------------------------------------------*/
00558 cpl_imagelist * sinfo_new_cube_getvig(
00559         cpl_imagelist *        cube_in,
00560         int             loleft_x,
00561         int             loleft_y,
00562         int             upright_x,
00563         int             upright_y)
00564 {
00565     cpl_imagelist     *        cube_out ;
00566     int                 i ;
00567     int                 outlx,
00568                                         outly ;
00569 
00570     int ilx=0;
00571     int ily=0;
00572     int inp=0;
00573     cpl_image* i_img=NULL;
00574     cpl_image* o_img=NULL;
00575 
00576     if (cube_in==NULL) return NULL ;
00577 
00578     ilx=cpl_image_get_size_x(cpl_imagelist_get(cube_in,0));
00579     ily=cpl_image_get_size_y(cpl_imagelist_get(cube_in,0));
00580     inp=cpl_imagelist_get_size(cube_in);
00581 
00582         if ((loleft_x>upright_x) ||
00583             (loleft_y>upright_y)) {
00584             sinfo_msg_error("ill-defined slit for extraction: aborting");
00585             return NULL ;
00586         }
00587 
00588     /* Extraction coordinates include rectangular zone  */
00589     outlx = upright_x - loleft_x + 1 ;
00590     outly = upright_y - loleft_y + 1 ;
00591     /*
00592     cube_out = sinfo_new_cube(outlx, outly, cube_in->np) ;
00593     */
00594     cube_out = cpl_imagelist_new() ;
00595     /* Loop on all input planes */
00596     for (i=0 ; i<cpl_imagelist_get_size(cube_in) ; i++) {
00597 
00598       i_img=cpl_imagelist_get(cube_in,i);
00599         /* Extract a slit from this plane   */
00600                 o_img = sinfo_new_image_getvig(i_img,
00601                         loleft_x, loleft_y,
00602                         upright_x, upright_y) ;
00603         cpl_imagelist_set(cube_out,o_img,i);
00604     }
00605     return cube_out ;
00606 }
00607 /*---------------------------------------------------------------------------*/
00624 /*---------------------------------------------------------------------------*/
00625 
00626 static cpl_image *
00627 sinfo_new_image_getvig(
00628         cpl_image    *    image_in,
00629         int             loleft_x,
00630         int             loleft_y,
00631         int             upright_x,
00632         int             upright_y)
00633 {
00634     cpl_image    *        slit_img ;
00635     int                 i, j ;
00636     register
00637     pixelvalue  *       inpt,
00638                                 *       outpt ;
00639     int         outlx, outly ;
00640     int ilx=0;
00641     int ily=0;
00642 
00643     float* pidata=NULL;
00644     float* podata=NULL;
00645 
00646     if (image_in==NULL) return NULL ;
00647 
00648     ilx=cpl_image_get_size_x(image_in);
00649     ily=cpl_image_get_size_y(image_in);
00650     pidata=cpl_image_get_data_float(image_in);
00651 
00652     if ((loleft_x<1) || (loleft_x>ilx) ||
00653         (loleft_y<1) || (loleft_y>ily) ||
00654         (upright_x<1) || (upright_x>ilx) ||
00655         (upright_y<1) || (upright_y>ily) ||
00656         (loleft_x>upright_x) || (loleft_y>upright_y)) {
00657         sinfo_msg_error("extraction zone is [%d %d] [%d %d]\n"
00658                         "cannot extract such zone: aborting slit extraction",
00659                 loleft_x, loleft_y, upright_x, upright_y) ;
00660         return NULL ;
00661     }
00662 
00663     outlx = upright_x - loleft_x + 1 ;
00664     outly = upright_y - loleft_y + 1 ;
00665     slit_img = cpl_image_new(outlx, outly,CPL_TYPE_FLOAT) ;
00666     podata=cpl_image_get_data_float(slit_img);
00667 
00668     for (j=0 ; j<outly ; j++) {
00669         inpt = pidata+loleft_x-1 + (j+loleft_y-1)*ilx ;
00670         outpt = podata + j*outlx ;
00671         for (i=0 ; i<outlx ; i++) {
00672             *outpt++ = *inpt++ ;
00673         }
00674     }
00675     return slit_img ;
00676 }
00677 
00689 int
00690 sinfo_new_set_wcs_cube(cpl_imagelist* cub, const char* name, double clambda,
00691          double dis, double cpix, double cx, double cy)
00692 {
00693   cpl_propertylist* plist=NULL;
00694 
00695   if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
00696     sinfo_msg_error( "getting header from frame %s",name);
00697     cpl_propertylist_delete(plist) ;
00698     return -1 ;
00699   }
00700 
00701   sinfo_new_change_plist_cube(plist, clambda, dis, cpix, cx, cy) ;
00702 
00703   if (cpl_imagelist_save(cub, name, CPL_BPP_IEEE_FLOAT,
00704                          plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00705     sinfo_msg_error( "Cannot save the product %s",name);
00706     cpl_propertylist_delete(plist) ;
00707     return -1 ;
00708   }
00709   cpl_propertylist_delete(plist) ;
00710   return 0;
00711 
00712 }
00721 int
00722 sinfo_new_set_wcs_image(cpl_image* img,
00723                         const char* name,
00724                         double cx,
00725                         double cy)
00726 {
00727   cpl_propertylist* plist=NULL;
00728 
00729   if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
00730     sinfo_msg_error( "getting header from frame %s",name);
00731     cpl_propertylist_delete(plist) ;
00732     return -1 ;
00733   }
00734   sinfo_new_change_plist_image(plist, cx, cy) ;
00735 
00736   if (cpl_image_save(img, name, CPL_BPP_IEEE_FLOAT,
00737                      plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00738     sinfo_msg_error( "Cannot save the product %s",name);
00739     cpl_propertylist_delete(plist) ;
00740     return -1 ;
00741   }
00742   cpl_propertylist_delete(plist) ;
00743   return 0;
00744 
00745 }
00746 
00747 
00757 int
00758 sinfo_new_set_wcs_spectrum(cpl_image* img, const char* name, double clambda,
00759              double dis, double cpix)
00760 {
00761   cpl_propertylist* plist=NULL;
00762 
00763   if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
00764     sinfo_msg_error( "getting header from frame %s",name);
00765     cpl_propertylist_delete(plist) ;
00766     return -1 ;
00767   }
00768   sinfo_new_change_plist_spectrum(plist, clambda, dis,cpix) ;
00769 
00770   if (cpl_image_save(img, name, CPL_BPP_IEEE_FLOAT,
00771                      plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00772     sinfo_msg_error( "Cannot save the product %s",name);
00773     cpl_propertylist_delete(plist) ;
00774     return -1 ;
00775   }
00776   cpl_propertylist_delete(plist) ;
00777   return 0;
00778 
00779 }
00791 static void
00792 sinfo_new_change_plist_cube (cpl_propertylist * plist,
00793             float cenLambda,
00794             float dispersion,
00795             int   center_z,
00796             float center_x,
00797             float center_y )
00798 {
00799 
00800     float pixelscale ;
00801     double ra ;
00802     double dec ;
00803     double angle ;
00804     float radangle ;
00805     double cd1_1, cd1_2, cd2_1, cd2_2 ;
00806     int sign_swap = -1;
00807     char firsttext[2*FILE_NAME_SZ] ;
00808 
00809     double cdelt1=0;
00810     double cdelt2=0;
00811     double cdelt3=dispersion;
00812 
00813     double crpix1=center_x;
00814     double crpix2=center_y;
00815     int crpix3=center_z;
00816 
00817     double crval1=0;
00818     double crval2=0;
00819     double crval3=cenLambda;
00820 
00821 
00822     strcpy(firsttext, "sinfo_rec_objnod -f \0") ;
00823 
00824     pixelscale = sinfo_pfits_get_pixscale(plist)/2. ;
00825     ra = sinfo_pfits_get_ra(plist) ;
00826     dec = sinfo_pfits_get_DEC(plist) ;
00827 
00828     //get better coordinate values
00829     ra=sinfo_pfits_get_targ_alpha(plist);
00830     dec=sinfo_pfits_get_targ_delta(plist);
00831     //sinfo_msg("ra=%f",ra);
00832     //sinfo_msg("dec=%f",dec);
00833     ra=sinfo_hms2deg(ra);
00834     dec=sinfo_sess2deg(dec);
00835     //sinfo_msg("ra=%f",ra);
00836     //sinfo_msg("dec=%f",dec);
00837 
00838     crval1=ra;
00839     crval2=dec;
00840 
00841     angle = sinfo_pfits_get_posangle(plist) ;
00842     /* in PUPIL data there is not posangle info: we reset the error */
00843     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00844        cpl_error_reset();
00845     }
00846     cdelt1=sign_swap*pixelscale / 3600.;
00847     cdelt2=         +pixelscale / 3600.;
00848 
00849 
00850     radangle = angle * PI_NUMB / 180. ;
00851     cd1_1 = +cdelt1*cos(radangle);
00852     cd1_2 = -cdelt2*sin(radangle);
00853     cd2_1 = +cdelt1*sin(radangle);
00854     cd2_2 = +cdelt2*cos(radangle);
00855 
00856 
00857     sinfo_set_coord1(&plist,crpix1,crval1,cdelt1);
00858     sinfo_set_coord2(&plist,crpix2,crval2,cdelt2);
00859     sinfo_set_coord3(&plist,crpix3,crval3,cdelt3);
00860     sinfo_set_cd_matrix2(&plist,cd1_1,cd1_2,cd2_1,cd2_2);
00861     sinfo_set_cd_matrix3(&plist,0,0,0,0,dispersion);
00862 
00863 
00864     }
00865 
00866 
00867 
00868 
00877 static void
00878 sinfo_set_coord1(cpl_propertylist** plist,
00879                  const double crpix1,
00880                  const double crval1,
00881                  const double cdelt1)
00882 {
00883    cpl_propertylist_erase_regexp(*plist, "^CTYPE1",0);
00884     cpl_propertylist_insert_after_string(*plist,"EXPTIME","CTYPE1","RA---TAN");
00885     cpl_propertylist_set_comment(*plist, "CTYPE1", "Projected Rectascension");
00886     cpl_propertylist_erase_regexp(*plist, "^CRPIX1",0);
00887     cpl_propertylist_insert_after_double(*plist,"CTYPE1","CRPIX1", crpix1) ;
00888     cpl_propertylist_set_comment(*plist, "CRPIX1","Reference pixel in RA" ) ;
00889 
00890     cpl_propertylist_erase_regexp(*plist, "^CRVAL1",0);
00891     cpl_propertylist_insert_after_double(*plist, "CRPIX1", "CRVAL1", crval1 ) ;
00892     cpl_propertylist_set_comment(*plist, "CRVAL1","Reference RA" ) ;
00893 
00894     cpl_propertylist_erase_regexp(*plist, "^CDELT1",0);
00895     cpl_propertylist_insert_after_double(*plist,"CRVAL1","CDELT1",cdelt1 ) ;
00896     cpl_propertylist_set_comment(*plist, "CDELT1","pixel scale" ) ;
00897 
00898     cpl_propertylist_erase_regexp(*plist, "^CUNIT1",0);
00899     cpl_propertylist_insert_after_string(*plist, "CDELT1",  "CUNIT1", "deg" ) ;
00900     cpl_propertylist_set_comment(*plist, "CUNIT1","RA-UNIT" ) ;
00901 
00902     return;
00903 }
00904 
00905 
00914 static void
00915 sinfo_set_coord2(cpl_propertylist** plist,
00916                  const double crpix2,
00917                  const double crval2,
00918                  const double cdelt2)
00919 {
00920     cpl_propertylist_erase_regexp(*plist, "^CTYPE2",0);
00921     cpl_propertylist_insert_after_string(*plist,"CUNIT1","CTYPE2","DEC--TAN");
00922     cpl_propertylist_set_comment(*plist, "CTYPE2", "Projected Declination") ;
00923 
00924     cpl_propertylist_erase_regexp(*plist, "^CRPIX2",0);
00925     cpl_propertylist_insert_after_double(*plist,"CTYPE2","CRPIX2",crpix2 ) ;
00926     cpl_propertylist_set_comment(*plist, "CRPIX2", "Reference pixel in DEC") ;
00927 
00928     cpl_propertylist_erase_regexp(*plist,"^CRVAL2",0);
00929     cpl_propertylist_insert_after_double(*plist,"CRPIX2","CRVAL2",crval2) ;
00930     cpl_propertylist_set_comment(*plist,"CRVAL2","Reference DEC") ;
00931 
00932     cpl_propertylist_erase_regexp(*plist,"^CDELT2",0);
00933     cpl_propertylist_insert_after_double(*plist,"CRVAL2","CDELT2",cdelt2 ) ;
00934     cpl_propertylist_set_comment(*plist,"CDELT2","pixel scale") ;
00935 
00936     cpl_propertylist_erase_regexp(*plist,"^CUNIT2",0);
00937     cpl_propertylist_insert_after_string(*plist,"CDELT2","CUNIT2", "deg" ) ;
00938     cpl_propertylist_set_comment(*plist,"CUNIT2","DEC-UNIT") ;
00939 
00940 
00941 }
00942 
00943 
00944 
00953 static void
00954 sinfo_set_coord3(cpl_propertylist** plist,
00955                  const int crpix3,
00956                  const double crval3,
00957                  const double cdelt3)
00958 {
00959     cpl_propertylist_erase_regexp(*plist, "^CTYPE3",0);
00960     cpl_propertylist_insert_after_string(*plist,"EXPTIME", "CTYPE3", "WAVE" ) ;
00961     cpl_propertylist_set_comment(*plist,"CTYPE3","wavelength axis in microns") ;
00962 
00963     cpl_propertylist_erase_regexp(*plist, "^CRPIX3",0);
00964     cpl_propertylist_insert_after_int(*plist,"CTYPE3",  "CRPIX3", crpix3 ) ;
00965     cpl_propertylist_set_comment(*plist, "CRPIX3", "Reference pixel in z") ;
00966 
00967     cpl_propertylist_erase_regexp(*plist, "^CRVAL3",0);
00968     cpl_propertylist_insert_after_double(*plist,"CRPIX3", "CRVAL3", crval3) ;
00969     cpl_propertylist_set_comment(*plist, "CRVAL3", "central wavelength") ;
00970 
00971     cpl_propertylist_erase_regexp(*plist, "^CDELT3",0);
00972 
00973     cpl_propertylist_insert_after_double(*plist,"CRVAL3","CDELT3",cdelt3) ;
00974     cpl_propertylist_set_comment(*plist, "CDELT3", "microns per pixel") ;
00975 
00976     cpl_propertylist_erase_regexp(*plist, "^CUNIT3",0);
00977     cpl_propertylist_insert_after_string(*plist,"CDELT3", "CUNIT3", "mum" ) ;
00978     cpl_propertylist_set_comment(*plist, "CUNIT3",  "spectral unit" ) ;
00979 
00980 }
00981 
00982 
00993 static void
00994 sinfo_set_cd_matrix2(cpl_propertylist** plist,
00995             const double cd1_1,
00996             const double cd1_2,
00997             const double cd2_1,
00998             const double cd2_2)
00999 {
01000 
01001     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD1_1",0));
01002     check_nomsg(cpl_propertylist_insert_after_double(*plist,"EXPTIME",
01003                              "CD1_1", cd1_1 )) ;
01004     check_nomsg(cpl_propertylist_set_comment(*plist, "CD1_1",
01005                          "CD rotation matrix" )) ;
01006 
01007     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD1_2",0));
01008     check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD1_1",
01009                              "CD1_2", cd1_2 )) ;
01010     check_nomsg(cpl_propertylist_set_comment(*plist, "CD1_2",
01011                          "CD rotation matrix" )) ;
01012 
01013     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD2_1",0));
01014     check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD1_2",
01015                              "CD2_1", cd2_1 )) ;
01016     check_nomsg(cpl_propertylist_set_comment(*plist, "CD2_1",
01017                          "CD rotation matrix" )) ;
01018 
01019     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD2_2",0));
01020     check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD2_1",
01021                              "CD2_2", cd2_2 )) ;
01022     check_nomsg(cpl_propertylist_set_comment(*plist, "CD2_2",
01023                          "CD rotation matrix" )) ;
01024 
01025  cleanup:
01026     return;
01027 
01028 
01029 }
01030 
01031 
01044 static void
01045 sinfo_set_cd_matrix3(cpl_propertylist** plist,
01046                      const double cd1_3,
01047                      const double cd2_3,
01048                      const double cd3_1,
01049                      const double cd3_2,
01050                      const double cd3_3)
01051 {
01052 
01053 
01054     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD1_3",0));
01055     check_nomsg(cpl_propertylist_insert_after_double(*plist,"EXPTIME",
01056                              "CD1_3", cd1_3 )) ;
01057     check_nomsg(cpl_propertylist_set_comment(*plist, "CD1_3",
01058                          "CD rotation matrix" )) ;
01059 
01060 
01061     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD2_3",0));
01062     check_nomsg(cpl_propertylist_insert_after_double(*plist,"CD1_3",
01063                              "CD2_3", cd2_3 )) ;
01064     check_nomsg(cpl_propertylist_set_comment(*plist, "CD2_3",
01065                          "CD rotation matrix" )) ;
01066 
01067 
01068 
01069     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD3_1",0));
01070     check_nomsg(cpl_propertylist_insert_after_double(*plist,"CD2_3",
01071                              "CD3_1", cd3_1 )) ;
01072     check_nomsg(cpl_propertylist_set_comment(*plist, "CD3_1",
01073                          "CD rotation matrix" )) ;
01074 
01075     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD3_2",0));
01076     check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD3_1",
01077                              "CD3_2", cd3_2 )) ;
01078     check_nomsg(cpl_propertylist_set_comment(*plist, "CD3_2",
01079                          "CD rotation matrix" )) ;
01080 
01081     check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD3_3",0));
01082     check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD3_2",
01083                              "CD3_3", cd3_3 )) ;
01084     check_nomsg(cpl_propertylist_set_comment(*plist, "CD3_3",
01085                          "CD rotation matrix" )) ;
01086 
01087  cleanup:
01088     return;
01089 
01090 
01091 }
01092 
01093 
01102 static void
01103 sinfo_set_spect_coord1(cpl_propertylist** plist,
01104                  const int crpix1,
01105                  const double crval1,
01106                  const double cdelt1)
01107 {
01108 
01109     cpl_propertylist_erase_regexp(*plist, "^CTYPE1",0);
01110     cpl_propertylist_insert_after_string(*plist,"EXPTIME",  "CTYPE1", "PIXEL");
01111     cpl_propertylist_set_comment(*plist, "CTYPE1", "Pixel coordinate system.");
01112 
01113     cpl_propertylist_erase_regexp(*plist, "^CRPIX1",0);
01114     cpl_propertylist_insert_after_int(*plist, "CTYPE1",  "CRPIX1", crpix1 ) ;
01115     cpl_propertylist_set_comment(*plist, "CRPIX1", "Reference pixel in x") ;
01116 
01117     cpl_propertylist_erase_regexp(*plist, "^CRVAL1",0);
01118     cpl_propertylist_insert_after_double(*plist,"CRPIX1",  "CRVAL1", crval1 ) ;
01119     cpl_propertylist_set_comment(*plist, "CRVAL1", "value of ref pixel.") ;
01120 
01121     cpl_propertylist_erase_regexp(*plist, "^CDELT1",0);
01122     cpl_propertylist_insert_after_double(*plist,"CRVAL1",  "CDELT1", cdelt1 ) ;
01123     cpl_propertylist_set_comment(*plist,"CDELT1", "pixel scale") ;
01124 
01125 
01126     cpl_propertylist_erase_regexp(*plist, "^CUNIT1",0);
01127     cpl_propertylist_insert_after_string(*plist,"CDELT1",  "CUNIT1", "Pixel" );
01128     cpl_propertylist_set_comment(*plist, "CUNIT1",  "spectral unit" );
01129 
01130 }
01131 
01132 
01133 
01134 
01135 
01144 static void
01145 sinfo_set_spect_coord2(cpl_propertylist** plist,
01146                  const int crpix2,
01147                  const double crval2,
01148                  const double cdelt2)
01149 {
01150 
01151     cpl_propertylist_erase_regexp(*plist, "^CTYPE2",0);
01152     cpl_propertylist_insert_after_string(*plist, "EXPTIME","CTYPE2","WAVE" );
01153     cpl_propertylist_set_comment(*plist,"CTYPE2","wavelength axis in microns");
01154 
01155     cpl_propertylist_erase_regexp(*plist, "^CRPIX2",0);
01156     cpl_propertylist_insert_after_int(*plist, "CTYPE2",  "CRPIX2",crpix2 ) ;
01157     cpl_propertylist_set_comment(*plist, "CRPIX2", "Reference pixel in x") ;
01158 
01159     cpl_propertylist_erase_regexp(*plist, "^CRVAL2",0);
01160     cpl_propertylist_insert_after_double(*plist, "CRPIX2","CRVAL2",crval2 ) ;
01161     cpl_propertylist_set_comment(*plist,"CRVAL2", "central wavelength") ;
01162 
01163     cpl_propertylist_erase_regexp(*plist, "^CDELT2",0);
01164     cpl_propertylist_insert_after_double(*plist, "CRVAL2", "CDELT2",cdelt2);
01165     cpl_propertylist_set_comment(*plist,"CDELT2", "microns per pixel");
01166 
01167     cpl_propertylist_erase_regexp(*plist, "^CUNIT2",0);
01168     cpl_propertylist_insert_after_string(*plist,"CDELT2",  "CUNIT2", "mum");
01169     cpl_propertylist_set_comment(*plist, "CUNIT2",  "spectral unit" );
01170 
01171 
01172 }
01182 static void
01183 sinfo_new_change_plist_spectrum (cpl_propertylist * plist,
01184                 double cenLambda,
01185                 double dispersion,
01186                 int   cenpix)
01187 {
01188 
01189   int crpix1=1;
01190   double crval1=1;
01191   double cdelt1=1;
01192 
01193   int crpix2=cenpix;
01194   double crval2=cenLambda;
01195   double cdelt2=dispersion;
01196 
01197 
01198 
01199   cpl_propertylist_erase_regexp(plist, "^CTYPE3",0);
01200   cpl_propertylist_erase_regexp(plist, "^CRPIX3",0);
01201   cpl_propertylist_erase_regexp(plist, "^CRVAL3",0);
01202   cpl_propertylist_erase_regexp(plist, "^CDELT3",0);
01203   cpl_propertylist_erase_regexp(plist, "^CUNIT3",0);
01204 
01205   cpl_propertylist_erase_regexp(plist, "^CTYPE2",0);
01206   cpl_propertylist_erase_regexp(plist, "^CRPIX2",0);
01207   cpl_propertylist_erase_regexp(plist, "^CRVAL2",0);
01208   cpl_propertylist_erase_regexp(plist, "^CDELT2",0);
01209   cpl_propertylist_erase_regexp(plist, "^CUNIT2",0);
01210 
01211 
01212   cpl_propertylist_erase_regexp(plist, "^CD1_1",0);
01213   cpl_propertylist_erase_regexp(plist, "^CD1_2",0);
01214   cpl_propertylist_erase_regexp(plist, "^CD2_1",0);
01215   cpl_propertylist_erase_regexp(plist, "^CD2_2",0);
01216 
01217 
01218 
01219 
01220   sinfo_set_spect_coord1(&plist,crpix1,crval1,cdelt1);
01221   sinfo_set_spect_coord2(&plist,crpix2,crval2,cdelt2);
01222 
01223 
01224 
01225 }
01235 static void
01236 sinfo_new_change_plist_image (cpl_propertylist * plist,
01237             float center_x,
01238             float center_y )
01239 {
01240 
01241     float pixelscale ;
01242     double ra ;
01243     double dec ;
01244     double angle ;
01245     float radangle ;
01246     float cd1_1, cd1_2, cd2_1, cd2_2 ;
01247     char firsttext[2*FILE_NAME_SZ] ;
01248     int sign_swap = -1;
01249 
01250 
01251     double cdelt1=0;
01252     double cdelt2=0;
01253 
01254     double crpix1=center_x;
01255     double crpix2=center_y;
01256 
01257     double crval1=0;
01258     double crval2=0;
01259 
01260 
01261 
01262 
01263     strcpy(firsttext, "sinfo_rec_objnod -f \0") ;
01264 
01265     pixelscale = sinfo_pfits_get_pixscale(plist)/2. ;
01266     ra = sinfo_pfits_get_ra(plist) ;
01267     dec = sinfo_pfits_get_DEC(plist) ;
01268 
01269 
01270 
01271     //get better coordinate values
01272     ra=sinfo_pfits_get_targ_alpha(plist);
01273     dec=sinfo_pfits_get_targ_delta(plist);
01274     ra=sinfo_hms2deg(ra);
01275     dec=sinfo_sess2deg(dec);
01276 
01277 
01278 
01279     crval1=ra;
01280     crval2=dec;
01281 
01282     angle = sinfo_pfits_get_posangle(plist) ;
01283     /* in PUPIL data there is not posangle info: we reset the error */
01284     if(cpl_error_get_code() != CPL_ERROR_NONE) {
01285        cpl_error_reset();
01286     }
01287 
01288     radangle = angle * PI_NUMB / 180. ;
01289 
01290     cdelt1=sign_swap * pixelscale / 3600. ;
01291     cdelt2=            pixelscale / 3600. ;
01292 
01293 
01294     cd1_1 = +cdelt1*cos(radangle) ;
01295     cd1_2 = -cdelt2*sin(radangle) ;
01296     cd2_1 = +cdelt1*sin(radangle) ;
01297     cd2_2 = +cdelt2*cos(radangle) ;
01298 
01299 
01300 
01301 
01302 
01303     check_nomsg(sinfo_set_coord1(&plist,crpix1,crval1,cdelt1));
01304     check_nomsg(sinfo_set_coord2(&plist,crpix2,crval2,cdelt2));
01305     check_nomsg(sinfo_set_cd_matrix2(&plist,cd1_1,cd1_2,cd2_1,cd2_2));
01306 
01307 
01308  cleanup:
01309     return;
01310 }
01311 
01319 cpl_imagelist**
01320 sinfo_new_sinfoni_correct_median(cpl_imagelist** cubes, const int n_cubes)
01321 {
01322   int i=0;
01323   cpl_imagelist** cubes_cor=NULL;
01324   double local_median=0;
01325   int z=0;
01326   cpl_image* i_img=NULL;
01327   cpl_image* o_img=NULL;
01328 
01329 
01330   if ( cubes == NULL ) {
01331     sinfo_msg_error ("no cube list given!") ;
01332     return NULL ;
01333   }
01334   if ( n_cubes <= 0 ) {
01335     sinfo_msg_error ("wrong number of data cubes in list!") ;
01336     return NULL ;
01337   }
01338 
01339   cubes_cor = (cpl_imagelist**) cpl_calloc (n_cubes, sizeof (cpl_imagelist*)) ;
01340 
01341   for ( i = 0 ; i < n_cubes ; i++ ) {
01342     cubes_cor[i] = cpl_imagelist_new();
01343     for(z=0;z< cpl_imagelist_get_size(cubes[i]); z++) {
01344       i_img=cpl_imagelist_get(cubes[i],z);
01345       local_median=cpl_image_get_median(i_img);;
01346       o_img=cpl_image_duplicate(i_img);
01347       if(!isnan(local_median)) {
01348         cpl_image_subtract_scalar(o_img,local_median);
01349        }
01350       cpl_imagelist_set(cubes_cor[i],o_img,z);
01351     }
01352   }
01353 
01354   return cubes_cor;
01355 }
01356 
01363 int sinfo_new_sinfoni_correct_median_it(cpl_imagelist** inp)
01364 {
01365 
01366   double local_median=0;
01367   int z=0;
01368   cpl_image* img=NULL;
01369 
01370   for(z=0;z< cpl_imagelist_get_size((*inp)); z++) {
01371     img=cpl_imagelist_get((*inp),z);
01372     local_median=sinfo_new_my_median_image(img);
01373     if(!isnan(local_median)) {
01374       cpl_image_subtract_scalar(img,local_median);
01375     }  else {
01376       sinfo_msg_error("local_median is NAN");
01377     }
01378     cpl_imagelist_set((*inp),img,z);
01379   }
01380 
01381   return 0;
01382 }
01383 
01393 cpl_imagelist** sinfo_new_sinfoni_correct_sky(cpl_imagelist** cubes,
01394                   const int nc,
01395                   cpl_imagelist* sky_cube)
01396 
01397 {
01398   cpl_imagelist** cubes_sky=NULL;
01399   int x=0;
01400   int y=0;
01401   int z=0;
01402   int i=0;
01403   float k=0.5;
01404   int ovr=0;
01405   int ks=0;
01406   int nclip=0;
01407   double med=0;
01408   double avg=0;
01409   double sig=0;
01410   int msk_sum=0;
01411   double val_msk_sum=0;
01412   cpl_vector* val=NULL;
01413   cpl_vector* msk=NULL;
01414   int ilx=0;
01415   int ily=0;
01416   int inp=0;
01417 
01418 
01419   int sky_lx=0;
01420   int sky_ly=0;
01421 
01422   float* pidata=NULL;
01423   float* p_sky_data=NULL;
01424   cpl_image* i_img=NULL;
01425   cpl_image* sky_img=NULL;
01426 
01427 
01428   if ( cubes == NULL ) {
01429     sinfo_msg_error ("no cube list given!") ;
01430     return NULL ;
01431   }
01432   ilx=cpl_image_get_size_x(cpl_imagelist_get(cubes[0],0));
01433   ily=cpl_image_get_size_y(cpl_imagelist_get(cubes[0],0));
01434   inp=cpl_imagelist_get_size(cubes[0]);
01435 
01436 
01437   sky_lx=cpl_image_get_size_x(cpl_imagelist_get(sky_cube,0));
01438   sky_ly=cpl_image_get_size_y(cpl_imagelist_get(sky_cube,0));
01439   if ( nc <= 0 ) {
01440     sinfo_msg_error ("wrong number of data cubes in list!") ;
01441     return NULL ;
01442   }
01443 
01444   cubes_sky = (cpl_imagelist**) cpl_calloc (nc, sizeof (cpl_imagelist*)) ;
01445   for(z=0;z< inp; z++) {
01446     sky_img=cpl_imagelist_get(sky_cube,z);
01447     p_sky_data=cpl_image_get_data_float(sky_img);
01448     for(y=0;y< ily; y++) {
01449       for(x=0;x< ilx; x++) {
01450         /* here we start to do a k-s clipping */
01451     msk=cpl_vector_new(nc);
01452     for (i=0;i<nc;i++) {
01453       cpl_vector_set(msk,i,1);
01454     }
01455         nclip=0;
01456         for (ks=0;ks<nc;ks++) {
01457       sig=0;
01458       med=0;
01459       ovr=0;
01460       val=cpl_vector_new(nc-nclip);
01461 
01462       for ( i = 0 ; i < nc ; i++ ) {
01463             i_img=cpl_imagelist_get(cubes[i],z);
01464             pidata=cpl_image_get_data_float(i_img);
01465         if ((!isnan(pidata[x+y*ilx])) &&
01466                  (cpl_vector_get(msk,i) != 0) ) {
01467           cpl_vector_set(val,ovr,(double)pidata[x+y*ilx]);
01468           ovr++;
01469         }
01470       }
01471 
01472       if(ovr>0) {
01473         avg=cpl_vector_get_mean(val);
01474         med=cpl_vector_get_median_const(val);
01475         if(ovr>1) {
01476           sig=cpl_vector_get_stdev(val);
01477         } else {
01478               sig=0;
01479         }
01480       } else {
01481         avg=cpl_vector_get(val,0);
01482         med=avg;
01483         sig=0;
01484       }
01485 
01486       cpl_vector_delete(val);
01487       for ( i = 0 ; i < nc ; i++ ) {
01488             i_img=cpl_imagelist_get(cubes[i],z);
01489             pidata=cpl_image_get_data_float(i_img);
01490       /* Do k-s clipping at each pixel */
01491         if ((!isnan(pidata[x+y*ilx])) &&
01492                 (cpl_vector_get(msk,i) != 0)) {
01493           if(abs((pidata[x+y*ilx]-med))> k*sig) {
01494         cpl_vector_set(msk,i,0);
01495                 nclip++;
01496           }
01497         }
01498       }
01499     }/* end of k-s clipping */
01500     msk_sum=0;
01501     val_msk_sum=0;
01502     for ( i = 0 ; i < nc ; i++ ) {
01503       /* computes sky at each point */
01504       if (!isnan(pidata[x+y*ilx])) {
01505         msk_sum+=cpl_vector_get(msk,i);
01506                 val_msk_sum+=pidata[x+y*ilx]*
01507           cpl_vector_get(msk,i);
01508       }
01509     }
01510     p_sky_data[x+y*sky_lx]=val_msk_sum/msk_sum;
01511     cpl_vector_delete(msk);
01512       } /* end loop over x */
01513     } /* end loop over y */
01514   } /* end loop over z */
01515   for ( i = 0 ; i < nc ; i++ ) {
01516     cubes_sky[i]=cpl_imagelist_duplicate(cubes[i]);
01517     /* subtract the variable clean sky */
01518     cpl_imagelist_subtract(cubes_sky[i],sky_cube);
01519 
01520   }
01521 
01522 
01523   return cubes_sky;
01524 }
01525 
01540 cpl_imagelist** sinfo_new_sinfoni_correct_sky2(cpl_imagelist** cubes,
01541                   const int nc,
01542                   cpl_imagelist* sky_cube,
01543                               cpl_imagelist* med_cube,
01544                               cpl_imagelist* msk_cube,
01545                               cpl_imagelist* avg_cube,
01546                               cpl_imagelist* sig_cube,
01547                               cpl_imagelist* ovr_cube)
01548 {
01549   cpl_imagelist** cubes_sky=NULL;
01550   int x=0;
01551   int y=0;
01552   int z=0;
01553   int i=0;
01554   float k=0.5;
01555   int ovr=0;
01556   int ks=0;
01557   int nclip=0;
01558   double med=0;
01559   double avg=0;
01560   double sig=0;
01561   int msk_sum=0;
01562   double val_msk_sum=0;
01563   cpl_vector* val=NULL;
01564   cpl_vector* msk=NULL;
01565 
01566   int ilx=0;
01567   int ily=0;
01568   int inp=0;
01569 
01570   int ovr_lx=0;
01571   int msk_lx=0;
01572   int avg_lx=0;
01573   int sig_lx=0;
01574   int sky_lx=0;
01575   int med_lx=0;
01576 
01577 
01578   float* p_ovr_data=NULL;
01579   float* p_msk_data=NULL;
01580   float* p_avg_data=NULL;
01581   float* p_sig_data=NULL;
01582   float* p_sky_data=NULL;
01583   float* p_med_data=NULL;
01584 
01585   float* pidata=NULL;
01586 
01587   if ( cubes == NULL ) {
01588     sinfo_msg_error ("no cube list given!") ;
01589     return NULL ;
01590   }
01591 
01592   ilx=cpl_image_get_size_x(cpl_imagelist_get(cubes[0],0));
01593   ily=cpl_image_get_size_y(cpl_imagelist_get(cubes[0],0));
01594   inp=cpl_imagelist_get_size(cubes[0]);
01595 
01596   if ( nc <= 0 ) {
01597     sinfo_msg_error ("wrong number of data cubes in list!") ;
01598     return NULL ;
01599   }
01600 
01601   cubes_sky = (cpl_imagelist**) cpl_calloc (nc, sizeof (cpl_imagelist*)) ;
01602 
01603   ovr_lx=ilx;
01604   msk_lx=ilx;
01605   avg_lx=ilx;
01606   sig_lx=ilx;
01607   sky_lx=ilx;
01608   med_lx=ilx;
01609 
01610   for(z=0;z< inp; z++) {
01611     p_ovr_data=cpl_image_get_data_float(cpl_imagelist_get(ovr_cube,z));
01612     p_msk_data=cpl_image_get_data_float(cpl_imagelist_get(msk_cube,z));
01613     p_avg_data=cpl_image_get_data_float(cpl_imagelist_get(avg_cube,z));
01614     p_sig_data=cpl_image_get_data_float(cpl_imagelist_get(sig_cube,z));
01615     p_sky_data=cpl_image_get_data_float(cpl_imagelist_get(sky_cube,z));
01616     p_med_data=cpl_image_get_data_float(cpl_imagelist_get(med_cube,z));
01617 
01618 
01619     for(y=0;y< ily; y++) {
01620       for(x=0;x< ilx; x++) {
01621         /* here we start to do a k-s clipping */
01622     msk=cpl_vector_new(nc);
01623     for (i=0;i<nc;i++) {
01624       cpl_vector_set(msk,i,1);
01625     }
01626     p_ovr_data[x+y*ovr_lx]=nc;
01627     p_msk_data[x+y*msk_lx]=nc;
01628         nclip=0;
01629         for (ks=0;ks<nc;ks++) {
01630       sig=0;
01631       med=0;
01632       ovr=0;
01633       val=cpl_vector_new(nc-nclip);
01634 
01635       for ( i = 0 ; i < nc ; i++ ) {
01636             pidata=cpl_image_get_data_float(cpl_imagelist_get(cubes[i],z));
01637         if ((!isnan(pidata[x+y*ilx])) &&
01638                  (cpl_vector_get(msk,i) != 0) ) {
01639           cpl_vector_set(val,ovr,(double)pidata[x+y*ilx]);
01640           ovr++;
01641         }
01642       }
01643 
01644       if(ovr>1) {
01645         avg=cpl_vector_get_mean(val);
01646         med=cpl_vector_get_median_const(val);
01647         sig=cpl_vector_get_stdev(val);
01648       } else {
01649         avg=cpl_vector_get(val,0);
01650         med=avg;
01651         sig=0;
01652       }
01653 
01654       p_med_data[x+y*med_lx]=med;
01655       p_avg_data[x+y*avg_lx]=avg;
01656       p_sig_data[x+y*sig_lx]=sig;
01657       cpl_vector_delete(val);
01658       for ( i = 0 ; i < nc ; i++ ) {
01659            pidata=cpl_image_get_data_float(cpl_imagelist_get(cubes[i],z));
01660       /* Do k-s clipping at each pixel */
01661         if ((!isnan(pidata[x+y*ilx])) &&
01662                 (cpl_vector_get(msk,i) != 0)) {
01663           if(abs((pidata[x+y*ilx]-med))> k*sig) {
01664         /* pidata[x+y*ilx]=0; */
01665         p_msk_data[x+y*msk_lx]-=1;
01666         cpl_vector_set(msk,i,0);
01667                 nclip++;
01668           }
01669         }
01670       }
01671     }/* end of k-s clipping */
01672     msk_sum=0;
01673     val_msk_sum=0;
01674     for ( i = 0 ; i < nc ; i++ ) {
01675            pidata=cpl_image_get_data_float(cpl_imagelist_get(cubes[i],z));
01676       /* computes sky at each point */
01677       if (!isnan(pidata[x+y*ilx])) {
01678         /*
01679         msk_sum+=p_msk_data[x+y*msk_lx];
01680                 val_msk_sum+=pidata[x+y*cubes[i]->lx]*
01681           p_msk_data[x+y*msk_lx];
01682         */
01683         msk_sum+=cpl_vector_get(msk,i);
01684                 val_msk_sum+=pidata[x+y*ilx]*
01685           cpl_vector_get(msk,i);
01686       }
01687     }
01688     p_sky_data[x+y*sky_lx]=val_msk_sum/msk_sum;
01689     cpl_vector_delete(msk);
01690       } /* end loop over x */
01691     } /* end loop over y */
01692   } /* end loop over z */
01693   for ( i = 0 ; i < nc ; i++ ) {
01694     cubes_sky[i]=cpl_imagelist_duplicate(cubes[i]);
01695     /* subtract the variable clean sky */
01696     cpl_imagelist_subtract(cubes_sky[i],sky_cube);
01697   }
01698 
01699 
01700   return cubes_sky;
01701 }
01702 
01703 
01716 int
01717 sinfo_new_assign_offset(const int n,
01718                         const char* name,
01719                         float* offsetx,
01720                         float* offsety,
01721                         const float ref_offx,
01722                         const float ref_offy)
01723 {
01724 
01725   float offx=0;
01726   float offy=0;
01727   double mjd_obs=0;
01728   /*
01729   double pixelscale=0;
01730   double angle=0;
01731   double radangle=0;
01732   double cd1_1=0;
01733   double cd1_2=0;
01734   double cd2_1=0;
01735   double cd2_2=0;
01736   double ra=0;
01737   double dec=0;
01738   */
01739 
01740   cpl_propertylist * plist=NULL;
01741   sinfo_msg_debug("Assign offsets");
01742 
01743   if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
01744     sinfo_msg_error( "getting header from reference frame %s",name);
01745     cpl_propertylist_delete(plist) ;
01746     return -1 ;
01747   }
01748 
01749   offx = sinfo_pfits_get_cumoffsetx(plist) - ref_offx ;  /* was - */
01750   if(cpl_error_get_code() != CPL_ERROR_NONE) {
01751     sinfo_msg_warning(" could not read fits header keyword cummoffsetx!") ;
01752     sinfo_msg_warning(" Set relative offset to 0 - %f!",ref_offx) ;
01753     offx =  - ref_offx;
01754     cpl_error_reset();
01755     /* return -1 ; */
01756   }
01757 
01758   offy = sinfo_pfits_get_cumoffsety(plist) - ref_offy ; /* was - */
01759   if(cpl_error_get_code() != CPL_ERROR_NONE) {
01760     sinfo_msg_warning(" could not read fits header keyword! cumoffsety") ;
01761     sinfo_msg_warning(" Set relative offset to 0 - %f!",ref_offx) ;
01762     offy =  - ref_offy;
01763     cpl_error_reset();
01764     /* return -1 ; */
01765   }
01766   sinfo_msg_debug("offx=%f offy=%f",offx,offy);
01767 
01768   if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
01769     mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
01770   } else {
01771     sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
01772     cpl_propertylist_delete(plist) ;
01773     return -1;
01774   }
01775 
01776   cpl_propertylist_delete(plist) ;
01777 
01778   if (mjd_obs > 53825. ) {
01779     /* April 1st 2006 */
01780     //sinfo_msg("New cumoffset setting convention");
01781     sinfo_new_array_set_value(offsetx,2*offx,n);
01782     sinfo_new_array_set_value(offsety,2*offy,n);
01783   } else if ((mjd_obs > 53421.58210082 ) && (mjd_obs <= 53825.)){
01784     /* after detector's upgrade */
01785     /*
01786     sinfo_new_array_set_value(offsetx,-offx*2,n);
01787     sinfo_new_array_set_value(offsety,+offy*2,n);
01788     */
01789     sinfo_new_array_set_value(offsetx,-2*offx,n);
01790     sinfo_new_array_set_value(offsety,2*offy,n);
01791   } else {
01792     /* before detector's upgrade */
01793     /*
01794     sinfo_new_array_set_value(offsetx,+offx*2,n);
01795     sinfo_new_array_set_value(offsety,-offy*2,n);
01796     */
01797     sinfo_new_array_set_value(offsetx,2*offx,n);
01798     sinfo_new_array_set_value(offsety,-2*offy,n);
01799   }
01800 
01801   return 0;
01802 
01803 
01804 }
01805 
01806 
01807 
01808 
01809 
01822 int
01823 sinfo_new_assign_offset2(const int n,
01824                         const char* name,
01825                         float* offsetx,
01826                         float* offsety,
01827                         const float ref_offx,
01828                         const float ref_offy)
01829 {
01830 
01831   float offx=0;
01832   float offy=0;
01833   double mjd_obs=0;
01834 
01835   cpl_propertylist * plist=NULL;
01836   sinfo_msg_debug("Assign offsets as set by user");
01837   offx = offsetx[n] - ref_offx ;  /* was - */
01838   offy = offsety[n] - ref_offy ;  /* was - */
01839   sinfo_msg_debug("offx=%f offy=%f",offx,offy);
01840 
01841   sinfo_msg_debug("Assign offsets");
01842 
01843   if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
01844     sinfo_msg_error( "getting header from reference frame %s",name);
01845     cpl_propertylist_delete(plist) ;
01846     return -1 ;
01847   }
01848 
01849   if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
01850     mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
01851   } else {
01852     sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
01853     cpl_propertylist_delete(plist) ;
01854     return -1;
01855   }
01856   cpl_propertylist_delete(plist) ;
01857 
01858   //sinfo_new_array_set_value(offsetx,-2*offx,n);
01859   //sinfo_new_array_set_value(offsety,2*offy,n);
01860 
01861   if (mjd_obs > 53825. ) {
01862     /* April 1st 2006 */
01863     //sinfo_msg("New cumoffset setting convention");
01864     sinfo_new_array_set_value(offsetx,2*offx,n);
01865     sinfo_new_array_set_value(offsety,2*offy,n);
01866   } else if ((mjd_obs > 53421.58210082 ) && (mjd_obs <= 53825.)){
01867     /* after detector's upgrade */
01868     /*
01869     sinfo_new_array_set_value(offsetx,-offx*2,n);
01870     sinfo_new_array_set_value(offsety,+offy*2,n);
01871     */
01872     sinfo_new_array_set_value(offsetx,-2*offx,n);
01873     sinfo_new_array_set_value(offsety,2*offy,n);
01874   } else {
01875     /* before detector's upgrade */
01876     /*
01877     sinfo_new_array_set_value(offsetx,+offx*2,n);
01878     sinfo_new_array_set_value(offsety,-offy*2,n);
01879     */
01880     sinfo_new_array_set_value(offsetx,2*offx,n);
01881     sinfo_new_array_set_value(offsety,-2*offy,n);
01882   }
01883 
01884 
01885   return 0;
01886 
01887 
01888 }
01889 
01890 
01903 int
01904 sinfo_new_object_assign_offset(const char* name,
01905                                const int n,
01906                                double* ref_offx,
01907                                double* ref_offy,
01908                                float** offsetx,
01909                                float** offsety)
01910 {
01911 
01912   float offx=0;
01913   float offy=0;
01914   double mjd_obs=0;
01915   cpl_propertylist * plist=NULL;
01916   sinfo_msg_debug("Assign offsets");
01917 
01918   if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
01919     sinfo_msg_error( "getting header from reference frame %s",name);
01920     cpl_propertylist_delete(plist) ;
01921     return -1 ;
01922   }
01923   if ( n == 0 ) {
01924 
01925     *ref_offx = sinfo_pfits_get_cumoffsetx(plist) ;
01926     if(cpl_error_get_code() != CPL_ERROR_NONE) {
01927       sinfo_msg_error(" could not read fits header keyword cummoffsetx!") ;
01928       /* return -1 ; */
01929       cpl_error_reset();
01930     }
01931 
01932     *ref_offy = sinfo_pfits_get_cumoffsety(plist) ;
01933     if(cpl_error_get_code() != CPL_ERROR_NONE) {
01934       sinfo_msg_error(" could not read fits header keyword! cumoffsety") ;
01935       cpl_error_reset();
01936        /* return -1 ; */
01937     }
01938     sinfo_msg_debug("Reference offx=%f offy=%f",*ref_offx,*ref_offy);
01939 
01940     offx = 0. ;
01941     offy = 0. ;
01942 
01943   } else {
01944 
01945     offx = sinfo_pfits_get_cumoffsetx(plist) - *ref_offx ;  /* was - */
01946     if(cpl_error_get_code() != CPL_ERROR_NONE) {
01947       sinfo_msg_error(" could not read fits header keyword cummoffsetx!") ;
01948       cpl_error_reset();
01949       /* return -1 ; */
01950     }
01951 
01952     offy = sinfo_pfits_get_cumoffsety(plist) - *ref_offy ; /* was - */
01953     if(cpl_error_get_code() != CPL_ERROR_NONE) {
01954       sinfo_msg_error(" could not read fits header keyword! cumoffsety") ;
01955       /* return -1 ; */
01956       cpl_error_reset();
01957     }
01958     sinfo_msg_debug("offx=%f offy=%f",offx,offy);
01959   }
01960 
01961 
01962     /* rotate the coordinates
01963        offx_rot = cd1_1 * offx + cd2_1 * offy ;
01964        offy_rot = cd1_2 * offx + cd2_2 * offy ;
01965         convert the coordinates to pixel units
01966        offx_rot_pix = offx_rot / pixelscale ;
01967        offy_rot_pix = offy_rot / pixelscale ;
01968        offsetx[i] = offx_rot_pix ;
01969        offsety[i] = offy_rot_pix ;
01970     */
01971 
01972 
01973   if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
01974     mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
01975   } else {
01976     sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
01977     cpl_propertylist_delete(plist) ;
01978     return -1;
01979   }
01980 
01981   cpl_propertylist_delete(plist) ;
01982 
01983   if (mjd_obs > 53825. ) {
01984     /* April 1st 2006 */
01985     //sinfo_msg("New cumoffset setting convention");
01986     sinfo_new_array_set_value(*offsetx,2*offx,n);
01987     sinfo_new_array_set_value(*offsety,2*offy,n);
01988   } else if ((mjd_obs > 53421.58210082 ) && (mjd_obs <= 53825.)){
01989     /* after detector's upgrade */
01990     sinfo_new_array_set_value(*offsetx,-offx*2,n);
01991     sinfo_new_array_set_value(*offsety,+offy*2,n);
01992   } else {
01993     /* before detector's upgrade */
01994     sinfo_new_array_set_value(*offsetx,+offx*2,n);
01995     sinfo_new_array_set_value(*offsety,-offy*2,n);
01996   }
01997 
01998   return 0;
01999 }
02000 
02012 cpl_imagelist*
02013 sinfo_new_fine_tune(cpl_imagelist* cube,
02014               float* correct_dist,
02015               const char* method,
02016               const int order,
02017               const int nslits) {
02018   int i =0;
02019   cpl_imagelist* outcube2=NULL;
02020   float* neg_dist=NULL;
02021   sinfo_msg("Finetuning, method=%s",method);
02022 
02023   if (strcmp(method,"P")==0)
02024     {
02025       outcube2 = sinfo_new_fine_tune_cube( cube, correct_dist, order ) ;
02026       if (outcube2 == NULL)
02027     {
02028       sinfo_msg_error (" could not fine tune the data cube\n") ;
02029       return NULL ;
02030     }
02031     }
02032   else if (strcmp(method,"F")==0)
02033     {
02034       neg_dist=cpl_calloc(nslits,sizeof(float));
02035       for ( i = 0 ; i < nslits ; i++ )
02036     {
02037       neg_dist[i] = -correct_dist[i] ;
02038     }
02039       outcube2 = sinfo_new_fine_tune_cube_by_FFT( cube, neg_dist ) ;
02040      cpl_free(neg_dist);
02041       if ( outcube2 == NULL )
02042     {
02043       sinfo_msg_error (" could not fine tune the data cube\n") ;
02044       return NULL ;
02045     }
02046     }
02047   else if (strcmp(method,"S")==0)
02048     {
02049       outcube2 = sinfo_new_fine_tune_cube_by_spline( cube, correct_dist ) ;
02050       if ( outcube2 == NULL )
02051     {
02052       sinfo_msg_error (" could not fine tune the data cube\n") ;
02053       return NULL ;
02054     }
02055     }
02056   else
02057     {
02058       sinfo_msg_error (" wrong method indicator given!") ;
02059       return NULL ;
02060     }
02061 
02062 
02063 
02064 return outcube2;
02065 
02066 }
02067 
02068 
02069 
02070 /*----------------------------------------------------------------------------*/
02080 /*----------------------------------------------------------------------------*/
02081 
02082 int sinfo_calib_flux_std(
02083         const char  *   seds_file,
02084         const char  *   stdstars,
02085         const char  *   filter,
02086         cpl_frame   *   frame,
02087         cpl_table   *   tab,
02088         double          mag)
02089 {
02090     double                  surface = 3.1415926535897932384626433 * 400 * 400 ;
02091     cpl_propertylist    *   plist ;
02092     double                  dit, ra, dec, magnitude, cent_wl ;
02093     sinfo_band             band ;
02094     int                     nelem ;
02095     cpl_vector          *   wl ;
02096     cpl_vector          *   spec ;
02097     cpl_bivector        *   spec_biv ;
02098     cpl_bivector        *   sed ;
02099     cpl_vector          *   efficiency ;
02100     cpl_vector          *   mag_zero ;
02101     cpl_vector          *   conversion ;
02102     char                    star_name[512] ;
02103     char                    star_type[32] ;
02104     int                     i ;
02105 
02106 
02107     /* Test entries */
02108     if (seds_file == NULL) {
02109       sinfo_msg_warning("Null SEDS file");
02110       return -1 ;
02111     }
02112     if (stdstars == NULL) {
02113       sinfo_msg_warning("Null STDSATRS file");
02114       return -1 ;
02115     }
02116     if (frame == NULL) {
02117       sinfo_msg_warning("Null input frame");
02118       return -1 ;
02119     }
02120     if (tab == NULL) {
02121       sinfo_msg_warning("Null input table");
02122       return -1 ;
02123     }
02124     if (cpl_error_get_code()) {
02125       sinfo_msg_warning("Some error previously set");
02126       return -1 ;
02127     }
02128     /* Get the propertylist */
02129     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0))==NULL) {
02130         cpl_msg_error(__func__, "Cannot load the property list") ;
02131         return -1 ;
02132     }
02133 
02134     /* Get DIT */
02135     dit = sinfo_pfits_get_dit(plist) ;
02136     if (cpl_error_get_code()) {
02137         cpl_msg_error(__func__, "Cannot get DIT") ;
02138         cpl_propertylist_delete(plist);
02139         return -1 ;
02140     }
02141     cpl_msg_info(__func__, "DIT:   %g", dit) ;
02142 
02143     /* Get band */
02144     if ((band = sinfo_get_associated_filter(filter))
02145             == SINFO_BAND_UNKNOWN) {
02146         cpl_msg_error(__func__, "Cannot associate the filter to a BB one") ;
02147         cpl_propertylist_delete(plist);
02148         return -1 ;
02149     }
02150     cpl_msg_info(__func__, "Band : %s", sinfo_std_band_name(band)) ;
02151 
02152     /* Get RA / DEC */
02153     ra =  sinfo_pfits_get_ra(plist) ;
02154     dec =  sinfo_pfits_get_dec(plist) ;
02155     if (cpl_error_get_code()) {
02156         cpl_msg_error(__func__, "Cannot get star position") ;
02157         cpl_propertylist_delete(plist);
02158         return -1 ;
02159     }
02160     cpl_msg_info(__func__, "RA:  %g  DEC:  %g", ra, dec) ;
02161    sinfo_msg("eff4");
02162 
02163     /* Get magnitude */
02164     if (mag < 0) {
02165         /* Reference star */
02166       if ((irplib_stdstar_get_mag(stdstars,ra, dec,
02167                   sinfo_std_band_name(sinfo_get_bbfilter(filter)),
02168                     "all",&magnitude,
02169                                         star_name,star_type,2.0)) == -1) {
02170             cpl_msg_error(__func__, "Cannot find the star in catalogs") ;
02171             cpl_propertylist_delete(plist);
02172             return -1 ;
02173         }
02174     } else {
02175         magnitude = mag ;
02176     }
02177     cpl_propertylist_delete(plist);
02178    //AMO: TODO: why the following??Is it useful to propagate this?
02179     //sprintf(filter_ref, irplib_std_band_name(sinfo_get_bbfilter(filter))) ;
02180     cpl_msg_info(__func__, "Magnitude: %g", magnitude) ;
02181     //AMO: TODO: why the following??Is it useful to propagate this?
02182     //sinfo_spc_jitter_config.std_magnitude = magnitude ;
02183 
02184    sinfo_msg("eff5");
02185 
02186     /* Get band center */
02187     switch (band) {
02188       //case SINFO_BAND_Z:        cent_wl = CENT_WL_BAND_Z ;  break ;
02189       //case SINFO_BAND_SZ:       cent_wl = CENT_WL_BAND_SZ ; break ;
02190         case SINFO_BAND_J:        cent_wl = CENTRALLAMBDA_J ;  break ;
02191         case SINFO_BAND_JBLOCK:   cent_wl = CENTRALLAMBDA_J ;  break ;
02192         case SINFO_BAND_SH:       cent_wl = CENTRALLAMBDA_H ;  break ;
02193         case SINFO_BAND_SK:       cent_wl = CENTRALLAMBDA_K ;  break ;
02194         case SINFO_BAND_K:        cent_wl = CENTRALLAMBDA_K ;  break ;
02195       //case SINFO_BAND_SL:       cent_wl = CENT_WL_BAND_SL ; break ;
02196       //case SINFO_BAND_M:        cent_wl = CENT_WL_BAND_M ;  break ;
02197         default:
02198             cpl_msg_error(__func__, "Unsupported band: %s",
02199                     sinfo_std_band_name(band)) ;
02200             return -1 ;
02201     }
02202     cpl_msg_info(__func__, "Center of band : %g microns", cent_wl) ;
02203 
02204     sinfo_msg("ref star name: %s", star_name);
02205     sinfo_msg("ref star spectral type: %s", star_type);
02206     //AMO: TODO: why the following??Is it useful to propagate this?
02207     //strcpy(isaac_spc_jitter_config.starname, refstar->name) ;
02208     //strcpy(isaac_spc_jitter_config.sptype, refstar->sptype) ;
02209 
02210     /* Get the SED */
02211     if ((sed = irplib_stdstar_get_sed(seds_file, star_type)) == NULL) {
02212         cpl_msg_error(__func__, "Cannot get the SED") ;
02213         return -1 ;
02214     }
02215 
02216     /* Put the spectrum in a bivector */
02217     nelem = cpl_table_get_nrow(tab) ;
02218     wl = cpl_vector_wrap(nelem, cpl_table_get_data_double(tab,
02219                 "wavelength")) ;
02220     spec = cpl_vector_wrap(nelem, cpl_table_get_data_double(tab,
02221                 "counts_tot")) ;
02222     spec_biv = cpl_bivector_wrap_vectors(wl, spec) ;
02223 
02224     sinfo_msg("eff6");
02225 
02226     /* Get the conversion */
02227     if ((conversion = irplib_stdstar_get_conversion(spec_biv, dit, surface,
02228                     4.5, magnitude)) == NULL) {
02229         cpl_msg_error(__func__, "Cannot get the conversion") ;
02230         cpl_bivector_unwrap_vectors(spec_biv) ;
02231         cpl_vector_unwrap(spec) ;
02232         cpl_vector_unwrap(wl) ;
02233         cpl_bivector_delete(sed) ;
02234         return -1 ;
02235     }
02236 
02237 
02238     /* Get the 0 magnitude spectrum */
02239     if ((mag_zero = irplib_stdstar_get_mag_zero(sed,
02240                         cpl_bivector_get_x(spec_biv),
02241                         cent_wl)) == NULL) {
02242         cpl_msg_error(__func__, "Cannot get the 0 magnitude spectrum") ;
02243         cpl_bivector_unwrap_vectors(spec_biv) ;
02244         cpl_vector_unwrap(spec) ;
02245         cpl_vector_unwrap(wl) ;
02246         cpl_bivector_delete(sed) ;
02247         cpl_vector_delete(conversion) ;
02248         return -1 ;
02249     }
02250     cpl_bivector_unwrap_vectors(spec_biv) ;
02251     cpl_vector_unwrap(spec) ;
02252     cpl_vector_unwrap(wl) ;
02253     cpl_bivector_delete(sed) ;
02254 
02255     sinfo_msg("eff7");
02256 
02257     /* Set the mag_zero 0 values to 1 */
02258     for (i=0 ; i<cpl_vector_get_size(mag_zero) ; i++) {
02259         if (cpl_vector_get(mag_zero, i) < 1e-19) {
02260             cpl_vector_set(mag_zero, i, 1.0) ;
02261         }
02262     }
02263 
02264 
02265     /* Get the efficiency */
02266     efficiency = cpl_vector_duplicate(conversion) ;
02267     if (cpl_vector_divide(efficiency, mag_zero) != CPL_ERROR_NONE) {
02268         cpl_msg_error(__func__, "Missing SED info in this wavelength range") ;
02269         cpl_vector_delete(conversion) ;
02270         cpl_vector_delete(mag_zero) ;
02271         cpl_vector_delete(efficiency) ;
02272         return -1 ;
02273     }
02274     cpl_vector_delete(mag_zero) ;
02275 
02276     sinfo_msg("eff8");
02277 
02278     /* Set the efficiency to 0 where the mag_zero is 1 */
02279     for (i=0 ; i<cpl_vector_get_size(mag_zero) ; i++) {
02280         if (cpl_vector_get(mag_zero, i) == 1.0) {
02281             cpl_vector_set(efficiency, i, 0.0) ;
02282         }
02283     }
02284     cpl_vector_delete(mag_zero) ;
02285 
02286     /* Create extra columns for the results */
02287     cpl_table_wrap_double(tab, cpl_vector_get_data(efficiency), "Efficiency") ;
02288     cpl_table_wrap_double(tab, cpl_vector_get_data(conversion), "Conversion") ;
02289     cpl_vector_unwrap(efficiency) ;
02290     cpl_vector_unwrap(conversion) ;
02291 
02292     sinfo_msg("eff9");
02293     /* Plot the spectra if requested */
02294     /*
02295     if (isaac_spc_jitter_config.display) {
02296         wl = cpl_vector_wrap(nelem, cpl_table_get_data_double(tab,
02297                     "X_coordinate")) ;
02298         spec = cpl_vector_wrap(nelem, cpl_table_get_data_double(tab,
02299                     "Conversion")) ;
02300         spec_biv = cpl_bivector_wrap_vectors(wl, spec) ;
02301         irplib_bivector_plot(NULL, "t 'Conversion' w lines",
02302                 NULL, spec_biv) ;
02303         cpl_bivector_unwrap_vectors(spec_biv) ;
02304         cpl_vector_unwrap(spec) ;
02305         spec = cpl_vector_wrap(nelem, cpl_table_get_data_double(tab,
02306                     "Efficiency")) ;
02307         spec_biv = cpl_bivector_wrap_vectors(wl, spec) ;
02308         irplib_bivector_plot(NULL, "t 'Efficiency' w lines", NULL, spec_biv) ;
02309         cpl_bivector_unwrap_vectors(spec_biv) ;
02310         cpl_vector_unwrap(spec) ;
02311         cpl_vector_unwrap(wl) ;
02312     }
02313     */
02314     return 0 ;
02315 }
02316 
02317 /*----------------------------------------------------------------------------*/
02323 /*----------------------------------------------------------------------------*/
02324 sinfo_band sinfo_get_associated_filter(const char * f)
02325 {
02326     if (!strcmp(f, "J"))            return SINFO_BAND_J ;
02327     if (!strcmp(f, "Js"))           return SINFO_BAND_JS ;
02328     if (!strcmp(f, "Z"))            return SINFO_BAND_Z ;
02329     if (!strcmp(f, "SZ"))           return SINFO_BAND_SZ ;
02330     if (!strcmp(f, "SH"))           return SINFO_BAND_SH ;
02331     if (!strcmp(f, "H"))            return SINFO_BAND_H ;
02332     if (!strcmp(f, "Ks"))           return SINFO_BAND_KS ;
02333     if (!strcmp(f, "K"))            return SINFO_BAND_K ;
02334     if (!strcmp(f, "SK"))           return SINFO_BAND_SK ;
02335     if (!strcmp(f, "L"))            return SINFO_BAND_L ;
02336     if (!strcmp(f, "SL"))           return SINFO_BAND_SL ;
02337     if (!strcmp(f, "M"))            return SINFO_BAND_M ;
02338     if (!strcmp(f, "M_NB"))         return SINFO_BAND_M ;
02339     if (!strcmp(f, "NB_1.06"))      return SINFO_BAND_J ;
02340     if (!strcmp(f, "NB_1.08"))      return SINFO_BAND_J ;
02341     if (!strcmp(f, "NB_1.19"))      return SINFO_BAND_J ;
02342     if (!strcmp(f, "NB_1.21"))      return SINFO_BAND_J ;
02343     if (!strcmp(f, "NB_1.26"))      return SINFO_BAND_J ;
02344     if (!strcmp(f, "NB_1.28"))      return SINFO_BAND_J ;
02345     if (!strcmp(f, "NB_1.64"))      return SINFO_BAND_H ;
02346     if (!strcmp(f, "NB_1.71"))      return SINFO_BAND_H ;
02347     if (!strcmp(f, "NB_2.07"))      return SINFO_BAND_KS ;
02348     if (!strcmp(f, "NB_2.09"))      return SINFO_BAND_KS ;
02349     if (!strcmp(f, "NB_2.13"))      return SINFO_BAND_KS ;
02350     if (!strcmp(f, "NB_2.17"))      return SINFO_BAND_KS ;
02351     if (!strcmp(f, "NB_2.19"))      return SINFO_BAND_KS ;
02352     if (!strcmp(f, "NB_2.25"))      return SINFO_BAND_KS ;
02353     if (!strcmp(f, "NB_2.29"))      return SINFO_BAND_KS ;
02354     if (!strcmp(f, "NB_2.34"))      return SINFO_BAND_KS ;
02355     if (!strcmp(f, "NB_3.21"))      return SINFO_BAND_L ;
02356     if (!strcmp(f, "NB_3.28"))      return SINFO_BAND_L ;
02357     if (!strcmp(f, "NB_3.80"))      return SINFO_BAND_L ;
02358     if (!strcmp(f, "NB_4.07"))      return SINFO_BAND_L ;
02359     return SINFO_BAND_UNKNOWN ;
02360 }
02361 
02362 
02363 
02364 /*----------------------------------------------------------------------------*/
02370 /*----------------------------------------------------------------------------*/
02371 sinfo_band sinfo_get_bbfilter(const char * f)
02372 {
02373     if (!strcmp(f, "J"))            return SINFO_BAND_J ;
02374     if (!strcmp(f, "J+Block"))      return SINFO_BAND_J ;
02375     if (!strcmp(f, "Js"))           return SINFO_BAND_J ;
02376     if (!strcmp(f, "Z"))            return SINFO_BAND_J ;
02377     if (!strcmp(f, "SZ"))           return SINFO_BAND_J ;
02378     if (!strcmp(f, "SH"))           return SINFO_BAND_H ;
02379     if (!strcmp(f, "H"))            return SINFO_BAND_H ;
02380     if (!strcmp(f, "Ks"))           return SINFO_BAND_KS ;
02381     if (!strcmp(f, "K"))            return SINFO_BAND_K ;
02382     if (!strcmp(f, "SK"))           return SINFO_BAND_K ;
02383     if (!strcmp(f, "L"))            return SINFO_BAND_L ;
02384     if (!strcmp(f, "SL"))           return SINFO_BAND_L ;
02385     if (!strcmp(f, "M"))            return SINFO_BAND_M ;
02386     if (!strcmp(f, "M_NB"))         return SINFO_BAND_M ;
02387     if (!strcmp(f, "NB_1.06"))      return SINFO_BAND_J ;
02388     if (!strcmp(f, "NB_1.08"))      return SINFO_BAND_J ;
02389     if (!strcmp(f, "NB_1.19"))      return SINFO_BAND_J ;
02390     if (!strcmp(f, "NB_1.21"))      return SINFO_BAND_J ;
02391     if (!strcmp(f, "NB_1.26"))      return SINFO_BAND_J ;
02392     if (!strcmp(f, "NB_1.28"))      return SINFO_BAND_J ;
02393     if (!strcmp(f, "NB_1.64"))      return SINFO_BAND_H ;
02394     if (!strcmp(f, "NB_1.71"))      return SINFO_BAND_H ;
02395     if (!strcmp(f, "NB_2.07"))      return SINFO_BAND_KS ;
02396     if (!strcmp(f, "NB_2.09"))      return SINFO_BAND_KS ;
02397     if (!strcmp(f, "NB_2.13"))      return SINFO_BAND_KS ;
02398     if (!strcmp(f, "NB_2.17"))      return SINFO_BAND_KS ;
02399     if (!strcmp(f, "NB_2.19"))      return SINFO_BAND_KS ;
02400     if (!strcmp(f, "NB_2.25"))      return SINFO_BAND_KS ;
02401     if (!strcmp(f, "NB_2.29"))      return SINFO_BAND_KS ;
02402     if (!strcmp(f, "NB_2.34"))      return SINFO_BAND_KS ;
02403     if (!strcmp(f, "NB_3.21"))      return SINFO_BAND_L ;
02404     if (!strcmp(f, "NB_3.28"))      return SINFO_BAND_L ;
02405     if (!strcmp(f, "NB_3.80"))      return SINFO_BAND_L ;
02406     if (!strcmp(f, "NB_4.07"))      return SINFO_BAND_L ;
02407     return SINFO_BAND_UNKNOWN ;
02408 }
02409 
02410 

Generated on 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1