source: branches/fitshead-branch/Utils/position_related.cc @ 1441

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

Major update to this branch, with all the edits to date.
Have added a new class FitsHeader?, to incorporate the wcsprm structs,
plus other header information such as units, beam size and so on...
Not quite working, but nearly there.

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