source: trunk/src/get_field.m @ 874

Last change on this file since 874 was 874, checked in by sommeria, 9 years ago

merge_proj corrected + various bugs

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