25 #include <muse_instrument.h>
26 #include "muse_data_format_z.h"
82 #define PRINT_USAGE(rc) \
83 fprintf(stderr, "Usage: %s [ -i INTABLE ] [ -r REFIMAGE [ -n extname ] ] " \
84 "[ -t ] ASCIIFILE IFUNUM OUTTABLE\n", argv[0]); \
85 cpl_end(); return (rc);
87 int main(
int argc,
char **argv)
94 cpl_init(CPL_INIT_DEFAULT);
95 if (getenv(
"ESOREX_MSG_LEVEL") && !strncmp(getenv(
"ESOREX_MSG_LEVEL"),
97 cpl_msg_set_level(CPL_MSG_DEBUG);
105 unsigned char nifu = 0;
106 cpl_boolean trimmedcoords = CPL_FALSE;
110 for (i = 1; i < argc; i++) {
111 if (strncmp(argv[i],
"-i", 3) == 0) {
119 }
else if (strncmp(argv[i],
"-r", 3) == 0) {
127 }
else if (strncmp(argv[i],
"-n", 3) == 0) {
135 }
else if (strncmp(argv[i],
"-t", 3) == 0) {
137 trimmedcoords = CPL_TRUE;
138 }
else if (strncmp(argv[i],
"-", 1) == 0) {
141 if (fname && toname) {
146 }
else if (nifu == 0) {
147 int ifunum = atoi(argv[i]);
148 if (ifunum < 1 || ifunum > kMuseNumIFUs) {
159 FILE *fp = fopen(fname,
"r");
167 printf(
"Converting coordinates from trimmed to raw data dimensions\n");
169 #define START_SIZE 10000
171 cpl_size irow = 0, nrow = cpl_table_get_nrow(table);
173 while (fscanf(fp,
"%d %d %d", &ix, &iy, &idq) == 3) {
175 cpl_table_set_size(table, nrow + START_SIZE);
176 nrow = cpl_table_get_nrow(table);
181 cpl_table_set_int(table, MUSE_BADPIX_X, irow, ix);
182 cpl_table_set_int(table, MUSE_BADPIX_Y, irow, iy);
183 cpl_table_set_int(table, MUSE_BADPIX_DQ, irow++, idq);
185 cpl_table_set_size(table, irow);
186 printf(
"%"CPL_SIZE_FORMAT
" bad pixel%s read from \"%s\"\n", irow,
187 irow != 1 ?
"s" :
"", fname);
189 if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
190 printf(
"first 10 entries of the new table, as converted from ASCII:\n");
191 cpl_table_dump(table, 0, 10, stdout);
198 cpl_propertylist *htest = cpl_propertylist_load(tiname, 0);
200 cpl_propertylist_delete(htest);
202 printf(
"WARNING: could not open input table \"%s\"!\n", tiname);
208 char *chan = cpl_sprintf(
"CHAN%02hhu", nifu);
209 cpl_table *intable = NULL;
212 cpl_errorstate state = cpl_errorstate_get();
215 cpl_errorstate_set(state);
222 cpl_boolean savedsomething = rc == CPL_ERROR_NONE;
225 inext = cpl_fits_find_extension(toname, chan);
227 cpl_propertylist *pheader = NULL,
230 printf(
"WARNING: no pre-existing data, creating minimal header from "
232 pheader = cpl_propertylist_new();
233 cpl_propertylist_append_string(pheader,
"TELESCOP",
"ESO-VLT-U4");
234 cpl_propertylist_append_string(pheader,
"INSTRUME",
"MUSE");
235 cpl_propertylist_append_string(pheader,
"OBJECT",
236 "Bad pixel table for MUSE (BADPIX_TABLE)");
237 header = cpl_propertylist_new();
238 cpl_propertylist_append_string(header,
"EXTNAME", chan);
239 cpl_propertylist_append_string(header,
"ESO DET CHIP NAME", chan);
241 printf(
"Using primary header from \"%s\" as starting point\n", riname);
242 pheader = cpl_propertylist_load_regexp(riname, 0,
"TELESCOP|INSTRUME|"
246 cpl_propertylist_erase_regexp(pheader,
"ESO DET DEV[0-9] (SHUT |EXP )|"
247 "ESO DET (EXP |[DU]IT|NDIT|DKTM)", 0);
248 cpl_propertylist_erase_regexp(pheader,
"ESO DET (CHIP |OUT[1-4])", 0);
249 cpl_propertylist_update_string(pheader,
"OBJECT",
250 "Bad pixel table for MUSE (BADPIX_TABLE)");
251 int refext = cpl_fits_find_extension(riname, extname);
253 printf(
"Using extension header from \"%s[%s]\" (%d)\n", riname, extname,
255 header = cpl_propertylist_load_regexp(riname, refext,
256 "^EXT|ESO DET (CHIP |OUT[1-4])", 0);
258 printf(
"WARNING: no pre-existing extension found, creating minimal "
259 "extension header from scratch!\n");
260 header = cpl_propertylist_new();
261 cpl_propertylist_append_string(header,
"EXTNAME", chan);
262 cpl_propertylist_append_string(header,
"ESO DET CHIP NAME", chan);
265 if (savedsomething) {
267 rc = cpl_table_save(table, NULL, header, toname, CPL_IO_EXTEND);
270 cpl_propertylist_update_string(pheader,
"PIPEFILE", toname);
271 cpl_propertylist_set_comment(pheader,
"PIPEFILE",
272 "pretend to be a pipeline output file");
273 cpl_propertylist_update_string(pheader,
"ESO PRO CATG", MUSE_TAG_BADPIX_TABLE);
274 cpl_propertylist_set_comment(pheader,
"ESO PRO CATG",
275 "MUSE bad pixel table");
276 rc = cpl_table_save(table, pheader, header, toname, CPL_IO_CREATE);
278 if (rc != CPL_ERROR_NONE) {
279 fprintf(stderr,
"Saving to \"%s\" failed (rc=%d): %s\n", toname, rc,
280 cpl_error_get_message());
282 printf(
"Saved to \"%s\" (%s)\n", toname,
283 savedsomething ?
"new extension" :
"new file");
285 cpl_propertylist_delete(pheader);
286 cpl_propertylist_delete(header);
289 cpl_table_delete(table);
291 if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
292 printf(
"Output file \"%s\" has primary header and %"CPL_SIZE_FORMAT
293 " extensions\n", toname, cpl_fits_count_extensions(toname));
294 cpl_errorstate_dump(0, CPL_FALSE, NULL);
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.
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.
cpl_table * muse_cpltable_new(const muse_cpltable_def *aDef, cpl_size aLength)
Create an empty table according to the specified definition.
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.