source: trunk/src/read_field.m @ 526

Last change on this file since 526 was 526, checked in by sommeria, 12 years ago

bugs corrected; Posiibility of usual 1D plots introduced with the new way of using get_field. time_series and aver_stat updated

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