%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%                                                                 %%%
%%%%%%%              Fluorometer Data file processing                   %%%
%%%%%%%                                                                 %%%
%%%%%%%                                                                 %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all;
clear all;

%ask for file to enter
a=input('Enter data file name:','s');
%read in text file 
[time,dataType,data]=textread(a,'%12s%s%n%*[^\n]','delimiter',',','headerlines',2);
%convert hr/min/sec to secs
time=convTime(time);
%make a cell array of data to sort
cellSt=[dataType,num2cell(time),num2cell(data)];
%sort by data type (temp, press, i.e.)
cellSt=Sortrows(cellSt,1);

GridData=[];
Data=[];
AveTime=[];
%put data in a grid format so each row is one data record
NumFields=12;

NumRows=size(cellSt,1);
RowsPerRec=NumRows/NumFields;
q={};
for i=1:NumFields,
    q=[q,cellSt(RowsPerRec*(i-1)+1:RowsPerRec*i,1:3)];
    GridData=[GridData,cell2mat(cellSt(RowsPerRec*(i-1)+1:RowsPerRec*i,2:3))];
end
 
%Grid Data format
%  time    data    time data
% make new array without any strings and with the sample time averaged
% time will be the ave time from all the samples, data will be a matrix
% where the columns are a data type and the rows are the samples
% the order of the columns for Data will be
% ave 1)fluor  2)humid   3) press 4)rpos(cnts) 5)rpos(mm) 6)tpos(cnts) 7)tpos(deg)
% 8.temp 9.xtilt 10.ytilt 11.zpos(cnts) 12. zpos(mm), the next modifation to Data will be
% to have only flur and the pos data in mm or deg and time

for i=1:size(GridData,2)/2,
    AveTime=[AveTime,GridData(:,1+2*(i-1))];
    Data=[Data,GridData(:,2+2*(i-1))];
end

time_data=[mean(AveTime,2),std(AveTime,0,2)];
% get rid of unnecess data columns
%the resulting data matrix will have the following column assignment 
% 1.time(ave) 2.fluor 3.rpos(mm) 4. tpos(deg) 5. zpos(mm)  
DataShort=[time_data(:,1),Data(:,1),Data(:,5),Data(:,7),Data(:,12)];
% remove large files not used
clear cellSt;
clear dataType;
clear data;
clear time;
clear GridData;

t=DataShort(:,1);
n=1;
T=DataShort(n:end,4);
Fl=DataShort(n:end,2);
R=DataShort(n:end,3);
R=R+410;
Z=DataShort(n:end,5);
[X,Y,Z]=pol2cart(T*pi/180,R,Z);

plot(t,Fl,'b',t,X./10,'r',t,Y./10,'g',t,Z./10,'c');
figure;plot(t,Fl,'b',t,(R-(max(R)-min(R)))./10,'r',t,T./10,'g');
Xa=linspace(min(X),max(X),100);
Ya=linspace(min(Y),max(Y),100);
[XI,YI]=meshgrid(Xa,Ya);
ZI=griddata(X,Y,Fl,XI,YI);
figure;mesh(XI,YI,ZI);
figure;[c,h]=contourf(XI,YI,ZI,25);
set(h,'linestyle','none');
figure;plot3(X,Y,Fl,'x-');
figure;polar(T*pi/180,R)
figure;
subplot(2,2,1);
plot3(X,Y,Fl,'x-');
xlabel('X pos (mm)');ylabel('Y pos (mm)');zlabel('Fluorescence');

subplot(2,2,2)
[c,h]=contourf(XI,YI,ZI,20);
set(h,'linestyle','none');
xlabel('X pos (mm)');ylabel('Y pos (mm)');zlabel('Fluorescence');

subplot(2,2,3)
mesh(XI,YI,ZI);
xlabel('X pos (mm)');ylabel('Y pos (mm)');zlabel('Fluorescence');
% subplot(2,2,3);hold on;
% plot3(X,Y,Fl,'xk');
break;
figure;
subplot(2,1,1);
[c,h]=contourf(XI,YI,ZI,20);
set(h,'linestyle','none');
xlabel('X pos (mm)');ylabel('Y pos (mm)');zlabel('Fluorescence');
colorbar;

subplot(2,1,2);
plot3(X,Y,Fl,'x-');
xlabel('X pos (mm)');ylabel('Y pos (mm)');zlabel('Fluorescence');


% 
% [newTheta,i,j]=unique(T);
% Fl=DataShort(i,2);
% R=DataShort(i,3);
% Z=DataShort(i,5);
% %map from cylindrical coordinates to cartesian
% [X,Y,Z]=pol2cart(newTheta*pi/180,R,Z);
% Xa=linspace(min(X),max(X),100);
% Ya=linspace(min(Y),max(Y),100);
% [XI,YI]=meshgrid(Xa,Ya);
% ZI=griddata(X,Y,Fl,XI,YI);
% figure;mesh(XI,YI,ZI)
% 
% figure; [c,h]=contourf(XI,YI,ZI,25);
% set(h,'linestyle','none')
% [newR,i,j]=unique(R);
% Fl=DataShort(i,2);
% T=DataShort(i,4);
% Z=DataShort(i,5);
% plot(newR,Fl,'x');
% xlabel('R pos (mm)');
% ylabel('Fluorescence');
% %map from cylindrical coordinates to cartesian
% [X,Y,Z]=pol2cart(T*pi/180,newR,Z);
% Xa=linspace(min(X),max(X),100);
% Ya=linspace(min(Y),max(Y),100);
% [XI,YI]=meshgrid(Xa,Ya);
% ZI=griddata(X,Y,Fl,XI,YI);
% figure;mesh(XI,YI,ZI)
% 
% figure; [c,h]=contourf(XI,YI,ZI,25);
% set(h,'linestyle','none')

[newZ,i,j]=unique(Z);
Fl=DataShort(i,2);
T=DataShort(i,4);
R=DataShort(i,3);
plot(newZ,Fl,'x');
xlabel('Z pos (mm)');
ylabel('Fluorescence');

%
% Zn=Z-113
% Zn=Zn.*-1
% Zn=Zn+26.5
% plot(Zn,Fl)
% plot(Zn,Fl,'x')
%xlabel('Z pos (mm)');
%ylabel('Fluorescence');
