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 #include <math.h>
00033 #include <cpl.h>
00034 #include <moses.h>
00035 #include <fors_dfs.h>
00036
00037 static int fors_remove_bias_create(cpl_plugin *);
00038 static int fors_remove_bias_exec(cpl_plugin *);
00039 static int fors_remove_bias_destroy(cpl_plugin *);
00040 static int fors_remove_bias(cpl_parameterlist *, cpl_frameset *);
00041
00042 static char fors_remove_bias_description[] =
00043 "This recipe is used to subtract the master bias, produced by the recipe\n"
00044 "fors_bias, from one raw data frame. The overscan regions, if present, are\n"
00045 "used to compensate for variations of the bias level between master bias\n"
00046 "and input raw frame. The overscan regions are then trimmed from the result.\n"
00047 "In the table below the MXU acronym can be alternatively read as MOS and\n"
00048 "LSS.\n\n"
00049 "Input files:\n\n"
00050 " DO category: Type: Explanation: Required:\n"
00051 " LAMP_MXU\n"
00052 " or SCIENCE_MXU\n"
00053 " or STANDARD_MXU Raw Raw data frame Y\n"
00054 " MASTER_BIAS Calib Master bias frame Y\n\n"
00055 "Output files:\n\n"
00056 " DO category: Data type: Explanation:\n"
00057 " LAMP_UNBIAS_MXU\n"
00058 " or SCIENCE_UNBIAS_MXU\n"
00059 " or STANDARD_UNBIAS_MXU FITS image Bias subtracted frame\n\n";
00060
00061 #define fors_remove_bias_exit(message) \
00062 { \
00063 if (message) cpl_msg_error(recipe, message); \
00064 cpl_image_delete(raw_image); \
00065 cpl_image_delete(master_bias); \
00066 cpl_propertylist_delete(header); \
00067 cpl_table_delete(overscans); \
00068 cpl_msg_indent_less(); \
00069 return -1; \
00070 }
00071
00072 #define fors_remove_bias_exit_memcheck(message) \
00073 { \
00074 if (message) cpl_msg_info(recipe, message); \
00075 printf("free raw_image (%p)\n", raw_image); \
00076 cpl_image_delete(raw_image); \
00077 printf("free master_bias (%p)\n", master_bias); \
00078 cpl_image_delete(master_bias); \
00079 printf("free header (%p)\n", header); \
00080 cpl_propertylist_delete(header); \
00081 printf("free overscans (%p)\n", overscans); \
00082 cpl_table_delete(overscans); \
00083 cpl_msg_indent_less(); \
00084 return 0; \
00085 }
00086
00087
00099 int cpl_plugin_get_info(cpl_pluginlist *list)
00100 {
00101 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe );
00102 cpl_plugin *plugin = &recipe->interface;
00103
00104 cpl_plugin_init(plugin,
00105 CPL_PLUGIN_API,
00106 FORS_BINARY_VERSION,
00107 CPL_PLUGIN_TYPE_RECIPE,
00108 "fors_remove_bias",
00109 "Subtract bias from input frame",
00110 fors_remove_bias_description,
00111 "Carlo Izzo",
00112 PACKAGE_BUGREPORT,
00113 "This file is currently part of the FORS Instrument Pipeline\n"
00114 "Copyright (C) 2002-2010 European Southern Observatory\n\n"
00115 "This program is free software; you can redistribute it and/or modify\n"
00116 "it under the terms of the GNU General Public License as published by\n"
00117 "the Free Software Foundation; either version 2 of the License, or\n"
00118 "(at your option) any later version.\n\n"
00119 "This program is distributed in the hope that it will be useful,\n"
00120 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00121 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
00122 "GNU General Public License for more details.\n\n"
00123 "You should have received a copy of the GNU General Public License\n"
00124 "along with this program; if not, write to the Free Software Foundation,\n"
00125 "Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n",
00126 fors_remove_bias_create,
00127 fors_remove_bias_exec,
00128 fors_remove_bias_destroy);
00129
00130 cpl_pluginlist_append(list, plugin);
00131
00132 return 0;
00133 }
00134
00135
00146 static int fors_remove_bias_create(cpl_plugin *plugin)
00147 {
00148 cpl_recipe *recipe;
00149
00150
00151
00152
00153
00154
00155
00156
00157 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00158 recipe = (cpl_recipe *)plugin;
00159 else
00160 return -1;
00161
00162
00163
00164
00165
00166 recipe->parameters = cpl_parameterlist_new();
00167
00168 return 0;
00169 }
00170
00171
00180 static int fors_remove_bias_exec(cpl_plugin *plugin)
00181 {
00182 cpl_recipe *recipe;
00183
00184 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00185 recipe = (cpl_recipe *)plugin;
00186 else
00187 return -1;
00188
00189 return fors_remove_bias(recipe->parameters, recipe->frames);
00190 }
00191
00192
00201 static int fors_remove_bias_destroy(cpl_plugin *plugin)
00202 {
00203 cpl_recipe *recipe;
00204
00205 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00206 recipe = (cpl_recipe *)plugin;
00207 else
00208 return -1;
00209
00210 cpl_parameterlist_delete(recipe->parameters);
00211
00212 return 0;
00213 }
00214
00215
00225 static int fors_remove_bias(cpl_parameterlist *parlist, cpl_frameset *frameset)
00226 {
00227
00228 const char *recipe = "fors_remove_bias";
00229
00230
00231
00232
00233
00234
00235 cpl_image *raw_image = NULL;
00236 cpl_image *master_bias = NULL;
00237 cpl_image *dummy = NULL;
00238 cpl_table *overscans = NULL;
00239 cpl_propertylist *header = NULL;
00240
00241
00242
00243
00244
00245 char version[80];
00246 const char *master_bias_tag = "MASTER_BIAS";
00247 const char *raw_image_tag;
00248 const char *pro_image_tag;
00249 char *instrume = NULL;
00250 int lamp_mxu;
00251 int lamp_mos;
00252 int lamp_lss;
00253 int science_mxu;
00254 int science_mos;
00255 int science_lss;
00256 int standard_mxu;
00257 int standard_mos;
00258 int standard_lss;
00259 int nbias, nframe;
00260
00261
00262 cpl_msg_set_indentation(2);
00263
00264 if (dfs_files_dont_exist(frameset))
00265 fors_remove_bias_exit(NULL);
00266
00267
00268 cpl_msg_info(recipe, "Check input set-of-frames:");
00269 cpl_msg_indent_more();
00270
00271 nbias = cpl_frameset_count_tags(frameset, master_bias_tag);
00272 if (nbias == 0) {
00273 cpl_msg_error(recipe, "Missing required input: %s", master_bias_tag);
00274 fors_remove_bias_exit(NULL);
00275 }
00276 if (nbias > 1) {
00277 cpl_msg_error(recipe, "Too many in input (%d > 1): %s",
00278 nbias, master_bias_tag);
00279 fors_remove_bias_exit(NULL);
00280 }
00281
00282 nframe = lamp_mxu = cpl_frameset_count_tags(frameset, "LAMP_MXU");
00283 nframe += lamp_mos = cpl_frameset_count_tags(frameset, "LAMP_MOS");
00284 nframe += lamp_lss = cpl_frameset_count_tags(frameset, "LAMP_LSS");
00285 nframe += science_mxu = cpl_frameset_count_tags(frameset, "SCIENCE_MXU");
00286 nframe += science_mos = cpl_frameset_count_tags(frameset, "SCIENCE_MOS");
00287 nframe += science_lss = cpl_frameset_count_tags(frameset, "SCIENCE_LSS");
00288 nframe += standard_mxu = cpl_frameset_count_tags(frameset, "STANDARD_MXU");
00289 nframe += standard_mos = cpl_frameset_count_tags(frameset, "STANDARD_MOS");
00290 nframe += standard_lss = cpl_frameset_count_tags(frameset, "STANDARD_LSS");
00291
00292 if (nframe == 0) {
00293 fors_remove_bias_exit("Missing required input raw frame");
00294 }
00295 if (nframe > 1) {
00296 cpl_msg_error(recipe, "Too many input raw frames (%d > 1)", nframe);
00297 fors_remove_bias_exit(NULL);
00298 }
00299
00300 if (lamp_mxu) {
00301 raw_image_tag = "LAMP_MXU";
00302 pro_image_tag = "LAMP_UNBIAS_MXU";
00303 }
00304 else if (lamp_mos) {
00305 raw_image_tag = "LAMP_MOS";
00306 pro_image_tag = "LAMP_UNBIAS_MOS";
00307 }
00308 else if (lamp_lss) {
00309 raw_image_tag = "LAMP_LSS";
00310 pro_image_tag = "LAMP_UNBIAS_LSS";
00311 }
00312 else if (science_mxu) {
00313 raw_image_tag = "SCIENCE_MXU";
00314 pro_image_tag = "SCIENCE_UNBIAS_MXU";
00315 }
00316 else if (science_mos) {
00317 raw_image_tag = "SCIENCE_MOS";
00318 pro_image_tag = "SCIENCE_UNBIAS_MOS";
00319 }
00320 else if (science_lss) {
00321 raw_image_tag = "SCIENCE_LSS";
00322 pro_image_tag = "SCIENCE_UNBIAS_LSS";
00323 }
00324 else if (standard_mxu) {
00325 raw_image_tag = "STANDARD_MXU";
00326 pro_image_tag = "STANDARD_UNBIAS_MXU";
00327 }
00328 else if (standard_mos) {
00329 raw_image_tag = "STANDARD_MOS";
00330 pro_image_tag = "STANDARD_UNBIAS_MOS";
00331 }
00332 else if (standard_lss) {
00333 raw_image_tag = "STANDARD_LSS";
00334 pro_image_tag = "STANDARD_UNBIAS_LSS";
00335 }
00336
00337 if (!dfs_equal_keyword(frameset, "ESO DET CHIP1 ID"))
00338 fors_remove_bias_exit("Input frames are not from the same chip");
00339
00340 header = dfs_load_header(frameset, raw_image_tag, 0);
00341
00342 if (header == NULL) {
00343 cpl_msg_error(recipe, "Cannot load header of %s frame", raw_image_tag);
00344 fors_remove_bias_exit(NULL);
00345 }
00346
00347 instrume = (char *)cpl_propertylist_get_string(header, "INSTRUME");
00348 if (instrume == NULL) {
00349 cpl_msg_error(recipe, "Missing keyword INSTRUME in %s header",
00350 raw_image_tag);
00351 fors_remove_bias_exit(NULL);
00352 }
00353
00354 if (instrume[4] == '1')
00355 snprintf(version, 80, "%s/%s", "fors1", VERSION);
00356 if (instrume[4] == '2')
00357 snprintf(version, 80, "%s/%s", "fors2", VERSION);
00358
00359 cpl_msg_indent_less();
00360 cpl_msg_info(recipe, "Load input frames:");
00361 cpl_msg_indent_more();
00362
00363 master_bias = dfs_load_image(frameset,
00364 master_bias_tag, CPL_TYPE_FLOAT, 0, 1);
00365 if (master_bias == NULL)
00366 fors_remove_bias_exit("Cannot load master bias");
00367
00368 raw_image = dfs_load_image(frameset,
00369 raw_image_tag, CPL_TYPE_FLOAT, 0, 0);
00370 if (raw_image == NULL) {
00371 cpl_msg_error(recipe, "Cannot load %s frame", raw_image_tag);
00372 fors_remove_bias_exit(NULL);
00373 }
00374
00375 overscans = mos_load_overscans_vimos(header, 1);
00376
00377 cpl_msg_indent_less();
00378 cpl_msg_info(recipe, "Subtract the master bias from input %s...",
00379 raw_image_tag);
00380 cpl_msg_indent_more();
00381
00382 dummy = mos_remove_bias(raw_image, master_bias, overscans);
00383 cpl_table_delete(overscans); overscans = NULL;
00384 cpl_image_delete(master_bias); master_bias = NULL;
00385 cpl_image_delete(raw_image); raw_image = dummy;
00386
00387 if (raw_image == NULL) {
00388 cpl_msg_error(recipe, "Cannot remove bias from %s frame",
00389 raw_image_tag);
00390 fors_remove_bias_exit(NULL);
00391 }
00392
00393 cpl_msg_indent_less();
00394
00395 if (dfs_save_image(frameset, raw_image, pro_image_tag,
00396 header, parlist, recipe, version))
00397 fors_remove_bias_exit(NULL);
00398
00399 cpl_propertylist_delete(header); header = NULL;
00400 cpl_image_delete(raw_image); raw_image = NULL;
00401
00402 return 0;
00403 }