File: radeccoord.h

package info (click to toggle)
wsclean 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,196 kB
  • sloc: cpp: 34,504; ansic: 234; python: 174; makefile: 10
file content (289 lines) | stat: -rw-r--r-- 8,269 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef RADECCOORD_H
#define RADECCOORD_H

#include <string>
#include <sstream>
#include <stdexcept>
#include <cstdlib>
#include <cmath>

#ifndef M_PIl
#define M_PIl 3.141592653589793238462643383279502884L
#endif

class RaDecCoord
{
	private:
		static bool isRASeparator(char c) { return c==':' || c==' '; }
		static bool isDecSeparator(char c) { return c=='.' || c==' '; }
	public:
		static long double ParseRA(const std::string &str)
		{
			char *cstr;
			bool sign = false;
			for(size_t i=0; i!=str.size(); ++i)
			{
				if(str[i] == '-')
				{
					sign = true;
					break;
				} else if(str[i] != ' ')
				{
					sign = false;
					break;
				}
			}
			long double secs=0.0, mins=0.0,
				hrs = strtol(str.c_str(), &cstr, 10);
			// Parse format '00h00m00.0s'
			if(*cstr == 'h')
			{
				++cstr;
				mins = strtol(cstr, &cstr, 10);
				if(*cstr == 'm')
				{
					++cstr;
					secs = strtold(cstr, &cstr);
					if(*cstr == 's')
						++cstr;
					else throw std::runtime_error("Missing 's'");
				} else throw std::runtime_error("Missing 'm'");
			}
			// Parse format '00:00:00.0'
			else if(isRASeparator(*cstr))
			{
				++cstr;
				mins = strtol(cstr, &cstr, 10);
				if(isRASeparator(*cstr))
				{
					++cstr;
					secs = strtold(cstr, &cstr);
				} else throw std::runtime_error("Missing ':' after minutes");
			}
			else throw std::runtime_error("Missing 'h' or ':'");
			if(*cstr != 0)
				throw std::runtime_error("Could not parse RA (string contains more tokens than expected)");
			if(sign)
				return (hrs/24.0 - mins/(24.0*60.0) - secs/(24.0*60.0*60.0))*2.0*M_PIl;
			else
				return (hrs/24.0 + mins/(24.0*60.0) + secs/(24.0*60.0*60.0))*2.0*M_PIl;
		}
		
		static long double ParseDec(const std::string &str)
		{
			char *cstr;
			bool sign = false;
			for(size_t i=0; i!=str.size(); ++i)
			{
				if(str[i] == '-')
				{
					sign = true;
					break;
				} else if(str[i] != ' ')
				{
					sign = false;
					break;
				}
			}
			long double secs=0.0, mins=0.0,
				degs = strtol(str.c_str(), &cstr, 10);
			// Parse format '00d00m00.0s'
			if(*cstr == 'd')
			{
				++cstr;
				mins = strtol(cstr, &cstr, 10);
				if(*cstr == 'm')
				{
					++cstr;
					secs = strtold(cstr, &cstr);
					if(*cstr == 's')
						++cstr;
					else throw std::runtime_error("Missing 's'");
				} else throw std::runtime_error("Missing 'm'");
			}
			// Parse format '00.00.00.0'
			else if(isDecSeparator(*cstr))
			{
				++cstr;
				mins = strtol(cstr, &cstr, 10);
				if(isDecSeparator(*cstr))
				{
					++cstr;
					secs = strtold(cstr, &cstr);
				} else throw std::runtime_error("Missing '.' after minutes");
			}
			else throw std::runtime_error("Missing 'd' or '.' after degrees");
			if(*cstr != 0)
				throw std::runtime_error("Could not parse Dec (string contains more tokens than expected)");
			else if(sign)
				return (degs/360.0 - mins/(360.0*60.0) - secs/(360.0*60.0*60.0))*2.0*M_PIl;
			else
				return (degs/360.0 + mins/(360.0*60.0) + secs/(360.0*60.0*60.0))*2.0*M_PIl;
		}
		
		static std::string RAToString(long double ra)
		{
			const long double partsPerHour = 60.0L*60.0L*1000.0L;
			long double hrs = fmodl(ra * (12.0L / M_PIl), 24.0L);
			hrs = roundl(hrs * partsPerHour)/partsPerHour;
			std::stringstream s;
			if(hrs < 0) {
				hrs = -hrs;
				s << '-';
			}
			hrs = (roundl(hrs*partsPerHour)+0.5) / partsPerHour;
			int hrsInt = int(floorl(hrs)), minInt = int(floorl(fmodl(hrs,1.0L)*60.0L)),
				secInt = int(floorl(fmodl(hrs*60.0L,1.0L)*(60.0L))),
				subSecInt = int(floorl(fmodl(hrs*(60.0L*60.0L),1.0L)*(1000.0L)));
			s << (char) ((hrsInt/10)+'0') << (char) ((hrsInt%10)+'0') << 'h'
				<< (char) ((minInt/10)+'0') << (char) ((minInt%10)+'0') << 'm'
				<< (char) ((secInt/10)+'0') << (char) ((secInt%10)+'0');
			s << '.' << (char) (subSecInt/100+'0');
			if(subSecInt%100!=0)
			{
				s << (char) ((subSecInt%100)/10+'0');
				if(subSecInt%10!=0)
					s << (char) ((subSecInt%10)+'0');
			}
			s << 's';
			return s.str();
		}
		
