%% script to make multiple colored jarkus transects in Google Earth (with different colors per year)
% this function also uses the script: combine_JarkusTimeseries2kmz.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


clear all
clear functions
clear classes
clear java
clear import

%% OPTIONS

kustvak     = 2;    %specify number [2:13,15:17] or 'all'

ncolors     = 'all'; %specify number or 'all' to plot each year in a different color

homepath    = 'F:\OpenEarthTools\GoogleEarth\JarkusTimeseries\'; % specify your own path here

%% LAAD JARKUS INFO
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');
        transects       = [0:length(id)-1]';
    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);
        transects       = [start_transect:end_transect]';
        clear start_transect end_transect no_of_transects
end


xRSP            = nc_varget(url,'cross_shore');
year            = str2num(datestr(nc_cf_time(url,'time'),'yyyy'));
start_year      = find(year == 1965)-1; % zero-based indexing!
end_year        = find(year == year(end))-1;
no_of_years     = end_year - (start_year-1);
year            = str2num(datestr(nc_cf_time(url,'time',start_year,no_of_years),'yyyy'));

%%% maak een matrix met andere plotkleuren voor elk jaar
switch ncolors
    case 'all'
        col     = jet(no_of_years);
    otherwise
        col     = jet(ncolors);
        col     = repmat(col,ceil(no_of_years/ncolors),[]);
        col     = col(1:no_of_years,:);
end
    

%% LAAD DATA PER RAAI (anders ontstaan problemen met geheugen)
for iraai = 1:length(id)
    transectID      = transects(iraai);
    area            = areas{iraai};
    areacode        = areacodes(iraai);
    
    lat             = nc_varget(url,'lat',[transectID,0],[1,-1]);
    lon             = nc_varget(url,'lon',[transectID,0],[1,-1]);
    z               = nc_varget(url,'altitude',[start_year,transectID,0],[no_of_years,1,-1]);
    
    
    %% Interpolation of z, along xRSP
    zi      = nan(size(z));
    for yy = 1:no_of_years
        not_nan     = ~isnan(z(yy,:));
        if sum(not_nan)==0, continue; end
        zi(yy,:)    = interp1(xRSP(not_nan),z(yy,not_nan),xRSP);
    end
    
    
    %% remove the years that have no data (remove rows)
    % find non-empty rows (nanmean returns NaN for rows with ONLY nans)
    mask = ~isnan(nanmean(zi,2));
    
    % remove these rows:
    zi      = zi(mask,:);
    yeari   = year(mask,:);
    coli    = col(mask,:);
    
    timeIn  = datenum(yeari,1,1);
    timeOut = datenum(yeari+1,1,1);
    
    %% write kml files
    for iy = 1:length(yeari)
        kmlName     = [num2str(id(iraai)-areacode*1e6,'%05d') ' - ' num2str(yeari(iy),'%i')];
        kmzPath     = [homepath area '\'];
        if ~exist(kmzPath,'dir')
            mkdir(kmzPath)
        end
        KMLline(lat,lon,zi(iy,:),'fillColor',[.5 .5 .5],'fillAlpha',0.8,...
            'lineColor',coli(iy,:),'lineWidth',1.5,...
            'timeIn',timeIn(iy),'timeOut',timeOut(iy),...
            'zScaleFun',@(z) (z+20)*5,'kmlName',kmlName,...
            'fileName',[kmzPath kmlName '.kml']);
        
    end, clear iy nn kmzPath1 kmzPath2 kmlName kmzPath
    clear transectID lat lon z zi yeari coli timeIn timeOut
    
        
end
clearvars -except homepath kustvak

%%
% NOTE: if an error occurs for the following function, just run the
% function again manually

combine_JarkusColoredTimeseries2kmz(kustvak,homepath)
