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_flatcor_create(cpl_plugin *) ;
00045 static int vircam_flatcor_exec(cpl_plugin *) ;
00046 static int vircam_flatcor_destroy(cpl_plugin *) ;
00047 static int vircam_flatcor_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_flatcor_save(cpl_frameset *framelist,
00049 cpl_parameterlist *parlist);
00050 static void vircam_flatcor_init(void);
00051 static void vircam_flatcor_tidy(void);
00052
00053 static struct {
00054
00055
00056
00057 int extenum;
00058
00059 } vircam_flatcor_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 } ps;
00068
00069 static int isfirst;
00070 static cpl_frame *product_frame = NULL;
00071
00072 static char vircam_flatcor_description[] =
00073 "vircam_flatcor -- VIRCAM flat correction test recipe.\n\n"
00074 "Flat correct an input frame using a master flat frame\n\n"
00075 "The program accepts the following files in the SOF:\n\n"
00076 " Tag Description\n"
00077 " -----------------------------------------------------------------------\n"
00078 " %-21s A input uncorrected image\n"
00079 " %-21s A master flat field frame\n"
00080 "\n";
00081
00127
00128
00129
00137
00138
00139 int cpl_plugin_get_info(cpl_pluginlist *list) {
00140 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00141 cpl_plugin *plugin = &recipe->interface;
00142 char alldesc[SZ_ALLDESC];
00143 (void)snprintf(alldesc,SZ_ALLDESC,vircam_flatcor_description,
00144 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_TWILIGHT_FLAT);
00145
00146 cpl_plugin_init(plugin,
00147 CPL_PLUGIN_API,
00148 VIRCAM_BINARY_VERSION,
00149 CPL_PLUGIN_TYPE_RECIPE,
00150 "vircam_flatcor",
00151 "VIRCAM flat field division test recipe [test]",
00152 alldesc,
00153 "Jim Lewis",
00154 "jrl@ast.cam.ac.uk",
00155 vircam_get_license(),
00156 vircam_flatcor_create,
00157 vircam_flatcor_exec,
00158 vircam_flatcor_destroy);
00159
00160 cpl_pluginlist_append(list,plugin);
00161
00162 return(0);
00163 }
00164
00165
00174
00175
00176 static int vircam_flatcor_create(cpl_plugin *plugin) {
00177 cpl_recipe *recipe;
00178 cpl_parameter *p;
00179
00180
00181
00182 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00183 recipe = (cpl_recipe *)plugin;
00184 else
00185 return(-1);
00186
00187
00188
00189 recipe->parameters = cpl_parameterlist_new();
00190
00191
00192
00193 p = cpl_parameter_new_range("vircam.vircam_flatcor.extenum",
00194 CPL_TYPE_INT,
00195 "Extension number to be done, 0 == all",
00196 "vircam.vircam_flatcor",1,0,16);
00197 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00198 cpl_parameterlist_append(recipe->parameters,p);
00199
00200
00201
00202 return(0);
00203 }
00204
00205
00211
00212
00213 static int vircam_flatcor_exec(cpl_plugin *plugin) {
00214 cpl_recipe *recipe;
00215
00216
00217
00218 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00219 recipe = (cpl_recipe *)plugin;
00220 else
00221 return(-1);
00222
00223 return(vircam_flatcor_test(recipe->parameters,recipe->frames));
00224 }
00225
00226
00227
00233
00234
00235 static int vircam_flatcor_destroy(cpl_plugin *plugin) {
00236 cpl_recipe *recipe ;
00237
00238
00239
00240 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00241 recipe = (cpl_recipe *)plugin;
00242 else
00243 return(-1);
00244
00245 cpl_parameterlist_delete(recipe->parameters);
00246 return(0);
00247 }
00248
00249
00256
00257
00258 static int vircam_flatcor_test(cpl_parameterlist *parlist,
00259 cpl_frameset *framelist) {
00260 const char *fctid="vircam_flatcor";
00261 cpl_parameter *p;
00262 int nlab,jst,jfn,status,j;
00263
00264
00265
00266 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00267 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00268 return(-1);
00269 }
00270
00271
00272
00273 vircam_flatcor_init();
00274
00275
00276
00277 p = cpl_parameterlist_find(parlist,"vircam.vircam_flatcor.extenum");
00278 vircam_flatcor_config.extenum = cpl_parameter_get_int(p);
00279
00280
00281
00282 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00283 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00284 vircam_flatcor_tidy();
00285 return(-1);
00286 }
00287
00288
00289
00290 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00291 &nlab)) == NULL) {
00292 cpl_msg_error(fctid,"Cannot labelise the input frames");
00293 vircam_flatcor_tidy();
00294 return(-1);
00295 }
00296 if ((ps.flat = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00297 VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
00298 cpl_msg_info(fctid,"No master flat found -- cannot continue");
00299 vircam_flatcor_tidy();
00300 return(-1);
00301 }
00302 if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00303 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00304 cpl_msg_info(fctid,"No raw image found -- cannot continue");
00305 vircam_flatcor_tidy();
00306 return(-1);
00307 }
00308
00309
00310
00311
00312
00313 vircam_exten_range(vircam_flatcor_config.extenum,(const cpl_frame *)ps.img,
00314 &jst,&jfn);
00315 if (jst == -1 || jfn == -1) {
00316 cpl_msg_error(fctid,"Unable to continue");
00317 vircam_flatcor_tidy();
00318 return(-1);
00319 }
00320
00321
00322
00323 status = VIR_OK;
00324 for (j = jst; j <= jfn; j++) {
00325 isfirst = (j == jst);
00326
00327
00328
00329 ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00330 ps.flatf = vircam_fits_load(ps.flat,CPL_TYPE_FLOAT,j);
00331 if (ps.img == NULL || ps.flatf == NULL) {
00332 vircam_flatcor_tidy();
00333 return(-1);
00334 }
00335
00336
00337
00338 cpl_msg_info(fctid,"Doing the flat fielding for extension %d",j);
00339 (void)vircam_flatcor(ps.imgf,ps.flatf,&status);
00340 if (status != VIR_OK) {
00341 vircam_flatcor_tidy();
00342 return(-1);
00343 }
00344
00345
00346
00347 cpl_msg_info(fctid,"Saving results for extension %d",j);
00348 if (vircam_flatcor_save(framelist,parlist) != 0) {
00349 vircam_flatcor_tidy();
00350 return(-1);
00351 }
00352
00353
00354
00355 freefits(ps.imgf);
00356 freefits(ps.flatf);
00357 }
00358 vircam_flatcor_tidy();
00359 return(0);
00360 }
00361
00362
00363
00370
00371
00372 static int vircam_flatcor_save(cpl_frameset *framelist,
00373 cpl_parameterlist *parlist) {
00374 const char *fctid = "vircam_flatcor_save";
00375 const char *outfile = "flatcor.fits";
00376 const char *recipeid = "vircam_flatcor";
00377 cpl_propertylist *plist;
00378
00379
00380
00381
00382 if (isfirst) {
00383
00384
00385
00386 product_frame = cpl_frame_new();
00387 cpl_frame_set_filename(product_frame,outfile);
00388 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00389 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00390 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00391 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00392
00393
00394
00395 plist = vircam_fits_get_phu(ps.imgf);
00396 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00397 parlist,(char *)recipeid,
00398 "?Dictionary?");
00399
00400
00401
00402 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00403 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00404 cpl_msg_error(fctid,"Cannot save product PHU");
00405 cpl_frame_delete(product_frame);
00406 return(-1);
00407 }
00408 cpl_frameset_insert(framelist,product_frame);
00409 }
00410
00411
00412
00413 plist = vircam_fits_get_ehu(ps.imgf);
00414
00415
00416
00417 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00418 (char *)recipeid,"?Dictionary?");
00419
00420
00421
00422 if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_BPP_IEEE_FLOAT,
00423 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00424 cpl_msg_error(fctid,"Cannot save product image extension");
00425 return(-1);
00426 }
00427
00428 return(0);
00429 }
00430
00431
00432
00436
00437
00438 static void vircam_flatcor_init(void) {
00439 ps.labels = NULL;
00440 ps.flat = NULL;
00441 ps.flatf = NULL;
00442 ps.img = NULL;
00443 ps.imgf = NULL;
00444 }
00445
00446
00447
00451
00452
00453 static void vircam_flatcor_tidy(void) {
00454 freespace(ps.labels);
00455 freefits(ps.imgf);
00456 freefits(ps.flatf);
00457 freeframe(ps.flat);
00458 freeframe(ps.img);
00459 }
00460
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
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