! Adds an ACC column to a Type 5000 state vector that previously only had POS
! and VEL. This assumes the file has 9+ entries and that POS and VEL are the
! only column to keep from the initial file. Basically it is looking for a
! file that looks like nxm.sys.dat.sv1.prm.
!
! @author  J. Czechowski IV
! @version $Id: add_acc.mm,v 1.2 2013/03/13 01:44:38 jxc Exp $
! @since   NeXtMidas 3.3.2
startmacro FN:inFile FN:outFile
  res subrecs (POS/VD/CARTESIAN/DISTANCE_M,&
               VEL/VD/CARTESIAN/SPEED_M_S,&
               ACC/VD/CARTESIAN/ACCELERATION_M_S2)

  file copy/l/f inFile outFile{COMPONENTS=^{subrecs}} /PROPAGATE=FALSE /PROPMASK=ALL

  file open/w f outFile
    invoke dt f.getXDelta()

    ! ---- MIDDLE ENTRIES ------------------------------------------------------
    do i 1 f.size-2
      invoke rowIm1 f.getDataTable(^{i}-1)        ! row[i-1]
      invoke rowI   f.getDataTable(^{i})          ! row[ i ]
      invoke rowIp1 f.getDataTable(^{i}+1)        ! row[i+1]
      
      
      calc accImH   rowI.VEL   rowIm1.VEL - dt /  ! acc[i - 1/2]
      calc accIpH   rowIp1.VEL rowI.VEL   - dt /  ! acc[i + 1/2]
      calc rowI.ACC accImH accIpH + 2 /           ! acc[   i   ]
      
      invoke ,, f.setData(^{i},rowI)
    enddo

    ! ---- FIRST/LAST ENTRIES --------------------------------------------------
    invoke row0   f.getDataTable(0)               ! row[ 0 ]
    invoke row0p1 f.getDataTable(1)               ! row[0+1]
    invoke row0p2 f.getDataTable(2)               ! row[0+2]
    
    invoke rowNm2 f.getDataTable(^{f.size}-3)     ! row[N-2]
    invoke rowNm1 f.getDataTable(^{f.size}-2)     ! row[N-1]
    invoke rowN   f.getDataTable(^{f.size}-1)     ! row[ N ]
    
    calc row0.ACC row0p1.ACC 2 * row0p2.ACC -     ! acc0 = acc1-delta = acc1-(acc2-acc1)
    calc rowN.ACC rowNm1.ACC 2 * rowNm2.ACC -     ! accN
    
    invoke ,, f.setData(0,row0)
    invoke ,, f.setData(^{f.size}-1,rowN)
  file close f
  key outFile PUT COMMENT "A copy of ^{inFile} with ACC column generated by &
                           nxm.sys.test.add_acc.mm."
endmacro
