hawki_utils.c

00001 /* $Id: hawki_utils.c,v 1.45 2010/02/23 10:57:38 cgarcia Exp $
00002  *
00003  * This file is part of the HAWKI Pipeline
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: cgarcia $
00023  * $Date: 2010/02/23 10:57:38 $
00024  * $Revision: 1.45 $
00025  * $Name: hawki-1_7_2 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <float.h>
00037 #include <string.h>
00038 #include <math.h>
00039 #include <cpl.h>
00040 
00041 #include "irplib_cat.h"  
00042 #include "irplib_wcs.h"  
00043 
00044 #include "hawki_utils.h"
00045 #include "hawki_pfits.h"
00046 #include "hawki_load.h"
00047 
00048 /*----------------------------------------------------------------------------*/
00052 /*----------------------------------------------------------------------------*/
00053 
00056 /*----------------------------------------------------------------------------*/
00064 /*----------------------------------------------------------------------------*/
00065 const char * hawki_get_license(void)
00066 {
00067     const char  *   hawki_license = 
00068         "This file is part of the HAWKI Instrument Pipeline\n"
00069         "Copyright (C) 2002,2003 European Southern Observatory\n"
00070         "\n"
00071         "This program is free software; you can redistribute it and/or modify\n"
00072         "it under the terms of the GNU General Public License as published by\n"
00073         "the Free Software Foundation; either version 2 of the License, or\n"
00074         "(at your option) any later version.\n"
00075         "\n"
00076         "This program is distributed in the hope that it will be useful,\n"
00077         "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00078         "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
00079         "GNU General Public License for more details.\n"
00080         "\n"
00081         "You should have received a copy of the GNU General Public License\n"
00082         "along with this program; if not, write to the Free Software\n"
00083         "Foundation, Inc., 59 Temple Place, Suite 330, Boston, \n"
00084         "MA  02111-1307  USA" ;
00085     return hawki_license ;
00086 }
00087 
00088 /*----------------------------------------------------------------------------*/
00092 /*----------------------------------------------------------------------------*/
00093 void hawki_print_banner(void)
00094 {
00095     cpl_msg_info(__func__, "*****************************************");
00096     cpl_msg_info(__func__, "Welcome to HAWK-I Pipeline release %s",
00097                  hawki_get_version());
00098     cpl_msg_info(__func__, "*****************************************");
00099 }
00100 
00101 /*----------------------------------------------------------------------------*/
00105 /*----------------------------------------------------------------------------*/
00106 const char * hawki_get_version(void)
00107 {
00108     static const char version[100] = PACKAGE_VERSION; //Defined in config.h
00109     return version;
00110 }
00111 
00112 /*----------------------------------------------------------------------------*/
00119 /*----------------------------------------------------------------------------*/
00120 cpl_image * hawki_compute_darkbpm(
00121         const cpl_image     *   in,
00122         double                  sigma)
00123 {
00124     double                  med, stdev, threshold ;
00125     cpl_image           *   bpm ;
00126     cpl_image           *   bpm_int ;
00127 
00128     /* Test entries */
00129     if (in == NULL) return NULL ;
00130     if (sigma <= 0) return NULL ;
00131 
00132     bpm = cpl_image_duplicate(in);
00133 
00134     /* Compute the threshold */
00135     med = cpl_image_get_median_dev(bpm, &stdev) ;
00136     threshold = med + sigma*stdev ;
00137     cpl_msg_info(__func__, "Threshold : %g = %g + %g * %g", 
00138             threshold, med, sigma, stdev) ;
00139 
00140     /* Compute the bpm */
00141     cpl_image_threshold(bpm, threshold, threshold, 0.0, 1.0) ;
00142     
00143     /* Convert */
00144     bpm_int = cpl_image_cast(bpm, CPL_TYPE_INT) ;
00145     cpl_image_delete(bpm) ;
00146     
00147     return bpm_int ;
00148 }
00149 
00150 /*----------------------------------------------------------------------------*/
00163 /*----------------------------------------------------------------------------*/
00164 cpl_image * hawki_compute_flatbpm
00165 (const cpl_image *   in,
00166  double              sigma,
00167  double              lowval,
00168  double              highval)
00169 {
00170     cpl_matrix          *   kernel ;
00171     cpl_image           *   filtered ;
00172     double                  med, stdev, threshold ;
00173     cpl_image           *   bpm_sigma;
00174     cpl_image           *   bpm_lowhigh;
00175     cpl_image           *   bpm;
00176     cpl_image           *   bpm_int ;
00177 
00178     /* Test entries */
00179     if (in == NULL) return NULL ;
00180     if (sigma <= 0) return NULL ;
00181 
00182     /* Filter the input image */
00183     kernel = cpl_matrix_new(3, 3) ;
00184     cpl_matrix_fill(kernel, 1.0) ;
00185     filtered = cpl_image_filter_median(in, kernel) ;
00186     cpl_matrix_delete(kernel) ;
00187 
00188     /* Remove the low freq signal */
00189     bpm_sigma = cpl_image_subtract_create(in, filtered) ;
00190     cpl_image_delete(filtered) ;
00191 
00192     /* Compute the threshold */
00193     med = cpl_image_get_median_dev(bpm_sigma, &stdev) ;
00194     threshold = med + sigma*stdev ;
00195     cpl_msg_info(__func__, "Threshold : %g = %g + %g * %g", 
00196             threshold, med, sigma, stdev) ;
00197 
00198     /* Compute the bpm with the sigma values */
00199     cpl_image_threshold(bpm_sigma, threshold, threshold, 0.0, 1.0) ;
00200     
00201     /* Count the pixels below and above the lowval and highval */
00202     bpm_lowhigh = cpl_image_duplicate(in);
00203     hawki_image_inverse_threshold(bpm_lowhigh, lowval, highval, 0.0, 1.0);
00204     
00205     /* Merge both masks */
00206     bpm = cpl_image_add_create(bpm_sigma, bpm_lowhigh);
00207     cpl_image_threshold(bpm, 0.0, 1.0, 0.0, 1.0);
00208     
00209     /* Convert */
00210     bpm_int = cpl_image_cast(bpm, CPL_TYPE_INT) ;
00211     cpl_image_delete(bpm) ;
00212     cpl_image_delete(bpm_sigma);
00213     cpl_image_delete(bpm_lowhigh);
00214     
00215     return bpm_int ;
00216 }
00217 
00218 /*----------------------------------------------------------------------------*/
00237 /*----------------------------------------------------------------------------*/
00238 cpl_error_code hawki_image_inverse_threshold
00239 (cpl_image *    image_in,
00240  double         lo_valid,
00241  double         hi_valid,
00242  double         assign_in_range,
00243  double         assign_out_range)
00244 {
00245     int   i;
00246     int   npix;
00247 
00248     cpl_ensure_code(image_in != NULL, CPL_ERROR_NULL_INPUT);
00249     cpl_ensure_code(lo_valid <= hi_valid, CPL_ERROR_ILLEGAL_INPUT);
00250 
00251     /* Get number of pixels of image */
00252     npix = cpl_image_get_size_x(image_in) *  cpl_image_get_size_y(image_in);
00253     
00254     /* Switch on image type */
00255     switch (cpl_image_get_type(image_in)) 
00256     {
00257         case CPL_TYPE_DOUBLE: {
00258             double * pdi = cpl_image_get_data_double(image_in);
00259             for (i=0; i<npix; i++) {
00260                 if ((pdi[i]>lo_valid) && (pdi[i]<hi_valid))
00261                     pdi[i] = (double)assign_in_range;
00262                 else
00263                     pdi[i] = (double)assign_out_range;
00264             }
00265             break;
00266         }
00267         case CPL_TYPE_FLOAT: {
00268             float * pdi = cpl_image_get_data_float(image_in);
00269             for (i=0; i<npix; i++) {
00270                 if ((pdi[i]>lo_valid) && (pdi[i]<hi_valid))
00271                     pdi[i] = (float)assign_in_range;
00272                 else
00273                     pdi[i] = (float)assign_out_range;
00274             }
00275             break;
00276         }
00277         case CPL_TYPE_INT: {
00278             int * pdi = cpl_image_get_data_int(image_in);
00279             for (i=0; i<npix; i++) {
00280                 if (((double)pdi[i]>lo_valid) && ((double)pdi[i]<hi_valid))
00281                     pdi[i] = (int)assign_in_range;
00282                 else
00283                     pdi[i] = (int)assign_out_range;
00284             }
00285             break;
00286         }
00287         default:
00288           cpl_ensure_code(0, CPL_ERROR_INVALID_TYPE);
00289     }
00290     return CPL_ERROR_NONE;
00291 }
00292 
00293 /*----------------------------------------------------------------------------*/
00301 /*----------------------------------------------------------------------------*/
00302 cpl_image * hawki_images_stitch
00303 (cpl_image   ** ima,
00304  double      *  x,
00305  double      *  y)
00306 {
00307     int                     lx, ly ;
00308     cpl_image           *   ima_ext[HAWKI_NB_DETECTORS] ;
00309     cpl_imagelist       *   in ;
00310     cpl_bivector        *   offsets ;
00311     double              *   offsets_x ;
00312     double              *   offsets_y ;
00313     cpl_image           **  combined ;
00314     cpl_image           *   stitched ;
00315     int                     i ;
00316 
00317     /* Test entries */
00318     if (ima == NULL) return NULL ;
00319     if (x   == NULL) return NULL ;
00320     if (y   == NULL) return NULL ;
00321 
00322     /* Take the smallest size */
00323     lx = cpl_image_get_size_x(ima[0]) ;
00324     ly = cpl_image_get_size_y(ima[0]) ;
00325     for (i=1 ; i<HAWKI_NB_DETECTORS ; i++) {
00326         if (lx > cpl_image_get_size_x(ima[i]))
00327             lx = cpl_image_get_size_x(ima[i]) ;
00328         if (ly > cpl_image_get_size_y(ima[i]))
00329             ly = cpl_image_get_size_y(ima[i]) ;
00330     }
00331 
00332     /* Create the image list */
00333     in = cpl_imagelist_new() ;
00334     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
00335         ima_ext[i] = cpl_image_extract(ima[i], 1, 1, lx, ly) ;
00336         cpl_imagelist_set(in, ima_ext[i], i) ;
00337     }
00338 
00339     /* Create the offsets */
00340     offsets = cpl_bivector_new(HAWKI_NB_DETECTORS) ;
00341     offsets_x = cpl_bivector_get_x_data(offsets) ;
00342     offsets_y = cpl_bivector_get_y_data(offsets) ;
00343     offsets_x[0] = HAWKI_DET1_POSX ;
00344     offsets_y[0] = HAWKI_DET1_POSY ;
00345     offsets_x[1] = x[0] - x[1] + HAWKI_DET2_POSX ;
00346     offsets_y[1] = y[0] - y[1] + HAWKI_DET2_POSY ;
00347     offsets_x[2] = x[0] - x[2] + HAWKI_DET3_POSX ;
00348     offsets_y[2] = y[0] - y[2] + HAWKI_DET3_POSY ;
00349     offsets_x[3] = x[0] - x[3] + HAWKI_DET4_POSX ;
00350     offsets_y[3] = y[0] - y[3] + HAWKI_DET4_POSY ;
00351 
00352     /* Recombine the images */
00353     if ((combined = cpl_geom_img_offset_saa(in, offsets,
00354             CPL_KERNEL_DEFAULT, 0, 0, CPL_GEOM_UNION, NULL, NULL)) == NULL) 
00355     {
00356         cpl_msg_error(__func__, "Cannot recombine the images") ;
00357         cpl_bivector_delete(offsets) ;
00358         cpl_imagelist_delete(in) ;
00359         return NULL ;
00360     }
00361     cpl_bivector_delete(offsets) ;
00362     cpl_imagelist_delete(in) ;
00363 
00364     /* Return  */
00365     stitched = combined[0] ;
00366     cpl_image_delete(combined[1]) ;
00367     cpl_free(combined) ;
00368     return stitched ;
00369 }
00370 
00371 /*----------------------------------------------------------------------------*/
00381 /*----------------------------------------------------------------------------*/
00382 int hawki_apply_harmonization(
00383         cpl_imagelist   *   in,
00384         double              h1,
00385         double              h2,
00386         double              h3,
00387         double              h4)
00388 {
00389     /* Test entries */
00390     if (in == NULL) return -1 ;
00391 
00392     cpl_image_multiply_scalar((cpl_image *)cpl_imagelist_get(in, 0), h1) ;
00393     cpl_image_multiply_scalar((cpl_image *)cpl_imagelist_get(in, 1), h2) ;
00394     cpl_image_multiply_scalar((cpl_image *)cpl_imagelist_get(in, 2), h3) ;
00395     cpl_image_multiply_scalar((cpl_image *)cpl_imagelist_get(in, 3), h4) ;
00396 
00397     return 0 ;
00398 }
00399 
00400 /*----------------------------------------------------------------------------*/
00419 /*----------------------------------------------------------------------------*/
00420 int hawki_compute_harmonization(
00421         const cpl_imagelist *   in,
00422         double              *   h1,
00423         double              *   h2,
00424         double              *   h3,
00425         double              *   h4,
00426         double              *   h)
00427 {
00428     int                 width = 64 ;
00429     int                 nx, ny ;
00430     const cpl_image *   ima ;
00431     double              avg1, avg2, avg3, avg4 ;
00432     double              val1, val2 ;
00433     int                 llx, lly, urx, ury ;
00434 
00435     /* Test entries */
00436     if (in == NULL) return -1 ;
00437     if (h1==NULL || h2==NULL || h3==NULL || h4==NULL || h==NULL) return -1 ;
00438 
00439     /* Compute the avg1 */
00440     ima = cpl_imagelist_get_const(in, 0) ;
00441     nx = cpl_image_get_size_x(ima) ;
00442     ny = cpl_image_get_size_y(ima) ;
00443     llx = 1 ; lly = ny - width + 1 ; urx = nx ; ury = ny ;
00444     val1 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ;
00445     if (cpl_error_get_code()) {
00446         cpl_msg_error(__func__, "Cannot get statistics from chip 1") ;
00447         return -1 ;
00448     }
00449     llx = nx - width + 1 ; lly = 1 ; urx = nx ; ury = ny ;
00450     val2 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ;
00451     if (cpl_error_get_code()) {
00452         cpl_msg_error(__func__, "Cannot get statistics from chip 1") ;
00453         return -1 ;
00454     }
00455     avg1 = (val1 + val2) / 2.0 ;
00456 
00457     /* Compute the avg2 */
00458     ima = cpl_imagelist_get_const(in, 1) ;
00459     nx = cpl_image_get_size_x(ima) ;
00460     ny = cpl_image_get_size_y(ima) ;
00461     llx = 1 ; lly = 1 ; urx = width ; ury = ny ;
00462     val1 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ;
00463     if (cpl_error_get_code()) {
00464         cpl_msg_error(__func__, "Cannot get statistics from chip 2") ;
00465         return -1 ;
00466     }
00467     llx = 1 ; lly = ny - width + 1 ; urx = nx ; ury = ny ;
00468     val2 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ;
00469     if (cpl_error_get_code()) {
00470         cpl_msg_error(__func__, "Cannot get statistics from chip 2") ;
00471         return -1 ;
00472     }
00473     avg2 = (val1 + val2) / 2.0 ;
00474 
00475     /* Compute the avg3 */
00476     ima = cpl_imagelist_get_const(in, 2) ;
00477     nx = cpl_image_get_size_x(ima) ;
00478     ny = cpl_image_get_size_y(ima) ;
00479     llx = 1 ; lly = 1 ; urx = nx ; ury = width ;
00480     val1 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ;
00481     if (cpl_error_get_code()) {
00482         cpl_msg_error(__func__, "Cannot get statistics from chip 3") ;
00483         return -1 ;
00484     }
00485     llx = nx - width + 1 ; lly = 1 ; urx = nx ; ury = ny ;
00486     val2 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ;
00487     if (cpl_error_get_code()) {
00488         cpl_msg_error(__func__, "Cannot get statistics from chip 3") ;
00489         return -1 ;
00490     }
00491     avg3 = (val1 + val2) / 2.0 ;
00492 
00493     /* Compute the avg4 */
00494     ima = cpl_imagelist_get_const(in, 3) ;
00495     nx = cpl_image_get_size_x(ima) ;
00496     ny = cpl_image_get_size_y(ima) ;
00497     llx = 1 ; lly = 1 ; urx = width ; ury = ny ;
00498     val1 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ;
00499     if (cpl_error_get_code()) {
00500         cpl_msg_error(__func__, "Cannot get statistics from chip 4") ;
00501         return -1 ;
00502     }
00503     llx = 1 ; lly = 1 ; urx = nx ; ury = width ;
00504     val2 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ;
00505     if (cpl_error_get_code()) {
00506         cpl_msg_error(__func__, "Cannot get statistics from chip 4") ;
00507         return -1 ;
00508     }
00509     avg4 = (val1 + val2) / 2.0 ;
00510 
00511     /* Compute h */
00512     *h = (avg1 + avg2 + avg3 + avg4) / 4.0 ;
00513 
00514     *h1 = *h / avg1 ;
00515     *h2 = *h / avg2 ;
00516     *h3 = *h / avg3 ;
00517     *h4 = *h / avg4 ;
00518 
00519     return 0 ;
00520 }
00521 
00522 /*----------------------------------------------------------------------------*/
00528 /*----------------------------------------------------------------------------*/
00529 cpl_image * hawki_compute_lsbg(const cpl_image * in)
00530 {
00531     cpl_image       *   out ;
00532     cpl_image       *   tmp ;
00533     cpl_image       *   filtered ;
00534     cpl_image       *   subsampled ;
00535     cpl_matrix      *   kernel ;
00536     int                 nscales ;
00537     cpl_polynomial  *   poly ;
00538     cpl_bivector    *   xy_pos ;
00539     cpl_vector      *   vals ;
00540     int                 quad_sz, nbpoints, lx, ly, nx, ny ;
00541     double          *   pxy_pos_x ;
00542     double          *   pxy_pos_y ;
00543     double          *   pvals ;
00544     float           *   pima ;
00545     int                 i, j ;
00546 
00547     /* Check entries */
00548     if (in == NULL) return NULL ;
00549     nx = cpl_image_get_size_x(in) ;
00550     ny = cpl_image_get_size_y(in) ;
00551     
00552     /* Initialise */
00553     nscales = 7 ;
00554     tmp = (cpl_image *)in ;
00555     subsampled = NULL ;
00556     
00557     /* Check entries */
00558     quad_sz = pow(2, (double)nscales) ;
00559     lx = nx / quad_sz ;
00560     ly = ny / quad_sz ;
00561     nbpoints = lx * ly ;
00562     if (quad_sz >= nx || quad_sz >= ny) return NULL ;
00563 
00564     /* Create filter kernel */
00565     kernel = cpl_matrix_new(3, 3) ;
00566     cpl_matrix_fill(kernel, 1.0) ; 
00567 
00568     /* Loop nscales times */
00569     for (i=0 ; i<nscales ; i++) {
00570 
00571         /* Filter the image */
00572         filtered = cpl_image_filter_median(tmp, kernel) ;
00573         if (i>0) cpl_image_delete(tmp) ;
00574 
00575         /* Subsample the image */
00576         subsampled = cpl_image_extract_subsample(filtered, 2, 2) ;
00577         cpl_image_delete(filtered) ;
00578     
00579         tmp = subsampled ;
00580     }
00581     cpl_matrix_delete(kernel) ;
00582 
00583     /* Check nbpoints */
00584     if (nbpoints != 
00585             cpl_image_get_size_x(subsampled)*cpl_image_get_size_y(subsampled)) {
00586         cpl_msg_error(__func__, "Invalid size") ;
00587         cpl_image_delete(subsampled) ;
00588         return NULL ;
00589     }
00590     
00591     /* Create anchor points for the fit */
00592     xy_pos = cpl_bivector_new(nbpoints) ;
00593     vals = cpl_vector_new(nbpoints) ;
00594     pxy_pos_x = cpl_bivector_get_x_data(xy_pos) ;
00595     pxy_pos_y = cpl_bivector_get_y_data(xy_pos) ;
00596     pvals = cpl_vector_get_data(vals) ;
00597     pima = cpl_image_get_data_float(subsampled) ;
00598     for (j=0 ; j<ly ; j++) {
00599         for (i=0 ; i<lx ; i++) {
00600             pxy_pos_x[i+j*lx] = i * quad_sz + quad_sz/2 ;
00601             pxy_pos_y[i+j*lx] = j * quad_sz + quad_sz/2 ;
00602             pvals[i+j*lx] = (double)pima[i+j*lx];
00603         }
00604     }
00605     cpl_image_delete(subsampled) ;
00606 
00607     /* Fit the polynomial */
00608     if ((poly = cpl_polynomial_fit_2d_create(xy_pos, vals, 3, NULL)) == NULL) {
00609         cpl_msg_error(__func__, "Cannot fit the polynomial") ;
00610         cpl_bivector_delete(xy_pos) ;
00611         cpl_vector_delete(vals) ;
00612         return NULL ;
00613     }
00614     cpl_bivector_delete(xy_pos) ;
00615     cpl_vector_delete(vals) ;
00616 
00617     /* Regenerate the big bgd image */
00618     out = cpl_image_duplicate(in) ;
00619     cpl_image_fill_polynomial(out, poly, 1.0, 1.0, 1.0, 1.0) ;
00620     cpl_polynomial_delete(poly) ;
00621     
00622     return out ;
00623 }
00624 
00625 /*----------------------------------------------------------------------------*/
00632 /*----------------------------------------------------------------------------*/
00633 const char * hawki_extract_first_filename(
00634         const cpl_frameset  *   in,
00635         const char          *   tag)
00636 {
00637     const cpl_frame     *   cur_frame ;
00638 
00639     /* Get the frame  */
00640     if ((cur_frame = cpl_frameset_find_const(in, tag)) == NULL) return NULL ;
00641     return cpl_frame_get_filename(cur_frame) ;
00642 }
00643 
00644 /*----------------------------------------------------------------------------*/
00650 /*----------------------------------------------------------------------------*/
00651 hawki_band hawki_get_band(const char * f)
00652 {
00653     if (!strcmp(f, "J"))            return HAWKI_BAND_J ;
00654     if (!strcmp(f, "H"))            return HAWKI_BAND_H ;
00655     if (!strcmp(f, "K"))            return HAWKI_BAND_K ;
00656     if (!strcmp(f, "Ks"))           return HAWKI_BAND_K ;
00657     if (!strcmp(f, "Y"))            return HAWKI_BAND_Y ;
00658     return HAWKI_BAND_UNKNOWN ;
00659 }
00660 
00661 /*-------------------------------------------------------------------------*/
00667 /*--------------------------------------------------------------------------*/
00668 const char * hawki_std_band_name(hawki_band band)
00669 {
00670     switch (band) {
00671         case HAWKI_BAND_J:        return "J" ;
00672         case HAWKI_BAND_H:        return "H" ;
00673         case HAWKI_BAND_K:        return "K" ;
00674         case HAWKI_BAND_Y:        return "Y" ;
00675         default:            return "Unknown" ;
00676     } 
00677 }
00678 
00679 /*----------------------------------------------------------------------------*/
00688 /*----------------------------------------------------------------------------*/
00689 cpl_bivector * hawki_get_header_tel_offsets(const cpl_frameset * fset)
00690 {
00691     cpl_bivector        *   offsets ;
00692     double              *   offsets_x ;
00693     double              *   offsets_y ;
00694     const cpl_frame     *   frame ;
00695     cpl_propertylist    *   plist ;
00696     int                     nfiles ;
00697     int                     i ;
00698     cpl_errorstate          error_prevstate = cpl_errorstate_get();
00699     
00700 
00701     /* Test entries */
00702     if (fset == NULL) return NULL ;
00703 
00704     /* Create the offsets bi vector */
00705     nfiles = cpl_frameset_get_size(fset) ;
00706     offsets = cpl_bivector_new(nfiles) ;
00707     offsets_x = cpl_bivector_get_x_data(offsets) ;
00708     offsets_y = cpl_bivector_get_y_data(offsets) ;
00709     for (i=0 ; i<nfiles ; i++) {
00710 
00711         /* X and Y offsets */
00712         frame = cpl_frameset_get_frame_const(fset, i) ;
00713         plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0);
00714         offsets_x[i] = hawki_pfits_get_cumoffsetx(plist) ;
00715         offsets_y[i] = hawki_pfits_get_cumoffsety(plist) ;
00716         cpl_propertylist_delete(plist) ;
00717         if(!cpl_errorstate_is_equal(error_prevstate ))
00718         {
00719             cpl_msg_error(__func__, "Cannot get offsets from header") ;
00720             cpl_bivector_delete(offsets) ;
00721             return NULL ;
00722         }
00723     }
00724     return offsets ;
00725 }
00726 
00727 /*----------------------------------------------------------------------------*/
00733 /*----------------------------------------------------------------------------*/
00734 double hawki_get_mean_airmass(cpl_frameset *   set)
00735 {
00736     int                     nframes;
00737     cpl_frame           *   cur_frame;
00738     cpl_propertylist    *   plist;
00739     int                     iframe;
00740     double                  mean_airmass = 0.0;
00741 
00742     /* Test inputs  */
00743     if (set == NULL) return -1;
00744 
00745     /* Initialize */
00746     nframes = cpl_frameset_get_size(set);
00747 
00748     for (iframe=0 ; iframe<nframes ; iframe++) 
00749     {
00750         cur_frame = cpl_frameset_get_frame(set, iframe);
00751         plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
00752         mean_airmass +=   hawki_pfits_get_airmass_start(plist) +
00753                           hawki_pfits_get_airmass_end(plist); 
00754         cpl_propertylist_delete(plist);
00755     }
00756     mean_airmass /= 2. * nframes;
00757 
00758     /* Free and return */
00759     return mean_airmass;
00760 }
00761 
00762 /*----------------------------------------------------------------------------*/
00774 /*----------------------------------------------------------------------------*/
00775 int * hawki_detectors_labelise
00776 (const cpl_frameset * in)
00777 {
00778     int             *   labels ;
00779     cpl_bivector    *   offsets ;
00780     int                 nframes ;
00781     double          *   poff_x ;
00782     double          *   poff_y ;
00783     double              off_x_mean;
00784     double              off_y_mean;
00785     int                 i ;
00786 
00787     /* Check entries */
00788     if (in == NULL) return NULL ;
00789 
00790     /* Initialise */
00791     nframes = cpl_frameset_get_size(in) ;
00792 
00793     /* Get the offsets */
00794     if ((offsets = hawki_get_header_tel_offsets((cpl_frameset *)in)) == NULL) {
00795         cpl_msg_error(__func__, "Cannot read the offsets") ;
00796         return NULL ;
00797     }
00798     poff_x = cpl_bivector_get_x_data(offsets) ;
00799     poff_y = cpl_bivector_get_y_data(offsets) ;
00800 
00801     /* Get the mean offsets */
00802     off_x_mean = cpl_vector_get_mean(cpl_bivector_get_x(offsets));
00803     off_y_mean = cpl_vector_get_mean(cpl_bivector_get_y(offsets));
00804     
00805     /* Allocate labels */
00806     labels = cpl_malloc(nframes * sizeof(int)) ;
00807     for (i=0 ; i<nframes ; i++) {
00808         if (poff_x[i] - off_x_mean <= 0 && poff_y[i] - off_y_mean <= 0)
00809             labels[i] = 1 ;
00810         else if (poff_x[i] - off_x_mean >= 0 && poff_y[i] - off_y_mean <= 0) 
00811             labels[i] = 2 ;
00812         else if (poff_x[i] - off_x_mean >= 0 && poff_y[i] - off_y_mean >= 0) 
00813             labels[i] = 3 ;
00814         else if (poff_x[i] - off_x_mean <= 0 && poff_y[i] - off_y_mean >= 0) 
00815             labels[i] = 4 ;
00816         else labels[i] = 0 ;
00817     }
00818     cpl_bivector_delete(offsets) ;
00819     return labels ;
00820 }
00821 
00822 /*----------------------------------------------------------------------------*/
00829 /*----------------------------------------------------------------------------*/
00830 int hawki_detectors_locate_star
00831 (const cpl_frameset * in,
00832  double               star_ra,
00833  double               star_dec,
00834  int                * labels)
00835 {
00836     int     nframes;
00837     int     idet, iframe;
00838 
00839     /* Check entries */
00840     if (in == NULL) return -1;
00841 
00842     /* Initialise */
00843     nframes = cpl_frameset_get_size(in) ;
00844 
00845     /* Allocate labels */
00846     for (iframe=0 ; iframe<nframes ; iframe++) 
00847     {
00848         const char * filename;
00849         filename = cpl_frame_get_filename
00850             (cpl_frameset_get_frame_const(in, iframe));
00851         
00852         for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00853         {
00854             cpl_propertylist * main_header;
00855             cpl_propertylist * ext_header;
00856             cpl_wcs          * wcs;
00857             double             naxis1, naxis2;
00858             double             star_x, star_y;
00859         
00860             /* Get the headers */
00861             main_header = cpl_propertylist_load(filename, 0);
00862             ext_header  = cpl_propertylist_load
00863                 (filename, hawki_get_ext_from_detector(filename,idet + 1));
00864             
00865             /* Get the position of the star in pixels */
00866             wcs = cpl_wcs_new_from_propertylist(ext_header);
00867             if(wcs == NULL)
00868             {
00869                 cpl_msg_error(__func__, "Could not get WCS info");
00870                 return -1;
00871             }
00872             if(irplib_wcs_radectoxy(wcs, star_ra, star_dec, &star_x, &star_y)
00873                     != CPL_ERROR_NONE)
00874             {
00875                 cpl_errorstate_set(CPL_ERROR_NONE);
00876             }
00877             
00878             /* Check for the limits */
00879             naxis1 = (double)hawki_pfits_get_naxis1(ext_header);
00880             naxis2 = (double)hawki_pfits_get_naxis2(ext_header);
00881             if(star_x > 0 && star_x < naxis1 && star_y > 0 && star_y < naxis2)
00882             {
00883                 labels[iframe] = idet + 1;
00884             }
00885             
00886             /* Free */
00887             cpl_propertylist_delete(ext_header);
00888             cpl_propertylist_delete(main_header);
00889             cpl_wcs_delete(wcs);
00890         }
00891         if(labels[iframe] == 0)
00892         {
00893             cpl_msg_error(__func__,"Frame %d does not contain the star in any "
00894                           "detector", iframe + 1);
00895         }
00896     }
00897     return 0;
00898 }
00899 
00900 /*----------------------------------------------------------------------------*/
00908 /*----------------------------------------------------------------------------*/
00909 double hawki_vector_get_max_select
00910 (const cpl_vector * self, const cpl_vector * valid)
00911 {
00912     double max_val = DBL_MIN;
00913     int    initialized = 0;
00914     int    ival;
00915     int    nvals;
00916     
00917     nvals = cpl_vector_get_size(self);
00918     for(ival = 0; ival < nvals; ++ival)
00919     {
00920         if(cpl_vector_get(valid, ival) >= -0.5) 
00921         {
00922             if(!initialized)
00923             {
00924                 max_val = cpl_vector_get(self, ival);
00925                 initialized = 1;
00926             }
00927             if(cpl_vector_get(self, ival) > max_val)
00928                 max_val = cpl_vector_get(self, ival);
00929         }
00930     }
00931     return max_val;
00932 }
00933 
00934 /*----------------------------------------------------------------------------*/
00942 /*----------------------------------------------------------------------------*/
00943 double hawki_vector_get_min_select
00944 (const cpl_vector * self, const cpl_vector * valid)
00945 {
00946     double min_val = DBL_MAX;
00947     int    initialized = 0;
00948     int    ival;
00949     int    nvals;
00950     
00951     nvals = cpl_vector_get_size(self);
00952     for(ival = 0; ival < nvals; ++ival)
00953     {
00954         if(cpl_vector_get(valid, ival) >= -0.5) 
00955         {
00956             if(!initialized)
00957             {
00958                 min_val = cpl_vector_get(self, ival);
00959                 initialized = 1;
00960             }
00961             if(cpl_vector_get(self, ival) < min_val)
00962                 min_val = cpl_vector_get(self, ival);
00963         }
00964     }
00965     return min_val;
00966 }
00967 
00968 /*----------------------------------------------------------------------------*/
00974 /*----------------------------------------------------------------------------*/
00975 double hawki_vector_get_mode(cpl_vector * vec)
00976 {
00977     int                 nb ;
00978     int                 nbins ;
00979     double              min, max ;
00980     double              bin_size ;
00981     cpl_bivector    *   hist ;
00982     cpl_vector      *   hist_x ;
00983     cpl_vector      *   hist_y ;
00984     double              cur_val ;
00985     int                 cur_bin ;
00986     double              max_val ;
00987     int                 max_bin ;
00988     double              mode ;
00989     int                 i ;
00990 
00991     /* Test entries  */
00992     if (vec == NULL) return -1.0 ;
00993     
00994     /* Initialise */
00995     nb = cpl_vector_get_size(vec) ;
00996 
00997     /* Create the histogram */
00998     nbins = 10 ;
00999     min = cpl_vector_get_min(vec) ;
01000     max = cpl_vector_get_max(vec) ;
01001     bin_size = (max-min)/nbins ;
01002     hist = cpl_bivector_new(nbins) ;
01003     hist_x = cpl_bivector_get_x(hist) ;
01004     hist_y = cpl_bivector_get_y(hist) ;
01005     cpl_vector_fill(hist_x, 0.0) ;
01006     cpl_vector_fill(hist_y, 0.0) ;
01007     for (i=0 ; i<nbins ; i++) {
01008         cpl_vector_set(hist_x, i, min + i * bin_size) ;
01009     }
01010     for (i=0 ; i<nb ; i++) {
01011         cur_val = cpl_vector_get(vec, i) ;
01012         cur_bin = (int)((cur_val - min) / bin_size) ;
01013         if (cur_bin >= nbins) cur_bin -= 1.0 ;
01014         cur_val = cpl_vector_get(hist_y, cur_bin) ;
01015         cur_val += 1.0 ;
01016         cpl_vector_set(hist_y, cur_bin, cur_val) ;
01017     }
01018     
01019     /* Get the mode of the histogram */
01020     max_val = cpl_vector_get(hist_y, 0) ;
01021     max_bin = 0 ;
01022     for (i=0 ; i<nbins ; i++) {
01023         cur_val = cpl_vector_get(hist_y, i) ;
01024         if (cur_val > max_val) {
01025             max_val = cur_val ;
01026             max_bin = i ;
01027         }
01028     }
01029     mode = cpl_vector_get(hist_x, max_bin) ;
01030     cpl_bivector_delete(hist) ;
01031     return mode ;
01032 }
01033  

Generated on 10 Jun 2010 for HAWKI Pipeline Reference Manual by  doxygen 1.6.1