MUSE Pipeline Reference Manual  2.1.1
muse_pixtable_crop.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 /*----------------------------------------------------------------------------*/
71 /*----------------------------------------------------------------------------*/
72 
75 #define PRINT_USAGE(rc) \
76  fprintf(stderr, "Usage: %s [ -l1 lambda1 ] [ -l2 lambda2 ] [ -x1 xpos1 ] " \
77  "[ -x2 xpos2 ] [ -y1 ypos1 ] [ -y2 ypos2 ] [ -i IFU ] [ -s slice ] " \
78  "PIXTABLE_IN PIXTABLE_OUT\n", argv[0]); \
79  cpl_end(); return (rc);
80 
81 int main(int argc, char **argv)
82 {
83  cpl_init(CPL_INIT_DEFAULT);
85 
86  if (argc <= 1) {
87  /* filename is needed at least */
88  PRINT_USAGE(1);
89  }
90 
91  char *tiname = NULL, /* input table */
92  *toname = NULL; /* output table */
93  /* default to low and high values that mean to not crop anything */
94  double x1 = -FLT_MAX, x2 = FLT_MAX,
95  y1 = -FLT_MAX, y2 = FLT_MAX,
96  l1 = -FLT_MAX, l2 = FLT_MAX;
97  unsigned char ifu = 0;
98  unsigned short slice = 0;
99  int i;
100 
101  /* argument processing */
102  for (i = 1; i < argc; i++) {
103  if (strncmp(argv[i], "-x1", 4) == 0) {
104  /* skip to next arg to get start value */
105  i++;
106  if (i < argc) {
107  x1 = atof(argv[i]);
108  } else {
109  PRINT_USAGE(2);
110  }
111  } else if (strncmp(argv[i], "-x2", 4) == 0) {
112  i++;
113  if (i < argc) {
114  x2 = atof(argv[i]);
115  } else {
116  PRINT_USAGE(2);
117  }
118  } else if (strncmp(argv[i], "-y1", 4) == 0) {
119  i++;
120  if (i < argc) {
121  y1 = atof(argv[i]);
122  } else {
123  PRINT_USAGE(3);
124  }
125  } else if (strncmp(argv[i], "-y2", 4) == 0) {
126  i++;
127  if (i < argc) {
128  y2 = atof(argv[i]);
129  } else {
130  PRINT_USAGE(3);
131  }
132  } else if (strncmp(argv[i], "-l1", 4) == 0) {
133  /* skip to next arg to get start value */
134  i++;
135  if (i < argc) {
136  l1 = atof(argv[i]);
137  } else {
138  PRINT_USAGE(4);
139  }
140  } else if (strncmp(argv[i], "-l2", 4) == 0) {
141  i++;
142  if (i < argc) {
143  l2 = atof(argv[i]);
144  } else {
145  PRINT_USAGE(4);
146  }
147  } else if (strncmp(argv[i], "-i", 3) == 0) {
148  i++;
149  if (i < argc) {
150  ifu = atoi(argv[i]);
151  } else {
152  PRINT_USAGE(5);
153  }
154  } else if (strncmp(argv[i], "-s", 3) == 0) {
155  i++;
156  if (i < argc) {
157  slice = atoi(argv[i]);
158  } else {
159  PRINT_USAGE(6);
160  }
161  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
162  PRINT_USAGE(9);
163  } else {
164  if (tiname && toname) {
165  break; /* we have the required name, skip the rest */
166  }
167  if (!tiname) {
168  tiname = argv[i]; /* set the name for the input table */
169  } else {
170  toname = argv[i]; /* set the name for the output table */
171  }
172  }
173  } /* for i (all arguments) */
174  if (!tiname || !toname) {
175  PRINT_USAGE(1);
176  }
177 
178  cpl_msg_set_level(CPL_MSG_WARNING); /* swallow INFO output */
179  muse_pixtable *table = muse_pixtable_load(tiname);
180  if (!table) {
181  PRINT_USAGE(10);
182  }
183 
184  /* Limit crop ranges to what's in the pixel table, but do this for display *
185  * only. Otherwise it might cause the muse_pixtable_restrict_*() functions *
186  * to do more work than necessary, because float/double conversions may *
187  * the less/greater comparisons in those functionts to be false. */
188  double ll = fmax(l1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_LLO)),
189  lu = fmin(l2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_LHI)),
190  xl = fmax(x1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_XLO)),
191  xu = fmin(x2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_XHI)),
192  yl = fmax(y1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_YLO)),
193  yu = fmin(y2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_YHI));
194 
195  printf("MUSE pixel table \"%s\" (%"CPL_SIZE_FORMAT" rows)\n"
196  " cropping to lambda = %.2f..%.2f Angstrom\n"
197  " xpos = %.3e..%.3e %s\n"
198  " ypos = %.3e..%.3e %s\n",
199  tiname, muse_pixtable_get_nrow(table), ll, lu,
200  xl, xu, cpl_table_get_column_unit(table->table, MUSE_PIXTABLE_XPOS),
201  yl, yu, cpl_table_get_column_unit(table->table, MUSE_PIXTABLE_YPOS));
202  /* ignore the returns of these functions, they only test for NULL input */
203  muse_pixtable_restrict_wavelength(table, l1, l2);
204  muse_pixtable_restrict_xpos(table, x1, x2);
205  muse_pixtable_restrict_ypos(table, y1, y2);
206 
207  /* possibly select by IFU and/or slice */
208  if (ifu >= 1 && ifu <= kMuseNumIFUs) {
209  printf(" selecting pixels from IFU %02hhu ", ifu);
210  cpl_table_unselect_all(table->table);
211  const int *origin = cpl_table_get_data_int_const(table->table,
213  cpl_size irow, nrow = muse_pixtable_get_nrow(table);
214  for (irow = 0; irow < nrow; irow++) {
215  if (muse_pixtable_origin_get_ifu(origin[irow]) != ifu) {
216  cpl_table_select_row(table->table, irow);
217  } /* if */
218  } /* for irow */
219  printf("(%d of %d)\n", (int)cpl_table_count_selected(table->table), (int)nrow);
220  cpl_table_erase_selected(table->table);
221  } /* if ifu */
222  if (slice >= 1 && slice <= kMuseSlicesPerCCD) {
223  printf(" selecting pixels from slice %02hu ", slice);
224  cpl_table_unselect_all(table->table);
225  const int *origin = cpl_table_get_data_int_const(table->table,
227  cpl_size irow, nrow = muse_pixtable_get_nrow(table);
228  for (irow = 0; irow < nrow; irow++) {
229  if (muse_pixtable_origin_get_slice(origin[irow]) != slice) {
230  cpl_table_select_row(table->table, irow);
231  } /* if */
232  } /* for irow */
233  printf("(%d of %d)\n", (int)cpl_table_count_selected(table->table), (int)nrow);
234  cpl_table_erase_selected(table->table);
235  } /* if slice */
236 
237  /* all selection done, save and clean up */
238  cpl_error_code rc = muse_pixtable_save(table, toname);
239  switch (rc) {
240  case CPL_ERROR_NONE:
241  rc = 0;
242  break;
243  default:
244  rc = 50;
245  } /* switch */
246  printf("MUSE pixel table \"%s\" (%"CPL_SIZE_FORMAT" rows) saved\n",
247  toname, muse_pixtable_get_nrow(table));
248 
249  muse_pixtable_delete(table);
250  cpl_end();
251  return rc;
252 }
253 
#define MUSE_PIXTABLE_XPOS
Definition: muse_pixtable.h:51
muse_pixtable * muse_pixtable_load(const char *aFilename)
Load the table itself and the FITS headers of a MUSE pixel table from a file.
unsigned short muse_pixtable_origin_get_slice(uint32_t aOrigin)
Get the slice number from the encoded 32bit origin number.
#define MUSE_HDR_PT_XLO
FITS header keyword contains the lower limit of the data in x-direction.
cpl_size muse_pixtable_get_nrow(const muse_pixtable *aPixtable)
get the number of rows within the pixel table
#define MUSE_HDR_PT_LHI
FITS header keyword contains the upper limit of the data in spectral direction.
cpl_table * table
The pixel table.
cpl_error_code muse_pixtable_restrict_wavelength(muse_pixtable *aPixtable, double aLow, double aHigh)
Restrict a pixel table to a certain wavelength range.
Structure definition of MUSE pixel table.
#define MUSE_HDR_PT_YLO
FITS header keyword contains the lower limit of the data in y-direction.
#define MUSE_PIXTABLE_ORIGIN
Definition: muse_pixtable.h:54
#define MUSE_HDR_PT_LLO
FITS header keyword contains the lower limit of the data in spectral direction.
cpl_error_code muse_pixtable_save(muse_pixtable *aPixtable, const char *aFilename)
Save a MUSE pixel table to a file on disk.
unsigned short muse_pixtable_origin_get_ifu(uint32_t aOrigin)
Get the IFU number from the encoded 32bit origin number.
cpl_error_code muse_pixtable_restrict_xpos(muse_pixtable *aPixtable, double aLo, double aHi)
Restrict a pixel table to a certain x coordinate range.
#define MUSE_PIXTABLE_YPOS
Definition: muse_pixtable.h:52
void muse_pixtable_delete(muse_pixtable *aPixtable)
Deallocate memory associated to a pixel table object.
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.
cpl_error_code muse_pixtable_restrict_ypos(muse_pixtable *aPixtable, double aLo, double aHi)
Restrict a pixel table to a certain y coordinate range.
#define MUSE_HDR_PT_YHI
FITS header keyword contains the upper limit of the data in y-direction.
cpl_propertylist * header
The FITS header.
#define MUSE_HDR_PT_XHI
FITS header keyword contains the upper limit of the data in x-ion.