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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const NWC: i32 = 1024;
const CHAR: i32 = 1;
/// DAS, update data, character
///
/// Update character data in a specified range of DAS logical
/// addresses with substrings of a character array.
///
/// # Required Reading
///
/// * [DAS](crate::required_reading::das)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// HANDLE I DAS file handle.
/// FIRST,
/// LAST I Range of DAS character logical addresses.
/// BPOS,
/// EPOS I Begin and end positions of substrings.
/// DATA I Data having addresses FIRST through LAST.
/// ```
///
/// # Detailed Input
///
/// ```text
/// HANDLE is a file handle of a DAS file opened for writing.
///
/// FIRST,
/// LAST are the first and last of a range of DAS logical
/// addresses of characters. These addresses satisfy
/// the inequality
///
/// 1 <= FIRST <= LAST <= LASTC
///
/// where LASTC is the last character logical address
/// in use in the DAS file designated by HANDLE.
///
/// BPOS,
/// EPOS are the begin and end character positions that define the
/// substrings in each of the elements of the input array
/// that are to replace the data in the range of DAS
/// character addresses given by FIRST and LAST.
///
/// DATA is an array of strings. The contents of the specified
/// substrings of the elements of the array DATA will be
/// written to the indicated DAS file in order:
/// DATA(1)(BPOS:BPOS) will be written to character logical
/// address FIRST; DATA(1)(BPOS+1:BPOS+1) will be written to
/// the character logical address FIRST+1, and so on; in this
/// ordering scheme, character (BPOS:BPOS) of DATA(I+1) is
/// the successor of character (EPOS:EPOS) of DATA(I).
///
/// DATA must be declared at least as
///
/// CHARACTER*(EPOS) DATA ( R )
///
/// with the dimension R being at least
///
/// R = INT( ( LAST - FIRST + SUBLEN ) / SUBLEN )
///
/// and SUBLEN, the length of each of the substrings in
/// the array to be written to the DAS file, being
///
/// SUBLEN = EPOS - BPOS + 1
/// ```
///
/// # Detailed Output
///
/// ```text
/// None.
///
/// See $Particulars for a description of the effect of this routine.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the input file handle is invalid, an error is signaled by
/// a routine in the call tree of this routine.
///
/// 2) Only logical addresses that already contain data may be
/// updated: if either FIRST or LAST are outside the range
///
/// [ 1, LASTC ]
///
/// where LASTC is the last character logical address that
/// currently contains data in the indicated DAS file, the error
/// SPICE(INVALIDADDRESS) is signaled. The DAS file will not be
/// modified.
///
/// 3) If EPOS or BPOS are outside of the range
///
/// [ 1, LEN( DATA(1) ) ]
///
/// the error SPICE(INVALIDINDEX) is signaled.
///
/// 4) If BPOS is greater than EPOS, the error
/// SPICE(INDICESOUTOFORDER) is signaled.
///
/// 5) If FIRST > LAST but both addresses are valid, this routine
/// will not modify the indicated DAS file. No error will be
/// signaled.
///
/// 6) If an I/O error occurs during the data update attempted
/// by this routine, the error is signaled by a routine in the
/// call tree of this routine. FIRST and LAST will not be
/// modified.
/// ```
///
/// # Files
///
/// ```text
/// See the description of the argument HANDLE in $Detailed_Input.
/// ```
///
/// # Particulars
///
/// ```text
/// DAS is a low-level format meant to store and transmit data. As
/// such, character data in DAS files are not interpreted by SPICELIB
/// DAS input or output routines. There are no limits on which
/// character values may be placed in the virtual character array of a
/// DAS file.
///
/// This routine replaces the character data in the specified range
/// of logical addresses within a DAS file with the contents of the
/// specified substrings of the input array DATA.
///
/// The actual physical write operations that update the indicated
/// DAS file with the contents of the input array DATA may not take
/// place before this routine returns, since the DAS system buffers
/// data that are written as well as data that are read. In any case,
/// the data will be flushed to the file at the time the file is
/// closed, if not earlier. A physical write of all buffered
/// records can be forced by calling the SPICELIB routine DASWBR
/// (DAS, write buffered records).
///
/// In order to append character data to a DAS file, filling in a
/// range of character logical addresses that starts immediately
/// after the last character logical address currently in use, the
/// SPICELIB routine DASADC (DAS add data, character) should be
/// used.
/// ```
///
/// # 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) The following example demonstrates the capabilities of the
/// DAS character data routines. The reader should notice that
/// in these interfaces, the character data are treated not as
/// strings (or arrays of strings) but as a stream of single
/// characters: DAS character data are not limited to
/// human-readable text. For example, one can store images or
/// DEM data as DAS character data.
///
/// The example shows how to add a variable amount of character
/// data to a new DAS file, how to update some of the character
/// logical addresses within that file, and how to read that
/// data out to a different array.
///
///
/// Example code begins here.
///
///
/// PROGRAM DASUDC_EX1
/// IMPLICIT NONE
///
/// C
/// C Local parameters.
/// C
/// CHARACTER*(*) FNAME
/// PARAMETER ( FNAME = 'dasudc_ex1.das' )
///
/// CHARACTER*(*) TYPE
/// PARAMETER ( TYPE = 'TEST' )
///
/// C
/// C Local variables.
/// C
/// CHARACTER*(22) CDATIN ( 3 )
/// CHARACTER*(30) CDATOU ( 10 )
///
/// INTEGER HANDLE
/// INTEGER I
///
/// DATA CDATOU / '..............................',
/// . '..............................',
/// . '..............................',
/// . '..............................',
/// . '..............................',
/// . '..............................',
/// . '..............................',
/// . '..............................',
/// . ' 1 2 3',
/// . '123456789012345678901234567890' /
///
/// C
/// C Open a new DAS file. Use the file name as the internal
/// C file name, and reserve no records for comments.
/// C
/// CALL DASONW ( FNAME, TYPE, FNAME, 0, HANDLE )
///
/// C
/// C Set the input data. Note that these data will be
/// C considered as a binary data stream: DAS character data
/// C are not limited to human-readable text. For example,
/// C one can store images or DEM data as DAS character data.
/// C
/// CDATIN ( 1 ) = '--F-345678901234567890'
/// CDATIN ( 2 ) = '--S-345678901234567890'
/// CDATIN ( 3 ) = '--T-IRDxxxxxxxxxxxxxxx'
///
/// C
/// C Add the last 20 characters of the first two elements
/// C of CDATIN, and the 3rd character from the third one.
/// C
/// CALL DASADC ( HANDLE, 41, 3, 22, CDATIN )
///
/// C
/// C Update the 10th, 20th and 30th character in the DAS
/// C file with a vertical bar.
/// C
/// DO I = 1, 3
///
/// CALL DASUDC ( HANDLE, I*10, I*10, 1, 1, '|' )
///
/// END DO
///
/// C
/// C Close the file.
/// C
/// CALL DASCLS ( HANDLE )
///
/// C
/// C Now verify the addition of data by opening the
/// C file for read access and retrieving the data.
/// C
/// CALL DASOPR ( FNAME, HANDLE )
///
/// C
/// C Read the 41 characters that we stored on the DAS
/// C file. Update the data on the CDATOU array, placing
/// C 6 characters on each element, starting from the
/// C 10th position.
/// C
/// CALL DASRDC ( HANDLE, 1, 41, 10, 15, CDATOU )
///
/// C
/// C Dump the data to the screen. Note that the last
/// C three lines should remain unmodified, and that
/// C only 5 characters will be written on the 7th line.
/// C
/// WRITE (*,*)
/// WRITE (*,*) 'Data from "', FNAME, '":'
/// WRITE (*,*)
///
/// DO I = 1, 10
/// WRITE (*,*) CDATOU(I)
/// END DO
///
/// C
/// C Close the file.
/// C
/// CALL DASCLS ( HANDLE )
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Data from "dasudc_ex1.das":
///
/// .........F-3456...............
/// .........789|12...............
/// .........345678...............
/// .........9|S-34...............
/// .........56789|...............
/// .........123456...............
/// .........7890T................
/// ..............................
/// 1 2 3
/// 123456789012345678901234567890
///
///
/// Note that after run completion, a new DAS file exists in the
/// output directory.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// K.R. Gehringer (JPL)
/// W.L. Taber (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.0.0, 19-MAY-2021 (NJB) (JDR)
///
/// Added error checks for invalid begin and end indices BPOS
/// and EPOS.
///
/// Added IMPLICIT NONE statement.
///
/// Updated entries in $Exceptions and $Revisions sections and
/// removed reference to nonexistent API from $Particulars.
///
/// Edited the header to comply with NAIF standard.
///
/// Replaced example code with one that demonstrates the usage and
/// effect of all DAS character data routines.
///
/// Updated entries in $Revisions section.
///
/// - SPICELIB Version 1.3.0, 10-APR-2014 (NJB)
///
/// Deleted declarations of unused parameters.
///
/// Corrected header comments: routine that flushes
/// written, buffered records is DASWBR, not DASWUR.
///
/// - SPICELIB Version 1.2.1, 19-DEC-1995 (NJB)
///
/// Corrected title of permuted index entry section.
///
/// - SPICELIB Version 1.2.0, 12-MAY-1995 (NJB)
///
/// Bug fix: routine handled values of BPOS incorrectly when
/// BPOS > 1.
///
/// - SPICELIB Version 1.1.0, 12-MAY-1994 (KRG) (NJB)
///
/// Test of FAILED() added to loop termination conditions.
///
/// Removed references to specific DAS file open routines in the
/// $Detailed_Input section of the header. This was done in order
/// to minimize documentation changes if the DAS open routines ever
/// change.
///
/// Modified the $Examples section to demonstrate the new ID word
/// format which includes a file type and to include a call to the
/// new routine DASONW, open new for write, which makes use of the
/// file type. Also, a variable for the type of the file to be
/// created was added.
///
/// - SPICELIB Version 1.0.0, 12-NOV-1992 (NJB) (WLT)
/// ```
///
/// # Revisions
///
/// ```text
/// - SPICELIB Version 1.2.0, 12-MAY-1995 (NJB)
///
/// Bug fix: routine handled values of BPOS incorrectly when
/// BPOS > 1. This was due to the incorrect initialization
/// of the internal variables CHR and ELT. The initialization
/// was corrected.
///
/// - SPICELIB Version 1.1.0, 12-MAY-1994 (KRG) (NJB)
///
/// Tests of FAILED() added to loop termination conditions.
/// Without these tests, infinite loops could result if DASA2L or
/// DASURC signaled an error inside the loops.
/// ```
pub fn dasudc(
ctx: &mut SpiceContext,
handle: i32,
first: i32,
last: i32,
bpos: i32,
epos: i32,
data: CharArray,
) -> crate::Result<()> {
DASUDC(handle, first, last, bpos, epos, data, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure DASUDC ( DAS, update data, character )
pub fn DASUDC(
HANDLE: i32,
FIRST: i32,
LAST: i32,
BPOS: i32,
EPOS: i32,
DATA: CharArray,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let DATA = DummyCharArray::new(DATA, None, 1..);
let mut CHR: i32 = 0;
let mut CLBASE: i32 = 0;
let mut CLSIZE: i32 = 0;
let mut ELT: i32 = 0;
let mut L: i32 = 0;
let mut LASTC: i32 = 0;
let mut LASTD: i32 = 0;
let mut LASTI: i32 = 0;
let mut N: i32 = 0;
let mut NMOVE: i32 = 0;
let mut NMOVED: i32 = 0;
let mut NUMCHR: i32 = 0;
let mut NWRITN: i32 = 0;
let mut RECNO: i32 = 0;
let mut RCPOS: i32 = 0;
let mut WORDNO: i32 = 0;
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"DASUDC", ctx)?;
//
// Get the last logical addresses in use in this DAS file.
//
DASLLA(HANDLE, &mut LASTC, &mut LASTD, &mut LASTI, ctx)?;
//
// Validate the input addresses.
//
if ((((FIRST < 1) || (FIRST > LASTC)) || (LAST < 1)) || (LAST > LASTC)) {
SETMSG(b"FIRST was #. LAST was #. Valid range is [1,#].", ctx);
ERRINT(b"#", FIRST, ctx);
ERRINT(b"#", LAST, ctx);
ERRINT(b"#", LASTC, ctx);
SIGERR(b"SPICE(INVALIDADDRESS)", ctx)?;
CHKOUT(b"DASUDC", ctx)?;
return Ok(());
}
//
// Make sure BPOS and EPOS are valid and compatible with the string
// length of the input array.
//
if ((BPOS < 1) || (BPOS > intrinsics::LEN(&DATA[1]))) {
SETMSG(
b"String begin index must be in the range #:# but was #.",
ctx,
);
ERRINT(b"#", 1, ctx);
ERRINT(b"#", intrinsics::LEN(&DATA[1]), ctx);
ERRINT(b"#", BPOS, ctx);
SIGERR(b"SPICE(INVALIDINDEX)", ctx)?;
CHKOUT(b"DASUDC", ctx)?;
return Ok(());
}
if ((EPOS < 1) || (EPOS > intrinsics::LEN(&DATA[1]))) {
SETMSG(b"String end index must be in the range #:# but was #.", ctx);
ERRINT(b"#", 1, ctx);
ERRINT(b"#", intrinsics::LEN(&DATA[1]), ctx);
ERRINT(b"#", EPOS, ctx);
SIGERR(b"SPICE(INVALIDINDEX)", ctx)?;
CHKOUT(b"DASUDC", ctx)?;
return Ok(());
}
if (BPOS > EPOS) {
SETMSG(
b"String begin index # must be less than or equal to the end index #.",
ctx,
);
ERRINT(b"#", BPOS, ctx);
ERRINT(b"#", EPOS, ctx);
SIGERR(b"SPICE(INDICESOUTOFORDER)", ctx)?;
CHKOUT(b"DASUDC", ctx)?;
return Ok(());
}
//
// Get the length of the substrings of DATA. Count the total number
// of characters to write.
//
L = ((EPOS - BPOS) + 1);
N = ((LAST - FIRST) + 1);
NWRITN = 0;
//
// Find out the physical location of the first character to update.
//
DASA2L(
HANDLE,
CHAR,
FIRST,
&mut CLBASE,
&mut CLSIZE,
&mut RECNO,
&mut WORDNO,
ctx,
)?;
//
// Write as much data into record RECNO as is necessary and possible.
//
// NUMCHR is the number of characters to write to the current record.
//
// ELT is the index of the element of the input array that we're
// taking data from. CHR is the position in that array element of
// the next character to move to the file.
//
// NMOVED is the number of characters we've moved into the current
// record so far.
//
// RCPOS is the character position we'll write to next in the current
// record.
//
NUMCHR = intrinsics::MIN0(&[N, ((NWC - WORDNO) + 1)]);
ELT = 1;
CHR = BPOS;
NMOVED = 0;
RCPOS = WORDNO;
while ((NMOVED < NUMCHR) && !FAILED(ctx)) {
if (CHR > EPOS) {
ELT = (ELT + 1);
CHR = BPOS;
}
//
// Find out how many characters to move from the current array
// element to the current record.
//
NMOVE = intrinsics::MIN0(&[(NUMCHR - NMOVED), ((EPOS - CHR) + 1)]);
//
// Update the current record.
//
DASURC(
HANDLE,
RECNO,
RCPOS,
((RCPOS + NMOVE) - 1),
fstr::substr(&DATA[ELT], CHR..=((CHR + NMOVE) - 1)),
ctx,
)?;
NMOVED = (NMOVED + NMOVE);
RCPOS = (RCPOS + NMOVE);
CHR = (CHR + NMOVE);
}
NWRITN = NUMCHR;
RECNO = (RECNO + 1);
//
// Update as many additional records as necessary.
//
while ((NWRITN < N) && !FAILED(ctx)) {
//
// At this point, RECNO is the correct number of the record to
// write to next. CLBASE is the number of the first record of
// the cluster we're about to write to.
//
if (RECNO < (CLBASE + CLSIZE)) {
//
// We can continue writing the current cluster. Find
// out how many elements to write to the current record,
// and write them.
//
NUMCHR = intrinsics::MIN0(&[(N - NWRITN), NWC]);
NMOVED = 0;
RCPOS = 1;
while ((NMOVED < NUMCHR) && !FAILED(ctx)) {
if (CHR > L) {
ELT = (ELT + 1);
CHR = BPOS;
}
//
// Find out how many characters to move from the array
// element to the current record.
//
NMOVE = intrinsics::MIN0(&[(NUMCHR - NMOVED), ((EPOS - CHR) + 1)]);
DASURC(
HANDLE,
RECNO,
RCPOS,
((RCPOS + NMOVE) - 1),
fstr::substr(&DATA[ELT], CHR..=((CHR + NMOVE) - 1)),
ctx,
)?;
NMOVED = (NMOVED + NMOVE);
RCPOS = (RCPOS + NMOVE);
CHR = (CHR + NMOVE);
}
NWRITN = (NWRITN + NUMCHR);
RECNO = (RECNO + 1);
} else {
//
// We must find the next character cluster to write to.
// The first character in this cluster has address FIRST +
// NWRITN.
//
DASA2L(
HANDLE,
CHAR,
(FIRST + NWRITN),
&mut CLBASE,
&mut CLSIZE,
&mut RECNO,
&mut WORDNO,
ctx,
)?;
}
}
CHKOUT(b"DASUDC", ctx)?;
Ok(())
}