HAWKI Pipeline Reference Manual  1.8.12
hawki_image_stats.c
1 /* $Id: hawki_image_stats.c,v 1.8 2012/05/03 10:42:32 cgarcia Exp $
2  *
3  * This file is part of the HAWKI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2012/05/03 10:42:32 $
24  * $Revision: 1.8 $
25  * $Name: hawki-1_8_12 $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <string.h>
37 #include <cpl.h>
38 
39 #include "hawki_image_stats.h"
40 #include "hawki_dfs.h"
41 #include "hawki_utils.h"
42 #include "hawki_load.h"
43 
44 /*----------------------------------------------------------------------------*/
45 /* Private functions */
46 /*----------------------------------------------------------------------------*/
47 float hawki_tools_get_kth_float(float * a,
48  int n,
49  int k);
50 
51 /*----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------*/
56 
59 /*----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------*/
70 (cpl_table ** raw_stats)
71 {
72  int idet;
73  /* Error state variables */
74  cpl_errorstate prestate = cpl_errorstate_get();
75 
76  /* Check inputs */
77  if(raw_stats == NULL)
78  return -1;
79  for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
80  {
81  if(raw_stats[idet] == NULL)
82  return -1;
83  }
84 
85  /* Creates the proper columns */
86  for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
87  {
88  cpl_table_new_column
89  (raw_stats[idet],HAWKI_COL_STAT_MIN,CPL_TYPE_DOUBLE);
90  cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_MIN,"ADU");
91  cpl_table_new_column
92  (raw_stats[idet],HAWKI_COL_STAT_MAX,CPL_TYPE_DOUBLE);
93  cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_MAX,"ADU");
94  cpl_table_new_column
95  (raw_stats[idet],HAWKI_COL_STAT_MED,CPL_TYPE_DOUBLE);
96  cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_MED,"ADU");
97  cpl_table_new_column
98  (raw_stats[idet],HAWKI_COL_STAT_MEAN,CPL_TYPE_DOUBLE);
99  cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_MEAN,"ADU");
100  cpl_table_new_column
101  (raw_stats[idet],HAWKI_COL_STAT_RMS,CPL_TYPE_DOUBLE);
102  cpl_table_set_column_unit(raw_stats[idet],HAWKI_COL_STAT_RMS,"ADU");
103  cpl_table_new_column
104  (raw_stats[idet],HAWKI_COL_STAT_USED,CPL_TYPE_INT);
105 
106  }
107  /* Check error status and exit */
108  if(!cpl_errorstate_is_equal(prestate))
109  return -1;
110  return 0;
111 }
112 
113 /*----------------------------------------------------------------------------*/
135 /*----------------------------------------------------------------------------*/
137 (cpl_table ** image_stats,
138  const cpl_image * image,
139  int llx,
140  int lly,
141  int urx,
142  int ury,
143  int idet,
144  int irow)
145 {
146  /* stats variables */
147  double minval;
148  double maxval;
149  double median;
150  double stdev;
151  double mean;
152  cpl_stats * stats_ima ;
153 
154  /* Error state variables */
155  cpl_errorstate prestate = cpl_errorstate_get();
156 
157  /* Checking input */
158  if(image_stats == NULL || image == NULL)
159  return -1;
160 
161  /* Compute statistics */
162  stats_ima = cpl_stats_new_from_image_window
163  (image, CPL_STATS_ALL, llx, lly, urx, ury) ;
164  if(stats_ima == NULL)
165  return -1;
166 
167  /* Get the stats from the storage structure */
168  minval = cpl_stats_get_min(stats_ima);
169  maxval = cpl_stats_get_max(stats_ima);
170  median = cpl_stats_get_median(stats_ima);
171  stdev = cpl_stats_get_stdev(stats_ima);
172  mean = cpl_stats_get_mean(stats_ima);
173  cpl_stats_delete(stats_ima);
174 
175  /* Store in table */
176  cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_MIN,
177  irow, minval);
178  cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_MAX,
179  irow, maxval);
180  cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_MED,
181  irow, median);
182  cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_MEAN,
183  irow, mean);
184  cpl_table_set_double(image_stats[idet], HAWKI_COL_STAT_RMS,
185  irow, stdev) ;
186  cpl_table_set_int(image_stats[idet], HAWKI_COL_STAT_USED,
187  irow, 1) ;
188 
189  /* Check error status and exit */
190  if(!cpl_errorstate_is_equal(prestate))
191  return -1;
192  return 0;
193 }
194 
195 int hawki_image_stats_odd_even_column_row_fill_from_image
196 (cpl_table ** odd_column_stats,
197  cpl_table ** even_column_stats,
198  cpl_table ** odd_row_stats,
199  cpl_table ** even_row_stats,
200  const cpl_image * image,
201  int idet,
202  int irow)
203 {
204  /* stats variables */
205  int i;
206  int j;
207  int nx;
208  int ny;
209  double minval;
210  double maxval;
211  double median;
212  double stdev;
213  double mean;
214  cpl_stats * stats_ima;
215  cpl_image * tmp_ima;
216  cpl_mask * mask;
217 
218  /* Error state variables */
219  cpl_errorstate prestate = cpl_errorstate_get();
220 
221  /* Checking input */
222  if(odd_column_stats == NULL ||
223  even_column_stats == NULL ||
224  odd_row_stats == NULL ||
225  even_row_stats == NULL ||
226  image == NULL)
227  return -1;
228 
229  /* Copying the target image */
230  tmp_ima = cpl_image_duplicate(image);
231  nx = cpl_image_get_size_x(tmp_ima);
232  ny = cpl_image_get_size_y(tmp_ima);
233 
234  /* Compute statistics odd column */
235  mask = cpl_image_get_bpm(tmp_ima);
236  for(i=0 ; i < nx ; ++i)
237  {
238  if((i+1) % 2)
239  for(j=0 ; j < ny ; ++j)
240  {
241  cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1);
242  }
243  }
244  stats_ima = cpl_stats_new_from_image
245  (tmp_ima, CPL_STATS_ALL);
246  if(stats_ima == NULL)
247  {
248  cpl_image_delete(tmp_ima);
249  return -1;
250  }
251 
252  /* Get the stats from the storage structure */
253  minval = cpl_stats_get_min(stats_ima);
254  maxval = cpl_stats_get_max(stats_ima);
255  median = cpl_stats_get_median(stats_ima);
256  stdev = cpl_stats_get_stdev(stats_ima);
257  mean = cpl_stats_get_mean(stats_ima);
258  cpl_stats_delete(stats_ima);
259 
260  /* Store in table */
261  cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_MIN,
262  irow, minval);
263  cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_MAX,
264  irow, maxval);
265  cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_MED,
266  irow, median);
267  cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_MEAN,
268  irow, mean);
269  cpl_table_set_double(odd_column_stats[idet], HAWKI_COL_STAT_RMS,
270  irow, stdev) ;
271  cpl_table_set_int(odd_column_stats[idet], HAWKI_COL_STAT_USED,
272  irow, 1) ;
273 
274  /* Compute statistics even column */
275  //cpl_image_reject_from_mask();
276  cpl_image_accept_all(tmp_ima);
277  mask = cpl_image_get_bpm(tmp_ima);
278  for(i=0 ; i < nx ; ++i)
279  {
280  if(i % 2)
281  for(j=0 ; j < ny ; ++j)
282  {
283  cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1);
284  }
285  }
286  stats_ima = cpl_stats_new_from_image
287  (tmp_ima, CPL_STATS_ALL);
288  if(stats_ima == NULL)
289  {
290  cpl_image_delete(tmp_ima);
291  return -1;
292  }
293 
294  /* Get the stats from the storage structure */
295  minval = cpl_stats_get_min(stats_ima);
296  maxval = cpl_stats_get_max(stats_ima);
297  median = cpl_stats_get_median(stats_ima);
298  stdev = cpl_stats_get_stdev(stats_ima);
299  mean = cpl_stats_get_mean(stats_ima);
300  cpl_stats_delete(stats_ima);
301 
302  /* Store in table */
303  cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_MIN,
304  irow, minval);
305  cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_MAX,
306  irow, maxval);
307  cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_MED,
308  irow, median);
309  cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_MEAN,
310  irow, mean);
311  cpl_table_set_double(even_column_stats[idet], HAWKI_COL_STAT_RMS,
312  irow, stdev) ;
313  cpl_table_set_int(even_column_stats[idet], HAWKI_COL_STAT_USED,
314  irow, 1) ;
315 
316  /* Compute statistics odd rows */
317  cpl_image_accept_all(tmp_ima);
318  mask = cpl_image_get_bpm(tmp_ima);
319  for(j=0 ; j < ny ; ++j)
320  {
321  if((j+1) % 2)
322  for(i=0 ; i < nx ; ++i)
323  {
324  cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1);
325  }
326  }
327  stats_ima = cpl_stats_new_from_image
328  (tmp_ima, CPL_STATS_ALL) ;
329  if(stats_ima == NULL)
330  {
331  cpl_image_delete(tmp_ima);
332  return -1;
333  }
334 
335  /* Get the stats from the storage structure */
336  minval = cpl_stats_get_min(stats_ima);
337  maxval = cpl_stats_get_max(stats_ima);
338  median = cpl_stats_get_median(stats_ima);
339  stdev = cpl_stats_get_stdev(stats_ima);
340  mean = cpl_stats_get_mean(stats_ima);
341  cpl_stats_delete(stats_ima);
342 
343  /* Store in table */
344  cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_MIN,
345  irow, minval);
346  cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_MAX,
347  irow, maxval);
348  cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_MED,
349  irow, median);
350  cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_MEAN,
351  irow, mean);
352  cpl_table_set_double(odd_row_stats[idet], HAWKI_COL_STAT_RMS,
353  irow, stdev) ;
354  cpl_table_set_int(odd_row_stats[idet], HAWKI_COL_STAT_USED,
355  irow, 1) ;
356 
357  /* Compute statistics even row */
358  cpl_image_accept_all(tmp_ima);
359  mask = cpl_image_get_bpm(tmp_ima);
360  for(j=0 ; j < ny ; ++j)
361  {
362  if(j % 2)
363  for(i=0 ; i < nx ; ++i)
364  {
365  cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1);
366  }
367  }
368  stats_ima = cpl_stats_new_from_image
369  (tmp_ima, CPL_STATS_ALL) ;
370  if(stats_ima == NULL)
371  {
372  cpl_image_delete(tmp_ima);
373  return -1;
374  }
375 
376  /* Get the stats from the storage structure */
377  minval = cpl_stats_get_min(stats_ima);
378  maxval = cpl_stats_get_max(stats_ima);
379  median = cpl_stats_get_median(stats_ima);
380  stdev = cpl_stats_get_stdev(stats_ima);
381  mean = cpl_stats_get_mean(stats_ima);
382  cpl_stats_delete(stats_ima);
383 
384  /* Store in table */
385  cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_MIN,
386  irow, minval);
387  cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_MAX,
388  irow, maxval);
389  cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_MED,
390  irow, median);
391  cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_MEAN,
392  irow, mean);
393  cpl_table_set_double(even_row_stats[idet], HAWKI_COL_STAT_RMS,
394  irow, stdev) ;
395  cpl_table_set_int(even_row_stats[idet], HAWKI_COL_STAT_USED,
396  irow, 1) ;
397 
398  /* Free */
399  cpl_image_delete(tmp_ima);
400 
401  /* Check error status and exit */
402  if(!cpl_errorstate_is_equal(prestate))
403  return -1;
404  return 0;
405 }
406 
407 /*----------------------------------------------------------------------------*/
428 /*----------------------------------------------------------------------------*/
430 (cpl_table ** image_stats,
431  const cpl_frame * frame,
432  int irow)
433 {
434  int idet;
435  cpl_imagelist * images;
436 
437  /* Loading the four chips */
438  images = hawki_load_frame(frame, CPL_TYPE_FLOAT);
439  if(images == NULL)
440  {
441  cpl_msg_error(__func__,"Could not read file %s",
442  cpl_frame_get_filename(frame));
443  return -1;
444  }
445 
446  for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
447  {
448  int nx, ny;
449  nx = cpl_image_get_size_x(cpl_imagelist_get(images,idet));
450  ny = cpl_image_get_size_y(cpl_imagelist_get(images,idet));
452  (image_stats,
453  cpl_imagelist_get(images,idet),
454  1, 1, nx, ny, idet, irow);
455  }
456 
457  /* Free and exit */
458  cpl_imagelist_delete(images);
459  return 0;
460 }
461 
462 int hawki_image_stats_print
463 (cpl_table ** table_stats)
464 {
465  int idet;
466  int istat;
467 
468  /* Print header */
469  cpl_msg_info(__func__, "Stats summary") ;
470 
471  /* Loop on detectors */
472  cpl_msg_indent_more();
473  for( idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
474  {
475 
476  /* Chip header */
477  cpl_msg_info(__func__, "Chip number %d", idet+1) ;
478  cpl_msg_info(__func__, "image min max med rms") ;
479  cpl_msg_info(__func__, "--------------------------------------------") ;
480 
481  /* Loop on images */
482  for(istat = 0; istat < cpl_table_get_nrow(table_stats[idet]); ++istat)
483  {
484  cpl_msg_info(__func__, "%02d %10.2f %10.2f %10.2f %10.2f",
485  istat+1,
486  cpl_table_get_double(table_stats[idet],
487  HAWKI_COL_STAT_MIN,istat,NULL),
488  cpl_table_get_double(table_stats[idet],
489  HAWKI_COL_STAT_MAX,istat,NULL),
490  cpl_table_get_double(table_stats[idet],
491  HAWKI_COL_STAT_MED,istat,NULL ),
492  cpl_table_get_double(table_stats[idet],
493  HAWKI_COL_STAT_RMS,istat,NULL ));
494  }
495  }
496  cpl_msg_indent_less();
497  return 0;
498 }
499 
500 /*----------------------------------------------------------------------------*/
509 /*----------------------------------------------------------------------------*/
510 
512 (cpl_table ** image_stats,
513  cpl_propertylist ** stats_stats)
514 {
515  cpl_array * col_names;
516  int idet;
517  int icol;
518 
519  /* Check entries */
520  if(image_stats == NULL || stats_stats == NULL)
521  return -1;
522 
523  /* Fill the name of the interesting columns */
524  col_names = cpl_array_new(5, CPL_TYPE_STRING);
525  cpl_array_set_string(col_names, 0, HAWKI_COL_STAT_MIN);
526  cpl_array_set_string(col_names, 1, HAWKI_COL_STAT_MAX);
527  cpl_array_set_string(col_names, 2, HAWKI_COL_STAT_MED);
528  cpl_array_set_string(col_names, 3, HAWKI_COL_STAT_MEAN);
529  cpl_array_set_string(col_names, 4, HAWKI_COL_STAT_RMS);
530 
531  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; ++idet)
532  {
533  for(icol = 0;icol < 5; ++icol)
534  {
535  const char * this_col_name = cpl_array_get_string(col_names, icol);
536  char mean_col_name[256] = "ESO QC RAW ";
537  char median_col_name[256] = "ESO QC RAW ";
538  char minimum_col_name[256] = "ESO QC RAW ";
539  char maximum_col_name[256] = "ESO QC RAW ";
540  char stdev_col_name[256] = "ESO QC RAW ";
541  strncat(mean_col_name, this_col_name, 244);
542  strncat(mean_col_name, " MEAN", 236);
543  cpl_propertylist_append_double
544  (stats_stats[idet], mean_col_name,
545  cpl_table_get_column_mean(image_stats[idet],this_col_name));
546  strncat(median_col_name, this_col_name, 255);
547  strncat(median_col_name, " MEDIAN", 236);
548  cpl_propertylist_append_double
549  (stats_stats[idet], median_col_name,
550  cpl_table_get_column_median(image_stats[idet],this_col_name));
551  strncat(minimum_col_name, this_col_name, 255);
552  strncat(minimum_col_name, " MINIMUM", 236);
553  cpl_propertylist_append_double
554  (stats_stats[idet], minimum_col_name,
555  cpl_table_get_column_min(image_stats[idet],this_col_name));
556  strncat(maximum_col_name, this_col_name, 255);
557  strncat(maximum_col_name, " MAXIMUM", 236);
558  cpl_propertylist_append_double
559  (stats_stats[idet], maximum_col_name,
560  cpl_table_get_column_max(image_stats[idet],this_col_name));
561  strncat(stdev_col_name, this_col_name, 255);
562  strncat(stdev_col_name, " STDEV", 236);
563  cpl_propertylist_append_double
564  (stats_stats[idet], stdev_col_name,
565  cpl_table_get_column_stdev(image_stats[idet],this_col_name));
566  }
567  }
568 
569  /*Free and return */
570  cpl_array_delete(col_names);
571  return 0;
572 }
573 
574 /*----------------------------------------------------------------------------*/
581 /*----------------------------------------------------------------------------*/
583 {
584  int npixels;
585  int ipix_0_25;
586  int ipix_0_75;
587  double first_quartil;
588  double third_quartil;
589  double sigma_from_quartile;
590  float * data;
591 
592  npixels = cpl_image_get_size_x(image) * cpl_image_get_size_y(image);
593  data = cpl_image_get_data(image);
594  ipix_0_25 = (int)(npixels * 0.25);
595  ipix_0_75 = (int)(npixels * 0.75);
596 
597  first_quartil = hawki_tools_get_kth_float(data, npixels, ipix_0_25);
598  third_quartil = hawki_tools_get_kth_float(data, npixels, ipix_0_75);
599  sigma_from_quartile = (third_quartil - first_quartil) / 1.35;
600  return sigma_from_quartile;
601 }
602 
603 
604 /* Swap macro */
605 #undef SWAP
606 #define SWAP(a,b) { register float t=(a);(a)=(b);(b)=t; }
607 
608 float hawki_tools_get_kth_float(float * a,
609  int n,
610  int k)
611 {
612  register float x;
613  register int i, j, l, m;
614 
615  cpl_ensure(a, CPL_ERROR_NULL_INPUT, 0.00);
616 
617  l=0; m=n-1;
618  while (l<m) {
619  x=a[k];
620  i=l;
621  j=m;
622  do {
623  while (a[i]<x) i++;
624  while (x<a[j]) j--;
625  if (i<=j) {
626  SWAP(a[i],a[j]);
627  i++; j--;
628  }
629  } while (i<=j);
630  if (j<k) l=i;
631  if (k<i) m=j;
632  }
633  return a[k];
634 }
635