summaryrefslogtreecommitdiffstats
path: root/ds9/library/convert.tcl
blob: 5bf61fae5be62d88b3125f2dc72aacd5969c3070 (plain)
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
#  Copyright (C) 1999-2017
#  Smithsonian Astrophysical Observatory, Cambridge, MA, USA
#  For conditions of distribution and use, see copyright notice in "copyright"

package provide DS9 1.0

set M_PI 3.14159265358979323846

# hours to degrees
proc h2d {h} {
    return [expr ($h + 0.0) * 15.0]
}

# degrees to hours
proc d2h {d} {
    return [expr ($d + 0.0) / 15.0]
}

# degrees to radians -- returns 0 <= r < PI
proc d2r {d} {
    global M_PI
    while {[::math::fuzzy::tge $d 360.0]} {
	set d [expr $d - 360.0]
    }
    return [expr ($d + 0.0) * ($M_PI / 360.0)]
}

# radians to degrees -- returns 0 <= d < PI
proc r2d {r} {
    global M_PI
    while {[::math::fuzzy::tge $r $M_PI]} {
	set r [expr $r - $M_PI]
    }
    return [expr ($r + 0.0) * (360.0 / $M_PI)]
}

#
#    strtod -- convert string to double
#
#    Supports sexagesimal values:
#    12:30:45.6 12h30m45.6s 12d30m45.6s "12 30 45.6"

#    Supports 12.5d (degrees) 12.5r (radians)
#
#    A hidden global _strtod returns the number of arguments in the input.
#    if this value is 3 or 4, then hms or dms was input. This can be used
#    to determine that hms was input for ra so that you can convert hours
#    to degrees.
#    
#
set _strtod 0
proc strtod {d} {
    global _strtod

    set d [string trim $d]
    set d [string trimleft $d 0]
    if { $d == {} } {
	set d 0
    }

    if { [string first - $d] >= 0 } {
	set sign "-"
	regsub -all -- "-" $d  {} d
    } elseif { [string first + $d] >= 0 } {
	set sign {}
	regsub -all -- {\+} $d  {} d
    } else {
	set sign {}
    }

    regsub -all {[ \t]*[hms][ \t]*} $d   ":" d
    set arglist [split $d ": "]

    set _strtod 0
    foreach arg $arglist {
	set args($_strtod) [string trimleft $arg 0]
	if { $args($_strtod) == {} } {
	    set args($_strtod) 0
	}
	incr _strtod
    }

    switch $_strtod {
	2 {
	    error "ERROR: strtod h:m:s|d:m:s|d"
	}
	3 {
	    set d [expr double($args(0)) + 0.0];
	    set m [expr double($args(1)) + 0.0];
	    set s [expr double($args(2)) + 0.0];
	}
	4 {
	    set d [expr double($args(0)) + 0.0];
	    set m [expr double($args(1)) + 0.0];
	    set s [expr double($args(2)) + 0.0];
	}
	default {
            set c [string range $d end end]
            if { $c == "r" } {
		set d [string trimright $d r]
		set d [r2d $d]
            } elseif { $c == "d" } {
		set d [string trimright $d d]
	    }
	    set m 0
	    set s 0
	}
    }
    set val [expr  $d + ($m / 60.0) + ($s / 3600.0)]
    # we don't want this. it rounds off to a precision of 6, which can
    # cause problems with h:m:s to degree convertions
    #   set val [format "%s%g" $sign $val]
    set val "$sign$val"
    return $val
}

#
# _uformat -- primative unit format converter to convert a float to a string
# output format can be:
#   # or : (output in sexagesimal) or d (output in decimal)
#
proc _uformat {oformat value} {

    if { $value < 0.0 } {
	set sign "-"
	set d [expr -$value]
    } else {
	set sign {}
	set d $value
    }
    switch $oformat {
	{#} { 
	    set m [expr ($d - (int($d))) * 60]
	    if { $m < 0 } {
		set m 0.0
	    }
	    set s [expr ($m - (int($m))) * 60]
	    if { $s < 0 } {
		set s 0.0
	    }
	    return [format "%s%d %d %.3f" \
			$sign [expr int($d)] [expr int($m)] $s]
	}
	:   {
	    set m [expr ($d - (int($d))) * 60]
	    if { $m < 0 } {
		set m 0.0
	    }
	    set s [expr ($m - (int($m))) * 60]
	    if { $s < 0 } {
		set s 0.0
	    }
	    return [format "%s%d:%d:%.3f" \
			$sign [expr int($d)] [expr int($m)] $s]
	}
	d  {
	    return [format "%s%f" $sign $d]
	}
    }
}

#
# uformat -- unit format converter
#
# uformat input_format output_format value
#
# where input format can be:
#   h (hours) d (degrees) m (minutes) s (seconds)
# and output format can be the same, with a suffix of:
#   # or : (output in sexagesimal) or d (output in decimal)
#
proc uformat {iformat oformat value} {
    set itype [string index $iformat 0]
    set otype [string index $oformat 0]
    set oform [string index $oformat 1]
    if { $oform == {} } {
	set oform d
    }
    set value [strtod $value]

    switch $itype {
	h {
	    switch $otype {
		h   {return [_uformat $oform $value]}
		d   {return [_uformat $oform [h2d $value]]}
		m   {return [_uformat $oform [h2d $value]*60]}
		s   {return [_uformat $oform [h2d $value]*60*60]}
	    }
	}
	d {
	    switch $otype {
		h   {return [_uformat $oform [d2h $value]]}
		d   {return [_uformat $oform $value]}
		m   {return [_uformat $oform [expr $value*60]]}
		s   {return [_uformat $oform [expr $value*60*60]]}
	    }
	}
	m {
	    switch $otype {
		h   {return [_uformat $oform [d2h $value/60]]}
		d   {return [_uformat $oform [expr $value/60]]}
		m   {return [_uformat $oform $value]}
		s   {return [_uformat $oform [expr $value*60]]}
	    }
	}
	s {
	    switch $otype {
		h   {return [_uformat $oform [h2d $value/60/60]]}
		d   {return [_uformat $oform [expr $value/60/60]]}
		m   {return [_uformat $oform [expr $value/60]]}
		s   {return [_uformat $oform $value]}
	    }
	}
    }
}