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 int *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 nlab,jst,jfn,status,j;
00291
00292
00293
00294 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00295 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00296 return(-1);
00297 }
00298
00299
00300
00301 vircam_platesol_init();
00302
00303
00304
00305 p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.nconst");
00306 vircam_platesol_config.nconst = cpl_parameter_get_int(p);
00307 p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.shiftan");
00308 vircam_platesol_config.shiftan = cpl_parameter_get_bool(p);
00309 p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.extenum");
00310 vircam_platesol_config.extenum = cpl_parameter_get_int(p);
00311
00312
00313
00314 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00315 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00316 vircam_platesol_tidy();
00317 return(-1);
00318 }
00319
00320
00321
00322 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00323 &nlab)) == NULL) {
00324 cpl_msg_error(fctid,"Cannot labelise the input frames");
00325 vircam_platesol_tidy();
00326 return(-1);
00327 }
00328 if ((ps.mstds = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00329 VIRCAM_CAL_MSTDTAB)) == NULL) {
00330 cpl_msg_info(fctid,"No matched standards table found -- cannot continue");
00331 vircam_platesol_tidy();
00332 return(-1);
00333 }
00334 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00335 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00336 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00337 vircam_platesol_tidy();
00338 return(-1);
00339 }
00340
00341
00342
00343
00344
00345 vircam_exten_range(vircam_platesol_config.extenum,(const cpl_frame *)ps.img,
00346 &jst,&jfn);
00347 if (jst == -1 || jfn == -1) {
00348 cpl_msg_error(fctid,"Unable to continue");
00349 vircam_platesol_tidy();
00350 return(-1);
00351 }
00352
00353
00354
00355 status = VIR_OK;
00356 for (j = jst; j <= jfn; j++) {
00357
00358
00359
00360 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00361 ps.mstdsf = vircam_tfits_load(ps.mstds,j);
00362 if (ps.img == NULL || ps.mstdsf == NULL) {
00363 freefits(ps.imgf);
00364 freetfits(ps.mstdsf);
00365 cpl_msg_warning(fctid,"Unable to load one of the inputs");
00366 continue;
00367 }
00368
00369
00370
00371 cpl_msg_info(fctid,"Doing the plate solution for extension %d",j);
00372 (void)vircam_platesol(vircam_fits_get_ehu(ps.imgf),NULL,
00373 vircam_tfits_get_table(ps.mstdsf),
00374 vircam_platesol_config.nconst,
00375 vircam_platesol_config.shiftan,&status);
00376 if (status != VIR_OK) {
00377 cpl_msg_warning(fctid,"Plate solution failed");
00378 status = VIR_OK;
00379 }
00380
00381
00382
00383 cpl_msg_info(fctid,"Saving results for extension %d",j);
00384 if (vircam_platesol_save() != 0)
00385 cpl_msg_warning(fctid,"Save routine failed");
00386
00387
00388
00389 freefits(ps.imgf);
00390 freetfits(ps.mstdsf);
00391 }
00392 vircam_platesol_tidy();
00393 return(0);
00394 }
00395
00396
00401
00402
00403 static int vircam_platesol_save(void) {
00404 const char *fctid = "vircam_platesol_save";
00405 const char *outfile = "platesol";
00406 const char *keys[] = {"CRVAL1","CRVAL2","CRPIX1","CRPIX2","CD1_1","CD1_2",
00407 "CD2_1","CD2_2","PV2_3","ESO DRS NUMBRMS",
00408 "ESO DRS STDCRMS","ESO DRS WCSRAOFF",
00409 "ESO DRS WCSDECOFF"};
00410 int i,nkeys=13;
00411 cpl_propertylist *plist,*p2,*p3;
00412
00413
00414
00415 plist = vircam_fits_get_ehu(ps.imgf);
00416
00417
00418
00419 if ((p2 = vircam_paf_req_items(plist)) == NULL) {
00420 cpl_msg_error(fctid,"Unable to find required items in header");
00421 return(-1);
00422 }
00423 p3 = vircam_paf_phu_items(vircam_fits_get_phu(ps.imgf));
00424 vircam_merge_propertylists(p2,p3);
00425 freepropertylist(p3);
00426
00427
00428
00429 for (i = 0; i < nkeys; i++) {
00430 cpl_propertylist_copy_property(p2,plist,keys[i]);
00431 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00432 cpl_msg_error(fctid,"A header parameter %s is missing",keys[i]);
00433 cpl_propertylist_delete(p2);
00434 return(-1);
00435 }
00436 }
00437
00438
00439
00440 if (vircam_paf_print((char *)outfile,"VIRCAM/vircam_platesol",
00441 "Test QC file",p2) != VIR_OK) {
00442 cpl_msg_error(fctid,"Error writing PAF\n");
00443 cpl_propertylist_delete(p2);
00444 return(-1);
00445 }
00446
00447
00448
00449 cpl_propertylist_delete(p2);
00450 return(0);
00451 }
00452
00453
00454
00458
00459
00460 static void vircam_platesol_init(void) {
00461 ps.labels = NULL;
00462 ps.img = NULL;
00463 ps.imgf = NULL;
00464 ps.mstds = NULL;
00465 ps.mstdsf = NULL;
00466 }
00467
00468
00469
00473
00474
00475 static void vircam_platesol_tidy(void) {
00476 freespace(ps.labels);
00477 freefits(ps.imgf);
00478 freetfits(ps.mstdsf);
00479 freeframe(ps.mstds);
00480 freeframe(ps.img);
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
00527
00528
00529
00530
00531
00532