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
00039
00041
00042
00043
00044
00045 #include <cpl.h>
00046
00047 #include "xsh_utils.h"
00048 #include "xsh_pfits.h"
00049 #include "xsh_dfs.h"
00050
00051
00052
00053
00054
00055 static int xsh_util_genconfig_create(cpl_plugin *);
00056 static int xsh_util_genconfig_exec(cpl_plugin *);
00057 static int xsh_util_genconfig_destroy(cpl_plugin *);
00058 static int xsh_util_genconfig(cpl_parameterlist *, cpl_frameset *);
00059 static int xsh_util_genconfig_save(cpl_table *, cpl_parameterlist *,
00060 cpl_frameset *);
00061
00062
00063
00064
00065
00066 static char xsh_util_genconfig_description[] =
00067 "This recipe is used to generate the model configuration file.\n"
00068 "The sof file contains the names of the input ASCII file\n"
00069 "tagged with "XSH_UTIL_GENCONFIG_RAW".\n"
00070 "The ASCII file must contain five columns:\n"
00071 "1st: The best guess value\n"
00072 "2nd: The low limit\n"
00073 "3th: The high limit\n"
00074 "4th: The Flag to recompute or not\n"
00075 "5th: Name of the Parameter\n"
00076 "The ASCII files are in the catalogs/ directory of the XSH distribution.\n"
00077 "This recipe produces 1 file:\n"
00078 "First product: the table with the configuration for the model.\n"
00079 " (PRO CATG = XSH_MOD_CFG_TAB_ARM)\n" ;
00080
00081
00082
00083
00084
00085
00093
00094 int cpl_plugin_get_info(cpl_pluginlist * list)
00095 {
00096 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe));
00097 cpl_plugin * plugin = &recipe->interface;
00098
00099 cpl_plugin_init(plugin,
00100 CPL_PLUGIN_API,
00101 XSH_BINARY_VERSION,
00102 CPL_PLUGIN_TYPE_RECIPE,
00103 "xsh_util_genconfig",
00104 "Generate spectrum calibration FITS tables",
00105 xsh_util_genconfig_description,
00106 "Yves Jung",
00107 "yjung@eso.org",
00108 xsh_get_license(),
00109 xsh_util_genconfig_create,
00110 xsh_util_genconfig_exec,
00111 xsh_util_genconfig_destroy);
00112
00113 cpl_pluginlist_append(list, plugin);
00114
00115 return 0 ;
00116 }
00117
00118
00127
00128 static int xsh_util_genconfig_create(cpl_plugin * plugin)
00129 {
00130 cpl_recipe * recipe ;
00131
00132
00133 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00134 recipe = (cpl_recipe *)plugin ;
00135 else return -1 ;
00136
00137
00138 recipe->parameters = cpl_parameterlist_new() ;
00139
00140 return 0 ;
00141 }
00142
00143
00149
00150 static int xsh_util_genconfig_exec(cpl_plugin * plugin)
00151 {
00152 cpl_recipe * recipe ;
00153
00154
00155 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00156 recipe = (cpl_recipe *)plugin ;
00157 else return -1 ;
00158
00159 return xsh_util_genconfig(recipe->parameters, recipe->frames) ;
00160 }
00161
00162
00168
00169 static int xsh_util_genconfig_destroy(cpl_plugin * plugin)
00170 {
00171 cpl_recipe * recipe ;
00172
00173
00174 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00175 recipe = (cpl_recipe *)plugin ;
00176 else return -1 ;
00177
00178 cpl_parameterlist_delete(recipe->parameters) ;
00179 return 0 ;
00180 }
00181
00182
00190
00191 static int xsh_util_genconfig(
00192 cpl_parameterlist * parlist,
00193 cpl_frameset * framelist)
00194 {
00195 FILE * in ;
00196 char line[1024];
00197 cpl_frame * cur_frame ;
00198 const char * cur_fname ;
00199 int nentries ;
00200 char name[1024] ;
00201 double best, low, high ;
00202 int flag ;
00203 cpl_table * tab ;
00204 int i ;
00205
00206
00207
00208
00209
00210
00211
00212
00213 cur_frame = cpl_frameset_get_frame(framelist, 0) ;
00214 cur_fname = cpl_frame_get_filename(cur_frame) ;
00215
00216
00217 if ((in = fopen(cur_fname, "r")) == NULL) {
00218 cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
00219 return -1 ;
00220 }
00221
00222
00223 nentries = 0 ;
00224 while (fgets(line, 1024, in) != NULL) {
00225 if (line[0] != '#' && sscanf(line, "%lg %lg %lg %d %s",
00226 &best, &low, &high, &flag, name) == 5) nentries++ ;
00227 }
00228 if (nentries == 0) {
00229 cpl_msg_error(__func__, "No valid entryin the file") ;
00230 fclose(in) ;
00231 return -1 ;
00232 }
00233
00234
00235 tab = cpl_table_new(nentries) ;
00236 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_BEST, CPL_TYPE_DOUBLE) ;
00237 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_LOW, CPL_TYPE_DOUBLE) ;
00238 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_HIGH, CPL_TYPE_DOUBLE) ;
00239 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_FLAG, CPL_TYPE_INT) ;
00240 cpl_table_new_column(tab, XSH_COL_MODEL_CONF_NAME, CPL_TYPE_STRING) ;
00241
00242
00243 i = 0 ;
00244 rewind(in) ;
00245 while (fgets(line, 1024, in) != NULL) {
00246 if (line[0] != '#' && sscanf(line, "%lg %lg %lg %d %s",
00247 &best, &low, &high, &flag, name) == 5) {
00248 cpl_table_set_string(tab, XSH_COL_MODEL_CONF_NAME, i, name) ;
00249 cpl_table_set_double(tab, XSH_COL_MODEL_CONF_BEST, i, best) ;
00250 cpl_table_set_double(tab, XSH_COL_MODEL_CONF_LOW, i, low) ;
00251 cpl_table_set_double(tab, XSH_COL_MODEL_CONF_HIGH, i, high) ;
00252 cpl_table_set_int(tab, XSH_COL_MODEL_CONF_FLAG, i, flag) ;
00253 i++ ;
00254 }
00255 }
00256 fclose(in) ;
00257
00258
00259 cpl_msg_info(__func__, "Saving the table with %d rows", nentries) ;
00260 if (xsh_util_genconfig_save(tab, parlist, framelist) == -1) {
00261 cpl_msg_error(__func__, "Cannot write the table") ;
00262 cpl_table_delete(tab) ;
00263 return -1 ;
00264 }
00265 cpl_table_delete(tab) ;
00266 return 0 ;
00267 }
00268
00269
00277
00278 static int xsh_util_genconfig_save(
00279 cpl_table * out_table,
00280 cpl_parameterlist * parlist,
00281 cpl_frameset * set)
00282 {
00283 char name_o[512] ;
00284 cpl_propertylist * plist ;
00285 cpl_frame * product_frame ;
00286
00287
00288
00289
00290
00291
00292
00293 sprintf(name_o, "xsh_util_genconfig_save.fits") ;
00294 cpl_msg_info(__func__, "Writing %s" , name_o) ;
00295
00296
00297 plist = cpl_propertylist_new();
00298 cpl_propertylist_append_string(plist, "INSTRUME", "XSH") ;
00299
00300
00301 product_frame = cpl_frame_new() ;
00302 cpl_frame_set_filename(product_frame, name_o) ;
00303 cpl_frame_set_tag(product_frame, XSH_MOD_CFG) ;
00304 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
00305 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
00306 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
00307
00308
00309 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00310 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00311 "xsh_util_genconfig", PACKAGE "/" PACKAGE_VERSION,
00312 "PRO-1.15",NULL)!=CPL_ERROR_NONE) {
00313 cpl_msg_warning(__func__, "Problem in the product DFS-compliance") ;
00314 cpl_error_reset() ;
00315 }
00316 #else
00317 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00318 "xsh_util_genconfig", PACKAGE "/" PACKAGE_VERSION,
00319 "PRO-1.15")!=CPL_ERROR_NONE) {
00320 cpl_msg_warning(__func__, "Problem in the product DFS-compliance") ;
00321 cpl_error_reset() ;
00322 }
00323 #endif
00324
00325 if (cpl_table_save(out_table, plist, NULL, name_o,
00326 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00327 cpl_msg_error(__func__, "Cannot save the product");
00328 cpl_frame_delete(product_frame) ;
00329 cpl_propertylist_delete(plist) ;
00330 return -1 ;
00331 }
00332 cpl_propertylist_delete(plist) ;
00333
00334
00335 cpl_frameset_insert(set, product_frame);
00336
00337 return 0 ;
00338 }