! ROSCO IO
! This file is automatically generated by write_registry.py using ROSCO v2.10.1
! For any modification to the registry, please edit the rosco_types.yaml accordingly
 
MODULE ROSCO_IO
    USE, INTRINSIC :: ISO_C_Binding
    USE ROSCO_Types
    USE ReadSetParameters
    USE Constants
IMPLICIT NONE

CONTAINS

SUBROUTINE WriteRestartFile(LocalVar, CntrPar, ErrVar, objInst, RootName, size_avcOUTNAME)
    TYPE(LocalVariables), INTENT(IN)                :: LocalVar
    TYPE(ControlParameters), INTENT(INOUT)          :: CntrPar
    TYPE(ObjectInstances), INTENT(INOUT)            :: objInst
    TYPE(ErrorVariables), INTENT(INOUT)             :: ErrVar
    INTEGER(IntKi), INTENT(IN)                      :: size_avcOUTNAME
    CHARACTER(size_avcOUTNAME), INTENT(IN)        :: RootName 
    
    INTEGER(IntKi)               :: Un                  ! I/O unit for pack/unpack (checkpoint & restart)
    INTEGER(IntKi)               :: I                   ! Generic index.
    CHARACTER(128)               :: InFile              ! Input checkpoint file
    INTEGER(IntKi)               :: ErrStat
    CHARACTER(128)               :: ErrMsg              
    CHARACTER(128)               :: n_t_global          ! timestep number as a string

    WRITE(n_t_global, '(I0.0)' ) NINT(LocalVar%Time/LocalVar%DT)
    InFile = TRIM(RootName)//TRIM( n_t_global )//'.RO.chkp'
    CALL GetNewUnit(Un, ErrVar)
    OPEN(unit=Un, FILE=TRIM(InFile), STATUS='UNKNOWN', FORM='UNFORMATTED' , ACCESS='STREAM', IOSTAT=ErrStat, ACTION='WRITE' )

    IF ( ErrStat /= 0 ) THEN
        ErrVar%aviFAIL = 1
        ErrVar%ErrMsg  = 'ROSCO_IO: Cannot open checkpoint file '//TRIM(InFile)//' for writing'

    ELSE
        WRITE( Un, IOSTAT=ErrStat) LocalVar%iStatus
        WRITE( Un, IOSTAT=ErrStat) LocalVar%AlreadyInitialized
        WRITE( Un, IOSTAT=ErrStat) LocalVar%RestartWSE
        WRITE( Un, IOSTAT=ErrStat) LocalVar%Time
        WRITE( Un, IOSTAT=ErrStat) LocalVar%DT
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WriteThisStep
        WRITE( Un, IOSTAT=ErrStat) LocalVar%n_DT
        WRITE( Un, IOSTAT=ErrStat) LocalVar%Time_Last
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_GenPwr
        WRITE( Un, IOSTAT=ErrStat) LocalVar%GenSpeed
        WRITE( Un, IOSTAT=ErrStat) LocalVar%RotSpeed
        WRITE( Un, IOSTAT=ErrStat) LocalVar%NacHeading
        WRITE( Un, IOSTAT=ErrStat) LocalVar%NacVane
        WRITE( Un, IOSTAT=ErrStat) LocalVar%NacVaneF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WindDir
        WRITE( Un, IOSTAT=ErrStat) LocalVar%HorWindV
        WRITE( Un, IOSTAT=ErrStat) LocalVar%HorWindV_F
        WRITE( Un, IOSTAT=ErrStat) LocalVar%rootMOOP(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%rootMOOP(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%rootMOOP(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%rootMOOPF(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%rootMOOPF(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%rootMOOPF(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%BlPitch(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%BlPitch(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%BlPitch(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%BlPitchCMeas
        WRITE( Un, IOSTAT=ErrStat) LocalVar%Azimuth
        WRITE( Un, IOSTAT=ErrStat) LocalVar%OL_Azimuth
        WRITE( Un, IOSTAT=ErrStat) LocalVar%AzUnwrapped
        WRITE( Un, IOSTAT=ErrStat) LocalVar%AzError
        WRITE( Un, IOSTAT=ErrStat) LocalVar%GenTqAz
        WRITE( Un, IOSTAT=ErrStat) LocalVar%AzBuffer(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%AzBuffer(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%NumBl
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_Acc_TT
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SS_Acc_TT
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_Acc_Nac
        WRITE( Un, IOSTAT=ErrStat) LocalVar%NacIMU_FA_RAcc
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_AccHPF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_AccHPFI
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_PitCom(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_PitCom(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_PitCom(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpd
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpd_TSR
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpd_TRA
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpd_RL
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_RefSpd
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_RefSpd_SS
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_RefSpd_PRC
        WRITE( Un, IOSTAT=ErrStat) LocalVar%RotSpeedF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%GenSpeedF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%GenTq
        WRITE( Un, IOSTAT=ErrStat) LocalVar%GenTqMeas
        WRITE( Un, IOSTAT=ErrStat) LocalVar%GenArTq
        WRITE( Un, IOSTAT=ErrStat) LocalVar%GenBrTq
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_KOmega2_GenTq
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_ConstPwr_GenTq
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_PitComF(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_PitComF(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_PitComF(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_KP
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_KI
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_KD
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_TF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_MaxPit
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_MinPit
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT_Last
        WRITE( Un, IOSTAT=ErrStat) LocalVar%BlPitchCMeasF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT_IPC(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT_IPC(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT_IPC(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_PwrErr
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_SpdErr
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_AxisTilt_1P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_AxisYaw_1P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_AxisTilt_2P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_AxisYaw_2P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%axisTilt_1P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%axisYaw_1P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%axisYawF_1P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%axisTilt_2P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%axisYaw_2P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%axisYawF_2P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_KI(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_KI(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_KP(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_KP(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%IPC_IntSat
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PC_State
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitCom(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitCom(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitCom(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitCom_SD(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitCom_SD(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitCom_SD(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitComAct(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitComAct(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PitComAct(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SS_DelOmegaF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%TestType
        WRITE( Un, IOSTAT=ErrStat) LocalVar%Kp_Float
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_MaxTq
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_LastGenTrq
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_LastGenPwr
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_MechGenPwr
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_SpdErrAr
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_SpdErrBr
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_SpdErr
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_State
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_Rgn3Pitch
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE_Vw
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE_Vw_F
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE_VwI
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE_VwIdot
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE_Op
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE_Op_Last
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_LastGenTrqF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PRC_WSE_F
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PRC_R_Speed
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PRC_R_Torque
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PRC_R_Pitch
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PRC_R_Total
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PRC_Min_Pitch
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PS_Min_Pitch
        WRITE( Un, IOSTAT=ErrStat) LocalVar%OL_Index
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SU_Stage
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SU_LoadStageStartTime
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SU_RotSpeedF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SD_Trigger
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SD_BlPitchF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SD_NacVaneF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SD_GenSpeedF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SD_Stage
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SD_StageStartTime
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SD_MaxPitchRate
        WRITE( Un, IOSTAT=ErrStat) LocalVar%SD_MaxTorqueRate
        WRITE( Un, IOSTAT=ErrStat) LocalVar%GenTq_SD
        WRITE( Un, IOSTAT=ErrStat) LocalVar%Fl_PitCom
        WRITE( Un, IOSTAT=ErrStat) LocalVar%NACIMU_FA_AccF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_AccF
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FA_Hist
        WRITE( Un, IOSTAT=ErrStat) LocalVar%TRA_LastRefSpd
        WRITE( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpeed
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTDX
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTDY
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTDZ
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRDX
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRDY
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRDZ
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTVX
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTVY
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTVZ
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRVX
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRVY
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRVZ
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTAX
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTAY
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmTAZ
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRAX
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRAY
        WRITE( Un, IOSTAT=ErrStat) LocalVar%PtfmRAZ
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(4)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(5)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(6)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(7)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(8)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(9)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(10)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(11)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(12)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(4)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(5)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(6)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(7)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(8)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(9)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(10)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(11)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(12)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(4)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(5)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(6)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(7)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(8)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(9)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(10)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(11)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(12)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(4)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(5)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(6)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(7)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(8)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(9)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(10)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(11)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%StC_Input(12)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%Flp_Angle(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%Flp_Angle(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%Flp_Angle(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%RootMyb_Last(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%RootMyb_Last(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%RootMyb_Last(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ACC_INFILE_SIZE
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ACC_INFILE
        WRITE( Un, IOSTAT=ErrStat) LocalVar%restart
        WRITE( Un, IOSTAT=ErrStat) LocalVar%AWC_complexangle(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%AWC_complexangle(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%AWC_complexangle(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%TiltMean
        WRITE( Un, IOSTAT=ErrStat) LocalVar%YawMean
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_ID
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_YawOffset
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_TorqueOffset
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_PitOffset(1)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_PitOffset(2)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_PitOffset(3)
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_R_Speed
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_R_Torque
        WRITE( Un, IOSTAT=ErrStat) LocalVar%ZMQ_R_Pitch
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE%om_r
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE%v_t
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE%v_m
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE%v_h
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE%P
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE%xh
        WRITE( Un, IOSTAT=ErrStat) LocalVar%WE%K
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_a1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_a0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_b1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_b0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_InputSignalLast
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_OutputSignalLast
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_a2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_a1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_a0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_b2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_b1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_b0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_InputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_OutputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_InputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_OutputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_a2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_a1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_a0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_b2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_b1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_b0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_InputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_OutputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_InputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_OutputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%hpf_InputSignalLast
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%hpf_OutputSignalLast
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_OutputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_OutputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_InputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_InputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_b2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_b0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_a2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_a1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_a0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_OutputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_OutputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_InputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_InputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_b2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_b1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_b0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_a1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%FP%nf_a0
        WRITE( Un, IOSTAT=ErrStat) LocalVar%piP%ITerm
        WRITE( Un, IOSTAT=ErrStat) LocalVar%piP%ITermLast
        WRITE( Un, IOSTAT=ErrStat) LocalVar%piP%ITerm2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%piP%ITermLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%piP%ELast
        WRITE( Un, IOSTAT=ErrStat) LocalVar%resP%res_OutputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%resP%res_OutputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%resP%res_InputSignalLast1
        WRITE( Un, IOSTAT=ErrStat) LocalVar%resP%res_InputSignalLast2
        WRITE( Un, IOSTAT=ErrStat) LocalVar%rlP%LastSignal
        WRITE( Un, IOSTAT=ErrStat) objInst%instLPF
        WRITE( Un, IOSTAT=ErrStat) objInst%instSecLPF
        WRITE( Un, IOSTAT=ErrStat) objInst%instSecLPFV
        WRITE( Un, IOSTAT=ErrStat) objInst%instHPF
        WRITE( Un, IOSTAT=ErrStat) objInst%instNotchSlopes
        WRITE( Un, IOSTAT=ErrStat) objInst%instNotch
        WRITE( Un, IOSTAT=ErrStat) objInst%instPI
        WRITE( Un, IOSTAT=ErrStat) objInst%instRes
        WRITE( Un, IOSTAT=ErrStat) objInst%instRL
        Close ( Un )
        IF ( ErrStat /= 0 ) THEN
            ErrVar%aviFAIL = 1
            ErrVar%ErrMsg  = 'ROSCO_IO: Error writing checkpoint file.'
        ENDIF
    ENDIF
END SUBROUTINE WriteRestartFile

 
SUBROUTINE ReadRestartFile(avrSWAP, LocalVar, CntrPar, objInst, PerfData, RootName, size_avcOUTNAME, ErrVar)
    TYPE(LocalVariables), INTENT(INOUT)             :: LocalVar
    TYPE(ControlParameters), INTENT(INOUT)          :: CntrPar
    TYPE(ObjectInstances), INTENT(INOUT)            :: objInst
    TYPE(PerformanceData), INTENT(INOUT)            :: PerfData
    TYPE(ErrorVariables), INTENT(INOUT)             :: ErrVar
    REAL(ReKi), INTENT(IN)                          :: avrSWAP(*)
    INTEGER(IntKi), INTENT(IN)                      :: size_avcOUTNAME
    CHARACTER(size_avcOUTNAME), INTENT(IN)        :: RootName 
    
    INTEGER(IntKi)               :: Un                  ! I/O unit for pack/unpack (checkpoint & restart)
    INTEGER(IntKi)               :: I                   ! Generic index.
    CHARACTER(128)               :: InFile              ! Input checkpoint file
    INTEGER(IntKi)               :: ErrStat
    CHARACTER(128)               :: ErrMsg              
    CHARACTER(128)               :: n_t_global          ! timestep number as a string

    WRITE(n_t_global, '(I0.0)' ) NINT(avrSWAP(2)/avrSWAP(3))
    InFile = TRIM(RootName)//TRIM( n_t_global )//'.RO.chkp'
    CALL GetNewUnit(Un, ErrVar)
    OPEN(unit=Un, FILE=TRIM(InFile), STATUS='UNKNOWN', FORM='UNFORMATTED' , ACCESS='STREAM', IOSTAT=ErrStat, ACTION='READ' )

    IF ( ErrStat /= 0 ) THEN
        ErrVar%aviFAIL = 1
        ErrVar%ErrMsg  = 'ROSCO_IO: Cannot open checkpoint file '//TRIM(InFile)//' for reading'

    ELSE
        READ( Un, IOSTAT=ErrStat) LocalVar%iStatus
        READ( Un, IOSTAT=ErrStat) LocalVar%AlreadyInitialized
        READ( Un, IOSTAT=ErrStat) LocalVar%RestartWSE
        READ( Un, IOSTAT=ErrStat) LocalVar%Time
        READ( Un, IOSTAT=ErrStat) LocalVar%DT
        READ( Un, IOSTAT=ErrStat) LocalVar%WriteThisStep
        READ( Un, IOSTAT=ErrStat) LocalVar%n_DT
        READ( Un, IOSTAT=ErrStat) LocalVar%Time_Last
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_GenPwr
        READ( Un, IOSTAT=ErrStat) LocalVar%GenSpeed
        READ( Un, IOSTAT=ErrStat) LocalVar%RotSpeed
        READ( Un, IOSTAT=ErrStat) LocalVar%NacHeading
        READ( Un, IOSTAT=ErrStat) LocalVar%NacVane
        READ( Un, IOSTAT=ErrStat) LocalVar%NacVaneF
        READ( Un, IOSTAT=ErrStat) LocalVar%WindDir
        READ( Un, IOSTAT=ErrStat) LocalVar%HorWindV
        READ( Un, IOSTAT=ErrStat) LocalVar%HorWindV_F
        READ( Un, IOSTAT=ErrStat) LocalVar%rootMOOP(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%rootMOOP(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%rootMOOP(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%rootMOOPF(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%rootMOOPF(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%rootMOOPF(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%BlPitch(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%BlPitch(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%BlPitch(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%BlPitchCMeas
        READ( Un, IOSTAT=ErrStat) LocalVar%Azimuth
        READ( Un, IOSTAT=ErrStat) LocalVar%OL_Azimuth
        READ( Un, IOSTAT=ErrStat) LocalVar%AzUnwrapped
        READ( Un, IOSTAT=ErrStat) LocalVar%AzError
        READ( Un, IOSTAT=ErrStat) LocalVar%GenTqAz
        READ( Un, IOSTAT=ErrStat) LocalVar%AzBuffer(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%AzBuffer(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%NumBl
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_Acc_TT
        READ( Un, IOSTAT=ErrStat) LocalVar%SS_Acc_TT
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_Acc_Nac
        READ( Un, IOSTAT=ErrStat) LocalVar%NacIMU_FA_RAcc
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_AccHPF
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_AccHPFI
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_PitCom(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_PitCom(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_PitCom(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpd
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpd_TSR
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpd_TRA
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpd_RL
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_RefSpd
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_RefSpd_SS
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_RefSpd_PRC
        READ( Un, IOSTAT=ErrStat) LocalVar%RotSpeedF
        READ( Un, IOSTAT=ErrStat) LocalVar%GenSpeedF
        READ( Un, IOSTAT=ErrStat) LocalVar%GenTq
        READ( Un, IOSTAT=ErrStat) LocalVar%GenTqMeas
        READ( Un, IOSTAT=ErrStat) LocalVar%GenArTq
        READ( Un, IOSTAT=ErrStat) LocalVar%GenBrTq
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_KOmega2_GenTq
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_ConstPwr_GenTq
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_PitComF(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_PitComF(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_PitComF(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_KP
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_KI
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_KD
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_TF
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_MaxPit
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_MinPit
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT_Last
        READ( Un, IOSTAT=ErrStat) LocalVar%BlPitchCMeasF
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT_IPC(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT_IPC(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_PitComT_IPC(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_PwrErr
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_SpdErr
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_AxisTilt_1P
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_AxisYaw_1P
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_AxisTilt_2P
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_AxisYaw_2P
        READ( Un, IOSTAT=ErrStat) LocalVar%axisTilt_1P
        READ( Un, IOSTAT=ErrStat) LocalVar%axisYaw_1P
        READ( Un, IOSTAT=ErrStat) LocalVar%axisYawF_1P
        READ( Un, IOSTAT=ErrStat) LocalVar%axisTilt_2P
        READ( Un, IOSTAT=ErrStat) LocalVar%axisYaw_2P
        READ( Un, IOSTAT=ErrStat) LocalVar%axisYawF_2P
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_KI(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_KI(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_KP(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_KP(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%IPC_IntSat
        READ( Un, IOSTAT=ErrStat) LocalVar%PC_State
        READ( Un, IOSTAT=ErrStat) LocalVar%PitCom(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%PitCom(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%PitCom(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%PitCom_SD(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%PitCom_SD(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%PitCom_SD(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%PitComAct(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%PitComAct(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%PitComAct(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%SS_DelOmegaF
        READ( Un, IOSTAT=ErrStat) LocalVar%TestType
        READ( Un, IOSTAT=ErrStat) LocalVar%Kp_Float
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_MaxTq
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_LastGenTrq
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_LastGenPwr
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_MechGenPwr
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_SpdErrAr
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_SpdErrBr
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_SpdErr
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_State
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_Rgn3Pitch
        READ( Un, IOSTAT=ErrStat) LocalVar%WE_Vw
        READ( Un, IOSTAT=ErrStat) LocalVar%WE_Vw_F
        READ( Un, IOSTAT=ErrStat) LocalVar%WE_VwI
        READ( Un, IOSTAT=ErrStat) LocalVar%WE_VwIdot
        READ( Un, IOSTAT=ErrStat) LocalVar%WE_Op
        READ( Un, IOSTAT=ErrStat) LocalVar%WE_Op_Last
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_LastGenTrqF
        READ( Un, IOSTAT=ErrStat) LocalVar%PRC_WSE_F
        READ( Un, IOSTAT=ErrStat) LocalVar%PRC_R_Speed
        READ( Un, IOSTAT=ErrStat) LocalVar%PRC_R_Torque
        READ( Un, IOSTAT=ErrStat) LocalVar%PRC_R_Pitch
        READ( Un, IOSTAT=ErrStat) LocalVar%PRC_R_Total
        READ( Un, IOSTAT=ErrStat) LocalVar%PRC_Min_Pitch
        READ( Un, IOSTAT=ErrStat) LocalVar%PS_Min_Pitch
        READ( Un, IOSTAT=ErrStat) LocalVar%OL_Index
        READ( Un, IOSTAT=ErrStat) LocalVar%SU_Stage
        READ( Un, IOSTAT=ErrStat) LocalVar%SU_LoadStageStartTime
        READ( Un, IOSTAT=ErrStat) LocalVar%SU_RotSpeedF
        READ( Un, IOSTAT=ErrStat) LocalVar%SD_Trigger
        READ( Un, IOSTAT=ErrStat) LocalVar%SD_BlPitchF
        READ( Un, IOSTAT=ErrStat) LocalVar%SD_NacVaneF
        READ( Un, IOSTAT=ErrStat) LocalVar%SD_GenSpeedF
        READ( Un, IOSTAT=ErrStat) LocalVar%SD_Stage
        READ( Un, IOSTAT=ErrStat) LocalVar%SD_StageStartTime
        READ( Un, IOSTAT=ErrStat) LocalVar%SD_MaxPitchRate
        READ( Un, IOSTAT=ErrStat) LocalVar%SD_MaxTorqueRate
        READ( Un, IOSTAT=ErrStat) LocalVar%GenTq_SD
        READ( Un, IOSTAT=ErrStat) LocalVar%Fl_PitCom
        READ( Un, IOSTAT=ErrStat) LocalVar%NACIMU_FA_AccF
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_AccF
        READ( Un, IOSTAT=ErrStat) LocalVar%FA_Hist
        READ( Un, IOSTAT=ErrStat) LocalVar%TRA_LastRefSpd
        READ( Un, IOSTAT=ErrStat) LocalVar%VS_RefSpeed
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTDX
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTDY
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTDZ
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRDX
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRDY
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRDZ
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTVX
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTVY
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTVZ
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRVX
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRVY
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRVZ
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTAX
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTAY
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmTAZ
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRAX
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRAY
        READ( Un, IOSTAT=ErrStat) LocalVar%PtfmRAZ
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(4)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(5)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(6)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(7)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(8)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(9)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(10)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(11)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_DesiredL(12)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(4)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(5)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(6)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(7)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(8)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(9)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(10)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(11)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedL(12)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(4)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(5)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(6)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(7)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(8)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(9)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(10)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(11)
        READ( Un, IOSTAT=ErrStat) LocalVar%CC_ActuatedDL(12)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(4)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(5)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(6)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(7)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(8)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(9)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(10)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(11)
        READ( Un, IOSTAT=ErrStat) LocalVar%StC_Input(12)
        READ( Un, IOSTAT=ErrStat) LocalVar%Flp_Angle(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%Flp_Angle(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%Flp_Angle(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%RootMyb_Last(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%RootMyb_Last(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%RootMyb_Last(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%ACC_INFILE_SIZE
        ALLOCATE(LocalVar%ACC_INFILE(LocalVar%ACC_INFILE_SIZE))
        READ( Un, IOSTAT=ErrStat) LocalVar%ACC_INFILE
        READ( Un, IOSTAT=ErrStat) LocalVar%restart
        READ( Un, IOSTAT=ErrStat) LocalVar%AWC_complexangle(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%AWC_complexangle(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%AWC_complexangle(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%TiltMean
        READ( Un, IOSTAT=ErrStat) LocalVar%YawMean
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_ID
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_YawOffset
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_TorqueOffset
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_PitOffset(1)
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_PitOffset(2)
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_PitOffset(3)
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_R_Speed
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_R_Torque
        READ( Un, IOSTAT=ErrStat) LocalVar%ZMQ_R_Pitch
        READ( Un, IOSTAT=ErrStat) LocalVar%WE%om_r
        READ( Un, IOSTAT=ErrStat) LocalVar%WE%v_t
        READ( Un, IOSTAT=ErrStat) LocalVar%WE%v_m
        READ( Un, IOSTAT=ErrStat) LocalVar%WE%v_h
        READ( Un, IOSTAT=ErrStat) LocalVar%WE%P
        READ( Un, IOSTAT=ErrStat) LocalVar%WE%xh
        READ( Un, IOSTAT=ErrStat) LocalVar%WE%K
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_a1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_a0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_b1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_b0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_InputSignalLast
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf1_OutputSignalLast
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_a2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_a1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_a0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_b2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_b1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_b0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_InputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_OutputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_InputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpf2_OutputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_a2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_a1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_a0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_b2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_b1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_b0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_InputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_OutputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_InputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%lpfV_OutputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%hpf_InputSignalLast
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%hpf_OutputSignalLast
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_OutputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_OutputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_InputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_InputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_b2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_b0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_a2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_a1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nfs_a0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_OutputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_OutputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_InputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_InputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_b2
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_b1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_b0
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_a1
        READ( Un, IOSTAT=ErrStat) LocalVar%FP%nf_a0
        READ( Un, IOSTAT=ErrStat) LocalVar%piP%ITerm
        READ( Un, IOSTAT=ErrStat) LocalVar%piP%ITermLast
        READ( Un, IOSTAT=ErrStat) LocalVar%piP%ITerm2
        READ( Un, IOSTAT=ErrStat) LocalVar%piP%ITermLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%piP%ELast
        READ( Un, IOSTAT=ErrStat) LocalVar%resP%res_OutputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%resP%res_OutputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%resP%res_InputSignalLast1
        READ( Un, IOSTAT=ErrStat) LocalVar%resP%res_InputSignalLast2
        READ( Un, IOSTAT=ErrStat) LocalVar%rlP%LastSignal
        READ( Un, IOSTAT=ErrStat) objInst%instLPF
        READ( Un, IOSTAT=ErrStat) objInst%instSecLPF
        READ( Un, IOSTAT=ErrStat) objInst%instSecLPFV
        READ( Un, IOSTAT=ErrStat) objInst%instHPF
        READ( Un, IOSTAT=ErrStat) objInst%instNotchSlopes
        READ( Un, IOSTAT=ErrStat) objInst%instNotch
        READ( Un, IOSTAT=ErrStat) objInst%instPI
        READ( Un, IOSTAT=ErrStat) objInst%instRes
        READ( Un, IOSTAT=ErrStat) objInst%instRL
        Close ( Un )
        IF ( ErrStat /= 0 ) THEN
            ErrVar%aviFAIL = 1
            ErrVar%ErrMsg  = 'ROSCO_IO: Error reading checkpoint file.'
        ENDIF
    ENDIF
    ! Read Parameter files
    CALL ReadControlParameterFileSub(CntrPar, LocalVar, LocalVar%ACC_INFILE, LocalVar%ACC_INFILE_SIZE, RootName, ErrVar)
    IF (CntrPar%WE_Mode > 0) THEN
        CALL READCpFile(CntrPar, PerfData, ErrVar)
    ENDIF
END SUBROUTINE ReadRestartFile

 
SUBROUTINE Debug(LocalVar, CntrPar, DebugVar, ErrVar, avrSWAP, RootName, size_avcOUTNAME)
! Debug routine, defines what gets printed to DEBUG.dbg if LoggingLevel = 1

    TYPE(ControlParameters), INTENT(IN) :: CntrPar
    TYPE(LocalVariables), INTENT(IN) :: LocalVar
    TYPE(DebugVariables), INTENT(IN) :: DebugVar
    TYPE(ErrorVariables),       INTENT(INOUT)   :: ErrVar

    INTEGER(IntKi), INTENT(IN)      :: size_avcOUTNAME
    INTEGER(IntKi)                  :: I , nDebugOuts, nLocalVars   ! Generic index.
    CHARACTER(1), PARAMETER         :: Tab = CHAR(9)                ! The tab character.
    CHARACTER(100)                  :: FmtDat                       ! The format of the debugging data
    INTEGER(IntKi), SAVE            :: UnDb                         ! I/O unit for the debugging information
    INTEGER(IntKi), SAVE            :: UnDb2                        ! I/O unit for the debugging information, avrSWAP
    INTEGER(IntKi), SAVE            :: UnDb3                        ! I/O unit for the debugging information, avrSWAP
    REAL(ReKi), INTENT(INOUT)       :: avrSWAP(*)                   ! The swap array, used to pass data to, and receive data from, the DLL controller.
    CHARACTER(size_avcOUTNAME), INTENT(IN) :: RootName            ! a Fortran version of the input C string (not considered an array here)    [subtract 1 for the C null-character]
    CHARACTER(200)                  :: Version                      ! git version of ROSCO
    CHARACTER(15), ALLOCATABLE      :: DebugOutStrings(:), DebugOutUnits(:)

    ! Avr output writing
    INTEGER(IntKi), SAVE, DIMENSION(:), ALLOCATABLE     :: avrIndices
    INTEGER(IntKi)                  :: avrBaseLength = 85
    INTEGER(IntKi)                  :: Ind
    CHARACTER(100)                  :: avrFmt

    REAL(DbKi), ALLOCATABLE         :: DebugOutData(:)
 
    CHARACTER(15), DIMENSION(159)      :: LocalVarOutStrings
    REAL(DbKi), ALLOCATABLE         :: LocalVarOutData(:)
 
    nDebugOuts = 26
    Allocate(DebugOutData(nDebugOuts))
    Allocate(DebugOutStrings(nDebugOuts))
    Allocate(DebugOutUnits(nDebugOuts))
    DebugOutData(1) = DebugVar%WE_Cp
    DebugOutData(2) = DebugVar%WE_b
    DebugOutData(3) = DebugVar%WE_w
    DebugOutData(4) = DebugVar%WE_t
    DebugOutData(5) = DebugVar%WE_Vm
    DebugOutData(6) = DebugVar%WE_Vt
    DebugOutData(7) = DebugVar%WE_Vw
    DebugOutData(8) = DebugVar%WE_lambda
    DebugOutData(9) = DebugVar%PC_PICommand
    DebugOutData(10) = DebugVar%GenSpeedF
    DebugOutData(11) = DebugVar%RotSpeedF
    DebugOutData(12) = DebugVar%NacIMU_FA_AccF
    DebugOutData(13) = DebugVar%FA_AccF
    DebugOutData(14) = DebugVar%Fl_PitCom
    DebugOutData(15) = DebugVar%PC_MinPit
    DebugOutData(16) = DebugVar%axisTilt_1P
    DebugOutData(17) = DebugVar%axisYaw_1P
    DebugOutData(18) = DebugVar%axisTilt_2P
    DebugOutData(19) = DebugVar%axisYaw_2P
    DebugOutData(20) = DebugVar%YawRateCom
    DebugOutData(21) = DebugVar%NacHeadingTarget
    DebugOutData(22) = DebugVar%NacVaneOffset
    DebugOutData(23) = DebugVar%Yaw_Err
    DebugOutData(24) = DebugVar%YawState
    DebugOutData(25) = DebugVar%VS_RefSpd
    DebugOutData(26) = DebugVar%PC_RefSpd
    DebugOutStrings = [CHARACTER(15) ::  'WE_Cp', 'WE_b', 'WE_w', 'WE_t', 'WE_Vm', & 
                                      'WE_Vt', 'WE_Vw', 'WE_lambda', 'PC_PICommand', 'GenSpeedF', & 
                                      'RotSpeedF', 'NacIMU_FA_AccF', 'FA_AccF', 'Fl_PitCom', 'PC_MinPit', & 
                                      'axisTilt_1P', 'axisYaw_1P', 'axisTilt_2P', 'axisYaw_2P', 'YawRateCom', & 
                                      'NacHeadingTarget', 'NacVaneOffset', 'Yaw_Err', 'YawState', 'VS_RefSpd', & 
                                      'PC_RefSpd']
    DebugOutUnits = [CHARACTER(15) ::  '[-]', '[-]', '[-]', '[-]', '[m/s]', & 
                                      '[m/s]', '[m/s]', '[rad]', '[rad]', '[rad/s]', & 
                                      '[rad/s]', '[rad/s]', '[m/s]', '[rad]', '[rad]', & 
                                      '[N/A]', '[N/A]', '[N/A]', '[N/A]', '[rad/s]', & 
                                      '[deg]', '[deg]', '[deg]', '[N/A]', '[rad/s]', & 
                                      '[rad/s]']
    nLocalVars = 159
    Allocate(LocalVarOutData(nLocalVars))
    LocalVarOutData(1) = LocalVar%iStatus
    LocalVarOutData(2) = LocalVar%AlreadyInitialized
    LocalVarOutData(3) = LocalVar%RestartWSE
    LocalVarOutData(4) = LocalVar%Time
    LocalVarOutData(5) = LocalVar%DT
    LocalVarOutData(6) = LocalVar%n_DT
    LocalVarOutData(7) = LocalVar%Time_Last
    LocalVarOutData(8) = LocalVar%VS_GenPwr
    LocalVarOutData(9) = LocalVar%GenSpeed
    LocalVarOutData(10) = LocalVar%RotSpeed
    LocalVarOutData(11) = LocalVar%NacHeading
    LocalVarOutData(12) = LocalVar%NacVane
    LocalVarOutData(13) = LocalVar%NacVaneF
    LocalVarOutData(14) = LocalVar%WindDir
    LocalVarOutData(15) = LocalVar%HorWindV
    LocalVarOutData(16) = LocalVar%HorWindV_F
    LocalVarOutData(17) = LocalVar%rootMOOP(1)
    LocalVarOutData(18) = LocalVar%rootMOOPF(1)
    LocalVarOutData(19) = LocalVar%BlPitch(1)
    LocalVarOutData(20) = LocalVar%BlPitchCMeas
    LocalVarOutData(21) = LocalVar%Azimuth
    LocalVarOutData(22) = LocalVar%OL_Azimuth
    LocalVarOutData(23) = LocalVar%AzUnwrapped
    LocalVarOutData(24) = LocalVar%AzError
    LocalVarOutData(25) = LocalVar%GenTqAz
    LocalVarOutData(26) = LocalVar%AzBuffer(1)
    LocalVarOutData(27) = LocalVar%NumBl
    LocalVarOutData(28) = LocalVar%FA_Acc_TT
    LocalVarOutData(29) = LocalVar%SS_Acc_TT
    LocalVarOutData(30) = LocalVar%FA_Acc_Nac
    LocalVarOutData(31) = LocalVar%NacIMU_FA_RAcc
    LocalVarOutData(32) = LocalVar%FA_AccHPF
    LocalVarOutData(33) = LocalVar%FA_AccHPFI
    LocalVarOutData(34) = LocalVar%FA_PitCom(1)
    LocalVarOutData(35) = LocalVar%VS_RefSpd
    LocalVarOutData(36) = LocalVar%VS_RefSpd_TSR
    LocalVarOutData(37) = LocalVar%VS_RefSpd_TRA
    LocalVarOutData(38) = LocalVar%VS_RefSpd_RL
    LocalVarOutData(39) = LocalVar%PC_RefSpd
    LocalVarOutData(40) = LocalVar%PC_RefSpd_SS
    LocalVarOutData(41) = LocalVar%PC_RefSpd_PRC
    LocalVarOutData(42) = LocalVar%RotSpeedF
    LocalVarOutData(43) = LocalVar%GenSpeedF
    LocalVarOutData(44) = LocalVar%GenTq
    LocalVarOutData(45) = LocalVar%GenTqMeas
    LocalVarOutData(46) = LocalVar%GenArTq
    LocalVarOutData(47) = LocalVar%GenBrTq
    LocalVarOutData(48) = LocalVar%VS_KOmega2_GenTq
    LocalVarOutData(49) = LocalVar%VS_ConstPwr_GenTq
    LocalVarOutData(50) = LocalVar%IPC_PitComF(1)
    LocalVarOutData(51) = LocalVar%PC_KP
    LocalVarOutData(52) = LocalVar%PC_KI
    LocalVarOutData(53) = LocalVar%PC_KD
    LocalVarOutData(54) = LocalVar%PC_TF
    LocalVarOutData(55) = LocalVar%PC_MaxPit
    LocalVarOutData(56) = LocalVar%PC_MinPit
    LocalVarOutData(57) = LocalVar%PC_PitComT
    LocalVarOutData(58) = LocalVar%PC_PitComT_Last
    LocalVarOutData(59) = LocalVar%BlPitchCMeasF
    LocalVarOutData(60) = LocalVar%PC_PitComT_IPC(1)
    LocalVarOutData(61) = LocalVar%PC_PwrErr
    LocalVarOutData(62) = LocalVar%PC_SpdErr
    LocalVarOutData(63) = LocalVar%IPC_AxisTilt_1P
    LocalVarOutData(64) = LocalVar%IPC_AxisYaw_1P
    LocalVarOutData(65) = LocalVar%IPC_AxisTilt_2P
    LocalVarOutData(66) = LocalVar%IPC_AxisYaw_2P
    LocalVarOutData(67) = LocalVar%axisTilt_1P
    LocalVarOutData(68) = LocalVar%axisYaw_1P
    LocalVarOutData(69) = LocalVar%axisYawF_1P
    LocalVarOutData(70) = LocalVar%axisTilt_2P
    LocalVarOutData(71) = LocalVar%axisYaw_2P
    LocalVarOutData(72) = LocalVar%axisYawF_2P
    LocalVarOutData(73) = LocalVar%IPC_KI(1)
    LocalVarOutData(74) = LocalVar%IPC_KP(1)
    LocalVarOutData(75) = LocalVar%IPC_IntSat
    LocalVarOutData(76) = LocalVar%PC_State
    LocalVarOutData(77) = LocalVar%PitCom(1)
    LocalVarOutData(78) = LocalVar%PitCom_SD(1)
    LocalVarOutData(79) = LocalVar%PitComAct(1)
    LocalVarOutData(80) = LocalVar%SS_DelOmegaF
    LocalVarOutData(81) = LocalVar%TestType
    LocalVarOutData(82) = LocalVar%Kp_Float
    LocalVarOutData(83) = LocalVar%VS_MaxTq
    LocalVarOutData(84) = LocalVar%VS_LastGenTrq
    LocalVarOutData(85) = LocalVar%VS_LastGenPwr
    LocalVarOutData(86) = LocalVar%VS_MechGenPwr
    LocalVarOutData(87) = LocalVar%VS_SpdErrAr
    LocalVarOutData(88) = LocalVar%VS_SpdErrBr
    LocalVarOutData(89) = LocalVar%VS_SpdErr
    LocalVarOutData(90) = LocalVar%VS_State
    LocalVarOutData(91) = LocalVar%VS_Rgn3Pitch
    LocalVarOutData(92) = LocalVar%WE_Vw
    LocalVarOutData(93) = LocalVar%WE_Vw_F
    LocalVarOutData(94) = LocalVar%WE_VwI
    LocalVarOutData(95) = LocalVar%WE_VwIdot
    LocalVarOutData(96) = LocalVar%WE_Op
    LocalVarOutData(97) = LocalVar%WE_Op_Last
    LocalVarOutData(98) = LocalVar%VS_LastGenTrqF
    LocalVarOutData(99) = LocalVar%PRC_WSE_F
    LocalVarOutData(100) = LocalVar%PRC_R_Speed
    LocalVarOutData(101) = LocalVar%PRC_R_Torque
    LocalVarOutData(102) = LocalVar%PRC_R_Pitch
    LocalVarOutData(103) = LocalVar%PRC_R_Total
    LocalVarOutData(104) = LocalVar%PRC_Min_Pitch
    LocalVarOutData(105) = LocalVar%PS_Min_Pitch
    LocalVarOutData(106) = LocalVar%OL_Index
    LocalVarOutData(107) = LocalVar%SU_Stage
    LocalVarOutData(108) = LocalVar%SU_LoadStageStartTime
    LocalVarOutData(109) = LocalVar%SU_RotSpeedF
    LocalVarOutData(110) = LocalVar%SD_Trigger
    LocalVarOutData(111) = LocalVar%SD_BlPitchF
    LocalVarOutData(112) = LocalVar%SD_NacVaneF
    LocalVarOutData(113) = LocalVar%SD_GenSpeedF
    LocalVarOutData(114) = LocalVar%SD_Stage
    LocalVarOutData(115) = LocalVar%SD_StageStartTime
    LocalVarOutData(116) = LocalVar%SD_MaxPitchRate
    LocalVarOutData(117) = LocalVar%SD_MaxTorqueRate
    LocalVarOutData(118) = LocalVar%GenTq_SD
    LocalVarOutData(119) = LocalVar%Fl_PitCom
    LocalVarOutData(120) = LocalVar%NACIMU_FA_AccF
    LocalVarOutData(121) = LocalVar%FA_AccF
    LocalVarOutData(122) = LocalVar%FA_Hist
    LocalVarOutData(123) = LocalVar%TRA_LastRefSpd
    LocalVarOutData(124) = LocalVar%VS_RefSpeed
    LocalVarOutData(125) = LocalVar%PtfmTDX
    LocalVarOutData(126) = LocalVar%PtfmTDY
    LocalVarOutData(127) = LocalVar%PtfmTDZ
    LocalVarOutData(128) = LocalVar%PtfmRDX
    LocalVarOutData(129) = LocalVar%PtfmRDY
    LocalVarOutData(130) = LocalVar%PtfmRDZ
    LocalVarOutData(131) = LocalVar%PtfmTVX
    LocalVarOutData(132) = LocalVar%PtfmTVY
    LocalVarOutData(133) = LocalVar%PtfmTVZ
    LocalVarOutData(134) = LocalVar%PtfmRVX
    LocalVarOutData(135) = LocalVar%PtfmRVY
    LocalVarOutData(136) = LocalVar%PtfmRVZ
    LocalVarOutData(137) = LocalVar%PtfmTAX
    LocalVarOutData(138) = LocalVar%PtfmTAY
    LocalVarOutData(139) = LocalVar%PtfmTAZ
    LocalVarOutData(140) = LocalVar%PtfmRAX
    LocalVarOutData(141) = LocalVar%PtfmRAY
    LocalVarOutData(142) = LocalVar%PtfmRAZ
    LocalVarOutData(143) = LocalVar%CC_DesiredL(1)
    LocalVarOutData(144) = LocalVar%CC_ActuatedL(1)
    LocalVarOutData(145) = LocalVar%CC_ActuatedDL(1)
    LocalVarOutData(146) = LocalVar%StC_Input(1)
    LocalVarOutData(147) = LocalVar%Flp_Angle(1)
    LocalVarOutData(148) = LocalVar%RootMyb_Last(1)
    LocalVarOutData(149) = LocalVar%ACC_INFILE_SIZE
    LocalVarOutData(150) = LocalVar%AWC_complexangle(1)
    LocalVarOutData(151) = LocalVar%TiltMean
    LocalVarOutData(152) = LocalVar%YawMean
    LocalVarOutData(153) = LocalVar%ZMQ_ID
    LocalVarOutData(154) = LocalVar%ZMQ_YawOffset
    LocalVarOutData(155) = LocalVar%ZMQ_TorqueOffset
    LocalVarOutData(156) = LocalVar%ZMQ_PitOffset(1)
    LocalVarOutData(157) = LocalVar%ZMQ_R_Speed
    LocalVarOutData(158) = LocalVar%ZMQ_R_Torque
    LocalVarOutData(159) = LocalVar%ZMQ_R_Pitch

    LocalVarOutStrings(1) = 'iStatus'
    LocalVarOutStrings(2) = 'AlreadyInitialized'
    LocalVarOutStrings(3) = 'RestartWSE'
    LocalVarOutStrings(4) = 'Time'
    LocalVarOutStrings(5) = 'DT'
    LocalVarOutStrings(6) = 'n_DT'
    LocalVarOutStrings(7) = 'Time_Last'
    LocalVarOutStrings(8) = 'VS_GenPwr'
    LocalVarOutStrings(9) = 'GenSpeed'
    LocalVarOutStrings(10) = 'RotSpeed'
    LocalVarOutStrings(11) = 'NacHeading'
    LocalVarOutStrings(12) = 'NacVane'
    LocalVarOutStrings(13) = 'NacVaneF'
    LocalVarOutStrings(14) = 'WindDir'
    LocalVarOutStrings(15) = 'HorWindV'
    LocalVarOutStrings(16) = 'HorWindV_F'
    LocalVarOutStrings(17) = 'rootMOOP'
    LocalVarOutStrings(18) = 'rootMOOPF'
    LocalVarOutStrings(19) = 'BlPitch'
    LocalVarOutStrings(20) = 'BlPitchCMeas'
    LocalVarOutStrings(21) = 'Azimuth'
    LocalVarOutStrings(22) = 'OL_Azimuth'
    LocalVarOutStrings(23) = 'AzUnwrapped'
    LocalVarOutStrings(24) = 'AzError'
    LocalVarOutStrings(25) = 'GenTqAz'
    LocalVarOutStrings(26) = 'AzBuffer'
    LocalVarOutStrings(27) = 'NumBl'
    LocalVarOutStrings(28) = 'FA_Acc_TT'
    LocalVarOutStrings(29) = 'SS_Acc_TT'
    LocalVarOutStrings(30) = 'FA_Acc_Nac'
    LocalVarOutStrings(31) = 'NacIMU_FA_RAcc'
    LocalVarOutStrings(32) = 'FA_AccHPF'
    LocalVarOutStrings(33) = 'FA_AccHPFI'
    LocalVarOutStrings(34) = 'FA_PitCom'
    LocalVarOutStrings(35) = 'VS_RefSpd'
    LocalVarOutStrings(36) = 'VS_RefSpd_TSR'
    LocalVarOutStrings(37) = 'VS_RefSpd_TRA'
    LocalVarOutStrings(38) = 'VS_RefSpd_RL'
    LocalVarOutStrings(39) = 'PC_RefSpd'
    LocalVarOutStrings(40) = 'PC_RefSpd_SS'
    LocalVarOutStrings(41) = 'PC_RefSpd_PRC'
    LocalVarOutStrings(42) = 'RotSpeedF'
    LocalVarOutStrings(43) = 'GenSpeedF'
    LocalVarOutStrings(44) = 'GenTq'
    LocalVarOutStrings(45) = 'GenTqMeas'
    LocalVarOutStrings(46) = 'GenArTq'
    LocalVarOutStrings(47) = 'GenBrTq'
    LocalVarOutStrings(48) = 'VS_KOmega2_GenTq'
    LocalVarOutStrings(49) = 'VS_ConstPwr_GenTq'
    LocalVarOutStrings(50) = 'IPC_PitComF'
    LocalVarOutStrings(51) = 'PC_KP'
    LocalVarOutStrings(52) = 'PC_KI'
    LocalVarOutStrings(53) = 'PC_KD'
    LocalVarOutStrings(54) = 'PC_TF'
    LocalVarOutStrings(55) = 'PC_MaxPit'
    LocalVarOutStrings(56) = 'PC_MinPit'
    LocalVarOutStrings(57) = 'PC_PitComT'
    LocalVarOutStrings(58) = 'PC_PitComT_Last'
    LocalVarOutStrings(59) = 'BlPitchCMeasF'
    LocalVarOutStrings(60) = 'PC_PitComT_IPC'
    LocalVarOutStrings(61) = 'PC_PwrErr'
    LocalVarOutStrings(62) = 'PC_SpdErr'
    LocalVarOutStrings(63) = 'IPC_AxisTilt_1P'
    LocalVarOutStrings(64) = 'IPC_AxisYaw_1P'
    LocalVarOutStrings(65) = 'IPC_AxisTilt_2P'
    LocalVarOutStrings(66) = 'IPC_AxisYaw_2P'
    LocalVarOutStrings(67) = 'axisTilt_1P'
    LocalVarOutStrings(68) = 'axisYaw_1P'
    LocalVarOutStrings(69) = 'axisYawF_1P'
    LocalVarOutStrings(70) = 'axisTilt_2P'
    LocalVarOutStrings(71) = 'axisYaw_2P'
    LocalVarOutStrings(72) = 'axisYawF_2P'
    LocalVarOutStrings(73) = 'IPC_KI'
    LocalVarOutStrings(74) = 'IPC_KP'
    LocalVarOutStrings(75) = 'IPC_IntSat'
    LocalVarOutStrings(76) = 'PC_State'
    LocalVarOutStrings(77) = 'PitCom'
    LocalVarOutStrings(78) = 'PitCom_SD'
    LocalVarOutStrings(79) = 'PitComAct'
    LocalVarOutStrings(80) = 'SS_DelOmegaF'
    LocalVarOutStrings(81) = 'TestType'
    LocalVarOutStrings(82) = 'Kp_Float'
    LocalVarOutStrings(83) = 'VS_MaxTq'
    LocalVarOutStrings(84) = 'VS_LastGenTrq'
    LocalVarOutStrings(85) = 'VS_LastGenPwr'
    LocalVarOutStrings(86) = 'VS_MechGenPwr'
    LocalVarOutStrings(87) = 'VS_SpdErrAr'
    LocalVarOutStrings(88) = 'VS_SpdErrBr'
    LocalVarOutStrings(89) = 'VS_SpdErr'
    LocalVarOutStrings(90) = 'VS_State'
    LocalVarOutStrings(91) = 'VS_Rgn3Pitch'
    LocalVarOutStrings(92) = 'WE_Vw'
    LocalVarOutStrings(93) = 'WE_Vw_F'
    LocalVarOutStrings(94) = 'WE_VwI'
    LocalVarOutStrings(95) = 'WE_VwIdot'
    LocalVarOutStrings(96) = 'WE_Op'
    LocalVarOutStrings(97) = 'WE_Op_Last'
    LocalVarOutStrings(98) = 'VS_LastGenTrqF'
    LocalVarOutStrings(99) = 'PRC_WSE_F'
    LocalVarOutStrings(100) = 'PRC_R_Speed'
    LocalVarOutStrings(101) = 'PRC_R_Torque'
    LocalVarOutStrings(102) = 'PRC_R_Pitch'
    LocalVarOutStrings(103) = 'PRC_R_Total'
    LocalVarOutStrings(104) = 'PRC_Min_Pitch'
    LocalVarOutStrings(105) = 'PS_Min_Pitch'
    LocalVarOutStrings(106) = 'OL_Index'
    LocalVarOutStrings(107) = 'SU_Stage'
    LocalVarOutStrings(108) = 'SU_LoadStageStartTime'
    LocalVarOutStrings(109) = 'SU_RotSpeedF'
    LocalVarOutStrings(110) = 'SD_Trigger'
    LocalVarOutStrings(111) = 'SD_BlPitchF'
    LocalVarOutStrings(112) = 'SD_NacVaneF'
    LocalVarOutStrings(113) = 'SD_GenSpeedF'
    LocalVarOutStrings(114) = 'SD_Stage'
    LocalVarOutStrings(115) = 'SD_StageStartTime'
    LocalVarOutStrings(116) = 'SD_MaxPitchRate'
    LocalVarOutStrings(117) = 'SD_MaxTorqueRate'
    LocalVarOutStrings(118) = 'GenTq_SD'
    LocalVarOutStrings(119) = 'Fl_PitCom'
    LocalVarOutStrings(120) = 'NACIMU_FA_AccF'
    LocalVarOutStrings(121) = 'FA_AccF'
    LocalVarOutStrings(122) = 'FA_Hist'
    LocalVarOutStrings(123) = 'TRA_LastRefSpd'
    LocalVarOutStrings(124) = 'VS_RefSpeed'
    LocalVarOutStrings(125) = 'PtfmTDX'
    LocalVarOutStrings(126) = 'PtfmTDY'
    LocalVarOutStrings(127) = 'PtfmTDZ'
    LocalVarOutStrings(128) = 'PtfmRDX'
    LocalVarOutStrings(129) = 'PtfmRDY'
    LocalVarOutStrings(130) = 'PtfmRDZ'
    LocalVarOutStrings(131) = 'PtfmTVX'
    LocalVarOutStrings(132) = 'PtfmTVY'
    LocalVarOutStrings(133) = 'PtfmTVZ'
    LocalVarOutStrings(134) = 'PtfmRVX'
    LocalVarOutStrings(135) = 'PtfmRVY'
    LocalVarOutStrings(136) = 'PtfmRVZ'
    LocalVarOutStrings(137) = 'PtfmTAX'
    LocalVarOutStrings(138) = 'PtfmTAY'
    LocalVarOutStrings(139) = 'PtfmTAZ'
    LocalVarOutStrings(140) = 'PtfmRAX'
    LocalVarOutStrings(141) = 'PtfmRAY'
    LocalVarOutStrings(142) = 'PtfmRAZ'
    LocalVarOutStrings(143) = 'CC_DesiredL'
    LocalVarOutStrings(144) = 'CC_ActuatedL'
    LocalVarOutStrings(145) = 'CC_ActuatedDL'
    LocalVarOutStrings(146) = 'StC_Input'
    LocalVarOutStrings(147) = 'Flp_Angle'
    LocalVarOutStrings(148) = 'RootMyb_Last'
    LocalVarOutStrings(149) = 'ACC_INFILE_SIZE'
    LocalVarOutStrings(150) = 'AWC_complexangle'
    LocalVarOutStrings(151) = 'TiltMean'
    LocalVarOutStrings(152) = 'YawMean'
    LocalVarOutStrings(153) = 'ZMQ_ID'
    LocalVarOutStrings(154) = 'ZMQ_YawOffset'
    LocalVarOutStrings(155) = 'ZMQ_TorqueOffset'
    LocalVarOutStrings(156) = 'ZMQ_PitOffset'
    LocalVarOutStrings(157) = 'ZMQ_R_Speed'
    LocalVarOutStrings(158) = 'ZMQ_R_Torque'
    LocalVarOutStrings(159) = 'ZMQ_R_Pitch'
    ! Initialize debug file
    IF ((LocalVar%iStatus == 0) .OR. (LocalVar%iStatus == -9))  THEN ! .TRUE. if we're on the first call to the DLL
        IF (CntrPar%LoggingLevel > 0) THEN
            CALL GetNewUnit(UnDb, ErrVar)
            OPEN(unit=UnDb, FILE=TRIM(RootName)//'.RO.dbg')
            WRITE(UnDb, *)  'Generated on '//CurDate()//' at '//CurTime()//' using ROSCO-'//TRIM(rosco_version)
            WRITE(UnDb, '(99(a20,TR5:))') 'Time',   DebugOutStrings
            WRITE(UnDb, '(99(a20,TR5:))') '(sec)',  DebugOutUnits
        END IF

        IF (CntrPar%LoggingLevel > 1) THEN
            CALL GetNewUnit(UnDb2, ErrVar)
            OPEN(unit=UnDb2, FILE=TRIM(RootName)//'.RO.dbg2')
            WRITE(UnDb2, *)  'Generated on '//CurDate()//' at '//CurTime()//' using ROSCO-'//TRIM(rosco_version)
            WRITE(UnDb2, '(160(a20,TR5:))') 'Time',   LocalVarOutStrings
            WRITE(UnDb2, '(160(a20,TR5:))')
        END IF

        IF (CntrPar%LoggingLevel > 2) THEN
            ! Set avrIndices, start with basic indices
            Allocate(avrIndices(avrBaseLength))
            DO Ind = 1, avrBaseLength
                avrIndices(Ind) = Ind
            END DO

            ! Cable control indices
            IF (CntrPar%CC_Mode > 0) THEN
                DO Ind = 1, SIZE(CntrPar%CC_GroupIndex)
                    Call AddToList(avrIndices,CntrPar%CC_GroupIndex(Ind))
                    Call AddToList(avrIndices,CntrPar%CC_GroupIndex(Ind)+1)
                END DO
            END IF

            ! Structural control indices
            IF (CntrPar%StC_Mode > 0) THEN
                DO Ind = 1, SIZE(CntrPar%StC_GroupIndex)
                    Call AddToList(avrIndices,CntrPar%StC_GroupIndex(Ind))
                END DO
            END IF

            ! Format string
            avrFmt = '(A21,'//TRIM(Int2LStr(SIZE(avrIndices)))//'(TR12,"'//'AvrSWAP(",I4,")"))'

            CALL GetNewUnit(UnDb3, ErrVar)
            OPEN(unit=UnDb3, FILE=TRIM(RootName)//'.RO.dbg3')
            WRITE(UnDb3,'(/////)')
            WRITE(UnDb3,avrFmt)  'LocalVar%Time ', (avrIndices)
            WRITE(UnDb3,'(A21,'//TRIM(Int2LStr(SIZE(avrIndices)))//'(TR22,"(-)"))')  '(s)'
        END IF
    END IF

        ! Print simulation status, every 10 seconds
    IF (MODULO(LocalVar%Time, 10.0_DbKi) == 0) THEN
        WRITE(*, 100) LocalVar%GenSpeedF*RPS2RPM, LocalVar%BlPitch(1)*R2D, avrSWAP(15)/1000.0, LocalVar%WE_Vw
        100 FORMAT('Generator speed: ', f6.1, ' RPM, Pitch angle: ', f5.1, ' deg, Power: ', f7.1, ' kW, Est. wind Speed: ', f5.1, ' m/s')
    END IF

    ! Process DebugOutData, LocalVarOutData
    ! Remove very small numbers that cause ******** outputs
    DO I = 1,SIZE(DebugOutData)
        IF (ABS(DebugOutData(I)) < 1E-99) THEN
            DebugOutData(I) = 0
        END IF
        IF (ABS(DebugOutData(I)) > 1E+99) THEN
            DebugOutData(I) = 1E+99
        END IF
    END DO
    
    DO I = 1,SIZE(LocalVarOutData)
        IF (ABS(LocalVarOutData(I)) < 1E-99) THEN
            LocalVarOutData(I) = 0
        END IF
        IF (ABS(LocalVarOutData(I)) > 1E+99) THEN
            LocalVarOutData(I) = 1E+99
        END IF
    END DO
    
    ! Write debug files
    FmtDat = "(F20.5,TR5,159(ES20.5E2,TR5:))"   ! The format of the debugging data
    IF ( MOD(LocalVar%n_DT, CntrPar%n_DT_Out) == 0) THEN
        IF((CntrPar%LoggingLevel > 0) .AND. (LocalVar%iStatus .ge. 0)) THEN
            WRITE (UnDb, TRIM(FmtDat))  LocalVar%Time, DebugOutData
        END IF

        IF((CntrPar%LoggingLevel > 1) .AND. (LocalVar%iStatus .ge. 0)) THEN
            WRITE (UnDb2, TRIM(FmtDat))  LocalVar%Time, LocalVarOutData
        END IF

        IF((CntrPar%LoggingLevel > 2) .AND. (LocalVar%iStatus .ge. 0)) THEN
            WRITE (UnDb3, TRIM(FmtDat))    LocalVar%Time, avrSWAP(avrIndices)
        END IF
    END IF

    ! Close all files when simulation finished
    IF (LocalVar%iStatus < 0) THEN
        CLOSE(UnDb)
        CLOSE(UnDb2)
        CLOSE(UnDb3)
    ENDIF
END SUBROUTINE Debug

END MODULE ROSCO_IO
