MUSE Pipeline Reference Manual  1.0.2
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-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 /*----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------*/
67 
70 #define PRINT_USAGE(rc) \
71  fprintf(stderr, "Usage: %s [ -s1 first_slice ] [ -s2 last_slice ] " \
72  "TRACE_SAMPLES [ TRACE_TABLE ] [ MASTER_FLAT ]\n", argv[0]); \
73  cpl_end(); return (rc);
74 
75 int main(int argc, char **argv)
76 {
77  cpl_init(CPL_INIT_DEFAULT);
78 
79  if (argc <= 1) {
80  /* filename is needed at least */
81  PRINT_USAGE(1);
82  }
83 
84  char *tsname = NULL, /* samples table */
85  *ttname = NULL, /* trace table */
86  *iname = NULL; /* master flat image */
87  short slice1 = 24,
88  slice2 = 25;
89 
90  /* argument processing */
91  int i;
92  for (i = 1; i < argc; i++) {
93  if (strncmp(argv[i], "-s1", 4) == 0) {
94  /* skip to next arg to first slice number */
95  i++;
96  if (i < argc) {
97  slice1 = atoi(argv[i]);
98  } else {
99  PRINT_USAGE(2);
100  }
101  } else if (strncmp(argv[i], "-s2", 4) == 0) {
102  /* skip to next arg to last slice number */
103  i++;
104  if (i < argc) {
105  slice2 = atoi(argv[i]);
106  } else {
107  PRINT_USAGE(3);
108  }
109  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
110  PRINT_USAGE(9);
111  } else {
112  if (tsname && ttname && iname) {
113  break; /* we have the possible names, skip the rest */
114  }
115  if (!tsname) {
116  tsname = argv[i]; /* set the name for the samples table */
117  } else if (!ttname) {
118  ttname = argv[i]; /* set the name for the trace table */
119  } else {
120  iname = argv[i] /* set the name for the master flat image */;
121  }
122  }
123  } /* for i (all arguments) */
124 
125  cpl_table *ts = cpl_table_load(tsname, 1, 0);
126  if (!ts) {
127  /* fail, this is mandatory */
128  PRINT_USAGE(10);
129  }
130  printf("MUSE TRACE_SAMPLES table \"%s\", contains %"CPL_SIZE_FORMAT" rows\n",
131  tsname, cpl_table_get_nrow(ts));
132 
133  cpl_table *tt = NULL;
134  if (ttname) {
135  tt = cpl_table_load(ttname, 1, 0);
136  /* if loading didn't work, just warn, this should *
137  * not be fatal,we can still plot the sample points */
138  if (!tt) {
139  fprintf(stderr, "%s: loading the TRACE_TABLE \"%s\" failed: %s\n",
140  argv[0], ttname, cpl_error_get_message());
141  } else {
142  printf("MUSE TRACE table \"%s\", contains %"CPL_SIZE_FORMAT" rows\n",
143  ttname, cpl_table_get_nrow(tt));
144  }
145  }
146  muse_image *image = NULL;
147  if (iname) {
148  image = muse_image_load(iname);
149  /* if loading didn't work, just warn, this should *
150  * not be fatal,we can still plot the sample points */
151  if (image) { /* error message comes from the library if not found */
152  printf("MUSE MASTER_FLAT image \"%s\", has size "
153  "%"CPL_SIZE_FORMAT"x%"CPL_SIZE_FORMAT"\n", iname,
154  cpl_image_get_size_x(image->data), cpl_image_get_size_y(image->data));
155  }
156  }
157 
158  cpl_error_code rc = muse_trace_plot_samples(ts, tt, slice1, slice2, image);
159  switch (rc) {
160  case CPL_ERROR_NONE:
161  rc = 0;
162  break;
163  case CPL_ERROR_ILLEGAL_INPUT:
164  fprintf(stderr, "%s: \"%s\" does not seem to contain a MUSE tracing "
165  "samples table!\n", argv[0], tsname);
166  rc = 11;
167  break;
168  case CPL_ERROR_FILE_NOT_CREATED:
169  /* use manipulated CPL error message to show the failing file name */
170  fprintf(stderr, "%s: %s (temporary file for plotting)\n",
171  argv[0], cpl_error_get_message());
172  rc = 12;
173  break;
174  case CPL_ERROR_UNSUPPORTED_MODE:
175  fprintf(stderr, "%s: your platform does not seem to support pipes "
176  "[popen()/pclose()]!\n", argv[0]);
177  rc = 20;
178  break;
179  case CPL_ERROR_ASSIGNING_STREAM:
180  fprintf(stderr, "%s: could not open gnuplot (this tool uses it for "
181  "plotting)!\n", argv[0]);
182  rc = 21;
183  break;
184  default:
185  rc = 50;
186  } /* switch */
187 
188  cpl_table_delete(ts);
189  cpl_table_delete(tt);
190  muse_image_delete(image);
191  cpl_end();
192  return rc;
193 }
194 
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
Definition: muse_image.c:85
cpl_error_code muse_trace_plot_samples(cpl_table *aSamples, cpl_table *aTrace, unsigned short aSlice1, unsigned short aSlice2, muse_image *aImage)
Plotting of trace sample points and solution using gnuplot.
cpl_image * data
the data extension
Definition: muse_image.h:46
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
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:223