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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
pub const LBCELL: i32 = -5;
const BLANK: &[u8; 1] = &fstr::extend_const::<1>(b" ");
const LPAREN: &[u8; 1] = &fstr::extend_const::<1>(b"(");
const RPAREN: &[u8; 3] = &fstr::extend_const::<3>(b" )");
const COMMA: &[u8; 2] = &fstr::extend_const::<2>(b",");
/// Write a variable to a kernel file
///
/// Write the value of a variable in a double precision symbol
/// table to a NAIF ASCII kernel file.
///
/// # Required Reading
///
/// * [KERNEL](crate::required_reading::kernel)
/// * [SYMBOLS](crate::required_reading::symbols)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// UNIT I Output logical unit.
/// NAME I Name of the variable.
/// DIRCTV I Kernel directive: '=' or '+='.
/// TABSYM,
/// TABPTR,
/// TABVAL I Double precision symbol table.
/// ```
///
/// # Detailed Input
///
/// ```text
/// UNIT is the logical unit to which the variable will be
/// written. This is usually the logical unit to which the
/// output kernel file is connected.
///
/// NAME is the name of the variable to be written to UNIT.
///
/// DIRCTV is the directive linking NAME and its associated values
/// in the kernel file. This may be any of the directives
/// recognized by RDKVAR.
///
/// TABSYM,
/// TABPTR,
/// TABVAL are the components of a double precision symbol table.
///
/// On input, the table may or may not contain any variables.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If an error occurs writing the variable to UNIT, the error
/// SPICE(WRITEERROR) is signaled.
/// ```
///
/// # Files
///
/// ```text
/// If the variable is to be written to an output kernel file, the
/// file should be opened with a logical unit determined by the
/// calling program.
/// ```
///
/// # Particulars
///
/// ```text
/// If the table symbol table does not contain any variables, nothing
/// will be written to UNIT.
/// ```
///
/// # Examples
///
/// ```text
/// If NAME = 'MEAN_ANOM'
/// DIRCTV = '='
///
/// And the contents of the symbol table are:
///
/// DELTA_T_A --> 32.184
/// K --> 0.D0
/// MEAN_ANOM --> 6.239996D0
/// 1.99096871D-7
/// ORBIT_ECC --> 1.671D-2
///
/// The output to UNIT might look like this, depending on the
/// length of the symbol table variables:
///
/// MEAN_ANOM = ( 6.239996D0,
/// 1.99096871D-7 )
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 08-APR-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// Updated $Brief_I/O entry for symbol table (TABSYM, TABPTR,
/// TABVAR) to indicate that these arguments only are an input to
/// the routine, and not an output. Removed $Detailed_Output
/// documentation, as the routine operates by side-effects and has
/// no output arguments.
///
/// - 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, 31-JAN-1990 (IMU) (NJB)
/// ```
///
/// # Revisions
///
/// ```text
/// - Beta Version 1.1.0, 20-DEC-1988 (NJB)
///
/// Call to IOERR changed to be consistent with new calling
/// protocol. SETMSG call deleted, since IOERR now calls SETMSG.
/// ```
pub fn wrkvar(
ctx: &mut SpiceContext,
unit: i32,
name: &str,
dirctv: &str,
tabsym: CharArray,
tabptr: &[i32],
tabval: &[f64],
) -> crate::Result<()> {
WRKVAR(
unit,
name.as_bytes(),
dirctv.as_bytes(),
tabsym,
tabptr,
tabval,
ctx.raw_context(),
)?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure WRKVAR ( Write a variable to a kernel file )
pub fn WRKVAR(
UNIT: i32,
NAME: &[u8],
DIRCTV: &[u8],
TABSYM: CharArray,
TABPTR: &[i32],
TABVAL: &[f64],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let TABSYM = DummyCharArray::new(TABSYM, None, LBCELL..);
let TABPTR = DummyArray::new(TABPTR, LBCELL..);
let TABVAL = DummyArray::new(TABVAL, LBCELL..);
let mut VARLEN: i32 = 0;
let mut MARGIN: i32 = 0;
let mut VARDIM: i32 = 0;
let mut LINE = [b' '; 132];
let mut DVALUE: f64 = 0.0;
let mut FOUND: bool = false;
let mut IOSTAT: i32 = 0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"WRKVAR", ctx)?;
}
//
// Preliminary measurements.
//
VARLEN = intrinsics::LEN(&TABSYM[1]);
MARGIN = (VARLEN + 6);
VARDIM = SYDIMD(
NAME,
TABSYM.as_arg(),
TABPTR.as_slice(),
TABVAL.as_slice(),
ctx,
)?;
//
// One value per line.
//
for I in 1..=VARDIM {
SYNTHD(
NAME,
I,
TABSYM.as_arg(),
TABPTR.as_slice(),
TABVAL.as_slice(),
&mut DVALUE,
&mut FOUND,
ctx,
)?;
//
// The first line contains the variable name, the directive,
// an optional left parenthesis, and the first value. The values
// of a multi-dimensional variable are separated by commas.
//
if (I == 1) {
LJUST(NAME, &mut LINE);
RJUST(
DIRCTV,
fstr::substr_mut(&mut LINE, (MARGIN - 4)..=(MARGIN - 3)),
);
if (VARDIM > 1) {
fstr::assign(
fstr::substr_mut(&mut LINE, (MARGIN - 1)..=(MARGIN - 1)),
LPAREN,
);
{
use f2rust_std::{
data::Val,
io::{self, Writer},
};
let mut writer = io::ListDirectedWriter::new(ctx.io_unit(UNIT)?, None)?;
IOSTAT = io::capture_iostat(|| {
writer.start()?;
writer.write_str(fstr::substr(&LINE, 1..=MARGIN))?;
writer.write_f64(DVALUE)?;
writer.write_str(COMMA)?;
writer.finish()?;
Ok(())
})?;
}
} else {
{
use f2rust_std::{
data::Val,
io::{self, Writer},
};
let mut writer = io::ListDirectedWriter::new(ctx.io_unit(UNIT)?, None)?;
IOSTAT = io::capture_iostat(|| {
writer.start()?;
writer.write_str(fstr::substr(&LINE, 1..=MARGIN))?;
writer.write_f64(DVALUE)?;
writer.finish()?;
Ok(())
})?;
}
}
//
// The last line of a multi-dimensional variable ends with a
// right parenthesis.
//
} else if ((I > 1) && (I == VARDIM)) {
fstr::assign(&mut LINE, b" ");
{
use f2rust_std::{
data::Val,
io::{self, Writer},
};
let mut writer = io::ListDirectedWriter::new(ctx.io_unit(UNIT)?, None)?;
IOSTAT = io::capture_iostat(|| {
writer.start()?;
writer.write_str(fstr::substr(&LINE, 1..=MARGIN))?;
writer.write_f64(DVALUE)?;
writer.write_str(RPAREN)?;
writer.finish()?;
Ok(())
})?;
}
} else {
fstr::assign(&mut LINE, b" ");
{
use f2rust_std::{
data::Val,
io::{self, Writer},
};
let mut writer = io::ListDirectedWriter::new(ctx.io_unit(UNIT)?, None)?;
IOSTAT = io::capture_iostat(|| {
writer.start()?;
writer.write_str(fstr::substr(&LINE, 1..=MARGIN))?;
writer.write_f64(DVALUE)?;
writer.write_str(COMMA)?;
writer.finish()?;
Ok(())
})?;
}
}
}
if (IOSTAT != 0) {
IOERR(
b"writing a variable to the output kernel file",
b" ",
IOSTAT,
ctx,
);
SIGERR(b"SPICE(WRITEERROR)", ctx)?;
}
CHKOUT(b"WRKVAR", ctx)?;
Ok(())
}