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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// Derivative of separation angle
///
/// Calculate the time derivative of the separation angle between
/// two input states, S1 and S2.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// S1 I State vector of the first body.
/// S2 I State vector of the second body.
///
/// The function returns the time derivative of the separation angle
/// between the two input states, S1 and S2.
/// ```
///
/// # Detailed Input
///
/// ```text
/// S1,
/// S2 are, respectively, the state vector of the first and
/// second target bodies as seen from the observer.
///
/// An implicit assumption exists that both states lie in the
/// same reference frame with the same observer for the same
/// epoch. If this is not the case, the numerical result has
/// no meaning.
/// ```
///
/// # Detailed Output
///
/// ```text
/// The function returns the double precision value of the time
/// derivative of the angular separation between S1 and S2.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If numeric overflow and underflow cases are detected, an error
/// is signaled by a routine in the call tree of this routine.
///
/// 2) If called in 'RETURN' mode, the function returns 0.
///
/// 3) Linear dependent position components of S1 and S1 constitutes
/// a non-error exception. The function returns 0 for this case.
/// ```
///
/// # Particulars
///
/// ```text
/// In this discussion, the notation
///
/// < V1, V2 >
///
/// indicates the dot product of vectors V1 and V2. The notation
///
/// V1 x V2
///
/// indicates the cross product of vectors V1 and V2.
///
/// To start out, note that we need consider only unit vectors,
/// since the angular separation of any two non-zero vectors
/// equals the angular separation of the corresponding unit vectors.
/// Call these vectors U1 and U2; let their velocities be V1 and V2.
///
/// For unit vectors having angular separation
///
/// THETA
///
/// the identity
///
/// || U1 x U1 || = ||U1|| * ||U2|| * sin(THETA) (1)
///
/// reduces to
///
/// || U1 x U2 || = sin(THETA) (2)
///
/// and the identity
///
/// | < U1, U2 > | = || U1 || * || U2 || * cos(THETA) (3)
///
/// reduces to
///
/// | < U1, U2 > | = cos(THETA) (4)
///
/// Since THETA is an angular separation, THETA is in the range
///
/// 0 : Pi
///
/// Then letting s be +1 if cos(THETA) > 0 and -1 if cos(THETA) < 0,
/// we have for any value of THETA other than 0 or Pi
///
///
/// 2 1/2
/// cos(THETA) = s * ( 1 - sin (THETA) ) (5)
///
/// or
///
/// 2 1/2
/// < U1, U2 > = s * ( 1 - sin (THETA) ) (6)
///
///
/// At this point, for any value of THETA other than 0 or Pi,
/// we can differentiate both sides with respect to time (T)
/// to obtain
///
/// 2 -1/2
/// < U1, V2 > + < V1, U2 > = s * (1/2)(1 - sin (THETA))
///
/// * (-2) sin(THETA)*cos(THETA)
///
/// * d(THETA)/dT (7a)
///
///
/// Using equation (5), and noting that s = 1/s, we can cancel
/// the cosine terms on the right hand side
///
/// -1
/// < U1, V2 > + < V1, U2 > = (1/2)(cos(THETA))
///
/// * (-2) sin(THETA)*cos(THETA)
///
/// * d(THETA)/dT (7b)
///
/// With (7b) reducing to
///
/// < U1, V2 > + < V1, U2 > = - sin(THETA) * d(THETA)/dT (8)
///
/// Using equation (2) and switching sides, we obtain
///
/// || U1 x U2 || * d(THETA)/dT = - < U1, V2 > - < V1, U2 > (9)
///
/// or, provided U1 and U2 are linearly independent,
///
/// d(THETA)/dT = ( - < U1, V2 > - < V1, U2 > ) / ||U1 x U2|| (10)
///
/// Note for times when U1 and U2 have angular separation 0 or Pi
/// radians, the derivative of angular separation with respect to
/// time doesn't exist. (Consider the graph of angular separation
/// with respect to time; typically the graph is roughly v-shaped at
/// the singular points.)
/// ```
///
/// # Examples
///
/// ```text
/// The numerical results shown for this example may differ across
/// platforms. The results depend on the SPICE kernels used as
/// input, the compiler and supporting libraries, and the machine
/// specific arithmetic implementation.
///
/// 1) Calculate the time derivative of the angular separation of
/// the Earth and Moon as seen from the Sun at a given time.
///
///
/// Use the meta-kernel shown below to load the required SPICE
/// kernels.
///
///
/// KPL/MK
///
/// File name: dvsep_ex1.tm
///
/// This meta-kernel is intended to support operation of SPICE
/// example programs. The kernels shown here should not be
/// assumed to contain adequate or correct versions of data
/// required by SPICE-based user applications.
///
/// In order for an application to use this meta-kernel, the
/// kernels referenced here must be present in the user's
/// current working directory.
///
/// The names and contents of the kernels referenced
/// by this meta-kernel are as follows:
///
/// File name Contents
/// --------- --------
/// de421.bsp Planetary ephemeris
/// pck00010.tpc Planet orientation and
/// radii
/// naif0012.tls Leapseconds
///
///
/// \begindata
///
/// KERNELS_TO_LOAD = ( 'de421.bsp',
/// 'pck00010.tpc',
/// 'naif0012.tls' )
///
/// \begintext
///
/// End of meta-kernel
///
///
/// Example code begins here.
///
///
/// PROGRAM DVSEP_EX1
/// IMPLICIT NONE
///
/// DOUBLE PRECISION ET
/// DOUBLE PRECISION LT
/// DOUBLE PRECISION DSEPT
/// DOUBLE PRECISION STATEE (6)
/// DOUBLE PRECISION STATEM (6)
///
/// INTEGER STRLEN
/// PARAMETER ( STRLEN = 64 )
///
/// CHARACTER*(STRLEN) BEGSTR
///
/// DOUBLE PRECISION DVSEP
///
/// C
/// C Load kernels.
/// C
/// CALL FURNSH ('dvsep_ex1.tm')
///
/// C
/// C An arbitrary time.
/// C
/// BEGSTR = 'JAN 1 2009'
/// CALL STR2ET( BEGSTR, ET )
///
/// C
/// C Calculate the state vectors Sun to Moon, and
/// C Sun to Earth at ET.
/// C
/// C
/// CALL SPKEZR ( 'EARTH', ET, 'J2000', 'NONE', 'SUN',
/// . STATEE, LT)
///
/// CALL SPKEZR ( 'MOON', ET, 'J2000', 'NONE', 'SUN',
/// . STATEM, LT)
///
/// C
/// C Calculate the time derivative of the angular separation
/// C of the Earth and Moon as seen from the Sun at ET.
/// C
/// DSEPT = DVSEP( STATEE, STATEM )
/// WRITE(*,*) 'Time derivative of angular'
/// WRITE(*,*) ' separation (rad/sec): ', DSEPT
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Time derivative of angular
/// separation (rad/sec): 3.8121193666132696E-009
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// E.D. Wright (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.0.1, 06-JUL-2021 (JDR)
///
/// Edited the header to comply with NAIF standard. Added problem
/// statement and meta-kernel to the example. Modified output to
/// comply with maximum line length of header comments.
///
/// - SPICELIB Version 2.0.0, 21-MAR-2014 (EDW)
///
/// Reimplemented algorithm using ZZDIV.
///
/// - SPICELIB Version 1.0.1, 15-MAR-2010 (EDW)
///
/// Trivial header format clean-up.
///
/// - SPICELIB Version 1.0.0, 31-MAR-2009 (EDW)
/// ```
pub fn dvsep(ctx: &mut SpiceContext, s1: &[f64; 6], s2: &[f64; 6]) -> crate::Result<f64> {
let ret = DVSEP(s1, s2, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(ret)
}
//$Procedure DVSEP ( Derivative of separation angle )
pub fn DVSEP(S1: &[f64], S2: &[f64], ctx: &mut Context) -> f2rust_std::Result<f64> {
let S1 = DummyArray::new(S1, 1..=6);
let S2 = DummyArray::new(S2, 1..=6);
let mut DVSEP: f64 = 0.0;
let mut DENOM: f64 = 0.0;
let mut NUMR: f64 = 0.0;
let mut PCROSS = StackArray::<f64, 3>::new(1..=3);
let mut U1 = StackArray::<f64, 6>::new(1..=6);
let mut U2 = StackArray::<f64, 6>::new(1..=6);
//
// SPICELIB functions
//
//
// Local variables
//
if RETURN(ctx) {
DVSEP = 0.0;
return Ok(DVSEP);
}
CHKIN(b"DVSEP", ctx)?;
//
// Compute the unit vectors and corresponding time derivatives
// for the input state vectors.
//
DVHAT(S1.as_slice(), U1.as_slice_mut());
DVHAT(S2.as_slice(), U2.as_slice_mut());
//
// Calculate the cross product vector of U1 and U2. As both vectors
// have magnitude one, the magnitude of the cross product equals
// sin(THETA), with THETA the angle between S1 and S2.
//
VCRSS(U1.subarray(1), U2.subarray(1), PCROSS.as_slice_mut());
//
// Handle the parallel and anti-parallel cases.
//
if VZERO(PCROSS.as_slice()) {
DVSEP = 0.0;
CHKOUT(b"DVSEP", ctx)?;
return Ok(DVSEP);
}
//
// Now calculate the time derivative of the angular separation
// between S1 and S2.
//
//
// Separately calculate the numerator and denominator.
//
NUMR = (VDOT(U1.subarray(1), U2.subarray(4)) + VDOT(U1.subarray(4), U2.subarray(1)));
DENOM = VNORM(PCROSS.as_slice());
//
// ZZDIV checks for over- or underflow. Finite precision
// arithmetic is a pain.
//
DVSEP = ZZDIV(-NUMR, DENOM, ctx)?;
//
// Return, the expectation exists that a FAILED() call
// follows the DVSEP call.
//
CHKOUT(b"DVSEP", ctx)?;
Ok(DVSEP)
}