%This program computes a 1-min-av slinding on 10 min.
% Finds the maximum 1-min mean over this 10 min period 
% occured and computes the 1 min wind direction for this minute (see lili
% for this)
clear
hurricane='frances';
fs=1;
files=dir(['C:\Documents and Settings\lorsolo\My Documents\research\',hurricane,'\data\tower data\*0100.mat']); 
original=files(1).name
 data=load(['C:\Documents and Settings\lorsolo\My Documents\research\',hurricane,'\data\tower data\',original]);
data=data.data;

yy=2004;%input('enter year yy=');
mm=9;%input('enter month  mm=');
dd=input('enter day  dd=');
hh=input('enter day  hh=');
mn=input('enter day  mn=');
s=input('enter day  s=');


ws_col=8;

ws1min_final=[]; % contains all the means for 1 min for a particular 10 min period
ws3s_final=[];
max1min=[]; % max of the 1min mean ws over 10min
max3s=[];

win_10min=fs*600; % # of data for 10 min
win_min=fs*60; % # of data for 1 min
win_3s=fs*3;
N=(win_10min-win_min)+1; % # of time we wil slide the 1min window in 10 min
N3s=(win_10min-win_3s)+1;
nwin=floor(length(data(:,1))/win_10min) % # of 10 min periods in the data set in the data set (no overlap)

std_10min=[];
mean_10min=[];

t0 = datenum(yy,mm,dd,hh,mn,s);
dt=datenum(yy,mm,dd,hh,mn+10,s)-t0; %10 min interval
mil=datenum(yy,mm,dd,hh,mn+5,s)-t0;%5 min interval to find time in the middle of the 10 min period
time=t0+mil:dt:t0+(nwin-1)*dt+mil;
t=datestr(time,31);
for n=0:nwin-1
    ws_10min=data(n*win_10min+1:(n+1)*win_10min,ws_col);
    std_10min=[std_10min;std(ws_10min(:,1))];
    mean_10min=[mean_10min;mean(ws_10min(:,1))];
    
    for i=1:N
        mean_1min=mean(ws_10min(1+(i-1):(i-1)+win_min,1));%sliding window over the 10 min under investigation
        ws1min_final=[ws1min_final;mean_1min];
        
    end
    for i=1:N3s
        mean_3s=mean(ws_10min(1+(i-1):(i-1)+win_3s,1));%sliding window over the 10 min under investigation
        ws3s_final=[ws3s_final;mean_3s];
    end
    max3s=[max3s;max(ws3s_final)];
    max1min=[max1min;max(ws1min_final)];
    ws1min_final=[];
    ws3s_final=[];
end

dd=str2num(t(:,9:10));

hh=str2num(t(:,12:13));
mn=str2num(t(:,15:16));
yy=str2num(t(:,1:4));
mm=str2num(t(:,6:7));
final=[yy(1:length(mean_10min),1) mm(1:length(mean_10min),1) dd(1:length(mean_10min),1) hh(1:length(mean_10min),1) mn(1:length(mean_10min),1) mean_10min max1min max3s];

z0=exp(log(10)-(mean_10min./std_10min));
u_star=(mean_10min*0.4)./(log(10./z0));
u_star_openexp=((0.03./z0).^0.0706).*u_star;
Uadj=(u_star_openexp.*log(10/0.03))/0.4;

final=[yy(1:length(mean_10min),1) mm(1:length(mean_10min),1) dd(1:length(mean_10min),1) hh(1:length(mean_10min),1) mn(1:length(mean_10min),1) mean_10min Uadj z0 max1min max3s];


%result=[mod_data(:,1:4) mod_data(:,6:9) final(4:length(mod_data)+3,6:7)];
%keep result
%save('C:\Documents and Settings\lorsolo\My Documents\research\rita\data\Hwind_White.dat','result','-ascii','-tabs')
