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
00166 cleanup:
00167
00168
00169 return 0;
00170 }
00171
00172
00178
00179 static int xsh_util_afc_exec(cpl_plugin * plugin)
00180 {
00181 cpl_recipe * recipe ;
00182 int code=0;
00183 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00184
00185
00186 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00187 recipe = (cpl_recipe *)plugin ;
00188 else return -1 ;
00189 cpl_error_reset();
00190
00191 code = xsh_util_afc(recipe->parameters, recipe->frames) ;
00192
00193
00194 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00195
00196
00197 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00198 }
00199
00200 return code ;
00201 }
00202
00203
00209
00210 static int xsh_util_afc_destroy(cpl_plugin * plugin)
00211 {
00212 cpl_recipe * recipe ;
00213
00214
00215 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00216 recipe = (cpl_recipe *)plugin ;
00217 else return -1 ;
00218
00219 cpl_parameterlist_delete(recipe->parameters) ;
00220 return 0 ;
00221 }
00222
00223
00224
00231
00232 static int
00233 xsh_util_afc( cpl_parameterlist * parlist,
00234 cpl_frameset * frames)
00235 {
00236 const char* recipe_tags[1] = {XSH_AFC_SLT};
00237 int recipe_tags_size = 1;
00238
00239 xsh_instrument* instrument=NULL;
00240
00241 cpl_frame* frm_tab=NULL;
00242 cpl_frame* frm_afc=NULL;
00243
00244 cpl_frameset* raws=NULL;
00245 cpl_frameset* calib=NULL;
00246
00247 cpl_image* ima_afc=NULL;
00248 cpl_table* tbl_afc=NULL;
00249
00250 const char* name=NULL;
00251 char afc_name[80];
00252 double xc=0;
00253 double yc=0;
00254 int size=4;
00255 double resx_med=0;
00256 double resy_med=0;
00257 double resx_avg=0;
00258 double resy_avg=0;
00259 cpl_propertylist* plist=NULL;
00260
00261 float* px=NULL;
00262 float* py=NULL;
00263 float* pxm=NULL;
00264 float* pym=NULL;
00265 int i=0;
00266 int x=0;
00267 int y=0;
00268
00269 int nrow=0;
00270 cpl_frame* frm_tab_afc=NULL;
00271 const char* tag=NULL;
00272
00273
00274 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
00275 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
00276
00277
00278
00279 check( xsh_begin( frames, parlist, &instrument, &raws, &calib,
00280 recipe_tags, recipe_tags_size,
00281 RECIPE_ID, XSH_BINARY_VERSION,
00282 xsh_util_afc_description_short ) ) ;
00283
00284
00285
00286
00287
00288 check(frm_afc=xsh_find_frame_with_tag(raws,XSH_AFC_SLT, instrument));
00289 check(frm_tab=xsh_find_frame_with_tag(calib,XSH_AFC_POS,instrument));
00290
00291
00292 check(name=cpl_frame_get_filename(frm_afc));
00293
00294 check(ima_afc=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
00295
00296 check(name=cpl_frame_get_filename(frm_tab));
00297 check(tbl_afc=cpl_table_load(name,1,0));
00298 check(nrow=cpl_table_get_nrow(tbl_afc));
00299
00300 cpl_table_new_column(tbl_afc,"XM",CPL_TYPE_FLOAT);
00301 cpl_table_new_column(tbl_afc,"YM",CPL_TYPE_FLOAT);
00302 cpl_table_fill_column_window_float(tbl_afc,"XM",0,nrow,-1);
00303 cpl_table_fill_column_window_float(tbl_afc,"YM",0,nrow,-1);
00304
00305 check(px=cpl_table_get_data_float(tbl_afc,"X"));
00306 check(py=cpl_table_get_data_float(tbl_afc,"Y"));
00307
00308 check(pxm=cpl_table_get_data_float(tbl_afc,"XM"));
00309 check(pym=cpl_table_get_data_float(tbl_afc,"YM"));
00310
00311
00312 for(i=0;i<nrow;i++) {
00313 x=(int)(px[i]+0.5);
00314 y=(int)(py[i]+0.5);
00315
00316
00317
00318
00319
00320 check(xc=cpl_image_get_centroid_x_window(ima_afc,
00321 x-size,y-size,
00322 x+size,y+size));
00323 check(yc=cpl_image_get_centroid_y_window(ima_afc,
00324 x-size,y-size,
00325 x+size,y+size));
00326
00327 pxm[i]=xc;
00328 pym[i]=yc;
00329 }
00330
00331 cpl_table_duplicate_column(tbl_afc,"RESX",tbl_afc,"XM");
00332 cpl_table_duplicate_column(tbl_afc,"RESY",tbl_afc,"YM");
00333
00334 cpl_table_subtract_columns(tbl_afc,"RESX","X");
00335 cpl_table_subtract_columns(tbl_afc,"RESY","Y");
00336
00337 resx_med=cpl_table_get_column_median(tbl_afc,"RESX");
00338 resy_med=cpl_table_get_column_median(tbl_afc,"RESY");
00339
00340 resx_avg=cpl_table_get_column_mean(tbl_afc,"RESX");
00341 resy_avg=cpl_table_get_column_mean(tbl_afc,"RESY");
00342 xsh_msg("AFC med shift X: %g Y: %g",resx_med,resy_med);
00343 xsh_msg("AFC avg shift X: %g Y: %g",resx_avg,resy_avg);
00344 check(tag=XSH_GET_TAG_FROM_ARM(XSH_AFC_POS,instrument));
00345 sprintf(afc_name,"%s.fits",tag);
00346 check(plist=cpl_propertylist_new());
00347 check(cpl_propertylist_append_double(plist,XSH_QC_AFC_RESX_MED,resx_med));
00348 check(cpl_propertylist_append_double(plist,XSH_QC_AFC_RESY_MED,resy_med));
00349 check(cpl_propertylist_append_double(plist,XSH_QC_AFC_RESX_AVG,resx_avg));
00350 check(cpl_propertylist_append_double(plist,XSH_QC_AFC_RESY_AVG,resy_avg));
00351 check(cpl_table_save(tbl_afc,plist,NULL,afc_name,CPL_IO_DEFAULT));
00352 check(frm_tab_afc=xsh_frame_product(afc_name,tag,
00353 CPL_FRAME_TYPE_TABLE,
00354 CPL_FRAME_GROUP_PRODUCT,
00355 CPL_FRAME_LEVEL_FINAL));
00356 check(xsh_add_product_table_multi(frm_tab_afc, frames, parlist, RECIPE_ID,
00357 instrument));
00358 cleanup:
00359 xsh_instrument_free(&instrument);
00360 xsh_free_frame(&frm_tab_afc);
00361 xsh_free_frameset(&raws);
00362 xsh_free_frameset(&calib);
00363
00364 xsh_free_propertylist(&plist);
00365 xsh_free_image(&ima_afc);
00366 xsh_free_table(&tbl_afc);
00367
00368 if (cpl_error_get_code()) {
00369 return -1 ;
00370 } else {
00371 return 0 ;
00372 }
00373
00374 }