GIRAFFE Pipeline Reference Manual

gigrating.c

00001 /* $Id: gigrating.c,v 1.10 2007/08/23 08:39:06 rpalsa Exp $
00002  *
00003  * This file is part of the GIRAFFE Pipeline
00004  * Copyright (C) 2002-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /*
00022  * $Author: rpalsa $
00023  * $Date: 2007/08/23 08:39:06 $
00024  * $Revision: 1.10 $
00025  * $Name: giraffe-2_5_2 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #include <math.h>
00033 
00034 #include "gimacros.h"
00035 #include "gialias.h"
00036 #include "gigrating.h"
00037 
00038 #define GIFITS_KEYWORD_MISSING_MSG "FITS KEYWORD [%s] not found!! Aborting..."
00039 
00040 
00049 static const cxdouble GI_WAVELENGTH_EPSILON = 1.e-8;  /* 1.e-4 */
00050 
00051 
00052 inline static cxint
00053 _giraffe_grating_copy(GiGrating *self, cpl_table *grating, cxint row,
00054                       GiInstrumentMode mode)
00055 {
00056 
00057     const cxchar *c_resolution = NULL;
00058 
00059 
00060     cx_assert(self != NULL);
00061     cx_assert(grating != NULL);
00062 
00063 
00064     if (row >= cpl_table_get_nrow(grating)) {
00065         return 1;
00066     }
00067 
00068 
00069     if (!cpl_table_has_column(grating, "ORDER")) {
00070         return 2;
00071     }
00072     else {
00073         self->order = cpl_table_get_int(grating, "ORDER", row, NULL);
00074     }
00075 
00076 
00077     if (!cpl_table_has_column(grating, "WLEN0")) {
00078         return 2;
00079     }
00080     else {
00081         self->wlen0 = cpl_table_get_double(grating, "WLEN0", row, NULL);
00082     }
00083 
00084 
00085     if (!cpl_table_has_column(grating, "WLMIN")) {
00086         return 2;
00087     }
00088     else {
00089         self->wlenmin = cpl_table_get_double(grating, "WLMIN", row, NULL);
00090     }
00091 
00092 
00093     if (!cpl_table_has_column(grating, "WLMAX")) {
00094         return 2;
00095     }
00096     else {
00097         self->wlenmax = cpl_table_get_double(grating, "WLMAX", row, NULL);
00098     }
00099 
00100 
00101     if (!cpl_table_has_column(grating, "BAND")) {
00102         return 2;
00103     }
00104     else {
00105         self->band = cpl_table_get_double(grating, "BAND", row, NULL);
00106     }
00107 
00108 
00109     switch (mode) {
00110         case GIMODE_MEDUSA:
00111             c_resolution = "RMED";
00112             break;
00113 
00114         case GIMODE_IFU:
00115         case GIMODE_ARGUS:
00116             c_resolution = "RIFA";
00117             break;
00118 
00119         default:
00120             return 3;
00121             break;
00122     }
00123 
00124     if (!cpl_table_has_column(grating, c_resolution)) {
00125         return 2;
00126     }
00127     else {
00128         self->resol = cpl_table_get_int(grating, c_resolution, row, NULL);
00129     }
00130 
00131 
00132     if (!cpl_table_has_column(grating, "THETA")) {
00133         return 2;
00134     }
00135     else {
00136         self->theta = cpl_table_get_double(grating, "THETA", row, NULL);
00137     }
00138 
00139 
00140     if (!cpl_table_has_column(grating, "FCOLL")) {
00141         return 2;
00142     }
00143     else {
00144         self->fcoll = cpl_table_get_double(grating, "FCOLL", row, NULL);
00145     }
00146 
00147 
00148     if (!cpl_table_has_column(grating, "GCAM")) {
00149         return 2;
00150     }
00151     else {
00152         self->gcam = cpl_table_get_double(grating, "GCAM", row, NULL);
00153     }
00154 
00155 
00156     if (!cpl_table_has_column(grating, "SDX")) {
00157         return 2;
00158     }
00159     else {
00160         self->sdx = cpl_table_get_double(grating, "SDX", row, NULL);
00161     }
00162 
00163 
00164     if (!cpl_table_has_column(grating, "SDY")) {
00165         return 2;
00166     }
00167     else {
00168         self->sdy = cpl_table_get_double(grating, "SDY", row, NULL);
00169     }
00170 
00171 
00172     if (!cpl_table_has_column(grating, "SPHI")) {
00173         return 2;
00174     }
00175     else {
00176         self->sphi = cpl_table_get_double(grating, "SPHI", row, NULL);
00177     }
00178 
00179     return 0;
00180 
00181 }
00182 
00183 
00193 GiGrating *
00194 giraffe_grating_new(void)
00195 {
00196 
00197     GiGrating *grating = (GiGrating *) cx_calloc(1, sizeof *grating);
00198 
00199     grating->name   = cx_string_new();
00200     grating->setup  = cx_string_new();
00201     grating->filter = cx_string_new();
00202     grating->slit   = cx_string_new();
00203 
00204     return grating;
00205 
00206 }
00207 
00208 
00225 GiGrating *
00226 giraffe_grating_create(const GiImage *spectra, const GiTable *grating)
00227 {
00228 
00229     const cxchar *const c_setup = "SETUP";
00230 
00231     const cxchar *setup = NULL;
00232 
00233     cxint i;
00234     cxint order;
00235     cxint row = -1;
00236     cxint status = 0;
00237 
00238     cxdouble wlen0;
00239 
00240     cpl_propertylist *properties = NULL;
00241 
00242     cpl_table *_grating = NULL;
00243 
00244     GiGrating *self = NULL;
00245 
00246     GiInstrumentMode mode;
00247 
00248 
00249 
00250     if (spectra == NULL || grating == NULL) {
00251         return NULL;
00252     }
00253 
00254     properties = giraffe_image_get_properties(spectra);
00255 
00256     if (properties == NULL) {
00257         return NULL;
00258     }
00259 
00260     _grating = giraffe_table_get(grating);
00261 
00262     if (_grating == NULL) {
00263         return NULL;
00264     }
00265 
00266 
00267     /*
00268      * Get instrument setup information from the reference image
00269      * and fill the grating setup structure with the appropriate
00270      * data from the grating table. The central wavelength and the
00271      * order information provided by the reference image is used
00272      * to select the grating data.
00273      */
00274 
00275     self = giraffe_grating_new();
00276 
00277     /* Grating name */
00278 
00279     if (!cpl_propertylist_has(properties, GIALIAS_GRATNAME)) {
00280         giraffe_grating_delete(self);
00281         return NULL;
00282     }
00283     else {
00284         cx_string_set(self->name,
00285                       cpl_propertylist_get_string(properties,
00286                                                   GIALIAS_GRATNAME));
00287     }
00288 
00289     /* Order sorting filter name */
00290 
00291     if (!cpl_propertylist_has(properties, GIALIAS_FILTNAME)) {
00292         giraffe_grating_delete(self);
00293         return NULL;
00294     }
00295     else {
00296         cx_string_set(self->filter,
00297                       cpl_propertylist_get_string(properties,
00298                                                   GIALIAS_FILTNAME));
00299     }
00300 
00301     /* Slit name */
00302 
00303     if (!cpl_propertylist_has(properties, GIALIAS_SLITNAME)) {
00304         giraffe_grating_delete(self);
00305         return NULL;
00306     }
00307     else {
00308         cx_string_set(self->slit,
00309                       cpl_propertylist_get_string(properties,
00310                                                   GIALIAS_SLITNAME));
00311     }
00312 
00313     /* Spacing of grating grooves */
00314 
00315     if (!cpl_propertylist_has(properties, GIALIAS_GRATGRV)) {
00316         giraffe_grating_delete(self);
00317         return NULL;
00318     }
00319     else {
00320 
00321         cxdouble grooves = cpl_propertylist_get_double(properties,
00322                                                        GIALIAS_GRATGRV);
00323         self->space = 1. / fabs(GI_MM_TO_NM * grooves);
00324 
00325     }
00326 
00327     /* Instrument mode */
00328 
00329     mode = giraffe_get_mode(properties);
00330 
00331 
00332     /*
00333      * Select the grating data for the current setup
00334      */
00335 
00336     if (!cpl_table_has_column(_grating, "ORDER") ||
00337         !cpl_table_has_column(_grating, "WLEN0")) {
00338 
00339         giraffe_grating_delete(self);
00340         return NULL;
00341 
00342     }
00343 
00344     if (!cpl_propertylist_has(properties, GIALIAS_GRATWLEN)) {
00345         giraffe_grating_delete(self);
00346         return NULL;
00347     }
00348     else {
00349         wlen0 = cpl_propertylist_get_double(properties, GIALIAS_GRATWLEN);
00350     }
00351 
00352     if (!cpl_propertylist_has(properties, GIALIAS_GRATORDER)) {
00353         giraffe_grating_delete(self);
00354         return NULL;
00355     }
00356     else {
00357         order = cpl_propertylist_get_int(properties, GIALIAS_GRATORDER);
00358     }
00359 
00360 
00361     for (i = 0; i < cpl_table_get_nrow(_grating); i++) {
00362 
00363         cxint _order = cpl_table_get_int(_grating, "ORDER", i, NULL);
00364 
00365         if (order == _order) {
00366 
00367             cxdouble _wlen0 = cpl_table_get_double(_grating, "WLEN0",
00368                                                    i, NULL);
00369 
00370             if (fabs(wlen0 - _wlen0) < GI_WAVELENGTH_EPSILON) {
00371                 row = i;
00372                 break;
00373             }
00374 
00375         }
00376 
00377     }
00378 
00379     if (row < 0) {
00380         giraffe_grating_delete(self);
00381         return NULL;
00382     }
00383 
00384     /*
00385      * Try to figure out the setup identifier.
00386      */
00387 
00388     /* FIXME: Check whether we can live with an empty setup string
00389      *        at later stages of the wavelength calibration.
00390      */
00391 
00392     if (cpl_propertylist_has(properties, GIALIAS_SETUPNAME)) {
00393         setup = cpl_propertylist_get_string(properties, GIALIAS_SETUPNAME);
00394         cx_string_set(self->setup, setup);
00395     }
00396     else {
00397         if (cpl_table_has_column(_grating, c_setup)) {
00398             setup = cpl_table_get_string(_grating, c_setup, row);
00399             cx_string_set(self->setup, setup);
00400         }
00401     }
00402 
00403 
00404     status = _giraffe_grating_copy(self, _grating, row, mode);
00405 
00406     if (status != 0) {
00407         giraffe_grating_delete(self);
00408         return NULL;
00409     }
00410 
00411     return self;
00412 
00413 }
00414 
00415 
00425 void
00426 giraffe_grating_delete(GiGrating *self)
00427 {
00428 
00429     if (self == NULL) {
00430         return;
00431     }
00432 
00433     if (self->name != NULL) {
00434         cx_string_delete(self->name);
00435     }
00436 
00437     if (self->setup != NULL) {
00438         cx_string_delete(self->setup);
00439     }
00440 
00441     if (self->filter != NULL) {
00442         cx_string_delete(self->filter);
00443     }
00444 
00445     if (self->slit != NULL) {
00446         cx_string_delete(self->slit);
00447     }
00448 
00449     cx_free(self);
00450 
00451     return;
00452 
00453 }
00454 
00455 
00478 cxint
00479 giraffe_grating_setup(GiTable *grating_table, GiImage *spectra,
00480                       GiGrating *grating_setup)
00481 {
00482 
00483     const cxchar* const fctid = "giraffe_grating_setup";
00484 
00485     const cxchar* c_name_setup  = "SETUP";
00486     const cxchar* c_name_order  = "ORDER";
00487     const cxchar* c_name_wl0    = "WLEN0";
00488     const cxchar* c_name_wlmin  = "WLMIN";
00489     const cxchar* c_name_wlmax  = "WLMAX";
00490     const cxchar* c_name_band   = "BAND";
00491     const cxchar* c_name_theta  = "THETA";
00492     const cxchar* c_name_fcoll  = "FCOLL";
00493     const cxchar* c_name_gcam   = "GCAM";
00494     const cxchar* c_name_sdx    = "SDX";
00495     const cxchar* c_name_sdy    = "SDY";
00496     const cxchar* c_name_sdphi  = "SPHI";
00497     const cxchar* c_name_rmed   = "RMED";
00498     const cxchar* c_name_rifa   = "RIFA";
00499 
00500     cxint i = 0;
00501     cxint row_match = 0;
00502     cxint row_nulls = 0;
00503 
00504     cxdouble wlen_match = 0.;
00505     cxdouble wlen = 0.;
00506     cxdouble tmp_gratgrv = 0.;
00507 
00508 
00509     cx_string* slit_name = NULL;
00510 
00511     cpl_propertylist* ref_plimg = NULL;
00512 
00513     cpl_table* ref_gtable = NULL;
00514 
00515     GiInstrumentMode  instrument_mode;
00516 
00517 
00518     /************************************************************************
00519                                     Preprocessing
00520     ************************************************************************/
00521 
00522     if (grating_table  == NULL) {
00523         return 1;
00524     }
00525 
00526     if (spectra == NULL) {
00527         return 1;
00528     }
00529 
00530     if (grating_setup == NULL) {
00531         return 1;
00532     }
00533 
00534     ref_plimg = giraffe_image_get_properties(spectra);
00535     if (ref_plimg == NULL) {
00536         return 128;
00537     }
00538 
00539     ref_gtable = giraffe_table_get(grating_table);
00540     if (ref_gtable == NULL) {
00541         return 128;
00542     }
00543 
00544     slit_name = cx_string_new();
00545 
00546     /************************************************************************
00547                                      Processing
00548     ************************************************************************/
00549 
00550     /*
00551      *  Retrieve Grating information from associated image...
00552      */
00553 
00554     if (cpl_propertylist_has(ref_plimg, GIALIAS_GRATWLEN) == FALSE) {
00555         cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_GRATWLEN);
00556         cx_string_delete(slit_name);
00557         return 2;
00558     }
00559     else {
00560         grating_setup->wlen0 = cpl_propertylist_get_double(ref_plimg,
00561                                                            GIALIAS_GRATWLEN);
00562     }
00563 
00564     if (cpl_propertylist_has(ref_plimg, GIALIAS_SLITNAME) == FALSE) {
00565         cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_SLITNAME);
00566         cx_string_delete(slit_name);
00567         return 2;
00568     }
00569     else {
00570         cx_string_set(slit_name,
00571                       cpl_propertylist_get_string(ref_plimg,
00572                                                    GIALIAS_SLITNAME));
00573     }
00574 
00575     if (cpl_propertylist_has(ref_plimg, GIALIAS_GRATGRV) == FALSE) {
00576         cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_GRATGRV);
00577         cx_string_delete(slit_name);
00578         return 2;
00579     }
00580     else {
00581         tmp_gratgrv = cpl_propertylist_get_double(ref_plimg,
00582                                                   GIALIAS_GRATGRV);
00583     }
00584 
00585     if (cpl_propertylist_has(ref_plimg, GIALIAS_GRATNAME) == FALSE) {
00586         cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_GRATNAME);
00587         cx_string_delete(slit_name);
00588         return 2;
00589     }
00590     else {
00591         cx_string_set(grating_setup->name,
00592                       cpl_propertylist_get_string(ref_plimg,
00593                                                   GIALIAS_GRATNAME));
00594     }
00595 
00596     if (cpl_propertylist_has(ref_plimg, GIALIAS_FILTNAME) == FALSE) {
00597         cpl_msg_error(fctid, GIFITS_KEYWORD_MISSING_MSG, GIALIAS_FILTNAME);
00598         cx_string_delete(slit_name);
00599         return 2;
00600     }
00601     else {
00602         cx_string_set(grating_setup->filter,
00603                       cpl_propertylist_get_string(ref_plimg,
00604                                                   GIALIAS_FILTNAME));
00605     }
00606 
00607 
00608     /*
00609      *  Find wavelength nearest to central wavelength...
00610      */
00611 
00612     for (i = 0; i < cpl_table_get_nrow(ref_gtable); i++) {
00613 
00614         wlen = cpl_table_get(ref_gtable, c_name_wl0, i, &row_nulls);
00615 
00616         if (fabs(wlen - grating_setup->wlen0) <
00617             fabs(wlen_match - grating_setup->wlen0)) {
00618             wlen_match = wlen;
00619             row_match  = i;
00620         }
00621 
00622     }
00623 
00624 
00625     /*
00626      *  Have we found a match?...
00627      */
00628 
00629     if (fabs(wlen_match - grating_setup->wlen0) > GI_WAVELENGTH_EPSILON) {
00630 
00631         cpl_msg_error(fctid, "Central wavelength [%f] nout found in grating "
00632                       "table, aborting...", grating_setup->wlen0);
00633         cx_string_delete(slit_name);
00634         return 3;
00635     }
00636     else {
00637         cpl_msg_debug(fctid, "Found wlen0 in grating table at position %d",
00638                       row_match);
00639     }
00640 
00641 
00642     /*
00643      * Retrieve values associated to matched wavelength from
00644      * grating table...
00645      */
00646 
00647     cx_string_set(grating_setup->setup,
00648                   (cxchar*) cpl_table_get_string(ref_gtable, c_name_setup,
00649                                                  row_match));
00650 
00651     cx_string_set(grating_setup->slit, cx_string_get(slit_name));
00652 
00653     grating_setup->order = cpl_table_get(ref_gtable, c_name_order,
00654                                          row_match, &row_nulls);
00655 
00656     grating_setup->wlenmin = cpl_table_get(ref_gtable, c_name_wlmin,
00657                                            row_match, &row_nulls);
00658 
00659     grating_setup->wlenmax = cpl_table_get(ref_gtable, c_name_wlmax,
00660                                            row_match, &row_nulls);
00661 
00662     grating_setup->band = cpl_table_get(ref_gtable, c_name_band,
00663                                         row_match, &row_nulls);
00664 
00665     grating_setup->theta = cpl_table_get(ref_gtable, c_name_theta,
00666                                          row_match, &row_nulls);
00667 
00668     grating_setup->space = 1.0 / fabs(GI_MM_TO_NM * tmp_gratgrv);
00669 
00670 
00671     instrument_mode = giraffe_get_mode(ref_plimg);
00672 
00673     switch (instrument_mode) {
00674         case GIMODE_MEDUSA:
00675             grating_setup->resol = cpl_table_get(ref_gtable, c_name_rmed,
00676                                                  row_match, &row_nulls);
00677             break;
00678 
00679         case GIMODE_IFU:
00680             grating_setup->resol = cpl_table_get(ref_gtable, c_name_rifa,
00681                                                  row_match, &row_nulls);
00682             break;
00683 
00684         case GIMODE_ARGUS:
00685             grating_setup->resol = cpl_table_get(ref_gtable, c_name_rifa,
00686                                                  row_match, &row_nulls);
00687             break;
00688 
00689         default:
00690             grating_setup->resol = -1.0;
00691             break;
00692     }
00693 
00694     grating_setup->fcoll   =
00695         cpl_table_get(ref_gtable, c_name_fcoll, row_match, &row_nulls);
00696 
00697     grating_setup->gcam    =
00698         cpl_table_get(ref_gtable, c_name_gcam,  row_match, &row_nulls);
00699 
00700     grating_setup->sdx  =
00701         cpl_table_get(ref_gtable, c_name_sdx,   row_match, &row_nulls);
00702 
00703     grating_setup->sdy  =
00704         cpl_table_get(ref_gtable, c_name_sdy,   row_match, &row_nulls);
00705 
00706     grating_setup->sphi =
00707         cpl_table_get(ref_gtable, c_name_sdphi, row_match, &row_nulls);
00708 
00709     cx_string_delete(slit_name);
00710 
00711     return 0;
00712 
00713 }
00714 
00715 
00726 void
00727 giraffe_grating_dump(const GiGrating *grating)
00728 {
00729 
00730     const cxchar *fctid = "giraffe_grating_dump";
00731 
00732     if (grating == NULL) {
00733         return;
00734     }
00735 
00736     cpl_msg_debug(fctid, "---- GiGrating -------------------------");
00737 
00738     cpl_msg_debug(fctid, "Grating Name        : %s",
00739                   cx_string_get(grating->name));
00740     cpl_msg_debug(fctid, "Grating Filter Name : %s",
00741                   cx_string_get(grating->filter));
00742     cpl_msg_debug(fctid, "Grating Setup Name  : %s",
00743                   cx_string_get(grating->setup));
00744     cpl_msg_debug(fctid, "Grating Order       : %12d", grating->order);
00745     cpl_msg_debug(fctid, "Grating Wlen0       : %12.6f", grating->wlen0);
00746     cpl_msg_debug(fctid, "Grating Wlen Min    : %12.6f", grating->wlenmin);
00747     cpl_msg_debug(fctid, "Grating Wlen Max    : %12.6f", grating->wlenmax);
00748     cpl_msg_debug(fctid, "Grating Band        : %12.6f", grating->band);
00749     cpl_msg_debug(fctid, "Grating Resol       : %12d", grating->resol);
00750     cpl_msg_debug(fctid, "Grating Space       : %12.6f", grating->space);
00751     cpl_msg_debug(fctid, "Grating Theta       : %12.6f", grating->theta);
00752     cpl_msg_debug(fctid, "Grating FColl       : %12.6f", grating->fcoll);
00753     cpl_msg_debug(fctid, "Grating GCam        : %12.6f", grating->gcam);
00754     cpl_msg_debug(fctid, "Grating SlitDx      : %12.6f", grating->sdx);
00755     cpl_msg_debug(fctid, "Grating SlitDy      : %12.6f", grating->sdy);
00756     cpl_msg_debug(fctid, "Grating SlitPhi     : %12.6f", grating->sphi);
00757 
00758     return;
00759 
00760 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.5.2.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Fri Jun 13 14:36:22 2008 by doxygen 1.4.6 written by Dimitri van Heesch, © 1997-2004