source: tags/release-1.0.5/src/Utils/position_related.cc @ 1455

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

Introduced error and warning reporting functions, to normalise the format
of errors and warnings. Definitions of functions go in duchamp.cc.
Changed all code that reports errors/warnings to use these new functions.

Fixed a couple of bugs that affected the way 2D images were dealt with:
ReconSearch? now looks at how many dimensions there are in the data array
before choosing the dimension of the reconstruction, and the minChannels
test was improved for the case of minChannels=0.

Some minor additions made to the Guide as well.

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