%% contruct kml file with all Jarkus transect ID's
% may still need some editing...
% Claire van Oeveren, Deltares 2010


load('W:\bulletin\oeveren\Jarkus\toetsraaien.mat')

url             = jarkus_url;
area            = cellstr(nc_varget(url,'areaname'));
id              = nc_varget(url,'id');
lat             = nc_varget(url,'lat');
lon             = nc_varget(url,'lon');
id              = nc_varget(url,'id');
xRSP            = nc_varget(url,'cross_shore');

placemark       = find(xRSP==1200); % location for the placemark, along the RSP axis
linestart       = find(xRSP==-500); % start of the line, on the RSP axis
lineend         = find(xRSP==1500); % end of the line, on the RSP axis


%% gebruik alleen de toetsraaien
mask    = ismember(id,toetsraaien);
index   = find(mask);
lat     = lat(mask,:);
lon     = lon(mask,:);
id      = id(mask);
area    = area(index);
clear mask index


%% make GE-lines of transects
lat2 = lat(:,[linestart lineend]);
lon2 = lon(:,[linestart lineend]);
KMLline(lat2',lon2','Filename',[rootPath 'Jarkus transect lines.kml'],...
    'kmlName','transect lines');


%% make GE-points with names
raaiID      = id - 1e5*round(id/1e5);
vakID       = round(id/1e6);
for ii = 1:length(id)
    raaiName        = num2str(raaiID(ii),'%05d');
    raaiName2        = num2str(raaiID(ii),'%d');
    raaiNames{ii,1} = raaiName;
    vakNum          = num2str(vakID(ii),'%02d');
    areaNames{ii,1} = [vakNum ' ' area{ii}];
    
    kmlstr = ge_point(lon(ii,placemark),lat(ii,placemark),0,...
        'iconURL','http://maps.google.com/mapfiles/kml/shapes/open-diamond.png',...
        'iconScale',0,...
        'name',raaiName2,'extrude',1);
    
    if ~exist([rootPath vakNum ' ' area{ii}],'dir'), mkdir([rootPath vakNum ' ' area{ii}]), end
    fid = fopen([rootPath vakNum ' ' area{ii} '\' raaiName '.kml'],'wt');
    fprintf(fid,'%s',kmlstr);
    fclose(fid);
    

    clear ans kmlstr raaiName fid vakNum raaiName2
end, clear ii


%% combine areafiles into single kml-file

% first, find filenames of all kml-files in the folder
[areaNames,index] = unique(areaNames); area = area(index);

for ipat = 1:length(areaNames)
    pat = areaNames{ipat};
    [kmlname,kmlname] = strtok(pat);
    
    sourcefiles     = dir([rootPath pat]);
    dirs            = [sourcefiles.isdir];
    sourcefiles     = {sourcefiles.name};
    sourcefiles     = sourcefiles(~dirs);
    [exts,exts]    = strtok(sourcefiles,'.');
    exts            = strtok(exts,'.');
    iskml           = strcmp('kml',exts);
    sourcefiles     = sourcefiles(iskml);
    sourcefiles     = sourcefiles';
    
    homePath        = pwd;
    cd([rootPath pat])
    KMLmerge_files('fileName',['..\' pat '.kml'],'sourceFiles',sourcefiles,'kmlName',kmlname);
    cd(homePath)
    clear sourcefiles pat dirs exts iskml kmlname
end
foldernames         = areaNames; 
foldernames{end+1}  = 'transect lines';


%% combine all areas into single file
sourcefiles     = dir(rootPath);
dirs            = [sourcefiles.isdir];
sourcefiles     = {sourcefiles.name};
sourcefiles     = sourcefiles(~dirs);
[exts,exts]    = strtok(sourcefiles,'.');
exts            = strtok(exts,'.');
iskml           = strcmp('kml',exts);
sourcefiles     = sourcefiles(iskml);
sourcefiles     = sourcefiles';

homePath        = pwd;
cd(rootPath)
KMLmerge_files('fileName','..\Jarkus_toetsraaien.kml','sourceFiles',sourcefiles,...
    'foldernames',foldernames,'kmlName','Jarkus transect IDs');
cd(homePath)
clear sourcefiles pat dirs exts iskml



