OMEGA Pipeline Reference Manual  1.0.5
omega_stare.c
1 /* $Id: omega_stare.c,v 1.12 2012-03-29 12:17:12 agabasch Exp $
2  *
3  * This file is part of the OMEGA 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: agabasch $
23  * $Date: 2012-03-29 12:17:12 $
24  * $Revision: 1.12 $
25  * $Name: not supported by cvs2svn $
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 <math.h>
38 #include <stdlib.h>
39 
40 #include "omega_wcscor.h"
41 #include "omega_background.h"
42 #include "omega_bpm.h"
43 #include "omega_cosmic.h"
44 #include "omega_dfs.h"
45 #include "omega_flats.h"
46 #include "omega_fits.h"
47 #include "omega_pfits.h"
48 #include "omega_satellites.h"
49 #include "omega_science.h"
50 #include "omega_stats.h"
51 #include "omega_trim.h"
52 #include "omega_utils.h"
53 
54 #define RECIPE "omega_science"
55 
92 /*int omega_science_process(cpl_parameterlist *pars, int nraw, int ext)
93 {
94 
95  int i = 0;
96  double exptime = 0.0;
97  const char *tmpreduced = "omega_stare_reduced_temp.fits";
98  const char *bckname = "omega_stare_back_temp.fits";
99  const char *weight_name = "omega_stare_weight_temp.fits";
100  const char *starename = "omega_stare_temp.fits";
101  const char *smoothed = "omega_stare_smoothed.fits";
102  const char *refname,*monit_name, *extname;
103  const char *colname = NULL;
104 
105  cpl_image *bpm_img, *mask_img, *image, *weight, *weight_img;
106  cpl_image *illum_img = NULL;
107  cpl_image *backimg = NULL;
108  cpl_image *reduced = NULL;
109  cpl_image *smooth = NULL;
110  cpl_mask *satu_map,*bpm_map;
111  cpl_mask *cosmic_map, *satellite_map;
112  cpl_propertylist *plist;
113  cpl_table *phototbl, *exttbl;
114  const cpl_frame *stareframe;
115 
116  All member of ps structure are deallocated in main recipe
117 
118  Load master flat
119  ps.mflat = cpl_image_load(cpl_frame_get_filename(ps.mflatfr),CPL_TYPE_FLOAT,0,ext);
120  if (ps.mflat == NULL){
121  cpl_msg_error(cpl_func,"Cannot load Master Flat image",cpl_error_get_message());
122  return -1;
123  }
124  if(cpl_image_get_mean(ps.mflat) < 0.1){
125  cpl_msg_error(cpl_func,"Master Flat image is NULL");
126  return -1;
127  }
128 
129  Load bad pixels map
130  bpm_img = cpl_image_load(cpl_frame_get_filename(ps.bpmfr), CPL_TYPE_INT, 0, ext);
131  if(bpm_img == NULL){
132  cpl_msg_error(cpl_func, "Cannot load BPM", cpl_error_get_message());
133  return -1;
134  }
135 
136  Load optional illumination map
137  if(ps.illumfr != NULL)
138  illum_img = cpl_image_load(cpl_frame_get_filename(ps.illumfr),CPL_TYPE_FLOAT,0,ext);
139 
140  Loop through all science frames in frame set
141  stareframe = cpl_frameset_get_first_const(ps.scilist);
142 
143  for (i = 0; i < nraw; i++){
144  cpl_msg_info(cpl_func,"Reducing image %s", cpl_frame_get_filename(stareframe));
145 
146  Create a Saturated Pixels Map
147  satu_map = create_saturated_map(stareframe, omega_science_config.oc, ext, pars);
148  if (satu_map == NULL)
149  cpl_msg_warning(cpl_func,"Null saturation map");
150 
151  Create logical mask from saturated map and input bad pixels map
152  bpm_map = cpl_mask_threshold_image_create(bpm_img, 0.5, 1.5) ;
153  if(satu_map != NULL){
154  cpl_mask_and(bpm_map, satu_map);
155  freemask(satu_map);
156  }
157  mask_img = cpl_image_new_from_mask(bpm_map);
158  freemask(bpm_map);
159 
160  Create Reduced image
161  reduced = omega_reduce_science(stareframe, mask_img, pars, ext);
162  if(reduced == NULL){
163  freeimage(ps.mflat);
164  ps.mflat = NULL;
165  freeimage(mask_img);
166  freeimage(bpm_img);
167  freeimage(illum_img);
168  cpl_msg_error(cpl_func,"Cannot reduce image");
169  return -1;
170  }
171 
172  Create Cosmic Rays Map
173  image = cpl_image_cast(mask_img,CPL_TYPE_FLOAT);
174  weight = cpl_image_multiply_create(ps.mflat,image);
175  freeimage(image);
176 
177  if(ps.illumfr != NULL)
178  cpl_image_divide(weight,illum_img);
179 
180  cpl_image_power(weight, 0.5);
181  cpl_image_save(reduced, tmpreduced, BITPIX, NULL, CPL_IO_DEFAULT);
182  omega_create_background(pars, tmpreduced, bckname);
183  backimg = cpl_image_load(bckname,CPL_TYPE_FLOAT, 0, 0);
184  image = cpl_image_subtract_create(reduced,backimg);
185  cpl_image_multiply(image,weight);
186  freeimage(backimg);
187  freeimage(weight);
188 
189  cosmic_map = detcosmic(image, pars);
190  if(cosmic_map == NULL)
191  cpl_msg_warning(cpl_func,"Cannot create cosmic rays map");
192 
193  Create Satellite Tracks Map
194  FIXME: revise this function
195  satellite_map = detsat(image, pars);
196  if(satellite_map == NULL)
197  cpl_msg_warning(cpl_func,"Cannot create satellite tracks map");
198 
199  freeimage(image);
200 
201  Create Weight Map
202  weight_img = create_weightframe(ps.mflat, mask_img, cosmic_map, satellite_map);
203  if(weight_img == NULL){
204  cpl_msg_warning(cpl_func,"Cannot create weight map");
205  weight_img = NULL;
206  }
207 
208  if((illum_img != NULL) && (weight_img != NULL)) {
209  cpl_image_divide(weight_img, illum_img);
210  }
211 
212  freeimage(mask_img);
213  freemask(cosmic_map);
214  freemask(satellite_map);
215 
216  Multiply each science by its weight frame
217  // cpl_image_save(weight_img, weight_name, BITPIX, NULL, CPL_IO_DEFAULT);
218  cpl_image_multiply(reduced, weight_img);
219 
220  if(i == 0){
221  ps.reduced = cpl_image_duplicate(reduced);
222  ps.weight = cpl_image_duplicate(weight_img);
223  }
224  else{
225  cpl_image_add(ps.reduced, reduced);
226  cpl_image_add(ps.weight, weight_img);
227  }
228  freeimage(reduced);
229  freeimage(weight_img);
230 
231  Get next frame
232  stareframe = cpl_frameset_get_next_const(ps.scilist);
233  }
234 
235  freeimage(ps.mflat);
236  ps.mflat = NULL;
237  freeimage(bpm_img);
238  freeimage(illum_img);
239 
240  Weighted average of STARE frames
241  if(cpl_image_divide(ps.reduced, ps.weight) != CPL_ERROR_NONE){
242  cpl_msg_error(cpl_func,"Cannot create weighted average of images. %s", cpl_error_get_message());
243  // freeimage(sumwght);
244  dummy = 1;
245  return 1;
246  }
247 
248  Cast weight map to INT
249  ps.weight = cpl_image_cast(ps.weight,CPL_TYPE_INT);
250 
251  Correct coordinates after trimming
252  ps.astrolist = cpl_propertylist_duplicate(ps.eh);
253  if(omega_shift_refpix(ps.firstframe, ext, ps.astrolist) != 0){
254  cpl_msg_warning(cpl_func,"Cannot shift reference pixel of trimmed image");
255  freeplist(ps.astrolist);
256  ps.astrolist = NULL;
257  dummy = 1;
258  return 1;
259  }
260 
261 
262  * Calculate new zeropoint using photometric table
263 
264  photom_tbl = cpl_table_load(cpl_frame_get_filename(ps.photomfr),ext,0);
265  if(photom_tbl == NULL){
266  cpl_msg_error(cpl_func,"Cannot load photometric table. %s",cpl_error_get_message());
267  return -1;
268  }
269 
270  zeropoint = cpl_table_get_float(photom_tbl,"ZEROPOINT",0,NULL);
271  extinction = cpl_table_get_float(photom_tbl,"EXTINCTION",0,NULL);
272  omega_sci_qc.ZP = zeropoint + 2.5* log10 (omega_pfits_get_exptime(ps.eh));
273  omega_sci_qc.ZP -= extinction*omega_pfits_get_mean_airmass(ps.ph,&airmass);
274  freetable(photom_tbl);
275 
276  Add newZP to header of product
277  cpl_propertylist_update_float(ps.astrolist,"ESO QC SCI ZEROPNT",omega_sci_qc.ZP);
278  cpl_propertylist_set_comment(ps.astrolist,"ESO QC SCI ZEROPNT", "new zeropoint");
279 
280 
281  * Use pattern matching to detect stars.
282  * Smooth image before creating catalogue
283 
284  smooth = omega_smooth_image(ps.reduced, 7);
285 
286  Save star as intermediate product
287  cpl_propertylist_save(NULL,smoothed, CPL_IO_CREATE);
288  cpl_image_save(smooth, smoothed, BITPIX, ps.astrolist, CPL_IO_EXTEND);
289  freeimage(smooth);
290 
291 
292  * Calculate WCS distortion for image
293 
294  plist = omega_match_points(smoothed, ps.usnoa2, pars,
295  &omega_sci_qc.nmatches, &omega_sci_qc.mratio);
296 
297  if(plist == NULL){
298  cpl_msg_warning(cpl_func,"Error in astrometric calculation");
299  freeplist(plist);
300  dummy = 0;
301  return 1;
302  }
303 
304  Append WCS header to extension header
305  if(omega_pfits_update_header(&ps.astrolist, plist) != 0)
306  cpl_msg_debug(cpl_func,"Cannot append WCS header to extension header");
307 
308  freeplist(plist);
309 
310 
311  * NOTE: Need a secondary standard star catalogue to
312  * calculate ZP for all science frames!!!
313 
314 
315  return 0;
316 }*/
317 
318 
338 omega_fits *omega_jitter_process(omega_fits *scifits,
339  const cpl_image *mbias,
340  const cpl_image *mflat,
341  const cpl_image *bpm,
342  const cpl_table *photom,
343  const cpl_frame *usnoa2,
344  const cpl_image *illum,
345  const cpl_image *fringes,
346  const cpl_image *nsky,
347  double * zeropoint_final,
348  cpl_parameterlist *pars, int ext)
349 {
350 
351  int i = 0;
352  int oc = 0;
353  int sub_back = 0;
354  int nmatches = 0;
355  double ZP = 0.0;
356  double zeropoint = 0.0;
357  double zeropoint_error = 0.0;
358  double extinction = 0.0;
359  double median = 1.0;
360  double mratio = 0.0;
361  double exptime = 0.0;
362  double airmass = 0.0;
363  double gain = 1.0;
364  const char *smoothed = "omega_sci_smoothed.fits";
365  const char *smoothed_weight = "omega_sci_smoothed_weight.fits";
366 
367  cpl_image *mask_img, *image, *trim, *reduced, *combmap;
368  cpl_image *back = NULL;
369  cpl_image *smooth = NULL;
370  cpl_mask *satu_map,*bpm_map, *mask;
371  cpl_mask *cosmic, *satellite;
372  cpl_propertylist *plist, *astrolist, *wcslist;
373  cpl_parameter *p;
374  omega_fits *simplefits;
375  double mag_zpt_no_std_table=0.0;
376  double mag_zpt_err_no_std_table=0.0;
377  double ext_no_std_table=0.0;
378 
379  if((scifits == NULL) || (mbias == NULL) || (mflat == NULL) ||
380  (bpm == NULL) || (usnoa2 == NULL) || (pars == NULL))
381  return NULL;
382 
383 
384 /* if(cpl_image_get_mean(mflat) < 0.1){
385  cpl_msg_error(cpl_func,"Master Flat image is NULL");
386  return NULL;
387  }*/
388 
389  /* Get a few parameters */
390  p = cpl_parameterlist_find(pars, "omega.omega_science.OverscanMethod") ;
391  oc = cpl_parameter_get_int(p);
392  p = cpl_parameterlist_find(pars,"omega.omega_science.SubtractBackground");
393  sub_back = cpl_parameter_get_bool(p);
394 
395 
396  p = cpl_parameterlist_find(pars, "omega.omega_science.mag_zpt_no_std_table") ;
397  mag_zpt_no_std_table = cpl_parameter_get_double(p);
398  p = cpl_parameterlist_find(pars,"omega.omega_science.mag_zpt_err_no_std_table");
399  mag_zpt_err_no_std_table = cpl_parameter_get_double(p);
400  p = cpl_parameterlist_find(pars,"omega.omega_science.ext_no_std_table");
401  ext_no_std_table = cpl_parameter_get_double(p);
402 
403 
404  /* Calculate new zeropoint using photometric table or use default values*/
405 
406  if(photom != NULL){
407  zeropoint = cpl_table_get_double(photom,"ZEROPOINT",0,NULL);
408  zeropoint_error = cpl_table_get_double(photom, "ZEROPOINT_ERR", 0, NULL);
409  extinction = cpl_table_get_double(photom,"EXTINCTION",0,NULL);
410  }
411  else {
412  zeropoint = mag_zpt_no_std_table;
413  zeropoint_error = mag_zpt_err_no_std_table;
414  extinction = ext_no_std_table;
415  }
416  /* Trim and overscan correct all images */
417  if((trim = omega_trim_oscan_correct(scifits, oc)) == NULL){
418  cpl_msg_error(cpl_func,"Unable to trim image");
419  return NULL;
420  }
421 
422  /* Correct coordinates after trimming */
423  astrolist = omega_fits_get_ehu(scifits);
424  omega_shift_refpix(omega_fits_get_frame(scifits), ext, astrolist);
425 
426  /* Add newZP to header of product */
427  plist = omega_fits_get_phu(scifits);
428  if((exptime = omega_pfits_get_exptime(astrolist)) <= 0.0)
429  exptime = omega_pfits_get_exptime(plist);
430 
431 
432  ZP = zeropoint + 2.5* log10(exptime);
433  omega_pfits_get_mean_airmass(plist,&airmass);
434  ZP -= extinction*airmass;
435 
436  cpl_propertylist_update_double(astrolist,"ESO QC SCI ZEROPNT",ZP);
437  cpl_propertylist_set_comment(astrolist,"ESO QC SCI ZEROPNT", "new zp with flux in ADU");
438  cpl_propertylist_update_double(astrolist, "ESO QC SCI ZEROPNT ERR", zeropoint_error) ;
439  cpl_propertylist_set_comment(astrolist, "ESO QC SCI ZEROPNT ERR", "zp error");
440 
441 
442  /*Get gain*/
443  omega_pfits_get_conad(astrolist,&gain);
444 
445  cpl_propertylist_update_double(astrolist,"ESO QC SCI ZEROPNT_ELECTRON",ZP+(2.5*log10(gain)));
446  cpl_propertylist_set_comment(astrolist,"ESO QC SCI ZEROPNT_ELECTRON", "new zp with flux in e-");
447  cpl_propertylist_update_double(astrolist, "ESO QC SCI ZEROPNT_ELECTRON ERR", zeropoint_error) ;
448  cpl_propertylist_set_comment(astrolist, "ESO QC SCI ZEROPNT_ELECTRON ERR", "zp error");
449 
450  *zeropoint_final=ZP;
451 
452  /* Subtract the bias */
453  omega_biascor(trim, mbias);
454 
455  /* Divide by the flat */
456  if(omega_flatcor(trim, mflat, nsky) != 0){
457  cpl_msg_error(cpl_func,"Error in flat correction");
458  freeimage(trim);
459  plist = NULL;
460  astrolist = NULL;
461  return NULL;
462  }
463 
464  /* Create BPM+SATURATION mask */
465  satu_map = omega_saturated_map(trim, oc, pars);
466 
467  bpm_map = cpl_mask_threshold_image_create(bpm, 0.5, 1.5) ;
468  if(satu_map != NULL){
469 // cpl_mask_and(bpm_map, satu_map);
470  cpl_mask_or(bpm_map, satu_map);
471  freemask(satu_map);
472  }
473  mask_img = cpl_image_new_from_mask(bpm_map);
474  /*Add the bad pixel mask to the image*/
475  cpl_image_reject_from_mask(trim, bpm_map);
476  freemask(bpm_map);
477 
478  /* Correct for fringing */
479  if(fringes != NULL){
480  omega_fringecor(trim, fringes, mask_img, pars);
481  }
482 
483  /* Get background */
484  back = omega_background(trim, pars);
485 
486  if(back != NULL){
487  median = cpl_image_get_median(back);
488  cpl_propertylist_update_double(astrolist, "ESO DRS MEDIAN BKG", median);
489  }
490  if(back == NULL)
491  cpl_msg_debug(cpl_func,"Unable to create background image");
492 
493  /* Detect cosmic rays */
494  image = cpl_image_subtract_create(trim, back);
495  cosmic = omega_cosmic_rays(image,mflat,illum,mask_img,pars);
496 
497  /* Subtract background from science */
498  if(sub_back == 1){
499  freeimage(trim);
500  trim = cpl_image_duplicate(image);
501  }
502 
503  freeimage(image);
504  freeimage(back);
505 
506  /* Detect satellite tracks in background subtracted image */
507  satellite = detsat(trim, pars);
508 
509  /* Create combination map (BPM,saturated,cosmic,satellite) */
510  /* The bad pixels are set to CPL_BINARY_0 in combmap */
511  combmap = create_weightframe(mflat,mask_img,cosmic,satellite);
512 // cpl_image_save(combmap,"combmap.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_CREATE);
513 
514  freemask(satellite);
515  freemask(cosmic);
516  freeimage(mask_img);
517 
518  /* Save median value of image in header */
519  mask = cpl_mask_threshold_image_create(combmap, 0.5, 1.5);
520  cpl_mask_not(mask);
521  cpl_image_reject_from_mask(trim, mask);
522  median = cpl_image_get_median(trim);
523  cpl_propertylist_update_double(astrolist, "ESO DRS MEDIAN", median);
524 
525 
526  /* Clean bad pixels */
527  if(cpl_detector_interpolate_rejected(trim) != CPL_ERROR_NONE)
528  cpl_msg_debug(cpl_func,"Cannot clean rejected pixels. %s",cpl_error_get_message());
529 
530  /*The image should remember the bad pixels even after interpolation!*/
531 
532 
533  /*
534  * Use pattern matching to detect stars.
535  */
536 
537  /* Smooth image before creating catalogue */
538  smooth = omega_smooth_image(trim, 1);
539 
540  cpl_image_reject_from_mask(trim, mask);
541  freemask(mask);
542 
543  /* Save star as intermediate product */
544  cpl_propertylist_save(plist,smoothed, CPL_IO_CREATE);
545  cpl_image_save(smooth, smoothed, CPL_BPP_IEEE_FLOAT, astrolist, CPL_IO_EXTEND);
546 
547  /* Save weight map as intermediate product */
548  cpl_propertylist_save(plist,smoothed_weight, CPL_IO_CREATE);
549  cpl_image_save(combmap, smoothed_weight, CPL_BPP_IEEE_FLOAT, astrolist, CPL_IO_EXTEND);
550 
551  freeimage(smooth);
552  freeimage(combmap);
553  /*
554  * Calculate WCS distortion for simple image
555  */
556  wcslist = omega_match_points(smoothed, smoothed_weight, usnoa2, pars,ext,&nmatches, &mratio);
557 
558  /* Debugging information */
559  cpl_msg_debug(cpl_func,"Number matches between USNOA2 cat and stars. %d", nmatches);
560  cpl_msg_debug(cpl_func,"ratio USNOA2 matches/detected stars. %g", mratio);
561 
562  if(wcslist == NULL){
563  cpl_msg_warning(cpl_func,"Unable to calculate WCS distortion for image. %s",
564  cpl_error_get_message());
565  }
566 
567  /* Append WCS header to extension header */
568  omega_pfits_update_header(astrolist, wcslist);
569  freeplist(wcslist);
570 
571  /* Wrap product into omega fits structure using 1st frame headers */
572  simplefits = omega_fits_wrap(trim,NULL,plist,astrolist);
573 
574 // plist = NULL;
575 // astrolist = NULL;
576 
577  return simplefits;
578 
579 }
580 
581 /*
582  * Copied from omega_reduce_std()
583  */
584 /*
585 cpl_image *omega_reduce_science(const cpl_frame *sciframe, cpl_image *mask_img,
586  cpl_parameterlist *pars, int ext)
587 {
588 
589  const char *cpl_func = "make_std_reduced";
590  const char *bckname = "omega_background_temp.fits";
591  const char *tempname1111 = "omega_reduced_temp.fits";
592  cpl_image *trim_img, *mbias_img, *mflat_img, *nsky_img;
593  cpl_image *back_img = NULL;
594  cpl_image *illum_img = NULL;
595  cpl_image *fringe_img = NULL;
596  cpl_image *reduced = NULL;
597  cpl_mask *bpmtemp_map;
598 
599 
600  Trim and overscan correct image
601  trim_img = TrimOscanCorrect(sciframe, omega_science_config.oc, ext);
602  if (trim_img == NULL) {
603  cpl_msg_error(cpl_func,"Cannot trim image %s",cpl_error_get_message());
604  return NULL;
605  }
606 
607  Subtract the bias
608  mbias_img = cpl_image_load(cpl_frame_get_filename(ps.mbiasfr), CPL_TYPE_FLOAT,0,ext);
609  if (mbias_img == NULL) {
610  cpl_msg_error(cpl_func,"Cannot load master bias %s", cpl_error_get_message());
611  freeimage(trim_img);
612  return NULL;
613  }
614  cpl_image_subtract(trim_img, mbias_img);
615  freeimage(mbias_img);
616 
617 
618  Correct for flat field. If there is a night sky flat, use it.
619  if(ps.nskyfr != NULL){
620  nsky_img = cpl_image_load(cpl_frame_get_filename(ps.nskyfr), CPL_TYPE_FLOAT, 0, ext);
621  if(nsky_img != NULL){
622  mflat_img = cpl_image_multiply_create(ps.mflat, nsky_img);
623  cpl_image_divide(trim_img, mflat_img);
624  freeimage(nsky_img);
625  freeimage(mflat_img);
626  }
627  else {
628  cpl_image_divide(trim_img, ps.mflat);
629  }
630  }
631  else {
632  cpl_image_divide(trim_img, ps.mflat);
633  }
634 
635  Correct for fringing. Check if filter really requires fringing correction
636  FIXME: should this be a fatal error or a warning?
637  if(ps.fringesfr != NULL){
638  fringe_img = cpl_image_load(cpl_frame_get_filename(ps.fringesfr),CPL_TYPE_FLOAT,0,ext);
639  if(fringe_img == NULL) {
640  cpl_msg_warning(cpl_func,"Cannot load Master Fringe image");
641  reduced = cpl_image_duplicate(trim_img);
642  }
643  else {
644  bpmtemp_map = cpl_mask_threshold_image_create(mask_img, 0.5, 1.5);
645  cpl_msg_info(cpl_func,"Applying fringing correction");
646  reduced = omega_fringecor(trim_img,fringe_img, bpmtemp_map, pars);
647  if(reduced == NULL) {
648  cpl_msg_warning(cpl_func,"Cannot apply fringing correction");
649  reduced = cpl_image_duplicate(trim_img);
650  }
651  freemask(bpmtemp_map);
652  freeimage(fringe_img);
653  }
654 
655  }
656  else {
657  reduced = cpl_image_duplicate(trim_img);
658  }
659 
660  freeimage(trim_img);
661 
662  FIXME: review this
663  Illumination Correction and Background subtraction
664  // iillum = (illum == NULL) ? 0 : 1;
665 
666  if((ps.illumfr != NULL) && (omega_science_config.bckg == 0)){
667 
668  // if (!(iillum == 0 && bckg == 1)) {
669  Save temporary image to create a background in Sextractor
670  cpl_image_save(reduced, tempname1111, BITPIX, NULL, CPL_IO_DEFAULT);
671 
672  cpl_msg_info("","Measuring statistics with background");
673 
674  stats = omega_iter_stat_opts(reduced_img, 5.0, 5);
675  cpl_msg_info("","Mean :%10.3f",cpl_stats_get_mean(stats));
676  cpl_msg_info("","Median :%10.3f",cpl_stats_get_median(stats));
677  cpl_msg_info("","Stdev :%10.3f",cpl_stats_get_stdev(stats));
678 
679  cpl_msg_info(cpl_func,"Doing background subtraction");
680  if(omega_create_background(pars, tempname1111, bckname) != 0){
681  cpl_msg_error(cpl_func,"Cannot create background image");
682  freeimage(reduced);
683  return NULL;
684  }
685 
686  back_img = cpl_image_load(bckname, CPL_TYPE_FLOAT, 0, 0);
687  if(back_img == NULL) {
688  cpl_msg_warning(cpl_func,"Cannot load background image %s", bckname);
689  freeimage(reduced);
690  return NULL;
691  }
692 
693  cpl_image_subtract(reduced, back_img);
694  cpl_stats_delete(stats);
695  }
696 
697 
698  // if(iillum == 1) {
699  if (ps.illumfr != NULL){
700  illum_img = cpl_image_load(cpl_frame_get_filename(ps.illumfr),CPL_TYPE_FLOAT, 0, ext);
701  if(illum_img != NULL) {
702  cpl_msg_info(cpl_func,"Applying illumination correction");
703  cpl_image_multiply(reduced, illum_img);
704  freeimage(illum_img);
705  }
706  else{
707  cpl_msg_warning(cpl_func,"Cannot load Illumination Correction image");
708  }
709  if(omega_std_config.bckg == 1) {
710  cpl_msg_info("","Restoring background to image");
711  cpl_image_add(reduced, back_img);
712  }
713  }
714 
715 
716  if (back_img != NULL){
717  freeimage(back_img);
718  }
719 
720  return reduced;
721 
722 
723 }
724 */
725 
726