HAWKI Pipeline Reference Manual  1.8.12
hawki_cal_zpoint.c
1 /* $Id: hawki_cal_zpoint.c,v 1.36 2013/02/01 16:51:43 cgarcia Exp $
2  *
3  * This file is part of the HAWKI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2013/02/01 16:51:43 $
24  * $Revision: 1.36 $
25  * $Name: hawki-1_8_12 $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <string.h>
37 #include <math.h>
38 #include <cpl.h>
39 
40 #include "irplib_utils.h"
41 #include "irplib_calib.h"
42 #include "irplib_strehl.h"
43 #include "irplib_stdstar.h"
44 #include "irplib_cat.h"
45 #include "irplib_wcs.h"
46 
47 #include "hawki_image_stats.h"
48 #include "hawki_utils.h"
49 #include "hawki_calib.h"
50 #include "hawki_load.h"
51 #include "hawki_save.h"
52 #include "hawki_pfits.h"
53 #include "hawki_dfs.h"
54 #include "hawki_alloc.h"
55 
56 /*-----------------------------------------------------------------------------
57  Functions prototypes
58  -----------------------------------------------------------------------------*/
59 
60 #ifdef __cplusplus
61 extern "C"
62 #endif
63 int cpl_plugin_get_info(cpl_pluginlist * list);
64 
65 static int hawki_cal_zpoint_create(cpl_plugin *) ;
66 static int hawki_cal_zpoint_exec(cpl_plugin *) ;
67 static int hawki_cal_zpoint_destroy(cpl_plugin *) ;
68 static int hawki_cal_zpoint(cpl_parameterlist *, cpl_frameset *) ;
69 
70 static void hawki_cal_zpoint_output_init(void);
71 static int hawki_cal_zpoint_retrieve_input_param
72 (cpl_parameterlist * parlist);
73 static cpl_table ** hawki_cal_zpoint_reduce
74 (cpl_frameset * set,
75  const char * stdstars,
76  const char * bpm,
77  const char * flat,
78  cpl_table ** raw_zpoint_stats,
79  int * labels,
80  cpl_imagelist ** images);
81 static int hawki_cal_zpoint_save
82 (cpl_table ** zpoint_tables,
83  int * labels,
84  cpl_imagelist * images,
85  cpl_table ** raw_zpoint_stats,
86  cpl_frameset * zpoint_frames,
87  cpl_frameset * calib_frames,
88  const cpl_frame * stars_frame,
89  cpl_parameterlist * parlist,
90  cpl_frameset * set);
91 static int hawki_cal_zpoint_compute_qc
92 (cpl_propertylist * qcmainparams,
93  cpl_propertylist ** qcextparams,
94  cpl_frameset * set);
95 static cpl_table ** hawki_cal_zpoint_photom
96 (cpl_imagelist * ilist,
97  cpl_bivector * pos,
98  int * labels);
99 static int hawki_cal_zpoint_get_mag(const char *, double, double, hawki_band) ;
100 static int hawki_cal_zpoint_compute_keywords
101 (cpl_frameset * set,
102  int * labels);
103 static cpl_error_code hawki_cal_zpoint_get_expected_pos
104 (cpl_frameset * set,
105  int * labels);
106 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist);
107 
108 /*-----------------------------------------------------------------------------
109  Static variables
110  -----------------------------------------------------------------------------*/
111 
112 static struct {
113  /* Inputs */
114  double xcoord[HAWKI_NB_DETECTORS] ;
115  double ycoord[HAWKI_NB_DETECTORS] ;
116  double target_ra;
117  double target_dec;
118  double stdstar_given_magnitude;
119  double detect_sigma ;
120  int sx ;
121  int sy ;
122  double phot_star_radius ;
123  double phot_bg_r1 ;
124  double phot_bg_r2 ;
125 } hawki_cal_zpoint_config;
126 
127 static struct {
128  /* Outputs */
129  double dit;
130  double pixscale;
131  char filter[512];
132  hawki_band band;
133  char starname[512];
134  char sptype[512];
135  char catalog[512];
136  double humidity;
137  double airmass[HAWKI_NB_DETECTORS];
138  double zpoint[HAWKI_NB_DETECTORS];
139  double atx0[HAWKI_NB_DETECTORS];
140  double posx[HAWKI_NB_DETECTORS];
141  double posy[HAWKI_NB_DETECTORS];
142  double flux[HAWKI_NB_DETECTORS];
143  double instrmag[HAWKI_NB_DETECTORS];
144  double peak[HAWKI_NB_DETECTORS];
145  double bgd[HAWKI_NB_DETECTORS];
146  double fwhmx[HAWKI_NB_DETECTORS];
147  double fwhmy[HAWKI_NB_DETECTORS];
148  double fwhm[HAWKI_NB_DETECTORS];
149  double fwhmx_as[HAWKI_NB_DETECTORS];
150  double fwhmy_as[HAWKI_NB_DETECTORS];
151  double fwhm_as[HAWKI_NB_DETECTORS];
152  double mean_zpoint;
153  double mean_atx0;
154  double mean_airmass;
155  double ext_coeff;
156  double stdstar_ra;
157  double stdstar_dec;
158  double stdstar_mag_filter;
159  double stdstar_mag_H;
160  double stdstar_mag_J;
161  double stdstar_mag_K;
162  double stdstar_mag_Y;
163  int stdstar_incat_found;
164  int stdstar_mag_available;
165  int stdstar_image_detected[HAWKI_NB_DETECTORS];
166  int zpoint_computable[HAWKI_NB_DETECTORS];
167  int zpoint_mean_computable;
168 } hawki_cal_zpoint_outputs;
169 
170 static char hawki_cal_zpoint_description[] =
171 "hawki_cal_zpoint -- Zero point recipe\n"
172 "The input of the recipe files listed in the Set Of Frames (sof-file)\n"
173 "must be tagged as:\n"
174 "raw-file.fits "HAWKI_CAL_ZPOINT_RAW" or\n"
175 "stdstars-file.fits "HAWKI_CALPRO_STDSTARS" or\n"
176 "flat-file.fits "HAWKI_CALPRO_FLAT" or\n"
177 "bpm-file.fits "HAWKI_CALPRO_BPM"\n"
178 "The recipe creates as an output:\n"
179 "hawki_cal_zpoint.fits ("HAWKI_CALPRO_ZPOINT_TAB"): Zero point solution table\n"
180 "hawki_cal_zpoint_check.fits ("HAWKI_CALPRO_ZPOINT_IMA"): Standard star images corrected (for checking purposes)\n"
181 "hawki_cal_zpoint_stats.fits ("HAWKI_CALPRO_ZPOINT_STATS"): Statistics of the raw standard star images\n"
182 "Return code:\n"
183 "esorex exits with an error code of 0 if the recipe completes successfully\n"
184 "or 1 otherwise";
185 
186 /*-----------------------------------------------------------------------------
187  Functions code
188  -----------------------------------------------------------------------------*/
189 
190 /*----------------------------------------------------------------------------*/
198 /*----------------------------------------------------------------------------*/
199 int cpl_plugin_get_info(cpl_pluginlist * list)
200 {
201  cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe));
202  cpl_plugin * plugin = &recipe->interface;
203 
204  cpl_plugin_init(plugin,
205  CPL_PLUGIN_API,
206  HAWKI_BINARY_VERSION,
207  CPL_PLUGIN_TYPE_RECIPE,
208  "hawki_cal_zpoint",
209  "Zero point computation recipe",
210  hawki_cal_zpoint_description,
211  "Cesar Enrique Garcia Dabo",
212  PACKAGE_BUGREPORT,
214  hawki_cal_zpoint_create,
215  hawki_cal_zpoint_exec,
216  hawki_cal_zpoint_destroy) ;
217 
218  cpl_pluginlist_append(list, plugin) ;
219 
220  return 0;
221 }
222 
223 /*----------------------------------------------------------------------------*/
232 /*----------------------------------------------------------------------------*/
233 static int hawki_cal_zpoint_create(cpl_plugin * plugin)
234 {
235  cpl_recipe * recipe ;
236  cpl_parameter * p ;
237 
238  /* Get the recipe out of the plugin */
239  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
240  recipe = (cpl_recipe *)plugin ;
241  else return -1 ;
242 
243  /* Create the parameters list in the cpl_recipe object */
244  recipe->parameters = cpl_parameterlist_new();
245  if (recipe->parameters == NULL)
246  return 1;
247 
248  /* Fill the parameters list */
249  /* --detect_sigma */
250  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.detect_sigma",
251  CPL_TYPE_DOUBLE, "the sigma value for object detection",
252  "hawki.hawki_cal_zpoint", 7.0);
253  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "detect_sigma") ;
254  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
255  cpl_parameterlist_append(recipe->parameters, p) ;
256  /* --star_r */
257  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.star_r",CPL_TYPE_DOUBLE,
258  "the star radius", "hawki.hawki_cal_zpoint", -1.0) ;
259  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "star_r") ;
260  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
261  cpl_parameterlist_append(recipe->parameters, p) ;
262  /* --bg_r1 */
263  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.bg_r1", CPL_TYPE_DOUBLE,
264  "the internal background radius", "hawki.hawki_cal_zpoint", -1.0) ;
265  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bg_r1") ;
266  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
267  cpl_parameterlist_append(recipe->parameters, p) ;
268  /* --bg_r2 */
269  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.bg_r2", CPL_TYPE_DOUBLE,
270  "the external background radius", "hawki.hawki_cal_zpoint", -1.0) ;
271  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bg_r2") ;
272  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
273  cpl_parameterlist_append(recipe->parameters, p) ;
274  /* --ra */
275  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.ra", CPL_TYPE_DOUBLE,
276  "RA in degrees", "hawki.hawki_cal_zpoint", 999.0) ;
277  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ra") ;
278  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
279  cpl_parameterlist_append(recipe->parameters, p) ;
280  /* --dec */
281  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.dec", CPL_TYPE_DOUBLE,
282  "DEC in degrees", "hawki.hawki_cal_zpoint", 999.0) ;
283  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dec") ;
284  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
285  cpl_parameterlist_append(recipe->parameters, p) ;
286  /* --mag */
287  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.mag", CPL_TYPE_DOUBLE,
288  "magnitude", "hawki.hawki_cal_zpoint", 99.0) ;
289  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mag") ;
290  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
291  cpl_parameterlist_append(recipe->parameters, p) ;
292  /* --sx */
293  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.sx", CPL_TYPE_INT,
294  "x half-size of the search box", "hawki.hawki_cal_zpoint", 100) ;
295  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sx") ;
296  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
297  cpl_parameterlist_append(recipe->parameters, p) ;
298  /* --sy */
299  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.sy", CPL_TYPE_INT,
300  "y half-size of the search box", "hawki.hawki_cal_zpoint", 100) ;
301  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sy") ;
302  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
303  cpl_parameterlist_append(recipe->parameters, p) ;
304  /* --xcoord */
305  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.xcoord", CPL_TYPE_STRING,
306  "Coordinates in X where the standard star is located. If -1 use WCS",
307  "hawki.hawki_cal_zpoint", "-1., -1., -1., -1.");
308  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xcoord") ;
309  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
310  cpl_parameterlist_append(recipe->parameters, p) ;
311  /* --ycoord */
312  p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.ycoord", CPL_TYPE_STRING,
313  "Coordinates in Y where the standard star is located. If -1 use WCS",
314  "hawki.hawki_cal_zpoint", "-1., -1., -1., -1.") ;
315  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ycoord") ;
316  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
317  cpl_parameterlist_append(recipe->parameters, p) ;
318 
319  /* Return */
320  return 0;
321 }
322 
323 /*----------------------------------------------------------------------------*/
329 /*----------------------------------------------------------------------------*/
330 static int hawki_cal_zpoint_exec(cpl_plugin * plugin)
331 {
332  cpl_recipe * recipe ;
333 
334  /* Get the recipe out of the plugin */
335  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
336  recipe = (cpl_recipe *)plugin ;
337  else return -1 ;
338 
339  /* Issue a banner */
341 
342  return hawki_cal_zpoint(recipe->parameters, recipe->frames) ;
343 }
344 
345 /*----------------------------------------------------------------------------*/
351 /*----------------------------------------------------------------------------*/
352 static int hawki_cal_zpoint_destroy(cpl_plugin * plugin)
353 {
354  cpl_recipe * recipe ;
355 
356  /* Get the recipe out of the plugin */
357  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
358  recipe = (cpl_recipe *)plugin ;
359  else return -1 ;
360 
361  cpl_parameterlist_delete(recipe->parameters) ;
362  return 0 ;
363 }
364 
365 /*----------------------------------------------------------------------------*/
372 /*----------------------------------------------------------------------------*/
373 static int hawki_cal_zpoint(
374  cpl_parameterlist * parlist,
375  cpl_frameset * framelist)
376 {
377  cpl_parameter * par ;
378  const char * flat ;
379  const char * bpm ;
380  const char * stdstars ;
381  cpl_frameset * zpoint_frames ;
382  cpl_frameset * calib_frames ;
383  const cpl_frame * stars_frame;
384  cpl_table ** raw_zpoint_stats;
385  cpl_table ** zpoint_tables;
386  cpl_imagelist * std_star_images ;
387  int * labels;
388  int idet;
389 
390  /* Initialise Output */
391  hawki_cal_zpoint_output_init();
392  zpoint_frames = NULL ;
393  par = NULL ;
394 
395  /* Retrieve input parameters */
396  if(hawki_cal_zpoint_retrieve_input_param(parlist))
397  {
398  cpl_msg_error(__func__, "Wrong parameters");
399  return -1;
400  }
401 
402  /* Identify the RAW and CALIB frames in the input frameset */
403  if (hawki_dfs_set_groups(framelist)) {
404  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
405  return -1 ;
406  }
407 
408  /* Retrieve calibration data */
409  calib_frames = cpl_frameset_new();
410  flat = hawki_extract_first_filename(framelist, HAWKI_CALPRO_FLAT) ;
411  if(flat)
412  cpl_frameset_insert(calib_frames, cpl_frame_duplicate(
413  cpl_frameset_find_const(framelist, HAWKI_CALPRO_FLAT)));
414  bpm = hawki_extract_first_filename(framelist, HAWKI_CALPRO_BPM) ;
415  if(bpm)
416  cpl_frameset_insert(calib_frames, cpl_frame_duplicate(
417  cpl_frameset_find_const(framelist, HAWKI_CALPRO_BPM)));
418 
419  /* STD stars catalog requested */
420  stars_frame = cpl_frameset_find_const(framelist, HAWKI_CALPRO_STDSTARS);
421  if (stars_frame == NULL)
422  {
423  cpl_msg_error(__func__,"Cannot find the catalog in the input list (%s)",
424  HAWKI_CALPRO_STDSTARS);
425  cpl_frameset_delete(calib_frames);
426  return -1 ;
427  }
428  stdstars = cpl_frame_get_filename(stars_frame);
429 
430  /* Retrieve raw frames */
431  if ((zpoint_frames = hawki_extract_frameset(framelist,
432  HAWKI_CAL_ZPOINT_RAW)) != NULL) {
433  } else {
434  cpl_msg_error(__func__, "Cannot find raw frames in the input list (%s)",
435  HAWKI_CAL_ZPOINT_RAW);
436  cpl_frameset_delete(calib_frames);
437  return -1 ;
438  }
439 
440  /* Exactly 4 images are expected */
441  if (cpl_frameset_get_size(zpoint_frames) != 4) {
442  cpl_msg_error(__func__,
443  "4 input raw frames are expected, not %"CPL_SIZE_FORMAT,
444  cpl_frameset_get_size(zpoint_frames)) ;
445  cpl_frameset_delete(zpoint_frames) ;
446  cpl_frameset_delete(calib_frames);
447  return -1 ;
448  }
449 
450  /* Create the statistics table */
451  raw_zpoint_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
452  for(idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
453  {
454  raw_zpoint_stats[idet] = cpl_table_new(
455  cpl_frameset_get_size(zpoint_frames));
456  }
457  hawki_image_stats_initialize(raw_zpoint_stats);
458 
459  /* Compute the zpoint values */
460  cpl_msg_info(__func__, "Reduce the data") ;
461  cpl_msg_indent_more() ;
462  labels = cpl_calloc(cpl_frameset_get_size(zpoint_frames), sizeof(int)) ;
463  if ((zpoint_tables = hawki_cal_zpoint_reduce(zpoint_frames, stdstars,
464  bpm, flat, raw_zpoint_stats, labels, &std_star_images))==NULL)
465  {
466  cpl_msg_error(__func__, "Cannot reduce the data") ;
467  cpl_frameset_delete(zpoint_frames) ;
468  cpl_frameset_delete(calib_frames);
469  hawki_table_delete(raw_zpoint_stats);
470  cpl_free(labels);
471  cpl_msg_indent_less() ;
472  return -1 ;
473  }
474  cpl_msg_indent_less() ;
475 
476  /* Save the products */
477  cpl_msg_info(__func__, "Save the products") ;
478  cpl_msg_indent_more() ;
479  if (hawki_cal_zpoint_save
480  (zpoint_tables, labels, std_star_images, raw_zpoint_stats,
481  zpoint_frames, calib_frames, stars_frame,
482  parlist, framelist) == -1)
483  {
484  cpl_msg_warning(__func__, "Data could not be saved. "
485  "Check permisions or disk space") ;
486  cpl_frameset_delete(zpoint_frames);
487  hawki_table_delete(zpoint_tables) ;
488  cpl_imagelist_delete(std_star_images) ;
489  cpl_frameset_delete(calib_frames);
490  hawki_table_delete(raw_zpoint_stats);
491  cpl_free(labels);
492  cpl_msg_indent_less() ;
493  return -1 ;
494  }
495  cpl_msg_indent_less() ;
496 
497  /* Free and return */
498  cpl_frameset_delete(zpoint_frames);
499  cpl_frameset_delete(calib_frames);
500  cpl_imagelist_delete(std_star_images);
501  hawki_table_delete(zpoint_tables);
502  hawki_table_delete(raw_zpoint_stats);
503  cpl_free(labels);
504 
505  /* Return */
506  if (cpl_error_get_code())
507  {
508  cpl_msg_error(__func__,
509  "HAWK-I pipeline could not recover from previous errors");
510  return -1 ;
511  }
512  else return 0 ;
513 }
514 
515 /*----------------------------------------------------------------------------*/
525 /*----------------------------------------------------------------------------*/
526 static cpl_table ** hawki_cal_zpoint_reduce
527 (cpl_frameset * set,
528  const char * stdstars,
529  const char * bpm,
530  const char * flat,
531  cpl_table ** raw_zpoint_stats,
532  int * labels,
533  cpl_imagelist ** star_images)
534 {
535  cpl_frame * cur_frame ;
536  cpl_propertylist * plist ;
537  const char * sval ;
538  cpl_imagelist * star_images_frame_order ;
539  int nima ;
540  cpl_bivector * positions ;
541  cpl_image * filt_ima ;
542  cpl_mask * kernel;
543  int size_x, size_y ;
544  double pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist ;
545  cpl_apertures * aperts ;
546  cpl_table ** zpoint_tables;
547  cpl_image * tmp_ima ;
548  int iaper;
549  int idet;
550  int iframe;
551  int iframe_star = -1;
552  int nframes;
553  char rastr[32];
554  char decstr[32];
555  cpl_errorstate error_prevstate;
556  int return_code;
557 
558  /* Check inputs */
559  if (set == NULL) return NULL ;
560  if (stdstars == NULL) return NULL ;
561  if (star_images == NULL) return NULL ;
562 
563  /* Get the filter name, DIT, Target RA and DEC */
564  error_prevstate = cpl_errorstate_get();
565  cur_frame = cpl_frameset_get_frame(set, 0) ;
566  plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
567  if ((sval = hawki_pfits_get_filter(plist)) == NULL) return NULL ;
568  else sprintf(hawki_cal_zpoint_outputs.filter, sval) ;
569  if (hawki_cal_zpoint_config.target_ra > 998.0)
570  {
571  hawki_cal_zpoint_config.target_ra = hawki_pfits_get_targ_alpha(plist);
572  //hawki_cal_zpoint_config.target_ra = hawki_pfits_get_ra(plist) -
573  //hawki_pfits_get_cumoffseta(plist) / 3600.;// Not valid before Nov 2008
574  if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
575  {
576  cpl_propertylist_delete(plist);
577  return NULL;
578  }
579  }
580  if (hawki_cal_zpoint_config.target_dec > 998.0)
581  {
582  hawki_cal_zpoint_config.target_dec = hawki_pfits_get_targ_delta(plist);
583  //hawki_cal_zpoint_config.target_dec = hawki_pfits_get_dec(plist) -
584  //hawki_pfits_get_cumoffsetd(plist) / 3600.;// Not valid before Nov 2008
585  if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
586  {
587  cpl_propertylist_delete(plist);
588  return NULL;
589  }
590  }
591  hawki_cal_zpoint_outputs.dit = hawki_pfits_get_dit(plist) ;
592  hawki_cal_zpoint_outputs.pixscale = hawki_pfits_get_pixscale(plist) ;
593  cpl_propertylist_delete(plist) ;
594  if(!cpl_errorstate_is_equal(error_prevstate))
595  {
596  cpl_msg_error(__func__, "Cannot get keywords from main header:");
597  cpl_msg_indent_more();
598  cpl_msg_error(__func__, "%s",cpl_error_get_message());
599  cpl_msg_indent_less();
600  return NULL ;
601  }
602  cpl_msg_info(__func__,"Searching catalog stars closest to target:");
603  cpl_msg_indent_more();
604  hawki_utils_ra2str(rastr, 32, hawki_cal_zpoint_config.target_ra);
605  hawki_utils_dec2str(decstr, 32, hawki_cal_zpoint_config.target_dec);
606  cpl_msg_info(__func__,"RA = %g (%s); DEC = %g (%s)",
607  hawki_cal_zpoint_config.target_ra, rastr,
608  hawki_cal_zpoint_config.target_dec, decstr);
609  cpl_msg_info(__func__,"HAWK-I Filter: %s", hawki_cal_zpoint_outputs.filter);
610  cpl_msg_indent_less();
611 
612  /* Get the band */
613  if ((hawki_cal_zpoint_outputs.band =
614  hawki_get_band(hawki_cal_zpoint_outputs.filter)) ==
615  HAWKI_BAND_UNKNOWN) {
616  cpl_msg_error(__func__, "Cannot associate the filter %s to a band",
617  hawki_cal_zpoint_outputs.filter) ;
618  return NULL ;
619  }
620 
621  /* Get the standard star information from database */
622  cpl_msg_indent_more();
623  return_code = hawki_cal_zpoint_get_mag(stdstars,
624  hawki_cal_zpoint_config.target_ra,
625  hawki_cal_zpoint_config.target_dec,
626  hawki_cal_zpoint_outputs.band);
627  if (return_code == -1)
628  {
629  cpl_msg_error(__func__, "Could not open star database");
630  return NULL ;
631  }
632  else if(return_code == 1)
633  {
634  cpl_msg_warning(__func__,"No suitable star found in catalog");
635  cpl_msg_warning(__func__,"Using the target coordinates "
636  "as the standard star coordinates: ");
637  hawki_cal_zpoint_outputs.stdstar_ra =
638  hawki_cal_zpoint_config.target_ra;
639  hawki_cal_zpoint_outputs.stdstar_dec =
640  hawki_cal_zpoint_config.target_dec;
641  hawki_utils_ra2str(rastr, 32, hawki_cal_zpoint_outputs.stdstar_ra);
642  hawki_utils_dec2str(decstr, 32, hawki_cal_zpoint_outputs.stdstar_dec);
643  cpl_msg_info(__func__, " RA = %g (%s) ; DEC = %g (%s)",
644  hawki_cal_zpoint_outputs.stdstar_ra, rastr,
645  hawki_cal_zpoint_outputs.stdstar_dec, decstr);
646  }
647  else
648  {
649  cpl_msg_info(__func__, "Catalog where the star was found: %s",
650  hawki_cal_zpoint_outputs.catalog);
651  cpl_msg_info(__func__, "Star name: %s",
652  hawki_cal_zpoint_outputs.starname);
653  if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
654  {
655  cpl_msg_info(__func__, "Star magnitude in filter %s : %g [mag]",
656  hawki_cal_zpoint_outputs.filter,
657  hawki_cal_zpoint_outputs.stdstar_mag_filter);
658  }
659  hawki_utils_ra2str(rastr, 32, hawki_cal_zpoint_outputs.stdstar_ra);
660  hawki_utils_dec2str(decstr, 32, hawki_cal_zpoint_outputs.stdstar_dec);
661  cpl_msg_info(__func__, "Star coordinates: RA = %g (%s) ; DEC = %g (%s)",
662  hawki_cal_zpoint_outputs.stdstar_ra, rastr,
663  hawki_cal_zpoint_outputs.stdstar_dec, decstr);
664  }
665  if (hawki_cal_zpoint_config.stdstar_given_magnitude < 98.0)
666  {
667  hawki_cal_zpoint_outputs.stdstar_mag_available = 1;
668  hawki_cal_zpoint_outputs.stdstar_mag_filter =
669  hawki_cal_zpoint_config.stdstar_given_magnitude;
670  cpl_msg_info(__func__, "Using user defined "
671  "star magnitude in filter %s : %g [mag]",
672  hawki_cal_zpoint_outputs.filter,
673  hawki_cal_zpoint_outputs.stdstar_mag_filter);
674  }
675  cpl_msg_indent_less();
676 
677  /* Labelise frames */
678  cpl_msg_info(__func__, "Guessing which frame the STD is in for each chip");
680  (set, hawki_cal_zpoint_outputs.stdstar_ra,
681  hawki_cal_zpoint_outputs.stdstar_dec, labels);
682  if (labels == NULL)
683  {
684  cpl_msg_error(__func__, "Cannot determine which frame the STD is on") ;
685  return NULL ;
686  }
687 
688  /* Compute the expected position of the star in pixels */
689  /* This is stored in hawki_cal_zpoint_config.xcoord, ycoord */
690  cpl_msg_indent_more();
691  if(hawki_cal_zpoint_get_expected_pos(set, labels) != CPL_ERROR_NONE)
692  {
693  cpl_msg_error(__func__,"Could not determine where the star is located");
694  cpl_msg_indent_less();
695  return NULL;
696  }
697  cpl_msg_indent_less();
698 
699  /* Fetch the airmass and humidity */
700  hawki_cal_zpoint_compute_keywords(set, labels);
701 
702  /* Create the positions vector */
703  nima = cpl_frameset_get_size(set) ;
704  positions = cpl_bivector_new(nima) ;
705 
706  /* Initialize */
707  *star_images = cpl_imagelist_new();
708 
709  /* Loop on the detectors */
710  nframes = cpl_frameset_get_size(set) ;
711  cpl_msg_info(__func__,"Loop on the chips");
712  cpl_msg_indent_more() ;
713  for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
714  {
715  cpl_imagelist * sky_images;
716  cpl_image * star_ima = NULL;
717  cpl_image * sky;
718  cpl_image * flat_im;
719  int ext_nb;
720 
721  cpl_msg_info(__func__, "Loading the chip %d", idet+1);
722  cpl_msg_indent_more() ;
723 
724  /* Allocate */
725  sky_images = cpl_imagelist_new();
726 
727  cpl_msg_indent_more() ;
728  for (iframe=0 ; iframe<nframes ; iframe++)
729  {
730  cpl_image * ima_cur;
731 
732  /* Load the image */
733  ima_cur = hawki_load_image(set, iframe, idet+1, CPL_TYPE_FLOAT) ;
734  if(ima_cur == NULL)
735  {
736  cpl_bivector_delete(positions) ;
737  cpl_imagelist_delete(*star_images);
738  cpl_imagelist_delete(sky_images) ;
739  cpl_msg_error(__func__, "Error reading image");
740  return NULL;
741  }
742  /* Get image statistics */
743  size_x = cpl_image_get_size_x(ima_cur) ;
744  size_y = cpl_image_get_size_y(ima_cur) ;
746  (raw_zpoint_stats,
747  ima_cur,
748  1,
749  1,
750  size_x,
751  size_y,
752  idet,
753  iframe) !=0 )
754  {
755  cpl_msg_error(__func__,"Cannot compute stats on ima %d det %d",
756  iframe+1, idet+1);
757  cpl_bivector_delete(positions) ;
758  cpl_imagelist_delete(*star_images);
759  cpl_imagelist_delete(sky_images) ;
760  cpl_image_delete(ima_cur);
761  cpl_msg_indent_less() ;
762  return NULL ;
763  }
764 
765  /* Add the image to either the sky images or the star image */
766  if(labels[iframe] == idet + 1)
767  {
768  star_ima = ima_cur;
769  iframe_star = iframe;
770  }
771  else
772  cpl_imagelist_set(sky_images, ima_cur,
773  cpl_imagelist_get_size(sky_images));
774  }
775  cpl_msg_indent_less();
776 
777  /* Create the sky */
778  cpl_msg_info(__func__, "Correct for the sky");
779  sky = cpl_imagelist_collapse_median_create(sky_images);
780  cpl_imagelist_delete(sky_images) ;
781 
782  /* Subtract the sky */
783  cpl_image_subtract(star_ima, sky) ;
784  cpl_image_delete(sky) ;
785 
786  /* Divide by the flatfield if one is provided */
787  if (flat) {
788  cpl_msg_info(__func__, "Correct for the flat") ;
789 
790  /* Get the extension with the current chip */
791  if ((ext_nb = hawki_get_ext_from_detector(flat, idet + 1)) == -1)
792  {
793  cpl_msg_error(__func__, "Cannot get the extension with chip %d",
794  idet + 1);
795  cpl_imagelist_delete(*star_images) ;
796  cpl_bivector_delete(positions) ;
797  return NULL ;
798  }
799  /* Load */
800  flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, ext_nb) ;
801  cpl_image_divide(star_ima, flat_im) ;
802  cpl_image_delete(flat_im) ;
803  }
804 
805  /* Correct the bad pixels */
806  if (bpm) {
807  cpl_msg_info(__func__, "Correct for the bad pixels") ;
808  if (hawki_bpm_calib(star_ima, bpm, idet + 1) == -1)
809  {
810  cpl_msg_error(__func__, "Cannot correct the BPM for chip %d",
811  idet + 1);
812  cpl_imagelist_delete(*star_images) ;
813  cpl_bivector_delete(positions) ;
814  return NULL ;
815  }
816  }
817 
818  /* Put the result in the image list */
819  cpl_imagelist_set(*star_images, star_ima,
820  cpl_imagelist_get_size(*star_images)) ;
821 
822  /* Object detection */
823  cpl_msg_info(__func__,"For chip %d the STD should be on frame %d",
824  idet + 1, iframe_star+1);
825  pos_x_cen = pos_y_cen = -1.0 ;
826  size_x = cpl_image_get_size_x(star_ima) ;
827  size_y = cpl_image_get_size_y(star_ima) ;
828 
829  /* Filtering the image*/
830  kernel = cpl_mask_new(3, 3);
831  cpl_mask_not(kernel);
832  filt_ima = cpl_image_new(cpl_image_get_size_x(star_ima),
833  cpl_image_get_size_y(star_ima),
834  cpl_image_get_type(star_ima));
835  cpl_image_filter_mask(filt_ima, star_ima, kernel, CPL_FILTER_MEDIAN,
836  CPL_BORDER_FILTER);
837  cpl_mask_delete(kernel);
838 
839  /* Looking for apertures */
840  aperts = cpl_apertures_extract_sigma(filt_ima,
841  hawki_cal_zpoint_config.detect_sigma) ;
842  cpl_image_delete(filt_ima) ;
843  if (aperts == NULL)
844  {
845  cpl_msg_error(__func__, "Cannot find the central object") ;
846  cpl_imagelist_delete(*star_images) ;
847  cpl_bivector_delete(positions) ;
848  return NULL ;
849  }
850  min_dist = size_x * size_x + size_y * size_y ;
851  for (iaper=0 ; iaper<cpl_apertures_get_size(aperts) ; iaper++) {
852  pos_x = cpl_apertures_get_centroid_x(aperts, iaper+1) ;
853  pos_y = cpl_apertures_get_centroid_y(aperts, iaper+1) ;
854  dist = (pos_x-hawki_cal_zpoint_config.xcoord[idet])*
855  (pos_x-hawki_cal_zpoint_config.xcoord[idet]) +
856  (pos_y-hawki_cal_zpoint_config.ycoord[idet])*
857  (pos_y-hawki_cal_zpoint_config.ycoord[idet]);
858  if (dist<min_dist) {
859  min_dist = dist ;
860  pos_x_cen = pos_x ;
861  pos_y_cen = pos_y ;
862  }
863  }
864  cpl_apertures_delete(aperts) ;
865 
866  cpl_vector_set(cpl_bivector_get_x(positions), iframe_star, pos_x_cen) ;
867  cpl_vector_set(cpl_bivector_get_y(positions), iframe_star, pos_y_cen) ;
868  cpl_msg_info(__func__, "Expected star position: %g %g",
869  hawki_cal_zpoint_config.xcoord[idet],
870  hawki_cal_zpoint_config.ycoord[idet]);
871  cpl_msg_info(__func__, "Bright object position: %g %g",
872  pos_x_cen, pos_y_cen) ;
873 
874  /* Check that the star is within the search window */
875  if(fabs(pos_x_cen - hawki_cal_zpoint_config.xcoord[idet]) >
876  hawki_cal_zpoint_config.sx ||
877  fabs(pos_y_cen - hawki_cal_zpoint_config.ycoord[idet]) >
878  hawki_cal_zpoint_config.sy)
879  {
880  hawki_cal_zpoint_outputs.stdstar_image_detected[idet] = 0;
881  cpl_msg_warning(cpl_func,"No object has been found within the box"
882  "limits [%d, %d] around the expected position",
883  hawki_cal_zpoint_config.sx,
884  hawki_cal_zpoint_config.sy);
885  }
886  else
887  {
888  hawki_cal_zpoint_outputs.stdstar_image_detected[idet] = 1;
889  }
890 
891  /* Free */
892  cpl_msg_indent_less() ;
893  }
894  cpl_msg_indent_less() ;
895 
896  /* Reorder the images (frame order) */
897  star_images_frame_order = cpl_imagelist_new() ;
898  for (iframe=0 ; iframe< nframes; iframe++)
899  {
900  tmp_ima = cpl_image_duplicate
901  (cpl_imagelist_get(*star_images, labels[iframe] -1 ));
902  cpl_imagelist_set(star_images_frame_order, tmp_ima, iframe);
903  }
904 
905  /* Compute the photometry */
906  cpl_msg_info(__func__, "Compute the photometry") ;
907  cpl_msg_indent_more() ;
908  if ((zpoint_tables = hawki_cal_zpoint_photom
909  (star_images_frame_order, positions, labels))==NULL) {
910  cpl_msg_error(__func__, "Cannot reduce") ;
911  cpl_bivector_delete(positions) ;
912  cpl_imagelist_delete(star_images_frame_order) ;
913  cpl_msg_indent_less() ;
914  return NULL ;
915  }
916 
917 
918  /* Free and exit */
919  cpl_msg_indent_less() ;
920  cpl_bivector_delete(positions) ;
921  cpl_imagelist_delete(star_images_frame_order) ;
922 
923  return zpoint_tables;
924 }
925 
926 /*----------------------------------------------------------------------------*/
936 /*----------------------------------------------------------------------------*/
937 static cpl_table ** hawki_cal_zpoint_photom(
938  cpl_imagelist * ilist,
939  cpl_bivector * pos,
940  int * labels)
941 {
942  cpl_table ** zpoint;
943  int nframes;
944  double r, r1, r2;
945  double stdstar_mag;
946  double dit;
947  double extinction;
948  double pixscale;
949  cpl_image * ima ;
950  double * pos_x ;
951  double * pos_y ;
952  double bgd, fl, zp, peak, fwhm_x, fwhm_y ;
953  cpl_bivector * iqe_res ;
954  int iframe;
955  int idet;
956 
957  /* Test entries */
958  if (ilist == NULL) return NULL ;
959  if (pos == NULL) return NULL ;
960 
961  /* Initialise */
962  nframes = cpl_imagelist_get_size(ilist) ;
963  stdstar_mag = hawki_cal_zpoint_outputs.stdstar_mag_filter;
964  dit = hawki_cal_zpoint_outputs.dit ;
965  pixscale = hawki_cal_zpoint_outputs.pixscale ;
966 
967  /* Get extinction */
968  switch (hawki_cal_zpoint_outputs.band) {
969  case HAWKI_BAND_J: extinction = 0.098 ; break ;
970  case HAWKI_BAND_H: extinction = 0.039 ; break ;
971  case HAWKI_BAND_K: extinction = 0.065 ; break ;
972  case HAWKI_BAND_Y: extinction = 0.00 ; break ;
973  default: extinction = 0.00 ; break ;
974  }
975  hawki_cal_zpoint_outputs.ext_coeff = extinction;
976  cpl_msg_info(__func__,"Using tabulated extinction for band %s: %f",
977  hawki_cal_zpoint_outputs.filter, extinction);
978 
979  /* Loop on the images */
980  for (iframe=0 ; iframe<nframes ; iframe++) {
981  idet = labels[iframe]-1;
982  if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
983  {
984  /* Get the current image */
985  ima = cpl_imagelist_get(ilist, iframe) ;
986 
987  /* Get the current position */
988  pos_x = cpl_bivector_get_x_data(pos) ;
989  pos_y = cpl_bivector_get_y_data(pos) ;
990 
991  /* FWHM_X / FWHM_Y */
992  iqe_res = cpl_image_iqe
993  (ima, (int)(pos_x[iframe]-10.0), (int)(pos_y[iframe]-10.0),
994  (int)(pos_x[iframe]+10.0), (int)(pos_y[iframe]+10.0));
995  if (iqe_res == NULL)
996  {
997  cpl_msg_debug(__func__,"Cannot compute FWHM for chip %d",
998  idet + 1);
999  fwhm_x = fwhm_y = -1.0 ;
1000  cpl_error_reset() ;
1001  } else {
1002  fwhm_x = cpl_vector_get(cpl_bivector_get_x(iqe_res), 2) ;
1003  fwhm_y = cpl_vector_get(cpl_bivector_get_x(iqe_res), 3) ;
1004  cpl_bivector_delete(iqe_res) ;
1005  }
1006 
1007  /* Determine the radii */
1008  r = hawki_cal_zpoint_config.phot_star_radius ;
1009  if (r < 0) {
1010  if (fwhm_x>0 && fwhm_y>0) r = 5*(fwhm_x+fwhm_y)/2.0 ;
1011  else r = HAWKI_PHOT_STAR_RADIUS ;
1012  }
1013  r1 = hawki_cal_zpoint_config.phot_bg_r1 ;
1014  r2 = hawki_cal_zpoint_config.phot_bg_r2 ;
1015  if (r1 < 0) r1 = r + 10.0 ;
1016  if (r2 < 0) r2 = r1 + 20.0 ;
1017  //cpl_msg_info(__func__, "Use radii for star: %g and background: %g, %g",
1018  // r, r1, r2) ;
1019 
1020  /* Compute the photometry */
1021  /* Background */
1022  bgd = irplib_strehl_ring_background(ima, (int)(pos_x[iframe]),
1023  (int)(pos_y[iframe]), (int)r1, (int)r2, IRPLIB_BG_METHOD_MEDIAN);
1024  /* Flux */
1025  fl = irplib_strehl_disk_flux(ima,
1026  (int)(pos_x[iframe]), (int)(pos_y[iframe]), (int)r, bgd);
1027 
1028  //cpl_msg_info(__func__, "Zero point in chip %d: %g", labels[iframe], zp) ;
1029  /* Peak */
1030  peak = cpl_image_get_max_window(ima,
1031  (int)(pos_x[iframe]-5), (int)(pos_y[iframe]-5),
1032  (int)(pos_x[iframe]+5), (int)(pos_y[iframe]+5));
1033 
1034  /* Zero Point */
1035  if (hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
1036  {
1037  if (fl > 0 && dit > 0)
1038  {
1039  hawki_cal_zpoint_outputs.zpoint_computable[idet] = 1;
1040  zp = stdstar_mag + 2.5 * log10(fl) - 2.5 * log10(dit);
1041 
1042  hawki_cal_zpoint_outputs.zpoint[idet] = zp;
1043  hawki_cal_zpoint_outputs.atx0[idet] = zp +
1044  extinction * hawki_cal_zpoint_outputs.airmass[idet];
1045  }
1046  else
1047  hawki_cal_zpoint_outputs.zpoint_computable[idet] = 0;
1048  }
1049  hawki_cal_zpoint_outputs.posx[idet] = pos_x[iframe];
1050  hawki_cal_zpoint_outputs.posy[idet] = pos_y[iframe];
1051  hawki_cal_zpoint_outputs.flux[idet] = fl;
1052  hawki_cal_zpoint_outputs.instrmag[idet] = 2.5 * log10(fl/dit);
1053  hawki_cal_zpoint_outputs.peak[idet] = peak;
1054  hawki_cal_zpoint_outputs.bgd[idet] = bgd;
1055  hawki_cal_zpoint_outputs.fwhmx[idet] = fwhm_x;
1056  hawki_cal_zpoint_outputs.fwhmy[idet] = fwhm_y;
1057  if (fwhm_x > 0 && fwhm_y > 0)
1058  hawki_cal_zpoint_outputs.fwhm[idet] = sqrt(fwhm_x*fwhm_y);
1059  else
1060  hawki_cal_zpoint_outputs.fwhm[idet] = -1.0;
1061  hawki_cal_zpoint_outputs.fwhmx_as[idet] = fwhm_x * pixscale;
1062  hawki_cal_zpoint_outputs.fwhmy_as[idet] = fwhm_y * pixscale;
1063  if (fwhm_x > 0 && fwhm_y > 0)
1064  hawki_cal_zpoint_outputs.fwhm_as[idet] =
1065  sqrt(fwhm_x*fwhm_y*pixscale*pixscale);
1066  else
1067  hawki_cal_zpoint_outputs.fwhm_as[labels[iframe]-1] = -1.0;
1068  }
1069  else
1070  {
1071  cpl_msg_warning(cpl_func,"Standard star not detected in chip %d. "
1072  "No zeropoint computed.", idet + 1);
1073  }
1074 
1075  }
1076 
1077  /* Create the table */
1078  zpoint = hawki_table_new(1);
1079  //tab = cpl_table_new(nframes) ;
1080  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
1081  cpl_table_new_column(zpoint[idet],
1082  HAWKI_COL_ZPOINT_CHIP, CPL_TYPE_INT);
1083  cpl_table_new_column(zpoint[idet],
1084  HAWKI_COL_ZPOINT_STARNAME, CPL_TYPE_STRING);
1085  cpl_table_new_column(zpoint[idet],
1086  HAWKI_COL_ZPOINT_POSX, CPL_TYPE_DOUBLE);
1087  cpl_table_set_column_unit(zpoint[idet],
1088  HAWKI_COL_ZPOINT_POSX,"pix");
1089  cpl_table_new_column(zpoint[idet],
1090  HAWKI_COL_ZPOINT_POSY, CPL_TYPE_DOUBLE);
1091  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_POSY,"pix");
1092  cpl_table_new_column(zpoint[idet],
1093  HAWKI_COL_ZPOINT_ZPOINT, CPL_TYPE_DOUBLE);
1094  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ZPOINT,"mag");
1095  cpl_table_new_column(zpoint[idet],
1096  HAWKI_COL_ZPOINT_ATX0, CPL_TYPE_DOUBLE);
1097  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ATX0,"mag");
1098  cpl_table_new_column(zpoint[idet],
1099  HAWKI_COL_ZPOINT_AIRMASS, CPL_TYPE_DOUBLE);
1100  cpl_table_new_column(zpoint[idet],
1101  HAWKI_COL_ZPOINT_FLUX, CPL_TYPE_DOUBLE);
1102  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FLUX,"ADU");
1103  cpl_table_new_column(zpoint[idet],
1104  HAWKI_COL_ZPOINT_INSTRMAG, CPL_TYPE_DOUBLE);
1105  cpl_table_set_column_unit(zpoint[idet],
1106  HAWKI_COL_ZPOINT_INSTRMAG,"log(ADU/s)");
1107  cpl_table_new_column(zpoint[idet],
1108  HAWKI_COL_ZPOINT_FILTER, CPL_TYPE_STRING);
1109  cpl_table_new_column(zpoint[idet],
1110  HAWKI_COL_ZPOINT_PEAK, CPL_TYPE_DOUBLE);
1111  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_PEAK,"ADU");
1112  cpl_table_new_column(zpoint[idet],
1113  HAWKI_COL_ZPOINT_BGD, CPL_TYPE_DOUBLE);
1114  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_BGD,"ADU");
1115  cpl_table_new_column(zpoint[idet],
1116  HAWKI_COL_ZPOINT_FWHMX, CPL_TYPE_DOUBLE);
1117  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX,"pix");
1118  cpl_table_new_column(zpoint[idet],
1119  HAWKI_COL_ZPOINT_FWHMY, CPL_TYPE_DOUBLE);
1120  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY,"pix");
1121  cpl_table_new_column(zpoint[idet],
1122  HAWKI_COL_ZPOINT_FWHM, CPL_TYPE_DOUBLE);
1123  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM,"pix");
1124  cpl_table_new_column(zpoint[idet],
1125  HAWKI_COL_ZPOINT_FWHMX_AS, CPL_TYPE_DOUBLE);
1126  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX_AS,"arcsec");
1127  cpl_table_new_column(zpoint[idet],
1128  HAWKI_COL_ZPOINT_FWHMY_AS, CPL_TYPE_DOUBLE);
1129  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY_AS,"arcsec");
1130  cpl_table_new_column(zpoint[idet],
1131  HAWKI_COL_ZPOINT_FWHM_AS, CPL_TYPE_DOUBLE);
1132  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM_AS,"arcsec");
1133  cpl_table_new_column(zpoint[idet],
1134  HAWKI_COL_ZPOINT_STARMAG, CPL_TYPE_DOUBLE);
1135  cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_STARMAG,"mag");
1136 
1137  cpl_table_set_int(zpoint[idet], HAWKI_COL_ZPOINT_CHIP, 0, idet+1) ;
1138  cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_FILTER, 0,
1139  hawki_cal_zpoint_outputs.filter);
1140  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_AIRMASS, 0,
1141  hawki_cal_zpoint_outputs.airmass[idet]) ;
1142  if(hawki_cal_zpoint_outputs.stdstar_incat_found== 1)
1143  {
1144  cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_STARNAME, 0,
1145  hawki_cal_zpoint_outputs.starname) ;
1146  if(hawki_cal_zpoint_outputs.stdstar_mag_available== 1)
1147  {
1148  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_STARMAG, 0,
1149  hawki_cal_zpoint_outputs.stdstar_mag_filter);
1150  }
1151  }
1152  if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
1153  {
1154  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSX, 0,
1155  hawki_cal_zpoint_outputs.posx[idet]) ;
1156  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSY, 0,
1157  hawki_cal_zpoint_outputs.posy[idet]) ;
1158  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FLUX, 0,
1159  hawki_cal_zpoint_outputs.flux[idet]) ;
1160  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_INSTRMAG, 0,
1161  hawki_cal_zpoint_outputs.instrmag[idet]) ;
1162  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_PEAK, 0,
1163  hawki_cal_zpoint_outputs.peak[idet]) ;
1164  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_BGD, 0,
1165  hawki_cal_zpoint_outputs.bgd[idet]) ;
1166  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX, 0,
1167  hawki_cal_zpoint_outputs.fwhmx[idet]) ;
1168  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY, 0,
1169  hawki_cal_zpoint_outputs.fwhmy[idet]) ;
1170  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM, 0,
1171  hawki_cal_zpoint_outputs.fwhm[idet]) ;
1172  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX_AS, 0,
1173  hawki_cal_zpoint_outputs.fwhmx_as[idet]) ;
1174  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY_AS, 0,
1175  hawki_cal_zpoint_outputs.fwhmy_as[idet]) ;
1176  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM_AS, 0,
1177  hawki_cal_zpoint_outputs.fwhm_as[idet]) ;
1178  if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
1179  {
1180  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ZPOINT, 0,
1181  hawki_cal_zpoint_outputs.zpoint[idet]);
1182  cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ATX0, 0,
1183  hawki_cal_zpoint_outputs.atx0[idet]);
1184  }
1185  }
1186  }
1187 
1188  /* Mean values */
1189  if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
1190  {
1191  int nzpoint = 0;
1192  hawki_cal_zpoint_outputs.mean_zpoint = 0.0 ;
1193  hawki_cal_zpoint_outputs.mean_atx0 = 0.0 ;
1194  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
1195  if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
1196  {
1197 
1198  hawki_cal_zpoint_outputs.mean_zpoint +=
1199  hawki_cal_zpoint_outputs.zpoint[idet] ;
1200  hawki_cal_zpoint_outputs.mean_atx0 +=
1201  hawki_cal_zpoint_outputs.atx0[idet] ;
1202  nzpoint++;
1203  }
1204  }
1205  if(nzpoint > 0)
1206  {
1207  hawki_cal_zpoint_outputs.zpoint_mean_computable = 1;
1208  hawki_cal_zpoint_outputs.mean_zpoint /= nzpoint ;
1209  hawki_cal_zpoint_outputs.mean_atx0 /= nzpoint ;
1210  }
1211  }
1212 
1213  /* Output results */
1214  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1215  {
1216  if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
1217  {
1218  cpl_msg_info(__func__, "Zero point [at airmass=1] in chip %d:",
1219  idet + 1);
1220  cpl_msg_indent_more();
1221  cpl_msg_info(__func__," ZP: %g [%g]",
1222  hawki_cal_zpoint_outputs.zpoint[idet],
1223  hawki_cal_zpoint_outputs.atx0[idet]);
1224  cpl_msg_info(__func__," Flux of star: %f",
1225  hawki_cal_zpoint_outputs.flux[idet]);
1226  cpl_msg_indent_less();
1227  }
1228  else
1229  cpl_msg_info(__func__, "Zero point not available for chip %d",
1230  idet + 1);
1231 
1232  }
1233 
1234  return zpoint;
1235 }
1236 
1237 /*----------------------------------------------------------------------------*/
1245 /*----------------------------------------------------------------------------*/
1246 static int hawki_cal_zpoint_get_mag(
1247  const char * std_file,
1248  double pointing_ra,
1249  double pointing_dec,
1250  hawki_band band)
1251 {
1252  cpl_table * catalogue;
1253  char * star_name;
1254  char * used_catname;
1255  char * star_type;
1256  double stdstar_ra;
1257  double stdstar_dec;
1258  double mag_filter;
1259  double mag_J;
1260  double mag_H;
1261  double mag_K;
1262  double mag_Y;
1263  int mag_invalid;
1264  int star_ind;
1265 
1266  hawki_cal_zpoint_outputs.stdstar_incat_found = 0;
1267 
1268  /* Load the catalog */
1269  if ((catalogue = irplib_stdstar_load_catalog(std_file, "all")) == NULL) {
1270  cpl_msg_error(cpl_func,"Cannot read catalogue file %s",std_file);
1271  return -1;
1272  }
1273 
1274  /* Check that the columns are there */
1275  if (irplib_stdstar_check_columns_exist(catalogue) != CPL_ERROR_NONE) {
1276  cpl_msg_error(cpl_func,"Note all the columns are present"
1277  " in the catalogue file %s",std_file);
1278  return -1;
1279  }
1280 
1281  /* Select stars within a given distance */
1282  if (irplib_stdstar_select_stars_dist(catalogue,
1283  pointing_ra, pointing_dec, 5.0) == -1) {
1284  cpl_table_delete(catalogue) ;
1285  return 1;
1286  }
1287 
1288  /* Take the closest */
1289  if ((star_ind=irplib_stdstar_find_closest(catalogue,
1290  pointing_ra, pointing_dec)) < 0) {
1291  cpl_table_delete(catalogue) ;
1292  return 1;
1293  }
1294 
1295  /* Retrieve the star information */
1296  hawki_cal_zpoint_outputs.stdstar_incat_found = 1;
1297  star_name = cpl_strdup(cpl_table_get_string(catalogue,
1298  IRPLIB_STDSTAR_STAR_COL, star_ind));
1299  used_catname = cpl_strdup(cpl_table_get_string
1300  (catalogue, IRPLIB_STDSTAR_CAT_COL, star_ind));
1301  star_type = cpl_strdup(cpl_table_get_string(catalogue,
1302  IRPLIB_STDSTAR_TYPE_COL, star_ind));
1303  stdstar_ra = cpl_table_get_double(catalogue,
1304  IRPLIB_STDSTAR_RA_COL, star_ind, NULL);
1305  stdstar_dec = cpl_table_get_double(catalogue,
1306  IRPLIB_STDSTAR_DEC_COL, star_ind, NULL);
1307  mag_filter = cpl_table_get_double(catalogue, hawki_std_band_name(band),
1308  star_ind, &mag_invalid);
1309  mag_H = cpl_table_get_double(catalogue, hawki_std_band_name(HAWKI_BAND_H),
1310  star_ind, NULL);
1311  mag_J = cpl_table_get_double(catalogue, hawki_std_band_name(HAWKI_BAND_J),
1312  star_ind, NULL);
1313  mag_K = cpl_table_get_double(catalogue, hawki_std_band_name(HAWKI_BAND_K),
1314  star_ind, NULL);
1315  mag_Y = cpl_table_get_double(catalogue, hawki_std_band_name(HAWKI_BAND_Y),
1316  star_ind, NULL);
1317 
1318  /* Store results */
1319  strncpy(hawki_cal_zpoint_outputs.starname, star_name, 510);
1320  strncpy(hawki_cal_zpoint_outputs.sptype, star_type, 510);
1321  strncpy(hawki_cal_zpoint_outputs.catalog, used_catname, 510);
1322  hawki_cal_zpoint_outputs.stdstar_ra = stdstar_ra;
1323  hawki_cal_zpoint_outputs.stdstar_dec = stdstar_dec;
1324  if(mag_invalid == 0)
1325  {
1326  hawki_cal_zpoint_outputs.stdstar_mag_available = 1;
1327  hawki_cal_zpoint_outputs.stdstar_mag_filter = mag_filter;
1328  }
1329  else
1330  hawki_cal_zpoint_outputs.stdstar_mag_available = 0;
1331  hawki_cal_zpoint_outputs.stdstar_mag_H = mag_H;
1332  hawki_cal_zpoint_outputs.stdstar_mag_J = mag_J;
1333  hawki_cal_zpoint_outputs.stdstar_mag_K = mag_K;
1334  hawki_cal_zpoint_outputs.stdstar_mag_Y = mag_Y;
1335  cpl_free(star_name);
1336  cpl_free(star_type);
1337  cpl_free(used_catname);
1338  cpl_table_delete(catalogue);
1339 
1340  return 0;
1341 }
1342 
1343 /*----------------------------------------------------------------------------*/
1352 /*----------------------------------------------------------------------------*/
1353 static int hawki_cal_zpoint_save
1354 (cpl_table ** zpoint_tables,
1355  int * labels,
1356  cpl_imagelist * images,
1357  cpl_table ** raw_zpoint_stats,
1358  cpl_frameset * zpoint_frames,
1359  cpl_frameset * calib_frames,
1360  const cpl_frame * stars_frame,
1361  cpl_parameterlist * parlist,
1362  cpl_frameset * set)
1363 {
1364  cpl_propertylist * qcmainparams;
1365  cpl_propertylist ** qcextparams;
1366  cpl_propertylist * mainheader;
1367  cpl_propertylist ** extheaders;
1368  cpl_propertylist ** statsqcextparams;
1369  cpl_propertylist ** statsextheaders;
1370  cpl_frameset * used_frames;
1371  const char * ref_filename;
1372  const char * recipe_name = "hawki_cal_zpoint" ;
1373  int idet;
1374  int iframe;
1375  cpl_errorstate error_prevstate = cpl_errorstate_get();
1376 
1377 
1378  /* Get the reference frame */
1379  ref_filename = hawki_get_extref_file(set);
1380 
1381  /* Create QC parameters for the zpoint table */
1382  qcmainparams = cpl_propertylist_new();
1383  qcextparams = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
1384  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1385  qcextparams[idet] = cpl_propertylist_new();
1386  /* Write QC parameters */
1387  hawki_cal_zpoint_compute_qc(qcmainparams, qcextparams, set);
1388 
1389  /* Create QC parameters for the stats table: Statistics of the raw images */
1390  statsqcextparams = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
1391  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1392  statsqcextparams[idet] = cpl_propertylist_new();
1393  hawki_image_stats_stats(raw_zpoint_stats, statsqcextparams);
1394 
1395  /* Create the main and extension headers for the zpoint table*/
1396  mainheader = cpl_propertylist_new();
1397  extheaders = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
1398  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1399  extheaders[idet] = cpl_propertylist_new();
1400  /* Copy QC params to the headers */
1401  cpl_propertylist_append(mainheader, qcmainparams);
1402  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1403  cpl_propertylist_append(extheaders[idet], qcextparams[idet]) ;
1404 
1405  /* Create the extension headers for the stats table*/
1406  statsextheaders = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
1407  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1408  statsextheaders[idet] = cpl_propertylist_duplicate(statsqcextparams[idet]);
1409 
1410  /* Write additional keywords to the headers */
1411  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1412  {
1413  cpl_propertylist * inputlist;
1414  cpl_propertylist * wcslist;
1415  int this_iframe = -1;
1416  int ext_nb;
1417 
1418  /* Propagate some keywords from input raw frame extensions */
1419  ext_nb=hawki_get_ext_from_detector(ref_filename, idet+1);
1420  inputlist = cpl_propertylist_load_regexp(ref_filename, ext_nb,
1421  HAWKI_HEADER_EXT_FORWARD, 0);
1422  cpl_propertylist_append(extheaders[idet], inputlist);
1423  cpl_propertylist_append(statsextheaders[idet], inputlist);
1424  cpl_propertylist_delete(inputlist);
1425 
1426  /* Propagate WCS keywords */
1427  for(iframe=0; iframe<cpl_frameset_get_size(zpoint_frames); iframe++)
1428  if(labels[iframe] == idet + 1)
1429  this_iframe = iframe;
1430  wcslist = cpl_propertylist_load_regexp
1431  (cpl_frame_get_filename(cpl_frameset_get_frame(zpoint_frames, this_iframe)),
1432  ext_nb, HAWKI_HEADER_WCS, 0);
1433  cpl_propertylist_copy_property_regexp
1434  (extheaders[idet], wcslist, HAWKI_HEADER_WCS, 0);
1435  cpl_propertylist_delete(wcslist);
1436  }
1437 
1438  /* Write the zpoint table */
1439  used_frames = cpl_frameset_duplicate(zpoint_frames);
1440  for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
1441  cpl_frameset_insert(used_frames, cpl_frame_duplicate(
1442  cpl_frameset_get_frame(calib_frames, iframe)));
1443  cpl_frameset_insert(used_frames, cpl_frame_duplicate(stars_frame));
1444  hawki_tables_save(set,
1445  parlist,
1446  used_frames,
1447  (const cpl_table **)zpoint_tables,
1448  recipe_name,
1449  HAWKI_CALPRO_ZPOINT_TAB,
1450  HAWKI_PROTYPE_ZPOINT_TAB,
1451  mainheader,
1452  (const cpl_propertylist **)extheaders,
1453  "hawki_cal_zpoint.fits");
1454  cpl_frameset_delete(used_frames);
1455 
1456  /* Write the table with the raw zpoint objects statistics */
1457  used_frames = cpl_frameset_duplicate(zpoint_frames);
1458  hawki_tables_save(set,
1459  parlist,
1460  used_frames,
1461  (const cpl_table **)raw_zpoint_stats,
1462  recipe_name,
1463  HAWKI_CALPRO_ZPOINT_STATS,
1464  HAWKI_PROTYPE_ZPOINT_STATS,
1465  NULL,
1466  (const cpl_propertylist **)statsextheaders,
1467  "hawki_cal_zpoint_stats.fits");
1468 
1469  /* Write the images */
1470  for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
1471  cpl_frameset_insert(used_frames, cpl_frame_duplicate(
1472  cpl_frameset_get_frame(calib_frames, iframe)));
1474  parlist,
1475  used_frames,
1476  images,
1477  recipe_name,
1478  HAWKI_CALPRO_ZPOINT_IMA,
1479  HAWKI_PROTYPE_ZPOINT_IMA,
1480  NULL,
1481  NULL,
1482  "hawki_cal_zpoint_check.fits") ;
1483  cpl_frameset_delete(used_frames);
1484 
1485  /* Free */
1486  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
1487  cpl_propertylist_delete(extheaders[idet]) ;
1488  cpl_propertylist_delete(qcextparams[idet]) ;
1489  cpl_propertylist_delete(statsqcextparams[idet]);
1490  cpl_propertylist_delete(statsextheaders[idet]);
1491  }
1492  cpl_propertylist_delete(mainheader);
1493  cpl_propertylist_delete(qcmainparams);
1494  cpl_free(extheaders);
1495  cpl_free(qcextparams);
1496  cpl_free(statsqcextparams);
1497  cpl_free(statsextheaders);
1498 
1499  /* Free */
1500  if(!cpl_errorstate_is_equal(error_prevstate))
1501  {
1502  cpl_errorstate_set(CPL_ERROR_NONE);
1503  return -1;
1504  }
1505  return 0;
1506 }
1507 
1508 /*----------------------------------------------------------------------------*/
1515 /*----------------------------------------------------------------------------*/
1516 static int hawki_cal_zpoint_compute_qc
1517 (cpl_propertylist * qcmainparams,
1518  cpl_propertylist ** qcextparams,
1519  cpl_frameset * set)
1520 {
1521  int idet;
1522  int nframes;
1523 
1524 
1525  nframes = cpl_frameset_get_size(set) ;
1526 
1527  /* Check inputs */
1528  if (qcmainparams == NULL) return -1;
1529  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1530  if (qcextparams[idet] == NULL) return -1;
1531 
1532  /* Write the QC params common to all extensions in the main header */
1533  cpl_propertylist_append_string(qcmainparams, "ESO QC FILTER OBS",
1534  hawki_cal_zpoint_outputs.filter);
1535  cpl_propertylist_set_comment(qcmainparams, "ESO QC FILTER OBS",
1536  "Observation filter");
1537  cpl_propertylist_append_string(qcmainparams, "ESO QC FILTER REF",
1538  hawki_std_band_name(hawki_cal_zpoint_outputs.band)) ;
1539  cpl_propertylist_set_comment(qcmainparams, "ESO QC FILTER REF",
1540  "Reference filter");
1541  cpl_propertylist_append_double(qcmainparams, "ESO QC AMBI RHUM AVG",
1542  hawki_cal_zpoint_outputs.humidity);
1543  cpl_propertylist_set_comment(qcmainparams, "ESO QC AMBI RHUM AVG",
1544  "(percent) ambient relative humidity @ 30/2 m");
1545  cpl_propertylist_append_double(qcmainparams, "ESO QC AIRMASS MEAN",
1546  hawki_cal_zpoint_outputs.mean_airmass) ;
1547  cpl_propertylist_set_comment(qcmainparams, "ESO QC AIRMASS MEAN",
1548  "Average airmass");
1549  cpl_propertylist_append_double(qcmainparams, "ESO QC DATANCOM",
1550  nframes);
1551  cpl_propertylist_set_comment(qcmainparams, "ESO QC DATANCOM",
1552  "Number of files used for the reduction");
1553  if(hawki_cal_zpoint_outputs.stdstar_incat_found == 1)
1554  {
1555  cpl_propertylist_append_string(qcmainparams, "ESO QC STDNAME",
1556  hawki_cal_zpoint_outputs.starname) ;
1557  cpl_propertylist_set_comment(qcmainparams, "ESO QC STDNAME",
1558  "Standard star name");
1559  cpl_propertylist_append_string(qcmainparams, "ESO QC SPECTYPE",
1560  hawki_cal_zpoint_outputs.sptype) ;
1561  cpl_propertylist_set_comment(qcmainparams, "ESO QC SPECTYPE",
1562  "Standard star spectral type");
1563  cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG H",
1564  hawki_cal_zpoint_outputs.stdstar_mag_H) ;
1565  cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG H",
1566  "Standard star magnitude in H");
1567  cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG J",
1568  hawki_cal_zpoint_outputs.stdstar_mag_J) ;
1569  cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG J",
1570  "Standard star magnitude in J");
1571  cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG K",
1572  hawki_cal_zpoint_outputs.stdstar_mag_K) ;
1573  cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG K",
1574  "Standard star magnitude in K");
1575  cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG Y",
1576  hawki_cal_zpoint_outputs.stdstar_mag_Y) ;
1577  cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG Y",
1578  "Standard star magnitude in Y");
1579  cpl_propertylist_append_string(qcmainparams, "ESO QC CATNAME",
1580  hawki_cal_zpoint_outputs.catalog) ;
1581  cpl_propertylist_set_comment(qcmainparams, "ESO QC CATNAME",
1582  "Standard star catalogue name");
1583  }
1584  if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
1585  {
1586  cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG",
1587  hawki_cal_zpoint_outputs.stdstar_mag_filter) ;
1588  cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG",
1589  "Standard star magnitude in observed filter");
1590  }
1591  if(hawki_cal_zpoint_outputs.zpoint_mean_computable == 1)
1592  {
1593  cpl_propertylist_append_double(qcmainparams, "ESO QC ZPOINT MEAN",
1594  hawki_cal_zpoint_outputs.mean_zpoint) ;
1595  cpl_propertylist_set_comment(qcmainparams, "ESO QC ZPOINT MEAN",
1596  "Mean measured zero-point for all the chips [mag]");
1597  cpl_propertylist_append_double(qcmainparams, "ESO QC ATX0 MEAN",
1598  hawki_cal_zpoint_outputs.mean_atx0);
1599  cpl_propertylist_set_comment(qcmainparams, "ESO QC ATX0 MEAN",
1600  "Mean extinction corrected zero-point for all the chips [mag]");
1601  cpl_propertylist_append_double(qcmainparams, "ESO QC ZPOINT EXT COEFF",
1602  hawki_cal_zpoint_outputs.ext_coeff);
1603  cpl_propertylist_set_comment(qcmainparams, "ESO QC ZPOINT EXT COEFF",
1604  "Extinction coefficient used in the computation of ATX0");
1605  }
1606 
1607  /* Write QC params that are specific of the extension */
1608  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1609  {
1610  if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
1611  {
1612  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT",
1613  hawki_cal_zpoint_outputs.zpoint[idet]) ;
1614  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT",
1615  "Measured zero-point for a given chip [mag]");
1616  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ATX0",
1617  hawki_cal_zpoint_outputs.atx0[idet]);
1618  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ATX0",
1619  "Extinction corrected zero-point for a given chip [mag]");
1620  }
1621  if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
1622  {
1623  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT POSX",
1624  hawki_cal_zpoint_outputs.posx[idet]);
1625  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT POSX",
1626  "X position of the standard star [pixel]");
1627  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT POSY",
1628  hawki_cal_zpoint_outputs.posy[idet]);
1629  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT POSY",
1630  "Y position of the standard star [pixel]");
1631  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FLUX",
1632  hawki_cal_zpoint_outputs.flux[idet]);
1633  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FLUX",
1634  "Flux of the standard star [ADU]");
1635  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT PEAK",
1636  hawki_cal_zpoint_outputs.peak[idet]) ;
1637  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT PEAK",
1638  "Peak of the standard star [ADU]");
1639  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT BGD",
1640  hawki_cal_zpoint_outputs.bgd[idet]) ;
1641  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT BGD",
1642  "Background around the standard star [ADU]");
1643  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHMX",
1644  hawki_cal_zpoint_outputs.fwhmx[idet]);
1645  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHMX",
1646  "X FWHM of the standard star [pixel]");
1647  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHMY",
1648  hawki_cal_zpoint_outputs.fwhmy[idet]);
1649  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHMY",
1650  "Y FWHM of the standard star [pixel]");
1651  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHM",
1652  hawki_cal_zpoint_outputs.fwhm[idet]);
1653  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHM",
1654  "FWHM of the standard star [pixel]");
1655  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHMX_AS",
1656  hawki_cal_zpoint_outputs.fwhmx_as[idet]);
1657  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHMX_AS",
1658  "X FWHM of the standard star [arcsec]");
1659  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHMY_AS",
1660  hawki_cal_zpoint_outputs.fwhmy_as[idet]);
1661  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHMY_AS",
1662  "Y FWHM of the standard star [arcsec]");
1663  cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHM_AS",
1664  hawki_cal_zpoint_outputs.fwhm_as[idet]);
1665  cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHM_AS",
1666  "FWHM of the standard star [arcsec]");
1667  }
1668  }
1669  return 0;
1670 }
1671 
1672 /*----------------------------------------------------------------------------*/
1678 /*----------------------------------------------------------------------------*/
1679 static int hawki_cal_zpoint_compute_keywords(
1680  cpl_frameset * set,
1681  int * labels)
1682 {
1683  int nframes ;
1684  cpl_vector * hum_vec ;
1685  cpl_frame * cur_frame ;
1686  cpl_propertylist * plist ;
1687  int iframe;
1688 
1689  /* Test inputs */
1690  if (set == NULL) return -1 ;
1691 
1692  /* Initialize */
1693  nframes = cpl_frameset_get_size(set) ;
1694  hawki_cal_zpoint_outputs.mean_airmass = 0.0 ;
1695 
1696  hum_vec = cpl_vector_new(nframes) ;
1697 
1698  for (iframe=0 ; iframe<nframes ; iframe++) {
1699  if (cpl_error_get_code()) {
1700  cpl_vector_delete(hum_vec) ;
1701  return -1 ;
1702  }
1703  cur_frame = cpl_frameset_get_frame(set, iframe) ;
1704  plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
1705  if (iframe==0)
1706  hawki_cal_zpoint_outputs.mean_airmass +=
1708  if (iframe==nframes-1)
1709  hawki_cal_zpoint_outputs.mean_airmass +=
1711  hawki_cal_zpoint_outputs.airmass[labels[iframe] - 1] =
1713  hawki_pfits_get_airmass_end(plist)) / 2.;
1714  cpl_vector_set(hum_vec, iframe, hawki_pfits_get_humidity_level(plist));
1715  cpl_propertylist_delete(plist) ;
1716  if (cpl_error_get_code()) {
1717  cpl_vector_delete(hum_vec) ;
1718  cpl_error_reset() ;
1719  return -1 ;
1720  }
1721  }
1722  hawki_cal_zpoint_outputs.humidity = cpl_vector_get_mean(hum_vec) ;
1723  hawki_cal_zpoint_outputs.mean_airmass /= 2 ;
1724 
1725  /* Free and return */
1726  cpl_vector_delete(hum_vec) ;
1727  if (cpl_error_get_code()) return -1 ;
1728  return 0 ;
1729 }
1730 
1731 static cpl_error_code hawki_cal_zpoint_get_expected_pos
1732 (cpl_frameset * set,
1733  int * labels)
1734 {
1735  const char * filename;
1736  int iframe;
1737 
1738  for(iframe=0 ; iframe<HAWKI_NB_DETECTORS ; iframe++)
1739  {
1740  cpl_propertylist * wcs_plist;
1741  cpl_wcs * wcs;
1742  int idet;
1743 
1744  idet = labels[iframe];
1745  if(hawki_cal_zpoint_config.xcoord[idet - 1] == -1 ||
1746  hawki_cal_zpoint_config.ycoord[idet - 1] == -1)
1747  {
1748  filename = cpl_frame_get_filename
1749  (cpl_frameset_get_frame_const(set, iframe));
1750  wcs_plist = cpl_propertylist_load
1751  (filename, hawki_get_ext_from_detector(filename, idet));
1752  wcs = cpl_wcs_new_from_propertylist(wcs_plist);
1753  cpl_propertylist_delete(wcs_plist);
1754  if(wcs == NULL)
1755  {
1756  cpl_msg_error(__func__, "Could not get WCS info");
1757  cpl_wcs_delete(wcs);
1758  return CPL_ERROR_ILLEGAL_INPUT;
1759  }
1760  if(irplib_wcs_radectoxy(wcs,
1761  hawki_cal_zpoint_outputs.stdstar_ra,
1762  hawki_cal_zpoint_outputs.stdstar_dec,
1763  &(hawki_cal_zpoint_config.xcoord[idet - 1]),
1764  &(hawki_cal_zpoint_config.ycoord[idet - 1]))
1765  != CPL_ERROR_NONE)
1766  {
1767  cpl_msg_error(__func__,"Could not get the expected position of star");
1768  cpl_wcs_delete(wcs);
1769  return CPL_ERROR_UNSPECIFIED;
1770  }
1771  cpl_msg_info(cpl_func,
1772  "Star expected position in detector %d is X=%f Y=%f",
1773  idet, hawki_cal_zpoint_config.xcoord[idet - 1],
1774  hawki_cal_zpoint_config.ycoord[idet - 1]);
1775 
1776  /* Free */
1777  cpl_wcs_delete(wcs);
1778  }
1779  else
1780  {
1781  cpl_msg_info(cpl_func,
1782  "Using given star position in detector %d: X=%f Y=%f",
1783  idet,
1784  hawki_cal_zpoint_config.xcoord[idet - 1],
1785  hawki_cal_zpoint_config.ycoord[idet - 1]);
1786 
1787  }
1788  }
1789 
1790  return 0;
1791 }
1792 
1793 
1794 static void hawki_cal_zpoint_output_init(void)
1795 {
1796  int idet;
1797 
1798  hawki_cal_zpoint_outputs.starname[0] = (char)0 ;
1799  hawki_cal_zpoint_outputs.sptype[0] = (char)0 ;
1800  hawki_cal_zpoint_outputs.filter[0] = (char)0 ;
1801  hawki_cal_zpoint_outputs.catalog[0] = (char)0 ;
1802  hawki_cal_zpoint_outputs.pixscale = -1.0 ;
1803  hawki_cal_zpoint_outputs.dit = -1.0 ;
1804  hawki_cal_zpoint_outputs.humidity = -1.0 ;
1805  hawki_cal_zpoint_outputs.mean_airmass = -1.0 ;
1806  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1807  {
1808  hawki_cal_zpoint_outputs.airmass[idet] = -1.0 ;
1809  hawki_cal_zpoint_outputs.zpoint[idet] = -1.0 ;
1810  hawki_cal_zpoint_outputs.atx0[idet] = -1.0 ;
1811  hawki_cal_zpoint_outputs.posx[idet] = -1.0 ;
1812  hawki_cal_zpoint_outputs.posy[idet] = -1.0 ;
1813  hawki_cal_zpoint_outputs.flux[idet] = -1.0 ;
1814  hawki_cal_zpoint_outputs.peak[idet] = -1.0 ;
1815  hawki_cal_zpoint_outputs.bgd[idet] = -1.0 ;
1816  hawki_cal_zpoint_outputs.fwhmx[idet] = -1.0 ;
1817  hawki_cal_zpoint_outputs.fwhmy[idet] = -1.0 ;
1818  hawki_cal_zpoint_outputs.fwhm[idet] = -1.0 ;
1819  hawki_cal_zpoint_outputs.fwhmx_as[idet] = -1.0 ;
1820  hawki_cal_zpoint_outputs.fwhmy_as[idet] = -1.0 ;
1821  hawki_cal_zpoint_outputs .fwhm_as[idet] = -1.0 ;
1822  }
1823 }
1824 
1825 int hawki_cal_zpoint_retrieve_input_param
1826 (cpl_parameterlist * parlist)
1827 {
1828  cpl_parameter * par ;
1829  const char * sval ;
1830 
1831  par = NULL ;
1832 
1833  /* --ra */
1834  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.ra") ;
1835  hawki_cal_zpoint_config.target_ra = cpl_parameter_get_double(par) ;
1836  /* --dec */
1837  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.dec") ;
1838  hawki_cal_zpoint_config.target_dec = cpl_parameter_get_double(par) ;
1839  /* --mag */
1840  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.mag") ;
1841  hawki_cal_zpoint_config.stdstar_given_magnitude =
1842  cpl_parameter_get_double(par) ;
1843  /* --detect_sigma */
1844  par = cpl_parameterlist_find(parlist,"hawki.hawki_cal_zpoint.detect_sigma");
1845  hawki_cal_zpoint_config.detect_sigma = cpl_parameter_get_double(par) ;
1846  /* --sx */
1847  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.sx") ;
1848  hawki_cal_zpoint_config.sx = cpl_parameter_get_int(par) ;
1849  /* --sy */
1850  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.sy") ;
1851  hawki_cal_zpoint_config.sy = cpl_parameter_get_int(par) ;
1852  /* --star_r */
1853  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.star_r") ;
1854  hawki_cal_zpoint_config.phot_star_radius = cpl_parameter_get_double(par) ;
1855  /* --bg_r1 */
1856  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.bg_r1") ;
1857  hawki_cal_zpoint_config.phot_bg_r1 = cpl_parameter_get_double(par) ;
1858  /* --bg_r2 */
1859  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.bg_r2") ;
1860  hawki_cal_zpoint_config.phot_bg_r2 = cpl_parameter_get_double(par) ;
1861  /* --xcoord */
1862  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.xcoord");
1863  sval = cpl_parameter_get_string(par);
1864  if (sscanf(sval, "%lf,%lf,%lf,%lf",
1865  hawki_cal_zpoint_config.xcoord,
1866  hawki_cal_zpoint_config.xcoord+1,
1867  hawki_cal_zpoint_config.xcoord+2,
1868  hawki_cal_zpoint_config.xcoord+3)!=4)
1869  {
1870  return -1;
1871  }
1872  /* --ycoord */
1873  par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.ycoord");
1874  sval = cpl_parameter_get_string(par);
1875  if (sscanf(sval, "%lf,%lf,%lf,%lf",
1876  hawki_cal_zpoint_config.ycoord,
1877  hawki_cal_zpoint_config.ycoord+1,
1878  hawki_cal_zpoint_config.ycoord+2,
1879  hawki_cal_zpoint_config.ycoord+3)!=4)
1880  {
1881  return -1;
1882  }
1883 
1884  return 0;
1885 }
1886 
1887 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist)
1888 {
1889  if(hawki_pfits_get_targ_epoch(plist) != 2000. ||
1890  hawki_pfits_get_targ_equinox(plist) != 2000.)
1891  {
1892  cpl_msg_error(__func__,"Epoch and equinox must be 2000.");
1893  return -1;
1894  }
1895  else
1896  return 0;
1897 }