GIRAFFE Pipeline Reference Manual

gigrating.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 "gimacros.h"
35 #include "gialias.h"
36 #include "gigrating.h"
37 
38 #define GIFITS_KEYWORD_MISSING_MSG "FITS KEYWORD [%s] not found!! Aborting..."
39 
40 
49 static const cxdouble GI_WAVELENGTH_EPSILON = 1.e-8; /* 1.e-4 */
50 
51 
52 inline static cxint
53 _giraffe_grating_copy(GiGrating *self, cpl_table *grating, cxint row,
54  GiInstrumentMode mode)
55 {
56 
57  const cxchar *c_resolution = NULL;
58 
59 
60  cx_assert(self != NULL);
61  cx_assert(grating != NULL);
62 
63 
64  if (row >= cpl_table_get_nrow(grating)) {
65  return 1;
66  }
67 
68 
69  if (!cpl_table_has_column(grating, "ORDER")) {
70  return 2;
71  }
72  else {
73  self->order = cpl_table_get_int(grating, "ORDER", row, NULL);
74  }
75 
76 
77  if (!cpl_table_has_column(grating, "WLEN0")) {
78  return 2;
79  }
80  else {
81  self->wlen0 = cpl_table_get_double(grating, "WLEN0", row, NULL);
82  }
83 
84 
85  if (!cpl_table_has_column(grating, "WLMIN")) {
86  return 2;
87  }
88  else {
89  self->wlenmin = cpl_table_get_double(grating, "WLMIN", row, NULL);
90  }
91 
92 
93  if (!cpl_table_has_column(grating, "WLMAX")) {
94  return 2;
95  }
96  else {
97  self->wlenmax = cpl_table_get_double(grating, "WLMAX", row, NULL);
98  }
99 
100 
101  if (!cpl_table_has_column(grating, "BAND")) {
102  return 2;
103  }
104  else {
105  self->band = cpl_table_get_double(grating, "BAND", row, NULL);
106  }
107 
108 
109  switch (mode) {
110  case GIMODE_MEDUSA:
111  c_resolution = "RMED";
112  break;
113 
114  case GIMODE_IFU:
115  case GIMODE_ARGUS:
116  c_resolution = "RIFA";
117  break;
118 
119  default:
120  return 3;
121  break;
122  }
123 
124  if (!cpl_table_has_column(grating, c_resolution)) {
125  return 2;
126  }
127  else {
128  self->resol = cpl_table_get_int(grating, c_resolution, row, NULL);
129  }
130 
131 
132  if (!cpl_table_has_column(grating, "THETA")) {
133  return 2;
134  }
135  else {
136  self->theta = cpl_table_get_double(grating, "THETA", row, NULL);
137  }
138 
139 
140  if (!cpl_table_has_column(grating, "FCOLL")) {
141  return 2;
142  }
143  else {
144  self->fcoll = cpl_table_get_double(grating, "FCOLL", row, NULL);
145  }
146 
147 
148  if (!cpl_table_has_column(grating, "GCAM")) {
149  return 2;
150  }
151  else {
152  self->gcam = cpl_table_get_double(grating, "GCAM", row, NULL);
153  }
154 
155 
156  if (!cpl_table_has_column(grating, "SDX")) {
157  return 2;
158  }
159  else {
160  self->sdx = cpl_table_get_double(grating, "SDX", row, NULL);
161  }
162 
163 
164  if (!cpl_table_has_column(grating, "SDY")) {
165  return 2;
166  }
167  else {
168  self->sdy = cpl_table_get_double(grating, "SDY", row, NULL);
169  }
170 
171 
172  if (!cpl_table_has_column(grating, "SPHI")) {
173  return 2;
174  }
175  else {
176  self->sphi = cpl_table_get_double(grating, "SPHI", row, NULL);
177  }
178 
179  return 0;
180 
181 }
182 
183 
193 GiGrating *
195 {
196 
197  GiGrating *grating = (GiGrating *) cx_calloc(1, sizeof *grating);
198 
199  grating->name = cx_string_new();
200  grating->setup = cx_string_new();
201  grating->filter = cx_string_new();
202  grating->slit = cx_string_new();
203 
204  return grating;
205 
206 }
207 
208 
225 GiGrating *
226 giraffe_grating_create(const GiImage *spectra, const GiTable *grating)
227 {
228 
229  const cxchar *const c_setup = "SETUP";
230 
231  const cxchar *setup = NULL;
232 
233  cxint i;
234  cxint order;
235  cxint row = -1;
236  cxint status = 0;
237 
238  cxdouble wlen0;
239 
240  cpl_propertylist *properties = NULL;
241 
242  cpl_table *_grating = NULL;
243 
244  GiGrating *self = NULL;
245 
246  GiInstrumentMode mode;
247 
248 
249 
250  if (spectra == NULL || grating == NULL) {
251  return NULL;
252  }
253 
254  properties = giraffe_image_get_properties(spectra);
255 
256  if (properties == NULL) {
257  return NULL;
258  }
259 
260  _grating = giraffe_table_get(grating);
261 
262  if (_grating == NULL) {
263  return NULL;
264  }
265 
266 
267  /*
268  * Get instrument setup information from the reference image
269  * and fill the grating setup structure with the appropriate
270  * data from the grating table. The central wavelength and the
271  * order information provided by the reference image is used
272  * to select the grating data.
273  */
274 
275  self = giraffe_grating_new();
276 
277  /* Grating name */
278 
279  if (!cpl_propertylist_has(properties, GIALIAS_GRATNAME)) {
281  return NULL;
282  }
283  else {
284  cx_string_set(self->name,
285  cpl_propertylist_get_string(properties,
286  GIALIAS_GRATNAME));
287  }
288 
289  /* Order sorting filter name */
290 
291  if (!cpl_propertylist_has(properties, GIALIAS_FILTNAME)) {
293  return NULL;
294  }
295  else {
296  cx_string_set(self->filter,
297  cpl_propertylist_get_string(properties,
298  GIALIAS_FILTNAME));
299  }
300 
301  /* Slit name */
302 
303  if (!cpl_propertylist_has(properties, GIALIAS_SLITNAME)) {
305  return NULL;
306  }
307  else {
308  cx_string_set(self->slit,
309  cpl_propertylist_get_string(properties,
310  GIALIAS_SLITNAME));
311  }
312 
313  /* Spacing of grating grooves */
314 
315  if (!cpl_propertylist_has(properties, GIALIAS_GRATGRV)) {
317  return NULL;
318  }
319  else {
320 
321  cxdouble grooves = cpl_propertylist_get_double(properties,
322  GIALIAS_GRATGRV);
323  self->space = 1. / fabs(GI_MM_TO_NM * grooves);
324 
325  }
326 
327  /* Instrument mode */
328 
329  mode = giraffe_get_mode(properties);
330 
331 
332  /*
333  * Select the grating data for the current setup
334  */
335 
336  if (!cpl_table_has_column(_grating, "ORDER") ||
337  !cpl_table_has_column(_grating, "WLEN0")) {
338 
340  return NULL;
341 
342  }
343 
344  if (!cpl_propertylist_has(properties, GIALIAS_GRATWLEN)) {
346  return NULL;
347  }
348  else {
349  wlen0 = cpl_propertylist_get_double(properties, GIALIAS_GRATWLEN);
350  }
351 
352  if (!cpl_propertylist_has(properties, GIALIAS_GRATORDER)) {
354  return NULL;
355  }
356  else {
357  order = cpl_propertylist_get_int(properties, GIALIAS_GRATORDER);
358  }
359 
360 
361  for (i = 0; i < cpl_table_get_nrow(_grating); i++) {
362 
363  cxint _order = cpl_table_get_int(_grating, "ORDER", i, NULL);
364 
365  if (order == _order) {
366 
367  cxdouble _wlen0 = cpl_table_get_double(_grating, "WLEN0",
368  i, NULL);
369 
370  if (fabs(wlen0 - _wlen0) < GI_WAVELENGTH_EPSILON) {
371  row = i;
372  break;
373  }
374 
375  }
376 
377  }
378 
379  if (row < 0) {
381  return NULL;
382  }
383 
384  /*
385  * Try to figure out the setup identifier.
386  */
387 
388  /* FIXME: Check whether we can live with an empty setup string
389  * at later stages of the wavelength calibration.
390  */
391 
392  if (cpl_propertylist_has(properties, GIALIAS_SETUPNAME)) {
393  setup = cpl_propertylist_get_string(properties, GIALIAS_SETUPNAME);
394  cx_string_set(self->setup, setup);
395  }
396  else {
397  if (cpl_table_has_column(_grating, c_setup)) {
398  setup = cpl_table_get_string(_grating, c_setup, row);
399  cx_string_set(self->setup, setup);
400  }
401  }
402 
403 
404  status = _giraffe_grating_copy(self, _grating, row, mode);
405 
406  if (status != 0) {
408  return NULL;
409  }
410 
411  return self;
412 
413 }
414 
415 
428 void
430 {
431 
432  if (self == NULL) {
433  return;
434  }
435 
436  if (self->name != NULL) {
437  cx_string_delete(self->name);
438  }
439 
440  if (self->setup != NULL) {
441  cx_string_delete(self->setup);
442  }
443 
444  if (self->filter != NULL) {
445  cx_string_delete(self->filter);
446  }
447 
448  if (self->slit != NULL) {
449  cx_string_delete(self->slit);
450  }
451 
452  cx_free(self);
453 
454  return;
455 
456 }
457 
458 
481 cxint
482 giraffe_grating_setup(GiTable *grating_table, GiImage *spectra,
483  GiGrating *grating_setup)
484 {
485 
486  const cxchar* const fctid = "giraffe_grating_setup";
487 
488  const cxchar* c_name_setup = "SETUP";
489  const cxchar* c_name_order = "ORDER";
490  const cxchar* c_name_wl0 = "WLEN0";
491  const cxchar* c_name_wlmin = "WLMIN";
492  const cxchar* c_name_wlmax = "WLMAX";
493  const cxchar* c_name_band = "BAND";
494  const cxchar* c_name_theta = "THETA";
495  const cxchar* c_name_fcoll = "FCOLL";
496  const cxchar* c_name_gcam = "GCAM";
497  const cxchar* c_name_sdx = "SDX";
498  const cxchar* c_name_sdy = "SDY";
499  const cxchar* c_name_sdphi = "SPHI";
500  const cxchar* c_name_rmed = "RMED";
501  const cxchar* c_name_rifa = "RIFA";
502 
503  cxint i = 0;
504  cxint row_match = 0;
505  cxint row_nulls = 0;
506 
507  cxdouble wlen_match = 0.;
508  cxdouble wlen = 0.;
509  cxdouble tmp_gratgrv = 0.;
510 
511 
512  cx_string* slit_name = NULL;
513 
514  cpl_propertylist* ref_plimg = NULL;
515 
516  cpl_table* ref_gtable = NULL;
517 
518  GiInstrumentMode instrument_mode;
519 
520 
521  /************************************************************************
522  Preprocessing
523  ************************************************************************/
524 
525  if (grating_table == NULL) {
526  return 1;
527  }
528 
529  if (spectra == NULL) {
530  return 1;
531  }
532 
533  if (grating_setup == NULL) {
534  return 1;
535  }
536 
537  ref_plimg = giraffe_image_get_properties(spectra);
538  if (ref_plimg == NULL) {
539  return 128;
540  }
541 
542  ref_gtable = giraffe_table_get(grating_table);
543  if (ref_gtable == NULL) {
544  return 128;
545  }
546 
547  slit_name = cx_string_new();
548 
549  /************************************************************************
550  Processing
551  ************************************************************************/
552 
553  /*
554  * Retrieve Grating information from associated image...
555  */
556 
557  if (cpl_propertylist_has(ref_plimg, GIALIAS_GRATWLEN) == FALSE) {
558  cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_GRATWLEN);
559  cx_string_delete(slit_name);
560  return 2;
561  }
562  else {
563  grating_setup->wlen0 = cpl_propertylist_get_double(ref_plimg,
564  GIALIAS_GRATWLEN);
565  }
566 
567  if (cpl_propertylist_has(ref_plimg, GIALIAS_SLITNAME) == FALSE) {
568  cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_SLITNAME);
569  cx_string_delete(slit_name);
570  return 2;
571  }
572  else {
573  cx_string_set(slit_name,
574  cpl_propertylist_get_string(ref_plimg,
575  GIALIAS_SLITNAME));
576  }
577 
578  if (cpl_propertylist_has(ref_plimg, GIALIAS_GRATGRV) == FALSE) {
579  cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_GRATGRV);
580  cx_string_delete(slit_name);
581  return 2;
582  }
583  else {
584  tmp_gratgrv = cpl_propertylist_get_double(ref_plimg,
585  GIALIAS_GRATGRV);
586  }
587 
588  if (cpl_propertylist_has(ref_plimg, GIALIAS_GRATNAME) == FALSE) {
589  cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_GRATNAME);
590  cx_string_delete(slit_name);
591  return 2;
592  }
593  else {
594  cx_string_set(grating_setup->name,
595  cpl_propertylist_get_string(ref_plimg,
596  GIALIAS_GRATNAME));
597  }
598 
599  if (cpl_propertylist_has(ref_plimg, GIALIAS_FILTNAME) == FALSE) {
600  cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_FILTNAME);
601  cx_string_delete(slit_name);
602  return 2;
603  }
604  else {
605  cx_string_set(grating_setup->filter,
606  cpl_propertylist_get_string(ref_plimg,
607  GIALIAS_FILTNAME));
608  }
609 
610 
611  /*
612  * Find wavelength nearest to central wavelength...
613  */
614 
615  for (i = 0; i < cpl_table_get_nrow(ref_gtable); i++) {
616 
617  wlen = cpl_table_get(ref_gtable, c_name_wl0, i, &row_nulls);
618 
619  if (fabs(wlen - grating_setup->wlen0) <
620  fabs(wlen_match - grating_setup->wlen0)) {
621  wlen_match = wlen;
622  row_match = i;
623  }
624 
625  }
626 
627 
628  /*
629  * Have we found a match?...
630  */
631 
632  if (fabs(wlen_match - grating_setup->wlen0) > GI_WAVELENGTH_EPSILON) {
633 
634  cpl_msg_error(fctid, "Central wavelength [%f] nout found in grating "
635  "table, aborting...", grating_setup->wlen0);
636  cx_string_delete(slit_name);
637  return 3;
638  }
639  else {
640  cpl_msg_debug(fctid, "Found wlen0 in grating table at position %d",
641  row_match);
642  }
643 
644 
645  /*
646  * Retrieve values associated to matched wavelength from
647  * grating table...
648  */
649 
650  cx_string_set(grating_setup->setup,
651  (cxchar*) cpl_table_get_string(ref_gtable, c_name_setup,
652  row_match));
653 
654  cx_string_set(grating_setup->slit, cx_string_get(slit_name));
655 
656  grating_setup->order = cpl_table_get(ref_gtable, c_name_order,
657  row_match, &row_nulls);
658 
659  grating_setup->wlenmin = cpl_table_get(ref_gtable, c_name_wlmin,
660  row_match, &row_nulls);
661 
662  grating_setup->wlenmax = cpl_table_get(ref_gtable, c_name_wlmax,
663  row_match, &row_nulls);
664 
665  grating_setup->band = cpl_table_get(ref_gtable, c_name_band,
666  row_match, &row_nulls);
667 
668  grating_setup->theta = cpl_table_get(ref_gtable, c_name_theta,
669  row_match, &row_nulls);
670 
671  grating_setup->space = 1.0 / fabs(GI_MM_TO_NM * tmp_gratgrv);
672 
673 
674  instrument_mode = giraffe_get_mode(ref_plimg);
675 
676  switch (instrument_mode) {
677  case GIMODE_MEDUSA:
678  grating_setup->resol = cpl_table_get(ref_gtable, c_name_rmed,
679  row_match, &row_nulls);
680  break;
681 
682  case GIMODE_IFU:
683  grating_setup->resol = cpl_table_get(ref_gtable, c_name_rifa,
684  row_match, &row_nulls);
685  break;
686 
687  case GIMODE_ARGUS:
688  grating_setup->resol = cpl_table_get(ref_gtable, c_name_rifa,
689  row_match, &row_nulls);
690  break;
691 
692  default:
693  grating_setup->resol = -1.0;
694  break;
695  }
696 
697  grating_setup->fcoll =
698  cpl_table_get(ref_gtable, c_name_fcoll, row_match, &row_nulls);
699 
700  grating_setup->gcam =
701  cpl_table_get(ref_gtable, c_name_gcam, row_match, &row_nulls);
702 
703  grating_setup->sdx =
704  cpl_table_get(ref_gtable, c_name_sdx, row_match, &row_nulls);
705 
706  grating_setup->sdy =
707  cpl_table_get(ref_gtable, c_name_sdy, row_match, &row_nulls);
708 
709  grating_setup->sphi =
710  cpl_table_get(ref_gtable, c_name_sdphi, row_match, &row_nulls);
711 
712  cx_string_delete(slit_name);
713 
714  return 0;
715 
716 }
717 
718 
729 void
731 {
732 
733  const cxchar *fctid = "giraffe_grating_dump";
734 
735  if (grating == NULL) {
736  return;
737  }
738 
739  cpl_msg_debug(fctid, "---- GiGrating -------------------------");
740 
741  cpl_msg_debug(fctid, "Grating Name : %s",
742  cx_string_get(grating->name));
743  cpl_msg_debug(fctid, "Grating Filter Name : %s",
744  cx_string_get(grating->filter));
745  cpl_msg_debug(fctid, "Grating Setup Name : %s",
746  cx_string_get(grating->setup));
747  cpl_msg_debug(fctid, "Grating Order : %12d", grating->order);
748  cpl_msg_debug(fctid, "Grating Wlen0 : %12.6f", grating->wlen0);
749  cpl_msg_debug(fctid, "Grating Wlen Min : %12.6f", grating->wlenmin);
750  cpl_msg_debug(fctid, "Grating Wlen Max : %12.6f", grating->wlenmax);
751  cpl_msg_debug(fctid, "Grating Band : %12.6f", grating->band);
752  cpl_msg_debug(fctid, "Grating Resol : %12d", grating->resol);
753  cpl_msg_debug(fctid, "Grating Space : %12.6f", grating->space);
754  cpl_msg_debug(fctid, "Grating Theta : %12.6f", grating->theta);
755  cpl_msg_debug(fctid, "Grating FColl : %12.6f", grating->fcoll);
756  cpl_msg_debug(fctid, "Grating GCam : %12.6f", grating->gcam);
757  cpl_msg_debug(fctid, "Grating SlitDx : %12.6f", grating->sdx);
758  cpl_msg_debug(fctid, "Grating SlitDy : %12.6f", grating->sdy);
759  cpl_msg_debug(fctid, "Grating SlitPhi : %12.6f", grating->sphi);
760 
761  return;
762 
763 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.12.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Mon Mar 24 2014 11:43:52 by doxygen 1.8.2 written by Dimitri van Heesch, © 1997-2004