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 "crires_recipe.h"
00037
00038 #include "crires_photom.h"
00039
00040
00041
00042
00043
00044 #define RECIPE_STRING "crires_util_conversion"
00045
00046
00047
00048
00049
00050 static int crires_util_conversion_save(const cpl_table **,
00051 const cpl_parameterlist *, cpl_frameset *) ;
00052
00053 static char crires_util_conversion_description[] =
00054 "This recipe accepts 2 parameters:\n"
00055 "First parameter: the table of the extracted spectrum in wavelength.\n"
00056 " (PRO TYPE = "CRIRES_PROTYPE_SPEC_WL")\n"
00057 "Second parameter: the table with the standard star photospheric flux.\n"
00058 " (PRO TYPE = "CRIRES_PROTYPE_PHO_FLUX")\n"
00059 "\n"
00060 "This recipe produces 1 file:\n"
00061 "First product: the table with the conversion signal.\n"
00062 " (PRO TYPE = "CRIRES_PROTYPE_CONVERS")\n" ;
00063
00064 CRIRES_RECIPE_DEFINE(crires_util_conversion,
00065 CRIRES_PARAM_DISPLAY,
00066 "Compute the conversion factor",
00067 crires_util_conversion_description) ;
00068
00069
00070
00071
00072
00073 static struct {
00074
00075 int display ;
00076
00077 int win_mode ;
00078 double conv_med[CRIRES_NB_DETECTORS] ;
00079 double thro_med[CRIRES_NB_DETECTORS] ;
00080 } crires_util_conversion_config ;
00081
00082
00083
00084
00085
00086
00093
00094 static int crires_util_conversion(
00095 cpl_frameset * frameset,
00096 const cpl_parameterlist * parlist)
00097 {
00098 cpl_frame * fr ;
00099 const char * sval ;
00100 cpl_propertylist * plist ;
00101 cpl_table * ext_tab[CRIRES_NB_DETECTORS] ;
00102 cpl_table * std_flux ;
00103 cpl_bivector * std_biv ;
00104 int i, j ;
00105
00106
00107 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00108 crires_util_conversion_config.conv_med[i] = -1.0 ;
00109 crires_util_conversion_config.thro_med[i] = -1.0 ;
00110 ext_tab[i] = NULL ;
00111 }
00112
00113
00114 crires_util_conversion_config.display = crires_parameterlist_get_int(
00115 parlist, RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00116
00117
00118 if (crires_dfs_set_groups(frameset, NULL)) {
00119 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00120 return -1 ;
00121 }
00122
00123
00124 cpl_msg_info(__func__, "Second frame validity check") ;
00125 cpl_msg_indent_more() ;
00126 fr = cpl_frameset_get_frame(frameset, 1);
00127 if ((std_flux = crires_load_table_check(cpl_frame_get_filename(fr), 1,
00128 CRIRES_PROTYPE_PHO_FLUX, -1, -1, 0)) == NULL) {
00129 cpl_msg_error(__func__, "Second frame PRO TYPE must be %s",
00130 CRIRES_PROTYPE_PHO_FLUX) ;
00131 cpl_msg_indent_less() ;
00132 return -1 ;
00133 }
00134 cpl_msg_indent_less() ;
00135
00136
00137 cpl_msg_info(__func__, "Standard star photospheric flux retrieval") ;
00138 cpl_msg_indent_more() ;
00139 if ((std_biv = crires_photom_conv_get_star(std_flux,
00140 cpl_frame_get_filename(cpl_frameset_get_frame(frameset, 0))))==NULL) {
00141 cpl_msg_error(__func__, "Cannot find the star") ;
00142 cpl_msg_indent_less() ;
00143 cpl_table_delete(std_flux) ;
00144 return -1 ;
00145 }
00146 cpl_table_delete(std_flux) ;
00147 cpl_msg_indent_less() ;
00148
00149
00150 fr = cpl_frameset_get_frame(frameset, 0);
00151 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
00152 return -1 ;
00153 sval = crires_pfits_get_ncorrs(plist) ;
00154 if (!strcmp(sval, "FowlerNsampGRstWin")) {
00155 crires_util_conversion_config.win_mode = 1 ;
00156 } else {
00157 crires_util_conversion_config.win_mode = 0 ;
00158 }
00159 cpl_propertylist_delete(plist) ;
00160
00161
00162 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00163
00164 if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
00165 (crires_util_conversion_config.win_mode == 1)) {
00166 continue ;
00167 }
00168
00169 cpl_msg_info(__func__, "Conversion factor for chip %d", i+1) ;
00170
00171
00172 cpl_msg_info(__func__, "Load the extracted spectrum") ;
00173 cpl_msg_indent_more() ;
00174 fr = cpl_frameset_get_frame(frameset, 0);
00175 if ((ext_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr),
00176 i+1, CRIRES_PROTYPE_SPEC_WL, -1, -1, 0)) == NULL) {
00177 cpl_msg_indent_less() ;
00178 continue ;
00179 }
00180 cpl_msg_indent_less() ;
00181
00182
00183 cpl_msg_info(__func__, "Conversion factor computation") ;
00184 cpl_msg_indent_more() ;
00185 if (crires_photom_conv_engine(ext_tab[i], std_biv, i+1,
00186 crires_util_conversion_config.display==i+1) == -1) {
00187 cpl_msg_error(__func__, "Cannot compute conversion factor") ;
00188 cpl_msg_indent_less() ;
00189 cpl_bivector_delete(std_biv) ;
00190 for (j=0 ; j<i ; j++) {
00191 if (ext_tab[j] != NULL) cpl_table_delete(ext_tab[j]) ;
00192 }
00193 return -1 ;
00194 }
00195 cpl_msg_indent_less() ;
00196
00197
00198 crires_util_conversion_config.conv_med[i] =
00199 cpl_table_get_column_median(ext_tab[i], CRIRES_COL_CONVERSION_RECT);
00200 crires_util_conversion_config.thro_med[i] =
00201 cpl_table_get_column_median(ext_tab[i], CRIRES_COL_THROUGHPUT) ;
00202 }
00203 cpl_bivector_delete(std_biv) ;
00204
00205
00206 if (crires_util_conversion_config.win_mode == 1) {
00207 if (ext_tab[1] != NULL) {
00208 ext_tab[0] = cpl_table_duplicate(ext_tab[1]) ;
00209 cpl_table_set_size(ext_tab[0], 0) ;
00210 ext_tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(ext_tab[0]) ;
00211 }
00212 }
00213
00214
00215 cpl_msg_info(__func__, "Save the product") ;
00216 cpl_msg_indent_more() ;
00217 if (crires_util_conversion_save((const cpl_table **)ext_tab, parlist,
00218 frameset)) {
00219 cpl_msg_error(__func__, "Cannot save the product") ;
00220 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00221 if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00222 }
00223 cpl_msg_indent_less() ;
00224 return -1 ;
00225 }
00226 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00227 if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
00228 }
00229 cpl_msg_indent_less() ;
00230
00231
00232 if (cpl_error_get_code()) return -1 ;
00233 else return 0 ;
00234 }
00235
00236
00244
00245 static int crires_util_conversion_save(
00246 const cpl_table ** out,
00247 const cpl_parameterlist * parlist,
00248 cpl_frameset * set)
00249 {
00250 cpl_propertylist ** qclists ;
00251 const cpl_frame * ref_frame ;
00252 cpl_propertylist * inputlist ;
00253 const char * recipe_name = "crires_util_conversion" ;
00254 int i ;
00255
00256
00257 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00258
00259
00260 qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00261 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00262 qclists[i] = cpl_propertylist_new() ;
00263 cpl_propertylist_append_double(qclists[i], "ESO QC CONVMED",
00264 crires_util_conversion_config.conv_med[i]) ;
00265 cpl_propertylist_append_double(qclists[i], "ESO QC THROMED",
00266 crires_util_conversion_config.thro_med[i]) ;
00267
00268
00269 inputlist = cpl_propertylist_load_regexp(
00270 cpl_frame_get_filename(ref_frame), i+1,
00271 CRIRES_HEADER_EXT_FORWARD, 0) ;
00272 cpl_propertylist_copy_property_regexp(qclists[i], inputlist,
00273 CRIRES_HEADER_EXT_FORWARD, 0) ;
00274 cpl_propertylist_delete(inputlist) ;
00275 }
00276
00277
00278 crires_table_save(set,
00279 parlist,
00280 set,
00281 out,
00282 recipe_name,
00283 CRIRES_EXTRACT_CONV_TAB,
00284 CRIRES_PROTYPE_CONVERS,
00285 NULL,
00286 (const cpl_propertylist **)qclists,
00287 PACKAGE "/" PACKAGE_VERSION,
00288 "crires_util_conversion.fits") ;
00289
00290
00291 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00292 cpl_propertylist_delete(qclists[i]) ;
00293 }
00294 cpl_free(qclists) ;
00295 return 0;
00296 }
00297