source: trunk/src/get_field.m @ 669

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

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