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_gaincor_create(cpl_plugin *) ;
00045 static int vircam_gaincor_exec(cpl_plugin *) ;
00046 static int vircam_gaincor_destroy(cpl_plugin *) ;
00047 static int vircam_gaincor_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_gaincor_save(cpl_frameset *framelist,
00049 cpl_parameterlist *parlist);
00050 static void vircam_gaincor_init(void);
00051 static void vircam_gaincor_tidy(void);
00052
00053 static struct {
00054
00055
00056
00057 int extenum;
00058
00059 } vircam_gaincor_config;
00060
00061 static struct {
00062 int *labels;
00063 cpl_frame *flat;
00064 cpl_frame *img;
00065 vir_fits *flatf;
00066 vir_fits *imgf;
00067 float *gaincors;
00068 } ps;
00069
00070 static int isfirst;
00071 static cpl_frame *product_frame = NULL;
00072
00073 static char vircam_gaincor_description[] =
00074 "vircam_gaincor -- VIRCAM gain correction test recipe.\n\n"
00075 "Gain correct an input frame using a master flat 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 uncorrected image\n"
00080 " %-21s A master flat field frame\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_gaincor_description,
00145 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_TWILIGHT_FLAT);
00146
00147 cpl_plugin_init(plugin,
00148 CPL_PLUGIN_API,
00149 VIRCAM_BINARY_VERSION,
00150 CPL_PLUGIN_TYPE_RECIPE,
00151 "vircam_gaincor",
00152 "VIRCAM gain correction test recipe [test]",
00153 alldesc,
00154 "Jim Lewis",
00155 "jrl@ast.cam.ac.uk",
00156 vircam_get_license(),
00157 vircam_gaincor_create,
00158 vircam_gaincor_exec,
00159 vircam_gaincor_destroy);
00160
00161 cpl_pluginlist_append(list,plugin);
00162
00163 return(0);
00164 }
00165
00166
00175
00176
00177 static int vircam_gaincor_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_gaincor.extenum",
00195 CPL_TYPE_INT,
00196 "Extension number to be done, 0 == all",
00197 "vircam.vircam_gaincor",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_gaincor_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_gaincor_test(recipe->parameters,recipe->frames));
00225 }
00226
00227
00228
00234
00235
00236 static int vircam_gaincor_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
00257
00258
00259 static int vircam_gaincor_test(cpl_parameterlist *parlist,
00260 cpl_frameset *framelist) {
00261 const char *fctid="vircam_gaincor";
00262 cpl_parameter *p;
00263 int nlab,jst,jfn,status,j;
00264 float gaincor_fac;
00265
00266
00267
00268 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00269 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00270 return(-1);
00271 }
00272
00273
00274
00275 vircam_gaincor_init();
00276
00277
00278
00279 p = cpl_parameterlist_find(parlist,"vircam.vircam_gaincor.extenum");
00280 vircam_gaincor_config.extenum = cpl_parameter_get_int(p);
00281
00282
00283
00284 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00285 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00286 vircam_gaincor_tidy();
00287 return(-1);
00288 }
00289
00290
00291
00292 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00293 &nlab)) == NULL) {
00294 cpl_msg_error(fctid,"Cannot labelise the input frames");
00295 vircam_gaincor_tidy();
00296 return(-1);
00297 }
00298 if ((ps.flat = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00299 VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
00300 cpl_msg_info(fctid,"No master flat found -- cannot continue");
00301 vircam_gaincor_tidy();
00302 return(-1);
00303 }
00304 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00305 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00306 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00307 vircam_gaincor_tidy();
00308 return(-1);
00309 }
00310
00311
00312
00313 status = VIR_OK;
00314 if (vircam_gaincor_calc(ps.flat,&j,&(ps.gaincors),&status) != VIR_OK) {
00315 cpl_msg_error(fctid,"Error calculating gain corrections");
00316 vircam_gaincor_tidy();
00317 return(-1);
00318 }
00319
00320
00321
00322
00323
00324 vircam_exten_range(vircam_gaincor_config.extenum,(const cpl_frame *)ps.img,
00325 &jst,&jfn);
00326 if (jst == -1 || jfn == -1) {
00327 cpl_msg_error(fctid,"Unable to continue");
00328 vircam_gaincor_tidy();
00329 return(-1);
00330 }
00331
00332
00333
00334 status = VIR_OK;
00335 for (j = jst; j <= jfn; j++) {
00336 isfirst = (j == jst);
00337 gaincor_fac = (ps.gaincors)[j-1];
00338
00339
00340
00341 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00342 ps.flatf = vircam_fits_load(ps.flat,CPL_TYPE_FLOAT,j);
00343 if (ps.img == NULL || ps.flatf == NULL) {
00344 vircam_gaincor_tidy();
00345 return(-1);
00346 }
00347
00348
00349
00350 cpl_msg_info(fctid,"Doing the flat fielding for extension %d",j);
00351 (void)vircam_gaincor(ps.imgf,gaincor_fac,&status);
00352 if (status != VIR_OK) {
00353 vircam_gaincor_tidy();
00354 return(-1);
00355 }
00356
00357
00358
00359 cpl_msg_info(fctid,"Saving results for extension %d",j);
00360 if (vircam_gaincor_save(framelist,parlist) != 0) {
00361 vircam_gaincor_tidy();
00362 return(-1);
00363 }
00364
00365
00366
00367 freefits(ps.imgf);
00368 freefits(ps.flatf);
00369 }
00370 vircam_gaincor_tidy();
00371 return(0);
00372 }
00373
00374
00375
00382
00383
00384 static int vircam_gaincor_save(cpl_frameset *framelist,
00385 cpl_parameterlist *parlist) {
00386 const char *fctid = "vircam_gaincor_save";
00387 const char *outfile = "gaincor.fits";
00388 const char *recipeid = "vircam_gaincor";
00389 cpl_propertylist *plist;
00390
00391
00392
00393
00394 if (isfirst) {
00395
00396
00397
00398 product_frame = cpl_frame_new();
00399 cpl_frame_set_filename(product_frame,outfile);
00400 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00401 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00402 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00403 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00404
00405
00406
00407 plist = vircam_fits_get_phu(ps.imgf);
00408 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00409 parlist,(char *)recipeid,
00410 "?Dictionary?");
00411
00412
00413
00414 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00415 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00416 cpl_msg_error(fctid,"Cannot save product PHU");
00417 cpl_frame_delete(product_frame);
00418 return(-1);
00419 }
00420 cpl_frameset_insert(framelist,product_frame);
00421 }
00422
00423
00424
00425 plist = vircam_fits_get_ehu(ps.imgf);
00426
00427
00428
00429 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00430 (char *)recipeid,"?Dictionary?");
00431
00432
00433
00434 if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_BPP_IEEE_FLOAT,
00435 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00436 cpl_msg_error(fctid,"Cannot save product image extension");
00437 return(-1);
00438 }
00439
00440 return(0);
00441 }
00442
00443
00444
00448
00449
00450 static void vircam_gaincor_init(void) {
00451 ps.labels = NULL;
00452 ps.flat = NULL;
00453 ps.flatf = NULL;
00454 ps.img = NULL;
00455 ps.imgf = NULL;
00456 ps.gaincors = NULL;
00457 }
00458
00459
00460
00464
00465
00466 static void vircam_gaincor_tidy(void) {
00467 freespace(ps.labels);
00468 freefits(ps.imgf);
00469 freefits(ps.flatf);
00470 freeframe(ps.flat);
00471 freeframe(ps.img);
00472 freespace(ps.gaincors);
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