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 #include <math.h>
00037
00038 #include "vircam_utils.h"
00039 #include "vircam_mask.h"
00040 #include "vircam_dfs.h"
00041 #include "vircam_mods.h"
00042 #include "vircam_fits.h"
00043
00044
00045
00046 static int vircam_imdither_create(cpl_plugin *) ;
00047 static int vircam_imdither_exec(cpl_plugin *) ;
00048 static int vircam_imdither_destroy(cpl_plugin *) ;
00049 static int vircam_imdither_test(cpl_parameterlist *, cpl_frameset *) ;
00050 static int vircam_imdither_save(cpl_frameset *framelist,
00051 cpl_parameterlist *parlist);
00052 static void vircam_imdither_init(void);
00053 static void vircam_imdither_tidy(void);
00054
00055
00056
00057 static struct {
00058
00059
00060
00061 int extenum;
00062
00063 } vircam_imdither_config;
00064
00065
00066 static struct {
00067 int *labels;
00068 cpl_frameset *imagelist;
00069 vir_fits **images;
00070 cpl_frameset *conflist;
00071 vir_fits **confs;
00072 int nimages;
00073 int nconfs;
00074 cpl_image *outimage;
00075 cpl_image *outconf;
00076 cpl_propertylist *plist;
00077 } ps;
00078
00079 static int isfirst;
00080 static cpl_frame *product_frame = NULL;
00081 static cpl_frame *product_conf = NULL;
00082
00083
00084 static char vircam_imdither_description[] =
00085 "vircam_imdither -- VIRCAM test jitter recipe.\n\n"
00086 "Dither a list of frames into an output frame.\n\n"
00087 "The program accepts the following files in the SOF:\n\n"
00088 " Tag Description\n"
00089 " -----------------------------------------------------------------------\n"
00090 " %-21s A list of images\n"
00091 " %-21s A list of confidence maps\n"
00092 "\n";
00093
00138
00139
00140
00148
00149
00150 int cpl_plugin_get_info(cpl_pluginlist *list) {
00151 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00152 cpl_plugin *plugin = &recipe->interface;
00153 char alldesc[SZ_ALLDESC];
00154 (void)snprintf(alldesc,SZ_ALLDESC,vircam_imdither_description,
00155 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
00156
00157 cpl_plugin_init(plugin,
00158 CPL_PLUGIN_API,
00159 VIRCAM_BINARY_VERSION,
00160 CPL_PLUGIN_TYPE_RECIPE,
00161 "vircam_imdither",
00162 "VIRCAM jitter test recipe [test]",
00163 alldesc,
00164 "Jim Lewis",
00165 "jrl@ast.cam.ac.uk",
00166 vircam_get_license(),
00167 vircam_imdither_create,
00168 vircam_imdither_exec,
00169 vircam_imdither_destroy);
00170
00171 cpl_pluginlist_append(list,plugin);
00172
00173 return(0);
00174 }
00175
00176
00185
00186
00187 static int vircam_imdither_create(cpl_plugin *plugin) {
00188 cpl_recipe *recipe;
00189 cpl_parameter *p;
00190
00191
00192
00193 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00194 recipe = (cpl_recipe *)plugin;
00195 else
00196 return(-1);
00197
00198
00199
00200 recipe->parameters = cpl_parameterlist_new();
00201
00202
00203
00204 p = cpl_parameter_new_range("vircam.vircam_imdither.extenum",
00205 CPL_TYPE_INT,
00206 "Extension number to be done, 0 == all",
00207 "vircam.vircam_imdither",
00208 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
00218
00224
00225
00226 static int vircam_imdither_exec(cpl_plugin *plugin) {
00227 cpl_recipe *recipe;
00228
00229
00230
00231 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00232 recipe = (cpl_recipe *)plugin;
00233 else
00234 return(-1);
00235
00236 return(vircam_imdither_test(recipe->parameters,recipe->frames));
00237 }
00238
00239
00245
00246
00247 static int vircam_imdither_destroy(cpl_plugin *plugin) {
00248 cpl_recipe *recipe ;
00249
00250
00251
00252 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00253 recipe = (cpl_recipe *)plugin;
00254 else
00255 return(-1);
00256
00257 cpl_parameterlist_delete(recipe->parameters);
00258 return(0);
00259 }
00260
00261
00268
00269
00270 static int vircam_imdither_test(cpl_parameterlist *parlist,
00271 cpl_frameset *framelist) {
00272 const char *fctid="vircam_imdither";
00273 int nlab,j,jst,jfn,retval,status;
00274 cpl_parameter *p;
00275
00276
00277
00278
00279 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00280 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00281 return(-1);
00282 }
00283
00284
00285
00286 vircam_imdither_init();
00287
00288
00289
00290 p = cpl_parameterlist_find(parlist,"vircam.vircam_imdither.extenum");
00291 vircam_imdither_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_imdither_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_imdither_tidy();
00307 return(-1);
00308 }
00309 if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00310 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00311 cpl_msg_error(fctid,"Cannot get images in input frameset");
00312 vircam_imdither_tidy();
00313 return(-1);
00314 }
00315 ps.nimages = cpl_frameset_get_size(ps.imagelist);
00316 if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00317 VIRCAM_CAL_CONF)) == NULL) {
00318 cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
00319 vircam_imdither_tidy();
00320 return(-1);
00321 }
00322 ps.nconfs = cpl_frameset_get_size(ps.conflist);
00323
00324
00325
00326
00327
00328 vircam_exten_range(vircam_imdither_config.extenum,
00329 (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00330 &jst,&jfn);
00331 if (jst == -1 || jfn == -1) {
00332 cpl_msg_error(fctid,"Unable to continue");
00333 vircam_imdither_tidy();
00334 return(-1);
00335 }
00336
00337
00338
00339 status = VIR_OK;
00340 for (j = jst; j <= jfn; j++) {
00341 isfirst = (j == jst);
00342
00343
00344
00345 ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00346 ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
00347
00348
00349
00350 cpl_msg_info(fctid,"Doing jittering for extension %d\n",j);
00351 (void)vircam_imdither(ps.images,ps.confs,ps.nimages,ps.nconfs,
00352 5.0,5.0,&(ps.plist),&(ps.outimage),
00353 &(ps.outconf),&status);
00354 if (status != VIR_OK) {
00355 vircam_imdither_tidy();
00356 return(-1);
00357 }
00358
00359
00360
00361 cpl_msg_info(fctid,"Saving combined image extension %d\n",j);
00362 retval = vircam_imdither_save(framelist,parlist);
00363 if (retval != 0) {
00364 vircam_imdither_tidy();
00365 return(-1);
00366 }
00367 freefitslist(ps.images,ps.nimages);
00368 freefitslist(ps.confs,ps.nconfs);
00369 freeimage(ps.outimage);
00370 freeimage(ps.outconf);
00371 freepropertylist(ps.plist);
00372 }
00373 vircam_imdither_tidy();
00374 return(0);
00375 }
00376
00377
00378
00385
00386
00387 static int vircam_imdither_save(cpl_frameset *framelist,
00388 cpl_parameterlist *parlist) {
00389 cpl_propertylist *plist;
00390 const char *recipeid = "vircam_imdither";
00391 const char *fctid = "vircam_imdither_save";
00392 const char *outfile = "comb.fits";
00393 const char *outconf = "combconf.fits";
00394
00395
00396
00397
00398 if (isfirst) {
00399
00400
00401
00402 product_frame = cpl_frame_new();
00403 cpl_frame_set_filename(product_frame,outfile);
00404 cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
00405 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00406 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00407 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00408
00409
00410
00411 plist = vircam_fits_get_phu(ps.images[0]);
00412 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00413 parlist,(char *)recipeid,
00414 "?Dictionary?");
00415
00416
00417
00418 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00419 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00420 cpl_msg_error(fctid,"Cannot save product PHU");
00421 cpl_frame_delete(product_frame);
00422 return(-1);
00423 }
00424 cpl_frameset_insert(framelist,product_frame);
00425
00426
00427
00428 product_conf = cpl_frame_new();
00429 cpl_frame_set_filename(product_conf,outconf);
00430 cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
00431 cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
00432 cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
00433 cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
00434
00435
00436
00437 if (cpl_image_save(NULL,outconf,CPL_BPP_8_UNSIGNED,plist,
00438 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00439 cpl_msg_error(fctid,"Cannot save product PHU");
00440 cpl_frame_delete(product_conf);
00441 return(-1);
00442 }
00443 cpl_frameset_insert(framelist,product_conf);
00444 }
00445
00446
00447
00448 plist = ps.plist;
00449
00450
00451
00452 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00453 parlist,(char *)recipeid,
00454 "?Dictionary?");
00455
00456
00457
00458 if (cpl_image_save(ps.outimage,outfile,CPL_BPP_IEEE_FLOAT,plist,
00459 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00460 cpl_msg_error(fctid,"Cannot save dithered image extension");
00461 return(-1);
00462 }
00463
00464
00465
00466 if (cpl_image_save(ps.outconf,outconf,CPL_BPP_16_SIGNED,plist,
00467 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00468 cpl_msg_error(fctid,"Cannot save confidence map image extension");
00469 return(-1);
00470 }
00471
00472
00473
00474 return(0);
00475 }
00476
00477
00481
00482
00483 static void vircam_imdither_init(void) {
00484 ps.labels = NULL;
00485 ps.imagelist = NULL;
00486 ps.images = NULL;
00487 ps.conflist = NULL;
00488 ps.confs = NULL;
00489 ps.outimage = NULL;
00490 ps.outconf = NULL;
00491 ps.plist = NULL;
00492 }
00493
00494
00498
00499
00500 static void vircam_imdither_tidy(void) {
00501 freespace(ps.labels);
00502 freeframeset(ps.imagelist);
00503 freefitslist(ps.images,ps.nimages);
00504 freeframeset(ps.conflist);
00505 freefitslist(ps.confs,ps.nconfs);
00506 freeimage(ps.outimage);
00507 freeimage(ps.outconf);
00508 freepropertylist(ps.plist);
00509 }
00510
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
00546
00547
00548
00549
00550