c
c                
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: This file contains routines to export to text files information describing
c              the kinematics of markers analyzed by ANZ.
c
      subroutine ExportMrk( CMND,inc,RegHeader,IgorHeader,
     &                      MF,OF )
      implicit none
      include 'a_const.inc'

      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_mrk.inc'
      include 'a_names.inc'
                          
      record /CmndLine/    CMND
      record /marker_flag/ MF
      record /option_flag/ OF    

      integer*4 inc
      logical*4 IgorHeader,RegHeader

      real*4    TimeInc
      integer*4 i,j,k,Mrk,NumExpMrkVar,ExpVarNum,CSType,NumOpt,ExpVarNumCmnd(3),
     &          SegNum,NumCmnd,NumDist,Start,Num,NumMrk,Len,LnEnd,ns,frm
      character LineText*80,ExpMrkVarName(3)*3,OptText(5)*3,SegText*3,
     &          DistName*6,MrkText*4,CompName(3)*1,MrkrName(6)*10,
     &          IgorText*80,chnum(15)*2
      logical*4 AllMarkers,OpenTextFile,Error,GetCSType,GetMrkNum,GetSegNum,
     &          ExpToVis
      
      data ExpMrkVarName / 'POS','DIS','VAA' /
      data NumExpMrkVar  / 3 /
      data ExpVarNumCmnd / 5,5,5 /
      data CompName / 'x','y','z' /
      data chnum /'1 ','2 ','3 ','4 ','5 ','6 ','7 ','8 ',
     &            '9 ','10','11','12','13','14','15' /
c
c     Check validity of export quantity name
c
      Error= .true.
      do i= 1, NumExpMrkVar
        if ( CMND.CmndText(3) .eq. ExpMrkVarName(i) ) then
          Error= .false.
          ExpVarNum= i
        end if
      end do
      if ( Error ) goto 1000 ! bad name
c
c     Now check options if present
c
      CSType= GCS_CS
      ExpToVis= .false.
      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
        if ( CMND.NumOpt(3) .gt. 1 ) goto 1040
        if ( (OptText(1) .eq. 'VIS') .or. (OptText(1) .eq. 'ANI') ) then
          ExpToVis= .true.
        else
          if ( .not. GetCSType( NumOpt,OptText,CSType) ) goto 1050
          if ( CSType .gt. LCS_CS ) goto 1050
        end if
      end if
c
c     Open Text output file...
c
      NumCmnd= ExpVarNumCmnd(ExpVarNum)
      if ( (ExpVarNum .eq. 1) .and. ( CSType .eq. 2) ) NumCmnd= 6
      if ( ExptoVis ) NumCmnd= 4
      if ( CMND.NumCmnd .lt. NumCmnd ) goto 1070
      LineText= CMND.Text(1) 
      Start= CMND.CmndStart(NumCmnd)
      LnEnd= CMND.CmndEnd(NumCmnd)
      if ( .not. ExpToVis ) then
        if ( .not. OpenTextFile(LineText,Start,.true.) ) goto 1030 ! error
      end if
c                             
c     Now dump whatever has been requested
c                                      
      TimeInc= float(inc)/rMtnFrmRate
      if ( ExpVarNum .eq. 1 ) then
c
c       Dump VIS files of marker shapes and motions
c
        if ( ExpToVis ) then
          LineText(LnEnd+1:LnEnd+4)= '.BYU'
          if ( .not. OpenTextFile(LineText,Start,.true.) ) goto 1030 ! error
          ns= MP.num_mrk
          write(2,600) ns,8*ns,6*ns,24*ns,0
600       format(5i5)
          do i= 1, ns
            write(2,610) 6*i-5,6*i        
610         format(2i5)
          end do
          do i= 1, MP.num_mrk
            write(2,247)  0.01, 0.01,-0.01
247         format(1x,3f10.4)
            write(2,247) -0.01, 0.01,-0.01
            write(2,247) -0.01,-0.01,-0.01
            write(2,247)  0.01,-0.01,-0.01
            write(2,247)  0.01, 0.01, 0.01
            write(2,247) -0.01, 0.01, 0.01
            write(2,247) -0.01,-0.01, 0.01
            write(2,247)  0.01,-0.01, 0.01
          end do
          do i= 1, ns
            j= 8*(i-1)
            write(2,620) j+1,j+5,j+6,-(j+2)
            write(2,620) j+4,j+8,j+5,-(j+1)
            write(2,620) j+3,j+7,j+8,-(j+4)
            write(2,620) j+2,j+6,j+7,-(j+3)
            write(2,620) j+5,j+8,j+7,-(j+6)
            write(2,620) j+4,j+1,j+2,-(j+3)
