Changeset 23663


Ignore:
Timestamp:
01/25/19 10:47:13 (6 years ago)
Author:
Mathieu Morlighem
Message:

CHG: updated dem externalpackage

Location:
issm/trunk-jpl/externalpackages/dem
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/externalpackages/dem/dem.m

    r23108 r23663  
    1 function [h,I]=dem(x,y,z,varargin)
     1function varargout=dem(x,y,z,varargin)
    22%DEM Shaded relief image plot
     3%
    34%       DEM(X,Y,Z) plots the Digital Elevation Model defined by X and Y
    45%       coordinate vectors and elevation matrix Z, as a lighted image using
    56%       specific "landcolor" and "seacolor" colormaps. DEM uses IMAGESC
    67%       function which is much faster than SURFL when dealing with large
    7 %       high-resolution DEM.
    8 %
    9 %       DEM(X,Y,Z,OPT) specifies options with OPT = [A,C,SCUT,ZMIN,ZMAX,ZCUT],
    10 %       where sorted optional scalars are:
    11 %         A = azimuth light (in degrees relative to North). Default is A = -45
    12 %             for a natural northwestern illumination.
    13 %         C = controls contrast, as the exponent of the gradient value. Default
    14 %             is C = 1 for linear contrast; use C = 0 to remove lighting,
    15 %             C = 0.5 for moderate lighting, C = 2 or more for strong contrast.
    16 %         SCUT = controls lighting scale saturation with a median-style filter
    17 %             in % of elements. Default is SCUT = 0.2 (0.2% maximum gradient
    18 %             values is ignored). Use SCUT = 0 for full scale gradient.
    19 %         ZMIN,ZMAX = fixes min and max elevation values for colormap. Use NaN
    20 %             to keep real min and/or max data values.
    21 %         ZCUT = median-style filter to cut extremes values (in % of elements).
    22 %             Default is ZCUT = 0.5 to ignore the 0.5% of most min/max
    23 %             elevation values. Use ZCUT = 0 for full scale.
    24 %       Use OPT=[] to keep default values and define following arguments.
    25 %
    26 %       DEM(...,CMAP) uses CMAP colormap instead of default (landcolor, if
    27 %       exists or jet). Note that defining CMAP disables the default SEACOLOR
    28 %       colormap attribution for Z<=0 elevations.
    29 %
    30 %       DEM(...,NOVALUE) defines the values that will be replaced by NaN. This
    31 %       might be mandatory for DEM that use a value like -99999 or -32768 if
    32 %       you don't want a flat image...
    33 %
    34 %       DEM(...,SEACOLOR) sets the colormap used for zero and negative values.
    35 %       Default is seacolor (if exists) or single color [0.7,0.9,1] (a light
    36 %       cyan) to simulate sea color. Use [] to apply colormap CMAP on the full
    37 %       elevation scale.
    38 %
    39 %       DEM(...,'interp') interpolates linearly NaN values (fills the gaps).
    40 %
    41 %       DEM(...,'lake') detects automaticaly flat areas different from sea
    42 %       level (non-zero elevations) and draws them as lake surfaces.
    43 %
    44 %       DEM(...,'dec') plots classic basemap-style axis, considering
    45 %       coordinates as cartesian (decimal).
    46 %
    47 %       DEM(...,'dms') plots geographic basemap-style axis in deg/min/sec,
    48 %       considering coordinates X as longitude and Y as latitude. Axis aspect
    49 %       ratio will be adjusted to approximatively preserve distances (this is 
    50 %       not a real projection!).
    51 %
    52 %       DEM(...,'scale') adds a legend to the right of graph, with elevation
    53 %       scale (colormap) and a distance scale if 'dms' option is used.
    54 %
    55 %       [H,I]=DEM(...) returns graphic handle H and illuminated image as I, an
    56 %       MxNx3 matrix (if Z is MxN).
    57 %
    58 %       Informations:
    59 %        - For optimization purpose, DEM will automatically decimate data to
    60 %          limit to a total of 1500x1500 pixels images. To avoid it, use option
    61 %          DEM(...,'nodecim') or DEM(...,'decim',N) where N is an integer, but
    62 %          be aware that large grids may require computer ressources and induce 
    63 %          disk swap or memory errors.
    64 %        - Colormaps are Mx3 RGB matrix so it is easy to modify saturation
    65 %          (CMAP.^N), set darker (CMAP/N), lighter ((N - 1 + CMAP)/N), inverse
    66 %           it (flipud(CMAP)), etc...
    67 %        - To get free worldwide topographic data (SRTM), see READHGT function.
     8%       high-resolution DEM. It produces also high-quality and moderate-size
     9%       Postscript image adapted for publication.
     10%
     11%       DEM(X,Y,Z,'Param1',Value1,'Param2',Value2,...) specifies options or
     12%       parameter/value couple (case insensitive):
     13%
     14%       [H,I] = DEM(...); returns graphic handle H and optional illuminated
     15%       image as I, a MxNx3 matrix (if Z is MxN and DECIM is 1).
     16%
     17%       I = DEM(...,'noplot') returns a structure I containing fields x, y, z,
     18%       and illuminated image .rgb without producing a graph on current figure.
     19%
     20%
     21%       --- Lighting options ---
     22%
     23%       'Azimuth',A
     24%               Light azimuth in degrees clockwise relative to North. Default is
     25%               A = -45 for     a natural northwestern illumination.
     26%
     27%       'Contrast',C
     28%               Light contrast, as the exponent of the gradient value:
     29%                       C = 1 for linear contrast (default),
     30%                       C = 0 to remove lighting,
     31%                       C = 0.5 for moderate lighting,
     32%                       C = 2 or more for strong contrast.
     33%
     34%       'LCut',LC
     35%               Lighting scale saturation cut with a median-style filter in % of
     36%           elements, such as LC% of maximum gradient values are ignored:
     37%                       LC = 0.2 is default,
     38%                       LC = 0 for full scale gradient.
     39%
     40%       'km'
     41%               Stands that X and Y coordinates are in km instead of m (default).
     42%               This allows correct lighting. Ignored if LATLON option is used.
     43%
     44%
     45%       --- Elevation colorscale options ---
     46%
     47%       'ZLim',[ZMIN,ZMAX]
     48%               Fixes min and max elevation values for colormap. Use NaN to keep
     49%               real min and/or max data values.
     50%
     51%       'ZCut',ZC
     52%               Median-style filter to cut extremes values of Z (in % of elements),
     53%               such that ZC% of most min/max elevation values are ignored in the
     54%               colormap application:
     55%                       ZC = 0.5 is default,
     56%                       ZC = 0 for full scale.
     57%
     58%
     59%       --- "No Value" elevation options ---
     60%
     61%       'NoValue',NOVALUE
     62%               Defines the values that will be replaced by NaN. Note that values
     63%               equal to minimum of Z class are automatically detected as NaN
     64%               (e.g., -32768 for int16 class).
     65%
     66%       'NaNColor',[R,G,B]
     67%               Sets the RGB color for NaN/NoValue pixels (default is a dark gray).
     68%               Note that your must specify a valid 3-scalar vector (between 0 and
     69%               1);     color characters like 'w' or 'k' are not allowed, use [1,1,1]
     70%               or [0,0,0] instead.
     71%
     72%       'Interp'
     73%               Interpolates linearly all NaN values (fills the gaps using linear
     74%               triangulation), using an optimized algorithm.
     75%
     76%
     77%       --- Colormap options ---
     78%
     79%       'LandColor',LMAP
     80%               Uses LMAP colormap instead of default (landcolor, if exists or
     81%               jet) for Z > 0 elevations.
     82%
     83%       'SeaColor',SMAP
     84%               Sets the colormap used for Z <= 0 elevations. Default is seacolor
     85%               (if exists) or single color [0.7,0.9,1] (a light cyan) to simulate
     86%               sea color.
     87%
     88%       'ColorMap',CMAP
     89%               Uses CMAP colormap for full range of elevations, instead of default
     90%               land/sea. This option overwrites LANDCOLOR/SEACOLOR options.
     91%
     92%       'Lake'
     93%               Detects automaticaly flat areas different from sea level (non-zero
     94%               elevations) and colors them as lake surfaces.
     95%
     96%       'Watermark',N
     97%               Makes the whole image lighter by a factor of N.
     98%
     99%
     100%       --- Basemap and scale options ---
     101%
     102%       'Legend'
     103%               Adds legends to the right of graph: elevation scale (colorbar)
     104%               and a distance scale (in km).
     105%
     106%       'Cartesian'
     107%               Plots classic basemap-style axis, considering coordinates X and Y
     108%               as cartesian in meters. Use parameter "km' for X/Y in km.
     109%
     110%       'LatLon'
     111%               Plots geographic basemap-style axis in deg/min/sec, considering
     112%               coordinates X as longitude and Y as latitude. Axis aspect ratio
     113%               will be adjusted to approximatively preserve distances (this is 
     114%               not a real projection!). This overwrites ZRatio option.
     115%
     116%       'AxisEqual', 'auto' (default) | 'manual' | 'off'
     117%               When 'Cartesian' or 'LatLon' option is used, automatic axes scaling
     118%               is applied to respect data aspect ratio. Default mode is 'auto' and
     119%               uses AXIS EQUAL and DASPECT functions. The 'manual' mode modifies
     120%               axes width or height with respect to the paper size in order to
     121%               produce correct data scaling at print (but not necessarily at
     122%               screen). The 'off' mode disables any scaling.
     123%
     124%       Additionnal options for basemap CARTESIAN or LATLON:
     125%
     126%       'BorderWidth',BW
     127%               Border width of the basemap axis, in % of axis height. Default is
     128%               BW = 1%.
     129%
     130%       'XTick',DX
     131%       'YTick',DY
     132%               X and Y Tick length (same unit as X and Y). Default is automatic.
     133%               Tick labels are every 2 ticks.
     134%
     135%       'FontSize',FS
     136%               Font size for X and Y tick labels. Default is FS = 10.
     137%
     138%       'FontBold'
     139%               Font weight bold for tick labels.
     140%
     141%       'Position',P
     142%               Position of the tick labels: 'southwest' (default), 'southeast',
     143%               'northwest','northeast'
     144%
     145%
     146%       --- Decimation options ---
     147%
     148%       For optimization purpose, DEM will automatically decimate data to limit
     149%       to a total of 1500x1500 pixels images. To avoid this, use following
     150%       options, but be aware that large grids may require huge computer
     151%       ressources or induce disk swap or memory errors.
     152%
     153%       'Decim',N
     154%               Decimates matrix Z at 1/N times of the original sampling.
     155%
     156%       'NoDecim'
     157%               Forces full resolution of Z, no decimation.
     158%
     159%
     160%
     161%       --- Informations ---
     162%
     163%       Colormaps are Mx3 RGB matrix so it is easy to modify saturation
     164%       (CMAP.^N), set darker (CMAP/N), lighter (1 - 1/N + CMAP/N), inverse
     165%       it (flipud(CMAP)), etc...
     166%
     167%       To get free worldwide topographic data (SRTM), see READHGT function.
     168%
     169%       For backward compatibility, the former syntax is still accepted:
     170%       DEM(X,Y,Z,OPT,CMAP,NOVALUE,SEACOLOR) where OPT = [A,C,LC,ZMIN,ZMAX,ZC],
     171%       also option aliases DEC, DMS and SCALE, but there is no argument
     172%       checking. Please prefer the param/value syntax.
    68173%
    69174%       Author: François Beauducel <beauducel@ipgp.fr>
    70 %       Created: 2007-05-17
    71 %       Updated: 2013-01-05
    72 
    73 %       Copyright (c) 2013, François Beauducel, covered by BSD License.
     175%       Created: 2007-05-17 in Guadeloupe, French West Indies
     176%       Updated: 2016-01-31
     177
     178%       History:
     179%       [2016-04-19] v2.3
     180%               - major update (thanks to mas Wiwit)
     181%       [2016-01-31] v2.2
     182%               - adds option 'Position' for tick labels
     183%       [2015-08-22] v2.1
     184%               - minor fix (former versions of Matlab compatibility)
     185%       [2015-08-19] v2.0
     186%               - image is now 100% true color (including the legend colorbar),
     187%             thus completely independent from the figure colormap
     188%       [2014-10-14]
     189%               - 'decim' option allows oversampling (negative value)
     190%       [2014-06-06]
     191%               - improves backward compatibility (adds strjoin subfunction)
     192%       [2014-03-18]
     193%               - adds new axisequal option
     194%       [2013-03-11]
     195%               - new options: 'km', 'watermark', 'fontsize', 'bordersize'
     196%               - improve legend colorbar
     197%               - all options now passed as param/value
     198%       [2013-01-14]
     199%               - improved light rendering (using surface normals instead of gradient)
     200%               - improved 'lake' detection algorithm
     201%               - new 'nancolor' option to set NaN color
     202%               - adds a length scale with 'dec' option
     203%               - minor code improvements
     204%       [2013-01-07]
     205%               - adds 'interp' option (fill the gaps)
     206%               - adds 'seacolor' colormap for negative elevations (bathymetry)
     207%       [2013-01-02]
     208%               - adds a 'lake' option
     209%               - minor bug correction
     210%       [2012-09-26]
     211%               - now accepts row/column vectors for X and/or Y.
     212%       [2012-05-29]
     213%               - adds basemap-style axis in decimal or lat/lon modes
     214%               - adds elevation and distance scales
     215%       [2012-05-18]
     216%               - new landcolor.m colormap function
     217%               - new arguments to control colormap scaling
     218%               - median-style filters for light and colormap
     219%       [2012-04-26]
     220%               - Optimizations: adds a decimation for large DEM grids.
     221
     222%
     223%       Copyright (c) 2016, François Beauducel, covered by BSD License.
    74224%       All rights reserved.
    75225%
     
    100250end
    101251
    102 % default OPT arguments
    103 a = -45;
    104 c = 1;
    105 scut = 0.2;
    106 zmin = NaN;
    107 zmax = NaN;
    108 zcut = 0.5;
    109 grey = 0.2*[1,1,1];
    110 csea = [];
    111 fs = 10;        % tick label fontsize
    112 
    113 dec = 0;
    114 dms = 0;
    115 scale = 0;
    116 inter = 0;
    117 lake = 0;
    118 decimflag = 0;
    119 decim = 0;
    120 
    121 novalue_color = [0,0,0];
    122 
    123 if ~isnumeric(x) | ~isnumeric(y) | ~isnumeric(z)
     252holdon = ishold;
     253
     254degkm = 6378*pi/180; % one latitude degree in km
     255sea_color = [.7,.9,1]; % default sea color (light cyan)
     256grey = 0.2*[1,1,1]; % a dark gray
     257
     258
     259% -------------------------------------------------------------------------
     260% --- Manage input arguments
     261
     262% number of arguments param/value
     263nargs = 0;
     264
     265if ~isnumeric(x) || ~isnumeric(y) || ~isnumeric(z)
    124266        error('X,Y and Z must be numeric.')
    125267end
    126268
    127 if all(size(x) ~= 1) | all(size(y) ~= 1)
     269if all(size(x) ~= 1) || all(size(y) ~= 1)
    128270        error('X and Y must be vectors, not matrix.')
    129271end
    130272
    131 if length(x) ~= size(z,2) | length(y) ~= size(z,1)
     273if length(x) ~= size(z,2) || length(y) ~= size(z,1)
    132274        error('If Z has a size of [M,N], X must have a length of N, and Y a length of M.')
    133275end
    134276
    135 if nargin > 3
    136         dec = any(strcmp(varargin,'dec'));
    137         dms = any(strcmp(varargin,'dms'));
    138         if dms & any(abs(y) > 91)
    139                 error('With DMS option Y must be in valid latitudes interval (decimal degrees).')
    140         end
    141         scale = any(strcmp(varargin,'scale'));
    142         inter = any(strcmp(varargin,'interp'));
    143         lake = any(strcmp(varargin,'lake'));
    144         if any(strcmp(varargin,'nodecim'))
    145                 decim = 1;
    146                 decimflag = 1;
    147         end
    148         kdecim = find(strcmp(varargin,'decim'));
    149         if ~isempty(kdecim)
    150                 decimflag = 1;
    151                 if (kdecim + 1) <= nargin & isnumeric(varargin{kdecim+1})
    152                         decim = round(varargin{kdecim+1});
    153                         decimflag = 2;
    154                 end
    155         end
    156 end
    157 nargs = decimflag + dec + dms + scale + lake + inter;
    158 
    159 if (nargin - nargs) > 3
     277% OPTIONS and PARAM/VALUE arguments
     278                       
     279% AZIMUTH param/value
     280[s,az] = checkparam(varargin,'azimuth',@isscalar);
     281nargs = nargs + 2;
     282if s==0
     283        az = -45; % default
     284end
     285
     286% ELEVATION param/value
     287[s,el] = checkparam(varargin,'elevation',@isscalar);
     288nargs = nargs + 2;
     289if s==0
     290        el = 0; % default
     291end
     292
     293% CONTRAST param/value
     294[s,ct] = checkparam(varargin,'contrast',@isscalar);
     295nargs = nargs + 2;
     296if s
     297        ct = abs(ct);
     298else
     299        ct = 1; % default
     300end
     301
     302% LCUT param/value
     303[s,lcut] = checkparam(varargin,'lcut',@isperc);
     304nargs = nargs + 2;
     305if s==0
     306        lcut = .2; % default
     307end
     308
     309% NOVALUE param/value
     310[s,novalue] = checkparam(varargin,'novalue',@isscalar);
     311nargs = nargs + 2;
     312if s==0
     313        % default: min value for integer class / NaN for float
     314        S = whos('z');
     315        if strfind(S.class,'int')
     316                novalue = intmin(S.class);
     317        else
     318                novalue = NaN;
     319        end
     320end
     321
     322% NANCOLOR param/value
     323[s,novalue_color] = checkparam(varargin,'nancolor',@isrgb);
     324nargs = nargs + 2;
     325if s==0
     326        novalue_color = grey; % default
     327end
     328
     329% LANDCOLOR param/value
     330[s,cland] = checkparam(varargin,'landcolor',@isrgb);
     331nargs = nargs + 2;
     332if s==0
     333        % default: landcolor or jet
     334        if exist('landcolor','file')
     335                cland = landcolor.^1.3;
     336        else
     337                cland = jet(256);
     338        end
     339end
     340
     341% SEACOLOR param/value
     342[s,csea] = checkparam(varargin,'seacolor',@isrgb);
     343nargs = nargs + 2;
     344if s==0
     345        % default: seacolor or single color
     346        if exist('seacolor','file')
     347                csea = seacolor;
     348        else
     349                csea = sea_color;
     350        end
     351end
     352
     353% COLORMAP param/value
     354[s,cmap] = checkparam(varargin,'colormap',@isrgb);
     355nargs = nargs + 2;
     356if s
     357        cland = [];
     358        csea = [];
     359else
     360        % default
     361        cmap = cland;
     362end
     363
     364% ZLIM param/value
     365[s,zmm] = checkparam(varargin,'zlim',@isvec);
     366nargs = nargs + 2;
     367if s
     368        zmin = min(zmm);
     369        zmax = max(zmm);
     370else
     371        zmin = NaN; % default
     372        zmax = NaN; % default
     373end
     374
     375% ZCUT param/value
     376[s,zcut] = checkparam(varargin,'zcut',@isperc);
     377nargs = nargs + 2;
     378if s==0
     379        zcut = .5; % default
     380end
     381
     382% ZRATIO param/value
     383[s,zratio] = checkparam(varargin,'zratio',@isscalar);
     384nargs = nargs + 2;
     385if s==0
     386        zratio = 1; % default
     387end
     388
     389% WATERMARK param/value
     390[s,wmark] = checkparam(varargin,'watermark',@isscalar);
     391nargs = nargs + 2;
     392if s
     393        wmark = abs(wmark);
     394else
     395        wmark = 0; % default
     396end
     397
     398% DECIM param/value and NODECIM option
     399[s,decim] = checkparam(varargin,'decim',@isscalar);
     400if s
     401        decim = round(decim);
     402        nargs = nargs + 2;
     403else
     404        decim = any(strcmpi(varargin,'nodecim')); % default
     405        nargs = nargs + 1;
     406end
     407
     408% FONTSIZE param/value
     409[s,fs] = checkparam(varargin,'fontsize',@isscalar);
     410nargs = nargs + 2;
     411if s==0
     412        fs = 10; % default
     413end
     414
     415% BORDERWIDTH param/value
     416[s,bw] = checkparam(varargin,'borderwidth',@isperc);
     417nargs = nargs + 2;
     418if s==0
     419        bw = 1; % default
     420end
     421
     422% XTICK param/value
     423[s,ddx] = checkparam(varargin,'xtick',@isscalar);
     424nargs = nargs + 2;
     425if s==0
     426        ddx = 0; % default (automatic)
     427end
     428
     429% YTICK param/value
     430[s,ddy] = checkparam(varargin,'ytick',@isscalar);
     431nargs = nargs + 2;
     432if s==0
     433        ddy = 0; % default (automatic)
     434end
     435
     436% POSITION param/value
     437[s,tpos] = checkparam(varargin,'position',@ischar,{'southwest','southeast','northwest','northeast'});
     438nargs = nargs + 2;
     439if s==0
     440        tpos = 'southwest'; % default
     441end
     442
     443% AXISEQUAL param/value
     444[s,axeq] = checkparam(varargin,'axisequal',@ischar,{'auto','manual','off'});
     445nargs = nargs + 2;
     446if s==0 || ~any(strcmpi(axeq,{'manual','off'}))
     447        axeq = 'auto'; % default (automatic)
     448end
     449
     450% CROP param/value
     451[s,crop] = checkparam(varargin,'crop',@isvec,4);
     452nargs = nargs + 2;
     453
     454% options without argument value
     455km = any(strcmpi(varargin,'km'));
     456dec = any(strcmpi(varargin,'cartesian') | strcmpi(varargin,'dec'));
     457dms = any(strcmpi(varargin,'latlon') | strcmpi(varargin,'dms'));
     458scale = any(strcmpi(varargin,'legend') | strcmpi(varargin,'scale'));
     459inter = any(strcmpi(varargin,'interp'));
     460lake = any(strcmpi(varargin,'lake'));
     461fbold = any(strcmpi(varargin,'fontbold'));
     462noplot = any(strcmpi(varargin,'noplot'));
     463
     464
     465% for backward compatibility (former syntax)...
     466nargs = nargs + dec + dms + scale + lake + inter + km + fbold + noplot;
     467
     468if (nargin - nargs) > 3 && ~isempty(varargin{1})
    160469        opt = varargin{1};
    161470        if ~isnumeric(opt)
    162                 error('OPT = [A,C,S,ZMIN,ZMAX] argument must be numeric.');
     471                error('OPT = [A,C,S,ZMIN,ZMAX,ZCUT] argument must be numeric.');
    163472        end
    164473        if ~isempty(opt)
    165                 a = opt(1);
     474                az = opt(1);
    166475        end
    167476        if length(opt) > 1
    168                 c = opt(2);
    169                 if c < 0
    170                         error('C argument must be positive.');
    171                 end
     477                ct = opt(2);
    172478        end
    173479        if length(opt) > 2
    174                 scut = opt(3);
    175                 if scut < 0 | scut >= 100
    176                         error('SCUT argument must be a positive percentage.');
    177                 end
     480                lcut = opt(3);
    178481        end
    179482        if length(opt) > 4
     
    183486        if length(opt) > 5
    184487                zcut = opt(6);
    185                 if zcut < 0 | zcut >= 100
    186                         error('ZCUT argument must be a positive percentage.');
    187                 end
    188         end
    189 end
    190 
    191 if (nargin - nargs) < 5
    192         cmap = [];
    193 else
     488        end
     489end
     490
     491if (nargin - nargs) > 4 && ~isempty(varargin{2})
    194492        cmap = varargin{2};
    195         if ~isnumeric(cmap) | (~isempty(cmap) & (size(cmap,2) ~= 3 | min(cmap(:)) < 0 | max(cmap(:)) > 1))
    196                 error('CMAP must be a valid colormap (3-column [R,G,B] matrix with 0.0 to 1.0 values).')
    197         end
    198 end
    199 
    200 if (nargin - nargs) < 6
    201         novalue = NaN;
    202 else
     493        csea = [];
     494end
     495
     496if (nargin - nargs) > 5 && ~isempty(varargin{3})
    203497        novalue = varargin{3};
    204         if ~isnumeric(novalue) | numel(novalue) > 1
    205                 error('NOVALUE must be scalar.')
    206         end
    207 end
    208 
    209 if (nargin - nargs) < 7
    210         if isempty(cmap)
    211                 if exist('seacolor','file')
    212                         csea = seacolor(256);
    213                 else
    214                         csea = [.7,.9,1];
    215                 end
    216         end
    217 else
     498end
     499
     500if (nargin - nargs) > 6 && ~isempty(varargin{4})
    218501        csea = varargin{4};
    219         if ~isnumeric(csea)
    220                 error('Unknown option')
    221         elseif (~isempty(csea) & (size(csea,2) ~= 3 | min(csea) < 0 | max(csea) > 1))
    222                 error('SEACOLOR must be a valid [R,G,B] vector with 0.0 to 1.0 values).')
    223         end
    224 end
    225 
    226 if isempty(cmap)
    227         if exist('landcolor','file')
    228                 cmap = landcolor.^1.3;
    229         else
    230                 cmap = jet(256);
    231         end
    232 end
    233 
    234 
     502end
     503
     504
     505% further test of input arguments
     506if dms && any(abs(y) > 91)
     507        error('With LATLON option Y must be in valid latitudes interval (decimal degrees).')
     508end
     509
     510if km
     511        zratio = 1000;
     512end
     513
     514
     515% -------------------------------------------------------------------------
     516% --- Pre-process DEM data
     517
     518% crops data if needed
     519if numel(crop)==4
     520        fprintf('DEM: crops original data from [%g,%g,%g,%g] to [%g,%g,%g,%g]...\n', ...
     521                min(x(:)),max(x(:)),min(y(:)),max(y(:)),crop);
     522        kx = find(x >= crop(1) & x <= crop(2));
     523        ky = find(y >= crop(3) & y <= crop(4));
     524        x = x(kx);
     525        y = y(ky);
     526        z = z(ky,kx);
     527end
    235528
    236529% decimates data to avoid disk swap/out of memory...
     
    245538        y = y(1:n:end);
    246539        z = z(1:n:end,1:n:end);
    247         fprintf('DEM: on the plot data has been decimated by a factor of %d...\n',n);
     540        fprintf('DEM: data has been decimated by a factor of %d...\n',n);
    248541end
    249542
     
    251544z(z==novalue) = NaN;
    252545
     546if isempty(csea)
     547        k = (z~=0 & ~isnan(z));
     548else
     549        k = ~isnan(z);
     550end
     551
     552if isnan(zmin)
     553        zmin = nmedian(z(k),zcut/100);
     554end
     555if isnan(zmax)
     556        zmax = nmedian(z(k),1 - zcut/100);
     557end
     558dz = zmax - zmin;
     559
     560if decim & n < 0
     561        xi = linspace(x(1),x(end),-n*length(x));
     562        yi = linspace(y(1),y(end),-n*length(y))';
     563        [xx,yy] = meshgrid(xi,yi);
     564        z = interp2(x,y,z,xx,yy,'*cubic');
     565        x = xi;
     566        y = yi;
     567        fprintf('DEM: data has been oversampled by a factor of %d...\n',-n);
     568end
     569
    253570if inter
    254571        z = fillgap(x,y,z);
    255572end
    256573
    257 if isempty(csea)
    258         k = (z~=0 & ~isnan(z));
    259 else
    260         k = ~isnan(z);
    261 end
    262 
    263 if isnan(zmin)
    264         zmin = nmedian(z(k),zcut/100);
    265 end
    266 if isnan(zmax)
    267         zmax = nmedian(z(k),1 - zcut/100);
    268 end
    269 dz = zmax - zmin;
     574% -------------------------------------------------------------------------
     575% --- Process lighting
    270576
    271577if dz > 0
    272578        % builds the colormap: concatenates seacolor and landcolor around 0
    273         if ~isempty(csea) & zmin < 0 & zmax > 0
     579        if ~isempty(csea)
    274580                l = size(csea,1);
    275                 r = size(cmap,1)*abs(zmin)/zmax/l;
    276                 cmap = cat(1,interp1(1:l,csea,linspace(1,l,round(l*r)),'*linear'),cmap);
     581                if zmin < 0 && zmax > 0
     582                        r = size(cland,1)*abs(zmin)/zmax/l;
     583                        cmap = cat(1,interp1(1:l,csea,linspace(1,l,ceil(l*r)),'*linear'),cland);
     584                elseif zmax <=0
     585                        cmap = csea;
     586                end
    277587        end
    278588       
    279589        % normalisation of Z using CMAP and convertion to RGB
    280         I = ind2rgb(uint16((z - zmin)*(length(cmap)/dz)),cmap);
     590        I = ind2rgb(uint16(round((z - zmin)*(size(cmap,1) - 1)/dz) + 1),cmap);
    281591       
    282         if c > 0
     592        if ct > 0
    283593                % computes lighting from elevation gradient
    284                 [fx,fy] = gradient(z,x,y);
    285                 %fx = filter([1,0,0,0,-1],1,rf(z'))';
    286                 %fy = filter([1,0,0,0,-1],1,rf(z));
    287                 fxy = -fx*sind(a) - fy*cosd(a);
    288                 clear fx fy     % free some memory...
     594                %[fx,fy] = gradient(z,x,y);
     595                if dms
     596                        ryz = degkm*1000;
     597                        rxz = degkm*1000*cosd(mean(y));
     598                else
     599                        rxz = zratio;
     600                        ryz = zratio;
     601                end
     602                [xx,yy] = meshgrid(x*rxz,y*ryz);
     603                [fx,fy,fz] = surfnorm(xx,yy,z);
     604                [ux,uy,uz] = sph2cart((90-az)*pi/180,el*pi/180,1);
     605                fxy = fx*ux + fy*uy + fz*uz;
     606                clear xx yy fx fy fz    % free some memory...
    289607               
    290                 % lake option: zero gradient
    291                 if lake
    292                         dx = diff(z,1,2);
    293                         dy = diff(z,1,1);
    294                         u1 = ones(size(z,1),1);
    295                         u2 = ones(1,size(z,2));
    296                         z(cat(1,u2,dy)==0 & cat(1,dy,u2)==0 & cat(2,dx,u1)==0 & cat(2,u1,dx)==0) = 0;
    297                         clear dx dy     % free some memory...
    298                 end
    299 
    300608                fxy(isnan(fxy)) = 0;
    301609
    302                 % computes maximum absolute gradient (median-style), normalizes, saturates and duplicates in 3-D matrix
    303                 r = repmat(max(min(fxy/nmedian(abs(fxy),1 - scut/100),1),-1),[1,1,3]);
     610                % computes maximum absolute gradient (median-style), normalizes,
     611                % saturates and duplicates in 3-D matrix
     612                li = 1 - abs(sind(el)); % light amplitude (experimental)
     613                r = repmat(max(min(li*fxy/nmedian(abs(fxy),1 - lcut/100),1),-1),[1,1,3]);
     614                rp = (1 - abs(r)).^ct;
    304615       
    305616                % applies contrast using exponent
    306                 rp = (1 - abs(r)).^c;
    307617                I = I.*rp;
    308618       
    309619                % lighter for positive gradient
    310                 k = find(r > 0);
    311                 I(k) = I(k) + (1 - rp(k));
     620                I(r>0) = I(r>0) + (1 - rp(r>0));
    312621                               
    313622        end
    314        
    315         % set novalues / NaN to black color
     623
     624        % set novalues / NaN to nancolor
    316625        [i,j] = find(isnan(z));
    317626        if ~isempty(i)
    318627                I(sub2ind(size(I),repmat(i,1,3),repmat(j,1,3),repmat(1:3,size(i,1),1))) = repmat(novalue_color,size(i,1),1);
    319628        end
     629       
     630        % lake option
     631        if lake
     632                klake = islake(z);
     633        else
     634                klake = 0;
     635        end
     636       
    320637        % set the seacolor for 0 values
    321638        if ~isempty(csea)
    322                 [i,j] = find(z==0);
     639                [i,j] = find(z==0 | klake);
    323640                if ~isempty(i)
    324641                        I(sub2ind(size(I),repmat(i,1,3),repmat(j,1,3),repmat(1:3,size(i,1),1))) = repmat(csea(end,:),size(i,1),1);
     
    326643        end
    327644
    328         hh = imagesc(x,y,I);
    329 else
    330         hh = imagesc(x,y,z);
    331         colormap(cmap);
    332         text(mean(x),mean(y),'SPLASH!','Color','c','FontWeight','bold','HorizontalAlignment','center')
    333 end
    334 
    335 orient tall
    336 axis xy, axis equal, axis tight
    337 
     645        if wmark
     646                I = watermark(I,wmark);
     647        end
     648        txt = '';
     649       
     650else
     651       
     652        I = repmat(shiftdim(sea_color,-1),size(z));
     653        cmap = repmat(sea_color,[256,1]);
     654        txt = 'Mak Byur!';      % Splash !
     655end
     656
     657% -------------------------------------------------------------------------
     658% --- ends the function when 'noplot' option is on
     659if noplot
     660        varargout{1} = struct('x',x,'y',y,'z',z,'rgb',I);
     661        return
     662end
     663
     664% -------------------------------------------------------------------------
     665% --- plots the RGB image
     666hh = imagesc(x,y,I);
     667
     668if ~isempty(txt)
     669        text(mean(x),mean(y),txt,'Color',sea_color/4, ...
     670                'FontWeight','bold','HorizontalAlignment','center')
     671end
     672
     673orient tall; axis xy
     674if strcmpi(axeq,'auto')
     675        axis equal
     676end
     677axis tight
    338678xlim = [min(x),max(x)];
    339679ylim = [min(y),max(y)];
    340 zlim = [min(z(:)),max(z(:))];
    341 
    342 % axis basemap style
    343 if dec | dms
     680zlim = [min([z(z(:) ~= novalue);zmin]),max([z(z(:) ~= novalue);zmax])];
     681
     682if dms
     683        % approximates X-Y aspect ratio for this latitude (< 20-m precision for 1x1° grid)
     684        xyr = cos(mean(y)*pi/180);
     685else
     686        xyr = 1;
     687end
     688
     689bw0 = max(diff(xlim)*xyr,diff(ylim))/100;
     690bwy = bw*bw0; % Y border width = 1%
     691bwx = bwy/xyr; % border width (in degree of longitude)
     692
     693
     694% -------------------------------------------------------------------------
     695% --- Axis basemap style
     696if dec || dms
    344697        axis off
    345698
    346         if dms
    347                 % approximates X-Y aspect ratio for this latitude (< 20-m precision for 1x1° grid)
    348                 xyr = cos(mean(y)*pi/180);
    349         else
    350                 xyr = 1;
    351         end
    352         set(gca,'DataAspectRatio',[1,xyr,1])
    353         bwy = 0.008*diff(ylim); % Y border width = 1%
    354         bwx = bwy/xyr; % border width (in degree of longitude)
    355 
    356         % transparent borders
    357         patch([xlim(1)-bwx,xlim(2)+bwx,xlim(2)+bwx,xlim(1)-bwx],ylim(1) - bwy*[0,0,1,1],'k','FaceColor','none','clipping','off')
    358         patch([xlim(1)-bwx,xlim(2)+bwx,xlim(2)+bwx,xlim(1)-bwx],ylim(2) + bwy*[0,0,1,1],'k','FaceColor','none','clipping','off')
    359         patch(xlim(1) - bwx*[0,0,1,1],[ylim(1)-bwy,ylim(2)+bwy,ylim(2)+bwy,ylim(1)-bwy],'k','FaceColor','none','clipping','off')
    360         patch(xlim(2) + bwx*[0,0,1,1],[ylim(1)-bwy,ylim(2)+bwy,ylim(2)+bwy,ylim(1)-bwy],'k','FaceColor','none','clipping','off')
    361 
     699        if strcmpi(axeq,'manual')
     700                ppos = get(gcf,'PaperPosition');
     701                apos = get(gca,'Position');
     702                xyf = (xyr*diff(xlim)/apos(3)/ppos(3))/(diff(ylim)/apos(4)/ppos(4));
     703                if xyf >= 1
     704                        set(gca,'Position',[apos(1),apos(2),apos(3),apos(4)/xyf]);
     705                else
     706                        set(gca,'Position',[apos(1),apos(2),apos(3)*xyf,apos(4)]);
     707                end
     708        end
     709        if strcmpi(axeq,'auto')
     710                if diff(xlim)*xyr <= diff(ylim)
     711                        set(gca,'DataAspectRatio',[1,xyr,1])
     712                else
     713                        set(gca,'DataAspectRatio',[1/xyr,1,1])
     714                end
     715        end
     716
     717        if bw > 0
     718                % transparent borders
     719                patch([xlim(1)-bwx,xlim(2)+bwx,xlim(2)+bwx,xlim(1)-bwx],ylim(1) - bwy*[0,0,1,1],'k','FaceColor','none','clipping','off')
     720                patch([xlim(1)-bwx,xlim(2)+bwx,xlim(2)+bwx,xlim(1)-bwx],ylim(2) + bwy*[0,0,1,1],'k','FaceColor','none','clipping','off')
     721                patch(xlim(1) - bwx*[0,0,1,1],[ylim(1)-bwy,ylim(2)+bwy,ylim(2)+bwy,ylim(1)-bwy],'k','FaceColor','none','clipping','off')
     722                patch(xlim(2) + bwx*[0,0,1,1],[ylim(1)-bwy,ylim(2)+bwy,ylim(2)+bwy,ylim(1)-bwy],'k','FaceColor','none','clipping','off')
     723        end
    362724        dlon = {'E','W'};
    363725        dlat = {'N','S'};
    364 
    365         if dec
    366                 ddx = dtick(diff(xlim));
    367                 ddy = dtick(diff(ylim));
     726        if fbold
     727                fw = 'bold';
    368728        else
    369                 ddx = dtick(diff(xlim),1);
    370                 ddy = dtick(diff(ylim),1);
    371         end
    372 
     729                fw = 'normal';
     730        end
     731       
     732        if ddx == 0
     733                ddx = dtick(diff(xlim),dms);
     734        end
     735        if ddy == 0
     736                ddy = dtick(diff(ylim),dms);
     737        end
    373738        xtick = (ddx*ceil(xlim(1)/ddx)):ddx:xlim(2);
    374739        for xt = xtick(1:2:end)
    375740                dt = ddx - max(0,xt + ddx - xlim(2));
    376741                patch(repmat(xt + dt*[0,1,1,0]',[1,2]),[ylim(1) - bwy*[0,0,1,1];ylim(2) + bwy*[0,0,1,1]]','k','clipping','off')
    377                 text(xt,ylim(1) - bwy,deg2dms(xt,dlon,dec),'FontSize',fs,'HorizontalAlignment','center','VerticalAlignment','top');
     742                if fs > 0
     743                        if ~isempty(regexp(tpos,'north','once'))
     744                                text(xt,ylim(2) + 1.2*bwy,deg2dms(xt,dlon,dec),'FontSize',fs,'FontWeight',fw, ...
     745                                        'HorizontalAlignment','center','VerticalAlignment','bottom');
     746                        else
     747                                text(xt,ylim(1) - 1.2*bwy,deg2dms(xt,dlon,dec),'FontSize',fs,'FontWeight',fw, ...
     748                                        'HorizontalAlignment','center','VerticalAlignment','top');
     749                        end
     750                end
    378751        end
    379752
     
    382755                dt = ddy - max(0,yt + ddy - ylim(2));
    383756                patch([xlim(1) - bwx*[0,0,1,1];xlim(2) + bwx*[0,0,1,1]]',repmat(yt + dt*[0,1,1,0]',[1,2]),'k','clipping','off')
    384                 text(xlim(1) - 1.1*bwx,yt,deg2dms(yt,dlat,dec),'FontSize',fs,'HorizontalAlignment','right','VerticalAlignment','middle');
    385         end
    386 end
    387 
    388 % scale legend
     757                if fs > 0
     758                        if ~isempty(regexp(tpos,'east','once'))
     759                                text(xlim(2) + 1.2*bwx,yt,deg2dms(yt,dlat,dec),'FontSize',fs,'FontWeight',fw, ...
     760                                        'HorizontalAlignment','center','VerticalAlignment','top','rotation',90);
     761                        else
     762                                text(xlim(1) - 1.2*bwx,yt,deg2dms(yt,dlat,dec),'FontSize',fs,'FontWeight',fw, ...
     763                                        'HorizontalAlignment','center','VerticalAlignment','bottom','rotation',90);
     764                        end
     765                end
     766        end
     767end
     768
     769% -------------------------------------------------------------------------
     770% --- Scales legend
    389771if scale
    390772        %wsc = diff(xlim)*0.01;
    391         wsc = bwx;
    392         xsc = xlim(2) + wsc*4;
    393 
    394         % elevation scale (colorbar)
    395         zscale = linspace(zlim(1),zlim(2),length(cmap));
     773        wsc = bw0;
     774        xsc = xlim(2) + wsc*2 + bwx;
     775
     776        if wmark
     777                cmap = watermark(cmap,wmark);
     778        end
     779
     780        % -- elevation scale (colorbar)
     781        zscale = linspace(zmin,zmax,length(cmap))';
    396782        yscale = linspace(0,diff(ylim)/2,length(cmap));
     783        ddz = dtick(dz*max(0.5*xyr*diff(xlim)/yscale(end),1));
     784        ztick = (ddz*ceil(zscale(1)/ddz)):ddz:zscale(end);
     785        rgbscale = ind2rgb(uint16(round((zscale - zmin)*(size(cmap,1) - 1)/dz) + 1),cmap);
    397786        ysc = ylim(1);
    398         ddz = dtick(dz*max(0.5*xyr*diff(xlim)/yscale(end),1));
    399         ztick = (ddz*ceil(zlim(1)/ddz)):ddz:zlim(2);
    400         patch(xsc + repmat(wsc*[-1;1;1;-1],[1,length(cmap)]), ...
    401                 ysc + [repmat(yscale,[2,1]);repmat(yscale + diff(yscale(1:2)),[2,1])], ...
    402                 repmat(zscale,[4,1]), ...
    403                 'EdgeColor','flat','LineWidth',.1,'FaceColor','flat','clipping','off')
    404         colormap(cmap)
    405         caxis([zmin,zmax])
     787        hold on
     788        imagesc(xsc + wsc*[-1,1]/2,ysc + yscale,repmat(rgbscale,1,2),'clipping','off');
    406789        patch(xsc + wsc*[-1,1,1,-1],ysc + yscale(end)*[0,0,1,1],'k','FaceColor','none','Clipping','off')
    407         text(xsc + 2*wsc + zeros(size(ztick)),ysc + (ztick - zlim(1))*0.5*diff(ylim)/diff(zlim),num2str(ztick'), ...
    408                 'HorizontalAlignment','left','VerticalAlignment','middle','FontSize',8)
     790        text(xsc + 2*wsc + zeros(size(ztick)),ysc + (ztick - zscale(1))*0.5*diff(ylim)/diff(zscale([1,end])),num2str(ztick'), ...
     791                'HorizontalAlignment','left','VerticalAlignment','middle','FontSize',6)
     792        % indicates min and max Z values
     793        text(xsc,ysc - bwy/2,sprintf('%g m',roundsd(zlim(1),3)),'FontWeight','bold', ...
     794                'HorizontalAlignment','left','VerticalAlignment','top','FontSize',6)
     795        text(xsc,ysc + .5*diff(ylim) + bwy/2,sprintf('%g m',roundsd(zlim(2),3)),'FontWeight','bold', ...
     796                'HorizontalAlignment','left','VerticalAlignment','bottom','FontSize',6)
    409797       
    410         % distance scale (in case of DMS only)
     798        % frees axes only if not hold on
     799        if ~holdon
     800                hold off
     801        end
     802       
     803        % -- distance scale (in km)
    411804        if dms
    412                 degkm = 6370*pi/180;
    413                 dkm = dtick(diff(ylim)*degkm);
    414                 ysc = ylim(2) - 0.5*dkm/degkm;
    415                 patch(xsc + wsc*[-1,-1,0,0],ysc + dkm*0.5*[-1,1,1,-1]/degkm,'k','FaceColor',grey,'clipping','off')
    416                 if dkm > 1
    417                         skm = sprintf('%g km',dkm);
    418                 else
    419                         skm = sprintf('%g m',dkm*1000);
    420                 end
    421                 text(xsc,ysc,skm,'rotation',-90,'HorizontalAlignment','center','VerticalAlignment','bottom', ...
    422                         'Color',grey,'FontWeight','bold')
    423         end
    424 
     805                fsc = degkm;
     806        else
     807                fsc = zratio/1e3;
     808        end
     809        dkm = dtick(diff(ylim)*fsc);
     810        ysc = ylim(2) - 0.5*dkm/fsc;
     811        patch(xsc + wsc*[-1,-1,0,0],ysc + dkm*0.5*[-1,1,1,-1]/fsc,'k','FaceColor',grey,'clipping','off')
     812        if dkm > 1
     813                skm = sprintf('%g km',dkm);
     814        else
     815                skm = sprintf('%g m',dkm*1000);
     816        end
     817        text(xsc,ysc,skm,'rotation',-90,'HorizontalAlignment','center','VerticalAlignment','bottom', ...
     818                        'Color',grey,'FontWeight','bold','FontSize',6)
    425819end
    426820
    427821
    428822if nargout > 0
    429         h = hh;
     823        varargout{1} = hh;
     824end
     825if nargout > 1
     826        varargout{2} = I;
     827end
     828if nargout > 2
     829        varargout{3} = z;
    430830end
    431831
     
    455855end
    456856
    457 if deg & dlim <= 2/60
     857if deg && dlim <= 2/60
    458858        % less than 2 minutes: base 36
    459859        m = 10^floor(log10(dlim*36))/36;
    460 elseif deg & dlim <= 2
     860elseif deg && dlim <= 2
    461861        % less than 2 degrees: base 6
    462862        m = 10^floor(log10(dlim*6))/6;
     
    485885else
    486886        xa = abs(x) + 1/360000;
    487         sd = sprintf('%d%c',floor(xa),176);     % ASCII char 176 is the degree sign
     887        %sd = sprintf('%d%c',floor(xa),176);    % ASCII char 176 is the degree sign
     888        sd = sprintf('%d°',floor(xa));
    488889        sm = '';
    489890        ss = '';
     
    502903end
    503904
     905
    504906%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    505907function z = fillgap(x,y,z)
    506908% GRIDDATA is not efficient for large arrays, but has great advantage to be
    507 % included in Matlab core functions! To optimize interpolation, we
    508 % reduce the number of relevant data by building a mask of surrounding
     909% included in Matlab's core functions! To optimize interpolation, we
     910% reduce the amount of relevant data by building a mask of all surrounding
    509911% pixels of novalue areas... playing with linear index!
    510912
     
    529931[i,j] = ind2sub(sz,k);
    530932k2 = sub2ind(fliplr(sz),j,i); % switched i and j: k2 is linear index in row order
     933
     934
     935%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     936function k = islake(z)
     937% ISLAKE mask of zero gradient on 3x3 tiles
     938% We use diff matrix in row and column directions, and shift it to build
     939% a single vectorized test of surrounding pixels. To do this we must
     940% concatenate unit vectors in different combinations...
     941
     942dx = diff(z,1,2);       % differences in X direction
     943dy = diff(z,1,1);       % differences in Y direction
     944u1 = ones(size(z,1),1); % row unit vector
     945u2 = ones(1,size(z,2)); % column unit vector
     946u2r = u2(2:end);
     947
     948% index of the tiles center pixel
     949k = ( ...
     950        [u2;dy] == 0 & [dy;u2] == 0 & ...
     951        [u1,dx] == 0 & [dx,u1] == 0 & ...
     952        [u1,[dx(2:end,:);u2r]] == 0 & [[dx(2:end,:);u2r],u1] == 0 & ...
     953        [u1,[u2r;dx(1:end-1,:)]] == 0 & [[u2r;dx(1:end-1,:)],u1] == 0 ...
     954);
     955
     956% now extends it to surrounding pixels
     957k(1:end-1,:) = (k(1:end-1,:) | k(2:end,:));
     958k(2:end,:) = (k(2:end,:) | k(1:end-1,:));
     959k(:,1:end-1) = (k(:,1:end-1) | k(:,2:end));
     960k(:,2:end) = (k(:,2:end) | k(:,1:end-1));
     961
     962
     963%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     964function s = isrgb(x,n)
     965
     966if nargin < 2
     967        n = 0;
     968end
     969if isnumeric(x) && (n == 1 && all(size(x) == [1,3]) || n == 0 && size(x,2) == 3) ...
     970                && all(x(:) >= 0 & x(:) <= 1)
     971        s = 1;
     972else
     973        s = 0;
     974end
     975
     976
     977%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     978function s = isperc(x)
     979
     980if isnumeric(x) && isscalar(x) && x >= 0 && x <= 100
     981        s = 1;
     982else
     983        s = 0;
     984end
     985
     986
     987%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     988function s = isvec(x,n)
     989
     990if nargin < 2
     991        n = 2;
     992end
     993if isnumeric(x) && numel(x) == n
     994        s = 1;
     995else
     996        s = 0;
     997end
     998
     999
     1000%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     1001function y=roundsd(x,n)
     1002
     1003og = 10.^(floor(log10(abs(x)) - n + 1));
     1004y = round(x./og).*og;
     1005y(x==0) = 0;
     1006
     1007
     1008%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     1009function y = watermark(x,n)
     1010
     1011if nargin < 2
     1012        n = 2;
     1013end
     1014
     1015if n == 0
     1016    y = x;
     1017else
     1018    y = (x/n + 1 - 1/n);
     1019end
     1020
     1021
     1022%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     1023function [s,v] = checkparam(arg,nam,func,val)
     1024
     1025switch func2str(func)
     1026        case 'isscalar'
     1027                num = 1;
     1028                mes = 'scalar value';
     1029        case 'isperc'
     1030                num = 1;
     1031                mes = 'percentage scalar value';
     1032        case 'isvec'
     1033                num = 1;
     1034                if nargin < 4
     1035                        val = 2;
     1036                end
     1037                mes = sprintf('%d-element vector',val);
     1038        case 'isrgb'
     1039                num = 1;
     1040                mes = '[R,G,B] vector with 0.0 to 1.0 values';
     1041        case 'ischar'
     1042                num = 0;
     1043                mes = 'string';
     1044                if nargin > 3
     1045                        mes = sprintf('%s (%s)',mes,strjoin(val,' or '));
     1046                end
     1047        otherwise
     1048                num = 1;
     1049                mes = 'value';
     1050end
     1051
     1052s = 0;
     1053v = [];
     1054k = find(strcmpi(arg,nam));
     1055if ~isempty(k)
     1056        if (k + 1) <= length(arg) ...
     1057                        && (~num || isnumeric(arg{k+1})) ...
     1058                        && (nargin < 4 && func(arg{k+1}) ...
     1059                                || (nargin > 3 && (strcmp(func2str(func),'ischar') && ismember(arg{k+1},val)) ...
     1060                                         || strcmp(func2str(func),'isvec') && func(arg{k+1},val)))
     1061                v = arg{k+1};
     1062                s = 1;
     1063        else
     1064                error('%s option must be followed by a valid %s.',upper(nam),mes)
     1065        end
     1066end
     1067
     1068
     1069%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     1070function s=strjoin(c,d)
     1071%STRJOIN Join cell array of strings
     1072%(this is for Matlab versions < 2013a backward compatibility)
     1073
     1074if nargin < 2
     1075        d = '';
     1076end
     1077n = numel(c);
     1078ss = cell(2,n);
     1079ss(1,:) = reshape(c,1,n);
     1080ss(2,1:n-1) = {d};
     1081s = [ss{:}];
  • issm/trunk-jpl/externalpackages/dem/license.txt

    r14224 r23663  
    1 Copyright (c) 2013, François Beauducel
     1Copyright (c) 2016, François Beauducel
    22All rights reserved.
    33
    4 Redistribution and use in source and binary forms, with or without 
    5 modification, are permitted provided that the following conditions are 
     4Redistribution and use in source and binary forms, with or without
     5modification, are permitted provided that the following conditions are
    66met:
    77
    8     * Redistributions of source code must retain the above copyright 
     8    * Redistributions of source code must retain the above copyright
    99      notice, this list of conditions and the following disclaimer.
    10     * Redistributions in binary form must reproduce the above copyright 
    11       notice, this list of conditions and the following disclaimer in 
     10    * Redistributions in binary form must reproduce the above copyright
     11      notice, this list of conditions and the following disclaimer in
    1212      the documentation and/or other materials provided with the distribution
    13      
    14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
    17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
    18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
    19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
    20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
    21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
    23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
     13
     14THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     15AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     18LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     19CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     20SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     21INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     22CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     23ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    2424POSSIBILITY OF SUCH DAMAGE.
Note: See TracChangeset for help on using the changeset viewer.