c                                                   
c                                      
c           
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: This file contains routines to export information describing
c              joint quantities calculated by ANZ into text files. The covers
c              both kinematics and kinetics.
c
c     NOTE: There is a trick in these routines which makes life alot easier when
c           you are dumping all of the joints at once for a specific quantity.
c           Normally, you would have to type in an export command for every joint
c           you wanted to export. However, by including a wild card, *, in the 
c           export command all the joints can be dumped in one command. For example,
c
c           EXP JNT JAN RTAK RAK_FILE.TXT  : dumps only the right ankle angles
c           EXP JNT JAN ALL _FILE.TXT      : dumps all joint angles
c
c           The file names in this example will be RTAK_FILE.TXT,RTKN_FILE.TXT and so on.
c           You can change where the joint name is inserted by placing a wild card in
c           the file name of the command:
c
c           EXP JNT JAN ALL BOB_*.JAN
c
c           This will produce files with the names BOB_RTAK.JAN, BOB_RTKN.JAN, etc.
c
      subroutine ExportJnt( CMND,inc,RegHeader,IgorHeader,
     &                      JF,SF,CF,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_seg.inc'
      include 'a_enr.inc'
                 
      record /CmndLine/     CMND
      record /joint_flag/   JF
      record /segment_flag/ SF
      record /cycle_flag/   CF
      record /option_flag/  OF

      integer*4 inc
      logical*4 IgorHeader,RegHeader

      integer*4 JntNum,frm,i,j,k,CSType,jnt,NumOpt,NumExpJntVar,Start,
     &          ExpVarNumCmnd(16),ExpVarNum,NumCmnd,Side,ierr,NumJnt,
     &          AllJntNum(MAX_JNT),End
      real*4    LegLength(2)
      character LineText*80,JntText*3,OptText(5)*3,
     &          ExpJntVarName(16)*3,Filename(15)*40
      logical*4 GetJntNum,OpenTextFile,GetCSType,UsUnits,NormWt,
     &          NormWtHt,NormWtLeg,AllJnt,Error,Upper,Lower,NormVAA,
     &          Animate,JntOK,ExpSIMM
                 
      data NumExpJntVar / 16 /
      data ExpJntVarName / 'JAN','JVA','EAN','EVA','LOA',
     &                     'LLD','WRE','POW','LPW','FSC',
     &                     'ISC','SAN','TRN','MOA','WRA','REF' /
      data ExpVarNumCmnd / 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,4 /
                 
c
c     Check validity of export quantity name
c
      Error= .true.
      do i= 1, NumExpJntVar
        if ( CMND.CmndText(3) .eq. ExpJntVarName(i) ) then
          Error= .false.
          ExpVarNum= i
        end if
      end do
      if ( Error ) goto 1000 ! bad name
c
c     Now check options if present
c
      NormWt=    .false.
      NormWtHt=  .false.
      NormWtLeg= .false.
      UsUnits=   .false.
      NormVAA=   .false.   
      Animate=   .false.
      ExpSIMM=   .false.
      CSType= JCS_CS
      if ( (ExpVarNum .eq. 14) .or. (ExpVarNum .eq. 15) ) CSType= PRO_CS
      if ( CMND.NumOpt(3) .gt. 0 ) then
        NumOpt= CMND.NumOpt(3)            ! stupid trick needed to make mac 
        do i= 1, CMND.NumOpt(3)           ! version work correctly !!!!
          OptText(i)= CMND.OptText(i,3)
        end do
        do i= 1, CMND.NumOpt(3)
          if ( ((ExpVarNum .eq. 5) .or. (ExpVarNum .eq. 6) .or.
     &          (ExpVarNum .eq. 8) .or. (ExpVarNum .eq. 9)     ) .and.
     &         ((OptText(i) .eq. 'NWT') .or. (OptText(i) .eq. 'NWH') .or.
     &          (OptText(i) .eq. 'NWL')) ) then
            if ( OptText(i) .eq. 'NWT' ) then
              if ( GP.weight .lt. 1.0e-5 ) goto 1280 ! error
              NormWt=    .true.
              NormWtHt=  .false.
              NormWtLeg= .false.
            else if ( OptText(i) .eq. 'NWH' ) then
              if ( GP.weight .lt. 1.0e-5 ) goto 1280 ! error
              if ( GP.height .lt. 1.0e-5 ) goto 1290 ! error
              NormWt=    .false.
              NormWtHt=  .true.
              NormWtLeg= .false.
            else if ( OptText(i) .eq. 'NWL' ) then
              if ( GP.weight .lt. 1.0e-5 ) goto 1280 ! error
              if ( .not. SF.param ) goto 1320 ! error
              if ( (SP(rtcf).length .lt. 1.0e-5) .or.
     &             (SP(rtth).length .lt. 1.0e-5) .or.
     &             (SP(lfcf).length .lt. 1.0e-5) .or.
     &             (SP(lfth).length .lt. 1.0e-5)      ) goto 1310 ! error
              LegLength(rt)= SP(rtcf).length + SP(rtth).length
              LegLength(lf)= SP(lfcf).length + SP(lfth).length
              NormWt=    .false.             
              NormWtHt=  .false.
              NormWtLeg= .true.
            end if
          else if ( ( (EXPVarNum .eq. 2) .or.
     &                (EXPVarNum .eq. 5) .or.
     &                (EXPVarNum .eq. 8)     ) .and.
     &                ((OptText(i) .eq. 'ANI') .or.
     &	               (OptText(i) .eq. 'VIS') .or.
     &                 (OptText(i) .eq. 'SIM')     ) ) then
            Animate= .true.
            if (OptText(i) .eq. 'SIM') ExpSIMM= .true.
          else if ( ((ExpVarNum .eq. 5) .or. (ExpVarNum .eq. 6) .or.
     &               (ExpVarNum .eq. 8) .or. (ExpVarNum .eq. 9)     ) .and.
     &              (OptText(i) .eq. 'LBS') ) then
            UsUnits= .true.
          else if ( ((ExpVarNum .eq. 2) .or. (ExpVarNum .eq. 4)) .and.
     &              (OptText(i) .eq. 'NOR') ) then
            NormVAA= .true.
          else if ( (ExpVarNum .eq. 5)  .or. (ExpVarNum .eq. 6)  .or.
     &              (ExpVarNum .eq. 12) .or. (ExpVarNum .eq. 13) .or.
     &              (ExpVarNum .eq. 14) .or. (ExpVarNum .eq. 15)  ) then    
            if ( .not. GetCSType( NumOpt,OptText,CSType) ) goto 1020
            if ( (CSType .eq. EUL_CS) .or. 
     &           (CSType .eq. LCS_CS)      ) goto 1020
            if ( (ExpVarNum .eq. 14) .or. (ExpVarNum .eq. 15) ) then
              if ( CSType .eq. JCS_CS ) goto 1030
            else if ( ExpVarNum .eq. 12 ) then
              if ( CSType .eq. GCS_CS ) goto 1040
            else if ( ExpVarNum .eq. 6 ) then                       
              if ( (CSType .eq. PRO_CS) .or. 
     &             (CSType .eq. DIS_CS) ) goto 1050
            end if
          else
            goto 1060 ! error - no options valid
          end if
        end do
      end if                                         
c
c     Figure out which joint will be exported
c
      NumCmnd= ExpVarNumCmnd(ExpVarNum)
c      if ( Animate ) NumCmnd= NumCmnd - 1
      if ( CMND.NumCmnd .lt. NumCmnd ) goto 1070 ! error
      AllJnt= .false.
      Lower=  .false.
      Upper=  .false.
      if ( (ExpVarNum .eq. 6)  .or. (ExpVarNum .eq. 9) ) then 
        if ( CMND.CmndText(NumCmnd-1)(1:2) .eq. 'RT' ) then
          Side= rt
        else if ( CMND.CmndText(NumCmnd-1)(1:2) .eq. 'LF' ) then
          Side= lf
        else
          goto 1080 ! error
        end if
      else if ( ( (CMND.CmndText(NumCmnd-1) .eq. 'ANI') .or.
     &            (CMND.CmndText(NumCmnd-1) .eq. 'VIS') .or.
     &            (CMND.CmndText(NumCmnd-1) .eq. 'SIM')     ) .and.
     &          ((ExpVarNum .eq. 2) .or. (EXPVarNum .eq. 5) .or.
     &           (EXPVarNum .eq. 8))  ) then
        Animate= .true.  
        if (CMND.CmndText(NumCmnd-1) .eq. 'SIM') ExpSIMM= .true.
      else if ( ExpVarNum .lt. 16 ) then
        if ( CMND.CmndText(NumCmnd-1) .eq. 'ALL' ) then
          AllJnt= .true.                 
        else if ( ((ExpVarNum .eq. 1) .or. (ExpVarNum .eq. 3)) .and.
     &            (CMND.CmndText(NumCmnd-1) .eq. 'LOW') ) then
          Lower= .true.
          Upper= .false.
        else if ( (ExpVarNum .eq. 1) .and.
     &            (CMND.CmndText(NumCmnd-1) .eq. 'UPP') ) then
          Lower= .false.
          Upper= .true.
        else
          JntText= CMND.CmndText(NumCmnd-1)
          JntOK= GetJntNum( JntText,JntNum )
          if ( (JntText .ne. 'SIM') .and. (.not. JntOK) ) goto 1090
        end if
      end if
c
c     Open Text output file...
c
      LineText= CMND.Text(1)  
      Start= CMND.CmndStart(NumCmnd)
      if ( AllJnt ) then
        End=   CMND.CmndEnd(NumCmnd)
        NumJnt= GP.num_jnt
        do i= 1, NumJnt
          AllJntNum(i)= GP.jnt_num(i)
        end do
        call BuildFileNames( LineText,Start,End,jnt_names,
     &                       NumJnt,AllJntNum,FileName     )
      else
        if ( .not. OpenTextFile(LineText,Start,.true.) ) goto 2010 ! error
      end if
c
c     Now dump whatever has been requested
c
      if ( ExpVarNum .eq. 1 ) then ! JCS joint angle
        if ( .not. AllJnt ) then
          if ( JntText .eq. 'SIM' ) then
            call ExpJntJCSAngSIMM( inc,JF,SF,OF,error )
          else
            call ExpJntJCSAng( inc,RegHeader,IgorHeader,Lower,Upper,
     &                         JntNum,JF,SF,OF,error )
          end if
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntJCSAng( inc,RegHeader,IgorHeader,Lower,Upper,
     &                         JntNum,JF,SF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 2 ) then ! JCS joint velocity/acceleration
        if ( Animate ) then
          call ExpJntVelAnimate( CMND,inc,ExpSIMM,JF,OF,error )
          close(unit=2)
          if ( error ) return

        else if ( .not. AllJnt ) then
          call ExpJntJCSVAA( inc,RegHeader,IgorHeader,NormVAA,
     &                       JntNum,JF,CF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntJCSVAA( inc,RegHeader,IgorHeader,NormVAA,
     &                         JntNum,JF,CF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 3 ) then ! Euler joint angle
        if ( (.not. AllJnt) .or. Lower ) then
          call ExpJntEulAng( inc,RegHeader,IgorHeader,Lower,
     &                       JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntEulAng( inc,RegHeader,IgorHeader,Lower,
     &                         JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 4 ) then ! Euler joint velocity/acceleration
        if ( .not. AllJnt ) then
          call ExpJntEulVAA( inc,RegHeader,IgorHeader,NormVAA,
     &                       JntNum,JF,CF,OF,error )
          close(unit=2)
          if ( error ) return
        else      
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntEulVAA( inc,RegHeader,IgorHeader,NormVAA,
     &                         JntNum,JF,CF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if
 
      else if ( ExpVarNum .eq. 5 ) then ! Joint Load
        if ( Animate ) then
          call ExpJntLoadAnimate( inc,ExpSIMM,JF,OF,error )
          close(unit=2)
          if ( error ) return
          
        else if ( .not. AllJnt ) then
          call ExpJntLoad( inc,RegHeader,IgorHeader,CSType,
     &                     USUnits,NormWt,NormWtHt,NormWtLeg,
     &                     LegLength,JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return

        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntLoad( inc,RegHeader,IgorHeader,CSType,
     &                       USUnits,NormWt,NormWtHt,NormWtLeg,
     &                       LegLength,JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if
        
      else if ( ExpVarNum .eq. 6 ) then ! Summed Leg Load
        call ExpJntLegLoad( inc,RegHeader,IgorHeader,CSType,
     &                      USUnits,NormWt,NormWtHt,NormWtLeg,
     &                      LegLength,Side,JF,OF,error )
        close(unit=2)
        if ( error ) return

      else if ( ExpVarNum .eq. 7 ) then ! Joint load wrench
        if ( .not. AllJnt ) then
          call ExpJntWre( inc,RegHeader,IgorHeader,
     &                    JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntWre( inc,RegHeader,IgorHeader,
     &                      JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 8 ) then ! Joint Power Transfer
        if ( .not. AllJnt ) then
          call ExpJntPower( inc,RegHeader,IgorHeader,
     &                      USUnits,NormWt,NormWtHt,NormWtLeg,
     &                      LegLength,JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntPower( inc,RegHeader,IgorHeader,
     &                        USUnits,NormWt,NormWtHt,NormWtLeg,
     &                        LegLength,JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 9 ) then ! Summed Leg Power Transfer
        call ExpJntLegPower( inc,RegHeader,IgorHeader,
     &                       USUnits,NormWt,NormWtHt,NormWtLeg,
     &                       LegLength,Side,JF,OF,error )
        close(unit=2)
        if ( error ) return

      else if ( ExpVarNum .eq. 10 ) then ! Joint finite screw
        if ( .not. AllJnt ) then
          call ExpJntFinScrew( inc,RegHeader,IgorHeader,
     &                         JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntFinScrew( inc,RegHeader,IgorHeader,
     &                           JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 11 ) then ! Joint instantaneous screw
        if ( .not. AllJnt ) then
          call ExpJntInsScrew( inc,RegHeader,IgorHeader,
     &                         JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntInsScrew( inc,RegHeader,IgorHeader,
     &                           JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 12 ) then ! Joint finite screw angle
        if ( .not. AllJnt ) then
          call ExpJntScrewAng( inc,RegHeader,IgorHeader,CSType,
     &                         JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntScrewAng( inc,RegHeader,IgorHeader,CSType,
     &                           JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 13 ) then ! Joint translation
        if ( .not. AllJnt ) then
          call ExpJntTrans( inc,RegHeader,IgorHeader,CSType,
     &                      JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJntTrans( inc,RegHeader,IgorHeader,CSType,
     &                        JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 14 ) then ! Joint Moment Arm
        if ( .not. AllJnt ) then
          call ExpJma( inc,RegHeader,IgorHeader,CSType,
     &                       JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJma( inc,RegHeader,IgorHeader,CSType,
     &                         JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if

      else if ( ExpVarNum .eq. 15 ) then ! Joint Wrench Arm
        if ( .not. AllJnt ) then
          call ExpJwa( inc,RegHeader,IgorHeader,CSType,
     &                       JntNum,JF,OF,error )
          close(unit=2)
          if ( error ) return
        else
          do i= 1, GP.num_jnt
            JntNum= GP.jnt_num(i)
            open( unit=2,file=filename(i),status='unknown',iostat=ierr,err=2000 )
            call ExpJwa( inc,RegHeader,IgorHeader,CSType,
     &                         JntNum,JF,OF,error )
            close(unit=2)
            if ( error ) return
          end do
        end if
        
      else if ( ExpVarNum .eq. 16 ) then ! Joint Angle Reference correction
        call ExpJntAngRefCorAng( JF,OF,error )
        close(unit=2)
        if ( error ) return
        
      end if
      return

1000  write(6,1005) (ExpJntVarName(i),i=1,NumExpJntVar)
      if ( OF.ToLog ) write(3,1005) (ExpJntVarName(i),i=1,NumExpJntVar)
1005  format(' Error- Invalid EXPort JoiNT command'/
     &       '        Valid commands are: ',5a5/2(28x,5a5/))
      return

1020  write(6,1025) OptText(i)               
      if ( OF.ToLog ) write(3,1025) OptText(i)
1025  format(' Error- ',a3,' is not a valid coordinate system name'/
     &       '        Valid coordinate systems are: GCS PRO DIS JCS')        
      return

1030  write(6,1035) OptText(i)               
      if ( OF.ToLog ) write(3,1035) OptText(i)
1035  format(' Error- ',a3,' is not a valid coord System for Moment/Wrench Arm'/
     &       '        Valid coordinate systems are: GCS PRO DIS')        
      return
      
1040  write(6,1045) OptText(i)               
      if ( OF.ToLog ) write(3,1045) OptText(i)
1045  format(' Error- ',a3,' is not a valid coord System for Jnt Trans'/
     &       '        Valid coordinate systems are: JCS PRO DIS')        
      return
      
1050  write(6,1055) OptText(i)               
      if ( OF.ToLog ) write(3,1055) OptText(i)
1055  format(' Error- ',a3,' is not a valid coord System for Leg loads'/
     &       '        Valid coordinate systems are: JCS GCS')        
      return
      
1060  write(6,1065) OptText(i),CMND.CmndText(3)
      if ( OF.ToLog ) write(3,1065) OptText(i),CMND.CmndText(3)
1065  format(' Error- ',a3,' is not a valid option for EXPort JoiNT ',a3/
     &       '        Valid options are:'/
     &       9x,'JVA, EVA : NOR, VIS'/
     &       9x,'LOA      : (GCS,JCS,PRO,DIS), (NWT,NWH,NWL), VIS'/
     &       9x,'LLD      : (GCS,JCS), (NWT,NWH,NWL)'/
     &       9x,'POW, LPW : (NWT,NWH,NWL)'/
     &       9x,'SAN      : (JCS,PRO,DIS)'/
     &       9x,'TRN      : (GCS,JCS,PRO,DIS)'/
     &       9x,'MOA, WRA : (GCS,PRO,DIS)')        
      return
        
1070  write(6,1075) CMND.NumCmnd,NumCmnd
      if ( OF.ToLog ) write(6,1075) CMND.NumCmnd,NumCmnd
1075  format(' Error- There are only ',i2,' commands present for EXP JNT'/
     &       '        but there must be ',i2,' commands for this quantity')
      return

1080  write(6,1085) CMND.CmndText(NumCmnd-1)
      if ( OF.ToLog ) write(6,1085) CMND.CmndText(NumCmnd-1)
1085  format(' Error- ',a3,' is not a valid side name'/
     &       '        Valid sides are: RighT LeFt')
      return

1090  write(6,1095) JntText,(jnt_names(GP.jnt_num(i)),
     &                       i= 1, GP.num_jnt)
      if ( OF.ToLog ) write(6,1095) JntText,
     &                          (jnt_names(GP.jnt_num(i)),
     &                           i= 1, GP.num_jnt)
1095  format(' Error- ',a3,' is not a valid joint name'/
     &       '        Valid joint names are : ALL - Most Quantities'/
     &       32x,' UPPer LOWer - Only for JAN'/
     &       3(32x,5a5/))
      return

2000  write(6,2005) filename(i),ierr
      if ( OF.ToLog ) write(3,2005) filename(i),ierr
2005  format(' Error- Trouble opening ',a40/
     &       '        Error # ',i4,' occured')        
      return

2010  write(6,2015)                 
      if ( OF.ToLog ) write(3,2015)
2015  format(' Error- File could not be opened')        
      return

1280  write(6,1285) GP.weight
      if ( OF.ToLog ) write(3,1285) GP.weight
1285  format(' Error- Normaization requires a patient weight > 0.0'/
     &       '        In this case the weight is ',f10.4)
      return

1290  write(6,1295) GP.height
      if ( OF.ToLog ) write(3,1295) GP.height
1295  format(' Error- Normaization requires a patient height > 0.0'/
     &       '        In this case the height is ',f10.4)
      return

1310  write(6,1315) 
      if ( OF.ToLog ) write(3,1315) 
1315  format(' Error- Weight*Leg length normalization requires the calf and '/
     &       '        thigh of both legs to be > 0.0 which they are not')
      return

1320  write(6,1325) 
      if ( OF.ToLog ) write(3,1325) 
1325  format(' Error- Weight*Leg length normalization requires the segment'/
     &       '        parameters to have been calculated which they have not')
      return
      end                  
c
c                                        
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ConvertNormJntLoad( JntLoad,Jnt,UsUnits,
     &                               NormWt,NormWtHt,NormWtLeg,
     &                               Weight,Height,LegLength,ConvLoad )
      implicit none

      integer*4 Jnt
      real*4    JntLoad(6),ConvLoad(6),LegLength(2),Weight,Height
      logical*4 UsUnits,NormWt,NormWtHt,NormWtLeg

      real*4    Lbs,In,RtJnt(6),LfJnt(6),Gravity
      integer*4 i
      logical*4 RightSide,LeftSide
 
      data Lbs,In,Gravity / 0.22481,39.370,9.871 / ! lbs/N, in/m, m/s^2
      data RtJnt / 1,2,3,10,11,14 / ! rtft,rtcf,rtth,rtua,rtla,rthd
      data LfJnt / 4,5,6,12,13,15 / ! lfft,lfcf,lfth,lfua,lfla,lfhd
 
      if ( NormWt ) then
        do i= 1, 6
          ConvLoad(i)= JntLoad(i)/(Weight*Gravity)
        end do
        if ( UsUnits ) then
          do i= 4, 6
            ConvLoad(i)= ConvLoad(i)*In
          end do
        end if
      else if ( NormWtHt ) then
        do i= 1, 6
          ConvLoad(i)= JntLoad(i)/(Weight*Gravity*Height)
        end do
        if ( UsUnits ) then
          do i= 1,3
            ConvLoad(i)= ConvLoad(i)/In
          end do
        end if
      else if ( NormWtLeg ) then
        RightSide= .false.
        LeftSide=  .false.
        do i= 1, 6
          if ( Jnt .eq. RtJnt(i) ) RightSide= .true.
          if ( Jnt .eq. LfJnt(i) ) LeftSide=  .true.
        end do
        if ( RightSide ) then
          do i= 1, 6
            ConvLoad(i)= JntLoad(i)/(Weight*Gravity*LegLength(1))
          end do
        else if ( LeftSide ) then
          do i= 1, 6
            ConvLoad(i)= JntLoad(i)/(Weight*Gravity*LegLength(2))
          end do
        else ! neither side - use average
          do i= 1, 6
            ConvLoad(i)= 2.0*JntLoad(i)/
     &                   (Weight*Gravity*(LegLength(1)+LegLength(2)))
          end do
        end if
        if ( UsUnits ) then
          do i= 1,3
            ConvLoad(i)= ConvLoad(i)/In
          end do
        end if
      else ! no normalization
        if ( UsUnits ) then
          do i= 1, 3
            ConvLoad(i)=   JntLoad(i)*Lbs
            ConvLoad(i+3)= JntLoad(i+3)*Lbs*In
          end do
        else
          do i= 1, 6
            ConvLoad(i)= JntLoad(i)
          end do
        end if
      end if ! normalize
      return
      end
c
c                                        
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ConvertNormJntPower( JntPower,Jnt,UsUnits,
     &                                NormWt,NormWtHt,NormWtLeg,
     &                                Weight,Height,LegLength,ConvPower )
      implicit none

      integer*4 Jnt
      real*4    JntPower(4),ConvPower(4),LegLength(2),Weight,Height
      logical*4 UsUnits,NormWt,NormWtHt,NormWtLeg

      real*4    Lbs,In,RtJnt(6),LfJnt(6),Gravity
      integer*4 i
      logical*4 RightSide,LeftSide
 
      data Lbs,In,Gravity / 0.22481,39.370,9.871 / ! lbs/N, in/m, m/s^2
      data RtJnt / 1,2,3,10,11,14 / ! rtft,rtcf,rtth,rtua,rtla,rthd
      data LfJnt / 4,5,6,12,13,15 / ! lfft,lfcf,lfth,lfua,lfla,lfhd
 
      if ( NormWt ) then
        do i= 1, 4
          ConvPower(i)= JntPower(i)/(Weight*Gravity)
        end do
        if ( UsUnits ) then
          do i= 1, 4
            ConvPower(i)= ConvPower(i)*In
          end do
        end if
      else if ( NormWtHt ) then
        do i= 1, 4
          ConvPower(i)= JntPower(i)/(Weight*Gravity*Height)
        end do
      else if ( NormWtLeg ) then
        RightSide= .false.
        LeftSide=  .false.
        do i= 1, 4
          if ( Jnt .eq. RtJnt(i) ) RightSide= .true.
          if ( Jnt .eq. LfJnt(i) ) LeftSide=  .true.
        end do
        if ( RightSide ) then
          do i= 1, 4
            ConvPower(i)= JntPower(i)/(Weight*Gravity*LegLength(1))
          end do
        else if ( LeftSide ) then
          do i= 1, 4
            ConvPower(i)= JntPower(i)/(Weight*Gravity*LegLength(2))
          end do
        else ! neither side - use average
          do i= 1, 4
            ConvPower(i)= 2.0*JntPower(i)/
     &                    (Weight*Gravity*(LegLength(1)+LegLength(2)))
          end do
        end if
      else ! no normalization
        if ( UsUnits ) then
          do i= 1, 4
            ConvPower(i)= JntPower(i)*Lbs*In
          end do
        else
          do i= 1, 4
            ConvPower(i)= JntPower(i)
          end do
        end if
      end if ! normalize
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntJCSAng( inc,RegHeader,IgorHeader,Lower,Upper,
     &                         JntNum,JF,SF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      include 'a_seg.inc'
      
      record /joint_flag/   JF
      record /segment_flag/ SF
      record /option_flag/  OF
      
      integer*4 JntNum,inc
      logical*4 Error,RegHeader,IgorHeader,Lower,Upper

      real*4    TimeInc
      integer*4 frm,i,j,k
      
      Error= .true.
      if ( Upper ) then ! Special case for exporting for postprocessing
        if ( GP.analysis_type .ne. UPR_ANL ) goto 1340
        if ( .not. JF.angle_JCS ) goto 1020 ! error
        if ( .not. SF.angle ) goto 1350 ! error
        if ( IgorHeader ) goto 1030 ! error
        if ( GP.num_jnt .eq. 4 ) then
          do i= 1, MP.nfrm,inc        
            write(2,399) i,(JangJCS(j,i,rtel),j=1,3),
     &                     (JangJCS(j,i,rtsh),j=1,3),
     &                     (SangGCS(j,i,trnk),j=1,3)
399           format(i3,9(2x,f10.4))
          end do
        else  
          do i= 1, MP.nfrm,inc        
            write(2,400) i,(JangJCS(j,i,rtel),j=1,3),
     &                     (JangJCS(j,i,rtsh),j=1,3),
     &                     (JangJCS(j,i,neck),j=1,3),
     &                     (SangGCS(j,i,trnk),j=1,3)
400         format(i3,12(2x,f10.4))
          end do
        end if
        
      else if ( Lower ) then ! another special postprocessing case
        if ( GP.analysis_type .eq. UPR_ANL ) goto 1360
        if ( .not. JF.angle_JCS ) goto 1020 ! error
        if ( IgorHeader ) goto 1030 ! error
        if ( RegHeader ) write(2,410)
410     format( ' JCS angles for rtak,rtkn,rtkn,lfak,lfkn,lfhp,pvlb respectively'/
     &          ' Angle order is flexion(+)/extension(-)'/
     &          '                adduction(+)/abduction(i)'/
     &          '                internal(+)/external(-)'/
     &          ' For Pelvis angles:  flexion (front lowers +)'/
     &          '                     obliquity (left side rises +)'/
     &          '                     rotation (right side forward +)'//
     &          ' Gait Events :  RHS = 1   RTO = 3'/
     &          '                LHS = 2   LTO = 4'//
     &          ' Frame  Event   Flex/Ext     Ad/Ab        Int/Ext...'/)
        do i= 1, MP.nfrm,inc                
          write(2,420) i,GaitEvent(i),((JangJCS(j,i,k),j=1,3),k=1,7)
420       format(' ',i4,i3,21(f11.5))
        end do
        
      else ! regular single joint export
        if ( .not. JF.angle_JCS ) goto 1020 ! error
        if ( IgorHeader ) then
          TimeInc= float(inc)/rMtnFrmRate
          write(2,100) (Jnt_Names(JntNum),i=1,3)
100       format(' IGOR'/
     &           ' WAVES ',a4,'_JanFe, ',a4,'_JanAA, ',a4,'_JanIe'/
     &           ' BEGIN')
          do frm= 1, MP.nfrm, inc
            write(2,110) (JangJCS(i,frm,JntNum),i=1,3)
110         format(2x,3(f12.5))
          end do
          write(2,119)
119       format(' END')
          do i= 1, 3
            write(2,120) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
          end do
120       format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JanFe'/
     &           ' X SetScale y 0,0,"deg", ',a4,'_JanFe'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JanAa'/
     &           ' X SetScale y 0,0,"deg", ',a4,'_JanAa'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JanIe'/
     &           ' X SetScale y 0,0,"deg", ',a4,'_JanIe')
        else
          if ( RegHeader ) write(2,200) Jnt_Names(JntNum)
200       format(' JCS angles for ',a4//
     &           ' Angle order is flexion(+)/extension(-)'/
     &           '                adduction(+)/abduction(i)'/
     &           '                internal(+)/external(-)'/
     &           ' For Pelvis angles:  flexion (front lowers +)'/
     &           '                     obliquity (left side rises +)'/
     &           '                     rotation (right side forward +)'//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &           ' Frame  Event   Flex/Ext     Ad/Ab        Int/Ext'/)
          do frm= 1, MP.nfrm, inc
            write(2,210) frm,GaitEvent(frm),(JangJCS(i,frm,JntNum),i=1,3)
210         format(2x,i3,5x,i1,3(f12.5))
          end do
        end if ! Igor or Regular
      end if ! regular joint or upper or lower extremity
      Error= .false.
      return

1020  write(6,1025)      
      if ( OF.ToLog ) write(3,1025)
1025  format(' Error- JCS joint angles have not been calculated yet')        
      return
      
1030  write(6,1035)      
      if ( OF.ToLog ) write(3,1035)
1035  format(' Error- Igor header option is only valid for normal'/
     &       '        JCS joint angle export not for the special'/
     &       '        cases of UPPer or LOWer')        
      return
      
1340  write(6,1345) 
      if ( OF.ToLog ) write(3,1345) 
1345  format(' Error- For EXP JNT JAN UPP the analysis type must be set'/
     &       '        to upper body')
      return
      
1350  write(6,1355) 
      if ( OF.ToLog ) write(3,1355) 
1355  format(' Error- Segment angles (SEG ANG) have not been calculated yet')
      return

1360  write(6,1365) 
      if ( OF.ToLog ) write(3,1365) 
1365  format(' Error- Cannot export lower joint angles when analysis type is'
     &       '        set to upper extremity marker configuration')
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntJCSAngSIMM( inc,JF,SF,OF,error )
      implicit none

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

      real*4    TimeInc,LabCS(3,3),plvang(3)
      integer*4 frm,i,j,k,ierr
                             
      data LabCS / 1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0 /
                           
      Error= .true.
      if ( .not. JF.angle_JCS ) goto 1020 ! error
      write(2,90) MP.nfrm/inc,float(MP.nfrm)
90    format(' #include <simmkeys.h>'//
     &       ' name gait_cycle'/' datacolumns 13'/' datarows ',i3/
     &       ' range 0.0 ',f8.3/' keys g_key'/' wrap'/' endheader'//)
      write(2,100)                                    
100   format(1x,'plv_tx',5x,'plv_ty',5x,'plv_tz',5x,'plv_rx',5x,'plv_ry',5x,
     &          'plv_rz',5x,'mtp_angle',5x,'ankle_angle',5x,'subt_angle',5x,
     &          'knee_angle',5x,'hip_flexion',5x,'hip_adduction',5x,
     &          'hip_rotation'//)
      do frm= 1, MP.nfrm, inc
        call EulerAnglesXZY( LabCS,SgpGCS(1,1,frm,pelv),plvang,ierr )
        if ( ierr .ne. 0 ) goto 1030
        write(2,110) SgpGCS(1,4,frm,pelv),SgpGCS(3,4,frm,pelv),
     &               -SgpGCS(2,4,frm,pelv),
     &               plvang(1),plvang(2),plvang(3),
     &               0.0,JangJCS(1,frm,rtak),
     &               JangJCS(3,frm,rtak),-JangJCS(1,frm,rtkn),
     &               (JangJCS(i,frm,rthp),i=1,3)
110     format(1x,13(f10.5,1x))
      end do
      Error= .false.                                  
      return

1020  write(6,1025)      
      if ( OF.ToLog ) write(3,1025)
1025  format(' Error- JCS joint angles have not been calculated yet')        
      return
      
1030  write(6,1035) ierr,frm
      if ( OF.ToLog ) write(3,1035) ierr,frm
1035  format(' Error- Error number ',i2,' occurred at frame ',i3,' while'/
     &       '        calculating pelvis euler angles')
      return            
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntJCSVAA( inc,RegHeader,IgorHeader,Norm,
     &                         JntNum,JF,CF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /cycle_flag/   CF
      record /option_flag/  OF
      
      integer*4 JntNum,inc
      logical*4 Error,RegHeader,IgorHeader,Norm

      real*4    TimeInc,CycleTime
      integer*4 frm,i,j
      
      Error= .true.
      if ( .not. JF.vel_acc_JCS ) goto 1040 ! error
      if ( Norm ) then
        if ( .not. CF.event ) goto 2020 ! error
        if ( rMtnFrmRate .le. 1.0e-5 ) goto 2030 ! error
        CycleTime= float(MP.cycle_length(1))/rMtnFrmRate
      end if
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        write(2,100) (Jnt_Names(JntNum),i=1,6)
100     format(' IGOR'/
     &         ' WAVES ',a4,'_JvlFe, ',a4,'_JvlAa, ',a4,'_JvlIe, ',
     &                   a4,'_JacFe, ',a4,'_JacAa, ',a4,'_JacIe'/
     &         ' BEGIN')
        if ( .not. Norm ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JvelJCS(i,frm,JntNum),i=1,3),
     &                   (JaccJCS(i,frm,JntNum),i=1,3)
110         format(1x,6(2x,e13.5))             
          end do
          write(2,119)
119       format(' END')
          do i= 1, 6
            write(2,120) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
          end do
120       format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JvlFe'/
     &           ' X SetScale y 0,0,"d/s", ',a4,'_JvlFe'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JvlAa'/
     &           ' X SetScale y 0,0,"d/s", ',a4,'_JvlAa'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JvlIe'/
     &           ' X SetScale y 0,0,"d/s", ',a4,'_JvlIe'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JacFe'/
     &           ' X SetScale y 0,0,"ds2", ',a4,'_JacFe'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JacAa'/
     &           ' X SetScale y 0,0,"ds2", ',a4,'_JacAa'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JacIe'/
     &           ' X SetScale y 0,0,"ds2", ',a4,'_JacIe')
        else ! Normalize Vel/Acc
          do frm= 1, MP.nfrm, inc
            write(2,110) (JvelJCS(i,frm,JntNum)/CycleTime,i=1,3),
     &                   (JaccJCS(i,frm,JntNum)/(CycleTime**2),i=1,3)
          end do
          write(2,119)
          do i= 1, 6
            write(2,130) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
          end do
130       format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JvlFen'/
     &           ' X SetScale y 0,0,"d/%", ',a4,'_JvlFen'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JvlAan'/
     &           ' X SetScale y 0,0,"d/%", ',a4,'_JvlAan'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JvlIen'/
     &           ' X SetScale y 0,0,"d/%", ',a4,'_JvlIen'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JacFen'/
     &           ' X SetScale y 0,0,"d%2", ',a4,'_JacFen'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JacAan'/
     &           ' X SetScale y 0,0,"d%2", ',a4,'_JacAan'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JacIen'/
     &           ' X SetScale y 0,0,"d%2", ',a4,'_JacIen')
        end if
      else ! Regular Export
        if ( .not. Norm ) then
          if ( RegHeader ) write(2,140) Jnt_Names(JntNum)
140       format(' JCS velocities (deg/sec) and '/
     &           '     accelerations (deg/sec^2) for ',a4//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &           ' Frame  Gait',18x,'Velocity',26x,'Acceleration'/
     &           '   #    Event',2('    Flex/Ext      Ab/Ad       Int/Ext')/)
          do frm= 1, MP.nfrm, inc
            write(2,150) frm,GaitEvent(frm),(JvelJCS(i,frm,JntNum),i=1,3),
     &                                  (JaccJCS(i,frm,JntNum),i=1,3)
150         format(1x,i3,5x,i2,2x,6(2x,f10.3))             
          end do                
        else ! Normalize to persent cycle
          if ( RegHeader ) write(2,160) Jnt_Names(JntNum)
160       format(' JCS velocities (deg/%cycle) and '/
     &           '     accelerations (deg/%cycle^2) for ',a4//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &           ' Frame  Gait',18x,'Velocity',26x,'Acceleration'/
     &           '   #    Event',2('    Flex/Ext      Ab/Ad       Int/Ext')/)
          do frm= 1, MP.nfrm, inc
            write(2,150) frm,GaitEvent(frm),
     &                   (JvelJCS(i,frm,JntNum)/CycleTime,i=1,3),
     &                   (JaccJCS(i,frm,JntNum)/(CycleTime**2),i=1,3)
          end do                
        end if                                     
      end if ! Igor or Regular
      Error= .false.
      return

1040  write(6,1045)      
      if ( OF.ToLog ) write(3,1045)
1045  format(' Error- JCS joint vel/accel has not been calculated yet')
      return

2020  write(6,2025) 
      if ( OF.ToLog ) write(3,2025)
2025  format(' Error- Normalization of the joint vel/accel is not possible'/
     &       '        because the gait events have not been input')
      return

2030  write(6,2035) 
      if ( OF.ToLog ) write(3,2035) 
2035  format(' Error- Normalization of the joint vel/accel is not possible'/
     &       '        because the motion frame rate is <= 0.0')
      return
      end
c
c
c
      subroutine ExpJntVelAnimate( CMND,inc,ExpSIMM,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_seg.inc'
      include 'a_jnt.inc'             
      include 'a_gls2.inc'
      include 'a_frc.inc'
                                  
      record /CmndLine/ CMND
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 inc
      logical*4 error,ExpSIMM

      real*4    TimeInc,jntpos(3),relvel(6),relacc(6),jpw,velnrm(3),vecdot
      integer*4 frm,i,j,k,jntnum,distseg,proxseg,ierr,vecnorm
      logical*4 UseVel,UseAccel,UseMom,UsePower
      
      Error= .true.
      if ( .not. JF.vel_acc_JCS ) goto 1000 ! error
c
c     Check for options on the animate command
c     Default export behavior is to include velocity and moment
c     Options are:
c          /VEL - velocity only (trans and rot)
c          /ACC - acceleration (trans and rot)
c          /MOM - include rot velocity and moment
c          /POW - include power with velocity
c                          
      UseMom=   .true.
      UseAccel= .false.             
      UsePower= .false.
      UseVel=   .false.
      do i= 1, CMND.NumOpt(4)
        if ( CMND.OptText(i,4) .eq. 'ACC' ) then
          UseAccel= .true.
          UseMom=   .false.
          UsePower= .false.
          UseVel=   .false.
        else if ( CMND.OptText(i,4) .eq. 'POW' ) then
          UseAccel= .false.
          UseMom=   .false.
          UsePower= .true.
          UseVel=   .false.
        else if ( CMND.OptText(i,4) .eq. 'VEL' ) then
          UseAccel= .false.
          UseMom=   .false.
          UsePower= .false.
          UseVel=   .true.
        else if ( CMND.OptText(i,4) .eq. 'MOM' ) then
          UseAccel= .false.
          UseMom=   .true.
          UsePower= .false.
          UseVel=   .false.
        else               
          goto 1010      
        end if
      end do                   
      if ( (UseMom .or. UsePower)  .and. (.not. JF.Load)  ) goto 1020
c
c     Export Joint velocity and moment in GCS Coordinate system
c                                           
      timeinc= float(inc)/rMtnFrmRate
      do i= 1, MP.nfrm, inc
        write(2,235) i,float(i)*timeinc          
235     format(1x,i4,f10.4)
        do j= 1, GP.num_jnt
          jntnum= GP.jnt_num(j)       
          if ( jntnum .ne. pvlb ) then           
            distseg= JntSegNum(distal,jntnum)
            proxseg= JntSegNum(proximal,jntnum)
            if ( jntnum .eq. pvtk ) then
              call PntChangeCS( SgpGCS(1,1,i,trnk),
     &                          SP(trnk).end(1,3),jntpos )
              call VecSub( SvlGCS(x,i,trnk),
     &                     SvlGCS(x,i,pelv),
     &                     RelVel(x) )              
              call VecSub( SvlGCS(wx,i,trnk),
     &                     SvlGCS(wx,i,pelv),
     &                     RelVel(wx) )              
              call VecSub( SacGCS(x,i,trnk),
     &                     SacGCS(x,i,pelv),
     &                     RelAcc(x) )              
              call VecSub( SacGCS(wx,i,trnk),
     &                     SacGCS(wx,i,pelv),
     &                     RelAcc(wx) )              
            else
              call PntChangeCS( SgpGCS(1,1,i,distseg),
     &                          SP(distseg).end(1,proximal),jntpos )
              call VecSub( SvlGCS(x,i,distseg),
     &                     SvlGCS(x,i,proxseg),
     &                     RelVel(x) )              
              call VecSub( SvlGCS(wx,i,distseg),
     &                     SvlGCS(wx,i,proxseg),
     &                     RelVel(wx) )              
              call VecSub( SacGCS(x,i,distseg),
     &                     SacGCS(x,i,proxseg),
     &                     RelAcc(x) )              
              call VecSub( SacGCS(wx,i,distseg),
     &                     SacGCS(wx,i,proxseg),
     &                     RelAcc(wx) )              
            end if                       
            if ( UseMom ) then
              if ( JntCalcOK(i,jntnum) ) then
                 if ( .not. ExpSIMM ) then
                    write(2,240) (jntpos(k),k=1,3),((relvel(k)),k=1,3),  
     &                           (JrlGCS(k,i,jntnum),k=4,6)
 240                format(9(1x,f10.4))
                 else
                    write(2,240) (jntpos(k),k=1,3),
     &                           (JrlGCS(k,i,jntnum),k=4,6)
                 end if
              else
                 if ( .not. ExpSIMM ) then
                    write(2,240) (jntpos(k),k=1,3),0.0,0.0,0.0,0.0,0.0,0.0
                 else
                    write(2,240) (jntpos(k),k=1,3),0.0,0.0,0.0
                 end if
              end if                                       
            else if ( UseVel ) then
               if ( .not. ExpSIMM ) then
                  write(2,240) (jntpos(k),k=1,3),((relvel(k)),k=1,6)
               else
                  write(2,240) (jntpos(k),k=1,3),((relvel(k)),k=4,6)
               end if
            else if ( UseAccel ) then                           
               if ( .not. ExpSIMM ) then
                  write(2,240) (jntpos(k),k=1,3),((relacc(k)),k=1,6)
               else
                  write(2,240) (jntpos(k),k=1,3),((relacc(k)),k=4,6)
               end if
            else if ( UsePower ) then
              if ( JntCalcOK(i,jntnum) ) then
                jpw= VecDot( RelVel,JrlGCS(mx,i,jntnum) )
                ierr= vecnorm( relvel(wx),velnrm )
                if ( ierr .eq. 0 ) then
                  if ( .not. ExpSIMM ) then
                     write(2,240) (jntpos(k),k=1,3),
     &                            jpw*velnrm(x),jpw*velnrm(y),jpw*velnrm(z),
     &                            (JrlGCS(k,i,jntnum),k=4,6)
                  else
                     write(2,240) (jntpos(k),k=1,3),
     &                            jpw*velnrm(x),jpw*velnrm(y),jpw*velnrm(z)
                  end if
                else
                   if ( .not. ExpSIMM ) then
                      write(2,240) (jntpos(k),k=1,3),0.0,0.0,0.0,0.0,0.0,0.0
                   else
                      write(2,240) (jntpos(k),k=1,3),0.0,0.0,0.0
                   end if
                end if
              else
                 if ( .not. ExpSIMM ) then
                    write(2,240) (jntpos(k),k=1,3),0.0,0.0,0.0,0.0,0.0,0.0
                 else
                    write(2,240) (jntpos(k),k=1,3),0.0,0.0,0.0
                 end if
              end if
            end if
          end if  
        end do
        write(2,319)
319     format(/)
      end do                            
      error= .false.
      return
                                         
1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint vel/accel have not been calculated yet')
      return

1010  write(6,1015) CMND.OptText(i,4)
      if ( OF.ToLog ) write(3,1015) CMND.OptText(i,4)
1015  format(' Error- ',a3,' is not a valid option of EXP JNT VAA ANI'/
     &       '        Valid options are: /VELocity only'/
     &       '                           /ACCeleration only'/
     &       '                           /POWer and velocity'/
     &       '                           /MOMent and velocity (default)')
      return

1020  write(6,1025)      
      if ( OF.ToLog ) write(3,1025)
1025  format(' Error- Joint loads have not been calculated yet')
      return
      end      
c           
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntEulAng( inc,RegHeader,IgorHeader,Lower,
     &                         JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      include 'a_seg.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 JntNum,inc
      logical*4 Error,RegHeader,IgorHeader,Lower
      
      real*4    TimeInc
      integer*4 frm,i,j,k
      
      Error= .true.
      if ( .not. JF.angle_EUL ) goto 1020 ! error
      if ( Lower ) then
        if ( IgorHeader ) goto 1030 ! error
        if ( RegHeader ) write(2,410)
410     format( ' Euler angles for rtak,rtkn,rtkn,lfak,lfkn,lfhp,pvlb respectively'/
     &          ' Angle order is flexion(+)/extension(-)'/
     &          '                adduction(+)/abduction(i)'/
     &          '                internal(+)/external(-)'/
     &          ' For Pelvis angles:  flexion (front lowers +)'/
     &          '                     obliquity (left side rises +)'/
     &          '                     rotation (right side forward +)'//
     &          ' Gait Events :  RHS = 1   RTO = 3'/
     &          '                LHS = 2   LTO = 4'//
     &          ' Frame  Event   Flex/Ext     Ad/Ab        Int/Ext...'/)
        do i= 1, MP.nfrm,inc                
          write(2,420) i,GaitEvent(i),((JangEUL(j,i,k),j=1,3),k=1,7)
420       format(' ',i4,i3,21(f11.5))
        end do
      
      else
        if ( IgorHeader ) then
          TimeInc= float(inc)/rMtnFrmRate
          write(2,100) (Jnt_Names(JntNum),i=1,3)
100       format(' IGOR'/
     &           ' WAVES ',a4,'_EanFe, ',a4,'_EanAA, ',a4,'_EanIe'/
     &           ' BEGIN')
          do frm= 1, MP.nfrm, inc
            write(2,110) (JangEUL(i,frm,JntNum),i=1,3)
110         format(2x,3(f12.5))
          end do
          write(2,119)
119       format(' END')
          do i= 1, 3 
            write(2,120) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
          end do
120       format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EanFe'/
     &           ' X SetScale y 0,0,"deg", ',a4,'_EanFe'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EanAa'/
     &           ' X SetScale y 0,0,"deg", ',a4,'_EanAa'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EanIe'/
     &           ' X SetScale y 0,0,"deg", ',a4,'_EanIe')
        else
          if ( RegHeader ) write(2,200) Jnt_Names(JntNum)
200       format(' Fixed Euler angles for ',a4//
     &           ' Angle order is flexion(+)/extension(-)'/
     &           '                adduction(+)/abduction(i)'/
     &           '                internal(+)/external(-)'/
     &           ' For Pelvis angles:  flexion (front lowers +)'/
     &           '                     obliquity (left side rises +)'/
     &           '                     rotation (right side forward +)'//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &           ' Frame  Event   Flex/Ext     Ad/Ab        Int/Ext'/)
          do frm= 1, MP.nfrm, inc
            write(2,210) frm,GaitEvent(frm),(JangEUL(i,frm,JntNum),i=1,3)
210         format(2x,i3,5x,i1,3(f12.5))
          end do
        end if ! Igor or Regular
      end if
      Error= .false.
      return

1020  write(6,1025)      
      if ( OF.ToLog ) write(3,1025)
1025  format(' Error- EUler joint angles have not been calculated yet')        
      return

1030  write(6,1035)      
      if ( OF.ToLog ) write(3,1035)
1035  format(' Error- Igor header option is only valid for normal'/
     &       '        Euler joint angle export not for the special'/
     &       '        case of LOWer extremity angles')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntEulVAA( inc,RegHeader,IgorHeader,Norm,
     &                         JntNum,JF,CF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /cycle_flag/   CF
      record /option_flag/  OF
      
      integer*4 JntNum,inc
      logical*4 Error,RegHeader,IgorHeader,Norm

      real*4    TimeInc,CycleTime
      integer*4 frm,i,j
      
      Error= .true.
      if ( .not. JF.vel_acc_EUL ) goto 1040 ! error
      if ( Norm ) then
        if ( .not. CF.event ) goto 2020 ! error
        if ( rMtnFrmRate .le. 1.0e-5 ) goto 2030 ! error
        CycleTime= float(MP.cycle_length(1))/rMtnFrmRate
      end if
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        write(2,100) (Jnt_Names(JntNum),i=1,6)
100     format(' IGOR'/
     &         ' WAVES ',a4,'_EvlFe, ',a4,'_EvlAa, ',a4,'_EvlIe, ',
     &                   a4,'_EacFe, ',a4,'_EacAa, ',a4,'_EacIe'/
     &         ' BEGIN')
        if ( .not. Norm ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JvelEUL(i,frm,JntNum),i=1,3),
     &                   (JaccEUL(i,frm,JntNum),i=1,3)
110         format(1x,6(2x,e13.5))             
          end do
          write(2,119)
119       format(' END')
          do i= 1, 6
            write(2,120) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
          end do
120       format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EvlFe'/
     &           ' X SetScale y 0,0,"d/s", ',a4,'_EvlFe'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EvlAa'/
     &           ' X SetScale y 0,0,"d/s", ',a4,'_EvlAa'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EvlIe'/
     &           ' X SetScale y 0,0,"d/s", ',a4,'_EvlIe'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EacFe'/
     &           ' X SetScale y 0,0,"ds2", ',a4,'_EacFe'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EacAa'/
     &           ' X SetScale y 0,0,"ds2", ',a4,'_EacAa'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EacIe'/
     &           ' X SetScale y 0,0,"ds2", ',a4,'_EacIe')
        else ! Normalize Vel/Acc
          do frm= 1, MP.nfrm, inc
            write(2,110) (JvelEUL(i,frm,JntNum)/CycleTime,i=1,3),
     &                   (JaccEUL(i,frm,JntNum)/(CycleTime**2),i=1,3)
          end do
          write(2,119)
          do i= 1, 6
            write(2,130) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
          end do
130       format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EvlFen'/
     &           ' X SetScale y 0,0,"d/%", ',a4,'_EvlFen'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EvlAan'/
     &           ' X SetScale y 0,0,"d/%", ',a4,'_EvlAan'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EvlIen'/
     &           ' X SetScale y 0,0,"d/%", ',a4,'_EvlIen'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EacFen'/
     &           ' X SetScale y 0,0,"d%2", ',a4,'_EacFen'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EacAan'/
     &           ' X SetScale y 0,0,"d%2", ',a4,'_EacAan'/
     &           ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_EacIen'/
     &           ' X SetScale y 0,0,"d%2", ',a4,'_EacIen')
        end if
      else ! Regular Export
        if ( .not. Norm ) then
          if ( RegHeader ) write(2,140) Jnt_Names(JntNum)
140       format(' Fixed Euler velocities (deg/sec) and '/
     &           '             accelerations (deg/sec^2) for ',a4//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &           ' Frame  Gait',18x,'Velocity',26x,'Acceleration'/
     &           '   #    Event',2('    Flex/Ext      Ab/Ad       Int/Ext')/)
          do frm= 1, MP.nfrm, inc
            write(2,150) frm,GaitEvent(frm),(JvelEUL(i,frm,JntNum),i=1,3),
     &                                  (JaccEUL(i,frm,JntNum),i=1,3)
150         format(1x,i3,5x,i2,2x,6(2x,f10.3))             
          end do                
        else ! Normalize to persent cycle
          if ( RegHeader ) write(2,160) Jnt_Names(JntNum)
160       format(' Fixed Euler velocities (deg/%cycle) and '/
     &           '             accelerations (deg/%cycle^2) for ',a4//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &           ' Frame  Gait',18x,'Velocity',26x,'Acceleration'/
     &           '   #    Event',2('    Flex/Ext      Ab/Ad       Int/Ext')/)
          do frm= 1, MP.nfrm, inc
            write(2,150) frm,GaitEvent(frm),
     &                   (JvelEUL(i,frm,JntNum)/CycleTime,i=1,3),
     &                   (JaccEUL(i,frm,JntNum)/(CycleTime**2),i=1,3)
          end do                
        end if                                     
      end if ! Igor or Regular
      Error= .false.
      return

1040  write(6,1045)      
      if ( OF.ToLog ) write(3,1045)
1045  format(' Error- Euler joint vel/accel has not been calculated yet')
      return

2020  write(6,2025) 
      if ( OF.ToLog ) write(3,2025)
2025  format(' Error- Normalization of the joint vel/accel is not possible'/
     &       '        because the gait events have not been input')
      return

2030  write(6,2035) 
      if ( OF.ToLog ) write(3,2035) 
2035  format(' Error- Normalization of the joint vel/accel is not possible'/
     &       '        because the motion frame rate is <= 0.0')
      return
      end
c
c
c
      subroutine ExpJntLoadAnimate( inc,ExpSIMM,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_seg.inc'
      include 'a_jnt.inc'
      include 'a_gls2.inc'
      include 'a_frc.inc'
                                  
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 inc
      logical*4 error,ExpSIMM

      real*4    TimeInc,jntpos(3)
      integer*4 frm,i,j,k,jntnum,distseg
      
      Error= .true.
      if ( .not. JF.Load ) goto 1000 ! error
c
c     Export Joint loads in GCS Coordinate system
c
      timeinc= float(inc)/rMtnFrmRate
      do i= 1, MP.nfrm, inc
        write(2,235) i,float(i)*timeinc
235     format(1x,i4,f10.4)
        do j= 1, GP.num_jnt
          jntnum= GP.jnt_num(j)
          if ( jntnum .ne. pvlb ) then
            if ( jntnum .eq. pvtk ) then
              call PntChangeCS( SgpGCS(1,1,i,trnk),
     &                          SP(trnk).end(1,3),jntpos )
            else
              distseg= JntSegNum(distal,jntnum)
              call PntChangeCS( SgpGCS(1,1,i,distseg),
     &                          SP(distseg).end(1,proximal),jntpos )
            end if
            if ( JntCalcOK(i,jntnum) ) then
               if ( .not. ExpSIMM ) then
                  write(2,240) (jntpos(k),k=1,3),
     &                         (JrlGCS(k,i,jntnum),k=1,6)
 240              format(9(1x,f10.4))
               else
                  write(2,240) (jntpos(k),k=1,3),
     &                         (JrlGCS(k,i,jntnum),k=1,3)
               end if
            else
               if ( .not. ExpSIMM ) then
                  write(2,240) (jntpos(k),k=1,3),0.0,0.0,0.0,0.0,0.0,0.0
               else
                  write(2,240) (jntpos(k),k=1,3),0.0,0.0,0.0
               end if
            end if
          end if
        end do
        do j= rt, lf
           if ( .not. ExpSIMM ) then
              write(2,317) (FtCpGCS(k,i,j),k=1,3),(-FtGrl(k,i,j),k=1,6)
 317          format(9(1x,f10.4))
           else
              write(2,317) (FtCpGCS(k,i,j),k=1,3),(FtGrl(k,i,j),k=1,3)
           end if
        end do
        write(2,319)
319     format(/)
      end do                            
      error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint loads have not been calculated yet')
      return
      end      
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntLoad( inc,RegHeader,IgorHeader,CSType,
     &                       USUnits,NormWt,NormWtHt,NormWtLeg,
     &                       LegLength,JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      real*4    LegLength(2)
      integer*4 JntNum,inc,CSType
      logical*4 Error,RegHeader,IgorHeader,USUnits,NormWt,NormWtHt,
     &          NormWtLeg

      real*4    TimeInc,TempLoad(6)
      integer*4 frm,i,j,LogtoInt
      
      Error= .true.
      if ( .not. JF.Load ) goto 1000 ! error
c
c     Export Joint loads in GCS Coordinate system
c
      TimeInc= float(inc)/rMtnFrmRate
      if ( CSType .eq. GCS_CS ) then ! GCS
        if ( IgorHeader ) then
          write(2,100) (Jnt_Names(JntNum),i=1,6)                   
100       format(' IGOR'/
     &           ' WAVES ',a4,'_JldFxG, ',a4,'_JldFyG, ',a4,'_JldFzG, ',
     &                     a4,'_JldMxG, ',a4,'_JldMyG, ',a4,'_JldMzG'/
     &           ' BEGIN')
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,JntNum) ) then
              call ConvertNormJntLoad( JrlGCS(fx,frm,JntNum),JntNum,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,110) (TempLoad(i),i=1,6)
110             format(2x,6(e13.5,2x))
            else
              write(2,110) 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do    
          call PrintIgorJntLoad( jnt_names(JntNum),TimeInc,'G',UsUnits )   
        else ! regular export of GCS joint load
          if ( RegHeader ) then
            write(2,130) Jnt_Names(JntNum)                    
130         format(' Joint loads for ',a4,' in the Global Coordinate System'/)
            write(2,140)
140         format(' Gait Events :  RHS = 1   RTO = 3'/       
     &             '                LHS = 2   LTO = 4'//
     &             ' Calculation types : 1 - Right single support top down'/
     &             21x,'2 - Left single support top down'/
     &             21x,'3 - Right single support bottom up'/
     &             21x,'4 - Left single support bottom up'/
     &             21x,'5 - Double support, good GRL both feet'/
     &             21x,'6 - Double support, good GRL for right foot'/
     &             21x,'7 - Double support, good GRL for left foot'/
     &             21x,'8 - Double support, bad  GRL both feet'/
     &             21x,'9 - Flight phase, no GRL'/
     &             21x,'10- Double support, good GRL right foot, limited calc'/
     &             21x,'11- Double support, good GRL left foot, limited calc'/)
            write(2,145)
145         format(' Frame  Event CalcOK CalcType',5x,'Fx',14x,'Fy',14x,'Fz',14x,
     &             'Mx',14x,'My',14x,'Mz')
            call PrintJntLoadUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
          end if ! Print header....        
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,JntNum) ) then
              call ConvertNormJntLoad( JrlGCS(fx,frm,JntNum),JntNum,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,170) frm,GaitEvent(frm),
     &                     LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                     JrlCalcType(frm),(TempLoad(i),i=1,6)
170             format(2x,i3,5x,i1,5x,i2,4x,i2,6(3x,e13.5))
            else
              write(2,170) frm,GaitEvent(frm),
     &                     LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                     JrlCalcType(frm),0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do
        end if
             
      else if ( CSType .eq. PRO_CS ) then ! Proximal segment LCS
        if ( IgorHeader ) then
          write(2,180) (Jnt_Names(JntNum),i=1,6)                   
180       format(' IGOR'/
     &           ' WAVES ',a4,'_JldFxP, ',a4,'_JldFyP, ',a4,'_JldFzP, ',
     &                     a4,'_JldMxP, ',a4,'_JldMyP, ',a4,'_JldMzP'/
     &           ' BEGIN')
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,JntNum) ) then
              call ConvertNormJntLoad( JrlPCS(fx,frm,JntNum),JntNum,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,110) (TempLoad(i),i=1,6)
            else
              write(2,110) 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do    
          call PrintIgorJntLoad( jnt_names(JntNum),TimeInc,'P',UsUnits )   
        else ! regular export of proximal LCS joint load
          if ( RegHeader ) then
            write(2,200) Jnt_Names(JntNum)                    
200         format(' Joint loads for ',a4,' in the proximal segment LCS'/)
            write(2,140)
            write(2,145)
            call PrintJntLoadUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
          end if ! Print header....        
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,JntNum) ) then
              call ConvertNormJntLoad( JrlPCS(fx,frm,JntNum),JntNum,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,170) frm,GaitEvent(frm),
     &                     LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                     JrlCalcType(frm),(TempLoad(i),i=1,6)
            else
              write(2,170) frm,GaitEvent(frm),
     &                     LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                     JrlCalcType(frm),0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do
        end if

      else if ( CSType .eq. DIS_CS ) then ! Distal segment LCS
        if ( IgorHeader ) then
          write(2,210) (Jnt_Names(JntNum),i=1,6)                   
210       format(' IGOR'/
     &           ' WAVES ',a4,'_JldFxD, ',a4,'_JldFyD, ',a4,'_JldFzD, ',
     &                     a4,'_JldMxD, ',a4,'_JldMyD, ',a4,'_JldMzD'/
     &           ' BEGIN')
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,JntNum) ) then
              call ConvertNormJntLoad( JrlDCS(fx,frm,JntNum),JntNum,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,110) (TempLoad(i),i=1,6)
            else
              write(2,110) 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do 
          call PrintIgorJntLoad( jnt_names(JntNum),TimeInc,'D',UsUnits )   
        else ! regular export of proximal LCS joint load
          if ( RegHeader ) then
            write(2,220) Jnt_Names(JntNum)                    
220         format(' Joint loads for ',a4,' in the distal segment LCS'/)
            write(2,140)
            write(2,145)
            call PrintJntLoadUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
          end if ! Print header....        
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,JntNum) ) then
              call ConvertNormJntLoad( JrlDCS(fx,frm,JntNum),JntNum,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,170) frm,GaitEvent(frm),
     &                     LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                     JrlCalcType(frm),(TempLoad(i),i=1,6)
            else
              write(2,170) frm,GaitEvent(frm),
     &                     LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                     JrlCalcType(frm),0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do
        end if
        
      else ! Joint Coordinate System
        if ( IgorHeader ) then
          write(2,230) (Jnt_Names(JntNum),i=1,6)                   
230       format(' IGOR'/
     &           ' WAVES ',a4,'_JldMlfJ, ',a4,'_JldApfJ, ',a4,'_JldCdfJ, ',
     &                     a4,'_JldFemJ, ',a4,'_JldAamJ, ',a4,'_JldIemJ'/
     &           ' BEGIN')
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,JntNum) ) then
              call ConvertNormJntLoad( JrlJCS(fx,frm,JntNum),JntNum,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,110) (TempLoad(i),i=1,6)
            else
              write(2,110) 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do 
          call PrintIgorJntLoad( jnt_names(JntNum),TimeInc,'J',UsUnits )   
        else ! regular export of JCS joint load
          if ( RegHeader ) then
            write(2,240) Jnt_Names(JntNum)                    
240         format(' Joint loads for ',a4,' in the Joint Coordinate System'/)
            write(2,140)
            write(2,250)
250         format(' Frame  Event CalcOK CalcType',21x,'Forces',42x,'Moments'/
     &           32x,'Med/Lat',9x,'Ant/Post',8x,'Dist/Comp',9x,'Flex/Ext',9x,
     &           'Ab/Ad',9x,'Int/Ext')
            call PrintJntLoadUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
          end if ! Print header....        
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,JntNum) ) then
              call ConvertNormJntLoad( JrlJCS(fx,frm,JntNum),JntNum,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,170) frm,GaitEvent(frm),
     &                     LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                     JrlCalcType(frm),(TempLoad(i),i=1,6)
            else
              write(2,170) frm,GaitEvent(frm),
     &                     LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                     JrlCalcType(frm),0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do
        end if
      end if ! which Coord System
      Error= .false.
      return
      
1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint loads have not been calculated yet')
      return
      end      
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine PrintJntLoadUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
      implicit none
      
      logical*4 UsUnits,NormWt,NormWtHt,NormWtLeg
      
      if ( UsUnits ) then
        if ( NormWt ) then
          write(2,100)
100       format(' NOTE: Normalized by patient weight'/
     &           32x,3('(lbs/lbs)',8x),3('(inlb/lb)',8x)/)
        else if ( NormWtHt ) then
          write(2,110)
110       format(' NOTE: Normalized by patient weight*height'/
     &           32x,3('(lb/inlb)',8x),3('(inlb/inlb)',6x)/)
        else if ( NormWtLeg ) then
          write(2,115)
115       format(' NOTE: Normalized by patient weight*leg length'/
     &           32x,3('(lb/inlb)',8x),3('(inlb/inlb)',6x)/)
        else 
          write(2,120)
120       format(32x,3('(lbs)',12x),3('(inlb)',11x)/)
        end if
      else
        if ( NormWt ) then
          write(2,130)               
130       format(' NOTE: Normalized by patient weight'/
     &           33x,3('(N/N)',11x),3('(Nm/N)',10x)/)
        else if ( NormWtHt ) then
          write(2,140)               
140       format(' NOTE: Normalized by patient weight*height'/
     &           33x,3('(N/Nm)',10x),3('(Nm/Nm)',9x)/)
        else if ( NormWtLeg ) then
          write(2,145)               
145       format(' NOTE: Normalized by patient weight*leg length'/
     &           33x,3('(N/Nm)',10x),3('(Nm/Nm)',9x)/)
        else
          write(2,150)               
150       format(33x,3('(N)',13x),3('(Nm)',12x)/)
        end if
      end if
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      Subroutine PrintIgorJntLoad( JntText,TimeInc,CS,UsUnits )  
      implicit none
      
      real*4    TimeInc 
      integer*4 i
      character JntText*4,CS*1,CompName(6)*12,UnitName(2)*3
      logical*4 UsUnits
         
      if ( CS .ne. 'J' ) then
        CompName(1)= JntText//'_JldFx'//CS
        CompName(2)= JntText//'_JldFy'//CS
        CompName(3)= JntText//'_JldFz'//CS
        CompName(4)= JntText//'_JldMx'//CS
        CompName(5)= JntText//'_JldMy'//CS
        CompName(6)= JntText//'_JldMz'//CS
      else
        CompName(1)= JntText//'_JldMlfJ'
        CompName(2)= JntText//'_JldApfJ'
        CompName(3)= JntText//'_JldCdfJ'
        CompName(4)= JntText//'_JldFemJ'
        CompName(5)= JntText//'_JldAamJ'
        CompName(6)= JntText//'_JldIemJ'
      end if
      if ( .not. USUnits ) then
        UnitName(1)= 'N  '
        UnitName(2)= 'Nm '
      else
        UnitName(1)= 'lbf'
        UnitName(2)= 'ilb'
      end if
      write(2,99)
99    format(' END')
      do i= 1, 3
        write(2,100) TimeInc,CompName(i),UnitName(1),CompName(i)
      end do
      do i= 4, 6
        write(2,100) TimeInc,CompName(i),UnitName(2),CompName(i)
      end do
100   format(' X SetScale /P x 0,',f10.6,',"sec", ',a12/
     &       ' X SetScale y 0,0,"',a3,'", ',a12/ )
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntLegLoad( inc,RegHeader,IgorHeader,CSType,
     &                          USUnits,NormWt,NormWtHt,NormWtLeg,
     &                          LegLength,Side,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      real*4    LegLength(2)
      integer*4 Side,inc,CSType
      logical*4 Error,RegHeader,IgorHeader,USUnits,NormWt,NormWtHt,
     &          NormWtLeg

      real*4    TimeInc,TempLoad(6)
      integer*4 frm,i,j,ankle,LogtoInt
      character LoadName*6,SideName(2)*5
      
      data SideName / 'Right','Left ' /
      
      Error= .true.
      if ( .not. JF.Load ) goto 1000 ! error
      if ( .not. JF.LegSummed ) goto 1010 ! error
c
c     Export Leg loads in GCS Coordinate system
c
      LoadName= 'Rt_Lld'
      ankle= rtak
      if ( side .eq. lf ) then
        LoadName= 'Lf_Lld'
        ankle= lfak
      end if
      TimeInc= float(inc)/rMtnFrmRate
      if ( CSType .eq. GCS_CS ) then ! GCS
        if ( IgorHeader ) then
          write(2,100) (LoadName,i=1,6)                   
100       format(' IGOR'/
     &           ' WAVES ',a6,'FxG, ',a6,'FyG, ',a6,'FzG, ',
     &                     a6,'MxG, ',a6,'MyG, ',a6,'MzG'/
     &           ' BEGIN')
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,ankle) ) then
              call ConvertNormJntLoad( JRLegGCS(fx,frm,Side),ankle,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,110) (TempLoad(i),i=1,6)
110             format(2x,6(e13.5,2x))
            else
              write(2,110) 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do    
          call PrintIgorLegLoad( LoadName,TimeInc,'G',UsUnits )  
        else ! regular export of GCS leg load
          if ( RegHeader ) then
            write(2,130) SideName(Side)                    
130         format(' Summed leg loads for ',a5,' side in the Global Coordinate System'/)
            write(2,140)
140         format(' Gait Events :  RHS = 1   RTO = 3'/       
     &             '                LHS = 2   LTO = 4'//
     &             ' Calculation types : 1 - Right single support top down'/
     &             21x,'2 - Left single support top down'/
     &             21x,'3 - Right single support bottom up'/
     &             21x,'4 - Left single support bottom up'/
     &             21x,'5 - Double support, good GRL both feet'/
     &             21x,'6 - Double support, good GRL for right foot'/
     &             21x,'7 - Double support, good GRL for left foot'/
     &             21x,'8 - Double support, bad  GRL both feet'/
     &             21x,'9 - Flight phase, no GRL'/
     &             21x,'10- Double support, good GRL right foot, limited calc'/
     &             21x,'11- Double support, good GRL left foot, limited calc'/)
            write(2,145)
145         format(' Frame  Event CalcOK CalcType',5x,'Fx',14x,'Fy',14x,'Fz',14x,
     &             'Mx',14x,'My',14x,'Mz')
            call PrintJntLoadUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
          end if ! Print header....        
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,ankle) ) then
              call ConvertNormJntLoad( JRLegGCS(fx,frm,Side),ankle,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,170) frm,GaitEvent(frm),LogtoInt(JntCalcOK(frm,ankle))+1,
     &                     JrlCalcType(frm),(TempLoad(i),i=1,6)
