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 #include <string.h>
00036 #include <math.h>
00037
00038
00039 #include <cpl.h>
00040
00041 #include <xsh_dfs.h>
00042 #include <xsh_data_pre.h>
00043 #include <xsh_parameters.h>
00044 #include <xsh_drl.h>
00045 #include <xsh_msg.h>
00046 #include <xsh_pfits.h>
00047 #include <xsh_pfits_qc.h>
00048 #include <xsh_error.h>
00049 #include <xsh_utils_image.h>
00050
00051
00052
00053
00054
00055
00056 #define RECIPE_ID "xsh_util_afc"
00057 #define RECIPE_AUTHOR "A.Modigliani"
00058 #define RECIPE_CONTACT "Andrea.Modigliani@eso.org"
00059 #define PRO_IMA "PRO_IMA_UVB"
00060 #define KEY_VALUE_HPRO_DID "PRO-1.15"
00061
00062 #define SPEED_LIGHT 2.99792458*1.E8
00063 #define TEL_AREA 51.2e4
00064
00065
00066
00067
00068 static int xsh_util_afc_create(cpl_plugin *) ;
00069 static int xsh_util_afc_exec(cpl_plugin *) ;
00070 static int xsh_util_afc_destroy(cpl_plugin *) ;
00071 static int xsh_util_afc(cpl_parameterlist *, cpl_frameset *) ;
00072
00073
00074
00075
00076
00077 static char
00078 xsh_util_afc_description_short[] = "Measure AFC frame shift";
00079 static char xsh_util_afc_description[] =
00080 "This recipe compute shift of AFC frame with respect to reference pos.\n"
00081 "The input files should be tagged as: \n"
00082 "AFC frame: AFC_arm,\n"
00083 "AFC pos table: AFC_POS_arm,\n"
00084 "The output is a table with measured positions, shifts and QC\n"
00085 "Information on relevant parameters can be found with\n"
00086 "esorex --params xsh_util_afc\n"
00087 "esorex --help xsh_util_afc\n"
00088 "\n";
00089
00090
00091
00092
00093
00098
00099
00101
00109
00110 int cpl_plugin_get_info(cpl_pluginlist * list)
00111 {
00112 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00113 cpl_plugin * plugin = &recipe->interface ;
00114
00115 cpl_plugin_init(plugin,
00116 CPL_PLUGIN_API,
00117 XSH_BINARY_VERSION,
00118 CPL_PLUGIN_TYPE_RECIPE,
00119 RECIPE_ID,
00120 xsh_util_afc_description_short,
00121 xsh_util_afc_description,
00122 RECIPE_AUTHOR,
00123 RECIPE_CONTACT,
00124 xsh_get_license(),
00125 xsh_util_afc_create,
00126 xsh_util_afc_exec,
00127 xsh_util_afc_destroy) ;
00128
00129 cpl_pluginlist_append(list, plugin) ;
00130
00131 return 0;
00132 }
00133
00134
00143
00144 static int xsh_util_afc_create(cpl_plugin * plugin)
00145 {
00146 cpl_recipe * recipe ;
00147
00148
00149 xsh_init();
00150
00151
00152 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00153 recipe = (cpl_recipe *)plugin ;
00154 else return -1 ;
00155 cpl_error_reset();
00156
00157
00158
00159 recipe->parameters = cpl_parameterlist_new() ;
00160
00161
00162
00163 check( xsh_parameters_generic(RECIPE_ID,
00164 recipe->parameters ) ) ;
00165 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00166
00167 cleanup:
00168
00169
00170 return 0;
00171 }
00172
00173
00179
00180 static int xsh_util_afc_exec(cpl_plugin * plugin)
00181 {
00182 cpl_recipe * recipe ;
00183 int code=0;
00184 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00185
00186
00187 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00188 recipe = (cpl_recipe *)plugin ;
00189 else return -1 ;
00190 cpl_error_reset();
00191
00192 code = xsh_util_afc(recipe->parameters, recipe->frames) ;
00193
00194
00195 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00196
00197
00198 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00199 }
00200
00201 return code ;
00202 }
00203
00204
00210
00211 static int xsh_util_afc_destroy(cpl_plugin * plugin)
00212 {
00213 cpl_recipe * recipe ;
00214
00215
00216 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00217 recipe = (cpl_recipe *)plugin ;
00218 else return -1 ;
00219
00220 cpl_parameterlist_delete(recipe->parameters) ;
00221 return 0 ;
00222 }
00223
00224
00225
00232
00233 static int
00234 xsh_util_afc( cpl_parameterlist * parlist,
00235 cpl_frameset * frames)
00236 {
00237 const char* recipe_tags[1] = {XSH_AFC_SLT};
00238 int recipe_tags_size = 1;
00239
00240 xsh_instrument* instrument=NULL;
00241
00242 cpl_frame* frm_tab=NULL;
00243 cpl_frame* frm_afc=NULL;
00244
00245 cpl_frameset* raws=NULL;
00246 cpl_frameset* calib=NULL;
00247
00248 cpl_image* ima_afc=NULL;
00249 cpl_table* tbl_afc=NULL;
00250
00251 const char* name=NULL;
00252 char afc_name[256];
00253 double xc=0;
00254 double yc=0;
00255 int size=4;
00256 double resx_med=0;
00257 double resy_med=0;
00258 double resx_avg=0;
00259 double resy_avg=0;
00260 cpl_propertylist* plist=NULL;
00261
00262 float* px=NULL;
00263 float* py=NULL;
00264 float* pxm=NULL;
00265 float* pym=NULL;
00266 int i=0;
00267 int x=0;
00268 int y=0;
00269
00270 int nrow=0;
00271 cpl_frame* frm_tab_afc=NULL;
00272 const char* tag=NULL;
00273
00274
00275 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
00276 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
00277
00278
00279
00280 check( xsh_begin( frames, parlist, &instrument, &raws, &calib,
00281 recipe_tags, recipe_tags_size,
00282 RECIPE_ID, XSH_BINARY_VERSION,
00283 xsh_util_afc_description_short ) ) ;
00284
00285
00286
00287
00288
00289 check(frm_afc=xsh_find_frame_with_tag(raws,XSH_AFC_SLT, instrument));
00290 check(frm_tab=xsh_find_frame_with_tag(calib,XSH_AFC_POS,instrument));
00291
00292
00293 check(name=cpl_frame_get_filename(frm_afc));
00294
00295 check(ima_afc=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
00296
00297 check(name=cpl_frame_get_filename(frm_tab));
00298 check(tbl_afc=cpl_table_load(name,1,0));
00299 check(nrow=cpl_table_get_nrow(tbl_afc));
00300
00301 cpl_table_new_column(tbl_afc,"XM",CPL_TYPE_FLOAT);
00302 cpl_table_new_column(tbl_afc,"YM",CPL_TYPE_FLOAT);
00303 cpl_table_fill_column_window_float(tbl_afc,"XM",0,nrow,-1);
00304 cpl_table_fill_column_window_float(tbl_afc,"YM",0,nrow,-1);
00305
00306 check(px=cpl_table_get_data_float(tbl_afc,"X"));
00307 check(py=cpl_table_get_data_float(tbl_afc,"Y"));
00308
00309 check(pxm=cpl_table_get_data_float(tbl_afc,"XM"));
00310 check(pym=cpl_table_get_data_float(tbl_afc,"YM"));
00311
00312
00313 for(i=0;i<nrow;i++) {
00314 x=(int)(px[i]+0.5);
00315 y=(int)(py[i]+0.5);
00316
00317
00318
00319
00320
00321 check(xc=cpl_image_get_centroid_x_window(ima_afc,
00322 x-size,y-size,
00323 x+size,y+size));
00324 check(yc=cpl_image_get_centroid_y_window(ima_afc,
00325 x-size,y-size,
00326 x+size,y+size));
00327
00328 pxm[i]=xc;
00329 pym[i]=yc;
00330 }
00331
00332 cpl_table_duplicate_column(tbl_afc,"RESX",tbl_afc,"XM");
00333 cpl_table_duplicate_column(tbl_afc,"RESY",tbl_afc,"YM");
00334
00335 cpl_table_subtract_columns(tbl_afc,"RESX","X");
00336 cpl_table_subtract_columns(tbl_afc,"RESY","Y");
00337
00338 resx_med=cpl_table_get_column_median(tbl_afc,"RESX");
00339 resy_med=cpl_table_get_column_median(tbl_afc,"RESY");
00340
00341 resx_avg=cpl_table_get_column_mean(tbl_afc,"RESX");
00342 resy_avg=cpl_table_get_column_mean(tbl_afc,"RESY");
00343 xsh_msg("AFC med shift X: %g Y: %g",resx_med,resy_med);
00344 xsh_msg("AFC avg shift X: %g Y: %g",resx_avg,resy_avg);
00345 check(tag=XSH_GET_TAG_FROM_ARM(XSH_AFC_POS,instrument));
00346 sprintf(afc_name,"%s.fits",tag);
00347 check(plist=cpl_propertylist_new());
00348 check(cpl_propertylist_append_double(plist,XSH_QC_AFC_RESX_MED,resx_med));
00349 check(cpl_propertylist_append_double(plist,XSH_QC_AFC_RESY_MED,resy_med));
00350 check(cpl_propertylist_append_double(plist,XSH_QC_AFC_RESX_AVG,resx_avg));
00351 check(cpl_propertylist_append_double(plist,XSH_QC_AFC_RESY_AVG,resy_avg));
00352 check(cpl_table_save(tbl_afc,plist,NULL,afc_name,CPL_IO_DEFAULT));
00353 check(frm_tab_afc=xsh_frame_product(afc_name,tag,
00354 CPL_FRAME_TYPE_TABLE,
00355 CPL_FRAME_GROUP_PRODUCT,
00356 CPL_FRAME_LEVEL_FINAL));
00357 check(xsh_add_product_table(frm_tab_afc, frames, parlist, RECIPE_ID,
00358 instrument));
00359 cleanup:
00360 xsh_instrument_free(&instrument);
00361 xsh_free_frame(&frm_tab_afc);
00362 xsh_free_frameset(&raws);
00363 xsh_free_frameset(&calib);
00364
00365 xsh_free_propertylist(&plist);
00366 xsh_free_image(&ima_afc);
00367 xsh_free_table(&tbl_afc);
00368
00369 if (cpl_error_get_code()) {
00370 return -1 ;
00371 } else {
00372 return 0 ;
00373 }
00374
00375 }