1 /* spkw03.f -- translated by f2c (version 19980913).
2    You must link the resulting object file with the libraries:
3 	-lf2c -lm   (in that order)
4 */
5 
6 #include "f2c.h"
7 
8 /* Table of constant values */
9 
10 static integer c__27 = 27;
11 static integer c__40 = 40;
12 static integer c__2 = 2;
13 static integer c__6 = 6;
14 static integer c__1 = 1;
15 
16 /* $Procedure SPKW03 ( SPK, write segment, type 3 ) */
spkw03_(integer * handle,integer * body,integer * center,char * frame,doublereal * first,doublereal * last,char * segid,doublereal * intlen,integer * n,integer * polydg,doublereal * cdata,doublereal * btime,ftnlen frame_len,ftnlen segid_len)17 /* Subroutine */ int spkw03_(integer *handle, integer *body, integer *center,
18 	char *frame, doublereal *first, doublereal *last, char *segid,
19 	doublereal *intlen, integer *n, integer *polydg, doublereal *cdata,
20 	doublereal *btime, ftnlen frame_len, ftnlen segid_len)
21 {
22     /* System generated locals */
23     integer i__1;
24     doublereal d__1, d__2;
25 
26     /* Local variables */
27     integer i__, k;
28     extern /* Subroutine */ int etcal_(doublereal *, char *, ftnlen), chkin_(
29 	    char *, ftnlen), dafps_(integer *, integer *, doublereal *,
30 	    integer *, doublereal *);
31     doublereal descr[5];
32     extern /* Subroutine */ int errch_(char *, char *, ftnlen, ftnlen);
33     doublereal ltime;
34     extern /* Subroutine */ int errdp_(char *, doublereal *, ftnlen);
35     doublereal rsize;
36     char etstr[40];
37     extern /* Subroutine */ int dafada_(doublereal *, integer *), dafbna_(
38 	    integer *, doublereal *, char *, ftnlen), dafena_(void);
39     extern logical failed_(void);
40     extern /* Subroutine */ int chckid_(char *, integer *, char *, ftnlen,
41 	    ftnlen);
42     integer refcod, ninrec;
43     extern /* Subroutine */ int namfrm_(char *, integer *, ftnlen);
44     doublereal radius, numrec;
45     extern /* Subroutine */ int sigerr_(char *, ftnlen), chkout_(char *,
46 	    ftnlen), setmsg_(char *, ftnlen), errint_(char *, integer *,
47 	    ftnlen);
48     extern logical return_(void);
49     char netstr[40];
50     doublereal dcd[2];
51     integer icd[6];
52     doublereal mid, tol;
53 
54 /* $ Abstract */
55 
56 /*     Write a type 3 segment to an SPK file. */
57 
58 /* $ Disclaimer */
59 
60 /*     THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE */
61 /*     CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S. */
62 /*     GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE */
63 /*     ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE */
64 /*     PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS" */
65 /*     TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY */
66 /*     WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A */
67 /*     PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC */
68 /*     SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE */
69 /*     SOFTWARE AND RELATED MATERIALS, HOWEVER USED. */
70 
71 /*     IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA */
72 /*     BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT */
73 /*     LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, */
74 /*     INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, */
75 /*     REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE */
76 /*     REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY. */
77 
78 /*     RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF */
79 /*     THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY */
80 /*     CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE */
81 /*     ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE. */
82 
83 /* $ Required_Reading */
84 
85 /*     NAIF_IDS */
86 /*     SPC */
87 /*     SPK */
88 
89 /* $ Keywords */
90 
91 /*     EPHEMERIS */
92 
93 /* $ Declarations */
94 /* $ Abstract */
95 
96 /*     Declare parameters specific to SPK type 3. */
97 
98 /* $ Disclaimer */
99 
100 /*     THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE */
101 /*     CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S. */
102 /*     GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE */
103 /*     ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE */
104 /*     PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS" */
105 /*     TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY */
106 /*     WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A */
107 /*     PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC */
108 /*     SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE */
109 /*     SOFTWARE AND RELATED MATERIALS, HOWEVER USED. */
110 
111 /*     IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA */
112 /*     BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT */
113 /*     LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, */
114 /*     INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, */
115 /*     REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE */
116 /*     REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY. */
117 
118 /*     RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF */
119 /*     THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY */
120 /*     CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE */
121 /*     ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE. */
122 
123 /* $ Required_Reading */
124 
125 /*     SPK */
126 
127 /* $ Keywords */
128 
129 /*     SPK */
130 
131 /* $ Restrictions */
132 
133 /*     None. */
134 
135 /* $ Author_and_Institution */
136 
137 /*     N.J. Bachman      (JPL) */
138 
139 /* $ Literature_References */
140 
141 /*     None. */
142 
143 /* $ Version */
144 
145 /* -    SPICELIB Version 1.0.0, 30-DEC-2013 (NJB) */
146 
147 /* -& */
148 /*     MAXDEG         is the maximum allowed degree of the input */
149 /*                    Chebyshev expansions. If the value of MAXDEG is */
150 /*                    increased, the SPICELIB routine SPKPVN must be */
151 /*                    changed accordingly. In particular, the size of */
152 /*                    the record passed to SPKRnn and SPKEnn must be */
153 /*                    increased, and comments describing the record size */
154 /*                    must be changed. */
155 
156 /*                    The record size requirement is */
157 
158 /*                       MAXREC = ( 6 * (MAXDEG+1) ) + 3 */
159 
160 
161 
162 /*     TOLSCL         is a tolerance scale factor (also called a */
163 /*                    "relative tolerance") used for time coverage */
164 /*                    bound checking. TOLSCL is unitless. TOLSCL */
165 /*                    produces a tolerance value via the formula */
166 
167 /*                       TOL = TOLSCL * MAX( ABS(FIRST), ABS(LAST) ) */
168 
169 /*                    where FIRST and LAST are the coverage time bounds */
170 /*                    of a type 3 segment, expressed as seconds past */
171 /*                    J2000 TDB. */
172 
173 /*                    The resulting parameter TOL is used as a tolerance */
174 /*                    for comparing the input segment descriptor time */
175 /*                    bounds to the first and last epoch covered by the */
176 /*                    sequence of time intervals defined by the inputs */
177 /*                    to SPKW03: */
178 
179 /*                       BTIME */
180 /*                       INTLEN */
181 /*                       N */
182 
183 /*     Tolerance scale for coverage gap at the endpoints */
184 /*     of the segment coverage interval: */
185 
186 
187 /*     End of include file spk03.inc. */
188 
189 /* $ Brief_I/O */
190 
191 /*     Variable  I/O  Description */
192 /*     --------  ---  -------------------------------------------------- */
193 /*     MAXDEG     P   Maximum degree of Chebyshev expansions. */
194 /*     TOLSCL     P   Scale factor used to compute time bound tolerance. */
195 /*     HANDLE     I   Handle of SPK file open for writing. */
196 /*     BODY       I   NAIF code for ephemeris object. */
197 /*     CENTER     I   NAIF code for the center of motion of the body. */
198 /*     FRAME      I   Reference frame name. */
199 /*     FIRST      I   Start time of interval covered by segment. */
200 /*     LAST       I   End time of interval covered by segment. */
201 /*     SEGID      I   Segment identifier. */
202 /*     INTLEN     I   Length of time covered by record. */
203 /*     N          I   Number of records in segment. */
204 /*     POLYDG     I   Chebyshev polynomial degree. */
205 /*     CDATA      I   Array of Chebyshev coefficients. */
206 /*     BTIME      I   Begin time of first record. */
207 
208 /* $ Detailed_Input */
209 
210 /*     HANDLE         DAF handle of an SPK file to which a type 3 segment */
211 /*                    is to be added.  The SPK file must be open for */
212 /*                    writing. */
213 
214 /*     BODY           NAIF integer code for an ephemeris object whose */
215 /*                    state relative to another body is described by the */
216 /*                    segment to be created. */
217 
218 /*     CENTER         NAIF integer code for the center of motion of the */
219 /*                    object identified by BODY. */
220 
221 /*     FRAME          NAIF name for a reference frame relative to which */
222 /*                    the state information for BODY is specified. */
223 
224 /*     FIRST, */
225 /*     LAST           Start and stop times of the time interval over */
226 /*                    which the segment defines the state of body. */
227 
228 /*     SEGID          Segment identifier.  An SPK segment identifier may */
229 /*                    contain up to 40 characters. */
230 
231 /*     INTLEN         Length of time, in seconds, covered by each set of */
232 /*                    Chebyshev polynomial coefficients (each logical */
233 /*                    record).  Each set of Chebyshev coefficents must */
234 /*                    cover this fixed time interval, INTLEN. */
235 
236 /*     N              Number of sets of Chebyshev polynomial coefficients */
237 /*                    for coordinates and their derivatives (number of */
238 /*                    logical records) to be stored in the segment. */
239 /*                    There is one set of Chebyshev coefficients for each */
240 /*                    time period. */
241 
242 /*     POLYDG         Degree of each set of Chebyshev polynomials. POLYDG */
243 /*                    must not exceed MAXDEG (see Parameters below). */
244 
245 /*     CDATA          Array containing all the sets of Chebyshev */
246 /*                    polynomial coefficients to be placed in the */
247 /*                    segment of the SPK file. The coefficients are */
248 /*                    stored in CDATA in order as follows: */
249 
250 /*                       the (degree + 1) coefficients for the first */
251 /*                       coordinate of the first logical record */
252 
253 /*                       the coefficients for the second coordinate */
254 
255 /*                       the coefficients for the third coordinate */
256 
257 /*                       the coefficients for the derivative with respect */
258 /*                       to time of the first coordinate */
259 
260 /*                       the coefficients for the derivative with respect */
261 /*                       to time of the second coordinate */
262 
263 /*                       the coefficients for the derivative with respect */
264 /*                       to time of the third coordinate */
265 
266 /*                       the coefficients for the first coordinate for */
267 /*                       the second logical record, ... */
268 
269 /*                       and so on. */
270 
271 /*                    The logical data records are stored contiguously: */
272 
273 /*                       +----------+ */
274 /*                       | Record 1 | */
275 /*                       +----------+ */
276 /*                       | Record 2 | */
277 /*                       +----------+ */
278 /*                           ... */
279 /*                       +----------+ */
280 /*                       | Record N | */
281 /*                       +----------+ */
282 
283 /*                    The contents of an individual record are: */
284 
285 /*                       +--------------------------------------+ */
286 /*                       | Coeff set for X position component   | */
287 /*                       +--------------------------------------+ */
288 /*                       | Coeff set for Y position component   | */
289 /*                       +--------------------------------------+ */
290 /*                       | Coeff set for Z position component   | */
291 /*                       +--------------------------------------+ */
292 /*                       | Coeff set for X velocity component   | */
293 /*                       +--------------------------------------+ */
294 /*                       | Coeff set for Y velocity component   | */
295 /*                       +--------------------------------------+ */
296 /*                       | Coeff set for Z velocity component   | */
297 /*                       +--------------------------------------+ */
298 
299 /*                   Each coefficient set has the structure: */
300 
301 /*                       +--------------------------------------+ */
302 /*                       | Coefficient of T_0                   | */
303 /*                       +--------------------------------------+ */
304 /*                       | Coefficient of T_1                   | */
305 /*                       +--------------------------------------+ */
306 /*                                         ... */
307 /*                       +--------------------------------------+ */
308 /*                       | Coefficient of T_POLYDG              | */
309 /*                       +--------------------------------------+ */
310 
311 /*                    Where T_n represents the Chebyshev polynomial */
312 /*                    of the first kind of degree n. */
313 
314 
315 /*     BTIME          Begin time (seconds past J2000 TDB) of first set */
316 /*                    of Chebyshev polynomial coefficients (first */
317 /*                    logical record). */
318 
319 /* $ Detailed_Output */
320 
321 /*     None. */
322 
323 /* $ Parameters */
324 
325 /*     See the include file spk03.inc for declarations of the */
326 /*     parameters described below. */
327 
328 /*     TOLSCL         is a tolerance scale for coverage gap at endpoints */
329 /*                    of the segment coverage interval. */
330 
331 /*     MAXDEG         is the maximum allowed degree of the input */
332 /*                    Chebyshev expansions. */
333 
334 /* $ Exceptions */
335 
336 /*     1)  If the number of sets of coefficients is not positive */
337 /*         SPICE(NUMCOEFFSNOTPOS) is signaled. */
338 
339 /*     2)  If the interval length is not positive, SPICE(INTLENNOTPOS) */
340 /*         is signaled. */
341 
342 /*     3)  If the name of the reference frame is not recognized, */
343 /*         SPICE(INVALIDREFFRAME) is signaled. */
344 
345 /*     4)  If segment stop time is not greater then the begin time, */
346 /*         SPICE(BADDESCRTIMES) is signaled. */
347 
348 /*     5)  If the start time of the first record exceeds the descriptor */
349 /*         begin time by more than a computed tolerance, or if the end */
350 /*         time of the last record precedes the descriptor end time by */
351 /*         more than a computed tolerance, the error SPICE(COVERAGEGAP) */
352 /*         is signaled. See the Parameters section above for a */
353 /*         description of the tolerance. */
354 
355 /*     6)  If the input degree POLYDG is less than 0 or greater than */
356 /*         MAXDEG, the error SPICE(INVALIDDEGREE) is signaled. */
357 
358 /*     7)  If the last non-blank character of SEGID occurs past index */
359 /*         40, or if SEGID contains any nonprintable characters, the */
360 /*         error will be diagnosed by a routine in the call tree of this */
361 /*         routine. */
362 
363 /* $ Files */
364 
365 /*     A new type 3 SPK segment is written to the SPK file attached */
366 /*     to HANDLE. */
367 
368 /* $ Particulars */
369 
370 /*     This routine writes an SPK type 3 data segment to the designated */
371 /*     SPK file, according to the format described in the SPK Required */
372 /*     Reading. */
373 
374 /*     Each segment can contain data for only one target, central body, */
375 /*     and reference frame.  The Chebyshev polynomial degree and length */
376 /*     of time covered by each logical record are also fixed.  However, */
377 /*     an arbitrary number of logical records of Chebyshev polynomial */
378 /*     coefficients can be written in each segment. Minimizing the */
379 /*     number of segments in an SPK file will help optimize how the SPICE */
380 /*     system accesses the file. */
381 
382 /* $ Examples */
383 
384 /*     Suppose that you have sets of Chebyshev polynomial coefficients */
385 /*     in an array CDATA pertaining to the position of the moon (NAIF ID */
386 /*     = 301), relative to the Earth-moon barycenter (NAIF ID = 3), in */
387 /*     the J2000 reference frame, and want to put these into a type 2 */
388 /*     segment in an existing SPK file.  The following code could be used */
389 /*     to add one new type 2 segment.  To add multiple segments, put the */
390 /*     call to SPKW02 in a loop. */
391 
392 /*     C */
393 /*     C      First open the SPK file and get a handle for it. */
394 /*     C */
395 /*            CALL DAFOPW ( SPKNAM, HANDLE ) */
396 
397 /*     C */
398 /*     C      Create a segment identifier. */
399 /*     C */
400 /*            SEGID = 'MY_SAMPLE_SPK_TYPE_3_SEGMENT' */
401 
402 /*     C */
403 /*     C      Write the segment. */
404 
405 /*            SUBROUTINE SPKW03 ( HANDLE, 301,    3,      'J2000', */
406 /*          .                     FIRST,  LAST,   SEGID,  INTLEN, */
407 /*          .                     N,      POLYDG, CDATA,  BTIME ) */
408 
409 /*     C */
410 /*     C      Close the file. */
411 /*     C */
412 /*            CALL DAFCLS ( HANDLE ) */
413 
414 /* $ Restrictions */
415 
416 /*     None. */
417 
418 /* $ Literature_References */
419 
420 /*     None. */
421 
422 /* $ Author_and_Institution */
423 
424 /*     K.S. Zukor (JPL) */
425 
426 /* $ Version */
427 
428 /* -    SPICELIB Version 2.0.0, 18-JAN-2014 (NJB) */
429 
430 /*        Relaxed test on relationship between the time bounds of the */
431 /*        input record set (determined by BTIME, INTLEN, and N) and the */
432 /*        descriptor bounds FIRST and LAST. Now the descriptor bounds */
433 /*        may extend beyond the time bounds of the record set by a ratio */
434 /*        computed using the parameter TOLSCL (see Parameters above for */
435 /*        details). Added checks on input polynomial degree. */
436 
437 /* -    SPICELIB Version 1.1.0, 30-OCT-2006 (BVS) */
438 
439 /*        Removed restriction that the input reference frame should be */
440 /*        inertial by changing the routine that determines the frame ID */
441 /*        from the name from IRFNUM to NAMFRM. */
442 
443 /* -    SPICELIB Version 1.0.1, 19-SEP-2006 (EDW) */
444 
445 /*        Corrected typo in the section name ("Example" to "Examples"). */
446 
447 /* -    SPICELIB Version 1.0.0, 01-AUG-1995 (KSZ) */
448 
449 /* -& */
450 /* $ Index_Entries */
451 
452 /*     write spk type_3 data segment */
453 
454 /* -& */
455 
456 /*     SPICELIB functions */
457 
458 
459 /*     Local Parameters */
460 
461 /*     DTYPE is the SPK data type. */
462 
463 
464 /*     ND is the number of double precision components in an SPK */
465 /*     segment descriptor. SPK uses ND = 2. */
466 
467 
468 /*     NI is the number of integer components in an SPK segment */
469 /*     descriptor. SPK uses NI = 6. */
470 
471 
472 /*     NS is the size of a packed SPK segment descriptor. */
473 
474 
475 /*     SIDLEN is the maximum number of characters allowed in an */
476 /*     SPK segment identifier. */
477 
478 
479 /*     Local variables */
480 
481 
482 /*     Standard SPICE error handling. */
483 
484     if (return_()) {
485 	return 0;
486     }
487     chkin_("SPKW03", (ftnlen)6);
488 
489 /*     The number of sets of coefficients must be positive. */
490 
491     if (*n <= 0) {
492 	setmsg_("The number of sets of coordinatecoefficients is not positiv"
493 		"e. N = #.", (ftnlen)68);
494 	errint_("#", n, (ftnlen)1);
495 	sigerr_("SPICE(NUMCOEFFSNOTPOS)", (ftnlen)22);
496 	chkout_("SPKW03", (ftnlen)6);
497 	return 0;
498     }
499 
500 /*     Make sure that the degree of the interpolating polynomials is */
501 /*     in range. */
502 
503     if (*polydg < 0 || *polydg > 27) {
504 	setmsg_("The interpolating polynomials have degree #; the valid degr"
505 		"ee range is [0, #].", (ftnlen)78);
506 	errint_("#", polydg, (ftnlen)1);
507 	errint_("#", &c__27, (ftnlen)1);
508 	sigerr_("SPICE(INVALIDDEGREE)", (ftnlen)20);
509 	chkout_("SPKW03", (ftnlen)6);
510 	return 0;
511     }
512 
513 /*    The interval length must be positive. */
514 
515     if (*intlen <= 0.) {
516 	setmsg_("The interval length is not positive.N = #", (ftnlen)41);
517 	errdp_("#", intlen, (ftnlen)1);
518 	sigerr_("SPICE(INTLENNOTPOS)", (ftnlen)19);
519 	chkout_("SPKW03", (ftnlen)6);
520 	return 0;
521     }
522 
523 /*     Get the NAIF integer code for the reference frame. */
524 
525     namfrm_(frame, &refcod, frame_len);
526     if (refcod == 0) {
527 	setmsg_("The reference frame # is not supported.", (ftnlen)39);
528 	errch_("#", frame, (ftnlen)1, frame_len);
529 	sigerr_("SPICE(INVALIDREFFRAME)", (ftnlen)22);
530 	chkout_("SPKW03", (ftnlen)6);
531 	return 0;
532     }
533 
534 /*     The segment stop time must be greater than the begin time. */
535 
536     if (*first > *last) {
537 	setmsg_("The segment descriptor start time: # is greater than the se"
538 		"gment end time: #", (ftnlen)76);
539 	etcal_(first, etstr, (ftnlen)40);
540 	errch_("#", etstr, (ftnlen)1, (ftnlen)40);
541 	etcal_(last, netstr, (ftnlen)40);
542 	errch_("#", netstr, (ftnlen)1, (ftnlen)40);
543 	sigerr_("SPICE(BADDESCRTIMES)", (ftnlen)20);
544 	chkout_("SPKW03", (ftnlen)6);
545 	return 0;
546     }
547 
548 /*     Compute the tolerance to use for descriptor time bound checks. */
549 
550 /* Computing MAX */
551     d__1 = abs(*first), d__2 = abs(*last);
552     tol = max(d__1,d__2) * 1e-13;
553     if (*first < *btime - tol) {
554 	setmsg_("The segment descriptor start time # is too much less than t"
555 		"he beginning time of the  segment data # (in seconds past J2"
556 		"000: #). The difference is # seconds; the  tolerance is # se"
557 		"conds.", (ftnlen)185);
558 	etcal_(first, etstr, (ftnlen)40);
559 	errch_("#", etstr, (ftnlen)1, (ftnlen)40);
560 	etcal_(btime, etstr, (ftnlen)40);
561 	errch_("#", etstr, (ftnlen)1, (ftnlen)40);
562 	errdp_("#", first, (ftnlen)1);
563 	d__1 = *btime - *first;
564 	errdp_("#", &d__1, (ftnlen)1);
565 	errdp_("#", &tol, (ftnlen)1);
566 	sigerr_("SPICE(COVERAGEGAP)", (ftnlen)18);
567 	chkout_("SPKW03", (ftnlen)6);
568 	return 0;
569     }
570 
571 /*     The end time of the final record must be greater than or */
572 /*     equal to the end time of the segment. */
573 
574     ltime = *btime + *n * *intlen;
575     if (*last > ltime + tol) {
576 	setmsg_("The segment descriptor end time # is too much greater than "
577 		"the end time of the segment data # (in seconds past J2000: #"
578 		"). The difference is # seconds; the tolerance is # seconds.",
579 		(ftnlen)178);
580 	etcal_(last, etstr, (ftnlen)40);
581 	errch_("#", etstr, (ftnlen)1, (ftnlen)40);
582 	etcal_(&ltime, etstr, (ftnlen)40);
583 	errch_("#", etstr, (ftnlen)1, (ftnlen)40);
584 	errdp_("#", last, (ftnlen)1);
585 	d__1 = *last - ltime;
586 	errdp_("#", &d__1, (ftnlen)1);
587 	errdp_("#", &tol, (ftnlen)1);
588 	sigerr_("SPICE(COVERAGEGAP)", (ftnlen)18);
589 	chkout_("SPKW03", (ftnlen)6);
590 	return 0;
591     }
592 
593 /*     Now check the validity of the segment identifier. */
594 
595     chckid_("SPK segment identifier", &c__40, segid, (ftnlen)22, segid_len);
596     if (failed_()) {
597 	chkout_("SPKW03", (ftnlen)6);
598 	return 0;
599     }
600 
601 /*     Store the start and end times to be associated */
602 /*     with this segment. */
603 
604     dcd[0] = *first;
605     dcd[1] = *last;
606 
607 /*     Create the integer portion of the descriptor. */
608 
609     icd[0] = *body;
610     icd[1] = *center;
611     icd[2] = refcod;
612     icd[3] = 3;
613 
614 /*     Pack the segment descriptor. */
615 
616     dafps_(&c__2, &c__6, dcd, icd, descr);
617 
618 /*     Begin a new segment of SPK type 3 form: */
619 
620 /*        Record 1 */
621 /*        Record 2 */
622 /*        ... */
623 /*        Record N */
624 /*        INIT       ( initial epoch of first record ) */
625 /*        INTLEN     ( length of interval covered by each record ) */
626 /*        RSIZE      ( number of data elements in each record ) */
627 /*        N          ( number of records in segment ) */
628 
629 /*     Each record will have the form: */
630 
631 /*        MID        ( midpoint of time interval ) */
632 /*        RADIUS     ( radius of time interval ) */
633 /*        X coefficients, Y coefficients, Z coefficients */
634 /*        X' coefficients, Y' coefficents, Z' coefficients */
635 
636     dafbna_(handle, descr, segid, segid_len);
637 
638 /*     Calculate the number of Chebyshev coefficients in a record. */
639 
640     ninrec = (*polydg + 1) * 6;
641 
642 /*     Fill segment with N records of data. */
643 
644     i__1 = *n;
645     for (i__ = 1; i__ <= i__1; ++i__) {
646 
647 /*        Calculate the midpoint and radius of the time of each */
648 /*        record, and put that at the beginning of each record. */
649 
650 	radius = *intlen / 2;
651 	mid = *btime + radius + (i__ - 1) * *intlen;
652 	dafada_(&mid, &c__1);
653 	dafada_(&radius, &c__1);
654 
655 /*        Put one set of coefficients into the segment. */
656 
657 	k = (i__ - 1) * ninrec + 1;
658 	dafada_(&cdata[k - 1], &ninrec);
659     }
660 
661 /*     Store the initial epoch of the first record. */
662 
663     dafada_(btime, &c__1);
664 
665 /*     Store the length of interval covered by each record. */
666 
667     dafada_(intlen, &c__1);
668 
669 /*     Store the size of each record (total number of array elements). */
670 
671     rsize = (doublereal) (ninrec + 2);
672     dafada_(&rsize, &c__1);
673 
674 /*     Store the number of records contained in the segment. */
675 
676     numrec = (doublereal) (*n);
677     dafada_(&numrec, &c__1);
678 
679 /*     End this segment. */
680 
681     dafena_();
682     chkout_("SPKW03", (ftnlen)6);
683     return 0;
684 } /* spkw03_ */
685 
686