170             format(2x,i3,5x,i1,5x,i2,4x,i2,6(3x,e13.5))
            else
              write(2,170) frm,GaitEvent(frm),LogtoInt(JntCalcOK(frm,ankle))+1,
     &                     JrlCalcType(frm),0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do
        end if
        
      else ! Joint Coordinate System
        if ( IgorHeader ) then
          write(2,230) (LoadName,i=1,6) 
230       format(' IGOR'/
     &           ' WAVES ',a6,'MlfJ, ',a6,'ApfJ, ',a6,'CdfJ, ',
     &                     a6,'FemJ, ',a6,'AamJ, ',a6,'IemJ'/
     &           ' BEGIN')
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,ankle) ) then
              call ConvertNormJntLoad( JRLegJCS(fx,frm,Side),ankle,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,110) (TempLoad(i),i=1,6)
            else
              write(2,110) 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do 
          call PrintIgorLegLoad( LoadName,TimeInc,'J',UsUnits )  
        else ! regular export of JCS summed leg load
          if ( RegHeader ) then
            write(2,240) SideName(Side)                    
240         format(' Summed leg loads for ',a5,' side in the Joint Coordinate System'/)
            write(2,140)
            write(2,250)
250         format(' Frame  Event CalcOK CalcType',21x,'Forces',42x,'Moments'/
     &           32x,'Med/Lat',9x,'Ant/Post',8x,'Dist/Comp',9x,'Flex/Ext',9x,
     &           'Ab/Ad',9x,'Int/Ext')
            call PrintJntLoadUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
          end if ! Print header....        
          do frm= 1, MP.nfrm, inc
            if ( JntCalcOK(frm,ankle) ) then
              call ConvertNormJntLoad( JRLegJCS(fx,frm,Side),ankle,
     &                                 UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                 GP.weight,GP.height,
     &                                 LegLength,TempLoad )
              write(2,170) frm,GaitEvent(frm),LogtoInt(JntCalcOK(frm,ankle))+1,
     &                     JrlCalcType(frm),(TempLoad(i),i=1,6)
            else
              write(2,170) frm,GaitEvent(frm),LogtoInt(JntCalcOK(frm,ankle))+1,
     &                     JrlCalcType(frm),0.0, 0.0, 0.0, 0.0, 0.0, 0.0
            end if
          end do
        end if             
      end if ! which Coord System
      Error= .false.
      return
      
