source: trunk/src/read_field.m @ 769

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

corrections to be able to process image correlation in series

File size: 11.7 KB
RevLine 
[497]1%'read_field': read the fields from files in different formats (netcdf files, images, video)
[181]2%--------------------------------------------------------------------------
[450]3%  function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[181]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
[466]15% FileName: name of the input file
[497]16% FileType: type of file, as determined by the function get_file_type.m
[450]17% ParamIn: movie object or Matlab structure of input parameters
[445]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'...)
[181]20%     .ColorVar: variable used for vector color
21%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
[693]22%     .TimeDimName: name of the dimension considered as 'time', selected index value then set by input 'num'   
[466]23% num: frame number for movies
[497]24%
25% see also read_image.m,read_civxdata.m,read_civdata.m,
[466]26
[450]27function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[748]28%% default output and check input
[181]29Field=[];
[354]30if ~exist('num','var')
31    num=1;
32end
33if ~exist('ParamIn','var')
34    ParamIn=[];
35end
36ParamOut=ParamIn;%default
[181]37errormsg='';
[635]38if ~exist(FileName,'file')
[747]39    errormsg=['input file ' FileName ' does not exist'];
[635]40    return
41end
[334]42A=[];
[575]43InputField={};
44check_colorvar=0;
[526]45if isstruct(ParamIn)
[575]46    if isfield(ParamIn,'FieldName')
47        if ischar(ParamIn.FieldName)
[576]48            InputField={ParamIn.FieldName};
[575]49        else
50            InputField= ParamIn.FieldName;
51        end
[526]52    end
[654]53    check_colorvar=zeros(size(InputField));
[684]54    if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar)
[526]55        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
[654]56        check_colorvar(numel(InputField))=1;
[526]57    end
[521]58end
[575]59
[334]60%% distingush different input file types
[527]61switch FileType
[748]62    case 'civdata'% new format for civ results
[527]63        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
64        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
[769]65        if ~isempty(strcmp('C',ParamIn.FieldName))% if C image correlation is requested as field (not color visu)
66            ScalarIndex=strcmp('C',Field.ListVarName);
67            Field.VarAttribute{ScalarIndex}.Role='scalar';%put role as 'scalar' instead of ancillary
68        end     
[527]69        ParamOut.CivStage=Field.CivStage;
[748]70    case 'civx'% old (obsolete) format for civ results
[527]71        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
72        [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
73        if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end
74        ParamOut.CivStage=Field.CivStage;
[748]75    case 'netcdf'% general netcdf file (not recognized as civ)
[527]76        ListVar={};
[675]77        Role={};
78        ProjModeRequest={};
[684]79        ListInputField={};
80        ListOperator={};
[681]81        checkU=0;
82        checkV=0;
[527]83        for ilist=1:numel(InputField)
[748]84            % look for input variables to read
[527]85            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
[654]86            if isempty(r)%  no operator used
[681]87                if isempty(find(strcmp(InputField{ilist},ListVar)))
[684]88                    ListVar=[ListVar InputField(ilist)];%append the variable name if not already in the list
89                    ListInputField=[ListInputField InputField(ilist)];
90                    ListOperator=[ListOperator {''}];
[681]91                end
[654]92                if check_colorvar(ilist)
[667]93                    Role{numel(ListVar)}='ancillary';% not projected with interpolation
94                    ProjModeRequest{numel(ListVar)}='';
[654]95                else
[667]96                    Role{numel(ListVar)}='scalar';
97                    ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
[654]98                end
99            else  % an operator 'vec' or 'norm' is used
[667]100                if ~check_colorvar(ilist) && strcmp(r.Operator,'norm')
[675]101                    ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot)
[667]102                else
[675]103                    ProjModeRequestVar='';
[667]104                end
[675]105                ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName
106                ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName
107                if isempty(ind_var_U)
[684]108                    ListVar=[ListVar {r.UName}]; % append the variable in the list if not previously listed
[675]109                    Role=[Role {'vector_x'}];
110                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
[684]111                    ListInputField=[ListInputField InputField(ilist)];
112                    %ListOperator=[ListOperator {[r.Operator '_U']}];
[681]113                else
114                    checkU=1;
[675]115                end
116                if isempty(ind_var_V)
[684]117                    ListVar=[ListVar {r.VName}];% append the variable in the list if not previously listed
[675]118                    Role=[Role {'vector_y'}];
119                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
[684]120                    ListInputField=[ListInputField {''}];
121                    %ListOperator=[ListOperator {[r.Operator '_V']}];
[681]122                else
123                    checkV=1;
[675]124                end
[493]125            end
[527]126        end
[748]127        if ~isfield(ParamIn,'Coord_z')
128            ParamIn.Coord_z=[];
129        end
130        NbCoord=~isempty(ParamIn.Coord_x)+~isempty(ParamIn.Coord_y)+~isempty(ParamIn.Coord_z);
[648]131        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
[748]132            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
[747]133        elseif isfield(ParamIn,'TimeVarName')% case of reading of a single time  in a multidimensional array
[748]134            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeVarName',ParamIn.TimeVarName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
135            NbCoord=NbCoord+1;% adds time coordinate
[648]136        else
[748]137            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
[648]138        end
[747]139        if ~isempty(errormsg)
[534]140            return
141        end
[748]142        %scan all the variables beyond the two first ones, ParamIn.Coord_x and ParamIn.Coord_y.
143        for ilist=NbCoord+1:numel(Field.VarDimName)
144            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
[675]145                Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates
146                Field.VarAttribute{2}.Role='coord_y';
[748]147                if NbCoord>=3
148                    Field.VarAttribute{3}.Role='coord_z';
149                end
[675]150                break
151            end
152        end
[684]153        NormName='';
154        UName='';
155        VName='';
156        for ilist=1:numel(ListVar)
[748]157            Field.VarAttribute{ilist+NbCoord}.Role=Role{ilist};
158            Field.VarAttribute{ilist+NbCoord}.ProjModeRequest=ProjModeRequest{ilist};
[648]159            if isfield(ParamIn,'FieldName')
[748]160                Field.VarAttribute{ilist+NbCoord}.FieldName=ListInputField{ilist};
[648]161            end
[684]162            r=regexp(ListInputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
163            if ~isempty(r)&& strcmp(r.Operator,'norm')
164                NormName='norm';
165                if ~isempty(find(strcmp(ListVar,'norm')))
166                    NormName='norm_1';
167                end
168                Field.ListVarName=[Field.ListVarName {NormName}];
169                ilistmax=numel(Field.ListVarName);
170                Field.VarDimName{ilistmax}=Field.VarDimName{ilist+2};
171                Field.VarAttribute{ilistmax}.Role='scalar';
172                Field.(NormName)=Field.(r.UName).*Field.(r.UName)+Field.(r.VName).*Field.(r.VName);
173                Field.(NormName)=sqrt(Field.(NormName));
174                UName=r.UName;
175                VName=r.VName;
[681]176            end
177        end
[684]178        if ~isempty(NormName)% remove U and V if norm has been calculated and U and V are not needed as variables
179            ind_var_U=find(strcmp(UName,ListVar));%check previous listing of variable r.UName
180            ind_var_V=find(strcmp(VName,ListVar));%check previous listing of variable r.VName
[769]181            if ~checkU && ~checkV
182                Field.ListVarName([ind_var_U+2 ind_var_V+2])=[];
183                Field.VarDimName([ind_var_U+2 ind_var_V+2])=[];
184                Field.VarAttribute([ind_var_U+2 ind_var_V+2])=[];
185            elseif ~checkU
186                Field.ListVarName(ind_var_U+2)=[];
187                Field.VarDimName(ind_var_U+2)=[];
188                Field.VarAttribute(ind_var_U+2 )=[];
189            elseif ~checkV
190                Field.ListVarName(ind_var_V+2)=[];
191                Field.VarDimName(ind_var_V+2)=[];
192                Field.VarAttribute(ind_var_V+2 )=[];
193            end
[684]194        end
[527]195    case 'video'
196        if strcmp(class(ParamIn),'VideoReader')
197            A=read(ParamIn,num);
198        else
199            ParamOut=VideoReader(FileName);
200            A=read(ParamOut,num);
201        end
202    case 'mmreader'
203        if strcmp(class(ParamIn),'mmreader')
204            A=read(ParamIn,num);
205        else
206            ParamOut=mmreader(FileName);
207            A=read(ParamOut,num);
208        end
209    case 'vol'
210        A=imread(FileName);
211        Npz=size(A,1)/ParamIn.Npy;
212        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
213        A=permute(A,[3 2 1]);
214    case 'multimage'
215        warning 'off'
216        A=imread(FileName,num);
217    case 'image'
218        A=imread(FileName);
219end
220if ~isempty(errormsg)
221    errormsg=[FileType ' input: ' errormsg];
[445]222    return
[334]223end
[445]224
[334]225%% case of image
226if ~isempty(A)
[452]227    if isstruct(ParamOut)
[580]228        ParamOut.FieldName='image';
[452]229    end
[182]230    Npz=1;%default
[181]231    npxy=size(A);
[580]232    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
233    %     Rangy=[npxy(1)-0.5 0.5]; %
[181]234    Field.NbDim=2;%default
235    Field.AName='image';
236    Field.ListVarName={'AY','AX','A'}; %
237    if ndims(A)==3
238        if Npz==1;%color
239            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
240            Field.AY=[npxy(1)-0.5 0.5];
241            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[452]242            if isstruct(ParamOut)
[580]243                ParamOut.Npx=npxy(2);% display image size on the interface
244                ParamOut.Npy=npxy(1);
[452]245            end
[186]246            Field.VarAttribute{3}.Mesh=1;
[181]247        else
248            Field.NbDim=3;
249            Field.ListVarName=['AZ' Field.ListVarName];
250            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
251            Field.AZ=[npxy(1)-0.5 0.5];
252            Field.AY=[npxy(2)-0.5 0.5];
253            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
[452]254            if isstruct(ParamOut)
[580]255                ParamOut.Npx=npxy(3);% display image size on the interface
256                ParamOut.Npy=npxy(2);
257            end
[186]258            Field.VarAttribute{4}.Mesh=1;
[181]259        end
260    else
261        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
262        Field.AY=[npxy(1)-0.5 0.5];
263        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[182]264        ParamOut.Npx=npxy(2);% display image size on the interface
265        ParamOut.Npy=npxy(1);
[186]266        Field.VarAttribute{3}.Mesh=1;
[181]267    end
268    Field.A=A;
269    Field.CoordUnit='pixel'; %used for mouse_motion
270end
271
272
[334]273
Note: See TracBrowser for help on using the repository browser.