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

/src/ckw02_c.c

https://github.com/mattbornski/spice
C | 544 lines | 37 code | 13 blank | 494 comment | 0 complexity | a6f3e5e6db6f73455cebab18099bdb0b MD5 | raw file
  1. /*
  2. -Procedure ckw02_c ( C-Kernel, write segment to C-kernel, data type 2 )
  3. -Abstract
  4. Write a type 2 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 ckw02_c
  39. void ckw02_c ( SpiceInt handle,
  40. SpiceDouble begtim,
  41. SpiceDouble endtim,
  42. SpiceInt inst,
  43. ConstSpiceChar * ref,
  44. ConstSpiceChar * segid,
  45. SpiceInt nrec,
  46. ConstSpiceDouble start [],
  47. ConstSpiceDouble stop [],
  48. ConstSpiceDouble quats [][4],
  49. ConstSpiceDouble avvs [][3],
  50. ConstSpiceDouble rates [] )
  51. /*
  52. -Brief_I/O
  53. Variable I/O Description
  54. -------- --- --------------------------------------------------
  55. handle I Handle of an open CK file.
  56. begtim I The beginning encoded SCLK of the segment.
  57. endtim I The ending encoded SCLK of the segment.
  58. inst I The NAIF instrument ID code.
  59. ref I The reference frame of the segment.
  60. segid I Segment identifier.
  61. nrec I Number of pointing records.
  62. start I Encoded SCLK interval start times.
  63. stop I Encoded SCLK interval stop times.
  64. quats I Quaternions representing instrument pointing.
  65. avvs I Angular velocity vectors.
  66. rates I Number of seconds per tick for each interval.
  67. -Detailed_Input
  68. handle is the handle of the CK file to which the segment will
  69. be written. The file must have been opened with write
  70. access.
  71. begtim is the beginning encoded SCLK time of the segment. This
  72. value should be less than or equal to the first START
  73. time in the segment.
  74. endtim is the encoded SCLK time at which the segment ends.
  75. This value should be greater than or equal to the last
  76. STOP time in the segment.
  77. inst is the NAIF integer ID code for the instrument.
  78. ref is a character string that specifies the
  79. reference frame of the segment. This should be one of
  80. the frames supported by the SPICELIB routine NAMFRM
  81. which is an entry point of FRAMEX.
  82. segid is the segment identifier. A CK segment identifier may
  83. contain up to 40 characters.
  84. nrec is the number of pointing intervals that will be
  85. written to the segment.
  86. start are the start times of each interval in encoded
  87. spacecraft clock. These times must be strictly
  88. increasing.
  89. stop are the stop times of each interval in encoded
  90. spacecraft clock. These times must be greater than
  91. the START times that they correspond to but less
  92. than or equal to the START time of the next interval.
  93. quats are the quaternions representing the C-matrices
  94. associated with the start times of each interval. See the
  95. discussion of "Quaternion Styles" in the Particulars
  96. section below.
  97. AVVS are the angular velocity vectors for each interval.
  98. RATES are the number of seconds per encoded spacecraft clock
  99. tick for each interval.
  100. In most applications this value will be the same for
  101. each interval within a segment. For example, when
  102. constructing a predict C-kernel for Mars Observer, the
  103. rate would be 1/256 for each interval since this is
  104. the smallest time unit expressible by the MO clock. The
  105. nominal seconds per tick rates for Galileo and Voyager
  106. are 1/120 and 0.06 respectively.
  107. -Detailed_Output
  108. None. See Files section.
  109. -Parameters
  110. None.
  111. -Exceptions
  112. 1) If handle is not the handle of a C-kernel opened for writing
  113. the error will be diagnosed by routines called by this
  114. routine.
  115. 2) If segid is more than 40 characters long, the error
  116. SPICE(SEGIDTOOLONG) is signaled.
  117. 3) If segid contains any nonprintable characters, the error
  118. SPICE(NONPRINTABLECHARS) is signaled.
  119. 4) If the first START time is negative, the error
  120. SPICE(INVALIDSCLKTIME) is signaled. If any of the subsequent
  121. START times are negative the error SPICE(TIMESOUTOFORDER)
  122. will be signaled.
  123. 5) If any of the STOP times are negative, the error
  124. SPICE(DEGENERATEINTERVAL) is signaled.
  125. 6) If the STOP time of any of the intervals is less than or equal
  126. to the START time, the error SPICE(DEGENERATEINTERVAL) is
  127. signaled.
  128. 7) If the START times are not strictly increasing, the
  129. error SPICE(TIMESOUTOFORDER) is signaled.
  130. 8) If the STOP time of one interval is greater than the START
  131. time of the next interval, the error SPICE(BADSTOPTIME)
  132. is signaled.
  133. 9) If begtim is greater than START[0] or endtim is less than
  134. STOP[NREC-1], the error SPICE(INVALIDDESCRTIME) is
  135. signaled.
  136. 10) If the name of the reference frame is not one of those
  137. supported by the routine NAMFRM, the error
  138. SPICE(INVALIDREFFRAME) is signaled.
  139. 11) If nrec, the number of pointing records, is less than or
  140. equal to 0, the error SPICE(INVALIDNUMRECS) is signaled.
  141. 12) If any quaternion has magnitude zero, the error
  142. SPICE(ZEROQUATERNION) is signaled.
  143. -Files
  144. This routine adds a type 2 segment to a C-kernel. The C-kernel
  145. may be either a new one or an existing one opened for writing.
  146. -Particulars
  147. For a detailed description of a type 2 CK segment please see the
  148. CK Required Reading.
  149. This routine relieves the user from performing the repetitive
  150. calls to the DAF routines necessary to construct a CK segment.
  151. Quaternion Styles
  152. -----------------
  153. There are different "styles" of quaternions used in
  154. science and engineering applications. Quaternion styles
  155. are characterized by
  156. - The order of quaternion elements
  157. - The quaternion multiplication formula
  158. - The convention for associating quaternions
  159. with rotation matrices
  160. Two of the commonly used styles are
  161. - "SPICE"
  162. > Invented by Sir William Rowan Hamilton
  163. > Frequently used in mathematics and physics textbooks
  164. - "Engineering"
  165. > Widely used in aerospace engineering applications
  166. CSPICE function interfaces ALWAYS use SPICE quaternions.
  167. Quaternions of any other style must be converted to SPICE
  168. quaternions before they are passed to CSPICE functions.
  169. Relationship between SPICE and Engineering Quaternions
  170. ------------------------------------------------------
  171. Let M be a rotation matrix such that for any vector V,
  172. M*V
  173. is the result of rotating V by theta radians in the
  174. counterclockwise direction about unit rotation axis vector A.
  175. Then the SPICE quaternions representing M are
  176. (+/-) ( cos(theta/2),
  177. sin(theta/2) A(1),
  178. sin(theta/2) A(2),
  179. sin(theta/2) A(3) )
  180. while the engineering quaternions representing M are
  181. (+/-) ( -sin(theta/2) A(1),
  182. -sin(theta/2) A(2),
  183. -sin(theta/2) A(3),
  184. cos(theta/2) )
  185. For both styles of quaternions, if a quaternion q represents
  186. a rotation matrix M, then -q represents M as well.
  187. Given an engineering quaternion
  188. QENG = ( q0, q1, q2, q3 )
  189. the equivalent SPICE quaternion is
  190. QSPICE = ( q3, -q0, -q1, -q2 )
  191. Associating SPICE Quaternions with Rotation Matrices
  192. ----------------------------------------------------
  193. Let FROM and TO be two right-handed reference frames, for
  194. example, an inertial frame and a spacecraft-fixed frame. Let the
  195. symbols
  196. V , V
  197. FROM TO
  198. denote, respectively, an arbitrary vector expressed relative to
  199. the FROM and TO frames. Let M denote the transformation matrix
  200. that transforms vectors from frame FROM to frame TO; then
  201. V = M * V
  202. TO FROM
  203. where the expression on the right hand side represents left
  204. multiplication of the vector by the matrix.
  205. Then if the unit-length SPICE quaternion q represents M, where
  206. q = (q0, q1, q2, q3)
  207. the elements of M are derived from the elements of q as follows:
  208. +- -+
  209. | 2 2 |
  210. | 1 - 2*( q2 + q3 ) 2*(q1*q2 - q0*q3) 2*(q1*q3 + q0*q2) |
  211. | |
  212. | |
  213. | 2 2 |
  214. M = | 2*(q1*q2 + q0*q3) 1 - 2*( q1 + q3 ) 2*(q2*q3 - q0*q1) |
  215. | |
  216. | |
  217. | 2 2 |
  218. | 2*(q1*q3 - q0*q2) 2*(q2*q3 + q0*q1) 1 - 2*( q1 + q2 ) |
  219. | |
  220. +- -+
  221. Note that substituting the elements of -q for those of q in the
  222. right hand side leaves each element of M unchanged; this shows
  223. that if a quaternion q represents a matrix M, then so does the
  224. quaternion -q.
  225. To map the rotation matrix M to a unit quaternion, we start by
  226. decomposing the rotation matrix as a sum of symmetric
  227. and skew-symmetric parts:
  228. 2
  229. M = [ I + (1-cos(theta)) OMEGA ] + [ sin(theta) OMEGA ]
  230. symmetric skew-symmetric
  231. OMEGA is a skew-symmetric matrix of the form
  232. +- -+
  233. | 0 -n3 n2 |
  234. | |
  235. OMEGA = | n3 0 -n1 |
  236. | |
  237. | -n2 n1 0 |
  238. +- -+
  239. The vector N of matrix entries (n1, n2, n3) is the rotation axis
  240. of M and theta is M's rotation angle. Note that N and theta
  241. are not unique.
  242. Let
  243. C = cos(theta/2)
  244. S = sin(theta/2)
  245. Then the unit quaternions Q corresponding to M are
  246. Q = +/- ( C, S*n1, S*n2, S*n3 )
  247. The mappings between quaternions and the corresponding rotations
  248. are carried out by the CSPICE routines
  249. q2m_c {quaternion to matrix}
  250. m2q_c {matrix to quaternion}
  251. m2q_c always returns a quaternion with scalar part greater than
  252. or equal to zero.
  253. SPICE Quaternion Multiplication Formula
  254. ---------------------------------------
  255. Given a SPICE quaternion
  256. Q = ( q0, q1, q2, q3 )
  257. corresponding to rotation axis A and angle theta as above, we can
  258. represent Q using "scalar + vector" notation as follows:
  259. s = q0 = cos(theta/2)
  260. v = ( q1, q2, q3 ) = sin(theta/2) * A
  261. Q = s + v
  262. Let Q1 and Q2 be SPICE quaternions with respective scalar
  263. and vector parts s1, s2 and v1, v2:
  264. Q1 = s1 + v1
  265. Q2 = s2 + v2
  266. We represent the dot product of v1 and v2 by
  267. <v1, v2>
  268. and the cross product of v1 and v2 by
  269. v1 x v2
  270. Then the SPICE quaternion product is
  271. Q1*Q2 = s1*s2 - <v1,v2> + s1*v2 + s2*v1 + (v1 x v2)
  272. If Q1 and Q2 represent the rotation matrices M1 and M2
  273. respectively, then the quaternion product
  274. Q1*Q2
  275. represents the matrix product
  276. M1*M2
  277. -Examples
  278. This example writes a predict type 2 C-kernel segment for
  279. the Mars Observer spacecraft bus to a previously opened CK file
  280. attached to handle.
  281. /.
  282. Assume arrays of quaternions, angular velocities, and interval
  283. start and stop times are produced elsewhere.
  284. ./
  285. .
  286. .
  287. .
  288. /.
  289. The nominal number of seconds in a tick for MO is 1/256.
  290. ./
  291. sectik = 1. / 256.;
  292. for ( i = 0; i < nrec; i++ )
  293. {
  294. rate[i] = sectik;
  295. }
  296. /.
  297. The subroutine ckw02_c needs the following components of the
  298. segment descriptor:
  299. 1) SCLK limits of the segment.
  300. 2) Instrument code.
  301. 3) Reference frame.
  302. ./
  303. begtim = start [ 0 ];
  304. endtim = stop [nrec-1];
  305. inst = -94000;
  306. ref = "j2000";
  307. segid = "mo predict seg type 2";
  308. /.
  309. Write the segment.
  310. ./
  311. ckw02_c ( handle, begtim, endtim, inst, ref, segid,
  312. nrec, start, stop, quat, avv, rates );
  313. -Restrictions
  314. None.
  315. -Literature_References
  316. None.
  317. -Author_and_Institution
  318. N.J. Bachman (JPL)
  319. K.R. Gehringer (JPL)
  320. J.M. Lynch (JPL)
  321. -Version
  322. -CSPICE Version 2.0.0, 01-JUN-2010 (NJB)
  323. The check for non-unit quaternions has been replaced
  324. with a check for zero-length quaternions. (The
  325. implementation of the check is located in ckw02_.)
  326. -CSPICE Version 1.2.1, 27-FEB-2008 (NJB)
  327. Updated header; added information about SPICE
  328. quaternion conventions.
  329. -CSPICE Version 1.2.0, 28-AUG-2001 (NJB)
  330. Changed prototype: inputs start, stop, sclkdp, quats,
  331. and avvs are now const-qualified. Implemented interface
  332. macros for casting these inputs to const.
  333. -CSPICE Version 1.1.0, 08-FEB-1998 (NJB)
  334. References to C2F_CreateStr_Sig were removed; code was
  335. cleaned up accordingly. String checks are now done using
  336. the macro CHKFSTR.
  337. -CSPICE Version 1.0.0, 25-OCT-1997 (NJB)
  338. Based on SPICELIB Version 2.0.0, 28-DEC-1993 (WLT)
  339. -Index_Entries
  340. write ck type_2 pointing data segment
  341. -&
  342. */
  343. { /* Begin ckw02_c */
  344. /*
  345. Participate in error handling.
  346. */
  347. chkin_c ( "ckw02_c" );
  348. /*
  349. Check the input strings to make sure the pointers
  350. are non-null and the string lengths are non-zero.
  351. */
  352. CHKFSTR ( CHK_STANDARD, "ckw02_c", ref );
  353. CHKFSTR ( CHK_STANDARD, "ckw02_c", segid );
  354. /*
  355. Write the segment. Note that the quaternion and angular velocity
  356. arrays DO NOT require transposition!
  357. */
  358. ckw02_( ( integer * ) &handle,
  359. ( doublereal * ) &begtim,
  360. ( doublereal * ) &endtim,
  361. ( integer * ) &inst,
  362. ( char * ) ref,
  363. ( char * ) segid,
  364. ( integer * ) &nrec,
  365. ( doublereal * ) start,
  366. ( doublereal * ) stop,
  367. ( doublereal * ) quats,
  368. ( doublereal * ) avvs,
  369. ( doublereal * ) rates,
  370. ( ftnlen ) strlen(ref),
  371. ( ftnlen ) strlen(segid) );
  372. chkout_c ( "ckw02_c" );
  373. } /* End ckw02_c */