PageRenderTime 24ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ckw01_c.c

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