Index: trunk/src/get_field.m
===================================================================
--- trunk/src/get_field.m	(revision 672)
+++ trunk/src/get_field.m	(revision 674)
@@ -1,6 +1,18 @@
 %'get_field': display variables and attributes from a Netcdf file, and OK selected fields
 %------------------------------------------------------------------------
-%function varargout = get_field(varargin)
+% GetFieldData=get_field(FileName,ParamIn)
 % associated with the GUI get_field.fig
+%
+% OUPUT:
+% GetFieldData: structure containing the information on the selected
+%      fields, obtained by applying the fct red_GUI to the GUI get_field
+%   .FieldOption='vectors': variables are used for vector plot
+%                  'scalar': variables are used for scalar plot, 
+%                  '1Dplot': variables are used for usual x-y plot,
+%                  'civdata...': go back to automatic reading of civ data
+%   .PanelVectors: sub-structure variables used as vector components
+%   .PanelScalar:
+% INPUT:
+% FileName: name (including path) of the netcdf file to open
 %
 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
@@ -42,5 +54,5 @@
 end
 % End initialization code - DO NOT EDIT
-
+      
 %------------------------------------------------------------------------
 % --- Executes just before get_field is made visible.
@@ -54,15 +66,19 @@
 
 %% enter input data
-set(handles.inputfile,'String',filename)% fill the input file name
-Field=nc2struct(filename,[]);% reads the  field structure, without the variables
+if ischar(filename)% input file name
+    set(handles.inputfile,'String',filename)% fill the input file name
+    Field=nc2struct(filename,[]);% reads the  field structure, without the variables
+else
+    'bad input to get_field'
+end
+if ~exist('ParamIn','var')
+    ParamIn=[];
+end
 if isfield(Field,'Txt')
-    msgbox_uvmat('ERROR',['get_field/nc2struct/' Field.Txt])% display error message for input fiel reading
+    msgbox_uvmat('ERROR',['get_field/nc2struct/' Field.Txt])% display error message for input file reading
     return
 end
 if ~isfield(Field,'ListVarName')
     return
-end
-if ~exist('ParamIn','var')
-    ParamIn=[];
 end
 
@@ -111,10 +127,8 @@
 ListSwitchVarIndexTime={'file index'};% default setting: the time is the file index
 % look at global attributes with numerical values
-check_numvalue=false;
-check_time=false;
+check_numvalue=false(1,numel(Field.ListGlobalAttribute));
 for ilist=1:numel(Field.ListGlobalAttribute)
     Value=Field.(Field.ListGlobalAttribute{ilist});
     check_numvalue(ilist)=isnumeric(Value);
-    check_time(ilist)=~isempty(find(regexp(Field.ListGlobalAttribute{ilist},'Time'),1));
 end
 Field.Display.ListGlobalAttribute=Field.ListGlobalAttribute(check_numvalue);% select the attributes with float numerical value
@@ -122,10 +136,17 @@
     ListSwitchVarIndexTime=[ListSwitchVarIndexTime; {'attribute'}];% the time can be chosen as a global attribute
 end
-nboption=numel(ListSwitchVarIndexTime);
 if Field.MaxDim>=2
     ListSwitchVarIndexTime=[ListSwitchVarIndexTime;{'variable'};{'dim index'}];% the time can be chosen as a dim index
 end
-if ~isempty(find(check_time, 1))
+
+%% select the Time attribute from input
+if isfield(ParamIn,'TimeAttrName')
+    time_index=find(strcmp(ParamIn.TimeAttrName,Field.Display.ListGlobalAttribute),1);
+else
+    time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);
+end
+if ~isempty(time_index)
     set(handles.SwitchVarIndexTime,'Value',2);
+    set(handles.TimeName,'UserData',time_index)
 else
     set(handles.SwitchVarIndexTime,'Value',1);
@@ -150,11 +171,23 @@
 end
 if Field.MaxDim>=2 % case of 2D (or 3D) fields
-    if isfield(CellInfo{imax},'VarIndex_vector_x') &&  isfield(CellInfo{imax},'VarIndex_vector_y')
+    check_vec_input=0;
+    if isfield(ParamIn,'vector_x')&& isfield(ParamIn,'vector_y')
+        ichoice_x=find(strcmp(ParamIn.vector_x,Field.Display.ListVarName),1);
+        ichoice_y=find(strcmp(ParamIn.vector_y,Field.Display.ListVarName),1);
+        if ~isempty(ichoice_x)&&~isempty(ichoice_y)
+            set(handles.vector_x,'UserData',ichoice_x)
+            set(handles.vector_y,'UserData',ichoice_y)
+            check_vec_input=1;
+        end
+    end
+    if ~check_vec_input && isfield(CellInfo{imax},'VarIndex_vector_x') &&  isfield(CellInfo{imax},'VarIndex_vector_y')
+        set(handles.vector_x,'UserData',CellInfo{imax}.VarIndex_vector_x(1))
+        set(handles.vector_y,'UserData',CellInfo{imax}.VarIndex_vector_y(1))
+        check_vec_input=1;
+    end
+    if check_vec_input
         set(handles.FieldOption,'Value',3)% set vector selection option
-        set(handles.vector_x,'Value',CellInfo{imax}.VarIndex_vector_x(1))
-        set(handles.vector_y,'Value',CellInfo{imax}.VarIndex_vector_y(1))
-        set(handles.FieldOption,'Value',3)
     else
