Changeset 866 for trunk/src/sub_field.m


Ignore:
Timestamp:
Feb 8, 2015, 7:26:02 AM (10 years ago)
Author:
sommeria
Message:

stereo_civ_updated

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sub_field.m

    r864 r866  
    125125
    126126%% look for coordinates common to Field in Field_1
    127 ind_remove=zeros(size(Field_1.ListVarName));
     127ind_remove=false(size(Field_1.ListVarName));
    128128% loop on the variables of the second field Field_1
    129129for ilist=1:numel(Field_1.ListVarName)
    130130    % case of variable with a single dimension
    131     if ~isempty(regexp(Field_1.VarAttribute{ilist}.Role,'^coord'))
     131    if ~isempty(Field_1.VarAttribute{ilist}) && ~isempty(regexp(Field_1.VarAttribute{ilist}.Role,'^coord'))% if variable with Role coord... is found.
    132132        OldDimName=Field_1.VarDimName{ilist};
    133133        if ischar(OldDimName), OldDimName={OldDimName}; end% transform char string to cell if relevant
     
    147147    end
    148148end
    149 Field_1.ListVarName(find(ind_remove))=[];%removes the redondent coordinate
    150 Field_1.VarDimName(find(ind_remove))=[];
    151 Field_1.VarAttribute(find(ind_remove))=[];
     149if ~isempty(find(ind_remove, 1))
     150Field_1.ListVarName(ind_remove)=[];%removes the redondent coordinate
     151Field_1.VarDimName(ind_remove)=[];
     152Field_1.VarAttribute(ind_remove)=[];
     153end
    152154
    153155%% append the other variables of the second field, modifying their name if needed
    154 ListVarNameNew=Field_1.ListVarName;
    155 check_rename=zeros(size(ListVarNameNew));
    156 check_remove=zeros(size(ListVarNameNew));
    157 for ilist=1:numel(ListVarNameNew)
    158     VarName=Field_1.ListVarName{ilist};
    159     ind_prev=find(strcmp(ListVarNameNew{ilist},Field.ListVarName),1);% look for duplicated variable name
     156ListVarNameSub=Field_1.ListVarName;
     157ListVarNameNew=ListVarNameSub;
     158check_rename=zeros(size(ListVarNameSub));
     159check_remove=zeros(size(ListVarNameSub));
     160for ilist=1:numel(ListVarNameSub)
     161    ind_prev=find(strcmp(ListVarNameSub{ilist},Field.ListVarName),1);% look for duplicated variable name
    160162    if ~isempty(ind_prev)% variable name exists in Field
    161 %         check_rename(ilist)=0;
    162         check_remove(ilist)=1;
    163         if isfield(Field_1.VarAttribute{ilist},'Role')&& ismember(Field_1.VarAttribute{ilist}.Role,{'scalar','vector_x','vector_y'})
    164                             ListVarNameNew{ilist}=[ListVarNameNew{ilist} '_1'];
    165                             check_rename(ilist)=1;
    166         end
    167     end
    168     SubData.ListVarName=[SubData.ListVarName ListVarNameNew{ilist}];
    169     SubData.VarDimName=[SubData.VarDimName Field_1.VarDimName(ilist)];
    170     Field_1.VarAttribute{ilist}.CheckSub=1;
    171     SubData.VarAttribute=[SubData.VarAttribute Field_1.VarAttribute{ilist}];
    172     SubData.(ListVarNameNew{ilist})=Field_1.(VarName);% teke the values of the old variable for the newly named one
    173     %SubData.VarAttribute=[SubData.VarAttribute Field_1.VarDimName(ilist)];
    174 end
    175 
    176 %% replace variable name in field expression FieldName, e.g. 'norm(U,V)'-> 'norm(U_1,V_1)'
    177 for ilist=1:numel(ListVarNameNew)
    178     if check_rename(ilist)&&  isfield(Field_1.VarAttribute{ilist},'FieldName')
    179         for ivar=1:numel(find(check_rename))
    180             Field_1.VarAttribute{ilist}.FieldName=regexprep_r(Field_1.VarAttribute{ilist}.FieldName,...
    181                 Field_1.ListVarName{ivar},ListVarNameNew{ivar});
    182         end
    183     end
    184     SubData.VarAttribute=[SubData.VarAttribute Field_1.VarAttribute(ilist)];
    185     SubData.VarAttribute{end}.CheckSub=1;% mark that the field needs to be substracted as an attribute
    186 end
     163        if isfield(Field_1.VarAttribute{ilist},'Role')&&...
     164            ismember(Field_1.VarAttribute{ilist}.Role,{'coord_x','coord_y','scalar','vector_x','vector_y','errorflag'})
     165            ListVarNameNew{ilist}=[ListVarNameSub{ilist} '_1'];%modify the name of the second variable
     166            check_rename(ilist)=1;
     167        else
     168            check_remove(ilist)=1;% variable will be removed
     169        end
     170    end
     171end
     172ListVarNameSub=ListVarNameSub(~check_remove); %eliminate removed variables from the list of the second field
     173ListVarNameNew=ListVarNameNew(~check_remove); % %list of renaimed varaibles corresponding to ListVarNameSub
     174VarDimNameSub=Field_1.VarDimName(~check_remove);
     175VarAttributeSub=Field_1.VarAttribute(~check_remove);
     176check_rename=check_rename(~check_remove);
     177
     178% apply the variable renaming and mark the second field variables with the attribute .CheckSub
     179for ilist=1:numel(ListVarNameSub)
     180     SubData.(ListVarNameNew{ilist})=Field_1.(ListVarNameSub{ilist});% copy the variable content to the new name
     181    if check_rename(ilist)   
     182          % replace name in field expression FieldName, e.g. 'norm(U,V)'-> 'norm(U_1,V_1)'
     183        if  isfield(VarAttributeSub{ilist},'FieldName')
     184            for ivar=1:numel(find(check_rename))
     185                VarAttributeSub{ilist}.FieldName=regexprep_r(VarAttributeSub{ilist}.FieldName,...
     186                    ListVarNameSub{ivar},ListVarNameNew{ivar});
     187            end
     188        end
     189    end
     190    VarAttributeSub{ilist}.CheckSub=1;% mark that the field needs to be substracted as an attribute
     191end
     192
     193SubData.ListVarName=[SubData.ListVarName ListVarNameNew];
     194SubData.VarDimName=[SubData.VarDimName VarDimNameSub];
     195SubData.VarAttribute=[SubData.VarAttribute VarAttributeSub];
    187196
    188197%% substrat fields when possible
    189198[CellInfo,NbDim,errormsg]=find_field_cells(SubData);
    190 ind_remove=zeros(size(SubData.ListVarName));
     199ind_remove=false(size(SubData.ListVarName));
    191200ivar=[];
    192201ivar_1=[];
    193202for icell=1:numel(CellInfo)
    194203    if ~isempty(CellInfo{icell})
     204        % if two scalar are in the same cell
    195205        if isfield(CellInfo{icell},'VarIndex_scalar') && numel(CellInfo{icell}.VarIndex_scalar)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_scalar(2)}.CheckSub;
    196206            ivar=[ivar CellInfo{icell}.VarIndex_scalar(1)];
    197207            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_scalar(2)];
    198208        end
     209        % if two vector u components are in the same cell
    199210        if isfield(CellInfo{icell},'VarIndex_vector_x') && numel(CellInfo{icell}.VarIndex_vector_x)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_x(2)}.CheckSub;
    200211            ivar=[ivar CellInfo{icell}.VarIndex_vector_x(1)];
    201212            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_x(2)];
    202213        end
     214         % if two vector v components are in the same cell
    203215        if isfield(CellInfo{icell},'VarIndex_vector_y') && numel(CellInfo{icell}.VarIndex_vector_y)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;
    204216            ivar=[ivar CellInfo{icell}.VarIndex_vector_y(1)];
    205217            ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_y(2)];
    206218        end
    207     end
    208 end
     219        % merge the error flags if needed
     220        if isfield(CellInfo{icell},'VarIndex_errorflag') && numel(CellInfo{icell}.VarIndex_errorflag)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;
     221            ivar_flag=CellInfo{icell}.VarIndex_errorflag(1);
     222            ivar_flag_1=CellInfo{icell}.VarIndex_errorflag(2);
     223            VarName=SubData.ListVarName{ivar_flag};
     224            VarName_1=SubData.ListVarName{ivar_flag_1};
     225            SubData.(VarName)=SubData.(VarName)~=0 | SubData.(VarName_1)~=0;% combine the error flags of the two fields
     226            ind_remove(ivar_flag_1)=1;
     227        end
     228    end
     229end
     230% subtract fields if relevant
    209231for imod=1:numel(ivar)
    210232        VarName=SubData.ListVarName{ivar(imod)};
     
    213235        ind_remove(ivar_1(imod))=1;
    214236end
    215 SubData.ListVarName(find(ind_remove))=[];
    216 SubData.VarDimName(find(ind_remove))=[];
    217 SubData.VarAttribute(find(ind_remove))=[];
     237SubData.ListVarName(ind_remove)=[];
     238SubData.VarDimName(ind_remove)=[];
     239SubData.VarAttribute(ind_remove)=[];
    218240
    219241function OutputCell=regexprep_r(InputCell,search_string,new_string)
Note: See TracChangeset for help on using the changeset viewer.