clc
clear

ro=998;
d=0.30;
a=0.3*0.3;
v=0.6;


output1=fopen('N=2,P=2.5,V=0.6,d=1in,X=1D,Y=0,Ct,Cp.plt','w');
output2=fopen('N=2,P=2.5,V=0.6,d=1in,X=1D,Y=0,Ct,Cp-mean.plt','w');

root='tsr=';
exten1='.CSV';

fprintf(output2,'variables= "tsr","RPM","Mean torque","Mean Ct","Mean Power","Mean Cp" \r\n');


tests=[0.5,1.0,1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.0,3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8];
n=length(tests);


%%
%%%%%%%%
%offset 1
%%%%%%%%
fid=fopen('ofset-1.CSV');
line1=textscan(fid,'%s %*[^\n]',1);
line2=textscan(fid,'%s%*[^\n]',1);
line3=textscan(fid,'%s%*[^\n]',1);
line4=textscan(fid,'%s%*[^\n]',1);
line5=textscan(fid,'%s%*[^\n]',1);
data=textscan(fid,'%f%f%f%f%*[^\n]','delimiter', ',');
time_offset1=data{1}(:);
torque_volt_offset1=data{2}(:);
RPM_volt_offset1=data{3}(:);
position_volt_offset1=data{4}(:);

%%
%%%%%%%%
%offset 2
%%%%%%%%
fid=fopen('ofset-2.CSV');
line1=textscan(fid,'%s %*[^\n]',1);
line2=textscan(fid,'%s%*[^\n]',1);
line3=textscan(fid,'%s%*[^\n]',1);
line4=textscan(fid,'%s%*[^\n]',1);
line5=textscan(fid,'%s%*[^\n]',1);
data=textscan(fid,'%f%f%f%f%*[^\n]','delimiter', ',');
time_offset2=data{1}(:);
torque_volt_offset2=data{2}(:);
RPM_volt_offset2=data{3}(:);
position_volt_offset2=data{4}(:);

%%
%%%%%%%%%
%data extraction
%%%%%%%%%
for i=1:n
    tsr(i)=tests(i);    
    num=num2str(tests(i),'%3.1f');
    file_name1=strcat(root,num,exten1);
    vel(i)=v;
    
    fid=fopen(file_name1);
    line1=textscan(fid,'%s %*[^\n]',1);
    line2=textscan(fid,'%s%*[^\n]',1);
    line3=textscan(fid,'%s%*[^\n]',1);
    line4=textscan(fid,'%s%*[^\n]',1);
    line5=textscan(fid,'%s%*[^\n]',1);
    data=textscan(fid,'%f%f%f%f%*[^\n]','delimiter', ',');
    time(:,i)=data{1}(:);
    torque_volt(:,i)=data{2}(:);
    RPM_volt(:,i)=data{3}(:);
    position_volt(:,i)=data{4}(:);
%%
    %%%%%%%%
    %calculations
    %%%%%%%%    
    torque_offset=mean(0.5*(torque_volt_offset1+torque_volt_offset2));
    RPM_offset=mean(0.5*(RPM_volt_offset1+RPM_volt_offset2));
    torque(:,i)=(torque_volt(:,i)-torque_offset);
    torque_coef(:,i)=torque(:,i)/(0.5*ro*a*(d/2)*vel(i)^2);
    torque_mean(i)=mean(torque(:,i));
    torque_coef_mean(i)=torque_mean(i)/(0.5*ro*a*(d/2)*vel(i)^2);    
    
    RPM(:,i)=(RPM_volt(:,i)-RPM_offset)*30;
    RPM_mean(i)=mean(RPM(:,i));
    
    angular_velocity(:,i)=(RPM(:,i)/60)*2*pi; %(rad/s)
    angular_velocity_mean(i)=mean(angular_velocity(:,i));
    
    power(:,i)=angular_velocity(:,i).*torque(:,i);
    power_coef(:,i)=power(:,i)/(0.5*ro*a*vel(i)^3);
    power_mean(i)=mean(power(:,i));
    power_coef_mean(i)=power_mean(i)/(0.5*ro*a*vel(i)^3);
    position(:,i)=position_volt(:,i)*36;
%%
    %%%%%%%%%%%%%%%
    %Output files
    %%%%%%%%%%%%%%%
    fprintf(output1,'variables= "time","Position","Torque","Ct","Power","Cp","RPM","angular velocity" \r\n');
    fprintf(output1,'zone t="tsr=%3.1f" \r\n',tsr(i));
    vector=[transpose(time(:,i));transpose(position(:,i));transpose(torque(:,i));transpose(torque_coef(:,i));transpose(power(:,i));...
    transpose(power_coef(:,i));transpose(RPM(:,i));transpose(angular_velocity(:,i))];
    fprintf(output1,'%f  %f  %f  %f  %f  %f  %f  %f\n',vector);
    fprintf(output2,'%f  %f  %f  %f  %f  %f  \n',tsr(i),RPM_mean(i),torque_mean(i),torque_coef_mean(i),power_mean(i),power_coef_mean(i));
end

fclose('all');    

close
