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 "visir_recipe.h"
00037
00038 #include <string.h>
00039
00040
00041
00042
00043
00044 #define RECIPE_STRING "visir_util_spc_txt2fits"
00045
00046 #ifndef FLEN_VALUE
00047
00048 #define FLEN_VALUE 71
00049 #endif
00050
00051
00052
00053
00054 static cpl_error_code visir_util_spc_txt2fits_save(cpl_frameset *,
00055 const cpl_parameterlist *,
00056 const cpl_propertylist *,
00057 const char *,
00058 const cpl_table *);
00059
00060 static cpl_error_code visir_propertylist_fill_comments(cpl_propertylist *,
00061 FILE *);
00062
00063 VISIR_RECIPE_DEFINE(visir_util_spc_txt2fits, VISIR_PARAM_QEFF,
00064 "Generate spectrum calibration FITS tables",
00065 "This recipe shall be used to generate spectrum "
00066 "calibration tables.\n"
00067 "The sof file shall consist of 1 line with the name of an "
00068 "ASCII-file\n"
00069 "currently tagged with either " VISIR_SPC_LINES_ASCII " or "
00070 VISIR_SPC_QEFF_ASCII ".\n"
00071 "The file must comprise two columns:\n"
00072 "1st: Must be wavelengths in increasing order in units "
00073 "of meter\n"
00074 "2nd: For " VISIR_SPC_LINES_ASCII "-files must be the "
00075 "atmospheric emission, "
00076 "while\n"
00077 " for " VISIR_SPC_QEFF_ASCII "-files must be the "
00078 "quantum efficiency of "
00079 "the detector.\n"
00080 "A " VISIR_SPC_LINES_ASCII "-file will generate a "
00081 VISIR_CALIB_LINES_SPC
00082 "-file, and \n"
00083 "a " VISIR_SPC_QEFF_ASCII "-file will generate a "
00084 VISIR_CALIB_QEFF_SPC
00085 "-file.\n"
00086 "The current " VISIR_CALIB_LINES_SPC "- and "
00087 VISIR_CALIB_QEFF_SPC
00088 "-files are\n"
00089 "generated using the ASCII-files in the catalogs/ "
00090 "directory of the VISIR\n"
00091 "source-code distribution.");
00092
00093
00097
00098
00099
00100
00101
00102
00103
00113
00114 static int visir_util_spc_txt2fits(cpl_frameset * framelist,
00115 const cpl_parameterlist * parlist)
00116 {
00117 const char * label2 = NULL;
00118 const char * procat;
00119 cpl_propertylist * qclist = cpl_propertylist_new();
00120 irplib_framelist * allframes = NULL;
00121 irplib_framelist * rawframes = NULL;
00122 cpl_bivector * spectrum = NULL;
00123 cpl_table * tab = NULL;
00124 FILE * stream = NULL;
00125 int nframes;
00126 int i, nvals;
00127
00128
00129 skip_if (visir_dfs_set_groups(framelist));
00130
00131
00132 allframes = irplib_framelist_cast(framelist);
00133 skip_if(allframes == NULL);
00134 rawframes = irplib_framelist_extract_regexp(allframes, "^("
00135 VISIR_SPC_LINES_ASCII "|"
00136 VISIR_SPC_QEFF_ASCII ")$",
00137 CPL_FALSE);
00138 skip_if (rawframes == NULL);
00139
00140 nframes = irplib_framelist_get_size(rawframes);
00141
00142
00143 for (i = 0; i < nframes; i++) {
00144 const cpl_frame * frame = irplib_framelist_get_const(rawframes, i);
00145 const char * filename = cpl_frame_get_filename(frame);
00146 const char * basepos;
00147 const char * dotpos;
00148 char extname[FLEN_VALUE];
00149 size_t baselen;
00150
00151 skip_if(filename == NULL);
00152 cpl_propertylist_empty(qclist);
00153
00154 basepos = strrchr(filename, '/');
00155 basepos = basepos == NULL ? filename : basepos + 1;
00156 dotpos = strrchr(basepos, '.');
00157 baselen = dotpos == NULL ? strlen(basepos) : (size_t)(dotpos - basepos);
00158 if (baselen > 0) {
00159 strncpy(extname, basepos, CX_MIN(FLEN_VALUE, baselen));
00160 extname[CX_MIN(FLEN_VALUE - 1, baselen)] = '\0';
00161
00162 skip_if(cpl_propertylist_append_string(qclist, "EXTNAME", extname));
00163 }
00164
00165 if (stream != NULL) fclose(stream);
00166 stream = fopen(filename, "r");
00167
00168 skip_if (stream == NULL);
00169
00170 cpl_bivector_delete(spectrum);
00171 spectrum = cpl_bivector_new(2481);
00172 skip_if( visir_bivector_load(spectrum, stream));
00173
00174 if (strcmp(cpl_frame_get_tag(frame), VISIR_SPC_LINES_ASCII)) {
00175
00176
00177 procat = VISIR_SPEC_CAL_QEFF_PROCATG;
00178 label2 = "Efficiency";
00179
00180 skip_if(cpl_propertylist_set_comment(qclist, "EXTNAME", "Match "
00181 "with HIERARCH.ESO."
00182 "DET.CHIP.NAME"));
00183
00184
00185 } else {
00186
00187 procat = VISIR_SPEC_CAL_LINES_PROCATG;
00188 label2 = "Emission";
00189 }
00190
00191
00192 nvals = cpl_bivector_get_size(spectrum);
00193
00194
00195 if (tab != NULL) {
00196 cpl_table_unwrap(tab, "Wavelength");
00197 cpl_table_unwrap(tab, label2);
00198 cpl_table_delete(tab);
00199 }
00200 tab = cpl_table_new(nvals);
00201
00202 skip_if( tab == NULL );
00203
00204 skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_x_data(spectrum),
00205 "Wavelength"));
00206 skip_if (cpl_table_set_column_unit(tab, "Wavelength", "m"));
00207
00208 skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_y_data(spectrum),
00209 label2));
00210
00211 skip_if (cpl_table_set_column_unit(tab, label2, "[0;1] (Unitless)"));
00212
00213 skip_if(visir_propertylist_fill_comments(qclist, stream));
00214
00215
00216 cpl_msg_info(cpl_func, "Saving the table with %d rows", nvals);
00217 if (i == 0) {
00218
00219 skip_if (visir_util_spc_txt2fits_save(framelist, parlist, qclist,
00220 procat, tab));
00221 } else {
00222 skip_if (cpl_table_save(tab, NULL, qclist, RECIPE_STRING
00223 CPL_DFS_FITS, CPL_IO_EXTEND));
00224 }
00225 }
00226
00227 end_skip;
00228
00229 if (stream != NULL) fclose(stream);
00230 cpl_bivector_delete(spectrum);
00231 if (tab) {
00232 cpl_table_unwrap(tab, "Wavelength");
00233 cpl_table_unwrap(tab, label2);
00234 cpl_table_delete(tab);
00235 }
00236 irplib_framelist_delete(allframes);
00237 irplib_framelist_delete(rawframes);
00238 cpl_propertylist_delete(qclist);
00239
00240 return cpl_error_get_code();
00241 }
00242
00243
00255
00256 static
00257 cpl_error_code visir_util_spc_txt2fits_save(cpl_frameset * set,
00258 const cpl_parameterlist * parlist,
00259 const cpl_propertylist * qclist,
00260 const char * procat,
00261 const cpl_table * tab)
00262 {
00263
00264 cpl_propertylist * applist = cpl_propertylist_new();
00265
00266
00267 bug_if(cpl_propertylist_append_string(applist, "INSTRUME", "VISIR"));
00268
00269 skip_if (irplib_dfs_save_table(set, parlist, set, tab, qclist, RECIPE_STRING,
00270 procat, applist, NULL, visir_pipe_id,
00271 RECIPE_STRING CPL_DFS_FITS));
00272
00273 end_skip;
00274
00275 cpl_propertylist_delete(applist);
00276
00277 return cpl_error_get_code();
00278 }
00279
00280
00281
00291
00292 static cpl_error_code visir_propertylist_fill_comments(cpl_propertylist * self,
00293 FILE * stream)
00294 {
00295
00296 bug_if(self == NULL);
00297 bug_if(stream == NULL);
00298
00299 if (!fseek(stream, 0, SEEK_SET)) {
00300
00301 char line[1024];
00302
00303 while (fgets(line, 1024, stream) != NULL) {
00304
00305 if (line[0] == '#') {
00306 char value[FLEN_VALUE];
00307 strncpy(value, line+1, FLEN_VALUE);
00308 value[FLEN_VALUE - 1] = '\0';
00309 cpl_propertylist_append_string(self, "COMMENT", value);
00310 }
00311 }
00312 }
00313
00314 end_skip;
00315
00316 return cpl_error_get_code();
00317 }