function plotBgrid(varargin)
% plotBgrid(filename)
% plotBgrid(A, lat, lon, areaName)
%  where A, lat, lon are outputs from 'readBgrid'
%  and areaName is the region code

if nargin == 1
    [A, lat, lon] = readBgrid(varargin{1});
    % get region name
    s = split(varargin{1},'.');
    areaName = s{3};
else
    [A, lat, lon, areaName] = varargin{:};
end

[X,Y] = meshgrid(lon-360,lat);
figure;
s = surf(X,Y,A);
axis equal
view(0,90)
s.EdgeColor = 'none';
xlabel('E longitude (deg)')
ylabel('N latitude (deg)')
colorbar
colormap('jet')
if nargin == 1
    [filepath,name,ext] = fileparts(varargin{1});
    title(name);
end

% set axes limits to boundaries of full grid
gridLim = [[min(lon) max(lon)]-360 min(lat) max(lat)];
axis(gridLim);
disp([gridLim(1:2)+360 gridLim(3:4)]);

% overlay the shoreline for land areas
zmax = max(max(s.CData));
plotBorders3d(areaName, zmax);
end

function plotBorders3d(areaName, z)

% load list of filesnames
switch lower(areaName)
%     case 'conus'
%         basemapName = 'continental us';
%     case 'alaska'
%         basemapName = 'alaska';
%         %areaList = {'AK'}; % seemingly duplicated by 'borders'
    case 'pr'
        areaList = {'PR'};
    case 'vi'
        areaList = {'VQ'};
    case 'hawaii'
        areaList = {'HI'};
    case 'as'
        areaList = {'AS'};
    case 'guam'
        areaList = {'GQ'};
    case 'cnmi'
        areaList = {'CQ'};
    otherwise
        error('areaName not recognized\n does not work for conus or alaska')
end

% % draw state boundaries
% if ~isempty(basemapName)
%     borders(basemapName,'k');
% end

hold on; % hold figure

% supplement with GMT files, if needed for area
bb = strrep(userpath,';','\');
str = sprintf('%s//boundaries/',bb);
for k = 1:length(areaList)
    % get list of files for each area
    ff = dir(sprintf('%s%s*.gmt',str,areaList{k}));
    for i = 1:length(ff)
        % load and plot file
        x{i} = load([str ff(i).name]);
        plot3(x{i}(:,1)-360, x{i}(:,2), ...
            ones(size(x{i},1),1)*z, 'k'); hold on
    end
end
end