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