c
c
c     subroutine read_anz_file
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Read in the binary file containing the results of a previous analysis
c              by ANZ. This file is a direct access binary that contains all the
c              various quantities. There can be a few or a large number of different
c              quantities inside the file. The file is written in 512 byte blocks. The
c              first contains pointers to the first element of all the different
c              quantities. The meaning of each the elements in this first block can be
c              determined by looking at the descriptive names associated with them in
c              the file a_file.inc. The equivalence of the structure anz_layout
c              shows this layout. The second block contains the scaling factors for
c              each of the data sections. All data is stored as four byte integers
c              rather floating point. This was done to make the files more transportable
c              between computer systems. Routines such as read_array_1dim will read in
c              the integer data and rescale it to floating point numbers. This 
c              transportability was tested by moving the binary files over to a Macintosh
c              and reading them using a version of ANZ compiled on it. The byte order
c              is swapped on the macintosh relative to the VAX so there are provisions
c              in the code here to swapped the ordering around as the data is read. It
c              works quit well and will detect that the swapping is needed automatically.
c
c              To find out what is in an ANZ data file, read the file in and use the
c              status commands to see what results the file contains.
c
c              For the results of the ANZ analyses to be used by other computer programs
c              either the ANZ file format must be directly supported- as in TELIO and
c              SHOW3D - or the results must be exported in text form using the numerous
c              export options.
c
c              As the data is read in a running display is provided as to the status of
c              the reading process. This can be a problem if the program is run under
c              batch mode because large log files will result. The updating can be 
c              turned off with the command OPTion GENeral NoUPdate.
c
c              Special provisions are made to detect arrays in the ANZ files that are
c              larger than the arrays allocated in the common data blocks. The only
c              work around for this problem is to recompile the program with the 
c              constants in a_const.inc set larger. The only way this situation can
c              occur is if the ANZ file was produced by another verison of ANZ which
c              had larger array sizes than the present ANZ program. This has happenned
c              to me because I produced a regular and large memory version of ANZ for
c              the Ohio State Gait Lab. The large version was only used on very slow
c              moving patients who produced enormous data files. This version used very
c              large amounts of system resources, so it was used as little as possible.
c              Also, special versions of TELIO and SHOW3D are needed to produce graphical
c              views of the results. The moral of the story - stick to a single maximum
c              number of frames for motion and electical data as defined by the constants
c              in a_const.inc and you'll never see this problem.
c
c              Lastly, you'll notice some special logicals called CompactSegData and
c              UnCompactSegData. These indicate that for certain analyses the segment
c              (and possibly joint) data is stored noncontiguously in the data arrays
c              and if the arrays were just written to disk alot of space would be 
c              wasted with unused memory locations. SO... These flags invoke compacting
c              and uncompacting of the arrays when writing and reading, respectively,
c              the results to and from the disk files. This situation occurs when the
c              upper extremity only marker is used, as an example. The standard full
c              body marker set used by OSU will not have blanks- all the data elements
c              are used.
c
c              NOTE: There are sections of code commented out in this routine which
c                    are specific to the Macintosh version of ANZ. This verison was
c                    produced for my personal use away form the Gait Lab using the
c                    FORTRAN compiler from Language Systems. It worked quite well but
c                    used HUGE amounts of RAM because of the static allocation of arrays
c                    done in FORTRAN- something like 8-10 Megabytes ! The Macintosh code
c                    left for those who might want to experiment, but I don't recommend
c                    this approach. ANZ ought to be rewritten in C if it is to run on
c                    a small platform like a Mac or PC. This way the memory allocation
c                    will only take place when the actual data is calculated rather than
c                    blocking out memory for all possible calculable quantities as ANZ
c                    does now on starting up. By the way, all the IGOR options in the
c                    export commands were for MY benefit so that the exported data 
c                    would be formatted properly for the Macintosh scientific analysis
c                    and display program Igor. I used this program constantly in my
c                    research and recommend it whole heartedly.
c
c	Routines called:
c     cycle_itor
c     force_itor
c     general_itor
c     read_1dim_array
c     read_2dim_array
c     read_3dim_array
c     read_4dim_array
c     SwapInteger
c     SwapIntegerArray
c     SwapLogical
c     SwapLogicalArray
c     WriteStatus
c     zero_force_flags
c     zero_motion_flags
c
c
c	Common Block Variables MODIFIED by routine:
c	Block Name		Variable Name
c
c     Just about EVERYTHING in the common blocks gets modified here...
c
      subroutine read_anz_file( filename,MF,SF,FF,JF,AF,CF,EF,OF,
     &                          data_read,SwapBytes,NoFlags,Skip1 )
      implicit none
      include 'a_const.inc'

      include 'a_struct.inc'
      include 'a_file.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_mrk.inc'
      include 'a_seg.inc'                             
      include 'a_gls2.inc'
      include 'a_frc.inc'
      include 'a_jnt.inc'
      include 'a_enr.inc'
      include 'a_emg.inc'

      record /marker_flag/    MF
      record /segment_flag/   SF
      record /force_flag/     FF
      record /joint_flag/     JF                      
      record /avg_pos_flag/   AF
      record /cycle_flag/     CF
      record /emg_flag/       EF
      record /option_flag/    OF
      record /MiscFlags/      MISC_FLG
                                                  
      record /anz_layout/        ptr,scale
      record /general_param_INT/ GP_ANZ
      record /motion_param_VerB/ MP_VERB
      record /force_param_INT/   FP_ANZ
      record /seg_param_INT/     SP_ANZ(MAX_SEG)
      record /cycle_INT/         CP_ANZ
      record /AllOldFlags/       FLAGS
      record /SegJntInfo/        SgJtInfo
                                      
      character filename*60 
      logical*4 data_read,SwapBytes,NoFlags,Skip1,rbody
               
      integer*4 error,pntr_num,i,j,k,seg_num,NumVar,NumVarTotal,
     &          VarNum(128),VarCnt,RorW,seg
      real*4    temp(MAX_MTN),temp2(MAX_MTN,MAX_SEG),temp3(MAX_ELC,2),
     &          temp4(MAX_MTN,2),temp5(MAX_ELC,4),temp6(MAX_ELC),
     &          jvrefnum
      logical*4 UnCompactJntData,UnCompactSegData,LogTemp,SetFlags,
     &          InttoLog
c
c     Open file
c
      RorW= 0
      data_read= .false.
c														! VAX
c      This code will open the ANZ file on the VAX		! VAX
c														! VAX
      open(unit=2,file=filename,status='old',access='direct',recl=512,
     &     form='unformatted',recordtype='fixed',iostat=error,err=1000)
c																	! MAC
c      This code will open the ANZ file on the Macintosh			! MAC
c																	! MAC
c      if ( filename(1:1) .eq. '*' ) then							! MAC
c        open( unit=2,file=*,status='old',access='direct',recl=512,	! MAC
c     &        form='unformatted',creator='DaM ',filetype='Data',	! MAC
c     &        iostat=error,err=1000 )
c        i= jvrefnum(2)
c        call f_setvolume(i)								! MAC
c      else															! MAC
c        open( unit=2,file=filename,status='old',access='direct',recl=512,	! MAC
c     &        form='unformatted',creator='DaM ',filetype='Data',			! MAC
c     &        iostat=error,err=1000 )								! MAC
c      end if														! MAC
c                   
c     Read the pointer and scaling blocks
c
      pntr_num= 1
      if ( Skip1 ) pntr_num= 2              
      read(2,rec=pntr_num,err=1010,iostat=error) ptr.array    ! will this read the
      if ( SwapBytes ) call SwapIntegerArray( 128,ptr.array )
      pntr_num= pntr_num + 1
      read(2,rec=pntr_num,err=1010,iostat=error) scale.array  ! entire array ?????
      if ( SwapBytes ) call SwapIntegerArray( 128,scale.array )
*$ifdef DEBUG_SHOW3D
      do i= 1, 128
         write(3,700) AnzVarName(i),ptr.array(i),scale.array(i)
 700     format(1x,a20,1x,i8,1x,i8)
      end do
*$endif
c
c     Scan the pointer block and figure out how many individual
c     quantities are going to be read in.  This will be used for
c     a status display
c                      
      NumVar= 0
      VarCnt= 0      
      NumVarTotal= ptr.array(128)                                              
      if ( NumVarTotal .gt. 128 ) NumVarTotal= 110 ! there is a problem....
      do i= 1, NumVarTotal ! at present total number of variables is 110
        if ( ptr.array(i) .ne. 0 ) then
          NumVar= NumVar + 1
          VarNum(NumVar)= i                                  
        end if
      end do
c           
c     Read in the parameter structures.
c     Convert the GEN,FRC,MTN,SEG,CYC structures from integer to real format
c     This conversion is done so that the ANZ file contains only integers
c     and characters in the hope that the file will be device independent.
c     Thus, the same ANZ file could be used on a VAX, UNIX box or Macintosh.
c
      call WriteStatus(RorW, Ptr.PRM_General,Ptr,NumVar,NumVarTotal,
     &                 VarCnt,VarNum,OF )
      read(2,rec=ptr.PRM_general,err=1010,iostat=error) GP_ANZ.array
      call general_itor( GP_ANZ, scale.PRM_general, GP, SwapBytes )
*$ifdef DEBUG_SHOW3D
      write(3,710)
 710  format(' General Parameters read')
*$endif
c
c     Check of byteorder number in GP structure- This should always be equal
c     to 1.  If it is not then give error message indicating that byteorder is
c     probably not correct.  This will occur if an ANZ file created on a VAX and
c     transfered to a Macintosh and read without the rea/byte switch.  The 
c     converse is also true- creating the file on a macintosh and sending it to
c     a VAX later on.
c
      if ( GP.ByteOrderCheck .ne. 1 ) write(6,111) GP.ByteOrderCheck
