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 cpl_size *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 j,jst,jfn,retval,status;
00321 cpl_size nlab;
00322 cpl_parameter *p;
00323 unsigned char *rejmask,*rejplus;
00324 cpl_propertylist *drs;
00325
00326
00327
00328 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00329 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00330 return(-1);
00331 }
00332
00333
00334
00335 vircam_imcombine_init();
00336
00337
00338
00339 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.combtype");
00340 vircam_imcombine_config.combtype = cpl_parameter_get_int(p);
00341 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.scaletype");
00342 vircam_imcombine_config.scaletype = cpl_parameter_get_int(p);
00343 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.xrej");
00344 vircam_imcombine_config.xrej = cpl_parameter_get_bool(p);
00345 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.thresh");
00346 vircam_imcombine_config.thresh = (float)cpl_parameter_get_double(p);
00347 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.extenum");
00348 vircam_imcombine_config.extenum = cpl_parameter_get_int(p);
00349
00350
00351
00352 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00353 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00354 vircam_imcombine_tidy();
00355 return(-1);
00356 }
00357
00358
00359
00360 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00361 &nlab)) == NULL) {
00362 cpl_msg_error(fctid,"Cannot labelise the input frames");
00363 vircam_imcombine_tidy();
00364 return(-1);
00365 }
00366 if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00367 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00368 cpl_msg_error(fctid,"Cannot find images in input frameset");
00369 vircam_imcombine_tidy();
00370 return(-1);
00371 }
00372 ps.nimages = cpl_frameset_get_size(ps.imagelist);
00373
00374
00375
00376
00377
00378 vircam_exten_range(vircam_imcombine_config.extenum,
00379 (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00380 &jst,&jfn);
00381 if (jst == -1 || jfn == -1) {
00382 cpl_msg_error(fctid,"Unable to continue");
00383 vircam_imcombine_tidy();
00384 return(-1);
00385 }
00386
00387
00388
00389 status = VIR_OK;
00390 for (j = jst; j <= jfn; j++) {
00391 isfirst = (j == jst);
00392
00393
00394
00395 ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00396
00397
00398
00399 cpl_msg_info(fctid,"Doing combination for extension %" CPL_SIZE_FORMAT,
00400 (cpl_size)j);
00401 (void)vircam_imcombine(ps.images,ps.nimages,
00402 vircam_imcombine_config.combtype,
00403 vircam_imcombine_config.scaletype,
00404 vircam_imcombine_config.xrej,
00405 vircam_imcombine_config.thresh,
00406 &(ps.outimage),&rejmask,&rejplus,&drs,&status);
00407 freespace(rejmask);
00408 freespace(rejplus);
00409 freepropertylist(drs);
00410 if (status != VIR_OK) {
00411 vircam_imcombine_tidy();
00412 return(-1);
00413 }
00414
00415
00416
00417 cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
00418 (cpl_size)j);
00419 retval = vircam_imcombine_save(framelist,parlist);
00420 if (retval != 0) {
00421 vircam_imcombine_tidy();
00422 return(-1);
00423 }
00424 freefitslist(ps.images,ps.nimages);
00425 freeimage(ps.outimage);
00426 }
00427 vircam_imcombine_tidy();
00428 return(0);
00429 }
00430
00431
00432
00439
00440
00441 static int vircam_imcombine_save(cpl_frameset *framelist,
00442 cpl_parameterlist *parlist) {
00443 cpl_propertylist *plist;
00444 const char *recipeid = "vircam_imcombine";
00445 const char *fctid = "vircam_imcombine_save";
00446 const char *outfile = "comb.fits";
00447
00448
00449
00450
00451 if (isfirst) {
00452
00453
00454
00455 product_frame = cpl_frame_new();
00456 cpl_frame_set_filename(product_frame,outfile);
00457 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00458 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00459 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00460 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00461
00462
00463
00464 plist = vircam_fits_get_phu(ps.images[0]);
00465 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00466 parlist,(char *)recipeid,
00467 "?Dictionary?",NULL,0);
00468
00469
00470 if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00471 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00472 cpl_msg_error(fctid,"Cannot save product PHU");
00473 cpl_frame_delete(product_frame);
00474 return(-1);
00475 }
00476 cpl_frameset_insert(framelist,product_frame);
00477 }
00478
00479
00480
00481 plist = vircam_fits_get_ehu(ps.images[0]);
00482
00483
00484
00485 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00486 parlist,(char *)recipeid,
00487 "?Dictionary?",NULL);
00488
00489
00490
00491 if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
00492 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00493 cpl_msg_error(fctid,"Cannot save product image extension");
00494 return(-1);
00495 }
00496
00497
00498
00499 return(0);
00500 }
00501
00502
00506
00507
00508 static void vircam_imcombine_init(void) {
00509 ps.labels = NULL;
00510 ps.imagelist = NULL;
00511 ps.images = NULL;
00512 ps.outimage = NULL;
00513 }
00514
00515
00519
00520
00521 static void vircam_imcombine_tidy(void) {
00522 freespace(ps.labels);
00523 freeframeset(ps.imagelist);
00524 freefitslist(ps.images,ps.nimages);
00525 freeimage(ps.outimage);
00526 }
00527
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
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586