source: trunk/src/read_field.m @ 648

Last change on this file since 648 was 648, checked in by sommeria, 7 years ago

get_field updated, several bugs corrected,open_uvmat suppressd

File size: 6.9 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_type.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% num: frame number for movies
23%
24% see also read_image.m,read_civxdata.m,read_civdata.m,
25
26function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
27Field=[];
28if ~exist('num','var')
29    num=1;
30end
31if ~exist('ParamIn','var')
32    ParamIn=[];
33end
34ParamOut=ParamIn;%default
35errormsg='';
36if ~exist(FileName,'file')
37    erromsg=['input file ' FileName ' does not exist'];
38    return
39end
40A=[];
41InputField={};
42check_colorvar=0;
43if isstruct(ParamIn)
44    if isfield(ParamIn,'FieldName')
45        if ischar(ParamIn.FieldName)
46            InputField={ParamIn.FieldName};
47        else
48            InputField= ParamIn.FieldName;
49        end
50    end
51    if isfield(ParamIn,'ColorVar')
52        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
53        check_colorvar=1;
54    end
55end
56
57%% distingush different input file types
58switch FileType
59    case 'civdata'
60        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
61        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
62        ParamOut.CivStage=Field.CivStage;
63    case 'civx'
64        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
65        [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
66        if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end
67        ParamOut.CivStage=Field.CivStage;
68    case 'netcdf'
69        ListVar={};
70        for ilist=1:numel(InputField)
71            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
72            if isempty(r)
73                ListVar=[ListVar InputField(ilist)];
74                Role{numel(ListVar)}='scalar';
75                ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
76            else
77                ListVar=[ListVar {r.UName,r.VName}];
78                Role{numel(ListVar)}='vector_y';
79                Role{numel(ListVar)-1}='vector_x';
80                            switch r.Operator
81                                case 'norm'
82                                    ProjModeRequest{numel(ListVar)-1}='interp_lin';%scalar field (requires interpolation for plot)
83                                    ProjModeRequest{numel(ListVar)}='interp_lin';
84                                otherwise
85                                   ProjModeRequest{numel(ListVar)-1}='';
86                                   ProjModeRequest{numel(ListVar)}='';
87                            end
88            end
89        end
90        if check_colorvar
91            Role{numel(ListVar)}='ancillary';% scalar used for color vector (not projected)
92        end
93        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
94            [Field,var_detect,ichoice]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
95        else
96        [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
97        end
98        if isfield(Field,'Txt')
99            errormsg=Field.Txt;
100            return
101        end
102        for ivar=1:numel(ListVar)
103            Field.VarAttribute{ivar+2}.Role=Role{ivar};
104            if isfield(ParamIn,'FieldName')
105                Field.VarAttribute{ivar+2}.FieldName=ParamIn.FieldName;
106            end
107            Field.VarAttribute{ivar+2}.ProjModeRequest=ProjModeRequest{ivar};
108        end       
109    case 'video'
110        if strcmp(class(ParamIn),'VideoReader')
111            A=read(ParamIn,num);
112        else
113            ParamOut=VideoReader(FileName);
114            A=read(ParamOut,num);
115        end
116    case 'mmreader'
117        if strcmp(class(ParamIn),'mmreader')
118            A=read(ParamIn,num);
119        else
120            ParamOut=mmreader(FileName);
121            A=read(ParamOut,num);
122        end
123    case 'vol'
124        A=imread(FileName);
125        Npz=size(A,1)/ParamIn.Npy;
126        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
127        A=permute(A,[3 2 1]);
128    case 'multimage'
129        warning 'off'
130        A=imread(FileName,num);
131    case 'image'
132        A=imread(FileName);
133end
134if ~isempty(errormsg)
135    errormsg=[FileType ' input: ' errormsg];
136    return
137end
138
139%% case of image
140if ~isempty(A)
141    if isstruct(ParamOut)
142        ParamOut.FieldName='image';
143    end
144    Npz=1;%default
145    npxy=size(A);
146    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
147    %     Rangy=[npxy(1)-0.5 0.5]; %
148    Field.NbDim=2;%default
149    Field.AName='image';
150    Field.ListVarName={'AY','AX','A'}; %
151    if ndims(A)==3
152        if Npz==1;%color
153            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
154            Field.AY=[npxy(1)-0.5 0.5];
155            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
156            if isstruct(ParamOut)
157                ParamOut.Npx=npxy(2);% display image size on the interface
158                ParamOut.Npy=npxy(1);
159            end
160            Field.VarAttribute{3}.Mesh=1;
161        else
162            Field.NbDim=3;
163            Field.ListVarName=['AZ' Field.ListVarName];
164            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
165            Field.AZ=[npxy(1)-0.5 0.5];
166            Field.AY=[npxy(2)-0.5 0.5];
167            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
168            if isstruct(ParamOut)
169                ParamOut.Npx=npxy(3);% display image size on the interface
170                ParamOut.Npy=npxy(2);
171            end
172            Field.VarAttribute{4}.Mesh=1;
173        end
174    else
175        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
176        Field.AY=[npxy(1)-0.5 0.5];
177        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
178        ParamOut.Npx=npxy(2);% display image size on the interface
179        ParamOut.Npy=npxy(1);
180        Field.VarAttribute{3}.Mesh=1;
181    end
182    Field.A=A;
183    Field.CoordUnit='pixel'; %used for mouse_motion
184end
185
186
187
Note: See TracBrowser for help on using the repository browser.