Index: /issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m	(revision 21670)
+++ /issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m	(revision 21671)
@@ -56,4 +56,5 @@
 % 04/10/15: Suggest a workaround for issue #41 (missing font path; thanks Mariia Fedotenkova)
 % 22/02/16: Bug fix from latest release of this file (workaround for issue #41)
+% 20/03/17: Added informational message in case of GS croak (issue #186)
 
     % Intialise the options string for ghostscript
@@ -159,4 +160,5 @@
                 fprintf(2, '  or maybe the%s option(s) are not accepted by your GS version\n', gs_options);
             end
+            fprintf(2, '  or maybe you have another gs executable in your system''s path\n');
             fprintf(2, 'Ghostscript options: %s\n\n', orig_options);
             error(message);
Index: /issm/trunk-jpl/externalpackages/export_fig/export_fig.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/export_fig.m	(revision 21670)
+++ /issm/trunk-jpl/externalpackages/export_fig/export_fig.m	(revision 21671)
@@ -25,4 +25,5 @@
 %   export_fig ... -update
 %   export_fig ... -nofontswap
+%   export_fig ... -linecaps
 %   export_fig(..., handle)
 %
@@ -43,4 +44,5 @@
 %   - Vector formats: pdf, eps
 %   - Bitmap formats: png, tiff, jpg, bmp, export to workspace
+%   - Rounded line-caps (optional; pdf & eps only)
 %
 % This function is especially suited to exporting figures for use in
@@ -153,4 +155,5 @@
 %             done in vector formats (only): 11 standard Matlab fonts are
 %             replaced by the original figure fonts. This option prevents this.
+%   -linecaps - option to create rounded line-caps (vector formats only).
 %   handle -  The handle of the figure, axes or uipanels (can be an array of
 %             handles, but the objects must be in the same figure) to be
@@ -241,4 +244,8 @@
 % 17/05/16: Fixed case of image YData containing more than 2 elements (issue #151)
 % 08/08/16: Enabled exporting transparency to TIF, in addition to PNG/PDF (issue #168)
+% 11/12/16: Added alert in case of error creating output PDF/EPS file (issue #179)
+% 13/12/16: Minor fix to the commit for issue #179 from 2 days ago
+% 22/03/17: Fixed issue #187: only set manual ticks when no exponent is present
+% 09/04/17: Added -linecaps option (idea by Baron Finer, issue #192)
 %}
 
@@ -271,5 +278,5 @@
     else
         % Check we have a figure
-        if ~isequal(get(fig, 'Type'), 'figure');
+        if ~isequal(get(fig, 'Type'), 'figure')
             error('Handle must be that of a figure, axes or uipanel');
         end
@@ -583,5 +590,5 @@
                 if hasTransparency || hasPatches
                     % This is *MUCH* slower, but more accurate for patches and transparent annotations (issue #39)
-                    renderer = '-painters';
+                    renderer = '-painters'; %ISSM fix
                 else
                     renderer = '-painters';
@@ -655,5 +662,18 @@
                 eps2pdf(tmp_nam, pdf_nam_tmp, 1, options.append, options.colourspace==2, options.quality, options.gs_options);
                 % Ghostscript croaks on % chars in the output PDF file, so use tempname and then rename the file
-                try movefile(pdf_nam_tmp, pdf_nam, 'f'); catch, end
+                try
+                    % Rename the file (except if it is already the same)
+                    % Abbie K's comment on the commit for issue #179 (#commitcomment-20173476)
+                    if ~isequal(pdf_nam_tmp, pdf_nam)
+                        movefile(pdf_nam_tmp, pdf_nam, 'f');
+                    end
+                catch
+                    % Alert in case of error creating output PDF/EPS file (issue #179)
+                    if exist(pdf_nam_tmp, 'file')
+                        error(['Could not create ' pdf_nam ' - perhaps the folder does not exist, or you do not have write permissions']);
+                    else
+                        error('Could not generate the intermediary EPS file.');
+                    end
+                end
             catch ex
                 % Delete the eps
@@ -663,5 +683,5 @@
             % Delete the eps
             delete(tmp_nam);
-            if options.eps
+            if options.eps || options.linecaps
                 try
                     % Generate an eps from the pdf
@@ -669,5 +689,20 @@
                     eps_nam_tmp = strrep(pdf_nam_tmp,'.pdf','.eps');
                     pdf2eps(pdf_nam, eps_nam_tmp);
-                    movefile(eps_nam_tmp,  [options.name '.eps'], 'f');
+
+                    % Issue #192: enable rounded line-caps
+                    if options.linecaps
+                        fstrm = read_write_entire_textfile(eps_nam_tmp);
+                        fstrm = regexprep(fstrm, '[02] J', '1 J');
+                        read_write_entire_textfile(eps_nam_tmp, fstrm);
+                        if options.pdf
+                            eps2pdf(eps_nam_tmp, pdf_nam, 1, options.append, options.colourspace==2, options.quality, options.gs_options);
+                        end
+                    end
+
+                    if options.eps
+                        movefile(eps_nam_tmp, [options.name '.eps'], 'f');
+                    else  % if options.pdf
+                        try delete(eps_nam_tmp); catch, end
+                    end
                 catch ex
                     if ~options.pdf
@@ -856,4 +891,5 @@
         'update',       false, ...
         'fontswap',     true, ...
+        'linecaps',     false, ...
         'gs_options',   {{}});
 end
@@ -946,4 +982,6 @@
                     case 'nofontswap'
                         options.fontswap = false;
+                    case 'linecaps'
+                        options.linecaps = true;
                     otherwise
                         try
@@ -1161,5 +1199,5 @@
 
 function A = rgb2grey(A)
-    A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A)); %#ok<ZEROLIKE>
+    A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A)); % #ok<ZEROLIKE>
 end
 
@@ -1257,7 +1295,20 @@
         M = {M};
     end
-    M = cellfun(@(c) strcmp(c, 'linear'), M);
-    set(Hlims(M), [ax 'TickMode'], 'manual');
-    %set(Hlims(M), [ax 'TickLabelMode'], 'manual');  % this hides exponent label in HG2!
+    %idx = cellfun(@(c) strcmp(c, 'linear'), M);
+    idx = find(strcmp(M,'linear'));
+    %set(Hlims(idx), [ax 'TickMode'], 'manual');  % issue #187
+    %set(Hlims(idx), [ax 'TickLabelMode'], 'manual');  % this hides exponent label in HG2!
+    for idx2 = 1 : numel(idx)
+        try
+            % Fix for issue #187 - only set manual ticks when no exponent is present
+            hAxes = Hlims(idx(idx2));
+            props = {[ax 'TickMode'],'manual', [ax 'TickLabelMode'],'manual'};
+            if isempty(strtrim(hAxes.([ax 'Ruler']).SecondaryLabel.String))
+                set(hAxes, props{:});  % no exponent, so update moth ticks and tick labels to manual
+            end
+        catch  % probably HG1
+            set(hAxes, props{:});  % revert back to old behavior
+        end
+    end
 end
 
Index: /issm/trunk-jpl/externalpackages/export_fig/pdftops.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/pdftops.m	(revision 21670)
+++ /issm/trunk-jpl/externalpackages/export_fig/pdftops.m	(revision 21671)
@@ -33,5 +33,5 @@
 
     % Call pdftops
-    [varargout{1:nargout}] = system(sprintf('"%s" %s', xpdf_path, cmd));
+    [varargout{1:nargout}] = system([xpdf_command(xpdf_path()) cmd]);
 end
 
@@ -138,5 +138,5 @@
 function good = check_xpdf_path(path_)
     % Check the path is valid
-    [good, message] = system(sprintf('"%s" -h', path_)); %#ok<ASGLU>
+    [good, message] = system([xpdf_command(path_) '-h']); %#ok<ASGLU>
     % system returns good = 1 even when the command runs
     % Look for something distinct in the help text
@@ -149,2 +149,19 @@
     end
 end
+
+function cmd = xpdf_command(path_)
+    % Initialize any required system calls before calling ghostscript
+    % TODO: in Unix/Mac, find a way to determine whether to use "export" (bash) or "setenv" (csh/tcsh)
+    shell_cmd = '';
+    if isunix
+        % Avoids an error on Linux with outdated MATLAB lib files
+        % R20XXa/bin/glnxa64/libtiff.so.X
+        % R20XXa/sys/os/glnxa64/libstdc++.so.X
+        shell_cmd = 'export LD_LIBRARY_PATH=""; ';
+    end
+    if ismac
+        shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';
+    end
+    % Construct the command string
+    cmd = sprintf('%s"%s" ', shell_cmd, path_);
+end
Index: /issm/trunk-jpl/externalpackages/export_fig/print2array.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/print2array.m	(revision 21670)
+++ /issm/trunk-jpl/externalpackages/export_fig/print2array.m	(revision 21671)
@@ -50,4 +50,5 @@
 % 28/05/15: Fixed issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() func)
 % 07/07/15: Fixed issue #83: use numeric handles in HG1
+% 11/12/16: Fixed cropping issue reported by Harry D.
 %}
 
