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 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
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_pfits.h"
00043 #include "hawki_dfs.h"
00044 #include "hawki_load.h"
00045
00046
00047
00048
00049
00050 static int hawki_step_stitch_create(cpl_plugin *) ;
00051 static int hawki_step_stitch_exec(cpl_plugin *) ;
00052 static int hawki_step_stitch_destroy(cpl_plugin *) ;
00053 static int hawki_step_stitch(cpl_parameterlist *, cpl_frameset *) ;
00054 static int hawki_step_stitch_save
00055 (cpl_image * in,
00056 cpl_frame * combined,
00057 cpl_parameterlist * parlist,
00058 cpl_frameset * set);
00059
00060
00061
00062
00063
00064 static char hawki_step_stitch_description[] =
00065 "hawki_step_stitch -- Stitching utility\n"
00066 "This recipe accepts 1 parameter:\n"
00067 "First parameter: the HAWKI image to stitch "
00068 " (PRO CATG = "HAWKI_CALPRO_COMBINED")\n"
00069 "\n"
00070 "This recipe produces 1 file:\n"
00071 "First product: the stitch image.\n"
00072 " (PRO CATG = "HAWKI_CALPRO_STITCHED")\n" ;
00073
00074
00075
00076
00077
00078
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_step_stitch",
00098 "Stitching utility",
00099 hawki_step_stitch_description,
00100 "Cesar Enrique Garcia",
00101 PACKAGE_BUGREPORT,
00102 hawki_get_license(),
00103 hawki_step_stitch_create,
00104 hawki_step_stitch_exec,
00105 hawki_step_stitch_destroy) ;
00106
00107 cpl_pluginlist_append(list, plugin) ;
00108
00109 return 0;
00110 }
00111
00112
00120
00121 static int hawki_step_stitch_create(cpl_plugin * plugin)
00122 {
00123 cpl_recipe * recipe ;
00124
00125
00126 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00127 recipe = (cpl_recipe *)plugin ;
00128 else return -1 ;
00129
00130
00131 recipe->parameters = cpl_parameterlist_new() ;
00132
00133
00134 return 0;
00135 }
00136
00137
00143
00144 static int hawki_step_stitch_exec(cpl_plugin * plugin)
00145 {
00146 cpl_recipe * recipe ;
00147
00148
00149 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00150 recipe = (cpl_recipe *)plugin ;
00151 else return -1 ;
00152
00153
00154 hawki_print_banner();
00155
00156 return hawki_step_stitch(recipe->parameters, recipe->frames) ;
00157 }
00158
00159
00165
00166 static int hawki_step_stitch_destroy(cpl_plugin * plugin)
00167 {
00168 cpl_recipe * recipe ;
00169
00170
00171 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00172 recipe = (cpl_recipe *)plugin ;
00173 else return -1 ;
00174
00175 cpl_parameterlist_delete(recipe->parameters) ;
00176 return 0 ;
00177 }
00178
00179
00186
00187 static int hawki_step_stitch(
00188 cpl_parameterlist * parlist,
00189 cpl_frameset * frameset)
00190 {
00191 const char * comb_filename ;
00192 cpl_frameset * combframes;
00193 cpl_frame * combframe;
00194 cpl_propertylist * plist ;
00195 cpl_image * stitched ;
00196 cpl_image * in[HAWKI_NB_DETECTORS] ;
00197 double posx[HAWKI_NB_DETECTORS] ;
00198 double posy[HAWKI_NB_DETECTORS] ;
00199 int i, j ;
00200 cpl_errorstate error_prevstate;
00201
00202
00203
00204
00205
00206 if (hawki_dfs_set_groups(frameset)) {
00207 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00208 return -1 ;
00209 }
00210
00211
00212 cpl_msg_info(__func__, "Identifying the combined frame");
00213 combframes = hawki_extract_frameset
00214 (frameset, HAWKI_CALPRO_COMBINED);
00215 if (combframes == NULL)
00216 {
00217 cpl_msg_error(__func__, "No combined images found (%s)",
00218 HAWKI_CALPRO_COMBINED);
00219 cpl_frameset_delete(combframes);
00220 return -1 ;
00221 }
00222
00223
00224 if (cpl_frameset_get_size(combframes) != 1) {
00225 cpl_msg_error(__func__, "Expects one single combined images") ;
00226 cpl_frameset_delete(combframes);
00227 return -1 ;
00228 }
00229
00230
00231 cpl_msg_info(__func__,"Loading combined frame");
00232 for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
00233 if ((in[i] = hawki_load_image(combframes, 0, i+1,
00234 CPL_TYPE_FLOAT)) == NULL) {
00235 cpl_msg_error(__func__, "Cannot load chip nb %d", i+1) ;
00236 for (j=0 ; j<i ; i++) cpl_image_delete(in[j]) ;
00237 cpl_frameset_delete(combframes);
00238 return -1 ;
00239 }
00240 }
00241
00242
00243 combframe = cpl_frameset_get_first(combframes);
00244 comb_filename = cpl_frame_get_filename(combframe);
00245
00246
00247 error_prevstate = cpl_errorstate_get();
00248 for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
00249 plist = cpl_propertylist_load_regexp(comb_filename, i+1, "QC", 0) ;
00250 posx[i] = hawki_pfits_get_comb_posx(plist);
00251 posy[i] = hawki_pfits_get_comb_posy(plist);
00252 cpl_propertylist_delete(plist) ;
00253 if(!cpl_errorstate_is_equal(error_prevstate))
00254 {
00255 cpl_msg_error(__func__, "Cannot get POS infos for chip %d", i+1) ;
00256 return -1 ;
00257 }
00258 }
00259
00260
00261 cpl_msg_info(__func__, "Computing the stiched image") ;
00262 if ((stitched = hawki_images_stitch(in, posx, posy)) == NULL) {
00263 cpl_msg_error(__func__, "Cannot stitch the images") ;
00264 for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) cpl_image_delete(in[i]) ;
00265 return -1 ;
00266 }
00267 for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) cpl_image_delete(in[i]) ;
00268
00269
00270 if (hawki_step_stitch_save(stitched, combframe, parlist, frameset) == -1)
00271 cpl_msg_warning(__func__,"Some data could not be saved. "
00272 "Check permisions or disk space");
00273
00274
00275 cpl_frameset_delete(combframes);
00276 cpl_image_delete(stitched);
00277
00278
00279 if (cpl_error_get_code())
00280 {
00281 cpl_msg_error(__func__,
00282 "HAWK-I pipeline could not recover from previous errors");
00283 return -1 ;
00284 }
00285 else return 0 ;
00286 }
00287
00288
00296
00297 static int hawki_step_stitch_save
00298 (cpl_image * in,
00299 cpl_frame * combined,
00300 cpl_parameterlist * parlist,
00301 cpl_frameset * set)
00302 {
00303 cpl_propertylist * plist;
00304 cpl_propertylist * wcslist;
00305 const char * recipe_name = "hawki_step_stitch" ;
00306 int ext_chip_1;
00307 cpl_errorstate error_prevstate = cpl_errorstate_get();
00308
00309 cpl_msg_indent_more();
00310
00311
00312 plist = cpl_propertylist_new();
00313 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
00314 HAWKI_PROTYPE_STITCHED) ;
00315 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
00316 HAWKI_CALPRO_STITCHED) ;
00317
00318
00319 ext_chip_1 = 1;
00320 wcslist = cpl_propertylist_load_regexp(
00321 cpl_frame_get_filename(combined), ext_chip_1, HAWKI_HEADER_WCS, 0);
00322 cpl_propertylist_append(plist, wcslist);
00323
00324
00325 if(cpl_dfs_save_image(set,
00326 NULL,
00327 parlist,
00328 set,
00329 NULL,
00330 in,
00331 CPL_BPP_IEEE_FLOAT,
00332 recipe_name,
00333 plist,
00334 NULL,
00335 PACKAGE "/" PACKAGE_VERSION,
00336 "hawki_step_stitch.fits") != CPL_ERROR_NONE)
00337 cpl_msg_error(__func__,"Could not save stitched image");
00338
00339 cpl_propertylist_delete(plist) ;
00340 cpl_propertylist_delete(wcslist) ;
00341 cpl_msg_indent_less();
00342 if(!cpl_errorstate_is_equal(error_prevstate))
00343 {
00344 cpl_errorstate_set(CPL_ERROR_NONE);
00345 return -1;
00346 }
00347 return 0;
00348 }
00349