      subroutine DigFilterJnt( CMND,JF,OF )
      implicit none                       

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      include 'a_enr.inc'

      record /CmndLine/    CMND
      record /joint_flag/  JF
      record /option_flag/ OF         

      real*4    smplfreq,Filter(256),TempData(MAX_MTN)
      integer*4 JntVarNum,i,j,k,n,NumFilPnts,NumBeginZero,NumEndZero,jnt,axis,
     &          side,load
      logical*4 JntVarFound,DoWindow,Error              
      character JntVarName(6)*3,AxisName(3)*4,LoadName(6)*3

      data JntVarName / 'JAN','JVA','EAN','EVA','LOA','POW' /
      data AxisName / 'flex','abad','inex' /
      data LoadName / 'fem','aam','iem','mlf','apf','cdf' /
c
c     check whether a proper joint variable has been specified to be filtered
c
      JntVarFound= .false.
      JntVarNum= 0
      Do while ( (JntVarNum .le. 5) .and. (.not. JntVarFound) )
        JntVarNum= JntVarNum + 1
        if ( CMND.CmndText(3) .eq. JntVarName(JntVarNum) ) JntVarFound= .true.
      end do
      if ( .not. JntVarFound ) goto 1000 ! error
      if ( (JntVarNum .eq. 1) .and. (.not. JF.angle_JCS) )   goto 1010 ! error
      if ( (JntVarNum .eq. 2) .and. (.not. JF.vel_acc_JCS) ) goto 1020 ! error
      if ( (JntVarNum .eq. 3) .and. (.not. JF.angle_EUL) )   goto 1030 ! error
      if ( (JntVarNum .eq. 4) .and. (.not. JF.vel_acc_EUL) ) goto 1040 ! error
      if ( (JntVarNum .eq. 5) .and. (.not. JF.load) )        goto 1080 ! error
      if ( (JntVarNum .eq. 6) .and. (.not. JF.power) )       goto 1090 ! error
      if ( rMtnFrmRate .lt. 1.0e-5 ) goto 1050 ! error  
      if ( MP.nfrm .lt. 2 )       goto 1060 ! error
c
c     Figure out how many zeros must be added to data.  Zeros are added
c     symmetrically to the beginning and end of the array.  This should
c     take care of most of the end effects resulting from fourier transform
c     process.             
c                               
      n= MP.nfrm                                
      i= 1    
      do while( (n - 2**i) .gt. 0 )
        i= i + 1
      end do         
      if ( i .gt. 9 ) goto 1070 ! error- too many samples, must be <=512
      NumFilPnts= 2**i                                                
      NumBeginZero= (NumFilPnts-n)/2
      NumEndZero=   (NumFilPnts-n) - NumBeginZero                          
c                              
c     Build filter function                             
c                                                       
      smplfreq= rMtnFrmRate
      call BuildFilter( CMND,2,NumFilPnts,smplfreq,
     &                  Filter,DoWindow,Error,OF )
      if ( Error ) return ! error                                
c
c     Do filtering of joint variable
c                                              
      do i= 1, GP.num_jnt                      
        jnt= GP.jnt_num(i)
        if ( JntVarNum .le. 4 ) then
          do axis= flex, inex               
            if ( OF.UpDate ) write(6,100) jnt,GP.num_jnt,Jnt_Names(jnt),AxisName(axis)
