!!Stack= 0

c
c                                  
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Compute average segment positions and orientations, joint angles, and/
c              or reference positions/orientations from a set of motion data.
c              The reference positions are used to correct for skewed marker placement.
c
      subroutine do_AVG_calc( CMND,SF,JF,AF,OF )
      implicit none
      include 'a_const.inc'
                                   
      include 'a_struct.inc'
      include 'a_prm.inc'          
      include 'a_seg.inc'
      include 'a_jnt.inc'
      include 'a_names.inc'

      record /CmndLine/     CMND
      record /segment_flag/ SF
      record /joint_flag/   JF
      record /avg_pos_flag/ AF
      record /option_flag/  OF

      integer*4 i,j,k,frm,error,seg,jnt
      real*4    temp_Sgp(3,4),temp_angle(3),VecNorm
      character axis_name(3)*1
                             
      data axis_name / 'X','Y','Z' /

      if ( CMND.CmndText(2) .eq. 'POS' ) then
        AF.position= .false.
        if ( .not. SF.position ) goto 1000
        if ( MP.nfrm .lt. 0 ) goto 1030
        do i= 1, GP.num_seg
          seg= GP.seg_num(i)
          do j= x, cntr                          
            do k= x, z
              temp_Sgp(k,j)= 0.0
            end do ! k
          end do ! j
          do frm= 1, MP.nfrm
            do j= x, cntr
              do k= x, z   
                temp_Sgp(k,j)= temp_Sgp(k,j) + SgpGCS(k,j,frm,seg)
              end do ! k
            end do ! j
          end do ! frm
          do j= x, cntr
            do k= x, z
              SgpAvg(k,j,seg)= temp_Sgp(k,j)/float(MP.nfrm)
            end do ! k
            if ( j .ne. cntr ) then
              error= VecNorm( SgpAvg(x,j,seg),SgpAvg(x,j,seg) )
              if ( error .ne. 0 ) goto 1020
            end if
          end do ! j
        end do ! i => seg
        AF.position= .true.
        GP.AvgFile= GP.MtnFile
        
      else if ( CMND.CmndText(2) .eq. 'ANG' ) then
        AF.angle_JCS= .false.
        AF.angle_EUL= .false.
        if ( MP.nfrm .lt. 0 ) goto 1030
        if ( (.not. JF.angle_JCS) .and. (.not. JF.angle_EUL) ) goto 1010
        if ( JF.angle_JCS ) then
          do i= 1, GP.num_jnt
            jnt= GP.jnt_num(i)
            do j= flex, abad
              temp_angle(j)= 0.0
            end do ! j
            do frm= 1, MP.nfrm
              do j= flex, inex
                temp_angle(j)= temp_angle(j) + JangJCS(j,frm,jnt)
              end do ! j
            end do ! frm
            do j= flex, inex
              JangAvgJCS(j,jnt)= temp_angle(j)/float(MP.nfrm)
            end do ! j
          end do ! i => jnt
          AF.angle_JCS= .true.
          GP.AvgFile= GP.MtnFile
        else if ( JF.angle_EUL ) then
          do i= 1, GP.num_jnt
            jnt= GP.jnt_num(i)
            do j= flex, inex
              temp_angle(j)= 0.0
            end do ! j
            do frm= 1, MP.nfrm
              do j= flex, abad
                temp_angle(j)= temp_angle(j) + JangEUL(j,frm,jnt)
              end do ! j
            end do ! frm
            do j= flex, inex
              JangAvgEUL(j,jnt)= temp_angle(j)/float(MP.nfrm)
            end do ! j
          end do ! i => jnt
          AF.angle_EUL= .true.
          GP.AvgFile= GP.MtnFile
        end if 
        
      else if ( CMND.CmndText(2) .eq. 'REF' ) then
        call CalcAnatSgpRef( CMND,AF,OF )  
           
      else
        write(6,100) CMND.CmndText(2)
        if ( OF.ToLog ) write(3,100) CMND.CmndText(2)
100     format(' Error- ',a3,' is not a valid AVG command.'/
     &         '        Valid commands are: POS ANG REF')
      end if
      return

1000  write(6,1005)
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Segment position has not been calculated yet.')
      return
          
1010  write(6,1015)
      if ( OF.ToLog ) write(3,1015)
1015  format(' Error- Joint angles have not been calculated yet.')
      return 

1020  write(6,1025) seg_names(seg),frm,axis_name(j)
      if ( OF.ToLog ) write(3,1025)
1025  format(' Error- Vector magnitude too small to normalize.'/
     &       '        Trouble occured at ',a4,' frame #',i3,' ',a1,' axis.')
      return                        

1030  write(6,1035)
      if ( OF.ToLog ) write(3,1035)
1035  format(' Error- Number of motion frames less than or equal to 0.')
      return 
      end
                               
