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 #include <math.h>
00037
00038 #include "vircam_utils.h"
00039 #include "vircam_mask.h"
00040 #include "vircam_dfs.h"
00041 #include "vircam_mods.h"
00042 #include "vircam_stats.h"
00043 #include "vircam_fits.h"
00044 #include "vircam_wcsutils.h"
00045
00046
00047
00048 static int vircam_interleave_create(cpl_plugin *) ;
00049 static int vircam_interleave_exec(cpl_plugin *) ;
00050 static int vircam_interleave_destroy(cpl_plugin *) ;
00051 static int vircam_interleave_test(cpl_parameterlist *, cpl_frameset *) ;
00052 static int vircam_interleave_save(cpl_frameset *framelist,
00053 cpl_parameterlist *parlist);
00054 static void vircam_interleave_init(void);
00055 static void vircam_interleave_tidy(void);
00056
00057
00058
00059 static struct {
00060
00061
00062
00063 int extenum;
00064
00065 } vircam_interleave_config;
00066
00067
00068 static struct {
00069 int *labels;
00070 cpl_frameset *imagelist;
00071 vir_fits **images;
00072 cpl_frameset *conflist;
00073 vir_fits **confs;
00074 int nimages;
00075 int nconfs;
00076 cpl_image *outimage;
00077 cpl_image *outconf;
00078 cpl_propertylist *plist;
00079 } ps;
00080
00081 static int isfirst;
00082 static cpl_frame *product_frame = NULL;
00083 static cpl_frame *product_conf = NULL;
00084
00085 static char vircam_interleave_description[] =
00086 "vircam_interleave -- VIRCAM test interleave recipe.\n\n"
00087 "Interleave a list of frames into an output frame.\n\n"
00088 "The program accepts the following files in the SOF:\n\n"
00089 " Tag Description\n"
00090 " -----------------------------------------------------------------------\n"
00091 " %-21s A list of images\n"
00092 " %-21s A list of confidence maps\n"
00093 "\n";
00094
00137
00138
00139
00147
00148
00149 int cpl_plugin_get_info(cpl_pluginlist *list) {
00150 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00151 cpl_plugin *plugin = &recipe->interface;
00152 char alldesc[SZ_ALLDESC];
00153 (void)snprintf(alldesc,SZ_ALLDESC,vircam_interleave_description,
00154 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
00155
00156 cpl_plugin_init(plugin,
00157 CPL_PLUGIN_API,
00158 VIRCAM_BINARY_VERSION,
00159 CPL_PLUGIN_TYPE_RECIPE,
00160 "vircam_interleave",
00161 "VIRCAM interleaf test recipe [test]",
00162 alldesc,
00163 "Jim Lewis",
00164 "jrl@ast.cam.ac.uk",
00165 vircam_get_license(),
00166 vircam_interleave_create,
00167 vircam_interleave_exec,
00168 vircam_interleave_destroy);
00169
00170 cpl_pluginlist_append(list,plugin);
00171
00172 return(0);
00173 }
00174
00175
00184
00185
00186 static int vircam_interleave_create(cpl_plugin *plugin) {
00187 cpl_recipe *recipe;
00188 cpl_parameter *p;
00189
00190
00191
00192 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00193 recipe = (cpl_recipe *)plugin;
00194 else
00195 return(-1);
00196
00197
00198
00199 recipe->parameters = cpl_parameterlist_new();
00200
00201
00202
00203 p = cpl_parameter_new_range("vircam.vircam_interleave.extenum",
00204 CPL_TYPE_INT,
00205 "Extension number to be done, 0 == all",
00206 "vircam.vircam_interleave",
00207 1,0,16);
00208 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00209 cpl_parameterlist_append(recipe->parameters,p);
00210
00211
00212
00213 return(0);
00214 }
00215
00216
00217
00223
00224
00225 static int vircam_interleave_exec(cpl_plugin *plugin) {
00226 cpl_recipe *recipe;
00227
00228
00229
00230 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00231 recipe = (cpl_recipe *)plugin;
00232 else
00233 return(-1);
00234
00235 return(vircam_interleave_test(recipe->parameters,recipe->frames));
00236 }
00237
00238
00244
00245
00246 static int vircam_interleave_destroy(cpl_plugin *plugin) {
00247 cpl_recipe *recipe ;
00248
00249
00250
00251 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00252 recipe = (cpl_recipe *)plugin;
00253 else
00254 return(-1);
00255
00256 cpl_parameterlist_delete(recipe->parameters);
00257 return(0);
00258 }
00259
00260
00267
00268
00269 static int vircam_interleave_test(cpl_parameterlist *parlist,
00270 cpl_frameset *framelist) {
00271 const char *fctid="vircam_interleave";
00272 int nlab,j,jst,jfn,retval,status,i,nstep,*dims;
00273 long npts;
00274 float val;
00275 double refx,refy,refra,refdec,x,y;
00276 cpl_parameter *p;
00277 cpl_image *image;
00278 cpl_array *a;
00279 cpl_wcs *wcs;
00280
00281
00282
00283
00284 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00285 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00286 return(-1);
00287 }
00288
00289
00290
00291 vircam_interleave_init();
00292
00293
00294
00295 p = cpl_parameterlist_find(parlist,"vircam.vircam_interleave.extenum");
00296 vircam_interleave_config.extenum = cpl_parameter_get_int(p);
00297
00298
00299
00300 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00301 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00302 vircam_interleave_tidy();
00303 return(-1);
00304 }
00305
00306
00307
00308 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00309 &nlab)) == NULL) {
00310 cpl_msg_error(fctid,"Cannot labelise the input frames");
00311 vircam_interleave_tidy();
00312 return(-1);
00313 }
00314 if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00315 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00316 cpl_msg_error(fctid,"Cannot get images in input frameset");
00317 vircam_interleave_tidy();
00318 return(-1);
00319 }
00320 ps.nimages = cpl_frameset_get_size(ps.imagelist);
00321 nstep = (int)sqrt((float)ps.nimages);
00322 if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00323 VIRCAM_CAL_CONF)) == NULL) {
00324 cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
00325 vircam_interleave_tidy();
00326 return(-1);
00327 }
00328 ps.nconfs = cpl_frameset_get_size(ps.conflist);
00329
00330
00331
00332
00333
00334 vircam_exten_range(vircam_interleave_config.extenum,
00335 (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00336 &jst,&jfn);
00337 if (jst == -1 || jfn == -1) {
00338 cpl_msg_error(fctid,"Unable to continue");
00339 vircam_interleave_tidy();
00340 return(-1);
00341 }
00342
00343
00344
00345 status = VIR_OK;
00346 for (j = jst; j <= jfn; j++) {
00347 isfirst = (j == jst);
00348
00349
00350
00351 ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00352 ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
00353
00354
00355
00356
00357
00358 for (i = 0; i < ps.nimages; i++) {
00359 image = vircam_fits_get_image(ps.images[i]);
00360 npts = vircam_getnpts(image);
00361 val = vircam_med(cpl_image_get_data_float(image),NULL,npts);
00362 cpl_propertylist_update_float(vircam_fits_get_ehu(ps.images[i]),
00363 "ESO DRS BACKMED",val);
00364 wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ps.images[i]));
00365 if (i == 0) {
00366 a = cpl_wcs_get_image_dims(wcs);
00367 dims = cpl_array_get_data_int(a);
00368 refx = 0.5*(double)dims[0];
00369 refy = 0.5*(double)dims[1];
00370 vircam_xytoradec(wcs,refx,refy,&refra,&refdec);
00371 }
00372 vircam_radectoxy(wcs,refra,refdec,&x,&y);
00373 x = refx - x;
00374 y = refy - y;
00375 cpl_propertylist_update_double(vircam_fits_get_ehu(ps.images[i]),
00376 "ESO DRS XOFFMICRO",x);
00377 cpl_propertylist_update_double(vircam_fits_get_ehu(ps.images[i]),
00378 "ESO DRS YOFFMICRO",y);
00379 cpl_wcs_delete(wcs);
00380 }
00381
00382
00383
00384 cpl_msg_info(fctid,"Doing interleaving for extension %d\n",j);
00385 (void)vircam_interleave(ps.images,ps.nimages,ps.confs,ps.nconfs,
00386 nstep,&(ps.plist),&(ps.outimage),&(ps.outconf),
00387 &status);
00388 if (status != VIR_OK) {
00389 vircam_interleave_tidy();
00390 return(-1);
00391 }
00392
00393
00394
00395 cpl_msg_info(fctid,"Saving combined image extension %d\n",j);
00396 retval = vircam_interleave_save(framelist,parlist);
00397 if (retval != 0) {
00398 vircam_interleave_tidy();
00399 return(-1);
00400 }
00401 freefitslist(ps.images,ps.nimages);
00402 freefitslist(ps.confs,ps.nconfs);
00403 freeimage(ps.outimage);
00404 freeimage(ps.outconf);
00405 freepropertylist(ps.plist);
00406 }
00407 vircam_interleave_tidy();
00408 return(0);
00409 }
00410
00411
00412
00420
00421
00422 static int vircam_interleave_save(cpl_frameset *framelist,
00423 cpl_parameterlist *parlist) {
00424 cpl_propertylist *plist;
00425 const char *recipeid = "vircam_interleave";
00426 const char *fctid = "vircam_interleave_save";
00427 const char *outfile = "comb.fits";
00428 const char *outconf = "combconf.fits";
00429
00430
00431
00432
00433 if (isfirst) {
00434
00435
00436
00437 product_frame = cpl_frame_new();
00438 cpl_frame_set_filename(product_frame,outfile);
00439 cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_TEST);
00440 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00441 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00442 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00443
00444
00445
00446 plist = vircam_fits_get_phu(ps.images[0]);
00447 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00448 parlist,(char *)recipeid,
00449 "?Dictionary?");
00450
00451
00452
00453 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00454 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00455 cpl_msg_error(fctid,"Cannot save product PHU");
00456 cpl_frame_delete(product_frame);
00457 return(-1);
00458 }
00459 cpl_frameset_insert(framelist,product_frame);
00460
00461
00462
00463 product_conf = cpl_frame_new();
00464 cpl_frame_set_filename(product_conf,outconf);
00465 cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
00466 cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
00467 cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
00468 cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
00469
00470
00471
00472 if (cpl_image_save(NULL,outconf,CPL_BPP_8_UNSIGNED,plist,
00473 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00474 cpl_msg_error(fctid,"Cannot save product PHU");
00475 cpl_frame_delete(product_conf);
00476 return(-1);
00477 }
00478 cpl_frameset_insert(framelist,product_conf);
00479 }
00480
00481
00482
00483 plist = ps.plist;
00484
00485
00486
00487 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00488 parlist,(char *)recipeid,
00489 "?Dictionary?");
00490
00491
00492
00493 if (cpl_image_save(ps.outimage,outfile,CPL_BPP_IEEE_FLOAT,plist,
00494 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00495 cpl_msg_error(fctid,"Cannot save interleaved image extension");
00496 return(-1);
00497 }
00498
00499
00500
00501 if (cpl_image_save(ps.outconf,outconf,CPL_BPP_16_SIGNED,plist,
00502 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00503 cpl_msg_error(fctid,"Cannot save confidence map image extension");
00504 return(-1);
00505 }
00506
00507
00508
00509 return(0);
00510 }
00511
00512
00516
00517
00518 static void vircam_interleave_init(void) {
00519 ps.labels = NULL;
00520 ps.imagelist = NULL;
00521 ps.images = NULL;
00522 ps.conflist = NULL;
00523 ps.confs = NULL;
00524 ps.outimage = NULL;
00525 ps.outconf = NULL;
00526 ps.plist = NULL;
00527 }
00528
00529
00533
00534
00535 static void vircam_interleave_tidy(void) {
00536 freespace(ps.labels);
00537 freeframeset(ps.imagelist);
00538 freefitslist(ps.images,ps.nimages);
00539 freeframeset(ps.conflist);
00540 freefitslist(ps.confs,ps.nconfs);
00541 freeimage(ps.outimage);
00542 freeimage(ps.outconf);
00543 freepropertylist(ps.plist);
00544 }
00545
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605