%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Program to plot wind speed and direction of WEMITE data  %
%                                                          %
% This program is designed for single vector datasets only %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%load filenames into arrays
[windspeed,path] = uigetfile('*.txt','Select the wind speed dataset');
[winddir,path] = uigetfile('*.txt','Select the wind direction dataset');

%load actual data
windspeed = load([path,windspeed]);
winddir = load([path,winddir]);
startyear = input('What is the start year for data collection? ','s');
startmonth = input('What is the start month for data collection (number only)? ','s');
startdate = input('What is the start date for data collection? ','s');
starthour = input('What is the start hour for data collection? ','s');
startminute = input('What is the start minute for data collection? ','s');
frequency = input('What is the sampling frequency (number only, no units)? ');
name = input('What is the name of the storm? ','s');
lat = input('What is the lat of the tower? ','s');
lon = input('What is the lon of the tower? ','s');
height = input('What is the height of the data recorded (number only, no units)? ','s');

%Change numerical month value to word
if startmonth == '6'
    startmonthconverted = 'June';
elseif startmonth == '7'
    startmonthconverted = 'July';
elseif startmonth == '8'
    startmonthconverted = 'August';
elseif startmonth == '9'
    startmonthconverted = 'September';
elseif startmonth == '10'
    startmonthconverted = 'October';
elseif startmonth == '11'
    startmonthconverted = 'November';
elseif startmonth == '12'
    startmonthconverted = 'December';
end

%plot wind speed and direction, where x is for the length of the x-axis
x = 1:length(windspeed);
[AX,H1,H2] = plotyy(x,winddir,x,windspeed);

%Find WS statistics
    %Empty 3sec matrix
peakws = max(windspeed);
peakws = num2str(peakws);
   %Find peak 3-sec avg
check = 0;
   for i=1:length(x)-(3*frequency)
       temp = mean(windspeed(i:i+((3*frequency)-1)));
       if temp > check
           check = temp;
       end
   end
peakthreesec = check;
peakthreesec = num2str(peakthreesec);
   %Find peak 1-min avg
check = 0;
   for i=1:length(x)-(60*frequency)
       temp= mean(windspeed(i:i+((60*frequency)-1)));
       if temp > check
           check = temp;
       end
   end
peakonemin = check;
peakonemin = num2str(peakonemin);
%limit of axes, shape image
set(AX(2),'XLim',[0 length(windspeed)]);
set(AX(1),'XLim',[0 length(winddir)]);
set(AX(2),'YLim',[0 (max(windspeed)+10)]);
set(AX(1),'YLim',[0 (max(winddir)+100)]);

%Change the properties of the winddir time series line
set(H1, 'LineStyle', 'none');
set(H1, 'Marker', 'o');
set(H1, 'MarkerSize', 0.5);

%Label the plot
set(get(AX(1),'Ylabel'),'String','Wind Direction (degrees)')
set(get(AX(2),'Ylabel'),'String','Wind Speed (m/s)')
title(['Wind Speed and Direction Time Series Plot for ',name]);
xlabel('Time');

%Insert text boxes with pertinent information
freq = num2str(frequency);

text(12500,430,['Start Date: ',startdate,' ',startmonthconverted,' ',startyear],'FontSize',8);
text(12500,410,['Start Time: ',starthour,':',startminute,' UTC'],'FontSize',8);
text(12500,390,['Sampling Frequency: ',freq,' Hz'],'FontSize',8);

text(length(x)/2-45000,430,['Lat: ',lat,'ºN'],'FontSize',8);
text(length(x)/2-45000,410,['Lon: ',lon,'ºW'],'FontSize',8);
text(length(x)/2-45000,390,['Height: ',height, ' m'],'FontSize',8);

text(length(x)-185000,430,['Peak WS: ',peakws,' m/s'],'FontSize',8);
text(length(x)-185000,410,['Peak 3-sec: ',peakthreesec,' m/s'],'FontSize',8);
text(length(x)-185000,390,['Peak 1-min: ',peakonemin,' m/s'],'FontSize',8);

%Calculations for correct time along x-axis
%DataTime = 0:1/frequency:(length(x)/frequency)-1/frequency;
Tick = get(AX(1),'XTick');
SerialDateNumber = datenum(str2num(startyear),str2num(startmonth),str2num(startdate),str2num(starthour),str2num(startminute),0+Tick/5');
str = datestr(SerialDateNumber,13);
set(AX,'XTickLabel','');
set(AX,'XTickLabel',str);
xlabel('Time(HH:MM:SS)');