% read in Second wind data from file capture off wev (CSV)
clear all;
fclose all;
close all;

%s='C:\Data\CRUISES\Icealot\Second_wind_Frederick.csv';
%s='Z:\BAO\Tower\Second_Wind\Raw\Second_wind_Frederick.csv';
%s='Z:\BAO\Tower\Second_Wind\Raw\20081107.csv';
s='Z:\BAO\Tower\Second_Wind\Raw\20081107_19.csv';
%s='Z:\BAO\Tower\Second_Wind\Raw\20081113_14.csv';

fo = 'Z:\BAO\Tower\Second_Wind\Raw_Images\';
pltit = 1;


%%%%%%%%%%%%%%%%%%%%%%%%%
%reads in the data file.  
%%%%%%%%%%%%%%%%%%%%%%%%%
flist=fopen(s,'r');
temp =textscan(flist,'%f %s %f %f %f %s %f %s %f %f  %*[^\n]', 'headerlines', 1,'delimiter',',',  'emptyvalue', NaN);

%%%%%%%%%%%%%%%%%%%%%%%%%
%variable indentification
%%%%%%%%%%%%%%%%%%%%%%%%%
%variable temp contains double array and cell array of strings.
inputID=temp{1};
input=temp{2};             %cell array of strings
logger=temp{3};
stationheight=temp{4};
stationazimuth=temp{5};
functionOf=temp{6};        %cell array of strings
functionInterval=temp{7};
functionName=temp{8};      %cell array of strings
timestamp=temp{9};
datavalue=temp{10};

%%%%%%%%%%%%%%%%%%%%%%%%%
%select function and plot data
%%%%%%%%%%%%%%%%%%%%%%%%%
%here use of cellstr to create cell array of strings from character array,
%to be able to compare to variable functionOf

ii = (strcmp(functionOf, cellstr('Temperature')) & strcmp(functionName, cellstr('Average')));   %7492 values
T(:,1) = timestamp(ii);
T(:,2) = datavalue(ii);
T(:,3) = stationheight(ii);
T(:,4) = stationazimuth(ii);

t50 = find(T(:,3) == 50);
t100 = find(T(:,3) == 100);
t150 = find(T(:,3) == 150);
t200 = find(T(:,3) == 200);
%%
ll = length(t50);
%%%%% Calculate date/time
for i = 1:ll
            y(i) = T(t50(i),1)/(60*60*24);
            z(i) = y(i)+datenum([1996,1,1,0,0,0]);
            dt = datestr(z(i), 31);    %2008-10-07 16:35:53
            yr(i) = str2num(dt(1:4));
            month(i) = str2num(dt(6:7));
            day(i) = str2num(dt(9:10));
            hour(i) = str2num(dt(12:13));
            minutes(i) = str2num(dt(15:16));
            secs(i) = str2num(dt(18:19));
end
            jd50 = datenum(yr,month,day,hour,minutes,secs)-datenum(yr-1,12,31);
         
            clear y z dt yr month day hour minutes secs

ll = length(t100);
%%%%% Calculate date/time
for i = 1:ll
            y(i) = T(t100(i),1)/(60*60*24);
            z(i) = y(i)+datenum([1996,1,1,0,0,0]);
            dt = datestr(z(i), 31);    %2008-10-07 16:35:53
            yr(i) = str2num(dt(1:4));
            month(i) = str2num(dt(6:7));
            day(i) = str2num(dt(9:10));
            hour(i) = str2num(dt(12:13));
            minutes(i) = str2num(dt(15:16));
            secs(i) = str2num(dt(18:19));
end
            jd100 = datenum(yr,month,day,hour,minutes,secs)-datenum(yr-1,12,31);
            
            clear y z dt yr month day hour minutes secs

     ll = length(t150);
%%%%% Calculate date/time
for i = 1:ll
            y(i) = T(t150(i),1)/(60*60*24);
            z(i) = y(i)+datenum([1996,1,1,0,0,0]);
            dt = datestr(z(i), 31);    %2008-10-07 16:35:53
            yr(i) = str2num(dt(1:4));
            month(i) = str2num(dt(6:7));
            day(i) = str2num(dt(9:10));
            hour(i) = str2num(dt(12:13));
            minutes(i) = str2num(dt(15:16));
            secs(i) = str2num(dt(18:19));
end
            jd150 = datenum(yr,month,day,hour,minutes,secs)-datenum(yr-1,12,31);
            
            clear y z dt yr month day hour minutes secs
            
            ll = length(t200);
%%%%% Calculate date/time
for i = 1:ll
            y(i) = T(t200(i),1)/(60*60*24);
            z(i) = y(i)+datenum([1996,1,1,0,0,0]);
            dt = datestr(z(i), 31);    %2008-10-07 16:35:53
            yr(i) = str2num(dt(1:4));
            month(i) = str2num(dt(6:7));
            day(i) = str2num(dt(9:10));
            hour(i) = str2num(dt(12:13));
            minutes(i) = str2num(dt(15:16));
            secs(i) = str2num(dt(18:19));
end
            jd200 = datenum(yr,month,day,hour,minutes,secs)-datenum(yr-1,12,31);

%%
bd = 322;
jds = num2str(bd)
ed = bd + 1;
bt = -5;
et = 10;

