function jarkus_GEChart
%JARKUS_GECHART  from Jarkus create MKL, BKL and TKL data info.
%
%   The function creates variables with coastline data of the jarkus database. As a
%   start for a range of years the MKL, BKL and TKL lines are in matrix
%   (charts) for a given jarkus transect at a location along the Dutch coast.
%   Also additional data is stored (see Save:)
%
%   Syntax:
%   jarkus_GEChart
%
%   Input:
%   varargin  =
%
%   Output:
%
%   Save:
%     a save is done of various data, the info is saved in "CoastLineInfo"
%     charts  : the coast line values for the transects and the years
%     years   : the years with possible data for the coast lines
%     secTrans: the id for the section and the transect = section*10^6 +
%               transect
%     LonLat  : the Lon/lat coordinates for the given transects in secTrans
%     sections: the section numbers and the related section names
%     
%
%   Example
%   jarkus_GEChart
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2010 <COMPANY>
%       Lou Verhage
%
%       <lou.verhage@deltares.nl>	
%
%       <ADDRESS>
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://OpenEarth.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 25 Feb 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

% $Id: jarkus_GEChart.m 4926 2011-04-18 13:00:40Z verhage $
% $Date: 2011-04-18 15:00:40 +0200 (Mon, 18 Apr 2011) $
% $Author: verhage $
% $Revision: 4926 $
% $HeadURL: https://svn.oss.deltares.nl/repos/openearthrawdata/trunk/rijkswaterstaat/kustlijnkaartenboek/scripts/jarkus_GEChart.m $
% $Keywords: $

%% initialize the function, set the directory with the raw data
theDir = 'f:\checkout\openearthrawdata\rijkswaterstaat\kustlijnkaartenboek\raw\';
%% read XLS files

% get the sheets from the MKL file
[type,sheets]=xlsfinfo ([theDir 'mklall']);
MKLdat = []; %KustLijndata BKL, TKL, MKL
for ii=1:length(sheets)
    disp(['Processing MKLall, sheet=' sheets(ii)])
    [data, txt, rawData] = xlsread([theDir 'mklall'],sheets{ii});
    % get the area with possible data
    [rowsRange,columnsRange] = getMKLDataArea(rawData);

    % read the columns with section and transect
    section    = cell2mat(rawData(rowsRange(1):rowsRange(2),1));
    try
        nn = 0;
        transectNr1 = [];
        for tNr = rowsRange(1):rowsRange(2)
            nn = nn + 1;
            if isnumeric(rawData{tNr,2})
                transectNr1(nn) = rawData{tNr,2};
            else
                transectNr1(nn) = str2num(rawData{tNr,2});
            end
        end
        transectNr = transectNr1';
%         transectNr = str2num(char(rawData(rowsRange(1):rowsRange(2),2)));
    catch
        transectNr = cell2mat(rawData(rowsRange(1):rowsRange(2),2));
    end
        
    %get the years with a MKL value
    n          = 0;
    for c = columnsRange(1):columnsRange(2)
        n = n + 1;
        if isnumeric(rawData{2,c})
            years(n) = rawData{2,c};
        else
            years(n) = str2num(rawData{2,c});
        end
    end
    
    %get the values
    m = 0;
    for c = columnsRange(1) : columnsRange(2)
        m = m + 1;
        n = 0;
        value = [];
        for r = rowsRange(1) : rowsRange(2)
            n = n + 1;
            v = rawData{r,c};
            if isnumeric(v)
                vv = v;
                if vv == 0
                    vv = nan;
                end
            else
                if strcmpi(v,' -') || strcmpi(v,'-') || strcmpi(v,'**') || ...
                        strcmpi(v,'*') || strcmpi(v,' ')
                    vv=nan;
                else
                    vv = str2num(v);
                end
            end
            if m == 23 && n == 24 && ii == 12
               pp=1;
               value(n) = vv;
            else 
                 try
                     value(n) = vv;
                 catch
                     jj=1;
                 end
            end
        end
        MKL(:,2) = section;    % 2 'section'
        MKL(:,3) = transectNr; % 3 'raai'
        MKL(:,4) = value;      % 4 'MKL'
        MKL(:,1) = years(m) ;%ones(length(MKL),1)* years(m);    % 1 'years'
        MKL(isnan(MKL(:,2)),:)=[];
        MKLdat = [MKLdat;MKL];
        MKL    = [];
    end
end    
 
