1% Initialize structure for packing and unpacking given their mask. 2% 3% s = statevector_init(mask1, mask2, ...) 4% 5% Initialize structure for packing and unpacking 6% multiple variables given their corresponding land-sea mask. 7% 8% Input: 9% mask1, mask2,...: land-sea mask for variable 1,2,... Sea grid points correspond to one and land grid points to zero. 10% Every mask can have a different shape. 11% 12% Output: 13% s: structure to be used with statevector_pack and statevector_unpack. 14% 15% Note: 16% see also statevector_pack, statevector_unpack 17 18% Author: Alexander Barth, 2009 <a.barth@ulg.ac.be> 19% License: GPL 2 or later 20 21function s = statevector_init(varargin) 22 23s.nvar = nargin; 24 25 26for i=1:nargin 27 mask = varargin{i}; 28 s.mask{i} = mask; 29 s.numels(i) = sum(mask(:) == 1); 30 s.numels_all(i) = numel(mask); 31 s.size{i} = size(mask); 32end 33 34s.ind = [0 cumsum(s.numels)]; 35 36s.n = s.ind(end); 37 38%!test 39%! mask = rand(10,10) > .5; 40%! mask_u = rand(9,10) > .5; 41%! mask_v = rand(10,9) > .5; 42%! sv = statevector_init(mask,mask_u,mask_v); 43%! var = rand(10,10); 44%! var(mask==0) = 0; 45%! var_u = rand(9,10); 46%! var_u(mask_u==0) = 0; 47%! var(mask==0) = 0; 48%! var_v = rand(10,9); 49%! var_v(mask_v==0) = 0; 50%! [E] = statevector_pack(sv,var,var_u,var_v); 51%! [Ezeta2,Eu2,Ev2] = statevector_unpack(sv,E); 52%! assert(Ezeta2,var) 53%! assert(Eu2,var_u) 54%! assert(Ev2,var_v) 55 56 57 58% Copyright (C) 2009 Alexander Barth <a.barth@ulg.ac.be> 59% 60% This program is free software; you can redistribute it and/or modify 61% it under the terms of the GNU General Public License as published by 62% the Free Software Foundation; either version 2 of the License, or 63% (at your option) any later version. 64% 65% This program is distributed in the hope that it will be useful, 66% but WITHOUT ANY WARRANTY; without even the implied warranty of 67% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 68% GNU General Public License for more details. 69% 70% You should have received a copy of the GNU General Public License 71% along with this program; If not, see <http://www.gnu.org/licenses/>. 72 73 74% LocalWords: statevector init GPL 75