1% Unpack a vector into different variables under the control of a mask.
2%
3% [var1, var2, ...] = statevector_unpack(s,x)
4% [var1, var2, ...] = statevector_unpack(s,x,fillvalue)
5%
6% Unpack the vector x into the different variables var1, var2, ...
7%
8% Input:
9%   s: structure generated by statevector_init.
10%   x: vector of the packed elements. The size of this vector is the number of elements equal to 1
11%     in all masks.
12%
13% Optional input parameter:
14%   fillvalue: The value to fill in var1, var2,... where the masks correspond to a land grid point. The default is zero.
15%
16% Output:
17%   var1, var2,...: unpacked variables.
18%
19% Notes:
20% If x is a matrix, then the second dimension is assumed
21% to represent the different ensemble members. In this case,
22% var1, var2, ... have also an additional trailing dimension.
23
24% Author: Alexander Barth, 2009 <a.barth@ulg.ac.be>
25% License: GPL 2 or later
26
27function varargout = statevector_unpack(s,x,fillvalue)
28
29if (nargin ==  2)
30  fillvalue = 0;
31end
32
33k = size(x,2);
34
35for i=1:s.nvar
36  v = zeros(s.numels_all(i),k);
37  v(:) = fillvalue;
38
39  ind = find(s.mask{i}==1);
40
41  v(ind,:) = x(s.ind(i)+1:s.ind(i+1),:);
42
43  varargout{i} = reshape(v,[s.size{i} k]);
44end
45
46
47
48% Copyright (C) 2009 Alexander Barth <a.barth@ulg.ac.be>
49%
50% This program is free software; you can redistribute it and/or modify
51% it under the terms of the GNU General Public License as published by
52% the Free Software Foundation; either version 2 of the License, or
53% (at your option) any later version.
54%
55% This program is distributed in the hope that it will be useful,
56% but WITHOUT ANY WARRANTY; without even the implied warranty of
57% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
58% GNU General Public License for more details.
59%
60% You should have received a copy of the GNU General Public License
61% along with this program; If not, see <http://www.gnu.org/licenses/>.
62
63
64% LocalWords:  statevector fillvalue init GPL varargout
65