Changeset 674 for trunk/src/get_field.m


Ignore:
Timestamp:
Aug 23, 2013, 2:56:17 PM (11 years ago)
Author:
sommeria
Message:

various bugs repaired, in particula timing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/get_field.m

    r672 r674  
    11%'get_field': display variables and attributes from a Netcdf file, and OK selected fields
    22%------------------------------------------------------------------------
    3 %function varargout = get_field(varargin)
     3% GetFieldData=get_field(FileName,ParamIn)
    44% associated with the GUI get_field.fig
     5%
     6% OUPUT:
     7% GetFieldData: structure containing the information on the selected
     8%      fields, obtained by applying the fct red_GUI to the GUI get_field
     9%   .FieldOption='vectors': variables are used for vector plot
     10%                  'scalar': variables are used for scalar plot,
     11%                  '1Dplot': variables are used for usual x-y plot,
     12%                  'civdata...': go back to automatic reading of civ data
     13%   .PanelVectors: sub-structure variables used as vector components
     14%   .PanelScalar:
     15% INPUT:
     16% FileName: name (including path) of the netcdf file to open
    517%
    618%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
     
    4254end
    4355% End initialization code - DO NOT EDIT
    44 
     56     
    4557%------------------------------------------------------------------------
    4658% --- Executes just before get_field is made visible.
     
    5466
    5567%% enter input data
    56 set(handles.inputfile,'String',filename)% fill the input file name
    57 Field=nc2struct(filename,[]);% reads the  field structure, without the variables
     68if ischar(filename)% input file name
     69    set(handles.inputfile,'String',filename)% fill the input file name
     70    Field=nc2struct(filename,[]);% reads the  field structure, without the variables
     71else
     72    'bad input to get_field'
     73end
     74if ~exist('ParamIn','var')
     75    ParamIn=[];
     76end
    5877if isfield(Field,'Txt')
    59     msgbox_uvmat('ERROR',['get_field/nc2struct/' Field.Txt])% display error message for input fiel reading
     78    msgbox_uvmat('ERROR',['get_field/nc2struct/' Field.Txt])% display error message for input file reading
    6079    return
    6180end
    6281if ~isfield(Field,'ListVarName')
    6382    return
    64 end
    65 if ~exist('ParamIn','var')
    66     ParamIn=[];
    6783end
    6884
     
    111127ListSwitchVarIndexTime={'file index'};% default setting: the time is the file index
    112128% look at global attributes with numerical values
    113 check_numvalue=false;
    114 check_time=false;
     129check_numvalue=false(1,numel(Field.ListGlobalAttribute));
    115130for ilist=1:numel(Field.ListGlobalAttribute)
    116131    Value=Field.(Field.ListGlobalAttribute{ilist});
    117132    check_numvalue(ilist)=isnumeric(Value);
    118     check_time(ilist)=~isempty(find(regexp(Field.ListGlobalAttribute{ilist},'Time'),1));
    119133end
    120134Field.Display.ListGlobalAttribute=Field.ListGlobalAttribute(check_numvalue);% select the attributes with float numerical value
     
    122136    ListSwitchVarIndexTime=[ListSwitchVarIndexTime; {'attribute'}];% the time can be chosen as a global attribute
    123137end
    124 nboption=numel(ListSwitchVarIndexTime);
    125138if Field.MaxDim>=2
    126139    ListSwitchVarIndexTime=[ListSwitchVarIndexTime;{'variable'};{'dim index'}];% the time can be chosen as a dim index
    127140end
    128 if ~isempty(find(check_time, 1))
     141
     142%% select the Time attribute from input
     143if isfield(ParamIn,'TimeAttrName')
     144    time_index=find(strcmp(ParamIn.TimeAttrName,Field.Display.ListGlobalAttribute),1);
     145else
     146    time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);
     147end
     148if ~isempty(time_index)
    129149    set(handles.SwitchVarIndexTime,'Value',2);
     150    set(handles.TimeName,'UserData',time_index)
    130151else
    131152    set(handles.SwitchVarIndexTime,'Value',1);
     
    150171end
    151172if Field.MaxDim>=2 % case of 2D (or 3D) fields
    152     if isfield(CellInfo{imax},'VarIndex_vector_x') &&  isfield(CellInfo{imax},'VarIndex_vector_y')
     173    check_vec_input=0;
     174    if isfield(ParamIn,'vector_x')&& isfield(ParamIn,'vector_y')
     175        ichoice_x=find(strcmp(ParamIn.vector_x,Field.Display.ListVarName),1);
     176        ichoice_y=find(strcmp(ParamIn.vector_y,Field.Display.ListVarName),1);
     177        if ~isempty(ichoice_x)&&~isempty(ichoice_y)
     178            set(handles.vector_x,'UserData',ichoice_x)
     179            set(handles.vector_y,'UserData',ichoice_y)
     180            check_vec_input=1;
     181        end
     182    end
     183    if ~check_vec_input && isfield(CellInfo{imax},'VarIndex_vector_x') &&  isfield(CellInfo{imax},'VarIndex_vector_y')
     184        set(handles.vector_x,'UserData',CellInfo{imax}.VarIndex_vector_x(1))
     185        set(handles.vector_y,'UserData',CellInfo{imax}.VarIndex_vector_y(1))
     186        check_vec_input=1;
     187    end
     188    if check_vec_input
    153189        set(handles.FieldOption,'Value',3)% set vector selection option
    154         set(handles.vector_x,'Value',CellInfo{imax}.VarIndex_vector_x(1))
    155         set(handles.vector_y,'Value',CellInfo{imax}.VarIndex_vector_y(1))
    156         set(handles.FieldOption,'Value',3)
    157190    else
    158         set(handles.FieldOption,'Value',2)
     191        set(handles.FieldOption,'Value',2)% set scalar selection option
    159192    end
    160193else % case of 1D fields
     
    177210%% fill menus for coordinates and time
    178211FieldOption_Callback(handles.variables,[], handles)% list the global attributes
    179 %     if isfield(CellInfo{imax},'VarIndex_coord_x')&&  isfield(CellInfo{imax},'VarIndex_coord_y')
    180 %         set(handles.Coord_x,'Value',CellInfo{imax}.VarIndex_coord_x(1))
    181 %         set(handles.Coord_y,'Value',CellInfo{imax}.VarIndex_coord_y(1))
    182 %     end
    183212
    184213%% Make choices of coordinates from input
     
    214243return
    215244
    216 SwitchVarIndexTime_Callback([],[], handles)
    217 
    218245%% set z coordinate menu if relevant
    219246if Field.MaxDim>=3
     
    227254    set(handles.Coord_z,'Visible','off')
    228255    set(handles.Z_title,'Visible','off')
     256end
     257
     258%% make selections according to ParamIn
     259if isfield(ParamIn,'vector_x') && isfield(ParamIn,'vector_y')
     260   
    229261end
    230262
     
    300332            for iline=1:length(attr_list)
    301333                Tabcell{iline,1}=attr_list{iline};
    302                 eval(['val=VarAttr.' attr_list{iline} ';'])
     334                val=VarAttr.(attr_list{iline}) ;
    303335                if ischar(val);
    304336                    Tabcell{iline,2}=val;
     
    406438        end       
    407439        scalar_Callback(hObject, eventdata, handles)
    408        
     440             
    409441    case 'vectors'
     442        set(handles.PanelVectors,'Visible','on')
    410443        set(handles.Coordinates,'Visible','on')
    411444        set(handles.PanelOrdinate,'Visible','off')
    412445        set(handles.PanelScalar,'Visible','off')
    413         set(handles.PanelVectors,'Visible','on')
    414446        pos=get(handles.PanelVectors,'Position');
    415447        pos(1)=2;
     
    420452        set(handles.Y_title,'Visible','on')
    421453        %default vector selection
    422         test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
    423         for ilist=1:numel(Field.Display.VarDimName)
    424             if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
    425                 Role=Field.Display.VarAttribute{ilist}.Role;
    426                 if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
     454        vector_x_value=get(handles.vector_x,'UserData');
     455        vector_y_value=get(handles.vector_y,'UserData');
     456        if ~isempty(vector_x_value)&&~isempty(vector_y_value)
     457            set(handles.vector_x,'Value',vector_x_value)
     458            set(handles.vector_y,'Value',vector_y_value)
     459        else
     460            test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordinate
     461            for ilist=1:numel(Field.Display.VarDimName)
     462                if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
     463                    Role=Field.Display.VarAttribute{ilist}.Role;
     464                    if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
     465                        test_coord(ilist)=1;
     466                    end
     467                end
     468                dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
     469                if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
    427470                    test_coord(ilist)=1;
    428471                end
    429472            end
    430             dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
    431             if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
    432                 test_coord(ilist)=1;
     473            vector_index=find(~test_coord,2);%get the two first variables not a coordinate
     474            if isempty(vector_index)
     475                set(handles.vector_x,'Value',1)
     476                set(handles.vector_y,'Value',2)
     477            else
     478                set(handles.vector_x,'Value',vector_index(1))
     479                set(handles.vector_y,'Value',vector_index(2))
    433480            end
    434481        end
    435         vector_index=find(~test_coord,2);%get the first variable not a coordiante
    436         if isempty(vector_index)
    437             set(handles.vector_x,'Value',1)
    438             set(handles.vector_y,'Value',2)
    439         else
    440             set(handles.vector_x,'Value',vector_index(1))
    441             set(handles.vector_y,'Value',vector_index(2))
    442         end       
    443482        vector_Callback(handles)
    444483       
     
    525564end 
    526565update_field(handles,YName)
    527 
     566         
    528567%------------------------------------------------------------------------
    529568% --- Executes on selection change in scalar menu.
     
    801840    case 'attribute'
    802841        set(handles.TimeName, 'Visible','on')% timeName menu represents the available attributes
    803         time_index=[];
    804         PreviousList=get(handles.TimeName, 'String');
    805         index=[];
    806         if ~isempty(PreviousList)
    807             PreviousAttr=PreviousList{get(handles.TimeName, 'Value')};
    808             index=find(strcmp(PreviousAttr,Field.Display.ListGlobalAttributes));
    809         end
    810         if isempty(index)
    811             time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')));% index of the attributes containing the string 'Time'
    812         end
     842        time_index=get(handles.TimeName,'UserData');    %select the input data
     843        if isempty(time_index)
     844            PreviousList=get(handles.TimeName, 'String');
     845            if ~isempty(PreviousList)
     846                PreviousAttr=PreviousList{get(handles.TimeName, 'Value')};
     847                index=find(strcmp(PreviousAttr,Field.Display.ListGlobalAttributes),1);
     848            end
     849        end
     850        if isempty(time_index)
     851            time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);% index of the attributes containing the string 'Time'
     852        end     
    813853        if ~isempty(time_index)
    814             set(handles.TimeName,'Value',time_index(1))
     854            set(handles.TimeName,'Value',time_index)
    815855        else
    816856            set(handles.TimeName,'Value',1)
    817857        end
    818858        set(handles.TimeName, 'String',Field.Display.ListGlobalAttribute)
     859
    819860    case 'variable'% TimeName menu represents the available variables
    820861        set(handles.TimeName, 'Visible','on')
Note: See TracChangeset for help on using the changeset viewer.