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