source: trunk/src/get_field.m @ 951

Last change on this file since 951 was 924, checked in by g7moreau, 5 years ago
  • Update Copyright Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
File size: 44.1 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% OUTPUT:
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%=======================================================================
19% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
20%   http://www.legi.grenoble-inp.fr
21%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
22%
23%     This file is part of the toolbox UVMAT.
24%
25%     UVMAT is free software; you can redistribute it and/or modify
26%     it under the terms of the GNU General Public License as published
27%     by the Free Software Foundation; either version 2 of the license,
28%     or (at your option) any later version.
29%
30%     UVMAT is distributed in the hope that it will be useful,
31%     but WITHOUT ANY WARRANTY; without even the implied warranty of
32%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33%     GNU General Public License (see LICENSE.txt) for more details.
34%=======================================================================
35
36function varargout = get_field(varargin)
37
38% Last Modified by GUIDE v2.5 18-Feb-2015 23:42:12
39
40% Begin initialization code - DO NOT EDIT
41gui_Singleton = 1;
42gui_State = struct('gui_Name',       mfilename, ...
43                   'gui_Singleton',  gui_Singleton, ...
44                   'gui_OpeningFcn', @get_field_OpeningFcn, ...
45                   'gui_OutputFcn',  @get_field_OutputFcn, ...
46                   'gui_LayoutFcn',  [] , ...
47                   'gui_Callback',   []);
48if nargin && ischar(varargin{1})&& ~isempty(regexp(varargin{1},'_Callback','once'))
49    gui_State.gui_Callback = str2func(varargin{1});
50end
51
52if nargout
53    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
54else
55    gui_mainfcn(gui_State, varargin{:});
56end
57% End initialization code - DO NOT EDIT
58     
59%------------------------------------------------------------------------
60% --- Executes just before get_field is made visible.
61%------------------------------------------------------------------------
62function get_field_OpeningFcn(hObject, eventdata, handles,filename,ParamIn)
63
64%% GUI settings
65handles.output = 'Cancel';
66guidata(hObject, handles);
67set(hObject,'WindowButtonDownFcn',{'mouse_down'}) % allows mouse action with right button (zoom for uicontrol display)
68set(hObject,'CloseRequestFcn',{@closefcn,handles})
69
70%% enter input data
71if ischar(filename) % input file name
72    set(handles.inputfile,'String',filename)% fill the input file name
73    [Field,tild,tild,errormsg]=nc2struct(filename,[]);% reads the  field structure, without the variables
74else
75    msgbox_uvmat('ERROR','get_field requires a file name as input')% display error message for input file reading
76    return
77end
78if ~isempty(errormsg)
79    msgbox_uvmat('ERROR',['get_field/nc2struct/' errormsg])% display error message for input file reading
80    return
81end
82if ~isfield(Field,'ListVarName')
83    msgbox_uvmat('ERROR',['no variable found in ' filename])% display error message for input file reading
84    return
85end
86if ~exist('ParamIn','var')
87    ParamIn=[];
88end
89
90%% look at singletons and variables with a single dimension
91Field.Display=Field;
92Field.Check0D=zeros(size(Field.ListVarName));% =1 for arrays with a single value
93NbVar=numel(Field.VarDimName);%nbre of variables in the input data
94for ilist=1:NbVar
95    if ischar(Field.VarDimName{ilist})
96        Field.VarDimName{ilist}={Field.VarDimName{ilist}}; %transform string into cell
97    end
98    NbDim=numel(Field.VarDimName{ilist});
99    check_singleton=false(1,NbDim);%  check singleton, false by default
100    for idim=1:NbDim
101        dim_index=strcmp(Field.VarDimName{ilist}{idim},Field.ListDimName);%index in the list of dimensions
102        check_singleton(idim)=isequal(Field.DimValue(dim_index),1);%check_singleton=1 for singleton
103    end
104    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
105    if ~Field.Check0D(ilist)
106    Field.Display.VarDimName{ilist}=Field.VarDimName{ilist}(~check_singleton);% eliminate singletons in the list of variable dimensions
107    end
108end
109if ~isfield(Field,'VarAttribute')
110    Field.VarAttribute={};
111end
112if numel(Field.VarAttribute)<NbVar% complement VarAttribute by blanjs if neded
113    Field.VarAttribute(numel(Field.VarAttribute)+1:NbVar)=cell(1,NbVar-numel(Field.VarAttribute));
114end
115% Field.Display = list of variables and corresponding properties obtained after removal of variables with a single value and singleton dimensions
116Field.Display.ListVarName=Field.ListVarName(~Field.Check0D); %list of variables available for plots, after eliminating variables with a single value
117Field.Display.VarAttribute=Field.VarAttribute(~Field.Check0D);
118Field.Display.VarDimName=Field.Display.VarDimName(~Field.Check0D);
119Field.Display.ListDimName=Field.ListDimName(Field.DimValue~=1);% list of non singleton dimension names
120Field.Display.DimValue=Field.DimValue(Field.DimValue~=1);% corresponding list of non singleton dimension values
121
122
123%% analyse the input field cells
124[CellInfo,NbDim,errormsg]=find_field_cells(Field.Display);
125if ~isempty(errormsg)
126    msgbox_uvmat('ERROR',['get_field / Field_input / find_field_cells: ' errormsg])
127    return
128end
129[Field.MaxDim,imax]=max(NbDim);
130
131%% set time mode
132ListSwitchVarIndexTime={'file index'};% default setting: the time is the file index
133% look at global attributes with numerical values
134check_numvalue=false(1,numel(Field.ListGlobalAttribute));
135for ilist=1:numel(Field.ListGlobalAttribute)
136    Value=Field.(Field.ListGlobalAttribute{ilist});
137    check_numvalue(ilist)=isnumeric(Value);
138end
139Field.Display.ListGlobalAttribute=Field.ListGlobalAttribute(check_numvalue);% select the attributes with float numerical value
140if ~isempty(Field.Display.ListGlobalAttribute)
141    ListSwitchVarIndexTime=[ListSwitchVarIndexTime; {'attribute'}];% the time can be chosen as a global attribute
142end
143if Field.MaxDim>=2
144    ListSwitchVarIndexTime=[ListSwitchVarIndexTime;{'variable'};{'matrix index'}];% the time can be chosen as a dim index
145end
146
147%% select the Time attribute from input
148if isfield(ParamIn,'TimeAttrName')
149    time_index=find(strcmp(ParamIn.TimeAttrName,Field.Display.ListGlobalAttribute),1);
150else
151    time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);% look for global attribute containing name 'Time'
152end
153if ~isempty(time_index)
154    set(handles.SwitchVarIndexTime,'Value',2);
155    set(handles.TimeName,'UserData',time_index)
156else
157    set(handles.SwitchVarIndexTime,'Value',1);
158end
159set(handles.SwitchVarIndexTime,'String',ListSwitchVarIndexTime)
160set(handles.get_field,'UserData',Field);% record the finput field structure
161SwitchVarIndexTime_Callback([], [], handles)
162
163%% set vector menu (priority) if detected or scalar menu for space dim >=2, or usual (x,y) plot for 1D fields
164set(handles.vector_x,'String',Field.Display.ListVarName)% fill the menu of x vector components
165set(handles.vector_y,'String',Field.Display.ListVarName)% fill the menu of y vector components
166set(handles.vector_z,'String',[{''} Field.Display.ListVarName])% fill the menu of y vector components
167set(handles.vec_color,'String',[{''} Field.Display.ListVarName])% fill the menu of y vector components
168set(handles.scalar,'Value',1)% fill the menu of y vector components
169set(handles.scalar,'String',Field.Display.ListVarName)% fill the menu for scalar
170set(handles.ordinate,'Value',1)% fill the menu of y vector components
171set(handles.ordinate,'String',Field.Display.ListVarName)% fill the menu of y coordinate for 1D plots
172checkseries=0;
173if isfield(ParamIn,'SeriesInput') && ParamIn.SeriesInput% case of call by series
174    set(handles.FieldOption,'value',1)
175    if isfield(Field,'Conventions')&& strcmp(Field.Conventions,'uvmat/civdata')
176    set(handles.FieldOption,'String',{'scalar';'vectors';'civdata...'})
177    else
178       set(handles.FieldOption,'String',{'scalar';'vectors'})
179    end
180    checkseries=1;
181    set(handles.scalar,'Max',2)
182elseif isfield(Field,'Conventions')&& strcmp(Field.Conventions,'uvmat/civdata')
183    set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors';'civdata...'})% provides the possibility to come back to civdata
184    set(handles.scalar,'Max',1)
185else
186    set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors'})
187    set(handles.scalar,'Max',1)
188end
189if Field.MaxDim>=2 && ~checkseries% case of 2D (or 3D) fields
190    check_vec_input=0;
191    if isfield(ParamIn,'vector_x')&& isfield(ParamIn,'vector_y')
192        ichoice_x=find(strcmp(ParamIn.vector_x,Field.Display.ListVarName),1);
193        ichoice_y=find(strcmp(ParamIn.vector_y,Field.Display.ListVarName),1);
194        if ~isempty(ichoice_x)&&~isempty(ichoice_y)
195            set(handles.vector_x,'UserData',ichoice_x)
196            set(handles.vector_y,'UserData',ichoice_y)
197            check_vec_input=1;
198        end
199    end
200    if ~check_vec_input && isfield(CellInfo{imax},'VarIndex_vector_x') &&  isfield(CellInfo{imax},'VarIndex_vector_y')
201        set(handles.vector_x,'UserData',CellInfo{imax}.VarIndex_vector_x(1))
202        set(handles.vector_y,'UserData',CellInfo{imax}.VarIndex_vector_y(1))
203        check_vec_input=1;
204    end
205    if check_vec_input
206        set(handles.FieldOption,'Value',3)% set vector selection option
207    else
208        set(handles.FieldOption,'Value',2)% set scalar selection option
209    end
210else % case of 1D fields
211    set(handles.FieldOption,'Value',1)
212end
213
214%% fill the general list of dimensions, variables, attributes
215if isfield(Field,'ListDimName')&&~isempty(Field.ListDimName)
216    Tabcell(:,1)=Field.ListDimName;
217    for iline=1:length(Field.ListDimName)
218        Tabcell{iline,2}=num2str(Field.DimValue(iline));
219    end
220    Tabchar=cell2tab(Tabcell,' = ');
221    set(handles.dimensions,'String',Tabchar)
222end
223
224%% fill menus for coordinates and time
225FieldOption_Callback(handles.variables,[], handles)% list the global attributes
226
227%% Make choices of coordinates from input
228if isfield(CellInfo{imax},'CoordIndex')
229    CoordIndex=CellInfo{imax}.CoordIndex;
230    if numel(CoordIndex)==2
231        YName=Field.ListVarName{CoordIndex(1)};
232        XName=Field.ListVarName{CoordIndex(2)};
233        ListCoord=get(handles.Coord_x,'String');
234        XIndex=find(strcmp(XName,ListCoord));
235        if ~isempty(XIndex)
236            set(handles.Coord_x,'Value',XIndex)
237        end
238        YIndex=find(strcmp(YName,ListCoord));
239        if ~isempty(YIndex)
240            set(handles.Coord_y,'Value',YIndex)
241        end
242    end
243end
244
245%% put the GUI on the lower right of the sceen
246set(hObject,'Unit','pixels')
247%pos_view_field=get(hObject,'Position');
248set(0,'Unit','pixels')
249ScreenSize=get(0,'ScreenSize');
250pos_view_field(3:4)=[955 648];
251pos_view_field(1)=ScreenSize(1)+ScreenSize(3)-pos_view_field(3);
252pos_view_field(2)=ScreenSize(2);
253set(hObject,'Position',pos_view_field)
254set(handles.get_field,'WindowStyle','modal')% Make the GUI modal
255if isfield(ParamIn,'Title')
256    set(hObject,'Name',ParamIn.Title)
257end
258
259%% set z coordinate menu if relevant
260if Field.MaxDim>=3 && prod(Field.DimValue)<10^8; % 3D field (with memory content smaller than 400 Mo)
261    set(handles.Check3D,'Value',1)
262else
263    set(handles.Check3D,'Value',0)
264end
265Check3D_Callback(hObject, eventdata, handles)
266set(handles.variables,'Value',1)
267set(handles.variables,'String',[{'*'} Field.ListVarName])
268variables_Callback(handles.variables,[], handles)% list the global attributes
269drawnow
270uiwait(handles.get_field);
271
272% -----------------------------------------------------------------------
273% --- Activated by selection in the list of variables
274% ----------------------------------------------------------------------
275function variables_Callback(hObject, eventdata, handles)
276
277Tabchar={''};%default
278Tabcell=[];
279hselect_field=get(handles.variables,'parent');
280Field=get(handles.get_field,'UserData');
281index=get(handles.variables,'Value');%index in the list 'variables'
282
283%% list global TimeAttribute names and values if index=1 (blank TimeVariable display) is selected
284if isequal(index,1)
285    set(handles.attributes_txt,'String','global attributes')
286    if isfield(Field,'ListGlobalAttribute') && ~isempty(Field.ListGlobalAttribute)
287        for iline=1:length(Field.ListGlobalAttribute)
288            Tabcell{iline,1}=Field.ListGlobalAttribute{iline};
289            if isfield(Field, Field.ListGlobalAttribute{iline})
290                val=Field.(Field.ListGlobalAttribute{iline});
291                if ischar(val);% attribute value is char string
292                    Tabcell{iline,2}=val;
293                elseif size(val,1)==1 %attribute value is a number or matlab vector
294                    Tabcell{iline,2}=num2str(val);
295                end
296            end
297        end
298        Tabchar=cell2tab(Tabcell,'=');
299    end
300    %% list Attribute names and values associated to the Variable # index-1
301else
302    list_var=get(handles.variables,'String');
303    if index>numel(list_var)
304        return
305    end
306    var_select=list_var{index};
307    set(handles.attributes_txt,'String', ['attributes of ' var_select])
308    if isfield(Field,'VarAttribute')&& length(Field.VarAttribute)>=index-1
309        %         nbline=0;
310        VarAttr=Field.VarAttribute{index-1};
311        if isstruct(VarAttr)
312            attr_list=fieldnames(VarAttr);
313            for iline=1:length(attr_list)
314                Tabcell{iline,1}=attr_list{iline};
315                val=VarAttr.(attr_list{iline}) ;
316                if ischar(val);
317                    Tabcell{iline,2}=val;
318                else
319                    Tabcell{iline,2}=num2str(val);
320                end
321            end
322        end
323    end
324end
325if ~isempty(Tabcell)
326    Tabchar=cell2tab(Tabcell,'=');
327    %     Tabchar=[{''};Tabchar];
328end
329set(handles.attributes,'Value',1);% select the first item
330set(handles.attributes,'String',Tabchar);
331
332%% update dimensions;
333if isfield(Field,'ListDimName')
334    Tabdim={};%default
335    if isequal(index,1)%list all dimensions
336        dim_indices=1:length(Field.ListDimName);
337        set(handles.dimensions_txt,'String', 'dimensions')
338    else
339        DimCell=Field.VarDimName{index-1};
340        if ischar(DimCell)
341            DimCell={DimCell};
342        end
343        dim_indices=[];
344        for idim=1:length(DimCell)
345            dim_index=strcmp(DimCell{idim},Field.ListDimName);%vector with size of Field.ListDimName, =0
346            dim_index=find(dim_index,1);
347            dim_indices=[dim_indices dim_index];
348        end
349        set(handles.dimensions_txt,'String', ['dimensions of ' var_select])
350    end
351    for iline=1:length(dim_indices)
352        Tabdim{iline,1}=Field.ListDimName{dim_indices(iline)};
353        Tabdim{iline,2}=num2str(Field.DimValue(dim_indices(iline)));
354    end
355    Tabchar=cell2tab(Tabdim,' = ');
356    Tabchar=[{''} ;Tabchar];
357    set(handles.dimensions,'Value',1)
358    set(handles.dimensions,'String',Tabchar)
359end
360
361%------------------------------------------------------------------------
362% --- Executes on selection change in FieldOption.
363%------------------------------------------------------------------------
364function FieldOption_Callback(hObject, eventdata, handles)
365
366Field=get(handles.get_field,'UserData');
367FieldList=get(handles.FieldOption,'String');
368FieldOption=FieldList{get(handles.FieldOption,'Value')};
369switch FieldOption
370   
371    case '1D plot'
372        set(handles.Coordinates,'Visible','on')
373        set(handles.PanelOrdinate,'Visible','on')
374        pos=get(handles.PanelOrdinate,'Position');
375        pos(1)=2;
376        pos_coord=get(handles.Coordinates,'Position');
377        pos(2)=pos_coord(2)-pos(4)-2;
378        set(handles.PanelOrdinate,'Position',pos)
379        set(handles.PanelScalar,'Visible','off')
380        set(handles.PanelVectors,'Visible','off')
381        set(handles.Coord_y,'Visible','off')
382        set(handles.Y_title,'Visible','off')
383        set(handles.Coord_z,'Visible','off')
384        set(handles.Z_title,'Visible','off')
385        ordinate_Callback(hObject, eventdata, handles)
386       
387    case {'scalar'}
388        set(handles.Coordinates,'Visible','on')
389        set(handles.PanelOrdinate,'Visible','off')
390        set(handles.PanelScalar,'Visible','on')
391        set(handles.PanelVectors,'Visible','off')
392        pos=get(handles.PanelScalar,'Position');
393        pos(1)=2;
394        pos_coord=get(handles.Coordinates,'Position');
395        pos(2)=pos_coord(2)-pos(4)-2;
396        set(handles.PanelScalar,'Position',pos)
397        set(handles.Coord_y,'Visible','on')
398        set(handles.Y_title,'Visible','on')
399        %default scalar selection
400        test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
401        for ilist=1:numel(Field.Display.VarDimName)
402            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
403                Role=Field.Display.VarAttribute{ilist}.Role;
404                if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
405                    test_coord(ilist)=1;
406                end
407            end
408            dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
409            if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
410                test_coord(ilist)=1;
411            end
412        end
413        scalar_index=find(~test_coord,1);%get the first variable not a coordinate
414        if isempty(scalar_index)
415            set(handles.scalar,'Value',1)
416        else
417            set(handles.scalar,'Value',scalar_index)
418        end       
419        scalar_Callback(hObject, eventdata, handles)
420             
421    case 'vectors'
422        set(handles.PanelVectors,'Visible','on')
423        set(handles.Coordinates,'Visible','on')
424        set(handles.PanelOrdinate,'Visible','off')
425        set(handles.PanelScalar,'Visible','off')
426        pos=get(handles.PanelVectors,'Position');
427        pos(1)=2;
428        pos_coord=get(handles.Coordinates,'Position');
429        pos(2)=pos_coord(2)-pos(4)-2;
430        set(handles.PanelVectors,'Position',pos)
431        set(handles.Coord_y,'Visible','on')
432        set(handles.Y_title,'Visible','on')
433        %default vector selection
434        vector_x_value=get(handles.vector_x,'UserData');
435        vector_y_value=get(handles.vector_y,'UserData');
436        if ~isempty(vector_x_value)&&~isempty(vector_y_value)
437            set(handles.vector_x,'Value',vector_x_value)
438            set(handles.vector_y,'Value',vector_y_value)
439        else
440            test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordinate
441            for ilist=1:numel(Field.Display.VarDimName)
442                if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
443                    Role=Field.Display.VarAttribute{ilist}.Role;
444                    if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
445                        test_coord(ilist)=1;
446                    end
447                end
448                dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
449                if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
450                    test_coord(ilist)=1;
451                end
452            end
453            vector_index=find(~test_coord,2);%get the two first variables not a coordinate
454            if isempty(vector_index)
455                set(handles.vector_x,'Value',1)
456                set(handles.vector_y,'Value',2)
457            else
458                set(handles.vector_x,'Value',vector_index(1))
459                set(handles.vector_y,'Value',vector_index(2))
460            end
461        end
462        vector_Callback(handles)
463       
464    case 'civdata...'
465        set(handles.PanelOrdinate,'Visible','off')
466        set(handles.PanelScalar,'Visible','off')
467        set(handles.PanelVectors,'Visible','off')
468        set(handles.Coordinates,'Visible','off')
469end
470
471%------------------------------------------------------------------------
472function ordinate_Callback(hObject, eventdata, handles)
473%------------------------------------------------------------------------
474Field=get(handles.get_field,'UserData');
475y_index=get(handles.ordinate,'Value');
476y_menu=get(handles.ordinate,'String');
477if isempty(y_menu)
478    return
479else
480YName=y_menu{y_index};
481end
482
483%% set list of possible coordinates
484test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
485test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
486ListCoord={''};
487dim_var=Field.Display.VarDimName{y_index};%list of dimensions of the selected variable
488
489for ilist=1:numel(Field.Display.VarDimName)
490    dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
491    if isequal(dimnames,dim_var)
492        test_component(ilist)=1;
493    elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
494        test_coord(ilist)=1;
495    end
496end
497var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
498var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
499ListCoord=Field.Display.ListVarName([var_component var_coord]);
500
501%% set default coord selection
502if numel(find(test_coord))>3
503     SwitchVarIndexTime=get(handles.SwitchVarIndexTime,'String');
504    if numel(SwitchVarIndexTime)<3
505        SwitchVarIndexTime=[SwitchVarIndexTime;'matrix_index'];
506        set(handles.SwitchVarIndexTime,'String',SwitchVarIndexTime)
507    end
508    set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
509    SwitchVarIndexTime_Callback([], [], handles)
510end
511if numel(var_component)<2
512    if numel(test_coord)<2
513        ListCoord={''};
514    else
515        set(handles.Coord_x,'Value',2)
516        set(handles.Coord_y,'Value',1)
517    end
518else
519    coord_val=1;
520    for ilist=1:numel(var_component)
521        ivar=var_component(ilist);
522        if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
523            Role=Field.Display.VarAttribute{ivar}.Role;
524            if strcmp(Role,'coord_x')
525                coord_val=ilist;
526            end
527        end
528    end
529    set(handles.Coord_x,'Value',coord_val)
530end
531set(handles.Coord_x,'String',ListCoord)
532
533
534%% set list of time coordinates
535menu=get(handles.SwitchVarIndexTime,'String');
536TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
537switch TimeOption
538    case 'variable'
539        if numel(find(test_coord))<3
540            ListTime={''};
541        else
542            ListTime=Field.Display.ListVarName(find(test_coord,end));
543        end
544        set(handles.TimeName,'Value',1)
545        set(handles.TimeName,'String',ListTime)
546    case 'matrix index'
547        if numel(find(test_coord))<3
548            ListTime={''};
549        else
550            ListTime=Field.Display.VarDimName{find(test_coord,end)};
551        end
552        set(handles.TimeName,'Value',1)
553        set(handles.TimeName,'String',ListTime)
554end 
555update_field(handles,YName)
556         
557%------------------------------------------------------------------------
558% --- Executes on selection change in scalar menu.
559%------------------------------------------------------------------------
560function scalar_Callback(hObject, eventdata, handles)
561
562Field=get(handles.get_field,'UserData');
563scalar_index=get(handles.scalar,'Value');
564scalar_menu=get(handles.scalar,'String');
565ScalarName=scalar_menu{scalar_index};
566
567%% set list of possible coordinates
568test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
569test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
570dim_var=Field.Display.VarDimName{scalar_index};%list of dimensions of the selected variable
571if ~get(handles.CheckDimensionX,'Value') 
572    %look for coordinate variables among the other variables
573    for ilist=1:numel(Field.Display.VarDimName)
574        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
575        if isequal(dimnames,dim_var)
576            test_component(ilist)=1;% the listed variable has the same dimension as the selected scalar-> possibly chosen as unstructured coordinate
577        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var), 1))%variable ilist is a 1D array which can be coordinate variable
578            test_coord(ilist)=1;
579        end
580    end
581end
582var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
583var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
584var_coord(var_coord==scalar_index)=[];
585var_component(var_component==scalar_index)=[];
586ListCoord=Field.Display.ListVarName([var_coord var_component]);
587
588%% set default coord selection
589% if numel(find(test_coord))>3
590%     SwitchVarIndexTime=get(handles.SwitchVarIndexTime,'String');
591%     if numel(SwitchVarIndexTime)<3
592%         SwitchVarIndexTime=[SwitchVarIndexTime;'matrix_index'];
593%         set(handles.SwitchVarIndexTime,'String',SwitchVarIndexTime)
594%     end
595%     set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
596%     SwitchVarIndexTime_Callback([], [], handles)
597% end
598
599coord_val=[0 0];
600% look for labelled unstructured coordinates
601for ilist=1:numel(var_component)
602    ivar=var_component(ilist);
603    if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
604        Role=Field.Display.VarAttribute{ivar}.Role;
605        if strcmp(Role,'coord_x')
606            coord_val(1)=ilist;
607        elseif strcmp(Role,'coord_y')
608            coord_val(2)=ilist;
609            elseif strcmp(Role,'coord_z')
610            coord_val(3)=ilist;
611        end
612    end
613end
614if numel(find(coord_val))<2
615    if numel(var_coord)>=3
616         coord_val=[3 2 1];
617    elseif numel(var_coord)>=2
618       % coord_val=[numel(var_component)+2 numel(var_component)+1];
619       coord_val=[2 1];
620    else
621        coord_val=[1 2];
622    end
623end
624if  get(handles.CheckDimensionX,'Value')
625    set(handles.Coord_x,'Value',2)
626    set(handles.Coord_x,'String',dim_var')
627else
628    set(handles.Coord_x,'Value',coord_val(1))
629    set(handles.Coord_x,'String',ListCoord)
630end
631if  get(handles.CheckDimensionX,'Value')
632    set(handles.Coord_y,'Value',1)
633    set(handles.Coord_y,'String',dim_var')
634else
635    set(handles.Coord_y,'Value',coord_val(2))
636    set(handles.Coord_y,'String',ListCoord)
637end
638if  get(handles.CheckDimensionX,'Value')
639    set(handles.Coord_z,'Value',1)
640    set(handles.Coord_z,'String',dim_var')
641else
642    set(handles.Coord_z,'Value',coord_val(2))
643    set(handles.Coord_z,'String',ListCoord)
644end
645
646%% set list of time coordinates
647menu=get(handles.SwitchVarIndexTime,'String');
648TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
649switch TimeOption
650    case 'variable'
651        if numel(find(test_coord))<3
652            ListTime={''};
653        else
654            ListTime=Field.Display.ListVarName(find(test_coord,end));
655        end
656        set(handles.TimeName,'Value',1)
657        set(handles.TimeName,'String',ListTime)
658    case 'dim index'
659        if numel(find(test_coord))<3
660            ListTime={''};
661        else
662            ListTime=Field.Display.VarDimName{find(test_coord,end)};
663        end
664        set(handles.TimeName,'Value',1)
665        set(handles.TimeName,'String',ListTime)
666end 
667update_field(handles,ScalarName)
668
669% --- Executes on button press in check_rgb.
670function check_rgb_Callback(hObject, eventdata, handles)
671
672
673%------------------------------------------------------------------------
674% --- Executes on selection change in vector_x.
675%------------------------------------------------------------------------
676function vector_x_Callback(hObject, eventdata, handles)
677
678vector_x_menu=get(handles.vector_x,'String');
679vector_x_index=get(handles.vector_x,'Value');
680vector_x=vector_x_menu{vector_x_index};
681vector_Callback(handles)
682update_field(handles,vector_x)
683
684%------------------------------------------------------------------------
685% --- Executes on selection change in vector_x.
686function vector_y_Callback(hObject, eventdata, handles)
687%------------------------------------------------------------------------
688vector_y_menu=get(handles.vector_x,'String');
689vector_y_index=get(handles.vector_x,'Value');
690vector_y=vector_y_menu{vector_y_index};
691vector_Callback(handles)
692update_field(handles,vector_y)
693
694%------------------------------------------------------------------------
695% --- Executes on selection change in vector_z.
696function vector_z_Callback(hObject, eventdata, handles)
697%------------------------------------------------------------------------
698vector_z_menu=get(handles.vector_z,'String');
699vector_z_index=get(handles.vector_z,'Value');
700vector_z=vector_z_menu{vector_z_index};
701vector_Callback(handles)
702update_field(handles,vector_z)
703
704%------------------------------------------------------------------------
705% --- Executes on selection change in vec_color.
706function vec_color_Callback(hObject, eventdata, handles)
707%------------------------------------------------------------------------
708index=get(handles.vec_color,'Value');
709string=get(handles.vec_color,'String');
710VarName=string{index};
711vector_Callback(handles)
712update_field(handles,VarName)
713
714%------------------------------------------------------------------------
715% --- Executes on selection change in vector_x or vector_y
716function vector_Callback( handles)
717%------------------------------------------------------------------------
718Field=get(handles.get_field,'UserData');
719vector_x_index=get(handles.vector_x,'Value');
720vector_y_index=get(handles.vector_y,'Value');
721vec_color_index=get(handles.vec_color,'Value');
722
723%% set list of possible coordinates
724test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
725test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordinate
726check_consistent=1;%check that the selected vector components (and possibly color var) have the same dimensiosn
727ListCoord={''};
728dim_var=Field.Display.VarDimName{vector_x_index};%list of dimensions of the selected variable
729if ~isequal(dim_var,Field.Display.VarDimName{vector_y_index})
730    check_consistent=0;
731elseif vec_color_index~=1 && ~isequal(dim_var,Field.Display.VarDimName{vec_color_index})
732    check_consistent=0;
733end
734% the two vector components have consistent dimensions
735if check_consistent
736    for ilist=1:numel(Field.Display.VarDimName)
737        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
738        if isequal(dimnames,dim_var)
739            test_component(ilist)=1;
740        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
741            test_coord(ilist)=1;
742        end
743    end
744    var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
745    var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
746    var_component(var_component==vector_x_index|var_component==vector_y_index)=[];
747    var_coord(var_coord==vector_x_index|var_coord==vector_y_index)=[];% remove vector components form te possible list of coordinates
748    ListCoord=Field.Display.ListVarName([var_coord var_component]);
749   
750    %% set default coord selection
751    if numel(find(test_coord))>3
752        set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
753    end
754    if numel(var_component)<2
755        if numel(find(test_coord))<2
756            ListCoord={''};
757        else
758            if numel(find(test_coord))>=3
759                set(handles.Coord_x,'Value',3)
760                set(handles.Coord_y,'Value',2)
761                set(handles.Coord_z,'Value',1)
762            else
763                set(handles.Coord_x,'Value',2)
764                set(handles.Coord_y,'Value',1)
765            end
766        end
767    else
768        coord_val=[0 0 0];
769        for ilist=1:numel(var_component)
770            ivar=var_component(ilist);
771            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
772                Role=Field.Display.VarAttribute{ivar}.Role;
773                if strcmp(Role,'coord_x')
774                    coord_val(1)=ilist;
775                elseif strcmp(Role,'coord_y')
776                    coord_val(2)=ilist;
777                elseif strcmp(Role,'coord_z')
778                    coord_val(3)=ilist;
779                end
780            end
781        end
782        if isempty(find(coord_val))
783            coord_val=var_coord;% case of dimension coordinates
784        end
785        if numel(find(coord_val))<2
786            %coord_val=[numel(var_component)+2 numel(var_component)+1];
787            coord_val=[1 2 3];
788        end
789        set(handles.Coord_x,'Value',coord_val(1))
790        set(handles.Coord_y,'Value',coord_val(2))
791        if numel(coord_val)>=3
792            set(handles.Coord_z,'Value',coord_val(3))
793        end
794    end
795end
796set(handles.Coord_z,'String',ListCoord)
797set(handles.Coord_y,'String',ListCoord)
798set(handles.Coord_x,'String',ListCoord)
799
800
801%% set list of time coordinates
802menu=get(handles.SwitchVarIndexTime,'String');
803TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
804switch TimeOption
805    case 'variable'
806        if numel(find(test_coord))<3
807            ListTime={''};
808        else
809            ListTime=Field.Display.ListVarName(find(test_coord,end));
810        end
811        set(handles.TimeName,'Value',1)
812        set(handles.TimeName,'String',ListTime)
813    case 'dim index'
814        if numel(find(test_coord))<3
815            ListTime={''};
816        else
817            ListTime=Field.Display.VarDimName{find(test_coord,end)};
818        end
819        set(handles.TimeName,'Value',1)
820        set(handles.TimeName,'String',ListTime)
821end 
822
823%------------------------------------------------------------------------
824% --- Executes on selection change in SwitchVarIndexX.
825%------------------------------------------------------------------------
826function SwitchVarIndexX_Callback(hObject, eventdata, handles)
827
828%------------------------------------------------------------------------
829% --- Executes on selection change in Coord_x.
830%------------------------------------------------------------------------
831function Coord_x_Callback(hObject, eventdata, handles)
832
833index=get(handles.Coord_x,'Value');
834string=get(handles.Coord_x,'String');
835VarName=string{index};
836update_field(handles,VarName)
837
838%------------------------------------------------------------------------
839% --- Executes on selection change in Coord_y.
840%------------------------------------------------------------------------
841function Coord_y_Callback(hObject, eventdata, handles)
842
843index=get(handles.Coord_y,'Value');
844string=get(handles.Coord_y,'String');
845VarName=string{index};
846update_field(handles,VarName)
847
848%------------------------------------------------------------------------
849% --- Executes on selection change in Coord_z.
850%------------------------------------------------------------------------
851function Coord_z_Callback(hObject, eventdata, handles)
852
853index=get(handles.Coord_z,'Value');
854string=get(handles.Coord_z,'String');
855VarName=string{index};
856update_field(handles,VarName)
857
858%------------------------------------------------------------------------
859% --- Executes on selection change in SwitchVarIndexTime.
860%------------------------------------------------------------------------
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        VarNbDim=cellfun('length',Field.Display.VarDimName); % check the nbre of dimensions of each input variable
894        TimeVarName=Field.Display.ListVarName(VarNbDim==1);% list of variables with a single dimension (candidate for time)
895        List=get(handles.TimeName,'String');% list of names on the menu for time
896        if isempty(List)
897            ind=1;
898        else
899            option=List{get(handles.TimeName,'Value')};% previous selected option
900            ind=find(strcmp(option,TimeVarName)); %check whether the previous selection is available in the newlist
901            if isempty(ind)
902                ind=1;
903            end
904        end
905        if ~isempty(TimeVarName)
906            set(handles.TimeName, 'Value',ind);% select first value in the menu if the option is not found
907            set(handles.TimeName, 'String',TimeVarName)% update the menu for time name
908        end
909    case 'matrix index'% TimeName menu represents the available dimensions
910        set(handles.TimeName, 'Visible','on')     
911        set(handles.TimeName, 'Value',1);
912        set(handles.TimeName, 'String',Field.Display.ListDimName)
913end
914TimeName_Callback(hObject, [], handles)
915
916%-----------------------------------------------------------------------
917function update_field(handles,VarName)
918%-----------------------------------------------------------------------
919Field=get(handles.get_field,'UserData');
920index=name2index(VarName,Field.ListVarName);
921if ~isempty(index)
922    set(handles.variables,'Value',index+1)
923    variables_Callback(handles.variables, [], handles)
924end
925
926%------------------------------------------------------------------------
927% --- give index numbers of the strings str in the list ListvarName
928% -----------------------------------------------------------------------
929function VarIndex_y=name2index(cell_str,ListVarName)
930
931VarIndex_y=[];
932if ischar(cell_str)
933    for ivar=1:length(ListVarName)
934        varlist=ListVarName{ivar};
935        if isequal(varlist,cell_str)
936            VarIndex_y= ivar;
937            break
938        end
939    end
940elseif iscell(cell_str)
941    for isel=1:length(cell_str)
942        varsel=cell_str{isel};
943        for ivar=1:length(ListVarName)
944            varlist=ListVarName{ivar};
945            if isequal(varlist,varsel)
946                VarIndex_y=[VarIndex_y ivar];
947            end
948        end
949    end
950end
951
952% --- Executes on button press in CheckDimensionY.
953function CheckDimensionX_Callback(hObject, eventdata, handles)
954CheckDimensionX=get(handles.CheckDimensionX,'value')
955if CheckDimensionX
956    set(handles.Coordinates,'visible','off')
957else
958    set(handles.Coordinates,'visible','on')
959end
960% FieldList=get(handles.FieldOption,'String');
961% FieldOption=FieldList{get(handles.FieldOption,'Value')};
962% switch FieldOption
963%     case '1D plot'
964%         
965%     case {'scalar'}
966%        scalar_Callback(hObject, eventdata, handles)
967%     case 'vectors'
968% end
969
970% % --- Executes on button press in CheckDimensionY.
971% function CheckDimensionY_Callback(hObject, eventdata, handles)
972% FieldList=get(handles.FieldOption,'String');
973% FieldOption=FieldList{get(handles.FieldOption,'Value')};
974% switch FieldOption
975%     case '1D plot'
976%         
977%     case {'scalar','pick variables'}
978%        scalar_Callback(hObject, eventdata, handles)
979%     case 'vectors'
980% end
981%
982%
983% % --- Executes on button press in CheckDimensionZ.
984% function CheckDimensionZ_Callback(hObject, eventdata, handles)
985% FieldList=get(handles.FieldOption,'String');
986% FieldOption=FieldList{get(handles.FieldOption,'Value')};
987% switch FieldOption
988%     case '1D plot'
989%         
990%     case 'scalar'
991%        scalar_Callback(hObject, eventdata, handles)
992%     case 'vectors'
993% end
994
995% --- Executes on selection change in TimeName.
996function TimeName_Callback(hObject, eventdata, handles)
997Field=get(handles.get_field,'UserData');
998index=get(handles.SwitchVarIndexTime,'Value');
999MenuIndex=get(handles.TimeName,'Value');
1000string=get(handles.TimeName,'String');
1001TimeName='';%default
1002if ~isempty(string)&&iscell(string)
1003TimeName=string{MenuIndex};
1004end
1005switch index
1006    case 1
1007        set(handles.num_TimeDimension,'String','')
1008        set(handles.TimeUnit,'String','index')
1009    case 2
1010        set(handles.num_TimeDimension,'String','')
1011        attr_index=find(strcmpi([TimeName 'Unit'],Field.ListGlobalAttribute));% look for time unit
1012        if ~isempty(attr_index)
1013            AttrName=Field.ListGlobalAttribute{attr_index};
1014            set(handles.TimeUnit,'String',Field.(AttrName))
1015        else
1016            set(handles.TimeUnit,'String','')
1017        end
1018    case {3 ,4}
1019        if index==3  % TimeName is used to chose a variable
1020            VarIndex=name2index(TimeName,Field.ListVarName);
1021            DimName=Field.VarDimName{VarIndex};
1022            DimIndex=name2index(DimName,Field.ListDimName);
1023            DimValue=Field.DimValue(DimIndex);
1024            set(handles.num_TimeDimension,'String',num2str(DimValue))
1025            unit='';
1026            if isfield(Field,'VarAttribute')&& isfield(Field.VarAttribute{VarIndex},'Unit')
1027                unit=Field.VarAttribute{VarIndex}.Unit;
1028            end
1029            set(handles.TimeUnit,'String',unit)
1030            update_field(handles,TimeName)
1031        elseif index==4% TimeName is used to chose a dimension
1032            DimName=string{MenuIndex};
1033            DimIndex=name2index(DimName,Field.ListDimName);
1034            DimValue=Field.DimValue(DimIndex);
1035            set(handles.num_TimeDimension,'String',num2str(DimValue))
1036            set(handles.TimeUnit,'String','index')
1037        end
1038end
1039
1040
1041% --- Executes on button press in Check3D.
1042function Check3D_Callback(hObject, eventdata, handles)
1043if get(handles.Check3D,'Value')% 3D fields
1044    status='on';
1045else% fields studied as 2D
1046    status='off';
1047 
1048end
1049set(handles.Coord_z,'Visible',status)
1050% set(handles.CheckDimensionZ,'Visible',status)
1051set(handles.Z_title,'Visible',status)
1052set(handles.vector_z,'Visible',status)
1053set(handles.W_title,'Visible',status)   
1054if strcmp(status,'on')
1055   Field=get(handles.get_field,'UserData');
1056    if Field.MaxDim>=3% for 3D fields, propose to use the third variable as time
1057        menu=get(handles.SwitchVarIndexTime,'String');
1058        val=find(strcmp('variable',menu));
1059        if ~isempty(val)
1060            set(handles.SwitchVarIndexTime,'Value',val)
1061            SwitchVarIndexTime_Callback(handles.SwitchVarIndexTime,[], handles)
1062        end
1063    end
1064end
1065%------------------------------------------------------------------------
1066% --- Executes on button press in OK.
1067%------------------------------------------------------------------------
1068function OK_Callback(hObject, eventdata, handles)
1069handles.output=read_GUI(handles.get_field);
1070guidata(hObject, handles);% Update handles structure
1071uiresume(handles.get_field);
1072drawnow
1073% this function then activate get_field_OutputFcn
1074
1075%------------------------------------------------------------------------
1076% --- Executes when the GUI is closed by the mouse on upper right corner.
1077%------------------------------------------------------------------------
1078function closefcn(hObject, eventdata, handles)
1079handles.output=[];
1080guidata(hObject, handles);% Update handles structure
1081uiresume(handles.get_field);
1082drawnow
1083
1084%------------------------------------------------------------------------
1085% --- Outputs from this function are returned to the command line.
1086%------------------------------------------------------------------------
1087function varargout = get_field_OutputFcn(hObject, eventdata, handles)
1088
1089varargout{1} =handles.output;
1090delete(handles.get_field)
Note: See TracBrowser for help on using the repository browser.