source: trunk/src/read_field.m @ 837

Last change on this file since 837 was 822, checked in by sommeria, 10 years ago

various

File size: 12.8 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%        .VelType
10%        .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2  (fix1)....     
11%        .Npx,.Npy: for images, nbre of pixels in x and y
12% errormsg: error message, ='' by default
13%
14%INPUT
15% FileName: name of the input file
16% FileType: type of file, as determined by the function get_file_info.m
17% ParamIn: movie object or Matlab structure of input parameters
18%     .FieldName: name (char string) of the input field (for Civx data)
19%     .VelType: char string giving the type of velocity data ('civ1', 'filter1', 'civ2'...)
20%     .ColorVar: variable used for vector color
21%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
22%     .TimeDimName: name of the dimension considered as 'time', selected index value then set by input 'num'   
23% num: frame number for movies
24%
25% see also read_image.m,read_civxdata.m,read_civdata.m,
26
27%=======================================================================
28% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
29%   http://www.legi.grenoble-inp.fr
30%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
31%
32%     This file is part of the toolbox UVMAT.
33%
34%     UVMAT is free software; you can redistribute it and/or modify
35%     it under the terms of the GNU General Public License as published
36%     by the Free Software Foundation; either version 2 of the license,
37%     or (at your option) any later version.
38%
39%     UVMAT is distributed in the hope that it will be useful,
40%     but WITHOUT ANY WARRANTY; without even the implied warranty of
41%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42%     GNU General Public License (see LICENSE.txt) for more details.
43%=======================================================================
44
45function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
46%% default output and check input
47Field=[];
48if ~exist('num','var')
49    num=1;
50end
51if ~exist('ParamIn','var')
52    ParamIn=[];
53end
54ParamOut=ParamIn;%default
55errormsg='';
56if ~exist(FileName,'file')
57    errormsg=['input file ' FileName ' does not exist'];
58    return
59end
60A=[];
61InputField={};
62check_colorvar=0;
63if isstruct(ParamIn)
64    if isfield(ParamIn,'FieldName')
65        if ischar(ParamIn.FieldName)
66            InputField={ParamIn.FieldName};
67        else
68            InputField= ParamIn.FieldName;
69        end
70    end
71    check_colorvar=zeros(size(InputField));
72    if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar)
73        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
74        check_colorvar(numel(InputField))=1;
75    end
76end
77
78%% distingush different input file types
79switch FileType
80    case 'civdata'% new format for civ results
81        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
82        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
83%         if ~isempty(strcmp('C',ParamIn.FieldName))% if C image correlation is requested as field (not color visu)
84%             ScalarIndex=strcmp('C',Field.ListVarName);
85%             Field.VarAttribute{ScalarIndex}.Role='scalar';%put role as 'scalar' instead of ancillary
86%         end     
87        ParamOut.CivStage=Field.CivStage;
88    case 'civx'% old (obsolete) format for civ results
89        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
90        [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
91        if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end
92        ParamOut.CivStage=Field.CivStage;
93    case 'netcdf'% general netcdf file (not recognized as civ)
94        ListVar={};
95        Role={};
96        ProjModeRequest={};
97        ListInputField={};
98        ListOperator={};
99        checkU=0;
100        checkV=0;
101        for ilist=1:numel(InputField)
102            % look for input variables to read
103            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
104            if isempty(r)%  no operator used
105                if isempty(find(strcmp(InputField{ilist},ListVar)))
106                    ListVar=[ListVar InputField(ilist)];%append the variable name if not already in the list
107                    ListInputField=[ListInputField InputField(ilist)];
108                    ListOperator=[ListOperator {''}];
109                end
110                if check_colorvar(ilist)
111                    Role{numel(ListVar)}='ancillary';% not projected with interpolation
112                    ProjModeRequest{numel(ListVar)}='';
113                else
114                    Role{numel(ListVar)}='scalar';
115                    ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
116                end
117            else  % an operator 'vec' or 'norm' is used
118                if ~check_colorvar(ilist) && strcmp(r.Operator,'norm')
119                    ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot)
120                else
121                    ProjModeRequestVar='';
122                end
123                ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName
124                ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName
125                if isempty(ind_var_U)
126                    ListVar=[ListVar {r.UName}]; % append the variable in the list if not previously listed
127                    Role=[Role {'vector_x'}];
128                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
129                    ListInputField=[ListInputField InputField(ilist)];
130                    %ListOperator=[ListOperator {[r.Operator '_U']}];
131                else
132                    checkU=1;
133                end
134                if isempty(ind_var_V)
135                    ListVar=[ListVar {r.VName}];% append the variable in the list if not previously listed
136                    Role=[Role {'vector_y'}];
137                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
138                    ListInputField=[ListInputField {''}];
139                    %ListOperator=[ListOperator {[r.Operator '_V']}];
140                else
141                    checkV=1;
142                end
143            end
144        end
145        if ~isfield(ParamIn,'Coord_z')
146            ParamIn.Coord_z=[];
147        end
148        NbCoord=~isempty(ParamIn.Coord_x)+~isempty(ParamIn.Coord_y)+~isempty(ParamIn.Coord_z);
149        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
150            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
151        elseif isfield(ParamIn,'TimeVarName')% case of reading of a single time  in a multidimensional array
152            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeVarName',ParamIn.TimeVarName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
153            NbCoord=NbCoord+1;% adds time coordinate
154        else
155            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
156        end
157        if ~isempty(errormsg)
158            return
159        end
160        %scan all the variables beyond the two first ones, ParamIn.Coord_x and ParamIn.Coord_y.
161        for ilist=NbCoord+1:numel(Field.VarDimName)
162            if isequal(Field.VarDimName{1},Field.VarDimName{ilist}) % if a variable has the same dimension as the coordinate, it denotes a field with unstructured coordinates
163                Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates
164                Field.VarAttribute{2}.Role='coord_y';
165                if NbCoord>=3
166                    Field.VarAttribute{3}.Role='coord_z';
167                end
168                break
169            end
170        end
171        NormName='';
172        UName='';
173        VName='';
174        for ilist=1:numel(ListVar)
175            Field.VarAttribute{ilist+NbCoord}.Role=Role{ilist};
176            Field.VarAttribute{ilist+NbCoord}.ProjModeRequest=ProjModeRequest{ilist};
177            if isfield(ParamIn,'FieldName')
178                Field.VarAttribute{ilist+NbCoord}.FieldName=ListInputField{ilist};
179            end
180            r=regexp(ListInputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
181            if ~isempty(r)&& strcmp(r.Operator,'norm')
182                NormName='norm';
183                if ~isempty(find(strcmp(ListVar,'norm')))
184                    NormName='norm_1';
185                end
186                Field.ListVarName=[Field.ListVarName {NormName}];
187                ilistmax=numel(Field.ListVarName);
188                Field.VarDimName{ilistmax}=Field.VarDimName{ilist+2};
189                Field.VarAttribute{ilistmax}.Role='scalar';
190                Field.(NormName)=Field.(r.UName).*Field.(r.UName)+Field.(r.VName).*Field.(r.VName);
191                Field.(NormName)=sqrt(Field.(NormName));
192                UName=r.UName;
193                VName=r.VName;
194            end
195        end
196        if ~isempty(NormName)% remove U and V if norm has been calculated and U and V are not needed as variables
197            ind_var_U=find(strcmp(UName,ListVar));%check previous listing of variable r.UName
198            ind_var_V=find(strcmp(VName,ListVar));%check previous listing of variable r.VName
199            if ~checkU && ~checkV
200                Field.ListVarName([ind_var_U+2 ind_var_V+2])=[];
201                Field.VarDimName([ind_var_U+2 ind_var_V+2])=[];
202                Field.VarAttribute([ind_var_U+2 ind_var_V+2])=[];
203            elseif ~checkU
204                Field.ListVarName(ind_var_U+2)=[];
205                Field.VarDimName(ind_var_U+2)=[];
206                Field.VarAttribute(ind_var_U+2 )=[];
207            elseif ~checkV
208                Field.ListVarName(ind_var_V+2)=[];
209                Field.VarDimName(ind_var_V+2)=[];
210                Field.VarAttribute(ind_var_V+2 )=[];
211            end
212        end
213    case 'video'
214        if strcmp(class(ParamIn),'VideoReader')
215            A=read(ParamIn,num);
216        else
217            ParamOut=VideoReader(FileName);
218            A=read(ParamOut,num);
219        end
220    case 'mmreader'
221        if strcmp(class(ParamIn),'mmreader')
222            A=read(ParamIn,num);
223        else
224            ParamOut=mmreader(FileName);
225            A=read(ParamOut,num);
226        end
227    case 'vol'
228        A=imread(FileName);
229        Npz=size(A,1)/ParamIn.Npy;
230        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
231        A=permute(A,[3 2 1]);
232    case 'multimage'
233      %  warning 'off'
234        A=imread(FileName,num);
235    case 'image'
236        A=imread(FileName);
237    case 'rdvision'
238        [A,FileInfo,timestamps]=read_rdvision(FileName,num);
239    otherwise
240        errormsg=[ FileType ': invalid input file type for uvmat'];
241end
242
243%% case of image
244if ~isempty(A)
245    if strcmp(FileType,'rdvision')
246        Field.Time=timestamps;
247    end
248    if isstruct(ParamOut)
249        ParamOut.FieldName='image';
250    end
251    Npz=1;%default
252    npxy=size(A);
253    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
254    %     Rangy=[npxy(1)-0.5 0.5]; %
255    Field.NbDim=2;%default
256    Field.AName='image';
257    Field.ListVarName={'Coord_y','Coord_x','A'}; %
258    if ndims(A)==3
259        if Npz==1;%color
260            Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x','rgb'}}; %
261            Field.Coord_y=[npxy(1)-0.5 0.5];
262            Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
263            if isstruct(ParamOut)
264                ParamOut.Npx=npxy(2);% display image size on the interface
265                ParamOut.Npy=npxy(1);
266            end
267            Field.VarAttribute{3}.Mesh=1;
268        else
269            Field.NbDim=3;
270            Field.ListVarName=['AZ' Field.ListVarName];
271            Field.VarDimName={'AZ','Coord_y','Coord_x',{'AZ','Coord_y','Coord_x'}};
272            Field.AZ=[npxy(1)-0.5 0.5];
273            Field.Coord_y=[npxy(2)-0.5 0.5];
274            Field.Coord_x=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
275            if isstruct(ParamOut)
276                ParamOut.Npx=npxy(3);% display image size on the interface
277                ParamOut.Npy=npxy(2);
278            end
279            Field.VarAttribute{4}.Mesh=1;
280        end
281    else
282        Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x'}}; %
283        Field.Coord_y=[npxy(1)-0.5 0.5];
284        Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
285        ParamOut.Npx=npxy(2);% display image size on the interface
286        ParamOut.Npy=npxy(1);
287        Field.VarAttribute{3}.Mesh=1;
288    end
289    Field.A=A;
290    Field.CoordUnit='pixel'; %used for mouse_motion
291       
292end
293
294
295
Note: See TracBrowser for help on using the repository browser.