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_destripe_create(cpl_plugin *) ;
00045 static int vircam_destripe_exec(cpl_plugin *) ;
00046 static int vircam_destripe_destroy(cpl_plugin *) ;
00047 static int vircam_destripe_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_destripe_save(cpl_frameset *framelist,
00049 cpl_parameterlist *parlist);
00050 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
00051 cpl_parameterlist *parlist);
00052 static void vircam_destripe_init(void);
00053 static void vircam_destripe_tidy(int level);
00054
00055 static struct {
00056
00057
00058
00059 int extenum;
00060
00061 } vircam_destripe_config;
00062
00063 static struct {
00064 int *labels;
00065 cpl_frame *img;
00066 vir_fits *imgf;
00067 vir_mask *bpm;
00068 } ps;
00069
00070 static int isfirst;
00071 static int dummy;
00072 static cpl_frame *product_frame = NULL;
00073
00074
00075 static char vircam_destripe_description[] =
00076 "vircam_destripe -- VIRCAM destripe correction test recipe.\n\n"
00077 "Destripe an input frame.\n\n"
00078 "The program accepts the following files in the SOF:\n\n"
00079 " Tag Description\n"
00080 " -----------------------------------------------------------------------\n"
00081 " %-21s A input uncorrected image\n"
00082 " %-21s Optional master bad pixel map or\n"
00083 " %-21s Optional master confidence map\n"
00084 "\n";
00085
00131
00132
00133
00134
00142
00143
00144 int cpl_plugin_get_info(cpl_pluginlist *list) {
00145 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00146 cpl_plugin *plugin = &recipe->interface;
00147 char alldesc[SZ_ALLDESC];
00148 (void)snprintf(alldesc,SZ_ALLDESC,vircam_destripe_description,
00149 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_BPM,VIRCAM_CAL_CONF);
00150
00151 cpl_plugin_init(plugin,
00152 CPL_PLUGIN_API,
00153 VIRCAM_BINARY_VERSION,
00154 CPL_PLUGIN_TYPE_RECIPE,
00155 "vircam_destripe",
00156 "VIRCAM destripe correction test recipe [test]",
00157 alldesc,
00158 "Jim Lewis",
00159 "jrl@ast.cam.ac.uk",
00160 vircam_get_license(),
00161 vircam_destripe_create,
00162 vircam_destripe_exec,
00163 vircam_destripe_destroy);
00164
00165 cpl_pluginlist_append(list,plugin);
00166
00167 return(0);
00168 }
00169
00170
00179
00180
00181 static int vircam_destripe_create(cpl_plugin *plugin) {
00182 cpl_recipe *recipe;
00183 cpl_parameter *p;
00184
00185
00186
00187 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00188 recipe = (cpl_recipe *)plugin;
00189 else
00190 return(-1);
00191
00192
00193
00194 recipe->parameters = cpl_parameterlist_new();
00195
00196
00197
00198 p = cpl_parameter_new_range("vircam.vircam_destripe.extenum",
00199 CPL_TYPE_INT,
00200 "Extension number to be done, 0 == all",
00201 "vircam.vircam_destripe",1,0,16);
00202 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00203 cpl_parameterlist_append(recipe->parameters,p);
00204
00205
00206
00207 return(0);
00208 }
00209
00210
00216
00217
00218 static int vircam_destripe_exec(cpl_plugin *plugin) {
00219 cpl_recipe *recipe;
00220
00221
00222
00223 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00224 recipe = (cpl_recipe *)plugin;
00225 else
00226 return(-1);
00227
00228 return(vircam_destripe_test(recipe->parameters,recipe->frames));
00229 }
00230
00231
00237
00238
00239 static int vircam_destripe_destroy(cpl_plugin *plugin) {
00240 cpl_recipe *recipe ;
00241
00242
00243
00244 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00245 recipe = (cpl_recipe *)plugin;
00246 else
00247 return(-1);
00248
00249 cpl_parameterlist_delete(recipe->parameters);
00250 return(0);
00251 }
00252
00253
00260
00261
00262 static int vircam_destripe_test(cpl_parameterlist *parlist,
00263 cpl_frameset *framelist) {
00264 const char *fctid="vircam_destripe";
00265 cpl_parameter *p;
00266 int nlab,jst,jfn,status,j,nx,ny,retval;
00267
00268
00269
00270 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00271 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00272 return(-1);
00273 }
00274
00275
00276
00277 vircam_destripe_init();
00278
00279
00280
00281 p = cpl_parameterlist_find(parlist,"vircam.vircam_destripe.extenum");
00282 vircam_destripe_config.extenum = cpl_parameter_get_int(p);
00283
00284
00285
00286 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00287 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00288 vircam_destripe_tidy(2);
00289 return(-1);
00290 }
00291
00292
00293
00294 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00295 &nlab)) == NULL) {
00296 cpl_msg_error(fctid,"Cannot labelise the input frames");
00297 vircam_destripe_tidy(2);
00298 return(-1);
00299 }
00300 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00301 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00302 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00303 vircam_destripe_tidy(2);
00304 return(-1);
00305 }
00306
00307
00308
00309 ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
00310
00311
00312
00313
00314
00315 vircam_exten_range(vircam_destripe_config.extenum,ps.img,&jst,&jfn);
00316 if (jst == -1 || jfn == -1) {
00317 cpl_msg_error(fctid,"Unable to continue");
00318 vircam_destripe_tidy(2);
00319 return(-1);
00320 }
00321
00322
00323
00324 for (j = jst; j <= jfn; j++) {
00325 status = VIR_OK;
00326 isfirst = (j == jst);
00327 dummy = 0;
00328
00329
00330
00331 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00332 if (ps.img == NULL) {
00333 cpl_msg_info(fctid,"Extension %d frame wouldn't load",j);
00334 dummy = 1;
00335 retval = vircam_destripe_lastbit(j,framelist,parlist);
00336 if (retval != 0)
00337 return(-1);
00338 }
00339
00340
00341
00342 nx = cpl_image_get_size_x(vircam_fits_get_image(ps.imgf));
00343 ny = cpl_image_get_size_y(vircam_fits_get_image(ps.imgf));
00344
00345
00346
00347 (void)vircam_mask_load(ps.bpm,j,nx,ny);
00348
00349
00350
00351 cpl_msg_info(fctid,"Doing the stripe correction for extension %d",j);
00352 (void)vircam_destripe(ps.imgf,ps.bpm,&status);
00353 if (status != VIR_OK) {
00354 cpl_msg_info(fctid,"Extension %d destripe failed",j);
00355 dummy = 1;
00356 retval = vircam_destripe_lastbit(j,framelist,parlist);
00357 if (retval != 0)
00358 return(-1);
00359 }
00360
00361
00362
00363 retval = vircam_destripe_lastbit(j,framelist,parlist);
00364 if (retval != 0)
00365 return(-1);
00366 }
00367 vircam_destripe_tidy(2);
00368 return(0);
00369 }
00370
00371
00378
00379
00380 static int vircam_destripe_save(cpl_frameset *framelist,
00381 cpl_parameterlist *parlist) {
00382 const char *fctid = "vircam_destripe_save";
00383 const char *outfile = "destripe.fits";
00384 const char *recipeid = "vircam_destripe";
00385 cpl_propertylist *plist;
00386
00387
00388
00389
00390 if (isfirst) {
00391
00392
00393
00394 product_frame = cpl_frame_new();
00395 cpl_frame_set_filename(product_frame,outfile);
00396 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00397 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00398 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00399 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00400
00401
00402
00403 plist = vircam_fits_get_phu(ps.imgf);
00404 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00405 parlist,(char *)recipeid,
00406 "?Dictionary?");
00407
00408
00409
00410 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00411 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00412 cpl_msg_error(fctid,"Cannot save product PHU");
00413 cpl_frame_delete(product_frame);
00414 return(-1);
00415 }
00416 cpl_frameset_insert(framelist,product_frame);
00417 }
00418
00419
00420
00421 plist = vircam_fits_get_ehu(ps.imgf);
00422
00423
00424
00425 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00426 (char *)recipeid,"?Dictionary?");
00427 if (dummy)
00428 vircam_dummy_property(plist);
00429
00430
00431
00432 if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_BPP_IEEE_FLOAT,
00433 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00434 cpl_msg_error(fctid,"Cannot save product image extension");
00435 return(-1);
00436 }
00437
00438 return(0);
00439 }
00440
00441
00449
00450
00451 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
00452 cpl_parameterlist *parlist) {
00453 int retval;
00454 const char *fctid="vircam_destripe_lastbit";
00455
00456
00457
00458 cpl_msg_info(fctid,"Saving products for extension %d",jext);
00459 retval = vircam_destripe_save(framelist,parlist);
00460 if (retval != 0) {
00461 vircam_destripe_tidy(2);
00462 return(-1);
00463 }
00464
00465
00466
00467 vircam_destripe_tidy(1);
00468 return(0);
00469 }
00470
00471
00475
00476
00477 static void vircam_destripe_init(void) {
00478 ps.labels = NULL;
00479 ps.img = NULL;
00480 ps.imgf = NULL;
00481 ps.bpm = NULL;
00482 }
00483
00484
00485
00489
00490
00491 static void vircam_destripe_tidy(int level) {
00492 if (level == 1) {
00493 freefits(ps.imgf);
00494 vircam_mask_clear(ps.bpm);
00495 } else {
00496 freefits(ps.imgf);
00497 freemask(ps.bpm);
00498 freespace(ps.labels);
00499 freeframe(ps.img);
00500 }
00501 }
00502
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531