irplib_wcs.c

00001 /* $Id: irplib_wcs.c,v 1.6 2009/08/12 15:02:04 cgarcia Exp $
00002  *
00003  * This file is part of the irplib package
00004  * Copyright (C) 2002,2003 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  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: cgarcia $
00023  * $Date: 2009/08/12 15:02:04 $
00024  * $Revision: 1.6 $
00025  * $Name: naco-4_2_1 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <cpl.h>
00038 
00039 #include "irplib_wcs.h"
00040 
00041 /*----------------------------------------------------------------------------*/
00045 /*----------------------------------------------------------------------------*/
00046 
00049 /*----------------------------------------------------------------------------*/
00061 /*----------------------------------------------------------------------------*/
00062 cpl_error_code irplib_wcs_xytoradec
00063 (const cpl_wcs *wcs,
00064  double         x,
00065  double         y,
00066  double        *ra,
00067  double        *dec)
00068 {
00069     double *        xy;
00070     double *        radec;
00071     cpl_matrix *    from;
00072     cpl_matrix *    to;
00073     cpl_array *     status;
00074     cpl_error_code  err_code;
00075 
00076     /* Load up the information */
00077 
00078     from = cpl_matrix_new(1,2);
00079     xy = cpl_matrix_get_data(from);
00080     xy[0] = x;
00081     xy[1] = y;
00082 
00083     /* Call the conversion routine */
00084 
00085     err_code =  cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_PHYS2WORLD);
00086 
00087     /* Pass it back now */
00088 
00089     radec = cpl_matrix_get_data(to);
00090     *ra = radec[0];
00091     *dec = radec[1];
00092 
00093     /* Tidy and exit */
00094 
00095     cpl_matrix_delete(from);
00096     cpl_matrix_delete(to);
00097     cpl_array_delete(status);
00098 
00099     return err_code;
00100 }
00101 
00102 /*----------------------------------------------------------------------------*/
00113 /*----------------------------------------------------------------------------*/
00114 cpl_error_code irplib_wcs_radectoxy
00115 (const cpl_wcs * wcs,
00116  double          ra,
00117  double          dec,
00118  double        * x,
00119  double        * y)
00120 {
00121     cpl_matrix *    radec_coord;
00122     cpl_matrix *    xy_coord;
00123     cpl_array *     status;
00124     cpl_error_code  err_code;
00125 
00126     /* Feed the matrix with RA, DEC */
00127     radec_coord = cpl_matrix_new(1, 2);
00128     cpl_matrix_set(radec_coord, 0, 0, ra);
00129     cpl_matrix_set(radec_coord, 0, 1, dec);
00130 
00131     xy_coord = NULL;
00132     status   = NULL;
00133     err_code = cpl_wcs_convert(wcs, radec_coord, &xy_coord,
00134                                &status, CPL_WCS_WORLD2PHYS);
00135 
00136     *x  = cpl_matrix_get(xy_coord, 0, 0);
00137     *y  = cpl_matrix_get(xy_coord, 0, 1);
00138 
00139     /* Tidy and exit */
00140 
00141     cpl_array_delete(status);
00142     cpl_matrix_delete(radec_coord);
00143     cpl_matrix_delete(xy_coord);
00144 
00145     return err_code;
00146 
00147 }
00148 
00149 /*----------------------------------------------------------------------------*/
00159 /*----------------------------------------------------------------------------*/
00160 double irplib_wcs_great_circle_dist(double ra1,
00161                                     double dec1,
00162                                     double ra2,
00163                                     double dec2)
00164 {
00165 
00166     /* Convert all input from degrees to radian - and back for the result */
00167     const double dra  = sin( CPL_MATH_RAD_DEG * (ra2  - ra1 )/2.0 );
00168     const double ddec = sin( CPL_MATH_RAD_DEG * (dec2 - dec1)/2.0 );
00169 
00170     dec1 *= CPL_MATH_RAD_DEG;
00171     dec2 *= CPL_MATH_RAD_DEG;
00172 
00173     return 2.0 * asin(sqrt( ddec*ddec + cos(dec1)*cos(dec2)*dra*dra))
00174         * CPL_MATH_DEG_RAD;
00175 }
00176 

Generated on Tue Jun 29 12:16:39 2010 for NACO Pipeline Reference Manual by  doxygen 1.4.7