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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const NAMLEN: i32 = 1000;
const SUMLEN: i32 = 128;
/// DAF, Re-order arrays
///
/// Reorder the arrays in a DAF according to a given order vector.
///
/// # Required Reading
///
/// * [DAF](crate::required_reading::daf)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// HANDLE I Handle of DAF.
/// IORDER I Order vector.
/// N I Dimension of IORDER.
/// ```
///
/// # Detailed Input
///
/// ```text
/// HANDLE is the handle of a DAF that has been opened for
/// write access. Use DAFOPW, for example, to open
/// an existing file and get its handle.
///
/// IORDER is the order vector to be used to re-order the
/// arrays stored in the DAF specified by HANDLE.
///
/// An integer order vector is an array of length
/// N whose elements are the integers 1 through N.
///
/// The first element of IORDER is the index of the
/// first array in the re-ordered file, and so on.
///
/// N is the number of elements in the order vector.
/// This may be less than the number of arrays in
/// the file.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If IORDER is not an order vector (that is, if it does
/// not contain every integer between 1 and N), the error
/// SPICE(DISORDER) is signaled.
///
/// 2) If N is greater than the number of arrays in the file,
/// the error SPICE(DISARRAY) is signaled.
/// ```
///
/// # Files
///
/// ```text
/// See argument HANDLE.
/// ```
///
/// # Particulars
///
/// ```text
/// DAFRA does not actually move the elements of the double
/// precision arrays; it works by rearranging the contents
/// of the summary and name records in the file. The result
/// is that the search routines (BFS, FNA, BBS, FPA) will
/// return the arrays in the indicated order.
///
/// After re-ordering, array IORDER(1) of the input file is the
/// first array of the output file, array IORDER(2) of the input
/// file is the second array of the output file, and so on.
///
/// The order vector used by DAFRA is typically created for
/// a related array by one of the ORDER routines, as shown in
/// the example below.
/// ```
///
/// # Examples
///
/// ```text
/// The following code fragment sorts the arrays in a DAF by name.
///
/// C
/// C Collect the names of the arrays in the file.
/// C
/// CALL DAFOPW ( FILE, HANDLE )
///
/// N = 0
/// CALL DAFBFS ( HANDLE )
/// CALL DAFFNA ( FOUND )
///
/// DO WHILE ( FOUND )
/// N = N + 1
/// CALL DAFGN ( NAMES(I) )
/// CALL DAFFNA ( FOUND )
/// END DO
///
/// C
/// C Sort the names.
/// C
/// CALL ORDERC ( NAMES, N, IORDER )
///
/// C
/// C Re-order the arrays.
/// C
/// CALL DARFA ( HANDLE, IORDER, N )
/// CALL DAFCLS ( HANDLE )
///
/// Afterward, a forward search like the one shown below
///
/// CALL DAFBFS ( HANDLE )
/// CALL DAFFNA ( FOUND )
///
/// DO WHILE ( FOUND )
/// CALL DAFGN ( NAME )
/// WRITE (*,*) NAME
///
/// CALL DAFFNA ( FOUND )
/// END DO
///
/// produces an ordered list of the names in the sorted file.
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 27-AUG-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.0.1, 10-MAR-1992 (WLT)
///
/// Comment section for permuted index source lines was added
/// following the header.
///
/// - SPICELIB Version 1.0.0, 28-MAR-1991 (IMU)
/// ```
pub fn dafra(ctx: &mut SpiceContext, handle: i32, iorder: &mut [i32], n: i32) -> crate::Result<()> {
DAFRA(handle, iorder, n, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure DAFRA ( DAF, Re-order arrays )
pub fn DAFRA(HANDLE: i32, IORDER: &mut [i32], N: i32, ctx: &mut Context) -> f2rust_std::Result<()> {
let mut IORDER = DummyArrayMut::new(IORDER, 1..);
let mut HOLDN = [b' '; NAMLEN as usize];
let mut TEMPN = [b' '; NAMLEN as usize];
let mut HOLDS = StackArray::<f64, 128>::new(1..=SUMLEN);
let mut TEMPS = StackArray::<f64, 128>::new(1..=SUMLEN);
let mut HOLD: i32 = 0;
let mut INDEX: i32 = 0;
let mut START: i32 = 0;
let mut TOTAL: i32 = 0;
let mut FOUND: bool = false;
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"DAFRA", ctx)?;
}
//
// If the order vector has fewer than two elements, don't bother.
//
if (N < 2) {
CHKOUT(b"DAFRA", ctx)?;
return Ok(());
}
//
// If IORDER is not an order vector, complain.
//
if !ISORDV(IORDER.as_slice_mut(), N) {
SETMSG(b"Sorry, IORDER is not an order vector.", ctx);
SIGERR(b"SPICE(DISORDER)", ctx)?;
CHKOUT(b"DAFRA", ctx)?;
return Ok(());
}
//
// If the number of arrays to be moved exceeds the number of
// arrays in the file, complain.
//
TOTAL = 0;
DAFBFS(HANDLE, ctx)?;
DAFFNA(&mut FOUND, ctx)?;
while (FOUND && !FAILED(ctx)) {
TOTAL = (TOTAL + 1);
DAFFNA(&mut FOUND, ctx)?;
}
if FAILED(ctx) {
CHKOUT(b"DAFRA", ctx)?;
return Ok(());
} else if (TOTAL < N) {
SETMSG(b"N (#) exceeds number of arrays (#).", ctx);
ERRINT(b"#", N, ctx);
ERRINT(b"#", TOTAL, ctx);
SIGERR(b"SPICE(DISARRAY)", ctx)?;
CHKOUT(b"DAFRA", ctx)?;
return Ok(());
}
//
// Not surprisingly, this routine is patterned closely after the
// (original) REORDx routines in SPICELIB. The only differences
// are that
//
// 1) This routine is not error free---it checks to make
// sure that IORDER is in fact an order vector, and that
// every element in IORDER refers to an existing array.
//
// 2) Instead of moving elements of an array in and out of
// a temporary location, it moves summaries and names.
// This means that two sets of temporary storage locations
// are needed: one to hold the summary and name of the
// guy who began the current cycle; and one to hold the guy
// being moved from location HOLD to location INDEX.
//
START = 1;
while ((START < N) && !FAILED(ctx)) {
//
// Start the cycle. One guy (pair of summary and name record)
// has to sit out (in HOLDS and HOLDN) until the end of the cycle
// is reached.
//
INDEX = START;
HOLD = IORDER[INDEX];
DAFBFS(HANDLE, ctx)?;
for I in 1..=INDEX {
DAFFNA(&mut FOUND, ctx)?;
}
DAFGS(HOLDS.as_slice_mut(), ctx)?;
DAFGN(&mut HOLDN, ctx)?;
//
// Move guys from HOLD to INDEX; then update HOLD (to point
// to the next guy to be moved) and INDEX (to point at the
// space just vacated).
//
// Keep going until HOLD points to the first guy moved during
// the current cycle. This ends the cycle.
//
while (HOLD != START) {
//
// Get the guy in position HOLD.
//
DAFBFS(HANDLE, ctx)?;
for I in 1..=HOLD {
DAFFNA(&mut FOUND, ctx)?;
}
DAFGS(TEMPS.as_slice_mut(), ctx)?;
DAFGN(&mut TEMPN, ctx)?;
//
// Move him to position INDEX. (Note that DAFWS is used to
// update the summary instead of DAFRS, because the addresses
// are actually being changed.)
//
DAFBFS(HANDLE, ctx)?;
for I in 1..=INDEX {
DAFFNA(&mut FOUND, ctx)?;
}
DAFWS(TEMPS.as_slice(), ctx)?;
DAFRN(&TEMPN, ctx)?;
//
// Update HOLD and INDEX.
//
INDEX = HOLD;
HOLD = IORDER[HOLD];
IORDER[INDEX] = -IORDER[INDEX];
}
//
// The last element in the cycle is restored from TEMP.
//
DAFBFS(HANDLE, ctx)?;
for I in 1..=INDEX {
DAFFNA(&mut FOUND, ctx)?;
}
DAFWS(HOLDS.as_slice(), ctx)?;
DAFRN(&HOLDN, ctx)?;
IORDER[HOLD] = -IORDER[HOLD];
//
// Begin the next cycle at the next element in the order
// vector with a positive sign. (That is, the next one
// that hasn't been moved.)
//
while ((IORDER[START] < 0) && (START < N)) {
START = (START + 1);
}
}
//
// Restore the original signs of the elements of the order
// vector, for the next go around.
//
{
let m1__: i32 = 1;
let m2__: i32 = N;
let m3__: i32 = 1;
INDEX = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
IORDER[INDEX] = i32::abs(IORDER[INDEX]);
INDEX += m3__;
}
}
CHKOUT(b"DAFRA", ctx)?;
Ok(())
}