MUSE Pipeline Reference Manual  2.1.1
muse_wave_plot_residuals.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 /*----------------------------------------------------------------------------*/
67 /*----------------------------------------------------------------------------*/
68 
71 #define PRINT_USAGE(rc) \
72  fprintf(stderr, "Usage: %s [ -n nifu ] [ -s slice ] [ -i iteration ] [ -l ] "\
73  "[ -c c1 c2 ] WAVECAL_RESIDUALS\n", argv[0]); \
74  cpl_end(); return (rc);
75 
76 int main(int argc, char **argv)
77 {
78  cpl_init(CPL_INIT_DEFAULT);
80 
81  if (argc <= 1) {
82  /* filename is needed at least */
83  PRINT_USAGE(1);
84  }
85 
86  char *tname = NULL;
87  unsigned char nifu = 0; /* IFU number to load */
88  unsigned short slice = 0;
89  unsigned int iteration = 0;
90  cpl_boolean lambda = CPL_FALSE;
91  cpl_vector *cuts = NULL;
92 
93  /* argument processing */
94  int i;
95  for (i = 1; i < argc; i++) {
96  if (strncmp(argv[i], "-s", 3) == 0) {
97  /* skip to next arg to get slice number */
98  i++;
99  if (i < argc) {
100  slice = atol(argv[i]);
101  } else {
102  PRINT_USAGE(2);
103  }
104  } else if (strncmp(argv[i], "-i", 3) == 0) {
105  /* skip to next arg to get iteration value */
106  i++;
107  if (i < argc) {
108  iteration = atol(argv[i]);
109  } else {
110  cpl_vector_delete(cuts);
111  PRINT_USAGE(3);
112  }
113  } else if (strncmp(argv[i], "-c", 3) == 0) {
114  cuts = cpl_vector_new(2);
115  /* skip to next arg to get iteration value */
116  i++;
117  if (i + 1 < argc) {
118  cpl_vector_set(cuts, 0, atof(argv[i++]));
119  cpl_vector_set(cuts, 1, atof(argv[i]));
120  } else {
121  PRINT_USAGE(4);
122  }
123  } else if (strncmp(argv[i], "-n", 3) == 0) {
124  /* skip to next arg to get extension name */
125  i++;
126  if (i < argc) {
127  nifu = atoi(argv[i]);
128  if (nifu == 0 || nifu > kMuseNumIFUs) {
129  PRINT_USAGE(6);
130  }
131  } else {
132  PRINT_USAGE(5);
133  }
134  } else if (strncmp(argv[i], "-l", 3) == 0) {
135  lambda = CPL_TRUE;
136  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
137  PRINT_USAGE(9);
138  } else {
139  tname = argv[i];
140  break; /* we have the required name, skip the rest */
141  }
142  }
143 
144  int iext = 1;
145  if (nifu) {
146  iext = muse_utils_get_extension_for_ifu(tname, nifu);
147  }
148  cpl_table *table = cpl_table_load(tname, iext, 1);
149  if (!table) {
150  PRINT_USAGE(10);
151  }
152  /* get extension names for the output messages */
153  char *extname = NULL;
154  cpl_propertylist *header = cpl_propertylist_load(tname, iext);
155  if (cpl_propertylist_has(header, "EXTNAME")) {
156  extname = cpl_sprintf("[%s]", muse_pfits_get_extname(header));
157  }
158  cpl_propertylist_delete(header);
159 
160  printf("MUSE WAVECAL_RESIDUALS table \"%s%s\", contains %"CPL_SIZE_FORMAT
161  " rows\n", tname, extname ? extname : "", cpl_table_get_nrow(table));
162  cpl_error_code rc = muse_wave_plot_residuals(table, nifu, slice, iteration,
163  lambda, cuts);
164  cpl_vector_delete(cuts);
165  switch (rc) {
166  case CPL_ERROR_NONE:
167  rc = 0;
168  break;
169  case CPL_ERROR_ILLEGAL_INPUT:
170  fprintf(stderr, "%s: \"%s%s\" does not seem to contain a MUSE wavelength "
171  "calibration residuals table!\n", argv[0], tname,
172  extname ? extname : "");
173  rc = 11;
174  break;
175  case CPL_ERROR_DATA_NOT_FOUND:
176  if (iteration > 0) {
177  fprintf(stderr, "%s: \"%s%s\" does not seem to contain data for slice %d "
178  "and iteration %d!\n", argv[0], tname, extname ? extname : "",
179  slice, iteration);
180  } else {
181  fprintf(stderr, "%s: \"%s%s\" does not seem to contain data for slice %d "
182  "and the last iteration!\n", argv[0], tname,
183  extname ? extname : "", slice);
184  }
185  rc = 12;
186  break;
187  case CPL_ERROR_UNSUPPORTED_MODE:
188  fprintf(stderr, "%s: your platform does not seem to support pipes "
189  "[popen()/pclose()]!\n", argv[0]);
190  rc = 20;
191  break;
192  case CPL_ERROR_ASSIGNING_STREAM:
193  fprintf(stderr, "%s: could not open gnuplot (this tool uses it for "
194  "plotting)!\n", argv[0]);
195  rc = 21;
196  break;
197  default:
198  rc = 50;
199  } /* switch */
200  cpl_free(extname);
201 
202  cpl_table_delete(table);
203  cpl_end();
204  return rc;
205 }
206 
cpl_error_code muse_wave_plot_residuals(cpl_table *aTable, unsigned char aIFU, unsigned short aSlice, unsigned int aIter, cpl_boolean aPlotLambda, cpl_vector *aCuts)
Fancy plotting of wavelength calibration residuals (color coded over x/y-position) using gnuplot...
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
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.