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 <cpl.h>
00037 
00038 #include "irplib_utils.h"
00039 
00040 #include "isaac_utils.h"
00041 #include "isaac_pfits.h"
00042 #include "isaac_dfs.h"
00043 
00044 
00045 
00046 
00047 
00048 static int isaac_util_genlines_create(cpl_plugin *);
00049 static int isaac_util_genlines_exec(cpl_plugin *);
00050 static int isaac_util_genlines_destroy(cpl_plugin *);
00051 static int isaac_util_genlines(cpl_parameterlist *, cpl_frameset *);
00052 static int isaac_util_genlines_save(cpl_table *, cpl_parameterlist *, 
00053         cpl_frameset *);
00054 
00055 
00056 
00057 
00058 
00059 static struct {
00060     
00061     int             fill_blanks; 
00062     int             display;
00063     int             mode;
00064     double          wl_factor;
00065     
00066 } isaac_util_genlines_config;
00067 
00068 static char isaac_util_genlines_description[] =
00069 "This recipe is used to generate spectrum calibration tables.\n"
00070 "The sof file contains the names of the input ASCII file\n"
00071 "tagged with "ISAAC_UTIL_GENLINES_RAW".\n"
00072 "The ASCII file must contain two columns:\n"
00073 "1st: Wavelengths in increasing order (the unit is corrected by\n"
00074 "     the factor option to obtain nanometers).\n"
00075 "2nd: The atmospheric emission.\n"
00076 "The ASCII files are in the catalogs/ directory of the ISAAC distribution.\n"
00077 "This recipe produces 1 file:\n"
00078 "First product:     the table with the lines.\n"
00079 "                   (PRO CATG = "ISAAC_UTIL_GENLINES_OH_CAT") or\n"
00080 "                   (PRO CATG = "ISAAC_UTIL_GENLINES_XE_CAT") or\n"
00081 "                   (PRO CATG = "ISAAC_UTIL_GENLINES_AR_CAT")\n";
00082 
00083 
00084 
00085 
00086 
00087 
00095 
00096 int cpl_plugin_get_info(cpl_pluginlist * list)
00097 {
00098     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00099     cpl_plugin  *   plugin = &recipe->interface;
00100 
00101     cpl_plugin_init(plugin,
00102                     CPL_PLUGIN_API,
00103                     ISAAC_BINARY_VERSION,
00104                     CPL_PLUGIN_TYPE_RECIPE,
00105                     "isaac_util_genlines",
00106                     "Generate spectrum calibration FITS tables",
00107                     isaac_util_genlines_description,
00108                     "Lars Lundin",
00109                     PACKAGE_BUGREPORT,
00110                     isaac_get_license(),
00111                     isaac_util_genlines_create,
00112                     isaac_util_genlines_exec,
00113                     isaac_util_genlines_destroy);
00114 
00115     cpl_pluginlist_append(list, plugin);
00116     
00117     return 0;
00118 }
00119 
00120 
00129 
00130 static int isaac_util_genlines_create(cpl_plugin * plugin)
00131 {
00132     cpl_recipe      * recipe;
00133     cpl_parameter   * p;
00134 
00135     
00136     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00137         recipe = (cpl_recipe *)plugin;
00138     else return -1;
00139 
00140     
00141     recipe->parameters = cpl_parameterlist_new();
00142 
00143     
00144     
00145     p = cpl_parameter_new_value("isaac.isaac_util_genlines.fill_blanks",
00146             CPL_TYPE_BOOL, "flag to fill blanks", "isaac.isaac_util_genlines",
00147             FALSE);
00148     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fill_blanks");
00149     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00150     cpl_parameterlist_append(recipe->parameters, p);
00151     
00152     p = cpl_parameter_new_value("isaac.isaac_util_genlines.display",
00153             CPL_TYPE_BOOL, "flag to plot", "isaac.isaac_util_genlines",
00154             FALSE);
00155     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "display");
00156     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00157     cpl_parameterlist_append(recipe->parameters, p);
00158     
00159     p = cpl_parameter_new_value("isaac.isaac_util_genlines.mode",
00160             CPL_TYPE_INT, "1-OH, 2-XE, 3-AR", 
00161             "isaac.isaac_util_genlines", 1);
00162     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mode");
00163     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00164     cpl_parameterlist_append(recipe->parameters, p);
00165     
00166     p = cpl_parameter_new_value("isaac.isaac_util_genlines.wl_factor",
00167             CPL_TYPE_DOUBLE, "The factor used to multiply the wl", 
00168             "isaac.isaac_util_genlines", 1.0);
00169     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wl_factor");
00170     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00171     cpl_parameterlist_append(recipe->parameters, p);
00172  
00173     return 0;
00174 }
00175 
00176 
00182 
00183 static int isaac_util_genlines_exec(cpl_plugin * plugin)
00184 {
00185     cpl_recipe  *   recipe;
00186 
00187     
00188     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00189         recipe = (cpl_recipe *)plugin;
00190     else return -1;
00191 
00192     return isaac_util_genlines(recipe->parameters, recipe->frames);
00193 }
00194 
00195 
00201 
00202 static int isaac_util_genlines_destroy(cpl_plugin * plugin)
00203 {
00204     cpl_recipe  *   recipe;
00205 
00206     
00207     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00208         recipe = (cpl_recipe *)plugin;
00209     else return -1;
00210 
00211     cpl_parameterlist_delete(recipe->parameters);
00212     return 0;
00213 }
00214 
00215 
00224 
00225 static int isaac_util_genlines(
00226         cpl_parameterlist   *   parlist,
00227         cpl_frameset        *   framelist)
00228 {
00229     cpl_bivector    *   bivec;
00230     double          *   pbivec_x;
00231     double          *   pbivec_y;
00232     cpl_bivector    *   bivec_fill;
00233     double          *   pbivec_fill_x;
00234     double          *   pbivec_fill_y;
00235     cpl_frame       *   cur_frame;
00236     int                 nvals, nb_new_vals;
00237     double              wavel;
00238     cpl_table       *   tab;
00239     cpl_parameter   *   par;
00240     int                 i;
00241 
00242     
00243     par = NULL;
00244     
00245     
00246     
00247     par = cpl_parameterlist_find(parlist, 
00248             "isaac.isaac_util_genlines.fill_blanks");
00249     isaac_util_genlines_config.fill_blanks = cpl_parameter_get_bool(par);
00250     
00251     par = cpl_parameterlist_find(parlist,"isaac.isaac_util_genlines.display");
00252     isaac_util_genlines_config.display = cpl_parameter_get_bool(par);
00253     
00254     par = cpl_parameterlist_find(parlist,"isaac.isaac_util_genlines.mode");
00255     isaac_util_genlines_config.mode = cpl_parameter_get_int(par);
00256     
00257     par=cpl_parameterlist_find(parlist,"isaac.isaac_util_genlines.wl_factor");
00258     isaac_util_genlines_config.wl_factor = cpl_parameter_get_double(par);
00259    
00260     
00261     if (isaac_dfs_set_groups(framelist)) {
00262         cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
00263         return -1;
00264     }
00265    
00266     
00267     cur_frame = cpl_frameset_get_frame(framelist, 0);
00268     if ((bivec=cpl_bivector_read(cpl_frame_get_filename(cur_frame)))==NULL) {
00269         cpl_msg_error(cpl_func, "Cannot load the file in the bivector");
00270         return -1;
00271     }
00272     nvals = cpl_bivector_get_size(bivec);
00273     
00274      
00275     if (isaac_util_genlines_config.fill_blanks) {
00276         nb_new_vals = 3 * nvals;
00277         bivec_fill = cpl_bivector_new(nb_new_vals);
00278         pbivec_fill_x = cpl_bivector_get_x_data(bivec_fill);
00279         pbivec_fill_y = cpl_bivector_get_y_data(bivec_fill);
00280         pbivec_x = cpl_bivector_get_x_data(bivec);
00281         pbivec_y = cpl_bivector_get_y_data(bivec);
00282         for (i=0; i<nvals; i++) {
00283             wavel = pbivec_x[i] * isaac_util_genlines_config.wl_factor;
00284             pbivec_fill_x[3*i] = wavel - 0.01;
00285             pbivec_fill_y[3*i] = 0.0;
00286             pbivec_fill_x[3*i+1] = wavel;
00287             pbivec_fill_y[3*i+1] = pbivec_y[i];
00288             pbivec_fill_x[3*i+2] = wavel + 0.01;
00289             pbivec_fill_y[3*i+2] = 0.0;
00290         }
00291         cpl_bivector_delete(bivec);
00292         bivec = bivec_fill;
00293         bivec_fill = NULL;
00294         nvals = cpl_bivector_get_size(bivec);
00295     } else {
00296         cpl_vector_multiply_scalar(cpl_bivector_get_x(bivec),
00297                 isaac_util_genlines_config.wl_factor);
00298     }
00299 
00300     
00301     if (isaac_util_genlines_config.display) {
00302         cpl_plot_bivector(
00303                 "set grid;set xlabel 'Wavelength (A)';set ylabel 'Emission';",
00304                 "t 'Catalog lines' w lines", "", bivec);
00305     }
00306     
00307     
00308     tab = cpl_table_new(nvals);
00309     cpl_table_wrap_double(tab, cpl_bivector_get_x_data(bivec),
00310             ISAAC_COL_WAVELENGTH);
00311     cpl_table_wrap_double(tab, cpl_bivector_get_y_data(bivec), 
00312             ISAAC_COL_EMISSION);
00313    
00314     
00315     cpl_msg_info(cpl_func, "Saving the table with %d rows", nvals);
00316     if (isaac_util_genlines_save(tab, parlist, framelist) == -1) {
00317         cpl_msg_error(cpl_func, "Cannot write the table");
00318         cpl_bivector_delete(bivec);
00319         cpl_table_unwrap(tab, ISAAC_COL_WAVELENGTH);
00320         cpl_table_unwrap(tab, ISAAC_COL_EMISSION);
00321         cpl_table_delete(tab);
00322         return -1;
00323     }
00324     cpl_bivector_delete(bivec);
00325     cpl_table_unwrap(tab, ISAAC_COL_WAVELENGTH);
00326     cpl_table_unwrap(tab, ISAAC_COL_EMISSION);
00327     cpl_table_delete(tab);
00328     return 0;
00329 }
00330 
00331 
00339 
00340 static int isaac_util_genlines_save(
00341         cpl_table           *   out_table,
00342         cpl_parameterlist   *   parlist,
00343         cpl_frameset        *   set)
00344 {
00345     char                    name_o[512];
00346     cpl_propertylist    *   plist;
00347     cpl_frame           *   product_frame;
00348 
00349     
00350     sprintf(name_o, "isaac_util_genlines_save.fits");
00351     cpl_msg_info(cpl_func, "Writing %s" , name_o);
00352 
00353     
00354     plist = cpl_propertylist_new();
00355     cpl_propertylist_append_string(plist, "INSTRUME", "ISAAC");
00356             
00357     
00358     product_frame = cpl_frame_new();
00359     cpl_frame_set_filename(product_frame, name_o);
00360     if (isaac_util_genlines_config.mode == 1) 
00361         cpl_frame_set_tag(product_frame, ISAAC_UTIL_GENLINES_OH_CAT);
00362     else if (isaac_util_genlines_config.mode == 2)
00363         cpl_frame_set_tag(product_frame, ISAAC_UTIL_GENLINES_XE_CAT);
00364     else if (isaac_util_genlines_config.mode == 3)
00365         cpl_frame_set_tag(product_frame, ISAAC_UTIL_GENLINES_AR_CAT);
00366     else 
00367         cpl_frame_set_tag(product_frame, "UNKNOWN");
00368 
00369     cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
00370     cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
00371     cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
00372 
00373     
00374     if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00375             "isaac_util_genlines", PACKAGE "/" PACKAGE_VERSION,
00376         "PRO-1.15", NULL)!=CPL_ERROR_NONE) {
00377         cpl_msg_warning(cpl_func, "Problem in the product DFS-compliance");
00378         cpl_error_reset();
00379     }
00380 
00381     
00382     if (cpl_table_save(out_table, plist, NULL, name_o,
00383                 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00384         cpl_msg_error(cpl_func, "Cannot save the product");
00385         cpl_frame_delete(product_frame);
00386         cpl_propertylist_delete(plist);
00387         return -1;
00388     }
00389     cpl_propertylist_delete(plist);
00390 
00391     
00392     cpl_frameset_insert(set, product_frame);
00393     
00394     return 0;
00395 }