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 int *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 nlab,jst,jfn,status,j;
00261
00262
00263
00264 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00265 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00266 return(-1);
00267 }
00268
00269
00270
00271 vircam_matchstds_init();
00272
00273
00274
00275 p = cpl_parameterlist_find(parlist,"vircam.vircam_matchstds.extenum");
00276 vircam_matchstds_config.extenum = cpl_parameter_get_int(p);
00277
00278
00279
00280 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00281 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00282 vircam_matchstds_tidy();
00283 return(-1);
00284 }
00285
00286
00287
00288 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00289 &nlab)) == NULL) {
00290 cpl_msg_error(fctid,"Cannot labelise the input frames");
00291 vircam_matchstds_tidy();
00292 return(-1);
00293 }
00294 if ((ps.cat = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00295 VIRCAM_CAL_OBJCAT)) == NULL) {
00296 cpl_msg_info(fctid,"No object catalogue found -- cannot continue");
00297 vircam_matchstds_tidy();
00298 return(-1);
00299 }
00300 if ((ps.stds = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00301 VIRCAM_CAL_STDTAB)) == NULL) {
00302 cpl_msg_info(fctid,"No standards catalogue found -- cannot continue");
00303 vircam_matchstds_tidy();
00304 return(-1);
00305 }
00306
00307
00308
00309
00310
00311 vircam_exten_range(vircam_matchstds_config.extenum,
00312 (const cpl_frame *)ps.cat,&jst,&jfn);
00313 if (jst == -1 || jfn == -1) {
00314 cpl_msg_error(fctid,"Unable to continue");
00315 vircam_matchstds_tidy();
00316 return(-1);
00317 }
00318
00319
00320
00321 status = VIR_OK;
00322 for (j = jst; j <= jfn; j++) {
00323 isfirst = (j == jst);
00324
00325
00326
00327 ps.catf = vircam_tfits_load(ps.cat,j);
00328 ps.stdsf = vircam_tfits_load(ps.stds,j);
00329 if (ps.stdsf == NULL || ps.catf == NULL) {
00330 freetfits(ps.catf);
00331 freetfits(ps.stdsf);
00332 cpl_msg_info(fctid,"No matching possible");
00333 continue;
00334 }
00335
00336
00337
00338 cpl_msg_info(fctid,"Doing the matching for extension %d",j);
00339 (void)vircam_matchstds(vircam_tfits_get_table(ps.catf),
00340 vircam_tfits_get_table(ps.stdsf),10.0,
00341 &(ps.outtab),&status);
00342 if (status != VIR_OK) {
00343 cpl_msg_info(fctid,"No matching done");
00344 status = VIR_OK;
00345 }
00346
00347
00348
00349 cpl_msg_info(fctid,"Saving results for extension %d",j);
00350 if (vircam_matchstds_save(framelist,parlist) != 0)
00351 cpl_msg_info(fctid,"No matching saved");
00352
00353
00354
00355
00356 freetfits(ps.catf);
00357 freetfits(ps.stdsf);
00358 freetable(ps.outtab);
00359 }
00360 vircam_matchstds_tidy();
00361 return(0);
00362 }
00363
00364
00371
00372
00373 static int vircam_matchstds_save(cpl_frameset *framelist,
00374 cpl_parameterlist *parlist) {
00375 const char *recipeid = "vircam_matchstds";
00376 const char *fctid = "vircam_matchstds_save";
00377 const char *outfile = "matchstds.fits";
00378 cpl_propertylist *plist,*elist;
00379
00380
00381
00382 if (isfirst) {
00383
00384
00385
00386 product_frame = cpl_frame_new();
00387 cpl_frame_set_filename(product_frame,outfile);
00388 cpl_frame_set_tag(product_frame,VIRCAM_PRO_MSTDTAB);
00389 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00390 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00391 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00392
00393
00394
00395 plist = vircam_tfits_get_phu(ps.catf);
00396 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00397 parlist,(char *)recipeid,
00398 "?Dictionary?");
00399
00400
00401
00402 elist = vircam_tfits_get_ehu(ps.catf);
00403 vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00404 parlist,(char *)recipeid,
00405 "?Dictionary?");
00406
00407
00408
00409 if (cpl_table_save(ps.outtab,plist,elist,outfile,CPL_IO_DEFAULT)
00410 != CPL_ERROR_NONE) {
00411 cpl_msg_error(fctid,"Cannot save product table");
00412 cpl_frame_delete(product_frame);
00413 return(-1);
00414 }
00415 cpl_frameset_insert(framelist,product_frame);
00416
00417
00418
00419 } else {
00420
00421
00422
00423 elist = vircam_tfits_get_ehu(ps.catf);
00424 vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00425 parlist,(char *)recipeid,
00426 "?Dictionary?");
00427
00428
00429
00430 if (cpl_table_save(ps.outtab,NULL,elist,outfile,CPL_IO_EXTEND)
00431 != CPL_ERROR_NONE) {
00432 cpl_msg_error(fctid,"Cannot save product table");
00433 return(-1);
00434 }
00435 }
00436
00437 return(0);
00438 }
00439
00440
00441
00445
00446
00447 static void vircam_matchstds_init(void) {
00448 ps.labels = NULL;
00449 ps.cat = NULL;
00450 ps.catf = NULL;
00451 ps.stds = NULL;
00452 ps.stdsf = NULL;
00453 ps.outtab = NULL;
00454 }
00455
00456
00457
00461
00462
00463 static void vircam_matchstds_tidy(void) {
00464 freespace(ps.labels);
00465 freetfits(ps.catf);
00466 freetfits(ps.stdsf);
00467 freeframe(ps.stds);
00468 freeframe(ps.cat);
00469 freetable(ps.outtab);
00470 }
00471
00475
00476
00477
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