|
HAWKI Pipeline Reference Manual 1.8.2
|
00001 /* $Id: hawki_utils.c,v 1.48 2011/02/17 12:44:30 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: 2011/02/17 12:44:30 $ 00024 * $Revision: 1.48 $ 00025 * $Name: hawki-1_8_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_mask * 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_mask_new(3, 3) ; 00184 cpl_mask_not(kernel) ; 00185 filtered = cpl_image_new(cpl_image_get_size_x(in), cpl_image_get_size_y(in), 00186 cpl_image_get_type(in)); 00187 cpl_image_filter_mask(filtered, in, kernel, CPL_FILTER_MEDIAN, 00188 CPL_BORDER_FILTER); 00189 cpl_mask_delete(kernel) ; 00190 00191 /* Remove the low freq signal */ 00192 bpm_sigma = cpl_image_subtract_create(in, filtered) ; 00193 cpl_image_delete(filtered) ; 00194 00195 /* Compute the threshold */ 00196 med = cpl_image_get_median_dev(bpm_sigma, &stdev) ; 00197 threshold = med + sigma*stdev ; 00198 cpl_msg_info(__func__, "Threshold : %g = %g + %g * %g", 00199 threshold, med, sigma, stdev) ; 00200 00201 /* Compute the bpm with the sigma values */ 00202 cpl_image_threshold(bpm_sigma, threshold, threshold, 0.0, 1.0) ; 00203 00204 /* Count the pixels below and above the lowval and highval */ 00205 bpm_lowhigh = cpl_image_duplicate(in); 00206 hawki_image_inverse_threshold(bpm_lowhigh, lowval, highval, 0.0, 1.0); 00207 00208 /* Merge both masks */ 00209 bpm = cpl_image_add_create(bpm_sigma, bpm_lowhigh); 00210 cpl_image_threshold(bpm, 0.0, 1.0, 0.0, 1.0); 00211 00212 /* Convert */ 00213 bpm_int = cpl_image_cast(bpm, CPL_TYPE_INT) ; 00214 cpl_image_delete(bpm) ; 00215 cpl_image_delete(bpm_sigma); 00216 cpl_image_delete(bpm_lowhigh); 00217 00218 return bpm_int ; 00219 } 00220 00221 /*----------------------------------------------------------------------------*/ 00240 /*----------------------------------------------------------------------------*/ 00241 cpl_error_code hawki_image_inverse_threshold 00242 (cpl_image * image_in, 00243 double lo_valid, 00244 double hi_valid, 00245 double assign_in_range, 00246 double assign_out_range) 00247 { 00248 int i; 00249 int npix; 00250 00251 cpl_ensure_code(image_in != NULL, CPL_ERROR_NULL_INPUT); 00252 cpl_ensure_code(lo_valid <= hi_valid, CPL_ERROR_ILLEGAL_INPUT); 00253 00254 /* Get number of pixels of image */ 00255 npix = cpl_image_get_size_x(image_in) * cpl_image_get_size_y(image_in); 00256 00257 /* Switch on image type */ 00258 switch (cpl_image_get_type(image_in)) 00259 { 00260 case CPL_TYPE_DOUBLE: { 00261 double * pdi = cpl_image_get_data_double(image_in); 00262 for (i=0; i<npix; i++) { 00263 if ((pdi[i]>lo_valid) && (pdi[i]<hi_valid)) 00264 pdi[i] = (double)assign_in_range; 00265 else 00266 pdi[i] = (double)assign_out_range; 00267 } 00268 break; 00269 } 00270 case CPL_TYPE_FLOAT: { 00271 float * pdi = cpl_image_get_data_float(image_in); 00272 for (i=0; i<npix; i++) { 00273 if ((pdi[i]>lo_valid) && (pdi[i]<hi_valid)) 00274 pdi[i] = (float)assign_in_range; 00275 else 00276 pdi[i] = (float)assign_out_range; 00277 } 00278 break; 00279 } 00280 case CPL_TYPE_INT: { 00281 int * pdi = cpl_image_get_data_int(image_in); 00282 for (i=0; i<npix; i++) { 00283 if (((double)pdi[i]>lo_valid) && ((double)pdi[i]<hi_valid)) 00284 pdi[i] = (int)assign_in_range; 00285 else 00286 pdi[i] = (int)assign_out_range; 00287 } 00288 break; 00289 } 00290 default: 00291 cpl_ensure_code(0, CPL_ERROR_INVALID_TYPE); 00292 } 00293 return CPL_ERROR_NONE; 00294 } 00295 00296 /*----------------------------------------------------------------------------*/ 00304 /*----------------------------------------------------------------------------*/ 00305 cpl_image * hawki_images_stitch 00306 (cpl_image ** ima, 00307 double * x, 00308 double * y) 00309 { 00310 int lx, ly ; 00311 cpl_image * ima_ext[HAWKI_NB_DETECTORS] ; 00312 cpl_imagelist * in ; 00313 cpl_bivector * offsets ; 00314 double * offsets_x ; 00315 double * offsets_y ; 00316 cpl_image ** combined ; 00317 cpl_image * stitched ; 00318 int i ; 00319 00320 /* Test entries */ 00321 if (ima == NULL) return NULL ; 00322 if (x == NULL) return NULL ; 00323 if (y == NULL) return NULL ; 00324 00325 /* Take the smallest size */ 00326 lx = cpl_image_get_size_x(ima[0]) ; 00327 ly = cpl_image_get_size_y(ima[0]) ; 00328 for (i=1 ; i<HAWKI_NB_DETECTORS ; i++) { 00329 if (lx > cpl_image_get_size_x(ima[i])) 00330 lx = cpl_image_get_size_x(ima[i]) ; 00331 if (ly > cpl_image_get_size_y(ima[i])) 00332 ly = cpl_image_get_size_y(ima[i]) ; 00333 } 00334 00335 /* Create the image list */ 00336 in = cpl_imagelist_new() ; 00337 for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) { 00338 ima_ext[i] = cpl_image_extract(ima[i], 1, 1, lx, ly) ; 00339 cpl_imagelist_set(in, ima_ext[i], i) ; 00340 } 00341 00342 /* Create the offsets */ 00343 offsets = cpl_bivector_new(HAWKI_NB_DETECTORS) ; 00344 offsets_x = cpl_bivector_get_x_data(offsets) ; 00345 offsets_y = cpl_bivector_get_y_data(offsets) ; 00346 offsets_x[0] = HAWKI_DET1_POSX ; 00347 offsets_y[0] = HAWKI_DET1_POSY ; 00348 offsets_x[1] = x[0] - x[1] + HAWKI_DET2_POSX ; 00349 offsets_y[1] = y[0] - y[1] + HAWKI_DET2_POSY ; 00350 offsets_x[2] = x[0] - x[2] + HAWKI_DET3_POSX ; 00351 offsets_y[2] = y[0] - y[2] + HAWKI_DET3_POSY ; 00352 offsets_x[3] = x[0] - x[3] + HAWKI_DET4_POSX ; 00353 offsets_y[3] = y[0] - y[3] + HAWKI_DET4_POSY ; 00354 00355 /* Recombine the images */ 00356 if ((combined = cpl_geom_img_offset_saa(in, offsets, 00357 CPL_KERNEL_DEFAULT, 0, 0, CPL_GEOM_UNION, NULL, NULL)) == NULL) 00358 { 00359 cpl_msg_error(__func__, "Cannot recombine the images") ; 00360 cpl_bivector_delete(offsets) ; 00361 cpl_imagelist_delete(in) ; 00362 return NULL ; 00363 } 00364 cpl_bivector_delete(offsets) ; 00365 cpl_imagelist_delete(in) ; 00366 00367 /* Return */ 00368 stitched = combined[0] ; 00369 cpl_image_delete(combined[1]) ; 00370 cpl_free(combined) ; 00371 return stitched ; 00372 } 00373 00374 /*----------------------------------------------------------------------------*/ 00384 /*----------------------------------------------------------------------------*/ 00385 int hawki_apply_harmonization( 00386 cpl_imagelist * in, 00387 double h1, 00388 double h2, 00389 double h3, 00390 double h4) 00391 { 00392 /* Test entries */ 00393 if (in == NULL) return -1 ; 00394 00395 cpl_image_multiply_scalar((cpl_image *)cpl_imagelist_get(in, 0), h1) ; 00396 cpl_image_multiply_scalar((cpl_image *)cpl_imagelist_get(in, 1), h2) ; 00397 cpl_image_multiply_scalar((cpl_image *)cpl_imagelist_get(in, 2), h3) ; 00398 cpl_image_multiply_scalar((cpl_image *)cpl_imagelist_get(in, 3), h4) ; 00399 00400 return 0 ; 00401 } 00402 00403 /*----------------------------------------------------------------------------*/ 00422 /*----------------------------------------------------------------------------*/ 00423 int hawki_compute_harmonization( 00424 const cpl_imagelist * in, 00425 double * h1, 00426 double * h2, 00427 double * h3, 00428 double * h4, 00429 double * h) 00430 { 00431 int width = 64 ; 00432 int nx, ny ; 00433 const cpl_image * ima ; 00434 double avg1, avg2, avg3, avg4 ; 00435 double val1, val2 ; 00436 int llx, lly, urx, ury ; 00437 00438 /* Test entries */ 00439 if (in == NULL) return -1 ; 00440 if (h1==NULL || h2==NULL || h3==NULL || h4==NULL || h==NULL) return -1 ; 00441 00442 /* Compute the avg1 */ 00443 ima = cpl_imagelist_get_const(in, 0) ; 00444 nx = cpl_image_get_size_x(ima) ; 00445 ny = cpl_image_get_size_y(ima) ; 00446 llx = 1 ; lly = ny - width + 1 ; urx = nx ; ury = ny ; 00447 val1 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ; 00448 if (cpl_error_get_code()) { 00449 cpl_msg_error(__func__, "Cannot get statistics from chip 1") ; 00450 return -1 ; 00451 } 00452 llx = nx - width + 1 ; lly = 1 ; urx = nx ; ury = ny ; 00453 val2 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ; 00454 if (cpl_error_get_code()) { 00455 cpl_msg_error(__func__, "Cannot get statistics from chip 1") ; 00456 return -1 ; 00457 } 00458 avg1 = (val1 + val2) / 2.0 ; 00459 00460 /* Compute the avg2 */ 00461 ima = cpl_imagelist_get_const(in, 1) ; 00462 nx = cpl_image_get_size_x(ima) ; 00463 ny = cpl_image_get_size_y(ima) ; 00464 llx = 1 ; lly = 1 ; urx = width ; ury = ny ; 00465 val1 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ; 00466 if (cpl_error_get_code()) { 00467 cpl_msg_error(__func__, "Cannot get statistics from chip 2") ; 00468 return -1 ; 00469 } 00470 llx = 1 ; lly = ny - width + 1 ; urx = nx ; ury = ny ; 00471 val2 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ; 00472 if (cpl_error_get_code()) { 00473 cpl_msg_error(__func__, "Cannot get statistics from chip 2") ; 00474 return -1 ; 00475 } 00476 avg2 = (val1 + val2) / 2.0 ; 00477 00478 /* Compute the avg3 */ 00479 ima = cpl_imagelist_get_const(in, 2) ; 00480 nx = cpl_image_get_size_x(ima) ; 00481 ny = cpl_image_get_size_y(ima) ; 00482 llx = 1 ; lly = 1 ; urx = nx ; ury = width ; 00483 val1 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ; 00484 if (cpl_error_get_code()) { 00485 cpl_msg_error(__func__, "Cannot get statistics from chip 3") ; 00486 return -1 ; 00487 } 00488 llx = nx - width + 1 ; lly = 1 ; urx = nx ; ury = ny ; 00489 val2 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ; 00490 if (cpl_error_get_code()) { 00491 cpl_msg_error(__func__, "Cannot get statistics from chip 3") ; 00492 return -1 ; 00493 } 00494 avg3 = (val1 + val2) / 2.0 ; 00495 00496 /* Compute the avg4 */ 00497 ima = cpl_imagelist_get_const(in, 3) ; 00498 nx = cpl_image_get_size_x(ima) ; 00499 ny = cpl_image_get_size_y(ima) ; 00500 llx = 1 ; lly = 1 ; urx = width ; ury = ny ; 00501 val1 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ; 00502 if (cpl_error_get_code()) { 00503 cpl_msg_error(__func__, "Cannot get statistics from chip 4") ; 00504 return -1 ; 00505 } 00506 llx = 1 ; lly = 1 ; urx = nx ; ury = width ; 00507 val2 = cpl_image_get_mean_window(ima, llx, lly, urx, ury) ; 00508 if (cpl_error_get_code()) { 00509 cpl_msg_error(__func__, "Cannot get statistics from chip 4") ; 00510 return -1 ; 00511 } 00512 avg4 = (val1 + val2) / 2.0 ; 00513 00514 /* Compute h */ 00515 *h = (avg1 + avg2 + avg3 + avg4) / 4.0 ; 00516 00517 *h1 = *h / avg1 ; 00518 *h2 = *h / avg2 ; 00519 *h3 = *h / avg3 ; 00520 *h4 = *h / avg4 ; 00521 00522 return 0 ; 00523 } 00524 00525 /*----------------------------------------------------------------------------*/ 00531 /*----------------------------------------------------------------------------*/ 00532 cpl_image * hawki_compute_lsbg(const cpl_image * in) 00533 { 00534 cpl_image * out ; 00535 cpl_image * tmp ; 00536 cpl_image * filtered ; 00537 cpl_image * subsampled ; 00538 cpl_mask * kernel ; 00539 int nscales ; 00540 cpl_polynomial * poly ; 00541 cpl_bivector * xy_pos ; 00542 cpl_vector * vals ; 00543 int quad_sz, nbpoints, lx, ly, nx, ny ; 00544 double * pxy_pos_x ; 00545 double * pxy_pos_y ; 00546 double * pvals ; 00547 float * pima ; 00548 int i, j ; 00549 00550 /* Check entries */ 00551 if (in == NULL) return NULL ; 00552 nx = cpl_image_get_size_x(in) ; 00553 ny = cpl_image_get_size_y(in) ; 00554 00555 /* Initialise */ 00556 nscales = 7 ; 00557 tmp = (cpl_image *)in ; 00558 subsampled = NULL ; 00559 00560 /* Check entries */ 00561 quad_sz = pow(2, (double)nscales) ; 00562 lx = nx / quad_sz ; 00563 ly = ny / quad_sz ; 00564 nbpoints = lx * ly ; 00565 if (quad_sz >= nx || quad_sz >= ny) return NULL ; 00566 00567 /* Create filter kernel */ 00568 kernel = cpl_mask_new(3, 3) ; 00569 cpl_mask_not(kernel) ; 00570 00571 /* Loop nscales times */ 00572 for (i=0 ; i<nscales ; i++) { 00573 00574 /* Filter the image */ 00575 filtered = cpl_image_new(cpl_image_get_size_x(tmp), 00576 cpl_image_get_size_y(tmp), 00577 cpl_image_get_type(tmp)); 00578 cpl_image_filter_mask(filtered, in, kernel, CPL_FILTER_MEDIAN, 00579 CPL_BORDER_FILTER); 00580 if (i>0) cpl_image_delete(tmp) ; 00581 00582 /* Subsample the image */ 00583 subsampled = cpl_image_extract_subsample(filtered, 2, 2) ; 00584 cpl_image_delete(filtered) ; 00585 00586 tmp = subsampled ; 00587 } 00588 cpl_mask_delete(kernel) ; 00589 00590 /* Check nbpoints */ 00591 if (nbpoints != 00592 cpl_image_get_size_x(subsampled)*cpl_image_get_size_y(subsampled)) { 00593 cpl_msg_error(__func__, "Invalid size") ; 00594 cpl_image_delete(subsampled) ; 00595 return NULL ; 00596 } 00597 00598 /* Create anchor points for the fit */ 00599 xy_pos = cpl_bivector_new(nbpoints) ; 00600 vals = cpl_vector_new(nbpoints) ; 00601 pxy_pos_x = cpl_bivector_get_x_data(xy_pos) ; 00602 pxy_pos_y = cpl_bivector_get_y_data(xy_pos) ; 00603 pvals = cpl_vector_get_data(vals) ; 00604 pima = cpl_image_get_data_float(subsampled) ; 00605 for (j=0 ; j<ly ; j++) { 00606 for (i=0 ; i<lx ; i++) { 00607 pxy_pos_x[i+j*lx] = i * quad_sz + quad_sz/2 ; 00608 pxy_pos_y[i+j*lx] = j * quad_sz + quad_sz/2 ; 00609 pvals[i+j*lx] = (double)pima[i+j*lx]; 00610 } 00611 } 00612 cpl_image_delete(subsampled) ; 00613 00614 /* Fit the polynomial */ 00615 if ((poly = cpl_polynomial_fit_2d_create(xy_pos, vals, 3, NULL)) == NULL) { 00616 cpl_msg_error(__func__, "Cannot fit the polynomial") ; 00617 cpl_bivector_delete(xy_pos) ; 00618 cpl_vector_delete(vals) ; 00619 return NULL ; 00620 } 00621 cpl_bivector_delete(xy_pos) ; 00622 cpl_vector_delete(vals) ; 00623 00624 /* Regenerate the big bgd image */ 00625 out = cpl_image_duplicate(in) ; 00626 cpl_image_fill_polynomial(out, poly, 1.0, 1.0, 1.0, 1.0) ; 00627 cpl_polynomial_delete(poly) ; 00628 00629 return out ; 00630 } 00631 00632 /*----------------------------------------------------------------------------*/ 00639 /*----------------------------------------------------------------------------*/ 00640 const char * hawki_extract_first_filename( 00641 const cpl_frameset * in, 00642 const char * tag) 00643 { 00644 const cpl_frame * cur_frame ; 00645 00646 /* Get the frame */ 00647 if ((cur_frame = cpl_frameset_find_const(in, tag)) == NULL) return NULL ; 00648 return cpl_frame_get_filename(cur_frame) ; 00649 } 00650 00651 /*----------------------------------------------------------------------------*/ 00657 /*----------------------------------------------------------------------------*/ 00658 hawki_band hawki_get_band(const char * f) 00659 { 00660 if (!strcmp(f, "J")) return HAWKI_BAND_J ; 00661 if (!strcmp(f, "H")) return HAWKI_BAND_H ; 00662 if (!strcmp(f, "K")) return HAWKI_BAND_K ; 00663 if (!strcmp(f, "Ks")) return HAWKI_BAND_K ; 00664 if (!strcmp(f, "Y")) return HAWKI_BAND_Y ; 00665 return HAWKI_BAND_UNKNOWN ; 00666 } 00667 00668 /*-------------------------------------------------------------------------*/ 00674 /*--------------------------------------------------------------------------*/ 00675 const char * hawki_std_band_name(hawki_band band) 00676 { 00677 switch (band) { 00678 case HAWKI_BAND_J: return "J" ; 00679 case HAWKI_BAND_H: return "H" ; 00680 case HAWKI_BAND_K: return "K" ; 00681 case HAWKI_BAND_Y: return "Y" ; 00682 default: return "Unknown" ; 00683 } 00684 } 00685 00686 /*----------------------------------------------------------------------------*/ 00695 /*----------------------------------------------------------------------------*/ 00696 cpl_bivector * hawki_get_header_tel_offsets(const cpl_frameset * fset) 00697 { 00698 cpl_bivector * offsets ; 00699 double * offsets_x ; 00700 double * offsets_y ; 00701 const cpl_frame * frame ; 00702 cpl_propertylist * plist ; 00703 int nfiles ; 00704 int i ; 00705 cpl_errorstate error_prevstate = cpl_errorstate_get(); 00706 00707 00708 /* Test entries */ 00709 if (fset == NULL) return NULL ; 00710 00711 /* Create the offsets bi vector */ 00712 nfiles = cpl_frameset_get_size(fset) ; 00713 offsets = cpl_bivector_new(nfiles) ; 00714 offsets_x = cpl_bivector_get_x_data(offsets) ; 00715 offsets_y = cpl_bivector_get_y_data(offsets) ; 00716 for (i=0 ; i<nfiles ; i++) { 00717 00718 /* X and Y offsets */ 00719 frame = cpl_frameset_get_frame_const(fset, i) ; 00720 plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0); 00721 offsets_x[i] = hawki_pfits_get_cumoffsetx(plist) ; 00722 offsets_y[i] = hawki_pfits_get_cumoffsety(plist) ; 00723 cpl_propertylist_delete(plist) ; 00724 if(!cpl_errorstate_is_equal(error_prevstate )) 00725 { 00726 cpl_msg_error(__func__, "Cannot get offsets from header") ; 00727 cpl_bivector_delete(offsets) ; 00728 return NULL ; 00729 } 00730 } 00731 return offsets ; 00732 } 00733 00734 /*----------------------------------------------------------------------------*/ 00740 /*----------------------------------------------------------------------------*/ 00741 double hawki_get_mean_airmass(cpl_frameset * set) 00742 { 00743 int nframes; 00744 cpl_frame * cur_frame; 00745 cpl_propertylist * plist; 00746 int iframe; 00747 double mean_airmass = 0.0; 00748 00749 /* Test inputs */ 00750 if (set == NULL) return -1; 00751 00752 /* Initialize */ 00753 nframes = cpl_frameset_get_size(set); 00754 00755 for (iframe=0 ; iframe<nframes ; iframe++) 00756 { 00757 cur_frame = cpl_frameset_get_frame(set, iframe); 00758 plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0); 00759 mean_airmass += hawki_pfits_get_airmass_start(plist) + 00760 hawki_pfits_get_airmass_end(plist); 00761 cpl_propertylist_delete(plist); 00762 } 00763 mean_airmass /= 2. * nframes; 00764 00765 /* Free and return */ 00766 return mean_airmass; 00767 } 00768 00769 /*----------------------------------------------------------------------------*/ 00781 /*----------------------------------------------------------------------------*/ 00782 int * hawki_detectors_labelise 00783 (const cpl_frameset * in) 00784 { 00785 int * labels ; 00786 cpl_bivector * offsets ; 00787 int nframes ; 00788 double * poff_x ; 00789 double * poff_y ; 00790 double off_x_mean; 00791 double off_y_mean; 00792 int i ; 00793 00794 /* Check entries */ 00795 if (in == NULL) return NULL ; 00796 00797 /* Initialise */ 00798 nframes = cpl_frameset_get_size(in) ; 00799 00800 /* Get the offsets */ 00801 if ((offsets = hawki_get_header_tel_offsets((cpl_frameset *)in)) == NULL) { 00802 cpl_msg_error(__func__, "Cannot read the offsets") ; 00803 return NULL ; 00804 } 00805 poff_x = cpl_bivector_get_x_data(offsets) ; 00806 poff_y = cpl_bivector_get_y_data(offsets) ; 00807 00808 /* Get the mean offsets */ 00809 off_x_mean = cpl_vector_get_mean(cpl_bivector_get_x(offsets)); 00810 off_y_mean = cpl_vector_get_mean(cpl_bivector_get_y(offsets)); 00811 00812 /* Allocate labels */ 00813 labels = cpl_malloc(nframes * sizeof(int)) ; 00814 for (i=0 ; i<nframes ; i++) { 00815 if (poff_x[i] - off_x_mean <= 0 && poff_y[i] - off_y_mean <= 0) 00816 labels[i] = 1 ; 00817 else if (poff_x[i] - off_x_mean >= 0 && poff_y[i] - off_y_mean <= 0) 00818 labels[i] = 2 ; 00819 else if (poff_x[i] - off_x_mean >= 0 && poff_y[i] - off_y_mean >= 0) 00820 labels[i] = 3 ; 00821 else if (poff_x[i] - off_x_mean <= 0 && poff_y[i] - off_y_mean >= 0) 00822 labels[i] = 4 ; 00823 else labels[i] = 0 ; 00824 } 00825 cpl_bivector_delete(offsets) ; 00826 return labels ; 00827 } 00828 00829 /*----------------------------------------------------------------------------*/ 00836 /*----------------------------------------------------------------------------*/ 00837 int hawki_detectors_locate_star 00838 (const cpl_frameset * in, 00839 double star_ra, 00840 double star_dec, 00841 int * labels) 00842 { 00843 int nframes; 00844 int idet, iframe; 00845 00846 /* Check entries */ 00847 if (in == NULL) return -1; 00848 00849 /* Initialise */ 00850 nframes = cpl_frameset_get_size(in) ; 00851 00852 /* Allocate labels */ 00853 for (iframe=0 ; iframe<nframes ; iframe++) 00854 { 00855 const char * filename; 00856 filename = cpl_frame_get_filename 00857 (cpl_frameset_get_frame_const(in, iframe)); 00858 00859 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 00860 { 00861 cpl_propertylist * main_header; 00862 cpl_propertylist * ext_header; 00863 cpl_wcs * wcs; 00864 double naxis1, naxis2; 00865 double star_x, star_y; 00866 00867 /* Get the headers */ 00868 main_header = cpl_propertylist_load(filename, 0); 00869 ext_header = cpl_propertylist_load 00870 (filename, hawki_get_ext_from_detector(filename,idet + 1)); 00871 00872 /* Get the position of the star in pixels */ 00873 wcs = cpl_wcs_new_from_propertylist(ext_header); 00874 if(wcs == NULL) 00875 { 00876 cpl_msg_error(__func__, "Could not get WCS info"); 00877 cpl_propertylist_delete(ext_header); 00878 cpl_propertylist_delete(main_header); 00879 return -1; 00880 } 00881 if(irplib_wcs_radectoxy(wcs, star_ra, star_dec, &star_x, &star_y) 00882 != CPL_ERROR_NONE) 00883 { 00884 cpl_errorstate_set(CPL_ERROR_NONE); 00885 } 00886 00887 /* Check for the limits */ 00888 naxis1 = (double)hawki_pfits_get_naxis1(ext_header); 00889 naxis2 = (double)hawki_pfits_get_naxis2(ext_header); 00890 if(star_x > 0 && star_x < naxis1 && star_y > 0 && star_y < naxis2) 00891 { 00892 labels[iframe] = idet + 1; 00893 } 00894 00895 /* Free */ 00896 cpl_propertylist_delete(ext_header); 00897 cpl_propertylist_delete(main_header); 00898 cpl_wcs_delete(wcs); 00899 } 00900 if(labels[iframe] == 0) 00901 { 00902 cpl_msg_error(__func__,"Frame %d does not contain the star in any " 00903 "detector", iframe + 1); 00904 } 00905 } 00906 return 0; 00907 } 00908 00909 /*----------------------------------------------------------------------------*/ 00917 /*----------------------------------------------------------------------------*/ 00918 double hawki_vector_get_max_select 00919 (const cpl_vector * self, const cpl_vector * valid) 00920 { 00921 double max_val = DBL_MIN; 00922 int initialized = 0; 00923 int ival; 00924 int nvals; 00925 00926 nvals = cpl_vector_get_size(self); 00927 for(ival = 0; ival < nvals; ++ival) 00928 { 00929 if(cpl_vector_get(valid, ival) >= -0.5) 00930 { 00931 if(!initialized) 00932 { 00933 max_val = cpl_vector_get(self, ival); 00934 initialized = 1; 00935 } 00936 if(cpl_vector_get(self, ival) > max_val) 00937 max_val = cpl_vector_get(self, ival); 00938 } 00939 } 00940 return max_val; 00941 } 00942 00943 /*----------------------------------------------------------------------------*/ 00951 /*----------------------------------------------------------------------------*/ 00952 double hawki_vector_get_min_select 00953 (const cpl_vector * self, const cpl_vector * valid) 00954 { 00955 double min_val = DBL_MAX; 00956 int initialized = 0; 00957 int ival; 00958 int nvals; 00959 00960 nvals = cpl_vector_get_size(self); 00961 for(ival = 0; ival < nvals; ++ival) 00962 { 00963 if(cpl_vector_get(valid, ival) >= -0.5) 00964 { 00965 if(!initialized) 00966 { 00967 min_val = cpl_vector_get(self, ival); 00968 initialized = 1; 00969 } 00970 if(cpl_vector_get(self, ival) < min_val) 00971 min_val = cpl_vector_get(self, ival); 00972 } 00973 } 00974 return min_val; 00975 } 00976 00977 /*----------------------------------------------------------------------------*/ 00983 /*----------------------------------------------------------------------------*/ 00984 double hawki_vector_get_mode(cpl_vector * vec) 00985 { 00986 int nb ; 00987 int nbins ; 00988 double min, max ; 00989 double bin_size ; 00990 cpl_bivector * hist ; 00991 cpl_vector * hist_x ; 00992 cpl_vector * hist_y ; 00993 double cur_val ; 00994 int cur_bin ; 00995 double max_val ; 00996 int max_bin ; 00997 double mode ; 00998 int i ; 00999 01000 /* Test entries */ 01001 if (vec == NULL) return -1.0 ; 01002 01003 /* Initialise */ 01004 nb = cpl_vector_get_size(vec) ; 01005 01006 /* Create the histogram */ 01007 nbins = 10 ; 01008 min = cpl_vector_get_min(vec) ; 01009 max = cpl_vector_get_max(vec) ; 01010 bin_size = (max-min)/nbins ; 01011 hist = cpl_bivector_new(nbins) ; 01012 hist_x = cpl_bivector_get_x(hist) ; 01013 hist_y = cpl_bivector_get_y(hist) ; 01014 cpl_vector_fill(hist_x, 0.0) ; 01015 cpl_vector_fill(hist_y, 0.0) ; 01016 for (i=0 ; i<nbins ; i++) { 01017 cpl_vector_set(hist_x, i, min + i * bin_size) ; 01018 } 01019 for (i=0 ; i<nb ; i++) { 01020 cur_val = cpl_vector_get(vec, i) ; 01021 cur_bin = (int)((cur_val - min) / bin_size) ; 01022 if (cur_bin >= nbins) cur_bin -= 1.0 ; 01023 cur_val = cpl_vector_get(hist_y, cur_bin) ; 01024 cur_val += 1.0 ; 01025 cpl_vector_set(hist_y, cur_bin, cur_val) ; 01026 } 01027 01028 /* Get the mode of the histogram */ 01029 max_val = cpl_vector_get(hist_y, 0) ; 01030 max_bin = 0 ; 01031 for (i=0 ; i<nbins ; i++) { 01032 cur_val = cpl_vector_get(hist_y, i) ; 01033 if (cur_val > max_val) { 01034 max_val = cur_val ; 01035 max_bin = i ; 01036 } 01037 } 01038 mode = cpl_vector_get(hist_x, max_bin) ; 01039 cpl_bivector_delete(hist) ; 01040 return mode ; 01041 } 01042 01043 /*----------------------------------------------------------------------------*/ 01051 /*----------------------------------------------------------------------------*/ 01052 int hawki_utils_check_equal_double_keys 01053 (cpl_frameset * frames, double (*func)(const cpl_propertylist *)) 01054 { 01055 int iframe; 01056 01057 for(iframe = 0; iframe < cpl_frameset_get_size(frames); ++iframe) 01058 { 01059 double value; 01060 cpl_propertylist * header; 01061 header = cpl_propertylist_load( 01062 cpl_frame_get_filename( 01063 cpl_frameset_get_frame_const(frames, iframe)),0); 01064 if(iframe == 0) 01065 value = (func)(header); 01066 else 01067 if(value != (func)(header)) 01068 { 01069 cpl_propertylist_delete(header); 01070 return 0; 01071 } 01072 cpl_propertylist_delete(header); 01073 } 01074 return 1; 01075 } 01076 01077 /*----------------------------------------------------------------------------*/ 01085 /*----------------------------------------------------------------------------*/ 01086 int hawki_utils_check_equal_int_keys 01087 (cpl_frameset * frames, int (*func)(const cpl_propertylist *)) 01088 { 01089 int iframe; 01090 01091 for(iframe = 0; iframe < cpl_frameset_get_size(frames); ++iframe) 01092 { 01093 int value; 01094 cpl_propertylist * header; 01095 header = cpl_propertylist_load( 01096 cpl_frame_get_filename( 01097 cpl_frameset_get_frame_const(frames, iframe)),0); 01098 if(iframe == 0) 01099 value = (func)(header); 01100 else 01101 if(value != (func)(header)) 01102 { 01103 cpl_propertylist_delete(header); 01104 return 0; 01105 } 01106 cpl_propertylist_delete(header); 01107 } 01108 return 1; 01109 } 01110 01111
1.7.3