source: trunk/src/read_field.m @ 493

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

various bugs repaired, tests in Windows OS

File size: 10.3 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'
157            if strcmp(class(ParamIn),'VideoReader')
158                A=read(ParamIn,num);
159            else
160                ParamOut=VideoReader(FileName);
161                A=read(ParamOut,num);
162            end
163        case 'mmreader'
164            if strcmp(class(ParamIn),'mmreader')
165                A=read(ParamIn,num);
166            else
167                ParamOut=mmreader(FileName);
168                A=read(ParamOut,num);
169            end
170        case 'vol'
171            A=imread(FileName);
172            Npz=size(A,1)/ParamIn.Npy;
173            A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
174            A=permute(A,[3 2 1]);
175        case 'multimage'
176            warning 'off'
177            A=imread(FileName,num);
178        case 'image'
179            A=imread(FileName);
180    end
181catch ME
182    errormsg=[FileType ' input: ' ME.message];
183    return
184end
185
186%% case of image
187if ~isempty(A)
188    if isstruct(ParamOut)
189    ParamOut.FieldName='image';
190    ParamOut.FieldList={'image'};
191    end
192    Npz=1;%default
193    npxy=size(A);
194%     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
195%     Rangy=[npxy(1)-0.5 0.5]; %
196    Field.NbDim=2;%default
197    Field.AName='image';
198    Field.ListVarName={'AY','AX','A'}; %
199    if ndims(A)==3
200        if Npz==1;%color
201            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
202            Field.AY=[npxy(1)-0.5 0.5];
203            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
204            if isstruct(ParamOut)
205            ParamOut.Npx=npxy(2);% display image size on the interface
206            ParamOut.Npy=npxy(1);
207            end
208            Field.VarAttribute{3}.Mesh=1;
209        else
210            Field.NbDim=3;
211            Field.ListVarName=['AZ' Field.ListVarName];
212            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
213            Field.AZ=[npxy(1)-0.5 0.5];
214            Field.AY=[npxy(2)-0.5 0.5];
215            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
216            if isstruct(ParamOut)
217            ParamOut.Npx=npxy(3);% display image size on the interface
218            ParamOut.Npy=npxy(2);
219             end
220            Field.VarAttribute{4}.Mesh=1;
221        end
222    else
223        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
224        Field.AY=[npxy(1)-0.5 0.5];
225        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
226        ParamOut.Npx=npxy(2);% display image size on the interface
227        ParamOut.Npy=npxy(1);
228        Field.VarAttribute{3}.Mesh=1;
229    end
230    Field.A=A;
231    Field.CoordUnit='pixel'; %used for mouse_motion
232end
233
234
235
Note: See TracBrowser for help on using the repository browser.