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_getstds_create(cpl_plugin *) ;
00045 static int vircam_getstds_exec(cpl_plugin *) ;
00046 static int vircam_getstds_destroy(cpl_plugin *) ;
00047 static int vircam_getstds_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_getstds_save(cpl_frameset *framelist,
00049 cpl_parameterlist *parlist);
00050 static void vircam_getstds_init(void);
00051 static void vircam_getstds_tidy(void);
00052
00053 static struct {
00054
00055
00056
00057 int extenum;
00058
00059 } vircam_getstds_config;
00060
00061 static struct {
00062 int *labels;
00063 cpl_frame *img;
00064 vir_fits *imgf;
00065 cpl_propertylist *plist;
00066 cpl_table *stds;
00067 char *catpath;
00068 char *catname;
00069 } ps;
00070
00071 static int isfirst;
00072 static cpl_frame *product_frame = NULL;
00073
00074 static char vircam_getstds_description[] =
00075 "vircam_getstds -- VIRCAM test recipe to get standard stars for a frame\n\n"
00076 "The program accepts the following files in the SOF:\n\n"
00077 " Tag Description\n"
00078 " -----------------------------------------------------------------------\n"
00079 " %-21s A input image\n"
00080 " %-21s Standard catalogue index file\n"
00081 "\n";
00082
00128
00129
00130
00138
00139
00140 int cpl_plugin_get_info(cpl_pluginlist *list) {
00141 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00142 cpl_plugin *plugin = &recipe->interface;
00143 char alldesc[SZ_ALLDESC];
00144 (void)snprintf(alldesc,SZ_ALLDESC,vircam_getstds_description,
00145 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_2MASS);
00146
00147 cpl_plugin_init(plugin,
00148 CPL_PLUGIN_API,
00149 VIRCAM_BINARY_VERSION,
00150 CPL_PLUGIN_TYPE_RECIPE,
00151 "vircam_getstds",
00152 "VIRCAM standard star extraction test recipe [test]",
00153 alldesc,
00154 "Jim Lewis",
00155 "jrl@ast.cam.ac.uk",
00156 vircam_get_license(),
00157 vircam_getstds_create,
00158 vircam_getstds_exec,
00159 vircam_getstds_destroy);
00160
00161 cpl_pluginlist_append(list,plugin);
00162
00163 return(0);
00164 }
00165
00166
00175
00176
00177 static int vircam_getstds_create(cpl_plugin *plugin) {
00178 cpl_recipe *recipe;
00179 cpl_parameter *p;
00180
00181
00182
00183 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00184 recipe = (cpl_recipe *)plugin;
00185 else
00186 return(-1);
00187
00188
00189
00190 recipe->parameters = cpl_parameterlist_new();
00191
00192
00193
00194 p = cpl_parameter_new_range("vircam.vircam_getstds.extenum",
00195 CPL_TYPE_INT,
00196 "Extension number to be done, 0 == all",
00197 "vircam.vircam_getstds",1,0,16);
00198 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00199 cpl_parameterlist_append(recipe->parameters,p);
00200
00201
00202
00203 return(0);
00204 }
00205
00206
00212
00213
00214 static int vircam_getstds_exec(cpl_plugin *plugin) {
00215 cpl_recipe *recipe;
00216
00217
00218
00219 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00220 recipe = (cpl_recipe *)plugin;
00221 else
00222 return(-1);
00223
00224 return(vircam_getstds_test(recipe->parameters,recipe->frames));
00225 }
00226
00227
00228
00234
00235
00236 static int vircam_getstds_destroy(cpl_plugin *plugin) {
00237 cpl_recipe *recipe ;
00238
00239
00240
00241 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00242 recipe = (cpl_recipe *)plugin;
00243 else
00244 return(-1);
00245
00246 cpl_parameterlist_delete(recipe->parameters);
00247 return(0);
00248 }
00249
00250
00251
00258
00259
00260 static int vircam_getstds_test(cpl_parameterlist *parlist,
00261 cpl_frameset *framelist) {
00262 const char *fctid="vircam_getstds";
00263 cpl_parameter *p;
00264 int nlab,jst,jfn,status,j;
00265 cpl_frame *catindex;
00266
00267
00268
00269 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00270 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00271 return(-1);
00272 }
00273
00274
00275
00276 vircam_getstds_init();
00277
00278
00279
00280 p = cpl_parameterlist_find(parlist,"vircam.vircam_getstds.extenum");
00281 vircam_getstds_config.extenum = cpl_parameter_get_int(p);
00282
00283
00284
00285 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00286 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00287 vircam_getstds_tidy();
00288 return(-1);
00289 }
00290
00291
00292
00293 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00294 &nlab)) == NULL) {
00295 cpl_msg_error(fctid,"Cannot labelise the input frames");
00296 vircam_getstds_tidy();
00297 return(-1);
00298 }
00299 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00300 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00301 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00302 vircam_getstds_tidy();
00303 return(-1);
00304 }
00305 if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00306 VIRCAM_CAL_2MASS)) == NULL) {
00307 cpl_msg_info(fctid,"No 2MASS index found -- cannot continue");
00308 vircam_getstds_tidy();
00309 return(-1);
00310 }
00311
00312
00313
00314 vircam_catpars(catindex,&(ps.catpath),&(ps.catname));
00315 cpl_frame_delete(catindex);
00316
00317
00318
00319
00320
00321 vircam_exten_range(vircam_getstds_config.extenum,(const cpl_frame *)ps.img,
00322 &jst,&jfn);
00323 if (jst == -1 || jfn == -1) {
00324 cpl_msg_error(fctid,"Unable to continue");
00325 vircam_getstds_tidy();
00326 return(-1);
00327 }
00328
00329
00330
00331 status = VIR_OK;
00332 for (j = jst; j <= jfn; j++) {
00333 isfirst = (j == jst);
00334
00335
00336
00337 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00338 if (ps.img == NULL) {
00339 vircam_getstds_tidy();
00340 return(-1);
00341 }
00342
00343
00344
00345 cpl_msg_info(fctid,"Extracting the standards");
00346 (void)vircam_getstds(vircam_fits_get_ehu(ps.imgf),1,ps.catpath,
00347 ps.catname,&(ps.stds),&status);
00348 if (status != VIR_OK) {
00349 vircam_getstds_tidy();
00350 return(-1);
00351 }
00352
00353
00354
00355 cpl_msg_info(fctid,"Saving results for extension %d",j);
00356 if (vircam_getstds_save(framelist,parlist) != 0) {
00357 vircam_getstds_tidy();
00358 return(-1);
00359 }
00360
00361
00362
00363 freetable(ps.stds);
00364 freefits(ps.imgf);
00365 }
00366 vircam_getstds_tidy();
00367 return(0);
00368 }
00369
00370
00371
00378
00379
00380 static int vircam_getstds_save(cpl_frameset *framelist,
00381 cpl_parameterlist *parlist) {
00382 const char *fctid = "vircam_getstds_save";
00383 const char *outfile = "getstds.fits";
00384 const char *recipeid = "vircam_getstds";
00385 cpl_propertylist *plist,*elist;
00386
00387
00388
00389
00390 if (isfirst) {
00391
00392
00393
00394 product_frame = cpl_frame_new();
00395 cpl_frame_set_filename(product_frame,outfile);
00396 cpl_frame_set_tag(product_frame,VIRCAM_PRO_STDTAB);
00397 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00398 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00399 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00400
00401
00402
00403 plist = vircam_fits_get_phu(ps.imgf);
00404 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00405 parlist,(char *)recipeid,
00406 "?Dictionary?");
00407
00408
00409
00410 elist = vircam_fits_get_ehu(ps.imgf);
00411 vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00412 parlist,(char *)recipeid,
00413 "?Dictionary?");
00414
00415
00416
00417 if (cpl_table_save(ps.stds,plist,elist,outfile,CPL_IO_DEFAULT)
00418 != CPL_ERROR_NONE) {
00419 cpl_msg_error(fctid,"Cannot save product");
00420 cpl_frame_delete(product_frame);
00421 return(-1);
00422 }
00423 cpl_frameset_insert(framelist,product_frame);
00424
00425
00426
00427 } else {
00428
00429
00430
00431 elist = vircam_fits_get_ehu(ps.imgf);
00432 vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00433 parlist,(char *)recipeid,
00434 "?Dictionary?");
00435
00436 if (cpl_table_save(ps.stds,NULL,elist,outfile,CPL_IO_EXTEND)
00437 != CPL_ERROR_NONE) {
00438 cpl_msg_error(fctid,"Cannot save product table extension");
00439 return(-1);
00440 }
00441 }
00442
00443 return(0);
00444 }
00445
00446
00447
00451
00452
00453 static void vircam_getstds_init(void) {
00454 ps.img = NULL;
00455 ps.imgf = NULL;
00456 ps.labels = NULL;
00457 ps.plist = NULL;
00458 ps.stds = NULL;
00459 ps.catname = NULL;
00460 ps.catpath = NULL;
00461 }
00462
00463
00464
00468
00469
00470 static void vircam_getstds_tidy(void) {
00471 freeframe(ps.img);
00472 freefits(ps.imgf);
00473 freespace(ps.labels);
00474 freepropertylist(ps.plist);
00475 freetable(ps.stds);
00476 freespace(ps.catname);
00477 freespace(ps.catpath);
00478 }
00479
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
00527
00528
00529
00530
00531
00532
00533
00534
00535