620         format(4i5)
          end do
          close(unit=2)
          LineText(LnEnd+1:LnEnd+4)= '.ANI'
          if ( .not. OpenTextFile(LineText,Start,.true.) ) goto 1030 ! error
          do frm= 1, MP.nfrm,inc                                
            write(2,630) frm,float(frm-1)*timeinc+timeinc
630         format(1x,i4,1x,f12.6)
            do i= 1, MP.num_mrk                     
              write(2,640) (MrkData(j,frm,i),j=1,3),0.0,0.0,0.0,0.0
640           format(1x,7(1x,e12.5))
            end do
          end do

        else if ( CSType .eq. GCS_CS ) then
          if ( .not. MF.read ) goto 1090 ! error
          MrkText= CMND.Text(1)(CMND.CmndStart(4):CMND.CmndStart(4)+3)
          Num=  CMND.CmndIntVal(4)
          if ( .not. GetMrkNum( MrkText,Num,Mrk,AllMarkers ) ) goto 1020
          if ( AllMarkers ) then
            do i= 1, MP.num_mrk
              write(2,200) MP.mrk_name(i),i
200           format(/' Marker Name: ',a4,'(',i2,')'/)
              do j= 1, MP.nfrm, inc
                write(2,205) j,(MrkData(k,j,i),k=1,3)
205             format(1x,i4,3(1x,f10.5))
              end do
            end do
          else                                      
            if ( IgorHeader ) then
              if ( mrk .lt. 10 ) then
                do i= 1, 3
                  MrkrName(i)= 'Mrk'//MrkText(1:1)//CompName(i)
                end do
              else
                do i= 1, 3
                  MrkrName(i)= 'Mk'//MrkText(1:2)//CompName(i)
                end do
              end if
              write(2,250) (MrkrName(i)(1:5),i=1,3)
250           format(' IGOR'/' WAVES ',2(a5,', '),a5/' BEGIN')
              do i= 1, MP.nfrm,inc
                write(2,255) (MrkData(j,i,Mrk),j=1,3)
255             format(1x,3f10.5)
              end do
              write(2,269)
269           format(' END')
              do i= 1, 3
                write(2,270) TimeInc,MrkrName(i)(1:5),MrkrName(i)(1:5)
              end do
270           format(' X SetScale /P x 0,',f10.6,',"sec", ',a5/
     &               ' X SetScale y 0,0,"m", ',a5/ )
            else 
              if ( RegHeader ) write(2,260) Mrk,MP.mrk_name(Mrk)
260           format(' Position data for marker # ',i2,2x,a4/
     &               ' Frame   x         y         z'/)
              do i= 1, MP.nfrm,inc
                write(2,210) i,(MrkData(j,i,Mrk),j=1,3)
210             format(1x,i4,3f10.5)
              end do
            end if
          end if
          
        else ! LCS_CS
          if ( .not. MF.diagnostics ) goto 1060 ! error
          SegText= CMND.CmndText(4)
          if ( .not. GetSegNum( SegText,SegNum ) ) goto 1080
          NumMrk= SP(SegNum).num_mrk
          MrkText= CMND.Text(1)(CMND.CmndStart(5):CMND.CmndStart(5)+3)
          Num=  CMND.CmndIntVal(5)
          if ( .not. GetMrkNum( MrkText,Num,Mrk,AllMarkers ) ) goto 1020
          Error= .true.
          do i= 1, NumMrk
            if ( Mrk .eq. SP(SegNum).mrk_num(i) ) then
              Error= .false.
              Num= i
            end if
          end do
          if ( Error ) goto 2020 ! bad marker #
          if ( IgorHeader ) then
            if ( mrk .lt. 10 ) then
              do i= 1, 3
                MrkrName(i)= seg_names(SegNum)//'_Mrk'//MrkText(1:1)//CompName(i)
              end do
            else
              do i= 1, 3
                MrkrName(i)= seg_names(SegNum)//'_Mk'//MrkText(1:2)//CompName(i)
              end do
            end if
            write(2,300) (MrkrName(i),i=1,3)
