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_imcombine_create(cpl_plugin *) ;
00047 static int vircam_imcombine_exec(cpl_plugin *) ;
00048 static int vircam_imcombine_destroy(cpl_plugin *) ;
00049 static int vircam_imcombine_test(cpl_parameterlist *, cpl_frameset *) ;
00050 static int vircam_imcombine_save(cpl_frameset *framelist,
00051 cpl_parameterlist *parlist);
00052 static void vircam_imcombine_init(void);
00053 static void vircam_imcombine_tidy(void);
00054
00055
00056
00057 static struct {
00058
00059
00060
00061 int combtype;
00062 int scaletype;
00063 int xrej;
00064 float thresh;
00065 int extenum;
00066
00067 } vircam_imcombine_config;
00068
00069
00070 static struct {
00071 int *labels;
00072 cpl_frameset *imagelist;
00073 vir_fits **images;
00074 int nimages;
00075 cpl_image *outimage;
00076 } ps;
00077
00078 static int isfirst;
00079 static cpl_frame *product_frame = NULL;
00080
00081 static char vircam_imcombine_description[] =
00082 "vircam_imcombine -- VIRCAM test imcombine recipe.\n\n"
00083 "Combine a list of frames into a mean frame.\n\n"
00084 "The program accepts the following files in the SOF:\n\n"
00085 " Tag Description\n"
00086 " -----------------------------------------------------------------------\n"
00087 " %-21s A list of images\n"
00088 "\n";
00089
00146
00147
00148
00156
00157
00158 int cpl_plugin_get_info(cpl_pluginlist *list) {
00159 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00160 cpl_plugin *plugin = &recipe->interface;
00161 char alldesc[SZ_ALLDESC];
00162 (void)snprintf(alldesc,SZ_ALLDESC,vircam_imcombine_description,
00163 VIRCAM_TEST_SCIENCE_RAW);
00164
00165 cpl_plugin_init(plugin,
00166 CPL_PLUGIN_API,
00167 VIRCAM_BINARY_VERSION,
00168 CPL_PLUGIN_TYPE_RECIPE,
00169 "vircam_imcombine",
00170 "VIRCAM test image combination recipe [test]",
00171 alldesc,
00172 "Jim Lewis",
00173 "jrl@ast.cam.ac.uk",
00174 vircam_get_license(),
00175 vircam_imcombine_create,
00176 vircam_imcombine_exec,
00177 vircam_imcombine_destroy);
00178
00179 cpl_pluginlist_append(list,plugin);
00180
00181 return(0);
00182 }
00183
00184
00193
00194
00195 static int vircam_imcombine_create(cpl_plugin *plugin) {
00196 cpl_recipe *recipe;
00197 cpl_parameter *p;
00198
00199
00200
00201 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00202 recipe = (cpl_recipe *)plugin;
00203 else
00204 return(-1);
00205
00206
00207
00208 recipe->parameters = cpl_parameterlist_new();
00209
00210
00211
00212 p = cpl_parameter_new_range("vircam.vircam_imcombine.combtype",
00213 CPL_TYPE_INT,
00214 "1 == Median,\n 2 == Mean",
00215 "vircam.vircam_imcombine",
00216 2,1,2);
00217 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"comb");
00218 cpl_parameterlist_append(recipe->parameters,p);
00219
00220
00221
00222 p = cpl_parameter_new_range("vircam.vircam_imcombine.scaletype",
00223 CPL_TYPE_INT,
00224 "0 == none,\n 1 == additive offset,\n 2 == multiplicative offset,\n 3 == exposure time scaling + additive offset",
00225 "vircam.vircam_imcombine",
00226 1,0,3);
00227 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"scale");
00228 cpl_parameterlist_append(recipe->parameters,p);
00229
00230
00231
00232 p = cpl_parameter_new_value("vircam.vircam_imcombine.xrej",
00233 CPL_TYPE_BOOL,
00234 "True if using extra rejection cycle",
00235 "vircam.vircam_imcombine",
00236 TRUE);
00237 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"xrej");
00238 cpl_parameterlist_append(recipe->parameters,p);
00239
00240
00241
00242 p = cpl_parameter_new_value("vircam.vircam_imcombine.thresh",
00243 CPL_TYPE_DOUBLE,
00244 "Rejection threshold in sigma above background",
00245 "vircam.vircam_imcombine",5.0);
00246 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
00247 cpl_parameterlist_append(recipe->parameters,p);
00248
00249
00250
00251 p = cpl_parameter_new_range("vircam.vircam_imcombine.extenum",
00252 CPL_TYPE_INT,
00253 "Extension number to be done, 0 == all",
00254 "vircam.vircam_imcombine",
00255 1,0,16);
00256 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00257 cpl_parameterlist_append(recipe->parameters,p);
00258
00259
00260
00261 return(0);
00262 }
00263
00264
00265
00271
00272
00273 static int vircam_imcombine_exec(cpl_plugin *plugin) {
00274 cpl_recipe *recipe;
00275
00276
00277
00278 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00279 recipe = (cpl_recipe *)plugin;
00280 else
00281 return(-1);
00282
00283 return(vircam_imcombine_test(recipe->parameters,recipe->frames));
00284 }
00285
00286
00292
00293
00294 static int vircam_imcombine_destroy(cpl_plugin *plugin) {
00295 cpl_recipe *recipe ;
00296
00297
00298
00299 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00300 recipe = (cpl_recipe *)plugin;
00301 else
00302 return(-1);
00303
00304 cpl_parameterlist_delete(recipe->parameters);
00305 return(0);
00306 }
00307
00308
00315
00316
00317 static int vircam_imcombine_test(cpl_parameterlist *parlist,
00318 cpl_frameset *framelist) {
00319 const char *fctid="vircam_imcombine";
00320 int nlab,j,jst,jfn,retval,status;
00321 cpl_parameter *p;
00322 unsigned char *rejmask,*rejplus;
00323 cpl_propertylist *drs;
00324
00325
00326
00327 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00328 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00329 return(-1);
00330 }
00331
00332
00333
00334 vircam_imcombine_init();
00335
00336
00337
00338 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.combtype");
00339 vircam_imcombine_config.combtype = cpl_parameter_get_int(p);
00340 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.scaletype");
00341 vircam_imcombine_config.scaletype = cpl_parameter_get_int(p);
00342 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.xrej");
00343 vircam_imcombine_config.xrej = cpl_parameter_get_bool(p);
00344 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.thresh");
00345 vircam_imcombine_config.thresh = (float)cpl_parameter_get_double(p);
00346 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.extenum");
00347 vircam_imcombine_config.extenum = cpl_parameter_get_int(p);
00348
00349
00350
00351 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00352 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00353 vircam_imcombine_tidy();
00354 return(-1);
00355 }
00356
00357
00358
00359 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00360 &nlab)) == NULL) {
00361 cpl_msg_error(fctid,"Cannot labelise the input frames");
00362 vircam_imcombine_tidy();
00363 return(-1);
00364 }
00365 if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00366 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00367 cpl_msg_error(fctid,"Cannot find images in input frameset");
00368 vircam_imcombine_tidy();
00369 return(-1);
00370 }
00371 ps.nimages = cpl_frameset_get_size(ps.imagelist);
00372
00373
00374
00375
00376
00377 vircam_exten_range(vircam_imcombine_config.extenum,
00378 (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00379 &jst,&jfn);
00380 if (jst == -1 || jfn == -1) {
00381 cpl_msg_error(fctid,"Unable to continue");
00382 vircam_imcombine_tidy();
00383 return(-1);
00384 }
00385
00386
00387
00388 status = VIR_OK;
00389 for (j = jst; j <= jfn; j++) {
00390 isfirst = (j == jst);
00391
00392
00393
00394 ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00395
00396
00397
00398 cpl_msg_info(fctid,"Doing combination for extension %d\n",j);
00399 (void)vircam_imcombine(ps.images,ps.nimages,
00400 vircam_imcombine_config.combtype,
00401 vircam_imcombine_config.scaletype,
00402 vircam_imcombine_config.xrej,
00403 vircam_imcombine_config.thresh,
00404 &(ps.outimage),&rejmask,&rejplus,&drs,&status);
00405 freespace(rejmask);
00406 freespace(rejplus);
00407 freepropertylist(drs);
00408 if (status != VIR_OK) {
00409 vircam_imcombine_tidy();
00410 return(-1);
00411 }
00412
00413
00414
00415 cpl_msg_info(fctid,"Saving combined image extension %d\n",j);
00416 retval = vircam_imcombine_save(framelist,parlist);
00417 if (retval != 0) {
00418 vircam_imcombine_tidy();
00419 return(-1);
00420 }
00421 freefitslist(ps.images,ps.nimages);
00422 freeimage(ps.outimage);
00423 }
00424 vircam_imcombine_tidy();
00425 return(0);
00426 }
00427
00428
00429
00436
00437
00438 static int vircam_imcombine_save(cpl_frameset *framelist,
00439 cpl_parameterlist *parlist) {
00440 cpl_propertylist *plist;
00441 const char *recipeid = "vircam_imcombine";
00442 const char *fctid = "vircam_imcombine_save";
00443 const char *outfile = "comb.fits";
00444
00445
00446
00447
00448 if (isfirst) {
00449
00450
00451
00452 product_frame = cpl_frame_new();
00453 cpl_frame_set_filename(product_frame,outfile);
00454 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00455 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00456 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00457 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00458
00459
00460
00461 plist = vircam_fits_get_phu(ps.images[0]);
00462 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00463 parlist,(char *)recipeid,
00464 "?Dictionary?");
00465
00466
00467 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00468 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00469 cpl_msg_error(fctid,"Cannot save product PHU");
00470 cpl_frame_delete(product_frame);
00471 return(-1);
00472 }
00473 cpl_frameset_insert(framelist,product_frame);
00474 }
00475
00476
00477
00478 plist = vircam_fits_get_ehu(ps.images[0]);
00479
00480
00481
00482 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00483 parlist,(char *)recipeid,
00484 "?Dictionary?");
00485
00486
00487
00488 if (cpl_image_save(ps.outimage,outfile,CPL_BPP_IEEE_FLOAT,plist,
00489 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00490 cpl_msg_error(fctid,"Cannot save product image extension");
00491 return(-1);
00492 }
00493
00494
00495
00496 return(0);
00497 }
00498
00499
00503
00504
00505 static void vircam_imcombine_init(void) {
00506 ps.labels = NULL;
00507 ps.imagelist = NULL;
00508 ps.images = NULL;
00509 ps.outimage = NULL;
00510 }
00511
00512
00516
00517
00518 static void vircam_imcombine_tidy(void) {
00519 freespace(ps.labels);
00520 freeframeset(ps.imagelist);
00521 freefitslist(ps.images,ps.nimages);
00522 freeimage(ps.outimage);
00523 }
00524
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
00563
00564
00565
00566
00567
00568
00569
00570
00571