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_pfits.h"
00042
00043
00044
00045 static int vircam_lincor_create(cpl_plugin *) ;
00046 static int vircam_lincor_exec(cpl_plugin *) ;
00047 static int vircam_lincor_destroy(cpl_plugin *) ;
00048 static int vircam_lincor_test(cpl_parameterlist *, cpl_frameset *) ;
00049 static int vircam_lincor_save(cpl_frameset *framelist,
00050 cpl_parameterlist *parlist);
00051 static void vircam_lincor_init(void);
00052 static void vircam_lincor_tidy(void);
00053
00054 static struct {
00055
00056
00057
00058 int extenum;
00059
00060 } vircam_lincor_config;
00061
00062 static struct {
00063 int *labels;
00064 cpl_frame *chan;
00065 cpl_frame *img;
00066 vir_tfits *chanf;
00067 vir_fits *imgf;
00068 } ps;
00069
00070 static int isfirst;
00071 static cpl_frame *product_frame = NULL;
00072
00073 static char vircam_lincor_description[] =
00074 "vircam_lincor -- VIRCAM linearity correction test recipe.\n\n"
00075 "Linearity correct an input frame using a pre-existing channel table\n\n"
00076 "The program accepts the following files in the SOF:\n\n"
00077 " Tag Description\n"
00078 " -----------------------------------------------------------------------\n"
00079 " %-21s A input uncorrected image\n"
00080 " %-21s A channel table\n"
00081 "\n";
00082
00129
00130
00131
00139
00140
00141 int cpl_plugin_get_info(cpl_pluginlist *list) {
00142 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00143 cpl_plugin *plugin = &recipe->interface;
00144 char alldesc[SZ_ALLDESC];
00145 (void)snprintf(alldesc,SZ_ALLDESC,vircam_lincor_description,
00146 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CHANTAB);
00147
00148 cpl_plugin_init(plugin,
00149 CPL_PLUGIN_API,
00150 VIRCAM_BINARY_VERSION,
00151 CPL_PLUGIN_TYPE_RECIPE,
00152 "vircam_lincor",
00153 "VIRCAM linearisation test recipe [test]",
00154 alldesc,
00155 "Jim Lewis",
00156 "jrl@ast.cam.ac.uk",
00157 vircam_get_license(),
00158 vircam_lincor_create,
00159 vircam_lincor_exec,
00160 vircam_lincor_destroy);
00161
00162 cpl_pluginlist_append(list,plugin);
00163
00164 return(0);
00165 }
00166
00167
00176
00177
00178 static int vircam_lincor_create(cpl_plugin *plugin) {
00179 cpl_recipe *recipe;
00180 cpl_parameter *p;
00181
00182
00183
00184 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00185 recipe = (cpl_recipe *)plugin;
00186 else
00187 return(-1);
00188
00189
00190
00191 recipe->parameters = cpl_parameterlist_new();
00192
00193
00194
00195 p = cpl_parameter_new_range("vircam.vircam_lincor.extenum",
00196 CPL_TYPE_INT,
00197 "Extension number to be done, 0 == all",
00198 "vircam.vircam_lincor",1,0,16);
00199 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00200 cpl_parameterlist_append(recipe->parameters,p);
00201
00202
00203
00204 return(0);
00205 }
00206
00207
00213
00214
00215 static int vircam_lincor_exec(cpl_plugin *plugin) {
00216 cpl_recipe *recipe;
00217
00218
00219
00220 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00221 recipe = (cpl_recipe *)plugin;
00222 else
00223 return(-1);
00224
00225 return(vircam_lincor_test(recipe->parameters,recipe->frames));
00226 }
00227
00228
00229
00235
00236
00237 static int vircam_lincor_destroy(cpl_plugin *plugin) {
00238 cpl_recipe *recipe ;
00239
00240
00241
00242 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00243 recipe = (cpl_recipe *)plugin;
00244 else
00245 return(-1);
00246
00247 cpl_parameterlist_delete(recipe->parameters);
00248 return(0);
00249 }
00250
00251
00258
00259
00260 static int vircam_lincor_test(cpl_parameterlist *parlist,
00261 cpl_frameset *framelist) {
00262 const char *fctid="vircam_lincor";
00263 cpl_parameter *p;
00264 int nlab,jst,jfn,status,j,ndit;
00265 cpl_propertylist *pp;
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_lincor_init();
00277
00278
00279
00280 p = cpl_parameterlist_find(parlist,"vircam.vircam_lincor.extenum");
00281 vircam_lincor_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_lincor_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_lincor_tidy();
00297 return(-1);
00298 }
00299 if ((ps.chan = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00300 VIRCAM_CAL_CHANTAB)) == NULL) {
00301 cpl_msg_info(fctid,"No channel table found -- cannot continue");
00302 vircam_lincor_tidy();
00303 return(-1);
00304 }
00305 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00306 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00307 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00308 vircam_lincor_tidy();
00309 return(-1);
00310 }
00311
00312
00313
00314 pp = cpl_propertylist_load(cpl_frame_get_filename(ps.img),0);
00315 if (vircam_pfits_get_ndit(pp,&ndit) != VIR_OK) {
00316 cpl_msg_error(fctid,"No value for NDIT available");
00317 freepropertylist(pp);
00318 vircam_lincor_tidy();
00319 return(-1);
00320 }
00321 cpl_propertylist_delete(pp);
00322
00323
00324
00325
00326
00327 vircam_exten_range(vircam_lincor_config.extenum,(const cpl_frame *)ps.img,
00328 &jst,&jfn);
00329 if (jst == -1 || jfn == -1) {
00330 cpl_msg_error(fctid,"Unable to continue");
00331 vircam_lincor_tidy();
00332 return(-1);
00333 }
00334
00335
00336
00337 status = VIR_OK;
00338 for (j = jst; j <= jfn; j++) {
00339 isfirst = (j == jst);
00340
00341
00342
00343 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00344 ps.chanf = vircam_tfits_load(ps.chan,j);
00345 if (ps.img == NULL || ps.chanf == NULL) {
00346 vircam_lincor_tidy();
00347 return(-1);
00348 }
00349
00350
00351
00352 cpl_msg_info(fctid,"Doing the linearisation for extension %d",j);
00353 (void)vircam_lincor(ps.imgf,ps.chanf,1,ndit,&status);
00354 if (status != VIR_OK) {
00355 vircam_lincor_tidy();
00356 return(-1);
00357 }
00358
00359
00360
00361 cpl_msg_info(fctid,"Saving results for extension %d",j);
00362 if (vircam_lincor_save(framelist,parlist) != 0) {
00363 vircam_lincor_tidy();
00364 return(-1);
00365 }
00366
00367
00368
00369 freefits(ps.imgf);
00370 freetfits(ps.chanf);
00371 }
00372 vircam_lincor_tidy();
00373 return(0);
00374 }
00375
00376
00377
00384
00385
00386 static int vircam_lincor_save(cpl_frameset *framelist,
00387 cpl_parameterlist *parlist) {
00388 const char *recipeid = "vircam_lincor";
00389 const char *fctid = "vircam_lincor_save";
00390 const char *outfile = "lincor.fits";
00391 cpl_propertylist *plist;
00392
00393
00394
00395
00396 if (isfirst) {
00397
00398
00399
00400 product_frame = cpl_frame_new();
00401 cpl_frame_set_filename(product_frame,outfile);
00402 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00403 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00404 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00405 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00406
00407
00408
00409 plist = vircam_fits_get_phu(ps.imgf);
00410 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00411 parlist,(char *)recipeid,
00412 "?Dictionary?",NULL,0);
00413
00414
00415
00416 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00417 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00418 cpl_msg_error(fctid,"Cannot save product PHU");
00419 cpl_frame_delete(product_frame);
00420 return(-1);
00421 }
00422 cpl_frameset_insert(framelist,product_frame);
00423 }
00424
00425
00426
00427 plist = vircam_fits_get_ehu(ps.imgf);
00428
00429
00430
00431 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00432 (char *)recipeid,"?Dictionary?",NULL);
00433
00434
00435
00436 if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_BPP_IEEE_FLOAT,
00437 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00438 cpl_msg_error(fctid,"Cannot save product image extension");
00439 return(-1);
00440 }
00441
00442 return(0);
00443 }
00444
00445
00446
00450
00451
00452 static void vircam_lincor_init(void) {
00453 ps.labels = NULL;
00454 ps.chan = NULL;
00455 ps.chanf = NULL;
00456 ps.img = NULL;
00457 ps.imgf = NULL;
00458 }
00459
00460
00461
00465
00466
00467 static void vircam_lincor_tidy(void) {
00468 freespace(ps.labels);
00469 freefits(ps.imgf);
00470 freetfits(ps.chanf);
00471 freeframe(ps.chan);
00472 freeframe(ps.img);
00473 }
00474
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
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526