300         format(' IGOR'/' WAVES ',2(a10,', '),a10/' BEGIN')
            do i= 1, MP.nfrm,inc
              write(2,255) (MpnLCS(j,Num,i,SegNum),j=1,3)
            end do
            write(2,269)
            do i= 1, 3
              write(2,310) TimeInc,MrkrName(i),MrkrName(i)
            end do
310         format(' X SetScale /P x 0,',f10.6,',"sec", ',a10/
     &             ' X SetScale y 0,0,"m", ',a10/ )
          else 
            if ( RegHeader ) write(2,320) Mrk,MP.mrk_name(Mrk),
     &                                    seg_names(SegNum)
320         format(' Position data for marker # ',i2,2x,a4/
     &             ' In the Local CS of ',a4,' Segment'/
     &             ' Frame   x         y         z'/)
            do i= 1, MP.nfrm,inc
              write(2,210) i,(MpnLCS(j,Num,i,SegNum),j=1,3)
            end do
          end if
        end if ! GCS or LCS
        close(unit=2)

      else if ( ExpVarNum .eq. 2 ) then ! Intermarker distance
        if ( .not. MF.diagnostics ) goto 2030 ! error
        SegText= CMND.CmndText(4)
        if ( .not. GetSegNum( SegText,SegNum ) ) goto 1080
        NumDist= SP(SegNum).num_MrkDist
        if ( IgorHeader ) then
          DistName= seg_names(SegNum)//'_d'
          do i= 1, 80
            IgorText(i:i)= ' '
          end do
          Len= 0
          do i= 1, NumDist-1
            IgorText(Len+1:Len+9)= DistName//chnum(i)//', '
            Len= Len+9
          end do
          IgorText(Len+1:Len+7)= DistName//chnum(NumDist)
          Len= Len+7
          write(2,400) IgorText
400       format(' IGOR'/
     &           ' WAVES ',a80/
     &           ' BEGIN')
          do i= 1, MP.nfrm,inc
            write(2,410) (MrkDist(j,i,SegNum),j=1,NumDist)
410         format(1x,6f10.6)
          end do
          write(2,415)
415       format(' END')
          do i= 1, NumDist
            write(2,420) TimeInc,DistName,i,DistName,i
420         format(' X SetScale /P x 0,',f10.6,',"sec", ',a6,i1/
     &             ' X SetScale y 0,0,"m", ',a6,i1 )
          end do
        else 
          if ( RegHeader ) write(2,430) seg_names(SegNum),(i,i=1,NumDist)
430       format(' Intermarker distance for ',a4,' segment'/
     &           ' Frame',6(4x,'d',i1,4x)/ )
          do i= 1, MP.nfrm,inc
            write(2,440) i,(MrkDist(j,i,SegNum),j=1,NumDist)
440         format(1x,i4,6f10.6)
          end do 
        end if

      else if ( ExpVarNum .eq. 3 ) then ! marker vel/accel
        if ( .not. MF.VelAcc ) goto 2000 ! error
        MrkText= CMND.Text(1)(CMND.CmndStart(4):CMND.CmndStart(4)+3)
        Num=  CMND.CmndIntVal(4)
        if ( .not. GetMrkNum( MrkText,Num,Mrk,AllMarkers ) ) goto 1020
        if ( AllMarkers ) goto 2010
        if ( IgorHeader ) then
          if ( mrk .lt. 10 ) then
            do i= 1, 3
              MrkrName(i)=   'Mrk'//MrkText(1:1)//'V'//CompName(i)
              MrkrName(i+3)= 'Mrk'//MrkText(1:1)//'A'//CompName(i)
            end do
          else
            do i= 1, 3
              MrkrName(i)=   'Mk'//MrkText(1:2)//'V'//CompName(i)
              MrkrName(i+3)= 'Mk'//MrkText(1:2)//'A'//CompName(i)
            end do
          end if
          write(2,500) (MrkrName(i)(1:6),i=1,6)
500       format(' IGOR'/' WAVES ',5(a6,', '),a6/' BEGIN')
          do i= 1, MP.nfrm,inc
            write(2,510) (MrkVel(j,i,Mrk),j=1,3),(MrkAcc(j,i,Mrk),j=1,3)
510         format(1x,6f10.5)
          end do
          write(2,269)
          do i= 1, 3
            write(2,520) TimeInc,MrkrName(i)(1:6),MrkrName(i)(1:6)
          end do
520       format(' X SetScale /P x 0,',f10.6,',"sec", ',a6/
     &           ' X SetScale y 0,0,"m/s", ',a6/ )
          do i= 4, 6
            write(2,521) TimeInc,MrkrName(i)(1:6),MrkrName(i)(1:6)
          end do
