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 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00032
00033
00034 #include <string.h>
00035 #include <math.h>
00036
00037
00038 #include <cpl.h>
00039
00040 #include <xsh_dfs.h>
00041 #include <xsh_data_pre.h>
00042 #include <xsh_parameters.h>
00043 #include <xsh_drl.h>
00044 #include <xsh_msg.h>
00045 #include <xsh_pfits.h>
00046 #include <xsh_pfits_qc.h>
00047 #include <xsh_error.h>
00048 #include <xsh_utils_image.h>
00049 #include <xsh_data_atmos_ext.h>
00050
00051
00052
00053
00054
00055 #define RECIPE_ID "xsh_util_efficiency"
00056 #define RECIPE_AUTHOR "A.Modigliani"
00057 #define RECIPE_CONTACT "Andrea.Modigliani@eso.org"
00058 #define PRO_IMA "PRO_IMA_UVB"
00059 #define KEY_VALUE_HPRO_DID "PRO-1.15"
00060
00061 #define SPEED_LIGHT 2.99792458*1.E8
00062 #define TEL_AREA 51.2e4
00063
00064
00065
00066
00067 static int xsh_util_efficiency_create(cpl_plugin *) ;
00068 static int xsh_util_efficiency_exec(cpl_plugin *) ;
00069 static int xsh_util_efficiency_destroy(cpl_plugin *) ;
00070 static int xsh_util_efficiency(cpl_parameterlist *, cpl_frameset *) ;
00071
00072
00073
00074
00075
00076 static char
00077 xsh_util_efficiency_description_short[] = "Compute efficiency";
00078 static char xsh_util_efficiency_description[] =
00079 "This recipe compute efficiency.\n"
00080 "The input files should be tagged as: \n"
00081 "input observed spectrum: IMA_arm,\n"
00082 "input reference spectrum: FLUX_STD_TABLE,\n"
00083 "input atmospheric extintion table: EXTCOEFF_TABLE)\n"
00084 "The output is a table with efficiency as function of wavelength\n"
00085 "Information on relevant parameters can be found with\n"
00086 "esorex --params xsh_util_efficiency\n"
00087 "esorex --help xsh_util_efficiency\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_efficiency_description_short,
00121 xsh_util_efficiency_description,
00122 RECIPE_AUTHOR,
00123 RECIPE_CONTACT,
00124 xsh_get_license(),
00125 xsh_util_efficiency_create,
00126 xsh_util_efficiency_exec,
00127 xsh_util_efficiency_destroy) ;
00128
00129 cpl_pluginlist_append(list, plugin) ;
00130
00131 return 0;
00132 }
00133
00134
00143
00144 static int xsh_util_efficiency_create(cpl_plugin * plugin)
00145 {
00146 cpl_recipe * recipe ;
00147 cpl_parameter * p ;
00148
00149
00150 xsh_init();
00151
00152
00153 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00154 recipe = (cpl_recipe *)plugin ;
00155 else return -1 ;
00156 cpl_error_reset();
00157
00158
00159
00160 recipe->parameters = cpl_parameterlist_new() ;
00161
00162
00163
00164 check( xsh_parameters_generic(RECIPE_ID,
00165 recipe->parameters ) ) ;
00166 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00167
00168 p = cpl_parameter_new_value("xsh.xsh_util_efficiency.airmass",
00169 CPL_TYPE_DOUBLE,
00170 "Airmass value",
00171 "xsh.xsh_util_efficiency",1.2);
00172 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "airmass") ;
00173 cpl_parameterlist_append(recipe->parameters, p) ;
00174
00175
00176 p = cpl_parameter_new_value("xsh.xsh_util_efficiency.gain",
00177 CPL_TYPE_DOUBLE,
00178 "Detector gain value",
00179 "xsh.xsh_util_efficiency",1.5);
00180 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
00181 cpl_parameterlist_append(recipe->parameters, p) ;
00182
00183 cleanup:
00184
00185
00186 return 0;
00187 }
00188
00189
00195
00196 static int xsh_util_efficiency_exec(cpl_plugin * plugin)
00197 {
00198 cpl_recipe * recipe ;
00199 int code=0;
00200 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00201
00202
00203 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00204 recipe = (cpl_recipe *)plugin ;
00205 else return -1 ;
00206 cpl_error_reset();
00207
00208 code = xsh_util_efficiency(recipe->parameters, recipe->frames) ;
00209
00210
00211 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00212
00213
00214 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00215 }
00216
00217 return code ;
00218 }
00219
00220
00226
00227 static int xsh_util_efficiency_destroy(cpl_plugin * plugin)
00228 {
00229 cpl_recipe * recipe ;
00230
00231
00232 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00233 recipe = (cpl_recipe *)plugin ;
00234 else return -1 ;
00235
00236 cpl_parameterlist_delete(recipe->parameters) ;
00237 return 0 ;
00238 }
00239
00240 static double
00241 xsh_table_get_interpolated(cpl_table* tbl,
00242 double wav,
00243 const char* colx,
00244 const char* coly)
00245 {
00246
00247 double y=0;
00248 double w1=0;
00249 double w2=0;
00250 double y1=0;
00251 double y2=0;
00252 float* pe=NULL;
00253 float* pw=NULL;
00254 int nrow=0;
00255 int i=0;
00256
00257
00258 w1=cpl_table_get_column_min(tbl,colx);
00259 w2=cpl_table_get_column_max(tbl,colx);
00260 y1=cpl_table_get_column_min(tbl,coly);
00261 y2=cpl_table_get_column_max(tbl,coly);
00262
00263
00264 pw=cpl_table_get_data_float(tbl,colx);
00265 pe=cpl_table_get_data_float(tbl,coly);
00266 nrow=cpl_table_get_nrow(tbl);
00267
00268
00269 for(i=0;i<nrow-1;i++) {
00270 if(pw[i]<wav) {
00271 w1=pw[i];
00272 w2=pw[i+1];
00273 y1=pe[i];
00274 y2=pe[i+1];
00275 }
00276 }
00277
00278 y=y1+(y2-y1)/(w2-w1)*(wav-w1);
00279
00280
00281 return y;
00282
00283 }
00284
00285
00286
00293
00294 static int
00295 xsh_util_efficiency( cpl_parameterlist * parlist,
00296 cpl_frameset * frames)
00297 {
00298 const char* recipe_tags[1] = {XSH_EFFICIENCY};
00299 int recipe_tags_size = 1;
00300
00301 xsh_instrument* instrument=NULL;
00302 cpl_parameter * param= NULL ;
00303 cpl_frame* frm_sci=NULL;
00304 cpl_frame* frm_ref=NULL;
00305 cpl_frame* frm_atmext=NULL;
00306
00307 cpl_frameset* raws=NULL;
00308 cpl_frameset* calib=NULL;
00309
00310 cpl_image* ima_sci=NULL;
00311 cpl_table* tbl_ref=NULL;
00312 cpl_table* tbl_ext=NULL;
00313 cpl_table* tbl_atmext=NULL;
00314
00315 const char* name=NULL;
00316 double aimprim=1.446;
00317
00318 double gain=1.5;
00319 double ref_wmin=0;
00320 double ref_wmax=0;
00321 double src_wmin=0;
00322 double src_wmax=0;
00323 double ext_wmin=0;
00324 double ext_wmax=0;
00325 double ext_wdel=0;
00326
00327 double wmin=0;
00328 double wmax=0;
00329
00330 cpl_propertylist* plist=NULL;
00331 double crpix1=0;
00332 double crval1=0;
00333 double cdelt1=0;
00334 int naxis1=0;
00335 int nrow=0;
00336 float* psrc=NULL;
00337 float* pext=NULL;
00338 float* pwav=NULL;
00339 float* pcor=NULL;
00340 float* pref=NULL;
00341 float* peph=NULL;
00342
00343
00344 double exptime=0;
00345 double confidence=0;
00346 cpl_vector* rec_profile=NULL;
00347 double x=0;
00348 int i=0;
00349 double airmass=0;
00350 double binsize=0.1;
00351
00352 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
00353 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
00354
00355
00356
00357
00358
00359
00360
00361
00362 check(param=cpl_parameterlist_find(parlist,"xsh.xsh_util_efficiency.airmass"));
00363 check(airmass=cpl_parameter_get_double(param));
00364
00365
00366 check(param=cpl_parameterlist_find(parlist,"xsh.xsh_util_efficiency.gain"));
00367 check(gain=cpl_parameter_get_double(param));
00368
00369
00370
00371 check(frm_sci=cpl_frameset_get_first(frames));
00372 check(frm_ref=cpl_frameset_get_next(frames));
00373 check(frm_atmext=cpl_frameset_get_next(frames));
00374
00375 check(name=cpl_frame_get_filename(frm_sci));
00376 check(ima_sci=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00377 check(plist=cpl_propertylist_load(name,0));
00378 check(crpix1=xsh_pfits_get_crpix1(plist));
00379 check(crval1=xsh_pfits_get_crval1(plist));
00380 check(cdelt1=xsh_pfits_get_cdelt1(plist));
00381 check(naxis1=xsh_pfits_get_naxis1(plist));
00382 check(exptime=xsh_pfits_get_exptime(plist));
00383 xsh_free_propertylist(&plist);
00384
00385 src_wmin=crval1;
00386 src_wmax=src_wmin+naxis1*cdelt1;
00387
00388 check(name=cpl_frame_get_filename(frm_ref));
00389 check(tbl_ref=cpl_table_load(name,1,0));
00390
00391 check(name=cpl_frame_get_filename(frm_atmext));
00392 check(tbl_atmext=cpl_table_load(name,1,0));
00393
00394 cpl_table_divide_scalar(tbl_ref,"WAVELENGTH",10.);
00395 check(ref_wmin=cpl_table_get_column_min(tbl_ref,"WAVELENGTH"));
00396 check(ref_wmax=cpl_table_get_column_max(tbl_ref,"WAVELENGTH"));
00397
00398
00399 cpl_table_divide_scalar(tbl_atmext,"LAMBDA",10.);
00400 check(ext_wmin=cpl_table_get_column_min(tbl_atmext,"LAMBDA"));
00401 check(ext_wmax=cpl_table_get_column_max(tbl_atmext,"LAMBDA"));
00402 nrow=cpl_table_get_nrow(tbl_atmext);
00403 ext_wdel=(ext_wmax-ext_wmin)/(nrow-1);
00404 xsh_msg("wdel=%g",ext_wdel);
00405 wmin=(src_wmin>ref_wmin) ? src_wmin : ref_wmin;
00406 wmax=(src_wmax<ref_wmax) ? src_wmax : ref_wmax;
00407 xsh_msg("eff wave min: %g max: %g",wmin,wmax);
00408
00409 wmin=(ext_wmin>wmin) ? ext_wmin : wmin;
00410 wmax=(ext_wmax<wmax) ? ext_wmax : wmax;
00411
00412 xsh_msg("ref wave min: %g max: %g",ref_wmin,ref_wmax);
00413 xsh_msg("src wave min: %g max: %g",src_wmin,src_wmax);
00414 xsh_msg("ext wave min: %g max: %g",ext_wmin,ext_wmax);
00415 xsh_msg("eff wave min: %g max: %g",wmin,wmax);
00416
00417 check(cpl_table_and_selected_float(tbl_ref,"WAVELENGTH",CPL_NOT_LESS_THAN,wmin));
00418 check(cpl_table_and_selected_float(tbl_ref,"WAVELENGTH",CPL_NOT_GREATER_THAN,wmax));
00419 check(tbl_ext=cpl_table_extract_selected(tbl_ref));
00420 check(ref_wmin=cpl_table_get_column_min(tbl_ext,"WAVELENGTH"));
00421 check(ref_wmax=cpl_table_get_column_max(tbl_ext,"WAVELENGTH"));
00422 xsh_msg("sel wave min: %g max: %g",ref_wmin,ref_wmax);
00423
00424 check(nrow=cpl_table_get_nrow(tbl_ext));
00425
00426 check(cpl_table_new_column(tbl_ext,"SRC",CPL_TYPE_FLOAT));
00427 check(cpl_table_fill_column_window_float(tbl_ext,"SRC",0,nrow,-1));
00428
00429 check(cpl_table_new_column(tbl_ext,"REF",CPL_TYPE_FLOAT));
00430 check(cpl_table_fill_column_window_float(tbl_ext,"REF",0,nrow,-1));
00431
00432 check(cpl_table_new_column(tbl_ext,"EXT",CPL_TYPE_FLOAT));
00433 check(cpl_table_fill_column_window_float(tbl_ext,"EXT",0,nrow,-1));
00434
00435 check(cpl_table_new_column(tbl_ext,"COR",CPL_TYPE_FLOAT));
00436 check(cpl_table_fill_column_window_float(tbl_ext,"COR",0,nrow,-1));
00437
00438 check(cpl_table_new_column(tbl_ext,"EPHOT",CPL_TYPE_FLOAT));
00439 check(cpl_table_fill_column_window_float(tbl_ext,"EPHOT",0,nrow,-1));
00440
00441
00442 check(psrc=cpl_table_get_data_float(tbl_ext,"SRC"));
00443 check(pref=cpl_table_get_data_float(tbl_ext,"REF"));
00444 check(pext=cpl_table_get_data_float(tbl_ext,"EXT"));
00445 check(pcor=cpl_table_get_data_float(tbl_ext,"COR"));
00446 check(peph=cpl_table_get_data_float(tbl_ext,"EPHOT"));
00447 check(peph=cpl_table_get_data_float(tbl_ext,"EPHOT"));
00448
00449 check(pwav=cpl_table_get_data_float(tbl_ext,"WAVELENGTH"));
00450 check( rec_profile = cpl_vector_new( CPL_KERNEL_DEF_SAMPLES ) ) ;
00451 check( cpl_vector_fill_kernel_profile( rec_profile,
00452 CPL_KERNEL_DEFAULT,
00453 1 ) ) ;
00454 for(i=0;i<nrow;i++) {
00455 x=(pwav[i]-src_wmin)/cdelt1;
00456
00457 check(psrc[i]=cpl_image_get_interpolated( ima_sci,x, 1,
00458 rec_profile,
00459 1,
00460 rec_profile,
00461 1,
00462 &confidence ) ) ;
00463
00464
00465
00466
00467 check(pext[i]=xsh_table_get_interpolated(tbl_atmext,pwav[i],"LAMBDA",
00468 XSH_ATMOS_EXT_LIST_COLNAME_K));
00469
00470 check(pref[i]=xsh_table_get_interpolated(tbl_ref,pwav[i],
00471 "WAVELENGTH","FLUX"));
00472
00473 pcor[i]=pow(10,(0.4*pext[i]*aimprim));
00474 if(airmass != 0) pcor[i]/=pow(10,(0.4*pext[i]*airmass));
00475 peph[i]=1.986e-16/(pwav[i]*1e-8);
00476
00477 }
00478
00479 check(cpl_table_duplicate_column(tbl_ext,"SRC_COR",tbl_ext,"SRC"));
00480 check(cpl_table_multiply_columns(tbl_ext,"SRC_COR","COR"));
00481 check(cpl_table_duplicate_column(tbl_ext,"EFF",tbl_ext,"SRC_COR"));
00482
00483 check(cpl_table_multiply_scalar(tbl_ext,"EFF",gain));
00484 check(cpl_table_multiply_columns(tbl_ext,"EFF","EPHOT"));
00485 check(cpl_table_divide_scalar(tbl_ext,"EFF",exptime));
00486 check(cpl_table_divide_scalar(tbl_ext,"EFF",TEL_AREA));
00487 check(cpl_table_divide_columns(tbl_ext,"EFF","REF"));
00488
00489 check(cpl_table_save(tbl_ext,NULL,NULL,"tab.fits",CPL_IO_DEFAULT));
00490 cleanup:
00491 xsh_free_propertylist(&plist);
00492 xsh_free_image(&ima_sci);
00493 xsh_free_table(&tbl_ref);
00494 xsh_free_table(&tbl_ext);
00495 xsh_free_table(&tbl_atmext);
00496 xsh_free_vector(&rec_profile);
00497 if (cpl_error_get_code()) {
00498 return -1 ;
00499 } else {
00500 return 0 ;
00501 }
00502
00503 }