MUSE Pipeline Reference Manual  1.0.2
muse_badpix_from_region.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 #include <muse_instrument.h>
26 #include "muse_data_format_z.h"
27 
28 /*----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------*/
80 
83 #define PRINT_USAGE(rc) \
84  fprintf(stderr, "Usage: %s [ -i INTABLE ] [ -r REFIMAGE [ -n extname ] ] " \
85  "[ -t ] REGION DQVAL IFUNUM OUTTABLE\n", argv[0]); \
86  cpl_end(); return (rc);
87 
88 int main(int argc, char **argv)
89 {
90  if (argc <= 4) {
91  PRINT_USAGE(1);
92  }
93 
94  cpl_init(CPL_INIT_DEFAULT);
95  if (getenv("ESOREX_MSG_LEVEL") && !strncmp(getenv("ESOREX_MSG_LEVEL"),
96  "debug", 6)) {
97  cpl_msg_set_level(CPL_MSG_DEBUG);
98  }
99 
100  char *region = NULL, /* input region */
101  *toname = NULL, /* output table */
102  *riname = NULL, /* optional reference image */
103  *tiname = NULL, /* optional input table */
104  *extname = NULL; /* optional extension name */
105  long dqvalue = 0; /* DQ value */
106  unsigned char nifu = 0; /* IFU number */
107  cpl_boolean trimmedcoords = CPL_FALSE;
108 
109  /* argument processing */
110  int i;
111  for (i = 1; i < argc; i++) {
112  if (strncmp(argv[i], "-i", 3) == 0) {
113  /* skip to next arg to input table filename */
114  i++;
115  if (i < argc) {
116  tiname = argv[i];
117  } else {
118  PRINT_USAGE(2);
119  }
120  } else if (strncmp(argv[i], "-r", 3) == 0) {
121  /* skip to next arg to input image filename */
122  i++;
123  if (i < argc) {
124  riname = argv[i];
125  } else {
126  PRINT_USAGE(3);
127  }
128  } else if (strncmp(argv[i], "-n", 3) == 0) {
129  /* skip to next arg to get sigma value */
130  i++;
131  if (i < argc) {
132  extname = argv[i];
133  } else {
134  PRINT_USAGE(5);
135  }
136  } else if (strncmp(argv[i], "-t", 3) == 0) {
137  /* skip to next arg to get sigma value */
138  trimmedcoords = CPL_TRUE;
139  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
140  PRINT_USAGE(9);
141  } else {
142  if (region && dqvalue && toname) {
143  break; /* we have the possible names, skip the rest */
144  }
145  if (!region) {
146  region = argv[i]; /* set the name for the input ASCII file */
147  } else if (!dqvalue) {
148  dqvalue = atol(argv[i]);
149  } else if (nifu == 0) {
150  int ifunum = atoi(argv[i]);
151  if (ifunum < 1 || ifunum > kMuseNumIFUs) {
152  PRINT_USAGE(9);
153  }
154  nifu = ifunum;
155  } else {
156  toname = argv[i]; /* set the name for the output table */
157  }
158  }
159  } /* for i (all arguments) */
160 
161  /* parse the region */
162  int x1, x2, y1, y2,
163  n = sscanf(region, "[%d:%d,%d:%d]", &x1, &x2, &y1, &y2);
164  if (n != 4) {
165  PRINT_USAGE(10);
166  }
167 
168  /* part1: ======================================= *
169  * convert the ASCII file into a new table */
170  if (trimmedcoords) {
171  printf("Converting coordinates from trimmed to raw data dimensions\n");
172  }
173  cpl_table *table = muse_cpltable_new(muse_badpix_table_def,
174  (x2-x1+1) * (y2-y1+1));
175  cpl_size irow = 0;
176  for (i = x1; i <= x2; i++) {
177  int j;
178  for (j = y1; j <= y2; j++) {
179  int ix = i,
180  iy = j;
181  if (trimmedcoords) {
182  muse_quadrants_coords_to_raw(NULL, &ix, &iy);
183  }
184  if (ix < 1 || ix > (kMuseOutputXRight + 4*kMusePreOverscanSize) ||
185  iy < 1 || iy > (kMuseOutputYTop + 4*kMusePreOverscanSize)) {
186  fprintf(stderr, "Excluding bad pixel at given %d,%d %s coordinates, as "
187  "it would be outside the MUSE CCD!\n", i, j,
188  trimmedcoords ? "trimmed" : "raw");
189  continue;
190  } /* if outside */
191  cpl_table_set_int(table, MUSE_BADPIX_X, irow, ix);
192  cpl_table_set_int(table, MUSE_BADPIX_Y, irow, iy);
193  cpl_table_set_int(table, MUSE_BADPIX_DQ, irow++, dqvalue);
194  } /* for j (vertical pixels) */
195  } /* for i (horizontal pixels) */
196  cpl_table_set_size(table, irow);
197  printf("%"CPL_SIZE_FORMAT" bad pixel%s created from region %s\n", irow,
198  irow != 1 ? "s" : "", region);
199  if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
200  printf("first 10 entries of the new table, as converted from region:\n");
201  cpl_table_dump(table, 0, 10, stdout);
202  fflush(stdout);
203  }
204 
205  /* part2: ============================================ *
206  * load and handle the input badpix table if it exists */
207  if (tiname) { /* try to open the input table, verify that it really exists */
208  cpl_propertylist *htest = cpl_propertylist_load(tiname, 0);
209  if (htest) {
210  cpl_propertylist_delete(htest);
211  } else {
212  printf("WARNING: could not open input table \"%s\"!\n", tiname);
213  tiname = NULL; /* it doesn't exist, don't keep the name */
214  } /* else */
215  } /* if tiname */
216 
217  /* merge created table with existing one, if there is one */
218  char *chan = cpl_sprintf("CHAN%02hhu", nifu);
219  cpl_table *intable = NULL;
220  int inext = -1;
221  if (tiname) {
222  cpl_errorstate state = cpl_errorstate_get();
223  intable = muse_quality_merge_badpix_from_file(table, tiname, chan, &inext);
224  if (!intable) {
225  cpl_errorstate_set(state);
226  intable = table;
227  } /* if !intable */
228  } /* if tiname */
229  /* copy the input BADPIX_TABLE, maybe replacing the one extension we modified */
230  cpl_error_code rc = muse_quality_copy_badpix_table(tiname, toname, inext,
231  intable);
232  cpl_boolean savedsomething = rc == CPL_ERROR_NONE;
233 
234  /* save or append the extension if not already done above */
235  inext = cpl_fits_find_extension(toname, chan);
236  if (inext <= 0) {
237  cpl_propertylist *pheader = NULL,
238  *header = NULL;
239  if (!riname) {
240  printf("WARNING: no pre-existing data, creating minimal header from "
241  "scratch!\n");
242  pheader = cpl_propertylist_new();
243  cpl_propertylist_append_string(pheader, "TELESCOP", "ESO-VLT-U4");
244  cpl_propertylist_append_string(pheader, "INSTRUME", "MUSE");
245  cpl_propertylist_append_string(pheader, "OBJECT",
246  "Bad pixel table for MUSE (BADPIX_TABLE)");
247  header = cpl_propertylist_new();
248  cpl_propertylist_append_string(header, "EXTNAME", chan);
249  cpl_propertylist_append_string(header, "ESO DET CHIP NAME", chan);
250  } else {
251  printf("Using primary header from \"%s\" as starting point\n", riname);
252  pheader = cpl_propertylist_load_regexp(riname, 0, "TELESCOP|INSTRUME|"
253  "ESO DET ", 0);
254  /* remove exposure-specifc info and the stuff about *
255  * CHIP and OUTi again, that belongs into the extension */
256  cpl_propertylist_erase_regexp(pheader, "ESO DET DEV[0-9] (SHUT |EXP )|"
257  "ESO DET (EXP |[DU]IT|NDIT|DKTM)", 0);
258  cpl_propertylist_erase_regexp(pheader, "ESO DET (CHIP |OUT[1-4])", 0);
259  cpl_propertylist_update_string(pheader, "OBJECT",
260  "Bad pixel table for MUSE (BADPIX_TABLE)");
261  int refext = cpl_fits_find_extension(riname, extname);
262  if (refext >= 0) {
263  printf("Using extension header from \"%s[%s]\" (%d)\n", riname, extname,
264  refext);
265  header = cpl_propertylist_load_regexp(riname, refext,
266  "^EXT|ESO DET (CHIP |OUT[1-4])", 0);
267  } else {
268  printf("WARNING: no pre-existing extension found, creating minimal "
269  "extension header from scratch!\n");
270  header = cpl_propertylist_new();
271  cpl_propertylist_append_string(header, "EXTNAME", chan);
272  cpl_propertylist_append_string(header, "ESO DET CHIP NAME", chan);
273  }
274  }
275  if (savedsomething) {
276  /* just extend the already saved data with the new extension */
277  rc = cpl_table_save(table, NULL, header, toname, CPL_IO_EXTEND);
278  } else {
279  /* save the whole file, overwriting what may be there under that name */
280  cpl_propertylist_update_string(pheader, "PIPEFILE", toname);
281  cpl_propertylist_set_comment(pheader, "PIPEFILE",
282  "pretend to be a pipeline output file");
283  cpl_propertylist_update_string(pheader, "ESO PRO CATG", MUSE_TAG_BADPIX_TABLE);
284  cpl_propertylist_set_comment(pheader, "ESO PRO CATG",
285  "MUSE bad pixel table");
286  rc = cpl_table_save(table, pheader, header, toname, CPL_IO_CREATE);
287  }
288  if (rc != CPL_ERROR_NONE) {
289  fprintf(stderr, "Saving to \"%s\" failed (rc=%d): %s\n", toname, rc,
290  cpl_error_get_message());
291  } else {
292  printf("Saved to \"%s\" (%s)\n", toname,
293  savedsomething ? "new extension" : "new file");
294  }
295  cpl_propertylist_delete(pheader);
296  cpl_propertylist_delete(header);
297  } /* if new table now saved yet */
298  cpl_free(chan);
299  cpl_table_delete(table);
300 
301  if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
302  printf("Output file \"%s\" has primary header and %"CPL_SIZE_FORMAT
303  " extensions\n", toname, cpl_fits_count_extensions(toname));
304  cpl_errorstate_dump(0, CPL_FALSE, NULL);
305  cpl_memory_dump();
306  fflush(NULL);
307  }
308  cpl_end();
309  return 0;
310 }
311 
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
cpl_table * muse_cpltable_new(const muse_cpltable_def *aDef, cpl_size aLength)
Create an empty table according to the specified definition.
const muse_cpltable_def muse_badpix_table_def[]
cpl_error_code muse_quadrants_coords_to_raw(cpl_propertylist *aHeader, int *aX, int *aY)
Convert coordinates of a trimmed image to raw-image coordinates.