GIRAFFE Pipeline Reference Manual

gifibers.c
1 /*
2  * This file is part of the GIRAFFE Pipeline
3  * Copyright (C) 2002-2019 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 
24 #include <cxmemory.h>
25 #include <cxmessages.h>
26 #include <cxstrutils.h>
27 
28 #include <cpl_msg.h>
29 #include <cpl_parameterlist.h>
30 
31 #include "giframe.h"
32 #include "gifiberutils.h"
33 #include "gifibers.h"
34 
35 
71 GiTable *
72 giraffe_fibers_select(const cpl_frame *frame, const GiTable *reference,
73  GiFibersConfig *config)
74 {
75 
76  const cxchar *fctid = "giraffe_fibers_select";
77 
78  const cxchar *filename;
79 
80  cxint nspec = 0;
81  cxint *spectra = NULL;
82 
83  cpl_table *_fibers;
84 
85  GiTable *fibers;
86 
87 
88  if (!frame || !config) {
89  return NULL;
90  }
91 
92  filename = cpl_frame_get_filename(frame);
93  cx_assert(filename != NULL);
94 
95 
96  if (config->spectra && *config->spectra != '\0') {
97 
98  if (strcmp(config->spectra, "setup") == 0) {
99 
100  if (reference != NULL) {
101  spectra = giraffe_create_spectrum_selection(filename, reference,
102  &nspec);
103  }
104 
105  if (!spectra) {
106  cpl_msg_error(fctid, "Invalid fiber setup!");
107  return NULL;
108  }
109 
110  }
111  else {
112 
113  spectra = giraffe_parse_spectrum_selection(config->spectra,
114  &nspec);
115  if (!spectra) {
116  cpl_msg_error(fctid, "Invalid selection string `%s'!",
117  config->spectra);
118  return NULL;
119  }
120 
121  }
122 
123  if (config->nspec > 0) {
124 
125  /*
126  * Both, the number of spectra and a selection list were
127  * given
128  */
129 
130  if (config->nspec < nspec) {
131 
132  spectra = cx_realloc(spectra,
133  config->nspec * sizeof(cxint));
134  nspec = config->nspec;
135 
136  cpl_msg_warning(fctid, "Requested number of spectra (%d) "
137  "is less than number of listed spectra "
138  "(%d). Using %d spectra.", config->nspec,
139  nspec, config->nspec);
140 
141  }
142  else {
143  if (config->nspec > nspec) {
144 
145  cpl_msg_warning(fctid, "Number of requested spectra "
146  "(%d) exceeds the number of listed "
147  "spectra (%d). Using all spectra in "
148  "the list!", config->nspec, nspec);
149 
150  }
151  }
152  }
153  }
154  else {
155 
156  if (config->nspec > 0) {
157 
158  /*
159  * No selection list, but the number of spectra to process
160  * was given.
161  */
162 
163  register cxint i;
164 
165  nspec = config->nspec;
166  spectra = cx_malloc(nspec * sizeof(cxint));
167 
168  /*
169  * Fiber positions in the image are counted starting from 1!
170  */
171 
172  for (i = 0; i < nspec; i++) {
173  spectra[i] = i + 1;
174  }
175  }
176  }
177 
178  _fibers = giraffe_fiberlist_create(filename, nspec, spectra);
179 
180  fibers = giraffe_table_new();
181  giraffe_table_set(fibers, _fibers);
182 
183  cpl_table_delete(_fibers);
184 
185 
186  /*
187  * Cleanup
188  */
189 
190  if (spectra) {
191  cx_free(spectra);
192  }
193 
194  return fibers;
195 
196 }
197 
198 
217 GiTable *
218 giraffe_fibers_setup(const cpl_frame *frame, const cpl_frame *reference)
219 {
220 
221  const cxchar *fctid = "giraffe_fibers_setup";
222 
223  cxchar *filename = NULL;
224 
225  cpl_table *_fibers =NULL;
226 
227  GiTable *fibers = NULL;
228 
229 
230  if (frame == NULL) {
231  cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
232  return NULL;
233  }
234 
235  filename = (cxchar *)cpl_frame_get_filename(frame);
236 
237  if (filename == NULL) {
238  cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
239  return NULL;
240  }
241 
242  _fibers = giraffe_fiberlist_create(filename, 0, NULL);
243 
244  if (_fibers == NULL) {
245  return NULL;
246  }
247 
248  fibers = giraffe_table_new();
249  giraffe_table_set(fibers, _fibers);
250 
251  cpl_table_delete(_fibers);
252  _fibers = NULL;
253 
254 
255  /*
256  * Associate the newly created fiber setup list with a reference list
257  * if it was given.
258  */
259 
260  if (reference != NULL) {
261 
262  cxint status;
263 
264  GiTable *rfibers = 0;
265 
266 
267  filename = (cxchar *)cpl_frame_get_filename(reference);
268 
269  if (filename == NULL) {
270 
271  giraffe_table_delete(fibers);
272  cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
273  return NULL;
274 
275  }
276 
277  rfibers = giraffe_fiberlist_load(filename, 1, GIFRAME_FIBER_SETUP);
278 
279  if (rfibers == NULL) {
280 
281  giraffe_table_delete(fibers);
282  return NULL;
283 
284  }
285 
286  status = giraffe_fiberlist_associate(fibers, rfibers);
287 
288  if (status) {
289  giraffe_table_delete(fibers);
290  giraffe_table_delete(rfibers);
291 
292  return NULL;
293  }
294 
295  giraffe_table_delete(rfibers);
296 
297  }
298 
299  return fibers;
300 
301 }
302 
303 
314 GiFibersConfig *
315 giraffe_fibers_config_create(cpl_parameterlist *list)
316 {
317 
318  cpl_parameter *p;
319 
320  GiFibersConfig *config = NULL;
321 
322 
323  if (!list) {
324  return NULL;
325  }
326 
327  config = cx_calloc(1, sizeof *config);
328 
329 
330  /*
331  * Some defaults
332  */
333 
334  config->nspec = 0;
335  config->spectra = NULL;
336 
337 
338  p = cpl_parameterlist_find(list, "giraffe.fibers.nspectra");
339  config->nspec = cpl_parameter_get_int(p);
340 
341 
342  p = cpl_parameterlist_find(list, "giraffe.fibers.spectra");
343  config->spectra = cx_strdup(cpl_parameter_get_string(p));
344 
345  return config;
346 
347 }
348 
349 
362 void
363 giraffe_fibers_config_destroy(GiFibersConfig *config)
364 {
365 
366  if (config) {
367  if (config->spectra) {
368  cx_free(config->spectra);
369  config->spectra = NULL;
370  }
371 
372  cx_free(config);
373  }
374 
375  return;
376 }
377 
378 
390 void
391 giraffe_fibers_config_add(cpl_parameterlist *list)
392 {
393 
394  cpl_parameter *p;
395 
396 
397  if (!list) {
398  return;
399  }
400 
401  p = cpl_parameter_new_value("giraffe.fibers.spectra",
402  CPL_TYPE_STRING,
403  "Index list of spectra to use for "
404  "localization (e.g. 2,10,30-40,55).",
405  "giraffe.fibers",
406  "");
407  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fiber-splist");
408  cpl_parameterlist_append(list, p);
409 
410  p = cpl_parameter_new_range("giraffe.fibers.nspectra",
411  CPL_TYPE_INT,
412  "Number of spectra to localize.",
413  "giraffe.fibers",
414  0, 0, CX_MAXINT - 1);
415  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fiber-nspec");
416  cpl_parameterlist_append(list, p);
417 
418  return;
419 
420 }
GiTable * giraffe_fibers_select(const cpl_frame *frame, const GiTable *reference, GiFibersConfig *config)
Selects the spectra to process.
Definition: gifibers.c:72
void giraffe_fibers_config_destroy(GiFibersConfig *config)
Destroys a fibers setup structure.
Definition: gifibers.c:363
void giraffe_fibers_config_add(cpl_parameterlist *list)
Adds parameters for the spectrum selection.
Definition: gifibers.c:391
GiFibersConfig * giraffe_fibers_config_create(cpl_parameterlist *list)
Creates a setup structure for the fiber selection.
Definition: gifibers.c:315
GiTable * giraffe_fibers_setup(const cpl_frame *frame, const cpl_frame *reference)
Setup a fiber list.
Definition: gifibers.c:218
cxint * giraffe_create_spectrum_selection(const cxchar *filename, const GiTable *reference, cxint *nspec)
Create a spectrum selection from a reference table.
cxint * giraffe_parse_spectrum_selection(const cxchar *selection, cxint *nspec)
Parses a spectrum selection string.
cpl_table * giraffe_fiberlist_create(const cxchar *filename, cxint nspec, const cxint *spectra)
Creates the fiber table.
Definition: gifiberutils.c:85
GiTable * giraffe_fiberlist_load(const cxchar *filename, cxint dataset, const cxchar *tag)
Load a fiber table from a file.
Definition: gifiberutils.c:715
cxint giraffe_fiberlist_associate(GiTable *fibers, const GiTable *reference)
Associate a fiberlist with a reference list.
Definition: gifiberutils.c:983
cxint giraffe_table_set(GiTable *self, cpl_table *table)
Sets the table data.
Definition: gitable.c:456
GiTable * giraffe_table_new(void)
Creates a new, empty Giraffe table.
Definition: gitable.c:85
void giraffe_table_delete(GiTable *self)
Destroys a Giraffe table.
Definition: gitable.c:154

This file is part of the GIRAFFE Pipeline Reference Manual 2.16.10.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Thu Dec 15 2022 21:18:51 by doxygen 1.9.1 written by Dimitri van Heesch, © 1997-2004