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

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

Several bug fixes:

  • The determination of the blank pixel value was not working correctly, due

to confusion between which out of par and head had the correct values.
getCube now reads the header values into the FitsHeader? class, and then these
are copied into the Param class by the new function Param::copyHeaderInfo.

  • The zoom box in the spectral output was scaling the flux scale by all data

points, and this caused problems when the MW channels were in view. These
channels are now omitted in the determination of the flux axis range.

  • The precision in the implied position given by the IAU name has been

increased -- it is now of the format J125345-362412 or G323.124+05.457.

Also added a CHANGES file, as we want to go to v.1.0.1, and updated
the version number in configure.ac.

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