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 #include "vircam_paf.h"
00042
00043
00044
00045 static int vircam_platesol_create(cpl_plugin *) ;
00046 static int vircam_platesol_exec(cpl_plugin *) ;
00047 static int vircam_platesol_destroy(cpl_plugin *) ;
00048 static int vircam_platesol_test(cpl_parameterlist *, cpl_frameset *) ;
00049 static int vircam_platesol_save(void);
00050 static void vircam_platesol_init(void);
00051 static void vircam_platesol_tidy(void);
00052
00053 static struct {
00054
00055
00056
00057 int nconst;
00058 int shiftan;
00059 int extenum;
00060
00061 } vircam_platesol_config;
00062
00063 static struct {
00064 cpl_size *labels;
00065 cpl_frame *img;
00066 cpl_frame *mstds;
00067 vir_fits *imgf;
00068 vir_tfits *mstdsf;
00069 FILE *paf;
00070 } ps;
00071
00072
00073 static char vircam_platesol_description[] =
00074 "vircam_platesol -- VIRCAM plate solution fitting test recipe.\n\n"
00075 "Fit a plate solution to a matched standards table and write the resulting\n"
00076 "WCS to an input image header.\n\n"
00077 "The program accepts the following files in the SOF:\n\n"
00078 " Tag Description\n"
00079 " -----------------------------------------------------------------------\n"
00080 " %-21s A input uncorrected image\n"
00081 " %-21s A matched standards table\n"
00082 "The WCS values from the solution are written to a paf file. This is the\n"
00083 "only product of this recipe\n"
00084 "\n";
00085
00137
00138
00139
00140
00148
00149
00150 int cpl_plugin_get_info(cpl_pluginlist *list) {
00151 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00152 cpl_plugin *plugin = &recipe->interface;
00153 char alldesc[SZ_ALLDESC];
00154 (void)snprintf(alldesc,SZ_ALLDESC,vircam_platesol_description,
00155 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_MSTDTAB);
00156
00157 cpl_plugin_init(plugin,
00158 CPL_PLUGIN_API,
00159 VIRCAM_BINARY_VERSION,
00160 CPL_PLUGIN_TYPE_RECIPE,
00161 "vircam_platesol",
00162 "VIRCAM plate solution test recipe [test]",
00163 alldesc,
00164 "Jim Lewis",
00165 "jrl@ast.cam.ac.uk",
00166 vircam_get_license(),
00167 vircam_platesol_create,
00168 vircam_platesol_exec,
00169 vircam_platesol_destroy);
00170
00171 cpl_pluginlist_append(list,plugin);
00172
00173 return(0);
00174 }
00175
00176
00185
00186
00187 static int vircam_platesol_create(cpl_plugin *plugin) {
00188 cpl_recipe *recipe;
00189 cpl_parameter *p;
00190
00191
00192
00193 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00194 recipe = (cpl_recipe *)plugin;
00195 else
00196 return(-1);
00197
00198
00199
00200 recipe->parameters = cpl_parameterlist_new();
00201
00202
00203
00204 p = cpl_parameter_new_enum("vircam.vircam_platesol.nconst",
00205 CPL_TYPE_INT,
00206 "Number of plate constants",
00207 "vircam.vircam_platesol",6,2,4,6);
00208 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nconst");
00209 cpl_parameterlist_append(recipe->parameters,p);
00210
00211
00212
00213 p = cpl_parameter_new_value("vircam.vircam_platesol.shiftan",
00214 CPL_TYPE_BOOL,
00215 "Shift position of tangent point",
00216 "vircam.vircam_platesol",FALSE);
00217 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"shiftan");
00218 cpl_parameterlist_append(recipe->parameters,p);
00219
00220
00221
00222 p = cpl_parameter_new_range("vircam.vircam_platesol.extenum",
00223 CPL_TYPE_INT,
00224 "Extension number to be done, 0 == all",
00225 "vircam.vircam_platesol",1,0,16);
00226 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00227 cpl_parameterlist_append(recipe->parameters,p);
00228
00229
00230
00231 return(0);
00232 }
00233
00234
00240
00241
00242 static int vircam_platesol_exec(cpl_plugin *plugin) {
00243 cpl_recipe *recipe;
00244
00245
00246
00247 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00248 recipe = (cpl_recipe *)plugin;
00249 else
00250 return(-1);
00251
00252 return(vircam_platesol_test(recipe->parameters,recipe->frames));
00253 }
00254
00255
00261
00262
00263 static int vircam_platesol_destroy(cpl_plugin *plugin) {
00264 cpl_recipe *recipe ;
00265
00266
00267
00268 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00269 recipe = (cpl_recipe *)plugin;
00270 else
00271 return(-1);
00272
00273 cpl_parameterlist_delete(recipe->parameters);
00274 return(0);
00275 }
00276
00277
00284
00285
00286 static int vircam_platesol_test(cpl_parameterlist *parlist,
00287 cpl_frameset *framelist) {
00288 const char *fctid="vircam_platesol";
00289 cpl_parameter *p;
00290 int jst,jfn,status,j;
00291 cpl_size nlab;
00292
00293
00294
00295 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00296 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00297 return(-1);
00298 }
00299
00300
00301
00302 vircam_platesol_init();
00303
00304
00305
00306 p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.nconst");
00307 vircam_platesol_config.nconst = cpl_parameter_get_int(p);
00308 p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.shiftan");
00309 vircam_platesol_config.shiftan = cpl_parameter_get_bool(p);
00310 p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.extenum");
00311 vircam_platesol_config.extenum = cpl_parameter_get_int(p);
00312
00313
00314
00315 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00316 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00317 vircam_platesol_tidy();
00318 return(-1);
00319 }
00320
00321
00322
00323 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00324 &nlab)) == NULL) {
00325 cpl_msg_error(fctid,"Cannot labelise the input frames");
00326 vircam_platesol_tidy();
00327 return(-1);
00328 }
00329 if ((ps.mstds = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00330 VIRCAM_CAL_MSTDTAB)) == NULL) {
00331 cpl_msg_info(fctid,"No matched standards table found -- cannot continue");
00332 vircam_platesol_tidy();
00333 return(-1);
00334 }
00335 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00336 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00337 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00338 vircam_platesol_tidy();
00339 return(-1);
00340 }
00341
00342
00343
00344
00345
00346 vircam_exten_range(vircam_platesol_config.extenum,(const cpl_frame *)ps.img,
00347 &jst,&jfn);
00348 if (jst == -1 || jfn == -1) {
00349 cpl_msg_error(fctid,"Unable to continue");
00350 vircam_platesol_tidy();
00351 return(-1);
00352 }
00353
00354
00355
00356 status = VIR_OK;
00357 for (j = jst; j <= jfn; j++) {
00358
00359
00360
00361 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00362 ps.mstdsf = vircam_tfits_load(ps.mstds,j);
00363 if (ps.img == NULL || ps.mstdsf == NULL) {
00364 freefits(ps.imgf);
00365 freetfits(ps.mstdsf);
00366 cpl_msg_warning(fctid,"Unable to load one of the inputs");
00367 continue;
00368 }
00369
00370
00371
00372 cpl_msg_info(fctid,"Doing the plate solution for extension %" CPL_SIZE_FORMAT,
00373 (cpl_size)j);
00374 (void)vircam_platesol(vircam_fits_get_ehu(ps.imgf),NULL,
00375 vircam_tfits_get_table(ps.mstdsf),
00376 vircam_platesol_config.nconst,
00377 vircam_platesol_config.shiftan,&status);
00378 if (status != VIR_OK) {
00379 cpl_msg_warning(fctid,"Plate solution failed");
00380 status = VIR_OK;
00381 }
00382
00383
00384
00385 cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
00386 (cpl_size)j);
00387 if (vircam_platesol_save() != 0)
00388 cpl_msg_warning(fctid,"Save routine failed");
00389
00390
00391
00392 freefits(ps.imgf);
00393 freetfits(ps.mstdsf);
00394 }
00395 vircam_platesol_tidy();
00396 return(0);
00397 }
00398
00399
00404
00405
00406 static int vircam_platesol_save(void) {
00407 const char *fctid = "vircam_platesol_save";
00408 const char *outfile = "platesol";
00409 const char *keys[] = {"CRVAL1","CRVAL2","CRPIX1","CRPIX2","CD1_1","CD1_2",
00410 "CD2_1","CD2_2","PV2_3","ESO DRS NUMBRMS",
00411 "ESO DRS STDCRMS","ESO DRS WCSRAOFF",
00412 "ESO DRS WCSDECOFF"};
00413 int i,nkeys=13;
00414 cpl_propertylist *plist,*p2,*p3;
00415
00416
00417
00418 plist = vircam_fits_get_ehu(ps.imgf);
00419
00420
00421
00422 if ((p2 = vircam_paf_req_items(plist)) == NULL) {
00423 cpl_msg_error(fctid,"Unable to find required items in header");
00424 return(-1);
00425 }
00426 p3 = vircam_paf_phu_items(vircam_fits_get_phu(ps.imgf));
00427 vircam_merge_propertylists(p2,p3);
00428 freepropertylist(p3);
00429
00430
00431
00432 for (i = 0; i < nkeys; i++) {
00433 cpl_propertylist_copy_property(p2,plist,keys[i]);
00434 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00435 cpl_msg_error(fctid,"A header parameter %s is missing",keys[i]);
00436 cpl_propertylist_delete(p2);
00437 return(-1);
00438 }
00439 }
00440
00441
00442
00443 if (vircam_paf_print((char *)outfile,"VIRCAM/vircam_platesol",
00444 "Test QC file",p2) != VIR_OK) {
00445 cpl_msg_error(fctid,"Error writing PAF");
00446 cpl_propertylist_delete(p2);
00447 return(-1);
00448 }
00449
00450
00451
00452 cpl_propertylist_delete(p2);
00453 return(0);
00454 }
00455
00456
00457
00461
00462
00463 static void vircam_platesol_init(void) {
00464 ps.labels = NULL;
00465 ps.img = NULL;
00466 ps.imgf = NULL;
00467 ps.mstds = NULL;
00468 ps.mstdsf = NULL;
00469 }
00470
00471
00472
00476
00477
00478 static void vircam_platesol_tidy(void) {
00479 freespace(ps.labels);
00480 freefits(ps.imgf);
00481 freetfits(ps.mstdsf);
00482 freeframe(ps.mstds);
00483 freeframe(ps.img);
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
00536
00537
00538
00539
00540
00541