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_mkconf_create(cpl_plugin *) ;
00045 static int vircam_mkconf_exec(cpl_plugin *) ;
00046 static int vircam_mkconf_destroy(cpl_plugin *) ;
00047 static int vircam_mkconf_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_mkconf_save(cpl_frameset *framelist,
00049 cpl_parameterlist *parlist);
00050 static void vircam_mkconf_init(void);
00051 static void vircam_mkconf_tidy(void);
00052
00053 static struct {
00054
00055
00056
00057 int extenum;
00058
00059 } vircam_mkconf_config;
00060
00061 static struct {
00062 int *labels;
00063 cpl_frame *flat;
00064 vir_mask *bpm;
00065 vir_fits *flatf;
00066 cpl_image *outimg;
00067 cpl_propertylist *drs;
00068 } ps;
00069
00070 static int isfirst;
00071 static cpl_frame *product_frame = NULL;
00072
00073 static char vircam_mkconf_description[] =
00074 "vircam_mkconf -- VIRCAM confidence map generation test recipe.\n\n"
00075 "Create a confidence map using an input flat field and an input mask\n"
00076 "The program accepts the following files in the SOF:\n\n"
00077 " Tag Description\n"
00078 " -----------------------------------------------------------------------\n"
00079 " %-21s A input master flat field\n"
00080 " %-21s A master bad pixel map or\n"
00081 " %-21s A master confidence map\n"
00082 "\n";
00083
00129
00130
00131
00132
00140
00141
00142 int cpl_plugin_get_info(cpl_pluginlist *list) {
00143 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00144 cpl_plugin *plugin = &recipe->interface;
00145 char alldesc[SZ_ALLDESC];
00146 (void)snprintf(alldesc,SZ_ALLDESC,vircam_mkconf_description,
00147 VIRCAM_CAL_TWILIGHT_FLAT,VIRCAM_CAL_BPM,VIRCAM_CAL_CONF);
00148
00149 cpl_plugin_init(plugin,
00150 CPL_PLUGIN_API,
00151 VIRCAM_BINARY_VERSION,
00152 CPL_PLUGIN_TYPE_RECIPE,
00153 "vircam_mkconf",
00154 "VIRCAM confidence map test recipe [test]",
00155 alldesc,
00156 "Jim Lewis",
00157 "jrl@ast.cam.ac.uk",
00158 vircam_get_license(),
00159 vircam_mkconf_create,
00160 vircam_mkconf_exec,
00161 vircam_mkconf_destroy);
00162
00163 cpl_pluginlist_append(list,plugin);
00164
00165 return(0);
00166 }
00167
00168
00177
00178
00179 static int vircam_mkconf_create(cpl_plugin *plugin) {
00180 cpl_recipe *recipe;
00181 cpl_parameter *p;
00182
00183
00184
00185 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00186 recipe = (cpl_recipe *)plugin;
00187 else
00188 return(-1);
00189
00190
00191
00192 recipe->parameters = cpl_parameterlist_new();
00193
00194
00195
00196 p = cpl_parameter_new_range("vircam.vircam_mkconf.extenum",
00197 CPL_TYPE_INT,
00198 "Extension number to be done, 0 == all",
00199 "vircam.vircam_mkconf",1,0,16);
00200 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00201 cpl_parameterlist_append(recipe->parameters,p);
00202
00203
00204
00205 return(0);
00206 }
00207
00208
00214
00215
00216 static int vircam_mkconf_exec(cpl_plugin *plugin) {
00217 cpl_recipe *recipe;
00218
00219
00220
00221 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00222 recipe = (cpl_recipe *)plugin;
00223 else
00224 return(-1);
00225
00226 return(vircam_mkconf_test(recipe->parameters,recipe->frames));
00227 }
00228
00229
00235
00236
00237 static int vircam_mkconf_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_mkconf_test(cpl_parameterlist *parlist,
00261 cpl_frameset *framelist) {
00262 const char *fctid="vircam_mkconf";
00263 cpl_parameter *p;
00264 int nlab,jst,jfn,status,j,nx,ny;
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_mkconf_init();
00276
00277
00278
00279 p = cpl_parameterlist_find(parlist,"vircam.vircam_mkconf.extenum");
00280 vircam_mkconf_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_mkconf_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_mkconf_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_mkconf_tidy();
00302 return(-1);
00303 }
00304
00305
00306
00307 ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
00308
00309
00310
00311
00312
00313 vircam_exten_range(vircam_mkconf_config.extenum,(const cpl_frame *)ps.flat,
00314 &jst,&jfn);
00315 if (jst == -1 || jfn == -1) {
00316 cpl_msg_error(fctid,"Unable to continue");
00317 vircam_mkconf_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.flatf = vircam_fits_load(ps.flat,CPL_TYPE_FLOAT,j);
00330 if (ps.flatf == NULL) {
00331 vircam_mkconf_tidy();
00332 return(-1);
00333 }
00334
00335
00336
00337 nx = cpl_image_get_size_x(vircam_fits_get_image(ps.flatf));
00338 ny = cpl_image_get_size_y(vircam_fits_get_image(ps.flatf));
00339
00340
00341
00342 (void)vircam_mask_load(ps.bpm,j,nx,ny);
00343
00344
00345
00346 cpl_msg_info(fctid,"Making confidence map for extension %d",j);
00347 (void)vircam_mkconf(vircam_fits_get_image(ps.flatf),
00348 vircam_fits_get_fullname(ps.flatf),ps.bpm,
00349 &(ps.outimg),&(ps.drs),&status);
00350 if (status != VIR_OK) {
00351 vircam_mkconf_tidy();
00352 return(-1);
00353 }
00354
00355
00356
00357 cpl_msg_info(fctid,"Saving results for extension %d",j);
00358 if (vircam_mkconf_save(framelist,parlist) != 0) {
00359 vircam_mkconf_tidy();
00360 return(-1);
00361 }
00362
00363
00364
00365 freefits(ps.flatf);
00366 vircam_mask_clear(ps.bpm);
00367 freeimage(ps.outimg);
00368 freepropertylist(ps.drs);
00369 }
00370 vircam_mkconf_tidy();
00371 return(0);
00372 }
00373
00374
00381
00382
00383 static int vircam_mkconf_save(cpl_frameset *framelist,
00384 cpl_parameterlist *parlist) {
00385 const char *recipeid = "vircam_mkconf";
00386 const char *fctid = "vircam_mkconf_save";
00387 const char *outfile = "mkconf.fits";
00388 cpl_propertylist *plist;
00389
00390
00391
00392
00393 if (isfirst) {
00394
00395
00396
00397 product_frame = cpl_frame_new();
00398 cpl_frame_set_filename(product_frame,outfile);
00399 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_TEST);
00400 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00401 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00402 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00403
00404
00405
00406 plist = vircam_fits_get_phu(ps.flatf);
00407 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00408 parlist,(char *)recipeid,
00409 "?Dictionary?");
00410
00411
00412
00413 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00414 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00415 cpl_msg_error(fctid,"Cannot save product PHU");
00416 cpl_frame_delete(product_frame);
00417 return(-1);
00418 }
00419 cpl_frameset_insert(framelist,product_frame);
00420 }
00421
00422
00423
00424 plist = vircam_fits_get_ehu(ps.flatf);
00425
00426
00427
00428 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00429 (char *)recipeid,"?Dictionary?");
00430
00431
00432
00433 if (cpl_image_save(ps.outimg,outfile,CPL_BPP_IEEE_FLOAT,plist,
00434 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00435 cpl_msg_error(fctid,"Cannot save product image extension");
00436 return(-1);
00437 }
00438
00439 return(0);
00440 }
00441
00442
00443
00447
00448
00449 static void vircam_mkconf_init(void) {
00450 ps.labels = NULL;
00451 ps.flat = NULL;
00452 ps.bpm = NULL;
00453 ps.flatf = NULL;
00454 ps.outimg = NULL;
00455 ps.drs = NULL;
00456 }
00457
00458
00459
00463
00464
00465 static void vircam_mkconf_tidy(void) {
00466 freespace(ps.labels);
00467 freeframe(ps.flat);
00468 freemask(ps.bpm);
00469 freefits(ps.flatf);
00470 freeimage(ps.outimg);
00471 freepropertylist(ps.drs);
00472 }
00473
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
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518