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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
const RECSYS: &[u8] = b"RECTANGULAR";
const LATSYS: &[u8] = b"LATITUDINAL";
const SPHSYS: &[u8] = b"SPHERICAL";
const RADSYS: &[u8] = b"RA/DEC";
const CYLSYS: &[u8] = b"CYLINDRICAL";
const GEOSYS: &[u8] = b"GEODETIC";
const PGRSYS: &[u8] = b"PLANETOGRAPHIC";
const XCRD: &[u8] = b"X";
const YCRD: &[u8] = b"Y";
const ZCRD: &[u8] = b"Z";
const RADCRD: &[u8] = b"RADIUS";
const LONCRD: &[u8] = b"LONGITUDE";
const LATCRD: &[u8] = b"LATITUDE";
const RACRD: &[u8] = b"RIGHT ASCENSION";
const DECCRD: &[u8] = b"DECLINATION";
const RNGCRD: &[u8] = b"RANGE";
const CLTCRD: &[u8] = b"COLATITUDE";
const ALTCRD: &[u8] = b"ALTITUDE";
const POSDEF: &[u8] = b"POSITION";
const SOBDEF: &[u8] = b"SUB-OBSERVER POINT";
const SINDEF: &[u8] = b"SURFACE INTERCEPT POINT";
const NWREL: i32 = 5;
const NWLONG: i32 = 7;
const EXWIDX: i32 = ((NWREL + NWLONG) + 1);
const MXBEGM: i32 = 55;
const MXENDM: i32 = 13;
const MXMSG: i32 = ((MXBEGM + MXENDM) + 10);
const UPIDX: i32 = 1;
const ESTIDX: i32 = 2;
const NORIDX: i32 = 3;
//$Procedure ZZGFCPRX ( GF, coordinate derivative proxy )
pub fn ZZGFCPRX(
STATE: &[f64],
CORSYS: &[u8],
RE: f64,
F: f64,
SENSE: i32,
CDSIGN: &mut [i32],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let STATE = DummyArray::new(STATE, 1..=6);
let mut CDSIGN = DummyArrayMut::new(CDSIGN, 1..=3);
let mut ALT: f64 = 0.0;
let mut DP: f64 = 0.0;
let mut LAT: f64 = 0.0;
let mut LON: f64 = 0.0;
let mut NORMAL = StackArray::<f64, 3>::new(1..=3);
let mut RTNVEL = StackArray::<f64, 3>::new(1..=3);
let mut VEL = StackArray::<f64, 3>::new(1..=3);
let mut XMAT = StackArray2D::<f64, 9>::new(1..=3, 1..=3);
let mut DPSIGN: i32 = 0;
let mut RTNSGN = StackArray::<i32, 3>::new(1..=3);
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Local variables
//
// Internally, we're going to use the more
// descriptive names EAST for the "tangential"
// direction and NORTH for the "normal" direction.
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"ZZGFCPRX", ctx)?;
//
// For planetographic coordinates, check the longitude sense.
//
if fstr::eq(CORSYS, PGRSYS) {
if ((SENSE != 1) && (SENSE != -1)) {
SETMSG(b"Longitude sense # should be 1 or -1.", ctx);
ERRINT(b"#", SENSE, ctx);
SIGERR(b"SPICE(VALUEOUTOFRANGE)", ctx)?;
CHKOUT(b"ZZGFCPRX", ctx)?;
return Ok(());
}
}
//
// If we have a zero velocity vector, just indicate that each
// velocity coordinate isn't changing and return now. If the
// velocity vector is non-zero, convert it to a unit vector; this
// guarantees that overflow can't occur.
if VZERO(STATE.subarray(4)) {
//
// The velocity is zero. Indicate that the coordinates are
// not changing and return. Returning now simplifies the
// logic of the rest of the routine, since the case of
// zero-velocity can be ignored.
//
CLEARI(3, CDSIGN.as_slice_mut());
CHKOUT(b"ZZGFCPRX", ctx)?;
return Ok(());
} else {
VHAT(STATE.subarray(4), VEL.as_slice_mut());
}
//
// The rectangular case is trivial; handle it now.
//
if fstr::eq(CORSYS, RECSYS) {
//
// The output system is rectangular. Just indicate the
// signs of the input velocity.
//
for I in 1..=3 {
if (VEL[I] == 0.0) {
CDSIGN[I] = 0;
} else {
//
// Use the Fortran sign transfer intrinsic function
// to set CDSIGN(I) to 1 or -1, depending
// on whether the corresponding velocity component
// is positive or negative. See reference [1] for a
// discussion of this Fortran intrinsic function.
//
CDSIGN[I] = intrinsics::IDNINT(f64::copysign(1.0, VEL[I]));
}
}
//
// All done.
//
CHKOUT(b"ZZGFCPRX", ctx)?;
return Ok(());
}
//
// There's quite a bit of common logic for the "on Z-axis" case;
// take care of it here.
//
if ((STATE[1] == 0.0) && (STATE[2] == 0.0)) {
//
// The position lies on the Z-axis.
//
// For all of the coordinate systems having a longitude
// coordinate (this includes right ascension), the derivative of
// longitude with respect to time is undefined; we set the sign
// of the derivative to zero.
//
// For all of the coordinate systems having a latitude coordinate
// (this includes declination), if the position is not at the
// origin, the derivative of latitude with respect to time is
// undefined unless the input velocity is zero. At the origin,
// the derivative of latitude with respect to time doesn't exist.
// In both cases, we set the sign of the velocity components
// to zero.
//
// For the coordinate systems that have a radius or range
// coordinate, where distance is measured from the origin, when
// the input position is not at the origin, distance is
// increasing, constant, or decreasing depending on whether the
// dot product of velocity and the position's Z-coordinate is
// positive, zero, or negative, respectively. This dot product
// test is valid for the derivative of altitude as well (we
// assert this without proof for the case of positions inside
// prolate spheroids).
//
// If the position is at the origin, then since range and
// altitude are not differentiable, their signs are set to
// zero.
//
// Cylindrical coordinates are a special case which we treat
// separately.
//
if (STATE[3] != 0.0) {
//
// The position is on the Z-axis but not at the origin.
//
// Compute the dot product used for the range/altitude
// derivative.
//
DP = VDOT(STATE.as_slice(), VEL.as_slice());
if (DP == 0.0) {
DPSIGN = 0;
} else {
DPSIGN = intrinsics::IDNINT(f64::copysign(1.0, DP));
}
} else {
//
// The position is at the origin. We know the velocity
// is non-zero, and any movement increases radius or
// altitude. However, neither radius nor altitude are
// differentiable here, so we indicate no sign.
//
DPSIGN = 0;
}
//
// Set the coordinate derivative signs for all but the
// rectangular system, which was handled already, and
// the cylindrical system.
//
//
// Recall the coordinate systems and their coordinate orders:
//
// System Coordinates
// ---------- -----------
// Rectangular X, Y, Z
// Latitudinal Radius, Longitude, Latitude
// Spherical Radius, Colatitude, Longitude
// RA/Dec Range, Right Ascension, Declination
// Cylindrical Radius, Longitude, Z
// Geodetic Longitude, Latitude, Altitude
// Planetographic Longitude, Latitude, Altitude
//
//
if fstr::eq(CORSYS, LATSYS) {
//
// The radial derivative sign was computed; the
// other derivative signs are set to zero.
//
CDSIGN[1] = DPSIGN;
CDSIGN[2] = 0;
CDSIGN[3] = 0;
} else if fstr::eq(CORSYS, SPHSYS) {
//
// The radial derivative sign was computed; the
// longitude derivative signs is set to zero.
//
CDSIGN[1] = DPSIGN;
CDSIGN[3] = 0;
//
// Co-latitude is a special case. Co-latitude is
// not differentiable with respect to Cartesian
// position for positions on the Z-axis, since
// co-latitude is a v-shaped function of distance
// from the Z-axis. We simply set the sign
// of the co-latitude derivative to zero in this
// case.
//
CDSIGN[2] = 0;
} else if fstr::eq(CORSYS, RADSYS) {
//
// RA/Dec derivatives are assigned in the same manner
// as latitudinal ones.
//
CDSIGN[1] = DPSIGN;
CDSIGN[2] = 0;
CDSIGN[3] = 0;
} else if fstr::eq(CORSYS, GEOSYS) {
//
// Altitude plays the role of radius for this
// system.
//
CDSIGN[1] = 0;
CDSIGN[2] = 0;
CDSIGN[3] = DPSIGN;
} else if fstr::eq(CORSYS, PGRSYS) {
//
// Altitude plays the role of radius for this
// system.
//
CDSIGN[1] = 0;
CDSIGN[2] = 0;
CDSIGN[3] = DPSIGN;
} else if fstr::eq(CORSYS, CYLSYS) {
CDSIGN[1] = 0;
CDSIGN[2] = 0;
//
// For cylindrical coordinates, the derivative of Z with
// respect to time is already present in VEL.
//
if (VEL[3] == 0.0) {
CDSIGN[3] = 0;
} else {
CDSIGN[3] = intrinsics::IDNINT(f64::copysign(1.0, VEL[3]));
}
} else {
//
// If we end up here, we have an invalid coordinate system.
//
SETMSG(b"Coordinate system # is not supported. Verify that the coordinate system specifier matches a value from zzgf.inc.", ctx);
ERRCH(b"#", CORSYS, ctx);
SIGERR(b"SPICE(NOTSUPPORTED)", ctx)?;
CHKOUT(b"ZZGFCPRX", ctx)?;
return Ok(());
}
//
// We've handled the on-Z-axis cases. Return now.
//
CHKOUT(b"ZZGFCPRX", ctx)?;
return Ok(());
}
//
// This is the normal case: the position is not on the Z-axis.
//
// The type of MRTN frame we use depends on the coordinate system.
// Planetodetic and planetographic coordinate systems are a special
// case.
//
if (fstr::eq(CORSYS, GEOSYS) || fstr::eq(CORSYS, PGRSYS)) {
//
// Instead of defining the MRTN frame using the input
// position vector, we define it using an outward normal vector
// on the reference ellipsoid at the geodetic latitude
// and longitude of the input position.
//
RECGEO(STATE.as_slice(), RE, F, &mut LON, &mut LAT, &mut ALT, ctx)?;
if FAILED(ctx) {
CHKOUT(b"ZZGFCPRX", ctx)?;
return Ok(());
}
LATREC(1.0, LON, LAT, NORMAL.as_slice_mut());
} else if fstr::eq(CORSYS, CYLSYS) {
//
// The normal vector is aligned with the local radial
// direction; this vector is parallel to the X-Y plane.
//
VPACK(STATE[1], STATE[2], 0.0, NORMAL.as_slice_mut());
VHATIP(NORMAL.as_slice_mut());
} else {
//
// The position vector provides the normal direction.
//
VHAT(STATE.as_slice(), NORMAL.as_slice_mut());
}
// Obtain the matrix required to transform the velocity to the MRTN
// frame; transform the velocity.
//
ZZRTNMAT(NORMAL.as_slice(), XMAT.as_slice_mut(), ctx)?;
MXV(XMAT.as_slice(), VEL.as_slice(), RTNVEL.as_slice_mut());
//
// We can think of the basis vectors of the MRTN frame as local "up",
// "East," "North" directions. Compute the signs of the up, East,
// and North velocity components.
//
for I in 1..=3 {
if (RTNVEL[I] == 0.0) {
RTNSGN[I] = 0;
} else {
RTNSGN[I] = intrinsics::IDNINT(f64::copysign(1.0, RTNVEL[I]));
}
}
//
// Set the signs of the coordinate derivatives from the MRTN
// derivative signs.
//
//
// Recall the coordinate systems and their coordinate orders:
//
// System Coordinates
// ---------- -----------
// Rectangular X, Y, Z
// Latitudinal Radius, Longitude, Latitude
// Spherical Radius, Colatitude, Longitude
// RA/Dec Range, Right Ascension, Declination
// Cylindrical Radius, Longitude, Z
// Geodetic Longitude, Latitude, Altitude
// Planetographic Longitude, Latitude, Altitude
//
//
if fstr::eq(CORSYS, LATSYS) {
CDSIGN[1] = RTNSGN[UPIDX];
CDSIGN[2] = RTNSGN[ESTIDX];
CDSIGN[3] = RTNSGN[NORIDX];
} else if fstr::eq(CORSYS, SPHSYS) {
//
// For spherical coordinate systems, the sign of the
// derivative of co-latitude is the negative of the
// sign of the North derivative.
//
CDSIGN[1] = RTNSGN[UPIDX];
CDSIGN[2] = -RTNSGN[NORIDX];
CDSIGN[3] = RTNSGN[ESTIDX];
} else if fstr::eq(CORSYS, RADSYS) {
CDSIGN[1] = RTNSGN[UPIDX];
CDSIGN[2] = RTNSGN[ESTIDX];
CDSIGN[3] = RTNSGN[NORIDX];
} else if fstr::eq(CORSYS, GEOSYS) {
CDSIGN[1] = RTNSGN[ESTIDX];
CDSIGN[2] = RTNSGN[NORIDX];
CDSIGN[3] = RTNSGN[UPIDX];
} else if fstr::eq(CORSYS, PGRSYS) {
//
// For planetographic coordinates, altitude and latitude
// behave identically to their geodetic counterparts. We
// need to adjust the sign of the longitude derivative
// according to whether longitude is positive East or West.
//
CDSIGN[1] = (RTNSGN[ESTIDX] * SENSE);
CDSIGN[2] = RTNSGN[NORIDX];
CDSIGN[3] = RTNSGN[UPIDX];
} else if fstr::eq(CORSYS, CYLSYS) {
CDSIGN[1] = RTNSGN[UPIDX];
CDSIGN[2] = RTNSGN[ESTIDX];
CDSIGN[3] = RTNSGN[NORIDX];
} else {
//
// If we end up here, we have an invalid coordinate system.
//
SETMSG(b"Coordinate system # is not supported. Verify that the coordinate system specifier matches a value from zzgf.inc.", ctx);
ERRCH(b"#", CORSYS, ctx);
SIGERR(b"SPICE(NOTSUPPORTED)", ctx)?;
CHKOUT(b"ZZGFCPRX", ctx)?;
return Ok(());
}
CHKOUT(b"ZZGFCPRX", ctx)?;
Ok(())
}