source: trunk/src/read_field.m @ 641

Last change on this file since 641 was 635, checked in by sommeria, 11 years ago
File size: 6.6 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
51    if isfield(ParamIn,'ColorVar')
52        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
53        check_colorvar=1;
54    end
[521]55end
[575]56
[334]57%% distingush different input file types
[527]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                %                     FieldRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
[493]76            else
[527]77                ListVar=[ListVar {r.UName,r.VName}];
78                Role{numel(ListVar)}='vector_y';
79                Role{numel(ListVar)-1}='vector_x';
80                %                    TODO; introduce that for unstructured coordinates
81                %                     switch r.Operator TODO; introduce that for unstructured coordinates
82                %                         case 'norm'
83                %                             FieldRequest{numel(ListVar)-1}='interp_lin';%scalar field (requires interpolation for plot)
84                %                             FieldRequest{numel(ListVar)}='interp_lin';
85                %                         otherwise
86                %                            FieldRequest{numel(ListVar)-1}='';
87                %                     end
[493]88            end
[527]89        end
90        if check_colorvar
91            Role{numel(ListVar)}='ancillary';% scalar used for color vector (not projected)
92        end
[595]93        [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
[534]94        if isfield(Field,'Txt')
95            errormsg=Field.Txt;
96            return
97        end
[527]98        for ivar=1:numel(ListVar)
99            Field.VarAttribute{ivar+2}.Role=Role{ivar};
100            %                 Field.VarAttribute{ivar+2}.FieldRequest=FieldRequest{ivar};
101        end
102       
103    case 'video'
104        if strcmp(class(ParamIn),'VideoReader')
105            A=read(ParamIn,num);
106        else
107            ParamOut=VideoReader(FileName);
108            A=read(ParamOut,num);
109        end
110    case 'mmreader'
111        if strcmp(class(ParamIn),'mmreader')
112            A=read(ParamIn,num);
113        else
114            ParamOut=mmreader(FileName);
115            A=read(ParamOut,num);
116        end
117    case 'vol'
118        A=imread(FileName);
119        Npz=size(A,1)/ParamIn.Npy;
120        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
121        A=permute(A,[3 2 1]);
122    case 'multimage'
123        warning 'off'
124        A=imread(FileName,num);
125    case 'image'
126        A=imread(FileName);
127end
128if ~isempty(errormsg)
129    errormsg=[FileType ' input: ' errormsg];
[445]130    return
[334]131end
[445]132
[334]133%% case of image
134if ~isempty(A)
[452]135    if isstruct(ParamOut)
[580]136        ParamOut.FieldName='image';
[452]137    end
[182]138    Npz=1;%default
[181]139    npxy=size(A);
[580]140    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
141    %     Rangy=[npxy(1)-0.5 0.5]; %
[181]142    Field.NbDim=2;%default
143    Field.AName='image';
144    Field.ListVarName={'AY','AX','A'}; %
145    if ndims(A)==3
146        if Npz==1;%color
147            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
148            Field.AY=[npxy(1)-0.5 0.5];
149            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[452]150            if isstruct(ParamOut)
[580]151                ParamOut.Npx=npxy(2);% display image size on the interface
152                ParamOut.Npy=npxy(1);
[452]153            end
[186]154            Field.VarAttribute{3}.Mesh=1;
[181]155        else
156            Field.NbDim=3;
157            Field.ListVarName=['AZ' Field.ListVarName];
158            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
159            Field.AZ=[npxy(1)-0.5 0.5];
160            Field.AY=[npxy(2)-0.5 0.5];
161            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
[452]162            if isstruct(ParamOut)
[580]163                ParamOut.Npx=npxy(3);% display image size on the interface
164                ParamOut.Npy=npxy(2);
165            end
[186]166            Field.VarAttribute{4}.Mesh=1;
[181]167        end
168    else
169        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
170        Field.AY=[npxy(1)-0.5 0.5];
171        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[182]172        ParamOut.Npx=npxy(2);% display image size on the interface
173        ParamOut.Npy=npxy(1);
[186]174        Field.VarAttribute{3}.Mesh=1;
[181]175    end
176    Field.A=A;
177    Field.CoordUnit='pixel'; %used for mouse_motion
178end
179
180
[334]181
Note: See TracBrowser for help on using the repository browser.