00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <muse.h>
00023 #include <string.h>
00024
00025 #include <muse_instrument.h>
00026 #include "muse_data_format_z.h"
00027
00028
00079
00080
00083 #define PRINT_USAGE(rc) \
00084 fprintf(stderr, "Usage: %s [ -i INTABLE ] [ -r REFIMAGE [ -n extname ] ] " \
00085 "[ -t ] REGION DQVAL IFUNUM OUTTABLE\n", argv[0]); \
00086 cpl_end(); return (rc);
00087
00088 int main(int argc, char **argv)
00089 {
00090 cpl_init(CPL_INIT_DEFAULT);
00091 muse_processing_recipeinfo(NULL);
00092 if (argc <= 4) {
00093 PRINT_USAGE(1);
00094 }
00095
00096 if (getenv("ESOREX_MSG_LEVEL") && !strncmp(getenv("ESOREX_MSG_LEVEL"),
00097 "debug", 6)) {
00098 cpl_msg_set_level(CPL_MSG_DEBUG);
00099 }
00100
00101 char *region = NULL,
00102 *toname = NULL,
00103 *riname = NULL,
00104 *tiname = NULL,
00105 *extname = NULL;
00106 long dqvalue = 0;
00107 unsigned char nifu = 0;
00108 cpl_boolean trimmedcoords = CPL_FALSE;
00109
00110
00111 int i;
00112 for (i = 1; i < argc; i++) {
00113 if (strncmp(argv[i], "-i", 3) == 0) {
00114
00115 i++;
00116 if (i < argc) {
00117 tiname = argv[i];
00118 } else {
00119 PRINT_USAGE(2);
00120 }
00121 } else if (strncmp(argv[i], "-r", 3) == 0) {
00122
00123 i++;
00124 if (i < argc) {
00125 riname = argv[i];
00126 } else {
00127 PRINT_USAGE(3);
00128 }
00129 } else if (strncmp(argv[i], "-n", 3) == 0) {
00130
00131 i++;
00132 if (i < argc) {
00133 extname = argv[i];
00134 } else {
00135 PRINT_USAGE(5);
00136 }
00137 } else if (strncmp(argv[i], "-t", 3) == 0) {
00138
00139 trimmedcoords = CPL_TRUE;
00140 } else if (strncmp(argv[i], "-", 1) == 0) {
00141 PRINT_USAGE(9);
00142 } else {
00143 if (region && dqvalue && toname) {
00144 break;
00145 }
00146 if (!region) {
00147 region = argv[i];
00148 } else if (!dqvalue) {
00149 dqvalue = atol(argv[i]);
00150 } else if (nifu == 0) {
00151 int ifunum = atoi(argv[i]);
00152 if (ifunum < 1 || ifunum > kMuseNumIFUs) {
00153 PRINT_USAGE(9);
00154 }
00155 nifu = ifunum;
00156 } else {
00157 toname = argv[i];
00158 }
00159 }
00160 }
00161
00162
00163 int x1, x2, y1, y2,
00164 n = sscanf(region, "[%d:%d,%d:%d]", &x1, &x2, &y1, &y2);
00165 if (n != 4) {
00166 PRINT_USAGE(10);
00167 }
00168
00169
00170
00171 if (trimmedcoords) {
00172 printf("Converting coordinates from trimmed to raw data dimensions\n");
00173 }
00174 cpl_table *table = muse_cpltable_new(muse_badpix_table_def,
00175 (x2-x1+1) * (y2-y1+1));
00176 cpl_size irow = 0;
00177 for (i = x1; i <= x2; i++) {
00178 int j;
00179 for (j = y1; j <= y2; j++) {
00180 int ix = i,
00181 iy = j;
00182 if (trimmedcoords) {
00183 muse_quadrants_coords_to_raw(NULL, &ix, &iy);
00184 }
00185 if (ix < 1 || ix > (kMuseOutputXRight + 4*kMusePreOverscanSize) ||
00186 iy < 1 || iy > (kMuseOutputYTop + 4*kMusePreOverscanSize)) {
00187 fprintf(stderr, "Excluding bad pixel at given %d,%d %s coordinates, as "
00188 "it would be outside the MUSE CCD!\n", i, j,
00189 trimmedcoords ? "trimmed" : "raw");
00190 continue;
00191 }
00192 cpl_table_set_int(table, MUSE_BADPIX_X, irow, ix);
00193 cpl_table_set_int(table, MUSE_BADPIX_Y, irow, iy);
00194 cpl_table_set_int(table, MUSE_BADPIX_DQ, irow++, dqvalue);
00195 }
00196 }
00197 cpl_table_set_size(table, irow);
00198 printf("%"CPL_SIZE_FORMAT" bad pixel%s created from region %s\n", irow,
00199 irow != 1 ? "s" : "", region);
00200 if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
00201 printf("first 10 entries of the new table, as converted from region:\n");
00202 cpl_table_dump(table, 0, 10, stdout);
00203 fflush(stdout);
00204 }
00205
00206
00207
00208 if (tiname) {
00209 cpl_propertylist *htest = cpl_propertylist_load(tiname, 0);
00210 if (htest) {
00211 cpl_propertylist_delete(htest);
00212 } else {
00213 printf("WARNING: could not open input table \"%s\"!\n", tiname);
00214 tiname = NULL;
00215 }
00216 }
00217
00218
00219 char *chan = cpl_sprintf("CHAN%02hhu", nifu);
00220 cpl_table *intable = NULL;
00221 int inext = -1;
00222 if (tiname) {
00223 cpl_errorstate state = cpl_errorstate_get();
00224 intable = muse_quality_merge_badpix_from_file(table, tiname, chan, &inext);
00225 if (!intable) {
00226 cpl_errorstate_set(state);
00227 intable = table;
00228 }
00229 }
00230
00231 cpl_error_code rc = muse_quality_copy_badpix_table(tiname, toname, inext,
00232 intable);
00233 cpl_boolean savedsomething = rc == CPL_ERROR_NONE;
00234
00235
00236 inext = cpl_fits_find_extension(toname, chan);
00237 if (inext <= 0) {
00238 cpl_propertylist *pheader = NULL,
00239 *header = NULL;
00240 if (!riname) {
00241 printf("WARNING: no pre-existing data, creating minimal header from "
00242 "scratch!\n");
00243 pheader = cpl_propertylist_new();
00244 cpl_propertylist_append_string(pheader, "TELESCOP", "ESO-VLT-U4");
00245 cpl_propertylist_append_string(pheader, "INSTRUME", "MUSE");
00246 cpl_propertylist_append_string(pheader, "OBJECT",
00247 "Bad pixel table for MUSE (BADPIX_TABLE)");
00248 header = cpl_propertylist_new();
00249 cpl_propertylist_append_string(header, "EXTNAME", chan);
00250 cpl_propertylist_append_string(header, "ESO DET CHIP NAME", chan);
00251 } else {
00252 printf("Using primary header from \"%s\" as starting point\n", riname);
00253 pheader = cpl_propertylist_load_regexp(riname, 0, "TELESCOP|INSTRUME|"
00254 "ESO DET ", 0);
00255
00256
00257 cpl_propertylist_erase_regexp(pheader, "ESO DET DEV[0-9] (SHUT |EXP )|"
00258 "ESO DET (EXP |[DU]IT|NDIT|DKTM)", 0);
00259 cpl_propertylist_erase_regexp(pheader, "ESO DET (CHIP |OUT[1-4])", 0);
00260 cpl_propertylist_update_string(pheader, "OBJECT",
00261 "Bad pixel table for MUSE (BADPIX_TABLE)");
00262 int refext = cpl_fits_find_extension(riname, extname);
00263 if (refext >= 0) {
00264 printf("Using extension header from \"%s[%s]\" (%d)\n", riname, extname,
00265 refext);
00266 header = cpl_propertylist_load_regexp(riname, refext,
00267 "^EXT|ESO DET (CHIP |OUT[1-4])", 0);
00268 } else {
00269 printf("WARNING: no pre-existing extension found, creating minimal "
00270 "extension header from scratch!\n");
00271 header = cpl_propertylist_new();
00272 cpl_propertylist_append_string(header, "EXTNAME", chan);
00273 cpl_propertylist_append_string(header, "ESO DET CHIP NAME", chan);
00274 }
00275 }
00276 if (savedsomething) {
00277
00278 rc = cpl_table_save(table, NULL, header, toname, CPL_IO_EXTEND);
00279 } else {
00280
00281 cpl_propertylist_update_string(pheader, "PIPEFILE", toname);
00282 cpl_propertylist_set_comment(pheader, "PIPEFILE",
00283 "pretend to be a pipeline output file");
00284 cpl_propertylist_update_string(pheader, "ESO PRO CATG", MUSE_TAG_BADPIX_TABLE);
00285 cpl_propertylist_set_comment(pheader, "ESO PRO CATG",
00286 "MUSE bad pixel table");
00287 rc = cpl_table_save(table, pheader, header, toname, CPL_IO_CREATE);
00288 }
00289 if (rc != CPL_ERROR_NONE) {
00290 fprintf(stderr, "Saving to \"%s\" failed (rc=%d): %s\n", toname, rc,
00291 cpl_error_get_message());
00292 } else {
00293 printf("Saved to \"%s\" (%s)\n", toname,
00294 savedsomething ? "new extension" : "new file");
00295 }
00296 cpl_propertylist_delete(pheader);
00297 cpl_propertylist_delete(header);
00298 }
00299 cpl_free(chan);
00300 cpl_table_delete(table);
00301
00302 if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
00303 printf("Output file \"%s\" has primary header and %"CPL_SIZE_FORMAT
00304 " extensions\n", toname, cpl_fits_count_extensions(toname));
00305 cpl_errorstate_dump(0, CPL_FALSE, NULL);
00306 cpl_memory_dump();
00307 fflush(NULL);
00308 }
00309 cpl_end();
00310 return 0;
00311 }
00312