VISIR Pipeline Reference Manual  4.1.0
visir_img_phot.c
1 /*
2  * This file is part of the VISIR Pipeline
3  * Copyright (C) 2002,2003,2013,2014 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 
25 /*-----------------------------------------------------------------------------
26  Includes
27  -----------------------------------------------------------------------------*/
28 
29 #include "visir_recipe.h"
30 #include "visir_pfits.h"
31 #include "irplib_strehl.h"
32 #include "irplib_framelist.h"
33 
34 #include <string.h>
35 #include <float.h>
36 
37 #define SQR(a) ((a) * (a))
38 
39 /*-----------------------------------------------------------------------------
40  Define
41  -----------------------------------------------------------------------------*/
42 
43 #define VISIR_IMG_PHOT_POS_WARN 10.0
44 #define VISIR_IMG_PHOT_POS_ERROR 25.0
45 #define VISIR_IMG_PHOT_POS_UNCERTAINTY 10.0
46 #define STREHL_M1 8.0
47 #define STREHL_M2 1.1
48 #define STREHL_BOX_SIZE 64
49 #define STREHL_STAR_RADIUS 2.0
50 #define STREHL_BG_R1 2.0
51 #define STREHL_BG_R2 3.0
52 
53 #define RECIPE_STRING "visir_old_img_phot"
54 /* still used by chain recipe internally, so keep name for output */
55 #define RECIPE_SAVE_STRING "visir_img_phot"
56 
57 
58 /* FITS keys to be loaded for all raw files */
59 #define RECIPE_KEYS_REGEXP_ALL \
60  VISIR_PFITS_REGEXP_IMG_RECOMBINE \
61  "|" VISIR_PFITS_REGEXP_IMG_SENSIT
62 
63 /* FITS keys to be loaded for first raw file */
64 #define RECIPE_KEYS_REGEXP \
65  RECIPE_KEYS_REGEXP_ALL \
66  "|" VISIR_PFITS_IMG_PHOT_COPY \
67  "|" VISIR_PFITS_REGEXP_IMG_PHOT_PAF
68 
69 /* FITS keys to be loaded for first raw file, in case WCS is used */
70 #define RECIPE_KEYS_REGEXP_WCS \
71  RECIPE_KEYS_REGEXP \
72  "|" IRPLIB_PFITS_WCS_REGEXP
73 
74 
75 /*-----------------------------------------------------------------------------
76  Private Functions prototypes
77  -----------------------------------------------------------------------------*/
78 
79 typedef struct beamshape_ {
80  /* simple fwhm in x and y from cpl, very inaccurate */
81  double fwhm_x, fwhm_y;
82  /* fwhm from gauss fit */
83  double peak, peak_err;
84  double major, major_err;
85  double minor, minor_err;
86  double angle, angle_err;
87 } beamshape_t;
88 
89 
90 static double visir_img_phot_eccent_four(const cpl_apertures *, int, int,
91  const cpl_apertures *, int, int);
92 
93 static int visir_img_phot_flux_one(const cpl_image *, const cpl_image *,
94  const cpl_image *, const cpl_propertylist *);
95 #ifdef VISIR_IMG_PHOT_USE_ECCENT_THREE
96 static double visir_img_phot_eccent_three(const cpl_apertures *, int,
97  const cpl_apertures *, int, int);
98 #endif
99 
100 static double visir_img_phot_jy_from_cat(const char *, const char *, double,
101  double, const char *);
102 static int visir_img_phot_sensit(const cpl_image *, const cpl_image *,
103  const cpl_image *, const irplib_framelist *,
104  const char *, cpl_boolean, cpl_propertylist *);
105 static int visir_img_phot_flux_three(const cpl_image *);
106 static int visir_img_phot_flux_four(const cpl_image *);
107 static int visir_img_phot_flux(const cpl_image *, const cpl_image *,
108  const cpl_image *, double,
109  double, int, int, int, double *,
110  double *, int *, double *, beamshape_t *);
111 
112 static cpl_error_code visir_get_filter_infos(const char *, double *, double *);
113 
114 static cpl_error_code visir_img_phot_qc(cpl_propertylist *,
115  cpl_boolean,
116  const irplib_framelist *);
117 
118 static cpl_error_code visir_img_phot_save(cpl_frameset *,
119  const cpl_parameterlist *,
120  const cpl_propertylist *,
121  const cpl_image *,
122  const cpl_image *,
123  const cpl_image *,
124  const cpl_image *,
125  const cpl_image *);
126 
127 static char * visir_img_phot_filter2label(const char *);
128 
129 #ifdef VISIR_CHAIN
130 #define cpl_plugin_get_info visir_old_img_phot_get_info
131 #endif
132 VISIR_RECIPE_DEFINE(visir_old_img_phot,
133  VISIR_PARAM_RADII | VISIR_PARAM_JYVAL |
134  VISIR_PARAM_REFINE | VISIR_PARAM_XCORR |
135  VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS |
136  VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
137  VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
138  VISIR_PARAM_UNION | VISIR_PARAM_REJECT |
139  VISIR_PARAM_COMBINE | VISIR_PARAM_ECCMAX |
140  VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
141  VISIR_PARAM_PLOT,
142  "Old DRS detector: Sensitivity computation",
143  "This recipe computes the conversion factor and the "
144  "sentitivity of a\n"
145  "standard star. It requires a standard star catalog.\n"
146  "The files listed in the Set Of Frames (sof-file) "
147  "must be tagged:\n"
148  "VISIR-std-star-observation.fits " VISIR_IMG_PHOT_RAW
149  "\n"
150  "VISIR-Imaging-Standard-Star-Catalog.fits "
151  VISIR_IMA_STD_CAT_PROCATG "\n"
152  "\n"
153  "The primary product will have a FITS card\n"
154  "'HIERARCH " CPL_DFS_PRO_CATG "' with a string value of\n"
155  "'" VISIR_IMG_PHOT_COMBINED_PROCATG "'\n"
156  " and the corresponding beam-collapsed product will "
157  "have a FITS card\n"
158  "'HIERARCH " CPL_DFS_PRO_CATG "' with a value of\n"
159  VISIR_IMG_PHOT_ONEBEAM_PROCATG
160  "\n"
161  MAN_VISIR_CALIB_BPM_IMG);
162 
163 /*-----------------------------------------------------------------------------
164  Static variables
165  -----------------------------------------------------------------------------*/
166 
167 static const int fits_strlen = IRPLIB_FITS_STRLEN;
168 
169 static struct {
170  /* Inputs */
171  double jy_val;
172  int r0_max;
173  int r1;
174  int r2;
175 
176  /* Outputs */
177  double exptime;
178  double pscale;
179  char star_name[IRPLIB_FITS_STRLEN+1];
180  char filter[IRPLIB_FITS_STRLEN+1];
181  double bg_sigma;
182  double flux_snr;
183  double flux_snr_noise;
184  int flux_snr_radius[4];
185  double flux_tot;
186  double nelec;
187  double nphot;
188  beamshape_t beamshape;
189  double fwhm_x_pos1;
190  double fwhm_y_pos1;
191  double fwhm_x_pos2;
192  double fwhm_y_pos2;
193  double fwhm_x_neg1;
194  double fwhm_y_neg1;
195  double fwhm_x_neg2;
196  double fwhm_y_neg2;
197  double sensitivity;
198  double area_sensit;
199  double conversion;
200  double strehl;
201  double strehl_err;
202 } visir_img_phot_config;
203 
204 
205 /*----------------------------------------------------------------------------*/
209 /*----------------------------------------------------------------------------*/
210 
211 /*-----------------------------------------------------------------------------
212  Functions code
213  -----------------------------------------------------------------------------*/
214 
215 
216 static cpl_error_code
217 propagate_qc_header(cpl_propertylist * qclist, const cpl_propertylist * ppqc)
218 {
219  double d;
220  int i;
221  const char * s;
222 
223  s = cpl_propertylist_get_string(ppqc, "BUNIT");
224  cpl_propertylist_append_string(qclist, "BUNIT", s);
225 
226  d = cpl_propertylist_get_double(ppqc, "ESO QC BACKGD MEAN");
227  cpl_propertylist_append_double(qclist, "ESO QC BACKGD MEAN", d);
228 
229  d = cpl_propertylist_get_double(ppqc, "ESO QC EXPTIME EFFECTIVE");
230  cpl_propertylist_append_double(qclist, "ESO QC EXPTIME EFFECTIVE", d);
231 
232  if (cpl_propertylist_has(ppqc, "ESO QC EXECTIME")) {
233  d = cpl_propertylist_get_double(ppqc, "ESO QC EXECTIME");
234  cpl_propertylist_append_double(qclist, "ESO QC EXECTIME", d);
235  }
236 
237  i = cpl_propertylist_get_int(ppqc, "ESO QC NIMAGES TOTAL");
238  cpl_propertylist_append_int(qclist, "ESO QC NIMAGES TOTAL", i);
239 
240  i = cpl_propertylist_get_int(ppqc, "ESO QC NIMAGES EFFECTIVE");
241  cpl_propertylist_append_int(qclist, "ESO QC NIMAGES EFFECTIVE", i);
242 
243  s = cpl_propertylist_get_string(ppqc, "ESO QC BEAMID");
244  cpl_propertylist_append_string(qclist, "ESO QC BEAMID", s);
245 
246  error_if(strcmp(s, "UNDEFINED") == 0, CPL_ERROR_ILLEGAL_INPUT,
247  "Object detection was not successful, "
248  "please provide visir_util_detect_position with the object "
249  "positions to allow photometry");
250  if (strcmp(s, "COMBINED") != 0) {
251  d = cpl_propertylist_get_double(ppqc, "ESO QC BEAMX");
252  cpl_propertylist_append_double(qclist, "ESO QC BEAMX", d);
253 
254  d = cpl_propertylist_get_double(ppqc, "ESO QC BEAMY");
255  cpl_propertylist_append_double(qclist, "ESO QC BEAMY", d);
256  }
257 
258  end_skip;
259 
260  return cpl_error_get_code();
261 }
262 
263 
264 /*----------------------------------------------------------------------------*/
271 /*----------------------------------------------------------------------------*/
272 static int visir_old_img_phot(cpl_frameset * framelist,
273  const cpl_parameterlist * parlist)
274 {
275  cpl_errorstate cleanstate = cpl_errorstate_get();
276  irplib_framelist * allframes = NULL;
277  irplib_framelist * rawframes = NULL;
278  cpl_propertylist * ppqc = NULL;
279  cpl_propertylist * qclist = NULL;
280  const char * badpix;
281  const char * star_cat;
282  const char * flat;
283  cpl_image * weights = NULL;
284  cpl_image * contrib = NULL;
285  cpl_image ** combined = NULL;
286  cpl_image ** beam1 = NULL;
287  const char * sval;
288  cpl_boolean drop_wcs = CPL_FALSE;
289  const char * keys_regexp = "^(" RECIPE_KEYS_REGEXP_WCS
290  "|"VISIR_PFITS_REGEXP_DIT
291  "|ESO OBS.*)$";
292  const char * combine_string;
293  cpl_geom_combine combine_mode;
294  cpl_boolean one_beam = CPL_FALSE;
295  const char * dit_key = VISIR_PFITS_DOUBLE_DIT;
296 
297 
298 
299  visir_img_phot_config.exptime = -1.0;
300  visir_img_phot_config.pscale = -1.0;
301  visir_img_phot_config.star_name[0] = (char)0;
302  visir_img_phot_config.filter[0] = (char)0;
303  visir_img_phot_config.bg_sigma = -1.0;
304  visir_img_phot_config.flux_snr = -1.0;
305  visir_img_phot_config.flux_snr_noise = -1.0;
306  visir_img_phot_config.flux_snr_radius[0] = -1;
307  visir_img_phot_config.flux_snr_radius[1] = -1;
308  visir_img_phot_config.flux_snr_radius[2] = -1;
309  visir_img_phot_config.flux_snr_radius[3] = -1;
310  visir_img_phot_config.flux_tot = -1.0;
311  visir_img_phot_config.nelec = -1.0;
312  visir_img_phot_config.nphot = -1.0;
313  visir_img_phot_config.beamshape =
314  (beamshape_t){.fwhm_x = -1., .fwhm_y = -1.,
315  .peak = -1., .peak_err = 0.,
316  .major = -1., .major_err = 0.,
317  .minor = -1., .minor_err = 0.,
318  .angle = -1., .angle_err = 0.};
319  visir_img_phot_config.fwhm_x_pos1 = -1.0;
320  visir_img_phot_config.fwhm_y_pos1 = -1.0;
321  visir_img_phot_config.fwhm_x_pos2 = -1.0;
322  visir_img_phot_config.fwhm_y_pos2 = -1.0;
323  visir_img_phot_config.fwhm_x_neg1 = -1.0;
324  visir_img_phot_config.fwhm_y_neg1 = -1.0;
325  visir_img_phot_config.fwhm_x_neg2 = -1.0;
326  visir_img_phot_config.fwhm_y_neg2 = -1.0;
327  visir_img_phot_config.sensitivity = -1.0;
328  visir_img_phot_config.area_sensit = -1.0;
329  visir_img_phot_config.conversion = -1.0;
330  visir_img_phot_config.strehl = -1.0;
331  visir_img_phot_config.strehl_err = -1.0;
332 
333  /* Retrieve input parameters */
334  combine_string = visir_parameterlist_get_string(parlist, RECIPE_STRING,
335  VISIR_PARAM_COMBINE);
336 
337  bug_if (combine_string == NULL);
338 
339  if (combine_string[0] == 'u')
340  combine_mode = CPL_GEOM_UNION;
341  else if (combine_string[0] == 'f')
342  combine_mode = CPL_GEOM_FIRST;
343  else if (combine_string[0] == 'i')
344  combine_mode = CPL_GEOM_INTERSECT;
345  else
346  skip_if(1);
347 
348 
349  visir_img_phot_config.jy_val =
350  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_JYVAL);
351  skip_if (0);
352 
353  sval = visir_parameterlist_get_string(parlist, RECIPE_STRING, VISIR_PARAM_RADII);
354  skip_if (sval == NULL);
355 
356  skip_if (sscanf(sval, "%d%*c%d%*c%d",
357  &visir_img_phot_config.r0_max,
358  &visir_img_phot_config.r1,
359  &visir_img_phot_config.r2) != 3);
360 
361  /* Identify the RAW and CALIB frames in the input frameset */
362  skip_if (visir_dfs_set_groups(framelist));
363 
364  /* Objects observation */
365  allframes = irplib_framelist_cast(framelist);
366  skip_if(allframes == NULL);
367  {
368  rawframes = irplib_framelist_extract(allframes, VISIR_IMG_PHOT_RAW);
369  if (rawframes == NULL) {
370  cpl_errorstate_set(cleanstate);
371  rawframes = irplib_framelist_extract(allframes,
372  "IM_CAL_PHOT_PREPROCESSED");
373  skip_if (rawframes == NULL);
374  const char * fn =
375  irplib_frameset_find_file(framelist, VISIR_UTIL_QC_PROCATG);
376  if(fn == NULL) {
377  cpl_msg_error(cpl_func, "The input files has no header tagged "
378  "%s", VISIR_UTIL_QC_PROCATG);
379  visir_error_set(CPL_ERROR_DATA_NOT_FOUND);
380  skip_if(1);
381  }
382  ppqc = cpl_propertylist_load(fn, 0);
383  one_beam = CPL_TRUE;
384  }
385  }
386 
387  skip_if (rawframes == NULL);
388 
389  irplib_framelist_empty(allframes);
390 
391  skip_if(irplib_framelist_load_propertylist(rawframes, 0, 0, keys_regexp,
392  CPL_FALSE));
393  qclist = cpl_propertylist_duplicate(irplib_framelist_get_propertylist(rawframes, 0));
394  if (ppqc)
395  skip_if(propagate_qc_header(qclist, ppqc));
396 
397  skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
398  RECIPE_KEYS_REGEXP_ALL
399  "|"VISIR_PFITS_REGEXP_DIT
400  ")$", CPL_FALSE));
401 
402  skip_if(visir_dfs_check_framelist_tag(rawframes));
403 
404  if (cpl_propertylist_has(irplib_framelist_get_propertylist(rawframes, 0),
405  VISIR_PFITS_DOUBLE_SEQ1_DIT))
406  dit_key = VISIR_PFITS_DOUBLE_SEQ1_DIT;
407  skip_if(0);
408 
409  /* Standard star catalog */
410  star_cat = irplib_frameset_find_file(framelist, VISIR_CALIB_STDSTAR_IMG);
411  if (star_cat == NULL) {
412  cpl_msg_error(cpl_func, "The input files has no star catalog tagged "
413  "%s", VISIR_CALIB_STDSTAR_IMG);
414  visir_error_set(CPL_ERROR_DATA_NOT_FOUND);
415  skip_if(1);
416  }
417 
418  /* Bad pixels calibration file */
419  badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
420 
421  /* Flatfield calibration file */
422  flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
423 
424 
425  /* DIT must be present every where */
426  skip_if(irplib_framelist_contains(rawframes, dit_key,
427  CPL_TYPE_DOUBLE, CPL_FALSE, 0.0));
428  if(irplib_framelist_contains(rawframes, dit_key,
429  CPL_TYPE_DOUBLE, CPL_TRUE, 1e-5)) {
430  /* Allow 0.1 ms difference - or warn */
431  /* FIXME: The recipe does not properly handle non-uniform DITSs */
432  visir_error_reset("DIT differs by more than %g", 1e-5);
433  }
434 
435  /* FIXME: Verify the angular distance */
436  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_DOUBLE_RA,
437  CPL_TYPE_DOUBLE, CPL_FALSE, 0.0));
438 
439  /* FIXME: Allow 1 degree difference */
440  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_DOUBLE_DEC,
441  CPL_TYPE_DOUBLE, CPL_TRUE, 1.0));
442 
443  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_INT_CHOP_NCYCLES,
444  CPL_TYPE_INT, CPL_TRUE, 0.0));
445 
446  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_INT_NDIT,
447  CPL_TYPE_INT, CPL_TRUE, 0.0));
448 
449  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_STRING_STARNAME,
450  CPL_TYPE_STRING, CPL_TRUE, 0.0));
451 
452  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_STRING_INSMODE,
453  CPL_TYPE_STRING, CPL_TRUE, 0.0));
454 
455  if (strncmp("IMG", visir_pfits_get_insmode(
457  6) == 0) {
458  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_STRING_FILTER1,
459  CPL_TYPE_STRING, CPL_TRUE, 0.0));
460  } else {
461  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_STRING_FILTER2,
462  CPL_TYPE_STRING, CPL_TRUE, 0.0));
463  }
464 
465  skip_if(irplib_framelist_contains(rawframes, VISIR_PFITS_STRING_PIXSCALE,
466  CPL_TYPE_STRING, CPL_TRUE, 0.0));
467 
468 
469  {
470  const char * prew =
471  irplib_frameset_find_file(framelist, "WEIGHT_MAP");
472  if (prew) {
473  cpl_msg_info(cpl_func, "Loading weight map %s", prew);
474  weights = cpl_image_load(prew, CPL_TYPE_UNSPECIFIED, 0, 0);
475  }
476  }
477 
478  {
479  const char * prec =
480  irplib_frameset_find_file(framelist, "CONTRIBUTION_MAP");
481  if (prec) {
482  cpl_msg_info(cpl_func, "Loading contribution map %s", prec);
483  contrib = cpl_image_load(prec, CPL_TYPE_INT, 0, 0);
484  }
485  }
486 
487  skip_if(0);
488  /* Combine the frames */
489  cpl_msg_info(cpl_func, "Combining the images");
490  {
491  const char * pre =
492  irplib_frameset_find_file(framelist, "IM_CAL_PHOT_PREPROCESSED");
493  if (pre) {
494  combined = cpl_calloc(2, sizeof(combined[0]));
495  combined[0] = cpl_image_load(pre, CPL_TYPE_UNSPECIFIED, 0, 0);
496  combined[1] = contrib ? cpl_image_duplicate(contrib) : NULL;
497  if (combined[0] == NULL) {
498  cpl_msg_error(cpl_func, "Could not load the input frames");
499  skip_if(1);
500  }
501  if (weights) {
502  cpl_mask * m = cpl_mask_threshold_image_create(weights, -1,
503  FLT_EPSILON);
504  cpl_image_reject_from_mask(combined[0], m);
505  cpl_mask_delete(m);
506  }
507  }
508  else {
509  combined = visir_img_recombine(RECIPE_STRING, parlist, rawframes,
510  badpix, flat, combine_mode,
511  &drop_wcs, CPL_FALSE, 0.0, 0);
512  if (combined == NULL) {
513  cpl_msg_error(cpl_func, "Could not combine the input frames");
514  skip_if(1);
515  }
516  beam1 = visir_img_collapse_beam(qclist, combined[0], parlist, RECIPE_STRING,
517  VISIR_CHOPNOD_AUTO,
519  (rawframes, 0));
520  if (beam1 == NULL)
521  irplib_error_recover(cleanstate, "Could not collapse the "
522  "beams of the combined image");
523 
524  /* Compute the background values of the HCYCLE frames
525  * done by repack in preprocessed mode */
526  skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
527  }
528  }
529 
530  /* Compute here the sensitivity */
531  cpl_msg_info(cpl_func, "Computing the sensitivity");
532  if (visir_img_phot_sensit(combined[0], weights, contrib, rawframes,
533  star_cat, one_beam, ppqc)) {
534  cpl_msg_error(cpl_func, "Could not compute sensitivity: '%s' in %s",
535  cpl_error_get_message(), cpl_error_get_where());
536  skip_if(1);
537  }
538 
539  skip_if (visir_img_phot_qc(qclist, drop_wcs, rawframes));
540  irplib_framelist_empty(rawframes);
541 
542  /* Save the combined image and contribution map */
543  if (beam1 == NULL) {
544  cpl_msg_info(cpl_func, "Saving the combined image with its contribution "
545  "map");
546  skip_if (visir_img_phot_save(framelist, parlist, qclist, combined[0],
547  combined[1], NULL, NULL, weights));
548  } else {
549  cpl_msg_info(cpl_func, "Saving the combined and the beam-combined "
550  "images with their contribution maps");
551  skip_if (visir_img_phot_save(framelist, parlist, qclist, combined[0],
552  combined[1], beam1[0], beam1[1], weights));
553  }
554 
555  end_skip;
556 
557  cpl_propertylist_delete(qclist);
558  irplib_framelist_delete(allframes);
559  irplib_framelist_delete(rawframes);
560  cpl_propertylist_delete(ppqc);
561  cpl_image_delete(weights);
562  cpl_image_delete(contrib);
563 
564  if (combined) {
565  cpl_image_delete(combined[0]);
566  cpl_image_delete(combined[1]);
567  cpl_free(combined);
568  }
569 
570  if (beam1) {
571  cpl_image_delete(beam1[0]);
572  cpl_image_delete(beam1[1]);
573  cpl_free(beam1);
574  }
575 
576  return cpl_error_get_code();
577 
578 }
579 
580 /*----------------------------------------------------------------------------*/
605 /*----------------------------------------------------------------------------*/
606 static int visir_img_phot_sensit(const cpl_image * combined,
607  const cpl_image * weights,
608  const cpl_image * contrib,
609  const irplib_framelist * rawframes,
610  const char * star_cat,
611  cpl_boolean one_beam,
612  cpl_propertylist * ppqc)
613 {
614  cpl_errorstate cleanstate = cpl_errorstate_get();
615  const cpl_propertylist * plist = NULL;
616  double ra, dec;
617  const char * sval;
618  double f2;
619 
620 
621  skip_if (0);
622 
623  plist = irplib_framelist_get_propertylist_const(rawframes, 0);
624  skip_if (0);
625 
626  /* Get the total exposure time */
627  {
628  double exptime;
629 
630  if (ppqc) /* FIXME: use effective exptime? */
631  exptime = cpl_propertylist_get_double(ppqc, "ESO QC EXPTIME TOTAL");
632  else {
633  const int nnod = irplib_framelist_get_size(rawframes);
634  exptime = visir_utils_get_exptime(nnod, plist);
635  }
636  visir_img_phot_config.exptime = exptime;
637 
638  if (exptime <= 0 || cpl_error_get_code()) {
639  cpl_msg_error(cpl_func, "Illegal exposure time: %g", exptime);
640  skip_if(1);
641  }
642  }
643 
644  /* Copy the standard star name */
645  skip_if ((sval = visir_pfits_get_starname(plist)) == NULL);
646  (void) strncpy(visir_img_phot_config.star_name, sval, fits_strlen);
647 
648  /* Copy the filter name */
649  skip_if ((sval = visir_pfits_get_filter(plist)) == NULL);
650  (void)strncpy(visir_img_phot_config.filter, sval, fits_strlen);
651 
652  /* Get RA / DEC */
653  ra = visir_pfits_get_ra(plist);
654  skip_if (0);
655  dec = visir_pfits_get_dec(plist);
656  skip_if (0);
657 
658  /* Get the pixel scale */
659  visir_img_phot_config.pscale = visir_pfits_get_pixscale(plist);
660 
661  /* Get the JY value from the catalog if not user provided */
662  if (visir_img_phot_config.jy_val < -998) {
663  visir_img_phot_config.jy_val =
664  visir_img_phot_jy_from_cat(star_cat, visir_img_phot_config.filter,
665  ra,dec, visir_img_phot_config.star_name);
666  skip_if (visir_img_phot_config.jy_val < -998);
667  }
668 
669  /* Display the result */
670  cpl_msg_info(cpl_func, "Star %s with filter %s : %g Jy",
671  visir_img_phot_config.star_name,
672  visir_img_phot_config.filter,
673  visir_img_phot_config.jy_val);
674 
675  /* Compute the background signa */
676  visir_img_phot_config.bg_sigma = visir_img_phot_sigma_clip(combined);
677  skip_if (0);
678 
679  /* Get the flux and flux noise and fwhm */
680  cpl_msg_info(cpl_func, "Compute the star flux");
681  sval = visir_pfits_get_chopnod_dir(plist);
682 #if 1
683  if (one_beam) {
684  skip_if (visir_img_phot_flux_one(combined, weights, contrib, plist));
685  }
686  else if (sval != NULL && !strcmp(sval, "PERPENDICULAR")) {
687  /* 4 sources */
688  skip_if (visir_img_phot_flux_four(combined));
689  } else if (sval != NULL && !strcmp(sval, "PARALLEL")) {
690  /* 3 sources */
691  skip_if (visir_img_phot_flux_three(combined));
692  } else {
693  if (sval == NULL) {
694  visir_error_reset("Could not get FITS key");
695  } else {
696  cpl_msg_warning(cpl_func, "Unknown chopping direction: %s", sval);
697  }
698  cpl_msg_warning(cpl_func, "Proceeding as if FITS card %s had value %s",
699  "ESO SEQ CHOPNOD DIR", "PERPENDICULAR");
700  if (visir_img_phot_flux_four(combined)) {
701  visir_error_reset("Proceeding as if FITS card %s had value %s",
702  "ESO SEQ CHOPNOD DIR", "PARALLEL");
703  skip_if (visir_img_phot_flux_three(combined));
704  }
705  }
706 #else
707  if (sval == NULL) {
708  visir_error_reset("Could not get chopping direction");
709  if (visir_img_phot_flux_three(combined)) {
710  cpl_msg_error(cpl_func, "Could not compute the flux");
711  skip_if(1);
712  }
713  } else if (!strcmp(sval, "PARALLEL")) {
714  /* 3 sources */
715  if (visir_img_phot_flux_three(combined)) {
716  cpl_msg_error(cpl_func, "Could not compute the flux");
717  skip_if(1);
718  }
719  } else {
720  /* Default is 4 sources */
721  if (strcmp(sval, "PERPENDICULAR"))
722  cpl_msg_warning(cpl_func, "Unknown chopping direction: %s", sval);
723  if (visir_img_phot_flux_four(combined)) {
724  cpl_msg_error(cpl_func, "Could not compute the flux");
725  skip_if(1);
726  }
727  }
728 #endif
729 
730 
731  /* compute number of photons and electrons for the efficiency */
732  {
733  double fluxSI = visir_img_phot_config.jy_val * 1e-26;
734  double dit = visir_pfits_get_dit(plist);;
735  /* area of telescope [m^2] */
736  double area = (8.2/2.) * (8.2/2.) * CPL_MATH_PI;
737  const char * filter = visir_pfits_get_filter(plist);
738  double wl;
739  /* filter bandwidth delta lambda [m] */
740  double bandwidth;
741  double gain = 20.;
742 
743  if (ppqc && cpl_propertylist_has(ppqc, "ESO DET CHIP1 GAIN"))
744  gain = cpl_propertylist_get_double(ppqc, "ESO DET CHIP1 GAIN");
745  else if (ppqc && cpl_propertylist_has(ppqc, "ESO DET CHIP GAIN"))
746  gain = cpl_propertylist_get_double(ppqc, "ESO DET CHIP GAIN");
747  else
748  cpl_msg_warning(cpl_func, "ESO DET CHIP1 GAIN not found, "
749  "assuming %g", gain);
750 
751  if (filter && !visir_get_filter_infos(filter, &wl, &bandwidth)) {
752  /* filter bandwidth deltanue [Hz] */
753  double bwh, ptot, ephot;
754  wl *= 1e-6; /* [micron] -> [m] */
755  bandwidth *= 1e-6; /* [micron] -> [m] */
756 
757  bwh = CPL_PHYS_C / (wl * wl) * bandwidth;
758  /* total power [W] */
759  ptot = fluxSI * area * bwh;
760  /* photon energy [Ws] */
761  ephot = CPL_PHYS_H * CPL_PHYS_C / wl;
762 
763  visir_img_phot_config.nelec = visir_img_phot_config.flux_tot * gain;
764  visir_img_phot_config.nphot = ptot / ephot * dit;
765  }
766  else
767  cpl_msg_warning(cpl_func, "Unknown filter: %s", filter);
768  }
769 
770  /* Compute the sensitivity and the conversion factor */
771 
772  skip_if ( visir_img_phot_config.flux_snr == 0 );
773  skip_if ( visir_img_phot_config.flux_snr_noise == 0 );
774  skip_if ( visir_img_phot_config.jy_val == 0 );
775 
776  f2 = visir_img_phot_config.flux_snr / visir_img_phot_config.flux_snr_noise;
777  f2 *= sqrt(3600/visir_img_phot_config.exptime);
778  visir_img_phot_config.sensitivity = visir_img_phot_config.jy_val*1000*10/f2;
779  /* compute conversion, accounting for double signal in parallel mode */
780  visir_img_phot_config.conversion = visir_img_phot_config.flux_tot /
781  visir_img_phot_config.jy_val /
782  (ppqc ? visir_pfits_get_img_weight(ppqc) : 1.);
783 
784  /* sensitivity using the noise under 1as circle */
785  visir_img_phot_config.area_sensit =
786  visir_img_phot_config.sensitivity *
787  sqrt((1. / SQR(visir_img_phot_config.pscale)) /
788  SQR(visir_img_phot_config.flux_snr_radius[0]));
789 
790  end_skip;
791 
792  cpl_msg_info(cpl_func, "Sensitivity : %g", visir_img_phot_config.sensitivity);
793  cpl_msg_info(cpl_func, "Conversion : %g", visir_img_phot_config.conversion);
794  cpl_msg_info(cpl_func, "Strehl : %g", visir_img_phot_config.strehl);
795  cpl_msg_info(cpl_func, "Strehl error: %g", visir_img_phot_config.strehl_err);
796  cpl_msg_info(cpl_func, "Exposure : %g s", visir_img_phot_config.exptime);
797 
798  return cpl_error_get_code();
799 }
800 
801 /*----------------------------------------------------------------------------*/
811 /*----------------------------------------------------------------------------*/
812 static double visir_img_phot_jy_from_cat(
813  const char * star_cat,
814  const char * filter,
815  double ra,
816  double dec,
817  const char * star_name)
818 {
819  cpl_table * tab = NULL;
820  cpl_vector * v_ra = NULL;
821  cpl_vector * v_dec = NULL;
822  const char * stdstar;
823  char * label = NULL;
824  int nb_stars;
825  const double max_radius = VISIR_STAR_MAX_RADIUS;
826  double value = -999;
827  double jy;
828  double dist;
829  int min_dist_ind;
830 
831 
832  skip_if( 0 );
833  skip_if( star_cat == NULL );
834  skip_if( star_name == NULL );
835  skip_if( filter == NULL );
836 
837  label = visir_img_phot_filter2label(filter);
838  skip_if(label == NULL);
839 
840  /* Open the star catalog */
841  if ((tab = cpl_table_load(star_cat, 1, 1)) == NULL) {
842  cpl_error_set_message(cpl_func, cpl_error_get_code(),
843  "Could not load the star catalog: %s",
844  star_cat);
845  skip_if(1);
846  }
847 
848  /* Check that the filter is in the table */
849  if (!cpl_table_has_column(tab, label)) {
850  cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
851  "Catalog %s has no column for filter: %s",
852  star_cat, filter);
853  skip_if(1);
854  }
855 
856  if (!cpl_table_has_column(tab, "STARS")) {
857  cpl_error_set_message(cpl_func, CPL_ERROR_BAD_FILE_FORMAT,
858  "Catalog %s does not have a column labeled %s",
859  star_cat, "STARS");
860  skip_if(1);
861  }
862 
863  nb_stars = cpl_table_get_nrow(tab);
864  skip_if (nb_stars < 1);
865 
866  /* Get the RA and DEC columns */
867  v_ra = cpl_vector_wrap(nb_stars, cpl_table_get_data_double(tab, "RA"));
868  skip_if( v_ra == NULL);
869 
870  v_dec = cpl_vector_wrap(nb_stars, cpl_table_get_data_double(tab, "DEC"));
871  skip_if( v_dec == NULL);
872 
873  min_dist_ind = visir_star_find(v_ra, v_dec, ra, dec, max_radius, &dist);
874 
875  if (min_dist_ind < 0) {
876  cpl_error_set_message(cpl_func, cpl_error_get_code(),
877  "Observation target '%s' was not found among "
878  "the %d entries in the standard star catalog %s",
879  star_name, nb_stars, star_cat);
880  skip_if(1);
881  }
882 
883  stdstar = cpl_table_get_string(tab, "STARS", min_dist_ind);
884  skip_if ( stdstar == NULL );
885 
886  int null;
887  jy = cpl_table_get(tab, label, min_dist_ind, &null);
888  skip_if( 0 );
889 
890  if (null || isnan(jy)) {
891  cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
892  "Catalog does not contain a value for %s in "
893  "filter %s", star_name, filter);
894  skip_if(1);
895  }
896 
897  value = jy;
898 
899  if (strcmp(stdstar, star_name) != 0 && dist > 0.0) {
900  /* The names do not match, nor does the location (exactly) */
901  cpl_msg_warning(cpl_func, "The standard star '%s' at (RA,DEC)=(%g,%g) "
902  "in the FITS header is closest to the catalog star "
903  "'%s' at (RA,DEC)=(%g,%g) at distance %g degrees",
904  star_name, ra, dec, stdstar,
905  cpl_vector_get(v_ra, min_dist_ind),
906  cpl_vector_get(v_dec, min_dist_ind), dist);
907  } else if (dist > 0.0) {
908  /* The names match, but the location does not (exactly) */
909  cpl_msg_warning(cpl_func, "The location of the standard star '%s' in "
910  "the FITS header (RA,DEC)=(%g,%g) and the catalog "
911  "(RA,DEC)=(%g,%g) differ by %g degrees", stdstar, ra,
912  dec, cpl_vector_get(v_ra, min_dist_ind),
913  cpl_vector_get(v_dec, min_dist_ind), dist);
914  } else if (strcmp(stdstar, star_name) != 0) {
915  /* The names do not match, but the location does */
916  cpl_msg_warning(cpl_func, "The name of the standard star at (RA,DEC)="
917  "(%g,%g) in the FITS header '%s' and the catalog '%s' "
918  "are different", ra, dec, star_name, stdstar);
919  } else {
920  cpl_msg_info(cpl_func, "Standard star is '%s' at (RA, DEC)=(%g,%g)",
921  stdstar, ra, dec);
922 
923  if (cpl_msg_get_level() <= CPL_MSG_DEBUG)
924  cpl_table_dump(tab, min_dist_ind, 1, stdout);
925  }
926 
927  cpl_msg_info(cpl_func, "The standard star '%s' has %g Jy through filter %s",
928  stdstar, value, filter);
929 
930  end_skip;
931 
932  cpl_free(label);
933  cpl_table_delete(tab);
934  cpl_vector_unwrap(v_ra);
935  cpl_vector_unwrap(v_dec);
936 
937  return value;
938 }
939 
940 /*----------------------------------------------------------------------------*/
949 /*----------------------------------------------------------------------------*/
950 static int visir_img_phot_flux_three(const cpl_image * combined)
951 {
952  cpl_errorstate cleanstate = cpl_errorstate_get();
953  cpl_image * min_combined = NULL;
954  cpl_apertures * appos = NULL;
955  cpl_apertures * apneg = NULL;
956  cpl_vector * sigmas = NULL;
957  double psigmas[] = {5, 2, 1, 0.5};
958  double x[3];
959  double y[3];
960  double flux_snr[3];
961  double flux_snr_noise[3];
962  int flux_snr_radius[3];
963  double flux_tot[3];
964  beamshape_t beamshapes[3];
965  double dist1, dist2;
966  int ngood_fwhm;
967  double lam, dlam;
968  double star_bg,star_peak,star_flux,psf_peak,psf_flux,bg_noise;
969  double eccmin = DBL_MAX;
970  const int nsigmas = sizeof(psigmas)/sizeof(double);
971  int isigma;
972 #ifdef VISIR_IMG_PHOT_USE_ECCENT_THREE
973  int ipos, ineg1, ineg2;
974 #endif
975  int iappos, iapneg2[2];
976  int i;
977 
978 
979  if (cpl_error_get_code()) return cpl_error_get_code();
980 
981  cpl_ensure_code(combined != NULL, CPL_ERROR_NULL_INPUT);
982 
983  cpl_msg_info(cpl_func, "Detecting the 3-source star using %d sigma-levels "
984  "ranging from %g down to %g", nsigmas, psigmas[0],
985  psigmas[nsigmas-1]);
986 
987  sigmas = cpl_vector_new(1);
988  min_combined = cpl_image_multiply_scalar_create(combined, -1.0);
989  bug_if(0);
990  for (isigma = 0; isigma < nsigmas; isigma++) {
991 
992  /* FIXME: Why ?! */
993  irplib_error_recover(cleanstate, "Resetting error (why?)");
994 
995  bug_if(cpl_vector_set(sigmas, 0, psigmas[isigma]));
996 
997  /* Detect where the POSITIVE star is */
998  cpl_apertures_delete(appos);
999  appos = cpl_apertures_extract(combined, sigmas, NULL);
1000  if (appos == NULL) {
1001  cpl_msg_warning(cpl_func, "Found no positive star at sigma=%g",
1002  psigmas[isigma]);
1003  continue;
1004  }
1005 
1006  /* Detect where the NEGATIVE stars are */
1007  cpl_apertures_delete(apneg);
1008  apneg = cpl_apertures_extract(min_combined, sigmas, NULL);
1009  if (apneg == NULL) {
1010  cpl_msg_warning(cpl_func, "Found no negative stars at sigma=%g",
1011  psigmas[isigma]);
1012  continue;
1013  }
1014  if (cpl_apertures_get_size(apneg) < 2) {
1015  cpl_msg_warning(cpl_func, "Found just 1 negative star at sigma=%g, "
1016  "need two", psigmas[isigma]);
1017  continue;
1018  }
1019 
1020  cpl_msg_info(cpl_func, "Found positive and negative stars at sigma=%g",
1021  psigmas[isigma]);
1022  break;
1023  }
1024 
1025  skip_if(appos == NULL);
1026  skip_if(apneg == NULL);
1027  skip_if (cpl_apertures_get_size(apneg) < 2);
1028 
1029  if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
1030  cpl_apertures_dump(appos, stdout);
1031  cpl_apertures_dump(apneg, stdout);
1032  }
1033 
1034 #ifndef VISIR_IMG_PHOT_USE_ECCENT_THREE
1035  bug_if(irplib_apertures_find_max_flux(appos, &iappos, 1));
1036  bug_if(irplib_apertures_find_max_flux(apneg, iapneg2, 2));
1037 #else
1038  if (cpl_apertures_get_size(appos) > 1 || cpl_apertures_get_size(apneg) > 2)
1039  cpl_msg_info(cpl_func, "Selecting from %d positive and %d negative "
1040  "stars 3 stars on a vertical line",
1041  cpl_apertures_get_size(appos),
1042  cpl_apertures_get_size(apneg));
1043 
1044  for (ipos = 1; ipos <= cpl_apertures_get_size(appos); ipos++) {
1045  for (ineg1 = 2; ineg1 <= cpl_apertures_get_size(apneg); ineg1++) {
1046  for (ineg2 = 1; ineg2 < ineg1; ineg2++) {
1047  const double ecc = visir_img_phot_eccent_three(appos, ipos,
1048  apneg,
1049  ineg1, ineg2);
1050  if (ecc < eccmin) {
1051  if (eccmin < DBL_MAX)
1052  cpl_msg_debug(cpl_func, "Found star positions with "
1053  "reduced mis-alignment [pixel]: "
1054  "%g < %g", ecc, eccmin);
1055  eccmin = ecc;
1056  iappos = ipos;
1057  iapneg2[0] = ineg1;
1058  iapneg2[1] = ineg2;
1059  }
1060  }
1061  }
1062  }
1063 
1064  /* Star 1 should have largest flux */
1065  if (cpl_apertures_get_flux(apneg, iapneg2[0]) <
1066  cpl_apertures_get_flux(apneg, iapneg2[1])) {
1067  const int tmp = iapneg2[0];
1068  iapneg2[0] = iapneg2[1];
1069  iapneg2[1] = tmp;
1070  }
1071 
1072 #endif
1073 
1074  x[0] = cpl_apertures_get_centroid_x(appos, iappos);
1075  y[0] = cpl_apertures_get_centroid_y(appos, iappos);
1076 
1077  x[1] = cpl_apertures_get_centroid_x(apneg, iapneg2[0]);
1078  y[1] = cpl_apertures_get_centroid_y(apneg, iapneg2[0]);
1079  x[2] = cpl_apertures_get_centroid_x(apneg, iapneg2[1]);
1080  y[2] = cpl_apertures_get_centroid_y(apneg, iapneg2[1]);
1081 
1082  cpl_apertures_delete(appos);
1083  cpl_apertures_delete(apneg);
1084  appos = NULL;
1085  apneg = NULL;
1086 
1087  cpl_msg_info(cpl_func, "Positive star at position %g %g", x[0], y[0]);
1088  cpl_msg_info(cpl_func, "Negative star 1 at position %g %g", x[1], y[1]);
1089  cpl_msg_info(cpl_func, "Negative star 2 at position %g %g", x[2], y[2]);
1090 
1091  dist1 = sqrt((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0]));
1092  dist2 = sqrt((x[2]-x[0])*(x[2]-x[0])+(y[2]-y[0])*(y[2]-y[0]));
1093  cpl_msg_info(cpl_func, "Star 1 Pos/Neg Distance: %g", dist1);
1094  cpl_msg_info(cpl_func, "Star 2 Pos/Neg Distance: %g", dist2);
1095 
1096 
1097  if (eccmin < DBL_MAX) {
1098  cpl_msg_info(cpl_func, "The deviation from a vertical line by "
1099  "the three stars [pixel]: %g", eccmin);
1100  if (eccmin > VISIR_IMG_PHOT_POS_WARN) {
1101  if (eccmin > VISIR_IMG_PHOT_POS_ERROR) {
1102  cpl_msg_error(cpl_func, "The deviation from a vertical line"
1103  " by the three stars exceed %g, the detected "
1104  "objects are wrong", VISIR_IMG_PHOT_POS_ERROR);
1105  skip_if(1);
1106  }
1107  cpl_msg_warning(cpl_func, "The deviation from a vertical line "
1108  "by the three stars exceed %g, the detected "
1109  "objects may be wrong", VISIR_IMG_PHOT_POS_WARN);
1110  }
1111  }
1112 
1113  /* FIXME: Pick other object instead, and lower sigma if needed */
1114  if ((int)x[0]-IRPLIB_STREHL_BORDER <= 0 ||
1115  (int)y[0]-IRPLIB_STREHL_BORDER <= 0 ||
1116  (int)x[0]+IRPLIB_STREHL_BORDER > cpl_image_get_size_x(combined) ||
1117  (int)y[0]+IRPLIB_STREHL_BORDER > cpl_image_get_size_y(combined)) {
1118  cpl_msg_error(cpl_func, "Positive star at (%g,%g) is less than %d "
1119  "pixels from the image border", x[0], y[0],
1120  1+IRPLIB_STREHL_BORDER);
1121  skip_if(1);
1122  }
1123 
1124  /* FIXME: Useful ? */
1125  /* Verify the stars positions */
1126  if (fabs(dist1-dist2) > VISIR_IMG_PHOT_POS_UNCERTAINTY) {
1127  cpl_msg_error(cpl_func, "Too large Pos/Neg Distance between the two "
1128  "stars: %g > %g", fabs(dist1-dist2),
1129  VISIR_IMG_PHOT_POS_UNCERTAINTY);
1130  skip_if(1);
1131  }
1132 
1133  /* Photometry on positive stars */
1134  skip_if (visir_img_phot_flux(combined, NULL, NULL,
1135  x[0], y[0],
1136  visir_img_phot_config.r0_max,
1137  visir_img_phot_config.r1,
1138  visir_img_phot_config.r2,
1139  &(flux_snr[0]),
1140  &(flux_snr_noise[0]),
1141  &(flux_snr_radius[0]),
1142  &(flux_tot[0]),
1143  &(beamshapes[0])));
1144 
1145  /* Photometry on negative stars */
1146  for (i=1 ; i<3 ; i++)
1147  skip_if (visir_img_phot_flux(min_combined, NULL, NULL,
1148  x[i], y[i],
1149  visir_img_phot_config.r0_max,
1150  visir_img_phot_config.r1,
1151  visir_img_phot_config.r2,
1152  &(flux_snr[i]),
1153  &(flux_snr_noise[i]),
1154  &(flux_snr_radius[i]),
1155  &(flux_tot[i]),
1156  &(beamshapes[i])));
1157 
1158  cpl_image_delete(min_combined);
1159  min_combined = NULL;
1160 
1161  /* Compute the results */
1162  /* Flux */
1163  visir_img_phot_config.flux_snr = 0.0;
1164  for (i=0 ; i<3 ; i++) visir_img_phot_config.flux_snr += flux_snr[i];
1165 
1166  /* Flux noise */
1167  visir_img_phot_config.flux_snr_noise = 0.0;
1168  for (i=0 ; i<3 ; i++) visir_img_phot_config.flux_snr_noise +=
1169  flux_snr_noise[i]*flux_snr_noise[i];
1170  visir_img_phot_config.flux_snr_noise =
1171  sqrt(visir_img_phot_config.flux_snr_noise);
1172 
1173  for (i=0 ; i<3 ; i++)
1174  visir_img_phot_config.flux_snr_radius[i] = flux_snr_radius[i];
1175 
1176  /* Total flux */
1177  visir_img_phot_config.flux_tot = 0.0;
1178  for (i=0 ; i<3 ; i++) visir_img_phot_config.flux_tot += flux_tot[i];
1179 
1180  /* FWHM */
1181  ngood_fwhm = 0;
1182  visir_img_phot_config.beamshape.fwhm_x = 0.0;
1183  for (i=0 ; i<3 ; i++) {
1184  if (beamshapes[i].fwhm_x > 0.0) {
1185  visir_img_phot_config.beamshape.fwhm_x += beamshapes[i].fwhm_x;
1186  ngood_fwhm ++;
1187  }
1188  }
1189  if (ngood_fwhm > 0) visir_img_phot_config.beamshape.fwhm_x /= ngood_fwhm;
1190  else visir_img_phot_config.beamshape.fwhm_x = -1.0;
1191  ngood_fwhm = 0;
1192  visir_img_phot_config.beamshape.fwhm_y = 0.0;
1193  for (i=0 ; i<3 ; i++) {
1194  if (beamshapes[i].fwhm_y > 0.0) {
1195  visir_img_phot_config.beamshape.fwhm_y += beamshapes[i].fwhm_y;
1196  ngood_fwhm ++;
1197  }
1198  }
1199  if (ngood_fwhm > 0) visir_img_phot_config.beamshape.fwhm_y /= ngood_fwhm;
1200  else visir_img_phot_config.beamshape.fwhm_y = -1.0;
1201  visir_img_phot_config.fwhm_x_pos1 = beamshapes[0].fwhm_x;
1202  visir_img_phot_config.fwhm_y_pos1 = beamshapes[0].fwhm_y;
1203  visir_img_phot_config.fwhm_x_neg1 = beamshapes[1].fwhm_x;
1204  visir_img_phot_config.fwhm_y_neg1 = beamshapes[1].fwhm_y;
1205  visir_img_phot_config.fwhm_x_neg2 = beamshapes[2].fwhm_x;
1206  visir_img_phot_config.fwhm_y_neg2 = beamshapes[2].fwhm_y;
1207 
1208  /* Get lam and dlam from the filter name for the Strehl computation */
1209  if (visir_get_filter_infos(visir_img_phot_config.filter, &lam, &dlam)) {
1210  cpl_msg_error(cpl_func, "Could not get info for filter: %s",
1211  visir_img_phot_config.filter);
1212  skip_if(1);
1213  }
1214 
1215  /* Strehl computation */
1216  cpl_errorstate errstate = cpl_errorstate_get();
1217  if (irplib_strehl_compute(combined, STREHL_M1, STREHL_M2, lam, dlam,
1218  visir_img_phot_config.pscale,
1219  STREHL_BOX_SIZE, x[0], y[0], STREHL_STAR_RADIUS,
1220  STREHL_BG_R1, STREHL_BG_R2, -1, -1,
1221  &(visir_img_phot_config.strehl),
1222  &(visir_img_phot_config.strehl_err),
1223  &star_bg, &star_peak, &star_flux, &psf_peak, &psf_flux,
1224  &bg_noise)) {
1225  cpl_msg_error(cpl_func, "Could not compute the strehl: '%s' in %s",
1226  cpl_error_get_message(), cpl_error_get_where());
1227  cpl_errorstate_set(errstate);
1228  }
1229 
1230 
1231  end_skip;
1232 
1233  cpl_apertures_delete(appos);
1234  cpl_apertures_delete(apneg);
1235  cpl_vector_delete(sigmas);
1236  cpl_image_delete(min_combined);
1237 
1238  return cpl_error_get_code();
1239 }
1240 
1241 static int visir_img_phot_flux_one(const cpl_image * combined,
1242  const cpl_image * weights,
1243  const cpl_image * contrib,
1244  const cpl_propertylist * plist)
1245 {
1246  double x;
1247  double y;
1248  double flux_snr;
1249  double flux_snr_noise;
1250  int flux_snr_radius;
1251  double flux_tot;
1252  beamshape_t beamshape;
1253  double lam, dlam;
1254  double star_bg,star_peak,star_flux,psf_peak,psf_flux,bg_noise;
1255 
1256  if (cpl_error_get_code()) return cpl_error_get_code();
1257 
1258  cpl_ensure_code(combined != NULL, CPL_ERROR_NULL_INPUT);
1259 
1260  x = cpl_propertylist_get_double(plist, "CRPIX1");
1261  y = cpl_propertylist_get_double(plist, "CRPIX2");
1262  {
1263  /* CRPIX can be a few pixels off center due to slicing of
1264  * the images around the beam done in detect_shift
1265  * only the int cast is used later, so pixel precision of
1266  * maxpos is ok */
1267  cpl_size yp, xp;
1268  cpl_image_get_maxpos_window(combined, x - 5, y - 5,
1269  x + 5, y + 5, &xp, &yp);
1270  x = xp;
1271  y = yp;
1272  }
1273 
1274  cpl_msg_info(cpl_func, "Positive star 1 at position %g %g", x, y);
1275 
1276  if ((int)x-IRPLIB_STREHL_BORDER <= 0 ||
1277  (int)y-IRPLIB_STREHL_BORDER <= 0) {
1278  cpl_msg_error(cpl_func, "Positive star 1 at (%g,%g) is less than %d "
1279  "pixels from the image border", x, y,
1280  1+IRPLIB_STREHL_BORDER);
1281  skip_if(1);
1282  }
1283 
1284  /* Photometry on positive stars */
1285  skip_if (visir_img_phot_flux(combined, weights, contrib,
1286  x, y,
1287  visir_img_phot_config.r0_max,
1288  visir_img_phot_config.r1,
1289  visir_img_phot_config.r2,
1290  &(flux_snr),
1291  &(flux_snr_noise),
1292  &(flux_snr_radius),
1293  &(flux_tot),
1294  &(beamshape)));
1295 
1296 
1297  /* Compute the results */
1298  /* Flux */
1299  visir_img_phot_config.flux_snr = flux_snr;
1300 
1301  /* Flux noise */
1302  visir_img_phot_config.flux_snr_noise = flux_snr_noise;
1303 
1304  visir_img_phot_config.flux_snr_radius[0] = flux_snr_radius;
1305 
1306  /* Total flux */
1307  visir_img_phot_config.flux_tot = flux_tot;
1308 
1309  /* FWHM */
1310  visir_img_phot_config.beamshape = beamshape;
1311  visir_img_phot_config.fwhm_x_pos1 = beamshape.fwhm_x;
1312  visir_img_phot_config.fwhm_y_pos1 = beamshape.fwhm_y;
1313 
1314  /* Get lam and dlam from the filter name for the Strehl computation */
1315  if (visir_get_filter_infos(visir_img_phot_config.filter, &lam, &dlam)) {
1316  cpl_msg_error(cpl_func, "Central wavelength and width is missing for "
1317  "filter: %s", visir_img_phot_config.filter);
1318  skip_if(1);
1319  }
1320 
1321  /* Strehl computation */
1322  cpl_errorstate errstate = cpl_errorstate_get();
1323  if (irplib_strehl_compute(combined, STREHL_M1, STREHL_M2, lam, dlam,
1324  visir_img_phot_config.pscale,
1325  STREHL_BOX_SIZE, x, y, STREHL_STAR_RADIUS,
1326  STREHL_BG_R1, STREHL_BG_R2, -1, -1,
1327  &(visir_img_phot_config.strehl),
1328  &(visir_img_phot_config.strehl_err),
1329  &star_bg, &star_peak, &star_flux, &psf_peak, &psf_flux,
1330  &bg_noise)) {
1331  cpl_msg_error(cpl_func, "Could not compute the strehl: '%s' in %s",
1332  cpl_error_get_message(), cpl_error_get_where());
1333  cpl_errorstate_set(errstate);
1334  }
1335 
1336 
1337  end_skip;
1338 
1339  return cpl_error_get_code();
1340 
1341 }
1342 
1343 /*----------------------------------------------------------------------------*/
1352 /*----------------------------------------------------------------------------*/
1353 static int visir_img_phot_flux_four(const cpl_image * combined)
1354 {
1355  cpl_errorstate cleanstate = cpl_errorstate_get();
1356  cpl_image * min_combined = NULL;
1357  cpl_apertures * appos = NULL;
1358  cpl_apertures * apneg = NULL;
1359  cpl_vector * sigmas = NULL;
1360  double psigmas[] = {5, 2, 1, 0.5};
1361  double x[4];
1362  double y[4];
1363  double flux_snr[4];
1364  double flux_snr_noise[4];
1365  int flux_snr_radius[4];
1366  double flux_tot[4];
1367  beamshape_t beamshapes[4];
1368  double dist1, dist2;
1369  int ngood_fwhm;
1370  double lam, dlam;
1371  double star_bg,star_peak,star_flux,psf_peak,psf_flux,bg_noise;
1372  double eccmin = DBL_MAX;
1373  const int nsigmas = sizeof(psigmas)/sizeof(double);
1374  int isigma;
1375  int ipos1, ipos2, ineg1, ineg2;
1376  int i;
1377  int iappos2[] = {0, 0}; /* Avoid (false) uninit warning */
1378  int iapneg2[] = {0, 0}; /* Avoid (false) uninit warning */
1379 
1380 
1381  if (cpl_error_get_code()) return cpl_error_get_code();
1382 
1383  cpl_ensure_code(combined != NULL, CPL_ERROR_NULL_INPUT);
1384 
1385  cpl_msg_info(cpl_func, "Detecting the 4-source star using %d sigma-levels "
1386  "ranging from %g down to %g", nsigmas, psigmas[0],
1387  psigmas[nsigmas-1]);
1388 
1389  sigmas = cpl_vector_new(1);
1390  min_combined = cpl_image_multiply_scalar_create(combined, -1.0);
1391  bug_if(0);
1392  for (isigma = 0; isigma < nsigmas; isigma++) {
1393 
1394  /* FIXME: Why ?! */
1395  irplib_error_recover(cleanstate, "Resetting error (why?)");
1396 
1397  bug_if(cpl_vector_set(sigmas, 0, psigmas[isigma]));
1398 
1399  /* Detect where the POSITIVE stars are */
1400  cpl_apertures_delete(appos);
1401  appos = cpl_apertures_extract(combined, sigmas, NULL);
1402  if (appos == NULL) {
1403  cpl_msg_warning(cpl_func, "Found no positive stars at sigma=%g",
1404  psigmas[isigma]);
1405  continue;
1406  }
1407  if (cpl_apertures_get_size(appos) < 2) {
1408  cpl_msg_warning(cpl_func, "Found just 1 positive star at sigma=%g, "
1409  "need two", psigmas[isigma]);
1410  continue;
1411  }
1412 
1413  /* Detect where the NEGATIVE stars are */
1414  cpl_apertures_delete(apneg);
1415  apneg = cpl_apertures_extract(min_combined, sigmas, NULL);
1416  if (apneg == NULL) {
1417  cpl_msg_warning(cpl_func, "Found no negative stars at sigma=%g",
1418  psigmas[isigma]);
1419  continue;
1420  }
1421  if (cpl_apertures_get_size(apneg) < 2) {
1422  cpl_msg_warning(cpl_func, "Found just 1 negative star at sigma=%g, "
1423  "need two", psigmas[isigma]);
1424  continue;
1425  }
1426 
1427  cpl_msg_info(cpl_func, "Found positive and negative stars at sigma=%g",
1428  psigmas[isigma]);
1429  break;
1430  }
1431 
1432  skip_if(appos == NULL);
1433  skip_if(apneg == NULL);
1434  skip_if (cpl_apertures_get_size(appos) < 2);
1435  skip_if (cpl_apertures_get_size(apneg) < 2);
1436 
1437  if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
1438  cpl_apertures_dump(appos, stdout);
1439  cpl_apertures_dump(apneg, stdout);
1440  }
1441 
1442  if (cpl_apertures_get_size(appos) > 2 || cpl_apertures_get_size(apneg) > 2)
1443  cpl_msg_info(cpl_func, "Selecting from %d positive and %d negative "
1444  "stars two pairs that outline a square",
1445  (int)cpl_apertures_get_size(appos),
1446  (int)cpl_apertures_get_size(apneg));
1447 
1448  for (ipos1 = 2; ipos1 <= cpl_apertures_get_size(appos); ipos1++) {
1449  for (ipos2 = 1; ipos2 < ipos1; ipos2++) {
1450  for (ineg1 = 2; ineg1 <= cpl_apertures_get_size(apneg); ineg1++) {
1451  for (ineg2 = 1; ineg2 < ineg1; ineg2++) {
1452  const double ecc = visir_img_phot_eccent_four(appos, ipos1,
1453  ipos2, apneg,
1454  ineg1, ineg2);
1455 
1456  if (ecc < eccmin) {
1457  if (eccmin < DBL_MAX)
1458  cpl_msg_debug(cpl_func, "Found star positions with "
1459  "reduced non-square-ness [pixel]: "
1460  "%g < %g", ecc, eccmin);
1461  eccmin = ecc;
1462  iappos2[0] = ipos1;
1463  iappos2[1] = ipos2;
1464  iapneg2[0] = ineg1;
1465  iapneg2[1] = ineg2;
1466  }
1467  }
1468  }
1469  }
1470  }
1471 
1472  /* Star 1 should have largest flux */
1473  if (cpl_apertures_get_flux(appos, iappos2[0]) <
1474  cpl_apertures_get_flux(appos, iappos2[1])) {
1475  const int tmp = iappos2[0];
1476  iappos2[0] = iappos2[1];
1477  iappos2[1] = tmp;
1478  }
1479  if (cpl_apertures_get_flux(apneg, iapneg2[0]) <
1480  cpl_apertures_get_flux(apneg, iapneg2[1])) {
1481  const int tmp = iapneg2[0];
1482  iapneg2[0] = iapneg2[1];
1483  iapneg2[1] = tmp;
1484  }
1485 
1486 
1487  x[0] = cpl_apertures_get_centroid_x(appos, iappos2[0]);
1488  y[0] = cpl_apertures_get_centroid_y(appos, iappos2[0]);
1489  x[1] = cpl_apertures_get_centroid_x(appos, iappos2[1]);
1490  y[1] = cpl_apertures_get_centroid_y(appos, iappos2[1]);
1491 
1492  x[2] = cpl_apertures_get_centroid_x(apneg, iapneg2[0]);
1493  y[2] = cpl_apertures_get_centroid_y(apneg, iapneg2[0]);
1494  x[3] = cpl_apertures_get_centroid_x(apneg, iapneg2[1]);
1495  y[3] = cpl_apertures_get_centroid_y(apneg, iapneg2[1]);
1496 
1497  cpl_apertures_delete(appos);
1498  cpl_apertures_delete(apneg);
1499  appos = NULL;
1500  apneg = NULL;
1501 
1502  cpl_msg_info(cpl_func, "Positive star 1 at position %g %g", x[0], y[0]);
1503  cpl_msg_info(cpl_func, "Positive star 2 at position %g %g", x[1], y[1]);
1504 
1505  cpl_msg_info(cpl_func, "Negative star 1 at position %g %g", x[2], y[2]);
1506  cpl_msg_info(cpl_func, "Negative star 2 at position %g %g", x[3], y[3]);
1507 
1508  /* FIXME: Useful ? - change to 1D-distances - and move into sigma loop */
1509  dist1 = sqrt((x[2]-x[0])*(x[2]-x[0])+(y[2]-y[0])*(y[2]-y[0]));
1510  dist2 = sqrt((x[3]-x[1])*(x[3]-x[1])+(y[3]-y[1])*(y[3]-y[1]));
1511  cpl_msg_info(cpl_func, "Star 1 Pos/Neg Distance: %g", dist1);
1512  cpl_msg_info(cpl_func, "Star 2 Pos/Neg Distance: %g", dist2);
1513 
1514  if (eccmin < DBL_MAX) {
1515  cpl_msg_info(cpl_func, "The deviation from a horizontal square by "
1516  "the two star pairs [pixel]: %g", eccmin);
1517  if (eccmin > VISIR_IMG_PHOT_POS_WARN) {
1518  if (eccmin > VISIR_IMG_PHOT_POS_ERROR) {
1519  cpl_msg_error(cpl_func, "The deviation from a horizontal square"
1520  " by the two star pairs exceed %g, the detected "
1521  "objects are wrong", VISIR_IMG_PHOT_POS_ERROR);
1522  skip_if(1);
1523  }
1524  cpl_msg_warning(cpl_func, "The deviation from a horizontal square "
1525  "by the two star pairs exceed %g, the detected "
1526  "objects may be wrong", VISIR_IMG_PHOT_POS_WARN);
1527  }
1528  }
1529 
1530  if ((int)x[0]-IRPLIB_STREHL_BORDER <= 0 ||
1531  (int)y[0]-IRPLIB_STREHL_BORDER <= 0 ||
1532  (int)x[0]+IRPLIB_STREHL_BORDER > cpl_image_get_size_x(combined) ||
1533  (int)y[0]+IRPLIB_STREHL_BORDER > cpl_image_get_size_y(combined)) {
1534  cpl_msg_error(cpl_func, "Positive star 1 at (%g,%g) is less than %d "
1535  "pixels from the image border", x[0], y[0],
1536  1+IRPLIB_STREHL_BORDER);
1537  skip_if(1);
1538  }
1539 
1540  /* Verify the stars positions */
1541  if (fabs(dist1-dist2) > VISIR_IMG_PHOT_POS_UNCERTAINTY) {
1542  cpl_msg_error(cpl_func, "Too large Pos/Neg Distance between the two "
1543  "stars: %g > %g", fabs(dist1-dist2),
1544  VISIR_IMG_PHOT_POS_UNCERTAINTY);
1545  skip_if(1);
1546  }
1547 
1548  /* Photometry on positive stars */
1549  for (i=0 ; i<2 ; i++)
1550  skip_if (visir_img_phot_flux(combined, NULL, NULL,
1551  x[i], y[i],
1552  visir_img_phot_config.r0_max,
1553  visir_img_phot_config.r1,
1554  visir_img_phot_config.r2,
1555  &(flux_snr[i]),
1556  &(flux_snr_noise[i]),
1557  &(flux_snr_radius[i]),
1558  &(flux_tot[i]),
1559  &(beamshapes[i])));
1560 
1561  /* Photometry on negative stars */
1562  for (i=2 ; i<4 ; i++)
1563  skip_if (visir_img_phot_flux(min_combined, NULL, NULL,
1564  x[i], y[i],
1565  visir_img_phot_config.r0_max,
1566  visir_img_phot_config.r1,
1567  visir_img_phot_config.r2,
1568  &(flux_snr[i]),
1569  &(flux_snr_noise[i]),
1570  &(flux_snr_radius[i]),
1571  &(flux_tot[i]),
1572  &(beamshapes[i])));
1573 
1574  cpl_image_delete(min_combined);
1575  min_combined = NULL;
1576 
1577  /* Compute the results */
1578  /* Flux */
1579  visir_img_phot_config.flux_snr = 0.0;
1580  for (i=0 ; i<4 ; i++) visir_img_phot_config.flux_snr += flux_snr[i];
1581 
1582  /* Flux noise */
1583  visir_img_phot_config.flux_snr_noise = 0.0;
1584  for (i=0 ; i<4 ; i++) visir_img_phot_config.flux_snr_noise +=
1585  flux_snr_noise[i]*flux_snr_noise[i];
1586  visir_img_phot_config.flux_snr_noise =
1587  sqrt(visir_img_phot_config.flux_snr_noise);
1588 
1589  for (i=0 ; i<4 ; i++)
1590  visir_img_phot_config.flux_snr_radius[i] = flux_snr_radius[i];
1591 
1592  /* Total flux */
1593  visir_img_phot_config.flux_tot = 0.0;
1594  for (i=0 ; i<4 ; i++) visir_img_phot_config.flux_tot += flux_tot[i];
1595 
1596  /* FWHM */
1597  ngood_fwhm = 0;
1598  visir_img_phot_config.beamshape.fwhm_x = 0.0;
1599  for (i=0 ; i<4 ; i++) {
1600  if (beamshapes[i].fwhm_x > 0.0) {
1601  visir_img_phot_config.beamshape.fwhm_x += beamshapes[i].fwhm_x;
1602  ngood_fwhm ++;
1603  }
1604  }
1605  if (ngood_fwhm > 0) visir_img_phot_config.beamshape.fwhm_x /= ngood_fwhm;
1606  else visir_img_phot_config.beamshape.fwhm_x = -1.0;
1607  ngood_fwhm = 0;
1608  visir_img_phot_config.beamshape.fwhm_y = 0.0;
1609  for (i=0 ; i<4 ; i++) {
1610  if (beamshapes[i].fwhm_y > 0.0) {
1611  visir_img_phot_config.beamshape.fwhm_y += beamshapes[i].fwhm_y;
1612  ngood_fwhm ++;
1613  }
1614  }
1615  if (ngood_fwhm > 0) visir_img_phot_config.beamshape.fwhm_y /= ngood_fwhm;
1616  else visir_img_phot_config.beamshape.fwhm_y = -1.0;
1617  visir_img_phot_config.fwhm_x_pos1 = beamshapes[0].fwhm_x;
1618  visir_img_phot_config.fwhm_y_pos1 = beamshapes[0].fwhm_y;
1619  visir_img_phot_config.fwhm_x_pos2 = beamshapes[1].fwhm_x;
1620  visir_img_phot_config.fwhm_y_pos2 = beamshapes[1].fwhm_y;
1621  visir_img_phot_config.fwhm_x_neg1 = beamshapes[2].fwhm_x;
1622  visir_img_phot_config.fwhm_y_neg1 = beamshapes[2].fwhm_y;
1623  visir_img_phot_config.fwhm_x_neg2 = beamshapes[3].fwhm_x;
1624  visir_img_phot_config.fwhm_y_neg2 = beamshapes[3].fwhm_y;
1625 
1626  /* Get lam and dlam from the filter name for the Strehl computation */
1627  if (visir_get_filter_infos(visir_img_phot_config.filter, &lam, &dlam)) {
1628  cpl_msg_error(cpl_func, "Central wavelength and width is missing for "
1629  "filter: %s", visir_img_phot_config.filter);
1630  skip_if(1);
1631  }
1632 
1633  /* Strehl computation */
1634  cpl_errorstate errstate = cpl_errorstate_get();
1635  if (irplib_strehl_compute(combined, STREHL_M1, STREHL_M2, lam, dlam,
1636  visir_img_phot_config.pscale,
1637  STREHL_BOX_SIZE, x[0], y[0], STREHL_STAR_RADIUS,
1638  STREHL_BG_R1, STREHL_BG_R2, -1, -1,
1639  &(visir_img_phot_config.strehl),
1640  &(visir_img_phot_config.strehl_err),
1641  &star_bg, &star_peak, &star_flux, &psf_peak, &psf_flux,
1642  &bg_noise)) {
1643  cpl_msg_error(cpl_func, "Could not compute the strehl: '%s' in %s",
1644  cpl_error_get_message(), cpl_error_get_where());
1645  cpl_errorstate_set(errstate);
1646  }
1647 
1648 
1649  end_skip;
1650 
1651  cpl_apertures_delete(appos);
1652  cpl_apertures_delete(apneg);
1653  cpl_vector_delete(sigmas);
1654  cpl_image_delete(min_combined);
1655 
1656  return cpl_error_get_code();
1657 
1658 }
1659 
1660 
1661 /*----------------------------------------------------------------------------*/
1677 /*----------------------------------------------------------------------------*/
1678 static double visir_img_phot_eccent_four(const cpl_apertures * appos,
1679  int ipos1, int ipos2,
1680  const cpl_apertures * apneg,
1681  int ineg1, int ineg2)
1682 {
1683 
1684  /* NB: Lower left pixel is (1, 1) */
1685 
1686  /* The two positive points */
1687  const double xp1 = cpl_apertures_get_centroid_x(appos, ipos1);
1688  const double xp2 = cpl_apertures_get_centroid_x(appos, ipos2);
1689  const double yp1 = cpl_apertures_get_centroid_y(appos, ipos1);
1690  const double yp2 = cpl_apertures_get_centroid_y(appos, ipos2);
1691 
1692  /* The leftmost positive point */
1693  const double xpl = xp1 < xp2 ? xp1 : xp2;
1694  const double ypl = xp1 < xp2 ? yp1 : yp2;
1695 
1696  /* The rightmost positive point */
1697  const double xpr = xp1 < xp2 ? xp2 : xp1;
1698  const double ypr = xp1 < xp2 ? yp2 : yp1;
1699 
1700  /* The two negative points */
1701  const double xn1 = cpl_apertures_get_centroid_x(apneg, ineg1);
1702  const double yn1 = cpl_apertures_get_centroid_y(apneg, ineg1);
1703  const double xn2 = cpl_apertures_get_centroid_x(apneg, ineg2);
1704  const double yn2 = cpl_apertures_get_centroid_y(apneg, ineg2);
1705 
1706  /* The leftmost negative point */
1707  const double xln = xn1 < xn2 ? xn1 : xn2;
1708  const double yln = xn1 < xn2 ? yn1 : yn2;
1709 
1710  /* The rightmost engative point */
1711  const double xrn = xn1 < xn2 ? xn2 : xn1;
1712  const double yrn = xn1 < xn2 ? yn2 : yn1;
1713 
1714  const double lx1 = xrn - xpl; /* The length of the top x-side */
1715  const double lx2 = xpr - xln; /* The length of the bottom x-side */
1716  const double ly1 = ypl - yln; /* The length of the left y-side */
1717  const double ly2 = yrn - ypr; /* The length of the right y-side */
1718 
1719  const double lmean = 0.25 * ( lx1 + lx2 + ly1 + ly2);
1720 
1721  const double dx1 = lx1 - lmean;
1722  const double dx2 = lx2 - lmean;
1723  const double dy1 = ly1 - lmean;
1724  const double dy2 = ly2 - lmean;
1725 
1726  const double ey1 = yrn - ypl; /* The displacement in the top x-side */
1727  const double ey2 = ypr - yln; /* The displacement in the bottom x-side */
1728  const double ex1 = xpl - xln; /* The displacement in the left y-side */
1729  const double ex2 = xpr - xrn; /* The displacement in the right y-side */
1730 
1731  const double result = sqrt(dx1 * dx1 + dx2 * dx2 + dy1 * dy1 + dy2 * dy2 +
1732  ex1 * ex1 + ex2 * ex2 + ey1 * ey1 + ey2 * ey2);
1733 
1734 
1735  bug_if(0);
1736 
1737  bug_if(appos == apneg);
1738  bug_if(ipos1 == ipos2);
1739  bug_if(ineg1 == ineg2);
1740 
1741  end_skip;
1742 
1743  return result;
1744 
1745 
1746 }
1747 
1748 #ifdef VISIR_IMG_PHOT_USE_ECCENT_THREE
1749 /*----------------------------------------------------------------------------*/
1764 /*----------------------------------------------------------------------------*/
1765 static double visir_img_phot_eccent_three(const cpl_apertures * appos,
1766  int ipos,
1767  const cpl_apertures * apneg,
1768  int ineg1, int ineg2)
1769 {
1770 
1771  /* NB: Lower left pixel is (1, 1) */
1772 
1773  /* The positive point */
1774  const double xp = cpl_apertures_get_centroid_x(appos, ipos);
1775  const double yp = cpl_apertures_get_centroid_y(appos, ipos);
1776 
1777  /* The two negative points */
1778  const double xn1 = cpl_apertures_get_centroid_x(apneg, ineg1);
1779  const double yn1 = cpl_apertures_get_centroid_y(apneg, ineg1);
1780  const double xn2 = cpl_apertures_get_centroid_x(apneg, ineg2);
1781  const double yn2 = cpl_apertures_get_centroid_y(apneg, ineg2);
1782 
1783  /* The bottom negative point */
1784  const double xnb = xn1 < xn2 ? xn1 : xn2;
1785  const double ynb = xn1 < xn2 ? yn1 : yn2;
1786 
1787  /* The top engative point */
1788  const double xnt = xn1 < xn2 ? xn2 : xn1;
1789  const double ynt = xn1 < xn2 ? yn2 : yn1;
1790 
1791  const double l1 = ynt - yp; /* The length of the top line */
1792  const double l2 = yp - ynb; /* The length of the bottom line */
1793  const double ln = ynt - ynb; /* The length of the top to bottom line */
1794 
1795  const double lmean = 0.25 * ( l1 + l2 + ln);
1796 
1797  const double d1 = l1 - lmean;
1798  const double d2 = l2 - lmean;
1799  const double dn = 0.5 * ln - lmean;
1800 
1801  const double e1 = xp - xnt; /* The displacement in the top line */
1802  const double e2 = xp - xnb; /* The displacement in the bottom line */
1803 
1804  const double result = sqrt(d1 * d1 + d2 * d2 + dn * dn + e1 * e1 + e2 * e2);
1805 
1806 
1807  bug_if(0);
1808 
1809  bug_if(appos == apneg);
1810  bug_if(ineg1 == ineg2);
1811 
1812  end_skip;
1813 
1814  return result;
1815 
1816 
1817 }
1818 
1819 #endif
1820 
1821 /*----------------------------------------------------------------------------*/
1843 /*----------------------------------------------------------------------------*/
1844 static int visir_img_phot_flux(
1845  const cpl_image * combined,
1846  const cpl_image * weights_,
1847  const cpl_image * contrib,
1848  double x_pos,
1849  double y_pos,
1850  int r0_max,
1851  int r1,
1852  int r2,
1853  double * flux_snr,
1854  double * flux_snr_noise,
1855  int * flux_snr_radius,
1856  double * flux_tot,
1857  beamshape_t * beamshape)
1858 {
1859  cpl_apertures * apert = NULL;
1860  cpl_image * labels;
1861  cpl_image * bg_subtracted = NULL;
1862  cpl_vector * r0 = NULL;
1863  cpl_vector * fl = NULL;
1864  cpl_vector * fl_noise = NULL;
1865  cpl_vector * snr = NULL;
1866  double bg;
1867  double max_val = 0.0; /* Avoid (false) uninit warning */
1868  int max_ind = 0; /* Avoid (false) uninit warning */
1869  int i;
1870  cpl_size nx, ny;
1871  int max_contrib = 1;
1872  cpl_image * weights = weights_ == NULL ?
1873  NULL : cpl_image_duplicate(weights_);
1874 
1875 
1876  if (cpl_error_get_code()) return cpl_error_get_code();
1877 
1878  cpl_ensure_code(combined != NULL, CPL_ERROR_NULL_INPUT);
1879  cpl_ensure_code(r0_max > 0, CPL_ERROR_ILLEGAL_INPUT);
1880 
1881  nx = cpl_image_get_size_x(combined);
1882  ny = cpl_image_get_size_y(combined);
1883 
1884  if (weights) {
1885  cpl_ensure_code(nx == cpl_image_get_size_x(weights) &&
1886  ny == cpl_image_get_size_y(weights),
1887  CPL_ERROR_ILLEGAL_INPUT);
1888  }
1889 
1890  if (contrib) {
1891  cpl_ensure_code(nx == cpl_image_get_size_x(contrib) &&
1892  ny == cpl_image_get_size_y(contrib),
1893  CPL_ERROR_ILLEGAL_INPUT);
1894  max_contrib = cpl_image_get_max(contrib);
1895  cpl_ensure_code(max_contrib >= 1, CPL_ERROR_ILLEGAL_INPUT);
1896  }
1897 
1898  /* Create the label image defining the background ring */
1899  if ((labels = visir_create_ring_intimage(nx, ny,
1900  (int)x_pos, (int)y_pos, r1, r2)) == NULL) {
1901  cpl_msg_error(cpl_func, "Could not create a ring image");
1902  skip_if(1);
1903  }
1904  /* Compute the background */
1905  apert = cpl_apertures_new_from_image(combined, labels);
1906  cpl_image_delete(labels);
1907  labels = NULL;
1908  bg = cpl_apertures_get_median(apert, 1);
1909  cpl_apertures_delete(apert);
1910  apert = NULL;
1911  cpl_msg_info(cpl_func, "Background : %g", bg);
1912 
1913  /* Create the label image defining the total star disk */
1914  if ((labels = visir_create_disk_intimage(nx, ny,
1915  (int)x_pos, (int)y_pos, r0_max)) == NULL) {
1916  cpl_msg_error(cpl_func, "Could not create a disk image");
1917  skip_if(1);
1918  }
1919 
1920  /* Compute the total flux and the associated error */
1921 
1922  bg_subtracted = cpl_image_subtract_scalar_create(combined, bg);
1923 
1924  /* due to bad pixels rescale the sum to equal contribution */
1925  if (contrib) {
1926  cpl_image * scale = cpl_image_cast(contrib, CPL_TYPE_DOUBLE);
1927  cpl_image_power(scale, -1);
1928  cpl_image_multiply_scalar(scale, max_contrib);
1929  cpl_image_multiply(bg_subtracted, scale);
1930  if (weights)
1931  cpl_image_multiply(weights, scale);
1932  cpl_image_delete(scale);
1933  }
1934 
1935  apert = cpl_apertures_new_from_image(bg_subtracted, labels);
1936  cpl_image_delete(labels);
1937  labels = NULL;
1938  *flux_tot = cpl_apertures_get_flux(apert, 1);
1939  cpl_apertures_delete(apert);
1940  apert = NULL;
1941  cpl_msg_info(cpl_func, "Star total flux (error): %g", *flux_tot);
1942 
1943  /* Create and fill r0 */
1944  r0 = cpl_vector_new(r0_max);
1945  for (i=0 ; i<r0_max ; i++) cpl_vector_set(r0, i, i+1);
1946 
1947  /* Create fl, fl_noise */
1948  fl = cpl_vector_new(r0_max);
1949  fl_noise = cpl_vector_new(r0_max);
1950 
1951  /* For each radius, compute fl and fl_noise */
1952  for (i=0 ; i<r0_max ; i++) {
1953  float fl_val = 0.f, fl_noise_val = 0.f;
1954  /* Create the label image defining the current star disk */
1955  if ((labels = visir_create_disk_intimage(nx, ny,
1956  (int)x_pos, (int)y_pos, (int)cpl_vector_get(r0, i))) == NULL) {
1957  cpl_msg_error(cpl_func, "Could not create a disk image: %d", i);
1958  break;
1959  }
1960 
1961  if (weights) {
1962  double sum = 0, esum = 0;
1963  int d;
1964  for (int ix = 1; ix < 1 + nx; ix++) {
1965  for (int iy = 1; iy < 1 + ny; iy++) {
1966  if (!cpl_image_get(labels, ix, iy, &d) || d)
1967  continue;
1968 
1969  sum += cpl_image_get(bg_subtracted, ix, iy, &d);
1970  /* assume gaussian error propagation, e = sqrt(1/w)
1971  * => e = sqrt(sum_i(1/w_i)) */
1972  esum += (1. / cpl_image_get(weights, ix, iy, &d));
1973  }
1974  }
1975  fl_val = sum;
1976  fl_noise_val = sqrt(esum);
1977  }
1978  else {
1979  cpl_apertures * pert =
1980  cpl_apertures_new_from_image(bg_subtracted, labels);
1981  /* FIXME: pert == NULL ? */
1982  fl_val = cpl_apertures_get_flux(pert, 1);
1983  fl_noise_val = visir_img_phot_config.bg_sigma *
1984  sqrt((double)cpl_apertures_get_npix(pert, 1));
1985  cpl_apertures_delete(pert);
1986  }
1987 
1988  /* Compute the statistics on the zone defined by the labels */
1989  cpl_image_delete(labels);
1990  labels = NULL;
1991  cpl_vector_set(fl, i, fl_val);
1992  cpl_vector_set(fl_noise, i, fl_noise_val);
1993  }
1994  skip_if( 0 );
1995 
1996  /* Compute the flux (and error) for the best signal to noise */
1997  snr = cpl_vector_duplicate(fl);
1998  cpl_vector_divide(snr, fl_noise);
1999  for (i=0 ; i<r0_max ; i++) {
2000  if (i == 0 || max_val < cpl_vector_get(snr, i)) {
2001  max_val = cpl_vector_get(snr, i);
2002  max_ind = i;
2003  }
2004  }
2005  *flux_snr = cpl_vector_get(fl, max_ind);
2006  *flux_snr_noise = cpl_vector_get(fl_noise, max_ind);
2007  *flux_snr_radius = (int)cpl_vector_get(r0, max_ind);
2008  cpl_msg_info(cpl_func, "Best SNR star flux : %g (%g) for radius %d <= %d "
2009  " [pixel]", *flux_snr, *flux_snr_noise,
2010  (int)cpl_vector_get(r0, max_ind),
2011  (int)cpl_vector_get(r0, r0_max-1));
2012 
2013  if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
2014  cpl_matrix * mdump = cpl_matrix_new(1+r0_max, 3);
2015  for (i = 0; i < r0_max; i++) {
2016  cpl_matrix_set(mdump, i+1, 0, cpl_vector_get(fl, i));
2017  cpl_matrix_set(mdump, i+1, 1, cpl_vector_get(fl_noise, i));
2018  cpl_matrix_set(mdump, i+1, 2, cpl_vector_get(snr, i));
2019  }
2020  cpl_msg_debug(cpl_func, "Flux, flux-noise and S/N for radii out to %d",
2021  (int)cpl_vector_get(r0, r0_max-1));
2022 
2023  cpl_matrix_dump(mdump, stdout);
2024  cpl_matrix_delete(mdump);
2025  }
2026 
2027  *beamshape = (beamshape_t){.fwhm_x = -1., .fwhm_y = -1.,
2028  .peak = -1., .peak_err = 0.,
2029  .major = -1., .major_err = 0.,
2030  .minor = -1., .minor_err = 0.,
2031  .angle = -1., .angle_err = 0.};
2032 
2033  /* Compute the FWHM */
2034  skip_if (cpl_image_get_fwhm(bg_subtracted, (int)x_pos, (int)y_pos, &beamshape->fwhm_x,
2035  &beamshape->fwhm_y));
2036 
2037  skip_if ( beamshape->fwhm_x <= 0.0 );
2038  skip_if ( beamshape->fwhm_y <= 0.0 );
2039 
2040  cpl_errorstate cleanstate = cpl_errorstate_get();
2041 
2042  if (fit_2d_gauss(bg_subtracted, weights, (cpl_size)x_pos, (cpl_size)y_pos,
2043  beamshape->fwhm_x, beamshape->fwhm_y,
2044  &beamshape->peak, &beamshape->peak_err,
2045  &beamshape->major, &beamshape->major_err,
2046  &beamshape->minor, &beamshape->minor_err,
2047  &beamshape->angle, &beamshape->angle_err) == CPL_ERROR_NONE) {
2048  cpl_msg_info(cpl_func, "Peak: %g +- %g, FWHM : %g +- %g major ; %g +- %g minor, "
2049  "angle %g +- %g", beamshape->peak, beamshape->peak_err,
2050  beamshape->major, beamshape->major_err,
2051  beamshape->minor, beamshape->minor_err, beamshape->angle *
2052  CPL_MATH_DEG_RAD, beamshape->angle_err *
2053  CPL_MATH_DEG_RAD);
2054  }
2055  else {
2056  cpl_msg_warning(cpl_func, "2D gauss fit failed, approximate FWHM : %g"
2057  "in x ; %g in y ", beamshape->fwhm_x, beamshape->fwhm_y);
2058  cpl_errorstate_set(cleanstate);
2059  }
2060 
2061  end_skip;
2062 
2063  cpl_apertures_delete(apert);
2064  cpl_image_delete(labels);
2065  cpl_image_delete(bg_subtracted);
2066  cpl_image_delete(weights);
2067 
2068  cpl_vector_delete(r0);
2069  cpl_vector_delete(fl);
2070  cpl_vector_delete(snr);
2071  cpl_vector_delete(fl_noise);
2072 
2073  return cpl_error_get_code();
2074 }
2075 
2076 
2077 /*----------------------------------------------------------------------------*/
2088 /*----------------------------------------------------------------------------*/
2089 static cpl_error_code visir_get_filter_infos(
2090  const char * f,
2091  double * pcwlen,
2092  double * pdwlen)
2093 {
2094 
2095  double cwlen = -1;
2096  double dwlen = -1;
2097 
2098  cpl_ensure_code(f, CPL_ERROR_NULL_INPUT);
2099  cpl_ensure_code(pcwlen, CPL_ERROR_NULL_INPUT);
2100  cpl_ensure_code(pdwlen, CPL_ERROR_NULL_INPUT);
2101 
2102  skip_if (!strcmp(f, "MV"));
2103 
2104  if (!strcmp(f, "N-BAND") || !strcmp(f, "N_BAND"))
2105  { cwlen = 10.56; dwlen = 5.37;}
2106  else if (!strcmp(f, "SIC")) { cwlen = 11.848;dwlen = 2.34;}
2107  else if (!strcmp(f, "PAH1_1")) { cwlen = 8.19;dwlen = 0.15;}
2108  else if (!strcmp(f, "PAH1")) { cwlen = 8.586;dwlen = 0.421;}
2109  else if (!strcmp(f, "ARIII")) { cwlen = 8.992;dwlen = 0.138;}
2110  else if (!strcmp(f, "SIV_1")) { cwlen = 10.02;dwlen = 0.18;}
2111  else if (!strcmp(f, "SIV")) { cwlen = 10.485;dwlen = 0.159;}
2112  else if (!strcmp(f, "PAH2_1")) { cwlen = 10.76;dwlen = 0.69;}
2113  else if (!strcmp(f, "SIV_2")) { cwlen = 11.1;dwlen = 0.19;}
2114  else if (!strcmp(f, "PAH2")) { cwlen = 11.254;dwlen = 0.594;}
2115  else if (!strcmp(f, "PAH2_2")) { cwlen = 12.13;dwlen = 0.37;}
2116  else if (!strcmp(f, "NEII_1")) { cwlen = 12.51;dwlen = 0.18;}
2117  else if (!strcmp(f, "NEII")) { cwlen = 12.805;dwlen = 0.21;}
2118  else if (!strcmp(f, "NEII_2")) { cwlen = 13.036;dwlen = 0.219;}
2119  else if (!strcmp(f, "Q0")) { cwlen = 16.554;dwlen = 0.398;}
2120  else if (!strcmp(f, "QH2")) { cwlen = 17.11;dwlen = 0.398;}
2121  else if (!strcmp(f, "Q1")) { cwlen = 17.653;dwlen = 0.83;}
2122  else if (!strcmp(f, "Q2")) { cwlen = 18.718;dwlen = 0.878;}
2123  else if (!strcmp(f, "Q3")) { cwlen = 19.5;dwlen = 0.4;}
2124  else if (!strcmp(f, "Q4")) { cwlen = 20.5;dwlen = 1.0;}
2125  else if (!strcmp(f, "Q7")) { cwlen = 23.1;dwlen = 0.8;}
2126  else if (!strcmp(f, "Q8")) { cwlen = 24.5;dwlen = 0.8;}
2127  else if (!strcmp(f, "N-SW-spec") || !strcmp(f, "N_SW_spec"))
2128  { cwlen = 8.85; dwlen = 2.7;}
2129  else if (!strcmp(f, "H2S4-spec") || !strcmp(f, "H2S4_spec"))
2130  { cwlen = 8.12; dwlen = 0.3;}
2131  else if (!strcmp(f, "ARIII-spec") || !strcmp(f, "ARIII_spec"))
2132  { cwlen = 8.44; dwlen = 0.78;}
2133  else if (!strcmp(f, "NEII_2-spec") || !strcmp(f, "NEII_2_spec"))
2134  { cwlen = 12.805; dwlen = 0.2;}
2135  else if (!strcmp(f, "H2S3-spec") || !strcmp(f, "H2S3_spec"))
2136  { cwlen = 9.62; dwlen = 0.2;}
2137  else if (!strcmp(f, "H2S1-spec") || !strcmp(f, "H2S1_spec"))
2138  { cwlen = 17.0;dwlen = 0.4;}
2139  else if (!strcmp(f, "M-BAND") || !strcmp(f, "M_BAND"))
2140  { cwlen = 4.82;dwlen = 0.35;}
2141 
2142  /* The width of each new, below filter is the Full Width at Half Maximum */
2143 
2144  else if (!strcmp(f, "J7.9") || !strcmp(f, "J7_9"))
2145  { cwlen = (7.483 + 8.035)/2.0;
2146  dwlen = 8.035 - 7.483;}
2147  else if (!strcmp(f, "J8.9") || !strcmp(f, "J8_9"))
2148  { cwlen = (8.338 + 9.068)/2.0;
2149  dwlen = 9.068 - 8.338;}
2150  else if (!strcmp(f, "J9.8") || !strcmp(f, "J9_8"))
2151  { cwlen = (9.123 + 10.059)/2.0;
2152  dwlen = 10.059 - 9.123;}
2153  else if (!strcmp(f, "J12.2") || !strcmp(f, "J12_2"))
2154  { cwlen = (11.700 + 12.216)/2.0;
2155  dwlen = 12.216 - 11.700;}
2156  else if (!strcmp(f, "B8.7") || !strcmp(f, "B8_7"))
2157  { cwlen = (8.436 + 9.410)/2.0;
2158  dwlen = 9.410 - 8.436;}
2159  else if (!strcmp(f, "B9.7") || !strcmp(f, "B9_7"))
2160  { cwlen = (9.402 + 10.242)/2.0;
2161  dwlen = 10.242 - 9.402;}
2162  else if (!strcmp(f, "B10.7") || !strcmp(f, "B10_7"))
2163  { cwlen = (9.970 + 11.338)/2.0;
2164  dwlen = 11.338 - 9.970;}
2165  else if (!strcmp(f, "B11.7") || !strcmp(f, "B11_7"))
2166  { cwlen = (11.098 + 11.950)/2.0;
2167  dwlen = 11.950 - 11.098;}
2168  else if (!strcmp(f, "B12.4") || !strcmp(f, "B12_4"))
2169  { cwlen = (11.971 + 12.961)/2.0;
2170  dwlen = 12.961 - 11.971;}
2171 
2172  *pcwlen = cwlen;
2173  *pdwlen = dwlen;
2174 
2175  cpl_ensure_code(cwlen > 0, CPL_ERROR_DATA_NOT_FOUND);
2176  cpl_ensure_code(dwlen > 0, CPL_ERROR_DATA_NOT_FOUND);
2177 
2178  end_skip;
2179 
2180  return cpl_error_get_code();
2181 }
2182 
2183 /*----------------------------------------------------------------------------*/
2191 /*----------------------------------------------------------------------------*/
2192 static cpl_error_code visir_img_phot_qc(cpl_propertylist * qclist,
2193  cpl_boolean drop_wcs,
2194  const irplib_framelist * rawframes)
2195 {
2196 
2197  const cpl_propertylist * reflist
2199 
2200 
2201  /* QC.EXPTIME */
2202  bug_if (cpl_propertylist_append_double(qclist, "ESO QC EXPTIME",
2203  visir_img_phot_config.exptime));
2204  /* QC.JYVAL */
2205  bug_if (cpl_propertylist_append_double(qclist, "ESO QC JYVAL",
2206  visir_img_phot_config.jy_val));
2207  /* QC.STARNAME */
2208  bug_if (cpl_propertylist_append_string(qclist, "ESO QC STARNAME",
2209  visir_img_phot_config.star_name));
2210  /* QC.FILTER */
2211  bug_if (cpl_propertylist_append_string(qclist, "ESO QC FILTER",
2212  visir_img_phot_config.filter));
2213 
2214  /* QC.NELEC */
2215  bug_if (cpl_propertylist_append_double(qclist, "ESO QC NELEC",
2216  visir_img_phot_config.nelec));
2217  /* QC.NPHOT */
2218  bug_if (cpl_propertylist_append_double(qclist, "ESO QC NPHOT",
2219  visir_img_phot_config.nphot));
2220  /* QC.BACKGD.SIGMA */
2221  bug_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD SIGMA",
2222  visir_img_phot_config.bg_sigma));
2223  /* QC.FLUXTOT */
2224  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FLUXTOT",
2225  visir_img_phot_config.flux_tot));
2226  /* QC.FLUXSNR */
2227  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FLUXSNR",
2228  visir_img_phot_config.flux_snr));
2229  /* QC.FLUXSNR.NOISE */
2230  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FLUXSNR NOISE",
2231  visir_img_phot_config.flux_snr_noise));
2232  /* QC.FLUXSNR.RADIUS */
2233  for (int i = 0; i < 4; i++) {
2234  const int r = visir_img_phot_config.flux_snr_radius[i];
2235  char buffer[100];
2236  sprintf(buffer, "ESO QC FLUXSNR RADIUS%d", i + 1);
2237  bug_if (cpl_propertylist_append_int(qclist, buffer, r));
2238  }
2239  /* QC.FWHMX */
2240  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX",
2241  visir_img_phot_config.beamshape.fwhm_x));
2242  /* QC.FWHMY */
2243  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY",
2244  visir_img_phot_config.beamshape.fwhm_y));
2245  bug_if (cpl_propertylist_append_double(qclist, "ESO QC GAUSSFIT FWHM_MAX",
2246  visir_img_phot_config.beamshape.major));
2247  cpl_propertylist_set_comment(qclist, "ESO QC GAUSSFIT FWHM_MAX",
2248  "major axis [pix]");
2249  bug_if (cpl_propertylist_append_double(qclist, "ESO QC GAUSSFIT FWHM_MAX_ERR",
2250  visir_img_phot_config.beamshape.major_err));
2251  bug_if (cpl_propertylist_append_double(qclist, "ESO QC GAUSSFIT FWHM_MIN",
2252  visir_img_phot_config.beamshape.minor));
2253  cpl_propertylist_set_comment(qclist, "ESO QC GAUSSFIT FWHM_MIN",
2254  "minor axis [pix]");
2255  bug_if (cpl_propertylist_append_double(qclist, "ESO QC GAUSSFIT FWHM_MIN_ERR",
2256  visir_img_phot_config.beamshape.minor_err));
2257  bug_if (cpl_propertylist_append_double(qclist, "ESO QC GAUSSFIT ANGLE",
2258  visir_img_phot_config.beamshape.angle * CPL_MATH_DEG_RAD));
2259  cpl_propertylist_set_comment(qclist, "ESO QC GAUSSFIT ANGLE",
2260  "[deg]");
2261  bug_if (cpl_propertylist_append_double(qclist, "ESO QC GAUSSFIT ANGLE_ERR",
2262  visir_img_phot_config.beamshape.angle_err * CPL_MATH_DEG_RAD));
2263  bug_if (cpl_propertylist_append_double(qclist, "ESO QC GAUSSFIT PEAK",
2264  visir_img_phot_config.beamshape.peak));
2265  bug_if (cpl_propertylist_append_double(qclist, "ESO QC GAUSSFIT PEAK_ERR",
2266  visir_img_phot_config.beamshape.peak_err));
2267  /* QC.FWHMX.POS1 */
2268  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX POS1",
2269  visir_img_phot_config.fwhm_x_pos1));
2270  /* QC.FWHMY.POS1 */
2271  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY POS1",
2272  visir_img_phot_config.fwhm_y_pos1));
2273  /* QC.FWHMX.POS2 */
2274  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX POS2",
2275  visir_img_phot_config.fwhm_x_pos2));
2276  /* QC.FWHMY.POS2 */
2277  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY POS2",
2278  visir_img_phot_config.fwhm_y_pos2));
2279  /* QC.FWHMX.NEG1 */
2280  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX NEG1",
2281  visir_img_phot_config.fwhm_x_neg1));
2282  /* QC.FWHMY.NEG1 */
2283  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY NEG1",
2284  visir_img_phot_config.fwhm_y_neg1));
2285  /* QC.FWHMX.NEG2 */
2286  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX NEG2",
2287  visir_img_phot_config.fwhm_x_neg2));
2288  /* QC.FWHMY.NEG2 */
2289  bug_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY NEG2",
2290  visir_img_phot_config.fwhm_y_neg2));
2291  /* QC.SENSIT */
2292  bug_if (cpl_propertylist_append_double(qclist, "ESO QC SENSIT",
2293  visir_img_phot_config.sensitivity));
2294  /* QC.AREASENSIT */
2295  bug_if (cpl_propertylist_append_double(qclist, "ESO QC AREA SENSIT",
2296  visir_img_phot_config.area_sensit));
2297  cpl_propertylist_set_comment(qclist, "ESO QC AREA SENSIT",
2298  "scaled to noise of 1 arcsecond");
2299  /* QC.CONVER */
2300  bug_if (cpl_propertylist_append_double(qclist, "ESO QC CONVER",
2301  visir_img_phot_config.conversion));
2302  /* QC.STREHL */
2303  bug_if (cpl_propertylist_append_double(qclist, "ESO QC STREHL",
2304  visir_img_phot_config.strehl));
2305  /* QC.STREHL.ERROR */
2306  bug_if (cpl_propertylist_append_double(qclist, "ESO QC STREHL ERROR",
2307  visir_img_phot_config.strehl_err));
2308 
2309  /* QC.CAPA */
2310  skip_if (visir_qc_append_capa(qclist, rawframes));
2311 
2312  if (drop_wcs) {
2313  cpl_propertylist * pcopy = cpl_propertylist_new();
2314  const cpl_error_code error
2315  = cpl_propertylist_copy_property_regexp(pcopy, reflist, "^("
2316  IRPLIB_PFITS_WCS_REGEXP
2317  ")$", 0);
2318  if (!error && cpl_propertylist_get_size(pcopy) > 0) {
2319  cpl_msg_warning(cpl_func, "Combined image will have no WCS "
2320  "coordinates");
2321  }
2322  cpl_propertylist_delete(pcopy);
2323  bug_if(0);
2324 
2325  bug_if(cpl_propertylist_copy_property_regexp(qclist, reflist, "^("
2326  VISIR_PFITS_IMG_PHOT_COPY
2327  ")$", 0));
2328  } else {
2329  bug_if(cpl_propertylist_copy_property_regexp(qclist, reflist, "^("
2330  VISIR_PFITS_IMG_PHOT_COPY
2331  "|" IRPLIB_PFITS_WCS_REGEXP
2332  ")$", 0));
2333  }
2334 
2335  bug_if (irplib_pfits_set_airmass(qclist, rawframes));
2336 
2337  end_skip;
2338 
2339  return cpl_error_get_code();
2340 
2341 }
2342 
2343 /*----------------------------------------------------------------------------*/
2355 /*----------------------------------------------------------------------------*/
2356 static cpl_error_code visir_img_phot_save(cpl_frameset * self,
2357  const cpl_parameterlist * parlist,
2358  const cpl_propertylist * qclist,
2359  const cpl_image * combined,
2360  const cpl_image * contrib,
2361  const cpl_image * beam1,
2362  const cpl_image * beam1i,
2363  const cpl_image * weights)
2364 {
2365 
2366  cpl_propertylist * xtlist = cpl_propertylist_new();
2367  const char * procatg = VISIR_IMG_PHOT_COMBINED_PROCATG;
2368 
2369  bug_if (0);
2370 
2371  if (weights) {
2372  const char * s = cpl_propertylist_get_string(qclist, "ESO QC BEAMID");
2373  bug_if(s == NULL);
2374 
2375  if (strcmp(s, "COMBINED") == 0)
2376  procatg = VISIR_IMG_PHOT_COMBINED_PROCATG;
2377  else
2378  procatg = VISIR_IMG_PHOT_ONEBEAM_PROCATG;
2379  }
2380 
2381  /* SAVE THE COMBINED IMAGE */
2382  skip_if (irplib_dfs_save_image(self, parlist, self, combined,
2383  CPL_BPP_IEEE_FLOAT, RECIPE_SAVE_STRING,
2384  procatg, qclist, NULL, visir_pipe_id,
2385  RECIPE_SAVE_STRING CPL_DFS_FITS));
2386 
2387  if (!weights) {
2388  /* THE CONTRIBUTION MAP */
2389  bug_if(cpl_propertylist_append_string(xtlist, "EXTNAME",
2390  "Contribution Map"));
2391  skip_if (cpl_image_save(contrib, RECIPE_SAVE_STRING CPL_DFS_FITS,
2392  CPL_BPP_16_UNSIGNED, xtlist, CPL_IO_EXTEND));
2393  }
2394 
2395  if (beam1 != NULL) {
2396  bug_if(weights);
2397  /* THE BEAM COLLAPSED IMAGE */
2398  skip_if (irplib_dfs_save_image(self, parlist, self, beam1,
2399  CPL_BPP_IEEE_FLOAT, RECIPE_SAVE_STRING,
2400  VISIR_IMG_PHOT_ONEBEAM_PROCATG, qclist,
2401  NULL, visir_pipe_id,
2402  RECIPE_SAVE_STRING "_onebeam" CPL_DFS_FITS));
2403 
2404  /* THE BEAM COLLAPSED CONTRIBUTION MAP */
2405  skip_if (cpl_image_save(beam1i, RECIPE_SAVE_STRING "_onebeam" CPL_DFS_FITS,
2406  CPL_BPP_8_UNSIGNED, xtlist, CPL_IO_EXTEND));
2407  }
2408 
2409  end_skip;
2410 
2411  cpl_propertylist_delete(xtlist);
2412 
2413  return cpl_error_get_code();
2414 
2415 }
2416 
2417 /*----------------------------------------------------------------------------*/
2427 /*----------------------------------------------------------------------------*/
2428 static char * visir_img_phot_filter2label(const char * self)
2429 {
2430 
2431  char * label = NULL;
2432  char * p;
2433 
2434  bug_if(self == NULL);
2435 
2436  label = cpl_strdup(self);
2437 
2438  for (p = label; *p != (char)0; p++) {
2439  if (*p == '.' || *p == '-') *p = '_';
2440  }
2441 
2442  end_skip;
2443 
2444  return label;
2445 
2446 }
double visir_parameterlist_get_double(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR parameter of type double.
double visir_pfits_get_dec(const cpl_propertylist *self)
The DEC.
Definition: visir_pfits.c:289
cpl_error_code visir_dfs_check_framelist_tag(const irplib_framelist *self)
Check the tags in a frameset (group raw only)
Definition: visir_dfs.c:173
cpl_error_code irplib_apertures_find_max_flux(const cpl_apertures *self, int *ind, int nfind)
Find the aperture(s) with the greatest flux.
const char * visir_pfits_get_starname(const cpl_propertylist *self)
The std star name.
Definition: visir_pfits.c:725
cpl_error_code irplib_pfits_set_airmass(cpl_propertylist *self, const irplib_framelist *rawframes)
Update/Set the AIRMASS property.
Definition: irplib_pfits.c:338
const char * visir_pfits_get_filter(const cpl_propertylist *self)
The filter.
Definition: visir_pfits.c:341
const cpl_propertylist * irplib_framelist_get_propertylist_const(const irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
void irplib_framelist_empty(irplib_framelist *self)
Erase all frames from a framelist.
cpl_image ** visir_img_collapse_beam(cpl_propertylist *qclist, const cpl_image *self, const cpl_parameterlist *parlist, const char *recipename, visir_chopnod_mode mode, const cpl_propertylist *plist)
Collapse the 3/4 beams of a combined image.
Definition: visir_inputs.c:504
cpl_error_code irplib_dfs_save_image(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_image *image, cpl_type_bpp bpp, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an image as a DFS-compliant pipeline product.
Definition: irplib_utils.c:192
cpl_error_code irplib_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
const char * visir_parameterlist_get_string(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR string parameter.
const char * visir_pfits_get_insmode(const cpl_propertylist *self)
The mode.
Definition: visir_pfits.c:419
double visir_pfits_get_img_weight(const cpl_propertylist *self)
The relative weight of the image compared to the other images.
Definition: visir_pfits.c:827
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: visir_dfs.c:72
double visir_pfits_get_dit(const cpl_propertylist *self)
The DIT.
Definition: visir_pfits.c:301
double visir_pfits_get_pixscale(const cpl_propertylist *self)
The pixel scale.
Definition: visir_pfits.c:631
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
const char * visir_pfits_get_chopnod_dir(const cpl_propertylist *self)
The chopping direction.
Definition: visir_pfits.c:131
cpl_error_code irplib_framelist_contains(const irplib_framelist *self, const char *key, cpl_type type, cpl_boolean is_equal, double fp_tol)
Verify that a property is present for all frames.
const char * irplib_frameset_find_file(const cpl_frameset *self, const char *tag)
Find the filename with the given tag in a frame set.
cpl_propertylist * irplib_framelist_get_propertylist(irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
cpl_error_code irplib_framelist_load_propertylist(irplib_framelist *self, int pos, int ind, const char *regexp, cpl_boolean invert)
Load the propertylist of the specified frame in the framelist.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
double visir_pfits_get_ra(const cpl_propertylist *self)
The RA.
Definition: visir_pfits.c:659
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.
cpl_image ** visir_img_recombine(const char *recipename, const cpl_parameterlist *parlist, const irplib_framelist *rawframes, const char *badpix, const char *flat, cpl_geom_combine combine_mode, cpl_boolean *pdid_resize, cpl_boolean do_spc_fix, double wlen, visir_spc_resol resol)
The VISIR imaging combination using cross correlation.