IIINSTRUMENT Pipeline Reference Manual  1.5.13
sofi_img_zpoint.c
1 /* $Id: sofi_img_zpoint.c,v 1.30 2013-03-12 08:04:53 llundin Exp $
2  *
3  * This file is part of the SOFI 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: 2013-03-12 08:04:53 $
24  * $Revision: 1.30 $
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 /* FIXME: This recipe was created by s/isaac/sofi/g */
37 
38 #include "irplib_plugin.h"
39 #include "irplib_utils.h"
40 #include "irplib_calib.h"
41 #include "irplib_stdstar.h"
42 #include "irplib_strehl.h"
43 
44 #include "sofi_utils.h"
45 #include "sofi_pfits.h"
46 #include "sofi_dfs.h"
47 
48 #include <string.h>
49 #include <math.h>
50 #include <cpl.h>
51 
52 
53 /*-----------------------------------------------------------------------------
54  Define
55  -----------------------------------------------------------------------------*/
56 
57 #define RECIPE_STRING "sofi_img_zpoint"
58 
59 #define PHOT_STAR_RADIUS 30.0
60 #define PHOT_BACKGROUND_R1 40.0
61 #define PHOT_BACKGROUND_R2 60.0
62 
63 #define DEF_LOCATE_SX 10
64 #define DEF_LOCATE_SY 10
65 
66 /*-----------------------------------------------------------------------------
67  Functions prototypes
68  -----------------------------------------------------------------------------*/
69 
70 static cpl_table * sofi_img_zpoint_reduce(cpl_frameset *, const char *,
71  const char *, const char *, const char *, const char *, cpl_image **);
72 static cpl_imagelist * sofi_img_zpoint_load(cpl_frameset *, const char *,
73  const char *, const char *, const char *);
74 static int sofi_img_zpoint_save(cpl_table *, cpl_image *, cpl_frameset *,
75  const cpl_parameterlist *, cpl_frameset *);
76 static cpl_table * sofi_img_zpoint_photom(cpl_imagelist *, cpl_bivector *);
77 static cpl_error_code sofi_img_zpoint_get_mag(const char *, double, double,
78  sofi_band);
79 static int sofi_img_zpoint_gradients(cpl_vector *, double *, double *,
80  double *, double *);
81 static cpl_bivector * sofi_img_zpoint_get_offsets(cpl_frameset *);
82 static int sofi_img_zpoint_compute_keywords(cpl_frameset *, double *,double *);
83 static cpl_image * sofi_img_zpoint_check_im(cpl_imagelist *,
84  cpl_bivector *, double, double, double);
85 
86 cpl_recipe_define(sofi_img_zpoint, SOFI_BINARY_VERSION,
87  "Lars Lundin", PACKAGE_BUGREPORT, "2008",
88  "SOFI Zero point computation recipe",
89  RECIPE_STRING " -- SOFI Zero point recipe\n"
90  "The files listed in the Set Of Frames (sof-file) must "
91  "be tagged:\n"
92  "raw-file.fits "SOFI_IMG_ZPOINT_RAW" or\n"
93  "stdstars.fits "SOFI_CALIB_STDSTARS" or\n"
94  "flat-file.fits "SOFI_CALIB_FLAT" or\n"
95  "detlin-a-file.fits "SOFI_CALIB_DETLIN_A" or\n"
96  "detlin-b-file.fits "SOFI_CALIB_DETLIN_B" or\n"
97  "detlin-c-file.fits "SOFI_CALIB_DETLIN_C"\n");
98 
99 /*-----------------------------------------------------------------------------
100  Static variables
101  -----------------------------------------------------------------------------*/
102 
103 static struct {
104  /* Inputs */
105  double ra;
106  double dec;
107  double magnitude;
108  int sx;
109  int sy;
110  double phot_star_radius;
111  double phot_bg_r1;
112  double phot_bg_r2;
113  int check_im;
114  /* Outputs */
115  double dit;
116  char filter[512];
117  sofi_band band;
118  char * starname;
119  char * sptype;
120  char * catalog;
121  double zpoint;
122  double zpointrms;
123  double flux_med;
124  double gradx;
125  double grady;
126  double graddx;
127  double graddy;
128  double fwhm_mean;
129 } sofi_img_zpoint_config;
130 
131 /*-----------------------------------------------------------------------------
132  Functions code
133  -----------------------------------------------------------------------------*/
134 
135 /*----------------------------------------------------------------------------*/
143 /*----------------------------------------------------------------------------*/
144 static
145 cpl_error_code sofi_img_zpoint_fill_parameterlist(cpl_parameterlist * self)
146 {
147 
148  const char * context = PACKAGE "." RECIPE_STRING;
149  cpl_error_code err;
150 
151  cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
152 
153  /* Fill the parameters list */
154 
155  /* --star_r */
156  err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
157  "star_r", PHOT_STAR_RADIUS, NULL,
158  context, "The star radius");
159  cpl_ensure_code(!err, err);
160 
161  /* --bg_r1 */
162  err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
163  "bg_r1", PHOT_BACKGROUND_R1, NULL,
164  context, "The internal background "
165  "radius");
166  cpl_ensure_code(!err, err);
167 
168  /* --bg_r2 */
169  err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
170  "bg_r2", PHOT_BACKGROUND_R2, NULL,
171  context, "The external background "
172  "radius");
173  cpl_ensure_code(!err, err);
174 
175  /* --ra */
176  err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
177  "ra", 999.0, NULL, context,
178  "Right Ascension [degrees]");
179  cpl_ensure_code(!err, err);
180 
181  /* --dec */
182  err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
183  "dec", 999.0, NULL, context,
184  "Declination [degrees]");
185  cpl_ensure_code(!err, err);
186 
187  /* --mag */
188  err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
189  "mag", 99.0, NULL, context,
190  "Standard Star Magnitude to use "
191  "instead of catalogue value");
192  cpl_ensure_code(!err, err);
193 
194  /* --sx */
195  err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
196  "sx", DEF_LOCATE_SX, NULL, context,
197  "X-size of the search window");
198  cpl_ensure_code(!err, err);
199 
200  /* --sy */
201  err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
202  "sy", DEF_LOCATE_SY, NULL, context,
203  "Y-size of the search window");
204  cpl_ensure_code(!err, err);
205 
206  /* --check_im */
207  err = irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING,
208  "check_im", CPL_FALSE, NULL, context,
209  "Flag to create the check image");
210  cpl_ensure_code(!err, err);
211 
212  return CPL_ERROR_NONE;
213 }
214 
215 
216 
217 /*----------------------------------------------------------------------------*/
224 /*----------------------------------------------------------------------------*/
225 static int sofi_img_zpoint(cpl_frameset * framelist,
226  const cpl_parameterlist * parlist)
227 {
228  const char * stdstars;
229  const char * flat;
230  const char * detlin_a;
231  const char * detlin_b;
232  const char * detlin_c;
233  cpl_frameset * rawframes = NULL;
234  cpl_table * tab = NULL;
235  cpl_image * check_im = NULL;
236 
237  /* Initialise */
238  sofi_img_zpoint_config.starname = NULL;
239  sofi_img_zpoint_config.sptype = NULL;
240  sofi_img_zpoint_config.filter[0] = (char)0;
241  sofi_img_zpoint_config.catalog = NULL;
242  sofi_img_zpoint_config.gradx = -1.0;
243  sofi_img_zpoint_config.grady = -1.0;
244  sofi_img_zpoint_config.graddx = -1.0;
245  sofi_img_zpoint_config.graddy = -1.0;
246  sofi_img_zpoint_config.fwhm_mean = -1.0;
247 
248  /* Retrieve input parameters */
249  /* --ra */
250  sofi_img_zpoint_config.ra =
251  irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING, "ra");
252 
253  /* --dec */
254  sofi_img_zpoint_config.dec =
255  irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING, "dec");
256 
257  /* --mag */
258  sofi_img_zpoint_config.magnitude =
259  irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING, "mag");
260 
261  /* --sx */
262  sofi_img_zpoint_config.sx =
263  irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING, "sx");
264 
265  /* --sy */
266  sofi_img_zpoint_config.sy =
267  irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING, "sy");
268 
269  /* --star_r */
270  sofi_img_zpoint_config.phot_star_radius =
271  irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
272  "star_r");
273  /* --bg_r1 */
274  sofi_img_zpoint_config.phot_bg_r1 =
275  irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
276  "bg_r1");
277 
278  /* --bg_r2 */
279  sofi_img_zpoint_config.phot_bg_r2 =
280  irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
281  "bg_r2");
282 
283  /* --check_im */
284  sofi_img_zpoint_config.check_im =
285  irplib_parameterlist_get_bool(parlist, PACKAGE, RECIPE_STRING,
286  "check_im");
287 
288  /* Identify the RAW and CALIB frames in the input frameset */
289  skip_if (sofi_dfs_set_groups(framelist));
290 
291 
292  /* Retrieve calibration data */
293  detlin_a = sofi_extract_filename(framelist, SOFI_CALIB_DETLIN_A);
294  detlin_b = sofi_extract_filename(framelist, SOFI_CALIB_DETLIN_B);
295  detlin_c = sofi_extract_filename(framelist, SOFI_CALIB_DETLIN_C);
296  flat = sofi_extract_filename(framelist, SOFI_CALIB_FLAT);
297  stdstars = sofi_extract_filename(framelist, SOFI_CALIB_STDSTARS);
298 
299  /* Retrieve raw frames */
300  rawframes = sofi_extract_frameset(framelist, SOFI_IMG_ZPOINT_RAW);
301  error_if(rawframes == NULL, CPL_ERROR_DATA_NOT_FOUND, "Cannot find "
302  "raw frames in the input list");
303 
304  /* Compute the zpoint values */
305  cpl_msg_info(cpl_func, "Reduce the data");
306  cpl_msg_indent_more();
307  tab = sofi_img_zpoint_reduce(rawframes, stdstars, flat, detlin_a,
308  detlin_b, detlin_c, &check_im);
309  cpl_msg_indent_less();
310  skip_if (tab == NULL);
311 
312  /* Save the products */
313  cpl_msg_info(cpl_func, "Save the paf file");
314  cpl_msg_indent_more();
315  sofi_img_zpoint_save(tab, check_im, rawframes, parlist, framelist);
316  cpl_msg_indent_less();
317  skip_if (0);
318 
319  end_skip;
320 
321  cpl_image_delete(check_im);
322  cpl_frameset_delete(rawframes);
323  cpl_table_delete(tab);
324 
325  cpl_free(sofi_img_zpoint_config.starname);
326  cpl_free(sofi_img_zpoint_config.sptype);
327  cpl_free(sofi_img_zpoint_config.catalog);
328 
329  return cpl_error_get_code();
330 }
331 
332 /*----------------------------------------------------------------------------*/
344 /*----------------------------------------------------------------------------*/
345 static cpl_table * sofi_img_zpoint_reduce(
346  cpl_frameset * set,
347  const char * stdstars,
348  const char * flat,
349  const char * detlin_a,
350  const char * detlin_b,
351  const char * detlin_c,
352  cpl_image ** check_im)
353 {
354  cpl_frame * cur_frame;
355  cpl_propertylist * plist;
356  const char * sval;
357  cpl_imagelist * imlist;
358  cpl_image * ima;
359  int niter;
360  int size_x, size_y;
361  double pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist;
362  cpl_apertures * aperts;
363  cpl_bivector * offsets,
364  * positions;
365  double off_x, off_y;
366  int llx, lly, urx, ury;
367  double val, sqsum, zprms, avg_zp;
368  cpl_table * tab_res;
369  cpl_vector * tmp_vec;
370  int nb_ok;
371  int i;
372 
373  /* Initialise */
374  *check_im = NULL;
375  pos_x_cen = pos_y_cen = -1.0;
376 
377  /* Get the filter name, DIT, RA and DEC */
378  cur_frame = cpl_frameset_get_position(set, 0);
379  plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
380  if ((sval = sofi_pfits_get_filter(plist)) == NULL) {
381  cpl_propertylist_delete(plist);
382  return NULL;
383  } else strcpy(sofi_img_zpoint_config.filter, sval); /* FIXME: Replace */
384  if (sofi_img_zpoint_config.ra > 998.0)
385  sofi_img_zpoint_config.ra = sofi_pfits_get_ra(plist);
386  if (sofi_img_zpoint_config.dec > 998.0)
387  sofi_img_zpoint_config.dec = sofi_pfits_get_dec(plist);
388  sofi_img_zpoint_config.dit = sofi_pfits_get_dit(plist);
389  cpl_propertylist_delete(plist);
390  if (cpl_error_get_code()) {
391  cpl_msg_error(cpl_func, "Cannot get some header informations");
392  return NULL;
393  }
394  cpl_msg_info(cpl_func, "Using star at position: RA = %g; DEC = %g",
395  sofi_img_zpoint_config.ra, sofi_img_zpoint_config.dec);
396 
397  /* Get the band */
398  if ((sofi_img_zpoint_config.band =
399  sofi_get_bbfilter(sofi_img_zpoint_config.filter)) ==
400  SOFI_BAND_UNKNOWN) {
401  cpl_msg_error(cpl_func, "Cannot associate the filter to a BB one");
402  return NULL;
403  }
404 
405  /* Get the standard star information from database */
406  if (sofi_img_zpoint_config.magnitude > 98.0) {
407  cpl_msg_info(cpl_func, "Get the star magnitude");
408  if (sofi_img_zpoint_get_mag(stdstars, sofi_img_zpoint_config.ra,
409  sofi_img_zpoint_config.dec,
410  sofi_img_zpoint_config.band)) {
411  cpl_msg_error(cpl_func, "Cannot get the magnitude from the catalog");
412  return NULL;
413  }
414  }
415  cpl_msg_info(cpl_func, "Star magnitude with filter %s : %g",
416  sofi_img_zpoint_config.filter,
417  sofi_img_zpoint_config.magnitude);
418 
419  /* Load the images */
420  cpl_msg_info(cpl_func, "Load images");
421  if ((imlist = sofi_img_zpoint_load(set, flat, detlin_a, detlin_b,
422  detlin_c)) == NULL) {
423  cpl_msg_error(cpl_func, "Cannot load the images");
424  return NULL;
425  }
426 
427  /* Detect the central object in the first frame */
428  cpl_msg_info(cpl_func, "Detect a bright object in the first frame");
429  size_x = cpl_image_get_size_x(cpl_imagelist_get(imlist, 0));
430  size_y = cpl_image_get_size_y(cpl_imagelist_get(imlist, 0));
431  aperts = cpl_apertures_extract_sigma(cpl_imagelist_get(imlist, 0), 5.0);
432  min_dist = size_x * size_x + size_y * size_y;
433  for (i=0; i<cpl_apertures_get_size(aperts); i++) {
434  pos_x = cpl_apertures_get_centroid_x(aperts, i+1);
435  pos_y = cpl_apertures_get_centroid_y(aperts, i+1);
436  dist = (pos_x-size_x/2)*(pos_x-size_x/2) +
437  (pos_y-size_y/2)*(pos_y-size_y/2);
438  if (dist<min_dist) {
439  min_dist = dist;
440  pos_x_cen = pos_x;
441  pos_y_cen = pos_y;
442  }
443  }
444  cpl_apertures_delete(aperts);
445  if (cpl_error_get_code()) {
446  cpl_msg_error(cpl_func, "Cannot find the central object");
447  cpl_imagelist_delete(imlist);
448  return NULL;
449  }
450  cpl_msg_info(cpl_func,
451  "Bright object position: %g %g", pos_x_cen, pos_y_cen);
452 
453  /* Get the offsets */
454  cpl_msg_info(cpl_func, "Read the offsets in the header");
455  offsets = sofi_img_zpoint_get_offsets(set);
456 
457  /* Deduce the objects positions */
458  niter = cpl_imagelist_get_size(imlist);
459  positions = cpl_bivector_new(niter);
460  for (i=0; i<niter; i++) {
461  off_x = cpl_vector_get(cpl_bivector_get_x(offsets), i) -
462  cpl_vector_get(cpl_bivector_get_x(offsets), 0);
463  off_y = cpl_vector_get(cpl_bivector_get_y(offsets), i) -
464  cpl_vector_get(cpl_bivector_get_y(offsets), 0);
465  pos_x = pos_x_cen + off_x;
466  pos_y = pos_y_cen + off_y;
467  cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
468  cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
469  }
470  cpl_bivector_delete(offsets);
471 
472  /* Refine the positions */
473  cpl_msg_info(cpl_func, "Refine the star positions");
474  for (i=1; i<niter; i++) {
475  pos_x = cpl_vector_get(cpl_bivector_get_x(positions), i);
476  pos_y = cpl_vector_get(cpl_bivector_get_y(positions), i);
477  llx = pos_x - sofi_img_zpoint_config.sx;
478  urx = pos_x + sofi_img_zpoint_config.sx;
479  lly = pos_y - sofi_img_zpoint_config.sy;
480  ury = pos_y + sofi_img_zpoint_config.sy;
481  ima = cpl_imagelist_get(imlist, i);
482  pos_x = cpl_image_get_centroid_x_window(ima, llx, lly, urx, ury);
483  pos_y = cpl_image_get_centroid_y_window(ima, llx, lly, urx, ury);
484  cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
485  cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
486  }
487  if (cpl_error_get_code()) {
488  cpl_msg_error(cpl_func, "Cannot refine the positions");
489  cpl_imagelist_delete(imlist);
490  cpl_bivector_delete(positions);
491  return NULL;
492  }
493 
494  /* Create the check image if requested */
495  if (sofi_img_zpoint_config.check_im) {
496  *check_im = sofi_img_zpoint_check_im(imlist, positions,
497  sofi_img_zpoint_config.phot_star_radius,
498  sofi_img_zpoint_config.phot_bg_r1,
499  sofi_img_zpoint_config.phot_bg_r2);
500  }
501 
502  /* Reduce the images */
503  cpl_msg_info(cpl_func, "Compute the photometry");
504  cpl_msg_indent_more();
505  if ((tab_res = sofi_img_zpoint_photom(imlist, positions)) == NULL) {
506  cpl_msg_error(cpl_func, "Cannot reduce");
507  cpl_bivector_delete(positions);
508  cpl_imagelist_delete(imlist);
509  cpl_image_delete(*check_im);
510  *check_im = NULL;
511  cpl_msg_indent_less();
512  return NULL;
513  }
514  cpl_msg_indent_less();
515  cpl_imagelist_delete(imlist);
516  cpl_bivector_delete(positions);
517 
518  /* Compute the median of the flux */
519  sofi_img_zpoint_config.flux_med =
520  cpl_table_get_column_median(tab_res, "FLUX");
521 
522  /* Get the mean FWHM */
523  sofi_img_zpoint_config.fwhm_mean =
524  cpl_table_get_column_mean(tab_res, "FWHMX");
525  sofi_img_zpoint_config.fwhm_mean +=
526  cpl_table_get_column_mean(tab_res, "FWHMY");
527  sofi_img_zpoint_config.fwhm_mean /= 2.0;
528 
529  /* Compute the flux gradient */
530  tmp_vec = cpl_vector_new(niter);
531  for (i=0; i<niter; i++) {
532  cpl_vector_set(tmp_vec, i,
533  cpl_table_get_double(tab_res, "FLUX", i, NULL));
534  }
535  sofi_img_zpoint_gradients(tmp_vec,
536  &(sofi_img_zpoint_config.gradx),
537  &(sofi_img_zpoint_config.grady),
538  &(sofi_img_zpoint_config.graddx),
539  &(sofi_img_zpoint_config.graddy));
540  cpl_vector_delete(tmp_vec);
541 
542  /* Compute the averages of the results for the zero point */
543  tmp_vec = cpl_vector_new(niter);
544  for (i=0; i<niter; i++) {
545  cpl_vector_set(tmp_vec, i,
546  cpl_table_get_double(tab_res, "ZPOINT", i, NULL));
547  }
548  nb_ok = 0;
549  avg_zp = sqsum = 0.0;
550  cpl_vector_sort(tmp_vec, 1);
551  /* Reject highest and lowest value */
552  for (i=1; i<niter-1; i++) {
553  val = cpl_vector_get(tmp_vec, i);
554  if (val > 0.0) {
555  avg_zp += val;
556  sqsum += val * val;
557  nb_ok ++;
558  }
559  }
560  cpl_vector_delete(tmp_vec);
561  if (nb_ok < 1) {
562  cpl_msg_error(cpl_func, "no valid zpoint measurement: cannot compute");
563  sofi_img_zpoint_config.zpoint = -1.0;
564  sofi_img_zpoint_config.zpointrms = -1.0;
565  } else {
566  avg_zp /= (double)nb_ok;
567  sqsum /= (double)nb_ok;
568  zprms = sqsum - avg_zp * avg_zp;
569  zprms = zprms > 0 ? sqrt(zprms) : 0;
570  sofi_img_zpoint_config.zpoint = avg_zp;
571  sofi_img_zpoint_config.zpointrms = zprms;
572  }
573 
574  /* Print final results */
575  cpl_msg_info(cpl_func, "***** FINAL RESULTS *****");
576  cpl_msg_info(cpl_func, "Zero point : %g", sofi_img_zpoint_config.zpoint);
577  cpl_msg_info(cpl_func, "Zero p. RMS: %g", sofi_img_zpoint_config.zpointrms);
578 
579  return tab_res;
580 }
581 
582 /*----------------------------------------------------------------------------*/
597 /*----------------------------------------------------------------------------*/
598 static cpl_image * sofi_img_zpoint_check_im(
599  cpl_imagelist * imlist,
600  cpl_bivector * positions,
601  double r1,
602  double r2,
603  double r3)
604 {
605  int nima, in_nx, in_ny, nx, ny, box_sz, llx, lly;
606  cpl_image * in_ima;
607  float * pin_ima;
608  cpl_image * out_ima;
609  float * pout_ima;
610  int out_pos, in_pos;
611  double * pos_x;
612  double * pos_y;
613  double dist;
614  int i, j, k;
615 
616  /* Check entries */
617  if (imlist == NULL) return NULL;
618  if (positions == NULL) return NULL;
619  nima = cpl_imagelist_get_size(imlist);
620  if (cpl_bivector_get_size(positions) != nima) return NULL;
621 
622  /* Initialise */
623  in_ima = cpl_imagelist_get(imlist, 0);
624  in_nx = cpl_image_get_size_x(in_ima);
625  in_ny = cpl_image_get_size_y(in_ima);
626  pos_x = cpl_bivector_get_x_data(positions);
627  pos_y = cpl_bivector_get_y_data(positions);
628 
629  /* Create the output image */
630  box_sz = 2 * (int)r3 + 1;
631  nx = nima * box_sz;
632  ny = box_sz;
633  out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
634  pout_ima = cpl_image_get_data_float(out_ima);
635 
636  /* Loop on the input images */
637  for (k=0; k<nima; k++) {
638  /* Get the current input image */
639  in_ima = cpl_imagelist_get(imlist, k);
640  pin_ima = cpl_image_get_data_float(in_ima);
641 
642  /* Get the sub image position */
643  llx = (int)(pos_x[k] - r3);
644  lly = (int)(pos_y[k] - r3);
645 
646  for (i=0; i<box_sz; i++) {
647  for (j=0; j<box_sz; j++) {
648  out_pos = (box_sz * k) + i + j * nx;
649  in_pos = llx + i + (lly+j) * in_nx;
650  if (in_pos >= 0 && in_pos < in_nx*in_ny)
651  pout_ima[out_pos] = pin_ima[in_pos];
652  }
653  }
654  }
655 
656  /* Draw circles */
657  for (i=0; i<box_sz; i++) {
658  for (j=0; j<box_sz; j++) {
659  dist = sqrt((i - (box_sz/2)) * (i - (box_sz/2)) +
660  (j - (box_sz/2)) * (j - (box_sz/2)));
661  if ((fabs(dist-sqrt(r1*r1))) < 0.5) pout_ima[i + j * nx] = 10000;
662  if ((fabs(dist-sqrt(r2*r2))) < 0.5) pout_ima[i + j * nx] = 10000;
663  if ((fabs(dist-sqrt(r3*r3))) < 0.5) pout_ima[i + j * nx] = 10000;
664  }
665  }
666 
667  /* Return */
668  return out_ima;
669 }
670 
671 /*----------------------------------------------------------------------------*/
677 /*----------------------------------------------------------------------------*/
678 static cpl_imagelist * sofi_img_zpoint_load(
679  cpl_frameset * set,
680  const char * flat,
681  const char * detlin_a,
682  const char * detlin_b,
683  const char * detlin_c)
684 
685 {
686  cpl_imagelist * imlist;
687  cpl_imagelist * diffs;
688  cpl_image * flat_im;
689  cpl_image * ima;
690  int i;
691 
692  /* Load the images */
693  if ((imlist = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 1,
694  0)) == NULL) {
695  cpl_msg_error(cpl_func, "Cannot load the images");
696  return NULL;
697  }
698 
699  /* Correct the detector linearity */
700  if (detlin_a && detlin_b && detlin_c) {
701  cpl_msg_info(cpl_func, "Correct for non-linearity");
702  if (sofi_detlin_correct(imlist, detlin_a, detlin_b, detlin_c) == -1) {
703  cpl_msg_error(cpl_func, "Cannot correct for non-linearity");
704  }
705  }
706 
707  /* Divide by the flatfield if one is provided */
708  if (flat) {
709  cpl_msg_info(cpl_func, "Divide by the flat field");
710  flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, 0);
711  if (cpl_imagelist_divide_image(imlist, flat_im) != CPL_ERROR_NONE) {
712  cpl_msg_error(cpl_func, "Cannot divide by flat field");
713  if (flat_im) cpl_image_delete(flat_im);
714  cpl_imagelist_delete(imlist);
715  return NULL;
716  }
717  cpl_image_delete(flat_im);
718  }
719 
720  /* Build the difference image list */
721  diffs = cpl_imagelist_new();
722  for (i=0; i<cpl_imagelist_get_size(imlist)-1; i++) {
723  ima = cpl_image_subtract_create(
724  cpl_imagelist_get(imlist, i),
725  cpl_imagelist_get(imlist, i+1));
726  cpl_imagelist_set(diffs, ima, 2*i);
727  ima = cpl_image_subtract_create(
728  cpl_imagelist_get(imlist, i+1),
729  cpl_imagelist_get(imlist, i));
730  cpl_imagelist_set(diffs, ima, 2*i+1);
731  }
732  cpl_imagelist_delete(imlist);
733  if (cpl_error_get_code()) {
734  cpl_msg_error(cpl_func, "Cannot build the difference images");
735  cpl_imagelist_delete(diffs);
736  return NULL;
737  }
738  return diffs;
739 }
740 
741 /*----------------------------------------------------------------------------*/
750 /*----------------------------------------------------------------------------*/
751 static cpl_table * sofi_img_zpoint_photom(
752  cpl_imagelist * ilist,
753  cpl_bivector * pos)
754 {
755  cpl_table * tab;
756  int nbima;
757  double r, r1, r2, mag, dit;
758  cpl_image * ima;
759  double * pos_x;
760  double * pos_y;
761  double bgd, fl, zp, peak, fwhm_x, fwhm_y;
762  int i;
763 
764  /* Test entries */
765  if (ilist == NULL) return NULL;
766  if (pos == NULL) return NULL;
767 
768  /* Initialise */
769  nbima = cpl_imagelist_get_size(ilist);
770  mag = sofi_img_zpoint_config.magnitude;
771  dit = sofi_img_zpoint_config.dit;
772  r = sofi_img_zpoint_config.phot_star_radius;
773  r1 = sofi_img_zpoint_config.phot_bg_r1;
774  r2 = sofi_img_zpoint_config.phot_bg_r2;
775 
776  /* Create the output table */
777  tab = cpl_table_new(nbima);
778  cpl_table_new_column(tab, "POSX", CPL_TYPE_DOUBLE);
779  cpl_table_new_column(tab, "POSY", CPL_TYPE_DOUBLE);
780  cpl_table_new_column(tab, "ZPOINT", CPL_TYPE_DOUBLE);
781  cpl_table_new_column(tab, "FLUX", CPL_TYPE_DOUBLE);
782  cpl_table_new_column(tab, "PEAK", CPL_TYPE_DOUBLE);
783  cpl_table_new_column(tab, "BGD", CPL_TYPE_DOUBLE);
784  cpl_table_new_column(tab, "FWHMX", CPL_TYPE_DOUBLE);
785  cpl_table_new_column(tab, "FWHMY", CPL_TYPE_DOUBLE);
786 
787  /* Loop on the images */
788  pos_x = cpl_bivector_get_x_data(pos);
789  pos_y = cpl_bivector_get_y_data(pos);
790  for (i=0; i<nbima; i++) {
791  /* Get the current image */
792  ima = cpl_imagelist_get(ilist, i);
793  /* Compute the photometry */
794  /* Background */
795  bgd = irplib_strehl_ring_background(ima, (int)(pos_x[i]),
796  (int)(pos_y[i]), (int)r1, (int)r2, IRPLIB_BG_METHOD_MEDIAN);
797  /* Flux */
798  fl = irplib_strehl_disk_flux(ima,
799  (int)(pos_x[i]), (int)(pos_y[i]), (int)r, bgd);
800  /* Zero Point */
801  zp = mag + 2.5 * log10(fl) - 2.5 * log10(dit);
802  cpl_msg_info(cpl_func, "Zero point nb %d: %g", i+1, zp);
803  /* Peak */
804  peak = cpl_image_get_max_window(ima,
805  (int)(pos_x[i]-5), (int)(pos_y[i]-5),
806  (int)(pos_x[i]+5), (int)(pos_y[i]+5));
807 
808  /* FWHM_X / FWHM_Y */
809  if (cpl_image_get_fwhm(ima, (int)(pos_x[i]), (int)(pos_y[i]),
810  &fwhm_x, &fwhm_y) != CPL_ERROR_NONE) {
811  cpl_msg_debug(cpl_func, "Cannot compute FWHM for image %d", i+1);
812  cpl_error_reset();
813  }
814 
815  /* Put the results in the table */
816  cpl_table_set_double(tab, "POSX", i, pos_x[i]);
817  cpl_table_set_double(tab, "POSY", i, pos_y[i]);
818  cpl_table_set_double(tab, "ZPOINT", i, zp);
819  cpl_table_set_double(tab, "FLUX", i, fl);
820  cpl_table_set_double(tab, "PEAK", i, peak);
821  cpl_table_set_double(tab, "BGD", i, bgd);
822  cpl_table_set_double(tab, "FWHMX", i, fwhm_x);
823  cpl_table_set_double(tab, "FWHMY", i, fwhm_y);
824  }
825  return tab;
826 }
827 
828 /*----------------------------------------------------------------------------*/
829 /*
830  @brief Compute the flux gradients
831  @param flux a vector with 8 fluxes
832  @param gradx the x gradient
833  @param grady the y gradient
834  @param graddx the x gradient error
835  @param graddy the y gradient error
836  @return 0 if ok, -1 otherwise
837  3 2
838  1
839  4 5
840  */
841 /*----------------------------------------------------------------------------*/
842 static int sofi_img_zpoint_gradients(
843  cpl_vector * flux,
844  double * gradx,
845  double * grady,
846  double * graddx,
847  double * graddy)
848 {
849  int nflux;
850  double two, three, four, five, mean;
851  double * pflux;
852 
853  /* Test entries */
854  if (flux == NULL) return CPL_ERROR_UNSPECIFIED;
855 
856  /* Initialise */
857  nflux = cpl_vector_get_size(flux);
858  if (nflux != 8) {
859  *gradx = -1.0;
860  *grady = -1.0;
861  *graddx = -1.0;
862  *graddy = -1.0;
863  return 0;
864  }
865  mean = cpl_vector_get_mean(flux);
866 
867  /* Get the 4 positions fluxes */
868  pflux = cpl_vector_get_data(flux);
869  two = (pflux[1] + pflux[2]) / 2.0;
870  three = (pflux[3] + pflux[4]) / 2.0;
871  four = (pflux[5] + pflux[6]) / 2.0;
872  five = pflux[7];
873 
874  *gradx = (two + five - three - four) / 2;
875  *grady = (three + two - four - five) / 2;
876  *graddx = sqrt((two-five)*(two-five) + (three-four)*(three-four))/2.0;
877  *graddy = sqrt((three-two)*(three-two) + (four-five)*(four-five))/2.0;
878 
879  *gradx /= mean;
880  *grady /= mean;
881  *graddx /= mean;
882  *graddy /= mean;
883 
884  return 0;
885 }
886 
887 /*----------------------------------------------------------------------------*/
893 /*----------------------------------------------------------------------------*/
894 static cpl_bivector * sofi_img_zpoint_get_offsets(cpl_frameset * set)
895 {
896  int nframes;
897  int noffsets;
898  cpl_bivector * offs;
899  cpl_frame * cur_frame;
900  cpl_propertylist * plist;
901  double off_x, off_y;
902  int i;
903 
904  /* Test entries */
905  if (set == NULL) return NULL;
906 
907  /* Initialize */
908  nframes = cpl_frameset_get_size(set);
909 
910  noffsets = 2 * (nframes-1);
911 
912  /* Check error code */
913  if (cpl_error_get_code()) return NULL;
914 
915  /* Create the bivector */
916  offs = cpl_bivector_new(noffsets);
917 
918  /* Fill it */
919  for (i=0; i<nframes; i++) {
920  cur_frame = cpl_frameset_get_position(set, i);
921  plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),0);
922  off_x = sofi_pfits_get_cumoffsetx(plist);
923  off_y = sofi_pfits_get_cumoffsety(plist);
924  cpl_propertylist_delete(plist);
925  if (i==0) {
926  cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
927  cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
928  } else if (i==nframes-1) {
929  cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
930  cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
931  } else {
932  cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
933  cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
934  cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
935  cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
936  }
937  }
938 
939  /* Check if it went ok */
940  if (cpl_error_get_code()) {
941  cpl_bivector_delete(offs);
942  cpl_msg_error(cpl_func, "Cannot find offsets in headers");
943  return NULL;
944  }
945  return offs;
946 }
947 
948 
949 /*----------------------------------------------------------------------------*/
957 /*----------------------------------------------------------------------------*/
958 static cpl_error_code sofi_img_zpoint_get_mag(const char * stdstars,
959  double ra,
960  double dec,
961  sofi_band band)
962 {
963  cpl_errorstate prestate = cpl_errorstate_get();
964 
965  bug_if(0);
966  bug_if(stdstars == NULL);
967 
968  switch (band) {
969  /* SW mode */
970  case SOFI_BAND_J:
971  case SOFI_BAND_H:
972  case SOFI_BAND_K:
973  case SOFI_BAND_KS:
974  if (!irplib_stdstar_find_star(stdstars, ra, dec,
975  sofi_std_band_name(band),
976  "LCO-Palomar.txt",
977  &sofi_img_zpoint_config.magnitude,
978  &sofi_img_zpoint_config.starname,
979  &sofi_img_zpoint_config.sptype,
980  &sofi_img_zpoint_config.catalog,
981  NULL, NULL, 2.0) ||
982  !irplib_stdstar_find_star(stdstars, ra, dec,
983  sofi_std_band_name(band),
984  "LCO-Palomar-NICMOS-Red-Stars.txt",
985  &sofi_img_zpoint_config.magnitude,
986  &sofi_img_zpoint_config.starname,
987  &sofi_img_zpoint_config.sptype,
988  &sofi_img_zpoint_config.catalog,
989  NULL, NULL, 2.0) ||
990  !irplib_stdstar_find_star(stdstars, ra, dec,
991  sofi_std_band_name(band),
992  "all",
993  &sofi_img_zpoint_config.magnitude,
994  &sofi_img_zpoint_config.starname,
995  &sofi_img_zpoint_config.sptype,
996  &sofi_img_zpoint_config.catalog,
997  NULL, NULL, 2.0) ||
998  /* Special case: swap K and Ks if needed */
999  (band == SOFI_BAND_K &&
1000  !irplib_stdstar_find_star(stdstars, ra, dec,
1001  sofi_std_band_name(SOFI_BAND_KS),
1002  "all",
1003  &sofi_img_zpoint_config.magnitude,
1004  &sofi_img_zpoint_config.starname,
1005  &sofi_img_zpoint_config.sptype,
1006  &sofi_img_zpoint_config.catalog,
1007  NULL, NULL, 2.0)) ||
1008  (band == SOFI_BAND_KS &&
1009  !irplib_stdstar_find_star(stdstars, ra, dec,
1010  sofi_std_band_name(SOFI_BAND_K),
1011  "all",
1012  &sofi_img_zpoint_config.magnitude,
1013  &sofi_img_zpoint_config.starname,
1014  &sofi_img_zpoint_config.sptype,
1015  &sofi_img_zpoint_config.catalog,
1016  NULL, NULL, 2.0))) {
1017  cpl_errorstate_set(prestate);
1018  } else {
1019  skip_if(1);
1020  }
1021  break;
1022  /* LW mode */
1023  case SOFI_BAND_L:
1024  case SOFI_BAND_M:
1025  if (!irplib_stdstar_find_star(stdstars, ra, dec,
1026  sofi_std_band_name(band),
1027  "ESO-VanDerBliek.txt",
1028  &sofi_img_zpoint_config.magnitude,
1029  &sofi_img_zpoint_config.starname,
1030  &sofi_img_zpoint_config.sptype,
1031  &sofi_img_zpoint_config.catalog,
1032  NULL, NULL, 2.0) ||
1033  !irplib_stdstar_find_star(stdstars, ra, dec,
1034  sofi_std_band_name(band),
1035  "MSSSO-Photometric.txt",
1036  &sofi_img_zpoint_config.magnitude,
1037  &sofi_img_zpoint_config.starname,
1038  &sofi_img_zpoint_config.sptype,
1039  &sofi_img_zpoint_config.catalog,
1040  NULL, NULL, 2.0) ||
1041  !irplib_stdstar_find_star(stdstars, ra, dec,
1042  sofi_std_band_name(band),
1043  "MSSSO-Spectroscopic.txt",
1044  &sofi_img_zpoint_config.magnitude,
1045  &sofi_img_zpoint_config.starname,
1046  &sofi_img_zpoint_config.sptype,
1047  &sofi_img_zpoint_config.catalog,
1048  NULL, NULL, 2.0) ||
1049  !irplib_stdstar_find_star(stdstars, ra, dec,
1050  sofi_std_band_name(band), "all",
1051  &sofi_img_zpoint_config.magnitude,
1052  &sofi_img_zpoint_config.starname,
1053  &sofi_img_zpoint_config.sptype,
1054  &sofi_img_zpoint_config.catalog,
1055  NULL, NULL, 2.0)) {
1056  cpl_errorstate_set(prestate);
1057  } else {
1058  skip_if(1);
1059  }
1060  break;
1061  default:
1062  cpl_msg_error(cpl_func, "cannot determine associated filter");
1063  skip_if(1);
1064  }
1065 
1066  end_skip;
1067 
1068  return cpl_error_get_code();
1069 }
1070 
1071 /*----------------------------------------------------------------------------*/
1081 /*----------------------------------------------------------------------------*/
1082 static int sofi_img_zpoint_save(
1083  cpl_table * tab,
1084  cpl_image * check_im,
1085  cpl_frameset * raw,
1086  const cpl_parameterlist * parlist,
1087  cpl_frameset * set)
1088 {
1089  cpl_propertylist * plist;
1090  cpl_propertylist * paflist;
1091  cpl_propertylist * qclist;
1092  const cpl_frame * ref_frame;
1093  const char * sval;
1094  double hum = 0.0, airm;
1095 
1096  /* Get the QC params in qclist */
1097  qclist = cpl_propertylist_new();
1098 
1099  /* Get the reference frame */
1100  ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
1101  if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
1102  0)) == NULL) {
1103  cpl_msg_error(cpl_func, "getting header from reference frame");
1104  cpl_propertylist_delete(qclist);
1105  return CPL_ERROR_UNSPECIFIED;
1106  }
1107  /* Test the status */
1108  if (cpl_error_get_code()) {
1109  cpl_propertylist_delete(qclist);
1110  cpl_propertylist_delete(plist);
1111  return CPL_ERROR_UNSPECIFIED;
1112  }
1113  sval = sofi_pfits_get_filter(plist);
1114  if (cpl_error_get_code()) cpl_error_reset();
1115  else cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", sval);
1116  cpl_propertylist_append_string(qclist, "ESO QC FILTER REF",
1117  sofi_std_band_name(sofi_img_zpoint_config.band));
1118  if (sofi_img_zpoint_compute_keywords(raw, &hum, &airm) != -1) {
1119  cpl_propertylist_append_double(qclist, "ESO QC AMBI RHUM AVG", hum);
1120  cpl_propertylist_append_double(qclist, "ESO QC AIRMASS", airm);
1121  }
1122  cpl_propertylist_delete(plist);
1123  cpl_propertylist_append_double(qclist, "ESO QC ZPOINT",
1124  sofi_img_zpoint_config.zpoint);
1125  cpl_propertylist_append_double(qclist, "ESO QC ZPOINTRMS",
1126  sofi_img_zpoint_config.zpointrms);
1127  cpl_propertylist_append_double(qclist, "ESO QC FLUX MED",
1128  sofi_img_zpoint_config.flux_med);
1129  cpl_propertylist_append_string(qclist, "ESO QC STDNAME",
1130  sofi_img_zpoint_config.starname);
1131  cpl_propertylist_append_string(qclist, "ESO QC SPECTYPE",
1132  sofi_img_zpoint_config.sptype);
1133  cpl_propertylist_append_double(qclist, "ESO QC STARMAG",
1134  sofi_img_zpoint_config.magnitude);
1135  cpl_propertylist_append_string(qclist, "ESO QC CATNAME",
1136  sofi_img_zpoint_config.catalog);
1137  cpl_propertylist_append_double(qclist, "ESO QC GRADX",
1138  sofi_img_zpoint_config.gradx);
1139  cpl_propertylist_append_double(qclist, "ESO QC GRADY",
1140  sofi_img_zpoint_config.grady);
1141  cpl_propertylist_append_double(qclist, "ESO QC GRADDX",
1142  sofi_img_zpoint_config.graddx);
1143  cpl_propertylist_append_double(qclist, "ESO QC GRADDY",
1144  sofi_img_zpoint_config.graddy);
1145  cpl_propertylist_append_double(qclist, "ESO QC FWHM MEAN",
1146  sofi_img_zpoint_config.fwhm_mean);
1147 
1148  /* Write the zpoint table */
1149  irplib_dfs_save_table(set,
1150  parlist,
1151  set,
1152  tab,
1153  NULL,
1154  "sofi_img_zpoint",
1155  SOFI_IMG_ZPOINT_TAB,
1156  qclist,
1157  NULL,
1158  PACKAGE "/" PACKAGE_VERSION,
1159  "sofi_img_zpoint.fits");
1160 
1161  /* Write the check image */
1162  if (check_im) {
1163  irplib_dfs_save_image(set,
1164  parlist,
1165  set,
1166  check_im,
1167  CPL_BPP_IEEE_FLOAT,
1168  "sofi_img_zpoint",
1169  SOFI_IMG_ZPOINT_CHECK,
1170  qclist,
1171  NULL,
1172  PACKAGE "/" PACKAGE_VERSION,
1173  "sofi_img_zpoint_check.fits");
1174  }
1175 
1176  /* Get the reference frame */
1177  ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
1178 
1179  /* Get FITS header from reference file */
1180  if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
1181  0)) == NULL) {
1182  cpl_msg_error(cpl_func, "getting header from reference frame");
1183  cpl_propertylist_delete(qclist);
1184  return CPL_ERROR_UNSPECIFIED;
1185  }
1186 
1187  /* Get the keywords for the paf file */
1188  paflist = cpl_propertylist_new();
1189  cpl_propertylist_copy_property_regexp(paflist, plist,
1190  "^(ARCFILE|ESO TPL ID|DATE-OBS|MJD-OBS|ESO TEL AIRM START|ESO INS MODE|"
1191  "ESO OBS ID|ESO DET DIT|ESO INS PIXSCALE|RA|DEC)$", 0);
1192  cpl_propertylist_delete(plist);
1193 
1194  /* Get the QC params in qclist and keys for paf in paflist */
1195  cpl_propertylist_copy_property_regexp(paflist, qclist, "ESO QC", 0);
1196  cpl_propertylist_delete(qclist);
1197 
1198  /* Save the PAF file */
1199  cpl_dfs_save_paf("SOFI",
1200  "sofi_img_zpoint",
1201  paflist,
1202  "sofi_img_zpoint.paf");
1203  cpl_propertylist_delete(paflist);
1204  return 0;
1205 }
1206 
1207 /*----------------------------------------------------------------------------*/
1213 /*----------------------------------------------------------------------------*/
1214 static int sofi_img_zpoint_compute_keywords(
1215  cpl_frameset * set,
1216  double * hum,
1217  double * airm)
1218 {
1219  int nframes;
1220  cpl_vector * hum_vec;
1221  cpl_frame * cur_frame;
1222  cpl_propertylist * plist;
1223  int i;
1224 
1225  /* Test inputs */
1226  if (set == NULL) return CPL_ERROR_UNSPECIFIED;
1227 
1228  /* Initialize */
1229  nframes = cpl_frameset_get_size(set);
1230  *airm = 0.0;
1231 
1232  hum_vec = cpl_vector_new(nframes);
1233 
1234  for (i=0; i<nframes; i++) {
1235  if (cpl_error_get_code()) {
1236  cpl_vector_delete(hum_vec);
1237  return CPL_ERROR_UNSPECIFIED;
1238  }
1239  cur_frame = cpl_frameset_get_position(set, i);
1240  plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
1241  if (i==0) *airm += sofi_pfits_get_airmass_start(plist);
1242  if (i==nframes-1) *airm += sofi_pfits_get_airmass_end(plist);
1243  cpl_vector_set(hum_vec, i, sofi_pfits_get_humidity_level(plist));
1244  cpl_propertylist_delete(plist);
1245  if (cpl_error_get_code()) {
1246  cpl_vector_delete(hum_vec);
1247  cpl_error_reset();
1248  return CPL_ERROR_UNSPECIFIED;
1249  }
1250  }
1251  *hum = cpl_vector_get_mean(hum_vec);
1252  *airm /= 2;
1253 
1254  /* Free and return */
1255  cpl_vector_delete(hum_vec);
1256  if (cpl_error_get_code()) return CPL_ERROR_UNSPECIFIED;
1257  return 0;
1258 }
1259 
1260 
sofi_detlin_correct
int sofi_detlin_correct(cpl_imagelist *ilist, const char *detlin_a, const char *detlin_b, const char *detlin_c)
Apply the detector linearity correction.
Definition: sofi_utils.c:363
sofi_pfits_get_ra
double sofi_pfits_get_ra(const cpl_propertylist *plist)
find out the RA
Definition: sofi_pfits.c:461
sofi_pfits_get_dec
double sofi_pfits_get_dec(const cpl_propertylist *plist)
find out the DEC
Definition: sofi_pfits.c:170
sofi_pfits_get_dit
double sofi_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
Definition: sofi_pfits.c:195
sofi_std_band_name
const char * sofi_std_band_name(sofi_band band)
Return a band name.
Definition: sofi_utils.c:266
sofi_extract_frameset
cpl_frameset * sofi_extract_frameset(const cpl_frameset *in, const char *tag)
Extract the frames with the given tag from a frameset.
Definition: sofi_utils.c:298
sofi_dfs_set_groups
int sofi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: sofi_dfs.c:60
sofi_pfits_get_airmass_start
double sofi_pfits_get_airmass_start(const cpl_propertylist *plist)
find out airmass start
Definition: sofi_pfits.c:61
sofi_pfits_get_cumoffsety
double sofi_pfits_get_cumoffsety(const cpl_propertylist *plist)
find out the cumulative offset in Y
Definition: sofi_pfits.c:122
sofi_pfits_get_humidity_level
double sofi_pfits_get_humidity_level(const cpl_propertylist *plist)
find out the humidity level
Definition: sofi_pfits.c:265
sofi_pfits_get_airmass_end
double sofi_pfits_get_airmass_end(const cpl_propertylist *plist)
find out airmass end
Definition: sofi_pfits.c:73
sofi_get_bbfilter
sofi_band sofi_get_bbfilter(char *f)
Get the broad band filter.
Definition: sofi_utils.c:233
sofi_extract_filename
const char * sofi_extract_filename(const cpl_frameset *in, const char *tag)
Extract the filename ffor the first frame of the given tag.
Definition: sofi_utils.c:342
sofi_pfits_get_cumoffsetx
double sofi_pfits_get_cumoffsetx(const cpl_propertylist *plist)
find out the cumulative offset in X
Definition: sofi_pfits.c:110
sofi_pfits_get_filter
const char * sofi_pfits_get_filter(const cpl_propertylist *plist)
find out which wave band is active in short wavelength
Definition: sofi_pfits.c:243