source: trunk/src/read_field.m @ 477

Last change on this file since 477 was 472, checked in by sommeria, 12 years ago

many bugs repaired. series set to work in mode background

File size: 10.0 KB
Line 
1
2%'read_field': read input fields in different formats
3%--------------------------------------------------------------------------
4%  function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
5%
6% OUTPUT:
7% Field: matlab structure representing the field
8% ParamOut: structure representing parameters:
9%        .FieldName; field name
10%        .FieldList: menu of possible fields
11%        .VelType
12%        .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2  (fix1)....     
13%        .Npx,.Npy: for images, nbre of pixels in x and y
14% errormsg: error message, ='' by default
15%
16%INPUT
17% FileName: name of the input file
18% FileType: type of file
19%     = netcdf : netcdf file
20%     = image : usual image as recognised by Matlab
21%     = multimage: image series stored in a single file
22%     = mmreader: movie read with mmreader
23%     = video: movie read with VideoReader (recent versions of Matlab)
24%     = vol: images representing scanned volume (images concatened in the y direction)
25% ParamIn: movie object or Matlab structure of input parameters
26%     .FieldName: name (char string) of the input field (for Civx data)
27%     .VelType: char string giving the type of velocity data ('civ1', 'filter1', 'civ2'...)
28%     .ColorVar: variable used for vector color
29%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
30% num: frame number for movies
31
32function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
33Field=[];
34if ~exist('num','var')
35    num=1;
36end
37if ~exist('ParamIn','var')
38    ParamIn=[];
39end
40ParamOut=ParamIn;%default
41errormsg='';
42% if isfield(ParamIn,'VelType')
43% VelType=ParamIn.VelType;
44% end
45A=[];
46%% distingush different input file types
47try
48    switch FileType
49        case {'civx','civdata','netcdf'}  %read the first nc field
50            %         ParamOut.FieldName=ParamIn.FieldName;
51            GUIName='get_field'; %default name of the GUI get_field
52            if isfield(ParamIn,'GUIName')
53                GUIName=ParamIn.GUIName;
54            end
55            CivStage=0;
56            if ~strcmp(ParamIn.FieldName,'get_field...')% if get_field is not requested, look for Civx data
57                FieldList=calc_field;%list of possible fields for Civx data
58                ParamOut.ColorVar='';%default
59                if ischar(ParamIn.FieldName)
60                    FieldName=ParamIn.FieldName;
61                else
62                    FieldName=ParamIn.FieldName{1};
63                end
64                field_index=strcmp(FieldName,FieldList);%look for ParamIn.FieldName in the list of possible fields for Civx data
65                if isempty(find(field_index,1))% ParamIn.FieldName is not in the list, check whether Civx data exist
66                    Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ','CivStage');
67                    % case of new civdata conventions
68                    if isequal(Data.Conventions,'uvmat/civdata')
69                        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
70                        ParamOut.ColorVar='ima_cor';
71                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
72                        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType,Data.CivStage);
73                        if ~isempty(errormsg),errormsg=['read_civdata:' errormsg];return,end
74                        CivStage=Field.CivStage;
75                        ParamOut.CivStage=Field.CivStage;
76                        %case of old civx conventions
77                    elseif ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0)
78                        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
79                        ParamOut.ColorVar='ima_cor';
80                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
81                        [Field,ParamOut.VelType]=read_civxdata(FileName,InputField,ParamIn.VelType);
82                        if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
83                        CivStage=Field.CivStage;
84                        ParamOut.CivStage=Field.CivStage;
85                        % not cvix file, fields will be chosen through the GUI get_field
86                    else
87                        ParamOut.FieldName='get_field...';
88                        hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
89                        if ~isempty(hget_field)
90                            delete(hget_field)%delete  get_field for reinitialisation
91                        end
92                    end
93                else             
94                    InputField=ParamOut.FieldName;
95                    if ischar(InputField)
96                        InputField={InputField};
97                    end
98                    if isfield(ParamIn,'ColorVar')
99                        ParamOut.ColorVar=ParamIn.ColorVar;
100                        InputField=[InputField {ParamOut.ColorVar}];
101                    end
102                    [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
103                    if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
104                    CivStage=Field.CivStage;
105                    ParamOut.CivStage=Field.CivStage;
106                end
107                ParamOut.FieldList=[{'image'};FieldList;{'get_field...'}];
108            end
109            if CivStage==0% read the field names on the interface get_field.
110                hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
111                if isempty(hget_field)% open the GUI get_field if it is not found
112                    hget_field= get_field(FileName);%open the get_field GUI
113                    set(hget_field,'Name',GUIName)%update the name of get_field (e.g. get_field_1)
114                end
115                hhget_field=guidata(hget_field);
116                %% update  the get_field GUI
117                set(hhget_field.inputfile,'String',FileName)
118                set(hhget_field.list_fig,'Value',1)
119                if exist('num','var')&&~isnan(num)
120                    set(hhget_field.TimeIndexValue,'String',num2str(num))
121                end
122                funct_list=get(hhget_field.ACTION,'UserData');
123                funct_index=get(hhget_field.ACTION,'Value');
124                funct=funct_list{funct_index};%select  the current action in get_field, e;g. PLOT
125                Field=funct(hget_field); %%activate the current action selected in get_field, e;g.read the names of the variables to plot
126                Tabchar={''};%default
127                Tabcell=[];
128                set(hhget_field.inputfile,'String',FileName)
129                if isfield(Field,'ListGlobalAttribute')&& ~isempty(Field.ListGlobalAttribute)
130                    for iline=1:length(Field.ListGlobalAttribute)
131                        Tabcell{iline,1}=Field.ListGlobalAttribute{iline};
132                        if isfield(Field, Field.ListGlobalAttribute{iline})
133                            val=Field.(Field.ListGlobalAttribute{iline});
134                            if ischar(val);
135                                Tabcell{iline,2}=val;
136                            else
137                                Tabcell{iline,2}=num2str(val);
138                            end
139                        end
140                    end
141                    if ~isempty(Tabcell)
142                        Tabchar=cell2tab(Tabcell,'=');
143                        Tabchar=[{''};Tabchar];
144                    end
145                end
146                ParamOut.CivStage=0;
147                ParamOut.VelType=[];
148                if isfield(Field,'TimeIndex')
149                    ParamOut.TimeIndex=Field.TimeIndex;
150                end
151                if isfield(Field,'TimeValue')
152                    ParamOut.TimeValue=Field.TimeValue;
153                end
154                ParamOut.FieldList={'get_field...'};
155            end
156        case {'video','mmreader'}
157            A=read(ParamIn,num);
158        case 'vol'
159            A=imread(FileName);
160            Npz=size(A,1)/ParamIn.Npy;
161            A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
162            A=permute(A,[3 2 1]);
163        case 'multimage'
164            warning 'off'
165            A=imread(FileName,num);
166        case 'image'
167            A=imread(FileName);
168    end
169catch ME
170    errormsg=[FileType ' input: ' ME.message];
171    return
172end
173
174%% case of image
175if ~isempty(A)
176    if isstruct(ParamOut)
177    ParamOut.FieldName='image';
178    ParamOut.FieldList={'image'};
179    end
180    Npz=1;%default
181    npxy=size(A);
182%     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
183%     Rangy=[npxy(1)-0.5 0.5]; %
184    Field.NbDim=2;%default
185    Field.AName='image';
186    Field.ListVarName={'AY','AX','A'}; %
187    if ndims(A)==3
188        if Npz==1;%color
189            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
190            Field.AY=[npxy(1)-0.5 0.5];
191            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
192            if isstruct(ParamOut)
193            ParamOut.Npx=npxy(2);% display image size on the interface
194            ParamOut.Npy=npxy(1);
195            end
196            Field.VarAttribute{3}.Mesh=1;
197        else
198            Field.NbDim=3;
199            Field.ListVarName=['AZ' Field.ListVarName];
200            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
201            Field.AZ=[npxy(1)-0.5 0.5];
202            Field.AY=[npxy(2)-0.5 0.5];
203            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
204            if isstruct(ParamOut)
205            ParamOut.Npx=npxy(3);% display image size on the interface
206            ParamOut.Npy=npxy(2);
207             end
208            Field.VarAttribute{4}.Mesh=1;
209        end
210    else
211        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
212        Field.AY=[npxy(1)-0.5 0.5];
213        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
214        ParamOut.Npx=npxy(2);% display image size on the interface
215        ParamOut.Npy=npxy(1);
216        Field.VarAttribute{3}.Mesh=1;
217    end
218    Field.A=A;
219    Field.CoordUnit='pixel'; %used for mouse_motion
220end
221
222
223
Note: See TracBrowser for help on using the repository browser.