source: trunk/src/read_field.m @ 521

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

various bugs corrected

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