source: trunk/src/mask_proj.m @ 1114

Last change on this file since 1114 was 1107, checked in by g7moreau, 3 years ago

Update Copyright to 2022

File size: 5.4 KB
Line 
1%'mask_proj': restrict input fields to a mask region, set to 0 outside
2%--------------------------------------------------------------------------
3%  function [ProjData,errormsg]=mask_proj(FieldData,MaskData)
4
5%=======================================================================
6% Copyright 2008-2022, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
7%   http://www.legi.grenoble-inp.fr
8%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
9%
10%     This file is part of the toolbox UVMAT.
11%
12%     UVMAT is free software; you can redistribute it and/or modify
13%     it under the terms of the GNU General Public License as published
14%     by the Free Software Foundation; either version 2 of the license,
15%     or (at your option) any later version.
16%
17%     UVMAT is distributed in the hope that it will be useful,
18%     but WITHOUT ANY WARRANTY; without even the implied warranty of
19%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20%     GNU General Public License (see LICENSE.txt) for more details.
21%=======================================================================
22
23function [ProjData,errormsg]=mask_proj(FieldData,MaskData)
24errormsg='';%default
25ProjData=FieldData;
26
27
28%% group the variables (fields of 'FieldData') in cells of variables with the same dimensions
29[CellInfo,NbDimArray,errormsg]=find_field_cells(FieldData);
30if ~isempty(errormsg)
31    errormsg=['error in proj_field/proj_plane:' errormsg];
32    return
33end
34[Npy,Npx]=size(MaskData.A);
35
36for icell=1:numel(CellInfo)
37    if NbDimArray(icell)==2
38        if isfield(CellInfo{icell},'VarIndex_errorflag')
39            FFName=FieldData.ListVarName{CellInfo{icell}.VarIndex_errorflag};
40            check_new_error_flag=0;
41        else
42            FFName='FF';%default error (mask) flag name (if not already used)
43            if isfield(FieldData,'FF')
44                ind=1;
45                while isfield(FieldData,['FF_' num2str(ind)])
46                    ind=ind+1;
47                end
48                FFName=['FF_' num2str(ind)];% append an index to the name of error flag, FF_1,FF_2...
49            end
50            ProjData.ListVarName=[FieldData.ListVarName {FFName}];
51            ProjData.VarAttribute{numel(ProjData.ListVarName)}.Role='errorflag';
52            check_new_error_flag=1;
53        end
54        switch CellInfo{icell}.CoordType;
55            case 'scattered'   
56                XName=FieldData.ListVarName{CellInfo{icell}.Coord_x};
57                YName=FieldData.ListVarName{CellInfo{icell}.Coord_y};
58                DX=(MaskData.Coord_x(2)-MaskData.Coord_x(1))/(Npx-1);
59                DY=(MaskData.Coord_y(2)-MaskData.Coord_y(1))/(Npy-1);
60                mask_ind_i=round(0.5+(FieldData.(XName)-MaskData.Coord_x(1))/DX);%nbpoint terms
61                mask_ind_j=round(0.5+(FieldData.(YName)-MaskData.Coord_y(1))/DY);%nbpoint terms
62                checkin=mask_ind_j+Npy*(mask_ind_i-1);%array  of mask indices for the nbpoints
63                checkin=checkin(mask_ind_i>=1 & mask_ind_i<=Npx & mask_ind_j>=1 & mask_ind_j<=Npy);%reduced array  of mask indices (inside the image)
64                checkfalse=true(size(FieldData.(XName)));
65                MaskData.A=reshape(MaskData.A,1,[]);
66                checkfalse(MaskData.A(checkin)>200)=0;
67                for ivar=1:numel(CellInfo{icell}.VarIndex)
68                    VarName=FieldData.ListVarName{CellInfo{icell}.VarIndex(ivar)};
69                    ProjData.(VarName)(checkfalse)=0;
70                end
71                if check_new_error_flag% an error flag needs to be created in the current cell
72                    ProjData.(FFName)=zeros(size(ProjData.(VarName)));
73                    ProjData.VarDimName=[FieldData.VarDimName FieldData.VarDimName(CellInfo{icell}.VarIndex(1))];
74                end
75                ProjData.(FFName)(checkfalse)=1;% update the existing error flag             
76            case 'grid'
77                XName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(2)};
78                YName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(1)};
79                Var1Name=FieldData.ListVarName{CellInfo{icell}.VarIndex(1)};
80                [Npy_field,Npx_field]=size(FieldData.(Var1Name));
81                XArray=linspace(FieldData.(XName)(1),FieldData.(XName)(end),Npx_field);
82                YArray=linspace(FieldData.(YName)(1),FieldData.(YName)(end),Npy_field);
83                XMask=linspace(MaskData.Coord_x(1),MaskData.Coord_x(end),Npx);
84                YMask=linspace(MaskData.Coord_y(1),MaskData.Coord_y(end),Npy);
85                [XMask,YMask]=meshgrid(XMask,YMask);
86                Mask = interp2(XMask,YMask,MaskData.A,XArray,YArray','nearest');
87                Mask=Mask>200;               
88                for ivar=1:numel(CellInfo{icell}.VarIndex)
89                    VarName=FieldData.ListVarName{CellInfo{icell}.VarIndex(ivar)};
90                    if ~strcmp(VarName,FFName)
91                        ProjData.(VarName)=FieldData.(VarName).*Mask;
92                    end
93                end
94                if check_new_error_flag% an error flag needs to be created in the current cell
95                    ProjData.(FFName)= ~Mask;
96                    ProjData.VarDimName=[FieldData.VarDimName FieldData.VarDimName(CellInfo{icell}.VarIndex(1))];
97                else
98                    ProjData.(FFName)=FieldData.(FFName) | ~Mask;
99                end
100        end
101    end
102end
103
104
Note: See TracBrowser for help on using the repository browser.