NACO Pipeline Reference Manual  4.4.0
naco_img_zpoint.c
1 /* $Id: naco_img_zpoint.c,v 1.99 2011-12-22 11:09:36 llundin Exp $
2  *
3  * This file is part of the NACO 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., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: llundin $
23  * $Date: 2011-12-22 11:09:36 $
24  * $Revision: 1.99 $
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 "naco_recipe.h"
37 #include "naco_strehl.h"
38 
39 #include "irplib_strehl.h"
40 #include "irplib_wcs.h"
41 #include "irplib_stdstar.h"
42 
43 #include <string.h>
44 
45 /*-----------------------------------------------------------------------------
46  Defines
47  -----------------------------------------------------------------------------*/
48 
49 #define RECIPE_STRING "naco_img_zpoint"
50 
51 /* @cond */
52 typedef enum _NACO_BAND_ {
53  BAND_J,
54  BAND_JS,
55  BAND_JBLOCK,
56  BAND_H,
57  BAND_K,
58  BAND_KS,
59  BAND_L,
60  BAND_M,
61  BAND_LP,
62  BAND_MP,
63  BAND_Z,
64  BAND_SZ,
65  BAND_SH,
66  BAND_SK,
67  BAND_SL,
68  BAND_UNKNOWN
69 } naco_band;
70 /* @endcond */
71 
72 /*-----------------------------------------------------------------------------
73  Private Functions prototypes
74  -----------------------------------------------------------------------------*/
75 
76 static cpl_table * naco_img_zpoint_reduce(cpl_propertylist *,
77  const irplib_framelist *,
78  const cpl_parameterlist *,
79  const char *, const char *,
80  cpl_image **);
81 
82 static cpl_error_code naco_img_zpoint_qc(cpl_propertylist *,
83  cpl_propertylist *,
84  const irplib_framelist *);
85 
86 static cpl_error_code naco_img_zpoint_save(cpl_frameset *,
87  const cpl_parameterlist *,
88  const cpl_propertylist *,
89  const cpl_propertylist *,
90  const cpl_table *,
91  const cpl_image *);
92 
93 static cpl_error_code naco_img_zpoint_reduce_one(cpl_table *, cpl_vector *, int,
94  const cpl_image *,
95  const cpl_parameterlist *,
96  const char *, double, double,
97  double, double *, double *,
98  double *, double *, double *);
99 
100 static double naco_img_zpoint_find_mag(cpl_propertylist *, double, double,
101  const char *, const char *);
102 
103 static cpl_table * naco_img_zpoint_load_std_star(const char *, double, double, double);
104 
105 static cpl_error_code naco_img_zpoint_find_std_star(cpl_table *,
106  const char *[],
107  double, double,
108  const char *,
109  cpl_boolean,
110  int *);
111 
112 static cpl_error_code naco_img_zpoint_qc_all(cpl_propertylist *,
113  cpl_propertylist *,
114  const irplib_framelist *);
115 
116 static cpl_error_code naco_img_zpoint_check_im(cpl_image **, const cpl_image *,
117  int, int, double, double,
118  double, double, double);
119 
120 static naco_band naco_get_bbfilter(const char *);
121 
122 static const char * naco_std_band_name(naco_band);
123 
124 
125 NACO_RECIPE_DEFINE(naco_img_zpoint,
126  NACO_PARAM_STAR_R |
127  NACO_PARAM_BG_RINT |
128  NACO_PARAM_BG_REXT |
129  NACO_PARAM_RA |
130  NACO_PARAM_DEC |
131  NACO_PARAM_PIXSCALE|
132  NACO_PARAM_MAGNITD |
133  NACO_PARAM_SX |
134  NACO_PARAM_SY |
135  NACO_PARAM_CHK_IMG,
136  "Zero point computation recipe",
137  RECIPE_STRING " -- Zero point recipe\n"
138  "The files listed in the Set Of Frames (sof-file) "
139  "must be tagged:\n"
140  "NACO-raw-file.fits " NACO_IMG_ZPOINT_CHOP " or\n"
141  "NACO-raw-file.fits " NACO_IMG_ZPOINT_JITTER ".\n"
142  "NACO-Imaging-Standard-Star-Catalog.fits "
143  NACO_IMG_STD_CAT "\n"
144  "Optionally, a flat field frame may be inluded:\n"
145  "NACO-flat-file.fits " NACO_CALIB_FLAT "\n");
146 
147 /*-----------------------------------------------------------------------------
148  Static variables
149  -----------------------------------------------------------------------------*/
150 
151 static struct {
152  /* Inputs */
153  double ra;
154  double dec;
155  double magnitude;
156  int sx;
157  int sy;
158  double phot_star_radius;
159  double phot_bg_r1;
160  double phot_bg_r2;
161  double pscale;
162  int check_im;
163 
164 } naco_img_zpoint_config;
165 
166 
167 /*----------------------------------------------------------------------------*/
171 /*----------------------------------------------------------------------------*/
172 
173 /*-----------------------------------------------------------------------------
174  Functions code
175  -----------------------------------------------------------------------------*/
176 
177 /*----------------------------------------------------------------------------*/
185 /*----------------------------------------------------------------------------*/
186 static int naco_img_zpoint(cpl_frameset * framelist,
187  const cpl_parameterlist * parlist)
188 {
189  irplib_framelist* allframes = NULL;
190  irplib_framelist* rawframes = NULL;
191  cpl_propertylist* qclist = cpl_propertylist_new();
192  cpl_propertylist* paflist = cpl_propertylist_new();
193  const char * flat;
194  const char * star_cat;
195  cpl_table * tab = NULL;
196  cpl_image * check_im = NULL;
197 
198  /* Retrieve input parameters */
199  /* --ra */
200  naco_img_zpoint_config.ra
201  = naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_RA);
202  /* --dec */
203  naco_img_zpoint_config.dec
204  = naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_DEC);
205  /* --pscale */
206  naco_img_zpoint_config.pscale
207  = naco_parameterlist_get_double(parlist,RECIPE_STRING,NACO_PARAM_PIXSCALE);
208  /* --mag */
209  naco_img_zpoint_config.magnitude
210  = naco_parameterlist_get_double(parlist, RECIPE_STRING,NACO_PARAM_MAGNITD);
211  /* --sx */
212  naco_img_zpoint_config.sx
213  = naco_parameterlist_get_int(parlist, RECIPE_STRING, NACO_PARAM_SX);
214 
215  skip_if_lt(naco_img_zpoint_config.sx, 1,
216  "for the size of the search window in X-direction [pixel]");
217 
218  /* --sy */
219  naco_img_zpoint_config.sy
220  = naco_parameterlist_get_int(parlist, RECIPE_STRING, NACO_PARAM_SY);
221 
222  skip_if_lt(naco_img_zpoint_config.sy, 1,
223  "for the size of the search window in Y-direction [pixel]");
224 
225  /* --star_r */
226  naco_img_zpoint_config.phot_star_radius
227  = naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_STAR_R);
228 
229  /* --bg_r1 */
230  naco_img_zpoint_config.phot_bg_r1
231  = naco_parameterlist_get_double(parlist, RECIPE_STRING,NACO_PARAM_BG_RINT);
232 
233  /* --bg_r2 */
234  naco_img_zpoint_config.phot_bg_r2
235  = naco_parameterlist_get_double(parlist, RECIPE_STRING,NACO_PARAM_BG_REXT);
236 
237  /* --check_im */
238  naco_img_zpoint_config.check_im
239  = naco_parameterlist_get_bool(parlist, RECIPE_STRING, NACO_PARAM_CHK_IMG);
240 
241  /* Identify the RAW and CALIB frames in the input frameset */
242  skip_if (naco_dfs_set_groups(framelist));
243 
244  allframes = irplib_framelist_cast(framelist);
245  skip_if(allframes == NULL);
246 
247  rawframes = irplib_framelist_extract_regexp(allframes,
248  "^(" NACO_IMG_ZPOINT_JITTER
249  "|" NACO_IMG_ZPOINT_CHOP ")$",
250  CPL_FALSE);
251  skip_if(rawframes == NULL);
252  irplib_framelist_empty(allframes);
253 
254  /* Standard star catalog */
255  star_cat = irplib_frameset_find_file(framelist, NACO_IMG_STD_CAT);
256  error_if (star_cat == NULL, CPL_ERROR_DATA_NOT_FOUND, "The input "
257  "file(s) have no star catalog tagged %s", NACO_IMG_STD_CAT);
258 
259  /* Retrieve calibration data */
260  flat = irplib_frameset_find_file(framelist, NACO_CALIB_FLAT);
261  bug_if(0);
262 
263  skip_if( irplib_framelist_load_propertylist(rawframes, 0, 0, "^("
264  IRPLIB_PFITS_REGEXP_RECAL "|"
265  NACO_PFITS_REGEXP_ZPOINT "|"
266  NACO_PFITS_REGEXP_ZPOINT_PAF "|"
267  NACO_PFITS_REGEXP_ZPOINT_REF
268  ")$", CPL_FALSE));
269 
270  skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
271  NACO_PFITS_REGEXP_ZPOINT
272  ")$", CPL_FALSE));
273 
274  /* Compute the strehl/zpoint values */
275  tab = naco_img_zpoint_reduce(qclist, rawframes, parlist, star_cat, flat,
276  &check_im);
277  skip_if (tab == NULL);
278 
279  skip_if(naco_img_zpoint_qc(qclist, paflist, rawframes));
280 
281  /* Reduce maximum number of pointers used */
282  irplib_framelist_empty(rawframes);
283 
284  /* PRO.CATG */
285  bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
286  NACO_IMG_ZPOINT_RES));
287 
288  /* Save the products */
289  skip_if (naco_img_zpoint_save(framelist, parlist, qclist, paflist, tab,
290  check_im));
291 
292  end_skip;
293 
294  cpl_table_delete(tab);
295  cpl_image_delete(check_im);
296  irplib_framelist_delete(allframes);
297  irplib_framelist_delete(rawframes);
298  cpl_propertylist_delete(qclist);
299  cpl_propertylist_delete(paflist);
300 
301  return cpl_error_get_code();
302 }
303 
304 /*----------------------------------------------------------------------------*/
316 /*----------------------------------------------------------------------------*/
317 static cpl_table * naco_img_zpoint_reduce(cpl_propertylist * qclist,
318  const irplib_framelist * rawframes,
319  const cpl_parameterlist * parlist,
320  const char * star_cat,
321  const char * flat,
322  cpl_image ** pcheck_im)
323 {
324  const cpl_propertylist * plist
326  cpl_image * flat_im = NULL;
327  cpl_image * rawimage = NULL;
328  cpl_image * imdiff = NULL;
329  const char * filter;
330  const int ndiff = 2 * irplib_framelist_get_size(rawframes)-2;
331  double pos_x_cen = DBL_MAX; /* Avoid uninit warning */
332  double pos_y_cen = DBL_MAX; /* Avoid uninit warning */
333  cpl_apertures * aperts = NULL;
334  double psigmas[] = {20.0, 10.0, 8.0, 6.0, 5.0};
335  const int nsigmas = (int)(sizeof(psigmas)/sizeof(double));
336  cpl_size isigma;
337  cpl_vector * sigmas = NULL;
338  cpl_table * out_tab = NULL;
339  cpl_vector * zpoints = NULL;
340  double dit;
341  double off_x0 = DBL_MAX; /* Avoid uninit warning */
342  double off_y0 = DBL_MAX; /* Avoid uninit warning */
343  double off_x = DBL_MAX; /* Avoid uninit warning */
344  double off_y = DBL_MAX; /* Avoid uninit warning */
345  double str_sum = 0.0;
346  double str_err_sum = 0.0;
347  double pe_sum = 0.0;
348  double fl_sum = 0.0;
349  double bg_no_sum = 0.0;
350  double sqsum = 0.0;
351  double avg_zp = 0.0;
352  int nok_zp = 0;
353  int nok_strehl = 0;
354  int i;
355 
356  /* QC Parameters */
357  double zpointrms;
358  double strehl;
359  double strehl_err;
360  double strehl_rms;
361  double star_peak;
362  double star_flux;
363 
364 
365 
366  bug_if (0);
367  bug_if (pcheck_im == NULL);
368  bug_if (*pcheck_im != NULL);
369 
370  skip_if_lt(ndiff/2+1, 3, "raw frames");
371 
372  /* Get the filter name, DIT, RA, DEC and pixelscale */
373 
374  dit = naco_pfits_get_dit(plist);
375  skip_if (dit <= 0.0);
376  if (naco_img_zpoint_config.ra > 998.0)
377  naco_img_zpoint_config.ra = naco_pfits_get_ra(plist);
378  if (naco_img_zpoint_config.dec > 998.0)
379  naco_img_zpoint_config.dec = naco_pfits_get_dec(plist);
380  skip_if(0);
381 
382  if (naco_img_zpoint_config.pscale <= 0.0) {
383  naco_img_zpoint_config.pscale = naco_pfits_get_pixscale(plist);
384  skip_if(naco_img_zpoint_config.pscale <= 0.0);
385  }
386 
387  cpl_msg_info(cpl_func, "Using star at position: RA = %g ; DEC = %g",
388  naco_img_zpoint_config.ra, naco_img_zpoint_config.dec);
389 
390  filter = naco_pfits_get_filter(plist);
391  skip_if(0);
392 
393  /* Get the standard star information from database */
394  if (naco_img_zpoint_config.magnitude > IRPLIB_STDSTAR_LIMIT) {
395  naco_img_zpoint_config.magnitude
396  = naco_img_zpoint_find_mag(qclist,
397  naco_img_zpoint_config.ra,
398  naco_img_zpoint_config.dec,
399  star_cat, filter);
400  if (cpl_error_get_code()) {
401  const cpl_propertylist * reflist
403  const char * starname = naco_pfits_get_object(reflist);
404 
405  if (starname != NULL) {
406  error_if (0, cpl_error_get_code(), "Star '%s' has "
407  "no magnitude for filter '%s' in catalogue '%s'",
408  starname, filter, star_cat);
409  }
410  }
411  }
412  cpl_msg_info(cpl_func, "Star magnitude with filter %s : %g", filter,
413  naco_img_zpoint_config.magnitude);
414  bug_if(cpl_propertylist_append_double(qclist, "ESO QC STARMAG",
415  naco_img_zpoint_config.magnitude));
416 
417  zpoints = cpl_vector_new(ndiff);
418  out_tab = cpl_table_new(ndiff);
419  cpl_table_new_column(out_tab, "POSX", CPL_TYPE_DOUBLE);
420  cpl_table_new_column(out_tab, "POSY", CPL_TYPE_DOUBLE);
421  cpl_table_new_column(out_tab, "ZPOINT", CPL_TYPE_DOUBLE);
422  cpl_table_new_column(out_tab, "PEAK", CPL_TYPE_DOUBLE);
423  cpl_table_new_column(out_tab, "FLUX", CPL_TYPE_DOUBLE);
424  cpl_table_new_column(out_tab, "BGD_NOISE", CPL_TYPE_DOUBLE);
425  cpl_table_new_column(out_tab, "STREHL", CPL_TYPE_DOUBLE);
426  cpl_table_new_column(out_tab, "STREHL_ERR", CPL_TYPE_DOUBLE);
427  cpl_table_new_column(out_tab, "BGD", CPL_TYPE_DOUBLE);
428  cpl_table_new_column(out_tab, "FWHMX", CPL_TYPE_DOUBLE);
429  cpl_table_new_column(out_tab, "FWHMY", CPL_TYPE_DOUBLE);
430  bug_if(0);
431 
432  /* Load the the flatfield if one is provided */
433  if (flat != NULL) {
434  cpl_msg_info(cpl_func, "Loading the flat field from %s", flat);
435  flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, 0);
436 
437  if (flat_im == NULL) {
438  cpl_msg_error(cpl_func, "Could not load flat field");
439  skip_if(1);
440  }
441  }
442 
443  cpl_msg_info(cpl_func, "Loading the image from raw frame %d",1);
444  rawimage = cpl_image_load(cpl_frame_get_filename(
445  irplib_framelist_get_const(rawframes, 0)),
446  CPL_TYPE_FLOAT, 0, 0);
447  if (rawimage == NULL) {
448  cpl_msg_error(cpl_func, "Could not load image from first raw frame");
449  skip_if(1);
450  }
451 
452  for (i=0; i < ndiff; i++) {
453 
454  double pos_x, pos_y;
455  double str, str_err, pe, fl, bg_no;
456 
457 
458  bug_if(0);
459 
460  if (i == 0 || i % 2 == 1) {
461  /* Get the offsets */
462 
463  off_x = naco_pfits_get_cumoffsetx(plist);
464  off_y = naco_pfits_get_cumoffsety(plist);
465 
466  skip_if(0);
467 
468  if (i == 0) {
469  off_x0 = off_x;
470  off_y0 = off_y;
471  }
472 
473  off_x -= off_x0;
474  off_y -= off_y0;
475 
476  cpl_msg_info(cpl_func, "Offsets for difference image %d: (%g,%g)",
477  i, off_x, off_y);
478 
479  /* Update the plist to that of the next frame */
480  if (i + 1 < ndiff)
481  plist = irplib_framelist_get_propertylist_const(rawframes,
482  1 + (i+1)/2);
483  }
484 
485  if (i % 2 == 0) {
486  const char * filename = cpl_frame_get_filename(
487  irplib_framelist_get_const(rawframes, 1+i/2));
488 
489  bug_if(0);
490 
491  cpl_image_delete(imdiff);
492  imdiff = rawimage;
493 
494  cpl_msg_info(cpl_func, "Loading the image from raw frame %d",2+i/2);
495  rawimage = cpl_image_load(filename, CPL_TYPE_FLOAT, 0, 0);
496  if (rawimage == NULL) {
497  cpl_msg_error(cpl_func, "Could not load image %d", 2+i/2);
498  skip_if(1);
499  }
500  bug_if(cpl_image_subtract(imdiff, rawimage));
501  if (flat_im != NULL) skip_if(cpl_image_divide(imdiff, flat_im));
502  } else {
503  bug_if(cpl_image_multiply_scalar(imdiff, -1.0));
504  }
505 
506  if (i == 0) {
507  double min_dist = DBL_MAX; /* Avoid (false) uninit warning */
508  const int size_x = cpl_image_get_size_x(imdiff);
509  const int size_y = cpl_image_get_size_y(imdiff);
510  int iap;
511 
512  /* Detect the central object in the first frame */
513  cpl_msg_info(cpl_func, "Detecting a bright object in the first "
514  "difference image using %d sigma-levels ranging from "
515  "%g down to %g", nsigmas, psigmas[0],
516  psigmas[nsigmas-1]);
517  sigmas = cpl_vector_wrap(nsigmas, psigmas);
518 
519  aperts = cpl_apertures_extract(imdiff, sigmas, &isigma);
520  if (aperts == NULL || cpl_apertures_get_size(aperts) < 1) {
521  cpl_msg_error(cpl_func, "Could not detect any bright object");
522  skip_if(1);
523  }
524  for (iap=0; iap < cpl_apertures_get_size(aperts); iap++) {
525  const double d_x
526  = cpl_apertures_get_centroid_x(aperts, iap+1) - size_x/2.0;
527  const double d_y
528  = cpl_apertures_get_centroid_y(aperts, iap+1) - size_y/2.0;
529  const double dist = d_x * d_x + d_y * d_y;
530  if (iap == 0 || dist < min_dist) {
531  min_dist = dist;
532  pos_x_cen = d_x;
533  pos_y_cen = d_y;
534  }
535  }
536  cpl_apertures_delete(aperts);
537  aperts = NULL;
538  bug_if(0);
539  pos_x_cen += size_x/2.0;
540  pos_y_cen += size_y/2.0;
541  cpl_msg_info(cpl_func, "Detected a bright object at sigma=%g, at "
542  "position: %g %g", psigmas[isigma], pos_x_cen,
543  pos_y_cen);
544 
545  }
546 
547  pos_x = pos_x_cen + off_x;
548  pos_y = pos_y_cen + off_y;
549 
550  if (i > 0) {
551  /* Refine the positions */
552  const int llx = (int)pos_x - naco_img_zpoint_config.sx;
553  const int urx = (int)pos_x + naco_img_zpoint_config.sx;
554  const int lly = (int)pos_y - naco_img_zpoint_config.sy;
555  const int ury = (int)pos_y + naco_img_zpoint_config.sy;
556  pos_x = cpl_image_get_centroid_x_window(imdiff, llx, lly, urx, ury);
557  pos_y = cpl_image_get_centroid_y_window(imdiff, llx, lly, urx, ury);
558  error_if (0, cpl_error_get_code(), "Could not refine the positions "
559  "of difference image %d", i+1);
560  }
561 
562  bug_if(0);
563 
564  /* Create the check image if requested */
565  if (naco_img_zpoint_config.check_im) {
566  const double r1 = naco_img_zpoint_config.phot_star_radius
567  /naco_img_zpoint_config.pscale;
568  const double r2 = naco_img_zpoint_config.phot_bg_r1
569  /naco_img_zpoint_config.pscale;
570  const double r3 = naco_img_zpoint_config.phot_bg_r2
571  /naco_img_zpoint_config.pscale;
572 
573  skip_if(naco_img_zpoint_check_im(pcheck_im, imdiff, i, ndiff,
574  pos_x, pos_y, r1, r2, r3));
575  }
576 
577  /* Compute the flux/strehl in the current image */
578  skip_if (naco_img_zpoint_reduce_one(out_tab, zpoints, i, imdiff,
579  parlist, filter,
580  pos_x, pos_y, dit,
581  &str, &str_err, &pe, &fl, &bg_no));
582 
583  /* Compute the averages of the results for the strehl */
584  if (str <= 0.0 || str >= 1.0) continue;
585  nok_strehl++;
586 
587  str_sum += str;
588  str_err_sum += str_err;
589  bg_no_sum += bg_no;
590  pe_sum += pe;
591  fl_sum += fl;
592  }
593 
594  skip_if_lt(nok_strehl, 1, "valid strehl measurements");
595 
596  strehl = str_sum / (double)nok_strehl;
597  strehl_err = str_err_sum / (double)nok_strehl;
598  strehl_rms = bg_no_sum / (double)nok_strehl;
599  star_peak = pe_sum / (double)nok_strehl;
600  star_flux = fl_sum / (double)nok_strehl;
601 
602  /* Compute the averages of the results for the zero point */
603  cpl_vector_sort(zpoints, 1);
604  /* Reject highest and lowest value */
605  for (i=1; i < ndiff-1; i++) {
606  const double zp = cpl_vector_get(zpoints, i);
607  if (zp > 0.0) {
608  avg_zp += zp;
609  sqsum += zp * zp;
610  nok_zp ++;
611  }
612  }
613  cpl_vector_delete(zpoints);
614  zpoints = NULL;
615 
616  skip_if_lt(nok_zp, 1, "valid zpoint measurements");
617 
618  avg_zp /= (double)nok_zp;
619  sqsum /= (double)nok_zp;
620  zpointrms = sqsum - avg_zp * avg_zp;
621  zpointrms = zpointrms > 0.0 ? sqrt(zpointrms) : 0.0;
622 
623  /* Print final results */
624  cpl_msg_info(cpl_func, "***** FINAL RESULTS *****");
625  cpl_msg_info(cpl_func, "Strehl : %g", strehl);
626  cpl_msg_info(cpl_func, "Strehl error : %g", strehl_err);
627  cpl_msg_info(cpl_func, "Peak : %g", star_peak);
628  cpl_msg_info(cpl_func, "Flux : %g", star_flux);
629  cpl_msg_info(cpl_func, "Strehl RMS %g", strehl_rms);
630  cpl_msg_info(cpl_func, "Zero point : %g", avg_zp);
631  cpl_msg_info(cpl_func, "Zero p. RMS: %g", zpointrms);
632 
633  cpl_propertylist_append_double(qclist, "ESO QC ZPOINT", avg_zp);
634  cpl_propertylist_append_double(qclist, "ESO QC ZPOINTRMS", zpointrms);
635  cpl_propertylist_append_double(qclist, "ESO QC STREHL", strehl);
636  cpl_propertylist_append_double(qclist, "ESO QC STREHL ERROR", strehl_err);
637  cpl_propertylist_append_double(qclist, "ESO QC STREHL RMS", strehl_rms);
638  cpl_propertylist_append_double(qclist, "ESO QC STREHL PEAK", star_peak);
639  cpl_propertylist_append_double(qclist, "ESO QC STREHL FLUX", star_flux);
640  cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", filter);
641 
642  bug_if(0);
643 
644  end_skip;
645 
646  cpl_image_delete(rawimage);
647  cpl_image_delete(imdiff);
648  cpl_image_delete(flat_im);
649  cpl_apertures_delete(aperts);
650  cpl_vector_unwrap(sigmas);
651  cpl_vector_delete(zpoints);
652 
653  if (cpl_error_get_code()) {
654  cpl_image_delete(*pcheck_im);
655  *pcheck_im = NULL;
656  cpl_table_delete(out_tab);
657  out_tab = NULL;
658  }
659 
660  return out_tab;
661 }
662 
663 /*----------------------------------------------------------------------------*/
683 /*----------------------------------------------------------------------------*/
684 static cpl_error_code naco_img_zpoint_check_im(cpl_image ** self,
685  const cpl_image * imdiff,
686  int idiff,
687  int nima,
688  double pos_x,
689  double pos_y,
690  double r1,
691  double r2,
692  double r3)
693 {
694  const float * pin_ima = cpl_image_get_data_float_const(imdiff);
695  float * pout_ima;
696  const int in_nx = cpl_image_get_size_x(imdiff);
697  const int in_ny = cpl_image_get_size_y(imdiff);
698  const int box_sz = 2 * (int)r3 + 1;
699  const int nx = box_sz * nima;
700  const int ny = box_sz;
701  /* Get the sub image position */
702  const int llx = (int)(pos_x - r3);
703  const int lly = (int)(pos_y - r3);
704  int i, j;
705 
706 
707  bug_if(0);
708 
709  bug_if(self == NULL);
710 
711  skip_if (r1 > r2);
712  skip_if (r2 >= r3);
713 
714  if (idiff == 0) {
715  bug_if(*self != NULL);
716 
717  cpl_msg_info(cpl_func, "Creating %d X %d X %d check-image",
718  nima, box_sz, box_sz);
719 
720  /* Create the output image */
721  *self = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
722  }
723 
724  pout_ima = cpl_image_get_data_float(*self);
725 
726  bug_if(0);
727 
728  for (j=0; j < box_sz; j++) {
729  if (j + lly < 0 || j + lly >= in_ny) continue;
730  for (i=0; i < box_sz; i++) {
731  if (i + llx >= 0 && i + llx < in_nx) {
732  const int in_pos = llx + i + (lly+j) * in_nx;
733  const int out_pos = (box_sz * idiff) + i + j * nx;
734 
735  bug_if ( in_pos < 0 || in_pos >= in_nx*in_ny);
736  bug_if (out_pos < 0 || out_pos >= nx* ny);
737 
738  pout_ima[out_pos] = pin_ima[in_pos];
739  }
740  }
741  }
742 
743  if (idiff == 0) {
744  /* Draw the two circles */
745  const double sqr1 = sqrt(r1*r1);
746  const double sqr2 = sqrt(r2*r2);
747  const double sqr3 = sqrt(r3*r3);
748  for (j=0; j < box_sz; j++) {
749  const int jdist = j - box_sz/2;
750  for (i=0; i < box_sz; i++) {
751  const int idist = i - box_sz/2;
752  const double dist
753  = sqrt((double)(idist * idist + jdist * jdist));
754  if (fabs(dist-sqr1) < 0.5 ||
755  fabs(dist-sqr2) < 0.5 ||
756  fabs(dist-sqr3) < 0.5) pout_ima[i + j * nx] = 10000;
757  }
758  }
759  }
760 
761  end_skip;
762 
763  return cpl_error_get_code();
764 }
765 
766 /*----------------------------------------------------------------------------*/
787 /*----------------------------------------------------------------------------*/
788 static
789 cpl_error_code naco_img_zpoint_reduce_one(cpl_table * zptab,
790  cpl_vector * zpoints,
791  int idiff,
792  const cpl_image * ima,
793  const cpl_parameterlist * parlist,
794  const char * filter,
795  double pos_x,
796  double pos_y,
797  double dit,
798  double * pstrehl,
799  double * pstrehl_err,
800  double * pstar_peak,
801  double * pstar_flux,
802  double * pbg_noise)
803 {
804  cpl_errorstate cleanstate = cpl_errorstate_get();
805  double zeropoint, background;
806  double r, r1, r2;
807  double lam, dlam;
808  double star_bg, psf_peak, psf_flux;
809  double fwhm_x, fwhm_y;
810 
811 
812  bug_if(pstar_flux == NULL);
813  bug_if(filter == NULL);
814 
815  /* Compute the photometry */
816  r = naco_img_zpoint_config.phot_star_radius/naco_img_zpoint_config.pscale;
817  r1 = naco_img_zpoint_config.phot_bg_r1/naco_img_zpoint_config.pscale;
818  r2 = naco_img_zpoint_config.phot_bg_r2/naco_img_zpoint_config.pscale;
819  background = irplib_strehl_ring_background(ima, pos_x, pos_y, r1, r2,
820  IRPLIB_BG_METHOD_AVER_REJ);
821  skip_if(0);
822  *pstar_flux = irplib_strehl_disk_flux(ima, pos_x, pos_y, r, background);
823  skip_if(0);
824 
825  /* Get lam and dlam from the filter name for the Strehl computation */
826  irplib_check(naco_get_filter_infos(filter, &lam, &dlam),
827  "Cannot get filter infos [%s]", filter);
828 
829  /* Compute the strehl */
830  irplib_check(naco_strehl_compute(ima, parlist, RECIPE_STRING, lam, dlam,
831  pos_x, pos_y,
832  naco_img_zpoint_config.pscale,
833  pstrehl, pstrehl_err, &star_bg,
834  pstar_peak, pstar_flux,
835  &psf_peak, &psf_flux, pbg_noise),
836  "Could not compute the Strehl ratio");
837 
838  /* FWHM_X / FWHM_Y */
839  if (cpl_image_get_fwhm(ima, (int)pos_x, (int)pos_y, &fwhm_x, &fwhm_y)) {
840  irplib_error_recover(cleanstate, "Could not compute FWHM for image %d:",
841  idiff+1);
842  fwhm_x = fwhm_y = 0.0;
843  } else {
844  if (fwhm_x <= 0.0) {
845  cpl_msg_warning(cpl_func, "Could not compute FWHM in x for image "
846  "%d:", idiff+1);
847  fwhm_x = 0.0;
848  }
849  if (fwhm_y <= 0.0) {
850  cpl_msg_warning(cpl_func, "Could not compute FWHM in y for image "
851  "%d:", idiff+1);
852  fwhm_y = 0.0;
853  }
854  }
855 
856  zeropoint = naco_img_zpoint_config.magnitude + 2.5 * log10(*pstar_flux/dit);
857 
858  cpl_msg_info(cpl_func, "Strehl : %g", *pstrehl);
859  cpl_msg_info(cpl_func, "Strehl error : %g", *pstrehl_err);
860  cpl_msg_info(cpl_func, "Peak : %g", *pstar_peak);
861  cpl_msg_info(cpl_func, "Flux : %g", *pstar_flux);
862  cpl_msg_info(cpl_func, "Background : %g", background);
863  cpl_msg_info(cpl_func, "Bg noise : %g", *pbg_noise);
864  cpl_msg_info(cpl_func, "Zero point : %g", zeropoint);
865  cpl_msg_info(cpl_func, "FWHM in x : %g", fwhm_x);
866  cpl_msg_info(cpl_func, "FWHM in y : %g", fwhm_y);
867 
868  /* Save the results in the vectors */
869  cpl_vector_set(zpoints, idiff, zeropoint);
870  cpl_table_set_double(zptab, "POSX", idiff, pos_x);
871  cpl_table_set_double(zptab, "POSY", idiff, pos_y);
872  cpl_table_set_double(zptab, "ZPOINT", idiff, zeropoint);
873  cpl_table_set_double(zptab, "PEAK", idiff, *pstar_peak);
874  cpl_table_set_double(zptab, "FLUX", idiff, *pstar_flux);
875  cpl_table_set_double(zptab, "BGD_NOISE", idiff, *pbg_noise);
876  cpl_table_set_double(zptab, "STREHL", idiff, *pstrehl);
877  cpl_table_set_double(zptab, "STREHL_ERR", idiff, *pstrehl_err);
878  cpl_table_set_double(zptab, "BGD", idiff, background);
879  cpl_table_set_double(zptab, "FWHMX", idiff, fwhm_x);
880  cpl_table_set_double(zptab, "FWHMY", idiff, fwhm_y);
881 
882  bug_if(0);
883 
884  end_skip;
885 
886  if (cpl_error_get_code())
887  cpl_msg_error(cpl_func, "Cannot reduce the image %d", idiff+1);
888 
889  return cpl_error_get_code();
890 }
891 
892 /*----------------------------------------------------------------------------*/
903 /*----------------------------------------------------------------------------*/
904 static double naco_img_zpoint_find_mag(cpl_propertylist * qclist,
905  double ra,
906  double dec,
907  const char * star_cat,
908  const char * filter)
909 {
910  cpl_error_code error;
911  const char * star_name;
912  const char * star_type;
913  const char * cat_name;
914  int istar;
915  double star_mag = 0.0;
916  naco_band band;
917  const char * bandname = NULL;
918  cpl_table * stdstars = NULL;
919  const char * sw_cat[] = {"LCO-Palomar", "LCO-Palomar-NICMOS-Red-Stars",
920  "ESO-VanDerBliek", "UKIRT-Extended",
921  "UKIRT-Fundamental", "SAAO-Carter", NULL};
922  const char * lw_cat[] = {"ESO-VanDerBliek", "UKIRT-Standards",
923  "UKIRT-LM", NULL};
924 
925  bug_if (filter == NULL);
926 
927  cpl_msg_info(cpl_func, "Get the star magnitude with filter: %s", filter);
928 
929  band = naco_get_bbfilter(filter); /* Get the band */
930 
931  skip_if(0);
932 
933  /* Get the nearby standard stars */
934  stdstars = naco_img_zpoint_load_std_star(star_cat, ra, dec, /* degrees */
935  (IRPLIB_STDSTAR_MAXDIST)/60.0);
936 
937  skip_if(stdstars == NULL);
938  skip_if(cpl_table_get_nrow(stdstars) == 0);
939 
940  istar = -1;
941 
942  switch (band) {
943  /* SW mode */
944  case BAND_J:
945  case BAND_H:
946  case BAND_K:
947  case BAND_KS:
948 
949  bandname = naco_std_band_name(band);
950 
951  error = naco_img_zpoint_find_std_star(stdstars, sw_cat, ra, dec,
952  bandname, CPL_FALSE, &istar);
953 
954  if (error) break;
955  if (istar >= 0) break;
956 
957 
958  /* Special case: swap K and Ks if needed */
959  if (band == BAND_K) {
960  bandname = naco_std_band_name(BAND_KS);
961 
962  cpl_msg_info(cpl_func, "Retrying with alternative band: %s",
963  bandname);
964 
965  error = naco_img_zpoint_find_std_star(stdstars, sw_cat, ra, dec,
966  bandname, CPL_TRUE, &istar);
967 
968  } else if (band == BAND_KS) {
969  bandname = naco_std_band_name(BAND_K);
970 
971  cpl_msg_info(cpl_func, "Retrying with alternative band: %s",
972  bandname);
973 
974  error = naco_img_zpoint_find_std_star(stdstars, sw_cat, ra, dec,
975  bandname, CPL_TRUE, &istar);
976  }
977  break;
978 
979  /* LW mode */
980  case BAND_L:
981  case BAND_M:
982  case BAND_LP:
983  case BAND_MP:
984 
985  bandname = naco_std_band_name(band);
986 
987  error = naco_img_zpoint_find_std_star(stdstars, lw_cat, ra, dec,
988  bandname, CPL_FALSE, &istar);
989 
990  if (error) break;
991  if (istar < 0) {
992  naco_band other = BAND_UNKNOWN;
993 
994  switch(band) {
995  case BAND_L:
996  other = BAND_LP;
997  break;
998  case BAND_M:
999  other = BAND_MP;
1000  break;
1001  case BAND_LP:
1002  other = BAND_L;
1003  break;
1004  case BAND_MP:
1005  other = BAND_M;
1006  break;
1007  default:
1008  bug_if (1);
1009  }
1010 
1011  bandname = naco_std_band_name(other);
1012 
1013  cpl_msg_info(cpl_func, "Retrying with alternative band: %s",
1014  bandname);
1015 
1016  error = naco_img_zpoint_find_std_star(stdstars, lw_cat, ra, dec,
1017  bandname, CPL_TRUE, &istar);
1018  }
1019  break;
1020  default:
1021  skip_if(1);
1022  }
1023 
1024  error_if (error || istar < 0, CPL_ERROR_DATA_NOT_FOUND, "Star magnitude "
1025  "for filter '%s' not found in catalog(s) at (RA,DEC)=(%g,%g)"
1026  " with tolerance=%g (degrees)", filter, ra, dec,
1027  IRPLIB_STDSTAR_MAXDIST/60.0);
1028 
1029  star_mag = cpl_table_get_double(stdstars, bandname, istar, NULL);
1030  star_name = cpl_table_get_string(stdstars, IRPLIB_STDSTAR_STAR_COL, istar);
1031  star_type = cpl_table_get_string(stdstars, IRPLIB_STDSTAR_TYPE_COL, istar);
1032  cat_name = cpl_table_get_string(stdstars, "CAT_NAME", istar);
1033 
1034 
1035  /* Store results */
1036  bug_if(cpl_propertylist_append_string(qclist, "ESO QC STDNAME",
1037  star_name));
1038  bug_if(cpl_propertylist_append_string(qclist, "ESO QC CATNAME",
1039  cat_name));
1040  bug_if(cpl_propertylist_append_string(qclist, "ESO QC SPECTYPE",
1041  star_type));
1042 
1043  cpl_msg_info(cpl_func, "Found standard star '%s' of type '%s' with "
1044  "magnitude %g for filter %s from catalog '%s'",
1045  star_name, star_type, star_mag, bandname, cat_name);
1046 
1047  end_skip;
1048 
1049  cpl_table_delete(stdstars);
1050 
1051  return star_mag;
1052 }
1053 
1054 
1055 /*----------------------------------------------------------------------------*/
1077 /*----------------------------------------------------------------------------*/
1078 static cpl_error_code naco_img_zpoint_find_std_star(cpl_table * self,
1079  const char * cat[],
1080  double ra, double dec,
1081  const char * bandname,
1082  cpl_boolean retry,
1083  int * pistar)
1084 {
1085 
1086  double min_dist = IRPLIB_STDSTAR_MAXDIST / 60.0; /* Maximum */
1087  int nsel;
1088 
1089  bug_if (pistar == NULL);
1090  *pistar = -1;
1091 
1092  bug_if (self == NULL);
1093  bug_if (bandname == NULL);
1094 
1095  nsel = (retry
1096  ? cpl_table_or_selected_double
1097  : cpl_table_and_selected_double)(self, bandname,
1098  CPL_LESS_THAN,
1099  IRPLIB_STDSTAR_LIMIT);
1100 
1101  if (nsel == 0) {
1102  cpl_msg_info(cpl_func, "None of the standard star catalog(s) have "
1103  "a star with a known magnitude for band %s within a "
1104  "distance of tol=%g [degrees] from (RA,DEC)=(%g,%g)",
1105  bandname, min_dist, ra, dec);
1106  } else {
1107 
1108  /* At this point we are sure to find a star, if not in the specified
1109  list of catalogs, then when searching them all */
1110 
1111  const int nrows = cpl_table_get_nrow(self);
1112  int ibest = -1;
1113  int i;
1114 
1115  int icat;
1116 
1117  bug_if (cat == NULL);
1118 
1119  for (icat = 0; cat[icat] != NULL; icat++) {
1120 
1121  char * regexp = cpl_sprintf("^%s$", cat[icat]);
1122 
1123  cpl_msg_info(cpl_func, "Trying %s", cat[icat]);
1124 
1125  nsel = cpl_table_and_selected_string(self, "CAT_NAME",
1126  CPL_EQUAL_TO, regexp);
1127  cpl_free(regexp);
1128 
1129  if (nsel > 0) break;
1130 
1131  /* No stars found in this catalog, reset to try next */
1132  nsel = cpl_table_or_selected_double(self, bandname,
1133  CPL_LESS_THAN,
1134  IRPLIB_STDSTAR_LIMIT);
1135  bug_if(nsel == 0);
1136  }
1137 
1138  bug_if (nsel == 0);
1139 
1140  /* Compute distances to the selected rows */
1141 
1142  for (i=0; i < nrows; i++) {
1143  if (cpl_table_is_selected(self, i)) {
1144  /* The row is selected - compute the distance */
1145  const double distance
1146  = irplib_wcs_great_circle_dist(ra, dec,
1147  cpl_table_get_double(self, IRPLIB_STDSTAR_RA_COL, i, NULL),
1148  cpl_table_get_double(self, IRPLIB_STDSTAR_DEC_COL, i,NULL));
1149 
1150  if (distance < min_dist) {
1151  min_dist = distance;
1152  ibest = i;
1153  }
1154  }
1155  }
1156 
1157  bug_if (ibest < 0);
1158 
1159  cpl_msg_info(cpl_func, "Found %d star(s) with known magnitude in band "
1160  "%s - selected one with distance %g", nsel, bandname,
1161  min_dist);
1162 
1163  *pistar = ibest;
1164  }
1165 
1166  end_skip;
1167 
1168  return cpl_error_get_code();
1169 }
1170 
1171 
1172 /*----------------------------------------------------------------------------*/
1181 /*----------------------------------------------------------------------------*/
1182 static cpl_error_code naco_img_zpoint_qc(cpl_propertylist * qclist,
1183  cpl_propertylist * paflist,
1184  const irplib_framelist * rawframes)
1185 {
1186 
1187  cpl_errorstate cleanstate = cpl_errorstate_get();
1188  const cpl_propertylist * reflist
1190 
1191  const char * sval;
1192 
1193 
1194  bug_if(0);
1195 
1196  /* Copy the keywords for the paf file */
1197  bug_if(cpl_propertylist_copy_property_regexp(paflist, reflist, "^("
1198  NACO_PFITS_REGEXP_ZPOINT_PAF
1199  ")$", 0));
1200 
1201  /* Add QC parameters */
1202 
1203  sval = naco_pfits_get_opti3_name(reflist);
1204  if (sval == NULL)
1205  naco_error_reset("Could not get FITS key:");
1206  else
1207  bug_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER NDENS",
1208  sval));
1209  sval = naco_pfits_get_opti4_name(reflist);
1210  if (sval == NULL)
1211  naco_error_reset("Could not get FITS key:");
1212  else
1213  bug_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER POL",
1214  sval));
1215 
1216  skip_if (naco_img_zpoint_qc_all(qclist, paflist, rawframes));
1217 
1218  bug_if(cpl_propertylist_append(paflist, qclist));
1219 
1220  bug_if(cpl_propertylist_copy_property_regexp(qclist, reflist, "^("
1221  IRPLIB_PFITS_REGEXP_RECAL
1222  ")$", 0));
1223 
1224  bug_if (irplib_pfits_set_airmass(qclist, rawframes));
1225  bug_if(cpl_propertylist_append_double(qclist, "ESO QC AIRMASS",
1226  irplib_pfits_get_double(qclist,
1227  "AIRMASS")));
1228 
1229  end_skip;
1230 
1231  return cpl_error_get_code();
1232 }
1233 
1234 /*----------------------------------------------------------------------------*/
1246 /*----------------------------------------------------------------------------*/
1247 static cpl_error_code naco_img_zpoint_save(cpl_frameset * set,
1248  const cpl_parameterlist * parlist,
1249  const cpl_propertylist * qclist,
1250  const cpl_propertylist * paflist,
1251  const cpl_table * tab,
1252  const cpl_image * check_im)
1253 {
1254 
1255  bug_if(0);
1256 
1257  /* Write the zpoint table */
1258  skip_if (irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
1259  NACO_IMG_ZPOINT_RES, qclist, NULL, naco_pipe_id,
1260  RECIPE_STRING CPL_DFS_FITS));
1261 
1262 
1263  /* Write the check image */
1264  if (check_im)
1265  skip_if (irplib_dfs_save_image(set, parlist, set, check_im,
1266  CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
1267  NACO_IMG_ZPOINT_CHECK, qclist, NULL,
1268  naco_pipe_id,
1269  RECIPE_STRING "_check" CPL_DFS_FITS));
1270 
1271  skip_if (cpl_dfs_save_paf("NACO", RECIPE_STRING, paflist,
1272  RECIPE_STRING CPL_DFS_PAF));
1273 
1274  end_skip;
1275 
1276  return cpl_error_get_code();
1277 }
1278 
1279 /*----------------------------------------------------------------------------*/
1288 /*----------------------------------------------------------------------------*/
1289 static cpl_error_code naco_img_zpoint_qc_all(cpl_propertylist * qclist,
1290  cpl_propertylist * paflist,
1291  const irplib_framelist * rawframes)
1292 {
1293  const int nframes = irplib_framelist_get_size(rawframes);
1294  cpl_vector * ec_vec = cpl_vector_new(nframes);
1295  cpl_vector * flux_vec = cpl_vector_new(nframes);
1296  cpl_vector * l0_vec = cpl_vector_new(nframes);
1297  cpl_vector * t0_vec = cpl_vector_new(nframes);
1298  cpl_vector * r0_vec = cpl_vector_new(nframes);
1299  cpl_vector * hum_vec = cpl_vector_new(nframes);
1300  double ec, flux, l0, t0, r0, hum;
1301  int i;
1302 
1303 
1304  bug_if(0);
1305 
1306  bug_if(nframes <= 0);
1307 
1308  for (i = 0; i < nframes; i++) {
1309  const cpl_propertylist * plist
1311 
1312  bug_if(0);
1313 
1314  skip_if(cpl_vector_set(ec_vec, i, naco_pfits_get_ecmean(plist)));
1315  skip_if(cpl_vector_set(flux_vec, i, naco_pfits_get_fluxmean(plist)));
1316  skip_if(cpl_vector_set(t0_vec, i, naco_pfits_get_t0mean(plist)));
1317  skip_if(cpl_vector_set(l0_vec, i, naco_pfits_get_l0mean(plist)));
1318  skip_if(cpl_vector_set(r0_vec, i, naco_pfits_get_r0mean(plist)));
1319  skip_if(cpl_vector_set(hum_vec, i,
1321 
1322  }
1323 
1324  bug_if(0);
1325 
1326  ec = cpl_vector_get_median(ec_vec); /* vector-permute */
1327  flux = cpl_vector_get_median(flux_vec); /* vector-permute */
1328  l0 = cpl_vector_get_median(l0_vec); /* vector-permute */
1329  t0 = cpl_vector_get_median(t0_vec); /* vector-permute */
1330  r0 = cpl_vector_get_median(r0_vec); /* vector-permute */
1331  hum = cpl_vector_get_mean(hum_vec);
1332  bug_if(0);
1333 
1334  cpl_propertylist_append_double(qclist, "ESO QC AMBI RHUM AVG", hum);
1335  cpl_propertylist_append_double(paflist,
1336  "ESO AOS RTC DET DST L0MEAN", l0);
1337  cpl_propertylist_append_double(paflist,
1338  "ESO AOS RTC DET DST T0MEAN", t0);
1339  cpl_propertylist_append_double(paflist,
1340  "ESO AOS RTC DET DST R0MEAN", r0);
1341  cpl_propertylist_append_double(paflist,
1342  "ESO AOS RTC DET DST ECMEAN", ec);
1343  cpl_propertylist_append_double(paflist,
1344  "ESO AOS RTC DET DST FLUXMEAN", flux);
1345  bug_if(0);
1346 
1347 
1348  end_skip;
1349 
1350  cpl_vector_delete(ec_vec);
1351  cpl_vector_delete(flux_vec);
1352  cpl_vector_delete(r0_vec);
1353  cpl_vector_delete(t0_vec);
1354  cpl_vector_delete(l0_vec);
1355  cpl_vector_delete(hum_vec);
1356 
1357  return cpl_error_get_code();
1358 }
1359 
1360 
1361 /*----------------------------------------------------------------------------*/
1368 /*----------------------------------------------------------------------------*/
1369 static naco_band naco_get_bbfilter(const char * filter)
1370 {
1371  naco_band self = BAND_UNKNOWN;
1372 
1373 
1374  bug_if (filter == NULL);
1375 
1376  if (!strcmp(filter, "J")) self = BAND_J;
1377  else if (!strcmp(filter, "Jc")) self = BAND_J;
1378  else if (!strcmp(filter, "H")) self = BAND_H;
1379  else if (!strcmp(filter, "K")) self = BAND_K;
1380  else if (!strcmp(filter, "Ks")) self = BAND_KS;
1381  else if (!strcmp(filter, "L")) self = BAND_L;
1382  else if (!strcmp(filter, "L_prime")) self = BAND_LP;
1383  else if (!strcmp(filter, "M_prime")) self = BAND_MP;
1384  else if (!strcmp(filter, "NB_1.04")) self = BAND_J;
1385  else if (!strcmp(filter, "NB_1.08")) self = BAND_J;
1386  else if (!strcmp(filter, "NB_1.09")) self = BAND_J;
1387  else if (!strcmp(filter, "NB_1.24")) self = BAND_J;
1388  else if (!strcmp(filter, "NB_1.26")) self = BAND_J;
1389  else if (!strcmp(filter, "NB_1.28")) self = BAND_J;
1390  else if (!strcmp(filter, "NB_1.64")) self = BAND_H;
1391  else if (!strcmp(filter, "NB_1.75")) self = BAND_H;
1392  else if (!strcmp(filter, "NB_3.74")) self = BAND_L;
1393  else if (!strcmp(filter, "IB_2.00")) self = BAND_K;
1394  else if (!strcmp(filter, "IB_2.03")) self = BAND_K;
1395  else if (!strcmp(filter, "IB_2.06")) self = BAND_K;
1396  else if (!strcmp(filter, "IB_2.09")) self = BAND_K;
1397  else if (!strcmp(filter, "IB_2.12")) self = BAND_K;
1398  else if (!strcmp(filter, "NB_2.12")) self = BAND_K;
1399  else if (!strcmp(filter, "IB_2.15")) self = BAND_K;
1400  else if (!strcmp(filter, "NB_2.17")) self = BAND_K;
1401  else if (!strcmp(filter, "IB_2.18")) self = BAND_K;
1402  else if (!strcmp(filter, "IB_2.21")) self = BAND_K;
1403  else if (!strcmp(filter, "IB_2.24")) self = BAND_K;
1404  else if (!strcmp(filter, "IB_2.27")) self = BAND_K;
1405  else if (!strcmp(filter, "IB_2.30")) self = BAND_K;
1406  else if (!strcmp(filter, "IB_2.33")) self = BAND_K;
1407  else if (!strcmp(filter, "IB_2.36")) self = BAND_K;
1408  else if (!strcmp(filter, "IB_2.39")) self = BAND_K;
1409  else if (!strcmp(filter, "IB_2.42")) self = BAND_K;
1410  else if (!strcmp(filter, "IB_2.45")) self = BAND_K;
1411  else if (!strcmp(filter, "IB_2.48")) self = BAND_K;
1412  else if (!strcmp(filter, "NB_4.05")) self = BAND_M;
1413  /* As per email 2009-10-13 from epompei@eso.org */
1414  else if (!strcmp(filter, "IB_4.05")) self = BAND_M;
1415  else if (!strcmp(filter, "SJ")) self = BAND_UNKNOWN;
1416  else if (!strcmp(filter, "SH")) self = BAND_UNKNOWN;
1417  else if (!strcmp(filter, "SK")) self = BAND_UNKNOWN;
1418 
1419  error_if (self == BAND_UNKNOWN, CPL_ERROR_UNSUPPORTED_MODE, "No broad-"
1420  "band could be associated with the filter: %s", filter);
1421 
1422  end_skip;
1423 
1424  return self;
1425 }
1426 
1427 
1428 /*----------------------------------------------------------------------------*/
1439 /*----------------------------------------------------------------------------*/
1440 static cpl_table * naco_img_zpoint_load_std_star(const char * star_cat,
1441  double ra, double dec,
1442  double tol)
1443 {
1444 
1445  cpl_table * self = cpl_table_load(star_cat, 1, 0);
1446  const double * pra
1447  = cpl_table_get_data_double_const(self, IRPLIB_STDSTAR_RA_COL);
1448  const double * pdec
1449  = cpl_table_get_data_double_const(self, IRPLIB_STDSTAR_DEC_COL);
1450  const int nrow = cpl_table_get_nrow(self);
1451  int i;
1452 
1453  skip_if(self == NULL);
1454  bug_if(star_cat == NULL);
1455  skip_if(pra == NULL);
1456  skip_if(pdec == NULL);
1457  skip_if(tol < 0.0);
1458 
1459  bug_if(cpl_table_unselect_all(self));
1460 
1461  for (i = 0; i < nrow; i++) {
1462  const double deci = pdec[i];
1463 
1464  if (deci > dec + tol ||
1465  deci < dec - tol ||
1466  irplib_wcs_great_circle_dist(ra, dec, pra[i], deci) > tol) {
1467 
1468  bug_if(cpl_table_select_row(self, i));
1469 
1470  }
1471  }
1472 
1473  if (cpl_table_count_selected(self) == nrow) {
1474  double mindist = DBL_MAX; /* Avoid uninit warning */
1475  int idist = 0; /* Avoid uninit warning */
1476 
1477  for (i = 0; i < nrow; i++) {
1478  const double dist
1479  = irplib_wcs_great_circle_dist(ra, dec, pra[i], pdec[i]);
1480 
1481  if (i == 0 || dist < mindist) {
1482  idist = i;
1483  mindist = dist;
1484  }
1485  }
1486 
1487  (void)cpl_error_set_message_macro(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1488  __FILE__, __LINE__, "Catalog '%s' "
1489  "star %d of %d at (RA,DEC)=(%g,%g) "
1490  "is closest to target at "
1491  "(RA,DEC)=(%g,%g) with a too large "
1492  "distance of %g [degrees] > %g",
1493  star_cat, 1+idist, nrow, pra[idist],
1494  pdec[idist], ra, dec, mindist, tol);
1495  }
1496 
1497  bug_if(cpl_table_erase_selected(self));
1498 
1499  cpl_msg_info(cpl_func, "Loaded %d of %d stars from '%s' which are within "
1500  "%g degrees from (RA,DEC)=(%g,%g)",
1501  (int)cpl_table_get_nrow(self), nrow, star_cat, tol, ra, dec);
1502  if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
1503  cpl_table_dump(self, 0, cpl_table_get_nrow(self), stdout);
1504  }
1505 
1506  end_skip;
1507 
1508  return self;
1509 
1510 }
1511 
1512 /*-------------------------------------------------------------------------*/
1519 /*--------------------------------------------------------------------------*/
1520 static const char * naco_std_band_name(naco_band band)
1521 {
1522  switch (band) {
1523  case BAND_J: return "J";
1524  case BAND_JS: return "Js";
1525  case BAND_JBLOCK: return "J+Block";
1526  case BAND_H: return "H";
1527  case BAND_K: return "K";
1528  case BAND_KS: return "Ks";
1529  case BAND_L: return "L";
1530  case BAND_M: return "M";
1531  case BAND_LP: return "Lp";
1532  case BAND_MP: return "Mp";
1533  case BAND_Z: return "Z";
1534  case BAND_SZ: return "SZ";
1535  case BAND_SH: return "SH";
1536  case BAND_SK: return "SK";
1537  case BAND_SL: return "SL";
1538  default: return "Unknown";
1539  }
1540 }
1541 
cpl_error_code irplib_dfs_save_table(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_table *table, const cpl_propertylist *tablelist, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a table as a DFS-compliant pipeline product.
Definition: irplib_utils.c:351
cpl_error_code naco_strehl_compute(const cpl_image *self, const cpl_parameterlist *parlist, const char *recipename, double lam, double dlam, double pos_x, double pos_y, double pixscale, double *pstrehl, double *pstrehl_err, double *pstar_bg, double *pstar_peak, double *pstar_flux, double *ppsf_peak, double *ppsf_flux, double *pbg_noise)
Compute the strehl ratio in an image.
Definition: naco_strehl.c:80
double naco_pfits_get_dit(const cpl_propertylist *self)
find out the DIT
Definition: naco_pfits.c:131
double naco_pfits_get_pixscale(const cpl_propertylist *self)
find out the pixel scale
Definition: naco_pfits.c:341
double naco_pfits_get_cumoffsety(const cpl_propertylist *self)
find out the cumulative offset in Y
Definition: naco_pfits.c:107
const char * naco_pfits_get_opti4_name(const cpl_propertylist *self)
find out the OPTI4.NAME key
Definition: naco_pfits.c:316
cpl_error_code naco_get_filter_infos(const char *f, double *lam, double *dlam)
Get the infos of one of the filters.
Definition: naco_utils.c:61
double naco_pfits_get_t0mean(const cpl_propertylist *self)
find out the T0MEAN key
Definition: naco_pfits.c:401
double naco_pfits_get_ra(const cpl_propertylist *self)
find out the RA
Definition: naco_pfits.c:365
int naco_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: naco_dfs.c:62
double naco_pfits_get_cumoffsetx(const cpl_propertylist *self)
find out the cumulative offset in X
Definition: naco_pfits.c:95
cpl_error_code irplib_pfits_set_airmass(cpl_propertylist *self, const irplib_framelist *rawframes)
Update/Set the AIRMASS property.
Definition: irplib_pfits.c:373
double naco_pfits_get_dec(const cpl_propertylist *self)
find out the DEC
Definition: naco_pfits.c:119
irplib_framelist * irplib_framelist_extract_regexp(const irplib_framelist *self, const char *regexp, cpl_boolean invert)
Extract the frames with the given tag from a framelist.
double naco_pfits_get_l0mean(const cpl_propertylist *self)
find out the L0MEAN
Definition: naco_pfits.c:244
int naco_parameterlist_get_int(const cpl_parameterlist *self, const char *recipe, naco_parameter bitmask)
Retrieve the value of a NACO integer parameter.
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_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:208
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.
double naco_parameterlist_get_double(const cpl_parameterlist *self, const char *recipe, naco_parameter bitmask)
Retrieve the value of a NACO parameter of type double.
cpl_boolean naco_parameterlist_get_bool(const cpl_parameterlist *self, const char *recipe, naco_parameter bitmask)
Retrieve the value of a NACO boolean parameter.
double naco_pfits_get_fluxmean(const cpl_propertylist *self)
find out the FLUXMEAN key
Definition: naco_pfits.c:208
double naco_pfits_get_humidity_level(const cpl_propertylist *self)
find out the humidity level
Definition: naco_pfits.c:232
const char * naco_pfits_get_object(const cpl_propertylist *self)
Get the Object name.
Definition: naco_pfits.c:280
const cpl_frame * irplib_framelist_get_const(const irplib_framelist *self, int pos)
Get the specified frame from the framelist.
const char * naco_pfits_get_opti3_name(const cpl_propertylist *self)
find out the OPTI3.NAME key
Definition: naco_pfits.c:304
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
const char * irplib_frameset_find_file(const cpl_frameset *self, const char *tag)
Find the filename with the given tag in a frame set.
double naco_pfits_get_r0mean(const cpl_propertylist *self)
find out the R0MEAN key
Definition: naco_pfits.c:353
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.
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.
double naco_pfits_get_ecmean(const cpl_propertylist *self)
find out the ECMEAN key
Definition: naco_pfits.c:143
const char * naco_pfits_get_filter(const cpl_propertylist *self)
find out the filter
Definition: naco_pfits.c:167