1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint loads have not been calculated yet')
      return

1010  write(6,1015)      
      if ( OF.ToLog ) write(3,1015)
1015  format(' Error- Summed leg loads have not been calculated')
      return
      end      
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      Subroutine PrintIgorLegLoad( LegText,TimeInc,CS,UsUnits )  
      implicit none
      
      integer*4 i
      real*4    TimeInc 
      character LegText*6,CS*1,CompName(6)*10,UnitName(2)*3
      logical*4 UsUnits
         
      if ( CS .ne. 'J' ) then
        CompName(1)= LegText//'FxG'
        CompName(2)= LegText//'FyG'
        CompName(3)= LegText//'FzG'
        CompName(4)= LegText//'MxG'
        CompName(5)= LegText//'MyG'
        CompName(6)= LegText//'MzG'
      else
        CompName(1)= LegText//'MlfJ'
        CompName(2)= LegText//'ApfJ'
        CompName(3)= LegText//'CdfJ'
        CompName(4)= LegText//'FemJ'
        CompName(5)= LegText//'AamJ'
        CompName(6)= LegText//'IemJ'
      end if
      if ( .not. USUnits ) then
        UnitName(1)= 'N  '
        UnitName(2)= 'Nm '
      else
        UnitName(1)= 'lbf'
        UnitName(2)= 'ilb'
      end if
      write(2,99)
