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
00054
00055
00058 #define PRINT_USAGE(rc) \
00059 fprintf(stderr, "Usage: %s [ -i INTABLE ] INFILE OUTTABLE\n", argv[0]); \
00060 cpl_end(); return (rc);
00061
00062 int main(int argc, char **argv)
00063 {
00064 cpl_init(CPL_INIT_DEFAULT);
00065 muse_processing_recipeinfo(NULL);
00066 if (argc <= 2) {
00067
00068 PRINT_USAGE(1);
00069 }
00070
00071 if (getenv("ESOREX_MSG_LEVEL") && !strncmp(getenv("ESOREX_MSG_LEVEL"),
00072 "debug", 6)) {
00073 cpl_msg_set_level(CPL_MSG_DEBUG);
00074 }
00075
00076 char *iname = NULL,
00077 *tiname = NULL,
00078 *toname = NULL;
00079
00080
00081 int i;
00082 for (i = 1; i < argc; i++) {
00083 if (strncmp(argv[i], "-i", 3) == 0) {
00084
00085 i++;
00086 if (i < argc) {
00087 tiname = argv[i];
00088 } else {
00089 PRINT_USAGE(2);
00090 }
00091 } else if (strncmp(argv[i], "-", 1) == 0) {
00092 PRINT_USAGE(9);
00093 } else {
00094 if (iname && toname) {
00095 break;
00096 }
00097 if (!iname) {
00098 iname = argv[i];
00099 } else {
00100 toname = argv[i];
00101 }
00102 }
00103 }
00104
00105 int extension = cpl_fits_find_extension(iname, EXTNAME_DQ);
00106 cpl_image *image = cpl_image_load(iname, CPL_TYPE_INT, 0, extension);
00107 if (!image) {
00108 PRINT_USAGE(10);
00109 }
00110
00111 cpl_propertylist *header = cpl_propertylist_load(iname, 0);
00112 int nx = cpl_image_get_size_x(image),
00113 ny = cpl_image_get_size_y(image);
00114 unsigned char nifu = muse_utils_get_ifu(header);
00115 printf("Read %dx%d image of IFU %hhu from \"%s\"\n", nx, ny, nifu, iname);
00116
00117
00118
00119 cpl_table *table = muse_quality_convert_dq(image);
00120 #if 0
00121 cpl_table_dump(table, 0, 1000, stdout);
00122 fflush(stdout);
00123 #endif
00124 cpl_size nrow = cpl_table_get_nrow(table);
00125 printf("%"CPL_SIZE_FORMAT" bad pixel%s found\n", nrow, nrow != 1 ? "s" : "");
00126
00127
00128
00129 if (tiname) {
00130 cpl_propertylist *htest = cpl_propertylist_load(tiname, 0);
00131 if (htest) {
00132 cpl_propertylist_delete(htest);
00133 } else {
00134 printf("WARNING: could not open input table \"%s\"!\n", tiname);
00135 tiname = NULL;
00136 }
00137 }
00138
00139
00140 cpl_table *intable = NULL;
00141 int inext = -1;
00142 if (tiname) {
00143 char *extifu = cpl_sprintf("CHAN%02hhu", nifu);
00144 intable = muse_quality_merge_badpix_from_file(table, tiname, extifu, &inext);
00145 cpl_free(extifu);
00146 if (!intable) {
00147 intable = table;
00148 }
00149 }
00150
00151 muse_quality_copy_badpix_table(tiname, toname, inext, intable);
00152
00153
00154 if (inext < 0) {
00155 cpl_propertylist *pheader = NULL;
00156
00157 if (!tiname) {
00158 pheader = cpl_propertylist_load_regexp(iname, 0, "TELESCOP|INSTRUME|"
00159 "ESO DET ", 0);
00160
00161
00162 cpl_propertylist_erase_regexp(pheader, "ESO DET DEV[0-9] (SHUT |EXP )|"
00163 "ESO DET (EXP |[DU]IT|NDIT|DKTM)", 0);
00164 cpl_propertylist_erase_regexp(pheader, "ESO DET (CHIP |OUT[1-4])", 0);
00165 cpl_propertylist_update_string(pheader, "OBJECT",
00166 "Bad pixel table for MUSE (BADPIX_TABLE)");
00167 cpl_propertylist_update_string(pheader, "PIPEFILE", toname);
00168 cpl_propertylist_set_comment(pheader, "PIPEFILE",
00169 "pretend to be a pipeline output file");
00170 }
00171
00172
00173 cpl_propertylist_erase_regexp(header, "^EXT|ESO DET (CHIP |OUT[1-4])", 1);
00174
00175 cpl_error_code rc = cpl_table_save(table, pheader, header, toname,
00176 pheader ? CPL_IO_CREATE : CPL_IO_EXTEND);
00177 if (rc != CPL_ERROR_NONE) {
00178 fprintf(stderr, "Saving to \"%s\" failed (rc=%d): %s\n", toname, rc,
00179 cpl_error_get_message());
00180 } else {
00181 printf("Saved to \"%s\"\n", toname);
00182 }
00183 cpl_propertylist_delete(pheader);
00184 }
00185 cpl_table_delete(table);
00186 cpl_image_delete(image);
00187 cpl_propertylist_delete(header);
00188
00189 if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
00190 printf("Output file \"%s\" has primary header and %"CPL_SIZE_FORMAT
00191 " extensions\n", toname, cpl_fits_count_extensions(toname));
00192 cpl_errorstate_dump(0, CPL_FALSE, NULL);
00193 cpl_memory_dump();
00194 }
00195 cpl_end();
00196 return 0;
00197 }
00198