source: trunk/src/Utils/position_related.cc @ 925

Last change on this file since 925 was 925, checked in by MatthewWhiting, 12 years ago

Fixes for #141 - making sure the incrementing of minutes flows through to degrees/hours if need be, and getting the IAU name correct (making use of the decToDMS code to avoid doubling up).

File size: 7.0 KB
Line 
1// -----------------------------------------------------------------------
2// position_related.cc: General utility functions related to WCS positions
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <sstream>
30#include <iomanip>
31#include <string>
32#include <stdlib.h>
33#include <math.h>
34#include <duchamp/Utils/utils.hh>
35
36using std::setw;
37using std::setfill;
38using std::setprecision;
39
40std::string getIAUNameEQ(double ra, double dec, float equinox)
41{
42  /**
43   * std::string getIAUNameEQ(double, double, float)
44   *  both ra and dec are assumed to be in degrees.
45   *  returns name of the form J123456-654321 for equinox = 2000,
46   *   and B1234-4321 otherwise
47   */
48
49  std::string rastr=decToDMS(ra,"RA",0);
50  rastr.erase(rastr.begin()+2);
51  rastr.erase(rastr.begin()+4);
52  if(equinox==2000.)
53    rastr.erase(rastr.begin()+6,rastr.end());
54  else
55    rastr.erase(rastr.begin()+4,rastr.end());
56  std::string decstr=decToDMS(dec,"DEC",0);
57  decstr.erase(decstr.begin()+3);
58  decstr.erase(decstr.begin()+5);
59  if(equinox==2000.)
60    decstr.erase(decstr.begin()+7,decstr.end());
61  else
62    decstr.erase(decstr.begin()+5,decstr.end());
63   
64  std::stringstream ss;
65  if(equinox==2000.) ss<<"J";
66  else ss<<"B";
67  ss<<rastr<<decstr;
68  return ss.str();
69
70}
71
72std::string getIAUNameGAL(double lon, double lat)
73{
74  /**
75   * std::string getIAUNameGAL(double, double)
76   *  both ra and dec are assumed to be in degrees.
77   *  returns name of the form G321.123+01.234
78   */
79
80  std::stringstream ss(std::stringstream::out);
81  ss.setf(std::ios::showpoint);
82  ss.setf(std::ios::fixed);
83  ss<<"G";
84  ss<<setw(7)<<setfill('0')<<setprecision(3)<<lon;
85  ss.setf(std::ios::showpos);
86  ss.setf(std::ios::internal);
87  ss<<setw(7)<<setfill('0')<<setprecision(3)<<lat;
88  ss.unsetf(std::ios::internal);
89  ss.unsetf(std::ios::showpos);
90  ss.unsetf(std::ios::showpoint);
91  ss.unsetf(std::ios::fixed);
92  return ss.str();
93}
94
95std::string decToDMS(const double dec, const std::string type, int decPrecision)
96{
97  /**
98   *Converts a decimal angle (in degrees) to a format reflecting the axis type:
99   *  RA   (right ascension):     hh:mm:ss.ss, with dec modulo 360. (24hrs)
100   *  DEC  (declination):        sdd:mm:ss.ss  (with sign, either + or -)
101   *  GLON (galactic longitude): ddd:mm:ss.ss, with dec made modulo 360.
102   *  GLAT (galactic latitude):  sdd:mm:ss.ss  (with sign, either + or -)
103   *    Any other type defaults to RA, and prints warning.
104   *
105   * \param dec Decimal value of the angle, in degrees.
106   * \param type String indicating desired type of output. Options RA, DEC,
107   *              GLON, GLAT
108   * \return String with angle in desired format.
109   */
110
111  double dec_abs,sec;
112  int deg,min;
113  const double onemin=1./60.;
114  double thisDec = dec;
115  std::string sign="";
116  int degSize = 2; // number of figures in the degrees part of the output.
117
118  int precision=std::max(0,decPrecision);
119  if(type=="RA") precision++;
120
121  if((type=="RA")||(type=="GLON")){
122    if(type=="GLON")  degSize = 3; // longitude has three figures in degrees.
123    // Make these modulo 360.;
124    while (thisDec < 0.) { thisDec += 360.; }
125    while (thisDec >= 360.) { thisDec -= 360.; }
126    if(type=="RA") thisDec /= 15.;  // Convert to hours.
127  }
128  else if((type=="DEC")||(type=="GLAT")){
129    if(thisDec<0.) sign = "-";
130    else sign = "+";
131  }
132  else { // UNKNOWN TYPE -- DEFAULT TO RA.
133    std::cerr << "WARNING <decToDMS> : Unknown axis type ("
134              << type << "). Defaulting to using RA.\n";
135    while (thisDec < 0.) { thisDec += 360.; }
136    while (thisDec >= 360.) { thisDec -= 360.; }
137    thisDec /= 15.;
138  }
139 
140  dec_abs = fabs(thisDec);
141  deg = int(dec_abs);
142  min = int(fmod(dec_abs,1.)*60.);
143  sec = fmod(dec_abs,onemin)*3600.;
144  if(fabs(sec-60.)<pow(10,precision)){ // to prevent rounding errors stuffing things up
145    sec=0.;
146    min++;
147    if(min==60){
148      min=0;
149      deg++;
150    }
151  }
152  std::stringstream ss(std::stringstream::out);
153  ss.setf(std::ios::showpoint);
154  ss.setf(std::ios::fixed);
155  ss << sign;
156  ss << setw(degSize)<<setfill('0')<<deg<<":";
157  ss<<setw(2)<<setfill('0')<<min<<":";
158  if(precision>0)
159    ss<<setw(precision+3)<<setprecision(precision)<<sec;
160  else {
161    //ss.unsetf(std::ios::showpoint);
162    //    ss.unsetf(std::ios::fixed);
163    ss << setw(2) << int(sec);
164    //    ss<<setw(2)<<setfill('0')<<setprecision(precision)<<sec;
165  }
166  return ss.str();
167}
168
169
170double dmsToDec(std::string dms)
171{
172  /**
173   *  double dmsToDec(string)
174   *   Converts a std::string in the format +12:23:34.45 to a decimal angle in degrees.
175   *   Assumes the angle given is in degrees, so if passing RA as the argument,
176   *   need to multiply by 15 to get the result in degrees rather than hours.
177   *   The sign of the angle is preserved, if present.
178   */
179
180
181  bool isNeg = false;
182  if(dms[0]=='-') isNeg = true;
183
184  std::stringstream ss;
185  ss.str(dms);
186  std::string deg,min,sec;
187  getline(ss,deg,':');
188  getline(ss,min,':');
189  getline(ss,sec);
190  char *end;
191  double d = strtod(deg.c_str(),&end);
192  double m = strtod(min.c_str(),&end);
193  double s = strtod(sec.c_str(),&end); 
194
195  double dec = fabs(d) + m/60. + s/3600.;
196  if(isNeg) dec = dec * -1.;
197
198  return dec;
199
200}
201
202const long double degToRadian=M_PI/180.;
203 
204double angularSeparation(double &ra1, double &dec1, double &ra2, double &dec2)
205{
206  /**
207   * double angularSeparation(double &,double &,double &,double &);
208   *  Enter ra & dec for two positions.
209   *    (all positions in degrees)
210   *  Returns the angular separation in degrees.
211   */
212
213  long double dra = (ra1-ra2)*degToRadian;
214  long double d1 = dec1*degToRadian;
215  long double d2 = dec2*degToRadian;
216  long double angsep;
217  if((fabs(ra1-ra2) < 1./3600.)&&(fabs(dec1-dec2)<1./3600.))
218    return sqrt(dra*dra + (d1-d2)*(d1-d2)) / degToRadian;
219  else {
220    if(fabs(ra1-ra2) < 1./3600.)
221      angsep = cos(d1)*cos(d2) - dra*dra*cos(d1)*cos(d2)/2. + sin(d1)*sin(d2);
222    else
223      angsep = cos(dra)*cos(d1)*cos(d2) + sin(d1)*sin(d2);
224    double dangsep = acos(angsep) / degToRadian;
225    return dangsep;
226  }
227
228}
Note: See TracBrowser for help on using the repository browser.