Index: /issm/trunk/src/m/kml/kml_mesh_elem.m
===================================================================
--- /issm/trunk/src/m/kml/kml_mesh_elem.m	(revision 6460)
+++ /issm/trunk/src/m/kml/kml_mesh_elem.m	(revision 6461)
@@ -2,8 +2,7 @@
 %  create kml polygons for the element mesh.
 %
-%  []=kml_mesh_elem(fid,md,params)
+%  [kfold]=kml_mesh_elem(md,params)
 %
 %  where the required input is:
-%    fid           (numeric, file ID of .kml file)
 %    md            (model, model class object)
 %
@@ -18,5 +17,8 @@
 %    cmap          (char or numeric, colormap definition)
 %
-function []=kml_mesh_elem(varargin)
+%  and the required output is:
+%    kfold         (kml_folder, folder of polygon placemarks)
+%
+function [kfold]=kml_mesh_elem(varargin)
 
 if ~nargin
@@ -29,13 +31,5 @@
 iarg=1;
 if (nargin >= 1)
-    fid=varargin{1};
-end
-if ~exist('fid','var') || isempty(fid) || (fid < 0)
-    error('File ID ''%d'' must be open.',fid);
-end
-
-iarg=iarg+1;
-if (nargin >= 2)
-    md=varargin{2};
+    md=varargin{1};
 end
 if ~exist('md','var') || isempty(md) || ~isa(md,'model')
@@ -106,53 +100,54 @@
 %%  write folder for mesh
 
-fprintf(fid,'    <Folder>\n');
+kfold=kml_folder();
 if exist('cdata','var') && ~isempty(cdata)
-    fprintf(fid,'      <name>Data: %s</name>\n',cdata);
+    kfold.name      =sprintf('Data: %s',cdata);
 else
-    fprintf(fid,'      <name>Mesh</name>\n');
+    kfold.name      =sprintf('Mesh');
 end
-fprintf(fid,'      <visibility>1</visibility>\n');
-fprintf(fid,'      <description>Elements=%d, Grids=%d</description>\n',...
+kfold.visibility=1;
+kfold.descript  =sprintf('Elements=%d, Grids=%d',...
     md.numberofelements,md.numberofgrids);
+kfold.feature   ={repmat(kml_placemark(),1,size(md.elements,1))};
 
-%  write each element as a polygon
+%  write each element as a polygon placemark
 
 disp(['Writing ' num2str(size(md.elements,1)) ' tria elements as KML polygons.']);
 for i=1:size(md.elements,1)
-    fprintf(fid,'      <Placemark>\n');
-    fprintf(fid,'        <name>Element %d</name>\n',i);
-    fprintf(fid,'        <visibility>1</visibility>\n');
+    kplace=kml_placemark();
+    kplace.name      =sprintf('Element %d',i);
+    kplace.visibility=1;
     if exist('edata','var')
-        fprintf(fid,'        <description>Element data: %g</description>\n',edata(i));
+        kplace.descript  =sprintf('Element data: %g',edata(i));
         imap = fix((edata(i)-cmin)/(cmax-cmin)*size(cmap,1))+1;
         if     (imap >= 1) && (imap <= size(cmap,1))
-            fprintf(fid,'        <styleUrl>#MatlabColor%d</styleUrl>\n',imap);
+            kplace.styleurl  =sprintf('#MatlabColor%d',imap);
         elseif (edata(i) == cmax)
-            fprintf(fid,'        <styleUrl>#MatlabColor%d</styleUrl>\n',size(cmap,1));
+            kplace.styleurl  =sprintf('#MatlabColor%d',size(cmap,1));
         else
-            fprintf(fid,'        <styleUrl>#BlackLineEmptyPoly</styleUrl>\n');
+            kplace.styleurl  =sprintf('#BlackLineEmptyPoly');
         end
     else
-        fprintf(fid,'        <styleUrl>#BlackLineRandomPoly</styleUrl>\n');
+        kplace.styleurl  =sprintf('#BlackLineRandomPoly');
     end
-    fprintf(fid,'        <Polygon>\n');
-    fprintf(fid,'          <extrude>1</extrude>\n');
-    fprintf(fid,'          <altitudeMode>relativeToGround</altitudeMode>\n');
-    fprintf(fid,'          <outerBoundaryIs>\n');
-    fprintf(fid,'            <LinearRing>\n');
-    fprintf(fid,'              <coordinates>\n');
+
+    kpoly=kml_polygon();
+    kpoly.extrude   =1;
+    kpoly.altmode   ='relativeToGround';
+
+    kring=kml_linearring();
+    kring.coords    =zeros(size(md.elements,2)+1,3);
+
     for j=1:size(md.elements,2)
-        [lat(j),long(j)]=mapxy(md.x(md.elements(i,j)),md.y(md.elements(i,j)),'s');
-        fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(j),lat(j),alt);
+        [lat,long]=mapxy(md.x(md.elements(i,j)),md.y(md.elements(i,j)),'s');
+        kring.coords(j,:)=[long lat alt];
     end
-    fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(1),lat(1),alt);
+    kring.coords(end,:)=kring.coords(1,:);
 
-    fprintf(fid,'              </coordinates>\n');
-    fprintf(fid,'            </LinearRing>\n');
-    fprintf(fid,'          </outerBoundaryIs>\n');
-    fprintf(fid,'        </Polygon>\n');
-    fprintf(fid,'      </Placemark>\n');
+    kpoly.outer=kring;
+    kplace.geometry=kpoly;
+    kfold.feature{1}(i)=kplace;
+    clear kring kpoly kplace
 end
-fprintf(fid,'    </Folder>\n');
 
 end
Index: /issm/trunk/src/m/kml/kml_mesh_write.m
===================================================================
--- /issm/trunk/src/m/kml/kml_mesh_write.m	(revision 6460)
+++ /issm/trunk/src/m/kml/kml_mesh_write.m	(revision 6461)
@@ -114,19 +114,21 @@
 fprintf(fid,'<?xml version="1.0" encoding="UTF-8"?>\n');
 fprintf(fid,'<kml xmlns="http://www.opengis.net/kml/2.2">\n');
-fprintf(fid,'  <Document>\n');
-fprintf(fid,'    <name>ISSM Mesh: %s</name>\n',md.name);
-fprintf(fid,'    <open>1</open>\n');
-fprintf(fid,'    <description>');
+
+kdoc=kml_document();
+kdoc.name      =sprintf('ISSM Mesh: %s',md.name);
+kdoc.open      =1;
 ifirst=true;
 for i=1:numel(md.notes)
     if ~isempty(md.notes{i})
         if ~ifirst
