function varargout = ca(varargin)
% CA M-file for ca.fig
%      CA, by itself, creates a new CA or raises the existing
%      singleton*.
%
%      H = CA returns the handle to a new CA or the handle to
%      the existing singleton*.
%
%      CA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CA.M with the given input arguments.
%
%      CA('Property','Value',...) creates a new CA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ca_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ca_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ca

% Last Modified by GUIDE v2.5 02-Jun-2016 09:30:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ca_OpeningFcn, ...
                   'gui_OutputFcn',  @ca_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ca is made visible.
function ca_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ca (see VARARGIN)

% Choose default command line output for ca
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);


% UIWAIT makes ca wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ca_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
DropHeight=get(hObject,'Value')
set(handles.edit1,'String',num2str(DropHeight))

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
DropHeight=1500;
set(hObject,'Value',DropHeight)




function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
set(handles.slider1,'Value',str2num(get(hObject,'String')))

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
DropHeight=1500;
set(hObject,'String',num2str(DropHeight))


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
DropSpeed=get(hObject,'Value')
set(handles.edit2,'String',num2str(DropSpeed))

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

DropSpeed=22;
set(hObject,'Value',DropSpeed)


function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double
set(handles.slider2,'Value',str2num(get(hObject,'String')))

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
DropSpeed=22;
set(hObject,'String',num2str(DropSpeed))

% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of togglebutton1
[fil,path,ext]=uigetfile('c:\lidar\*.vad');
filename=fil;
pathr=path;
set(handles.text5,'String',filename)
[vad,prof]=Pathfinder(fullfile(pathr,filename),22,1.007);

axes(handles.axes1)
haxe1=gca;
set(haxe1,'fontsize',12,'fontweight','n')
xlabel('Horizontal Wind (knt)','fontsize',12,'fontweight','n')
ylabel('Height AGL (kft)','fontsize',12,'fontweight','n')
plot(1.94384*prof.Vh,0.00328*prof.heights,'linewidth',3)
grid on
axis([0 60 0 5])
set(haxe1,'XTick',[0 20 40 60])

axes(handles.axes2)
scatter(prof.theta,0.00328*prof.heights,'linewidth',3)
grid on
haxe2=gca;
set(haxe2,'fontsize',12,'fontweight','n')
xlabel('Direction','fontsize',12,'fontweight','n')
axis([0 360 0 5])
set(haxe2,'XTick',[0 90 180 270 360])

axes(handles.axes3)
plot(1.94384*prof.w,0.00328*prof.heights,'linewidth',3)
grid on
haxe3=gca;
set(haxe3,'fontsize',12,'fontweight','n')
xlabel('Vertical Wind (knt)','fontsize',12,'fontweight','n')
axis([-10 10 0 5])
set(haxe3,'XTick',[-10 -5 0 5 10])
text(-20,5.2,['Wind Conditions ',datestr(vad.start_time),' UTC'],'fontsize',14,'fontweight','n')

axes(handles.axes4)
plot(1.94384*prof.turb,0.00328*prof.heights,'linewidth',3)
grid on
haxe4=gca;
set(haxe4,'fontsize',12,'fontweight','n')
xlabel('Variance (knt)','fontsize',12,'fontweight','n')
axis([0 10 0 5])
set(haxe4,'XTick',[0 5 10])

DropHeight=get(handles.slider1,'Value');
DropSpeed=get(handles.slider2,'Value');


ival=find(~isnan(prof.u)& ~isnan(prof.v));

ucum=cumtrapz(prof.heights(ival),prof.u(ival));
vcum=cumtrapz(prof.heights(ival),prof.v(ival));
mew=sqrt(ucum.^2+vcum.^2);
mew=mew./prof.heights(ival)';
theta=atand(abs(ucum)./abs(vcum));
for j=1:length(ival)
if (ucum(j)>0 && vcum(j)>0)
theta(j)=theta(j)+180;
elseif (ucum(j)>0 && vcum(j)<0)
theta(j)=360-theta(j);
elseif (ucum(j)<0 && vcum(j)<0)
theta(j)=theta(j);
elseif (ucum(j)<0 && vcum(j)>0)
theta(j)=360-theta(j)-180;
end
end

mav=interp1(prof.heights(ival)*3.28084,mew,DropHeight);
mdirav=interp1(prof.heights(ival)*3.28084,theta,DropHeight);

dt=DropHeight/DropSpeed;
DriftOffset=mav*dt;
DriftDir=mdirav;
set(handles.text19,'String',[num2str(DriftOffset),' m'])
set(handles.text21,'String',[num2str(int16(DriftDir)),' deg'])
set(handles.text17,'String',num2str(mav*1.94384))
set(handles.text16,'String',num2str(int16(mdirav)))
[gwindm,gdirm,gtempm,ghumm,gpressm]=airmar();
set(handles.text7,'String',[num2str(int16(gwindm)),' kts'])
set(handles.text9,'String',num2str(int16(gdirm)))
set(handles.text10,'String',[num2str(int16(gtempm)),' C'])
set(handles.text11,'String',num2str(int16(ghumm)))


    ttfile=vad.start_time;
    YEAR=datestr(ttfile,'yyyy');
    MONTH=datestr(ttfile,'mm');
    DAY=datestr(ttfile,'dd');
    TIME=datestr(ttfile,'HHMMSS');
    PROC_DIR=['c:/lidar/Processed/'];
    PROC_FILENAME=['Pathfinder_',YEAR,MONTH,DAY,'_',TIME,'.mat'];
    PROC_IMAGE_FILENAME=['Pathfinder_',YEAR,MONTH,DAY,'_',TIME,'.png'];
    if exist([PROC_DIR,YEAR,MONTH,DAY,'/'],'dir')==0
        mkdir([PROC_DIR,YEAR,MONTH,DAY,'/'])
    end

    save([PROC_DIR,YEAR,MONTH,DAY,'/',PROC_FILENAME],'vad','prof','mav','mdirav','mew','theta','DriftOffset','DriftDir')
    print('-dpng', [PROC_DIR,YEAR,MONTH,DAY,'/',PROC_IMAGE_FILENAME])
   
