function combine_JarkusColoredTimeseries2kmz(kustvak,homepath)
% combine kml-files into one kmz file for all transects
% this function is used by the script: JarkusColoredTimeseries2kml.m
% both scripts are still in ad-hoc programming, so some adjustments may 
% still be necessary to make them more generic for further use.
% feel free to improve them!
%
% Claire van Oeveren, januari 2011
% claire.vanoeveren@deltares.nl


%% LAAD JARKUS INFO
cd(homepath)

url             = jarkus_url; %get areas from old url, new url makes matlab crash
areas           = cellstr(nc_varget(url,'areaname')); 
areacodes       = nc_varget(url,'areacode');

switch kustvak
    case 'all'
        id              = nc_varget(url,'id');
    otherwise
        start_transect  = find((areacodes==kustvak),1,'first')-1;
        end_transect    = find((areacodes==kustvak),1,'last')-1;
        no_of_transects = end_transect - (start_transect-1);
        id              = nc_varget(url,'id',[start_transect],[no_of_transects]);
        areacodes       = areacodes(start_transect+1:end_transect+1);
        areas           = areas(start_transect+1:end_transect+1);
        clear start_transect end_transect no_of_transects
end
[vaknums,index] = unique(areacodes);
areanames       = areas(index);
clear index


%% in de eerdere url 2009 data nog niet opgenomen, gebruik daarom de testversie:
year            = 1970 + floor(nc_varget(url,'time')/365);
start_year      = find(year == 1965)-1; %always subtract 1 because of zero-based indexing!
end_year        = find(year == 2009)-1;
no_of_years     = end_year - (start_year-1);
year            = 1970 + floor(nc_varget(url,'time',start_year,no_of_years)/365);



%%
for ivak = 1:length(vaknums)
    filename        = areanames{ivak};
    kmzPath         = [homepath areanames{ivak} '\'];
    
    %%% make list of all files available in directory
    list            = dir(kmzPath);
    dirs            = [list.isdir];
    list            = {list.name};
    list            = list(~dirs)'; clear dirs
    
    workdir     = pwd;
    cd(kmzPath)
    
    %% separated per transect:
    %% (which means that, when you open the kmz file, the data is first sorted PER TRANSECT, and then per year)
    %% (This is favourable if you want to select several specific transects in the GEarth UI and view all years at these transects)
    
    raaimask    = (areacodes==vaknums(ivak));
    raainums    = id(raaimask)-vaknums(ivak)*1e6;
    for ii = 1:length(raainums)
        raaien{ii,1}= num2str(raainums(ii),'%05d');
    end, clear raainums raaimask
    
    %%% step 1: combine years per transect
    for ii = 1:length(raaien)
        kmlName         = raaien{ii};
        select          = regexp(list,[kmlName ' - ']);
        select          = find(~cellfun('isempty',select));
        if isempty(select)
            continue
        end
        sourcefiles     = list(select);
        kmlfiles{ii}    = [kmlName '.kml'];
        KMLmerge_files('fileName',[kmlName '.kml'],...
            'sourceFiles',sourcefiles,...
            'kmlName',kmlName,'foldernames',sourcefiles);
    end
    
    % step 2: combine all transects and move file up
    KMLmerge_files('fileName',[areanames{ivak} '.kmz'],...
        'sourceFiles',kmlfiles,...
        'kmlName',areanames{ivak},'foldernames',kmlfiles);
    movefile([areanames{ivak} '.kmz'],['..\' areanames{ivak} '.kmz'])
    delete(kmlfiles{:})
    clear kmlfiles
    
    
    
    %% separated per year:
    %% (which means that, when you open the kmz file, the data is first sorted PER YEAR, and then per transect)
    %% (This is favourable if you want to select several specific YEARS in the GEarth UI and view all transects for these years)
    
    %%% step 1: combine transects per year
%     for ii = 1:length(year)
%         kmlName         = num2str(year(ii));
%         select          = regexp(list,[' - ' kmlName]);
%         select          = find(~cellfun('isempty',select));
%         if isempty(select)
%             continue
%         end
%         sourcefiles     = list(select);
%         kmlfiles{ii}    = [kmlName '.kml'];
%         KMLmerge_files('fileName',[kmlName '.kml'],...
%             'sourceFiles',sourcefiles,...
%             'kmlName',kmlName,'foldernames',sourcefiles);
%     end
%     
%     % step 2: combine all transects and move file up
%     KMLmerge_files('fileName',[areanames{ivak} '.kmz'],...
%         'sourceFiles',kmlfiles,...
%         'kmlName',areanames{ivak},'foldernames',kmlfiles);
%     movefile([areanames{ivak} '.kmz'],['..\' areanames{ivak} '_perJaar.kmz'])
%     delete(kmlfiles{:})
%     clear kmlfiles
    


    %% clean up workspace (not while debugging!)
%     delete(list{:})
    cd(workdir)
   

end