-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdiffcell.m
103 lines (78 loc) · 3.11 KB
/
cdiffcell.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
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
function ddt = cdiffcell(inpt, varargin);
% The function computes the first derivative of a given input dataset inpt
% according to the method of central differences. For the first (last)
% field, the derivative is computed according to forward (backward)
% differences.
%--------------------------------------------------------------------------
% Input: inpt {m x n} Cell array which contains the input
% fields.
% clms [1 x 3] column indices of the input containing
% month, year and the corresponding field
%
% Output: otpt [m x k] matrix containing the area-weighted
% means
%--------------------------------------------------------------------------
% Author: Christof Lorenz
% Date: July 2011
%--------------------------------------------------------------------------
% Uses: area_wghts.m
%--------------------------------------------------------------------------
% Checking input arguments and setting default values
pp = inputParser;
pp.addRequired('inpt', @(x)iscell(x)); % Input dataset (cell)
pp.addOptional('clms', [3 4 8], @(x) isnumeric(x)); % Columns of m/y/flds
pp.addOptional('time', 0, @(x) isnumeric(x)); % start and end time
pp.addOptional('domweight', 1, @(x) isnumeric(x)); % Apply days of month weighting
pp.parse(inpt, varargin{:})
clms = pp.Results.clms;
time = pp.Results.time;
domweight = pp.Results.domweight;
if size(clms) == [1 1] % No time information is available
if domweight == 1
warning('No time infomration provided!!! Skipping dom weighting')
end
indx_c = (1:length(inpt))';
indx_b = [1; indx_c(1:end-1)];
indx_f = [indx_c(2:end); indx_c(end)];
div = [1; ones(length(indx_c)-2,1)*2; 1];
else
mnth = cell2mat(inpt(:, clms(1)));
yr = cell2mat(inpt(:, clms(2)));
dom = eomday(yr, mnth);
flds = inpt(:,clms(3));
if time == 0
sind = 1;
eind = length(flds);
else
sind = find((mnth) == 1 & yr == time(1));
eind = find((mnth) == 12 & yr == time(2));
end
indx_c = (sind:1:eind)';
div = zeros(length(indx_c),1);
% if sind == 1
% indx_b = [1; indx_c(1:end-1-1)];
% div(1) = [1; ones(length(indx_c)-2,1)*2; 1];
% else
% indx_b = [sind-1; indx_c(1:end-1-1)];
% end
%
% if eind == numel(flds)
% indx_f = [indx_c(2:end); indx_c(end)];
% else
% indx_f = [indx_c(2:end); eind+1];
% end
end
fields = inpt(sind:eind, clms);
n = length(fields);
keyboard
for i = 1:n
ddt{i,1} = fields{i,1};
ddt{i,2} = fields{i,2};
if i == 1
ddt{i,3} = (dom(i+1)*fields{i+1,3} - dom(i)*fields{i,3})/(dom(i+1) + dom(i));
elseif i == n
ddt{i,3} = (dom(i)*fields{i,3} - dom(i-1)*fields{i-1,3})/(dom(i) + dom(i-1));
else
ddt{i,3} = (dom(i+1)*fields{i+1,3} - dom(i-1)*fields{i-1,3})/(dom(i+1)/2 + dom(i) + dom(i-1)/2);
end
end