function dd = cmdate_dday(ymd, hms, yearbase)
% function dd = cmdate_dday(YYYYMMDD, hhmmss.dd, yearbase)
%
% Given the YYYYMMDD and hhmmss.dd from the WOCE
% moored current meter records, return the
% decimal day.  Yearbase is an optional input argument;
% if missing the first year in the first argument is
% used. All arguments are numeric, and may be vector or
% scalar.
%
% On the WOCE CM DAC CD, the netcdf variable names for
% the first two arguments are "woce_date" and
% "woce_time_of_day".  To calculate the decimal day
% times for the records in file rcm51058.nc, you can
% first create a netcdf object:
%     a = netcdf('rcm51058.nc');
% and then calculate:
%     dd = cmdate_dday(a{'woce_date'}, a{'woce_time_of_day'});
%
%
% EF 2001/09/10

[DD, others] = digits(ymd, 2);
[MM, YYYY] = digits(others, 2);

[hs, others] = digits(hms*100, 4);
ss = hs / 100;
[mm, hh] = digits(others, 2);

if nargin < 3, yearbase = YYYY(1); end

dd = datenum(YYYY, MM, DD, hh, mm, ss) - datenum(yearbase, 1, 1, 0, 0, 0);

%-------------------------------------------------------------------------

function [rhdigits, others] = digits(y, n)
others = fix(y ./ 10^n);
rhdigits = y - 10^n * others;

