function parse_data(filename,destfile)

%clear all;

disp('Parsering measurementfile:');
disp(filename);


fid = fopen(filename, 'r');
fseek(fid,0,'eof');
filesize = ftell(fid);
frewind(fid);

readCounter = 0;

%dayOfMonths = [31,28,31,30,31,30,31,31,30,31,30,31];
dayOfMonths = [0,(31*84600),((31+28)*86400),((31+28+31)*86400),((31+28+31+30)*86400),30,31,31,30,31,30,31];
%allocate vector sizes according to the file size
ecgData = zeros(round(0.9*filesize),2);
ecgPointer = 1;
implantXAxisData = zeros(round(0.5*filesize),2);
implantXAxisPointer = 1;
implantYAxisData = zeros(round(0.5*filesize),2);
implantYAxisPointer = 1;
implantZAxisData = zeros(round(0.5*filesize),2);
implantZAxisPointer = 1;
implantBatteryData = zeros(round(0.005*filesize),2);
implantBatteryPointer = 1;
implantTemperatureData = zeros(round(0.005*filesize),2);
implantTemperaturePointer = 1;
collarXAxisData = zeros(round(0.5*filesize),2);
collarXAxisPointer = 1;
collarYAxisData = zeros(round(0.5*filesize),2);
collarYAxisPointer = 1;
collarZAxisData = zeros(round(0.5*filesize),2);
collarZAxisPointer = 1;
collarBatteryData = zeros(round(0.005*filesize),2);
collarBatteryPointer = 1;
collarTemperatureData = zeros(round(0.005*filesize),2);
collarTemperaturePointer = 1;

