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