%close all
%clear all

%global P;
%global count;

%path = 'X:\707976 EPBuoyPrototypeAndTesting\TestMachine\Test Data\Feb29_2012\';
%filename = 'D000000_120229_170502';



grav = 9.81;  %m/s^2
epoch_serialdate = datenum(1970,1,1,0,0,0);

%Packet Structure
DataSize = 120;
S.EpochUsecs = uint32(NaN);
S.EpochSecs = uint32(NaN);
S.rollAngle = int16(NaN);
S.pitchAngle = int16(NaN);
S.yawAngleTrue = int16(NaN);
S.xRateCorrected = int16(NaN);
S.yRateCorrected = int16(NaN);
S.zRateCorrected = int16(NaN);
S.xAccel = int16(NaN);
S.yAccel = int16(NaN);
S.zAccel = int16(NaN);
S.northVelocity = int16(NaN);
S.eastVelocity = int16(NaN);
S.downVelocity = int16(NaN);
S.longitudeGPS = int32(NaN);
S.latitudeGPS = int32(NaN);
S.altitudeGPS = int16(NaN);
S.xRateTemperature = int16(NaN);
S.timeITOW = uint32(NaN);
S.statusBits = int16(NaN);

S.padBytes = uint16(NaN);

S.LoadDumpVoltage_deprecated = double(NaN);
S.BatteryVoltage = double(NaN);
S.BatteryCurrent = double(NaN);

S.UpperSpringTemperature = int32(NaN);
S.LowerSpringTemperature = int32(NaN);
S.SeawaterTemperature = int32(NaN);
S.IsothermalTemperature = int32(NaN);
S.UpperSpringPressure = int32(NaN);
S.LowerSpringPressure = int32(NaN);
S.LoadCellForce = int32(NaN);
S.PistonDistance = int32(NaN);

S.LoadDumpVoltage = int16(NaN);
S.LoadDumpCurrent = int16(NaN);
S.LoadDumpTemp = int16(NaN);
S.LoadDumpRelayPos = int16(NaN);
S.SafetyRelayPos = int16(NaN);



  fid = fopen([path filename]);
  count = DataSize;
  ii = 0;
  while (count == DataSize)
    ii = ii+1
    [A, count] = fread(fid,DataSize);
    if(count == DataSize)
       P = ParseRawData(uint8(A),S);
       Eng = ScaleData(P,S);
       names = fieldnames(Eng);
       for jj = 1:length(names)
          eval([names{jj} '(ii) = Eng.' names{jj} ';']);
       end
    end
  end
  fclose(fid);

  save([path filename])
 
  clear S P Eng fid filename names jj ii count grav epoch_serialdate DataSize;

  


