source: issm/trunk-jpl/src/m/plot/kmlgridded.m@ 21828

Last change on this file since 21828 was 21828, checked in by Mathieu Morlighem, 8 years ago

CHG: simplified the arguments of InterpFromMeshToGrid

File size: 5.4 KB
Line 
1function kmlgridded(md,data,varargin)
2
3%process options
4options=pairoptions(varargin{:});
5
6%process options
7options=changefieldvalue(options,'coord','latlon');
8
9%process mesh and data
10[x y z elements is2d isplanet]=processmesh(md,[],options);
11[data datatype]=processdata(md,data,options);
12
13%check is2d
14if ~is2d,
15 error('buildgridded error message: gridded not supported for 3d meshes, project on a layer');
16end
17
18%Get xlim and ylim (used to extract radar image)
19xlim=[min(x) max(x)];
20ylim=[min(y) max(y)];
21post=getfieldvalue(options,'posting',diff(xlim)/1000);
22if(diff(xlim)/post>10000),
23 error(['posting too large']);
24end
25
26%Interpolating data on grid
27x_m = xlim(1):post:xlim(2);
28y_m = ylim(1):post:ylim(2);
29data_grid=InterpFromMeshToGrid(elements,x,y,data,x_m,y_m,NaN);
30if size(data_grid,1)<3 | size(data_grid,2)<3,
31 error('data_grid size too small, check posting and units');
32end
33pos=find(isinf(data_grid));
34if ~isempty(pos),
35 disp('Warning: removing Infs from vector (probably log(0)?)');
36 data_grid(pos)=NaN;
37end
38
39%Process data_grid: add white in NaN and correct caxis accordingly
40data_nan=find(isnan(data_grid));
41data_min=min(data_grid(:));
42data_max=max(data_grid(:));
43if exist(options,'caxis'),
44 caxis_opt=getfieldvalue(options,'caxis');
45 data_grid(find(data_grid<caxis_opt(1)))=caxis_opt(1);
46 data_grid(find(data_grid>caxis_opt(2)))=caxis_opt(2);
47 data_min=caxis_opt(1);
48 data_max=caxis_opt(2);
49end
50
51%Get colormap
52colorm = getcolormap(options);
53len = size(colorm,1);
54ind = ceil((len-1)*(data_grid-data_min)/(data_max - data_min + eps) +1);
55ind(find(ind>len))=len;
56ind(find(ind<1) )=1;
57ind(find(isnan(ind)))=1;
58image_rgb=zeros(size(data_grid,1),size(data_grid,2),3);
59r=colorm(:,1); image_rgb(:,:,1)=r(ind); clear r;
60g=colorm(:,2); image_rgb(:,:,2)=g(ind); clear g;
61b=colorm(:,3); image_rgb(:,:,3)=b(ind); clear b;
62
63%Deal with alpha
64alpha=getfieldvalue(options,'alpha',.8);
65alphaMatrix = alpha*ones(size(data_grid));
66alphaMatrix(data_nan) = 0;
67
68%write kml
69kmlfilename=getfieldvalue(options,'kmlfilename','tempfile.kml');
70kmlroot=getfieldvalue(options,'kmlroot','./');
71kmlimagename=getfieldvalue(options,'kmlimagename','tempimage');
72kmlresolution=getfieldvalue(options,'kmlresolution',1);
73kmlfolder=getfieldvalue(options,'kmlfolder','Ground Overlay');
74kmlfolderdescription=getfieldvalue(options,'kmlfolderdescription','');
75kmlgroundoverlayname=getfieldvalue(options,'kmlgroundoverlayname','ground overlay');
76kmlgroundoverlaydescription=getfieldvalue(options,'kmlgroundoverlaydescription','description');
77
78%write png
79imwrite(image_rgb,[kmlimagename '.png'],'png','alpha',alphaMatrix);
80clear image_rgb alphaMatrix
81
82%prepare colorbar
83iscolorbar=0;
84if strcmpi(getfieldvalue(options,'colorbar','on'),'on'),
85 X = linspace(0,1,len)';
86 Xlab = round(linspace(data_min,data_max,len+1));
87 html = ['<TABLE border=' num2str(1) ' bgcolor=#FFFFFF>',10];
88
89 for k=len:-1:1
90 f = (Xlab(k)-data_min)/(data_max-data_min);
91 if f<0, f=0; end
92 if f>1, f=1; end
93 polyColor(1,1) = interp1(X,colorm(:,1),f);
94 polyColor(1,2) = interp1(X,colorm(:,2),f);
95 polyColor(1,3) = interp1(X,colorm(:,3),f);
96 polyColorStr(1:2) = dec2hex(round(polyColor(1)*255),2);
97 polyColorStr(3:4) = dec2hex(round(polyColor(2)*255),2);
98 polyColorStr(5:6) = dec2hex(round(polyColor(3)*255),2);
99 html = [html,'<TR><TD width="15px" bgcolor=#',polyColorStr, '>&nbsp;</TD>','<TD bgcolor=#FFFFFF>'];
100 if k==1
101 html=[html,'&lt;= ',num2str(Xlab(k),'%g')];
102 elseif k==len
103 html=[html,'&gt;= ',num2str(Xlab(k),'%g')];
104 else
105 html=[html,num2str(Xlab(k),'%g'),' to ',num2str(Xlab(k+1),'%g'),'</TD>'];
106 end
107 html = [html,'</TR>',10];
108 end
109 html = [html,'</TABLE>'];
110 iscolorbar = 1;
111end
112
113%now write kml file
114fid=fopen([kmlroot '/' kmlfilename],'w');
115fprintf(fid,'%s\n','<?xml version="1.0" encoding="UTF-8"?>');
116fprintf(fid,'%s\n','<kml xmlns="http://earth.google.com/kml/2.1">');
117fprintf(fid,'%s\n','<Document>');
118fprintf(fid,'%s%s%s\n','<name>',kmlfilename,'</name>');
119if iscolorbar,
120 fprintf(fid,'<Placemark id="colorbar">\n');
121 fprintf(fid,'%s%s%s\n','<name>','click the icon to see the colorbar','</name>');
122 fprintf(fid,'%s%s%s\n','<description>','Ground overlay colorbar','</description>');
123 fprintf(fid,'<visibility>1</visibility>\n');
124 fprintf(fid,['<description>',10,'<![CDATA[' html ']]>',10,'</description>',10,'\n']);
125 fprintf(fid,['<Style><IconStyle><scale>1</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/donut.png</href></Icon></IconStyle><ListStyle></ListStyle></Style><Point id="poly_colorbar">\n']);
126 fprintf(fid,'<altitudeMode>clampToGround</altitudeMode>\n');
127 fprintf(fid,'<extrude>1</extrude>\n');
128 fprintf(fid,'<tessellate>1</tessellate>\n');
129 fprintf(fid,'%s%g,%g%s\n','<coordinates>',max(x),mean(y),'</coordinates>');
130 fprintf(fid,'</Point>\n');
131 fprintf(fid,'</Placemark>\n');
132end
133fprintf(fid,'%s\n','<GroundOverlay id="groundoverlay">');
134fprintf(fid,'%s%s%s\n','<name>',kmlgroundoverlayname,'</name>');
135fprintf(fid,'%s\n','<description>',kmlgroundoverlaydescription,'</description>');
136fprintf(fid,'%s%s.%s%s\n','<Icon>',kmlimagename,'png','</Icon>');
137fprintf(fid,'%s\n','<LatLonBox>');
138fprintf(fid,'%s%f%s\n','<north>',max(y_m),'</north>');
139fprintf(fid,'%s%f%s\n','<south>',min(y_m),'</south>');
140fprintf(fid,'%s%f%s\n','<east>',max(x_m),'</east>');
141fprintf(fid,'%s%f%s\n','<west>',min(x_m),'</west>');
142fprintf(fid,'%s\n','<rotation>0</rotation>');
143fprintf(fid,'%s\n','</LatLonBox>');
144fprintf(fid,'%s\n','</GroundOverlay>');
145fprintf(fid,'%s\n','</Document>');
146fprintf(fid,'%s\n','</kml>');
147fclose(fid);
Note: See TracBrowser for help on using the repository browser.