HAWKI Pipeline Reference Manual 1.8.12
|
00001 /* $Id: hawki_image_stats.c,v 1.8 2012/05/03 10:42:32 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: 2012/05/03 10:42:32 $ 00024 * $Revision: 1.8 $ 00025 * $Name: hawki-1_8_12 $ 00026 */ 00027 00028 #ifdef HAVE_CONFIG_H 00029 #include <config.h> 00030 #endif 00031 00032 /*----------------------------------------------------------------------------- 00033 Includes 00034 -----------------------------------------------------------------------------*/ 00035 00036 #include <string.h> 00037 #include <cpl.h> 00038 00039 #include "hawki_image_stats.h" 00040 #include "hawki_dfs.h" 00041 #include "hawki_utils.h" 00042 #include "hawki_load.h" 00043 00044 /*----------------------------------------------------------------------------*/ 00045 /* Private functions */ 00046 /*----------------------------------------------------------------------------*/ 00047 float hawki_tools_get_kth_float(float * a, 00048 int n, 00049 int k); 00050 00051 /*----------------------------------------------------------------------------*/ 00055 /*----------------------------------------------------------------------------*/ 00056 00059 /*----------------------------------------------------------------------------*/ 00068 /*----------------------------------------------------------------------------*/ 00069 int hawki_image_stats_initialize 00070 (cpl_table ** raw_stats) 00071 { 00072 int idet; 00073 /* Error state variables */ 00074 cpl_errorstate prestate = cpl_errorstate_get(); 00075 00076 /* Check inputs */ 00077 if(raw_stats == NULL) 00078 return -1; 00079 for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 00080 { 00081 if(raw_stats[idet] == NULL) 00082 return -1; 00083 } 00084 00085 /* Creates the proper columns */ 00086 for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 00087 { 00088 cpl_table_new_column 00089 (raw_stats[idet],HAWKI_COL_STAT_MIN,CPL_TYPE_DOUBLE); 00090 cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_MIN,"ADU"); 00091 cpl_table_new_column 00092 (raw_stats[idet],HAWKI_COL_STAT_MAX,CPL_TYPE_DOUBLE); 00093 cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_MAX,"ADU"); 00094 cpl_table_new_column 00095 (raw_stats[idet],HAWKI_COL_STAT_MED,CPL_TYPE_DOUBLE); 00096 cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_MED,"ADU"); 00097 cpl_table_new_column 00098 (raw_stats[idet],HAWKI_COL_STAT_MEAN,CPL_TYPE_DOUBLE); 00099 cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_MEAN,"ADU"); 00100 cpl_table_new_column 00101 (raw_stats[idet],HAWKI_COL_STAT_RMS,CPL_TYPE_DOUBLE); 00102 cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_RMS,"ADU"); 00103 cpl_table_new_column 00104 (raw_stats[idet],HAWKI_COL_STAT_USED,CPL_TYPE_INT); 00105 00106 } 00107 /* Check error status and exit */ 00108 if(!cpl_errorstate_is_equal(prestate)) 00109 return -1; 00110 return 0; 00111 } 00112 00113 /*----------------------------------------------------------------------------*/ 00135 /*----------------------------------------------------------------------------*/ 00136 int hawki_image_stats_fill_from_image 00137 (cpl_table ** image_stats, 00138 const cpl_image * image, 00139 int llx, 00140 int lly, 00141 int urx, 00142 int ury, 00143 int idet, 00144 int irow) 00145 { 00146 /* stats variables */ 00147 double minval; 00148 double maxval; 00149 double median; 00150 double stdev; 00151 double mean; 00152 cpl_stats * stats_ima ; 00153 00154 /* Error state variables */ 00155 cpl_errorstate prestate = cpl_errorstate_get(); 00156 00157 /* Checking input */ 00158 if(image_stats == NULL || image == NULL) 00159 return -1; 00160 00161 /* Compute statistics */ 00162 stats_ima = cpl_stats_new_from_image_window 00163 (image, CPL_STATS_ALL, llx, lly, urx, ury) ; 00164 if(stats_ima == NULL) 00165 return -1; 00166 00167 /* Get the stats from the storage structure */ 00168 minval = cpl_stats_get_min(stats_ima); 00169 maxval = cpl_stats_get_max(stats_ima); 00170 median = cpl_stats_get_median(stats_ima); 00171 stdev = cpl_stats_get_stdev(stats_ima); 00172 mean = cpl_stats_get_mean(stats_ima); 00173 cpl_stats_delete(stats_ima); 00174 00175 /* Store in table */ 00176 cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_MIN, 00177 irow, minval); 00178 cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_MAX, 00179 irow, maxval); 00180 cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_MED, 00181 irow, median); 00182 cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_MEAN, 00183 irow, mean); 00184 cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_RMS, 00185 irow, stdev) ; 00186 cpl_table_set_int(image_stats[idet], HAWKI_COL_STAT_USED, 00187 irow, 1) ; 00188 00189 /* Check error status and exit */ 00190 if(!cpl_errorstate_is_equal(prestate)) 00191 return -1; 00192 return 0; 00193 } 00194 00195 int hawki_image_stats_odd_even_column_row_fill_from_image 00196 (cpl_table ** odd_column_stats, 00197 cpl_table ** even_column_stats, 00198 cpl_table ** odd_row_stats, 00199 cpl_table ** even_row_stats, 00200 const cpl_image * image, 00201 int idet, 00202 int irow) 00203 { 00204 /* stats variables */ 00205 int i; 00206 int j; 00207 int nx; 00208 int ny; 00209 double minval; 00210 double maxval; 00211 double median; 00212 double stdev; 00213 double mean; 00214 cpl_stats * stats_ima; 00215 cpl_image * tmp_ima; 00216 cpl_mask * mask; 00217 00218 /* Error state variables */ 00219 cpl_errorstate prestate = cpl_errorstate_get(); 00220 00221 /* Checking input */ 00222 if(odd_column_stats == NULL || 00223 even_column_stats == NULL || 00224 odd_row_stats == NULL || 00225 even_row_stats == NULL || 00226 image == NULL) 00227 return -1; 00228 00229 /* Copying the target image */ 00230 tmp_ima = cpl_image_duplicate(image); 00231 nx = cpl_image_get_size_x(tmp_ima); 00232 ny = cpl_image_get_size_y(tmp_ima); 00233 00234 /* Compute statistics odd column */ 00235 mask = cpl_image_get_bpm(tmp_ima); 00236 for(i=0 ; i < nx ; ++i) 00237 { 00238 if((i+1) % 2) 00239 for(j=0 ; j < ny ; ++j) 00240 { 00241 cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1); 00242 } 00243 } 00244 stats_ima = cpl_stats_new_from_image 00245 (tmp_ima, CPL_STATS_ALL); 00246 if(stats_ima == NULL) 00247 { 00248 cpl_image_delete(tmp_ima); 00249 return -1; 00250 } 00251 00252 /* Get the stats from the storage structure */ 00253 minval = cpl_stats_get_min(stats_ima); 00254 maxval = cpl_stats_get_max(stats_ima); 00255 median = cpl_stats_get_median(stats_ima); 00256 stdev = cpl_stats_get_stdev(stats_ima); 00257 mean = cpl_stats_get_mean(stats_ima); 00258 cpl_stats_delete(stats_ima); 00259 00260 /* Store in table */ 00261 cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_MIN, 00262 irow, minval); 00263 cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_MAX, 00264 irow, maxval); 00265 cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_MED, 00266 irow, median); 00267 cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_MEAN, 00268 irow, mean); 00269 cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_RMS, 00270 irow, stdev) ; 00271 cpl_table_set_int(odd_column_stats[idet], HAWKI_COL_STAT_USED, 00272 irow, 1) ; 00273 00274 /* Compute statistics even column */ 00275 //cpl_image_reject_from_mask(); 00276 cpl_image_accept_all(tmp_ima); 00277 mask = cpl_image_get_bpm(tmp_ima); 00278 for(i=0 ; i < nx ; ++i) 00279 { 00280 if(i % 2) 00281 for(j=0 ; j < ny ; ++j) 00282 { 00283 cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1); 00284 } 00285 } 00286 stats_ima = cpl_stats_new_from_image 00287 (tmp_ima, CPL_STATS_ALL); 00288 if(stats_ima == NULL) 00289 { 00290 cpl_image_delete(tmp_ima); 00291 return -1; 00292 } 00293 00294 /* Get the stats from the storage structure */ 00295 minval = cpl_stats_get_min(stats_ima); 00296 maxval = cpl_stats_get_max(stats_ima); 00297 median = cpl_stats_get_median(stats_ima); 00298 stdev = cpl_stats_get_stdev(stats_ima); 00299 mean = cpl_stats_get_mean(stats_ima); 00300 cpl_stats_delete(stats_ima); 00301 00302 /* Store in table */ 00303 cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_MIN, 00304 irow, minval); 00305 cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_MAX, 00306 irow, maxval); 00307 cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_MED, 00308 irow, median); 00309 cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_MEAN, 00310 irow, mean); 00311 cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_RMS, 00312 irow, stdev) ; 00313 cpl_table_set_int(even_column_stats[idet], HAWKI_COL_STAT_USED, 00314 irow, 1) ; 00315 00316 /* Compute statistics odd rows */ 00317 cpl_image_accept_all(tmp_ima); 00318 mask = cpl_image_get_bpm(tmp_ima); 00319 for(j=0 ; j < ny ; ++j) 00320 { 00321 if((j+1) % 2) 00322 for(i=0 ; i < nx ; ++i) 00323 { 00324 cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1); 00325 } 00326 } 00327 stats_ima = cpl_stats_new_from_image 00328 (tmp_ima, CPL_STATS_ALL) ; 00329 if(stats_ima == NULL) 00330 { 00331 cpl_image_delete(tmp_ima); 00332 return -1; 00333 } 00334 00335 /* Get the stats from the storage structure */ 00336 minval = cpl_stats_get_min(stats_ima); 00337 maxval = cpl_stats_get_max(stats_ima); 00338 median = cpl_stats_get_median(stats_ima); 00339 stdev = cpl_stats_get_stdev(stats_ima); 00340 mean = cpl_stats_get_mean(stats_ima); 00341 cpl_stats_delete(stats_ima); 00342 00343 /* Store in table */ 00344 cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_MIN, 00345 irow, minval); 00346 cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_MAX, 00347 irow, maxval); 00348 cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_MED, 00349 irow, median); 00350 cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_MEAN, 00351 irow, mean); 00352 cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_RMS, 00353 irow, stdev) ; 00354 cpl_table_set_int(odd_row_stats[idet], HAWKI_COL_STAT_USED, 00355 irow, 1) ; 00356 00357 /* Compute statistics even row */ 00358 cpl_image_accept_all(tmp_ima); 00359 mask = cpl_image_get_bpm(tmp_ima); 00360 for(j=0 ; j < ny ; ++j) 00361 { 00362 if(j % 2) 00363 for(i=0 ; i < nx ; ++i) 00364 { 00365 cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1); 00366 } 00367 } 00368 stats_ima = cpl_stats_new_from_image 00369 (tmp_ima, CPL_STATS_ALL) ; 00370 if(stats_ima == NULL) 00371 { 00372 cpl_image_delete(tmp_ima); 00373 return -1; 00374 } 00375 00376 /* Get the stats from the storage structure */ 00377 minval = cpl_stats_get_min(stats_ima); 00378 maxval = cpl_stats_get_max(stats_ima); 00379 median = cpl_stats_get_median(stats_ima); 00380 stdev = cpl_stats_get_stdev(stats_ima); 00381 mean = cpl_stats_get_mean(stats_ima); 00382 cpl_stats_delete(stats_ima); 00383 00384 /* Store in table */ 00385 cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_MIN, 00386 irow, minval); 00387 cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_MAX, 00388 irow, maxval); 00389 cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_MED, 00390 irow, median); 00391 cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_MEAN, 00392 irow, mean); 00393 cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_RMS, 00394 irow, stdev) ; 00395 cpl_table_set_int(even_row_stats[idet], HAWKI_COL_STAT_USED, 00396 irow, 1) ; 00397 00398 /* Free */ 00399 cpl_image_delete(tmp_ima); 00400 00401 /* Check error status and exit */ 00402 if(!cpl_errorstate_is_equal(prestate)) 00403 return -1; 00404 return 0; 00405 } 00406 00407 /*----------------------------------------------------------------------------*/ 00428 /*----------------------------------------------------------------------------*/ 00429 int hawki_image_stats_fill_from_frame 00430 (cpl_table ** image_stats, 00431 const cpl_frame * frame, 00432 int irow) 00433 { 00434 int idet; 00435 cpl_imagelist * images; 00436 00437 /* Loading the four chips */ 00438 images = hawki_load_frame(frame, CPL_TYPE_FLOAT); 00439 if(images == NULL) 00440 { 00441 cpl_msg_error(__func__,"Could not read file %s", 00442 cpl_frame_get_filename(frame)); 00443 return -1; 00444 } 00445 00446 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet) 00447 { 00448 int nx, ny; 00449 nx = cpl_image_get_size_x(cpl_imagelist_get(images,idet)); 00450 ny = cpl_image_get_size_y(cpl_imagelist_get(images,idet)); 00451 hawki_image_stats_fill_from_image 00452 (image_stats, 00453 cpl_imagelist_get(images,idet), 00454 1, 1, nx, ny, idet, irow); 00455 } 00456 00457 /* Free and exit */ 00458 cpl_imagelist_delete(images); 00459 return 0; 00460 } 00461 00462 int hawki_image_stats_print 00463 (cpl_table ** table_stats) 00464 { 00465 int idet; 00466 int istat; 00467 00468 /* Print header */ 00469 cpl_msg_info(__func__, "Stats summary") ; 00470 00471 /* Loop on detectors */ 00472 cpl_msg_indent_more(); 00473 for( idet = 0; idet < HAWKI_NB_DETECTORS; ++idet) 00474 { 00475 00476 /* Chip header */ 00477 cpl_msg_info(__func__, "Chip number %d", idet+1) ; 00478 cpl_msg_info(__func__, "image min max med rms") ; 00479 cpl_msg_info(__func__, "--------------------------------------------") ; 00480 00481 /* Loop on images */ 00482 for(istat = 0; istat < cpl_table_get_nrow(table_stats[idet]); ++istat) 00483 { 00484 cpl_msg_info(__func__, "%02d %10.2f %10.2f %10.2f %10.2f", 00485 istat+1, 00486 cpl_table_get_double(table_stats[idet], 00487 HAWKI_COL_STAT_MIN,istat,NULL), 00488 cpl_table_get_double(table_stats[idet], 00489 HAWKI_COL_STAT_MAX,istat,NULL), 00490 cpl_table_get_double(table_stats[idet], 00491 HAWKI_COL_STAT_MED,istat,NULL ), 00492 cpl_table_get_double(table_stats[idet], 00493 HAWKI_COL_STAT_RMS,istat,NULL )); 00494 } 00495 } 00496 cpl_msg_indent_less(); 00497 return 0; 00498 } 00499 00500 /*----------------------------------------------------------------------------*/ 00509 /*----------------------------------------------------------------------------*/ 00510 00511 int hawki_image_stats_stats 00512 (cpl_table ** image_stats, 00513 cpl_propertylist ** stats_stats) 00514 { 00515 cpl_array * col_names; 00516 int idet; 00517 int icol; 00518 00519 /* Check entries */ 00520 if(image_stats == NULL || stats_stats == NULL) 00521 return -1; 00522 00523 /* Fill the name of the interesting columns */ 00524 col_names = cpl_array_new(5, CPL_TYPE_STRING); 00525 cpl_array_set_string(col_names, 0, HAWKI_COL_STAT_MIN); 00526 cpl_array_set_string(col_names, 1, HAWKI_COL_STAT_MAX); 00527 cpl_array_set_string(col_names, 2, HAWKI_COL_STAT_MED); 00528 cpl_array_set_string(col_names, 3, HAWKI_COL_STAT_MEAN); 00529 cpl_array_set_string(col_names, 4, HAWKI_COL_STAT_RMS); 00530 00531 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; ++idet) 00532 { 00533 for(icol = 0;icol < 5; ++icol) 00534 { 00535 const char * this_col_name = cpl_array_get_string(col_names, icol); 00536 char mean_col_name[256] = "ESO QC RAW "; 00537 char median_col_name[256] = "ESO QC RAW "; 00538 char minimum_col_name[256] = "ESO QC RAW "; 00539 char maximum_col_name[256] = "ESO QC RAW "; 00540 char stdev_col_name[256] = "ESO QC RAW "; 00541 strncat(mean_col_name, this_col_name, 244); 00542 strncat(mean_col_name, " MEAN", 236); 00543 cpl_propertylist_append_double 00544 (stats_stats[idet], mean_col_name, 00545 cpl_table_get_column_mean(image_stats[idet],this_col_name)); 00546 strncat(median_col_name, this_col_name, 255); 00547 strncat(median_col_name, " MEDIAN", 236); 00548 cpl_propertylist_append_double 00549 (stats_stats[idet], median_col_name, 00550 cpl_table_get_column_median(image_stats[idet],this_col_name)); 00551 strncat(minimum_col_name, this_col_name, 255); 00552 strncat(minimum_col_name, " MINIMUM", 236); 00553 cpl_propertylist_append_double 00554 (stats_stats[idet], minimum_col_name, 00555 cpl_table_get_column_min(image_stats[idet],this_col_name)); 00556 strncat(maximum_col_name, this_col_name, 255); 00557 strncat(maximum_col_name, " MAXIMUM", 236); 00558 cpl_propertylist_append_double 00559 (stats_stats[idet], maximum_col_name, 00560 cpl_table_get_column_max(image_stats[idet],this_col_name)); 00561 strncat(stdev_col_name, this_col_name, 255); 00562 strncat(stdev_col_name, " STDEV", 236); 00563 cpl_propertylist_append_double 00564 (stats_stats[idet], stdev_col_name, 00565 cpl_table_get_column_stdev(image_stats[idet],this_col_name)); 00566 } 00567 } 00568 00569 /*Free and return */ 00570 cpl_array_delete(col_names); 00571 return 0; 00572 } 00573 00574 /*----------------------------------------------------------------------------*/ 00581 /*----------------------------------------------------------------------------*/ 00582 double hawki_image_float_get_sigma_from_quartile(cpl_image * image) 00583 { 00584 int npixels; 00585 int ipix_0_25; 00586 int ipix_0_75; 00587 double first_quartil; 00588 double third_quartil; 00589 double sigma_from_quartile; 00590 float * data; 00591 00592 npixels = cpl_image_get_size_x(image) * cpl_image_get_size_y(image); 00593 data = cpl_image_get_data(image); 00594 ipix_0_25 = (int)(npixels * 0.25); 00595 ipix_0_75 = (int)(npixels * 0.75); 00596 00597 first_quartil = hawki_tools_get_kth_float(data, npixels, ipix_0_25); 00598 third_quartil = hawki_tools_get_kth_float(data, npixels, ipix_0_75); 00599 sigma_from_quartile = (third_quartil - first_quartil) / 1.35; 00600 return sigma_from_quartile; 00601 } 00602 00603 00604 /* Swap macro */ 00605 #undef SWAP 00606 #define SWAP(a,b) { register float t=(a);(a)=(b);(b)=t; } 00607 00608 float hawki_tools_get_kth_float(float * a, 00609 int n, 00610 int k) 00611 { 00612 register float x; 00613 register int i, j, l, m; 00614 00615 cpl_ensure(a, CPL_ERROR_NULL_INPUT, 0.00); 00616 00617 l=0; m=n-1; 00618 while (l<m) { 00619 x=a[k]; 00620 i=l; 00621 j=m; 00622 do { 00623 while (a[i]<x) i++; 00624 while (x<a[j]) j--; 00625 if (i<=j) { 00626 SWAP(a[i],a[j]); 00627 i++; j--; 00628 } 00629 } while (i<=j); 00630 if (j<k) l=i; 00631 if (k<i) m=j; 00632 } 00633 return a[k]; 00634 } 00635