IIINSTRUMENT Pipeline Reference Manual  1.3.12b
detmon_utils.c
1 /* $Id: detmon_utils.c,v 1.8 2013-07-15 12:03:32 amodigli Exp $
2  *
3  * This file is part of the DETMON 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: amodigli $
23  * $Date: 2013-07-15 12:03:32 $
24  * $Revision: 1.8 $
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 "detmon_utils.h"
37 #include "irplib_utils.h"
38 
40 /*----------------------------------------------------------------------------*/
44 /*----------------------------------------------------------------------------*/
45 
46 
47 /*----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------*/
56 const char * detmon_get_license(void)
57 {
58  const char * detmon_license =
59  "This file is part of the DETMON Instrument Pipeline\n"
60  "Copyright (C) 2002,2003 European Southern Observatory\n"
61  "\n"
62  "This program is free software; you can redistribute it and/or modify\n"
63  "it under the terms of the GNU General Public License as published by\n"
64  "the Free Software Foundation; either version 2 of the License, or\n"
65  "(at your option) any later version.\n"
66  "\n"
67  "This program is distributed in the hope that it will be useful,\n"
68  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
69  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
70  "GNU General Public License for more details.\n"
71  "\n"
72  "You should have received a copy of the GNU General Public License\n"
73  "along with this program; if not, write to the Free Software\n"
74  "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, \n"
75  "MA 02111-1307 USA" ;
76  return detmon_license ;
77 }
78 
79 
80 /* ---------------------------------------------------------------------------*/
91 /* ---------------------------------------------------------------------------*/
92 static void expand_region(const cpl_image * ref,
93  cpl_size * llx,
94  cpl_size * lly,
95  cpl_size * urx,
96  cpl_size * ury)
97 {
98  if (*llx < 1)
99  *llx = 1;
100  if (*lly < 1)
101  *lly = 1;
102  if (*urx < 1)
103  *urx = cpl_image_get_size_x(ref);
104  if (*ury < 1)
105  *ury = cpl_image_get_size_y(ref);
106 }
107 
108 
109 /* ---------------------------------------------------------------------------*/
129 /* ---------------------------------------------------------------------------*/
130 cpl_imagelist *
131 detmon_load_frameset_window(const cpl_frameset * fset, cpl_type type,
132  cpl_size pnum, cpl_size xtnum,
133  cpl_size llx, cpl_size lly,
134  cpl_size urx, cpl_size ury,
135  cpl_size nx, cpl_size ny)
136 {
137  cpl_imagelist * list = cpl_imagelist_new();
138  const cpl_size n = cpl_frameset_get_size(fset);
139  skip_if(pnum < 0);
140  if (nx >= 0 && ny >= 0) {
141  error_if(urx - llx + 1 > nx || ury - lly + 1 > ny ,
142  CPL_ERROR_ILLEGAL_INPUT,
143  "window size [%d:%d,%d:%d] larger than output size [%d, %d]",
144  (int)llx, (int)urx, (int)lly, (int)ury, (int)nx, (int)ny);
145  }
146 
147  for (cpl_size i = 0; i < n; i++) {
148  const cpl_frame * frm = cpl_frameset_get_position_const(fset, i);
149  const char * fn = cpl_frame_get_filename(frm);
150  if (xtnum < 0) {
151  const cpl_size next = cpl_frame_get_nextensions(frm);
152  for (cpl_size e = 0; e < next; e++) {
153  cpl_image * image = cpl_image_load_window(fn, type, pnum, e,
154  llx, lly, urx, ury);
155  skip_if(image == NULL);
156  if(nx < 0 || ny < 0 ) {
157  cpl_imagelist_set(list, image, cpl_imagelist_get_size(list));
158  } else {
159  cpl_image * full = cpl_image_new(nx, ny, type);
160  cpl_image_copy(full, image, llx, lly);
161  cpl_image_delete(image);
162  cpl_imagelist_set(list, full, cpl_imagelist_get_size(list));
163  }
164 
165  }
166  }
167  else {
168  cpl_image * image = cpl_image_load_window(fn, type, pnum, xtnum,
169  llx, lly, urx, ury);
170  skip_if(image == NULL);
171  if(nx < 0 || ny < 0 ) {
172  cpl_imagelist_set(list, image, cpl_imagelist_get_size(list));
173  } else {
174  cpl_image * full = cpl_image_new(nx, ny, type);
175  cpl_image_copy(full, image, llx, lly);
176  cpl_image_delete(image);
177  cpl_imagelist_set(list, full, cpl_imagelist_get_size(list));
178  }
179  }
180  }
181 
182  end_skip;
183 
184  if (cpl_error_get_code()) {
185  cpl_imagelist_delete(list);
186  return NULL;
187  }
188 
189  return list;
190 }
191 
192 
193 /* ---------------------------------------------------------------------------*/
206 /* ---------------------------------------------------------------------------*/
207 cpl_image * detmon_subtract_create_window(const cpl_image * a,
208  const cpl_image * b,
209  cpl_size llx,
210  cpl_size lly,
211  cpl_size urx,
212  cpl_size ury)
213 {
214  cpl_image * na = cpl_image_extract(a, llx, lly, urx, ury);
215  cpl_image * nb = cpl_image_extract(b, llx, lly, urx, ury);
216  cpl_image_subtract(na, nb);
217  cpl_image_delete(nb);
218 
219  return na;
220 }
221 
222 
223 /* ---------------------------------------------------------------------------*/
239 /* ---------------------------------------------------------------------------*/
240 cpl_image * detmon_subtracted_avg(const cpl_image * on1,
241  const cpl_image * off1,
242  const cpl_image * on2,
243  const cpl_image * off2,
244  cpl_size llx, cpl_size lly,
245  cpl_size urx, cpl_size ury)
246 {
247  expand_region(on1, &llx, &lly, &urx, &ury);
248  cpl_image * dif1 = cpl_image_extract(on1, llx, lly, urx, ury);
249  cpl_image * dif2 = cpl_image_extract(on2, llx, lly, urx, ury);
250  cpl_image * b = cpl_image_extract(off1, llx, lly, urx, ury);
251  cpl_image * dif_avg;
252 
253  cpl_image_subtract(dif1, b);
254  if (off1 != off2) {
255  cpl_image_delete(b);
256  b = cpl_image_extract(off2, llx, lly, urx, ury);
257  cpl_image_subtract(dif1, b);
258  }
259  else {
260  cpl_image_subtract(dif2, b);
261  }
262  cpl_image_delete(b);
263 
264  dif_avg = cpl_image_average_create(dif1, dif2);
265  cpl_image_abs(dif_avg);
266 
267  cpl_image_delete(dif1);
268  cpl_image_delete(dif2);
269 
270  return dif_avg;
271 }
272