| [7197] | 1 | function plot_gridded(md,data,options,plotlines,plotcols,i) | 
|---|
|  | 2 | %PLOT_OVERLAY - superimpose radar image to a given field | 
|---|
|  | 3 | % | 
|---|
|  | 4 | %   Usage: | 
|---|
|  | 5 | %      plot_gridded(md,options,plotlines,plotcols,i) | 
|---|
|  | 6 | % | 
|---|
|  | 7 | %   See also: PLOTMODEL | 
|---|
|  | 8 |  | 
|---|
|  | 9 | %process mesh and data | 
|---|
| [8472] | 10 | [x y z elements is2d isplanet]=processmesh(md,[],options); | 
|---|
| [8001] | 11 | [data datatype]=processdata(md,data,options); | 
|---|
| [7197] | 12 |  | 
|---|
| [24762] | 13 | islevelset = exist(options,'levelset'); | 
|---|
|  | 14 | if islevelset | 
|---|
|  | 15 | levelset = getfieldvalue(options,'levelset'); | 
|---|
| [27356] | 16 | options2 = options; | 
|---|
|  | 17 | options2.removefield('caxis',false); | 
|---|
|  | 18 | options2.removefield('log',false); | 
|---|
| [24762] | 19 | [levelset datatype]=processdata(md,levelset,options); | 
|---|
|  | 20 | end | 
|---|
|  | 21 |  | 
|---|
| [7197] | 22 | %check is2d | 
|---|
|  | 23 | if ~is2d, | 
|---|
|  | 24 | error('buildgridded error message: gridded not supported for 3d meshes, project on a layer'); | 
|---|
|  | 25 | end | 
|---|
|  | 26 |  | 
|---|
|  | 27 | %Get xlim and ylim (used to extract radar image) | 
|---|
|  | 28 | xlim=getfieldvalue(options,'xlim',[min(x) max(x)]); | 
|---|
|  | 29 | ylim=getfieldvalue(options,'ylim',[min(y) max(y)]); | 
|---|
| [20670] | 30 | postx=getfieldvalue(options,'posting',diff(xlim)/1000); | 
|---|
|  | 31 | posty=getfieldvalue(options,'posting',diff(ylim)/1000); | 
|---|
| [7197] | 32 |  | 
|---|
|  | 33 | %Interpolating data on grid | 
|---|
| [21828] | 34 | x_m = xlim(1):postx:xlim(2); | 
|---|
|  | 35 | y_m = ylim(1):posty:ylim(2); | 
|---|
|  | 36 | data_grid=InterpFromMeshToGrid(elements,x,y,data,x_m,y_m,NaN); | 
|---|
| [15399] | 37 | data_grid_save = data_grid; | 
|---|
| [10413] | 38 | if size(data_grid,1)<3 | size(data_grid,2)<3, | 
|---|
| [14195] | 39 | error('data_grid size too small in plot_gridded, check posting and units'); | 
|---|
| [10413] | 40 | end | 
|---|
| [7197] | 41 |  | 
|---|
| [24762] | 42 | %Mask values if levelset>0 | 
|---|
|  | 43 | if islevelset | 
|---|
|  | 44 | ls_grid=InterpFromMeshToGrid(elements,x,y,levelset,x_m,y_m,NaN); | 
|---|
|  | 45 | data_grid(ls_grid>0) = NaN; | 
|---|
|  | 46 | end | 
|---|
|  | 47 |  | 
|---|
| [7197] | 48 | %Process data_grid: add white in NaN and correct caxis accordingly | 
|---|
| [15399] | 49 | [data_nani data_nanj]=find(isnan(data_grid) | data_grid==-9999); | 
|---|
| [7197] | 50 | if exist(options,'caxis'), | 
|---|
|  | 51 | caxis_opt=getfieldvalue(options,'caxis'); | 
|---|
|  | 52 | data_grid(find(data_grid<caxis_opt(1)))=caxis_opt(1); | 
|---|
|  | 53 | data_grid(find(data_grid>caxis_opt(2)))=caxis_opt(2); | 
|---|
|  | 54 | data_min=caxis_opt(1); | 
|---|
|  | 55 | data_max=caxis_opt(2); | 
|---|
|  | 56 | else | 
|---|
|  | 57 | data_min=min(data_grid(:)); | 
|---|
|  | 58 | data_max=max(data_grid(:)); | 
|---|
|  | 59 | end | 
|---|
|  | 60 |  | 
|---|
|  | 61 | %Select plot area | 
|---|
| [12732] | 62 | subplotmodel(plotlines,plotcols,i,options); | 
|---|
| [7197] | 63 |  | 
|---|
|  | 64 | %shading interp; | 
|---|
| [15400] | 65 | map    = getcolormap(options); | 
|---|
| [15399] | 66 | image_rgb = ind2rgb(uint16((data_grid - data_min)*(length(map)/(data_max-data_min))),map); | 
|---|
|  | 67 | if exist(options,'shaded'), | 
|---|
| [22701] | 68 |  | 
|---|
|  | 69 | if exist(options,'dem'), | 
|---|
|  | 70 | dem_grid=InterpFromMeshToGrid(elements,x,y,getfieldvalue(options,'dem'),x_m,y_m,NaN); | 
|---|
|  | 71 | else | 
|---|
|  | 72 | dem_grid=data_grid_save; | 
|---|
|  | 73 | end | 
|---|
| [15399] | 74 | a    = -45; | 
|---|
|  | 75 | scut = 0.2; | 
|---|
|  | 76 | c    = 1; | 
|---|
|  | 77 | % computes lighting from elevation gradient | 
|---|
| [22701] | 78 | [fx,fy] = gradient(dem_grid,x_m,y_m); | 
|---|
| [15399] | 79 | fxy = -fx*sind(a) - fy*cosd(a); | 
|---|
|  | 80 | clear fx fy % free some memory... | 
|---|
|  | 81 | fxy(isnan(fxy)) = 0; | 
|---|
|  | 82 |  | 
|---|
|  | 83 | % computes maximum absolute gradient (median-style), normalizes, saturates and duplicates in 3-D matrix | 
|---|
|  | 84 | r = repmat(max(min(fxy/nmedian(abs(fxy),1 - scut/100),1),-1),[1,1,3]); | 
|---|
|  | 85 |  | 
|---|
|  | 86 | % applies contrast using exponent | 
|---|
|  | 87 | rp = (1 - abs(r)).^c; | 
|---|
|  | 88 | image_rgb = image_rgb.*rp; | 
|---|
|  | 89 |  | 
|---|
|  | 90 | % lighter for positive gradient | 
|---|
|  | 91 | k = find(r > 0); | 
|---|
|  | 92 | image_rgb(k) = image_rgb(k) + (1 - rp(k)); | 
|---|
| [14411] | 93 | end | 
|---|
| [15399] | 94 |  | 
|---|
|  | 95 | % set novalues / NaN to black color | 
|---|
|  | 96 | if ~isempty(data_nani) | 
|---|
| [15400] | 97 | nancolor=getfieldvalue(options,'nancolor',[1 1 1]); | 
|---|
| [15399] | 98 | image_rgb(sub2ind(size(image_rgb),repmat(data_nani,1,3),repmat(data_nanj,1,3),repmat(1:3,size(data_nani,1),1))) = repmat(nancolor,size(data_nani,1),1); | 
|---|
|  | 99 | end | 
|---|
|  | 100 |  | 
|---|
|  | 101 | %plot grid | 
|---|
|  | 102 | h=imagesc(xlim,ylim,image_rgb); | 
|---|
| [14411] | 103 | axis xy | 
|---|
| [7197] | 104 |  | 
|---|
|  | 105 | %last step: mesh gridded? | 
|---|
|  | 106 | if exist(options,'edgecolor'), | 
|---|
|  | 107 | A=elements(:,1); B=elements(:,2); C=elements(:,3); | 
|---|
| [11009] | 108 | patch('Faces',[A B C],'Vertices', [x y z],'FaceVertexCData',data_grid(1)*ones(size(x)),'FaceColor','none','EdgeColor',getfieldvalue(options,'edgecolor')); | 
|---|
| [7197] | 109 | end | 
|---|
|  | 110 |  | 
|---|
|  | 111 | %Apply options | 
|---|
| [19026] | 112 | if ~isnan(data_min) & ~isinf(data_min), | 
|---|
| [15399] | 113 | options=changefieldvalue(options,'caxis',[data_min data_max]); % force caxis so that the colorbar is ready | 
|---|
|  | 114 | end | 
|---|
| [15400] | 115 | options=addfielddefault(options,'axis','xy equal'); % default axis | 
|---|
| [7197] | 116 | applyoptions(md,data,options); | 
|---|
| [21240] | 117 |  | 
|---|
|  | 118 | function y = nmedian(x,n) | 
|---|
|  | 119 | %NMEDIAN Generalized median filter | 
|---|
|  | 120 | %  NMEDIAN(X,N) sorts elemets of X and returns N-th value (N normalized). | 
|---|
|  | 121 | %  So: | 
|---|
|  | 122 | %     N = 0 is minimum value | 
|---|
|  | 123 | %     N = 0.5 is median value | 
|---|
|  | 124 | %     N = 1 is maximum value | 
|---|
|  | 125 |  | 
|---|
|  | 126 | if nargin < 2 | 
|---|
|  | 127 | n = 0.5; | 
|---|
|  | 128 | end | 
|---|
|  | 129 | y = sort(x(:)); | 
|---|
|  | 130 | y = interp1(sort(y),n*(length(y)-1) + 1); | 
|---|