source: trunk/src/read_field.m @ 527

Last change on this file since 527 was 527, checked in by sommeria, 9 years ago

various bugs corrected

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