-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstroConvert.cs
More file actions
132 lines (111 loc) · 4.38 KB
/
AstroConvert.cs
File metadata and controls
132 lines (111 loc) · 4.38 KB
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OccultWatcher.Guide
{
public static class AstroConvert
{
/// <summary>
/// Converts a right ascension string in formats HH:MM:SS.S; HH:MM:SS; HH:MM.M; HH.H; HH MM SS.S; HH MM SS; HH MM.M; HH.H to a double number
/// </summary>
/// <param name="hhmmss"></param>
/// <returns></returns>
public static double ToRightAcsension(string value)
{
return ToDoubleValue(value, false);
}
/// <summary>
/// Converts a declination string in formats +DD:MM:SS.S; +DD:MM:SS; +DD:MM.M; +DD.D; +DD MM SS.S; DD MM SS; DD MM.M; DD.D to a double number
/// </summary>
/// <param name="hhmmss"></param>
/// <returns></returns>
public static double ToDeclination(string value)
{
return ToDoubleValue(value, true);
}
private static double ToDoubleValue(string value, bool allowSign)
{
if (value == null) throw new ArgumentNullException("value");
if (value == string.Empty) throw new ArgumentException("'value' cannot be blank");
value = value.Trim();
char groupDelimiter = ':';
if (value.IndexOf(' ') > -1) groupDelimiter = ' ';
int sign = 1;
if (allowSign)
{
if (value[0] == '-') sign = -1;
if ("-+".IndexOf(value[0]) > -1)
{
value = value.Substring(1).Trim();
}
}
string[] tokens = value.Split(new char[] { groupDelimiter }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length > 3) throw new FormatException("");
double retval = double.NaN;
if (tokens.Length == 3)
{
retval = double.Parse(tokens[2], CultureInfo.InvariantCulture) / 3600.0;
retval += (int.Parse(tokens[1], CultureInfo.InvariantCulture) / 60.0);
retval += int.Parse(tokens[0], CultureInfo.InvariantCulture);
}
else if (tokens.Length == 2)
{
retval = (double.Parse(tokens[1], CultureInfo.InvariantCulture) / 60.0);
retval += int.Parse(tokens[0], CultureInfo.InvariantCulture);
}
else if (tokens.Length == 1)
{
retval = double.Parse(tokens[0], CultureInfo.InvariantCulture);
}
return retval * sign;
}
/// <summary>
/// Converts a double value into a string representation using the specified format. The following
/// formats are supported:
///
/// + - adds a plus sign if the value is positive
/// HH or DD - whole part (degrees or hours)
/// MM - minutes; SS - seconds; T and TT - parts of a second
///
/// </summary>
/// <param name="value"></param>
/// <param name="format"></param>
/// <returns></returns>
public static string ToStringValue(double value, string format)
{
int sign = Math.Sign(value);
string signs = sign > 0 ? "+" : "-";
value = Math.Abs(value);
int dd = (int)value;
double mmd = (value - dd) * 60.0;
int mm = (int)mmd;
double ssd = (mmd - mm) * 60.0;
int ss = (int)ssd;
int ssr = (int)Math.Round(ssd);
int t = (int)((ssd - ss) * 10);
int tt = (int)((ssd - ss) * 100);
if (format.IndexOf("+") > -1)
format = format.Replace("+", signs);
else
dd = dd * sign;
format = format.Replace("DD", dd.ToString("00"));
format = format.Replace("HH", dd.ToString("00"));
format = format.Replace("MM", mm.ToString("00"));
if (format.IndexOf("TT") > -1 ||
format.IndexOf("T") > -1)
{
format = format.Replace("SS", ss.ToString("00"));
format = format.Replace("TT", tt.ToString("00"));
format = format.Replace("T", t.ToString("0"));
}
else
{
format = format.Replace("SS", ssr.ToString("00"));
}
return format;
}
}
}