source: trunk/src/get_field.m @ 787

Last change on this file since 787 was 775, checked in by sommeria, 10 years ago

different bug corrections in series (time display for netcdf files)

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