MUSE Pipeline Reference Manual  2.1.1
muse_trace_plot_samples.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-2015 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 /*----------------------------------------------------------------------------*/
70 /*----------------------------------------------------------------------------*/
71 
74 #define PRINT_USAGE(rc) \
75  fprintf(stderr, "Usage: %s [ -s1 first_slice ] [ -s2 last_slice ] " \
76  "[ -n nifu ] TRACE_SAMPLES [ TRACE_TABLE ] [ MASTER_FLAT ]\n", \
77  argv[0]); \
78  cpl_end(); return (rc);
79 
80 int main(int argc, char **argv)
81 {
82  cpl_init(CPL_INIT_DEFAULT);
84 
85  if (argc <= 1) {
86  /* filename is needed at least */
87  PRINT_USAGE(1);
88  }
89 
90  char *tsname = NULL, /* samples table */
91  *ttname = NULL, /* trace table */
92  *iname = NULL; /* master flat image */
93  short slice1 = 24,
94  slice2 = 25;
95  unsigned char nifu = 0; /* IFU number to load */
96 
97  /* argument processing */
98  int i;
99  for (i = 1; i < argc; i++) {
100  if (strncmp(argv[i], "-s1", 4) == 0) {
101  /* skip to next arg to first slice number */
102  i++;
103  if (i < argc) {
104  slice1 = atoi(argv[i]);
105  } else {
106  PRINT_USAGE(2);
107  }
108  } else if (strncmp(argv[i], "-s2", 4) == 0) {
109  /* skip to next arg to last slice number */
110  i++;
111  if (i < argc) {
112  slice2 = atoi(argv[i]);
113  } else {
114  PRINT_USAGE(3);
115  }
116  } else if (strncmp(argv[i], "-n", 3) == 0) {
117  /* skip to next arg to get extension name */
118  i++;
119  if (i < argc) {
120  nifu = atoi(argv[i]);
121  if (nifu == 0 || nifu > kMuseNumIFUs) {
122  PRINT_USAGE(5);
123  }
124  } else {
125  PRINT_USAGE(4);
126  }
127  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
128  PRINT_USAGE(9);
129  } else {
130  if (tsname && ttname && iname) {
131  break; /* we have the possible names, skip the rest */
132  }
133  if (!tsname) {
134  tsname = argv[i]; /* set the name for the samples table */
135  } else if (!ttname) {
136  ttname = argv[i]; /* set the name for the trace table */
137  } else {
138  iname = argv[i] /* set the name for the master flat image */;
139  }
140  }
141  } /* for i (all arguments) */
142 
143  int iext = 1;
144  if (nifu) {
145  iext = muse_utils_get_extension_for_ifu(tsname, nifu);
146  }
147  cpl_table *ts = cpl_table_load(tsname, iext, 1);
148  if (!ts) {
149  /* fail, this is mandatory */
150  PRINT_USAGE(10);
151  }
152  cpl_propertylist *header = cpl_propertylist_load(tsname, iext);
153  char *extname = NULL;
154  if (cpl_propertylist_has(header, "EXTNAME")) {
155  extname = cpl_sprintf("[%s]", muse_pfits_get_extname(header));
156  }
157  printf("MUSE TRACE_SAMPLES table \"%s%s\", contains %"CPL_SIZE_FORMAT" rows\n",
158  tsname, extname ? extname : "", cpl_table_get_nrow(ts));
159  cpl_free(extname);
160  extname = NULL;
161  cpl_propertylist_delete(header);
162 
163  cpl_table *tt = NULL;
164  if (ttname) {
165  iext = 1;
166  if (nifu) {
167  iext = muse_utils_get_extension_for_ifu(ttname, nifu);
168  }
169  tt = cpl_table_load(ttname, iext, 1);
170  header = cpl_propertylist_load(ttname, iext);
171  if (cpl_propertylist_has(header, "EXTNAME")) {
172  extname = cpl_sprintf("[%s]", muse_pfits_get_extname(header));
173  }
174  cpl_propertylist_delete(header);
175  /* if loading didn't work, just warn, this should *
176  * not be fatal,we can still plot the sample points */
177  if (!tt) {
178  fprintf(stderr, "%s: loading the TRACE_TABLE \"%s%s\" failed: %s\n",
179  argv[0], ttname, extname ? extname : "", cpl_error_get_message());
180  } else {
181  printf("MUSE TRACE table \"%s%s\", contains %"CPL_SIZE_FORMAT" rows\n",
182  ttname, extname ? extname : "", cpl_table_get_nrow(tt));
183  }
184  cpl_free(extname);
185  extname = NULL;
186  }
187  muse_image *image = NULL;
188  if (iname) {
189  if (nifu) {
190  image = muse_image_load_from_extensions(iname, nifu);
191  }
192  if (!image) {
193  image = muse_image_load(iname);
194  }
195  /* if loading didn't work, just warn, this should *
196  * not be fatal,we can still plot the sample points */
197  if (image) { /* error message comes from the library if not found */
198  extname = cpl_sprintf("[%s]", muse_pfits_get_extname(image->header));
199  printf("MUSE MASTER_FLAT image \"%s%s\", has size "
200  "%"CPL_SIZE_FORMAT"x%"CPL_SIZE_FORMAT"\n", iname, extname,
201  cpl_image_get_size_x(image->data), cpl_image_get_size_y(image->data));
202  cpl_free(extname);
203  extname = NULL;
204  }
205  }
206 
207  cpl_error_code rc = muse_trace_plot_samples(ts, tt, slice1, slice2, nifu,
208  image);
209  switch (rc) {
210  case CPL_ERROR_NONE:
211  rc = 0;
212  break;
213  case CPL_ERROR_ILLEGAL_INPUT:
214  fprintf(stderr, "%s: \"%s\" does not seem to contain a MUSE tracing "
215  "samples table!\n", argv[0], tsname);
216  rc = 11;
217  break;
218  case CPL_ERROR_FILE_NOT_CREATED:
219  /* use manipulated CPL error message to show the failing file name */
220  fprintf(stderr, "%s: %s (temporary file for plotting)\n",
221  argv[0], cpl_error_get_message());
222  rc = 12;
223  break;
224  case CPL_ERROR_UNSUPPORTED_MODE:
225  fprintf(stderr, "%s: your platform does not seem to support pipes "
226  "[popen()/pclose()]!\n", argv[0]);
227  rc = 20;
228  break;
229  case CPL_ERROR_ASSIGNING_STREAM:
230  fprintf(stderr, "%s: could not open gnuplot (this tool uses it for "
231  "plotting)!\n", argv[0]);
232  rc = 21;
233  break;
234  default:
235  rc = 50;
236  } /* switch */
237 
238  cpl_table_delete(ts);
239  cpl_table_delete(tt);
240  muse_image_delete(image);
241  cpl_end();
242  return rc;
243 }
244 
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
Definition: muse_image.c:85
const char * muse_pfits_get_extname(const cpl_propertylist *aHeaders)
find out the extension name
Definition: muse_pfits.c:205
int muse_utils_get_extension_for_ifu(const char *aFilename, unsigned char aIFU)
Return extension number that corresponds to this IFU/channel number.
Definition: muse_utils.c:118
cpl_image * data
the data extension
Definition: muse_image.h:46
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
cpl_propertylist * header
the FITS header
Definition: muse_image.h:72
cpl_error_code muse_trace_plot_samples(cpl_table *aSamples, cpl_table *aTrace, unsigned short aSlice1, unsigned short aSlice2, unsigned char aIFU, muse_image *aImage)
Plotting of trace sample points and solution using gnuplot.
muse_image * muse_image_load(const char *aFilename)
Load the three extensions and the FITS headers of a MUSE image from a file.
Definition: muse_image.c:228
muse_image * muse_image_load_from_extensions(const char *aFilename, unsigned char aIFU)
Load the three extensions and the FITS headers of a MUSE image from extensions of a merged file...
Definition: muse_image.c:262
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.