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 cpl_size *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 jst,jfn,status,j,nx,ny,retval;
00267 cpl_size nlab;
00268
00269
00270
00271 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00272 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00273 return(-1);
00274 }
00275
00276
00277
00278 vircam_destripe_init();
00279
00280
00281
00282 p = cpl_parameterlist_find(parlist,"vircam.vircam_destripe.extenum");
00283 vircam_destripe_config.extenum = cpl_parameter_get_int(p);
00284
00285
00286
00287 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00288 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00289 vircam_destripe_tidy(2);
00290 return(-1);
00291 }
00292
00293
00294
00295 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00296 &nlab)) == NULL) {
00297 cpl_msg_error(fctid,"Cannot labelise the input frames");
00298 vircam_destripe_tidy(2);
00299 return(-1);
00300 }
00301 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00302 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00303 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00304 vircam_destripe_tidy(2);
00305 return(-1);
00306 }
00307
00308
00309
00310 ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
00311
00312
00313
00314
00315
00316 vircam_exten_range(vircam_destripe_config.extenum,ps.img,&jst,&jfn);
00317 if (jst == -1 || jfn == -1) {
00318 cpl_msg_error(fctid,"Unable to continue");
00319 vircam_destripe_tidy(2);
00320 return(-1);
00321 }
00322
00323
00324
00325 for (j = jst; j <= jfn; j++) {
00326 status = VIR_OK;
00327 isfirst = (j == jst);
00328 dummy = 0;
00329
00330
00331
00332 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00333 if (ps.img == NULL) {
00334 cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " frame wouldn't load",
00335 (cpl_size)j);
00336 dummy = 1;
00337 retval = vircam_destripe_lastbit(j,framelist,parlist);
00338 if (retval != 0)
00339 return(-1);
00340 }
00341
00342
00343
00344 nx = (int)cpl_image_get_size_x(vircam_fits_get_image(ps.imgf));
00345 ny = (int)cpl_image_get_size_y(vircam_fits_get_image(ps.imgf));
00346
00347
00348
00349 (void)vircam_mask_load(ps.bpm,j,nx,ny);
00350
00351
00352
00353 cpl_msg_info(fctid,"Doing the stripe correction for extension %" CPL_SIZE_FORMAT,
00354 (cpl_size)j);
00355 (void)vircam_destripe(ps.imgf,ps.bpm,&status);
00356 if (status != VIR_OK) {
00357 cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " destripe failed",
00358 (cpl_size)j);
00359 dummy = 1;
00360 retval = vircam_destripe_lastbit(j,framelist,parlist);
00361 if (retval != 0)
00362 return(-1);
00363 }
00364
00365
00366
00367 retval = vircam_destripe_lastbit(j,framelist,parlist);
00368 if (retval != 0)
00369 return(-1);
00370 }
00371 vircam_destripe_tidy(2);
00372 return(0);
00373 }
00374
00375
00382
00383
00384 static int vircam_destripe_save(cpl_frameset *framelist,
00385 cpl_parameterlist *parlist) {
00386 const char *fctid = "vircam_destripe_save";
00387 const char *outfile = "destripe.fits";
00388 const char *recipeid = "vircam_destripe";
00389 cpl_propertylist *plist;
00390
00391
00392
00393
00394 if (isfirst) {
00395
00396
00397
00398 product_frame = cpl_frame_new();
00399 cpl_frame_set_filename(product_frame,outfile);
00400 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00401 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00402 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00403 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00404
00405
00406
00407 plist = vircam_fits_get_phu(ps.imgf);
00408 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00409 parlist,(char *)recipeid,
00410 "?Dictionary?",NULL,0);
00411
00412
00413
00414 if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00415 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00416 cpl_msg_error(fctid,"Cannot save product PHU");
00417 cpl_frame_delete(product_frame);
00418 return(-1);
00419 }
00420 cpl_frameset_insert(framelist,product_frame);
00421 }
00422
00423
00424
00425 plist = vircam_fits_get_ehu(ps.imgf);
00426
00427
00428
00429 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00430 (char *)recipeid,"?Dictionary?",NULL);
00431 if (dummy)
00432 vircam_dummy_property(plist);
00433
00434
00435
00436 if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
00437 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00438 cpl_msg_error(fctid,"Cannot save product image extension");
00439 return(-1);
00440 }
00441
00442 return(0);
00443 }
00444
00445
00453
00454
00455 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
00456 cpl_parameterlist *parlist) {
00457 int retval;
00458 const char *fctid="vircam_destripe_lastbit";
00459
00460
00461
00462 cpl_msg_info(fctid,"Saving products for extension %" CPL_SIZE_FORMAT,
00463 (cpl_size)jext);
00464 retval = vircam_destripe_save(framelist,parlist);
00465 if (retval != 0) {
00466 vircam_destripe_tidy(2);
00467 return(-1);
00468 }
00469
00470
00471
00472 vircam_destripe_tidy(1);
00473 return(0);
00474 }
00475
00476
00480
00481
00482 static void vircam_destripe_init(void) {
00483 ps.labels = NULL;
00484 ps.img = NULL;
00485 ps.imgf = NULL;
00486 ps.bpm = NULL;
00487 }
00488
00489
00490
00494
00495
00496 static void vircam_destripe_tidy(int level) {
00497 if (level == 1) {
00498 freefits(ps.imgf);
00499 vircam_mask_clear(ps.bpm);
00500 } else {
00501 freefits(ps.imgf);
00502 freemask(ps.bpm);
00503 freespace(ps.labels);
00504 freeframe(ps.img);
00505 }
00506 }
00507
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545