-            fprintf(fid,'\n');
+            kdoc.descript  =[kdoc.descript sprintf('\n')];
         end
         ifirst=false;
-        fprintf(fid,'%s',md.notes{i});
-    end
-end
-fprintf(fid,'</description>\n');
+        kdoc.descript  =[kdoc.descript sprintf('%s',md.notes{i})];
+    end
+end
+clear ifirst
+kdoc.style     ={repmat(kml_style(),0,0)};
+kdoc.feature   ={repmat(kml_folder(),0,0)};
 
 %  write style templates for defaults and for each color of the matlab
@@ -142,42 +144,50 @@
 end
 
-fprintf(fid,'    <Style id="BlackLineRandomPoly">\n');
-fprintf(fid,'      <LineStyle>\n');
-fprintf(fid,'        <color>ff000000</color>\n');
-fprintf(fid,'        <colorMode>normal</colorMode>\n');
-fprintf(fid,'        <width>%g</width>\n',lwidth);
-fprintf(fid,'      </LineStyle>\n');
-fprintf(fid,'      <PolyStyle>\n');
-fprintf(fid,'        <color>%02xffffff</color>\n',round(popac*255));
-fprintf(fid,'        <colorMode>random</colorMode>\n');
-fprintf(fid,'      </PolyStyle>\n');
-fprintf(fid,'    </Style>\n');
-fprintf(fid,'    <Style id="BlackLineEmptyPoly">\n');
-fprintf(fid,'      <LineStyle>\n');
-fprintf(fid,'        <color>ff000000</color>\n');
-fprintf(fid,'        <colorMode>normal</colorMode>\n');
-fprintf(fid,'        <width>%g</width>\n',lwidth);
-fprintf(fid,'      </LineStyle>\n');
-fprintf(fid,'      <PolyStyle>\n');
-fprintf(fid,'        <color>00ffffff</color>\n');
-fprintf(fid,'        <colorMode>random</colorMode>\n');
-fprintf(fid,'      </PolyStyle>\n');
-fprintf(fid,'    </Style>\n');
-fprintf(fid,'    <Style id="RedLineRedPoly">\n');
-fprintf(fid,'      <LineStyle>\n');
-fprintf(fid,'        <color>ff0000ff</color>\n');
-fprintf(fid,'        <colorMode>normal</colorMode>\n');
-fprintf(fid,'        <width>%g</width>\n',lwidth);
-fprintf(fid,'      </LineStyle>\n');
-fprintf(fid,'      <PolyStyle>\n');
-fprintf(fid,'        <color>%02x0000ff</color>\n',round(popac*255));
-fprintf(fid,'        <colorMode>random</colorMode>\n');
-fprintf(fid,'      </PolyStyle>\n');
-fprintf(fid,'    </Style>\n');
-if exist('edata','var')
+klsty=kml_linestyle();
+klsty.color     ='ff000000';
+klsty.colormode ='normal';
+klsty.width     =lwidth;
+kpsty=kml_polystyle();
+kpsty.color     =sprintf('%02xffffff',round(popac*255));
+kpsty.colormode ='random';
+kstyle=kml_style();
+kstyle.id        =sprintf('BlackLineRandomPoly');
+kstyle.line      =klsty;
+kstyle.poly      =kpsty;
+kdoc.style{1}(end+1)=kstyle;
+clear kstyle kpoly kline
+
+klsty=kml_linestyle();
+klsty.color     ='ff000000';
+klsty.colormode ='normal';
+klsty.width     =lwidth;
+kpsty=kml_polystyle();
+kpsty.color     =sprintf('00ffffff');
+kpsty.colormode ='random';
+kstyle=kml_style();
+kstyle.id        =sprintf('BlackLineEmptyPoly');
+kstyle.line      =klsty;
+kstyle.poly      =kpsty;
+kdoc.style{1}(end+1)=kstyle;
+clear kstyle kpoly kline
+
+klsty=kml_linestyle();
+klsty.color     ='ff0000ff';
+klsty.colormode ='normal';
+klsty.width     =lwidth;
+kpsty=kml_polystyle();
+kpsty.color     =sprintf('%02x0000ff',round(popac*255));
+kpsty.colormode ='random';
+kstyle=kml_style();
+kstyle.id        =sprintf('RedLineRedPoly');
+kstyle.line      =klsty;
+kstyle.poly      =kpsty;
+kdoc.style{1}(end+1)=kstyle;
+clear kstyle kpoly kline
 
 %  colormap command operates on a figure, so create an invisible one
 %  (could also directly call colormaps, e.g. jet(64), but risky)
 
+if exist('edata','var')
     hfig=figure('Visible','off');
     if exist('cmap','var')
@@ -189,16 +199,18 @@
     disp(['Writing ' num2str(size(cmap,1)) ' Matlab colors as KML style templates.']);
     for i=1:size(cmap,1)
