LLMAP

%Map field on a lat-lon map
%
%Input:
%                  a               Field to be plotted
%                  latbox        Latitude limit
%                  lonbox       Longitude Limit
%                  cont          contours ( Regularly spaced intervals or arbitrary
%                  shading      'on' | 'off'  ( not available for irregular contours)
%                  extshad     [value(1)  value(2)]  shading is only outside the interval val(1) -- val(2))
%                                  [value(1)]       shading is only for values greater/smaller for pos/neg value(1)
%                                   0          Shading only positive values
%                                   [0 0]     Shading only negative values
%                  label           Frequency of labeling contours (< 0 no label)
%                  colormap    Colormap selected
%                  undefvalue  Undefined values
%                  contours     Plotting also contour
%                  landcolor     0.5*[1 1 1]  color for landmasses
%                  proj             Projection desired
function [c,hp]=llmap(f,latbox,lonbox,ccc,tit,varargin)

%  Usage:
% [h,ht,hcb]=llmap(z(:,1),[0 90],[0 360],[-0.05:0.01:0.05],'test','shading','on','extshad',[0.02]);
% [h,ht,hcb]=llmap(z(:,1),[0 90],[0 360],[-0.05:0.01:0.05],'test','shading','on','extshad',[0.02]);
% [h,ht,hcb]=llmap(z(:,1),[0 90],[0 360],[-0.05:0.01:0.05],'test','shading','on','extshad',[0.02]);
%Based on m_map


shvalue=[];
landcolor=[];
colmap='gray';
flshad='on';
uvalue =9999;
labelfrequency=5;
contours=1;
cbar='off';
viszero='on';
flover=0;
proj='eqdcylin';

if nargin > 5

    for i = 1:length(varargin)

        if ischar(varargin{i});
            option=varargin{i};
            switch option;
                case 'flipns'
                    a=fliplr(a);
                case 'flipew'
                    a=flipud(a);
                case 'extshad'
                    if ischar(varargin{i+1})
                        switch varargin{i+1};
                            case 'positive'
                                shvalue = 0;
                                disp(' Extreme shading for positive numbers ');
                            case 'negative'
                                shvalue = [0 0];
                                disp(' Extreme shading for negative numbers ');
                            case 'none'
                                shvalue = [];
                            otherwise
                                disp('Choices are number, positive, negative, none');
                                error(' llmap --- Wrong choice for extreme shading ');
                        end
                    else
                        shvalue=varargin{i+1};
                        disp([' Extreme shading with values set to ',sprintf(' %d  \n',shvalue)]);
                    end
                case 'colormap'
                    colmap = varargin{i+1};
                    colormap(colmap);
                    disp(sprintf(' Colormap set to %s',colmap));
                case 'shading'
                    flshad=varargin{i+1};
                    disp(sprintf('Shading is  %s ',flshad));
                case 'undefvalue'
                    uvalue=varargin{i+1};
                case 'colorbar'
                    cbar=varargin{i+1};
                case 'nocontours'
                    contours=0; disp(' Not displying contours');
                case 'landcolor'
                    landcolor=varargin{i+1};
                case 'label'
                    labelfrequency=varargin{i+1};
                case 'overlay'
                    flover=1;
                case 'projection'
                    proj=varargin{i+1};
                case 'viszero'
                    viszero=varargin{i+1};
                case 'latlabels'
                    yt=varargin{i+1};
                case 'lonlabels'
                    xt=varargin{i+1};
                otherwise
            end
        end
    end

end


%Identify Field
%
%
[s,a,restype]= makefield(f);

% Set for undefined values
%
%
notavalue=find(a == uvalue); a(notavalue)=NaN;

%Generate grid for vector data
[llon,llat,dlsm]= makeggrid(restype);

% Generate wrap-around
%
b= [a'  a(1,:)'];

%  Coast
load coast;
if lonbox(2) - lonbox(1) < 80;
    dmerid= [];
else
    dmerid=60;
end
if latbox(2) - latbox(1) < 40;
    dpar= [];
else
    dpar=30;
end

if flover > 0
    hold on;
else
    axesm (proj,'frame','on','flatlimit',[], ...
        'maplatlimit', latbox, 'maplonlimit', lonbox, ...
        'parallellabel','on', 'meridianlabel','on',...
        'mlinelocation',dmerid,'plinelocation',dpar, ...
        'labelformat','compass', ...
        'origin',[ 0 mean(lonbox) 0], ...
        'grid','on');

    setm(gca,'mlabelparallel','s');
    setm(gca,'fontname','times');
    hold on;
end

title(tit,   'fontsize',12,'fontname','times', 'fontweight', 'bold');

% Pick contours
[co]=mkcontour(a,ccc);

if strcmp(flshad,'on')

%Plot filled contours - Use always patches
[c,hp]=contourfm(llat,llon,b,co);

% Check if contours borders are needed
if contours > 0
    set(hp,'edgecolor','k');
else
    set(hp,'edgecolor','none')
end;

% Define colormap along the contours
if length(co) > 2; contourcmap(co,colmap);end;

%Colorbar if needed
if strcmp(cbar,'on');
    hcb=colorbar('horiz','xminortick','on');
end

%Labels if needed
if labelfrequency > 0
    clabelm(c,hp,co(1:labelfrequency:length(co)), ...
        'fontsize',8,'fontname','times', 'fontweight', 'bold',...
        'rotation',0,'backgroundcolor','none','labelspacing', 200);
end ;

%control color
mkcolor(co,shvalue);

%Dashed line if negative are present
if co(1)*co(length(co)) <= 0
    % Use dash for negative
    ht=cell2mat(get(hp,'cdata'));
    set(hp(ht < 0),'linestyle','--');
    if strcmp(viszero,'on')
        set(hp(ht == 0),'linewidth',1.5);
    else
        set(hp(ht == 0),'edgecolor','none');
    end
end
else

%Plot only contours
[c,hp]=contourm(llat,llon,b,co);
set(gca,'color','none');

%Dashed line if negative are present
if co(1)*co(length(co)) <= 0
    % Use dash for negative
    hc=allchild(hp);
    ht=cell2mat(get(hc,'userdata'));
    set(hc(ht < 0),'linestyle','--');
    if strcmp(viszero,'on')
        set(hc(ht == 0),'linewidth',1.0);
    else
        set(hp(ht == 0),'edgecolor','none');
    end
end

 %Labels if needed
if labelfrequency > 0
    clabelm(c,hp,co(1:labelfrequency:length(co)), ...
        'fontsize',8,'fontname','times', 'fontweight', 'bold',...
        'rotation',0,'backgroundcolor','none','labelspacing', 200);
end ;
end

% If this is an overlay do not plot coasts
if flover < 1
    hc=plotm(lat,long,'linewidth',1,'color','black');
    if ~isempty(landcolor)
        patchesm(lat,long,'facecolor',landcolor);
    end
end

tightmap;
hold off;
return;