@@ -102,34 +103,41 @@
     end
     if nargin > 2 && strcmp(renderer, '-painters')
-        % Print to eps file
-        if isTempDirOk
-            tmp_eps = [tempname '.eps'];
-        else
-            tmp_eps = fullfile(fpath,[fname '.eps']);
-        end
-        print2eps(tmp_eps, fig, 0, renderer, '-loose');
+        % First try to print directly to tif file
         try
-            % Initialize the command to export to tiff using ghostscript
-            cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
-            % Set the font path
-            fp = font_path();
-            if ~isempty(fp)
-                cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
-            end
-            % Add the filenames
-            cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"' gs_options];
-            % Execute the ghostscript command
-            ghostscript(cmd_str);
-        catch me
+            % Print the file into a temporary TIF file and read it into array A
+            [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam);
+            if err, rethrow(ex); end
+        catch  % error - try to print to EPS and then using Ghostscript to TIF
+            % Print to eps file
+            if isTempDirOk
+                tmp_eps = [tempname '.eps'];
+            else
+                tmp_eps = fullfile(fpath,[fname '.eps']);
+            end
+            print2eps(tmp_eps, fig, 0, renderer, '-loose');
+            try
+                % Initialize the command to export to tiff using ghostscript
+                cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
+                % Set the font path
+                fp = font_path();
+                if ~isempty(fp)
+                    cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
+                end
+                % Add the filenames
+                cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"' gs_options];
+                % Execute the ghostscript command
+                ghostscript(cmd_str);
+            catch me
+                % Delete the intermediate file
+                delete(tmp_eps);
+                rethrow(me);
+            end
             % Delete the intermediate file
             delete(tmp_eps);