-        fprintf(fid,'    <Style id="MatlabColor%d">\n',i);
-        fprintf(fid,'      <LineStyle>\n');
-        fprintf(fid,'        <color>ff000000</color>\n');
-        fprintf(fid,'        <colorMode>normal</colorMode>\n');
-        fprintf(fid,'        <width>%g</width>\n',lwidth);
-        fprintf(fid,'      </LineStyle>\n');
-        fprintf(fid,'      <PolyStyle>\n');
-        fprintf(fid,'        <color>%02x%02x%02x%02x</color>\n',round(popac*255),...
+        klsty=kml_linestyle();
+        klsty.color     ='ff000000';
+        klsty.colormode ='normal';
+        klsty.width     =lwidth;
+        kpsty=kml_polystyle();
+        kpsty.color     =sprintf('%02x%02x%02x%02x',round(popac*255),...
             round(cmap(i,3)*255),round(cmap(i,2)*255),round(cmap(i,1)*255));
-        fprintf(fid,'        <colorMode>normal</colorMode>\n');
-        fprintf(fid,'      </PolyStyle>\n');
-        fprintf(fid,'    </Style>\n');
+        kpsty.colormode ='normal';
+        kstyle=kml_style();
+        kstyle.id        =sprintf('MatlabColor%d',i);
+        kstyle.line      =klsty;
+        kstyle.poly      =kpsty;
+        kdoc.style{1}(end+1)=kstyle;
+        clear kstyle kpoly kline
     end
 end
@@ -206,5 +218,5 @@
 %  write folder for mesh
 
-kml_mesh_elem(fid,md,varargin{3:end});
+kdoc.feature{1}(end+1)=kml_mesh_elem(md,varargin{3:end});
 
 %  write folder for partition segments
@@ -212,5 +224,5 @@
 if (~exist('prtplt','var') || strncmpi(prtplt,'on' ,2) || strncmpi(prtplt,'y',1)) && ...
     md.npart
-    kml_part_flagedges(fid,md,varargin{3:end});
+    kdoc.feature{1}(end+1)=kml_part_flagedges(md,varargin{3:end});
 end
 
@@ -219,5 +231,5 @@
 if (~exist('prtplt','var') || strncmpi(prtplt,'on' ,2) || strncmpi(prtplt,'y',1)) && ...
     md.npart
-    kml_unsh_edges(fid,md,varargin{3:end});
+    kdoc.feature{1}(end+1)=kml_unsh_edges(md,varargin{3:end});
 end
 
@@ -226,5 +238,5 @@
 if (~exist('prtplt','var') || strncmpi(prtplt,'on' ,2) || strncmpi(prtplt,'y',1)) && ...
     md.npart
-    kml_part_elems(fid,md,varargin{3:end});
+    kdoc.feature{1}(end+1)=kml_part_elems(md,varargin{3:end});
 end
 
@@ -233,5 +245,5 @@
 if (~exist('prtplt','var') || strncmpi(prtplt,'on' ,2) || strncmpi(prtplt,'y',1)) && ...
     md.npart
-    kml_part_edges(fid,md,varargin{3:end});
+    kdoc.feature{1}(end+1)=kml_part_edges(md,varargin{3:end});
 end
 
@@ -240,10 +252,14 @@
 if (~exist('prtplt','var') || strncmpi(prtplt,'on' ,2) || strncmpi(prtplt,'y',1)) && ...
     md.npart
-    kml_partitions(fid,md,varargin{3:end});
-end
+    kdoc.feature{1}(end+1)=kml_partitions(md,varargin{3:end});
+end
+
+%  write document
+
+display(sprintf('Writing kml file ''%s''.',filek2));
+kml_write(kdoc,fid,'  ');
 
 %  write trailer data
 
-fprintf(fid,'  </Document>\n');
 fprintf(fid,'</kml>\n');
 
Index: /issm/trunk/src/m/kml/kml_part_edges.m
===================================================================
--- /issm/trunk/src/m/kml/kml_part_edges.m	(revision 6460)
+++ /issm/trunk/src/m/kml/kml_part_edges.m	(revision 6461)
@@ -2,8 +2,7 @@
 %  create kml linestrings for the partition edges.
 %
-%  []=kml_part_edges(fid,md,params)
+%  [kfold]=kml_part_edges(md,params)
 %
 %  where the required input is:
-%    fid           (char, file ID of .kml file)
 %    md            (model, model class object)
 %
@@ -19,5 +18,8 @@
 %    prtplt        (char, 'off'/'no' for partition segment plot)
 %
-function []=kml_part_edges(varargin)
+%  and the required output is:
+%    kfold         (kml_folder, folder of linestring placemarks)
+%
+function [kfold]=kml_part_edges(varargin)
 
 if ~nargin
@@ -30,13 +32,5 @@
 iarg=1;
 if (nargin >= 1)
-    fid=varargin{1};
-end
-if ~exist('fid','var') || isempty(fid) || (fid < 0)
-    error('File ID ''%d'' must be open.',fid);
-end
-
-iarg=iarg+1;
-if (nargin >= 2)
-    md=varargin{2};
+    md=varargin{1};
 end
 if ~exist('md','var') || isempty(md) || ~isa(md,'model')
@@ -109,11 +103,12 @@
 if (~exist('prtplt','var') || strncmpi(prtplt,'on' ,2) || strncmpi(prtplt,'y',1)) && ...
     md.npart
-    fprintf(fid,'    <Folder>\n');
-    fprintf(fid,'      <name>Partition Edges</name>\n');
-    fprintf(fid,'      <visibility>1</visibility>\n');
-    fprintf(fid,'      <description>Partitions=%d, Nodes=%d</description>\n',...
+    kfold=kml_folder();
+    kfold.name      ='Partition Edges';
+    kfold.visibility=1;
+    kfold.descript  =sprintf('Partitions=%d, Nodes=%d',...
         md.npart,md.numberofgrids);
-
-%  write each partition loop as linestrings
+    kfold.feature   ={repmat(kml_placemark(),0,0)};
+
+%  write each partition loop as a linestring placemark
 
     disp(['Writing ' num2str(md.npart) ' partitions as KML linestrings.']);
@@ -160,34 +155,31 @@
 
         for i=1:length(iloop)-1
-            fprintf(fid,'      <Placemark>\n');
+            kplace=kml_placemark();
             if (length(iloop)-1 > 1)
-                fprintf(fid,'        <name>Partition %d, Loop %d</name>\n',k,i);
+                kplace.name      =sprintf('Partition %d, Loop %d',k,i);
             else
-                fprintf(fid,'        <name>Partition %d</name>\n',k);
-            end
-            fprintf(fid,'        <visibility>1</visibility>\n');
+                kplace.name      =sprintf('Partition %d',k);
+            end
+            kplace.visibility=1;
             if exist('pdata','var')
-                fprintf(fid,'        <description>Partition data: %g</description>\n',pdata(k));
+                kplace.descript  =sprintf('Partition data: %g',pdata(k));
                 imap = fix((pdata(k)-cmin)/(cmax-cmin)*size(cmap,1))+1;
                 if     (imap >= 1) && (imap <= size(cmap,1))
-                    fprintf(fid,'        <styleUrl>#MatlabColor%d</styleUrl>\n',imap);
+                    kplace.styleurl  =sprintf('#MatlabColor%d',imap);
                 elseif (pdata(k) == cmax)
-                    fprintf(fid,'        <styleUrl>#MatlabColor%d</styleUrl>\n',size(cmap,1));
+                    kplace.styleurl  =sprintf('#MatlabColor%d',size(cmap,1));
                 else
-                    fprintf(fid,'        <styleUrl>#BlackLineEmptyPoly</styleUrl>\n');
+                    kplace.styleurl  =sprintf('#BlackLineEmptyPoly');
                 end
             else
-                fprintf(fid,'        <styleUrl>#BlackLineRandomPoly</styleUrl>\n');
-            end
-%            fprintf(fid,'        <Polygon>\n');
-%            fprintf(fid,'          <extrude>1</extrude>\n');
-%            fprintf(fid,'          <altitudeMode>relativeToGround</altitudeMode>\n');
-%            fprintf(fid,'          <outerBoundaryIs>\n');
-%            fprintf(fid,'            <LinearRing>\n');
-        fprintf(fid,'        <LineString>\n');
-        fprintf(fid,'          <extrude>1</extrude>\n');
-        fprintf(fid,'          <tessellate>1</tessellate>\n');
-        fprintf(fid,'          <altitudeMode>relativeToGround</altitudeMode>\n');
-            fprintf(fid,'              <coordinates>\n');
+                kplace.styleurl  =sprintf('#BlackLineRandomPoly');
+            end
+
+            kline=kml_linestring();
+            kline.extrude   =1;
+            kline.tessellate=1;
+            kline.altmode   ='relativeToGround';
+            kline.coords    =zeros(0,3);
+
             elast=0;
             nlast=0;
@@ -218,9 +210,9 @@
 %  if first edge, write out first node
                     if ~elast
-                        [lat(end+1),long(end+1)]=mapxy(md.x(edgeper(j,1)),md.y(edgeper(j,1)),'s');
-                        fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                        [lat,long]=mapxy(md.x(edgeper(j,1)),md.y(edgeper(j,1)),'s');
+                        kline.coords(end+1,:)=[long lat alt];
                     end
-                    [lat(end+1),long(end+1)]=mapxy(md.x(edgeper(j,2)),md.y(edgeper(j,2)),'s');
-                    fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                    [lat,long]=mapxy(md.x(edgeper(j,2)),md.y(edgeper(j,2)),'s');
+                    kline.coords(end+1,:)=[long lat alt];
                     elast=elemper(j);
                     nlast=edgeper(j,2);
@@ -309,16 +301,16 @@
 %  write out first node of first side for half-edge to midpoint
 %                                disp(['segment j=' int2str(j) ' unshared half edge from node ' int2str(elemp(ielem,nlast)) ' (node ' int2str(nlast) ') on side ' int2str(slast) ' from element ' int2str(ielem) ' written.'])
-                                [lat(end+1),long(end+1)]=mapxy((md.x(elemp(ielem,nlast))),...
-                                                               (md.y(elemp(ielem,nlast))),'s');
-                                fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                                [lat,long]=mapxy((md.x(elemp(ielem,nlast))),...
+                                                 (md.y(elemp(ielem,nlast))),'s');
+                                kline.coords(end+1,:)=[long lat alt];
                             end
                             nlast=0;
                             
 %  write out midpoint of first side
-                            [lat(end+1),long(end+1)]=mapxy((md.x(elemp(ielem,slast))...
-                                                           +md.x(elemp(ielem,mod(slast,3)+1)))/2.,...
-                                                           (md.y(elemp(ielem,slast))...
-                                                           +md.y(elemp(ielem,mod(slast,3)+1)))/2.,'s');
-                            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                            [lat,long]=mapxy((md.x(elemp(ielem,slast))...
+                                             +md.x(elemp(ielem,mod(slast,3)+1)))/2.,...
+                                             (md.y(elemp(ielem,slast))...
+                                             +md.y(elemp(ielem,mod(slast,3)+1)))/2.,'s');
+                            kline.coords(end+1,:)=[long lat alt];
                         end
 
@@ -359,9 +351,9 @@
 %  write out half-edge from current node to midpoint of unshared side
 %                            disp(['segment j=' int2str(j) ' unshared half edge from node ' int2str(elemp(ielem,nlast)) ' (node ' int2str(nlast) ') on side ' int2str(slast) ' from element ' int2str(ielem) ' written.'])
-                            [lat(end+1),long(end+1)]=mapxy((md.x(elemp(ielem,nlast))...
-                                                           +md.x(elemp(ielem,nnext)))/2.,...
-                                                           (md.y(elemp(ielem,nlast))...
-                                                           +md.y(elemp(ielem,nnext)))/2.,'s');
-                            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                            [lat,long]=mapxy((md.x(elemp(ielem,nlast))...
+                                             +md.x(elemp(ielem,nnext)))/2.,...
+                                             (md.y(elemp(ielem,nlast))...
+                                             +md.y(elemp(ielem,nnext)))/2.,'s');
+                            kline.coords(end+1,:)=[long lat alt];
                             nlast=0;
 
@@ -393,7 +385,7 @@
 %  all different, so cut through centroid
 %                                disp(['element ielem=' int2str(ielem) ' centroid written.'])
-                                [lat(end+1),long(end+1)]=mapxy(sum(md.x(elemp(ielem,:)))/3.,...
-                                                               sum(md.y(elemp(ielem,:)))/3.,'s');
-                                fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                                [lat,long]=mapxy(sum(md.x(elemp(ielem,:)))/3.,...
+                                                 sum(md.y(elemp(ielem,:)))/3.,'s');
+                                kline.coords(end+1,:)=[long lat alt];
                             end
 %  one node is in current partition, so cut off other two nodes
@@ -409,9 +401,9 @@
 %  write out midpoint of opposite side
 %                        disp(['segment j=' int2str(j) ' internal edge from side ' int2str(slast) ' to side ' int2str(snext) ' from element ' int2str(ielem) ' written.'])
-                        [lat(end+1),long(end+1)]=mapxy((md.x(elemp(ielem,snext))...
-                                                       +md.x(elemp(ielem,mod(snext,3)+1)))/2.,...
-                                                       (md.y(elemp(ielem,snext))...
-                                                       +md.y(elemp(ielem,mod(snext,3)+1)))/2.,'s');
-                        fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                        [lat,long]=mapxy((md.x(elemp(ielem,snext))...
+                                         +md.x(elemp(ielem,mod(snext,3)+1)))/2.,...
+                                         (md.y(elemp(ielem,snext))...
+                                         +md.y(elemp(ielem,mod(snext,3)+1)))/2.,'s');
+                        kline.coords(end+1,:)=[long lat alt];
                         elast=ielem;
                         nlast=0;
@@ -442,7 +434,7 @@
                             end
 %                            disp(['segment j=' int2str(j) ' unshared half edge on side ' int2str(slast) ' to node ' int2str(elemp(elast,nnext)) ' (node ' int2str(nnext) ') from element ' int2str(elast) ' written.'])
-                            [lat(end+1),long(end+1)]=mapxy(md.x(elemp(elast,nnext)),...
-                                                           md.y(elemp(elast,nnext)),'s');
-                            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                            [lat,long]=mapxy(md.x(elemp(elast,nnext)),...
+                                             md.y(elemp(elast,nnext)),'s');
+                            kline.coords(end+1,:)=[long lat alt];
                             break
 %  if not unshared, advance perimeter list and watch for end
@@ -461,17 +453,12 @@
                 end
             end
-%            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(iloop(i)),lat(iloop(i)),alt);
-
-            fprintf(fid,'              </coordinates>\n');
-        fprintf(fid,'        </LineString>\n');
-%            fprintf(fid,'            </LinearRing>\n');
-%            fprintf(fid,'          </outerBoundaryIs>\n');
-%            fprintf(fid,'        </Polygon>\n');
-            fprintf(fid,'      </Placemark>\n');
+
+            kplace.geometry=kline;
+            kfold.feature{1}(end+1)=kplace;
+            clear kline kplace
         end
     end
-    fprintf(fid,'    </Folder>\n');
-end
-
-end
-
+end
+
+end
+
Index: /issm/trunk/src/m/kml/kml_part_elems.m
===================================================================
--- /issm/trunk/src/m/kml/kml_part_elems.m	(revision 6460)
+++ /issm/trunk/src/m/kml/kml_part_elems.m	(revision 6461)
@@ -2,8 +2,7 @@
 %  create kml polygons for the partition elements.
 %
-%  []=kml_part_elems(fid,md,params)
+%  [kfold]=kml_part_elems(md,params)
 %
 %  where the required input is:
-%    fid           (char, file ID of .kml file)
 %    md            (model, model class object)
 %
@@ -19,5 +18,8 @@
 %    prtplt        (char, 'off'/'no' for partition segment plot)
 %
-function []=kml_part_elems(varargin)
+%  and the required output is:
+%    kfold         (kml_folder, folder of polygon placemarks)
+%
+function [kfold]=kml_part_elems(varargin)
 
 if ~nargin
@@ -30,13 +32,5 @@
 iarg=1;
 if (nargin >= 1)
-    fid=varargin{1};
-end
-if ~exist('fid','var') || isempty(fid) || (fid < 0)
-    error('File ID ''%d'' must be open.',fid);
-end
-
-iarg=iarg+1;
-if (nargin >= 2)
-    md=varargin{2};
+    md=varargin{1};
 end
 if ~exist('md','var') || isempty(md) || ~isa(md,'model')
@@ -109,11 +103,12 @@
 if (~exist('prtplt','var') || strncmpi(prtplt,'on' ,2) || strncmpi(prtplt,'y',1)) && ...
     md.npart
-    fprintf(fid,'    <Folder>\n');
-    fprintf(fid,'      <name>Partition Elements</name>\n');
-    fprintf(fid,'      <visibility>1</visibility>\n');
-    fprintf(fid,'      <description>Partitions=%d, Nodes=%d</description>\n',...
+    kfold=kml_folder();
+    kfold.name      ='Partition Elements';
+    kfold.visibility=1;
+    kfold.descript  =sprintf('Partitions=%d, Nodes=%d\n',...
         md.npart,md.numberofgrids);
+    kfold.feature   ={repmat(kml_placemark(),0,0)};
 
-%  write each partition loop as a polygon
+%  write each partition loop as a polygon placemark
 
     disp(['Writing ' num2str(md.npart) ' partitions as KML polygons.']);
@@ -144,44 +139,44 @@
         end
         for i=1:length(iloop)-1
-            fprintf(fid,'      <Placemark>\n');
+            kplace=kml_placemark();
             if (length(iloop)-1 > 1)
-                fprintf(fid,'        <name>Partition %d, Loop %d</name>\n',k,i);
+                kplace.name      =sprintf('Partition %d, Loop %d',k,i);
             else
-                fprintf(fid,'        <name>Partition %d</name>\n',k);
+                kplace.name      =sprintf('Partition %d',k);
             end
-            fprintf(fid,'        <visibility>1</visibility>\n');
+            kplace.visibility=1;
             if exist('pdata','var')
-                fprintf(fid,'        <description>Partition data: %g</description>\n',pdata(k));
+                kplace.descript  =sprintf('Partition data: %g',pdata(k));
                 imap = fix((pdata(k)-cmin)/(cmax-cmin)*size(cmap,1))+1;
                 if     (imap >= 1) && (imap <= size(cmap,1))
-                    fprintf(fid,'        <styleUrl>#MatlabColor%d</styleUrl>\n',imap);
+                    kplace.styleurl  =sprintf('#MatlabColor%d',imap);
                 elseif (pdata(k) == cmax)
-                    fprintf(fid,'        <styleUrl>#MatlabColor%d</styleUrl>\n',size(cmap,1));
+                    kplace.styleurl  =sprintf('#MatlabColor%d',size(cmap,1));
                 else
-                    fprintf(fid,'        <styleUrl>#BlackLineEmptyPoly</styleUrl>\n');
+                    kplace.styleurl  =sprintf('#BlackLineEmptyPoly');
                 end
             else
-                fprintf(fid,'        <styleUrl>#BlackLineRandomPoly</styleUrl>\n');
+                kplace.styleurl  =sprintf('#BlackLineRandomPoly');
             end
-            fprintf(fid,'        <Polygon>\n');
-            fprintf(fid,'          <extrude>1</extrude>\n');
-            fprintf(fid,'          <altitudeMode>relativeToGround</altitudeMode>\n');
-            fprintf(fid,'          <outerBoundaryIs>\n');
-            fprintf(fid,'            <LinearRing>\n');
-            fprintf(fid,'              <coordinates>\n');
+
+            kpoly=kml_polygon();
+            kpoly.extrude   =1;
+            kpoly.altmode   ='relativeToGround';
+
+            kring=kml_linearring();
+            kring.coords    =zeros(iloop(i+1)-iloop(i)+1,3);
+
             for j=iloop(i):iloop(i+1)-1
-                [lat(j),long(j)]=mapxy(md.x(edgeper(j,1)),md.y(edgeper(j,1)),'s');
-                fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(j),lat(j),alt);
+                [lat,long]=mapxy(md.x(edgeper(j,1)),md.y(edgeper(j,1)),'s');
+                kring.coords(j-iloop(i)+1,:)=[long lat alt];
             end
-            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(iloop(i)),lat(iloop(i)),alt);
+            kring.coords(end,:)=kring.coords(1,:);
 
