source: trunk/src/get_field.m @ 740

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

several bugs repaired

File size: 39.0 KB
Line 
1%'get_field': display variables and attributes from a Netcdf file, and OK selected fields
2%------------------------------------------------------------------------
3% GetFieldData=get_field(FileName,ParamIn)
4% 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
17%
18%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
19%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
20%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
21%     This file is part of the toolbox UVMAT.
22%
23%     UVMAT is free software; you can redistribute it and/or modify
24%     it under the terms of the GNU General Public License as published by
25%     the Free Software Foundation; either version 2 of the License, or
26%     (at your option) any later version.
27%
28%     UVMAT is distributed in the hope that it will be useful,
29%     but WITHOUT ANY WARRANTY; without even the implied warranty of
30%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
32%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
33
34function varargout = get_field(varargin)
35
36% Last Modified by GUIDE v2.5 23-Oct-2013 23:44:17
37
38% Begin initialization code - DO NOT EDIT
39gui_Singleton = 1;
40gui_State = struct('gui_Name',       mfilename, ...
41                   'gui_Singleton',  gui_Singleton, ...
42                   'gui_OpeningFcn', @get_field_OpeningFcn, ...
43                   'gui_OutputFcn',  @get_field_OutputFcn, ...
44                   'gui_LayoutFcn',  [] , ...
45                   'gui_Callback',   []);
46if nargin && ischar(varargin{1})&& ~isempty(regexp(varargin{1},'_Callback','once'))
47    gui_State.gui_Callback = str2func(varargin{1});
48end
49
50if nargout
51    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
52else
53    gui_mainfcn(gui_State, varargin{:});
54end
55% End initialization code - DO NOT EDIT
56     
57%------------------------------------------------------------------------
58% --- Executes just before get_field is made visible.
59%------------------------------------------------------------------------
60function get_field_OpeningFcn(hObject, eventdata, handles,filename,ParamIn)
61
62%% GUI settings
63handles.output = 'Cancel';
64guidata(hObject, handles);
65set(hObject,'WindowButtonDownFcn',{'mouse_down'}) % allows mouse action with right button (zoom for uicontrol display)
66
67%% enter input data
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
77if isfield(Field,'Txt')
78    msgbox_uvmat('ERROR',['get_field/nc2struct/' Field.Txt])% display error message for input file reading
79    return
80end
81if ~isfield(Field,'ListVarName')
82    return
83end
84
85%% look at singletons and variables with a single dimension
86Field.Display=Field;
87Field.Check0D=zeros(size(Field.ListVarName));% =1 for arrays with a single value
88NbVar=numel(Field.VarDimName);
89for ilist=1:NbVar
90    if ischar(Field.VarDimName{ilist})
91        Field.VarDimName{ilist}={Field.VarDimName{ilist}}; %transform string into cell
92    end
93    NbDim=numel(Field.VarDimName{ilist});
94    check_singleton=false(1,NbDim);
95    for idim=1:NbDim
96        dim_index=strcmp(Field.VarDimName{ilist}{idim},Field.ListDimName);%index in the list of dimensions
97        check_singleton(idim)=isequal(Field.DimValue(dim_index),1);%check_singleton=1 for singleton
98    end
99    Field.Check0D(ilist)=(isequal(check_singleton,ones(1,NbDim)))||(~isequal(Field.VarType(ilist),4)&&~isequal(Field.VarType(ilist),5)&&~isequal(Field.VarType(ilist),6));% =1 if the variable reduces to a single value
100    if ~Field.Check0D(ilist)
101    Field.Display.VarDimName{ilist}=Field.VarDimName{ilist}(~check_singleton);% eliminate singletons in the list of variable dimensions
102    end
103    %Field.NbDim(ilist)=numel(Field.VarDimNameNonSingleton{ilist});%nbre of array dimensions after elimination of singletons
104end
105if ~isfield(Field,'VarAttribute')
106    Field.VarAttribute={};
107end
108if numel(Field.VarAttribute)<NbVar% complement VarAttribute by blanjs if neded
109    Field.VarAttribute(numel(Field.VarAttribute)+1:NbVar)=cell(1,NbVar-numel(Field.VarAttribute));
110end
111% Field.Display = list of variables and corresponding properties obtained after removal of singletons
112Field.Display.ListVarName=Field.ListVarName(~Field.Check0D);
113Field.Display.VarAttribute=Field.VarAttribute(~Field.Check0D);
114Field.Display.VarDimName=Field.Display.VarDimName(~Field.Check0D);
115Field.Display.DimValue=Field.DimValue(Field.DimValue~=1);
116Field.Display.ListDimName=Field.ListDimName(Field.DimValue~=1);
117
118%% analyse the input field cells
119[CellInfo,NbDim,errormsg]=find_field_cells(Field.Display);
120if ~isempty(errormsg)
121    msgbox_uvmat('ERROR',['get_field / Field_input / find_field_cells: ' errormsg])
122    return
123end
124[Field.MaxDim,imax]=max(NbDim);
125
126%% set time mode
127ListSwitchVarIndexTime={'file index'};% default setting: the time is the file index
128% look at global attributes with numerical values
129check_numvalue=false(1,numel(Field.ListGlobalAttribute));
130for ilist=1:numel(Field.ListGlobalAttribute)
131    Value=Field.(Field.ListGlobalAttribute{ilist});
132    check_numvalue(ilist)=isnumeric(Value);
133end
134Field.Display.ListGlobalAttribute=Field.ListGlobalAttribute(check_numvalue);% select the attributes with float numerical value
135if ~isempty(Field.Display.ListGlobalAttribute)
136    ListSwitchVarIndexTime=[ListSwitchVarIndexTime; {'attribute'}];% the time can be chosen as a global attribute
137end
138if Field.MaxDim>=2
139    ListSwitchVarIndexTime=[ListSwitchVarIndexTime;{'variable'};{'matrix index'}];% the time can be chosen as a dim index
140end
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);% look for global attribute containing name 'Time'
147end
148if ~isempty(time_index)
149    set(handles.SwitchVarIndexTime,'Value',2);
150    set(handles.TimeName,'UserData',time_index)
151else
152    set(handles.SwitchVarIndexTime,'Value',1);
153end
154set(handles.SwitchVarIndexTime,'String',ListSwitchVarIndexTime)
155set(handles.get_field,'UserData',Field);% record the finput field structure
156SwitchVarIndexTime_Callback([], [], handles)
157
158%% set vector menu (priority) if detected or scalar menu for space dim >=2, or usual (x,y) plot for 1D fields
159set(handles.vector_x,'String',Field.Display.ListVarName)% fill the menu of x vector components
160set(handles.vector_y,'String',Field.Display.ListVarName)% fill the menu of y vector components
161set(handles.vector_z,'String',[{''} Field.Display.ListVarName])% fill the menu of y vector components
162set(handles.vec_color,'String',[{''} Field.Display.ListVarName])% fill the menu of y vector components
163set(handles.scalar,'Value',1)% fill the menu of y vector components
164set(handles.scalar,'String',Field.Display.ListVarName)% fill the menu of y vector components
165set(handles.ordinate,'Value',1)% fill the menu of y vector components
166set(handles.ordinate,'String',Field.Display.ListVarName)% fill the menu of y vector components
167if isfield(Field,'Conventions')&& strcmp(Field.Conventions,'uvmat/civdata')
168    set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors';'civdata...'})
169else
170    set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors'})
171end
172if Field.MaxDim>=2 % case of 2D (or 3D) fields
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
189        set(handles.FieldOption,'Value',3)% set vector selection option
190    else
191        set(handles.FieldOption,'Value',2)% set scalar selection option
192    end
193else % case of 1D fields
194    set(handles.FieldOption,'Value',1)
195end
196
197%% fill the general list of dimensions, variables, attributes
198if isfield(Field,'ListDimName')&&~isempty(Field.ListDimName)
199    Tabcell(:,1)=Field.ListDimName;
200    for iline=1:length(Field.ListDimName)
201        Tabcell{iline,2}=num2str(Field.DimValue(iline));
202    end
203    Tabchar=cell2tab(Tabcell,' = ');
204    set(handles.dimensions,'String',Tabchar)
205end
206set(handles.variables,'Value',1)
207set(handles.variables,'String',[{'*'} Field.ListVarName])
208variables_Callback(handles.variables,[], handles)% list the global attributes
209
210%% fill menus for coordinates and time
211FieldOption_Callback(handles.variables,[], handles)% list the global attributes
212
213%% Make choices of coordinates from input
214if isfield(CellInfo{imax},'CoordIndex')
215    CoordIndex=CellInfo{imax}.CoordIndex;
216    if numel(CoordIndex)==2
217        YName=Field.ListVarName{CoordIndex(1)};
218        XName=Field.ListVarName{CoordIndex(2)};
219        ListCoord=get(handles.Coord_x,'String');
220        XIndex=find(strcmp(XName,ListCoord));
221        if ~isempty(XIndex)
222            set(handles.Coord_x,'Value',XIndex)
223        end
224        YIndex=find(strcmp(YName,ListCoord));
225        if ~isempty(YIndex)
226            set(handles.Coord_y,'Value',YIndex)
227        end
228    end
229end
230
231%% put the GUI on the lower right of the sceen
232set(hObject,'Unit','pixel')
233pos_view_field=get(hObject,'Position');
234set(0,'Unit','pixels')
235ScreenSize=get(0,'ScreenSize');
236pos_view_field(1)=ScreenSize(1)+ScreenSize(3)-pos_view_field(3);
237pos_view_field(2)=ScreenSize(2);
238set(hObject,'Position',pos_view_field)
239set(handles.get_field,'WindowStyle','modal')% Make the GUI modal
240drawnow
241uiwait(handles.get_field);
242
243return
244
245%% set z coordinate menu if relevant
246if Field.MaxDim>=3
247    set(handles.vector_z,'Visible','on')
248    set(handles.vector_z,'String',Field.ListVarName(~Field.Check0D))
249    set(handles.Coord_z,'Visible','on')
250    set(handles.SwitchVarIndexZ,'Visible','on')
251    set(handles.Z_title,'Visible','on')
252else
253    set(handles.vector_z,'Visible','off')
254    set(handles.Coord_z,'Visible','off')
255    set(handles.Z_title,'Visible','off')
256end
257
258%% make selections according to ParamIn
259if isfield(ParamIn,'vector_x') && isfield(ParamIn,'vector_y')
260   
261end
262
263%------------------------------------------------------------------------
264% --- Outputs from this function are returned to the command line.
265%------------------------------------------------------------------------
266function varargout = get_field_OutputFcn(hObject, eventdata, handles)
267
268varargout{1} = handles.output;
269delete(handles.get_field)
270
271%------------------------------------------------------------------------
272% --- Executes when user attempts to close get_field.
273%------------------------------------------------------------------------
274function get_field_CloseRequestFcn(hObject, eventdata, handles)
275
276if isequal(get(handles.get_field, 'waitstatus'), 'waiting')
277    % The GUI is still in UIWAIT, us UIRESUME
278    uiresume(handles.get_field);
279else
280    % The GUI is no longer waiting, just close it
281    delete(handles.get_field);
282end
283
284%------------------------------------------------------------------------
285% --- Executes on button press in OK.
286%------------------------------------------------------------------------
287function OK_Callback(hObject, eventdata, handles)
288
289handles.output=read_GUI(handles.get_field);
290guidata(hObject, handles);% Update handles structure
291uiresume(handles.get_field);
292drawnow
293
294% -----------------------------------------------------------------------
295% --- Activated by selection in the list of variables
296% ----------------------------------------------------------------------
297function variables_Callback(hObject, eventdata, handles)
298
299Tabchar={''};%default
300Tabcell=[];
301hselect_field=get(handles.variables,'parent');
302Field=get(handles.get_field,'UserData');
303index=get(handles.variables,'Value');%index in the list 'variables'
304
305%% list global TimeAttribute names and values if index=1 (blank TimeVariable display) is selected
306if isequal(index,1)
307    set(handles.attributes_txt,'String','global attributes')
308    if isfield(Field,'ListGlobalAttribute') && ~isempty(Field.ListGlobalAttribute)
309        for iline=1:length(Field.ListGlobalAttribute)
310            Tabcell{iline,1}=Field.ListGlobalAttribute{iline};
311            if isfield(Field, Field.ListGlobalAttribute{iline})
312                val=Field.(Field.ListGlobalAttribute{iline});
313                if ischar(val);% attribute value is char string
314                    Tabcell{iline,2}=val;
315                elseif size(val,1)==1 %attribute value is a number or matlab vector
316                    Tabcell{iline,2}=num2str(val);
317                end
318            end
319        end
320        Tabchar=cell2tab(Tabcell,'=');
321    end
322    %% list Attribute names and values associated to the Variable # index-1
323else
324    list_var=get(handles.variables,'String');
325    var_select=list_var{index};
326    set(handles.attributes_txt,'String', ['attributes of ' var_select])
327    if isfield(Field,'VarAttribute')&& length(Field.VarAttribute)>=index-1
328        %         nbline=0;
329        VarAttr=Field.VarAttribute{index-1};
330        if isstruct(VarAttr)
331            attr_list=fieldnames(VarAttr);
332            for iline=1:length(attr_list)
333                Tabcell{iline,1}=attr_list{iline};
334                val=VarAttr.(attr_list{iline}) ;
335                if ischar(val);
336                    Tabcell{iline,2}=val;
337                else
338                    Tabcell{iline,2}=num2str(val);
339                end
340            end
341        end
342    end
343   
344end
345if ~isempty(Tabcell)
346    Tabchar=cell2tab(Tabcell,'=');
347    %     Tabchar=[{''};Tabchar];
348end
349set(handles.attributes,'Value',1);% select the first item
350set(handles.attributes,'String',Tabchar);
351
352%% update dimensions;
353if isfield(Field,'ListDimName')
354    Tabdim={};%default
355    if isequal(index,1)%list all dimensions
356        dim_indices=1:length(Field.ListDimName);
357        set(handles.dimensions_txt,'String', 'dimensions')
358    else
359        DimCell=Field.VarDimName{index-1};
360        if ischar(DimCell)
361            DimCell={DimCell};
362        end
363        dim_indices=[];
364        for idim=1:length(DimCell)
365            dim_index=strcmp(DimCell{idim},Field.ListDimName);%vector with size of Field.ListDimName, =0
366            dim_index=find(dim_index,1);
367            dim_indices=[dim_indices dim_index];
368        end
369        set(handles.dimensions_txt,'String', ['dimensions of ' var_select])
370    end
371    for iline=1:length(dim_indices)
372        Tabdim{iline,1}=Field.ListDimName{dim_indices(iline)};
373        Tabdim{iline,2}=num2str(Field.DimValue(dim_indices(iline)));
374    end
375    Tabchar=cell2tab(Tabdim,' = ');
376    Tabchar=[{''} ;Tabchar];
377    set(handles.dimensions,'Value',1)
378    set(handles.dimensions,'String',Tabchar)
379end
380
381%------------------------------------------------------------------------
382% --- Executes on selection change in FieldOption.
383%------------------------------------------------------------------------
384function FieldOption_Callback(hObject, eventdata, handles)
385
386Field=get(handles.get_field,'UserData');
387FieldList=get(handles.FieldOption,'String');
388FieldOption=FieldList{get(handles.FieldOption,'Value')};
389switch FieldOption
390   
391    case '1D plot'
392        set(handles.Coordinates,'Visible','on')
393        set(handles.PanelOrdinate,'Visible','on')
394        pos=get(handles.PanelOrdinate,'Position');
395        pos(1)=2;
396        pos_coord=get(handles.Coordinates,'Position');
397        pos(2)=pos_coord(2)-pos(4)-2;
398        set(handles.PanelOrdinate,'Position',pos)
399        set(handles.PanelScalar,'Visible','off')
400        set(handles.PanelVectors,'Visible','off')
401        set(handles.Coord_y,'Visible','off')
402        set(handles.Y_title,'Visible','off')
403        set(handles.Coord_z,'Visible','off')
404        set(handles.Z_title,'Visible','off')
405        ordinate_Callback(hObject, eventdata, handles)
406       
407    case 'scalar'
408        set(handles.Coordinates,'Visible','on')
409        set(handles.PanelOrdinate,'Visible','off')
410        set(handles.PanelScalar,'Visible','on')
411        set(handles.PanelVectors,'Visible','off')
412        pos=get(handles.PanelScalar,'Position');
413        pos(1)=2;
414        pos_coord=get(handles.Coordinates,'Position');
415        pos(2)=pos_coord(2)-pos(4)-2;
416        set(handles.PanelScalar,'Position',pos)
417        set(handles.Coord_y,'Visible','on')
418        set(handles.Y_title,'Visible','on')
419        %default scalar selection
420        test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
421        for ilist=1:numel(Field.Display.VarDimName)
422            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
423                Role=Field.Display.VarAttribute{ilist}.Role;
424                if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
425                    test_coord(ilist)=1;
426                end
427            end
428            dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
429            if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
430                test_coord(ilist)=1;
431            end
432        end
433        scalar_index=find(~test_coord,1);%get the first variable not a coordiante
434        if isempty(scalar_index)
435            set(handles.scalar,'Value',1)
436        else
437            set(handles.scalar,'Value',scalar_index)
438        end       
439        scalar_Callback(hObject, eventdata, handles)
440             
441    case 'vectors'
442        set(handles.PanelVectors,'Visible','on')
443        set(handles.Coordinates,'Visible','on')
444        set(handles.PanelOrdinate,'Visible','off')
445        set(handles.PanelScalar,'Visible','off')
446        pos=get(handles.PanelVectors,'Position');
447        pos(1)=2;
448        pos_coord=get(handles.Coordinates,'Position');
449        pos(2)=pos_coord(2)-pos(4)-2;
450        set(handles.PanelVectors,'Position',pos)
451        set(handles.Coord_y,'Visible','on')
452        set(handles.Y_title,'Visible','on')
453        %default vector selection
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
470                    test_coord(ilist)=1;
471                end
472            end
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))
480            end
481        end
482        vector_Callback(handles)
483       
484    case 'civdata...'
485        set(handles.PanelOrdinate,'Visible','off')
486        set(handles.PanelScalar,'Visible','off')
487        set(handles.PanelVectors,'Visible','off')
488        set(handles.Coordinates,'Visible','off')
489end
490
491%------------------------------------------------------------------------
492function ordinate_Callback(hObject, eventdata, handles)
493%------------------------------------------------------------------------
494Field=get(handles.get_field,'UserData');
495y_index=get(handles.ordinate,'Value');
496y_menu=get(handles.ordinate,'String');
497if isempty(y_menu)
498    return
499else
500YName=y_menu{y_index};
501end
502
503%% set list of possible coordinates
504test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
505test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
506ListCoord={''};
507dim_var=Field.Display.VarDimName{y_index};%list of dimensions of the selected variable
508
509for ilist=1:numel(Field.Display.VarDimName)
510    dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
511    if isequal(dimnames,dim_var)
512        test_component(ilist)=1;
513    elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
514        test_coord(ilist)=1;
515    end
516end
517var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
518var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
519ListCoord=Field.Display.ListVarName([var_component var_coord]);
520
521%% set default coord selection
522if numel(find(test_coord))>3
523     SwitchVarIndexTime=get(handles.SwitchVarIndexTime,'String');
524    if numel(SwitchVarIndexTime)<3
525        SwitchVarIndexTime=[SwitchVarIndexTime;'matrix_index'];
526        set(handles.SwitchVarIndexTime,'String',SwitchVarIndexTime)
527    end
528    set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
529    SwitchVarIndexTime_Callback([], [], handles)
530end
531if numel(var_component)<2
532    if numel(test_coord)<2
533        ListCoord={''};
534    else
535        set(handles.Coord_x,'Value',2)
536        set(handles.Coord_y,'Value',1)
537    end
538else
539    coord_val=1;
540    for ilist=1:numel(var_component)
541        ivar=var_component(ilist);
542        if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
543            Role=Field.Display.VarAttribute{ivar}.Role;
544            if strcmp(Role,'coord_x')
545                coord_val=ilist;
546            end
547        end
548    end
549    set(handles.Coord_x,'Value',coord_val)
550end
551set(handles.Coord_x,'String',ListCoord)
552
553
554%% set list of time coordinates
555menu=get(handles.SwitchVarIndexTime,'String');
556TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
557switch TimeOption
558    case 'variable'
559        if numel(find(test_coord))<3
560            ListTime={''};
561        else
562            ListTime=Field.Display.ListVarName(find(test_coord,end));
563        end
564        set(handles.TimeName,'Value',1)
565        set(handles.TimeName,'String',ListTime)
566    case 'matrix index'
567        if numel(find(test_coord))<3
568            ListTime={''};
569        else
570            ListTime=Field.Display.VarDimName{find(test_coord,end)};
571        end
572        set(handles.TimeName,'Value',1)
573        set(handles.TimeName,'String',ListTime)
574end 
575update_field(handles,YName)
576         
577%------------------------------------------------------------------------
578% --- Executes on selection change in scalar menu.
579%------------------------------------------------------------------------
580function scalar_Callback(hObject, eventdata, handles)
581
582Field=get(handles.get_field,'UserData');
583scalar_index=get(handles.scalar,'Value');
584scalar_menu=get(handles.scalar,'String');
585ScalarName=scalar_menu{scalar_index};
586
587%% set list of possible coordinates
588test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
589test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
590dim_var=Field.Display.VarDimName{scalar_index};%list of dimensions of the selected variable
591CheckDimensionX=get(handles.CheckDimensionX,'Value');
592CheckDimensionY=get(handles.CheckDimensionY,'Value');
593if ~CheckDimensionX  || ~CheckDimensionY
594    %look for coordinate variables among the other variables
595    for ilist=1:numel(Field.Display.VarDimName)
596        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
597        if isequal(dimnames,dim_var)
598            test_component(ilist)=1;% the listed variable has the same dimension as the selected scalar-> possibly chosen as unstructured coordinate
599        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var), 1))%variable ilist is a 1D array which can be coordinate variable
600            test_coord(ilist)=1;
601        end
602    end
603end
604var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
605var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
606ListCoord=Field.Display.ListVarName([var_component var_coord]);
607
608%% set default coord selection
609if numel(find(test_coord))>3
610    SwitchVarIndexTime=get(handles.SwitchVarIndexTime,'String');
611    if numel(SwitchVarIndexTime)<3
612        SwitchVarIndexTime=[SwitchVarIndexTime;'matrix_index'];
613        set(handles.SwitchVarIndexTime,'String',SwitchVarIndexTime)
614    end
615    set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
616    SwitchVarIndexTime_Callback([], [], handles)
617end
618% if numel(var_component)<2
619%     if numel(test_coord)<2
620%         ListCoord={''};
621%     else
622%         set(handles.Coord_x,'Value',2)
623%         set(handles.Coord_y,'Value',1)
624%     end
625% else
626coord_val=[0 0];
627% look for labelled unstructured coordinates
628for ilist=1:numel(var_component)
629    ivar=var_component(ilist);
630    if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
631        Role=Field.Display.VarAttribute{ivar}.Role;
632        if strcmp(Role,'coord_x')
633            coord_val(1)=ilist;
634        elseif strcmp(Role,'coord_y')
635            coord_val(2)=ilist;
636        end
637    end
638end
639if numel(find(coord_val))<2
640    if numel(var_coord)>=2
641        coord_val=[numel(var_component)+2 numel(var_component)+1];
642    else
643        coord_val=[1 2];
644    end
645end
646if  CheckDimensionX
647    set(handles.Coord_x,'Value',2)
648    set(handles.Coord_x,'String',dim_var')
649else
650    set(handles.Coord_x,'Value',coord_val(1))
651    set(handles.Coord_x,'String',ListCoord)
652end
653if  CheckDimensionY
654    set(handles.Coord_y,'Value',1)
655    set(handles.Coord_y,'String',dim_var')
656else
657    set(handles.Coord_y,'Value',coord_val(2))
658    set(handles.Coord_y,'String',ListCoord)
659end
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 
682update_field(handles,ScalarName)
683
684% --- Executes on button press in check_rgb.
685function check_rgb_Callback(hObject, eventdata, handles)
686
687
688%------------------------------------------------------------------------
689% --- Executes on selection change in vector_x.
690%------------------------------------------------------------------------
691function vector_x_Callback(hObject, eventdata, handles)
692
693vector_x_menu=get(handles.vector_x,'String');
694vector_x_index=get(handles.vector_x,'Value');
695vector_x=vector_x_menu{vector_x_index};
696vector_Callback(handles)
697update_field(handles,vector_x)
698
699%------------------------------------------------------------------------
700% --- Executes on selection change in vector_x.
701function vector_y_Callback(hObject, eventdata, handles)
702%------------------------------------------------------------------------
703vector_y_menu=get(handles.vector_x,'String');
704vector_y_index=get(handles.vector_x,'Value');
705vector_y=vector_y_menu{vector_y_index};
706vector_Callback(handles)
707update_field(handles,vector_y)
708
709%------------------------------------------------------------------------
710% --- Executes on selection change in vector_z.
711function vector_z_Callback(hObject, eventdata, handles)
712%------------------------------------------------------------------------
713vector_z_menu=get(handles.vector_z,'String');
714vector_z_index=get(handles.vector_z,'Value');
715vector_z=vector_z_menu{vector_z_index};
716vector_Callback(handles)
717update_field(handles,vector_z)
718
719%------------------------------------------------------------------------
720% --- Executes on selection change in vec_color.
721function vec_color_Callback(hObject, eventdata, handles)
722%------------------------------------------------------------------------
723index=get(handles.vec_color,'Value');
724string=get(handles.vec_color,'String');
725VarName=string{index};
726vector_Callback(handles)
727update_field(handles,VarName)
728
729%------------------------------------------------------------------------
730% --- Executes on selection change in vector_x or vector_y
731function vector_Callback( handles)
732%------------------------------------------------------------------------
733Field=get(handles.get_field,'UserData');
734vector_x_index=get(handles.vector_x,'Value');
735vector_y_index=get(handles.vector_y,'Value');
736vec_color_index=get(handles.vec_color,'Value');
737
738%% set list of possible coordinates
739test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
740test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
741check_consistent=1;%check that the selected vector components (and possibly color var) have the same dimensiosn
742ListCoord={''};
743dim_var=Field.Display.VarDimName{vector_x_index};%list of dimensions of the selected variable
744if ~isequal(dim_var,Field.Display.VarDimName{vector_y_index})
745    check_consistent=0;
746elseif vec_color_index~=1 && ~isequal(dim_var,Field.Display.VarDimName{vec_color_index})
747    check_consistent=0;
748end
749% the two vector components have consistent dimensions
750if check_consistent
751    for ilist=1:numel(Field.Display.VarDimName)
752        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
753        if isequal(dimnames,dim_var)
754            test_component(ilist)=1;
755        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
756            test_coord(ilist)=1;
757        end
758    end
759    var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
760    var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
761    ListCoord=Field.Display.ListVarName([var_component var_coord]);
762   
763    %% set default coord selection
764    if numel(find(test_coord))>3
765        set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
766    end
767    if numel(var_component)<2
768        if numel(test_coord)<2
769            ListCoord={''};
770        else
771            set(handles.Coord_x,'Value',2)
772            set(handles.Coord_y,'Value',1)
773        end
774    else
775        coord_val=[0 0];
776        for ilist=1:numel(var_component)
777            ivar=var_component(ilist);
778            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
779                Role=Field.Display.VarAttribute{ivar}.Role;
780                if strcmp(Role,'coord_x')
781                    coord_val(1)=ilist;
782                elseif strcmp(Role,'coord_y')
783                    coord_val(2)=ilist;
784                end
785            end
786        end
787        if isempty(find(coord_val))
788            coord_val=var_coord;% case of dimension coordinates
789        end
790        if numel(find(coord_val))<2
791            %coord_val=[numel(var_component)+2 numel(var_component)+1];
792            coord_val=[1 2];
793        end
794        set(handles.Coord_x,'Value',coord_val(1))
795        set(handles.Coord_y,'Value',coord_val(2))
796    end
797end
798set(handles.Coord_y,'String',ListCoord)
799set(handles.Coord_x,'String',ListCoord)
800
801
802%% set list of time coordinates
803menu=get(handles.SwitchVarIndexTime,'String');
804TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
805switch TimeOption
806    case 'variable'
807        if numel(find(test_coord))<3
808            ListTime={''};
809        else
810            ListTime=Field.Display.ListVarName(find(test_coord,end));
811        end
812        set(handles.TimeName,'Value',1)
813        set(handles.TimeName,'String',ListTime)
814    case 'dim index'
815        if numel(find(test_coord))<3
816            ListTime={''};
817        else
818            ListTime=Field.Display.VarDimName{find(test_coord,end)};
819        end
820        set(handles.TimeName,'Value',1)
821        set(handles.TimeName,'String',ListTime)
822end 
823
824%------------------------------------------------------------------------
825% --- Executes on selection change in SwitchVarIndexX.
826%------------------------------------------------------------------------
827function SwitchVarIndexX_Callback(hObject, eventdata, handles)
828
829%------------------------------------------------------------------------
830% --- Executes on selection change in Coord_x.
831%------------------------------------------------------------------------
832function Coord_x_Callback(hObject, eventdata, handles)
833
834index=get(handles.Coord_x,'Value');
835string=get(handles.Coord_x,'String');
836VarName=string{index};
837update_field(handles,VarName)
838
839%------------------------------------------------------------------------
840% --- Executes on selection change in Coord_y.
841%------------------------------------------------------------------------
842function Coord_y_Callback(hObject, eventdata, handles)
843
844index=get(handles.Coord_y,'Value');
845string=get(handles.Coord_y,'String');
846VarName=string{index};
847update_field(handles,VarName)
848
849%------------------------------------------------------------------------
850% --- Executes on selection change in Coord_z.
851%------------------------------------------------------------------------
852function Coord_z_Callback(hObject, eventdata, handles)
853
854index=get(handles.Coord_z,'Value');
855string=get(handles.Coord_z,'String');
856VarName=string{index};
857update_field(handles,VarName)
858
859%------------------------------------------------------------------------
860% --- Executes on selection change in SwitchVarIndexTime.
861%------------------------------------------------------------------------
862function SwitchVarIndexTime_Callback(hObject, eventdata, handles)
863
864Field=get(handles.get_field,'UserData');
865menu=get(handles.SwitchVarIndexTime,'String');
866option=menu{get(handles.SwitchVarIndexTime,'Value')};
867
868switch option
869    case 'file index'
870        set(handles.TimeName, 'Visible','off')% the time is taken as the file index
871    case 'attribute'
872        set(handles.TimeName, 'Visible','on')% timeName menu represents the available attributes
873        time_index=get(handles.TimeName,'UserData');    %select the input data
874        if isempty(time_index)
875            PreviousList=get(handles.TimeName, 'String');
876            if ~isempty(PreviousList)
877                PreviousAttr=PreviousList{get(handles.TimeName, 'Value')};
878                index=find(strcmp(PreviousAttr,Field.Display.ListGlobalAttributes),1);
879            end
880        end
881        if isempty(time_index)
882            time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);% index of the attributes containing the string 'Time'
883        end     
884        if ~isempty(time_index)
885            set(handles.TimeName,'Value',time_index)
886        else
887            set(handles.TimeName,'Value',1)
888        end
889        set(handles.TimeName, 'String',Field.Display.ListGlobalAttribute)
890
891    case 'variable'% TimeName menu represents the available variables
892        set(handles.TimeName, 'Visible','on')
893        TimeVarName=Field.Display.SingleVarName;% slist of variables with a single dimension (candidate for time)
894        List=get(handles.TimeName,'String');
895        option=List{get(handles.TimeName,'Value')};
896        ind=find(strcmp(option,TimeVarName));
897        if isempty(ind)
898            set(handles.TimeName, 'Value',1);
899        else
900            set(handles.TimeName, 'Value',ind);
901        end
902        set(handles.TimeName, 'String',TimeVarName)
903    case 'matrix_index'% TimeName menu represents the available dimensions
904        set(handles.TimeName, 'Visible','on')     
905        set(handles.TimeName, 'Value',1);
906        set(handles.TimeName, 'String',Field.Display.ListDimName)
907end
908
909%-----------------------------------------------------------------------
910function update_field(handles,VarName)
911%-----------------------------------------------------------------------
912Field=get(handles.get_field,'UserData');
913index=name2index(VarName,Field.ListVarName);
914if ~isempty(index)
915    set(handles.variables,'Value',index+1)
916    variables_Callback(handles.variables, [], handles)
917end
918
919%------------------------------------------------------------------------
920% --- give index numbers of the strings str in the list ListvarName
921% -----------------------------------------------------------------------
922function VarIndex_y=name2index(cell_str,ListVarName)
923
924VarIndex_y=[];
925if ischar(cell_str)
926    for ivar=1:length(ListVarName)
927        varlist=ListVarName{ivar};
928        if isequal(varlist,cell_str)
929            VarIndex_y= ivar;
930            break
931        end
932    end
933elseif iscell(cell_str)
934    for isel=1:length(cell_str)
935        varsel=cell_str{isel};
936        for ivar=1:length(ListVarName)
937            varlist=ListVarName{ivar};
938            if isequal(varlist,varsel)
939                VarIndex_y=[VarIndex_y ivar];
940            end
941        end
942    end
943end
944
945% --- Executes on button press in CheckDimensionY.
946function CheckDimensionX_Callback(hObject, eventdata, handles)
947FieldList=get(handles.FieldOption,'String');
948FieldOption=FieldList{get(handles.FieldOption,'Value')};
949switch FieldOption
950    case '1D plot'
951       
952    case 'scalar'
953       scalar_Callback(hObject, eventdata, handles)
954    case 'vectors'
955end
956
957% --- Executes on button press in CheckDimensionY.
958function CheckDimensionY_Callback(hObject, eventdata, handles)
959FieldList=get(handles.FieldOption,'String');
960FieldOption=FieldList{get(handles.FieldOption,'Value')};
961switch FieldOption
962    case '1D plot'
963       
964    case 'scalar'
965       scalar_Callback(hObject, eventdata, handles)
966    case 'vectors'
967end
968% hObject    handle to CheckDimensionY (see GCBO)
969% eventdata  reserved - to be defined in a future version of MATLAB
970% handles    structure with handles and user data (see GUIDATA)
971
972% Hint: get(hObject,'Value') returns toggle state of CheckDimensionY
Note: See TracBrowser for help on using the repository browser.