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
00037 #include "vircam_utils.h"
00038 #include "vircam_dfs.h"
00039 #include "vircam_fits.h"
00040 #include "vircam_mods.h"
00041
00042
00043
00044 static int vircam_matchstds_create(cpl_plugin *);
00045 static int vircam_matchstds_exec(cpl_plugin *);
00046 static int vircam_matchstds_destroy(cpl_plugin *);
00047 static int vircam_matchstds_test(cpl_parameterlist *, cpl_frameset *);
00048 static int vircam_matchstds_save(cpl_frameset *framelist,
00049 cpl_parameterlist *parlist);
00050 static void vircam_matchstds_init(void);
00051 static void vircam_matchstds_tidy(void);
00052
00053 static struct {
00054
00055
00056
00057 int extenum;
00058
00059 } vircam_matchstds_config;
00060
00061
00062 static struct {
00063 cpl_size *labels;
00064 cpl_frame *cat;
00065 cpl_frame *stds;
00066 vir_tfits *catf;
00067 vir_tfits *stdsf;
00068 cpl_table *outtab;
00069 } ps;
00070
00071 static int isfirst;
00072 static cpl_frame *product_frame = NULL;
00073
00074 static char vircam_matchstds_description[] =
00075 "vircam_matchstds -- VIRCAM recipe to test vircam_matchstds.\n\n"
00076 "Match a catalogue with a table of standards\n\n"
00077 "The program accepts the following files in the SOF:\n\n"
00078 " Tag Description\n"
00079 " -----------------------------------------------------------------------\n"
00080 " %-21s An input catalogue of objects extracted from an image\n"
00081 " %-21s An input catalogue of standard stars\n"
00082 "\n";
00083
00126
00127
00128
00136
00137
00138 int cpl_plugin_get_info(cpl_pluginlist *list) {
00139 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00140 cpl_plugin *plugin = &recipe->interface;
00141 char alldesc[SZ_ALLDESC];
00142 (void)snprintf(alldesc,SZ_ALLDESC,vircam_matchstds_description,
00143 VIRCAM_CAL_OBJCAT,VIRCAM_CAL_STDTAB);
00144
00145 cpl_plugin_init(plugin,
00146 CPL_PLUGIN_API,
00147 VIRCAM_BINARY_VERSION,
00148 CPL_PLUGIN_TYPE_RECIPE,
00149 "vircam_matchstds",
00150 "VIRCAM catalogue and standards matching test recipe [test]",
00151 alldesc,
00152 "Jim Lewis",
00153 "jrl@ast.cam.ac.uk",
00154 vircam_get_license(),
00155 vircam_matchstds_create,
00156 vircam_matchstds_exec,
00157 vircam_matchstds_destroy);
00158
00159 cpl_pluginlist_append(list,plugin);
00160
00161 return(0);
00162 }
00163
00164
00173
00174
00175 static int vircam_matchstds_create(cpl_plugin *plugin) {
00176 cpl_recipe *recipe;
00177 cpl_parameter *p;
00178
00179
00180
00181 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00182 recipe = (cpl_recipe *)plugin;
00183 else
00184 return(-1);
00185
00186
00187
00188 recipe->parameters = cpl_parameterlist_new();
00189
00190
00191
00192 p = cpl_parameter_new_range("vircam.vircam_matchstds.extenum",
00193 CPL_TYPE_INT,
00194 "Extension number to be done, 0 == all",
00195 "vircam.vircam_matchstds",1,0,16);
00196 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00197 cpl_parameterlist_append(recipe->parameters,p);
00198
00199
00200
00201 return(0);
00202 }
00203
00204
00210
00211
00212 static int vircam_matchstds_exec(cpl_plugin *plugin) {
00213 cpl_recipe *recipe;
00214
00215
00216
00217 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00218 recipe = (cpl_recipe *)plugin;
00219 else
00220 return(-1);
00221
00222 return(vircam_matchstds_test(recipe->parameters,recipe->frames));
00223 }
00224
00225
00231
00232
00233 static int vircam_matchstds_destroy(cpl_plugin *plugin) {
00234 cpl_recipe *recipe ;
00235
00236
00237
00238 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00239 recipe = (cpl_recipe *)plugin;
00240 else
00241 return(-1);
00242
00243 cpl_parameterlist_delete(recipe->parameters);
00244 return(0);
00245 }
00246
00247
00254
00255
00256 static int vircam_matchstds_test(cpl_parameterlist *parlist,
00257 cpl_frameset *framelist) {
00258 const char *fctid="vircam_matchstds";
00259 cpl_parameter *p;
00260 int jst,jfn,status,j;
00261 cpl_size nlab;
00262
00263
00264
00265 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00266 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00267 return(-1);
00268 }
00269
00270
00271
00272 vircam_matchstds_init();
00273
00274
00275
00276 p = cpl_parameterlist_find(parlist,"vircam.vircam_matchstds.extenum");
00277 vircam_matchstds_config.extenum = cpl_parameter_get_int(p);
00278
00279
00280
00281 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00282 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00283 vircam_matchstds_tidy();
00284 return(-1);
00285 }
00286
00287
00288
00289 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00290 &nlab)) == NULL) {
00291 cpl_msg_error(fctid,"Cannot labelise the input frames");
00292 vircam_matchstds_tidy();
00293 return(-1);
00294 }
00295 if ((ps.cat = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00296 VIRCAM_CAL_OBJCAT)) == NULL) {
00297 cpl_msg_info(fctid,"No object catalogue found -- cannot continue");
00298 vircam_matchstds_tidy();
00299 return(-1);
00300 }
00301 if ((ps.stds = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00302 VIRCAM_CAL_STDTAB)) == NULL) {
00303 cpl_msg_info(fctid,"No standards catalogue found -- cannot continue");
00304 vircam_matchstds_tidy();
00305 return(-1);
00306 }
00307
00308
00309
00310
00311
00312 vircam_exten_range(vircam_matchstds_config.extenum,
00313 (const cpl_frame *)ps.cat,&jst,&jfn);
00314 if (jst == -1 || jfn == -1) {
00315 cpl_msg_error(fctid,"Unable to continue");
00316 vircam_matchstds_tidy();
00317 return(-1);
00318 }
00319
00320
00321
00322 status = VIR_OK;
00323 for (j = jst; j <= jfn; j++) {
00324 isfirst = (j == jst);
00325
00326
00327
00328 ps.catf = vircam_tfits_load(ps.cat,j);
00329 ps.stdsf = vircam_tfits_load(ps.stds,j);
00330 if (ps.stdsf == NULL || ps.catf == NULL) {
00331 freetfits(ps.catf);
00332 freetfits(ps.stdsf);
00333 cpl_msg_info(fctid,"No matching possible");
00334 continue;
00335 }
00336
00337
00338
00339 cpl_msg_info(fctid,"Doing the matching for extension %" CPL_SIZE_FORMAT,
00340 (cpl_size)j);
00341 (void)vircam_matchstds(vircam_tfits_get_table(ps.catf),
00342 vircam_tfits_get_table(ps.stdsf),300.0,
00343 &(ps.outtab),&status);
00344 if (status != VIR_OK) {
00345 cpl_msg_info(fctid,"No matching done");
00346 status = VIR_OK;
00347 }
00348
00349
00350
00351 cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
00352 (cpl_size)j);
00353 if (vircam_matchstds_save(framelist,parlist) != 0)
00354 cpl_msg_info(fctid,"No matching saved");
00355
00356
00357
00358
00359 freetfits(ps.catf);
00360 freetfits(ps.stdsf);
00361 freetable(ps.outtab);
00362 }
00363 vircam_matchstds_tidy();
00364 return(0);
00365 }
00366
00367
00374
00375
00376 static int vircam_matchstds_save(cpl_frameset *framelist,
00377 cpl_parameterlist *parlist) {
00378 const char *recipeid = "vircam_matchstds";
00379 const char *fctid = "vircam_matchstds_save";
00380 const char *outfile = "matchstds.fits";
00381 cpl_propertylist *plist,*elist;
00382
00383
00384
00385 if (isfirst) {
00386
00387
00388
00389 product_frame = cpl_frame_new();
00390 cpl_frame_set_filename(product_frame,outfile);
00391 cpl_frame_set_tag(product_frame,VIRCAM_PRO_MSTDTAB);
00392 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00393 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00394 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00395
00396
00397
00398 plist = vircam_tfits_get_phu(ps.catf);
00399 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00400 parlist,(char *)recipeid,
00401 "?Dictionary?",NULL,0);
00402
00403
00404
00405 elist = vircam_tfits_get_ehu(ps.catf);
00406 vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00407 parlist,(char *)recipeid,
00408 "?Dictionary?",NULL);
00409
00410
00411
00412 if (cpl_table_save(ps.outtab,plist,elist,outfile,CPL_IO_DEFAULT)
00413 != CPL_ERROR_NONE) {
00414 cpl_msg_error(fctid,"Cannot save product table");
00415 cpl_frame_delete(product_frame);
00416 return(-1);
00417 }
00418 cpl_frameset_insert(framelist,product_frame);
00419
00420
00421
00422 } else {
00423
00424
00425
00426 elist = vircam_tfits_get_ehu(ps.catf);
00427 vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00428 parlist,(char *)recipeid,
00429 "?Dictionary?",NULL);
00430
00431
00432
00433 if (cpl_table_save(ps.outtab,NULL,elist,outfile,CPL_IO_EXTEND)
00434 != CPL_ERROR_NONE) {
00435 cpl_msg_error(fctid,"Cannot save product table");
00436 return(-1);
00437 }
00438 }
00439
00440 return(0);
00441 }
00442
00443
00444
00448
00449
00450 static void vircam_matchstds_init(void) {
00451 ps.labels = NULL;
00452 ps.cat = NULL;
00453 ps.catf = NULL;
00454 ps.stds = NULL;
00455 ps.stdsf = NULL;
00456 ps.outtab = NULL;
00457 }
00458
00459
00460
00464
00465
00466 static void vircam_matchstds_tidy(void) {
00467 freespace(ps.labels);
00468 freetfits(ps.catf);
00469 freetfits(ps.stdsf);
00470 freeframe(ps.stds);
00471 freeframe(ps.cat);
00472 freetable(ps.outtab);
00473 }
00474
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526