-        set(handles.FieldOption,'Value',2)
+        set(handles.FieldOption,'Value',2)% set scalar selection option
     end
 else % case of 1D fields
@@ -177,8 +210,4 @@
 %% fill menus for coordinates and time
 FieldOption_Callback(handles.variables,[], handles)% list the global attributes
-%     if isfield(CellInfo{imax},'VarIndex_coord_x')&&  isfield(CellInfo{imax},'VarIndex_coord_y')
-%         set(handles.Coord_x,'Value',CellInfo{imax}.VarIndex_coord_x(1))
-%         set(handles.Coord_y,'Value',CellInfo{imax}.VarIndex_coord_y(1))
-%     end
 
 %% Make choices of coordinates from input
@@ -214,6 +243,4 @@
 return
 
-SwitchVarIndexTime_Callback([],[], handles)
-
 %% set z coordinate menu if relevant
 if Field.MaxDim>=3
@@ -227,4 +254,9 @@
     set(handles.Coord_z,'Visible','off')
     set(handles.Z_title,'Visible','off')
+end
+
+%% make selections according to ParamIn
+if isfield(ParamIn,'vector_x') && isfield(ParamIn,'vector_y')
+    
 end
 
@@ -300,5 +332,5 @@
             for iline=1:length(attr_list)
                 Tabcell{iline,1}=attr_list{iline};
-                eval(['val=VarAttr.' attr_list{iline} ';'])
+                val=VarAttr.(attr_list{iline}) ;
                 if ischar(val);
                     Tabcell{iline,2}=val;
@@ -406,10 +438,10 @@
         end       
         scalar_Callback(hObject, eventdata, handles)
-        
+             
     case 'vectors'
+        set(handles.PanelVectors,'Visible','on')
         set(handles.Coordinates,'Visible','on')
         set(handles.PanelOrdinate,'Visible','off')
         set(handles.PanelScalar,'Visible','off')
-        set(handles.PanelVectors,'Visible','on')
         pos=get(handles.PanelVectors,'Position');
         pos(1)=2;
@@ -420,25 +452,32 @@
         set(handles.Y_title,'Visible','on')
         %default vector selection
-        test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
-        for ilist=1:numel(Field.Display.VarDimName)
-            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
-                Role=Field.Display.VarAttribute{ilist}.Role;
-                if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
+        vector_x_value=get(handles.vector_x,'UserData');
+        vector_y_value=get(handles.vector_y,'UserData');
+        if ~isempty(vector_x_value)&&~isempty(vector_y_value)
+            set(handles.vector_x,'Value',vector_x_value)
+            set(handles.vector_y,'Value',vector_y_value)
+        else
+            test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordinate
+            for ilist=1:numel(Field.Display.VarDimName)
+                if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
+                    Role=Field.Display.VarAttribute{ilist}.Role;
+                    if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
+                        test_coord(ilist)=1;
+                    end
+                end
+                dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
+                if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
                     test_coord(ilist)=1;
                 end
             end
-            dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
-            if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
-                test_coord(ilist)=1;
+            vector_index=find(~test_coord,2);%get the two first variables not a coordinate
+            if isempty(vector_index)
+                set(handles.vector_x,'Value',1)
+                set(handles.vector_y,'Value',2)
+            else
+                set(handles.vector_x,'Value',vector_index(1))
+                set(handles.vector_y,'Value',vector_index(2))
             end
         end
-        vector_index=find(~test_coord,2);%get the first variable not a coordiante
-        if isempty(vector_index)
-            set(handles.vector_x,'Value',1)
-            set(handles.vector_y,'Value',2)
-        else
-            set(handles.vector_x,'Value',vector_index(1))
-            set(handles.vector_y,'Value',vector_index(2))
-        end       
         vector_Callback(handles)
         
@@ -525,5 +564,5 @@
 end  
 update_field(handles,YName)
-
+         
 %------------------------------------------------------------------------
 % --- Executes on selection change in scalar menu.
@@ -801,20 +840,22 @@
     case 'attribute'
         set(handles.TimeName, 'Visible','on')% timeName menu represents the available attributes
-        time_index=[];
-        PreviousList=get(handles.TimeName, 'String');
-        index=[];
-        if ~isempty(PreviousList)
-            PreviousAttr=PreviousList{get(handles.TimeName, 'Value')};
-            index=find(strcmp(PreviousAttr,Field.Display.ListGlobalAttributes));
-        end
-        if isempty(index)
-            time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')));% index of the attributes containing the string 'Time'
-        end
+        time_index=get(handles.TimeName,'UserData');    %select the input data
+        if isempty(time_index)
+            PreviousList=get(handles.TimeName, 'String');
+            if ~isempty(PreviousList)
+                PreviousAttr=PreviousList{get(handles.TimeName, 'Value')};
+                index=find(strcmp(PreviousAttr,Field.Display.ListGlobalAttributes),1);
+            end
+        end
+        if isempty(time_index)
+            time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);% index of the attributes containing the string 'Time'
+        end      
         if ~isempty(time_index)
-            set(handles.TimeName,'Value',time_index(1))
+            set(handles.TimeName,'Value',time_index)
         else
             set(handles.TimeName,'Value',1)
         end
         set(handles.TimeName, 'String',Field.Display.ListGlobalAttribute)
+
     case 'variable'% TimeName menu represents the available variables
         set(handles.TimeName, 'Visible','on')
