source: tags/release-0.9/Utils/position_related.cc

Last change on this file was 38, checked in by Matthew Whiting, 18 years ago

Utils/wcsFunctions.cc Added a single-pixel-offset that had been missed in

rev.22.

Utils/position_related.cc Improved decToDMS to take into account different

axis types, and format the string accordingly.

Utils/utils.hh Corrected header call to match decToDMS change.
Detection/detection.cc Changed calls of decToDMS. Removed use of

Detection::lngRatio, as now unnecessary.

Detection/detection.hh Removed lngRatio and accessor functions from

Detection definition.

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