IIINSTRUMENT Pipeline Reference Manual 4.6.1
visir_img_achro.c
1/* $Id: visir_img_achro.c,v 1.63 2009-01-29 08:56:58 llundin Exp $
2 *
3 * This file is part of the VISIR 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: 2009-01-29 08:56:58 $
24 * $Revision: 1.63 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32
33
34/*-----------------------------------------------------------------------------
35 Includes
36 -----------------------------------------------------------------------------*/
37
38#include "visir_recipe.h"
39
40/*-----------------------------------------------------------------------------
41 Defines
42 -----------------------------------------------------------------------------*/
43
44#define VISIR_IMG_ACHRO_MIN_DIST 5
45
46#define RECIPE_STRING "visir_img_achro"
47
48/*-----------------------------------------------------------------------------
49 Private Functions prototypes
50 -----------------------------------------------------------------------------*/
51
52static cpl_table * visir_img_achro_reduce(const irplib_framelist *);
53static cpl_error_code visir_img_achro_save(cpl_frameset *,
54 const cpl_parameterlist *,
55 const cpl_table *);
56
57VISIR_RECIPE_DEFINE(visir_img_achro, 0, "Achromatism recipe",
58 "This recipe computes the best transmission wavelength "
59 "at different\n"
60 "positions on the detector.\n"
61 "The files listed in the Set Of Frames (sof-file) "
62 "must be tagged:\n"
63 "VISIR-achromatism-file.fits IM_CAL_ACHRO\n"
64 "The results are given in an output table.\n");
65
66/*----------------------------------------------------------------------------*/
70/*----------------------------------------------------------------------------*/
71
72/*-----------------------------------------------------------------------------
73 Functions code
74 -----------------------------------------------------------------------------*/
75
76/*----------------------------------------------------------------------------*/
83/*----------------------------------------------------------------------------*/
84static int visir_img_achro(cpl_frameset * framelist,
85 const cpl_parameterlist * parlist)
86{
87 irplib_framelist * allframes = NULL;
88 irplib_framelist * rawframes = NULL;
89 cpl_table * tab = NULL;
90
91
92 /* Identify the RAW and CALIB frames in the input frameset */
93 skip_if (visir_dfs_set_groups(framelist));
94
95 /* Objects observation */
96 allframes = irplib_framelist_cast(framelist);
97 skip_if(allframes == NULL);
98 rawframes = irplib_framelist_extract(allframes, VISIR_IMG_ACHRO_RAW);
99 skip_if (rawframes == NULL);
100
101 skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
102 visir_property_regexp,
103 CPL_FALSE));
104
105 skip_if(visir_dfs_check_framelist_tag(rawframes));
106
107 /* Apply the reduction */
108 cpl_msg_info(cpl_func, "Compute the central wavelengths");
109 if ((tab = visir_img_achro_reduce(rawframes)) == NULL) {
110 cpl_msg_error(cpl_func, "Cannot compute the wavelengths");
111 skip_if(1);
112 }
113
114 /* Save the products */
115 cpl_msg_info(cpl_func, "Save the products");
116 skip_if (visir_img_achro_save(framelist, parlist, tab));
117
118 end_skip;
119
120 irplib_framelist_delete(allframes);
121 irplib_framelist_delete(rawframes);
122 cpl_table_delete(tab);
123
124 return cpl_error_get_code();
125}
126
127/*----------------------------------------------------------------------------*/
133/*----------------------------------------------------------------------------*/
134static cpl_table * visir_img_achro_reduce(const irplib_framelist * rawframes)
135{
136 cpl_imagelist * loaded = NULL;
137 double * wls = NULL;
138 cpl_table * tab = NULL;
139 cpl_table * tab_out = NULL;
140 int nfiles, nb_pos;
141 int j;
142
143
144 skip_if (0);
145
146 skip_if (rawframes == NULL);
147
148 /* Load the frames */
149 cpl_msg_info(cpl_func, "Load the input frames");
150 if ((loaded = visir_imagelist_load_last(rawframes)) == NULL) {
151 cpl_msg_error(cpl_func, "Cannot load the input frames");
152 skip_if(1);
153 }
154 nfiles = cpl_imagelist_get_size(loaded);
155 skip_if (nfiles < 1);
156
157 /* Get the wavelengths from the headers */
158 cpl_msg_info(cpl_func, "Get the wavelengths from the input headers");
159 if ((wls = visir_utils_get_wls(rawframes)) == NULL) {
160 cpl_msg_error(cpl_func, "Cannot get wavelengths");
161 skip_if(1);
162 }
163
164 /* Apply detection and log the detected objects in a table */
165 cpl_msg_info(cpl_func, "Detect the objects and get their flux");
166
167 /* Create the table and fill it with the relevant informations */
168 tab = visir_table_new_xypos(loaded, "FLUX");
169 skip_if (tab == NULL);
170
171 skip_if (cpl_table_wrap_double(tab, wls, "WAVELENGTH"));
172 wls = NULL;
173
174 /* Normalise the FLUX column */
175 skip_if (cpl_table_divide_scalar(tab, "FLUX",
176 cpl_table_get_column_max(tab, "FLUX")));
177
178 /* Identify the different positions */
179 cpl_msg_info(cpl_func, "Group the frames with the same object position");
180 cpl_msg_info(cpl_func, "Compute the central wavelength at each position");
181
182 /* Create the output table and fill it */
183 tab_out = cpl_table_new(nfiles);
184
185 /* In addition to the flux table it has an extra column for counting */
186 skip_if (cpl_table_copy_structure(tab_out, tab));
187 skip_if (cpl_table_new_column(tab_out, "NVALS", CPL_TYPE_DOUBLE));
188
189 skip_if (cpl_table_fill_column_window(tab_out, "X_POS", 0, nfiles, 0));
190 skip_if (cpl_table_fill_column_window(tab_out, "Y_POS", 0, nfiles, 0));
191 skip_if (cpl_table_fill_column_window(tab_out, "WAVELENGTH", 0, nfiles, 0));
192 skip_if (cpl_table_fill_column_window(tab_out, "FLUX", 0, nfiles, 0));
193 skip_if (cpl_table_fill_column_window(tab_out, "NVALS", 0, nfiles, 0));
194
195 nb_pos = 0;
196
197 for (j=0 ; j < nfiles ; j++) {
198 const double refx = cpl_table_get(tab, "X_POS", j, NULL);
199 const double refy = cpl_table_get(tab, "Y_POS", j, NULL);
200 const double flux = cpl_table_get(tab, "FLUX", j, NULL);
201 const double wlsj = cpl_table_get(tab, "WAVELENGTH", j, NULL);
202
203 int i;
204
205 if (refx <= 0 || refy <= 0 || flux <= 0) continue;
206
207 for (i = 0; i < nb_pos; i++) {
208 const double difx = cpl_table_get(tab, "X_POS", i, NULL) - refx;
209 const double dify = cpl_table_get(tab, "Y_POS", i, NULL) - refy;
210
211 /* Look for positions within the given distance */
212 if (difx * difx + dify * dify
213 < VISIR_IMG_ACHRO_MIN_DIST * VISIR_IMG_ACHRO_MIN_DIST) break;
214 }
215 if (i == nb_pos) nb_pos++; /* Found a new (refx, refy) */
216
217 /* Add row-values to its group in the output table */
218 skip_if (cpl_table_set(tab_out, "NVALS", i, 1
219 + cpl_table_get(tab_out, "NVALS", i, NULL)));
220 skip_if (cpl_table_set(tab_out, "X_POS", i, refx
221 + cpl_table_get(tab_out, "X_POS", i, NULL)));
222 skip_if (cpl_table_set(tab_out, "Y_POS", i, refy
223 + cpl_table_get(tab_out, "Y_POS", i, NULL)));
224 skip_if (cpl_table_set(tab_out, "FLUX", i, flux
225 + cpl_table_get(tab_out, "FLUX", i, NULL)));
226 skip_if (cpl_table_set(tab_out, "WAVELENGTH", i, flux * wlsj
227 + cpl_table_get(tab_out, "WAVELENGTH", i, NULL)));
228
229 }
230
231 skip_if (nb_pos < 1);
232
233 skip_if (cpl_table_set_size(tab_out, nb_pos));
234
235 skip_if (cpl_table_divide_columns(tab_out, "X_POS", "NVALS"));
236 skip_if (cpl_table_divide_columns(tab_out, "Y_POS", "NVALS"));
237 skip_if (cpl_table_divide_columns(tab_out, "WAVELENGTH", "FLUX"));
238
239 skip_if (cpl_table_erase_column(tab_out, "FLUX"));
240 skip_if (cpl_table_erase_column(tab_out, "NVALS"));
241
242 end_skip;
243
244 cpl_free(wls);
245 cpl_table_delete(tab);
246 cpl_imagelist_delete(loaded);
247
248 if (cpl_error_get_code()) {
249 cpl_table_delete(tab_out);
250 tab_out = NULL;
251 }
252
253 return tab_out;
254}
255
256/*----------------------------------------------------------------------------*/
264/*----------------------------------------------------------------------------*/
265static cpl_error_code visir_img_achro_save(cpl_frameset * set,
266 const cpl_parameterlist * parlist,
267 const cpl_table * tab)
268{
269
270 skip_if(irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
271 VISIR_IMG_ACHRO_TAB_PROCATG, NULL, NULL,
272 visir_pipe_id, RECIPE_STRING CPL_DFS_FITS));
273 end_skip;
274
275 return cpl_error_get_code();
276}
cpl_error_code visir_dfs_check_framelist_tag(const irplib_framelist *self)
Check the tags in a frameset (group raw only)
Definition: visir_dfs.c:234
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: visir_dfs.c:72
cpl_imagelist * visir_imagelist_load_last(const irplib_framelist *rawframes)
Load the last frame of the input files to an image list.