source: tags/release-1.1.4/src/Utils/position_related.cc @ 1441

Last change on this file since 1441 was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

File size: 5.4 KB
Line 
1#include <iostream>
2#include <sstream>
3#include <iomanip>
4#include <string>
5#include <math.h>
6#include <duchamp/Utils/utils.hh>
7
8using std::setw;
9using std::setfill;
10using std::setprecision;
11
12std::string getIAUNameEQ(double ra, double dec, float equinox)
13{
14  /**
15   * std::string getIAUNameEQ(double, double, float)
16   *  both ra and dec are assumed to be in degrees.
17   *  returns name of the form J1234-4321 for equinox = 2000,
18   *   and B1234-4321 otherwise
19   */
20
21  double raHrs = ra / 15.;
22  int h = int(raHrs);
23  int m = (int)(fmod(raHrs,1.)*60.);
24  int s = (int)(fmod(raHrs,1./60.)*3600.);
25  std::stringstream ss(std::stringstream::out);
26  ss.setf(std::ios::showpoint);
27  ss.setf(std::ios::fixed);
28  if(equinox==2000.) ss << "J";
29  else ss << "B";
30  ss<<setw(2)<<setfill('0')<<h;
31  ss<<setw(2)<<setfill('0')<<m;
32  ss<<setw(2)<<setfill('0')<<s;
33  int sign = int( dec / fabs(dec) );
34  double d = dec / sign;
35  h = int(d);
36  m = (int)(fmod(d,1.)*60.);
37  s = (int)(fmod(d,1./60.)*3600.);
38  if(sign==1) ss<<"+"; else ss<<"-";
39  ss<<setw(2)<<setfill('0')<<h;
40  ss.unsetf(std::ios::showpos);
41  ss<<setw(2)<<setfill('0')<<m;
42  ss<<setw(2)<<setfill('0')<<s;
43  return ss.str();
44}
45
46std::string getIAUNameGAL(double lon, double lat)
47{
48  /**
49   * std::string getIAUNameGAL(double, double)
50   *  both ra and dec are assumed to be in degrees.
51   *  returns name of the form G321.123+01.234
52   */
53
54  std::stringstream ss(std::stringstream::out);
55  ss.setf(std::ios::showpoint);
56  ss.setf(std::ios::fixed);
57  ss<<"G";
58  ss<<setw(7)<<setfill('0')<<setprecision(3)<<lon;
59  ss.setf(std::ios::showpos);
60  ss.setf(std::ios::internal);
61  ss<<setw(7)<<setfill('0')<<setprecision(3)<<lat;
62  ss.unsetf(std::ios::internal);
63  ss.unsetf(std::ios::showpos);
64  ss.unsetf(std::ios::showpoint);
65  ss.unsetf(std::ios::fixed);
66  return ss.str();
67}
68
69std::string decToDMS(const double dec, const std::string type)
70{
71  /**
72   *Converts a decimal angle (in degrees) to a format reflecting the axis type:
73   *  RA   (right ascension):     hh:mm:ss.ss, with dec modulo 360. (24hrs)
74   *  DEC  (declination):        sdd:mm:ss.ss  (with sign, either + or -)
75   *  GLON (galactic longitude): ddd:mm:ss.ss, with dec made modulo 360.
76   *  GLAT (galactic latitude):  sdd:mm:ss.ss  (with sign, either + or -)
77   *    Any other type defaults to RA, and prints warning.
78   *
79   * \param dec Decimal value of the angle, in degrees.
80   * \param type String indicating desired type of output. Options RA, DEC,
81   *              GLON, GLAT
82   * \return String with angle in desired format.
83   */
84
85  double dec_abs,sec;
86  int deg,min;
87  const double onemin=1./60.;
88  double thisDec = dec;
89  std::string sign="";
90  int degSize = 2; // number of figures in the degrees part of the output.
91
92  if((type=="RA")||(type=="GLON")){
93    if(type=="GLON")  degSize = 3; // longitude has three figures in degrees.
94    // Make these modulo 360.;
95    while (thisDec < 0.) { thisDec += 360.; }
96    while (thisDec >= 360.) { thisDec -= 360.; }
97    if(type=="RA") thisDec /= 15.;  // Convert to hours.
98  }
99  else if((type=="DEC")||(type=="GLAT")){
100    if(thisDec<0.) sign = "-";
101    else sign = "+";
102  }
103  else { // UNKNOWN TYPE -- DEFAULT TO RA.
104    std::cerr << "WARNING <decToDMS> : Unknown axis type ("
105              << type << "). Defaulting to using RA.\n";
106    while (thisDec < 0.) { thisDec += 360.; }
107    while (thisDec >= 360.) { thisDec -= 360.; }
108    thisDec /= 15.;
109  }
110 
111  dec_abs = fabs(thisDec);
112  deg = int(dec_abs);
113  min = int(fmod(dec_abs,1.)*60.);
114  sec = fmod(dec_abs,onemin)*3600.;
115  if(fabs(sec-60.)<1.e-10){ // to prevent rounding errors stuffing things up
116    sec=0.;
117    min++;
118  }
119  std::stringstream ss(std::stringstream::out);
120  ss.setf(std::ios::showpoint);
121  ss.setf(std::ios::fixed);
122  ss << sign;
123  ss << setw(degSize)<<setfill('0')<<deg<<":";
124  ss<<setw(2)<<setfill('0')<<min<<":";
125  ss<<setw(5)<<setprecision(2)<<sec;
126  return ss.str();
127}
128
129
130double dmsToDec(std::string dms)
131{
132  /**
133   *  double dmsToDec(string)
134   *   Converts a std::string in the format +12:23:34.45 to a decimal angle in degrees.
135   *   Assumes the angle given is in degrees, so if passing RA as the argument,
136   *   need to multiply by 15 to get the result in degrees rather than hours.
137   *   The sign of the angle is preserved, if present.
138   */
139
140
141  bool isNeg = false;
142  if(dms[0]=='-') isNeg = true;
143
144  std::stringstream ss;
145  ss.str(dms);
146  std::string deg,min,sec;
147  getline(ss,deg,':');
148  getline(ss,min,':');
149  getline(ss,sec);
150  char *end;
151  double d = strtod(deg.c_str(),&end);
152  double m = strtod(min.c_str(),&end);
153  double s = strtod(sec.c_str(),&end); 
154
155  double dec = fabs(d) + m/60. + s/3600.;
156  if(isNeg) dec = dec * -1.;
157
158  return dec;
159
160}
161
162const long double degToRadian=M_PI/180.;
163 
164double angularSeparation(double &ra1, double &dec1, double &ra2, double &dec2)
165{
166  /**
167   * double angularSeparation(double &,double &,double &,double &);
168   *  Enter ra & dec for two positions.
169   *    (all positions in degrees)
170   *  Returns the angular separation in degrees.
171   */
172
173  long double dra = (ra1-ra2)*degToRadian;
174  long double d1 = dec1*degToRadian;
175  long double d2 = dec2*degToRadian;
176  long double angsep;
177  if((fabs(ra1-ra2) < 1./3600.)&&(fabs(dec1-dec2)<1./3600.))
178    return sqrt(dra*dra + (d1-d2)*(d1-d2)) / degToRadian;
179  else {
180    if(fabs(ra1-ra2) < 1./3600.)
181      angsep = cos(d1)*cos(d2) - dra*dra*cos(d1)*cos(d2)/2. + sin(d1)*sin(d2);
182    else
183      angsep = cos(dra)*cos(d1)*cos(d2) + sin(d1)*sin(d2);
184    double dangsep = acos(angsep) / degToRadian;
185    return dangsep;
186  }
187
188}
Note: See TracBrowser for help on using the repository browser.