-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate2doy.m
51 lines (44 loc) · 1.48 KB
/
date2doy.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function [doy,fraction] = date2doy(inputDate)
%DATE2DOY Converts the date to decimal day of the year.
% [doy,fraction] = date2doy(inputDate)
%
% Descriptions of Input Variables:
% inputDate: Input date as a MATLAB serial datenumber
%
% Descriptions of Output Variables:
% doy: Decimal day of the year. For instance, an output of 1.5 would
% indicate that the time is noon on January 1.
% fraction: Outputs the fraction of a year that has elapsed by the input
% date.
%
% Example(s):
% >> [doy,frac] = date2doy(datenum('1/25/2004'));
%
% See also:
% Author: Anthony Kendall
% Contact: anthony [dot] kendall [at] gmail [dot] com
% Created: 2008-03-11
% Copyright 2008 Michigan State University.
%Want inputs in rowwise format
[doy,fraction] = deal(zeros(size(inputDate)));
inputDate = inputDate(:);
%Parse the inputDate
[dateVector] = datevec(inputDate);
%Set everything in the date vector to 0 except for the year
dateVector(:,2:end) = 0;
dateYearBegin = datenum(dateVector);
%Calculate the day of the year
doyRow = inputDate - dateYearBegin;
%Optionally, calculate the fraction of the year that has elapsed
flagFrac = (nargout==2);
if flagFrac
%Set the date as the end of the year
dateVector(:,1) = dateVector(:,1) + 1;
dateYearEnd = datenum(dateVector);
fracRow = (doyRow - 1) ./ (dateYearEnd - dateYearBegin);
end
%Fill appropriately-sized output array
doy(:) = doyRow;
if flagFrac
fraction(:) = fracRow;
end