function ticks = getDateticks(dateNumbers,datespec,datestep)
%GETDATETICKS  Get the ticks on an axis for a date range with given step
%
%   The function computes the date ticks for given date step. The step type
%   is given in datespec. The ticks are set to whole 
%
%   Syntax:
%   ticks = getDateticks(dateNumbers,datespec,datestep)
%
%   Input:
%   dateNumbers  = the dates given as date numbers (from datenum)
%   datespec     = the specification of the step:
%                  index 1: 'year', 'month', 'day'
%                  index 2: 'interval' each tick is a multiple of the step,
%                                      the first tick is before the first
%                                      date
%                           'start'    set the start of the tick just
%                                      before the first date
%                  the last tick is always beyond the last date
%   date step    = the step in time according to the specification. The
%                  step is always rounded to the nearest whole figure.
%
%   Output:
%   ticks        = the date ticks given as date numbers
%
%   Example
%
%   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: 08 Sep 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%% set the correct date step

step = fix(datestep);

dateStr1 = datevec(dateNumbers(1));
dateStr2 = datevec(dateNumbers(length(dateNumbers)));

% initialize the start
switch lower(datespec{1})
    case 'year'
        theYear = dateStr1(1);
        if strcmpi(datespec{2},'interval')
            aYear = theYear/step;
            aYear = int16(fix(aYear))*step;
            aYear = double(aYear);
        else
            aYear = theYear;
        end
        aTick = [aYear, 1,1,0,0,0];
        ticks{1} = datenum(aTick);
        tel = 1;
        while true
            aYear = aYear + step;
            aTick = [aYear, 1,1,0,0,0];
            aTick = datenum(aTick);
            if aTick < dateNumbers(length(dateNumbers))
                tel = tel + 1;
                ticks{tel} = aTick;
            else
                tel = tel + 1;
                ticks{tel} = aTick;
                break
            end
        end
    case 'month'
        
    case 'day'
        
    otherwise
        ticks = [];
end