100         format('+ Filtering joint # ',i2,'/',i2,' ',a4,' ',a4)
c             
c           Filter the proper joint variable quantity
c                                                      
            if ( JntVarNum .eq. 1 ) then ! filter JCS angles
              do j= 1, MP.nfrm              
                TempData(j)= JangJCS(axis,j,jnt)
              end do
              call DigFilterDataSet( TempData,n,
     &                               NumFilPnts,NumBeginZero,NumEndZero,
     &                               Filter,DoWindow )
              do j= 1, MP.nfrm
                JangJCS(axis,j,jnt)= TempData(j)
              end do
              
            else if ( JntVarNum .eq. 2 ) then ! filter JCS vel and accel
              do j= 1, MP.nfrm
                TempData(j)= JvelJCS(axis,j,jnt)
              end do
              call DigFilterDataSet( TempData,n,
     &                               NumFilPnts,NumBeginZero,NumEndZero,
     &                               Filter,DoWindow )        
              do j= 1, MP.nfrm
                JvelJCS(axis,j,jnt)= TempData(j)
              end do
              do j= 1, MP.nfrm
                TempData(j)= JaccJCS(axis,j,jnt)
              end do
              call DigFilterDataSet( TempData,n,
     &                               NumFilPnts,NumBeginZero,NumEndZero,
     &                               Filter,DoWindow )      
              do j= 1, MP.nfrm
                JaccJCS(axis,j,jnt)= TempData(j)
              end do
              
            else if ( JntVarNum .eq. 3 ) then ! filter EUL angles
              do j= 1, MP.nfrm              
                TempData(j)= JangEUL(axis,j,jnt)
              end do
              call DigFilterDataSet( TempData,n,
     &                               NumFilPnts,NumBeginZero,NumEndZero,
     &                               Filter,DoWindow )
              do j= 1, MP.nfrm
                JangEUL(axis,j,jnt)= TempData(j)
              end do
            else if ( JntVarNum .eq. 4 ) then ! filter JCS vel and accel
              do j= 1, MP.nfrm
                TempData(j)= JvelEUL(axis,j,jnt)
              end do
              call DigFilterDataSet( TempData,n,
     &                               NumFilPnts,NumBeginZero,NumEndZero,
     &                               Filter,DoWindow )        
              do j= 1, MP.nfrm
                JvelEUL(axis,j,jnt)= TempData(j)
              end do
              do j= 1, MP.nfrm
                TempData(j)= JaccEUL(axis,j,jnt)
              end do
              call DigFilterDataSet( TempData,n,
     &                               NumFilPnts,NumBeginZero,NumEndZero,
     &                               Filter,DoWindow )      
              do j= 1, MP.nfrm
                JaccEUL(axis,j,jnt)= TempData(j)
              end do                    
            end if
          end do ! axis    
                                   
        else if ( JntVarNum .eq. 5 ) then ! joint loads
          do load= fem, cdf
            if ( OF.UpDate ) write(6,103) jnt,GP.num_jnt,Jnt_Names(jnt),LoadName(load)
103         format('+ Filtering joint # ',i2,'/',i2,' ',a4,' ',a4)
            do j= 1, MP.nfrm
              TempData(j)= JrlJCS(load,j,jnt)
            end do
            call DigFilterDataSet( TempData,n,
     &                               NumFilPnts,NumBeginZero,NumEndZero,
     &                             Filter,DoWindow )        
            do j= 1, MP.nfrm
              JrlJCS(load,j,jnt)= TempData(j)
            end do
            do j= 1, MP.nfrm
              TempData(j)= JrlGCS(load,j,jnt)
            end do
            call DigFilterDataSet( TempData,n,
     &                             NumFilPnts,NumBeginZero,NumEndZero,
     &                             Filter,DoWindow )        
            do j= 1, MP.nfrm
              JrlGCS(load,j,jnt)= TempData(j)
            end do
            do j= 1, MP.nfrm
              TempData(j)= JrlPCS(load,j,jnt)
            end do
            call DigFilterDataSet( TempData,n,
     &                             NumFilPnts,NumBeginZero,NumEndZero,
     &                             Filter,DoWindow )        
            do j= 1, MP.nfrm
              JrlPCS(load,j,jnt)= TempData(j)
            end do
            do j= 1, MP.nfrm
              TempData(j)= JrlDCS(load,j,jnt)
            end do
            call DigFilterDataSet( TempData,n,
     &                             NumFilPnts,NumBeginZero,NumEndZero,
     &                             Filter,DoWindow )        
            do j= 1, MP.nfrm
              JrlDCS(load,j,jnt)= TempData(j)
            end do
          end do ! load
          
        else ! JntVarNum = 6 - joint power
          if ( OF.UpDate ) write(6,104) jnt,GP.num_jnt,Jnt_Names(jnt)