		static std::string RAToString(long double ra, char delimiter)
		{
			const long double partsPerHour = 60.0L*60.0L*1000.0L;
			long double hrs = fmodl(ra * (12.0L / M_PIl), 24.0L);
			hrs = roundl(hrs * partsPerHour)/partsPerHour;
			std::stringstream s;
			if(hrs < 0) {
				hrs = -hrs;
				s << '-';
			}
			hrs = (roundl(hrs*partsPerHour)+0.5) / partsPerHour;
			int hrsInt = int(floorl(hrs)), minInt = int(floorl(fmodl(hrs,1.0L)*60.0L)),
				secInt = int(floorl(fmodl(hrs*60.0L,1.0L)*(60.0L))),
				subSecInt = int(floorl(fmodl(hrs*(60.0L*60.0L),1.0L)*(1000.0L)));
			s << (char) ((hrsInt/10)+'0') << (char) ((hrsInt%10)+'0') << delimiter
				<< (char) ((minInt/10)+'0') << (char) ((minInt%10)+'0') << delimiter
				<< (char) ((secInt/10)+'0') << (char) ((secInt%10)+'0');
			s << '.' << (char) (subSecInt/100+'0');
			if(subSecInt%100!=0)
			{
				s << (char) ((subSecInt%100)/10+'0');
				if(subSecInt%10!=0)
					s << (char) ((subSecInt%10)+'0');
			}
			return s.str();
		}
		
		static std::string RaDecToString(long double ra, long double dec)
		{
			return RAToString(ra) + ' ' + DecToString(dec);
		}
		
		static void RAToHMS(long double ra, int& hrs, int& min, double& sec)
		{
			const long double partsPerHour = 60.0L*60.0L*100.0L;
			long double hrsf = fmodl(ra * (12.0L / M_PIl), 24.0L);
			hrsf = roundl(hrsf * partsPerHour)/partsPerHour;
			bool negate = hrsf < 0;
			if(negate) {
				hrsf = -hrsf;
			}
			hrsf = (roundl(hrsf*partsPerHour)+0.5) / partsPerHour;
			if(negate)
				hrs = -int(floorl(hrsf));
			else
				hrs = int(floorl(hrsf));
			min = int(floorl(fmodl(hrsf,1.0L)*60.0L));
			sec = floorl(100.0L*fmodl(hrsf*60.0L,1.0L)*60.0L)/100.0L;
		}
		
		static std::string DecToString(long double dec)
		{
			const long double partsPerDeg = 60.0L*60.0L*1000.0L;
			long double deg = dec * (180.0L / M_PIl);
			deg = round(deg * partsPerDeg)/partsPerDeg;
			std::stringstream s;
			if(deg < 0) {
				deg = -deg;
				s << '-';
			}
			deg = (round(deg*partsPerDeg)+0.5L) / partsPerDeg;
			int
				degInt = int(floorl(deg)),
				minInt = int(floorl(fmodl(deg,1.0L)*60.0L)),
				secInt = int(floorl(fmodl(deg,1.0L/60.0L)*(60.0L*60.0L))),
				subSecInt = int(floorl(fmodl(deg,1.0L/60.0L/60.0L)*(60.0L*60.0L*1000.0L)));
			s << (char) ((degInt/10)+'0') << (char) ((degInt%10)+'0') << 'd'
				<< (char) ((minInt/10)+'0') << (char) ((minInt%10)+'0') << 'm'
				<< (char) ((secInt/10)+'0') << (char) ((secInt%10)+'0');
			if(subSecInt!=0)
			{
				s << '.' << (char) (subSecInt/100+'0');
				if(subSecInt%100!=0)
				{
					s << (char) ((subSecInt/10)%10+'0');
					if(subSecInt%10!=0)
						s << (char) (subSecInt%10+'0');
				}
			}
			s << 's';
			return s.str();
		}
		
		static std::string DecToString(long double dec, char delimiter)
		{
			const long double partsPerDeg = 60.0L*60.0L*1000.0L;
			long double deg = dec * (180.0L / M_PIl);
			deg = roundl(deg * partsPerDeg)/partsPerDeg;
			std::stringstream s;
			if(deg < 0) {
				deg = -deg;
				s << '-';
			}
			deg = (roundl(deg*partsPerDeg)+0.5L) / partsPerDeg;
			int
				degInt = int(floorl(deg)),
				minInt = int(floorl(fmodl(deg,1.0L)*60.0L)),
				secInt = int(floorl(fmodl(deg,1.0L/60.0L)*(60.0L*60.0L))),
				subSecInt = int(floorl(fmodl(deg,1.0L/60.0L/60.0L)*(60.0L*60.0L*1000.0L)));
			s << (char) ((degInt/10)+'0') << (char) ((degInt%10)+'0') << delimiter
				<< (char) ((minInt/10)+'0') << (char) ((minInt%10)+'0') << delimiter
				<< (char) ((secInt/10)+'0') << (char) ((secInt%10)+'0');
			if(subSecInt!=0)
			{
				s << '.' << (char) (subSecInt/100+'0');
				if(subSecInt%100!=0)
				{
					s << (char) ((subSecInt/10)%10+'0');
					if(subSecInt%10!=0)
						s << (char) (subSecInt%10+'0');
				}
			}
			return s.str();
		}
		
		static void DecToDMS(long double dec, int& deg, int& min, double& sec)
		{
			const long double partsPerDeg = 60.0L*60.0L*100.0L;
			long double degf = dec * (180.0 / M_PIl);
			degf = round(degf * partsPerDeg)/partsPerDeg;
			bool negate = degf < 0;
			if(negate) {
				degf = -degf;
			}
			degf = (round(degf*partsPerDeg)+0.5) / partsPerDeg;
			
			if(negate)
				deg = -int(floor(degf));
			else
				deg = int(floor(degf));
			min = int(floor(fmod(degf,1.0)*60.0)),
			sec = floor(100.*fmod(degf,1.0/60.0)*(60.0*60.0))/100.0;
		}
	private:
		RaDecCoord() { }
};

#endif