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 <cpl.h>
00035 #include <math.h>
00036
00037 #include "vircam_utils.h"
00038 #include "vircam_dfs.h"
00039 #include "vircam_mods.h"
00040 #include "vircam_fits.h"
00041 #include "../vircam/catalogue/imcore.h"
00042
00043
00044
00045 static int vircam_imcore_create(cpl_plugin *) ;
00046 static int vircam_imcore_exec(cpl_plugin *) ;
00047 static int vircam_imcore_destroy(cpl_plugin *) ;
00048 static int vircam_imcore_test(cpl_parameterlist *, cpl_frameset *) ;
00049 static int vircam_imcore_save(cpl_frameset *framelist,
00050 cpl_parameterlist *parlist);
00051 static void vircam_imcore_init(void);
00052 static void vircam_imcore_tidy(void);
00053
00054
00055
00056 static struct {
00057
00058
00059
00060 int ipix;
00061 float threshold;
00062 int icrowd;
00063 float rcore;
00064 float filtfwhm;
00065 int nbsize;
00066 int cattype;
00067 int extenum;
00068
00069
00070
00071 float skylevel;
00072 float skynoise;
00073
00074 } vircam_imcore_config ;
00075
00076 static struct {
00077 int *labels;
00078 cpl_frame *img;
00079 cpl_frame *conf;
00080 vir_fits *imgf;
00081 vir_fits *conff;
00082 vir_tfits *outcat;
00083 } ps;
00084
00085 static int isfirst;
00086 static cpl_frame *product_frame = NULL;
00087
00088 #define BUZZ_OFF {vircam_imcore_tidy(); return(-1);}
00089
00090 static char vircam_imcore_description[] =
00091 "vircam_imcore -- VIRCAM catalogue generation recipe.\n\n"
00092 "Extract objects from an image and write the catalogue to a FITS table.\n\n"
00093 "If more than one file of each type is specified in the SOF only the \n"
00094 "first will be used and the rest will be ingored\n"
00095 "The program requires the following files in the SOF:\n\n"
00096 " Tag Description\n"
00097 " -----------------------------------------------------------------------\n"
00098 " %-21s An input image\n"
00099 " %-21s A master confidence maps\n"
00100 "\n";
00101
00153
00154
00155
00156
00164
00165
00166 int cpl_plugin_get_info(cpl_pluginlist *list) {
00167 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00168 cpl_plugin *plugin = &recipe->interface;
00169 char alldesc[SZ_ALLDESC];
00170 (void)snprintf(alldesc,SZ_ALLDESC,vircam_imcore_description,
00171 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
00172
00173
00174 cpl_plugin_init(plugin,
00175 CPL_PLUGIN_API,
00176 VIRCAM_BINARY_VERSION,
00177 CPL_PLUGIN_TYPE_RECIPE,
00178 "vircam_imcore",
00179 "VIRCAM recipe to extract objects from a frame [test]",
00180 alldesc,
00181 "Jim Lewis",
00182 "jrl@ast.cam.ac.uk",
00183 vircam_get_license(),
00184 vircam_imcore_create,
00185 vircam_imcore_exec,
00186 vircam_imcore_destroy);
00187
00188 cpl_pluginlist_append(list,plugin);
00189
00190 return(0);
00191 }
00192
00193
00194
00203
00204
00205 static int vircam_imcore_create(cpl_plugin *plugin) {
00206 cpl_recipe *recipe;
00207 cpl_parameter *p;
00208
00209
00210
00211 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00212 recipe = (cpl_recipe *)plugin;
00213 else
00214 return(-1);
00215
00216
00217
00218 recipe->parameters = cpl_parameterlist_new();
00219
00220
00221
00222 p = cpl_parameter_new_value("vircam.vircam_imcore.ipix",
00223 CPL_TYPE_INT,
00224 "Minimum pixel area for each detected object",
00225 "vircam.vircam_imcore",5);
00226 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ipix");
00227 cpl_parameterlist_append(recipe->parameters,p);
00228
00229
00230
00231 p = cpl_parameter_new_value("vircam.vircam_imcore.thresh",
00232 CPL_TYPE_DOUBLE,
00233 "Detection threshold in sigma above sky",
00234 "vircam.vircam_imcore",2.0);
00235 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
00236 cpl_parameterlist_append(recipe->parameters,p);
00237
00238
00239
00240 p = cpl_parameter_new_value("vircam.vircam_imcore.icrowd",
00241 CPL_TYPE_BOOL,"Use deblending?",
00242 "vircam.vircam_imcore",0);
00243 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"icrowd");
00244 cpl_parameterlist_append(recipe->parameters,p);
00245
00246
00247
00248 p = cpl_parameter_new_value("vircam.vircam_imcore.rcore",
00249 CPL_TYPE_DOUBLE,"Value of Rcore in pixels",
00250 "vircam.vircam_imcore",4.0);
00251 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"rcore");
00252 cpl_parameterlist_append(recipe->parameters,p);
00253
00254
00255
00256 p = cpl_parameter_new_value("vircam.vircam_imcore.nbsize",
00257 CPL_TYPE_INT,"Background smoothing box size",
00258 "vircam.vircam_imcore",64);
00259 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nb");
00260 cpl_parameterlist_append(recipe->parameters,p);
00261
00262
00263
00264 p = cpl_parameter_new_value("vircam.vircam_imcore.filtfwhm",
00265 CPL_TYPE_DOUBLE,
00266 "FWHM of smoothing kernel in pixels",
00267 "vircam.vircam_imcore",2.0);
00268 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"filtfwhm");
00269 cpl_parameterlist_append(recipe->parameters,p);
00270
00271
00272
00273 p = cpl_parameter_new_enum("vircam.vircam_imcore.cattype",
00274 CPL_TYPE_INT,"Catalogue type",
00275 "vircam.vircam_imcore",2,4,1,2,3,4);
00276 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"cattype");
00277 cpl_parameterlist_append(recipe->parameters,p);
00278
00279
00280
00281 p = cpl_parameter_new_range("vircam.vircam_imcore.extenum",
00282 CPL_TYPE_INT,
00283 "Extension number to be done, 0 == all",
00284 "vircam.vircam_imcore",
00285 1,0,16);
00286 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00287 cpl_parameterlist_append(recipe->parameters,p);
00288
00289
00290
00291 return(0);
00292 }
00293
00294
00300
00301
00302 static int vircam_imcore_destroy(cpl_plugin *plugin) {
00303 cpl_recipe *recipe ;
00304
00305
00306
00307 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00308 recipe = (cpl_recipe *)plugin;
00309 else
00310 return(-1);
00311
00312 cpl_parameterlist_delete(recipe->parameters);
00313 return(0);
00314 }
00315
00316
00322
00323
00324 static int vircam_imcore_exec(cpl_plugin *plugin) {
00325 cpl_recipe *recipe;
00326
00327
00328
00329 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00330 recipe = (cpl_recipe *)plugin;
00331 else
00332 return(-1);
00333
00334 return(vircam_imcore_test(recipe->parameters,recipe->frames));
00335 }
00336
00337
00344
00345
00346 static int vircam_imcore_test(cpl_parameterlist *parlist,
00347 cpl_frameset *framelist) {
00348 const char *fctid="vircam_imcore";
00349 cpl_parameter *p;
00350 int nlab,j,jst,jfn,retval,ipix,icrowd,nbsize,mcattype,status;
00351 float thresh,rcore,filtfwhm;
00352
00353
00354
00355 vircam_imcore_init();
00356
00357
00358
00359 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.ipix");
00360 vircam_imcore_config.ipix = cpl_parameter_get_int(p);
00361 ipix = vircam_imcore_config.ipix;
00362 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.thresh");
00363 vircam_imcore_config.threshold = (float)cpl_parameter_get_double(p);
00364 thresh = vircam_imcore_config.threshold;
00365 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.icrowd");
00366 vircam_imcore_config.icrowd = cpl_parameter_get_bool(p);
00367 icrowd = vircam_imcore_config.icrowd;
00368 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.rcore");
00369 vircam_imcore_config.rcore = (float)cpl_parameter_get_double(p);
00370 rcore = vircam_imcore_config.rcore;
00371 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.filtfwhm");
00372 vircam_imcore_config.filtfwhm = (float)cpl_parameter_get_double(p);
00373 filtfwhm = vircam_imcore_config.filtfwhm;
00374 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.nbsize");
00375 vircam_imcore_config.nbsize = cpl_parameter_get_int(p);
00376 nbsize = vircam_imcore_config.nbsize;
00377 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.cattype");
00378 vircam_imcore_config.cattype = cpl_parameter_get_int(p);
00379 mcattype = vircam_imcore_config.cattype;
00380 p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.extenum");
00381 vircam_imcore_config.extenum = cpl_parameter_get_int(p);
00382
00383
00384
00385 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00386 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00387 vircam_imcore_tidy();
00388 return(-1);
00389 }
00390
00391
00392
00393 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00394 &nlab)) == NULL) {
00395 cpl_msg_error(fctid,"Cannot labelise the input frameset");
00396 BUZZ_OFF
00397 }
00398 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00399 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00400 cpl_msg_error(fctid,"Cannot find any image frames in input frameset");
00401 BUZZ_OFF
00402 }
00403 if ((ps.conf = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00404 VIRCAM_CAL_CONF)) == NULL) {
00405 cpl_msg_info(fctid,"No confidence map specified. Proceding without one");
00406 ps.conf = NULL;
00407 }
00408
00409
00410
00411 vircam_exten_range(vircam_imcore_config.extenum,(const cpl_frame *)ps.img,
00412 &jst,&jfn);
00413 if (jst == -1 || jfn == -1) {
00414 cpl_msg_error(fctid,"Unable to continue");
00415 vircam_imcore_tidy();
00416 return(-1);
00417 }
00418 for (j = jst; j <= jfn; j++) {
00419 cpl_msg_info(fctid,"Processing extension %d\n",j);
00420 status = VIR_OK;
00421 isfirst = (j == jst);
00422
00423
00424
00425 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00426 if (ps.imgf == NULL) {
00427 vircam_imcore_tidy();
00428 return(-1);
00429 }
00430 if (ps.conf != NULL)
00431 ps.conff = vircam_fits_load(ps.conf,CPL_TYPE_INT,j);
00432
00433
00434
00435 (void)vircam_imcore(ps.imgf,ps.conff,ipix,thresh,icrowd,rcore,
00436 nbsize,mcattype,filtfwhm,&(ps.outcat),&status);
00437 if (status != VIR_OK) {
00438 cpl_msg_error(fctid,"Error extracting objects in extension %d",j);
00439 cpl_error_reset();
00440 ps.outcat = vircam_tfits_wrap(vircam_dummy_catalogue(mcattype),
00441 NULL,
00442 vircam_fits_get_phu(ps.imgf),
00443 vircam_fits_get_ehu(ps.imgf));
00444 }
00445
00446
00447
00448 retval = vircam_imcore_save(framelist,parlist);
00449 if (retval != 0) {
00450 cpl_msg_error(fctid,"Error saving products in extension %d",j);
00451 BUZZ_OFF
00452 }
00453
00454
00455
00456 freetfits(ps.outcat);
00457 freefits(ps.imgf);
00458 freefits(ps.conff);
00459 }
00460 vircam_imcore_tidy();
00461 return(0);
00462 }
00463
00464
00471
00472
00473 static int vircam_imcore_save(cpl_frameset *framelist,
00474 cpl_parameterlist *parlist) {
00475 const char *recipeid = "vircam_imcore";
00476 const char *fctid = "vircam_imcore_save";
00477 const char *outfile = "imcoretab.fits";
00478 cpl_propertylist *elist,*plist;
00479
00480
00481
00482 if (isfirst) {
00483
00484
00485
00486 product_frame = cpl_frame_new();
00487 cpl_frame_set_filename(product_frame,outfile);
00488 cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_TEST);
00489 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00490 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00491 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00492
00493
00494
00495 plist = vircam_tfits_get_phu(ps.outcat);
00496 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00497 parlist,(char *)recipeid,
00498 "?Dictionary?");
00499
00500
00501
00502 elist = vircam_tfits_get_ehu(ps.outcat);
00503 vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00504 parlist,(char *)recipeid,
00505 "?Dictionary?");
00506
00507
00508
00509 if (cpl_table_save(vircam_tfits_get_table(ps.outcat),plist,elist,
00510 outfile,CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00511 cpl_msg_error(fctid,"Cannot save product table");
00512 cpl_frame_delete(product_frame);
00513 return(-1);
00514 }
00515 cpl_frameset_insert(framelist,product_frame);
00516
00517
00518
00519 } else {
00520
00521
00522
00523 elist = vircam_tfits_get_ehu(ps.outcat);
00524 vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00525 parlist,(char *)recipeid,
00526 "?Dictionary?");
00527
00528
00529
00530 if (cpl_table_save(vircam_tfits_get_table(ps.outcat),NULL,elist,
00531 outfile,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00532 cpl_msg_error(fctid,"Cannot save product table");
00533 return(-1);
00534 }
00535 }
00536
00537 return(0);
00538 }
00539
00540
00544
00545
00546 static void vircam_imcore_init(void) {
00547 ps.labels = NULL;
00548 ps.img = NULL;
00549 ps.conf = NULL;
00550 ps.imgf = NULL;
00551 ps.conff = NULL;
00552 ps.outcat = NULL;
00553 }
00554
00555
00559
00560
00561 static void vircam_imcore_tidy(void) {
00562 freespace(ps.labels);
00563 freeframe(ps.img);
00564 freeframe(ps.conf);
00565 freefits(ps.imgf);
00566 freefits(ps.conff);
00567 freetfits(ps.outcat);
00568 }
00569
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623