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