% Get the excell files with BKL and TKL data
KLfiles = dir([theDir 'tkl*.xls']);
BKLdat = [];
for ii = 1:length(KLfiles);
    disp(['processing ' KLfiles(ii).name])
    [data, txt, rawData] = xlsread([theDir KLfiles(ii).name]);
    text{ii,1}           = KLfiles(ii).name(4:7);
    text(ii,2:7)         = txt(1,1:6);
    years                 = str2double(KLfiles(ii).name(4:7));
    KL(:,2)              = data(:,1); % 2 'section'
    KL(:,3)              = data(:,2); % 3 'raai'
    KL(:,4)              = data(:,3); % 4 'BKL'
    KL(:,5)              = data(:,4); % 5 'TKL'
    KL(:,1)              = years;      % 1 'years'
    KL(isnan(KL(:,2)),:) =[];
    BKLdat               = [BKLdat;KL];
    KL                   = [];
end
%% get unique data

% years
years = unique([MKLdat(:,1); BKLdat(:,1)]);

% section - transects combinations
secTrans = unique([MKLdat(:,2)*1e6+MKLdat(:,3); BKLdat(:,2)*1e6+BKLdat(:,3)]);
LonLat = getRSPLonLat(secTrans);

charts.MKL = getKustlijn(secTrans,years,MKLdat,4);
[charts.BKL,charts.TKL] = getKustlijn(secTrans,years,BKLdat,4,5);

sections = getSectionIds;
save CoastLineInfo charts years secTrans LonLat sections
clear all
return
%% Rest of function is not used "yet"
years      = BKLdat(:,1);
section    = BKLdat(:,2);
transectNr = BKLdat(:,3);
trID       = BKLdat(:,2)*1e6+ BKLdat(:,3);
BKL        = BKLdat(:,4);
TKL        = BKLdat(:,5);

transectNr(section==13&transectNr==1528)=1525;
transectNr(section==17&transectNr==1242)=1241;

disp('converting coordinates');
    [xBKL,yBKL] =             jarkus_rsp2xy(BKL,section,transectNr); % jarkus_rsp2xy
[LonBKL,LatBKL] = convertCoordinates(xBKL,yBKL,'CS1.name','Amersfoort / RD New','CS2.name','WGS 84','CS2.type','geo');
    [xTKL,yTKL] =             jarkus_rsp2xy(TKL,section,transectNr);
[LonTKL,LatTKL] = convertCoordinates(xTKL,yTKL,'CS1.name','Amersfoort / RD New','CS2.name','WGS 84','CS2.type','geo');

%% put the data in structure D
disp('pre allocating  data in structure');
%pre allocating
D.year = unique(years);
D.trID = unique(trID);
D.BKL = nan(length(D.trID),length(D.year));

   D.BKL = nan(length(D.trID),length(D.year));
  D.xBKL = nan(length(D.trID),length(D.year));
  D.yBKL = nan(length(D.trID),length(D.year));
D.LatBKL = nan(length(D.trID),length(D.year));
D.LonBKL = nan(length(D.trID),length(D.year));

   D.TKL = nan(length(D.trID),length(D.year));
  D.xTKL = nan(length(D.trID),length(D.year));
  D.yTKL = nan(length(D.trID),length(D.year));
D.LatTKL = nan(length(D.trID),length(D.year));
D.LonTKL = nan(length(D.trID),length(D.year));

   D.TND = nan(length(D.trID),length(D.year));
  D.xTND = nan(length(D.trID),length(D.year));
  D.yTND = nan(length(D.trID),length(D.year));
D.LatTND = nan(length(D.trID),length(D.year));
D.LonTND = nan(length(D.trID),length(D.year));

% loop through the data
for jj = 1:length(D.year);
    disp(['reshaping ' num2str(D.year(jj))])
    for ii = 1:length(D.trID) 
        ind = find(trID==D.trID(ii)&years==D.year(jj));
        if ~isempty(ind)
               D.BKL(ii,jj) =    BKL(ind);
              D.xBKL(ii,jj) =   xBKL(ind);
              D.yBKL(ii,jj) =   yBKL(ind);
            D.LatBKL(ii,jj) = LatBKL(ind);
            D.LonBKL(ii,jj) = LonBKL(ind);
               
               D.TKL(ii,jj) =    TKL(ind);
              D.xTKL(ii,jj) =   xTKL(ind);
              D.yTKL(ii,jj) =   yTKL(ind);
            D.LatTKL(ii,jj) = LatTKL(ind);
            D.LonTKL(ii,jj) = LonTKL(ind);
        end
    end
end
save('KustLijnData.mat','D');



