[3] | 1 | #include <iostream> |
---|
| 2 | #include <sstream> |
---|
| 3 | #include <iomanip> |
---|
| 4 | #include <string> |
---|
| 5 | #include <math.h> |
---|
| 6 | #include <Utils/utils.hh> |
---|
| 7 | |
---|
| 8 | using std::setw; |
---|
| 9 | using std::setfill; |
---|
| 10 | using std::setprecision; |
---|
| 11 | |
---|
| 12 | string 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.); |
---|
[112] | 24 | int s = (int)(fmod(raHrs,1./60.)*3600.); |
---|
[3] | 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; |
---|
[112] | 32 | ss<<setw(2)<<setfill('0')<<s; |
---|
[3] | 33 | int sign = int( dec / fabs(dec) ); |
---|
| 34 | double d = dec / sign; |
---|
| 35 | h = int(d); |
---|
| 36 | m = (int)(fmod(d,1.)*60.); |
---|
[112] | 37 | s = (int)(fmod(d,1./60.)*3600.); |
---|
[3] | 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; |
---|
[112] | 42 | ss<<setw(2)<<setfill('0')<<s; |
---|
[3] | 43 | return ss.str(); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | string getIAUNameGAL(double lon, double lat) |
---|
| 47 | { |
---|
| 48 | /** |
---|
| 49 | * string getIAUNameGAL(double, double) |
---|
| 50 | * both ra and dec are assumed to be in degrees. |
---|
[112] | 51 | * returns name of the form G321.123+01.234 |
---|
[3] | 52 | */ |
---|
| 53 | |
---|
| 54 | std::stringstream ss(std::stringstream::out); |
---|
| 55 | ss.setf(std::ios::showpoint); |
---|
| 56 | ss.setf(std::ios::fixed); |
---|
| 57 | ss<<"G"; |
---|
[112] | 58 | ss<<setw(7)<<setfill('0')<<setprecision(3)<<lon; |
---|
[3] | 59 | ss.setf(std::ios::showpos); |
---|
| 60 | ss.setf(std::ios::internal); |
---|
[112] | 61 | ss<<setw(7)<<setfill('0')<<setprecision(3)<<lat; |
---|
[3] | 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 | |
---|
[38] | 69 | string decToDMS(const double dec, const string type) |
---|
[3] | 70 | { |
---|
| 71 | /** |
---|
[38] | 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. |
---|
[3] | 79 | */ |
---|
| 80 | |
---|
| 81 | double dec_abs,sec; |
---|
| 82 | int deg,min; |
---|
| 83 | const double onemin=1./60.; |
---|
[38] | 84 | double thisDec = dec; |
---|
| 85 | string sign=""; |
---|
| 86 | int degSize = 2; // number of figures in the degrees part of the output. |
---|
[3] | 87 | |
---|
[38] | 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. |
---|
[201] | 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"; |
---|
[38] | 104 | while (thisDec < 0.) { thisDec += 360.; } |
---|
| 105 | while (thisDec >= 360.) { thisDec -= 360.; } |
---|
| 106 | thisDec /= 15.; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | dec_abs = fabs(thisDec); |
---|
[3] | 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); |
---|
[38] | 120 | ss << sign; |
---|
| 121 | ss << setw(degSize)<<setfill('0')<<deg<<":"; |
---|
[3] | 122 | ss<<setw(2)<<setfill('0')<<min<<":"; |
---|
| 123 | ss<<setw(5)<<setprecision(2)<<sec; |
---|
| 124 | return ss.str(); |
---|
| 125 | } |
---|
| 126 | |
---|
[38] | 127 | |
---|
[3] | 128 | double 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 | |
---|
| 160 | double 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 | } |
---|