111     format(/' Warning- The byte order check number has a value of ',
     &  i8/10x,'It should be 1 if the file is being correctly read.'/
     &  '          The data is probably being read incorrectly.'/
     &  '          Try reading the file with the switch /byte'//)
c
c     Check the storage order of the segment and joint data and see if the
c     data must be uncompacted when it is read in
c
      UnCompactJntData= .false.
      do i= 1, GP.num_jnt-1
        if ( abs(GP.jnt_num(i)-GP.jnt_num(i+1)) .ne. 1 )
     &    UnCompactJntData= .true.
      end do                                                              
      if ( GP.jnt_num(1) .ne. 1 ) UnCompactJntData= .true.
      UnCompactSegData= .false.
      do i= 1, GP.num_seg-1
        if ( abs(GP.seg_num(i)-GP.seg_num(i+1)) .ne. 1 )
     &    UnCompactSegData= .true.
      end do
      if ( GP.seg_num(1) .ne. 1 ) UnCompactSegData= .true.      
c
c     Read in the flags which indicate what and how calculations had
c     been carried out in the ANZ file
c                                                                 
      SetFlags= .true.
      if ( (Ptr.FLAGS .ne. 0) .and. (.not. NoFlags) ) then
        if ( GP.FileFormatVersion .eq. 'a'  ) then
          write(6,502) GP.FileFormatVersion,ANZFileVersion              
502       format(
     &    /' NOTE - This file''s format is version "',a1,'" whereas'/
     &     '        this version of ANZ reads format version "',a1,'"'/
     &     '        Most data can be read but some FLAGS data and the'/
     &     '        calculation history will not be read properly'//)
          SetFlags= .false.
          call WriteStatus(RorW, Ptr.FLAGS,Ptr,NumVar,NumVarTotal,
     &                     VarCnt,VarNum,OF )
          read(2,rec=Ptr.FLAGS,err=1010,iostat=error) FLAGS.array    ! will this read the
          if ( SwapBytes ) then
            call SwapLogicalArray( 56,FLAGS.array )
            do i= 67, 71 ! EMG flags
              LogTemp= InttoLog(FLAGS.array(i))
              call SwapLogical( LogTemp,FLAGS.array(i) )
            end do
            i= FLAGS.array(72) ! EMG.FLG.NumProcOp
            call SwapInteger( i,FLAGS.array(72) )
          end if
          do i= 1, 7
            MF.array(i)= FLAGS.MRK.array(i)
          end do
          MF.NumProcOps= 0
          do i= 1, 12
            SF.array(i)= FLAGS.SEG.array(i)
          end do
          SF.NumProcOps= 0
          do i= 1, 12
            JF.array(i)= FLAGS.JNT.array(i)
          end do              
          JF.NumProcOps= 0 
          do i= 1, 5
            AF.array(i)= FLAGS.AVG.array(i)
          end do              
          do i= 1, 26
            FF.array(i)= FLAGS.FRC.array(i)
          end do
          do i= 1, 56
            EF.array(i)= FLAGS.EMG.array(i)
          end do
          History(1).NumHistory= 1             
          History(1).NumLine= 2
          History(1).Line(1)= 'ANZ file version "A"'
          History(1).Line(2)= filename(1:20)
          MF.NumProcOps= 0
          SF.NumProcOps= 0
          JF.NumProcOps= 0
        else if ( ichar(GP.FileFormatVersion) .ge. ichar('b') ) then
          SetFlags= .false.
          call WriteStatus(RorW, Ptr.FLAGS,Ptr,NumVar,NumVarTotal,
     &                     VarCnt,VarNum,OF )
          read(2,rec=Ptr.FLAGS,err=1010,iostat=error)   MF.array
          read(2,rec=Ptr.FLAGS+1,err=1010,iostat=error) SF.array
          read(2,rec=Ptr.FLAGS+2,err=1010,iostat=error) JF.array
          read(2,rec=Ptr.FLAGS+3,err=1010,iostat=error) FF.array
          read(2,rec=Ptr.FLAGS+4,err=1010,iostat=error) EF.array
          read(2,rec=Ptr.FLAGS+5,err=1010,iostat=error) MISC_FLG.array
          if ( SwapBytes ) then
            call SwapLogicalArray( 7,MF.array )
            i= MF.NumProcOps
            call SwapInteger( i,MF.NumProcOps )
            call SwapLogicalArray( 12,SF.array )
            call SwapLogical( SF.array(24) )  ! added for cc marker set
            call SwapLogical( SF.array(25) )  ! dam 10/92
            i= SF.NumProcOps
            call SwapInteger( i,SF.NumProcOps )
            call SwapLogicalArray( 16,JF.array )
            i= JF.NumProcOps
            call SwapInteger( i,JF.NumProcOps )
            call SwapLogicalArray( 16,FF.array )
            call SwapLogicalArray( 5,EF.array )
            i= EF.NumProcOps
            call SwapInteger( i,EF.NumProcOps )
            call SwapLogicalArray( 12,MISC_FLG.array )
            CF= MISC_FLG.CycFlag
            AF= MISC_FLG.AvgFlag
            OF= MISC_FLG.OptFlag
          end if
        else
          write(6,500) GP.FileFormatVersion,ANZFileVersion
500       format(/
     &    ' NOTE - This file''s format is version "',a1,'" whereas'/
     &    '        this version of ANZ reads format version "',a1,'"'/
     &    '        Most data can be read but some FLAGS data and the'/
     &    '        calculation history will not be read properly'/)
          History(1).NumHistory= 1
          History(1).NumLine= 2
          History(1).Line(1)= 'ANZ file version "0"'
          History(1).Line(2)= filename(1:20)
          MF.NumProcOps= 0                   
          SF.NumProcOps= 0
          JF.NumProcOps= 0
        end if ! file format is new enough to contain proper flags
      end if ! flags are present and not skipped by manual override

      if ( ptr.PRM_motion .ne. 0 ) then
        call WriteStatus(RorW, Ptr.PRM_Motion,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        if ( ichar(GP.FileFormatVersion) .lt. ichar('c') ) then
          read(2,rec=ptr.PRM_motion,err=1010,iostat=error) MP_VERB.array
          if ( SwapBytes ) call SwapIntegerArray( 128,MP_VERB.array )
          do i= 1, 128
            MP.array(i)= MP_VERB.array(i)
          end do
          do i= 1, 30
            MP.mrk_name(i)= MP_VERB.mrk_name(i)
          end do
          MP.first_frm= MP_VERB.first_frm
        else
          read(2,rec=ptr.PRM_motion,err=1010,iostat=error) MP.array
          if ( SwapBytes ) call SwapIntegerArray( 128,MP.array )
        end if
        if ( NoFlags ) then
          rMtnFrmRate= float(MP.frm_rate)
        else
          rMtnFrmRate= 0.001*float(MP.frm_rate)
          MP.frm_rate= MP.frm_rate*0.001
        end if
        if ( MP.num_cycle .ne. 0 ) then
          CF.event= .true.        ! reconstruct event array
          do i= 1, MP.nfrm     
            GaitEvent(i)= 0
          end do
          do i= 1, MP.num_rhs
            GaitEvent(MP.rhs_frm(i))= 1
          end do
          do i= 1, MP.num_lhs
            GaitEvent(MP.lhs_frm(i))= 2
          end do
          do i= 1, MP.num_rto
            GaitEvent(MP.rto_frm(i))= 3
          end do
          do i= 1, MP.num_lto
            GaitEvent(MP.lto_frm(i))= 4        
          end do
        end if
      end if

      if ( ptr.PRM_force .ne. 0 ) then
        call WriteStatus(RorW, Ptr.PRM_Force,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        read(2,rec=ptr.PRM_force,err=1010,iostat=error) FP_ANZ.array
        call force_itor( FP_ANZ, scale.PRM_force, FP, SwapBytes )
      end if                            

      if ( ptr.PRM_cycle .ne. 0 ) then ! gait cycle statistics are present
        call WriteStatus(RorW, Ptr.PRM_Cycle,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        read(2,rec=ptr.PRM_cycle,err=1010,iostat=error) CP_ANZ.array
        call cycle_itor( CP_ANZ, scale.PRM_cycle, CP, SwapBytes )
        CF.statistics= .true.                         
      end if

      if ( ptr.PRM_EMG .ne. 0 ) then   
        call WriteStatus(RorW, Ptr.PRM_EMG,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        read(2,rec=ptr.PRM_EMG,err=1010,iostat=error) EP.array
        if ( SwapBytes ) then
          call SwapInteger( EP.NumChnl,EP.NumChnl )
          call SwapInteger( EP.NumSamples,EP.NumSamples )
          call SwapInteger( EP.SampleRate,EP.SampleRate )
          call SwapIntegerArray( 16,EP.CodeNum )
          do i= 1, 16
            call SwapLogical( EP.Surface(i),EP.Surface(i) )
          end do
        end if
c
c       This is a big time kludge to fix a latent bug which I can't seem to
c       find which rears its ugly head by setting the number of EMG samples equal
c       to 0 while not seeming to affect anything else.  dam 12/92  
c
        if ( EP.NumSamples .eq. 0 ) then
          if ( FP.frm_rate .gt. 0 ) then
            EP.NumSamples= FP.nfrm*(EP.SampleRate/FP.frm_rate)
          else
            EP.NumSamples= 1
          end if
        end if
      end if
c                          
c     Read in ANZ command history
c
      if ( (GP.FileFormatVersion .ge. 'b') .and.
     &     (Ptr.History .ne. 0)                        ) then
        call WriteStatus(RorW, Ptr.History,Ptr,NumVar,NumVarTotal,
     &       VarCnt,VarNum,OF )
        read(2,rec=ptr.History,err=1010,iostat=error) History(1).array
        if ( SwapBytes ) then
          i= History(1).NumHistory
          call SwapInteger( i,History(1).NumHistory )
          i= History(1).NumLine
          call SwapInteger( i,History(1).NumLine )
        end if
        if ( History(1).NumHistory .gt. MAX_NUM_HISTORY ) then
          write(6,511) MAX_NUM_HISTORY
          if ( OF.ToLog) write(3,511) MAX_NUM_HISTORY
511       format(/' NOTE- Number of command histories truncated at',i2/)
          History(1).NumHistory= MAX_NUM_HISTORY
        end if
        do i= 2, History(1).NumHistory
          read(2,rec=ptr.History+i-1,err=1010,iostat=error) 
     &      History(i).array
          if ( SwapBytes ) then
            j= History(i).NumHistory
            call SwapInteger( j,History(i).NumHistory )
            j= History(i).NumLine     
            call SwapInteger( j,History(i).NumLine )
          end if
        end do       
      end if  
c
c     Read in the segment and joint names and connectivity data if the
c     file version is 'd' or greater, otherwise set the names/connectivity
c     to the default values
c     Added by dam 1/93 to allow non-locomotion analyses
c
      if ( (GP.FileFormatVersion .ge. 'd') .and. 
     &     (Ptr.SegJntInfo .ne. 0)               ) then
        call WriteStatus(RorW, Ptr.SegJntInfo,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        read(2,rec=ptr.SegJntInfo,err=1010,iostat=error) SgJtInfo.array
        do i= 1, GP.num_seg
          seg_names(i)= SgJtInfo.seg_names(i)
        end do
        do i= 1, GP.num_jnt
          jnt_names(i)= SgJtInfo.jnt_names(i)
          JntSegNum(1,i)= SgJtInfo.JntSegNum(1,i)
          JntSegNum(2,i)= SgJtInfo.JntSegNum(2,i)
        end do
        if ( GP.FileFormatVersion .ge. 'e' ) then
           do i= 1, GP.num_seg
              do j= 1, 4
                 SegJntNum(j,i)= SgJtInfo.SegJntNum(j,i)
              end do
           end do
        end if
      else
        call SetUpFullBodyParam
      end if
c                                      
c     Now read in each group of data using the value of the pointer in the
c     ptr structure as an indicator of whether the data is present or not.
c     If ptr is 0, then the data has not been calculated and saved in the
c     ANZ file.             
c                                   
c     marker data    
c
      if ( ptr.MRK_pos_GCS .ne. 0 ) then
        if ( MP.nfrm .gt. MAX_MTN ) then
          write(6,2005) MP.nfrm,MAX_MTN
          if ( OF.ToLog ) write(3,2005) MP.nfrm,MAX_MTN
2005      format(
     &    ' Error- There are ',i4,' frames of motion data but there is'/
     &    '        is only array space for ',i4,' frames'/
     &    '        This ANZ file was created on another version of ANZ'/
     &    '        which had larger arrays allocated.  Nothing can be'/
     &    '        done to read the motion data of this file except to'/
     &    '        run a larger version of ANZ')
          call zero_motion_flags( MF,SF,JF,AF,CF )   
          goto 333 ! continue to force data...
        end if
        call WriteStatus(RorW, Ptr.MRK_pos_GCS,Ptr,NumVar,NumVarTotal,
     &   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.MRK_pos_GCS,MrkData,3,MAX_MTN,MAX_MRK,
     &                        3,MP.nfrm,MP.num_mrk,
     &                        scale.MRK_pos_GCS,.false.,GP.jnt_num,
     &                        SwapBytes,error )
        if ( error .ne. 0 ) goto 1010                  
        MF.read=         .true.
        MF.interpolated= .true. 
        MF.adjusted=     .true.
      end if
                                                    
      if ( ptr.MpnLCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.MpnLCS,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_4dim_array( ptr.MpnLCS,MpnLCS,3,5,MAX_MTN,MAX_SEG,
     &                        3,5,MP.nfrm,GP.num_seg,
     &                        scale.MpnLCS,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.MrkDist,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.MrkDist,MrkDist,6,MAX_MTN,MAX_SEG,
     &                        6,MP.nfrm,GP.num_seg,
     &                        scale.MrkDist,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        MF.diagnostics=  .true.
      end if 

      if ( ptr.MRK_Vel .ne. 0 ) then
        call WriteStatus(RorW, Ptr.MRK_Vel,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.MRK_Vel,MrkVel,3,MAX_MTN,MAX_MRK,
     &                        3,MP.nfrm,MP.num_mrk,
     &                        scale.MRK_Vel,.false.,GP.jnt_num,
     &                        SwapBytes,error )
        if ( error .ne. 0 ) goto 1010                  
        call WriteStatus(RorW, Ptr.MRK_Acc,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.MRK_Acc,MrkAcc,3,MAX_MTN,MAX_MRK,
     &                        3,MP.nfrm,MP.num_mrk,
     &                        scale.MRK_Acc,.false.,GP.jnt_num,
     &                        SwapBytes,error )
        if ( error .ne. 0 ) goto 1010      
        MF.smoothed= .true.
        MF.VelAcc= .true.
      end if                         
c                                                      
c     Segment data                   
c
      if ( ptr.SEG_param .ne. 0 ) then
*$ifdef DEBUG
	write(3,886)
886     format(/' Reading segment parameters from anz file')
*$endif
        if ( ichar(GP.FileFormatVersion) .ge. ichar('c') ) rbody= .true.
        call WriteStatus(RorW, Ptr.SEG_param,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        pntr_num= ptr.SEG_param
        do i= 1, GP.num_seg
          seg_num= GP.seg_num(i)
*$ifdef DEBUG
          write(3,997) i,seg_num,pntr_num
 997      format(/' Index: ',i2,'  Segment number: ',i2,' Pointer: ',i4)
*$endif
          read(2,rec=pntr_num,err=1010,iostat=error) 
     &       SP_ANZ(seg_num).array
          call segment_itor( SP_ANZ(seg_num), scale.SEG_param, 
     &                       SP(seg_num), SwapBytes, rbody )
          pntr_num= pntr_num + 1                
        end do
        SF.param= .true.        
      end if
      
      if ( ptr.Sgp .ne. 0 ) then
        call WriteStatus(RorW, Ptr.Sgp,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_4dim_array( ptr.Sgp,SgpGCS,3,4,MAX_MTN,MAX_SEG,
     &                        3,4,MP.nfrm,GP.num_seg,
     &                        scale.Sgp,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.position= .true.                 
      end if                           
      
      if ( ptr.SvlGCS .ne. 0 ) then ! if this is present then all
                                         ! the segment vel and acc are present
        call WriteStatus(RorW, Ptr.SvlGCS,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SvlGCS,SvlGCS,6,MAX_MTN,MAX_SEG,
     &                        6,MP.nfrm,GP.num_seg,
     &                        scale.SvlGCS,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.SacGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SacGCS,SacGCS,6,MAX_MTN,MAX_SEG,
     &                        6,MP.nfrm,GP.num_seg,
     &                        scale.SacGCS,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010    
        call WriteStatus(RorW, Ptr.SvlLCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SvlLCS,SvlLCS,6,MAX_MTN,MAX_SEG,
     &                        6,MP.nfrm,GP.num_seg,
     &                        scale.SvlLCS,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.SacLCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SacLCS,SacLCS,6,MAX_MTN,MAX_SEG,
     &                        6,MP.nfrm,GP.num_seg,
     &                        scale.SacLCS,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.vel_acc= .true.      
      end if
      
      if ( ptr.SEG_KE .ne. 0 ) then ! if this is present then all energies
                                    ! are present
        call WriteStatus(RorW, Ptr.SEG_KE,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SEG_KE,SegKE,3,MAX_MTN,MAX_SEG,
     &                        3,MP.nfrm,GP.num_seg,
     &                        scale.SEG_KE,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.SEG_PE,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.SEG_PE,SegPE,MAX_MTN,MAX_SEG,
     &                        MP.nfrm,GP.num_seg,
     &                        scale.SEG_PE,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.SEG_TE,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.SEG_TE,SegTE,MAX_MTN,MAX_SEG,
     &                        MP.nfrm,GP.num_seg,
     &                        scale.SEG_TE,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.SEG_BodyKE,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.SEG_BodyKE,BodyKE,3,MAX_MTN,
     &                        3,MP.nfrm,scale.SEG_BodyKE,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010    
        call WriteStatus(RorW, Ptr.SEG_BodyPE,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_1dim_array( ptr.SEG_BodyPE,BodyPE,MAX_MTN,
     &                        MP.nfrm,scale.SEG_BodyPE,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010    
        call WriteStatus(RorW, Ptr.SEG_BodyTE,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_1dim_array( ptr.SEG_BodyTE,BodyTE,MAX_MTN,
     &                        MP.nfrm,scale.SEG_BodyTE,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.energy= .true.          
      end if
      
      if ( ptr.SEG_FinScrew .ne. 0 ) then
        call WriteStatus(RorW, Ptr.SEG_FinScrew,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SEG_FinScrew,SgFinScrGCS,8,MAX_MTN,MAX_SEG,
     &                        8,MP.nfrm,GP.num_seg,
     &                        scale.SEG_FinScrew,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.FiniteScrew= .true.  
      end if
      
      if ( ptr.SEG_InsScrew .ne. 0 ) then
        call WriteStatus(RorW, Ptr.SEG_InsScrew,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SEG_InsScrew,SgInsScrGCS,8,MAX_MTN,MAX_SEG,
     &                        8,MP.nfrm,GP.num_seg,
     &                        scale.SEG_InsScrew,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.InstantScrew= .true.
      end if
      
      if ( ptr.SEG_BodyCM .ne. 0 ) then
        call WriteStatus(RorW, Ptr.SEG_BodyCM,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.SEG_BodyCM,BodyCM,3,MAX_MTN,
     &                        3,MP.nfrm,scale.SEG_BodyCM,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.cntr_mass= .true.         
      end if
      
      if ( ptr.SEG_BCMVel .ne. 0 ) then
        call WriteStatus(RorW, Ptr.SEG_BCMVel,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.SEG_BCMVel,BCMVel,3,MAX_MTN,
     &                        3,MP.nfrm,scale.SEG_BCMVel,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.BCMVel= .true.         
      end if

      if ( ptr.SgMom .ne. 0 ) then
        call WriteStatus(RorW, Ptr.SgMom,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SgMom,SgMom,8,MAX_MTN,MAX_SEG,
     &                        8,MP.nfrm,GP.num_seg,
     &                        scale.SgMom,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.SEG_BdMom,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.SEG_BdMom,BdMom,8,MAX_MTN,
     &                        8,MP.nfrm,scale.SEG_BdMom,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.momentum= .true.
      end if                         
      
      if ( ptr.SEG_angle .ne. 0 ) then                             
        call WriteStatus(RorW, Ptr.SEG_angle,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SEG_angle,SangGCS,3,MAX_MTN,MAX_SEG,
     &                        3,MP.nfrm,GP.num_seg,
     &                        scale.SEG_angle,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        SF.angle= .true.
      end if
c
c     Force plate data               
c
333   if ( ptr.FRC_plate_FpData .ne. 0 ) then 
        if ( FP.nfrm .gt. MAX_ELC ) then
          write(6,2015) FP.nfrm,MAX_ELC
          if ( OF.ToLog ) write(3,2015) FP.nfrm,MAX_ELC
2015      format(' Error- There are ',i5,' frames of force plate data but there is'/
     &       '        is only array space for ',i5,' frames'/
     &       '        This ANZ file was created on another version of ANZ'/
     &       '        which had larger arrays allocated.  Nothing can be'/
     &       '        done to read the force data of this file except to'/
     &       '        run a larger version of ANZ')
          call zero_force_flags( FF,EF )
          goto 444 ! Skip to EMG section...
        end if
        call WriteStatus(RorW, Ptr.FRC_plate_FpData,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_plate_FpData,FpData,6,MAX_ELC,4,
     &                        6,FP.nfrm,FP.num_plt,
     &                        scale.FRC_plate_FpData,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.read=     .true.     
        if ( (FP.nfrm .eq. MP.nfrm) .and.
     &       (GP.FileFormatVersion .eq. 'a')  )  FF.DataClipped= .true.
      end if

      if ( ptr.FRC_FpDataOK .ne. 0 ) then ! FRC has been adjusted
        call WriteStatus(RorW, Ptr.FRC_FpDataOK,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_FpDataOK,temp5,MAX_ELC,4,
     &                        FP.nfrm,FP.num_plt,
     &                        scale.FRC_FpDataOK,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        do i= 1, FP.num_plt
          do j= 1, FP.nfrm
            FpDataOK(j,i)= .false.
            if ( abs(temp5(j,i)+1.0) .le. 1.0e-5 ) FpDataOK(j,i)= .true.
          end do
        end do
        FF.adjusted= .true.
      end if  

      if ( ptr.FRC_combined_GRL .ne. 0 ) then ! FRC has been adjusted
        call WriteStatus(RorW, Ptr.FRC_combined_GRL,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_combined_GRL,CmbGrl,6,MAX_ELC,
     &                        6,FP.nfrm,scale.FRC_combined_GRL,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.adjusted= .true.
      end if

      if ( ptr.FRC_FcDataOK .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_FcDataOK,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_1dim_array( ptr.FRC_FcDataOK,temp6,MAX_ELC,
     &                        FP.nfrm,scale.FRC_FcDataOK,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        do j= 1, FP.nfrm
          FcDataOK(j)= .false.
          if ( abs(temp6(j)+1.0) .le. 1.0e-5 ) FcDataOK(j)= .true.
        end do
      end if

      if ( ptr.FRC_FlrCntct .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_FlrCntct,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_FlrCntct,temp4,MAX_MTN,2,
     &                        MP.nfrm,2,
     &                        scale.FRC_FlrCntct,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        do i= 1, 2
          do j= 1, MP.nfrm   
            FlrCntct(j,i)= .false.
            if ( abs(temp4(j,i)+1.0) .lt. 0.0001 ) FlrCntct(j,i)= .true.
          end do ! j
        end do ! i
      end if
                                      
      if ( ptr.FtGrlOK .ne. 0 ) then ! this plus single and combined
                                         ! ground reaction loads will all
                                         ! be present
        call WriteStatus(RorW, Ptr.FtGrlOK,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FtGrlOK,temp3,MAX_ELC,2,
     &                        FP.nfrm,2,scale.FtGrlOK,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        do i= 1, 2              
          do j= 1, FP.nfrm
            FtGrlOK(j,i)= .false.
            if ( abs(temp3(j,i)+1.0) .lt. 0.0001 ) FtGrlOK(j,i)= .true.
          end do ! j
        end do ! i

        call WriteStatus(RorW, Ptr.FRC_single_GRL,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_single_GRL,FtGrl,6,MAX_ELC,2,
     &                        6,FP.nfrm,2,scale.FRC_single_GRL,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.read=     .true.
        FF.adjusted= .true.
        FF.aligned=  .true.
        FF.individual_feet= .true.
      end if
      
      if ( ptr.FRC_single_CP .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_single_CP,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_single_CP,FtCpGCS,3,MAX_ELC,2,
     &                        3,FP.nfrm,2,scale.FRC_single_CP,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.cpr_GCS= .true.            
      end if

      if ( ptr.FRC_combined_CP .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_combined_CP,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_combined_CP,CmbCp,3,MAX_ELC,
     &                        3,FP.nfrm,scale.FRC_combined_CP,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010                                     
      end if
      
      if ( ptr.FRC_single_CP_LCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_single_CP_LCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_single_CP_LCS,FtCpLCS,3,MAX_MTN,2,
     &                        3,FP.nfrm,2,scale.FRC_single_CP_LCS,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.cpr_LCS= .true.              
      end if
      
      if ( ptr.FRC_StrIdx .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_StrIdx,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_StrIdx,StrIdx,MAX_MTN,2,
     &                        FP.nfrm,2,scale.FRC_StrIdx,
     &                        .false.,GP.seg_num,SwapBytes,error)
        if ( error .ne. 0 ) goto 1010
        FF.StrIdx= .true. 
      end if              

      if ( ptr.FRC_FtGRLLCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_FtGRLLCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_FtGRLLCS,FtGRLLCS,6,MAX_MTN,2,
     &                        6,MP.nfrm,2,scale.FRC_FtGRLLCS,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
      end if
      
      if ( ptr.FRC_single_wrench .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_single_wrench,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_single_wrench,FtGRLWre,8,MAX_ELC,2,
     &                        8,FP.nfrm,2,scale.FRC_single_wrench,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        do i= 1, 2    ! special scale problems
          do j= 1, FP.nfrm
            FtGRLWre(7,j,i)= FtGRLWre(7,j,i)*10000.0
          end do
        end do
      end if                                   

      if ( ptr.FRC_combined_wrench .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_combined_wrench,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_combined_wrench,CmbGrlWre,8,MAX_ELC,
     &                        8,FP.nfrm,scale.FRC_combined_wrench,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        do j= 1, FP.nfrm  ! special scale problems
          CmbGrlWre(7,j)= CmbGrlWre(7,j)*10000.0
        end do
        FF.wrench= .true.           
      end if
      
      if ( ptr.FRC_FtIntegral .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_FtIntegral,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_FtIntegral,FtIntegral,4,MAX_ELC,2,
     &                        4,FP.nfrm,2,scale.FRC_FtIntegral,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
      end if

      if ( ptr.FRC_FtIntegral2 .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_FtIntegral2,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_FtIntegral2,FtIntegral2,4,MAX_ELC,2,
     &                        4,FP.nfrm,2,scale.FRC_FtIntegral2,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
      end if

      if ( ptr.FRC_CombIntegral .ne. 0 ) then      
        call WriteStatus(RorW, Ptr.FRC_CombIntegral,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_CombIntegral,CombIntegral,4,MAX_ELC,
     &                        4,FP.nfrm,scale.FRC_CombIntegral,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.Integral= .true.           
      end if      

      if ( ptr.FRC_CombIntegral2 .ne. 0 ) then      
        call WriteStatus(RorW, Ptr.FRC_CombIntegral2,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_CombIntegral2,CombIntegral2,4,MAX_ELC,
     &                        4,FP.nfrm,scale.FRC_CombIntegral2,
     &                        .false.,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.Integral= .true.           
      end if      

      if ( ptr.FRC_plate_CP .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_plate_CP,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_plate_CP,FpCp,3,MAX_ELC,4,
     &                        3,FP.nfrm,FP.num_plt,
     &                        scale.FRC_plate_CP,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
c        FF.force_only= .true.  
        FF.cpr_GCS=    .true.
      end if
      
      if ( ptr.FRC_plate_wrench .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_plate_wrench,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_plate_wrench,FpWre,8,MAX_ELC,4,
     &                        8,FP.nfrm,FP.num_plt,
     &                        scale.FRC_plate_wrench,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.wrench=     .true.        
        do i= 1, FP.num_plt    ! special scale problems
          do j= 1, FP.nfrm
            FpWre(7,j,i)= FpWre(7,j,i)*10000.0
          end do
        end do
      end if
      
      if ( ptr.FRC_PltIntegral .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_PltIntegral,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_PltIntegral,PltIntegral,4,MAX_ELC,4,
     &                        4,FP.nfrm,FP.num_plt,
     &                        scale.FRC_PltIntegral,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.Integral=    .true.        
      end if                      

      if ( ptr.FRC_PltIntegral2 .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_PltIntegral2,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_PltIntegral2,PltIntegral2,4,MAX_ELC,4,
     &                        4,FP.nfrm,FP.num_plt,
     &                        scale.FRC_PltIntegral2,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.Integral=    .true.        
      end if                      

      if ( ptr.FRC_PMAPnt .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_PMAPnt,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_PMAPnt,PmaPnt,3,MAX_MTN,
     &                        3,FP.nfrm,
     &                        scale.FRC_PMAPnt,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.FRC_PMATrk,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_PMATrk,PmaTrkLCS,3,MAX_MTN,
     &                        3,FP.nfrm,
     &                        scale.FRC_PMATrk,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.FRC_PMAPlv,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_PMAPlv,PmaPlvLCS,3,MAX_MTN,
     &                        3,FP.nfrm,
     &                        scale.FRC_PMAPlv,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.FRC_PMAGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_PMAGCS,PmaGCS,4,MAX_MTN,
     &                        4,FP.nfrm,
     &                        scale.FRC_PMAGCS,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.PitchMomArm=    .true.        
      end if                                  

      if ( ptr.FRC_PWAPnt .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_PWAPnt,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_PWAPnt,PwaPnt,3,MAX_MTN,
     &                        3,FP.nfrm,
     &                        scale.FRC_PWAPnt,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.FRC_PWATrk,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_PWATrk,PwaTrkLCS,3,MAX_MTN,
     &                        3,FP.nfrm,   
     &                        scale.FRC_PWATrk,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.FRC_PWAPlv,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_PWAPlv,PwaPlvLCS,3,MAX_MTN,
     &                        3,FP.nfrm,   
     &                        scale.FRC_PWAPlv,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.FRC_PWAGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.FRC_PWAGCS,PwaGCS,4,MAX_MTN,
     &                        4,FP.nfrm,   
     &                        scale.FRC_PWAGCS,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.PitchWreArm=    .true.        
      end if                                       

      if ( ptr.FRC_FtCprVelGCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.FRC_FtCprVelGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.FRC_FtCprVelGCS,FtCprVelGCS,3,MAX_MTN,2,
     &                        3,MP.nfrm,2,
     &                        scale.FRC_FtCprVelGCS,.false.,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.CprVelGCS=    .true.        
      end if                      
c                                             
c     Joint data                               
c
      if ( MP.nfrm .gt. MAX_MTN ) goto 444 ! skip to EMG section
      if ( ptr.JangJCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JangJCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JangJCS,JangJCS,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JangJCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JaxesJCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_4dim_array( ptr.JaxesJCS,JaxesJCS,3,3,MAX_MTN,MAX_JNT,
     &                        3,3,MP.nfrm,GP.num_jnt,
     &                        scale.JaxesJCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010      
        JF.angle_jcs= .true.
      end if
      
      if ( ptr.JvelJCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JvelJCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JvelJCS,JvelJCS,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JvelJCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JaccJCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JaccJCS,JaccJCS,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JaccJCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.vel_acc_jcs= .true.
      end if
      
      if ( ptr.JangEUL .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JangEUL,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JangEUL,JangEUL,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JangEUL,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JaxesEUL,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_4dim_array( ptr.JaxesEUL,JaxesEUL,3,3,MAX_MTN,MAX_JNT,
     &                        3,3,MP.nfrm,GP.num_jnt,
     &                        scale.JaxesEUL,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010      
        JF.angle_EUL= .true.
      end if
      
      if ( ptr.JvelEUL .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JvelEUL,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JvelEUL,JvelEUL,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JvelEUL,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010     
        call WriteStatus(RorW, Ptr.JaccEUL,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JaccEUL,JaccEUL,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JaccEUL,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010                    
        JF.vel_acc_EUL= .true.
      end if
                                
      if ( ptr.JrlGCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JntCalcOK,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.JntCalcOK,temp2,MAX_MTN,MAX_JNT,
     &                        MP.nfrm,GP.num_jnt,
     &                        scale.JntCalcOK,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        do i= 1, MP.nfrm         
          do j= 1, MAX_JNT
            JntCalcOK(i,j)= .false.
            if ( abs(temp2(i,j)+1.0) .lt. 0.0001 ) JntCalcOK(i,j)= .true.
          end do ! j
        end do ! i                             
        call WriteStatus(RorW, Ptr.JNT_calc_type,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_1dim_array( ptr.JNT_calc_type,temp,MAX_MTN,
     &                        MP.nfrm,scale.JNT_calc_type,
     &                        .false.,GP.jnt_num,SwapBytes,error)
        if ( error .ne. 0 ) goto 1010
        do i= 1, MP.nfrm     
          JrlCalcType(i)= temp(i)
        end do ! i                             
        
        call WriteStatus(RorW, Ptr.JrlGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JrlGCS,JrlGCS,6,MAX_MTN,MAX_JNT,
     &                        6,MP.nfrm,GP.num_jnt,
     &                        scale.JrlGCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JrlJCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JrlJCS,JrlJCS,8,MAX_MTN,MAX_JNT,
     &                        8,MP.nfrm,GP.num_jnt,
     &                        scale.JrlJCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JrlDCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JrlDCS,JrlDCS,6,MAX_MTN,MAX_JNT,
     &                        6,MP.nfrm,GP.num_jnt,
     &                        scale.JrlDCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JrlPCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JrlPCS,JrlPCS,6,MAX_MTN,MAX_JNT,
     &                        6,MP.nfrm,GP.num_jnt,
     &                        scale.JrlPCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.load= .true.        
      end if                                   
      
      if ( ptr.JNT_LoadLegJCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_LoadLegJCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_LoadLegJCS,JRLegJCS,6,MAX_MTN,2,
     &                        6,MP.nfrm,2,
     &                        scale.JNT_LoadLegJCS,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_LoadLegGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_LoadLegGCS,JRLegGCS,6,MAX_MTN,2,
     &                        6,MP.nfrm,2,
     &                        scale.JNT_LoadLegGCS,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010        
        JF.legsummed= .true.
      end if
      
      if ( ptr.JwreLCS .ne. 0 ) then  
        call WriteStatus(RorW, Ptr.JwreLCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JwreLCS,JwreLCS,8,MAX_MTN,MAX_JNT,
     &                        8,MP.nfrm,GP.num_jnt,
     &                        scale.JwreLCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.wrench= .true.              
      end if
                    
      if ( ptr.JNT_Power .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_Power,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_Power,JntPower,4,MAX_MTN,MAX_JNT,
     &                        4,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_Power,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.power= .true.        
      end if                         
                                          
      if ( ptr.JNT_PowerLeg .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_PowerLeg,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_PowerLeg,JntPowerLeg,4,MAX_MTN,2,
     &                        4,MP.nfrm,2,
     &                        scale.JNT_PowerLeg,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.legsummed= .true.        
      end if
            
      if ( ptr.JNT_FinScrew .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_FinScrew,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_FinScrew,JFinScr,8,MAX_MTN,MAX_JNT,
     &                        8,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_FinScrew,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.FiniteScrew= .true.       
      end if
      
      if ( ptr.JNT_InsScrew .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_InsScrew,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_InsScrew,JInsScr,8,MAX_MTN,MAX_JNT,
     &                        8,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_InsScrew,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.InstantScrew= .true. 
      end if

      if ( ptr.JNT_TransGCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_TransGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_TransGCS,JtrnGCS,4,MAX_MTN,MAX_JNT,
     &                        4,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_TransGCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_TransJCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_TransJCS,JtrnJCS,4,MAX_MTN,MAX_JNT,
     &                        4,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_TransJCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_TransPCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_TransPCS,JtrnPCS,4,MAX_MTN,MAX_JNT,
     &                        4,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_TransPCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_TransDCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_TransDCS,JtrnDCS,4,MAX_MTN,MAX_JNT,
     &                        4,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_TransDCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.Translation= .true. 
      end if

      if ( ptr.JNT_FscAngJCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_FscAngJCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_FscAngJCS,JfsAngJCS,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_FscAngJCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_FscAngPCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_FscAngPCS,JfsAngPCS,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_FscAngPCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_FscAngDCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_FscAngDCS,JfsAngDCS,3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_FscAngDCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.ScrewAngle= .true. 
      end if                                      

      if ( ptr.JNT_MomArmPCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_MomArmPCS,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_MomArmPCS,JmaPCS,
     &                        3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_MomArmPCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_MomArmDCS,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_MomArmDCS,JmaDCS,
     &                        3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_MomArmDCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_MomArmGCS,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_MomArmGCS,JmaGCS,
     &                        4,MAX_MTN,MAX_JNT,
     &                        4,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_MomArmGCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.MomentArm= .true. 
      end if

      if ( ptr.JNT_WreArmPCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_WreArmPCS,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_WreArmPCS,JwaPCS,
     &                        3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_WreArmPCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_WreArmDCS,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_WreArmDCS,JwaDCS,
     &                        3,MAX_MTN,MAX_JNT,
     &                        3,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_WreArmDCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.JNT_WreArmGCS,Ptr,NumVar,NumVarTotal,
     &                   VarCnt,VarNum,OF )
        call read_3dim_array( ptr.JNT_WreArmGCS,JwaGCS,
     &                        4,MAX_MTN,MAX_JNT,
     &                        4,MP.nfrm,GP.num_jnt,
     &                        scale.JNT_WreArmGCS,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.WrenchArm= .true. 
      end if
c
c     average position data
c       
      if ( ptr.SgpAvg .ne. 0 ) then
        call WriteStatus(RorW, Ptr.SgpAvg,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.SgpAvg,SgpAvg,3,4,MAX_SEG,
     &                        3,4,GP.num_seg,scale.SgpAvg,
     &                        UnCompactSegData,GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        AF.read= .true.
        AF.position= .true.
      end if                              
      if ( ptr.AVG_angle_JCS .ne. 0 ) then
        call WriteStatus(RorW, Ptr.AVG_angle_JCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.AVG_angle_JCS,JangAvgJCS,3,MAX_JNT,
     &                        3,GP.num_seg,scale.AVG_angle_JCS,
     &                        UnCompactJntData,GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        AF.angle_JCS= .true.
      end if
      if ( ptr.AVG_angle_EUL .ne. 0 ) then
        call WriteStatus(RorW, Ptr.AVG_angle_EUL,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.AVG_angle_EUL,JangAvgEUL,3,MAX_JNT,
     &                        3,GP.num_seg,scale.AVG_angle_EUL,
     &                        UnCompactJntData,GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        AF.angle_EUL= .true.
      end if
c
c     Read reconstructed ground reaction force
c
      if ( ptr.REC_IndFtLoadLCS .ne. 0 ) then ! All reconstructed stuff is present
        call WriteStatus(RorW, Ptr.REC_IndFtLoadLCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.REC_IndFtLoadLCS,rFtGrlLCS,6,MAX_MTN,2,
     &                        6,MP.nfrm,2,
     &                        scale.REC_IndFtLoadLCS,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.REC_IndFtLoadGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.REC_IndFtLoadGCS,rFtGrlGCS,6,MAX_MTN,2,
     &                        6,MP.nfrm,2,
     &                        scale.REC_IndFtLoadGCS,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.REC_IndFtCpLCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.REC_IndFtCpLCS,rFtCpLCS,3,MAX_MTN,2,
     &                        3,MP.nfrm,2,
     &                        scale.REC_IndFtCpLCS,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.REC_IndFtCpGCS,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_3dim_array( ptr.REC_IndFtCpGCS,rFtCpGCS,3,MAX_MTN,2,
     &                        3,MP.nfrm,2,
     &                        scale.REC_IndFtCpGCS,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.REC_CombFtLoad,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.REC_CombFtLoad,rCmbGrl,6,MAX_MTN,
     &                        6,MP.nfrm,
     &                        scale.REC_CombFtLoad,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        call WriteStatus(RorW, Ptr.REC_CombFtCp,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.REC_CombFtCp,rCmbCp,6,MAX_MTN,
     &                        6,MP.nfrm,
     &                        scale.REC_CombFtCp,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010       
        call WriteStatus(RorW, Ptr.REC_StrikeIndex,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.REC_StrikeIndex,rStrIdx,MAX_MTN,2,
     &                        MP.nfrm,2,
     &                        scale.REC_StrikeIndex,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        FF.reconstructed= .true.
      end if ! reconstructed GRL present    
c
c     Read EMG data if present
c
444   if ( ptr.PRM_EMG .ne. 0 ) then
        if ( EP.NumSamples .gt. MAX_EMF ) then
          write(6,2025) EP.NumSamples,MAX_EMF
          if ( OF.ToLog ) write(3,2025) EP.NumSamples,MAX_EMF
2025      format(' Error- There are ',i5,' frames of EMG data but there is'/
     &       '        is only array space for ',i5,' frames'/
     &       '        This ANZ file was created on another version of ANZ'/
     &       '        which had larger arrays allocated.  Nothing can be'/
     &       '        done to read the EMG data of this file except to'/
     &       '        run a larger version of ANZ')
          EF.read= .false.         
          goto 555 ! goto last section...
        end if
        call WriteStatus(RorW, Ptr.EMG_Data,Ptr,NumVar,NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.EMG_Data,EMGData,MAX_EMF,MAX_EMG,
     &                        EP.NumSamples,EP.NumChnl,
     &                        scale.EMG_Data,.false.,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        EF.read= .true.         
      end if
c
c     Segment position and Joint angle correction transformations
c
555   if ( ptr.AVG_SgpRefCorAng .ne. 0 ) then
        call WriteStatus(RorW, Ptr.AVG_SgpRefCorAng,Ptr,NumVar,
     &                   NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.AVG_SgpRefCorAng,SgpRefCorAng,
     &                        3,MAX_SEG,3,GP.num_seg,
     &                        scale.AVG_SgpRefCorAng,UnCompactSegData,
     &                        GP.seg_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        AF.RefCorrectAng= .true.         
      end if
      
      if ( ptr.JNT_JntAngRefCorAng .ne. 0 ) then
        call WriteStatus(RorW, Ptr.JNT_JntAngRefCorAng,Ptr,NumVar,
     &                   NumVarTotal,VarCnt,VarNum,OF )
        call read_2dim_array( ptr.JNT_JntAngRefCorAng,JntAngRefCorAng,
     &                        3,MAX_JNT,3,GP.num_jnt,
     &                        scale.JNT_JntAngRefCorAng,UnCompactJntData,
     &                        GP.jnt_num,SwapBytes,error )
        if ( error .ne. 0 ) goto 1010
        JF.RefCorrectAng= .true.         
      end if
      
      close(unit=2)              
      data_read= .true.
      return                            
                     
1000  write(6,1005) filename,error
      if ( OF.ToLog ) write(3,1005) filename,error
1005  format(' Error- Trouble opening ',a40/
     &       '        Error number ',i2,' occurred.'/)
      return      
      
1010  write(6,1015) error,filename
      if ( OF.ToLog ) write(3,1015) error,filename
1015  format(' Error- Error # ',i4,' occured while reading ',a40)
      close(unit=2)
      return      
      end
c
c
c                                   
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Display a running list of what is being read in currently. Print out
c              the name as well as the number of the quantity relative to the total
c              number of quantities to be read in.
c
      subroutine WriteStatus( RorW,Address,Ptr,NumVar,NumVarTotal,VarCnt,
     &                        VarNum,OF )
      implicit none
      include 'a_const.inc'

      include 'a_struct.inc'  
      include 'a_file.inc'
      include 'a_names.inc'

      record / anz_layout /  Ptr
      record / option_flag / OF 
                    
      integer*4 Address,NumVar,NumVarTotal,VarCnt,i,RorW,VarNum(128)
                 
      if ( .not. OF.UpDate ) return
      if ( RorW .eq. 0 ) then ! reading file...
        i= 1
        do while ( (Ptr.array(i) .ne. Address) .and. (i .le. NumVarTotal) )
          i= i + 1
        end do
        if ( i .le. NumVarTotal ) then
          VarCnt= VarCnt + 1
          write(6,500) VarCnt,NumVar,AnzVarName(i)
          if ( OF.ToLog ) write(3,500) VarCnt,NumVar,AnzVarName(i)
500       format('+ Reading data item ',i3,'/',i3,' : ',a20)
        end if   
      else
        VarCnt= VarCnt + 1
        write(6,510) VarCnt,NumVar,AnzVarName(VarNum(VarCnt))
        if ( OF.ToLog ) write(3,510) VarCnt,NumVar,
     &                                       AnzVarName(VarNum(VarCnt))
510     format('+ Writing data item ',i3,'/',i3,' : ',a20)
      end if
      return     
      end                           
c                                                
c
c        
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Convert the GP structure from a special integer version that
c              is stored in the ANZ file to the normal version which contains a
c              mixture of integer and real variables
c
      subroutine general_itor( GP_ANZ, scale_factor, GP, SwapBytes )
      implicit none
      include 'a_const.inc'

      include 'a_struct.inc'
      include 'a_file.inc'

      record /general_param/     GP
      record /general_param_INT/ GP_ANZ
      integer*4 scale_factor
      logical*4 SwapBytes

      integer*4 i,j,IntTemp

      GP.MtnFile=  GP_ANZ.MtnFile
      GP.AvgFile= GP_ANZ.AvgFile
      GP.FrcFile=   GP_ANZ.FrcFile
      GP.last_updated= GP_ANZ.last_updated
      GP.name=         GP_ANZ.name
      GP.comment=      GP_ANZ.comment
      if ( SwapBytes ) then
        call SwapInteger( GP_ANZ.Age,GP.Age )
        call SwapInteger( GP_ANZ.weight,IntTemp )
        GP.weight= IntTemp/(10.0**scale_factor)
        call SwapInteger( GP_ANZ.height,IntTemp )
        GP.height= IntTemp/(10.0**scale_factor)
        call SwapInteger( GP_ANZ.num_seg,GP.num_seg )
        do i= 1, GP.num_seg
          call SwapInteger( GP_ANZ.seg_num(i),GP.seg_num(i) )
        end do
        call SwapInteger( GP_ANZ.num_jnt,GP.num_jnt )
        do i= 1, GP.num_jnt
          call SwapInteger( GP_ANZ.jnt_num(i),GP.jnt_num(i) )
        end do
        call SwapLogical( GP_ANZ.single_cycle,GP.single_cycle )
        call SwapLogical( GP_ANZ.normalized,GP.normalized )
        call SwapLogical( GP_ANZ.seg_unrotated,GP.seg_unrotated )
        call SwapLogical( GP_ANZ.seg_smooth,GP.seg_smooth )
        call SwapInteger( GP_ANZ.int_poly_order,GP.int_poly_order )
        call SwapInteger( GP_ANZ.analysis_type,GP.analysis_type )
        call SwapInteger( GP_ANZ.ByteOrderCheck,GP.ByteOrderCheck )
      else
        GP.age=     GP_ANZ.age
        GP.weight=  GP_ANZ.weight/(10.0**scale_factor)
        GP.height=  GP_ANZ.height/(10.0**scale_factor)
        GP.num_seg= GP_ANZ.num_seg
        do i= 1, GP.num_seg
          GP.seg_num(i)= GP_ANZ.seg_num(i)
        end do
        GP.num_jnt= GP_ANZ.num_jnt
        do i= 1, GP.num_jnt
          GP.jnt_num(i)= GP_ANZ.jnt_num(i)
        end do
        GP.single_cycle=   GP_ANZ.single_cycle
        GP.normalized=     GP_ANZ.normalized
        GP.seg_unrotated=  GP_ANZ.seg_unrotated
        GP.seg_smooth=     GP_ANZ.seg_smooth
        GP.int_poly_order= GP_ANZ.int_poly_order
        GP.analysis_type=  GP_ANZ.analysis_type
        GP.ByteOrderCheck= GP_ANZ.ByteOrderCheck
      end if
      GP.Sex= GP_ANZ.Sex
      GP.FileFormatVersion= GP_ANZ.FileFormatVersion
      return
      end
c
c                 
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Convert the FP structure from a special integer version that
c              is stored in the ANZ file to the normal version which contains a
c              mixture of integer and real variables
c
      subroutine force_itor( FP_ANZ, scale_factor, FP, SwapBytes )
      implicit none
      include 'a_const.inc'                                  

      include 'a_struct.inc'
      include 'a_file.inc'

      record /force_param/     FP
      record /force_param_INT/ FP_ANZ
      integer*4 scale_factor
      logical*4 SwapBytes

      integer*4 i,j,k
      
      if ( SwapBytes ) call SwapIntegerArray( 128,FP_ANZ.array )

      FP.nfrm=      FP_ANZ.nfrm
      FP.frm_rate=     FP_ANZ.frm_rate
      FP.av_frm_ratio= (FP_ANZ.av_frm_ratio)/(10.0**scale_factor)
      FP.frm_offset=   FP_ANZ.frm_offset
      FP.first_frm=    FP_ANZ.first_frm
      FP.last_frm=     FP_ANZ.last_frm
      FP.num_plt=      FP_ANZ.num_plt
      do i= 1, 2
        do j= 1, 4
          FP.plt_size(i,j)= FP_ANZ.plt_size(i,j)/(10.0**scale_factor)
        end do
      end do
      do i= 1, 4
        do j= 1, 3
          do k= 1, 4
            FP.plt_loc(i).LCS(j,k)= 
     &                    FP_ANZ.plt_loc(i).LCS(j,k)/(10.0**scale_factor)
          end do
        end do
      end do
      FP.num_rtft_plt= FP_ANZ.num_rtft_plt
      FP.num_lfft_plt= FP_ANZ.num_lfft_plt
      do i= 1, 4
        FP.rtft_plt(i)= FP_ANZ.rtft_plt(i)
        FP.lfft_plt(i)= FP_ANZ.lfft_plt(i)
      end do
      FP.dbl_stance_OK= FP_ANZ.dbl_stance_OK
      FP.FirstFoot=     FP_ANZ.FirstFoot
      FP.Direction=     FP_ANZ.Direction
      return
      end   
c                                                                             
c           
c        
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Convert the SP structure from a special integer version that
c              is stored in the ANZ file to the normal version which contains a
c              mixture of integer and real variables
c
      subroutine segment_itor( SP_ANZ, scale_factor, SP,
     &                         SwapBytes,RigidBody )
      implicit none
      include 'a_const.inc'                                  

      include 'a_struct.inc'
      include 'a_file.inc'

      record /seg_param/     SP
      record /seg_param_INT/ SP_ANZ
      integer*4 scale_factor
      logical*4 SwapBytes,RigidBody
                   
      integer*4 i,j
      real*4    scale
      
      if ( SwapBytes ) call SwapIntegerArray( 128,SP_ANZ.array )
                 
*$ifdef DEBUG
      
      write(3,100) scale_factor,SP_ANZ.length
 100  format(' Scale : 10^',i2/' Length: ',i12)

      write(3,110) SP_ANZ.mass,SP_ANZ.avg_cntr(1),
     &             SP_ANZ.avg_cntr(2),
     &             SP_ANZ.avg_cntr(3),SP_ANZ.num_end
 110  format(' Mass  : ',i12/' Center: ',3(i12,1x)/,' # Ends: ',i12)

      write(3,120) SP_ANZ.inertia(1),SP_ANZ.inertia(2),
     &             SP_ANZ.inertia(3)
 120  format(' Inertia: ',3(i12,1x))

      write(3,130)
 130  format(' Shape:')
      do j= 1, 8
         write(3,140) SP_ANZ.shape(1,j),SP_ANZ.shape(2,j),
     &                SP_ANZ.shape(3,j)
 140     format(3(i12,1x))
      end do

      write(3,150)
 150  format(' Ends:')
      do j= 1, 4
         write(3,140) SP_ANZ.end(1,j),SP_ANZ.end(2,j),
     &                SP_ANZ.end(3,j)
      end do    

      write(3,160) SP_ANZ.num_mrk
 160  format(' # Mrk : ',i12)

      do i= 1, 5
         write(3,170) SP_ANZ.mrk_num(i)
 170     format(1x,i12)
      end do                                                               

      write(3,180) SP_ANZ.num_mrkdist
 180  format(' # MrkD: ',i12)
 
      do i= 1, 5
         write(3,190) SP_ANZ.dist_mrk_num(1,i),
     &                SP_ANZ.dist_mrk_num(2,i)
 190     format(1x,i12,1x,i12)
      end do                                                               

      if ( RigidBody ) then

         write(3,200)
 200     format(' Rigid Body parameters'/' TCStoLCS'/)
         do i= 1, 4
            write(3,140) SP_ANZ.TCStoLCS(1,i),
     &                   SP_ANZ.TCStoLCS(2,i),       
     &                   SP_ANZ.TCStoLCS(3,i)
         end do

         write(3,210) SP_ANZ.nmTCS
 210     format('# Mrk : ',i12)
         SP_ANZ.nmTCS= SP_ANZ.nmTCS
         do i= 1, 5
            write(3,220) SP_ANZ.mnTCS(i),
     &                   SP_ANZ.mpnLCS(1,i),
     &                   SP_ANZ.mpnLCS(2,i),
     &                   SP_ANZ.mpnLCS(3,i)
 220        format(1x,i12,3(i12,1x))
         end do
      end if
*$endif

      scale= 10.0**scale_factor
      SP.length= float(SP_ANZ.length)/scale
      do i= 1, 3
        SP.avg_cntr(i)= float(SP_ANZ.avg_cntr(i))/scale
      end do
      SP.mass=   float(SP_ANZ.mass)/scale
      SP.num_end= SP_ANZ.num_end
      do i= 1, 3
        SP.inertia(i)= float(SP_ANZ.inertia(i))/scale
c        SP.inertia(i)= SP.inertia(i)*1.0e-5  !  <- cheating
        do j= 1, 8
          SP.shape(i,j)= float(SP_ANZ.shape(i,j))/scale
        end do
        do j= 1, 4
          SP.end(i,j)= float(SP_ANZ.end(i,j))/scale
        end do    
      end do
      SP.num_mrk= SP_ANZ.num_mrk
      if ( SP.num_mrk .gt. 5 )  SP.num_mrk= 0 ! strange error trap
      do i= 1, SP.num_mrk
        SP.mrk_num(i)= SP_ANZ.mrk_num(i)
      end do                                                               
      SP.num_MrkDist= SP_ANZ.num_MrkDist
      if ( SP.num_MrkDist .gt. 10 )  SP.num_MrkDist= 0 ! error trap
      do i= 1, SP.num_MrkDist
        do j= 1,2
          SP.dist_mrk_num(j,i)= SP_ANZ.dist_mrk_num(j,i)         
        end do
      end do
      if ( RigidBody ) then
        do i= 1, 4
          do j= 1, 3
            SP.TCStoLCS(j,i)= SP_ANZ.TCStoLCS(j,i)/scale
          end do
        end do
        SP.nmTCS= SP_ANZ.nmTCS
        do i= 1, 5
          do j= 1, 3
            SP.mpnLCS(j,i)= SP_ANZ.mpnLCS(j,i)/scale
          end do
          SP.mnTCS(i)= SP_ANZ.mnTCS(i)
        end do
      end if
      return     
      end            
c                 
c
c                                                                   
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Convert the CP structure from a special integer version that
c              is stored in the ANZ file to the normal version which contains a
c              mixture of integer and real variables
c
      subroutine cycle_itor( CP_ANZ, scale_factor, CP, SwapBytes )
      implicit none
      include 'a_const.inc'                                  
            
      include 'a_struct.inc'
      include 'a_file.inc'

      record /cycle/     CP
      record /cycle_INT/ CP_ANZ
      integer*4 scale_factor,GaitEvent(MAX_MTN)
      logical*4 SwapBytes

      integer*4 i
      real*4    Scale

      if ( SwapBytes ) call SwapIntegerArray( 128,CP_ANZ.array )

      Scale= 10.0**scale_factor
      CP.first_side=          CP_ANZ.first_side
      CP.stride_length=       CP_ANZ.stride_length/Scale
      CP.NormStrideLength=    CP_ANZ.NormStrideLength/Scale
      CP.PctNormStrideLength= CP_ANZ.PctNormStrideLength/Scale
      CP.step_width=          CP_ANZ.step_width/Scale
      CP.NormStepWidth=       CP_ANZ.NormStepWidth/Scale
      CP.PctNormStepWidth=    CP_ANZ.PctNormStepWidth/Scale
      CP.cycle_time=          CP_ANZ.cycle_time/Scale
      CP.NormCycleTime=       CP_ANZ.NormCycleTime/Scale
      CP.PctNormCycleTime=    CP_ANZ.PctNormCycleTime/Scale
      CP.cadence=             CP_ANZ.cadence/Scale
      CP.NormCadence=         CP_ANZ.NormCadence/Scale
      CP.PctNormCadence=      CP_ANZ.PctNormCadence/Scale
      CP.velocity=            CP_ANZ.velocity/Scale
      CP.NormVelocity=        CP_ANZ.NormVelocity/Scale
      CP.PctNormVelocity=     CP_ANZ.PctNormVelocity/Scale
      do i= 1, 2      
        CP.step_length(i)=        CP_ANZ.step_length(i)/Scale
        CP.NormStepLength(i)=     CP_ANZ.NormStepLength(i)/Scale
        CP.PctNormStepLength(i)=  CP_ANZ.PctNormStepLength(i)/Scale
        CP.sgl_time(i)=           CP_ANZ.sgl_time(i)/Scale
        CP.sgl_percent(i)=        CP_ANZ.sgl_percent(i)/Scale
        CP.NormSglPct(i)=         CP_ANZ.NormSglPct(i)/Scale
        CP.dbl_time(i)=           CP_ANZ.dbl_time(i)/Scale
        CP.dbl_percent(i)=        CP_ANZ.dbl_percent(i)/Scale
        CP.NormDblPct(i)=         CP_ANZ.NormDblPct(i)/Scale
        CP.total_time(i)=         CP_ANZ.total_time(i)/Scale
        CP.total_percent(i)=      CP_ANZ.total_percent(i)/Scale
        CP.NormTotalPct(i)=       CP_ANZ.NormTotalPct(i)/Scale
        CP.to_percent(i)=         CP_ANZ.to_percent(i)/Scale
        CP.NormToPct(i)=          CP_ANZ.NormToPct(i)/Scale
      end do                                                  
      do i= 1, 3                                                        
        CP.hs_percent(i)= CP_ANZ.hs_percent(i)/Scale
        CP.NormHsPct(i)=  CP_ANZ.NormHsPct(i)/Scale
      end do
      return          
      end   
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Swap the byte order of an array of integers.
c
      subroutine SwapIntegerArray( Size,Array )
      implicit none
      
      integer*4 Size,Array(1),i,IntIn
      
      do i= 1, Size
        IntIn= Array(i)
        call SwapInteger( IntIn,Array(i) )
c        write(3,100) i,IntIn,Array(i)
c 100    format(1x,i3,1x,i12,2x,i12)
      end do
c      write(3,110)
c 110  format(//)
      return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Swap the byte order of an array of logicals.
c
      subroutine SwapLogicalArray( Size,Array )
      implicit none
      
      integer*4 Size,i
      logical*4 Array(1),LogIn
      
      do i= 1, Size
        LogIn= Array(i)
        call SwapLogical( LogIn,Array(i) )
      end do
      return
      end
c
c
c                     
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Swap the byte order of an integer.
c
      subroutine SwapInteger( IntIn,IntOut )
      implicit none
      
      integer*4 IntIn,IntOut,i,IntInTemp,IntOutTemp
      character CharIn*4,CharOut*4
      
      equivalence ( IntInTemp,CharIn )
      equivalence ( IntOutTemp,CharOut )
      
      IntInTemp= IntIn
c
c      sgi to intel byte order
c      do i= 1, 4
c        CharOut(i:i)= CharIn(5-i:5-i)
c      end do
c
c     dec to intel byte order
c
      CharOut(1:1)= CharIn(3:3)
      CharOut(2:2)= CharIn(4:4)
      CharOut(3:3)= CharIn(1:1)
      CharOut(4:4)= CharIn(2:2)
      IntOut= IntOutTemp
      return
      end                
c
c
c                     
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Swap the byte order of a logical.
c
      subroutine SwapLogical( LogIn,LogOut )
      implicit none
      
      integer*4 i                             
      logical*4 LogIn,LogOut,LogInTemp,LogOutTemp
      character CharIn*4,CharOut*4
      
      equivalence ( LogInTemp,CharIn )
      equivalence ( LogOutTemp,CharOut )
      
      LogInTemp= LogIn
c
c      sgi to intel byte order
c      do i= 1, 4
c        CharOut(i:i)= CharIn(5-i:5-i)
c      end do
c
c     dec to intel byte order
c
      CharOut(1:1)= CharIn(3:3)
      CharOut(2:2)= CharIn(4:4)
      CharOut(3:3)= CharIn(1:1)
      CharOut(4:4)= CharIn(2:2)
      LogOut= LogOutTemp
      return
      end                
c                     
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Swap the byte order of a real*4 number
c
      subroutine SwapReal4( RealIn,RealOut )
      implicit none
      
      integer*4 i
      real*4    RealIn,RealOut,RealInTemp,RealOutTemp
      integer*1 CharIn(4),CharOut(4)
      
      equivalence ( RealInTemp,CharIn )
      equivalence ( RealOutTemp,CharOut )
      
      RealInTemp= RealIn
c
c      sgi to intel byte order
c      do i= 1, 4
c        CharOut(i)= CharIn(5-i)
c      end do
c
c     dec to intel byte order
c
      CharOut(1)= CharIn(3)
      CharOut(2)= CharIn(4)
      CharOut(3)= CharIn(1)
      CharOut(4)= CharIn(2)
      if ( (CharOut(1) .ne. 0) .or. (CharOut(2) .ne. 0) .or.
     &     (CharOut(3) .ne. 0) .or. (CharOut(4) .ne. 0)     )
     &  CharOut(4)= CharOut(4) - 1
      RealOut= RealOutTemp
c      write(3,100) charin(1),charin(2),charin(3),charin(4),
c     &             charout(1),charout(2),charout(3),charout(4),
c     &             realin,realout
c100   format(1x,4(i4,1x)/1x,4(i4,1x)/1x,e12.6,1x,e12.6/)
      return
      end                
c                            
c
c                               
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Read in a 1 dimensional array of integers from the ANZ binary file
c              starting at record number rec_num and converting them to floating
c              point values using the array scaling factor scale_factor. Also,
c              uncompact the data and/or swap the byte order, if needed.
c
      subroutine read_1dim_array( rec_num,data,dim1,size1,scale_factor,
     &                            UnCompactData,StorageOrder,SwapBytes,error )
      implicit none
                         
      integer*4 rec_num,dim1,size1,scale_factor,error,StorageOrder(15)
      real*4    data(dim1)
      logical*4 UnCompactData,SwapBytes
      
      integer*4 i,j,pntr,buffer(128),IntOut

      error= 0
      read(2,rec=rec_num,err=999,iostat=error) buffer
      pntr= 1
      do i= 1, size1    
        if ( SwapBytes ) then
          call SwapInteger( buffer(pntr),IntOut )
          data(i)= IntOut/(10.0**scale_factor)
        else
          data(i)= buffer(pntr)/(10.0**scale_factor)
        end if
        pntr= pntr+1
        if ( pntr .gt. 128 ) then
          if ( i .ge. size1 ) return
          rec_num= rec_num+1
          read(2,rec=rec_num,err=999,iostat=error) buffer
          pntr= 1
        end if
      end do ! i
999   return  
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Read in a 2 dimensional array of integers from the ANZ binary file
c              starting at record number rec_num and converting them to floating
c              point values using the array scaling factor scale_factor. Also,
c              uncompact the data and/or swap the byte order, if needed.
c
      subroutine read_2dim_array( rec_num,data,dim1,dim2,size1,size2,
     &                            scale_factor,UnCompactData,StorageOrder,SwapBytes,error )
      implicit none

      integer*4 rec_num,dim1,dim2,size1,size2,scale_factor,error,
     &          StorageOrder(15)
      real*4    data(dim1,dim2)
      logical*4 UnCompactData,SwapBytes

      integer*4 i,j,pntr,buffer(128),NumElements,IntOut

      error= 0
      NumElements= size1*size2
      read(2,rec=rec_num,err=999,iostat=error) buffer
      pntr= 1
      do i= 1, size2    
        do j= 1, size1       
          if ( SwapBytes ) then
            call SwapInteger( buffer(pntr),IntOut )
            data(j,i)= IntOut/(10.0**scale_factor)
          else
            data(j,i)= buffer(pntr)/(10.0**scale_factor)
          end if
          pntr= pntr+1
          if ( pntr .gt. 128 ) then
            if ( i*j .ge. NumElements ) goto 900
            rec_num= rec_num+1
            read(2,rec=rec_num,err=999,iostat=error) buffer
            pntr= 1
          end if
        end do ! j
      end do ! i
c
c     Uncompact the data so as not to scramble it internally
c
900   if ( UnCompactData ) then
        do i= size2, 1, -1
          do j= 1, size1
            data(j,StorageOrder(i))= data(j,i)
          end do
        end do
      end if
999   return
      end         
c
c
c                   
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Read in a 3 dimensional array of integers from the ANZ binary file
c              starting at record number rec_num and converting them to floating
c              point values using the array scaling factor scale_factor. Also,
c              uncompact the data and/or swap the byte order, if needed.
c
      subroutine read_3dim_array( rec_num,data,dim1,dim2,dim3,
     &                            size1,size2,size3,scale_factor,
     &                            UnCompactData,StorageOrder,SwapBytes,error )
      implicit none

      integer*4 rec_num,dim1,dim2,dim3,size1,size2,size3,scale_factor,
     &          StorageOrder(15),error
      real*4    data(dim1,dim2,dim3)
      logical*4 UnCompactData,SwapBytes
                                      
      integer*4 i,j,k,pntr,buffer(128),NumElements,IntOut

      error= 0
      NumElements= size1*size2*size3
      read(2,rec=rec_num,err=999,iostat=error) buffer
      pntr= 1
      do i= 1, size3    
        do j= 1, size2       
          do k= 1, size1
            if ( SwapBytes ) then
              call SwapInteger( buffer(pntr),IntOut )
              data(k,j,i)= IntOut/(10.0**scale_factor)
            else
              data(k,j,i)= buffer(pntr)/(10.0**scale_factor)
            end if
            pntr= pntr+1
            if ( pntr .gt. 128 ) then
              if ( i*j*k .ge. NumElements ) goto 900
              rec_num= rec_num+1
              read(2,rec=rec_num,err=999,iostat=error) buffer
              pntr= 1
            end if                   
          end do ! k
        end do ! j
      end do ! i
c
c     Uncompact the data so as not to scramble it internally
c
900   if ( UnCompactData ) then
        do i= size3, 1, -1
          do j= 1, size2
            do k= 1, size1
              data(k,j,StorageOrder(i))= data(k,j,i)
            end do
          end do
        end do
      end if                                        
999   return
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Read in a 4 dimensional array of integers from the ANZ binary file
c              starting at record number rec_num and converting them to floating
c              point values using the array scaling factor scale_factor. Also,
c              uncompact the data and/or swap the byte order, if needed.
c
      subroutine read_4dim_array( rec_num,data,dim1,dim2,dim3,dim4,
     &                            size1,size2,size3,size4,scale_factor,
     &                            UnCompactData,StorageOrder,SwapBytes,error )
      implicit none

      integer*4 rec_num,size1,size2,size3,size4,error,dim1,dim2,dim3,dim4,
     &          scale_factor,StorageOrder(15)
      real*4    data(dim1,dim2,dim3,dim4)
      logical*4 UnCompactData,SwapBytes

      integer*4 i,j,k,l,pntr,buffer(128),NumElements,IntOut

      error= 0
      NumElements= size1*size2*size3*size4
      read(2,rec=rec_num,err=999,iostat=error) buffer 
      pntr= 1
      do i= 1, size4    
        do j= 1, size3       
          do k= 1, size2
            do l= 1, size1
              if ( SwapBytes ) then
                call SwapInteger( buffer(pntr),IntOut )
                data(l,k,j,i)= IntOut/(10.0**scale_factor)
              else
                data(l,k,j,i)= buffer(pntr)/(10.0**scale_factor)
              end if
              pntr= pntr+1
              if ( pntr .gt. 128 ) then
                if ( i*j*k*l .ge. NumElements ) goto 900
                rec_num= rec_num+1
                read(2,rec=rec_num,err=999,iostat=error) buffer
                pntr= 1
              end if
            end do ! l  
          end do ! k
        end do ! j
      end do ! i
c
c     Uncompact the data so as not to scramble it internally
c
900   if ( UnCompactData ) then
        do i= size4, 1, -1
          do j= 1, size3
            do k= 1, size2
              do l= 1, size1
                data(l,k,j,StorageOrder(i))= data(l,k,j,i)
              end do
            end do
          end do
        end do
      end if
999   return 
      end
c
c
c                   
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Write out the entirety of the ANZ analysis to a bianry file. This
c              file contains all data, the current state of the program flags, and
c              the history of the commands executed to produce the analysis results.
c              This when the file is read back in the program will be in exactly the
c              same state as when the file was written. A description of the particulars
c              of the file format can be found at the beginning of this file.
c
c	Routines called:
c     CountNumVarSaved
c     cycle_rtoi
c     Date
c     force_rtoi
c     general_rtoi
c     segment_rtoi
c     write_1dim_array
c     write_2dim_array
c     write_3dim_array
c     write_4dim_array
c     WriteStatus
c
      subroutine write_anz_file( filename,MF,SF,FF,JF,
     &                           AF,CF,EF,OF,NoFlags,
     &                           data_saved )
      implicit none
      include 'a_const.inc'                                  

      include 'a_struct.inc'
      include 'a_file.inc'
      include 'a_prm.inc'
      include 'a_names.inc'
      include 'a_mrk.inc'
      include 'a_seg.inc'
      include 'a_gls2.inc'
      include 'a_frc.inc'
      include 'a_jnt.inc'
      include 'a_enr.inc'
      include 'a_emg.inc'

      record /marker_flag/    MF
      record /segment_flag/   SF
      record /force_flag/     FF                 
      record /joint_flag/     JF
      record /avg_pos_flag/   AF
      record /cycle_flag/     CF
      record /emg_flag/       EF
      record /option_flag/    OF
      record /MiscFlags/      MISC_FLG
                                                  
      record /anz_layout/ ptr,scale
      record /general_param_INT/ GP_ANZ
      record /motion_param/      MP_ANZ
      record /force_param_INT/   FP_ANZ
      record /seg_param_INT/     SP_ANZ(MAX_SEG)
      record /cycle_INT/         CP_ANZ
      record /SegJntInfo/        SgJtInfo
                                      
      character filename*60
      logical*4 data_saved,NoFlags
      
      real*4    temp(MAX_ELC,2),temp2(MAX_MTN,MAX_SEG),temp3(MAX_MTN),
     &          temp4(MAX_MTN,2),temp5(MAX_ELC,4),temp6(MAX_ELC)
      integer*4 i,j,error,rec_num,seg_num,NumVar,VarNum(128),CurVarNum,RorW,
     &          MaxVar,jvrefnum,seg
      logical*4 CompactJntData,CompactSegData
      character Day*9,blank4*4

      data blank4 /'    '/
c                        
c     zero out all the pointers and scale factors
c
      do i= 1, 128                                   
        ptr.array(i)=   0
        scale.array(i)= 0
      end do   
      MaxVar= 110 ! NOTE this must be updated if more quantities are added to the file
      ptr.array(128)= MaxVar  
c                                 
c     Open file              
c                         
      data_saved= .false.
c														! VAX
c      This code will open the ANZ file on the VAX		! VAX
c 														! VAX
      open( unit=2,file=filename,status='unknown',access='direct',recl=512,		! VAX
     &      form='unformatted',recordtype='fixed',iostat=error,err=1000 )	! VAX
c																	! MAC
c      This code will open the ANZ file on the Macintosh			! MAC
c																	! MAC
c      if ( filename(1:1) .eq. '*' ) then							! MAC
c        open( unit=2,file=*,status='new',access='direct',recl=512,	! MAC
c     &        form='unformatted',creator='DaM ',filetype='Data',	! MAC
c     &        iostat=error,err=1000 )								! MAC
c        i= jvrefnum(2)
c        call f_setvolume(i)								! MAC
c      else															! MAC
c        open( unit=2,file=filename,status='new',access='direct',recl=512,	! MAC
c     &        form='unformatted',creator='DaM ',filetype='Data',			! MAC
c     &        iostat=error,err=1000 )								! MAC
c      end if														! MAC
c
c     Count the number of variables to be saved so that running display of the
c     progress of the file save can be displayed...
c
      call CountNumVarSaved( MF,SF,FF,JF,
     &                       AF,CF,EF,NoFLags,
     &                       NumVar,VarNum )
      CurVarNum= 0               
      RorW= 1
c
c     Write out the flags which indicate what and how the various
c     quantities in the ANZ file were calculated
c 
      rec_num= 2    
      if ( .not. NoFlags ) then
        rec_num= rec_num + 7
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        write(2,rec=3,err=1010,iostat=error) MF.array
        write(2,rec=4,err=1010,iostat=error) SF.array
        write(2,rec=5,err=1010,iostat=error) JF.array
        write(2,rec=6,err=1010,iostat=error) FF.array
        write(2,rec=7,err=1010,iostat=error) EF.array
        MISC_FLG.AvgFlag= AF
        MISC_FLG.OptFlag= OF
        MISC_FLG.CycFlag= CF
        write(2,rec=8,err=1010,iostat=error) MISC_FLG.array
        ptr.FLAGS= 3
        scale.FLAGS= 0  
      else
        ptr.FLAGS= 0        
      end if                             
c                                  
c     Write out the parameter structures.
c     Convert the GEN,FRC,MTN,SEG,CYC structures from integer to real format
c     This conversion is done so that the ANZ file contains only integers
c     and characters in the hopes that the file will be device independent.
c     Thus, the same ANZ file could be used on a VAX, mac, UNIX box or Macintosh.
c                           
c      call Date( Day )			! Mac version...
      call MakeDate(Day)
c								! VAX version....
      GP.last_updated= Day  
      GP.FileFormatVersion= ANZFileVersion           
      rec_num= rec_num + 1
      scale.PRM_general= 3
      GP.ByteOrderCheck= 1
      call general_rtoi( GP, scale.PRM_general, GP_ANZ )         
      call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
      write(2,rec=rec_num,err=1010,iostat=error) GP_ANZ.array
      ptr.PRM_general= rec_num                         
c
c     Check the storage order of the segment and joint data and see if the
c     data must be compacted when it is written to disk
c
      do i= 1, GP.num_jnt-1
        if ( GP.jnt_num(i) .gt. GP.jnt_num(i+1) ) goto 1040 ! error
      end do
      do i= 1, GP.num_seg-1
        if ( GP.seg_num(i) .gt. GP.seg_num(i+1) ) goto 1050 ! error
      end do
      CompactJntData= .false.
      do i= 1, GP.num_jnt-1
        if ( abs(GP.jnt_num(i)-GP.jnt_num(i+1)) .ne. 1 )
     &    CompactJntData= .true.
      end do
      if ( GP.jnt_num(1) .ne. 1 ) CompactJntData= .true.
      CompactSegData= .false.
      do i= 1, GP.num_seg-1
        if ( abs(GP.seg_num(i)-GP.seg_num(i+1)) .ne. 1 )
     &    CompactSegData= .true.
      end do
      if ( GP.seg_num(1) .ne. 1 ) CompactSegData= .true.      
          
      if ( MF.read .or. JF.TRU_angle .or. 
     &     (MP.nfrm .gt. 0) ) then
        scale.PRM_motion= 3
        rec_num= rec_num + 1
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        MP.frm_rate= 1000*int(rMtnFrmRate)
        write(2,rec=rec_num,err=1010,iostat=error) MP.array
        ptr.PRM_motion= rec_num
      end if               
                            
      if ( FF.read .or. (FP.nfrm .gt. 0) ) then
        scale.PRM_force= 3
        rec_num= rec_num + 1
        call force_rtoi( FP, scale.PRM_force, FP_ANZ )
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        write(2,rec=rec_num,err=1010,iostat=error) FP_ANZ.array
        ptr.PRM_force= rec_num                                      
      end if                
      
      if ( CF.statistics ) then ! gait cycle statistics are present
        scale.PRM_cycle= 3
        rec_num= rec_num + 1
        call cycle_rtoi( CP, scale.PRM_cycle, CP_ANZ )
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        write(2,rec=rec_num,err=1010,iostat=error) CP_ANZ.array
        ptr.PRM_cycle= rec_num
      end if
      
      if ( EF.read ) then                                  
        scale.PRM_EMG= 3
        rec_num= rec_num + 1
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        write(2,rec=rec_num,err=1010,iostat=error) EP.array
        ptr.PRM_EMG= rec_num
      end if                                     
c                                               
c     Save the history of the calculations done within ANZ
c
      if ( .not. NoFlags ) then 
        scale.History= 0
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        do i= 1, History(1).NumHistory
          write(2,rec=rec_num+i,err=1010,iostat=error) History(i).array
        end do                                          
        ptr.History= rec_num + 1
        rec_num= rec_num + History(1).NumHistory
      else                                      
        ptr.History= 0
      end if
c
c     Save the segment and joint names and connectivity info
c     Added by dam 1/93 to allow non-locomotion analyses
c     Pushes file version up to 'd'
c     Addition of SegJntNum pushes file version to 'e' - 12/93
c
      do i= 1, MAX_SEG   
        SgJtInfo.seg_names(i)= blank4
        do j= 1, 5
           SgJtInfo.SegJntNum(j,i)= 0
        end do
      end do
      do i= 1, GP.num_seg
        seg= GP.seg_num(i)
        SgJtInfo.seg_names(i)= seg_names(i)
        do j= 1, SP(seg).num_end
           SgJtInfo.SegJntNum(j,seg)= SegJntNum(j,seg)
        end do
      end do
      do i= 1, MAX_JNT
         SgJtInfo.jnt_names(i)= blank4
         SgJtInfo.JntSegNum(1,i)= 0
         SgJtInfo.JntSegNum(2,i)= 0
      end do	
      do i= 1, GP.num_jnt
        SgJtInfo.jnt_names(i)= jnt_names(i)
        SgJtInfo.JntSegNum(1,i)= JntSegNum(1,i)
        SgJtInfo.JntSegNum(2,i)= JntSegNum(2,i)
      end do
      rec_num= rec_num + 1
      call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
      write(2,rec=rec_num,err=1010,iostat=error) SgJtInfo.array
      ptr.SegJntInfo= rec_num
c                                               
c     Now write out the data...
c
c     marker data        
c                                     
      rec_num= rec_num + 1    
      if ( MF.read ) then
        ptr.MRK_pos_GCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,MrkData,3,MAX_MTN,MAX_MRK,
     &                        3,MP.nfrm,MP.num_mrk,
     &                        scale.MRK_pos_GCS,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
           
      if ( MF.diagnostics ) then
        ptr.MpnLCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_4dim_array( rec_num,MpnLCS,3,5,MAX_MTN,MAX_SEG,
     &                        3,5,MP.nfrm,GP.num_seg,
     &                        scale.MpnLCS,CompactSegData,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.MrkDist= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,MrkDist,6,MAX_MTN,MAX_SEG,
     &                         6,MP.nfrm,GP.num_seg,
     &                         scale.MrkDist,CompactSegData,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
      end if                

      if ( MF.VelAcc ) then
        ptr.MRK_Vel= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,MrkVel,3,MAX_MTN,MAX_MRK,
     &                        3,MP.nfrm,MP.num_mrk,
     &                        scale.MRK_Vel,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.MRK_Acc= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,MrkAcc,3,MAX_MTN,MAX_MRK,
     &                        3,MP.nfrm,MP.num_mrk,
     &                        scale.MRK_Acc,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
c                                                      
c     Segment data                   
c                  
*$ifdef DEBUG
      write(3,886)
 886  format(/' Segment parameters at anz file save')
*$endif
      if ( GP.num_seg .gt. 0 ) then
        if ( SF.param ) then
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          ptr.SEG_param= rec_num 
          scale.SEG_param= 4
          do i= 1, GP.num_seg
            seg_num= GP.seg_num(i)
*$ifdef DEBUG
	    write(3,997) i,seg_num,rec_num
 997        format(\' Index: ',i2,'  Segment number: ',i2,' Pointer: ',i4)
*$endif
            call segment_rtoi( SP(seg_num), scale.SEG_param, 
     &                       SP_ANZ(seg_num) )
            write(2,rec=rec_num,err=1010,iostat=error) SP_ANZ(seg_num).array
            rec_num= rec_num + 1
          end do              
        end if ! seg params present
        
        if ( SF.position ) then
          ptr.Sgp= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_4dim_array( rec_num,SgpGCS,3,4,MAX_MTN,MAX_SEG,
     &                          3,4,MP.nfrm,GP.num_seg,
     &                          scale.Sgp,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
        
        if ( SF.vel_acc ) then ! if this is present then all
                                ! the segment vel and acc are present
          ptr.SvlGCS= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SvlGCS,6,MAX_MTN,MAX_SEG,
     &                           6,MP.nfrm,GP.num_seg,
     &                           scale.SvlGCS,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.SacGCS= rec_num                        
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SacGCS,6,MAX_MTN,MAX_SEG,
     &                           6,MP.nfrm,GP.num_seg,
     &                           scale.SacGCS,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.SvlLCS= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SvlLCS,6,MAX_MTN,MAX_SEG,
     &                           6,MP.nfrm,GP.num_seg,
     &                           scale.SvlLCS,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.SacLCS= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SacLCS,6,MAX_MTN,MAX_SEG,
     &                           6,MP.nfrm,GP.num_seg,
     &                           scale.SacLCS,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if ! segment velocities and accelerations present
        
        if ( SF.energy ) then ! if this is present then all energies are present
          ptr.SEG_KE= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SegKE,3,MAX_MTN,MAX_SEG,
     &                           3,MP.nfrm,GP.num_seg,
     &                           scale.SEG_KE,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010           
          ptr.SEG_PE= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,SegPE,MAX_MTN,MAX_SEG,
     &                           MP.nfrm,GP.num_seg,
     &                           scale.SEG_PE,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.SEG_TE= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,SegTE,MAX_MTN,MAX_SEG,
     &                           MP.nfrm,GP.num_seg,
     &                           scale.SEG_TE,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.SEG_BodyKE= rec_num                
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,BodyKE,3,MAX_MTN,
     &                           3,MP.nfrm,scale.SEG_BodyKE,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010           
          ptr.SEG_BodyPE= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_1dim_array( rec_num,BodyPE,MAX_MTN,
     &                           MP.nfrm,scale.SEG_BodyPE,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.SEG_BodyTE= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_1dim_array( rec_num,BodyTE,MAX_MTN,
     &                           MP.nfrm,scale.SEG_BodyTE,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if ! segment energies are present
        
        if ( SF.FiniteScrew ) then
          ptr.SEG_FinScrew= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SgFinScrGCS,8,MAX_MTN,MAX_SEG,
     &                           8,MP.nfrm,GP.num_seg,
     &                           scale.SEG_FinScrew,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if                  

        if ( SF.InstantScrew ) then
          ptr.SEG_InsScrew= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SgInsScrGCS,8,MAX_MTN,MAX_SEG,
     &                           8,MP.nfrm,GP.num_seg,
     &                           scale.SEG_InsScrew,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
        
        if ( SF.cntr_mass ) then
          ptr.SEG_BodyCM= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,BodyCM,3,MAX_MTN,
     &                           3,MP.nfrm,scale.SEG_BodyCM,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
                  
        if ( SF.BCMVel ) then
          ptr.SEG_BCMVel= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,BCMVel,3,MAX_MTN,
     &                           3,MP.nfrm,scale.SEG_BCMVel,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if

        if ( SF.momentum ) then
          ptr.SgMom= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SgMom,8,MAX_MTN,MAX_SEG,
     &                           8,MP.nfrm,GP.num_seg,
     &                           scale.SgMom,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.SEG_BdMom= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,BdMom,8,MAX_MTN,
     &                           8,MP.nfrm,scale.SEG_BdMom,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if                    
        
        if ( SF.angle ) then
          ptr.SEG_angle= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,SangGCS,3,MAX_MTN,MAX_SEG,
     &                           3,MP.nfrm,GP.num_seg,
     &                           scale.SEG_angle,CompactSegData,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
        
      end if ! segment is present
c                            
c     Force plate data               
c
      if ( FF.read ) then
        ptr.FRC_plate_FpData= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,FpData,6,MAX_ELC,4,
     &                         6,FP.nfrm,FP.num_plt,
     &                         scale.FRC_plate_FpData,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010         
      end if

      if ( FF.adjusted ) then
        ptr.FRC_combined_GRL= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,CmbGrl,6,MAX_ELC,
     &                         6,FP.nfrm,scale.FRC_combined_GRL,
     &                         .false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.FRC_FpDataOK= rec_num
        do i= 1, FP.nfrm
          do j= 1, FP.num_plt
            temp5(i,j)= 0.0
            if ( FpDataOK(i,j) )  temp5(i,j)= -1.0
          end do ! j
        end do ! i
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,temp5,MAX_ELC,4,
     &                         FP.nfrm,FP.num_plt,
     &                         scale.FRC_FpDataOK,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.FRC_FcDataOK= rec_num
        do i= 1, FP.nfrm
          temp6(i)= 0.0
          if ( FcDataOK(i) ) temp6(i)= -1.0
        end do ! i
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_1dim_array( rec_num,temp6,MAX_ELC,FP.nfrm,
     &                         scale.FRC_FcDataOK,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010          
      end if        
        
      if ( FF.cpr_GCS ) then
        ptr.FRC_plate_CP= rec_num                
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,FpCp,3,MAX_ELC,4,
     &                         3,FP.nfrm,FP.num_plt,
     &                         scale.FRC_plate_CP,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
        if ( FF.adjusted ) then
          ptr.FRC_combined_CP= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,CmbCp,3,MAX_ELC,
     &                           3,FP.nfrm,scale.FRC_combined_CP,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
      end if

      if ( FF.wrench ) then
        ptr.FRC_plate_wrench= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        do i= 1, FP.num_plt    ! special scale problems
          do j= 1, FP.nfrm
            FpWre(7,j,i)= FpWre(7,j,i)/10000.0
          end do
        end do
        call write_3dim_array( rec_num,FpWre,8,MAX_ELC,4,
     &                         8,FP.nfrm,FP.num_plt,
     &                         scale.FRC_plate_wrench,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
        do i= 1, FP.num_plt    ! special scale problems
          do j= 1, FP.nfrm
            FpWre(7,j,i)= FpWre(7,j,i)*10000.0
          end do
        end do
        if ( FF.adjusted ) then          
          ptr.FRC_combined_wrench= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          do j= 1, FP.nfrm  ! special scale problems
            CmbGrlWre(7,j)= CmbGrlWre(7,j)/10000.0
          end do
          call write_2dim_array( rec_num,CmbGrlWre,8,MAX_ELC,
     &                           8,FP.nfrm,scale.FRC_combined_wrench,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          do j= 1, FP.nfrm  ! special scale problems
            CmbGrlWre(7,j)= CmbGrlWre(7,j)*10000.0
          end do
        end if
      end if

      if ( FF.Integral ) then
        ptr.FRC_PltIntegral= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,PltIntegral,4,MAX_ELC,4,
     &                         4,FP.nfrm,FP.num_plt,
     &                         scale.FRC_PltIntegral,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.FRC_PltIntegral2= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,PltIntegral2,4,MAX_ELC,4,
     &                         4,FP.nfrm,FP.num_plt,
     &                         scale.FRC_PltIntegral2,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
        if ( FF.adjusted ) then     
          ptr.FRC_CombIntegral= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,CombIntegral,4,MAX_ELC,
     &                           4,FP.nfrm,
     &                           scale.FRC_CombIntegral,.false.,
     &                           GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_CombIntegral2= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,CombIntegral2,4,MAX_ELC,
     &                           4,FP.nfrm,
     &                           scale.FRC_CombIntegral2,.false.,
     &                           GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
      end if

      if ( CF.event ) then
        ptr.FRC_FlrCntct= rec_num
        do i= 1, MP.nfrm
          do j= 1, 2
            temp4(i,j)= 0.0
            if ( FlrCntct(i,j) ) temp4(i,j)= -1.0
          end do ! j
        end do ! i
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,temp4,MAX_MTN,2,
     &                         MP.nfrm,2,
     &                         scale.FRC_FlrCntct,.false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
        
      if ( FF.aligned ) then
        ptr.FtGrlOK= rec_num
        do i= 1, FP.nfrm
          do j= 1, 2
            temp(i,j)= 0.0
            if ( FtGrlOK(i,j) )  temp(i,j)= -1.0
          end do ! j
        end do ! i
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,temp,MAX_ELC,2,
     &                         FP.nfrm,2,scale.FtGrlOK,
     &                         .false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010

        ptr.FRC_single_GRL= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,FtGrl,6,MAX_ELC,2,
     &                         6,FP.nfrm,2,scale.FRC_single_GRL,
     &                         .false.,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
                                 
        if ( FF.cpr_GCS ) then
          ptr.FRC_single_CP= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,FtCpGCS,3,MAX_ELC,2,
     &                           3,FP.nfrm,2,scale.FRC_single_CP,
     &                           .false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010          
        end if
                                    
        if ( FF.cpr_LCS ) then
          ptr.FRC_single_CP_LCS= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,FtCpLCS,3,MAX_MTN,2,
     &                           3,FP.nfrm,2,
     &                           scale.FRC_single_CP_LCS,.false.,
     &                           GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_FtGRLLCS= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,FtGRLLCS,6,MAX_MTN,2,
     &                           6,MP.nfrm,2,
     &                           scale.FRC_FtGRLLCS,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010       
        end if

        if ( FF.StrIdx ) then
          ptr.FRC_StrIdx= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array(rec_num,StrIdx,MAX_MTN,2,FP.nfrm,2,
     &                          scale.FRC_StrIdx,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
        
        if ( FF.wrench ) then  
          ptr.FRC_single_wrench= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          do i= 1, 2
            do j= 1, FP.nfrm
              FtGRLWre(7,j,i)= FtGRLWre(7,j,i)/10000.0
            end do
          end do
          call write_3dim_array( rec_num,FtGRLWre,8,MAX_ELC,2,
     &                           8,FP.nfrm,2,
     &                           scale.FRC_single_wrench,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          do i= 1, 2
            do j= 1, FP.nfrm
              FtGRLWre(7,j,i)= FtGRLWre(7,j,i)*10000.0
            end do
          end do
        end if
                   
        if ( FF.Integral ) then
          ptr.FRC_FtIntegral= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,FtIntegral,4,MAX_ELC,2,
     &                           4,FP.nfrm,2,
     &                           scale.FRC_FtIntegral,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_FtIntegral2= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,FtIntegral2,4,MAX_ELC,2,
     &                           4,FP.nfrm,2,
     &                           scale.FRC_FtIntegral2,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if

        if ( FF.PitchMomArm ) then
          ptr.FRC_PMAPnt= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,PmaPnt,3,MAX_MTN,
     &                           3,FP.nfrm,
     &                           scale.FRC_PMAPnt,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_PMATrk= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,PmaTrkLCS,3,MAX_MTN,
     &                           3,FP.nfrm,
     &                           scale.FRC_PMATrk,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_PMAPlv= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,PmaPlvLCS,3,MAX_MTN,
     &                           3,FP.nfrm,
     &                           scale.FRC_PMAPlv,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_PMAGCS= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,PmaGCS,3,MAX_MTN,
     &                           3,FP.nfrm,
     &                           scale.FRC_PMAGCS,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if

        if ( FF.PitchWreArm ) then
          ptr.FRC_PWAPnt= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,PwaPnt,3,MAX_MTN,
     &                           3,FP.nfrm,
     &                           scale.FRC_PWAPnt,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_PWATrk= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,PwaTrkLCS,3,MAX_MTN,
     &                           3,FP.nfrm,
     &                           scale.FRC_PWATrk,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_PWAPlv= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,PwaPlvLCS,3,MAX_MTN,
     &                           3,FP.nfrm,
     &                           scale.FRC_PWAPlv,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
          ptr.FRC_PWAGCS= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_2dim_array( rec_num,PwaGCS,3,MAX_MTN,
     &                           3,FP.nfrm,
     &                           scale.FRC_PWAGCS,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
        
        if ( FF.CprVelGCS ) then
          ptr.FRC_FtCprVelGCS= rec_num 
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,FtCprVelGCS,3,MAX_MTN,2,
     &                           3,MP.nfrm,2,
     &                           scale.FRC_FtCprVelGCS,.false.,GP.seg_num,error )
          if ( error .ne. 0 ) goto 1010
        end if
      end if ! force data aligned
c       
c     Joint data                               
c
      if ( JF.angle_JCS ) then
        ptr.JangJCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JangJCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JangJCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JaxesJCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_4dim_array( rec_num,JaxesJCS,3,3,MAX_MTN,MAX_JNT,
     &                         3,3,MP.nfrm,GP.num_jnt,   
     &                         scale.JaxesJCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010      
      end if
      
      if ( JF.vel_acc_JCS ) then
        ptr.JvelJCS= rec_num                                       
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JvelJCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JvelJCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JaccJCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JaccJCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,   
     &                         scale.JaccJCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
      
      if ( JF.angle_EUL ) then
        ptr.JangEUL= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JangEUL,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JangEUL,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JaxesEUL= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_4dim_array( rec_num,JaxesEUL,3,3,MAX_MTN,MAX_JNT,
     &                         3,3,MP.nfrm,GP.num_jnt,
     &                         scale.JaxesEUL,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010      
      end if
      
      if ( JF.vel_acc_EUL ) then
        ptr.JvelEUL= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JvelEUL,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JvelEUL,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JaccEUL= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JaccEUL,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JaccEUL,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
      
      if ( JF.load ) then
        do i= 1, MP.nfrm
          do j= 1, MAX_JNT
            temp2(i,j)= 0.0           
            if ( JntCalcOK(i,j) ) temp2(i,j)= -1.0
          end do ! j
        end do ! i
        ptr.JntCalcOK= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,temp2,MAX_MTN,MAX_JNT,
     &                         MP.nfrm,GP.num_jnt,
     &                         scale.JntCalcOK,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        do i= 1, MP.nfrm
          temp3(i)= JrlCalcType(i)            
        end do ! i
        ptr.JNT_calc_type= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_1dim_array( rec_num,temp3,MAX_MTN,
     &                         MP.nfrm,
     &                         scale.JNT_calc_type,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JrlGCS= rec_num     
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JrlGCS,6,MAX_MTN,MAX_JNT,
     &                         6,MP.nfrm,GP.num_jnt,
     &                         scale.JrlGCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010                     
        ptr.JrlJCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JrlJCS,8,MAX_MTN,MAX_JNT,
     &                         8,MP.nfrm,GP.num_jnt,
     &                         scale.JrlJCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JrlDCS= rec_num          
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JrlDCS,6,MAX_MTN,MAX_JNT,
     &                         6,MP.nfrm,GP.num_jnt,
     &                         scale.JrlDCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010                   
        ptr.JrlPCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JrlPCS,6,MAX_MTN,MAX_JNT,
     &                         6,MP.nfrm,GP.num_jnt,
     &                         scale.JrlPCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        if ( JF.legsummed ) then             
          ptr.JNT_LoadLegJCS= rec_num   
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,JRLegJCS,6,MAX_MTN,2,
     &                           6,MP.nfrm,2,
     &                           scale.JNT_LoadLegJCS,.false.,
     &                           GP.jnt_num,error )
          if ( error .ne. 0 ) goto 1010                   
          ptr.JNT_LoadLegGCS= rec_num   
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,JRLegGCS,6,MAX_MTN,2,
     &                           6,MP.nfrm,2,
     &                           scale.JNT_LoadLegGCS,.false.,
     &                           GP.jnt_num,error )
          if ( error .ne. 0 ) goto 1010
        end if ! legs summed              
      end if ! joint load present
      
      if ( JF.wrench ) then
        ptr.JwreLCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JwreLCS,8,MAX_MTN,MAX_JNT,
     &                         8,MP.nfrm,GP.num_jnt,
     &                         scale.JwreLCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if                                          
      
      if ( JF.power ) then         
        ptr.JNT_Power= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JntPower,4,MAX_MTN,MAX_JNT,
     &                         4,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_Power,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        if ( JF.legsummed ) then
          ptr.JNT_PowerLeg= rec_num
          call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
          call write_3dim_array( rec_num,JntPowerLeg,4,MAX_MTN,2,
     &                           4,MP.nfrm,2,
     &                           scale.JNT_PowerLeg,.false.,
     &                           GP.jnt_num,error )
          if ( error .ne. 0 ) goto 1010
        end if ! legs summed
      end if
      
      if ( JF.FiniteScrew ) then
        ptr.JNT_FinScrew= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JFinScr,8,MAX_MTN,MAX_JNT,
     &                         8,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_FinScrew,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if

      if ( JF.InstantScrew ) then
        ptr.JNT_InsScrew= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JInsScr,8,MAX_MTN,MAX_JNT,
     &                         8,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_InsScrew,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if      

      if ( JF.Translation ) then
        ptr.JNT_TransGCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JtrnGCS,4,MAX_MTN,MAX_JNT,
     &                         4,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_TransGCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_TransJCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JtrnJCS,4,MAX_MTN,MAX_JNT,
     &                         4,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_TransJCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_TransPCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JtrnPCS,4,MAX_MTN,MAX_JNT,
     &                         4,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_TransPCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_TransDCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JtrnDCS,4,MAX_MTN,MAX_JNT,
     &                         4,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_TransDCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if      

      if ( JF.ScrewAngle ) then
        ptr.JNT_FscAngJCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JfsAngJCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_FscAngJCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_FscAngPCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JfsAngPCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_FscAngPCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_FscAngDCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JfsAngDCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_FscAngDCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if                                   

      if ( JF.MomentArm ) then
        ptr.JNT_MomArmPCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JmaPCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_MomArmPCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_MomArmDCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JmaDCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_MomArmDCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_MomArmGCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JmaGCS,4,MAX_MTN,MAX_JNT,
     &                         4,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_MomArmGCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if

      if ( JF.WrenchArm ) then
        ptr.JNT_WreArmPCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JwaPCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_WreArmPCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_WreArmDCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JwaDCS,3,MAX_MTN,MAX_JNT,
     &                         3,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_WreArmDCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        ptr.JNT_WreArmGCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,JwaGCS,4,MAX_MTN,MAX_JNT,
     &                         4,MP.nfrm,GP.num_jnt,
     &                         scale.JNT_WreArmGCS,CompactJntData,
     &                         GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
c
c     Average position data
c
      if ( AF.position ) then
        ptr.SgpAvg= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,SgpAvg,3,4,MAX_SEG,
     &                         3,4,GP.num_seg,scale.SgpAvg,
     &                         CompactSegData,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
                        
      if ( AF.angle_JCS ) then
        ptr.AVG_angle_JCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,JangAvgJCS,3,MAX_JNT,
     &                         3,GP.num_seg,scale.AVG_angle_JCS,
     &                         CompactJntData,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if                         
      if ( AF.angle_EUL ) then
        ptr.AVG_angle_EUL= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,JangAvgEUL,3,MAX_JNT,
     &                         3,GP.num_seg,scale.AVG_angle_EUL,
     &                         CompactJntData,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if                         
c
c     Ground reaction load reconstruction        
c
      if ( FF.reconstructed ) then
        ptr.REC_IndFtLoadLCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,rFtGrlLCS,6,MAX_MTN,2,
     &                         6,MP.nfrm,2,
     &                         scale.REC_IndFtLoadLCS,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010

        ptr.REC_IndFtLoadGCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,rFtGrlGCS,6,MAX_MTN,2,
     &                         6,MP.nfrm,2,
     &                         scale.REC_IndFtLoadGCS,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010

        ptr.REC_IndFtCpLCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,rFtCpLCS,3,MAX_MTN,2,
     &                         3,MP.nfrm,2,
     &                         scale.REC_IndFtCpLCS,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010

        ptr.REC_IndFtCpGCS= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_3dim_array( rec_num,rFtCpGCS,3,MAX_MTN,2,
     &                         3,MP.nfrm,2,
     &                         scale.REC_IndFtCpGCS,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        
        ptr.REC_CombFtLoad= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,rCmbGrl,6,MAX_MTN,
     &                         6,MP.nfrm,
     &                         scale.REC_CombFtLoad,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        
        ptr.REC_CombFtCp= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,rCmbCp,3,MAX_MTN,
     &                         3,MP.nfrm,
     &                         scale.REC_CombFtCp,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
        
        ptr.REC_StrikeIndex= rec_num
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,rStrIdx,MAX_MTN,2,
     &                         MP.nfrm,2,
     &                         scale.REC_StrikeIndex,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if ! GRL reconstructed
c
c     EMG Data
c
      if ( EF.read ) then
        ptr.EMG_Data= rec_num
c
c       Kludge to fix a latent bug that sets EP.NumSamples= 0
c       I have tried countless times to find the origin of this but have
c       not been successful. Hopefully this will work in most cases.
c       dam 12/92
c
        if ( EP.NumSamples .eq. 0 ) then
          if ( FP.frm_rate .ne. 0 ) then
            EP.NumSamples= FP.nfrm*
     &                          (EP.SampleRate/FP.frm_rate)
          else
            EP.NumSamples= FP.nfrm
          end if
        end if
        call WriteStatus( RorW,0,0,NumVar,MaxVar,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,EMGData,MAX_EMF,MAX_EMG,
     &                         EP.NumSamples,EP.NumChnl,
     &                         scale.EMG_Data,.false.,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
c
c     Anatomic position/angle correction transformations
c
      if ( AF.RefCorrectAng ) then
        ptr.AVG_SgpRefCorAng= rec_num
        call WriteStatus( RorW,0,0,NumVar,106,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,SgpRefCorAng,3,MAX_SEG,
     &                         3,GP.num_seg,
     &                         scale.AVG_SgpRefCorAng,
     &                         CompactSegData,GP.seg_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
      
      if ( JF.RefCorrectAng ) then
        ptr.JNT_JntAngRefCorAng= rec_num
        call WriteStatus( RorW,0,0,NumVar,106,CurVarNum,VarNum,OF )
        call write_2dim_array( rec_num,JntAngRefCorAng,3,MAX_JNT,
     &                         3,GP.num_jnt,
     &                         scale.JNT_JntAngRefCorAng,
     &                         CompactJntData,GP.jnt_num,error )
        if ( error .ne. 0 ) goto 1010
      end if
c
c     Write out the record number pointer and scaling structures
c                                    
      write(2,rec=1,err=1020,iostat=error) ptr.array
      write(2,rec=2,err=1030,iostat=error) scale.array
      
      close(unit=2)
      data_saved= .true.
      return
                                     
1000  write(6,1005) filename,error
      if ( OF.ToLog ) write(3,1005) filename,error
1005  format(' Error- Trouble opening ',a40/
     &       '        Error # ',i3,' occurred.'/)
      return      
      
1010  write(6,1015) error,filename
      if ( OF.ToLog ) write(3,1015) error,filename
1015  format(' Error- Error # ',i3,' occured while writing to ',a40)
      close(unit=2)
      return
      
1020  write(6,1025) error
      if ( OF.ToLog ) write(3,1025) error
1025  format(' Error- Error # ',i3,' occured while writing pointer block.')
      close(unit=2)
      return
      
1030  write(6,1035) error
      if ( OF.ToLog ) write(3,1035) error
1035  format(' Error- Error # ',i3,' occured while writing scaling block.')
      close(unit=2)
      return      

1040  write(6,1045) 
      if ( OF.ToLog ) write(3,1045) 
1045  format(' Error- The order in which the joint data is stored in the'/
     &       '        internal arrays is not in a strictly ascending order'/
     &       '        The data cannot be saved without being scrambled'/
     &       '        This error should only occur if ANALYZE has been'/
     &       '        incorrectly modified')
      close(unit=2)
      return      

1050  write(6,1055) 
      if ( OF.ToLog ) write(3,1055) 
1055  format(' Error- The order in which the segment data is stored in the'/
     &       '        internal arrays is not in a strictly ascending order'/
     &       '        The data cannot be saved without being scrambled'/
     &       '        This error should only occur if ANALYZE has been'/
     &       '        incorrectly modified')
      close(unit=2)
      return      
      end      
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
c
c     Purpose: Count the number of data items to be saved in the ANZ file so that a
c              a running count of the progress of the save can be given. Basically,
c              the flag structures are checked for what quantities have been calculated
c              and hence must be saved.
c
c    Routines called
c      IncVarCnt
c
      subroutine CountNumVarSaved( MF,SF,FF,JF,AF,CF,EF,NoFlags,NumVar,VarNum )
      implicit none
      include 'a_const.inc'                                  

      include 'a_struct.inc'
      include 'a_prm.inc'
      include 'a_names.inc'

      record /marker_flag/    MF
      record /segment_flag/   SF
      record /force_flag/     FF                 
      record /joint_flag/     JF
      record /avg_pos_flag/   AF
      record /cycle_flag/     CF
      record /emg_flag/       EF

      integer*4 NumVar,VarNum(128)            
      logical*4 NoFlags
c
c     First check the parameter structures
c
      NumVar= 0
      if ( .not. NoFlags ) Call IncVarCnt( 107,VarNum(NumVar+1),NumVar ) ! Flags
      Call IncVarCnt( 1,VarNum(NumVar+1),NumVar ) ! General Params
      if ( MF.read .or. JF.TRU_angle .or. (MP.nfrm .gt. 0) ) 
     &                          Call IncVarCnt( 2,VarNum(NumVar+1),NumVar )
      if ( FF.read .or. (FP.nfrm .gt. 0) )
     &                          Call IncVarCnt( 3,VarNum(NumVar+1),NumVar )
      if ( CF.statistics ) Call IncVarCnt( 4,VarNum(NumVar+1),NumVar )
      if ( EF.read )       Call IncVarCnt( 71,VarNum(NumVar+1),NumVar )
      if ( .not. NoFlags ) Call IncVarCnt( 109,VarNum(NumVar+1),NumVar ) ! History
      Call IncVarCnt( 110,VarNum(NumVar+1),NumVar ) ! SegJntInfo
c
c     Now check the marker data               
c
      if ( MF.read ) Call IncVarCnt( 5,VarNum(NumVar+1),NumVar )
      if ( MF.diagnostics ) then
        Call IncVarCnt( 6,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 7,VarNum(NumVar+1),NumVar )
      end if
      if ( MF.VelAcc ) then
        call IncVarCnt( 99,VarNum(NumVar+1),NumVar )
        call IncVarCnt( 100,VarNum(NumVar+1),NumVar )
      end if
c
c     Now check segment data
c
      if ( GP.num_seg .gt. 0 ) then ! check all segment quantities
        if ( SF.param ) Call IncVarCnt( 8,VarNum(NumVar+1),NumVar )
        if ( SF.position ) Call IncVarCnt( 9,VarNum(NumVar+1),NumVar )
        if ( SF.vel_acc ) then ! all vel/acc present
          Call IncVarCnt( 10,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 11,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 12,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 13,VarNum(NumVar+1),NumVar )
        end if
        if ( SF.energy ) then ! all segment/body energies present
          Call IncVarCnt( 14,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 15,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 16,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 17,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 18,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 19,VarNum(NumVar+1),NumVar )
        end if
        if ( SF.FiniteScrew ) Call IncVarCnt( 20,VarNum(NumVar+1),NumVar )
        if ( SF.InstantScrew ) Call IncVarCnt( 73,VarNum(NumVar+1),NumVar )
        if ( SF.cntr_mass ) Call IncVarCnt( 21,VarNum(NumVar+1),NumVar )
        if ( SF.BCMVel ) Call IncVarCnt( 108,VarNum(NumVar+1),NumVar )
        if ( SF.momentum ) then
          Call IncVarCnt( 22,VarNum(NumVar+1),NumVar )
          Call IncVarCnt( 23,VarNum(NumVar+1),NumVar )
        end if
        if ( SF.angle ) Call IncVarCnt( 24,VarNum(NumVar+1),NumVar )
      end if ! segment data present at all...
c
c       Now check Force plate data
c
      if ( FF.read ) Call IncVarCnt( 25,VarNum(NumVar+1),NumVar ) ! load_
      if ( FF.adjusted ) then 
        Call IncVarCnt( 29,VarNum(NumVar+1),NumVar ) ! CmbGrl
        Call IncVarCnt( 75,VarNum(NumVar+1),NumVar ) ! FpDataOK
        Call IncVarCnt( 76,VarNum(NumVar+1),NumVar ) ! FcDataOK
      end if
      if ( FF.cpr_GCS ) then
        Call IncVarCnt( 38,VarNum(NumVar+1),NumVar ) ! FpCp
        if ( FF.adjusted ) Call IncVarCnt( 31,VarNum(NumVar+1),NumVar )
      end if                                   ! ^ comb_cp
      if ( FF.wrench ) then
        Call IncVarCnt( 39,VarNum(NumVar+1),NumVar ) ! FpWrench
        if ( FF.adjusted ) Call IncVarCnt( 35,VarNum(NumVar+1),NumVar )
      end if                                    ! ^ FcWrench
      if ( FF.Integral ) then
        Call IncVarCnt( 40,VarNum(NumVar+1),NumVar ) ! FpIntegral     
        Call IncVarCnt( 103,VarNum(NumVar+1),NumVar ) ! FpIntegral2     
        if ( FF.adjusted ) then
          Call IncVarCnt( 37,VarNum(NumVar+1),NumVar ) ! CombIntegral
          Call IncVarCnt( 102,VarNum(NumVar+1),NumVar ) ! CombIntegral2 
        end if    
      end if                                   !  
      if ( CF.event ) Call IncVarCnt( 27,VarNum(NumVar+1),NumVar ) ! FlrCntct
      if ( FF.aligned ) then
        Call IncVarCnt( 26,VarNum(NumVar+1),NumVar ) ! FtGrlOK
        Call IncVarCnt( 28,VarNum(NumVar+1),NumVar ) ! FtGrl
        if ( FF.cpr_GCS ) Call IncVarCnt( 30,VarNum(NumVar+1),NumVar ) ! FtCpGCS
        if ( FF.cpr_LCS ) then
          Call IncVarCnt( 32,VarNum(NumVar+1),NumVar ) ! FtCpLCS
          Call IncVarCnt( 77,VarNum(NumVar+1),NumVar ) ! FtGRLLCS
        end if   
        if ( FF.StrIdx ) Call IncVarCnt( 33,VarNum(NumVar+1),NumVar ) ! FtSI 
        if ( FF.wrench ) Call IncVarCnt( 34,VarNum(NumVar+1),NumVar ) ! FtWrench
        if ( FF.Integral ) then
          Call IncVarCnt( 36,VarNum(NumVar+1),NumVar ) ! FtIntegral
          Call IncVarCnt( 101,VarNum(NumVar+1),NumVar ) ! FtIntegral2
        end if
        if ( FF.PitchMomArm ) then
          call IncVarCnt( 85,VarNum(NumVar+1),NumVar )
          call IncVarCnt( 86,VarNum(NumVar+1),NumVar )
          call IncVarCnt( 87,VarNum(NumVar+1),NumVar )
          call IncVarCnt( 88,VarNum(NumVar+1),NumVar )
        end if
        if ( FF.PitchWreArm ) then
          call IncVarCnt( 89,VarNum(NumVar+1),NumVar )
          call IncVarCnt( 90,VarNum(NumVar+1),NumVar )
          call IncVarCnt( 91,VarNum(NumVar+1),NumVar )
          call IncVarCnt( 92,VarNum(NumVar+1),NumVar )
        end if
        if ( FF.CprVelGCS ) call IncVarCnt( 104,VarNum(NumVar+1),NumVar )
      end if ! Force data aligned
c 
c     Now check joint data
c                           
      if ( JF.angle_JCS ) then
        Call IncVarCnt( 41,VarNum(NumVar+1),NumVar ) ! JangJCS
        Call IncVarCnt( 44,VarNum(NumVar+1),NumVar ) ! JaxesJCS
      end if
      if ( JF.vel_acc_JCS ) then
        Call IncVarCnt( 42,VarNum(NumVar+1),NumVar ) ! JvelJCS
        Call IncVarCnt( 43,VarNum(NumVar+1),NumVar ) ! JaccJCS
      end if
      if ( JF.angle_EUL ) then
        Call IncVarCnt( 45,VarNum(NumVar+1),NumVar ) ! JangEUL
        Call IncVarCnt( 48,VarNum(NumVar+1),NumVar ) ! JaxesEUL
      end if
      if ( JF.vel_acc_JCS ) then
        Call IncVarCnt( 46,VarNum(NumVar+1),NumVar ) ! JvelEUL
        Call IncVarCnt( 47,VarNum(NumVar+1),NumVar ) ! JaccEUL
      end if
      if ( JF.load ) then
        Call IncVarCnt( 49,VarNum(NumVar+1),NumVar ) ! JntCalcOK
        Call IncVarCnt( 50,VarNum(NumVar+1),NumVar ) ! jnt_calc_type
        Call IncVarCnt( 51,VarNum(NumVar+1),NumVar ) ! JrlGCS
        Call IncVarCnt( 52,VarNum(NumVar+1),NumVar ) ! JrlJCS
        Call IncVarCnt( 53,VarNum(NumVar+1),NumVar ) ! JrlDCS
        Call IncVarCnt( 54,VarNum(NumVar+1),NumVar ) ! JrlPCS
        if ( JF.legsummed ) then
          Call IncVarCnt( 55,VarNum(NumVar+1),NumVar ) ! JRLegJCS
          Call IncVarCnt( 56,VarNum(NumVar+1),NumVar ) ! JRLegGCS
        end if
      end if ! joint loads present
      if ( JF.wrench ) Call IncVarCnt( 57,VarNum(NumVar+1),NumVar ) ! JntWrench
      if ( JF.power ) then
        Call IncVarCnt( 58,VarNum(NumVar+1),NumVar ) ! JntPower
        if ( JF.legsummed ) Call IncVarCnt( 59,VarNum(NumVar+1),NumVar )
      end if                                    ! ^ JntPowerLeg
      if ( JF.FiniteScrew ) Call IncVarCnt( 60,VarNum(NumVar+1),NumVar )
      if ( JF.InstantScrew ) Call IncVarCnt( 74,VarNum(NumVar+1),NumVar )
      if ( JF.Translation ) then
        Call IncVarCnt( 78,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 79,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 80,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 81,VarNum(NumVar+1),NumVar )
      end if
      if ( JF.ScrewAngle ) then
        Call IncVarCnt( 82,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 83,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 84,VarNum(NumVar+1),NumVar )
      end if
      if ( JF.MomentArm ) then
        Call IncVarCnt( 93,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 94,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 95,VarNum(NumVar+1),NumVar )
      end if              
      if ( JF.WrenchArm ) then
        Call IncVarCnt( 96,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 97,VarNum(NumVar+1),NumVar )
        Call IncVarCnt( 100,VarNum(NumVar+1),NumVar ) 
      end if
c
c     Check Average position data
c
      if ( AF.position ) Call IncVarCnt( 61,VarNum(NumVar+1),NumVar )
      if ( AF.angle_JCS ) Call IncVarCnt( 62,VarNum(NumVar+1),NumVar )
      if ( AF.angle_JCS ) Call IncVarCnt( 63,VarNum(NumVar+1),NumVar )
c
c     Check reconstructed ground reaction load data
c
      if ( FF.reconstructed ) then
        Call IncVarCnt( 64,VarNum(NumVar+1),NumVar ) ! rFtGrlLCS
        Call IncVarCnt( 65,VarNum(NumVar+1),NumVar ) ! rFtGrlGCS
        Call IncVarCnt( 66,VarNum(NumVar+1),NumVar ) ! IndFtLoadCpLCS
        Call IncVarCnt( 67,VarNum(NumVar+1),NumVar ) ! IndFtLoadCpGCS
        Call IncVarCnt( 68,VarNum(NumVar+1),NumVar ) ! rCmbGrl
        Call IncVarCnt( 69,VarNum(NumVar+1),NumVar ) ! rCmbCp
        Call IncVarCnt( 70,VarNum(NumVar+1),NumVar ) ! SiRec
      end if
c
c     Check the emg data
c
      if ( EF.read ) Call IncVarCnt( 72,VarNum(NumVar+1),NumVar )
c
c     Lastly check the anatomic position/angle correction transformations
c      
      if ( AF.RefCorrectAng ) 
     &   Call IncVarCnt( 105,VarNum(NumVar+1),NumVar )
      if ( JF.RefCorrectAng ) 
     &   Call IncVarCnt( 106,VarNum(NumVar+1),NumVar )
      return
      end
c
c                                           
c                                                                  
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Increment the counter of the number of quantities to be saved and their
c              ID numbers. The ID's are used to print out a text name of the quantity
c              during the save.
c
      subroutine IncVarCnt( VarID,VarNum,NumVar )
      implicit none
 
      integer*4 VarID,VarNum,NumVar

      VarNum= VarID
      NumVar= NumVar + 1
      return
      end
c
c
c                         
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Convert the GP structure from its normal form to a special 
c              version which contains only integers so that it may be saved in
c              the ANZ file.
c
      subroutine general_rtoi( GP, scale_factor, GP_ANZ )
      implicit none
      include 'a_const.inc'                                  

      include 'a_struct.inc'
      include 'a_file.inc'

      record /general_param/     GP
      record /general_param_INT/ GP_ANZ
      integer*4 scale_factor
                                      
      integer*4 i,j
       
      scale_factor= 3
      GP_ANZ.MtnFile=  GP.MtnFile
      GP_ANZ.AvgFile= GP.AvgFile
      GP_ANZ.FrcFile=   GP.FrcFile
      GP_ANZ.last_updated= GP.last_updated
      GP_ANZ.name=         GP.name
      GP_ANZ.comment=      GP.comment
      GP_ANZ.age=          GP.age
      GP_ANZ.weight= int(GP.weight*1000.)
      GP_ANZ.height= int(GP.height*1000.)
      GP_ANZ.num_seg=      GP.num_seg
      do i= 1, GP.num_seg    
        GP_ANZ.seg_num(i)= GP.seg_num(i)
      end do    
      GP_ANZ.num_jnt=      GP.num_jnt
      do i= 1, GP.num_jnt
        GP_ANZ.jnt_num(i)= GP.jnt_num(i)
      end do    
      GP_ANZ.single_cycle=   GP.single_cycle
      GP_ANZ.normalized=     GP.normalized
      GP_ANZ.seg_unrotated=  GP.seg_unrotated
      GP_ANZ.seg_smooth=     GP.seg_smooth
      GP_ANZ.int_poly_order= GP.int_poly_order
      GP_ANZ.analysis_type=  GP.analysis_type
      GP_ANZ.ByteOrderCheck= GP.ByteOrderCheck
      GP_ANZ.Sex=            GP.Sex
      GP_ANZ.FileFormatVersion= GP.FileFormatVersion
      return                                
      end                                  
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Convert the FP structure from its normal form to a special 
c              version which contains only integers so that it may be saved in
c              the ANZ file.
c
      subroutine force_rtoi( FP, scale_factor, FP_ANZ )
      implicit none
      include 'a_const.inc'                                  

      include 'a_struct.inc'
      include 'a_file.inc'

      record /force_param/     FP
      record /force_param_INT/ FP_ANZ
      integer*4 scale_factor

      integer*4 i,j,k
                                             
      scale_factor= 3
      FP_ANZ.nfrm=    FP.nfrm
      FP_ANZ.frm_rate=   FP.frm_rate
      FP_ANZ.av_frm_ratio= int( FP.av_frm_ratio*1000. )
      FP_ANZ.frm_offset= FP.frm_offset
      FP_ANZ.first_frm=  FP.first_frm
      FP_ANZ.last_frm=   FP.last_frm
      FP_ANZ.num_plt=    FP.num_plt
      do i= 1, 2    
        do j= 1, 4
          FP_ANZ.plt_size(i,j)= int( FP.plt_size(i,j)*1000. )
        end do
      end do
      do i= 1, 4
        do j= 1, 3
          do k= 1, 4
            FP_ANZ.plt_loc(i).LCS(j,k)= 
     &                                  int( FP.plt_loc(i).LCS(j,k)*1000. )
          end do
        end do
      end do
      FP_ANZ.num_rtft_plt= FP.num_rtft_plt
      FP_ANZ.num_lfft_plt= FP.num_lfft_plt
      do i= 1, 4
        FP_ANZ.rtft_plt(i)= FP.rtft_plt(i)
        FP_ANZ.lfft_plt(i)= FP.lfft_plt(i)
      end do        
      FP_ANZ.dbl_stance_OK= FP.dbl_stance_OK
      FP_ANZ.FirstFoot=     FP.FirstFoot
      FP_ANZ.Direction=     FP.Direction
      return            
      end   
c
c           
c        
c                              
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Convert a SP structure from its normal form to a special 
c              version which contains only integers so that it may be saved in
c              the ANZ file.
c
      subroutine segment_rtoi( SP, scale_factor, SP_ANZ )
      implicit none
      include 'a_const.inc'                                  

      include 'a_struct.inc'
      include 'a_file.inc'
                  
      record /seg_param/     SP
      record /seg_param_INT/ SP_ANZ
      integer*4 scale_factor
                                    
      integer*4 i,j                                 
      real*4    scale
                                                    
      scale_factor= 6
      scale= 10.0**scale_factor

*$ifdef DEBUG
      
      write(3,99) scale_factor,scale
      write(6,99) scale_factor,scale
 99   format('Scale Factor: ',i2/' Scale: ',f15.6)

      write(3,101) SP.length
      write(6,101) SP.length
 101  format(' Length: ',f12.6)

      write(3,111) SP.mass,SP.avg_cntr(1),
     &             SP.avg_cntr(2),
     &             SP.avg_cntr(3),SP.num_end
      write(6,111) SP.mass,SP.avg_cntr(1),
     &             SP.avg_cntr(2),
     &             SP.avg_cntr(3),SP.num_end
 111  format(' Mass  : ',f12.6/' Center: ',3(f12.6,1x)/,' # Ends: ',i12)

      write(6,121) SP.inertia(1),SP.inertia(2),
     &             SP.inertia(3)
 121  format(' Inertia: ',3(f12.6,1x))

      write(3,131)
      write(6,131)
 131  format(' Shape:')
      do j= 1, 8
         write(3,141) SP.shape(1,j),SP.shape(2,j),
     &                SP.shape(3,j)
         write(6,141) SP.shape(1,j),SP.shape(2,j),
     &                SP.shape(3,j)
 141     format(3(f12.6,1x))
      end do

      write(3,151)
      write(6,151)
 151  format(' Ends:')
      do j= 1, 4
         write(3,141) SP.end(1,j),SP.end(2,j),
     &                SP.end(3,j)
         write(6,141) SP.end(1,j),SP.end(2,j),
     &                SP.end(3,j)
      end do    

      write(3,161) SP.num_mrk
      write(6,161) SP.num_mrk
 161  format(' # Mrk : ',i12)

      do i= 1, 5
         write(3,171) SP.mrk_num(i)
         write(6,171) SP.mrk_num(i)
 171     format(1x,i12)
      end do                                                               

      write(3,181) SP.num_mrkdist
      write(6,181) SP.num_mrkdist
 181  format(' # MrkD: ',i12)
 
      do i= 1, 5
         write(3,191) SP.dist_mrk_num(1,i),
     &                SP.dist_mrk_num(2,i)
         write(6,191) SP.dist_mrk_num(1,i),
     &                SP.dist_mrk_num(2,i)
 191     format(1x,i12,1x,i12)
      end do                                                               


         write(3,201)
         write(6,201)
 201     format(' Rigid Body parameters'/' TCStoLCS'/)
         do i= 1, 4
            write(3,141) SP.TCStoLCS(1,i),
     &                   SP.TCStoLCS(2,i),       
     &                   SP.TCStoLCS(3,i)
            write(6,141) SP.TCStoLCS(1,i),
     &                   SP.TCStoLCS(2,i),       
     &                   SP.TCStoLCS(3,i)
         end do

         write(3,211) SP.nmTCS
         write(6,211) SP.nmTCS
 211     format('# Mrk : ',i12)
         SP.nmTCS= SP.nmTCS
         do i= 1, 5
            write(3,221) SP.mnTCS(i),
     &                   SP.mpnLCS(1,i),
     &                   SP.mpnLCS(2,i),
     &                   SP.mpnLCS(3,i)
            write(6,221) SP.mnTCS(i),
     &                   SP.mpnLCS(1,i),
     &                   SP.mpnLCS(2,i),
     &                   SP.mpnLCS(3,i)
 221        format(1x,i12,3(f12.6,1x))
         end do
*$endif

      SP_ANZ.length= int( SP.length*scale )
      do i= 1, 3    
        SP_ANZ.avg_cntr(i)= int( SP.avg_cntr(i)*scale )
      end do
      SP_ANZ.mass=   int( SP.mass*scale )
      SP_ANZ.num_end= SP.num_end         
      do i= 1, 3 
        SP_ANZ.inertia(i)= int( SP.inertia(i)*scale )
        do j= 1, 8 
          SP_ANZ.shape(i,j)= int( SP.shape(i,j)*scale )
        end do                                   
        do j= 1, 4                               
          SP_ANZ.end(i,j)= int( SP.end(i,j)*scale )
        end do
      end do      
      SP_ANZ.num_mrk= SP.num_mrk
      do i= 1, SP.num_mrk                   
        SP_ANZ.mrk_num(i)= SP.mrk_num(i)
      end do    
      SP_ANZ.num_MrkDist= SP.num_MrkDist
      do i= 1, SP.num_MrkDist
        do j= 1,2 
          SP_ANZ.dist_mrk_num(j,i)= SP.dist_mrk_num(j,i)
        end do    
      end do
      do i= 1, 4
        do j= 1, 3
          SP_ANZ.TCStoLCS(j,i)= SP.TCStoLCS(j,i)*scale
        end do
      end do
      SP_ANZ.nmTCS= SP.nmTCS
      do i= 1, 5
        do j= 1, 3
          SP_ANZ.mpnLCS(j,i)= SP.mpnLCS(j,i)*scale
        end do
        SP_ANZ.mnTCS(i)= SP.mnTCS(i)
      end do

*$ifdef DEBUG
      
      write(3,100) scale_factor,SP_ANZ.length
      write(6,100) scale_factor,SP_ANZ.length
 100  format(' Scale : 10^',i2/' Length: ',i12)

      write(3,110) SP_ANZ.mass,SP_ANZ.avg_cntr(1),
     &             SP_ANZ.avg_cntr(2),
     &             SP_ANZ.avg_cntr(3),SP_ANZ.num_end
      write(6,110) SP_ANZ.mass,SP_ANZ.avg_cntr(1),
     &             SP_ANZ.avg_cntr(2),
     &             SP_ANZ.avg_cntr(3),SP_ANZ.num_end
 110  format(' Mass  : ',i12/' Center: ',3(i12,1x)/,' # Ends: ',i12)

      write(3,120) SP_ANZ.inertia(1),SP_ANZ.inertia(2),
     &             SP_ANZ.inertia(3)
      write(6,120) SP_ANZ.inertia(1),SP_ANZ.inertia(2),
     &             SP_ANZ.inertia(3)
 120  format(' Inertia: ',3(i12,1x))

      write(3,130)
      write(6,130)
 130  format(' Shape:')
      do j= 1, 8
         write(3,140) SP_ANZ.shape(1,j),SP_ANZ.shape(2,j),
     &                SP_ANZ.shape(3,j)
         write(6,140) SP_ANZ.shape(1,j),SP_ANZ.shape(2,j),
     &                SP_ANZ.shape(3,j)
 140     format(3(i12,1x))
      end do

      write(3,150)
      write(6,150)
 150  format(' Ends:')
      do j= 1, 4
         write(3,140) SP_ANZ.end(1,j),SP_ANZ.end(2,j),
     &                SP_ANZ.end(3,j)
         write(6,140) SP_ANZ.end(1,j),SP_ANZ.end(2,j),
     &                SP_ANZ.end(3,j)
      end do    

      write(3,160) SP_ANZ.num_mrk
      write(6,160) SP_ANZ.num_mrk
 160  format(' # Mrk : ',i12)

      do i= 1, 5
         write(3,170) SP_ANZ.mrk_num(i)
         write(6,170) SP_ANZ.mrk_num(i)
 170     format(1x,i12)
      end do                                                               

      write(3,180) SP_ANZ.num_mrkdist
      write(6,180) SP_ANZ.num_mrkdist
 180  format(' # MrkD: ',i12)
 
      do i= 1, 5
         write(3,190) SP_ANZ.dist_mrk_num(1,i),
     &                SP_ANZ.dist_mrk_num(2,i)
         write(6,190) SP_ANZ.dist_mrk_num(1,i),
     &                SP_ANZ.dist_mrk_num(2,i)
 190     format(1x,i12,1x,i12)
      end do                                                               


         write(3,200)
         write(6,200)
 200     format(' Rigid Body parameters'/' TCStoLCS'/)
         do i= 1, 4
            write(3,140) SP_ANZ.TCStoLCS(1,i),
     &                   SP_ANZ.TCStoLCS(2,i),       
     &                   SP_ANZ.TCStoLCS(3,i)
            write(6,140) SP_ANZ.TCStoLCS(1,i),
     &                   SP_ANZ.TCStoLCS(2,i),       
     &                   SP_ANZ.TCStoLCS(3,i)
         end do

         write(3,210) SP_ANZ.nmTCS
         write(6,210) SP_ANZ.nmTCS
 210     format('# Mrk : ',i12)
         SP_ANZ.nmTCS= SP_ANZ.nmTCS
         do i= 1, 5
            write(3,220) SP_ANZ.mnTCS(i),
     &                   SP_ANZ.mpnLCS(1,i),
     &                   SP_ANZ.mpnLCS(2,i),
     &                   SP_ANZ.mpnLCS(3,i)
            write(6,220) SP_ANZ.mnTCS(i),
     &                   SP_ANZ.mpnLCS(1,i),
     &                   SP_ANZ.mpnLCS(2,i),
     &                   SP_ANZ.mpnLCS(3,i)
 220        format(1x,i12,3(i12,1x))
         end do
*$endif
      return
      end                                                    
c                         
c                                                                           
c        
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Convert the CP structure from its normal form to a special 
c              version which contains only integers so that it may be saved in
c              the ANZ file.
c
      subroutine cycle_rtoi( CP, scale_factor, CP_ANZ )
      implicit none
      include 'a_const.inc'                                  

      include 'a_struct.inc'
      include 'a_file.inc'

      record /cycle/     CP
      record /cycle_INT/ CP_ANZ
      integer*4 scale_factor

      integer*4 i
      real*4    Scale

      Scale= 10.0**scale_factor
      scale_factor= 3       
      CP_ANZ.first_side=    CP.first_side
      CP_ANZ.stride_length=       int( CP.stride_length*Scale)
      CP_ANZ.NormStrideLength=    int( CP.NormStrideLength*Scale)
      CP_ANZ.PctNormStrideLength= int( CP.PctNormStrideLength*Scale)
      CP_ANZ.step_width=          int( CP.step_width*Scale)
      CP_ANZ.NormStepWidth=       int( CP.NormStepWidth*Scale)
      CP_ANZ.PctNormStepWidth=    int( CP.PctNormStepWidth*Scale)
      CP_ANZ.cycle_time=          int( CP.cycle_time*Scale)
      CP_ANZ.NormCycleTime=       int( CP.NormCycleTime*Scale)
      CP_ANZ.PctNormCycleTime=    int( CP.PctNormCycleTime*Scale)
      CP_ANZ.cadence=             int( CP.cadence*Scale)
      CP_ANZ.NormCadence=         int( CP.NormCadence*Scale)
      CP_ANZ.PctNormCadence=      int( CP.PctNormCadence*Scale)
      CP_ANZ.velocity=            int( CP.velocity*Scale)
      CP_ANZ.NormVelocity=        int( CP.NormVelocity*Scale)
      CP_ANZ.PctNormVelocity=     int( CP.PctNormVelocity*Scale)
      do i= 1, 2
        CP_ANZ.step_length(i)=    int( CP.step_length(i)*Scale )
        CP_ANZ.NormStepLength(i)= int( CP.NormStepLength(i)*Scale)
        CP_ANZ.PctNormStepLength(i)= int( CP.PctNormStepLength(i)*Scale)
        CP_ANZ.sgl_time(i)=       int( CP.sgl_time(i)*Scale )
        CP_ANZ.sgl_percent(i)=    int( CP.sgl_percent(i)*Scale )
        CP_ANZ.NormSglPct(i)=     int( CP.NormSglPct(i)*Scale )
        CP_ANZ.dbl_time(i)=       int( CP.dbl_time(i)*Scale)
        CP_ANZ.dbl_percent(i)=    int( CP.dbl_percent(i)*Scale)
        CP_ANZ.NormDblPct(i)=     int( CP.NormDblPct(i)*Scale )
        CP_ANZ.total_time(i)=     int( CP.total_time(i)*Scale )
        CP_ANZ.total_percent(i)=  int( CP.total_percent(i)*Scale )
        CP_ANZ.NormTotalPct(i)=   int( CP.NormTotalPct(i)*Scale )
        CP_ANZ.to_percent(i)=     int( CP.to_percent(i)*Scale )
        CP_ANZ.NormToPct(i)=      int( CP.NormToPct(i)*Scale )
      end do
      do i= 1, 3          
        CP_ANZ.hs_percent(i)= int( CP.hs_percent(i)*Scale )
        CP_ANZ.NormHsPct(i)=  int( CP.NormHsPct(i)*Scale )
      end do
      return
      end                        
                 
c                    
c                               
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Write a one dimensional real array to the output file and 
c              return the record number of the next available record to which data 
c              may be written. The process of writing the data out involves computing
c              the scaling factor for the real data, using this scaling factor to
c              convert the real data to integer data, compacting the array if needed,
c              and then writing this out to disk in 512 byte blocks.
c
c     Routines called:
c       find_scale_factor_1d
c
      subroutine Write_1dim_Array( rec_num,data,dim1,size1,scale_factor,
     &                             CompactData,StorageOrder,error )
      implicit none
                   
      integer*4 rec_num,dim1,size1,scale_factor,error,StorageOrder(15)
      real*4    data(dim1)
      logical*4 CompactData
                                                   
      integer*4 i,j,pntr,buffer(128)

      call find_scale_factor_1d( dim1,size1,data,scale_factor )
      pntr= 1            
      do i= 1, size1    
        buffer(pntr)= int( data(i)*(10.0**scale_factor) )
        pntr= pntr+1
        if ( pntr .gt. 128 ) then
          write(2,rec=rec_num,err=999,iostat=error) buffer
          rec_num= rec_num+1
          pntr= 1
        end if
      end do ! i
      if ( pntr .ne. 1 ) then
        write(2,rec=rec_num,err=999,iostat=error) buffer
        rec_num= rec_num+1
      end if                  
999   return
      end
c
c                            
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Write a two dimensional real array to the output file and 
c              return the record number of the next available record to which data 
c              may be written. The process of writing the data out involves computing
c              the scaling factor for the real data, using this scaling factor to
c              convert the real data to integer data, compacting the array if needed,
c              and then writing this out to disk in 512 byte blocks.
c
c     Routines called:
c       find_scale_factor_2d
c
      subroutine Write_2dim_Array( rec_num,data,dim1,dim2,size1,size2,
     &                             scale_factor,CompactData,StorageOrder,error )
      implicit none                                               
                   
      integer*4 rec_num,dim1,dim2,size1,size2,error,scale_factor,
     &          StorageOrder(15)
      real*4    data(dim1,dim2)
      logical*4 CompactData

      integer*4 i,j,pntr,buffer(128)

c
c     Compact the data if necessary
c
      if ( CompactData ) then
        do i= 1, size2
          do j= 1, size1
            data(j,i)= data(j,StorageOrder(i))
          end do
        end do
      end if
c                         
c     Save the data after scaling 
c
      call find_scale_factor_2d( dim1,dim2,size1,size2,data,scale_factor )
      pntr= 1
      do i= 1, size2
        do j= 1, size1       
          buffer(pntr)= int( data(j,i)*(10.0**scale_factor) )
          pntr= pntr+1
          if ( pntr .gt. 128 ) then                                      
            write(2,rec=rec_num,err=999,iostat=error) buffer
            rec_num= rec_num+1
            pntr= 1
          end if
        end do ! j
      end do ! i
      if ( pntr .ne. 1 ) then
        write(2,rec=rec_num,err=999,iostat=error) buffer
        rec_num= rec_num+1
      end if                  
c
c     Uncompact the data so as not to scramble it internally
c
999   if ( CompactData ) then
        do i= size2, 1, -1
          do j= 1, size1
            data(j,StorageOrder(i))= data(j,i)
          end do
        end do
      end if
      return                      
      end
c                             
c
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Write a three dimensional real array to the output file and 
c              return the record number of the next available record to which data 
c              may be written. The process of writing the data out involves computing
c              the scaling factor for the real data, using this scaling factor to
c              convert the real data to integer data, compacting the array if needed,
c              and then writing this out to disk in 512 byte blocks.
c
c     Routines called:
c       find_scale_factor_3d
c
      subroutine Write_3dim_Array( rec_num,data,dim1,dim2,dim3,
     &                             size1,size2,size3,scale_factor,
     &                             CompactData,StorageOrder,error )
      implicit none
            
      integer*4 rec_num,dim1,dim2,dim3,size1,size2,size3,scale_factor,
     &          error,StorageOrder(15)
      real*4    data(dim1,dim2,dim3)
      logical*4 CompactData                                 
                                                                  
      integer*4 i,j,k,pntr,buffer(128)

c
c     Compact the data if necessary
c
      if ( CompactData ) then
        do i= 1, size3
          do j= 1, size2
            do k= 1, size1
              data(k,j,i)= data(k,j,StorageOrder(i))
            end do
          end do
        end do
      end if
c                         
c     Save the data after scaling 
c
      call find_scale_factor_3d( dim1,dim2,dim3,size1,size2,size3,
     &                           data,scale_factor )
      pntr= 1
      do i= 1, size3
        do j= 1, size2       
          do k= 1, size1
            buffer(pntr)= int( data(k,j,i)*(10.0**scale_factor) )
            pntr= pntr+1
            if ( pntr .gt. 128 ) then
              write(2,rec=rec_num,err=999,iostat=error) buffer
              rec_num= rec_num+1
              pntr= 1                
            end if
          end do ! k
        end do ! j
      end do ! i
      if ( pntr .ne. 1 ) then
        write(2,rec=rec_num,err=999,iostat=error) buffer
        rec_num= rec_num+1           
      end if
c
c     Uncompact the data so as not to scramble it internally
c
999   if ( CompactData ) then
        do i= size3, 1, -1
          do j= 1, size2
            do k= 1, size1
              data(k,j,StorageOrder(i))= data(k,j,i)
            end do
          end do
        end do
      end if
      return                      
      end
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Write a four dimensional real array to the output file and 
c              return the record number of the next available record to which data 
c              may be written. The process of writing the data out involves computing
c              the scaling factor for the real data, using this scaling factor to
c              convert the real data to integer data, compacting the array if needed,
c              and then writing this out to disk in 512 byte blocks.
c
c     Routines called:
c       find_scale_factor_4d
c
      subroutine Write_4dim_Array( rec_num,data,dim1,dim2,dim3,dim4,
     &                             size1,size2,size3,size4,scale_factor,
     &                             CompactData,StorageOrder,error )
      implicit none

      integer*4 rec_num,size1,size2,size3,size4,error,dim1,dim2,dim3,dim4,
     &          scale_factor,StorageOrder(15)
      real*4    data(dim1,dim2,dim3,dim4)
      logical*4 CompactData                                 
                   
      integer*4 i,j,k,l,pntr,buffer(128)

c
c     Compact the data if necessary
c
      if ( CompactData ) then
        do i= 1, size4
          do j= 1, size3
            do k= 1, size2
              do l= 1, size1
                data(l,k,j,i)= data(l,k,j,StorageOrder(i))
              end do
            end do
          end do                      
        end do
      end if
c                         
c     Save the data after scaling 
c
      call find_scale_factor_4d( dim1,dim2,dim3,dim4,size1,size2,size3,size4,
     &                           data,scale_factor )
      pntr= 1
      do i= 1, size4
        do j= 1, size3       
          do k= 1, size2
            do l= 1, size1
              buffer(pntr)= int( data(l,k,j,i)*(10.0**scale_factor) )
              pntr= pntr+1
              if ( pntr .gt. 128 ) then
                write(2,rec=rec_num,err=999,iostat=error) buffer
                rec_num= rec_num+1                        
                pntr= 1
              end if
            end do ! l
          end do ! k
        end do ! j                                          
      end do ! i
      if ( pntr .ne. 1 ) then
        write(2,rec=rec_num,err=999,iostat=error) buffer
        rec_num= rec_num+1
      end if                                          
c
c     Uncompact the data so as not to scramble it internally
c
999   if ( CompactData ) then
        do i= size4, 1, -1
          do j= 1, size3
            do k= 1, size2
              do l= 1, size1
                data(l,k,j,StorageOrder(i))= data(l,k,j,i)
              end do
            end do
          end do
        end do
      end if
      return                      
      end       
c                   
c               
c                            
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose:
c     This routine will scan the data in the 1d data array and find an appropriate
c     scaling factor which it can be multiplied by such that when the data
c     is stored as integer precision loss is minimized.  Since the data
c     will be stored in the ANZ files in integer*4 format the maximum value
c     which may be stored is approx. +/-2x10**9.  Thus, the scale_factor will
c     be set such that the max value that the data*scale_factor can attain
c     is 10**8- thus allowing 9 digits of precision to be stored and at
c     the same time not exceeding the ability of the integer*4 representation
c     to store the number.
c
      subroutine find_scale_factor_1d( dim1,size1,data,scale_factor )
      implicit none

      integer*4 dim1,size1,scale_factor
      real*4    data(dim1)

      integer*4 i
      real*4    max,log_value

      max= abs(data(1))
      do i= 1, size1
        if ( abs(data(i)) .gt. max ) max= abs(data(i))
      end do
      if ( max .lt. 1.0e-20 ) then
        log_value= 0
      else
        log_value= alog10(max)
      end if
      if ( log_value .gt. 0.0 ) then ! log is positive
        scale_factor= 7 - int( log_value ) - 1 ! this adjustment just assures
      else ! log is negative                   ! that 10**8 is never exceeded
        scale_factor= 7 - int( log_value ) + 1
      end if
      return
      end
c
c
c                            
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose:
c     This routine will scan the data in the 2d data array and find an appropriate
c     scaling factor which it can be multiplied by such that when the data
c     is stored as integer precision loss is minimized.
c
      subroutine find_scale_factor_2d( dim1,dim2,size1,size2,data,scale_factor )
      implicit none

      integer*4 dim1,dim2,size1,size2,scale_factor
      real*4    data(dim1,dim2)

      integer*4 i,j        
      real*4    max,log_value

      max= abs(data(1,1))
      do i= 1, size2
        do j= 1, size1
          if ( abs(data(j,i)) .gt. max ) max= abs(data(j,i))
        end do
      end do
      if ( max .lt. 1.0e-20 ) then
        log_value= 0
      else
        log_value= alog10(max)
      end if
      if ( log_value .gt. 0.0 ) then ! log is positive
        scale_factor= 7 - int( log_value ) - 1 ! this adjustment just assures
      else ! log is negative                   ! that 10**9 is never exceeded
        scale_factor= 7 - int( log_value ) + 1
      end if
      return
      end   
c
c
c                            
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose:
c     This routine will scan the data in the 3d data array and find an appropriate
c     scaling factor which it can be multiplied by such that when the data
c     is stored as integer precision loss is minimized.
c
      subroutine find_scale_factor_3d( dim1,dim2,dim3,size1,size2,size3,
     &                                 data,scale_factor )
      implicit none

      integer*4 dim1,dim2,dim3,size1,size2,size3,scale_factor
      real*4    data(dim1,dim2,dim3)
 
      integer*4 i,j,k
      real*4    max,log_value

      max= abs(data(1,1,1))
      do i= 1, size3
        do j= 1, size2
          do k= 1, size1
            if ( abs(data(k,j,i)) .gt. max ) max= abs(data(k,j,i))
          end do
        end do
      end do
      if ( max .lt. 1.0e-20 ) then
        log_value= 0
      else
        log_value= alog10(max)
      end if
      if ( log_value .gt. 0.0 ) then ! log is positive
        scale_factor= 7 - int( log_value ) - 1 ! this adjustment just assures
      else ! log is negative                   ! that 10**9 is never exceeded
        scale_factor= 7 - int( log_value ) + 1
      end if
      return
      end   
c
c
c                            
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose:
c     This routine will scan the data in the data array and find an appropriate
c     scaling factor which it can be multiplied by such that when the data
c     is stored as integer precision loss is minimized.
c
      subroutine find_scale_factor_4d( dim1,dim2,dim3,dim4,size1,size2,
     &                                 size3,size4,data,scale_factor )
      implicit none

      integer*4 dim1,dim2,dim3,dim4,size1,size2,size3,size4,scale_factor
      real*4    data(dim1,dim2,dim3,dim4)
 
      integer*4 i,j,k,l
      real*4    max,log_value

      max= abs(data(1,1,1,1))
      do i= 1, size4
        do j= 1, size3
          do k= 1, size2
            do l= 1, size1
              if ( abs(data(l,k,j,i)) .gt. max ) max= abs(data(l,k,j,i))
            end do
          end do
        end do          
      end do
      if ( max .lt. 1.0e-20 ) then
        log_value= 0
      else
        log_value= alog10(max)
      end if
      if ( log_value .gt. 0.0 ) then ! log is positive
        scale_factor= 7 - int( log_value ) - 1 ! this adjustment just assures
      else ! log is negative                   ! that 10**9 is never exceeded
        scale_factor= 7 - int( log_value ) + 1
      end if
      return
      end   
c
c                                    
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Set all the flags indicating whether a motion quantity has been 
c              calculated to false. This makes ANZ assume that no motion calculations
c              have been carried though there may be data sitting in the various
c              arrays. This "left over" data will not be written out since these
c              flags are checked to determine what should be saved.
c
      subroutine zero_motion_flags( MF,SF,JF,AF,CF )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'

      record /marker_flag/    MF
      record /segment_flag/   SF
      record /avg_pos_flag/   AF
      record /joint_flag/     JF
      record /cycle_flag/     CF

      MF.read=         .false.
      MF.interpolated= .false.
      MF.adjusted=     .false.
      MF.smoothed=     .false.
      MF.diagnostics=  .false.
      MF.VelAcc=       .false.

c      SF.param=        .false.
      SF.position=     .false.
      SF.vel_acc=      .false.    
      SF.energy=       .false.
      SF.FiniteScrew=  .false.
      SF.InstantScrew= .false.
      SF.cntr_mass=    .false.
      SF.momentum=     .false.
      SF.angle=        .false.
      SF.RefCorrect=   .false.
      SF.BCMVel=       .false.
c      SF.RightSideAlign= .false.
c      SF.LeftSideAlign=  .false.

      JF.angle_JCS=   .false.
      JF.vel_acc_JCS= .false.
      JF.angle_EUL=   .false.
      JF.vel_acc_EUL= .false.
      JF.load=        .false.
c      JF.leg_load=    .false.
      JF.wrench=      .false.
      JF.power=       .false.
      JF.FiniteScrew=  .false.
      JF.InstantScrew= .false.
      JF.legsummed=   .false.
      JF.TRU_angle=   .false.
      JF.Translation= .false.
      JF.ScrewAngle=  .false.
      JF.MomentArm=   .false.
      JF.WrenchArm=   .false.
      JF.RefCorrectAng= .false.
      
c      AF.RefCorrectAng= .false.

      CF.event=      .false.
      CF.statistics= .false.
      
      GP.last_updated=  'NEVER    '      
      return
      end        
c
c
c
c
c     Copyright by Dwight Meglan 1990
c
c     Purpose: Set all the flags indicating whether a force/EMG quantity has been 
c              calculated to false. This makes ANZ assume that no force calculations
c              have been carried though there may be data sitting in the various
c              arrays. This "left over" data will not be written out since these
c              flags are checked to determine what should be saved.
c
      subroutine zero_force_flags( FF,EF )
      implicit none

      include 'a_struct.inc'

      record /force_flag/ FF
      record /emg_flag/   EF
      
      integer*4 i,j

      FF.read=          .false.
      FF.gls2_file=     .false.
      FF.filtered=      .false.
      FF.adjusted=      .false.
      FF.aligned=       .false.
      FF.individual_feet= .false.
      FF.cpr_GCS=       .false.
      FF.cpr_LCS=       .false.
      FF.StrIdx=  .false.
      FF.wrench=        .false.
      FF.Integral=      .false.
      FF.reconstructed= .false.
      FF.DataClipped=   .false.
      FF.PitchMomArm=   .false.
      FF.PitchWreArm=   .false.
      FF.CprVelGCS=     .false.
      do i= 1, 40
        FF.FiltOp(i:i)= ' '
      end do

      EF.read=          .false.
      EF.filtered=      .false.
      EF.rectified=     .false.
      EF.integrated=    .false.
      EF.averaged=      .false.
      EF.NumProcOps=    0
      do i= 1, 5
        do j= 1, 40
          EF.ProcOp(i)(j:j)= ' '
        end do
      end do     
      EF.Maximum= .false.
      EF.Normalized= .false.
      return
      end
 
c
c
c
      function InttoLog( IntFlag )
      implicit none

      logical*4 InttoLog
      integer*4 IntFlag

      InttoLog= .false.
      if ( IntFlag .eq. 1 ) InttoLog= .true.	
      return
      end

c
c       Interface subroutine for Watcom Fortran
c
	subroutine MakeDate( Day )
	implicit none

	character*9 Day

	integer*2   date,month,year
	character*3 cmonth(12)

	data cmonth /'JAN','FEB','MAR','APR','MAY','JUN',
     &               'JUL','AUG','SEP','OCT','NOV','DEC' /

	call getdat(year,month,date)
	if ( year .gt. 1900 ) year= year - 1900
	write(Day,100) date,cmonth(month),year
100	format(i2,'-',a3,'-',i2)
	return
	end

c
c
c
c
      subroutine zero_all_motion_params( ZeroSegParam )
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'

      logical*4 ZeroSegParam

      integer*4 i,j,k
      character blank20*20,blank40*40    
      
      do i= 1, 20
         blank20(i:i)= ' '
      end do
      do i= 1, 40
         blank40(i:i)= ' '
      end do

      GP.MtnFile= blank40
      GP.AvgFile= blank40
      GP.FrcFile= blank40
      GP.last_updated= 'NEVER    '
      GP.name= blank40
      GP.comment= blank40//blank40
      GP.age= 0
      GP.weight= 0.0
      GP.height= 0.0
      GP.num_seg= 0
      do i= 1, MAX_SEG
         GP.seg_num(i)= 0
      end do
      GP.num_jnt= 0
      do i= 1, MAX_JNT
         GP.jnt_num(i)= 0
      end do
      GP.single_cycle=  .false.
      GP.normalized=    .false.
      GP.seg_unrotated= .false.
      GP.seg_smooth=    .false.
      GP.int_poly_order= 3
      GP.analysis_type=  0
      GP.ByteOrderCheck= 1
      GP.Sex= 'M'
      GP.FileFormatVersion= 'e'

      MP.nfrm= 0
      MP.frm_rate= 0.0
      MP.num_cycle= 0
      do i= 1, 5
         MP.cycle_first_frm(i)= 0
         MP.cycle_last_frm(i)= 0
         MP.cycle_length(i)= 0
         MP.rhs_frm(i)= 0
         MP.lhs_frm(i)= 0
         MP.rto_frm(i)= 0
         MP.lto_frm(i)= 0
      end do
      MP.num_rhs= 0
      MP.num_lhs= 0
      MP.num_rto= 0
      MP.num_lto= 0
      MP.first_ft= 0
      MP.num_mrk= 0
      MP.first_frm= 0
      do i= 1, 40
         MP.mrk_name(i)= '    '
      end do

      if ( ZeroSegParam ) then
         do i= 1, MAX_SEG
            SP(i).length= 0.0
            do j= 1, 3
               SP(i).avg_cntr(j)= 0.0
               SP(i).inertia(j)= 0.0
               do k= 1, 8
                  SP(i).shape(j,k)= 0.0
               end do
               do k= 1, 4
                  SP(i).end(j,k)= 0.0
               end do
            end do
            SP(i).mass= 0.0
            SP(i).num_end= 0
            SP(i).num_mrk= 0
            do j= 1, 5
               SP(i).mrk_num(j)= 0
            end do
            SP(i).num_MrkDist= 0
            do j= 1, 2
               do k= 1, 10
                  SP(i).dist_mrk_num(j,k)= 0
               end do
            end do
            do j= 1, 3
               do k= 1, 4
                  SP(i).TCStoLCS(j,k)= 0.0
               end do
               do k= 1, 5
                  SP(i).mpnLCS(j,k)= 0.0
               end do
            end do
            SP(i).nmTCS= 0
            do j= 1, 5
               SP(i).mnTCS(j)= 0
            end do
         end do
      end if

      CP.first_side= 0
      CP.stride_length= 0.0
      do i= 1, 2
         CP.step_length(i)=   0.0
         CP.sgl_time(i)=      0.0
         CP.sgl_percent(i)=   0.0
         CP.dbl_time(i)=      0.0
         CP.dbl_percent(i)=   0.0
         CP.total_time(i)=    0.0
         CP.total_percent(i)= 0.0
         CP.to_percent(i)=    0.0
         CP.hs_percent(i)=    0.0
         CP.NormStepLength(i)= 0.0
         CP.PctNormStepLength(i)= 0.0
         CP.NormSglPct(i)=    0.0
         CP.NormDblPct(i)=    0.0
         CP.NormTotalPct(i)=  0.0
         CP.NormHsPct(i)=     0.0
         CP.NormToPct(i)=     0.0
      end do
      CP.step_width= 0.0
      CP.cycle_time=   0.0
      CP.cadence=      0.0
      CP.velocity=     0.0
      CP.hs_percent(3)=       0.0
      CP.NormStrideLength=    0.0
      CP.NormStepWidth=       0.0
      CP.NormCycleTime=       0.0
      CP.NormCadence=         0.0
      CP.NormVelocity=        0.0
      CP.PctNormStrideLength= 0.0
      CP.PctNormStepWidth=    0.0
      CP.PctNormCycleTime=    0.0
      CP.PctNormCadence=      0.0
      CP.PctNormVelocity=     0.0
      CP.NormHsPct(3)=        0.0

      return
      end

c
c
c
c
      subroutine zero_all_force_params
      implicit none

      include 'a_const.inc'
      include 'a_struct.inc'
      include 'a_prm.inc'


      integer*4 i,j,k
      character blank20*20,blank40*40
      
      do i= 1, 20
         blank20(i:i)= ' '
      end do
      do i= 1, 40
         blank40(i:i)= ' '
      end do

      FP.nfrm= 0
      FP.frm_rate= 0
      FP.av_frm_ratio= 0.0
      FP.frm_offset= 0
      FP.last_frm= 0
      FP.first_frm= 0
      FP.num_plt= 0
      do j= 1, 4
         do i= 1, 2
            FP.plt_size(i,j)= 0.0
         end do
         do i= 1, 3
            do k= 1, 4
               FP.plt_loc(j).LCS(i,k)= 0.0
            end do
         end do
         FP.rtft_plt(j)= 0
         FP.lfft_plt(j)= 0
      end do
      FP.num_rtft_plt= 0
      FP.num_lfft_plt= 0
      FP.dbl_stance_ok= .false.
      FP.FirstFoot= 0
      FP.Direction= 0

      EP.NumChnl= 0
      EP.NumSamples= 0
      EP.SampleRate= 0
      do i= 1, 16
         EP.CodeNum(i)= 0
         EP.Name(i)= blank20
         EP.Surface(i)= .false.
      end do
      EP.Filter= blank40

      return
      end
      