while ((~feof(fid))&&(readCounter < filesize))

    highByte = fread(fid,1,'uint8');
    indicator = bitand(highByte,240);
    readCounter = readCounter + 1;
    
    switch(indicator)
        case 0
            %ecg result
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            if ecgPointer > 1
                %ecgData(ecgPointer,1) = ecgData(ecgPointer-1,1) + 1/ecgFreq;
                ecgData(ecgPointer,1) = ecgData(ecgPointer-1,1) + ecgInterval;
                %if(ecgData(ecgPointer,1) > (24*3600))
                %    ecgData(ecgPointer,1) = 0;
                %end
            end    
            ecgData(ecgPointer,2) = ((byte1*256) + byte2)*(2.048/4096);
            readCounter = readCounter + 1;
            ecgPointer = ecgPointer + 1;
        
        case 16
            %implant x-axis acceleration
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            accelerationValue = ((byte1*256) + byte2);
            if (bitand(accelerationValue,2048) ==  0)
                accelerationValue = 4*accelerationValue;
            else
                accelerationValue = (4096 - accelerationValue);
                accelerationValue = -4*accelerationValue;
            end  
                                        
            if implantXAxisPointer > 1
                %xAxisData(xAxisPointer,1) = xAxisData(xAxisPointer-1,1) + 1/xAxisFreq;
                implantXAxisData(implantXAxisPointer,1) = implantXAxisData(implantXAxisPointer-1,1) + implantXAxisInterval;
                %if(xAxisData(xAxisPointer,1) > (24*3600))
                %    xAxisData(xAxisPointer,1) = 0;
                %end    
            end    
            implantXAxisData(implantXAxisPointer,2) = accelerationValue;        
            readCounter = readCounter + 1;
            implantXAxisPointer = implantXAxisPointer + 1;
        
         case 80
            %collar x-axis acceleration
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            accelerationValue = ((byte1*256) + byte2);
            if (bitand(accelerationValue,2048) ==  0)
                accelerationValue = 4*accelerationValue;
            else
                accelerationValue = (4096 - accelerationValue);
                accelerationValue = -4*accelerationValue;
            end  
                                        
            if collarXAxisPointer > 1
                %xAxisData(xAxisPointer,1) = xAxisData(xAxisPointer-1,1) + 1/xAxisFreq;
                collarXAxisData(collarXAxisPointer,1) = collarXAxisData(collarXAxisPointer-1,1) + collarXAxisInterval;
                %if(xAxisData(xAxisPointer,1) > (24*3600))
                %    xAxisData(xAxisPointer,1) = 0;
                %end    
            end    
            collarXAxisData(collarXAxisPointer,2) = accelerationValue;        
            readCounter = readCounter + 1;
            collarXAxisPointer = collarXAxisPointer + 1;    
                        
        case 32
            %implant y-axis acceleration
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            accelerationValue = ((byte1*256) + byte2);
            if (bitand(accelerationValue,2048) ==  0)
                accelerationValue = 4*accelerationValue;
            else
                accelerationValue = (4096 - accelerationValue);
                accelerationValue = -4*accelerationValue;
            end
                      
            if implantYAxisPointer > 1
                %yAxisData(yAxisPointer,1) = yAxisData(yAxisPointer-1,1) + 1/yAxisFreq;
                implantYAxisData(implantYAxisPointer,1) = implantYAxisData(implantYAxisPointer-1,1) + implantYAxisInterval;
                %if(yAxisData(yAxisPointer,1) > (24*3600))
                %    yAxisData(yAxisPointer,1) = 0;
                %end    
            end
            
            implantYAxisData(implantYAxisPointer,2) = accelerationValue;
            readCounter = readCounter + 1;
            implantYAxisPointer = implantYAxisPointer + 1;
       
        case 96
            %collar y-axis acceleration
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            accelerationValue = ((byte1*256) + byte2);
            if (bitand(accelerationValue,2048) ==  0)
                accelerationValue = 4*accelerationValue;
            else
                accelerationValue = (4096 - accelerationValue);
                accelerationValue = -4*accelerationValue;
            end
                      
            if collarYAxisPointer > 1
                %yAxisData(yAxisPointer,1) = yAxisData(yAxisPointer-1,1) + 1/yAxisFreq;
                collarYAxisData(collarYAxisPointer,1) = collarYAxisData(collarYAxisPointer-1,1) + collarYAxisInterval;
                %if(yAxisData(yAxisPointer,1) > (24*3600))
                %    yAxisData(yAxisPointer,1) = 0;
                %end    
            end
            
            collarYAxisData(collarYAxisPointer,2) = accelerationValue;
            readCounter = readCounter + 1;
            collarYAxisPointer = collarYAxisPointer + 1;    
            
        case 48
            %implant z-axis acceleration
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            accelerationValue = ((byte1*256) + byte2);
            if (bitand(accelerationValue,2048) ==  0)
                accelerationValue = 4*accelerationValue;
            else
                accelerationValue = (4096 - accelerationValue);
                accelerationValue = -4*accelerationValue;
            end
            
            if implantZAxisPointer > 1
                %zAxisData(zAxisPointer,1) = zAxisData(zAxisPointer-1,1) + 1/zAxisFreq;
                implantZAxisData(implantZAxisPointer,1) = implantZAxisData(implantZAxisPointer-1,1) + implantZAxisInterval;
                %if(zAxisData(zAxisPointer,1) > (24*3600))
                %    zAxisData(zAxisPointer,1) = 0;
                %end    
            end
            
            implantZAxisData(implantZAxisPointer,2) = accelerationValue;
            readCounter = readCounter + 1;    
            implantZAxisPointer = implantZAxisPointer + 1;
        
        case 112
            %collar z-axis acceleration
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            accelerationValue = ((byte1*256) + byte2);
            if (bitand(accelerationValue,2048) ==  0)
                accelerationValue = 4*accelerationValue;
            else
                accelerationValue = (4096 - accelerationValue);
                accelerationValue = -4*accelerationValue;
            end
            
            if collarZAxisPointer > 1
                %zAxisData(zAxisPointer,1) = zAxisData(zAxisPointer-1,1) + 1/zAxisFreq;
                collarZAxisData(collarZAxisPointer,1) = collarZAxisData(collarZAxisPointer-1,1) + collarZAxisInterval;
                %if(zAxisData(zAxisPointer,1) > (24*3600))
                %    zAxisData(zAxisPointer,1) = 0;
                %end    
            end
            
            collarZAxisData(collarZAxisPointer,2) = accelerationValue;
            readCounter = readCounter + 1;    
            collarZAxisPointer = collarZAxisPointer + 1;    
            
        case 144
            %implant battery voltage
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            
            if implantBatteryPointer > 1
                %battData(battPointer,1) = battData(battPointer-1,1) + 1/battFreq;
                implantBatteryData(implantBatteryPointer,1) = implantBatteryData(implantBatteryPointer-1,1) + implantBatteryInterval;
                %if(battData(battPointer,1) > (24*3600))
                %    battData(battPointer,1) = 0;
                %end    
            end
            
            implantBatteryData(implantBatteryPointer,2) = ((byte1*256) + byte2)*(2.048/4096)*2;    
            readCounter = readCounter + 1;
            implantBatteryPointer = implantBatteryPointer + 1;
            
        case 160
            %collar battery voltage
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            
            if collarBatteryPointer > 1
                %battData(battPointer,1) = battData(battPointer-1,1) + 1/battFreq;
                collarBatteryData(collarBatteryPointer,1) = collarBatteryData(collarBatteryPointer-1,1) + collarBatteryInterval;
                %if(battData(battPointer,1) > (24*3600))
                %    battData(battPointer,1) = 0;
                %end    
            end
            
            collarBatteryData(collarBatteryPointer,2) = ((byte1*256) + byte2)*(2.87/4096)*3;    
            readCounter = readCounter + 1;
            collarBatteryPointer = collarBatteryPointer + 1;    
            
        case 64
            %implant temperature data
            %temperatureRead = fread(fid,1,'uint8');
            %if (bitand(temperatureRead,128) == 0)
            %    temperatureRead = temperatureRead;
            %else
            %    temperatureRead = -1*(256-temperatureRead);
            %end
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            temperatureRead = ((byte1*256) + byte2);
            %countRemain = (256*byte1 + byte2);
            %byte1 = fread(fid,1,'uint8');
            %byte2 = fread(fid,1,'uint8');
            %countPerC = (256*byte1 + byte2);
            readCounter = readCounter + 1;
            
            %if ecgPointer == 1 
            %    implantTemperatureData(implantTemperaturePointer,1) = ecgData(1,1);
            %else
            if implantTemperaturePointer > 1
                implantTemperatureData(implantTemperaturePointer,1) = implantTemperatureData(implantTemperaturePointer-1,1) + implantTemperatureInterval;
            end
            
            if temperatureRead < 2048
                implantTemperatureData(implantTemperaturePointer,2) = (temperatureRead*0.0625);
            else
                temperatureRead = (4096 - temperatureRead)  
                implantTemperatureData(implantTemperaturePointer,2) = (-1*temperatureRead*0.0625);
            end
            
            %if ((temperatureRead*0.0625) > 100)
            %    implantTemperatureData(implantTemperaturePointer,2) = implantTemperatureData(implantTemperaturePointer-1,2);
            %end
            
            implantTemperaturePointer = implantTemperaturePointer + 1;
            
            
            
        case 128
            %implant temperature data
            %temperatureRead = fread(fid,1,'uint8');
            %if (bitand(temperatureRead,128) == 0)
            %    temperatureRead = temperatureRead;
            %else
            %    temperatureRead = -1*(256-temperatureRead);
            %end
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            temperatureRead = ((byte1*256) + byte2);
            %countRemain = (256*byte1 + byte2);
            %byte1 = fread(fid,1,'uint8');
            %byte2 = fread(fid,1,'uint8');
            %countPerC = (256*byte1 + byte2);
            readCounter = readCounter + 1;
            
            %if ecgPointer == 1 
            %    implantTemperatureData(implantTemperaturePointer,1) = ecgData(1,1);
            %else
            if collarTemperaturePointer > 1
                collarTemperatureData(collarTemperaturePointer,1) = collarTemperatureData(collarTemperaturePointer-1,1) + collarTemperatureInterval;
            end
            
            if temperatureRead < 2048
                collarTemperatureData(collarTemperaturePointer,2) = (temperatureRead*0.0625);
            else
                temperatureRead = (4096 - temperatureRead);
                collarTemperatureData(collarTemperaturePointer,2) = (-1*temperatureRead*0.0625);
            end
            
            collarTemperaturePointer = collarTemperaturePointer + 1;    
            
        case 192
            %time field
            hour = fread(fid,1,'uint8');
            minute = fread(fid,1,'uint8');
            second = fread(fid,1,'uint8');
            day = fread(fid,1,'uint8');
            month = fread(fid,1,'uint8');
            byte1 = fread(fid,1,'uint8');
            byte2 = fread(fid,1,'uint8');
            year = (byte1*256) + byte2;
            readCounter = readCounter + 7;
            startTime = datenum(year,month,day,hour,minute,second);

            if collarXAxisPointer == 1
                disp('measurement timestamp');
                disp(datestr(startTime));
            end;

            %set times in seconds
            if ecgPointer == 1 
                ecgData(ecgPointer,1) = startTime;
            else
		ecgData(ecgPointer-1,1) = startTime;
            end   
            
            if implantXAxisPointer == 1
               implantXAxisData(implantXAxisPointer,1) = startTime;
	    else
               implantXAxisData(implantXAxisPointer-1,1) = startTime;   
            end   
             
            if implantYAxisPointer == 1
               implantYAxisData(implantYAxisPointer,1) = startTime;   
			else
				implantYAxisData(implantYAxisPointer-1,1) = startTime;   
            end   
             
            if implantZAxisPointer == 1
               implantZAxisData(implantZAxisPointer,1) = startTime;
			else   
				implantZAxisData(implantZAxisPointer-1,1) = startTime;
            end   
                
            if implantBatteryPointer == 1
               implantBatteryData(implantBatteryPointer,1) = startTime;   
			else
				implantBatteryData(implantBatteryPointer-1,1) = startTime;   
            end   
            
            if implantTemperaturePointer == 1
               implantTemperatureData(implantTemperaturePointer,1) = startTime;   
			else
				implantTemperatureData(implantTemperaturePointer-1,1) = startTime;   
            end   
            
            if collarXAxisPointer == 1
               collarXAxisData(collarXAxisPointer,1) = startTime;
			else
               collarXAxisData(collarXAxisPointer-1,1) = startTime;   
            end   
             
            if collarYAxisPointer == 1
               collarYAxisData(collarYAxisPointer,1) = startTime;   
			else
				collarYAxisData(collarYAxisPointer-1,1) = startTime;   
            end   
             
            if collarZAxisPointer == 1
               collarZAxisData(collarZAxisPointer,1) = startTime;
			else   
				collarZAxisData(collarZAxisPointer-1,1) = startTime;
            end   

            if collarBatteryPointer == 1
               collarBatteryData(collarBatteryPointer,1) = startTime;   
			else
				collarBatteryData(collarBatteryPointer-1,1) = startTime;   
            end   
            
            if collarTemperaturePointer == 1
               collarTemperatureData(collarTemperaturePointer,1) = startTime;   
			else
				collarTemperatureData(collarTemperaturePointer-1,1) = startTime;   
            end   
            
        case 224
            %samplerate field
            byte1 = fread(fid,1,'uint8');
            byte2 = fread(fid,1,'uint8');
            %ecgFreq = 1/(((byte1*256) + byte2)/4096);
            implantXAxisInterval = ((((byte1*256) + byte2)/4096)/(24*3600));
            implantYAxisInterval = implantXAxisInterval;
            implantZAxisInterval = implantXAxisInterval;
            %ecgInterval = ((((byte1*256) + byte2)/4096)/(24*3600));
            
            byte1 = fread(fid,1,'uint8');
            byte2 = fread(fid,1,'uint8');
            %battFreq = (1/(((byte1*256) + byte2)/4096))/16;
            implantBatteryInterval = ((((byte1*256) + byte2)/4096))/(24*3600);
            collarBatteryInterval = implantBatteryInterval;
            
            byte1 = fread(fid,1,'uint8');
            byte2 = fread(fid,1,'uint8');
            %xAxisFreq = 1/(((byte1*256) + byte2)/4096);
            implantTemperatureInterval = (((byte1*256) + byte2)/4096)/(24*3600);
            collarTemperatureInterval = implantTemperatureInterval;
            
            byte1 = fread(fid,1,'uint8');
            byte2 = fread(fid,1,'uint8');
            %yAxisFreq = 1/(((byte1*256) + byte2)/4096);
            collarXAxisInterval = ((((byte1*256) + byte2)/4096)/(24*3600));
            collarYAxisInterval = implantXAxisInterval;
            collarZAxisInterval = implantXAxisInterval;
            
            byte1 = fread(fid,1,'uint8');
            byte2 = fread(fid,1,'uint8');
            ecgInterval = ((((byte1*256) + byte2)/4096)/(24*3600));
            %zAxisFreq = 1/(((byte1*256) + byte2)/4096);
            %zAxisInterval = (((byte1*256) + byte2)/4096)/(24*3600);
            byte1 = fread(fid,1,'uint8');
            readCounter = readCounter + 11;
        
        case 208
            byte1 = bitand(highByte,15);
            byte2 = fread(fid,1,'uint8');
            disp('data missing');
            missingBytes = (byte1*256) + byte2;
            disp(missingBytes);
            readCounter = readCounter + 1;
            %this is for testing only
            %if ecgPointer > 1
            %    ecgData(ecgPointer,1) = ecgData(ecgPointer-1,1) + ecgInterval;
            %end    
            %ecgData(ecgPointer,2) = 2.5;
            %ecgPointer = ecgPointer + 1;
            
        %case 240
        %    byte1 = bitand(highByte,15);
        %    byte2 = fread(fid,1,'uint8');
        %    disp('write error');
        %    missingBytes = (byte1*256) + byte2;
        %    disp(missingBytes);
        %    readCounter = readCounter + 1;
            
        otherwise
            disp('undefined command byte');
    end
   
   %report progress
   if mod(readCounter,100*1024) == 0
       disp((readCounter/filesize) * 100);
   end    
