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
00029
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033
00034 #include <stdio.h>
00035 #include <cpl.h>
00036
00037 #include "vircam_utils.h"
00038 #include "vircam_dfs.h"
00039 #include "vircam_fits.h"
00040 #include "vircam_mods.h"
00041
00042
00043
00044 static int vircam_darkcor_create(cpl_plugin *) ;
00045 static int vircam_darkcor_exec(cpl_plugin *) ;
00046 static int vircam_darkcor_destroy(cpl_plugin *) ;
00047 static int vircam_darkcor_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_darkcor_save(cpl_frameset *framelist,
00049 cpl_parameterlist *parlist);
00050 static void vircam_darkcor_init(void);
00051 static void vircam_darkcor_tidy(void);
00052
00053 static struct {
00054
00055
00056
00057 float darkscl;
00058 int extenum;
00059
00060 } vircam_darkcor_config;
00061
00062 static struct {
00063 int *labels;
00064 cpl_frame *dark;
00065 cpl_frame *img;
00066 vir_fits *darkf;
00067 vir_fits *imgf;
00068 } ps;
00069
00070 static int isfirst;
00071 static cpl_frame *product_frame = NULL;
00072
00073
00074 static char vircam_darkcor_description[] =
00075 "vircam_darkcor -- VIRCAM dark correction test recipe.\n\n"
00076 "Dark correct an input frame using a master dark frame\n\n"
00077 "The program accepts the following files in the SOF:\n\n"
00078 " Tag Description\n"
00079 " -----------------------------------------------------------------------\n"
00080 " %-21s A input uncorrected image\n"
00081 " %-21s A master dark frame\n"
00082 "\n";
00083
00129
00130
00131
00132
00140
00141
00142 int cpl_plugin_get_info(cpl_pluginlist *list) {
00143 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00144 cpl_plugin *plugin = &recipe->interface;
00145 char alldesc[SZ_ALLDESC];
00146 (void)snprintf(alldesc,SZ_ALLDESC,vircam_darkcor_description,
00147 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_DARK);
00148
00149 cpl_plugin_init(plugin,
00150 CPL_PLUGIN_API,
00151 VIRCAM_BINARY_VERSION,
00152 CPL_PLUGIN_TYPE_RECIPE,
00153 "vircam_darkcor",
00154 "VIRCAM dark correction test recipe [test]",
00155 alldesc,
00156 "Jim Lewis",
00157 "jrl@ast.cam.ac.uk",
00158 vircam_get_license(),
00159 vircam_darkcor_create,
00160 vircam_darkcor_exec,
00161 vircam_darkcor_destroy);
00162
00163 cpl_pluginlist_append(list,plugin);
00164
00165 return(0);
00166 }
00167
00168
00177
00178
00179 static int vircam_darkcor_create(cpl_plugin *plugin) {
00180 cpl_recipe *recipe;
00181 cpl_parameter *p;
00182
00183
00184
00185 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00186 recipe = (cpl_recipe *)plugin;
00187 else
00188 return(-1);
00189
00190
00191
00192 recipe->parameters = cpl_parameterlist_new();
00193
00194
00195
00196 p = cpl_parameter_new_value("vircam.vircam_darkcor.darkscl",
00197 CPL_TYPE_DOUBLE,
00198 "Dark correction scale factor",
00199 "vircam.vircam_darkcor",1.0);
00200 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"darkscl");
00201 cpl_parameterlist_append(recipe->parameters,p);
00202
00203
00204
00205 p = cpl_parameter_new_range("vircam.vircam_darkcor.extenum",
00206 CPL_TYPE_INT,
00207 "Extension number to be done, 0 == all",
00208 "vircam.vircam_darkcor",1,0,16);
00209 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00210 cpl_parameterlist_append(recipe->parameters,p);
00211
00212
00213
00214 return(0);
00215 }
00216
00217
00223
00224
00225 static int vircam_darkcor_exec(cpl_plugin *plugin) {
00226 cpl_recipe *recipe;
00227
00228
00229
00230 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00231 recipe = (cpl_recipe *)plugin;
00232 else
00233 return(-1);
00234
00235 return(vircam_darkcor_test(recipe->parameters,recipe->frames));
00236 }
00237
00238
00244
00245
00246 static int vircam_darkcor_destroy(cpl_plugin *plugin) {
00247 cpl_recipe *recipe ;
00248
00249
00250
00251 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00252 recipe = (cpl_recipe *)plugin;
00253 else
00254 return(-1);
00255
00256 cpl_parameterlist_delete(recipe->parameters);
00257 return(0);
00258 }
00259
00260
00267
00268
00269 static int vircam_darkcor_test(cpl_parameterlist *parlist,
00270 cpl_frameset *framelist) {
00271 const char *fctid="vircam_darkcor";
00272 cpl_parameter *p;
00273 int nlab,jst,jfn,status,j;
00274
00275
00276
00277 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00278 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00279 return(-1);
00280 }
00281
00282
00283
00284 vircam_darkcor_init();
00285
00286
00287
00288 p = cpl_parameterlist_find(parlist,"vircam.vircam_darkcor.darkscl");
00289 vircam_darkcor_config.darkscl = cpl_parameter_get_double(p);
00290 p = cpl_parameterlist_find(parlist,"vircam.vircam_darkcor.extenum");
00291 vircam_darkcor_config.extenum = cpl_parameter_get_int(p);
00292
00293
00294
00295 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00296 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00297 vircam_darkcor_tidy();
00298 return(-1);
00299 }
00300
00301
00302
00303 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00304 &nlab)) == NULL) {
00305 cpl_msg_error(fctid,"Cannot labelise the input frames");
00306 vircam_darkcor_tidy();
00307 return(-1);
00308 }
00309 if ((ps.dark = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00310 VIRCAM_CAL_DARK)) == NULL) {
00311 cpl_msg_info(fctid,"No master dark found -- cannot continue");
00312 vircam_darkcor_tidy();
00313 return(-1);
00314 }
00315 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00316 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00317 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00318 vircam_darkcor_tidy();
00319 return(-1);
00320 }
00321
00322
00323
00324
00325
00326 vircam_exten_range(vircam_darkcor_config.extenum,(const cpl_frame *)ps.img,
00327 &jst,&jfn);
00328 if (jst == -1 || jfn == -1) {
00329 cpl_msg_error(fctid,"Unable to continue");
00330 vircam_darkcor_tidy();
00331 return(-1);
00332 }
00333
00334
00335
00336 status = VIR_OK;
00337 for (j = jst; j <= jfn; j++) {
00338 isfirst = (j == jst);
00339
00340
00341
00342 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00343 ps.darkf = vircam_fits_load(ps.dark,CPL_TYPE_FLOAT,j);
00344 if (ps.img == NULL || ps.darkf == NULL) {
00345 vircam_darkcor_tidy();
00346 return(-1);
00347 }
00348
00349
00350
00351 cpl_msg_info(fctid,"Doing the dark correction for extension %d",j);
00352 (void)vircam_darkcor(ps.imgf,ps.darkf,vircam_darkcor_config.darkscl,
00353 &status);
00354 if (status != VIR_OK) {
00355 vircam_darkcor_tidy();
00356 return(-1);
00357 }
00358
00359
00360
00361 cpl_msg_info(fctid,"Saving results for extension %d",j);
00362 if (vircam_darkcor_save(framelist,parlist) != 0) {
00363 vircam_darkcor_tidy();
00364 return(-1);
00365 }
00366
00367
00368
00369 freefits(ps.imgf);
00370 freefits(ps.darkf);
00371 }
00372 vircam_darkcor_tidy();
00373 return(0);
00374 }
00375
00376
00383
00384
00385 static int vircam_darkcor_save(cpl_frameset *framelist,
00386 cpl_parameterlist *parlist) {
00387 const char *fctid = "vircam_darkcor_save";
00388 const char *outfile = "darkcor.fits";
00389 const char *recipeid = "vircam_darkcor";
00390 cpl_propertylist *plist;
00391
00392
00393
00394
00395 if (isfirst) {
00396
00397
00398
00399 product_frame = cpl_frame_new();
00400 cpl_frame_set_filename(product_frame,outfile);
00401 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00402 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00403 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00404 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00405
00406
00407
00408 plist = vircam_fits_get_phu(ps.imgf);
00409 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00410 parlist,(char *)recipeid,
00411 "?Dictionary?");
00412
00413
00414
00415 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00416 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00417 cpl_msg_error(fctid,"Cannot save product PHU");
00418 cpl_frame_delete(product_frame);
00419 return(-1);
00420 }
00421 cpl_frameset_insert(framelist,product_frame);
00422 }
00423
00424
00425
00426 plist = vircam_fits_get_ehu(ps.imgf);
00427
00428
00429
00430 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00431 (char *)recipeid,"?Dictionary?");
00432
00433
00434
00435 if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_BPP_IEEE_FLOAT,
00436 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00437 cpl_msg_error(fctid,"Cannot save product image extension");
00438 return(-1);
00439 }
00440
00441 return(0);
00442 }
00443
00444
00445
00449
00450
00451 static void vircam_darkcor_init(void) {
00452 ps.labels = NULL;
00453 ps.dark = NULL;
00454 ps.darkf = NULL;
00455 ps.img = NULL;
00456 ps.imgf = NULL;
00457 }
00458
00459
00460
00464
00465
00466 static void vircam_darkcor_tidy(void) {
00467 freespace(ps.labels);
00468 freefits(ps.imgf);
00469 freefits(ps.darkf);
00470 freeframe(ps.dark);
00471 freeframe(ps.img);
00472 }
00473
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515