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