MUSE Pipeline Reference Manual  1.0.2
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 /*----------------------------------------------------------------------------*/
65 /*----------------------------------------------------------------------------*/
66 
69 #define PRINT_USAGE(rc) \
70  fprintf(stderr, "Usage: %s [ -l1 lambda1 ] [ -l2 lambda2 ] [ -x1 xpos1 ] " \
71  "[ -x2 xpos2 ] [ -y1 ypos1 ] [ -y2 ypos2 ] PIXTABLE_IN " \
72  "PIXTABLE_OUT\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 *tiname = NULL, /* input table */
85  *toname = NULL; /* output table */
86  /* default to low and high values that mean to not crop anything */
87  double x1 = -FLT_MAX, x2 = FLT_MAX,
88  y1 = -FLT_MAX, y2 = FLT_MAX,
89  l1 = -FLT_MAX, l2 = FLT_MAX;
90  int i;
91 
92  /* argument processing */
93  for (i = 1; i < argc; i++) {
94  if (strncmp(argv[i], "-x1", 4) == 0) {
95  /* skip to next arg to get start value */
96  i++;
97  if (i < argc) {
98  x1 = atof(argv[i]);
99  } else {
100  PRINT_USAGE(2);
101  }
102  } else if (strncmp(argv[i], "-x2", 4) == 0) {
103  i++;
104  if (i < argc) {
105  x2 = atof(argv[i]);
106  } else {
107  PRINT_USAGE(2);
108  }
109  } else if (strncmp(argv[i], "-y1", 4) == 0) {
110  i++;
111  if (i < argc) {
112  y1 = atof(argv[i]);
113  } else {
114  PRINT_USAGE(3);
115  }
116  } else if (strncmp(argv[i], "-y2", 4) == 0) {
117  i++;
118  if (i < argc) {
119  y2 = atof(argv[i]);
120  } else {
121  PRINT_USAGE(3);
122  }
123  } else if (strncmp(argv[i], "-l1", 4) == 0) {
124  /* skip to next arg to get start value */
125  i++;
126  if (i < argc) {
127  l1 = atof(argv[i]);
128  } else {
129  PRINT_USAGE(4);
130  }
131  } else if (strncmp(argv[i], "-l2", 4) == 0) {
132  i++;
133  if (i < argc) {
134  l2 = atof(argv[i]);
135  } else {
136  PRINT_USAGE(4);
137  }
138  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
139  PRINT_USAGE(9);
140  } else {
141  if (tiname && toname) {
142  break; /* we have the required name, skip the rest */
143  }
144  if (!tiname) {
145  tiname = argv[i]; /* set the name for the input table */
146  } else {
147  toname = argv[i]; /* set the name for the output table */
148  }
149  }
150  } /* for i (all arguments) */
151  if (!tiname || !toname) {
152  PRINT_USAGE(1);
153  }
154 
155  cpl_msg_set_level(CPL_MSG_WARNING); /* swallow INFO output */
156  muse_pixtable *table = muse_pixtable_load(tiname);
157  if (!table) {
158  PRINT_USAGE(10);
159  }
160 
161  /* Limit crop ranges to what's in the pixel table, but do this for display *
162  * only. Otherwise it might cause the muse_pixtable_restrict_*() functions *
163  * to do more work than necessary, because float/double conversions may *
164  * the less/greater comparisons in those functionts to be false. */
165  double ll = fmax(l1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_LLO)),
166  lu = fmin(l2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_LHI)),
167  xl = fmax(x1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_XLO)),
168  xu = fmin(x2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_XHI)),
169  yl = fmax(y1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_YLO)),
170  yu = fmin(y2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_YHI));
171 
172  printf("MUSE pixel table \"%s\" (%"CPL_SIZE_FORMAT" rows)\n"
173  " cropping to lambda = %.2f..%.2f Angstrom\n"
174  " xpos = %.3e..%.3e %s\n"
175  " ypos = %.3e..%.3e %s\n",
176  tiname, muse_pixtable_get_nrow(table), ll, lu,
177  xl, xu, cpl_table_get_column_unit(table->table, MUSE_PIXTABLE_XPOS),
178  yl, yu, cpl_table_get_column_unit(table->table, MUSE_PIXTABLE_YPOS));
179  /* ignore the returns of these functions, they only test for NULL input */
180  muse_pixtable_restrict_wavelength(table, l1, l2);
181  muse_pixtable_restrict_xpos(table, x1, x2);
182  muse_pixtable_restrict_ypos(table, y1, y2);
183  cpl_error_code rc = muse_pixtable_save(table, toname);
184  switch (rc) {
185  case CPL_ERROR_NONE:
186  rc = 0;
187  break;
188  default:
189  rc = 50;
190  } /* switch */
191  printf("MUSE pixel table \"%s\" (%"CPL_SIZE_FORMAT" rows) saved\n",
192  toname, muse_pixtable_get_nrow(table));
193 
194  muse_pixtable_delete(table);
195  cpl_end();
196  return rc;
197 }
198 
muse_pixtable * muse_pixtable_load(const char *aFilename)
Load the table itself and the FITS headers of a MUSE pixel table from a file.
#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.
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.