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_defringe_create(cpl_plugin *) ;
00045 static int vircam_defringe_exec(cpl_plugin *) ;
00046 static int vircam_defringe_destroy(cpl_plugin *) ;
00047 static int vircam_defringe_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_defringe_save(cpl_frameset *framelist,
00049 cpl_parameterlist *parlist);
00050 static int vircam_defringe_lastbit(int jext, cpl_frameset *framelist,
00051 cpl_parameterlist *parlist);
00052 static void vircam_defringe_init(void);
00053 static void vircam_defringe_tidy(int level);
00054
00055 static struct {
00056
00057
00058
00059 int nbsize;
00060 int extenum;
00061
00062 } vircam_defringe_config;
00063
00064 static struct {
00065 cpl_size *labels;
00066 cpl_frame *fringe;
00067 cpl_frame *img;
00068 vir_fits *fringef;
00069 vir_fits *imgf;
00070 vir_mask *bpm;
00071 } ps;
00072
00073 static int isfirst;
00074 static int dummy;
00075 static cpl_frame *product_frame = NULL;
00076
00077 static char vircam_defringe_description[] =
00078 "vircam_defringe -- VIRCAM defringe test recipe.\n\n"
00079 "Defringe an input frame using a master fringe frame\n\n"
00080 "The program accepts the following files in the SOF:\n\n"
00081 " Tag Description\n"
00082 " -----------------------------------------------------------------------\n"
00083 " %-21s A input uncorrected image\n"
00084 " %-21s A master fringe frame\n"
00085 " %-21s Optional master bad pixel map or\n"
00086 " %-21s Optional master confidence map\n"
00087 "\n";
00088
00135
00136
00137
00138
00146
00147
00148 int cpl_plugin_get_info(cpl_pluginlist *list) {
00149 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00150 cpl_plugin *plugin = &recipe->interface;
00151 char alldesc[SZ_ALLDESC];
00152 (void)snprintf(alldesc,SZ_ALLDESC,vircam_defringe_description,
00153 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_FRINGE,VIRCAM_CAL_BPM,
00154 VIRCAM_CAL_CONF);
00155
00156 cpl_plugin_init(plugin,
00157 CPL_PLUGIN_API,
00158 VIRCAM_BINARY_VERSION,
00159 CPL_PLUGIN_TYPE_RECIPE,
00160 "vircam_defringe",
00161 "VIRCAM fringe correction test recipe [test]",
00162 alldesc,
00163 "Jim Lewis",
00164 "jrl@ast.cam.ac.uk",
00165 vircam_get_license(),
00166 vircam_defringe_create,
00167 vircam_defringe_exec,
00168 vircam_defringe_destroy);
00169
00170 cpl_pluginlist_append(list,plugin);
00171
00172 return(0);
00173 }
00174
00175
00184
00185
00186 static int vircam_defringe_create(cpl_plugin *plugin) {
00187 cpl_recipe *recipe;
00188 cpl_parameter *p;
00189
00190
00191
00192 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00193 recipe = (cpl_recipe *)plugin;
00194 else
00195 return(-1);
00196
00197
00198
00199 recipe->parameters = cpl_parameterlist_new();
00200
00201
00202
00203 p = cpl_parameter_new_value("vircam.vircam_defringe.nbsize",
00204 CPL_TYPE_INT,
00205 "Cell size in pixels",
00206 "vircam.vircam_defringe",128);
00207 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nbsize");
00208 cpl_parameterlist_append(recipe->parameters,p);
00209
00210
00211
00212 p = cpl_parameter_new_range("vircam.vircam_defringe.extenum",
00213 CPL_TYPE_INT,
00214 "Extension number to be done, 0 == all",
00215 "vircam.vircam_defringe",1,0,16);
00216 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00217 cpl_parameterlist_append(recipe->parameters,p);
00218
00219
00220
00221 return(0);
00222 }
00223
00224
00230
00231
00232 static int vircam_defringe_exec(cpl_plugin *plugin) {
00233 cpl_recipe *recipe;
00234
00235
00236
00237 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00238 recipe = (cpl_recipe *)plugin;
00239 else
00240 return(-1);
00241
00242 return(vircam_defringe_test(recipe->parameters,recipe->frames));
00243 }
00244
00245
00251
00252
00253 static int vircam_defringe_destroy(cpl_plugin *plugin) {
00254 cpl_recipe *recipe ;
00255
00256
00257
00258 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00259 recipe = (cpl_recipe *)plugin;
00260 else
00261 return(-1);
00262
00263 cpl_parameterlist_delete(recipe->parameters);
00264 return(0);
00265 }
00266
00267
00274
00275
00276 static int vircam_defringe_test(cpl_parameterlist *parlist,
00277 cpl_frameset *framelist) {
00278 const char *fctid="vircam_defringe";
00279 cpl_parameter *p;
00280 int jst,jfn,status,j,retval,nx,ny;
00281 cpl_size nlab;
00282
00283
00284
00285 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00286 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00287 return(-1);
00288 }
00289
00290
00291
00292 vircam_defringe_init();
00293
00294
00295
00296 p = cpl_parameterlist_find(parlist,"vircam.vircam_defringe.nbsize");
00297 vircam_defringe_config.nbsize = cpl_parameter_get_int(p);
00298 p = cpl_parameterlist_find(parlist,"vircam.vircam_defringe.extenum");
00299 vircam_defringe_config.extenum = cpl_parameter_get_int(p);
00300
00301
00302
00303 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00304 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00305 vircam_defringe_tidy(2);
00306 return(-1);
00307 }
00308
00309
00310
00311 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00312 &nlab)) == NULL) {
00313 cpl_msg_error(fctid,"Cannot labelise the input frames");
00314 vircam_defringe_tidy(2);
00315 return(-1);
00316 }
00317 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00318 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00319 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00320 vircam_defringe_tidy(2);
00321 return(-1);
00322 }
00323 if ((ps.fringe = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00324 VIRCAM_CAL_FRINGE)) == NULL) {
00325 cpl_msg_info(fctid,"No fringe image found -- cannot continue");
00326 vircam_defringe_tidy(2);
00327 return(-1);
00328 }
00329
00330
00331
00332 ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
00333
00334
00335
00336
00337
00338 vircam_exten_range(vircam_defringe_config.extenum,(const cpl_frame *)ps.img,
00339 &jst,&jfn);
00340 if (jst == -1 || jfn == -1) {
00341 cpl_msg_error(fctid,"Unable to continue");
00342 vircam_defringe_tidy(2);
00343 return(-1);
00344 }
00345
00346
00347
00348 status = VIR_OK;
00349 for (j = jst; j <= jfn; j++) {
00350 isfirst = (j == jst);
00351
00352
00353
00354 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00355 ps.fringef = vircam_fits_load(ps.fringe,CPL_TYPE_FLOAT,j);
00356 if (ps.img == NULL || ps.fringef == NULL) {
00357 cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " frame wouldn't load",
00358 (cpl_size)j);
00359 dummy = 1;
00360 retval = vircam_defringe_lastbit(j,framelist,parlist);
00361 if (retval != 0)
00362 return(-1);
00363 }
00364
00365
00366
00367 nx = (int)cpl_image_get_size_x(vircam_fits_get_image(ps.imgf));
00368 ny = (int)cpl_image_get_size_y(vircam_fits_get_image(ps.imgf));
00369
00370
00371
00372 (void)vircam_mask_load(ps.bpm,j,nx,ny);
00373
00374
00375
00376 cpl_msg_info(fctid,"Doing the fringe correction for extension %" CPL_SIZE_FORMAT,
00377 (cpl_size)j);
00378 (void)vircam_defringe(&(ps.imgf),1,&(ps.fringef),1,ps.bpm,
00379 vircam_defringe_config.nbsize,&status);
00380 if (status != VIR_OK) {
00381 cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " defringe failed",
00382 (cpl_size)j);
00383 dummy = 1;
00384 retval = vircam_defringe_lastbit(j,framelist,parlist);
00385 if (retval != 0)
00386 return(-1);
00387 }
00388
00389
00390
00391 retval = vircam_defringe_lastbit(j,framelist,parlist);
00392 if (retval != 0)
00393 return(-1);
00394 }
00395 vircam_defringe_tidy(2);
00396 return(0);
00397 }
00398
00399
00406
00407
00408 static int vircam_defringe_save(cpl_frameset *framelist,
00409 cpl_parameterlist *parlist) {
00410 const char *fctid = "vircam_defringe_save";
00411 const char *outfile = "defringe.fits";
00412 const char *recipeid = "vircam_defringe";
00413 cpl_propertylist *plist;
00414
00415
00416
00417
00418 if (isfirst) {
00419
00420
00421
00422 product_frame = cpl_frame_new();
00423 cpl_frame_set_filename(product_frame,outfile);
00424 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00425 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00426 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00427 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00428
00429
00430
00431 plist = vircam_fits_get_phu(ps.imgf);
00432 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00433 parlist,(char *)recipeid,
00434 "?Dictionary?",NULL,0);
00435
00436
00437
00438 if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00439 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00440 cpl_msg_error(fctid,"Cannot save product PHU");
00441 cpl_frame_delete(product_frame);
00442 return(-1);
00443 }
00444 cpl_frameset_insert(framelist,product_frame);
00445 }
00446
00447
00448
00449 plist = vircam_fits_get_ehu(ps.imgf);
00450
00451
00452
00453 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00454 (char *)recipeid,"?Dictionary?",NULL);
00455 if (dummy)
00456 vircam_dummy_property(plist);
00457
00458
00459
00460 if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
00461 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00462 cpl_msg_error(fctid,"Cannot save product image extension");
00463 return(-1);
00464 }
00465
00466 return(0);
00467 }
00468
00469
00477
00478
00479 static int vircam_defringe_lastbit(int jext, cpl_frameset *framelist,
00480 cpl_parameterlist *parlist) {
00481 int retval;
00482 const char *fctid="vircam_defringe_lastbit";
00483
00484
00485
00486 cpl_msg_info(fctid,"Saving products for extension %" CPL_SIZE_FORMAT,
00487 (cpl_size)jext);
00488 retval = vircam_defringe_save(framelist,parlist);
00489 if (retval != 0) {
00490 vircam_defringe_tidy(2);
00491 return(-1);
00492 }
00493
00494
00495
00496 vircam_defringe_tidy(1);
00497 return(0);
00498 }
00499
00500
00501
00505
00506
00507 static void vircam_defringe_init(void) {
00508 ps.labels = NULL;
00509 ps.fringe = NULL;
00510 ps.fringef = NULL;
00511 ps.img = NULL;
00512 ps.imgf = NULL;
00513 }
00514
00515
00516
00520
00521
00522 static void vircam_defringe_tidy(int level) {
00523 freefits(ps.imgf);
00524 freefits(ps.fringef);
00525 vircam_mask_clear(ps.bpm);
00526 if (level == 1)
00527 return;
00528 freemask(ps.bpm);
00529 freeframe(ps.img);
00530 freeframe(ps.fringe);
00531 freespace(ps.labels);
00532 }
00533
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571