IIINSTRUMENT Pipeline Reference Manual  6.2.2
isaac_utils.c
1 /* $Id: isaac_utils.c,v 1.29 2013-03-12 08:06:48 llundin Exp $
2  *
3  * This file is part of the ISAAC 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:06:48 $
24  * $Revision: 1.29 $
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 <string.h>
37 #include <math.h>
38 #include <cpl.h>
39 
40 #include "irplib_oddeven.h"
41 
42 #include "isaac_utils.h"
43 #include "isaac_pfits.h"
44 
45 /*----------------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------------*/
50 
53 /*----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------*/
62 const char * isaac_get_license(void)
63 {
64  const char * isaac_license =
65  "This file is part of the ISAAC Instrument Pipeline\n"
66  "Copyright (C) 2002,2003 European Southern Observatory\n"
67  "\n"
68  "This program is free software; you can redistribute it and/or modify\n"
69  "it under the terms of the GNU General Public License as published by\n"
70  "the Free Software Foundation; either version 2 of the License, or\n"
71  "(at your option) any later version.\n"
72  "\n"
73  "This program is distributed in the hope that it will be useful,\n"
74  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
75  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
76  "GNU General Public License for more details.\n"
77  "\n"
78  "You should have received a copy of the GNU General Public License\n"
79  "along with this program; if not, write to the Free Software\n"
80  "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, \n"
81  "MA 02111-1307 USA";
82  return isaac_license;
83 }
84 
85 /*----------------------------------------------------------------------------*/
91 /*----------------------------------------------------------------------------*/
92 cpl_bivector * isaac_get_offsets(const cpl_frameset * fset)
93 {
94  cpl_bivector * offsets;
95  double * offsets_x;
96  double * offsets_y;
97  const cpl_frame * frame;
98  cpl_propertylist * plist;
99  int nfiles;
100  int i;
101 
102  /* Test entries */
103  if (fset == NULL) return NULL;
104 
105  /* Create the offsets bi vector */
106  nfiles = cpl_frameset_get_size(fset);
107  offsets = cpl_bivector_new(nfiles);
108  offsets_x = cpl_bivector_get_x_data(offsets);
109  offsets_y = cpl_bivector_get_y_data(offsets);
110  for (i=0; i<nfiles; i++) {
111  if (cpl_error_get_code()) {
112  cpl_bivector_delete(offsets);
113  return NULL;
114  }
115  /* X and Y offsets */
116  frame = cpl_frameset_get_position_const(fset, i);
117  plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0);
118  offsets_x[i] = isaac_pfits_get_cumoffsetx(plist);
119  offsets_y[i] = isaac_pfits_get_cumoffsety(plist);
120  cpl_propertylist_delete(plist);
121  if (cpl_error_get_code()) {
122  cpl_msg_error(cpl_func, "Cannot get offsets from header");
123  cpl_bivector_delete(offsets);
124  return NULL;
125  }
126  }
127  /* Subtract the first offset to all offsets */
128  for (i=1; i<nfiles; i++) {
129  offsets_x[i] -= offsets_x[0];
130  offsets_y[i] -= offsets_y[0];
131  }
132  offsets_x[0] = offsets_y[0] = 0.00;
133 
134  return offsets;
135 }
136 
137 /*----------------------------------------------------------------------------*/
143 /*----------------------------------------------------------------------------*/
144 isaac_band isaac_get_bbfilter(const char * f)
145 {
146  if (!strcmp(f, "J")) return ISAAC_BAND_J;
147  if (!strcmp(f, "J+Block")) return ISAAC_BAND_J;
148  if (!strcmp(f, "Js")) return ISAAC_BAND_J;
149  if (!strcmp(f, "Z")) return ISAAC_BAND_J;
150  if (!strcmp(f, "SZ")) return ISAAC_BAND_J;
151  if (!strcmp(f, "SH")) return ISAAC_BAND_H;
152  if (!strcmp(f, "H")) return ISAAC_BAND_H;
153  if (!strcmp(f, "Ks")) return ISAAC_BAND_KS;
154  if (!strcmp(f, "K")) return ISAAC_BAND_K;
155  if (!strcmp(f, "SK")) return ISAAC_BAND_K;
156  if (!strcmp(f, "L")) return ISAAC_BAND_L;
157  if (!strcmp(f, "L-new")) return ISAAC_BAND_L;
158  if (!strcmp(f, "SL")) return ISAAC_BAND_L;
159  if (!strcmp(f, "M")) return ISAAC_BAND_M;
160  if (!strcmp(f, "M_NB")) return ISAAC_BAND_M;
161  if (!strcmp(f, "M_NB-new")) return ISAAC_BAND_M;
162  if (!strcmp(f, "NB_1.06")) return ISAAC_BAND_J;
163  if (!strcmp(f, "NB_1.08")) return ISAAC_BAND_J;
164  if (!strcmp(f, "NB_1.19")) return ISAAC_BAND_J;
165  if (!strcmp(f, "NB_1.21")) return ISAAC_BAND_J;
166  if (!strcmp(f, "NB_1.26")) return ISAAC_BAND_J;
167  if (!strcmp(f, "NB_1.28")) return ISAAC_BAND_J;
168  if (!strcmp(f, "NB_1.64")) return ISAAC_BAND_H;
169  if (!strcmp(f, "NB_1.71")) return ISAAC_BAND_H;
170  if (!strcmp(f, "NB_2.07")) return ISAAC_BAND_KS;
171  if (!strcmp(f, "NB_2.09")) return ISAAC_BAND_KS;
172  if (!strcmp(f, "NB_2.13")) return ISAAC_BAND_KS;
173  if (!strcmp(f, "NB_2.17")) return ISAAC_BAND_KS;
174  if (!strcmp(f, "NB_2.19")) return ISAAC_BAND_KS;
175  if (!strcmp(f, "NB_2.25")) return ISAAC_BAND_KS;
176  if (!strcmp(f, "NB_2.29")) return ISAAC_BAND_KS;
177  if (!strcmp(f, "NB_2.34")) return ISAAC_BAND_KS;
178  if (!strcmp(f, "NB_3.21")) return ISAAC_BAND_L;
179  if (!strcmp(f, "NB_3.28")) return ISAAC_BAND_L;
180  if (!strcmp(f, "NB_3.80")) return ISAAC_BAND_L;
181  if (!strcmp(f, "NB_4.07")) return ISAAC_BAND_L;
182 
183  (void)cpl_error_set_message(cpl_func, CPL_ERROR_UNSUPPORTED_MODE, "%s", f);
184 
185  return ISAAC_BAND_UNKNOWN;
186 }
187 
188 /*----------------------------------------------------------------------------*/
194 /*----------------------------------------------------------------------------*/
195 isaac_band isaac_get_associated_filter(const char * f)
196 {
197  if (!strcmp(f, "J")) return ISAAC_BAND_J;
198  if (!strcmp(f, "Js")) return ISAAC_BAND_JS;
199  if (!strcmp(f, "Z")) return ISAAC_BAND_Z;
200  if (!strcmp(f, "SZ")) return ISAAC_BAND_SZ;
201  if (!strcmp(f, "SH")) return ISAAC_BAND_SH;
202  if (!strcmp(f, "H")) return ISAAC_BAND_H;
203  if (!strcmp(f, "Ks")) return ISAAC_BAND_KS;
204  if (!strcmp(f, "K")) return ISAAC_BAND_K;
205  if (!strcmp(f, "SK")) return ISAAC_BAND_SK;
206  if (!strcmp(f, "L")) return ISAAC_BAND_L;
207  if (!strcmp(f, "SL")) return ISAAC_BAND_SL;
208  if (!strcmp(f, "M")) return ISAAC_BAND_M;
209  if (!strcmp(f, "M_NB")) return ISAAC_BAND_M;
210  if (!strcmp(f, "NB_1.06")) return ISAAC_BAND_J;
211  if (!strcmp(f, "NB_1.08")) return ISAAC_BAND_J;
212  if (!strcmp(f, "NB_1.19")) return ISAAC_BAND_J;
213  if (!strcmp(f, "NB_1.21")) return ISAAC_BAND_J;
214  if (!strcmp(f, "NB_1.26")) return ISAAC_BAND_J;
215  if (!strcmp(f, "NB_1.28")) return ISAAC_BAND_J;
216  if (!strcmp(f, "NB_1.64")) return ISAAC_BAND_H;
217  if (!strcmp(f, "NB_1.71")) return ISAAC_BAND_H;
218  if (!strcmp(f, "NB_2.07")) return ISAAC_BAND_KS;
219  if (!strcmp(f, "NB_2.09")) return ISAAC_BAND_KS;
220  if (!strcmp(f, "NB_2.13")) return ISAAC_BAND_KS;
221  if (!strcmp(f, "NB_2.17")) return ISAAC_BAND_KS;
222  if (!strcmp(f, "NB_2.19")) return ISAAC_BAND_KS;
223  if (!strcmp(f, "NB_2.25")) return ISAAC_BAND_KS;
224  if (!strcmp(f, "NB_2.29")) return ISAAC_BAND_KS;
225  if (!strcmp(f, "NB_2.34")) return ISAAC_BAND_KS;
226  if (!strcmp(f, "NB_3.21")) return ISAAC_BAND_L;
227  if (!strcmp(f, "NB_3.28")) return ISAAC_BAND_L;
228  if (!strcmp(f, "NB_3.80")) return ISAAC_BAND_L;
229  if (!strcmp(f, "NB_4.07")) return ISAAC_BAND_L;
230 
231  (void)cpl_error_set_message(cpl_func, CPL_ERROR_UNSUPPORTED_MODE, "%s", f);
232 
233  return ISAAC_BAND_UNKNOWN;
234 }
235 
236 /*-------------------------------------------------------------------------*/
242 /*--------------------------------------------------------------------------*/
243 const char * isaac_std_band_name(isaac_band band)
244 {
245  switch (band) {
246  case ISAAC_BAND_J: return "J";
247  case ISAAC_BAND_JS: return "Js";
248  case ISAAC_BAND_JBLOCK: return "J+Block";
249  case ISAAC_BAND_H: return "H";
250  case ISAAC_BAND_K: return "K";
251  case ISAAC_BAND_KS: return "Ks";
252  case ISAAC_BAND_L: return "L";
253  case ISAAC_BAND_M: return "M";
254  case ISAAC_BAND_LP: return "Lp";
255  case ISAAC_BAND_MP: return "Mp";
256  case ISAAC_BAND_Z: return "Z";
257  case ISAAC_BAND_SZ: return "SZ";
258  case ISAAC_BAND_SH: return "SH";
259  case ISAAC_BAND_SK: return "SK";
260  case ISAAC_BAND_SL: return "SL";
261  default:
262  (void)cpl_error_set_message(cpl_func, CPL_ERROR_UNSUPPORTED_MODE,
263  "%d", (int)band);
264  return "Unknown";
265  }
266 }
267 
268 /*----------------------------------------------------------------------------*/
274 /*----------------------------------------------------------------------------*/
275 cpl_image * isaac_oddeven_correct(const cpl_image * in)
276 {
277  cpl_image * quad1;
278  cpl_image * quad2;
279  cpl_image * quad3;
280  cpl_image * quad4;
281  cpl_image * quad1_corr;
282  cpl_image * quad2_corr;
283  cpl_image * quad3_corr;
284  cpl_image * quad4_corr;
285  float * pout;
286  float * pin1;
287  float * pin2;
288  float * pin3;
289  float * pin4;
290  int nx, ny;
291  cpl_image * out;
292  int i, j, pos1, pos2, pos3, pos4;
293 
294  /* Test entries */
295  if (in == NULL) return NULL;
296 
297  /* Initialise */
298  nx = cpl_image_get_size_x(in);
299  ny = cpl_image_get_size_y(in);
300 
301  /* Extract quadrants */
302  quad1 = cpl_image_extract(in, 1, 1, nx/2, ny/2);
303  quad2 = cpl_image_extract(in, (nx/2)+1, 1, nx, ny/2);
304  quad3 = cpl_image_extract(in, 1, (ny/2)+1, nx/2, ny);
305  quad4 = cpl_image_extract(in, (nx/2)+1, (ny/2)+1, nx, ny);
306 
307  /* Apply the FFT to the quadrants */
308  quad1_corr = irplib_oddeven_correct(quad1);
309  quad2_corr = irplib_oddeven_correct(quad2);
310  quad3_corr = irplib_oddeven_correct(quad3);
311  quad4_corr = irplib_oddeven_correct(quad4);
312 
313  cpl_image_delete(quad1);
314  cpl_image_delete(quad2);
315  cpl_image_delete(quad3);
316  cpl_image_delete(quad4);
317 
318  /* Construct the output image */
319  out = cpl_image_duplicate(in);
320  pout = cpl_image_get_data_float(out);
321  pin1 = cpl_image_get_data_float(quad1_corr);
322  pin2 = cpl_image_get_data_float(quad2_corr);
323  pin3 = cpl_image_get_data_float(quad3_corr);
324  pin4 = cpl_image_get_data_float(quad4_corr);
325 
326  for (j=0; j<ny/2; j++) {
327  for (i=0; i<nx/2; i++) {
328  pos1 = i + j * nx;
329  pos2 = (i+(nx/2)) + j * nx;
330  pos3 = i + (j+(ny/2)) * nx;
331  pos4 = (i+(nx/2)) + (j+(ny/2)) * nx;
332  pout[pos1] = pin1[i+j*(nx/2)];
333  pout[pos2] = pin2[i+j*(nx/2)];
334  pout[pos3] = pin3[i+j*(nx/2)];
335  pout[pos4] = pin4[i+j*(nx/2)];
336  }
337  }
338 
339  cpl_image_delete(quad1_corr);
340  cpl_image_delete(quad2_corr);
341  cpl_image_delete(quad3_corr);
342  cpl_image_delete(quad4_corr);
343  return out;
344 }
345 
346 /*----------------------------------------------------------------------------*/
355 /*----------------------------------------------------------------------------*/
356 cpl_frameset * isaac_extract_frameset(const cpl_frameset * self,
357  const char * tag)
358 {
359  const cpl_size nframes = cpl_frameset_get_size(self);
360  cpl_frameset * newset = NULL;
361 
362 
363  if (self == NULL) return NULL;
364  if (tag == NULL) return NULL;
365 
366  for (cpl_size i = 0; i < nframes; i++) {
367  const cpl_frame * frame = cpl_frameset_get_position_const(self, i);
368  const char * mytag = cpl_frame_get_tag(frame);
369 
370  if (mytag != NULL && !strcmp(mytag, tag)) {
371  cpl_frame * newframe = cpl_frame_duplicate(frame);
372 
373  /* Create the output frameset */
374  if (newset == NULL) newset = cpl_frameset_new();
375 
376  if (cpl_frameset_insert(newset, newframe)) {
377  cpl_frame_delete(newframe);
378  cpl_frameset_delete(newset);
379  (void)cpl_error_set(cpl_func, cpl_error_get_code());
380  return NULL;
381  }
382  }
383  }
384 
385  return newset;
386 }
387 
388 /*----------------------------------------------------------------------------*/
396 /*----------------------------------------------------------------------------*/
397 const char * isaac_extract_filename(const cpl_frameset * self,
398  const char * tag)
399 {
400  const cpl_frame * frame;
401 
402  if (self == NULL) return NULL;
403  if (tag == NULL) return NULL;
404 
405  /* Get the frame */
406  frame = cpl_frameset_find_const(self, tag);
407 
408  return frame == NULL ? NULL : cpl_frame_get_filename(frame);
409 }
410 
double isaac_pfits_get_cumoffsety(const cpl_propertylist *plist)
find out the cumulative offset in Y
Definition: isaac_pfits.c:211
double isaac_pfits_get_cumoffsetx(const cpl_propertylist *plist)
find out the cumulative offset in X
Definition: isaac_pfits.c:196
const char * isaac_extract_filename(const cpl_frameset *self, const char *tag)
Extract the filename of the first frame of the given tag.
Definition: isaac_utils.c:397
const char * isaac_std_band_name(isaac_band band)
Return a band name.
Definition: isaac_utils.c:243
cpl_image * isaac_oddeven_correct(const cpl_image *in)
Correct the odd/even in an image.
Definition: isaac_utils.c:275
isaac_band isaac_get_bbfilter(const char *f)
Get the broad band filter.
Definition: isaac_utils.c:144
cpl_bivector * isaac_get_offsets(const cpl_frameset *fset)
Get the offsets from a set of frames.
Definition: isaac_utils.c:92
cpl_frameset * isaac_extract_frameset(const cpl_frameset *self, const char *tag)
Extract the frames with the given tag from a frameset.
Definition: isaac_utils.c:356
const char * isaac_get_license(void)
Get the pipeline copyright and license.
Definition: isaac_utils.c:62
isaac_band isaac_get_associated_filter(const char *f)
Get the broad band filter.
Definition: isaac_utils.c:195