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

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

Minor changes to comments and readability.

File size: 5.4 KB
RevLine 
[3]1#include <iostream>
2#include <sstream>
3#include <iomanip>
4#include <string>
5#include <math.h>
6#include <Utils/utils.hh>
7
8using std::setw;
9using std::setfill;
10using std::setprecision;
11
[232]12std::string getIAUNameEQ(double ra, double dec, float equinox)
[3]13{
14  /**
[232]15   * std::string getIAUNameEQ(double, double, float)
[3]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.);
[112]24  int s = (int)(fmod(raHrs,1./60.)*3600.);
[3]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;
[112]32  ss<<setw(2)<<setfill('0')<<s;
[3]33  int sign = int( dec / fabs(dec) );
34  double d = dec / sign;
35  h = int(d);
36  m = (int)(fmod(d,1.)*60.);
[112]37  s = (int)(fmod(d,1./60.)*3600.);
[3]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;
[112]42  ss<<setw(2)<<setfill('0')<<s;
[3]43  return ss.str();
44}
45
[232]46std::string getIAUNameGAL(double lon, double lat)
[3]47{
48  /**
[232]49   * std::string getIAUNameGAL(double, double)
[3]50   *  both ra and dec are assumed to be in degrees.
[112]51   *  returns name of the form G321.123+01.234
[3]52   */
53
54  std::stringstream ss(std::stringstream::out);
55  ss.setf(std::ios::showpoint);
56  ss.setf(std::ios::fixed);
57  ss<<"G";
[112]58  ss<<setw(7)<<setfill('0')<<setprecision(3)<<lon;
[3]59  ss.setf(std::ios::showpos);
60  ss.setf(std::ios::internal);
[112]61  ss<<setw(7)<<setfill('0')<<setprecision(3)<<lat;
[3]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
[232]69std::string decToDMS(const double dec, const std::string type)
[3]70{
71  /**
[324]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 -)
[38]77   *    Any other type defaults to RA, and prints warning.
[324]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.
[3]83   */
84
85  double dec_abs,sec;
86  int deg,min;
87  const double onemin=1./60.;
[38]88  double thisDec = dec;
[232]89  std::string sign="";
[38]90  int degSize = 2; // number of figures in the degrees part of the output.
[3]91
[38]92  if((type=="RA")||(type=="GLON")){
[324]93    if(type=="GLON")  degSize = 3; // longitude has three figures in degrees.
[38]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.
[201]104    std::cerr << "WARNING <decToDMS> : Unknown axis type ("
105              << type << "). Defaulting to using RA.\n";
[38]106    while (thisDec < 0.) { thisDec += 360.; }
107    while (thisDec >= 360.) { thisDec -= 360.; }
108    thisDec /= 15.;
109  }
110 
111  dec_abs = fabs(thisDec);
[324]112  deg = int(dec_abs);
113  min = int(fmod(dec_abs,1.)*60.);
[3]114  sec = fmod(dec_abs,onemin)*3600.;
[324]115  if(fabs(sec-60.)<1.e-10){ // to prevent rounding errors stuffing things up
[3]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);
[38]122  ss << sign;
123  ss << setw(degSize)<<setfill('0')<<deg<<":";
[3]124  ss<<setw(2)<<setfill('0')<<min<<":";
125  ss<<setw(5)<<setprecision(2)<<sec;
126  return ss.str();
127}
128
[38]129
[232]130double dmsToDec(std::string dms)
[3]131{
132  /**
133   *  double dmsToDec(string)
[232]134   *   Converts a std::string in the format +12:23:34.45 to a decimal angle in degrees.
[3]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);
[232]146  std::string deg,min,sec;
[3]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}
[213]161
162const long double degToRadian=M_PI/180.;
[3]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
[213]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  }
[3]187
188}
Note: See TracBrowser for help on using the repository browser.