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 cpl_size *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 j,jst,jfn,retval,status;
00274 cpl_size nlab;
00275 cpl_parameter *p;
00276
00277
00278
00279
00280 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00281 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00282 return(-1);
00283 }
00284
00285
00286
00287 vircam_imdither_init();
00288
00289
00290
00291 p = cpl_parameterlist_find(parlist,"vircam.vircam_imdither.extenum");
00292 vircam_imdither_config.extenum = cpl_parameter_get_int(p);
00293
00294
00295
00296 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00297 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00298 vircam_imdither_tidy();
00299 return(-1);
00300 }
00301
00302
00303
00304 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00305 &nlab)) == NULL) {
00306 cpl_msg_error(fctid,"Cannot labelise the input frames");
00307 vircam_imdither_tidy();
00308 return(-1);
00309 }
00310 if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00311 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00312 cpl_msg_error(fctid,"Cannot get images in input frameset");
00313 vircam_imdither_tidy();
00314 return(-1);
00315 }
00316 ps.nimages = cpl_frameset_get_size(ps.imagelist);
00317 if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00318 VIRCAM_CAL_CONF)) == NULL) {
00319 cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
00320 vircam_imdither_tidy();
00321 return(-1);
00322 }
00323 ps.nconfs = cpl_frameset_get_size(ps.conflist);
00324
00325
00326
00327
00328
00329 vircam_exten_range(vircam_imdither_config.extenum,
00330 (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00331 &jst,&jfn);
00332 if (jst == -1 || jfn == -1) {
00333 cpl_msg_error(fctid,"Unable to continue");
00334 vircam_imdither_tidy();
00335 return(-1);
00336 }
00337
00338
00339
00340 status = VIR_OK;
00341 for (j = jst; j <= jfn; j++) {
00342 isfirst = (j == jst);
00343
00344
00345
00346 ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00347 ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
00348
00349
00350
00351 cpl_msg_info(fctid,"Doing jittering for extension %" CPL_SIZE_FORMAT,
00352 (cpl_size)j);
00353 (void)vircam_imdither(ps.images,ps.confs,ps.nimages,ps.nconfs,
00354 5.0,5.0,&(ps.plist),&(ps.outimage),
00355 &(ps.outconf),&status);
00356 if (status != VIR_OK) {
00357 vircam_imdither_tidy();
00358 return(-1);
00359 }
00360
00361
00362
00363 cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
00364 (cpl_size)j);
00365 retval = vircam_imdither_save(framelist,parlist);
00366 if (retval != 0) {
00367 vircam_imdither_tidy();
00368 return(-1);
00369 }
00370 freefitslist(ps.images,ps.nimages);
00371 freefitslist(ps.confs,ps.nconfs);
00372 freeimage(ps.outimage);
00373 freeimage(ps.outconf);
00374 freepropertylist(ps.plist);
00375 }
00376 vircam_imdither_tidy();
00377 return(0);
00378 }
00379
00380
00381
00388
00389
00390 static int vircam_imdither_save(cpl_frameset *framelist,
00391 cpl_parameterlist *parlist) {
00392 cpl_propertylist *plist;
00393 const char *recipeid = "vircam_imdither";
00394 const char *fctid = "vircam_imdither_save";
00395 const char *outfile = "comb.fits";
00396 const char *outconf = "combconf.fits";
00397
00398
00399
00400
00401 if (isfirst) {
00402
00403
00404
00405 product_frame = cpl_frame_new();
00406 cpl_frame_set_filename(product_frame,outfile);
00407 cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
00408 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00409 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00410 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00411
00412
00413
00414 plist = vircam_fits_get_phu(ps.images[0]);
00415 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00416 parlist,(char *)recipeid,
00417 "?Dictionary?",NULL,0);
00418
00419
00420
00421 if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00422 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00423 cpl_msg_error(fctid,"Cannot save product PHU");
00424 cpl_frame_delete(product_frame);
00425 return(-1);
00426 }
00427 cpl_frameset_insert(framelist,product_frame);
00428
00429
00430
00431 product_conf = cpl_frame_new();
00432 cpl_frame_set_filename(product_conf,outconf);
00433 cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
00434 cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
00435 cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
00436 cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
00437
00438
00439
00440 if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
00441 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00442 cpl_msg_error(fctid,"Cannot save product PHU");
00443 cpl_frame_delete(product_conf);
00444 return(-1);
00445 }
00446 cpl_frameset_insert(framelist,product_conf);
00447 }
00448
00449
00450
00451 plist = ps.plist;
00452
00453
00454
00455 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00456 parlist,(char *)recipeid,
00457 "?Dictionary?",NULL);
00458
00459
00460
00461 if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
00462 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00463 cpl_msg_error(fctid,"Cannot save dithered image extension");
00464 return(-1);
00465 }
00466
00467
00468
00469 if (cpl_image_save(ps.outconf,outconf,CPL_TYPE_SHORT,plist,
00470 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00471 cpl_msg_error(fctid,"Cannot save confidence map image extension");
00472 return(-1);
00473 }
00474
00475
00476
00477 return(0);
00478 }
00479
00480
00484
00485
00486 static void vircam_imdither_init(void) {
00487 ps.labels = NULL;
00488 ps.imagelist = NULL;
00489 ps.images = NULL;
00490 ps.conflist = NULL;
00491 ps.confs = NULL;
00492 ps.outimage = NULL;
00493 ps.outconf = NULL;
00494 ps.plist = NULL;
00495 }
00496
00497
00501
00502
00503 static void vircam_imdither_tidy(void) {
00504 freespace(ps.labels);
00505 freeframeset(ps.imagelist);
00506 freefitslist(ps.images,ps.nimages);
00507 freeframeset(ps.conflist);
00508 freefitslist(ps.confs,ps.nconfs);
00509 freeimage(ps.outimage);
00510 freeimage(ps.outconf);
00511 freepropertylist(ps.plist);
00512 }
00513
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
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562