source: trunk/src/get_field.m @ 654

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

various bugs corrected

File size: 32.0 KB
Line 
1%'get_field': display variables and attributes from a Netcdf file, and OK selected fields
2%------------------------------------------------------------------------
3%function varargout = get_field(varargin)
4% associated with the GUI get_field.fig
5%
6%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
7%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
8%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
9%     This file is part of the toolbox UVMAT.
10%
11%     UVMAT is free software; you can redistribute it and/or modify
12%     it under the terms of the GNU General Public License as published by
13%     the Free Software Foundation; either version 2 of the License, or
14%     (at your option) any later version.
15%
16%     UVMAT is distributed in the hope that it will be useful,
17%     but WITHOUT ANY WARRANTY; without even the implied warranty of
18%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
20%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
21
22function varargout = get_field(varargin)
23
24% Last Modified by GUIDE v2.5 09-Jun-2013 11:15:54
25
26% Begin initialization code - DO NOT EDIT
27gui_Singleton = 1;
28gui_State = struct('gui_Name',       mfilename, ...
29                   'gui_Singleton',  gui_Singleton, ...
30                   'gui_OpeningFcn', @get_field_OpeningFcn, ...
31                   'gui_OutputFcn',  @get_field_OutputFcn, ...
32                   'gui_LayoutFcn',  [] , ...
33                   'gui_Callback',   []);
34if nargin && ischar(varargin{1})&& ~isempty(regexp(varargin{1},'_Callback','once'))
35    gui_State.gui_Callback = str2func(varargin{1});
36end
37
38if nargout
39    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
40else
41    gui_mainfcn(gui_State, varargin{:});
42end
43% End initialization code - DO NOT EDIT
44
45%------------------------------------------------------------------------
46% --- Executes just before get_field is made visible.
47%------------------------------------------------------------------------
48function get_field_OpeningFcn(hObject, eventdata, handles,filename,ParamIn)
49
50%% GUI settings
51handles.output = 'Cancel';
52guidata(hObject, handles);
53set(hObject,'WindowButtonDownFcn',{'mouse_down'}) % allows mouse action with right button (zoom for uicontrol display)
54
55%% enter input data
56set(handles.inputfile,'String',filename)% fill the input file name
57Field=nc2struct(filename,[]);% reads the  field structure, without the variables
58if isfield(Field,'Txt')
59    msgbox_uvmat('ERROR',['get_field/nc2struct/' Field.Txt])% display error message for input fiel reading
60    return
61end
62if ~isfield(Field,'ListVarName')
63    return
64end
65if ~exist('ParamIn','var')
66    ParamIn=[];
67end
68
69%% look at singletons and variables with a single dimension
70Field.Display=Field;
71Field.Check0D=zeros(size(Field.ListVarName));% =1 for arrays with a single value
72NbVar=numel(Field.VarDimName);
73for ilist=1:NbVar
74    if ischar(Field.VarDimName{ilist})
75        Field.VarDimName{ilist}={Field.VarDimName{ilist}}; %transform string into cell
76    end
77    NbDim=numel(Field.VarDimName{ilist});
78    check_singleton=false(1,NbDim);
79    for idim=1:NbDim
80        dim_index=strcmp(Field.VarDimName{ilist}{idim},Field.ListDimName);%index in the list of dimensions
81        check_singleton(idim)=isequal(Field.DimValue(dim_index),1);%check_singleton=1 for singleton
82    end
83    Field.Check0D(ilist)=(isequal(check_singleton,ones(1,NbDim)));% =1 if the variable reduces to a single value
84    if ~Field.Check0D(ilist)
85    Field.Display.VarDimName{ilist}=Field.VarDimName{ilist}(~check_singleton);% eliminate singletons in the list of variable dimensions
86    end
87    %Field.NbDim(ilist)=numel(Field.VarDimNameNonSingleton{ilist});%nbre of array dimensions after elimination of singletons
88end
89if numel(Field.VarAttribute)<NbVar% complement VarAttribute by blanjs if neded
90    Field.VarAttribute(numel(Field.VarAttribute)+1:NbVar)=cell(1,NbVar-numel(Field.VarAttribute));
91end
92Field.Display.ListVarName=Field.ListVarName(~Field.Check0D);
93Field.Display.VarAttribute=Field.VarAttribute(~Field.Check0D);
94Field.Display.VarDimName=Field.Display.VarDimName(~Field.Check0D);
95Field.Display.DimValue=Field.DimValue(Field.DimValue~=1);
96Field.Display.ListDimName=Field.ListDimName(Field.DimValue~=1);
97
98%% analyse the input field cells
99[CellInfo,NbDim,errormsg]=find_field_cells(Field.Display);
100if ~isempty(errormsg)
101    msgbox_uvmat('ERROR',['get_field / Field_input / find_field_cells: ' errormsg])
102    return
103end
104[Field.MaxDim,imax]=max(NbDim);
105
106%% set time mode
107ListSwitchVarIndexTime={'file index'};% default setting: the time is the file index
108% look at global attributes with numerical values
109check_numvalue=false;
110check_time=false;
111for ilist=1:numel(Field.ListGlobalAttribute)
112    Value=Field.(Field.ListGlobalAttribute{ilist});
113    check_numvalue(ilist)=isnumeric(Value);
114    check_time(ilist)=~isempty(find(regexp(Field.ListGlobalAttribute{ilist},'Time'),1));
115end
116Field.Display.ListGlobalAttribute=Field.ListGlobalAttribute(check_numvalue);% select the attributes with float numerical value
117if ~isempty(Field.Display.ListGlobalAttribute)
118    ListSwitchVarIndexTime=[ListSwitchVarIndexTime; {'attribute'}];% the time can be chosen as a global attribute
119end
120nboption=numel(ListSwitchVarIndexTime);
121if Field.MaxDim>=2
122    ListSwitchVarIndexTime=[ListSwitchVarIndexTime;{'variable'};{'dim index'}];% the time can be chosen as a dim index
123end
124if ~isempty(find(check_time, 1))
125    set(handles.SwitchVarIndexTime,'Value',2);
126else
127    set(handles.SwitchVarIndexTime,'Value',1);
128end
129set(handles.SwitchVarIndexTime,'String',ListSwitchVarIndexTime)
130set(handles.get_field,'UserData',Field);% record the finput field structure
131SwitchVarIndexTime_Callback([], [], handles)
132
133%% set vector menu (priority) if detected or scalar menu for space dim >=2, or usual (x,y) plot for 1D fields
134set(handles.vector_x,'String',Field.Display.ListVarName)% fill the menu of x vector components
135set(handles.vector_y,'String',Field.Display.ListVarName)% fill the menu of y vector components
136set(handles.vector_z,'String',[{''} Field.ListVarName])% fill the menu of y vector components
137set(handles.vec_color,'String',[{''} Field.ListVarName])% fill the menu of y vector components
138set(handles.scalar,'Value',1)% fill the menu of y vector components
139set(handles.scalar,'String',Field.ListVarName)% fill the menu of y vector components
140set(handles.ordinate,'Value',1)% fill the menu of y vector components
141set(handles.ordinate,'String',Field.ListVarName)% fill the menu of y vector components
142if isfield(Field,'Conventions')&& strcmp(Field.Conventions,'uvmat/civdata')
143        set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors';'civdata...'})
144else
145     set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors'})
146end
147if Field.MaxDim>=2 % case of 2D (or 3D) fields
148    if isfield(CellInfo{imax},'VarIndex_vector_x') &&  isfield(CellInfo{imax},'VarIndex_vector_y')
149        set(handles.FieldOption,'Value',3)% set vector selection option
150        set(handles.vector_x,'Value',CellInfo{imax}.VarIndex_vector_x(1))
151        set(handles.vector_y,'Value',CellInfo{imax}.VarIndex_vector_y(1))
152        set(handles.FieldOption,'Value',3)
153    else
154        set(handles.FieldOption,'Value',2)
155    end
156
157else % case of 1D fields
158    set(handles.FieldOption,'Value',1)
159end
160
161%% fill the general list of dimensions, variables, attributes
162if isfield(Field,'ListDimName')&&~isempty(Field.ListDimName)
163    Tabcell(:,1)=Field.ListDimName;
164    for iline=1:length(Field.ListDimName)
165        Tabcell{iline,2}=num2str(Field.DimValue(iline));
166    end
167    Tabchar=cell2tab(Tabcell,' = ');
168    set(handles.dimensions,'String',Tabchar)
169end
170set(handles.variables,'Value',1)
171set(handles.variables,'String',[{'*'} Field.ListVarName])
172variables_Callback(handles.variables,[], handles)% list the global attributes
173
174%% fill menus for coordinates and time
175FieldOption_Callback(handles.variables,[], handles)% list the global attributes
176%     if isfield(CellInfo{imax},'VarIndex_coord_x')&&  isfield(CellInfo{imax},'VarIndex_coord_y')
177%         set(handles.Coord_x,'Value',CellInfo{imax}.VarIndex_coord_x(1))
178%         set(handles.Coord_y,'Value',CellInfo{imax}.VarIndex_coord_y(1))
179%     end
180
181%% Make choices in menus from input
182% if exist('ParamIn','var')&&~isempty(ParamIn)
183%     fill_GUI(ParamIn,handles.get_field);
184% end
185%FieldOption_Callback([],[],handles)
186
187
188%% put the GUI on the lower right of the sceen
189set(hObject,'Unit','pixel')
190pos_view_field=get(hObject,'Position');
191set(0,'Unit','pixels')
192ScreenSize=get(0,'ScreenSize');
193pos_view_field(1)=ScreenSize(1)+ScreenSize(3)-pos_view_field(3);
194pos_view_field(2)=ScreenSize(2);
195set(hObject,'Position',pos_view_field)
196set(handles.get_field,'WindowStyle','modal')% Make the GUI modal
197drawnow
198uiwait(handles.get_field);
199
200return
201
202SwitchVarIndexTime_Callback([],[], handles)
203
204%% set z coordinate menu if relevant
205if Field.MaxDim>=3
206    set(handles.vector_z,'Visible','on')
207    set(handles.vector_z,'String',Field.ListVarName(~Field.Check0D))
208    set(handles.Coord_z,'Visible','on')
209    set(handles.SwitchVarIndexZ,'Visible','on')
210    set(handles.Z_title,'Visible','on')
211else
212    set(handles.vector_z,'Visible','off')
213    set(handles.Coord_z,'Visible','off')
214    set(handles.Z_title,'Visible','off')
215end
216
217
218
219%------------------------------------------------------------------------
220
221%------------------------------------------------------------------------
222% --- Outputs from this function are returned to the command line.
223function varargout = get_field_OutputFcn(hObject, eventdata, handles)
224%------------------------------------------------------------------------
225varargout{1} = handles.output;
226delete(handles.get_field)
227
228%------------------------------------------------------------------------
229% --- Executes when user attempts to close get_field.
230%------------------------------------------------------------------------
231function get_field_CloseRequestFcn(hObject, eventdata, handles)
232
233if isequal(get(handles.get_field, 'waitstatus'), 'waiting')
234    % The GUI is still in UIWAIT, us UIRESUME
235    uiresume(handles.get_field);
236else
237    % The GUI is no longer waiting, just close it
238    delete(handles.get_field);
239end
240
241%---------------------------------------------------------
242% --- Executes on button press in OK.
243%------------------------------------------------------------------------
244function OK_Callback(hObject, eventdata, handles)
245
246handles.output=read_GUI(handles.get_field);
247guidata(hObject, handles);% Update handles structure
248uiresume(handles.get_field);
249drawnow
250
251% -----------------------------------------------------------------------
252% --- Activated by selection in the list of variables
253function variables_Callback(hObject, eventdata, handles)
254% -----------------------------------------------------------------------
255Tabchar={''};%default
256Tabcell=[];
257hselect_field=get(handles.variables,'parent');
258Field=get(handles.get_field,'UserData');
259index=get(handles.variables,'Value');%index in the list 'variables'
260
261%% list global TimeAttribute names and values if index=1 (blank TimeVariable display) is selected
262if isequal(index,1)
263    set(handles.attributes_txt,'String','global attributes')
264    if isfield(Field,'ListGlobalAttribute') && ~isempty(Field.ListGlobalAttribute)
265        for iline=1:length(Field.ListGlobalAttribute)
266            Tabcell{iline,1}=Field.ListGlobalAttribute{iline};   
267            if isfield(Field, Field.ListGlobalAttribute{iline})
268                val=Field.(Field.ListGlobalAttribute{iline});
269                if ischar(val);% attribute value is char string
270                    Tabcell{iline,2}=val;
271                elseif size(val,1)==1 %attribute value is a number or matlab vector
272                    Tabcell{iline,2}=num2str(val);
273                end
274            end
275        end
276        Tabchar=cell2tab(Tabcell,'=');
277    end
278%% list Attribute names and values associated to the Variable # index-1   
279else
280    list_var=get(handles.variables,'String');
281    var_select=list_var{index};
282    set(handles.attributes_txt,'String', ['attributes of ' var_select])
283    if isfield(Field,'VarAttribute')&& length(Field.VarAttribute)>=index-1
284%         nbline=0;
285        VarAttr=Field.VarAttribute{index-1};
286        if isstruct(VarAttr)
287            attr_list=fieldnames(VarAttr);
288            for iline=1:length(attr_list)
289                Tabcell{iline,1}=attr_list{iline};
290                eval(['val=VarAttr.' attr_list{iline} ';'])
291                if ischar(val);
292                    Tabcell{iline,2}=val;
293                else
294                     Tabcell{iline,2}=num2str(val);
295                end
296            end
297        end
298    end
299
300end
301if ~isempty(Tabcell)
302    Tabchar=cell2tab(Tabcell,'=');
303%     Tabchar=[{''};Tabchar];
304end
305set(handles.attributes,'Value',1);% select the first item
306set(handles.attributes,'String',Tabchar);
307
308%% update dimensions;
309if isfield(Field,'ListDimName')
310    Tabdim={};%default
311    if isequal(index,1)%list all dimensions
312        dim_indices=1:length(Field.ListDimName);
313        set(handles.dimensions_txt,'String', 'dimensions')
314    else
315        DimCell=Field.VarDimName{index-1};
316        if ischar(DimCell)
317            DimCell={DimCell};
318        end   
319        dim_indices=[];
320        for idim=1:length(DimCell)
321            dim_index=strcmp(DimCell{idim},Field.ListDimName);%vector with size of Field.ListDimName, =0
322            dim_index=find(dim_index,1);
323            dim_indices=[dim_indices dim_index];
324        end
325        set(handles.dimensions_txt,'String', ['dimensions of ' var_select])
326    end
327    for iline=1:length(dim_indices)
328        Tabdim{iline,1}=Field.ListDimName{dim_indices(iline)};
329        Tabdim{iline,2}=num2str(Field.DimValue(dim_indices(iline)));
330    end
331    Tabchar=cell2tab(Tabdim,' = ');
332    Tabchar=[{''} ;Tabchar];
333    set(handles.dimensions,'Value',1)
334    set(handles.dimensions,'String',Tabchar) 
335end 
336
337%------------------------------------------------------------------------
338% --- Executes on selection change in FieldOption.
339%------------------------------------------------------------------------
340function FieldOption_Callback(hObject, eventdata, handles)
341
342FieldList=get(handles.FieldOption,'String');
343FieldOption=FieldList{get(handles.FieldOption,'Value')};
344switch FieldOption
345    case '1D plot'
346        set(handles.Coordinates,'Visible','on')
347        set(handles.PanelOrdinate,'Visible','on')
348        pos=get(handles.PanelOrdinate,'Position');
349        pos(1)=2;
350        pos_coord=get(handles.Coordinates,'Position');
351        pos(2)=pos_coord(2)-pos(4)-2;
352        set(handles.PanelOrdinate,'Position',pos)
353        set(handles.PanelScalar,'Visible','off')
354        set(handles.PanelVectors,'Visible','off')
355        set(handles.Coord_y,'Visible','off')
356        set(handles.Y_title,'Visible','off')
357        set(handles.Coord_z,'Visible','off')
358        set(handles.Z_title,'Visible','off')
359        ordinate_Callback(hObject, eventdata, handles)
360    case 'scalar'
361        set(handles.Coordinates,'Visible','on')
362        set(handles.PanelOrdinate,'Visible','off')
363        set(handles.PanelScalar,'Visible','on')
364        set(handles.PanelVectors,'Visible','off')
365        pos=get(handles.PanelScalar,'Position');
366        pos(1)=2;
367        pos_coord=get(handles.Coordinates,'Position');
368        pos(2)=pos_coord(2)-pos(4)-2;
369        set(handles.PanelScalar,'Position',pos)
370        set(handles.Coord_y,'Visible','on')
371        set(handles.Y_title,'Visible','on')
372        scalar_Callback(hObject, eventdata, handles)
373    case 'vectors'
374        set(handles.Coordinates,'Visible','on')
375        set(handles.PanelOrdinate,'Visible','off')
376        set(handles.PanelScalar,'Visible','off')
377        set(handles.PanelVectors,'Visible','on')
378        pos=get(handles.PanelVectors,'Position');
379        pos(1)=2;
380        pos_coord=get(handles.Coordinates,'Position');
381        pos(2)=pos_coord(2)-pos(4)-2;
382        set(handles.PanelVectors,'Position',pos)
383        set(handles.Coord_y,'Visible','on')
384        set(handles.Y_title,'Visible','on')     
385        vector_Callback(handles)
386    case 'civdata...'
387        set(handles.PanelOrdinate,'Visible','off')
388        set(handles.PanelScalar,'Visible','off')
389        set(handles.PanelVectors,'Visible','off')
390        set(handles.Coordinates,'Visible','off')
391end
392
393%------------------------------------------------------------------------
394function ordinate_Callback(hObject, eventdata, handles)
395%------------------------------------------------------------------------
396Field=get(handles.get_field,'UserData');
397y_index=get(handles.ordinate,'Value');
398y_menu=get(handles.ordinate,'String');
399YName=y_menu{y_index};
400
401%% set list of possible coordinates
402test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
403test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
404ListCoord={''};
405dim_var=Field.Display.VarDimName{y_index};%list of dimensions of the selected variable
406
407for ilist=1:numel(Field.Display.VarDimName)
408    dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
409    if isequal(dimnames,dim_var)
410        test_component(ilist)=1;
411    elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
412        test_coord(ilist)=1;
413    end
414end
415var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
416var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
417ListCoord=Field.Display.ListVarName([var_component var_coord]);
418
419%% set default coord selection
420if numel(find(test_coord))>3
421    set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
422end
423if numel(var_component)<2
424    if numel(test_coord)<2
425        ListCoord={''};
426    else
427        set(handles.Coord_x,'Value',2)
428        set(handles.Coord_y,'Value',1)
429    end
430else
431    coord_val=1;
432    for ilist=1:numel(var_component)
433        ivar=var_component(ilist);
434        if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
435            Role=Field.Display.VarAttribute{ivar}.Role;
436            if strcmp(Role,'coord_x')
437                coord_val=ilist;
438            end
439        end
440    end
441    set(handles.Coord_x,'Value',coord_val)
442end
443set(handles.Coord_x,'String',ListCoord)
444
445
446%% set list of time coordinates
447menu=get(handles.SwitchVarIndexTime,'String');
448TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
449switch TimeOption
450    case 'variable'
451        if numel(find(test_coord))<3
452            ListTime={''};
453        else
454            ListTime=Field.Display.ListVarName(find(test_coord,end));
455        end
456        set(handles.TimeName,'Value',1)
457        set(handles.TimeName,'String',ListTime)
458    case 'dim index'
459        if numel(find(test_coord))<3
460            ListTime={''};
461        else
462            ListTime=Field.Display.VarDimName{find(test_coord,end)};
463        end
464        set(handles.TimeName,'Value',1)
465        set(handles.TimeName,'String',ListTime)
466end 
467update_field(handles,YName)
468
469%------------------------------------------------------------------------
470% --- Executes on selection change in scalar menu.
471%------------------------------------------------------------------------
472function scalar_Callback(hObject, eventdata, handles)
473
474Field=get(handles.get_field,'UserData');
475scalar_index=get(handles.scalar,'Value');
476scalar_menu=get(handles.scalar,'String');
477ScalarName=scalar_menu{scalar_index};
478
479%% set list of possible coordinates
480test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
481test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
482ListCoord={''};
483dim_var=Field.Display.VarDimName{scalar_index};%list of dimensions of the selected variable
484
485for ilist=1:numel(Field.Display.VarDimName)
486    dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
487    if isequal(dimnames,dim_var)
488        test_component(ilist)=1;
489    elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
490        test_coord(ilist)=1;
491    end
492end
493var_component=find(test_component);% list of variable indices elligible as unstructured coordiantes
494var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
495ListCoord=Field.Display.ListVarName([var_component var_coord]);
496
497%% set default coord selection
498if numel(find(test_coord))>3
499    set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
500end
501if numel(var_component)<2
502    if numel(test_coord)<2
503        ListCoord={''};
504    else
505        set(handles.Coord_x,'Value',2)
506        set(handles.Coord_y,'Value',1)
507    end
508else
509    coord_val=[1 2];
510    for ilist=1:numel(var_component)
511        ivar=var_component(ilist);
512        if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
513            Role=Field.Display.VarAttribute{ivar}.Role;
514            if strcmp(Role,'coord_x')
515                coord_val(1)=ilist;
516            elseif strcmp(Role,'coord_y')
517                coord_val(2)=ilist;
518            end
519        end
520    end
521    set(handles.Coord_x,'Value',coord_val(1))
522    set(handles.Coord_y,'Value',coord_val(2))
523end
524
525set(handles.Coord_y,'String',ListCoord)
526set(handles.Coord_x,'String',ListCoord)
527
528
529%% set list of time coordinates
530menu=get(handles.SwitchVarIndexTime,'String');
531TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
532switch TimeOption
533    case 'variable'
534        if numel(find(test_coord))<3
535            ListTime={''};
536        else
537            ListTime=Field.Display.ListVarName(find(test_coord,end));
538        end
539        set(handles.TimeName,'Value',1)
540        set(handles.TimeName,'String',ListTime)
541    case 'dim index'
542        if numel(find(test_coord))<3
543            ListTime={''};
544        else
545            ListTime=Field.Display.VarDimName{find(test_coord,end)};
546        end
547        set(handles.TimeName,'Value',1)
548        set(handles.TimeName,'String',ListTime)
549end 
550update_field(handles,ScalarName)
551
552% --- Executes on button press in check_rgb.
553function check_rgb_Callback(hObject, eventdata, handles)
554
555
556%------------------------------------------------------------------------
557% --- Executes on selection change in vector_x.
558function vector_x_Callback(hObject, eventdata, handles)
559%------------------------------------------------------------------------
560vector_x_menu=get(handles.vector_x,'String');
561vector_x_index=get(handles.vector_x,'Value');
562vector_x=vector_x_menu{vector_x_index};
563vector_Callback(handles)
564update_field(handles,vector_x)
565
566%------------------------------------------------------------------------
567% --- Executes on selection change in vector_x.
568function vector_y_Callback(hObject, eventdata, handles)
569%------------------------------------------------------------------------
570vector_y_menu=get(handles.vector_x,'String');
571vector_y_index=get(handles.vector_x,'Value');
572vector_y=vector_y_menu{vector_y_index};
573vector_Callback(handles)
574update_field(handles,vector_y)
575
576%------------------------------------------------------------------------
577% --- Executes on selection change in vector_z.
578function vector_z_Callback(hObject, eventdata, handles)
579%------------------------------------------------------------------------
580vector_z_menu=get(handles.vector_z,'String');
581vector_z_index=get(handles.vector_z,'Value');
582vector_z=vector_z_menu{vector_z_index};
583vector_Callback(handles)
584update_field(handles,vector_z)
585
586%------------------------------------------------------------------------
587% --- Executes on selection change in vec_color.
588function vec_color_Callback(hObject, eventdata, handles)
589%------------------------------------------------------------------------
590index=get(handles.vec_color,'Value');
591string=get(handles.vec_color,'String');
592VarName=string{index};
593vector_Callback(handles)
594update_field(handles,VarName)
595
596%------------------------------------------------------------------------
597% --- Executes on selection change in vector_x or vector_y
598function vector_Callback( handles)
599%------------------------------------------------------------------------
600Field=get(handles.get_field,'UserData');
601vector_x_index=get(handles.vector_x,'Value');
602vector_y_index=get(handles.vector_y,'Value');
603vec_color_index=get(handles.vec_color,'Value');
604
605%% set list of possible coordinates
606test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
607test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
608check_consistent=1;%check that the selected vector components (and possibly color var) have the same dimensiosn
609ListCoord={''};
610dim_var=Field.Display.VarDimName{vector_x_index};%list of dimensions of the selected variable
611if ~isequal(dim_var,Field.Display.VarDimName{vector_y_index})
612    check_consistent=0;
613elseif vec_color_index~=1 && ~isequal(dim_var,Field.Display.VarDimName{vec_color_index})
614    check_consistent=0;
615end
616if check_consistent
617    for ilist=1:numel(Field.Display.VarDimName)
618        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
619        if isequal(dimnames,dim_var)
620            test_component(ilist)=1;
621        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
622            test_coord(ilist)=1;
623        end
624    end
625    var_component=find(test_component);% list of variable indices elligible as unstructured coordiantes
626    var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
627    ListCoord=Field.Display.ListVarName([var_component var_coord]);
628   
629    %% set default coord selection
630    if numel(find(test_coord))>3
631        set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
632    end
633    if numel(var_component)<2
634        if numel(test_coord)<2
635            ListCoord={''};
636        else
637            set(handles.Coord_x,'Value',2)
638            set(handles.Coord_y,'Value',1)
639        end
640    else
641        coord_val=[1 2];
642        for ilist=1:numel(var_component)
643            ivar=var_component(ilist);
644            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
645                Role=Field.Display.VarAttribute{ivar}.Role;
646                if strcmp(Role,'coord_x')
647                    coord_val(1)=ilist;
648                elseif strcmp(Role,'coord_y')
649                    coord_val(2)=ilist;
650                end
651            end
652        end
653        set(handles.Coord_x,'Value',coord_val(1))
654        set(handles.Coord_y,'Value',coord_val(2))
655    end
656end
657set(handles.Coord_y,'String',ListCoord)
658set(handles.Coord_x,'String',ListCoord)
659
660
661%% set list of time coordinates
662menu=get(handles.SwitchVarIndexTime,'String');
663TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
664switch TimeOption
665    case 'variable'
666        if numel(find(test_coord))<3
667            ListTime={''};
668        else
669            ListTime=Field.Display.ListVarName(find(test_coord,end));
670        end
671        set(handles.TimeName,'Value',1)
672        set(handles.TimeName,'String',ListTime)
673    case 'dim index'
674        if numel(find(test_coord))<3
675            ListTime={''};
676        else
677            ListTime=Field.Display.VarDimName{find(test_coord,end)};
678        end
679        set(handles.TimeName,'Value',1)
680        set(handles.TimeName,'String',ListTime)
681end 
682
683%------------------------------------------------------------------------
684% --- Executes on selection change in SwitchVarIndexX.
685%------------------------------------------------------------------------
686function SwitchVarIndexX_Callback(hObject, eventdata, handles)
687
688%------------------------------------------------------------------------
689% --- Executes on selection change in Coord_x.
690function Coord_x_Callback(hObject, eventdata, handles)
691%------------------------------------------------------------------------
692index=get(handles.Coord_x,'Value');
693string=get(handles.Coord_x,'String');
694VarName=string{index};
695update_field(handles,VarName)
696
697%------------------------------------------------------------------------
698% --- Executes on selection change in Coord_y.
699function Coord_y_Callback(hObject, eventdata, handles)
700%------------------------------------------------------------------------
701index=get(handles.Coord_y,'Value');
702string=get(handles.Coord_y,'String');
703VarName=string{index};
704update_field(handles,VarName)
705
706%------------------------------------------------------------------------
707% --- Executes on selection change in Coord_z.
708function Coord_z_Callback(hObject, eventdata, handles)
709%------------------------------------------------------------------------
710index=get(handles.Coord_z,'Value');
711string=get(handles.Coord_z,'String');
712VarName=string{index};
713update_field(handles,VarName)
714
715%------------------------------------------------------------------------
716% --- Executes on selection change in SwitchVarIndexTime.
717%------------------------------------------------------------------------
718function SwitchVarIndexTime_Callback(hObject, eventdata, handles)
719Field=get(handles.get_field,'UserData');
720menu=get(handles.SwitchVarIndexTime,'String');
721option=menu{get(handles.SwitchVarIndexTime,'Value')};
722
723switch option
724    case 'file index'
725        set(handles.TimeName, 'Visible','off')% the time is taken as the file index
726    case 'attribute'
727        set(handles.TimeName, 'Visible','on')% timeName menu represents the available attributes
728        time_index=[];
729        PreviousList=get(handles.TimeName, 'String');
730        index=[];
731        if ~isempty(PreviousList)
732            PreviousAttr=PreviousList{get(handles.TimeName, 'Value')};
733            index=find(strcmp(PreviousAttr,Field.Display.ListGlobalAttributes));
734        end
735        if isempty(index)
736            time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')));% index of the attributes containing the string 'Time'
737        end
738        if ~isempty(time_index)
739            set(handles.TimeName,'Value',time_index(1))
740        else
741            set(handles.TimeName,'Value',1)
742        end
743        set(handles.TimeName, 'String',Field.Display.ListGlobalAttribute)
744    case 'variable'% TimeName menu represents the available variables
745        set(handles.TimeName, 'Visible','on')
746        TimeVarName=Field.Display.SingleVarName;% slist of variables with a single dimension (candidate for time)
747        List=get(handles.TimeName,'String');
748        option=List{get(handles.TimeName,'Value')};
749        ind=find(strcmp(option,TimeVarName));
750        if isempty(ind)
751            set(handles.TimeName, 'Value',1);
752        else
753            set(handles.TimeName, 'Value',ind);
754        end
755        set(handles.TimeName, 'String',TimeVarName)
756    case 'dim index'% TimeName menu represents the available dimensions
757        set(handles.TimeName, 'Visible','on')
758        TimeVarName=Field.Display.SingleDimName;
759        List=get(handles.TimeName,'String');
760        option=List{get(handles.TimeName,'Value')};
761        ind=find(strcmp(option,TimeVarName));
762        if isempty(ind)
763            set(handles.TimeName, 'Value',1);
764        else
765            set(handles.TimeName, 'Value',ind);
766        end
767        set(handles.TimeName, 'String',TimeVarName)
768end
769
770%-----------------------------------------------------------------------
771function update_field(handles,VarName)
772%-----------------------------------------------------------------------
773Field=get(handles.get_field,'UserData');
774index=name2index(VarName,Field.ListVarName);
775if ~isempty(index)
776    set(handles.variables,'Value',index+1)
777    variables_Callback(handles.variables, [], handles)
778end
779
780%-------------------------------------------------
781% give index numbers of the strings str in the list ListvarName
782function VarIndex_y=name2index(cell_str,ListVarName)
783VarIndex_y=[];
784if ischar(cell_str)
785    for ivar=1:length(ListVarName)
786        varlist=ListVarName{ivar};
787        if isequal(varlist,cell_str)
788            VarIndex_y= ivar;
789            break
790        end
791    end
792elseif iscell(cell_str)
793    for isel=1:length(cell_str)
794        varsel=cell_str{isel};
795        for ivar=1:length(ListVarName)
796            varlist=ListVarName{ivar};
797            if isequal(varlist,varsel)
798                VarIndex_y=[VarIndex_y ivar];
799            end
800        end
801    end
802end
Note: See TracBrowser for help on using the repository browser.