File size: 3,888 Bytes
5ab75a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function [DateNum,DateVec] = DOY2Date(DOY,YEAR)
% DOY2DATE  - Converts Day of Year (DOY) to MATLAB date number and full 
%             calendar date. 
%
% Syntax:     [DateNum,DateVec] = DOY2DATE(DOY,YEAR)
%
% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Input:
%
%   DOY     -   Column vector of days of years between 0 (exclusive)  [nx1]
%               and 365 (or 366 on leap years) (inclusive)
%   YEAR    -   Column vector of positive years                       [nx1]
%
% =========================================================================
%
% Output:
%
%   DateNum -   Column vector of MATLAB date numbers                  [nx1]
%   DateVec -   Matrix of calendar dates                              [nx6]
%
% =========================================================================
%
% Initial version: Feb 2015;  Latest update: Jun 2023
%
% ----------------- BEGIN CODE -----------------

    % Input validation
    
    % Check that input vectors are non-empty
    if isempty(DOY) || isempty(YEAR)
        error('DOY2Date:InputMustBeNonempty', 'ERROR - Input vectors cannot be empty');
    end
    
    % Check that inputs are column vectors
    if ~(iscolumn(DOY) && iscolumn(YEAR))
        error('DOY2Date:InputsMustBeColumnVectors', 'ERROR - Inputs must be column vectors');
    end
    
    % Check that vectors of days and years are the same size
    if length(DOY) ~= length(YEAR)
        error('DOY2Date:InputSizesMustMatch', 'ERROR - Input vector sizes must match');
    end
    
    % Check that both inputs are numeric
    if ~(isa(DOY, 'numeric') && isa(YEAR, 'numeric'))
        error('DOY2Date:InputsMustBeNumeric', 'ERROR - DOY and YEAR must be numeric');
    end
    
    % Check that both inputs are positive
    if any(DOY <= 0) || any(YEAR <= 0)
        error('DOY2Date:InputsMustBePositive', 'ERROR - DOY and YEAR must be positive numbers');
    end
    
    % Check that the day of year never exceeds 366 and can only be 366 on
    % leap years
    if any(DOY > 366)
        error('DOY2Date:DOYLimitExceeded', 'ERROR - DOY cannot exceed 366');
    elseif any(DOY == 366)
        yearCheck = YEAR(DOY == 366);
        for i = 1:length(yearCheck)
            if ~(mod(yearCheck(i), 4) == 0 && ...
                    (mod(yearCheck(i), 400) == 0 || ...
                    ~mod(yearCheck(i), 100) == 0))
                error('DOY2Date:InputYearNotLeap', 'ERROR - Input year is not a leap year');
            end
        end
    end
    
    % Calculate MATLAB date number
    DateNum = DOY + datenum(horzcat(YEAR,zeros(length(YEAR),5)));
    
    % Convert MATLAB date number to calendar date
    DateVec = datevec(DateNum);
    
return

% ----------------- END OF CODE -----------------
%
% Please record any changes to the software in the change history 
% shown below:
%
%---------------- CHANGE HISTORY ------------------
% Developer      |    Date    |     Description
%--------------------------------------------------
% D. Plakalovic  |  Feb-2015  |  Initial Development
% D. Plakalovic  | 02-23-2015 |  Checked for functionality
% E. White       | 06-07-2023 |  Added input validation, added new header 
%                                and footer

% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================