104       format('+ Filtering joint # ',i2,'/',i2,' ',a4)
          do k= 1, 4
            do j= 1, MP.nfrm
              TempData(j)= JntPower(k,j,jnt)
            end do
            call DigFilterDataSet( TempData,n,
     &                             NumFilPnts,NumBeginZero,NumEndZero,
     &                             Filter,DoWindow )        
            do j= 1, MP.nfrm
              JntPower(k,j,jnt)= TempData(j)
            end do
          end do ! k
        end if
      end do ! jnt     
                                            
      if ( (JntVarNum .eq. 5) .and. (JF.legsummed) ) then
        do side= rt, lf
          do load= fem, cdf
            do j= 1, MP.nfrm
              TempData(j)= JRLegJCS(load,j,side)
            end do
            call DigFilterDataSet( TempData,n,
     &                             NumFilPnts,NumBeginZero,NumEndZero,
     &                             Filter,DoWindow )        
            do j= 1, MP.nfrm
              JRLegJCS(load,j,side)= TempData(j)
            end do
            do j= 1, MP.nfrm
              TempData(j)= JRLegGCS(load,j,side)
            end do
            call DigFilterDataSet( TempData,n,
     &                             NumFilPnts,NumBeginZero,NumEndZero,
     &                             Filter,DoWindow )        
            do j= 1, MP.nfrm
              JRLegGCS(load,j,side)= TempData(j)
            end do
          end do ! load
        end do ! side
      end if
      
      if ( (JntVarNum .eq. 6) .and. (JF.legsummed) ) then
        do side= rt, lf
          do k= 1, 4
            do j= 1, MP.nfrm
              TempData(j)= JntPowerLeg(k,j,side)
            end do
            call DigFilterDataSet( TempData,n,
     &                             NumFilPnts,NumBeginZero,NumEndZero,
     &                             Filter,DoWindow )        
            do j= 1, MP.nfrm
              JntPowerLeg(k,j,side)= TempData(j)
            end do
          end do ! k 
        end do ! side
      end if
      if ( JF.NumProcOps .le. 4 ) then
        JF.NumProcOps= JF.NumProcOps + 1
        JF.ProcOp(JF.NumProcOps)= CMND.Text(1)(1:40)
      end if
      return              

1000  write(6,1005) CMND.CmndText(3)
      if ( OF.ToLog ) write(3,1005) CMND.CmndText(3)
1005  format(' Error- ',a3,' is not valid joint variable that can be filtered'/
     &       '         Valid variables are: JAN JVA EAN EVA')
      return
      
1010  write(6,1015) 
      if ( OF.ToLog ) write(3,1015) 
1015  format(' Error- JCS Angles cannot be filtered because they have not'/
     &       '        been calculated yet.')
      return
                           
1020  write(6,1025) 
      if ( OF.ToLog ) write(3,1025) 
1025  format(' Error- JCS vel/accel cannot be filtered because they have not'/
     &       '        been calculated yet.')
      return
      
1030  write(6,1035) 
      if ( OF.ToLog ) write(3,1035) 
1035  format(' Error- Euler Angles cannot be filtered because they have not'/
     &       '        been calculated yet.')
      return
      
1040  write(6,1045) 
      if ( OF.ToLog ) write(3,1045) 
1045  format(' Error- Euler vel/accel cannot be filtered because they have not'/
     &       '        been calculated yet.')
      return
      
1050  write(6,1055) rMtnFrmRate
      if ( OF.ToLog ) write(3,1055) rMtnFrmRate
1055  format(' Error- Sample rate is ',f10.6,' Hz which is too small.'/
     &       '        Something has gone wrong in reading the data file')
      return                
      
1060  write(6,1065) MP.nfrm
      if ( OF.ToLog ) write(3,1065) MP.nfrm
1065  format(' Error- There are only ',i4,' samples of data for each joint'/
     &       '        There must be at least 2 samples to perform filtering')
      return
                            
1070  write(6,1075) MP.nfrm
      if ( OF.ToLog ) write(3,1075) MP.nfrm
1075  format(' Error- There are ',i6,' samples per joint, but at most'/
     &       '        512 samples/joint can be filtered')
      return    

1080  write(6,1085) 
      if ( OF.ToLog ) write(3,1085) 
1085  format(' Error- Joint loads cannot be filtered because they have not'/
     &       '        been calculated yet.')
      return

1090  write(6,1095) 
      if ( OF.ToLog ) write(3,1095) 
1095  format(' Error- Joint powers cannot be filtered because they have not'/
     &       '        been calculated yet.')
      return
      end          