99    format(' END')
      do i= 1, 3
        write(2,100) TimeInc,CompName(i),UnitName(1),CompName(i)
      end do
      do i= 4, 6
        write(2,100) TimeInc,CompName(i),UnitName(2),CompName(i)
      end do
100   format(' X SetScale /P x 0,',f10.6,',"sec", ',a10/
     &       ' X SetScale y 0,0,"',a3,'", ',a10/ )
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntWre( inc,RegHeader,IgorHeader,
     &                      JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      include 'a_seg.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 JntNum,inc
      logical*4 Error,RegHeader,IgorHeader
      
      real*4    TimeInc
      integer*4 frm,i,j,LogtoInt
      
      Error= .true.
      if ( .not. JF.wrench ) goto 1000 ! error
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        write(2,100) (Jnt_Names(JntNum),i=1,3)
100     format(' IGOR'/
     &         ' WAVES ',a4,'_JwrNx, ',a4,'_JwrNy, ',a4,'_JwrNz, ',
     &                   a4,'_JwrPx, ',a4,'_JwrPy, ',a4,'_JwrPz, ',
     &                   a4,'_JwrMom, ',a4,'_JwrFrc'/
     &         ' BEGIN')
        do frm= 1, MP.nfrm, inc
          if ( JntCalcOK(frm,JntNum) ) then
            write(2,110) (JwreLCS(i,frm,JntNum),i=1,8)
110         format(2x,8(e13.5,2x))
          else
            write(2,110) 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
          end if
        end do    
        write(2,119)
119     format(' END')
        do i= 1, 8
          write(2,120) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JwrNx'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JwrNx'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JwrNy'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JwrNy'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JwrNz'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JwrNz'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JwrPx'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JwrPx'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JwrPy'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JwrPy'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JwrPz'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JwrPz'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JwrMom'/
     &         ' X SetScale y 0,0,"Nm", ',a4,'_JwrMom'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JwrFrc'/
     &         ' X SetScale y 0,0,"N", ',a4,'_JwrFrc')
      else
        if ( RegHeader ) write(2,200) Jnt_Names(JntNum)
