source: trunk/src/get_field.m @ 983

Last change on this file since 983 was 977, checked in by g7moreau, 4 years ago
  • Update Copyright 2008-2017 notice
File size: 44.5 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-2017, 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.SwitchVarIndexTime,'UserData',ListSwitchVarIndexTime); % keep string in memory for check3D
161set(handles.get_field,'UserData',Field);% record the finput field structure
162SwitchVarIndexTime_Callback([], [], handles)
163
164%% set vector menu (priority) if detected or scalar menu for space dim >=2, or usual (x,y) plot for 1D fields
165set(handles.vector_x,'String',Field.Display.ListVarName)% fill the menu of x vector components
166set(handles.vector_y,'String',Field.Display.ListVarName)% fill the menu of y vector components
167set(handles.vector_z,'String',[{''} Field.Display.ListVarName])% fill the menu of y vector components
168set(handles.vec_color,'String',[{''} Field.Display.ListVarName])% fill the menu of y vector components
169set(handles.scalar,'Value',1)% fill the menu of y vector components
170set(handles.scalar,'String',Field.Display.ListVarName)% fill the menu for scalar
171set(handles.ordinate,'Value',1)% fill the menu of y vector components
172set(handles.ordinate,'String',Field.Display.ListVarName)% fill the menu of y coordinate for 1D plots
173checkseries=0;
174if isfield(ParamIn,'SeriesInput') && ParamIn.SeriesInput% case of call by series
175    set(handles.FieldOption,'value',1)
176    if isfield(Field,'Conventions')&& strcmp(Field.Conventions,'uvmat/civdata')
177    set(handles.FieldOption,'String',{'scalar';'vectors';'civdata...'})
178    else
179       set(handles.FieldOption,'String',{'scalar';'vectors'})
180    end
181    checkseries=1;
182    set(handles.scalar,'Max',2)
183elseif isfield(Field,'Conventions')&& strcmp(Field.Conventions,'uvmat/civdata')
184    set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors';'civdata...'})% provides the possibility to come back to civdata
185    set(handles.scalar,'Max',1)
186else
187    set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors'})
188    set(handles.scalar,'Max',1)
189end
190if Field.MaxDim>=2 && ~checkseries% case of 2D (or 3D) fields
191    check_vec_input=0;
192    if isfield(ParamIn,'vector_x')&& isfield(ParamIn,'vector_y')
193        ichoice_x=find(strcmp(ParamIn.vector_x,Field.Display.ListVarName),1);
194        ichoice_y=find(strcmp(ParamIn.vector_y,Field.Display.ListVarName),1);
195        if ~isempty(ichoice_x)&&~isempty(ichoice_y)
196            set(handles.vector_x,'UserData',ichoice_x)
197            set(handles.vector_y,'UserData',ichoice_y)
198            check_vec_input=1;
199        end
200    end
201    if ~check_vec_input && isfield(CellInfo{imax},'VarIndex_vector_x') &&  isfield(CellInfo{imax},'VarIndex_vector_y')
202        set(handles.vector_x,'UserData',CellInfo{imax}.VarIndex_vector_x(1))
203        set(handles.vector_y,'UserData',CellInfo{imax}.VarIndex_vector_y(1))
204        check_vec_input=1;
205    end
206    if check_vec_input
207        set(handles.FieldOption,'Value',3)% set vector selection option
208    else
209        set(handles.FieldOption,'Value',2)% set scalar selection option
210    end
211else % case of 1D fields
212    set(handles.FieldOption,'Value',1)
213end
214
215%% fill the general list of dimensions, variables, attributes
216if isfield(Field,'ListDimName')&&~isempty(Field.ListDimName)
217    Tabcell(:,1)=Field.ListDimName;
218    for iline=1:length(Field.ListDimName)
219        Tabcell{iline,2}=num2str(Field.DimValue(iline));
220    end
221    Tabchar=cell2tab(Tabcell,' = ');
222    set(handles.dimensions,'String',Tabchar)
223end
224
225%% fill menus for coordinates and time
226FieldOption_Callback(handles.variables,[], handles)% list the global attributes
227
228%% Make choices of coordinates from input
229if isfield(CellInfo{imax},'CoordIndex')
230    CoordIndex=CellInfo{imax}.CoordIndex;
231    if numel(CoordIndex)==2
232        YName=Field.ListVarName{CoordIndex(1)};
233        XName=Field.ListVarName{CoordIndex(2)};
234        ListCoord=get(handles.Coord_x,'String');
235        XIndex=find(strcmp(XName,ListCoord));
236        if ~isempty(XIndex)
237            set(handles.Coord_x,'Value',XIndex)
238        end
239        YIndex=find(strcmp(YName,ListCoord));
240        if ~isempty(YIndex)
241            set(handles.Coord_y,'Value',YIndex)
242        end
243    end
244end
245
246%% put the GUI on the lower right of the sceen
247set(hObject,'Unit','pixels')
248%pos_view_field=get(hObject,'Position');
249set(0,'Unit','pixels')
250ScreenSize=get(0,'ScreenSize');
251pos_view_field(3:4)=[955 648];
252pos_view_field(1)=ScreenSize(1)+ScreenSize(3)-pos_view_field(3);
253pos_view_field(2)=ScreenSize(2);
254set(hObject,'Position',pos_view_field)
255set(handles.get_field,'WindowStyle','modal')% Make the GUI modal
256if isfield(ParamIn,'Title')
257    set(hObject,'Name',ParamIn.Title)
258end
259
260%% set z coordinate menu if relevant
261if Field.MaxDim>=3 && prod(Field.DimValue)<10^8; % 3D field (with memory content smaller than 400 Mo)
262    set(handles.Check3D,'Value',1)
263else
264    set(handles.Check3D,'Value',0)
265end
266Check3D_Callback(hObject, eventdata, handles)
267set(handles.variables,'Value',1)
268set(handles.variables,'String',[{'*'} Field.ListVarName])
269variables_Callback(handles.variables,[], handles)% list the global attributes
270drawnow
271uiwait(handles.get_field);
272
273% -----------------------------------------------------------------------
274% --- Activated by selection in the list of variables
275% ----------------------------------------------------------------------
276function variables_Callback(hObject, eventdata, handles)
277
278Tabchar={''};%default
279Tabcell=[];
280hselect_field=get(handles.variables,'parent');
281Field=get(handles.get_field,'UserData');
282index=get(handles.variables,'Value');%index in the list 'variables'
283
284%% list global TimeAttribute names and values if index=1 (blank TimeVariable display) is selected
285if isequal(index,1)
286    set(handles.attributes_txt,'String','global attributes')
287    if isfield(Field,'ListGlobalAttribute') && ~isempty(Field.ListGlobalAttribute)
288        for iline=1:length(Field.ListGlobalAttribute)
289            Tabcell{iline,1}=Field.ListGlobalAttribute{iline};
290            if isfield(Field, Field.ListGlobalAttribute{iline})
291                val=Field.(Field.ListGlobalAttribute{iline});
292                if ischar(val);% attribute value is char string
293                    Tabcell{iline,2}=val;
294                elseif size(val,1)==1 %attribute value is a number or matlab vector
295                    Tabcell{iline,2}=num2str(val);
296                end
297            end
298        end
299        Tabchar=cell2tab(Tabcell,'=');
300    end
301    %% list Attribute names and values associated to the Variable # index-1
302else
303    list_var=get(handles.variables,'String');
304    if index>numel(list_var)
305        return
306    end
307    var_select=list_var{index};
308    set(handles.attributes_txt,'String', ['attributes of ' var_select])
309    if isfield(Field,'VarAttribute')&& length(Field.VarAttribute)>=index-1
310        %         nbline=0;
311        VarAttr=Field.VarAttribute{index-1};
312        if isstruct(VarAttr)
313            attr_list=fieldnames(VarAttr);
314            for iline=1:length(attr_list)
315                Tabcell{iline,1}=attr_list{iline};
316                val=VarAttr.(attr_list{iline}) ;
317                if ischar(val);
318                    Tabcell{iline,2}=val;
319                else
320                    Tabcell{iline,2}=num2str(val);
321                end
322            end
323        end
324    end
325end
326if ~isempty(Tabcell)
327    Tabchar=cell2tab(Tabcell,'=');
328    %     Tabchar=[{''};Tabchar];
329end
330set(handles.attributes,'Value',1);% select the first item
331set(handles.attributes,'String',Tabchar);
332
333%% update dimensions;
334if isfield(Field,'ListDimName')
335    Tabdim={};%default
336    if isequal(index,1)%list all dimensions
337        dim_indices=1:length(Field.ListDimName);
338        set(handles.dimensions_txt,'String', 'dimensions')
339    else
340        DimCell=Field.VarDimName{index-1};
341        if ischar(DimCell)
342            DimCell={DimCell};
343        end
344        dim_indices=[];
345        for idim=1:length(DimCell)
346            dim_index=strcmp(DimCell{idim},Field.ListDimName);%vector with size of Field.ListDimName, =0
347            dim_index=find(dim_index,1);
348            dim_indices=[dim_indices dim_index];
349        end
350        set(handles.dimensions_txt,'String', ['dimensions of ' var_select])
351    end
352    for iline=1:length(dim_indices)
353        Tabdim{iline,1}=Field.ListDimName{dim_indices(iline)};
354        Tabdim{iline,2}=num2str(Field.DimValue(dim_indices(iline)));
355    end
356    Tabchar=cell2tab(Tabdim,' = ');
357    Tabchar=[{''} ;Tabchar];
358    set(handles.dimensions,'Value',1)
359    set(handles.dimensions,'String',Tabchar)
360end
361
362%------------------------------------------------------------------------
363% --- Executes on selection change in FieldOption.
364%------------------------------------------------------------------------
365function FieldOption_Callback(hObject, eventdata, handles)
366
367Field=get(handles.get_field,'UserData');
368FieldList=get(handles.FieldOption,'String');
369FieldOption=FieldList{get(handles.FieldOption,'Value')};
370switch FieldOption
371   
372    case '1D plot'
373        set(handles.Coordinates,'Visible','on')
374        set(handles.PanelOrdinate,'Visible','on')
375        pos=get(handles.PanelOrdinate,'Position');
376        pos(1)=2;
377        pos_coord=get(handles.Coordinates,'Position');
378        pos(2)=pos_coord(2)-pos(4)-2;
379        set(handles.PanelOrdinate,'Position',pos)
380        set(handles.PanelScalar,'Visible','off')
381        set(handles.PanelVectors,'Visible','off')
382        set(handles.Coord_y,'Visible','off')
383        set(handles.Y_title,'Visible','off')
384        set(handles.Coord_z,'Visible','off')
385        set(handles.Z_title,'Visible','off')
386        ordinate_Callback(hObject, eventdata, handles)
387       
388    case {'scalar'}
389        set(handles.Coordinates,'Visible','on')
390        set(handles.PanelOrdinate,'Visible','off')
391        set(handles.PanelScalar,'Visible','on')
392        set(handles.PanelVectors,'Visible','off')
393        pos=get(handles.PanelScalar,'Position');
394        pos(1)=2;
395        pos_coord=get(handles.Coordinates,'Position');
396        pos(2)=pos_coord(2)-pos(4)-2;
397        set(handles.PanelScalar,'Position',pos)
398        set(handles.Coord_y,'Visible','on')
399        set(handles.Y_title,'Visible','on')
400        %default scalar selection
401        test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
402        for ilist=1:numel(Field.Display.VarDimName)
403            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
404                Role=Field.Display.VarAttribute{ilist}.Role;
405                if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
406                    test_coord(ilist)=1;
407                end
408            end
409            dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
410            if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
411                test_coord(ilist)=1;
412            end
413        end
414        scalar_index=find(~test_coord,1);%get the first variable not a coordinate
415        if isempty(scalar_index)
416            set(handles.scalar,'Value',1)
417        else
418            set(handles.scalar,'Value',scalar_index)
419        end       
420        scalar_Callback(hObject, eventdata, handles)
421             
422    case 'vectors'
423        set(handles.PanelVectors,'Visible','on')
424        set(handles.Coordinates,'Visible','on')
425        set(handles.PanelOrdinate,'Visible','off')
426        set(handles.PanelScalar,'Visible','off')
427        pos=get(handles.PanelVectors,'Position');
428        pos(1)=2;
429        pos_coord=get(handles.Coordinates,'Position');
430        pos(2)=pos_coord(2)-pos(4)-2;
431        set(handles.PanelVectors,'Position',pos)
432        set(handles.Coord_y,'Visible','on')
433        set(handles.Y_title,'Visible','on')
434        %default vector selection
435        vector_x_value=get(handles.vector_x,'UserData');
436        vector_y_value=get(handles.vector_y,'UserData');
437        if ~isempty(vector_x_value)&&~isempty(vector_y_value)
438            set(handles.vector_x,'Value',vector_x_value)
439            set(handles.vector_y,'Value',vector_y_value)
440        else
441            test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordinate
442            for ilist=1:numel(Field.Display.VarDimName)
443                if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
444                    Role=Field.Display.VarAttribute{ilist}.Role;
445                    if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
446                        test_coord(ilist)=1;
447                    end
448                end
449                dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
450                if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
451                    test_coord(ilist)=1;
452                end
453            end
454            vector_index=find(~test_coord,2);%get the two first variables not a coordinate
455            if isempty(vector_index)
456                set(handles.vector_x,'Value',1)
457                set(handles.vector_y,'Value',2)
458            else
459                set(handles.vector_x,'Value',vector_index(1))
460                set(handles.vector_y,'Value',vector_index(2))
461            end
462        end
463        vector_Callback(handles)
464       
465    case 'civdata...'
466        set(handles.PanelOrdinate,'Visible','off')
467        set(handles.PanelScalar,'Visible','off')
468        set(handles.PanelVectors,'Visible','off')
469        set(handles.Coordinates,'Visible','off')
470end
471
472%------------------------------------------------------------------------
473function ordinate_Callback(hObject, eventdata, handles)
474%------------------------------------------------------------------------
475Field=get(handles.get_field,'UserData');
476y_index=get(handles.ordinate,'Value');
477y_menu=get(handles.ordinate,'String');
478if isempty(y_menu)
479    return
480else
481YName=y_menu{y_index};
482end
483
484%% set list of possible coordinates
485test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
486test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
487ListCoord={''};
488dim_var=Field.Display.VarDimName{y_index};%list of dimensions of the selected variable
489
490for ilist=1:numel(Field.Display.VarDimName)
491    dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
492    if isequal(dimnames,dim_var)
493        test_component(ilist)=1;
494    elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
495        test_coord(ilist)=1;
496    end
497end
498var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
499var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
500ListCoord=Field.Display.ListVarName([var_component var_coord]);
501
502%% set default coord selection
503if numel(find(test_coord))>3
504     SwitchVarIndexTime=get(handles.SwitchVarIndexTime,'String');
505    if numel(SwitchVarIndexTime)<3
506        SwitchVarIndexTime=[SwitchVarIndexTime;'matrix_index'];
507        set(handles.SwitchVarIndexTime,'String',SwitchVarIndexTime)
508    end
509    set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
510    SwitchVarIndexTime_Callback([], [], handles)
511end
512if numel(var_component)<2
513    if numel(test_coord)<2
514        ListCoord={''};
515    else
516        set(handles.Coord_x,'Value',2)
517        set(handles.Coord_y,'Value',1)
518    end
519else
520    coord_val=1;
521    for ilist=1:numel(var_component)
522        ivar=var_component(ilist);
523        if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
524            Role=Field.Display.VarAttribute{ivar}.Role;
525            if strcmp(Role,'coord_x')
526                coord_val=ilist;
527            end
528        end
529    end
530    set(handles.Coord_x,'Value',coord_val)
531end
532set(handles.Coord_x,'String',ListCoord)
533
534
535%% set list of time coordinates
536menu=get(handles.SwitchVarIndexTime,'String');
537TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
538switch TimeOption
539    case 'variable'
540        if numel(find(test_coord))<3
541            ListTime={''};
542        else
543            ListTime=Field.Display.ListVarName(find(test_coord,end));
544        end
545        set(handles.TimeName,'Value',1)
546        set(handles.TimeName,'String',ListTime)
547    case 'matrix index'
548        if numel(find(test_coord))<3
549            ListTime={''};
550        else
551            ListTime=Field.Display.VarDimName{find(test_coord,end)};
552        end
553        set(handles.TimeName,'Value',1)
554        set(handles.TimeName,'String',ListTime)
555end 
556update_field(handles,YName)
557         
558%------------------------------------------------------------------------
559% --- Executes on selection change in scalar menu.
560%------------------------------------------------------------------------
561function scalar_Callback(hObject, eventdata, handles)
562
563Field=get(handles.get_field,'UserData');
564scalar_index=get(handles.scalar,'Value');
565scalar_menu=get(handles.scalar,'String');
566ScalarName=scalar_menu{scalar_index};
567
568%% set list of possible coordinates
569test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
570test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
571dim_var=Field.Display.VarDimName{scalar_index};%list of dimensions of the selected variable
572if ~get(handles.CheckDimensionX,'Value') 
573    %look for coordinate variables among the other variables
574    for ilist=1:numel(Field.Display.VarDimName)
575        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
576        if isequal(dimnames,dim_var)
577            test_component(ilist)=1;% the listed variable has the same dimension as the selected scalar-> possibly chosen as unstructured coordinate
578        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var), 1))%variable ilist is a 1D array which can be coordinate variable
579            test_coord(ilist)=1;
580        end
581    end
582end
583var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
584var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
585var_coord(var_coord==scalar_index)=[];
586var_component(var_component==scalar_index)=[];
587ListCoord=Field.Display.ListVarName([var_coord var_component]);
588
589%% set default coord selection
590% if numel(find(test_coord))>3
591%     SwitchVarIndexTime=get(handles.SwitchVarIndexTime,'String');
592%     if numel(SwitchVarIndexTime)<3
593%         SwitchVarIndexTime=[SwitchVarIndexTime;'matrix_index'];
594%         set(handles.SwitchVarIndexTime,'String',SwitchVarIndexTime)
595%     end
596%     set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
597%     SwitchVarIndexTime_Callback([], [], handles)
598% end
599
600coord_val=[0 0];
601% look for labelled unstructured coordinates
602for ilist=1:numel(var_component)
603    ivar=var_component(ilist);
604    if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
605        Role=Field.Display.VarAttribute{ivar}.Role;
606        if strcmp(Role,'coord_x')
607            coord_val(1)=ilist;
608        elseif strcmp(Role,'coord_y')
609            coord_val(2)=ilist;
610            elseif strcmp(Role,'coord_z')
611            coord_val(3)=ilist;
612        end
613    end
614end
615if numel(find(coord_val))<2
616    if numel(var_coord)>=3
617         coord_val=[3 2 1];
618    elseif numel(var_coord)>=2
619       % coord_val=[numel(var_component)+2 numel(var_component)+1];
620       coord_val=[2 1];
621    else
622        coord_val=[1 2];
623    end
624end
625if  get(handles.CheckDimensionX,'Value')
626    set(handles.Coord_x,'Value',2)
627    set(handles.Coord_x,'String',dim_var')
628else
629    set(handles.Coord_x,'Value',coord_val(1))
630    set(handles.Coord_x,'String',ListCoord)
631end
632if  get(handles.CheckDimensionX,'Value')
633    set(handles.Coord_y,'Value',1)
634    set(handles.Coord_y,'String',dim_var')
635else
636    set(handles.Coord_y,'Value',coord_val(2))
637    set(handles.Coord_y,'String',ListCoord)
638end
639if  get(handles.CheckDimensionX,'Value')
640    set(handles.Coord_z,'Value',1)
641    set(handles.Coord_z,'String',dim_var')
642else
643    set(handles.Coord_z,'Value',coord_val(2))
644    set(handles.Coord_z,'String',ListCoord)
645end
646
647%% set list of time coordinates
648menu=get(handles.SwitchVarIndexTime,'String');
649TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
650switch TimeOption
651    case 'variable'
652        if numel(find(test_coord))<3
653            ListTime={''};
654        else
655            ListTime=Field.Display.ListVarName(find(test_coord,end));
656        end
657        set(handles.TimeName,'Value',1)
658        set(handles.TimeName,'String',ListTime)
659    case 'dim index'
660        if numel(find(test_coord))<3
661            ListTime={''};
662        else
663            ListTime=Field.Display.VarDimName{find(test_coord,end)};
664        end
665        set(handles.TimeName,'Value',1)
666        set(handles.TimeName,'String',ListTime)
667end 
668update_field(handles,ScalarName)
669
670% --- Executes on button press in check_rgb.
671function check_rgb_Callback(hObject, eventdata, handles)
672
673
674%------------------------------------------------------------------------
675% --- Executes on selection change in vector_x.
676%------------------------------------------------------------------------
677function vector_x_Callback(hObject, eventdata, handles)
678
679vector_x_menu=get(handles.vector_x,'String');
680vector_x_index=get(handles.vector_x,'Value');
681vector_x=vector_x_menu{vector_x_index};
682vector_Callback(handles)
683update_field(handles,vector_x)
684
685%------------------------------------------------------------------------
686% --- Executes on selection change in vector_x.
687function vector_y_Callback(hObject, eventdata, handles)
688%------------------------------------------------------------------------
689vector_y_menu=get(handles.vector_x,'String');
690vector_y_index=get(handles.vector_x,'Value');
691vector_y=vector_y_menu{vector_y_index};
692vector_Callback(handles)
693update_field(handles,vector_y)
694
695%------------------------------------------------------------------------
696% --- Executes on selection change in vector_z.
697function vector_z_Callback(hObject, eventdata, handles)
698%------------------------------------------------------------------------
699vector_z_menu=get(handles.vector_z,'String');
700vector_z_index=get(handles.vector_z,'Value');
701vector_z=vector_z_menu{vector_z_index};
702vector_Callback(handles)
703update_field(handles,vector_z)
704
705%------------------------------------------------------------------------
706% --- Executes on selection change in vec_color.
707function vec_color_Callback(hObject, eventdata, handles)
708%------------------------------------------------------------------------
709index=get(handles.vec_color,'Value');
710string=get(handles.vec_color,'String');
711VarName=string{index};
712vector_Callback(handles)
713update_field(handles,VarName)
714
715%------------------------------------------------------------------------
716% --- Executes on selection change in vector_x or vector_y
717function vector_Callback( handles)
718%------------------------------------------------------------------------
719Field=get(handles.get_field,'UserData');
720vector_x_index=get(handles.vector_x,'Value');
721vector_y_index=get(handles.vector_y,'Value');
722vec_color_index=get(handles.vec_color,'Value');
723
724%% set list of possible coordinates
725test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
726test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordinate
727check_consistent=1;%check that the selected vector components (and possibly color var) have the same dimensiosn
728ListCoord={''};
729dim_var=Field.Display.VarDimName{vector_x_index};%list of dimensions of the selected variable
730if ~isequal(dim_var,Field.Display.VarDimName{vector_y_index})
731    check_consistent=0;
732elseif vec_color_index~=1 && ~isequal(dim_var,Field.Display.VarDimName{vec_color_index})
733    check_consistent=0;
734end
735% the two vector components have consistent dimensions
736if check_consistent
737    for ilist=1:numel(Field.Display.VarDimName)
738        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
739        if isequal(dimnames,dim_var)
740            test_component(ilist)=1;
741        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
742            test_coord(ilist)=1;
743        end
744    end
745    var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
746    var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
747    var_component(var_component==vector_x_index|var_component==vector_y_index)=[];
748    var_coord(var_coord==vector_x_index|var_coord==vector_y_index)=[];% remove vector components form te possible list of coordinates
749    ListCoord=Field.Display.ListVarName([var_coord var_component]);
750   
751    %% set default coord selection
752    if numel(find(test_coord))>3
753        set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
754    end
755    if numel(var_component)<2
756        if numel(find(test_coord))<2
757            ListCoord={''};
758        else
759            if numel(find(test_coord))>=3
760                set(handles.Coord_x,'Value',3)
761                set(handles.Coord_y,'Value',2)
762                set(handles.Coord_z,'Value',1)
763            else
764                set(handles.Coord_x,'Value',2)
765                set(handles.Coord_y,'Value',1)
766            end
767        end
768    else
769        coord_val=[0 0 0];
770        for ilist=1:numel(var_component)
771            ivar=var_component(ilist);
772            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
773                Role=Field.Display.VarAttribute{ivar}.Role;
774                if strcmp(Role,'coord_x')
775                    coord_val(1)=ilist;
776                elseif strcmp(Role,'coord_y')
777                    coord_val(2)=ilist;
778                elseif strcmp(Role,'coord_z')
779                    coord_val(3)=ilist;
780                end
781            end
782        end
783        if isempty(find(coord_val))
784            coord_val=var_coord;% case of dimension coordinates
785        end
786        if numel(find(coord_val))<2
787            %coord_val=[numel(var_component)+2 numel(var_component)+1];
788            coord_val=[1 2 3];
789        end
790        set(handles.Coord_x,'Value',coord_val(1))
791        set(handles.Coord_y,'Value',coord_val(2))
792        if numel(coord_val)>=3
793            set(handles.Coord_z,'Value',coord_val(3))
794        end
795    end
796end
797set(handles.Coord_z,'String',ListCoord)
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%------------------------------------------------------------------------
862
863function SwitchVarIndexTime_Callback(hObject, eventdata, handles)
864
865Field=get(handles.get_field,'UserData');
866menu=get(handles.SwitchVarIndexTime,'String');
867option=menu{get(handles.SwitchVarIndexTime,'Value')};
868
869switch option
870    case 'file index'
871        set(handles.TimeName, 'Visible','off')% the time is taken as the file index
872    case 'attribute'
873        set(handles.TimeName, 'Visible','on')% timeName menu represents the available attributes
874        time_index=get(handles.TimeName,'UserData');    %select the input data
875        if isempty(time_index)
876            PreviousList=get(handles.TimeName, 'String');
877            if ~isempty(PreviousList)
878                PreviousAttr=PreviousList{get(handles.TimeName, 'Value')};
879                index=find(strcmp(PreviousAttr,Field.Display.ListGlobalAttributes),1);
880            end
881        end
882        if isempty(time_index)
883            time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);% index of the attributes containing the string 'Time'
884        end     
885        if ~isempty(time_index)
886            set(handles.TimeName,'Value',time_index)
887        else
888            set(handles.TimeName,'Value',1)
889        end
890        set(handles.TimeName, 'String',Field.Display.ListGlobalAttribute)
891
892    case 'variable'% TimeName menu represents the available variables
893        set(handles.TimeName, 'Visible','on')
894        VarNbDim=cellfun('length',Field.Display.VarDimName); % check the nbre of dimensions of each input variable
895        TimeVarName=Field.Display.ListVarName(VarNbDim==1);% list of variables with a single dimension (candidate for time)
896        List=get(handles.TimeName,'String');% list of names on the menu for time
897        if isempty(List)
898            ind=1;
899        else
900            option=List{get(handles.TimeName,'Value')};% previous selected option
901            ind=find(strcmp(option,TimeVarName)); %check whether the previous selection is available in the newlist
902            if isempty(ind)
903                ind=1;
904            end
905        end
906        if ~isempty(TimeVarName)
907            set(handles.TimeName, 'Value',ind);% select first value in the menu if the option is not found
908            set(handles.TimeName, 'String',TimeVarName)% update the menu for time name
909        end
910    case 'matrix index'% TimeName menu represents the available dimensions
911        set(handles.TimeName, 'Visible','on')     
912        set(handles.TimeName, 'Value',1);
913        set(handles.TimeName, 'String',Field.Display.ListDimName)
914end
915TimeName_Callback(hObject, [], handles)
916
917%-----------------------------------------------------------------------
918function update_field(handles,VarName)
919%-----------------------------------------------------------------------
920Field=get(handles.get_field,'UserData');
921index=name2index(VarName,Field.ListVarName);
922if ~isempty(index)
923    set(handles.variables,'Value',index+1)
924    variables_Callback(handles.variables, [], handles)
925end
926
927%------------------------------------------------------------------------
928% --- give index numbers of the strings str in the list ListvarName
929% -----------------------------------------------------------------------
930function VarIndex_y=name2index(cell_str,ListVarName)
931
932VarIndex_y=[];
933if ischar(cell_str)
934    for ivar=1:length(ListVarName)
935        varlist=ListVarName{ivar};
936        if isequal(varlist,cell_str)
937            VarIndex_y= ivar;
938            break
939        end
940    end
941elseif iscell(cell_str)
942    for isel=1:length(cell_str)
943        varsel=cell_str{isel};
944        for ivar=1:length(ListVarName)
945            varlist=ListVarName{ivar};
946            if isequal(varlist,varsel)
947                VarIndex_y=[VarIndex_y ivar];
948            end
949        end
950    end
951end
952
953% --- Executes on button press in CheckDimensionY.
954function CheckDimensionX_Callback(hObject, eventdata, handles)
955CheckDimensionX=get(handles.CheckDimensionX,'value')
956if CheckDimensionX
957    set(handles.Coordinates,'visible','off')
958else
959    set(handles.Coordinates,'visible','on')
960end
961% FieldList=get(handles.FieldOption,'String');
962% FieldOption=FieldList{get(handles.FieldOption,'Value')};
963% switch FieldOption
964%     case '1D plot'
965%         
966%     case {'scalar'}
967%        scalar_Callback(hObject, eventdata, handles)
968%     case 'vectors'
969% end
970
971% % --- Executes on button press in CheckDimensionY.
972% function CheckDimensionY_Callback(hObject, eventdata, handles)
973% FieldList=get(handles.FieldOption,'String');
974% FieldOption=FieldList{get(handles.FieldOption,'Value')};
975% switch FieldOption
976%     case '1D plot'
977%         
978%     case {'scalar','pick variables'}
979%        scalar_Callback(hObject, eventdata, handles)
980%     case 'vectors'
981% end
982%
983%
984% % --- Executes on button press in CheckDimensionZ.
985% function CheckDimensionZ_Callback(hObject, eventdata, handles)
986% FieldList=get(handles.FieldOption,'String');
987% FieldOption=FieldList{get(handles.FieldOption,'Value')};
988% switch FieldOption
989%     case '1D plot'
990%         
991%     case 'scalar'
992%        scalar_Callback(hObject, eventdata, handles)
993%     case 'vectors'
994% end
995
996% --- Executes on selection change in TimeName.
997function TimeName_Callback(hObject, eventdata, handles)
998Field=get(handles.get_field,'UserData');
999index=get(handles.SwitchVarIndexTime,'Value');
1000MenuIndex=get(handles.TimeName,'Value');
1001string=get(handles.TimeName,'String');
1002TimeName='';%default
1003if ~isempty(string)&&iscell(string)
1004TimeName=string{MenuIndex};
1005end
1006switch index
1007    case 1
1008        set(handles.num_TimeDimension,'String','')
1009        set(handles.TimeUnit,'String','index')
1010    case 2
1011        set(handles.num_TimeDimension,'String','')
1012        attr_index=find(strcmpi([TimeName 'Unit'],Field.ListGlobalAttribute));% look for time unit
1013        if ~isempty(attr_index)
1014            AttrName=Field.ListGlobalAttribute{attr_index};
1015            set(handles.TimeUnit,'String',Field.(AttrName))
1016        else
1017            set(handles.TimeUnit,'String','')
1018        end
1019    case {3 ,4}
1020        if index==3  % TimeName is used to chose a variable
1021            VarIndex=name2index(TimeName,Field.ListVarName);
1022            DimName=Field.VarDimName{VarIndex};
1023            DimIndex=name2index(DimName,Field.ListDimName);
1024            DimValue=Field.DimValue(DimIndex);
1025            set(handles.num_TimeDimension,'String',num2str(DimValue))
1026            unit='';
1027            if isfield(Field,'VarAttribute')&& isfield(Field.VarAttribute{VarIndex},'Unit')
1028                unit=Field.VarAttribute{VarIndex}.Unit;
1029            end
1030            set(handles.TimeUnit,'String',unit)
1031            update_field(handles,TimeName)
1032        elseif index==4% TimeName is used to chose a dimension
1033            DimName=string{MenuIndex};
1034            DimIndex=name2index(DimName,Field.ListDimName);
1035            DimValue=Field.DimValue(DimIndex);
1036            set(handles.num_TimeDimension,'String',num2str(DimValue))
1037            set(handles.TimeUnit,'String','index')
1038        end
1039end
1040
1041
1042% --- Executes on button press in Check3D.
1043function Check3D_Callback(hObject, eventdata, handles)
1044if get(handles.Check3D,'Value')% 3D fields
1045    status='on';
1046else% fields studied as 2D
1047    status='off';
1048end
1049
1050set(handles.Coord_z,'Visible',status)
1051% set(handles.CheckDimensionZ,'Visible',status)
1052set(handles.Z_title,'Visible',status)
1053set(handles.vector_z,'Visible',status)
1054set(handles.W_title,'Visible',status)
1055if strcmp(status,'on')% ask for 3D input   
1056    Field=get(handles.get_field,'UserData');
1057    if Field.MaxDim>3% for 4D fields, propose to use the fourth variable as time
1058        %set(handles.Time,'Visible','on')
1059        menu=get(handles.SwitchVarIndexTime,'String');
1060        val=find(strcmp('variable',menu));
1061        if ~isempty(val)
1062            set(handles.SwitchVarIndexTime,'Value',val)
1063        end
1064    else
1065        set(handles.SwitchVarIndexTime,'Value',1)
1066        set(handles.SwitchVarIndexTime,'String',{'file index'})
1067    end
1068else
1069   set(handles.SwitchVarIndexTime,'String',get(handles.SwitchVarIndexTime,'UserData'))
1070end
1071SwitchVarIndexTime_Callback(handles.SwitchVarIndexTime,[], handles)
1072
1073%------------------------------------------------------------------------
1074% --- Executes on button press in OK.
1075%------------------------------------------------------------------------
1076function OK_Callback(hObject, eventdata, handles)
1077handles.output=read_GUI(handles.get_field);
1078guidata(hObject, handles);% Update handles structure
1079uiresume(handles.get_field);
1080drawnow
1081% this function then activate get_field_OutputFcn
1082
1083%------------------------------------------------------------------------
1084% --- Executes when the GUI is closed by the mouse on upper right corner.
1085%------------------------------------------------------------------------
1086function closefcn(hObject, eventdata, handles)
1087handles.output=[];
1088guidata(hObject, handles);% Update handles structure
1089uiresume(handles.get_field);
1090drawnow
1091
1092%------------------------------------------------------------------------
1093% --- Outputs from this function are returned to the command line.
1094%------------------------------------------------------------------------
1095function varargout = get_field_OutputFcn(hObject, eventdata, handles)
1096
1097varargout{1} =handles.output;
1098delete(handles.get_field)
Note: See TracBrowser for help on using the repository browser.