function [Eng] = ScaleData(P,S)

epoch_serialdate = datenum(1970,1,1,0,0,0);

names = fieldnames(S);

Eng.time = cast(P.EpochSecs,'double')+cast(P.EpochUsecs,'double')/1000000;

for i = 1:length(names)
   switch (names{i})
       case {'rollAngle','pitchAngle','yawAngleTrue'}
         Eng.(names{i}) = cast(P.(names{i}),'double')*(360/2^16);
       case {'xRateCorrected','yRateCorrected','zRateCorrected'}
         Eng.(names{i}) = cast(P.(names{i}),'double')*(1260/2^16);
       case {'xAccel','yAccel','zAccel'}
         Eng.(names{i}) = cast(P.(names{i}),'double')*(20/2^16);  
       case {'northVelocity','eastVelocity','downVelocity'}
         Eng.(names{i}) = cast(P.(names{i}),'double')*(512/2^16);  
       case {'longitudeGPS','latitudeGPS'}    
         Eng.(names{i}) = cast(P.(names{i}),'double')*(360/2^32);  
       case {'altitudeGPS'}
         Eng.(names{i}) = cast(P.(names{i}),'double')*(1/2^2);      
       case {'xRateTemperature'}
         Eng.(names{i}) = cast(P.(names{i}),'double')*(200/2^16);   
       case {'statusBits','timeITOW'}  %No cast or scale
         Eng.(names{i}) = P.(names{i});              
       case {'UpperSpringPressure','LowerSpringPressure','LoadCellForce','PistonDistance','UpperSpringTemperature','LowerSpringTemperature','SeawaterTemperature','IsothermalTemperature'}  %Fixed Point, lowest 10 bits are fractional part.
         Eng.(names{i}) = cast(P.(names{i}),'double')/1024;  
       otherwise %No scaling needed, just cast to double.
           %sprintf('%s %x\n',names{i},P.(names{i}))
         Eng.(names{i}) = cast(P.(names{i}),'double');
   end
end
Eng.latGPSdegrees = fix(Eng.latitudeGPS);
Eng.latGPSminutes = abs(Eng.latitudeGPS-Eng.latGPSdegrees)*60;
Eng.lonGPSdegrees = fix(Eng.longitudeGPS);
Eng.lonGPSminutes = abs(Eng.longitudeGPS-Eng.lonGPSdegrees)*60;

Eng.time = Eng.EpochSecs+Eng.EpochUsecs/1000000;
Eng.serialdate = epoch_serialdate + Eng.time/(24*60*60);

end

