39 #include "vircam_utils.h"
40 #include "vircam_mask.h"
41 #include "vircam_pfits.h"
42 #include "vircam_dfs.h"
43 #include "vircam_mods.h"
44 #include "vircam_fits.h"
45 #include "vircam_tfits.h"
46 #include "vircam_jmp_utils.h"
50 static int vircam_mesostep_analyse_create(cpl_plugin *);
51 static int vircam_mesostep_analyse_exec(cpl_plugin *);
52 static int vircam_mesostep_analyse_destroy(cpl_plugin *);
53 static int vircam_mesostep_analyse(cpl_parameterlist *, cpl_frameset *);
54 static cpl_propertylist *vircam_mesostep_analyse_dummyqc(
int type);
57 static char vircam_mesostep_analyse_description[] =
58 "vircam_mesostep_analyse -- VIRCAM mesostep processing recipe.\n\n"
59 "Process a complete mesostep sequence of vircam data. Remove instrumental\n"
60 "signature and sky subtract if desired. Work out the illumination correction\n"
61 "for all of the input frames and then smooth the result by fitting a 2d\n"
62 "polynomial. Evaluate the polynomial at the grid points to form the final\n"
63 "illumination correction data product\n"
64 "The program accepts the following files in the SOF:\n\n"
66 " -----------------------------------------------------------------------\n"
67 " %-21s A list of raw science images\n"
68 " %-21s A master dark frame\n"
69 " %-21s A master twilight flat frame\n"
70 " %-21s A channel table\n"
71 " %-21s A photometric calibration table\n"
72 " %-21s A master confidence map or\n"
73 " %-21s A master bad pixel mask\n"
74 " %-21s A master standard star index\n"
75 "All of the above are required\n"
168 int cpl_plugin_get_info(cpl_pluginlist *list) {
169 cpl_recipe *recipe = cpl_calloc(1,
sizeof(*recipe));
170 cpl_plugin *plugin = &recipe->interface;
171 char alldesc[SZ_ALLDESC];
172 (void)snprintf(alldesc,SZ_ALLDESC,vircam_mesostep_analyse_description,
173 VIRCAM_ILLUM_RAW,VIRCAM_CAL_DARK,VIRCAM_CAL_TWILIGHT_FLAT,
174 VIRCAM_CAL_CHANTAB,VIRCAM_CAL_PHOTTAB,VIRCAM_CAL_CONF,
175 VIRCAM_CAL_BPM,VIRCAM_CAL_2MASS);
177 cpl_plugin_init(plugin,
179 VIRCAM_BINARY_VERSION,
180 CPL_PLUGIN_TYPE_RECIPE,
181 "vircam_mesostep_analyse",
182 "VIRCAM mesostep analysis recipe",
187 vircam_mesostep_analyse_create,
188 vircam_mesostep_analyse_exec,
189 vircam_mesostep_analyse_destroy);
191 cpl_pluginlist_append(list,plugin);
207 static int vircam_mesostep_analyse_create(cpl_plugin *plugin) {
213 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
214 recipe = (cpl_recipe *)plugin;
220 recipe->parameters = cpl_parameterlist_new();
224 p = cpl_parameter_new_value(
"vircam.vircam_mesostep_analyse.ipix",
226 "Minimum pixel area for each detected object",
227 "vircam.vircam_mesostep_analyse",5);
228 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"ipix");
229 cpl_parameterlist_append(recipe->parameters,p);
233 p = cpl_parameter_new_value(
"vircam.vircam_mesostep_analyse.thresh",
235 "Detection threshold in sigma above sky",
236 "vircam.vircam_mesostep_analyse",2.0);
237 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"thr");
238 cpl_parameterlist_append(recipe->parameters,p);
242 p = cpl_parameter_new_value(
"vircam.vircam_mesostep_analyse.icrowd",
243 CPL_TYPE_BOOL,
"Use deblending?",
244 "vircam.vircam_mesostep_analyse",0);
245 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"icrowd");
246 cpl_parameterlist_append(recipe->parameters,p);
250 p = cpl_parameter_new_value(
"vircam.vircam_mesostep_analyse.rcore",
251 CPL_TYPE_DOUBLE,
"Value of Rcore in pixels",
252 "vircam.vircam_mesostep_analyse",3.0);
253 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"rcore");
254 cpl_parameterlist_append(recipe->parameters,p);
258 p = cpl_parameter_new_value(
"vircam.vircam_mesostep_analyse.nbsize",
259 CPL_TYPE_INT,
"Background smoothing box size",
260 "vircam.vircam_mesostep_analyse",64);
261 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"nb");
262 cpl_parameterlist_append(recipe->parameters,p);
266 p = cpl_parameter_new_value(
"vircam.vircam_mesostep_analyse.destripe",
267 CPL_TYPE_BOOL,
"Destripe images?",
268 "vircam.vircam_mesostep_analyse",1);
269 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"destripe");
270 cpl_parameterlist_append(recipe->parameters,p);
274 p = cpl_parameter_new_value(
"vircam.vircam_mesostep_analyse.skycor",
275 CPL_TYPE_BOOL,
"Sky correct images?",
276 "vircam.vircam_mesostep_analyse",1);
277 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"skycor");
278 cpl_parameterlist_append(recipe->parameters,p);
282 p = cpl_parameter_new_value(
"vircam.vircam_mesostep_analyse.nord",
284 "Polynomial order for surface fit",
285 "vircam.vircam_mesostep_analyse",3);
286 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"nord");
287 cpl_parameterlist_append(recipe->parameters,p);
291 p = cpl_parameter_new_range(
"vircam.vircam_mesostep_analyse.extenum",
293 "Extension number to be done, 0 == all",
294 "vircam.vircam_mesostep_analyse",
296 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"ext");
297 cpl_parameterlist_append(recipe->parameters,p);
312 static int vircam_mesostep_analyse_exec(cpl_plugin *plugin) {
317 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
318 recipe = (cpl_recipe *)plugin;
322 return(vircam_mesostep_analyse(recipe->parameters,recipe->frames));
333 static int vircam_mesostep_analyse_destroy(cpl_plugin *plugin) {
338 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
339 recipe = (cpl_recipe *)plugin;
343 cpl_parameterlist_delete(recipe->parameters);
356 static int vircam_mesostep_analyse(cpl_parameterlist *parlist,
357 cpl_frameset *framelist) {
358 const char *fctid=
"vircam_mesostep_analyse";
360 cpl_polynomial *poly;
361 int jst,jfn,status,isconf,j,i,live,nrows,n,ndit;
363 float *xx1,*xx2,*yy1,*yy2,*ill,gaincor_fac;
364 double *bv_x,*bv_y,*vdata,val;
370 cpl_propertylist *pp;
374 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
375 cpl_msg_error(fctid,
"Input framelist NULL or has no input data");
382 cpl_msg_error(fctid,
"Input frameset is missing files. Check SOF");
389 (void)strncpy(vircam_recipename,fctid,VIRCAM_PATHSZ);
390 (void)snprintf(vircam_recipepaf,VIRCAM_PATHSZ,
"VIRCAM/%s",fctid);
395 p = cpl_parameterlist_find(parlist,
396 "vircam.vircam_mesostep_analyse.ipix");
397 vircam_jmp_config.ipix = cpl_parameter_get_int(p);
398 p = cpl_parameterlist_find(parlist,
399 "vircam.vircam_mesostep_analyse.thresh");
400 vircam_jmp_config.threshold = (float)cpl_parameter_get_double(p);
401 p = cpl_parameterlist_find(parlist,
402 "vircam.vircam_mesostep_analyse.icrowd");
403 vircam_jmp_config.icrowd = cpl_parameter_get_bool(p);
404 p = cpl_parameterlist_find(parlist,
405 "vircam.vircam_mesostep_analyse.rcore");
406 vircam_jmp_config.rcore = (float)cpl_parameter_get_double(p);
407 p = cpl_parameterlist_find(parlist,
408 "vircam.vircam_mesostep_analyse.nbsize");
409 vircam_jmp_config.nbsize = cpl_parameter_get_int(p);
410 p = cpl_parameterlist_find(parlist,
411 "vircam.vircam_mesostep_analyse.destripe");
412 vircam_jmp_config.destripe = cpl_parameter_get_bool(p);
413 p = cpl_parameterlist_find(parlist,
414 "vircam.vircam_mesostep_analyse.skycor");
415 vircam_jmp_config.skycor = cpl_parameter_get_bool(p);
416 p = cpl_parameterlist_find(parlist,
417 "vircam.vircam_mesostep_analyse.nord");
418 vircam_jmp_config.nord = cpl_parameter_get_int(p);
419 p = cpl_parameterlist_find(parlist,
420 "vircam.vircam_mesostep_analyse.extenum");
421 vircam_jmp_config.extenum = cpl_parameter_get_int(p);
426 cpl_msg_error(fctid,
"Cannot identify RAW and CALIB frames");
435 cpl_msg_error(fctid,
"Cannot labelise the input frames");
442 if ((ps.science_frames =
444 VIRCAM_ILLUM_RAW)) == NULL) {
445 cpl_msg_error(fctid,
"No science images to process!");
452 if ((ps.master_dark =
454 VIRCAM_CAL_DARK)) == NULL) {
455 cpl_msg_error(fctid,
"No master dark found");
462 if ((ps.master_twilight_flat =
464 VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
465 cpl_msg_error(fctid,
"No master twilight flat found");
474 &status) != VIR_OK) {
475 cpl_msg_error(fctid,
"Error calculating gain corrections");
485 if ((ps.master_conf =
487 VIRCAM_CAL_CONF)) == NULL) {
489 if ((ps.master_conf =
491 VIRCAM_CAL_BPM)) == NULL) {
492 cpl_msg_error(fctid,
"No master confidence map found");
502 VIRCAM_CAL_CHANTAB)) == NULL) {
503 cpl_msg_error(fctid,
"No channel table found");
511 VIRCAM_CAL_PHOTTAB)) == NULL) {
512 cpl_msg_error(fctid,
"No photometric table found");
516 if ((ps.tphottab = cpl_table_load(cpl_frame_get_filename(ps.phottab),1,0)) == NULL) {
517 cpl_msg_error(fctid,
"Unable to load photometric table");
525 VIRCAM_CAL_2MASS)) == NULL) {
526 cpl_msg_info(fctid,
"No 2MASS index found -- cannot continue");
533 if (
vircam_catpars(catindex,&(ps.catpath),&(ps.catname)) == VIR_FATAL) {
535 cpl_frame_delete(catindex);
538 cpl_frame_delete(catindex);
542 pp = cpl_propertylist_load(cpl_frame_get_filename(cpl_frameset_get_frame(ps.science_frames,0)),0);
544 cpl_msg_error(fctid,
"No value for NDIT available");
545 freepropertylist(pp);
549 cpl_propertylist_delete(pp);
556 (
const cpl_frame *)cpl_frameset_get_frame(ps.science_frames,0),
558 if (jst == -1 || jfn == -1) {
559 cpl_msg_error(fctid,
"Unable to continue");
567 for (j = jst; j <= jfn; j++) {
568 isfirst = (j == jst);
569 gaincor_fac = (ps.gaincors)[j-1];
575 if (ps.fdark == NULL) {
577 "Error loading master dark %s[%" CPL_SIZE_FORMAT
"]\n%s",
578 cpl_frame_get_filename(ps.master_dark),(cpl_size)j,
579 cpl_error_get_message());
584 if (ps.fflat == NULL) {
586 "Error loading master flat %s[%" CPL_SIZE_FORMAT
"]\n%s",
587 cpl_frame_get_filename(ps.master_twilight_flat),
588 (cpl_size)j,cpl_error_get_message());
593 if (ps.fconf == NULL) {
595 "Error loading master conf %s[%" CPL_SIZE_FORMAT
"]\n%s",
596 cpl_frame_get_filename(ps.master_conf),(cpl_size)j,
597 cpl_error_get_message());
607 "Error loading mask from master conf %s[%" CPL_SIZE_FORMAT
"]\n%s",
608 cpl_frame_get_filename(ps.master_conf),(cpl_size)j,
609 cpl_error_get_message());
614 if (ps.fchantab == NULL) {
615 cpl_msg_error(fctid,
"Error loading channel table %s[%" CPL_SIZE_FORMAT
"]\n%s",
616 cpl_frame_get_filename(ps.chantab),(cpl_size)j,
617 cpl_error_get_message());
624 ps.nscience = cpl_frameset_get_size(ps.science_frames);
626 if (ps.sci_fits == NULL) {
628 "Error loading science frames extension %" CPL_SIZE_FORMAT
": %s",
629 (cpl_size)j,cpl_error_get_message());
637 for (i = 0; i < ps.nscience; i++) {
646 cpl_msg_info(fctid,
"Doing stage1 corrections on %s",
648 for (i = 0; i < ps.nscience; i++) {
651 cpl_msg_info(fctid,
"Detector is flagged dead in %s",
660 if (vircam_jmp_config.destripe)
667 if (vircam_jmp_config.skycor) {
668 cpl_msg_info(fctid,
"Doing sky correction");
674 cpl_msg_info(fctid,
"Doing illumination correction");
675 (void)strcpy(current_cat,ps.catname);
676 (void)strcpy(current_catpath,ps.catpath);
681 if (ps.illcor != NULL) {
683 nrows = (int)cpl_table_get_nrow(ic);
684 xx1 = cpl_table_get_data_float(ic,
"xmin");
685 xx2 = cpl_table_get_data_float(ic,
"xmax");
686 yy1 = cpl_table_get_data_float(ic,
"ymin");
687 yy2 = cpl_table_get_data_float(ic,
"ymax");
688 ill = cpl_table_get_data_float(ic,
"illcor");
690 for (i = 0; i < nrows; i++)
698 cpl_msg_warning(fctid,
"Illum correction table is all NULLs");
705 bv = cpl_bivector_new((cpl_size)n);
706 bv_x = cpl_bivector_get_x_data(bv);
707 bv_y = cpl_bivector_get_y_data(bv);
708 v = cpl_vector_new((cpl_size)n);
709 vdata = cpl_vector_get_data(v);
711 for (i = 0; i < nrows; i++) {
714 bv_x[n] = 0.5*(double)(xx2[i] + xx1[i]);
715 bv_y[n] = 0.5*(double)(yy2[i] + yy1[i]);
716 vdata[n++] = (double)ill[i];
721 poly = cpl_polynomial_fit_2d_create(bv,v,
722 (cpl_size)(vircam_jmp_config.nord),
724 cpl_vector_delete(v);
725 if (cpl_error_get_code() != 0) {
733 v = cpl_vector_new(2);
734 vdata = cpl_vector_get_data(v);
735 for (i = 0; i < nrows; i++) {
736 vdata[0] = 0.5*(double)(xx2[i] + xx1[i]);
737 vdata[1] = 0.5*(double)(yy2[i] + yy1[i]);
738 val = cpl_polynomial_eval(poly,v);
741 cpl_vector_delete(v);
743 cpl_bivector_delete(bv);
744 cpl_polynomial_delete(poly);
750 cpl_msg_info(fctid,
"Saving illumination correction table");
751 dummyqc = vircam_mesostep_analyse_dummyqc(3);
753 freepropertylist(dummyqc);
766 static cpl_propertylist *vircam_mesostep_analyse_dummyqc(
int type) {
771 p = cpl_propertylist_new();
780 cpl_propertylist_update_float(p,
"ESO QC ILLUMCOR_RMS",0.0);
781 cpl_propertylist_set_comment(p,
"ESO QC ILLUMCOR_RMS",
782 "RMS of illumination correction map");