-            fprintf(fid,'              </coordinates>\n');
-            fprintf(fid,'            </LinearRing>\n');
-            fprintf(fid,'          </outerBoundaryIs>\n');
-            fprintf(fid,'        </Polygon>\n');
-            fprintf(fid,'      </Placemark>\n');
+            kpoly.outer=kring;
+            kplace.geometry=kpoly;
+            kfold.feature{1}(end+1)=kplace;
+            clear kring kpoly kplace
         end
     end
-    fprintf(fid,'    </Folder>\n');
 end
 
Index: /issm/trunk/src/m/kml/kml_part_flagedges.m
===================================================================
--- /issm/trunk/src/m/kml/kml_part_flagedges.m	(revision 6460)
+++ /issm/trunk/src/m/kml/kml_part_flagedges.m	(revision 6461)
@@ -2,8 +2,7 @@
 %  create kml linestrings for the flagged partition edges.
 %
-%  []=kml_part_flagedges(fid,md,params)
+%  [kfold]=kml_part_flagedges(md,params)
 %
 %  where the required input is:
-%    fid           (char, file ID of .kml file)
 %    md            (model, model class object)
 %
@@ -13,7 +12,10 @@
 %  and the optional input is:
 %    alt           (numeric, altitude for polygons, default 10000)
