source: trunk/src/get_field.m @ 754

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