200       format(' Joint load wrenches for ',a4,' in the proximal segment LCS'//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &           ' Calculation types : 1 - Right single support top down'/
     &           21x,'2 - Left single support top down'/
     &           21x,'3 - Right single support bottom up'/
     &           21x,'4 - Left single support bottom up'/
     &           21x,'5 - Double support, good GRL both feet'/
     &           21x,'6 - Double support, good GRL for right foot'/
     &           21x,'7 - Double support, good GRL for left foot'/
     &           21x,'8 - Double support, bad  GRL both feet'/
     &           21x,'9 - Flight phase, no GRL'/
     &           21x,'10- Double support, good GRL right foot, limited calc'/
     &           21x,'11- Double support, good GRL left foot, limited calc'//
     &           ' Frame  Event CalcOK CalcType',21x,
     &           'Wrench Axis',18x,'Point on Wrench (m)',10x,
     &           'Moment (Nm)',2x,'Force (N)'//)
        do frm= 1, MP.nfrm, inc
          if ( JntCalcOK(frm,JntNum) ) then
            write(2,210) frm,GaitEvent(frm),LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                   JrlCalcType(frm),(JwreLCS(i,frm,JntNum),i=1,8)
210         format(2x,i3,5x,i1,5x,i2,5x,i2,3x,8(2x,f8.4))
          else
            write(2,210) frm,GaitEvent(frm),LogtoInt(JntCalcOK(frm,JntNum))+1,
     &                   0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
          end if
        end do
      end if ! Igor or Regular
      Error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint wrenches have not been calculated yet')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntPower( inc,RegHeader,IgorHeader,
     &                        USUnits,NormWt,NormWtHt,NormWtLeg,
     &                        LegLength,JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_enr.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      real*4    LegLength(2)
      integer*4 JntNum,inc
      logical*4 Error,RegHeader,IgorHeader,USUnits,NormWt,NormWtHt,
     &          NormWtLeg
      
      real*4    TimeInc,TempPower(4)
      integer*4 frm,i,j
      
      Error= .true.
      if ( .not. JF.Power ) goto 1000 ! error
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        write(2,100) (Jnt_Names(JntNum),i=1,4)
100     format(' IGOR'/
     &         ' WAVES ',a4,'_JpwFe, ',a4,'_JpwAa, ',a4,'_JpwIe, ',
     &                   a4,'_JpwMag'/
     &         ' BEGIN')
        do i= 1, MP.nfrm
          if ( JntCalcOK(i,JntNum) ) then
            call ConvertNormJntPower( JntPower(1,i,JntNum),JntNum,
     &                                UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                GP.weight,GP.height,
     &                                LegLength,TempPower )
            write(2,110) (TempPower(j),j=1,4)
110         format(2x,4(1x,e13.5))
          else
            write(2,110) 0.0, 0.0, 0.0, 0.0
          end if
        end do
        write(2,119)
119     format(' END')
        do i= 1, 4
          write(2,120) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JpwFe'/
     &         ' X SetScale y 0,0,"W", ',a4,'_JpwFe'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JpwAa'/
     &         ' X SetScale y 0,0,"W", ',a4,'_JpwAa'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JpwIe'/
     &         ' X SetScale y 0,0,"W", ',a4,'_JpwIe'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JpwMag'/
     &         ' X SetScale y 0,0,"W", ',a4,'_JpwMag')
      else
        if ( RegHeader ) then
          write(2,200) jnt_names(JntNum)
