source: trunk/src/read_field.m @ 681

Last change on this file since 681 was 681, checked in by sommeria, 11 years ago

mouse action improved for translations, use of magenta color to indicate that REFRESH is needed

File size: 9.2 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)
[466]22% num: frame number for movies
[497]23%
24% see also read_image.m,read_civxdata.m,read_civdata.m,
[466]25
[450]26function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[181]27Field=[];
[354]28if ~exist('num','var')
29    num=1;
30end
31if ~exist('ParamIn','var')
32    ParamIn=[];
33end
34ParamOut=ParamIn;%default
[181]35errormsg='';
[635]36if ~exist(FileName,'file')
37    erromsg=['input file ' FileName ' does not exist'];
38    return
39end
[334]40A=[];
[575]41InputField={};
42check_colorvar=0;
[526]43if isstruct(ParamIn)
[575]44    if isfield(ParamIn,'FieldName')
45        if ischar(ParamIn.FieldName)
[576]46            InputField={ParamIn.FieldName};
[575]47        else
48            InputField= ParamIn.FieldName;
49        end
[526]50    end
[654]51    check_colorvar=zeros(size(InputField));
[526]52    if isfield(ParamIn,'ColorVar')
53        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
[654]54        check_colorvar(numel(InputField))=1;
[526]55    end
[521]56end
[575]57
[334]58%% distingush different input file types
[527]59switch FileType
60    case 'civdata'
61        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
62        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
63        ParamOut.CivStage=Field.CivStage;
64    case 'civx'
65        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
66        [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
67        if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end
68        ParamOut.CivStage=Field.CivStage;
69    case 'netcdf'
70        ListVar={};
[675]71        Role={};
72        ProjModeRequest={};
[681]73        checkU=0;
74        checkV=0;
[527]75        for ilist=1:numel(InputField)
76            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
[681]77            Operator='';
[654]78            if isempty(r)%  no operator used
[681]79                if isempty(find(strcmp(InputField{ilist},ListVar)))
80                ListVar=[ListVar InputField(ilist)];%append the variable name if not already in the list
81                end
[654]82                if check_colorvar(ilist)
[667]83                    Role{numel(ListVar)}='ancillary';% not projected with interpolation
84                    ProjModeRequest{numel(ListVar)}='';
[654]85                else
[667]86                    Role{numel(ListVar)}='scalar';
87                    ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
[654]88                end
89            else  % an operator 'vec' or 'norm' is used
[681]90                Operator=r.Operator;
[667]91                if ~check_colorvar(ilist) && strcmp(r.Operator,'norm')
[675]92                    ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot)
[667]93                else
[675]94                    ProjModeRequestVar='';
[667]95                end
[675]96                ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName
97                ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName
98                if isempty(ind_var_U)
99                    ListVar=[ListVar r.UName]; % append the variable in the list if not previously listed
100                    Role=[Role {'vector_x'}];
101                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
[681]102                else
103                    checkU=1;
[675]104                end
105                if isempty(ind_var_V)
106                    ListVar=[ListVar r.VName];% append the variable in the list if not previously listed
107                    Role=[Role {'vector_y'}];
108                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
[681]109                else
110                    checkV=1;
[675]111                end
[493]112            end
[527]113        end
[648]114        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
115            [Field,var_detect,ichoice]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
116        else
[667]117            [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
[648]118        end
[534]119        if isfield(Field,'Txt')
120            errormsg=Field.Txt;
121            return
122        end
[675]123        for ilist=3:numel(Field.VarDimName)
124            if isequal(Field.VarDimName{1},Field.VarDimName{ilist})
125                Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates
126                Field.VarAttribute{2}.Role='coord_y';
127                break
128            end
129        end
[527]130        for ivar=1:numel(ListVar)
131            Field.VarAttribute{ivar+2}.Role=Role{ivar};
[648]132            if isfield(ParamIn,'FieldName')
133                Field.VarAttribute{ivar+2}.FieldName=ParamIn.FieldName;
134            end
135            Field.VarAttribute{ivar+2}.ProjModeRequest=ProjModeRequest{ivar};
[667]136        end
[681]137        if strcmp(Operator,'norm')
138             NormName='norm';
139            if ~isempty(strcmp(ListVar,'norm'))
140                NormName='norm_1';
141            end
142            Field.ListVarName=[Field.ListVarName {NormName}];
143            ilist=numel(Field.ListVarName);
144            Field.VarDimName{ilist}=Field.VarDimName{ind_var_U};
145            Field.VarDimName{ilist}.Role='scalar';
146            Field.(NormName)=Field.(r.UName).*Field.(r.UName)+Field.(r.VName).*Field.(r.VName);
147            Field.(NormName)=sqrt(Field.(NormName));
148            if ~checkU && ~checkV
149                Field.ListVarName([ind_var_U ind_var_V])=[];
150                Field.VarDimName([ind_var_U ind_var_V])=[];
151                Field.VarAttribute([ind_var_U ind_var_V])=[];
152            elseif ~checkU
153                Field.ListVarName(ind_var_U)=[];
154                Field.VarDimName(ind_var_U)=[];
155                Field.VarAttribute(ind_var_U )=[];
156            elseif ~checkV
157                                Field.ListVarName(ind_var_V)=[];
158                Field.VarDimName(ind_var_V)=[];
159                Field.VarAttribute(ind_var_V )=[];
160            end
161        end
[527]162    case 'video'
163        if strcmp(class(ParamIn),'VideoReader')
164            A=read(ParamIn,num);
165        else
166            ParamOut=VideoReader(FileName);
167            A=read(ParamOut,num);
168        end
169    case 'mmreader'
170        if strcmp(class(ParamIn),'mmreader')
171            A=read(ParamIn,num);
172        else
173            ParamOut=mmreader(FileName);
174            A=read(ParamOut,num);
175        end
176    case 'vol'
177        A=imread(FileName);
178        Npz=size(A,1)/ParamIn.Npy;
179        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
180        A=permute(A,[3 2 1]);
181    case 'multimage'
182        warning 'off'
183        A=imread(FileName,num);
184    case 'image'
185        A=imread(FileName);
186end
187if ~isempty(errormsg)
188    errormsg=[FileType ' input: ' errormsg];
[445]189    return
[334]190end
[445]191
[334]192%% case of image
193if ~isempty(A)
[452]194    if isstruct(ParamOut)
[580]195        ParamOut.FieldName='image';
[452]196    end
[182]197    Npz=1;%default
[181]198    npxy=size(A);
[580]199    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
200    %     Rangy=[npxy(1)-0.5 0.5]; %
[181]201    Field.NbDim=2;%default
202    Field.AName='image';
203    Field.ListVarName={'AY','AX','A'}; %
204    if ndims(A)==3
205        if Npz==1;%color
206            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
207            Field.AY=[npxy(1)-0.5 0.5];
208            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[452]209            if isstruct(ParamOut)
[580]210                ParamOut.Npx=npxy(2);% display image size on the interface
211                ParamOut.Npy=npxy(1);
[452]212            end
[186]213            Field.VarAttribute{3}.Mesh=1;
[181]214        else
215            Field.NbDim=3;
216            Field.ListVarName=['AZ' Field.ListVarName];
217            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
218            Field.AZ=[npxy(1)-0.5 0.5];
219            Field.AY=[npxy(2)-0.5 0.5];
220            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
[452]221            if isstruct(ParamOut)
[580]222                ParamOut.Npx=npxy(3);% display image size on the interface
223                ParamOut.Npy=npxy(2);
224            end
[186]225            Field.VarAttribute{4}.Mesh=1;
[181]226        end
227    else
228        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
229        Field.AY=[npxy(1)-0.5 0.5];
230        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[182]231        ParamOut.Npx=npxy(2);% display image size on the interface
232        ParamOut.Npy=npxy(1);
[186]233        Field.VarAttribute{3}.Mesh=1;
[181]234    end
235    Field.A=A;
236    Field.CoordUnit='pixel'; %used for mouse_motion
237end
238
239
[334]240
Note: See TracBrowser for help on using the repository browser.