source: trunk/src/get_field.m @ 1009

Last change on this file since 1009 was 1009, checked in by sommeria, 7 years ago

various bugs fixed

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