ST50(:,1) = jd50;
ST50(:,2) = T(t50,2);
ST100(:,1) = jd100;
ST100(:,2) = T(t100,2);
ST150(:,1) = jd150;
ST150(:,2) = T(t150,2);
ST200(:,1) = jd200;
ST200(:,2) = T(t200,2);

figure(1);plot(ST50(:,1),ST50(:,2),'k');
hold
figure(1);plot(ST100(:,1),T(t100,2),'b');
figure(1);plot(ST150(:,1),ST150(:,2),'r');
figure(1);plot(ST200(:,1),ST200(:,2),'g');
legend('50m','100m','150m','200m','Location','Best')
xlim([bd ed ])
ylabel('Temperature (C)');
xlabel('Time');
title('Second Wind BAO');
if pltit
    print_buffer = [fo '2wnd_T_' jds '.png'];
    print('-dpng ', print_buffer);
end


clear ii
ii = (strcmp(functionOf, cellstr('Direction')) & strcmp(functionName, cellstr('Vec Average')));   %7492 values
D(:,1) = timestamp(ii);
D(:,2) = datavalue(ii);
D(:,3) = stationheight(ii);
D(:,4) = stationazimuth(ii);

d50 = find(D(:,3) == 50);
d100 = find(D(:,3) == 100);
d150 = find(D(:,3) == 150);
d200 = find(D(:,3) == 200);

SD50(:,1) = jd50;
SD50(:,2) = D(d50,2);
SD100(:,1) = jd100;
SD100(:,2) = D(d100,2);
SD150(:,1) = jd150;
SD150(:,2) = D(d150,2);
SD200(:,1) = jd200;
SD200(:,2) = D(d200,2);

figure(2);plot(SD50(:,1),SD50(:,2),'k');
hold
figure(2);plot(SD100(:,1),SD100(:,2),'b');
figure(2);plot(SD150(:,1),SD150(:,2),'r');
figure(2);plot(SD200(:,1),SD200(:,2),'g');
legend('50m','100m','150m','200m','Location','Best')
ylabel('Direction (True)');
xlabel('Time');
xlim([bd ed ])
title('Second Wind BAO');
if pltit
    print_buffer = [fo '2wnd_WD_' jds '.png'];
    print('-dpng ', print_buffer);
end

%figure;plot(timestamp(ii),datavalue(ii),'.');title('Direction')

clear ii
ii = (strcmp(functionOf, cellstr('Speed')) & strcmp(functionName, cellstr('Average')) & stationazimuth == 315);   %7492 values
Sn(:,1) = timestamp(ii);
Sn(:,2) = datavalue(ii);
Sn(:,3) = stationheight(ii);
Sn(:,4) = stationazimuth(ii);
sn50 = find(Sn(:,3) == 50);
sn100 = find(Sn(:,3) == 100);
sn150 = find(Sn(:,3) == 150);
sn200 = find(Sn(:,3) == 200);

SSn50(:,1) = jd50;
SSn50(:,2) = Sn(sn50,2);
SSn100(:,1) = jd100;
SSn100(:,2) = Sn(sn100,2);
SSn150(:,1) = jd150;
SSn150(:,2) = Sn(sn150,2);
SSn200(:,1) = jd200;
SSn200(:,2) = Sn(sn200,2);

figure(3);plot(SSn50(:,1),SSn50(:,2),'k');
hold
figure(3);plot(SSn100(:,1),SSn100(:,2),'b');
figure(3);plot(SSn150(:,1),SSn150(:,2),'r');
figure(3);plot(SSn200(:,1),SSn200(:,2),'g');
legend('50m','100m','150m','200m','Location','Best')
ylabel('North Speed (m/s)');
xlabel('Time');
title('Second Wind BAO');
xlim([bd ed ])
if pltit
    print_buffer = [fo '2wnd_WS_N_' jds '.png'];
    print('-dpng ', print_buffer);
end

clear ii
ii = (strcmp(functionOf, cellstr('Speed')) & strcmp(functionName, cellstr('Average')) & stationazimuth == 135);   %7492 values
Ss(:,1) = timestamp(ii);
Ss(:,2) = datavalue(ii);
Ss(:,3) = stationheight(ii);
Ss(:,4) = stationazimuth(ii);
ss50 = find(Ss(:,3) == 50);
ss100 = find(Ss(:,3) == 100);
ss150 = find(Ss(:,3) == 150);
ss200 = find(Ss(:,3) == 200);

SSs50(:,1) = jd50;
SSs50(:,2) = Sn(ss50,2);
SSs100(:,1) = jd100;
SSs100(:,2) = Sn(ss100,2);
SSs150(:,1) = jd150;
SSs150(:,2) = Sn(ss150,2);
SSs200(:,1) = jd200;
SSs200(:,2) = Sn(ss200,2);


figure(4);plot(SSs50(:,1),SSs50(:,2),'k');
hold
figure(4);plot(SSs100(:,1),SSs100(:,2),'b');
figure(4);plot(SSs150(:,1),SSs150(:,2),'r');
figure(4);plot(SSs150(:,1),SSs150(:,2),'g');
legend('50m','100m','150m','200m','Location','Best')
ylabel('South Speed (m/s)');
xlabel('Time');
xlim([bd ed ])
title('Second Wind BAO');
if pltit
    print_buffer = [fo '2wnd_WS_S_' jds '.png'];
    print('-dpng ', print_buffer);
end
