function astPlotWMMContours( WMMResults, plotWMM, param, landAreas, geod_lat, geod_lon, decimal_year, model_epoch)
% astFormatContourLines: Helper function to create the contour maps for the
% astWMMContours example

% Copyright 2009-2017 The MathWorks, Inc.

    %**************************************
    % Create Mercator Plot : -70 < lat < 70
    %**************************************
    axesMercator = subplot(2,2,1:2);
    
    axesm('MapProjection','mercator','MapLatLimit',[-70 70],...
        'MapLonLimit',[-180 180],'Grid','on','Frame','on',...
        'MeridianLabel','on','LabelFormat','none',...
        'MLabelParallel','South','MLabelLocation',60,...
        'ParallelLabel','on','PLabelLocation',30,'FontSize',9)
    
    % plot world coast line with Mapping Toolbox function |geoshow|
    geoshow(landAreas,'FaceColor',[1 1 .5],'EdgeColor',[.6 .6 .6]);
    
    % Overlay contours
    hold on
    
    % Data is stored in the matrix WMMResults (length(lat),1:7,length(lon)):
    [ccc,hhh] = contourm(geod_lat,geod_lon,squeeze(WMMResults(:,plotWMM.colIdx{param},:)),...
        'levelStep',plotWMM.level_step{param},...
        'edgecolor',[.7 .7 .7],'linewidth',.05);

    % Format major contour lines' color and width to match published reports.
    % http://www.ngdc.noaa.gov/geomag/WMM/
    labelLine = plotWMM.formatRange{param}(1):plotWMM.text_step{param}:plotWMM.formatRange{param}(2);
    astFormatContourLines(ccc,hhh,labelLine);
    
    % Plot Title
    if ~strcmpi(plotWMM.magParam{param},{'h','f'})
        title({['Epoch: ', model_epoch, ...
            '  Decyear: ',num2str(decimal_year), '  Contour interval: ', ...
            num2str(plotWMM.level_step{param}),' ',plotWMM.unitsStr{param}];...
            ['Red: Positive (', plotWMM.redDir{param}, ')  Blue: Negative (', ...
            plotWMM.blueDir{param}, ')  Green: Zero (Agonic) Line']});
    else
        title({['Epoch: ', model_epoch, ...
            ' Decyear: ',num2str(decimal_year), ' Contour interval: ',...
            num2str(plotWMM.level_step{param}),' ',plotWMM.unitsStr{param}];});
    end
    
    % Format axes
    astTightMap(axesMercator);
    set(axesMercator,'Position',[0.0300 0.5038 0.9688 0.4265]);
    axis(axesMercator,'off');  
    
    hold off
    
    %***************************************
    % Create Stereographic Plot : North Pole
    %***************************************
    axesStereographicNorth = subplot(2,2,3);
    
    axesm('MapProjection','stereo','MapLatLimit',[55 90],...
        'MapLonLimit',[-180 180],'Grid','on','Frame','on',...
        'MeridianLabel','on','LabelFormat','none',...
        'MLabelParallel',55,'MLabelLocation',30,'ParallelLabel','off',...
        'PLabelLocation',30,'PLineLocation',10,'FontSize',9)
    
    geoshow(landAreas,'FaceColor',[1 1 .5],'EdgeColor',[.6 .6 .6],'DisplayType','polygon');
    
    % Overlay contours
    hold on
    
    % Data is stored in the matrix WMMResults (length(lat),1:7,length(lon)):
    [ccc2,hhh2] = contourm(geod_lat,geod_lon,squeeze(WMMResults(:,plotWMM.colIdx{param},:)),'levelStep',plotWMM.level_stepN{param},...
        'edgecolor',[.7 .7 .7],'linewidth',.05);       
        
    % Format major contour lines' color and width to match published reports.
    % http://www.ngdc.noaa.gov/geomag/WMM/
    labelLine = plotWMM.formatRange{param}(1):plotWMM.text_stepN{param}:plotWMM.formatRange{param}(2);
    astFormatContourLines(ccc2,hhh2,labelLine);
    
    % Format axes
    astTightMap(axesStereographicNorth);
    set(axesStereographicNorth,'Position',[0.0965 0.025 0.4016 0.3883]);
    axis(axesStereographicNorth,'off');
    title('North Polar Region','FontSize',10.5);
    
    hold off
    
    %***************************************
    % Create Stereographic Plot : South Pole
    %***************************************
    axesStereographicSouth = subplot(2,2,4);
    
    axesm('MapProjection','stereo','MapLatLimit',[-90 -55],...
        'Grid','on','Frame','on',...
        'MeridianLabel','on','LabelFormat','none',...
        'MLabelParallel',55,'MLabelLocation',30,'ParallelLabel','off',...
        'PLabelLocation',30,'PLineLocation',10,'FontSize',9)
    
    geoshow(landAreas,'FaceColor',[1 1 .5],'EdgeColor',[.6 .6 .6],'DisplayType','polygon');
    
    % Overlay contours
    hold on
    
    % Data is stored in the matrix WMMResults (length(lat),1:7,length(lon)):
    [ccc3,hhh3] = contourm(geod_lat,geod_lon,squeeze(WMMResults(:,plotWMM.colIdx{param},:)),...
        'levelStep',plotWMM.level_stepS{param},...
        'edgecolor',[.7 .7 .7],'linewidth',.05);        
    
    % Format major contour lines' color and width to match published reports.
    % http://www.ngdc.noaa.gov/geomag/WMM/
    labelLine = plotWMM.formatRange{param}(1):plotWMM.text_stepS{param}:plotWMM.formatRange{param}(2);
    astFormatContourLines(ccc3,hhh3,labelLine);
    
    % Format axes
    astTightMap(axesStereographicSouth);
    set(axesStereographicSouth,'Position',[0.5368 0.025 0.4016 0.3883]);
    axis(axesStereographicSouth,'off');
    title('South Polar Region','FontSize',10.5);
    
    hold off
    
end

function astTightMap(ax)

    param = 0.002;

    hframe = handlem('Frame',ax);

    newframe = 0;
    if isempty(hframe)
        hframe = framem;
        newframe = true;
    end

    xframe = get(hframe,'Xdata');
    yframe = get(hframe,'Ydata');

    xdiff = max(xframe) - min(xframe);
    ydiff = max(yframe) - min(yframe);

    xLimits = [min(xframe) max(xframe)] + param*xdiff*[-1 1];
    yLimits = [min(yframe) max(yframe)] + param*ydiff*[-1 1];

    set(ax,'XLim',xLimits,'YLim',yLimits)

    if newframe
        delete(hframe);
    end
end