IIINSTRUMENT Pipeline Reference Manual  1.5.13
sofi_utils.c
1 /* $Id: sofi_utils.c,v 1.17 2013-03-12 08:04:54 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:54 $
24  * $Revision: 1.17 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <math.h>
37 #include <string.h>
38 #include <cpl.h>
39 
40 #include "sofi_utils.h"
41 #include "sofi_pfits.h"
42 
43 /*----------------------------------------------------------------------------*/
47 /*----------------------------------------------------------------------------*/
48 
51 /*----------------------------------------------------------------------------*/
59 /*----------------------------------------------------------------------------*/
60 const char * sofi_get_license(void)
61 {
62  const char * sofi_license =
63  "This file is part of the SOFI Instrument Pipeline\n"
64  "Copyright (C) 2002,2003 European Southern Observatory\n"
65  "\n"
66  "This program is free software; you can redistribute it and/or modify\n"
67  "it under the terms of the GNU General Public License as published by\n"
68  "the Free Software Foundation; either version 2 of the License, or\n"
69  "(at your option) any later version.\n"
70  "\n"
71  "This program is distributed in the hope that it will be useful,\n"
72  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
73  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
74  "GNU General Public License for more details.\n"
75  "\n"
76  "You should have received a copy of the GNU General Public License\n"
77  "along with this program; if not, write to the Free Software\n"
78  "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, \n"
79  "MA 02111-1307 USA" ;
80  return sofi_license ;
81 }
82 
83 /*----------------------------------------------------------------------------*/
90 /*----------------------------------------------------------------------------*/
91 int sofi_correct_crosstalk_list(cpl_imagelist * ilist)
92 {
93  cpl_image * ima ;
94  int nima ;
95  int i ;
96 
97  /* Test entries */
98  if (ilist == NULL) return -1 ;
99  ima = cpl_imagelist_get(ilist, 0) ;
100  if (cpl_image_get_type(ima) != CPL_TYPE_FLOAT) return -1 ;
101 
102  /* Initialise */
103  nima = cpl_imagelist_get_size(ilist) ;
104 
105  /* Loop on the images */
106  for (i=0 ; i<nima ; i++) {
107  ima = cpl_imagelist_get(ilist, i) ;
108  if (sofi_correct_crosstalk(ima) == -1) {
109  cpl_msg_error(cpl_func, "Cannot correct crosstalk in image %d", i+1) ;
110  return -1 ;
111  }
112  }
113  return 0 ;
114 }
115 
116 /*----------------------------------------------------------------------------*/
123 /*----------------------------------------------------------------------------*/
124 int sofi_correct_crosstalk(cpl_image * ima)
125 {
126  int nx, ny ;
127  cpl_image * collapsed ;
128  cpl_image * corr ;
129  float * pcorr ;
130  float * pcollapsed ;
131  float * pima ;
132  float val ;
133  int i, j, k ;
134 
135  /* Test entries */
136  if (ima == NULL) return -1 ;
137  if (cpl_image_get_type(ima) != CPL_TYPE_FLOAT) return -1 ;
138 
139  /* Initialise */
140  nx = cpl_image_get_size_x(ima) ;
141  ny = cpl_image_get_size_y(ima) ;
142 
143  /* Collapse the input image */
144  collapsed = cpl_image_new(1, ny, CPL_TYPE_FLOAT) ;
145  pcollapsed = cpl_image_get_data_float(collapsed) ;
146  pima = cpl_image_get_data_float(ima) ;
147  for (j=0 ; j<ny ; j++) {
148  val = 0.0 ;
149  for (i=0 ; i<nx ; i++) val += pima[i+j*nx] ;
150  pcollapsed[j] = val * 1.4e-5 ;
151  }
152 
153  /* Upper part plus lower part */
154  corr = cpl_image_new(1, ny, CPL_TYPE_FLOAT) ;
155  pcorr = cpl_image_get_data_float(corr) ;
156  for (j=0 ; j<ny ; j++) {
157  if (j < ny/2) k = j + ny/2 ;
158  else k = j - ny/2 ;
159  pcorr[j] = pcollapsed[j] + pcollapsed[k] ;
160  }
161  cpl_image_delete(collapsed) ;
162 
163  /* Subtract to the input image */
164  pima = cpl_image_get_data_float(ima) ;
165  for (j=0 ; j<ny ; j++) {
166  for (i=0 ; i<nx ; i++) {
167  pima[i+j*nx] -= pcorr[j] ;
168  }
169  }
170  cpl_image_delete(corr) ;
171  return 0 ;
172 }
173 
174 /*----------------------------------------------------------------------------*/
180 /*----------------------------------------------------------------------------*/
181 cpl_bivector * sofi_get_offsets(cpl_frameset * fset)
182 {
183  cpl_bivector * offsets ;
184  double * offsets_x ;
185  double * offsets_y ;
186  cpl_frame * frame ;
187  cpl_propertylist * plist ;
188  int nfiles ;
189  int i ;
190 
191  /* Test entries */
192  if (fset == NULL) return NULL ;
193 
194  /* Create the offsets bi vector */
195  nfiles = cpl_frameset_get_size(fset) ;
196  offsets = cpl_bivector_new(nfiles) ;
197  offsets_x = cpl_bivector_get_x_data(offsets) ;
198  offsets_y = cpl_bivector_get_y_data(offsets) ;
199  for (i=0 ; i<nfiles ; i++) {
200  if (cpl_error_get_code()) {
201  cpl_bivector_delete(offsets) ;
202  return NULL ;
203  }
204  /* X and Y offsets */
205  frame = cpl_frameset_get_position(fset, i) ;
206  plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0);
207  offsets_x[i] = sofi_pfits_get_cumoffsetx(plist) ;
208  offsets_y[i] = sofi_pfits_get_cumoffsety(plist) ;
209  cpl_propertylist_delete(plist) ;
210  if (cpl_error_get_code()) {
211  cpl_msg_error(cpl_func, "Cannot get offsets from header") ;
212  cpl_bivector_delete(offsets) ;
213  return NULL ;
214  }
215  }
216  /* Subtract the first offset to all offsets */
217  for (i=1 ; i<nfiles ; i++) {
218  offsets_x[i] -= offsets_x[0] ;
219  offsets_y[i] -= offsets_y[0] ;
220  }
221  offsets_x[0] = offsets_y[0] = 0.00 ;
222 
223  return offsets ;
224 }
225 
226 /*----------------------------------------------------------------------------*/
232 /*----------------------------------------------------------------------------*/
233 sofi_band sofi_get_bbfilter(char * f)
234 {
235  if (!strcmp(f, "NB_2.195")) return SOFI_BAND_KS ;
236  if (!strcmp(f, "NB_2.09")) return SOFI_BAND_KS ;
237  if (!strcmp(f, "NB_1.187")) return SOFI_BAND_J ;
238  if (!strcmp(f, "NB_1.061")) return SOFI_BAND_J ;
239  if (!strcmp(f, "Z")) return SOFI_BAND_J ;
240  if (!strcmp(f, "Js")) return SOFI_BAND_J ;
241  if (!strcmp(f, "J")) return SOFI_BAND_J ;
242  if (!strcmp(f, "H")) return SOFI_BAND_H ;
243  if (!strcmp(f, "Ks")) return SOFI_BAND_KS ;
244  if (!strcmp(f, "NB_2.28")) return SOFI_BAND_KS ;
245  if (!strcmp(f, "NB_2.248")) return SOFI_BAND_KS ;
246  if (!strcmp(f, "NB_HeI_J")) return SOFI_BAND_J ;
247  if (!strcmp(f, "NB_1.215")) return SOFI_BAND_J ;
248  if (!strcmp(f, "NB_FeII_J")) return SOFI_BAND_J ;
249  if (!strcmp(f, "NB_Pbeta")) return SOFI_BAND_J ;
250  if (!strcmp(f, "NB_FeII_H")) return SOFI_BAND_H ;
251  if (!strcmp(f, "NB_1.71")) return SOFI_BAND_H ;
252  if (!strcmp(f, "NB_HeI_K")) return SOFI_BAND_KS ;
253  if (!strcmp(f, "NB_H2_S1")) return SOFI_BAND_KS ;
254  if (!strcmp(f, "NB_Brgamma")) return SOFI_BAND_KS ;
255  if (!strcmp(f, "NB_CO")) return SOFI_BAND_KS ;
256  return SOFI_BAND_UNKNOWN ;
257 }
258 
259 /*-------------------------------------------------------------------------*/
265 /*--------------------------------------------------------------------------*/
266 const char * sofi_std_band_name(sofi_band band)
267 {
268  switch (band) {
269  case SOFI_BAND_J: return "J" ;
270  case SOFI_BAND_JS: return "Js" ;
271  case SOFI_BAND_JBLOCK: return "J+Block" ;
272  case SOFI_BAND_H: return "H" ;
273  case SOFI_BAND_K: return "K" ;
274  case SOFI_BAND_KS: return "Ks" ;
275  case SOFI_BAND_L: return "L" ;
276  case SOFI_BAND_M: return "M" ;
277  case SOFI_BAND_LP: return "Lp" ;
278  case SOFI_BAND_MP: return "Mp" ;
279  case SOFI_BAND_Z: return "Z" ;
280  case SOFI_BAND_SZ: return "SZ" ;
281  case SOFI_BAND_SH: return "SH" ;
282  case SOFI_BAND_SK: return "SK" ;
283  case SOFI_BAND_SL: return "SL" ;
284  default: return "Unknown" ;
285  }
286 }
287 
288 /*----------------------------------------------------------------------------*/
297 /*----------------------------------------------------------------------------*/
298 cpl_frameset * sofi_extract_frameset(
299  const cpl_frameset * in,
300  const char * tag)
301 {
302  cpl_frameset * out ;
303  const cpl_frame * cur_frame ;
304  cpl_frame * loc_frame ;
305  int nbframes, nbext ;
306  int i ;
307 
308  /* Test entries */
309  if (in == NULL) return NULL ;
310  if (tag == NULL) return NULL ;
311 
312  /* Initialise */
313  nbframes = cpl_frameset_get_size(in) ;
314 
315  /* Count the frames with the tag */
316  if ((nbext = cpl_frameset_count_tags(in, tag)) == 0) return NULL ;
317 
318  /* Create the output frameset */
319  out = cpl_frameset_new() ;
320 
321  /* Loop on the requested frames and store them in out */
322  nbext = 0 ;
323  for (i=0 ; i<nbframes ; i++) {
324  cur_frame = cpl_frameset_get_position_const(in, i) ;
325  if (!strcmp(cpl_frame_get_tag(cur_frame), tag)) {
326  loc_frame = cpl_frame_duplicate(cur_frame) ;
327  cpl_frameset_insert(out, loc_frame) ;
328  nbext ++ ;
329  }
330  }
331  return out ;
332 }
333 
334 /*----------------------------------------------------------------------------*/
341 /*----------------------------------------------------------------------------*/
343  const cpl_frameset * in,
344  const char * tag)
345 {
346  const cpl_frame * cur_frame ;
347 
348  /* Get the frame */
349  if ((cur_frame = cpl_frameset_find_const(in, tag)) == NULL) return NULL ;
350  return cpl_frame_get_filename(cur_frame) ;
351 }
352 
353 /*----------------------------------------------------------------------------*/
362 /*----------------------------------------------------------------------------*/
364  cpl_imagelist * ilist,
365  const char * detlin_a,
366  const char * detlin_b,
367  const char * detlin_c)
368 {
369  cpl_image * ima ;
370  cpl_image * imb ;
371  cpl_image * imc ;
372  float * pima ;
373  float * pimb ;
374  float * pimc ;
375  float * pdata ;
376  int nx, ny, ni ;
377  float val, val2, val3 ;
378  int i, j ;
379 
380  /* Test entries */
381  if (!ilist || !detlin_a || !detlin_b || !detlin_c) return -1 ;
382 
383  /* Load the 3 coeffs images */
384  ima = cpl_image_load(detlin_a, CPL_TYPE_FLOAT, 0, 0) ;
385  imb = cpl_image_load(detlin_b, CPL_TYPE_FLOAT, 0, 0) ;
386  imc = cpl_image_load(detlin_c, CPL_TYPE_FLOAT, 0, 0) ;
387  if (!ima || !imb || !imc) {
388  cpl_msg_error(cpl_func, "Cannot load the detlin images") ;
389  if (ima) cpl_image_delete(ima) ;
390  if (imb) cpl_image_delete(imb) ;
391  if (imc) cpl_image_delete(imc) ;
392  return -1 ;
393  }
394  pima = cpl_image_get_data_float(ima) ;
395  pimb = cpl_image_get_data_float(imb) ;
396  pimc = cpl_image_get_data_float(imc) ;
397 
398  /* Test sizes */
399  nx = cpl_image_get_size_x(cpl_imagelist_get(ilist, 0)) ;
400  ny = cpl_image_get_size_y(cpl_imagelist_get(ilist, 0)) ;
401  ni = cpl_imagelist_get_size(ilist) ;
402  if ((cpl_image_get_size_x(ima) != nx) ||
403  (cpl_image_get_size_x(imb) != nx) ||
404  (cpl_image_get_size_x(imc) != nx) ||
405  (cpl_image_get_size_y(ima) != ny) ||
406  (cpl_image_get_size_y(imb) != ny) ||
407  (cpl_image_get_size_y(imc) != ny)) {
408  cpl_msg_error(cpl_func, "Incompatible sizes") ;
409  cpl_image_delete(ima) ;
410  cpl_image_delete(imb) ;
411  cpl_image_delete(imc) ;
412  return -1 ;
413  }
414 
415  /* Loop on pixels */
416  for (i=0 ; i<nx*ny ; i++) {
417  if (fabs(pimc[i]) < 1e-5) {
418  /* Correct this pixel in each plane */
419  for (j=0 ; j<ni ; j++) {
420  pdata = cpl_image_get_data_float(cpl_imagelist_get(ilist, j)) ;
421  val = pdata[i] ;
422  pdata[i] = val-pima[i] ;
423  }
424  } else if (fabs(pimb[i]) < 1e-3) {
425  for (j=0 ; j<ni ; j++) {
426  pdata = cpl_image_get_data_float(cpl_imagelist_get(ilist, j)) ;
427  pdata[i] = 0.0 ;
428  }
429  } else {
430  val2 = 2 * pimc[i] / (pimb[i] * pimb[i]) ;
431  /* Correct this pixel in each plane */
432  for (j=0 ; j<ni ; j++) {
433  pdata = cpl_image_get_data_float(cpl_imagelist_get(ilist, j)) ;
434  val = pdata[i] ;
435  val3 = 1-2*val2*(pima[i]-val) ;
436  if (val3 < 0.0) {
437  pdata[i] = val-pima[i] ;
438  } else {
439  pdata[i]=((float)sqrt(val3)-1) / val2 ;
440  }
441  }
442  }
443  }
444  /* Free and return */
445  cpl_image_delete(ima) ;
446  cpl_image_delete(imb) ;
447  cpl_image_delete(imc) ;
448  return 0 ;
449 }
450 
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_correct_crosstalk_list
int sofi_correct_crosstalk_list(cpl_imagelist *ilist)
Remove the Cross-talk effect from an image list.
Definition: sofi_utils.c:91
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_get_offsets
cpl_bivector * sofi_get_offsets(cpl_frameset *fset)
Get the offsets from a set of frames.
Definition: sofi_utils.c:181
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_get_license
const char * sofi_get_license(void)
Get the pipeline copyright and license.
Definition: sofi_utils.c:60
sofi_get_bbfilter
sofi_band sofi_get_bbfilter(char *f)
Get the broad band filter.
Definition: sofi_utils.c:233
sofi_correct_crosstalk
int sofi_correct_crosstalk(cpl_image *ima)
Remove the Cross-talk effect.
Definition: sofi_utils.c:124
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