source: branches/pixel-map-branch/src/Utils/position_related.cc @ 1441

Last change on this file since 1441 was 232, checked in by Matthew Whiting, 17 years ago

Large raft of changes. Most are minor ones related to fixing up the use of std::string and std::vector (whether they are declared as using, or not...). Other changes include:

  • Moving the reconstruction filter class Filter into its own header/implementation files filter.{hh,cc}. As a result, the atrous.cc file is removed, but atrous.hh remains with just the function prototypes.
  • Incorporating a Filter object into the Param set, so that the reconstruction routines can see it without the messy "extern" call. The define() function is now called in both the Param() and Param::readParams() functions, and no longer in the main body.
  • Col functions in columns.cc moved into the Column namespace, while the template function printEntry is moved into the columns.hh file -- it would not compile on delphinus with it in the columns.cc, even though all the implementations were present.
  • Improved the introductory section of the Guide, with a bit more detail about each of the execution steps. This way the reader can read this section and have a reasonably good idea about what is happening.
  • Other minor changes to the Guide.
File size: 5.4 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
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   *  std::string decToDMS(double, string)
73   *   converts a decimal angle (in degrees) to a format reflecting the axis type:
74   *       RA   (right ascension)    --> hh:mm:ss.ss, with dec made modulo 360. (or 24hrs)
75   *       DEC  (declination)        --> sdd:mm:ss.ss  (with sign, either + or -)
76   *       GLON (galactic longitude) --> ddd:mm:ss.ss, with dec made modulo 360.
77   *       GLAT (galactic latitude)  --> sdd:mm:ss.ss  (with sign, either + or -)
78   *    Any other type defaults to RA, and prints warning.
79   */
80
81  double dec_abs,sec;
82  int deg,min;
83  const double onemin=1./60.;
84  double thisDec = dec;
85  std::string sign="";
86  int degSize = 2; // number of figures in the degrees part of the output.
87
88  if((type=="RA")||(type=="GLON")){
89    if(type=="GLON")  degSize = 3; // three figures in degrees when doing longitude.
90    // Make these modulo 360.;
91    while (thisDec < 0.) { thisDec += 360.; }
92    while (thisDec >= 360.) { thisDec -= 360.; }
93    if(type=="RA") thisDec /= 15.;  // Convert to hours.
94  }
95  else if((type=="DEC")||(type=="GLAT")){
96    if(thisDec<0.) sign = "-";
97    else sign = "+";
98  }
99  else { // UNKNOWN TYPE -- DEFAULT TO RA.
100//     duchampWarning("decToDMS",
101//                 "Unknown axis type (" + type + "). Defaulting to using RA.\n");
102    std::cerr << "WARNING <decToDMS> : Unknown axis type ("
103              << type << "). Defaulting to using RA.\n";
104    while (thisDec < 0.) { thisDec += 360.; }
105    while (thisDec >= 360.) { thisDec -= 360.; }
106    thisDec /= 15.;
107  }
108 
109  dec_abs = fabs(thisDec);
110  deg = int(dec_abs);//floor(d)
111  min = (int)(fmod(dec_abs,1.)*60.);
112  sec = fmod(dec_abs,onemin)*3600.;
113  if(fabs(sec-60.)<1.e-10){ /* to prevent rounding errors stuffing things up*/
114    sec=0.;
115    min++;
116  }
117  std::stringstream ss(std::stringstream::out);
118  ss.setf(std::ios::showpoint);
119  ss.setf(std::ios::fixed);
120  ss << sign;
121  ss << setw(degSize)<<setfill('0')<<deg<<":";
122  ss<<setw(2)<<setfill('0')<<min<<":";
123  ss<<setw(5)<<setprecision(2)<<sec;
124  return ss.str();
125}
126
127
128double dmsToDec(std::string dms)
129{
130  /**
131   *  double dmsToDec(string)
132   *   Converts a std::string in the format +12:23:34.45 to a decimal angle in degrees.
133   *   Assumes the angle given is in degrees, so if passing RA as the argument,
134   *   need to multiply by 15 to get the result in degrees rather than hours.
135   *   The sign of the angle is preserved, if present.
136   */
137
138
139  bool isNeg = false;
140  if(dms[0]=='-') isNeg = true;
141
142  std::stringstream ss;
143  ss.str(dms);
144  std::string deg,min,sec;
145  getline(ss,deg,':');
146  getline(ss,min,':');
147  getline(ss,sec);
148  char *end;
149  double d = strtod(deg.c_str(),&end);
150  double m = strtod(min.c_str(),&end);
151  double s = strtod(sec.c_str(),&end); 
152
153  double dec = fabs(d) + m/60. + s/3600.;
154  if(isNeg) dec = dec * -1.;
155
156  return dec;
157
158}
159
160const long double degToRadian=M_PI/180.;
161 
162double angularSeparation(double &ra1, double &dec1, double &ra2, double &dec2)
163{
164  /**
165   * double angularSeparation(double &,double &,double &,double &);
166   *  Enter ra & dec for two positions.
167   *    (all positions in degrees)
168   *  Returns the angular separation in degrees.
169   */
170
171  long double dra = (ra1-ra2)*degToRadian;
172  long double d1 = dec1*degToRadian;
173  long double d2 = dec2*degToRadian;
174  long double angsep;
175  if((fabs(ra1-ra2) < 1./3600.)&&(fabs(dec1-dec2)<1./3600.))
176    return sqrt(dra*dra + (d1-d2)*(d1-d2)) / degToRadian;
177  else {
178    if(fabs(ra1-ra2) < 1./3600.)
179      angsep = cos(d1)*cos(d2) - dra*dra*cos(d1)*cos(d2)/2. + sin(d1)*sin(d2);
180    else
181      angsep = cos(dra)*cos(d1)*cos(d2) + sin(d1)*sin(d2);
182    double dangsep = acos(angsep) / degToRadian;
183    return dangsep;
184  }
185
186}
Note: See TracBrowser for help on using the repository browser.