-            rethrow(me);
-        end
-        % Delete the intermediate file
-        delete(tmp_eps);
-        % Read in the generated bitmap
-        A = imread(tmp_nam);
-        % Delete the temporary bitmap file
-        delete(tmp_nam);
+            % Read in the generated bitmap
+            A = imread(tmp_nam);
+            % Delete the temporary bitmap file
+            delete(tmp_nam);
+        end
         % Set border pixels to the correct colour
         if isequal(bcol, 'none')
@@ -168,28 +176,6 @@
             renderer = '-opengl';
         end
-        err = false;
-        % Set paper size
-        old_pos_mode = get(fig, 'PaperPositionMode');
-        old_orientation = get(fig, 'PaperOrientation');
-        set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
-        try
-            % Workaround for issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() function)
-            fp = [];  % in case we get an error below
-            fp = findall(fig, 'Type','patch', 'LineWidth',0.75);
-            set(fp, 'LineWidth',0.5);
-            % Fix issue #83: use numeric handles in HG1
-            if ~using_hg2(fig),  fig = double(fig);  end
-            % Print to tiff file
-            print(fig, renderer, res_str, '-dtiff', tmp_nam);
-            % Read in the printed file
-            A = imread(tmp_nam);
-            % Delete the temporary file
-            delete(tmp_nam);
-        catch ex
-            err = true;
-        end
-        set(fp, 'LineWidth',0.75);  % restore original figure appearance
-        % Reset paper size
-        set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
+        % Print the file into a temporary TIF file and read it into array A
+        [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam);
         % Throw any error that occurred
         if err
@@ -220,4 +206,33 @@
 end
 
+% Function to create a TIF image of the figure and read it into an array
+function [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam)
+    err = false;
+    ex = [];
+    % Temporarily set the paper size
+    old_pos_mode    = get(fig, 'PaperPositionMode');
+    old_orientation = get(fig, 'PaperOrientation');
+    set(fig, 'PaperPositionMode','auto', 'PaperOrientation','portrait');
+    try
+        % Workaround for issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() function)
+        fp = [];  % in case we get an error below
+        fp = findall(fig, 'Type','patch', 'LineWidth',0.75);
+        set(fp, 'LineWidth',0.5);
+        % Fix issue #83: use numeric handles in HG1
+        if ~using_hg2(fig),  fig = double(fig);  end
+        % Print to tiff file
+        print(fig, renderer, res_str, '-dtiff', tmp_nam);
+        % Read in the printed file
+        A = imread(tmp_nam);
+        % Delete the temporary file
+        delete(tmp_nam);
+    catch ex
+        err = true;
+    end
+    set(fp, 'LineWidth',0.75);  % restore original figure appearance
+    % Reset the paper size
+    set(fig, 'PaperPositionMode',old_pos_mode, 'PaperOrientation',old_orientation);
+end
+
 % Function to return (and create, where necessary) the font path
 function fp = font_path()
Index: /issm/trunk-jpl/externalpackages/export_fig/print2eps.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/print2eps.m	(revision 21670)
+++ /issm/trunk-jpl/externalpackages/export_fig/print2eps.m	(revision 21671)
@@ -27,4 +27,5 @@
 %       fontswap   - Whether to swap non-default fonts in figure. Default: true
 %       renderer   - Renderer used to generate bounding-box. Default: 'opengl'
+%                    (available only via the struct alternative)
 %       crop_amounts - 4-element vector of crop amounts: [top,right,bottom,left]
 %                    (available only via the struct alternative)
