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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// First derivative of a function, df(x)/dx
///
/// Calculate the first derivative of a caller-specified scalar
/// function using a three-point estimation.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// UDFUNC I The routine that computes the scalar value
/// of interest.
/// X I Independent variable of UDFUNC.
/// DX I Interval from X for derivative calculation.
/// DERIV O Approximate derivative of UDFUNC at X.
/// ```
///
/// # Detailed Input
///
/// ```text
/// UDFUNC is the routine that returns the value of the scalar
/// quantity function of interest at X. The calling
/// sequence for UDFUNC is:
///
/// CALL UDFUNC ( X, VALUE )
///
/// where:
///
/// X the double precision value of the
/// independent variable of the function
/// at which to determine the scalar value.
///
/// VALUE the double precision value returned by
/// UDFUNC at X.
///
/// Functionally:
///
/// VALUE = UDFUNC ( X )
///
/// X is a scalar double precision value at which to determine
/// the derivative of UDFUNC.
///
/// For many SPICE uses, X will represent ephemeris time,
/// expressed as seconds past J2000 TDB.
///
/// DX is a scalar double precision value representing half the
/// interval in units of X separating the evaluation
/// values of UDFUNC; the evaluations occur at (X + DX)
/// and (X - DX).
///
/// DX may be negative but must be non-zero.
/// ```
///
/// # Detailed Output
///
/// ```text
/// DERIV is the scalar double precision approximate value of the
/// first derivative of UDFUNC with respect to X.
///
/// Functionally:
///
/// d UDFUNC ( y ) |
/// DERIV = ---------------- |
/// dy | y=X
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If DX has a value of zero, an error is signaled by a routine
/// in the call tree of this routine.
/// ```
///
/// # Files
///
/// ```text
/// If the evaluation of UDFUNC requires SPICE kernel data, the
/// appropriate kernels must be loaded before calling this routine.
///
/// - SPK data: the calling application must load ephemeris data
/// for the targets, observer, and any intermediate objects in
/// a chain connecting the targets and observer for the time
/// used in the evaluation. If aberration corrections are
/// used, the states of target and observer relative to the
/// solar system barycenter must be calculable from the
/// available ephemeris data.
///
/// - If non-inertial reference frames are used, then PCK
/// files, frame kernels, C-kernels, and SCLK kernels may be
/// needed.
///
/// Such kernel data are normally loaded once per program run, NOT
/// every time this routine is called.
/// ```
///
/// # Particulars
///
/// ```text
/// This routine provides a simple interface to numerically calculate
/// the first derivative of a scalar quantity function, UDFUNC.
/// UDFUNC is expected to be "well behaved" across at the evaluation
/// interval [ X - DX, X + DX ]. This means a linear approximation to
/// the function over the interval is sufficiently accurate to
/// calculate the approximate derivative at X.
///
/// The routine QDERIV performs the differentiation using a three
/// point estimation. See the header of the SPICE routine QDERIV for
/// details of the discrete derivative computation performed by this
/// routine.
/// ```
///
/// # 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 light time corresponding
/// to the apparent position of Mercury relative to the Moon at
/// time "JAN 1 2009."
///
/// Use the meta-kernel shown below to load the required SPICE
/// kernels.
///
///
/// KPL/MK
///
/// File name: uddf_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
/// naif0009.tls Leapseconds
///
///
/// \begindata
///
/// KERNELS_TO_LOAD = ( 'de421.bsp',
/// 'naif0009.tls' )
///
/// \begintext
///
/// End of meta-kernel
///
///
/// Example code begins here.
///
///
/// PROGRAM UDDF_EX1
/// IMPLICIT NONE
///
/// EXTERNAL UDFUNC
///
/// DOUBLE PRECISION ET
/// DOUBLE PRECISION DT
/// DOUBLE PRECISION DERIV
///
/// C
/// C Load leapsecond and SPK kernels. The name of the
/// C meta kernel file shown here is fictitious; you
/// C must supply the name of a file available
/// C on your own computer system.
/// C
/// CALL FURNSH ( 'uddf_ex1.tm' )
///
/// C
/// C Use a shift of one second off the epoch of interest.
/// C
/// DT = 1.D0
///
/// C
/// C Convert the epoch date string to ephemeris seconds.
/// C
/// CALL STR2ET ( 'JAN 1 2009', ET )
///
/// C
/// C Calculate the derivative of UDFUNC at ET.
/// C
/// CALL UDDF ( UDFUNC, ET, DT, DERIV )
///
/// C
/// C Output the calculated derivative.
/// C
/// WRITE(*,*) DERIV
///
/// END
///
/// C
/// C A scalar quantity function that returns the light-time
/// C between the Moon and Mercury at ET.
/// C
/// SUBROUTINE UDFUNC ( ET, VALUE )
///
/// IMPLICIT NONE
///
/// DOUBLE PRECISION ET
/// DOUBLE PRECISION VALUE
///
/// DOUBLE PRECISION POS (3)
/// DOUBLE PRECISION LT
///
/// C
/// C Evaluate the apparent position of Mercury with respect
/// C to the Moon at ET.
/// C
/// CALL SPKPOS ( 'MERCURY', ET, 'J2000', 'LT+S', 'MOON',
/// . POS, LT )
///
/// C
/// C Return the light-time value as the scalar quantity.
/// C
/// VALUE = LT
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// -1.3567094055133566E-004
/// ```
///
/// # Restrictions
///
/// ```text
/// 1) The function UDFUNC must exist everywhere within
/// [X - DX, X + DX].
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// E.D. Wright (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.0.1, 05-JUL-2021 (JDR)
///
/// Edited the header to comply with NAIF standard. Included
/// required meta-kernel and added IMPICIT NONE statement to code
/// example.
///
/// Moved reference to QDERIV header from $Literature_References to
/// $Particulars with description of what it is expected from that
/// header.
///
/// - SPICELIB Version 1.0.0, 31-MAR-2010 (EDW) (NJB)
/// ```
pub fn uddf(
ctx: &mut SpiceContext,
udfunc: fn(&mut f64, &mut f64, &mut Context) -> f2rust_std::Result<()>,
x: f64,
dx: f64,
deriv: &mut f64,
) -> crate::Result<()> {
UDDF(udfunc, x, dx, deriv, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure UDDF ( First derivative of a function, df(x)/dx )
pub fn UDDF(
UDFUNC: fn(&mut f64, &mut f64, &mut Context) -> f2rust_std::Result<()>,
X: f64,
DX: f64,
DERIV: &mut f64,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let mut N: i32 = 0;
let mut DFDX = StackArray::<f64, 1>::new(1..=1);
let mut UDVAL = StackArray::<f64, 2>::new(1..=2);
//
// SPICELIB functions
//
//
// Local Variables
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"UDDF", ctx)?;
//
// Apply a three-point estimation of the derivative for
// UDFUNC at X by evaluating UDFUNC at [X-DX, X+DX].
//
// The QDERIV call returns a single value in the DFDX array.
//
N = 1;
//
// Evaluate the scalar function at the interval boundaries.
// Check for a FAILED event.
//
UDFUNC(&mut (X - DX).clone(), &mut UDVAL[1], ctx)?;
if FAILED(ctx) {
CHKOUT(b"UDDF", ctx)?;
return Ok(());
}
UDFUNC(&mut (X + DX).clone(), &mut UDVAL[2], ctx)?;
if FAILED(ctx) {
CHKOUT(b"UDDF", ctx)?;
return Ok(());
}
//
// Estimate the derivative at X.
//
QDERIV(
N,
UDVAL.subarray(1),
UDVAL.subarray(2),
DX,
DFDX.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"UDDF", ctx)?;
return Ok(());
}
*DERIV = DFDX[1];
CHKOUT(b"UDDF", ctx)?;
Ok(())
}