source: trunk/src/mask_proj.m @ 672

Last change on this file since 672 was 672, checked in by sommeria, 11 years ago

various bugs corrected

File size: 5.1 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%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
6%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
7%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8%     This file is part of the toolbox UVMAT.
9%
10%     UVMAT is free software; you can redistribute it and/or modify
11%     it under the terms of the GNU General Public License as published by
12%     the Free Software Foundation; either version 2 of the License, or
13%     (at your option) any later version.
14%
15%     UVMAT is distributed in the hope that it will be useful,
16%     but WITHOUT ANY WARRANTY; without even the implied warranty of
17%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
19%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
20
21function [ProjData,errormsg]=mask_proj(FieldData,MaskData)
22errormsg='';%default
23ProjData=FieldData;
24
25
26%% group the variables (fields of 'FieldData') in cells of variables with the same dimensions
27[CellInfo,NbDimArray,errormsg]=find_field_cells(FieldData);
28if ~isempty(errormsg)
29    errormsg=['error in proj_field/proj_plane:' errormsg];
30    return
31end
32[Npy,Npx]=size(MaskData.A);
33DX=(MaskData.AX(2)-MaskData.AX(1))/(Npx-1);
34DY=(MaskData.AY(2)-MaskData.AY(1))/(Npy-1);
35for icell=1:numel(CellInfo)
36    if NbDimArray(icell)==2
37        XName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(1)};
38        YName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(2)};
39        if isfield(CellInfo{icell},'VarIndex_errorflag')
40            FFName=FieldData.ListVarName{CellInfo{icell}.VarIndex_errorflag};
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.VarDimName=[FieldData.VarDimName FieldData.VarDimName(CellInfo{icell}.CoordIndex(1))];
52            ProjData.VarAttribute{numel(FieldData.VarDimName)}.Role='errorflag';
53        end
54        switch CellInfo{icell}.CoordType;
55            case  'scattered'               
56                mask_ind_i=round(0.5+(FieldData.(XName)-MaskData.AX(1))/DX);%nbpoint terms
57                mask_ind_j=round(0.5+(FieldData.(YName)-MaskData.AY(1))/DY);%nbpoint terms
58                checkin=mask_ind_j+Npy*(mask_ind_i-1);%array  of mask indices for the nbpoints
59                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)
60                checkfalse=true(size(FieldData.(XName)));
61                MaskData.A=reshape(MaskData.A,1,[]);
62                checkfalse(MaskData.A(checkin)>200)=0;
63                for ivar=1:numel(CellInfo{icell}.VarIndex)
64                    VarName=FieldData.ListVarName{CellInfo{icell}.VarIndex(ivar)};
65                    ProjData.(VarName)(checkfalse)=0;
66                end
67                if ~isfield(CellInfo{icell},'VarIndex_errorflag')% an error flag already exists in the current cell
68                    ProjData.(FFName)=zeros(size(ProjData.(VarName)));
69                end
70                ProjData.(FFName)(checkfalse)=1;
71            case  'grid'
72                Var1Name=FieldData.ListVarName{CellInfo{icell}.VarIndex(1)};
73                [Npy_field,Npx_field]=size(FieldData.(Var1Name));
74                DX_field=(FieldData.(XName)(end)-FieldData.(XName)(1))/(Npx_field-1);
75                DY_field=(FieldData.(YName)(end)-FieldData.(YName)(1))/(Npy_field-1);
76                XArray=FieldData.(XName)(1):DX_field:FieldData.(XName)(end);
77                YArray=FieldData.(YName)(1):DY_field:FieldData.(YName)(end);
78                XMask=MaskData.AX(1):DX:MaskData.AX(end);
79                YMask=MaskData.AY(end):-DY:MaskData.AY(1);
80                [XMask,YMask]=meshgrid(XMask,YMask);
81                Mask = interp2(XMask,YMask,MaskData.A,XArray,YArray','nearest');
82                Mask=Mask>200;               
83                for ivar=1:numel(CellInfo{icell}.VarIndex)
84                    VarName=FieldData.ListVarName{CellInfo{icell}.VarIndex(ivar)};
85                    if ~strcmp(VarName,FFName)
86                        ProjData.(VarName)=FieldData.(VarName).*Mask;
87                    end
88                end
89                if isfield(CellInfo{icell},'VarIndex_errorflag')% an error flag already exists in the current cell
90                    ProjData.(FFName)=FieldData.(FFName) | ~Mask;
91                else
92                    ProjData.(FFName)= ~Mask;
93                end
94        end
95    end
96end
97
98
Note: See TracBrowser for help on using the repository browser.