00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036
00037
00038 #include "visir_recipe.h"
00039
00040
00041
00042
00043
00044 #define VISIR_IMG_ACHRO_MIN_DIST 5
00045
00046 #define RECIPE_STRING "visir_img_achro"
00047
00048
00049
00050
00051
00052 static cpl_table * visir_img_achro_reduce(const irplib_framelist *);
00053 static cpl_error_code visir_img_achro_save(cpl_frameset *,
00054 const cpl_parameterlist *,
00055 const cpl_table *);
00056
00057 VISIR_RECIPE_DEFINE(visir_img_achro, 0, "Achromatism recipe",
00058 "This recipe computes the best transmission wavelength "
00059 "at different\n"
00060 "positions on the detector.\n"
00061 "The files listed in the Set Of Frames (sof-file) "
00062 "must be tagged:\n"
00063 "VISIR-achromatism-file.fits IM_CAL_ACHRO\n"
00064 "The results are given in an output table.\n");
00065
00066
00070
00071
00072
00073
00074
00075
00076
00083
00084 static int visir_img_achro(cpl_frameset * framelist,
00085 const cpl_parameterlist * parlist)
00086 {
00087 irplib_framelist * allframes = NULL;
00088 irplib_framelist * rawframes = NULL;
00089 cpl_table * tab = NULL;
00090
00091
00092
00093 skip_if (visir_dfs_set_groups(framelist));
00094
00095
00096 allframes = irplib_framelist_cast(framelist);
00097 skip_if(allframes == NULL);
00098 rawframes = irplib_framelist_extract(allframes, VISIR_IMG_ACHRO_RAW);
00099 skip_if (rawframes == NULL);
00100
00101 skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
00102 visir_property_regexp,
00103 CPL_FALSE));
00104
00105 skip_if(visir_dfs_check_framelist_tag(rawframes));
00106
00107
00108 cpl_msg_info(cpl_func, "Compute the central wavelengths");
00109 if ((tab = visir_img_achro_reduce(rawframes)) == NULL) {
00110 cpl_msg_error(cpl_func, "Cannot compute the wavelengths");
00111 skip_if(1);
00112 }
00113
00114
00115 cpl_msg_info(cpl_func, "Save the products");
00116 skip_if (visir_img_achro_save(framelist, parlist, tab));
00117
00118 end_skip;
00119
00120 irplib_framelist_delete(allframes);
00121 irplib_framelist_delete(rawframes);
00122 cpl_table_delete(tab);
00123
00124 return cpl_error_get_code();
00125 }
00126
00127
00133
00134 static cpl_table * visir_img_achro_reduce(const irplib_framelist * rawframes)
00135 {
00136 cpl_imagelist * loaded = NULL;
00137 double * wls = NULL;
00138 cpl_table * tab = NULL;
00139 cpl_table * tab_out = NULL;
00140 int nfiles, nb_pos;
00141 int j;
00142
00143
00144 skip_if (0);
00145
00146 skip_if (rawframes == NULL);
00147
00148
00149 cpl_msg_info(cpl_func, "Load the input frames");
00150 if ((loaded = visir_imagelist_load_last(rawframes)) == NULL) {
00151 cpl_msg_error(cpl_func, "Cannot load the input frames");
00152 skip_if(1);
00153 }
00154 nfiles = cpl_imagelist_get_size(loaded);
00155 skip_if (nfiles < 1);
00156
00157
00158 cpl_msg_info(cpl_func, "Get the wavelengths from the input headers");
00159 if ((wls = visir_utils_get_wls(rawframes)) == NULL) {
00160 cpl_msg_error(cpl_func, "Cannot get wavelengths");
00161 skip_if(1);
00162 }
00163
00164
00165 cpl_msg_info(cpl_func, "Detect the objects and get their flux");
00166
00167
00168 tab = visir_table_new_xypos(loaded, "FLUX");
00169 skip_if (tab == NULL);
00170
00171 skip_if (cpl_table_wrap_double(tab, wls, "WAVELENGTH"));
00172 wls = NULL;
00173
00174
00175 skip_if (cpl_table_divide_scalar(tab, "FLUX",
00176 cpl_table_get_column_max(tab, "FLUX")));
00177
00178
00179 cpl_msg_info(cpl_func, "Group the frames with the same object position");
00180 cpl_msg_info(cpl_func, "Compute the central wavelength at each position");
00181
00182
00183 tab_out = cpl_table_new(nfiles);
00184
00185
00186 skip_if (cpl_table_copy_structure(tab_out, tab));
00187 skip_if (cpl_table_new_column(tab_out, "NVALS", CPL_TYPE_DOUBLE));
00188
00189 skip_if (cpl_table_fill_column_window(tab_out, "X_POS", 0, nfiles, 0));
00190 skip_if (cpl_table_fill_column_window(tab_out, "Y_POS", 0, nfiles, 0));
00191 skip_if (cpl_table_fill_column_window(tab_out, "WAVELENGTH", 0, nfiles, 0));
00192 skip_if (cpl_table_fill_column_window(tab_out, "FLUX", 0, nfiles, 0));
00193 skip_if (cpl_table_fill_column_window(tab_out, "NVALS", 0, nfiles, 0));
00194
00195 nb_pos = 0;
00196
00197 for (j=0 ; j < nfiles ; j++) {
00198 const double refx = cpl_table_get(tab, "X_POS", j, NULL);
00199 const double refy = cpl_table_get(tab, "Y_POS", j, NULL);
00200 const double flux = cpl_table_get(tab, "FLUX", j, NULL);
00201 const double wlsj = cpl_table_get(tab, "WAVELENGTH", j, NULL);
00202
00203 int i;
00204
00205 if (refx <= 0 || refy <= 0 || flux <= 0) continue;
00206
00207 for (i = 0; i < nb_pos; i++) {
00208 const double difx = cpl_table_get(tab, "X_POS", i, NULL) - refx;
00209 const double dify = cpl_table_get(tab, "Y_POS", i, NULL) - refy;
00210
00211
00212 if (difx * difx + dify * dify
00213 < VISIR_IMG_ACHRO_MIN_DIST * VISIR_IMG_ACHRO_MIN_DIST) break;
00214 }
00215 if (i == nb_pos) nb_pos++;
00216
00217
00218 skip_if (cpl_table_set(tab_out, "NVALS", i, 1
00219 + cpl_table_get(tab_out, "NVALS", i, NULL)));
00220 skip_if (cpl_table_set(tab_out, "X_POS", i, refx
00221 + cpl_table_get(tab_out, "X_POS", i, NULL)));
00222 skip_if (cpl_table_set(tab_out, "Y_POS", i, refy
00223 + cpl_table_get(tab_out, "Y_POS", i, NULL)));
00224 skip_if (cpl_table_set(tab_out, "FLUX", i, flux
00225 + cpl_table_get(tab_out, "FLUX", i, NULL)));
00226 skip_if (cpl_table_set(tab_out, "WAVELENGTH", i, flux * wlsj
00227 + cpl_table_get(tab_out, "WAVELENGTH", i, NULL)));
00228
00229 }
00230
00231 skip_if (nb_pos < 1);
00232
00233 skip_if (cpl_table_set_size(tab_out, nb_pos));
00234
00235 skip_if (cpl_table_divide_columns(tab_out, "X_POS", "NVALS"));
00236 skip_if (cpl_table_divide_columns(tab_out, "Y_POS", "NVALS"));
00237 skip_if (cpl_table_divide_columns(tab_out, "WAVELENGTH", "FLUX"));
00238
00239 skip_if (cpl_table_erase_column(tab_out, "FLUX"));
00240 skip_if (cpl_table_erase_column(tab_out, "NVALS"));
00241
00242 end_skip;
00243
00244 cpl_free(wls);
00245 cpl_table_delete(tab);
00246 cpl_imagelist_delete(loaded);
00247
00248 if (cpl_error_get_code()) {
00249 cpl_table_delete(tab_out);
00250 tab_out = NULL;
00251 }
00252
00253 return tab_out;
00254 }
00255
00256
00264
00265 static cpl_error_code visir_img_achro_save(cpl_frameset * set,
00266 const cpl_parameterlist * parlist,
00267 const cpl_table * tab)
00268 {
00269
00270 skip_if(irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
00271 VISIR_IMG_ACHRO_TAB_PROCATG, NULL, NULL,
00272 visir_pipe_id, RECIPE_STRING CPL_DFS_FITS));
00273 end_skip;
00274
00275 return cpl_error_get_code();
00276 }