-%    prtplt        (char, 'off'/'no' for partition segment plot)
+%    prtplt        (char, 'off'/'no' for partition edge plot)
 %
-function []=kml_part_flagedges(varargin)
+%  and the required output is:
+%    kfold         (kml_folder, folder of linestring placemarks)
+%
+function [kfold]=kml_part_flagedges(varargin)
 
 if ~nargin
@@ -26,13 +28,5 @@
 iarg=1;
 if (nargin >= 1)
-    fid=varargin{1};
-end
-if ~exist('fid','var') || isempty(fid) || (fid < 0)
-    error('File ID ''%d'' must be open.',fid);
-end
-
-iarg=iarg+1;
-if (nargin >= 2)
-    md=varargin{2};
+    md=varargin{1};
 end
 if ~exist('md','var') || isempty(md) || ~isa(md,'model')
@@ -66,33 +60,35 @@
     md.npart
     [xseg,yseg]=flagedges(md.elements,md.x,md.y,md.part);
-    fprintf(fid,'    <Folder>\n');
-    fprintf(fid,'      <name>Partition Segments</name>\n');
-    fprintf(fid,'      <visibility>1</visibility>\n');
-    fprintf(fid,'      <description>Partitions=%d, Segments=%d</description>\n',...
+    kfold=kml_folder();
+    kfold.name      ='Partition Segments';
+    kfold.visibility=1;
+    kfold.descript  =sprintf('Partitions=%d, Segments=%d',...
         md.npart,size(xseg,1));
+    kfold.feature   ={repmat(kml_placemark(),1,size(xseg,1))};
 
-%  write each segment as a linestring
+%  write each segment as a linestring placemark
 
     disp(['Writing ' num2str(size(xseg,1)) ' partition segments as KML linestrings.']);
     for i=1:size(xseg,1)
-        fprintf(fid,'      <Placemark>\n');
-        fprintf(fid,'        <name>Segment %d</name>\n',i);
-        fprintf(fid,'        <visibility>1</visibility>\n');
-        fprintf(fid,'        <styleUrl>#RedLineRedPoly</styleUrl>\n');
-        fprintf(fid,'        <LineString>\n');
-        fprintf(fid,'          <extrude>1</extrude>\n');
-        fprintf(fid,'          <tessellate>1</tessellate>\n');
-        fprintf(fid,'          <altitudeMode>relativeToGround</altitudeMode>\n');
-        fprintf(fid,'          <coordinates>\n');
+        kplace=kml_placemark();
+        kplace.name      =sprintf('Segment %d',i);
+        kplace.visibility=1;
+        kplace.styleurl  ='#RedLineRedPoly';
+
+        kline=kml_linestring();
+        kline.extrude   =1;
+        kline.tessellate=1;
+        kline.altmode   ='relativeToGround';
+        kline.coords    =zeros(2,3);
+
         for j=1:2
-            [lat(j),long(j)]=mapxy(xseg(i,j),yseg(i,j),'s');
-            fprintf(fid,'            %0.16g,%0.16g,%0.16g\n',long(j),lat(j),alt);
+            [lat,long]=mapxy(xseg(i,j),yseg(i,j),'s');
+            kline.coords(j,:)=[long lat alt];
         end
 
-        fprintf(fid,'          </coordinates>\n');
-        fprintf(fid,'        </LineString>\n');
-        fprintf(fid,'      </Placemark>\n');
+        kplace.geometry=kline;
+        kfold.feature{1}(i)=kplace;
+        clear kline kplace
     end
-    fprintf(fid,'    </Folder>\n');
 end
 
Index: /issm/trunk/src/m/kml/kml_partitions.m
===================================================================
--- /issm/trunk/src/m/kml/kml_partitions.m	(revision 6460)
+++ /issm/trunk/src/m/kml/kml_partitions.m	(revision 6461)
@@ -2,5 +2,5 @@
 %  create kml polygons for the partitions.
 %
-%  []=kml_partitions(fid,md,params)
+%  [kfold]=kml_partitions(fid,md,params)
 %
 %  where the required input is:
@@ -19,5 +19,8 @@
 %    prtplt        (char, 'off'/'no' for partition segment plot)
 %
-function []=kml_partitions(varargin)
+%  and the required output is:
+%    kfold         (kml_folder, folder of polygon placemarks)
+%
+function [kfold]=kml_partitions(varargin)
 
 if ~nargin
@@ -30,13 +33,5 @@
 iarg=1;
 if (nargin >= 1)
-    fid=varargin{1};
-end
-if ~exist('fid','var') || isempty(fid) || (fid < 0)
-    error('File ID ''%d'' must be open.',fid);
-end
-
-iarg=iarg+1;
-if (nargin >= 2)
-    md=varargin{2};
+    md=varargin{1};
 end
 if ~exist('md','var') || isempty(md) || ~isa(md,'model')
@@ -109,11 +104,12 @@
 if (~exist('prtplt','var') || strncmpi(prtplt,'on' ,2) || strncmpi(prtplt,'y',1)) && ...
     md.npart
-    fprintf(fid,'    <Folder>\n');
-    fprintf(fid,'      <name>Partitions</name>\n');
-    fprintf(fid,'      <visibility>1</visibility>\n');
-    fprintf(fid,'      <description>Partitions=%d, Nodes=%d</description>\n',...
+    kfold=kml_folder();
+    kfold.name      ='Partitions';
+    kfold.visibility=1;
+    kfold.descript  =sprintf('Partitions=%d, Nodes=%d',...
         md.npart,md.numberofgrids);
-
-%  write each partition loop as linestrings
+    kfold.feature   ={repmat(kml_placemark(),0,0)};
+
+%  write each partition loop as polygon placemark
 
     disp(['Writing ' num2str(md.npart) ' partitions as KML polygons.']);
@@ -160,30 +156,32 @@
 
         for i=1:length(iloop)-1
-            fprintf(fid,'      <Placemark>\n');
+            kplace=kml_placemark();
             if (length(iloop)-1 > 1)
-                fprintf(fid,'        <name>Partition %d, Loop %d</name>\n',k,i);
+                kplace.name      =sprintf('Partition %d, Loop %d',k,i);
             else
-                fprintf(fid,'        <name>Partition %d</name>\n',k);
-            end
-            fprintf(fid,'        <visibility>1</visibility>\n');
+                kplace.name      =sprintf('Partition %d',k);
+            end
+            kplace.visibility=1;
             if exist('pdata','var')
-                fprintf(fid,'        <description>Partition data: %g</description>\n',pdata(k));
+                kplace.descript  =sprintf('Partition data: %g',pdata(k));
                 imap = fix((pdata(k)-cmin)/(cmax-cmin)*size(cmap,1))+1;
                 if     (imap >= 1) && (imap <= size(cmap,1))
-                    fprintf(fid,'        <styleUrl>#MatlabColor%d</styleUrl>\n',imap);
+                    kplace.styleurl  =sprintf('#MatlabColor%d',imap);
                 elseif (pdata(k) == cmax)
-                    fprintf(fid,'        <styleUrl>#MatlabColor%d</styleUrl>\n',size(cmap,1));
+                    kplace.styleurl  =sprintf('#MatlabColor%d',size(cmap,1));
                 else
-                    fprintf(fid,'        <styleUrl>#BlackLineEmptyPoly</styleUrl>\n');
+                    kplace.styleurl  =sprintf('#BlackLineEmptyPoly');
                 end
             else
-                fprintf(fid,'        <styleUrl>#BlackLineRandomPoly</styleUrl>\n');
-            end
-            fprintf(fid,'        <Polygon>\n');
-            fprintf(fid,'          <extrude>1</extrude>\n');
-            fprintf(fid,'          <altitudeMode>relativeToGround</altitudeMode>\n');
-            fprintf(fid,'          <outerBoundaryIs>\n');
-            fprintf(fid,'            <LinearRing>\n');
-            fprintf(fid,'              <coordinates>\n');
+                kplace.styleurl  =sprintf('#BlackLineRandomPoly');
+            end
+
+            kpoly=kml_polygon();
+            kpoly.extrude   =1;
+            kpoly.altmode   ='relativeToGround';
+
+            kring=kml_linearring();
+            kring.coords    =zeros(0,3);
+
             elast=0;
             nlast=0;
@@ -214,9 +212,9 @@
 %  if first edge, write out first node
                     if ~elast
-                        [lat(end+1),long(end+1)]=mapxy(md.x(edgeper(j,1)),md.y(edgeper(j,1)),'s');
-                        fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                        [lat,long]=mapxy(md.x(edgeper(j,1)),md.y(edgeper(j,1)),'s');
+                        kring.coords(end+1,:)=[long lat alt];
                     end
-                    [lat(end+1),long(end+1)]=mapxy(md.x(edgeper(j,2)),md.y(edgeper(j,2)),'s');
-                    fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                    [lat,long]=mapxy(md.x(edgeper(j,2)),md.y(edgeper(j,2)),'s');
+                    kring.coords(end+1,:)=[long lat alt];
                     elast=elemper(j);
                     nlast=edgeper(j,2);
@@ -305,16 +303,16 @@
 %  write out first node of first side for half-edge to midpoint
 %                                disp(['segment j=' int2str(j) ' unshared half edge from node ' int2str(elemp(ielem,nlast)) ' (node ' int2str(nlast) ') on side ' int2str(slast) ' from element ' int2str(ielem) ' written.'])
-                                [lat(end+1),long(end+1)]=mapxy((md.x(elemp(ielem,nlast))),...
-                                                               (md.y(elemp(ielem,nlast))),'s');
-                                fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                                [lat,long]=mapxy((md.x(elemp(ielem,nlast))),...
+                                                 (md.y(elemp(ielem,nlast))),'s');
+                                kring.coords(end+1,:)=[long lat alt];
                             end
                             nlast=0;
                             
 %  write out midpoint of first side
-                            [lat(end+1),long(end+1)]=mapxy((md.x(elemp(ielem,slast))...
-                                                           +md.x(elemp(ielem,mod(slast,3)+1)))/2.,...
-                                                           (md.y(elemp(ielem,slast))...
-                                                           +md.y(elemp(ielem,mod(slast,3)+1)))/2.,'s');
-                            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                            [lat,long]=mapxy((md.x(elemp(ielem,slast))...
+                                             +md.x(elemp(ielem,mod(slast,3)+1)))/2.,...
+                                             (md.y(elemp(ielem,slast))...
+                                             +md.y(elemp(ielem,mod(slast,3)+1)))/2.,'s');
+                            kring.coords(end+1,:)=[long lat alt];
                         end
 
@@ -355,9 +353,9 @@
 %  write out half-edge from current node to midpoint of unshared side
 %                            disp(['segment j=' int2str(j) ' unshared half edge from node ' int2str(elemp(ielem,nlast)) ' (node ' int2str(nlast) ') on side ' int2str(slast) ' from element ' int2str(ielem) ' written.'])
-                            [lat(end+1),long(end+1)]=mapxy((md.x(elemp(ielem,nlast))...
-                                                           +md.x(elemp(ielem,nnext)))/2.,...
-                                                           (md.y(elemp(ielem,nlast))...
-                                                           +md.y(elemp(ielem,nnext)))/2.,'s');
-                            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                            [lat,long]=mapxy((md.x(elemp(ielem,nlast))...
+                                             +md.x(elemp(ielem,nnext)))/2.,...
+                                             (md.y(elemp(ielem,nlast))...
+                                             +md.y(elemp(ielem,nnext)))/2.,'s');
+                            kring.coords(end+1,:)=[long lat alt];
                             nlast=0;
 
@@ -389,7 +387,7 @@
 %  all different, so cut through centroid
 %                                disp(['element ielem=' int2str(ielem) ' centroid written.'])
-                                [lat(end+1),long(end+1)]=mapxy(sum(md.x(elemp(ielem,:)))/3.,...
-                                                               sum(md.y(elemp(ielem,:)))/3.,'s');
-                                fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                                [lat,long]=mapxy(sum(md.x(elemp(ielem,:)))/3.,...
+                                                 sum(md.y(elemp(ielem,:)))/3.,'s');
+                                kring.coords(end+1,:)=[long lat alt];
                             end
 %  one node is in current partition, so cut off other two nodes
@@ -405,9 +403,9 @@
 %  write out midpoint of opposite side
 %                        disp(['segment j=' int2str(j) ' internal edge from side ' int2str(slast) ' to side ' int2str(snext) ' from element ' int2str(ielem) ' written.'])
-                        [lat(end+1),long(end+1)]=mapxy((md.x(elemp(ielem,snext))...
-                                                       +md.x(elemp(ielem,mod(snext,3)+1)))/2.,...
-                                                       (md.y(elemp(ielem,snext))...
-                                                       +md.y(elemp(ielem,mod(snext,3)+1)))/2.,'s');
-                        fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                        [lat,long]=mapxy((md.x(elemp(ielem,snext))...
+                                         +md.x(elemp(ielem,mod(snext,3)+1)))/2.,...
+                                         (md.y(elemp(ielem,snext))...
+                                         +md.y(elemp(ielem,mod(snext,3)+1)))/2.,'s');
+                        kring.coords(end+1,:)=[long lat alt];
                         elast=ielem;
                         nlast=0;
@@ -438,7 +436,7 @@
                             end
 %                            disp(['segment j=' int2str(j) ' unshared half edge on side ' int2str(slast) ' to node ' int2str(elemp(elast,nnext)) ' (node ' int2str(nnext) ') from element ' int2str(elast) ' written.'])
-                            [lat(end+1),long(end+1)]=mapxy(md.x(elemp(elast,nnext)),...
-                                                           md.y(elemp(elast,nnext)),'s');
-                            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(end),lat(end),alt);
+                            [lat,long]=mapxy(md.x(elemp(elast,nnext)),...
+                                             md.y(elemp(elast,nnext)),'s');
+                            kring.coords(end+1,:)=[long lat alt];
                             break
 %  if not unshared, advance perimeter list and watch for end
@@ -457,16 +455,13 @@
                 end
             end
-%            fprintf(fid,'                %0.16g,%0.16g,%0.16g\n',long(iloop(1)),lat(iloop(1)),alt);
-
-            fprintf(fid,'              </coordinates>\n');
-            fprintf(fid,'            </LinearRing>\n');
-            fprintf(fid,'          </outerBoundaryIs>\n');
-            fprintf(fid,'        </Polygon>\n');
-            fprintf(fid,'      </Placemark>\n');
+
+            kpoly.outer=kring;
+            kplace.geometry=kpoly;
+            kfold.feature{1}(end+1)=kplace;
+            clear kring kpoly kplace
         end
     end
-    fprintf(fid,'    </Folder>\n');
-end
-
-end
-
+end
+
+end
+
Index: /issm/trunk/src/m/kml/kml_unsh_edges.m
===================================================================
--- /issm/trunk/src/m/kml/kml_unsh_edges.m	(revision 6460)
+++ /issm/trunk/src/m/kml/kml_unsh_edges.m	(revision 6461)
@@ -2,8 +2,7 @@
 %  create kml linestrings for the unshared element edges.
 %
-%  []=kml_unsh_edges(fid,md,params)
+%  [kfold]=kml_unsh_edges(md,params)
 %
 %  where the required input is:
-%    fid           (char, file ID of .kml file)
 %    md            (model, model class object)
 %
@@ -13,7 +12,10 @@
 %  and the optional input is:
 %    alt           (numeric, altitude for polygons, default 10000)
-%    prtplt        (char, 'off'/'no' for partition segment plot)
+%    prtplt        (char, 'off'/'no' for partition edge plot)
 %
-function []=kml_unsh_edges(varargin)
+%  and the required output is:
+%    kfold         (kml_folder, folder of linestring placemarks)
+%
+function [kfold]=kml_unsh_edges(varargin)
 
 if ~nargin
@@ -26,13 +28,5 @@
 iarg=1;
 if (nargin >= 1)
-    fid=varargin{1};
-end
-if ~exist('fid','var') || isempty(fid) || (fid < 0)
-    error('File ID ''%d'' must be open.',fid);
-end
-
-iarg=iarg+1;
-if (nargin >= 2)
-    md=varargin{2};
+    md=varargin{1};
 end
 if ~exist('md','var') || isempty(md) || ~isa(md,'model')
@@ -72,33 +66,35 @@
         edgeuns(i,2)=md.elements(irow(i),mod(icol(i),size(md.elements,2))+1);
     end
-    fprintf(fid,'    <Folder>\n');
-    fprintf(fid,'      <name>Unshared Edges</name>\n');
-    fprintf(fid,'      <visibility>1</visibility>\n');
-    fprintf(fid,'      <description>Partitions=%d, Edges=%d</description>\n',...
+    kfold=kml_folder();
+    kfold.name      ='Unshared Edges';
+    kfold.visibility=1;
+    kfold.descript  =sprintf('Partitions=%d, Edges=%d',...
         md.npart,size(edgeuns,1));
+    kfold.feature   ={repmat(kml_placemark(),1,size(edgeuns,1))};
 
-%  write each edge as a linestring
+%  write each edge as a linestring placemark
 
     disp(['Writing ' num2str(size(edgeuns,1)) ' unshared edges as KML linestrings.']);
     for i=1:size(edgeuns,1)
-        fprintf(fid,'      <Placemark>\n');
-        fprintf(fid,'        <name>Edge %d</name>\n',i);
-        fprintf(fid,'        <visibility>1</visibility>\n');
-        fprintf(fid,'        <styleUrl>#RedLineRedPoly</styleUrl>\n');
-        fprintf(fid,'        <LineString>\n');
-        fprintf(fid,'          <extrude>1</extrude>\n');
-        fprintf(fid,'          <tessellate>1</tessellate>\n');
-        fprintf(fid,'          <altitudeMode>relativeToGround</altitudeMode>\n');
-        fprintf(fid,'          <coordinates>\n');
+        kplace=kml_placemark();
+        kplace.name      =sprintf('Edge %d',i);
+        kplace.visibility=1;
+        kplace.styleurl  ='#RedLineRedPoly';
+
+        kline=kml_linestring();
+        kline.extrude   =1;
+        kline.tessellate=1;
+        kline.altmode   ='relativeToGround';
+        kline.coords    =zeros(2,3);
+
         for j=1:2
-            [lat(j),long(j)]=mapxy(md.x(edgeuns(i,j)),md.y(edgeuns(i,j)),'s');
-            fprintf(fid,'            %0.16g,%0.16g,%0.16g\n',long(j),lat(j),alt);
+            [lat,long]=mapxy(md.x(edgeuns(i,j)),md.y(edgeuns(i,j)),'s');
+            kline.coords(j,:)=[long lat alt];
         end
 
-        fprintf(fid,'          </coordinates>\n');
-        fprintf(fid,'        </LineString>\n');
-        fprintf(fid,'      </Placemark>\n');
+        kplace.geometry=kline;
+        kfold.feature{1}(i)=kplace;
+        clear kline kplace
     end
-    fprintf(fid,'    </Folder>\n');
 end
 
