MUSE Pipeline Reference Manual  0.18.5
muse_pixtable_erase_slice.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) 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 /*----------------------------------------------------------------------------*/
51 /*----------------------------------------------------------------------------*/
52 
55 #define PRINT_USAGE(rc) \
56  fprintf(stderr, "Usage: %s PIXTABLE_IN IFU SLICE PIXTABLE_OUT\n", argv[0]); \
57  cpl_end(); return (rc);
58 
59 /*---------------------------------------------------------------------------*/
71 /*---------------------------------------------------------------------------*/
72 static cpl_error_code
74 {
75  cpl_ensure_code(aPixtable && aPixtable->header && aPixtable->table,
76  CPL_ERROR_NULL_INPUT);
77  if (cpl_table_count_selected(aPixtable->table) < 1) {
78  return CPL_ERROR_NONE; /* nothing to do */
79  }
80  cpl_array *sel = cpl_table_where_selected(aPixtable->table);
81  cpl_size narray = cpl_array_get_size(sel),
82  nselprev = 0, /* selected ones in previous exposures */
83  ifst = 0, ilst = 0;
84  const cpl_size *asel = cpl_array_get_data_cplsize_const(sel);
85  unsigned int nexp = 0;
86  do {
87  /* get exposure range for (next) exposure */
88  char *kwfst = cpl_sprintf(MUSE_HDR_PT_EXP_FST, ++nexp),
89  *kwlst = cpl_sprintf(MUSE_HDR_PT_EXP_LST, nexp);
90  if (!cpl_propertylist_has(aPixtable->header, kwfst) ||
91  !cpl_propertylist_has(aPixtable->header, kwlst)) {
92  cpl_free(kwfst);
93  cpl_free(kwlst);
94  break; /* nothing (more) to do */
95  }
96  ifst = cpl_propertylist_get_long_long(aPixtable->header, kwfst);
97  ilst = cpl_propertylist_get_long_long(aPixtable->header, kwlst);
98  /* count selected */
99  cpl_size i, nsel = 0;
100  for (i = 0; i < narray; i++) {
101  if (asel[i] >= ifst && asel[i] <= ilst) {
102  nsel++;
103  } /* if in range of this exposure */
104  } /* for i (all array entries) */
105  cpl_size ifst2 = ifst - nselprev,
106  ilst2 = ilst - nsel - nselprev;
107  cpl_msg_debug(__func__, "exp %d old %"CPL_SIZE_FORMAT"..%"CPL_SIZE_FORMAT
108  ", %"CPL_SIZE_FORMAT" selected (previous: %"CPL_SIZE_FORMAT
109  "), new %"CPL_SIZE_FORMAT"..%"CPL_SIZE_FORMAT, nexp,
110  ifst, ilst, nsel, nselprev, ifst2, ilst2);
111  /* finally update the header entries */
112  muse_cplpropertylist_update_long_long(aPixtable->header, kwfst, ifst2);
113  muse_cplpropertylist_update_long_long(aPixtable->header, kwlst, ilst2);
114  cpl_free(kwfst);
115  cpl_free(kwlst);
116  nselprev += nsel; /* add selected ones for previous exposures */
117  } while (ilst >= ifst);
118  cpl_array_delete(sel);
119  return CPL_ERROR_NONE;
120 } /* muse_pixtable_fix_exp_headers() */
121 
122 /*---------------------------------------------------------------------------*/
141 /*---------------------------------------------------------------------------*/
142 static cpl_error_code
143 muse_pixtable_erase_ifu_slice(muse_pixtable *aPixtable, unsigned char aIFU,
144  unsigned short aSlice)
145 {
146  cpl_ensure_code(aPixtable, CPL_ERROR_NULL_INPUT);
147  cpl_size nrow = muse_pixtable_get_nrow(aPixtable);
148  cpl_ensure_code(nrow > 0, CPL_ERROR_DATA_NOT_FOUND);
149 
150  cpl_table_unselect_all(aPixtable->table);
151  uint32_t *origin = (uint32_t *)cpl_table_get_data_int(aPixtable->table,
152  MUSE_PIXTABLE_ORIGIN);
153  cpl_size irow;
154  for (irow = 0; irow < nrow; irow++) {
155  unsigned char ifu = muse_pixtable_origin_get_ifu(origin[irow]);
156  unsigned short slice = muse_pixtable_origin_get_slice(origin[irow]);
157  if (ifu == aIFU && slice == aSlice) {
158  cpl_table_select_row(aPixtable->table, irow);
159  } /* if same IFU and slice */
160  } /* for irow (all pixtable rows) */
161  cpl_size nsel = cpl_table_count_selected(aPixtable->table);
162  cpl_error_code rc = cpl_table_erase_selected(aPixtable->table);
163  cpl_msg_debug(__func__, "Erased %"CPL_SIZE_FORMAT" rows from pixel table",
164  nsel);
165 
167  muse_pixtable_compute_limits(aPixtable);
168  return rc;
169 } /* muse_pixtable_erase_ifu_slice() */
170 
171 int main(int argc, char **argv)
172 {
173  cpl_init(CPL_INIT_DEFAULT);
174  cpl_errorstate prestate = cpl_errorstate_get();
175 
176  if (argc != 5) {
177  /* four arguments are needed */
178  PRINT_USAGE(1);
179  }
180 
181  char *tiname = NULL, /* input table */
182  *toname = NULL; /* output table */
183  unsigned char ifu = 0;
184  unsigned short slice = 0;
185  int i;
186 
187  /* argument processing */
188  for (i = 1; i < argc; i++) {
189  if (tiname && toname) {
190  break; /* we have the required name, skip the rest */
191  }
192  if (!tiname) {
193  tiname = argv[i]; /* set the name for the input table */
194  } else if (!ifu) {
195  ifu = atoi(argv[i]);
196  } else if (!slice) {
197  slice = atoi(argv[i]);
198  } else {
199  toname = argv[i]; /* set the name for the output table */
200  }
201  } /* for i (all arguments) */
202  if (ifu == 0 || ifu > kMuseNumIFUs) {
203  printf("Illegal IFU number %hhu given\n", ifu);
204  PRINT_USAGE(2);
205  }
206  if (slice == 0 || slice > kMuseSlicesPerCCD) {
207  printf("Illegal slice number %hhu given\n", slice);
208  PRINT_USAGE(3);
209  }
210 
211  muse_pixtable *table = muse_pixtable_load(tiname);
212  if (!table) {
213  PRINT_USAGE(10);
214  }
215  cpl_size nrow = muse_pixtable_get_nrow(table);
216  printf("Loaded pixel table \"%s\" with %"CPL_SIZE_FORMAT" rows\n",
217  tiname, nrow);
218 
219  cpl_error_code rc = muse_pixtable_erase_ifu_slice(table, ifu, slice);
220  cpl_size nrow2 = muse_pixtable_get_nrow(table);
221  if (rc == CPL_ERROR_NONE) {
222  printf("Erased slice %hu of IFU %hhu (%"CPL_SIZE_FORMAT" pixels removed)\n",
223  slice, ifu, nrow - nrow2);
224  } else {
225  printf("Error while erasing slice %hu of IFU %hhu: %s\n", slice, ifu,
226  cpl_error_get_message());
227  }
228 
229  muse_pixtable_save(table, toname);
230  printf("MUSE pixel table \"%s\" (%"CPL_SIZE_FORMAT" rows) saved\n", toname,
231  nrow2);
232  muse_pixtable_delete(table);
233  rc = cpl_errorstate_is_equal(prestate) ? 0 : 50;
234  if (rc) {
235  cpl_errorstate_dump(prestate, CPL_FALSE, muse_cplerrorstate_dump_some);
236  }
237  cpl_end();
238  return rc;
239 }
240 
#define MUSE_HDR_PT_EXP_FST
FITS header keyword defining the first row index for a given exposure.
Definition: muse_pixtable.h:85
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.
cpl_size muse_pixtable_get_nrow(muse_pixtable *aPixtable)
get the number of rows within the pixel table
static cpl_error_code muse_pixtable_erase_ifu_slice(muse_pixtable *aPixtable, unsigned char aIFU, unsigned short aSlice)
Erase pixel table rows related to one slice of one IFU.
#define MUSE_HDR_PT_EXP_LST
FITS header keyword defining the last row index for a given exposure.
Definition: muse_pixtable.h:90
static cpl_error_code muse_pixtable_fix_exp_headers(muse_pixtable *aPixtable)
Fix the exposure ranges in the header of a pixel table.
cpl_table * table
The pixel table.
Structure definition of MUSE pixel table.
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
cpl_error_code muse_cplpropertylist_update_long_long(cpl_propertylist *aHeader, const char *aKeyword, cpl_size aValue)
Update an integer-like property irrespective of the real type.
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.
void muse_pixtable_delete(muse_pixtable *aPixtable)
Deallocate memory associated to a pixel table object.
cpl_error_code muse_pixtable_compute_limits(muse_pixtable *aPixtable)
(Re-)Compute the limits of the coordinate columns of a pixel table.
cpl_propertylist * header
The FITS header.