GIRAFFE Pipeline Reference Manual

gitransmission.c
1 /* $Id$
2  *
3  * This file is part of the GIRAFFE Pipeline
4  * Copyright (C) 2002-2006 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author$
23  * $Date$
24  * $Revision$
25  * $Name$
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 #include <math.h>
33 
34 #include <cxmemory.h>
35 #include <cxmessages.h>
36 #include <cxstrutils.h>
37 
38 #include <cpl_msg.h>
39 #include <cpl_parameterlist.h>
40 #include <cpl_propertylist.h>
41 #include <cpl_image.h>
42 
43 #include "gialias.h"
44 #include "gierror.h"
45 #include "gimessages.h"
46 #include "gifiberutils.h"
47 #include "gigrating.h"
48 #include "giwlsolution.h"
49 #include "giextraction.h"
50 #include "girebinning.h"
51 #include "gitransmission.h"
52 
53 
62 inline static cxint
63 _giraffe_transmission_apply(cpl_image *spectra, cpl_table *fibers)
64 {
65 
66  cxint i;
67  cxint nx = 0;
68  cxint ny = 0;
69 
70 
71  cxdouble *pixels = NULL;
72 
73 
74  cx_assert(spectra != NULL);
75  cx_assert(fibers != NULL);
76 
77  nx = cpl_image_get_size_x(spectra);
78  ny = cpl_image_get_size_y(spectra);
79 
80  pixels = cpl_image_get_data(spectra);
81 
82  if (pixels == NULL) {
83  return 1;
84  }
85 
86 
87  /*
88  * For each fiber spectrum each wavelength resolution element is
89  * corrected for the relative fiber transmission effects.
90  */
91 
92  for (i = 0; i < cpl_table_get_nrow(fibers); i++) {
93 
94  register cxint j;
95 
96  register cxdouble tc = cpl_table_get_double(fibers, "TRANSMISSION",
97  i, NULL);
98 
99 
100  for (j = 0; j < ny; j++) {
101  pixels[j * nx + i] /= tc;
102  }
103 
104  }
105 
106  return 0;
107 
108 }
109 
110 
111 cxint
112 giraffe_transmission_compute(GiExtraction *extraction, GiTable *fibers,
113  GiLocalization *localization,
114  GiTable *wcalcoeff, GiTable *grating,
115  GiTable *slitgeometry)
116 {
117 
118  const cxchar *idx = NULL;
119 
120  cxint i;
121  cxint pos = 0;
122  cxint status = 0;
123 
124  cxdouble peak = 0.;
125  cxdouble *flux = NULL;
126  cxdouble *error = NULL;
127 
128  cpl_image *tflux = NULL;
129  cpl_image *tvariance = NULL;
130  cpl_image *variance = NULL;
131 
132  cpl_table *_fibers = NULL;
133 
134  GiImage *spectra = NULL;
135 
136  GiTable *_wcalcoeff = NULL;
137 
138  GiRebinning *fspectra = giraffe_rebinning_new();
139 
140  GiRebinConfig rebin_config = {
141  GIREBIN_METHOD_LINEAR,
142  TRUE,
143  0.005,
144  GIREBIN_SCALE_LINEAR,
145  0,
146  GIREBIN_RANGE_COMMON
147  };
148 
149 
150  if (extraction == NULL) {
151  return 1;
152  }
153 
154  if (extraction->spectra == NULL || extraction->error == NULL) {
155  return 1;
156  }
157 
158  spectra = extraction->spectra;
159 
160 
161  /*
162  * If no wavelength solution table was provided create a default one.
163  * The wavelength solution residuals are not available in this case
164  * and are therefore disabled for the rebinning.
165  */
166 
167  if (wcalcoeff == NULL) {
168 
169  cxint nx = 0;
170 
171  cxdouble pixsize = 0.;
172 
173  cpl_propertylist *properties = giraffe_image_get_properties(spectra);
174 
175  cpl_image *_spectra = giraffe_image_get(spectra);
176 
177  GiGrating *setup = NULL;
178 
179  GiWlSolution *solution = NULL;
180 
181 
182  if (!cpl_propertylist_has(properties, GIALIAS_PIXSIZX)) {
183 
184  giraffe_rebinning_delete(fspectra);
185  return 1;
186 
187  }
188  else {
189 
190  pixsize = cpl_propertylist_get_double(properties,
191  GIALIAS_PIXSIZX);
192  pixsize /= 1000.;
193 
194  }
195 
196  nx = cpl_image_get_size_y(_spectra);
197 
198  setup = giraffe_grating_create(spectra, grating);
199 
200  if (setup == NULL) {
201 
202  giraffe_rebinning_delete(fspectra);
203  return 1;
204 
205  }
206 
207  solution = giraffe_wlsolution_new("xoptmod2", 1, nx, pixsize, setup);
208 
209  if (solution == NULL) {
210 
211  giraffe_grating_delete(setup);
212  giraffe_rebinning_delete(fspectra);
213 
214  return 1;
215 
216  }
217 
218  _wcalcoeff = giraffe_wlsolution_create_table(solution);
219 
220  if (_wcalcoeff == NULL) {
221 
222  giraffe_wlsolution_delete(solution);
223  giraffe_grating_delete(setup);
224  giraffe_rebinning_delete(fspectra);
225 
226  return 1;
227  }
228 
229  giraffe_grating_delete(setup);
230  setup = NULL;
231 
232  giraffe_wlsolution_delete(solution);
233  solution = NULL;
234 
235  rebin_config.xresiduals = FALSE;
236 
237  wcalcoeff = _wcalcoeff;
238 
239  }
240 
241 
242  status = giraffe_rebin_spectra(fspectra, extraction, fibers,
243  localization, grating, slitgeometry,
244  wcalcoeff, &rebin_config);
245 
246  if (status) {
247 
248  if (_wcalcoeff != NULL) {
249  giraffe_table_delete(_wcalcoeff);
250  _wcalcoeff = NULL;
251  }
252 
253  giraffe_rebinning_delete(fspectra);
254  fspectra = NULL;
255 
256  return 1;
257 
258  }
259 
260  if (_wcalcoeff != NULL) {
261  giraffe_table_delete(_wcalcoeff);
262  _wcalcoeff = NULL;
263  }
264 
265 
266  tflux = cpl_image_collapse_create(giraffe_image_get(fspectra->spectra),
267  0);
268 
269  if (tflux == NULL) {
270  giraffe_rebinning_delete(fspectra);
271  return 1;
272  }
273 
274 
275  /*
276  * The spectrum errors are the standard deviation. i.e. to get the
277  * error of the total flux the variances have to be summed.
278  */
279 
280  variance = cpl_image_power_create(giraffe_image_get(fspectra->errors),
281  2.);
282 
283  if (variance == NULL) {
284  cpl_image_delete(tflux);
285  tflux = NULL;
286 
287  giraffe_rebinning_delete(fspectra);
288  fspectra = NULL;
289 
290  return 1;
291  }
292 
293  tvariance = cpl_image_collapse_create(variance, 0);
294 
295  if (tvariance == NULL) {
296  cpl_image_delete(variance);
297  variance = NULL;
298 
299  cpl_image_delete(tflux);
300  tflux = NULL;
301 
302  giraffe_rebinning_delete(fspectra);
303  fspectra = NULL;
304 
305  return 1;
306  }
307 
308 
309  cpl_image_delete(variance);
310  variance = NULL;
311 
312  _fibers = giraffe_table_get(fibers);
313  idx = giraffe_fiberlist_query_index(_fibers);
314 
315  flux = cpl_image_get_data(tflux);
316 
317  for (i = 0; i < cpl_table_get_nrow(_fibers); i++) {
318 
319  register cxint rp = cpl_table_get_int(_fibers, "RP", i, NULL);
320 
321  if (rp != -1) {
322 
323  register cxint j = cpl_table_get_int(_fibers, idx, i , NULL) - 1;
324 
325  if (flux[j] > peak) {
326  peak = flux[j];
327  pos = i;
328  }
329 
330  }
331 
332  }
333 
334  giraffe_error_push();
335 
336  cpl_table_new_column(_fibers, "TRANSMISSION", CPL_TYPE_DOUBLE);
337  cpl_table_new_column(_fibers, "DTRANSMISSION", CPL_TYPE_DOUBLE);
338 
339  if (cpl_error_get_code() != CPL_ERROR_NONE) {
340 
341  cpl_image_delete(tflux);
342  tflux = NULL;
343 
344  cpl_image_delete(tvariance);
345  tvariance = NULL;
346 
347  giraffe_rebinning_delete(fspectra);
348  fspectra = NULL;
349 
350  return 1;
351  }
352 
353  giraffe_error_pop();
354 
355 
356  error = cpl_image_get_data(tvariance);
357 
358  for (i = 0; i < cpl_table_get_nrow(_fibers); i++) {
359 
360  cxint rp = cpl_table_get_int(_fibers, "RP", i, NULL);
361 
362  if (rp == -1 || i == pos) {
363  cpl_table_set_double(_fibers, "TRANSMISSION", i, 1.);
364  cpl_table_set_double(_fibers, "DTRANSMISSION", i, 0.);
365  }
366  else {
367 
368  cxint j = cpl_table_get_int(_fibers, idx, i , NULL) - 1;
369 
370  cpl_table_set_double(_fibers, "TRANSMISSION", i, flux[j] / peak);
371  cpl_table_set_double(_fibers, "DTRANSMISSION", i,
372  sqrt(error[j]) / peak);
373 
374  }
375 
376  }
377 
378  cpl_image_delete(tflux);
379  cpl_image_delete(tvariance);
380 
381  giraffe_rebinning_destroy(fspectra);
382 
383  return 0;
384 
385 }
386 
387 
388 cxint
389 giraffe_transmission_setup(GiTable *fibers, GiTable *reference)
390 {
391 
392  cxint i;
393 
394  cpl_table *_fibers = NULL;
395  cpl_table *_reference = NULL;
396 
397 
398  if (fibers == NULL) {
399  return -1;
400  }
401 
402  if (reference == NULL) {
403  return -2;
404  }
405 
406  _fibers = giraffe_table_get(fibers);
407  _reference = giraffe_table_get(reference);
408 
409  if (_fibers == NULL || cpl_table_has_column(_fibers, "FPS") == 0) {
410  return -3;
411  }
412 
413  if (_reference == NULL) {
414  return -4;
415  }
416 
417  if (cpl_table_has_column(_reference, "FPS") == 0 ||
418  cpl_table_has_column(_reference, "TRANSMISSION") == 0) {
419  return -4;
420  }
421 
422  if (cpl_table_has_column(_fibers, "TRANSMISSION") == 0) {
423 
424  cxint status = cpl_table_new_column(_fibers, "TRANSMISSION",
425  CPL_TYPE_DOUBLE);
426 
427  if (status) {
428  return 1;
429  }
430 
431  }
432 
433  for (i = 0; i < cpl_table_get_nrow(_fibers); i++) {
434 
435  cxint j;
436  cxint nrows = cpl_table_get_nrow(_reference);
437  cxint fps = cpl_table_get_int(_fibers, "FPS", i, NULL);
438 
439  cxdouble t = -1.;
440 
441 
442  for (j = 0; j < nrows; j++) {
443 
444  cxint _fps = cpl_table_get_int(_reference, "FPS", j, NULL);
445 
446  if (fps == _fps) {
447  t = cpl_table_get_double(_reference, "TRANSMISSION", j, NULL);
448  break;
449  }
450  }
451 
452  if (t < 0.) {
453  cpl_table_erase_column(_fibers, "TRANSMISSION");
454  return 2;
455  }
456  else {
457 
458  cxint status = cpl_table_set_double(_fibers, "TRANSMISSION",
459  i, t);
460 
461  if (status) {
462  return 3;
463  }
464 
465  }
466 
467  }
468 
469  return 0;
470 
471 }
472 
473 
474 cxint
475 giraffe_transmission_apply(GiExtraction *extraction, GiTable *fibers)
476 {
477 
478  cxint status;
479 
480  cpl_image *_spectra = NULL;
481 
482  cpl_table *_fibers = NULL;
483 
484 
485 
486  if (extraction == NULL) {
487  return -1;
488  }
489 
490  if (fibers == NULL) {
491  return -2;
492  }
493 
494 
495  if (extraction->spectra == NULL) {
496  return -3;
497  }
498 
499  _fibers = giraffe_table_get(fibers);
500 
501  if (_fibers == NULL) {
502  return -4;
503  }
504 
505 
506  if (cpl_table_has_column(_fibers, "TRANSMISSION") == 0) {
507  return -5;
508  }
509 
510 
511  _spectra = giraffe_image_get(extraction->spectra);
512  status = _giraffe_transmission_apply(_spectra, _fibers);
513 
514  if (status != 0) {
515  return 1;
516  }
517 
518  if (extraction->error != NULL) {
519 
520  _spectra = giraffe_image_get(extraction->error);
521  status = _giraffe_transmission_apply(_spectra, _fibers);
522 
523  if (status != 0) {
524  return 1;
525  }
526  }
527 
528  return 0;
529 
530 }
531 
532 
552 cxint
553 giraffe_transmission_attach(GiTable* fibers, const cxchar* filename)
554 {
555 
556  const cxchar* const _id = "giraffe_transmission_attach";
557 
558  cxint status = 0;
559 
560  GiTable* _fibers = NULL;
561 
562 
563  if ((fibers == NULL) || (filename == NULL)) {
564  return -1;
565  }
566 
567 
568  _fibers = giraffe_fiberlist_load(filename, 1, "FIBER_SETUP");
569 
570  if (fibers == NULL) {
571  cpl_error_set(_id, CPL_ERROR_ILLEGAL_INPUT);
572  return 1;
573  }
574 
575 
576  status = giraffe_transmission_setup(fibers, _fibers);
577 
578  giraffe_table_delete(_fibers);
579  _fibers = NULL;
580 
581  if (status < 0) {
582  cpl_error_set(_id, CPL_ERROR_DATA_NOT_FOUND);
583  return 2;
584  }
585 
586  if (status > 0) {
587  cpl_error_set(_id, CPL_ERROR_INCOMPATIBLE_INPUT);
588  return 3;
589  }
590 
591  return status;
592 
593 }
594 
595 
608 GiTransmissionConfig *
609 giraffe_transmission_config_create(cpl_parameterlist *list)
610 {
611 
612  GiTransmissionConfig *config = NULL;
613 
614 
615  if (!list) {
616  return NULL;
617  }
618 
619  config = cx_calloc(1, sizeof *config);
620 
621  return config;
622 
623 }
624 
625 
640 void
641 giraffe_transmission_config_destroy(GiTransmissionConfig *config)
642 {
643 
644  if (config) {
645  cx_free(config);
646  }
647 
648  return;
649 }
650 
651 
663 void
664 giraffe_transmission_config_add(cpl_parameterlist *list)
665 {
666 
667  if (!list) {
668  return;
669  }
670 
671  return;
672 
673 }
GiGrating * giraffe_grating_create(const GiImage *spectra, const GiTable *grating)
Create a GiGrating from a reference image.
Definition: gigrating.c:226
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
Definition: gitable.c:441
void giraffe_transmission_config_add(cpl_parameterlist *list)
Adds parameters for the transmission correction computation.
cxint giraffe_rebin_spectra(GiRebinning *rebinning, const GiExtraction *extraction, const GiTable *fibers, const GiLocalization *localization, const GiTable *grating, const GiTable *slitgeo, const GiTable *solution, const GiRebinConfig *config)
Rebin an Extracted Spectra Frame and associated Errors Frame.
Definition: girebinning.c:4059
void giraffe_table_delete(GiTable *self)
Destroys a Giraffe table.
Definition: gitable.c:162
const cxchar * giraffe_fiberlist_query_index(const cpl_table *fibers)
Query a fiber list for the name of the fiber reference index column.
void giraffe_transmission_config_destroy(GiTransmissionConfig *config)
Destroys a transmission field setup structure.
void giraffe_rebinning_delete(GiRebinning *rebinning)
Destroys a rebinning results container.
Definition: girebinning.c:4767
Structure to handle Grating Information.
Definition: gigrating.h:52
void giraffe_rebinning_destroy(GiRebinning *rebinning)
Destroys a rebinning results container and its contents.
Definition: girebinning.c:4795
void giraffe_grating_delete(GiGrating *self)
Destroys an GiGrating object.
Definition: gigrating.c:429
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.
Definition: giimage.c:226
GiRebinning * giraffe_rebinning_new(void)
Create an empty rebinning results container.
Definition: girebinning.c:4701
GiTransmissionConfig * giraffe_transmission_config_create(cpl_parameterlist *list)
Creates a setup structure for the relative transmission computation.
GiTable * giraffe_fiberlist_load(const cxchar *filename, cxint dataset, const cxchar *tag)
Load a fiber table from a file.
Definition: gifiberutils.c:723
cpl_propertylist * giraffe_image_get_properties(const GiImage *self)
Get the properties of an image.
Definition: giimage.c:290
cxint giraffe_transmission_attach(GiTable *fibers, const cxchar *filename)
Load relative fiber transmission data from a file and add it to a fiber table.

This file is part of the GIRAFFE Pipeline Reference Manual 2.14.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Wed Mar 11 2015 13:19:42 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2004