PageRenderTime 33ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/src/ckw03_c.c

https://github.com/mattbornski/spice
C | 667 lines | 41 code | 19 blank | 607 comment | 0 complexity | 38d53bd40c968c1c69288d06fbe729d3 MD5 | raw file
  1. /*
  2. -Procedure ckw03_c ( C-Kernel, write segment to C-kernel, data type 3 )
  3. -Abstract
  4. Add a type 3 segment to a C-kernel.
  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. DAF
  29. SCLK
  30. -Keywords
  31. POINTING
  32. UTILITY
  33. */
  34. #include "SpiceUsr.h"
  35. #include "SpiceZfc.h"
  36. #include "SpiceZmc.h"
  37. #include "SpiceZim.h"
  38. #undef ckw03_c
  39. void ckw03_c ( SpiceInt handle,
  40. SpiceDouble begtim,
  41. SpiceDouble endtim,
  42. SpiceInt inst,
  43. ConstSpiceChar * ref,
  44. SpiceBoolean avflag,
  45. ConstSpiceChar * segid,
  46. SpiceInt nrec,
  47. ConstSpiceDouble sclkdp [],
  48. ConstSpiceDouble quats [][4],
  49. ConstSpiceDouble avvs [][3],
  50. SpiceInt nints,
  51. ConstSpiceDouble starts [] )
  52. /*
  53. -Brief_I/O
  54. Variable I/O Description
  55. -------- --- --------------------------------------------------
  56. handle I Handle of an open CK file.
  57. begtim I The beginning encoded SCLK of the segment.
  58. endtim I The ending encoded SCLK of the segment.
  59. inst I The NAIF instrument ID code.
  60. ref I The reference frame of the segment.
  61. avflag I True if the segment will contain angular velocity.
  62. segid I Segment identifier.
  63. nrec I Number of pointing records.
  64. sclkdp I Encoded SCLK times.
  65. quats I Quaternions representing instrument pointing.
  66. avvs I Angular velocity vectors.
  67. nints I Number of intervals.
  68. starts I Encoded SCLK interval start times.
  69. -Detailed_Input
  70. handle is the handle of the CK file to which the segment will
  71. be written. The file must have been opened with write
  72. access.
  73. begtim is the beginning encoded SCLK time of the segment. This
  74. value should be less than or equal to the first time in
  75. the segment.
  76. endtim is the encoded SCLK time at which the segment ends.
  77. This value should be greater than or equal to the last
  78. time in the segment.
  79. inst is the NAIF integer ID code for the instrument.
  80. ref is a character string which specifies the
  81. reference frame of the segment. This should be one of
  82. the frames supported by the SPICELIB routine NAMFRM
  83. which is an entry point of FRAMEX.
  84. The rotation matrices represented by the quaternions
  85. that are to be written to the segment transform the
  86. components of vectors from the inertial reference frame
  87. specified by ref to components in the instrument fixed
  88. frame. Also, the components of the angular velocity
  89. vectors to be written to the segment should be given
  90. with respect to ref.
  91. ref should be the name of one of the frames supported
  92. by the SPICELIB routine NAMFRM.
  93. avflag is a boolean flag which indicates whether or not the
  94. segment will contain angular velocity.
  95. segid is the segment identifier. A CK segment identifier may
  96. contain up to 40 characters, excluding the terminating
  97. null.
  98. nrec is the number of pointing instances in the segment.
  99. sclkdp are the encoded spacecraft clock times associated with
  100. each pointing instance. These times must be strictly
  101. increasing.
  102. quats is an array of SPICE-style quaternions representing a
  103. sequence of C-matrices. See the discussion of "Quaternion
  104. Styles" in the Particulars section below.
  105. The C-matrix represented by the ith quaternion in
  106. quats is a rotation matrix that transforms the
  107. components of a vector expressed in the inertial
  108. frame specified by ref to components expressed in
  109. the instrument fixed frame at the time sclkdp[i].
  110. Thus, if a vector V has components x, y, z in the
  111. inertial frame, then V has components x', y', z' in
  112. the instrument fixed frame where:
  113. [ x' ] [ ] [ x ]
  114. | y' | = | cmat | | y |
  115. [ z' ] [ ] [ z ]
  116. avvs are the angular velocity vectors ( optional ).
  117. The ith vector in avvs gives the angular velocity of
  118. the instrument fixed frame at time sclkdp[i]. The
  119. components of the angular velocity vectors should
  120. be given with respect to the inertial reference frame
  121. specified by ref.
  122. The direction of an angular velocity vector gives
  123. the right-handed axis about which the instrument fixed
  124. reference frame is rotating. The magnitude of the
  125. vector is the magnitude of the instantaneous velocity
  126. of the rotation, in radians per second.
  127. If avflag is FALSE then this array is ignored by the
  128. routine; however it still must be supplied as part of
  129. the calling sequence.
  130. nints is the number of intervals that the pointing instances
  131. are partitioned into.
  132. starts are the start times of each of the interpolation
  133. intervals. These times must be strictly increasing
  134. and must coincide with times for which the segment
  135. contains pointing.
  136. -Detailed_Output
  137. None. See Files section.
  138. -Parameters
  139. None.
  140. -Exceptions
  141. 1) If handle is not the handle of a C-kernel opened for writing
  142. the error will be diagnosed by routines called by this
  143. routine.
  144. 2) If segid is more than 40 characters long, the error
  145. SPICE(SEGIDTOOLONG) is signaled.
  146. 3) If segid contains any nonprintable characters, the error
  147. SPICE(NONPRINTABLECHARS) is signaled.
  148. 4) If the first encoded SCLK time is negative then the error
  149. SPICE(INVALIDSCLKTIME) is signaled. If any subsequent times
  150. are negative the error SPICE(TIMESOUTOFORDER) is signaled.
  151. 5) If the encoded SCLK times are not strictly increasing,
  152. the error SPICE(TIMESOUTOFORDER) is signaled.
  153. 6) If begtim is greater than sclkdp[0] or endtim is less than
  154. sclkdp[nrec-1], the error SPICE(INVALIDDESCRTIME) is
  155. signaled.
  156. 7) If the name of the reference frame is not one of those
  157. supported by the SPICELIB routine NAMFRM, the error
  158. SPICE(INVALIDREFFRAME) is signaled.
  159. 8) If nrec, the number of pointing records, is less than or
  160. equal to 0, the error SPICE(INVALIDNUMRECS) is signaled.
  161. 9) If nints, the number of interpolation intervals, is less than
  162. or equal to 0, the error SPICE(INVALIDNUMINTS) is signaled.
  163. 10) If the encoded SCLK interval start times are not strictly
  164. increasing, the error SPICE(TIMESOUTOFORDER) is signaled.
  165. 11) If an interval start time does not coincide with a time for
  166. which there is an actual pointing instance in the segment,
  167. then the error SPICE(INVALIDSTARTTIME) is signaled.
  168. 12) This routine assumes that the rotation between adjacent
  169. quaternions that are stored in the same interval has a
  170. rotation angle of THETA radians, where
  171. 0 < THETA < pi.
  172. _
  173. The routines that evaluate the data in the segment produced
  174. by this routine cannot distinguish between rotations of THETA
  175. radians, where THETA is in the interval [0, pi), and
  176. rotations of
  177. THETA + 2 * k * pi
  178. radians, where k is any integer. These `large' rotations will
  179. yield invalid results when interpolated. You must ensure that
  180. the data stored in the segment will not be subject to this
  181. sort of ambiguity.
  182. 14) If the start time of the first interval and the time of the
  183. first pointing instance are not the same, the error
  184. SPICE(TIMESDONTMATCH) is signaled.
  185. 15) If any quaternion has magnitude zero, the error
  186. SPICE(ZEROQUATERNION) is signaled.
  187. -Files
  188. This routine adds a type 3 segment to a C-kernel. The C-kernel
  189. may be either a new one or an existing one opened for writing.
  190. -Particulars
  191. For a detailed description of a type 3 CK segment please see the
  192. CK Required Reading.
  193. This routine relieves the user from performing the repetitive
  194. calls to the DAF routines necessary to construct a CK segment.
  195. Quaternion Styles
  196. -----------------
  197. There are different "styles" of quaternions used in
  198. science and engineering applications. Quaternion styles
  199. are characterized by
  200. - The order of quaternion elements
  201. - The quaternion multiplication formula
  202. - The convention for associating quaternions
  203. with rotation matrices
  204. Two of the commonly used styles are
  205. - "SPICE"
  206. > Invented by Sir William Rowan Hamilton
  207. > Frequently used in mathematics and physics textbooks
  208. - "Engineering"
  209. > Widely used in aerospace engineering applications
  210. CSPICE function interfaces ALWAYS use SPICE quaternions.
  211. Quaternions of any other style must be converted to SPICE
  212. quaternions before they are passed to CSPICE functions.
  213. Relationship between SPICE and Engineering Quaternions
  214. ------------------------------------------------------
  215. Let M be a rotation matrix such that for any vector V,
  216. M*V
  217. is the result of rotating V by theta radians in the
  218. counterclockwise direction about unit rotation axis vector A.
  219. Then the SPICE quaternions representing M are
  220. (+/-) ( cos(theta/2),
  221. sin(theta/2) A(1),
  222. sin(theta/2) A(2),
  223. sin(theta/2) A(3) )
  224. while the engineering quaternions representing M are
  225. (+/-) ( -sin(theta/2) A(1),
  226. -sin(theta/2) A(2),
  227. -sin(theta/2) A(3),
  228. cos(theta/2) )
  229. For both styles of quaternions, if a quaternion q represents
  230. a rotation matrix M, then -q represents M as well.
  231. Given an engineering quaternion
  232. QENG = ( q0, q1, q2, q3 )
  233. the equivalent SPICE quaternion is
  234. QSPICE = ( q3, -q0, -q1, -q2 )
  235. Associating SPICE Quaternions with Rotation Matrices
  236. ----------------------------------------------------
  237. Let FROM and TO be two right-handed reference frames, for
  238. example, an inertial frame and a spacecraft-fixed frame. Let the
  239. symbols
  240. V , V
  241. FROM TO
  242. denote, respectively, an arbitrary vector expressed relative to
  243. the FROM and TO frames. Let M denote the transformation matrix
  244. that transforms vectors from frame FROM to frame TO; then
  245. V = M * V
  246. TO FROM
  247. where the expression on the right hand side represents left
  248. multiplication of the vector by the matrix.
  249. Then if the unit-length SPICE quaternion q represents M, where
  250. q = (q0, q1, q2, q3)
  251. the elements of M are derived from the elements of q as follows:
  252. +- -+
  253. | 2 2 |
  254. | 1 - 2*( q2 + q3 ) 2*(q1*q2 - q0*q3) 2*(q1*q3 + q0*q2) |
  255. | |
  256. | |
  257. | 2 2 |
  258. M = | 2*(q1*q2 + q0*q3) 1 - 2*( q1 + q3 ) 2*(q2*q3 - q0*q1) |
  259. | |
  260. | |
  261. | 2 2 |
  262. | 2*(q1*q3 - q0*q2) 2*(q2*q3 + q0*q1) 1 - 2*( q1 + q2 ) |
  263. | |
  264. +- -+
  265. Note that substituting the elements of -q for those of q in the
  266. right hand side leaves each element of M unchanged; this shows
  267. that if a quaternion q represents a matrix M, then so does the
  268. quaternion -q.
  269. To map the rotation matrix M to a unit quaternion, we start by
  270. decomposing the rotation matrix as a sum of symmetric
  271. and skew-symmetric parts:
  272. 2
  273. M = [ I + (1-cos(theta)) OMEGA ] + [ sin(theta) OMEGA ]
  274. symmetric skew-symmetric
  275. OMEGA is a skew-symmetric matrix of the form
  276. +- -+
  277. | 0 -n3 n2 |
  278. | |
  279. OMEGA = | n3 0 -n1 |
  280. | |
  281. | -n2 n1 0 |
  282. +- -+
  283. The vector N of matrix entries (n1, n2, n3) is the rotation axis
  284. of M and theta is M's rotation angle. Note that N and theta
  285. are not unique.
  286. Let
  287. C = cos(theta/2)
  288. S = sin(theta/2)
  289. Then the unit quaternions Q corresponding to M are
  290. Q = +/- ( C, S*n1, S*n2, S*n3 )
  291. The mappings between quaternions and the corresponding rotations
  292. are carried out by the CSPICE routines
  293. q2m_c {quaternion to matrix}
  294. m2q_c {matrix to quaternion}
  295. m2q_c always returns a quaternion with scalar part greater than
  296. or equal to zero.
  297. SPICE Quaternion Multiplication Formula
  298. ---------------------------------------
  299. Given a SPICE quaternion
  300. Q = ( q0, q1, q2, q3 )
  301. corresponding to rotation axis A and angle theta as above, we can
  302. represent Q using "scalar + vector" notation as follows:
  303. s = q0 = cos(theta/2)
  304. v = ( q1, q2, q3 ) = sin(theta/2) * A
  305. Q = s + v
  306. Let Q1 and Q2 be SPICE quaternions with respective scalar
  307. and vector parts s1, s2 and v1, v2:
  308. Q1 = s1 + v1
  309. Q2 = s2 + v2
  310. We represent the dot product of v1 and v2 by
  311. <v1, v2>
  312. and the cross product of v1 and v2 by
  313. v1 x v2
  314. Then the SPICE quaternion product is
  315. Q1*Q2 = s1*s2 - <v1,v2> + s1*v2 + s2*v1 + (v1 x v2)
  316. If Q1 and Q2 represent the rotation matrices M1 and M2
  317. respectively, then the quaternion product
  318. Q1*Q2
  319. represents the matrix product
  320. M1*M2
  321. -Examples
  322. This example code fragment writes a type 3 C-kernel segment
  323. for the Mars Global Surveyor spacecraft bus to a previously opened CK
  324. file attached to HANDLE.
  325. /.
  326. Include CSPICE interface definitions.
  327. ./
  328. #include "SpiceUsr.h"
  329. .
  330. .
  331. .
  332. /.
  333. Assume arrays of quaternions, angular velocities, and the
  334. associated SCLK times are produced elsewhere. The software
  335. that calls ckw03_c must then decide how to partition these
  336. pointing instances into intervals over which linear
  337. interpolation between adjacent points is valid.
  338. ./
  339. .
  340. .
  341. .
  342. /.
  343. The subroutine ckw03_c needs the following items for the
  344. segment descriptor:
  345. 1) SCLK limits of the segment.
  346. 2) Instrument code.
  347. 3) Reference frame.
  348. 4) The angular velocity flag.
  349. ./
  350. begtim = sclk [ 0 ];
  351. endtim = sclk [ nrec-1 ];
  352. inst = -94000;
  353. ref = "j2000";
  354. avflag = SPICETRUE;
  355. segid = "MGS spacecraft bus - data type 3";
  356. /.
  357. Write the segment.
  358. ./
  359. ckw03_c ( handle, begtim, endtim, inst, ref, avflag,
  360. segid, nrec, sclkdp, quats, avvs, nints,
  361. starts );
  362. .
  363. .
  364. .
  365. /.
  366. After all segments are written, close the C-kernel.
  367. ./
  368. ckcls_c ( handle );
  369. -Restrictions
  370. 1) The creator of the segment is given the responsibility for
  371. determining whether it is reasonable to interpolate between
  372. two given pointing values.
  373. 2) This routine assumes that the rotation between adjacent
  374. quaternions that are stored in the same interval has a
  375. rotation angle of THETA radians, where
  376. 0 < THETA < pi.
  377. _
  378. The routines that evaluate the data in the segment produced
  379. by this routine cannot distinguish between rotations of THETA
  380. radians, where THETA is in the interval [0, pi), and
  381. rotations of
  382. THETA + 2 * k * pi
  383. radians, where k is any integer. These `large' rotations will
  384. yield invalid results when interpolated. You must ensure that
  385. the data stored in the segment will not be subject to this
  386. sort of ambiguity.
  387. 3) All pointing instances in the segment must belong to one and
  388. only one of the intervals.
  389. -Literature_References
  390. None.
  391. -Author_and_Institution
  392. K.R. Gehringer (JPL)
  393. N.J. Bachman (JPL)
  394. J.M. Lynch (JPL)
  395. B.V. Semenov (JPL)
  396. E.D. Wright (JPL)
  397. -Version
  398. -CSPICE Version 2.0.0, 01-JUN-2010 (NJB)
  399. The check for non-unit quaternions has been replaced
  400. with a check for zero-length quaternions. (The
  401. implementation of the check is located in ckw03_.)
  402. -CSPICE Version 1.4.2, 27-FEB-2008 (NJB)
  403. Updated header; added information about SPICE
  404. quaternion conventions.
  405. -CSPICE Version 1.4.1, 27-SEP-2005 (BVS)
  406. Added an item for SPICE(TIMESDONTMATCH) exception to the
  407. Exceptions section of the header.
  408. -CSPICE Version 1.3.1, 07-JAN-2004 (EDW)
  409. Trivial typo correction in index entries section.
  410. -CSPICE Version 1.3.0, 28-AUG-2001 (NJB)
  411. Changed prototype: inputs sclkdp, quats, avvs, and starts
  412. are now const-qualified. Implemented interface macros for
  413. casting these inputs to const.
  414. -CSPICE Version 1.2.0, 02-SEP-1999 (NJB)
  415. Local type logical variable now used for angular velocity
  416. flag used in interface of ckw03_.
  417. -CSPICE Version 1.1.0, 08-FEB-1998 (NJB)
  418. References to C2F_CreateStr_Sig were removed; code was
  419. cleaned up accordingly. String checks are now done using
  420. the macro CHKFSTR.
  421. -CSPICE Version 1.0.0, 25-OCT-1997 (NJB)
  422. Based on SPICELIB Version 2.0.0, 28-DEC-1993 (WLT)
  423. -Index_Entries
  424. write ck type_3 pointing data segment
  425. -&
  426. */
  427. { /* Begin ckw03_c */
  428. /*
  429. Local variables
  430. */
  431. logical avf;
  432. /*
  433. Participate in error handling.
  434. */
  435. chkin_c ( "ckw03_c" );
  436. /*
  437. Check the input strings to make sure the pointers
  438. are non-null and the string lengths are non-zero.
  439. */
  440. CHKFSTR ( CHK_STANDARD, "ckw03_c", ref );
  441. CHKFSTR ( CHK_STANDARD, "ckw03_c", segid );
  442. /*
  443. Get a type logical copy of the a.v. flag.
  444. */
  445. avf = avflag;
  446. /*
  447. Write the segment. Note that the quaternion and angular velocity
  448. arrays DO NOT require transposition!
  449. */
  450. ckw03_( ( integer * ) &handle,
  451. ( doublereal * ) &begtim,
  452. ( doublereal * ) &endtim,
  453. ( integer * ) &inst,
  454. ( char * ) ref,
  455. ( logical * ) &avf,
  456. ( char * ) segid,
  457. ( integer * ) &nrec,
  458. ( doublereal * ) sclkdp,
  459. ( doublereal * ) quats,
  460. ( doublereal * ) avvs,
  461. ( integer * ) &nints,
  462. ( doublereal * ) starts,
  463. ( ftnlen ) strlen(ref),
  464. ( ftnlen ) strlen(segid) );
  465. chkout_c ( "ckw03_c" );
  466. } /* End ckw03_c */