39 #include "irplib_utils.h"
41 #include "hawki_utils.h"
42 #include "hawki_load.h"
43 #include "hawki_pfits.h"
44 #include "hawki_dfs.h"
45 #include "hawki_distortion.h"
46 #include "hawki_save.h"
55 int cpl_plugin_get_info(cpl_pluginlist * list);
57 static int hawki_util_gendist_create(cpl_plugin *) ;
58 static int hawki_util_gendist_exec(cpl_plugin *) ;
59 static int hawki_util_gendist_destroy(cpl_plugin *) ;
60 static int hawki_util_gendist(cpl_parameterlist *, cpl_frameset *) ;
61 static cpl_table * hawki_util_gendist_convert(
const char *) ;
62 static hawki_distortion * hawki_util_gendist_convert_to_images
63 (
const cpl_table * dist_tab,
71 static char hawki_util_gendist_description[] =
72 "hawki_util_gendist -- HAWK-I distortion calibration file creation.\n"
73 "The 4 files (chip 1 2 3 4) listed in the Set Of Frames (sof-file) \n"
75 "raw-file_chip1.txt "HAWKI_UTIL_DISTMAP_RAW
"\n"
76 "raw-file_chip2.txt "HAWKI_UTIL_DISTMAP_RAW
"\n"
77 "raw-file_chip3.txt "HAWKI_UTIL_DISTMAP_RAW
"\n"
78 "raw-file_chip4.txt "HAWKI_UTIL_DISTMAP_RAW
"\n" ;
93 int cpl_plugin_get_info(cpl_pluginlist * list)
95 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
96 cpl_plugin * plugin = &recipe->interface ;
98 cpl_plugin_init(plugin,
100 HAWKI_BINARY_VERSION,
101 CPL_PLUGIN_TYPE_RECIPE,
102 "hawki_util_gendist",
103 "Distortion map creation",
104 hawki_util_gendist_description,
108 hawki_util_gendist_create,
109 hawki_util_gendist_exec,
110 hawki_util_gendist_destroy) ;
112 cpl_pluginlist_append(list, plugin) ;
127 static int hawki_util_gendist_create(cpl_plugin * plugin)
129 cpl_recipe * recipe ;
133 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
134 recipe = (cpl_recipe *)plugin ;
138 recipe->parameters = cpl_parameterlist_new() ;
139 if (recipe->parameters == NULL)
144 p = cpl_parameter_new_value(
"hawki.hawki_util_gendist.dim_x",
145 CPL_TYPE_INT,
"Dimension of distortion image in X",
146 "hawki.hawki_util_gendist", HAWKI_DET_NPIX_X);
147 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"dim_x");
148 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
149 cpl_parameterlist_append(recipe->parameters, p);
152 p = cpl_parameter_new_value(
"hawki.hawki_util_gendist.dim_y",
153 CPL_TYPE_INT,
"Dimension of distortion image in Y",
154 "hawki.hawki_util_gendist", HAWKI_DET_NPIX_Y);
155 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"dim_y");
156 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
157 cpl_parameterlist_append(recipe->parameters, p);
171 static int hawki_util_gendist_exec(cpl_plugin * plugin)
173 cpl_recipe * recipe ;
176 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
177 recipe = (cpl_recipe *)plugin ;
183 return hawki_util_gendist(recipe->parameters, recipe->frames) ;
193 static int hawki_util_gendist_destroy(cpl_plugin * plugin)
195 cpl_recipe * recipe ;
198 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
199 recipe = (cpl_recipe *)plugin ;
202 cpl_parameterlist_delete(recipe->parameters) ;
213 static int hawki_util_gendist
214 (cpl_parameterlist * parlist,
215 cpl_frameset * framelist)
217 cpl_frameset * rawframes;
218 const cpl_frame * cur_frame;
219 const char * dist_name;
220 cpl_table ** distortion_table;
221 hawki_distortion ** distortion_im;
222 cpl_propertylist * plist;
223 cpl_propertylist * plist_ext;
225 const char * recipe_name =
"hawki_util_gendist";
234 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
240 HAWKI_UTIL_DISTMAP_RAW)) == NULL) {
241 cpl_msg_error(__func__,
"Cannot find raw frames in the input list") ;
246 if (cpl_frameset_get_size(rawframes) != HAWKI_NB_DETECTORS) {
247 cpl_msg_error(__func__,
"%d frames expected", HAWKI_NB_DETECTORS) ;
248 cpl_frameset_delete(rawframes) ;
253 par = cpl_parameterlist_find(parlist,
"hawki.hawki_util_gendist.dim_x") ;
254 dim_x = cpl_parameter_get_int(par);
255 par = cpl_parameterlist_find(parlist,
"hawki.hawki_util_gendist.dim_y") ;
256 dim_y = cpl_parameter_get_int(par);
259 distortion_table = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_table*)) ;
262 for (ichip=0 ; ichip<HAWKI_NB_DETECTORS ; ichip++) {
265 cur_frame = cpl_frameset_get_frame_const(rawframes, ichip) ;
266 dist_name = cpl_frame_get_filename(cur_frame) ;
269 cpl_msg_info(__func__,
"Create the output table for chip %d", ichip+1) ;
270 if ((distortion_table[ichip] = hawki_util_gendist_convert(dist_name)) == NULL) {
272 cpl_msg_error(__func__,
"Cannot create the output table") ;
273 cpl_frameset_delete(rawframes) ;
274 for (j=0 ; j<ichip ; j++) cpl_table_delete(distortion_table[j]) ;
275 cpl_free(distortion_table) ;
281 plist = cpl_propertylist_new() ;
282 cpl_propertylist_append_string(plist,
"INSTRUME",
"HAWKI") ;
283 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
284 HAWKI_PROTYPE_DISTORTION) ;
285 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
286 HAWKI_CALPRO_DISTORTION) ;
288 plist_ext = cpl_propertylist_new() ;
289 cpl_propertylist_prepend_string(plist_ext,
"EXTNAME",
"CHIP1.INT1") ;
290 if (cpl_dfs_save_table(framelist, NULL, parlist, rawframes, NULL,
291 distortion_table[0], plist_ext, recipe_name,
292 plist, NULL, PACKAGE
"/" PACKAGE_VERSION,
293 "hawki_util_gendist.fits") != CPL_ERROR_NONE) {
294 cpl_msg_error(__func__,
"Cannot save the first extension table") ;
295 cpl_propertylist_delete(plist_ext) ;
296 cpl_propertylist_delete(plist) ;
297 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++)
298 cpl_table_delete(distortion_table[iext]) ;
299 cpl_free(distortion_table) ;
300 cpl_frameset_delete(rawframes) ;
303 cpl_propertylist_delete(plist) ;
304 cpl_propertylist_delete(plist_ext) ;
307 for (iext=1 ; iext<HAWKI_NB_DETECTORS; iext++)
315 plist_ext = cpl_propertylist_new() ;
316 sprintf(sval,
"CHIP%d.INT1", ichip+1) ;
317 cpl_propertylist_prepend_string(plist_ext,
"EXTNAME", sval) ;
318 cpl_table_save(distortion_table[ichip], NULL, plist_ext,
319 "hawki_util_gendist.fits", CPL_IO_EXTEND);
320 cpl_propertylist_delete(plist_ext) ;
324 distortion_im = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(hawki_distortion*));
327 for (ichip=0 ; ichip<HAWKI_NB_DETECTORS ; ichip++)
330 cur_frame = cpl_frameset_get_frame_const(rawframes, ichip) ;
331 dist_name = cpl_frame_get_filename(cur_frame) ;
334 cpl_msg_info(__func__,
"Create the output images for extension %d", ichip+1) ;
335 if ((distortion_im[ichip] = hawki_util_gendist_convert_to_images
336 (distortion_table[ichip], dim_x, dim_y)) == NULL)
339 cpl_msg_error(__func__,
"Cannot create the output distortion images");
340 cpl_frameset_delete(rawframes);
341 for (j=0 ;j < ichip; j++)
343 cpl_free(distortion_im) ;
349 plist = cpl_propertylist_new() ;
350 cpl_propertylist_append_string(plist,
"INSTRUME",
"HAWKI");
352 cpl_propertylist_append_string(plist,
"MJD-OBS",
"55128.5000000");
353 cpl_propertylist_append_string(plist,
"FOR_QC",
"dummy.fits");
354 cpl_propertylist_append_string(plist,
"ORIGFILE",
355 "hawki_util_gendist_distx.fits") ;
357 "hawki_util_gendist",
358 HAWKI_CALPRO_DISTORTION_X,
359 HAWKI_PROTYPE_DISTORTION_X,
360 plist,
"hawki_util_gendist_distx.fits");
361 cpl_propertylist_erase(plist,
"ORIGFILE");
362 cpl_propertylist_append_string(plist,
"ORIGFILE",
363 "hawki_util_gendist_distx.fits") ;
365 "hawki_util_gendist",
366 HAWKI_CALPRO_DISTORTION_Y,
367 HAWKI_PROTYPE_DISTORTION_Y,
368 plist,
"hawki_util_gendist_disty.fits");
369 cpl_propertylist_delete(plist) ;
376 for (iext=0 ; iext<HAWKI_NB_DETECTORS; iext++) {
383 plist_ext = cpl_propertylist_new() ;
384 cpl_propertylist_append_double(plist_ext,
"CRVAL1",
385 distortion_im[ichip]->x_crval);
386 cpl_propertylist_append_double(plist_ext,
"CRVAL2",
387 distortion_im[ichip]->y_crval);
388 cpl_propertylist_append_double(plist_ext,
"CDELT1",
389 distortion_im[ichip]->x_cdelt);
390 cpl_propertylist_append_double(plist_ext,
"CDELT2",
391 distortion_im[ichip]->y_cdelt);
392 cpl_propertylist_append_double(plist_ext,
"CRPIX1", 1);
393 cpl_propertylist_append_double(plist_ext,
"CRPIX2", 1);
394 cpl_propertylist_append_string(plist_ext,
"CTYPE1",
"");
395 cpl_propertylist_append_string(plist_ext,
"CTYPE2",
"");
396 cpl_propertylist_append_string(plist_ext,
"CUNIT1",
"");
397 cpl_propertylist_append_string(plist_ext,
"CUNIT2",
"");
399 plist_ext,
"hawki_util_gendist_distx.fits");
401 plist_ext,
"hawki_util_gendist_disty.fits");
402 cpl_propertylist_delete(plist_ext);
405 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++)
406 cpl_table_delete(distortion_table[iext]);
407 cpl_free(distortion_table) ;
409 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++)
411 cpl_free(distortion_im) ;
412 cpl_frameset_delete(rawframes) ;
416 if (cpl_error_get_code())
418 cpl_msg_error(__func__,
419 "HAWK-I pipeline could not recover from previous errors");
451 static cpl_table * hawki_util_gendist_convert(
const char * filename)
461 if (filename == NULL)
return NULL ;
465 if ((in = fopen(filename,
"r")) == NULL) {
468 while (fgets(line, 1024, in) != NULL) {
469 if (line[0] !=
'#') nbentries ++ ;
474 out = cpl_table_new(nbentries);
475 cpl_table_new_column(out, HAWKI_COL_DIST_DXGC, CPL_TYPE_DOUBLE);
476 cpl_table_set_column_unit(out, HAWKI_COL_DIST_DXGC,
"pixels");
477 cpl_table_new_column(out, HAWKI_COL_DIST_DYGC, CPL_TYPE_DOUBLE);
478 cpl_table_set_column_unit(out, HAWKI_COL_DIST_DYGC,
"pixels");
479 cpl_table_new_column(out, HAWKI_COL_DIST_I, CPL_TYPE_INT);
480 cpl_table_set_column_unit(out, HAWKI_COL_DIST_I,
"pixels");
481 cpl_table_new_column(out, HAWKI_COL_DIST_J, CPL_TYPE_INT);
482 cpl_table_set_column_unit(out, HAWKI_COL_DIST_J,
"pixels");
485 if ((in = fopen(filename,
"r")) == NULL) {
486 cpl_table_delete(out) ;
490 while (fgets(line, 1024, in) != NULL) {
491 if (line[0] !=
'#') {
492 if (sscanf(line,
"%lg %lg %d %d",
493 &dxgc, &dygc, &i, &j) != 4) {
494 cpl_msg_error(__func__,
"Bad line %d", nbentries+1) ;
495 cpl_table_delete(out) ;
498 cpl_table_set_double(out, HAWKI_COL_DIST_DXGC, nbentries, dxgc);
499 cpl_table_set_double(out, HAWKI_COL_DIST_DYGC, nbentries, dygc);
500 cpl_table_set_int(out, HAWKI_COL_DIST_I, nbentries, i);
501 cpl_table_set_int(out, HAWKI_COL_DIST_J, nbentries, j);
529 static hawki_distortion * hawki_util_gendist_convert_to_images
530 (
const cpl_table * dist_tab,
534 hawki_distortion * out ;
545 if (dist_tab == NULL)
return NULL ;
548 nbentries = cpl_table_get_nrow(dist_tab);
549 ngrid = sqrt(nbentries);
550 if(ngrid * ngrid != nbentries)
552 cpl_msg_error(__func__,
"Only square grids are supported");
558 i_ptr = cpl_table_get_data_int_const(dist_tab, HAWKI_COL_DIST_I);
559 i_vec = cpl_array_wrap_int((
int *)i_ptr, nbentries);
560 out->x_crval = cpl_array_get_min(i_vec);
561 out->x_cdelt = (cpl_array_get_max(i_vec) - cpl_array_get_min(i_vec)) /
563 cpl_array_unwrap(i_vec);
564 j_ptr = cpl_table_get_data_int_const(dist_tab, HAWKI_COL_DIST_J);
565 j_vec = cpl_array_wrap_int((
int *)j_ptr, nbentries);
566 out->y_crval = cpl_array_get_min(j_vec);
567 out->y_cdelt = (cpl_array_get_max(j_vec) - cpl_array_get_min(j_vec)) /
569 cpl_array_unwrap(j_vec);
573 for(irow = 0; irow < nbentries; ++irow)
581 i_ima = (cpl_table_get_int(dist_tab, HAWKI_COL_DIST_I, irow, &null) -
582 out->x_crval) / out->x_cdelt;
583 if(floor(i_ima) != i_ima)
585 cpl_msg_error(__func__,
" The distortion tables must be defined "
586 "in a regular grid");
590 j_ima = (cpl_table_get_int(dist_tab, HAWKI_COL_DIST_J, irow, &null) -
591 out->y_crval) / out->y_cdelt;
592 if(floor(j_ima) != j_ima)
594 cpl_msg_error(__func__,
" The distortion tables must be defined "
595 "in a regular grid");
599 dx = cpl_table_get_double(dist_tab, HAWKI_COL_DIST_DXGC, irow, &null);
600 dy = cpl_table_get_double(dist_tab, HAWKI_COL_DIST_DYGC, irow, &null);
602 cpl_image_set(out->dist_x, (
int)i_ima + 1, (
int)j_ima + 1, dx);
603 cpl_image_set(out->dist_y, (
int)i_ima + 1, (
int)j_ima + 1, dy);