c
c     Copyright by Dwight Meglan 1990,1993
c
      subroutine ExpSegPoseSIMM( inc,SF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_seg.inc'
      
      record /segment_flag/ SF
      record /option_flag/  OF
      
      integer*4 inc
      logical*4 Error

      real*4    TimeInc,LabCS(3,3),simmang(3),TmpRM(3,3)
      integer*4 frm,i,j,k,ierr,ncol,seg
      character name*4
                             
      data LabCS / 1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0 /
                           
      Error= .true.
      if ( .not. SF.position ) goto 1020 ! error

      ncol= GP.num_seg*6
      write(2,90) ncol,MP.nfrm/inc,float(MP.nfrm)
90    format(' #include <simmkeys.h>'//
     &       ' name frame'/' datacolumns ',i3/' datarows ',i3/
     &       ' range 0.0 ',f8.3/' keys g_key'/' wrap'/' endheader'//)
      do i= 1, GP.num_seg
         seg= GP.seg_num(i)
         call lower_name(seg_names(seg),name)
         write(2,100) name,name,name,name,name,name
 100     format(1x,a4,'_tx',5x,a4,'_ty',5x,a4,'_tz',
     &          5x,a4,'_rz',5x,a4,'_rx',5x,a4,'_ry')
      end do
      do frm= 1, MP.nfrm, inc
         do i= 1, GP.num_seg
            seg= GP.seg_num(i)
            do j= 1, 3
               TmpRM(j,1)=  SgpGCS(j,1,frm,seg)
               TmpRM(j,2)=  SgpGCS(j,2,frm,seg) ! j,3, ?
               TmpRM(j,3)=  SgpGCS(j,3,frm,seg) ! -sgpGCS(j,2, ?
            end do
c            call EulerAnglesZXY( TmpRM,LabCS,simmang,ierr ) ! float zxy
            call JcsAngles( TmpRM,LabCS,simmang,ierr ) ! fixed zxy
            if ( ierr .ne. 0 ) goto 1030
            write(2,110) SgpGCS(1,4,frm,seg),SgpGCS(3,4,frm,seg),
     &                  -SgpGCS(2,4,frm,seg),
     &                  -simmang(1),simmang(2),simmang(3)
 110        format(1x,6(f10.5,1x))
         end do
         write(2,120)
 120     format(/)
      end do
      Error= .false.                                  
      return

1020  write(6,1025)      
      if ( OF.ToLog ) write(3,1025)
1025  format(' Error- Segment positions have not been calculated yet')        
      return
      
1030  write(6,1035) ierr,frm,seg_names(seg)
      if ( OF.ToLog ) write(3,1035) ierr,frm,seg_names(seg)
1035  format(' Error- Error number ',i2,' occurred at frame ',i3,' while'/
     &       '        calculating ',a4,' angles')
      return            
      end


      subroutine lower_name(up,low)
      implicit none

      character*4 up,low
      integer*4   i

      do i= 1, 4
         if ( (ichar(up(i:i)) .ge. ichar('A')) .and.
     &        (ichar(up(i:i)) .le. ichar('Z'))       ) then
            low(i:i)= char( ichar('a') + ichar(up(i:i))-ichar('A') )
         else
            low(i:i)= up(i:i)
         endif
      end do
      return
      end
