-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelstat2D.m
More file actions
69 lines (57 loc) · 2.49 KB
/
pixelstat2D.m
File metadata and controls
69 lines (57 loc) · 2.49 KB
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
function [days_above_threshold, first_time_threshold, data_maxima] = pixelstat2D(data,threshold)
%------------------------------------------------------------------------------
% pixelstat2D computes pixelwise maxima of a data,
% first day of occurrence of a threshold in each pixel
% and for how many days the data has remained above the thresold for each pixel
%------------------------------------------------------------------------------
% Input:
% data: 3D numeric array (latitude x longitude x time)
%
% Output:
% days_above_threshold:
% 2D array (latitude x longitude) containing how many days
% the value was above the threshold (e.g. temperature above
% 5 degrees)
%
% first_time_threshold:
% 2D array (latitude x longitude) containing the
% first time when the value rose abve the threshold
%
% data_maxima:
% 2D array (latitude x longitude) the maximum value of the
% data in each of the locations
%------------------------------------------------------------------------------
arguments
data {mustBeNumeric, mustBe3D, mustBeWithin366} % should be a 3d input data (latitude x longitude x time)
threshold (1,1) {mustBeNumeric} % can be negative or positive
end
% classify the data based on the matrix
for i = 1:size(data,1)
for j = 1:size(data,2)
% Extract the time series for every pixel using the daily mean data
timeseries = squeeze(data(i, j, :));
% Count the number of days where data >= threshold
days_above_threshold(i, j) = sum(timeseries >= threshold, 'omitnan');
% Find the first time index where data >= threshold
idx = find(timeseries >= threshold, 1);
% Store the time index if found, otherwise leave it as NaN
if ~isempty(idx)
first_time_threshold(i, j) = idx; % Store the time index
end
% find the maxima in each year for each of the pixel
data_maxima(i, j) = max(data(i, j, :), [], 'omitnan');
end
end
end
% Custom validation function to check if the input data is 3D
function mustBe3D(data)
if ndims(data) ~= 3
error("Input data must be a 3D matrix (latitude x longitude x days)")
end
end
function mustBeWithin366(data)
if size(data,3) > 366
error(['This is not a daily data for a year, The daily limit is 366, ...' ...
'Please keep the data within the format (latitude x longitude x days)'])
end
end