200       format(' Joint Power for ',a4//
     &           ' frame   fle/ext    ad/ab   int/ext    total'/)
          call PrintJntPowerUnits(UsUnits,NormWt,NormWtHt,NormWtLeg)
        end if
        do i= 1, MP.nfrm
          if ( JntCalcOK(i,JntNum) ) then
            call ConvertNormJntPower( JntPower(1,i,JntNum),JntNum,
     &                                UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                GP.weight,GP.height,
     &                                LegLength,TempPower )
            write(2,210) i,(TempPower(j),j=1,4)
210         format(2x,i4,4(1x,e13.5))
          else
            write(2,210) i, 0.0, 0.0, 0.0, 0.0
          end if
        end do
      end if ! Igor or Regular
      Error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint power has not been calculated yet')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine PrintJntPowerUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
      implicit none
      
      logical*4 UsUnits,NormWt,NormWtHt,NormWtLeg
      
      if ( UsUnits ) then
        if ( NormWt ) then
          write(2,100)
100       format(' NOTE: Normalized by patient weight'/
     &           7x,4('(inlb/s/lb)',2x)/)
        else if ( NormWtHt ) then
          write(2,110)
110       format(' NOTE: Normalized by patient weight*height'/
     &           7x,4('(inlb/s/inlb)',2x)/)
        else if ( NormWtLeg ) then
          write(2,115)