end

fclose(fid);
%delete left over cells
ecgData = ecgData(1:ecgPointer-1, :);

implantBatteryData = implantBatteryData(1:implantBatteryPointer-1, :);
collarBatteryData = collarBatteryData(1:collarBatteryPointer-1, :);

implantTemperatureData = implantTemperatureData(1:implantTemperaturePointer-1, :);
collarTemperatureData = collarTemperatureData(1:collarTemperaturePointer-1, :);

implantXAxisData = implantXAxisData(1:implantXAxisPointer-1, :);
implantYAxisData = implantYAxisData(1:implantYAxisPointer-1, :);
implantZAxisData = implantZAxisData(1:implantZAxisPointer-1, :);

collarXAxisData = collarXAxisData(1:collarXAxisPointer-1, :);
collarYAxisData = collarYAxisData(1:collarYAxisPointer-1, :);
collarZAxisData = collarZAxisData(1:collarZAxisPointer-1, :);

%figure('Name', destfile)
%subplot(4,1,1)
%plot(ecgData(:,2))
%subplot(4,1,2)
%plot(temperatureData(:,2))
%subplot(4,1,3)
%plot(battData(:,2))
%subplot(4,1,4)
%hold on
%plot(xAxisData(:,2),'color','r')
%plot(yAxisData(:,2),'color','b')
%plot(zAxisData(:,2),'color','g')
%hold off


disp('Writing result file.');
save(destfile,'ecgData','implantXAxisData','implantYAxisData','implantZAxisData','implantBatteryData','implantTemperatureData','collarXAxisData','collarYAxisData','collarZAxisData','collarTemperatureData','collarBatteryData','startTime');
disp('Completed');
clear all;
