[643] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // position_related.cc: General utility functions related to WCS positions |
---|
| 3 | // ----------------------------------------------------------------------- |
---|
| 4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 5 | // |
---|
| 6 | // This program is free software; you can redistribute it and/or modify it |
---|
| 7 | // under the terms of the GNU General Public License as published by the |
---|
| 8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 9 | // option) any later version. |
---|
| 10 | // |
---|
| 11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 14 | // for more details. |
---|
| 15 | // |
---|
| 16 | // You should have received a copy of the GNU General Public License |
---|
| 17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | // |
---|
| 20 | // Correspondence concerning Duchamp may be directed to: |
---|
| 21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 22 | // Postal address: Dr. Matthew Whiting |
---|
| 23 | // Australia Telescope National Facility, CSIRO |
---|
| 24 | // PO Box 76 |
---|
| 25 | // Epping NSW 1710 |
---|
| 26 | // AUSTRALIA |
---|
| 27 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
| 29 | #include <sstream> |
---|
| 30 | #include <iomanip> |
---|
| 31 | #include <string> |
---|
[643] | 32 | #include <stdlib.h> |
---|
[3] | 33 | #include <math.h> |
---|
[393] | 34 | #include <duchamp/Utils/utils.hh> |
---|
[3] | 35 | |
---|
| 36 | using std::setw; |
---|
| 37 | using std::setfill; |
---|
| 38 | using std::setprecision; |
---|
| 39 | |
---|
[232] | 40 | std::string getIAUNameEQ(double ra, double dec, float equinox) |
---|
[3] | 41 | { |
---|
| 42 | /** |
---|
[232] | 43 | * std::string getIAUNameEQ(double, double, float) |
---|
[3] | 44 | * both ra and dec are assumed to be in degrees. |
---|
| 45 | * returns name of the form J1234-4321 for equinox = 2000, |
---|
| 46 | * and B1234-4321 otherwise |
---|
| 47 | */ |
---|
| 48 | |
---|
| 49 | double raHrs = ra / 15.; |
---|
| 50 | int h = int(raHrs); |
---|
| 51 | int m = (int)(fmod(raHrs,1.)*60.); |
---|
[112] | 52 | int s = (int)(fmod(raHrs,1./60.)*3600.); |
---|
[3] | 53 | std::stringstream ss(std::stringstream::out); |
---|
| 54 | ss.setf(std::ios::showpoint); |
---|
| 55 | ss.setf(std::ios::fixed); |
---|
| 56 | if(equinox==2000.) ss << "J"; |
---|
| 57 | else ss << "B"; |
---|
| 58 | ss<<setw(2)<<setfill('0')<<h; |
---|
| 59 | ss<<setw(2)<<setfill('0')<<m; |
---|
[112] | 60 | ss<<setw(2)<<setfill('0')<<s; |
---|
[3] | 61 | int sign = int( dec / fabs(dec) ); |
---|
| 62 | double d = dec / sign; |
---|
| 63 | h = int(d); |
---|
| 64 | m = (int)(fmod(d,1.)*60.); |
---|
[112] | 65 | s = (int)(fmod(d,1./60.)*3600.); |
---|
[3] | 66 | if(sign==1) ss<<"+"; else ss<<"-"; |
---|
| 67 | ss<<setw(2)<<setfill('0')<<h; |
---|
| 68 | ss.unsetf(std::ios::showpos); |
---|
| 69 | ss<<setw(2)<<setfill('0')<<m; |
---|
[112] | 70 | ss<<setw(2)<<setfill('0')<<s; |
---|
[3] | 71 | return ss.str(); |
---|
| 72 | } |
---|
| 73 | |
---|
[232] | 74 | std::string getIAUNameGAL(double lon, double lat) |
---|
[3] | 75 | { |
---|
| 76 | /** |
---|
[232] | 77 | * std::string getIAUNameGAL(double, double) |
---|
[3] | 78 | * both ra and dec are assumed to be in degrees. |
---|
[112] | 79 | * returns name of the form G321.123+01.234 |
---|
[3] | 80 | */ |
---|
| 81 | |
---|
| 82 | std::stringstream ss(std::stringstream::out); |
---|
| 83 | ss.setf(std::ios::showpoint); |
---|
| 84 | ss.setf(std::ios::fixed); |
---|
| 85 | ss<<"G"; |
---|
[112] | 86 | ss<<setw(7)<<setfill('0')<<setprecision(3)<<lon; |
---|
[3] | 87 | ss.setf(std::ios::showpos); |
---|
| 88 | ss.setf(std::ios::internal); |
---|
[112] | 89 | ss<<setw(7)<<setfill('0')<<setprecision(3)<<lat; |
---|
[3] | 90 | ss.unsetf(std::ios::internal); |
---|
| 91 | ss.unsetf(std::ios::showpos); |
---|
| 92 | ss.unsetf(std::ios::showpoint); |
---|
| 93 | ss.unsetf(std::ios::fixed); |
---|
| 94 | return ss.str(); |
---|
| 95 | } |
---|
| 96 | |
---|
[232] | 97 | std::string decToDMS(const double dec, const std::string type) |
---|
[3] | 98 | { |
---|
| 99 | /** |
---|
[324] | 100 | *Converts a decimal angle (in degrees) to a format reflecting the axis type: |
---|
| 101 | * RA (right ascension): hh:mm:ss.ss, with dec modulo 360. (24hrs) |
---|
| 102 | * DEC (declination): sdd:mm:ss.ss (with sign, either + or -) |
---|
| 103 | * GLON (galactic longitude): ddd:mm:ss.ss, with dec made modulo 360. |
---|
| 104 | * GLAT (galactic latitude): sdd:mm:ss.ss (with sign, either + or -) |
---|
[38] | 105 | * Any other type defaults to RA, and prints warning. |
---|
[324] | 106 | * |
---|
| 107 | * \param dec Decimal value of the angle, in degrees. |
---|
| 108 | * \param type String indicating desired type of output. Options RA, DEC, |
---|
| 109 | * GLON, GLAT |
---|
| 110 | * \return String with angle in desired format. |
---|
[3] | 111 | */ |
---|
| 112 | |
---|
| 113 | double dec_abs,sec; |
---|
| 114 | int deg,min; |
---|
| 115 | const double onemin=1./60.; |
---|
[38] | 116 | double thisDec = dec; |
---|
[232] | 117 | std::string sign=""; |
---|
[38] | 118 | int degSize = 2; // number of figures in the degrees part of the output. |
---|
[3] | 119 | |
---|
[38] | 120 | if((type=="RA")||(type=="GLON")){ |
---|
[324] | 121 | if(type=="GLON") degSize = 3; // longitude has three figures in degrees. |
---|
[38] | 122 | // Make these modulo 360.; |
---|
| 123 | while (thisDec < 0.) { thisDec += 360.; } |
---|
| 124 | while (thisDec >= 360.) { thisDec -= 360.; } |
---|
| 125 | if(type=="RA") thisDec /= 15.; // Convert to hours. |
---|
| 126 | } |
---|
| 127 | else if((type=="DEC")||(type=="GLAT")){ |
---|
| 128 | if(thisDec<0.) sign = "-"; |
---|
| 129 | else sign = "+"; |
---|
| 130 | } |
---|
| 131 | else { // UNKNOWN TYPE -- DEFAULT TO RA. |
---|
[201] | 132 | std::cerr << "WARNING <decToDMS> : Unknown axis type (" |
---|
| 133 | << type << "). Defaulting to using RA.\n"; |
---|
[38] | 134 | while (thisDec < 0.) { thisDec += 360.; } |
---|
| 135 | while (thisDec >= 360.) { thisDec -= 360.; } |
---|
| 136 | thisDec /= 15.; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | dec_abs = fabs(thisDec); |
---|
[324] | 140 | deg = int(dec_abs); |
---|
| 141 | min = int(fmod(dec_abs,1.)*60.); |
---|
[3] | 142 | sec = fmod(dec_abs,onemin)*3600.; |
---|
[324] | 143 | if(fabs(sec-60.)<1.e-10){ // to prevent rounding errors stuffing things up |
---|
[3] | 144 | sec=0.; |
---|
| 145 | min++; |
---|
| 146 | } |
---|
| 147 | std::stringstream ss(std::stringstream::out); |
---|
| 148 | ss.setf(std::ios::showpoint); |
---|
| 149 | ss.setf(std::ios::fixed); |
---|
[38] | 150 | ss << sign; |
---|
| 151 | ss << setw(degSize)<<setfill('0')<<deg<<":"; |
---|
[3] | 152 | ss<<setw(2)<<setfill('0')<<min<<":"; |
---|
| 153 | ss<<setw(5)<<setprecision(2)<<sec; |
---|
| 154 | return ss.str(); |
---|
| 155 | } |
---|
| 156 | |
---|
[38] | 157 | |
---|
[232] | 158 | double dmsToDec(std::string dms) |
---|
[3] | 159 | { |
---|
| 160 | /** |
---|
| 161 | * double dmsToDec(string) |
---|
[232] | 162 | * Converts a std::string in the format +12:23:34.45 to a decimal angle in degrees. |
---|
[3] | 163 | * Assumes the angle given is in degrees, so if passing RA as the argument, |
---|
| 164 | * need to multiply by 15 to get the result in degrees rather than hours. |
---|
| 165 | * The sign of the angle is preserved, if present. |
---|
| 166 | */ |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | bool isNeg = false; |
---|
| 170 | if(dms[0]=='-') isNeg = true; |
---|
| 171 | |
---|
| 172 | std::stringstream ss; |
---|
| 173 | ss.str(dms); |
---|
[232] | 174 | std::string deg,min,sec; |
---|
[3] | 175 | getline(ss,deg,':'); |
---|
| 176 | getline(ss,min,':'); |
---|
| 177 | getline(ss,sec); |
---|
| 178 | char *end; |
---|
| 179 | double d = strtod(deg.c_str(),&end); |
---|
| 180 | double m = strtod(min.c_str(),&end); |
---|
| 181 | double s = strtod(sec.c_str(),&end); |
---|
| 182 | |
---|
| 183 | double dec = fabs(d) + m/60. + s/3600.; |
---|
| 184 | if(isNeg) dec = dec * -1.; |
---|
| 185 | |
---|
| 186 | return dec; |
---|
| 187 | |
---|
| 188 | } |
---|
[213] | 189 | |
---|
| 190 | const long double degToRadian=M_PI/180.; |
---|
[3] | 191 | |
---|
| 192 | double angularSeparation(double &ra1, double &dec1, double &ra2, double &dec2) |
---|
| 193 | { |
---|
| 194 | /** |
---|
| 195 | * double angularSeparation(double &,double &,double &,double &); |
---|
| 196 | * Enter ra & dec for two positions. |
---|
| 197 | * (all positions in degrees) |
---|
| 198 | * Returns the angular separation in degrees. |
---|
| 199 | */ |
---|
| 200 | |
---|
[213] | 201 | long double dra = (ra1-ra2)*degToRadian; |
---|
| 202 | long double d1 = dec1*degToRadian; |
---|
| 203 | long double d2 = dec2*degToRadian; |
---|
| 204 | long double angsep; |
---|
| 205 | if((fabs(ra1-ra2) < 1./3600.)&&(fabs(dec1-dec2)<1./3600.)) |
---|
| 206 | return sqrt(dra*dra + (d1-d2)*(d1-d2)) / degToRadian; |
---|
| 207 | else { |
---|
| 208 | if(fabs(ra1-ra2) < 1./3600.) |
---|
| 209 | angsep = cos(d1)*cos(d2) - dra*dra*cos(d1)*cos(d2)/2. + sin(d1)*sin(d2); |
---|
| 210 | else |
---|
| 211 | angsep = cos(dra)*cos(d1)*cos(d2) + sin(d1)*sin(d2); |
---|
| 212 | double dangsep = acos(angsep) / degToRadian; |
---|
| 213 | return dangsep; |
---|
| 214 | } |
---|
[3] | 215 | |
---|
| 216 | } |
---|