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 #include <math.h>
00037 #include <cpl.h>
00038
00039 #include "irplib_utils.h"
00040
00041 #include "visir_utils.h"
00042 #include "visir_pfits.h"
00043 #include "visir_dfs.h"
00044 #include "visir_inputs.h"
00045
00046
00047
00048
00049
00050 #define VISIR_IMG_ACHRO_MIN_DIST 5
00051
00052
00053
00054
00055
00056 static int visir_img_achro_create(cpl_plugin *);
00057 static int visir_img_achro_exec(cpl_plugin *);
00058 static int visir_img_achro_destroy(cpl_plugin *);
00059 static int visir_img_achro(cpl_parameterlist *, cpl_frameset *);
00060 static cpl_table * visir_img_achro_reduce(const cpl_frameset *);
00061 static int visir_img_achro_save(const cpl_table *, const cpl_parameterlist *,
00062 cpl_frameset *);
00063
00064
00065
00066
00067
00068 static const char * recipename = "visir_img_achro";
00069
00070 static char visir_img_achro_description[] =
00071 "This recipe computes the best transmission wavelength at different\n"
00072 "positions on the detector.\n"
00073 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00074 "VISIR-achromatism-file.fits IM_CAL_ACHRO\n"
00075 "The results are given in an output table.\n";
00076
00077
00078
00079
00080
00081
00090
00091 int cpl_plugin_get_info(cpl_pluginlist * list)
00092 {
00093 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe));
00094 cpl_plugin * plugin = &recipe->interface;
00095
00096
00097 if (cpl_plugin_init(plugin,
00098 CPL_PLUGIN_API,
00099 VISIR_BINARY_VERSION,
00100 CPL_PLUGIN_TYPE_RECIPE,
00101 recipename,
00102 "Achromatism recipe",
00103 visir_img_achro_description,
00104 "Lars Lundin",
00105 PACKAGE_BUGREPORT,
00106 visir_get_license(),
00107 visir_img_achro_create,
00108 visir_img_achro_exec,
00109 visir_img_achro_destroy)) return 1;
00110
00111 if (cpl_pluginlist_append(list, plugin)) return 1;
00112
00113 return 0;
00114 }
00115
00116
00125
00126 static int visir_img_achro_create(cpl_plugin * plugin)
00127 {
00128 cpl_recipe * recipe = (cpl_recipe *)plugin;
00129
00130
00131 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00132
00133
00134 recipe->parameters = cpl_parameterlist_new();
00135
00136 return 0;
00137
00138 }
00139
00140
00146
00147 static int visir_img_achro_exec(cpl_plugin * plugin)
00148 {
00149 cpl_recipe * recipe = (cpl_recipe *)plugin;
00150
00151
00152 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00153
00154 return visir_img_achro(recipe->parameters, recipe->frames);
00155 }
00156
00157
00163
00164 static int visir_img_achro_destroy(cpl_plugin * plugin)
00165 {
00166 cpl_recipe * recipe = (cpl_recipe *)plugin;
00167
00168
00169 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00170 cpl_parameterlist_delete(recipe->parameters);
00171 return 0;
00172 }
00173
00174
00181
00182 static int visir_img_achro(
00183 cpl_parameterlist * parlist,
00184 cpl_frameset * framelist)
00185 {
00186 cpl_frameset * rawframes = NULL;
00187 cpl_table * tab = NULL;
00188
00189
00190 if (cpl_error_get_code()) return cpl_error_get_code();
00191
00192
00193 skip_if (visir_dfs_set_groups(framelist));
00194
00195
00196 rawframes = irplib_frameset_extract(framelist, VISIR_IMG_ACHRO_RAW);
00197 skip_if (rawframes == NULL);
00198
00199 skip_if(visir_dfs_check_frameset_tag(rawframes));
00200
00201
00202 cpl_msg_info(__func__, "Compute the central wavelengths");
00203 if ((tab = visir_img_achro_reduce(rawframes)) == NULL) {
00204 cpl_msg_error(__func__, "Cannot compute the wavelengths");
00205 skip_if(1);
00206 }
00207
00208
00209 cpl_msg_info(__func__, "Save the products");
00210 if (visir_img_achro_save(tab, parlist, framelist)) {
00211 cpl_msg_error(__func__, "Cannot save products");
00212 skip_if(1);
00213 }
00214
00215 end_skip;
00216
00217 cpl_frameset_delete(rawframes);
00218 cpl_table_delete(tab);
00219
00220 return cpl_error_get_code();
00221 }
00222
00223
00229
00230 static cpl_table * visir_img_achro_reduce(const cpl_frameset * set)
00231 {
00232 cpl_imagelist * loaded = NULL;
00233 double * wls = NULL;
00234 cpl_table * tab = NULL;
00235 cpl_table * tab_out = NULL;
00236 int nfiles, nb_pos;
00237 int j;
00238
00239
00240 skip_if (cpl_error_get_code());
00241
00242 skip_if (set == NULL);
00243
00244
00245 cpl_msg_info(__func__, "Load the input frames");
00246 if ((loaded = visir_imagelist_load_last(set)) == NULL) {
00247 cpl_msg_error(__func__, "Cannot load the input frames");
00248 skip_if(1);
00249 }
00250 nfiles = cpl_imagelist_get_size(loaded);
00251 skip_if (nfiles < 1);
00252
00253
00254 cpl_msg_info(__func__, "Get the wavelengths from the input headers");
00255 if ((wls = visir_utils_get_wls(set)) == NULL) {
00256 cpl_msg_error(__func__, "Cannot get wavelengths");
00257 skip_if(1);
00258 }
00259
00260
00261 cpl_msg_info(__func__, "Detect the objects and get their flux");
00262
00263
00264 tab = visir_table_new_xypos(loaded, "FLUX");
00265 skip_if (tab == NULL);
00266
00267 skip_if (cpl_table_wrap_double(tab, wls, "WAVELENGTH"));
00268 wls = NULL;
00269
00270
00271 skip_if (cpl_table_divide_scalar(tab, "FLUX",
00272 cpl_table_get_column_max(tab, "FLUX")));
00273
00274
00275 cpl_msg_info(__func__, "Group the frames with the same object position");
00276 cpl_msg_info(__func__, "Compute the central wavelength at each position");
00277
00278
00279 tab_out = cpl_table_new(nfiles);
00280
00281
00282 skip_if (cpl_table_copy_structure(tab_out, tab));
00283 skip_if (cpl_table_new_column(tab_out, "NVALS", CPL_TYPE_DOUBLE));
00284
00285 skip_if (cpl_table_fill_column_window(tab_out, "X_POS", 0, nfiles, 0));
00286 skip_if (cpl_table_fill_column_window(tab_out, "Y_POS", 0, nfiles, 0));
00287 skip_if (cpl_table_fill_column_window(tab_out, "WAVELENGTH", 0, nfiles, 0));
00288 skip_if (cpl_table_fill_column_window(tab_out, "FLUX", 0, nfiles, 0));
00289 skip_if (cpl_table_fill_column_window(tab_out, "NVALS", 0, nfiles, 0));
00290
00291 nb_pos = 0;
00292
00293 for (j=0 ; j < nfiles ; j++) {
00294 const double refx = cpl_table_get(tab, "X_POS", j, NULL);
00295 const double refy = cpl_table_get(tab, "Y_POS", j, NULL);
00296 const double flux = cpl_table_get(tab, "FLUX", j, NULL);
00297 const double wlsj = cpl_table_get(tab, "WAVELENGTH", j, NULL);
00298
00299 int i;
00300
00301 if (refx <= 0 || refy <= 0 || flux <= 0) continue;
00302
00303 for (i = 0; i < nb_pos; i++) {
00304 const double difx = cpl_table_get(tab, "X_POS", i, NULL) - refx;
00305 const double dify = cpl_table_get(tab, "Y_POS", i, NULL) - refy;
00306
00307
00308 if (difx * difx + dify * dify
00309 < VISIR_IMG_ACHRO_MIN_DIST * VISIR_IMG_ACHRO_MIN_DIST) break;
00310 }
00311 if (i == nb_pos) nb_pos++;
00312
00313
00314 skip_if (cpl_table_set(tab_out, "NVALS", i, 1
00315 + cpl_table_get(tab_out, "NVALS", i, NULL)));
00316 skip_if (cpl_table_set(tab_out, "X_POS", i, refx
00317 + cpl_table_get(tab_out, "X_POS", i, NULL)));
00318 skip_if (cpl_table_set(tab_out, "Y_POS", i, refy
00319 + cpl_table_get(tab_out, "Y_POS", i, NULL)));
00320 skip_if (cpl_table_set(tab_out, "FLUX", i, flux
00321 + cpl_table_get(tab_out, "FLUX", i, NULL)));
00322 skip_if (cpl_table_set(tab_out, "WAVELENGTH", i, flux * wlsj
00323 + cpl_table_get(tab_out, "WAVELENGTH", i, NULL)));
00324
00325 }
00326
00327 skip_if (nb_pos < 1);
00328
00329 skip_if (cpl_table_set_size(tab_out, nb_pos));
00330
00331 skip_if (cpl_table_divide_columns(tab_out, "X_POS", "NVALS"));
00332 skip_if (cpl_table_divide_columns(tab_out, "Y_POS", "NVALS"));
00333 skip_if (cpl_table_divide_columns(tab_out, "WAVELENGTH", "FLUX"));
00334
00335 skip_if (cpl_table_erase_column(tab_out, "FLUX"));
00336 skip_if (cpl_table_erase_column(tab_out, "NVALS"));
00337
00338 end_skip;
00339
00340 cpl_free(wls);
00341 cpl_table_delete(tab);
00342 cpl_imagelist_delete(loaded);
00343
00344 if (cpl_error_get_code()) {
00345 cpl_table_delete(tab_out);
00346 tab_out = NULL;
00347 }
00348
00349 return tab_out;
00350 }
00351
00352
00360
00361 static int visir_img_achro_save(
00362 const cpl_table * tab,
00363 const cpl_parameterlist * parlist,
00364 cpl_frameset * set)
00365 {
00366
00367 return visir_table_save(parlist, set, tab, recipename,
00368 VISIR_IMG_ACHRO_TAB_PROCATG, NULL, NULL);
00369 }