function varargout = CortanaguiNOV2012(varargin)
% CORTANAGUINOV2012 M-file for CortanaguiNOV2012.fig
%      CORTANAGUINOV2012, by itself, creates a new CORTANAGUINOV2012 or raises the existing
%      singleton*.
%
%      H = CORTANAGUINOV2012 returns the handle to a new CORTANAGUINOV2012 or the handle to
%      the existing singleton*.
%
%      CORTANAGUINOV2012('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CORTANAGUINOV2012.M with the given input arguments.
%
%      CORTANAGUINOV2012('Property','Value',...) creates a new CORTANAGUINOV2012 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before CortanaguiNOV2012_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to CortanaguiNOV2012_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 CortanaguiNOV2012

% Last Modified by GUIDE v2.5 07-Nov-2012 02:48:32

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @CortanaguiNOV2012_OpeningFcn, ...
                   'gui_OutputFcn',  @CortanaguiNOV2012_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 CortanaguiNOV2012 is made visible.
function CortanaguiNOV2012_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 CortanaguiNOV2012 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = CortanaguiNOV2012_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 button press in EnterRegionRB.
function EnterRegionRB_Callback(hObject, eventdata, handles)
% hObject    handle to EnterRegionRB (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 EnterRegionRB



% --- Executes on selection change in AreaListbox.
function AreaListbox_Callback(hObject, eventdata, handles)
% hObject    handle to AreaListbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns AreaListbox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from AreaListbox
val = get(hObject,'Value');
contents = get(hObject,'String')
databox.place=contents{get(hObject,'Value')};
switch val
    case 1   % User selected the item: User Specified
        NorthLat=num2str(89.500);
        SouthLat=num2str(-89.500);
        EastLon=num2str(179.500);
        WestLon=num2str(-179.500);
        
    case 2 %User selected the  item: Alligator Harbor
        NorthLat=num2str(30.500);
        SouthLat=num2str(27.50);
        WestLon=num2str(-85.3);
        EastLon=num2str(-82.4);
        set(handles.MaxValueedit,'String', 10)
        
    case 3   % User selected the item: BATS
        NorthLat=num2str(33.000);
        SouthLat=num2str(31.500);
        WestLon=num2str(-65.500);
        EastLon=num2str(-63.500);
        set(handles.MaxValueedit,'String', 1)
        
    case 4   % User selected the item: Chesapeake Bay 36.995833,-75.959444
        NorthLat=num2str(40.000);
        SouthLat=num2str(35.500);
        WestLon=num2str(-77.500);
        EastLon=num2str(-75.000);
        set(handles.MaxValueedit,'String', 15)

    case 5   % User selected the  item: GOM
        NorthLat=num2str(45.000);
        SouthLat=num2str(40.500);
        WestLon=num2str(-71.000);
        EastLon=num2str(-65.500);
        set(handles.MaxValueedit,'String', 5)
        
    case 6   % User selected the  item: Gulf of Mexico
        NorthLat=num2str(32.000);
        SouthLat=num2str(18.000);
        WestLon=num2str(-100.000);
        EastLon=num2str(-80.500);
        set(handles.MaxValueedit,'String', 5)
        
    case 7 %User selected the  item: Gulf of St. Lawrence
        NorthLat=num2str(52.800);
        SouthLat=num2str(43.600);
        WestLon=num2str(-71.000);
        EastLon=num2str(-56.800);
        set(handles.MaxValueedit,'String', 10)
        
    case 8   % User selected the item: Hawaiian Islands (HOTS)
        NorthLat=num2str(23.000);
        SouthLat=num2str(18.00);
        WestLon=num2str(-162.000);
        EastLon=num2str(-154.00);
        set(handles.MaxValueedit,'String', 0.2)
        
        
    case 9 % User selected the item: Kuching Harbor [   ] 
        NorthLat=num2str(5.25);
        SouthLat=num2str(1.25);
        WestLon=num2str(108);
        EastLon=num2str(112);
        set(handles.MaxValueedit,'String', 15)

    case 10 %User selected the item: Manda Bay %[   ];
        NorthLat=num2str(.425);
        SouthLat=num2str(-3.725);
        WestLon=num2str(39.2);
        EastLon=num2str(43.5);
        set(handles.MaxValueedit,'String', 5)
        
    case 11 %User selected the item: Moreton Bay, Australia
        NorthLat=num2str(-26.7);
        SouthLat=num2str(-27.9);
        WestLon=num2str(152.7);
        EastLon=num2str(153.9);
        set(handles.MaxValueedit,'String', 10)
     
    case 12  %User selected the item: Namibe Bay %-15.16	12.15
        NorthLat=num2str(-12.000);
        SouthLat=num2str(-17.000);
        WestLon=num2str(9.000);
        EastLon=num2str(14.000);
        set(handles.MaxValueedit,'String', 5)
        
    case 13  %User selected the  item: NW Coast US 
        NorthLat=num2str(50.5);
        SouthLat=num2str(43.5);
        WestLon=num2str(-128);
        EastLon=num2str(-122);
        set(handles.MaxValueedit,'String', 10)
      
    case 14 %User selected the  item: San Andres
        NorthLat=num2str(13.1);
        SouthLat=num2str(10.7);
        WestLon=num2str(-83.8);
        EastLon=num2str(-80.2);
        set(handles.MaxValueedit,'String', 1)
        
    case 15 %User selected the  item: SF Bay
        NorthLat=num2str(39.5);
        SouthLat=num2str(35.5);
        WestLon=num2str(-124.5);
        EastLon=num2str(-121.5);
        set(handles.MaxValueedit,'String', 10)
            
    case 16 %User selected the  item: Tawau Harbor%	4.30	117.88
        NorthLat=num2str(6.000);
        SouthLat=num2str(2.000);
        WestLon=num2str(117.000);
        EastLon=num2str(121.000);
        set(handles.MaxValueedit,'String', 10)
        
    case 17 %User selected the  item: Tawau Harbor%	4.30	117.88
        NorthLat=num2str(28.500);
        SouthLat=num2str(24.500);
        WestLon=num2str(-84.000);
        EastLon=num2str(-80.000);
        set(handles.MaxValueedit,'String', 10)
        

        
   
        
      

end
  %TOPLOT=10.9 -9.9 103.3 124.2]; % Borneo
   
        set(handles.LatN,'String', NorthLat);
        set(handles.LatS,'String', SouthLat);
        set(handles.LonE,'String', EastLon);
        set(handles.LonW,'String', WestLon);
        set(handles.AreaListbox, 'userdata', databox)

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

% Hint: listbox 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


% --- Executes on button press in SelectRegionRB.
function SelectRegionRB_Callback(hObject, eventdata, handles)
% hObject    handle to SelectRegionRB (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 SelectRegionRB



function LatN_Callback(hObject, eventdata, handles)
% hObject    handle to LatN (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 LatN as text
%        str2double(get(hObject,'String')) returns contents of LatN as a double
%NorthLatBox= str2double(get(hObject,'String'));
%set(hObject, 'String', NorthLat);
%%display NorthLatBox %% writes "NorthLatBox on screen"
%%disp(NorthLatBox)   %% displays value of NorthLatBox on screen


% --- Executes during object creation, after setting all properties.
function LatN_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LatN (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



function LatS_Callback(hObject, eventdata, handles)
% hObject    handle to LatS (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 LatS as text
%        str2double(get(hObject,'String')) returns contents of LatS as a double
SouthLatBox= str2double(get(hObject,'String'));

% --- Executes during object creation, after setting all properties.
function LatS_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LatS (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



function LonE_Callback(hObject, eventdata, handles)
% hObject    handle to LonE (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 LonE as text
%        str2double(get(hObject,'String')) returns contents of LonE as a double
EastLonBox= str2double(get(hObject,'String'));

% --- Executes during object creation, after setting all properties.
function LonE_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LonE (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



function LonW_Callback(hObject, eventdata, handles)
% hObject    handle to LonW (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 LonW as text
%        str2double(get(hObject,'String')) returns contents of LonW as a double
WestLonBox= str2double(get(hObject,'String'));

% --- Executes during object creation, after setting all properties.
function LonW_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LonW (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




% --- Executes on button press in PlotRegPB.
function PlotRegPB_Callback(hObject, eventdata, handles)
% hObject    handle to PlotRegPB (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%load work/R1a.mat
%imagesc(R1a, 'parent', handles.plotaxes1); axis image; colorbar('location', 'southoutside')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%databox3.filebase=get(handles.SelectPlatformPU, 'string');
%databox1.place=get(handles.AreaListbox, 'string');
%databox2.month=get(handles.Timepopup, 'string'); 


dir=get(handles.Diredit, 'String'); 
if isempty(get(handles.SelectPlatformPU, 'userdata'));
    databox3.type='MODIS Chl mg/m^3'
    databox3.filebase='chlaL3m_CHL_4km.hdf';
else
databox3=get(handles.SelectPlatformPU, 'userdata');
end
if isempty(get(handles.AreaListbox, 'userdata'));
    placelist=get(handles.AreaListbox, 'string');  
    databox1.place=placelist(1, :);
else
databox1=get(handles.AreaListbox, 'userdata');
end

if isempty(get(handles.Timepopup, 'userdata'));
databox2.month='00';
databox2.timelabel='Annual ';
else
    databox2=get(handles.Timepopup, 'userdata');
end

%databox2=get(hObject, 'userdata');
filebase=databox3.filebase;
titlestr=databox3.type;%'Modis Aqua Chl mg/m^3 '
month=databox2.month;
timelabel=databox2.timelabel;
place=databox1.place;
filename=[dir month filebase];
display(filename)
    HINFO=hdfinfo(filename)
    dataset=hdfread(HINFO.SDS);
     %%% Lat Lon determination in decimal degrees
[rd, cd]= size(dataset);

%%% Latitude vector, assumes min lat is -90, max lat is 90
latinc= (180/rd)/2;
latvec=[90-latinc:-2*latinc:-90+latinc];

%%% Longitude vector, assumes min lon is -180, max lon is 180
loninc= (360/cd)/2;
lonvec=[-180+loninc:2*loninc:180-loninc];
    %TOPLOTMON=strvcat('January', 'February', 'March', 'April', 'May', 'June',...
       % 'July', 'August', 'September', 'October', 'November', 'December');
    xlabelstr='Longitude'; ylabelstr='Latitude';;
    chl_colormap1; %% run the colormap off the SEADAS discussion site
    R1=dataset;
    %Set negative values of CHL to NaN.
    R1(R1(:) < 0) = NaN;
   %%% Lat Lon determination in decimal degrees
[rd, cd]= size(dataset);

%%% Latitude vector, assumes min lat is -90, max lat is 90
latinc= (180/rd)/2;
latvec=[90-latinc:-2*latinc:-90+latinc];

%%% Longitude vector, assumes min lon is -180, max lon is 180
loninc= (360/cd)/2;
lonvec=[-180+loninc:2*loninc:180-loninc];
    
    NorthLat=str2double(get(handles.LatN, 'String'));
    SouthLat=str2double(get(handles.LatS, 'String'));
    WestLon=str2double(get(handles.LonW, 'String'));
    EastLon=str2double(get(handles.LonE, 'String'));
    TOPLOT=[NorthLat SouthLat WestLon EastLon];
    cmax=str2double(get(handles.MaxValueedit,'String'));
     [loopr, loopc]=size(TOPLOT);
    for i=1:loopr

        R1alatN=TOPLOT(i, 1);  
        R1alatS=TOPLOT(i, 2);  
        R1alonW=TOPLOT(i, 3); 
        R1alonE=TOPLOT(i, 4);  
        ind1=find(latvec>=R1alatN, 1, 'last');
        ind2=find(latvec<=R1alatS, 1, 'first');
        ind3=find(lonvec<=R1alonW, 1, 'last');
        ind4=find(lonvec>=R1alonE, 1, 'first');
        LATVECNOW=latvec(ind1:ind2);
        LONVECNOW=lonvec(ind3:ind4);
        R1a=R1(ind1:ind2, ind3:ind4);
        img=flipud(R1a); % this is needed due to the vagaries of imagesc with user defined axes

        %fig=figure;
        myclim=[0 cmax(i)];
        lonv=[LONVECNOW(1) LONVECNOW(end)];
        latv=[LATVECNOW(1) LATVECNOW(end)];      
        ih=imagesc(lonv, latv, R1a, myclim); 
        set(ih, 'parent', handles.plotaxes1);  
        %set(ih, 'buttondownfcn', 'get(gca,''currentpoint'')' )
        
        set(handles.GetPointToggle, 'userdata', R1a);
        colormap(nasacolor); colorbar
        axis image
        axis xy
        xlabel(xlabelstr);ylabel(fliplr(ylabelstr));
         title([place ' ' timelabel ' ' titlestr])
         %%%%%%%%% 
       
   R1a=get(handles.GetPointToggle, 'userdata');
        img=flipud(R1a);
bluecat=reshape(img, [], 1);
set(handles.MEANedit,'String', nanmean(bluecat))
set(handles.MEDedit,'String', nanmedian(bluecat))
set(handles.VARedit,'String', nanvar(bluecat))
set(handles.STDedit,'String', nanstd(bluecat))
set(handles.MINedit,'String', nanmin(bluecat))
set(handles.MAXedit,'String', nanmax(bluecat))  
            
assignin('base', ['d' place(1:3) timelabel],img )
          bluecat=reshape(img, [], 1);
          assignin('base', [place(1:3)  timelabel ],[nanmean(bluecat) nanmedian(bluecat) nanvar(bluecat) nanstd(bluecat), nanmin(bluecat), nanmax(bluecat) ])
assignin('base', ['timelabel'], [num2str(timelabel) ])
assignin('base', ['place'], [num2str(place(1:3)) ])
            
    end
gpt=get(handles.GetPointToggle,'Value')

if get(handles.GetPointToggle,'Value') >=1
    
% the image and axis
    %fig=figure;
    %cmax=str2num(get(handles.MaxValueedit,'String'));
    %cmax=int8(cmax);
     %   myclim=[0 cmax];
      %  lonv=[ LONVECNOW(1) LONVECNOW(end)];
       % latv=[LATVECNOW(1) LAplpTVECNOW(end) ];
       R1a=get(handles.GetPointToggle, 'userdata');
        img=flipud(R1a); % this is needed due to the vagaries of imagesc with user defined axes
       
       % ih=imagesc(lonv, latv, R1a, myclim); 
       
      %  colormap(nasacolor); colorbar
       % axis image
        %axis xy
        %xlabel(xlabelstr);ylabel(fliplr(ylabelstr)); 
       
% the engine for converting latlon and pixel space modified from 
% http://www.mathworks.de/matlabcentral/newsreader/view_thread/145678
% ...a scaling factor
     xl=get(gca,'xlim');
     amin=[xl;get(gca,'ylim')];
     arng=(diff(amin,[],2));
     irng=fliplr(size(img)).';
     sff=irng./arng;
     cp=get(gca, 'currentpoint')
% ...wrapped into a macro
    % impconv=@(cp) fix(sff.*(cp(1,1:2).'-amin(:,1)))+1;
% ...the callback command
     %com={
%          'cp=(get(gca,''currentpoint''));'
%           'xl=get(gca,''xlim'');'
%      'amin=[xl;get(gca,''ylim'')];'
%      'arng=(diff(amin,[],2));'
%       'R1a=get(handles.GetPointsRB,'
%      'img=flipud(R1a);'
%      'irng=fliplr(size(img));'
%      'sff=irng./arng;'
           % , r(1), r(2)]' %'rowcol=[r.'',img(r(2),r(1))]'
     
         % };
%      set(ih,'buttondownfcn', 'cp=(get(gca,''currentpoint''));' );    %%[com{:}]);
%       r=fix(sff.*(cp(1,1:2).''-amin(:,1)))+1; %%'r=impconv(cp);' 
         % latlonval=[cp(1,2), cp(1,1), img(r(2),r(1))]
         % set(handles.LatPedit,'String', num2str(cp(1,2)));
%    % cp=(get(gca,''currentpoint''));'
%       
      
end





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% --- Executes on selection change in SelectPlatformPU.
function SelectPlatformPU_Callback(hObject, eventdata, handles)
% hObject    handle to SelectPlatformPU (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns SelectPlatformPU contents as cell array
%        contents{get(hObject,'Value')} returns selected item from SelectPlatformPU
val=get(hObject,'Value');
contents = get(hObject,'String'); 
databox.type=contents{get(hObject,'Value')}
filebase='chlaL3m_CHL_4km.hdf'
switch val
    case 1 % Modis Chl
filebase='chlaL3m_CHL_4km.hdf'
    case 2 % Modis CDOM
        filebase= 'CUcdomL3m_cdom_index_4km.hdf'
    case 3 % MODIS dg gsm
        filebase='dg2009L3m_adg_443_gsm_4km.hdf';
    case 4 % MODIS dg qaa
        filebase='dg2009L3m_adg_443_qaa_4km.hdf';
    case 5 % monthly QAA adg values
        filebase='CDOM_adg_443_qaa_4km.hdf'
         set(handles.MaxValueedit,'String', 1)
    case 6 % 'NODC salinity' s00an1
        filebase='an1s'; %%need to change files to make this work
        
end

databox.filebase=filebase;
set(hObject, 'userdata', databox);  % retrieve via databox3=get(handles.SelectPlatformPU, 'userdata')



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

% Hint: popupmenu 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


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


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


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


% --- Executes on button press in GetPointsRB.
function GetPointsRB_Callback(hObject, eventdata, handles)
% hObject    handle to GetPointsRB (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 GetPointsRB



function LatPedit_Callback(hObject, eventdata, handles)
% hObject    handle to LatPedit (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 LatPedit as text
%        str2double(get(hObject,'String')) returns contents of LatPedit as a double


% --- Executes during object creation, after setting all properties.
function LatPedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LatPedit (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



function LonPedit_Callback(hObject, eventdata, handles)
% hObject    handle to LonPedit (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 LonPedit as text
%        str2double(get(hObject,'String')) returns contents of LonPedit as a double


% --- Executes during object creation, after setting all properties.
function LonPedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LonPedit (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



function ValuePedit_Callback(hObject, eventdata, handles)
% hObject    handle to ValuePedit (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 ValuePedit as text
%        str2double(get(hObject,'String')) returns contents of ValuePedit as a double


% --- Executes during object creation, after setting all properties.
function ValuePedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ValuePedit (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


% --- Executes on mouse press over figure background.
function figure1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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


% --- Executes on selection change in Timepopup.
function Timepopup_Callback(hObject, eventdata, handles)
% hObject    handle to Timepopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns Timepopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Timepopup
contents = get(hObject,'String')
time=get(hObject,'Value');
timelabel=contents{get(hObject,'Value')};

switch time
    case 1 %% Annual
        month='00';
    case 2 %% Spring
        month='13';
    case 3 %% Summer
        month='14';
    case 4 %% Fall
        month='15';
    case 5 %% Winter
        month='16';
    case 6 %% Jan
        month='01';
    case 7 %% Feb
        month='02';
    case 8 %% Mar
        month='03';
    case 9 %% Apr
        month='04';
    case 10 %% May
        month='05';
    case 11 %% Jun
        month='06';
    case 12 %% Jul
        month='07';
    case 13 %% Aug
        month='08';
    case 14 %% Sep
        month='09';
    case 15 %% Oct
        month='10';
    case 16 %% Nov
        month='11';
    case 17 %% Dec
        month='12';
end
databox.month=month;
databox.timelabel=timelabel;
set(hObject, 'userdata', databox)

        
        
        


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

% Hint: popupmenu 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

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




function MaxValueedit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxValueedit (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 MaxValueedit as text
%        str2double(get(hObject,'String')) returns contents of MaxValueedit as a double


% --- Executes during object creation, after setting all properties.
function MaxValueedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxValueedit (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



function Diredit_Callback(hObject, eventdata, handles)
% hObject    handle to Diredit (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 Diredit as text
%        str2double(get(hObject,'String')) returns contents of Diredit as a double
%'/Applications/MATLAB_R2007b/work/stedata/MODISClimatologies/';

dir=get(hObject,'String');



% --- Executes during object creation, after setting all properties.
function Diredit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Diredit (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


% --- Executes on button press in GetPointToggle.
function GetPointToggle_Callback(hObject, eventdata, handles)
% hObject    handle to GetPointToggle (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 GetPointToggle
tgetpoint=get(hObject, 'Value')
R1a=get(handles.GetPointToggle, 'userdata');
        img=flipud(R1a);
 xl=get(gca,'xlim');
     amin=[xl;get(gca,'ylim')]
     arng=(diff(amin,[],2))
     irng=fliplr(size(img)).'
     sff=irng./arng
     sff=sff(1);
    
%if tgetpoint>=1
    xl=get(handles.plotaxes1,'xlim');
     amin=[xl;get(handles.plotaxes1,'ylim')];
     arng=(diff(amin,[],2));
     irng=fliplr(size(img)).';
     sf=irng./arng;
% ...wrapped into a macro
     pconv=@(cp) fix(sf.*(cp(1,1:2).'-amin(:,1)))+1;
% ...the callback command
%set(handles.plotaxes1, 'buttondownfcn', [com{:}];
         cp=(get(handles.plotaxes1,'currentpoint'));
          r=pconv(cp)
          latlonval=[cp(1,2), cp(1,1), img(r(2),r(1))]
          set(handles.LatPedit,'String', num2str(cp(1,2)))
          set(handles.LonPedit,'String', num2str(cp(1,1)))
          set(handles.ValuePedit, 'String', num2str(img(r(2), r(1))))
          
         %end

% --- Executes on button press in GetAreaStats.
function GetAreaStats_Callback(hObject, eventdata, handles)
% hObject    handle to GetAreaStats (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
R1a=get(handles.GetPointToggle, 'userdata');
        img=flipud(R1a);
bluecat=reshape(img, [], 1);
set(handles.MEANedit,'String', nanmean(bluecat))
set(handles.MEDedit,'String', nanmedian(bluecat))
set(handles.VARedit,'String', nanvar(bluecat))
set(handles.STDedit,'String', nanstd(bluecat))
set(handles.MINedit,'String', nanmin(bluecat))
set(handles.MAXedit,'String', nanmax(bluecat))


function MEANedit_Callback(hObject, eventdata, handles)
% hObject    handle to MEANedit (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 MEANedit as text
%        str2double(get(hObject,'String')) returns contents of MEANedit as a double
set(handles.MEANedit,'String', nanmean(bluecat))

% --- Executes during object creation, after setting all properties.
function MEANedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MEANedit (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



function MEDedit_Callback(hObject, eventdata, handles)
% hObject    handle to MEDedit (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 MEDedit as text
%        str2double(get(hObject,'String')) returns contents of MEDedit as a double


% --- Executes during object creation, after setting all properties.
function MEDedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MEDedit (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



function VARedit_Callback(hObject, eventdata, handles)
% hObject    handle to VARedit (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 VARedit as text
%        str2double(get(hObject,'String')) returns contents of VARedit as a double


% --- Executes during object creation, after setting all properties.
function VARedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to VARedit (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





function STDedit_Callback(hObject, eventdata, handles)
% hObject    handle to STDedit (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 STDedit as text
%        str2double(get(hObject,'String')) returns contents of STDedit as a double


% --- Executes during object creation, after setting all properties.
function STDedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to STDedit (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



function MINedit_Callback(hObject, eventdata, handles)
% hObject    handle to MINedit (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 MINedit as text
%        str2double(get(hObject,'String')) returns contents of MINedit as a double


% --- Executes during object creation, after setting all properties.
function MINedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MINedit (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



function MAXedit_Callback(hObject, eventdata, handles)
% hObject    handle to MAXedit (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 MAXedit as text
%        str2double(get(hObject,'String')) returns contents of MAXedit as a double


% --- Executes during object creation, after setting all properties.
function MAXedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MAXedit (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


