rsspice 0.1.0

Pure Rust port of the SPICE Toolkit for space geometry
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//
// GENERATED FILE
//

use super::*;
use f2rust_std::*;

pub const RECL: i32 = 1024;
const BODID: i32 = 1;
const CENID: i32 = (BODID + 1);
const SPKFRM: i32 = (CENID + 1);
const SPKTYP: i32 = (SPKFRM + 1);
const START: i32 = (SPKTYP + 1);
const FINISH: i32 = (START + 1);
const CKRATE: i32 = SPKTYP;
const ND: i32 = 2;
const NI: i32 = 6;
const SUMSIZ: i32 = (ND + ((NI + 1) / 2));
const IDLEN: i32 = 12;
const MINPCH: i32 = 32;
const MAXPCH: i32 = 126;

//$Procedure ZZGETFAT ( Get file architecture, type, and unit )
pub fn ZZGETFAT(
    FILE: &[u8],
    ARCH: &mut [u8],
    TYPE: &mut [u8],
    NUMBER: &mut i32,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let mut IDWORD = [b' '; IDLEN as usize];
    let mut TMPWRD = [b' '; IDLEN as usize];
    let mut IOSTAT: i32 = 0;
    let mut DIROPN: bool = false;
    let mut SEQOPN: bool = false;
    let mut CHECK: bool = false;
    let mut EXIST: bool = false;
    let mut OPENED: bool = false;

    //

    //
    // SPICELIB functions
    //

    //
    // Local parameters
    //
    //
    // The following parameters point to the various slots in the
    // integer portion of the DAF descriptor where the values are
    // located.
    //

    //
    // These parameters give the number of integer and double precision
    // components of the descriptor for SPK and CK files.
    //

    //
    // The size of a summary.
    //

    //
    // Set the length of a SPICE kernel file ID word.
    //
    //
    // Set minimum and maximum values for the range of ASCII printing
    // characters.
    //

    //
    // Local Variables
    //

    //
    // Standard SPICE error handling.
    //
    if spicelib::RETURN(ctx) {
        return Ok(());
    } else {
        spicelib::CHKIN(b"ZZGETFAT", ctx)?;
    }

    //
    // Initialize the temporary storage variables that we use.
    //
    fstr::assign(&mut IDWORD, b" ");
    SEQOPN = false;
    CHECK = true;

    //
    // If the filename we got is blank, signal an error and return.
    //
    if fstr::eq(FILE, b" ") {
        spicelib::SETMSG(b"The file name is blank.", ctx);
        spicelib::SIGERR(b"SPICE(BLANKFILENAME)", ctx)?;
        spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
        return Ok(());
    }

    //
    // We'll do a bit of inquiring before we try opening anything.
    //
    {
        use f2rust_std::io;

        let specs = io::InquireSpecs {
            file: Some(FILE),
            exist: Some(&mut EXIST),
            opened: Some(&mut OPENED),
            ..Default::default()
        };
        IOSTAT = io::capture_iostat(|| ctx.inquire(specs))?;
    }

    //
    // Not too likely, but if the INQUIRE statement fails...
    //
    if (IOSTAT != 0) {
        spicelib::SETMSG(b"IOSTAT error in INQUIRE statement. IOSTAT = #.", ctx);
        spicelib::ERRINT(b"#", IOSTAT, ctx);
        spicelib::SIGERR(b"SPICE(INQUIREERROR)", ctx)?;
        spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
        return Ok(());
    }
    //
    // Note: the following two tests MUST be performed in the order in
    //       which they appear, since in some environments files that do
    //       not exist are considered to be open.
    //
    // By calling this routine, the user implies that the file exists.
    //
    if !EXIST {
        spicelib::SETMSG(b"The kernel file \'#\' does not exist.", ctx);
        spicelib::ERRCH(b"#", FILE, ctx);
        spicelib::SIGERR(b"SPICE(NOSUCHFILE)", ctx)?;
        spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
        return Ok(());
    }

    //
    // This routine should not be called if the file is already open.
    //
    if OPENED {
        spicelib::SETMSG(b"The kernel file \'#\' is already open.", ctx);
        spicelib::ERRCH(b"#", FILE, ctx);
        spicelib::SIGERR(b"SPICE(FILECURRENTLYOPEN)", ctx)?;
        spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
        return Ok(());
    }

    //
    // Open the file with a record length of RECL (the length of the
    // DAF and DAS records). We assume, for now, that opening the file as
    // a direct access file will work.
    //

    DIROPN = true;

    spicelib::GETLUN(NUMBER, ctx)?;

    {
        use f2rust_std::io;

        let specs = io::OpenSpecs {
            unit: Some(*NUMBER),
            file: Some(FILE),
            access: Some(b"DIRECT"),
            recl: Some(RECL),
            status: Some(b"OLD"),
            ..Default::default()
        };
        IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
    }

    //
    // If we had trouble opening the file, try opening it as a sequential
    // file.
    //
    if (IOSTAT != 0) {
        DIROPN = false;

        {
            use f2rust_std::io;

            let specs = io::OpenSpecs {
                unit: Some(*NUMBER),
                file: Some(FILE),
                access: Some(b"SEQUENTIAL"),
                status: Some(b"OLD"),
                ..Default::default()
            };
            IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
        }
        //
        // If we still have problems opening the file, we don't have a
        // clue about the file architecture and type.
        //
        if (IOSTAT != 0) {
            fstr::assign(ARCH, b"?");
            fstr::assign(TYPE, b"?");
            spicelib::SETMSG(b"Attempt to open the file \'#\' failed. IOSTAT = #.", ctx);
            spicelib::ERRCH(b"#", FILE, ctx);
            spicelib::ERRINT(b"#", IOSTAT, ctx);
            spicelib::SIGERR(b"SPICE(FILEOPENFAILED)", ctx)?;
            spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
            return Ok(());
        }
    }

    //
    // We opened the file successfully, so let's try to read from the
    // file. We need to be sure to use the correct form of the read
    // statement, depending on whether the file was opened with direct
    // access or sequential access.
    //
    if DIROPN {
        {
            use f2rust_std::{
                data::Val,
                io::{self, Reader},
            };

            let mut reader = io::UnformattedReader::new(ctx.io_unit(*NUMBER)?, Some(1))?;
            IOSTAT = io::capture_iostat(|| {
                reader.start()?;
                reader.read_str(&mut TMPWRD)?;
                reader.finish()?;
                Ok(())
            })?;
        }
        //
        // If we couldn't read from the file as a direct access file with
        // a fixed record length, then try to open the file as a
        // sequential file and read from it.
        //
        if (IOSTAT == 0) {
            SEQOPN = true;
            DIROPN = false;

            {
                use f2rust_std::io;

                let specs = io::CloseSpecs {
                    unit: Some(*NUMBER),
                    ..Default::default()
                };
                ctx.close(specs)?;
            }

            {
                use f2rust_std::io;

                let specs = io::OpenSpecs {
                    unit: Some(*NUMBER),
                    file: Some(FILE),
                    access: Some(b"SEQUENTIAL"),
                    status: Some(b"OLD"),
                    ..Default::default()
                };
                IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
            }
            //
            // If we could not open the file, we don't have a clue about
            // the file architecture and type.
            //
            if (IOSTAT != 0) {
                fstr::assign(ARCH, b"?");
                fstr::assign(TYPE, b"?");
                spicelib::SETMSG(b"Attempt to open the file \'#\' failed. IOSTAT = #.", ctx);
                spicelib::ERRCH(b"#", FILE, ctx);
                spicelib::ERRINT(b"#", IOSTAT, ctx);
                spicelib::SIGERR(b"SPICE(FILEOPENFAILED)", ctx)?;
                spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
                return Ok(());
            }
            //
            // Try to read from the file.
            //
            {
                use f2rust_std::{
                    data::Val,
                    io::{self, Reader},
                };

                let mut reader = io::FormattedReader::new(ctx.io_unit(*NUMBER)?, None, b"(A)")?;
                IOSTAT = io::capture_iostat(|| {
                    reader.start()?;
                    reader.read_str(&mut TMPWRD)?;
                    reader.finish()?;
                    Ok(())
                })?;
            }
        }
    } else {
        SEQOPN = true;
        {
            use f2rust_std::{
                data::Val,
                io::{self, Reader},
            };

            let mut reader = io::FormattedReader::new(ctx.io_unit(*NUMBER)?, None, b"(A)")?;
            IOSTAT = io::capture_iostat(|| {
                reader.start()?;
                reader.read_str(&mut TMPWRD)?;
                reader.finish()?;
                Ok(())
            })?;
        }
    }

    //
    // If we had an error while reading, we don't recognize this file.
    //
    if (IOSTAT != 0) {
        fstr::assign(ARCH, b"?");
        fstr::assign(TYPE, b"?");
        {
            use f2rust_std::io;

            let specs = io::CloseSpecs {
                unit: Some(*NUMBER),
                ..Default::default()
            };
            ctx.close(specs)?;
        }
        spicelib::SETMSG(b"Attempt to read from file \'#\' failed. IOSTAT = #.", ctx);
        spicelib::ERRCH(b"#", FILE, ctx);
        spicelib::ERRINT(b"#", IOSTAT, ctx);
        spicelib::SIGERR(b"SPICE(FILEREADFAILED)", ctx)?;
        spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
        return Ok(());
    }

    //
    // Loop until a known NAIF file ID word is found.
    //
    while CHECK {
        //
        // At this point, we have a candidate for an ID word. To avoid
        // difficulties with Fortran I/O and other things, we will now
        // replace any non printing ASCII characters with blanks.
        //
        for I in 1..=IDLEN {
            if ((intrinsics::ICHAR(fstr::substr(&TMPWRD, I..=I)) < MINPCH)
                || (intrinsics::ICHAR(fstr::substr(&TMPWRD, I..=I)) > MAXPCH))
            {
                fstr::assign(fstr::substr_mut(&mut TMPWRD, I..=I), b" ");
            }
        }

        //
        // Identify the architecture and type, if we can.
        //
        spicelib::LJUST(&TMPWRD.clone(), &mut TMPWRD);
        spicelib::UCASE(&TMPWRD.clone(), &mut TMPWRD, ctx);
        spicelib::NEXTWD(&TMPWRD.clone(), &mut IDWORD, &mut TMPWRD);

        if fstr::eq(&IDWORD, b"DAFETF") {
            //
            // We have a DAF encoded transfer file.
            //
            fstr::assign(ARCH, b"XFR");
            fstr::assign(TYPE, b"DAF");
            CHECK = false;
        } else if fstr::eq(&IDWORD, b"DASETF") {
            //
            // We have a DAS encoded transfer file.
            //
            fstr::assign(ARCH, b"XFR");
            fstr::assign(TYPE, b"DAS");
            CHECK = false;
        } else if fstr::eq(fstr::substr(&IDWORD, 1..=10), b"\'NAIF/DAF\'") {
            //
            // We have an old DAF decimal text file.
            //
            fstr::assign(ARCH, b"DEC");
            fstr::assign(TYPE, b"DAF");
            CHECK = false;
        } else if fstr::eq(fstr::substr(&IDWORD, 1..=8), b"NAIF/DAS") {
            //
            // We have a pre release DAS binary file.
            //
            fstr::assign(ARCH, b"DAS");
            fstr::assign(TYPE, b"PRE");
            CHECK = false;
        } else {
            //
            // Get the architecture and type from the ID word, if we can.
            //
            spicelib::IDW2AT(fstr::substr(&IDWORD, 1..=8), ARCH, TYPE, ctx)?;

            if (fstr::eq(ARCH, b"DAF") && fstr::eq(TYPE, b"?")) {
                CHECK = false;
            } else {
                //
                // No identification on line.  Read another line.
                //
                if SEQOPN {
                    {
                        use f2rust_std::{
                            data::Val,
                            io::{self, Reader},
                        };

                        let mut reader =
                            io::FormattedReader::new(ctx.io_unit(*NUMBER)?, None, b"(A)")?;
                        IOSTAT = io::capture_iostat(|| {
                            reader.start()?;
                            reader.read_str(&mut TMPWRD)?;
                            reader.finish()?;
                            Ok(())
                        })?;
                    }
                } else {
                    {
                        use f2rust_std::{
                            data::Val,
                            io::{self, Reader},
                        };

                        let mut reader =
                            io::UnformattedReader::new(ctx.io_unit(*NUMBER)?, Some(1))?;
                        IOSTAT = io::capture_iostat(|| {
                            reader.start()?;
                            reader.read_str(&mut TMPWRD)?;
                            reader.finish()?;
                            Ok(())
                        })?;
                    }
                }

                //
                // If IOSTAT is a negative value, we probably hit an
                // end-of-file.  Error out.
                //
                if (IOSTAT < 0) {
                    fstr::assign(ARCH, b"?");
                    fstr::assign(TYPE, b"?");
                    {
                        use f2rust_std::io;

                        let specs = io::CloseSpecs {
                            unit: Some(*NUMBER),
                            ..Default::default()
                        };
                        ctx.close(specs)?;
                    }
                    spicelib::SETMSG(
                        b"Encountered end-of-file of # before  finding known SPICE ID word.",
                        ctx,
                    );
                    spicelib::ERRCH(b"#", FILE, ctx);
                    spicelib::SIGERR(b"SPICE(ENDOFFILE)", ctx)?;
                    spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
                    return Ok(());
                }
            }
        }
    }

    spicelib::CHKOUT(b"ZZGETFAT", ctx)?;
    Ok(())
}