115       format(' NOTE: Normalized by patient weight*leg length'/
     &           7x,4('(inlb/s/inlb)',2x)/)
        else 
          write(2,120)
120       format(7x,4('(inlb/s)',4x)/)
        end if
      else
        if ( NormWt ) then
          write(2,130)               
130       format(' NOTE: Normalized by patient weight'/
     &           7x,4('(W/N)',8x)/)
        else if ( NormWtHt ) then
          write(2,140)               
140       format(' NOTE: Normalized by patient weight*height'/
     &           7x,4('(W/Nm)',8x)/)
        else if ( NormWtLeg ) then
          write(2,145)               
145       format(' NOTE: Normalized by patient weight*leg length'/
     &           7x,4('(W/Nm)',8x)/)
        else
          write(2,150)               
150       format(7x,4('(W)',10x)/)
        end if
      end if
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntLegPower( inc,RegHeader,IgorHeader,
     &                           USUnits,NormWt,NormWtHt,NormWtLeg,
     &                           LegLength,Side,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_enr.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      real*4    LegLength(2)
      integer*4 Side,inc
      logical*4 Error,RegHeader,IgorHeader,USUnits,NormWt,NormWtHt,
     &          NormWtLeg

      real*4    TimeInc,TempPower(4)
      integer*4 frm,i,j,ankle
      character PowerName*6,SideName(2)*5
      
      data SideName / 'Right','Left ' /
      
      Error= .true.
      if ( .not. JF.Power ) goto 1000 ! error
      if ( .not. JF.LegSummed ) goto 1010 ! error
c
c     Export Summed Leg Power in JCS Coordinate system
c
      PowerName= 'Rt_Lpw'
      ankle= rtak
      if ( side .eq. lf ) then
        PowerName= 'Lf_Lpw'
        ankle= lfak
      end if
      TimeInc= float(inc)/rMtnFrmRate
      if ( IgorHeader ) then
        write(2,100) (PowerName,i=1,4)                   
100     format(' IGOR'/
     &         ' WAVES ',a6,'Fe, ',a6,'Aa, ',a6,'Ie, ',a6,'Mag'/
     &         ' BEGIN')
        do i= 1, MP.nfrm
          if ( JntCalcOK(i,ankle) ) then
            call ConvertNormJntPower( JntPowerLeg(1,i,Side),ankle,
     &                                UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                GP.weight,GP.height,
     &                                LegLength,TempPower )
            write(2,110) (TempPower(j),j=1,4)
110         format(2x,4(1x,e13.5))
          else
            write(2,110) 0.0, 0.0, 0.0, 0.0
          end if
        end do
        write(2,119)
119     format(' END')
        do i= 1, 4
          write(2,120) TimeInc,PowerName,PowerName
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a6,'Fe'/
     &         ' X SetScale y 0,0,"W", ',a6,'Fe'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a6,'Aa'/
     &         ' X SetScale y 0,0,"W", ',a6,'Aa'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a6,'Ie'/
     &         ' X SetScale y 0,0,"W", ',a6,'Ie'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a6,'Mag'/
     &         ' X SetScale y 0,0,"W", ',a6,'Mag')
      else ! regular export of GCS leg load
        if ( RegHeader ) then
          write(2,130) SideName(Side)                    
130       format(' Summed leg power for ',a5,' side in the Joint Coordinate System'/
     &           ' frame   fle/ext    ad/ab   int/ext    total'/)
          call PrintJntPowerUnits( UsUnits,NormWt,NormWtHt,NormWtLeg )
        end if ! Print header....        
        do i= 1, MP.nfrm
          if ( JntCalcOK(i,ankle) ) then
            call ConvertNormJntPower( JntPowerLeg(1,i,Side),ankle,
     &                                UsUnits,NormWt,NormWtHt,NormWtLeg,
     &                                GP.weight,GP.height,
     &                                LegLength,TempPower )
            write(2,210) i,(TempPower(j),j=1,4)
210         format(2x,i4,4(1x,e13.5))
          else
            write(2,210) i, 0.0, 0.0, 0.0, 0.0
          end if
        end do
      end if
      Error= .false.
      return
      
1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint power has not been calculated yet')
      return

1010  write(6,1015)      
      if ( OF.ToLog ) write(3,1015)
1015  format(' Error- Summed leg power has not been calculated')
      return
      end      
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntFinScrew( inc,RegHeader,IgorHeader,
     &                           JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      include 'a_seg.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 JntNum,inc
      logical*4 Error,RegHeader,IgorHeader
      
      real*4    TimeInc
      integer*4 frm,i,j
      
      Error= .true.
      if ( .not. JF.FIniteScrew ) goto 1000 ! error
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        write(2,100) (Jnt_Names(JntNum),i=1,8)
100     format(' IGOR'/
     &         ' WAVES ',a4,'_JfsNx, ',a4,'_JfsNy, ',a4,'_JfsNz, ',
     &                   a4,'_JfsPx, ',a4,'_JfsPy, ',a4,'_JfsPz, ',
     &                   a4,'_JfsRot, ',a4,'_JfsTrn'/
     &         ' BEGIN')
        do frm= 1, MP.nfrm, inc
          write(2,110) (JFinScr(i,frm,JntNum),i=1,8)
110       format(2x,8(e13.5,2x))
        end do    
        write(2,119)
119     format(' END')
        do i= 1, 8
          write(2,120) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JfsNx'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JfsNx'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JfsNy'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JfsNy'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JfsNz'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JfsNz'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JfsPx'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JfsPx'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JfsPy'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JfsPy'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JfsPz'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JfsPz'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JfsRot'/
     &         ' X SetScale y 0,0,"deg", ',a4,'_JfsRot'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JfsTrn'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JfsTrn')
      else
        if ( RegHeader ) write(2,200) Jnt_Names(JntNum)
200     format(' Finite Screw for Joint: ',a4//   
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &         ' Frame',2x,'Event',11x,'Screw Axis',18x,'Point on Screw (m)',10x,
     &         'Angle (deg)',2x,'Trans (m)'//)
        do frm= 1, MP.nfrm, inc    
          write(2,210) frm,GaitEvent(frm),(JFinScr(i,frm,JntNum),i=1,8)
210       format(2x,i3,4x,i1,2x,8(2x,f8.4))
        end do                    
      end if ! Igor or Regular
      Error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint finite screw has not been calculated yet')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntInsScrew( inc,RegHeader,IgorHeader,
     &                           JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      include 'a_seg.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 JntNum,inc
      logical*4 Error,RegHeader,IgorHeader
      
      real*4    TimeInc
      integer*4 frm,i,j
      
      Error= .true.
      if ( .not. JF.InstantScrew ) goto 1000 ! error
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        write(2,100) (Jnt_Names(JntNum),i=1,8)
100     format(' IGOR'/
     &         ' WAVES ',a4,'_JisNx, ',a4,'_JisNy, ',a4,'_JisNz, ',
     &                   a4,'_JisPx, ',a4,'_JisPy, ',a4,'_JisPz, ',
     &                   a4,'_JisRot, ',a4,'_JisTrn'/
     &         ' BEGIN')
        do frm= 1, MP.nfrm, inc
          write(2,110) (JInsScr(i,frm,JntNum),i=1,8)
110       format(2x,8(e13.5,2x))
        end do    
        write(2,119)
119     format(' END')
        do i= 1, 8
          write(2,120) TimeInc,Jnt_Names(JntNum),Jnt_Names(JntNum)
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JisNx'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JisNx'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JisNy'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JisNy'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JisNz'/
     &         ' X SetScale y 0,0,"cos", ',a4,'_JisNz'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JisPx'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JisPx'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JisPy'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JisPy'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JisPz'/
     &         ' X SetScale y 0,0,"m", ',a4,'_JisPz'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JisRot'/
     &         ' X SetScale y 0,0,"d/s", ',a4,'_JisRot'/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_JisTrn'/
     &         ' X SetScale y 0,0,"m/s", ',a4,'_JisTrn')
      else
        if ( RegHeader ) write(2,200) Jnt_Names(JntNum)
200     format(' Instantaneous Screw for Joint: ',a4//   
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &         ' Frame',2x,'Event',11x,'Screw Axis',18x,'Point on Screw (m)',10x,
     &         'AngVel (deg/s)',2x,'TransVel (m/s)'//)
        do frm= 1, MP.nfrm, inc    
          write(2,210) frm,GaitEvent(frm),(JInsScr(i,frm,JntNum),i=1,8)
210       format(2x,i3,4x,i1,2x,8(2x,f8.4))
        end do                    
      end if ! Igor or Regular
      Error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint instantaneous screw has not been calculated yet')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntScrewAng( inc,RegHeader,IgorHeader,CSType,
     &                           JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 JntNum,inc,CSType
      logical*4 Error,RegHeader,IgorHeader
      
      real*4    TimeInc
      integer*4 frm,i,j
      character CS*1
      
      Error= .true.
      if ( .not. JF.ScrewAngle ) goto 1000 ! error
      if ( IgorHeader ) then
        if ( CSType .eq. PRO_CS ) then
          CS= 'P'
        else if ( CSType .eq. DIS_CS ) then
          CS= 'D'
        else ! JCS
          CS= 'J'
        end if
        TimeInc= float(inc)/rMtnFrmRate
        write(2,100) Jnt_Names(JntNum),CS,Jnt_Names(JntNum),CS,
     &               Jnt_Names(JntNum),CS
100     format(' IGOR'/
     &         ' WAVES ',a4,'_FsaFe',a1,', ',a4,'_FsaAa',a1,', ',
     &                   a4,'_FsaIe',a1/
     &         ' BEGIN')
          if ( CSType .eq. PRO_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JfsAngPCS(i,frm,JntNum),i=1,3)
110         format(2x,3(f13.5,1x))
          end do
        else if ( CSType .eq. DIS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JfsAngDCS(i,frm,JntNum),i=1,3)
          end do
        else ! JCS
          do frm= 1, MP.nfrm, inc
            write(2,110) (JfsAngJCS(i,frm,JntNum),i=1,3)
          end do
        end if     
        write(2,119)
119     format(' END')
        do i= 1, 3   
          write(2,120) TimeInc,Jnt_Names(JntNum),CS,Jnt_Names(JntNum),CS
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_FsaFe',a1/
     &         ' X SetScale y 0,0,"deg", ',a4,'_FscFe',a1/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_FsaAa',a1/
     &         ' X SetScale y 0,0,"deg", ',a4,'_FsaAa',a1/
     &         ' X SetScale /P x 0,',f10.6,',"sec", ',a4,'_FscIe',a1/
     &         ' X SetScale y 0,0,"deg", ',a4,'_FsaIe',a1)
      else
        if ( RegHeader ) then
          if ( CSType .eq. PRO_CS ) then
            write(2,130)