521       format(' X SetScale /P x 0,',f10.6,',"sec", ',a6/
     &           ' X SetScale y 0,0,"ms2", ',a6/ )
        else 
          if ( RegHeader ) write(2,530) Mrk,MP.mrk_name(Mrk)
530       format(' Velocity and Acceleration data for marker # ',i2,2x,a4/
     &           '   (m/s)         (m/s^2)'//
     &           ' Frame',4x,'vx',8x,'vy',8x,'vz',8x,'ax',8x,'ay',8x,'az'/)
          do i= 1, MP.nfrm,inc
            write(2,540) i,(MrkVel(j,i,Mrk),j=1,3),(MrkAcc(j,i,Mrk),j=1,3)
540         format(1x,i4,6f10.5)
          end do
        end if
        close(unit=2)
      end if
      return

1000  write(6,1005) CMND.CmndText(3)
      if ( OF.ToLog ) write(3,1005) CMND.CmndText(3)
1005  format(' Error- ',a3,' is not a valid EXPort MaRKer command'/
     &       '        Valid commands are: POSition'/
     &       '                            DIStance'/
     &       '                            Velocity And Acceleration')
      return
                                    
1020  write(6,1025) MrkText,MP.num_mrk,
     &              (MP.mrk_name(i),i=1,MP.num_mrk)
      if ( OF.ToLog ) write(3,1025) MrkText,MP.num_mrk,
     &                               (MP.mrk_name(i),i=1,MP.num_mrk)
1025  format(' Error- ',a3,' is not a valid marker identifier'/
     &       '        Valid marker #''s are 1 to ',i2,' and ALL as well as'/
     &       3('              ',10(a4,1x)/))
      return                                                 

1030  write(6,1035)                 
      if ( OF.ToLog ) write(3,1035)
1035  format(' Error- Output text file could not be opened')        
      return

1040  write(6,1045)                 
      if ( OF.ToLog ) write(3,1045)
1045  format(' Error- Too many options for EXPort MaRKer command'/
     &       '        Only one option allowed for EXP MRK POS only'/
     &       '        Valid options are: /GCS /LCS /VIS')        
      return

1050  write(6,1055)                 
      if ( OF.ToLog ) write(3,1055)
1055  format(' Error- Invalid Coord System option for EXPort MaRKer POSition command'/
     &       '        Only Coord Systems allowed are [GCS,LCS]')        
      return
      
1060  write(6,1065)
      if ( OF.ToLog ) write(3,1065)
1065  format(' Error- Marker position data in the segment LCS is not available')
      return

1070  write(6,1075) NumCmnd
      if ( OF.ToLog ) write(3,1075) NumCmnd
1075  format(' Error- Not enough entries given for EXPort MaRKer command'/
     &       '        Command must have ',i1,' entries')
      return
      
1080  write(6,1085) SegText,(seg_names(GP.seg_num(i)),
     &                      i= 1, GP.num_seg)
      if ( OF.ToLog ) write(6,1085) SegText,
     &                         (seg_names(GP.seg_num(i)),
     &                         i= 1, GP.num_seg)
1085  format(' Error- ',a3,' is not a valid segment name'/
     &       '        Valid segment names are: ',5a5/2(33x,5a5/))
      return

1090  write(6,1095)
      if ( OF.ToLog ) write(3,1095)
1095  format(' Error- Marker position data is not available')
      return

2000  write(6,2005)
      if ( OF.ToLog ) write(3,2005)
2005  format(' Error- Marker vel/accel data is not available')
      return

2010  write(6,2015)
      if ( OF.ToLog ) write(3,2015)
2015  format(' Error- The ALLmarkers selection is not available here')
      return

2020  write(6,2025) Mrk,seg_names(SegNum),
     &              (SP(SegNum).mrk_num(i),
     &               i=1,SP(SegNum).num_mrk)
      if ( OF.ToLog ) write(3,2025) Mrk,seg_names(SegNum),
     &                         (SP(SegNum).mrk_num(i),
     &                         i=1,SP(SegNum).num_mrk)
2025  format(' Error- ',i2,' is not a valid marker # for ',a4,' segment'/
     &       '        Valid marker #''s are : ',6i3)
      return

2030  write(6,2035)
      if ( OF.ToLog ) write(3,2035)
2035  format(' Error- InterMarker distance data is not available')
      return
      end                     
