MUSE Pipeline Reference Manual  1.0.2
muse_badpix_from_dq.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set sw=2 sts=2 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2007-2014 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include <muse.h>
23 #include <string.h>
24 
25 /*----------------------------------------------------------------------------*/
54 /*----------------------------------------------------------------------------*/
55 
58 #define PRINT_USAGE(rc) \
59  fprintf(stderr, "Usage: %s [ -i INTABLE ] INFILE OUTTABLE\n", argv[0]); \
60  cpl_end(); return (rc);
61 
62 int main(int argc, char **argv)
63 {
64  if (argc <= 2) {
65  /* two filenames are needed at least */
66  PRINT_USAGE(1);
67  }
68 
69  cpl_init(CPL_INIT_DEFAULT);
70  if (getenv("ESOREX_MSG_LEVEL") && !strncmp(getenv("ESOREX_MSG_LEVEL"),
71  "debug", 6)) {
72  cpl_msg_set_level(CPL_MSG_DEBUG);
73  }
74 
75  char *iname = NULL, /* input image */
76  *tiname = NULL, /* optional input table */
77  *toname = NULL; /* output table */
78 
79  /* argument processing */
80  int i;
81  for (i = 1; i < argc; i++) {
82  if (strncmp(argv[i], "-i", 3) == 0) {
83  /* skip to next arg to input table filename */
84  i++;
85  if (i < argc) {
86  tiname = argv[i];
87  } else {
88  PRINT_USAGE(2);
89  }
90  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
91  PRINT_USAGE(9);
92  } else {
93  if (iname && toname) {
94  break; /* we have the possible names, skip the rest */
95  }
96  if (!iname) {
97  iname = argv[i]; /* set the name for the input image */
98  } else {
99  toname = argv[i]; /* set the name for the output table */
100  }
101  }
102  } /* for i (all arguments) */
103 
104  int extension = cpl_fits_find_extension(iname, EXTNAME_DQ);
105  cpl_image *image = cpl_image_load(iname, CPL_TYPE_INT, 0, extension);
106  if (!image) {
107  PRINT_USAGE(10);
108  }
109  /* load main headers */
110  cpl_propertylist *header = cpl_propertylist_load(iname, 0);
111  int nx = cpl_image_get_size_x(image),
112  ny = cpl_image_get_size_y(image);
113  unsigned char nifu = muse_utils_get_ifu(header);
114  printf("Read %dx%d image of IFU %hhu from \"%s\"\n", nx, ny, nifu, iname);
115 
116  /* part1: ======================================= *
117  * convert the DQ from the image into a new table */
118  cpl_table *table = muse_quality_convert_dq(image);
119 #if 0
120  cpl_table_dump(table, 0, 1000, stdout);
121  fflush(stdout);
122 #endif
123  cpl_size nrow = cpl_table_get_nrow(table);
124  printf("%"CPL_SIZE_FORMAT" bad pixel%s found\n", nrow, nrow != 1 ? "s" : "");
125 
126  /* part2: ============================================ *
127  * load and handle the input badpix table if it exists */
128  if (tiname) { /* try to open the input table, verify that it really exists */
129  cpl_propertylist *htest = cpl_propertylist_load(tiname, 0);
130  if (htest) {
131  cpl_propertylist_delete(htest);
132  } else {
133  printf("WARNING: could not open input table \"%s\"!\n", tiname);
134  tiname = NULL; /* it doesn't exist, don't keep the name */
135  } /* else */
136  } /* if tiname */
137 
138  /* merge created table with existing one, if there is one */
139  cpl_table *intable = NULL;
140  int inext = -1;
141  if (tiname) {
142  char *extifu = cpl_sprintf("CHAN%02hhu", nifu);
143  intable = muse_quality_merge_badpix_from_file(table, tiname, extifu, &inext);
144  cpl_free(extifu);
145  if (!intable) {
146  intable = table;
147  } /* if !intable */
148  } /* if tiname */
149  /* copy the input BADPIX_TABLE, replacing the one extension we modified */
150  muse_quality_copy_badpix_table(tiname, toname, inext, intable);
151 
152  /* save or append the extension if not already done above */
153  if (inext < 0) {
154  cpl_propertylist *pheader = NULL;
155  /* create a primary header, if we create a new file */
156  if (!tiname) {
157  pheader = cpl_propertylist_load_regexp(iname, 0, "TELESCOP|INSTRUME|"
158  "ESO DET ", 0);
159  /* remove exposure-specifc info and the stuff about *
160  * CHIP and OUTi again, that belongs into the extension */
161  cpl_propertylist_erase_regexp(pheader, "ESO DET DEV[0-9] (SHUT |EXP )|"
162  "ESO DET (EXP |[DU]IT|NDIT|DKTM)", 0);
163  cpl_propertylist_erase_regexp(pheader, "ESO DET (CHIP |OUT[1-4])", 0);
164  cpl_propertylist_update_string(pheader, "OBJECT",
165  "Bad pixel table for MUSE (BADPIX_TABLE)");
166  cpl_propertylist_update_string(pheader, "PIPEFILE", toname);
167  cpl_propertylist_set_comment(pheader, "PIPEFILE",
168  "pretend to be a pipeline output file");
169  } /* if !tiname */
170  /* for saving, cut the extension header to the necessary *
171  * information of extension name and about CHIP and OUTi */
172  cpl_propertylist_erase_regexp(header, "^EXT|ESO DET (CHIP |OUT[1-4])", 1);
173  /* create new file if necessary or append to the file saved above */
174  cpl_error_code rc = cpl_table_save(table, pheader, header, toname,
175  pheader ? CPL_IO_CREATE : CPL_IO_EXTEND);
176  if (rc != CPL_ERROR_NONE) {
177  fprintf(stderr, "Saving to \"%s\" failed (rc=%d): %s\n", toname, rc,
178  cpl_error_get_message());
179  } else {
180  printf("Saved to \"%s\"\n", toname);
181  }
182  cpl_propertylist_delete(pheader); /* in case it was created */
183  } /* if inext < 0 (not saved yet) */
184  cpl_table_delete(table);
185  cpl_image_delete(image);
186  cpl_propertylist_delete(header);
187 
188  if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
189  printf("Output file \"%s\" has primary header and %"CPL_SIZE_FORMAT
190  " extensions\n", toname, cpl_fits_count_extensions(toname));
191  cpl_errorstate_dump(0, CPL_FALSE, NULL);
192  cpl_memory_dump();
193  }
194  cpl_end();
195  return 0;
196 }
197 
cpl_table * muse_quality_convert_dq(cpl_image *aDQ)
Convert a data quality (DQ) image extension to a bad pixel table.
Definition: muse_quality.c:511
unsigned char muse_utils_get_ifu(const cpl_propertylist *aHeaders)
Find out the IFU/channel from which this header originated.
Definition: muse_utils.c:99
cpl_error_code muse_quality_copy_badpix_table(const char *aInFile, const char *aOutFile, int aExtension, const cpl_table *aTable)
Copy bad pixel table on disk, replacing the table in one extension.
Definition: muse_quality.c:764
cpl_table * muse_quality_merge_badpix_from_file(const cpl_table *aTable, const char *aInFile, const char *aExtname, int *aExt)
Merge bad pixel table in memory with table from file on disk.
Definition: muse_quality.c:698