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

Last change on this file since 208 was 201, checked in by Matthew Whiting, 18 years ago
  • New functionality added: ability to smooth a cube (in each individual spectrum) using a hanning filter before searching.
  • This comes with associated input parameters: flagSmooth and hanningWidth.
  • Hanning smoothing implemented via a new class that does the smoothing
  • Other minor changes:
    • changed the way getIAUName is called.
    • new colour names in mycpgplot
    • a << operator for the StatsContainer? class
    • more robust ProgressBar? functions
    • updated the Makefile.in
File size: 5.0 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  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
46string getIAUNameGAL(double lon, double lat)
47{
48  /**
49   * 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
69string decToDMS(const double dec, const string type)
70{
71  /**
72   *  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  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(string dms)
129{
130  /**
131   *  double dmsToDec(string)
132   *   Converts a 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  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 
160double angularSeparation(double &ra1, double &dec1, double &ra2, double &dec2)
161{
162  /**
163   * double angularSeparation(double &,double &,double &,double &);
164   *  Enter ra & dec for two positions.
165   *    (all positions in degrees)
166   *  Returns the angular separation in degrees.
167   */
168
169  double angsep = cos((ra1-ra2)*M_PI/180.)*cos(dec1*M_PI/180.)*cos(dec2*M_PI/180.)
170    + sin(dec1*M_PI/180.)*sin(dec2*M_PI/180.);
171  angsep = acos(angsep)*180./M_PI;
172
173  return angsep;
174
175}
Note: See TracBrowser for help on using the repository browser.