tic

%Script for analysing ADCP data to give dates, depths, velocities,
%turbulence, directions as timeseries and max/mean/min values

%ACTIONS%

%1)line 22 change 'ii=1:n;' change n to number of bins recorded in data
%2)line 24 change 'fname=('n')' change n to name of data files to be loaded
%3)line 25 change 'datadir=strcat('n\')' change n to destination for saved
%results
%4)line 27 change 'Bins=n' change n to number of bins recorded in data
%5)line 28 change 'Dis1stBin=n' change n to distance to first bin (metres)
%6)line 29 change 'BH=n' change n to bin height (metres)
%7)line 30 change 'BN=[1,n]' change array to inclued amount of bins
%recorded
%8)line 31 change 'MD=n' change n to minimum water depth cut of point
%(decimetres)

%Defining inputs%

for ii = 1:27;
 
fname=('SPD1_');
datadir=strcat ('U:\');%LG2\DP4\Static\Max&Means\');

Bins=27;
Dis1stBin=2.33;
BH=1;
BN=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27];
MD=100; %select minimum depth cut off point

%Loading Files%

WH=load([fname num2str(ii) '.000'],'-ascii'); %loading raw ADCP velocity data file
DH=load([fname 'DEP' '.000'],'ascii');%loading raw depth file
WHD=load([fname 'D' '.000'],'-ascii');%loading raw dates file

