source: trunk/src/get_field.m @ 899

Last change on this file since 899 was 890, checked in by sommeria, 10 years ago

bugs solved for reading general netcdf files

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