-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_daily2monthly.m
72 lines (59 loc) · 1.54 KB
/
data_daily2monthly.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
%==========================================================================
% Calculate the monthly mean data
%
% Input : --- time0, the input daily time (MATLAB time format)
% --- data0, the input daily data
% --- varargin, time limits
%
% Output : --- time, the output monthly time (MATLAB time format)
% --- data, the output monthly mean data
%
% Usage : [time, data] = data_daily2monthly(time0, data0);
%
% v1.0
%
% Siqi Li
% 2021-05-26
%
% Updates:
%
%==========================================================================
function [time, data] = data_daily2monthly(time0, data0, varargin)
% time0 = dayt;
% data0 = data2;
tlim = [];
if ~isempty(varargin)
tlim = varargin{1};
if sum(tlim<3000)==2
tlim(1) = datenum(tlim(1), 1, 1);
tlim(2) = datenum(tlim(2), 12, 1);
end
end
t1 = time0(1);
t2 = time0(end);
if isempty(tlim)
tvec1 = datevec(t1);
tvec2 = datevec(t2);
else
tvec1 = datevec(tlim(1));
tvec2 = datevec(tlim(2));
end
tvec0 = [tvec1(1) tvec1(2) 1 0 0 0];
tvec3 = [tvec2(1) tvec2(2)+1 1 0 0 0];
it1 = datenum(tvec0);
ii = 1;
while it1 < datenum(tvec3)
it1_vec = datevec(it1);
it1_vec(2) = it1_vec(2) + 1;
it2 = datenum(it1_vec);
k = find(time0>=it1 & time0<it2);
time(ii,1) = it1;
data(ii,1) = nanmean(data0(k));
it1 = it2;
ii = ii + 1;
end
% if ~isempty(tlim)
% k = find(time>=tlim(1) & time<=tlim(2));
% time = time(k);
% data = data(k);
% end