|
HAWKI Pipeline Reference Manual 1.8.2
|
00001 /* $Id: hawki_util_gendist.c,v 1.20 2010/06/04 09:49:06 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: 2010/06/04 09:49:06 $ 00024 * $Revision: 1.20 $ 00025 * $Name: hawki-1_8_2 $ 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 00135 /* Fill the parameters list */ 00136 /* --dim_x */ 00137 p = cpl_parameter_new_value("hawki.hawki_util_gendist.dim_x", 00138 CPL_TYPE_INT, "Dimension of distortion image in X", 00139 "hawki.hawki_util_gendist", HAWKI_DET_NPIX_X); 00140 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dim_x"); 00141 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00142 cpl_parameterlist_append(recipe->parameters, p); 00143 00144 /* --dim_y */ 00145 p = cpl_parameter_new_value("hawki.hawki_util_gendist.dim_y", 00146 CPL_TYPE_INT, "Dimension of distortion image in Y", 00147 "hawki.hawki_util_gendist", HAWKI_DET_NPIX_Y); 00148 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dim_y"); 00149 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00150 cpl_parameterlist_append(recipe->parameters, p); 00151 00152 00153 /* Return */ 00154 return 0; 00155 } 00156 00157 /*----------------------------------------------------------------------------*/ 00163 /*----------------------------------------------------------------------------*/ 00164 static int hawki_util_gendist_exec(cpl_plugin * plugin) 00165 { 00166 cpl_recipe * recipe ; 00167 00168 /* Get the recipe out of the plugin */ 00169 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00170 recipe = (cpl_recipe *)plugin ; 00171 else return -1 ; 00172 00173 /* Issue a banner */ 00174 hawki_print_banner(); 00175 00176 return hawki_util_gendist(recipe->parameters, recipe->frames) ; 00177 } 00178 00179 /*----------------------------------------------------------------------------*/ 00185 /*----------------------------------------------------------------------------*/ 00186 static int hawki_util_gendist_destroy(cpl_plugin * plugin) 00187 { 00188 cpl_recipe * recipe ; 00189 00190 /* Get the recipe out of the plugin */ 00191 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00192 recipe = (cpl_recipe *)plugin ; 00193 else return -1 ; 00194 00195 cpl_parameterlist_delete(recipe->parameters) ; 00196 return 0 ; 00197 } 00198 00199 /*----------------------------------------------------------------------------*/ 00205 /*----------------------------------------------------------------------------*/ 00206 static int hawki_util_gendist 00207 (cpl_parameterlist * parlist, 00208 cpl_frameset * framelist) 00209 { 00210 cpl_frameset * rawframes; 00211 const cpl_frame * cur_frame; 00212 const char * dist_name; 00213 cpl_table ** distortion_table; 00214 hawki_distortion ** distortion_im; 00215 cpl_propertylist * plist; 00216 cpl_propertylist * plist_ext; 00217 char sval[64]; 00218 const char * recipe_name = "hawki_util_gendist"; 00219 int iext; 00220 int ichip; 00221 cpl_parameter * par; 00222 int dim_x; 00223 int dim_y; 00224 00225 /* Identify the RAW and CALIB frames in the input frameset */ 00226 if (hawki_dfs_set_groups(framelist)) { 00227 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ; 00228 return -1 ; 00229 } 00230 00231 /* Retrieve raw frames */ 00232 if ((rawframes = hawki_extract_frameset(framelist, 00233 HAWKI_UTIL_DISTMAP_RAW)) == NULL) { 00234 cpl_msg_error(__func__, "Cannot find raw frames in the input list") ; 00235 return -1 ; 00236 } 00237 00238 /* There should be HAWKI_NB_DETECTORS input frames ordered */ 00239 if (cpl_frameset_get_size(rawframes) != HAWKI_NB_DETECTORS) { 00240 cpl_msg_error(__func__, "%d frames expected", HAWKI_NB_DETECTORS) ; 00241 cpl_frameset_delete(rawframes) ; 00242 return -1 ; 00243 } 00244 00245 /* Retrieve parameters */ 00246 par = cpl_parameterlist_find(parlist, "hawki.hawki_util_gendist.dim_x") ; 00247 dim_x = cpl_parameter_get_int(par); 00248 par = cpl_parameterlist_find(parlist, "hawki.hawki_util_gendist.dim_y") ; 00249 dim_y = cpl_parameter_get_int(par); 00250 00251 /* Allocate holder for the tables */ 00252 distortion_table = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table*)) ; 00253 00254 /* Loop on the chips */ 00255 for (ichip=0 ; ichip<HAWKI_NB_DETECTORS ; ichip++) { 00256 00257 /* Get the file name */ 00258 cur_frame = cpl_frameset_get_frame_const(rawframes, ichip) ; 00259 dist_name = cpl_frame_get_filename(cur_frame) ; 00260 00261 /* Create the output table */ 00262 cpl_msg_info(__func__, "Create the output table for chip %d", ichip+1) ; 00263 if ((distortion_table[ichip] = hawki_util_gendist_convert(dist_name)) == NULL) { 00264 int j; 00265 cpl_msg_error(__func__, "Cannot create the output table") ; 00266 cpl_frameset_delete(rawframes) ; 00267 for (j=0 ; j<ichip ; j++) cpl_table_delete(distortion_table[j]) ; 00268 cpl_free(distortion_table) ; 00269 return -1 ; 00270 } 00271 } 00272 00273 /* Write the table */ 00274 plist = cpl_propertylist_new() ; 00275 cpl_propertylist_append_string(plist, "INSTRUME", "HAWKI") ; 00276 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, 00277 HAWKI_PROTYPE_DISTORTION) ; 00278 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, 00279 HAWKI_CALPRO_DISTORTION) ; 00280 00281 plist_ext = cpl_propertylist_new() ; 00282 cpl_propertylist_prepend_string(plist_ext, "EXTNAME", "CHIP1.INT1") ; 00283 if (cpl_dfs_save_table(framelist, NULL, parlist, rawframes, NULL, 00284 distortion_table[0], plist_ext, recipe_name, 00285 plist, NULL, PACKAGE "/" PACKAGE_VERSION, 00286 "hawki_util_gendist.fits") != CPL_ERROR_NONE) { 00287 cpl_msg_error(__func__, "Cannot save the first extension table") ; 00288 cpl_propertylist_delete(plist_ext) ; 00289 cpl_propertylist_delete(plist) ; 00290 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) 00291 cpl_table_delete(distortion_table[iext]) ; 00292 cpl_free(distortion_table) ; 00293 cpl_frameset_delete(rawframes) ; 00294 return -1 ; 00295 } 00296 cpl_propertylist_delete(plist) ; 00297 cpl_propertylist_delete(plist_ext) ; 00298 00299 /* Save the extensions */ 00300 for (iext=1 ; iext<HAWKI_NB_DETECTORS; iext++) 00301 { 00302 ichip = iext; 00303 //This is the actual layout of the chips in raw HAWK-I images. 00304 if(iext == 2) 00305 ichip = 3; 00306 if(iext == 3) 00307 ichip = 2; 00308 plist_ext = cpl_propertylist_new() ; 00309 sprintf(sval, "CHIP%d.INT1", ichip+1) ; 00310 cpl_propertylist_prepend_string(plist_ext, "EXTNAME", sval) ; 00311 cpl_table_save(distortion_table[ichip], NULL, plist_ext, 00312 "hawki_util_gendist.fits", CPL_IO_EXTEND); 00313 cpl_propertylist_delete(plist_ext) ; 00314 } 00315 00316 /* Allocate holder for the distortion images */ 00317 distortion_im = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(hawki_distortion*)); 00318 00319 /* Loop on the chips */ 00320 for (ichip=0 ; ichip<HAWKI_NB_DETECTORS ; ichip++) 00321 { 00322 /* Get the file name */ 00323 cur_frame = cpl_frameset_get_frame_const(rawframes, ichip) ; 00324 dist_name = cpl_frame_get_filename(cur_frame) ; 00325 00326 /* Create the output image */ 00327 cpl_msg_info(__func__, "Create the output images for extension %d", ichip+1) ; 00328 if ((distortion_im[ichip] = hawki_util_gendist_convert_to_images 00329 (distortion_table[ichip], dim_x, dim_y)) == NULL) 00330 { 00331 int j; 00332 cpl_msg_error(__func__,"Cannot create the output distortion images"); 00333 cpl_frameset_delete(rawframes); 00334 for (j=0 ;j < ichip; j++) 00335 hawki_distortion_delete(distortion_im[j]); 00336 cpl_free(distortion_im) ; 00337 return -1 ; 00338 } 00339 } 00340 00341 /* Write the distortion images */ 00342 plist = cpl_propertylist_new() ; 00343 cpl_propertylist_append_string(plist, "INSTRUME", "HAWKI"); 00344 //This two keywords are needed by QC. I do not why.. 00345 cpl_propertylist_append_string(plist, "MJD-OBS", "55128.5000000"); 00346 cpl_propertylist_append_string(plist, "FOR_QC", "dummy.fits"); 00347 cpl_propertylist_append_string(plist, "ORIGFILE", 00348 "hawki_util_gendist_distx.fits") ; 00349 hawki_main_header_save(framelist, parlist, rawframes, 00350 "hawki_util_gendist", 00351 HAWKI_CALPRO_DISTORTION_X, 00352 HAWKI_PROTYPE_DISTORTION_X, 00353 plist, "hawki_util_gendist_distx.fits"); 00354 cpl_propertylist_erase(plist, "ORIGFILE"); 00355 cpl_propertylist_append_string(plist, "ORIGFILE", 00356 "hawki_util_gendist_distx.fits") ; 00357 hawki_main_header_save(framelist, parlist, rawframes, 00358 "hawki_util_gendist", 00359 HAWKI_CALPRO_DISTORTION_Y, 00360 HAWKI_PROTYPE_DISTORTION_Y, 00361 plist, "hawki_util_gendist_disty.fits"); 00362 cpl_propertylist_delete(plist) ; 00363 00364 /* Save the extensions */ 00365 //There is kind of a hack here 00366 //We use the distortion table as a reference to save the distortion images 00367 //to have a proper layout of the detectors in the FITS file. 00368 //For that hawki_get_extref_file has been modified. 00369 for (iext=0 ; iext<HAWKI_NB_DETECTORS; iext++) { 00370 ichip = iext; 00371 //This is the actual layout of the chips in raw HAWK-I images. 00372 if(iext == 2) 00373 ichip = 3; 00374 if(iext == 3) 00375 ichip = 2; 00376 plist_ext = cpl_propertylist_new() ; 00377 cpl_propertylist_append_double(plist_ext, "CRVAL1", 00378 distortion_im[ichip]->x_crval); 00379 cpl_propertylist_append_double(plist_ext, "CRVAL2", 00380 distortion_im[ichip]->y_crval); 00381 cpl_propertylist_append_double(plist_ext, "CDELT1", 00382 distortion_im[ichip]->x_cdelt); 00383 cpl_propertylist_append_double(plist_ext, "CDELT2", 00384 distortion_im[ichip]->y_cdelt); 00385 cpl_propertylist_append_double(plist_ext, "CRPIX1", 1); 00386 cpl_propertylist_append_double(plist_ext, "CRPIX2", 1); 00387 cpl_propertylist_append_string(plist_ext, "CTYPE1", ""); 00388 cpl_propertylist_append_string(plist_ext, "CTYPE2", ""); 00389 cpl_propertylist_append_string(plist_ext, "CUNIT1", ""); 00390 cpl_propertylist_append_string(plist_ext, "CUNIT2", ""); 00391 hawki_image_ext_save(framelist, distortion_im[ichip]->dist_x, iext + 1, 00392 plist_ext, "hawki_util_gendist_distx.fits"); 00393 hawki_image_ext_save(framelist, distortion_im[ichip]->dist_y, iext + 1, 00394 plist_ext, "hawki_util_gendist_disty.fits"); 00395 cpl_propertylist_delete(plist_ext); 00396 } 00397 00398 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) 00399 cpl_table_delete(distortion_table[iext]); 00400 cpl_free(distortion_table) ; 00401 00402 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) 00403 hawki_distortion_delete(distortion_im[iext]); 00404 cpl_free(distortion_im) ; 00405 cpl_frameset_delete(rawframes) ; 00406 00407 00408 /* return */ 00409 if (cpl_error_get_code()) return -1 ; 00410 else return 0 ; 00411 } 00412 00413 /*----------------------------------------------------------------------------*/ 00438 /*----------------------------------------------------------------------------*/ 00439 static cpl_table * hawki_util_gendist_convert(const char * filename) 00440 { 00441 cpl_table * out ; 00442 int nbentries ; 00443 FILE * in ; 00444 double dxgc, dygc ; 00445 int i, j ; 00446 char line[1024]; 00447 00448 /* Check entries */ 00449 if (filename == NULL) return NULL ; 00450 00451 /* Get the number of lines */ 00452 nbentries = 0 ; 00453 if ((in = fopen(filename, "r")) == NULL) { 00454 return NULL ; 00455 } 00456 while (fgets(line, 1024, in) != NULL) { 00457 if (line[0] != '#') nbentries ++ ; 00458 } 00459 fclose(in) ; 00460 00461 /* Create the table */ 00462 out = cpl_table_new(nbentries); 00463 cpl_table_new_column(out, HAWKI_COL_DIST_DXGC, CPL_TYPE_DOUBLE); 00464 cpl_table_set_column_unit(out, HAWKI_COL_DIST_DXGC, "pixels"); 00465 cpl_table_new_column(out, HAWKI_COL_DIST_DYGC, CPL_TYPE_DOUBLE); 00466 cpl_table_set_column_unit(out, HAWKI_COL_DIST_DYGC, "pixels"); 00467 cpl_table_new_column(out, HAWKI_COL_DIST_I, CPL_TYPE_INT); 00468 cpl_table_set_column_unit(out, HAWKI_COL_DIST_I, "pixels"); 00469 cpl_table_new_column(out, HAWKI_COL_DIST_J, CPL_TYPE_INT); 00470 cpl_table_set_column_unit(out, HAWKI_COL_DIST_J, "pixels"); 00471 00472 /* Parse the file */ 00473 if ((in = fopen(filename, "r")) == NULL) { 00474 cpl_table_delete(out) ; 00475 return NULL ; 00476 } 00477 nbentries = 0 ; 00478 while (fgets(line, 1024, in) != NULL) { 00479 if (line[0] != '#') { 00480 if (sscanf(line, "%lg %lg %d %d", 00481 &dxgc, &dygc, &i, &j) != 4) { 00482 cpl_msg_error(__func__, "Bad line %d", nbentries+1) ; 00483 cpl_table_delete(out) ; 00484 return NULL ; 00485 } 00486 cpl_table_set_double(out, HAWKI_COL_DIST_DXGC, nbentries, dxgc); 00487 cpl_table_set_double(out, HAWKI_COL_DIST_DYGC, nbentries, dygc); 00488 cpl_table_set_int(out, HAWKI_COL_DIST_I, nbentries, i); 00489 cpl_table_set_int(out, HAWKI_COL_DIST_J, nbentries, j); 00490 nbentries ++ ; 00491 } 00492 } 00493 fclose(in) ; 00494 00495 return out ; 00496 } 00497 00498 /*----------------------------------------------------------------------------*/ 00516 /*----------------------------------------------------------------------------*/ 00517 static hawki_distortion * hawki_util_gendist_convert_to_images 00518 (const cpl_table * dist_tab, 00519 int detector_nx, 00520 int detector_ny) 00521 { 00522 hawki_distortion * out ; 00523 int nbentries ; 00524 int ngrid; 00525 const int * i_ptr; 00526 const int * j_ptr; 00527 cpl_array * i_vec; 00528 cpl_array * j_vec; 00529 int irow; 00530 00531 00532 /* Check entries */ 00533 if (dist_tab == NULL) return NULL ; 00534 00535 /* Create the table */ 00536 nbentries = cpl_table_get_nrow(dist_tab); 00537 ngrid = sqrt(nbentries); 00538 if(ngrid * ngrid != nbentries) 00539 { 00540 cpl_msg_error(__func__,"Only square grids are supported"); 00541 return NULL; 00542 } 00543 out = hawki_distortion_grid_new(detector_nx, detector_ny, ngrid); 00544 00545 /* Get the crval, cdelt */ 00546 i_ptr = cpl_table_get_data_int_const(dist_tab, HAWKI_COL_DIST_I); 00547 i_vec = cpl_array_wrap_int((int *)i_ptr, nbentries); 00548 out->x_crval = cpl_array_get_min(i_vec); 00549 out->x_cdelt = (cpl_array_get_max(i_vec) - cpl_array_get_min(i_vec)) / 00550 (ngrid - 1); 00551 cpl_array_unwrap(i_vec); 00552 j_ptr = cpl_table_get_data_int_const(dist_tab, HAWKI_COL_DIST_J); 00553 j_vec = cpl_array_wrap_int((int *)j_ptr, nbentries); 00554 out->y_crval = cpl_array_get_min(j_vec); 00555 out->y_cdelt = (cpl_array_get_max(j_vec) - cpl_array_get_min(j_vec)) / 00556 (ngrid - 1); 00557 cpl_array_unwrap(j_vec); 00558 00559 00560 /* Fill the image */ 00561 for(irow = 0; irow < nbentries; ++irow) 00562 { 00563 double i_ima; 00564 double j_ima; 00565 double dx; 00566 double dy; 00567 int null; 00568 00569 i_ima = (cpl_table_get_int(dist_tab, HAWKI_COL_DIST_I, irow, &null) - 00570 out->x_crval) / out->x_cdelt; 00571 if(floor(i_ima) != i_ima) 00572 { 00573 cpl_msg_error(__func__, " The distortion tables must be defined " 00574 "in a regular grid"); 00575 hawki_distortion_delete(out); 00576 return NULL; 00577 } 00578 j_ima = (cpl_table_get_int(dist_tab, HAWKI_COL_DIST_J, irow, &null) - 00579 out->y_crval) / out->y_cdelt; 00580 if(floor(j_ima) != j_ima) 00581 { 00582 cpl_msg_error(__func__, " The distortion tables must be defined " 00583 "in a regular grid"); 00584 hawki_distortion_delete(out); 00585 return NULL; 00586 } 00587 dx = cpl_table_get_double(dist_tab, HAWKI_COL_DIST_DXGC, irow, &null); 00588 dy = cpl_table_get_double(dist_tab, HAWKI_COL_DIST_DYGC, irow, &null); 00589 00590 cpl_image_set(out->dist_x, (int)i_ima + 1, (int)j_ima + 1, dx); 00591 cpl_image_set(out->dist_y, (int)i_ima + 1, (int)j_ima + 1, dy); 00592 } 00593 00594 return out ; 00595 }
1.7.3