%Sections datalengths Info%

 % for seconds then a 10min block would be 600 hence 601 (to get a
 % midpoint)
 
 X=DH(3601:length(DH));%just to get the data length with beginning cut off
 ta=601; %time average (ie moving average over block of ...eg 11(points, work out time, so here is only about 5.5seconds as each sample is 1/2second)
 bt=(ta+1)/2; %beginning midpoint of time average block
 datalengthfull=(length(X)-2); %choose data looking at
 datalength=datalengthfull-(ta-1); %length of data covered by time avereage blocks
 et=datalengthfull-((ta-1)/2); %end midpoint of time average block
 
%Depth Files

Depths=DH(3601:length(DH));%Select Depth
for i = 1:length(Depths);
    
    if (Depths(i) < MD);
    Depths(i)=NaN;  
else
    Depths(i)=Depths(i);
    end
end

Depths=Depths./10;%converting to metres
for i=1:length(Depths);
    Depth(i)=Depths(i)+0.5;
end %adding ADCP height

for i=1:Bins
    BinDepth(i)=Dis1stBin+((BN(i)-1)*BH)*cosd(20);
end
 
DEPTH=Depth(1:(length(Depth)-2));%Date length for full data (instantaneous vel)
SDEPTH=DEPTH(bt:et);%Date length for shortened data for 1st rolling vel means and TFs
SDEPTH2=SDEPTH(1:600:length(SDEPTH)); %Dates for moving vel mean
XSDEPTH=SDEPTH(bt:et-(ta-1));%Date length for even more shortened data for rolling TF means
XSDEPTH2=XSDEPTH(1:600:length(XSDEPTH)); %Dates for moving TF mean

save(strcat(datadir,'Depths_InstantVelo_',num2str(ii),'.mat'),'DEPTH');
save(strcat(datadir,'Depths_RollMeanVel_TurbFluc_',num2str(ii),'.mat'),'SDEPTH');
save(strcat(datadir,'Depths_MovMeanVelo_',num2str(ii),'.mat'),'SDEPTH2');
save(strcat(datadir,'Depths_RollMeanTF_',num2str(ii),'.mat'),'XSDEPTH');
save(strcat(datadir,'Depths_MovMeanTF_',num2str(ii),'.mat'),'XSDEPTH2');
save(strcat(datadir,'BinDepths_',num2str(ii),'.mat'),'BinDepth');
 

%Dates
 
Dates=WHD(:,1:6);
for i = 1:(length(Dates)); %changes date year from 0016 to 2016
    Dates(i,1)=Dates(i,1)+2000;
end

D=datenum(Dates);
DATES=D(3601:(length(D)-2));%Date length for full data (instantaneous vel)
SDATES=DATES(bt:et);%Date length for shortened data for 1st rolling vel means and TFs
SDATES2=SDATES(1:600:length(SDATES)); %Dates for moving vel mean
XSDATES=SDATES(bt:et-(ta-1));%Date length for even more shortened data for rolling TF means
XSDATES2=XSDATES(1:600:length(XSDATES)); %Dates for moving TF mean

save(strcat(datadir,'Dates_InstantVelo_',num2str(ii),'.mat'),'DATES');
save(strcat(datadir,'Dates_RollMeanVel_TurbFluc_',num2str(ii),'.mat'),'SDATES');
save(strcat(datadir,'Dates_MovMeanVelo_',num2str(ii),'.mat'),'SDATES2');
save(strcat(datadir,'Dates_RollMeanTF_',num2str(ii),'.mat'),'XSDATES');
save(strcat(datadir,'Dates_MovMeanTF_',num2str(ii),'.mat'),'XSDATES2');


%Raw Velocities%

e=WH(:,1); %Select East Velocity 
E=e(3601:length(e));
E=E./1000;
n=WH(:,2);
N=n(3601:length(n));%Select North Velocity
N=N./1000;
v=WH(:,3); 
V=v(3601:length(v));%Select Verticle Velocity
V=V./1000;
m=WH(:,4);
M=m(3601:length(m));%Select Magnitude
M=M./1000;

%Quality Control step 1

 for i = 1:length(E)


if (E(i) == 99.999);
    E(i)=NaN;  
else
    E(i)=E(i);
end
 end
 
  for i = 1:length(N)


if (N(i) == 99.999);
    N(i)=NaN;  
else
    N(i)=N(i);
end
  end

 
   for i = 1:length(V)


if (V(i) == 99.999);
    V(i)=NaN;  
else
    V(i)=V(i);
end
   end
 
    for i = 1:length(M)


if (M(i) == 99.999);
    M(i)=NaN;  
else
    M(i)=M(i);
end
    end
    
    
%Quality Control Step 2
    
%Calulate the acceleration between consucutive points
for i=1:(length(E)-1);
    
AccelerationE(i) =(E(i+1)-E(i))/1;

end

%Calculate the rate of change between consucutive points

for i=1:(length(AccelerationE)-1);
    
Rate_of_ChangeE(i) =(AccelerationE(i+1)-AccelerationE(i))/1;

end
%Determine a percentile in which things look erroneous

PercentileE=prctile(Rate_of_ChangeE,99.99);

%Take gradient of absolute velocity
%Apply where gradient is positive to avoid taking pre/post points out as
%well

AbsE=sqrt(E.^2);

for i=1:(length(AbsE)-1);
    
GradientE(i) =(AbsE(i+1)-AbsE(i))/1;

end

for i = 1:length(Rate_of_ChangeE)


if (Rate_of_ChangeE(i) > (PercentileE))&&(GradientE(i) <= 0);
    EV(i)=NaN; 
elseif (Rate_of_ChangeE(i) < (-1*PercentileE))&&(GradientE(i) <= 0);
    EV(i)=NaN; 
else
    EV(i)=E(i);
end
end

EV=transpose(EV);

%Calulate the acceleration between consucutive points
for i=1:(length(N)-1);
    
AccelerationN(i) =(N(i+1)-N(i))/1;

end

%Calculate the rate of change between consucutive points

for i=1:(length(AccelerationN)-1);
    
Rate_of_ChangeN(i) =(AccelerationN(i+1)-AccelerationN(i))/1;

end
%Determine a percentile in which things look erroneous

PercentileN=prctile(Rate_of_ChangeN,99.99);

%Take gradient of absolute velocity
%Apply where gradient is positive to avoid taking pre/post points out as
%well

AbsN=sqrt(N.^2);

for i=1:(length(AbsN)-1);
    
GradientN(i) =(AbsN(i+1)-AbsN(i))/1;

end

for i = 1:length(Rate_of_ChangeN)


if (Rate_of_ChangeN(i) > (PercentileN))&&(GradientN(i) <= 0);
    NV(i)=NaN; 
elseif (Rate_of_ChangeN(i) < (-1*PercentileN))&&(GradientN(i) <= 0);
    NV(i)=NaN; 
else
    NV(i)=N(i);
end
end

NV=transpose(NV);

%Calulate the acceleration between consucutive points
for i=1:(length(V)-1);
    
AccelerationV(i) =(V(i+1)-V(i))/1;

end

%Calculate the rate of change between consucutive points

for i=1:(length(AccelerationV)-1);
    
Rate_of_ChangeV(i) =(AccelerationV(i+1)-AccelerationV(i))/1;

end
%Determine a percentile in which things look erroneous

PercentileV=prctile(Rate_of_ChangeV,99.99);

%Take gradient of absolute velocity
%Apply where gradient is positive to avoid taking pre/post points out as
%well

AbsV=sqrt(V.^2);

for i=1:(length(AbsV)-1);
    
GradientV(i) =(AbsV(i+1)-AbsV(i))/1;

end

for i = 1:length(Rate_of_ChangeV)


if (Rate_of_ChangeV(i) > (PercentileV))&&(GradientV(i) <= 0);
    VV(i)=NaN; 
elseif (Rate_of_ChangeV(i) < (-1*PercentileV))&&(GradientV(i) <= 0);
    VV(i)=NaN; 
else
    VV(i)=V(i);
end
end

VV=transpose(VV);

%Calulate the acceleration between consucutive points
for i=1:(length(M)-1);
    
AccelerationM(i) =(M(i+1)-M(i))/1;

end

%Calculate the rate of change between consucutive points

for i=1:(length(AccelerationM)-1);
    
Rate_of_ChangeM(i) =(AccelerationM(i+1)-AccelerationM(i))/1;

end
%Determine a percentile in which things look erroneous

PercentileM=prctile(Rate_of_ChangeM,99.99);

%Take gradient of absolute velocity
%Apply where gradient is positive to avoid taking pre/post points out as
%well

AbsM=sqrt(M.^2);

for i=1:(length(AbsM)-1);
    
GradientM(i) =(AbsM(i+1)-AbsM(i))/1;

end

for i = 1:length(Rate_of_ChangeM)


if (Rate_of_ChangeM(i) > (PercentileM))&&(GradientM(i) <= 0);
    MV(i)=NaN; 
elseif (Rate_of_ChangeM(i) < (-1*PercentileM))&&(GradientM(i) <= 0);
    MV(i)=NaN; 
else
    MV(i)=M(i);
end
end

MV=transpose(MV);


% INSTANTANEOUS VELOCITIES
 
%Positive sets

EP=sqrt(EV.^2);
NP=sqrt(NV.^2);
VP=sqrt(VV.^2);
MP=sqrt(MV.^2);
 
%Maxs/Means/Mins

MxE=max(EV);
MnE=min(EV);
MeE=nanmean(EV);
SdE=nanstd(EV);
MXE=max(EP);
MNE=min(EP);
MEE=nanmean(EP);
SDE=nanstd(EP);

MxN=max(NV);
MnN=min(NV);
MeN=nanmean(NV);
SdN=nanstd(NV);
MXN=max(NP);
MNN=min(NP);
MEN=nanmean(NP);
SDN=nanstd(NP);

MxV=max(VV);
MnV=min(VV);
MeV=nanmean(VV);
SdV=nanstd(VV);
MXV=max(VP);
MNV=min(VP);
MEV=nanmean(VP);
SDV=nanstd(VP);

MxM=max(MV);
MnM=min(MV);
MeM=nanmean(MV);
SdM=nanstd(MV);
MXM=max(MP);
MNM=min(MP);
MEM=nanmean(MP);
SDM=nanstd(MP);


%saving files of Instantanous Velocity data
save(strcat(datadir,'InstantVelo_',num2str(ii),'.mat'),'EV','NV','VV','MV');
save(strcat(datadir,'InstantVeloE_',num2str(ii),'.mat'),'MxE','MnE','MeE','SdE','MXE','MNE','MEE','SDE');
save(strcat(datadir,'InstantVeloN_',num2str(ii),'.mat'),'MxN','MnN','MeN','SdN','MXN','MNN','MEN','SDN');
save(strcat(datadir,'InstantVeloV_',num2str(ii),'.mat'),'MxV','MnV','MeV','SdV','MXV','MNV','MEV','SDV');
save(strcat(datadir,'InstantVeloM_',num2str(ii),'.mat'),'MxM','MnM','MeM','SdM','MXM','MNM','MEM','SDM');


%ROLLING AVERAGE VELOCITIES
    
 %10minute moving average East

for i=1:datalength;
    MeanE (i)=mean (EV(i:i+(ta-1),1)); %data looking at (same as first above)
end

%10minute moving averge North

for i=1:datalength; 
    MeanN (i)=mean (NV(i:i+(ta-1),1)); %data looking at (same as first above)
end

%10minute moving average Verticle

for i=1:datalength; 
    MeanV (i)=mean (VV(i:i+(ta-1),1)); %data looking at (same as first above)
end

%recalucualted magnitude

for i=1:datalength;
    MeanM(i)=sqrt((MeanE(i).^2)+(MeanN(i).^2)+(MeanV(i).^2));
end

%Positive Sets
MeanEP=sqrt(MeanE.^2);
MeanNP=sqrt(MeanN.^2);
MeanVP=sqrt(MeanV.^2);
MeanMP=sqrt(MeanM.^2);

%Maxs/Means/Mins

MxME=max(MeanE);
MnME=min(MeanE);
MeME=nanmean(MeanE);
SdME=nanstd(MeanE);
MXME=max(MeanEP);
MNME=min(MeanEP);
MEME=nanmean(MeanEP);
SDME=nanstd(MeanEP);

MxMN=max(MeanN);
MnMN=min(MeanN);
MeMN=nanmean(MeanN);
SdMN=nanstd(MeanN);
MXMN=max(MeanNP);
MNMN=min(MeanNP);
MEMN=nanmean(MeanNP);
SDMN=nanstd(MeanNP);

MxMV=max(MeanV);
MnMV=min(MeanV);
MeMV=nanmean(MeanV);
SdMV=nanstd(MeanV);
MXMV=max(MeanVP);
MNMV=min(MeanVP);
MEMV=nanmean(MeanVP);
SDMV=nanstd(MeanVP);

MxMM=max(MeanM);
MnMM=min(MeanM);
MeMM=nanmean(MeanM);
SdMM=nanstd(MeanM);
MXMM=max(MeanMP);
MNMM=min(MeanMP);
MEMM=nanmean(MeanMP);
SDMM=nanstd(MeanMP);

%saving files for Rolling Mean Velocity Data
save(strcat(datadir,'RollMeanVelo_',num2str(ii),'.mat'),'MeanE','MeanN','MeanV','MeanM');
save(strcat(datadir,'RollMeanVeloE_',num2str(ii),'.mat'),'MxME','MnME','MeME','SdME','MXME','MNME','MEME','SDME');
save(strcat(datadir,'RollMeanVeloN_',num2str(ii),'.mat'),'MxMN','MnMN','MeMN','SdMN','MXMN','MNMN','MEMN','SDMN');
save(strcat(datadir,'RollMeanVeloV_',num2str(ii),'.mat'),'MxMV','MnMV','MeMV','SdMV','MXMV','MNMV','MEMV','SDMV');
save(strcat(datadir,'RollMeanVeloM_',num2str(ii),'.mat'),'MxMM','MnMM','MeMM','SdMM','MXMM','MNMM','MEMM','SDMM');


%MOVING AVERAGE VELOCITIES

%Moving average (every 10minutes)

MMeanE=MeanE(1:600:length(MeanE));
MMeanN=MeanN(1:600:length(MeanN));
MMeanV=MeanV(1:600:length(MeanV));
MMeanM=MeanM(1:600:length(MeanM));

%Positive Sets
MMeanEP=sqrt(MMeanE.^2);
MMeanNP=sqrt(MMeanN.^2);
MMeanVP=sqrt(MMeanV.^2);
MMeanMP=sqrt(MMeanM.^2);

%Maxs/Means/Mins

MxMME=max(MMeanE);
MnMME=min(MMeanE);
MeMME=nanmean(MMeanE);
SdMME=nanstd(MMeanE);
MXMME=max(MMeanEP);
MNMME=min(MMeanEP);
MEMME=nanmean(MMeanEP);
SDMME=nanstd(MMeanEP);

MxMMN=max(MMeanN);
MnMMN=min(MMeanN);
MeMMN=nanmean(MMeanN);
SdMMN=nanstd(MMeanN);
MXMMN=max(MMeanNP);
MNMMN=min(MMeanNP);
MEMMN=nanmean(MMeanNP);
SDMMN=nanstd(MMeanNP);

MxMMV=max(MMeanV);
MnMMV=min(MMeanV);
MeMMV=nanmean(MMeanV);
SdMMV=nanstd(MMeanV);
MXMMV=max(MMeanVP);
MNMMV=min(MMeanVP);
MEMMV=nanmean(MMeanVP);
SDMMV=nanstd(MMeanVP);

MxMMM=max(MMeanM);
MnMMM=min(MMeanM);
MeMMM=nanmean(MMeanM);
SdMMM=nanstd(MMeanM);
MXMMM=max(MMeanMP);
MNMMM=min(MMeanMP);
MEMMM=nanmean(MMeanMP);
SDMMM=nanstd(MMeanMP);

%saving files Moving Mean Velocity Data
save(strcat(datadir,'MovMeanVelo_',num2str(ii),'.mat'),'MMeanE','MMeanN','MMeanV','MMeanM');
save(strcat(datadir,'MovMeanVeloE_',num2str(ii),'.mat'),'MxMME','MnMME','MeMME','SdMME','MXMME','MNMME','MEMME','SDMME');
save(strcat(datadir,'MovMeanVeloN_',num2str(ii),'.mat'),'MxMMN','MnMMN','MeMMN','SdMMN','MXMMN','MNMMN','MEMMN','SDMMN');
save(strcat(datadir,'MovMeanVeloV_',num2str(ii),'.mat'),'MxMMV','MnMMV','MeMMV','SdMMV','MXMMV','MNMMV','MEMMV','SDMMV');
save(strcat(datadir,'MovMeanVeloM_',num2str(ii),'.mat'),'MxMMM','MnMMM','MeMMM','SdMMM','MXMMM','MNMMM','MEMMM','SDMMM');

   
%INSTANTANEOUS TURBULENCE FLUCTUATIONS

%Turbulence fluctuations calculations (U(y1/SerEmmpersec (instantaneous/recorded u))=u'(tf)+u(bar)(m))

%Turbulence Fluctutations East

yE = EV (bt:et,1);

MEANE=transpose(MeanE);

TFE=yE-MEANE;

%Turbulence Fluctuations North

yN = NV (bt:et,1);

MEANN=transpose(MeanN);

TFN=yN-MEANN;

%Turbulence Fluctuations Verticle

yV = VV (bt:et,1);

MEANV=transpose(MeanV);

TFV=yV-MEANV;

%Turbulenc Fluctuation Magnitude (recaulated from componants)

for i=1:datalength;
TFM(i)=sqrt((TFE(i).^2)+(TFN(i).^2)+(TFV(i).^2));
end;

%Positive Sets
TFEP=sqrt(TFE.^2);
TFNP=sqrt(TFN.^2);
TFVP=sqrt(TFV.^2);
TFMP=sqrt(TFM.^2);

%Maxs/Means/Mins

MxTFE=max(TFE);
MnTFE=min(TFE);
MeTFE=nanmean(TFE);
SdTFE=nanstd(TFE);
MXTFE=max(TFEP);
MNTFE=min(TFEP);
METFE=nanmean(TFEP);
SDTFE=nanstd(TFEP);

MxTFN=max(TFN);
MnTFN=min(TFN);
MeTFN=nanmean(TFN);
SdTFN=nanstd(TFN);
MXTFN=max(TFNP);
MNTFN=min(TFNP);
METFN=nanmean(TFNP);
SDTFN=nanstd(TFNP);

MxTFV=max(TFV);
MnTFV=min(TFV);
MeTFV=nanmean(TFV);
SdTFV=nanstd(TFV);
MXTFV=max(TFVP);
MNTFV=min(TFVP);
METFV=nanmean(TFVP);
SDTFV=nanstd(TFVP);

MxTFM=max(TFM);
MnTFM=min(TFM);
MeTFM=nanmean(TFM);
SdTFM=nanstd(TFM);
MXTFM=max(TFMP);
MNTFM=min(TFMP);
METFM=nanmean(TFMP);
SDTFM=nanstd(TFMP);

%saving files Instantaneous Turbulence Fluctuation Data
save(strcat(datadir,'TurbulenceFluc_',num2str(ii),'.mat'),'TFE','TFN','TFV','TFM');
save(strcat(datadir,'TurbulenceFlucE_',num2str(ii),'.mat'),'MxTFE','MnTFE','MeTFE','SdTFE','MXTFE','MNTFE','METFE','SDTFE');
save(strcat(datadir,'TurbulenceFlucN_',num2str(ii),'.mat'),'MxTFN','MnTFN','MeTFN','SdTFN','MXTFN','MNTFN','METFN','SDTFN');
save(strcat(datadir,'TurbulenceFlucV_',num2str(ii),'.mat'),'MxTFV','MnTFV','MeTFV','SdTFV','MXTFV','MNTFV','METFV','SDTFV');
save(strcat(datadir,'TurbulenceFlucM_',num2str(ii),'.mat'),'MxTFM','MnTFM','MeTFM','SdTFM','MXTFM','MNTFM','METFM','SDTFM');


%ROLLING 10 MINUTE AVERAGE TURBULENCE FLUCTUATIONS

shortdata=datalength-(ta-1);

%East

for i=1:shortdata; 
    meanTFE (i)=mean (TFE(i:i+(ta-1),1));
end

MeanTFE=transpose(meanTFE);

%North

for i=1:shortdata;
    meanTFN (i)=mean (TFN(i:i+(ta-1),1));
end

MeanTFN=transpose(meanTFN);

%Verticle

for i=1:shortdata; 
    meanTFV (i)=mean (TFV(i:i+(ta-1),1)); 
end

MeanTFV=transpose(meanTFV);

%Magnitude (recalculated from componants)

for i=1:shortdata; 
    meanTFM (i)=sqrt((meanTFE(i)^2)+(meanTFN(i)^2)+(meanTFV(i)^2)); %data looking at (same as first above)was E (3917) now TFE (2717)
end

MeanTFM=transpose(meanTFM);%may not need this line see once run

%Positive Sets
MeanTFEP=sqrt(MeanTFE.^2);
MeanTFNP=sqrt(MeanTFN.^2);
MeanTFVP=sqrt(MeanTFV.^2);
MeanTFMP=sqrt(MeanTFM.^2);

%Maxs/Means/Mins

MxMTFE=max(MeanTFE);
MnMTFE=min(MeanTFE);
MeMTFE=nanmean(MeanTFE);
SdMTFE=nanstd(MeanTFE);
MXMTFE=max(MeanTFEP);
MNMTFE=min(MeanTFEP);
MEMTFE=nanmean(MeanTFEP);
SDMTFE=nanstd(MeanTFEP);

MxMTFN=max(MeanTFN);
MnMTFN=min(MeanTFE);
MeMTFN=nanmean(MeanTFN);
SdMTFN=nanstd(MeanTFN);
MXMTFN=max(MeanTFNP);
MNMTFN=min(MeanTFNP);
MEMTFN=nanmean(MeanTFNP);
SDMTFN=nanstd(MeanTFNP);

MxMTFV=max(MeanTFV);
MnMTFV=min(MeanTFV);
MeMTFV=nanmean(MeanTFV);
SdMTFV=nanstd(MeanTFV);
MXMTFV=max(MeanTFVP);
MNMTFV=min(MeanTFVP);
MEMTFV=nanmean(MeanTFVP);
SDMTFV=nanstd(MeanTFVP);

MxMTFM=max(MeanTFM);
MnMTFM=min(MeanTFM);
MeMTFM=nanmean(MeanTFM);
SdMTFM=nanstd(MeanTFM);
MXMTFM=max(MeanTFMP);
MNMTFM=min(MeanTFMP);
MEMTFM=nanmean(MeanTFMP);
SDMTFM=nanstd(MeanTFMP);

%saving files for Rolling Mean if Turbuelance Fluctuations
save(strcat(datadir,'RollMeanTurbulenceFluc_',num2str(ii),'.mat'),'MeanTFE','MeanTFN','MeanTFV','MeanTFM');
save(strcat(datadir,'RollMeanTurbulenceFlucE_',num2str(ii),'.mat'),'MxMTFE','MnMTFE','MeMTFE','SdMTFE','MXMTFE','MNMTFE','MEMTFE','SDMTFE');
save(strcat(datadir,'RollMeanTurbulenceFlucN_',num2str(ii),'.mat'),'MxMTFN','MnMTFN','MeMTFN','SdMTFN','MXMTFN','MNMTFN','MEMTFN','SDMTFN');
save(strcat(datadir,'RollMeanTurbulenceFlucV_',num2str(ii),'.mat'),'MxMTFV','MnMTFV','MeMTFV','SdMTFV','MXMTFV','MNMTFV','MEMTFV','SDMTFV');
save(strcat(datadir,'RollMeanTurbulenceFlucM_',num2str(ii),'.mat'),'MxMTFM','MnMTFM','MeMTFM','SdMTFM','MXMTFM','MNMTFM','MEMTFM','SDMTFM');

%MOVING AVERAGE TURBULENCE FLUCTUATIONS

MMeanTFE=MeanTFE(1:600:length(MeanTFE));
MMeanTFN=MeanTFN(1:600:length(MeanTFN));
MMeanTFV=MeanTFV(1:600:length(MeanTFV));
MMeanTFM=MeanTFM(1:600:length(MeanTFM));

%Positive Sets
MMeanTFEP=sqrt(MMeanTFE.^2);
MMeanTFNP=sqrt(MMeanTFN.^2);
MMeanTFVP=sqrt(MMeanTFV.^2);
MMeanTFMP=sqrt(MMeanTFM.^2);

%Maxs/Means/Mins

MxMMTFE=max(MMeanTFE);
MnMMTFE=min(MMeanTFE);
MeMMTFE=nanmean(MMeanTFE);
SdMMTFE=nanstd(MMeanTFE);
MXMMTFE=max(MMeanTFEP);
MNMMTFE=min(MMeanTFEP);
MEMMTFE=nanmean(MMeanTFEP);
SDMMTFE=nanstd(MMeanTFEP);

MxMMTFN=max(MMeanTFN);
MnMMTFN=min(MMeanTFN);
MeMMTFN=nanmean(MMeanTFN);
SdMMTFN=nanstd(MMeanTFN);
MXMMTFN=max(MMeanTFNP);
MNMMTFN=min(MMeanTFNP);
MEMMTFN=nanmean(MMeanTFNP);
SDMMTFN=nanstd(MMeanTFNP);

MxMMTFV=max(MMeanTFV);
MnMMTFV=min(MMeanTFV);
MeMMTFV=nanmean(MMeanTFV);
SdMMTFV=nanstd(MMeanTFV);
MXMMTFV=max(MMeanTFVP);
MNMMTFV=min(MMeanTFVP);
MEMMTFV=nanmean(MMeanTFVP);
SDMMTFV=nanstd(MMeanTFVP);

MxMMTFM=max(MMeanTFM);
MnMMTFM=min(MMeanTFM);
MeMMTFM=nanmean(MMeanTFM);
SdMMTFM=nanstd(MMeanTFM);
MXMMTFM=max(MMeanTFMP);
MNMMTFM=min(MMeanTFMP);
MEMMTFM=nanmean(MMeanTFMP);
SDMMTFM=nanstd(MMeanTFMP);

%saving files for Moving Mean of Turbulence Fluctuations
save(strcat(datadir,'MovMeanTurbFluc_',num2str(ii),'.mat'),'MMeanTFE','MMeanTFN','MMeanTFV','MMeanTFM');
save(strcat(datadir,'MovMeanTurbFlucE_',num2str(ii),'.mat'),'MxMMTFE','MnMMTFE','MeMMTFE','SdMMTFE','MXMMTFE','MNMMTFE','MEMMTFE','SDMMTFE');
save(strcat(datadir,'MovMeanTurbFlucN_',num2str(ii),'.mat'),'MxMMTFN','MnMMTFN','MeMMTFN','SdMMTFN','MXMMTFN','MNMMTFN','MEMMTFN','SDMMTFN');
save(strcat(datadir,'MovMeanTurbFlucV_',num2str(ii),'.mat'),'MxMMTFV','MnMMTFV','MeMMTFV','SdMMTFV','MXMMTFV','MNMMTFV','MEMMTFV','SDMMTFV');
save(strcat(datadir,'MovMeanTurbFlucM_',num2str(ii),'.mat'),'MxMMTFM','MnMMTFM','MeMMTFM','SdMMTFM','MXMMTFM','MNMMTFM','MEMMTFM','SDMMTFM');

%DIRECTIONS (VELOCITY)
%Rolling 10min velocity directions

minVal=0 ;

for i=1:datalength;
    if (MeanE(i)>=minVal) && (MeanN(i)>=minVal);
        direction(i)=atand(MeanE(i)/MeanN(i));
    elseif (MeanE(i)<minVal) && (MeanN(i)<minVal);
        direction(i)=(atand((MeanE(i)*-1)/(MeanN(i)*-1)))+180;
    elseif (MeanE(i)>=minVal) && (MeanN(i)<minVal);
        direction(i)=(atand((MeanN(i)*-1)/MeanE(i)))+90;
    else; %EV(i)<0 && NV(i)>=0;
        direction(i)=(atand((MeanN(i)*-1)/MeanE(i)))+270;
    end
end

%Moving 10min velocity directions

mdatalength=length(MMeanM);

minVal=0 ;

for i=1:mdatalength;
    if (MMeanE(i)>=minVal) && (MMeanN(i)>=minVal);
        mdirection(i)=atand(MMeanE(i)/MMeanN(i));
    elseif (MMeanE(i)<minVal) && (MMeanN(i)<minVal);
        mdirection(i)=(atand((MMeanE(i)*-1)/(MMeanN(i)*-1)))+180;
    elseif (MMeanE(i)>=minVal) && (MMeanN(i)<minVal);
        mdirection(i)=(atand((MMeanN(i)*-1)/MMeanE(i)))+90;
    else; %EV(i)<0 && NV(i)>=0;
        mdirection(i)=(atand((MMeanN(i)*-1)/MMeanE(i)))+270;
    end
end

%SAVING DIRECTIONS

save(strcat(datadir,'RollMeanDirections_',num2str(ii),'.mat'),'direction','MeanM');%saved Hmag too so can use this to make a current rose
save(strcat(datadir,'MovMeanDirections_',num2str(ii),'.mat'),'mdirection','MMeanM');%saved Hmag too so can use this to make a current rose

clear

end
toc

