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
00039
00040
00041
00042 #define RECIPE_STRING "visir_util_repack"
00043
00044 #define VISIR_DRS_CUMOFFSETX "ESO DRS CUMOFFSETX"
00045 #define VISIR_DRS_CUMOFFSETY "ESO DRS CUMOFFSETY"
00046 #define VISIR_DRS_CONAD "ESO DRS OUT1 CONAD"
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 static cpl_error_code visir_util_repack_one(cpl_frameset *,
00061 irplib_framelist *, int,
00062 const cpl_parameterlist *);
00063
00064 static cpl_error_code visir_util_repack_check(const cpl_image *,
00065 const cpl_imagelist *,
00066 const cpl_imagelist *);
00067
00068 VISIR_RECIPE_DEFINE(visir_util_repack, 0,
00069 "Conversion of raw CUBE2 images to on- and off-cubes",
00070 "The files listed in the Set Of Frames (sof-file) "
00071 "must be tagged:\n"
00072 "VISIR-CUBE2-raw-file.fits " VISIR_UTIL_REPACK_RAW
00073 "\nThe on-product(s) will have a FITS card\n"
00074 "'HIERARCH ESO PRO CATG' with a value of:\n"
00075 VISIR_UTIL_REPACK_ON_PROCATG
00076 "\nThe off-product(s) will have a FITS card\n"
00077 "'HIERARCH ESO PRO CATG' with a value of:\n"
00078 VISIR_UTIL_REPACK_OFF_PROCATG
00079 "Additionally, the recipe collapses the on- and off-cubes,"
00080 "these product(s) will have a FITS card\n"
00081 "'HIERARCH ESO PRO CATG' with a value of:\n"
00082 VISIR_UTIL_REPACK_MEAN_ON_PROCATG "and"
00083 VISIR_UTIL_REPACK_MEAN_OFF_PROCATG "respectively.");
00084
00085
00089
00090
00091
00092
00093
00094
00095
00102
00103 static int visir_util_repack(cpl_frameset * framelist,
00104 const cpl_parameterlist * parlist)
00105 {
00106 #ifdef _OPENMP
00107 cpl_errorstate cleanstate = cpl_errorstate_get();
00108 #endif
00109 cpl_error_code didfail = CPL_ERROR_NONE;
00110 irplib_framelist * allframes = NULL;
00111 irplib_framelist * rawframes = NULL;
00112 int i, n;
00113
00114
00115
00116 skip_if (visir_dfs_set_groups(framelist));
00117
00118
00119 allframes = irplib_framelist_cast(framelist);
00120 skip_if(allframes == NULL);
00121 rawframes = irplib_framelist_extract(allframes, VISIR_UTIL_REPACK_RAW);
00122 skip_if (rawframes == NULL);
00123
00124 n = irplib_framelist_get_size(rawframes);
00125 #ifdef _OPENMP
00126 #pragma omp parallel for private(i)
00127 #endif
00128 for (i = 0; i < n; i++) {
00129 if (!didfail) {
00130
00131
00132
00133
00134
00135
00136
00137
00138 if (visir_util_repack_one(framelist, rawframes, i, parlist)) {
00139 const cpl_error_code errori = cpl_error_set_where(cpl_func);
00140 #ifdef _OPENMP
00141
00142
00143 cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
00144 cpl_errorstate_set(cleanstate);
00145 #pragma omp critical(visir_util_repack)
00146 #endif
00147 didfail = errori;
00148 }
00149 }
00150 }
00151
00152 error_if(didfail, didfail, "Failed to repack cubes in %d frame(s)", n);
00153
00154 end_skip;
00155
00156 irplib_framelist_delete(allframes);
00157 irplib_framelist_delete(rawframes);
00158
00159 return cpl_error_get_code();
00160 }
00161
00162
00163
00172
00173 static cpl_error_code visir_util_repack_one(cpl_frameset * framelist,
00174 irplib_framelist * rawframes, int i,
00175 const cpl_parameterlist * parlist)
00176 {
00177
00178 const double conad = 1.1;
00179
00180 const int n = irplib_framelist_get_size(rawframes);
00181 const cpl_error_code code =
00182 irplib_framelist_load_propertylist(rawframes, i, 0, "^(NAXIS3|"
00183 VISIR_PFITS_INT_CHOP_NCYCLES "|"
00184 VISIR_PFITS_DOUBLE_CHOP_THROW "|"
00185 VISIR_PFITS_STRING_PIXSCALE "|"
00186 VISIR_PFITS_STRING_FRAME_TYPE "|"
00187 VISIR_PFITS_DOUBLE_CUMOFFSETX "|"
00188 VISIR_PFITS_DOUBLE_CUMOFFSETY "|"
00189 VISIR_PFITS_STRING_NODPOS ")$",
00190 CPL_FALSE);
00191 const cpl_propertylist * plist =
00192 irplib_framelist_get_propertylist_const(rawframes, i);
00193 cpl_frameset * products = cpl_frameset_new();
00194 cpl_frameset * usedframes = cpl_frameset_new();
00195 const cpl_frame * frame;
00196 cpl_imagelist * on = cpl_imagelist_new();
00197 cpl_imagelist * off = cpl_imagelist_new();
00198 cpl_image * sum = visir_load_cube2_split(on, off,
00199 rawframes, i);
00200 cpl_image * sumon = cpl_imagelist_collapse_create(on);
00201 cpl_image * sumoff = cpl_imagelist_collapse_create(off);
00202 cpl_propertylist * onlist = cpl_propertylist_new();
00203 cpl_propertylist * offlist = cpl_propertylist_new();
00204 const int nexp = cpl_imagelist_get_size(on);
00205 const char * nodpos = visir_pfits_get_nodpos(plist);
00206 char * onname = cpl_sprintf(RECIPE_STRING "_%s_on_%d"
00207 CPL_DFS_FITS, nodpos, 1+i);
00208 char * offname = cpl_sprintf(RECIPE_STRING "_%s_off_%d"
00209 CPL_DFS_FITS, nodpos, 1+i);
00210 char * sonname = cpl_sprintf(RECIPE_STRING "_%s_mean_on_%d"
00211 CPL_DFS_FITS, nodpos, 1+i);
00212 char * soffname = cpl_sprintf(RECIPE_STRING "_%s_mean_off_%d"
00213 CPL_DFS_FITS, nodpos, 1+i);
00214 const char * remregexp = "^(" VISIR_PFITS_DOUBLE_CUMOFFSETX
00215 "|" VISIR_PFITS_DOUBLE_CUMOFFSETY ")$";
00216
00217
00218 const char * sscale = visir_pfits_get_pixscale(plist);
00219 const double pscale = sscale ? atof(sscale) : 0.0;
00220 const double pthrow = pscale > 0.0
00221 ? visir_pfits_get_chop_throw(plist) / pscale : 0.0;
00222
00223 const char * comoffx = cpl_propertylist_get_comment
00224 (plist, VISIR_PFITS_DOUBLE_CUMOFFSETX);
00225 const char * comoffy = cpl_propertylist_get_comment
00226 (plist, VISIR_PFITS_DOUBLE_CUMOFFSETY);
00227 const double offsetx = visir_pfits_get_cumoffsetx(plist);
00228 const double offsety = visir_pfits_get_cumoffsety(plist);
00229 cpl_image * bpm = i ? NULL :
00230 cpl_image_duplicate(cpl_imagelist_get(off, 0));
00231 const double bpmthresh = VISIR_HCYCLE_BPM_THRESHOLD
00232 - VISIR_HCYCLE_OFFSET;
00233
00234 skip_if(code);
00235
00236 cpl_msg_info(cpl_func, "Repacking %s-frame %d/%d ('%s'=%g)",
00237 cpl_type_get_name(cpl_image_get_type(sum)),
00238 1+i, n, VISIR_DRS_CUMOFFSETY, pthrow);
00239
00240 if (offsety != 0.0) {
00241 cpl_msg_warning(cpl_func, "Frame %d/%d has '%s' = %g != 0.0",
00242 1+i, n, VISIR_PFITS_DOUBLE_CUMOFFSETY, offsety);
00243 }
00244
00245 bug_if(cpl_propertylist_append_double(onlist, VISIR_DRS_CONAD, conad));
00246 bug_if(cpl_propertylist_append_double(onlist, VISIR_DRS_CUMOFFSETX,
00247 offsetx));
00248 bug_if(cpl_propertylist_append_double(onlist, VISIR_DRS_CUMOFFSETY,
00249 offsety));
00250 bug_if(cpl_propertylist_set_comment(onlist, VISIR_DRS_CUMOFFSETX,
00251 comoffx));
00252 bug_if(cpl_propertylist_set_comment(onlist, VISIR_DRS_CUMOFFSETY,
00253 comoffy));
00254 bug_if(cpl_propertylist_append(offlist, onlist));
00255 bug_if(cpl_propertylist_update_double(offlist, VISIR_DRS_CUMOFFSETY,
00256 offsety + pthrow));
00257 bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
00258 (irplib_framelist_get_const(rawframes, i))));
00259 skip_if(irplib_dfs_save_imagelist(products, parlist, usedframes, on,
00260 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
00261 "BASIC_CALIBRATED", onlist, remregexp,
00262 visir_pipe_id, onname));
00263 skip_if(irplib_dfs_save_imagelist(products, parlist, usedframes, off,
00264 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
00265 "BASIC_CALIBRATED", offlist,
00266 remregexp, visir_pipe_id, offname));
00267 bug_if(cpl_propertylist_update_double(offlist, VISIR_DRS_CONAD,
00268 nexp * conad));
00269 bug_if(cpl_propertylist_update_double(onlist, VISIR_DRS_CONAD,
00270 nexp * conad));
00271 skip_if(irplib_dfs_save_image(products, parlist, usedframes, sumon,
00272 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
00273 "BASIC_CALIBRATED", onlist,
00274 remregexp, visir_pipe_id, sonname));
00275 skip_if(irplib_dfs_save_image(products, parlist, usedframes, sumoff,
00276 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
00277 "BASIC_CALIBRATED", offlist, remregexp,
00278 visir_pipe_id, soffname));
00279 if (i == 0) {
00280 bug_if(cpl_image_threshold(bpm, bpmthresh, bpmthresh, 0.0, 1.0));
00281 skip_if(irplib_dfs_save_image(products, parlist, usedframes,bpm,
00282 CPL_BPP_8_UNSIGNED, RECIPE_STRING,
00283 "STATIC_MASK", NULL, remregexp,
00284 visir_pipe_id, RECIPE_STRING
00285 "_bpm" CPL_DFS_FITS));
00286 }
00287
00288 for (frame = cpl_frameset_get_first_const(products);
00289 frame != NULL;
00290 frame = cpl_frameset_get_next_const(products)) {
00291 cpl_frame * copy = cpl_frame_duplicate(frame);
00292 cpl_error_code error;
00293
00294 #ifdef _OPENMP
00295 #pragma omp critical(visir_util_repack_one)
00296 #endif
00297 error = cpl_frameset_insert(framelist, copy);
00298
00299 if (error) break;
00300 }
00301 bug_if(frame != NULL);
00302
00303 bug_if(visir_util_repack_check(sum, on, off));
00304
00305 end_skip;
00306
00307 cpl_image_delete(bpm);
00308 cpl_image_delete(sum);
00309 cpl_image_delete(sumon);
00310 cpl_image_delete(sumoff);
00311 cpl_imagelist_delete(on);
00312 cpl_imagelist_delete(off);
00313 cpl_propertylist_delete(onlist);
00314 cpl_propertylist_delete(offlist);
00315 cpl_free(onname);
00316 cpl_free(offname);
00317 cpl_free(sonname);
00318 cpl_free(soffname);
00319 cpl_frameset_delete(usedframes);
00320 cpl_frameset_delete(products);
00321
00322 return cpl_error_get_code();
00323 }
00324
00325
00333
00334 static cpl_error_code visir_util_repack_check(const cpl_image * self,
00335 const cpl_imagelist * on,
00336 const cpl_imagelist * off)
00337 {
00338 cpl_imagelist * diff = cpl_imagelist_duplicate(on);
00339 const cpl_error_code err1 = cpl_imagelist_subtract(diff, off);
00340 cpl_image * mean = cpl_imagelist_collapse_create(diff);
00341 const cpl_error_code err2 = cpl_image_subtract(mean, self);
00342 const unsigned bitmask = CPL_STATS_MIN | CPL_STATS_MAX | CPL_STATS_MEAN
00343 | CPL_STATS_MEDIAN | CPL_STATS_MEDIAN_DEV | CPL_STATS_STDEV;
00344
00345 cpl_stats * stats = cpl_stats_new_from_image(mean, bitmask);
00346
00347 bug_if(err1 + err2);
00348
00349 bug_if(cpl_stats_dump(stats, bitmask, stderr));
00350
00351 end_skip;
00352
00353 cpl_imagelist_delete(diff);
00354 cpl_image_delete(mean);
00355 cpl_stats_delete(stats);
00356
00357 return cpl_error_get_code();
00358
00359 }
00360
00361