PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ckw05_c.c

https://github.com/mattbornski/spice
C | 701 lines | 51 code | 23 blank | 627 comment | 1 complexity | a51f5852ad3bbfe81d1237dcea4127f7 MD5 | raw file
  1. /*
  2. -Procedure ckw05_c ( Write CK segment, type 5 )
  3. -Abstract
  4. Write a type 5 segment to a CK file.
  5. -Disclaimer
  6. THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE
  7. CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S.
  8. GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE
  9. ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE
  10. PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS"
  11. TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY
  12. WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A
  13. PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC
  14. SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE
  15. SOFTWARE AND RELATED MATERIALS, HOWEVER USED.
  16. IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA
  17. BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT
  18. LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS,
  20. REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE
  21. REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY.
  22. RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF
  23. THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY
  24. CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE
  25. ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE.
  26. -Required_Reading
  27. CK
  28. NAIF_IDS
  29. ROTATION
  30. TIME
  31. -Keywords
  32. POINTING
  33. FILES
  34. */
  35. #include "SpiceUsr.h"
  36. #include "SpiceZfc.h"
  37. #include "SpiceZst.h"
  38. #include "SpiceZmc.h"
  39. #undef ckw05_c
  40. void ckw05_c ( SpiceInt handle,
  41. SpiceCK05Subtype subtyp,
  42. SpiceInt degree,
  43. SpiceDouble begtim,
  44. SpiceDouble endtim,
  45. SpiceInt inst,
  46. ConstSpiceChar * ref,
  47. SpiceBoolean avflag,
  48. ConstSpiceChar * segid,
  49. SpiceInt n,
  50. ConstSpiceDouble sclkdp [],
  51. const void * packts,
  52. SpiceDouble rate,
  53. SpiceInt nints,
  54. ConstSpiceDouble starts [] )
  55. /*
  56. -Brief_I/O
  57. Variable I/O Description
  58. -------- --- --------------------------------------------------
  59. handle I Handle of an open CK file.
  60. subtyp I CK type 5 subtype code.
  61. degree I Degree of interpolating polynomials.
  62. begtim I The beginning encoded SCLK of the segment.
  63. endtim I The ending encoded SCLK of the segment.
  64. inst I The NAIF instrument ID code.
  65. ref I The reference frame of the segment.
  66. avflag I True if the segment will contain angular velocity.
  67. segid I Segment identifier.
  68. n I Number of packets.
  69. sclkdp I Encoded SCLK times.
  70. packts I Array of packets.
  71. rate I Nominal SCLK rate in seconds per tick.
  72. nints I Number of intervals.
  73. starts I Encoded SCLK interval start times.
  74. MAXDEG P Maximum allowed degree of interpolating polynomial.
  75. -Detailed_Input
  76. handle is the handle of the CK file to which the segment will be
  77. written. The file must have been opened with write
  78. access.
  79. subtyp is an integer code indicating the subtype of the
  80. segment to be created.
  81. degree is the degree of the polynomials used to interpolate the
  82. quaternions contained in the input packets. All
  83. components of the quaternions are interpolated by
  84. polynomials of fixed degree.
  85. begtim,
  86. endtim are the beginning and ending encoded SCLK times
  87. for which the segment provides pointing information.
  88. begtim must be less than or equal to endtim, and at least
  89. one data packet must have a time tag t such that
  90. begtim < t < endtim
  91. - -
  92. inst is the NAIF integer ID code for the instrument.
  93. ref is a character string which specifies the
  94. reference frame of the segment. This should be one of
  95. the frames supported by the SPICELIB routine NAMFRM
  96. which is an entry point of FRAMEX.
  97. The rotation matrices represented by the quaternions
  98. that are to be written to the segment transform the
  99. components of vectors from the inertial reference frame
  100. specified by ref to components in the instrument fixed
  101. frame. Also, the components of the angular velocity
  102. vectors to be written to the segment should be given
  103. with respect to ref.
  104. ref should be the name of one of the frames supported
  105. by the SPICELIB routine NAMFRM.
  106. avflag is a boolean flag which indicates whether or not the
  107. segment will contain angular velocity.
  108. segid is the segment identifier. A CK segment identifier may
  109. contain up to 40 characters, excluding the terminating
  110. null.
  111. packts contains a time-ordered array of data packets
  112. representing the orientation of inst relative to the
  113. frame ref. Each packet contains a SPICE-style quaternion
  114. and optionally, depending on the segment subtype,
  115. attitude derivative data, from which a C-matrix and an
  116. angular velocity vector may be derived.
  117. See the discussion of "Quaternion Styles" in the
  118. Particulars section below.
  119. The C-matrix represented by the Ith data packet is a
  120. rotation matrix that transforms the components of a
  121. vector expressed in the base frame specified by ref to
  122. components expressed in the instrument fixed frame at the
  123. time sclkdp(I).
  124. Thus, if a vector v has components x, y, z in the base
  125. frame, then v has components x', y', z' in the instrument
  126. fixed frame where:
  127. [ x' ] [ ] [ x ]
  128. | y' | = | cmat | | y |
  129. [ z' ] [ ] [ z ]
  130. The attitude derivative information in packts[i] gives
  131. the angular velocity of the instrument fixed frame at
  132. time sclkdp[i] with respect to the reference frame
  133. specified by ref.
  134. The direction of an angular velocity vector gives the
  135. right-handed axis about which the instrument fixed
  136. reference frame is rotating. The magnitude of the vector
  137. is the magnitude of the instantaneous velocity of the
  138. rotation, in radians per second.
  139. Packet contents and the corresponding interpolation
  140. methods depend on the segment subtype, and are as
  141. follows:
  142. Subtype 0: Hermite interpolation, 8-element packets.
  143. Quaternion and quaternion derivatives
  144. only, no angular velocity vector provided.
  145. Quaternion elements are listed first,
  146. followed by derivatives. Angular velocity
  147. is derived from the quaternions and
  148. quaternion derivatives.
  149. Subtype 1: Lagrange interpolation, 4-element packets.
  150. Quaternion only. Angular velocity is
  151. derived by differentiating the
  152. interpolating polynomials.
  153. Subtype 2: Hermite interpolation, 14-element packets.
  154. Quaternion and angular angular velocity
  155. vector, as well as derivatives of each,
  156. are provided. The quaternion comes first,
  157. then quaternion derivatives, then angular
  158. velocity and its derivatives.
  159. Subtype 3: Lagrange interpolation, 7-element packets.
  160. Quaternion and angular velocity vector
  161. provided. The quaternion comes first.
  162. Angular velocity is always specified relative to the base
  163. frame.
  164. rate is the nominal rate of the spacecraft clock associated
  165. with inst. Units are seconds per tick. rate is used to
  166. scale angular velocity to radians/second.
  167. nints is the number of intervals that the pointing instances
  168. are partitioned into.
  169. starts are the start times of each of the interpolation
  170. intervals. These times must be strictly increasing and
  171. must coincide with times for which the segment contains
  172. pointing.
  173. -Detailed_Output
  174. None. See Files section.
  175. -Parameters
  176. MAXDEG is the maximum allowed degree of the interpolating
  177. polynomial. If the value of MAXDEG is increased, the
  178. CSPICE routine ckpfs_ must be changed accordingly. In
  179. particular, the size of the record passed to ckrNN_ and
  180. ckeNN_ must be increased, and comments describing the
  181. record size must be changed.
  182. -Exceptions
  183. If any of the following exceptions occur, this routine will return
  184. without creating a new segment.
  185. 1) If handle is not the handle of a C-kernel opened for writing
  186. the error will be diagnosed by routines called by this
  187. routine.
  188. 2) If the last non-blank character of segid occurs past index 40,
  189. the error SPICE(SEGIDTOOLONG) is signaled.
  190. 3) If segid contains any nonprintable characters, the error
  191. SPICE(NONPRINTABLECHARS) is signaled.
  192. 4) If the first encoded SCLK time is negative then the error
  193. SPICE(INVALIDSCLKTIME) is signaled. If any subsequent times
  194. are negative the error will be detected in exception (5).
  195. 5) If the encoded SCLK times are not strictly increasing,
  196. the error SPICE(TIMESOUTOFORDER) is signaled.
  197. 6) If the name of the reference frame is not one of those
  198. supported by the routine framex_, the error
  199. SPICE(INVALIDREFFRAME) is signaled.
  200. 7) If the number of packets n is not at least 1, the error
  201. SPICE(TOOFEWPACKETS) will be signaled.
  202. 8) If nints, the number of interpolation intervals, is less than
  203. or equal to 0, the error SPICE(INVALIDNUMINTS) is signaled.
  204. 9) If the encoded SCLK interval start times are not strictly
  205. increasing, the error SPICE(TIMESOUTOFORDER) is signaled.
  206. 10) If an interval start time does not coincide with a time for
  207. which there is an actual pointing instance in the segment,
  208. then the error SPICE(INVALIDSTARTTIME) is signaled.
  209. 11) This routine assumes that the rotation between adjacent
  210. quaternions that are stored in the same interval has a
  211. rotation angle of theta radians, where
  212. 0 < theta < pi.
  213. _
  214. The routines that evaluate the data in the segment produced
  215. by this routine cannot distinguish between rotations of theta
  216. radians, where theta is in the interval [0, pi), and
  217. rotations of
  218. theta + 2 * k * pi
  219. radians, where k is any integer. These "large" rotations will
  220. yield invalid results when interpolated. You must ensure that
  221. the data stored in the segment will not be subject to this
  222. sort of ambiguity.
  223. 12) If any quaternion is the zero vector, the error
  224. SPICE(ZEROQUATERNION) is signaled.
  225. 13) If the interpolation window size implied by degree is not
  226. even, the error SPICE(INVALIDDEGREE) is signaled. The window
  227. size is degree+1 for Lagrange subtypes and is (degree+1)/2
  228. for Hermite subtypes.
  229. 14) If an unrecognized subtype code is supplied, the error
  230. SPICE(NOTSUPPORTED) is signaled.
  231. 15) If degree is not at least 1 or is greater than MAXDEG, the
  232. error SPICE(INVALIDDEGREE) is signaled.
  233. 16) If the segment descriptor bounds are out of order, the
  234. error SPICE(BADDESCRTIMES) is signaled.
  235. 17) If there is no element of SCLKDP that lies between BEGTIM and
  236. ENDTIM inclusive, the error SPICE(EMPTYSEGMENT) is signaled.
  237. 18) If RATE is zero, the error SPICE(INVALIDVALUE) is signaled.
  238. 18) If either the input frame or segment ID have null string
  239. pointers, the error SPICE(NULLPOINTER) is signaled.
  240. 19) If either the input frame or segment ID are zero-length
  241. strings, the error SPICE(EMPTYSTRING) is signaled.
  242. -Files
  243. A new type 5 CK segment is written to the CK file attached
  244. to handle.
  245. -Particulars
  246. This routine writes a CK type 5 data segment to the open CK
  247. file according to the format described in the type 5 section of
  248. the CK Required Reading. The CK file must have been opened with
  249. write access.
  250. Quaternion Styles
  251. -----------------
  252. There are different "styles" of quaternions used in
  253. science and engineering applications. Quaternion styles
  254. are characterized by
  255. - The order of quaternion elements
  256. - The quaternion multiplication formula
  257. - The convention for associating quaternions
  258. with rotation matrices
  259. Two of the commonly used styles are
  260. - "SPICE"
  261. > Invented by Sir William Rowan Hamilton
  262. > Frequently used in mathematics and physics textbooks
  263. - "Engineering"
  264. > Widely used in aerospace engineering applications
  265. CSPICE function interfaces ALWAYS use SPICE quaternions.
  266. Quaternions of any other style must be converted to SPICE
  267. quaternions before they are passed to CSPICE functions.
  268. Relationship between SPICE and Engineering Quaternions
  269. ------------------------------------------------------
  270. Let M be a rotation matrix such that for any vector V,
  271. M*V
  272. is the result of rotating V by theta radians in the
  273. counterclockwise direction about unit rotation axis vector A.
  274. Then the SPICE quaternions representing M are
  275. (+/-) ( cos(theta/2),
  276. sin(theta/2) A(1),
  277. sin(theta/2) A(2),
  278. sin(theta/2) A(3) )
  279. while the engineering quaternions representing M are
  280. (+/-) ( -sin(theta/2) A(1),
  281. -sin(theta/2) A(2),
  282. -sin(theta/2) A(3),
  283. cos(theta/2) )
  284. For both styles of quaternions, if a quaternion q represents
  285. a rotation matrix M, then -q represents M as well.
  286. Given an engineering quaternion
  287. QENG = ( q0, q1, q2, q3 )
  288. the equivalent SPICE quaternion is
  289. QSPICE = ( q3, -q0, -q1, -q2 )
  290. Associating SPICE Quaternions with Rotation Matrices
  291. ----------------------------------------------------
  292. Let FROM and TO be two right-handed reference frames, for
  293. example, an inertial frame and a spacecraft-fixed frame. Let the
  294. symbols
  295. V , V
  296. FROM TO
  297. denote, respectively, an arbitrary vector expressed relative to
  298. the FROM and TO frames. Let M denote the transformation matrix
  299. that transforms vectors from frame FROM to frame TO; then
  300. V = M * V
  301. TO FROM
  302. where the expression on the right hand side represents left
  303. multiplication of the vector by the matrix.
  304. Then if the unit-length SPICE quaternion q represents M, where
  305. q = (q0, q1, q2, q3)
  306. the elements of M are derived from the elements of q as follows:
  307. +- -+
  308. | 2 2 |
  309. | 1 - 2*( q2 + q3 ) 2*(q1*q2 - q0*q3) 2*(q1*q3 + q0*q2) |
  310. | |
  311. | |
  312. | 2 2 |
  313. M = | 2*(q1*q2 + q0*q3) 1 - 2*( q1 + q3 ) 2*(q2*q3 - q0*q1) |
  314. | |
  315. | |
  316. | 2 2 |
  317. | 2*(q1*q3 - q0*q2) 2*(q2*q3 + q0*q1) 1 - 2*( q1 + q2 ) |
  318. | |
  319. +- -+
  320. Note that substituting the elements of -q for those of q in the
  321. right hand side leaves each element of M unchanged; this shows
  322. that if a quaternion q represents a matrix M, then so does the
  323. quaternion -q.
  324. To map the rotation matrix M to a unit quaternion, we start by
  325. decomposing the rotation matrix as a sum of symmetric
  326. and skew-symmetric parts:
  327. 2
  328. M = [ I + (1-cos(theta)) OMEGA ] + [ sin(theta) OMEGA ]
  329. symmetric skew-symmetric
  330. OMEGA is a skew-symmetric matrix of the form
  331. +- -+
  332. | 0 -n3 n2 |
  333. | |
  334. OMEGA = | n3 0 -n1 |
  335. | |
  336. | -n2 n1 0 |
  337. +- -+
  338. The vector N of matrix entries (n1, n2, n3) is the rotation axis
  339. of M and theta is M's rotation angle. Note that N and theta
  340. are not unique.
  341. Let
  342. C = cos(theta/2)
  343. S = sin(theta/2)
  344. Then the unit quaternions Q corresponding to M are
  345. Q = +/- ( C, S*n1, S*n2, S*n3 )
  346. The mappings between quaternions and the corresponding rotations
  347. are carried out by the CSPICE routines
  348. q2m_c {quaternion to matrix}
  349. m2q_c {matrix to quaternion}
  350. m2q_c always returns a quaternion with scalar part greater than
  351. or equal to zero.
  352. SPICE Quaternion Multiplication Formula
  353. ---------------------------------------
  354. Given a SPICE quaternion
  355. Q = ( q0, q1, q2, q3 )
  356. corresponding to rotation axis A and angle theta as above, we can
  357. represent Q using "scalar + vector" notation as follows:
  358. s = q0 = cos(theta/2)
  359. v = ( q1, q2, q3 ) = sin(theta/2) * A
  360. Q = s + v
  361. Let Q1 and Q2 be SPICE quaternions with respective scalar
  362. and vector parts s1, s2 and v1, v2:
  363. Q1 = s1 + v1
  364. Q2 = s2 + v2
  365. We represent the dot product of v1 and v2 by
  366. <v1, v2>
  367. and the cross product of v1 and v2 by
  368. v1 x v2
  369. Then the SPICE quaternion product is
  370. Q1*Q2 = s1*s2 - <v1,v2> + s1*v2 + s2*v1 + (v1 x v2)
  371. If Q1 and Q2 represent the rotation matrices M1 and M2
  372. respectively, then the quaternion product
  373. Q1*Q2
  374. represents the matrix product
  375. M1*M2
  376. -Examples
  377. This example code fragment writes a type 5 C-kernel segment
  378. for the Mars Express spacecraft bus to a previously opened CK
  379. file attached to handle.
  380. /.
  381. Include CSPICE interface definitions.
  382. ./
  383. #include "SpiceUsr.h"
  384. .
  385. .
  386. .
  387. /.
  388. Assume arrays of quaternions, angular velocities, and the
  389. associated SCLK times are produced elsewhere. The software
  390. that calls ckw05_c must then decide how to partition these
  391. pointing instances into intervals over which linear
  392. interpolation between adjacent points is valid.
  393. ./
  394. .
  395. .
  396. .
  397. /.
  398. The subroutine ckw05_c needs the following items for the
  399. segment descriptor:
  400. 1) SCLK limits of the segment.
  401. 2) Instrument code.
  402. 3) Reference frame.
  403. 4) The angular velocity flag.
  404. ./
  405. begtim = sclk [ 0 ];
  406. endtim = sclk [ nrec-1 ];
  407. inst = -41000;
  408. ref = "J2000";
  409. avflag = SPICETRUE;
  410. segid = "MEX spacecraft bus - data type 5";
  411. /.
  412. Write the segment.
  413. ./
  414. ckw05_c ( handle, subtyp, degree, begtim, endtim, inst,
  415. ref, avflag, segid, n, sclkdp, packts,
  416. rate, nints, starts );
  417. .
  418. .
  419. .
  420. /.
  421. After all segments are written, close the C-kernel.
  422. ./
  423. ckcls_c ( handle );
  424. -Restrictions
  425. None.
  426. -Literature_References
  427. None.
  428. -Author_and_Institution
  429. N.J. Bachman (JPL)
  430. W.L. Taber (JPL)
  431. K.R. Gehringer (JPL)
  432. J.M. Lynch (JPL)
  433. -Version
  434. -CSPICE Version 2.0.0, 01-JUN-2010 (NJB)
  435. The check for non-unit quaternions has been replaced
  436. with a check for zero-length quaternions. (The
  437. implementation of the check is located in ckw05_.)
  438. -CSPICE Version 1.0.2, 27-FEB-2008 (NJB)
  439. Updated header; added information about SPICE
  440. quaternion conventions.
  441. -CSPICE Version 1.0.1, 07-JAN-2005 (NJB)
  442. Description in Detailed_Input header section of
  443. constraints on BEGTIM and ENDTIM was corrected
  444. -CSPICE Version 1.0.0, 30-AUG-2002 (NJB) (WLT) (KRG) (JML)
  445. -Index_Entries
  446. write ck type_5 data segment
  447. -&
  448. */
  449. { /* Begin ckw05_c */
  450. /*
  451. Local variables
  452. */
  453. logical avf;
  454. SpiceInt locSubtype;
  455. /*
  456. Participate in error tracingx.
  457. */
  458. if ( return_c() )
  459. {
  460. return;
  461. }
  462. chkin_c ( "ckw05_c" );
  463. /*
  464. Check the input strings to make sure the pointers
  465. are non-null and the string lengths are non-zero.
  466. */
  467. CHKFSTR ( CHK_STANDARD, "ckw05_c", ref );
  468. CHKFSTR ( CHK_STANDARD, "ckw05_c", segid );
  469. /*
  470. Get a type logical copy of the a.v. flag. Get a type SpiceInt
  471. copy of the CK type 5 subtype.
  472. */
  473. avf = (logical) avflag;
  474. locSubtype = (SpiceInt) subtyp;
  475. /*
  476. Write the segment. Note that the packet array
  477. DOES NOT require transposition!
  478. */
  479. ckw05_( ( integer * ) &handle,
  480. ( integer * ) &locSubtype,
  481. ( integer * ) &degree,
  482. ( doublereal * ) &begtim,
  483. ( doublereal * ) &endtim,
  484. ( integer * ) &inst,
  485. ( char * ) ref,
  486. ( logical * ) &avf,
  487. ( char * ) segid,
  488. ( integer * ) &n,
  489. ( doublereal * ) sclkdp,
  490. ( doublereal * ) packts,
  491. ( doublereal * ) &rate,
  492. ( integer * ) &nints,
  493. ( doublereal * ) starts,
  494. ( ftnlen ) strlen(ref),
  495. ( ftnlen ) strlen(segid) );
  496. chkout_c ( "ckw05_c" );
  497. } /* End ckw05_c */