function gridedit(x,y,u,coastline,range)
% GRIDEDIT:  GUI editor for data defined on a matlab xy grid.
%
% USAGE: gridedit(x,y,u,coastline,range);
%      
% PARAMETERS:
%     x, y:  spatial coordinates of grid.
%     u:     data defined on grid.  This is what is edited.
%     coastline:  two-column lon lat file to optionally display.
%                 Optional.
%     range: Controls how to display the data using pslice2. 
%            Optional.
%
% The user may make use of multiple mouse buttons to manipulate
% the grid.  Here are the functions
%
% Mouse button 1 ==> grid cell selection
% Mouse button 2 ==> define a new axis, xlim and ylim.
%                    Click down with mouse button 2 to define one
%                    corner, drag to opposite corner, release the mouse
%                    button.
% Mouse button 3 ==> Go back to previously defined axis.
%

global grid_obj;

if ( nargin < 3 )
    help gridedit;
    return;
end

if ( nargin < 4 )
    coastline = [];
    range = [min(u(:)) max(u(:))];
end

if ( nargin < 5 )
    range = [min(u(:)) max(u(:))];
end

grid_obj.figure = figure;
grid_obj.x = x;
grid_obj.nearx = x(:);
grid_obj.y = y;
grid_obj.neary= y(:);
grid_obj.u = u;
grid_obj.original_u = u;
grid_obj.range = range;
grid_obj.coastline = coastline;
grid_obj.point_index_list = [];
grid_obj.marker_list = [];

pslice2( grid_obj.x, ...
                       grid_obj.y, ...
                       grid_obj.u, ...
                       grid_obj.range );
shading ( 'faceted' );
colorbar
if ( length(grid_obj.coastline) ~= 0 )
    line ( grid_obj.coastline(:,1), grid_obj.coastline(:,2), 'color', 'red' );
end

grid_obj.pslice_axis = gca;

grideditControls;


edit_fig = findobj ( 0, 'tag', 'edit figure' );
grid_obj.edit_fig = edit_fig;

%
% The gridedit slider should have its max and min values
% set according to the range.
grid_slider = findobj(0,'tag','value slider');
set ( grid_slider, 'max', grid_obj.range(2) );
set ( grid_slider, 'min', grid_obj.range(1) );
mid_value = (grid_obj.range(2) + grid_obj.range(1)) / 2.0
set ( grid_slider, 'value', mid_value );

grid_edit = findobj(0,'tag','value edit' );
set ( grid_edit, 'string', mid_value );


%
% Set the grid window buttondown function callback
% to grideditCommand.
set ( grid_obj.figure, 'WindowButtonDownFcn', 'grideditCommand buttondown' );
set ( grid_obj.figure, 'WindowButtonUpFcn', 'grideditCommand buttonup' );

%
% Set the geographic extents
axes ( grid_obj.pslice_axis );
xlim = get ( gca, 'XLim' );
ylim = get ( gca, 'YLim' );

north_lat_edit = findobj ( edit_fig, 'Tag', 'northlat edit' );
south_lat_edit = findobj ( edit_fig, 'Tag', 'southlat edit' );
east_lon_edit = findobj ( edit_fig, 'Tag', 'eastlon edit' );
west_lon_edit = findobj ( edit_fig, 'Tag', 'westlon edit' );

set ( north_lat_edit, 'String', num2str(ylim(2)) );
set ( south_lat_edit, 'String', num2str(ylim(1)) );
set ( west_lon_edit, 'String', num2str(xlim(1)) );
set ( east_lon_edit, 'String', num2str(xlim(2)) );

grid_obj.axis_stack = [];



