HAWKI Pipeline Reference Manual 1.8.6
|
00001 /* $Id: hawki_util_gendist.c,v 1.22 2011/10/24 10:40:50 cgarcia Exp $ 00002 * 00003 * This file is part of the HAWKI Pipeline 00004 * Copyright (C) 2002,2003 European Southern Observatory 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 /* 00022 * $Author: cgarcia $ 00023 * $Date: 2011/10/24 10:40:50 $ 00024 * $Revision: 1.22 $ 00025 * $Name: hawki-1_8_6 $ 00026 */ 00027 00028 #ifdef HAVE_CONFIG_H 00029 #include <config.h> 00030 #endif 00031 00032 /*----------------------------------------------------------------------------- 00033 Includes 00034 -----------------------------------------------------------------------------*/ 00035 00036 #include <math.h> 00037 #include <cpl.h> 00038 00039 #include "irplib_utils.h" 00040 00041 #include "hawki_utils.h" 00042 #include "hawki_load.h" 00043 #include "hawki_pfits.h" 00044 #include "hawki_dfs.h" 00045 #include "hawki_distortion.h" 00046 #include "hawki_save.h" 00047 00048 /*----------------------------------------------------------------------------- 00049 Functions prototypes 00050 -----------------------------------------------------------------------------*/ 00051 00052 static int hawki_util_gendist_create(cpl_plugin *) ; 00053 static int hawki_util_gendist_exec(cpl_plugin *) ; 00054 static int hawki_util_gendist_destroy(cpl_plugin *) ; 00055 static int hawki_util_gendist(cpl_parameterlist *, cpl_frameset *) ; 00056 static cpl_table * hawki_util_gendist_convert(const char *) ; 00057 static hawki_distortion * hawki_util_gendist_convert_to_images 00058 (const cpl_table * dist_tab, 00059 int detector_nx, 00060 int detector_ny); 00061 00062 /*----------------------------------------------------------------------------- 00063 Static variables 00064 -----------------------------------------------------------------------------*/ 00065 00066 static char hawki_util_gendist_description[] = 00067 "hawki_util_gendist -- HAWK-I distortion calibration file creation.\n" 00068 "The 4 files (chip 1 2 3 4) listed in the Set Of Frames (sof-file) \n" 00069 "must be tagged:\n" 00070 "raw-file_chip1.txt "HAWKI_UTIL_DISTMAP_RAW"\n" 00071 "raw-file_chip2.txt "HAWKI_UTIL_DISTMAP_RAW"\n" 00072 "raw-file_chip3.txt "HAWKI_UTIL_DISTMAP_RAW"\n" 00073 "raw-file_chip4.txt "HAWKI_UTIL_DISTMAP_RAW"\n" ; 00074 00075 /*----------------------------------------------------------------------------- 00076 Functions code 00077 -----------------------------------------------------------------------------*/ 00078 00079 /*----------------------------------------------------------------------------*/ 00087 /*----------------------------------------------------------------------------*/ 00088 int cpl_plugin_get_info(cpl_pluginlist * list) 00089 { 00090 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ; 00091 cpl_plugin * plugin = &recipe->interface ; 00092 00093 cpl_plugin_init(plugin, 00094 CPL_PLUGIN_API, 00095 HAWKI_BINARY_VERSION, 00096 CPL_PLUGIN_TYPE_RECIPE, 00097 "hawki_util_gendist", 00098 "Distortion map creation", 00099 hawki_util_gendist_description, 00100 "Yves Jung", 00101 PACKAGE_BUGREPORT, 00102 hawki_get_license(), 00103 hawki_util_gendist_create, 00104 hawki_util_gendist_exec, 00105 hawki_util_gendist_destroy) ; 00106 00107 cpl_pluginlist_append(list, plugin) ; 00108 00109 return 0; 00110 } 00111 00112 /*----------------------------------------------------------------------------*/ 00121 /*----------------------------------------------------------------------------*/ 00122 static int hawki_util_gendist_create(cpl_plugin * plugin) 00123 { 00124 cpl_recipe * recipe ; 00125 cpl_parameter * p ; 00126 00127 /* Get the recipe out of the plugin */ 00128 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00129 recipe = (cpl_recipe *)plugin ; 00130 else return -1 ; 00131 00132 /* Create the parameters list in the cpl_recipe object */ 00133 recipe->parameters = cpl_parameterlist_new() ; 00134 if (recipe->parameters == NULL) 00135 return 1; 00136 00137 /* Fill the parameters list */ 00138 /* --dim_x */ 00139 p = cpl_parameter_new_value("hawki.hawki_util_gendist.dim_x", 00140 CPL_TYPE_INT, "Dimension of distortion image in X", 00141 "hawki.hawki_util_gendist", HAWKI_DET_NPIX_X); 00142 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dim_x"); 00143 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00144 cpl_parameterlist_append(recipe->parameters, p); 00145 00146 /* --dim_y */ 00147 p = cpl_parameter_new_value("hawki.hawki_util_gendist.dim_y", 00148 CPL_TYPE_INT, "Dimension of distortion image in Y", 00149 "hawki.hawki_util_gendist", HAWKI_DET_NPIX_Y); 00150 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dim_y"); 00151 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00152 cpl_parameterlist_append(recipe->parameters, p); 00153 00154 00155 /* Return */ 00156 return 0; 00157 } 00158 00159 /*----------------------------------------------------------------------------*/ 00165 /*----------------------------------------------------------------------------*/ 00166 static int hawki_util_gendist_exec(cpl_plugin * plugin) 00167 { 00168 cpl_recipe * recipe ; 00169 00170 /* Get the recipe out of the plugin */ 00171 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00172 recipe = (cpl_recipe *)plugin ; 00173 else return -1 ; 00174 00175 /* Issue a banner */ 00176 hawki_print_banner(); 00177 00178 return hawki_util_gendist(recipe->parameters, recipe->frames) ; 00179 } 00180 00181 /*----------------------------------------------------------------------------*/ 00187 /*----------------------------------------------------------------------------*/ 00188 static int hawki_util_gendist_destroy(cpl_plugin * plugin) 00189 { 00190 cpl_recipe * recipe ; 00191 00192 /* Get the recipe out of the plugin */ 00193 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00194 recipe = (cpl_recipe *)plugin ; 00195 else return -1 ; 00196 00197 cpl_parameterlist_delete(recipe->parameters) ; 00198 return 0 ; 00199 } 00200 00201 /*----------------------------------------------------------------------------*/ 00207 /*----------------------------------------------------------------------------*/ 00208 static int hawki_util_gendist 00209 (cpl_parameterlist * parlist, 00210 cpl_frameset * framelist) 00211 { 00212 cpl_frameset * rawframes; 00213 const cpl_frame * cur_frame; 00214 const char * dist_name; 00215 cpl_table ** distortion_table; 00216 hawki_distortion ** distortion_im; 00217 cpl_propertylist * plist; 00218 cpl_propertylist * plist_ext; 00219 char sval[64]; 00220 const char * recipe_name = "hawki_util_gendist"; 00221 int iext; 00222 int ichip; 00223 cpl_parameter * par; 00224 int dim_x; 00225 int dim_y; 00226 00227 /* Identify the RAW and CALIB frames in the input frameset */ 00228 if (hawki_dfs_set_groups(framelist)) { 00229 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ; 00230 return -1 ; 00231 } 00232 00233 /* Retrieve raw frames */ 00234 if ((rawframes = hawki_extract_frameset(framelist, 00235 HAWKI_UTIL_DISTMAP_RAW)) == NULL) { 00236 cpl_msg_error(__func__, "Cannot find raw frames in the input list") ; 00237 return -1 ; 00238 } 00239 00240 /* There should be HAWKI_NB_DETECTORS input frames ordered */ 00241 if (cpl_frameset_get_size(rawframes) != HAWKI_NB_DETECTORS) { 00242 cpl_msg_error(__func__, "%d frames expected", HAWKI_NB_DETECTORS) ; 00243 cpl_frameset_delete(rawframes) ; 00244 return -1 ; 00245 } 00246 00247 /* Retrieve parameters */ 00248 par = cpl_parameterlist_find(parlist, "hawki.hawki_util_gendist.dim_x") ; 00249 dim_x = cpl_parameter_get_int(par); 00250 par = cpl_parameterlist_find(parlist, "hawki.hawki_util_gendist.dim_y") ; 00251 dim_y = cpl_parameter_get_int(par); 00252 00253 /* Allocate holder for the tables */ 00254 distortion_table = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table*)) ; 00255 00256 /* Loop on the chips */ 00257 for (ichip=0 ; ichip<HAWKI_NB_DETECTORS ; ichip++) { 00258 00259 /* Get the file name */ 00260 cur_frame = cpl_frameset_get_frame_const(rawframes, ichip) ; 00261 dist_name = cpl_frame_get_filename(cur_frame) ; 00262 00263 /* Create the output table */ 00264 cpl_msg_info(__func__, "Create the output table for chip %d", ichip+1) ; 00265 if ((distortion_table[ichip] = hawki_util_gendist_convert(dist_name)) == NULL) { 00266 int j; 00267 cpl_msg_error(__func__, "Cannot create the output table") ; 00268 cpl_frameset_delete(rawframes) ; 00269 for (j=0 ; j<ichip ; j++) cpl_table_delete(distortion_table[j]) ; 00270 cpl_free(distortion_table) ; 00271 return -1 ; 00272 } 00273 } 00274 00275 /* Write the table */ 00276 plist = cpl_propertylist_new() ; 00277 cpl_propertylist_append_string(plist, "INSTRUME", "HAWKI") ; 00278 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, 00279 HAWKI_PROTYPE_DISTORTION) ; 00280 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, 00281 HAWKI_CALPRO_DISTORTION) ; 00282 00283 plist_ext = cpl_propertylist_new() ; 00284 cpl_propertylist_prepend_string(plist_ext, "EXTNAME", "CHIP1.INT1") ; 00285 if (cpl_dfs_save_table(framelist, NULL, parlist, rawframes, NULL, 00286 distortion_table[0], plist_ext, recipe_name, 00287 plist, NULL, PACKAGE "/" PACKAGE_VERSION, 00288 "hawki_util_gendist.fits") != CPL_ERROR_NONE) { 00289 cpl_msg_error(__func__, "Cannot save the first extension table") ; 00290 cpl_propertylist_delete(plist_ext) ; 00291 cpl_propertylist_delete(plist) ; 00292 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) 00293 cpl_table_delete(distortion_table[iext]) ; 00294 cpl_free(distortion_table) ; 00295 cpl_frameset_delete(rawframes) ; 00296 return -1 ; 00297 } 00298 cpl_propertylist_delete(plist) ; 00299 cpl_propertylist_delete(plist_ext) ; 00300 00301 /* Save the extensions */ 00302 for (iext=1 ; iext<HAWKI_NB_DETECTORS; iext++) 00303 { 00304 ichip = iext; 00305 //This is the actual layout of the chips in raw HAWK-I images. 00306 if(iext == 2) 00307 ichip = 3; 00308 if(iext == 3) 00309 ichip = 2; 00310 plist_ext = cpl_propertylist_new() ; 00311 sprintf(sval, "CHIP%d.INT1", ichip+1) ; 00312 cpl_propertylist_prepend_string(plist_ext, "EXTNAME", sval) ; 00313 cpl_table_save(distortion_table[ichip], NULL, plist_ext, 00314 "hawki_util_gendist.fits", CPL_IO_EXTEND); 00315 cpl_propertylist_delete(plist_ext) ; 00316 } 00317 00318 /* Allocate holder for the distortion images */ 00319 distortion_im = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(hawki_distortion*)); 00320 00321 /* Loop on the chips */ 00322 for (ichip=0 ; ichip<HAWKI_NB_DETECTORS ; ichip++) 00323 { 00324 /* Get the file name */ 00325 cur_frame = cpl_frameset_get_frame_const(rawframes, ichip) ; 00326 dist_name = cpl_frame_get_filename(cur_frame) ; 00327 00328 /* Create the output image */ 00329 cpl_msg_info(__func__, "Create the output images for extension %d", ichip+1) ; 00330 if ((distortion_im[ichip] = hawki_util_gendist_convert_to_images 00331 (distortion_table[ichip], dim_x, dim_y)) == NULL) 00332 { 00333 int j; 00334 cpl_msg_error(__func__,"Cannot create the output distortion images"); 00335 cpl_frameset_delete(rawframes); 00336 for (j=0 ;j < ichip; j++) 00337 hawki_distortion_delete(distortion_im[j]); 00338 cpl_free(distortion_im) ; 00339 return -1 ; 00340 } 00341 } 00342 00343 /* Write the distortion images */ 00344 plist = cpl_propertylist_new() ; 00345 cpl_propertylist_append_string(plist, "INSTRUME", "HAWKI"); 00346 //This two keywords are needed by QC. I do not why.. 00347 cpl_propertylist_append_string(plist, "MJD-OBS", "55128.5000000"); 00348 cpl_propertylist_append_string(plist, "FOR_QC", "dummy.fits"); 00349 cpl_propertylist_append_string(plist, "ORIGFILE", 00350 "hawki_util_gendist_distx.fits") ; 00351 hawki_main_header_save(framelist, parlist, rawframes, 00352 "hawki_util_gendist", 00353 HAWKI_CALPRO_DISTORTION_X, 00354 HAWKI_PROTYPE_DISTORTION_X, 00355 plist, "hawki_util_gendist_distx.fits"); 00356 cpl_propertylist_erase(plist, "ORIGFILE"); 00357 cpl_propertylist_append_string(plist, "ORIGFILE", 00358 "hawki_util_gendist_distx.fits") ; 00359 hawki_main_header_save(framelist, parlist, rawframes, 00360 "hawki_util_gendist", 00361 HAWKI_CALPRO_DISTORTION_Y, 00362 HAWKI_PROTYPE_DISTORTION_Y, 00363 plist, "hawki_util_gendist_disty.fits"); 00364 cpl_propertylist_delete(plist) ; 00365 00366 /* Save the extensions */ 00367 //There is kind of a hack here 00368 //We use the distortion table as a reference to save the distortion images 00369 //to have a proper layout of the detectors in the FITS file. 00370 //For that hawki_get_extref_file has been modified. 00371 for (iext=0 ; iext<HAWKI_NB_DETECTORS; iext++) { 00372 ichip = iext; 00373 //This is the actual layout of the chips in raw HAWK-I images. 00374 if(iext == 2) 00375 ichip = 3; 00376 if(iext == 3) 00377 ichip = 2; 00378 plist_ext = cpl_propertylist_new() ; 00379 cpl_propertylist_append_double(plist_ext, "CRVAL1", 00380 distortion_im[ichip]->x_crval); 00381 cpl_propertylist_append_double(plist_ext, "CRVAL2", 00382 distortion_im[ichip]->y_crval); 00383 cpl_propertylist_append_double(plist_ext, "CDELT1", 00384 distortion_im[ichip]->x_cdelt); 00385 cpl_propertylist_append_double(plist_ext, "CDELT2", 00386 distortion_im[ichip]->y_cdelt); 00387 cpl_propertylist_append_double(plist_ext, "CRPIX1", 1); 00388 cpl_propertylist_append_double(plist_ext, "CRPIX2", 1); 00389 cpl_propertylist_append_string(plist_ext, "CTYPE1", ""); 00390 cpl_propertylist_append_string(plist_ext, "CTYPE2", ""); 00391 cpl_propertylist_append_string(plist_ext, "CUNIT1", ""); 00392 cpl_propertylist_append_string(plist_ext, "CUNIT2", ""); 00393 hawki_image_ext_save(framelist, distortion_im[ichip]->dist_x, iext + 1, 00394 plist_ext, "hawki_util_gendist_distx.fits"); 00395 hawki_image_ext_save(framelist, distortion_im[ichip]->dist_y, iext + 1, 00396 plist_ext, "hawki_util_gendist_disty.fits"); 00397 cpl_propertylist_delete(plist_ext); 00398 } 00399 00400 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) 00401 cpl_table_delete(distortion_table[iext]); 00402 cpl_free(distortion_table) ; 00403 00404 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) 00405 hawki_distortion_delete(distortion_im[iext]); 00406 cpl_free(distortion_im) ; 00407 cpl_frameset_delete(rawframes) ; 00408 00409 00410 /* Return */ 00411 if (cpl_error_get_code()) 00412 { 00413 cpl_msg_error(__func__, 00414 "HAWK-I pipeline could not recover from previous errors"); 00415 return -1 ; 00416 } 00417 else return 0 ; 00418 } 00419 00420 /*----------------------------------------------------------------------------*/ 00445 /*----------------------------------------------------------------------------*/ 00446 static cpl_table * hawki_util_gendist_convert(const char * filename) 00447 { 00448 cpl_table * out ; 00449 int nbentries ; 00450 FILE * in ; 00451 double dxgc, dygc ; 00452 int i, j ; 00453 char line[1024]; 00454 00455 /* Check entries */ 00456 if (filename == NULL) return NULL ; 00457 00458 /* Get the number of lines */ 00459 nbentries = 0 ; 00460 if ((in = fopen(filename, "r")) == NULL) { 00461 return NULL ; 00462 } 00463 while (fgets(line, 1024, in) != NULL) { 00464 if (line[0] != '#') nbentries ++ ; 00465 } 00466 fclose(in) ; 00467 00468 /* Create the table */ 00469 out = cpl_table_new(nbentries); 00470 cpl_table_new_column(out, HAWKI_COL_DIST_DXGC, CPL_TYPE_DOUBLE); 00471 cpl_table_set_column_unit(out, HAWKI_COL_DIST_DXGC, "pixels"); 00472 cpl_table_new_column(out, HAWKI_COL_DIST_DYGC, CPL_TYPE_DOUBLE); 00473 cpl_table_set_column_unit(out, HAWKI_COL_DIST_DYGC, "pixels"); 00474 cpl_table_new_column(out, HAWKI_COL_DIST_I, CPL_TYPE_INT); 00475 cpl_table_set_column_unit(out, HAWKI_COL_DIST_I, "pixels"); 00476 cpl_table_new_column(out, HAWKI_COL_DIST_J, CPL_TYPE_INT); 00477 cpl_table_set_column_unit(out, HAWKI_COL_DIST_J, "pixels"); 00478 00479 /* Parse the file */ 00480 if ((in = fopen(filename, "r")) == NULL) { 00481 cpl_table_delete(out) ; 00482 return NULL ; 00483 } 00484 nbentries = 0 ; 00485 while (fgets(line, 1024, in) != NULL) { 00486 if (line[0] != '#') { 00487 if (sscanf(line, "%lg %lg %d %d", 00488 &dxgc, &dygc, &i, &j) != 4) { 00489 cpl_msg_error(__func__, "Bad line %d", nbentries+1) ; 00490 cpl_table_delete(out) ; 00491 return NULL ; 00492 } 00493 cpl_table_set_double(out, HAWKI_COL_DIST_DXGC, nbentries, dxgc); 00494 cpl_table_set_double(out, HAWKI_COL_DIST_DYGC, nbentries, dygc); 00495 cpl_table_set_int(out, HAWKI_COL_DIST_I, nbentries, i); 00496 cpl_table_set_int(out, HAWKI_COL_DIST_J, nbentries, j); 00497 nbentries ++ ; 00498 } 00499 } 00500 fclose(in) ; 00501 00502 return out ; 00503 } 00504 00505 /*----------------------------------------------------------------------------*/ 00523 /*----------------------------------------------------------------------------*/ 00524 static hawki_distortion * hawki_util_gendist_convert_to_images 00525 (const cpl_table * dist_tab, 00526 int detector_nx, 00527 int detector_ny) 00528 { 00529 hawki_distortion * out ; 00530 int nbentries ; 00531 int ngrid; 00532 const int * i_ptr; 00533 const int * j_ptr; 00534 cpl_array * i_vec; 00535 cpl_array * j_vec; 00536 int irow; 00537 00538 00539 /* Check entries */ 00540 if (dist_tab == NULL) return NULL ; 00541 00542 /* Create the table */ 00543 nbentries = cpl_table_get_nrow(dist_tab); 00544 ngrid = sqrt(nbentries); 00545 if(ngrid * ngrid != nbentries) 00546 { 00547 cpl_msg_error(__func__,"Only square grids are supported"); 00548 return NULL; 00549 } 00550 out = hawki_distortion_grid_new(detector_nx, detector_ny, ngrid); 00551 00552 /* Get the crval, cdelt */ 00553 i_ptr = cpl_table_get_data_int_const(dist_tab, HAWKI_COL_DIST_I); 00554 i_vec = cpl_array_wrap_int((int *)i_ptr, nbentries); 00555 out->x_crval = cpl_array_get_min(i_vec); 00556 out->x_cdelt = (cpl_array_get_max(i_vec) - cpl_array_get_min(i_vec)) / 00557 (ngrid - 1); 00558 cpl_array_unwrap(i_vec); 00559 j_ptr = cpl_table_get_data_int_const(dist_tab, HAWKI_COL_DIST_J); 00560 j_vec = cpl_array_wrap_int((int *)j_ptr, nbentries); 00561 out->y_crval = cpl_array_get_min(j_vec); 00562 out->y_cdelt = (cpl_array_get_max(j_vec) - cpl_array_get_min(j_vec)) / 00563 (ngrid - 1); 00564 cpl_array_unwrap(j_vec); 00565 00566 00567 /* Fill the image */ 00568 for(irow = 0; irow < nbentries; ++irow) 00569 { 00570 double i_ima; 00571 double j_ima; 00572 double dx; 00573 double dy; 00574 int null; 00575 00576 i_ima = (cpl_table_get_int(dist_tab, HAWKI_COL_DIST_I, irow, &null) - 00577 out->x_crval) / out->x_cdelt; 00578 if(floor(i_ima) != i_ima) 00579 { 00580 cpl_msg_error(__func__, " The distortion tables must be defined " 00581 "in a regular grid"); 00582 hawki_distortion_delete(out); 00583 return NULL; 00584 } 00585 j_ima = (cpl_table_get_int(dist_tab, HAWKI_COL_DIST_J, irow, &null) - 00586 out->y_crval) / out->y_cdelt; 00587 if(floor(j_ima) != j_ima) 00588 { 00589 cpl_msg_error(__func__, " The distortion tables must be defined " 00590 "in a regular grid"); 00591 hawki_distortion_delete(out); 00592 return NULL; 00593 } 00594 dx = cpl_table_get_double(dist_tab, HAWKI_COL_DIST_DXGC, irow, &null); 00595 dy = cpl_table_get_double(dist_tab, HAWKI_COL_DIST_DYGC, irow, &null); 00596 00597 cpl_image_set(out->dist_x, (int)i_ima + 1, (int)j_ima + 1, dx); 00598 cpl_image_set(out->dist_y, (int)i_ima + 1, (int)j_ima + 1, dy); 00599 } 00600 00601 return out ; 00602 }