-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswpc_cat.pro
11471 lines (8668 loc) · 447 KB
/
swpc_cat.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;----------------------------------------------------------------
;
; NOAA/SWPC CME ANALYSIS TOOL (CAT)
; Developed as part of the WSA-Enlil transition project
; Released to the Solar Physics community via Solarsoft
; February 2013
;
;----------------------------------------------------------------
;
; This software was developed at the NOAA Space Weather Prediction
; Center by employees of the Federal Government in the course
; of their official duties. Pursuant to title 17 Section 105 of the
; United States Code this software is not subject to copyright
; protection and is in the public domain. It is an experimental system.
; NOAA assumes no responsibility whatsoever for its use by other parties,
; and makes no guarantees, expressed or implied, about its quality,
; reliability, or any other characteristic.
;
; We would appreciate acknowledgement if the software is used.
; This software can be redistributed and/or modified freely provided
; that any derivative works bear some notice that they are derived from it,
; and any modified versions bear some notice that they have been modified.
;
; We would like to acknowledge use of several routines taken from the wider
; Physics and idl programming communities - most notably software developed
; by Craig Markwardt and David Fanning:
;
; http://cow.physics.wisc.edu/~craigm/idl/idl.html
; http://www.idlcoyote.com/
;
; The development of CAT and it's use within the context of the WSA-Enlil
; project has been published in Space Weather Journal:
; http://onlinelibrary.wiley.com/doi/10.1002/swe.20024/abstract
;
; George Millward
; Curt de Koning
; Vic Pizzo
; Doug Biesecker
;
; NOAA Space Weather Prediction Center
; 325 Broadway, Boulder, CO 80305
;
;
;----------------------------------------------------------------
;
;
;+
; NAME:
; QTCOMPOSE
;
; AUTHOR:
; Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770
; UPDATED VERSIONs can be found on my WEB PAGE:
; http://cow.physics.wisc.edu/~craigm/idl/idl.html
;
; PURPOSE:
; Convert a rotation angle and axis into quaternion
;
; MAJOR TOPICS:
; Geometry
;
; CALLING SEQUENCE:
; Q = QTCOMPOSE(VAXIS, PHI)
;
; DESCRIPTION:
;
; The function QTCOMPOSE accepts a unit vector rotation axis VAXIS
; and a rotation angle PHI, and returns the corresponding quaternion.
;
; The user must take care to pass the same number of axes as rotation
; angles.
;
; Use QTAXIS and QTANG to extract the properties of an existing
; quaternion. Use QTCOMPOSE to combine a rotation axis and angle
; into a new quaternion.
;
; Conventions for storing quaternions vary in the literature and from
; library to library. This library uses the convention that the
; first three components of each quaternion are the 3-vector axis of
; rotation, and the 4th component is the rotation angle. Expressed
; in formulae, a single quaternion is given by:
;
; Q(0:2) = [VX, VY, VZ]*SIN(PHI/2)
; Q(3) = COS(PHI/2)
;
; where PHI is the rotation angle, and VAXIS = [VX, VY, VZ] is the
; rotation eigen axis expressed as a unit vector. This library
; accepts quaternions of both signs, but by preference returns
; quaternions with a positive 4th component.
;
; INPUTS:
;
; VAXIS - array of one or more unit vectors specifying the rotation
; axes. For a single rotation, VAXIS should be a 3-vector.
; For N vectors, VAXIS should be a 3xN array.
;
; PHI - one or more rotation angles, in radians. For a single
; rotation, PHI should be a scalar. For N rotations, PHI
; should be an N-vector.
;
; RETURNS:
;
; For a single rotation, returns a quaternion as a 4-vector. For N
; rotations, returns a 4xN vector of quaternions.
;
;
; KEYWORD PARAMETERS:
;
; NONE
;
; EXAMPLE:
;
; IDL> print, qtcompose([0d,1,0], !dpi/4)
; 0.0000000 0.38268343 0.0000000 0.92387953
;
; Prints the quaternion composed of a rotation of !dpi/4 radians
; around the axis [0,1,0]
;
;
; SEE ALSO
; QTANG, QTAXIS, QTCOMPOSE, QTERP, QTEXP, QTFIND, QTINV, QTLOG,
; QTMAT, QTMULT, QTPOW, QTVROT
;
; MODIFICATION HISTORY:
; Written, July 2001, CM
; Documented, Dec 2001, CM
; Allow output to be DOUBLE, 27 Jan 2002, CM
; Allow vector vs scalar arguments, 28 Jan 2002, CM
; Usage message, error checking, 15 Mar 2002, CM
;
; $Id: qtcompose.pro,v 1.11 2008/12/14 20:00:31 craigm Exp $
;
;-
; Copyright (C) 2001, 2002, Craig Markwardt
; This software is provided as is without any warranty whatsoever.
; Permission to use, copy, modify, and distribute modified or
; unmodified copies is granted, provided this copyright and disclaimer
; are included unchanged.
;-
function swpc_cat_qtcompose, axis, phi
if n_params() EQ 0 then begin
info = 1
USAGE:
message, 'USAGE:', /info
message, 'Q = QTCOMPOSE(AXIS, PHI)', info=1
return, 0
endif
nph = n_elements(phi)
nv = n_elements(axis)/3
if nph LT 1 OR nv LT 1 then goto, USAGE
nq = nv > nph
q = make_array(value=axis(0)*phi(0)*0., 4,nq)
sph = sin(phi/2) & cph = cos(phi/2)
if nph EQ 1 AND nv EQ 1 then return, [ axis(0:2) * sph(0), cph(0) ]
if nph GT 1 AND nv EQ 1 then begin
;; Single axis, multiple rotation angles
q(0,*) = axis(0)*sph
q(1,*) = axis(1)*sph
q(2,*) = axis(2)*sph
q(3,*) = cph
endif else if nph EQ 1 AND nv GT 1 then begin
;; Multiple axis, single rotation
q(0:2,*) = axis*sph(0)
q(3,*) = cph(0)
endif else if nph EQ nv then begin
;; Multiple axes, multiple rotations
q(0:2,*) = axis*rebin(reform(temporary(sph),1,nq),3,nq)
q(3,*) = temporary(cph)
endif else begin
message, 'ERROR: number of axes and angles do not match'
endelse
return, q
end
function swpc_cat_qtmult, aqt, bqt, inv1=inverse1, inv2=inverse2
; THIS ROUTINE MULTIPLIES QUATERNIONS
; CQT CORRESPONDS TO THE ROTATION AQT FOLLOWED BY BQT
; ASSUMING S/C COORDINATES ARE INITIALLY ALIGN WITH INERTIAL COORD.
; THEN ROTATION AQT DESCRIBES ROTATION SUCH THAT THE SUBROUTINE
; QTXRA GIVES THE INERTIAL COORDINATES OF THE S/C X-AXIS
; THE FIRST 3 COMPONENTS OF AQT GIVE THE EIGENAXIS EXPRESSED
; IN S/C COORDINATES BEFORE THE ROTATION (=INTERTIAL COORD.).
; THE BQT ROTATION FOLLOWS THE AQT ROTATION. CQT THEN DESCRIBES
; THIS COMBINATION SUCH THAT QTXRA GIVES THE INERTIAL COORDINATES
; OF THE S/C X-AXIS AFTER BOTH ROTATIONS.
; THE FIRST 3 COMPONENTS OF BQT GIVE THE EIGENAXIS EXPRESSED
; IN S/C COORDINATES AFTER THE AQT ROTATION.
if n_params() EQ 0 then begin
info = 1
USAGE:
message, 'USAGE:', /info
message, 'QNEW = QTMULT(Q1, Q2)', info=info
return, 0
endif
sz1 = size(aqt)
sz2 = size(bqt)
if sz1(0) LT 1 OR sz2(0) LT 1 then $
message, 'ERROR: Q1 and Q2 must be quaternions'
if sz1(1) NE 4 OR sz2(1) NE 4 then $
message, 'ERROR: Q1 and Q2 must be quaternions'
n1 = n_elements(aqt)/4
n2 = n_elements(bqt)/4
if n1 NE n2 AND n1 NE 1 AND n2 NE 1 then $
message, 'ERROR: Q1 and Q2 must both have the same number of quaternions'
nq = n1>n2
cqt = make_array(value=aqt(0)*bqt(0)*0, dimension=[4,nq])
if n1 GT 1 then begin
aqt0 = aqt(0,*) & aqt1 = aqt(1,*) & aqt2 = aqt(2,*) & aqt3 = aqt(3,*)
endif else begin
aqt0 = aqt(0) & aqt1 = aqt(1) & aqt2 = aqt(2) & aqt3 = aqt(3)
endelse
if n2 GT 1 then begin
bqt0 = bqt(0,*) & bqt1 = bqt(1,*) & bqt2 = bqt(2,*) & bqt3 = bqt(3,*)
endif else begin
bqt0 = bqt(0) & bqt1 = bqt(1) & bqt2 = bqt(2) & bqt3 = bqt(3)
endelse
if keyword_set(inverse1) then begin
aqt0 = -aqt0 & aqt1 = -aqt1 & aqt2 = -aqt2
endif
if keyword_set(inverse2) then begin
bqt0 = -bqt0 & bqt1 = -bqt1 & bqt2 = -bqt2
endif
CQT(0,0) = AQT0*BQT3 + AQT1*BQT2 - AQT2*BQT1 + AQT3*BQT0
CQT(1,0) =-AQT0*BQT2 + AQT1*BQT3 + AQT2*BQT0 + AQT3*BQT1
CQT(2,0) = AQT0*BQT1 - AQT1*BQT0 + AQT2*BQT3 + AQT3*BQT2
CQT(3,0) =-AQT0*BQT0 - AQT1*BQT1 - AQT2*BQT2 + AQT3*BQT3
return, cqt
end
;+
; NAME:
; QTEULER
;
; AUTHOR:
; Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770
; UPDATED VERSIONs can be found on my WEB PAGE:
; http://cow.physics.wisc.edu/~craigm/idl/idl.html
;
; PURPOSE:
; Compose a series of euler-type rotations into a single quaternion
;
; MAJOR TOPICS:
; Geometry
;
; CALLING SEQUENCE:
; Q = QTEULER(AXES, ANG0, ANG1, ... )
;
; DESCRIPTION:
;
; The function QTEULER composes a series of Euler-type rotations into
; a single set of quaternion representations.
;
; The user specifies a set of axes, and the angles to rotation about
; those axes, and QTEULER constructs the corresponding quaternion.
;
; There must be a one-to-one correspondence between the elements of
; AXES and the number of rotations. AXES specifies the rotation axes
; as an string, which must be one of 'X', 'Y', or 'Z'. Other axes
; are invalid. For example, the following call:
;
; QTEULER(['X','Z'], THETA, PHI)
;
; will rotate first about the *Z* axis by the angle PHI, and then
; around the *resulting X* axis by angle THETA.
;
; Several things are worth noting here. First, rotations are applied
; first from the right, not the left. This conforms to the usual
; matrix notation for applying rotations to a vector on the right
; hand side. For example, in matrix notation,
;
; XNEW = A3 A2 A1 XOLD
;
; applies first A1, then A2 and finally A3 to the XOLD vector,
; resulting in the new vector XNEW. The same semantics apply here.
;
; A second thing to bear in mind is that the axes themselves change
; during the rotations. Thus, the coordinates specified in AXES
; should be considered attached to the "body" and not the inertial
; frame.
;
;
; INPUTS:
;
; AXES - a string array, specifies the rotation axes. Rotations are
; applied last element first. Each element of AXES must be
; one of 'X', 'Y' or 'Z'.
;
; ANG0, ..., ANGi - the successive rotation angles. Angle ANGi
; corresponds to axis AXES(i).
;
; If ANGi is a scalar, then it will be promoted to a vector
; the same size as the other rotation angles being performed.
; Otherwise, if the angles ANGi are vectors, then they must
; all be of the same size.
;
; RETURNS:
;
; The resulting quaternion (or, if ANGi are vectors, array of
; quaternions), which represent the requested rotations.
;
;
; KEYWORD PARAMETERS:
;
; NONE
;
; EXAMPLE:
;
; ;; Precession Nutation
; qtot = qteuler(['z','y','z', 'x','z','x' ], $
; -zeta, +theta, -z, +eps0, -dpsi, -eps)
;
; Applies a series of rotations to correct for earth nutation and
; precession. The order of rotations on a vector would be
; X-Z-X-Z-Y-Z (i.e., the reverse order printed).
;
; SEE ALSO
; QTANG, QTAXIS, QTCOMPOSE, QTERP, QTEXP, QTFIND, QTINV, QTLOG,
; QTMAT, QTMULT, QTPOW, QTVROT
;
; MODIFICATION HISTORY:
; Written, 27 Jan 2002, CM
; More error checking, 03 Mar 2002, CM
;
; $Id: qteuler.pro,v 1.4 2002/05/09 23:03:27 craigm Exp $
;
;-
; Copyright (C) 2002, Craig Markwardt
; This software is provided as is without any warranty whatsoever.
; Permission to use, copy, modify, and distribute modified or
; unmodified copies is granted, provided this copyright and disclaimer
; are included unchanged.
;-
;; Extract axis ei and angle angi
pro swpc_cat_qteuler_extract, ax, i, ei, angi, $
ang0, ang1, ang2, ang3, ang4, $
ang5, ang6, ang7, ang8, ang9, $
status=status, errmsg=errmsg
status = 0
zero = ang0(0)*0
ex = [1,zero,zero] & ey = [zero,1,zero] & ez = [zero,zero,1]
ei = [0D, 0D, 0D]
; the following line will not work under the idl virtual machine.
; commenting it out for now because I don't think it is ever used.
; ghgm 7/22/2013
if i eq 0 then begin
ei = ez
angi = ang0
endif
if i eq 1 then begin
ei = ey
angi = ang1
endif
if i eq 2 then begin
ei = ex
angi = ang2
endif
;print, ' here 1'
;
;print,'i = *',i,'*'
;print, ax(i)
;print, strtrim(i,2)
;
;void = execute('ei = e'+ax(i)+' & angi = ang'+strtrim(i,2))
;
;print, void
;
;print, ' here 2'
; if execute('ei = e'+ax(i)+' & angi = ang'+strtrim(i,2)) NE 1 then begin
; stop
; errmsg = 'Invalid axis specification'
; return
; endif
status = 1
return
end
function swpc_cat_qteuler, axes, block=block, $
ang0, ang1, ang2, ang3, ang4, ang5, ang6, ang7, ang8, ang9, $
ang10, ang11, ang12, ang13, ang14, ang15
if n_params() EQ 0 then begin
info = 1
USAGE_ERR:
message, 'USAGE: Q = QTEULER(AXES, ANG0, ...)', /info
message, ' AXES = ["X",...] ("X" or "Y" or "Z")', /info
message, ' ANGn = rotation angle (radians)', info=info
return, 0
endif
if n_elements(axes) LT 1 OR n_elements(ang0) LT 1 then $
goto, USAGE_ERR
nang = n_params()-1
;; Check to be sure each axis label is 'X' 'Y' or 'Z'
ax = strupcase(strmid(strtrim(axes,2),0,1))
wh = where(ax NE 'X' AND ax NE 'Y' AND ax NE 'Z', ct)
if ct GT 0 then begin
errmsg = 'AXES must be one of "X", "Y" or "Z"'
goto, BAD_AXIS
endif
if n_elements(ax) NE nang then begin
errmsg = 'Number of AXES and rotations ANGi must agree'
goto, BAD_AXIS
endif
swpc_cat_qteuler_extract, ax, 0, ev, angv, status=status, errmsg=errmsg, $
ang0, ang1, ang2, ang3, ang4, ang5, ang6, ang7, ang8, ang9
if status EQ 0 then begin
BAD_AXIS:
message, 'ERROR: '+errmsg, /info
goto, USAGE_ERR
endif
qq = swpc_cat_qtcompose(ev, angv)
for i = 1, nang-1 do begin
swpc_cat_qteuler_extract, ax, i, ev, angv, status=status, errmsg=errmsg, $
ang0, ang1, ang2, ang3, ang4, ang5, ang6, ang7, ang8, ang9
if status EQ 0 then goto, BAD_AXIS
qq = swpc_cat_qtmult(qq, swpc_cat_qtcompose(ev, angv))
endfor
return, qq
end
;+
; NAME:
; QTMULT
;
; AUTHOR:
; Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770
; UPDATED VERSIONs can be found on my WEB PAGE:
; http://cow.physics.wisc.edu/~craigm/idl/idl.html
;
; PURPOSE:
; Multiply quaternions
;
; MAJOR TOPICS:
; Geometry
;
; CALLING SEQUENCE:
; QRESULT = QTMULT(Q1, [/INV1,] Q2, [/INV2])
;
; DESCRIPTION:
;
; The function QTMULT performs multiplication of quaternions.
; Quaternion multiplication is not component-by-component, but
; rather represents the composition of two rotations, namely Q2
; followed by Q1.
;
; More than one multiplication can be performed at one time if Q1
; and Q2 are 4xN arrays. In that case both input arrays must be of
; the same dimension.
;
; If INV1 is set, then the inverse of Q1 is used. This is a
; convenience, to avoid the call QTINV(Q1). Of course, INV2 can
; be set to use the inverse of Q2.
;
; Note that quaternion multiplication is not commutative.
;
; Conventions for storing quaternions vary in the literature and from
; library to library. This library uses the convention that the
; first three components of each quaternion are the 3-vector axis of
; rotation, and the 4th component is the rotation angle. Expressed
; in formulae, a single quaternion is given by:
;
; Q(0:2) = [VX, VY, VZ]*SIN(PHI/2)
; Q(3) = COS(PHI/2)
;
; where PHI is the rotation angle, and VAXIS = [VX, VY, VZ] is the
; rotation eigen axis expressed as a unit vector. This library
; accepts quaternions of both signs, but by preference returns
; quaternions with a positive 4th component.
;
;
; INPUTS:
;
; Q1 - array of one or more unit quaternions, the first operand in
; the multiplication. For a single quaternion, Q1 should be a
; 4-vector. For N quaternions, Q1 should be a 4xN array.
; If INV1 is set, then the inverse of Q1 is used.
;
; Q2 - same as Q1, for the second operand.
; If INV2 is set, then the inverse of Q2 is used.
;
; RETURNS:
;
; The resulting multiplied unit quaternions. For a single inputs,
; returns a 4-vector. For N input quaternions, returns N
; quaternions as a 4xN array.
;
;
; KEYWORD PARAMETERS:
;
; INV1 - if set, use QTINV(Q1) in place of Q1.
;
; INV2 - if set, use QTINV(Q2) in place of Q2.
;
; EXAMPLE:
;
; Q1 = qtcompose([0,0,1], 32d*!dpi/180d)
; Q2 = qtcompose([1,0,0], 116d*!dpi/180d)
;
; IDL> print, qtmult(q1, q2)
; 0.81519615 0.23375373 0.14606554 0.50939109
;
; Form a rotation quaternion of 32 degrees around the Z axis, and
; 116 degrees around the X axis, then multiply the two quaternions.
;
; SEE ALSO
; QTANG, QTAXIS, QTCOMPOSE, QTERP, QTEXP, QTFIND, QTINV, QTLOG,
; QTMAT, QTMULT, QTMULTN, QTPOW, QTVROT
;
; MODIFICATION HISTORY:
; Written, July 2001, CM
; Documented, Dec 2001, CM
; Documentation, allow 1xN or Nx1 multiplies, 27 Jan 2002, CM
; Usage message, error checking, 15 Mar 2002, CM
; Add the INV1 and INV2 keywords, 30 Aug 2007, CM
;
; $Id: qtmult.pro,v 1.8 2007/09/03 07:18:25 craigm Exp $
;
;-
; Copyright (C) 2001, 2002, 2007, Craig Markwardt
; This software is provided as is without any warranty whatsoever.
; Permission to use, copy, modify, and distribute modified or
; unmodified copies is granted, provided this copyright and disclaimer
; are included unchanged.
;-
;+
; NAME:
; QTVROT
;
; AUTHOR:
; Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770
; UPDATED VERSIONs can be found on my WEB PAGE:
; http://cow.physics.wisc.edu/~craigm/idl/idl.html
;
; PURPOSE:
; Apply quaternion rotation to a 3-vector
;
; MAJOR TOPICS:
; Geometry
;
; CALLING SEQUENCE:
; VNEW = QTVROT(V, Q, [/INVERT])
;
; DESCRIPTION:
;
; The function QTVROT applies a quaternion rotation (or its inverse)
; to a 3-vector V to produce a new vector VNEW.
;
; If both V and VNEW are vector components measured in the same
; inertial coordinate system, then VNEW returns the components of
; the vector V rotated by quaternion Q. I.e., the AXES stay fixed
; and the VECTOR rotates. Replace Q by QTINV(Q) in the case of
; /INVERT.
;
; If V are components of a vector measured in the "body" coordinate
; frame, and Q represents the orientation of the body frame
; w.r.t. the inertial frame, then VNEW are the components of the
; same vector in the inertial frame. I.e., the VECTOR stays fixed
; and the AXES rotate. For /INVERT, the coordinate transformation
; is from inertial frame to body frame.
;
; If either Q is a single quaternion, or V is a single 3-vector,
; then QTVROT will expand the single to the number of elements of
; the other operand. Otherwise, the number of quaternions and
; vectors must be equal.
;
; Conventions for storing quaternions vary in the literature and from
; library to library. This library uses the convention that the
; first three components of each quaternion are the 3-vector axis of
; rotation, and the 4th component is the rotation angle. Expressed
; in formulae, a single quaternion is given by:
;
; Q(0:2) = [VX, VY, VZ]*SIN(PHI/2)
; Q(3) = COS(PHI/2)
;
; where PHI is the rotation angle, and VAXIS = [VX, VY, VZ] is the
; rotation eigen axis expressed as a unit vector. This library
; accepts quaternions of both signs, but by preference returns
; quaternions with a positive 4th component.
;
;
; INPUTS:
;
; V - array of one or more 3-vectors. For a single vector, V should
; be a 3-vector. For N vectors, V should be a 3xN array.
;
; Q - array of one or more unit quaternions. For a single
; quaternion, Q should be a 4-vector. For N quaternions, Q
; should be a 4xN array.
;
;
; RETURNS:
;
; The resulting rotated vectors. For single inputs, returns a
; 3-vector. For N inputs, returns N vectors as a 3xN array.
;
;
; KEYWORD PARAMETERS:
;
; INVERT - if set, then the antirotation represented by QTINV(Q) is
; performed.
;
;
; EXAMPLE:
;
; Q1 = qtcompose([0,0,1], 32d*!dpi/180d)
; Q2 = qtcompose([1,0,0], 116d*!dpi/180d)
; Q = qtmult(Q1, Q2)
;
; V = [[1d,0,0],[0,1,0],[0,0,1]]
;
; IDL> print, qtvrot(v, q)
; 0.84804810 0.52991926 0.0000000
; 0.23230132 -0.37175982 0.89879405
; 0.47628828 -0.76222058 -0.43837115
;
;
; SEE ALSO
; QTANG, QTAXIS, QTCOMPOSE, QTERP, QTEXP, QTFIND, QTINV, QTLOG,
; QTMAT, QTMULT, QTPOW, QTVROT
;
; MODIFICATION HISTORY:
; Written, July 2001, CM
; Documented, Dec 2001, CM
; Small changes, 28 Jan 2002, CM
; Usage message, error checking, 15 Mar 2002, CM
;
; $Id: qtvrot.pro,v 1.7 2002/05/09 23:03:27 craigm Exp $
;
;-
; Copyright (C) 2001, 2002, Craig Markwardt
; This software is provided as is without any warranty whatsoever.
; Permission to use, copy, modify, and distribute modified or
; unmodified copies is granted, provided this copyright and disclaimer
; are included unchanged.
;-
;; QVROT
;;
;; The FORWARD (default) transform:
;;
;; * takes a vector vin (components given in inertial coordinates) and
;; returns the components of the rotated vector vout (components
;; given in inertial coordinates) -- ie, the AXES stay fixed and the
;; VECTOR rotates; OR, equivalently,
;;
;; * takes a fixed vector vin (components given in body coordinates)
;; and returns the components of the vector in inertial coordinates,
;; where the body system is described by quaternion q -- ie, the
;; VECTOR stays fixed and the AXES rotate.
;;
;;
;; The INVERSE transform (gotten by setting /INVERT):
;;
;; * takes a vector vin (components given in inertial coordinates) and
;; returns the components of the anti-rotated vector vout
;; (components given in inertial coordinates) -- ie, the AXES stay
;; fixed and the VECTOR rotates. Anti-rotated here means rotated in
;; the opposite direction of q; OR, equivalently,
;;
;; * takes a fixed vector vin (components given in inertial
;; coordinates) and returns the components of the vector in body
;; coordinates, where the body system is described by quaternion q
;; -- ie, the VECTOR stays fixed and the AXES rotate.
;;
function swpc_cat_qtvrot, vin, q, invert=invert
if n_params() EQ 0 then begin
info = 1
USAGE:
message, 'USAGE:', /info
message, 'VNEW = QTVROT(V, Q)', info=info
return, 0
endif
nq = n_elements(q)/4
nv = n_elements(vin)/3
if nq LT 1 OR nv LT 1 then goto, USAGE
if n_elements(q) GT 4 AND n_elements(vin) GT 3 then begin
if n_elements(q)/4 NE n_elements(vin)/3 then begin
message, 'ERROR: incompatible number of quaternions & vectors'
return, -1L
end
vout = vin*q(0)*0.
nq = n_elements(q)/4
nv = nq
endif else if n_elements(q) GT 4 then begin
nq = n_elements(q)/4
nv = 1L
vout = vin(*) # (fltarr(nq)+1) * q(0)*0.
endif else begin
nq = 1L
nv = n_elements(vin)/3
vout = vin*q(0)*0.
endelse
vout = reform(vout, 3, max([nv,nq]), /overwrite)
q1 = q(0,*) & q2 = q(1,*) & q3 = q(2,*) & q4 = q(3,*)
if n_elements(q1) EQ 1 then begin
q1 = q1(0) & q2 = q2(0) & q3 = q3(0) & q4 = q4(0)
endif else begin
q1 = q1(*) & q2 = q2(*) & q3 = q3(*) & q4 = q4(*)
endelse
v0 = vin(0,*) & v1 = vin(1,*) & v2 = vin(2,*)
if n_elements(v0) EQ 1 then begin
v0 = v0(0) & v1 = v1(0) & v2 = v2(0)
endif else begin
v0 = v0(*) & v1 = v1(*) & v2 = v2(*)
endelse
if NOT keyword_set(INVERT) then begin
;; FORWARD TRANSFORMATION
VOUT(0,*)=((Q1*Q1-Q2*Q2-Q3*Q3+Q4*Q4)*V0 $
+ 2.D0*(Q1*Q2-Q3*Q4)*V1 $
+ 2.D0*(Q1*Q3+Q2*Q4)*V2)
VOUT(1,*)=(2.D0*(Q1*Q2+Q3*Q4)*V0 $
+ (-Q1*Q1+Q2*Q2-Q3*Q3+Q4*Q4)*V1 $
+ 2.D0*(Q2*Q3-Q1*Q4)*V2)
VOUT(2,*)=(2.D0*(Q1*Q3-Q2*Q4)*V0 $
+ 2.D0*(Q2*Q3+Q1*Q4)*V1 $
+ (-Q1*Q1-Q2*Q2+Q3*Q3+Q4*Q4)*V2)
endif else begin
;; INVERSE TRANSFORMATION
VOUT(0,*)=((Q1*Q1-Q2*Q2-Q3*Q3+Q4*Q4)*V0 $
+ 2.D0*(Q1*Q2+Q3*Q4)*V1 $
+ 2.D0*(Q1*Q3-Q2*Q4)*V2)
VOUT(1,*)=(2.D0*(Q1*Q2-Q3*Q4)*V0 $
+ (-Q1*Q1+Q2*Q2-Q3*Q3+Q4*Q4)*V1 $
+ 2.D0*(Q2*Q3+Q1*Q4)*V2)
VOUT(2,*)=(2.D0*(Q1*Q3+Q2*Q4)*V0 $
+ 2.D0*(Q2*Q3-Q1*Q4)*V1 $
+ (-Q1*Q1-Q2*Q2+Q3*Q3+Q4*Q4)*V2)
endelse
vout = vout
return, vout
end
pro swpc_cat_GENLEM_CALC_VERTICES, params, vert_data,calc_info, lemniscate_style $
, xverts=xverts, yverts=yverts
; BUILD THE LEMNISCATE IN THE STANDARD FRAME
; in the standard frame the central axis of the lemniscate is just the
; x-axis
if not(keyword_set(xverts)) then xverts = 30
if not(keyword_set(yverts)) then yverts = 90
nrows = long(xverts) > 2
ncols = long(yverts) > 2
dblp = double(params)
; build lemniscate used parametric equation
; tttt is a parameter ranging from [0,pi/2] (excluding endpoints)
; ssss is a parameter of revolution, ranging from [0,2pi]
tttt = !dpi/2.0d0 * (1.0d0+dindgen(nrows))/double(nrows+1L)
ssss = 2.0d0*!dpi * (dindgen(ncols))/double(ncols-1L)
xmax = dblp[0]
tmpx = xmax * cos(tttt)
if lemniscate_style then tmpx /= (1.0d0+sin(tttt)^2)
ymax = dblp[1] * cos(tttt)*sin(tttt)
tmpy = cos(ssss)
if lemniscate_style then ymax /= (1.0d0+sin(tttt)^2)
tmpz = -sin(ssss)
nverts = nrows*ncols + 2L
tmpdat = dblarr(nverts,3)
for rrr=0L,nrows-1L do begin
x1d = replicate(tmpx[rrr],ncols)
y1d = ymax[rrr]*tmpy
z1d = dblp[2]*ymax[rrr]*tmpz
; roundoff error can occur when y=ymax --- this should result in z=0,
; but sometimes it results in
; z^2=-1.0e-17, for example;
; in that case sqrt(z^2) will
; return a NaN
z1d[0] = 0.0d0
z1d[ncols-1] = 0.0d0
tmpdat[rrr*ncols:(rrr+1L)*ncols-1L,*] = [[x1d],[y1d],[z1d]]
endfor
tmpdat[nverts-2L,*] = [0.0d0,0.0d0,0.0d0]
tmpdat[nverts-1L,*] = [xmax,0.0d0,0.0d0]
vert_data = transpose(tmpdat)
calc_info = { info, $
n_xverts: nrows, $
n_yverts: ncols, $
numverts: nverts $
}
end
pro swpc_cat_GENLEM_ROTATE, angs,vert_orig, vert_rot
; ROTATE LEMNISCATE FROM STANDARD FRAME TO DATA FRAME
npts = n_elements(vert_orig) / 3L
; set up quaternion to rotate points from model-space to data-space
; 1st rotation: adjust for tilt
; 2nd rotation: rotate from inertial xy-plane to central plane of
; point cloud
; 3rd rotation: rotate from inertial x-axis to central axis of the
; point cloud
; see, also, wikipedia article on euler angles: rotation XYZ of points
; note that the complete 3-step rotation from Arfken [p 200] is not
; consistent with the physical concept of tilt
qrot = swpc_cat_qteuler(['Z','Y','X'],angs[0],-angs[1],-angs[2])
; perform rotation
vert_rot = swpc_cat_qtvrot(vert_orig,rebin(qrot,4L,npts))
END
pro swpc_cat_GENLEM_CALC_CONNECTIONS, calc_info, connex
; FILL IN THE CONNECTIVITY ARRAY
nrows = calc_info.n_xverts
ncols = calc_info.n_yverts
nconn = (ncols*(nrows-1L)*5L) + (2L*ncols*4L)
connex = lonarr(nconn)
i = 0L
for k=0L,nrows-2L do begin
for j=0L,ncols-1L do begin
connex[i] = 4L
connex[i+1L] = (k+1L)*ncols + j
w = ((j + 1L) mod ncols) + (k+1L)*ncols
connex[i+2L] = w
w = ((j + 1L) mod ncols) + k*ncols
connex[i+3L] = w
connex[i+4L] = k*ncols + j
i = i + 5L
endfor
endfor
; connect base point to first strip
for j=0L,ncols-1L do begin
connex[i] = 3L
connex[i+1L] = calc_info.numverts-1L
connex[i+2L] = j
connex[i+3L] = (j+1L) mod ncols
i = i + 4L
endfor
; connect xmax to last strip
for j=0L,ncols-1L do begin
connex[i] = 3L
connex[i+1L] = calc_info.numverts-2L
connex[i+2L] = j+(nrows-1L)*ncols
connex[i+3L] = ((j+1L) mod ncols) + (nrows-1L)*ncols
i = i + 4L
endfor
END
;-------------------------------------------------------------------------
; GENERAL_LEMNISCATE_DEFINE
;
; Purpose:
; Defines and populates a data structure for a generalized lemniscate.
; In the so-called standard frame, a generalized lemniscate is based
; on a quartic surface.
; For the lemniscate of Gerono, the surface is parameterized as
; x = c1*cos(t)
; y = c2*cos(t) sin(t) cos(s)
; z = c3*sin(s)
; For the lemniscate of Bernoulli, the surface is parameterized as
; x = c1*cos(t)/(1+sin(t)^2)
; y = c2*cos(t) sin(t) cos(s) / (1+sin(t)^2)
; z = c3*sin(s)
; In both cases, t=[0,pi/2], s=[0,2pi]
; To fully generalized the surface, it will be rotated such that its
; central axis (the x-axis in the standard frame) has an arbitrary
; orientaion; in addition, the generalized lemniscate will have an
; arbitrary tilt
;-------------------------------------------------------------------------
function swpc_cat_GENERAL_LEMNISCATE_DEFINE, params, in_angles, lemniscate_style $
, deg_or_rad=deg_or_rad $
, xverts=xverts, yverts=yverts
if not(keyword_set(xverts)) then xverts = 0
if not(keyword_set(yverts)) then yverts = 0
if not(keyword_set(deg_or_rad)) then deg_or_rad = 'rad'
if strlowcase(deg_or_rad) eq 'deg' then begin
pro_angs = !dpi/180.0d0 * in_angles
endif else pro_angs = in_angles
swpc_cat_genlem_calc_vertices,params,verts_stdfr,calc_info,lemniscate_style, $
xverts=xverts,yverts=yverts
swpc_cat_genlem_rotate,pro_angs,verts_stdfr,verts_datfr
swpc_cat_genlem_calc_connections,calc_info,connex
genlem = { lemniscate, $
parameters: params, $
orientation: in_angles[0:1], $
tilt: in_angles[2], $
vertices: verts_datfr, $
connections: connex $
}
return, genlem
END
;
;+
; NAME:
; FSC_NORMALIZE
;
; PURPOSE:
;
; This is a utility routine to calculate the scaling vector
; required to position a graphics primitive of specified range
; at a specific position in an arbitray coordinate system. The
; scaling vector is given as a two-element array like this:
;
; scalingVector = [translationFactor, scalingFactor]
;
; The scaling vector should be used with the [XYZ]COORD_CONV
; keywords of a graphics object or model. For example, if you
; wanted to scale an X axis into the coordinate range of -0.5 to 0.5,
; you might type something like this:
;
; xAxis->GetProperty, Range=xRange
; xScale = FSC_Normalize(xRange, Position=[-0.5, 0.5])
; xAxis, XCoord_Conv=xScale
;
; AUTHOR:
;
; FANNING SOFTWARE CONSULTING
; David Fanning, Ph.D.
; 1645 Sheely Drive