130         format( ' Finite Screw angles projected onto proximal LCS axes')
          else if ( CSType .eq. DIS_CS ) then
            write(2,140)
140         format( ' Finite Screw angles projected onto distal LCS axes')
          else ! JCS
            write(2,150)
150         format( ' Finite Screw angles projected onto JCS axes')
          end if
          write(2,160) Jnt_Names(JntNum)
160       format(' Angles (deg) for ',a4//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'//
     &           ' Angle order is flexion(+)/extension(-)'/
     &           '                adduction(+)/abduction(i)'/
     &           '                internal(+)/external(-)'/
     &           ' For Pelvis angles:  flexion (front lowers +)'/
     &           '                     obliquity (left side rises +)'/
     &           '                     rotation (right side forward +)'//
     &           ' Frame  Event   Flex/Ext     Ad/Ab        Int/Ext'/)
        end if
        if ( CSType .eq. PRO_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),(JfsAngPCS(i,frm,JntNum),i=1,3)
170         format(2x,i3,5x,i1,3(f13.5,1x))
          end do
        else if ( CSType .eq. DIS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),(JfsAngDCS(i,frm,JntNum),i=1,3)
          end do
        else ! JCS
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),(JfsAngJCS(i,frm,JntNum),i=1,3)
          end do
        end if        
      end if ! Igor or Regular
      Error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Finite screw joint angles have not been calculated yet')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntTrans( inc,RegHeader,IgorHeader,CSType,
     &                        JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 JntNum,inc,CSType
      logical*4 Error,RegHeader,IgorHeader
      
      real*4    TimeInc
      integer*4 frm,i,j
      character CS*1,CompName(4)*12
      
      Error= .true.
      if ( .not. JF.Translation ) goto 1000 ! error
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        if ( CSType .eq. PRO_CS ) then
          CS= 'P'
        else if ( CSType .eq. DIS_CS ) then
          CS= 'D'
        else if ( CSType .eq. GCS_CS ) then
          CS= 'G'
        end if
        if ( CSType .eq. JCS_CS ) then
          CompName(1)= jnt_names(JntNum)//'_JtrnMlJ'
          CompName(2)= jnt_names(JntNum)//'_JtrnApJ'
          CompName(3)= jnt_names(JntNum)//'_JtrnCdJ'
          CompName(4)= jnt_names(JntNum)//'_JtrnMgJ'
        else
          CompName(1)= jnt_names(JntNum)//'_Jtrnx'//CS
          CompName(2)= jnt_names(JntNum)//'_Jtrny'//CS
          CompName(3)= jnt_names(JntNum)//'_Jtrnz'//CS
          CompName(4)= jnt_names(JntNum)//'_JtrnMg'//CS
        end if
        write(2,100) (CompName(i),i=1,4)
100     format(' IGOR'/
     &         ' WAVES ',a12,', ',a12,', ', a12,', ',a12/
     &         ' BEGIN')
        if ( CSType .eq. PRO_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JtrnPCS(i,frm,JntNum),i=1,4)
110         format(2x,4(f13.5,1x))
          end do
        else if ( CSType .eq. DIS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JtrnDCS(i,frm,JntNum),i=1,4)
          end do
        else if ( CSType .eq. GCS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JtrnGCS(i,frm,JntNum),i=1,4)
          end do
        else ! JCS
          do frm= 1, MP.nfrm, inc
            write(2,110) (JtrnJCS(i,frm,JntNum),i=1,4)
          end do
        end if       
        write(2,119)
119     format(' END')
        do i= 1, 4
          write(2,120) TimeInc,CompName(i),CompName(i)
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a12/
     &         ' X SetScale y 0,0,"m", ',a12/ )
      else ! regular text export
        if ( RegHeader ) then
          if ( CSType .eq. PRO_CS ) then
            write(2,130)
130         format( ' Joint translation relative to proximal LCS axes')
          else if ( CSType .eq. DIS_CS ) then
            write(2,140)
140         format( ' Joint translation relative to distal LCS axes')
          else if ( CSType .eq. GCS_CS ) then
            write(2,145)
145         format( ' Joint translation relative to GCS axes')
          else ! JCS
            write(2,150)
150         format( ' Joint translation relative to JCS axes')
          end if
          write(2,160) Jnt_Names(JntNum)
160       format(' Translation (m) for ',a4//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'/)
          if ( CSType .eq. JCS_CS ) then
            write(2,165)
165         format(' Frame  Event   Med/Lat    Ant/Post     Comp/Dis   Mag'/)
          else
            write(2,167)
167         format(' Frame  Event',6x,'X',12x,'Y',12x,'Z',10x,'Mag'/)
          end if
        end if ! Print header
        if ( CSType .eq. PRO_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),(JtrnPCS(i,frm,JntNum),i=1,4)
170         format(2x,i3,5x,i1,4(f13.5,1x))
          end do
        else if ( CSType .eq. DIS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),(JtrnDCS(i,frm,JntNum),i=1,4)
          end do
        else if ( CSType .eq. GCS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),(JtrnGCS(i,frm,JntNum),i=1,4)
          end do
        else ! JCS
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),(JtrnJCS(i,frm,JntNum),i=1,4)
          end do
        end if        
      end if ! Igor or Regular
      Error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint translation has not been calculated yet')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJma( inc,RegHeader,IgorHeader,CSType,
     &                         JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 JntNum,inc,CSType
      logical*4 Error,RegHeader,IgorHeader
      
      real*4    TimeInc
      integer*4 frm,i,j
      character CS*1,CompName(4)*12
      
      Error= .true.
      if ( .not. JF.MomentArm ) goto 1000 ! error
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        if ( CSType .eq. PRO_CS ) then
          CS= 'P'
        else if ( CSType .eq. DIS_CS ) then
          CS= 'D'
        else ! GCS
          CS= 'G'
        end if
        CompName(1)= jnt_names(JntNum)//'_Jmoax'//CS
        CompName(2)= jnt_names(JntNum)//'_Jmoay'//CS
        CompName(3)= jnt_names(JntNum)//'_Jmoaz'//CS
        CompName(4)= jnt_names(JntNum)//'_JmoaMg'//CS
        write(2,100) (CompName(i),i=1,4)
100     format(' IGOR'/
     &         ' WAVES ',a12,', ',a12,', ', a12,', ',a12/
     &         ' BEGIN')
        if ( CSType .eq. PRO_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JmaPCS(i,frm,JntNum),i=1,3),
     &                    JmaGCS(4,frm,JntNum)
110         format(2x,4(f13.5,1x))
          end do
        else if ( CSType .eq. DIS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JmaDCS(i,frm,JntNum),i=1,3),
     &                    JmaGCS(4,frm,JntNum)
          end do
        else ! GCS
          do frm= 1, MP.nfrm, inc
            write(2,110) (JmaGCS(i,frm,JntNum),i=1,4)
          end do
        end if       
        write(2,119)
119     format(' END')
        do i= 1, 4
          write(2,120) TimeInc,CompName(i),CompName(i)
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a12/
     &         ' X SetScale y 0,0,"m", ',a12/)
      else ! regular text export
        if ( RegHeader ) then
          if ( CSType .eq. PRO_CS ) then
            write(2,130)
130         format( ' Joint load moment arm relative to proximal LCS axes')
          else if ( CSType .eq. DIS_CS ) then
            write(2,140)
140         format( ' Joint load moment arm relative to distal LCS axes')
          else ! GCS
            write(2,150)
150         format( ' Joint load moment arm relative to GCS axes')
          end if
          write(2,160) Jnt_Names(JntNum)
160       format(' Moment Arm (m) for ',a4//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'/
     &           ' Frame  Event',6x,'X',12x,'Y',12x,'Z',10x,'Mag'/)
        end if ! Print header
        if ( CSType .eq. PRO_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),
     &                   (JmaPCS(i,frm,JntNum),i=1,3),
     &                    JmaGCS(4,frm,JntNum)
170         format(2x,i3,5x,i1,4(1x,f13.5))
          end do
        else if ( CSType .eq. DIS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),
     &                   (JmaDCS(i,frm,JntNum),i=1,3),
     &                    JmaGCS(4,frm,JntNum)
          end do
        else ! GCS
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),
     &                   (JmaGCS(i,frm,JntNum),i=1,4)
          end do
        end if        
      end if ! Igor or Regular
      Error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint Moment Arm has not been calculated yet')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJwa( inc,RegHeader,IgorHeader,CSType,
     &                         JntNum,JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      integer*4 JntNum,inc,CSType
      logical*4 Error,RegHeader,IgorHeader
      
      real*4    TimeInc
      integer*4 frm,i,j
      character CS*1,CompName(4)*12
      
      Error= .true.
      if ( .not. JF.WrenchArm ) goto 1000 ! error
      if ( IgorHeader ) then
        TimeInc= float(inc)/rMtnFrmRate
        if ( CSType .eq. PRO_CS ) then
          CS= 'P'
        else if ( CSType .eq. DIS_CS ) then
          CS= 'D'
        else ! GCS
          CS= 'G'
        end if
        CompName(1)= jnt_names(JntNum)//'_Jwrax'//CS
        CompName(2)= jnt_names(JntNum)//'_Jwray'//CS
        CompName(3)= jnt_names(JntNum)//'_Jwraz'//CS
        CompName(4)= jnt_names(JntNum)//'_JwraMg'//CS
        write(2,100) (CompName(i),i=1,4)
100     format(' IGOR'/
     &         ' WAVES ',a12,', ',a12,', ', a12,', ',a12/
     &         ' BEGIN')
        if ( CSType .eq. PRO_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JwaPCS(i,frm,JntNum),i=1,3),
     &                    JwaGCS(4,frm,JntNum)
110         format(2x,4(f13.5,1x))
          end do
        else if ( CSType .eq. DIS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,110) (JwaDCS(i,frm,JntNum),i=1,3),
     &                    JwaGCS(4,frm,JntNum)
          end do
        else ! GCS
          do frm= 1, MP.nfrm, inc
            write(2,110) (JwaGCS(i,frm,JntNum),i=1,4)
          end do
        end if       
        write(2,119)
119     format(' END')
        do i= 1, 4
          write(2,120) TimeInc,CompName(i),CompName(i)
        end do
120     format(' X SetScale /P x 0,',f10.6,',"sec", ',a12/
     &         ' X SetScale y 0,0,"m", ',a12/)
      else ! regular text export
        if ( RegHeader ) then
          if ( CSType .eq. PRO_CS ) then
            write(2,130)
130         format( ' Joint load wrench arm relative to proximal LCS axes')
          else if ( CSType .eq. DIS_CS ) then
            write(2,140)
140         format( ' Joint load wrench arm relative to distal LCS axes')
          else ! GCS
            write(2,150)
150         format( ' Joint load wrench arm relative to GCS axes')
          end if
          write(2,160) Jnt_Names(JntNum)
160       format(' Wrench Arm (m) for ',a4//
     &           ' Gait Events :  RHS = 1   RTO = 3'/
     &           '                LHS = 2   LTO = 4'/
     &           ' Frame  Event',6x,'X',12x,'Y',12x,'Z',10x,'Mag'/)
        end if ! Print header
        if ( CSType .eq. PRO_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),
     &                   (JwaPCS(i,frm,JntNum),i=1,3),
     &                    JwaGCS(4,frm,JntNum)
170         format(2x,i3,5x,i1,4(1x,f13.5))
          end do
        else if ( CSType .eq. DIS_CS ) then
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),
     &                   (JwaDCS(i,frm,JntNum),i=1,3),
     &                    JwaGCS(4,frm,JntNum)
          end do
        else ! GCS
          do frm= 1, MP.nfrm, inc
            write(2,170) frm,GaitEvent(frm),
     &                   (JwaGCS(i,frm,JntNum),i=1,4)
          end do
        end if        
      end if ! Igor or Regular
      Error= .false.
      return

1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint Wrench Arm has not been calculated yet')        
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
      subroutine ExpJntAngRefCorAng( JF,OF,error )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_jnt.inc'
      
      record /joint_flag/   JF
      record /option_flag/  OF
      
      logical*4 error
      
      integer*4 i,j,seg,jnt

      Error= .true.
      if ( .not. JF.RefCorrectAng ) goto 1000 ! error
      write(2,100)
100   format(' Reference angle correction transformation angles'/
     &       ' Joint Segment',10x,'Reference Angles',10x
     &       ,'Experimental Data Angles',10x,'Correction Angles'/
     &       14x,3(4x,'Y',9x,'X',9x,'Z',5x)//)
      do i= 1, GP.num_jnt
        jnt= GP.jnt_num(i)
        seg= JntSegNum(distal,jnt)
        write(2,110) jnt_names(jnt),seg_names(seg),
     &               (JntRefAng(j,jnt),j=1,3),
     &               (ExpJntRefAng(j,jnt),j=1,3),
     &               (JntAngRefCorAng(j,jnt),j=1,3)
110     format(2x,a4,3x,a4,2x,9e13.5)  
      end do
      Error= .false.
      return
      
1000  write(6,1005)      
      if ( OF.ToLog ) write(3,1005)
1005  format(' Error- Joint reference angle correction not been calculated yet')        
      return
      end
      
